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::RESOURCES)
110 for(const PipelineState::BoundResource &r: self.resources)
112 if(!r.changed && changes!=~0U)
117 if(r.type==PipelineState::UNIFORM_BLOCK)
121 glBindBufferRange(GL_UNIFORM_BUFFER, r.binding, r.buffer->id, r.block->get_offset(), r.block->get_data_size());
122 dev_state.bound_uniform_blocks[r.binding] = 1;
124 else if(r.binding==ReflectData::DEFAULT_BLOCK && self.shprog)
126 const char *data = static_cast<const char *>(r.block->get_data_pointer());
127 for(const Program::UniformCall &call: self.shprog->uniform_calls)
128 call.func(call.location, call.size, data+call.location*16);
131 else if(r.type==PipelineState::TEXTURE)
133 if(ARB_direct_state_access)
134 glBindTextureUnit(r.binding, r.texture->id);
137 glActiveTexture(GL_TEXTURE0+r.binding);
138 if(dev_state.bound_tex_targets[r.binding] && static_cast<int>(r.texture->target)!=dev_state.bound_tex_targets[r.binding])
139 glBindTexture(dev_state.bound_tex_targets[r.binding], 0);
140 glBindTexture(r.texture->target, r.texture->id);
143 dev_state.bound_tex_targets[r.binding] = r.texture->target;
145 glBindSampler(r.binding, r.sampler->id);
146 r.sampler->refresh();
154 if(changes&PipelineState::VERTEX_SETUP)
156 const VertexSetup *vertex_setup = self.vertex_setup;
157 glBindVertexArray(vertex_setup ? vertex_setup->id : 0);
160 static Require _req(MSP_primitive_restart);
162 vertex_setup->refresh();
163 unsigned ri = (vertex_setup->get_index_type()==UNSIGNED_INT ? 0xFFFFFFFF : 0xFFFF);
164 if(ri!=dev_state.restart_index)
166 if(!dev_state.restart_index)
167 glEnable(GL_PRIMITIVE_RESTART);
168 glPrimitiveRestartIndex(ri);
169 dev_state.restart_index = ri;
174 if(changes&PipelineState::FACE_CULL)
176 glFrontFace(self.front_face==CLOCKWISE ? GL_CW : GL_CCW);
178 if(self.face_cull!=NO_CULL && self.front_face!=NON_MANIFOLD)
180 glEnable(GL_CULL_FACE);
181 glCullFace(self.face_cull==CULL_FRONT ? GL_FRONT : GL_BACK);
184 glDisable(GL_CULL_FACE);
187 if(changes&PipelineState::DEPTH_TEST)
189 const DepthTest &depth_test = self.depth_test;
190 if(depth_test.enabled)
192 glEnable(GL_DEPTH_TEST);
193 glDepthFunc(get_gl_predicate(depth_test.compare));
196 glDisable(GL_DEPTH_TEST);
198 glDepthMask(depth_test.write);
201 if(changes&PipelineState::STENCIL_TEST)
203 const StencilTest &stencil_test = self.stencil_test;
204 if(stencil_test.enabled)
206 glEnable(GL_STENCIL_TEST);
207 glStencilFunc(get_gl_predicate(stencil_test.compare), stencil_test.reference, 0xFFFFFFFF);
208 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));
211 glDisable(GL_STENCIL_TEST);
214 if(changes&PipelineState::BLEND)
216 const Blend &blend = self.blend;
220 glBlendEquation(get_gl_blend_equation(blend.equation));
221 glBlendFunc(get_gl_blend_factor(blend.src_factor), get_gl_blend_factor(blend.dst_factor));
222 glBlendColor(blend.constant.r, blend.constant.g, blend.constant.b, blend.constant.a);
223 ColorWriteMask cw = blend.write_mask;
224 glColorMask((cw&WRITE_RED)!=0, (cw&WRITE_GREEN)!=0, (cw&WRITE_BLUE)!=0, (cw&WRITE_ALPHA)!=0);
229 glColorMask(true, true, true, true);
233 applied_to = &device;
234 dev_state.last_pipeline = this;
238 void OpenGLPipelineState::clear()
240 OpenGLDeviceState &dev_state = Device::get_current().get_state();
241 if(dev_state.last_pipeline)
244 glBindVertexArray(0);
246 for(unsigned i=0; i<dev_state.n_clip_distances; ++i)
247 glDisable(GL_CLIP_PLANE0+i);
248 dev_state.n_clip_distances = 0;
250 for(unsigned i=0; i<dev_state.bound_tex_targets.size(); ++i)
251 if(dev_state.bound_tex_targets[i])
253 if(ARB_direct_state_access)
254 glBindTextureUnit(i, 0);
257 glActiveTexture(GL_TEXTURE0+i);
258 glBindTexture(dev_state.bound_tex_targets[i], 0);
260 dev_state.bound_tex_targets[i] = 0;
263 for(unsigned i=0; i<dev_state.bound_uniform_blocks.size(); ++i)
264 if(dev_state.bound_uniform_blocks[i])
266 glBindBufferBase(GL_UNIFORM_BUFFER, i, 0);
267 dev_state.bound_uniform_blocks[i] = 0;
270 glDisable(GL_DEPTH_TEST);
272 glDisable(GL_STENCIL_TEST);
275 dev_state.last_pipeline->applied_to = 0;
276 dev_state.last_pipeline = 0;