]> git.tdb.fi Git - libs/gl.git/blob - source/materials/renderpass.cpp
Rearrange material-related state management in RenderPass
[libs/gl.git] / source / materials / renderpass.cpp
1 #include <msp/core/algorithm.h>
2 #include <msp/datafile/collection.h>
3 #include <msp/io/print.h>
4 #include <msp/strings/format.h>
5 #include "error.h"
6 #include "renderpass.h"
7 #include "program.h"
8 #include "programdata.h"
9 #include "renderer.h"
10 #include "texture.h"
11 #include "texture2d.h"
12 #include "texturing.h"
13 #include "uniform.h"
14
15 using namespace std;
16
17 namespace Msp {
18 namespace GL {
19
20 RenderPass::RenderPass():
21         shprog(0),
22         shprog_from_material(false),
23         shdata(0),
24         material(0),
25         back_faces(false)
26 { }
27
28 void RenderPass::set_material_textures()
29 {
30         const Tag *material_texture_tags = material->get_texture_tags();
31         for(const Tag *tag=material_texture_tags; tag->id; ++tag)
32                 set_texture(*tag, material->get_texture(*tag), material->get_sampler());
33 }
34
35 void RenderPass::maybe_create_material_shader(DataFile::Collection *coll)
36 {
37         if(shprog && !shprog_from_material)
38                 return;
39
40         if(coll)
41         {
42                 shprog = material->create_compatible_shader(*coll);
43                 shprog.keep();
44         }
45         else
46                 throw invalid_operation("RenderPass::maybe_create_material_shader");
47
48         if(shdata)
49                 shdata = new ProgramData(*shdata, shprog.get());
50
51         shprog_from_material = true;
52 }
53
54 void RenderPass::set_shader_program(const Program *prog, const ProgramData *data)
55 {
56         shprog = prog;
57         shprog.keep();
58         shprog_from_material = false;
59         shdata = (data ? new ProgramData(*data) : 0);
60 }
61
62 Tag RenderPass::get_slotted_uniform_tag(Tag slot) const
63 {
64         map<Tag, Tag>::const_iterator i = uniform_slots.find(slot);
65         if(i==uniform_slots.end())
66                 return Tag();
67         return i->second;
68 }
69
70 void RenderPass::set_material(const Material *mat, DataFile::Collection *coll)
71 {
72         material = mat;
73         material.keep();
74         maybe_create_material_shader(coll);
75         set_material_textures();
76 }
77
78 void RenderPass::set_texture(Tag tag, const Texture *tex, const Sampler *samp)
79 {
80         vector<TextureSlot>::iterator i = find_member(textures, tag, &TextureSlot::tag);
81         if(i==textures.end())
82         {
83                 textures.push_back(TextureSlot(tag));
84                 i = textures.end()-1;
85         }
86         i->texture = tex;
87         if(samp)
88                 i->sampler = samp;
89 }
90
91 Tag RenderPass::get_texture_tag(const string &slot) const
92 {
93         vector<TextureSlot>::const_iterator i = find_member(textures, slot, &TextureSlot::slot_name);
94         return (i!=textures.end() ? i->tag : Tag());
95 }
96
97 void RenderPass::set_texture(unsigned index, const Texture *tex, const Sampler *samp)
98 {
99         if(!shprog)
100                 throw invalid_operation("RenderPass::set_texture");
101
102         const vector<Program::UniformInfo> &uniforms = shprog->get_uniforms();
103         for(vector<Program::UniformInfo>::const_iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
104                 if(is_image(i->type) && i->binding==static_cast<int>(index))
105                         return set_texture(i->tag, tex, samp);
106
107         if(shdata)
108         {
109                 const vector<Tag> &tags = shdata->get_uniform_tags();
110                 for(vector<Tag>::const_iterator i=tags.begin(); i!=tags.end(); ++i)
111                 {
112                         vector<Program::UniformInfo>::const_iterator j = find_member(uniforms, *i, &Program::UniformInfo::tag);
113                         if(j==uniforms.end() || !is_image(j->type))
114                                 continue;
115                         if(const Uniform1i *uni1i = dynamic_cast<const Uniform1i *>(shdata->find_uniform(*i)))
116                                 if(uni1i->get()==static_cast<int>(index))
117                                         return set_texture(*i, tex, samp);
118                 }
119         }
120 }
121
122 int RenderPass::get_texture_index(const string &n) const
123 {
124         vector<TextureSlot>::const_iterator i = find_member(textures, n, &TextureSlot::slot_name);
125         return (shprog && i!=textures.end() ? shprog->get_uniform_binding(i->tag) : -1);
126 }
127
128 void RenderPass::apply(Renderer &renderer) const
129 {
130         for(vector<TextureSlot>::const_iterator i=textures.begin(); i!=textures.end(); ++i)
131                 renderer.set_texture(i->tag, i->texture, i->sampler);
132         renderer.set_material(material.get());
133         renderer.set_shader_program(shprog.get(), shdata.get());
134         renderer.set_reverse_winding(back_faces);
135 }
136
137
138 DataFile::Loader::ActionMap RenderPass::Loader::shared_actions;
139
140 RenderPass::Loader::Loader(RenderPass &p):
141         DataFile::CollectionObjectLoader<RenderPass>(p, 0)
142 {
143         set_actions(shared_actions);
144 }
145
146 RenderPass::Loader::Loader(RenderPass &p, Collection &c):
147         DataFile::CollectionObjectLoader<RenderPass>(p, &c)
148 {
149         set_actions(shared_actions);
150 }
151
152 void RenderPass::Loader::init_actions()
153 {
154         add("shader",   &Loader::shader);
155         add("material", &Loader::material_inline);
156         add("material", &Loader::material);
157         add("material_slot", &RenderPass::material_slot);
158         add("back_faces",&RenderPass::back_faces);
159         add("texture", &Loader::texture);
160         add("uniforms", &Loader::uniforms);
161         add("uniform_slot", &Loader::uniform_slot);
162         add("uniform_slot", &Loader::uniform_slot2);
163
164         // Deprecated
165         add("texunit",  &Loader::texunit);
166         add("texunit",  &Loader::texture);
167         add("texunit",  &Loader::texunit_named);
168 }
169
170 void RenderPass::Loader::finish()
171 {
172         if(obj.material)
173                 obj.maybe_create_material_shader(coll);
174 }
175
176 // Temporary compatibility feature
177 string RenderPass::Loader::get_shader_name(const string &n)
178 {
179         if(n.size()>=5 && !n.compare(n.size()-5, 5, ".glsl"))
180         {
181                 IO::print(IO::cerr, "Warning: Loading module '%s' as shader is deprecated\n", n);
182                 return n+".shader";
183         }
184         return n;
185 }
186
187 void RenderPass::Loader::material_inline()
188 {
189         Material::GenericLoader ldr(coll);
190         load_sub_with(ldr);
191         obj.material = ldr.get_material();
192         obj.set_material_textures();
193 }
194
195 void RenderPass::Loader::material(const string &name)
196 {
197         obj.material = &get_collection().get<Material>(name);
198         obj.material.keep();
199         obj.set_material_textures();
200 }
201
202 void RenderPass::Loader::shader(const string &n)
203 {
204         obj.shprog = &get_collection().get<Program>(get_shader_name(n));
205         obj.shprog.keep();
206         obj.shprog_from_material = false;
207         if(obj.shdata)
208                 obj.shdata = new ProgramData(*obj.shdata, obj.shprog.get());
209 }
210
211 void RenderPass::Loader::texture(const string &n)
212 {
213         vector<TextureSlot>::iterator i = find_member(obj.textures, Tag(n), &TextureSlot::tag);
214         if(i==obj.textures.end())
215         {
216                 obj.textures.push_back(TextureSlot(n));
217                 i = obj.textures.end()-1;
218         }
219         TextureSlot::Loader ldr(*i, n, coll);
220         load_sub_with(ldr);
221 }
222
223 void RenderPass::Loader::texunit(unsigned)
224 {
225         IO::print(IO::cerr, "Warning: specifying textures by unit number is deprecated and may not produce expected results");
226         string name;
227         if(obj.shprog)
228         {
229                 const vector<Program::UniformInfo> &uniforms = obj.shprog->get_uniforms();
230                 for(vector<Program::UniformInfo>::const_iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
231                         if(is_image(i->type) && i->binding>=0)
232                         {
233                                 if(!name.empty())
234                                 {
235                                         name.clear();
236                                         break;
237                                 }
238                                 name = i->name;
239                         }
240         }
241
242         if(name.empty())
243                 throw runtime_error("Could not determine name for texture");
244
245         texture(name);
246 }
247
248 void RenderPass::Loader::texunit_named(unsigned, const string &n)
249 {
250         texture(n);
251 }
252
253 void RenderPass::Loader::uniforms()
254 {
255         if(!obj.shprog || obj.shprog_from_material)
256                 throw runtime_error("Shader is required for uniforms");
257         if(!obj.shdata)
258                 obj.shdata = new ProgramData(obj.shprog.get());
259         else if(obj.shdata.refcount()>1)
260                 obj.shdata = new ProgramData(*obj.shdata);
261         load_sub(*obj.shdata);
262 }
263
264 void RenderPass::Loader::uniform_slot(const string &name)
265 {
266         uniform_slot2(name, name);
267 }
268
269 void RenderPass::Loader::uniform_slot2(const string &name, const string &slot)
270 {
271         obj.uniform_slots[slot] = name;
272 }
273
274
275 RenderPass::TextureSlot::Loader::Loader(TextureSlot &ts, const string &an, Collection *c):
276         CollectionObjectLoader<TextureSlot>(ts, c),
277         auto_slot_name(an)
278 {
279         add("sampler", &TextureSlot::sampler);
280         add("slot", &Loader::slot_auto);
281         add("slot", &TextureSlot::slot_name);
282         add("texture", &TextureSlot::texture);
283 }
284
285 void RenderPass::TextureSlot::Loader::slot_auto()
286 {
287         obj.slot_name = auto_slot_name;
288 }
289
290 } // namespace GL
291 } // namespace Msp