1static const uint3 gl_WorkGroupSize = uint3(1u, 1u, 1u); 2 3RWByteAddressBuffer _15 : register(u0); 4ByteAddressBuffer _20 : register(t1); 5 6// Returns the inverse of a matrix, by using the algorithm of calculating the classical 7// adjoint and dividing by the determinant. The contents of the matrix are changed. 8float2x2 spvInverse(float2x2 m) 9{ 10 float2x2 adj; // The adjoint matrix (inverse after dividing by determinant) 11 12 // Create the transpose of the cofactors, as the classical adjoint of the matrix. 13 adj[0][0] = m[1][1]; 14 adj[0][1] = -m[0][1]; 15 16 adj[1][0] = -m[1][0]; 17 adj[1][1] = m[0][0]; 18 19 // Calculate the determinant as a combination of the cofactors of the first row. 20 float det = (adj[0][0] * m[0][0]) + (adj[0][1] * m[1][0]); 21 22 // Divide the classical adjoint matrix by the determinant. 23 // If determinant is zero, matrix is not invertable, so leave it unchanged. 24 return (det != 0.0f) ? (adj * (1.0f / det)) : m; 25} 26 27// Returns the determinant of a 2x2 matrix. 28float spvDet2x2(float a1, float a2, float b1, float b2) 29{ 30 return a1 * b2 - b1 * a2; 31} 32 33// Returns the inverse of a matrix, by using the algorithm of calculating the classical 34// adjoint and dividing by the determinant. The contents of the matrix are changed. 35float3x3 spvInverse(float3x3 m) 36{ 37 float3x3 adj; // The adjoint matrix (inverse after dividing by determinant) 38 39 // Create the transpose of the cofactors, as the classical adjoint of the matrix. 40 adj[0][0] = spvDet2x2(m[1][1], m[1][2], m[2][1], m[2][2]); 41 adj[0][1] = -spvDet2x2(m[0][1], m[0][2], m[2][1], m[2][2]); 42 adj[0][2] = spvDet2x2(m[0][1], m[0][2], m[1][1], m[1][2]); 43 44 adj[1][0] = -spvDet2x2(m[1][0], m[1][2], m[2][0], m[2][2]); 45 adj[1][1] = spvDet2x2(m[0][0], m[0][2], m[2][0], m[2][2]); 46 adj[1][2] = -spvDet2x2(m[0][0], m[0][2], m[1][0], m[1][2]); 47 48 adj[2][0] = spvDet2x2(m[1][0], m[1][1], m[2][0], m[2][1]); 49 adj[2][1] = -spvDet2x2(m[0][0], m[0][1], m[2][0], m[2][1]); 50 adj[2][2] = spvDet2x2(m[0][0], m[0][1], m[1][0], m[1][1]); 51 52 // Calculate the determinant as a combination of the cofactors of the first row. 53 float det = (adj[0][0] * m[0][0]) + (adj[0][1] * m[1][0]) + (adj[0][2] * m[2][0]); 54 55 // Divide the classical adjoint matrix by the determinant. 56 // If determinant is zero, matrix is not invertable, so leave it unchanged. 57 return (det != 0.0f) ? (adj * (1.0f / det)) : m; 58} 59 60// Returns the determinant of a 3x3 matrix. 61float spvDet3x3(float a1, float a2, float a3, float b1, float b2, float b3, float c1, float c2, float c3) 62{ 63 return a1 * spvDet2x2(b2, b3, c2, c3) - b1 * spvDet2x2(a2, a3, c2, c3) + c1 * spvDet2x2(a2, a3, b2, b3); 64} 65 66// Returns the inverse of a matrix, by using the algorithm of calculating the classical 67// adjoint and dividing by the determinant. The contents of the matrix are changed. 68float4x4 spvInverse(float4x4 m) 69{ 70 float4x4 adj; // The adjoint matrix (inverse after dividing by determinant) 71 72 // Create the transpose of the cofactors, as the classical adjoint of the matrix. 73 adj[0][0] = spvDet3x3(m[1][1], m[1][2], m[1][3], m[2][1], m[2][2], m[2][3], m[3][1], m[3][2], m[3][3]); 74 adj[0][1] = -spvDet3x3(m[0][1], m[0][2], m[0][3], m[2][1], m[2][2], m[2][3], m[3][1], m[3][2], m[3][3]); 75 adj[0][2] = spvDet3x3(m[0][1], m[0][2], m[0][3], m[1][1], m[1][2], m[1][3], m[3][1], m[3][2], m[3][3]); 76 adj[0][3] = -spvDet3x3(m[0][1], m[0][2], m[0][3], m[1][1], m[1][2], m[1][3], m[2][1], m[2][2], m[2][3]); 77 78 adj[1][0] = -spvDet3x3(m[1][0], m[1][2], m[1][3], m[2][0], m[2][2], m[2][3], m[3][0], m[3][2], m[3][3]); 79 adj[1][1] = spvDet3x3(m[0][0], m[0][2], m[0][3], m[2][0], m[2][2], m[2][3], m[3][0], m[3][2], m[3][3]); 80 adj[1][2] = -spvDet3x3(m[0][0], m[0][2], m[0][3], m[1][0], m[1][2], m[1][3], m[3][0], m[3][2], m[3][3]); 81 adj[1][3] = spvDet3x3(m[0][0], m[0][2], m[0][3], m[1][0], m[1][2], m[1][3], m[2][0], m[2][2], m[2][3]); 82 83 adj[2][0] = spvDet3x3(m[1][0], m[1][1], m[1][3], m[2][0], m[2][1], m[2][3], m[3][0], m[3][1], m[3][3]); 84 adj[2][1] = -spvDet3x3(m[0][0], m[0][1], m[0][3], m[2][0], m[2][1], m[2][3], m[3][0], m[3][1], m[3][3]); 85 adj[2][2] = spvDet3x3(m[0][0], m[0][1], m[0][3], m[1][0], m[1][1], m[1][3], m[3][0], m[3][1], m[3][3]); 86 adj[2][3] = -spvDet3x3(m[0][0], m[0][1], m[0][3], m[1][0], m[1][1], m[1][3], m[2][0], m[2][1], m[2][3]); 87 88 adj[3][0] = -spvDet3x3(m[1][0], m[1][1], m[1][2], m[2][0], m[2][1], m[2][2], m[3][0], m[3][1], m[3][2]); 89 adj[3][1] = spvDet3x3(m[0][0], m[0][1], m[0][2], m[2][0], m[2][1], m[2][2], m[3][0], m[3][1], m[3][2]); 90 adj[3][2] = -spvDet3x3(m[0][0], m[0][1], m[0][2], m[1][0], m[1][1], m[1][2], m[3][0], m[3][1], m[3][2]); 91 adj[3][3] = spvDet3x3(m[0][0], m[0][1], m[0][2], m[1][0], m[1][1], m[1][2], m[2][0], m[2][1], m[2][2]); 92 93 // Calculate the determinant as a combination of the cofactors of the first row. 94 float det = (adj[0][0] * m[0][0]) + (adj[0][1] * m[1][0]) + (adj[0][2] * m[2][0]) + (adj[0][3] * m[3][0]); 95 96 // Divide the classical adjoint matrix by the determinant. 97 // If determinant is zero, matrix is not invertable, so leave it unchanged. 98 return (det != 0.0f) ? (adj * (1.0f / det)) : m; 99} 100 101void comp_main() 102{ 103 float2x2 _23 = asfloat(uint2x2(_20.Load2(0), _20.Load2(8))); 104 float2x2 _24 = spvInverse(_23); 105 _15.Store2(0, asuint(_24[0])); 106 _15.Store2(8, asuint(_24[1])); 107 float3x3 _29 = asfloat(uint3x3(_20.Load3(16), _20.Load3(32), _20.Load3(48))); 108 float3x3 _30 = spvInverse(_29); 109 _15.Store3(16, asuint(_30[0])); 110 _15.Store3(32, asuint(_30[1])); 111 _15.Store3(48, asuint(_30[2])); 112 float4x4 _35 = asfloat(uint4x4(_20.Load4(64), _20.Load4(80), _20.Load4(96), _20.Load4(112))); 113 float4x4 _36 = spvInverse(_35); 114 _15.Store4(64, asuint(_36[0])); 115 _15.Store4(80, asuint(_36[1])); 116 _15.Store4(96, asuint(_36[2])); 117 _15.Store4(112, asuint(_36[3])); 118} 119 120[numthreads(1, 1, 1)] 121void main() 122{ 123 comp_main(); 124} 125