<commit_before>/*
    This file is part of Magnum.

    Copyright © 2010, 2011, 2012, 2013, 2014
              <NAME> <<EMAIL>>

    Permission is hereby granted, free of charge, to any person obtaining a
    copy of this software and associated documentation files (the "Software"),
    to deal in the Software without restriction, including without limitation
    the rights to use, copy, modify, merge, publish, distribute, sublicense,
    and/or sell copies of the Software, and to permit persons to whom the
    Software is furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included
    in all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
    THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
    FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
    DEALINGS IN THE SOFTWARE.
*/

#include "AbstractTexture.h"

#ifndef MAGNUM_TARGET_GLES2
#include "Magnum/BufferImage.h"
#endif
#include "Magnum/Array.h"
#include "Magnum/Color.h"
#include "Magnum/ColorFormat.h"
#include "Magnum/Context.h"
#include "Magnum/Extensions.h"
#include "Magnum/Image.h"
#include "Magnum/TextureFormat.h"

#ifdef MAGNUM_BUILD_DEPRECATED
#include "Magnum/Shader.h"
#endif

#include "Implementation/DebugState.h"
#include "Implementation/State.h"
#include "Implementation/TextureState.h"

namespace Magnum {

#ifdef MAGNUM_BUILD_DEPRECATED
Int AbstractTexture::maxLayers() { return Shader::maxCombinedTextureImageUnits(); }
#endif

#ifndef MAGNUM_TARGET_GLES
Int AbstractTexture::maxColorSamples() {
    if(!Context::current()->isExtensionSupported<Extensions::GL::ARB::texture_multisample>())
        return 0;

    GLint& value = Context::current()->state().texture->maxColorSamples;

    /* Get the value, if not already cached */
    if(value == 0)
        glGetIntegerv(GL_MAX_COLOR_TEXTURE_SAMPLES, &value);

    return value;
}

Int AbstractTexture::maxDepthSamples() {
    if(!Context::current()->isExtensionSupported<Extensions::GL::ARB::texture_multisample>())
        return 0;

    GLint& value = Context::current()->state().texture->maxDepthSamples;

    /* Get the value, if not already cached */
    if(value == 0)
        glGetIntegerv(GL_MAX_DEPTH_TEXTURE_SAMPLES, &value);

    return value;
}

Int AbstractTexture::maxIntegerSamples() {
    if(!Context::current()->isExtensionSupported<Extensions::GL::ARB::texture_multisample>())
        return 0;

    GLint& value = Context::current()->state().texture->maxIntegerSamples;

    /* Get the value, if not already cached */
    if(value == 0)
        glGetIntegerv(GL_MAX_INTEGER_SAMPLES, &value);

    return value;
}
#endif

AbstractTexture::AbstractTexture(GLenum target): _target(target) {
    glGenTextures(1, &_id);
}

AbstractTexture::~AbstractTexture() {
    /* Moved out, nothing to do */
    if(!_id) return;

    /* Remove all bindings */
    for(GLuint& binding: Context::current()->state().texture->bindings)
        if(binding == _id) binding = 0;

    glDeleteTextures(1, &_id);
}

std::string AbstractTexture::label() const {
    return Context::current()->state().debug->getLabelImplementation(GL_TEXTURE, _id);
}

AbstractTexture& AbstractTexture::setLabel(const std::string& label) {
    Context::current()->state().debug->labelImplementation(GL_TEXTURE, _id, label);
    return *this;
}

void AbstractTexture::bind(Int textureUnit) {
    Implementation::TextureState* const textureState = Context::current()->state().texture;

    /* If already bound in given texture unit, nothing to do */
    if(textureState->bindings[textureUnit] == _id) return;

    (this->*Context::current()->state().texture->bindImplementation)(textureUnit);
}

void AbstractTexture::bindImplementationDefault(GLint textureUnit) {
    Implementation::TextureState* const textureState = Context::current()->state().texture;

    /* Activate given texture unit, if not already active */
    if(textureState->currentTextureUnit != textureUnit)
        glActiveTexture(GL_TEXTURE0 + (textureState->currentTextureUnit = textureUnit));

    /* Bind the texture to the unit */
    glBindTexture(_target, (textureState->bindings[textureUnit] = _id));
}

#ifndef MAGNUM_TARGET_GLES
void AbstractTexture::bindImplementationMulti(GLint textureUnit) {
    glBindTextures(textureUnit, 1, &_id);
}

void AbstractTexture::bindImplementationDSA(GLint textureUnit) {
    glBindMultiTextureEXT(GL_TEXTURE0 + textureUnit, _target, (Context::current()->state().texture->bindings[textureUnit] = _id));
}
#endif

#ifndef MAGNUM_TARGET_GLES2
void AbstractTexture::setBaseLevel(Int level) {<commit_msg>Forgot to update state tracker in ARB_multi_bind texture binding impl.<commit_after>/*
    This file is part of Magnum.

    Copyright © 2010, 2011, 2012, 2013, 2014
              <NAME> <<EMAIL>>

    Permission is hereby granted, free of charge, to any person obtaining a
    copy of this software and associated documentation files (the "Software"),
    to deal in the Software without restriction, including without limitation
    the rights to use, copy, modify, merge, publish, distribute, sublicense,
    and/or sell copies of the Software, and to permit persons to whom the
    Software is furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included
    in all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
    THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
    FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
    DEALINGS IN THE SOFTWARE.
*/

#include "AbstractTexture.h"

#ifndef MAGNUM_TARGET_GLES2
#include "Magnum/BufferImage.h"
#endif
#include "Magnum/Array.h"
#include "Magnum/Color.h"
#include "Magnum/ColorFormat.h"
#include "Magnum/Context.h"
#include "Magnum/Extensions.h"
#include "Magnum/Image.h"
#include "Magnum/TextureFormat.h"

#ifdef MAGNUM_BUILD_DEPRECATED
#include "Magnum/Shader.h"
#endif

#include "Implementation/DebugState.h"
#include "Implementation/State.h"
#include "Implementation/TextureState.h"

namespace Magnum {

#ifdef MAGNUM_BUILD_DEPRECATED
Int AbstractTexture::maxLayers() { return Shader::maxCombinedTextureImageUnits(); }
#endif

#ifndef MAGNUM_TARGET_GLES
Int AbstractTexture::maxColorSamples() {
    if(!Context::current()->isExtensionSupported<Extensions::GL::ARB::texture_multisample>())
        return 0;

    GLint& value = Context::current()->state().texture->maxColorSamples;

    /* Get the value, if not already cached */
    if(value == 0)
        glGetIntegerv(GL_MAX_COLOR_TEXTURE_SAMPLES, &value);

    return value;
}

Int AbstractTexture::maxDepthSamples() {
    if(!Context::current()->isExtensionSupported<Extensions::GL::ARB::texture_multisample>())
        return 0;

    GLint& value = Context::current()->state().texture->maxDepthSamples;

    /* Get the value, if not already cached */
    if(value == 0)
        glGetIntegerv(GL_MAX_DEPTH_TEXTURE_SAMPLES, &value);

    return value;
}

Int AbstractTexture::maxIntegerSamples() {
    if(!Context::current()->isExtensionSupported<Extensions::GL::ARB::texture_multisample>())
        return 0;

    GLint& value = Context::current()->state().texture->maxIntegerSamples;

    /* Get the value, if not already cached */
    if(value == 0)
        glGetIntegerv(GL_MAX_INTEGER_SAMPLES, &value);

    return value;
}
#endif

AbstractTexture::AbstractTexture(GLenum target): _target(target) {
    glGenTextures(1, &_id);
}

AbstractTexture::~AbstractTexture() {
    /* Moved out, nothing to do */
    if(!_id) return;

    /* Remove all bindings */
    for(GLuint& binding: Context::current()->state().texture->bindings)
        if(binding == _id) binding = 0;

    glDeleteTextures(1, &_id);
}

std::string AbstractTexture::label() const {
    return Context::current()->state().debug->getLabelImplementation(GL_TEXTURE, _id);
}

AbstractTexture& AbstractTexture::setLabel(const std::string& label) {
    Context::current()->state().debug->labelImplementation(GL_TEXTURE, _id, label);
    return *this;
}

void AbstractTexture::bind(Int textureUnit) {
    Implementation::TextureState* const textureState = Context::current()->state().texture;

    /* If already bound in given texture unit, nothing to do */
    if(textureState->bindings[textureUnit] == _id) return;

    (this->*Context::current()->state().texture->bindImplementation)(textureUnit);
}

void AbstractTexture::bindImplementationDefault(GLint textureUnit) {
    Implementation::TextureState* const textureState = Context::current()->state().texture;

    /* Activate given texture unit, if not already active */
    if(textureState->currentTextureUnit != textureUnit)
        glActiveTexture(GL_TEXTURE0 + (textureState->currentTextureUnit = textureUnit));

    /* Bind the texture to the unit */
    glBindTexture(_target, (textureState->bindings[textureUnit] = _id));
}

#ifndef MAGNUM_TARGET_GLES
void AbstractTexture::bindImplementationMulti(GLint textureUnit) {
    /* Bind the texture to the unit, update state tracker */
    Context::current()->state().texture->bindings[textureUnit] = _id;
    glBindTextures(textureUnit, 1, &_id);
}

void AbstractTexture::bindImplementationDSA(GLint textureUnit) {
    glBindMultiTextureEXT(GL_TEXTURE0 + textureUnit, _target, (Context::current()->state().texture->bindings[textureUnit] = _id));
}
#endif

#ifndef MAGNUM_TARGET_GLES2
void AbstractTexture::setBaseLevel(Int level) {
{'fraction_non_alphanumeric': 0.11469072164948453, 'fraction_numerical': 0.0067406819984139575, 'mean_word_length': 4.719387755102041, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 69, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4097987', 'n_tokens_mistral': 2996, 'n_tokens_neox': 2844, 'n_words': 814}