Browse Source

Allocation-free overloads for *::setLabel().

In most cases the label is set directly from code, e.g.:

    texture.setLabel("diffuse-duck");

Avoiding conversion to std::string and passing char(&)[size] directly
will avoid one allocation and deallocation. Better solution would be to
use std::string_view everywhere, but we're not in C++17 yet.
pull/68/head
Vladimír Vondruš 12 years ago
parent
commit
1b45a71ff5
  1. 13
      src/Magnum/AbstractObject.cpp
  2. 9
      src/Magnum/AbstractObject.h
  3. 2
      src/Magnum/AbstractShaderProgram.cpp
  4. 12
      src/Magnum/AbstractShaderProgram.h
  5. 2
      src/Magnum/AbstractTexture.cpp
  6. 13
      src/Magnum/AbstractTexture.h
  7. 2
      src/Magnum/Buffer.cpp
  8. 11
      src/Magnum/Buffer.h
  9. 4
      src/Magnum/BufferTexture.h
  10. 4
      src/Magnum/CubeMapTexture.h
  11. 4
      src/Magnum/CubeMapTextureArray.h
  12. 2
      src/Magnum/Framebuffer.cpp
  13. 11
      src/Magnum/Framebuffer.h
  14. 2
      src/Magnum/Implementation/DebugState.h
  15. 2
      src/Magnum/Mesh.cpp
  16. 11
      src/Magnum/Mesh.h
  17. 4
      src/Magnum/MultisampleTexture.h
  18. 2
      src/Magnum/Query.cpp
  19. 24
      src/Magnum/Query.h
  20. 4
      src/Magnum/RectangleTexture.h
  21. 2
      src/Magnum/Renderbuffer.cpp
  22. 13
      src/Magnum/Renderbuffer.h
  23. 2
      src/Magnum/Shader.cpp
  24. 12
      src/Magnum/Shader.h
  25. 4
      src/Magnum/Texture.h
  26. 4
      src/Magnum/TextureArray.h

13
src/Magnum/AbstractObject.cpp

