]> git.tdb.fi Git - libs/gl.git/blob - source/batch.cpp
Make use of Bufferable in Batch
[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)
171                 throw incompatible_data("Batch::append");
172         else if(prim_type==POLYGON)
173                 throw incompatible_data("Batch::append");
174         else if(prim_type==TRIANGLE_FAN)
175                 static Require _req(NV_primitive_restart);
176
177         if(other.data.empty())
178                 return;
179
180         if(NV_primitive_restart)
181         {
182                 restart = true;
183                 if(data_type==UNSIGNED_SHORT)
184                         ::append<unsigned short>(data, 0xFFFF);
185                 else if(data_type==UNSIGNED_INT)
186                         ::append<unsigned>(data, 0xFFFFFFFF);
187                 else
188                         data.push_back(0xFF);
189         }
190         else if(prim_type==TRIANGLE_STRIP)
191         {
192                 append(get_index(size()-1));
193                 append(other.get_index(0));
194                 if(size()&1)
195                         append(other.get_index(0));
196         }
197         else if(prim_type==QUAD_STRIP)
198         {
199                 append(get_index(size()-1));
200                 append(get_index(size()-1));
201                 append(other.get_index(0));
202                 append(other.get_index(0));
203         }
204
205         unsigned count = other.size();
206         for(unsigned i=0; i<count; ++i)
207                 append(other.get_index(i));
208 }
209
210 void Batch::upload_data() const
211 {
212         get_buffer()->sub_data(get_offset(), data.size(), &data[0]);
213 }
214
215 unsigned Batch::get_index_size() const
216 {
217         if(data_type==UNSIGNED_SHORT)
218                 return sizeof(unsigned short);
219         else if(data_type==UNSIGNED_INT)
220                 return sizeof(unsigned);
221         return sizeof(unsigned char);
222 }
223
224 unsigned Batch::get_index(unsigned i) const
225 {
226         if(data_type==UNSIGNED_SHORT)
227                 return *(unsigned short *)&data[i*sizeof(unsigned short)];
228         else if(data_type==UNSIGNED_INT)
229                 return *(unsigned *)&data[i*sizeof(unsigned )];
230         else
231                 return data[i];
232 }
233
234 void Batch::draw() const
235 {
236         if(restart)
237         {
238                 unsigned index;
239                 if(data_type==UNSIGNED_SHORT)
240                         index = 0xFFFF;
241                 else if(data_type==UNSIGNED_INT)
242                         index = 0xFFFFFFFF;
243                 else
244                         index = 0xFF;
245
246                 if(index!=restart_index)
247                 {
248                         if(!restart_index)
249                                 glEnableClientState(GL_PRIMITIVE_RESTART_NV);
250                         glPrimitiveRestartIndexNV(index);
251                         restart_index = index;
252                 }
253         }
254         else if(restart_index && restart_index<max_index)
255         {
256                 glDisableClientState(GL_PRIMITIVE_RESTART_NV);
257                 restart_index = 0;
258         }
259
260         if(get_buffer())
261         {
262                 if(dirty)
263                         update_buffer();
264
265                 BufferAlias<ELEMENT_ARRAY_BUFFER> alias(*get_buffer());
266                 Bind bind_ibuf(alias, true);
267
268                 glDrawRangeElements(prim_type, min_index, max_index, size(), data_type, reinterpret_cast<void *>(get_offset()));
269         }
270         else
271                 glDrawRangeElements(prim_type, min_index, max_index, size(), data_type, &data[0]);
272 }
273
274
275 Batch::Loader::Loader(Batch &b):
276         DataFile::ObjectLoader<Batch>(b)
277 {
278         add("indices", &Loader::indices);
279 }
280
281 void Batch::Loader::indices(const vector<unsigned> &ind)
282 {
283         obj.append(ind);
284 }
285
286 } // namespace GL
287 } // namespace Msp