]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/builtin.cpp
Add support for uint types in GLSL
[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                 Parser parser;
32                 Module *module = new Module(parser.parse(*io, "<builtin>", BUILTIN_SOURCE));
33
34                 NodeList<Statement> &shared_body = module->shared.content.body;
35                 NodeList<Statement>::iterator insert_point = shared_body.begin();
36
37                 RefPtr<BasicTypeDeclaration> type = new BasicTypeDeclaration;
38                 type->source = BUILTIN_SOURCE;
39                 type->name = "void";
40                 type->kind = BasicTypeDeclaration::VOID;
41                 shared_body.insert(insert_point, type);
42
43                 type = new BasicTypeDeclaration;
44                 type->source = BUILTIN_SOURCE;
45                 type->name = "bool";
46                 type->size = 1;
47                 type->kind = BasicTypeDeclaration::BOOL;
48                 shared_body.insert(insert_point, type);
49
50                 type = new BasicTypeDeclaration;
51                 type->source = BUILTIN_SOURCE;
52                 type->name = "int";
53                 type->size = 32;
54                 type->kind = BasicTypeDeclaration::INT;
55                 shared_body.insert(insert_point, type);
56
57                 type = new BasicTypeDeclaration;
58                 type->source = BUILTIN_SOURCE;
59                 type->name = "uint";
60                 type->size = 32;
61                 type->sign = false;
62                 type->kind = BasicTypeDeclaration::INT;
63                 shared_body.insert(insert_point, type);
64
65                 type = new BasicTypeDeclaration;
66                 type->source = BUILTIN_SOURCE;
67                 type->name = "float";
68                 type->size = 32;
69                 type->kind = BasicTypeDeclaration::FLOAT;
70                 shared_body.insert(insert_point, type);
71
72                 populate_types(module->shared);
73                 for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
74                         populate_types(*i);
75
76                 builtins_module = module;
77         }
78         return builtins_module.get();
79 }
80
81 const Stage *get_builtins(Stage::Type type)
82 {
83         Module *module = get_builtins_module();
84         if(!module)
85                 return 0;
86
87         if(type==Stage::SHARED)
88                 return &module->shared;
89         for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
90                 if(i->type==type)
91                         return &*i;
92         return 0;
93 }
94
95 } // namespace SL
96 } // namespace GL
97 } // namespace Msp