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

Does a float3 in a constant buffer secretly add padding?

我的 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 中。

哪一个是正确的?如果有的话?

参见:'Packing Rules for Constant Variables' (https://msdn.microsoft.com/en-us/library/windows/desktop/bb509632(v=vs.85).aspx)

... Additionally, HLSL packs data so that it does not cross a 16-byte boundary. Variables are packed into a given four-component vector until the variable will straddle a 4-vector boundary; the next variables will be bounced to the next four-component vector.

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