]> git.tdb.fi Git - libs/gl.git/blobdiff - source/backends/opengl/pipelinestate_backend.cpp
Check the flat qualifier from the correct member
[libs/gl.git] / source / backends / opengl / pipelinestate_backend.cpp
index 08edc5e71a05d4a04a9932fbac0ff2de4f5e8144..da5f495712408926bde3f9445349b383009c819c 100644 (file)
@@ -1,8 +1,10 @@
 #include <msp/gl/extensions/arb_direct_state_access.h>
 #include <msp/gl/extensions/arb_sampler_objects.h>
+#include <msp/gl/extensions/arb_shader_image_load_store.h>
 #include <msp/gl/extensions/arb_shader_objects.h>
 #include <msp/gl/extensions/arb_uniform_buffer_object.h>
 #include <msp/gl/extensions/arb_vertex_array_object.h>
+#include <msp/gl/extensions/arb_tessellation_shader.h>
 #include <msp/gl/extensions/ext_framebuffer_object.h>
 #include <msp/gl/extensions/msp_primitive_restart.h>
 #include "blend.h"
@@ -36,12 +38,11 @@ void OpenGLPipelineState::apply() const
 {
        const PipelineState &self = *static_cast<const PipelineState *>(this);
        Device &device = Device::get_current();
-       unsigned mask = changes;
 
        if(applied_to && applied_to!=&device)
        {
                applied_to->get_state().last_pipeline = 0;
-               mask = ~0U;
+               changes = ~0U;
        }
 
        OpenGLDeviceState &dev_state = device.get_state();
@@ -52,10 +53,10 @@ void OpenGLPipelineState::apply() const
        {
                if(dev_state.last_pipeline)
                        dev_state.last_pipeline->applied_to = 0;
-               mask = ~0U;
+               changes = ~0U;
        }
 
-       if(mask&PipelineState::FRAMEBUFFER)
+       if(changes&PipelineState::FRAMEBUFFER)
        {
                const Framebuffer *framebuffer = self.framebuffer;
                glBindFramebuffer(GL_FRAMEBUFFER, framebuffer ? framebuffer->id : 0);
@@ -66,16 +67,16 @@ void OpenGLPipelineState::apply() const
                }
        }
 
-       if(mask&(PipelineState::VIEWPORT|PipelineState::SCISSOR|PipelineState::FRAMEBUFFER))
+       if(changes&(PipelineState::VIEWPORT|PipelineState::SCISSOR|PipelineState::FRAMEBUFFER))
                if(const Framebuffer *framebuffer = self.framebuffer)
                {
                        Rect fb_rect = framebuffer->get_rect();
-                       if(mask&(PipelineState::VIEWPORT|PipelineState::FRAMEBUFFER))
+                       if(changes&(PipelineState::VIEWPORT|PipelineState::FRAMEBUFFER))
                        {
                                Rect viewport = fb_rect.intersect(self.viewport);
                                glViewport(viewport.left, viewport.bottom, viewport.width, viewport.height);
                        }
-                       if(mask&(PipelineState::SCISSOR|PipelineState::FRAMEBUFFER))
+                       if(changes&(PipelineState::SCISSOR|PipelineState::FRAMEBUFFER))
                        {
                                Rect scissor = fb_rect.intersect(self.scissor);
                                if(scissor!=fb_rect)
@@ -88,7 +89,7 @@ void OpenGLPipelineState::apply() const
                        }
                }
 
-       if(mask&PipelineState::SHPROG)
+       if(changes&PipelineState::SHPROG)
        {
                glUseProgram(self.shprog ? self.shprog->id : 0);
 
@@ -106,58 +107,61 @@ void OpenGLPipelineState::apply() const
                }
        }
 
