From faed96b4cfc60a3d5d2e1fde3d143eb5363439da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Wed, 21 Apr 2021 12:47:10 +0200 Subject: [PATCH] Shaders: avoid repeated GL::Context::current() calls. It involves a TLS lookup each time and it's just unnecessary extra typing. --- src/Magnum/Shaders/DistanceFieldVectorGL.cpp | 12 ++++---- src/Magnum/Shaders/FlatGL.cpp | 12 ++++---- src/Magnum/Shaders/MeshVisualizerGL.cpp | 30 +++++++++++++------- src/Magnum/Shaders/PhongGL.cpp | 12 ++++---- src/Magnum/Shaders/VectorGL.cpp | 12 ++++---- src/Magnum/Shaders/VertexColorGL.cpp | 10 ++++--- 6 files changed, 54 insertions(+), 34 deletions(-) diff --git a/src/Magnum/Shaders/DistanceFieldVectorGL.cpp b/src/Magnum/Shaders/DistanceFieldVectorGL.cpp index 32c8e8795..1ce614bc3 100644 --- a/src/Magnum/Shaders/DistanceFieldVectorGL.cpp +++ b/src/Magnum/Shaders/DistanceFieldVectorGL.cpp @@ -48,10 +48,12 @@ template DistanceFieldVectorGL::DistanceFiel #endif Utility::Resource rs("MagnumShadersGL"); + const GL::Context& context = GL::Context::current(); + #ifndef MAGNUM_TARGET_GLES - const GL::Version version = GL::Context::current().supportedVersion({GL::Version::GL320, GL::Version::GL310, GL::Version::GL300, GL::Version::GL210}); + const GL::Version version = context.supportedVersion({GL::Version::GL320, GL::Version::GL310, GL::Version::GL300, GL::Version::GL210}); #else - const GL::Version version = GL::Context::current().supportedVersion({GL::Version::GLES300, GL::Version::GLES200}); + const GL::Version version = context.supportedVersion({GL::Version::GLES300, GL::Version::GLES200}); #endif GL::Shader vert = Implementation::createCompatibilityShader(rs, version, GL::Shader::Type::Vertex); @@ -71,7 +73,7 @@ template DistanceFieldVectorGL::DistanceFiel /* ES3 has this done in the shader directly */ #if !defined(MAGNUM_TARGET_GLES) || defined(MAGNUM_TARGET_GLES2) #ifndef MAGNUM_TARGET_GLES - if(!GL::Context::current().isExtensionSupported(version)) + if(!context.isExtensionSupported(version)) #endif { GL::AbstractShaderProgram::bindAttributeLocation(AbstractVectorGL::Position::Location, "position"); @@ -82,7 +84,7 @@ template DistanceFieldVectorGL::DistanceFiel CORRADE_INTERNAL_ASSERT_OUTPUT(GL::AbstractShaderProgram::link()); #ifndef MAGNUM_TARGET_GLES - if(!GL::Context::current().isExtensionSupported(version)) + if(!context.isExtensionSupported(version)) #endif { _transformationProjectionMatrixUniform = GL::AbstractShaderProgram::uniformLocation("transformationProjectionMatrix"); @@ -95,7 +97,7 @@ template DistanceFieldVectorGL::DistanceFiel } #ifndef MAGNUM_TARGET_GLES - if(!GL::Context::current().isExtensionSupported(version)) + if(!context.isExtensionSupported(version)) #endif { GL::AbstractShaderProgram::setUniform(GL::AbstractShaderProgram::uniformLocation("vectorTexture"), diff --git a/src/Magnum/Shaders/FlatGL.cpp b/src/Magnum/Shaders/FlatGL.cpp index bfd7b355a..cb054eaf9 100644 --- a/src/Magnum/Shaders/FlatGL.cpp +++ b/src/Magnum/Shaders/FlatGL.cpp @@ -56,10 +56,12 @@ template FlatGL::FlatGL(const Flags flags): #endif Utility::Resource rs("MagnumShadersGL"); + const GL::Context& context = GL::Context::current(); + #ifndef MAGNUM_TARGET_GLES - const GL::Version version = GL::Context::current().supportedVersion({GL::Version::GL320, GL::Version::GL310, GL::Version::GL300, GL::Version::GL210}); + const GL::Version version = context.supportedVersion({GL::Version::GL320, GL::Version::GL310, GL::Version::GL300, GL::Version::GL210}); #else - const GL::Version version = GL::Context::current().supportedVersion({GL::Version::GLES300, GL::Version::GLES200}); + const GL::Version version = context.supportedVersion({GL::Version::GLES300, GL::Version::GLES200}); #endif GL::Shader vert = Implementation::createCompatibilityShader(rs, version, GL::Shader::Type::Vertex); @@ -94,7 +96,7 @@ template FlatGL::FlatGL(const Flags flags): bindFragmentDataLocation() */ #if !defined(MAGNUM_TARGET_GLES) || defined(MAGNUM_TARGET_GLES2) #ifndef MAGNUM_TARGET_GLES - if(!GL::Context::current().isExtensionSupported(version)) + if(!context.isExtensionSupported(version)) #endif { bindAttributeLocation(Position::Location, "position"); @@ -120,7 +122,7 @@ template FlatGL::FlatGL(const Flags flags): CORRADE_INTERNAL_ASSERT_OUTPUT(link()); #ifndef MAGNUM_TARGET_GLES - if(!GL::Context::current().isExtensionSupported(version)) + if(!context.isExtensionSupported(version)) #endif { _transformationProjectionMatrixUniform = uniformLocation("transformationProjectionMatrix"); @@ -134,7 +136,7 @@ template FlatGL::FlatGL(const Flags flags): } #ifndef MAGNUM_TARGET_GLES - if(!GL::Context::current().isExtensionSupported(version)) + if(!context.isExtensionSupported(version)) #endif { if(flags & Flag::Textured) setUniform(uniformLocation("textureData"), TextureUnit); diff --git a/src/Magnum/Shaders/MeshVisualizerGL.cpp b/src/Magnum/Shaders/MeshVisualizerGL.cpp index 2319626f5..ef34527af 100644 --- a/src/Magnum/Shaders/MeshVisualizerGL.cpp +++ b/src/Magnum/Shaders/MeshVisualizerGL.cpp @@ -96,16 +96,18 @@ MeshVisualizerGLBase::MeshVisualizerGLBase(FlagsBase flags): _flags{flags} { } GL::Version MeshVisualizerGLBase::setupShaders(GL::Shader& vert, GL::Shader& frag, const Utility::Resource& rs) const { + GL::Context& context = GL::Context::current(); + #ifndef MAGNUM_TARGET_GLES - const GL::Version version = GL::Context::current().supportedVersion({GL::Version::GL320, GL::Version::GL310, GL::Version::GL300, GL::Version::GL210}); + const GL::Version version = context.supportedVersion({GL::Version::GL320, GL::Version::GL310, GL::Version::GL300, GL::Version::GL210}); /* Extended in MeshVisualizer3D for TBN visualization */ CORRADE_INTERNAL_ASSERT(!(_flags & FlagBase::Wireframe) || _flags & FlagBase::NoGeometryShader || version >= GL::Version::GL320); #elif !defined(MAGNUM_TARGET_WEBGL) - const GL::Version version = GL::Context::current().supportedVersion({GL::Version::GLES310, GL::Version::GLES300, GL::Version::GLES200}); + const GL::Version version = context.supportedVersion({GL::Version::GLES310, GL::Version::GLES300, GL::Version::GLES200}); /* Extended in MeshVisualizer3D for TBN visualization */ CORRADE_INTERNAL_ASSERT(!(_flags & FlagBase::Wireframe) || _flags & FlagBase::NoGeometryShader || version >= GL::Version::GLES310); #else - const GL::Version version = GL::Context::current().supportedVersion({GL::Version::GLES300, GL::Version::GLES200}); + const GL::Version version = context.supportedVersion({GL::Version::GLES300, GL::Version::GLES200}); #endif vert = Implementation::createCompatibilityShader(rs, version, GL::Shader::Type::Vertex); @@ -120,7 +122,7 @@ GL::Version MeshVisualizerGLBase::setupShaders(GL::Shader& vert, GL::Shader& fra #ifdef MAGNUM_TARGET_WEBGL .addSource("#define SUBSCRIPTING_WORKAROUND\n") #elif defined(MAGNUM_TARGET_GLES2) - .addSource(GL::Context::current().detectedDriver() & GL::Context::DetectedDriver::Angle ? + .addSource(context.detectedDriver() & GL::Context::DetectedDriver::Angle ? "#define SUBSCRIPTING_WORKAROUND\n" : "") #endif ; @@ -191,6 +193,10 @@ MeshVisualizerGL2D::MeshVisualizerGL2D(const Flags flags): Implementation::MeshV "Shaders::MeshVisualizerGL2D: at least Flag::Wireframe has to be enabled", ); #endif + #ifndef MAGNUM_TARGET_GLES + const GL::Context& context = GL::Context::current(); + #endif + Utility::Resource rs{"MagnumShadersGL"}; GL::Shader vert{NoCreate}; GL::Shader frag{NoCreate}; @@ -245,7 +251,7 @@ MeshVisualizerGL2D::MeshVisualizerGL2D(const Flags flags): Implementation::MeshV /* ES3 has this done in the shader directly */ #if !defined(MAGNUM_TARGET_GLES) || defined(MAGNUM_TARGET_GLES2) #ifndef MAGNUM_TARGET_GLES - if(!GL::Context::current().isExtensionSupported(version)) + if(!context.isExtensionSupported(version)) #endif { bindAttributeLocation(Position::Location, "position"); @@ -255,7 +261,7 @@ MeshVisualizerGL2D::MeshVisualizerGL2D(const Flags flags): Implementation::MeshV #endif #if !defined(MAGNUM_TARGET_GLES) || defined(MAGNUM_TARGET_GLES2) #ifndef MAGNUM_TARGET_GLES - if(!GL::Context::current().isVersionSupported(GL::Version::GL310)) + if(!context.isVersionSupported(GL::Version::GL310)) #endif { bindAttributeLocation(VertexIndex::Location, "vertexIndex"); @@ -267,7 +273,7 @@ MeshVisualizerGL2D::MeshVisualizerGL2D(const Flags flags): Implementation::MeshV CORRADE_INTERNAL_ASSERT_OUTPUT(link()); #ifndef MAGNUM_TARGET_GLES - if(!GL::Context::current().isExtensionSupported(version)) + if(!context.isExtensionSupported(version)) #endif { _transformationProjectionMatrixUniform = uniformLocation("transformationProjectionMatrix"); @@ -352,6 +358,10 @@ MeshVisualizerGL3D::MeshVisualizerGL3D(const Flags flags): Implementation::MeshV "Shaders::MeshVisualizerGL3D: at least Flag::Wireframe has to be enabled", ); #endif + #ifndef MAGNUM_TARGET_GLES + const GL::Context& context = GL::Context::current(); + #endif + Utility::Resource rs{"MagnumShadersGL"}; GL::Shader vert{NoCreate}; GL::Shader frag{NoCreate}; @@ -439,7 +449,7 @@ MeshVisualizerGL3D::MeshVisualizerGL3D(const Flags flags): Implementation::MeshV /* ES3 has this done in the shader directly */ #if !defined(MAGNUM_TARGET_GLES) || defined(MAGNUM_TARGET_GLES2) #ifndef MAGNUM_TARGET_GLES - if(!GL::Context::current().isExtensionSupported(version)) + if(!context.isExtensionSupported(version)) #endif { bindAttributeLocation(Position::Location, "position"); @@ -460,7 +470,7 @@ MeshVisualizerGL3D::MeshVisualizerGL3D(const Flags flags): Implementation::MeshV #if !defined(MAGNUM_TARGET_GLES) || defined(MAGNUM_TARGET_GLES2) #ifndef MAGNUM_TARGET_GLES - if(!GL::Context::current().isVersionSupported(GL::Version::GL310)) + if(!context.isVersionSupported(GL::Version::GL310)) #endif { bindAttributeLocation(VertexIndex::Location, "vertexIndex"); @@ -472,7 +482,7 @@ MeshVisualizerGL3D::MeshVisualizerGL3D(const Flags flags): Implementation::MeshV CORRADE_INTERNAL_ASSERT_OUTPUT(link()); #ifndef MAGNUM_TARGET_GLES - if(!GL::Context::current().isExtensionSupported(version)) + if(!context.isExtensionSupported(version)) #endif { _transformationMatrixUniform = uniformLocation("transformationMatrix"); diff --git a/src/Magnum/Shaders/PhongGL.cpp b/src/Magnum/Shaders/PhongGL.cpp index ba0d0b308..750c6c2ad 100644 --- a/src/Magnum/Shaders/PhongGL.cpp +++ b/src/Magnum/Shaders/PhongGL.cpp @@ -67,10 +67,12 @@ PhongGL::PhongGL(const Flags flags, const UnsignedInt lightCount): _flags{flags} #endif Utility::Resource rs("MagnumShadersGL"); + const GL::Context& context = GL::Context::current(); + #ifndef MAGNUM_TARGET_GLES - const GL::Version version = GL::Context::current().supportedVersion({GL::Version::GL320, GL::Version::GL310, GL::Version::GL300, GL::Version::GL210}); + const GL::Version version = context.supportedVersion({GL::Version::GL320, GL::Version::GL310, GL::Version::GL300, GL::Version::GL210}); #else - const GL::Version version = GL::Context::current().supportedVersion({GL::Version::GLES300, GL::Version::GLES200}); + const GL::Version version = context.supportedVersion({GL::Version::GLES300, GL::Version::GLES200}); #endif GL::Shader vert = Implementation::createCompatibilityShader(rs, version, GL::Shader::Type::Vertex); @@ -177,7 +179,7 @@ PhongGL::PhongGL(const Flags flags, const UnsignedInt lightCount): _flags{flags} bindFragmentDataLocation() */ #if !defined(MAGNUM_TARGET_GLES) || defined(MAGNUM_TARGET_GLES2) #ifndef MAGNUM_TARGET_GLES - if(!GL::Context::current().isExtensionSupported(version)) + if(!context.isExtensionSupported(version)) #endif { bindAttributeLocation(Position::Location, "position"); @@ -210,7 +212,7 @@ PhongGL::PhongGL(const Flags flags, const UnsignedInt lightCount): _flags{flags} CORRADE_INTERNAL_ASSERT_OUTPUT(link()); #ifndef MAGNUM_TARGET_GLES - if(!GL::Context::current().isExtensionSupported(version)) + if(!context.isExtensionSupported(version)) #endif { _transformationMatrixUniform = uniformLocation("transformationMatrix"); @@ -237,7 +239,7 @@ PhongGL::PhongGL(const Flags flags, const UnsignedInt lightCount): _flags{flags} } #ifndef MAGNUM_TARGET_GLES - if(flags && !GL::Context::current().isExtensionSupported(version)) + if(flags && !context.isExtensionSupported(version)) #endif { if(flags & Flag::AmbientTexture) setUniform(uniformLocation("ambientTexture"), AmbientTextureUnit); diff --git a/src/Magnum/Shaders/VectorGL.cpp b/src/Magnum/Shaders/VectorGL.cpp index d29cf4a1f..db401d3f4 100644 --- a/src/Magnum/Shaders/VectorGL.cpp +++ b/src/Magnum/Shaders/VectorGL.cpp @@ -48,10 +48,12 @@ template VectorGL::VectorGL(const Flags flag #endif Utility::Resource rs("MagnumShadersGL"); + const GL::Context& context = GL::Context::current(); + #ifndef MAGNUM_TARGET_GLES - const GL::Version version = GL::Context::current().supportedVersion({GL::Version::GL320, GL::Version::GL310, GL::Version::GL300, GL::Version::GL210}); + const GL::Version version = context.supportedVersion({GL::Version::GL320, GL::Version::GL310, GL::Version::GL300, GL::Version::GL210}); #else - const GL::Version version = GL::Context::current().supportedVersion({GL::Version::GLES300, GL::Version::GLES200}); + const GL::Version version = context.supportedVersion({GL::Version::GLES300, GL::Version::GLES200}); #endif GL::Shader vert = Implementation::createCompatibilityShader(rs, version, GL::Shader::Type::Vertex); @@ -71,7 +73,7 @@ template VectorGL::VectorGL(const Flags flag /* ES3 has this done in the shader directly */ #if !defined(MAGNUM_TARGET_GLES) || defined(MAGNUM_TARGET_GLES2) #ifndef MAGNUM_TARGET_GLES - if(!GL::Context::current().isExtensionSupported(version)) + if(!context.isExtensionSupported(version)) #endif { GL::AbstractShaderProgram::bindAttributeLocation(AbstractVectorGL::Position::Location, "position"); @@ -82,7 +84,7 @@ template VectorGL::VectorGL(const Flags flag CORRADE_INTERNAL_ASSERT_OUTPUT(GL::AbstractShaderProgram::link()); #ifndef MAGNUM_TARGET_GLES - if(!GL::Context::current().isExtensionSupported(version)) + if(!context.isExtensionSupported(version)) #endif { _transformationProjectionMatrixUniform = GL::AbstractShaderProgram::uniformLocation("transformationProjectionMatrix"); @@ -93,7 +95,7 @@ template VectorGL::VectorGL(const Flags flag } #ifndef MAGNUM_TARGET_GLES - if(!GL::Context::current().isExtensionSupported(version)) + if(!context.isExtensionSupported(version)) #endif { GL::AbstractShaderProgram::setUniform(GL::AbstractShaderProgram::uniformLocation("vectorTexture"), AbstractVectorGL::VectorTextureUnit); diff --git a/src/Magnum/Shaders/VertexColorGL.cpp b/src/Magnum/Shaders/VertexColorGL.cpp index 311ae603d..66ef23fc1 100644 --- a/src/Magnum/Shaders/VertexColorGL.cpp +++ b/src/Magnum/Shaders/VertexColorGL.cpp @@ -47,10 +47,12 @@ template VertexColorGL::VertexColorGL() { #endif Utility::Resource rs("MagnumShadersGL"); + const GL::Context& context = GL::Context::current(); + #ifndef MAGNUM_TARGET_GLES - const GL::Version version = GL::Context::current().supportedVersion({GL::Version::GL320, GL::Version::GL310, GL::Version::GL300, GL::Version::GL210}); + const GL::Version version = context.supportedVersion({GL::Version::GL320, GL::Version::GL310, GL::Version::GL300, GL::Version::GL210}); #else - const GL::Version version = GL::Context::current().supportedVersion({GL::Version::GLES300, GL::Version::GLES200}); + const GL::Version version = context.supportedVersion({GL::Version::GLES300, GL::Version::GLES200}); #endif GL::Shader vert = Implementation::createCompatibilityShader(rs, version, GL::Shader::Type::Vertex); @@ -69,7 +71,7 @@ template VertexColorGL::VertexColorGL() { /* ES3 has this done in the shader directly */ #if !defined(MAGNUM_TARGET_GLES) || defined(MAGNUM_TARGET_GLES2) #ifndef MAGNUM_TARGET_GLES - if(!GL::Context::current().isExtensionSupported(version)) + if(!context.isExtensionSupported(version)) #endif { bindAttributeLocation(Position::Location, "position"); @@ -80,7 +82,7 @@ template VertexColorGL::VertexColorGL() { CORRADE_INTERNAL_ASSERT_OUTPUT(link()); #ifndef MAGNUM_TARGET_GLES - if(!GL::Context::current().isExtensionSupported(version)) + if(!context.isExtensionSupported(version)) #endif { _transformationProjectionMatrixUniform = uniformLocation("transformationProjectionMatrix");