]> git.tdb.fi Git - libs/gl.git/commitdiff
Require explicit begin() and end() calls in Renderer
authorMikko Rasa <tdb@tdb.fi>
Fri, 12 Nov 2021 19:16:55 +0000 (21:16 +0200)
committerMikko Rasa <tdb@tdb.fi>
Fri, 12 Nov 2021 19:27:40 +0000 (21:27 +0200)
source/materials/pbrmaterial.cpp
source/render/renderer.cpp
source/render/renderer.h
source/render/view.cpp

index e01bbb9d5f0043463a6d38a987f04758a2c95022..ebd3321f2268a05054c05f086c035e5b88fdb39a 100644 (file)
@@ -55,9 +55,11 @@ const Texture2D &PbrMaterial::get_or_create_fresnel_lookup()
        Framebuffer fresnel_lookup_fbo((COLOR_ATTACHMENT,RG8));
        fresnel_lookup_fbo.attach(COLOR_ATTACHMENT, *fresnel_lookup);
        Renderer renderer;
+       renderer.begin();
        renderer.set_framebuffer(&fresnel_lookup_fbo);
        renderer.set_shader_program(&shprog, &shdata);
        mesh.draw(renderer);
+       renderer.end();
 
        return *fresnel_lookup;
 }
index 3b28d8545b668b75fe2b0d580232dfe96a22e06b..d53d71459304540512362bbc6846c637d6244a1f 100644 (file)
@@ -23,33 +23,44 @@ namespace GL {
 Renderer::Renderer()
 {
        state_stack.reserve(16);
-       state_stack.push_back(State());
        shdata_stack.reserve(32);
-       state = &state_stack.back();
-       add_shader_data(standard_shdata);
+       texture_stack.reserve(32);
 }
 
 Renderer::~Renderer()
 {
-       end();
+}
+
+void Renderer::begin()
+{
+       if(current_state)
+               throw invalid_operation("Renderer::begin");
+
+       state_stack.push_back(State());
+       current_state = &state_stack.back();
+       add_shader_data(standard_shdata);
 }
 
 void Renderer::end()
 {
-       if(state_stack.size()>1)
+       if(!current_state || state_stack.size()>1)
                throw invalid_operation("Renderer::end");
 
-       *state = State();
+       current_state = 0;
+       state_stack.clear();
+       texture_stack.clear();
        shdata_stack.clear();
-       add_shader_data(standard_shdata);
 
        commands.use_pipeline(0);
 }
 
 void Renderer::push_state()
 {
+       if(state_stack.empty())
+               throw invalid_operation("Renderer::push_state");
+
        state_stack.push_back(state_stack.back());
-       state = &state_stack.back();
+       current_state = &state_stack.back();
 }
 
 void Renderer::pop_state()
@@ -58,81 +69,94 @@ void Renderer::pop_state()
                throw stack_underflow("Renderer::pop_state");
 
        state_stack.pop_back();
-       state = &state_stack.back();
+       current_state = &state_stack.back();
        changed |= MATRIX;
 }
 
