Advice for a more efficient particle system.

Started by
5 comments, last by 21st Century Moose 11 years, 2 months ago

I need some some advice on my particle system.

Right now I have a particle emitter which holds a list of particles and each particle has a VBO for its vertices and uvs.

Which is bad I know this, it with drawing only 150 particles, I go from just over 100 draw calls, to 3000 draw calls using gDEBugger.

Each particle keeps track of its own local and world matrix.

I know the general idea is that I need to create a single VBO for each piece of data in the particle emitter, but then how to I pass the mvp for each particle, my particles are not always going to start at the same size.

Do I do something like this:


int i = 0;

// Pass in new mvp here and any other data that may have changed like colour

glDrawArrays(GL_TRIANGLE_STRIP, i, 4);

i+= 4;

My code is below:


// Particle Initialization

uvs.push_back(Vector2(1,1)); 
	vertices.push_back(Vector3(
		0+(particleInfo.sizeX/2),
			0+(particleInfo.sizeY/2),
			0));

	uvs.push_back(Vector2(0,1)); 
	vertices.push_back(Vector3(
			0-(particleInfo.sizeX/2),
			0+(particleInfo.sizeY/2),
			0));

	uvs.push_back(Vector2(1,0)); 
	vertices.push_back(Vector3(
			0+(particleInfo.sizeX/2),
			0-(particleInfo.sizeY/2),
			0));

	uvs.push_back(Vector2(0,0)); 
	vertices.push_back(Vector3(
			0-(particleInfo.sizeX/2),
			0-(particleInfo.sizeY/2),
			0));

// This is just a bind function.


// Binding the Data

void Particle::bind()
{
	glGenBuffers(1, &vertexbuffer);
	glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
	glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vector3), &vertices[0], GL_STATIC_DRAW);

	glGenBuffers(1, &uvbuffer);
	glBindBuffer(GL_ARRAY_BUFFER, uvbuffer);
	glBufferData(GL_ARRAY_BUFFER, uvs.size() * sizeof(Vector2), &uvs[0], GL_STATIC_DRAW);
}

Here is where I update the particle based on the properties.


// Updating the Matrix

/* removed for space */

localMatrix = Matrix4::toMatrix4(rotation,position,scale);

Drawing the Particles


void Particle::draw()
{
	if(alive)
	{
		glEnable(GL_BLEND);
		glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);

		glEnable(GL_DEPTH_TEST);
		glDepthFunc(GL_ALWAYS);

		Camera *cam = this->particleInfo.scene->getActivePerspectiveCamera();

		worldMatrix = cam->getObjectToWorld().inverse() * localMatrix;

		Matrix4 mvp = cam->getProjectionMatrix() * worldMatrix;

		glUniformMatrix4fv((*particleInfo.shader)["mvp"], 1, GL_FALSE, mvp.transpose().getArray());

		glUniform4f((*particleInfo.shader)["colour"],
			particleInfo.colour.R,
			particleInfo.colour.G,
			particleInfo.colour.B,
			particleInfo.colour.A);

		glEnableVertexAttribArray(0);
		glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
		glVertexAttribPointer(
			(*particleInfo.shader)["modelVertex"],	// attribute
			3,							// size
			GL_FLOAT,					// type
			GL_FALSE,					// normalized?
			0,							// stride
			(void*)0					// array buffer offset
		);

		// 2nd attribute buffer : UVs
		glEnableVertexAttribArray(1);
		glBindBuffer(GL_ARRAY_BUFFER, uvbuffer);
		glVertexAttribPointer(
			(*particleInfo.shader)["uv"],			// attribute
			2,							// size
			GL_FLOAT,					// type
			GL_FALSE,					// normalized?
			0,							// stride
			(void*)0					// array buffer offset
		);

		// Draw the triangle !
		glDrawArrays(GL_TRIANGLE_STRIP, 0, vertices.size());

		glDisableVertexAttribArray(0);
		glDisableVertexAttribArray(1);

		glDepthFunc(GL_LESS);

		glDisable(GL_BLEND);

	}
}

Particle Emitter Draw


void ParticleEmitter::draw()
{
	Component::draw();

	if(running)
	{
		iter = particles.begin();

		this->getShader()->begin();

		textureDiffuse->bindTexture((*this->getShader())["diffuseTextureSampler"],0);
	
		while (iter != particles.end())
		{
			(*iter)->draw();
			++iter;
		}

		glBindTexture(GL_TEXTURE_2D, 0);

		this->getShader()->end();	
	}
}
Advertisement

You don’t pass a matrix for every particle. You calculate the positions of the vertices manually on the CPU and you put the already-transformed coordinates into the buffer.

Your shader should account for this by excluding any “world” transforms from its calculations.

L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

You don’t pass a matrix for every particle. You calculate the positions of the vertices manually on the CPU and you put the already-transformed coordinates into the buffer.

Your shader should account for this by excluding any “world” transforms from its calculations.

L. Spiro

So what I should do then is hold the coordinates in the particle, then fetch them with the emitter every update and bind them to a buffer.


glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vector3), &vertices[0], GL_STATIC_DRAW);

Using this, But isnt it bad to change the size of a buffer every time particles are added.

Should I delete and recreate the buffer or just reuse it?

You are forgetting that you are not required to draw the whole buffer. Nor are you required to draw it from its first index.

Also, the buffer should be dynamic, not static.

It should never need resizing.

Firstly, you should start adding vertices from the last place on the buffer where you have no added any. Then draw that segment, and keep adding vertices after that point.

Don’t draw a part of the buffer and then start overwriting that part. Walk the buffer sequentially like a ring buffer.

When you run out of room, just issue a draw call from there and then continue walking over the buffer back at its start.

You may (definitely) also want to double-buffer these and change buffers each frame.

L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

to add to L. Spiro's answer, when updating the vbo(which should be dynamic), do not use glBufferData, instead map the buffer, and write the values like so:


//assumed that everything is setup for drawing. 
MyVertexStruct *VBOMap = (MyVertexStruct*)
glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY); 
int o=0; 
for(int i=0;i<ParticlesCount;i++,o+=sizeof(MyVertexStruct)){   
 if(o>=BufferSize){    
  glUnMapBuffer(GL_ARRAY_BUFFER);    
  glDrawArrays(GL_TRIANGLES, 0, o);    
  VBOMap = (MyVertexStruct*)glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);    
  o=0;
 }
 (VBOMap+o).xyz = Particle[i].xyz;
}
glUnMapBuffer(GL_ARRAY_BUFFER);
glDrawArrays(GL_TRIANGLES, 0, o)
//other stuff.
Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.

A couple of questions.

how do I create my buffers; like this?


glGenBuffers(1, &vertexbuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vector3), &vertices[0], GL_DYNAMIC_DRAW);

That wont work because I if I create my buffer before any particle has been created and added to the list it wont work. Can I just Generate the buffer and bind it, without using glBufferData?

In the previous post you are using a array of whatever my struct is, which in this case is my Vector3 and Vector2 classes.

In addition, I am trying to figure out how to pass my colour property now for each particle, for that I guess I just need to create another VBO to how colour for each vertex, which seems like overkill since there is 1 colour per particle.

Going on to the glMapBuffer, and in general your code, you are using a single buffer to hold all the data write? where as I create a buffer for each type of data, I have never used the method you are using before.

How exactly does MapBuffer work? how can I update my code as in seen in the first post? Why do you have two glDrawArrays, is it because you create a buffer of arbitrary size or do I never use glBufferData. Before your code I would need to bind the buffer as well.

That's how you create a buffer, but there are a few things missing from your understanding.

You generally will create a buffer at some fixed size, so pick a maximum number of particles that you're going to have (some number like 65536 is a nice arbitrary figure) and create it at that size. Pass NULL for the data parameter and GL will just give you an empty buffer, ready to be used.

GL_STREAM_DRAW is often a better choice than GL_DYNAMIC_DRAW for this kind of usage, by the way. The usage types are divided into how often you're going to draw from the buffer versus how often you're going to be updating it, so GL_STATIC_DRAW means that you'll never (or rarely) be updating the buffer, GL_DYNAMIC_DRAW means that you'll be drawing from it a few times before updating, GL_STREAM_DRAW means that you'll be drawing from it exactly once before updating - perfect for data that can change every frame like particles.

Create the buffer once only, during program startup. Creating resources every frame is expensive. While a buffer object may seem like just a chunk of memory containing data, in this case it's better to think of it as if it were a file (not an exact comparison though). Don't worry about the fact that you'll be drawing some variable number of particles each frame - the next bit will deal with that.

glMapBufferRange is better than glMapBuffer for this kind of updating. Append to the part of the buffer that's already been written to (or 0 if none of it has yet been written to), when you run out of space reset your "buffer offset" counter to 0 and make the next glMapBufferRange call with GL_INVALIDATE_BUFFER_BIT. Adjust the parameters of your Draw call to draw from the range you've just written. Done. This way you don't need to worry about double-buffering and the driver will handle everything for you that you would otherwise have to write a lot of code for yourself.

Mapping a buffer just gives you a pointer that you can write to; at draw time you can then rely on what you wrote to that pointer being in the buffer. The pointer may be to the actual backing store used by the buffer object itself, or it may be to an intermediate store provided by the driver but you don't need to know or worry about that - what happens in between that write and draw is none of your concern; the driver will manage this for you, so just use the API the way it was designed to be used and you'll have minimal problems.

I mentioned a "maximum size" above, and the the preceding discussion deals nicely with cases where the number of particles you need to draw in any given frame is below this size. What if it's above? Just run the process twice (or more); divide your particles into groups of 65535 (or whatever the maximum number you chose was) and Map, write, Unmap, Draw multiple times (in this case you'll want to use GL_INVALIDATE_BUFFER_BIT each time). If you're hitting this regularly the solution is easy - just make the buffer bigger (not always possible if you're targetting hardware with more limited memory, of course).

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

This topic is closed to new replies.

Advertisement