]> git.tdb.fi Git - libs/gl.git/commitdiff
Avoid unnecessary work in Batch's vector append
authorMikko Rasa <tdb@tdb.fi>
Tue, 11 Jan 2011 19:55:33 +0000 (19:55 +0000)
committerMikko Rasa <tdb@tdb.fi>
Tue, 11 Jan 2011 19:55:33 +0000 (19:55 +0000)
Make Immediate use Batch for drawing

source/batch.cpp
source/batch.h
source/immediate.cpp
source/immediate.h

index 228ff20bf7a0d395d6fbec42c3cf938d13dbd3fa..102b6487281244457950b6e0625b262e2ac3408e 100644 (file)
@@ -119,9 +119,42 @@ Batch &Batch::append(unsigned i)
 
 void Batch::append(const vector<unsigned> &ind)
 {
-       data.reserve(data.size()+ind.size()*get_index_size());
+       if(ind.empty())
+               return;
+
+       if(data.empty())
+               min_index = max_index = ind.front();
+
        for(vector<unsigned>::const_iterator i=ind.begin(); i!=ind.end(); ++i)
-               append(*i);
+       {
+               min_index = min(min_index, *i);
+               max_index = max(max_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);
+
+       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];
+       }
+       else
+       {
+               for(unsigned i=0; i<ind.size(); ++i)
+                       data[base+i] = ind[i];
+       }
 }
 
 void Batch::append(const Batch &other)
index c3e9848e8ae4a245c33498c92051c47399b7f425..696891ba3047ce1a557c9f36ddeee64b82c17882 100644 (file)
@@ -66,6 +66,7 @@ public:
        unsigned size() const { return data.size()/get_index_size(); }
        unsigned get_index(unsigned) const;
        void draw() const;
+
 private:
        unsigned get_index_size() const;
 
@@ -80,7 +81,7 @@ private:
 
        template<typename T, typename U>
        U convert(T) const;
-private:
+
        void unlink_from_ibuf();
        void update_ibuf_offsets();
 };
index 3d808d320897d9c1610ebf2da3650d56e7859d94..15d3a634a13248063efd29c2f5889fff7110c120 100644 (file)
@@ -5,6 +5,7 @@ Copyright © 2007  Mikko Rasa, Mikkosoft Productions
 Distributed under the LGPL
 */
 
+#include "batch.h"
 #include "immediate.h"
 
 namespace Msp {
@@ -23,15 +24,19 @@ void Immediate::reset()
                throw InvalidState("Can't reset Immediate between begin() and end()");
 
        array.clear();
+}
+
+void Immediate::begin_()
+{
        indices.clear();
 }
 
 void Immediate::end_()
 {
+       Batch batch(type);
+       batch.append(indices);
        array.apply();
-       draw_elements(type, indices.size(), &indices[0]);
-
-       indices.clear();
+       batch.draw();
 }
 
 void Immediate::element_(unsigned i)
index 89380c41310c3717a20db88b35d9b8ad95b22cc2..6822aba2640f99e543c0d044f21b6409d841b0fa 100644 (file)
@@ -30,7 +30,7 @@ public:
        Immediate(VertexFormat);
        void reset();
 private:
-       virtual void begin_() { }
+       virtual void begin_();
        virtual void end_();
        virtual void element_(unsigned);
 };