]> git.tdb.fi Git - libs/gl.git/blob - source/batch.cpp
Fix a comparison with restart_index
[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         append_index(i);
100         
101         update_offset();
102         dirty = true;
103
104         return *this;
105 }
106
107 void Batch::append(const vector<unsigned> &ind)
108 {
109         if(ind.empty())
110                 return;
111
112         data.reserve(data.size()+ind.size()*get_index_size());
113         for(vector<unsigned>::const_iterator i=ind.begin(); i!=ind.end(); ++i)
114                 append_index(*i);
115
116         update_offset();
117         dirty = true;
118 }
119
120 void Batch::append(const Batch &other)
121 {
122         if(other.prim_type!=prim_type)
123                 throw invalid_argument("Batch::append");
124         if(prim_type==LINE_STRIP || prim_type==LINE_LOOP || prim_type==TRIANGLE_FAN || prim_type==POLYGON)
125                 static Require _req(NV_primitive_restart);
126
127         if(other.data.empty())
128                 return;
129
130         if(prim_type==POINTS || prim_type==LINES || prim_type==TRIANGLES || prim_type==QUADS)
131                 ;
132         else if(NV_primitive_restart)
133         {
134                 restart = true;
135                 if(data_type==UNSIGNED_SHORT)
136                         ::append<unsigned short>(data, 0xFFFF);
137                 else if(data_type==UNSIGNED_INT)
138                         ::append<unsigned>(data, 0xFFFFFFFF);
139                 else
140                         data.push_back(0xFF);
141         }
142         else if(prim_type==TRIANGLE_STRIP)
143         {
144                 append(get_index(size()-1));
145                 append(other.get_index(0));
146                 if(size()&1)
147                         append(other.get_index(0));
148         }
149         else if(prim_type==QUAD_STRIP)
150         {
151                 append(get_index(size()-1));
152                 append(get_index(size()-1));
153                 append(other.get_index(0));
154                 append(other.get_index(0));
155         }
156
157         unsigned count = other.size();
158         for(unsigned i=0; i<count; ++i)
159                 append_index(other.get_index(i));
160
161         update_offset();
162         dirty = true;
163 }
164
165 void Batch::append_index(unsigned i)
166 {
167         if(data.empty())
168                 min_index = max_index = i;
169         else
170         {
171                 min_index = min(min_index, i);
172                 max_index = max(max_index, i);
173         }
174
175         if((data_type==UNSIGNED_BYTE || data_type==UNSIGNED_SHORT) && max_index>0xFFFE)
176                 set_data_type(UNSIGNED_INT);
177         else if(data_type==UNSIGNED_BYTE && max_index>0xFE)
178                 set_data_type(UNSIGNED_SHORT);
179
180         if(data_type==UNSIGNED_SHORT)
181                 ::append<unsigned short>(data, i);
182         else if(data_type==UNSIGNED_INT)
183                 ::append<unsigned>(data, i);
184         else
185                 data.push_back(i);
186 }
187
188 void Batch::upload_data() const
189 {
190         get_buffer()->sub_data(get_offset(), data.size(), &data[0]);
191 }
192
193 unsigned Batch::get_index_size() const
194 {
195         if(data_type==UNSIGNED_SHORT)
196                 return sizeof(unsigned short);
197         else if(data_type==UNSIGNED_INT)
198                 return sizeof(unsigned);
199         return sizeof(unsigned char);
200 }
201
202 unsigned Batch::get_index(unsigned i) const
203 {
204         if(data_type==UNSIGNED_SHORT)
205                 return *(unsigned short *)&data[i*sizeof(unsigned short)];
206         else if(data_type==UNSIGNED_INT)
207                 return *(unsigned *)&data[i*sizeof(unsigned )];
208         else
209                 return data[i];
210 }
211
212 void Batch::draw() const
213 {
214         if(restart)
215         {
216                 unsigned index;
217                 if(data_type==UNSIGNED_SHORT)
218                         index = 0xFFFF;
219                 else if(data_type==UNSIGNED_INT)
220                         index = 0xFFFFFFFF;
221                 else
222                         index = 0xFF;
223
224                 if(index!=restart_index)
225                 {
226                         if(!restart_index)
227                                 glEnableClientState(GL_PRIMITIVE_RESTART_NV);
228                         glPrimitiveRestartIndexNV(index);
229                         restart_index = index;
230                 }
231         }
232         else if(restart_index && restart_index<=max_index)
233         {
234                 glDisableClientState(GL_PRIMITIVE_RESTART_NV);
235                 restart_index = 0;
236         }
237
238         if(get_buffer())
239         {
240                 if(dirty)
241                         update_buffer();
242
243                 BufferAlias<ELEMENT_ARRAY_BUFFER> alias(*get_buffer());
244                 Bind bind_ibuf(alias, true);
245
246                 glDrawRangeElements(prim_type, min_index, max_index, size(), data_type, reinterpret_cast<void *>(get_offset()));
247         }
248         else
249                 glDrawRangeElements(prim_type, min_index, max_index, size(), data_type, &data[0]);
250 }
251
252
253 Batch::Loader::Loader(Batch &b):
254         DataFile::ObjectLoader<Batch>(b)
255 {
256         add("indices", &Loader::indices);
257 }
258
259 void Batch::Loader::indices(const vector<unsigned> &ind)
260 {
261         obj.append(ind);
262 }
263
264 } // namespace GL
265 } // namespace Msp