diff options
| author | Tim Foley <tfoleyNV@users.noreply.github.com> | 2021-03-26 10:53:58 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-03-26 10:53:58 -0700 |
| commit | 129faf8c4af4a57b7f1c71749f45b31aebfab038 (patch) | |
| tree | 8ce3f618abf76d9144e28ac5860e425866cb866a | |
| parent | abb020b15c4f1057657e5f8f63c02b15615bf6ec (diff) | |
Append proper suffixes to 16-bit literals for GLSL (#1767)
* Append proper suffixes to 16-bit literals for GLSL
The GLSL output path wasn't putting suffixes on literals of 16-bit types, and that was leading to compilation errors in downstream `glslang`. This change adds the suffixes defined by `GL_EXT_shader_explicit_arithmetic_types`.
This change also wraps up 8-bit literals so that they are emitted as, e.g., `int8_t(1)` instead of just `1`, to make sure we don't have implicit conversions in the output GLSL that weren't implicit in the Slang IR. We similarly wrap floating-point special values like infinities in their desired types when the type is `float` (e.g., `double(1.0 / 0.0)` for a double-precision infinity).
Note: Standad IEEE 754 half-precision doesn't provide an encoding for infinite or not-a-number values, so it might be considered an error if we emit `half(1.0 / 0.0)` but there really isn't a significantly better alternative for us to emit.
* fixup
| -rw-r--r-- | source/slang/slang-emit-glsl.cpp | 60 | ||||
| -rw-r--r-- | source/slang/slang-emit-glsl.h | 2 | ||||
| -rw-r--r-- | source/slang/slang-ir.cpp | 4 | ||||
| -rw-r--r-- | tests/ir/string-literal.slang.expected | 4 | ||||
| -rw-r--r-- | tests/language-feature/initializer-lists/default-init-16bit-types.slang | 35 | ||||
| -rw-r--r-- | tests/language-feature/initializer-lists/default-init-16bit-types.slang.expected.txt | 4 |
6 files changed, 103 insertions, 6 deletions
diff --git a/source/slang/slang-emit-glsl.cpp b/source/slang/slang-emit-glsl.cpp index a1142749b..5b29fb8fa 100644 --- a/source/slang/slang-emit-glsl.cpp +++ b/source/slang/slang-emit-glsl.cpp @@ -737,6 +737,17 @@ void GLSLSourceEmitter::emitLoopControlDecorationImpl(IRLoopControlDecoration* d } } +void GLSLSourceEmitter::_emitSpecialFloatImpl(IRType* type, const char* valueExpr) +{ + if( type->getOp() != kIROp_FloatType ) + { + emitType(type); + } + m_writer->emit("("); + m_writer->emit(valueExpr); + m_writer->emit(")"); +} + void GLSLSourceEmitter::emitSimpleValueImpl(IRInst* inst) { switch (inst->getOp()) @@ -753,14 +764,38 @@ void GLSLSourceEmitter::emitSimpleValueImpl(IRInst* inst) default: case BaseType::Int8: + { + emitType(type); + m_writer->emit("("); + m_writer->emit(litInst->value.intVal); + m_writer->emit(")"); + return; + } case BaseType::Int16: + { + m_writer->emit(litInst->value.intVal); + m_writer->emit("S"); + return; + } case BaseType::Int: { m_writer->emit(litInst->value.intVal); return; } case BaseType::UInt8: + { + emitType(type); + m_writer->emit("("); + m_writer->emit(UInt(litInst->value.intVal)); + m_writer->emit("U)"); + return; + } case BaseType::UInt16: + { + m_writer->emit(UInt(litInst->value.intVal)); + m_writer->emit("US"); + return; + } case BaseType::UInt: { m_writer->emit(UInt(litInst->value.intVal)); @@ -789,26 +824,43 @@ void GLSLSourceEmitter::emitSimpleValueImpl(IRInst* inst) { IRConstant* constantInst = static_cast<IRConstant*>(inst); + auto type = constantInst->getDataType(); IRConstant::FloatKind kind = constantInst->getFloatKind(); switch (kind) { case IRConstant::FloatKind::Nan: { - m_writer->emit("(0.0 / 0.0)"); + _emitSpecialFloatImpl(type, "0.0 / 0.0"); return; } case IRConstant::FloatKind::PositiveInfinity: { - m_writer->emit("(1.0 / 0.0)"); + _emitSpecialFloatImpl(type, "1.0 / 0.0"); return; } case IRConstant::FloatKind::NegativeInfinity: { - m_writer->emit("(-1.0 / 0.0)"); + _emitSpecialFloatImpl(type, "-1.0 / 0.0"); + return; + } + default: + { + m_writer->emit(((IRConstant*) inst)->value.floatVal); + switch( type->getOp() ) + { + case kIROp_HalfType: + m_writer->emit("HF"); + break; + case kIROp_DoubleType: + m_writer->emit("LF"); + break; + default: + break; + } + return; } - default: break; } break; } diff --git a/source/slang/slang-emit-glsl.h b/source/slang/slang-emit-glsl.h index 48ed95a53..e9d45de11 100644 --- a/source/slang/slang-emit-glsl.h +++ b/source/slang/slang-emit-glsl.h @@ -108,6 +108,8 @@ protected: void _requireRayTracing(); + void _emitSpecialFloatImpl(IRType* type, const char* valueExpr); + RefPtr<GLSLExtensionTracker> m_glslExtensionTracker; }; diff --git a/source/slang/slang-ir.cpp b/source/slang/slang-ir.cpp index 945cf488f..a983188b7 100644 --- a/source/slang/slang-ir.cpp +++ b/source/slang/slang-ir.cpp @@ -4945,10 +4945,14 @@ namespace Slang { case kIROp_IntLit: dump(context, irConst->value.intVal); + dump(context, " : "); + dumpType(context, irConst->getFullType()); return; case kIROp_FloatLit: dump(context, irConst->value.floatVal); + dump(context, " : "); + dumpType(context, irConst->getFullType()); return; case kIROp_BoolLit: diff --git a/tests/ir/string-literal.slang.expected b/tests/ir/string-literal.slang.expected index 80d7ce67c..91a6ccec2 100644 --- a/tests/ir/string-literal.slang.expected +++ b/tests/ir/string-literal.slang.expected @@ -1,8 +1,8 @@ result code = 0 standard error = { ### LOWER-TO-IR: -[entryPoint(6, "main")] -[numThreads(1, 1, 1)] +[entryPoint(6 : Int, "main")] +[numThreads(1 : Int, 1 : Int, 1 : Int)] [export("_S3tu04mainp1puV")] [nameHint("main")] func %main : Func(Void, UInt) diff --git a/tests/language-feature/initializer-lists/default-init-16bit-types.slang b/tests/language-feature/initializer-lists/default-init-16bit-types.slang new file mode 100644 index 000000000..5fb578ca4 --- /dev/null +++ b/tests/language-feature/initializer-lists/default-init-16bit-types.slang @@ -0,0 +1,35 @@ +// simple-namespace.slang + +//TEST(compute):COMPARE_COMPUTE:-vk + +// Test that default initialization works with 16-bit types under Vulkan. + +struct S +{ + int a; + int16_t b; + float16_t c; + int d; +} + +int test(int val) +{ + S s = {}; + s.a += val; + s.b += int16_t(val*16); + s.c += float16_t(val*16*16); + s.d += val*16*16*16; + return s.a + s.b + int(s.c) + s.d; +} + +//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):out,name=outputBuffer +RWStructuredBuffer<int> outputBuffer; + +[numthreads(4, 1, 1)] +void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) +{ + uint tid = dispatchThreadID.x; + int inVal = tid; + int outVal = test(inVal); + outputBuffer[tid] = outVal; +} diff --git a/tests/language-feature/initializer-lists/default-init-16bit-types.slang.expected.txt b/tests/language-feature/initializer-lists/default-init-16bit-types.slang.expected.txt new file mode 100644 index 000000000..6dbd5c939 --- /dev/null +++ b/tests/language-feature/initializer-lists/default-init-16bit-types.slang.expected.txt @@ -0,0 +1,4 @@ +0 +1111 +2222 +3333 |
