X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=source%2Frenderpass.cpp;h=835e78a672cd6203649cdd9cbba4345421fa41b8;hp=7c7cf2c4a2022cbb1da62c84c385914d543c2edf;hb=bec07999d95b76f4b47cffcc564d0cd0afc0435e;hpb=ed7ca8892daceebb963950945fed5e75bd96a8a1 diff --git a/source/renderpass.cpp b/source/renderpass.cpp index 7c7cf2c4..835e78a6 100644 --- a/source/renderpass.cpp +++ b/source/renderpass.cpp @@ -17,6 +17,7 @@ namespace GL { RenderPass::RenderPass(): shprog(0), + shprog_from_material(false), shdata(0), material(0), texturing(0), @@ -25,6 +26,7 @@ RenderPass::RenderPass(): RenderPass::RenderPass(const RenderPass &other): shprog(other.shprog), + shprog_from_material(other.shprog_from_material), shdata(other.shdata), uniform_slots(other.uniform_slots), material(other.material), @@ -37,6 +39,7 @@ RenderPass::RenderPass(const RenderPass &other): RenderPass &RenderPass::operator=(const RenderPass &other) { shprog = other.shprog; + shprog_from_material = other.shprog_from_material; shdata = other.shdata; uniform_slots = other.uniform_slots; material = other.material; @@ -52,8 +55,9 @@ RenderPass::~RenderPass() delete texturing; } -void RenderPass::finalize_material() +void RenderPass::finalize_material(DataFile::Collection *coll) { + maybe_create_material_shader(coll); ensure_private_shader_data(); if(!texturing) @@ -61,21 +65,44 @@ void RenderPass::finalize_material() material->attach_textures_to(*texturing, *shdata); } +void RenderPass::maybe_create_material_shader(DataFile::Collection *coll) +{ + if(shprog && !shprog_from_material) + return; + + if(coll) + { + shprog = material->create_compatible_shader(*coll); + shprog.keep(); + } + else + shprog = material->create_compatible_shader(); + + if(shdata) + shdata = new ProgramData(*shdata, shprog.get()); + + shprog_from_material = true; +} + void RenderPass::ensure_private_shader_data() { if(!shprog) throw invalid_operation("RenderPass::ensure_private_shader_data"); if(!shdata) - shdata = new ProgramData(shprog); - else if(shdata.refcount() > 1) + shdata = new ProgramData(shprog.get()); + else if(shdata.refcount()>1) shdata = new ProgramData(*shdata); } void RenderPass::set_shader_program(const Program *prog, const ProgramData *data) { shprog = prog; + shprog.keep(); + shprog_from_material = false; shdata = (data ? new ProgramData(*data) : 0); + if(material) + finalize_material(0); } const string &RenderPass::get_slotted_uniform_name(const string &slot) const @@ -93,7 +120,7 @@ void RenderPass::set_material(const Material *mat) { material = mat; material.keep(); - finalize_material(); + finalize_material(0); } void RenderPass::set_texture(unsigned index, const Texture *tex) @@ -101,7 +128,7 @@ void RenderPass::set_texture(unsigned index, const Texture *tex) if(!texturing) texturing = new Texturing; - texturing->attach(index, *tex); + texturing->attach(index, *tex, texturing->get_attached_sampler(index)); } int RenderPass::get_texture_index(const string &n) const @@ -116,7 +143,7 @@ void RenderPass::apply(Renderer &renderer) const { renderer.set_texturing(texturing); renderer.set_material(material.get()); - renderer.set_shader_program(shprog, shdata.get()); + renderer.set_shader_program(shprog.get(), shdata.get()); renderer.set_reverse_winding(back_faces); } @@ -145,6 +172,7 @@ void RenderPass::Loader::init() add("texunit", &Loader::texunit_named); add("uniforms", &Loader::uniforms); add("uniform_slot", &Loader::uniform_slot); + add("uniform_slot", &Loader::uniform_slot2); } void RenderPass::Loader::material_inline() @@ -152,23 +180,25 @@ void RenderPass::Loader::material_inline() Material::GenericLoader ldr(coll); load_sub_with(ldr); obj.material = ldr.get_material(); - obj.finalize_material(); + obj.finalize_material(coll); } void RenderPass::Loader::material(const string &name) { obj.material = &get_collection().get(name); obj.material.keep(); - obj.finalize_material(); + obj.finalize_material(coll); } void RenderPass::Loader::shader(const string &n) { obj.shprog = &get_collection().get(n); + obj.shprog.keep(); + obj.shprog_from_material = false; if(obj.shdata) - obj.shdata = new ProgramData(*obj.shdata, obj.shprog); + obj.shdata = new ProgramData(*obj.shdata, obj.shprog.get()); if(obj.material) - obj.finalize_material(); + obj.finalize_material(coll); } void RenderPass::Loader::texunit(unsigned i) @@ -216,34 +246,30 @@ void RenderPass::Loader::uniform_slot2(const string &name, const string &slot) RenderPass::TextureLoader::TextureLoader(Texturing &t, unsigned i, Collection *c): DataFile::CollectionObjectLoader(t, c), - index(i) + index(i), + tex(0), + samp(0) { + add("sampler", &TextureLoader::sampler); add("texture", &TextureLoader::texture); - add("texture2d", &TextureLoader::texture2d); } void RenderPass::TextureLoader::finish() { if(tex) - { - obj.attach(index, *tex); - tex.release(); - } + obj.attach(index, *tex, samp); + else if(samp) + obj.attach(index, *samp); } -void RenderPass::TextureLoader::texture(const string &name) +void RenderPass::TextureLoader::sampler(const string &name) { - tex = &get_collection().get(name); - tex.keep(); + samp = &get_collection().get(name); } -void RenderPass::TextureLoader::texture2d() +void RenderPass::TextureLoader::texture(const string &name) { - tex = new Texture2D; - if(coll) - load_sub(static_cast(*tex), get_collection()); - else - load_sub(static_cast(*tex)); + tex = &get_collection().get(name); } } // namespace GL