Skip to content

Commit 968d7b4

Browse files
authored
Merge pull request shader-slang#793 from csyonghe/yong-fix
Fix handling of GLSL sign function
2 parents 935b629 + a426159 commit 968d7b4

File tree

4 files changed

+55
-2
lines changed

4 files changed

+55
-2
lines changed

source/slang/emit.cpp

+19-2
Original file line numberDiff line numberDiff line change
@@ -3488,7 +3488,23 @@ struct EmitVisitor
34883488
emit(".");
34893489
operandIndex++;
34903490
}
3491-
3491+
// fixing issue #602 for GLSL sign function: https://github.com/shader-slang/slang/issues/602
3492+
bool glslSignFix = ctx->shared->target == CodeGenTarget::GLSL && name == "sign";
3493+
if (glslSignFix)
3494+
{
3495+
if (auto vectorType = as<IRVectorType>(inst->getDataType()))
3496+
{
3497+
emit("ivec");
3498+
emit(as<IRConstant>(vectorType->getElementCount())->value.intVal);
3499+
emit("(");
3500+
}
3501+
else if (auto scalarType = as<IRBasicType>(inst->getDataType()))
3502+
{
3503+
emit("int(");
3504+
}
3505+
else
3506+
glslSignFix = false;
3507+
}
34923508
emit(name);
34933509
emit("(");
34943510
bool first = true;
@@ -3499,7 +3515,8 @@ struct EmitVisitor
34993515
first = false;
35003516
}
35013517
emit(")");
3502-
3518+
if (glslSignFix)
3519+
emit(")");
35033520
maybeCloseParens(needClose);
35043521
}
35053522

tests/cross-compile/sign.slang

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// sign.slang
2+
3+
//TEST:CROSS_COMPILE:-target spirv-assembly -entry main -stage fragment
4+
//TEST:CROSS_COMPILE:-target dxil-assembly -entry main -stage fragment -profile sm_6_0
5+
6+
// Test cross compilation of the sign function
7+
8+
float4 main() : SV_Target
9+
{
10+
float4 s = sign(float4(1.5, 1.0, -1.5, -1.0));
11+
return s;
12+
}
13+

tests/cross-compile/sign.slang.glsl

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//TEST_IGNORE_FILE:
2+
#version 450
3+
layout(row_major) uniform;
4+
layout(row_major) buffer;
5+
6+
#line 8 0
7+
layout(location = 0)
8+
out vec4 _S1;
9+
10+
11+
#line 8
12+
void main()
13+
{
14+
ivec4 _S2 = ivec4(sign(vec4(1.50000000000000000000, 1.00000000000000000000, -1.50000000000000000000, -1.00000000000000000000)));
15+
_S1 = vec4(_S2);
16+
return;
17+
}

tests/cross-compile/sign.slang.hlsl

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//TEST_IGNORE_FILE:
2+
float4 main() : SV_Target
3+
{
4+
float4 s = sign(float4(1.5, 1.0, -1.5, -1.0));
5+
return s;
6+
}

0 commit comments

Comments
 (0)