Search code examples
typessizehlsl

HLSL Primitive type size issue


I was under the impression that the type float2x4 would occupy 32 bytes, however when I declare the following float2x4 varname[100], it occupies 64*100 bytes, not 32*100 bytes as expected. How come? Perhaps I'm not seeing something related to the padding of float2x4.


Solution

  • float2x4 is asking for 4 float2s. Since each "register" is 16 bytes wide (1 float4) then it uses 64 bytes worth of space to store the 4 float2s. Z and W in each of the 4 float4s is unused and acts as padding.

    In contrast, float4x2 is asking for 2 float4s, and so fits perfectly into two float4 registers (32 bytes).