| 1 |
#include "GuTexture.h" |
| 2 |
#include "GuDevice.h" |
| 3 |
#include "../../../../Main.h" |
| 4 |
|
| 5 |
#include <algorithm> |
| 6 |
#include <cstdlib> |
| 7 |
#include <cstring> |
| 8 |
#include <malloc.h> |
| 9 |
|
| 10 |
#include <pspge.h> |
| 11 |
#include <pspgu.h> |
| 12 |
#include <psputils.h> |
| 13 |
|
| 14 |
using namespace Death::Containers::Literals; |
| 15 |
|
| 16 |
namespace nCine::RHI::GU |
| 17 |
{ |
| 18 |
namespace |
| 19 |
{ |
| 20 |
// Rounds a texture dimension up to the next power of two the GE can address. The lower bound is 8 |
| 21 |
// rather than 1 so that every store is wide enough for one 16-byte swizzle block and tall enough for |
| 22 |
// one block row (see SwizzlePageInPlace), which keeps the padding rules uniform. |
| 23 |
std::int32_t NextPow2(std::int32_t value) |
| 24 |
{ |
| 25 |
std::int32_t result = 8; |
| 26 |
while (result < value && result < GuTexture::MaxPageDimension) { |
| 27 |
result <<= 1; |
| 28 |
} |
| 29 |
return result; |
| 30 |
} |
| 31 |
|
| 32 |
// GU_PSM_4444 keeps red in the LOW nibble (the GE's 16-bit formats are little-endian with red first), |
| 33 |
// which is the opposite end from the PowerVR's ARGB4444 |
| 34 |
inline std::uint16_t Abgr4444FromRgba(std::uint8_t r, std::uint8_t g, std::uint8_t b, std::uint8_t a) |
| 35 |
{ |
| 36 |
return std::uint16_t(((a >> 4) << 12) | ((b >> 4) << 8) | ((g >> 4) << 4) | (r >> 4)); |
| 37 |
} |
| 38 |
|
| 39 |
// The engine packs RGB565 with red in the HIGH bits (see Cinematics' palette conversion), while |
| 40 |
// GU_PSM_5650 puts red in the low five - so the two outer channels trade places |
| 41 |
inline std::uint16_t Bgr565FromEngine565(std::uint16_t value) |
| 42 |
{ |
| 43 |
return std::uint16_t(((value & 0xF800u) >> 11) | (value & 0x07E0u) | ((value & 0x001Fu) << 11)); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
@brief Rearranges one page into the GE's texture block interleave, in place |
| 48 |
|
| 49 |
The GE reads a swizzled texture as consecutive blocks of 16 bytes by 8 rows, block rows running |
| 50 |
top to bottom - which turns the 2D neighbourhood a sprite blit samples into a contiguous run and |
| 51 |
is measurably cheaper than the linear layout. The transform maps each block row onto exactly the |
| 52 |
bytes that block row occupied, so one block row of scratch is enough to do it without a second |
| 53 |
full-size buffer. |
| 54 |
*/ |
| 55 |
void SwizzlePageInPlace(std::uint8_t* data, std::int32_t widthBytes, std::int32_t height) |
| 56 |
{ |
| 57 |
// Widest supported page is 512 texels of 4 bytes; a block row is 8 of those |
| 58 |
alignas(16) static std::uint8_t blockRow[8 * GuTexture::MaxPageDimension * 4]; |
| 59 |
const std::int32_t blocksX = widthBytes / 16; |
| 60 |
const std::int32_t blockRows = height / 8; |
| 61 |
for (std::int32_t by = 0; by < blockRows; by++) { |
| 62 |
std::uint8_t* rows = data + std::size_t(by) * 8 * widthBytes; |
| 63 |
std::memcpy(blockRow, rows, std::size_t(8) * std::size_t(widthBytes)); |
| 64 |
std::uint8_t* dst = rows; |
| 65 |
for (std::int32_t bx = 0; bx < blocksX; bx++) { |
| 66 |
for (std::int32_t r = 0; r < 8; r++) { |
| 67 |
std::memcpy(dst, blockRow + std::size_t(r) * std::size_t(widthBytes) + std::size_t(bx) * 16, 16); |
| 68 |
dst += 16; |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
std::uint32_t GuTexture::_nextHandle = 0; |
| 76 |
std::uint32_t GuTexture::_nextContentVersion = 0; |
| 77 |
|
| 78 |
GuTexture::GuTexture(TextureTarget target) |
| 79 |
: _handle(++_nextHandle), _contentVersion(0), _target(target), _format(PixelFormat::Unknown), |
| 80 |
_uploadFormat(PixelFormat::Unknown), _width(0), _height(0), _strideBytes(0), _bytesPerPixel(0), |
| 81 |
_minFilter(nCine::SamplerFilter::Nearest), _magFilter(nCine::SamplerFilter::Nearest), |
| 82 |
_wrap(SamplerWrapping::ClampToEdge), _textureUnit(0), _isRenderTarget(false), _isPaletteTexture(false), |
| 83 |
_geStore(nullptr), _geStoreSize(0), _guFormat(-1), _geBytesPerTexel(0), _pagesX(0), _pagesY(0), |
| 84 |
_geStoreValid(false), _bakedStores{}, _activeBakedStore(-1), _nextBakedStore(0), _uploadCount(0), |
| 85 |
_streamingSwapPending(false), _renderTargetSurface(nullptr), _renderTargetStride(0), |
| 86 |
_renderTargetSurfaceInVram(false) |
| 87 |
{ |
| 88 |
_swizzle[0] = SwizzleChannel::Red; |
| 89 |
_swizzle[1] = SwizzleChannel::Green; |
| 90 |
_swizzle[2] = SwizzleChannel::Blue; |
| 91 |
_swizzle[3] = SwizzleChannel::Alpha; |
| 92 |
} |
| 93 |
|
| 94 |
GuTexture::~GuTexture() |
| 95 |
{ |
| 96 |
// Clear from every unit so a destroyed texture can't dangle in the device's bind tracking, and make |
| 97 |
// sure the GE is not still reading a store that is about to be freed |
| 98 |
GuDevice::UnbindTexture(this); |
| 99 |
FreeGeStores(); |
| 100 |
FreeRenderTargetSurface(); |
| 101 |
} |
| 102 |
|
| 103 |
std::int32_t GuTexture::BytesPerPixel(PixelFormat format) |
| 104 |
{ |
| 105 |
switch (format) { |
| 106 |
case PixelFormat::R8: return 1; |
| 107 |
case PixelFormat::RG8: return 2; |
| 108 |
case PixelFormat::RGB565: return 2; |
| 109 |
case PixelFormat::RGB8: return 3; |
| 110 |
case PixelFormat::RGBA8: return 4; |
| 111 |
default: return 0; |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
void GuTexture::Allocate(PixelFormat format, std::int32_t width, std::int32_t height) |
| 116 |
{ |
| 117 |
_uploadFormat = format; |
| 118 |
_format = format; |
| 119 |
_width = width; |
| 120 |
_height = height; |
| 121 |
_bytesPerPixel = BytesPerPixel(_format); |
| 122 |
_strideBytes = width * _bytesPerPixel; |
| 123 |
_pixels.assign(std::size_t(_strideBytes) * std::size_t(height > 0 ? height : 0), std::uint8_t(0)); |
| 124 |
_uploadCount = 0; |
| 125 |
_streamingSwapPending = false; |
| 126 |
FreeGeStores(); |
| 127 |
FreeRenderTargetSurface(); |
| 128 |
PlanGeStore(); |
| 129 |
_contentVersion = ++_nextContentVersion; |
| 130 |
} |
| 131 |
|
| 132 |
void GuTexture::PlanGeStore() |
| 133 |
{ |
| 134 |
_pages.clear(); |
| 135 |
_pagesX = 0; |
| 136 |
_pagesY = 0; |
| 137 |
_geBytesPerTexel = 0; |
| 138 |
_guFormat = -1; |
| 139 |
if (_width <= 0 || _height <= 0) { |
| 140 |
return; |
| 141 |
} |
| 142 |
|
| 143 |
switch (_uploadFormat) { |
| 144 |
case PixelFormat::R8: |
| 145 |
// Palette indices are exactly what GU_PSM_T8 samples; the CLUT is loaded per draw |
| 146 |
_guFormat = GU_PSM_T8; |
| 147 |
_geBytesPerTexel = 1; |
| 148 |
break; |
| 149 |
case PixelFormat::RG8: |
| 150 |
case PixelFormat::RGB8: |
| 151 |
case PixelFormat::RGBA8: |
| 152 |
// A paletted texel's alpha comes from the CLUT entry, so index+alpha has no hardware form and |
| 153 |
// is baked per palette row; true colour converts to the same 16-bit format (halving both the |
| 154 |
// store and the sampling bandwidth over 8888, which is what the PowerVR settles on too) |
| 155 |
_guFormat = GU_PSM_4444; |
| 156 |
_geBytesPerTexel = 2; |
| 157 |
break; |
| 158 |
case PixelFormat::RGB565: |
| 159 |
_guFormat = GU_PSM_5650; |
| 160 |
_geBytesPerTexel = 2; |
| 161 |
break; |
| 162 |
default: |
| 163 |
return; |
| 164 |
} |
| 165 |
if (_isRenderTarget) { |
| 166 |
// The GE renders into a 16-bit surface matching the display format |
| 167 |
_guFormat = GU_PSM_5650; |
| 168 |
_geBytesPerTexel = 2; |
| 169 |
} |
| 170 |
|
| 171 |
_pagesX = (_width + MaxPageDimension - 1) / MaxPageDimension; |
| 172 |
_pagesY = (_height + MaxPageDimension - 1) / MaxPageDimension; |
| 173 |
_pages.reserve(std::size_t(_pagesX) * std::size_t(_pagesY)); |
| 174 |
for (std::int32_t py = 0; py < _pagesY; py++) { |
| 175 |
for (std::int32_t px = 0; px < _pagesX; px++) { |
| 176 |
Page page; |
| 177 |
page.OriginX = px * MaxPageDimension; |
| 178 |
page.OriginY = py * MaxPageDimension; |
| 179 |
page.Width = std::min(MaxPageDimension, _width - page.OriginX); |
| 180 |
page.Height = std::min(MaxPageDimension, _height - page.OriginY); |
| 181 |
page.PaddedWidth = NextPow2(page.Width); |
| 182 |
// The GE addresses texture rows in 16-byte blocks, so a row has to be at least one block |
| 183 |
// wide - which for an 8bpp store means 16 texels rather than the 8 NextPow2() floors at |
| 184 |
while (page.PaddedWidth * _geBytesPerTexel < 16) { |
| 185 |
page.PaddedWidth <<= 1; |
| 186 |
} |
| 187 |
page.PaddedHeight = NextPow2(page.Height); |
| 188 |
_pages.push_back(page); |
| 189 |
} |
| 190 |
} |
| 191 |
if (_pagesX > 1 || _pagesY > 1) { |
| 192 |
LOGI("Texture {}x{} split into {}x{} GE pages", _width, _height, _pagesX, _pagesY); |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
void GuTexture::InvalidateGeStore() |
| 197 |
{ |
| 198 |
_geStoreValid = false; |
| 199 |
_activeBakedStore = -1; |
| 200 |
for (std::int32_t i = 0; i < BakedStoreCount; i++) { |
| 201 |
_bakedStores[i].Valid = false; |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
void GuTexture::FreeGeStores() |
| 206 |
{ |
| 207 |
if (_geStore != nullptr) { |
| 208 |
std::free(_geStore); |
| 209 |
_geStore = nullptr; |
| 210 |
_geStoreSize = 0; |
| 211 |
} |
| 212 |
for (std::int32_t i = 0; i < BakedStoreCount; i++) { |
| 213 |
if (_bakedStores[i].Data != nullptr) { |
| 214 |
std::free(_bakedStores[i].Data); |
| 215 |
_bakedStores[i].Data = nullptr; |
| 216 |
} |
| 217 |
_bakedStores[i].Valid = false; |
| 218 |
} |
| 219 |
_activeBakedStore = -1; |
| 220 |
_nextBakedStore = 0; |
| 221 |
_geStoreValid = false; |
| 222 |
} |
| 223 |
|
| 224 |
void GuTexture::FreeRenderTargetSurface() |
| 225 |
{ |
| 226 |
if (_renderTargetSurface != nullptr) { |
| 227 |
if (_renderTargetSurfaceInVram) { |
| 228 |
GuDevice::FreeVram(_renderTargetSurface); |
| 229 |
} else { |
| 230 |
std::free(_renderTargetSurface); |
| 231 |
} |
| 232 |
_renderTargetSurface = nullptr; |
| 233 |
_renderTargetStride = 0; |
| 234 |
_renderTargetSurfaceInVram = false; |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
void GuTexture::BuildPage(const Page& page, std::uint8_t* dst, bool swizzle) |
| 239 |
{ |
| 240 |
const std::int32_t rowBytes = page.PaddedWidth * _geBytesPerTexel; |
| 241 |
const std::uint8_t* const hostBase = _pixels.data() + std::size_t(page.OriginY) * _strideBytes |
| 242 |
+ std::size_t(page.OriginX) * _bytesPerPixel; |
| 243 |
|
| 244 |
for (std::int32_t y = 0; y < page.Height; y++) { |
| 245 |
const std::uint8_t* DEATH_RESTRICT src = hostBase + std::size_t(y) * _strideBytes; |
| 246 |
std::uint8_t* DEATH_RESTRICT row = dst + std::size_t(y) * rowBytes; |
| 247 |
switch (_uploadFormat) { |
| 248 |
case PixelFormat::R8: |
| 249 |
std::memcpy(row, src, std::size_t(page.Width)); |
| 250 |
break; |
| 251 |
case PixelFormat::RGB565: { |
| 252 |
std::uint16_t* DEATH_RESTRICT out = reinterpret_cast<std::uint16_t*>(row); |
| 253 |
const std::uint16_t* DEATH_RESTRICT in = reinterpret_cast<const std::uint16_t*>(src); |
| 254 |
for (std::int32_t x = 0; x < page.Width; x++) { |
| 255 |
out[x] = Bgr565FromEngine565(in[x]); |
| 256 |
} |
| 257 |
break; |
| 258 |
} |
| 259 |
case PixelFormat::RGB8: |
| 260 |
case PixelFormat::RGBA8: { |
| 261 |
std::uint16_t* DEATH_RESTRICT out = reinterpret_cast<std::uint16_t*>(row); |
| 262 |
if (_bytesPerPixel >= 4) { |
| 263 |
for (std::int32_t x = 0; x < page.Width; x++) { |
| 264 |
out[x] = Abgr4444FromRgba(src[0], src[1], src[2], src[3]); |
| 265 |
src += 4; |
| 266 |
} |
| 267 |
} else { |
| 268 |
for (std::int32_t x = 0; x < page.Width; x++) { |
| 269 |
out[x] = Abgr4444FromRgba(src[0], src[1], src[2], 255); |
| 270 |
src += _bytesPerPixel; |
| 271 |
} |
| 272 |
} |
| 273 |
break; |
| 274 |
} |
| 275 |
default: |
| 276 |
break; |
| 277 |
} |
| 278 |
// The padding columns are only ever reached by a bilinear tap at the very last texel, but leaving |
| 279 |
// them uninitialised would show up as fringing there |
| 280 |
std::memset(row + std::size_t(page.Width) * _geBytesPerTexel, 0, |
| 281 |
std::size_t(page.PaddedWidth - page.Width) * _geBytesPerTexel); |
| 282 |
} |
| 283 |
if (page.Height < page.PaddedHeight) { |
| 284 |
std::memset(dst + std::size_t(page.Height) * rowBytes, 0, |
| 285 |
std::size_t(page.PaddedHeight - page.Height) * rowBytes); |
| 286 |
} |
| 287 |
if (swizzle) { |
| 288 |
SwizzlePageInPlace(dst, rowBytes, page.PaddedHeight); |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
void GuTexture::BuildBakedPage(const Page& page, std::uint8_t* dst, const std::uint16_t* rgb444, bool swizzle) |
| 293 |
{ |
| 294 |
const std::int32_t rowBytes = page.PaddedWidth * _geBytesPerTexel; |
| 295 |
const std::uint8_t* const hostBase = _pixels.data() + std::size_t(page.OriginY) * _strideBytes |
| 296 |
+ std::size_t(page.OriginX) * _bytesPerPixel; |
| 297 |
|
| 298 |
for (std::int32_t y = 0; y < page.Height; y++) { |
| 299 |
const std::uint8_t* DEATH_RESTRICT src = hostBase + std::size_t(y) * _strideBytes; |
| 300 |
std::uint16_t* DEATH_RESTRICT out = reinterpret_cast<std::uint16_t*>(dst + std::size_t(y) * rowBytes); |
| 301 |
for (std::int32_t x = 0; x < page.Width; x++) { |
| 302 |
// Colour from the palette row the index points at, coverage from the texel's own alpha byte |
| 303 |
out[x] = std::uint16_t(rgb444[src[x * 2]] | std::uint16_t((src[x * 2 + 1] >> 4) << 12)); |
| 304 |
} |
| 305 |
std::memset(out + page.Width, 0, std::size_t(page.PaddedWidth - page.Width) * 2); |
| 306 |
} |
| 307 |
if (page.Height < page.PaddedHeight) { |
| 308 |
std::memset(dst + std::size_t(page.Height) * rowBytes, 0, |
| 309 |
std::size_t(page.PaddedHeight - page.Height) * rowBytes); |
| 310 |
} |
| 311 |
if (swizzle) { |
| 312 |
SwizzlePageInPlace(dst, rowBytes, page.PaddedHeight); |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
bool GuTexture::RefreshGeStore() |
| 317 |
{ |
| 318 |
if (_pages.empty() || _geBytesPerTexel <= 0 || _pixels.empty() || _isPaletteTexture) { |
| 319 |
return false; |
| 320 |
} |
| 321 |
// RG8 has no store of its own - the bake owns it (see EnsureBakedStore) |
| 322 |
if (_uploadFormat == PixelFormat::RG8) { |
| 323 |
return false; |
| 324 |
} |
| 325 |
|
| 326 |
std::size_t size = 0; |
| 327 |
for (const Page& page : _pages) { |
| 328 |
size += std::size_t(page.PaddedWidth) * std::size_t(page.PaddedHeight) * std::size_t(_geBytesPerTexel); |
| 329 |
} |
| 330 |
if (_geStore == nullptr || _geStoreSize != size) { |
| 331 |
if (_geStore != nullptr) { |
| 332 |
std::free(_geStore); |
| 333 |
} |
| 334 |
// The GE fetches texels through physical addresses and expects the base to be quad-word aligned |
| 335 |
_geStore = static_cast<std::uint8_t*>(memalign(16, size)); |
| 336 |
_geStoreSize = (_geStore != nullptr ? size : 0); |
| 337 |
if (_geStore == nullptr) { |
| 338 |
LOGE("Out of memory allocating a {} B GE store for a {}x{} texture", size, _width, _height); |
| 339 |
return false; |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
// Content that is replaced again after its first upload is streamed (the cinematic frames), and |
| 344 |
// interleaving it every frame would cost more than the sampling saves |
| 345 |
std::size_t offset = 0; |
| 346 |
for (Page& page : _pages) { |
| 347 |
const std::int32_t rowBytes = page.PaddedWidth * _geBytesPerTexel; |
| 348 |
const bool swizzle = (_uploadCount <= 1 && _uploadFormat != PixelFormat::RGB565 && |
| 349 |
(rowBytes % 16) == 0 && (page.PaddedHeight % 8) == 0); |
| 350 |
page.Data = _geStore + offset; |
| 351 |
page.Swizzled = swizzle; |
| 352 |
BuildPage(page, _geStore + offset, swizzle); |
| 353 |
offset += std::size_t(rowBytes) * std::size_t(page.PaddedHeight); |
| 354 |
} |
| 355 |
|
| 356 |
// The GE reads main memory without looking at the data cache |
| 357 |
sceKernelDcacheWritebackRange(_geStore, _geStoreSize); |
| 358 |
_geStoreValid = true; |
| 359 |
return true; |
| 360 |
} |
| 361 |
|
| 362 |
bool GuTexture::EnsureBakedStore(const std::uint32_t* paletteRow, std::uint32_t paletteRowIndex, |
| 363 |
std::uint32_t paletteGeneration, const void* palette) |
| 364 |
{ |
| 365 |
if (_uploadFormat != PixelFormat::RG8 || paletteRow == nullptr || _pixels.empty() || _pages.empty()) { |
| 366 |
return false; |
| 367 |
} |
| 368 |
|
| 369 |
// A bake that is already the active store needs nothing at all, which is the case for every draw of |
| 370 |
// the frame once the sprite's palette row has been seen once |
| 371 |
for (std::int32_t i = 0; i < BakedStoreCount; i++) { |
| 372 |
const BakedStore& store = _bakedStores[i]; |
| 373 |
if (store.Valid && store.PaletteRow == paletteRowIndex && store.Palette == palette && |
| 374 |
store.PaletteGeneration == paletteGeneration && store.ContentVersion == _contentVersion) { |
| 375 |
if (_activeBakedStore != i) { |
| 376 |
_activeBakedStore = i; |
| 377 |
std::size_t offset = 0; |
| 378 |
for (Page& page : _pages) { |
| 379 |
page.Data = store.Data + offset; |
| 380 |
offset += std::size_t(page.PaddedWidth) * std::size_t(page.PaddedHeight) * 2; |
| 381 |
} |
| 382 |
} |
| 383 |
_geStoreValid = true; |
| 384 |
return true; |
| 385 |
} |
| 386 |
} |
| 387 |
|
| 388 |
std::size_t size = 0; |
| 389 |
for (const Page& page : _pages) { |
| 390 |
size += std::size_t(page.PaddedWidth) * std::size_t(page.PaddedHeight) * 2; |
| 391 |
} |
| 392 |
|
| 393 |
// Reuse a stale entry of the same row, then any free one, then the round-robin victim |
| 394 |
std::int32_t slot = -1; |
| 395 |
for (std::int32_t i = 0; i < BakedStoreCount && slot < 0; i++) { |
| 396 |
if (_bakedStores[i].Valid && _bakedStores[i].PaletteRow == paletteRowIndex && |
| 397 |
_bakedStores[i].Palette == palette) { |
| 398 |
slot = i; |
| 399 |
} |
| 400 |
} |
| 401 |
for (std::int32_t i = 0; i < BakedStoreCount && slot < 0; i++) { |
| 402 |
if (!_bakedStores[i].Valid) { |
| 403 |
slot = i; |
| 404 |
} |
| 405 |
} |
| 406 |
if (slot < 0) { |
| 407 |
slot = _nextBakedStore; |
| 408 |
_nextBakedStore = (_nextBakedStore + 1) % BakedStoreCount; |
| 409 |
static bool warnedSlots = false; |
| 410 |
if (!warnedSlots) { |
| 411 |
warnedSlots = true; |
| 412 |
LOGW("More than {} palette rows used with one indexed+alpha texture, expect rebuild churn", BakedStoreCount); |
| 413 |
} |
| 414 |
} |
| 415 |
|
| 416 |
BakedStore& store = _bakedStores[slot]; |
| 417 |
if (store.Data == nullptr) { |
| 418 |
store.Data = static_cast<std::uint8_t*>(memalign(16, size)); |
| 419 |
if (store.Data == nullptr) { |
| 420 |
LOGE("Out of memory allocating a {} B baked store for a {}x{} texture", size, _width, _height); |
| 421 |
return false; |
| 422 |
} |
| 423 |
} |
| 424 |
|
| 425 |
// The 256 palette entries collapse into packed 12-bit BGR once per bake, so the texel loop is one |
| 426 |
// lookup and an alpha merge instead of unpacking and requantizing every pixel |
| 427 |
std::uint16_t rgb444[256]; |
| 428 |
for (std::int32_t i = 0; i < 256; i++) { |
| 429 |
const std::uint32_t color = paletteRow[i]; |
| 430 |
rgb444[i] = std::uint16_t(((((color >> 16) & 0xFF) >> 4) << 8) |
| 431 |
| ((((color >> 8) & 0xFF) >> 4) << 4) |
| 432 |
| (((color) & 0xFF) >> 4)); |
| 433 |
} |
| 434 |
|
| 435 |
std::size_t offset = 0; |
| 436 |
for (Page& page : _pages) { |
| 437 |
const std::int32_t rowBytes = page.PaddedWidth * 2; |
| 438 |
const bool swizzle = (_uploadCount <= 1 && (rowBytes % 16) == 0 && (page.PaddedHeight % 8) == 0); |
| 439 |
page.Data = store.Data + offset; |
| 440 |
page.Swizzled = swizzle; |
| 441 |
BuildBakedPage(page, store.Data + offset, rgb444, swizzle); |
| 442 |
offset += std::size_t(rowBytes) * std::size_t(page.PaddedHeight); |
| 443 |
} |
| 444 |
sceKernelDcacheWritebackRange(store.Data, size); |
| 445 |
|
| 446 |
store.Valid = true; |
| 447 |
store.PaletteRow = paletteRowIndex; |
| 448 |
store.PaletteGeneration = paletteGeneration; |
| 449 |
store.ContentVersion = _contentVersion; |
| 450 |
store.Palette = palette; |
| 451 |
_activeBakedStore = slot; |
| 452 |
_geStoreValid = true; |
| 453 |
return true; |
| 454 |
} |
| 455 |
|
| 456 |
const GuTexture::Page* GuTexture::AcquirePage(std::int32_t texelX, std::int32_t texelY) |
| 457 |
{ |
| 458 |
if (_pages.empty()) { |
| 459 |
return nullptr; |
| 460 |
} |
| 461 |
if (_isRenderTarget) { |
| 462 |
// The GE wrote the surface itself; there is nothing to convert |
| 463 |
return (_renderTargetSurface != nullptr ? &_pages[0] : nullptr); |
| 464 |
} |
| 465 |
if (_streamingSwapPending) { |
| 466 |
// The cinematic path wrote engine-order RGB565 straight into the store, so the two outer |
| 467 |
// channels are traded in place once - a single pass over the frame, where a separate host copy |
| 468 |
// would have cost a conversion AND a copy |
| 469 |
_streamingSwapPending = false; |
| 470 |
Page& page = _pages[0]; |
| 471 |
std::uint16_t* const base = reinterpret_cast<std::uint16_t*>(_geStore); |
| 472 |
for (std::int32_t y = 0; y < page.Height; y++) { |
| 473 |
std::uint16_t* DEATH_RESTRICT row = base + std::size_t(y) * page.PaddedWidth; |
| 474 |
for (std::int32_t x = 0; x < page.Width; x++) { |
| 475 |
row[x] = Bgr565FromEngine565(row[x]); |
| 476 |
} |
| 477 |
} |
| 478 |
sceKernelDcacheWritebackRange(_geStore, _geStoreSize); |
| 479 |
} |
| 480 |
if (!_geStoreValid && !RefreshGeStore()) { |
| 481 |
return nullptr; |
| 482 |
} |
| 483 |
|
| 484 |
const std::int32_t px = (texelX < 0 ? 0 : (texelX >= _width ? _width - 1 : texelX)) / MaxPageDimension; |
| 485 |
const std::int32_t py = (texelY < 0 ? 0 : (texelY >= _height ? _height - 1 : texelY)) / MaxPageDimension; |
| 486 |
return &_pages[std::size_t(py) * std::size_t(_pagesX) + std::size_t(px)]; |
| 487 |
} |
| 488 |
|
| 489 |
void* GuTexture::MapStreamingTexels(std::int32_t& strideBytes) |
| 490 |
{ |
| 491 |
// Only the format the hardware samples verbatim, and only while the whole image fits one page (a |
| 492 |
// paged store has no single linear pitch the caller could write through) |
| 493 |
if (_uploadFormat != PixelFormat::RGB565 || _isRenderTarget || _isPaletteTexture || |
| 494 |
_pages.size() != 1 || _width <= 0 || _height <= 0) { |
| 495 |
return nullptr; |
| 496 |
} |
| 497 |
if (_geStore == nullptr) { |
| 498 |
// The very first frame allocates the store through the ordinary rebuild |
| 499 |
_uploadCount = 2; // Streaming content is never swizzled - it is rewritten every frame |
| 500 |
if (!RefreshGeStore()) { |
| 501 |
return nullptr; |
| 502 |
} |
| 503 |
} |
| 504 |
|
| 505 |
strideBytes = _pages[0].PaddedWidth * 2; |
| 506 |
_streamingSwapPending = true; |
| 507 |
_geStoreValid = true; |
| 508 |
_contentVersion = ++_nextContentVersion; |
| 509 |
return _geStore; |
| 510 |
} |
| 511 |
|
| 512 |
void GuTexture::SetRenderTarget(bool isRenderTarget) |
| 513 |
{ |
| 514 |
if (_isRenderTarget == isRenderTarget) { |
| 515 |
return; |
| 516 |
} |
| 517 |
_isRenderTarget = isRenderTarget; |
| 518 |
FreeGeStores(); |
| 519 |
FreeRenderTargetSurface(); |
| 520 |
PlanGeStore(); |
| 521 |
if (!isRenderTarget || _width <= 0 || _height <= 0) { |
| 522 |
return; |
| 523 |
} |
| 524 |
if (_pages.size() != 1) { |
| 525 |
LOGE("A {}x{} render target cannot be sampled as one GE texture (limit is {})", _width, _height, |
| 526 |
MaxPageDimension); |
| 527 |
return; |
| 528 |
} |
| 529 |
|
| 530 |
// The GE writes into a 16-bit surface whose pitch is the padded width. Video memory is worth using |
| 531 |
// here - unlike for sprite atlases, the target is written by the rasterizer every frame - but it is |
| 532 |
// only 2 MB with the display buffers already in it, so main memory is an accepted fallback. |
| 533 |
Page& page = _pages[0]; |
| 534 |
const std::size_t size = std::size_t(page.PaddedWidth) * std::size_t(page.PaddedHeight) * 2; |
| 535 |
_renderTargetSurface = GuDevice::AllocateVram(size); |
| 536 |
_renderTargetSurfaceInVram = (_renderTargetSurface != nullptr); |
| 537 |
if (_renderTargetSurface == nullptr) { |
| 538 |
_renderTargetSurface = memalign(16, size); |
| 539 |
if (_renderTargetSurface == nullptr) { |
| 540 |
LOGE("Out of memory allocating a {} B render-target surface ({}x{})", size, _width, _height); |
| 541 |
return; |
| 542 |
} |
| 543 |
LOGW("Render target {}x{} did not fit video memory and renders into main memory", _width, _height); |
| 544 |
} |
| 545 |
// A render target has no host content worth keeping - the rasterizer owns every texel - and its |
| 546 |
// linear store would be another quarter of a megabyte of main memory for nothing |
| 547 |
_pixels = {}; |
| 548 |
|
| 549 |
_renderTargetStride = page.PaddedWidth; |
| 550 |
// The two GE registers this surface reaches do NOT take the same form of address. The draw-buffer |
| 551 |
// family (sceGuDrawBufferList) takes an offset RELATIVE to the start of video memory - which is |
| 552 |
// exactly what AllocateVram() hands out, and why `sceGuDrawBuffer(psm, 0, stride)` means "the front |
| 553 |
// of VRAM" - while sceGuTexImage takes an ABSOLUTE address, because a texture may just as well live |
| 554 |
// in main memory. Sampling the relative offset would point the texture unit at low memory instead of |
| 555 |
// the surface, so the page keeps the absolute form (the main-memory fallback below is already |
| 556 |
// absolute and needs no base). |
| 557 |
void* absoluteSurface = (_renderTargetSurfaceInVram |
| 558 |
? static_cast<std::uint8_t*>(sceGeEdramGetAddr()) + std::uintptr_t(_renderTargetSurface) |
| 559 |
: static_cast<std::uint8_t*>(_renderTargetSurface)); |
| 560 |
page.Data = absoluteSurface; |
| 561 |
page.Swizzled = false; |
| 562 |
_geStoreValid = true; |
| 563 |
|
| 564 |
std::memset(absoluteSurface, 0, size); |
| 565 |
if (!_renderTargetSurfaceInVram) { |
| 566 |
sceKernelDcacheWritebackRange(absoluteSurface, size); |
| 567 |
} |
| 568 |
} |
| 569 |
|
| 570 |
bool GuTexture::Bind(std::uint32_t textureUnit) const |
| 571 |
{ |
| 572 |
_textureUnit = textureUnit; |
| 573 |
GuDevice::BindTexture(textureUnit, this); |
| 574 |
return true; |
| 575 |
} |
| 576 |
|
| 577 |
bool GuTexture::Unbind() const |
| 578 |
{ |
| 579 |
GuDevice::BindTexture(_textureUnit, nullptr); |
| 580 |
return true; |
| 581 |
} |
| 582 |
|
| 583 |
bool GuTexture::Unbind(std::uint32_t textureUnit) |
| 584 |
{ |
| 585 |
GuDevice::BindTexture(textureUnit, nullptr); |
| 586 |
return true; |
| 587 |
} |
| 588 |
|
| 589 |
void GuTexture::TexImage2D(std::int32_t level, PixelFormat format, bool bgr, std::int32_t width, std::int32_t height, const void* data) |
| 590 |
{ |
| 591 |
static_cast<void>(bgr); |
| 592 |
if (level != 0) { |
| 593 |
return; // Level 0 only |
| 594 |
} |
| 595 |
Allocate(format, width, height); |
| 596 |
if (data != nullptr && !_pixels.empty()) { |
| 597 |
std::memcpy(_pixels.data(), data, _pixels.size()); |
| 598 |
_contentVersion = ++_nextContentVersion; |
| 599 |
_uploadCount++; |
| 600 |
} |
| 601 |
if (_isPaletteTexture) { |
| 602 |
GuDevice::NotifyPaletteTextureChanged(this, 0, _height); |
| 603 |
} else { |
| 604 |
InvalidateGeStore(); |
| 605 |
} |
| 606 |
} |
| 607 |
|
| 608 |
void GuTexture::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) |
| 609 |
{ |
| 610 |
static_cast<void>(bgr); |
| 611 |
if (level != 0 || data == nullptr || _pixels.empty()) { |
| 612 |
return; |
| 613 |
} |
| 614 |
const std::int32_t srcBpp = BytesPerPixel(format); |
| 615 |
const std::int32_t dstBpp = _bytesPerPixel; |
| 616 |
const std::int32_t copyBpp = (srcBpp < dstBpp ? srcBpp : dstBpp); |
| 617 |
|
| 618 |
for (std::int32_t y = 0; y < height; y++) { |
| 619 |
const std::int32_t dstY = yoffset + y; |
| 620 |
if (dstY < 0 || dstY >= _height) { |
| 621 |
continue; |
| 622 |
} |
| 623 |
std::int32_t dstX = xoffset; |
| 624 |
std::int32_t copyW = width; |
| 625 |
std::int32_t srcX0 = 0; |
| 626 |
if (dstX < 0) { |
| 627 |
srcX0 = -dstX; |
| 628 |
copyW += dstX; |
| 629 |
dstX = 0; |
| 630 |
} |
| 631 |
if (dstX + copyW > _width) { |
| 632 |
copyW = _width - dstX; |
| 633 |
} |
| 634 |
if (copyW <= 0) { |
| 635 |
continue; |
| 636 |
} |
| 637 |
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; |
| 638 |
std::uint8_t* dstRow = _pixels.data() + std::size_t(dstY) * _strideBytes + std::size_t(dstX) * dstBpp; |
| 639 |
if (srcBpp == dstBpp) { |
| 640 |
std::memcpy(dstRow, srcRow, std::size_t(copyW) * dstBpp); |
| 641 |
} else { |
| 642 |
// A narrower source fills the channels it has and opaque-fills the rest, which is what the |
| 643 |
// RGB8-into-RGBA8 uploads the loaders produce need |
| 644 |
for (std::int32_t x = 0; x < copyW; x++) { |
| 645 |
std::int32_t c = 0; |
| 646 |
for (; c < copyBpp; c++) { |
| 647 |
dstRow[x * dstBpp + c] = srcRow[x * srcBpp + c]; |
| 648 |
} |
| 649 |
for (; c < dstBpp; c++) { |
| 650 |
dstRow[x * dstBpp + c] = 255; |
| 651 |
} |
| 652 |
} |
| 653 |
} |
| 654 |
} |
| 655 |
_contentVersion = ++_nextContentVersion; |
| 656 |
if (xoffset == 0 && yoffset == 0 && width == _width && height == _height) { |
| 657 |
_uploadCount++; |
| 658 |
} |
| 659 |
if (_isPaletteTexture) { |
| 660 |
GuDevice::NotifyPaletteTextureChanged(this, yoffset, height); |
| 661 |
} else { |
| 662 |
// v1: any sub-update rebuilds the whole GE store on the next draw (sub-updates are rare - tileset |
| 663 |
// overrides at load time, and the palette texture, which is intercepted above) |
| 664 |
InvalidateGeStore(); |
| 665 |
} |
| 666 |
} |
| 667 |
|
| 668 |
void GuTexture::TexStorage2D(std::int32_t levels, PixelFormat format, std::int32_t width, std::int32_t height) |
| 669 |
{ |
| 670 |
static_cast<void>(levels); |
| 671 |
const bool wasRenderTarget = _isRenderTarget; |
| 672 |
Allocate(format, width, height); |
| 673 |
if (wasRenderTarget) { |
| 674 |
// Allocate() dropped the surface with the old size; re-attach one for the new one |
| 675 |
_isRenderTarget = false; |
| 676 |
SetRenderTarget(true); |
| 677 |
} |
| 678 |
} |
| 679 |
|
| 680 |
void GuTexture::CompressedTexImage2D(std::int32_t level, PixelFormat format, std::int32_t width, std::int32_t height, std::int32_t imageSize, const void* data) |
| 681 |
{ |
| 682 |
static_cast<void>(level); static_cast<void>(format); static_cast<void>(width); |
| 683 |
static_cast<void>(height); static_cast<void>(imageSize); static_cast<void>(data); |
| 684 |
} |
| 685 |
|
| 686 |
void GuTexture::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) |
| 687 |
{ |
| 688 |
static_cast<void>(level); static_cast<void>(xoffset); static_cast<void>(yoffset); static_cast<void>(width); |
| 689 |
static_cast<void>(height); static_cast<void>(format); static_cast<void>(imageSize); static_cast<void>(data); |
| 690 |
} |
| 691 |
|
| 692 |
void GuTexture::GetTexImage(std::int32_t level, PixelFormat format, bool bgr, void* pixels) |
| 693 |
{ |
| 694 |
static_cast<void>(level); |
| 695 |
static_cast<void>(format); |
| 696 |
static_cast<void>(bgr); |
| 697 |
if (pixels != nullptr && !_pixels.empty()) { |
| 698 |
std::memcpy(pixels, _pixels.data(), _pixels.size()); |
| 699 |
} |
| 700 |
} |
| 701 |
|
| 702 |
void GuTexture::SetMinFiltering(nCine::SamplerFilter filter) |
| 703 |
{ |
| 704 |
_minFilter = filter; |
| 705 |
} |
| 706 |
|
| 707 |
void GuTexture::SetMagFiltering(nCine::SamplerFilter filter) |
| 708 |
{ |
| 709 |
_magFilter = filter; |
| 710 |
} |
| 711 |
|
| 712 |
void GuTexture::SetWrap(SamplerWrapping wrap) |
| 713 |
{ |
| 714 |
_wrap = wrap; |
| 715 |
} |
| 716 |
|
| 717 |
void GuTexture::SetSwizzle(SwizzleChannel r, SwizzleChannel g, SwizzleChannel b, SwizzleChannel a) |
| 718 |
{ |
| 719 |
_swizzle[0] = r; |
| 720 |
_swizzle[1] = g; |
| 721 |
_swizzle[2] = b; |
| 722 |
_swizzle[3] = a; |
| 723 |
} |
| 724 |
|
| 725 |
void GuTexture::SetMaxLevel(std::int32_t maxLevel) |
| 726 |
{ |
| 727 |
static_cast<void>(maxLevel); |
| 728 |
} |
| 729 |
|
| 730 |
void GuTexture::SetUnpackAlignment(std::int32_t alignment) |
| 731 |
{ |
| 732 |
static_cast<void>(alignment); |
| 733 |
} |
| 734 |
|
| 735 |
void GuTexture::SetObjectLabel(StringView label) |
| 736 |
{ |
| 737 |
// The shared palette texture is uploaded by ContentResolver under this exact name; its rows become |
| 738 |
// hardware CLUTs loaded per draw instead of a sampled texture, so it keeps only the host store |
| 739 |
if (label == "Palettes"_s) { |
| 740 |
_isPaletteTexture = true; |
| 741 |
FreeGeStores(); |
| 742 |
GuDevice::RegisterPaletteTexture(this); |
| 743 |
} |
| 744 |
} |
| 745 |
} |