1 #include <msp/gl/extensions/arb_direct_state_access.h>
2 #include <msp/gl/extensions/arb_sampler_objects.h>
3 #include <msp/gl/extensions/arb_shader_objects.h>
4 #include <msp/gl/extensions/arb_uniform_buffer_object.h>
5 #include <msp/gl/extensions/arb_vertex_array_object.h>
6 #include <msp/gl/extensions/ext_framebuffer_object.h>
7 #include <msp/gl/extensions/msp_primitive_restart.h>
10 #include "depthtest.h"
12 #include "framebuffer.h"
14 #include "pipelinestate.h"
15 #include "pipelinestate_backend.h"
19 #include "stenciltest.h"
21 #include "uniformblock.h"
22 #include "vertexsetup.h"
29 OpenGLPipelineState::~OpenGLPipelineState()
32 applied_to->get_state().last_pipeline = 0;
35 void OpenGLPipelineState::apply() const
37 const PipelineState &self = *static_cast<const PipelineState *>(this);
38 Device &device = Device::get_current();
40 if(applied_to && applied_to!=&device)
42 applied_to->get_state().last_pipeline = 0;
46 OpenGLDeviceState &dev_state = device.get_state();
47 if(!dev_state.last_pipeline)
48 OpenGLTexture::unbind_scratch();
50 if(this!=dev_state.last_pipeline)
52 if(dev_state.last_pipeline)
53 dev_state.last_pipeline->applied_to = 0;
57 if(changes&PipelineState::FRAMEBUFFER)
59 const Framebuffer *framebuffer = self.framebuffer;
60 glBindFramebuffer(GL_FRAMEBUFFER, framebuffer ? framebuffer->id : 0);
63 framebuffer->refresh();
64 framebuffer->require_complete();
68 if(changes&(PipelineState::VIEWPORT|PipelineState::SCISSOR|PipelineState::FRAMEBUFFER))
69 if(const Framebuffer *framebuffer = self.framebuffer)
71 Rect fb_rect = framebuffer->get_rect();
72 if(changes&(PipelineState::VIEWPORT|PipelineState::FRAMEBUFFER))
74 Rect viewport = fb_rect.intersect(self.viewport);
75 glViewport(viewport.left, viewport.bottom, viewport.width, viewport.height);
77 if(changes&(PipelineState::SCISSOR|PipelineState::FRAMEBUFFER))
79 Rect scissor = fb_rect.intersect(self.scissor);
82 glEnable(GL_SCISSOR_TEST);
83 glScissor(scissor.left, scissor.bottom, scissor.width, scissor.height);
86 glDisable(GL_SCISSOR_TEST);
90 if(changes&PipelineState::SHPROG)
92 glUseProgram(self.shprog ? self.shprog->id : 0);
94 unsigned ncd = (self.shprog ? self.shprog->get_n_clip_distances() : 0);
95 if(ncd!=dev_state.n_clip_distances)
97 for(unsigned i=0; (i<ncd || i<dev_state.n_clip_distances); ++i)
100 glEnable(GL_CLIP_PLANE0+i);
102 glDisable(GL_CLIP_PLANE0+i);
104 dev_state.n_clip_distances = ncd;
108 if(changes&PipelineState::UNIFORMS)
110 for(const PipelineState::BoundUniformBlock &u: self.uniform_blocks)
111 if(u.changed || changes==~0U)
117 glBindBufferRange(GL_UNIFORM_BUFFER, u.binding, u.buffer->id, u.block->get_offset(), u.block->get_data_size());
118 dev_state.bound_uniform_blocks[u.binding] = 1;
120 else if(u.binding==ReflectData::DEFAULT_BLOCK && self.shprog)
122 const char *data = static_cast<const char *>(u.block->get_data_pointer());
123 for(const Program::UniformCall &call: self.shprog->uniform_calls)
124 call.func(call.location, call.size, data+call.location*16);
132 if(changes&PipelineState::TEXTURES)
134 for(const PipelineState::BoundTexture &t: self.textures)
135 if(t.changed || changes==~0U)
139 if(ARB_direct_state_access)
140 glBindTextureUnit(t.binding, t.texture->id);
143 glActiveTexture(GL_TEXTURE0+t.binding);
144 if(dev_state.bound_tex_targets[t.binding] && static_cast<int>(t.texture->target)!=dev_state.bound_tex_targets[t.binding])
145 glBindTexture(dev_state.bound_tex_targets[t.binding], 0);
146 glBindTexture(t.texture->target, t.texture->id);
149 dev_state.bound_tex_targets[t.binding] = t.texture->target;
151 glBindSampler(t.binding, t.sampler->id);
152 t.sampler->refresh();
159 if(changes&PipelineState::VERTEX_SETUP)
161 const VertexSetup *vertex_setup = self.vertex_setup;
162 glBindVertexArray(vertex_setup ? vertex_setup->id : 0);
165 static Require _req(MSP_primitive_restart);
167 vertex_setup->refresh();
168 unsigned ri = (vertex_setup->get_index_type()==UNSIGNED_INT ? 0xFFFFFFFF : 0xFFFF);
169 if(ri!=dev_state.restart_index)
171 if(!dev_state.restart_index)
172 glEnable(GL_PRIMITIVE_RESTART);
173 glPrimitiveRestartIndex(ri);
174 dev_state.restart_index = ri;
179 if(changes&PipelineState::FACE_CULL)
181 glFrontFace(self.front_face==CLOCKWISE ? GL_CW : GL_CCW);
183 if(self.face_cull!=NO_CULL && self.front_face!=NON_MANIFOLD)
185 glEnable(GL_CULL_FACE);
186 glCullFace(self.face_cull==CULL_FRONT ? GL_FRONT : GL_BACK);
189 glDisable(GL_CULL_FACE);
192 if(changes&PipelineState::DEPTH_TEST)
194 const DepthTest &depth_test = self.depth_test;
195 if(depth_test.enabled)
197 glEnable(GL_DEPTH_TEST);
198 glDepthFunc(get_gl_predicate(depth_test.compare));
201 glDisable(GL_DEPTH_TEST);
203 glDepthMask(depth_test.write);
206 if(changes&PipelineState::STENCIL_TEST)
208 const StencilTest &stencil_test = self.stencil_test;
209 if(stencil_test.enabled)
211 glEnable(GL_STENCIL_TEST);
212 glStencilFunc(get_gl_predicate(stencil_test.compare), stencil_test.reference, 0xFFFFFFFF);
213 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));
216 glDisable(GL_STENCIL_TEST);
219 if(changes&PipelineState::BLEND)
221 const Blend &blend = self.blend;
225 glBlendEquation(get_gl_blend_equation(blend.equation));
226 glBlendFunc(get_gl_blend_factor(blend.src_factor), get_gl_blend_factor(blend.dst_factor));
227 glBlendColor(blend.constant.r, blend.constant.g, blend.constant.b, blend.constant.a);
228 ColorWriteMask cw = blend.write_mask;
229 glColorMask((cw&WRITE_RED)!=0, (cw&WRITE_GREEN)!=0, (cw&WRITE_BLUE)!=0, (cw&WRITE_ALPHA)!=0);
234 glColorMask(true, true, true, true);
238 applied_to = &device;
239 dev_state.last_pipeline = this;
243 void OpenGLPipelineState::clear()
245 OpenGLDeviceState &dev_state = Device::get_current().get_state();
246 if(dev_state.last_pipeline)
249 glBindVertexArray(0);
251 for(unsigned i=0; i<dev_state.n_clip_distances; ++i)
252 glDisable(GL_CLIP_PLANE0+i);
253 dev_state.n_clip_distances = 0;
255 for(unsigned i=0; i<dev_state.bound_tex_targets.size(); ++i)
256 if(dev_state.bound_tex_targets[i])
258 if(ARB_direct_state_access)
259 glBindTextureUnit(i, 0);
262 glActiveTexture(GL_TEXTURE0+i);
263 glBindTexture(dev_state.bound_tex_targets[i], 0);
265 dev_state.bound_tex_targets[i] = 0;
268 for(unsigned i=0; i<dev_state.bound_uniform_blocks.size(); ++i)
269 if(dev_state.bound_uniform_blocks[i])
271 glBindBufferBase(GL_UNIFORM_BUFFER, i, 0);
272 dev_state.bound_uniform_blocks[i] = 0;
275 glDisable(GL_DEPTH_TEST);
277 glDisable(GL_STENCIL_TEST);
280 dev_state.last_pipeline->applied_to = 0;
281 dev_state.last_pipeline = 0;