Radium Engine  1.5.20
Loading...
Searching...
No Matches
Texture.hpp
1#pragma once
2
3#include <Core/Tasks/TaskQueue.hpp>
4#include <Core/Utils/Color.hpp>
5#include <Engine/OpenGL.hpp>
6#include <Engine/RaEngine.hpp>
7
8#include <memory>
9#include <mutex>
10#include <string>
11#include <variant>
12
13namespace globjects {
14class Texture;
15}
16
17namespace Ra {
18namespace Engine {
19namespace Data {
20
24 GLenum wrapS { GL_CLAMP_TO_EDGE };
26 GLenum wrapT { GL_CLAMP_TO_EDGE };
28 GLenum wrapR { GL_CLAMP_TO_EDGE };
30 GLenum minFilter { GL_LINEAR };
32 GLenum magFilter { GL_LINEAR };
33};
34
44
46 template <typename TexelType>
47 bool isTexelOfType() const {
48 return std::holds_alternative<TexelType>( texels );
49 }
50
51 ImageType getImage() const {
52 CORE_ASSERT( isTexelOfType<ImageType>(), "texture variant is not ImageType" );
53 return std::get<ImageType>( texels );
54 }
55 void setImage( ImageType image ) { texels = image; }
56
60 const void* getTexels() const { return getImage().get(); }
61
65 const CubeMapType getCubeMap() const {
66 CORE_ASSERT( isTexelOfType<CubeMapType>(), "texture variant is not CubeMapType" );
67 return std::get<CubeMapType>( texels );
68 }
69 void setCubeMap( CubeMapType cubeMap ) { texels = cubeMap; }
70
71 GLenum target { GL_TEXTURE_2D }; //< OpenGL target
72 size_t width { 1 }; //< width of the texture (s dimension)
73 size_t height { 1 }; //< height of the texture (t dimension)
74 size_t depth { 1 }; //< depth of the texture (r dimension)
75 GLenum format { GL_RGB }; //< Format of the external data
77 GLenum internalFormat { GL_RGB };
78 GLenum type { GL_UNSIGNED_BYTE }; //< Type of the components in external data
80 bool isLinear { false };
81 std::variant<ImageType, CubeMapType> texels { nullptr }; //< texels OR cubeMap, shared ownership
82};
83
100 std::string name {};
101 SamplerParameters sampler {};
102 ImageParameters image {};
103};
104
119class RA_ENGINE_API Texture final
120{
121 public:
124 Texture( const Texture& ) = delete;
125
128 void operator=( const Texture& ) = delete;
129
134 explicit Texture( const TextureParameters& texParameters );
135
138 ~Texture();
139
147 void initialize();
148
154 void initializeNow();
155
161 void destroy();
162
168 void destroyNow();
169
171 inline std::string getName() const { return m_textureParameters.name; }
173 inline void setName( const std::string& name ) { m_textureParameters.name = name; }
174
176 GLenum getFormat() const { return m_textureParameters.image.format; }
177
179 size_t getWidth() const { return m_textureParameters.image.width; }
180
182 size_t getHeight() const { return m_textureParameters.image.height; }
183
185 size_t getDepth() const { return m_textureParameters.image.depth; }
186
188 const void* getTexels() { return m_textureParameters.image.getTexels(); }
189
197 globjects::Texture* getGpuTexture() const { return m_texture.get(); }
198
200 const TextureParameters& getParameters() const { return m_textureParameters; }
202 TextureParameters& getParameters() { return m_textureParameters; }
203
212 void updateData( std::shared_ptr<void> newData );
213
225 void resize( size_t w = 1, size_t h = 1, size_t d = 1, std::shared_ptr<void> pix = nullptr );
226
233 void setParameters( const TextureParameters& textureParameters );
235 void setImageParameters( const ImageParameters& imageParameters );
237 void setSamplerParameters( const SamplerParameters& samplerParameters );
238
244 void bind( int unit = -1 );
245
255 void bindImageTexture( int unit, GLint level, GLboolean layered, GLint layer, GLenum access );
256
263 static void linearize( ImageParameters& image );
264
268 void registerUpdateImageDataTask();
269
273 void registerUpdateSamplerParametersTask();
274
276 void sendImageDataToGpu();
277 void readFromGpu( int level = 0 );
278
280 void sendSamplerParametersToGpu();
281
282 private:
289 bool isSupportedTarget();
290
292 void computeIsMipMappedFlag();
293
299 bool createTexture();
300
312 static void srgbToLinearRgb( uint8_t* texels,
313 uint width,
314 uint height,
315 uint depth,
316 uint numComponent,
317 bool hasAlphaChannel );
318
320 static void linearizeCubeMap( ImageParameters& image, uint numComponent, bool hasAlphaChannel );
321
322 TextureParameters m_textureParameters;
324 bool m_isMipMapped { false };
329 Core::TaskQueue::TaskId m_updateImageTaskId;
330
334 Core::TaskQueue::TaskId m_updateSamplerTaskId;
335
337 std::mutex m_updateMutex;
338};
339} // namespace Data
340} // namespace Engine
341} // namespace Ra
Utils::Index TaskId
Identifier for a task in the task queue.
Definition TaskQueue.hpp:51
Represent a Texture of the engine.
Definition Texture.hpp:120
size_t getWidth() const
Definition Texture.hpp:179
size_t getDepth() const
Definition Texture.hpp:185
void setName(const std::string &name)
Definition Texture.hpp:173
void operator=(const Texture &)=delete
Textures are not copyable, delete operator =.
globjects::Texture * getGpuTexture() const
Get ptr to the managed globjects::Texture.
Definition Texture.hpp:197
const TextureParameters & getParameters() const
get read access to texture parameters
Definition Texture.hpp:200
std::string getName() const
Definition Texture.hpp:171
const void * getTexels()
Definition Texture.hpp:188
GLenum getFormat() const
Definition Texture.hpp:176
size_t getHeight() const
Definition Texture.hpp:182
Texture(const Texture &)=delete
Textures are not copyable, delete copy constructor.
TextureParameters & getParameters()
read/write access to texture parameters
Definition Texture.hpp:202
T get(T... args)
hepler function to manage enum as underlying types in VariableSet
Definition Cage.cpp:3
bool isLinear
set to true when linearize texture rgb component. If true, linearize has no effect.
Definition Texture.hpp:80
const CubeMapType getCubeMap() const
Definition Texture.hpp:65
std::shared_ptr< void > ImageType
Types for texels variant.
Definition Texture.hpp:42
GLenum internalFormat
OpenGL internal format (WARNING, for Integer textures, must be GL_XXX_INTEGER)
Definition Texture.hpp:77
const void * getTexels() const
Definition Texture.hpp:60
bool isTexelOfType() const
check which type is held by texels
Definition Texture.hpp:47
GPU Sampler configuration.
Definition Texture.hpp:22
GLenum wrapT
OpenGL wrap mode in the t direction.
Definition Texture.hpp:26
GLenum minFilter
OpenGL minification filter ( GL_LINEAR or GL_NEAREST or GL_XXX_MIPMAP_YYY )
Definition Texture.hpp:30
GLenum wrapS
OpenGL wrap mode in the s direction.
Definition Texture.hpp:24
GLenum wrapR
OpenGL wrap mode in the r direction.
Definition Texture.hpp:28
GLenum magFilter
OpenGL magnification filter ( GL_LINEAR or GL_NEAREST )
Definition Texture.hpp:32
Describes the sampler and image of a texture.
Definition Texture.hpp:99