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

View File

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

View File

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