Jump to content
  • Advertisement
Sign in to follow this  
Gavin Williams

[SlimDX] How do I use an array instead of a stream when constructing a VertexBuffer

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi,

I'm trying to convert some XNA code to SlimDX, so I have a sphere generator which under XNA uses a vertex array, but I can only find examples of code that use a vertex stream in SlimDX, can anyone please show how to use arrays or point me to a code sample.

Thank you.

Share this post


Link to post
Share on other sites
Advertisement
It looks like I have to use streams, here is how i did it. This way I can still keep my arrays and just write them into the stream with stream.WriteRange(vertices);


// create the stream containing the vertex data
DataStream stream = new DataStream(vertices.Length * VertexPositionColorNormal.SizeInBytes, true, true);
stream.WriteRange(vertices);
stream.Position = 0;

// create vertex buffer
VertexBuffer = new Buffer(device11, stream, vertices.Length*VertexPositionColorNormal.SizeInBytes, ResourceUsage.Default, BindFlags.VertexBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0);
stream.Dispose();

Share this post


Link to post
Share on other sites
You can pass a managed array into the constructor of a DataStream, which it'll use as a backing store:

[source lang="csharp"]
// create the stream containing the vertex data
DataStream stream = new DataStream(vertices, true, true);

// create vertex buffer
vertexBuffer = new Buffer(device11, stream, vertices.Length*VertexPositionColorNormal.SizeInBytes, ResourceUsage.Default, BindFlags.VertexBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0);
stream.Dispose();
[/source]

The data stream may use it as a backing store, but it doesn't "own" the buffer, so it won't be disposed of when the stream is disposed. Instead the GCHandle (the array gets pinned into memory) is freed, if this was your concern in making sure you keep your arrays after setting up your vertex buffer?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!