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