float3在常量缓冲区中是否秘密添加填充?

时间:2015-02-09 14:59:28

标签: c# directx padding hlsl sharpdx

我的shader.fx文件中有一个cbuffer,如下所示:

cbuffer lights : register (b1)
{
    float4 Light1Color;
    float3 Light1Direction;

    float4 Light2Color;
    float3 Light2Direction;

    float4 Light3Color;
    float3 Light3Direction;
}

我有一些问题从我的代码填充这个cbuffer,它最终成为以下结构的数组:

struct Light
    {
        public Color4 Color;
        public Vector3 Direction;
        //For whatever reason the constant buffer that we fill with this 
        //light must have a bytesize that is a multiple of 16, so we add a float (4 bytes)...
        public float padding;
        public static int SizeInBytes = (4 * 4) + Vector3.SizeInBytes + 4;
}

我使用DataStream(SharpDX)将数组写入cbuffer。当我的结构中没有填充时,这个DataStream不高兴,但是我的着色器中没有这个填充。
生成的灯是正确的,这对我来说意味着已编写的填充浮点数被忽略,或者在编译期间在shader.fx中秘密添加。

其中哪一个是真的?如果有的话?

1 个答案:

答案 0 :(得分:0)

请参阅:'常量变量的包装规则'(https://msdn.microsoft.com/en-us/library/windows/desktop/bb509632(v=vs.85).aspx

...此外,HLSL打包数据使其不会跨越16字节边界。变量被打包到给定的四分量矢量中,直到变量跨越一个4矢量边界;下一个变量将被弹回到下一个四分量向量。

您可以使用packoffset修饰符将一些组件打包到“未使用”的空间中。但是,这不适合您的数据结构。 (https://msdn.microsoft.com/en-us/library/windows/desktop/dd607358(v=vs.85).aspx)。