Spirv_intrinsics: Add support of type specifier to spirv_type

Previously, spirv_type doesn't accept type specifier as its parameter.
With this change, we can input non-array type specifier. This is because
some SPIR-V type definition intructions often need to reference other
SPIR-V types as its source operands. We add the support to facilitate
such usage.
This commit is contained in:
Rex Xu 2023-06-24 11:23:45 +08:00 committed by kd-11
parent e0151ab8c7
commit 4218bbe746
10 changed files with 1468 additions and 1359 deletions

View File

@ -4515,50 +4515,56 @@ spv::Id TGlslangToSpvTraverser::convertGlslangToSpvType(const glslang::TType& ty
std::vector<spv::IdImmediate> operands;
for (const auto& typeParam : spirvType.typeParams) {
// Constant expression
if (typeParam.constant->isLiteral()) {
if (typeParam.constant->getBasicType() == glslang::EbtFloat) {
float floatValue = static_cast<float>(typeParam.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();
operands.push_back({false, literal});
} else if (typeParam.constant->getBasicType() == glslang::EbtUint) {
unsigned literal = typeParam.constant->getConstArray()[0].getUConst();
operands.push_back({false, literal});
} else if (typeParam.constant->getBasicType() == glslang::EbtBool) {
unsigned literal = typeParam.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();
unsigned literal = 0;
char* literalPtr = reinterpret_cast<char*>(&literal);
unsigned charCount = 0;
char ch = 0;
do {
ch = *(str++);
*(literalPtr++) = ch;
++charCount;
if (charCount == 4) {
operands.push_back({false, literal});
literalPtr = reinterpret_cast<char*>(&literal);
charCount = 0;
}
} while (ch != 0);
// Partial literal is padded with 0
if (charCount > 0) {
for (; charCount < 4; ++charCount)
*(literalPtr++) = 0;
if (typeParam.constant != nullptr) {
// Constant expression
if (typeParam.constant->isLiteral()) {
if (typeParam.constant->getBasicType() == glslang::EbtFloat) {
float floatValue = static_cast<float>(typeParam.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();
operands.push_back({false, literal});
} else if (typeParam.constant->getBasicType() == glslang::EbtUint) {
unsigned literal = typeParam.constant->getConstArray()[0].getUConst();
operands.push_back({false, literal});
} else if (typeParam.constant->getBasicType() == glslang::EbtBool) {
unsigned literal = typeParam.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();
unsigned literal = 0;
char* literalPtr = reinterpret_cast<char*>(&literal);
unsigned charCount = 0;
char ch = 0;
do {
ch = *(str++);
*(literalPtr++) = ch;
++charCount;
if (charCount == 4) {
operands.push_back({false, literal});
literalPtr = reinterpret_cast<char*>(&literal);
charCount = 0;
}
} while (ch != 0);
// Partial literal is padded with 0
if (charCount > 0) {
for (; charCount < 4; ++charCount)
*(literalPtr++) = 0;
operands.push_back({false, literal});
}
} else
assert(0); // Unexpected type
} else
assert(0); // Unexpected type
} else
operands.push_back({true, createSpvConstant(*typeParam.constant)});
operands.push_back({true, createSpvConstant(*typeParam.constant)});
} else {
// Type specifier
assert(typeParam.type != nullptr);
operands.push_back({true, convertGlslangToSpvType(*typeParam.type)});
}
}
assert(spirvInst.set == ""); // Currently, couldn't be extended instructions.

View File

@ -0,0 +1,35 @@
spv.intrinsicsSpirvTypeWithTypeSpecifier.vert
// Module Version 10000
// Generated by (magic number): 8000b
// Id's are bound by 14
Capability Shader
Capability Int64
Capability PhysicalStorageBufferAddressesEXT
Extension "SPV_EXT_physical_storage_buffer"
Extension "SPV_KHR_physical_storage_buffer"
Extension "SPV_KHR_variable_pointers"
1: ExtInstImport "GLSL.std.450"
MemoryModel PhysicalStorageBuffer64EXT GLSL450
EntryPoint Vertex 4 "main"
Source GLSL 450
SourceExtension "GL_ARB_gpu_shader_int64"
SourceExtension "GL_EXT_buffer_reference"
SourceExtension "GL_EXT_spirv_intrinsics"
Name 4 "main"
Name 8 "value"
2: TypeVoid
3: TypeFunction 2
6: TypeInt 32 0
7: TypePointer Function 6(int)
9: TypeInt 64 0
10: 9(int64_t) Constant 1 0
11: TypePointer PhysicalStorageBufferEXT 6(int)
4(main): 2 Function None 3
5: Label
8(value): 7(ptr) Variable Function
12: 11(ptr) ConvertUToPtr 10
13: 6(int) Load 12 Aligned 32
Store 8(value) 13
Return
FunctionEnd

View File

@ -0,0 +1,25 @@
#version 450 core
#extension GL_ARB_gpu_shader_int64: enable
#extension GL_EXT_buffer_reference: enable
#extension GL_EXT_spirv_intrinsics: enable
#define CapabilityPhysicalStorageBufferAddresses 5347
#define StorageClassPhysicalStorageBuffer 5349
#define OpTypePointer 32
#define OpLoad 61
#define OpConvertUToPtr 120
#define uintStoragePtr spirv_type(extensions = ["SPV_EXT_physical_storage_buffer", "SPV_KHR_variable_pointers"], capabilities = [CapabilityPhysicalStorageBufferAddresses], id = OpTypePointer, StorageClassPhysicalStorageBuffer, uint)
// Just to enable the memory model of physical storage buffer
layout(buffer_reference, std430) buffer Dummy {
uint dummy;
};
spirv_instruction(id = OpLoad) uint loadUint(uintStoragePtr pointer, spirv_literal uint memoryOperands, spirv_literal uint alignment);
spirv_instruction(id = OpConvertUToPtr) uintStoragePtr convertToPtr(uint64_t value);
void main() {
uint value = loadUint(convertToPtr(1), 0x2, 32);
}

View File

@ -98,12 +98,23 @@ struct TSpirvInstruction {
struct TSpirvTypeParameter {
POOL_ALLOCATOR_NEW_DELETE(GetThreadPoolAllocator())
TSpirvTypeParameter(const TIntermConstantUnion* arg) { constant = arg; }
TSpirvTypeParameter(const TIntermConstantUnion* arg)
{
constant = arg;
type = nullptr;
}
bool operator==(const TSpirvTypeParameter& rhs) const { return constant == rhs.constant; }
TSpirvTypeParameter(const TType *arg)
{
constant = nullptr;
type = arg;
}
bool operator==(const TSpirvTypeParameter& rhs) const;
bool operator!=(const TSpirvTypeParameter& rhs) const { return !operator==(rhs); }
const TIntermConstantUnion* constant;
const TIntermConstantUnion* constant; // Constant expression
const TType* type; // Type specifier
};
typedef TVector<TSpirvTypeParameter> TSpirvTypeParameters;

View File

@ -486,6 +486,7 @@ public:
TSpirvRequirement* mergeSpirvRequirements(const TSourceLoc& loc, TSpirvRequirement* spirvReq1,
TSpirvRequirement* spirvReq2);
TSpirvTypeParameters* makeSpirvTypeParameters(const TSourceLoc& loc, const TIntermConstantUnion* constant);
TSpirvTypeParameters* makeSpirvTypeParameters(const TSourceLoc& loc, const TPublicType& type);
TSpirvTypeParameters* mergeSpirvTypeParameters(TSpirvTypeParameters* spirvTypeParams1,
TSpirvTypeParameters* spirvTypeParams2);
TSpirvInstruction* makeSpirvInstruction(const TSourceLoc& loc, const TString& name, const TString& value);

View File

@ -45,6 +45,15 @@
namespace glslang {
bool TSpirvTypeParameter::operator==(const TSpirvTypeParameter& rhs) const
{
if (constant != nullptr)
return constant->getConstArray() == rhs.constant->getConstArray();
assert(type != nullptr);
return *type == *rhs.type;
}
//
// Handle SPIR-V requirements
//
@ -283,14 +292,19 @@ TSpirvTypeParameters* TParseContext::makeSpirvTypeParameters(const TSourceLoc& l
constant->getBasicType() != EbtBool &&
constant->getBasicType() != EbtString)
error(loc, "this type not allowed", constant->getType().getBasicString(), "");
else {
assert(constant);
else
spirvTypeParams->push_back(TSpirvTypeParameter(constant));
}
return spirvTypeParams;
}
TSpirvTypeParameters* TParseContext::makeSpirvTypeParameters(const TSourceLoc& loc, const TPublicType& type)
{
TSpirvTypeParameters* spirvTypeParams = new TSpirvTypeParameters;
spirvTypeParams->push_back(TSpirvTypeParameter(new TType(type)));
return spirvTypeParams;
}
TSpirvTypeParameters* TParseContext::mergeSpirvTypeParameters(TSpirvTypeParameters* spirvTypeParams1, TSpirvTypeParameters* spirvTypeParams2)
{
// Merge SPIR-V type parameters of the second one to the first one

View File

@ -4439,6 +4439,9 @@ spirv_type_parameter
: constant_expression {
$$ = parseContext.makeSpirvTypeParameters($1->getLoc(), $1->getAsConstantUnion());
}
| type_specifier_nonarray {
$$ = parseContext.makeSpirvTypeParameters($1.loc, $1);
}
spirv_instruction_qualifier
: SPIRV_INSTRUCTION LEFT_PAREN spirv_instruction_qualifier_list RIGHT_PAREN {

View File

@ -4439,6 +4439,9 @@ spirv_type_parameter
: constant_expression {
$$ = parseContext.makeSpirvTypeParameters($1->getLoc(), $1->getAsConstantUnion());
}
| type_specifier_nonarray {
$$ = parseContext.makeSpirvTypeParameters($1.loc, $1);
}
spirv_instruction_qualifier
: SPIRV_INSTRUCTION LEFT_PAREN spirv_instruction_qualifier_list RIGHT_PAREN {

File diff suppressed because it is too large Load Diff

View File

@ -398,6 +398,7 @@ INSTANTIATE_TEST_SUITE_P(
"spv.intrinsicsSpirvStorageClass.rchit",
"spv.intrinsicsSpirvType.rgen",
"spv.intrinsicsSpirvTypeLocalVar.vert",
"spv.intrinsicsSpirvTypeWithTypeSpecifier.vert",
"spv.invariantAll.vert",
"spv.layer.tese",
"spv.layoutNested.vert",