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_uniform_block(int binding, const UniformBlock *block)
128 auto i = lower_bound_member(uniform_blocks, binding, &BoundUniformBlock::binding);
129 if(i==uniform_blocks.end() || i->binding!=binding)
130 i = uniform_blocks.insert(i, BoundUniformBlock(binding));
131 if(block!=i->block || binding<0)
139 void PipelineState::set_depth_test(const DepthTest *dt)
141 set(depth_test, dt, DEPTH_TEST);
144 void PipelineState::set_stencil_test(const StencilTest *st)
146 set(stencil_test, st, STENCIL_TEST);
149 void PipelineState::set_blend(const Blend *b)
151 set(blend, b, BLEND);
154 void PipelineState::apply() const
157 Texture::unbind_scratch();
159 apply(this==last_applied ? changes : ~0U);
162 void PipelineState::apply(unsigned mask) const
166 glBindFramebuffer(GL_FRAMEBUFFER, framebuffer ? framebuffer->id : 0);
169 framebuffer->refresh();
170 framebuffer->require_complete();
177 glViewport(viewport->left, viewport->bottom, viewport->width, viewport->height);
179 glViewport(0, 0, framebuffer->get_width(), framebuffer->get_height());
186 glEnable(GL_SCISSOR_TEST);
187 glScissor(scissor->left, scissor->bottom, scissor->width, scissor->height);
190 glDisable(GL_SCISSOR_TEST);
194 glUseProgram(shprog ? shprog->id : 0);
196 if(mask&VERTEX_SETUP)
198 glBindVertexArray(vertex_setup ? vertex_setup->id : 0);
201 static Require _req(MSP_primitive_restart);
203 vertex_setup->refresh();
204 unsigned ri = (vertex_setup->get_index_type()==UNSIGNED_INT ? 0xFFFFFFFF : 0xFFFF);
205 if(ri!=restart_index)
208 glEnable(GL_PRIMITIVE_RESTART);
209 glPrimitiveRestartIndex(ri);
217 glFrontFace(front_face==CLOCKWISE ? GL_CW : GL_CCW);
219 if(face_cull!=NO_CULL && front_face!=NON_MANIFOLD)
221 glEnable(GL_CULL_FACE);
222 glCullFace(face_cull==CULL_FRONT ? GL_FRONT : GL_BACK);
225 glDisable(GL_CULL_FACE);
230 unsigned max_clip_planes = Limits::get_global().max_clip_planes;
231 for(unsigned i=0; i<max_clip_planes; ++i)
233 if((enabled_clip_planes>>i)&1)
234 glEnable(GL_CLIP_PLANE0+i);
236 glDisable(GL_CLIP_PLANE0+i);
242 for(const BoundTexture &t: textures)
243 if(t.changed || mask==~0U)
245 if(t.texture && t.sampler)
247 if(ARB_direct_state_access)
248 glBindTextureUnit(t.binding, t.texture->id);
251 glActiveTexture(GL_TEXTURE0+t.binding);
252 if(bound_tex_targets[t.binding] && static_cast<int>(t.texture->target)!=bound_tex_targets[t.binding])
253 glBindTexture(bound_tex_targets[t.binding], 0);
254 glBindTexture(t.texture->target, t.texture->id);
257 bound_tex_targets[t.binding] = t.texture->target;
259 glBindSampler(t.binding, t.sampler->id);
260 t.sampler->refresh();
269 for(const BoundUniformBlock &u: uniform_blocks)
270 if(u.changed || mask==~0U)
276 glBindBufferRange(GL_UNIFORM_BUFFER, u.binding, u.block->get_buffer()->id, u.block->get_offset(), u.block->get_data_size());
277 bound_uniform_blocks[u.binding] = 1;
281 const char *data = static_cast<const char *>(u.block->get_data_pointer());
282 for(const Program::UniformCall &call: shprog->uniform_calls)
283 call.func(call.location, call.size, data+call.location*16);
293 if(depth_test && depth_test->enabled)
295 glEnable(GL_DEPTH_TEST);
296 glDepthFunc(get_gl_predicate(depth_test->compare));
299 glDisable(GL_DEPTH_TEST);
301 glDepthMask(!depth_test || depth_test->write);
304 if(mask&STENCIL_TEST)
306 if(stencil_test && stencil_test->enabled)
308 glEnable(GL_STENCIL_TEST);
309 glStencilFunc(get_gl_predicate(stencil_test->compare), stencil_test->reference, 0xFFFFFFFF);
310 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));
313 glDisable(GL_STENCIL_TEST);
318 if(blend && blend->enabled)
321 glBlendEquation(get_gl_blend_equation(blend->equation));
322 glBlendFunc(get_gl_blend_factor(blend->src_factor), get_gl_blend_factor(blend->dst_factor));
323 glBlendColor(blend->constant.r, blend->constant.g, blend->constant.b, blend->constant.a);
333 void PipelineState::clear()
338 glBindVertexArray(0);
340 unsigned max_clip_planes = Limits::get_global().max_clip_planes;
341 for(unsigned i=0; i<max_clip_planes; ++i)
342 if((last_applied->enabled_clip_planes>>i)&1)
343 glDisable(GL_CLIP_PLANE0+i);
345 for(unsigned i=0; i<bound_tex_targets.size(); ++i)
346 if(bound_tex_targets[i])
348 if(ARB_direct_state_access)
349 glBindTextureUnit(i, 0);
352 glActiveTexture(GL_TEXTURE0+i);
353 glBindTexture(bound_tex_targets[i], 0);
355 bound_tex_targets[i] = 0;
358 for(unsigned i=0; i<bound_uniform_blocks.size(); ++i)
359 if(bound_uniform_blocks[i])
361 glBindBufferBase(GL_UNIFORM_BUFFER, i, 0);
362 bound_uniform_blocks[i] = 0;
365 glDisable(GL_DEPTH_TEST);
367 glDisable(GL_STENCIL_TEST);
375 PipelineState::BoundTexture::BoundTexture(unsigned b):
383 PipelineState::BoundUniformBlock::BoundUniformBlock(int b):