]> git.tdb.fi Git - libs/gl.git/blob - source/batch.cpp
Add a method to get the offset of a single component of a VertexFormat
[libs/gl.git] / source / batch.cpp
1 /* $Id$
2
3 This file is part of libmspgl
4 Copyright © 2007-2010  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #include "batch.h"
9 #include "bindable.h"
10 #include "buffer.h"
11 #include "extension.h"
12 #include "nv_primitive_restart.h"
13 #include "vertexarray.h"
14
15 using namespace std;
16
17 namespace Msp {
18 namespace GL {
19
20 unsigned Batch::restart_index = 0;
21
22 Batch::Batch(PrimitiveType t):
23         prim_type(t),
24         data_type(UNSIGNED_BYTE),
25         min_index(0),
26         max_index(0),
27         restart(false),
28         ibuf(0),
29         ibuf_offset(0),
30         next_in_ibuf(0),
31         prev_in_ibuf(0),
32         dirty(false)
33 { }
34
35 Batch::~Batch()
36 {
37         unlink_from_ibuf();
38 }
39
40 void Batch::set_data_type(DataType t)
41 {
42         if(t!=UNSIGNED_BYTE && t!=UNSIGNED_SHORT && t!=UNSIGNED_INT)
43                 throw InvalidParameterValue("Batch data type must be an unsigned integer");
44         if(t==UNSIGNED_BYTE && max_index>0xFE)
45                 throw InvalidState("UNSIGNED_BYTE can't hold all indices in Batch");
46         else if(t==UNSIGNED_SHORT && max_index>0xFFFE)
47                 throw InvalidState("UNSIGNED_SHORT can't hold all indices in Batch");
48
49         if(data_type==UNSIGNED_BYTE && t==UNSIGNED_SHORT)
50                 expand_data<unsigned char, unsigned short>();
51         else if(data_type==UNSIGNED_BYTE && t==UNSIGNED_INT)
52                 expand_data<unsigned char, unsigned>();
53         else if(data_type==UNSIGNED_SHORT && t==UNSIGNED_INT)
54                 expand_data<unsigned short, unsigned>();
55         else if(data_type==UNSIGNED_INT && t==UNSIGNED_BYTE)
56                 shrink_data<unsigned, unsigned char>();
57         else if(data_type==UNSIGNED_INT && t==UNSIGNED_SHORT)
58                 shrink_data<unsigned, unsigned short>();
59         else if(data_type==UNSIGNED_SHORT && t==UNSIGNED_BYTE)
60                 shrink_data<unsigned short, unsigned char>();
61
62         data_type = t;
63         update_ibuf_offsets();
64         dirty = true;
65 }
66
67 void Batch::use_index_buffer(Buffer *buf, Batch *prev)
68 {
69         if(buf && prev && prev->ibuf!=buf)
70                 throw InvalidParameterValue("Previous batch is not in the same buffer");
71
72         if(!buf)
73         {
74                 prev = 0;
75                 unlink_from_ibuf();
76         }
77
78         ibuf = buf;
79         prev_in_ibuf = prev;
80         next_in_ibuf = 0;
81         if(prev)
82         {
83                 prev->next_in_ibuf = this;
84                 ibuf_offset = prev->ibuf_offset+prev->data.size();
85         }
86         else
87                 ibuf_offset = 0;
88
89         dirty = true;
90 }
91
92 Batch &Batch::append(unsigned i)
93 {
94         if(data.empty())
95                 min_index = max_index = i;
96         else
97         {
98                 min_index = min(min_index, i);
99                 max_index = max(max_index, i);
100         }
101
102         if((data_type==UNSIGNED_BYTE || data_type==UNSIGNED_SHORT) && max_index>0xFFFE)
103                 set_data_type(UNSIGNED_INT);
104         else if(data_type==UNSIGNED_BYTE && max_index>0xFE)
105                 set_data_type(UNSIGNED_SHORT);
106
107         if(data_type==UNSIGNED_SHORT)
108                 append_index<unsigned short>(i);
109         else if(data_type==UNSIGNED_INT)
110                 append_index<unsigned>(i);
111         else
112                 data.push_back(i);
113         
114         update_ibuf_offsets();
115         dirty = true;
116
117         return *this;
118 }
119
120 void Batch::append(const vector<unsigned> &ind)
121 {
122         if(ind.empty())
123                 return;
124
125         if(data.empty())
126                 min_index = max_index = ind.front();
127
128         for(vector<unsigned>::const_iterator i=ind.begin(); i!=ind.end(); ++i)
129         {
130                 min_index = min(min_index, *i);
131                 max_index = max(max_index, *i);
132         }
133
134         if((data_type==UNSIGNED_BYTE || data_type==UNSIGNED_SHORT) && max_index>0xFFFE)
135                 set_data_type(UNSIGNED_INT);
136         else if(data_type==UNSIGNED_BYTE && max_index>0xFE)
137                 set_data_type(UNSIGNED_SHORT);
138
139         unsigned base = data.size();
140         data.resize(data.size()+ind.size()*get_index_size());
141         if(data_type==UNSIGNED_SHORT)
142         {
143                 unsigned short *ptr = reinterpret_cast<unsigned short *>(&data[base]);
144                 for(unsigned i=0; i<ind.size(); ++i)
145                         ptr[i] = ind[i];
146         }
147         else if(data_type==UNSIGNED_INT)
148         {
149                 unsigned *ptr = reinterpret_cast<unsigned *>(&data[base]);
150                 for(unsigned i=0; i<ind.size(); ++i)
151                         ptr[i] = ind[i];
152         }
153         else
154         {
155                 for(unsigned i=0; i<ind.size(); ++i)
156                         data[base+i] = ind[i];
157         }
158 }
159
160 void Batch::append(const Batch &other)
161 {
162         if(other.prim_type!=prim_type)
163                 throw InvalidParameterValue("Can't concatenate batches with different primitive types");
164         if(prim_type==LINE_STRIP || prim_type==LINE_LOOP)
165                 throw InvalidState("Can't concatenate line strips or loops");
166         else if(prim_type==POLYGON)
167                 throw InvalidState("Can't concatenate polygons");
168         else if(prim_type==TRIANGLE_FAN)
169                 static RequireExtension _ext("GL_NV_primitive_restart");
170
171         if(other.data.empty())
172                 return;
173
174         if(is_supported("GL_NV_primitive_restart"))
175         {
176                 restart = true;
177                 if(data_type==UNSIGNED_SHORT)
178                         append_index<unsigned short>(0xFFFF);
179                 else if(data_type==UNSIGNED_INT)
180                         append_index<unsigned>(0xFFFFFFFF);
181                 else
182                         data.push_back(0xFF);
183         }
184         else if(prim_type==TRIANGLE_STRIP)
185         {
186                 append(get_index(size()-1));
187                 append(other.get_index(0));
188                 if(size()&1)
189                         append(other.get_index(0));
190         }
191         else if(prim_type==QUAD_STRIP)
192         {
193                 append(get_index(size()-1));
194                 append(get_index(size()-1));
195                 append(other.get_index(0));
196                 append(other.get_index(0));
197         }
198
199         unsigned count = other.size();
200         for(unsigned i=0; i<count; ++i)
201                 append(other.get_index(i));
202 }
203
204 void Batch::draw() const
205 {
206         if(restart)
207         {
208                 unsigned index;
209                 if(data_type==UNSIGNED_SHORT)
210                         index = 0xFFFF;
211                 else if(data_type==UNSIGNED_INT)
212                         index = 0xFFFFFFFF;
213                 else
214                         index = 0xFF;
215
216                 if(index!=restart_index)
217                 {
218                         if(!restart_index)
219                                 glEnableClientState(GL_PRIMITIVE_RESTART_NV);
220                         glPrimitiveRestartIndexNV(index);
221                         restart_index = index;
222                 }
223         }
224         else if(restart_index && restart_index<max_index)
225         {
226                 glDisableClientState(GL_PRIMITIVE_RESTART_NV);
227                 restart_index = 0;
228         }
229
230         if(ibuf)
231         {
232                 if(dirty)
233                 {
234                         const Batch *b = this;
235                         for(; b->prev_in_ibuf; b=b->prev_in_ibuf) ;
236
237                         unsigned chain_size = 0;
238                         for(const Batch *a=b; a; a=a->next_in_ibuf)
239                                 chain_size += a->data.size();
240
241                         ibuf->data(chain_size, 0);
242
243                         for(; b; b=b->next_in_ibuf)
244                         {
245                                 ibuf->sub_data(b->ibuf_offset, b->data.size(), &b->data[0]);
246                                 b->dirty = false;
247                         }
248                 }
249
250                 BufferAlias<ELEMENT_ARRAY_BUFFER> alias(*ibuf);
251                 Bind bind_ibuf(alias, true);
252
253                 glDrawRangeElements(prim_type, min_index, max_index, size(), data_type, (void *)ibuf_offset);
254         }
255         else
256                 glDrawRangeElements(prim_type, min_index, max_index, size(), data_type, &data[0]);
257 }
258
259 unsigned Batch::get_index_size() const
260 {
261         if(data_type==UNSIGNED_SHORT)
262                 return sizeof(unsigned short);
263         else if(data_type==UNSIGNED_INT)
264                 return sizeof(unsigned);
265         return sizeof(unsigned char);
266 }
267
268 template<typename T>
269 void Batch::append_index(T i)
270 {
271         data.insert(data.end(), sizeof(T), 0);
272         *(T *)(&data[data.size()-sizeof(T)]) = i;
273 }
274
275 unsigned Batch::get_index(unsigned i) const
276 {
277         if(data_type==UNSIGNED_SHORT)
278                 return *(unsigned short *)&data[i*sizeof(unsigned short)];
279         else if(data_type==UNSIGNED_INT)
280                 return *(unsigned *)&data[i*sizeof(unsigned )];
281         else
282                 return data[i];
283 }
284
285 template<typename T, typename U>
286 void Batch::expand_data()
287 {
288         unsigned count = data.size()/sizeof(T);
289         data.resize(count*sizeof(U));
290         for(unsigned i=count; i--;)
291                 *(U *)(&data[i*sizeof(U)]) = convert<T, U>(*(T *)(&data[i*sizeof(T)]));
292 }
293
294 template<typename T, typename U>
295 void Batch::shrink_data()
296 {
297         unsigned count = data.size()/sizeof(T);
298         for(unsigned i=0; i<count; ++i)
299                 *(U *)(&data[i*sizeof(U)]) = convert<T, U>(*(T *)(&data[i*sizeof(T)]));
300         data.resize(count*sizeof(U));
301 }
302
303 template<typename T, typename U>
304 U Batch::convert(T i) const
305 {
306         if(!static_cast<T>(~i))
307                 return ~0;
308         else
309                 return i;
310 }
311
312 void Batch::unlink_from_ibuf()
313 {
314         if(next_in_ibuf)
315                 next_in_ibuf->prev_in_ibuf = prev_in_ibuf;
316         if(prev_in_ibuf)
317         {
318                 prev_in_ibuf->next_in_ibuf = next_in_ibuf;
319                 prev_in_ibuf->update_ibuf_offsets();
320         }
321         else if(next_in_ibuf)
322         {
323                 next_in_ibuf->ibuf_offset = 0;
324                 next_in_ibuf->update_ibuf_offsets();
325         }
326 }
327
328 void Batch::update_ibuf_offsets()
329 {
330         for(Batch *b=this; b->next_in_ibuf; b=b->next_in_ibuf)
331                 b->next_in_ibuf->ibuf_offset = b->ibuf_offset+b->data.size();
332 }
333
334
335 Batch::Loader::Loader(Batch &b):
336         DataFile::ObjectLoader<Batch>(b)
337 {
338         add("indices", &Loader::indices);
339 }
340
341 void Batch::Loader::indices(const vector<unsigned> &ind)
342 {
343         obj.append(ind);
344 }
345
346 } // namespace GL
347 } // namespace Msp