]> git.tdb.fi Git - libs/gl.git/blob - source/core/batch.cpp
Add tessellation shader support to the engine
[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 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 void Batch::set_patch_size(unsigned s)
77 {
78         if(prim_type!=PATCHES)
79                 throw invalid_operation("Batch::set_patch_size");
80         if(s<1)
81                 throw invalid_argument("Batch::set_patch_size");
82
83         patch_size = s;
84 }
85
86 Batch &Batch::append(unsigned i)
87 {
88         append_index(i);
89
90         update_offset();
91         mark_dirty();
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(unsigned i: ind)
103                 append_index(i);
104
105         update_offset();
106         mark_dirty();
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==TRIANGLE_FAN)
116                 return check_restart(false);
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==TRIANGLE_FAN)
126                 check_restart(true);
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(check_restart(false))
136         {
137                 if(index_type==UNSIGNED_INT)
138                         ::append<uint32_t>(data, 0xFFFFFFFF);
139                 else
140                         ::append<uint16_t>(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         mark_dirty();
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_t>(data, i);
172         else
173                 ::append<uint16_t>(data, i);
174 }
175
176 unsigned Batch::get_index(size_t i) const
177 {
178         if(index_type==UNSIGNED_INT)
179                 return *(uint32_t *)&data[i*sizeof(uint32_t)];
180         else
181                 return *(uint16_t *)&data[i*sizeof(uint16_t)];
182 }
183
184
185 Batch::Loader::Loader(Batch &b):
186         DataFile::ObjectLoader<Batch>(b)
187 {
188         add("indices", &Loader::indices);
189         add("patch_size", &Loader::patch_size);
190 }
191
192 void Batch::Loader::indices(const vector<unsigned> &ind)
193 {
194         obj.append(ind);
195 }
196
197 void Batch::Loader::patch_size(unsigned s)
198 {
199         obj.set_patch_size(s);
200 }
201
202 } // namespace GL
203 } // namespace Msp