@ -26,6 +26,7 @@
#include "AbstractObject.h"
#include <Corrade/Utility/Assert.h>
#include <Corrade/Containers/Array.h>
#include "Magnum/Context.h"
#include "Magnum/Extensions.h"
@ -123,26 +124,26 @@ Int AbstractObject::maxLabelLength() {
return value;
}
void AbstractObject::labelImplementationNoOp(GLenum, GLuint, const std::string&) {}
void AbstractObject::labelImplementationNoOp(GLenum, GLuint, Containers::ArrayReference<const char>) {}
void AbstractObject::labelImplementationKhr(const GLenum identifier, const GLuint name, const std::string& label) {
void AbstractObject::labelImplementationKhr(const GLenum identifier, const GLuint name, const Containers::ArrayReference<const char> label) {
/** @todo Re-enable when extension loader is available for ES */
#ifndef MAGNUM_TARGET_GLES
glObjectLabel(identifier, name, label.size(), label.data());
glObjectLabel(identifier, name, label.size(), label);
#else
static_cast<void>(identifier);
static_cast<void>(name);
static_cast<void>(label);
CORRADE_INTERNAL_ASSERT(false);
//glObjectLabelKHR(identifier, name, label.size(), label.data());
//glObjectLabelKHR(identifier, name, label.size(), label);
#endif
}
void AbstractObject::labelImplementationExt(const GLenum identifier, const GLuint name, const std::string& label) {
void AbstractObject::labelImplementationExt(const GLenum identifier, const GLuint name, const Containers::ArrayReference<const char> label) {
const GLenum type = extTypeFromKhrIdentifier(identifier);
/** @todo Re-enable when extension loader is available for ES */
#ifndef MAGNUM_TARGET_GLES
glLabelObjectEXT(type, name, label.size(), label.data());
glLabelObjectEXT(type, name, label.size(), label);
#else
static_cast<void>(type);
static_cast<void>(name);

9
src/Magnum/AbstractObject.h

@ -30,9 +30,10 @@
*/
#include <string>
#include <Corrade/Containers/Containers.h>
#include "Magnum/Magnum.h"
#include "Magnum/OpenGL.h"
#include "Magnum/Types.h"
#include "Magnum/visibility.h"
namespace Magnum {
@ -66,9 +67,9 @@ class MAGNUM_EXPORT AbstractObject {
MAGNUM_LOCAL ~AbstractObject() = default;
private:
static MAGNUM_LOCAL void labelImplementationNoOp(GLenum, GLuint, const std::string&);
static MAGNUM_LOCAL void labelImplementationExt(GLenum identifier, GLuint name, const std::string& label);
static MAGNUM_LOCAL void labelImplementationKhr(GLenum identifier, GLuint name, const std::string& label);
static MAGNUM_LOCAL void labelImplementationNoOp(GLenum, GLuint, Containers::ArrayReference<const char> label);
static MAGNUM_LOCAL void labelImplementationExt(GLenum identifier, GLuint name, Containers::ArrayReference<const char> label);
static MAGNUM_LOCAL void labelImplementationKhr(GLenum identifier, GLuint name, Containers::ArrayReference<const char> label);
static MAGNUM_LOCAL std::string getLabelImplementationNoOp(GLenum, GLuint);
static MAGNUM_LOCAL std::string getLabelImplementationExt(GLenum identifier, GLuint name);
static MAGNUM_LOCAL std::string getLabelImplementationKhr(GLenum identifier, GLuint name);

2
src/Magnum/AbstractShaderProgram.cpp

@ -225,7 +225,7 @@ std::string AbstractShaderProgram::label() const {
#endif
}
AbstractShaderProgram& AbstractShaderProgram::setLabel(const std::string& label) {
AbstractShaderProgram& AbstractShaderProgram::setLabelInternal(const Containers::ArrayReference<const char> label) {
#ifndef MAGNUM_TARGET_GLES
Context::current()->state().debug->labelImplementation(GL_PROGRAM, _id, label);
#else

12
src/Magnum/AbstractShaderProgram.h

@ -31,6 +31,7 @@
#include <functional>
#include <string>
#include <Corrade/Containers/Array.h>
#include <Corrade/Containers/EnumSet.h>
#include "Magnum/AbstractObject.h"
@ -540,7 +541,14 @@ class MAGNUM_EXPORT AbstractShaderProgram: public AbstractObject {
* @def_gl{PROGRAM} or @fn_gl_extension2{LabelObject,EXT,debug_label}
* with @def_gl{PROGRAM_OBJECT_EXT}
*/
AbstractShaderProgram& setLabel(const std::string& label);
AbstractShaderProgram& setLabel(const std::string& label) {
return setLabelInternal({label.data(), label.size()});
}
/** @overload */
template<std::size_t size> AbstractShaderProgram& setLabel(const char (&label)[size]) {
return setLabelInternal(label);
}
/**
* @brief Validate program
@ -817,6 +825,8 @@ class MAGNUM_EXPORT AbstractShaderProgram: public AbstractObject {
#endif
private:
AbstractShaderProgram& setLabelInternal(Containers::ArrayReference<const char> label);
#ifndef MAGNUM_BUILD_DEPRECATED
void use();
#endif

2
src/Magnum/AbstractTexture.cpp

@ -189,7 +189,7 @@ std::string AbstractTexture::label() const {
return Context::current()->state().debug->getLabelImplementation(GL_TEXTURE, _id);
}
AbstractTexture& AbstractTexture::setLabel(const std::string& label) {
AbstractTexture& AbstractTexture::setLabelInternal(const Containers::ArrayReference<const char> label) {
Context::current()->state().debug->labelImplementation(GL_TEXTURE, _id, label);
return *this;
}

13
src/Magnum/AbstractTexture.h

@ -29,6 +29,8 @@
* @brief Class @ref Magnum::AbstractTexture
*/
#include <Corrade/Containers/Array.h>
#include "Magnum/Sampler.h"
#include "Magnum/AbstractObject.h"
@ -258,7 +260,14 @@ class MAGNUM_EXPORT AbstractTexture: public AbstractObject {
* @fn_gl_extension2{LabelObject,EXT,debug_label} with
* @def_gl{TEXTURE}
*/
AbstractTexture& setLabel(const std::string& label);
AbstractTexture& setLabel(const std::string& label) {
return setLabelInternal({label.data(), label.size()});
}
/** @overload */
template<std::size_t size> AbstractTexture& setLabel(const char(&label)[size]) {
return setLabelInternal(label);
}
/** @brief OpenGL texture ID */
GLuint id() const { return _id; }
@ -288,6 +297,8 @@ class MAGNUM_EXPORT AbstractTexture: public AbstractObject {
explicit AbstractTexture(GLenum target);
AbstractTexture& setLabelInternal(Containers::ArrayReference<const char> label);
/* Unlike bind() this also sets the texture binding unit as active */
void MAGNUM_LOCAL bindInternal();

2
src/Magnum/Buffer.cpp

@ -138,7 +138,7 @@ std::string Buffer::label() const {
#endif
}
Buffer& Buffer::setLabel(const std::string& label) {
Buffer& Buffer::setLabelInternal(const Containers::ArrayReference<const char> label) {
#ifndef MAGNUM_TARGET_GLES
Context::current()->state().debug->labelImplementation(GL_BUFFER, _id, label);
#else

11
src/Magnum/Buffer.h

@ -582,7 +582,14 @@ class MAGNUM_EXPORT Buffer: public AbstractObject {
* or @fn_gl_extension2{LabelObject,EXT,debug_label} with
* @def_gl{BUFFER_OBJECT_EXT}
*/
Buffer& setLabel(const std::string& label);
Buffer& setLabel(const std::string& label) {
return setLabelInternal({label.data(), label.size()});
}
/** @overload */
template<std::size_t size> Buffer& setLabel(const char(&label)[size]) {
return setLabelInternal(label);
}
/** @brief Target hint */
Target targetHint() const { return _targetHint; }
@ -854,6 +861,8 @@ class MAGNUM_EXPORT Buffer: public AbstractObject {
#endif
private:
Buffer& setLabelInternal(Containers::ArrayReference<const char> label);
static void bind(Target hint, GLuint id);
Target MAGNUM_LOCAL bindInternal(Target hint);

4
src/Magnum/BufferTexture.h

@ -269,6 +269,10 @@ class MAGNUM_EXPORT BufferTexture: public AbstractTexture {
AbstractTexture::setLabel(label);
return *this;
}
template<std::size_t size> BufferTexture& setLabel(const char(&label)[size]) {
AbstractTexture::setLabel<size>(label);
return *this;
}
#endif
private:

4
src/Magnum/CubeMapTexture.h

@ -382,6 +382,10 @@ class MAGNUM_EXPORT CubeMapTexture: public AbstractTexture {
AbstractTexture::setLabel(label);
return *this;
}
template<std::size_t size> CubeMapTexture& setLabel(const char(&label)[size]) {
AbstractTexture::setLabel<size>(label);
return *this;
}
#endif
};

4
src/Magnum/CubeMapTextureArray.h

@ -405,6 +405,10 @@ class CubeMapTextureArray: public AbstractTexture {
AbstractTexture::setLabel(label);
return *this;
}
template<std::size_t size> CubeMapTextureArray& setLabel(const char(&label)[size]) {
AbstractTexture::setLabel<size>(label);
return *this;
}
#endif
};

2
src/Magnum/Framebuffer.cpp

@ -112,7 +112,7 @@ std::string Framebuffer::label() const {
return Context::current()->state().debug->getLabelImplementation(GL_FRAMEBUFFER, _id);
}
Framebuffer& Framebuffer::setLabel(const std::string& label) {
Framebuffer& Framebuffer::setLabelInternal(const Containers::ArrayReference<const char> label) {
Context::current()->state().debug->labelImplementation(GL_FRAMEBUFFER, _id, label);
return *this;
}

11
src/Magnum/Framebuffer.h

@ -354,7 +354,14 @@ class MAGNUM_EXPORT Framebuffer: public AbstractFramebuffer, public AbstractObje
* @fn_gl_extension2{LabelObject,EXT,debug_label} with
* @def_gl{FRAMEBUFFER}
*/
Framebuffer& setLabel(const std::string& label);
Framebuffer& setLabel(const std::string& label) {
return setLabelInternal({label.data(), label.size()});
}
/** @overload */
template<std::size_t size> Framebuffer& setLabel(const char(&label)[size]) {
return setLabelInternal(label);
}
/**
* @brief Check framebuffer status
@ -627,6 +634,8 @@ class MAGNUM_EXPORT Framebuffer: public AbstractFramebuffer, public AbstractObje
#endif
private:
Framebuffer& setLabelInternal(Containers::ArrayReference<const char> label);
void MAGNUM_LOCAL renderbufferImplementationDefault(BufferAttachment attachment, Renderbuffer& renderbuffer);
#ifndef MAGNUM_TARGET_GLES
void MAGNUM_LOCAL renderbufferImplementationDSA(BufferAttachment attachment, Renderbuffer& renderbuffer);

2
src/Magnum/Implementation/DebugState.h

@ -36,7 +36,7 @@ struct DebugState {
explicit DebugState(Context& context, std::vector<std::string>& extensions);
std::string(*getLabelImplementation)(GLenum, GLuint);
void(*labelImplementation)(GLenum, GLuint, const std::string&);
void(*labelImplementation)(GLenum, GLuint, Containers::ArrayReference<const char>);
void(*messageInsertImplementation)(DebugMessage::Source, DebugMessage::Type, UnsignedInt, DebugMessage::Severity, const std::string&);
void(*messageCallbackImplementation)(DebugMessage::Callback, const void*);

2
src/Magnum/Mesh.cpp

@ -148,7 +148,7 @@ std::string Mesh::label() const {
#endif
}
Mesh& Mesh::setLabel(const std::string& label) {
Mesh& Mesh::setLabelInternal(const Containers::ArrayReference<const char> label) {
#ifndef MAGNUM_TARGET_GLES
Context::current()->state().debug->labelImplementation(GL_VERTEX_ARRAY, _id, label);
#else

11
src/Magnum/Mesh.h

@ -467,7 +467,14 @@ class MAGNUM_EXPORT Mesh: public AbstractObject {
* @def_gl{VERTEX_ARRAY} or @fn_gl_extension2{LabelObject,EXT,debug_label}
* with @def_gl{VERTEX_ARRAY_OBJECT_EXT}
*/
Mesh& setLabel(const std::string& label);
Mesh& setLabel(const std::string& label) {
return setLabelInternal({label.data(), label.size()});
}
/** @overload */
template<std::size_t size> Mesh& setLabelInternal(const char(&label)[size]) {
return setLabelInternal(label);
}
/**
* @brief Whether the mesh is indexed
@ -836,6 +843,8 @@ class MAGNUM_EXPORT Mesh: public AbstractObject {
#endif
#endif
Mesh& setLabelInternal(Containers::ArrayReference<const char> label);
/* Computing stride of interleaved vertex attributes */
template<UnsignedInt location, class T, class ...U> static GLsizei strideOfInterleaved(const AbstractShaderProgram::Attribute<location, T>& attribute, const U&... attributes) {
return attribute.vectorSize()*AbstractShaderProgram::Attribute<location, T>::VectorCount + strideOfInterleaved(attributes...);

4
src/Magnum/MultisampleTexture.h

@ -180,6 +180,10 @@ template<UnsignedInt dimensions> class MultisampleTexture: public AbstractTextur
AbstractTexture::setLabel(label);
return *this;
}
template<std::size_t size> MultisampleTexture<dimensions>& setLabel(const char(&label)[size]) {
AbstractTexture::setLabel<size>(label);
return *this;
}
#endif
};

2
src/Magnum/Query.cpp

@ -66,7 +66,7 @@ std::string AbstractQuery::label() const {
#endif
}
AbstractQuery& AbstractQuery::setLabel(const std::string& label) {
AbstractQuery& AbstractQuery::setLabelInternal(const Containers::ArrayReference<const char> label) {
#ifndef MAGNUM_TARGET_GLES
Context::current()->state().debug->labelImplementation(GL_QUERY, _id, label);
#else

24
src/Magnum/Query.h

@ -29,6 +29,7 @@
* @brief Class @ref Magnum::AbstractQuery, @ref Magnum::PrimitiveQuery, @ref Magnum::SampleQuery, @ref Magnum::TimeQuery
*/
#include <Corrade/Containers/Array.h>
#include <Corrade/Utility/Assert.h>
#include "Magnum/AbstractObject.h"
@ -84,7 +85,14 @@ class MAGNUM_EXPORT AbstractQuery: public AbstractObject {
* @def_gl{QUERY} or @fn_gl_extension2{LabelObject,EXT,debug_label}
* with @def_gl{QUERY_OBJECT_EXT}
*/
AbstractQuery& setLabel(const std::string& label);
AbstractQuery& setLabel(const std::string& label) {
return setLabelInternal({label.data(), label.size()});
}
/** @overload */
template<std::size_t size> AbstractQuery& setLabel(const char(&label)[size]) {
return setLabelInternal(label);
}
/**
* @brief Whether the result is available
@ -140,6 +148,8 @@ class MAGNUM_EXPORT AbstractQuery: public AbstractObject {
void begin(GLenum target);
private:
AbstractQuery& setLabelInternal(Containers::ArrayReference<const char> label);
GLuint _id;
GLenum target;
};
@ -217,6 +227,10 @@ class PrimitiveQuery: public AbstractQuery {
AbstractQuery::setLabel(label);
return *this;
}
template<std::size_t size> PrimitiveQuery& setLabel(const char(&label)[size]) {
AbstractQuery::setLabel<size>(label);
return *this;
}
#endif
};
#endif
@ -367,6 +381,10 @@ class SampleQuery: public AbstractQuery {
AbstractQuery::setLabel(label);
return *this;
}
template<std::size_t size> SampleQuery& setLabel(const char(&label)[size]) {
AbstractQuery::setLabel<size>(label);
return *this;
}
#endif
};
@ -446,6 +464,10 @@ class TimeQuery: public AbstractQuery {
AbstractQuery::setLabel(label);
return *this;
}
template<std::size_t size> TimeQuery& setLabel(const char(&label)[size]) {
AbstractQuery::setLabel<size>(label);
return *this;
}
#endif
};

4
src/Magnum/RectangleTexture.h

@ -404,6 +404,10 @@ class MAGNUM_EXPORT RectangleTexture: public AbstractTexture {
AbstractTexture::setLabel(label);
return *this;
}
template<std::size_t size> RectangleTexture& setLabel(const char(&label)[size]) {
AbstractTexture::setLabel<size>(label);
return *this;
}
#endif
};

2
src/Magnum/Renderbuffer.cpp

@ -81,7 +81,7 @@ std::string Renderbuffer::label() const {
return Context::current()->state().debug->getLabelImplementation(GL_RENDERBUFFER, _id);
}
Renderbuffer& Renderbuffer::setLabel(const std::string& label) {
Renderbuffer& Renderbuffer::setLabelInternal(const Containers::ArrayReference<const char> label) {
Context::current()->state().debug->labelImplementation(GL_RENDERBUFFER, _id, label);
return *this;
}

13
src/Magnum/Renderbuffer.h

@ -29,6 +29,8 @@
* @brief Class @ref Magnum::Renderbuffer
*/
#include <Corrade/Containers/Array.h>
#include "Magnum/AbstractObject.h"
#include "Magnum/Magnum.h"
@ -135,7 +137,14 @@ class MAGNUM_EXPORT Renderbuffer: public AbstractObject {
* @fn_gl_extension2{LabelObject,EXT,debug_label} with
* @def_gl{RENDERBUFFER}
*/
Renderbuffer& setLabel(const std::string& label);
Renderbuffer& setLabel(const std::string& label) {
return setLabelInternal({label.data(), label.size()});
}
/** @overload */
template<std::size_t size> Renderbuffer& setLabel(const char(&label)[size]) {
return setLabelInternal(label);
}
/**
* @brief Set renderbuffer storage
@ -169,6 +178,8 @@ class MAGNUM_EXPORT Renderbuffer: public AbstractObject {
void setStorageMultisample(Int samples, RenderbufferFormat internalFormat, const Vector2i& size);
private:
Renderbuffer& setLabelInternal(Containers::ArrayReference<const char> label);
void MAGNUM_LOCAL storageImplementationDefault(RenderbufferFormat internalFormat, const Vector2i& size);
#ifndef MAGNUM_TARGET_GLES
void MAGNUM_LOCAL storageImplementationDSA(RenderbufferFormat internalFormat, const Vector2i& size);

2
src/Magnum/Shader.cpp

@ -580,7 +580,7 @@ std::string Shader::label() const {
#endif
}
Shader& Shader::setLabel(const std::string& label) {
Shader& Shader::setLabelInternal(const Containers::ArrayReference<const char> label) {
#ifndef MAGNUM_TARGET_GLES
Context::current()->state().debug->labelImplementation(GL_SHADER, _id, label);
#else

12
src/Magnum/Shader.h

@ -32,6 +32,7 @@
#include <functional>
#include <string>
#include <vector>
#include <Corrade/Containers/Array.h>
#include "Magnum/AbstractObject.h"
#include "Magnum/Magnum.h"
@ -504,7 +505,14 @@ class MAGNUM_EXPORT Shader: public AbstractObject {
* @def_gl{SHADER} or @fn_gl_extension2{LabelObject,EXT,debug_label}
* with @def_gl{SHADER_OBJECT_EXT}
*/
Shader& setLabel(const std::string& label);
Shader& setLabel(const std::string& label) {
return setLabelInternal({label.data(), label.size()});
}
/** @overload */
template<std::size_t size> Shader& setLabel(const char(&label)[size]) {
return setLabelInternal(label);
}
/** @brief %Shader type */
Type type() const { return _type; }
@ -545,6 +553,8 @@ class MAGNUM_EXPORT Shader: public AbstractObject {
bool compile() { return compile({*this}); }
private:
Shader& setLabelInternal(Containers::ArrayReference<const char> label);
Type _type;
GLuint _id;

4
src/Magnum/Texture.h

@ -759,6 +759,10 @@ template<UnsignedInt dimensions> class Texture: public AbstractTexture {
AbstractTexture::setLabel(label);
return *this;
}
template<std::size_t size> Texture<dimensions>& setLabel(const char(&label)[size]) {
AbstractTexture::setLabel<size>(label);
return *this;
}
#endif
};

4
src/Magnum/TextureArray.h

@ -389,6 +389,10 @@ template<UnsignedInt dimensions> class TextureArray: public AbstractTexture {
AbstractTexture::setLabel(label);
return *this;
}
template<std::size_t size> TextureArray<dimensions>& setLabel(const char(&label)[size]) {
AbstractTexture::setLabel<size>(label);
return *this;
}
#endif
};

Loading…
Cancel
Save