Some refactoring of the draw code path
authorMikko Rasa <tdb@tdb.fi>
Wed, 19 Jun 2019 23:01:43 +0000 (02:01 +0300)
committerMikko Rasa <tdb@tdb.fi>
Wed, 19 Jun 2019 23:24:02 +0000 (02:24 +0300)
These changes make it easier to add instanced versions of the draw calls.

source/batch.cpp
source/batch.h
source/renderer.cpp

index 37e4801ec82a6356a595a4dea62de926bf790d3e..a59fad99f03b988309ef60042324df71ebf3c6ab 100644 (file)
@@ -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<const void *>(get_offset());
+               return reinterpret_cast<const void *>(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)
index 10c61a289e2e5afb71e94c6049126ee4fcbfe877..7212383e01a2e973ac39185592f39c69f3ac76cc 100644 (file)
@@ -66,6 +66,7 @@ public:
 
        void draw() const;
 private:
+       const void *setup_draw() const;
        static void set_restart_index(unsigned);
 };
 
index e59949a3683f938797251a4a5d3f34a7ebbddc46..53a3f45237463e5fffed3e084805326453e5fd3a 100644 (file)
@@ -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();