diff options
| author | Devon <devonrutledge03@gmail.com> | 2025-03-20 00:38:52 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-03-20 06:38:52 +0000 |
| commit | 30068d75fb97fecc163253fb331daddbb09234e6 (patch) | |
| tree | 6e1a5b757db538a58c2397de4980618d280783d4 | |
| parent | 404e95cdd02830ba0fb215081e826025609c4d14 (diff) | |
Make image format reflection easier (#6550)
* image format json reflection
* format code
* use direct include
---------
Co-authored-by: slangbot <186143334+slangbot@users.noreply.github.com>
| -rw-r--r-- | include/slang-deprecated.h | 2 | ||||
| -rw-r--r-- | include/slang.h | 5 | ||||
| -rw-r--r-- | source/slang/slang-reflection-api.cpp | 17 | ||||
| -rw-r--r-- | source/slang/slang-reflection-json.cpp | 10 | ||||
| -rw-r--r-- | tests/reflection/texture-resource-type.slang | 7 | ||||
| -rw-r--r-- | tests/reflection/texture-resource-type.slang.expected | 104 | ||||
| -rw-r--r-- | tests/reflection/used-parameters.slang.expected | 16 |
7 files changed, 142 insertions, 19 deletions
diff --git a/include/slang-deprecated.h b/include/slang-deprecated.h index df6e41488..2ae91c6d8 100644 --- a/include/slang-deprecated.h +++ b/include/slang-deprecated.h @@ -681,6 +681,8 @@ extern "C" SLANG_API size_t spReflectionVariableLayout_GetSpace( SlangReflectionVariableLayout* var, SlangParameterCategory category); + SLANG_API SlangImageFormat + spReflectionVariableLayout_GetImageFormat(SlangReflectionVariableLayout* var); SLANG_API char const* spReflectionVariableLayout_GetSemanticName( SlangReflectionVariableLayout* var); diff --git a/include/slang.h b/include/slang.h index aab6fc47c..0507d8099 100644 --- a/include/slang.h +++ b/include/slang.h @@ -2921,6 +2921,11 @@ struct VariableLayoutReflection (SlangParameterCategory)category); } + SlangImageFormat getImageFormat() + { + return spReflectionVariableLayout_GetImageFormat((SlangReflectionVariableLayout*)this); + } + char const* getSemanticName() { return spReflectionVariableLayout_GetSemanticName((SlangReflectionVariableLayout*)this); diff --git a/source/slang/slang-reflection-api.cpp b/source/slang/slang-reflection-api.cpp index 9295bfdf6..5dfa9d55a 100644 --- a/source/slang/slang-reflection-api.cpp +++ b/source/slang/slang-reflection-api.cpp @@ -3305,6 +3305,23 @@ SLANG_API size_t spReflectionVariableLayout_GetSpace( return space; } +SLANG_API SlangImageFormat +spReflectionVariableLayout_GetImageFormat(SlangReflectionVariableLayout* inVarLayout) +{ + auto varLayout = convert(inVarLayout); + if (!varLayout) + return SLANG_IMAGE_FORMAT_unknown; + + if (auto leafVar = varLayout->getVariable()) + { + if (auto formatAttrib = leafVar->findModifier<FormatAttribute>()) + { + return (SlangImageFormat)formatAttrib->format; + } + } + return SLANG_IMAGE_FORMAT_unknown; +} + SLANG_API char const* spReflectionVariableLayout_GetSemanticName( SlangReflectionVariableLayout* inVarLayout) { diff --git a/source/slang/slang-reflection-json.cpp b/source/slang/slang-reflection-json.cpp index 4e8b6b2a4..82e8e1e3f 100644 --- a/source/slang/slang-reflection-json.cpp +++ b/source/slang/slang-reflection-json.cpp @@ -2,6 +2,7 @@ #include "slang-reflection-json.h" #include "../core/slang-blob.h" +#include "slang-ast-support-types.h" template<typename T> struct Range @@ -237,6 +238,15 @@ static void emitReflectionVarBindingInfoJSON( writer << "\"semanticIndex\": " << int(semanticIndex); } } + + if (auto format = var->getImageFormat()) + { + writer.maybeComma(); + auto formatName = getImageFormatInfo((Slang::ImageFormat)format).name; + writer << "\"format\": \""; + writer << formatName; + writer << "\""; + } } static void emitReflectionNameInfoJSON(PrettyWriter& writer, char const* name) diff --git a/tests/reflection/texture-resource-type.slang b/tests/reflection/texture-resource-type.slang index 2455ba611..36f7ff8d2 100644 --- a/tests/reflection/texture-resource-type.slang +++ b/tests/reflection/texture-resource-type.slang @@ -1,12 +1,13 @@ // texture-resource-type.slang -// Tests reflection of inner texture type. +// Tests reflection of inner texture type and formats. //TEST:REFLECTION:-stage compute -entry main -target hlsl Texture2D NoParameters; Texture2D<float> FloatTexture; Texture2D<float2> FloatTwoTexture; +Texture2D<float3> FloatThreeTexture; Texture2D<float4> FloatFourTexture; Texture2D<int> IntTexture; Texture2D<int2> IntTwoTexture; @@ -14,6 +15,10 @@ Texture2D<int4> IntFourTexture; Texture2D<uint> UintTexture; Texture2D<uint2> UintTwoTexture; Texture2D<uint4> UintFourTexture; +[format("r11f_g11f_b10f")] +Texture2D R11G11B10Texture; +[format("rgb10_a2ui")] +Texture2D<uint3> RGB10_A2_UI_Texture; [numthreads(1, 1, 1)] void main(uint3 dispatchThreadID : SV_DispatchThreadID) diff --git a/tests/reflection/texture-resource-type.slang.expected b/tests/reflection/texture-resource-type.slang.expected index 48d1f0f79..c318a8330 100644 --- a/tests/reflection/texture-resource-type.slang.expected +++ b/tests/reflection/texture-resource-type.slang.expected @@ -49,13 +49,29 @@ standard output = { } }, { - "name": "FloatFourTexture", + "name": "FloatThreeTexture", "binding": {"kind": "shaderResource", "index": 3}, "type": { "kind": "resource", "baseShape": "texture2D", "resultType": { "kind": "vector", + "elementCount": 3, + "elementType": { + "kind": "scalar", + "scalarType": "float32" + } + } + } + }, + { + "name": "FloatFourTexture", + "binding": {"kind": "shaderResource", "index": 4}, + "type": { + "kind": "resource", + "baseShape": "texture2D", + "resultType": { + "kind": "vector", "elementCount": 4, "elementType": { "kind": "scalar", @@ -66,7 +82,8 @@ standard output = { }, { "name": "IntTexture", - "binding": {"kind": "shaderResource", "index": 4}, + "binding": {"kind": "shaderResource", "index": 5}, + "format": "r32i", "type": { "kind": "resource", "baseShape": "texture2D", @@ -78,7 +95,8 @@ standard output = { }, { "name": "IntTwoTexture", - "binding": {"kind": "shaderResource", "index": 5}, + "binding": {"kind": "shaderResource", "index": 6}, + "format": "rg32i", "type": { "kind": "resource", "baseShape": "texture2D", @@ -94,7 +112,8 @@ standard output = { }, { "name": "IntFourTexture", - "binding": {"kind": "shaderResource", "index": 6}, + "binding": {"kind": "shaderResource", "index": 7}, + "format": "rgba32i", "type": { "kind": "resource", "baseShape": "texture2D", @@ -110,7 +129,8 @@ standard output = { }, { "name": "UintTexture", - "binding": {"kind": "shaderResource", "index": 7}, + "binding": {"kind": "shaderResource", "index": 8}, + "format": "r32ui", "type": { "kind": "resource", "baseShape": "texture2D", @@ -122,7 +142,8 @@ standard output = { }, { "name": "UintTwoTexture", - "binding": {"kind": "shaderResource", "index": 8}, + "binding": {"kind": "shaderResource", "index": 9}, + "format": "rg32ui", "type": { "kind": "resource", "baseShape": "texture2D", @@ -138,7 +159,25 @@ standard output = { }, { "name": "UintFourTexture", - "binding": {"kind": "shaderResource", "index": 9}, + "binding": {"kind": "shaderResource", "index": 10}, + "format": "rgba32ui", + "type": { + "kind": "resource", + "baseShape": "texture2D", + "resultType": { + "kind": "vector", + "elementCount": 4, + "elementType": { + "kind": "scalar", + "scalarType": "uint32" + } + } + } + }, + { + "name": "R11G11B10Texture", + "binding": {"kind": "shaderResource", "index": 11}, + "format": "r11f_g11f_b10f", "type": { "kind": "resource", "baseShape": "texture2D", @@ -147,6 +186,23 @@ standard output = { "elementCount": 4, "elementType": { "kind": "scalar", + "scalarType": "float32" + } + } + } + }, + { + "name": "RGB10_A2_UI_Texture", + "binding": {"kind": "shaderResource", "index": 12}, + "format": "rgb10_a2ui", + "type": { + "kind": "resource", + "baseShape": "texture2D", + "resultType": { + "kind": "vector", + "elementCount": 3, + "elementType": { + "kind": "scalar", "scalarType": "uint32" } } @@ -186,32 +242,52 @@ standard output = { "binding": {"kind": "shaderResource", "index": 2, "used": 0} }, { - "name": "FloatFourTexture", + "name": "FloatThreeTexture", "binding": {"kind": "shaderResource", "index": 3, "used": 0} }, { - "name": "IntTexture", + "name": "FloatFourTexture", "binding": {"kind": "shaderResource", "index": 4, "used": 0} }, { + "name": "IntTexture", + "binding": {"kind": "shaderResource", "index": 5, "used": 0}, + "format": "r32i" + }, + { "name": "IntTwoTexture", - "binding": {"kind": "shaderResource", "index": 5, "used": 0} + "binding": {"kind": "shaderResource", "index": 6, "used": 0}, + "format": "rg32i" }, { "name": "IntFourTexture", - "binding": {"kind": "shaderResource", "index": 6, "used": 0} + "binding": {"kind": "shaderResource", "index": 7, "used": 0}, + "format": "rgba32i" }, { "name": "UintTexture", - "binding": {"kind": "shaderResource", "index": 7, "used": 0} + "binding": {"kind": "shaderResource", "index": 8, "used": 0}, + "format": "r32ui" }, { "name": "UintTwoTexture", - "binding": {"kind": "shaderResource", "index": 8, "used": 0} + "binding": {"kind": "shaderResource", "index": 9, "used": 0}, + "format": "rg32ui" }, { "name": "UintFourTexture", - "binding": {"kind": "shaderResource", "index": 9, "used": 0} + "binding": {"kind": "shaderResource", "index": 10, "used": 0}, + "format": "rgba32ui" + }, + { + "name": "R11G11B10Texture", + "binding": {"kind": "shaderResource", "index": 11, "used": 0}, + "format": "r11f_g11f_b10f" + }, + { + "name": "RGB10_A2_UI_Texture", + "binding": {"kind": "shaderResource", "index": 12, "used": 0}, + "format": "rgb10_a2ui" } ] } diff --git a/tests/reflection/used-parameters.slang.expected b/tests/reflection/used-parameters.slang.expected index 0a4fba104..59b1ae14d 100644 --- a/tests/reflection/used-parameters.slang.expected +++ b/tests/reflection/used-parameters.slang.expected @@ -137,6 +137,7 @@ standard output = { { "name": "UsedBuffer", "binding": {"kind": "shaderResource", "index": 2}, + "format": "r32ui", "type": { "kind": "resource", "baseShape": "textureBuffer" @@ -145,6 +146,7 @@ standard output = { { "name": "UnusedBuffer", "binding": {"kind": "shaderResource", "index": 3}, + "format": "r32ui", "type": { "kind": "resource", "baseShape": "textureBuffer" @@ -211,6 +213,7 @@ standard output = { { "name": "UsedRWBuffer", "binding": {"kind": "unorderedAccess", "index": 2}, + "format": "r32ui", "type": { "kind": "resource", "baseShape": "textureBuffer", @@ -220,6 +223,7 @@ standard output = { { "name": "UnusedRWBuffer", "binding": {"kind": "unorderedAccess", "index": 3}, + "format": "r32ui", "type": { "kind": "resource", "baseShape": "textureBuffer", @@ -321,11 +325,13 @@ standard output = { }, { "name": "UsedBuffer", - "binding": {"kind": "shaderResource", "index": 2, "used": 1} + "binding": {"kind": "shaderResource", "index": 2, "used": 1}, + "format": "r32ui" }, { "name": "UnusedBuffer", - "binding": {"kind": "shaderResource", "index": 3, "used": 0} + "binding": {"kind": "shaderResource", "index": 3, "used": 0}, + "format": "r32ui" }, { "name": "UsedStructuredBuffer", @@ -345,11 +351,13 @@ standard output = { }, { "name": "UsedRWBuffer", - "binding": {"kind": "unorderedAccess", "index": 2, "used": 1} + "binding": {"kind": "unorderedAccess", "index": 2, "used": 1}, + "format": "r32ui" }, { "name": "UnusedRWBuffer", - "binding": {"kind": "unorderedAccess", "index": 3, "used": 0} + "binding": {"kind": "unorderedAccess", "index": 3, "used": 0}, + "format": "r32ui" }, { "name": "UsedRWStructuredBuffer", |
