1 #include <msp/gl/extensions/arb_draw_instanced.h>
2 #include <msp/gl/extensions/ext_draw_range_elements.h>
3 #include <msp/gl/extensions/msp_primitive_restart.h>
9 #include "vertexarray.h"
16 void append(vector<unsigned char> &data, T i)
18 data.insert(data.end(), sizeof(T), 0);
19 *(T *)(&data[data.size()-sizeof(T)]) = i;
22 template<typename T, typename U>
25 if(!static_cast<T>(~n))
31 template<typename T, typename U>
32 void expand(vector<unsigned char> &data)
34 unsigned count = data.size()/sizeof(T);
35 data.resize(count*sizeof(U));
36 for(unsigned i=count; i--;)
37 *(U *)(&data[i*sizeof(U)]) = convert<T, U>(*(T *)(&data[i*sizeof(T)]));
40 template<typename T, typename U>
41 void shrink(vector<unsigned char> &data)
43 unsigned count = data.size()/sizeof(T);
44 for(unsigned i=0; i<count; ++i)
45 *(U *)(&data[i*sizeof(U)]) = convert<T, U>(*(T *)(&data[i*sizeof(T)]));
46 data.resize(count*sizeof(U));
54 unsigned Batch::restart_index = 0;
56 Batch::Batch(PrimitiveType t):
58 data_type(UNSIGNED_BYTE),
68 void Batch::set_data_type(DataType t)
70 if(t!=UNSIGNED_BYTE && t!=UNSIGNED_SHORT && t!=UNSIGNED_INT)
71 throw invalid_argument("Batch::set_data_type");
72 if(t==UNSIGNED_BYTE && max_index>0xFE)
73 throw invalid_operation("Batch::set_data_type");
74 else if(t==UNSIGNED_SHORT && max_index>0xFFFE)
75 throw invalid_operation("Batch::set_data_type");
77 if(data_type==UNSIGNED_BYTE && t==UNSIGNED_SHORT)
78 expand<unsigned char, unsigned short>(data);
79 else if(data_type==UNSIGNED_BYTE && t==UNSIGNED_INT)
80 expand<unsigned char, unsigned>(data);
81 else if(data_type==UNSIGNED_SHORT && t==UNSIGNED_INT)
82 expand<unsigned short, unsigned>(data);
83 else if(data_type==UNSIGNED_INT && t==UNSIGNED_BYTE)
84 shrink<unsigned, unsigned char>(data);
85 else if(data_type==UNSIGNED_INT && t==UNSIGNED_SHORT)
86 shrink<unsigned, unsigned short>(data);
87 else if(data_type==UNSIGNED_SHORT && t==UNSIGNED_BYTE)
88 shrink<unsigned short, unsigned char>(data);
95 Batch &Batch::append(unsigned i)
105 Batch &Batch::append(const vector<unsigned> &ind)
110 data.reserve(data.size()+ind.size()*get_index_size());
111 for(vector<unsigned>::const_iterator i=ind.begin(); i!=ind.end(); ++i)
120 bool Batch::can_append(PrimitiveType other_type)
122 if(other_type!=prim_type)
124 else if(prim_type==LINE_STRIP || prim_type==LINE_LOOP || prim_type==TRIANGLE_FAN)
125 return MSP_primitive_restart;
130 Batch &Batch::append(const Batch &other)
132 if(other.prim_type!=prim_type)
133 throw invalid_argument("Batch::append");
134 if(prim_type==LINE_STRIP || prim_type==LINE_LOOP || prim_type==TRIANGLE_FAN)
135 static Require _req(MSP_primitive_restart);
137 if(other.data.empty())
140 if(prim_type==POINTS || prim_type==LINES || prim_type==TRIANGLES)
142 else if(MSP_primitive_restart)
145 if(data_type==UNSIGNED_SHORT)
146 ::append<unsigned short>(data, 0xFFFF);
147 else if(data_type==UNSIGNED_INT)
148 ::append<unsigned>(data, 0xFFFFFFFF);
150 data.push_back(0xFF);
152 else if(prim_type==TRIANGLE_STRIP)
154 append(get_index(size()-1));
155 append(other.get_index(0));
157 append(other.get_index(0));
160 unsigned count = other.size();
161 for(unsigned i=0; i<count; ++i)
162 append_index(other.get_index(i));
170 void Batch::append_index(unsigned i)
173 min_index = max_index = i;
176 min_index = min(min_index, i);
177 max_index = max(max_index, i);
180 if((data_type==UNSIGNED_BYTE || data_type==UNSIGNED_SHORT) && max_index>0xFFFE)
181 set_data_type(UNSIGNED_INT);
182 else if(data_type==UNSIGNED_BYTE && max_index>0xFE)
183 set_data_type(UNSIGNED_SHORT);
185 if(data_type==UNSIGNED_SHORT)
186 ::append<unsigned short>(data, i);
187 else if(data_type==UNSIGNED_INT)
188 ::append<unsigned>(data, i);
193 unsigned Batch::get_index_size() const
195 if(data_type==UNSIGNED_SHORT)
196 return sizeof(unsigned short);
197 else if(data_type==UNSIGNED_INT)
198 return sizeof(unsigned);
199 return sizeof(unsigned char);
202 unsigned Batch::get_index(unsigned i) const
204 if(data_type==UNSIGNED_SHORT)
205 return *(unsigned short *)&data[i*sizeof(unsigned short)];
206 else if(data_type==UNSIGNED_INT)
207 return *(unsigned *)&data[i*sizeof(unsigned )];
212 void Batch::draw() const
214 BindRestore _bind_ibuf(get_buffer(), ELEMENT_ARRAY_BUFFER);
215 const void *data_ptr = setup_draw();
217 if(EXT_draw_range_elements)
218 glDrawRangeElements(prim_type, min_index, max_index, size(), data_type, data_ptr);
220 glDrawElements(prim_type, size(), data_type, data_ptr);
223 void Batch::draw_instanced(unsigned count) const
225 static Require req(ARB_draw_instanced);
227 BindRestore _bind_ibuf(get_buffer(), ELEMENT_ARRAY_BUFFER);
228 const void *data_ptr = setup_draw();
230 glDrawElementsInstanced(prim_type, size(), data_type, data_ptr, count);
233 const void *Batch::setup_draw() const
238 if(data_type==UNSIGNED_SHORT)
240 else if(data_type==UNSIGNED_INT)
245 if(index!=restart_index)
246 set_restart_index(index);
248 else if(restart_index && restart_index<=max_index)
249 set_restart_index(0);
256 return reinterpret_cast<const void *>(get_offset());
262 void Batch::set_restart_index(unsigned index)
267 glEnable(GL_PRIMITIVE_RESTART);
268 glPrimitiveRestartIndex(index);
271 glDisable(GL_PRIMITIVE_RESTART);
273 restart_index = index;
277 Batch::Loader::Loader(Batch &b):
278 DataFile::ObjectLoader<Batch>(b)
280 add("indices", &Loader::indices);
283 void Batch::Loader::indices(const vector<unsigned> &ind)