SpecOp bool->uint/int and uint<->int conversion
Bool -> uint/int with OpSpecConstantOp OpSelect instruction.
uint <-> int conversion with OpSpecConstantOp OpIAdd instruction.
Note, implicit conversion: `const uint = an_int_spec_constant` is not
supported. Explicit type casting is required: `const uint =
uint(an_int_spec_constant)`
diff --git a/SPIRV/GlslangToSpv.cpp b/SPIRV/GlslangToSpv.cpp
index 9249574..0240d8d 100755
--- a/SPIRV/GlslangToSpv.cpp
+++ b/SPIRV/GlslangToSpv.cpp
@@ -1960,7 +1960,6 @@
glslang::TIntermTyped* specNode = arraySizes.getDimNode(dim);
if (specNode != nullptr) {
builder.clearAccessChain();
- // SpecConstantOpModeGuard set_to_spec_const_mode(&builder);
specNode->traverse(this);
return accessChainLoad(specNode->getAsTyped()->getType());
}
@@ -3307,7 +3306,27 @@
break;
case glslang::EOpConvUintToInt:
+ if (builder.isInSpecConstCodeGenMode()) {
+ // Build zero scalar or vector for OpIAdd to do the conversion when
+ // generating for OpSpecConstantOp instruction.
+ zero = builder.makeIntConstant(0);
+ zero = makeSmearedConstant(zero, vectorSize);
+ }
+ // Don't 'break' here as this case should be grouped together with
+ // EOpConvIntToUint when generating normal run-time conversion
+ // instruction.
case glslang::EOpConvIntToUint:
+ if (builder.isInSpecConstCodeGenMode()) {
+ // Build zero scalar or vector for OpIAdd.
+ if (zero == 0) {
+ zero = builder.makeUintConstant(0);
+ zero = makeSmearedConstant(zero, vectorSize);
+ }
+ // Use OpIAdd, instead of OpBitcast to do the conversion when
+ // generating for OpSpecConstantOp instruction.
+ return builder.createBinOp(spv::OpIAdd, destType, operand, zero);
+ }
+ // For normal run-time conversion instruction, use OpBitcast.
convOp = spv::OpBitcast;
break;
diff --git a/SPIRV/SpvBuilder.cpp b/SPIRV/SpvBuilder.cpp
index f69e7fe..df87e9c 100644
--- a/SPIRV/SpvBuilder.cpp
+++ b/SPIRV/SpvBuilder.cpp
@@ -1212,6 +1212,14 @@
Id Builder::createTriOp(Op opCode, Id typeId, Id op1, Id op2, Id op3)
{
+ // Generate code for spec constants if in spec constant operation
+ // generation mode.
+ if (generatingOpCodeForSpecConst) {
+ std::vector<Id> operands(3);
+ operands[0] = op1; operands[1] = op2; operands[2] = op3;
+ return createSpecConstantOp(
+ opCode, typeId, operands, std::vector<Id>());
+ }
Instruction* op = new Instruction(getUniqueId(), typeId, opCode);
op->addIdOperand(op1);
op->addIdOperand(op2);