-       if(mask&PipelineState::UNIFORMS)
+       if(changes&PipelineState::RESOURCES)
        {
-               for(const PipelineState::BoundUniformBlock &u: self.uniform_blocks)
-                       if(u.changed || mask==~0U)
+               for(const PipelineState::BoundResource &r: self.resources)
+               {
+                       if(!r.changed && changes!=~0U)
+                               continue;
+
+                       if(r.used)
                        {
-                               if(u.used)
+                               if(r.type==PipelineState::UNIFORM_BLOCK)
                                {
-                                       if(u.binding>=0)
+                                       if(r.binding>=0)
                                        {
-                                               glBindBufferRange(GL_UNIFORM_BUFFER, u.binding, u.buffer->id, u.block->get_offset(), u.block->get_data_size());
-                                               dev_state.bound_uniform_blocks[u.binding] = 1;
+                                               glBindBufferRange(GL_UNIFORM_BUFFER, r.binding, r.buffer->id, r.block->get_offset(), r.block->get_data_size());
+                                               dev_state.bound_uniform_blocks[r.binding] = 1;
                                        }
-                                       else if(u.binding==ReflectData::DEFAULT_BLOCK && self.shprog)
+                                       else if(r.binding==ReflectData::DEFAULT_BLOCK && self.shprog)
                                        {
-                                               const char *data = static_cast<const char *>(u.block->get_data_pointer());
+                                               const char *data = static_cast<const char *>(r.block->get_data_pointer());
                                                for(const Program::UniformCall &call: self.shprog->uniform_calls)
                                                        call.func(call.location, call.size, data+call.location*16);
                                        }
                                }
-
-                               u.changed = false;
-                       }
-       }
-
-       if(mask&PipelineState::TEXTURES)
-       {
-               for(const PipelineState::BoundTexture &t: self.textures)
-                       if(t.changed || mask==~0U)
-                       {
-                               if(t.used)
+                               else if(r.type==PipelineState::SAMPLED_TEXTURE)
                                {
                                        if(ARB_direct_state_access)
-                                               glBindTextureUnit(t.binding, t.texture->id);
+                                               glBindTextureUnit(r.binding, r.texture->id);
                                        else
                                        {
-                                               glActiveTexture(GL_TEXTURE0+t.binding);
-                                               if(dev_state.bound_tex_targets[t.binding] && static_cast<int>(t.texture->target)!=dev_state.bound_tex_targets[t.binding])
-                                                       glBindTexture(dev_state.bound_tex_targets[t.binding], 0);
-                                               glBindTexture(t.texture->target, t.texture->id);
+                                               glActiveTexture(GL_TEXTURE0+r.binding);
+                                               if(dev_state.bound_tex_targets[r.binding] && static_cast<int>(r.texture->target)!=dev_state.bound_tex_targets[r.binding])
+                                                       glBindTexture(dev_state.bound_tex_targets[r.binding], 0);
+                                               glBindTexture(r.texture->target, r.texture->id);
                                        }
 
-                                       dev_state.bound_tex_targets[t.binding] = t.texture->target;
+                                       dev_state.bound_tex_targets[r.binding] = r.texture->target;
 
-                                       glBindSampler(t.binding, t.sampler->id);
-                                       t.sampler->refresh();
+                                       glBindSampler(r.binding, r.sampler->id);
+                                       r.sampler->refresh();
                                }
+                               else if(r.type==PipelineState::STORAGE_TEXTURE)
+                               {
+                                       static Require _req(ARB_shader_image_load_store);
+                                       GLenum gl_format = get_gl_pixelformat(r.texture->get_format());
+                                       glBindImageTexture(r.binding, r.texture->id, 0, true, 0, GL_READ_WRITE, gl_format);
 
-                               t.changed = false;
+                                       dev_state.bound_storage_textures[r.binding] = 1;
+                               }
                        }
+
+                       r.changed = false;
+               }
        }
 
-       if(mask&PipelineState::VERTEX_SETUP)
+       if(changes&PipelineState::VERTEX_SETUP)
        {
                const VertexSetup *vertex_setup = self.vertex_setup;
                glBindVertexArray(vertex_setup ? vertex_setup->id : 0);
@@ -177,7 +181,14 @@ void OpenGLPipelineState::apply() const
                }
        }
 
-       if(mask&PipelineState::FACE_CULL)
+       if(changes&PipelineState::PATCH_SIZE)
+               if(self.patch_size)
+               {
+                       static Require _req(ARB_tessellation_shader);
+                       glPatchParameteri(GL_PATCH_VERTICES, self.patch_size);
+               }
+
+       if(changes&PipelineState::FACE_CULL)
        {
                glFrontFace(self.front_face==CLOCKWISE ? GL_CW : GL_CCW);
 
@@ -190,7 +201,7 @@ void OpenGLPipelineState::apply() const
                        glDisable(GL_CULL_FACE);
        }
 
-       if(mask&PipelineState::DEPTH_TEST)
+       if(changes&PipelineState::DEPTH_TEST)
        {
                const DepthTest &depth_test = self.depth_test;
                if(depth_test.enabled)
@@ -204,7 +215,7 @@ void OpenGLPipelineState::apply() const
                glDepthMask(depth_test.write);
        }
 
-       if(mask&PipelineState::STENCIL_TEST)
+       if(changes&PipelineState::STENCIL_TEST)
        {
                const StencilTest &stencil_test = self.stencil_test;
                if(stencil_test.enabled)
@@ -217,7 +228,7 @@ void OpenGLPipelineState::apply() const
                        glDisable(GL_STENCIL_TEST);
        }
 
-       if(mask&PipelineState::BLEND)
+       if(changes&PipelineState::BLEND)
        {
                const Blend &blend = self.blend;
                if(blend.enabled)
@@ -234,8 +245,18 @@ void OpenGLPipelineState::apply() const
                        glDisable(GL_BLEND);
                        glColorMask(true, true, true, true);
                }
+
+               if(blend.alpha_to_coverage && self.framebuffer && self.framebuffer->get_format().get_samples()>1)
+                       glEnable(GL_SAMPLE_ALPHA_TO_COVERAGE);
+               else
+                       glDisable(GL_SAMPLE_ALPHA_TO_COVERAGE);
        }
 
+#ifdef DEBUG
+       if(changes&(PipelineState::SHPROG|PipelineState::RESOURCES))
+               self.check_bound_resources();
+#endif
+
        applied_to = &device;
        dev_state.last_pipeline = this;
        changes = 0;
@@ -266,6 +287,13 @@ void OpenGLPipelineState::clear()
                                dev_state.bound_tex_targets[i] = 0;
                        }
 
+               for(unsigned i=0; i<dev_state.bound_storage_textures.size(); ++i)
+                       if(dev_state.bound_storage_textures[i])
+                       {
+                               glBindImageTexture(i, 0, 0, true, 0, GL_READ_ONLY, GL_RGBA8);
+                               dev_state.bound_storage_textures[i] = 0;
+                       }
+
                for(unsigned i=0; i<dev_state.bound_uniform_blocks.size(); ++i)
                        if(dev_state.bound_uniform_blocks[i])
                        {