| 1 |
#pragma once |
| 2 |
|
| 3 |
#include "GLHashMap.h" |
| 4 |
#include "../RhiTypes.h" |
| 5 |
|
| 6 |
#include <Containers/StringView.h> |
| 7 |
|
| 8 |
using namespace Death::Containers; |
| 9 |
|
| 10 |
namespace nCine |
| 11 |
{ |
| 12 |
class ImGuiDrawing; |
| 13 |
} |
| 14 |
|
| 15 |
namespace nCine::RHI::GL |
| 16 |
{ |
| 17 |
/** |
| 18 |
@brief Wraps an OpenGL texture object |
| 19 |
|
| 20 |
Manages the lifetime of a single OpenGL texture object of a given @ref TextureTarget. Binding is |
| 21 |
cached per texture unit, and the currently active unit is tracked, so that redundant |
| 22 |
`glActiveTexture()` and `glBindTexture()` calls are skipped. Provides image upload, immutable |
| 23 |
storage allocation, readback and parameter setting through a backend-neutral surface (aliased as |
| 24 |
`RHI::Texture`): all OpenGL formats, filters, wrap and swizzle enums are translated inside the |
| 25 |
backend, so callers only ever pass @ref PixelFormat, @ref SamplerFilter, @ref SamplerWrapping and |
| 26 |
@ref SwizzleChannel values. |
| 27 |
*/ |
| 28 |
class GLTexture |
| 29 |
{ |
| 30 |
friend class GLFramebuffer; |
| 31 |
friend class nCine::ImGuiDrawing; |
| 32 |
|
| 33 |
public: |
| 34 |
/** @brief Number of texture units tracked by the bind cache */ |
| 35 |
static constexpr std::uint32_t MaxTextureUnits = 8; |
| 36 |
|
| 37 |
explicit GLTexture(TextureTarget target); |
| 38 |
~GLTexture(); |
| 39 |
|
| 40 |
GLTexture(const GLTexture&) = delete; |
| 41 |
GLTexture& operator=(const GLTexture&) = delete; |
| 42 |
|
| 43 |
/** @brief Returns the OpenGL handle of the texture object */ |
| 44 |
inline GLuint GetGLHandle() const { |
| 45 |
return _glHandle; |
| 46 |
} |
| 47 |
/** @brief Returns a backend-neutral identifier uniquely identifying the texture (feeds material sort keys) */ |
| 48 |
inline std::uint32_t GetUniqueId() const { |
| 49 |
return _glHandle; |
| 50 |
} |
| 51 |
/** @brief Returns the target of the texture (e.g., `GL_TEXTURE_2D`) */ |
| 52 |
inline GLenum GetTarget() const { |
| 53 |
return _target; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* @brief Binds the texture to the specified texture unit |
| 58 |
* |
| 59 |
* @return `true` if a `glBindTexture()` call was issued, `false` if it was already bound |
| 60 |
*/ |
| 61 |
bool Bind(std::uint32_t textureUnit) const; |
| 62 |
/** @brief Binds the texture to texture unit 0 */ |
| 63 |
inline bool Bind() const { |
| 64 |
return Bind(0); |
| 65 |
} |
| 66 |
/** @brief Unbinds the texture from the unit it was last bound to */ |
| 67 |
bool Unbind() const; |
| 68 |
/** @brief Unbinds any texture of the given target from the specified texture unit */ |
| 69 |
static bool Unbind(GLenum target, std::uint32_t textureUnit); |
| 70 |
/** @brief Unbinds any `GL_TEXTURE_2D` texture from the specified texture unit */ |
| 71 |
static bool Unbind(std::uint32_t textureUnit); |
| 72 |
|
| 73 |
/** @brief Specifies a two-dimensional image for the given mipmap level */ |
| 74 |
void TexImage2D(std::int32_t level, PixelFormat format, bool bgr, std::int32_t width, std::int32_t height, const void* data); |
| 75 |
/** @brief Updates a rectangular subregion of a two-dimensional mipmap level */ |
| 76 |
void TexSubImage2D(std::int32_t level, std::int32_t xoffset, std::int32_t yoffset, std::int32_t width, std::int32_t height, PixelFormat format, bool bgr, const void* data); |
| 77 |
/** @brief Specifies a compressed two-dimensional image for the given mipmap level */ |
| 78 |
void CompressedTexImage2D(std::int32_t level, PixelFormat format, std::int32_t width, std::int32_t height, std::int32_t imageSize, const void* data); |
| 79 |
/** @brief Updates a rectangular subregion of a compressed two-dimensional mipmap level */ |
| 80 |
void CompressedTexSubImage2D(std::int32_t level, std::int32_t xoffset, std::int32_t yoffset, std::int32_t width, std::int32_t height, PixelFormat format, std::int32_t imageSize, const void* data); |
| 81 |
/** |
| 82 |
@brief Allocates an immutable two-dimensional storage with the given number of mipmap levels |
| 83 |
|
| 84 |
A format that resolves to an unsized internal format is allocated per level through |
| 85 |
@ref TexImage2D() instead, because immutable storage is defined for sized formats only --- the |
| 86 |
texture stays mutable, which callers cannot tell apart, as the upload goes through |
| 87 |
@ref TexSubImage2D() either way. See @ref GLTextureFormat::IsUnsizedInternalFormat(). |
| 88 |
*/ |
| 89 |
void TexStorage2D(std::int32_t levels, PixelFormat format, std::int32_t width, std::int32_t height); |
| 90 |
|
| 91 |
/** @brief Reads back a mipmap level of the texture into client memory (desktop only) */ |
| 92 |
void GetTexImage(std::int32_t level, PixelFormat format, bool bgr, void* pixels); |
| 93 |
|
| 94 |
/** @brief Sets the minification filtering mode */ |
| 95 |
void SetMinFiltering(SamplerFilter filter); |
| 96 |
/** @brief Sets the magnification filtering mode */ |
| 97 |
void SetMagFiltering(SamplerFilter filter); |
| 98 |
/** @brief Sets the wrapping mode for both `s` and `t` coordinates */ |
| 99 |
void SetWrap(SamplerWrapping wrap); |
| 100 |
/** |
| 101 |
@brief Remaps the channels returned when the texture is sampled |
| 102 |
|
| 103 |
A no-op on the ES2 profile and on WebGL, neither of which exposes `GL_TEXTURE_SWIZZLE_*` --- there |
| 104 |
@ref GLTextureFormat::Resolve() answers @ref PixelFormat::RG8 with `LUMINANCE_ALPHA` instead, which |
| 105 |
samples as `(L,L,L,A)` without one, see @ref nCine::Texture::SetSwizzle(). |
| 106 |
*/ |
| 107 |
void SetSwizzle(SwizzleChannel r, SwizzleChannel g, SwizzleChannel b, SwizzleChannel a); |
| 108 |
/** @brief Sets the highest mipmap level that is defined */ |
| 109 |
void SetMaxLevel(std::int32_t maxLevel); |
| 110 |
/** @brief Sets the byte alignment of client pixel rows read by the upload calls */ |
| 111 |
static void SetUnpackAlignment(std::int32_t alignment); |
| 112 |
|
| 113 |
/** @brief Sets a floating-point texture parameter */ |
| 114 |
void TexParameterf(GLenum pname, GLfloat param); |
| 115 |
/** @brief Sets an integer texture parameter (e.g., filter or wrap mode) */ |
| 116 |
void TexParameteri(GLenum pname, GLint param); |
| 117 |
|
| 118 |
/** @brief Sets an OpenGL object label for the texture, for debugging */ |
| 119 |
void SetObjectLabel(StringView label); |
| 120 |
|
| 121 |
/** @brief Returns `true` if immutable texture storage (`TexStorage2D`) is available */ |
| 122 |
static bool SupportsImmutableStorage(); |
| 123 |
/** @brief Returns `true` if the device can read texel data back into client memory */ |
| 124 |
static bool SupportsTextureReadback(); |
| 125 |
|
| 126 |
/** @brief Discards any pending device error state */ |
| 127 |
static void ClearErrors(); |
| 128 |
/** @brief Returns `true` if the device reported an error since the last @ref ClearErrors(), and clears it */ |
| 129 |
static bool CheckErrors(); |
| 130 |
|
| 131 |
/** @brief Verifies that the device supports the given pixel format, aborting if it does not */ |
| 132 |
static void CheckFormatSupport(PixelFormat format); |
| 133 |
|
| 134 |
/** @brief Drops the cached unit/texture bindings so the next binds re-apply (external code changed them) */ |
| 135 |
static void InvalidateCachedBindings(); |
| 136 |
|
| 137 |
private: |
| 138 |
static class GLHashMap<GLTextureMappingFunc::Size, GLTextureMappingFunc> _boundTextures[MaxTextureUnits]; |
| 139 |
static std::uint32_t _boundUnit; |
| 140 |
|
| 141 |
GLuint _glHandle; |
| 142 |
GLenum _target; |
| 143 |
/** |
| 144 |
* @brief Texture unit the texture was last bound to |
| 145 |
* |
| 146 |
* Mutable so that `const` texture objects can still be bound to a texture unit. |
| 147 |
*/ |
| 148 |
mutable std::uint32_t _textureUnit; |
| 149 |
|
| 150 |
static bool BindHandle(GLenum target, GLuint glHandle, std::uint32_t textureUnit); |
| 151 |
static bool BindHandle(GLenum target, GLuint glHandle) { |
| 152 |
return BindHandle(target, glHandle, 0); |
| 153 |
} |
| 154 |
|
| 155 |
static GLuint GetBoundHandle(GLenum target, unsigned int textureUnit); |
| 156 |
static GLuint GetBoundHandle(GLenum target) { |
| 157 |
return GetBoundHandle(target, 0); |
| 158 |
} |
| 159 |
}; |
| 160 |
} |