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/ext_framebuffer_object.h>
8 #include <msp/gl/extensions/msp_primitive_restart.h>
11 #include "depthtest.h"
13 #include "framebuffer.h"
15 #include "pipelinestate.h"
16 #include "pipelinestate_backend.h"
20 #include "stenciltest.h"
22 #include "uniformblock.h"
23 #include "vertexsetup.h"
30 OpenGLPipelineState::~OpenGLPipelineState()
33 applied_to->get_state().last_pipeline = 0;
36 void OpenGLPipelineState::apply() const
38 const PipelineState &self = *static_cast<const PipelineState *>(this);
39 Device &device = Device::get_current();
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(changes&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(changes&(PipelineState::VIEWPORT|PipelineState::SCISSOR|PipelineState::FRAMEBUFFER))
70 if(const Framebuffer *framebuffer = self.framebuffer)
72 Rect fb_rect = framebuffer->get_rect();
73 if(changes&(PipelineState::VIEWPORT|PipelineState::FRAMEBUFFER))
75 Rect viewport = fb_rect.intersect(self.viewport);
76 glViewport(viewport.left, viewport.bottom, viewport.width, viewport.height);
78 if(changes&(PipelineState::SCISSOR|PipelineState::FRAMEBUFFER))
80 Rect scissor = fb_rect.intersect(self.scissor);
83 glEnable(GL_SCISSOR_TEST);
84 glScissor(scissor.left, scissor.bottom, scissor.width, scissor.height);
87 glDisable(GL_SCISSOR_TEST);
91 if(changes&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!=dev_state.n_clip_distances)
98 for(unsigned i=0; (i<ncd || i<dev_state.n_clip_distances); ++i)
101 glEnable(GL_CLIP_PLANE0+i);
103 glDisable(GL_CLIP_PLANE0+i);
105 dev_state.n_clip_distances = ncd;
109 if(changes&PipelineState::RESOURCES)
111 for(const PipelineState::BoundResource &r: self.resources)
113 if(!r.changed && changes!=~0U)
118 if(r.type==PipelineState::UNIFORM_BLOCK)
122 glBindBufferRange(GL_UNIFORM_BUFFER, r.binding, r.buffer->id, r.block->get_offset(), r.block->get_data_size());
123 dev_state.bound_uniform_blocks[r.binding] = 1;
125 else if(r.binding==ReflectData::DEFAULT_BLOCK && self.shprog)
127 const char *data = static_cast<const char *>(r.block->get_data_pointer());
128 for(const Program::UniformCall &call: self.shprog->uniform_calls)
129 call.func(call.location, call.size, data+call.location*16);
132 else if(r.type==PipelineState::SAMPLED_TEXTURE)
134 if(ARB_direct_state_access)
135 glBindTextureUnit(r.binding, r.texture->id);
138 glActiveTexture(GL_TEXTURE0+r.binding);
139 if(dev_state.bound_tex_targets[r.binding] && static_cast<int>(r.texture->target)!=dev_state.bound_tex_targets[r.binding])
140 glBindTexture(dev_state.bound_tex_targets[r.binding], 0);
141 glBindTexture(r.texture->target, r.texture->id);
144 dev_state.bound_tex_targets[r.binding] = r.texture->target;
146 glBindSampler(r.binding, r.sampler->id);
147 r.sampler->refresh();
149 else if(r.type==PipelineState::STORAGE_TEXTURE)
151 static Require _req(ARB_shader_image_load_store);
152 GLenum gl_format = get_gl_pixelformat(r.texture->get_format());
153 glBindImageTexture(r.binding, r.texture->id, 0, true, 0, GL_READ_WRITE, gl_format);
155 dev_state.bound_storage_textures[r.binding] = 1;
163 if(changes&PipelineState::VERTEX_SETUP)
165 const VertexSetup *vertex_setup = self.vertex_setup;
166 glBindVertexArray(vertex_setup ? vertex_setup->id : 0);
169 static Require _req(MSP_primitive_restart);
171 vertex_setup->refresh();
172 unsigned ri = (vertex_setup->get_index_type()==UNSIGNED_INT ? 0xFFFFFFFF : 0xFFFF);
173 if(ri!=dev_state.restart_index)
175 if(!dev_state.restart_index)
176 glEnable(GL_PRIMITIVE_RESTART);
177 glPrimitiveRestartIndex(ri);
178 dev_state.restart_index = ri;
183 if(changes&PipelineState::FACE_CULL)
185 glFrontFace(self.front_face==CLOCKWISE ? GL_CW : GL_CCW);
187 if(self.face_cull!=NO_CULL && self.front_face!=NON_MANIFOLD)
189 glEnable(GL_CULL_FACE);
190 glCullFace(self.face_cull==CULL_FRONT ? GL_FRONT : GL_BACK);
193 glDisable(GL_CULL_FACE);
196 if(changes&PipelineState::DEPTH_TEST)
198 const DepthTest &depth_test = self.depth_test;
199 if(depth_test.enabled)
201 glEnable(GL_DEPTH_TEST);
202 glDepthFunc(get_gl_predicate(depth_test.compare));
205 glDisable(GL_DEPTH_TEST);
207 glDepthMask(depth_test.write);
210 if(changes&PipelineState::STENCIL_TEST)
212 const StencilTest &stencil_test = self.stencil_test;
213 if(stencil_test.enabled)
215 glEnable(GL_STENCIL_TEST);
216 glStencilFunc(get_gl_predicate(stencil_test.compare), stencil_test.reference, 0xFFFFFFFF);
217 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));
220 glDisable(GL_STENCIL_TEST);
223 if(changes&PipelineState::BLEND)
225 const Blend &blend = self.blend;
229 glBlendEquation(get_gl_blend_equation(blend.equation));
230 glBlendFunc(get_gl_blend_factor(blend.src_factor), get_gl_blend_factor(blend.dst_factor));
231 glBlendColor(blend.constant.r, blend.constant.g, blend.constant.b, blend.constant.a);
232 ColorWriteMask cw = blend.write_mask;
233 glColorMask((cw&WRITE_RED)!=0, (cw&WRITE_GREEN)!=0, (cw&WRITE_BLUE)!=0, (cw&WRITE_ALPHA)!=0);
238 glColorMask(true, true, true, true);
241 if(blend.alpha_to_coverage && self.framebuffer && self.framebuffer->get_format().get_samples()>1)
242 glEnable(GL_SAMPLE_ALPHA_TO_COVERAGE);
244 glDisable(GL_SAMPLE_ALPHA_TO_COVERAGE);
247 applied_to = &device;
248 dev_state.last_pipeline = this;
252 void OpenGLPipelineState::clear()
254 OpenGLDeviceState &dev_state = Device::get_current().get_state();
255 if(dev_state.last_pipeline)
258 glBindVertexArray(0);
260 for(unsigned i=0; i<dev_state.n_clip_distances; ++i)
261 glDisable(GL_CLIP_PLANE0+i);
262 dev_state.n_clip_distances = 0;
264 for(unsigned i=0; i<dev_state.bound_tex_targets.size(); ++i)
265 if(dev_state.bound_tex_targets[i])
267 if(ARB_direct_state_access)
268 glBindTextureUnit(i, 0);
271 glActiveTexture(GL_TEXTURE0+i);
272 glBindTexture(dev_state.bound_tex_targets[i], 0);
274 dev_state.bound_tex_targets[i] = 0;
277 for(unsigned i=0; i<dev_state.bound_storage_textures.size(); ++i)
278 if(dev_state.bound_storage_textures[i])
280 glBindImageTexture(i, 0, 0, true, 0, GL_READ_ONLY, GL_RGBA8);
281 dev_state.bound_storage_textures[i] = 0;
284 for(unsigned i=0; i<dev_state.bound_uniform_blocks.size(); ++i)
285 if(dev_state.bound_uniform_blocks[i])
287 glBindBufferBase(GL_UNIFORM_BUFFER, i, 0);
288 dev_state.bound_uniform_blocks[i] = 0;
291 glDisable(GL_DEPTH_TEST);
293 glDisable(GL_STENCIL_TEST);
296 dev_state.last_pipeline->applied_to = 0;
297 dev_state.last_pipeline = 0;