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