summaryrefslogtreecommitdiffstats
path: root/source/slang/slang-stdlib-textures.cpp
diff options
context:
space:
mode:
authorJay Kwak <82421531+jkwak-work@users.noreply.github.com>2024-05-14 15:42:12 -0700
committerGitHub <noreply@github.com>2024-05-14 15:42:12 -0700
commitd76bed6c1b03e5d7ef19c947fdd5fcaf33b595f7 (patch)
treea5709a08298ead8f5fe2fb51f1b0f30c61505a3d /source/slang/slang-stdlib-textures.cpp
parent5ceb8569b1ac7898c437b0c47ad29a5d8a9f7d90 (diff)
Implement texture functions for Metal target (#4158)
* Impl texture APIs for Metal target This commit is to implement texture functions for Metal target. The following functions are implemented and tested. - GetDimensions() - CalculateLevelOfDetail() - CalculateLevelOfDetailUnclamped() - Sample() - SampleBias() - SampleLevel() - SampleCmp() - SampleCmpLevelZero() - Gather() - SampleGrad() - Load() Metal has limited support for the texture functions compared to HLSL. - LOD is not supported for 1D texture, - Depth textures are limited to 2D, 2DArray, Cube and CubeArray textures. - "Offset" variants are limited to 2D, 2DArray, 2D-Depth, 2DArray-Depth and 3D textures. The functions that cannot be implemented for Metal should properly be handled by the capability system later. * Fix the failing test, multi-file.hlsl I am not sure why this change is needed. * Fix compile errors on macOS 2nd try * Remove a typo character to fix the compile error * Trivial clean up * Remove `as_type` where it was intended as static_cast * Use a simpler sytax for __intrinsic_asm * Trivial clean up * Remove TEST_AFTER_FIXING_CAPABILITY_PROBLEM after fixing normalize * Fix the failing test properly * Fix an incorrect setup of Depth-cube texture --------- Co-authored-by: Yong He <yonghe@outlook.com>
Diffstat (limited to 'source/slang/slang-stdlib-textures.cpp')
-rw-r--r--source/slang/slang-stdlib-textures.cpp59
1 files changed, 53 insertions, 6 deletions
diff --git a/source/slang/slang-stdlib-textures.cpp b/source/slang/slang-stdlib-textures.cpp
index f874c5da9..380ed1677 100644
--- a/source/slang/slang-stdlib-textures.cpp
+++ b/source/slang/slang-stdlib-textures.cpp
@@ -68,7 +68,8 @@ void TextureTypeInfo::writeFuncBody(
const String& cuda,
const String& spirvDefault,
const String& spirvRWDefault,
- const String& spirvCombined)
+ const String& spirvCombined,
+ const String& metal)
{
BraceScope funcScope{i, sb};
{
@@ -90,6 +91,11 @@ void TextureTypeInfo::writeFuncBody(
sb << i << "case cuda:\n";
sb << i << "__intrinsic_asm \"" << cuda << "\";\n";
}
+ if (metal.getLength())
+ {
+ sb << i << "case metal:\n";
+ sb << i << "__intrinsic_asm \"" << metal << "\";\n";
+ }
if(spirvDefault.getLength() && spirvCombined.getLength())
{
sb << i << "case spirv:\n";
@@ -127,14 +133,14 @@ void TextureTypeInfo::writeFuncWithSig(
const String& spirvRWDefault,
const String& spirvCombined,
const String& cuda,
+ const String& metal,
const ReadNoneMode readNoneMode)
{
if (readNoneMode == ReadNoneMode::Always)
sb << i << "[__readNone]\n";
- sb << i << "[__readNone]\n";
sb << i << "[ForceInline]\n";
sb << i << sig << "\n";
- writeFuncBody(funcName, glsl, cuda, spirvDefault, spirvRWDefault, spirvCombined);
+ writeFuncBody(funcName, glsl, cuda, spirvDefault, spirvRWDefault, spirvCombined, metal);
sb << "\n";
}
@@ -147,6 +153,7 @@ void TextureTypeInfo::writeFunc(
const String& spirvRWDefault,
const String& spirvCombined,
const String& cuda,
+ const String& metal,
const ReadNoneMode readNoneMode)
{
writeFuncWithSig(
@@ -157,6 +164,7 @@ void TextureTypeInfo::writeFunc(
spirvRWDefault,
spirvCombined,
cuda,
+ metal,
readNoneMode
);
}
@@ -184,27 +192,56 @@ void TextureTypeInfo::writeGetDimensionFunctions()
int sizeDimCount = 0;
StringBuilder params;
+ int paramCount = 0;
+
+ StringBuilder metal;
+ const char* metalMipLevel = "0";
+
if (includeMipInfo)
- params << "uint mipLevel, ";
+ {
+ ++paramCount;
+ params << "uint mipLevel,";
+
+ if (baseShape != SLANG_TEXTURE_1D)
+ metalMipLevel = "$1";
+ }
switch (baseShape)
{
case SLANG_TEXTURE_1D:
+ ++paramCount;
params << t << "width";
+ metal << "(*($" << String(paramCount) << ") = $0.get_width(" << String(metalMipLevel) << ")),";
+
sizeDimCount = 1;
break;
case SLANG_TEXTURE_2D:
case SLANG_TEXTURE_CUBE:
+ ++paramCount;
params << t << "width,";
+ metal << "(*($" << String(paramCount) << ") = $0.get_width(" << String(metalMipLevel) << ")),";
+
+ ++paramCount;
params << t << "height";
+ metal << "(*($" << String(paramCount) << ") = $0.get_height(" << String(metalMipLevel) << ")),";
+
sizeDimCount = 2;
break;
case SLANG_TEXTURE_3D:
+ ++paramCount;
params << t << "width,";
+ metal << "(*($" << String(paramCount) << ") = $0.get_width(" << String(metalMipLevel) << ")),";
+
+ ++paramCount;
params << t << "height,";
+ metal << "(*($" << String(paramCount) << ") = $0.get_height(" << String(metalMipLevel) << ")),";
+
+ ++paramCount;
params << t << "depth";
+ metal << "(*($" << String(paramCount) << ") = $0.get_depth(" << String(metalMipLevel) << ")),";
+
sizeDimCount = 3;
break;
@@ -215,18 +252,27 @@ void TextureTypeInfo::writeGetDimensionFunctions()
if (isArray)
{
+ ++sizeDimCount;
+ ++paramCount;
params << ", " << t << "elements";
- sizeDimCount++;
+ metal << "(*($" << String(paramCount) << ") = $0.get_array_size()),";
}
if (isMultisample)
{
+ ++paramCount;
params << ", " << t << "sampleCount";
+ metal << "(*($" << String(paramCount) << ") = $0.get_num_samples()),";
}
if (includeMipInfo)
+ {
+ ++paramCount;
params << ", " << t << "numberOfLevels";
+ metal << "(*($" << String(paramCount) << ") = $0.get_num_mip_levels()),";
+ }
+ metal.reduceLength(metal.getLength() - 1); // drop the last comma
StringBuilder glsl;
{
@@ -407,7 +453,7 @@ void TextureTypeInfo::writeGetDimensionFunctions()
sb << " __glsl_version(450)\n";
sb << " __glsl_extension(GL_EXT_samplerless_texture_functions)\n";
- sb << " [require(glsl_spirv, texture_sm_4_1)]\n";
+ sb << " [require(glsl_hlsl_metal_spirv, texture_sm_4_1)]\n";
writeFunc(
"void",
"GetDimensions",
@@ -417,6 +463,7 @@ void TextureTypeInfo::writeGetDimensionFunctions()
spirvRWDefault,
spirvCombined,
"",
+ metal,
ReadNoneMode::Always);
}
}