yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
8086adc90
master
1//TEST:SIMPLE(filecheck=METAL): -target metal -stage compute -entry computeMain -DTYPE=int 2//TEST:SIMPLE(filecheck=METALLIB): -target metallib -stage compute -entry computeMain -DTYPE=int 3//TEST(compute):COMPARE_COMPUTE_EX(filecheck-buffer=BUF):-slang -output-using-type -compute -mtl -shaderobj -xslang -DTYPE=int 4//TEST(compute):COMPARE_COMPUTE_EX(filecheck-buffer=BUF):-slang -output-using-type -compute -mtl -shaderobj -xslang -DTYPE=uint 5//TEST(compute):COMPARE_COMPUTE_EX(filecheck-buffer=BUF):-slang -output-using-type -compute -vk -shaderobj -xslang -DTYPE=int 6//TEST(compute):COMPARE_COMPUTE_EX(filecheck-buffer=BUF):-slang -output-using-type -compute -vk -shaderobj -xslang -DTYPE=uint 7 8#ifndef TYPE 9#define TYPE int 10#endif 11 12typealias m2x2 = matrix<TYPE, 2, 2>; 13typealias m2x3 = matrix<TYPE, 2, 3>; 14typealias m3x3 = matrix<TYPE, 3, 3>; 15typealias m2x4 = matrix<TYPE, 2, 4>; 16 17//TEST_INPUT:ubuffer(data=[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0], stride=4):out,name outputBuffer 18//TEST_INPUT:ubuffer(data=[-1 4], stride=4):name expectedBuffer 19RWStructuredBuffer<TYPE> outputBuffer; 20RWStructuredBuffer<TYPE> expectedBuffer; 21 22struct matrixWrapper { 23 m2x2 mat1 = m2x2(1, 2, 3, 4); 24 m2x3 mat2 = m2x3(5, 6, 7, 8, 9, 10); 25}; 26 27TYPE elementAdd(m2x2 matrix) 28{ 29 return matrix[0][0] 30 + matrix[0][1] 31 + matrix[1][0] 32 + matrix[1][1]; 33} 34 35// METAL: array<{{(int|uint)}}2, int(2)> 36// METALLIB: @computeMain 37 38[numthreads(1, 1, 1)] 39void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) 40{ 41 // Test matrix construction 42 m2x2 mat1 = m2x2(1, 2, 3, 4); 43 m3x3 mat2 = m3x3( 44 1, 2, 3, 45 4, 5, 6, 46 7, 8, 9 47 ); 48 m2x4 mat3 = m2x4( 49 10, 11, 12, 13, 50 14, 15, 16, 17 51 ); 52 53 // Test matrix element access 54 TYPE val1 = mat1[0][0]; 55 TYPE val2 = mat2[2][1]; 56 57 // Test matrix row access 58 vector<TYPE, 2> row = mat1[1]; 59 vector<TYPE, 3> row3 = mat2[0]; 60 61 // Test arithmetic operations 62 m2x2 mat5 = m2x2(2, 4, 6, 7); 63 64 m2x2 mat_scalar = 2 * mat1; 65 m2x2 mat_add = mat1 + mat5; 66 m2x2 mat_sub = mat5 - mat1; 67 m2x2 mat_mul = mat1 * mat5; 68 69 // Test passing matrices to functions 70 TYPE added = elementAdd(mat1); 71 72 // Test structs with matrix fields 73 matrixWrapper wrapper = {}; 74 75 // Test matrix intrinsic operations 76 77 // Test determinant for square matrices 78 m2x2 mat6 = m2x2(2, 1, 4, 3); 79 TYPE det2x2 = TYPE(determinant(mat6)); 80 TYPE det3x3 = TYPE(determinant(mat2)); 81 82 // Test transpose 83 matrix<TYPE, 2, 2> trans2x2 = transpose(mat1); 84 matrix<TYPE, 3, 2> trans2x3 = transpose(wrapper.mat2); 85 86 // Test element-wise min/max 87 m2x2 mat_min = min(mat1, mat5); 88 m2x2 mat_max = max(mat1, mat5); 89 90 // Test all/any operations (these return bool, but we'll cast to TYPE for output) 91 m2x2 zero_mat = m2x2(0, 0, 0, 0); 92 m2x2 mixed_mat = m2x2(1, 0, 2, 0); 93 94 TYPE all_nonzero = TYPE(all(mat1)); 95 TYPE all_zero = TYPE(all(zero_mat)); 96 TYPE any_nonzero = TYPE(any(mixed_mat)); 97 TYPE any_zero = TYPE(any(zero_mat)); 98 99 // Test bit shift operations 100 m2x2 shift_mat = m2x2(1, 2, 4, 8); 101 m2x2 left_shift = shift_mat << 1; 102 m2x2 right_shift = shift_mat >> 1; 103 104 // Test comparison operations (these return bool matrices, cast to TYPE for output) 105 m2x2 comp_mat1 = m2x2(1, 3, 2, 4); 106 m2x2 comp_mat2 = m2x2(2, 2, 3, 3); 107 108 matrix<bool, 2, 2> less_than = comp_mat1 < comp_mat2; 109 matrix<bool, 2, 2> greater_than = comp_mat1 > comp_mat2; 110 matrix<bool, 2, 2> less_equal = comp_mat1 <= comp_mat2; 111 matrix<bool, 2, 2> greater_equal = comp_mat1 >= comp_mat2; 112 matrix<bool, 2, 2> equal_to = comp_mat1 == comp_mat2; 113 matrix<bool, 2, 2> not_equal = comp_mat1 != comp_mat2; 114 115 // Test matrix negation operations 116 m2x2 neg_mat = m2x2(1, -2, 3, -4); 117 m2x2 negated = -neg_mat; 118 119 // Store results 120 outputBuffer[0] = val1; 121 // BUF: 1 122 outputBuffer[1] = val2; 123 // BUF-NEXT: 8 124 outputBuffer[2] = row.x; 125 // BUF-NEXT: 3 126 outputBuffer[3] = row.y; 127 // BUF-NEXT: 4 128 outputBuffer[4] = row3.y; 129 // BUF-NEXT: 2 130 outputBuffer[5] = mat_scalar[0][0]; 131 // BUF-NEXT: 2 132 outputBuffer[6] = mat_add[0][0]; 133 // BUF-NEXT: 3 134 outputBuffer[7] = mat_sub[0][0]; 135 // BUF-NEXT: 1 136 outputBuffer[8] = mat_mul[1][1]; 137 // BUF-NEXT: 28 138 outputBuffer[9] = added; 139 // BUF-NEXT: 10 140 outputBuffer[10] = wrapper.mat1[0][0] * wrapper.mat2[0][0]; 141 // BUF-NEXT: 5 142 143 // Matrix intrinsic operation results 144 outputBuffer[11] = det2x2; 145 // BUF-NEXT: 2 146 outputBuffer[12] = det3x3; 147 // BUF-NEXT: 0 148 outputBuffer[13] = mat_min[0][0]; 149 // BUF-NEXT: 1 150 outputBuffer[14] = mat_min[1][1]; 151 // BUF-NEXT: 4 152 outputBuffer[15] = mat_max[0][0]; 153 // BUF-NEXT: 2 154 outputBuffer[16] = mat_max[1][1]; 155 // BUF-NEXT: 7 156 outputBuffer[17] = all_nonzero; 157 // BUF-NEXT: 1 158 outputBuffer[18] = all_zero; 159 // BUF-NEXT: 0 160 outputBuffer[19] = any_nonzero; 161 // BUF-NEXT: 1 162 outputBuffer[20] = any_zero; 163 // BUF-NEXT: 0 164 outputBuffer[21] = trans2x2[0][0]; 165 // BUF-NEXT: 1 166 outputBuffer[22] = trans2x2[1][0]; 167 // BUF-NEXT: 2 168 outputBuffer[23] = trans2x3[0][0]; 169 // BUF-NEXT: 5 170 171 // Bit shift operation results 172 outputBuffer[24] = left_shift[0][0]; 173 // BUF-NEXT: 2 174 outputBuffer[25] = left_shift[0][1]; 175 // BUF-NEXT: 4 176 outputBuffer[26] = right_shift[1][0]; 177 // BUF-NEXT: 2 178 outputBuffer[27] = right_shift[1][1]; 179 // BUF-NEXT: 4 180 181 // Comparison operation results (bool matrices cast to TYPE) 182 outputBuffer[28] = TYPE(less_than[0][0]); 183 // BUF-NEXT: 1 184 outputBuffer[29] = TYPE(less_than[0][1]); 185 // BUF-NEXT: 0 186 outputBuffer[30] = TYPE(greater_than[0][1]); 187 // BUF-NEXT: 1 188 outputBuffer[31] = TYPE(greater_than[1][1]); 189 // BUF-NEXT: 1 190 outputBuffer[32] = TYPE(less_equal[0][0]); 191 // BUF-NEXT: 1 192 outputBuffer[33] = TYPE(less_equal[0][1]); 193 // BUF-NEXT: 0 194 outputBuffer[34] = TYPE(greater_equal[0][1]); 195 // BUF-NEXT: 1 196 outputBuffer[35] = TYPE(greater_equal[1][0]); 197 // BUF-NEXT: 0 198 outputBuffer[36] = TYPE(equal_to[0][0]); 199 // BUF-NEXT: 0 200 outputBuffer[37] = TYPE(negated[0][0] == expectedBuffer[0]); 201 // BUF-NEXT: 1 202 outputBuffer[38] = TYPE(negated[1][1] == expectedBuffer[1]); 203 // BUF-NEXT: 1 204}