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"
19 RenderPass::RenderPass():
21 shprog_from_material(false),
25 receive_shadows(false),
26 image_based_lighting(false)
29 void RenderPass::set_material_textures()
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());
36 void RenderPass::maybe_create_material_shader()
38 if(shprog && !shprog_from_material)
41 map<string, int> extra_spec;
43 extra_spec["use_shadow_map"] = true;
44 if(image_based_lighting)
45 extra_spec["use_image_based_lighting"] = true;
47 shprog = material->create_compatible_shader(extra_spec);
50 shdata = new ProgramData(*shdata, shprog);
52 shprog_from_material = true;
55 void RenderPass::set_shader_program(const Program *prog, const ProgramData *data)
58 shprog_from_material = false;
59 shdata = (data ? new ProgramData(*data) : 0);
62 Tag RenderPass::get_slotted_uniform_tag(Tag slot) const
64 map<Tag, Tag>::const_iterator i = uniform_slots.find(slot);
65 if(i==uniform_slots.end())
70 void RenderPass::set_material(const Material *mat)
73 maybe_create_material_shader();
74 set_material_textures();
77 void RenderPass::set_texture(Tag tag, const Texture *tex, const Sampler *samp)
79 vector<TextureSlot>::iterator i = find_member(textures, tag, &TextureSlot::tag);
82 textures.push_back(TextureSlot(tag));
90 Tag RenderPass::get_texture_tag(const string &slot) const
92 vector<TextureSlot>::const_iterator i = find_member(textures, slot, &TextureSlot::slot_name);
93 return (i!=textures.end() ? i->tag : Tag());
96 void RenderPass::set_texture(unsigned index, const Texture *tex, const Sampler *samp)
99 throw invalid_operation("RenderPass::set_texture");
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);
108 const vector<Tag> &tags = shdata->get_uniform_tags();
109 for(vector<Tag>::const_iterator i=tags.begin(); i!=tags.end(); ++i)
111 vector<Program::UniformInfo>::const_iterator j = find_member(uniforms, *i, &Program::UniformInfo::tag);
112 if(j==uniforms.end() || !is_image(j->type))
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);
121 int RenderPass::get_texture_index(const string &n) const
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);
127 void RenderPass::set_back_faces(bool bf)
132 void RenderPass::set_receive_shadows(bool rs)
134 receive_shadows = rs;
137 void RenderPass::apply(Renderer &renderer) const
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_shader_program(shprog, shdata.get());
143 renderer.add_shader_data(material->get_shader_data());
144 renderer.set_reverse_winding(back_faces);
147 void RenderPass::set_debug_name(const string &name)
150 if(shdata.refcount()==1)
151 shdata->set_debug_name(name+" [UBO]");
158 DataFile::Loader::ActionMap RenderPass::Loader::shared_actions;
160 RenderPass::Loader::Loader(RenderPass &p):
161 DataFile::CollectionObjectLoader<RenderPass>(p, 0)
163 set_actions(shared_actions);
166 RenderPass::Loader::Loader(RenderPass &p, Collection &c):
167 DataFile::CollectionObjectLoader<RenderPass>(p, &c)
169 set_actions(shared_actions);
172 void RenderPass::Loader::init_actions()
174 add("shader", &Loader::shader);
175 add("image_based_lighting", &RenderPass::image_based_lighting);
176 add("material", &Loader::material_inline);
177 add("material", &Loader::material);
178 add("material_slot", &RenderPass::material_slot);
179 add("back_faces",&RenderPass::back_faces);
180 add("receive_shadows", &RenderPass::receive_shadows);
181 add("texture", &Loader::texture);
182 add("uniforms", &Loader::uniforms);
183 add("uniform_slot", &Loader::uniform_slot);
184 add("uniform_slot", &Loader::uniform_slot2);
187 add("texunit", &Loader::texunit);
188 add("texunit", &Loader::texture);
189 add("texunit", &Loader::texunit_named);
192 void RenderPass::Loader::set_inline_base_name(const string &n)
194 inline_base_name = n;
197 void RenderPass::Loader::finish()
200 obj.maybe_create_material_shader();
203 // Temporary compatibility feature
204 string RenderPass::Loader::get_shader_name(const string &n)
206 if(n.size()>=5 && !n.compare(n.size()-5, 5, ".glsl"))
208 IO::print(IO::cerr, "Warning: Loading module '%s' as shader is deprecated\n", n);
214 void RenderPass::Loader::material_inline()
216 Material::GenericLoader ldr(coll);
218 RefPtr<Material> mat = ldr.get_material();
219 get_collection().add(inline_base_name+".mat", mat.get());
220 obj.material = mat.release();
221 obj.set_material_textures();
224 void RenderPass::Loader::material(const string &name)
226 obj.material = &get_collection().get<Material>(name);
227 obj.set_material_textures();
230 void RenderPass::Loader::shader(const string &n)
232 obj.shprog = &get_collection().get<Program>(get_shader_name(n));
233 obj.shprog_from_material = false;
235 obj.shdata = new ProgramData(*obj.shdata, obj.shprog);
238 void RenderPass::Loader::texture(const string &n)
240 vector<TextureSlot>::iterator i = find_member(obj.textures, Tag(n), &TextureSlot::tag);
241 if(i==obj.textures.end())
243 obj.textures.push_back(TextureSlot(n));
244 i = obj.textures.end()-1;
246 TextureSlot::Loader ldr(*i, n, coll);
250 void RenderPass::Loader::texunit(unsigned)
252 IO::print(IO::cerr, "Warning: specifying textures by unit number is deprecated and may not produce expected results");
256 const vector<Program::UniformInfo> &uniforms = obj.shprog->get_uniforms();
257 for(vector<Program::UniformInfo>::const_iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
258 if(is_image(i->type) && i->binding>=0)
270 throw runtime_error("Could not determine name for texture");
275 void RenderPass::Loader::texunit_named(unsigned, const string &n)
280 void RenderPass::Loader::uniforms()
282 if(!obj.shprog || obj.shprog_from_material)
283 throw runtime_error("Shader is required for uniforms");
285 obj.shdata = new ProgramData(obj.shprog);
286 else if(obj.shdata.refcount()>1)
287 obj.shdata = new ProgramData(*obj.shdata);
288 load_sub(*obj.shdata);
291 void RenderPass::Loader::uniform_slot(const string &name)
293 uniform_slot2(name, name);
296 void RenderPass::Loader::uniform_slot2(const string &name, const string &slot)
298 obj.uniform_slots[slot] = name;
302 RenderPass::TextureSlot::Loader::Loader(TextureSlot &ts, const string &an, Collection *c):
303 CollectionObjectLoader<TextureSlot>(ts, c),
306 add("sampler", &TextureSlot::sampler);
307 add("slot", &Loader::slot_auto);
308 add("slot", &TextureSlot::slot_name);
309 add("texture", &TextureSlot::texture);
312 void RenderPass::TextureSlot::Loader::slot_auto()
314 obj.slot_name = auto_slot_name;