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 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;
33 unsigned OpenGLPipelineState::n_clip_distances = 0;
35 OpenGLPipelineState::OpenGLPipelineState()
37 if(bound_tex_targets.empty())
38 bound_tex_targets.resize(Device::get_current().get_info().limits.max_texture_bindings);
39 if(bound_uniform_blocks.empty())
40 bound_uniform_blocks.resize(Device::get_current().get_info().limits.max_uniform_bindings);
43 OpenGLPipelineState::~OpenGLPipelineState()
45 if(this==last_applied)
49 void OpenGLPipelineState::apply() const
52 OpenGLTexture::unbind_scratch();
54 apply(this==last_applied ? static_cast<const PipelineState *>(this)->changes : ~0U);
57 void OpenGLPipelineState::apply(unsigned mask) const
59 const PipelineState &self = *static_cast<const PipelineState *>(this);
61 if(mask&PipelineState::FRAMEBUFFER)
63 const Framebuffer *framebuffer = self.framebuffer;
64 glBindFramebuffer(GL_FRAMEBUFFER, framebuffer ? framebuffer->id : 0);
67 framebuffer->refresh();
68 framebuffer->require_complete();
72 if(mask&(PipelineState::VIEWPORT|PipelineState::FRAMEBUFFER))
74 if(const Rect *viewport = self.viewport)
75 glViewport(viewport->left, viewport->bottom, viewport->width, viewport->height);
76 else if(const Framebuffer *framebuffer = self.framebuffer)
77 glViewport(0, 0, framebuffer->get_width(), framebuffer->get_height());
80 if(mask&PipelineState::SCISSOR)
82 if(const Rect *scissor = self.scissor)
84 glEnable(GL_SCISSOR_TEST);
85 glScissor(scissor->left, scissor->bottom, scissor->width, scissor->height);
88 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!=n_clip_distances)
98 for(unsigned i=0; (i<ncd || i<n_clip_distances); ++i)
101 glEnable(GL_CLIP_PLANE0+i);
103 glDisable(GL_CLIP_PLANE0+i);
105 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.block->get_buffer()->id, u.block->get_offset(), u.block->get_data_size());
119 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)
138 if(t.texture && t.sampler)
140 if(ARB_direct_state_access)
141 glBindTextureUnit(t.binding, t.texture->id);
144 glActiveTexture(GL_TEXTURE0+t.binding);
145 if(bound_tex_targets[t.binding] && static_cast<int>(t.texture->target)!=bound_tex_targets[t.binding])
146 glBindTexture(bound_tex_targets[t.binding], 0);
147 glBindTexture(t.texture->target, t.texture->id);
150 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!=restart_index)
173 glEnable(GL_PRIMITIVE_RESTART);
174 glPrimitiveRestartIndex(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 && 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 || depth_test->write);
207 if(mask&PipelineState::STENCIL_TEST)
209 const StencilTest *stencil_test = self.stencil_test;
210 if(stencil_test && 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;
223 if(blend && blend->enabled)
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);
240 self.changes &= ~mask;
243 void OpenGLPipelineState::clear()
248 glBindVertexArray(0);
250 for(unsigned i=0; i<n_clip_distances; ++i)
251 glDisable(GL_CLIP_PLANE0+i);
252 n_clip_distances = 0;
254 for(unsigned i=0; i<bound_tex_targets.size(); ++i)
255 if(bound_tex_targets[i])
257 if(ARB_direct_state_access)
258 glBindTextureUnit(i, 0);
261 glActiveTexture(GL_TEXTURE0+i);
262 glBindTexture(bound_tex_targets[i], 0);
264 bound_tex_targets[i] = 0;
267 for(unsigned i=0; i<bound_uniform_blocks.size(); ++i)
268 if(bound_uniform_blocks[i])
270 glBindBufferBase(GL_UNIFORM_BUFFER, i, 0);
271 bound_uniform_blocks[i] = 0;
274 glDisable(GL_DEPTH_TEST);
276 glDisable(GL_STENCIL_TEST);