X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=source%2Frenderpass.cpp;h=835e78a672cd6203649cdd9cbba4345421fa41b8;hp=93e2bc479b2d7197c3f9f8fe3bc59703c04d3579;hb=bec07999d95b76f4b47cffcc564d0cd0afc0435e;hpb=936a307eaf8dafd0874c74d211e991bd81a74387 diff --git a/source/renderpass.cpp b/source/renderpass.cpp index 93e2bc47..835e78a6 100644 --- a/source/renderpass.cpp +++ b/source/renderpass.cpp @@ -1,19 +1,14 @@ -/* $Id$ - -This file is part of libmspgl -Copyright © 2007 Mikko Rasa, Mikkosoft Productions -Distributed under the LGPL -*/ - -#include #include -#include +#include +#include "error.h" #include "material.h" #include "renderpass.h" #include "program.h" #include "programdata.h" +#include "renderer.h" #include "texture.h" #include "texture2d.h" +#include "texturing.h" using namespace std; @@ -22,95 +17,135 @@ namespace GL { RenderPass::RenderPass(): shprog(0), + shprog_from_material(false), shdata(0), - material(0) + material(0), + texturing(0), + back_faces(false) { } RenderPass::RenderPass(const RenderPass &other): - Bindable(other), shprog(other.shprog), - shdata(other.shdata ? new ProgramData(*other.shdata) : 0), + shprog_from_material(other.shprog_from_material), + shdata(other.shdata), + uniform_slots(other.uniform_slots), material(other.material), - textures(other.textures) + material_slot(other.material_slot), + texturing(other.texturing ? new Texturing(*other.texturing) : 0), + tex_names(other.tex_names), + back_faces(other.back_faces) { } -RenderPass::~RenderPass() +RenderPass &RenderPass::operator=(const RenderPass &other) { - delete shdata; + shprog = other.shprog; + shprog_from_material = other.shprog_from_material; + shdata = other.shdata; + uniform_slots = other.uniform_slots; + material = other.material; + material_slot = other.material_slot; + texturing = other.texturing ? new Texturing(*other.texturing) : 0; + tex_names = other.tex_names; + back_faces = other.back_faces; + return *this; } -void RenderPass::set_material(const Material *mat) +RenderPass::~RenderPass() { - material = mat; + delete texturing; } -void RenderPass::set_texture(unsigned index, const Texture *tex) +void RenderPass::finalize_material(DataFile::Collection *coll) { - for(vector::iterator i=textures.begin(); i!=textures.end(); ++i) - if(i->index==index) - { - i->texture = tex; - i->texture.keep(); - return; - } + maybe_create_material_shader(coll); + ensure_private_shader_data(); - throw KeyError("No texture slot for that unit", lexical_cast(index)); + if(!texturing) + texturing = new Texturing; + material->attach_textures_to(*texturing, *shdata); } -void RenderPass::bind() const +void RenderPass::maybe_create_material_shader(DataFile::Collection *coll) { - const RenderPass *old = current(); - if(!set_current(this)) + if(shprog && !shprog_from_material) return; - if(shprog) + if(coll) { - shprog->bind(); - shdata->apply(); + shprog = material->create_compatible_shader(*coll); + shprog.keep(); } - else if(old && old->shprog) - GL::Program::unbind(); + 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.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) - material->bind(); - else if(old && old->material) - GL::Material::unbind(); + finalize_material(0); +} - unsigned used_tex_units = 0; - for(vector::const_iterator i=textures.begin(); i!=textures.end(); ++i) - { - i->texture->bind_to(i->index); - used_tex_units |= 1<index; - } - if(old) +const string &RenderPass::get_slotted_uniform_name(const string &slot) const +{ + map::const_iterator i = uniform_slots.find(slot); + if(i==uniform_slots.end()) { - for(vector::const_iterator i=old->textures.begin(); i!=old->textures.end(); ++i) - if(!used_tex_units&(1<index)) - Texture::unbind_from(i->index); + static string empty; + return empty; } + return i->second; } -void RenderPass::unbind() +void RenderPass::set_material(const Material *mat) { - const RenderPass *old = current(); - if(!set_current(0)) - return; - - if(old->shprog) - GL::Program::unbind(); + material = mat; + material.keep(); + finalize_material(0); +} - if(old->material) - GL::Material::unbind(); +void RenderPass::set_texture(unsigned index, const Texture *tex) +{ + if(!texturing) + texturing = new Texturing; - for(unsigned i=old->textures.size(); i--; ) - GL::Texture::unbind_from(i); + texturing->attach(index, *tex, texturing->get_attached_sampler(index)); } +int RenderPass::get_texture_index(const string &n) const +{ + map::const_iterator i = tex_names.find(n); + if(i==tex_names.end()) + return -1; + return i->second; +} -RenderPass::TextureSlot::TextureSlot(unsigned i): - index(i), - texture(0) -{ } +void RenderPass::apply(Renderer &renderer) const +{ + renderer.set_texturing(texturing); + renderer.set_material(material.get()); + renderer.set_shader_program(shprog.get(), shdata.get()); + renderer.set_reverse_winding(back_faces); +} RenderPass::Loader::Loader(RenderPass &p): @@ -127,81 +162,114 @@ RenderPass::Loader::Loader(RenderPass &p, Collection &c): void RenderPass::Loader::init() { - allow_pointer_reload = false; - - add("shader", &RenderPass::shprog); - add("material", static_cast(&Loader::material)); - add("material", static_cast(&Loader::material)); + add("shader", &Loader::shader); + add("material", &Loader::material_inline); + add("material", &Loader::material); + add("material_slot", &RenderPass::material_slot); + add("back_faces",&RenderPass::back_faces); add("texunit", &Loader::texunit); + add("texunit", &Loader::texunit_auto); + add("texunit", &Loader::texunit_named); add("uniforms", &Loader::uniforms); + add("uniform_slot", &Loader::uniform_slot); + add("uniform_slot", &Loader::uniform_slot2); } -void RenderPass::Loader::finish() +void RenderPass::Loader::material_inline() { - // XXX Make shdata optional - if(obj.shprog && !obj.shdata) - obj.shdata = new ProgramData; + Material::GenericLoader ldr(coll); + load_sub_with(ldr); + obj.material = ldr.get_material(); + obj.finalize_material(coll); } -void RenderPass::Loader::material() +void RenderPass::Loader::material(const string &name) { - RefPtr mat = new Material; - load_sub(*mat); - obj.material = mat; + obj.material = &get_collection().get(name); + obj.material.keep(); + obj.finalize_material(coll); } -void RenderPass::Loader::material(const string &name) +void RenderPass::Loader::shader(const string &n) { - obj.material = get_collection().get(name); - obj.material.keep(); + 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.get()); + if(obj.material) + obj.finalize_material(coll); } void RenderPass::Loader::texunit(unsigned i) { - TextureSlot slot(i); - load_sub(slot); - obj.textures.push_back(slot); + if(!obj.texturing) + obj.texturing = new Texturing; + TextureLoader ldr(*obj.texturing, i, coll); + load_sub_with(ldr); +} + +void RenderPass::Loader::texunit_auto(const string &n) +{ + if(!obj.texturing) + obj.texturing = new Texturing; + int i = obj.texturing->find_free_unit(n); + if(i<0) + throw runtime_error("no free texunit"); + texunit_named(i, n); +} + +void RenderPass::Loader::texunit_named(unsigned i, const string &n) +{ + texunit(i); + obj.tex_names[n] = i; + obj.ensure_private_shader_data(); + obj.shdata->uniform(n, static_cast(i)); } void RenderPass::Loader::uniforms() { - if(!obj.shprog) - throw InvalidState("Can't load uniforms without a shader program"); - if(!obj.shdata) - obj.shdata = new ProgramData; - load_sub(*obj.shdata, *obj.shprog); + obj.ensure_private_shader_data(); + load_sub(*obj.shdata); } +void RenderPass::Loader::uniform_slot(const string &name) +{ + uniform_slot2(name, name); +} -RenderPass::TextureSlot::Loader::Loader(TextureSlot &s): - DataFile::CollectionObjectLoader(s, 0) +void RenderPass::Loader::uniform_slot2(const string &name, const string &slot) { - init(); + obj.uniform_slots[slot] = name; } -RenderPass::TextureSlot::Loader::Loader(TextureSlot &s, Collection &c): - DataFile::CollectionObjectLoader(s, &c) + +RenderPass::TextureLoader::TextureLoader(Texturing &t, unsigned i, Collection *c): + DataFile::CollectionObjectLoader(t, c), + index(i), + tex(0), + samp(0) { - init(); + add("sampler", &TextureLoader::sampler); + add("texture", &TextureLoader::texture); } -void RenderPass::TextureSlot::Loader::init() +void RenderPass::TextureLoader::finish() { - add("texture", &Loader::texture); - add("texture2d", &Loader::texture2d); + if(tex) + obj.attach(index, *tex, samp); + else if(samp) + obj.attach(index, *samp); } -void RenderPass::TextureSlot::Loader::texture(const string &name) +void RenderPass::TextureLoader::sampler(const string &name) { - obj.texture = get_collection().get(name); - obj.texture.keep(); + samp = &get_collection().get(name); } -void RenderPass::TextureSlot::Loader::texture2d() +void RenderPass::TextureLoader::texture(const string &name) { - RefPtr tex = new Texture2D; - load_sub(*tex); - obj.texture = tex; + tex = &get_collection().get(name); } } // namespace GL