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