I'm having some issues with the packing of my structure in C# and passing them through to cbuffers I have registered in HLSL. When I pack my struct in one manner the information seems to be able to pass to the shader:
[StructLayout(LayoutKind.Explicit, Size = 16)]
internal struct TestStruct
{
[FieldOffset(0)]
public Vector3 mEyePosition;
[FieldOffset(12)]
public int type;
}
This works perfectly when used against this HLSL fragment:
cbuffer PerFrame : register(b0)
{
Vector3 eyePos;
int type;
}
float3 GetColour()
{
float3 returnColour = float(0.0f, 0.0f, 0.0f);
switch(type)
{
case 0:
returnColour = float3(1.0f, 0.0f, 0.0f);
break;
case 1:
returnColour = float3(0.0f, 1.0f, 0.0f);
break;
case 2:
returnColour = float3(0.0f, 0.0f, 1.0f);
break;
}
return returnColour;
}
However, when I use the following structure definitions...
// Note this is 16 because HLSL packs in 4 float 'chunks'.
// It is also simplified, but still demonstrates the problem.
[StructLayout(Layout.Explicit, Size = 16)]
internal struct InternalTestStruct
{
[FieldOffset(0)]
public int type;
}
[StructLayout(LayoutKind.Explicit, Size = 32)]
internal struct TestStruct
{
[FieldOffset(0)]
public Vector3 mEyePosition;
//Missing 4 bytes here for correct packing.
[FieldOffset(16)]
public InternalTestStruct mInternal;
}
... the following HLSL fragment no longer works.
struct InternalType
{
int type;
}
cbuffer PerFrame : register(b0)
{
Vector3 eyePos;
InternalType internalStruct;
}
float3 GetColour()
{
float3 returnColour = float(0.0f, 0.0f, 0.0f);
switch(internaltype.type)
{
case 0:
returnColour = float3(1.0f, 0.0f, 0.0f);
break;
case 1:
returnColour = float3(0.0f, 1.0f, 0.0f);
break;
case 2:
returnColour = float3(0.0f, 0.0f, 1.0f);
break;
}
return returnColour;
}
Is there a problem with the way I am packing the struct, or is it another issue?
To re-iterate: I can pass a struct in a cbuffer so long as it does not contain a nested struct.