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_image_load_store.h>
4 #include <msp/gl/extensions/arb_shader_objects.h>
5 #include <msp/gl/extensions/arb_uniform_buffer_object.h>
6 #include <msp/gl/extensions/arb_vertex_array_object.h>
7 #include <msp/gl/extensions/arb_tessellation_shader.h>
8 #include <msp/gl/extensions/ext_framebuffer_object.h>
9 #include <msp/gl/extensions/msp_primitive_restart.h>
12 #include "depthtest.h"
14 #include "framebuffer.h"
16 #include "pipelinestate.h"
17 #include "pipelinestate_backend.h"
21 #include "stenciltest.h"
23 #include "uniformblock.h"
24 #include "vertexsetup.h"
31 OpenGLPipelineState::~OpenGLPipelineState()
34 applied_to->get_state().last_pipeline = 0;
37 void OpenGLPipelineState::apply() const
39 const PipelineState &self = *static_cast<const PipelineState *>(this);
40 Device &device = Device::get_current();
42 if(applied_to && applied_to!=&device)
44 applied_to->get_state().last_pipeline = 0;
48 OpenGLDeviceState &dev_state = device.get_state();
49 if(!dev_state.last_pipeline)
50 OpenGLTexture::unbind_scratch();
52 if(this!=dev_state.last_pipeline)
54 if(dev_state.last_pipeline)
55 dev_state.last_pipeline->applied_to = 0;
59 if(changes&PipelineState::FRAMEBUFFER)
61 const Framebuffer *framebuffer = self.framebuffer;
62 glBindFramebuffer(GL_FRAMEBUFFER, framebuffer ? framebuffer->id : 0);
65 framebuffer->refresh();
66 framebuffer->require_complete();
70 if(changes&(PipelineState::VIEWPORT|PipelineState::SCISSOR|PipelineState::FRAMEBUFFER))
71 if(const Framebuffer *framebuffer = self.framebuffer)
73 Rect fb_rect = framebuffer->get_rect();
74 if(changes&(PipelineState::VIEWPORT|PipelineState::FRAMEBUFFER))
76 Rect viewport = fb_rect.intersect(self.viewport);
77 glViewport(viewport.left, viewport.bottom, viewport.width, viewport.height);
79 if(changes&(PipelineState::SCISSOR|PipelineState::FRAMEBUFFER))
81 Rect scissor = fb_rect.intersect(self.scissor);
84 glEnable(GL_SCISSOR_TEST);
85 glScissor(scissor.left, scissor.bottom, scissor.width, scissor.height);
88 glDisable(GL_SCISSOR_TEST);
92 if(changes&PipelineState::SHPROG)
94 glUseProgram(self.shprog ? self.shprog->id : 0);
96 unsigned ncd = (self.shprog ? self.shprog->get_n_clip_distances() : 0);
97 if(ncd!=dev_state.n_clip_distances)
99 for(unsigned i=0; (i<ncd || i<dev_state.n_clip_distances); ++i)
102 glEnable(GL_CLIP_PLANE0+i);
104 glDisable(GL_CLIP_PLANE0+i);
106 dev_state.n_clip_distances = ncd;
110 if(changes&PipelineState::RESOURCES)
112 for(const PipelineState::BoundResource &r: self.resources)
114 if(!r.changed && changes!=~0U)
119 if(r.type==PipelineState::UNIFORM_BLOCK)
123 glBindBufferRange(GL_UNIFORM_BUFFER, r.binding, r.buffer->id, r.block->get_offset(), r.block->get_data_size());
124 dev_state.bound_uniform_blocks[r.binding] = 1;
126 else if(r.binding==ReflectData::DEFAULT_BLOCK && self.shprog)
128 const char *data = static_cast<const char *>(r.block->get_data_pointer());
129 for(const Program::UniformCall &call: self.shprog->uniform_calls)
130 call.func(call.location, call.size, data+call.location*16);
133 else if(r.type==PipelineState::SAMPLED_TEXTURE)
135 if(ARB_direct_state_access)
136 glBindTextureUnit(r.binding, r.texture->id);
139 glActiveTexture(GL_TEXTURE0+r.binding);
140 if(dev_state.bound_tex_targets[r.binding] && static_cast<int>(r.texture->target)!=dev_state.bound_tex_targets[r.binding])
141 glBindTexture(dev_state.bound_tex_targets[r.binding], 0);
142 glBindTexture(r.texture->target, r.texture->id);
145 dev_state.bound_tex_targets[r.binding] = r.texture->target;
147 glBindSampler(r.binding, r.sampler->id);
148 r.sampler->refresh();
150 else if(r.type==PipelineState::STORAGE_TEXTURE)
152 static Require _req(ARB_shader_image_load_store);
153 GLenum gl_format = get_gl_pixelformat(r.texture->get_format());
154 glBindImageTexture(r.binding, r.texture->id, 0, true, 0, GL_READ_WRITE, gl_format);
156 dev_state.bound_storage_textures[r.binding] = 1;
164 if(changes&PipelineState::VERTEX_SETUP)
166 const VertexSetup *vertex_setup = self.vertex_setup;
167 glBindVertexArray(vertex_setup ? vertex_setup->id : 0);
170 static Require _req(MSP_primitive_restart);
172 vertex_setup->refresh();
173 unsigned ri = (vertex_setup->get_index_type()==UNSIGNED_INT ? 0xFFFFFFFF : 0xFFFF);
174 if(ri!=dev_state.restart_index)
176 if(!dev_state.restart_index)
177 glEnable(GL_PRIMITIVE_RESTART);
178 glPrimitiveRestartIndex(ri);
179 dev_state.restart_index = ri;
184 if(changes&PipelineState::PATCH_SIZE)
187 static Require _req(ARB_tessellation_shader);
188 glPatchParameteri(GL_PATCH_VERTICES, self.patch_size);
191 if(changes&PipelineState::FACE_CULL)
193 glFrontFace(self.front_face==CLOCKWISE ? GL_CW : GL_CCW);
195 if(self.face_cull!=NO_CULL && self.front_face!=NON_MANIFOLD)
197 glEnable(GL_CULL_FACE);
198 glCullFace(self.face_cull==CULL_FRONT ? GL_FRONT : GL_BACK);
201 glDisable(GL_CULL_FACE);
204 if(changes&PipelineState::DEPTH_TEST)
206 const DepthTest &depth_test = self.depth_test;
207 if(depth_test.enabled)
209 glEnable(GL_DEPTH_TEST);
210 glDepthFunc(get_gl_predicate(depth_test.compare));
213 glDisable(GL_DEPTH_TEST);
215 glDepthMask(depth_test.write);
218 if(changes&PipelineState::STENCIL_TEST)
220 const StencilTest &stencil_test = self.stencil_test;
221 if(stencil_test.enabled)
223 glEnable(GL_STENCIL_TEST);
224 glStencilFunc(get_gl_predicate(stencil_test.compare), stencil_test.reference, 0xFFFFFFFF);
225 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));
228 glDisable(GL_STENCIL_TEST);
231 if(changes&PipelineState::BLEND)
233 const Blend &blend = self.blend;
237 glBlendEquation(get_gl_blend_equation(blend.equation));
238 glBlendFunc(get_gl_blend_factor(blend.src_factor), get_gl_blend_factor(blend.dst_factor));
239 glBlendColor(blend.constant.r, blend.constant.g, blend.constant.b, blend.constant.a);
240 ColorWriteMask cw = blend.write_mask;
241 glColorMask((cw&WRITE_RED)!=0, (cw&WRITE_GREEN)!=0, (cw&WRITE_BLUE)!=0, (cw&WRITE_ALPHA)!=0);
246 glColorMask(true, true, true, true);
249 if(blend.alpha_to_coverage && self.framebuffer && self.framebuffer->get_format().get_samples()>1)
250 glEnable(GL_SAMPLE_ALPHA_TO_COVERAGE);
252 glDisable(GL_SAMPLE_ALPHA_TO_COVERAGE);
255 applied_to = &device;
256 dev_state.last_pipeline = this;
260 void OpenGLPipelineState::clear()
262 OpenGLDeviceState &dev_state = Device::get_current().get_state();
263 if(dev_state.last_pipeline)
266 glBindVertexArray(0);
268 for(unsigned i=0; i<dev_state.n_clip_distances; ++i)
269 glDisable(GL_CLIP_PLANE0+i);
270 dev_state.n_clip_distances = 0;
272 for(unsigned i=0; i<dev_state.bound_tex_targets.size(); ++i)
273 if(dev_state.bound_tex_targets[i])
275 if(ARB_direct_state_access)
276 glBindTextureUnit(i, 0);
279 glActiveTexture(GL_TEXTURE0+i);
280 glBindTexture(dev_state.bound_tex_targets[i], 0);
282 dev_state.bound_tex_targets[i] = 0;
285 for(unsigned i=0; i<dev_state.bound_storage_textures.size(); ++i)
286 if(dev_state.bound_storage_textures[i])
288 glBindImageTexture(i, 0, 0, true, 0, GL_READ_ONLY, GL_RGBA8);
289 dev_state.bound_storage_textures[i] = 0;
292 for(unsigned i=0; i<dev_state.bound_uniform_blocks.size(); ++i)
293 if(dev_state.bound_uniform_blocks[i])
295 glBindBufferBase(GL_UNIFORM_BUFFER, i, 0);
296 dev_state.bound_uniform_blocks[i] = 0;
299 glDisable(GL_DEPTH_TEST);
301 glDisable(GL_STENCIL_TEST);
304 dev_state.last_pipeline->applied_to = 0;
305 dev_state.last_pipeline = 0;