| 1 |
#pragma once |
| 2 |
|
| 3 |
#include "RsxVram.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 <rsx/gcm_sys.h> |
| 13 |
|
| 14 |
using namespace Death::Containers; |
| 15 |
|
| 16 |
namespace nCine::RHI::RSX |
| 17 |
{ |
| 18 |
/** |
| 19 |
@brief Texture object of the RSX 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. |
| 26 |
|
| 27 |
A texture bound as a colour attachment is the exception: the GPU writes it, so its GPU-visible copy |
| 28 |
*is* the truth and the host store is only kept for a readback. That is also why the two live in |
| 29 |
separate allocations at all - the render target's surface has to stay put across frames while the |
| 30 |
host store may be reallocated by an upload. |
| 31 |
|
| 32 |
**The swizzle is free here, and that is the one real difference from the sceGxm backend.** sceGxm can |
| 33 |
only express a fixed set of channel patterns, so `GxmTexture` bakes the engine's swizzle into the |
| 34 |
texels and re-bakes them whenever it changes. The RSX has a general `remap` field in its texture |
| 35 |
control structure: each of the four output channels independently selects a source channel, or a |
| 36 |
constant zero or one. So the texels are uploaded once and never rewritten, and @ref SetSwizzle() only |
| 37 |
recomputes a word. |
| 38 |
|
| 39 |
That same field also absorbs the endianness problem the format list creates. The RSX's 32-bit colour |
| 40 |
format is `A8R8G8B8` - a word, not a byte order - and the engine's RGBA8 store read as a big-endian |
| 41 |
word lands as `0xRRGGBBAA`, so the hardware sees red where alpha belongs and so on round the cycle. |
| 42 |
Rather than byte-swapping every upload, @ref BuildRemap() composes that fixed rotation with whatever |
| 43 |
swizzle the engine asked for, and the GPU does both in the sampler for nothing. |
| 44 |
|
| 45 |
The GPU copy is laid out linearly (`GCM_TEXTURE_FORMAT_LIN`) with an explicit pitch, which the RSX |
| 46 |
carries in the control structure - so unlike sceGxm there is no width-dependent choice of layout and |
| 47 |
no addressing mode lost to it. The pitch is rounded up to 64 bytes, which is what a colour surface |
| 48 |
needs when the texture is also a render target, so one rule covers both uses. |
| 49 |
*/ |
| 50 |
class RsxTexture |
| 51 |
{ |
| 52 |
public: |
| 53 |
/** @brief Number of texture units tracked by the device */ |
| 54 |
static constexpr std::uint32_t MaxTextureUnits = 8; |
| 55 |
|
| 56 |
explicit RsxTexture(TextureTarget target); |
| 57 |
~RsxTexture(); |
| 58 |
|
| 59 |
RsxTexture(const RsxTexture&) = delete; |
| 60 |
RsxTexture& operator=(const RsxTexture&) = delete; |
| 61 |
|
| 62 |
/** @brief Returns the libgcm texture control structure, materializing the GPU copy if needed, or `nullptr` */ |
| 63 |
const gcmTexture* GetGcmTexture() const; |
| 64 |
/** @brief Returns the GPU-visible base address of the texels, allocating the copy if needed (used by @ref RsxRenderTarget) */ |
| 65 |
void* GetSurfaceData() const; |
| 66 |
/** @brief Returns the byte distance between two rows of the GPU copy (its surface stride) */ |
| 67 |
inline std::uint32_t GetSurfaceStride() const { |
| 68 |
return _gpuStride; |
| 69 |
} |
| 70 |
/** @brief Releases the GPU-visible copy (on re-allocation and destruction) */ |
| 71 |
void ReleaseGpu() const; |
| 72 |
/** |
| 73 |
@brief Programs this texture's filter and wrap modes onto a texture unit |
| 74 |
|
| 75 |
Unlike sceGxm, which keeps sampler state inside the texture structure, the RSX carries it in |
| 76 |
command-buffer methods - so it is re-issued per draw by @ref RsxDevice::DrawCommon() rather than |
| 77 |
baked in once, and this is part of the backend surface rather than an internal detail. |
| 78 |
*/ |
| 79 |
void ApplySamplerState(std::uint32_t textureUnit) const; |
| 80 |
|
| 81 |
/** |
| 82 |
@brief Returns a writable pointer to the GPU-visible store, for content rebuilt every frame |
| 83 |
|
| 84 |
Lets the cinematics produce a frame straight where the GPU will sample it instead of writing the |
| 85 |
host store and copying it across. Answered only for an ordinary RGBA8 texture: a colour |
| 86 |
attachment is written by the GPU, so handing its texels to the CPU would race the frame. |
| 87 |
|
| 88 |
@p strideBytes receives the row pitch, which is the 64-byte aligned one the GPU copy was |
| 89 |
allocated with rather than the texture's own width. Nothing written here reaches the host store, |
| 90 |
so such a texture must be rewritten in full every frame - and never read back, because this |
| 91 |
memory is local video memory the PPE reads at a crawl (see @ref RsxVram). |
| 92 |
*/ |
| 93 |
void* MapStreamingTexels(std::int32_t& strideBytes); |
| 94 |
|
| 95 |
/** @brief Returns a backend-neutral identifier uniquely identifying the texture (feeds material sort keys) */ |
| 96 |
inline std::uint32_t GetUniqueId() const { |
| 97 |
return _handle; |
| 98 |
} |
| 99 |
/** @brief Returns the texture target */ |
| 100 |
inline TextureTarget GetTarget() const { |
| 101 |
return _target; |
| 102 |
} |
| 103 |
|
| 104 |
/** @brief Returns the width of level 0 in texels */ |
| 105 |
inline std::int32_t GetWidth() const { |
| 106 |
return _width; |
| 107 |
} |
| 108 |
/** @brief Returns the height of level 0 in texels */ |
| 109 |
inline std::int32_t GetHeight() const { |
| 110 |
return _height; |
| 111 |
} |
| 112 |
/** @brief Returns the pixel format of the stored texels (always RGBA8 after the promotion) */ |
| 113 |
inline PixelFormat GetFormat() const { |
| 114 |
return _format; |
| 115 |
} |
| 116 |
/** @brief Returns the original upload format before promotion (R8/RG8/RGB8 kept) */ |
| 117 |
inline PixelFormat GetUploadFormat() const { |
| 118 |
return _uploadFormat; |
| 119 |
} |
| 120 |
/** @brief Returns the byte distance between two consecutive rows of the host store */ |
| 121 |
inline std::int32_t GetStrideBytes() const { |
| 122 |
return _strideBytes; |
| 123 |
} |
| 124 |
/** @brief Returns the base pointer of the level-0 host store (may be `nullptr` before an upload); the single-level store ignores @p level */ |
| 125 |
inline const std::uint8_t* GetPixels(std::int32_t level = 0) const { |
| 126 |
static_cast<void>(level); |
| 127 |
return (_pixels.empty() ? nullptr : _pixels.data()); |
| 128 |
} |
| 129 |
/** @brief Returns the horizontal texture-coordinate wrap mode */ |
| 130 |
inline SamplerWrapping GetWrapS() const { |
| 131 |
return _wrap; |
| 132 |
} |
| 133 |
/** @brief Returns the vertical texture-coordinate wrap mode (single stored mode, same as @ref GetWrapS()) */ |
| 134 |
inline SamplerWrapping GetWrapT() const { |
| 135 |
return _wrap; |
| 136 |
} |
| 137 |
/** @brief Returns the four-channel sampling swizzle (identity by default) */ |
| 138 |
inline const SwizzleChannel* GetSwizzle() const { |
| 139 |
return _swizzle; |
| 140 |
} |
| 141 |
/** @brief Returns the magnification filter (alias of @ref GetMagFiltering()) */ |
| 142 |
inline nCine::SamplerFilter GetMagFilter() const { |
| 143 |
return _magFilter; |
| 144 |
} |
| 145 |
/** @brief Returns `true` if the texture is bound as a color render target */ |
| 146 |
inline bool IsRenderTarget() const { |
| 147 |
return _isRenderTarget; |
| 148 |
} |
| 149 |
/** @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) */ |
| 150 |
void SetRenderTarget(bool isRenderTarget); |
| 151 |
/** @brief Returns a writable base pointer of the level-0 host store */ |
| 152 |
inline std::uint8_t* MutablePixels() { |
| 153 |
return (_pixels.empty() ? nullptr : _pixels.data()); |
| 154 |
} |
| 155 |
|
| 156 |
/** @brief Binds the texture to the specified texture unit on the device */ |
| 157 |
bool Bind(std::uint32_t textureUnit) const; |
| 158 |
/** @brief Binds the texture to texture unit 0 */ |
| 159 |
inline bool Bind() const { |
| 160 |
return Bind(0); |
| 161 |
} |
| 162 |
/** @brief Unbinds the texture from the unit it was last bound to */ |
| 163 |
bool Unbind() const; |
| 164 |
/** @brief Unbinds any texture from the specified texture unit */ |
| 165 |
static bool Unbind(std::uint32_t textureUnit); |
| 166 |
|
| 167 |
/** @brief Allocates level-0 storage of the given format/size and optionally uploads its texels */ |
| 168 |
void TexImage2D(std::int32_t level, PixelFormat format, bool bgr, std::int32_t width, std::int32_t height, const void* data); |
| 169 |
/** @brief Updates a rectangular subregion of level 0 */ |
| 170 |
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); |
| 171 |
/** @brief Allocates immutable level-0 storage of the given format/size (no texels yet) */ |
| 172 |
void TexStorage2D(std::int32_t levels, PixelFormat format, std::int32_t width, std::int32_t height); |
| 173 |
/** @brief Compressed upload (unsupported by this backend, accepted as a no-op) */ |
| 174 |
void CompressedTexImage2D(std::int32_t level, PixelFormat format, std::int32_t width, std::int32_t height, std::int32_t imageSize, const void* data); |
| 175 |
/** @brief Compressed sub-upload (unsupported by this backend, accepted as a no-op) */ |
| 176 |
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); |
| 177 |
/** @brief Reads back level-0 texels into client memory (from the GPU copy when the texture is a render target) */ |
| 178 |
void GetTexImage(std::int32_t level, PixelFormat format, bool bgr, void* pixels); |
| 179 |
|
| 180 |
/** @brief Sets the minification filter */ |
| 181 |
void SetMinFiltering(nCine::SamplerFilter filter); |
| 182 |
/** @brief Sets the magnification filter */ |
| 183 |
void SetMagFiltering(nCine::SamplerFilter filter); |
| 184 |
/** @brief Sets the wrap mode */ |
| 185 |
void SetWrap(SamplerWrapping wrap); |
| 186 |
/** @brief Sets the sampling swizzle (recomputes the remap word; the texels are not touched) */ |
| 187 |
void SetSwizzle(SwizzleChannel r, SwizzleChannel g, SwizzleChannel b, SwizzleChannel a); |
| 188 |
/** @brief Sets the highest defined mipmap level (ignored, the store is single-level) */ |
| 189 |
void SetMaxLevel(std::int32_t maxLevel); |
| 190 |
/** @brief Sets the client pixel-row alignment of uploads (ignored, uploads are tightly packed) */ |
| 191 |
static void SetUnpackAlignment(std::int32_t alignment); |
| 192 |
|
| 193 |
/** @brief Sets a debug label for the texture (ignored) */ |
| 194 |
void SetObjectLabel(StringView label); |
| 195 |
|
| 196 |
/** @brief Returns the magnification filter */ |
| 197 |
inline nCine::SamplerFilter GetMagFiltering() const { |
| 198 |
return _magFilter; |
| 199 |
} |
| 200 |
|
| 201 |
static bool SupportsImmutableStorage() { |
| 202 |
return false; |
| 203 |
} |
| 204 |
static bool SupportsTextureReadback() { |
| 205 |
return true; |
| 206 |
} |
| 207 |
static void ClearErrors() {} |
| 208 |
static bool CheckErrors() { |
| 209 |
return false; |
| 210 |
} |
| 211 |
static void CheckFormatSupport(PixelFormat format) { |
| 212 |
static_cast<void>(format); |
| 213 |
} |
| 214 |
|
| 215 |
/** @brief Returns the number of bytes occupied by one texel of the given format (0 if unsupported) */ |
| 216 |
static std::int32_t BytesPerPixel(PixelFormat format); |
| 217 |
|
| 218 |
private: |
| 219 |
static std::uint32_t _nextHandle; |
| 220 |
|
| 221 |
std::uint32_t _handle; |
| 222 |
TextureTarget _target; |
| 223 |
PixelFormat _format; |
| 224 |
PixelFormat _uploadFormat; |
| 225 |
std::int32_t _width; |
| 226 |
std::int32_t _height; |
| 227 |
std::int32_t _strideBytes; |
| 228 |
nCine::SamplerFilter _minFilter; |
| 229 |
nCine::SamplerFilter _magFilter; |
| 230 |
SamplerWrapping _wrap; |
| 231 |
SwizzleChannel _swizzle[4]; |
| 232 |
mutable std::uint32_t _textureUnit; |
| 233 |
SmallVector<std::uint8_t, 0> _pixels; |
| 234 |
bool _isRenderTarget; |
| 235 |
|
| 236 |
// GPU-visible copy, created lazily on the first bind (mutable so the const bind-time accessors can |
| 237 |
// materialize it). `_contentsDirty` forces a refresh from the host store; the sampler state is |
| 238 |
// programmed per bind rather than cached, because it lives in command-buffer methods rather than in |
| 239 |
// the texture structure the way sceGxm keeps it |
| 240 |
mutable RsxVram::Block _gpuBlock; |
| 241 |
mutable gcmTexture _gpuTexture; |
| 242 |
mutable std::uint32_t _gpuStride; |
| 243 |
mutable bool _gpuValid; |
| 244 |
mutable bool _contentsDirty; |
| 245 |
|
| 246 |
void Allocate(PixelFormat format, std::int32_t width, std::int32_t height); |
| 247 |
/** @brief (Re)creates the GPU copy and its control structure when missing, then refreshes it if stale */ |
| 248 |
bool EnsureGpuTexture() const; |
| 249 |
/** @brief Copies the host store into the GPU copy, applying the row padding */ |
| 250 |
void UploadPixels() const; |
| 251 |
/** |
| 252 |
@brief Builds the `gcmTexture::remap` word from the tracked swizzle |
| 253 |
|
| 254 |
Composes two mappings: the engine's swizzle (which logical channel, or a constant, feeds each |
| 255 |
output) and the fixed rotation that reinterprets the RGBA8 store as the hardware's A8R8G8B8 word |
| 256 |
(see the class documentation). |
| 257 |
*/ |
| 258 |
std::uint32_t BuildRemap() const; |
| 259 |
}; |
| 260 |
} |