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"
18 #include "stenciltest.h"
20 #include "uniformblock.h"
21 #include "vertexsetup.h"
28 const PipelineState *PipelineState::last_applied = 0;
29 vector<int> PipelineState::bound_tex_targets;
30 vector<char> PipelineState::bound_uniform_blocks;
31 unsigned PipelineState::restart_index = 0;
33 PipelineState::PipelineState():
39 front_face(COUNTERCLOCKWISE),
41 enabled_clip_planes(0),
47 if(bound_tex_targets.empty())
48 bound_tex_targets.resize(Limits::get_global().max_texture_bindings);
49 if(bound_uniform_blocks.empty())
50 bound_uniform_blocks.resize(Limits::get_global().max_uniform_bindings);
53 PipelineState::~PipelineState()
55 if(this==last_applied)
60 void PipelineState::set(T &target, T value, unsigned flag)
69 void PipelineState::set_framebuffer(const Framebuffer *f)
71 set(framebuffer, f, FRAMEBUFFER|VIEWPORT);
74 void PipelineState::set_viewport(const Rect *v)
76 set(viewport, v, VIEWPORT);
79 void PipelineState::set_scissor(const Rect *s)
81 set(scissor, s, SCISSOR);
84 void PipelineState::set_shader_program(const Program *p)
86 set(shprog, p, SHPROG);
89 void PipelineState::set_vertex_setup(const VertexSetup *s)
91 set(vertex_setup, s, VERTEX_SETUP);
94 void PipelineState::set_front_face(FaceWinding w)
96 set(front_face, w, FACE_CULL);
99 void PipelineState::set_face_cull(CullMode c)
101 set(face_cull, c, FACE_CULL);
104 void PipelineState::set_enabled_clip_planes(unsigned p)
106 set(enabled_clip_planes, p, CLIP_PLANES);
109 void PipelineState::set_texture(unsigned binding, const Texture *tex, const Sampler *samp)
111 if((tex!=0)!=(samp!=0))
112 throw invalid_argument("PipelineState::set_texture");
114 auto i = lower_bound_member(textures, binding, &BoundTexture::binding);
115 if(i==textures.end() || i->binding!=binding)
116 i = textures.insert(i, BoundTexture(binding));
117 if(tex!=i->texture || samp!=i->sampler)
126 void PipelineState::set_uniforms(const DefaultUniformBlock *block)
128 set_uniform_block_(-1, block);
131 void PipelineState::set_uniform_block(unsigned binding, const BufferBackedUniformBlock *block)
133 set_uniform_block_(binding, block);
136 void PipelineState::set_uniform_block_(int binding, const UniformBlock *block)
138 auto i = lower_bound_member(uniform_blocks, binding, &BoundUniformBlock::binding);
139 if(i==uniform_blocks.end() || i->binding!=binding)
140 i = uniform_blocks.insert(i, BoundUniformBlock(binding));
141 if(block!=i->block || binding<0)
149 void PipelineState::set_depth_test(const DepthTest *dt)
151 set(depth_test, dt, DEPTH_TEST);
154 void PipelineState::set_stencil_test(const StencilTest *st)
156 set(stencil_test, st, STENCIL_TEST);
159 void PipelineState::set_blend(const Blend *b)
161 set(blend, b, BLEND);
164 void PipelineState::apply() const
167 Texture::unbind_scratch();
169 apply(this==last_applied ? changes : ~0U);
172 void PipelineState::apply(unsigned mask) const
176 glBindFramebuffer(GL_FRAMEBUFFER, framebuffer ? framebuffer->id : 0);
179 framebuffer->refresh();
180 framebuffer->require_complete();
187 glViewport(viewport->left, viewport->bottom, viewport->width, viewport->height);
189 glViewport(0, 0, framebuffer->get_width(), framebuffer->get_height());
196 glEnable(GL_SCISSOR_TEST);
197 glScissor(scissor->left, scissor->bottom, scissor->width, scissor->height);
200 glDisable(GL_SCISSOR_TEST);
204 glUseProgram(shprog ? shprog->id : 0);
206 if(mask&VERTEX_SETUP)
208 glBindVertexArray(vertex_setup ? vertex_setup->id : 0);
211 static Require _req(MSP_primitive_restart);
213 vertex_setup->refresh();
214 unsigned ri = (vertex_setup->get_index_type()==UNSIGNED_INT ? 0xFFFFFFFF : 0xFFFF);
215 if(ri!=restart_index)
218 glEnable(GL_PRIMITIVE_RESTART);
219 glPrimitiveRestartIndex(ri);
227 glFrontFace(front_face==CLOCKWISE ? GL_CW : GL_CCW);
229 if(face_cull!=NO_CULL && front_face!=NON_MANIFOLD)
231 glEnable(GL_CULL_FACE);
232 glCullFace(face_cull==CULL_FRONT ? GL_FRONT : GL_BACK);
235 glDisable(GL_CULL_FACE);
240 unsigned max_clip_planes = Limits::get_global().max_clip_planes;
241 for(unsigned i=0; i<max_clip_planes; ++i)
243 if((enabled_clip_planes>>i)&1)
244 glEnable(GL_CLIP_PLANE0+i);
246 glDisable(GL_CLIP_PLANE0+i);
252 for(const BoundTexture &t: textures)
253 if(t.changed || mask==~0U)
255 if(t.texture && t.sampler)
257 if(ARB_direct_state_access)
258 glBindTextureUnit(t.binding, t.texture->id);
261 glActiveTexture(GL_TEXTURE0+t.binding);
262 if(bound_tex_targets[t.binding] && static_cast<int>(t.texture->target)!=bound_tex_targets[t.binding])
263 glBindTexture(bound_tex_targets[t.binding], 0);
264 glBindTexture(t.texture->target, t.texture->id);
267 bound_tex_targets[t.binding] = t.texture->target;
269 glBindSampler(t.binding, t.sampler->id);
278 for(const BoundUniformBlock &u: uniform_blocks)
279 if(u.changed || mask==~0U)
285 const BufferBackedUniformBlock *block = static_cast<const BufferBackedUniformBlock *>(u.block);
286 glBindBufferRange(GL_UNIFORM_BUFFER, u.binding, block->get_buffer()->id, block->get_offset(), block->get_data_size());
287 bound_uniform_blocks[u.binding] = 1;
290 static_cast<const DefaultUniformBlock *>(u.block)->apply();
299 if(depth_test && depth_test->enabled)
301 glEnable(GL_DEPTH_TEST);
302 glDepthFunc(get_gl_predicate(depth_test->compare));
305 glDisable(GL_DEPTH_TEST);
307 glDepthMask(!depth_test || depth_test->write);
310 if(mask&STENCIL_TEST)
312 if(stencil_test && stencil_test->enabled)
314 glEnable(GL_STENCIL_TEST);
315 glStencilFunc(get_gl_predicate(stencil_test->compare), stencil_test->reference, 0xFFFFFFFF);
316 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));
319 glDisable(GL_STENCIL_TEST);
324 if(blend && blend->enabled)
327 glBlendEquation(get_gl_blend_equation(blend->equation));
328 glBlendFunc(get_gl_blend_factor(blend->src_factor), get_gl_blend_factor(blend->dst_factor));
329 glBlendColor(blend->constant.r, blend->constant.g, blend->constant.b, blend->constant.a);
339 void PipelineState::clear()
344 glBindVertexArray(0);
346 unsigned max_clip_planes = Limits::get_global().max_clip_planes;
347 for(unsigned i=0; i<max_clip_planes; ++i)
348 if((last_applied->enabled_clip_planes>>i)&1)
349 glDisable(GL_CLIP_PLANE0+i);
351 for(unsigned i=0; i<bound_tex_targets.size(); ++i)
352 if(bound_tex_targets[i])
354 if(ARB_direct_state_access)
355 glBindTextureUnit(i, 0);
358 glActiveTexture(GL_TEXTURE0+i);
359 glBindTexture(bound_tex_targets[i], 0);
361 bound_tex_targets[i] = 0;
364 for(unsigned i=0; i<bound_uniform_blocks.size(); ++i)
365 if(bound_uniform_blocks[i])
367 glBindBufferBase(GL_UNIFORM_BUFFER, i, 0);
368 bound_uniform_blocks[i] = 0;
371 glDisable(GL_DEPTH_TEST);
373 glDisable(GL_STENCIL_TEST);
381 PipelineState::BoundTexture::BoundTexture(unsigned b):
389 PipelineState::BoundUniformBlock::BoundUniformBlock(int b):