]> git.tdb.fi Git - libs/gl.git/blob - source/materials/renderpass.cpp
4ab72b568d32a9704c1c56ff90dab503e7b8a297
[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         receive_shadows(false),
27         image_based_lighting(false)
28 { }
29
30 void RenderPass::set_material_textures()
31 {
32         const Tag *material_texture_tags = material->get_texture_tags();
33         for(const Tag *tag=material_texture_tags; tag->id; ++tag)
34                 set_texture(*tag, material->get_texture(*tag), material->get_sampler());
35 }
36
37 void RenderPass::maybe_create_material_shader()
38 {
39         if(shprog && !shprog_from_material)
40                 return;
41
42         map<string, int> extra_spec;
43         if(receive_shadows)
44                 extra_spec["use_shadow_map"] = true;
45         if(image_based_lighting)
46                 extra_spec["use_image_based_lighting"] = true;
47
48         shprog = material->create_compatible_shader(extra_spec);
49
50         if(shdata)
51                 shdata = new ProgramData(*shdata, shprog);
52
53         shprog_from_material = true;
54 }
55
56 void RenderPass::set_shader_program(const Program *prog, const ProgramData *data)
57 {
58         shprog = prog;
59         shprog_from_material = false;
60         shdata = (data ? new ProgramData(*data) : 0);
61 }
62
63 Tag RenderPass::get_slotted_uniform_tag(Tag slot) const
64 {
65         map<Tag, Tag>::const_iterator i = uniform_slots.find(slot);
66         if(i==uniform_slots.end())
67                 return Tag();
68         return i->second;
69 }
70
71 void RenderPass::set_material(const Material *mat)
72 {
73         material = mat;
74         maybe_create_material_shader();
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::set_back_faces(bool bf)
129 {
130         back_faces = bf;
131 }
132
133 void RenderPass::set_receive_shadows(bool rs)
134 {
135         receive_shadows = rs;
136 }
137
138 void RenderPass::apply(Renderer &renderer) const
139 {
140         for(vector<TextureSlot>::const_iterator i=textures.begin(); i!=textures.end(); ++i)
141                 renderer.set_texture(i->tag, i->texture, i->sampler);
142         renderer.set_shader_program(shprog, shdata.get());
143         if(material)
144                 renderer.add_shader_data(material->get_shader_data());
145         renderer.set_reverse_winding(back_faces);
146 }
147
148 void RenderPass::set_debug_name(const string &name)
149 {
150 #ifdef DEBUG
151         if(shdata.refcount()==1)
152                 shdata->set_debug_name(name+" [UBO]");
153 #else
154         (void)name;
155 #endif
156 }
157
158
159 DataFile::Loader::ActionMap RenderPass::Loader::shared_actions;
160
161 RenderPass::Loader::Loader(RenderPass &p):
162         DataFile::CollectionObjectLoader<RenderPass>(p, 0)
163 {
164         set_actions(shared_actions);
165 }
166
167 RenderPass::Loader::Loader(RenderPass &p, Collection &c):
168         DataFile::CollectionObjectLoader<RenderPass>(p, &c)
169 {
170         set_actions(shared_actions);
171 }
172
173 void RenderPass::Loader::init_actions()
174 {
175         add("shader",   &Loader::shader);
176         add("image_based_lighting", &RenderPass::image_based_lighting);
177         add("material", &Loader::material_inline);
178         add("material", &Loader::material);
179         add("material_slot", &RenderPass::material_slot);
180         add("back_faces",&RenderPass::back_faces);
181         add("receive_shadows", &RenderPass::receive_shadows);
182         add("texture", &Loader::texture);
183         add("uniforms", &Loader::uniforms);
184         add("uniform_slot", &Loader::uniform_slot);
185         add("uniform_slot", &Loader::uniform_slot2);
186
187         // Deprecated
188         add("texunit",  &Loader::texunit);
189         add("texunit",  &Loader::texture);
190         add("texunit",  &Loader::texunit_named);
191 }
192
193 void RenderPass::Loader::set_inline_base_name(const string &n)
194 {
195         inline_base_name = n;
196 }
197
198 void RenderPass::Loader::finish()
199 {
200         if(obj.material)
201                 obj.maybe_create_material_shader();
202 }
203
204 // Temporary compatibility feature
205 string RenderPass::Loader::get_shader_name(const string &n)
206 {
207         if(n.size()>=5 && !n.compare(n.size()-5, 5, ".glsl"))
208         {
209                 IO::print(IO::cerr, "Warning: Loading module '%s' as shader is deprecated\n", n);
210                 return n+".shader";
211         }
212         return n;
213 }
214
215 void RenderPass::Loader::material_inline()
216 {
217         Material::GenericLoader ldr(coll);
218         load_sub_with(ldr);
219         RefPtr<Material> mat = ldr.get_material();
220         get_collection().add(inline_base_name+".mat", mat.get());
221         obj.material = mat.release();
222         obj.set_material_textures();
223 }
224
225 void RenderPass::Loader::material(const string &name)
226 {
227         obj.material = &get_collection().get<Material>(name);
228         obj.set_material_textures();
229 }
230
231 void RenderPass::Loader::shader(const string &n)
232 {
233         obj.shprog = &get_collection().get<Program>(get_shader_name(n));
234         obj.shprog_from_material = false;
235         if(obj.shdata)
236                 obj.shdata = new ProgramData(*obj.shdata, obj.shprog);
237 }
238
239 void RenderPass::Loader::texture(const string &n)
240 {
241         vector<TextureSlot>::iterator i = find_member(obj.textures, Tag(n), &TextureSlot::tag);
242         if(i==obj.textures.end())
243         {
244                 obj.textures.push_back(TextureSlot(n));
245                 i = obj.textures.end()-1;
246         }
247         TextureSlot::Loader ldr(*i, n, coll);
248         load_sub_with(ldr);
249 }
250
251 void RenderPass::Loader::texunit(unsigned)
252 {
253         IO::print(IO::cerr, "Warning: specifying textures by unit number is deprecated and may not produce expected results");
254         string name;
255         if(obj.shprog)
256         {
257                 const vector<Program::UniformInfo> &uniforms = obj.shprog->get_uniforms();
258                 for(vector<Program::UniformInfo>::const_iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
259                         if(is_image(i->type) && i->binding>=0)
260                         {
261                                 if(!name.empty())
262                                 {
263                                         name.clear();
264                                         break;
265                                 }
266                                 name = i->name;
267                         }
268         }
269
270         if(name.empty())
271                 throw runtime_error("Could not determine name for texture");
272
273         texture(name);
274 }
275
276 void RenderPass::Loader::texunit_named(unsigned, const string &n)
277 {
278         texture(n);
279 }
280
281 void RenderPass::Loader::uniforms()
282 {
283         if(!obj.shprog || obj.shprog_from_material)
284                 throw runtime_error("Shader is required for uniforms");
285         if(!obj.shdata)
286                 obj.shdata = new ProgramData(obj.shprog);
287         else if(obj.shdata.refcount()>1)
288                 obj.shdata = new ProgramData(*obj.shdata);
289         load_sub(*obj.shdata);
290 }
291
292 void RenderPass::Loader::uniform_slot(const string &name)
293 {
294         uniform_slot2(name, name);
295 }
296
297 void RenderPass::Loader::uniform_slot2(const string &name, const string &slot)
298 {
299         obj.uniform_slots[slot] = name;
300 }
301
302
303 RenderPass::TextureSlot::Loader::Loader(TextureSlot &ts, const string &an, Collection *c):
304         CollectionObjectLoader<TextureSlot>(ts, c),
305         auto_slot_name(an)
306 {
307         add("sampler", &TextureSlot::sampler);
308         add("slot", &Loader::slot_auto);
309         add("slot", &TextureSlot::slot_name);
310         add("texture", &TextureSlot::texture);
311 }
312
313 void RenderPass::TextureSlot::Loader::slot_auto()
314 {
315         obj.slot_name = auto_slot_name;
316 }
317
318 } // namespace GL
319 } // namespace Msp