]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/builtin.cpp
Populate the builtin module's type maps directly
[libs/gl.git] / source / glsl / builtin.cpp
1 #include <msp/gl/resources.h>
2 #include <msp/io/seekable.h>
3 #include "builtin.h"
4 #include "parser.h"
5 #include "visitor.h"
6
7 using namespace std;
8
9 namespace Msp {
10 namespace GL {
11 namespace SL {
12
13 void populate_types(Stage &stage)
14 {
15         for(NodeList<Statement>::const_iterator i=stage.content.body.begin(); i!=stage.content.body.end(); ++i)
16                 if(TypeDeclaration *type = dynamic_cast<TypeDeclaration *>(i->get()))
17                         stage.types[type->name] = type;
18 }
19
20 Module *get_builtins_module()
21 {
22         static RefPtr<Module> builtins_module;
23         static bool initialized = false;
24         if(!initialized)
25         {
26                 initialized = true;
27
28                 RefPtr<IO::Seekable> io = Resources::get_builtins().open("_builtin.glsl");
29                 if(!io)
30                         return 0;
31
32                 Parser parser;
33                 Module *module = new Module(parser.parse(*io, "<builtin>", BUILTIN_SOURCE));
34
35                 NodeList<Statement> &shared_body = module->shared.content.body;
36                 NodeList<Statement>::iterator insert_point = shared_body.begin();
37
38                 RefPtr<BasicTypeDeclaration> type = new BasicTypeDeclaration;
39                 type->source = BUILTIN_SOURCE;
40                 type->name = "void";
41                 type->kind = BasicTypeDeclaration::VOID;
42                 shared_body.insert(insert_point, type);
43
44                 type = new BasicTypeDeclaration;
45                 type->source = BUILTIN_SOURCE;
46                 type->name = "bool";
47                 type->size = 1;
48                 type->kind = BasicTypeDeclaration::BOOL;
49                 shared_body.insert(insert_point, type);
50
51                 type = new BasicTypeDeclaration;
52                 type->source = BUILTIN_SOURCE;
53                 type->name = "int";
54                 type->size = 32;
55                 type->kind = BasicTypeDeclaration::INT;
56                 shared_body.insert(insert_point, type);
57
58                 type = new BasicTypeDeclaration;
59                 type->source = BUILTIN_SOURCE;
60                 type->name = "float";
61                 type->size = 32;
62                 type->kind = BasicTypeDeclaration::FLOAT;
63                 shared_body.insert(insert_point, type);
64
65                 populate_types(module->shared);
66                 for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
67                         populate_types(*i);
68
69                 builtins_module = module;
70         }
71         return builtins_module.get();
72 }
73
74 const Stage *get_builtins(Stage::Type type)
75 {
76         Module *module = get_builtins_module();
77         if(!module)
78                 return 0;
79
80         if(type==Stage::SHARED)
81                 return &module->shared;
82         for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
83                 if(i->type==type)
84                         return &*i;
85         return 0;
86 }
87
88 } // namespace SL
89 } // namespace GL
90 } // namespace Msp