Skip to content

Commit

Permalink
Properly cast argument types when adding abs to shader functions
Browse files Browse the repository at this point in the history
  • Loading branch information
dpw13 committed Feb 23, 2024
1 parent 861c36e commit 8500248
Showing 1 changed file with 5 additions and 23 deletions.
28 changes: 5 additions & 23 deletions vendor/hlslparser/src/GLSLGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -971,50 +971,32 @@ void GLSLGenerator::OutputExpression(HLSLExpression* expression, const HLSLType*
}
else if (String_Equal(functionName, "rsqrt"))
{
HLSLExpression* argument[1];
if (GetFunctionArguments(functionCall, argument, 1) != 1)
{
Error("rsqrt expects 1 argument");
return;
}
/* The documentation says that these functions return NaN for negative
* arguments. However, testing with DX9 shader model 3 shows that they
* most definitely do take the absolute value of the argument and do
* NOT return NaN.
* See https://github.com/projectM-visualizer/projectm/issues/724
*/
m_writer.Write("inversesqrt(abs(");
OutputExpression(argument[0]);
OutputExpressionList(functionCall->argument, functionCall->function->argument);
m_writer.Write("))");
handled = true;
}
else if (String_Equal(functionName, "sqrt") ||
String_Equal(functionName, "log") ||
String_Equal(functionName, "log2"))
{
HLSLExpression* argument[1];
if (GetFunctionArguments(functionCall, argument, 1) != 1)
{
Error("%s expects 1 argument", functionName);
return;
}
/* See rsqrt above */
m_writer.Write("%s(abs(", functionName);
OutputExpression(argument[0]);
OutputExpressionList(functionCall->argument, functionCall->function->argument);
m_writer.Write("))");
handled = true;
}
else if (String_Equal(functionName, "log10"))
{
HLSLExpression* argument[1];
if (GetFunctionArguments(functionCall, argument, 1) != 1)
{
Error("%s expects 1 argument", functionName);
return;
}
/* See rsqrt above regarding abs(). */
m_writer.Write("(log(abs(");
OutputExpression(argument[0]);
OutputExpressionList(functionCall->argument, functionCall->function->argument);
m_writer.Write("))/log(10.0))");
handled = true;
}
Expand All @@ -1031,9 +1013,9 @@ void GLSLGenerator::OutputExpression(HLSLExpression* expression, const HLSLType*
* the abs() call for compatibility across drivers.
*/
m_writer.Write("pow(abs(");
OutputExpression(argument[0]);
OutputExpression(argument[0], &functionCall->function->returnType);
m_writer.Write("),");
OutputExpression(argument[1]);
OutputExpression(argument[1], &functionCall->function->returnType);
m_writer.Write(")");
handled = true;
}
Expand Down

0 comments on commit 8500248

Please sign in to comment.