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