1 #include <msp/core/algorithm.h>
2 #include <msp/datafile/collection.h>
3 #include <msp/strings/format.h>
5 #include "rendermethod.h"
7 #include "programdata.h"
10 #include "texture2d.h"
17 RenderMethod::RenderMethod():
19 shprog_from_material(false),
23 receive_shadows(false),
24 image_based_lighting(false)
27 void RenderMethod::set_material_textures()
29 const Tag *material_texture_tags = material->get_texture_tags();
30 for(const Tag *tag=material_texture_tags; tag->id; ++tag)
31 set_texture(*tag, material->get_texture(*tag), material->get_sampler(*tag));
34 void RenderMethod::maybe_create_material_shader()
36 if(shprog && !shprog_from_material)
39 map<string, int> extra_spec;
41 extra_spec["use_shadow_map"] = true;
42 if(image_based_lighting)
43 extra_spec["use_image_based_lighting"] = true;
45 shprog = material->create_compatible_shader(extra_spec);
48 shdata = new ProgramData(*shdata, shprog);
50 shprog_from_material = true;
53 void RenderMethod::set_shader_program(const Program *prog, const ProgramData *data)
56 shprog_from_material = false;
57 shdata = (data ? new ProgramData(*data) : 0);
60 Tag RenderMethod::get_slotted_uniform_tag(Tag slot) const
62 auto i = uniform_slots.find(slot);
63 if(i==uniform_slots.end())
68 void RenderMethod::set_material(const Material *mat)
71 maybe_create_material_shader();
72 set_material_textures();
75 void RenderMethod::set_texture(Tag tag, const Texture *tex, const Sampler *samp)
77 auto i = find_member(textures, tag, &TextureSlot::tag);
80 textures.push_back(TextureSlot(tag));
88 Tag RenderMethod::get_texture_tag(const string &slot) const
90 auto i = find_member(textures, slot, &TextureSlot::slot_name);
91 return (i!=textures.end() ? i->tag : Tag());
94 void RenderMethod::set_face_cull(CullMode fc)
99 void RenderMethod::set_receive_shadows(bool rs)
101 receive_shadows = rs;
104 void RenderMethod::apply(Renderer &renderer) const
106 for(const TextureSlot &t: textures)
107 renderer.set_texture(t.tag, t.texture, t.sampler);
108 renderer.set_shader_program(shprog, shdata.get());
110 renderer.add_shader_data(material->get_shader_data());
111 renderer.set_face_cull(face_cull);
114 void RenderMethod::set_debug_name(const string &name)
117 if(shdata.refcount()==1)
118 shdata->set_debug_name(name+" [UBO]");
125 DataFile::Loader::ActionMap RenderMethod::Loader::shared_actions;
127 RenderMethod::Loader::Loader(RenderMethod &p, Collection &c):
128 DataFile::CollectionObjectLoader<RenderMethod>(p, &c)
130 set_actions(shared_actions);
133 void RenderMethod::Loader::init_actions()
135 add("face_cull", &RenderMethod::face_cull);
136 add("shader", &Loader::shader);
137 add("image_based_lighting", &RenderMethod::image_based_lighting);
138 add("material", &Loader::material_inline);
139 add("material", &Loader::material);
140 add("material_slot", &RenderMethod::material_slot);
141 add("receive_shadows", &RenderMethod::receive_shadows);
142 add("texture", &Loader::texture);
143 add("uniforms", &Loader::uniforms);
144 add("uniform_slot", &Loader::uniform_slot);
145 add("uniform_slot", &Loader::uniform_slot2);
148 void RenderMethod::Loader::set_inline_base_name(const string &n)
150 inline_base_name = n;
153 void RenderMethod::Loader::finish()
156 obj.maybe_create_material_shader();
159 void RenderMethod::Loader::material_inline()
161 Material::GenericLoader ldr(get_collection());
163 RefPtr<Material> mat = ldr.get_object();
164 get_collection().add(inline_base_name+".mat", mat.get());
165 obj.material = mat.release();
166 obj.set_material_textures();
169 void RenderMethod::Loader::material(const string &name)
171 obj.material = &get_collection().get<Material>(name);
172 obj.set_material_textures();
175 void RenderMethod::Loader::shader(const string &n)
177 obj.shprog = &get_collection().get<Program>(n);
178 obj.shprog_from_material = false;
180 obj.shdata = new ProgramData(*obj.shdata, obj.shprog);
183 void RenderMethod::Loader::texture(const string &n)
185 auto i = find_member(obj.textures, Tag(n), &TextureSlot::tag);
186 if(i==obj.textures.end())
188 obj.textures.push_back(TextureSlot(n));
189 i = obj.textures.end()-1;
191 TextureSlot::Loader ldr(*i, n, coll);
195 void RenderMethod::Loader::uniforms()
197 if(!obj.shprog || obj.shprog_from_material)
198 throw runtime_error("Shader is required for uniforms");
200 obj.shdata = new ProgramData(obj.shprog);
201 else if(obj.shdata.refcount()>1)
202 obj.shdata = new ProgramData(*obj.shdata);
203 load_sub(*obj.shdata);
206 void RenderMethod::Loader::uniform_slot(const string &name)
208 uniform_slot2(name, name);
211 void RenderMethod::Loader::uniform_slot2(const string &name, const string &slot)
213 obj.uniform_slots[slot] = name;
217 RenderMethod::TextureSlot::Loader::Loader(TextureSlot &ts, const string &an, Collection *c):
218 CollectionObjectLoader<TextureSlot>(ts, c),
221 add("sampler", &TextureSlot::sampler);
222 add("slot", &Loader::slot_auto);
223 add("slot", &TextureSlot::slot_name);
224 add("texture", &TextureSlot::texture);
227 void RenderMethod::TextureSlot::Loader::slot_auto()
229 obj.slot_name = auto_slot_name;