summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--source/slang/slang-emit-wgsl.cpp61
-rw-r--r--source/slang/slang-emit-wgsl.h4
-rw-r--r--tests/bugs/inf-float-literal.slang4
-rw-r--r--tests/expected-failure-github.txt1
4 files changed, 65 insertions, 5 deletions
diff --git a/source/slang/slang-emit-wgsl.cpp b/source/slang/slang-emit-wgsl.cpp
index 52ff790d7..dea95c6ec 100644
--- a/source/slang/slang-emit-wgsl.cpp
+++ b/source/slang/slang-emit-wgsl.cpp
@@ -26,6 +26,39 @@
namespace Slang
{
+// In WGSL, expression of "1.0/0.0" is not allowed, it will report compile error,
+// so to construct infinity or nan, we have to assign the float literal to a variable
+// and then use it to bypass the compile error.
+static const char* kWGSLBuiltinPreludeGetInfinity = R"(
+fn _slang_getInfinity(positive: bool) -> f32
+{
+ let a = select(f32(-1.0), f32(1.0), positive);
+ let b = f32(0.0);
+ return a / b;
+}
+)";
+
+static const char* kWGSLBuiltinPreludeGetNan = R"(
+fn _slang_getNan() -> f32
+{
+ let a = f32(0.0);
+ let b = f32(0.0);
+ return a / b;
+}
+)";
+
+void WGSLSourceEmitter::ensurePrelude(const char* preludeText)
+{
+ IRStringLit* stringLit;
+ if (!m_builtinPreludes.tryGetValue(preludeText, stringLit))
+ {
+ IRBuilder builder(m_irModule);
+ stringLit = builder.getStringValue(UnownedStringSlice(preludeText));
+ m_builtinPreludes[preludeText] = stringLit;
+ }
+ m_requiredPreludes.add(stringLit);
+}
+
void WGSLSourceEmitter::emitSwitchCaseSelectorsImpl(
const SwitchRegion::Case* const currentCase,
const bool isDefault)
@@ -878,8 +911,32 @@ void WGSLSourceEmitter::emitSimpleValueImpl(IRInst* inst)
case BaseType::Float:
{
- m_writer->emit(litInst->value.floatVal);
- m_writer->emit("f");
+ IRConstant::FloatKind kind = litInst->getFloatKind();
+ switch (kind)
+ {
+ case IRConstant::FloatKind::Nan:
+ {
+ ensurePrelude(kWGSLBuiltinPreludeGetNan);
+ m_writer->emit("_slang_getNan()");
+ break;
+ }
+ case IRConstant::FloatKind::PositiveInfinity:
+ {
+ ensurePrelude(kWGSLBuiltinPreludeGetInfinity);
+ m_writer->emit("_slang_getInfinity(true)");
+ break;
+ }
+ case IRConstant::FloatKind::NegativeInfinity:
+ {
+ ensurePrelude(kWGSLBuiltinPreludeGetInfinity);
+ m_writer->emit("_slang_getInfinity(false)");
+ break;
+ }
+ default:
+ m_writer->emit(litInst->value.floatVal);
+ m_writer->emit("f");
+ break;
+ }
}
break;
diff --git a/source/slang/slang-emit-wgsl.h b/source/slang/slang-emit-wgsl.h
index 1a8ec2fd5..f178d8f66 100644
--- a/source/slang/slang-emit-wgsl.h
+++ b/source/slang/slang-emit-wgsl.h
@@ -51,6 +51,10 @@ public:
void emit(const AddressSpace addressSpace);
virtual bool shouldFoldInstIntoUseSites(IRInst* inst) SLANG_OVERRIDE;
+ Dictionary<const char*, IRStringLit*> m_builtinPreludes;
+
+protected:
+ void ensurePrelude(const char* preludeText);
private:
// Emit the matrix type with 'rowCountWGSL' WGSL-rows and 'colCountWGSL' WGSL-columns
diff --git a/tests/bugs/inf-float-literal.slang b/tests/bugs/inf-float-literal.slang
index b523329df..3c919d0ab 100644
--- a/tests/bugs/inf-float-literal.slang
+++ b/tests/bugs/inf-float-literal.slang
@@ -1,7 +1,7 @@
//TEST(compute):COMPARE_COMPUTE_EX:-slang -compute -shaderobj
//TEST(compute,vulkan):COMPARE_COMPUTE_EX:-vk -slang -compute -shaderobj
//TEST(compute):COMPARE_COMPUTE_EX:-cpu -slang -compute -shaderobj
-//DISABLE_TEST(compute):COMPARE_COMPUTE_EX:-wgpu
+//TEST(compute):COMPARE_COMPUTE_EX:-wgpu -slang -compute -shaderobj
//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):out,name outputBuffer
@@ -24,4 +24,4 @@ void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID)
}
outputBuffer[idx] = a;
-} \ No newline at end of file
+}
diff --git a/tests/expected-failure-github.txt b/tests/expected-failure-github.txt
index 9fe6d72f7..150e36ad1 100644
--- a/tests/expected-failure-github.txt
+++ b/tests/expected-failure-github.txt
@@ -32,7 +32,6 @@ tests/bugs/gh-3980.slang.7 syn (wgpu)
tests/bugs/gh-471.slang.1 syn (wgpu)
tests/bugs/gh-518.slang.2 syn (wgpu)
tests/bugs/gh-566.slang.1 syn (wgpu)
-tests/bugs/inf-float-literal.slang.3 syn (wgpu)
tests/bugs/mutating/resource-specialization-inout.slang.1 syn (wgpu)
tests/bugs/nested-switch.slang.3 syn (wgpu)
tests/bugs/obfuscate-specialization-naming.slang.2 syn (wgpu)