]> git.tdb.fi Git - libs/gl.git/blobdiff - source/render/renderer.cpp
Move WindowView::render to the backend
[libs/gl.git] / source / render / renderer.cpp
index d86b78046eb24454f954155f687d0c2ec9c710c0..b935ce14c3e88bf94636135cc5ae0fe084574cbf 100644 (file)
@@ -1,21 +1,19 @@
 #include "batch.h"
 #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 "texunit.h"
 #include "vertexarray.h"
 #include "vertexsetup.h"
-#include "windingtest.h"
 
 using namespace std;
 
@@ -55,8 +53,27 @@ void Renderer::transform(const Matrix &matrix)
        changed |= MATRIX;
 }
 
+void Renderer::set_framebuffer(const Framebuffer *f)
+{
+       state->framebuffer = f;
+}
+
+void Renderer::set_viewport(const Rect *v)
+{
+       state->viewport = v;
+}
+
+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];
@@ -69,7 +86,7 @@ void Renderer::set_texture(Tag tag, const Texture *tex, const Sampler *samp)
                        flush_textures();
        }
 
-       for(vector<BoundTexture>::iterator i=texture_stack.end(); i!=texture_stack.begin(); )
+       for(auto i=texture_stack.end(); i!=texture_stack.begin(); )
                if((--i)->tag==tag)
                {
                        i->replaced = texture_stack.size();
@@ -86,17 +103,9 @@ void Renderer::set_texture(Tag tag, const Texture *tex, const Sampler *samp)
 
 void Renderer::flush_textures()
 {
-       for(unsigned i=0; i<texture_stack.size(); ++i)
-       {
-               BoundTexture &bt = texture_stack[i];
-               if(i>=state->texture_count && bt.unit>=0)
-               {
-                       Texture::unbind_from(bt.unit);
-                       Sampler::unbind_from(bt.unit);
-               }
-               else if(bt.replaced>=static_cast<int>(state->texture_count))
-                       bt.replaced = -1;
-       }
+       for(unsigned i=0; i<state->texture_count; ++i)
+               if(texture_stack[i].replaced>=static_cast<int>(state->texture_count))
+                       texture_stack[i].replaced = -1;
 
        texture_stack.erase(texture_stack.begin()+state->texture_count, texture_stack.end());
 }
@@ -113,13 +122,6 @@ void Renderer::set_lighting(const Lighting *l)
                add_shader_data(l->get_shader_data());
 }
 
-void Renderer::set_clipping(const Clipping *c)
-{
-       state->clipping = c;
-       if(c)
-               add_shader_data(c->get_shader_data());
-}
-
 void Renderer::set_shader_program(const Program *p, const ProgramData *d)
 {
        state->shprog = p;
@@ -156,14 +158,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->face_cull = cull;
+}
+
+void Renderer::set_depth_test(const DepthTest *dt)
+{
+       state->depth_test = dt;
+}
+
+void Renderer::set_stencil_test(const StencilTest *st)
 {
-       state->winding_test = w;
+       state->stencil_test = st;
 }
 
-void Renderer::set_reverse_winding(bool r)
+void Renderer::set_blend(const Blend *b)
 {
-       state->reverse_winding = r;
+       state->blend = b;
 }
 
 void Renderer::set_object_lod_bias(unsigned b)
@@ -194,49 +211,59 @@ void Renderer::end()
 
        *state = State();
        shdata_stack.clear();
-       excluded.clear();
+       add_shader_data(standard_shdata);
 
-       for(vector<BoundTexture>::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)
+void Renderer::clear(const ClearValue *values)
 {
-       excluded.insert(&renderable);
+       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::include(const Renderable &renderable)
+void Renderer::draw(const Batch &batch)
 {
-       excluded.erase(&renderable);
+       apply_state();
+       batch.refresh();
+       commands.use_pipeline(&pipeline_state);
+       commands.draw(batch);
 }
 
-void Renderer::render(const Renderable &renderable, Tag tag)
+void Renderer::draw_instanced(const Batch &batch, unsigned count)
 {
-       if(!excluded.count(&renderable))
-               renderable.render(*this, tag);
+       apply_state();
+       batch.refresh();
+       commands.use_pipeline(&pipeline_state);
+       commands.draw_instanced(batch, count);
 }
 
-void Renderer::draw(const Batch &batch)
+void Renderer::resolve_multisample(Framebuffer &target)
 {
-       apply_state();
+       if(!state->framebuffer)
+               throw invalid_operation("Renderer::resolve_multisample");
+
+       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");
 
-       batch.draw();
+       pipeline_state.set_framebuffer(state->framebuffer);
+       commands.use_pipeline(&pipeline_state);
+       commands.resolve_multisample(target);
 }
 
-void Renderer::draw_instanced(const Batch &batch, unsigned count)
+void Renderer::begin_query(const QueryPool &pool, unsigned index)
 {
-       apply_state();
+       commands.begin_query(pool, index);
+}
 
-       batch.draw_instanced(count);
+void Renderer::end_query(const QueryPool &pool, unsigned index)
+{
+       commands.end_query(pool, index);
 }
 
 void Renderer::apply_state()
@@ -244,33 +271,6 @@ void Renderer::apply_state()
        if(!state->shprog)
                throw invalid_operation("Renderer::apply_state");
 
-       /* We (mostly) let the objects themselves figure out if the binding has
-       changed */
-
-       if(state->texture_count<texture_stack.size())
-               flush_textures();
-
-       for(vector<BoundTexture>::const_iterator i=texture_stack.begin(); i!=texture_stack.end(); ++i)
-       {
-               int unit = state->shprog->get_uniform_binding(i->tag);
-               if(unit>=0)
-               {
-                       if(i->texture)
-                               i->texture->bind_to(unit);
-                       if(i->sampler)
-                               i->sampler->bind_to(unit);
-                       i->unit = unit;
-               }
-       }
-
-       if(state->clipping)
-               state->clipping->bind();
-       else
-               Clipping::unbind();
-
-       bool shprog_changed = (state->shprog!=Program::current());
-       state->shprog->bind();
-
        if(changed&MATRIX)
        {
                standard_shdata.uniform("world_obj_matrix", state->model_matrix);
@@ -280,8 +280,39 @@ 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);
+
+       if(state->texture_count<texture_stack.size())
+               flush_textures();
+
+       for(const BoundTexture &t: texture_stack)
+               if(t.texture && t.replaced<0)
+               {
+                       if(t.binding<0 || shprog_changed)
+                               t.binding = state->shprog->get_uniform_binding(t.tag);
+                       if(t.binding>=0)
+                               pipeline_state.set_texture(t.binding, t.texture, t.sampler);
+               }
+
        bool shdata_changed = changed&SHADER_DATA;
-       for(vector<BoundProgramData>::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);
 
@@ -289,56 +320,22 @@ void Renderer::apply_state()
        {
                if(extra_shdata)
                        shdata_stack.erase(shdata_stack.begin()+state->shdata_count, shdata_stack.end());
-               for(vector<BoundProgramData>::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