Sign in to follow this  
L. Spiro

Matrix Packing

Recommended Posts

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


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 this post


Link to post
Share on other sites
Hodgman    51328
As far as I know, a 3x3 matrix is packed following the same rules as 3 consecutive float3 variables. The packing rules then state [quote]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:[code]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;[/code]For a 2x2 matrix:[code]float2 one;
float2 two;
//or
float oneX, oneY, twoX, twoY;[/code][hr][EDIT] [b]Please note I'm just guessing[/b].
On second thought, it would make sense for matrices to be dealt with under the rules for array packing [quote]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:[code]float2 one;
float2 two;//start a new 16-byte element
//or
float oneX, oneY, pad1, pad2;
float twoX, twoY, pad3, pad4;[/code]

Share this post


Link to post
Share on other sites
L. Spiro    25638
[quote name='Hodgman' timestamp='1302670129' post='4797780']
As far as I know, a 3x3 matrix is packed following the same rules as 3 consecutive float3 variables. The packing rules then state [quote]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:[code]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;[/code]For a 2x2 matrix:[code]float2 one;
float2 two;
//or
float oneX, oneY, twoX, twoY;[/code][hr][EDIT] [b]Please note I'm just guessing[/b].
On second thought, it would make sense for matrices to be dealt with under the rules for array packing [quote]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:[code]float2 one;
float2 two;//start a new 16-byte element
//or
float oneX, oneY, pad1, pad2;
float twoX, twoY, pad3, pad4;[/code]
[/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 this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this