| 1 |
#include "GxTexture.h" |
| 2 |
#include "GxDevice.h" |
| 3 |
|
| 4 |
#include "../../../../Main.h" |
| 5 |
|
| 6 |
#include <cstring> |
| 7 |
#include <malloc.h> // memalign (libogc main memory must be 32-byte aligned for GX/DMA) |
| 8 |
|
| 9 |
#include <ogc/cache.h> // DCFlushRange |
| 10 |
|
| 11 |
using namespace Death::Containers::Literals; |
| 12 |
|
| 13 |
namespace nCine::RHI::GX |
| 14 |
{ |
| 15 |
namespace |
| 16 |
{ |
| 17 |
// Rounds a texture dimension up to the given tile edge (GX stores textures as tiles) |
| 18 |
inline std::int32_t AlignUp(std::int32_t value, std::int32_t alignment) |
| 19 |
{ |
| 20 |
return (value + alignment - 1) & ~(alignment - 1); |
| 21 |
} |
| 22 |
|
| 23 |
// Converts a linear 1-byte-per-texel index image to the tiled GX_TF_CI8 layout (8x4 texel tiles, |
| 24 |
// 32 bytes each, tiles ordered row-major) |
| 25 |
void TileCi8(std::uint8_t* DEATH_RESTRICT dst, const std::uint8_t* DEATH_RESTRICT src, |
| 26 |
std::int32_t width, std::int32_t height, std::int32_t srcStride) |
| 27 |
{ |
| 28 |
const std::int32_t tilesX = AlignUp(width, 8) / 8; |
| 29 |
const std::int32_t tilesY = AlignUp(height, 4) / 4; |
| 30 |
for (std::int32_t ty = 0; ty < tilesY; ty++) { |
| 31 |
for (std::int32_t tx = 0; tx < tilesX; tx++) { |
| 32 |
std::uint8_t* tile = dst + std::size_t(ty * tilesX + tx) * 32; |
| 33 |
for (std::int32_t row = 0; row < 4; row++) { |
| 34 |
const std::int32_t y = ty * 4 + row; |
| 35 |
for (std::int32_t col = 0; col < 8; col++) { |
| 36 |
const std::int32_t x = tx * 8 + col; |
| 37 |
tile[row * 8 + col] = (x < width && y < height ? src[std::size_t(y) * srcStride + x] : 0); |
| 38 |
} |
| 39 |
} |
| 40 |
} |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
// Converts a linear RGBA image (rgbaBpp = 4, or 3 with implicit opaque alpha, or an already-expanded |
| 45 |
// row source) to the tiled GX_TF_RGBA8 layout: 4x4 texel tiles of 64 bytes, the first 32 bytes |
| 46 |
// holding the AR pairs and the second 32 bytes the GB pairs of the 16 texels |
| 47 |
void TileRgba8(std::uint8_t* DEATH_RESTRICT dst, const std::uint8_t* DEATH_RESTRICT src, |
| 48 |
std::int32_t width, std::int32_t height, std::int32_t srcStride, std::int32_t srcBpp) |
| 49 |
{ |
| 50 |
const std::int32_t tilesX = AlignUp(width, 4) / 4; |
| 51 |
const std::int32_t tilesY = AlignUp(height, 4) / 4; |
| 52 |
for (std::int32_t ty = 0; ty < tilesY; ty++) { |
| 53 |
for (std::int32_t tx = 0; tx < tilesX; tx++) { |
| 54 |
std::uint8_t* tile = dst + std::size_t(ty * tilesX + tx) * 64; |
| 55 |
for (std::int32_t row = 0; row < 4; row++) { |
| 56 |
const std::int32_t y = ty * 4 + row; |
| 57 |
for (std::int32_t col = 0; col < 4; col++) { |
| 58 |
const std::int32_t x = tx * 4 + col; |
| 59 |
std::uint8_t r = 0, g = 0, b = 0, a = 0; |
| 60 |
if (x < width && y < height) { |
| 61 |
const std::uint8_t* px = src + std::size_t(y) * srcStride + std::size_t(x) * srcBpp; |
| 62 |
r = px[0]; |
| 63 |
g = (srcBpp >= 2 ? px[1] : std::uint8_t(255)); |
| 64 |
b = (srcBpp >= 3 ? px[2] : std::uint8_t(255)); |
| 65 |
a = (srcBpp >= 4 ? px[3] : std::uint8_t(255)); |
| 66 |
} |
| 67 |
const std::int32_t i = row * 4 + col; |
| 68 |
tile[i * 2 + 0] = a; |
| 69 |
tile[i * 2 + 1] = r; |
| 70 |
tile[32 + i * 2 + 0] = g; |
| 71 |
tile[32 + i * 2 + 1] = b; |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
inline std::size_t TiledSizeCi8(std::int32_t width, std::int32_t height) |
| 79 |
{ |
| 80 |
return std::size_t(AlignUp(width, 8)) * std::size_t(AlignUp(height, 4)); |
| 81 |
} |
| 82 |
|
| 83 |
inline std::size_t TiledSizeRgba8(std::int32_t width, std::int32_t height) |
| 84 |
{ |
| 85 |
return std::size_t(AlignUp(width, 4)) * std::size_t(AlignUp(height, 4)) * 4; |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
std::uint32_t GxTexture::_nextHandle = 1; |
| 90 |
std::uint32_t GxTexture::_nextContentVersion = 0; |
| 91 |
|
| 92 |
GxTexture::GxTexture(TextureTarget target) |
| 93 |
: _handle(_nextHandle++), _contentVersion(0), _target(target), _format(PixelFormat::Unknown), _uploadFormat(PixelFormat::Unknown), |
| 94 |
_width(0), _height(0), _strideBytes(0), _bytesPerPixel(0), |
| 95 |
_minFilter(nCine::SamplerFilter::Nearest), _magFilter(nCine::SamplerFilter::Nearest), _wrap(SamplerWrapping::ClampToEdge), |
| 96 |
_textureUnit(0), _isRenderTarget(false), _isPaletteTexture(false), |
| 97 |
_tiledStore(nullptr), _tiledStoreSize(0), _texObjValid(false), |
| 98 |
_bakedSlots{}, _nextBakedSlot(0) |
| 99 |
{ |
| 100 |
_swizzle[0] = SwizzleChannel::Red; |
| 101 |
_swizzle[1] = SwizzleChannel::Green; |
| 102 |
_swizzle[2] = SwizzleChannel::Blue; |
| 103 |
_swizzle[3] = SwizzleChannel::Alpha; |
| 104 |
} |
| 105 |
|
| 106 |
GxTexture::~GxTexture() |
| 107 |
{ |
| 108 |
// Clear from the device so a destroyed texture can't dangle in the bound-texture table |
| 109 |
GxDevice::UnbindTexture(this); |
| 110 |
FreeTiledStores(); |
| 111 |
} |
| 112 |
|
| 113 |
void GxTexture::FreeTiledStores() |
| 114 |
{ |
| 115 |
if (_tiledStore != nullptr) { |
| 116 |
free(_tiledStore); |
| 117 |
_tiledStore = nullptr; |
| 118 |
_tiledStoreSize = 0; |
| 119 |
} |
| 120 |
_texObjValid = false; |
| 121 |
for (std::int32_t i = 0; i < BakedSlotCount; i++) { |
| 122 |
if (_bakedSlots[i].Store != nullptr) { |
| 123 |
free(_bakedSlots[i].Store); |
| 124 |
_bakedSlots[i].Store = nullptr; |
| 125 |
} |
| 126 |
_bakedSlots[i].Valid = false; |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
std::int32_t GxTexture::BytesPerPixel(PixelFormat format) |
| 131 |
{ |
| 132 |
switch (format) { |
| 133 |
case PixelFormat::R8: return 1; |
| 134 |
case PixelFormat::RG8: return 2; |
| 135 |
case PixelFormat::RGB8: return 3; |
| 136 |
case PixelFormat::RGBA8: return 4; |
| 137 |
default: return 0; |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
void GxTexture::Allocate(PixelFormat format, std::int32_t width, std::int32_t height) |
| 142 |
{ |
| 143 |
_uploadFormat = format; |
| 144 |
_format = format; |
| 145 |
_width = width; |
| 146 |
_height = height; |
| 147 |
_bytesPerPixel = BytesPerPixel(_format); |
| 148 |
_strideBytes = width * _bytesPerPixel; |
| 149 |
_pixels.assign(std::size_t(_strideBytes) * std::size_t(height > 0 ? height : 0), std::uint8_t(0)); |
| 150 |
FreeTiledStores(); |
| 151 |
_contentVersion = ++_nextContentVersion; |
| 152 |
} |
| 153 |
|
| 154 |
void GxTexture::InitTexObj(GXTexObj& obj, void* store, std::uint8_t gxFormat, bool ci) |
| 155 |
{ |
| 156 |
const std::uint8_t wrap = (_wrap == SamplerWrapping::Repeat ? GX_REPEAT |
| 157 |
: (_wrap == SamplerWrapping::MirroredRepeat ? GX_MIRROR : GX_CLAMP)); |
| 158 |
if (ci) { |
| 159 |
// The TLUT name is patched per draw by the device (GX_InitTexObjTlut) before loading the object |
| 160 |
GX_InitTexObjCI(&obj, store, std::uint16_t(_width), std::uint16_t(_height), gxFormat, wrap, wrap, GX_FALSE, GX_TLUT0); |
| 161 |
} else { |
| 162 |
GX_InitTexObj(&obj, store, std::uint16_t(_width), std::uint16_t(_height), gxFormat, wrap, wrap, GX_FALSE); |
| 163 |
} |
| 164 |
const std::uint8_t filter = (_magFilter == nCine::SamplerFilter::Linear ? GX_LINEAR : GX_NEAR); |
| 165 |
GX_InitTexObjFilterMode(&obj, filter, filter); |
| 166 |
} |
| 167 |
|
| 168 |
void GxTexture::RefreshTiledStore() |
| 169 |
{ |
| 170 |
_texObjValid = false; |
| 171 |
if (_pixels.empty() || _width <= 0 || _height <= 0 || _isPaletteTexture) { |
| 172 |
return; |
| 173 |
} |
| 174 |
|
| 175 |
std::size_t required = 0; |
| 176 |
if (_uploadFormat == PixelFormat::R8) { |
| 177 |
required = TiledSizeCi8(_width, _height); |
| 178 |
} else if (_uploadFormat == PixelFormat::RGB8 || _uploadFormat == PixelFormat::RGBA8) { |
| 179 |
required = TiledSizeRgba8(_width, _height); |
| 180 |
} else { |
| 181 |
// RG8 keeps only the linear store; the tiled copy is baked per palette row on demand |
| 182 |
return; |
| 183 |
} |
| 184 |
|
| 185 |
if (_tiledStore == nullptr || _tiledStoreSize != required) { |
| 186 |
if (_tiledStore != nullptr) { |
| 187 |
free(_tiledStore); |
| 188 |
} |
| 189 |
_tiledStore = static_cast<std::uint8_t*>(memalign(32, required)); |
| 190 |
_tiledStoreSize = required; |
| 191 |
if (_tiledStore == nullptr) { |
| 192 |
LOGE("Out of memory allocating a {} B tiled texture store", required); |
| 193 |
return; |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
if (_uploadFormat == PixelFormat::R8) { |
| 198 |
TileCi8(_tiledStore, _pixels.data(), _width, _height, _strideBytes); |
| 199 |
InitTexObj(_texObj, _tiledStore, GX_TF_CI8, true); |
| 200 |
} else { |
| 201 |
TileRgba8(_tiledStore, _pixels.data(), _width, _height, _strideBytes, _bytesPerPixel); |
| 202 |
InitTexObj(_texObj, _tiledStore, GX_TF_RGBA8, false); |
| 203 |
} |
| 204 |
DCFlushRange(_tiledStore, std::uint32_t(_tiledStoreSize)); |
| 205 |
GX_InvalidateTexAll(); |
| 206 |
_texObjValid = true; |
| 207 |
} |
| 208 |
|
| 209 |
GXTexObj* GxTexture::GetTexObj() |
| 210 |
{ |
| 211 |
if (!_texObjValid && _isRenderTarget) { |
| 212 |
// A render target's tiled store is written by EFB copies; (re)create it lazily |
| 213 |
SetRenderTarget(true); |
| 214 |
} |
| 215 |
return (_texObjValid ? &_texObj : nullptr); |
| 216 |
} |
| 217 |
|
| 218 |
GXTexObj* GxTexture::EnsureBakedRgba(const std::uint32_t* paletteRow, std::uint32_t paletteRowIndex, std::uint32_t paletteGeneration, const void* palette) |
| 219 |
{ |
| 220 |
if (_pixels.empty() || _uploadFormat != PixelFormat::RG8 || paletteRow == nullptr) { |
| 221 |
return nullptr; |
| 222 |
} |
| 223 |
const std::uint32_t currentFrame = GxDevice::GetFrameCounter(); |
| 224 |
|
| 225 |
// Bakes that have gone unused for a while give their memory back: every copy costs as much as a |
| 226 |
// full RGBA8 texture, and a row only a menu screen needed would otherwise stay resident for the |
| 227 |
// texture's whole lifetime. Far older than anything the asynchronous FIFO could still read. |
| 228 |
constexpr std::uint32_t ReclaimAfterFrames = 300; |
| 229 |
for (std::int32_t i = 0; i < BakedSlotCount; i++) { |
| 230 |
if (_bakedSlots[i].Store != nullptr && currentFrame - _bakedSlots[i].LastUsedFrame > ReclaimAfterFrames) { |
| 231 |
free(_bakedSlots[i].Store); |
| 232 |
_bakedSlots[i].Store = nullptr; |
| 233 |
_bakedSlots[i].Valid = false; |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
BakedSlot* slot = nullptr; |
| 238 |
for (std::int32_t i = 0; i < BakedSlotCount; i++) { |
| 239 |
if (_bakedSlots[i].Valid && _bakedSlots[i].PaletteRow == paletteRowIndex && _bakedSlots[i].Palette == palette) { |
| 240 |
if (_bakedSlots[i].PaletteGeneration == paletteGeneration && _bakedSlots[i].ContentVersion == _contentVersion) { |
| 241 |
_bakedSlots[i].LastUsedFrame = currentFrame; |
| 242 |
return &_bakedSlots[i].TexObj; |
| 243 |
} |
| 244 |
slot = &_bakedSlots[i]; // Stale bake of the same row, refresh it in place |
| 245 |
break; |
| 246 |
} |
| 247 |
} |
| 248 |
if (slot == nullptr) { |
| 249 |
for (std::int32_t i = 0; i < BakedSlotCount; i++) { |
| 250 |
if (!_bakedSlots[i].Valid) { |
| 251 |
slot = &_bakedSlots[i]; |
| 252 |
break; |
| 253 |
} |
| 254 |
} |
| 255 |
} |
| 256 |
if (slot == nullptr) { |
| 257 |
// Never evict a bake the current frame still references - the FIFO consumes draws |
| 258 |
// asynchronously, so overwriting one could corrupt the already submitted quads |
| 259 |
for (std::int32_t i = 0; i < BakedSlotCount; i++) { |
| 260 |
const std::int32_t candidate = (_nextBakedSlot + i) % BakedSlotCount; |
| 261 |
if (_bakedSlots[candidate].LastUsedFrame != currentFrame) { |
| 262 |
slot = &_bakedSlots[candidate]; |
| 263 |
_nextBakedSlot = (candidate + 1) % BakedSlotCount; |
| 264 |
break; |
| 265 |
} |
| 266 |
} |
| 267 |
} |
| 268 |
if (slot == nullptr) { |
| 269 |
static bool warnedSlots = false; |
| 270 |
if (!warnedSlots) { |
| 271 |
warnedSlots = true; |
| 272 |
LOGW("More than {} palette rows used with one texture in a single frame, expect glitches", BakedSlotCount); |
| 273 |
} |
| 274 |
slot = &_bakedSlots[_nextBakedSlot]; |
| 275 |
_nextBakedSlot = (_nextBakedSlot + 1) % BakedSlotCount; |
| 276 |
} |
| 277 |
|
| 278 |
const std::size_t required = TiledSizeRgba8(_width, _height); |
| 279 |
if (slot->Store == nullptr) { |
| 280 |
slot->Store = static_cast<std::uint8_t*>(memalign(32, required)); |
| 281 |
if (slot->Store == nullptr) { |
| 282 |
LOGE("Out of memory allocating a {} B baked texture store", required); |
| 283 |
return nullptr; |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
// Resolve each texel through the palette row (index -> RGB) and its own alpha byte, into a |
| 288 |
// temporary linear RGBA row consumed by the tiler row by row |
| 289 |
std::vector<std::uint8_t> linear(std::size_t(_width) * std::size_t(_height) * 4); |
| 290 |
for (std::int32_t y = 0; y < _height; y++) { |
| 291 |
const std::uint8_t* src = _pixels.data() + std::size_t(y) * _strideBytes; |
| 292 |
std::uint8_t* dst = linear.data() + std::size_t(y) * _width * 4; |
| 293 |
for (std::int32_t x = 0; x < _width; x++) { |
| 294 |
const std::uint32_t color = paletteRow[src[x * 2]]; |
| 295 |
dst[x * 4 + 0] = std::uint8_t(color & 0xFF); |
| 296 |
dst[x * 4 + 1] = std::uint8_t((color >> 8) & 0xFF); |
| 297 |
dst[x * 4 + 2] = std::uint8_t((color >> 16) & 0xFF); |
| 298 |
dst[x * 4 + 3] = src[x * 2 + 1]; |
| 299 |
} |
| 300 |
} |
| 301 |
TileRgba8(slot->Store, linear.data(), _width, _height, _width * 4, 4); |
| 302 |
InitTexObj(slot->TexObj, slot->Store, GX_TF_RGBA8, false); |
| 303 |
DCFlushRange(slot->Store, std::uint32_t(required)); |
| 304 |
GX_InvalidateTexAll(); |
| 305 |
|
| 306 |
slot->Valid = true; |
| 307 |
slot->PaletteRow = paletteRowIndex; |
| 308 |
slot->PaletteGeneration = paletteGeneration; |
| 309 |
slot->ContentVersion = _contentVersion; |
| 310 |
slot->LastUsedFrame = currentFrame; |
| 311 |
slot->Palette = palette; |
| 312 |
return &slot->TexObj; |
| 313 |
} |
| 314 |
|
| 315 |
void GxTexture::SetRenderTarget(bool isRenderTarget) |
| 316 |
{ |
| 317 |
_isRenderTarget = isRenderTarget; |
| 318 |
if (isRenderTarget && _width > 0 && _height > 0) { |
| 319 |
// The EFB copy writes a tiled RGBA8 image; allocate/refresh the destination store |
| 320 |
const std::size_t required = TiledSizeRgba8(_width, _height); |
| 321 |
if (_tiledStore == nullptr || _tiledStoreSize != required) { |
| 322 |
if (_tiledStore != nullptr) { |
| 323 |
free(_tiledStore); |
| 324 |
} |
| 325 |
_tiledStore = static_cast<std::uint8_t*>(memalign(32, required)); |
| 326 |
_tiledStoreSize = required; |
| 327 |
} |
| 328 |
if (_tiledStore != nullptr) { |
| 329 |
InitTexObj(_texObj, _tiledStore, GX_TF_RGBA8, false); |
| 330 |
_texObjValid = true; |
| 331 |
} |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
bool GxTexture::Bind(std::uint32_t textureUnit) const |
| 336 |
{ |
| 337 |
_textureUnit = textureUnit; |
| 338 |
GxDevice::BindTexture(textureUnit, this); |
| 339 |
return true; |
| 340 |
} |
| 341 |
|
| 342 |
bool GxTexture::Unbind() const |
| 343 |
{ |
| 344 |
GxDevice::BindTexture(_textureUnit, nullptr); |
| 345 |
return true; |
| 346 |
} |
| 347 |
|
| 348 |
bool GxTexture::Unbind(std::uint32_t textureUnit) |
| 349 |
{ |
| 350 |
GxDevice::BindTexture(textureUnit, nullptr); |
| 351 |
return true; |
| 352 |
} |
| 353 |
|
| 354 |
void GxTexture::TexImage2D(std::int32_t level, PixelFormat format, bool bgr, std::int32_t width, std::int32_t height, const void* data) |
| 355 |
{ |
| 356 |
static_cast<void>(bgr); |
| 357 |
if (level != 0) { |
| 358 |
return; // Level 0 only |
| 359 |
} |
| 360 |
Allocate(format, width, height); |
| 361 |
if (data != nullptr && !_pixels.empty()) { |
| 362 |
std::memcpy(_pixels.data(), data, _pixels.size()); |
| 363 |
_contentVersion = ++_nextContentVersion; |
| 364 |
} |
| 365 |
if (_isPaletteTexture) { |
| 366 |
GxDevice::NotifyPaletteTextureChanged(this, 0, _height); |
| 367 |
} else { |
| 368 |
RefreshTiledStore(); |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
void GxTexture::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) |
| 373 |
{ |
| 374 |
static_cast<void>(bgr); |
| 375 |
if (level != 0 || data == nullptr || _pixels.empty()) { |
| 376 |
return; |
| 377 |
} |
| 378 |
const std::int32_t srcBpp = BytesPerPixel(format); |
| 379 |
const std::int32_t dstBpp = _bytesPerPixel; |
| 380 |
const std::int32_t copyBpp = (srcBpp < dstBpp ? srcBpp : dstBpp); |
| 381 |
for (std::int32_t y = 0; y < height; y++) { |
| 382 |
const std::int32_t dstY = yoffset + y; |
| 383 |
if (dstY < 0 || dstY >= _height) { |
| 384 |
continue; |
| 385 |
} |
| 386 |
std::int32_t dstX = xoffset; |
| 387 |
std::int32_t copyW = width; |
| 388 |
std::int32_t srcX0 = 0; |
| 389 |
if (dstX < 0) { |
| 390 |
srcX0 = -dstX; |
| 391 |
copyW += dstX; |
| 392 |
dstX = 0; |
| 393 |
} |
| 394 |
if (dstX + copyW > _width) { |
| 395 |
copyW = _width - dstX; |
| 396 |
} |
| 397 |
if (copyW <= 0) { |
| 398 |
continue; |
| 399 |
} |
| 400 |
const std::uint8_t* srcRow = static_cast<const std::uint8_t*>(data) + std::size_t(y) * std::size_t(width) * srcBpp + std::size_t(srcX0) * srcBpp; |
| 401 |
std::uint8_t* dstRow = _pixels.data() + std::size_t(dstY) * _strideBytes + std::size_t(dstX) * dstBpp; |
| 402 |
if (srcBpp == dstBpp) { |
| 403 |
std::memcpy(dstRow, srcRow, std::size_t(copyW) * dstBpp); |
| 404 |
} else { |
| 405 |
for (std::int32_t x = 0; x < copyW; x++) { |
| 406 |
std::int32_t c = 0; |
| 407 |
for (; c < copyBpp; c++) { |
| 408 |
dstRow[x * dstBpp + c] = srcRow[x * srcBpp + c]; |
| 409 |
} |
| 410 |
for (; c < dstBpp; c++) { |
| 411 |
dstRow[x * dstBpp + c] = 255; |
| 412 |
} |
| 413 |
} |
| 414 |
} |
| 415 |
} |
| 416 |
_contentVersion = ++_nextContentVersion; |
| 417 |
if (_isPaletteTexture) { |
| 418 |
GxDevice::NotifyPaletteTextureChanged(this, yoffset, height); |
| 419 |
} else { |
| 420 |
// v1 keeps the tiling simple: any sub-update re-tiles the whole level (sub-updates are rare - |
| 421 |
// tileset overrides and the palette, which is intercepted above) |
| 422 |
RefreshTiledStore(); |
| 423 |
} |
| 424 |
} |
| 425 |
|
| 426 |
void GxTexture::TexStorage2D(std::int32_t levels, PixelFormat format, std::int32_t width, std::int32_t height) |
| 427 |
{ |
| 428 |
static_cast<void>(levels); |
| 429 |
Allocate(format, width, height); |
| 430 |
if (_isRenderTarget) { |
| 431 |
SetRenderTarget(true); |
| 432 |
} |
| 433 |
} |
| 434 |
|
| 435 |
void GxTexture::CompressedTexImage2D(std::int32_t level, PixelFormat format, std::int32_t width, std::int32_t height, std::int32_t imageSize, const void* data) |
| 436 |
{ |
| 437 |
static_cast<void>(level); static_cast<void>(format); static_cast<void>(width); |
| 438 |
static_cast<void>(height); static_cast<void>(imageSize); static_cast<void>(data); |
| 439 |
} |
| 440 |
|
| 441 |
void GxTexture::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) |
| 442 |
{ |
| 443 |
static_cast<void>(level); static_cast<void>(xoffset); static_cast<void>(yoffset); static_cast<void>(width); |
| 444 |
static_cast<void>(height); static_cast<void>(format); static_cast<void>(imageSize); static_cast<void>(data); |
| 445 |
} |
| 446 |
|
| 447 |
void GxTexture::GetTexImage(std::int32_t level, PixelFormat format, bool bgr, void* pixels) |
| 448 |
{ |
| 449 |
static_cast<void>(level); |
| 450 |
static_cast<void>(format); |
| 451 |
static_cast<void>(bgr); |
| 452 |
if (pixels != nullptr && !_pixels.empty()) { |
| 453 |
std::memcpy(pixels, _pixels.data(), _pixels.size()); |
| 454 |
} |
| 455 |
} |
| 456 |
|
| 457 |
void GxTexture::SetMinFiltering(nCine::SamplerFilter filter) |
| 458 |
{ |
| 459 |
_minFilter = filter; |
| 460 |
if (_texObjValid) { |
| 461 |
const std::uint8_t f = (_magFilter == nCine::SamplerFilter::Linear ? GX_LINEAR : GX_NEAR); |
| 462 |
GX_InitTexObjFilterMode(&_texObj, f, f); |
| 463 |
} |
| 464 |
} |
| 465 |
|
| 466 |
void GxTexture::SetMagFiltering(nCine::SamplerFilter filter) |
| 467 |
{ |
| 468 |
_magFilter = filter; |
| 469 |
if (_texObjValid) { |
| 470 |
const std::uint8_t f = (_magFilter == nCine::SamplerFilter::Linear ? GX_LINEAR : GX_NEAR); |
| 471 |
GX_InitTexObjFilterMode(&_texObj, f, f); |
| 472 |
} |
| 473 |
} |
| 474 |
|
| 475 |
void GxTexture::SetWrap(SamplerWrapping wrap) |
| 476 |
{ |
| 477 |
if (_wrap == wrap) { |
| 478 |
return; |
| 479 |
} |
| 480 |
_wrap = wrap; |
| 481 |
// Wrap only parameterizes GX_InitTexObj (read by InitTexObj), so the texture objects are |
| 482 |
// re-initialized in place over the existing stores - the texel data is unaffected, and re-tiling |
| 483 |
// the whole level through RefreshTiledStore() here was pure wasted work |
| 484 |
if (_texObjValid && _tiledStore != nullptr) { |
| 485 |
// Render targets always hold an RGBA8 store, whatever the upload format was (see SetRenderTarget) |
| 486 |
if (_uploadFormat == PixelFormat::R8 && !_isRenderTarget) { |
| 487 |
InitTexObj(_texObj, _tiledStore, GX_TF_CI8, true); |
| 488 |
} else { |
| 489 |
InitTexObj(_texObj, _tiledStore, GX_TF_RGBA8, false); |
| 490 |
} |
| 491 |
} |
| 492 |
// The baked RGBA8 copies of an RG8 store carry their own texture objects with the same wrap state |
| 493 |
for (std::int32_t i = 0; i < BakedSlotCount; i++) { |
| 494 |
if (_bakedSlots[i].Valid && _bakedSlots[i].Store != nullptr) { |
| 495 |
InitTexObj(_bakedSlots[i].TexObj, _bakedSlots[i].Store, GX_TF_RGBA8, false); |
| 496 |
} |
| 497 |
} |
| 498 |
} |
| 499 |
|
| 500 |
void GxTexture::SetSwizzle(SwizzleChannel r, SwizzleChannel g, SwizzleChannel b, SwizzleChannel a) |
| 501 |
{ |
| 502 |
_swizzle[0] = r; |
| 503 |
_swizzle[1] = g; |
| 504 |
_swizzle[2] = b; |
| 505 |
_swizzle[3] = a; |
| 506 |
} |
| 507 |
|
| 508 |
void GxTexture::SetMaxLevel(std::int32_t maxLevel) |
| 509 |
{ |
| 510 |
static_cast<void>(maxLevel); |
| 511 |
} |
| 512 |
|
| 513 |
void GxTexture::SetUnpackAlignment(std::int32_t alignment) |
| 514 |
{ |
| 515 |
static_cast<void>(alignment); |
| 516 |
} |
| 517 |
|
| 518 |
void GxTexture::SetObjectLabel(StringView label) |
| 519 |
{ |
| 520 |
// The shared palette texture is uploaded by ContentResolver under this exact name; its rows are |
| 521 |
// turned into TLUTs by the device instead of a sampled texture |
| 522 |
if (label == "Palettes"_s) { |
| 523 |
_isPaletteTexture = true; |
| 524 |
FreeTiledStores(); |
| 525 |
GxDevice::RegisterPaletteTexture(this); |
| 526 |
} |
| 527 |
} |
| 528 |
} |