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