]> git.tdb.fi Git - libs/gl.git/blob - source/batch.cpp
Support different data types in Batch
[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         data.reserve(data.size()+ind.size()*get_index_size());
123         for(vector<unsigned>::const_iterator i=ind.begin(); i!=ind.end(); ++i)
124                 append(*i);
125 }
126
127 void Batch::append(const Batch &other)
128 {
129         if(other.prim_type!=prim_type)
130                 throw InvalidParameterValue("Can't concatenate batches with different primitive types");
131         if(prim_type==LINE_STRIP || prim_type==LINE_LOOP)
132                 throw InvalidState("Can't concatenate line strips or loops");
133         else if(prim_type==POLYGON)
134                 throw InvalidState("Can't concatenate polygons");
135         else if(prim_type==TRIANGLE_FAN)
136                 static RequireExtension _ext("GL_NV_primitive_restart");
137
138         if(other.data.empty())
139                 return;
140
141         if(is_supported("GL_NV_primitive_restart"))
142         {
143                 restart = true;
144                 if(data_type==UNSIGNED_SHORT)
145                         append_index<unsigned short>(0xFFFF);
146                 else if(data_type==UNSIGNED_INT)
147                         append_index<unsigned>(0xFFFFFFFF);
148                 else
149                         data.push_back(0xFF);
150         }
151         else if(prim_type==TRIANGLE_STRIP)
152         {
153                 append(get_index(size()-1));
154                 append(other.get_index(0));
155                 if(size()&1)
156                         append(other.get_index(0));
157         }
158         else if(prim_type==QUAD_STRIP)
159         {
160                 append(get_index(size()-1));
161                 append(get_index(size()-1));
162                 append(other.get_index(0));
163                 append(other.get_index(0));
164         }
165
166         unsigned count = other.size();
167         for(unsigned i=0; i<count; ++i)
168                 append(other.get_index(i));
169 }
170
171 void Batch::draw() const
172 {
173         if(restart)
174         {
175                 unsigned index;
176                 if(data_type==UNSIGNED_SHORT)
177                         index = 0xFFFF;
178                 else if(data_type==UNSIGNED_INT)
179                         index = 0xFFFFFFFF;
180                 else
181                         index = 0xFF;
182
183                 if(index!=restart_index)
184                 {
185                         if(!restart_index)
186                                 glEnableClientState(GL_PRIMITIVE_RESTART_NV);
187                         glPrimitiveRestartIndexNV(index);
188                         restart_index = index;
189                 }
190         }
191         else if(restart_index && restart_index<max_index)
192         {
193                 glDisableClientState(GL_PRIMITIVE_RESTART_NV);
194                 restart_index = 0;
195         }
196
197         if(ibuf)
198         {
199                 if(dirty)
200                 {
201                         const Batch *b = this;
202                         for(; b->prev_in_ibuf; b=b->prev_in_ibuf) ;
203
204                         unsigned chain_size = 0;
205                         for(const Batch *a=b; a; a=a->next_in_ibuf)
206                                 chain_size += a->data.size();
207
208                         ibuf->data(chain_size, 0);
209
210                         for(; b; b=b->next_in_ibuf)
211                         {
212                                 ibuf->sub_data(b->ibuf_offset, b->data.size(), &b->data[0]);
213                                 b->dirty = false;
214                         }
215                 }
216
217                 BufferAlias<ELEMENT_ARRAY_BUFFER> alias(*ibuf);
218                 Bind bind_ibuf(alias, true);
219
220                 glDrawRangeElements(prim_type, min_index, max_index, size(), data_type, (void *)ibuf_offset);
221         }
222         else
223                 glDrawRangeElements(prim_type, min_index, max_index, size(), data_type, &data[0]);
224 }
225
226 unsigned Batch::get_index_size() const
227 {
228         if(data_type==UNSIGNED_SHORT)
229                 return sizeof(unsigned short);
230         else if(data_type==UNSIGNED_INT)
231                 return sizeof(unsigned);
232         return sizeof(unsigned char);
233 }
234
235 template<typename T>
236 void Batch::append_index(T i)
237 {
238         data.insert(data.end(), sizeof(T), 0);
239         *(T *)(&data[data.size()-sizeof(T)]) = i;
240 }
241
242 unsigned Batch::get_index(unsigned i) const
243 {
244         if(data_type==UNSIGNED_SHORT)
245                 return *(unsigned short *)&data[i*sizeof(unsigned short)];
246         else if(data_type==UNSIGNED_INT)
247                 return *(unsigned *)&data[i*sizeof(unsigned )];
248         else
249                 return data[i];
250 }
251
252 template<typename T, typename U>
253 void Batch::expand_data()
254 {
255         unsigned count = data.size()/sizeof(T);
256         data.resize(count*sizeof(U));
257         for(unsigned i=count; i--;)
258                 *(U *)(&data[i*sizeof(U)]) = convert<T, U>(*(T *)(&data[i*sizeof(T)]));
259 }
260
261 template<typename T, typename U>
262 void Batch::shrink_data()
263 {
264         unsigned count = data.size()/sizeof(T);
265         for(unsigned i=0; i<count; ++i)
266                 *(U *)(&data[i*sizeof(U)]) = convert<T, U>(*(T *)(&data[i*sizeof(T)]));
267         data.resize(count*sizeof(U));
268 }
269
270 template<typename T, typename U>
271 U Batch::convert(T i) const
272 {
273         if(!static_cast<T>(~i))
274                 return ~0;
275         else
276                 return i;
277 }
278
279 void Batch::unlink_from_ibuf()
280 {
281         if(next_in_ibuf)
282                 next_in_ibuf->prev_in_ibuf = prev_in_ibuf;
283         if(prev_in_ibuf)
284         {
285                 prev_in_ibuf->next_in_ibuf = next_in_ibuf;
286                 prev_in_ibuf->update_ibuf_offsets();
287         }
288         else if(next_in_ibuf)
289         {
290                 next_in_ibuf->ibuf_offset = 0;
291                 next_in_ibuf->update_ibuf_offsets();
292         }
293 }
294
295 void Batch::update_ibuf_offsets()
296 {
297         for(Batch *b=this; b->next_in_ibuf; b=b->next_in_ibuf)
298                 b->next_in_ibuf->ibuf_offset = b->ibuf_offset+b->data.size();
299 }
300
301
302 Batch::Loader::Loader(Batch &b):
303         DataFile::ObjectLoader<Batch>(b)
304 {
305         add("indices", &Loader::indices);
306 }
307
308 void Batch::Loader::indices(const vector<unsigned> &ind)
309 {
310         obj.append(ind);
311 }
312
313 } // namespace GL
314 } // namespace Msp