Browse Source

Shaders: avoid repeated GL::Context::current() calls.

It involves a TLS lookup each time and it's just unnecessary extra
typing.
pull/518/head
Vladimír Vondruš 5 years ago
parent
commit
faed96b4cf
  1. 12
      src/Magnum/Shaders/DistanceFieldVectorGL.cpp
  2. 12
      src/Magnum/Shaders/FlatGL.cpp
  3. 30
      src/Magnum/Shaders/MeshVisualizerGL.cpp
  4. 12
      src/Magnum/Shaders/PhongGL.cpp
  5. 12
      src/Magnum/Shaders/VectorGL.cpp
  6. 10
      src/Magnum/Shaders/VertexColorGL.cpp

12
src/Magnum/Shaders/DistanceFieldVectorGL.cpp

@ -48,10 +48,12 @@ template<UnsignedInt dimensions> DistanceFieldVectorGL<dimensions>::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<UnsignedInt dimensions> DistanceFieldVectorGL<dimensions>::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<GL::Extensions::ARB::explicit_attrib_location>(version))
if(!context.isExtensionSupported<GL::Extensions::ARB::explicit_attrib_location>(version))
#endif
{
GL::AbstractShaderProgram::bindAttributeLocation(AbstractVectorGL<dimensions>::Position::Location, "position");
@ -82,7 +84,7 @@ template<UnsignedInt dimensions> DistanceFieldVectorGL<dimensions>::DistanceFiel
CORRADE_INTERNAL_ASSERT_OUTPUT(GL::AbstractShaderProgram::link());
#ifndef MAGNUM_TARGET_GLES
if(!GL::Context::current().isExtensionSupported<GL::Extensions::ARB::explicit_uniform_location>(version))
if(!context.isExtensionSupported<GL::Extensions::ARB::explicit_uniform_location>(version))
#endif
{
_transformationProjectionMatrixUniform = GL::AbstractShaderProgram::uniformLocation("transformationProjectionMatrix");
@ -95,7 +97,7 @@ template<UnsignedInt dimensions> DistanceFieldVectorGL<dimensions>::DistanceFiel
}
#ifndef MAGNUM_TARGET_GLES
if(!GL::Context::current().isExtensionSupported<GL::Extensions::ARB::shading_language_420pack>(version))
if(!context.isExtensionSupported<GL::Extensions::ARB::shading_language_420pack>(version))
#endif
{
GL::AbstractShaderProgram::setUniform(GL::AbstractShaderProgram::uniformLocation("vectorTexture"),

12
src/Magnum/Shaders/FlatGL.cpp

@ -56,10 +56,12 @@ template<UnsignedInt dimensions> FlatGL<dimensions>::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<UnsignedInt dimensions> FlatGL<dimensions>::FlatGL(const Flags flags):
bindFragmentDataLocation() */
#if !defined(MAGNUM_TARGET_GLES) || defined(MAGNUM_TARGET_GLES2)
#ifndef MAGNUM_TARGET_GLES
if(!GL::Context::current().isExtensionSupported<GL::Extensions::ARB::explicit_attrib_location>(version))
if(!context.isExtensionSupported<GL::Extensions::ARB::explicit_attrib_location>(version))
#endif
{
bindAttributeLocation(Position::Location, "position");
@ -120,7 +122,7 @@ template<UnsignedInt dimensions> FlatGL<dimensions>::FlatGL(const Flags flags):
CORRADE_INTERNAL_ASSERT_OUTPUT(link());
#ifndef MAGNUM_TARGET_GLES
if(!GL::Context::current().isExtensionSupported<GL::Extensions::ARB::explicit_uniform_location>(version))
if(!context.isExtensionSupported<GL::Extensions::ARB::explicit_uniform_location>(version))
#endif
{
_transformationProjectionMatrixUniform = uniformLocation("transformationProjectionMatrix");
@ -134,7 +136,7 @@ template<UnsignedInt dimensions> FlatGL<dimensions>::FlatGL(const Flags flags):
}
#ifndef MAGNUM_TARGET_GLES
if(!GL::Context::current().isExtensionSupported<GL::Extensions::ARB::shading_language_420pack>(version))
if(!context.isExtensionSupported<GL::Extensions::ARB::shading_language_420pack>(version))
#endif
{
if(flags & Flag::Textured) setUniform(uniformLocation("textureData"), TextureUnit);

30
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<GL::Extensions::ARB::explicit_attrib_location>(version))
if(!context.isExtensionSupported<GL::Extensions::ARB::explicit_attrib_location>(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<GL::Extensions::ARB::explicit_uniform_location>(version))
if(!context.isExtensionSupported<GL::Extensions::ARB::explicit_uniform_location>(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<GL::Extensions::ARB::explicit_attrib_location>(version))
if(!context.isExtensionSupported<GL::Extensions::ARB::explicit_attrib_location>(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<GL::Extensions::ARB::explicit_uniform_location>(version))
if(!context.isExtensionSupported<GL::Extensions::ARB::explicit_uniform_location>(version))
#endif
{
_transformationMatrixUniform = uniformLocation("transformationMatrix");

12
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<GL::Extensions::ARB::explicit_attrib_location>(version))
if(!context.isExtensionSupported<GL::Extensions::ARB::explicit_attrib_location>(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<GL::Extensions::ARB::explicit_uniform_location>(version))
if(!context.isExtensionSupported<GL::Extensions::ARB::explicit_uniform_location>(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<GL::Extensions::ARB::shading_language_420pack>(version))
if(flags && !context.isExtensionSupported<GL::Extensions::ARB::shading_language_420pack>(version))
#endif
{
if(flags & Flag::AmbientTexture) setUniform(uniformLocation("ambientTexture"), AmbientTextureUnit);

12
src/Magnum/Shaders/VectorGL.cpp

@ -48,10 +48,12 @@ template<UnsignedInt dimensions> VectorGL<dimensions>::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<UnsignedInt dimensions> VectorGL<dimensions>::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<GL::Extensions::ARB::explicit_attrib_location>(version))
if(!context.isExtensionSupported<GL::Extensions::ARB::explicit_attrib_location>(version))
#endif
{
GL::AbstractShaderProgram::bindAttributeLocation(AbstractVectorGL<dimensions>::Position::Location, "position");
@ -82,7 +84,7 @@ template<UnsignedInt dimensions> VectorGL<dimensions>::VectorGL(const Flags flag
CORRADE_INTERNAL_ASSERT_OUTPUT(GL::AbstractShaderProgram::link());
#ifndef MAGNUM_TARGET_GLES
if(!GL::Context::current().isExtensionSupported<GL::Extensions::ARB::explicit_uniform_location>(version))
if(!context.isExtensionSupported<GL::Extensions::ARB::explicit_uniform_location>(version))
#endif
{
_transformationProjectionMatrixUniform = GL::AbstractShaderProgram::uniformLocation("transformationProjectionMatrix");
@ -93,7 +95,7 @@ template<UnsignedInt dimensions> VectorGL<dimensions>::VectorGL(const Flags flag
}
#ifndef MAGNUM_TARGET_GLES
if(!GL::Context::current().isExtensionSupported<GL::Extensions::ARB::shading_language_420pack>(version))
if(!context.isExtensionSupported<GL::Extensions::ARB::shading_language_420pack>(version))
#endif
{
GL::AbstractShaderProgram::setUniform(GL::AbstractShaderProgram::uniformLocation("vectorTexture"), AbstractVectorGL<dimensions>::VectorTextureUnit);

10
src/Magnum/Shaders/VertexColorGL.cpp

@ -47,10 +47,12 @@ template<UnsignedInt dimensions> VertexColorGL<dimensions>::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<UnsignedInt dimensions> VertexColorGL<dimensions>::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<GL::Extensions::ARB::explicit_attrib_location>(version))
if(!context.isExtensionSupported<GL::Extensions::ARB::explicit_attrib_location>(version))
#endif
{
bindAttributeLocation(Position::Location, "position");
@ -80,7 +82,7 @@ template<UnsignedInt dimensions> VertexColorGL<dimensions>::VertexColorGL() {
CORRADE_INTERNAL_ASSERT_OUTPUT(link());
#ifndef MAGNUM_TARGET_GLES
if(!GL::Context::current().isExtensionSupported<GL::Extensions::ARB::explicit_uniform_location>(version))
if(!context.isExtensionSupported<GL::Extensions::ARB::explicit_uniform_location>(version))
#endif
{
_transformationProjectionMatrixUniform = uniformLocation("transformationProjectionMatrix");

Loading…
Cancel
Save