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)
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(DataFile::Collection *coll)
38 if(shprog && !shprog_from_material)
41 map<string, int> extra_spec;
43 extra_spec["use_shadow_map"] = true;
47 shprog = material->create_compatible_shader(*coll, extra_spec);
51 throw invalid_operation("RenderPass::maybe_create_material_shader");
54 shdata = new ProgramData(*shdata, shprog.get());
56 shprog_from_material = true;
59 void RenderPass::set_shader_program(const Program *prog, const ProgramData *data)
63 shprog_from_material = false;
64 shdata = (data ? new ProgramData(*data) : 0);
67 Tag RenderPass::get_slotted_uniform_tag(Tag slot) const
69 map<Tag, Tag>::const_iterator i = uniform_slots.find(slot);
70 if(i==uniform_slots.end())
75 void RenderPass::set_material(const Material *mat, DataFile::Collection *coll)
79 maybe_create_material_shader(coll);
80 set_material_textures();
83 void RenderPass::set_texture(Tag tag, const Texture *tex, const Sampler *samp)
85 vector<TextureSlot>::iterator i = find_member(textures, tag, &TextureSlot::tag);
88 textures.push_back(TextureSlot(tag));
96 Tag RenderPass::get_texture_tag(const string &slot) const
98 vector<TextureSlot>::const_iterator i = find_member(textures, slot, &TextureSlot::slot_name);
99 return (i!=textures.end() ? i->tag : Tag());
102 void RenderPass::set_texture(unsigned index, const Texture *tex, const Sampler *samp)
105 throw invalid_operation("RenderPass::set_texture");
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);
114 const vector<Tag> &tags = shdata->get_uniform_tags();
115 for(vector<Tag>::const_iterator i=tags.begin(); i!=tags.end(); ++i)
117 vector<Program::UniformInfo>::const_iterator j = find_member(uniforms, *i, &Program::UniformInfo::tag);
118 if(j==uniforms.end() || !is_image(j->type))
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);
127 int RenderPass::get_texture_index(const string &n) const
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);
133 void RenderPass::set_back_faces(bool bf)
138 void RenderPass::set_receive_shadows(bool rs)
140 receive_shadows = rs;
143 void RenderPass::apply(Renderer &renderer) const
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);
153 DataFile::Loader::ActionMap RenderPass::Loader::shared_actions;
155 RenderPass::Loader::Loader(RenderPass &p):
156 DataFile::CollectionObjectLoader<RenderPass>(p, 0)
158 set_actions(shared_actions);
161 RenderPass::Loader::Loader(RenderPass &p, Collection &c):
162 DataFile::CollectionObjectLoader<RenderPass>(p, &c)
164 set_actions(shared_actions);
167 void RenderPass::Loader::init_actions()
169 add("shader", &Loader::shader);
170 add("material", &Loader::material_inline);
171 add("material", &Loader::material);
172 add("material_slot", &RenderPass::material_slot);
173 add("back_faces",&RenderPass::back_faces);
174 add("receive_shadows", &RenderPass::receive_shadows);
175 add("texture", &Loader::texture);
176 add("uniforms", &Loader::uniforms);
177 add("uniform_slot", &Loader::uniform_slot);
178 add("uniform_slot", &Loader::uniform_slot2);
181 add("texunit", &Loader::texunit);
182 add("texunit", &Loader::texture);
183 add("texunit", &Loader::texunit_named);
186 void RenderPass::Loader::finish()
189 obj.maybe_create_material_shader(coll);
192 // Temporary compatibility feature
193 string RenderPass::Loader::get_shader_name(const string &n)
195 if(n.size()>=5 && !n.compare(n.size()-5, 5, ".glsl"))
197 IO::print(IO::cerr, "Warning: Loading module '%s' as shader is deprecated\n", n);
203 void RenderPass::Loader::material_inline()
205 Material::GenericLoader ldr(coll);
207 obj.material = ldr.get_material();
208 obj.set_material_textures();
211 void RenderPass::Loader::material(const string &name)
213 obj.material = &get_collection().get<Material>(name);
215 obj.set_material_textures();
218 void RenderPass::Loader::shader(const string &n)
220 obj.shprog = &get_collection().get<Program>(get_shader_name(n));
222 obj.shprog_from_material = false;
224 obj.shdata = new ProgramData(*obj.shdata, obj.shprog.get());
227 void RenderPass::Loader::texture(const string &n)
229 vector<TextureSlot>::iterator i = find_member(obj.textures, Tag(n), &TextureSlot::tag);
230 if(i==obj.textures.end())
232 obj.textures.push_back(TextureSlot(n));
233 i = obj.textures.end()-1;
235 TextureSlot::Loader ldr(*i, n, coll);
239 void RenderPass::Loader::texunit(unsigned)
241 IO::print(IO::cerr, "Warning: specifying textures by unit number is deprecated and may not produce expected results");
245 const vector<Program::UniformInfo> &uniforms = obj.shprog->get_uniforms();
246 for(vector<Program::UniformInfo>::const_iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
247 if(is_image(i->type) && i->binding>=0)
259 throw runtime_error("Could not determine name for texture");
264 void RenderPass::Loader::texunit_named(unsigned, const string &n)
269 void RenderPass::Loader::uniforms()
271 if(!obj.shprog || obj.shprog_from_material)
272 throw runtime_error("Shader is required for uniforms");
274 obj.shdata = new ProgramData(obj.shprog.get());
275 else if(obj.shdata.refcount()>1)
276 obj.shdata = new ProgramData(*obj.shdata);
277 load_sub(*obj.shdata);
280 void RenderPass::Loader::uniform_slot(const string &name)
282 uniform_slot2(name, name);
285 void RenderPass::Loader::uniform_slot2(const string &name, const string &slot)
287 obj.uniform_slots[slot] = name;
291 RenderPass::TextureSlot::Loader::Loader(TextureSlot &ts, const string &an, Collection *c):
292 CollectionObjectLoader<TextureSlot>(ts, c),
295 add("sampler", &TextureSlot::sampler);
296 add("slot", &Loader::slot_auto);
297 add("slot", &TextureSlot::slot_name);
298 add("texture", &TextureSlot::texture);
301 void RenderPass::TextureSlot::Loader::slot_auto()
303 obj.slot_name = auto_slot_name;