]> git.tdb.fi Git - libs/gl.git/blob - source/materials/technique.cpp
Convert Technique and RenderPass loaders to use shared actions
[libs/gl.git] / source / materials / 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(Tag tag)
17 {
18         return insert_unique(passes, tag, RenderPass())->second;
19 }
20
21 bool Technique::has_pass(Tag tag) const
22 {
23         return passes.count(tag);
24 }
25
26 const RenderPass &Technique::get_pass(Tag tag) const
27 {
28         return get_item(passes, tag);
29 }
30
31 const RenderPass *Technique::find_pass(Tag tag) const
32 {
33         PassMap::const_iterator i = passes.find(tag);
34         return (i!=passes.end() ? &i->second : 0);
35 }
36
37 bool Technique::replace_texture(const string &slot, const Texture &tex)
38 {
39         bool replaced = false;
40         for(PassMap::iterator i=passes.begin(); i!=passes.end(); ++i)
41         {
42                 Tag tag = i->second.get_texture_tag(slot);
43                 if(tag.id)
44                 {
45                         i->second.set_texture(tag, &tex);
46                         replaced = true;
47                 }
48         }
49
50         return replaced;
51 }
52
53 bool Technique::replace_material(const string &slot, const Material &mat)
54 {
55         bool replaced = false;
56         for(PassMap::iterator i=passes.begin(); i!=passes.end(); ++i)
57         {
58                 const string &pass_slot = i->second.get_material_slot_name();
59                 if(!pass_slot.empty() && pass_slot==slot)
60                 {
61                         i->second.set_material(&mat);
62                         replaced = true;
63                 }
64         }
65
66         return replaced;
67 }
68
69 bool Technique::replace_uniforms(const ProgramData &shdata)
70 {
71         bool replaced = false;
72         const vector<Tag> &uniform_tags = shdata.get_uniform_tags();
73         for(PassMap::iterator i=passes.begin(); i!=passes.end(); ++i)
74         {
75                 RefPtr<ProgramData> new_shdata;
76                 for(vector<Tag>::const_iterator j=uniform_tags.begin(); j!=uniform_tags.end(); ++j)
77                 {
78                         Tag tag = i->second.get_slotted_uniform_tag(*j);
79                         if(!tag.id)
80                                 continue;
81
82                         if(!new_shdata)
83                                 new_shdata = new ProgramData(*i->second.get_shader_data());
84
85                         new_shdata->uniform(tag, shdata.get_uniform(*j));
86                         replaced = true;
87                 }
88
89                 if(new_shdata)
90                         i->second.set_shader_program(i->second.get_shader_program(), new_shdata.get());
91         }
92
93         return replaced;
94 }
95
96 bool Technique::has_shaders() const
97 {
98         for(PassMap::const_iterator i=passes.begin(); i!=passes.end(); ++i)
99                 if(i->second.get_shader_program())
100                         return true;
101         return false;
102 }
103
104
105 DataFile::Loader::ActionMap Technique::Loader::shared_actions;
106
107 Technique::Loader::Loader(Technique &t):
108         DataFile::CollectionObjectLoader<Technique>(t, 0)
109 {
110         set_actions(shared_actions);
111 }
112
113 Technique::Loader::Loader(Technique &t, Collection &c):
114         DataFile::CollectionObjectLoader<Technique>(t, &c)
115 {
116         set_actions(shared_actions);
117 }
118
119 void Technique::Loader::init_actions()
120 {
121         add("inherit", &Loader::inherit);
122         add("pass", &Loader::pass);
123 }
124
125 void Technique::Loader::inherit(const string &n)
126 {
127         obj.passes = get_collection().get<Technique>(n).get_passes();
128         InheritLoader ldr(obj, get_collection());
129         load_sub_with(ldr);
130 }
131
132 void Technique::Loader::pass(const string &n)
133 {
134         RenderPass p;
135         if(coll)
136                 load_sub(p, get_collection());
137         else
138                 load_sub(p);
139
140         if(!p.get_shader_program())
141                 throw logic_error("no shader program in pass");
142
143         insert_unique(obj.passes, n, p);
144 }
145
146
147 Technique::InheritLoader::InheritLoader(Technique &t, Collection &c):
148         DataFile::CollectionObjectLoader<Technique>(t, &c)
149 {
150         add("material", &InheritLoader::material);
151         add("texture", &InheritLoader::texture);
152         add("uniforms", &InheritLoader::uniforms);
153 }
154
155 void Technique::InheritLoader::material(const string &slot, const string &name)
156 {
157         const Material &mat = get_collection().get<Material>(name);
158         if(obj.replace_material(slot, mat))
159                 return;
160
161         // For backwards compatibility
162         RenderPass &pass = get_item(obj.passes, slot);
163         if(const Material *base_mat = pass.get_material())
164         {
165                 for(PassMap::iterator i=obj.passes.begin(); i!=obj.passes.end(); ++i)
166                         if(i->second.get_material()==base_mat)
167                                 i->second.set_material(&mat);
168         }
169         else
170                 pass.set_material(&mat);
171 }
172
173 void Technique::InheritLoader::texture(const string &slot, const string &name)
174 {
175         if(!obj.replace_texture(slot, get_collection().get<Texture>(name)))
176                 throw key_error(slot);
177 }
178
179 void Technique::InheritLoader::uniforms()
180 {
181         ProgramData shdata;
182         load_sub(shdata);
183         obj.replace_uniforms(shdata);
184 }
185
186 } // namespace GL
187 } // namespace Msp