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();
39 unsigned mask = changes;
41 if(applied_to && applied_to!=&device)
43 applied_to->get_state().last_pipeline = 0;
47 OpenGLDeviceState &dev_state = device.get_state();
48 if(!dev_state.last_pipeline)
49 OpenGLTexture::unbind_scratch();
51 if(this!=dev_state.last_pipeline)
53 if(dev_state.last_pipeline)
54 dev_state.last_pipeline->applied_to = 0;
58 if(mask&PipelineState::FRAMEBUFFER)
60 const Framebuffer *framebuffer = self.framebuffer;
61 glBindFramebuffer(GL_FRAMEBUFFER, framebuffer ? framebuffer->id : 0);
64 framebuffer->refresh();
65 framebuffer->require_complete();
69 if(mask&(PipelineState::VIEWPORT|PipelineState::SCISSOR|PipelineState::FRAMEBUFFER))
70 if(const Framebuffer *framebuffer = self.framebuffer)
72 Rect fb_rect = framebuffer->get_rect();
73 if(mask&(PipelineState::VIEWPORT|PipelineState::FRAMEBUFFER))
75 Rect viewport = fb_rect.intersect(self.viewport);
76 glViewport(viewport.left, viewport.bottom, viewport.width, viewport.height);
78 if(mask&(PipelineState::SCISSOR|PipelineState::FRAMEBUFFER))
80 Rect scissor = fb_rect.intersect(self.scissor);
83 glEnable(GL_SCISSOR_TEST);
84 glScissor(scissor.left, scissor.bottom, scissor.width, scissor.height);
87 glDisable(GL_SCISSOR_TEST);
91 if(mask&PipelineState::SHPROG)
93 glUseProgram(self.shprog ? self.shprog->id : 0);
95 unsigned ncd = (self.shprog ? self.shprog->get_n_clip_distances() : 0);
96 if(ncd!=dev_state.n_clip_distances)
98 for(unsigned i=0; (i<ncd || i<dev_state.n_clip_distances); ++i)
101 glEnable(GL_CLIP_PLANE0+i);
103 glDisable(GL_CLIP_PLANE0+i);
105 dev_state.n_clip_distances = ncd;
109 if(mask&PipelineState::UNIFORMS)
111 for(const PipelineState::BoundUniformBlock &u: self.uniform_blocks)
112 if(u.changed || mask==~0U)
118 glBindBufferRange(GL_UNIFORM_BUFFER, u.binding, u.buffer->id, u.block->get_offset(), u.block->get_data_size());
119 dev_state.bound_uniform_blocks[u.binding] = 1;
121 else if(u.binding==ReflectData::DEFAULT_BLOCK && self.shprog)
123 const char *data = static_cast<const char *>(u.block->get_data_pointer());
124 for(const Program::UniformCall &call: self.shprog->uniform_calls)
125 call.func(call.location, call.size, data+call.location*16);
133 if(mask&PipelineState::TEXTURES)
135 for(const PipelineState::BoundTexture &t: self.textures)
136 if(t.changed || mask==~0U)
140 if(ARB_direct_state_access)
141 glBindTextureUnit(t.binding, t.texture->id);
144 glActiveTexture(GL_TEXTURE0+t.binding);
145 if(dev_state.bound_tex_targets[t.binding] && static_cast<int>(t.texture->target)!=dev_state.bound_tex_targets[t.binding])
146 glBindTexture(dev_state.bound_tex_targets[t.binding], 0);
147 glBindTexture(t.texture->target, t.texture->id);
150 dev_state.bound_tex_targets[t.binding] = t.texture->target;
152 glBindSampler(t.binding, t.sampler->id);
153 t.sampler->refresh();
160 if(mask&PipelineState::VERTEX_SETUP)
162 const VertexSetup *vertex_setup = self.vertex_setup;
163 glBindVertexArray(vertex_setup ? vertex_setup->id : 0);
166 static Require _req(MSP_primitive_restart);
168 vertex_setup->refresh();
169 unsigned ri = (vertex_setup->get_index_type()==UNSIGNED_INT ? 0xFFFFFFFF : 0xFFFF);
170 if(ri!=dev_state.restart_index)
172 if(!dev_state.restart_index)
173 glEnable(GL_PRIMITIVE_RESTART);
174 glPrimitiveRestartIndex(ri);
175 dev_state.restart_index = ri;
180 if(mask&PipelineState::FACE_CULL)
182 glFrontFace(self.front_face==CLOCKWISE ? GL_CW : GL_CCW);
184 if(self.face_cull!=NO_CULL && self.front_face!=NON_MANIFOLD)
186 glEnable(GL_CULL_FACE);
187 glCullFace(self.face_cull==CULL_FRONT ? GL_FRONT : GL_BACK);
190 glDisable(GL_CULL_FACE);
193 if(mask&PipelineState::DEPTH_TEST)
195 const DepthTest &depth_test = self.depth_test;
196 if(depth_test.enabled)
198 glEnable(GL_DEPTH_TEST);
199 glDepthFunc(get_gl_predicate(depth_test.compare));
202 glDisable(GL_DEPTH_TEST);
204 glDepthMask(depth_test.write);
207 if(mask&PipelineState::STENCIL_TEST)
209 const StencilTest &stencil_test = self.stencil_test;
210 if(stencil_test.enabled)
212 glEnable(GL_STENCIL_TEST);
213 glStencilFunc(get_gl_predicate(stencil_test.compare), stencil_test.reference, 0xFFFFFFFF);
214 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));
217 glDisable(GL_STENCIL_TEST);
220 if(mask&PipelineState::BLEND)
222 const Blend &blend = self.blend;
226 glBlendEquation(get_gl_blend_equation(blend.equation));
227 glBlendFunc(get_gl_blend_factor(blend.src_factor), get_gl_blend_factor(blend.dst_factor));
228 glBlendColor(blend.constant.r, blend.constant.g, blend.constant.b, blend.constant.a);
229 ColorWriteMask cw = blend.write_mask;
230 glColorMask((cw&WRITE_RED)!=0, (cw&WRITE_GREEN)!=0, (cw&WRITE_BLUE)!=0, (cw&WRITE_ALPHA)!=0);
235 glColorMask(true, true, true, true);
239 applied_to = &device;
240 dev_state.last_pipeline = this;
244 void OpenGLPipelineState::clear()
246 OpenGLDeviceState &dev_state = Device::get_current().get_state();
247 if(dev_state.last_pipeline)
250 glBindVertexArray(0);
252 for(unsigned i=0; i<dev_state.n_clip_distances; ++i)
253 glDisable(GL_CLIP_PLANE0+i);
254 dev_state.n_clip_distances = 0;
256 for(unsigned i=0; i<dev_state.bound_tex_targets.size(); ++i)
257 if(dev_state.bound_tex_targets[i])
259 if(ARB_direct_state_access)
260 glBindTextureUnit(i, 0);
263 glActiveTexture(GL_TEXTURE0+i);
264 glBindTexture(dev_state.bound_tex_targets[i], 0);
266 dev_state.bound_tex_targets[i] = 0;
269 for(unsigned i=0; i<dev_state.bound_uniform_blocks.size(); ++i)
270 if(dev_state.bound_uniform_blocks[i])
272 glBindBufferBase(GL_UNIFORM_BUFFER, i, 0);
273 dev_state.bound_uniform_blocks[i] = 0;
276 glDisable(GL_DEPTH_TEST);
278 glDisable(GL_STENCIL_TEST);
281 dev_state.last_pipeline->applied_to = 0;
282 dev_state.last_pipeline = 0;