]> git.tdb.fi Git - libs/gl.git/blob - core/batch.cpp
Check the flat qualifier from the correct member
[libs/gl.git] / 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         max_index(0)
52 {
53         set_index_type(UNSIGNED_SHORT);
54 }
55
56 void Batch::set_index_type(DataType t)
57 {
58         if(t==index_type)
59                 return;
60         if(t!=UNSIGNED_SHORT && t!=UNSIGNED_INT)
61                 throw invalid_argument("Batch::set_data_type");
62         if(t==UNSIGNED_SHORT && max_index>0xFFFE)
63                 throw invalid_operation("Batch::set_data_type");
64
65         if(index_type==UNSIGNED_SHORT && t==UNSIGNED_INT)
66                 expand<uint16_t, uint32_t>(data);
67         else if(index_type==UNSIGNED_INT && t==UNSIGNED_SHORT)
68                 shrink<uint32_t, uint16_t>(data);
69
70         index_type = t;
71         BatchBackend::set_index_type(t);
72         update_offset();
73         mark_dirty();
74 }
75
76 Batch &Batch::append(unsigned i)
77 {
78         append_index(i);
79
80         update_offset();
81         mark_dirty();
82
83         return *this;
84 }
85
86 Batch &Batch::append(const vector<unsigned> &ind)
87 {
88         if(ind.empty())
89                 return *this;
90
91         data.reserve(data.size()+ind.size()*get_index_size());
92         for(unsigned i: ind)
93                 append_index(i);
94
95         update_offset();
96         mark_dirty();
97
98         return *this;
99 }
100
101 bool Batch::can_append(PrimitiveType other_type)
102 {
103         if(other_type!=prim_type)
104                 return false;
105         else if(prim_type==LINE_STRIP || prim_type==TRIANGLE_FAN)
106                 return check_restart(false);
107         else
108                 return true;
109 }
110
111 Batch &Batch::append(const Batch &other)
112 {
113         if(other.prim_type!=prim_type)
114                 throw invalid_argument("Batch::append");
115         if(prim_type==LINE_STRIP || prim_type==TRIANGLE_FAN)
116                 check_restart(true);
117
118         if(other.data.empty())
119                 return *this;
120
121         // TODO allow appending triangles to a triangle strip
122
123         if(prim_type==POINTS || prim_type==LINES || prim_type==TRIANGLES)
124                 ;
125         else if(check_restart(false))
126         {
127                 if(index_type==UNSIGNED_INT)
128                         ::append<uint32_t>(data, 0xFFFFFFFF);
129                 else
130                         ::append<uint16_t>(data, 0xFFFF);
131         }
132         else if(prim_type==TRIANGLE_STRIP)
133         {
134                 append(get_index(size()-1));
135                 append(other.get_index(0));
136                 if(size()&1)
137                         append(other.get_index(0));
138         }
139
140         unsigned count = other.size();
141         for(unsigned i=0; i<count; ++i)
142                 append_index(other.get_index(i));
143
144         update_offset();
145         mark_dirty();
146
147         return *this;
148 }
149
150 void Batch::append_index(unsigned i)
151 {
152         if(data.empty())
153                 max_index = i;
154         else
155                 max_index = max(max_index, i);
156
157         if(index_type==UNSIGNED_SHORT && max_index>0xFFFE)
158                 set_index_type(UNSIGNED_INT);
159
160         if(index_type==UNSIGNED_INT)
161                 ::append<uint32_t>(data, i);
162         else
163                 ::append<uint16_t>(data, i);
164 }
165
166 unsigned Batch::get_index(size_t i) const
167 {
168         if(index_type==UNSIGNED_INT)
169                 return *(uint32_t *)&data[i*sizeof(uint32_t)];
170         else
171                 return *(uint16_t *)&data[i*sizeof(uint16_t)];
172 }
173
174
175 Batch::Loader::Loader(Batch &b):
176         DataFile::ObjectLoader<Batch>(b)
177 {
178         add("indices", &Loader::indices);
179 }
180
181 void Batch::Loader::indices(const vector<unsigned> &ind)
182 {
183         obj.append(ind);
184 }
185
186 } // namespace GL
187 } // namespace Msp