How to use multiple vertex streams with SetVertexBufferData()?

Hello. Hoping an expert in the ‘advanced mesh api’ can offer me some advice.

My question relates to the use of two methods (click for the documentation):

  1. Mesh.SetVertexBufferParams()
  2. Mesh.SetVertexBufferData()

The second of these takes a parameter ‘stream’ which apparently lets us set the vertex data for multiple streams.

I have a test rig working perfectly when just using the default stream (0) but I’m having trouble trying to introduce a second stream.

I assume that I need to set up two separate VertexBufferParams like so:

VertexAttributeDescriptor[] customVertexStream1 = new[] {
      new VertexAttributeDescriptor(VertexAttribute.Position, VertexAttributeFormat.Float32, 3),
};

VertexAttributeDescriptor[] customVertexStream2 = new[] {
      new VertexAttributeDescriptor(VertexAttribute.Normal, VertexAttributeFormat.Float32, 3),
      new VertexAttributeDescriptor(VertexAttribute.TexCoord0, VertexAttributeFormat.Float32, 2),
};

But when I use Mesh.SetVertexBufferParams() there doesn’t appear to be a way to set it for a stream other than the default:

mesh.SetVertexBufferParams(vertexBuffer1.Count, customVertexStream1); // Where's the stream parameter?

There’s no parameter for the stream number like there is for SetVertexBufferData.

Whatever I try, I always get the same exception when trying to call SetVertexBufferData with stream = 1 rather than 0:

SetVertexData() with out-of-bounds arguments; would need to copy 810000 bytes at offset 0 into a 0 byte buffer
This is the same exception you get if you fail to call SetVertexBufferParams() before calling SetVertexBufferData with stream = 0, so I’m pretty sure the issue is the SetVertexBufferParams().

I’ve looked through all the documentation, even the Mesh.cs source code. My question is simple. How do you SetVertexBufferParams() for a stream other than 0?

Just to avoid the obvious questions: I know I can just combine all my data into a single stream. I’ve done that and everything works perfectly. However I would like to try the multiple stream approach because it will open up some potential optimisations with how I generate the vertex buffer data.

Hoping it’s a just a simple step that I’m missing somewhere. Thanks!

Stream is part of VertexAttributeDescriptor, so (typing out of my head), it would be like (note I added the last aguments):

VertexAttributeDescriptor[] customVertexStreams = new[] {
      new VertexAttributeDescriptor(VertexAttribute.Position, VertexAttributeFormat.Float32, 3, stream:0),
      new VertexAttributeDescriptor(VertexAttribute.Normal, VertexAttributeFormat.Float32, 3, stream:1),
      new VertexAttributeDescriptor(VertexAttribute.TexCoord0, VertexAttributeFormat.Float32, 2, stream:1),
};
1 Like

Doh! Thank you so much for the quick reply. I knew it would be something obvious that I missed. It’s right here in the documentation.

Everything is working beautifully now. :slight_smile:

I tried your method, But another SetVertexBufferParams will cover before one, how can i set two stream to transfer data?