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