diff options
| author | Anders Leino <aleino@nvidia.com> | 2024-11-14 21:49:01 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-11-14 11:49:01 -0800 |
| commit | f0bc4642a563e2318634b38a5a7ac2c3ddd68917 (patch) | |
| tree | 3a17a128af7a854006fb62184db53428b70830a6 | |
| parent | 147ceb1991454b7a5ba6f3ec0c149dd40360a31d (diff) | |
Insert some casts for WGSL texture attribute queries (#5560)
* Add new texture sampling test for WebGPU
There are no 1d array textures in WGSL, so
add texture-sampling-no-1d-arrays.slang based on texture-sampling.slang, but without
1d texture arrays.
This helps to address issue #4943.
* Insert needed conversion when querying texture attributes in WGSL
This helps to address issue #4943.
---------
Co-authored-by: Yong He <yonghe@outlook.com>
| -rw-r--r-- | source/slang/slang-core-module-textures.cpp | 90 | ||||
| -rw-r--r-- | tests/compute/texture-sampling-no-1d-arrays.slang | 118 | ||||
| -rw-r--r-- | tests/compute/texture-sampling-no-1d-arrays.slang.expected.txt | 5 |
3 files changed, 200 insertions, 13 deletions
diff --git a/source/slang/slang-core-module-textures.cpp b/source/slang/slang-core-module-textures.cpp index e0b8d2b27..22c1fc63f 100644 --- a/source/slang/slang-core-module-textures.cpp +++ b/source/slang/slang-core-module-textures.cpp @@ -180,6 +180,55 @@ void TextureTypeInfo::writeFunc( readNoneMode); } +enum class DimType +{ + Float, + Int, + UInt, + + Count, +}; + +// The WGSL texture attribute types for 'expr' are unsigned int, and anything else requires a +// conversion. +template<typename S> +static String wgslTextureAttributeConversion(DimType type, S expr) +{ + + switch (type) + { + + case DimType::UInt: + return expr; + + + case DimType::Float: + { + // Conversion to float is exact for values <= 2^24. + String castExpr("f32("); + castExpr.append(expr); + castExpr.append(")"); + return castExpr; + } + break; + + case DimType::Int: + { + // We can assume two's complement and just do a bitcast, since texture dimensions can't + // be anywhere near big enough to yield a negative result. + String castExpr("bitcast<i32>("); + castExpr.append(expr); + castExpr.append(")"); + return castExpr; + } + break; + + default: + SLANG_UNREACHABLE("Unexpected DimType enum value"); + break; + }; +} + void TextureTypeInfo::writeGetDimensionFunctions() { static const char* kComponentNames[]{"x", "y", "z", "w"}; @@ -187,10 +236,11 @@ void TextureTypeInfo::writeGetDimensionFunctions() SlangResourceShape baseShape = base.baseShape; // `GetDimensions` - const char* dimParamTypes[] = {"out float ", "out int ", "out uint "}; - const char* dimParamTypesInner[] = {"float", "int", "uint"}; - for (int tid = 0; tid < 3; tid++) + const char* dimParamTypes[int(DimType::Count)] = {"out float ", "out int ", "out uint "}; + const char* dimParamTypesInner[int(DimType::Count)] = {"float", "int", "uint"}; + for (int tid = 0; tid < int(DimType::Count); tid++) { + DimType dimType = DimType(tid); auto t = dimParamTypes[tid]; auto rawT = dimParamTypesInner[tid]; @@ -227,8 +277,11 @@ void TextureTypeInfo::writeGetDimensionFunctions() params << t << "width"; metal << "(*($" << String(paramCount) << ") = $0.get_width(" << String(metalMipLevel) << ")),"; - wgsl << "($" << String(paramCount) << ") = textureDimensions($0" - << (includeMipInfo ? ", $1" : "") << ");"; + wgsl << "($" << String(paramCount) << ") = " + << wgslTextureAttributeConversion( + dimType, + String("textureDimensions($0") + (includeMipInfo ? ", $1" : "") + ")") + << ";"; sizeDimCount = 1; break; @@ -240,13 +293,15 @@ void TextureTypeInfo::writeGetDimensionFunctions() metal << "(*($" << String(paramCount) << ") = $0.get_width(" << String(metalMipLevel) << ")),"; wgsl << "var dim = textureDimensions($0" << (includeMipInfo ? ", $1" : "") << ");"; - wgsl << "($" << String(paramCount) << ") = dim.x;"; + wgsl << "($" << String(paramCount) + << ") = " << wgslTextureAttributeConversion(dimType, "dim.x") << ";"; ++paramCount; params << t << "height"; metal << "(*($" << String(paramCount) << ") = $0.get_height(" << String(metalMipLevel) << ")),"; - wgsl << "($" << String(paramCount) << ") = dim.y;"; + wgsl << "($" << String(paramCount) + << ") = " << wgslTextureAttributeConversion(dimType, "dim.y") << ";"; sizeDimCount = 2; break; @@ -257,19 +312,22 @@ void TextureTypeInfo::writeGetDimensionFunctions() metal << "(*($" << String(paramCount) << ") = $0.get_width(" << String(metalMipLevel) << ")),"; wgsl << "var dim = textureDimensions($0" << (includeMipInfo ? ", $1" : "") << ");"; - wgsl << "($" << String(paramCount) << ") = dim.x;"; + wgsl << "($" << String(paramCount) + << ") = " << wgslTextureAttributeConversion(dimType, "dim.x") << ";"; ++paramCount; params << t << "height,"; metal << "(*($" << String(paramCount) << ") = $0.get_height(" << String(metalMipLevel) << ")),"; - wgsl << "($" << String(paramCount) << ") = dim.y;"; + wgsl << "($" << String(paramCount) + << ") = " << wgslTextureAttributeConversion(dimType, "dim.y") << ";"; ++paramCount; params << t << "depth"; metal << "(*($" << String(paramCount) << ") = $0.get_depth(" << String(metalMipLevel) << ")),"; - wgsl << "($" << String(paramCount) << ") = dim.z;"; + wgsl << "($" << String(paramCount) + << ") = " << wgslTextureAttributeConversion(dimType, "dim.z") << ";"; sizeDimCount = 3; break; @@ -285,7 +343,9 @@ void TextureTypeInfo::writeGetDimensionFunctions() ++paramCount; params << ", " << t << "elements"; metal << "(*($" << String(paramCount) << ") = $0.get_array_size()),"; - wgsl << "($" << String(paramCount) << ") = textureNumLayers($0);"; + wgsl << "($" << String(paramCount) + << ") = " << wgslTextureAttributeConversion(dimType, "textureNumLayers($0)") + << ";"; } if (isMultisample) @@ -293,7 +353,9 @@ void TextureTypeInfo::writeGetDimensionFunctions() ++paramCount; params << ", " << t << "sampleCount"; metal << "(*($" << String(paramCount) << ") = $0.get_num_samples()),"; - wgsl << "($" << String(paramCount) << ") = textureNumSamples($0);"; + wgsl << "($" << String(paramCount) + << ") = " << wgslTextureAttributeConversion(dimType, "textureNumSamples($0)") + << ";"; } if (includeMipInfo) @@ -301,7 +363,9 @@ void TextureTypeInfo::writeGetDimensionFunctions() ++paramCount; params << ", " << t << "numberOfLevels"; metal << "(*($" << String(paramCount) << ") = $0.get_num_mip_levels()),"; - wgsl << "($" << String(paramCount) << ") = textureNumLevels($0);"; + wgsl << "($" << String(paramCount) + << ") = " << wgslTextureAttributeConversion(dimType, "textureNumLevels($0)") + << ";"; } metal.reduceLength(metal.getLength() - 1); // drop the last comma diff --git a/tests/compute/texture-sampling-no-1d-arrays.slang b/tests/compute/texture-sampling-no-1d-arrays.slang new file mode 100644 index 000000000..ed982ef70 --- /dev/null +++ b/tests/compute/texture-sampling-no-1d-arrays.slang @@ -0,0 +1,118 @@ +//TEST(compute):COMPARE_RENDER_COMPUTE: -shaderobj -output-using-type +//TEST(compute):COMPARE_RENDER_COMPUTE: -shaderobj -output-using-type -vk +//TEST(compute):COMPARE_RENDER_COMPUTE: -shaderobj -output-using-type -mtl + + +//TEST_INPUT: Texture1D(size=4, content = one):name=t1D +//TEST_INPUT: Texture2D(size=4, content = one):name=t2D +//TEST_INPUT: Texture3D(size=4, content = one):name=t3D +//TEST_INPUT: TextureCube(size=4, content = one):name=tCube +//TEST_INPUT: Texture2D(size=4, content = one, arrayLength=2):name=t2dArray +//TEST_INPUT: TextureCube(size=4, content = one, arrayLength=2):name=tCubeArray +//TEST_INPUT: Sampler:name=samplerState +//TEST_INPUT: ubuffer(data=[0 0 0 0], stride=4):out,name=outputBuffer + +// There are still some issues to fix in RHI, see issue #4943 +//DISABLE_TEST(compute):COMPARE_COMPUTE:-slang -shaderobj -wgpu + +Texture1D t1D; +Texture2D t2D; +Texture3D t3D; +TextureCube tCube; +Texture2DArray t2dArray; +TextureCubeArray tCubeArray; +SamplerState samplerState; +RWStructuredBuffer<float> outputBuffer; + +cbuffer Uniforms +{ + float4x4 modelViewProjection; +} + +struct AssembledVertex +{ + float3 position; + float3 color; + float2 uv; +}; + +struct CoarseVertex +{ + float3 color; + float2 uv; +}; + +struct Fragment +{ + float4 color; +}; + + +// Vertex Shader + +struct VertexStageInput +{ + AssembledVertex assembledVertex : A; +}; + +struct VertexStageOutput +{ + CoarseVertex coarseVertex : CoarseVertex; + float4 sv_position : SV_Position; +}; + +VertexStageOutput vertexMain(VertexStageInput input) +{ + VertexStageOutput output; + + float3 position = input.assembledVertex.position; + float3 color = input.assembledVertex.color; + + output.coarseVertex.color = color; + output.sv_position = mul(modelViewProjection, float4(position, 1.0)); + output.coarseVertex.uv = input.assembledVertex.uv; + return output; +} + +// Fragment Shader + +struct FragmentStageInput +{ + CoarseVertex coarseVertex : CoarseVertex; +}; + +struct FragmentStageOutput +{ + Fragment fragment : SV_Target; +}; + +FragmentStageOutput fragmentMain(FragmentStageInput input) +{ + FragmentStageOutput output; + + float3 color = input.coarseVertex.color; + float2 uv = input.coarseVertex.uv; + output.fragment.color = float4(color, 1.0); + + float4 val = 0.0; + val += t1D.Sample(samplerState, uv.x); + val += t2D.Sample(samplerState, uv); + val += t3D.Sample (samplerState, float3(uv, 0.5)); + + val += t2dArray.Sample(samplerState, float3(uv, 0.0)); + + val += tCubeArray.Sample(samplerState, float4(uv, 0.5, 0.0)); + val += tCube.Sample(samplerState, float3(uv, 0.5)); + + val += t2D.Load(int3(0)); + val += t2dArray.Load(int4(0)); + + val += t3D[int3(0)]; + + outputBuffer[0] = val.x; + + int w, h, l, lods; + t2dArray.GetDimensions(0, w, h, l, lods); + outputBuffer[1] = w + h + l + lods; + return output; +} diff --git a/tests/compute/texture-sampling-no-1d-arrays.slang.expected.txt b/tests/compute/texture-sampling-no-1d-arrays.slang.expected.txt new file mode 100644 index 000000000..279fd69f2 --- /dev/null +++ b/tests/compute/texture-sampling-no-1d-arrays.slang.expected.txt @@ -0,0 +1,5 @@ +type: float +9.000000 +13.000000 +0.000000 +0.000000 |
