Skip to content

Commit 7fc7d9b

Browse files
committed
Switching ShaderConductor to column-major matrices removes the need to transpose in C#.
1 parent e7b8f00 commit 7fc7d9b

12 files changed

Lines changed: 24 additions & 85 deletions

MonoGame.Framework/Graphics/Effect/EffectParameter.cs

Lines changed: 23 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -246,24 +246,11 @@ public Matrix GetValueMatrix ()
246246
throw new InvalidCastException();
247247

248248
var floatData = (float[])Data;
249-
#if OpenGL
250-
// OpenGL matrices are transposed compared to DX
251-
if(!MojoDataLayout)
252-
return new Matrix(floatData[0], floatData[1], floatData[2], floatData[3],
253-
floatData[4], floatData[5], floatData[6], floatData[7],
254-
floatData[8], floatData[9], floatData[10], floatData[11],
255-
floatData[12], floatData[13], floatData[14], floatData[15]);
256-
else
257-
return new Matrix(floatData[0], floatData[4], floatData[8], floatData[12],
258-
floatData[1], floatData[5], floatData[9], floatData[13],
259-
floatData[2], floatData[6], floatData[10], floatData[14],
260-
floatData[3], floatData[7], floatData[11], floatData[15]);
261-
#else
249+
262250
return new Matrix(floatData[0], floatData[4], floatData[8], floatData[12],
263251
floatData[1], floatData[5], floatData[9], floatData[13],
264252
floatData[2], floatData[6], floatData[10], floatData[14],
265253
floatData[3], floatData[7], floatData[11], floatData[15]);
266-
#endif
267254
}
268255

269256
public Matrix[] GetValueMatrixArray (int count)
@@ -505,71 +492,7 @@ public void SetValue(Matrix value)
505492
{
506493
if (ParameterClass != EffectParameterClass.Matrix || ParameterType != EffectParameterType.Single)
507494
throw new InvalidCastException();
508-
#if OPENGL
509-
// OpenGL matrices are transposed compared to DX
510-
if (!MojoDataLayout)
511-
SetMatrixTranspose(value);
512-
else
513-
SetMatrix(value);
514-
#else
515-
SetMatrix(value);
516-
#endif
517-
StateKey = unchecked(NextStateKey++);
518-
}
519-
520-
public void SetValueTranspose(Matrix value)
521-
{
522-
if (ParameterClass != EffectParameterClass.Matrix || ParameterType != EffectParameterType.Single)
523-
throw new InvalidCastException();
524-
#if OPENGL
525-
// OpenGL matrices are transposed compared to DX
526-
if (!MojoDataLayout)
527-
SetMatrix(value);
528-
else
529-
SetMatrixTranspose(value);
530-
#else
531-
SetMatrixTranspose(value);
532-
#endif
533-
StateKey = unchecked(NextStateKey++);
534-
}
535-
536-
public void SetValue (Matrix[] value)
537-
{
538-
if (ParameterClass != EffectParameterClass.Matrix || ParameterType != EffectParameterType.Single)
539-
throw new InvalidCastException();
540-
541-
#if OPENGL
542-
// OpenGL matrices are transposed compared to DX
543-
if (!MojoDataLayout)
544-
SetMatrixArrayTransposed(value);
545-
else
546-
SetMatrixArray(value);
547-
#else
548-
SetMatrixArray(value);
549-
#endif
550-
StateKey = unchecked(NextStateKey++);
551-
}
552-
553-
public void SetValueTranspose(Matrix[] value)
554-
{
555-
if (ParameterClass != EffectParameterClass.Matrix || ParameterType != EffectParameterType.Single)
556-
throw new InvalidCastException();
557495

558-
#if OPENGL
559-
// OpenGL matrices are transposed compared to DX
560-
if (!MojoDataLayout)
561-
SetMatrixArray(value);
562-
else
563-
SetMatrixArrayTransposed(value);
564-
#else
565-
SetMatrixArrayTransposed(value);
566-
#endif
567-
StateKey = unchecked(NextStateKey++);
568-
}
569-
570-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
571-
private void SetMatrix(Matrix value)
572-
{
573496
// HLSL expects matrices to be transposed by default.
574497
// These unrolled loops do the transpose during assignment.
575498
if (RowCount == 4 && ColumnCount == 4)
@@ -686,11 +609,15 @@ private void SetMatrix(Matrix value)
686609
fData[2] = value.M12;
687610
fData[3] = value.M22;
688611
}
612+
613+
StateKey = unchecked(NextStateKey++);
689614
}
690615

691-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
692-
private void SetMatrixTranspose(Matrix value)
616+
public void SetValueTranspose(Matrix value)
693617
{
618+
if (ParameterClass != EffectParameterClass.Matrix || ParameterType != EffectParameterType.Single)
619+
throw new InvalidCastException();
620+
694621
// HLSL expects matrices to be transposed by default, so copying them straight
695622
// from the in-memory version effectively transposes them back to row-major.
696623
if (RowCount == 4 && ColumnCount == 4)
@@ -807,10 +734,15 @@ private void SetMatrixTranspose(Matrix value)
807734
fData[2] = value.M21;
808735
fData[3] = value.M22;
809736
}
737+
738+
StateKey = unchecked(NextStateKey++);
810739
}
811740

812-
private void SetMatrixArray(Matrix[] value)
741+
public void SetValue (Matrix[] value)
813742
{
743+
if (ParameterClass != EffectParameterClass.Matrix || ParameterType != EffectParameterType.Single)
744+
throw new InvalidCastException();
745+
814746
if (RowCount == 4 && ColumnCount == 4)
815747
{
816748
for (var i = 0; i < value.Length; i++)
@@ -946,10 +878,15 @@ private void SetMatrixArray(Matrix[] value)
946878
fData[3] = value[i].M22;
947879
}
948880
}
881+
882+
StateKey = unchecked(NextStateKey++);
949883
}
950884

951-
private void SetMatrixArrayTransposed(Matrix[] value)
885+
public void SetValueTranspose(Matrix[] value)
952886
{
887+
if (ParameterClass != EffectParameterClass.Matrix || ParameterType != EffectParameterType.Single)
888+
throw new InvalidCastException();
889+
953890
// HLSL expects matrices to be transposed by default, so copying them straight
954891
// from the in-memory version effectively transposes them back to row-major.
955892
if (RowCount == 4 && ColumnCount == 4)
@@ -1018,8 +955,8 @@ private void SetMatrixArrayTransposed(Matrix[] value)
1018955
fData[6] = value[i].M23;
1019956
fData[7] = value[i].M24;
1020957

1021-
fData[8] = value[i].M31;
1022-
fData[9] = value[i].M32;
958+
fData[8] = value[i].M31;
959+
fData[9] = value[i].M32;
1023960
fData[10] = value[i].M33;
1024961
fData[11] = value[i].M34;
1025962
}
@@ -1086,6 +1023,8 @@ private void SetMatrixArrayTransposed(Matrix[] value)
10861023
fData[3] = value[i].M22;
10871024
}
10881025
}
1026+
1027+
StateKey = unchecked(NextStateKey++);
10891028
}
10901029

10911030
public void SetValue (Quaternion value)
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)