]> git.tdb.fi Git - libs/gl.git/blob - source/technique.cpp
Check for null camera in View::render
[libs/gl.git] / source / technique.cpp
1 #include <msp/core/refptr.h>
2 #include <msp/datafile/collection.h>
3 #include <msp/strings/format.h>
4 #include "material.h"
5 #include "program.h"
6 #include "programdata.h"
7 #include "tag.h"
8 #include "technique.h"
9 #include "texture.h"
10
11 using namespace std;
12
13 namespace Msp {
14 namespace GL {
15
16 RenderPass &Technique::add_pass(const Tag &tag)
17 {
18         return insert_unique(passes, tag, RenderPass())->second;
19 }
20
21 bool Technique::has_pass(const Tag &tag) const
22 {
23         return passes.count(tag);
24 }
25
26 const RenderPass &Technique::get_pass(const Tag &tag) const
27 {
28         return get_item(passes, tag);
29 }
30
31 bool Technique::replace_texture(const string &slot, const Texture &tex)
32 {
33         bool replaced = false;
34         for(PassMap::iterator i=passes.begin(); i!=passes.end(); ++i)
35         {
36                 int index = i->second.get_texture_index(slot);
37                 if(index>=0)
38                 {
39                         i->second.set_texture(index, &tex);
40                         replaced = true;
41                 }
42         }
43
44         return replaced;
45 }
46
47 bool Technique::replace_material(const string &slot, const Material &mat)
48 {
49         bool replaced = false;
50         for(PassMap::iterator i=passes.begin(); i!=passes.end(); ++i)
51         {
52                 const string &pass_slot = i->second.get_material_slot_name();
53                 if(!pass_slot.empty() && pass_slot==slot)
54                 {
55                         i->second.set_material(&mat);
56                         replaced = true;
57                 }
58         }
59
60         return replaced;
61 }
62
63 bool Technique::replace_uniforms(const ProgramData &shdata)
64 {
65         bool replaced = false;
66         const vector<string> &uniform_names = shdata.get_uniform_names();
67         for(PassMap::iterator i=passes.begin(); i!=passes.end(); ++i)
68         {
69                 RefPtr<ProgramData> new_shdata;
70                 for(vector<string>::const_iterator j=uniform_names.begin(); j!=uniform_names.end(); ++j)
71                 {
72                         const string &name = i->second.get_slotted_uniform_name(*j);
73                         if(name.empty())
74                                 continue;
75
76                         if(!new_shdata)
77                                 new_shdata = new ProgramData(*i->second.get_shader_data());
78
79                         new_shdata->uniform(name, shdata.get_uniform(*j));
80                         replaced = true;
81                 }
82
83                 if(new_shdata)
84                         i->second.set_shader_program(i->second.get_shader_program(), new_shdata.get());
85         }
86
87         return replaced;
88 }
89
90 bool Technique::has_shaders() const
91 {
92         for(PassMap::const_iterator i=passes.begin(); i!=passes.end(); ++i)
93                 if(i->second.get_shader_program())
94                         return true;
95         return false;
96 }
97
98
99 Technique::Loader::Loader(Technique &t):
100         DataFile::CollectionObjectLoader<Technique>(t, 0)
101 {
102         init();
103 }
104
105 Technique::Loader::Loader(Technique &t, Collection &c):
106         DataFile::CollectionObjectLoader<Technique>(t, &c)
107 {
108         init();
109 }
110
111 void Technique::Loader::init()
112 {
113         add("inherit", &Loader::inherit);
114         add("pass", &Loader::pass);
115 }
116
117 void Technique::Loader::inherit(const string &n)
118 {
119         obj.passes = get_collection().get<Technique>(n).get_passes();
120         InheritLoader ldr(obj, get_collection());
121         load_sub_with(ldr);
122 }
123
124 void Technique::Loader::pass(const string &n)
125 {
126         RenderPass p;
127         if(coll)
128                 load_sub(p, get_collection());
129         else
130                 load_sub(p);
131
132         insert_unique(obj.passes, n, p);
133 }
134
135
136 Technique::InheritLoader::InheritLoader(Technique &t, Collection &c):
137         DataFile::CollectionObjectLoader<Technique>(t, &c)
138 {
139         add("material", &InheritLoader::material);
140         add("texture", &InheritLoader::texture);
141         add("uniforms", &InheritLoader::uniforms);
142 }
143
144 void Technique::InheritLoader::material(const string &slot, const string &name)
145 {
146         const Material &mat = get_collection().get<Material>(name);
147         if(obj.replace_material(slot, mat))
148                 return;
149
150         // For backwards compatibility
151         RenderPass &pass = get_item(obj.passes, slot);
152         if(const Material *base_mat = pass.get_material())
153         {
154                 for(PassMap::iterator i=obj.passes.begin(); i!=obj.passes.end(); ++i)
155                         if(i->second.get_material()==base_mat)
156                                 i->second.set_material(&mat);
157         }
158         else
159                 pass.set_material(&mat);
160 }
161
162 void Technique::InheritLoader::texture(const string &slot, const string &name)
163 {
164         if(!obj.replace_texture(slot, get_collection().get<Texture>(name)))
165                 throw key_error(slot);
166 }
167
168 void Technique::InheritLoader::uniforms()
169 {
170         ProgramData shdata;
171         load_sub(shdata);
172         obj.replace_uniforms(shdata);
173 }
174
175 } // namespace GL
176 } // namespace Msp