]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/builtin.cpp
Add a scene type property to the Blender exporter
[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 = "float";
60                 type->size = 32;
61                 type->kind = BasicTypeDeclaration::FLOAT;
62                 shared_body.insert(insert_point, type);
63
64                 populate_types(module->shared);
65                 for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
66                         populate_types(*i);
67
68                 builtins_module = module;
69         }
70         return builtins_module.get();
71 }
72
73 const Stage *get_builtins(Stage::Type type)
74 {
75         Module *module = get_builtins_module();
76         if(!module)
77                 return 0;
78
79         if(type==Stage::SHARED)
80                 return &module->shared;
81         for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
82                 if(i->type==type)
83                         return &*i;
84         return 0;
85 }
86
87 } // namespace SL
88 } // namespace GL
89 } // namespace Msp