]> git.tdb.fi Git - libs/gl.git/commitdiff
Refactor creation of the shader builtin module
authorMikko Rasa <tdb@tdb.fi>
Mon, 15 Feb 2021 15:50:03 +0000 (17:50 +0200)
committerMikko Rasa <tdb@tdb.fi>
Mon, 15 Feb 2021 15:50:03 +0000 (17:50 +0200)
It can now use VariableResolver::apply like everything else.

source/glsl/builtin.cpp

index c9118f69d3ffa6e596d44111f8fc9e30b0835c42..1fd496b40c5e8210636960d2a69644363844e95a 100644 (file)
@@ -28,29 +28,35 @@ namespace Msp {
 namespace GL {
 namespace SL {
 
-Module *create_builtins_module()
+Module *get_builtins_module()
 {
-       Parser parser;
-       Module *module = new Module(parser.parse(builtins_src, "<builtin>"));
-       for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
+       static RefPtr<Module> builtins_module;
+       static bool initialized = false;
+       if(!initialized)
        {
-               VariableResolver().visit(i->content);
-               for(map<string, VariableDeclaration *>::iterator j=i->content.variables.begin(); j!=i->content.variables.end(); ++j)
-                       j->second->linked_declaration = j->second;
-       }
-       return module;
-}
+               initialized = true;
 
-Module &get_builtins_module()
-{
-       static RefPtr<Module> builtins_module = create_builtins_module();
-       return *builtins_module;
+               Parser parser;
+               Module *module = new Module(parser.parse(builtins_src, "<builtin>"));
+               for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
+               {
+                       VariableResolver().apply(*i);
+                       for(map<string, VariableDeclaration *>::iterator j=i->content.variables.begin(); j!=i->content.variables.end(); ++j)
+                               j->second->linked_declaration = j->second;
+               }
+
+               builtins_module = module;
+       }
+       return builtins_module.get();
 }
 
 Stage *get_builtins(Stage::Type type)
 {
-       Module &module = get_builtins_module();
-       for(list<Stage>::iterator i=module.stages.begin(); i!=module.stages.end(); ++i)
+       Module *module = get_builtins_module();
+       if(!module)
+               return 0;
+
+       for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
                if(i->type==type)
                        return &*i;
        return 0;