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::FRAMEBUFFER))
71 if(const Rect *viewport = self.viewport)
72 glViewport(viewport->left, viewport->bottom, viewport->width, viewport->height);
73 else if(const Framebuffer *framebuffer = self.framebuffer)
74 glViewport(0, 0, framebuffer->get_width(), framebuffer->get_height());
77 if(mask&PipelineState::SCISSOR)
79 if(const Rect *scissor = self.scissor)
81 glEnable(GL_SCISSOR_TEST);
82 glScissor(scissor->left, scissor->bottom, scissor->width, scissor->height);
85 glDisable(GL_SCISSOR_TEST);
88 if(mask&PipelineState::SHPROG)
90 glUseProgram(self.shprog ? self.shprog->id : 0);
92 unsigned ncd = (self.shprog ? self.shprog->get_n_clip_distances() : 0);
93 if(ncd!=dev_state.n_clip_distances)
95 for(unsigned i=0; (i<ncd || i<dev_state.n_clip_distances); ++i)
98 glEnable(GL_CLIP_PLANE0+i);
100 glDisable(GL_CLIP_PLANE0+i);
102 dev_state.n_clip_distances = ncd;
106 if(mask&PipelineState::UNIFORMS)
108 for(const PipelineState::BoundUniformBlock &u: self.uniform_blocks)
109 if(u.changed || mask==~0U)
115 glBindBufferRange(GL_UNIFORM_BUFFER, u.binding, u.buffer->id, u.block->get_offset(), u.block->get_data_size());
116 dev_state.bound_uniform_blocks[u.binding] = 1;
118 else if(u.binding==ReflectData::DEFAULT_BLOCK && self.shprog)
120 const char *data = static_cast<const char *>(u.block->get_data_pointer());
121 for(const Program::UniformCall &call: self.shprog->uniform_calls)
122 call.func(call.location, call.size, data+call.location*16);
130 if(mask&PipelineState::TEXTURES)
132 for(const PipelineState::BoundTexture &t: self.textures)
133 if(t.changed || mask==~0U)
137 if(ARB_direct_state_access)
138 glBindTextureUnit(t.binding, t.texture->id);
141 glActiveTexture(GL_TEXTURE0+t.binding);
142 if(dev_state.bound_tex_targets[t.binding] && static_cast<int>(t.texture->target)!=dev_state.bound_tex_targets[t.binding])
143 glBindTexture(dev_state.bound_tex_targets[t.binding], 0);
144 glBindTexture(t.texture->target, t.texture->id);
147 dev_state.bound_tex_targets[t.binding] = t.texture->target;
149 glBindSampler(t.binding, t.sampler->id);
150 t.sampler->refresh();
157 if(mask&PipelineState::VERTEX_SETUP)
159 const VertexSetup *vertex_setup = self.vertex_setup;
160 glBindVertexArray(vertex_setup ? vertex_setup->id : 0);
163 static Require _req(MSP_primitive_restart);
165 vertex_setup->refresh();
166 unsigned ri = (vertex_setup->get_index_type()==UNSIGNED_INT ? 0xFFFFFFFF : 0xFFFF);
167 if(ri!=dev_state.restart_index)
169 if(!dev_state.restart_index)
170 glEnable(GL_PRIMITIVE_RESTART);
171 glPrimitiveRestartIndex(ri);
172 dev_state.restart_index = ri;
177 if(mask&PipelineState::FACE_CULL)
179 glFrontFace(self.front_face==CLOCKWISE ? GL_CW : GL_CCW);
181 if(self.face_cull!=NO_CULL && self.front_face!=NON_MANIFOLD)
183 glEnable(GL_CULL_FACE);
184 glCullFace(self.face_cull==CULL_FRONT ? GL_FRONT : GL_BACK);
187 glDisable(GL_CULL_FACE);
190 if(mask&PipelineState::DEPTH_TEST)
192 const DepthTest *depth_test = self.depth_test;
193 if(depth_test && depth_test->enabled)
195 glEnable(GL_DEPTH_TEST);
196 glDepthFunc(get_gl_predicate(depth_test->compare));
199 glDisable(GL_DEPTH_TEST);
201 glDepthMask(!depth_test || depth_test->write);
204 if(mask&PipelineState::STENCIL_TEST)
206 const StencilTest *stencil_test = self.stencil_test;
207 if(stencil_test && stencil_test->enabled)
209 glEnable(GL_STENCIL_TEST);
210 glStencilFunc(get_gl_predicate(stencil_test->compare), stencil_test->reference, 0xFFFFFFFF);
211 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));
214 glDisable(GL_STENCIL_TEST);
217 if(mask&PipelineState::BLEND)
219 const Blend *blend = self.blend;
220 if(blend && blend->enabled)
223 glBlendEquation(get_gl_blend_equation(blend->equation));
224 glBlendFunc(get_gl_blend_factor(blend->src_factor), get_gl_blend_factor(blend->dst_factor));
225 glBlendColor(blend->constant.r, blend->constant.g, blend->constant.b, blend->constant.a);
226 ColorWriteMask cw = blend->write_mask;
227 glColorMask((cw&WRITE_RED)!=0, (cw&WRITE_GREEN)!=0, (cw&WRITE_BLUE)!=0, (cw&WRITE_ALPHA)!=0);
232 glColorMask(true, true, true, true);
236 applied_to = &device;
237 dev_state.last_pipeline = this;
241 void OpenGLPipelineState::clear()
243 OpenGLDeviceState &dev_state = Device::get_current().get_state();
244 if(dev_state.last_pipeline)
247 glBindVertexArray(0);
249 for(unsigned i=0; i<dev_state.n_clip_distances; ++i)
250 glDisable(GL_CLIP_PLANE0+i);
251 dev_state.n_clip_distances = 0;
253 for(unsigned i=0; i<dev_state.bound_tex_targets.size(); ++i)
254 if(dev_state.bound_tex_targets[i])
256 if(ARB_direct_state_access)
257 glBindTextureUnit(i, 0);
260 glActiveTexture(GL_TEXTURE0+i);
261 glBindTexture(dev_state.bound_tex_targets[i], 0);
263 dev_state.bound_tex_targets[i] = 0;
266 for(unsigned i=0; i<dev_state.bound_uniform_blocks.size(); ++i)
267 if(dev_state.bound_uniform_blocks[i])
269 glBindBufferBase(GL_UNIFORM_BUFFER, i, 0);
270 dev_state.bound_uniform_blocks[i] = 0;
273 glDisable(GL_DEPTH_TEST);
275 glDisable(GL_STENCIL_TEST);
278 dev_state.last_pipeline->applied_to = 0;
279 dev_state.last_pipeline = 0;