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>
10 #include "deviceinfo.h"
11 #include "depthtest.h"
12 #include "framebuffer.h"
13 #include "pipelinestate.h"
16 #include "stenciltest.h"
18 #include "uniformblock.h"
19 #include "vertexsetup.h"
26 const PipelineState *PipelineState::last_applied = 0;
27 vector<int> PipelineState::bound_tex_targets;
29 PipelineState::PipelineState():
35 front_face(COUNTERCLOCKWISE),
37 enabled_clip_planes(0),
43 if(!ARB_direct_state_access && bound_tex_targets.empty())
44 bound_tex_targets.resize(Limits::get_global().max_texture_bindings);
47 PipelineState::~PipelineState()
49 if(this==last_applied)
54 void PipelineState::set(T &target, T value, unsigned flag)
63 void PipelineState::set_framebuffer(const Framebuffer *f)
65 set(framebuffer, f, FRAMEBUFFER|VIEWPORT);
68 void PipelineState::set_viewport(const Rect *v)
70 set(viewport, v, VIEWPORT);
73 void PipelineState::set_scissor(const Rect *s)
75 set(scissor, s, SCISSOR);
78 void PipelineState::set_shader_program(const Program *p)
80 set(shprog, p, SHPROG);
83 void PipelineState::set_vertex_setup(const VertexSetup *s)
85 set(vertex_setup, s, VERTEX_SETUP);
88 void PipelineState::set_front_face(FaceWinding w)
90 set(front_face, w, FACE_CULL);
93 void PipelineState::set_face_cull(CullMode c)
95 set(face_cull, c, FACE_CULL);
98 void PipelineState::set_enabled_clip_planes(unsigned p)
100 set(enabled_clip_planes, p, CLIP_PLANES);
103 void PipelineState::set_texture(unsigned binding, const Texture *tex, const Sampler *samp)
105 if((tex!=0)!=(samp!=0))
106 throw invalid_argument("PipelineState::set_texture");
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)
120 void PipelineState::set_uniforms(const DefaultUniformBlock *block)
122 set_uniform_block_(-1, block);
125 void PipelineState::set_uniform_block(unsigned binding, const BufferBackedUniformBlock *block)
127 set_uniform_block_(binding, block);
130 void PipelineState::set_uniform_block_(int binding, const UniformBlock *block)
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)
143 void PipelineState::set_depth_test(const DepthTest *dt)
145 set(depth_test, dt, DEPTH_TEST);
148 void PipelineState::set_stencil_test(const StencilTest *st)
150 set(stencil_test, st, STENCIL_TEST);
153 void PipelineState::set_blend(const Blend *b)
155 set(blend, b, BLEND);
158 void PipelineState::apply() const
160 apply(this==last_applied ? changes : ~0U);
163 void PipelineState::apply(unsigned mask) const
167 glBindFramebuffer(GL_FRAMEBUFFER, framebuffer ? framebuffer->get_id() : 0);
170 framebuffer->refresh();
171 framebuffer->require_complete();
178 glViewport(viewport->left, viewport->bottom, viewport->width, viewport->height);
180 glViewport(0, 0, framebuffer->get_width(), framebuffer->get_height());
187 glEnable(GL_SCISSOR_TEST);
188 glScissor(scissor->left, scissor->bottom, scissor->width, scissor->height);
191 glDisable(GL_SCISSOR_TEST);
195 glUseProgram(shprog ? shprog->get_id() : 0);
197 if(mask&VERTEX_SETUP)
199 glBindVertexArray(vertex_setup ? vertex_setup->get_id() : 0);
201 vertex_setup->refresh();
206 glFrontFace(front_face==CLOCKWISE ? GL_CW : GL_CCW);
208 if(face_cull!=NO_CULL && front_face!=NON_MANIFOLD)
210 glEnable(GL_CULL_FACE);
211 glCullFace(face_cull==CULL_FRONT ? GL_FRONT : GL_BACK);
214 glDisable(GL_CULL_FACE);
219 unsigned max_clip_planes = Limits::get_global().max_clip_planes;
220 for(unsigned i=0; i<max_clip_planes; ++i)
222 if((enabled_clip_planes>>i)&1)
223 glEnable(GL_CLIP_PLANE0+i);
225 glDisable(GL_CLIP_PLANE0+i);
231 if(last_applied && this!=last_applied)
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())
237 if(i==textures.end() || j->binding<i->binding)
239 if(bound_tex_targets[j->binding])
241 if(ARB_direct_state_access)
242 glBindTextureUnit(j->binding, 0);
245 glActiveTexture(GL_TEXTURE0+j->binding);
246 glBindTexture(bound_tex_targets[j->binding], 0);
253 if(i->binding==j->binding)
260 for(vector<BoundTexture>::const_iterator i=textures.begin(); i!=textures.end(); ++i)
261 if(i->changed || mask==~0U)
263 if(i->texture && i->sampler)
265 if(ARB_direct_state_access)
266 glBindTextureUnit(i->binding, i->texture->get_id());
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();
276 glBindSampler(i->binding, i->sampler->get_id());
277 i->sampler->refresh();
279 else if(bound_tex_targets[i->binding])
281 if(ARB_direct_state_access)
282 glBindTextureUnit(i->binding, 0);
285 glActiveTexture(GL_TEXTURE0+i->binding);
286 glBindTexture(bound_tex_targets[i->binding], 0);
287 bound_tex_targets[i->binding] = 0;
297 if(last_applied && this!=last_applied)
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())
303 if(i==uniform_blocks.end() || j->binding<i->binding)
305 glBindBufferBase(GL_UNIFORM_BUFFER, j->binding, 0);
310 if(i->binding==j->binding)
317 for(vector<BoundUniformBlock>::const_iterator i=uniform_blocks.begin(); i!=uniform_blocks.end(); ++i)
318 if(i->changed || mask==~0U)
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());
328 static_cast<const DefaultUniformBlock *>(i->block)->apply();
331 glBindBufferBase(GL_UNIFORM_BUFFER, i->binding, 0);
339 if(depth_test && depth_test->enabled)
341 glEnable(GL_DEPTH_TEST);
342 glDepthFunc(get_gl_predicate(depth_test->compare));
345 glDisable(GL_DEPTH_TEST);
347 glDepthMask(!depth_test || depth_test->write);
350 if(mask&STENCIL_TEST)
352 if(stencil_test && stencil_test->enabled)
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));
359 glDisable(GL_STENCIL_TEST);
364 if(blend && blend->enabled)
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);
379 void PipelineState::clear()
384 glBindVertexArray(0);
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);
391 for(vector<BoundTexture>::const_iterator i=last_applied->textures.begin(); i!=last_applied->textures.end(); ++i)
392 if(i->texture && i->sampler)
394 if(ARB_direct_state_access)
395 glBindTextureUnit(i->binding, 0);
398 glActiveTexture(GL_TEXTURE0+i->binding);
399 glBindTexture(bound_tex_targets[i->binding], 0);
400 bound_tex_targets[i->binding] = 0;
404 for(vector<BoundUniformBlock>::const_iterator i=last_applied->uniform_blocks.begin(); i!=last_applied->uniform_blocks.end(); ++i)
406 glBindBufferBase(GL_UNIFORM_BUFFER, i->binding, 0);
408 glDisable(GL_DEPTH_TEST);
410 glDisable(GL_STENCIL_TEST);
418 PipelineState::BoundTexture::BoundTexture(unsigned b):
426 PipelineState::BoundUniformBlock::BoundUniformBlock(int b):