# Matrix Packing

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

## Recommended Posts

How are matrices packed in HLSL? Especially DirectX3D 10.
These do not explain:
http://msdn.microsoft.com/en-us/library/bb509623(v=VS.85).aspx
http://msdn.microsoft.com/en-us/library/bb509632(v=VS.85).aspx

Specifically I need to know if a 3×3 matrix is internally stored as a 4×4 matrix or if it is an array of 9 floats.
Similarly with 2×2 matrices.

Thank you,
Yogurt Emperor

##### Share on other sites
As far as I know, a 3x3 matrix is packed following the same rules as 3 consecutive float3 variables. The packing rules then state
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.[/quote]For a 3x3 matrix:float3 one; float3 two;//start a new 16-byte element float3 three;//start a new 16-byte element //or float oneX, oneY, oneZ, pad1; float twoX, twoY, twoZ, pad2; float threeX, threeY, threeZ, pad3;For a 2x2 matrix:float2 one; float2 two; //or float oneX, oneY, twoX, twoY;[hr][EDIT] Please note I'm just guessing.
On second thought, it would make sense for matrices to be dealt with under the rules for array packing
Arrays are not packed in HLSL by default. To avoid forcing the shader to take on ALU overhead for offset computations, every element in an array is stored in a four-component vector[/quote]For a 3x3 matrix it's the same as above, but for a 2x2 matrix:float2 one; float2 two;//start a new 16-byte element //or float oneX, oneY, pad1, pad2; float twoX, twoY, pad3, pad4;

##### Share on other sites

As far as I know, a 3x3 matrix is packed following the same rules as 3 consecutive float3 variables. The packing rules then state
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.
For a 3x3 matrix:float3 one; float3 two;//start a new 16-byte element float3 three;//start a new 16-byte element //or float oneX, oneY, oneZ, pad1; float twoX, twoY, twoZ, pad2; float threeX, threeY, threeZ, pad3;For a 2x2 matrix:float2 one; float2 two; //or float oneX, oneY, twoX, twoY;[hr][EDIT] Please note I'm just guessing.
On second thought, it would make sense for matrices to be dealt with under the rules for array packing
Arrays are not packed in HLSL by default. To avoid forcing the shader to take on ALU overhead for offset computations, every element in an array is stored in a four-component vector[/quote]For a 3x3 matrix it's the same as above, but for a 2x2 matrix:float2 one; float2 two;//start a new 16-byte element //or float oneX, oneY, pad1, pad2; float twoX, twoY, pad3, pad4;
[/quote]

Any way to verify which way they go?
I am only guessing that 3×3 is 3 rows of 4 vectors.
But 2×2 could be either way. If they do 2 rows of 4 vectors, there is so much wasted space.
And apparently there is no overhead by just doing [float2 float2]. But there is just as much logic for it to be [float2 | float2] as well.

In Direct3D 10, if I am using constant buffers (avoiding use of the Effects Framework), I have to match my data to the data in the shaders exactly, right?

Yogurt Emperor

##### Share on other sites
Any way to verify how this works?

Yogurt Emperor

1. 1
Rutin
32
2. 2
3. 3
4. 4
5. 5

• 13
• 9
• 9
• 9
• 14
• ### Forum Statistics

• Total Topics
633319
• Total Posts
3011349
• ### Who's Online (See full list)

There are no registered users currently online

×