[SOLVED] How to update (parts of the) Dynamic Vertex Buffers?

Hi guys,

I hope someone has experience with this.

How does vertexBuffer.SetData work?

To be more accurate: How do I update parts of the vertexBuffer only?

vertexBuffer.SetData(vertices) gives me no problem.

But let’s say I want to change the 3 and 4th vertex element only.

vertexBuffer.SetData(2, vertices, 2, 2, stride);

somehow results in some broken mess. I am fairly certain my stride size is calculated correctly (sizeof(float)*6 for vector3, vector2, single).

Can anyone with experience enlighten me or provide examples of how he dealt with that?

thanks :slight_smile:

The first parameter is offsetInBytes. A value of 2 bytes does not look right to me.

You probably want to keep the first parameter as 0. The startIndex and elementCount look ok.

ah yes, I realized that later, I only used 0 and elements*stride as an offset in the actual code for the first parameter.

Either way,

vertexBuffer.SetData(0, vertices, 0, 4, stride);

doesn’t work for me either, is it possible that i somehow got my bytesize wrong?

public struct TrailVertex
    {
        // Stores the starting position of the particle.
        public Vector3 Position;

        // Stores TexCoords
        public Vector2 TextureCoordinate;

        // Visibility term
        public float Visibility;

        public static readonly VertexDeclaration VertexDeclaration = new VertexDeclaration
            (
            new VertexElement(0, VertexElementFormat.Vector3,
                VertexElementUsage.Position, 0),
            new VertexElement(sizeof(float) * 3, VertexElementFormat.Vector2,
                VertexElementUsage.TextureCoordinate, 0),
            new VertexElement(sizeof(float) * 3 + sizeof(float) * 2, VertexElementFormat.Single,
                VertexElementUsage.TextureCoordinate, 1)
            );

        public const int SizeInBytes = sizeof(float)*6; //24
    }

plus later on in the implementation

int stride = TrailVertex.SizeInBytes;
_vBuffer.SetData(0,vertices,0,4,stride,SetDataOptions.None);

or

_vBuffer.SetData(stride*4,vertices,4,4,stride,SetDataOptions.None);

both have heavy artifacts, so I guess it must be the stride size right?

Does TrailVertex.VertexDeclaration.VertexStride agree with your calculation? From what it looks like, it should work. There are some unit tests for this. Perhaps they’re missing something?

Yep, TrailVertex.VertexDeclaration.VertexStride is also 24.

I’ll try to check out some more cases tomorrow, but I think this is a problem specific to me and my implementation, because as far as I am aware the particle systems, which I just copied from the MS XNA samples, work without problems.

Note:
If I use this to basically copy the whole array like so
int stride = TrailVertex.SizeInBytes;
_vBuffer.SetData(0, vertices, 0, vertices.Length, stride, SetDataOptions.None);

it works without problems. Only when I use smaller parts do these crop up.

EDIT:
Ok I checked and rechecked and I got the bug - you were right, I missed the offsetInBytes in one function, but I also had a different problem where I rendered vertices that had an index but were not initialized yet.
Thanks :slight_smile: