]> git.tdb.fi Git - libs/gl.git/blob - source/render/renderer.cpp
Remove some deprecated stuff that's getting in the way
[libs/gl.git] / source / render / renderer.cpp
1 #include "batch.h"
2 #include "buffer.h"
3 #include "camera.h"
4 #include "clipping.h"
5 #include "deviceinfo.h"
6 #include "error.h"
7 #include "lighting.h"
8 #include "material.h"
9 #include "program.h"
10 #include "programdata.h"
11 #include "renderable.h"
12 #include "renderer.h"
13 #include "sampler.h"
14 #include "texture.h"
15 #include "texunit.h"
16 #include "vertexarray.h"
17 #include "vertexsetup.h"
18 #include "windingtest.h"
19
20 using namespace std;
21
22 namespace Msp {
23 namespace GL {
24
25 Renderer::Renderer()
26 {
27         state_stack.reserve(16);
28         state_stack.push_back(State());
29         shdata_stack.reserve(32);
30         state = &state_stack.back();
31         add_shader_data(standard_shdata);
32 }
33
34 Renderer::~Renderer()
35 {
36         end();
37 }
38
39 void Renderer::set_camera(const Camera &c)
40 {
41         state->camera = &c;
42         add_shader_data(c.get_shader_data());
43         set_matrix(Matrix());
44 }
45
46 void Renderer::set_matrix(const Matrix &matrix)
47 {
48         state->model_matrix = matrix;
49         changed |= MATRIX;
50 }
51
52 void Renderer::transform(const Matrix &matrix)
53 {
54         state->model_matrix *= matrix;
55         changed |= MATRIX;
56 }
57
58 void Renderer::set_texture(Tag tag, const Texture *tex, const Sampler *samp)
59 {
60         if(texture_stack.size()>state->texture_count)
61         {
62                 BoundTexture &bt = texture_stack[state->texture_count];
63                 if(bt.tag==tag && bt.texture==tex && bt.sampler==samp)
64                 {
65                         ++state->texture_count;
66                         return;
67                 }
68                 else
69                         flush_textures();
70         }
71
72         for(vector<BoundTexture>::iterator i=texture_stack.end(); i!=texture_stack.begin(); )
73                 if((--i)->tag==tag)
74                 {
75                         i->replaced = texture_stack.size();
76                         break;
77                 }
78
79         texture_stack.push_back(BoundTexture());
80         BoundTexture &bound_tex = texture_stack.back();
81         bound_tex.tag = tag;
82         bound_tex.texture = tex;
83         bound_tex.sampler = samp;
84         state->texture_count = texture_stack.size();
85 }
86
87 void Renderer::flush_textures()
88 {
89         for(unsigned i=0; i<texture_stack.size(); ++i)
90         {
91                 BoundTexture &bt = texture_stack[i];
92                 if(i>=state->texture_count && bt.unit>=0)
93                 {
94                         Texture::unbind_from(bt.unit);
95                         Sampler::unbind_from(bt.unit);
96                 }
97                 else if(bt.replaced>=static_cast<int>(state->texture_count))
98                         bt.replaced = -1;
99         }
100
101         texture_stack.erase(texture_stack.begin()+state->texture_count, texture_stack.end());
102 }
103
104 void Renderer::set_material(const Material *m)
105 {
106         if(m)
107                 add_shader_data(m->get_shader_data());
108 }
109
110 void Renderer::set_lighting(const Lighting *l)
111 {
112         if(l)
113                 add_shader_data(l->get_shader_data());
114 }
115
116 void Renderer::set_clipping(const Clipping *c)
117 {
118         state->clipping = c;
119         if(c)
120                 add_shader_data(c->get_shader_data());
121 }
122
123 void Renderer::set_shader_program(const Program *p, const ProgramData *d)
124 {
125         state->shprog = p;
126         if(p && d)
127                 add_shader_data(*d);
128 }
129
130 void Renderer::add_shader_data(const ProgramData &d)
131 {
132         if(state->shdata_count<shdata_stack.size())
133         {
134                 const BoundProgramData &top = shdata_stack.back();
135                 if(top.shdata==&d && top.generation==d.get_generation())
136                 {
137                         ++state->shdata_count;
138                         return;
139                 }
140         }
141
142         flush_shader_data_();
143         shdata_stack.push_back(&d);
144         state->shdata_count = shdata_stack.size();
145         changed |= SHADER_DATA;
146 }
147
148 void Renderer::flush_shader_data_()
149 {
150         if(shdata_stack.size()>state->shdata_count)
151                 shdata_stack.erase(shdata_stack.begin()+state->shdata_count, shdata_stack.end());
152 }
153
154 void Renderer::set_vertex_setup(const VertexSetup *vs)
155 {
156         state->vertex_setup = vs;
157 }
158
159 void Renderer::set_winding_test(const WindingTest *w)
160 {
161         state->winding_test = w;
162 }
163
164 void Renderer::set_reverse_winding(bool r)
165 {
166         state->reverse_winding = r;
167 }
168
169 void Renderer::set_object_lod_bias(unsigned b)
170 {
171         state->object_lod_bias = b;
172 }
173
174 void Renderer::push_state()
175 {
176         state_stack.push_back(state_stack.back());
177         state = &state_stack.back();
178 }
179
180 void Renderer::pop_state()
181 {
182         if(state_stack.size()==1)
183                 throw stack_underflow("Renderer::pop_state");
184
185         state_stack.pop_back();
186         state = &state_stack.back();
187         changed |= MATRIX;
188 }
189
190 void Renderer::end()
191 {
192         if(state_stack.size()>1)
193                 throw invalid_operation("Renderer::end");
194
195         *state = State();
196         shdata_stack.clear();
197         excluded.clear();
198
199         for(vector<BoundTexture>::iterator i=texture_stack.begin(); i!=texture_stack.end(); ++i)
200                 if(i->unit>=0)
201                 {
202                         Texture::unbind_from(i->unit);
203                         Sampler::unbind_from(i->unit);
204                 }
205         Clipping::unbind();
206         Program::unbind();
207         VertexSetup::unbind();
208         Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
209         WindingTest::unbind();
210 }
211
212 void Renderer::exclude(const Renderable &renderable)
213 {
214         excluded.insert(&renderable);
215 }
216
217 void Renderer::include(const Renderable &renderable)
218 {
219         excluded.erase(&renderable);
220 }
221
222 void Renderer::render(const Renderable &renderable, Tag tag)
223 {
224         if(!excluded.count(&renderable))
225                 renderable.render(*this, tag);
226 }
227
228 void Renderer::draw(const Batch &batch)
229 {
230         apply_state();
231
232         batch.draw();
233 }
234
235 void Renderer::draw_instanced(const Batch &batch, unsigned count)
236 {
237         apply_state();
238
239         batch.draw_instanced(count);
240 }
241
242 void Renderer::apply_state()
243 {
244         if(!state->shprog)
245                 throw invalid_operation("Renderer::apply_state");
246
247         /* We (mostly) let the objects themselves figure out if the binding has
248         changed */
249
250         if(state->texture_count<texture_stack.size())
251                 flush_textures();
252
253         for(vector<BoundTexture>::const_iterator i=texture_stack.begin(); i!=texture_stack.end(); ++i)
254         {
255                 int unit = state->shprog->get_uniform_binding(i->tag);
256                 if(unit>=0)
257                 {
258                         if(i->texture)
259                                 i->texture->bind_to(unit);
260                         if(i->sampler)
261                                 i->sampler->bind_to(unit);
262                         i->unit = unit;
263                 }
264         }
265
266         if(state->clipping)
267                 state->clipping->bind();
268         else
269                 Clipping::unbind();
270
271         bool shprog_changed = (state->shprog!=Program::current());
272         state->shprog->bind();
273
274         if(changed&MATRIX)
275         {
276                 standard_shdata.uniform("world_obj_matrix", state->model_matrix);
277                 LinAl::SquareMatrix<float, 3> nm = state->model_matrix.block<3, 3>(0, 0);
278                 nm = transpose(invert(nm));
279                 standard_shdata.uniform("world_obj_normal_matrix", nm);
280                 changed &= ~MATRIX;
281         }
282
283         bool shdata_changed = changed&SHADER_DATA;
284         for(vector<BoundProgramData>::const_iterator i=shdata_stack.begin(); (!shdata_changed && i!=shdata_stack.end()); ++i)
285                 shdata_changed = (i->shdata->get_generation()!=i->generation);
286         bool extra_shdata = (shdata_stack.size()>state->shdata_count);
287
288         if(shdata_changed || shprog_changed || extra_shdata)
289         {
290                 if(extra_shdata)
291                         shdata_stack.erase(shdata_stack.begin()+state->shdata_count, shdata_stack.end());
292                 for(vector<BoundProgramData>::const_iterator i=shdata_stack.begin(); i!=shdata_stack.end(); ++i)
293                 {
294                         i->shdata->apply();
295                         i->generation = i->shdata->get_generation();
296                 }
297                 changed &= ~SHADER_DATA;
298         }
299
300         if(state->vertex_setup)
301                 state->vertex_setup->bind();
302         else
303                 VertexSetup::unbind();
304
305         if(state->winding_test)
306         {
307                 if(state->reverse_winding)
308                         state->winding_test->get_reverse().bind();
309                 else
310                         state->winding_test->bind();
311         }
312         else
313                 WindingTest::unbind();
314 }
315
316
317 Renderer::BoundTexture::BoundTexture():
318         unit(-1),
319         texture(0),
320         sampler(0),
321         replaced(-1)
322 { }
323
324
325 Renderer::BoundProgramData::BoundProgramData(const ProgramData *d):
326         shdata(d),
327         generation(0)
328 { }
329
330
331 Renderer::State::State():
332         camera(0),
333         texture_count(0),
334         lowest_effect_texunit(Limits::get_global().max_texture_bindings),
335         clipping(0),
336         shprog(0),
337         shdata_count(0),
338         vertex_setup(0),
339         winding_test(0),
340         reverse_winding(false),
341         object_lod_bias(0)
342 { }
343
344 } // namespace GL
345 } // namespace Msp