1 #include <msp/gl/extensions/arb_draw_instanced.h>
2 #include <msp/gl/extensions/msp_primitive_restart.h>
8 #include "vertexarray.h"
15 void append(vector<unsigned char> &data, T i)
17 data.insert(data.end(), sizeof(T), 0);
18 *(T *)(&data[data.size()-sizeof(T)]) = i;
21 template<typename T, typename U>
24 if(!static_cast<T>(~n))
30 template<typename T, typename U>
31 void expand(vector<unsigned char> &data)
33 unsigned count = data.size()/sizeof(T);
34 data.resize(count*sizeof(U));
35 for(unsigned i=count; i--;)
36 *(U *)(&data[i*sizeof(U)]) = convert<T, U>(*(T *)(&data[i*sizeof(T)]));
39 template<typename T, typename U>
40 void shrink(vector<unsigned char> &data)
42 unsigned count = data.size()/sizeof(T);
43 for(unsigned i=0; i<count; ++i)
44 *(U *)(&data[i*sizeof(U)]) = convert<T, U>(*(T *)(&data[i*sizeof(T)]));
45 data.resize(count*sizeof(U));
53 unsigned Batch::restart_index = 0;
55 Batch::Batch(PrimitiveType t):
57 data_type(UNSIGNED_BYTE),
66 void Batch::set_data_type(DataType t)
68 if(t!=UNSIGNED_BYTE && t!=UNSIGNED_SHORT && t!=UNSIGNED_INT)
69 throw invalid_argument("Batch::set_data_type");
70 if(t==UNSIGNED_BYTE && max_index>0xFE)
71 throw invalid_operation("Batch::set_data_type");
72 else if(t==UNSIGNED_SHORT && max_index>0xFFFE)
73 throw invalid_operation("Batch::set_data_type");
75 if(data_type==UNSIGNED_BYTE && t==UNSIGNED_SHORT)
76 expand<unsigned char, unsigned short>(data);
77 else if(data_type==UNSIGNED_BYTE && t==UNSIGNED_INT)
78 expand<unsigned char, unsigned>(data);
79 else if(data_type==UNSIGNED_SHORT && t==UNSIGNED_INT)
80 expand<unsigned short, unsigned>(data);
81 else if(data_type==UNSIGNED_INT && t==UNSIGNED_BYTE)
82 shrink<unsigned, unsigned char>(data);
83 else if(data_type==UNSIGNED_INT && t==UNSIGNED_SHORT)
84 shrink<unsigned, unsigned short>(data);
85 else if(data_type==UNSIGNED_SHORT && t==UNSIGNED_BYTE)
86 shrink<unsigned short, unsigned char>(data);
93 Batch &Batch::append(unsigned i)
103 Batch &Batch::append(const vector<unsigned> &ind)
108 data.reserve(data.size()+ind.size()*get_index_size());
109 for(vector<unsigned>::const_iterator i=ind.begin(); i!=ind.end(); ++i)
118 bool Batch::can_append(PrimitiveType other_type)
120 if(other_type!=prim_type)
122 else if(prim_type==LINE_STRIP || prim_type==LINE_LOOP || prim_type==TRIANGLE_FAN)
123 return MSP_primitive_restart;
128 Batch &Batch::append(const Batch &other)
130 if(other.prim_type!=prim_type)
131 throw invalid_argument("Batch::append");
132 if(prim_type==LINE_STRIP || prim_type==LINE_LOOP || prim_type==TRIANGLE_FAN)
133 static Require _req(MSP_primitive_restart);
135 if(other.data.empty())
138 if(prim_type==POINTS || prim_type==LINES || prim_type==TRIANGLES)
140 else if(MSP_primitive_restart)
143 if(data_type==UNSIGNED_SHORT)
144 ::append<unsigned short>(data, 0xFFFF);
145 else if(data_type==UNSIGNED_INT)
146 ::append<unsigned>(data, 0xFFFFFFFF);
148 data.push_back(0xFF);
150 else if(prim_type==TRIANGLE_STRIP)
152 append(get_index(size()-1));
153 append(other.get_index(0));
155 append(other.get_index(0));
158 unsigned count = other.size();
159 for(unsigned i=0; i<count; ++i)
160 append_index(other.get_index(i));
168 void Batch::append_index(unsigned i)
173 max_index = max(max_index, i);
175 if((data_type==UNSIGNED_BYTE || data_type==UNSIGNED_SHORT) && max_index>0xFFFE)
176 set_data_type(UNSIGNED_INT);
177 else if(data_type==UNSIGNED_BYTE && max_index>0xFE)
178 set_data_type(UNSIGNED_SHORT);
180 if(data_type==UNSIGNED_SHORT)
181 ::append<unsigned short>(data, i);
182 else if(data_type==UNSIGNED_INT)
183 ::append<unsigned>(data, i);
188 unsigned Batch::get_index_size() const
190 if(data_type==UNSIGNED_SHORT)
191 return sizeof(unsigned short);
192 else if(data_type==UNSIGNED_INT)
193 return sizeof(unsigned);
194 return sizeof(unsigned char);
197 unsigned Batch::get_index(unsigned i) const
199 if(data_type==UNSIGNED_SHORT)
200 return *(unsigned short *)&data[i*sizeof(unsigned short)];
201 else if(data_type==UNSIGNED_INT)
202 return *(unsigned *)&data[i*sizeof(unsigned )];
207 void Batch::draw() const
209 BindRestore _bind_ibuf(get_buffer(), ELEMENT_ARRAY_BUFFER);
210 const void *data_ptr = setup_draw();
212 glDrawElements(prim_type, size(), data_type, data_ptr);
215 void Batch::draw_instanced(unsigned count) const
217 static Require req(ARB_draw_instanced);
219 BindRestore _bind_ibuf(get_buffer(), ELEMENT_ARRAY_BUFFER);
220 const void *data_ptr = setup_draw();
222 glDrawElementsInstanced(prim_type, size(), data_type, data_ptr, count);
225 const void *Batch::setup_draw() const
230 if(data_type==UNSIGNED_SHORT)
232 else if(data_type==UNSIGNED_INT)
237 if(index!=restart_index)
238 set_restart_index(index);
240 else if(restart_index && restart_index<=max_index)
241 set_restart_index(0);
248 return reinterpret_cast<const void *>(get_offset());
254 void Batch::set_restart_index(unsigned index)
259 glEnable(GL_PRIMITIVE_RESTART);
260 glPrimitiveRestartIndex(index);
263 glDisable(GL_PRIMITIVE_RESTART);
265 restart_index = index;
269 Batch::Loader::Loader(Batch &b):
270 DataFile::ObjectLoader<Batch>(b)
272 add("indices", &Loader::indices);
275 void Batch::Loader::indices(const vector<unsigned> &ind)