• Create Account

## Writing to a dynamic vertex buffer

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

3 replies to this topic

### #1qorthos  Members

167
Like
0Likes
Like

Posted 04 February 2013 - 12:25 PM

Hello everyone, I'm currently instancing a set of primitives (cubes, spheres, etc) with modulo arithmetic and I'm having wit the DataStream.WriteRange<> method.

I'm using this to write to the InstanceBuffer:

DataStream dsInstances = instanceBuffer.Map(MapMode.WriteDiscard, SlimDX.Direct3D10.MapFlags.None);

if (instancesStart + instancesCount < instancesMax)
{
dsInstances.Position = instancesStart;
dsInstances.WriteRange<VertexPrimitiveInstance>(instances, instancesStart, instancesCount);
}
else
{
dsInstances.Position = instancesStart;
dsInstances.WriteRange<VertexPrimitiveInstance>(instances, instancesStart, instancesMax - instancesStart);

dsInstances.Position = 0;
dsInstances.WriteRange<VertexPrimitiveInstance>(instances, 0, instancesCount - (instancesMax - instancesStart));
}

instanceBuffer.Unmap();


I see nothing the first loop around from 0 to InstancesMax.  However, I start seeing my objects once I've written all values to the buffer at least once.  Then I get some very strange flicker and occasionally deformed geometry.

If I change the above to write the entire array of VertexPrimitiveInstances to the buffer, I get no flicker.

            DataStream dsInstances = instanceBuffer.Map(MapMode.WriteDiscard, SlimDX.Direct3D10.MapFlags.None);

dsInstances.Position = 0;
dsInstances.WriteRange<VertexPrimitiveInstance>(instances);

instanceBuffer.Unmap();


I presume that the second method is far slower than the first (if if worked) and is thus undesirable.  Am I using WriteRange wrong, or am I wrong in thinking I should only set the vertices that are changing (like I did in XNA)?

Edited by qorthos, 04 February 2013 - 12:28 PM.

### #2eppo  Members

4599
Like
1Likes
Like

Posted 04 February 2013 - 01:34 PM

Mapping with WRITE_DISCARD invalidates the contents of the buffer, so you'll have to update the entire buffer as you do in the second method.
You're effectively given a new buffer, which avoids stalls when the GPU is still using the resource.

Edited by eppo, 04 February 2013 - 01:40 PM.

### #3qorthos  Members

167
Like
0Likes
Like

Posted 04 February 2013 - 02:01 PM

Mapping with WRITE_DISCARD invalidates the contents of the buffer, so you'll have to update the entire buffer as you do in the second method.
You're effectively given a new buffer, which avoids stalls when the GPU is still using the resource.

Right, and I think I'm okay with doing.  The vertices that I'm setting in the first method are the only vertices that I'm trying to drawn that frame.

### #4qorthos  Members

167
Like
0Likes
Like

Posted 04 February 2013 - 03:27 PM

Hah, setting DataStream.Position sets the byte location to write at, I was using the index in the array.  I need to multiply the position by the size of the instance.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.