| 1 |
#pragma once |
| 2 |
|
| 3 |
#include <cstddef> |
| 4 |
#include <cstdint> |
| 5 |
|
| 6 |
#include <psp2/kernel/sysmem.h> |
| 7 |
#include <psp2/gxm.h> |
| 8 |
|
| 9 |
namespace nCine::RHI::GXM |
| 10 |
{ |
| 11 |
/** |
| 12 |
@brief GPU-visible memory of the sceGxm backend |
| 13 |
|
| 14 |
Every address the GPU reads or writes - vertex and index streams, texture texels, uniform buffers, |
| 15 |
the driver's own ring buffers and the display surfaces - has to live in a kernel memory block that |
| 16 |
was handed to `sceGxmMapMemory()`, so it cannot come from the C++ heap. This wraps that two-step |
| 17 |
allocate-and-map dance into one call, keeps the `SceUID` needed to release the block, and rounds the |
| 18 |
request up to the page size the block type requires (the kernel rejects a size that is not a |
| 19 |
multiple of it). |
| 20 |
|
| 21 |
Every block is allocated **uncached**. The Vita exposes no user-mode data-cache write-back call |
| 22 |
(`ksceKernelCpuDcacheWritebackRange` is kernel-only), so a cached mapping could not be made visible |
| 23 |
to the GPU reliably - the classic "nothing renders" failure the GU backend flushes around on the PSP |
| 24 |
has no user-space remedy here. The cost is only on the CPU side of an upload; the GPU has its own |
| 25 |
cache in front of this memory either way. |
| 26 |
|
| 27 |
Two address spaces sit alongside the mappable one and get their own entry points because they are |
| 28 |
mapped through different calls and hand back a USSE offset rather than only a pointer: the vertex |
| 29 |
and fragment USSE code windows the shader patcher uploads compiled programs into. |
| 30 |
*/ |
| 31 |
namespace GxmMemory |
| 32 |
{ |
| 33 |
/** @brief Value @ref Block::Uid carries when an allocation failed (the SDK has no `SCE_UID_INVALID_UID` macro) */ |
| 34 |
constexpr SceUID InvalidUid = SceUID(-1); |
| 35 |
|
| 36 |
/** @brief Which address space a block was mapped into (it decides the unmap call @ref Free() has to issue) */ |
| 37 |
enum class Kind |
| 38 |
{ |
| 39 |
Mapped, |
| 40 |
VertexUsse, |
| 41 |
FragmentUsse |
| 42 |
}; |
| 43 |
|
| 44 |
/** @brief A GPU-visible allocation: the kernel block that owns it and its CPU-side base address */ |
| 45 |
struct Block |
| 46 |
{ |
| 47 |
/** @brief Kernel memory-block identifier, or @ref InvalidUid when the allocation failed */ |
| 48 |
SceUID Uid = InvalidUid; |
| 49 |
/** @brief Address space the block is mapped into */ |
| 50 |
Kind MappedAs = Kind::Mapped; |
| 51 |
/** @brief Base address of the block, or `nullptr` when the allocation failed */ |
| 52 |
void* Base = nullptr; |
| 53 |
/** @brief Number of bytes actually reserved (the request rounded up to the block's page size) */ |
| 54 |
std::uint32_t Size = 0; |
| 55 |
/** @brief USSE offset of the block, for the two USSE allocators (0 otherwise) */ |
| 56 |
std::uint32_t UsseOffset = 0; |
| 57 |
|
| 58 |
inline bool IsValid() const { |
| 59 |
return (Base != nullptr); |
| 60 |
} |
| 61 |
}; |
| 62 |
|
| 63 |
/** |
| 64 |
@brief Allocates GPU-visible memory from main LPDDR and maps it for the GPU |
| 65 |
|
| 66 |
@param name Debug name of the kernel block (shown by the memory-usage tools) |
| 67 |
@param size Requested size in bytes, rounded up to the block type's 4 KB page size |
| 68 |
@param attribs What the GPU may do with the memory (read, write or both) |
| 69 |
*/ |
| 70 |
Block Alloc(const char* name, std::uint32_t size, SceGxmMemoryAttribFlags attribs); |
| 71 |
|
| 72 |
/** |
| 73 |
@brief Allocates GPU-visible memory from CDRAM and maps it for the GPU |
| 74 |
|
| 75 |
The 128 MB of CDRAM is the memory the display controller scans out of and the fastest memory the |
| 76 |
GPU can render into, so the display surfaces and the off-screen render targets are placed here. |
| 77 |
Falls back to LPDDR (@ref Alloc()) when CDRAM is exhausted, which keeps a large level loading |
| 78 |
rather than failing outright. Its allocation granularity is 256 KB, so small requests are better |
| 79 |
served by @ref Alloc(). |
| 80 |
*/ |
| 81 |
Block AllocCdram(const char* name, std::uint32_t size, SceGxmMemoryAttribFlags attribs); |
| 82 |
|
| 83 |
/** |
| 84 |
@brief Acquires CDRAM for a render target's texels, at an address reserved for that exact geometry |
| 85 |
|
| 86 |
A colour surface is identified to the driver by its base address, and a target of a given size always |
| 87 |
comes back here at the same address: blocks handed out through this pair are retired for reuse rather |
| 88 |
than released, and only ever reused for the identical stride and height. That matters because the |
| 89 |
pipeline destroys and rebuilds its whole viewport chain whenever the render passes change (entering a |
| 90 |
level, leaving it, splitting the screen), so a plain allocator hands the address a retired target used |
| 91 |
to occupy to the next target of a *different* size - and a driver that has cached anything about that |
| 92 |
address, an emulator especially, then has two conflicting descriptions of one surface and drops one of |
| 93 |
them. Trading a bounded amount of retained CDRAM for stable addresses avoids the whole class of |
| 94 |
problem; past @ref MaxRetainedSurfaces distinct geometries it degrades to @ref AllocCdram(). |
| 95 |
*/ |
| 96 |
Block AcquireSurface(const char* name, std::uint32_t stride, std::uint32_t height); |
| 97 |
/** @brief Retires a block from @ref AcquireSurface(), keeping its address reserved for the same geometry */ |
| 98 |
void ReleaseSurface(Block& block); |
| 99 |
|
| 100 |
/** @brief Allocates memory in the vertex USSE window (for the shader patcher's compiled vertex programs) */ |
| 101 |
Block AllocVertexUsse(const char* name, std::uint32_t size); |
| 102 |
/** @brief Allocates memory in the fragment USSE window (for the shader patcher's compiled fragment programs) */ |
| 103 |
Block AllocFragmentUsse(const char* name, std::uint32_t size); |
| 104 |
|
| 105 |
/** @brief Unmaps and releases a block obtained from any of the allocators above */ |
| 106 |
void Free(Block& block); |
| 107 |
|
| 108 |
/** @brief Returns the total number of bytes currently reserved through this allocator (for the memory report) */ |
| 109 |
std::uint32_t GetAllocatedBytes(); |
| 110 |
} |
| 111 |
} |