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