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"
11 #include "deviceinfo.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 const OpenGLPipelineState *OpenGLPipelineState::last_applied = 0;
30 vector<int> OpenGLPipelineState::bound_tex_targets;
31 vector<char> OpenGLPipelineState::bound_uniform_blocks;
32 unsigned OpenGLPipelineState::restart_index = 0;
34 OpenGLPipelineState::OpenGLPipelineState()
36 if(bound_tex_targets.empty())
37 bound_tex_targets.resize(DeviceInfo::get_global().limits.max_texture_bindings);
38 if(bound_uniform_blocks.empty())
39 bound_uniform_blocks.resize(DeviceInfo::get_global().limits.max_uniform_bindings);
42 OpenGLPipelineState::~OpenGLPipelineState()
44 if(this==last_applied)
48 void OpenGLPipelineState::apply() const
51 OpenGLTexture::unbind_scratch();
53 apply(this==last_applied ? static_cast<const PipelineState *>(this)->changes : ~0U);
56 void OpenGLPipelineState::apply(unsigned mask) const
58 const PipelineState *self = static_cast<const PipelineState *>(this);
60 if(mask&PipelineState::FRAMEBUFFER)
62 const Framebuffer *framebuffer = self->framebuffer;
63 glBindFramebuffer(GL_FRAMEBUFFER, framebuffer ? framebuffer->id : 0);
66 framebuffer->refresh();
67 framebuffer->require_complete();
71 if(mask&(PipelineState::VIEWPORT|PipelineState::FRAMEBUFFER))
73 if(const Rect *viewport = self->viewport)
74 glViewport(viewport->left, viewport->bottom, viewport->width, viewport->height);
75 else if(const Framebuffer *framebuffer = self->framebuffer)
76 glViewport(0, 0, framebuffer->get_width(), framebuffer->get_height());
79 if(mask&PipelineState::SCISSOR)
81 if(const Rect *scissor = self->scissor)
83 glEnable(GL_SCISSOR_TEST);
84 glScissor(scissor->left, scissor->bottom, scissor->width, scissor->height);
87 glDisable(GL_SCISSOR_TEST);
90 if(mask&PipelineState::SHPROG)
91 glUseProgram(self->shprog ? self->shprog->id : 0);
93 if(mask&PipelineState::VERTEX_SETUP)
95 const VertexSetup *vertex_setup = self->vertex_setup;
96 glBindVertexArray(vertex_setup ? vertex_setup->id : 0);
99 static Require _req(MSP_primitive_restart);
101 vertex_setup->refresh();
102 unsigned ri = (vertex_setup->get_index_type()==UNSIGNED_INT ? 0xFFFFFFFF : 0xFFFF);
103 if(ri!=restart_index)
106 glEnable(GL_PRIMITIVE_RESTART);
107 glPrimitiveRestartIndex(ri);
113 if(mask&PipelineState::FACE_CULL)
115 glFrontFace(self->front_face==CLOCKWISE ? GL_CW : GL_CCW);
117 if(self->face_cull!=NO_CULL && self->front_face!=NON_MANIFOLD)
119 glEnable(GL_CULL_FACE);
120 glCullFace(self->face_cull==CULL_FRONT ? GL_FRONT : GL_BACK);
123 glDisable(GL_CULL_FACE);
126 if(mask&PipelineState::CLIP_PLANES)
128 unsigned max_clip_planes = DeviceInfo::get_global().limits.max_clip_planes;
129 for(unsigned i=0; i<max_clip_planes; ++i)
131 if((self->enabled_clip_planes>>i)&1)
132 glEnable(GL_CLIP_PLANE0+i);
134 glDisable(GL_CLIP_PLANE0+i);
138 if(mask&PipelineState::TEXTURES)
140 for(const PipelineState::BoundTexture &t: self->textures)
141 if(t.changed || mask==~0U)
143 if(t.texture && t.sampler)
145 if(ARB_direct_state_access)
146 glBindTextureUnit(t.binding, t.texture->id);
149 glActiveTexture(GL_TEXTURE0+t.binding);
150 if(bound_tex_targets[t.binding] && static_cast<int>(t.texture->target)!=bound_tex_targets[t.binding])
151 glBindTexture(bound_tex_targets[t.binding], 0);
152 glBindTexture(t.texture->target, t.texture->id);
155 bound_tex_targets[t.binding] = t.texture->target;
157 glBindSampler(t.binding, t.sampler->id);
158 t.sampler->refresh();
165 if(mask&PipelineState::UNIFORMS)
167 for(const PipelineState::BoundUniformBlock &u: self->uniform_blocks)
168 if(u.changed || mask==~0U)
174 glBindBufferRange(GL_UNIFORM_BUFFER, u.binding, u.block->get_buffer()->id, u.block->get_offset(), u.block->get_data_size());
175 bound_uniform_blocks[u.binding] = 1;
177 else if(self->shprog)
179 const char *data = static_cast<const char *>(u.block->get_data_pointer());
180 for(const Program::UniformCall &call: self->shprog->uniform_calls)
181 call.func(call.location, call.size, data+call.location*16);
189 if(mask&PipelineState::DEPTH_TEST)
191 const DepthTest *depth_test = self->depth_test;
192 if(depth_test && depth_test->enabled)
194 glEnable(GL_DEPTH_TEST);
195 glDepthFunc(get_gl_predicate(depth_test->compare));
198 glDisable(GL_DEPTH_TEST);
200 glDepthMask(!depth_test || depth_test->write);
203 if(mask&PipelineState::STENCIL_TEST)
205 const StencilTest *stencil_test = self->stencil_test;
206 if(stencil_test && stencil_test->enabled)
208 glEnable(GL_STENCIL_TEST);
209 glStencilFunc(get_gl_predicate(stencil_test->compare), stencil_test->reference, 0xFFFFFFFF);
210 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));
213 glDisable(GL_STENCIL_TEST);
216 if(mask&PipelineState::BLEND)
218 const Blend *blend = self->blend;
219 if(blend && blend->enabled)
222 glBlendEquation(get_gl_blend_equation(blend->equation));
223 glBlendFunc(get_gl_blend_factor(blend->src_factor), get_gl_blend_factor(blend->dst_factor));
224 glBlendColor(blend->constant.r, blend->constant.g, blend->constant.b, blend->constant.a);
225 ColorWriteMask cw = blend->write_mask;
226 glColorMask((cw&WRITE_RED)!=0, (cw&WRITE_GREEN)!=0, (cw&WRITE_BLUE)!=0, (cw&WRITE_ALPHA)!=0);
231 glColorMask(true, true, true, true);
236 self->changes &= ~mask;
239 void OpenGLPipelineState::clear()
244 glBindVertexArray(0);
246 unsigned max_clip_planes = DeviceInfo::get_global().limits.max_clip_planes;
247 unsigned enabled_clip_planes = static_cast<const PipelineState *>(last_applied)->enabled_clip_planes;
248 for(unsigned i=0; i<max_clip_planes; ++i)
249 if((enabled_clip_planes>>i)&1)
250 glDisable(GL_CLIP_PLANE0+i);
252 for(unsigned i=0; i<bound_tex_targets.size(); ++i)
253 if(bound_tex_targets[i])
255 if(ARB_direct_state_access)
256 glBindTextureUnit(i, 0);
259 glActiveTexture(GL_TEXTURE0+i);
260 glBindTexture(bound_tex_targets[i], 0);
262 bound_tex_targets[i] = 0;
265 for(unsigned i=0; i<bound_uniform_blocks.size(); ++i)
266 if(bound_uniform_blocks[i])
268 glBindBufferBase(GL_UNIFORM_BUFFER, i, 0);
269 bound_uniform_blocks[i] = 0;
272 glDisable(GL_DEPTH_TEST);
274 glDisable(GL_STENCIL_TEST);