1 #include <msp/core/algorithm.h>
2 #include <msp/datafile/collection.h>
3 #include <msp/io/print.h>
4 #include <msp/strings/format.h>
6 #include "renderpass.h"
8 #include "programdata.h"
11 #include "texture2d.h"
12 #include "texturing.h"
20 RenderPass::RenderPass():
22 shprog_from_material(false),
26 receive_shadows(false),
27 image_based_lighting(false)
30 void RenderPass::set_material_textures()
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());
37 void RenderPass::maybe_create_material_shader()
39 if(shprog && !shprog_from_material)
42 map<string, int> extra_spec;
44 extra_spec["use_shadow_map"] = true;
45 if(image_based_lighting)
46 extra_spec["use_image_based_lighting"] = true;
48 shprog = material->create_compatible_shader(extra_spec);
52 shdata = new ProgramData(*shdata, shprog.get());
54 shprog_from_material = true;
57 void RenderPass::set_shader_program(const Program *prog, const ProgramData *data)
61 shprog_from_material = false;
62 shdata = (data ? new ProgramData(*data) : 0);
65 Tag RenderPass::get_slotted_uniform_tag(Tag slot) const
67 map<Tag, Tag>::const_iterator i = uniform_slots.find(slot);
68 if(i==uniform_slots.end())
73 void RenderPass::set_material(const Material *mat)
77 maybe_create_material_shader();
78 set_material_textures();
81 void RenderPass::set_texture(Tag tag, const Texture *tex, const Sampler *samp)
83 vector<TextureSlot>::iterator i = find_member(textures, tag, &TextureSlot::tag);
86 textures.push_back(TextureSlot(tag));
94 Tag RenderPass::get_texture_tag(const string &slot) const
96 vector<TextureSlot>::const_iterator i = find_member(textures, slot, &TextureSlot::slot_name);
97 return (i!=textures.end() ? i->tag : Tag());
100 void RenderPass::set_texture(unsigned index, const Texture *tex, const Sampler *samp)
103 throw invalid_operation("RenderPass::set_texture");
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);
112 const vector<Tag> &tags = shdata->get_uniform_tags();
113 for(vector<Tag>::const_iterator i=tags.begin(); i!=tags.end(); ++i)
115 vector<Program::UniformInfo>::const_iterator j = find_member(uniforms, *i, &Program::UniformInfo::tag);
116 if(j==uniforms.end() || !is_image(j->type))
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);
125 int RenderPass::get_texture_index(const string &n) const
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);
131 void RenderPass::set_back_faces(bool bf)
136 void RenderPass::set_receive_shadows(bool rs)
138 receive_shadows = rs;
141 void RenderPass::apply(Renderer &renderer) const
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);
150 void RenderPass::set_debug_name(const string &name)
153 if(shdata.refcount()==1)
154 shdata->set_debug_name(name+" [UBO]");
161 DataFile::Loader::ActionMap RenderPass::Loader::shared_actions;
163 RenderPass::Loader::Loader(RenderPass &p):
164 DataFile::CollectionObjectLoader<RenderPass>(p, 0)
166 set_actions(shared_actions);
169 RenderPass::Loader::Loader(RenderPass &p, Collection &c):
170 DataFile::CollectionObjectLoader<RenderPass>(p, &c)
172 set_actions(shared_actions);
175 void RenderPass::Loader::init_actions()
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);
190 add("texunit", &Loader::texunit);
191 add("texunit", &Loader::texture);
192 add("texunit", &Loader::texunit_named);
195 void RenderPass::Loader::finish()
198 obj.maybe_create_material_shader();
201 // Temporary compatibility feature
202 string RenderPass::Loader::get_shader_name(const string &n)
204 if(n.size()>=5 && !n.compare(n.size()-5, 5, ".glsl"))
206 IO::print(IO::cerr, "Warning: Loading module '%s' as shader is deprecated\n", n);
212 void RenderPass::Loader::material_inline()
214 Material::GenericLoader ldr(coll);
216 obj.material = ldr.get_material();
217 obj.set_material_textures();
220 void RenderPass::Loader::material(const string &name)
222 obj.material = &get_collection().get<Material>(name);
224 obj.set_material_textures();
227 void RenderPass::Loader::shader(const string &n)
229 obj.shprog = &get_collection().get<Program>(get_shader_name(n));
231 obj.shprog_from_material = false;
233 obj.shdata = new ProgramData(*obj.shdata, obj.shprog.get());
236 void RenderPass::Loader::texture(const string &n)
238 vector<TextureSlot>::iterator i = find_member(obj.textures, Tag(n), &TextureSlot::tag);
239 if(i==obj.textures.end())
241 obj.textures.push_back(TextureSlot(n));
242 i = obj.textures.end()-1;
244 TextureSlot::Loader ldr(*i, n, coll);
248 void RenderPass::Loader::texunit(unsigned)
250 IO::print(IO::cerr, "Warning: specifying textures by unit number is deprecated and may not produce expected results");
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)
268 throw runtime_error("Could not determine name for texture");
273 void RenderPass::Loader::texunit_named(unsigned, const string &n)
278 void RenderPass::Loader::uniforms()
280 if(!obj.shprog || obj.shprog_from_material)
281 throw runtime_error("Shader is required for uniforms");
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);
289 void RenderPass::Loader::uniform_slot(const string &name)
291 uniform_slot2(name, name);
294 void RenderPass::Loader::uniform_slot2(const string &name, const string &slot)
296 obj.uniform_slots[slot] = name;
300 RenderPass::TextureSlot::Loader::Loader(TextureSlot &ts, const string &an, Collection *c):
301 CollectionObjectLoader<TextureSlot>(ts, c),
304 add("sampler", &TextureSlot::sampler);
305 add("slot", &Loader::slot_auto);
306 add("slot", &TextureSlot::slot_name);
307 add("texture", &TextureSlot::texture);
310 void RenderPass::TextureSlot::Loader::slot_auto()
312 obj.slot_name = auto_slot_name;