summaryrefslogtreecommitdiffstats
path: root/source/slang/slang-core-module-textures.cpp
diff options
context:
space:
mode:
authorEllie Hermaszewska <ellieh@nvidia.com>2024-10-29 14:49:26 +0800
committerGitHub <noreply@github.com>2024-10-29 14:49:26 +0800
commitf65d756bff8d4c5cbc15bd0322a2ae8e6b896a21 (patch)
treeea1d61342cd29368e19135000ec2948813096205 /source/slang/slang-core-module-textures.cpp
parenta729c15e9dce9f5116a38afc66329ab2ca4cea54 (diff)
format
* format * Minor test fixes * enable checking cpp format in ci
Diffstat (limited to 'source/slang/slang-core-module-textures.cpp')
-rw-r--r--source/slang/slang-core-module-textures.cpp238
1 files changed, 124 insertions, 114 deletions
diff --git a/source/slang/slang-core-module-textures.cpp b/source/slang/slang-core-module-textures.cpp
index 064529ff4..568c6f37d 100644
--- a/source/slang/slang-core-module-textures.cpp
+++ b/source/slang/slang-core-module-textures.cpp
@@ -1,7 +1,9 @@
#include "slang-core-module-textures.h"
+
#include <spirv/unified1/spirv.h>
-#define EMIT_LINE_DIRECTIVE() sb << "#line " << (__LINE__+1) << " \"slang-core-module-textures.cpp\"\n"
+#define EMIT_LINE_DIRECTIVE() \
+ sb << "#line " << (__LINE__ + 1) << " \"slang-core-module-textures.cpp\"\n"
namespace Slang
{
@@ -24,7 +26,7 @@ static_assert(SLANG_COUNT_OF(spaces) % indentWidth == 1);
struct BraceScope
{
BraceScope(const char*& i, StringBuilder& sb, const char* end = "\n")
- :i(i), sb(sb), end(end)
+ : i(i), sb(sb), end(end)
{
// If we hit this assert, it means that we are indenting too deep and
// need more spaces in 'spaces' above.
@@ -79,7 +81,7 @@ void TextureTypeInfo::writeFuncBody(
sb << i << "case cpp:\n";
sb << i << "case hlsl:\n";
sb << i << "__intrinsic_asm \"." << funcName << "\";\n";
- if(glsl.getLength())
+ if (glsl.getLength())
{
sb << i << "case glsl:\n";
if (glsl.startsWith("if"))
@@ -87,7 +89,7 @@ void TextureTypeInfo::writeFuncBody(
else
sb << i << "__intrinsic_asm \"" << glsl << "\";\n";
}
- if(cuda.getLength())
+ if (cuda.getLength())
{
sb << i << "case cuda:\n";
sb << i << "__intrinsic_asm \"" << cuda << "\";\n";
@@ -103,7 +105,7 @@ void TextureTypeInfo::writeFuncBody(
sb << i << "if (access == " << kCoreModule_ResourceAccessReadWrite << ")\n";
sb << i << "return spirv_asm\n";
{
- BraceScope spirvRWScope{ i, sb, ";\n" };
+ BraceScope spirvRWScope{i, sb, ";\n"};
sb << spirvRWDefault << "\n";
}
sb << i << "else if (isCombined != 0)\n";
@@ -174,19 +176,18 @@ void TextureTypeInfo::writeFunc(
cuda,
metal,
wgsl,
- readNoneMode
- );
+ readNoneMode);
}
void TextureTypeInfo::writeGetDimensionFunctions()
{
- static const char* kComponentNames[]{ "x", "y", "z", "w" };
+ static const char* kComponentNames[]{"x", "y", "z", "w"};
SlangResourceShape baseShape = base.baseShape;
// `GetDimensions`
- const char* dimParamTypes[] = { "out float ", "out int ", "out uint " };
- const char* dimParamTypesInner[] = { "float", "int", "uint" };
+ const char* dimParamTypes[] = {"out float ", "out int ", "out uint "};
+ const char* dimParamTypesInner[] = {"float", "int", "uint"};
for (int tid = 0; tid < 3; tid++)
{
auto t = dimParamTypes[tid];
@@ -223,8 +224,10 @@ void TextureTypeInfo::writeGetDimensionFunctions()
case SLANG_TEXTURE_1D:
++paramCount;
params << t << "width";
- metal << "(*($" << String(paramCount) << ") = $0.get_width(" << String(metalMipLevel) << ")),";
- wgsl << "($" << String(paramCount) << ") = textureDimensions($0" << (includeMipInfo ? ", $1" : "") << ");";
+ metal << "(*($" << String(paramCount) << ") = $0.get_width("
+ << String(metalMipLevel) << ")),";
+ wgsl << "($" << String(paramCount) << ") = textureDimensions($0"
+ << (includeMipInfo ? ", $1" : "") << ");";
sizeDimCount = 1;
break;
@@ -233,13 +236,15 @@ void TextureTypeInfo::writeGetDimensionFunctions()
case SLANG_TEXTURE_CUBE:
++paramCount;
params << t << "width,";
- metal << "(*($" << String(paramCount) << ") = $0.get_width(" << String(metalMipLevel) << ")),";
+ 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) << ")),";
+ metal << "(*($" << String(paramCount) << ") = $0.get_height("
+ << String(metalMipLevel) << ")),";
wgsl << "($" << String(paramCount) << ") = dim.y;";
sizeDimCount = 2;
@@ -248,26 +253,27 @@ void TextureTypeInfo::writeGetDimensionFunctions()
case SLANG_TEXTURE_3D:
++paramCount;
params << t << "width,";
- metal << "(*($" << String(paramCount) << ") = $0.get_width(" << String(metalMipLevel) << ")),";
+ 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) << ")),";
+ 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) << ")),";
+ metal << "(*($" << String(paramCount) << ") = $0.get_depth("
+ << String(metalMipLevel) << ")),";
wgsl << "($" << String(paramCount) << ") = dim.z;";
sizeDimCount = 3;
break;
- default:
- assert(!"unexpected");
- break;
+ default: assert(!"unexpected"); break;
}
if (isArray)
@@ -301,71 +307,70 @@ void TextureTypeInfo::writeGetDimensionFunctions()
StringBuilder glsl;
{
auto emitIntrinsic = [&](UnownedStringSlice funcName, bool useLodStr)
+ {
+ int aa = 1;
+ StringBuilder opStrSB;
+ opStrSB << " = " << funcName << "($0";
+ if (useLodStr)
{
- int aa = 1;
- StringBuilder opStrSB;
- opStrSB << " = " << funcName << "($0";
- if (useLodStr)
- {
- String lodStr = ", 0";
- if (includeMipInfo)
- {
- int mipLevelArg = aa++;
- lodStr = ", int($";
- lodStr.append(mipLevelArg);
- lodStr.append(")");
- }
- opStrSB << lodStr;
- }
- auto opStr = opStrSB.produceString();
- int cc = 0;
- switch (baseShape)
+ String lodStr = ", 0";
+ if (includeMipInfo)
{
- case SLANG_TEXTURE_1D:
- glsl << "($" << aa++ << opStr << ")";
- if (isArray)
- {
- glsl << ".x";
- }
- glsl << ")";
- cc = 1;
- break;
-
- case SLANG_TEXTURE_2D:
- case SLANG_TEXTURE_CUBE:
- glsl << "($" << aa++ << opStr << ").x)";
- glsl << ", ($" << aa++ << opStr << ").y)";
- cc = 2;
- break;
-
- case SLANG_TEXTURE_3D:
- glsl << "($" << aa++ << opStr << ").x)";
- glsl << ", ($" << aa++ << opStr << ").y)";
- glsl << ", ($" << aa++ << opStr << ").z)";
- cc = 3;
- break;
-
- default:
- SLANG_UNEXPECTED("unhandled resource shape");
- break;
+ int mipLevelArg = aa++;
+ lodStr = ", int($";
+ lodStr.append(mipLevelArg);
+ lodStr.append(")");
}
-
+ opStrSB << lodStr;
+ }
+ auto opStr = opStrSB.produceString();
+ int cc = 0;
+ switch (baseShape)
+ {
+ case SLANG_TEXTURE_1D:
+ glsl << "($" << aa++ << opStr << ")";
if (isArray)
{
- glsl << ", ($" << aa++ << opStr << ")." << kComponentNames[cc] << ")";
+ glsl << ".x";
}
+ glsl << ")";
+ cc = 1;
+ break;
+
+ case SLANG_TEXTURE_2D:
+ case SLANG_TEXTURE_CUBE:
+ glsl << "($" << aa++ << opStr << ").x)";
+ glsl << ", ($" << aa++ << opStr << ").y)";
+ cc = 2;
+ break;
+
+ case SLANG_TEXTURE_3D:
+ glsl << "($" << aa++ << opStr << ").x)";
+ glsl << ", ($" << aa++ << opStr << ").y)";
+ glsl << ", ($" << aa++ << opStr << ").z)";
+ cc = 3;
+ break;
+
+ default: SLANG_UNEXPECTED("unhandled resource shape"); break;
+ }
+
+ if (isArray)
+ {
+ glsl << ", ($" << aa++ << opStr << ")." << kComponentNames[cc] << ")";
+ }
- if (isMultisample)
- {
- glsl << ", ($" << aa++ << " = textureSamples($0))";
- }
+ if (isMultisample)
+ {
+ glsl << ", ($" << aa++ << " = textureSamples($0))";
+ }
- if (includeMipInfo)
- {
- glsl << ", ($" << aa++ << " = textureQueryLevels($0))";
- }
- };
- glsl << "if (access == " << kCoreModule_ResourceAccessReadOnly << ") __intrinsic_asm \"";
+ if (includeMipInfo)
+ {
+ glsl << ", ($" << aa++ << " = textureQueryLevels($0))";
+ }
+ };
+ glsl << "if (access == " << kCoreModule_ResourceAccessReadOnly
+ << ") __intrinsic_asm \"";
emitIntrinsic(toSlice("textureSize"), !isMultisample);
glsl << "\";\n";
glsl << "__intrinsic_asm \"";
@@ -374,53 +379,56 @@ void TextureTypeInfo::writeGetDimensionFunctions()
}
// SPIRV ASM generation
- auto generateSpirvAsm = [&](StringBuilder& spirv, bool isRW, UnownedStringSlice imageVar)
+ auto generateSpirvAsm =
+ [&](StringBuilder& spirv, bool isRW, UnownedStringSlice imageVar)
{
spirv << "%vecSize:$$uint";
- if (sizeDimCount > 1) spirv << sizeDimCount;
+ if (sizeDimCount > 1)
+ spirv << sizeDimCount;
spirv << " = ";
if (isMultisample || isRW)
spirv << "OpImageQuerySize " << imageVar << ";";
else
- spirv << "OpImageQuerySizeLod " << imageVar <<" $0;";
+ spirv << "OpImageQuerySizeLod " << imageVar << " $0;";
auto convertAndStore = [&](UnownedStringSlice uintSourceVal, const char* destParam)
+ {
+ if (UnownedStringSlice(rawT) == "uint")
{
- if (UnownedStringSlice(rawT) == "uint")
+ spirv << "OpStore &" << destParam << " %" << uintSourceVal << ";";
+ }
+ else
+ {
+ if (UnownedStringSlice(rawT) == "int")
{
- spirv << "OpStore &" << destParam << " %" << uintSourceVal << ";";
+ spirv << "%c_" << uintSourceVal << " : $$" << rawT << " = OpBitcast %"
+ << uintSourceVal << "; ";
}
else
{
- if (UnownedStringSlice(rawT) == "int")
- {
- spirv << "%c_" << uintSourceVal << " : $$" << rawT << " = OpBitcast %" << uintSourceVal << "; ";
- }
- else
- {
- spirv << "%c_" << uintSourceVal << " : $$" << rawT << " = OpConvertUToF %" << uintSourceVal << "; ";
- }
- spirv << "OpStore &" << destParam << "%c_" << uintSourceVal << ";";
+ spirv << "%c_" << uintSourceVal << " : $$" << rawT
+ << " = OpConvertUToF %" << uintSourceVal << "; ";
}
- };
+ spirv << "OpStore &" << destParam << "%c_" << uintSourceVal << ";";
+ }
+ };
auto extractSizeComponent = [&](int componentId, const char* destParam)
+ {
+ String elementVal = String("_") + destParam;
+ if (sizeDimCount == 1)
{
- String elementVal = String("_") + destParam;
- if (sizeDimCount == 1)
- {
- spirv << "%" << elementVal << " : $$uint = OpCopyObject %vecSize; ";
- }
- else
- {
- spirv << "%" << elementVal << " : $$uint = OpCompositeExtract %vecSize " << componentId << "; ";
- }
- convertAndStore(elementVal.getUnownedSlice(), destParam);
- };
+ spirv << "%" << elementVal << " : $$uint = OpCopyObject %vecSize; ";
+ }
+ else
+ {
+ spirv << "%" << elementVal << " : $$uint = OpCompositeExtract %vecSize "
+ << componentId << "; ";
+ }
+ convertAndStore(elementVal.getUnownedSlice(), destParam);
+ };
switch (baseShape)
{
- case SLANG_TEXTURE_1D:
- extractSizeComponent(0, "width");
- break;
+ case SLANG_TEXTURE_1D: extractSizeComponent(0, "width"); break;
case SLANG_TEXTURE_2D:
case SLANG_TEXTURE_CUBE:
@@ -434,9 +442,7 @@ void TextureTypeInfo::writeGetDimensionFunctions()
extractSizeComponent(2, "depth");
break;
- default:
- assert(!"unexpected");
- break;
+ default: assert(!"unexpected"); break;
}
if (isArray)
@@ -479,11 +485,15 @@ void TextureTypeInfo::writeGetDimensionFunctions()
sb << " __glsl_extension(GL_EXT_samplerless_texture_functions)\n";
sb << " [require(cpp";
- if (glsl.getLength()) sb << "_glsl";
+ if (glsl.getLength())
+ sb << "_glsl";
sb << "_hlsl";
- if (metal.getLength()) sb << "_metal";
- if (spirvDefault.getLength() && spirvCombined.getLength()) sb << "_spirv";
- if (wgsl.getLength()) sb << "_wgsl";
+ if (metal.getLength())
+ sb << "_metal";
+ if (spirvDefault.getLength() && spirvCombined.getLength())
+ sb << "_spirv";
+ if (wgsl.getLength())
+ sb << "_wgsl";
sb << ", texture_sm_4_1)]\n";
writeFunc(
@@ -502,4 +512,4 @@ void TextureTypeInfo::writeGetDimensionFunctions()
}
}
-}
+} // namespace Slang