]> git.tdb.fi Git - libs/gl.git/blob - source/batch.cpp
Fix logic with appending Batches
[libs/gl.git] / source / batch.cpp
1 #include "batch.h"
2 #include "bindable.h"
3 #include "buffer.h"
4 #include "error.h"
5 #include "ext_draw_range_elements.h"
6 #include "nv_primitive_restart.h"
7 #include "vertexarray.h"
8
9 using namespace std;
10
11 namespace {
12
13 template<typename T>
14 void append(vector<unsigned char> &data, T i)
15 {
16         data.insert(data.end(), sizeof(T), 0);
17         *(T *)(&data[data.size()-sizeof(T)]) = i;
18 }
19
20 template<typename T, typename U>
21 U convert(T n)
22 {
23         if(!static_cast<T>(~n))
24                 return ~0;
25         else
26                 return n;
27 }
28
29 template<typename T, typename U>
30 void expand(vector<unsigned char> &data)
31 {
32         unsigned count = data.size()/sizeof(T);
33         data.resize(count*sizeof(U));
34         for(unsigned i=count; i--;)
35                 *(U *)(&data[i*sizeof(U)]) = convert<T, U>(*(T *)(&data[i*sizeof(T)]));
36 }
37
38 template<typename T, typename U>
39 void shrink(vector<unsigned char> &data)
40 {
41         unsigned count = data.size()/sizeof(T);
42         for(unsigned i=0; i<count; ++i)
43                 *(U *)(&data[i*sizeof(U)]) = convert<T, U>(*(T *)(&data[i*sizeof(T)]));
44         data.resize(count*sizeof(U));
45 }
46
47 }
48
49 namespace Msp {
50 namespace GL {
51
52 unsigned Batch::restart_index = 0;
53
54 Batch::Batch(PrimitiveType t):
55         prim_type(t),
56         data_type(UNSIGNED_BYTE),
57         min_index(0),
58         max_index(0),
59         restart(false)
60 {
61         /* XXX Should probably provide a fallback to glDrawElements since this class
62         is pretty much required to render anything. */
63         static Require _req(EXT_draw_range_elements);
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         if(data.empty())
100                 min_index = max_index = i;
101         else
102         {
103                 min_index = min(min_index, i);
104                 max_index = max(max_index, i);
105         }
106
107         if((data_type==UNSIGNED_BYTE || data_type==UNSIGNED_SHORT) && max_index>0xFFFE)
108                 set_data_type(UNSIGNED_INT);
109         else if(data_type==UNSIGNED_BYTE && max_index>0xFE)
110                 set_data_type(UNSIGNED_SHORT);
111
112         if(data_type==UNSIGNED_SHORT)
113                 ::append<unsigned short>(data, i);
114         else if(data_type==UNSIGNED_INT)
115                 ::append<unsigned>(data, i);
116         else
117                 data.push_back(i);
118         
119         update_offset();
120         dirty = true;
121
122         return *this;
123 }
124
125 void Batch::append(const vector<unsigned> &ind)
126 {
127         if(ind.empty())
128                 return;
129
130         if(data.empty())
131                 min_index = max_index = ind.front();
132
133         for(vector<unsigned>::const_iterator i=ind.begin(); i!=ind.end(); ++i)
134         {
135                 min_index = min(min_index, *i);
136                 max_index = max(max_index, *i);
137         }
138
139         if((data_type==UNSIGNED_BYTE || data_type==UNSIGNED_SHORT) && max_index>0xFFFE)
140                 set_data_type(UNSIGNED_INT);
141         else if(data_type==UNSIGNED_BYTE && max_index>0xFE)
142                 set_data_type(UNSIGNED_SHORT);
143
144         unsigned base = data.size();
145         data.resize(data.size()+ind.size()*get_index_size());
146         if(data_type==UNSIGNED_SHORT)
147         {
148                 unsigned short *ptr = reinterpret_cast<unsigned short *>(&data[base]);
149                 for(unsigned i=0; i<ind.size(); ++i)
150                         ptr[i] = ind[i];
151         }
152         else if(data_type==UNSIGNED_INT)
153         {
154                 unsigned *ptr = reinterpret_cast<unsigned *>(&data[base]);
155                 for(unsigned i=0; i<ind.size(); ++i)
156                         ptr[i] = ind[i];
157         }
158         else
159         {
160                 for(unsigned i=0; i<ind.size(); ++i)
161                         data[base+i] = ind[i];
162         }
163         dirty = true;
164 }
165
166 void Batch::append(const Batch &other)
167 {
168         if(other.prim_type!=prim_type)
169                 throw invalid_argument("Batch::append");
170         if(prim_type==LINE_STRIP || prim_type==LINE_LOOP || prim_type==TRIANGLE_FAN || prim_type==POLYGON)
171                 static Require _req(NV_primitive_restart);
172
173         if(other.data.empty())
174                 return;
175
176         if(prim_type==POINTS || prim_type==LINES || prim_type==TRIANGLES || prim_type==QUADS)
177                 ;
178         else if(NV_primitive_restart)
179         {
180                 restart = true;
181                 if(data_type==UNSIGNED_SHORT)
182                         ::append<unsigned short>(data, 0xFFFF);
183                 else if(data_type==UNSIGNED_INT)
184                         ::append<unsigned>(data, 0xFFFFFFFF);
185                 else
186                         data.push_back(0xFF);
187         }
188         else if(prim_type==TRIANGLE_STRIP)
189         {
190                 append(get_index(size()-1));
191                 append(other.get_index(0));
192                 if(size()&1)
193                         append(other.get_index(0));
194         }
195         else if(prim_type==QUAD_STRIP)
196         {
197                 append(get_index(size()-1));
198                 append(get_index(size()-1));
199                 append(other.get_index(0));
200                 append(other.get_index(0));
201         }
202
203         unsigned count = other.size();
204         for(unsigned i=0; i<count; ++i)
205                 append(other.get_index(i));
206 }
207
208 void Batch::upload_data() const
209 {
210         get_buffer()->sub_data(get_offset(), data.size(), &data[0]);
211 }
212
213 unsigned Batch::get_index_size() const
214 {
215         if(data_type==UNSIGNED_SHORT)
216                 return sizeof(unsigned short);
217         else if(data_type==UNSIGNED_INT)
218                 return sizeof(unsigned);
219         return sizeof(unsigned char);
220 }
221
222 unsigned Batch::get_index(unsigned i) const
223 {
224         if(data_type==UNSIGNED_SHORT)
225                 return *(unsigned short *)&data[i*sizeof(unsigned short)];
226         else if(data_type==UNSIGNED_INT)
227                 return *(unsigned *)&data[i*sizeof(unsigned )];
228         else
229                 return data[i];
230 }
231
232 void Batch::draw() const
233 {
234         if(restart)
235         {
236                 unsigned index;
237                 if(data_type==UNSIGNED_SHORT)
238                         index = 0xFFFF;
239                 else if(data_type==UNSIGNED_INT)
240                         index = 0xFFFFFFFF;
241                 else
242                         index = 0xFF;
243
244                 if(index!=restart_index)
245                 {
246                         if(!restart_index)
247                                 glEnableClientState(GL_PRIMITIVE_RESTART_NV);
248                         glPrimitiveRestartIndexNV(index);
249                         restart_index = index;
250                 }
251         }
252         else if(restart_index && restart_index<max_index)
253         {
254                 glDisableClientState(GL_PRIMITIVE_RESTART_NV);
255                 restart_index = 0;
256         }
257
258         if(get_buffer())
259         {
260                 if(dirty)
261                         update_buffer();
262
263                 BufferAlias<ELEMENT_ARRAY_BUFFER> alias(*get_buffer());
264                 Bind bind_ibuf(alias, true);
265
266                 glDrawRangeElements(prim_type, min_index, max_index, size(), data_type, reinterpret_cast<void *>(get_offset()));
267         }
268         else
269                 glDrawRangeElements(prim_type, min_index, max_index, size(), data_type, &data[0]);
270 }
271
272
273 Batch::Loader::Loader(Batch &b):
274         DataFile::ObjectLoader<Batch>(b)
275 {
276         add("indices", &Loader::indices);
277 }
278
279 void Batch::Loader::indices(const vector<unsigned> &ind)
280 {
281         obj.append(ind);
282 }
283
284 } // namespace GL
285 } // namespace Msp