demotool/texture.hh

140 lines
2.9 KiB
C++
Raw Normal View History

2017-09-30 12:59:04 +02:00
#pragma once
#ifndef REAL_BUILD
# include "externals.hh"
#endif
enum class E_TexType {
2017-09-30 12:59:04 +02:00
RGBA8 ,
RGBA16F ,
RGB8 ,
RGB16F ,
2017-09-30 12:59:04 +02:00
R8 ,
R16F ,
};
enum class E_TexSampling {
NEAREST ,
LINEAR ,
};
enum class E_TexWrap {
REPEAT ,
CLAMP_EDGE ,
CLAMP_BORDER
};
2017-09-30 12:59:04 +02:00
struct T_Texture
{
T_Texture( ) = delete;
T_Texture( T_Texture const& ) = delete;
T_Texture( T_Texture&& ) = delete;
T_Texture(
__rd__ const uint32_t width ,
__rd__ const uint32_t height ,
__rd__ const E_TexType type ,
__rd__ const uint32_t levels = 1 );
~T_Texture( );
T_Texture& samplingMode(
__rd__ const E_TexSampling mode );
T_Texture& wrap(
__rd__ const E_TexWrap mode );
2017-09-30 12:59:04 +02:00
GLuint id( ) const noexcept { return id_; }
uint32_t levels( ) const noexcept { return levels_; }
2017-09-30 12:59:04 +02:00
uint32_t width( ) const noexcept { return width_; }
uint32_t height( ) const noexcept { return height_; }
private:
GLuint id_;
uint32_t levels_ , width_ , height_;
2017-09-30 12:59:04 +02:00
};
2017-10-02 10:42:06 +02:00
struct T_TextureSampler
{
T_TextureSampler( T_TextureSampler const& ) = delete;
// --------------------------------------------------------------------
// Initialisation, destruction & move
T_TextureSampler( );
T_TextureSampler(
__rw__ T_TextureSampler&& other ) noexcept;
T_TextureSampler& operator=(
__rw__ T_TextureSampler&& other ) noexcept;
~T_TextureSampler( );
// --------------------------------------------------------------------
// Configuration
T_TextureSampler& sampling(
__rd__ const E_TexSampling mode );
T_TextureSampler& noMipmap( );
T_TextureSampler& mipmap(
__rd__ const E_TexSampling mode );
T_TextureSampler& wrap(
__rd__ const E_TexWrap mode );
T_TextureSampler& lod(
__rd__ const float min ,
__rd__ const float max );
// --------------------------------------------------------------------
// Usage
2017-10-02 10:57:51 +02:00
GLuint id( ) const noexcept { return id_; }
// unused
2017-10-02 10:42:06 +02:00
void bind( __rd__ const GLuint unit )
{ glBindSampler( unit , id_ ); }
private:
void setSamplingMode( ) const;
GLuint id_ = 0;
E_TexSampling sampling_ = E_TexSampling::NEAREST;
E_TexSampling lodSampling_ = E_TexSampling::NEAREST;
bool hasLOD_ = false;
};
2017-10-02 10:57:51 +02:00
struct T_TextureManagement
{
static constexpr uint32_t MaxUnits = 8;
NO_COPY( T_TextureManagement );
NO_MOVE( T_TextureManagement );
static T_TextureManagement& TM( );
static void Shutdown( );
void reset( );
T_TextureSampler const* sampler(
__rd__ std::string const& name ) const;
2017-10-02 10:57:51 +02:00
void bind( __rd__ const uint32_t unit ,
__rd__ T_Texture const& texture );
void bind( __rd__ const uint32_t unit ,
__rd__ T_Texture const& texture ,
__rd__ T_TextureSampler const& sampler );
2017-10-02 10:57:51 +02:00
private:
static std::unique_ptr< T_TextureManagement > instance_;
T_TextureManagement( );
2017-10-02 10:57:51 +02:00
struct T_Binding_
{
GLuint texture = 0;
GLuint sampler = 0;
2017-10-02 10:57:51 +02:00
};
std::map< std::string , std::shared_ptr< T_TextureSampler > > samplers_;
2017-10-02 10:57:51 +02:00
T_Binding_ bindings_[ MaxUnits ];
};