From a431d4f6da9e463c349c2e3819e87a83c8f2e043 Mon Sep 17 00:00:00 2001 From: Tim Foley Date: Wed, 18 Sep 2019 13:20:22 -0700 Subject: Clean up some behavior of operator% (#1060) Work on #1059 The `%` operator in the Slang implementation had several issues, and this change tries to address some of them: * Renamed most occurences of "mod" describing this operator to be "rem" for "remainder" to better match its semantics in HLSL * Split the operator into distinct integer and floating-point variants (`IRem` and `FRem`) to simplify having different codegen for the two * Added floating-point variants of `operator%` and `operator%=` to the stdlib. * Added custom C++ codegen for `kIROp_FRem` such that it maps to the standard C/C++ `remainder()` function * Added custom GLSL codegen so that `kIROp_FRem` maps to the GLSL `mod()` function (which isn't correct...) * Added a test case to confirm that D3D11, D3D12, and CPU targets all agree on the definition of floating-point `%` * Fixed `render-test-tool` to allow a negative integer in a `data=...` specification. This didn't end up being used in the final test, but still seems like a good fix. * Added a customized baseline for the Vulkan flavor of that test to confirm that we are *not* compiling correctly to SPIR-V just yet Addressing the correctness of the output for GLSL/SPIR-V will have to come as a later change given that the operation we want is not exposed directly by unextended GLSL. --- source/slang/slang-emit-glsl.cpp | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) (limited to 'source/slang/slang-emit-glsl.cpp') diff --git a/source/slang/slang-emit-glsl.cpp b/source/slang/slang-emit-glsl.cpp index d3c852fd6..a1c7b9170 100644 --- a/source/slang/slang-emit-glsl.cpp +++ b/source/slang/slang-emit-glsl.cpp @@ -1171,7 +1171,36 @@ bool GLSLSourceEmitter::tryEmitInstExprImpl(IRInst* inst, const EmitOpInfo& inOu // Use the default break; } - + case kIROp_FRem: + { + IRInst* left = inst->getOperand(0); + IRInst* right = inst->getOperand(1); + + // Handle as a function call + auto prec = getInfo(EmitOp::Postfix); + + EmitOpInfo outerPrec = inOuterPrec; + bool needClose = maybeEmitParens(outerPrec, outerPrec); + + // TODO: the GLSL `mod` function amounts to a floating-point + // modulus rather than a floating-point remainder. We need + // to fix this to emit the right SPIR-V opcode, but there is + // no built-in GLSL function that maps to the opcode we want. + // + m_writer->emit("mod("); + emitOperand(left, getInfo(EmitOp::General)); + m_writer->emit(","); + emitOperand(right, getInfo(EmitOp::General)); + m_writer->emit(")"); + + maybeCloseParens(needClose); + + return true; + } + // TODO: We should also special-case `kIROp_IRem` here, + // so that we emit a remainder instead of a modulus. As for + // `FRem` there is no direct GLSL translation, so we will + // leave things with the default behavior for now. default: break; } -- cgit v1.2.3