]> git.tdb.fi Git - libs/gl.git/blob - source/render/renderer.cpp
Check the flat qualifier from the correct member
[libs/gl.git] / source / render / renderer.cpp
1 #include "batch.h"
2 #include "buffer.h"
3 #include "camera.h"
4 #include "error.h"
5 #include "framebuffer.h"
6 #include "lighting.h"
7 #include "material.h"
8 #include "program.h"
9 #include "programdata.h"
10 #include "renderable.h"
11 #include "renderer.h"
12 #include "resourcemanager.h"
13 #include "resources.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 const Tag Renderer::world_obj_matrix_tag("world_obj_matrix");
25 const Tag Renderer::world_obj_normal_matrix_tag("world_obj_normal_matrix");
26
27 Renderer::Renderer():
28         placeholder_texture(Resources::get_global().get<Texture>("_placeholder.png")),
29         default_sampler(Resources::get_global().get<Sampler>("_linear_clamp.samp"))
30 {
31         state_stack.reserve(16);
32         shdata_stack.reserve(32);
33         texture_stack.reserve(32);
34 }
35
36 void Renderer::begin()
37 {
38         if(current_state)
39                 throw invalid_operation("Renderer::begin");
40
41         ++frame_index;
42         state_stack.emplace_back();
43         current_state = &state_stack.back();
44
45         RendererBackend::begin();
46
47         commands.begin_frame(frame_index);
48 }
49
50 void Renderer::end()
51 {
52         if(!current_state || state_stack.size()>1)
53                 throw invalid_operation("Renderer::end");
54
55         RendererBackend::end();
56
57         current_state = 0;
58         state_stack.clear();
59         texture_stack.clear();
60         shdata_stack.clear();
61 }
62
63 void Renderer::push_state()
64 {
65         if(state_stack.empty())
66                 throw invalid_operation("Renderer::push_state");
67
68         state_stack.push_back(state_stack.back());
69         current_state = &state_stack.back();
70 }
71
72 void Renderer::pop_state()
73 {
74         if(state_stack.size()<=1)
75                 throw stack_underflow("Renderer::pop_state");
76
77         uintptr_t old_pipeline = current_state->pipeline_key;
78
79         state_stack.pop_back();
80         current_state = &state_stack.back();
81         changed |= MATRIX;
82
83         if(current_state->pipeline_key!=old_pipeline)
84                 changed |= PIPELINE_KEY;
85 }
86
87 Renderer::State &Renderer::get_state() const
88 {
89 #ifdef DEBUG
90         if(!current_state)
91                 throw invalid_operation("Renderer::get_state");
92 #endif
93         return *current_state;
94 }
95
96 void Renderer::set_pipeline_key(uintptr_t key)
97 {
98         State &state = get_state();
99         if(key!=state.pipeline_key)
100         {
101                 state.pipeline_key = key;
102                 changed |= PIPELINE_KEY;
103         }
104 }
105
106 void Renderer::set_camera(const Camera &c)
107 {
108         get_state().camera = &c;
109         set_matrix(Matrix());
110 }
111
112 void Renderer::set_matrix(const Matrix &matrix)
113 {
114         get_state().model_matrix = matrix;
115         changed |= MATRIX;
116 }
117
118 void Renderer::set_framebuffer(const Framebuffer *f)
119 {
120         get_state().framebuffer = f;
121 }
122
123 void Renderer::set_viewport(const Rect *v)
124 {
125         get_state().viewport = v;
126 }
127
128 void Renderer::set_scissor(const Rect *s)
129 {
130         get_state().scissor = s;
131 }
132
133 void Renderer::set_shader_program(const Program *p, const ProgramData *d)
134 {
135         get_state().shprog = p;
136         if(p && d)
137                 add_shader_data(*d);
138 }
139
140 void Renderer::add_shader_data(const ProgramData &d)
141 {
142         State &state = get_state();
143
144         if(state.shdata_count<shdata_stack.size())
145         {
146                 const BoundProgramData &top = shdata_stack.back();
147                 if(top.shdata==&d && top.generation==d.get_generation())
148                 {
149                         ++state.shdata_count;
150                         return;
151                 }
152         }
153
154         flush_shader_data();
155         shdata_stack.push_back(&d);
156         state.shdata_count = shdata_stack.size();
157         changed |= SHADER_DATA;
158 }
159
160 void Renderer::set_texture(Tag tag, const Texture *tex, const Sampler *samp)
161 {
162         set_texture(tag, tex, -1, samp);
163 }
164
165 void Renderer::set_texture(Tag tag, const Texture *tex, int level, const Sampler *samp)
166 {
167         State &state = get_state();
168
169         if(tex)
170         {
171                 if(ResourceManager *res_mgr = tex->get_manager())
172                         res_mgr->resource_used(*tex);
173                 if(!tex->is_loaded())
174                         tex = &placeholder_texture;
175                 if(!samp)
176                         samp = &default_sampler;
177         }
178         else
179                 samp = 0;
180
181         set_resource(texture_stack, state.texture_count, tag, { tex, samp, level });
182 }
183
184 void Renderer::set_storage_texture(Tag tag, const Texture *tex)
185 {
186         State &state = get_state();
187         set_resource(texture_stack, state.texture_count, tag, { tex, 0, 0 });
188 }
189
190 template<typename T>
191 void Renderer::set_resource(vector<BoundResource<T>> &stack, unsigned &count, Tag tag, const T &res)
192 {
193         if(stack.size()>count)
194         {
195                 BoundResource<T> &top = stack[count];
196                 if(top.tag==tag && top.resource==res)
197                 {
198                         ++count;
199                         return;
200                 }
201                 else
202                         flush_resources(stack, count);
203         }
204
205         for(auto i=stack.end(); i!=stack.begin(); )
206                 if((--i)->tag==tag)
207                 {
208                         i->replaced = stack.size();
209                         break;
210                 }
211
212         stack.emplace_back();
213         BoundResource<T> &bound_res = stack.back();
214         bound_res.tag = tag;
215         bound_res.resource = res;
216         count = stack.size();
217 }
218
219 void Renderer::flush_shader_data()
220 {
221         const State &state = get_state();
222
223         if(shdata_stack.size()>state.shdata_count)
224                 shdata_stack.erase(shdata_stack.begin()+state.shdata_count, shdata_stack.end());
225 }
226
227 template<typename T>
228 void Renderer::flush_resources(vector<BoundResource<T>> &stack, unsigned &count)
229 {
230         for(unsigned i=0; i<count; ++i)
231                 if(stack[i].replaced>=static_cast<int>(count))
232                         stack[i].replaced = -1;
233
234         stack.erase(stack.begin()+count, stack.end());
235 }
236
237 void Renderer::set_vertex_setup(const VertexSetup *vs)
238 {
239         get_state().vertex_setup = vs;
240 }
241
242 void Renderer::set_front_face(FaceWinding winding)
243 {
244         get_state().front_face = winding;
245 }
246
247 void Renderer::set_face_cull(CullMode cull)
248 {
249         get_state().face_cull = cull;
250 }
251
252 void Renderer::set_depth_test(const DepthTest *dt)
253 {
254         get_state().depth_test = dt;
255 }
256
257 void Renderer::set_stencil_test(const StencilTest *st)
258 {
259         get_state().stencil_test = st;
260 }
261
262 void Renderer::set_blend(const Blend *b)
263 {
264         get_state().blend = b;
265 }
266
267 void Renderer::set_object_lod_bias(unsigned b)
268 {
269         get_state().object_lod_bias = b;
270 }
271
272 void Renderer::clear(const ClearValue *values)
273 {
274         apply_framebuffer();
275         commands.use_pipeline(&get_pipeline_state());
276         commands.clear(values);
277 }
278
279 void Renderer::draw(const Batch &batch)
280 {
281         apply_state();
282         apply_batch(batch);
283         commands.draw(batch);
284 }
285
286 void Renderer::draw_instanced(const Batch &batch, unsigned count)
287 {
288         apply_state();
289         apply_batch(batch);
290         commands.draw_instanced(batch, count);
291 }
292
293 void Renderer::apply_batch(const Batch &batch)
294 {
295         batch.refresh(frame_index);
296         PipelineState &ps = get_pipeline_state();
297         ps.set_primitive_type(batch.get_type());
298         if(batch.get_type()==PATCHES)
299                 ps.set_patch_size(batch.get_patch_size());
300         commands.use_pipeline(&ps);
301 }
302
303 void Renderer::dispatch(unsigned count_x, unsigned count_y, unsigned count_z)
304 {
305         apply_state();
306         PipelineState &ps = get_pipeline_state();
307         commands.use_pipeline(&ps);
308         commands.dispatch(count_x, count_y, count_z);
309 }
310
311 void Renderer::resolve_multisample()
312 {
313         const State &state = get_state();
314
315         if(!state.framebuffer)
316                 throw invalid_operation("Renderer::resolve_multisample");
317
318         apply_framebuffer();
319         commands.use_pipeline(&get_pipeline_state());
320         commands.resolve_multisample();
321 }
322
323 void Renderer::begin_query(const QueryPool &pool, unsigned index)
324 {
325         commands.begin_query(pool, index);
326 }
327
328 void Renderer::end_query(const QueryPool &pool, unsigned index)
329 {
330         commands.end_query(pool, index);
331 }
332
333 PipelineState &Renderer::get_pipeline_state()
334 {
335         if(changed&PIPELINE_KEY)
336         {
337                 RendererBackend::set_pipeline_key(current_state->pipeline_key);
338                 changed &= ~PIPELINE_KEY;
339         }
340
341         return RendererBackend::get_pipeline_state();
342 }
343
344 void Renderer::apply_framebuffer()
345 {
346         const State &state = get_state();
347
348         PipelineState &ps = get_pipeline_state();
349
350         ps.set_framebuffer(state.framebuffer);
351         static const Rect default_rect = Rect::max();
352         ps.set_viewport(state.viewport ? *state.viewport : default_rect);
353         ps.set_scissor(state.scissor ? *state.scissor : default_rect);
354 }
355
356 void Renderer::apply_state()
357 {
358         State &state = get_state();
359
360         if(!state.shprog)
361                 throw invalid_operation("Renderer::apply_state");
362
363         apply_framebuffer();
364
365         PipelineState &ps = get_pipeline_state();
366         bool pipeline_changed = (&ps!=last_pipeline);
367         last_pipeline = &ps;
368
369         bool shprog_changed = (state.shprog!=ps.get_shader_program());
370         ps.set_shader_program(state.shprog);
371
372         bool shdata_changed = changed&SHADER_DATA;
373         for(auto i=shdata_stack.begin(); (!shdata_changed && i!=shdata_stack.end()); ++i)
374                 shdata_changed = (i->shdata->get_generation()!=i->generation);
375         bool extra_shdata = (shdata_stack.size()>state.shdata_count);
376
377         if(changed&CAMERA)
378         {
379                 shdata_changed = true;
380                 changed &= ~CAMERA;
381         }
382
383         if(changed&MATRIX)
384         {
385                 standard_shdata.uniform(world_obj_matrix_tag, state.model_matrix);
386                 LinAl::Matrix<float, 3, 3> nm = state.model_matrix.block<3, 3>(0, 0);
387                 nm = transpose(invert(nm));
388                 standard_shdata.uniform(world_obj_normal_matrix_tag, nm);
389                 changed &= ~MATRIX;
390                 shdata_changed = true;
391         }
392
393         if(shdata_changed || shprog_changed || pipeline_changed || extra_shdata)
394         {
395                 if(extra_shdata)
396                         shdata_stack.erase(shdata_stack.begin()+state.shdata_count, shdata_stack.end());
397                 standard_shdata.apply(*state.shprog, ps, frame_index);
398                 if(state.camera)
399                         state.camera->get_shader_data().apply(*state.shprog, ps, frame_index);
400                 for(const BoundProgramData &d: shdata_stack)
401                 {
402                         d.shdata->apply(*state.shprog, ps, frame_index);
403                         d.generation = d.shdata->get_generation();
404                 }
405                 changed &= ~SHADER_DATA;
406         }
407
408         if(state.vertex_setup)
409         {
410                 if(const VertexArray *array = state.vertex_setup->get_vertex_array())
411                         array->refresh(frame_index);
412                 if(const VertexArray *array = state.vertex_setup->get_instance_array())
413                         array->refresh(frame_index);
414         }
415         ps.set_vertex_setup(state.vertex_setup);
416
417         ps.set_front_face(state.front_face);
418         ps.set_face_cull(state.face_cull);
419
420         if(state.texture_count<texture_stack.size())
421                 flush_resources(texture_stack, state.texture_count);
422
423         for(const BoundResource<SampledTexture> &t: texture_stack)
424                 if(t.resource.texture && t.replaced<0)
425                 {
426                         if(t.binding<0 || shprog_changed)
427                                 t.binding = state.shprog->get_uniform_binding(t.tag);
428                         if(t.binding>=0)
429                         {
430                                 if(t.resource.sampler)
431                                         ps.set_texture(t.binding, t.resource.texture, t.resource.level, t.resource.sampler);
432                                 else
433                                         ps.set_storage_texture(t.binding, t.resource.texture);
434                         }
435                 }
436
437         static const DepthTest default_depth_test;
438         ps.set_depth_test(state.depth_test ? *state.depth_test : default_depth_test);
439         static const StencilTest default_stencil_test;
440         ps.set_stencil_test(state.stencil_test ? *state.stencil_test : default_stencil_test);
441         static const Blend default_blend;
442         ps.set_blend(state.blend ? *state.blend : default_blend);
443 }
444
445
446 Renderer::BoundProgramData::BoundProgramData(const ProgramData *d):
447         shdata(d)
448 { }
449
450 } // namespace GL
451 } // namespace Msp