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