]> git.tdb.fi Git - libs/gl.git/blob - source/core/batch.cpp
Remove some rarely-used export settings
[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 "bindable.h"
5 #include "buffer.h"
6 #include "error.h"
7 #include "mesh.h"
8 #include "vertexarray.h"
9
10 using namespace std;
11
12 namespace {
13
14 template<typename T>
15 void append(vector<Msp::UInt8> &data, T i)
16 {
17         data.insert(data.end(), sizeof(T), 0);
18         *(T *)(&data[data.size()-sizeof(T)]) = i;
19 }
20
21 template<typename T, typename U>
22 U convert(T n)
23 {
24         if(!static_cast<T>(~n))
25                 return ~0;
26         else
27                 return n;
28 }
29
30 template<typename T, typename U>
31 void expand(vector<Msp::UInt8> &data)
32 {
33         unsigned count = data.size()/sizeof(T);
34         data.resize(count*sizeof(U));
35         for(unsigned i=count; i--;)
36                 *(U *)(&data[i*sizeof(U)]) = convert<T, U>(*(T *)(&data[i*sizeof(T)]));
37 }
38
39 template<typename T, typename U>
40 void shrink(vector<Msp::UInt8> &data)
41 {
42         unsigned count = data.size()/sizeof(T);
43         for(unsigned i=0; i<count; ++i)
44                 *(U *)(&data[i*sizeof(U)]) = convert<T, U>(*(T *)(&data[i*sizeof(T)]));
45         data.resize(count*sizeof(U));
46 }
47
48 }
49
50 namespace Msp {
51 namespace GL {
52
53 unsigned Batch::restart_index = 0;
54
55 Batch::Batch(PrimitiveType t):
56         prim_type(t),
57         index_type(UNSIGNED_SHORT),
58         gl_index_type(get_gl_type(index_type)),
59         max_index(0),
60         restart(false)
61 { }
62
63 Batch::~Batch()
64 {
65 }
66
67 void Batch::set_index_type(DataType t)
68 {
69         if(t!=UNSIGNED_SHORT && t!=UNSIGNED_INT)
70                 throw invalid_argument("Batch::set_data_type");
71         if(t==UNSIGNED_SHORT && max_index>0xFFFE)
72                 throw invalid_operation("Batch::set_data_type");
73
74         if(index_type==UNSIGNED_SHORT && t==UNSIGNED_INT)
75                 expand<UInt16, UInt32>(data);
76         else if(index_type==UNSIGNED_INT && t==UNSIGNED_SHORT)
77                 shrink<UInt32, UInt16>(data);
78
79         index_type = t;
80         gl_index_type = get_gl_type(t);
81         update_offset();
82         dirty = true;
83 }
84
85 Batch &Batch::append(unsigned i)
86 {
87         append_index(i);
88
89         update_offset();
90         dirty = true;
91
92         return *this;
93 }
94
95 Batch &Batch::append(const vector<unsigned> &ind)
96 {
97         if(ind.empty())
98                 return *this;
99
100         data.reserve(data.size()+ind.size()*get_index_size());
101         for(vector<unsigned>::const_iterator i=ind.begin(); i!=ind.end(); ++i)
102                 append_index(*i);
103
104         update_offset();
105         dirty = true;
106
107         return *this;
108 }
109
110 bool Batch::can_append(PrimitiveType other_type)
111 {
112         if(other_type!=prim_type)
113                 return false;
114         else if(prim_type==LINE_STRIP || prim_type==LINE_LOOP || prim_type==TRIANGLE_FAN)
115                 return MSP_primitive_restart;
116         else
117                 return true;
118 }
119
120 Batch &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)
125                 static Require _req(MSP_primitive_restart);
126
127         if(other.data.empty())
128                 return *this;
129
130         // TODO allow appending triangles to a triangle strip
131
132         if(prim_type==POINTS || prim_type==LINES || prim_type==TRIANGLES)
133                 ;
134         else if(MSP_primitive_restart)
135         {
136                 restart = true;
137                 if(index_type==UNSIGNED_INT)
138                         ::append<UInt32>(data, 0xFFFFFFFF);
139                 else
140                         ::append<UInt16>(data, 0xFFFF);
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
150         unsigned count = other.size();
151         for(unsigned i=0; i<count; ++i)
152                 append_index(other.get_index(i));
153
154         update_offset();
155         dirty = true;
156
157         return *this;
158 }
159
160 void Batch::append_index(unsigned i)
161 {
162         if(data.empty())
163                 max_index = i;
164         else
165                 max_index = max(max_index, i);
166
167         if(index_type==UNSIGNED_SHORT && max_index>0xFFFE)
168                 set_index_type(UNSIGNED_INT);
169
170         if(index_type==UNSIGNED_INT)
171                 ::append<UInt32>(data, i);
172         else
173                 ::append<UInt16>(data, i);
174 }
175
176 unsigned Batch::get_index_size() const
177 {
178         return (index_type==UNSIGNED_INT ? sizeof(UInt32) : sizeof(UInt16));
179 }
180
181 unsigned Batch::get_index(unsigned i) const
182 {
183         if(index_type==UNSIGNED_INT)
184                 return *(UInt32 *)&data[i*sizeof(UInt32)];
185         else
186                 return *(UInt16 *)&data[i*sizeof(UInt16)];
187 }
188
189 void Batch::draw() const
190 {
191         BindRestore _bind_ibuf(get_buffer(), ELEMENT_ARRAY_BUFFER);
192         const void *data_ptr = setup_draw();
193
194         glDrawElements(prim_type, size(), gl_index_type, data_ptr);
195 }
196
197 void Batch::draw_instanced(unsigned count) const
198 {
199         static Require req(ARB_draw_instanced);
200
201         BindRestore _bind_ibuf(get_buffer(), ELEMENT_ARRAY_BUFFER);
202         const void *data_ptr = setup_draw();
203
204         glDrawElementsInstanced(prim_type, size(), gl_index_type, data_ptr, count);
205 }
206
207 const void *Batch::setup_draw() const
208 {
209         if(!get_buffer())
210                 throw invalid_operation("Batch::setup_draw");
211
212         if(restart)
213         {
214                 unsigned index = (index_type==UNSIGNED_INT ? 0xFFFFFFFF : 0xFFFF);
215
216                 if(index!=restart_index)
217                         set_restart_index(index);
218         }
219         else if(restart_index && restart_index<=max_index)
220                 set_restart_index(0);
221
222         refresh();
223
224         return reinterpret_cast<const void *>(get_offset());
225 }
226
227 void Batch::set_restart_index(unsigned index)
228 {
229         if(index>0)
230         {
231                 if(!restart_index)
232                         glEnable(GL_PRIMITIVE_RESTART);
233                 glPrimitiveRestartIndex(index);
234         }
235         else
236                 glDisable(GL_PRIMITIVE_RESTART);
237
238         restart_index = index;
239 }
240
241
242 Batch::Loader::Loader(Batch &b):
243         DataFile::ObjectLoader<Batch>(b)
244 {
245         add("indices", &Loader::indices);
246 }
247
248 void Batch::Loader::indices(const vector<unsigned> &ind)
249 {
250         obj.append(ind);
251 }
252
253 } // namespace GL
254 } // namespace Msp