]> git.tdb.fi Git - libs/gl.git/blobdiff - source/batch.cpp
Some refactoring of the draw code path
[libs/gl.git] / source / batch.cpp
index 102b6487281244457950b6e0625b262e2ac3408e..a59fad99f03b988309ef60042324df71ebf3c6ab 100644 (file)
@@ -1,19 +1,54 @@
-/* $Id$
-
-This file is part of libmspgl
-Copyright © 2007-2010  Mikko Rasa, Mikkosoft Productions
-Distributed under the LGPL
-*/
-
+#include <msp/gl/extensions/ext_draw_range_elements.h>
+#include <msp/gl/extensions/msp_legacy_features.h>
+#include <msp/gl/extensions/msp_primitive_restart.h>
+#include <msp/gl/extensions/nv_primitive_restart.h>
 #include "batch.h"
 #include "bindable.h"
 #include "buffer.h"
-#include "extension.h"
-#include "nv_primitive_restart.h"
+#include "error.h"
+#include "mesh.h"
 #include "vertexarray.h"
 
 using namespace std;
 
+namespace {
+
+template<typename T>
+void append(vector<unsigned char> &data, T i)
+{
+       data.insert(data.end(), sizeof(T), 0);
+       *(T *)(&data[data.size()-sizeof(T)]) = i;
+}
+
+template<typename T, typename U>
+U convert(T n)
+{
+       if(!static_cast<T>(~n))
+               return ~0;
+       else
+               return n;
+}
+
+template<typename T, typename U>
+void expand(vector<unsigned char> &data)
+{
+       unsigned count = data.size()/sizeof(T);
+       data.resize(count*sizeof(U));
+       for(unsigned i=count; i--;)
+               *(U *)(&data[i*sizeof(U)]) = convert<T, U>(*(T *)(&data[i*sizeof(T)]));
+}
+
+template<typename T, typename U>
+void shrink(vector<unsigned char> &data)
+{
+       unsigned count = data.size()/sizeof(T);
+       for(unsigned i=0; i<count; ++i)
+               *(U *)(&data[i*sizeof(U)]) = convert<T, U>(*(T *)(&data[i*sizeof(T)]));
+       data.resize(count*sizeof(U));
+}
+
+}
+
 namespace Msp {
 namespace GL {
 
@@ -24,160 +59,101 @@ Batch::Batch(PrimitiveType t):
        data_type(UNSIGNED_BYTE),
        min_index(0),
        max_index(0),
-       restart(false),
-       ibuf(0),
-       ibuf_offset(0),
-       next_in_ibuf(0),
-       prev_in_ibuf(0),
-       dirty(false)
+       restart(false)
 { }
 
 Batch::~Batch()
 {
-       unlink_from_ibuf();
 }
 
 void Batch::set_data_type(DataType t)
 {
        if(t!=UNSIGNED_BYTE && t!=UNSIGNED_SHORT && t!=UNSIGNED_INT)
-               throw InvalidParameterValue("Batch data type must be an unsigned integer");
+               throw invalid_argument("Batch::set_data_type");
        if(t==UNSIGNED_BYTE && max_index>0xFE)
-               throw InvalidState("UNSIGNED_BYTE can't hold all indices in Batch");
+               throw invalid_operation("Batch::set_data_type");
        else if(t==UNSIGNED_SHORT && max_index>0xFFFE)
-               throw InvalidState("UNSIGNED_SHORT can't hold all indices in Batch");
+               throw invalid_operation("Batch::set_data_type");
 
        if(data_type==UNSIGNED_BYTE && t==UNSIGNED_SHORT)
-               expand_data<unsigned char, unsigned short>();
+               expand<unsigned char, unsigned short>(data);
        else if(data_type==UNSIGNED_BYTE && t==UNSIGNED_INT)
-               expand_data<unsigned char, unsigned>();
+               expand<unsigned char, unsigned>(data);
        else if(data_type==UNSIGNED_SHORT && t==UNSIGNED_INT)
-               expand_data<unsigned short, unsigned>();
+               expand<unsigned short, unsigned>(data);
        else if(data_type==UNSIGNED_INT && t==UNSIGNED_BYTE)
-               shrink_data<unsigned, unsigned char>();
+               shrink<unsigned, unsigned char>(data);
        else if(data_type==UNSIGNED_INT && t==UNSIGNED_SHORT)
-               shrink_data<unsigned, unsigned short>();
+               shrink<unsigned, unsigned short>(data);
        else if(data_type==UNSIGNED_SHORT && t==UNSIGNED_BYTE)
-               shrink_data<unsigned short, unsigned char>();
+               shrink<unsigned short, unsigned char>(data);
 
        data_type = t;
-       update_ibuf_offsets();
-       dirty = true;
-}
-
-void Batch::use_index_buffer(Buffer *buf, Batch *prev)
-{
-       if(buf && prev && prev->ibuf!=buf)
-               throw InvalidParameterValue("Previous batch is not in the same buffer");
-
-       if(!buf)
-       {
-               prev = 0;
-               unlink_from_ibuf();
-       }
-
-       ibuf = buf;
-       prev_in_ibuf = prev;
-       next_in_ibuf = 0;
-       if(prev)
-       {
-               prev->next_in_ibuf = this;
-               ibuf_offset = prev->ibuf_offset+prev->data.size();
-       }
-       else
-               ibuf_offset = 0;
-
+       update_offset();
        dirty = true;
 }
 
 Batch &Batch::append(unsigned i)
 {
-       if(data.empty())
-               min_index = max_index = i;
-       else
-       {
-               min_index = min(min_index, i);
-               max_index = max(max_index, i);
-       }
+       append_index(i);
 
-       if((data_type==UNSIGNED_BYTE || data_type==UNSIGNED_SHORT) && max_index>0xFFFE)
-               set_data_type(UNSIGNED_INT);
-       else if(data_type==UNSIGNED_BYTE && max_index>0xFE)
-               set_data_type(UNSIGNED_SHORT);
-
-       if(data_type==UNSIGNED_SHORT)
-               append_index<unsigned short>(i);
-       else if(data_type==UNSIGNED_INT)
-               append_index<unsigned>(i);
-       else
-               data.push_back(i);
-       
-       update_ibuf_offsets();
+       update_offset();
        dirty = true;
 
        return *this;
 }
 
-void Batch::append(const vector<unsigned> &ind)
+Batch &Batch::append(const vector<unsigned> &ind)
 {
        if(ind.empty())
-               return;
-
-       if(data.empty())
-               min_index = max_index = ind.front();
+               return *this;
 
+       data.reserve(data.size()+ind.size()*get_index_size());
        for(vector<unsigned>::const_iterator i=ind.begin(); i!=ind.end(); ++i)
-       {
-               min_index = min(min_index, *i);
-               max_index = max(max_index, *i);
-       }
+               append_index(*i);
 
-       if((data_type==UNSIGNED_BYTE || data_type==UNSIGNED_SHORT) && max_index>0xFFFE)
-               set_data_type(UNSIGNED_INT);
-       else if(data_type==UNSIGNED_BYTE && max_index>0xFE)
-               set_data_type(UNSIGNED_SHORT);
+       update_offset();
+       dirty = true;
 
-       unsigned base = data.size();
-       data.resize(data.size()+ind.size()*get_index_size());
-       if(data_type==UNSIGNED_SHORT)
-       {
-               unsigned short *ptr = reinterpret_cast<unsigned short *>(&data[base]);
-               for(unsigned i=0; i<ind.size(); ++i)
-                       ptr[i] = ind[i];
-       }
-       else if(data_type==UNSIGNED_INT)
-       {
-               unsigned *ptr = reinterpret_cast<unsigned *>(&data[base]);
-               for(unsigned i=0; i<ind.size(); ++i)
-                       ptr[i] = ind[i];
-       }
+       return *this;
+}
+
+bool Batch::can_append(PrimitiveType other_type)
+{
+       if(other_type!=prim_type)
+               return false;
+       else if(prim_type==LINE_STRIP || prim_type==LINE_LOOP || prim_type==TRIANGLE_FAN)
+               return MSP_primitive_restart || NV_primitive_restart;
        else
-       {
-               for(unsigned i=0; i<ind.size(); ++i)
-                       data[base+i] = ind[i];
-       }
+               return true;
 }
 
-void Batch::append(const Batch &other)
+Batch &Batch::append(const Batch &other)
 {
        if(other.prim_type!=prim_type)
-               throw InvalidParameterValue("Can't concatenate batches with different primitive types");
-       if(prim_type==LINE_STRIP || prim_type==LINE_LOOP)
-               throw InvalidState("Can't concatenate line strips or loops");
-       else if(prim_type==POLYGON)
-               throw InvalidState("Can't concatenate polygons");
-       else if(prim_type==TRIANGLE_FAN)
-               static RequireExtension _ext("GL_NV_primitive_restart");
+               throw invalid_argument("Batch::append");
+       if(prim_type==LINE_STRIP || prim_type==LINE_LOOP || prim_type==TRIANGLE_FAN)
+       {
+               if(!MSP_primitive_restart)
+               {
+                       static Require _req(NV_primitive_restart);
+                       // Make sure we have glEnable/DisableClientState as well
+                       static Require _req2(MSP_legacy_features);
+               }
+       }
 
        if(other.data.empty())
-               return;
+               return *this;
 
-       if(is_supported("GL_NV_primitive_restart"))
+       if(prim_type==POINTS || prim_type==LINES || prim_type==TRIANGLES || prim_type==QUADS)
+               ;
+       else if(MSP_primitive_restart || NV_primitive_restart)
        {
                restart = true;
                if(data_type==UNSIGNED_SHORT)
-                       append_index<unsigned short>(0xFFFF);
+                       ::append<unsigned short>(data, 0xFFFF);
                else if(data_type==UNSIGNED_INT)
-                       append_index<unsigned>(0xFFFFFFFF);
+                       ::append<unsigned>(data, 0xFFFFFFFF);
                else
                        data.push_back(0xFF);
        }
@@ -198,62 +174,35 @@ void Batch::append(const Batch &other)
 
        unsigned count = other.size();
        for(unsigned i=0; i<count; ++i)
-               append(other.get_index(i));
+               append_index(other.get_index(i));
+
+       update_offset();
+       dirty = true;
+
+       return *this;
 }
 
-void Batch::draw() const
+void Batch::append_index(unsigned i)
 {
-       if(restart)
-       {
-               unsigned index;
-               if(data_type==UNSIGNED_SHORT)
-                       index = 0xFFFF;
-               else if(data_type==UNSIGNED_INT)
-                       index = 0xFFFFFFFF;
-               else
-                       index = 0xFF;
-
-               if(index!=restart_index)
-               {
-                       if(!restart_index)
-                               glEnableClientState(GL_PRIMITIVE_RESTART_NV);
-                       glPrimitiveRestartIndexNV(index);
-                       restart_index = index;
-               }
-       }
-       else if(restart_index && restart_index<max_index)
+       if(data.empty())
+               min_index = max_index = i;
+       else
        {
-               glDisableClientState(GL_PRIMITIVE_RESTART_NV);
-               restart_index = 0;
+               min_index = min(min_index, i);
+               max_index = max(max_index, i);
        }
 
-       if(ibuf)
-       {
-               if(dirty)
-               {
-                       const Batch *b = this;
-                       for(; b->prev_in_ibuf; b=b->prev_in_ibuf) ;
-
-                       unsigned chain_size = 0;
-                       for(const Batch *a=b; a; a=a->next_in_ibuf)
-                               chain_size += a->data.size();
-
-                       ibuf->data(chain_size, 0);
-
-                       for(; b; b=b->next_in_ibuf)
-                       {
-                               ibuf->sub_data(b->ibuf_offset, b->data.size(), &b->data[0]);
-                               b->dirty = false;
-                       }
-               }
-
-               BufferAlias<ELEMENT_ARRAY_BUFFER> alias(*ibuf);
-               Bind bind_ibuf(alias, true);
+       if((data_type==UNSIGNED_BYTE || data_type==UNSIGNED_SHORT) && max_index>0xFFFE)
+               set_data_type(UNSIGNED_INT);
+       else if(data_type==UNSIGNED_BYTE && max_index>0xFE)
+               set_data_type(UNSIGNED_SHORT);
 
-               glDrawRangeElements(prim_type, min_index, max_index, size(), data_type, (void *)ibuf_offset);
-       }
+       if(data_type==UNSIGNED_SHORT)
+               ::append<unsigned short>(data, i);
+       else if(data_type==UNSIGNED_INT)
+               ::append<unsigned>(data, i);
        else
-               glDrawRangeElements(prim_type, min_index, max_index, size(), data_type, &data[0]);
+               data.push_back(i);
 }
 
 unsigned Batch::get_index_size() const
@@ -265,13 +214,6 @@ unsigned Batch::get_index_size() const
        return sizeof(unsigned char);
 }
 
-template<typename T>
-void Batch::append_index(T i)
-{
-       data.insert(data.end(), sizeof(T), 0);
-       *(T *)(&data[data.size()-sizeof(T)]) = i;
-}
-
 unsigned Batch::get_index(unsigned i) const
 {
        if(data_type==UNSIGNED_SHORT)
@@ -282,53 +224,72 @@ unsigned Batch::get_index(unsigned i) const
                return data[i];
 }
 
-template<typename T, typename U>
-void Batch::expand_data()
+void Batch::draw() const
 {
-       unsigned count = data.size()/sizeof(T);
-       data.resize(count*sizeof(U));
-       for(unsigned i=count; i--;)
-               *(U *)(&data[i*sizeof(U)]) = convert<T, U>(*(T *)(&data[i*sizeof(T)]));
-}
+       BindRestore _bind_ibuf(get_buffer(), ELEMENT_ARRAY_BUFFER);
+       const void *data_ptr = setup_draw();
 
-template<typename T, typename U>
-void Batch::shrink_data()
-{
-       unsigned count = data.size()/sizeof(T);
-       for(unsigned i=0; i<count; ++i)
-               *(U *)(&data[i*sizeof(U)]) = convert<T, U>(*(T *)(&data[i*sizeof(T)]));
-       data.resize(count*sizeof(U));
+       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);
 }
 
