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