X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=source%2Frender%2Frenderer.cpp;h=ad5f87d5a4fd6fd8add9613b7562cb4415de0156;hp=1d43a939432f9a593381992824aedec7fca87f84;hb=38712d8ecc57d043a2419ffbaeeb57f7a6586f14;hpb=e24dcc10ff0d78203331eabae6b934c9e064105d diff --git a/source/render/renderer.cpp b/source/render/renderer.cpp index 1d43a939..ad5f87d5 100644 --- a/source/render/renderer.cpp +++ b/source/render/renderer.cpp @@ -2,21 +2,19 @@ #include "buffer.h" #include "camera.h" #include "clipping.h" -#include "deviceinfo.h" #include "error.h" +#include "framebuffer.h" #include "lighting.h" #include "material.h" #include "program.h" #include "programdata.h" #include "renderable.h" #include "renderer.h" +#include "resourcemanager.h" #include "sampler.h" #include "texture.h" -#include "texturing.h" -#include "texunit.h" #include "vertexarray.h" #include "vertexsetup.h" -#include "windingtest.h" using namespace std; @@ -24,21 +22,7 @@ namespace Msp { namespace GL { Renderer::Renderer(): - default_camera(0) -{ - init(); -} - -Renderer::Renderer(const Camera *c): - default_camera(c) -{ - init(); - - if(c) - set_camera(*c); -} - -void Renderer::init() + changed(0) { state_stack.reserve(16); state_stack.push_back(State()); @@ -71,17 +55,31 @@ void Renderer::transform(const Matrix &matrix) changed |= MATRIX; } -void Renderer::set_texture(Tag tag, const Texture *tex, const Sampler *samp) +void Renderer::set_framebuffer(const Framebuffer *f) +{ + state->framebuffer = f; +} + +void Renderer::set_viewport(const Rect *v) { - set_texture(tag, -1, tex, samp); + state->viewport = v; } -void Renderer::set_texture(Tag tag, int unit, const Texture *tex, const Sampler *samp) +void Renderer::set_scissor(const Rect *s) { + state->scissor = s; +} + +void Renderer::set_texture(Tag tag, const Texture *tex, const Sampler *samp) +{ + if(tex) + if(ResourceManager *res_mgr = tex->get_manager()) + res_mgr->resource_used(*tex); + 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) + if(bt.tag==tag && bt.texture==tex && bt.sampler==samp) { ++state->texture_count; return; @@ -90,8 +88,8 @@ void Renderer::set_texture(Tag tag, int unit, const Texture *tex, const Sampler flush_textures(); } - for(vector::iterator i=texture_stack.end(); i!=texture_stack.begin(); ) - if((--i)->tag==tag && i->unit==unit) + for(auto i=texture_stack.end(); i!=texture_stack.begin(); ) + if((--i)->tag==tag) { i->replaced = texture_stack.size(); break; @@ -100,53 +98,20 @@ void Renderer::set_texture(Tag tag, int unit, const Texture *tex, const Sampler 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) -{ - 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; - } + for(unsigned i=0; itexture_count; ++i) + if(texture_stack[i].replaced>=static_cast(state->texture_count)) + texture_stack[i].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) -{ - 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() -{ - return --state->lowest_effect_texunit; -} - void Renderer::set_material(const Material *m) { if(m) @@ -202,14 +167,29 @@ void Renderer::set_vertex_setup(const VertexSetup *vs) state->vertex_setup = vs; } -void Renderer::set_winding_test(const WindingTest *w) +void Renderer::set_front_face(FaceWinding winding) +{ + state->front_face = winding; +} + +void Renderer::set_face_cull(CullMode cull) { - state->winding_test = w; + state->face_cull = cull; } -void Renderer::set_reverse_winding(bool r) +void Renderer::set_depth_test(const DepthTest *dt) { - state->reverse_winding = r; + state->depth_test = dt; +} + +void Renderer::set_stencil_test(const StencilTest *st) +{ + state->stencil_test = st; +} + +void Renderer::set_blend(const Blend *b) +{ + state->blend = b; } void Renderer::set_object_lod_bias(unsigned b) @@ -239,22 +219,11 @@ void Renderer::end() throw invalid_operation("Renderer::end"); *state = State(); - if(default_camera) - set_camera(*default_camera); shdata_stack.clear(); + add_shader_data(standard_shdata); excluded.clear(); - 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(); - Buffer::unbind_from(ELEMENT_ARRAY_BUFFER); - WindingTest::unbind(); + commands.use_pipeline(0); } void Renderer::exclude(const Renderable &renderable) @@ -273,51 +242,60 @@ void Renderer::render(const Renderable &renderable, Tag tag) renderable.render(*this, tag); } +void Renderer::clear(const ClearValue *values) +{ + pipeline_state.set_framebuffer(state->framebuffer); + pipeline_state.set_viewport(state->viewport); + pipeline_state.set_scissor(state->scissor); + commands.use_pipeline(&pipeline_state); + commands.clear(values); +} + void Renderer::draw(const Batch &batch) { apply_state(); - - batch.draw(); + batch.refresh(); + commands.use_pipeline(&pipeline_state); + commands.draw(batch); } void Renderer::draw_instanced(const Batch &batch, unsigned count) { apply_state(); - - batch.draw_instanced(count); + batch.refresh(); + commands.use_pipeline(&pipeline_state); + commands.draw_instanced(batch, count); } -void Renderer::apply_state() +void Renderer::resolve_multisample(Framebuffer &target) { - if(!state->shprog) - throw invalid_operation("Renderer::apply_state"); + if(!state->framebuffer) + throw invalid_operation("Renderer::resolve_multisample"); - /* We (mostly) let the objects themselves figure out if the binding has - changed */ + unsigned width = state->framebuffer->get_width(); + unsigned height = state->framebuffer->get_height(); + if(target.get_width()!=width || target.get_height()!=height) + throw incompatible_data("Renderer::resolve_multisample"); - if(state->texture_countframebuffer); + commands.use_pipeline(&pipeline_state); + commands.resolve_multisample(target); +} - for(vector::const_iterator i=texture_stack.begin(); i!=texture_stack.end(); ++i) - { - int unit = (i->tag.id ? state->shprog->get_uniform_binding(i->tag) : i->unit); - if(unit>=0) - { - if(i->texture) - i->texture->bind_to(unit); - if(i->sampler) - i->sampler->bind_to(unit); - i->unit = unit; - } - } +void Renderer::begin_query(const QueryPool &pool, unsigned index) +{ + commands.begin_query(pool, index); +} - if(state->clipping) - state->clipping->bind(); - else - Clipping::unbind(); +void Renderer::end_query(const QueryPool &pool, unsigned index) +{ + commands.end_query(pool, index); +} - bool shprog_changed = (state->shprog!=Program::current()); - state->shprog->bind(); +void Renderer::apply_state() +{ + if(!state->shprog) + throw invalid_operation("Renderer::apply_state"); if(changed&MATRIX) { @@ -328,8 +306,38 @@ void Renderer::apply_state() changed &= ~MATRIX; } + pipeline_state.set_framebuffer(state->framebuffer); + pipeline_state.set_viewport(state->viewport); + pipeline_state.set_scissor(state->scissor); + + bool shprog_changed = (state->shprog!=pipeline_state.get_shader_program()); + pipeline_state.set_shader_program(state->shprog); + + if(state->vertex_setup) + { + if(const VertexArray *array = state->vertex_setup->get_vertex_array()) + array->refresh(); + if(const VertexArray *array = state->vertex_setup->get_instance_array()) + array->refresh(); + } + pipeline_state.set_vertex_setup(state->vertex_setup); + + pipeline_state.set_front_face(state->front_face); + pipeline_state.set_face_cull(state->face_cull); + pipeline_state.set_enabled_clip_planes(state->clipping ? (1<clipping->get_n_planes())-1 : 0); + + if(state->texture_countshprog->get_uniform_binding(t.tag) : t.unit); + if(unit>=0) + pipeline_state.set_texture(unit, t.texture, t.sampler); + } + bool shdata_changed = changed&SHADER_DATA; - for(vector::const_iterator i=shdata_stack.begin(); (!shdata_changed && i!=shdata_stack.end()); ++i) + for(auto 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); @@ -337,56 +345,22 @@ void Renderer::apply_state() { 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) + for(const BoundProgramData &d: shdata_stack) { - i->shdata->apply(); - i->generation = i->shdata->get_generation(); + d.shdata->apply(*state->shprog, pipeline_state); + d.generation = d.shdata->get_generation(); } changed &= ~SHADER_DATA; } - if(state->vertex_setup) - state->vertex_setup->bind(); - else - VertexSetup::unbind(); - - if(state->winding_test) - { - if(state->reverse_winding) - state->winding_test->get_reverse().bind(); - else - state->winding_test->bind(); - } - else - WindingTest::unbind(); + pipeline_state.set_depth_test(state->depth_test); + pipeline_state.set_stencil_test(state->stencil_test); + pipeline_state.set_blend(state->blend); } -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_count(0), - lowest_effect_texunit(Limits::get_global().max_texture_bindings), - clipping(0), - shprog(0), - shdata_count(0), - vertex_setup(0), - winding_test(0), - reverse_winding(false), - object_lod_bias(0) + shdata(d) { } } // namespace GL