]> git.tdb.fi Git - libs/gl.git/blobdiff - source/backends/opengl/pipelinestate_backend.cpp
Don't try to apply push constants as uniforms in OpenGL
[libs/gl.git] / source / backends / opengl / pipelinestate_backend.cpp
index 738de3ac5fea34250f29659a09752e2208d497e7..19ee0f58f0a1788a27b46b1a326c41eafc133ca0 100644 (file)
@@ -8,7 +8,7 @@
 #include "blend.h"
 #include "buffer.h"
 #include "depthtest.h"
-#include "deviceinfo.h"
+#include "device.h"
 #include "framebuffer.h"
 #include "gl.h"
 #include "pipelinestate.h"
@@ -30,13 +30,14 @@ const OpenGLPipelineState *OpenGLPipelineState::last_applied = 0;
 vector<int> OpenGLPipelineState::bound_tex_targets;
 vector<char> OpenGLPipelineState::bound_uniform_blocks;
 unsigned OpenGLPipelineState::restart_index = 0;
+unsigned OpenGLPipelineState::n_clip_distances = 0;
 
 OpenGLPipelineState::OpenGLPipelineState()
 {
        if(bound_tex_targets.empty())
-               bound_tex_targets.resize(DeviceInfo::get_global().limits.max_texture_bindings);
+               bound_tex_targets.resize(Device::get_current().get_info().limits.max_texture_bindings);
        if(bound_uniform_blocks.empty())
-               bound_uniform_blocks.resize(DeviceInfo::get_global().limits.max_uniform_bindings);
+               bound_uniform_blocks.resize(Device::get_current().get_info().limits.max_uniform_bindings);
 }
 
 OpenGLPipelineState::~OpenGLPipelineState()
