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