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