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