]> git.tdb.fi Git - libs/gl.git/blob - source/materials/renderpass.cpp
Move the receive_shadows flag to 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         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_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_material(material.get());
143         renderer.set_shader_program(shprog.get(), shdata.get());
144         renderer.set_reverse_winding(back_faces);
145 }
146
147
148 DataFile::Loader::ActionMap RenderPass::Loader::shared_actions;
149
150 RenderPass::Loader::Loader(RenderPass &p):
151         DataFile::CollectionObjectLoader<RenderPass>(p, 0)
152 {
153         set_actions(shared_actions);
154 }
155
156 RenderPass::Loader::Loader(RenderPass &p, Collection &c):
157         DataFile::CollectionObjectLoader<RenderPass>(p, &c)
158 {
159         set_actions(shared_actions);
160 }
161
162 void RenderPass::Loader::init_actions()
163 {
164         add("shader",   &Loader::shader);
165         add("material", &Loader::material_inline);
166         add("material", &Loader::material);
167         add("material_slot", &RenderPass::material_slot);
168         add("back_faces",&RenderPass::back_faces);
169         add("receive_shadows", &RenderPass::receive_shadows);
170         add("texture", &Loader::texture);
171         add("uniforms", &Loader::uniforms);
172         add("uniform_slot", &Loader::uniform_slot);
173         add("uniform_slot", &Loader::uniform_slot2);
174
175         // Deprecated
176         add("texunit",  &Loader::texunit);
177         add("texunit",  &Loader::texture);
178         add("texunit",  &Loader::texunit_named);
179 }
180
181 void RenderPass::Loader::finish()
182 {
183         if(obj.material)
184                 obj.maybe_create_material_shader(coll);
185 }
186
187 // Temporary compatibility feature
188 string RenderPass::Loader::get_shader_name(const string &n)
189 {
190         if(n.size()>=5 && !n.compare(n.size()-5, 5, ".glsl"))
191         {
192                 IO::print(IO::cerr, "Warning: Loading module '%s' as shader is deprecated\n", n);
193                 return n+".shader";
194         }
195         return n;
196 }
197
198 void RenderPass::Loader::material_inline()
199 {
200         Material::GenericLoader ldr(coll);
201         load_sub_with(ldr);
202         obj.material = ldr.get_material();
203         obj.set_material_textures();
204 }
205
206 void RenderPass::Loader::material(const string &name)
207 {
208         obj.material = &get_collection().get<Material>(name);
209         obj.material.keep();
210         obj.set_material_textures();
211 }
212
213 void RenderPass::Loader::shader(const string &n)
214 {
215         obj.shprog = &get_collection().get<Program>(get_shader_name(n));
216         obj.shprog.keep();
217         obj.shprog_from_material = false;
218         if(obj.shdata)
219                 obj.shdata = new ProgramData(*obj.shdata, obj.shprog.get());
220 }
221
222 void RenderPass::Loader::texture(const string &n)
223 {
224         vector<TextureSlot>::iterator i = find_member(obj.textures, Tag(n), &TextureSlot::tag);
225         if(i==obj.textures.end())
226         {
227                 obj.textures.push_back(TextureSlot(n));
228                 i = obj.textures.end()-1;
229         }
230         TextureSlot::Loader ldr(*i, n, coll);
231         load_sub_with(ldr);
232 }
233
234 void RenderPass::Loader::texunit(unsigned)
235 {
236         IO::print(IO::cerr, "Warning: specifying textures by unit number is deprecated and may not produce expected results");
237         string name;
238         if(obj.shprog)
239         {
240                 const vector<Program::UniformInfo> &uniforms = obj.shprog->get_uniforms();
241                 for(vector<Program::UniformInfo>::const_iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
242                         if(is_image(i->type) && i->binding>=0)
243                         {
244                                 if(!name.empty())
245                                 {
246                                         name.clear();
247                                         break;
248                                 }
249                                 name = i->name;
250                         }
251         }
252
253         if(name.empty())
254                 throw runtime_error("Could not determine name for texture");
255
256         texture(name);
257 }
258
259 void RenderPass::Loader::texunit_named(unsigned, const string &n)
260 {
261         texture(n);
262 }
263
264 void RenderPass::Loader::uniforms()
265 {
266         if(!obj.shprog || obj.shprog_from_material)
267                 throw runtime_error("Shader is required for uniforms");
268         if(!obj.shdata)
269                 obj.shdata = new ProgramData(obj.shprog.get());
270         else if(obj.shdata.refcount()>1)
271                 obj.shdata = new ProgramData(*obj.shdata);
272         load_sub(*obj.shdata);
273 }
274
275 void RenderPass::Loader::uniform_slot(const string &name)
276 {
277         uniform_slot2(name, name);
278 }
279
280 void RenderPass::Loader::uniform_slot2(const string &name, const string &slot)
281 {
282         obj.uniform_slots[slot] = name;
283 }
284
285
286 RenderPass::TextureSlot::Loader::Loader(TextureSlot &ts, const string &an, Collection *c):
287         CollectionObjectLoader<TextureSlot>(ts, c),
288         auto_slot_name(an)
289 {
290         add("sampler", &TextureSlot::sampler);
291         add("slot", &Loader::slot_auto);
292         add("slot", &TextureSlot::slot_name);
293         add("texture", &TextureSlot::texture);
294 }
295
296 void RenderPass::TextureSlot::Loader::slot_auto()
297 {
298         obj.slot_name = auto_slot_name;
299 }
300
301 } // namespace GL
302 } // namespace Msp