| 1 |
#pragma once |
| 2 |
|
| 3 |
#include "../RhiTypes.h" |
| 4 |
|
| 5 |
#include <cstddef> |
| 6 |
#include <cstdint> |
| 7 |
|
| 8 |
#include <Containers/SmallVector.h> |
| 9 |
#include <Containers/StringView.h> |
| 10 |
|
| 11 |
#include <dc/pvr.h> |
| 12 |
|
| 13 |
using namespace Death::Containers; |
| 14 |
|
| 15 |
namespace nCine::RHI::PVR |
| 16 |
{ |
| 17 |
/** |
| 18 |
@brief Texture object of the PVR backend (aliased as `RHI::Texture`) |
| 19 |
|
| 20 |
Keeps a LINEAR host store of level 0 in the uploaded pixel format (the same native R8/RG8/RGBA8 |
| 21 |
layout the software backend uses) plus a VRAM store padded up to the PowerVR's power-of-two |
| 22 |
dimension requirement (the padding is compensated by @ref GetUScale() / @ref GetVScale(), which the |
| 23 |
device multiplies into every submitted texture coordinate): |
| 24 |
- R8 index textures upload as twiddled 8bpp paletted texels; the palette BANK is selected per draw |
| 25 |
by the device from the instance's `palOffset` (4 hardware banks of 256 ARGB8888 entries). |
| 26 |
- RGBA8 / RGB8 textures convert to twiddled ARGB4444. |
| 27 |
- RG8 (index + per-pixel alpha) has no paletted PVR equivalent (a paletted texel's alpha comes from |
| 28 |
the palette entry), so those keep only the linear store and the device requests a per-palette-row |
| 29 |
CPU bake to ARGB4444 (@ref EnsureBakedArgb4444), cached until the row or texel content changes. |
| 30 |
The shared 256x256 palette texture (labelled "Palettes") is intercepted: it keeps only the linear |
| 31 |
store and the device loads its rows into the hardware palette banks on demand. |
| 32 |
|
| 33 |
Render targets allocate a non-twiddled RGB565 VRAM surface the tile accelerator renders into |
| 34 |
(`pvr_scene_begin_txr`). Mip levels above 0 and compressed formats are accepted but not stored. |
| 35 |
*/ |
| 36 |
class PvrTexture |
| 37 |
{ |
| 38 |
public: |
| 39 |
/** @brief Number of texture units tracked by the device */ |
| 40 |
static constexpr std::uint32_t MaxTextureUnits = 8; |
| 41 |
|
| 42 |
explicit PvrTexture(TextureTarget target); |
| 43 |
~PvrTexture(); |
| 44 |
|
| 45 |
PvrTexture(const PvrTexture&) = delete; |
| 46 |
PvrTexture& operator=(const PvrTexture&) = delete; |
| 47 |
|
| 48 |
/** @brief Returns a backend-neutral identifier uniquely identifying the texture (feeds material sort keys) */ |
| 49 |
inline std::uint32_t GetUniqueId() const { |
| 50 |
return _handle; |
| 51 |
} |
| 52 |
/** @brief Returns the texture target */ |
| 53 |
inline TextureTarget GetTarget() const { |
| 54 |
return _target; |
| 55 |
} |
| 56 |
|
| 57 |
/** @brief Returns the width of level 0 in texels */ |
| 58 |
inline std::int32_t GetWidth() const { |
| 59 |
return _width; |
| 60 |
} |
| 61 |
/** @brief Returns the height of level 0 in texels */ |
| 62 |
inline std::int32_t GetHeight() const { |
| 63 |
return _height; |
| 64 |
} |
| 65 |
/** @brief Returns the pixel format of the linear host store (native, like the software backend) */ |
| 66 |
inline PixelFormat GetFormat() const { |
| 67 |
return _format; |
| 68 |
} |
| 69 |
/** @brief Returns the original upload format (R8/RG8 kept so the palette path can tell them apart) */ |
| 70 |
inline PixelFormat GetUploadFormat() const { |
| 71 |
return _uploadFormat; |
| 72 |
} |
| 73 |
/** @brief Returns the byte distance between two consecutive rows of the linear host store */ |
| 74 |
inline std::int32_t GetStrideBytes() const { |
| 75 |
return _strideBytes; |
| 76 |
} |
| 77 |
/** |
| 78 |
@brief Returns the base pointer of the linear host store (may be `nullptr` before an upload) |
| 79 |
|
| 80 |
Static R8/RG8 content keeps its host store run-length compressed to save main memory (see |
| 81 |
@ref StorePixels()), in which case there is no linear pointer to hand out and this returns |
| 82 |
`nullptr`. The only external consumer on this backend is the device's palette-texture path, |
| 83 |
and the palette texture is never compressed. |
| 84 |
*/ |
| 85 |
inline const std::uint8_t* GetPixels(std::int32_t level = 0) const { |
| 86 |
static_cast<void>(level); |
| 87 |
return (_pixels.empty() || _pixelsCompressed) ? nullptr : _pixels.data(); |
| 88 |
} |
| 89 |
/** @brief Returns a writable base pointer of the linear host store (`nullptr` when the store is compressed) */ |
| 90 |
inline std::uint8_t* MutablePixels() { |
| 91 |
return (_pixels.empty() || _pixelsCompressed) ? nullptr : _pixels.data(); |
| 92 |
} |
| 93 |
/** @brief Returns the horizontal texture-coordinate wrap mode */ |
| 94 |
inline SamplerWrapping GetWrapS() const { |
| 95 |
return _wrap; |
| 96 |
} |
| 97 |
/** @brief Returns the vertical texture-coordinate wrap mode (single stored mode) */ |
| 98 |
inline SamplerWrapping GetWrapT() const { |
| 99 |
return _wrap; |
| 100 |
} |
| 101 |
/** @brief Returns the four-channel sampling swizzle (identity by default; informational on PVR) */ |
| 102 |
inline const SwizzleChannel* GetSwizzle() const { |
| 103 |
return _swizzle; |
| 104 |
} |
| 105 |
/** @brief Returns the magnification filter */ |
| 106 |
inline nCine::SamplerFilter GetMagFiltering() const { |
| 107 |
return _magFilter; |
| 108 |
} |
| 109 |
/** @brief Alias of @ref GetMagFiltering() */ |
| 110 |
inline nCine::SamplerFilter GetMagFilter() const { |
| 111 |
return _magFilter; |
| 112 |
} |
| 113 |
/** @brief Returns `true` if the texture is bound as a color render target (tile-accelerator RTT surface) */ |
| 114 |
inline bool IsRenderTarget() const { |
| 115 |
return _isRenderTarget; |
| 116 |
} |
| 117 |
/** @brief Marks the texture as (or no longer as) a color render target; becoming one allocates the RTT surface */ |
| 118 |
void SetRenderTarget(bool isRenderTarget); |
| 119 |
/** @brief Returns a globally monotonic stamp of the texel store, advanced by every allocation or upload */ |
| 120 |
inline std::uint32_t GetContentVersion() const { |
| 121 |
return _contentVersion; |
| 122 |
} |
| 123 |
|
| 124 |
/** @brief Returns `true` when the VRAM store is 8bpp paletted (draws need a palette bank selected) */ |
| 125 |
inline bool IsIndexed() const { |
| 126 |
return (_uploadFormat == PixelFormat::R8); |
| 127 |
} |
| 128 |
/** @brief Returns `true` when the texture needs the per-palette-row CPU bake (RG8 index + alpha) */ |
| 129 |
inline bool NeedsPaletteBake() const { |
| 130 |
return (_uploadFormat == PixelFormat::RG8); |
| 131 |
} |
| 132 |
/** @brief Returns `true` when this is the intercepted shared palette texture (rows become palette banks) */ |
| 133 |
inline bool IsPaletteTexture() const { |
| 134 |
return _isPaletteTexture; |
| 135 |
} |
| 136 |
|
| 137 |
/** @brief Returns the VRAM texture pointer, or `nullptr` when none exists (RG8 without a bake, palette texture) */ |
| 138 |
inline pvr_ptr_t GetVramPointer() const { |
| 139 |
return _vram; |
| 140 |
} |
| 141 |
/** |
| 142 |
@brief Returns the VRAM texture pointer, rebuilding the store if it was reclaimed |
| 143 |
|
| 144 |
Should be used by the draw path: running out of video memory drops the least recently used |
| 145 |
stores (see @ref AllocateVram()), and a texture that is drawn again afterwards has to be |
| 146 |
uploaded from its main-memory copy once more. |
| 147 |
*/ |
| 148 |
pvr_ptr_t AcquireVramPointer(); |
| 149 |
/** @brief Returns the PVR texture format word of the VRAM store WITHOUT the palette-bank bits (the device ors them in) */ |
| 150 |
inline std::uint32_t GetVramFormat() const { |
| 151 |
return _vramFormat; |
| 152 |
} |
| 153 |
/** @brief Returns the power-of-two padded VRAM width */ |
| 154 |
inline std::int32_t GetPaddedWidth() const { |
| 155 |
return _paddedWidth; |
| 156 |
} |
| 157 |
/** @brief Returns the power-of-two padded VRAM height */ |
| 158 |
inline std::int32_t GetPaddedHeight() const { |
| 159 |
return _paddedHeight; |
| 160 |
} |
| 161 |
/** @brief Returns the U compensation factor for the power-of-two padding (`realW / paddedW`) */ |
| 162 |
inline float GetUScale() const { |
| 163 |
return _uScale; |
| 164 |
} |
| 165 |
/** @brief Returns the V compensation factor for the power-of-two padding (`realH / paddedH`) */ |
| 166 |
inline float GetVScale() const { |
| 167 |
return _vScale; |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* @brief Returns the VRAM pointer of an ARGB4444 copy of an RG8 store baked through one palette row |
| 172 |
* |
| 173 |
* Analogous to the GX backend's bake: index resolved through @p paletteRow (256 RGBA8 entries), |
| 174 |
* alpha from the texel's own alpha byte; one cached copy, rebuilt when the row/generation/content |
| 175 |
* changes. Returns `nullptr` on allocation failure. |
| 176 |
*/ |
| 177 |
pvr_ptr_t EnsureBakedArgb4444(const std::uint32_t* paletteRow, std::uint32_t paletteRowIndex, std::uint32_t paletteGeneration, const void* palette); |
| 178 |
|
| 179 |
/** |
| 180 |
@brief Returns a writable pointer to the VRAM store, for content that is rebuilt every frame |
| 181 |
|
| 182 |
Answered only for the formats the hardware samples verbatim from video memory (RGB565), which is |
| 183 |
what lets the cinematics build a frame straight where it will be read from instead of into a buffer |
| 184 |
that is then copied into the host store and copied again into video memory. Returns `nullptr` for |
| 185 |
anything else, and for a store that cannot be allocated. |
| 186 |
|
| 187 |
@p strideBytes receives the row pitch, which is the power-of-two padded width rather than the |
| 188 |
texture's own. Nothing written here reaches the host copy, so such a texture must be rewritten in |
| 189 |
full every frame and never read back. |
| 190 |
*/ |
| 191 |
void* MapStreamingTexels(std::int32_t& strideBytes); |
| 192 |
|
| 193 |
/** @brief Binds the texture to the specified texture unit on the device */ |
| 194 |
bool Bind(std::uint32_t textureUnit) const; |
| 195 |
/** @brief Binds the texture to texture unit 0 */ |
| 196 |
inline bool Bind() const { |
| 197 |
return Bind(0); |
| 198 |
} |
| 199 |
/** @brief Unbinds the texture from the unit it was last bound to */ |
| 200 |
bool Unbind() const; |
| 201 |
/** @brief Unbinds any texture from the specified texture unit */ |
| 202 |
static bool Unbind(std::uint32_t textureUnit); |
| 203 |
|
| 204 |
/** @brief Allocates level-0 storage of the given format/size and optionally uploads its texels */ |
| 205 |
void TexImage2D(std::int32_t level, PixelFormat format, bool bgr, std::int32_t width, std::int32_t height, const void* data); |
| 206 |
/** @brief Updates a rectangular subregion of level 0 */ |
| 207 |
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); |
| 208 |
/** @brief Allocates immutable level-0 storage of the given format/size (no texels yet) */ |
| 209 |
void TexStorage2D(std::int32_t levels, PixelFormat format, std::int32_t width, std::int32_t height); |
| 210 |
/** @brief Compressed upload (unsupported, accepted as a no-op) */ |
| 211 |
void CompressedTexImage2D(std::int32_t level, PixelFormat format, std::int32_t width, std::int32_t height, std::int32_t imageSize, const void* data); |
| 212 |
/** @brief Compressed sub-upload (unsupported, accepted as a no-op) */ |
| 213 |
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); |
| 214 |
/** @brief Reads back level-0 texels of the linear host store into client memory */ |
| 215 |
void GetTexImage(std::int32_t level, PixelFormat format, bool bgr, void* pixels); |
| 216 |
|
| 217 |
/** @brief Sets the minification filter */ |
| 218 |
void SetMinFiltering(nCine::SamplerFilter filter); |
| 219 |
/** @brief Sets the magnification filter */ |
| 220 |
void SetMagFiltering(nCine::SamplerFilter filter); |
| 221 |
/** @brief Sets the wrap mode */ |
| 222 |
void SetWrap(SamplerWrapping wrap); |
| 223 |
/** @brief Sets the sampling swizzle (stored, informational - the palette path keys off the upload format instead) */ |
| 224 |
void SetSwizzle(SwizzleChannel r, SwizzleChannel g, SwizzleChannel b, SwizzleChannel a); |
| 225 |
/** @brief Sets the highest defined mipmap level (ignored) */ |
| 226 |
void SetMaxLevel(std::int32_t maxLevel); |
| 227 |
/** @brief Sets the client pixel-row alignment of uploads (ignored, uploads are tightly packed) */ |
| 228 |
static void SetUnpackAlignment(std::int32_t alignment); |
| 229 |
|
| 230 |
/** @brief Sets a debug label; "Palettes" marks the shared palette texture whose rows become palette banks */ |
| 231 |
void SetObjectLabel(StringView label); |
| 232 |
|
| 233 |
static bool SupportsImmutableStorage() { |
| 234 |
return false; |
| 235 |
} |
| 236 |
static bool SupportsTextureReadback() { |
| 237 |
return true; |
| 238 |
} |
| 239 |
static void ClearErrors() {} |
| 240 |
static bool CheckErrors() { |
| 241 |
return false; |
| 242 |
} |
| 243 |
static void CheckFormatSupport(PixelFormat format) { |
| 244 |
static_cast<void>(format); |
| 245 |
} |
| 246 |
|
| 247 |
/** @brief Returns the number of bytes occupied by one texel of the given format (0 if unsupported) */ |
| 248 |
static std::int32_t BytesPerPixel(PixelFormat format); |
| 249 |
|
| 250 |
private: |
| 251 |
static std::uint32_t _nextHandle; |
| 252 |
static std::uint32_t _nextContentVersion; |
| 253 |
|
| 254 |
std::uint32_t _handle; |
| 255 |
std::uint32_t _contentVersion; |
| 256 |
TextureTarget _target; |
| 257 |
PixelFormat _format; |
| 258 |
PixelFormat _uploadFormat; |
| 259 |
std::int32_t _width; |
| 260 |
std::int32_t _height; |
| 261 |
std::int32_t _strideBytes; |
| 262 |
std::int32_t _bytesPerPixel; |
| 263 |
nCine::SamplerFilter _minFilter; |
| 264 |
nCine::SamplerFilter _magFilter; |
| 265 |
SamplerWrapping _wrap; |
| 266 |
SwizzleChannel _swizzle[4]; |
| 267 |
mutable std::uint32_t _textureUnit; |
| 268 |
SmallVector<std::uint8_t, 0> _pixels; |
| 269 |
// Whether _pixels holds the PackBits-compressed image instead of the linear one. The host store |
| 270 |
// exists only so the VRAM store can be rebuilt after eviction (and rows baked per palette), and all |
| 271 |
// of those paths read it strictly front to back - while a full set of linear copies costs several |
| 272 |
// megabytes of the console's 16 MB per level (the two biggest stock levels did not fit). Static |
| 273 |
// indexed content (R8/RG8) compresses to roughly a third, so it is kept compressed and streamed |
| 274 |
// through RlePixelReader where it is consumed. |
| 275 |
bool _pixelsCompressed; |
| 276 |
bool _isRenderTarget; |
| 277 |
bool _isPaletteTexture; |
| 278 |
|
| 279 |
pvr_ptr_t _vram; |
| 280 |
std::uint32_t _vramFormat; |
| 281 |
/** @brief Bytes per texel the current VRAM store was allocated for, so a format change reallocates it */ |
| 282 |
std::int32_t _vramBytesPerTexel; |
| 283 |
std::int32_t _paddedWidth; |
| 284 |
std::int32_t _paddedHeight; |
| 285 |
float _uScale; |
| 286 |
float _vScale; |
| 287 |
|
| 288 |
// One baked copy is kept per palette row: the tile accelerator consumes textures only at scene end, |
| 289 |
// so rebaking a row that an already submitted quad references would corrupt that quad (several rows |
| 290 |
// are commonly alive within one scene, e.g. text and its shadow). Each copy costs as much video |
| 291 |
// memory as the texture itself, and there are only 8 MB of it shared with the framebuffers, so the |
| 292 |
// count is a compromise between reuse and pressure on the video memory. |
| 293 |
static constexpr std::int32_t BakedSlotCount = 3; |
| 294 |
struct BakedSlot { |
| 295 |
pvr_ptr_t Vram; |
| 296 |
bool Valid; |
| 297 |
std::uint32_t PaletteRow; |
| 298 |
std::uint32_t PaletteGeneration; |
| 299 |
std::uint32_t ContentVersion; |
| 300 |
std::uint32_t LastUsedScene; |
| 301 |
const void* Palette; |
| 302 |
}; |
| 303 |
BakedSlot _bakedSlots[BakedSlotCount]; |
| 304 |
std::int32_t _nextBakedSlot; |
| 305 |
|
| 306 |
|
| 307 |
/** |
| 308 |
@brief Makes sure a VRAM store of the right size exists, reallocating it if the format changed |
| 309 |
|
| 310 |
A texture does not always end up in the format it was first uploaded in - a true-color one that |
| 311 |
turns out to fit a color table halves in size - and writing the new format into the old allocation |
| 312 |
would either waste half of it or, the other way round, run past the end of it. |
| 313 |
*/ |
| 314 |
bool EnsureVramStore(std::int32_t bytesPerTexel); |
| 315 |
|
| 316 |
// Every texture with video memory attached is linked into this list, most recently used first, so |
| 317 |
// an allocation that runs out of memory can reclaim the least recently used stores (see Reclaim()) |
| 318 |
static PvrTexture* _liveHead; |
| 319 |
static PvrTexture* _liveTail; |
| 320 |
PvrTexture* _livePrev; |
| 321 |
PvrTexture* _liveNext; |
| 322 |
// The scene this texture was last drawn in. NeverUsed marks one that has been uploaded but not |
| 323 |
// drawn yet, which must stay evictable - during level loading the scene counter does not advance, |
| 324 |
// so an untouched texture would otherwise look like part of the scene being assembled |
| 325 |
static constexpr std::uint32_t NeverUsed = ~std::uint32_t(0); |
| 326 |
std::uint32_t _lastUsedScene; |
| 327 |
|
| 328 |
void Allocate(PixelFormat format, std::int32_t width, std::int32_t height); |
| 329 |
void RefreshVramStore(); |
| 330 |
void FreeVramStores(); |
| 331 |
|
| 332 |
/** @brief Returns the size of the linear (uncompressed) level-0 image in bytes */ |
| 333 |
inline std::int32_t RawPixelsSize() const { |
| 334 |
return _strideBytes * (_height > 0 ? _height : 0); |
| 335 |
} |
| 336 |
/** |
| 337 |
@brief Returns `true` when this texture keeps its host store compressed |
| 338 |
|
| 339 |
Only static indexed content (R8/RG8) qualifies: those stores are read strictly sequentially |
| 340 |
(VRAM rebuild, per-palette-row bake), so they can be streamed out of a compressed form. The |
| 341 |
palette texture is excluded because the device reads its rows directly through @ref GetPixels(), |
| 342 |
and render targets have no meaningful host store at all. |
| 343 |
*/ |
| 344 |
bool CanCompressPixels() const; |
| 345 |
/** @brief Replaces the whole host store with @p data (compressed when @ref CanCompressPixels()) */ |
| 346 |
void StorePixels(const std::uint8_t* data); |
| 347 |
/** |
| 348 |
@brief Makes the host store linear and writable, decompressing (or zero-filling) it if needed |
| 349 |
|
| 350 |
Used by partial sub-uploads (tileset overrides), which patch arbitrary rows in place - the only |
| 351 |
access pattern the compressed form cannot serve. |
| 352 |
*/ |
| 353 |
void MaterializePixelsRaw(); |
| 354 |
/** @brief Re-compresses a linear host store after an in-place patch, if this texture qualifies */ |
| 355 |
void RecompressPixels(); |
| 356 |
|
| 357 |
/** @brief Moves this texture to the front of the live list and stamps it with the current scene */ |
| 358 |
void Touch(); |
| 359 |
/** |
| 360 |
@brief Links this texture at the back of the live list if it is not in it yet |
| 361 |
|
| 362 |
Attaching video memory has to make the texture visible to the eviction walk right away - a store |
| 363 |
uploaded during level loading would otherwise be unreachable (and its memory unreclaimable) until |
| 364 |
the first draw calls @ref Touch(). Linked least recently used, so untouched uploads are also the |
| 365 |
first to go when memory runs short. |
| 366 |
*/ |
| 367 |
void LinkAsLeastRecent(); |
| 368 |
/** @brief Unlinks this texture from the live list */ |
| 369 |
void Unlink(); |
| 370 |
|
| 371 |
/** |
| 372 |
@brief Allocates video memory, freeing the least recently used stores if necessary |
| 373 |
|
| 374 |
The 8 MB of video memory is shared by the framebuffers, the vertex buffer and every texture, so |
| 375 |
a level with many tilesets (or a menu section that pulls in more graphics) can exhaust it. The |
| 376 |
stores are a cache of the copies kept in main memory, so instead of failing the allocation the |
| 377 |
oldest ones are dropped and rebuilt when they are needed again. |
| 378 |
*/ |
| 379 |
static pvr_ptr_t AllocateVram(std::size_t size, const PvrTexture* keepAlive); |
| 380 |
}; |
| 381 |
} |