From f50822b9e73a6ecdacbc4af4c4d9aba435a72386 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Thu, 20 Jun 2019 02:01:43 +0300 Subject: [PATCH] Some refactoring of the draw code path These changes make it easier to add instanced versions of the draw calls. --- source/batch.cpp | 25 ++++++++++++++----------- source/batch.h | 1 + source/renderer.cpp | 13 ++++--------- 3 files changed, 19 insertions(+), 20 deletions(-) diff --git a/source/batch.cpp b/source/batch.cpp index 37e4801e..a59fad99 100644 --- a/source/batch.cpp +++ b/source/batch.cpp @@ -225,6 +225,17 @@ unsigned Batch::get_index(unsigned i) const } void Batch::draw() const +{ + BindRestore _bind_ibuf(get_buffer(), ELEMENT_ARRAY_BUFFER); + const void *data_ptr = setup_draw(); + + if(EXT_draw_range_elements) + glDrawRangeElements(prim_type, min_index, max_index, size(), data_type, data_ptr); + else + glDrawElements(prim_type, size(), data_type, data_ptr); +} + +const void *Batch::setup_draw() const { if(restart) { @@ -242,23 +253,15 @@ void Batch::draw() const else if(restart_index && restart_index<=max_index) set_restart_index(0); - const Buffer *ibuf = get_buffer(); - const void *data_ptr; - BindRestore _bind_ibuf(ibuf, ELEMENT_ARRAY_BUFFER); - if(ibuf) + if(get_buffer()) { if(dirty) update_buffer(); - data_ptr = reinterpret_cast(get_offset()); + return reinterpret_cast(get_offset()); } else - data_ptr = &data[0]; - - if(EXT_draw_range_elements) - glDrawRangeElements(prim_type, min_index, max_index, size(), data_type, data_ptr); - else - glDrawElements(prim_type, size(), data_type, data_ptr); + return &data[0]; } void Batch::set_restart_index(unsigned index) diff --git a/source/batch.h b/source/batch.h index 10c61a28..7212383e 100644 --- a/source/batch.h +++ b/source/batch.h @@ -66,6 +66,7 @@ public: void draw() const; private: + const void *setup_draw() const; static void set_restart_index(unsigned); }; diff --git a/source/renderer.cpp b/source/renderer.cpp index e59949a3..53a3f452 100644 --- a/source/renderer.cpp +++ b/source/renderer.cpp @@ -269,15 +269,6 @@ void Renderer::draw(const Batch &batch) { apply_state(); - bool legacy_bindings = (!state->shprog || state->shprog->uses_legacy_variables()); - if(state->mesh && legacy_bindings) - { - if(const Buffer *ibuf = state->mesh->get_index_buffer()) - ibuf->bind_to(ELEMENT_ARRAY_BUFFER); - else - Buffer::unbind_from(ELEMENT_ARRAY_BUFFER); - } - batch.draw(); } @@ -386,6 +377,10 @@ void Renderer::apply_state() { Mesh::unbind(); state->mesh->get_vertices().apply(); + if(const Buffer *ibuf = state->mesh->get_index_buffer()) + ibuf->bind_to(ELEMENT_ARRAY_BUFFER); + else + Buffer::unbind_from(ELEMENT_ARRAY_BUFFER); } else state->mesh->bind(); -- 2.43.0