1 #include <msp/datafile/collection.h>
2 #include <msp/strings/format.h>
5 #include "renderpass.h"
7 #include "programdata.h"
10 #include "texture2d.h"
11 #include "texturing.h"
18 RenderPass::RenderPass():
20 shprog_from_material(false),
27 RenderPass::RenderPass(const RenderPass &other):
29 shprog_from_material(other.shprog_from_material),
31 uniform_slots(other.uniform_slots),
32 material(other.material),
33 material_slot(other.material_slot),
34 texturing(other.texturing ? new Texturing(*other.texturing) : 0),
35 tex_names(other.tex_names),
36 back_faces(other.back_faces)
39 RenderPass &RenderPass::operator=(const RenderPass &other)
41 shprog = other.shprog;
42 shprog_from_material = other.shprog_from_material;
43 shdata = other.shdata;
44 uniform_slots = other.uniform_slots;
45 material = other.material;
46 material_slot = other.material_slot;
47 texturing = other.texturing ? new Texturing(*other.texturing) : 0;
48 tex_names = other.tex_names;
49 back_faces = other.back_faces;
53 RenderPass::~RenderPass()
58 void RenderPass::finalize_material(DataFile::Collection *coll)
60 maybe_create_material_shader(coll);
61 ensure_private_shader_data();
64 texturing = new Texturing;
65 material->attach_textures_to(*texturing, *shdata);
68 void RenderPass::maybe_create_material_shader(DataFile::Collection *coll)
70 if(shprog && !shprog_from_material)
75 shprog = material->create_compatible_shader(*coll);
79 shprog = material->create_compatible_shader();
82 shdata = new ProgramData(*shdata, shprog.get());
84 shprog_from_material = true;
87 void RenderPass::ensure_private_shader_data()
90 throw invalid_operation("RenderPass::ensure_private_shader_data");
93 shdata = new ProgramData(shprog.get());
94 else if(shdata.refcount()>1)
95 shdata = new ProgramData(*shdata);
98 void RenderPass::set_shader_program(const Program *prog, const ProgramData *data)
102 shprog_from_material = false;
103 shdata = (data ? new ProgramData(*data) : 0);
105 finalize_material(0);
108 const string &RenderPass::get_slotted_uniform_name(const string &slot) const
110 map<string, string>::const_iterator i = uniform_slots.find(slot);
111 if(i==uniform_slots.end())
119 void RenderPass::set_material(const Material *mat)
123 finalize_material(0);
126 void RenderPass::set_texture(unsigned index, const Texture *tex)
129 texturing = new Texturing;
131 texturing->attach(index, *tex, texturing->get_attached_sampler(index));
134 int RenderPass::get_texture_index(const string &n) const
136 map<string, unsigned>::const_iterator i = tex_names.find(n);
137 if(i==tex_names.end())
142 void RenderPass::apply(Renderer &renderer) const
144 renderer.set_texturing(texturing);
145 renderer.set_material(material.get());
146 renderer.set_shader_program(shprog.get(), shdata.get());
147 renderer.set_reverse_winding(back_faces);
151 RenderPass::Loader::Loader(RenderPass &p):
152 DataFile::CollectionObjectLoader<RenderPass>(p, 0)
157 RenderPass::Loader::Loader(RenderPass &p, Collection &c):
158 DataFile::CollectionObjectLoader<RenderPass>(p, &c)
163 void RenderPass::Loader::init()
165 add("shader", &Loader::shader);
166 add("material", &Loader::material_inline);
167 add("material", &Loader::material);
168 add("material_slot", &RenderPass::material_slot);
169 add("back_faces",&RenderPass::back_faces);
170 add("texunit", &Loader::texunit);
171 add("texunit", &Loader::texunit_auto);
172 add("texunit", &Loader::texunit_named);
173 add("uniforms", &Loader::uniforms);
174 add("uniform_slot", &Loader::uniform_slot);
175 add("uniform_slot", &Loader::uniform_slot2);
178 void RenderPass::Loader::material_inline()
180 Material::GenericLoader ldr(coll);
182 obj.material = ldr.get_material();
183 obj.finalize_material(coll);
186 void RenderPass::Loader::material(const string &name)
188 obj.material = &get_collection().get<Material>(name);
190 obj.finalize_material(coll);
193 void RenderPass::Loader::shader(const string &n)
195 obj.shprog = &get_collection().get<Program>(n);
197 obj.shprog_from_material = false;
199 obj.shdata = new ProgramData(*obj.shdata, obj.shprog.get());
201 obj.finalize_material(coll);
204 void RenderPass::Loader::texunit(unsigned i)
207 obj.texturing = new Texturing;
208 TextureLoader ldr(*obj.texturing, i, coll);
212 void RenderPass::Loader::texunit_auto(const string &n)
215 obj.texturing = new Texturing;
216 int i = obj.texturing->find_free_unit(n);
218 throw runtime_error("no free texunit");
222 void RenderPass::Loader::texunit_named(unsigned i, const string &n)
225 obj.tex_names[n] = i;
226 obj.ensure_private_shader_data();
227 obj.shdata->uniform(n, static_cast<int>(i));
230 void RenderPass::Loader::uniforms()
232 obj.ensure_private_shader_data();
233 load_sub(*obj.shdata);
236 void RenderPass::Loader::uniform_slot(const string &name)
238 uniform_slot2(name, name);
241 void RenderPass::Loader::uniform_slot2(const string &name, const string &slot)
243 obj.uniform_slots[slot] = name;
247 RenderPass::TextureLoader::TextureLoader(Texturing &t, unsigned i, Collection *c):
248 DataFile::CollectionObjectLoader<Texturing>(t, c),
253 add("sampler", &TextureLoader::sampler);
254 add("texture", &TextureLoader::texture);
257 void RenderPass::TextureLoader::finish()
260 obj.attach(index, *tex, samp);
262 obj.attach(index, *samp);
265 void RenderPass::TextureLoader::sampler(const string &name)
267 samp = &get_collection().get<Sampler>(name);
270 void RenderPass::TextureLoader::texture(const string &name)
272 tex = &get_collection().get<Texture>(name);