Use std::variant to represent TSpirvTypeParameter

This PR is try to address the review comment:
https://github.com/KhronosGroup/glslang/pull/3253#discussion_r1254932979.
This commit is contained in:
Rex Xu 2023-09-10 19:27:58 +08:00 committed by arcady-lunarg
parent afe6e781bd
commit 323836e46b
3 changed files with 35 additions and 29 deletions

View File

@ -4492,26 +4492,27 @@ spv::Id TGlslangToSpvTraverser::convertGlslangToSpvType(const glslang::TType& ty
std::vector<spv::IdImmediate> operands; std::vector<spv::IdImmediate> operands;
for (const auto& typeParam : spirvType.typeParams) { for (const auto& typeParam : spirvType.typeParams) {
if (typeParam.constant != nullptr) { if (typeParam.getAsConstant() != nullptr) {
// Constant expression // Constant expression
if (typeParam.constant->isLiteral()) { auto constant = typeParam.getAsConstant();
if (typeParam.constant->getBasicType() == glslang::EbtFloat) { if (constant->isLiteral()) {
float floatValue = static_cast<float>(typeParam.constant->getConstArray()[0].getDConst()); if (constant->getBasicType() == glslang::EbtFloat) {
float floatValue = static_cast<float>(constant->getConstArray()[0].getDConst());
unsigned literal; unsigned literal;
static_assert(sizeof(literal) == sizeof(floatValue), "sizeof(unsigned) != sizeof(float)"); static_assert(sizeof(literal) == sizeof(floatValue), "sizeof(unsigned) != sizeof(float)");
memcpy(&literal, &floatValue, sizeof(literal)); memcpy(&literal, &floatValue, sizeof(literal));
operands.push_back({false, literal}); operands.push_back({false, literal});
} else if (typeParam.constant->getBasicType() == glslang::EbtInt) { } else if (constant->getBasicType() == glslang::EbtInt) {
unsigned literal = typeParam.constant->getConstArray()[0].getIConst(); unsigned literal = constant->getConstArray()[0].getIConst();
operands.push_back({false, literal}); operands.push_back({false, literal});
} else if (typeParam.constant->getBasicType() == glslang::EbtUint) { } else if (constant->getBasicType() == glslang::EbtUint) {
unsigned literal = typeParam.constant->getConstArray()[0].getUConst(); unsigned literal = constant->getConstArray()[0].getUConst();
operands.push_back({false, literal}); operands.push_back({false, literal});
} else if (typeParam.constant->getBasicType() == glslang::EbtBool) { } else if (constant->getBasicType() == glslang::EbtBool) {
unsigned literal = typeParam.constant->getConstArray()[0].getBConst(); unsigned literal = constant->getConstArray()[0].getBConst();
operands.push_back({false, literal}); operands.push_back({false, literal});
} else if (typeParam.constant->getBasicType() == glslang::EbtString) { } else if (constant->getBasicType() == glslang::EbtString) {
auto str = typeParam.constant->getConstArray()[0].getSConst()->c_str(); auto str = constant->getConstArray()[0].getSConst()->c_str();
unsigned literal = 0; unsigned literal = 0;
char* literalPtr = reinterpret_cast<char*>(&literal); char* literalPtr = reinterpret_cast<char*>(&literal);
unsigned charCount = 0; unsigned charCount = 0;
@ -4536,11 +4537,11 @@ spv::Id TGlslangToSpvTraverser::convertGlslangToSpvType(const glslang::TType& ty
} else } else
assert(0); // Unexpected type assert(0); // Unexpected type
} else } else
operands.push_back({true, createSpvConstant(*typeParam.constant)}); operands.push_back({true, createSpvConstant(*constant)});
} else { } else {
// Type specifier // Type specifier
assert(typeParam.type != nullptr); assert(typeParam.getAsType() != nullptr);
operands.push_back({true, convertGlslangToSpvType(*typeParam.type)}); operands.push_back({true, convertGlslangToSpvType(*typeParam.getAsType())});
} }
} }

View File

@ -39,6 +39,7 @@
// GL_EXT_spirv_intrinsics // GL_EXT_spirv_intrinsics
// //
#include "Common.h" #include "Common.h"
#include <variant>
namespace glslang { namespace glslang {
@ -96,23 +97,27 @@ struct TSpirvInstruction {
struct TSpirvTypeParameter { struct TSpirvTypeParameter {
POOL_ALLOCATOR_NEW_DELETE(GetThreadPoolAllocator()) POOL_ALLOCATOR_NEW_DELETE(GetThreadPoolAllocator())
TSpirvTypeParameter(const TIntermConstantUnion* arg) TSpirvTypeParameter(const TIntermConstantUnion* arg) { value = arg; }
{ TSpirvTypeParameter(const TType* arg) { value = arg; }
constant = arg;
type = nullptr;
}
TSpirvTypeParameter(const TType *arg) const TIntermConstantUnion* getAsConstant() const
{ {
constant = nullptr; if (value.index() == 0)
type = arg; return std::get<const TIntermConstantUnion*>(value);
return nullptr;
}
const TType* getAsType() const
{
if (value.index() == 1)
return std::get<const TType*>(value);
return nullptr;
} }
bool operator==(const TSpirvTypeParameter& rhs) const; bool operator==(const TSpirvTypeParameter& rhs) const;
bool operator!=(const TSpirvTypeParameter& rhs) const { return !operator==(rhs); } bool operator!=(const TSpirvTypeParameter& rhs) const { return !operator==(rhs); }
const TIntermConstantUnion* constant; // Constant expression // Parameter value: constant expression or type specifier
const TType* type; // Type specifier std::variant<const TIntermConstantUnion*, const TType*> value;
}; };
typedef TVector<TSpirvTypeParameter> TSpirvTypeParameters; typedef TVector<TSpirvTypeParameter> TSpirvTypeParameters;

View File

@ -45,11 +45,11 @@ namespace glslang {
bool TSpirvTypeParameter::operator==(const TSpirvTypeParameter& rhs) const bool TSpirvTypeParameter::operator==(const TSpirvTypeParameter& rhs) const
{ {
if (constant != nullptr) if (getAsConstant() != nullptr)
return constant->getConstArray() == rhs.constant->getConstArray(); return getAsConstant()->getConstArray() == rhs.getAsConstant()->getConstArray();
assert(type != nullptr); assert(getAsType() != nullptr);
return *type == *rhs.type; return *getAsType() == *rhs.getAsType();
} }
// //