# problem defining the VS input struct

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

## Recommended Posts

I have run into a problem when I tried to add the third property to my input struct, and I cannot see what I'm doing wrong, but I'm sure it is something obvious.

I have defined my C# struct, the shader struct and the layout. Checking the vertex buffer I see the POS at the right position, and 1,2,3,4 for the 'normal' vector4 and 11,22,33,44 for the last property.

When I debug the shader code with the graphical editor (can't figure out how to debug the code using the AMD PerfStudio) then I see the input struct contains 1,2,3,4 for the POS property, and the other 2 shows 'cannot display'.

If I remove Test from the Vertex struct without changing anything else, then it works.

Any idea what I'm missing?

C# SharpDX definition

[Serializable, StructLayout(LayoutKind.Sequential, Pack=4)]
public struct Vertex
{
public Vector4 Pos;
public Vector4 Normal;
public Vector4 Test;
public Vertex(Vector4 pos)
{
Pos = pos;
Normal = new Vector4(1,2,3,4);
Test = new Vector4(11,22,33,44);
}
}

Layout definition

{
new InputElement("POSITION", 0, SharpDX.DXGI.Format.R32G32B32A32_Float,  0, 0),
new InputElement("NORMAL"  , 0, SharpDX.DXGI.Format.R32G32B32A32_Float, 16, 0),
new InputElement("TEXCOORD", 0, SharpDX.DXGI.Format.R32G32B32A32_Float, 32, 0)
});

fx definition
struct VS_IN
{
float4 pos : POSITION;
float4 normal : NORMAL;
float4 test : TEXCOORD;
};

vertex buffer
0 [0x00000000-0x00000003] |           -0.625
1 [0x00000004-0x00000007] |           -0.625
2 [0x00000008-0x0000000b] |           -0.625
3 [0x0000000c-0x0000000f] |               +1
4 [0x00000010-0x00000013] |               +1
5 [0x00000014-0x00000017] |               +2
6 [0x00000018-0x0000001b] |               +3
7 [0x0000001c-0x0000001f] |               +4
8 [0x00000020-0x00000023] |              +11
9 [0x00000024-0x00000027] |              +22
10 [0x00000028-0x0000002b] |              +33
11 [0x0000002c-0x0000002f] |              +44
12 [0x00000030-0x00000033] |           -0.625  <-- new vertex
13 [0x00000034-0x00000037] |           +0.625

##### Share on other sites

Did you update the stride/vertexsize value when you actually render the primitives?

n!

##### Share on other sites

argh... . That is it. I forgot to change the size when setting the vertex buffer. Still using the other vertex struct I had defined as a test.

rc.Context.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(m_vertexBuffer, Utilities.SizeOf<Vertex>(), 0));

Edited by jkristia

##### Share on other sites
That's a big vertex you got there

Yep, usually you can rely on the interop information for the size (Marshal.SizeOf(typeof(Vertex)) or the similar SharpDX convenience function). The offsets can also be grabbed using Marshal.OffsetOf. Or make your life even easier - given your vertex elements are tightly packed - using InputElement.AppendAligned, like so (disclaimer: unchecked)

    rc.InputLayout = new InputLayout(rc.Device, ShaderSignature.GetInputSignature(vertexShaderByteCode), new[]
{
new InputElement("POSITION", 0, SharpDX.DXGI.Format.R32G32B32A32_Float,  0, 0),
new InputElement("NORMAL"  , 0, SharpDX.DXGI.Format.R32G32B32A32_Float, InputElement.AppendAligned, 0),
new InputElement("TEXCOORD", 0, SharpDX.DXGI.Format.R32G32B32A32_Float, InputElement.AppendAligned, 0)
});


As an aside: You usually don't need a Vector4 for position (even less for normal). I still have to come across an MSDN entry, but a 3D vector (x,y,z) will be automatically expanded to (x,y,z,1) on the shader side, if declared as float4. You're wasting a bit of bandwidth otherwise.

##### Share on other sites

>>As an aside: You usually don't need a Vector4 for position...

ah, good information. Thank you.

• 18
• 29
• 11
• 21
• 16