]> git.tdb.fi Git - libs/gl.git/blob - source/core/batch.cpp
Use the same index type for all of a Mesh's batches
[libs/gl.git] / source / core / batch.cpp
1 #include <msp/gl/extensions/arb_draw_instanced.h>
2 #include <msp/gl/extensions/msp_primitive_restart.h>
3 #include "batch.h"
4 #include "buffer.h"
5 #include "error.h"
6 #include "mesh.h"
7 #include "vertexarray.h"
8
9 using namespace std;
10
11 namespace {
12
13 template<typename T>
14 void append(vector<Msp::UInt8> &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<Msp::UInt8> &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<Msp::UInt8> &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         index_type(UNSIGNED_SHORT),
57         gl_index_type(get_gl_type(index_type)),
58         max_index(0),
59         restart(false)
60 { }
61
62 Batch::~Batch()
63 {
64 }
65
66 void Batch::set_index_type(DataType t)
67 {
68         if(t==index_type)
69                 return;
70         if(t!=UNSIGNED_SHORT && t!=UNSIGNED_INT)
71                 throw invalid_argument("Batch::set_data_type");
72         if(t==UNSIGNED_SHORT && max_index>0xFFFE)
73                 throw invalid_operation("Batch::set_data_type");
74
75         if(index_type==UNSIGNED_SHORT && t==UNSIGNED_INT)
76                 expand<UInt16, UInt32>(data);
77         else if(index_type==UNSIGNED_INT && t==UNSIGNED_SHORT)
78                 shrink<UInt32, UInt16>(data);
79
80         index_type = t;
81         gl_index_type = get_gl_type(t);
82         update_offset();
83         dirty = true;
84 }
85
86 Batch &Batch::append(unsigned i)
87 {
88         append_index(i);
89
90         update_offset();
91         dirty = true;
92
93         return *this;
94 }
95
96 Batch &Batch::append(const vector<unsigned> &ind)
97 {
98         if(ind.empty())
99                 return *this;
100
101         data.reserve(data.size()+ind.size()*get_index_size());
102         for(vector<unsigned>::const_iterator i=ind.begin(); i!=ind.end(); ++i)
103                 append_index(*i);
104
105         update_offset();
106         dirty = true;
107
108         return *this;
109 }
110
111 bool Batch::can_append(PrimitiveType other_type)
112 {
113         if(other_type!=prim_type)
114                 return false;
115         else if(prim_type==LINE_STRIP || prim_type==LINE_LOOP || prim_type==TRIANGLE_FAN)
116                 return MSP_primitive_restart;
117         else
118                 return true;
119 }
120
121 Batch &Batch::append(const Batch &other)
122 {
123         if(other.prim_type!=prim_type)
124                 throw invalid_argument("Batch::append");
125         if(prim_type==LINE_STRIP || prim_type==LINE_LOOP || prim_type==TRIANGLE_FAN)
126                 static Require _req(MSP_primitive_restart);
127
128         if(other.data.empty())
129                 return *this;
130
131         // TODO allow appending triangles to a triangle strip
132
133         if(prim_type==POINTS || prim_type==LINES || prim_type==TRIANGLES)
134                 ;
135         else if(MSP_primitive_restart)
136         {
137                 restart = true;
138                 if(index_type==UNSIGNED_INT)
139                         ::append<UInt32>(data, 0xFFFFFFFF);
140                 else
141                         ::append<UInt16>(data, 0xFFFF);
142         }
143         else if(prim_type==TRIANGLE_STRIP)
144         {
145                 append(get_index(size()-1));
146                 append(other.get_index(0));
147                 if(size()&1)
148                         append(other.get_index(0));
149         }
150
151         unsigned count = other.size();
152         for(unsigned i=0; i<count; ++i)
153                 append_index(other.get_index(i));
154
155         update_offset();
156         dirty = true;
157
158         return *this;
159 }
160
161 void Batch::append_index(unsigned i)
162 {
163         if(data.empty())
164                 max_index = i;
165         else
166                 max_index = max(max_index, i);
167
168         if(index_type==UNSIGNED_SHORT && max_index>0xFFFE)
169                 set_index_type(UNSIGNED_INT);
170
171         if(index_type==UNSIGNED_INT)
172                 ::append<UInt32>(data, i);
173         else
174                 ::append<UInt16>(data, i);
175 }
176
177 unsigned Batch::get_index_size() const
178 {
179         return (index_type==UNSIGNED_INT ? sizeof(UInt32) : sizeof(UInt16));
180 }
181
182 unsigned Batch::get_index(unsigned i) const
183 {
184         if(index_type==UNSIGNED_INT)
185                 return *(UInt32 *)&data[i*sizeof(UInt32)];
186         else
187                 return *(UInt16 *)&data[i*sizeof(UInt16)];
188 }
189
190 void Batch::draw() const
191 {
192         const void *data_ptr = setup_draw();
193         glDrawElements(prim_type, size(), gl_index_type, data_ptr);
194 }
195
196 void Batch::draw_instanced(unsigned count) const
197 {
198         static Require req(ARB_draw_instanced);
199
200         const void *data_ptr = setup_draw();
201         glDrawElementsInstanced(prim_type, size(), gl_index_type, data_ptr, count);
202 }
203
204 const void *Batch::setup_draw() const
205 {
206         if(!get_buffer())
207                 throw invalid_operation("Batch::setup_draw");
208
209         if(restart)
210         {
211                 unsigned index = (index_type==UNSIGNED_INT ? 0xFFFFFFFF : 0xFFFF);
212
213                 if(index!=restart_index)
214                         set_restart_index(index);
215         }
216         else if(restart_index && restart_index<=max_index)
217                 set_restart_index(0);
218
219         refresh();
220
221         return reinterpret_cast<const void *>(get_offset());
222 }
223
224 void Batch::set_restart_index(unsigned index)
225 {
226         if(index>0)
227         {
228                 if(!restart_index)
229                         glEnable(GL_PRIMITIVE_RESTART);
230                 glPrimitiveRestartIndex(index);
231         }
232         else
233                 glDisable(GL_PRIMITIVE_RESTART);
234
235         restart_index = index;
236 }
237
238
239 Batch::Loader::Loader(Batch &b):
240         DataFile::ObjectLoader<Batch>(b)
241 {
242         add("indices", &Loader::indices);
243 }
244
245 void Batch::Loader::indices(const vector<unsigned> &ind)
246 {
247         obj.append(ind);
248 }
249
250 } // namespace GL
251 } // namespace Msp