Page 1 of 1

Writing to buffer

Posted: Sun Sep 27, 2015 7:28 am
by kubatp
I have read manual about reading/writing to hardware buffer, but it is not perfectly clear to me, what is the best practice to ONLY write to the buffer. Is it also HBL_NO_OVERWRITE?

Thank you

Re: Writing to buffer

Posted: Sun Sep 27, 2015 5:04 pm
by dark_sylinc
Usually the best practice to write to the buffer is to use HBL_DISCARD (assuming you will overwrite the entire buffer's contents).

HBL_NO_OVERWRITE is a very useful optimization that should be used instead of HBL_DISCARD for vertex data that changes very often, if you do it well.
When you use NO_OVERWRITE, you're promising you will write to contents of the buffer which are not yet in use by the GPU.

One common pattern is to do the following (using a triple buffer scheme, hence multiply by 3):

Code: Select all

vertexBuffer = createVertexBuffer( vertexCount * 3 );

renderOp.vertexStart = vertexCount * (currentFrameIndex % 3); //Offset the buffer to use the 1st region, then the 2nd, then 3rd, then 1st again.
renderOp.vertexCount = vertexCount;

//Use discard when we wrap around, since by then the GPU may be using all three regions
flag = (currentFrameIndex % 3) == 0 ? HBL_DISCARD : HBL_NO_OVERWRITE;

void *data = vertexBuffer->lock( flag );
memcpy( data + vertexCount * (currentFramIndex % 3), srcData, vertexCount );

In other words, we create a buffer 3 times its size, and then we write to each third in each frame; and when we need to "wrap around" back to start, we issue a Discard. This is basically a performance optimization.

Re: Writing to buffer

Posted: Sun Sep 27, 2015 8:37 pm
by kubatp
thank you very much for you reply.

I am actually using HBL_DISCARD now, but I was wondering if there is a better solution, because I need to change usually just few bytes of the texture.

What I have is a texture (used in pixel shader) where I pass some information about the scene. The problem is that I need to update it from time to time (so it is already used in GPU).
Is HBL_DISCARD really the only option?

Re: Writing to buffer

Posted: Sun Sep 27, 2015 9:31 pm
by dark_sylinc
The problem of not using discard is that your CPU will stall waiting until the GPU finishes using it.

I'm afraid you're between a rock and a hard place on this one. Either upload more data to avoid a stall, or stall and only upload a few bytes.

In Ogre 2.1 since we don't use D3D9 anymore you can use a staging buffer to prevent this situation and upload a few bytes optimally (basically you upload the few bytes to a temporary region that is stall free then this region will be copied to the actual location you want at a later place in the GPU). However it doesn't work for textures yet (it only works for buffers like uniform buffers, texture buffers, vertex buffers, etc) as the Texture interface wasn't overhauled yet.

But in Ogre 1.x which mantain compatibility with older APIs, that's the best you can get.

Re: Writing to buffer

Posted: Mon Sep 28, 2015 7:07 am
by kubatp
Hello again,
I understand. I just wanted to know if there is a better option worth trying or this is the best I can get so far.
Thank you very much for you replies again