Is VertexBuffer Lock/Unlock appropriate in this case?

Started by
5 comments, last by Prasoon Pankaj 12 years, 12 months ago
In my render loop I'm trying to change alpha value overtime to make objects to fade-in/out:


var blendedFillColor = /* color remains the same, just changing the alpha value */;
var vertices = (CustomVertex.TransformedColored[])_vertexBuffer.Lock(0, 0);
for (int vertexIndex = 0; vertexIndex < 4; vertexIndex++)
{
vertices[vertexIndex].Color = blendedFillColor.ToArgb();
}
_vertexBuffer.Unlock();

dxDevice.SetStreamSource(0, _vertexBuffer, 0);
dxDevice.VertexFormat = CustomVertex.TransformedColored.Format;
dxDevice.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2);


Is Lock() and Unlock() operation on the vertex buffer a performance hit? And also can I avoid it in my case?
Advertisement
It's a performance cost, but pretty much an unavoidable one. You might be able to change some shared color somewhere or use some clever shader tricks, but building vertices at runtime is pretty much the simplest way to submit sprites and particles. You should be fine as long as you keep the number of Lock() and Unlock() calls to a minimum. For example, Lock() once, submit a bunch of geometry, Unlock(), and then call DrawPrimitive to draw everything.
Unlocking/locking the vertex buffer is a bad idea for performance reasons as kdmilller said. This is a perfect example of needing a vertex/pixel shader.
Instead of changing the information per vertex, why not set an alpha value per object? There isnt a need to change it per vertex when it can be set in the shader.
Wisdom is knowing when to shut up, so try it.
--Game Development http://nolimitsdesigns.com: Reliable UDP library, Threading library, Math Library, UI Library. Take a look, its all free.

Instead of changing the information per vertex, why not set an alpha value per object? There isnt a need to change it per vertex when it can be set in the shader.


Can you please share few links to the stuff you are talking about? Or may be a sample code to illustrate.
http://msdn.microsoft.com/en-us/library/bb206368%28v=vs.85%29.aspx
http://msdn.microsoft.com/en-us/library/ee416223%28v=vs.85%29.aspx

You should research ID3DEffect for directx
Wisdom is knowing when to shut up, so try it.
--Game Development http://nolimitsdesigns.com: Reliable UDP library, Threading library, Math Library, UI Library. Take a look, its all free.
It was giving me some hit. So, I changed the logic to use CustomVertex.Transformed[] and used the following logic in my render loop:


var blendedFillColor = /* color remains the same, just changing the alpha value */;

device.TextureState[0].ConstantColor = blendedFillColor;
device.TextureState[0].ColorOperation = TextureOperation.SelectArg1;
device.TextureState[0].AlphaOperation = TextureOperation.SelectArg1;
device.TextureState[0].ColorArgument1 = TextureArgument.Constant;
device.TextureState[0].AlphaArgument1 = TextureArgument.Constant;

device.SetStreamSource(0, this._vertexBuffer, 0);
device.VertexFormat = CustomVertex.Transformed.Format;
device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2);


Anything you guys wanna suggest?

This topic is closed to new replies.

Advertisement