Note that there are some explanatory texts on larger screens.

plurals
  1. POTrying to pass struct with Vector4 array to cbuffer
    primarykey
    data
    text
    <p>I'm playing around with SlimDX (with DX10/11) a bit and I got some nice results so far, but I'm stuck on passing an array of Vector4s to the shaders.</p> <p>If I try the following struct:</p> <pre><code>[StructLayout(LayoutKind.Sequential)] public struct PerFrameBuffer { public Vector4 Position; public Matrix World; public Matrix View; public Matrix Projection; [MarshalAs( UnmanagedType.ByValArray, SizeConst=2)] public Vector4[] Lights; } </code></pre> <p>and try to store it in the following cbuffer:</p> <pre><code>cbuffer cPerFrame : register( b0 ) { float4 xPosition; matrix xWorld; matrix xView; matrix xProjection; float4 pointLights[2]; } </code></pre> <p>I don't get any errors, but the values in the matrices are broken.</p> <p>However when I declare my struct in c# as follows:</p> <pre><code>[StructLayout(LayoutKind.Sequential)] public struct PerFrameBuffer { public Vector4 Position; public Matrix World; public Matrix View; public Matrix Projection; public Vector4 Light1; public Vector4 Light2; } </code></pre> <p>Then everything works fine as expected.</p> <p>I don't really want to make 64 fields in the struct and fill them up like that (I could with reflection but still). How can I use an array to pass the Vector4's to my constantbuffer?</p> <p>If by any means this is a dumb idea and should be done in another way, please let me know. It's my first time programming directx and shader related stuff.</p> <p>To be complete:</p> <p>Declaration of the buffer in C#</p> <pre><code> cPerFrameBuffer = new SlimDX.Direct3D11.Buffer(device, new BufferDescription { Usage = ResourceUsage.Default, SizeInBytes = Marshal.SizeOf(typeof(PerFrameBuffer)), BindFlags = BindFlags.ConstantBuffer }); context.VertexShader.SetConstantBuffer(cPerFrameBuffer, 0); </code></pre> <p>How I'm updating the buffer:</p> <pre><code>var cb = new AbstractDXManager.PerFrameBuffer() { Position = new Vector4(camera.Position, 1.0f), Projection = Matrix.Transpose(camera.ProjectionMatrix), View = Matrix.Transpose(camera.ViewMatrix), World = Matrix.Transpose(Matrix.Identity), }; cb.Lights = new Vector4[2]; cb.Lights[0] = cb.Position; cb.Lights[1] = new Vector4(20, 20, 20, 1); var context = device.ImmediateContext; using (DataStream data = new DataStream(Marshal.SizeOf(typeof(PerFrameBuffer)), true, true)) { data.Write(cb); data.Position = 0; context.UpdateSubresource(new DataBox(0, 0, data), cPerFrameBuffer, 0); } </code></pre> <p>Thanks in advance</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload