1 #include <msp/datafile/collection.h>
2 #include <msp/io/print.h>
3 #include <msp/strings/format.h>
6 #include "renderpass.h"
8 #include "programdata.h"
11 #include "texture2d.h"
12 #include "texturing.h"
19 RenderPass::RenderPass():
21 shprog_from_material(false),
28 RenderPass::RenderPass(const RenderPass &other):
30 shprog_from_material(other.shprog_from_material),
32 uniform_slots(other.uniform_slots),
33 material(other.material),
34 material_slot(other.material_slot),
35 texturing(other.texturing ? new Texturing(*other.texturing) : 0),
36 tex_names(other.tex_names),
37 back_faces(other.back_faces)
40 RenderPass &RenderPass::operator=(const RenderPass &other)
42 shprog = other.shprog;
43 shprog_from_material = other.shprog_from_material;
44 shdata = other.shdata;
45 uniform_slots = other.uniform_slots;
46 material = other.material;
47 material_slot = other.material_slot;
48 texturing = other.texturing ? new Texturing(*other.texturing) : 0;
49 tex_names = other.tex_names;
50 back_faces = other.back_faces;
54 RenderPass::~RenderPass()
59 void RenderPass::finalize_material(DataFile::Collection *coll)
61 maybe_create_material_shader(coll);
62 ensure_private_shader_data();
65 texturing = new Texturing;
66 material->attach_textures_to(*texturing, *shdata);
69 void RenderPass::maybe_create_material_shader(DataFile::Collection *coll)
71 if(shprog && !shprog_from_material)
76 shprog = material->create_compatible_shader(*coll);
80 throw invalid_operation("no collection");
83 shdata = new ProgramData(*shdata, shprog.get());
85 shprog_from_material = true;
88 void RenderPass::ensure_private_shader_data()
91 throw invalid_operation("RenderPass::ensure_private_shader_data");
94 shdata = new ProgramData(shprog.get());
95 else if(shdata.refcount()>1)
96 shdata = new ProgramData(*shdata);
99 void RenderPass::set_shader_program(const Program *prog, const ProgramData *data)
103 shprog_from_material = false;
104 shdata = (data ? new ProgramData(*data) : 0);
106 finalize_material(0);
109 const string &RenderPass::get_slotted_uniform_name(const string &slot) const
111 map<string, string>::const_iterator i = uniform_slots.find(slot);
112 if(i==uniform_slots.end())
120 void RenderPass::set_material(const Material *mat, DataFile::Collection *coll)
124 finalize_material(coll);
127 void RenderPass::set_texture(unsigned index, const Texture *tex, const Sampler *samp)
130 texturing = new Texturing;
132 texturing->attach(index, *tex, (samp ? samp : texturing->get_attached_sampler(index)));
135 int RenderPass::get_texture_index(const string &n) const
137 map<string, unsigned>::const_iterator i = tex_names.find(n);
138 if(i==tex_names.end())
143 void RenderPass::apply(Renderer &renderer) const
145 renderer.set_texturing(texturing);
146 renderer.set_material(material.get());
147 renderer.set_shader_program(shprog.get(), shdata.get());
148 renderer.set_reverse_winding(back_faces);
152 RenderPass::Loader::Loader(RenderPass &p):
153 DataFile::CollectionObjectLoader<RenderPass>(p, 0)
158 RenderPass::Loader::Loader(RenderPass &p, Collection &c):
159 DataFile::CollectionObjectLoader<RenderPass>(p, &c)
164 void RenderPass::Loader::init()
166 add("shader", &Loader::shader);
167 add("material", &Loader::material_inline);
168 add("material", &Loader::material);
169 add("material_slot", &RenderPass::material_slot);
170 add("back_faces",&RenderPass::back_faces);
171 add("texunit", &Loader::texunit);
172 add("texunit", &Loader::texunit_auto);
173 add("texunit", &Loader::texunit_named);
174 add("uniforms", &Loader::uniforms);
175 add("uniform_slot", &Loader::uniform_slot);
176 add("uniform_slot", &Loader::uniform_slot2);
179 // Temporary compatibility feature
180 string RenderPass::Loader::get_shader_name(const string &n)
182 if(n.size()>=5 && !n.compare(n.size()-5, 5, ".glsl"))
184 IO::print(IO::cerr, "Warning: Loading module '%s' as shader is deprecated\n", n);
190 void RenderPass::Loader::material_inline()
192 Material::GenericLoader ldr(coll);
194 obj.material = ldr.get_material();
195 obj.finalize_material(coll);
198 void RenderPass::Loader::material(const string &name)
200 obj.material = &get_collection().get<Material>(name);
202 obj.finalize_material(coll);
205 void RenderPass::Loader::shader(const string &n)
207 obj.shprog = &get_collection().get<Program>(get_shader_name(n));
209 obj.shprog_from_material = false;
211 obj.shdata = new ProgramData(*obj.shdata, obj.shprog.get());
213 obj.finalize_material(coll);
216 void RenderPass::Loader::texunit(unsigned i)
219 obj.texturing = new Texturing;
220 TextureLoader ldr(*obj.texturing, i, coll);
224 void RenderPass::Loader::texunit_auto(const string &n)
227 obj.texturing = new Texturing;
228 int i = obj.texturing->find_free_unit(n);
230 throw runtime_error("no free texunit");
234 void RenderPass::Loader::texunit_named(unsigned i, const string &n)
237 obj.tex_names[n] = i;
238 obj.ensure_private_shader_data();
239 obj.shdata->uniform(n, static_cast<int>(i));
242 void RenderPass::Loader::uniforms()
244 obj.ensure_private_shader_data();
245 load_sub(*obj.shdata);
248 void RenderPass::Loader::uniform_slot(const string &name)
250 uniform_slot2(name, name);
253 void RenderPass::Loader::uniform_slot2(const string &name, const string &slot)
255 obj.uniform_slots[slot] = name;
259 RenderPass::TextureLoader::TextureLoader(Texturing &t, unsigned i, Collection *c):
260 DataFile::CollectionObjectLoader<Texturing>(t, c),
265 add("sampler", &TextureLoader::sampler);
266 add("texture", &TextureLoader::texture);
269 void RenderPass::TextureLoader::finish()
272 obj.attach(index, *tex, samp);
274 obj.attach(index, *samp);
277 void RenderPass::TextureLoader::sampler(const string &name)
279 samp = &get_collection().get<Sampler>(name);
282 void RenderPass::TextureLoader::texture(const string &name)
284 tex = &get_collection().get<Texture>(name);