yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
5c672cef1
master
1// WGSL supports "textureSample()" only in fragment shader 2//TEST:SIMPLE(filecheck=WGSL): -stage fragment -entry fragMain -target wgsl 3 4// TODO: offset variants requires the offset value to be compile-time constant 5// But the keyword, `constexpr`, doesn't seem to work for the combined-textures 6#define TEST_WHEN_CONSTEXPR_WORKS_FOR_OFFSET 0 7 8//WGSL:@binding(0) {{.*}}t1D_f32v3{{.*}} : texture_1d<f32>; 9//WGSL:@binding(1) {{.*}}t1D_f32v3{{.*}} : sampler; 10//TEST_INPUT: TextureSampler1D(size=4, content = zero):name t1D_f32v3 11Sampler1D<float3> t1D_f32v3; 12 13//WGSL:@binding(2) {{.*}}t2D_f32v3{{.*}} : texture_2d<f32>; 14//WGSL:@binding(3) {{.*}}t2D_f32v3{{.*}} : sampler; 15//TEST_INPUT: TextureSampler2D(size=4, content = zero):name t2D_f32v3 16Sampler2D<float3> t2D_f32v3; 17 18//TEST_INPUT: TextureSampler3D(size=4, content = zero):name t3D_f32v3 19Sampler3D<float3> t3D_f32v3; 20//TEST_INPUT: TextureSamplerCube(size=4, content = zero):name tCube_f32v3 21SamplerCube<float3> tCube_f32v3; 22 23//TEST_INPUT: TextureSampler1D(size=4, content = zero, arrayLength=2):name t1DArray_f32v3 24Sampler1DArray<float3> t1DArray_f32v3; 25//TEST_INPUT: TextureSampler2D(size=4, content = zero, arrayLength=2):name t2DArray_f32v3 26Sampler2DArray<float3> t2DArray_f32v3; 27//TEST_INPUT: TextureSamplerCube(size=4, content = zero, arrayLength=2):name tCubeArray_f32v3 28SamplerCubeArray<float3> tCubeArray_f32v3; 29 30//TEST_INPUT: TextureSampler1D(size=4, content = zero):name t1D_f32v4 31Sampler1D<float4> t1D_f32v4; 32//TEST_INPUT: TextureSampler2D(size=4, content = zero):name t2D_f32v4 33Sampler2D<float4> t2D_f32v4; 34//TEST_INPUT: TextureSampler3D(size=4, content = zero):name t3D_f32v4 35Sampler3D<float4> t3D_f32v4; 36//TEST_INPUT: TextureSamplerCube(size=4, content = zero):name tCube_f32v4 37SamplerCube<float4> tCube_f32v4; 38 39//TEST_INPUT: TextureSampler1D(size=4, content = zero, arrayLength=2):name t1DArray_f32v4 40Sampler1DArray<float4> t1DArray_f32v4; 41//TEST_INPUT: TextureSampler2D(size=4, content = zero, arrayLength=2):name t2DArray_f32v4 42Sampler2DArray<float4> t2DArray_f32v4; 43//TEST_INPUT: TextureSamplerCube(size=4, content = zero, arrayLength=2):name tCubeArray_f32v4 44SamplerCubeArray<float4> tCubeArray_f32v4; 45 46//TEST_INPUT: TextureSampler2D(size=4, content = zero):name d2D 47Sampler2DShadow d2D; 48//TEST_INPUT: TextureSamplerCube(size=4, content = zero):name dCube 49SamplerCubeShadow dCube; 50//TEST_INPUT: TextureSampler2D(size=4, content = zero, arrayLength=2):name d2DArray 51Sampler2DArrayShadow d2DArray; 52//TEST_INPUT: TextureSamplerCube(size=4, content = zero, arrayLength=2):name dCubeArray 53SamplerCubeArrayShadow dCubeArray; 54 55//TEST_INPUT: ubuffer(data=[0], stride=4):out,name outputBuffer 56RWStructuredBuffer<int> outputBuffer; 57 58 59bool TEST_texture<T>( 60 Sampler1D<T> t1D, 61 Sampler2D<T> t2D, 62 Sampler3D<T> t3D, 63 SamplerCube<T> tCube, 64 Sampler1DArray<T> t1DArray, 65 Sampler2DArray<T> t2DArray, 66 SamplerCubeArray<T> tCubeArray 67) where T:ITexelElement,IArithmetic 68{ 69 // WGSL-LABEL: TEST_texture 70 typealias Tvn = T; 71 typealias Tv4 = vector<T.Element,4>; 72 73 float u = 0; 74 float u2 = 0.5; 75 constexpr const float ddx = 0.0f; 76 constexpr const float ddy = 0.0f; 77 78 bool result = true 79 // =============================== 80 // float CalculateLevelOfDetail() 81 // =============================== 82 // WGSL doesn't have a way to calculate mip-map level for the given coordinate 83 84 // ======================================== 85 // float CalculateLevelOfDetailUnclamped() 86 // ======================================== 87 // WGSL doesn't have a way to calculate mip-map level for the given coordinate 88 89 // =========== 90 // T Sample() 91 // https://www.w3.org/TR/WGSL/#texturesample 92 // =========== 93 94 // WGSL: textureSample({{\(*}}t1D 95 && all(Tvn(T.Element(0)) == t1D.Sample(u)) 96 97 // WGSL: textureSample({{\(*}}t2D 98 && all(Tvn(T.Element(0)) == t2D.Sample(float2(u, u))) 99 100 // WGSL: textureSample({{\(*}}t3D 101 && all(Tvn(T.Element(0)) == t3D.Sample(float3(u, u, u))) 102 103 // WGSL: textureSample({{\(*}}tCube 104 && all(Tvn(T.Element(0)) == tCube.Sample(normalize(float3(u, 1 - u, u)))) 105 106 // WGSL doesn't support textureSample for 1d_array and 3d_array; only 2d and cube 107 108 // WGSL: textureSample({{\(*}}t2DArray 109 && all(Tvn(T.Element(0)) == t2DArray.Sample(float3(u, u, 0))) 110 111 // WGSL: textureSample({{\(*}}tCubeArray 112 && all(Tvn(T.Element(0)) == tCubeArray.Sample(float4(normalize(float3(u, 1 - u, u)), 0))) 113 114#if TEST_WHEN_CONSTEXPR_WORKS_FOR_OFFSET 115 // Offset variant 116 117 // WGSL: textureSample({{\(*}}t1D 118 && all(Tvn(T.Element(0)) == t1D.Sample(u, 1)) 119 120 // WGSL: textureSample({{\(*}}t2D 121 && all(Tvn(T.Element(0)) == t2D.Sample(float2(u, u), int2(1, 1))) 122 123 // WGSL: textureSample({{\(*}}t3D 124 && all(Tvn(T.Element(0)) == t3D.Sample(float3(u, u, u), int3(1, 1, 1))) 125 126 // WGSL doesn't support offset variant for cube and cube_array 127 128 // WGSL: textureSample({{\(*}}t2DArray 129 && all(Tvn(T.Element(0)) == t2DArray.Sample(float3(u, u, 0), int2(1, 1))) 130#endif // #if TEST_WHEN_CONSTEXPR_WORKS_FOR_OFFSET 131 132 // =============== 133 // T SampleBias() 134 // https://www.w3.org/TR/WGSL/#texturesamplebias 135 // =============== 136 137 // WGSL doesn't support Bias for 1D texture 138 139 // WGSL: textureSampleBias({{\(*}}t2D 140 && all(Tvn(T.Element(0)) == t2D.SampleBias(float2(u, u), float(-1))) 141 142 // WGSL: textureSampleBias({{\(*}}t3D 143 && all(Tvn(T.Element(0)) == t3D.SampleBias(float3(u, u, u), float(-1))) 144 145 // WGSL: textureSampleBias({{\(*}}tCube 146 && all(Tvn(T.Element(0)) == tCube.SampleBias(normalize(float3(u, 1 - u, u)), float(-1))) 147 148 // WGSL: textureSampleBias({{\(*}}t2DArray 149 && all(Tvn(T.Element(0)) == t2DArray.SampleBias(float3(u, u, 0), float(-1))) 150 151 // WGSL: textureSampleBias({{\(*}}tCubeArray 152 && all(Tvn(T.Element(0)) == tCubeArray.SampleBias(float4(normalize(float3(u, 1 - u, u)), 0), float(-1))) 153 154#if TEST_WHEN_CONSTEXPR_WORKS_FOR_OFFSET 155 // Offset variant 156 157 // W-GSL: textureSampleBias({{\(*}}t2D 158 && all(Tvn(T.Element(0)) == t2D.SampleBias(float2(u, u), float(-1), int2(1, 1))) 159 160 // W-GSL: textureSampleBias({{\(*}}t3D 161 && all(Tvn(T.Element(0)) == t3D.SampleBias(float3(u, u, u), float(-1), int3(1, 1, 1))) 162 163 // W-GSL: textureSampleBias({{\(*}}t2DArray 164 && all(Tvn(T.Element(0)) == t2DArray.SampleBias(float3(u, u, 0), float(-1), int2(1, 1))) 165#endif // #if TEST_WHEN_CONSTEXPR_WORKS_FOR_OFFSET 166 167 // =================================== 168 // T SampleLevel() 169 // https://www.w3.org/TR/WGSL/#texturesamplelevel 170 // =================================== 171 172 // WGSL doesn't support textureSampleLevel for 1D texture 173 174 // WGSL: textureSampleLevel({{\(*}}t2D 175 && all(Tvn(T.Element(0)) == t2D.SampleLevel(float2(u, u), 0)) 176 177 // WGSL: textureSampleLevel({{\(*}}t3D 178 && all(Tvn(T.Element(0)) == t3D.SampleLevel(float3(u, u, u), 0)) 179 180 // WGSL: textureSampleLevel({{\(*}}tCube 181 && all(Tvn(T.Element(0)) == tCube.SampleLevel(normalize(float3(u, 1 - u, u)), 0)) 182 183 // WGSL: textureSampleLevel({{\(*}}t2DArray 184 && all(Tvn(T.Element(0)) == t2DArray.SampleLevel(float3(u, u, 0), 0)) 185 186 // WGSL: textureSampleLevel({{\(*}}tCubeArray 187 && all(Tvn(T.Element(0)) == tCubeArray.SampleLevel(float4(normalize(float3(u, 1 - u, u)), 0), 0)) 188 189#if TEST_WHEN_CONSTEXPR_WORKS_FOR_OFFSET 190 // Offset variant 191 192 // W-GSL: textureSampleLevel({{\(*}}t2D 193 && all(Tvn(T.Element(0)) == t2D.SampleLevel(float2(u, u), 0, int2(1, 1))) 194 195 // W-GSL: textureSampleLevel({{\(*}}t3D 196 && all(Tvn(T.Element(0)) == t3D.SampleLevel(float3(u, u, u), 0, int3(1, 1, 1))) 197 198 // W-GSL: textureSampleLevel({{\(*}}t2DArray 199 && all(Tvn(T.Element(0)) == t2DArray.SampleLevel(float3(u, u, 0), 0, int2(1, 1))) 200#endif // #if TEST_WHEN_CONSTEXPR_WORKS_FOR_OFFSET 201 202 // ================== 203 // float SampleCmp() 204 // https://www.w3.org/TR/WGSL/#texturesamplecompare 205 // ================== 206 207 // WGSL: textureSampleCompare({{\(*}}d2D 208 && float(0) == d2D.SampleCmp(float2(u, u), 0) 209 210 // WGSL: textureSampleCompare({{\(*}}d2DArray 211 && float(0) == d2DArray.SampleCmp(float3(u, u, 0), 0) 212 213 // WGSL: textureSampleCompare({{\(*}}dCube 214 && float(0) == dCube.SampleCmp(normalize(float3(u, 1 - u, u)), 0) 215 216 // WGSL: textureSampleCompare({{\(*}}dCubeArray 217 && float(0) == dCubeArray.SampleCmp(float4(normalize(float3(u, 1 - u, u)), 0), 0) 218 219#if TEST_WHEN_CONSTEXPR_WORKS_FOR_OFFSET 220 // Offset variant 221 222 // W-GSL: textureSampleCompare({{\(*}}d2D 223 && float(0) == d2D.SampleCmp(float2(u2, u), 0, int2(0, 0)) 224 225 // W-GSL: textureSampleCompare({{\(*}}d2DArray 226 && float(0) == d2DArray.SampleCmp(float3(u2, u, u), 0, int2(0, 0)) 227#endif // #if TEST_WHEN_CONSTEXPR_WORKS_FOR_OFFSET 228 229 // =================================== 230 // float SampleCmpLevelZero() 231 // https://www.w3.org/TR/WGSL/#texturesamplecomparelevel 232 // =================================== 233 234 // WGSL: textureSampleCompareLevel({{\(*}}d2D 235 && float(0) == d2D.SampleCmpLevelZero(float2(u, u), 0) 236 237 // WGSL: textureSampleCompareLevel({{\(*}}d2DArray 238 && float(0) == d2DArray.SampleCmpLevelZero(float3(u, u, 0), 0) 239 240 // WGSL: textureSampleCompareLevel({{\(*}}dCube 241 && float(0) == dCube.SampleCmpLevelZero(normalize(float3(u, 1 - u, u)), 0) 242 243 // WGSL: textureSampleCompareLevel({{\(*}}dCubeArray 244 && float(0) == dCubeArray.SampleCmpLevelZero(float4(normalize(float3(u, 1-u, u)), 0), 0) 245 246#if TEST_WHEN_CONSTEXPR_WORKS_FOR_OFFSET 247 // Offset variant 248 249 // W-GSL: textureSampleCompareLevel({{\(*}}d2D 250 && float(0) == d2D.SampleCmpLevelZero(float2(u2, u), 0, int2(0, 0)) 251 252 // W-GSL: textureSampleCompareLevel({{\(*}}d2DArray 253 && float(0) == d2DArray.SampleCmpLevelZero(float3(u2, u, u), 0, int2(0, 0)) 254#endif // #if TEST_WHEN_CONSTEXPR_WORKS_FOR_OFFSET 255 256 // ================================== 257 // vector<T,4> Gather() 258 // https://www.w3.org/TR/WGSL/#texturegather 259 // ================================== 260 261 // WGSL: textureGather({{.*}}t2D 262 && all(Tv4(T.Element(0)) == t2D.Gather(float2(u, u))) 263 264 // WGSL: textureGather({{.*}}tCube 265 && all(Tv4(T.Element(0)) == tCube.Gather(normalize(float3(u, 1 - u, u)))) 266 267 // WGSL: textureGather({{.*}}t2DArray 268 && all(Tv4(T.Element(0)) == t2DArray.Gather(float3(u, u, 0))) 269 270 // WGSL: textureGather({{.*}}tCubeArray 271 && all(Tv4(T.Element(0)) == tCubeArray.Gather(float4(normalize(float3(u, 1 - u, u)), 0))) 272 273#if TEST_WHEN_CONSTEXPR_WORKS_FOR_OFFSET 274 // Offset variant 275 276 // W-GSL: textureGather({{.*}}t2D 277 && all(Tv4(T.Element(0)) == t2D.Gather(float2(u2, u), int2(0, 0))) 278 279 // W-GSL: textureGather({{.*}}t2DArray 280 && all(Tv4(T.Element(0)) == t2DArray.Gather(float3(u2, u, 0), int2(0, 0))) 281#endif // #if TEST_WHEN_CONSTEXPR_WORKS_FOR_OFFSET 282 283 // ===================================== 284 // T SampleGrad() 285 // https://www.w3.org/TR/WGSL/#texturesamplegrad 286 // ===================================== 287 288 // WGSL doesn't support textureSampleGrad for 1D textures 289 290 // WGSL: textureSampleGrad({{\(*}}t2D 291 && all(Tvn(T.Element(0)) == t2D.SampleGrad(float2(u, u), float2(ddx, ddx), float2(ddy, ddy))) 292 293 // WGSL: textureSampleGrad({{\(*}}t3D 294 && all(Tvn(T.Element(0)) == t3D.SampleGrad(float3(u, u, u), float3(ddx, ddx, ddx), float3(ddy, ddy, ddy))) 295 296 // WGSL: textureSampleGrad({{\(*}}tCube 297 && all(Tvn(T.Element(0)) == tCube.SampleGrad(normalize(float3(u, 1 - u, u)), float3(ddx, ddx, ddx), float3(ddy, ddy, ddy))) 298 299 // WGSL: textureSampleGrad({{\(*}}t2DArray 300 && all(Tvn(T.Element(0)) == t2DArray.SampleGrad(float3(u, u, 0.0f), float2(ddx, ddx), float2(ddy, ddy))) 301 302 // WGSL: textureSampleGrad({{\(*}}tCubeArray 303 && all(Tvn(T.Element(0)) == tCubeArray.SampleGrad(float4(normalize(float3(u, 1 - u, u)), 0), float3(ddx, ddx, ddx), float3(ddy, ddy, ddy))) 304 305#if TEST_WHEN_CONSTEXPR_WORKS_FOR_OFFSET 306 // Offset variant 307 308 // W-GSL: textureSampleGrad({{\(*}}t2D 309 && all(Tvn(T.Element(0)) == t2D.SampleGrad(float2(u2, u), float2(ddx, ddx), float2(ddy, ddy), int2(0, 0))) 310 311 // W-GSL: textureSampleGrad({{\(*}}t3D 312 && all(Tvn(T.Element(0)) == t3D.SampleGrad(float3(u2, u, u), float3(ddx, ddx, ddx), float3(ddy, ddy, ddy), int3(0, 0, 0))) 313 314 // W-GSL: textureSampleGrad({{\(*}}t2DArray 315 && all(Tvn(T.Element(0)) == t2DArray.SampleGrad(float3(u2, u, 0.0f), float2(ddx, ddx), float2(ddy, ddy), int2(0, 0))) 316#endif // #if TEST_WHEN_CONSTEXPR_WORKS_FOR_OFFSET 317 ; 318 319 return result; 320} 321 322void fragMain() 323{ 324 bool result = true 325 && TEST_texture<float3>( 326 t1D_f32v3, 327 t2D_f32v3, 328 t3D_f32v3, 329 tCube_f32v3, 330 t1DArray_f32v3, 331 t2DArray_f32v3, 332 tCubeArray_f32v3) 333 && TEST_texture<float4>( 334 t1D_f32v4, 335 t2D_f32v4, 336 t3D_f32v4, 337 tCube_f32v4, 338 t1DArray_f32v4, 339 t2DArray_f32v4, 340 tCubeArray_f32v4) 341 ; 342 343 outputBuffer[0] = int(result); 344}