Debian Codegraph @

Sources/nCine/Graphics/RHI/GXM/GxmTexture.h

raw
1 #pragma once
2
3 #include "GxmMemory.h"
4 #include "../RhiTypes.h"
5
6 #include <cstddef>
7 #include <cstdint>
8
9 #include <Containers/SmallVector.h>
10 #include <Containers/StringView.h>
11
12 #include <psp2/gxm.h>
13
14 using namespace Death::Containers;
15
16 namespace nCine::RHI::GXM
17 {
18 /**
19 @brief Texture object of the sceGxm backend (aliased as `RHI::Texture`)
20
21 Exposes the neutral upload surface `Texture.cpp` drives (`TexImage2D`, `TexSubImage2D`,
22 `TexStorage2D`, filter/wrap/swizzle setters) and keeps the texels twice: once in a host store the
23 uploads write into, and once in the GPU-visible copy the hardware samples, refreshed from the host
24 store whenever it went stale. Every format is promoted to RGBA8 in both, which keeps one texture
25 format on the GPU side and lets the sampling swizzle be baked into the texels (sceGxm can express a
26 channel swizzle in the texture format, but only for a fixed set of patterns).
27
28 A texture bound as a colour attachment is the exception: the GPU writes it, so its GPU-visible copy
29 *is* the truth and the host store is only kept for a readback. That is also why the two live in
30 separate allocations at all - the render target's surface has to stay put across frames while the
31 host store may be reallocated by an upload.
32
33 The GPU copy is laid out linearly, in one of the two layouts sceGxm offers for that:
34
35 - `SCE_GXM_TEXTURE_LINEAR` when the width is a multiple of 8, which the hardware needs because a
36 linear texture has no stride of its own (see `SceGxmTexture`: its control words carry only a width
37 and a height) and derives the row pitch from the width. This is the fully featured layout - every
38 addressing mode, mip filtering - and is what the atlases and the power-of-two render targets get.
39 - `SCE_GXM_TEXTURE_LINEAR_STRIDED` otherwise, which carries an explicit stride and so accepts any
40 width, at the cost of mip filtering and a separate minification filter (the hardware rejects both
41 with `SCE_GXM_ERROR_UNSUPPORTED` on a strided texture, so they are not programmed for one).
42
43 A colour attachment needs its stride padded to a multiple of 8 texels on top of that, because
44 `sceGxmColorSurfaceInit()` rejects anything else with `SCE_GXM_ERROR_INVALID_ALIGNMENT` - which is why
45 a render target whose width is not already a multiple of 8 is always a strided texture.
46 */
47 class GxmTexture
48 {
49 public:
50 /** @brief Number of texture units tracked by the device */
51 static constexpr std::uint32_t MaxTextureUnits = 8;
52
53 explicit GxmTexture(TextureTarget target);
54 ~GxmTexture();
55
56 GxmTexture(const GxmTexture&) = delete;
57 GxmTexture& operator=(const GxmTexture&) = delete;
58
59 /** @brief Returns the sceGxm texture control structure, materializing the GPU copy if needed, or `nullptr` */
60 const SceGxmTexture* GetGxmTexture() const;
61 /** @brief Returns the GPU-visible base address of the texels, allocating the copy if needed (used by @ref GxmRenderTarget) */
62 void* GetSurfaceData() const;
63 /** @brief Returns the byte distance between two rows of the GPU copy (its surface stride) */
64 inline std::uint32_t GetSurfaceStride() const {
65 return _gpuStride;
66 }
67 /** @brief Releases the GPU-visible copy (on re-allocation and destruction) */
68 void ReleaseGpu() const;
69
70 /** @brief Returns a backend-neutral identifier uniquely identifying the texture (feeds material sort keys) */
71 inline std::uint32_t GetUniqueId() const {
72 return _handle;
73 }
74 /** @brief Returns the texture target */
75 inline TextureTarget GetTarget() const {
76 return _target;
77 }
78
79 /** @brief Returns the width of level 0 in texels */
80 inline std::int32_t GetWidth() const {
81 return _width;
82 }
83 /** @brief Returns the height of level 0 in texels */
84 inline std::int32_t GetHeight() const {
85 return _height;
86 }
87 /** @brief Returns the pixel format of the stored texels (always RGBA8 after the promotion) */
88 inline PixelFormat GetFormat() const {
89 return _format;
90 }
91 /** @brief Returns the original upload format before promotion (R8/RG8/RGB8 kept) */
92 inline PixelFormat GetUploadFormat() const {
93 return _uploadFormat;
94 }
95 /** @brief Returns the byte distance between two consecutive rows of the host store */
96 inline std::int32_t GetStrideBytes() const {
97 return _strideBytes;
98 }
99 /** @brief Returns the base pointer of the level-0 host store (may be `nullptr` before an upload); the single-level store ignores @p level */
100 inline const std::uint8_t* GetPixels(std::int32_t level = 0) const {
101 static_cast<void>(level);
102 return (_pixels.empty() ? nullptr : _pixels.data());
103 }
104 /** @brief Returns the horizontal texture-coordinate wrap mode */
105 inline SamplerWrapping GetWrapS() const {
106 return _wrap;
107 }
108 /** @brief Returns the vertical texture-coordinate wrap mode (single stored mode, same as @ref GetWrapS()) */
109 inline SamplerWrapping GetWrapT() const {
110 return _wrap;
111 }
112 /** @brief Returns the four-channel sampling swizzle (identity by default) */
113 inline const SwizzleChannel* GetSwizzle() const {
114 return _swizzle;
115 }
116 /** @brief Returns the magnification filter (alias of @ref GetMagFiltering()) */
117 inline nCine::SamplerFilter GetMagFilter() const {
118 return _magFilter;
119 }
120 /** @brief Returns `true` if the texture is bound as a color render target */
121 inline bool IsRenderTarget() const {
122 return _isRenderTarget;
123 }
124 /** @brief Marks the texture as (or no longer as) a color render target (the GPU copy is rebuilt, since the GPU writes it from then on) */
125 void SetRenderTarget(bool isRenderTarget);
126 /** @brief Returns a writable base pointer of the level-0 host store */
127 inline std::uint8_t* MutablePixels() {
128 return (_pixels.empty() ? nullptr : _pixels.data());
129 }
130
131 /** @brief Binds the texture to the specified texture unit on the device */
132 bool Bind(std::uint32_t textureUnit) const;
133 /** @brief Binds the texture to texture unit 0 */
134 inline bool Bind() const {
135 return Bind(0);
136 }
137 /** @brief Unbinds the texture from the unit it was last bound to */
138 bool Unbind() const;
139 /** @brief Unbinds any texture from the specified texture unit */
140 static bool Unbind(std::uint32_t textureUnit);
141
142 /** @brief Allocates level-0 storage of the given format/size and optionally uploads its texels */
143 void TexImage2D(std::int32_t level, PixelFormat format, bool bgr, std::int32_t width, std::int32_t height, const void* data);
144 /** @brief Updates a rectangular subregion of level 0 */
145 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);
146 /** @brief Allocates immutable level-0 storage of the given format/size (no texels yet) */
147 void TexStorage2D(std::int32_t levels, PixelFormat format, std::int32_t width, std::int32_t height);
148 /** @brief Compressed upload (unsupported by this backend, accepted as a no-op) */
149 void CompressedTexImage2D(std::int32_t level, PixelFormat format, std::int32_t width, std::int32_t height, std::int32_t imageSize, const void* data);
150 /** @brief Compressed sub-upload (unsupported by this backend, accepted as a no-op) */
151 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);
152 /** @brief Reads back level-0 texels into client memory (from the GPU copy when the texture is a render target) */
153 void GetTexImage(std::int32_t level, PixelFormat format, bool bgr, void* pixels);
154
155 /** @brief Sets the minification filter */
156 void SetMinFiltering(nCine::SamplerFilter filter);
157 /** @brief Sets the magnification filter */
158 void SetMagFiltering(nCine::SamplerFilter filter);
159 /** @brief Sets the wrap mode */
160 void SetWrap(SamplerWrapping wrap);
161 /** @brief Sets the sampling swizzle (baked into the GPU copy's texels) */
162 void SetSwizzle(SwizzleChannel r, SwizzleChannel g, SwizzleChannel b, SwizzleChannel a);
163 /** @brief Sets the highest defined mipmap level (ignored, the store is single-level) */
164 void SetMaxLevel(std::int32_t maxLevel);
165 /** @brief Sets the client pixel-row alignment of uploads (ignored, uploads are tightly packed) */
166 static void SetUnpackAlignment(std::int32_t alignment);
167
168 /** @brief Sets a debug label for the texture (ignored) */
169 void SetObjectLabel(StringView label);
170
171 /** @brief Returns the magnification filter */
172 inline nCine::SamplerFilter GetMagFiltering() const {
173 return _magFilter;
174 }
175
176 static bool SupportsImmutableStorage() {
177 return false;
178 }
179 static bool SupportsTextureReadback() {
180 return true;
181 }
182 static void ClearErrors() {}
183 static bool CheckErrors() {
184 return false;
185 }
186 static void CheckFormatSupport(PixelFormat format) {
187 static_cast<void>(format);
188 }
189
190 /** @brief Returns the number of bytes occupied by one texel of the given format (0 if unsupported) */
191 static std::int32_t BytesPerPixel(PixelFormat format);
192
193 private:
194 static std::uint32_t _nextHandle;
195
196 std::uint32_t _handle;
197 TextureTarget _target;
198 PixelFormat _format;
199 PixelFormat _uploadFormat;
200 std::int32_t _width;
201 std::int32_t _height;
202 std::int32_t _strideBytes;
203 nCine::SamplerFilter _minFilter;
204 nCine::SamplerFilter _magFilter;
205 SamplerWrapping _wrap;
206 SwizzleChannel _swizzle[4];
207 mutable std::uint32_t _textureUnit;
208 SmallVector<std::uint8_t, 0> _pixels;
209 bool _isRenderTarget;
210
211 // GPU-visible copy, created lazily on the first bind (mutable so the const bind-time accessors can
212 // materialize it). `_contentsDirty` forces a refresh from the host store, `_samplerDirty` only a
213 // filter/wrap update of the control structure
214 mutable GxmMemory::Block _gpuBlock;
215 mutable SceGxmTexture _gpuTexture;
216 mutable std::uint32_t _gpuStride;
217 mutable bool _gpuStrided; // the GPU copy uses SCE_GXM_TEXTURE_LINEAR_STRIDED
218 mutable bool _gpuValid;
219 mutable bool _contentsDirty;
220 mutable bool _samplerDirty;
221
222 void Allocate(PixelFormat format, std::int32_t width, std::int32_t height);
223 /** @brief (Re)creates the GPU copy and its control structure when missing, then refreshes it if stale */
224 bool EnsureGpuTexture() const;
225 /** @brief Applies the tracked filter and wrap modes to the control structure */
226 void ApplySamplerState() const;
227 /** @brief Copies the host store into the GPU copy, baking the swizzle and the row padding in */
228 void UploadPixels() const;
229 /** @brief Returns `true` if @ref _swizzle is the identity mapping (R,G,B,A) */
230 bool IsIdentitySwizzle() const;
231 };
232 }