Sign in to follow this  
Followers 0
qorthos

Writing to a dynamic vertex buffer

3 posts in this topic

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
0

Share this post


Link to post
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
1

Share this post


Link to post
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.

0

Share this post


Link to post
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.

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0