-template<typename T, typename U>
-U Batch::convert(T i) const
+const void *Batch::setup_draw() const
 {
-       if(!static_cast<T>(~i))
-               return ~0;
+       if(restart)
+       {
+               unsigned index;
+               if(data_type==UNSIGNED_SHORT)
+                       index = 0xFFFF;
+               else if(data_type==UNSIGNED_INT)
+                       index = 0xFFFFFFFF;
+               else
+                       index = 0xFF;
+
+               if(index!=restart_index)
+                       set_restart_index(index);
+       }
+       else if(restart_index && restart_index<=max_index)
+               set_restart_index(0);
+
+       if(get_buffer())
+       {
+               if(dirty)
+                       update_buffer();
+
+               return reinterpret_cast<const void *>(get_offset());
+       }
        else
-               return i;
+               return &data[0];
 }
 
-void Batch::unlink_from_ibuf()
+void Batch::set_restart_index(unsigned index)
 {
-       if(next_in_ibuf)
-               next_in_ibuf->prev_in_ibuf = prev_in_ibuf;
-       if(prev_in_ibuf)
+       if(MSP_primitive_restart)
        {
-               prev_in_ibuf->next_in_ibuf = next_in_ibuf;
-               prev_in_ibuf->update_ibuf_offsets();
+               if(index>0)
+               {
+                       if(!restart_index)
+                               glEnable(GL_PRIMITIVE_RESTART);
+                       glPrimitiveRestartIndex(index);
+               }
+               else
+                       glDisable(GL_PRIMITIVE_RESTART);
        }
-       else if(next_in_ibuf)
+       else
        {
-               next_in_ibuf->ibuf_offset = 0;
-               next_in_ibuf->update_ibuf_offsets();
+               if(index>0)
+               {
+                       if(!restart_index)
+                               glEnableClientState(GL_PRIMITIVE_RESTART_NV);
+                       glPrimitiveRestartIndexNV(index);
+               }
+               else
+                       glDisableClientState(GL_PRIMITIVE_RESTART_NV);
        }
-}
 
-void Batch::update_ibuf_offsets()
-{
-       for(Batch *b=this; b->next_in_ibuf; b=b->next_in_ibuf)
-               b->next_in_ibuf->ibuf_offset = b->ibuf_offset+b->data.size();
+       restart_index = index;
 }