summaryrefslogtreecommitdiff
path: root/source/slang/slang-stdlib-textures.cpp
diff options
context:
space:
mode:
authorJay Kwak <82421531+jkwak-work@users.noreply.github.com>2024-09-20 19:55:49 -0700
committerGitHub <noreply@github.com>2024-09-20 19:55:49 -0700
commitc42b5e24b5b9d6b03352d809e0a49485d361154f (patch)
tree50226429a437230ab2920d2230299c0a5a9f5bd2 /source/slang/slang-stdlib-textures.cpp
parenta7fc5b4fa42a7bb0b63436e4c7aad5d5bbb9efe3 (diff)
WGSL implement texture intrinsics except gather and sampler-less (#5123)
This commit implements all of the texture intrinsics for WGSL except "Gather" and sampler-less. They will be implemented in a separate PR. A few things to note: - texture sampling functions are available only for the fragment shader stage; not for compute - WGSL doesn't have any functions similar to CalculateLevelOfDetail or CalculateLevelOfDetailUnclamped. - WGSL doesn't have a function overlaoding for textureSample with "clamp" or "status" arguments. - WGSL doesn't support Load operation with offset for texture_multisampled_XX and texture_storage_XX. - WGSL supports only four types of depth textures: 2D, 2D_array, cube and cube_array. - WGSL doesn't support "offset" variants for cube and cube_array.
Diffstat (limited to 'source/slang/slang-stdlib-textures.cpp')
-rw-r--r--source/slang/slang-stdlib-textures.cpp41
1 files changed, 37 insertions, 4 deletions
diff --git a/source/slang/slang-stdlib-textures.cpp b/source/slang/slang-stdlib-textures.cpp
index dd4df7d9c..a9f14522b 100644
--- a/source/slang/slang-stdlib-textures.cpp
+++ b/source/slang/slang-stdlib-textures.cpp
@@ -69,7 +69,8 @@ void TextureTypeInfo::writeFuncBody(
const String& spirvDefault,
const String& spirvRWDefault,
const String& spirvCombined,
- const String& metal)
+ const String& metal,
+ const String& wgsl)
{
BraceScope funcScope{i, sb};
{
@@ -96,7 +97,7 @@ void TextureTypeInfo::writeFuncBody(
sb << i << "case metal:\n";
sb << i << "__intrinsic_asm \"" << metal << "\";\n";
}
- if(spirvDefault.getLength() && spirvCombined.getLength())
+ if (spirvDefault.getLength() && spirvCombined.getLength())
{
sb << i << "case spirv:\n";
sb << i << "if (access == " << kStdlibResourceAccessReadWrite << ")\n";
@@ -122,6 +123,11 @@ void TextureTypeInfo::writeFuncBody(
}
sb << i << "}\n";
}
+ if (wgsl.getLength())
+ {
+ sb << i << "case wgsl:\n";
+ sb << i << "__intrinsic_asm \"" << wgsl << "\";\n";
+ }
}
}
@@ -134,13 +140,14 @@ void TextureTypeInfo::writeFuncWithSig(
const String& spirvCombined,
const String& cuda,
const String& metal,
+ const String& wgsl,
const ReadNoneMode readNoneMode)
{
if (readNoneMode == ReadNoneMode::Always)
sb << i << "[__readNone]\n";
sb << i << "[ForceInline]\n";
sb << i << sig << "\n";
- writeFuncBody(funcName, glsl, cuda, spirvDefault, spirvRWDefault, spirvCombined, metal);
+ writeFuncBody(funcName, glsl, cuda, spirvDefault, spirvRWDefault, spirvCombined, metal, wgsl);
sb << "\n";
}
@@ -154,6 +161,7 @@ void TextureTypeInfo::writeFunc(
const String& spirvCombined,
const String& cuda,
const String& metal,
+ const String& wgsl,
const ReadNoneMode readNoneMode)
{
writeFuncWithSig(
@@ -165,6 +173,7 @@ void TextureTypeInfo::writeFunc(
spirvCombined,
cuda,
metal,
+ wgsl,
readNoneMode
);
}
@@ -197,6 +206,9 @@ void TextureTypeInfo::writeGetDimensionFunctions()
StringBuilder metal;
const char* metalMipLevel = "0";
+ StringBuilder wgsl;
+ wgsl << "{";
+
if (includeMipInfo)
{
++paramCount;
@@ -212,6 +224,7 @@ void TextureTypeInfo::writeGetDimensionFunctions()
++paramCount;
params << t << "width";
metal << "(*($" << String(paramCount) << ") = $0.get_width(" << String(metalMipLevel) << ")),";
+ wgsl << "*($" << String(paramCount) << ") = textureDimensions($0" << (includeMipInfo ? ", $1" : "") << ");";
sizeDimCount = 1;
break;
@@ -221,10 +234,13 @@ void TextureTypeInfo::writeGetDimensionFunctions()
++paramCount;
params << t << "width,";
metal << "(*($" << String(paramCount) << ") = $0.get_width(" << String(metalMipLevel) << ")),";
+ wgsl << "var dim = textureDimensions($0" << (includeMipInfo ? ", $1" : "") << ");";
+ wgsl << "(*$" << String(paramCount) << ") = dim.x;";
++paramCount;
params << t << "height";
metal << "(*($" << String(paramCount) << ") = $0.get_height(" << String(metalMipLevel) << ")),";
+ wgsl << "(*$" << String(paramCount) << ") = dim.y;";
sizeDimCount = 2;
break;
@@ -233,14 +249,18 @@ void TextureTypeInfo::writeGetDimensionFunctions()
++paramCount;
params << t << "width,";
metal << "(*($" << String(paramCount) << ") = $0.get_width(" << String(metalMipLevel) << ")),";
+ wgsl << "var dim = textureDimensions($0" << (includeMipInfo ? ", $1" : "") << ");";
+ wgsl << "(*$" << String(paramCount) << ") = dim.x;";
++paramCount;
params << t << "height,";
metal << "(*($" << String(paramCount) << ") = $0.get_height(" << String(metalMipLevel) << ")),";
+ wgsl << "(*$" << String(paramCount) << ") = dim.y;";
++paramCount;
params << t << "depth";
metal << "(*($" << String(paramCount) << ") = $0.get_depth(" << String(metalMipLevel) << ")),";
+ wgsl << "(*$" << String(paramCount) << ") = dim.z;";
sizeDimCount = 3;
break;
@@ -256,6 +276,7 @@ void TextureTypeInfo::writeGetDimensionFunctions()
++paramCount;
params << ", " << t << "elements";
metal << "(*($" << String(paramCount) << ") = $0.get_array_size()),";
+ wgsl << "(*$" << String(paramCount) << ") = textureNumLayers($0);";
}
if (isMultisample)
@@ -263,6 +284,7 @@ void TextureTypeInfo::writeGetDimensionFunctions()
++paramCount;
params << ", " << t << "sampleCount";
metal << "(*($" << String(paramCount) << ") = $0.get_num_samples()),";
+ wgsl << "(*$" << String(paramCount) << ") = textureNumSamples($0);";
}
if (includeMipInfo)
@@ -270,9 +292,11 @@ void TextureTypeInfo::writeGetDimensionFunctions()
++paramCount;
params << ", " << t << "numberOfLevels";
metal << "(*($" << String(paramCount) << ") = $0.get_num_mip_levels()),";
+ wgsl << "(*$" << String(paramCount) << ") = textureNumLevels($0);";
}
metal.reduceLength(metal.getLength() - 1); // drop the last comma
+ wgsl << "}";
StringBuilder glsl;
{
@@ -453,7 +477,15 @@ void TextureTypeInfo::writeGetDimensionFunctions()
sb << " __glsl_version(450)\n";
sb << " __glsl_extension(GL_EXT_samplerless_texture_functions)\n";
- sb << " [require(cpp_glsl_hlsl_metal_spirv, texture_sm_4_1)]\n";
+
+ sb << " [require(cpp";
+ if (glsl.getLength()) sb << "_glsl";
+ sb << "_hlsl";
+ if (metal.getLength()) sb << "_metal";
+ if (spirvDefault.getLength() && spirvCombined.getLength()) sb << "_spirv";
+ if (wgsl.getLength()) sb << "_wgsl";
+ sb << ", texture_sm_4_1)]\n";
+
writeFunc(
"void",
"GetDimensions",
@@ -464,6 +496,7 @@ void TextureTypeInfo::writeGetDimensionFunctions()
spirvCombined,
"",
metal,
+ wgsl,
ReadNoneMode::Always);
}
}