+Renderer::State &Renderer::get_state() const
+{
+#ifdef DEBUG
+       if(!current_state)
+               throw invalid_operation("Renderer::get_state");
+#endif
+       return *current_state;
+}
+
 void Renderer::set_camera(const Camera &c)
 {
-       state->camera = &c;
+       get_state().camera = &c;
        add_shader_data(c.get_shader_data());
        set_matrix(Matrix());
 }
 
 void Renderer::set_matrix(const Matrix &matrix)
 {
-       state->model_matrix = matrix;
+       get_state().model_matrix = matrix;
        changed |= MATRIX;
 }
 
 void Renderer::transform(const Matrix &matrix)
 {
-       state->model_matrix *= matrix;
+       get_state().model_matrix *= matrix;
        changed |= MATRIX;
 }
 
 void Renderer::set_framebuffer(const Framebuffer *f)
 {
-       state->framebuffer = f;
+       get_state().framebuffer = f;
 }
 
 void Renderer::set_viewport(const Rect *v)
 {
-       state->viewport = v;
+       get_state().viewport = v;
 }
 
 void Renderer::set_scissor(const Rect *s)
 {
-       state->scissor = s;
+       get_state().scissor = s;
 }
 
 void Renderer::set_shader_program(const Program *p, const ProgramData *d)
 {
-       state->shprog = p;
+       get_state().shprog = p;
        if(p && d)
                add_shader_data(*d);
 }
 
 void Renderer::add_shader_data(const ProgramData &d)
 {
-       if(state->shdata_count<shdata_stack.size())
+       State &state = get_state();
+
+       if(state.shdata_count<shdata_stack.size())
        {
                const BoundProgramData &top = shdata_stack.back();
                if(top.shdata==&d && top.generation==d.get_generation())
                {
-                       ++state->shdata_count;
+                       ++state.shdata_count;
                        return;
                }
        }
 
        flush_shader_data();
        shdata_stack.push_back(&d);
-       state->shdata_count = shdata_stack.size();
+       state.shdata_count = shdata_stack.size();
        changed |= SHADER_DATA;
 }
 
 void Renderer::set_texture(Tag tag, const Texture *tex, const Sampler *samp)
 {
+       State &state = get_state();
+
        if(tex)
                if(ResourceManager *res_mgr = tex->get_manager())
                        res_mgr->resource_used(*tex);
 
-       if(texture_stack.size()>state->texture_count)
+       if(texture_stack.size()>state.texture_count)
        {
-               BoundTexture &bt = texture_stack[state->texture_count];
+               BoundTexture &bt = texture_stack[state.texture_count];
                if(bt.tag==tag && bt.texture==tex && bt.sampler==samp)
                {
-                       ++state->texture_count;
+                       ++state.texture_count;
                        return;
                }
                else
@@ -151,64 +175,70 @@ void Renderer::set_texture(Tag tag, const Texture *tex, const Sampler *samp)
        bound_tex.tag = tag;
        bound_tex.texture = tex;
        bound_tex.sampler = samp;
-       state->texture_count = texture_stack.size();
+       state.texture_count = texture_stack.size();
 }
 
 void Renderer::flush_shader_data()
 {
-       if(shdata_stack.size()>state->shdata_count)
-               shdata_stack.erase(shdata_stack.begin()+state->shdata_count, shdata_stack.end());
+       const State &state = get_state();
+
+       if(shdata_stack.size()>state.shdata_count)
+               shdata_stack.erase(shdata_stack.begin()+state.shdata_count, shdata_stack.end());
 }
 
 void Renderer::flush_textures()
 {
-       for(unsigned i=0; i<state->texture_count; ++i)
-               if(texture_stack[i].replaced>=static_cast<int>(state->texture_count))
+       const State &state = get_state();
+
+       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());
+       texture_stack.erase(texture_stack.begin()+state.texture_count, texture_stack.end());
 }
 
 void Renderer::set_vertex_setup(const VertexSetup *vs)
 {
-       state->vertex_setup = vs;
+       get_state().vertex_setup = vs;
 }
 
 void Renderer::set_front_face(FaceWinding winding)
 {
-       state->front_face = winding;
+       get_state().front_face = winding;
 }
 
 void Renderer::set_face_cull(CullMode cull)
 {
-       state->face_cull = cull;
+       get_state().face_cull = cull;
 }
 
 void Renderer::set_depth_test(const DepthTest *dt)
 {
-       state->depth_test = dt;
+       get_state().depth_test = dt;
 }
 
 void Renderer::set_stencil_test(const StencilTest *st)
 {
-       state->stencil_test = st;
+       get_state().stencil_test = st;
 }
 
 void Renderer::set_blend(const Blend *b)
 {
-       state->blend = b;
+       get_state().blend = b;
 }
 
 void Renderer::set_object_lod_bias(unsigned b)
 {
-       state->object_lod_bias = b;
+       get_state().object_lod_bias = b;
 }
 
 void Renderer::clear(const ClearValue *values)
 {
-       pipeline_state.set_framebuffer(state->framebuffer);
-       pipeline_state.set_viewport(state->viewport);
-       pipeline_state.set_scissor(state->scissor);
+       const State &state = get_state();
+
+       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);
 }
@@ -231,15 +261,17 @@ void Renderer::draw_instanced(const Batch &batch, unsigned count)
 
 void Renderer::resolve_multisample(Framebuffer &target)
 {
-       if(!state->framebuffer)
+       const State &state = get_state();
+
+       if(!state.framebuffer)
                throw invalid_operation("Renderer::resolve_multisample");
 
-       unsigned width = state->framebuffer->get_width();
-       unsigned height = state->framebuffer->get_height();
+       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");
 
-       pipeline_state.set_framebuffer(state->framebuffer);
+       pipeline_state.set_framebuffer(state.framebuffer);
        commands.use_pipeline(&pipeline_state);
        commands.resolve_multisample(target);
 }
@@ -256,20 +288,22 @@ void Renderer::end_query(const QueryPool &pool, unsigned index)
 
 void Renderer::apply_state()
 {
-       if(!state->shprog)
+       const State &state = get_state();
+
+       if(!state.shprog)
                throw invalid_operation("Renderer::apply_state");
 
-       pipeline_state.set_framebuffer(state->framebuffer);
-       pipeline_state.set_viewport(state->viewport);
-       pipeline_state.set_scissor(state->scissor);
+       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);
+       bool shprog_changed = (state.shprog!=pipeline_state.get_shader_program());
+       pipeline_state.set_shader_program(state.shprog);
 
        if(changed&MATRIX)
        {
-               standard_shdata.uniform("world_obj_matrix", state->model_matrix);
-               LinAl::SquareMatrix<float, 3> nm = state->model_matrix.block<3, 3>(0, 0);
+               standard_shdata.uniform("world_obj_matrix", state.model_matrix);
+               LinAl::SquareMatrix<float, 3> nm = state.model_matrix.block<3, 3>(0, 0);
                nm = transpose(invert(nm));
                standard_shdata.uniform("world_obj_normal_matrix", nm);
                changed &= ~MATRIX;
@@ -278,47 +312,47 @@ void Renderer::apply_state()
        bool shdata_changed = changed&SHADER_DATA;
        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);
