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_legacy_features.h>
4 #include <msp/gl/extensions/msp_primitive_restart.h>
5 #include <msp/gl/extensions/nv_primitive_restart.h>
11 #include "vertexarray.h"
18 void append(vector<unsigned char> &data, T i)
20 data.insert(data.end(), sizeof(T), 0);
21 *(T *)(&data[data.size()-sizeof(T)]) = i;
24 template<typename T, typename U>
27 if(!static_cast<T>(~n))
33 template<typename T, typename U>
34 void expand(vector<unsigned char> &data)
36 unsigned count = data.size()/sizeof(T);
37 data.resize(count*sizeof(U));
38 for(unsigned i=count; i--;)
39 *(U *)(&data[i*sizeof(U)]) = convert<T, U>(*(T *)(&data[i*sizeof(T)]));
42 template<typename T, typename U>
43 void shrink(vector<unsigned char> &data)
45 unsigned count = data.size()/sizeof(T);
46 for(unsigned i=0; i<count; ++i)
47 *(U *)(&data[i*sizeof(U)]) = convert<T, U>(*(T *)(&data[i*sizeof(T)]));
48 data.resize(count*sizeof(U));
56 unsigned Batch::restart_index = 0;
58 Batch::Batch(PrimitiveType t):
60 data_type(UNSIGNED_BYTE),
70 void Batch::set_data_type(DataType t)
72 if(t!=UNSIGNED_BYTE && t!=UNSIGNED_SHORT && t!=UNSIGNED_INT)
73 throw invalid_argument("Batch::set_data_type");
74 if(t==UNSIGNED_BYTE && max_index>0xFE)
75 throw invalid_operation("Batch::set_data_type");
76 else if(t==UNSIGNED_SHORT && max_index>0xFFFE)
77 throw invalid_operation("Batch::set_data_type");
79 if(data_type==UNSIGNED_BYTE && t==UNSIGNED_SHORT)
80 expand<unsigned char, unsigned short>(data);
81 else if(data_type==UNSIGNED_BYTE && t==UNSIGNED_INT)
82 expand<unsigned char, unsigned>(data);
83 else if(data_type==UNSIGNED_SHORT && t==UNSIGNED_INT)
84 expand<unsigned short, unsigned>(data);
85 else if(data_type==UNSIGNED_INT && t==UNSIGNED_BYTE)
86 shrink<unsigned, unsigned char>(data);
87 else if(data_type==UNSIGNED_INT && t==UNSIGNED_SHORT)
88 shrink<unsigned, unsigned short>(data);
89 else if(data_type==UNSIGNED_SHORT && t==UNSIGNED_BYTE)
90 shrink<unsigned short, unsigned char>(data);
97 Batch &Batch::append(unsigned i)
107 Batch &Batch::append(const vector<unsigned> &ind)
112 data.reserve(data.size()+ind.size()*get_index_size());
113 for(vector<unsigned>::const_iterator i=ind.begin(); i!=ind.end(); ++i)
122 bool Batch::can_append(PrimitiveType other_type)
124 if(other_type!=prim_type)
126 else if(prim_type==LINE_STRIP || prim_type==LINE_LOOP || prim_type==TRIANGLE_FAN)
127 return MSP_primitive_restart || NV_primitive_restart;
132 Batch &Batch::append(const Batch &other)
134 if(other.prim_type!=prim_type)
135 throw invalid_argument("Batch::append");
136 if(prim_type==LINE_STRIP || prim_type==LINE_LOOP || prim_type==TRIANGLE_FAN)
138 if(!MSP_primitive_restart)
140 static Require _req(NV_primitive_restart);
141 // Make sure we have glEnable/DisableClientState as well
142 static Require _req2(MSP_legacy_features);
146 if(other.data.empty())
149 if(prim_type==POINTS || prim_type==LINES || prim_type==TRIANGLES || prim_type==QUADS)
151 else if(MSP_primitive_restart || NV_primitive_restart)
154 if(data_type==UNSIGNED_SHORT)
155 ::append<unsigned short>(data, 0xFFFF);
156 else if(data_type==UNSIGNED_INT)
157 ::append<unsigned>(data, 0xFFFFFFFF);
159 data.push_back(0xFF);
161 else if(prim_type==TRIANGLE_STRIP)
163 append(get_index(size()-1));
164 append(other.get_index(0));
166 append(other.get_index(0));
168 else if(prim_type==QUAD_STRIP)
170 append(get_index(size()-1));
171 append(get_index(size()-1));
172 append(other.get_index(0));
173 append(other.get_index(0));
176 unsigned count = other.size();
177 for(unsigned i=0; i<count; ++i)
178 append_index(other.get_index(i));
186 void Batch::append_index(unsigned i)
189 min_index = max_index = i;
192 min_index = min(min_index, i);
193 max_index = max(max_index, i);
196 if((data_type==UNSIGNED_BYTE || data_type==UNSIGNED_SHORT) && max_index>0xFFFE)
197 set_data_type(UNSIGNED_INT);
198 else if(data_type==UNSIGNED_BYTE && max_index>0xFE)
199 set_data_type(UNSIGNED_SHORT);
201 if(data_type==UNSIGNED_SHORT)
202 ::append<unsigned short>(data, i);
203 else if(data_type==UNSIGNED_INT)
204 ::append<unsigned>(data, i);
209 unsigned Batch::get_index_size() const
211 if(data_type==UNSIGNED_SHORT)
212 return sizeof(unsigned short);
213 else if(data_type==UNSIGNED_INT)
214 return sizeof(unsigned);
215 return sizeof(unsigned char);
218 unsigned Batch::get_index(unsigned i) const
220 if(data_type==UNSIGNED_SHORT)
221 return *(unsigned short *)&data[i*sizeof(unsigned short)];
222 else if(data_type==UNSIGNED_INT)
223 return *(unsigned *)&data[i*sizeof(unsigned )];
228 void Batch::draw() const
230 BindRestore _bind_ibuf(get_buffer(), ELEMENT_ARRAY_BUFFER);
231 const void *data_ptr = setup_draw();
233 if(EXT_draw_range_elements)
234 glDrawRangeElements(prim_type, min_index, max_index, size(), data_type, data_ptr);
236 glDrawElements(prim_type, size(), data_type, data_ptr);
239 void Batch::draw_instanced(unsigned count) const
241 static Require req(ARB_draw_instanced);
243 BindRestore _bind_ibuf(get_buffer(), ELEMENT_ARRAY_BUFFER);
244 const void *data_ptr = setup_draw();
246 glDrawElementsInstanced(prim_type, size(), data_type, data_ptr, count);
249 const void *Batch::setup_draw() const
254 if(data_type==UNSIGNED_SHORT)
256 else if(data_type==UNSIGNED_INT)
261 if(index!=restart_index)
262 set_restart_index(index);
264 else if(restart_index && restart_index<=max_index)
265 set_restart_index(0);
272 return reinterpret_cast<const void *>(get_offset());
278 void Batch::set_restart_index(unsigned index)
280 if(MSP_primitive_restart)
285 glEnable(GL_PRIMITIVE_RESTART);
286 glPrimitiveRestartIndex(index);
289 glDisable(GL_PRIMITIVE_RESTART);
296 glEnableClientState(GL_PRIMITIVE_RESTART_NV);
297 glPrimitiveRestartIndexNV(index);
300 glDisableClientState(GL_PRIMITIVE_RESTART_NV);
303 restart_index = index;
307 Batch::Loader::Loader(Batch &b):
308 DataFile::ObjectLoader<Batch>(b)
310 add("indices", &Loader::indices);
313 void Batch::Loader::indices(const vector<unsigned> &ind)