From 36341ac3f1d9d65f6a7bd40b8265e69a7a3489ac Mon Sep 17 00:00:00 2001 From: Devon7925 Date: Sun, 1 Dec 2024 14:31:42 -0700 Subject: [PATCH 1/5] Add missing float image format inference --- source/slang/slang-check-decl.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/source/slang/slang-check-decl.cpp b/source/slang/slang-check-decl.cpp index e4206827f8..3a62dc1927 100644 --- a/source/slang/slang-check-decl.cpp +++ b/source/slang/slang-check-decl.cpp @@ -1668,6 +1668,20 @@ ImageFormat inferImageFormatFromTextureType( break; } break; + case BaseType::Float: + switch (vectorWidth) + { + case 1: + format = ImageFormat::r32f; + break; + case 2: + format = ImageFormat::rg32f; + break; + case 4: + format = ImageFormat::rgba32f; + break; + } + break; } } } From 76cfd1787fc3c214ddc4ef7acedfb795fc695917 Mon Sep 17 00:00:00 2001 From: Devon7925 Date: Tue, 3 Dec 2024 17:30:39 -0700 Subject: [PATCH 2/5] Drop float4 inference and adjust test --- source/slang/slang-check-decl.cpp | 3 --- tests/language-feature/spirv-asm/imageoperands-warning.slang | 4 ++-- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/source/slang/slang-check-decl.cpp b/source/slang/slang-check-decl.cpp index 3a62dc1927..b5ea6b3e6d 100644 --- a/source/slang/slang-check-decl.cpp +++ b/source/slang/slang-check-decl.cpp @@ -1677,9 +1677,6 @@ ImageFormat inferImageFormatFromTextureType( case 2: format = ImageFormat::rg32f; break; - case 4: - format = ImageFormat::rgba32f; - break; } break; } diff --git a/tests/language-feature/spirv-asm/imageoperands-warning.slang b/tests/language-feature/spirv-asm/imageoperands-warning.slang index fd22e6ae48..cbde4144fa 100644 --- a/tests/language-feature/spirv-asm/imageoperands-warning.slang +++ b/tests/language-feature/spirv-asm/imageoperands-warning.slang @@ -10,7 +10,7 @@ RWStructuredBuffer outputBuffer; // CHECK-NEXT: 1 //TEST_INPUT: Texture2D(size=4, content = one):name t2D -Texture2D t2D; +Texture2D t2D; //TEST_INPUT: Sampler:name samplerState SamplerState samplerState; @@ -28,7 +28,7 @@ void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) float r = spirv_asm { // The type of our sampled image - %sampledImageType = OpTypeSampledImage $$Texture2D; + %sampledImageType = OpTypeSampledImage $$Texture2D; // Combine the image with the sampler %sampledImage : %sampledImageType = OpSampledImage $t2D $samplerState; // Perform a sample From d50e9a59e69305f09051af1e94d0b65cf7233a80 Mon Sep 17 00:00:00 2001 From: Devon7925 Date: Tue, 3 Dec 2024 20:00:42 -0700 Subject: [PATCH 3/5] Do wgsl float format fix at emit time --- source/slang/slang-check-decl.cpp | 11 -------- source/slang/slang-emit-wgsl.cpp | 28 ++++++++++++++++++- .../spirv-asm/imageoperands-warning.slang | 4 +-- 3 files changed, 29 insertions(+), 14 deletions(-) diff --git a/source/slang/slang-check-decl.cpp b/source/slang/slang-check-decl.cpp index b5ea6b3e6d..e4206827f8 100644 --- a/source/slang/slang-check-decl.cpp +++ b/source/slang/slang-check-decl.cpp @@ -1668,17 +1668,6 @@ ImageFormat inferImageFormatFromTextureType( break; } break; - case BaseType::Float: - switch (vectorWidth) - { - case 1: - format = ImageFormat::r32f; - break; - case 2: - format = ImageFormat::rg32f; - break; - } - break; } } } diff --git a/source/slang/slang-emit-wgsl.cpp b/source/slang/slang-emit-wgsl.cpp index d49986263f..6408505c40 100644 --- a/source/slang/slang-emit-wgsl.cpp +++ b/source/slang/slang-emit-wgsl.cpp @@ -1,4 +1,5 @@ #include "slang-emit-wgsl.h" +#include "slang-ir-util.h" // A note on row/column "terminology reversal". // @@ -378,7 +379,32 @@ static const char* getWgslImageFormat(IRTextureTypeBase* type) case ImageFormat::rgba32f: return "rgba32float"; case ImageFormat::unknown: - // Unlike SPIR-V, WGSL doesn't have a texel format for "unknown". + // Unlike SPIR-V, WGSL doesn't have a texel format for "unknown" so we try to infer as best as possible. + auto elementType = type->getElementType(); + Int vectorWidth = 1; + if (auto vectorType = as(elementType)) { + if (auto intLitVal = as(vectorType->getElementCount())) { + vectorWidth = intLitVal->getValue(); + } + } + elementType = getVectorElementType((IRType*) elementType); + if (auto basicType = as(elementType)) + { + switch (basicType->getBaseType()) + { + case BaseType::Float: + switch (vectorWidth) + { + case 1: + return "r32float"; + case 2: + return "rg32float"; + case 4: + return "rgba32float"; + } + break; + } + } return "rgba32float"; default: // We may need to print a warning for types WGSL doesn't support diff --git a/tests/language-feature/spirv-asm/imageoperands-warning.slang b/tests/language-feature/spirv-asm/imageoperands-warning.slang index cbde4144fa..fd22e6ae48 100644 --- a/tests/language-feature/spirv-asm/imageoperands-warning.slang +++ b/tests/language-feature/spirv-asm/imageoperands-warning.slang @@ -10,7 +10,7 @@ RWStructuredBuffer outputBuffer; // CHECK-NEXT: 1 //TEST_INPUT: Texture2D(size=4, content = one):name t2D -Texture2D t2D; +Texture2D t2D; //TEST_INPUT: Sampler:name samplerState SamplerState samplerState; @@ -28,7 +28,7 @@ void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) float r = spirv_asm { // The type of our sampled image - %sampledImageType = OpTypeSampledImage $$Texture2D; + %sampledImageType = OpTypeSampledImage $$Texture2D; // Combine the image with the sampler %sampledImage : %sampledImageType = OpSampledImage $t2D $samplerState; // Perform a sample From 08e4d29f0566d61ae0660aed6e80d58d29377fec Mon Sep 17 00:00:00 2001 From: Devon7925 Date: Wed, 4 Dec 2024 12:23:03 -0700 Subject: [PATCH 4/5] improve formatting --- source/slang/slang-emit-wgsl.cpp | 57 +++++++++++++++++--------------- 1 file changed, 31 insertions(+), 26 deletions(-) diff --git a/source/slang/slang-emit-wgsl.cpp b/source/slang/slang-emit-wgsl.cpp index 6408505c40..a0fea38670 100644 --- a/source/slang/slang-emit-wgsl.cpp +++ b/source/slang/slang-emit-wgsl.cpp @@ -344,6 +344,36 @@ static const char* getWgslImageFormat(IRTextureTypeBase* type) // ImageFormat imageFormat = type->hasFormat() ? (ImageFormat)type->getFormat() : ImageFormat::unknown; + + if (imageFormat == ImageFormat::unknown) + { + // WGSL doesn't have a texel format for "unknown" so we try to infer float types that normally just resolve to unknown. + auto elementType = type->getElementType(); + Int vectorWidth = 1; + if (auto vectorType = as(elementType); auto intLitVal = as(vectorType->getElementCount())) + { + vectorWidth = intLitVal->getValue(); + } + elementType = getVectorElementType((IRType*) elementType); + if (auto basicType = as(elementType)) + { + switch (basicType->getBaseType()) + { + case BaseType::Float: + switch (vectorWidth) + { + case 1: + return "r32float"; + case 2: + return "rg32float"; + case 4: + return "rgba32float"; + } + break; + } + } + } + switch (imageFormat) { case ImageFormat::rgba8: @@ -379,32 +409,7 @@ static const char* getWgslImageFormat(IRTextureTypeBase* type) case ImageFormat::rgba32f: return "rgba32float"; case ImageFormat::unknown: - // Unlike SPIR-V, WGSL doesn't have a texel format for "unknown" so we try to infer as best as possible. - auto elementType = type->getElementType(); - Int vectorWidth = 1; - if (auto vectorType = as(elementType)) { - if (auto intLitVal = as(vectorType->getElementCount())) { - vectorWidth = intLitVal->getValue(); - } - } - elementType = getVectorElementType((IRType*) elementType); - if (auto basicType = as(elementType)) - { - switch (basicType->getBaseType()) - { - case BaseType::Float: - switch (vectorWidth) - { - case 1: - return "r32float"; - case 2: - return "rg32float"; - case 4: - return "rgba32float"; - } - break; - } - } + // Unlike SPIR-V, WGSL doesn't have a texel format for "unknown". return "rgba32float"; default: // We may need to print a warning for types WGSL doesn't support From 7950b5894b4afa406d7508dffa88beba0c25b007 Mon Sep 17 00:00:00 2001 From: slangbot <186143334+slangbot@users.noreply.github.com> Date: Wed, 4 Dec 2024 19:24:45 +0000 Subject: [PATCH 5/5] format code --- source/slang/slang-emit-wgsl.cpp | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/source/slang/slang-emit-wgsl.cpp b/source/slang/slang-emit-wgsl.cpp index a0fea38670..13c14cf340 100644 --- a/source/slang/slang-emit-wgsl.cpp +++ b/source/slang/slang-emit-wgsl.cpp @@ -1,4 +1,5 @@ #include "slang-emit-wgsl.h" + #include "slang-ir-util.h" // A note on row/column "terminology reversal". @@ -344,17 +345,19 @@ static const char* getWgslImageFormat(IRTextureTypeBase* type) // ImageFormat imageFormat = type->hasFormat() ? (ImageFormat)type->getFormat() : ImageFormat::unknown; - + if (imageFormat == ImageFormat::unknown) { - // WGSL doesn't have a texel format for "unknown" so we try to infer float types that normally just resolve to unknown. + // WGSL doesn't have a texel format for "unknown" so we try to infer float types that + // normally just resolve to unknown. auto elementType = type->getElementType(); Int vectorWidth = 1; - if (auto vectorType = as(elementType); auto intLitVal = as(vectorType->getElementCount())) + if (auto vectorType = as(elementType); + auto intLitVal = as(vectorType->getElementCount())) { vectorWidth = intLitVal->getValue(); } - elementType = getVectorElementType((IRType*) elementType); + elementType = getVectorElementType((IRType*)elementType); if (auto basicType = as(elementType)) { switch (basicType->getBaseType()) @@ -373,7 +376,7 @@ static const char* getWgslImageFormat(IRTextureTypeBase* type) } } } - + switch (imageFormat) { case ImageFormat::rgba8: