0
\$\begingroup\$

I have a particle system. So far it worked like this: I have a dynamic vertex buffer for a system, which is created with a size that can hold for example 100 000 particles. I map/unmap this and write the new data into it every frame.

But what if the particle count gets bigger than the buffer can hold? I thought of recreating the vertex buffer with the double of its previous capacity (then map/unmap into it). Is this the right direction for this or should I solve it in a different way?

A short example:

ID3D11VertexBuffer* buffer;
ID3D11Device*       graphicsDevice;
//...
D3D11_BUFFER_DESC desc;
buffer->GetDesc(&desc);
if(dataSize>(int)desc.ByteWidth){ //data can't fit, so destroy and recreate
    buffer->Release();
    desc.ByteWidth*=2;
    graphicsDevice->CreateBuffer( &desc, nullptr, &buffer ); //returns S_OK
}

Update:

I want to use it for other things, not just particles but for example instanced meshes. If I spawn a couple of instances, I'd only like to resize a buffer, without creating an other one.

\$\endgroup\$
1
\$\begingroup\$

If your particle count is bigger than the buffer can hold then your best option is to draw in two batches:

  • Draw a first batch of particles that fill the buffer
  • Draw a second batch of anything left over

That can extend to any arbitrarily high particle count (just add more batches) and avoids the overhead of runtime resource destruction and creation (which can be expensive since D3D11 performs validation at creation time).

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ What about if I want to use a solution which can be used for instanced meshes? At first I create an instance buffer which can hold 10 instances for example, but as I spawn some more objects, I'd like to resize this buffer, because two more (or so) instances would not require a second buffer by a long shot. And updated the question. \$\endgroup\$ – János Turánszki Jan 31 '15 at 15:19
  • \$\begingroup\$ In that case you're out of luck because D3D doesn't allow resizing of buffers; you must destroy and recreate. Either size your buffer for the max number of objects you expect to draw, or else make it dynamic and reuse it for a second batch (note: I'm not suggesting 2 or more buffers here; I'm suggesting reusing a single dynamic buffer). \$\endgroup\$ – Maximus Minimus Feb 1 '15 at 0:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.