]> git.tdb.fi Git - libs/gl.git/blob - source/render/renderer.cpp
Refactor Renderer's texture management to make it more extensible
[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 template<typename T>
185 void Renderer::set_resource(vector<BoundResource<T>> &stack, unsigned &count, Tag tag, const T &res)
186 {
187         if(stack.size()>count)
188         {
189                 BoundResource<T> &top = stack[count];
190                 if(top.tag==tag && top.resource==res)
191                 {
192                         ++count;
193                         return;
194                 }
195                 else
196                         flush_resources(stack, count);
197         }
198
199         for(auto i=stack.end(); i!=stack.begin(); )
200                 if((--i)->tag==tag)
201                 {
202                         i->replaced = stack.size();
203                         break;
204                 }
205
206         stack.emplace_back();
207         BoundResource<T> &bound_res = stack.back();
208         bound_res.tag = tag;
209         bound_res.resource = res;
210         count = stack.size();
211 }
212
213 void Renderer::flush_shader_data()
214 {
215         const State &state = get_state();
216
217         if(shdata_stack.size()>state.shdata_count)
218                 shdata_stack.erase(shdata_stack.begin()+state.shdata_count, shdata_stack.end());
219 }
220
221 template<typename T>
222 void Renderer::flush_resources(vector<BoundResource<T>> &stack, unsigned &count)
223 {
224         for(unsigned i=0; i<count; ++i)
225                 if(stack[i].replaced>=static_cast<int>(count))
226                         stack[i].replaced = -1;
227
228         stack.erase(stack.begin()+count, stack.end());
229 }
230
231 void Renderer::set_vertex_setup(const VertexSetup *vs)
232 {
233         get_state().vertex_setup = vs;
234 }
235
236 void Renderer::set_front_face(FaceWinding winding)
237 {
238         get_state().front_face = winding;
239 }
240
241 void Renderer::set_face_cull(CullMode cull)
242 {
243         get_state().face_cull = cull;
244 }
245
246 void Renderer::set_depth_test(const DepthTest *dt)
247 {
248         get_state().depth_test = dt;
249 }
250
251 void Renderer::set_stencil_test(const StencilTest *st)
252 {
253         get_state().stencil_test = st;
254 }
255
256 void Renderer::set_blend(const Blend *b)
257 {
258         get_state().blend = b;
259 }
260
261 void Renderer::set_object_lod_bias(unsigned b)
262 {
263         get_state().object_lod_bias = b;
264 }
265
266 void Renderer::clear(const ClearValue *values)
267 {
268         apply_framebuffer();
269         commands.use_pipeline(&get_pipeline_state());
270         commands.clear(values);
271 }
272
273 void Renderer::draw(const Batch &batch)
274 {
275         apply_state();
276         batch.refresh(frame_index);
277         PipelineState &ps = get_pipeline_state();
278         ps.set_primitive_type(batch.get_type());
279         commands.use_pipeline(&ps);
280         commands.draw(batch);
281 }
282
283 void Renderer::draw_instanced(const Batch &batch, unsigned count)
284 {
285         apply_state();
286         batch.refresh(frame_index);
287         PipelineState &ps = get_pipeline_state();
288         ps.set_primitive_type(batch.get_type());
289         commands.use_pipeline(&ps);
290         commands.draw_instanced(batch, count);
291 }
292
293 void Renderer::resolve_multisample(Framebuffer &target)
294 {
295         const State &state = get_state();
296
297         if(!state.framebuffer)
298                 throw invalid_operation("Renderer::resolve_multisample");
299
300         unsigned width = state.framebuffer->get_width();
301         unsigned height = state.framebuffer->get_height();
302         if(target.get_width()!=width || target.get_height()!=height)
303                 throw incompatible_data("Renderer::resolve_multisample");
304
305         apply_framebuffer();
306         commands.use_pipeline(&get_pipeline_state());
307         commands.resolve_multisample(target);
308 }
309
310 void Renderer::begin_query(const QueryPool &pool, unsigned index)
311 {
312         commands.begin_query(pool, index);
313 }
314
315 void Renderer::end_query(const QueryPool &pool, unsigned index)
316 {
317         commands.end_query(pool, index);
318 }
319
320 PipelineState &Renderer::get_pipeline_state()
321 {
322         if(changed&PIPELINE_KEY)
323         {
324                 RendererBackend::set_pipeline_key(current_state->pipeline_key);
325                 changed &= ~PIPELINE_KEY;
326         }
327
328         return RendererBackend::get_pipeline_state();
329 }
330
331 void Renderer::apply_framebuffer()
332 {
333         const State &state = get_state();
334
335         PipelineState &ps = get_pipeline_state();
336
337         ps.set_framebuffer(state.framebuffer);
338         static const Rect default_rect = Rect::max();
339         ps.set_viewport(state.viewport ? *state.viewport : default_rect);
340         ps.set_scissor(state.scissor ? *state.scissor : default_rect);
341 }
342
343 void Renderer::apply_state()
344 {
345         State &state = get_state();
346
347         if(!state.shprog)
348                 throw invalid_operation("Renderer::apply_state");
349
350         apply_framebuffer();
351
352         PipelineState &ps = get_pipeline_state();
353         bool pipeline_changed = (&ps!=last_pipeline);
354         last_pipeline = &ps;
355
356         bool shprog_changed = (state.shprog!=ps.get_shader_program());
357         ps.set_shader_program(state.shprog);
358
359         bool shdata_changed = changed&SHADER_DATA;
360         for(auto i=shdata_stack.begin(); (!shdata_changed && i!=shdata_stack.end()); ++i)
361                 shdata_changed = (i->shdata->get_generation()!=i->generation);
362         bool extra_shdata = (shdata_stack.size()>state.shdata_count);
363
364         if(changed&CAMERA)
365         {
366                 shdata_changed = true;
367                 changed &= ~CAMERA;
368         }
369
370         if(changed&MATRIX)
371         {
372                 standard_shdata.uniform(world_obj_matrix_tag, state.model_matrix);
373                 LinAl::Matrix<float, 3, 3> nm = state.model_matrix.block<3, 3>(0, 0);
374                 nm = transpose(invert(nm));
375                 standard_shdata.uniform(world_obj_normal_matrix_tag, nm);
376                 changed &= ~MATRIX;
377                 shdata_changed = true;
378         }
379
380         if(shdata_changed || shprog_changed || pipeline_changed || extra_shdata)
381         {
382                 if(extra_shdata)
383                         shdata_stack.erase(shdata_stack.begin()+state.shdata_count, shdata_stack.end());
384                 standard_shdata.apply(*state.shprog, ps, frame_index);
385                 if(state.camera)
386                         state.camera->get_shader_data().apply(*state.shprog, ps, frame_index);
387                 for(const BoundProgramData &d: shdata_stack)
388                 {
389                         d.shdata->apply(*state.shprog, ps, frame_index);
390                         d.generation = d.shdata->get_generation();
391                 }
392                 changed &= ~SHADER_DATA;
393         }
394
395         if(state.vertex_setup)
396         {
397                 if(const VertexArray *array = state.vertex_setup->get_vertex_array())
398                         array->refresh(frame_index);
399                 if(const VertexArray *array = state.vertex_setup->get_instance_array())
400                         array->refresh(frame_index);
401         }
402         ps.set_vertex_setup(state.vertex_setup);
403
404         ps.set_front_face(state.front_face);
405         ps.set_face_cull(state.face_cull);
406
407         if(state.texture_count<texture_stack.size())
408                 flush_resources(texture_stack, state.texture_count);
409
410         for(const BoundResource<SampledTexture> &t: texture_stack)
411                 if(t.resource.texture && t.replaced<0)
412                 {
413                         if(t.binding<0 || shprog_changed)
414                                 t.binding = state.shprog->get_uniform_binding(t.tag);
415                         if(t.binding>=0)
416                                 ps.set_texture(t.binding, t.resource.texture, t.resource.level, t.resource.sampler);
417                 }
418
419         static const DepthTest default_depth_test;
420         ps.set_depth_test(state.depth_test ? *state.depth_test : default_depth_test);
421         static const StencilTest default_stencil_test;
422         ps.set_stencil_test(state.stencil_test ? *state.stencil_test : default_stencil_test);
423         static const Blend default_blend;
424         ps.set_blend(state.blend ? *state.blend : default_blend);
425 }
426
427
428 Renderer::BoundProgramData::BoundProgramData(const ProgramData *d):
429         shdata(d)
430 { }
431
432 } // namespace GL
433 } // namespace Msp