# Writing to a dynamic vertex buffer

This topic is 2056 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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

##### Share on other sites
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

##### Share on other sites

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.

##### Share on other sites

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.

1. 1
2. 2
Rutin
18
3. 3
4. 4
5. 5
frob
13

• 9
• 9
• 23
• 16
• 9
• ### Forum Statistics

• Total Topics
632610
• Total Posts
3007403

×