]> git.tdb.fi Git - libs/gl.git/blob - source/core/pipelinestate.cpp
Convert framebuffers and related functionality to new state management
[libs/gl.git] / source / core / pipelinestate.cpp
1 #include <stdexcept>
2 #include <msp/core/algorithm.h>
3 #include <msp/gl/extensions/arb_direct_state_access.h>
4 #include <msp/gl/extensions/arb_sampler_objects.h>
5 #include <msp/gl/extensions/arb_shader_objects.h>
6 #include <msp/gl/extensions/arb_uniform_buffer_object.h>
7 #include <msp/gl/extensions/arb_vertex_array_object.h>
8 #include "blend.h"
9 #include "buffer.h"
10 #include "deviceinfo.h"
11 #include "depthtest.h"
12 #include "framebuffer.h"
13 #include "pipelinestate.h"
14 #include "program.h"
15 #include "rect.h"
16 #include "stenciltest.h"
17 #include "texture.h"
18 #include "uniformblock.h"
19 #include "vertexsetup.h"
20
21 using namespace std;
22
23 namespace Msp {
24 namespace GL {
25
26 const PipelineState *PipelineState::last_applied = 0;
27 vector<int> PipelineState::bound_tex_targets;
28
29 PipelineState::PipelineState():
30         framebuffer(0),
31         viewport(0),
32         scissor(0),
33         shprog(0),
34         vertex_setup(0),
35         front_face(COUNTERCLOCKWISE),
36         face_cull(NO_CULL),
37         enabled_clip_planes(0),
38         depth_test(0),
39         stencil_test(0),
40         blend(0),
41         changes(0)
42 {
43         if(!ARB_direct_state_access && bound_tex_targets.empty())
44                 bound_tex_targets.resize(Limits::get_global().max_texture_bindings);
45 }
46
47 PipelineState::~PipelineState()
48 {
49         if(this==last_applied)
50                 last_applied = 0;
51 }
52
53 template<typename T>
54 void PipelineState::set(T &target, T value, unsigned flag)
55 {
56         if(value!=target)
57         {
58                 target = value;
59                 changes |= flag;
60         }
61 }
62
63 void PipelineState::set_framebuffer(const Framebuffer *f)
64 {
65         set(framebuffer, f, FRAMEBUFFER|VIEWPORT);
66 }
67
68 void PipelineState::set_viewport(const Rect *v)
69 {
70         set(viewport, v, VIEWPORT);
71 }
72
73 void PipelineState::set_scissor(const Rect *s)
74 {
75         set(scissor, s, SCISSOR);
76 }
77
78 void PipelineState::set_shader_program(const Program *p)
79 {
80         set(shprog, p, SHPROG);
81 }
82
83 void PipelineState::set_vertex_setup(const VertexSetup *s)
84 {
85         set(vertex_setup, s, VERTEX_SETUP);
86 }
87
88 void PipelineState::set_front_face(FaceWinding w)
89 {
90         set(front_face, w, FACE_CULL);
91 }
92
93 void PipelineState::set_face_cull(CullMode c)
94 {
95         set(face_cull, c, FACE_CULL);
96 }
97
98 void PipelineState::set_enabled_clip_planes(unsigned p)
99 {
100         set(enabled_clip_planes, p, CLIP_PLANES);
101 }
102
103 void PipelineState::set_texture(unsigned binding, const Texture *tex, const Sampler *samp)
104 {
105         if((tex!=0)!=(samp!=0))
106                 throw invalid_argument("PipelineState::set_texture");
107
108         vector<BoundTexture>::iterator i = lower_bound_member(textures, binding, &BoundTexture::binding);
109         if(i==textures.end() || i->binding!=binding)
110                 i = textures.insert(i, BoundTexture(binding));
111         if(tex!=i->texture || samp!=i->sampler)
112         {
113                 i->texture = tex;
114                 i->sampler = samp;
115                 i->changed = true;
116                 changes |= TEXTURES;
117         }
118 }
119
120 void PipelineState::set_uniforms(const DefaultUniformBlock *block)
121 {
122         set_uniform_block_(-1, block);
123 }
124
125 void PipelineState::set_uniform_block(unsigned binding, const BufferBackedUniformBlock *block)
126 {
127         set_uniform_block_(binding, block);
128 }
129
130 void PipelineState::set_uniform_block_(int binding, const UniformBlock *block)
131 {
132         vector<BoundUniformBlock>::iterator i = lower_bound_member(uniform_blocks, binding, &BoundUniformBlock::binding);
133         if(i==uniform_blocks.end() || i->binding!=binding)
134                 i = uniform_blocks.insert(i, BoundUniformBlock(binding));
135         if(block!=i->block || binding<0)
136         {
137                 i->block = block;
138                 i->changed = true;
139                 changes |= UNIFORMS;
140         }
141 }
142
143 void PipelineState::set_depth_test(const DepthTest *dt)
144 {
145         set(depth_test, dt, DEPTH_TEST);
146 }
147
148 void PipelineState::set_stencil_test(const StencilTest *st)
149 {
150         set(stencil_test, st, STENCIL_TEST);
151 }
152
153 void PipelineState::set_blend(const Blend *b)
154 {
155         set(blend, b, BLEND);
156 }
157
158 void PipelineState::apply() const
159 {
160         apply(this==last_applied ? changes : ~0U);
161 }
162
163 void PipelineState::apply(unsigned mask) const
164 {
165         if(mask&FRAMEBUFFER)
166         {
167                 glBindFramebuffer(GL_FRAMEBUFFER, framebuffer ? framebuffer->get_id() : 0);
168                 if(framebuffer)
169                 {
170                         framebuffer->refresh();
171                         framebuffer->require_complete();
172                 }
173         }
174
175         if(mask&VIEWPORT)
176         {
177                 if(viewport)
178                         glViewport(viewport->left, viewport->bottom, viewport->width, viewport->height);
179                 else if(framebuffer)
180                         glViewport(0, 0, framebuffer->get_width(), framebuffer->get_height());
181         }
182
183         if(mask&SCISSOR)
184         {
185                 if(scissor)
186                 {
187                         glEnable(GL_SCISSOR_TEST);
188                         glScissor(scissor->left, scissor->bottom, scissor->width, scissor->height);
189                 }
190                 else
191                         glDisable(GL_SCISSOR_TEST);
192         }
193
194         if(mask&SHPROG)
195                 glUseProgram(shprog ? shprog->get_id() : 0);
196
197         if(mask&VERTEX_SETUP)
198         {
199                 glBindVertexArray(vertex_setup ? vertex_setup->get_id() : 0);
200                 if(vertex_setup)
201                         vertex_setup->refresh();
202         }
203
204         if(mask&FACE_CULL)
205         {
206                 glFrontFace(front_face==CLOCKWISE ? GL_CW : GL_CCW);
207
208                 if(face_cull!=NO_CULL && front_face!=NON_MANIFOLD)
209                 {
210                         glEnable(GL_CULL_FACE);
211                         glCullFace(face_cull==CULL_FRONT ? GL_FRONT : GL_BACK);
212                 }
213                 else
214                         glDisable(GL_CULL_FACE);
215         }
216
217         if(mask&CLIP_PLANES)
218         {
219                 unsigned max_clip_planes = Limits::get_global().max_clip_planes;
220                 for(unsigned i=0; i<max_clip_planes; ++i)
221                 {
222                         if((enabled_clip_planes>>i)&1)
223                                 glEnable(GL_CLIP_PLANE0+i);
224                         else
225                                 glDisable(GL_CLIP_PLANE0+i);
226                 }
227         }
228
229         if(mask&TEXTURES)
230         {
231                 if(last_applied && this!=last_applied)
232                 {
233                         vector<BoundTexture>::const_iterator i = textures.begin();
234                         vector<BoundTexture>::const_iterator j = last_applied->textures.begin();
235                         while(j!=last_applied->textures.end())
236                         {
237                                 if(i==textures.end() || j->binding<i->binding)
238                                 {
239                                         if(bound_tex_targets[j->binding])
240                                         {
241                                                 if(ARB_direct_state_access)
242                                                         glBindTextureUnit(j->binding, 0);
243                                                 else
244                                                 {
245                                                         glActiveTexture(GL_TEXTURE0+j->binding);
246                                                         glBindTexture(bound_tex_targets[j->binding], 0);
247                                                 }
248                                         }
249                                         ++j;
250                                 }
251                                 else
252                                 {
253                                         if(i->binding==j->binding)
254                                                 ++j;
255                                         ++i;
256                                 }
257                         }
258                 }
259
260                 for(vector<BoundTexture>::const_iterator i=textures.begin(); i!=textures.end(); ++i)
261                         if(i->changed || mask==~0U)
262                         {
263                                 if(i->texture && i->sampler)
264                                 {
265                                         if(ARB_direct_state_access)
266                                                 glBindTextureUnit(i->binding, i->texture->get_id());
267                                         else
268                                         {
269                                                 glActiveTexture(GL_TEXTURE0+i->binding);
270                                                 if(bound_tex_targets[i->binding] && static_cast<int>(i->texture->get_target())!=bound_tex_targets[i->binding])
271                                                         glBindTexture(bound_tex_targets[i->binding], 0);
272                                                 glBindTexture(i->texture->get_target(), i->texture->get_id());
273                                                 bound_tex_targets[i->binding] = i->texture->get_target();
274                                         }
275
276                                         glBindSampler(i->binding, i->sampler->get_id());
277                                         i->sampler->refresh();
278                                 }
279                                 else if(bound_tex_targets[i->binding])
280                                 {
281                                         if(ARB_direct_state_access)
282                                                 glBindTextureUnit(i->binding, 0);
283                                         else
284                                         {
285                                                 glActiveTexture(GL_TEXTURE0+i->binding);
286                                                 glBindTexture(bound_tex_targets[i->binding], 0);
287                                                 bound_tex_targets[i->binding] = 0;
288                                         }
289                                 }
290
291                                 i->changed = false;
292                         }
293         }
294
295         if(mask&UNIFORMS)
296         {
297                 if(last_applied && this!=last_applied)
298                 {
299                         vector<BoundUniformBlock>::const_iterator i = uniform_blocks.begin();
300                         vector<BoundUniformBlock>::const_iterator j = last_applied->uniform_blocks.begin();
301                         while(j!=last_applied->uniform_blocks.end())
302                         {
303                                 if(i==uniform_blocks.end() || j->binding<i->binding)
304                                 {
305                                         glBindBufferBase(GL_UNIFORM_BUFFER, j->binding, 0);
306                                         ++j;
307                                 }
308                                 else
309                                 {
310                                         if(i->binding==j->binding)
311                                                 ++j;
312                                         ++i;
313                                 }
314                         }
315                 }
316
317                 for(vector<BoundUniformBlock>::const_iterator i=uniform_blocks.begin(); i!=uniform_blocks.end(); ++i)
318                         if(i->changed || mask==~0U)
319                         {
320                                 if(i->block)
321                                 {
322                                         if(i->binding>=0)
323                                         {
324                                                 const BufferBackedUniformBlock *block = static_cast<const BufferBackedUniformBlock *>(i->block);
325                                                 glBindBufferRange(GL_UNIFORM_BUFFER, i->binding, block->get_buffer()->get_id(), block->get_offset(), block->get_data_size());
326                                         }
327                                         else
328                                                 static_cast<const DefaultUniformBlock *>(i->block)->apply();
329                                 }
330                                 else
331                                         glBindBufferBase(GL_UNIFORM_BUFFER, i->binding, 0);
332
333                                 i->changed = false;
334                         }
335         }
336
337         if(mask&DEPTH_TEST)
338         {
339                 if(depth_test && depth_test->enabled)
340                 {
341                         glEnable(GL_DEPTH_TEST);
342                         glDepthFunc(get_gl_predicate(depth_test->compare));
343                 }
344                 else
345                         glDisable(GL_DEPTH_TEST);
346
347                 glDepthMask(!depth_test || depth_test->write);
348         }
349
350         if(mask&STENCIL_TEST)
351         {
352                 if(stencil_test && stencil_test->enabled)
353                 {
354                         glEnable(GL_STENCIL_TEST);
355                         glStencilFunc(get_gl_predicate(stencil_test->compare), stencil_test->reference, 0xFFFFFFFF);
356                         glStencilOp(get_gl_stencil_op(stencil_test->stencil_fail_op), get_gl_stencil_op(stencil_test->depth_fail_op), get_gl_stencil_op(stencil_test->depth_pass_op));
357                 }
358                 else
359                         glDisable(GL_STENCIL_TEST);
360         }
361
362         if(mask&BLEND)
363         {
364                 if(blend && blend->enabled)
365                 {
366                         glEnable(GL_BLEND);
367                         glBlendEquation(get_gl_blend_equation(blend->equation));
368                         glBlendFunc(get_gl_blend_factor(blend->src_factor), get_gl_blend_factor(blend->dst_factor));
369                         glBlendColor(blend->constant.r, blend->constant.g, blend->constant.b, blend->constant.a);
370                 }
371                 else
372                         glDisable(GL_BLEND);
373         }
374
375         last_applied = this;
376         changes &= ~mask;
377 }
378
379 void PipelineState::clear()
380 {
381         if(last_applied)
382         {
383                 glUseProgram(0);
384                 glBindVertexArray(0);
385
386                 unsigned max_clip_planes = Limits::get_global().max_clip_planes;
387                 for(unsigned i=0; i<max_clip_planes; ++i)
388                         if((last_applied->enabled_clip_planes>>i)&1)
389                                 glDisable(GL_CLIP_PLANE0+i);
390
391                 for(vector<BoundTexture>::const_iterator i=last_applied->textures.begin(); i!=last_applied->textures.end(); ++i)
392                         if(i->texture && i->sampler)
393                         {
394                                 if(ARB_direct_state_access)
395                                         glBindTextureUnit(i->binding, 0);
396                                 else
397                                 {
398                                         glActiveTexture(GL_TEXTURE0+i->binding);
399                                         glBindTexture(bound_tex_targets[i->binding], 0);
400                                         bound_tex_targets[i->binding] = 0;
401                                 }
402                         }
403
404                 for(vector<BoundUniformBlock>::const_iterator i=last_applied->uniform_blocks.begin(); i!=last_applied->uniform_blocks.end(); ++i)
405                         if(i->block)
406                                 glBindBufferBase(GL_UNIFORM_BUFFER, i->binding, 0);
407
408                 glDisable(GL_DEPTH_TEST);
409                 glDepthMask(true);
410                 glDisable(GL_STENCIL_TEST);
411                 glDisable(GL_BLEND);
412
413                 last_applied = 0;
414         }
415 }
416
417
418 PipelineState::BoundTexture::BoundTexture(unsigned b):
419         binding(b),
420         changed(false),
421         texture(0),
422         sampler(0)
423 { }
424
425
426 PipelineState::BoundUniformBlock::BoundUniformBlock(int b):
427         binding(b),
428         changed(false),
429         block(0)
430 { }
431
432 } // namespace GL
433 } // namespace Msp