<commit_before>#include <Core/Utils/Log.hpp>
#include <Engine/Renderer/Texture/Texture.hpp>

#include "Texture.hpp"
#include <globjects/Texture.h>

#include <cmath>

namespace Ra {

using namespace Core::Utils; // log

Engine::Texture::Texture( const TextureParameters& texParameters ) :
    m_textureParameters{texParameters},
    m_texture{nullptr},
    m_isMipMaped{false},
    m_isLinear{false} {}

Engine::Texture::~Texture() = default;

void Engine::Texture::initializeGL( bool linearize ) {
    if ( ( m_textureParameters.target != GL_TEXTURE_1D ) &&
         ( m_textureParameters.target != GL_TEXTURE_2D ) &&
         ( m_textureParameters.target != GL_TEXTURE_3D ) )
    {
        LOG( logERROR ) << "Texture of type " << m_textureParameters.target
                        << " must be generated explicitely!";
        return;
    }
    // Transform texels if needed
    if ( linearize )
    {
        uint numcomp = 0;
        bool hasAlpha = false;
        switch ( m_textureParameters.format )
        {
            // RED texture store a gray scale color. Verify if we need to convert
        case GL_RED:
            numcomp = 1;
            break;
        case GL_RGB:
            numcomp = 3;
            break;
        case GL_RGBA:
            numcomp = 4;
            hasAlpha = true;
            break;
        default:
            LOG( logERROR ) << "Textures with format " << m_textureParameters.format
                            << " can't be linearized." << m_textureParameters.name;
            return;
        }
        // This will only do do the RGB space conversion
        sRGBToLinearRGB( reinterpret_cast<uint8_t*>( m_textureParameters.texels ), numcomp,
                         hasAlpha );
    }
    // Generate OpenGL texture
    if ( m_texture == nullptr )
    {
        m_texture = globjects::Texture::create( m_textureParameters.target );
        GL_CHECK_ERROR;
    }
    // Update the sampler parameters
    m_isMipMaped = !( m_textureParameters.minFilter == GL_NEAREST ||
                      m_textureParameters.minFilter == GL_LINEAR );
    updateParameters();
    // upload texture to the GPU
    updateData( m_textureParameters.texels );
    // Generate mipmap if needed.
    if ( m_isMipMaped )
    {
        m_texture->generateMipmap();
    }
}

void Engine::Texture::bind( int unit ) {
    if ( unit >= 0 )
    {
        m_texture->bindActive( uint( unit ) );
    } else
    { m_texture->bind(); }
}

void Engine::Texture::bindImageTexture( int unit, const GLint level, const GLboolean layered,
                                        const GLint layer, const GLenum access ) {
    m_texture->bindImageTexture( uint( unit ), level, layered, layer, access,
                                 m_textureParameters.internalFormat );
}

void Engine::Texture::updateData( void* data ) {
    switch ( m_texture->target() )
    {
    case GL_TEXTURE_1D:
    {
        m_texture->image1D( 0, m_textureParameters.internalFormat,
                            GLsizei( m_textureParameters.width ), 0, m_textureParameters.format,
                            m_textureParameters.type, data );
    }
    break;
    case GL_TEXTURE_2D:
    {
        m_texture->image2D( 0, m_textureParameters.internalFormat,
                            GLsizei( m_textureParameters.width ),
                            GLsizei( m_textureParameters.height ), 0, m_textureParameters.format,
                            m_textureParameters.type, data );
    }
    break;
    case GL_TEXTURE_3D:
    {
        m_texture->image3D(
            0, m_textureParameters.internalFormat, GLsizei( m_textureParameters.width ),
            GLsizei( m_textureParameters.height ), GLsizei( m_textureParameters.depth ), 0,
            m_textureParameters.format, m_textureParameters.type, data );
    }
    break;
    case GL_TEXTURE_CUBE_MAP:
    {
        m_texture->cubeMapImage( 0, m_textureParameters.internalFormat,
                                 GLsizei( m_textureParameters.width ),
                                 GLsizei( m_textureParameters.height ), 0,
                                 m_textureParameters.format, m_textureParameters.type, data );
    }
    break;
    default:
    { CORE_ASSERT( 0, "Unsupported texture type ?" ); }
    break;
    }
    GL_CHECK_ERROR;
}

// let the compiler warn about case fallthrough
void Engine::Texture::updateParameters() {<commit_msg>Add GL_CHECK_ERROR in switch case to help debugging<commit_after>#include <Core/Utils/Log.hpp>
#include <Engine/Renderer/Texture/Texture.hpp>

#include "Texture.hpp"
#include <globjects/Texture.h>

#include <cmath>

namespace Ra {

using namespace Core::Utils; // log

Engine::Texture::Texture( const TextureParameters& texParameters ) :
    m_textureParameters{texParameters},
    m_texture{nullptr},
    m_isMipMaped{false},
    m_isLinear{false} {}

Engine::Texture::~Texture() = default;

void Engine::Texture::initializeGL( bool linearize ) {
    if ( ( m_textureParameters.target != GL_TEXTURE_1D ) &&
         ( m_textureParameters.target != GL_TEXTURE_2D ) &&
         ( m_textureParameters.target != GL_TEXTURE_3D ) )
    {
        LOG( logERROR ) << "Texture of type " << m_textureParameters.target
                        << " must be generated explicitely!";
        return;
    }
    // Transform texels if needed
    if ( linearize )
    {
        uint numcomp = 0;
        bool hasAlpha = false;
        switch ( m_textureParameters.format )
        {
            // RED texture store a gray scale color. Verify if we need to convert
        case GL_RED:
            numcomp = 1;
            break;
        case GL_RGB:
            numcomp = 3;
            break;
        case GL_RGBA:
            numcomp = 4;
            hasAlpha = true;
            break;
        default:
            LOG( logERROR ) << "Textures with format " << m_textureParameters.format
                            << " can't be linearized." << m_textureParameters.name;
            return;
        }
        // This will only do do the RGB space conversion
        sRGBToLinearRGB( reinterpret_cast<uint8_t*>( m_textureParameters.texels ), numcomp,
                         hasAlpha );
    }
    // Generate OpenGL texture
    if ( m_texture == nullptr )
    {
        m_texture = globjects::Texture::create( m_textureParameters.target );
        GL_CHECK_ERROR;
    }
    // Update the sampler parameters
    m_isMipMaped = !( m_textureParameters.minFilter == GL_NEAREST ||
                      m_textureParameters.minFilter == GL_LINEAR );
    updateParameters();
    // upload texture to the GPU
    updateData( m_textureParameters.texels );
    // Generate mipmap if needed.
    if ( m_isMipMaped )
    {
        m_texture->generateMipmap();
    }
}

void Engine::Texture::bind( int unit ) {
    if ( unit >= 0 )
    {
        m_texture->bindActive( uint( unit ) );
    } else
    { m_texture->bind(); }
}

void Engine::Texture::bindImageTexture( int unit, const GLint level, const GLboolean layered,
                                        const GLint layer, const GLenum access ) {
    m_texture->bindImageTexture( uint( unit ), level, layered, layer, access,
                                 m_textureParameters.internalFormat );
}

void Engine::Texture::updateData( void* data ) {
    switch ( m_texture->target() )
    {
    case GL_TEXTURE_1D:
    {
        m_texture->image1D( 0, m_textureParameters.internalFormat,
                            GLsizei( m_textureParameters.width ), 0, m_textureParameters.format,
                            m_textureParameters.type, data );
        GL_CHECK_ERROR
    }
    break;
    case GL_TEXTURE_2D:
    {
        m_texture->image2D( 0, m_textureParameters.internalFormat,
                            GLsizei( m_textureParameters.width ),
                            GLsizei( m_textureParameters.height ), 0, m_textureParameters.format,
                            m_textureParameters.type, data );
        GL_CHECK_ERROR
    }
    break;
    case GL_TEXTURE_3D:
    {
        m_texture->image3D(
            0, m_textureParameters.internalFormat, GLsizei( m_textureParameters.width ),
            GLsizei( m_textureParameters.height ), GLsizei( m_textureParameters.depth ), 0,
            m_textureParameters.format, m_textureParameters.type, data );
        GL_CHECK_ERROR
    }
    break;
    case GL_TEXTURE_CUBE_MAP:
    {
        m_texture->cubeMapImage( 0, m_textureParameters.internalFormat,
                                 GLsizei( m_textureParameters.width ),
                                 GLsizei( m_textureParameters.height ), 0,
                                 m_textureParameters.format, m_textureParameters.type, data );
        GL_CHECK_ERROR
    }
    break;
    default:
    { CORE_ASSERT( 0, "Unsupported texture type ?" ); }
    break;
    }
    GL_CHECK_ERROR;
}

// let the compiler warn about case fallthrough
void Engine::Texture::updateParameters() {
{'fraction_non_alphanumeric': 0.09470377019748653, 'fraction_numerical': 0.005385996409335727, 'mean_word_length': 2.0503080082135523, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7045108', 'n_tokens_mistral': 2535, 'n_tokens_neox': 2481, 'n_words': 545}