Debian Codegraph @

Sources/nCine/Graphics/RHI/GX/GxTexture.h

raw
1 #pragma once
2
3 #include "../RhiTypes.h"
4
5 #include <cstddef>
6 #include <cstdint>
7 #include <vector>
8
9 #include <Containers/StringView.h>
10
11 #include <gccore.h>
12
13 using namespace Death::Containers;
14
15 namespace nCine::RHI::GX
16 {
17 /**
18 @brief Texture object of the GX backend (aliased as `RHI::Texture`)
19
20 Keeps two representations of level 0: a LINEAR host store in the uploaded pixel format (the same
21 native R8/RG8/RGBA8 layout the software backend uses - needed for sub-updates, readback and the
22 palette interception below), and a TILED GX store in main memory referenced by a `GXTexObj`:
23 - R8 index textures tile to `GX_TF_CI8`; the palette (TLUT) is selected per draw by the device
24 from the instance's `palOffset`, which is what makes runtime palette remaps work fixed-function.
25 - RGBA8 / RGB8 textures tile to `GX_TF_RGBA8`.
26 - RG8 (index + per-pixel alpha) has no GX equivalent - a CI8 texel's alpha can only come from the
27 palette entry. Those textures keep only the linear store; the device CPU-bakes an RGBA8 tiled
28 copy per palette row on first use (see `EnsureBakedRgba()`), cached until the palette row or the
29 texel content changes. Only a small set of translucent indexed sprites uses RG8.
30 The shared 256x256 palette texture (labelled "Palettes") is intercepted: it keeps only the linear
31 store, and the device turns its rows into TLUTs on demand.
32
33 Mip levels above 0 and compressed formats are accepted but not stored.
34 */
35 class GxTexture
36 {
37 public:
38 /** @brief Number of texture units tracked by the device */
39 static constexpr std::uint32_t MaxTextureUnits = 8;
40
41 explicit GxTexture(TextureTarget target);
42 ~GxTexture();
43
44 GxTexture(const GxTexture&) = delete;
45 GxTexture& operator=(const GxTexture&) = delete;
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 _handle;
50 }
51 /** @brief Returns the texture target */
52 inline TextureTarget GetTarget() const {
53 return _target;
54 }
55
56 /** @brief Returns the width of level 0 in texels */
57 inline std::int32_t GetWidth() const {
58 return _width;
59 }
60 /** @brief Returns the height of level 0 in texels */
61 inline std::int32_t GetHeight() const {
62 return _height;
63 }
64 /** @brief Returns the pixel format of the linear host store (native, like the software backend) */
65 inline PixelFormat GetFormat() const {
66 return _format;
67 }
68 /** @brief Returns the original upload format (R8/RG8 kept so the palette path can tell them apart) */
69 inline PixelFormat GetUploadFormat() const {
70 return _uploadFormat;
71 }
72 /** @brief Returns the byte distance between two consecutive rows of the linear host store */
73 inline std::int32_t GetStrideBytes() const {
74 return _strideBytes;
75 }
76 /** @brief Returns the base pointer of the linear host store (may be `nullptr` before an upload) */
77 inline const std::uint8_t* GetPixels(std::int32_t level = 0) const {
78 static_cast<void>(level);
79 return _pixels.empty() ? nullptr : _pixels.data();
80 }
81 /** @brief Returns a writable base pointer of the linear host store (for render-target readback) */
82 inline std::uint8_t* MutablePixels() {
83 return _pixels.empty() ? nullptr : _pixels.data();
84 }
85 /** @brief Returns the horizontal texture-coordinate wrap mode */
86 inline SamplerWrapping GetWrapS() const {
87 return _wrap;
88 }
89 /** @brief Returns the vertical texture-coordinate wrap mode (single stored mode) */
90 inline SamplerWrapping GetWrapT() const {
91 return _wrap;
92 }
93 /** @brief Returns the four-channel sampling swizzle (identity by default; informational on GX) */
94 inline const SwizzleChannel* GetSwizzle() const {
95 return _swizzle;
96 }
97 /** @brief Returns the magnification filter */
98 inline nCine::SamplerFilter GetMagFiltering() const {
99 return _magFilter;
100 }
101 /** @brief Alias of @ref GetMagFiltering() */
102 inline nCine::SamplerFilter GetMagFilter() const {
103 return _magFilter;
104 }
105 /** @brief Returns `true` if the texture is bound as a color render target (EFB copy destination) */
106 inline bool IsRenderTarget() const {
107 return _isRenderTarget;
108 }
109 /** @brief Marks the texture as (or no longer as) a color render target; becoming one allocates an RGBA8 tiled store to copy the EFB into */
110 void SetRenderTarget(bool isRenderTarget);
111 /** @brief Returns a globally monotonic stamp of the texel store, advanced by every allocation or upload */
112 inline std::uint32_t GetContentVersion() const {
113 return _contentVersion;
114 }
115
116 /** @brief Returns `true` when the tiled store is a CI8 index texture (draws need a TLUT bound) */
117 inline bool IsIndexed() const {
118 return (_uploadFormat == PixelFormat::R8);
119 }
120 /** @brief Returns `true` when the texture needs the per-palette-row CPU bake (RG8 index + alpha) */
121 inline bool NeedsPaletteBake() const {
122 return (_uploadFormat == PixelFormat::RG8);
123 }
124 /** @brief Returns `true` when this is the intercepted shared palette texture (rows become TLUTs) */
125 inline bool IsPaletteTexture() const {
126 return _isPaletteTexture;
127 }
128
129 /**
130 * @brief Returns the GX texture object of the tiled store, or `nullptr` when none exists
131 *
132 * Valid for CI8 and RGBA8 textures and for render targets; `nullptr` for RG8 (use
133 * @ref EnsureBakedRgba()) and for the palette texture (rows are TLUTs, not a texture).
134 */
135 GXTexObj* GetTexObj();
136
137 /**
138 * @brief Returns a GX texture object of an RGBA8 copy of an RG8 store baked through one palette row
139 *
140 * The bake resolves each texel's palette index through @p paletteRow (256 RGBA8 entries) and takes
141 * alpha from the texel's own alpha byte. One baked copy is cached; it is rebuilt when a different
142 * palette row, a newer palette generation or newer texel content is requested.
143 */
144 GXTexObj* EnsureBakedRgba(const std::uint32_t* paletteRow, std::uint32_t paletteRowIndex, std::uint32_t paletteGeneration, const void* palette);
145
146 /** @brief Binds the texture to the specified texture unit on the device */
147 bool Bind(std::uint32_t textureUnit) const;
148 /** @brief Binds the texture to texture unit 0 */
149 inline bool Bind() const {
150 return Bind(0);
151 }
152 /** @brief Unbinds the texture from the unit it was last bound to */
153 bool Unbind() const;
154 /** @brief Unbinds any texture from the specified texture unit */
155 static bool Unbind(std::uint32_t textureUnit);
156
157 /** @brief Allocates level-0 storage of the given format/size and optionally uploads its texels */
158 void TexImage2D(std::int32_t level, PixelFormat format, bool bgr, std::int32_t width, std::int32_t height, const void* data);
159 /** @brief Updates a rectangular subregion of level 0 */
160 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);
161 /** @brief Allocates immutable level-0 storage of the given format/size (no texels yet) */
162 void TexStorage2D(std::int32_t levels, PixelFormat format, std::int32_t width, std::int32_t height);
163 /** @brief Compressed upload (unsupported, accepted as a no-op) */
164 void CompressedTexImage2D(std::int32_t level, PixelFormat format, std::int32_t width, std::int32_t height, std::int32_t imageSize, const void* data);
165 /** @brief Compressed sub-upload (unsupported, accepted as a no-op) */
166 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);
167 /** @brief Reads back level-0 texels of the linear host store into client memory */
168 void GetTexImage(std::int32_t level, PixelFormat format, bool bgr, void* pixels);
169
170 /** @brief Sets the minification filter */
171 void SetMinFiltering(nCine::SamplerFilter filter);
172 /** @brief Sets the magnification filter */
173 void SetMagFiltering(nCine::SamplerFilter filter);
174 /** @brief Sets the wrap mode */
175 void SetWrap(SamplerWrapping wrap);
176 /** @brief Sets the sampling swizzle (stored, informational - the palette path keys off the upload format instead) */
177 void SetSwizzle(SwizzleChannel r, SwizzleChannel g, SwizzleChannel b, SwizzleChannel a);
178 /** @brief Sets the highest defined mipmap level (ignored) */
179 void SetMaxLevel(std::int32_t maxLevel);
180 /** @brief Sets the client pixel-row alignment of uploads (ignored, uploads are tightly packed) */
181 static void SetUnpackAlignment(std::int32_t alignment);
182
183 /** @brief Sets a debug label; "Palettes" marks the shared palette texture whose rows become TLUTs */
184 void SetObjectLabel(StringView label);
185
186 static bool SupportsImmutableStorage() {
187 return false;
188 }
189 static bool SupportsTextureReadback() {
190 return true;
191 }
192 static void ClearErrors() {}
193 static bool CheckErrors() {
194 return false;
195 }
196 static void CheckFormatSupport(PixelFormat format) {
197 static_cast<void>(format);
198 }
199
200 /** @brief Returns the number of bytes occupied by one texel of the given format (0 if unsupported) */
201 static std::int32_t BytesPerPixel(PixelFormat format);
202
203 /** @brief Returns the base pointer of the tiled render-target store the EFB is copied into (render targets only) */
204 inline std::uint8_t* GetRenderTargetStore() {
205 return _tiledStore;
206 }
207
208 private:
209 static std::uint32_t _nextHandle;
210 static std::uint32_t _nextContentVersion;
211
212 std::uint32_t _handle;
213 std::uint32_t _contentVersion;
214 TextureTarget _target;
215 PixelFormat _format;
216 PixelFormat _uploadFormat;
217 std::int32_t _width;
218 std::int32_t _height;
219 std::int32_t _strideBytes;
220 std::int32_t _bytesPerPixel;
221 nCine::SamplerFilter _minFilter;
222 nCine::SamplerFilter _magFilter;
223 SamplerWrapping _wrap;
224 SwizzleChannel _swizzle[4];
225 mutable std::uint32_t _textureUnit;
226 std::vector<std::uint8_t> _pixels;
227 bool _isRenderTarget;
228 bool _isPaletteTexture;
229
230 // Tiled GX store (32-byte aligned main memory) + its texture object, valid when _tiledStore != nullptr
231 std::uint8_t* _tiledStore;
232 std::size_t _tiledStoreSize;
233 GXTexObj _texObj;
234 bool _texObjValid;
235
236 // Per-palette-row baked RGBA8 copies of an RG8 store (see EnsureBakedRgba). One copy is kept per
237 // palette row: the GX FIFO consumes draws asynchronously, so rebaking a row that an already
238 // submitted quad references would corrupt that quad (several rows are commonly alive within one
239 // frame, e.g. text and its shadow)
240 static constexpr std::int32_t BakedSlotCount = 8;
241 struct BakedSlot {
242 std::uint8_t* Store;
243 GXTexObj TexObj;
244 bool Valid;
245 std::uint32_t PaletteRow;
246 std::uint32_t PaletteGeneration;
247 std::uint32_t ContentVersion;
248 std::uint32_t LastUsedFrame;
249 const void* Palette;
250 };
251 BakedSlot _bakedSlots[BakedSlotCount];
252 std::int32_t _nextBakedSlot;
253
254 void Allocate(PixelFormat format, std::int32_t width, std::int32_t height);
255 void RefreshTiledStore();
256 void InitTexObj(GXTexObj& obj, void* store, std::uint8_t gxFormat, bool ci);
257 void FreeTiledStores();
258 };
259 }