X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fbatch.cpp;h=bca33484ce976cc585d8da88187c8ba568baed23;hb=c233a90fd05f1f5424d7fca94bb6b999f3061271;hp=1d9b1d4cd4ab7081a3bf95522056f61b91375217;hpb=50175b88e2f189e80881d292dcc32523c5b272fc;p=libs%2Fgl.git diff --git a/source/batch.cpp b/source/batch.cpp index 1d9b1d4c..bca33484 100644 --- a/source/batch.cpp +++ b/source/batch.cpp @@ -1,9 +1,10 @@ +#include +#include #include "batch.h" #include "bindable.h" #include "buffer.h" #include "error.h" -#include "ext_draw_range_elements.h" -#include "nv_primitive_restart.h" +#include "mesh.h" #include "vertexarray.h" using namespace std; @@ -56,12 +57,7 @@ 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) { /* XXX Should probably provide a fallback to glDrawElements since this class is pretty much required to render anything. */ @@ -70,7 +66,6 @@ Batch::Batch(PrimitiveType t): Batch::~Batch() { - unlink_from_ibuf(); } void Batch::set_data_type(DataType t) @@ -96,80 +91,15 @@ void Batch::set_data_type(DataType t) shrink(data); data_type = t; - update_ibuf_offsets(); + update_offset(); dirty = true; } -void Batch::use_index_buffer(Buffer *buf, Batch *prev) -{ - if(buf && prev && prev->ibuf!=buf) - throw invalid_argument("Batch::use_index_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; - - dirty = true; -} - -void Batch::unlink_from_ibuf() -{ - if(next_in_ibuf) - next_in_ibuf->prev_in_ibuf = prev_in_ibuf; - if(prev_in_ibuf) - { - prev_in_ibuf->next_in_ibuf = next_in_ibuf; - prev_in_ibuf->update_ibuf_offsets(); - } - else if(next_in_ibuf) - { - next_in_ibuf->ibuf_offset = 0; - next_in_ibuf->update_ibuf_offsets(); - } -} - -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(); -} - 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); - } - - 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); + append_index(i); - if(data_type==UNSIGNED_SHORT) - ::append(data, i); - else if(data_type==UNSIGNED_INT) - ::append(data, i); - else - data.push_back(i); - - update_ibuf_offsets(); + update_offset(); dirty = true; return *this; @@ -180,56 +110,37 @@ void Batch::append(const vector &ind) if(ind.empty()) return; - if(data.empty()) - min_index = max_index = ind.front(); - + data.reserve(data.size()+ind.size()*get_index_size()); for(vector::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(&data[base]); - for(unsigned i=0; i(&data[base]); - for(unsigned i=0; i0xFFFE) + 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(data, i); + else if(data_type==UNSIGNED_INT) + ::append(data, i); + else + data.push_back(i); +} + +void Batch::upload_data() const +{ + get_buffer()->sub_data(get_offset(), data.size(), &data[0]); } unsigned Batch::get_index_size() const @@ -298,36 +240,33 @@ void Batch::draw() const restart_index = index; } } - else if(restart_index && restart_indexprev_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; - } + old_ibuf = Buffer::current(ELEMENT_ARRAY_BUFFER); + ibuf->bind_to(ELEMENT_ARRAY_BUFFER); } - BufferAlias alias(*ibuf); - Bind bind_ibuf(alias, true); + if(dirty) + update_buffer(); - glDrawRangeElements(prim_type, min_index, max_index, size(), data_type, reinterpret_cast(ibuf_offset)); + glDrawRangeElements(prim_type, min_index, max_index, size(), data_type, reinterpret_cast(get_offset())); + if(!have_vao) + { + if(old_ibuf) + old_ibuf->bind_to(ELEMENT_ARRAY_BUFFER); + else + Buffer::unbind_from(ELEMENT_ARRAY_BUFFER); + } } else glDrawRangeElements(prim_type, min_index, max_index, size(), data_type, &data[0]);