| 1 |
#include "GsVram.h" |
| 2 |
|
| 3 |
namespace nCine::RHI::GS |
| 4 |
{ |
| 5 |
std::uint64_t GsVram::_pageBitmap[GsVram::BitmapWords] = {}; |
| 6 |
std::uint32_t GsVram::_clutBitmap = 0; |
| 7 |
std::uint32_t GsVram::_clutSlotCount = 0; |
| 8 |
std::uint32_t GsVram::_clutFirstBlock = 0; |
| 9 |
std::uint32_t GsVram::_displayBufferPage[2] = {}; |
| 10 |
std::int32_t GsVram::_displayBufferCount = 0; |
| 11 |
std::int32_t GsVram::_displayBufferWidth = 0; |
| 12 |
GsPsm GsVram::_displayPsm = GsPsm::Ct16; |
| 13 |
std::uint32_t GsVram::_reserveFirstPage = 0; |
| 14 |
std::uint32_t GsVram::_reservePageCount = 0; |
| 15 |
std::uint32_t GsVram::_cacheFirstPage = 0; |
| 16 |
std::uint32_t GsVram::_cachePageCount = 0; |
| 17 |
std::uint32_t GsVram::_usedPageCount = 0; |
| 18 |
std::uint32_t GsVram::_peakUsedPageCount = 0; |
| 19 |
std::uint32_t GsVram::_failedAllocationCount = 0; |
| 20 |
bool GsVram::_initialized = false; |
| 21 |
|
| 22 |
namespace |
| 23 |
{ |
| 24 |
/** @brief Number of CLUT slots the slab bitmap can track */ |
| 25 |
constexpr std::uint32_t MaxClutSlots = 32; |
| 26 |
/** @brief Blocks of one CLUT slot */ |
| 27 |
constexpr std::uint32_t ClutSlotBlocks = GsVram::ClutSlotBytes / GsVram::BlockBytes; |
| 28 |
|
| 29 |
constexpr std::uint32_t AlignUp(std::uint32_t value, std::uint32_t alignment) |
| 30 |
{ |
| 31 |
return (value + alignment - 1) & ~(alignment - 1); |
| 32 |
} |
| 33 |
|
| 34 |
constexpr std::int32_t AlignUpInt(std::int32_t value, std::int32_t alignment) |
| 35 |
{ |
| 36 |
return (value + alignment - 1) / alignment * alignment; |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
void GsVram::GetPageGeometry(GsPsm psm, std::int32_t& width, std::int32_t& height) |
| 41 |
{ |
| 42 |
// One page is always 8 KB, so the texels it covers follow from the bits per texel - but the split |
| 43 |
// between the two axes is fixed by the hardware's page layout, not chosen |
| 44 |
switch (psm) { |
| 45 |
case GsPsm::Ct32: |
| 46 |
case GsPsm::Ct24: |
| 47 |
width = 64; |
| 48 |
height = 32; |
| 49 |
break; |
| 50 |
case GsPsm::Ct16: |
| 51 |
case GsPsm::Ct16S: |
| 52 |
width = 64; |
| 53 |
height = 64; |
| 54 |
break; |
| 55 |
case GsPsm::T8: |
| 56 |
width = 128; |
| 57 |
height = 64; |
| 58 |
break; |
| 59 |
case GsPsm::T4: |
| 60 |
width = 128; |
| 61 |
height = 128; |
| 62 |
break; |
| 63 |
default: |
| 64 |
// Unknown mode: assume the densest page so the reservation cannot come out too small |
| 65 |
width = 64; |
| 66 |
height = 32; |
| 67 |
break; |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
std::uint32_t GsVram::GetPageCount(GsPsm psm, std::int32_t width, std::int32_t height) |
| 72 |
{ |
| 73 |
if (width <= 0 || height <= 0) { |
| 74 |
return 0; |
| 75 |
} |
| 76 |
|
| 77 |
std::int32_t pageWidth, pageHeight; |
| 78 |
GetPageGeometry(psm, pageWidth, pageHeight); |
| 79 |
|
| 80 |
const std::int32_t pagesX = AlignUpInt(width, pageWidth) / pageWidth; |
| 81 |
const std::int32_t pagesY = AlignUpInt(height, pageHeight) / pageHeight; |
| 82 |
return std::uint32_t(pagesX) * std::uint32_t(pagesY); |
| 83 |
} |
| 84 |
|
| 85 |
std::int32_t GsVram::GetBufferWidth(GsPsm psm, std::int32_t width) |
| 86 |
{ |
| 87 |
if (width <= 0) { |
| 88 |
return 1; |
| 89 |
} |
| 90 |
|
| 91 |
std::int32_t pageWidth, pageHeight; |
| 92 |
GetPageGeometry(psm, pageWidth, pageHeight); |
| 93 |
|
| 94 |
// Padded to the page geometry, because that is the pitch the reserved pages were counted for. |
| 95 |
// `TBW`/`FBW` count 64 texels each, and every page geometry is a multiple of that, so the |
| 96 |
// division is exact |
| 97 |
return AlignUpInt(width, pageWidth) / BufferWidthUnit; |
| 98 |
} |
| 99 |
|
| 100 |
std::int32_t GsVram::GetPaddedWidth(GsPsm psm, std::int32_t width) |
| 101 |
{ |
| 102 |
if (width <= 0) { |
| 103 |
return BufferWidthUnit; |
| 104 |
} |
| 105 |
|
| 106 |
std::int32_t pageWidth, pageHeight; |
| 107 |
GetPageGeometry(psm, pageWidth, pageHeight); |
| 108 |
return AlignUpInt(width, pageWidth); |
| 109 |
} |
| 110 |
|
| 111 |
bool GsVram::Initialize(const GsVramLayout& layout) |
| 112 |
{ |
| 113 |
if (layout.DisplayWidth <= 0 || layout.DisplayHeight <= 0 || |
| 114 |
layout.DisplayBufferCount < 1 || layout.DisplayBufferCount > 2) { |
| 115 |
return false; |
| 116 |
} |
| 117 |
|
| 118 |
const std::uint32_t displayPages = GetPageCount(layout.DisplayPsm, layout.DisplayWidth, layout.DisplayHeight); |
| 119 |
const std::uint32_t clutSlots = (layout.ClutSlotCount < MaxClutSlots ? layout.ClutSlotCount : MaxClutSlots); |
| 120 |
const std::uint32_t clutPages = AlignUp(clutSlots * ClutSlotBytes, PageBytes) / PageBytes; |
| 121 |
|
| 122 |
const std::uint32_t staticPages = displayPages * std::uint32_t(layout.DisplayBufferCount) + layout.ReservedRttPages + clutPages; |
| 123 |
if (staticPages >= TotalPages) { |
| 124 |
// Nothing is committed on failure, so the caller can retry with a smaller display mode |
| 125 |
return false; |
| 126 |
} |
| 127 |
|
| 128 |
for (std::uint32_t i = 0; i < BitmapWords; i++) { |
| 129 |
_pageBitmap[i] = 0; |
| 130 |
} |
| 131 |
_clutBitmap = 0; |
| 132 |
_usedPageCount = 0; |
| 133 |
_peakUsedPageCount = 0; |
| 134 |
_failedAllocationCount = 0; |
| 135 |
|
| 136 |
std::uint32_t nextPage = 0; |
| 137 |
|
| 138 |
_displayBufferCount = layout.DisplayBufferCount; |
| 139 |
_displayBufferWidth = GetBufferWidth(layout.DisplayPsm, layout.DisplayWidth); |
| 140 |
_displayPsm = layout.DisplayPsm; |
| 141 |
for (std::int32_t i = 0; i < layout.DisplayBufferCount; i++) { |
| 142 |
_displayBufferPage[i] = nextPage; |
| 143 |
nextPage += displayPages; |
| 144 |
} |
| 145 |
if (layout.DisplayBufferCount == 1) { |
| 146 |
_displayBufferPage[1] = _displayBufferPage[0]; |
| 147 |
} |
| 148 |
|
| 149 |
_reserveFirstPage = nextPage; |
| 150 |
_reservePageCount = layout.ReservedRttPages; |
| 151 |
nextPage += layout.ReservedRttPages; |
| 152 |
|
| 153 |
_clutFirstBlock = nextPage * BlocksPerPage; |
| 154 |
_clutSlotCount = clutSlots; |
| 155 |
nextPage += clutPages; |
| 156 |
|
| 157 |
_cacheFirstPage = nextPage; |
| 158 |
_cachePageCount = TotalPages - nextPage; |
| 159 |
|
| 160 |
// The static regions are outside both allocation windows, so they are never handed out and do not |
| 161 |
// need to be marked; the bitmap only ever describes the reserve and the cache |
| 162 |
_initialized = true; |
| 163 |
return true; |
| 164 |
} |
| 165 |
|
| 166 |
bool GsVram::IsInitialized() |
| 167 |
{ |
| 168 |
return _initialized; |
| 169 |
} |
| 170 |
|
| 171 |
std::uint32_t GsVram::GetDisplayBufferPage(std::int32_t index) |
| 172 |
{ |
| 173 |
return _displayBufferPage[index & 1]; |
| 174 |
} |
| 175 |
|
| 176 |
std::int32_t GsVram::GetDisplayBufferWidth() |
| 177 |
{ |
| 178 |
return _displayBufferWidth; |
| 179 |
} |
| 180 |
|
| 181 |
GsPsm GsVram::GetDisplayPsm() |
| 182 |
{ |
| 183 |
return _displayPsm; |
| 184 |
} |
| 185 |
|
| 186 |
bool GsVram::IsPageFree(std::uint32_t page) |
| 187 |
{ |
| 188 |
return ((_pageBitmap[page >> 6] & (std::uint64_t(1) << (page & 63))) == 0); |
| 189 |
} |
| 190 |
|
| 191 |
void GsVram::MarkRange(std::uint32_t firstPage, std::uint32_t pageCount, bool used) |
| 192 |
{ |
| 193 |
for (std::uint32_t page = firstPage; page < firstPage + pageCount; page++) { |
| 194 |
const std::uint64_t mask = std::uint64_t(1) << (page & 63); |
| 195 |
if (used) { |
| 196 |
_pageBitmap[page >> 6] |= mask; |
| 197 |
} else { |
| 198 |
_pageBitmap[page >> 6] &= ~mask; |
| 199 |
} |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
std::uint32_t GsVram::AllocateWithin(std::uint32_t windowFirstPage, std::uint32_t windowPageCount, std::uint32_t pageCount) |
| 204 |
{ |
| 205 |
if (pageCount == 0 || pageCount > windowPageCount) { |
| 206 |
return InvalidPage; |
| 207 |
} |
| 208 |
|
| 209 |
// Best fit: the shortest free run that still holds the request, lowest address among equals. First |
| 210 |
// fit is the obvious alternative and is worse here for one specific reason - the working set mixes a |
| 211 |
// handful of large atlases with dozens of one-page sheets, and first fit spends the head of the |
| 212 |
// window (which after a drain is the only run long enough for an atlas) on whichever small sheet |
| 213 |
// asked first. Best fit spends the short runs on the short requests, which is exactly what keeps the |
| 214 |
// long ones available. The scan is over 512 bits and runs a few dozen times a frame. |
| 215 |
const std::uint32_t windowEndPage = windowFirstPage + windowPageCount; |
| 216 |
std::uint32_t bestPage = InvalidPage, bestLength = 0; |
| 217 |
std::uint32_t runStart = 0, runLength = 0; |
| 218 |
|
| 219 |
for (std::uint32_t page = windowFirstPage; page <= windowEndPage; page++) { |
| 220 |
if (page < windowEndPage && IsPageFree(page)) { |
| 221 |
if (runLength == 0) { |
| 222 |
runStart = page; |
| 223 |
} |
| 224 |
runLength++; |
| 225 |
continue; |
| 226 |
} |
| 227 |
// A used page (or the end of the window) closes the run that led up to it |
| 228 |
if (runLength >= pageCount && (bestLength == 0 || runLength < bestLength)) { |
| 229 |
bestPage = runStart; |
| 230 |
bestLength = runLength; |
| 231 |
if (runLength == pageCount) { |
| 232 |
break; // An exact fit cannot be improved on |
| 233 |
} |
| 234 |
} |
| 235 |
runLength = 0; |
| 236 |
} |
| 237 |
|
| 238 |
if (bestPage == InvalidPage) { |
| 239 |
return InvalidPage; |
| 240 |
} |
| 241 |
MarkRange(bestPage, pageCount, true); |
| 242 |
return bestPage; |
| 243 |
} |
| 244 |
|
| 245 |
std::uint32_t GsVram::LargestFreeRunWithin(std::uint32_t windowFirstPage, std::uint32_t windowPageCount) |
| 246 |
{ |
| 247 |
const std::uint32_t windowEndPage = windowFirstPage + windowPageCount; |
| 248 |
std::uint32_t longest = 0, runLength = 0; |
| 249 |
for (std::uint32_t page = windowFirstPage; page < windowEndPage; page++) { |
| 250 |
runLength = (IsPageFree(page) ? runLength + 1 : 0); |
| 251 |
if (runLength > longest) { |
| 252 |
longest = runLength; |
| 253 |
} |
| 254 |
} |
| 255 |
return longest; |
| 256 |
} |
| 257 |
|
| 258 |
std::uint32_t GsVram::AllocatePages(std::uint32_t pageCount) |
| 259 |
{ |
| 260 |
if (!_initialized) { |
| 261 |
_failedAllocationCount++; |
| 262 |
return InvalidPage; |
| 263 |
} |
| 264 |
|
| 265 |
const std::uint32_t page = AllocateWithin(_cacheFirstPage, _cachePageCount, pageCount); |
| 266 |
if (page == InvalidPage) { |
| 267 |
_failedAllocationCount++; |
| 268 |
return InvalidPage; |
| 269 |
} |
| 270 |
|
| 271 |
_usedPageCount += pageCount; |
| 272 |
if (_usedPageCount > _peakUsedPageCount) { |
| 273 |
_peakUsedPageCount = _usedPageCount; |
| 274 |
} |
| 275 |
return page; |
| 276 |
} |
| 277 |
|
| 278 |
void GsVram::FreePages(std::uint32_t firstPage, std::uint32_t pageCount) |
| 279 |
{ |
| 280 |
if (firstPage == InvalidPage || pageCount == 0) { |
| 281 |
return; |
| 282 |
} |
| 283 |
|
| 284 |
MarkRange(firstPage, pageCount, false); |
| 285 |
_usedPageCount -= (pageCount < _usedPageCount ? pageCount : _usedPageCount); |
| 286 |
} |
| 287 |
|
| 288 |
std::uint32_t GsVram::AllocateReservedPages(std::uint32_t pageCount) |
| 289 |
{ |
| 290 |
if (!_initialized || pageCount == 0 || pageCount > _reservePageCount) { |
| 291 |
_failedAllocationCount++; |
| 292 |
return InvalidPage; |
| 293 |
} |
| 294 |
|
| 295 |
const std::uint32_t page = AllocateWithin(_reserveFirstPage, _reservePageCount, pageCount); |
| 296 |
if (page == InvalidPage) { |
| 297 |
_failedAllocationCount++; |
| 298 |
} |
| 299 |
return page; |
| 300 |
} |
| 301 |
|
| 302 |
void GsVram::FreeReservedPages(std::uint32_t firstPage, std::uint32_t pageCount) |
| 303 |
{ |
| 304 |
if (firstPage == InvalidPage || pageCount == 0) { |
| 305 |
return; |
| 306 |
} |
| 307 |
// The reserve is outside the cache's usage accounting, so unlike FreePages() this only clears bits |
| 308 |
MarkRange(firstPage, pageCount, false); |
| 309 |
} |
| 310 |
|
| 311 |
std::uint32_t GsVram::AllocateClut() |
| 312 |
{ |
| 313 |
if (!_initialized) { |
| 314 |
_failedAllocationCount++; |
| 315 |
return InvalidBlock; |
| 316 |
} |
| 317 |
|
| 318 |
for (std::uint32_t slot = 0; slot < _clutSlotCount; slot++) { |
| 319 |
const std::uint32_t mask = std::uint32_t(1) << slot; |
| 320 |
if ((_clutBitmap & mask) == 0) { |
| 321 |
_clutBitmap |= mask; |
| 322 |
return _clutFirstBlock + slot * ClutSlotBlocks; |
| 323 |
} |
| 324 |
} |
| 325 |
|
| 326 |
_failedAllocationCount++; |
| 327 |
return InvalidBlock; |
| 328 |
} |
| 329 |
|
| 330 |
void GsVram::FreeClut(std::uint32_t block) |
| 331 |
{ |
| 332 |
if (block == InvalidBlock || block < _clutFirstBlock) { |
| 333 |
return; |
| 334 |
} |
| 335 |
|
| 336 |
const std::uint32_t slot = (block - _clutFirstBlock) / ClutSlotBlocks; |
| 337 |
if (slot < _clutSlotCount) { |
| 338 |
_clutBitmap &= ~(std::uint32_t(1) << slot); |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
std::uint32_t GsVram::GetCacheFirstPage() |
| 343 |
{ |
| 344 |
return _cacheFirstPage; |
| 345 |
} |
| 346 |
|
| 347 |
std::uint32_t GsVram::GetCachePageCount() |
| 348 |
{ |
| 349 |
return _cachePageCount; |
| 350 |
} |
| 351 |
|
| 352 |
std::uint32_t GsVram::GetUsedPageCount() |
| 353 |
{ |
| 354 |
return _usedPageCount; |
| 355 |
} |
| 356 |
|
| 357 |
std::uint32_t GsVram::GetPeakUsedPageCount() |
| 358 |
{ |
| 359 |
return _peakUsedPageCount; |
| 360 |
} |
| 361 |
|
| 362 |
std::uint32_t GsVram::GetLargestFreeRun() |
| 363 |
{ |
| 364 |
return (_initialized ? LargestFreeRunWithin(_cacheFirstPage, _cachePageCount) : 0); |
| 365 |
} |
| 366 |
|
| 367 |
std::uint32_t GsVram::GetFailedAllocationCount() |
| 368 |
{ |
| 369 |
return _failedAllocationCount; |
| 370 |
} |
| 371 |
} |