]> git.tdb.fi Git - libs/gl.git/blob - source/materials/renderpass.cpp
6e33b5d1f7256a638ab754267b0e010057e277ef
[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         shprog.keep();
50
51         if(shdata)
52                 shdata = new ProgramData(*shdata, shprog.get());
53
54         shprog_from_material = true;
55 }
56
57 void RenderPass::set_shader_program(const Program *prog, const ProgramData *data)
58 {
59         shprog = prog;
60         shprog.keep();
61         shprog_from_material = false;
62         shdata = (data ? new ProgramData(*data) : 0);
63 }
64
65 Tag RenderPass::get_slotted_uniform_tag(Tag slot) const
66 {
67         map<Tag, Tag>::const_iterator i = uniform_slots.find(slot);
68         if(i==uniform_slots.end())
69                 return Tag();
70         return i->second;
71 }
72
73 void RenderPass::set_material(const Material *mat)
74 {
75         material = mat;
76         material.keep();
77         maybe_create_material_shader();
78         set_material_textures();
79 }
80
81 void RenderPass::set_texture(Tag tag, const Texture *tex, const Sampler *samp)
82 {
83         vector<TextureSlot>::iterator i = find_member(textures, tag, &TextureSlot::tag);
84         if(i==textures.end())
85         {
86                 textures.push_back(TextureSlot(tag));
87                 i = textures.end()-1;
88         }
89         i->texture = tex;
90         if(samp)
91                 i->sampler = samp;
92 }
93
94 Tag RenderPass::get_texture_tag(const string &slot) const
95 {
96         vector<TextureSlot>::const_iterator i = find_member(textures, slot, &TextureSlot::slot_name);
97         return (i!=textures.end() ? i->tag : Tag());
98 }
99
100 void RenderPass::set_texture(unsigned index, const Texture *tex, const Sampler *samp)
101 {
102         if(!shprog)
103                 throw invalid_operation("RenderPass::set_texture");
104
105         const vector<Program::UniformInfo> &uniforms = shprog->get_uniforms();
106         for(vector<Program::UniformInfo>::const_iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
107                 if(is_image(i->type) && i->binding==static_cast<int>(index))
108                         return set_texture(i->tag, tex, samp);
109
110         if(shdata)
111         {
112                 const vector<Tag> &tags = shdata->get_uniform_tags();
113                 for(vector<Tag>::const_iterator i=tags.begin(); i!=tags.end(); ++i)
114                 {
115                         vector<Program::UniformInfo>::const_iterator j = find_member(uniforms, *i, &Program::UniformInfo::tag);
116                         if(j==uniforms.end() || !is_image(j->type))
117                                 continue;
118                         if(const Uniform1i *uni1i = dynamic_cast<const Uniform1i *>(shdata->find_uniform(*i)))
119                                 if(uni1i->get()==static_cast<int>(index))
120                                         return set_texture(*i, tex, samp);
121                 }
122         }
123 }
124
125 int RenderPass::get_texture_index(const string &n) const
126 {
127         vector<TextureSlot>::const_iterator i = find_member(textures, n, &TextureSlot::slot_name);
128         return (shprog && i!=textures.end() ? shprog->get_uniform_binding(i->tag) : -1);
129 }
130
131 void RenderPass::set_back_faces(bool bf)
132 {
133         back_faces = bf;
134 }
135
136 void RenderPass::set_receive_shadows(bool rs)
137 {
138         receive_shadows = rs;
139 }
140
141 void RenderPass::apply(Renderer &renderer) const
142 {
143         for(vector<TextureSlot>::const_iterator i=textures.begin(); i!=textures.end(); ++i)
144                 renderer.set_texture(i->tag, i->texture, i->sampler);
145         renderer.set_material(material.get());
146         renderer.set_shader_program(shprog.get(), shdata.get());
147         renderer.set_reverse_winding(back_faces);
148 }
149
150 void RenderPass::set_debug_name(const string &name)
151 {
152 #ifdef DEBUG
153         if(shdata.refcount()==1)
154                 shdata->set_debug_name(name+" [UBO]");
155 #else
156         (void)name;
157 #endif
158 }
159
160
161 DataFile::Loader::ActionMap RenderPass::Loader::shared_actions;
162
163 RenderPass::Loader::Loader(RenderPass &p):
164         DataFile::CollectionObjectLoader<RenderPass>(p, 0)
165 {
166         set_actions(shared_actions);
167 }
168
169 RenderPass::Loader::Loader(RenderPass &p, Collection &c):
170         DataFile::CollectionObjectLoader<RenderPass>(p, &c)
171 {
172         set_actions(shared_actions);
173 }
174
175 void RenderPass::Loader::init_actions()
176 {
177         add("shader",   &Loader::shader);
178         add("image_based_lighting", &RenderPass::image_based_lighting);
179         add("material", &Loader::material_inline);
180         add("material", &Loader::material);
181         add("material_slot", &RenderPass::material_slot);
182         add("back_faces",&RenderPass::back_faces);
183         add("receive_shadows", &RenderPass::receive_shadows);
184         add("texture", &Loader::texture);
185         add("uniforms", &Loader::uniforms);
186         add("uniform_slot", &Loader::uniform_slot);
187         add("uniform_slot", &Loader::uniform_slot2);
188
189         // Deprecated
190         add("texunit",  &Loader::texunit);
191         add("texunit",  &Loader::texture);
192         add("texunit",  &Loader::texunit_named);
193 }
194
195 void RenderPass::Loader::finish()
196 {
197         if(obj.material)
198                 obj.maybe_create_material_shader();
199 }
200
201 // Temporary compatibility feature
202 string RenderPass::Loader::get_shader_name(const string &n)
203 {
204         if(n.size()>=5 && !n.compare(n.size()-5, 5, ".glsl"))
205         {
206                 IO::print(IO::cerr, "Warning: Loading module '%s' as shader is deprecated\n", n);
207                 return n+".shader";
208         }
209         return n;
210 }
211
212 void RenderPass::Loader::material_inline()
213 {
214         Material::GenericLoader ldr(coll);
215         load_sub_with(ldr);
216         obj.material = ldr.get_material();
217         obj.set_material_textures();
218 }
219
220 void RenderPass::Loader::material(const string &name)
221 {
222         obj.material = &get_collection().get<Material>(name);
223         obj.material.keep();
224         obj.set_material_textures();
225 }
226
227 void RenderPass::Loader::shader(const string &n)
228 {
229         obj.shprog = &get_collection().get<Program>(get_shader_name(n));
230         obj.shprog.keep();
231         obj.shprog_from_material = false;
232         if(obj.shdata)
233                 obj.shdata = new ProgramData(*obj.shdata, obj.shprog.get());
234 }
235
236 void RenderPass::Loader::texture(const string &n)
237 {
238         vector<TextureSlot>::iterator i = find_member(obj.textures, Tag(n), &TextureSlot::tag);
239         if(i==obj.textures.end())
240         {
241                 obj.textures.push_back(TextureSlot(n));
242                 i = obj.textures.end()-1;
243         }
244         TextureSlot::Loader ldr(*i, n, coll);
245         load_sub_with(ldr);
246 }
247
248 void RenderPass::Loader::texunit(unsigned)
249 {
250         IO::print(IO::cerr, "Warning: specifying textures by unit number is deprecated and may not produce expected results");
251         string name;
252         if(obj.shprog)
253         {
254                 const vector<Program::UniformInfo> &uniforms = obj.shprog->get_uniforms();
255                 for(vector<Program::UniformInfo>::const_iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
256                         if(is_image(i->type) && i->binding>=0)
257                         {
258                                 if(!name.empty())
259                                 {
260                                         name.clear();
261                                         break;
262                                 }
263                                 name = i->name;
264                         }
265         }
266
267         if(name.empty())
268                 throw runtime_error("Could not determine name for texture");
269
270         texture(name);
271 }
272
273 void RenderPass::Loader::texunit_named(unsigned, const string &n)
274 {
275         texture(n);
276 }
277
278 void RenderPass::Loader::uniforms()
279 {
280         if(!obj.shprog || obj.shprog_from_material)
281                 throw runtime_error("Shader is required for uniforms");
282         if(!obj.shdata)
283                 obj.shdata = new ProgramData(obj.shprog.get());
284         else if(obj.shdata.refcount()>1)
285                 obj.shdata = new ProgramData(*obj.shdata);
286         load_sub(*obj.shdata);
287 }
288
289 void RenderPass::Loader::uniform_slot(const string &name)
290 {
291         uniform_slot2(name, name);
292 }
293
294 void RenderPass::Loader::uniform_slot2(const string &name, const string &slot)
295 {
296         obj.uniform_slots[slot] = name;
297 }
298
299
300 RenderPass::TextureSlot::Loader::Loader(TextureSlot &ts, const string &an, Collection *c):
301         CollectionObjectLoader<TextureSlot>(ts, c),
302         auto_slot_name(an)
303 {
304         add("sampler", &TextureSlot::sampler);
305         add("slot", &Loader::slot_auto);
306         add("slot", &TextureSlot::slot_name);
307         add("texture", &TextureSlot::texture);
308 }
309
310 void RenderPass::TextureSlot::Loader::slot_auto()
311 {
312         obj.slot_name = auto_slot_name;
313 }
314
315 } // namespace GL
316 } // namespace Msp