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 vector<char> PipelineState::bound_uniform_blocks;
30 unsigned PipelineState::restart_index = 0;
32 PipelineState::PipelineState():
38 front_face(COUNTERCLOCKWISE),
40 enabled_clip_planes(0),
46 if(bound_tex_targets.empty())
47 bound_tex_targets.resize(Limits::get_global().max_texture_bindings);
48 if(bound_uniform_blocks.empty())
49 bound_uniform_blocks.resize(Limits::get_global().max_uniform_bindings);
52 PipelineState::~PipelineState()
54 if(this==last_applied)
59 void PipelineState::set(T &target, T value, unsigned flag)
68 void PipelineState::set_framebuffer(const Framebuffer *f)
70 set(framebuffer, f, FRAMEBUFFER|VIEWPORT);
73 void PipelineState::set_viewport(const Rect *v)
75 set(viewport, v, VIEWPORT);
78 void PipelineState::set_scissor(const Rect *s)
80 set(scissor, s, SCISSOR);
83 void PipelineState::set_shader_program(const Program *p)
85 set(shprog, p, SHPROG);
88 void PipelineState::set_vertex_setup(const VertexSetup *s)
90 set(vertex_setup, s, VERTEX_SETUP);
93 void PipelineState::set_front_face(FaceWinding w)
95 set(front_face, w, FACE_CULL);
98 void PipelineState::set_face_cull(CullMode c)
100 set(face_cull, c, FACE_CULL);
103 void PipelineState::set_enabled_clip_planes(unsigned p)
105 set(enabled_clip_planes, p, CLIP_PLANES);
108 void PipelineState::set_texture(unsigned binding, const Texture *tex, const Sampler *samp)
110 if((tex!=0)!=(samp!=0))
111 throw invalid_argument("PipelineState::set_texture");
113 vector<BoundTexture>::iterator i = lower_bound_member(textures, binding, &BoundTexture::binding);
114 if(i==textures.end() || i->binding!=binding)
115 i = textures.insert(i, BoundTexture(binding));
116 if(tex!=i->texture || samp!=i->sampler)
125 void PipelineState::set_uniforms(const DefaultUniformBlock *block)
127 set_uniform_block_(-1, block);
130 void PipelineState::set_uniform_block(unsigned binding, const BufferBackedUniformBlock *block)
132 set_uniform_block_(binding, block);
135 void PipelineState::set_uniform_block_(int binding, const UniformBlock *block)
137 vector<BoundUniformBlock>::iterator i = lower_bound_member(uniform_blocks, binding, &BoundUniformBlock::binding);
138 if(i==uniform_blocks.end() || i->binding!=binding)
139 i = uniform_blocks.insert(i, BoundUniformBlock(binding));
140 if(block!=i->block || binding<0)
148 void PipelineState::set_depth_test(const DepthTest *dt)
150 set(depth_test, dt, DEPTH_TEST);
153 void PipelineState::set_stencil_test(const StencilTest *st)
155 set(stencil_test, st, STENCIL_TEST);
158 void PipelineState::set_blend(const Blend *b)
160 set(blend, b, BLEND);
163 void PipelineState::apply() const
166 Texture::unbind_scratch();
168 apply(this==last_applied ? changes : ~0U);
171 void PipelineState::apply(unsigned mask) const
175 glBindFramebuffer(GL_FRAMEBUFFER, framebuffer ? framebuffer->get_id() : 0);
178 framebuffer->refresh();
179 framebuffer->require_complete();
186 glViewport(viewport->left, viewport->bottom, viewport->width, viewport->height);
188 glViewport(0, 0, framebuffer->get_width(), framebuffer->get_height());
195 glEnable(GL_SCISSOR_TEST);
196 glScissor(scissor->left, scissor->bottom, scissor->width, scissor->height);
199 glDisable(GL_SCISSOR_TEST);
203 glUseProgram(shprog ? shprog->get_id() : 0);
205 if(mask&VERTEX_SETUP)
207 glBindVertexArray(vertex_setup ? vertex_setup->get_id() : 0);
210 static Require _req(MSP_primitive_restart);
212 vertex_setup->refresh();
213 unsigned ri = (vertex_setup->get_index_type()==UNSIGNED_INT ? 0xFFFFFFFF : 0xFFFF);
214 if(ri!=restart_index)
217 glEnable(GL_PRIMITIVE_RESTART);
218 glPrimitiveRestartIndex(ri);
226 glFrontFace(front_face==CLOCKWISE ? GL_CW : GL_CCW);
228 if(face_cull!=NO_CULL && front_face!=NON_MANIFOLD)
230 glEnable(GL_CULL_FACE);
231 glCullFace(face_cull==CULL_FRONT ? GL_FRONT : GL_BACK);
234 glDisable(GL_CULL_FACE);
239 unsigned max_clip_planes = Limits::get_global().max_clip_planes;
240 for(unsigned i=0; i<max_clip_planes; ++i)
242 if((enabled_clip_planes>>i)&1)
243 glEnable(GL_CLIP_PLANE0+i);
245 glDisable(GL_CLIP_PLANE0+i);
251 for(vector<BoundTexture>::const_iterator i=textures.begin(); i!=textures.end(); ++i)
252 if(i->changed || mask==~0U)
254 if(i->texture && i->sampler)
256 if(ARB_direct_state_access)
257 glBindTextureUnit(i->binding, i->texture->get_id());
260 glActiveTexture(GL_TEXTURE0+i->binding);
261 if(bound_tex_targets[i->binding] && static_cast<int>(i->texture->get_target())!=bound_tex_targets[i->binding])
262 glBindTexture(bound_tex_targets[i->binding], 0);
263 glBindTexture(i->texture->get_target(), i->texture->get_id());
266 bound_tex_targets[i->binding] = i->texture->get_target();
268 glBindSampler(i->binding, i->sampler->get_id());
269 i->sampler->refresh();
278 for(vector<BoundUniformBlock>::const_iterator i=uniform_blocks.begin(); i!=uniform_blocks.end(); ++i)
279 if(i->changed || mask==~0U)
285 const BufferBackedUniformBlock *block = static_cast<const BufferBackedUniformBlock *>(i->block);
286 glBindBufferRange(GL_UNIFORM_BUFFER, i->binding, block->get_buffer()->get_id(), block->get_offset(), block->get_data_size());
287 bound_uniform_blocks[i->binding] = 1;
290 static_cast<const DefaultUniformBlock *>(i->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):