How to access float4x4 by row/column

Hi,

I was looking to access a float4x4 as a flat array (16 floats) but it doesn’t seem to work - is that possible to do in a shader?

e.g.

float4x4 myMatrix;
float fValue = myMatrix[14];

I’ve also tried using an integer index but it only seems to work for one of the values.

This works:
float4 fRow = myMatrix[rowIndex][0];

This doesn’t work:
float fValue = myMatrix[rowIndex][colIndex];

Once i’ve retrieved the row I thought I could access the ‘column’ by using the float4 as 4 floats but I can’t seem to find a way of doing that without having 4 if statements:

if( colIndex == 0 ) fValue= fRow[0];
if( colIndex == 1 ) fValue= fRow[1];
if( colIndex == 2 ) fValue= fRow[2];
if( colIndex == 3 ) fValue= fRow[3];

Can anyone think of anything that would be more optimal?

Thanks!

I haven’t tried it but does this sort of thing work?

float fValueCol0 = myMatrix[rowIndex].x;
float fValueCol1 = myMatrix[rowIndex].y;
float fValueCol2 = myMatrix[rowIndex].z;
float fValueCol3 = myMatrix[rowIndex].w;

Thanks for that but I was looking for a specific value so even if I used that method I would still need to have an ‘IF’ statement to decide which one to use.

I would have thought the usual

float fValue = myMatrix[rowIndex][colIndex];

would work. Can you not store that row into another array and then access that one:

float tmpArray[4] = myMatrix[rowIndex].x;
float fValue = tmpArray[colIndex];

Yeah, it’s strange it doesn’t work but I guess the shader assembly language doesn’t support double indexing like that. Unfortunately your other suggestion doesn’t work either :frowning:

I managed to remove the conditional by multiplying the resulting row with a row from this matrix to isolate the value:

float4x4 _RowAccess = { 1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1 };

After I get a float4 from that I just total the xyzw components to get the value I need.

Thanks