X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Frender%2Frenderer.cpp;h=5d0bf6b8dab2bf78b995ff18584bc0351902fc01;hb=82282de52e8e8f3bbafefaf92bf76f53f2c2495e;hp=ad7ba5a78e173bbfe7d13a62cfa87d3b66e01a42;hpb=233dff2a6c552e08da832496aecd88ef4f8948f1;p=libs%2Fgl.git diff --git a/source/render/renderer.cpp b/source/render/renderer.cpp index ad7ba5a7..5d0bf6b8 100644 --- a/source/render/renderer.cpp +++ b/source/render/renderer.cpp @@ -44,7 +44,7 @@ void Renderer::init() shdata_stack.reserve(32); state = &state_stack.back(); - standard_shdata.uniform("projection_matrix", Matrix()); + standard_shdata.uniform("clip_eye_matrix", Matrix()); standard_shdata.uniform("eye_world_matrix", Matrix()); } @@ -56,37 +56,94 @@ Renderer::~Renderer() void Renderer::set_camera(const Camera &c) { state->camera = &c; - standard_shdata.uniform("projection_matrix", state->camera->get_projection_matrix()); + standard_shdata.uniform("clip_eye_matrix", state->camera->get_projection_matrix()); standard_shdata.uniform("eye_world_matrix", state->camera->get_view_matrix()); changed |= STANDARD_SHDATA; - set_matrix(state->camera->get_view_matrix()); + set_matrix(Matrix()); } void Renderer::set_matrix(const Matrix &matrix) { - state->modelview_matrix = matrix; + state->model_matrix = matrix; changed |= MATRIX; } void Renderer::transform(const Matrix &matrix) { - state->modelview_matrix *= matrix; + state->model_matrix *= matrix; changed |= MATRIX; } +void Renderer::set_texture(Tag tag, const Texture *tex, const Sampler *samp) +{ + set_texture(tag, -1, tex, samp); +} + +void Renderer::set_texture(Tag tag, int unit, const Texture *tex, const Sampler *samp) +{ + if(texture_stack.size()>state->texture_count) + { + BoundTexture &bt = texture_stack[state->texture_count]; + if((!tag.id || bt.tag==tag) && (unit<0 || bt.unit==unit) && bt.texture==tex && bt.sampler==samp) + { + ++state->texture_count; + return; + } + else + flush_textures(); + } + + for(vector::iterator i=texture_stack.end(); i!=texture_stack.begin(); ) + if((--i)->tag==tag && i->unit==unit) + { + i->replaced = texture_stack.size(); + break; + } + + texture_stack.push_back(BoundTexture()); + BoundTexture &bound_tex = texture_stack.back(); + bound_tex.tag = tag; + bound_tex.unit = unit; + bound_tex.texture = tex; + bound_tex.sampler = samp; + state->texture_count = texture_stack.size(); +} + void Renderer::set_texture(const Texture *t, const Sampler *s) { - state->texture = t; - state->sampler = s; - state->texturing = 0; + set_texture(Tag(), 0, t, s); +} + +void Renderer::flush_textures() +{ + for(unsigned i=0; i=state->texture_count && bt.unit>=0) + { + Texture::unbind_from(bt.unit); + Sampler::unbind_from(bt.unit); + } + else if(bt.replaced>=static_cast(state->texture_count)) + bt.replaced = -1; + } + + texture_stack.erase(texture_stack.begin()+state->texture_count, texture_stack.end()); } +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wdeprecated-declarations" void Renderer::set_texturing(const Texturing *t) { - state->texturing = t; - state->texture = 0; - state->sampler = 0; + if(t) + { + unsigned n_units = TexUnit::get_n_units(); + for(unsigned i=0; iget_attached_texture(i)) + set_texture(Tag(), i, tex, t->get_attached_sampler(i)); + } } +#pragma GCC diagnostic pop unsigned Renderer::allocate_effect_texunit() { @@ -102,10 +159,9 @@ void Renderer::set_material(const Material *m) void Renderer::set_lighting(const Lighting *l) { state->lighting = l; - state->lighting_matrix = state->modelview_matrix; if(l) { - l->update_shader_data(standard_shdata, state->lighting_matrix); + l->update_shader_data(standard_shdata, Matrix()); changed |= STANDARD_SHDATA; } } @@ -113,10 +169,9 @@ void Renderer::set_lighting(const Lighting *l) void Renderer::set_clipping(const Clipping *c) { state->clipping = c; - state->clipping_matrix = state->modelview_matrix; if(c) { - c->update_shader_data(standard_shdata, state->clipping_matrix); + c->update_shader_data(standard_shdata, Matrix()); changed |= STANDARD_SHDATA; } } @@ -130,15 +185,20 @@ void Renderer::set_shader_program(const Program *p, const ProgramData *d) void Renderer::add_shader_data(const ProgramData &d) { - if(state->shdata_countshdata_count]==&d) - ++state->shdata_count; - else + if(state->shdata_countshdata_count = shdata_stack.size(); - changed |= SHADER_DATA; + const BoundProgramData &top = shdata_stack.back(); + if(top.shdata==&d && top.generation==d.get_generation()) + { + ++state->shdata_count; + return; + } } + + flush_shader_data(); + shdata_stack.push_back(&d); + state->shdata_count = shdata_stack.size(); + changed |= SHADER_DATA; } void Renderer::flush_shader_data() @@ -189,32 +249,29 @@ void Renderer::pop_state() { if(state->camera) { - standard_shdata.uniform("projection_matrix", state->camera->get_projection_matrix()); + standard_shdata.uniform("clip_eye_matrix", state->camera->get_projection_matrix()); standard_shdata.uniform("eye_world_matrix", state->camera->get_view_matrix()); } else { - standard_shdata.uniform("projection_matrix", Matrix()); + standard_shdata.uniform("clip_eye_matrix", Matrix()); standard_shdata.uniform("eye_world_matrix", Matrix()); } changed |= STANDARD_SHDATA; } - /* This actually should compare the relevant matrices rather than check for - camera, but in practice lighting and clipping is set right after the camera - and a boolean check is much faster than matrix comparison. */ - if(state->lighting!=old_lighting || camera_changed) + if(state->lighting!=old_lighting) { if(state->lighting) { - state->lighting->update_shader_data(standard_shdata, state->lighting_matrix); + state->lighting->update_shader_data(standard_shdata, Matrix()); changed |= STANDARD_SHDATA; } } - if(state->clipping!=old_clipping || camera_changed) + if(state->clipping!=old_clipping) { if(state->clipping) { - state->clipping->update_shader_data(standard_shdata, state->clipping_matrix); + state->clipping->update_shader_data(standard_shdata, Matrix()); changed |= STANDARD_SHDATA; } } @@ -229,12 +286,16 @@ void Renderer::end() if(default_camera) set_camera(*default_camera); else - standard_shdata.uniform("projection_matrix", Matrix()); + standard_shdata.uniform("clip_eye_matrix", Matrix()); shdata_stack.clear(); excluded.clear(); - Texturing::unbind(); - Texture::unbind_from(0); + for(vector::iterator i=texture_stack.begin(); i!=texture_stack.end(); ++i) + if(i->unit>=0) + { + Texture::unbind_from(i->unit); + Sampler::unbind_from(i->unit); + } Clipping::unbind(); Program::unbind(); VertexSetup::unbind(); @@ -280,21 +341,20 @@ void Renderer::apply_state() /* We (mostly) let the objects themselves figure out if the binding has changed */ - if(state->texturing) - state->texturing->bind(); - else + if(state->texture_count::const_iterator i=texture_stack.begin(); i!=texture_stack.end(); ++i) { - Texturing::unbind(); - if(state->texture) + int unit = (i->tag.id ? state->shprog->get_uniform_binding(i->tag) : i->unit); + if(unit>=0) { - if(state->sampler) - state->sampler->bind_to(0); - else - Sampler::unbind_from(0); - state->texture->bind_to(0); + if(i->texture) + i->texture->bind_to(unit); + if(i->sampler) + i->sampler->bind_to(unit); + i->unit = unit; } - else - Texture::unbind_from(0); } if(state->clipping) @@ -307,10 +367,10 @@ void Renderer::apply_state() if(changed&MATRIX) { - standard_shdata.uniform("eye_obj_matrix", state->modelview_matrix); - LinAl::SquareMatrix nm = state->modelview_matrix.block<3, 3>(0, 0); + standard_shdata.uniform("world_obj_matrix", state->model_matrix); + LinAl::SquareMatrix nm = state->model_matrix.block<3, 3>(0, 0); nm = transpose(invert(nm)); - standard_shdata.uniform_matrix3("eye_obj_normal_matrix", &nm(0, 0)); + standard_shdata.uniform("world_obj_normal_matrix", nm); changed = (changed&~MATRIX)|STANDARD_SHDATA; } @@ -326,14 +386,20 @@ void Renderer::apply_state() changed &= ~STANDARD_SHDATA; } + bool shdata_changed = changed&SHADER_DATA; + for(vector::const_iterator i=shdata_stack.begin(); (!shdata_changed && i!=shdata_stack.end()); ++i) + shdata_changed = (i->shdata->get_generation()!=i->generation); bool extra_shdata = (shdata_stack.size()>state->shdata_count); - if((changed&SHADER_DATA) || shprog_changed || extra_shdata) + if(shdata_changed || shprog_changed || extra_shdata) { if(extra_shdata) shdata_stack.erase(shdata_stack.begin()+state->shdata_count, shdata_stack.end()); - for(vector::const_iterator i=shdata_stack.begin(); i!=shdata_stack.end(); ++i) - (*i)->apply(); + for(vector::const_iterator i=shdata_stack.begin(); i!=shdata_stack.end(); ++i) + { + i->shdata->apply(); + i->generation = i->shdata->get_generation(); + } changed &= ~SHADER_DATA; } @@ -354,10 +420,23 @@ void Renderer::apply_state() } +Renderer::BoundTexture::BoundTexture(): + unit(-1), + texture(0), + sampler(0), + replaced(-1) +{ } + + +Renderer::BoundProgramData::BoundProgramData(const ProgramData *d): + shdata(d), + generation(0) +{ } + + Renderer::State::State(): camera(0), - texture(0), - texturing(0), + texture_count(0), lowest_effect_texunit(TexUnit::get_n_units()), material(0), lighting(0),