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>
11 #include "deviceinfo.h"
12 #include "depthtest.h"
13 #include "framebuffer.h"
14 #include "pipelinestate.h"
17 #include "stenciltest.h"
19 #include "uniformblock.h"
20 #include "vertexsetup.h"
27 const PipelineState *PipelineState::last_applied = 0;
28 vector<int> PipelineState::bound_tex_targets;
29 unsigned PipelineState::restart_index = 0;
31 PipelineState::PipelineState():
37 front_face(COUNTERCLOCKWISE),
39 enabled_clip_planes(0),
45 if(!ARB_direct_state_access && bound_tex_targets.empty())
46 bound_tex_targets.resize(Limits::get_global().max_texture_bindings);
49 PipelineState::~PipelineState()
51 if(this==last_applied)
56 void PipelineState::set(T &target, T value, unsigned flag)
65 void PipelineState::set_framebuffer(const Framebuffer *f)
67 set(framebuffer, f, FRAMEBUFFER|VIEWPORT);
70 void PipelineState::set_viewport(const Rect *v)
72 set(viewport, v, VIEWPORT);
75 void PipelineState::set_scissor(const Rect *s)
77 set(scissor, s, SCISSOR);
80 void PipelineState::set_shader_program(const Program *p)
82 set(shprog, p, SHPROG);
85 void PipelineState::set_vertex_setup(const VertexSetup *s)
87 set(vertex_setup, s, VERTEX_SETUP);
90 void PipelineState::set_front_face(FaceWinding w)
92 set(front_face, w, FACE_CULL);
95 void PipelineState::set_face_cull(CullMode c)
97 set(face_cull, c, FACE_CULL);
100 void PipelineState::set_enabled_clip_planes(unsigned p)
102 set(enabled_clip_planes, p, CLIP_PLANES);
105 void PipelineState::set_texture(unsigned binding, const Texture *tex, const Sampler *samp)
107 if((tex!=0)!=(samp!=0))
108 throw invalid_argument("PipelineState::set_texture");
110 vector<BoundTexture>::iterator i = lower_bound_member(textures, binding, &BoundTexture::binding);
111 if(i==textures.end() || i->binding!=binding)
112 i = textures.insert(i, BoundTexture(binding));
113 if(tex!=i->texture || samp!=i->sampler)
122 void PipelineState::set_uniforms(const DefaultUniformBlock *block)
124 set_uniform_block_(-1, block);
127 void PipelineState::set_uniform_block(unsigned binding, const BufferBackedUniformBlock *block)
129 set_uniform_block_(binding, block);
132 void PipelineState::set_uniform_block_(int binding, const UniformBlock *block)
134 vector<BoundUniformBlock>::iterator i = lower_bound_member(uniform_blocks, binding, &BoundUniformBlock::binding);
135 if(i==uniform_blocks.end() || i->binding!=binding)
136 i = uniform_blocks.insert(i, BoundUniformBlock(binding));
137 if(block!=i->block || binding<0)
145 void PipelineState::set_depth_test(const DepthTest *dt)
147 set(depth_test, dt, DEPTH_TEST);
150 void PipelineState::set_stencil_test(const StencilTest *st)
152 set(stencil_test, st, STENCIL_TEST);
155 void PipelineState::set_blend(const Blend *b)
157 set(blend, b, BLEND);
160 void PipelineState::apply() const
162 apply(this==last_applied ? changes : ~0U);
165 void PipelineState::apply(unsigned mask) const
169 glBindFramebuffer(GL_FRAMEBUFFER, framebuffer ? framebuffer->get_id() : 0);
172 framebuffer->refresh();
173 framebuffer->require_complete();
180 glViewport(viewport->left, viewport->bottom, viewport->width, viewport->height);
182 glViewport(0, 0, framebuffer->get_width(), framebuffer->get_height());
189 glEnable(GL_SCISSOR_TEST);
190 glScissor(scissor->left, scissor->bottom, scissor->width, scissor->height);
193 glDisable(GL_SCISSOR_TEST);
197 glUseProgram(shprog ? shprog->get_id() : 0);
199 if(mask&VERTEX_SETUP)
201 glBindVertexArray(vertex_setup ? vertex_setup->get_id() : 0);
204 static Require _req(MSP_primitive_restart);
206 vertex_setup->refresh();
207 unsigned ri = (vertex_setup->get_index_type()==UNSIGNED_INT ? 0xFFFFFFFF : 0xFFFF);
208 if(ri!=restart_index)
211 glEnable(GL_PRIMITIVE_RESTART);
212 glPrimitiveRestartIndex(ri);
220 glFrontFace(front_face==CLOCKWISE ? GL_CW : GL_CCW);
222 if(face_cull!=NO_CULL && front_face!=NON_MANIFOLD)
224 glEnable(GL_CULL_FACE);
225 glCullFace(face_cull==CULL_FRONT ? GL_FRONT : GL_BACK);
228 glDisable(GL_CULL_FACE);
233 unsigned max_clip_planes = Limits::get_global().max_clip_planes;
234 for(unsigned i=0; i<max_clip_planes; ++i)
236 if((enabled_clip_planes>>i)&1)
237 glEnable(GL_CLIP_PLANE0+i);
239 glDisable(GL_CLIP_PLANE0+i);
245 if(last_applied && this!=last_applied)
247 vector<BoundTexture>::const_iterator i = textures.begin();
248 vector<BoundTexture>::const_iterator j = last_applied->textures.begin();
249 while(j!=last_applied->textures.end())
251 if(i==textures.end() || j->binding<i->binding)
253 if(bound_tex_targets[j->binding])
255 if(ARB_direct_state_access)
256 glBindTextureUnit(j->binding, 0);
259 glActiveTexture(GL_TEXTURE0+j->binding);
260 glBindTexture(bound_tex_targets[j->binding], 0);
267 if(i->binding==j->binding)
274 for(vector<BoundTexture>::const_iterator i=textures.begin(); i!=textures.end(); ++i)
275 if(i->changed || mask==~0U)
277 if(i->texture && i->sampler)
279 if(ARB_direct_state_access)
280 glBindTextureUnit(i->binding, i->texture->get_id());
283 glActiveTexture(GL_TEXTURE0+i->binding);
284 if(bound_tex_targets[i->binding] && static_cast<int>(i->texture->get_target())!=bound_tex_targets[i->binding])
285 glBindTexture(bound_tex_targets[i->binding], 0);
286 glBindTexture(i->texture->get_target(), i->texture->get_id());
287 bound_tex_targets[i->binding] = i->texture->get_target();
290 glBindSampler(i->binding, i->sampler->get_id());
291 i->sampler->refresh();
293 else if(bound_tex_targets[i->binding])
295 if(ARB_direct_state_access)
296 glBindTextureUnit(i->binding, 0);
299 glActiveTexture(GL_TEXTURE0+i->binding);
300 glBindTexture(bound_tex_targets[i->binding], 0);
301 bound_tex_targets[i->binding] = 0;
311 if(last_applied && this!=last_applied)
313 vector<BoundUniformBlock>::const_iterator i = uniform_blocks.begin();
314 vector<BoundUniformBlock>::const_iterator j = last_applied->uniform_blocks.begin();
315 while(j!=last_applied->uniform_blocks.end())
317 if(i==uniform_blocks.end() || j->binding<i->binding)
319 glBindBufferBase(GL_UNIFORM_BUFFER, j->binding, 0);
324 if(i->binding==j->binding)
331 for(vector<BoundUniformBlock>::const_iterator i=uniform_blocks.begin(); i!=uniform_blocks.end(); ++i)
332 if(i->changed || mask==~0U)
338 const BufferBackedUniformBlock *block = static_cast<const BufferBackedUniformBlock *>(i->block);
339 glBindBufferRange(GL_UNIFORM_BUFFER, i->binding, block->get_buffer()->get_id(), block->get_offset(), block->get_data_size());
342 static_cast<const DefaultUniformBlock *>(i->block)->apply();
345 glBindBufferBase(GL_UNIFORM_BUFFER, i->binding, 0);
353 if(depth_test && depth_test->enabled)
355 glEnable(GL_DEPTH_TEST);
356 glDepthFunc(get_gl_predicate(depth_test->compare));
359 glDisable(GL_DEPTH_TEST);
361 glDepthMask(!depth_test || depth_test->write);
364 if(mask&STENCIL_TEST)
366 if(stencil_test && stencil_test->enabled)
368 glEnable(GL_STENCIL_TEST);
369 glStencilFunc(get_gl_predicate(stencil_test->compare), stencil_test->reference, 0xFFFFFFFF);
370 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));
373 glDisable(GL_STENCIL_TEST);
378 if(blend && blend->enabled)
381 glBlendEquation(get_gl_blend_equation(blend->equation));
382 glBlendFunc(get_gl_blend_factor(blend->src_factor), get_gl_blend_factor(blend->dst_factor));
383 glBlendColor(blend->constant.r, blend->constant.g, blend->constant.b, blend->constant.a);
393 void PipelineState::clear()
398 glBindVertexArray(0);
400 unsigned max_clip_planes = Limits::get_global().max_clip_planes;
401 for(unsigned i=0; i<max_clip_planes; ++i)
402 if((last_applied->enabled_clip_planes>>i)&1)
403 glDisable(GL_CLIP_PLANE0+i);
405 for(vector<BoundTexture>::const_iterator i=last_applied->textures.begin(); i!=last_applied->textures.end(); ++i)
406 if(i->texture && i->sampler)
408 if(ARB_direct_state_access)
409 glBindTextureUnit(i->binding, 0);
412 glActiveTexture(GL_TEXTURE0+i->binding);
413 glBindTexture(bound_tex_targets[i->binding], 0);
414 bound_tex_targets[i->binding] = 0;
418 for(vector<BoundUniformBlock>::const_iterator i=last_applied->uniform_blocks.begin(); i!=last_applied->uniform_blocks.end(); ++i)
420 glBindBufferBase(GL_UNIFORM_BUFFER, i->binding, 0);
422 glDisable(GL_DEPTH_TEST);
424 glDisable(GL_STENCIL_TEST);
432 PipelineState::BoundTexture::BoundTexture(unsigned b):
440 PipelineState::BoundUniformBlock::BoundUniformBlock(int b):