Writing to buffer

A place for users of OGRE to discuss ideas and experiences of utilitising OGRE in their games / demos / applications.
Post Reply
kubatp
Gnome
Posts: 354
Joined: Tue Jan 06, 2009 1:12 pm
x 7

Writing to buffer

Post by kubatp » Sun Sep 27, 2015 7:28 am

Hi,
I have read manual about reading/writing to hardware buffer http://www.ogre3d.org/docs/manual/manual_53.html, 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
0 x

User avatar
dark_sylinc
OGRE Team Member
OGRE Team Member
Posts: 4075
Joined: Sat Jul 21, 2007 4:55 pm
Location: Buenos Aires, Argentina
x 231
Contact:

Re: Writing to buffer

Post by dark_sylinc » Sun Sep 27, 2015 5:04 pm

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 );
vertexBuffer->unlock();

++currentFrameIndex;
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.
0 x

kubatp
Gnome
Posts: 354
Joined: Tue Jan 06, 2009 1:12 pm
x 7

Re: Writing to buffer

Post by kubatp » Sun Sep 27, 2015 8:37 pm

Hi,
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?
0 x

User avatar
dark_sylinc
OGRE Team Member
OGRE Team Member
Posts: 4075
Joined: Sat Jul 21, 2007 4:55 pm
Location: Buenos Aires, Argentina
x 231
Contact:

Re: Writing to buffer

Post by dark_sylinc » Sun Sep 27, 2015 9:31 pm

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.
0 x

kubatp
Gnome
Posts: 354
Joined: Tue Jan 06, 2009 1:12 pm
x 7

Re: Writing to buffer

Post by kubatp » Mon Sep 28, 2015 7:07 am

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
0 x

Post Reply