]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/builtin.cpp
Minor tweaks to MemoryAllocator
[libs/gl.git] / source / glsl / builtin.cpp
1 #include <msp/core/algorithm.h>
2 #include <msp/gl/resources.h>
3 #include <msp/io/seekable.h>
4 #include "builtin.h"
5 #include "parser.h"
6
7 using namespace std;
8
9 namespace Msp {
10 namespace GL {
11 namespace SL {
12
13 void add_builtin_type(Stage &stage, const string &name, BasicTypeDeclaration::Kind kind, unsigned size, unsigned sign)
14 {
15         RefPtr<BasicTypeDeclaration> type = new BasicTypeDeclaration;
16         type->source = BUILTIN_SOURCE;
17         type->name = name;
18         type->kind = kind;
19         type->size = size;
20         type->sign = sign;
21         stage.content.body.push_back(type);
22         stage.types[name] = type.get();
23 }
24
25 Module *get_builtins_module()
26 {
27         static RefPtr<Module> builtins_module;
28         static bool initialized = false;
29         if(!initialized)
30         {
31                 initialized = true;
32
33                 RefPtr<IO::Seekable> io = Resources::get_builtins().open("_builtin.glsl");
34                 if(!io)
35                         return 0;
36
37                 builtins_module = new Module;
38                 add_builtin_type(builtins_module->shared, "void", BasicTypeDeclaration::VOID, 0, true);
39                 add_builtin_type(builtins_module->shared, "bool", BasicTypeDeclaration::BOOL, 1, true);
40                 add_builtin_type(builtins_module->shared, "int", BasicTypeDeclaration::INT, 32, true);
41                 add_builtin_type(builtins_module->shared, "uint", BasicTypeDeclaration::INT, 32, false);
42                 add_builtin_type(builtins_module->shared, "float", BasicTypeDeclaration::FLOAT, 32, true);
43
44                 try
45                 {
46                         Parser parser(0);
47                         parser.parse(*builtins_module, *io, "<builtin>", BUILTIN_SOURCE);
48                 }
49                 catch(...)
50                 {
51                         builtins_module = 0;
52                         throw;
53                 }
54         }
55         return builtins_module.get();
56 }
57
58 const Stage *get_builtins(Stage::Type type)
59 {
60         Module *module = get_builtins_module();
61         if(!module)
62                 return 0;
63
64         if(type==Stage::SHARED)
65                 return &module->shared;
66         auto i = find_member(module->stages, type, &Stage::type);
67         return (i!=module->stages.end() ? &*i : 0);
68 }
69
70 } // namespace SL
71 } // namespace GL
72 } // namespace Msp