]> git.tdb.fi Git - libs/gl.git/blob - source/render/renderer.cpp
ce0b3447b2b02a43df1029db2b358cd4f8aa2cb8
[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         batch.refresh(frame_index);
283         PipelineState &ps = get_pipeline_state();
284         ps.set_primitive_type(batch.get_type());
285         commands.use_pipeline(&ps);
286         commands.draw(batch);
287 }
288
289 void Renderer::draw_instanced(const Batch &batch, unsigned count)
290 {
291         apply_state();
292         batch.refresh(frame_index);
293         PipelineState &ps = get_pipeline_state();
294         ps.set_primitive_type(batch.get_type());
295         commands.use_pipeline(&ps);
296         commands.draw_instanced(batch, count);
297 }
298
299 void Renderer::dispatch(unsigned count_x, unsigned count_y, unsigned count_z)
300 {
301         apply_state();
302         PipelineState &ps = get_pipeline_state();
303         commands.use_pipeline(&ps);
304         commands.dispatch(count_x, count_y, count_z);
305 }
306
307 void Renderer::resolve_multisample(Framebuffer &target)
308 {
309         const State &state = get_state();
310
311         if(!state.framebuffer)
312                 throw invalid_operation("Renderer::resolve_multisample");
313
314         unsigned width = state.framebuffer->get_width();
315         unsigned height = state.framebuffer->get_height();
316         if(target.get_width()!=width || target.get_height()!=height)
317                 throw incompatible_data("Renderer::resolve_multisample");
318
319         apply_framebuffer();
320         commands.use_pipeline(&get_pipeline_state());
321         commands.resolve_multisample(target);
322 }
323
324 void Renderer::begin_query(const QueryPool &pool, unsigned index)
325 {
326         commands.begin_query(pool, index);
327 }
328
329 void Renderer::end_query(const QueryPool &pool, unsigned index)
330 {
331         commands.end_query(pool, index);
332 }
333
334 PipelineState &Renderer::get_pipeline_state()
335 {
336         if(changed&PIPELINE_KEY)
337         {
338                 RendererBackend::set_pipeline_key(current_state->pipeline_key);
339                 changed &= ~PIPELINE_KEY;
340         }
341
342         return RendererBackend::get_pipeline_state();
343 }
344
345 void Renderer::apply_framebuffer()
346 {
347         const State &state = get_state();
348
349         PipelineState &ps = get_pipeline_state();
350
351         ps.set_framebuffer(state.framebuffer);
352         static const Rect default_rect = Rect::max();
353         ps.set_viewport(state.viewport ? *state.viewport : default_rect);
354         ps.set_scissor(state.scissor ? *state.scissor : default_rect);
355 }
356
357 void Renderer::apply_state()
358 {
359         State &state = get_state();
360
361         if(!state.shprog)
362                 throw invalid_operation("Renderer::apply_state");
363
364         apply_framebuffer();
365
366         PipelineState &ps = get_pipeline_state();
367         bool pipeline_changed = (&ps!=last_pipeline);
368         last_pipeline = &ps;
369
370         bool shprog_changed = (state.shprog!=ps.get_shader_program());
371         ps.set_shader_program(state.shprog);
372
373         bool shdata_changed = changed&SHADER_DATA;
374         for(auto i=shdata_stack.begin(); (!shdata_changed && i!=shdata_stack.end()); ++i)
375                 shdata_changed = (i->shdata->get_generation()!=i->generation);
376         bool extra_shdata = (shdata_stack.size()>state.shdata_count);
377
378         if(changed&CAMERA)
379         {
380                 shdata_changed = true;
381                 changed &= ~CAMERA;
382         }
383
384         if(changed&MATRIX)
385         {
386                 standard_shdata.uniform(world_obj_matrix_tag, state.model_matrix);
387                 LinAl::Matrix<float, 3, 3> nm = state.model_matrix.block<3, 3>(0, 0);
388                 nm = transpose(invert(nm));
389                 standard_shdata.uniform(world_obj_normal_matrix_tag, nm);
390                 changed &= ~MATRIX;
391                 shdata_changed = true;
392         }
393
394         if(shdata_changed || shprog_changed || pipeline_changed || extra_shdata)
395         {
396                 if(extra_shdata)
397                         shdata_stack.erase(shdata_stack.begin()+state.shdata_count, shdata_stack.end());
398                 standard_shdata.apply(*state.shprog, ps, frame_index);
399                 if(state.camera)
400                         state.camera->get_shader_data().apply(*state.shprog, ps, frame_index);
401                 for(const BoundProgramData &d: shdata_stack)
402                 {
403                         d.shdata->apply(*state.shprog, ps, frame_index);
404                         d.generation = d.shdata->get_generation();
405                 }
406                 changed &= ~SHADER_DATA;
407         }
408
409         if(state.vertex_setup)
410         {
411                 if(const VertexArray *array = state.vertex_setup->get_vertex_array())
412                         array->refresh(frame_index);
413                 if(const VertexArray *array = state.vertex_setup->get_instance_array())
414                         array->refresh(frame_index);
415         }
416         ps.set_vertex_setup(state.vertex_setup);
417
418         ps.set_front_face(state.front_face);
419         ps.set_face_cull(state.face_cull);
420
421         if(state.texture_count<texture_stack.size())
422                 flush_resources(texture_stack, state.texture_count);
423
424         for(const BoundResource<SampledTexture> &t: texture_stack)
425                 if(t.resource.texture && t.replaced<0)
426                 {
427                         if(t.binding<0 || shprog_changed)
428                                 t.binding = state.shprog->get_uniform_binding(t.tag);
429                         if(t.binding>=0)
430                         {
431                                 if(t.resource.sampler)
432                                         ps.set_texture(t.binding, t.resource.texture, t.resource.level, t.resource.sampler);
433                                 else
434                                         ps.set_storage_texture(t.binding, t.resource.texture);
435                         }
436                 }
437
438         static const DepthTest default_depth_test;
439         ps.set_depth_test(state.depth_test ? *state.depth_test : default_depth_test);
440         static const StencilTest default_stencil_test;
441         ps.set_stencil_test(state.stencil_test ? *state.stencil_test : default_stencil_test);
442         static const Blend default_blend;
443         ps.set_blend(state.blend ? *state.blend : default_blend);
444 }
445
446
447 Renderer::BoundProgramData::BoundProgramData(const ProgramData *d):
448         shdata(d)
449 { }
450
451 } // namespace GL
452 } // namespace Msp