]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/builtin.cpp
670cfc2d9f4878b1d55abca1cf453adf8c628816
[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
30                 RefPtr<BasicTypeDeclaration> type = new BasicTypeDeclaration;
31                 type->source = BUILTIN_SOURCE;
32                 type->name = "void";
33                 type->kind = BasicTypeDeclaration::VOID;
34                 shared_body.insert(shared_body.begin(), type);
35
36                 type = new BasicTypeDeclaration;
37                 type->source = BUILTIN_SOURCE;
38                 type->name = "bool";
39                 type->kind = BasicTypeDeclaration::BOOL;
40                 shared_body.insert(shared_body.begin(), type);
41
42                 type = new BasicTypeDeclaration;
43                 type->source = BUILTIN_SOURCE;
44                 type->name = "int";
45                 type->size = 32;
46                 type->kind = BasicTypeDeclaration::INT;
47                 shared_body.insert(shared_body.begin(), type);
48
49                 type = new BasicTypeDeclaration;
50                 type->source = BUILTIN_SOURCE;
51                 type->name = "float";
52                 type->size = 32;
53                 type->kind = BasicTypeDeclaration::FLOAT;
54                 shared_body.insert(shared_body.begin(), type);
55
56                 TypeResolver().apply(module->shared);
57                 for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
58                         TypeResolver().apply(*i);
59
60                 builtins_module = module;
61         }
62         return builtins_module.get();
63 }
64
65 const Stage *get_builtins(Stage::Type type)
66 {
67         Module *module = get_builtins_module();
68         if(!module)
69                 return 0;
70
71         if(type==Stage::SHARED)
72                 return &module->shared;
73         for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
74                 if(i->type==type)
75                         return &*i;
76         return 0;
77 }
78
79 } // namespace SL
80 } // namespace GL
81 } // namespace Msp