2017-09-30 12:59:04 +02:00
|
|
|
#include "externals.hh"
|
2017-10-01 18:51:02 +02:00
|
|
|
#include "utilities.hh"
|
2017-09-30 12:59:04 +02:00
|
|
|
#include "texture.hh"
|
|
|
|
|
|
|
|
|
|
|
|
T_Texture::T_Texture(
|
|
|
|
__rd__ const uint32_t width ,
|
|
|
|
__rd__ const uint32_t height ,
|
|
|
|
__rd__ const E_TexType type ,
|
|
|
|
__rd__ const uint32_t levels )
|
2017-09-30 17:58:48 +02:00
|
|
|
: levels_( levels ) , width_( width ) , height_( height )
|
2017-09-30 12:59:04 +02:00
|
|
|
{
|
|
|
|
assert( levels > 0 );
|
|
|
|
|
|
|
|
glGenTextures( 1 , &id_ );
|
|
|
|
glBindTexture( GL_TEXTURE_2D , id_ );
|
|
|
|
|
|
|
|
GLenum ifmt , fmt , dt;
|
|
|
|
switch ( type ) {
|
|
|
|
case E_TexType::RGBA8:
|
|
|
|
ifmt = GL_RGBA8;
|
|
|
|
fmt = GL_RGBA;
|
|
|
|
dt = GL_UNSIGNED_BYTE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case E_TexType::RGBA16F:
|
|
|
|
ifmt = GL_RGBA16F;
|
|
|
|
fmt = GL_RGBA;
|
|
|
|
dt = GL_FLOAT;
|
|
|
|
break;
|
|
|
|
|
2017-09-30 17:58:48 +02:00
|
|
|
case E_TexType::RGB8:
|
|
|
|
ifmt = GL_RGB8;
|
|
|
|
fmt = GL_RGB;
|
|
|
|
dt = GL_UNSIGNED_BYTE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case E_TexType::RGB16F:
|
|
|
|
ifmt = GL_RGB16F;
|
|
|
|
fmt = GL_RGB;
|
|
|
|
dt = GL_FLOAT;
|
|
|
|
break;
|
|
|
|
|
2017-09-30 12:59:04 +02:00
|
|
|
case E_TexType::R8:
|
|
|
|
ifmt = GL_R8;
|
|
|
|
fmt = GL_RED;
|
|
|
|
dt = GL_UNSIGNED_BYTE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case E_TexType::R16F:
|
|
|
|
ifmt = GL_R16F;
|
|
|
|
fmt = GL_RED;
|
|
|
|
dt = GL_FLOAT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-09-30 17:58:48 +02:00
|
|
|
glTexParameteri( GL_TEXTURE_2D , GL_TEXTURE_BASE_LEVEL , 0 );
|
|
|
|
glTexParameteri( GL_TEXTURE_2D , GL_TEXTURE_MAX_LEVEL , levels - 1 );
|
|
|
|
glTexParameterf( GL_TEXTURE_2D , GL_TEXTURE_MIN_LOD , 0 );
|
|
|
|
glTexParameterf( GL_TEXTURE_2D , GL_TEXTURE_MAX_LOD , levels - 1 );
|
|
|
|
glTexParameteri( GL_TEXTURE_2D , GL_TEXTURE_MIN_FILTER , GL_NEAREST );
|
|
|
|
glTexParameteri( GL_TEXTURE_2D , GL_TEXTURE_MAG_FILTER , GL_NEAREST );
|
2017-09-30 12:59:04 +02:00
|
|
|
|
|
|
|
uint32_t w = width , h = height;
|
|
|
|
for ( auto i = 0u ; i < levels ; i ++ ) {
|
2017-10-01 18:51:02 +02:00
|
|
|
#ifdef INTRUSIVE_TRACES
|
|
|
|
printf( "init %p txid %d lv %d sz %dx%d\n" , this , id_ ,
|
|
|
|
i , w , h );
|
|
|
|
#endif
|
2017-09-30 12:59:04 +02:00
|
|
|
glTexImage2D( GL_TEXTURE_2D , i , ifmt , w , h , 0 , fmt , dt , nullptr );
|
2017-09-30 15:28:24 +02:00
|
|
|
w >>= 1;
|
|
|
|
h >>= 1;
|
2017-09-30 12:59:04 +02:00
|
|
|
assert( w && h );
|
|
|
|
}
|
|
|
|
|
2017-10-01 18:51:02 +02:00
|
|
|
GL_CHECK( );
|
2017-09-30 12:59:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
T_Texture::~T_Texture( )
|
|
|
|
{
|
|
|
|
glDeleteTextures( 1 , &id_ );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-09-30 17:58:48 +02:00
|
|
|
T_Texture& T_Texture::samplingMode(
|
|
|
|
__rd__ const E_TexSampling mode )
|
|
|
|
{
|
|
|
|
glBindTexture( GL_TEXTURE_2D , id_ );
|
|
|
|
|
|
|
|
GLenum min , max;
|
|
|
|
switch ( mode ) {
|
|
|
|
case E_TexSampling::NEAREST:
|
|
|
|
min = levels_ > 1 ? GL_NEAREST_MIPMAP_NEAREST : GL_NEAREST;
|
|
|
|
max = GL_NEAREST;
|
|
|
|
break;
|
|
|
|
case E_TexSampling::LINEAR:
|
|
|
|
min = levels_ > 1 ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR;
|
|
|
|
max = GL_LINEAR;
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
glTexParameteri( GL_TEXTURE_2D , GL_TEXTURE_MIN_FILTER , min );
|
|
|
|
glTexParameteri( GL_TEXTURE_2D , GL_TEXTURE_MAG_FILTER , max );
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
T_Texture& T_Texture::wrap(
|
|
|
|
__rd__ const E_TexWrap mode )
|
|
|
|
{
|
|
|
|
GLenum gm;
|
|
|
|
switch ( mode ) {
|
|
|
|
case E_TexWrap::REPEAT:
|
|
|
|
gm = GL_REPEAT;
|
|
|
|
break;
|
|
|
|
case E_TexWrap::CLAMP_EDGE:
|
|
|
|
gm = GL_CLAMP_TO_EDGE;
|
|
|
|
break;
|
|
|
|
case E_TexWrap::CLAMP_BORDER:
|
|
|
|
gm = GL_CLAMP_TO_BORDER;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
glTexParameteri( GL_TEXTURE_2D , GL_TEXTURE_WRAP_S , gm );
|
|
|
|
glTexParameteri( GL_TEXTURE_2D , GL_TEXTURE_WRAP_T , gm );
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2017-09-30 12:59:04 +02:00
|
|
|
/*============================================================================*/
|
|
|
|
|
|
|
|
|
|
|
|
T_TextureBinding::T_TextureBinding(
|
|
|
|
__rd__ const uint32_t binding )
|
|
|
|
: binding_( binding ) , uniform_( 0 ) , texture_( nullptr )
|
|
|
|
{ }
|
|
|
|
|
|
|
|
void T_TextureBinding::clear( )
|
|
|
|
{
|
|
|
|
uniform_ = 0;
|
|
|
|
texture_ = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void T_TextureBinding::set(
|
|
|
|
__rd__ const uint32_t uniform ,
|
|
|
|
__rd__ T_Texture const& texture )
|
|
|
|
{
|
|
|
|
uniform_ = uniform;
|
|
|
|
texture_ = &texture;
|
|
|
|
}
|
|
|
|
|
|
|
|
void T_TextureBinding::bind( ) const
|
|
|
|
{
|
2017-09-30 17:58:48 +02:00
|
|
|
glBindTextureUnit( binding_ ,
|
|
|
|
texture_ ? texture_->id( ) : 0 );
|
2017-09-30 12:59:04 +02:00
|
|
|
glUniform1i( uniform_ , binding_ );
|
|
|
|
assert( glGetError( ) == GL_NO_ERROR );
|
|
|
|
}
|