]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/builtin.cpp
Fix opcode for matrix inverse
[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 add_builtin_type(Stage &stage, const std::string &name, BasicTypeDeclaration::Kind kind, unsigned size, unsigned sign)
13 {
14         RefPtr<BasicTypeDeclaration> type = new BasicTypeDeclaration;
15         type->source = BUILTIN_SOURCE;
16         type->name = name;
17         type->kind = kind;
18         type->size = size;
19         type->sign = sign;
20         stage.content.body.push_back(type);
21         stage.types[name] = type.get();
22 }
23
24 Module *get_builtins_module()
25 {
26         static RefPtr<Module> builtins_module;
27         static bool initialized = false;
28         if(!initialized)
29         {
30                 initialized = true;
31
32                 RefPtr<IO::Seekable> io = Resources::get_builtins().open("_builtin.glsl");
33                 if(!io)
34                         return 0;
35
36                 builtins_module = new Module;
37                 add_builtin_type(builtins_module->shared, "void", BasicTypeDeclaration::VOID, 0, true);
38                 add_builtin_type(builtins_module->shared, "bool", BasicTypeDeclaration::BOOL, 1, true);
39                 add_builtin_type(builtins_module->shared, "int", BasicTypeDeclaration::INT, 32, true);
40                 add_builtin_type(builtins_module->shared, "uint", BasicTypeDeclaration::INT, 32, false);
41                 add_builtin_type(builtins_module->shared, "float", BasicTypeDeclaration::FLOAT, 32, true);
42
43                 try
44                 {
45                         Parser parser(0);
46                         parser.parse(*builtins_module, *io, "<builtin>", BUILTIN_SOURCE);
47                 }
48                 catch(...)
49                 {
50                         builtins_module = 0;
51                         throw;
52                 }
53         }
54         return builtins_module.get();
55 }
56
57 const Stage *get_builtins(Stage::Type type)
58 {
59         Module *module = get_builtins_module();
60         if(!module)
61                 return 0;
62
63         if(type==Stage::SHARED)
64                 return &module->shared;
65         for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
66                 if(i->type==type)
67                         return &*i;
68         return 0;
69 }
70
71 } // namespace SL
72 } // namespace GL
73 } // namespace Msp