From: Mikko Rasa Date: Fri, 12 Nov 2021 19:16:55 +0000 (+0200) Subject: Require explicit begin() and end() calls in Renderer X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=904ca5cf3923da3b1e6f68af941e7fc9ad6b7ed4;p=libs%2Fgl.git Require explicit begin() and end() calls in Renderer --- diff --git a/source/materials/pbrmaterial.cpp b/source/materials/pbrmaterial.cpp index e01bbb9d..ebd3321f 100644 --- a/source/materials/pbrmaterial.cpp +++ b/source/materials/pbrmaterial.cpp @@ -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; } diff --git a/source/render/renderer.cpp b/source/render/renderer.cpp index 3b28d854..d53d7145 100644 --- a/source/render/renderer.cpp +++ b/source/render/renderer.cpp @@ -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_countshdata_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; itexture_count; ++i) - if(texture_stack[i].replaced>=static_cast(state->texture_count)) + const State &state = get_state(); + + for(unsigned i=0; i=static_cast(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 nm = state->model_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("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_countshprog->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); } diff --git a/source/render/renderer.h b/source/render/renderer.h index 0371e837..4c24068e 100644 --- a/source/render/renderer.h +++ b/source/render/renderer.h @@ -100,7 +100,7 @@ private: unsigned char changed = 0; std::vector state_stack; - State *state; + State *current_state = 0; ProgramData standard_shdata; std::vector shdata_stack; std::vector 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 *); diff --git a/source/render/view.cpp b/source/render/view.cpp index d57c8530..411c2774 100644 --- a/source/render/view.cpp +++ b/source/render/view.cpp @@ -27,6 +27,7 @@ void View::render() { if(!internal_renderer) internal_renderer = new Renderer; + internal_renderer->begin(); render(*internal_renderer); internal_renderer->end(); }