| 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 |
using namespace Death::Containers; |
| 12 |
|
| 13 |
namespace nCine::RHI::Software |
| 14 |
{ |
| 15 |
/** |
| 16 |
@brief CPU texture object of the software backend (aliased as `RHI::Texture`) |
| 17 |
|
| 18 |
Wraps a single, tightly-packed level-0 pixel buffer in one of the formats the rasterizer can |
| 19 |
sample (@ref PixelFormat::RGBA8 for the textured path, @ref PixelFormat::R8 / @ref PixelFormat::RG8 |
| 20 |
for the palette path). R8 / RG8 uploads are stored NATIVELY at 1 / 2 bytes per texel (4x / 2x less |
| 21 |
memory than an RGBA8 store and correspondingly less gather bandwidth); the samplers expand each |
| 22 |
fetched texel to the 4-byte RGBA working form, filling the missing channels with 255. Only RGB8 is |
| 23 |
widened to RGBA8 at upload (3-byte texels are unaligned), and any store becomes RGBA8 the moment |
| 24 |
the texture is attached as a render target (the rasterizer composites 4 bytes per pixel). It |
| 25 |
exposes the neutral upload surface `Texture.cpp` drives (`TexImage2D`, `TexSubImage2D`, |
| 26 |
`TexStorage2D`, filter/wrap/swizzle setters); binding records the texture on the device so the |
| 27 |
effect running the draw can read its texels. Mip levels above 0 and compressed formats are |
| 28 |
accepted but not stored (the fast path samples level 0 only). |
| 29 |
*/ |
| 30 |
class SwTexture |
| 31 |
{ |
| 32 |
public: |
| 33 |
/** @brief Number of texture units tracked by the device */ |
| 34 |
static constexpr std::uint32_t MaxTextureUnits = 8; |
| 35 |
|
| 36 |
explicit SwTexture(TextureTarget target); |
| 37 |
~SwTexture(); |
| 38 |
|
| 39 |
SwTexture(const SwTexture&) = delete; |
| 40 |
SwTexture& operator=(const SwTexture&) = delete; |
| 41 |
|
| 42 |
/** @brief Returns a backend-neutral identifier uniquely identifying the texture (feeds material sort keys) */ |
| 43 |
inline std::uint32_t GetUniqueId() const { |
| 44 |
return _handle; |
| 45 |
} |
| 46 |
/** @brief Returns the texture target */ |
| 47 |
inline TextureTarget GetTarget() const { |
| 48 |
return _target; |
| 49 |
} |
| 50 |
|
| 51 |
/** @brief Returns the width of level 0 in texels */ |
| 52 |
inline std::int32_t GetWidth() const { |
| 53 |
return _width; |
| 54 |
} |
| 55 |
/** @brief Returns the height of level 0 in texels */ |
| 56 |
inline std::int32_t GetHeight() const { |
| 57 |
return _height; |
| 58 |
} |
| 59 |
/** @brief Returns the pixel format of the stored texels (native for R8/RG8; RGBA8 after the RGB8 or render-target widening) */ |
| 60 |
inline PixelFormat GetFormat() const { |
| 61 |
return _format; |
| 62 |
} |
| 63 |
/** @brief Returns the original upload format before any widening (R8/RG8/RGB8 kept, so the palette path can tell an R8 index texture from an RG8 index+alpha one even after a render-target promotion) */ |
| 64 |
inline PixelFormat GetUploadFormat() const { |
| 65 |
return _uploadFormat; |
| 66 |
} |
| 67 |
/** @brief Returns the byte distance between two consecutive rows of level 0 */ |
| 68 |
inline std::int32_t GetStrideBytes() const { |
| 69 |
return _strideBytes; |
| 70 |
} |
| 71 |
/** @brief Returns the byte size of one stored texel (1 for R8, 2 for RG8, 4 for RGBA8 and widened stores) */ |
| 72 |
inline std::int32_t GetBytesPerPixel() const { |
| 73 |
return _bytesPerPixel; |
| 74 |
} |
| 75 |
/** @brief Returns the base pointer of the level-0 texel store (may be `nullptr` before an upload); the single-level store ignores @p level */ |
| 76 |
inline const std::uint8_t* GetPixels(std::int32_t level = 0) const { |
| 77 |
static_cast<void>(level); |
| 78 |
return _pixels.empty() ? nullptr : _pixels.data(); |
| 79 |
} |
| 80 |
/** @brief Returns the horizontal texture-coordinate wrap mode (used by the rasterizer sampler) */ |
| 81 |
inline SamplerWrapping GetWrapS() const { |
| 82 |
return _wrap; |
| 83 |
} |
| 84 |
/** @brief Returns the vertical texture-coordinate wrap mode (single stored mode, same as @ref GetWrapS()) */ |
| 85 |
inline SamplerWrapping GetWrapT() const { |
| 86 |
return _wrap; |
| 87 |
} |
| 88 |
/** @brief Returns the four-channel sampling swizzle (identity by default; the palette path maps `.a` to green for RG8 index textures) */ |
| 89 |
inline const SwizzleChannel* GetSwizzle() const { |
| 90 |
return _swizzle; |
| 91 |
} |
| 92 |
/** @brief Returns the magnification filter (alias of @ref GetMagFiltering() the rasterizer samples with) */ |
| 93 |
inline nCine::SamplerFilter GetMagFilter() const { |
| 94 |
return _magFilter; |
| 95 |
} |
| 96 |
/** @brief Returns `true` if the texture is bound as a color render target (its store is treated as bottom-up by the fast blit) */ |
| 97 |
inline bool IsRenderTarget() const { |
| 98 |
return _isRenderTarget; |
| 99 |
} |
| 100 |
/** |
| 101 |
* @brief Marks the texture as (or no longer as) a color render target |
| 102 |
* |
| 103 |
* Becoming a render target widens a native R8/RG8 store to RGBA8 (discarding the texels - render |
| 104 |
* targets are always fully drawn or cleared before being read): the rasterizer composites 4 bytes |
| 105 |
* per pixel, so a narrower store would be overflowed by the first draw into it. |
| 106 |
*/ |
| 107 |
void SetRenderTarget(bool isRenderTarget); |
| 108 |
/** @brief Returns a writable base pointer of the level-0 texel store (for render-target output) */ |
| 109 |
inline std::uint8_t* MutablePixels() { |
| 110 |
return _pixels.empty() ? nullptr : _pixels.data(); |
| 111 |
} |
| 112 |
/** @brief Returns a globally monotonic stamp of the texel store, advanced by every allocation or upload (used to key content-derived caches; render-target writes bypass it) */ |
| 113 |
inline std::uint32_t GetContentVersion() const { |
| 114 |
return _contentVersion; |
| 115 |
} |
| 116 |
|
| 117 |
/** @brief Binds the texture to the specified texture unit on the device */ |
| 118 |
bool Bind(std::uint32_t textureUnit) const; |
| 119 |
/** @brief Binds the texture to texture unit 0 */ |
| 120 |
inline bool Bind() const { |
| 121 |
return Bind(0); |
| 122 |
} |
| 123 |
/** @brief Unbinds the texture from the unit it was last bound to */ |
| 124 |
bool Unbind() const; |
| 125 |
/** @brief Unbinds any texture from the specified texture unit */ |
| 126 |
static bool Unbind(std::uint32_t textureUnit); |
| 127 |
|
| 128 |
/** @brief Allocates level-0 storage of the given format/size and optionally uploads its texels */ |
| 129 |
void TexImage2D(std::int32_t level, PixelFormat format, bool bgr, std::int32_t width, std::int32_t height, const void* data); |
| 130 |
/** @brief Updates a rectangular subregion of level 0 */ |
| 131 |
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); |
| 132 |
/** @brief Allocates immutable level-0 storage of the given format/size (no texels yet) */ |
| 133 |
void TexStorage2D(std::int32_t levels, PixelFormat format, std::int32_t width, std::int32_t height); |
| 134 |
/** @brief Compressed upload (unsupported by the fast path, accepted as a no-op) */ |
| 135 |
void CompressedTexImage2D(std::int32_t level, PixelFormat format, std::int32_t width, std::int32_t height, std::int32_t imageSize, const void* data); |
| 136 |
/** @brief Compressed sub-upload (unsupported by the fast path, accepted as a no-op) */ |
| 137 |
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); |
| 138 |
/** @brief Reads back level-0 texels into client memory */ |
| 139 |
void GetTexImage(std::int32_t level, PixelFormat format, bool bgr, void* pixels); |
| 140 |
|
| 141 |
/** @brief Sets the minification filter (stored; the fast path currently samples nearest) */ |
| 142 |
void SetMinFiltering(nCine::SamplerFilter filter); |
| 143 |
/** @brief Sets the magnification filter (stored; the fast path currently samples nearest) */ |
| 144 |
void SetMagFiltering(nCine::SamplerFilter filter); |
| 145 |
/** @brief Sets the wrap mode (stored) */ |
| 146 |
void SetWrap(SamplerWrapping wrap); |
| 147 |
/** @brief Sets the sampling swizzle (stored; used by the palette path) */ |
| 148 |
void SetSwizzle(SwizzleChannel r, SwizzleChannel g, SwizzleChannel b, SwizzleChannel a); |
| 149 |
/** @brief Sets the highest defined mipmap level (ignored) */ |
| 150 |
void SetMaxLevel(std::int32_t maxLevel); |
| 151 |
/** @brief Sets the client pixel-row alignment of uploads (ignored, uploads are tightly packed) */ |
| 152 |
static void SetUnpackAlignment(std::int32_t alignment); |
| 153 |
|
| 154 |
/** @brief Sets a debug label for the texture (ignored) */ |
| 155 |
void SetObjectLabel(StringView label); |
| 156 |
|
| 157 |
/** @brief Returns the magnification filter, for the effect to choose nearest vs. bilinear */ |
| 158 |
inline nCine::SamplerFilter GetMagFiltering() const { |
| 159 |
return _magFilter; |
| 160 |
} |
| 161 |
|
| 162 |
static bool SupportsImmutableStorage() { |
| 163 |
return false; |
| 164 |
} |
| 165 |
static bool SupportsTextureReadback() { |
| 166 |
return true; |
| 167 |
} |
| 168 |
static void ClearErrors() {} |
| 169 |
static bool CheckErrors() { |
| 170 |
return false; |
| 171 |
} |
| 172 |
static void CheckFormatSupport(PixelFormat format) { |
| 173 |
static_cast<void>(format); |
| 174 |
} |
| 175 |
|
| 176 |
/** @brief Returns the number of bytes occupied by one texel of the given format (0 if unsupported) */ |
| 177 |
static std::int32_t BytesPerPixel(PixelFormat format); |
| 178 |
|
| 179 |
private: |
| 180 |
static std::uint32_t _nextHandle; |
| 181 |
static std::uint32_t _nextContentVersion; |
| 182 |
|
| 183 |
std::uint32_t _handle; |
| 184 |
std::uint32_t _contentVersion; |
| 185 |
TextureTarget _target; |
| 186 |
PixelFormat _format; |
| 187 |
PixelFormat _uploadFormat; |
| 188 |
std::int32_t _width; |
| 189 |
std::int32_t _height; |
| 190 |
std::int32_t _strideBytes; |
| 191 |
std::int32_t _bytesPerPixel; |
| 192 |
nCine::SamplerFilter _minFilter; |
| 193 |
nCine::SamplerFilter _magFilter; |
| 194 |
SamplerWrapping _wrap; |
| 195 |
SwizzleChannel _swizzle[4]; |
| 196 |
mutable std::uint32_t _textureUnit; |
| 197 |
std::vector<std::uint8_t> _pixels; |
| 198 |
bool _isRenderTarget; |
| 199 |
|
| 200 |
void Allocate(PixelFormat format, std::int32_t width, std::int32_t height); |
| 201 |
}; |
| 202 |
} |