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