+       bool extra_shdata = (shdata_stack.size()>state.shdata_count);
 
        if(shdata_changed || shprog_changed || extra_shdata)
        {
                if(extra_shdata)
-                       shdata_stack.erase(shdata_stack.begin()+state->shdata_count, shdata_stack.end());
+                       shdata_stack.erase(shdata_stack.begin()+state.shdata_count, shdata_stack.end());
                for(const BoundProgramData &d: shdata_stack)
                {
-                       d.shdata->apply(*state->shprog, pipeline_state);
+                       d.shdata->apply(*state.shprog, pipeline_state);
                        d.generation = d.shdata->get_generation();
                }
                changed &= ~SHADER_DATA;
        }
 
-       if(state->vertex_setup)
+       if(state.vertex_setup)
        {
-               if(const VertexArray *array = state->vertex_setup->get_vertex_array())
+               if(const VertexArray *array = state.vertex_setup->get_vertex_array())
                        array->refresh();
-               if(const VertexArray *array = state->vertex_setup->get_instance_array())
+               if(const VertexArray *array = state.vertex_setup->get_instance_array())
                        array->refresh();
        }
-       pipeline_state.set_vertex_setup(state->vertex_setup);
+       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_front_face(state.front_face);
+       pipeline_state.set_face_cull(state.face_cull);
 
-       if(state->texture_count<texture_stack.size())
+       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);
+                               t.binding = state.shprog->get_uniform_binding(t.tag);
                        if(t.binding>=0)
                                pipeline_state.set_texture(t.binding, t.texture, t.sampler);
                }
 
-       pipeline_state.set_depth_test(state->depth_test);
-       pipeline_state.set_stencil_test(state->stencil_test);
-       pipeline_state.set_blend(state->blend);
+       pipeline_state.set_depth_test(state.depth_test);
+       pipeline_state.set_stencil_test(state.stencil_test);
+       pipeline_state.set_blend(state.blend);
 }
 
 
index 0371e8374a6a9207faf913f13c16f653cf75fac1..4c24068e03cb7a4e792c020575c997623423d346 100644 (file)
@@ -100,7 +100,7 @@ private:
 
        unsigned char changed = 0;
        std::vector<State> state_stack;
-       State *state;
+       State *current_state = 0;
        ProgramData standard_shdata;
        std::vector<BoundProgramData> shdata_stack;
        std::vector<BoundTexture> texture_stack;
@@ -111,10 +111,11 @@ public:
        Renderer();
        ~Renderer();
 
+       /** Begins rendering, allowing commands to be issued. */
+       void begin();
 
-       /** Unbinds all objects and resets related state.  There must be no unpopped
-       state in the stack.  The Renderer remains valid and may be reused for
-       further rendering. */
+       /** Ends rendering.  Any global state is reset to defaults.  No further
+       commands are allowed before the next call to begin(). */
        void end();
 
        /** Saves the current state so it can be restored later. */
@@ -124,10 +125,14 @@ public:
        push_state call. */
        void pop_state();
 
+private:
+       State &get_state() const;
+
+public:
        /** Sets the camera to render from.  The model matrix is reset to identity. */
        void set_camera(const Camera &);
 
-       const Camera *get_camera() const { return state->camera; }
+       const Camera *get_camera() const { return get_state().camera; }
 
        /** Replaces the Renderer's model matrix. */
        void set_matrix(const Matrix &);
@@ -136,13 +141,13 @@ public:
        void transform(const Matrix &);
 
        /** Returns the current model matrix. */
-       const Matrix &get_matrix() const { return state->model_matrix; }
+       const Matrix &get_matrix() const { return get_state().model_matrix; }
 
        void set_framebuffer(const Framebuffer *);
        void set_viewport(const Rect *);
        void set_scissor(const Rect *);
 
-       const Framebuffer *get_framebuffer() const { return state->framebuffer; }
+       const Framebuffer *get_framebuffer() const { return get_state().framebuffer; }
 
        /** Sets the shader program to use.  As a convenience, uniform values may be
        specified at the same time. */
@@ -169,7 +174,7 @@ public:
        void set_blend(const Blend *);
 
        void set_object_lod_bias(unsigned);
-       unsigned get_object_lod_bias() const { return state->object_lod_bias; }
+       unsigned get_object_lod_bias() const { return get_state().object_lod_bias; }
 
        void clear(const ClearValue *);
 
index d57c8530719a494d67c7c106d750d248559ec311..411c277485453c18a6b7fd5342ca63274b13cb7c 100644 (file)
@@ -27,6 +27,7 @@ void View::render()
 {
        if(!internal_renderer)
                internal_renderer = new Renderer;
+       internal_renderer->begin();
        render(*internal_renderer);
        internal_renderer->end();
 }