]> git.tdb.fi Git - libs/gl.git/blob - source/core/batch.cpp
An assortment of minor fixes
[libs/gl.git] / source / core / batch.cpp
1 #include "batch.h"
2 #include "error.h"
3
4 using namespace std;
5
6 namespace {
7
8 template<typename T>
9 void append(vector<uint8_t> &data, T i)
10 {
11         data.insert(data.end(), sizeof(T), 0);
12         *(T *)(&data[data.size()-sizeof(T)]) = i;
13 }
14
15 template<typename T, typename U>
16 U convert(T n)
17 {
18         if(!static_cast<T>(~n))
19                 return ~0;
20         else
21                 return n;
22 }
23
24 template<typename T, typename U>
25 void expand(vector<uint8_t> &data)
26 {
27         unsigned count = data.size()/sizeof(T);
28         data.resize(count*sizeof(U));
29         for(unsigned i=count; i--;)
30                 *(U *)(&data[i*sizeof(U)]) = convert<T, U>(*(T *)(&data[i*sizeof(T)]));
31 }
32
33 template<typename T, typename U>
34 void shrink(vector<uint8_t> &data)
35 {
36         unsigned count = data.size()/sizeof(T);
37         for(unsigned i=0; i<count; ++i)
38                 *(U *)(&data[i*sizeof(U)]) = convert<T, U>(*(T *)(&data[i*sizeof(T)]));
39         data.resize(count*sizeof(U));
40 }
41
42 }
43
44 namespace Msp {
45 namespace GL {
46
47 Batch::Batch(PrimitiveType t):
48         BatchBackend(t),
49         prim_type(t),
50         index_type(VOID)
51 {
52         set_index_type(UNSIGNED_SHORT);
53 }
54
55 void Batch::set_index_type(DataType t)
56 {
57         if(t==index_type)
58                 return;
59         if(t!=UNSIGNED_SHORT && t!=UNSIGNED_INT)
60                 throw invalid_argument("Batch::set_data_type");
61         if(t==UNSIGNED_SHORT && max_index>0xFFFE)
62                 throw invalid_operation("Batch::set_data_type");
63
64         if(index_type==UNSIGNED_SHORT && t==UNSIGNED_INT)
65                 expand<uint16_t, uint32_t>(data);
66         else if(index_type==UNSIGNED_INT && t==UNSIGNED_SHORT)
67                 shrink<uint32_t, uint16_t>(data);
68
69         index_type = t;
70         BatchBackend::set_index_type(t);
71         update_offset();
72         mark_dirty();
73 }
74
75 void Batch::set_patch_size(unsigned s)
76 {
77         if(prim_type!=PATCHES)
78                 throw invalid_operation("Batch::set_patch_size");
79         if(s<1)
80                 throw invalid_argument("Batch::set_patch_size");
81
82         patch_size = s;
83 }
84
85 Batch &Batch::append(unsigned i)
86 {
87         append_index(i);
88
89         update_offset();
90         mark_dirty();
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(unsigned i: ind)
102                 append_index(i);
103
104         update_offset();
105         mark_dirty();
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==TRIANGLE_FAN)
115                 return check_restart(false);
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==TRIANGLE_FAN)
125                 check_restart(true);
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(check_restart(false))
135         {
136                 if(index_type==UNSIGNED_INT)
137                         ::append<uint32_t>(data, 0xFFFFFFFF);
138                 else
139                         ::append<uint16_t>(data, 0xFFFF);
140         }
141         else if(prim_type==TRIANGLE_STRIP)
142         {
143                 append(get_index(size()-1));
144                 append(other.get_index(0));
145                 if(size()&1)
146                         append(other.get_index(0));
147         }
148
149         unsigned count = other.size();
150         for(unsigned i=0; i<count; ++i)
151                 append_index(other.get_index(i));
152
153         update_offset();
154         mark_dirty();
155
156         return *this;
157 }
158
159 void Batch::append_index(unsigned i)
160 {
161         if(data.empty())
162                 max_index = i;
163         else
164                 max_index = max(max_index, i);
165
166         if(index_type==UNSIGNED_SHORT && max_index>0xFFFE)
167                 set_index_type(UNSIGNED_INT);
168
169         if(index_type==UNSIGNED_INT)
170                 ::append<uint32_t>(data, i);
171         else
172                 ::append<uint16_t>(data, i);
173 }
174
175 unsigned Batch::get_index(size_t i) const
176 {
177         if(index_type==UNSIGNED_INT)
178                 return *(uint32_t *)&data[i*sizeof(uint32_t)];
179         else
180                 return *(uint16_t *)&data[i*sizeof(uint16_t)];
181 }
182
183
184 Batch::Loader::Loader(Batch &b):
185         DataFile::ObjectLoader<Batch>(b)
186 {
187         add("indices", &Loader::indices);
188         add("patch_size", &Loader::patch_size);
189 }
190
191 void Batch::Loader::indices(const vector<unsigned> &ind)
192 {
193         obj.append(ind);
194 }
195
196 void Batch::Loader::patch_size(unsigned s)
197 {
198         obj.set_patch_size(s);
199 }
200
201 } // namespace GL
202 } // namespace Msp