]> git.tdb.fi Git - libs/gl.git/blob - source/batch.cpp
Support OpenGL ES on Android
[libs/gl.git] / source / batch.cpp
1 #include <msp/gl/extensions/ext_draw_range_elements.h>
2 #include <msp/gl/extensions/msp_legacy_features.h>
3 #include <msp/gl/extensions/nv_primitive_restart.h>
4 #include "batch.h"
5 #include "bindable.h"
6 #include "buffer.h"
7 #include "error.h"
8 #include "mesh.h"
9 #include "vertexarray.h"
10
11 using namespace std;
12
13 namespace {
14
15 template<typename T>
16 void append(vector<unsigned char> &data, T i)
17 {
18         data.insert(data.end(), sizeof(T), 0);
19         *(T *)(&data[data.size()-sizeof(T)]) = i;
20 }
21
22 template<typename T, typename U>
23 U convert(T n)
24 {
25         if(!static_cast<T>(~n))
26                 return ~0;
27         else
28                 return n;
29 }
30
31 template<typename T, typename U>
32 void expand(vector<unsigned char> &data)
33 {
34         unsigned count = data.size()/sizeof(T);
35         data.resize(count*sizeof(U));
36         for(unsigned i=count; i--;)
37                 *(U *)(&data[i*sizeof(U)]) = convert<T, U>(*(T *)(&data[i*sizeof(T)]));
38 }
39
40 template<typename T, typename U>
41 void shrink(vector<unsigned char> &data)
42 {
43         unsigned count = data.size()/sizeof(T);
44         for(unsigned i=0; i<count; ++i)
45                 *(U *)(&data[i*sizeof(U)]) = convert<T, U>(*(T *)(&data[i*sizeof(T)]));
46         data.resize(count*sizeof(U));
47 }
48
49 }
50
51 namespace Msp {
52 namespace GL {
53
54 unsigned Batch::restart_index = 0;
55
56 Batch::Batch(PrimitiveType t):
57         prim_type(t),
58         data_type(UNSIGNED_BYTE),
59         min_index(0),
60         max_index(0),
61         restart(false)
62 {
63         /* Make sure we have glEnable/DisableClientState to go with
64         NV_primitive_restart */
65         if(NV_primitive_restart)
66                 (bool)MSP_legacy_features;
67 }
68
69 Batch::~Batch()
70 {
71 }
72
73 void Batch::set_data_type(DataType t)
74 {
75         if(t!=UNSIGNED_BYTE && t!=UNSIGNED_SHORT && t!=UNSIGNED_INT)
76                 throw invalid_argument("Batch::set_data_type");
77         if(t==UNSIGNED_BYTE && max_index>0xFE)
78                 throw invalid_operation("Batch::set_data_type");
79         else if(t==UNSIGNED_SHORT && max_index>0xFFFE)
80                 throw invalid_operation("Batch::set_data_type");
81
82         if(data_type==UNSIGNED_BYTE && t==UNSIGNED_SHORT)
83                 expand<unsigned char, unsigned short>(data);
84         else if(data_type==UNSIGNED_BYTE && t==UNSIGNED_INT)
85                 expand<unsigned char, unsigned>(data);
86         else if(data_type==UNSIGNED_SHORT && t==UNSIGNED_INT)
87                 expand<unsigned short, unsigned>(data);
88         else if(data_type==UNSIGNED_INT && t==UNSIGNED_BYTE)
89                 shrink<unsigned, unsigned char>(data);
90         else if(data_type==UNSIGNED_INT && t==UNSIGNED_SHORT)
91                 shrink<unsigned, unsigned short>(data);
92         else if(data_type==UNSIGNED_SHORT && t==UNSIGNED_BYTE)
93                 shrink<unsigned short, unsigned char>(data);
94
95         data_type = t;
96         update_offset();
97         dirty = true;
98 }
99
100 Batch &Batch::append(unsigned i)
101 {
102         append_index(i);
103
104         update_offset();
105         dirty = true;
106
107         return *this;
108 }
109
110 void Batch::append(const vector<unsigned> &ind)
111 {
112         if(ind.empty())
113                 return;
114
115         data.reserve(data.size()+ind.size()*get_index_size());
116         for(vector<unsigned>::const_iterator i=ind.begin(); i!=ind.end(); ++i)
117                 append_index(*i);
118
119         update_offset();
120         dirty = true;
121 }
122
123 bool Batch::can_append(PrimitiveType other_type)
124 {
125         if(other_type!=prim_type)
126                 return false;
127         else if(prim_type==LINE_STRIP || prim_type==LINE_LOOP || prim_type==TRIANGLE_FAN)
128                 return NV_primitive_restart;
129         else
130                 return true;
131 }
132
133 void Batch::append(const Batch &other)
134 {
135         if(other.prim_type!=prim_type)
136                 throw invalid_argument("Batch::append");
137         if(prim_type==LINE_STRIP || prim_type==LINE_LOOP || prim_type==TRIANGLE_FAN)
138                 static Require _req(NV_primitive_restart);
139
140         if(other.data.empty())
141                 return;
142
143         if(prim_type==POINTS || prim_type==LINES || prim_type==TRIANGLES || prim_type==QUADS)
144                 ;
145         else if(NV_primitive_restart)
146         {
147                 restart = true;
148                 if(data_type==UNSIGNED_SHORT)
149                         ::append<unsigned short>(data, 0xFFFF);
150                 else if(data_type==UNSIGNED_INT)
151                         ::append<unsigned>(data, 0xFFFFFFFF);
152                 else
153                         data.push_back(0xFF);
154         }
155         else if(prim_type==TRIANGLE_STRIP)
156         {
157                 append(get_index(size()-1));
158                 append(other.get_index(0));
159                 if(size()&1)
160                         append(other.get_index(0));
161         }
162         else if(prim_type==QUAD_STRIP)
163         {
164                 append(get_index(size()-1));
165                 append(get_index(size()-1));
166                 append(other.get_index(0));
167                 append(other.get_index(0));
168         }
169
170         unsigned count = other.size();
171         for(unsigned i=0; i<count; ++i)
172                 append_index(other.get_index(i));
173
174         update_offset();
175         dirty = true;
176 }
177
178 void Batch::append_index(unsigned i)
179 {
180         if(data.empty())
181                 min_index = max_index = i;
182         else
183         {
184                 min_index = min(min_index, i);
185                 max_index = max(max_index, i);
186         }
187
188         if((data_type==UNSIGNED_BYTE || data_type==UNSIGNED_SHORT) && max_index>0xFFFE)
189                 set_data_type(UNSIGNED_INT);
190         else if(data_type==UNSIGNED_BYTE && max_index>0xFE)
191                 set_data_type(UNSIGNED_SHORT);
192
193         if(data_type==UNSIGNED_SHORT)
194                 ::append<unsigned short>(data, i);
195         else if(data_type==UNSIGNED_INT)
196                 ::append<unsigned>(data, i);
197         else
198                 data.push_back(i);
199 }
200
201 unsigned Batch::get_index_size() const
202 {
203         if(data_type==UNSIGNED_SHORT)
204                 return sizeof(unsigned short);
205         else if(data_type==UNSIGNED_INT)
206                 return sizeof(unsigned);
207         return sizeof(unsigned char);
208 }
209
210 unsigned Batch::get_index(unsigned i) const
211 {
212         if(data_type==UNSIGNED_SHORT)
213                 return *(unsigned short *)&data[i*sizeof(unsigned short)];
214         else if(data_type==UNSIGNED_INT)
215                 return *(unsigned *)&data[i*sizeof(unsigned )];
216         else
217                 return data[i];
218 }
219
220 void Batch::draw() const
221 {
222         if(restart)
223         {
224                 unsigned index;
225                 if(data_type==UNSIGNED_SHORT)
226                         index = 0xFFFF;
227                 else if(data_type==UNSIGNED_INT)
228                         index = 0xFFFFFFFF;
229                 else
230                         index = 0xFF;
231
232                 if(index!=restart_index)
233                 {
234                         if(!restart_index)
235                                 glEnableClientState(GL_PRIMITIVE_RESTART_NV);
236                         glPrimitiveRestartIndexNV(index);
237                         restart_index = index;
238                 }
239         }
240         else if(restart_index && restart_index<=max_index)
241         {
242                 glDisableClientState(GL_PRIMITIVE_RESTART_NV);
243                 restart_index = 0;
244         }
245
246         Buffer *ibuf = get_buffer();
247         const void *data_ptr;
248         BindRestore _bind_ibuf(ibuf, ELEMENT_ARRAY_BUFFER);
249         if(ibuf)
250         {
251                 if(dirty)
252                         update_buffer();
253
254                 data_ptr = reinterpret_cast<const void *>(get_offset());
255         }
256         else
257                 data_ptr = &data[0];
258
259         if(EXT_draw_range_elements)
260                 glDrawRangeElements(prim_type, min_index, max_index, size(), data_type, data_ptr);
261         else
262                 glDrawElements(prim_type, size(), data_type, data_ptr);
263 }
264
265
266 Batch::Loader::Loader(Batch &b):
267         DataFile::ObjectLoader<Batch>(b)
268 {
269         add("indices", &Loader::indices);
270 }
271
272 void Batch::Loader::indices(const vector<unsigned> &ind)
273 {
274         obj.append(ind);
275 }
276
277 } // namespace GL
278 } // namespace Msp