From a4261598be7e3229a6280002eaced96d8cb3ced6 Mon Sep 17 00:00:00 2001 From: Yong He Date: Wed, 23 Jan 2019 19:54:21 -0800 Subject: Fixing GLSL sign function. fixes #602 --- source/slang/emit.cpp | 21 +++++++++++++++++++-- tests/cross-compile/sign.slang | 13 +++++++++++++ tests/cross-compile/sign.slang.glsl | 17 +++++++++++++++++ tests/cross-compile/sign.slang.hlsl | 6 ++++++ 4 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 tests/cross-compile/sign.slang create mode 100644 tests/cross-compile/sign.slang.glsl create mode 100644 tests/cross-compile/sign.slang.hlsl diff --git a/source/slang/emit.cpp b/source/slang/emit.cpp index 0aebf6153..914aea7fd 100644 --- a/source/slang/emit.cpp +++ b/source/slang/emit.cpp @@ -3488,7 +3488,23 @@ struct EmitVisitor emit("."); operandIndex++; } - + // fixing issue #602 for GLSL sign function: https://github.com/shader-slang/slang/issues/602 + bool glslSignFix = ctx->shared->target == CodeGenTarget::GLSL && name == "sign"; + if (glslSignFix) + { + if (auto vectorType = as(inst->getDataType())) + { + emit("ivec"); + emit(as(vectorType->getElementCount())->value.intVal); + emit("("); + } + else if (auto scalarType = as(inst->getDataType())) + { + emit("int("); + } + else + glslSignFix = false; + } emit(name); emit("("); bool first = true; @@ -3499,7 +3515,8 @@ struct EmitVisitor first = false; } emit(")"); - + if (glslSignFix) + emit(")"); maybeCloseParens(needClose); } diff --git a/tests/cross-compile/sign.slang b/tests/cross-compile/sign.slang new file mode 100644 index 000000000..17a51d93d --- /dev/null +++ b/tests/cross-compile/sign.slang @@ -0,0 +1,13 @@ +// sign.slang + +//TEST:CROSS_COMPILE:-target spirv-assembly -entry main -stage fragment +//TEST:CROSS_COMPILE:-target dxil-assembly -entry main -stage fragment -profile sm_6_0 + +// Test cross compilation of the sign function + +float4 main() : SV_Target +{ + float4 s = sign(float4(1.5, 1.0, -1.5, -1.0)); + return s; +} + diff --git a/tests/cross-compile/sign.slang.glsl b/tests/cross-compile/sign.slang.glsl new file mode 100644 index 000000000..7a3a37c51 --- /dev/null +++ b/tests/cross-compile/sign.slang.glsl @@ -0,0 +1,17 @@ +//TEST_IGNORE_FILE: +#version 450 +layout(row_major) uniform; +layout(row_major) buffer; + +#line 8 0 +layout(location = 0) +out vec4 _S1; + + +#line 8 +void main() +{ + ivec4 _S2 = ivec4(sign(vec4(1.50000000000000000000, 1.00000000000000000000, -1.50000000000000000000, -1.00000000000000000000))); + _S1 = vec4(_S2); + return; +} \ No newline at end of file diff --git a/tests/cross-compile/sign.slang.hlsl b/tests/cross-compile/sign.slang.hlsl new file mode 100644 index 000000000..d7016dde7 --- /dev/null +++ b/tests/cross-compile/sign.slang.hlsl @@ -0,0 +1,6 @@ +//TEST_IGNORE_FILE: +float4 main() : SV_Target +{ + float4 s = sign(float4(1.5, 1.0, -1.5, -1.0)); + return s; +} \ No newline at end of file -- cgit v1.2.3