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 void RenderMethod::set_material_textures()
22 const Tag *material_texture_tags = material->get_texture_tags();
23 for(const Tag *tag=material_texture_tags; tag->id; ++tag)
24 set_texture(*tag, material->get_texture(*tag), material->get_sampler(*tag));
27 void RenderMethod::maybe_create_material_shader()
29 if(!material || (shprog && !shprog_from_material))
32 map<string, int> extra_spec;
34 extra_spec["use_shadow_map"] = true;
35 if(image_based_lighting)
36 extra_spec["use_image_based_lighting"] = true;
38 shprog = material->create_compatible_shader(extra_spec);
42 RefPtr<ProgramData> old_shdata = shdata;
43 shdata = new ProgramData(shprog);
44 shdata->copy_uniforms(*old_shdata);
47 shprog_from_material = true;
50 void RenderMethod::set_shader_program(const Program *prog, const ProgramData *data)
53 shprog_from_material = false;
56 shdata = new ProgramData;
57 shdata->copy_uniforms(*data);
61 maybe_create_material_shader();
64 Tag RenderMethod::get_slotted_uniform_tag(Tag slot) const
66 auto i = uniform_slots.find(slot);
67 if(i==uniform_slots.end())
72 void RenderMethod::set_material(const Material *mat)
75 maybe_create_material_shader();
76 set_material_textures();
79 void RenderMethod::set_texture(Tag tag, const Texture *tex, const Sampler *samp)
81 auto i = find_member(textures, tag, &TextureSlot::tag);
84 textures.emplace_back(tag);
92 Tag RenderMethod::get_texture_tag(const string &slot) const
94 auto i = find_member(textures, slot, &TextureSlot::slot_name);
95 return (i!=textures.end() ? i->tag : Tag());
98 void RenderMethod::set_face_cull(CullMode fc)
103 void RenderMethod::set_blend(const Blend &b)
108 void RenderMethod::set_receive_shadows(bool rs)
110 receive_shadows = rs;
111 maybe_create_material_shader();
114 void RenderMethod::set_image_based_lighting(bool ibl)
116 image_based_lighting = ibl;
117 maybe_create_material_shader();
120 void RenderMethod::apply(Renderer &renderer) const
122 for(const TextureSlot &t: textures)
123 renderer.set_texture(t.tag, t.texture, t.sampler);
124 renderer.set_shader_program(shprog, shdata.get());
126 renderer.add_shader_data(material->get_shader_data());
127 renderer.set_face_cull(face_cull);
128 renderer.set_blend(&blend);
131 void RenderMethod::set_debug_name(const string &name)
134 if(shdata.refcount()==1)
135 shdata->set_debug_name(name+" [UBO]");
142 DataFile::Loader::ActionMap RenderMethod::Loader::shared_actions;
144 RenderMethod::Loader::Loader(RenderMethod &p, Collection &c):
145 DataFile::CollectionObjectLoader<RenderMethod>(p, &c)
147 set_actions(shared_actions);
150 void RenderMethod::Loader::init_actions()
152 add("blend", &Loader::blend);
153 add("blend", &Loader::blend_factors);
154 add("face_cull", &RenderMethod::face_cull);
155 add("shader", &Loader::shader);
156 add("image_based_lighting", &RenderMethod::image_based_lighting);
157 add("material", &Loader::material_inline);
158 add("material", &Loader::material);
159 add("material_slot", &RenderMethod::material_slot);
160 add("receive_shadows", &RenderMethod::receive_shadows);
161 add("texture", &Loader::texture);
162 add("uniforms", &Loader::uniforms);
163 add("uniform_slot", &Loader::uniform_slot);
164 add("uniform_slot", &Loader::uniform_slot2);
167 void RenderMethod::Loader::set_inline_base_name(const string &n)
169 inline_base_name = n;
172 void RenderMethod::Loader::finish()
175 obj.maybe_create_material_shader();
178 void RenderMethod::Loader::blend()
183 void RenderMethod::Loader::blend_factors(BlendFactor src, BlendFactor dest)
185 obj.blend = Blend(src, dest);
188 void RenderMethod::Loader::material_inline()
190 Material::GenericLoader ldr(get_collection());
192 obj.material = ldr.store_object(get_collection(), inline_base_name+".mat");
193 obj.set_material_textures();
196 void RenderMethod::Loader::material(const string &name)
198 obj.material = &get_collection().get<Material>(name);
199 obj.set_material_textures();
202 void RenderMethod::Loader::shader(const string &n)
204 obj.shprog = &get_collection().get<Program>(n);
205 obj.shprog_from_material = false;
208 RefPtr<ProgramData> old_shdata = obj.shdata;
209 obj.shdata = new ProgramData(obj.shprog);
210 obj.shdata->copy_uniforms(*old_shdata);
214 void RenderMethod::Loader::texture(const string &n)
216 auto i = find_member(obj.textures, Tag(n), &TextureSlot::tag);
217 if(i==obj.textures.end())
219 obj.textures.emplace_back(n);
220 i = obj.textures.end()-1;
222 TextureSlot::Loader ldr(*i, n, coll);
226 void RenderMethod::Loader::uniforms()
228 if(!obj.shprog || obj.shprog_from_material)
229 throw runtime_error("Shader is required for uniforms");
231 obj.shdata = new ProgramData(obj.shprog);
232 else if(obj.shdata.refcount()>1)
234 RefPtr<ProgramData> old_shdata = obj.shdata;
235 obj.shdata = new ProgramData;
236 obj.shdata->copy_uniforms(*old_shdata);
238 load_sub(*obj.shdata);
241 void RenderMethod::Loader::uniform_slot(const string &name)
243 uniform_slot2(name, name);
246 void RenderMethod::Loader::uniform_slot2(const string &name, const string &slot)
248 obj.uniform_slots[slot] = name;
252 RenderMethod::TextureSlot::Loader::Loader(TextureSlot &ts, const string &an, Collection *c):
253 CollectionObjectLoader<TextureSlot>(ts, c),
256 add("sampler", &TextureSlot::sampler);
257 add("slot", &Loader::slot_auto);
258 add("slot", &TextureSlot::slot_name);
259 add("texture", &TextureSlot::texture);
262 void RenderMethod::TextureSlot::Loader::slot_auto()
264 obj.slot_name = auto_slot_name;