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<Msp::UInt8> &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<Msp::UInt8> &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<Msp::UInt8> &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 index_type(UNSIGNED_SHORT),
58 gl_index_type(get_gl_type(index_type)),
67 void Batch::set_index_type(DataType t)
69 if(t!=UNSIGNED_SHORT && t!=UNSIGNED_INT)
70 throw invalid_argument("Batch::set_data_type");
71 if(t==UNSIGNED_SHORT && max_index>0xFFFE)
72 throw invalid_operation("Batch::set_data_type");
74 if(index_type==UNSIGNED_SHORT && t==UNSIGNED_INT)
75 expand<UInt16, UInt32>(data);
76 else if(index_type==UNSIGNED_INT && t==UNSIGNED_SHORT)
77 shrink<UInt32, UInt16>(data);
80 gl_index_type = get_gl_type(t);
85 Batch &Batch::append(unsigned i)
95 Batch &Batch::append(const vector<unsigned> &ind)
100 data.reserve(data.size()+ind.size()*get_index_size());
101 for(vector<unsigned>::const_iterator i=ind.begin(); i!=ind.end(); ++i)
110 bool Batch::can_append(PrimitiveType other_type)
112 if(other_type!=prim_type)
114 else if(prim_type==LINE_STRIP || prim_type==LINE_LOOP || prim_type==TRIANGLE_FAN)
115 return MSP_primitive_restart;
120 Batch &Batch::append(const Batch &other)
122 if(other.prim_type!=prim_type)
123 throw invalid_argument("Batch::append");
124 if(prim_type==LINE_STRIP || prim_type==LINE_LOOP || prim_type==TRIANGLE_FAN)
125 static Require _req(MSP_primitive_restart);
127 if(other.data.empty())
130 // TODO allow appending triangles to a triangle strip
132 if(prim_type==POINTS || prim_type==LINES || prim_type==TRIANGLES)
134 else if(MSP_primitive_restart)
137 if(index_type==UNSIGNED_INT)
138 ::append<UInt32>(data, 0xFFFFFFFF);
140 ::append<UInt16>(data, 0xFFFF);
142 else if(prim_type==TRIANGLE_STRIP)
144 append(get_index(size()-1));
145 append(other.get_index(0));
147 append(other.get_index(0));
150 unsigned count = other.size();
151 for(unsigned i=0; i<count; ++i)
152 append_index(other.get_index(i));
160 void Batch::append_index(unsigned i)
165 max_index = max(max_index, i);
167 if(index_type==UNSIGNED_SHORT && max_index>0xFFFE)
168 set_index_type(UNSIGNED_INT);
170 if(index_type==UNSIGNED_INT)
171 ::append<UInt32>(data, i);
173 ::append<UInt16>(data, i);
176 unsigned Batch::get_index_size() const
178 return (index_type==UNSIGNED_INT ? sizeof(UInt32) : sizeof(UInt16));
181 unsigned Batch::get_index(unsigned i) const
183 if(index_type==UNSIGNED_INT)
184 return *(UInt32 *)&data[i*sizeof(UInt32)];
186 return *(UInt16 *)&data[i*sizeof(UInt16)];
189 void Batch::draw() const
191 const void *data_ptr = setup_draw();
192 glDrawElements(prim_type, size(), gl_index_type, data_ptr);
195 void Batch::draw_instanced(unsigned count) const
197 static Require req(ARB_draw_instanced);
199 const void *data_ptr = setup_draw();
200 glDrawElementsInstanced(prim_type, size(), gl_index_type, data_ptr, count);
203 const void *Batch::setup_draw() const
206 throw invalid_operation("Batch::setup_draw");
210 unsigned index = (index_type==UNSIGNED_INT ? 0xFFFFFFFF : 0xFFFF);
212 if(index!=restart_index)
213 set_restart_index(index);
215 else if(restart_index && restart_index<=max_index)
216 set_restart_index(0);
220 return reinterpret_cast<const void *>(get_offset());
223 void Batch::set_restart_index(unsigned index)
228 glEnable(GL_PRIMITIVE_RESTART);
229 glPrimitiveRestartIndex(index);
232 glDisable(GL_PRIMITIVE_RESTART);
234 restart_index = index;
238 Batch::Loader::Loader(Batch &b):
239 DataFile::ObjectLoader<Batch>(b)
241 add("indices", &Loader::indices);
244 void Batch::Loader::indices(const vector<unsigned> &ind)