@@ -55,11 +56,11 @@ void OpenGLPipelineState::apply() const
 
 void OpenGLPipelineState::apply(unsigned mask) const
 {
-       const PipelineState *self = static_cast<const PipelineState *>(this);
+       const PipelineState &self = *static_cast<const PipelineState *>(this);
 
        if(mask&PipelineState::FRAMEBUFFER)
        {
-               const Framebuffer *framebuffer = self->framebuffer;
+               const Framebuffer *framebuffer = self.framebuffer;
                glBindFramebuffer(GL_FRAMEBUFFER, framebuffer ? framebuffer->id : 0);
                if(framebuffer)
                {
@@ -70,15 +71,15 @@ void OpenGLPipelineState::apply(unsigned mask) const
 
        if(mask&(PipelineState::VIEWPORT|PipelineState::FRAMEBUFFER))
        {
-               if(const Rect *viewport = self->viewport)
+               if(const Rect *viewport = self.viewport)
                        glViewport(viewport->left, viewport->bottom, viewport->width, viewport->height);
-               else if(const Framebuffer *framebuffer = self->framebuffer)
+               else if(const Framebuffer *framebuffer = self.framebuffer)
                        glViewport(0, 0, framebuffer->get_width(), framebuffer->get_height());
        }
 
        if(mask&PipelineState::SCISSOR)
        {
-               if(const Rect *scissor = self->scissor)
+               if(const Rect *scissor = self.scissor)
                {
                        glEnable(GL_SCISSOR_TEST);
                        glScissor(scissor->left, scissor->bottom, scissor->width, scissor->height);
@@ -88,56 +89,50 @@ void OpenGLPipelineState::apply(unsigned mask) const
        }
 
        if(mask&PipelineState::SHPROG)
-               glUseProgram(self->shprog ? self->shprog->id : 0);
-
-       if(mask&PipelineState::VERTEX_SETUP)
        {
-               const VertexSetup *vertex_setup = self->vertex_setup;
-               glBindVertexArray(vertex_setup ? vertex_setup->id : 0);
-               if(vertex_setup)
-               {
-                       static Require _req(MSP_primitive_restart);
+               glUseProgram(self.shprog ? self.shprog->id : 0);
 
-                       vertex_setup->refresh();
-                       unsigned ri = (vertex_setup->get_index_type()==UNSIGNED_INT ? 0xFFFFFFFF : 0xFFFF);
-                       if(ri!=restart_index)
+               unsigned ncd = (self.shprog ? self.shprog->get_n_clip_distances() : 0);
+               if(ncd!=n_clip_distances)
+               {
+                       for(unsigned i=0; (i<ncd || i<n_clip_distances); ++i)
                        {
-                               if(!restart_index)
-                                       glEnable(GL_PRIMITIVE_RESTART);
-                               glPrimitiveRestartIndex(ri);
-                               restart_index = ri;
+                               if(i<ncd)
+                                       glEnable(GL_CLIP_PLANE0+i);
+                               else
+                                       glDisable(GL_CLIP_PLANE0+i);
                        }
+                       n_clip_distances = ncd;
                }
        }
 
-       if(mask&PipelineState::FACE_CULL)
+       if(mask&PipelineState::UNIFORMS)
        {
-               glFrontFace(self->front_face==CLOCKWISE ? GL_CW : GL_CCW);
-
-               if(self->face_cull!=NO_CULL && self->front_face!=NON_MANIFOLD)
-               {
-                       glEnable(GL_CULL_FACE);
-                       glCullFace(self->face_cull==CULL_FRONT ? GL_FRONT : GL_BACK);
-               }
-               else
-                       glDisable(GL_CULL_FACE);
-       }
+               for(const PipelineState::BoundUniformBlock &u: self.uniform_blocks)
+                       if(u.changed || mask==~0U)
+                       {
+                               if(u.block)
+                               {
+                                       if(u.binding>=0)
+                                       {
+                                               glBindBufferRange(GL_UNIFORM_BUFFER, u.binding, u.block->get_buffer()->id, u.block->get_offset(), u.block->get_data_size());
+                                               bound_uniform_blocks[u.binding] = 1;
+                                       }
+                                       else if(u.binding==ReflectData::DEFAULT_BLOCK && self.shprog)
+                                       {
+                                               const char *data = static_cast<const char *>(u.block->get_data_pointer());
+                                               for(const Program::UniformCall &call: self.shprog->uniform_calls)
+                                                       call.func(call.location, call.size, data+call.location*16);
+                                       }
+                               }
 
-       if(mask&PipelineState::CLIP_PLANES)
-       {
-               unsigned max_clip_planes = DeviceInfo::get_global().limits.max_clip_planes;
-               for(unsigned i=0; i<max_clip_planes; ++i)
-               {
-                       if((self->enabled_clip_planes>>i)&1)
-                               glEnable(GL_CLIP_PLANE0+i);
-                       else
-                               glDisable(GL_CLIP_PLANE0+i);
-               }
+                               u.changed = false;
+                       }
        }
 
        if(mask&PipelineState::TEXTURES)
        {
-               for(const PipelineState::BoundTexture &t: self->textures)
+               for(const PipelineState::BoundTexture &t: self.textures)
                        if(t.changed || mask==~0U)
                        {
                                if(t.texture && t.sampler)
@@ -162,33 +157,42 @@ void OpenGLPipelineState::apply(unsigned mask) const
                        }
        }
 
-       if(mask&PipelineState::UNIFORMS)
+       if(mask&PipelineState::VERTEX_SETUP)
        {
-               for(const PipelineState::BoundUniformBlock &u: self->uniform_blocks)
-                       if(u.changed || mask==~0U)
-                       {
-                               if(u.block)
-                               {
-                                       if(u.binding>=0)
-                                       {
-                                               glBindBufferRange(GL_UNIFORM_BUFFER, u.binding, u.block->get_buffer()->id, u.block->get_offset(), u.block->get_data_size());
-                                               bound_uniform_blocks[u.binding] = 1;
-                                       }
-                                       else if(self->shprog)
-                                       {
-                                               const char *data = static_cast<const char *>(u.block->get_data_pointer());
-                                               for(const Program::UniformCall &call: self->shprog->uniform_calls)
-                                                       call.func(call.location, call.size, data+call.location*16);
-                                       }
-                               }
+               const VertexSetup *vertex_setup = self.vertex_setup;
+               glBindVertexArray(vertex_setup ? vertex_setup->id : 0);
+               if(vertex_setup)
+               {
+                       static Require _req(MSP_primitive_restart);
 
-                               u.changed = false;
+                       vertex_setup->refresh();
+                       unsigned ri = (vertex_setup->get_index_type()==UNSIGNED_INT ? 0xFFFFFFFF : 0xFFFF);
+                       if(ri!=restart_index)
+                       {
+                               if(!restart_index)
+                                       glEnable(GL_PRIMITIVE_RESTART);
+                               glPrimitiveRestartIndex(ri);
+                               restart_index = ri;
                        }
+               }
+       }
+
+       if(mask&PipelineState::FACE_CULL)
+       {
+               glFrontFace(self.front_face==CLOCKWISE ? GL_CW : GL_CCW);
+
+               if(self.face_cull!=NO_CULL && self.front_face!=NON_MANIFOLD)
+               {
+                       glEnable(GL_CULL_FACE);
+                       glCullFace(self.face_cull==CULL_FRONT ? GL_FRONT : GL_BACK);
+               }
+               else
+                       glDisable(GL_CULL_FACE);
        }
 
        if(mask&PipelineState::DEPTH_TEST)
        {
-               const DepthTest *depth_test = self->depth_test;
+               const DepthTest *depth_test = self.depth_test;
                if(depth_test && depth_test->enabled)
                {
                        glEnable(GL_DEPTH_TEST);
@@ -202,7 +206,7 @@ void OpenGLPipelineState::apply(unsigned mask) const
 
        if(mask&PipelineState::STENCIL_TEST)
        {
-               const StencilTest *stencil_test = self->stencil_test;
+               const StencilTest *stencil_test = self.stencil_test;
                if(stencil_test && stencil_test->enabled)
                {
                        glEnable(GL_STENCIL_TEST);
@@ -215,7 +219,7 @@ void OpenGLPipelineState::apply(unsigned mask) const
 
        if(mask&PipelineState::BLEND)
        {
-               const Blend *blend = self->blend;
+               const Blend *blend = self.blend;
                if(blend && blend->enabled)
                {
                        glEnable(GL_BLEND);
@@ -233,7 +237,7 @@ void OpenGLPipelineState::apply(unsigned mask) const
        }
 
        last_applied = this;
-       self->changes &= ~mask;
+       self.changes &= ~mask;
 }
 
 void OpenGLPipelineState::clear()
@@ -243,11 +247,9 @@ void OpenGLPipelineState::clear()
                glUseProgram(0);
                glBindVertexArray(0);
 
-               unsigned max_clip_planes = DeviceInfo::get_global().limits.max_clip_planes;
-               unsigned enabled_clip_planes = static_cast<const PipelineState *>(last_applied)->enabled_clip_planes;
-               for(unsigned i=0; i<max_clip_planes; ++i)
-                       if((enabled_clip_planes>>i)&1)
-                               glDisable(GL_CLIP_PLANE0+i);
+               for(unsigned i=0; i<n_clip_distances; ++i)
+                       glDisable(GL_CLIP_PLANE0+i);
+               n_clip_distances = 0;
 
                for(unsigned i=0; i<bound_tex_targets.size(); ++i)
                        if(bound_tex_targets[i])