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