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()
19 const Tag *material_texture_tags = material->get_texture_tags();
20 for(const Tag *tag=material_texture_tags; tag->id; ++tag)
21 set_texture(*tag, material->get_texture(*tag), material->get_sampler(*tag));
24 void RenderMethod::maybe_create_material_shader()
26 if(shprog && !shprog_from_material)
29 map<string, int> extra_spec;
31 extra_spec["use_shadow_map"] = true;
32 if(image_based_lighting)
33 extra_spec["use_image_based_lighting"] = true;
35 shprog = material->create_compatible_shader(extra_spec);
38 shdata = new ProgramData(*shdata, shprog);
40 shprog_from_material = true;
43 void RenderMethod::set_shader_program(const Program *prog, const ProgramData *data)
46 shprog_from_material = false;
47 shdata = (data ? new ProgramData(*data) : 0);
50 Tag RenderMethod::get_slotted_uniform_tag(Tag slot) const
52 auto i = uniform_slots.find(slot);
53 if(i==uniform_slots.end())
58 void RenderMethod::set_material(const Material *mat)
61 maybe_create_material_shader();
62 set_material_textures();
65 void RenderMethod::set_texture(Tag tag, const Texture *tex, const Sampler *samp)
67 auto i = find_member(textures, tag, &TextureSlot::tag);
70 textures.push_back(TextureSlot(tag));
78 Tag RenderMethod::get_texture_tag(const string &slot) const
80 auto i = find_member(textures, slot, &TextureSlot::slot_name);
81 return (i!=textures.end() ? i->tag : Tag());
84 void RenderMethod::set_face_cull(CullMode fc)
89 void RenderMethod::set_blend(const Blend &b)
94 void RenderMethod::set_receive_shadows(bool rs)
99 void RenderMethod::apply(Renderer &renderer) const
101 for(const TextureSlot &t: textures)
102 renderer.set_texture(t.tag, t.texture, t.sampler);
103 renderer.set_shader_program(shprog, shdata.get());
105 renderer.add_shader_data(material->get_shader_data());
106 renderer.set_face_cull(face_cull);
107 renderer.set_blend(&blend);
110 void RenderMethod::set_debug_name(const string &name)
113 if(shdata.refcount()==1)
114 shdata->set_debug_name(name+" [UBO]");
121 DataFile::Loader::ActionMap RenderMethod::Loader::shared_actions;
123 RenderMethod::Loader::Loader(RenderMethod &p, Collection &c):
124 DataFile::CollectionObjectLoader<RenderMethod>(p, &c)
126 set_actions(shared_actions);
129 void RenderMethod::Loader::init_actions()
131 add("blend", &Loader::blend);
132 add("blend", &Loader::blend_factors);
133 add("face_cull", &RenderMethod::face_cull);
134 add("shader", &Loader::shader);
135 add("image_based_lighting", &RenderMethod::image_based_lighting);
136 add("material", &Loader::material_inline);
137 add("material", &Loader::material);
138 add("material_slot", &RenderMethod::material_slot);
139 add("receive_shadows", &RenderMethod::receive_shadows);
140 add("texture", &Loader::texture);
141 add("uniforms", &Loader::uniforms);
142 add("uniform_slot", &Loader::uniform_slot);
143 add("uniform_slot", &Loader::uniform_slot2);
146 void RenderMethod::Loader::set_inline_base_name(const string &n)
148 inline_base_name = n;
151 void RenderMethod::Loader::finish()
154 obj.maybe_create_material_shader();
157 void RenderMethod::Loader::blend()
162 void RenderMethod::Loader::blend_factors(BlendFactor src, BlendFactor dest)
164 obj.blend = Blend(src, dest);
167 void RenderMethod::Loader::material_inline()
169 Material::GenericLoader ldr(get_collection());
171 obj.material = ldr.store_object(get_collection(), inline_base_name+".mat");
172 obj.set_material_textures();
175 void RenderMethod::Loader::material(const string &name)
177 obj.material = &get_collection().get<Material>(name);
178 obj.set_material_textures();
181 void RenderMethod::Loader::shader(const string &n)
183 obj.shprog = &get_collection().get<Program>(n);
184 obj.shprog_from_material = false;
186 obj.shdata = new ProgramData(*obj.shdata, obj.shprog);
189 void RenderMethod::Loader::texture(const string &n)
191 auto i = find_member(obj.textures, Tag(n), &TextureSlot::tag);
192 if(i==obj.textures.end())
194 obj.textures.push_back(TextureSlot(n));
195 i = obj.textures.end()-1;
197 TextureSlot::Loader ldr(*i, n, coll);
201 void RenderMethod::Loader::uniforms()
203 if(!obj.shprog || obj.shprog_from_material)
204 throw runtime_error("Shader is required for uniforms");
206 obj.shdata = new ProgramData(obj.shprog);
207 else if(obj.shdata.refcount()>1)
208 obj.shdata = new ProgramData(*obj.shdata);
209 load_sub(*obj.shdata);
212 void RenderMethod::Loader::uniform_slot(const string &name)
214 uniform_slot2(name, name);
217 void RenderMethod::Loader::uniform_slot2(const string &name, const string &slot)
219 obj.uniform_slots[slot] = name;
223 RenderMethod::TextureSlot::Loader::Loader(TextureSlot &ts, const string &an, Collection *c):
224 CollectionObjectLoader<TextureSlot>(ts, c),
227 add("sampler", &TextureSlot::sampler);
228 add("slot", &Loader::slot_auto);
229 add("slot", &TextureSlot::slot_name);
230 add("texture", &TextureSlot::texture);
233 void RenderMethod::TextureSlot::Loader::slot_auto()
235 obj.slot_name = auto_slot_name;