From c1b42fd479c5ca4cbab258ee0fa5c7cac319f928 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Fri, 2 Apr 2021 00:26:37 +0300 Subject: [PATCH] Make constant id range configurable and avoid duplicates GlslangValidator apparently only allows IDs up to 2047. I've yet to find out if there's a limit in SPIR-V itself. --- source/glsl/compiler.cpp | 3 +-- source/glsl/features.cpp | 3 ++- source/glsl/features.h | 1 + source/glsl/generate.cpp | 27 ++++++++++++++++++++++++--- source/glsl/generate.h | 6 +++++- 5 files changed, 33 insertions(+), 7 deletions(-) diff --git a/source/glsl/compiler.cpp b/source/glsl/compiler.cpp index fab05473..5ff003e7 100644 --- a/source/glsl/compiler.cpp +++ b/source/glsl/compiler.cpp @@ -80,6 +80,7 @@ void Compiler::compile(Mode mode) { for(list::iterator i=module->stages.begin(); i!=module->stages.end(); ++i) generate(*i); + ConstantIdAssigner().apply(*module, features); bool valid = true; for(list::iterator i=module->stages.begin(); i!=module->stages.end(); ++i) @@ -267,8 +268,6 @@ void Compiler::generate(Stage &stage) variables through interfaces. */ InterfaceGenerator().apply(stage); resolve(stage, RESOLVE_BLOCKS|RESOLVE_TYPES|RESOLVE_VARIABLES); - - ConstantIdAssigner().apply(stage); } template diff --git a/source/glsl/features.cpp b/source/glsl/features.cpp index 11b6c942..dab230d3 100644 --- a/source/glsl/features.cpp +++ b/source/glsl/features.cpp @@ -15,7 +15,8 @@ Features::Features(): arb_gpu_shader5(false), arb_uniform_buffer_object(false), ext_gpu_shader4(false), - ext_texture_array(false) + ext_texture_array(false), + max_constant_id(0x7FFFFFFF) { } Features Features::from_context() diff --git a/source/glsl/features.h b/source/glsl/features.h index 8d32f8be..b65a61e2 100644 --- a/source/glsl/features.h +++ b/source/glsl/features.h @@ -16,6 +16,7 @@ struct Features bool arb_uniform_buffer_object; bool ext_gpu_shader4; bool ext_texture_array; + unsigned max_constant_id; Features(); diff --git a/source/glsl/generate.cpp b/source/glsl/generate.cpp index f345670b..92d172c5 100644 --- a/source/glsl/generate.cpp +++ b/source/glsl/generate.cpp @@ -8,9 +8,28 @@ namespace Msp { namespace GL { namespace SL { -void ConstantIdAssigner::apply(Stage &stage) +void ConstantIdAssigner::apply(Module &module, const Features &features) { - stage.content.visit(*this); + for(list::iterator i=module.stages.begin(); i!=module.stages.end(); ++i) + i->content.visit(*this); + + unsigned max_id = features.max_constant_id; + for(vector::iterator i=auto_constants.begin(); i!=auto_constants.end(); ++i) + { + unsigned id = hash32((*i)->name)%(max_id+1); + while(used_ids.count(id)) + ++id; + + vector &qualifiers = (*i)->layout->qualifiers; + for(vector::iterator j=qualifiers.begin(); j!=qualifiers.end(); ++j) + if(j->name=="constant_id") + { + j->value = id; + break; + } + + used_ids.insert(id); + } } void ConstantIdAssigner::visit(VariableDeclaration &var) @@ -22,7 +41,9 @@ void ConstantIdAssigner::visit(VariableDeclaration &var) if(i->name=="constant_id" && i->has_value) { if(i->value==-1) - i->value = hash32(var.name)&0x7FFFFFFF; + auto_constants.push_back(&var); + else + used_ids.insert(i->value); break; } } diff --git a/source/glsl/generate.h b/source/glsl/generate.h index b2cfadc4..eb7d488a 100644 --- a/source/glsl/generate.h +++ b/source/glsl/generate.h @@ -14,8 +14,12 @@ namespace SL { /** Assigns IDs to specialization constants with an automatic ID. */ class ConstantIdAssigner: private TraversingVisitor { +private: + std::set used_ids; + std::vector auto_constants; + public: - void apply(Stage &); + void apply(Module &, const Features &); private: virtual void visit(VariableDeclaration &); -- 2.43.0