1uniform half4 colorRed, colorGreen; 2 3bool test_matrix_op_matrix_float() { 4 bool ok = true; 5 6 // Addition, subtraction and division operate componentwise. 7 { 8 const float3x3 splat_4 = float3x3(4, 4, 4, 4, 4, 4, 4, 4, 4); 9 const float3x3 splat_2 = float3x3(2, 2, 2, 2, 2, 2, 2, 2, 2); 10 float3x3 m; 11 12 m = float3x3(2); m += splat_4; ok = ok && (m == float3x3(6, 4, 4, 4, 6, 4, 4, 4, 6)); 13 m = float3x3(2); m -= splat_4; ok = ok && (m == float3x3(-2,-4,-4,-4,-2,-4,-4,-4,-2)); 14 m = float3x3(2); m /= splat_4; ok = ok && (m == float3x3(0.5)); 15 16 m = splat_4; m += float3x3(2); ok = ok && (m == float3x3(6, 4, 4, 4, 6, 4, 4, 4, 6)); 17 m = splat_4; m -= float3x3(2); ok = ok && (m == float3x3(2, 4, 4, 4, 2, 4, 4, 4, 2)); 18 m = splat_4; m /= splat_2; ok = ok && (m == float3x3(2, 2, 2, 2, 2, 2, 2, 2 ,2)); 19 } 20 { 21 float4x4 m = float4x4(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16); 22 m += float4x4(16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1); 23 ok = ok && (m == float4x4(17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17)); 24 } 25 { 26 float2x2 m = float2x2(10, 20, 30, 40); 27 m -= float2x2(1, 2, 3, 4); 28 ok = ok && (m == float2x2(9, 18, 27, 36)); 29 } 30 { 31 float2x2 m = float2x2(2, 4, 6, 8); 32 m /= float2x2(2, 2, 2, 4); 33 ok = ok && (m == float2x2(1, 2, 3, 2)); 34 } 35 36 // Multiplication performs a proper matrix multiply. 37 { 38 float2x2 m = float2x2(1, 2, 7, 4); 39 m *= float2x2(3, 5, 3, 2); 40 ok = ok && (m == float2x2(38, 26, 17, 14)); 41 } 42 { 43 float3x3 m = float3x3(10, 4, 2, 20, 5, 3, 10, 6, 5); 44 m *= float3x3(3, 3, 4, 2, 3, 4, 4, 9, 2); 45 ok = ok && (m == float3x3(130, 51, 35, 120, 47, 33, 240, 73, 45)); 46 } 47 48 return ok; 49} 50 51bool test_matrix_op_matrix_half() { 52 bool ok = true; 53 54 // Addition, subtraction and division operate componentwise. 55 { 56 const half3x3 splat_4 = half3x3(4, 4, 4, 4, 4, 4, 4, 4, 4); 57 const half3x3 splat_2 = half3x3(2, 2, 2, 2, 2, 2, 2, 2, 2); 58 half3x3 m; 59 60 m = half3x3(2); m += splat_4; ok = ok && (m == half3x3(6, 4, 4, 4, 6, 4, 4, 4, 6)); 61 m = half3x3(2); m -= splat_4; ok = ok && (m == half3x3(-2,-4,-4,-4,-2,-4,-4,-4,-2)); 62 m = half3x3(2); m /= splat_4; ok = ok && (m == half3x3(0.5)); 63 64 m = splat_4; m += half3x3(2); ok = ok && (m == half3x3(6, 4, 4, 4, 6, 4, 4, 4, 6)); 65 m = splat_4; m -= half3x3(2); ok = ok && (m == half3x3(2, 4, 4, 4, 2, 4, 4, 4, 2)); 66 m = splat_4; m /= splat_2; ok = ok && (m == half3x3(2, 2, 2, 2, 2, 2, 2, 2 ,2)); 67 } 68 { 69 half4x4 m = half4x4(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16); 70 m += half4x4(16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1); 71 ok = ok && (m == half4x4(17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17)); 72 } 73 { 74 half2x2 m = half2x2(10, 20, 30, 40); 75 m -= half2x2(1, 2, 3, 4); 76 ok = ok && (m == half2x2(9, 18, 27, 36)); 77 } 78 { 79 half2x2 m = half2x2(2, 4, 6, 8); 80 m /= half2x2(2, 2, 2, 4); 81 ok = ok && (m == half2x2(1, 2, 3, 2)); 82 } 83 84 // Multiplication performs a proper matrix multiply. 85 { 86 half2x2 m = half2x2(1, 2, 7, 4); 87 m *= half2x2(3, 5, 3, 2); 88 ok = ok && (m == half2x2(38, 26, 17, 14)); 89 } 90 { 91 half3x3 m = half3x3(10, 4, 2, 20, 5, 3, 10, 6, 5); 92 m *= half3x3(3, 3, 4, 2, 3, 4, 4, 9, 2); 93 ok = ok && (m == half3x3(130, 51, 35, 120, 47, 33, 240, 73, 45)); 94 } 95 96 return ok; 97} 98 99half4 main(float2 coords) { 100 return (test_matrix_op_matrix_float() && test_matrix_op_matrix_half()) ? colorGreen : colorRed; 101} 102