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/ext_framebuffer_object.h>
9 #include <msp/gl/extensions/msp_primitive_restart.h>
12 #include "deviceinfo.h"
13 #include "depthtest.h"
14 #include "framebuffer.h"
15 #include "pipelinestate.h"
19 #include "stenciltest.h"
21 #include "uniformblock.h"
22 #include "vertexsetup.h"
29 const PipelineState *PipelineState::last_applied = 0;
30 vector<int> PipelineState::bound_tex_targets;
31 vector<char> PipelineState::bound_uniform_blocks;
32 unsigned PipelineState::restart_index = 0;
34 PipelineState::PipelineState():
40 front_face(COUNTERCLOCKWISE),
42 enabled_clip_planes(0),
48 if(bound_tex_targets.empty())
49 bound_tex_targets.resize(DeviceInfo::get_global().limits.max_texture_bindings);
50 if(bound_uniform_blocks.empty())
51 bound_uniform_blocks.resize(DeviceInfo::get_global().limits.max_uniform_bindings);
54 PipelineState::~PipelineState()
56 if(this==last_applied)
61 void PipelineState::set(T &target, T value, unsigned flag)
70 void PipelineState::set_framebuffer(const Framebuffer *f)
72 set(framebuffer, f, FRAMEBUFFER|VIEWPORT);
75 void PipelineState::set_viewport(const Rect *v)
77 set(viewport, v, VIEWPORT);
80 void PipelineState::set_scissor(const Rect *s)
82 set(scissor, s, SCISSOR);
85 void PipelineState::set_shader_program(const Program *p)
87 set(shprog, p, SHPROG);
90 void PipelineState::set_vertex_setup(const VertexSetup *s)
92 set(vertex_setup, s, VERTEX_SETUP);
95 void PipelineState::set_front_face(FaceWinding w)
97 set(front_face, w, FACE_CULL);
100 void PipelineState::set_face_cull(CullMode c)
102 set(face_cull, c, FACE_CULL);
105 void PipelineState::set_enabled_clip_planes(unsigned p)
107 set(enabled_clip_planes, p, CLIP_PLANES);
110 void PipelineState::set_texture(unsigned binding, const Texture *tex, const Sampler *samp)
112 if((tex!=0)!=(samp!=0))
113 throw invalid_argument("PipelineState::set_texture");
115 auto i = lower_bound_member(textures, binding, &BoundTexture::binding);
116 if(i==textures.end() || i->binding!=binding)
117 i = textures.insert(i, BoundTexture(binding));
118 if(tex!=i->texture || samp!=i->sampler)
127 void PipelineState::set_uniform_block(int binding, const UniformBlock *block)
129 auto i = lower_bound_member(uniform_blocks, binding, &BoundUniformBlock::binding);
130 if(i==uniform_blocks.end() || i->binding!=binding)
131 i = uniform_blocks.insert(i, BoundUniformBlock(binding));
132 if(block!=i->block || binding<0)
140 void PipelineState::set_depth_test(const DepthTest *dt)
142 set(depth_test, dt, DEPTH_TEST);
145 void PipelineState::set_stencil_test(const StencilTest *st)
147 set(stencil_test, st, STENCIL_TEST);
150 void PipelineState::set_blend(const Blend *b)
152 set(blend, b, BLEND);
155 void PipelineState::apply() const
158 Texture::unbind_scratch();
160 apply(this==last_applied ? changes : ~0U);
163 void PipelineState::apply(unsigned mask) const
167 glBindFramebuffer(GL_FRAMEBUFFER, framebuffer ? framebuffer->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->id : 0);
197 if(mask&VERTEX_SETUP)
199 glBindVertexArray(vertex_setup ? vertex_setup->id : 0);
202 static Require _req(MSP_primitive_restart);
204 vertex_setup->refresh();
205 unsigned ri = (vertex_setup->get_index_type()==UNSIGNED_INT ? 0xFFFFFFFF : 0xFFFF);
206 if(ri!=restart_index)
209 glEnable(GL_PRIMITIVE_RESTART);
210 glPrimitiveRestartIndex(ri);
218 glFrontFace(front_face==CLOCKWISE ? GL_CW : GL_CCW);
220 if(face_cull!=NO_CULL && front_face!=NON_MANIFOLD)
222 glEnable(GL_CULL_FACE);
223 glCullFace(face_cull==CULL_FRONT ? GL_FRONT : GL_BACK);
226 glDisable(GL_CULL_FACE);
231 unsigned max_clip_planes = DeviceInfo::get_global().limits.max_clip_planes;
232 for(unsigned i=0; i<max_clip_planes; ++i)
234 if((enabled_clip_planes>>i)&1)
235 glEnable(GL_CLIP_PLANE0+i);
237 glDisable(GL_CLIP_PLANE0+i);
243 for(const BoundTexture &t: textures)
244 if(t.changed || mask==~0U)
246 if(t.texture && t.sampler)
248 if(ARB_direct_state_access)
249 glBindTextureUnit(t.binding, t.texture->id);
252 glActiveTexture(GL_TEXTURE0+t.binding);
253 if(bound_tex_targets[t.binding] && static_cast<int>(t.texture->target)!=bound_tex_targets[t.binding])
254 glBindTexture(bound_tex_targets[t.binding], 0);
255 glBindTexture(t.texture->target, t.texture->id);
258 bound_tex_targets[t.binding] = t.texture->target;
260 glBindSampler(t.binding, t.sampler->id);
261 t.sampler->refresh();
270 for(const BoundUniformBlock &u: uniform_blocks)
271 if(u.changed || mask==~0U)
277 glBindBufferRange(GL_UNIFORM_BUFFER, u.binding, u.block->get_buffer()->id, u.block->get_offset(), u.block->get_data_size());
278 bound_uniform_blocks[u.binding] = 1;
282 const char *data = static_cast<const char *>(u.block->get_data_pointer());
283 for(const Program::UniformCall &call: shprog->uniform_calls)
284 call.func(call.location, call.size, data+call.location*16);
294 if(depth_test && depth_test->enabled)
296 glEnable(GL_DEPTH_TEST);
297 glDepthFunc(get_gl_predicate(depth_test->compare));
300 glDisable(GL_DEPTH_TEST);
302 glDepthMask(!depth_test || depth_test->write);
305 if(mask&STENCIL_TEST)
307 if(stencil_test && stencil_test->enabled)
309 glEnable(GL_STENCIL_TEST);
310 glStencilFunc(get_gl_predicate(stencil_test->compare), stencil_test->reference, 0xFFFFFFFF);
311 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 glDisable(GL_STENCIL_TEST);
319 if(blend && blend->enabled)
322 glBlendEquation(get_gl_blend_equation(blend->equation));
323 glBlendFunc(get_gl_blend_factor(blend->src_factor), get_gl_blend_factor(blend->dst_factor));
324 glBlendColor(blend->constant.r, blend->constant.g, blend->constant.b, blend->constant.a);
334 void PipelineState::clear()
339 glBindVertexArray(0);
341 unsigned max_clip_planes = DeviceInfo::get_global().limits.max_clip_planes;
342 for(unsigned i=0; i<max_clip_planes; ++i)
343 if((last_applied->enabled_clip_planes>>i)&1)
344 glDisable(GL_CLIP_PLANE0+i);
346 for(unsigned i=0; i<bound_tex_targets.size(); ++i)
347 if(bound_tex_targets[i])
349 if(ARB_direct_state_access)
350 glBindTextureUnit(i, 0);
353 glActiveTexture(GL_TEXTURE0+i);
354 glBindTexture(bound_tex_targets[i], 0);
356 bound_tex_targets[i] = 0;
359 for(unsigned i=0; i<bound_uniform_blocks.size(); ++i)
360 if(bound_uniform_blocks[i])
362 glBindBufferBase(GL_UNIFORM_BUFFER, i, 0);
363 bound_uniform_blocks[i] = 0;
366 glDisable(GL_DEPTH_TEST);
368 glDisable(GL_STENCIL_TEST);
376 PipelineState::BoundTexture::BoundTexture(unsigned b):
384 PipelineState::BoundUniformBlock::BoundUniformBlock(int b):