]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/builtin.cpp
Redesign loading of GLSL sources
[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
6 using namespace std;
7
8 namespace Msp {
9 namespace GL {
10 namespace SL {
11
12 void populate_types(Stage &stage)
13 {
14         for(NodeList<Statement>::const_iterator i=stage.content.body.begin(); i!=stage.content.body.end(); ++i)
15                 if(TypeDeclaration *type = dynamic_cast<TypeDeclaration *>(i->get()))
16                         stage.types[type->name] = type;
17 }
18
19 Module *get_builtins_module()
20 {
21         static RefPtr<Module> builtins_module;
22         static bool initialized = false;
23         if(!initialized)
24         {
25                 initialized = true;
26
27                 RefPtr<IO::Seekable> io = Resources::get_builtins().open("_builtin.glsl");
28                 if(!io)
29                         return 0;
30
31                 RefPtr<Module> module = new Module;
32                 Parser parser(0);
33                 parser.parse(*module, *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 = "uint";
61                 type->size = 32;
62                 type->sign = false;
63                 type->kind = BasicTypeDeclaration::INT;
64                 shared_body.insert(insert_point, type);
65
66                 type = new BasicTypeDeclaration;
67                 type->source = BUILTIN_SOURCE;
68                 type->name = "float";
69                 type->size = 32;
70                 type->kind = BasicTypeDeclaration::FLOAT;
71                 shared_body.insert(insert_point, type);
72
73                 populate_types(module->shared);
74                 for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
75                         populate_types(*i);
76
77                 builtins_module = module.release();
78         }
79         return builtins_module.get();
80 }
81
82 const Stage *get_builtins(Stage::Type type)
83 {
84         Module *module = get_builtins_module();
85         if(!module)
86                 return 0;
87
88         if(type==Stage::SHARED)
89                 return &module->shared;
90         for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
91                 if(i->type==type)
92                         return &*i;
93         return 0;
94 }
95
96 } // namespace SL
97 } // namespace GL
98 } // namespace Msp