]> git.tdb.fi Git - libs/gl.git/blob - source/batch.cpp
Remove useless namespace prefixes
[libs/gl.git] / source / batch.cpp
1 #include <msp/gl/extensions/ext_draw_range_elements.h>
2 #include <msp/gl/extensions/msp_legacy_features.h>
3 #include <msp/gl/extensions/msp_primitive_restart.h>
4 #include <msp/gl/extensions/nv_primitive_restart.h>
5 #include "batch.h"
6 #include "bindable.h"
7 #include "buffer.h"
8 #include "error.h"
9 #include "mesh.h"
10 #include "vertexarray.h"
11
12 using namespace std;
13
14 namespace {
15
16 template<typename T>
17 void append(vector<unsigned char> &data, T i)
18 {
19         data.insert(data.end(), sizeof(T), 0);
20         *(T *)(&data[data.size()-sizeof(T)]) = i;
21 }
22
23 template<typename T, typename U>
24 U convert(T n)
25 {
26         if(!static_cast<T>(~n))
27                 return ~0;
28         else
29                 return n;
30 }
31
32 template<typename T, typename U>
33 void expand(vector<unsigned char> &data)
34 {
35         unsigned count = data.size()/sizeof(T);
36         data.resize(count*sizeof(U));
37         for(unsigned i=count; i--;)
38                 *(U *)(&data[i*sizeof(U)]) = convert<T, U>(*(T *)(&data[i*sizeof(T)]));
39 }
40
41 template<typename T, typename U>
42 void shrink(vector<unsigned char> &data)
43 {
44         unsigned count = data.size()/sizeof(T);
45         for(unsigned i=0; i<count; ++i)
46                 *(U *)(&data[i*sizeof(U)]) = convert<T, U>(*(T *)(&data[i*sizeof(T)]));
47         data.resize(count*sizeof(U));
48 }
49
50 }
51
52 namespace Msp {
53 namespace GL {
54
55 unsigned Batch::restart_index = 0;
56
57 Batch::Batch(PrimitiveType t):
58         prim_type(t),
59         data_type(UNSIGNED_BYTE),
60         min_index(0),
61         max_index(0),
62         restart(false)
63 {
64         /* Make sure we have glEnable/DisableClientState to go with
65         NV_primitive_restart */
66         if(!MSP_primitive_restart && NV_primitive_restart)
67                 (void)(bool)MSP_legacy_features;
68 }
69
70 Batch::~Batch()
71 {
72 }
73
74 void Batch::set_data_type(DataType t)
75 {
76         if(t!=UNSIGNED_BYTE && t!=UNSIGNED_SHORT && t!=UNSIGNED_INT)
77                 throw invalid_argument("Batch::set_data_type");
78         if(t==UNSIGNED_BYTE && max_index>0xFE)
79                 throw invalid_operation("Batch::set_data_type");
80         else if(t==UNSIGNED_SHORT && max_index>0xFFFE)
81                 throw invalid_operation("Batch::set_data_type");
82
83         if(data_type==UNSIGNED_BYTE && t==UNSIGNED_SHORT)
84                 expand<unsigned char, unsigned short>(data);
85         else if(data_type==UNSIGNED_BYTE && t==UNSIGNED_INT)
86                 expand<unsigned char, unsigned>(data);
87         else if(data_type==UNSIGNED_SHORT && t==UNSIGNED_INT)
88                 expand<unsigned short, unsigned>(data);
89         else if(data_type==UNSIGNED_INT && t==UNSIGNED_BYTE)
90                 shrink<unsigned, unsigned char>(data);
91         else if(data_type==UNSIGNED_INT && t==UNSIGNED_SHORT)
92                 shrink<unsigned, unsigned short>(data);
93         else if(data_type==UNSIGNED_SHORT && t==UNSIGNED_BYTE)
94                 shrink<unsigned short, unsigned char>(data);
95
96         data_type = t;
97         update_offset();
98         dirty = true;
99 }
100
101 Batch &Batch::append(unsigned i)
102 {
103         append_index(i);
104
105         update_offset();
106         dirty = true;
107
108         return *this;
109 }
110
111 void Batch::append(const vector<unsigned> &ind)
112 {
113         if(ind.empty())
114                 return;
115
116         data.reserve(data.size()+ind.size()*get_index_size());
117         for(vector<unsigned>::const_iterator i=ind.begin(); i!=ind.end(); ++i)
118                 append_index(*i);
119
120         update_offset();
121         dirty = true;
122 }
123
124 bool Batch::can_append(PrimitiveType other_type)
125 {
126         if(other_type!=prim_type)
127                 return false;
128         else if(prim_type==LINE_STRIP || prim_type==LINE_LOOP || prim_type==TRIANGLE_FAN)
129                 return MSP_primitive_restart || NV_primitive_restart;
130         else
131                 return true;
132 }
133
134 void Batch::append(const Batch &other)
135 {
136         if(other.prim_type!=prim_type)
137                 throw invalid_argument("Batch::append");
138         if(prim_type==LINE_STRIP || prim_type==LINE_LOOP || prim_type==TRIANGLE_FAN)
139         {
140                 if(!MSP_primitive_restart)
141                         static Require _req(NV_primitive_restart);
142         }
143
144         if(other.data.empty())
145                 return;
146
147         if(prim_type==POINTS || prim_type==LINES || prim_type==TRIANGLES || prim_type==QUADS)
148                 ;
149         else if(MSP_primitive_restart || NV_primitive_restart)
150         {
151                 restart = true;
152                 if(data_type==UNSIGNED_SHORT)
153                         ::append<unsigned short>(data, 0xFFFF);
154                 else if(data_type==UNSIGNED_INT)
155                         ::append<unsigned>(data, 0xFFFFFFFF);
156                 else
157                         data.push_back(0xFF);
158         }
159         else if(prim_type==TRIANGLE_STRIP)
160         {
161                 append(get_index(size()-1));
162                 append(other.get_index(0));
163                 if(size()&1)
164                         append(other.get_index(0));
165         }
166         else if(prim_type==QUAD_STRIP)
167         {
168                 append(get_index(size()-1));
169                 append(get_index(size()-1));
170                 append(other.get_index(0));
171                 append(other.get_index(0));
172         }
173
174         unsigned count = other.size();
175         for(unsigned i=0; i<count; ++i)
176                 append_index(other.get_index(i));
177
178         update_offset();
179         dirty = true;
180 }
181
182 void Batch::append_index(unsigned i)
183 {
184         if(data.empty())
185                 min_index = max_index = i;
186         else
187         {
188                 min_index = min(min_index, i);
189                 max_index = max(max_index, i);
190         }
191
192         if((data_type==UNSIGNED_BYTE || data_type==UNSIGNED_SHORT) && max_index>0xFFFE)
193                 set_data_type(UNSIGNED_INT);
194         else if(data_type==UNSIGNED_BYTE && max_index>0xFE)
195                 set_data_type(UNSIGNED_SHORT);
196
197         if(data_type==UNSIGNED_SHORT)
198                 ::append<unsigned short>(data, i);
199         else if(data_type==UNSIGNED_INT)
200                 ::append<unsigned>(data, i);
201         else
202                 data.push_back(i);
203 }
204
205 unsigned Batch::get_index_size() const
206 {
207         if(data_type==UNSIGNED_SHORT)
208                 return sizeof(unsigned short);
209         else if(data_type==UNSIGNED_INT)
210                 return sizeof(unsigned);
211         return sizeof(unsigned char);
212 }
213
214 unsigned Batch::get_index(unsigned i) const
215 {
216         if(data_type==UNSIGNED_SHORT)
217                 return *(unsigned short *)&data[i*sizeof(unsigned short)];
218         else if(data_type==UNSIGNED_INT)
219                 return *(unsigned *)&data[i*sizeof(unsigned )];
220         else
221                 return data[i];
222 }
223
224 void Batch::draw() const
225 {
226         if(restart)
227         {
228                 unsigned index;
229                 if(data_type==UNSIGNED_SHORT)
230                         index = 0xFFFF;
231                 else if(data_type==UNSIGNED_INT)
232                         index = 0xFFFFFFFF;
233                 else
234                         index = 0xFF;
235
236                 if(index!=restart_index)
237                         set_restart_index(index);
238         }
239         else if(restart_index && restart_index<=max_index)
240                 set_restart_index(0);
241
242         Buffer *ibuf = get_buffer();
243         const void *data_ptr;
244         BindRestore _bind_ibuf(ibuf, ELEMENT_ARRAY_BUFFER);
245         if(ibuf)
246         {
247                 if(dirty)
248                         update_buffer();
249
250                 data_ptr = reinterpret_cast<const void *>(get_offset());
251         }
252         else
253                 data_ptr = &data[0];
254
255         if(EXT_draw_range_elements)
256                 glDrawRangeElements(prim_type, min_index, max_index, size(), data_type, data_ptr);
257         else
258                 glDrawElements(prim_type, size(), data_type, data_ptr);
259 }
260
261 void Batch::set_restart_index(unsigned index)
262 {
263         if(MSP_primitive_restart)
264         {
265                 if(index>0)
266                 {
267                         if(!restart_index)
268                                 glEnable(GL_PRIMITIVE_RESTART);
269                         glPrimitiveRestartIndex(index);
270                 }
271                 else
272                         glDisable(GL_PRIMITIVE_RESTART);
273         }
274         else
275         {
276                 if(index>0)
277                 {
278                         if(!restart_index)
279                                 glEnableClientState(GL_PRIMITIVE_RESTART_NV);
280                         glPrimitiveRestartIndexNV(index);
281                 }
282                 else
283                         glDisableClientState(GL_PRIMITIVE_RESTART_NV);
284         }
285
286         restart_index = index;
287 }
288
289
290 Batch::Loader::Loader(Batch &b):
291         DataFile::ObjectLoader<Batch>(b)
292 {
293         add("indices", &Loader::indices);
294 }
295
296 void Batch::Loader::indices(const vector<unsigned> &ind)
297 {
298         obj.append(ind);
299 }
300
301 } // namespace GL
302 } // namespace Msp