Skip to content

Commit e7e77ba

Browse files
committed
hlslparser: Add missing "ldexp" HLSL intrinsic.
Had to simulate it with the "x * exp2(exp)" expression in GLSL, as GLSL only supports integer types as exponents in its native ldexp implementation, while HLSL solely uses floats.
1 parent e90dc28 commit e7e77ba

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

Diff for: vendor/hlslparser/src/GLSLGenerator.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -1019,6 +1019,23 @@ void GLSLGenerator::OutputExpression(HLSLExpression* expression, const HLSLType*
10191019
m_writer.Write(")");
10201020
handled = true;
10211021
}
1022+
else if (String_Equal(functionName, "ldexp"))
1023+
{
1024+
/* HLSL has the second argument as float, while GLSL only supports ints, so we simulate the HLSL behaviour
1025+
* by using the equivalent "x * exp2(exp)" expression. */
1026+
HLSLExpression* argument[2];
1027+
if (GetFunctionArguments(functionCall, argument, 2) != 2)
1028+
{
1029+
Error("%s expects 2 arguments", functionName);
1030+
return;
1031+
}
1032+
m_writer.Write("(");
1033+
OutputExpression(argument[0], &functionCall->function->returnType);
1034+
m_writer.Write("*exp2(");
1035+
OutputExpression(argument[1], &functionCall->function->returnType);
1036+
m_writer.Write("))");
1037+
handled = true;
1038+
}
10221039

10231040
if (!handled)
10241041
{

Diff for: vendor/hlslparser/src/HLSLParser.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,23 @@ const Intrinsic _intrinsic[] =
620620
INTRINSIC_FLOAT2_FUNCTION( "step" ),
621621
INTRINSIC_FLOAT2_FUNCTION( "reflect" ),
622622

623+
Intrinsic("ldexp", HLSLBaseType_Float, HLSLBaseType_Float, HLSLBaseType_Float),
624+
Intrinsic("ldexp", HLSLBaseType_Float2, HLSLBaseType_Float2, HLSLBaseType_Float2),
625+
Intrinsic("ldexp", HLSLBaseType_Float3, HLSLBaseType_Float3, HLSLBaseType_Float3),
626+
Intrinsic("ldexp", HLSLBaseType_Float4, HLSLBaseType_Float4, HLSLBaseType_Float4),
627+
628+
Intrinsic("ldexp", HLSLBaseType_Float2x2, HLSLBaseType_Float2x2, HLSLBaseType_Float2x2),
629+
Intrinsic("ldexp", HLSLBaseType_Float2x3, HLSLBaseType_Float2x3, HLSLBaseType_Float2x3),
630+
Intrinsic("ldexp", HLSLBaseType_Float2x4, HLSLBaseType_Float2x4, HLSLBaseType_Float2x4),
631+
632+
Intrinsic("ldexp", HLSLBaseType_Float3x2, HLSLBaseType_Float3x2, HLSLBaseType_Float3x2),
633+
Intrinsic("ldexp", HLSLBaseType_Float3x3, HLSLBaseType_Float3x3, HLSLBaseType_Float3x3),
634+
Intrinsic("ldexp", HLSLBaseType_Float3x4, HLSLBaseType_Float3x4, HLSLBaseType_Float3x4),
635+
636+
Intrinsic("ldexp", HLSLBaseType_Float4x2, HLSLBaseType_Float4x2, HLSLBaseType_Float4x2),
637+
Intrinsic("ldexp", HLSLBaseType_Float4x3, HLSLBaseType_Float4x3, HLSLBaseType_Float4x3),
638+
Intrinsic("ldexp", HLSLBaseType_Float4x4, HLSLBaseType_Float4x4, HLSLBaseType_Float4x4),
639+
623640
Intrinsic("refract", HLSLBaseType_Float2, HLSLBaseType_Float2, HLSLBaseType_Float2, HLSLBaseType_Float),
624641
Intrinsic("refract", HLSLBaseType_Float3, HLSLBaseType_Float3, HLSLBaseType_Float3, HLSLBaseType_Float),
625642
Intrinsic("refract", HLSLBaseType_Float4, HLSLBaseType_Float4, HLSLBaseType_Float4, HLSLBaseType_Float),

0 commit comments

Comments
 (0)