   [MDX] Problem with GraphicsBuffer [Solved!]
  HelloI'm trying to do the following:public Geometry Create<T>(T[] v) where T : Vertex{D3D.VertexBuffer vb = new D3D.VertexBuffer(device, defaultBufferSize, D3D.Usage.WriteOnly, (D3D.VertexFormats)v[0].Format, D3D.Pool.Managed, null);// load the verticesGraphicsBuffer<T> gb = vb.Lock<T>(0, 0, D3D.LockFlags.None);gb.Write(v);vb.Unlock();...}Its a method for creating and filling a vertex buffer with vertices of arbitrary type. In the future the class with this method is supposed to manage all vertex buffers.It does not work because the generic parameter of the GraphicsBuffer has to be a value type (the constraint Vertex is an interface that all my custom vertex classes derives from). I can change the constraint on the method to struct, but then I can't get information about vertex stride size and vertex format (unless I pass the information on as arguments to the method, which I would like to avoid). Another option is to cast the vertices of type T[] to byte[] somehow and use the non-generic GraphicBuffer to write data. Since the GraphicsBuffer<T>.Write() method does not contain any overloads for writing offsetted data I would need the non-generic version in the future anyway.. So any ideas on how to solve this?ThanksOpwiz[Edited by - Opwiz on May 25, 2006 7:55:13 AM]
  I guess what I really need to know is how to convert any array into a byte array.   ;  Found a solution after looking around and finding this thread: Converting Objects to bytes? C# This seems to work fine:public Geometry Create<T>(T[] v) where T : Vertex{D3D.VertexBuffer vb = new D3D.VertexBuffer(device, defaultBufferSize, D3D.Usage.WriteOnly, (D3D.VertexFormats)v[0].Format, D3D.Pool.Managed, null);// load the verticesGraphicsBuffer gb = vb.Lock(0, 0, D3D.LockFlags.None);int size = Marshal.SizeOf(v[0]);byte[] bytes = new byte[size];IntPtr buffer = Marshal.AllocHGlobal(size);for(int i = 0; i < v.Length; ++i){Marshal.StructureToPtr(v, buffer, false);Marshal.Copy(buffer, bytes, 0, size);gb.Write(bytes);}Marshal.FreeHGlobal(buffer);vb.Unlock();...};  This is very ineffectiv. ;)1.You don’t need to create a HGlobal memory block. You can use UnsafeAddrOfPinnedArrayElement to get an IntPtr for the first byte of your byte array instead.  Don’t foget to look the array with a GCHandle. This makes the copy operation unnecessary.2.A graphics stream object has a DataBuffer (InternalData  in MDX1) property. This property is an IntPtr to the first byte in your looked buffer.  You can use this IntPtr with the StrutureToPtr. The get an IntPtr to the second vertex in the buffer you need to convert the IntPtr to an int or long (depends on the system you are running) add the size of an vertex and create a new IntPtr.  This will save you the internal copy that the write method does.There is an alternative solution for this problem.  You can build an object that provides a stream interface for a graphics buffer object. Than you can use a BinaryWriter with this stream to write all the base types to the Stream.  This makes it possible to add a method to your vertex data classes that writes the data to a BinaryWriter.  Additional you can use this method to save your vertex to a file. ;Quote:Original post by DemirugThis is very ineffectiv. ;)...Ok thanks :). However, looks like I don't need to convert to byte array right now anyway. Apparently you can have more than one constraint so I could add the "struct" constraint and use the generic GraphicsBuffer.   