]> git.tdb.fi Git - libs/gl.git/commitdiff
Make constant id range configurable and avoid duplicates
authorMikko Rasa <tdb@tdb.fi>
Thu, 1 Apr 2021 21:26:37 +0000 (00:26 +0300)
committerMikko Rasa <tdb@tdb.fi>
Thu, 1 Apr 2021 21:26:37 +0000 (00:26 +0300)
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
source/glsl/features.cpp
source/glsl/features.h
source/glsl/generate.cpp
source/glsl/generate.h

index fab054737a8872bc7e8631b2298615c0fc9b6cf0..5ff003e71722e5da09d0cda8786bbca89f7726f8 100644 (file)
@@ -80,6 +80,7 @@ void Compiler::compile(Mode mode)
 {
        for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
                generate(*i);
+       ConstantIdAssigner().apply(*module, features);
 
        bool valid = true;
        for(list<Stage>::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<typename T>
index 11b6c942fa4324b4e6b15f546d11bf440ab0bacc..dab230d339c517f2a4f4bd5a3ecf895b07c08c10 100644 (file)
@@ -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()
index 8d32f8beefa35254a3d1cce8bb1b8c72b2e91979..b65a61e2fc8c0690a2e380ca690755c09b47bcba 100644 (file)
@@ -16,6 +16,7 @@ struct Features
        bool arb_uniform_buffer_object;
        bool ext_gpu_shader4;
        bool ext_texture_array;
+       unsigned max_constant_id;
 
        Features();
 
index f345670b0451d462f0b1083eb047b605e4a006bb..92d172c586469f78cd17c5d338700fc68e503c00 100644 (file)
@@ -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<Stage>::iterator i=module.stages.begin(); i!=module.stages.end(); ++i)
+               i->content.visit(*this);
+
+       unsigned max_id = features.max_constant_id;
+       for(vector<VariableDeclaration *>::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<Layout::Qualifier> &qualifiers = (*i)->layout->qualifiers;
+               for(vector<Layout::Qualifier>::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;
                        }
        }
index b2cfadc428f6184e7e44b74c310e300e7cc122c4..eb7d488ae6071faa619f3e374c61084686e028b9 100644 (file)
@@ -14,8 +14,12 @@ namespace SL {
 /** Assigns IDs to specialization constants with an automatic ID. */
 class ConstantIdAssigner: private TraversingVisitor
 {
+private:
+       std::set<unsigned> used_ids;
+       std::vector<VariableDeclaration *> auto_constants;
+
 public:
-       void apply(Stage &);
+       void apply(Module &, const Features &);
 
 private:
        virtual void visit(VariableDeclaration &);