| 1 |
#include "GxmMemory.h" |
| 2 |
|
| 3 |
#include "../../../../Main.h" |
| 4 |
|
| 5 |
#include <cstring> |
| 6 |
|
| 7 |
namespace nCine::RHI::GXM |
| 8 |
{ |
| 9 |
namespace |
| 10 |
{ |
| 11 |
// LPDDR blocks are handed out in 4 KB pages and CDRAM ones in 256 KB pages; the kernel rejects any |
| 12 |
// size that is not a multiple of the granularity of the requested type |
| 13 |
constexpr std::uint32_t LpddrPageSize = 4 * 1024; |
| 14 |
constexpr std::uint32_t CdramPageSize = 256 * 1024; |
| 15 |
|
| 16 |
std::uint32_t _allocatedBytes = 0; |
| 17 |
|
| 18 |
// Render-target texels whose address is reserved for one geometry (see GxmMemory::AcquireSurface()). A |
| 19 |
// session uses a handful of sizes - the game's resolution, the levels of the blur chain, the display - |
| 20 |
// so a small fixed table covers every one of them, and the CDRAM a retired entry holds on to is the |
| 21 |
// price of the address staying meaningful. |
| 22 |
constexpr std::uint32_t MaxRetainedSurfaces = 16; |
| 23 |
|
| 24 |
struct RetainedSurface |
| 25 |
{ |
| 26 |
std::uint32_t Stride = 0; |
| 27 |
std::uint32_t Height = 0; |
| 28 |
GxmMemory::Block Memory; |
| 29 |
bool InUse = false; |
| 30 |
}; |
| 31 |
|
| 32 |
RetainedSurface _retainedSurfaces[MaxRetainedSurfaces]; |
| 33 |
std::uint32_t _retainedSurfaceCount = 0; |
| 34 |
|
| 35 |
inline std::uint32_t AlignUp(std::uint32_t value, std::uint32_t alignment) |
| 36 |
{ |
| 37 |
return (value + alignment - 1) & ~(alignment - 1); |
| 38 |
} |
| 39 |
|
| 40 |
GxmMemory::Block AllocInternal(const char* name, std::uint32_t size, SceKernelMemBlockType type, |
| 41 |
std::uint32_t pageSize, SceGxmMemoryAttribFlags attribs) |
| 42 |
{ |
| 43 |
GxmMemory::Block block; |
| 44 |
if (size == 0) { |
| 45 |
return block; |
| 46 |
} |
| 47 |
|
| 48 |
const std::uint32_t alignedSize = AlignUp(size, pageSize); |
| 49 |
SceUID uid = sceKernelAllocMemBlock(name, type, alignedSize, nullptr); |
| 50 |
if (uid < 0) { |
| 51 |
LOGE("sceKernelAllocMemBlock(\"{}\", {} bytes) failed with 0x{:.8x}", name, alignedSize, std::uint32_t(uid)); |
| 52 |
return block; |
| 53 |
} |
| 54 |
|
| 55 |
void* base = nullptr; |
| 56 |
std::int32_t result = sceKernelGetMemBlockBase(uid, &base); |
| 57 |
if (result < 0 || base == nullptr) { |
| 58 |
LOGE("sceKernelGetMemBlockBase(\"{}\") failed with 0x{:.8x}", name, std::uint32_t(result)); |
| 59 |
sceKernelFreeMemBlock(uid); |
| 60 |
return block; |
| 61 |
} |
| 62 |
|
| 63 |
result = sceGxmMapMemory(base, alignedSize, attribs); |
| 64 |
if (result < 0) { |
| 65 |
LOGE("sceGxmMapMemory(\"{}\", {} bytes) failed with 0x{:.8x}", name, alignedSize, std::uint32_t(result)); |
| 66 |
sceKernelFreeMemBlock(uid); |
| 67 |
return block; |
| 68 |
} |
| 69 |
|
| 70 |
block.Uid = uid; |
| 71 |
block.Base = base; |
| 72 |
block.Size = alignedSize; |
| 73 |
_allocatedBytes += alignedSize; |
| 74 |
return block; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
GxmMemory::Block GxmMemory::Alloc(const char* name, std::uint32_t size, SceGxmMemoryAttribFlags attribs) |
| 79 |
{ |
| 80 |
return AllocInternal(name, size, SCE_KERNEL_MEMBLOCK_TYPE_USER_RW_UNCACHE, LpddrPageSize, attribs); |
| 81 |
} |
| 82 |
|
| 83 |
GxmMemory::Block GxmMemory::AllocCdram(const char* name, std::uint32_t size, SceGxmMemoryAttribFlags attribs) |
| 84 |
{ |
| 85 |
Block block = AllocInternal(name, size, SCE_KERNEL_MEMBLOCK_TYPE_USER_CDRAM_RW, CdramPageSize, attribs); |
| 86 |
if (!block.IsValid()) { |
| 87 |
// CDRAM is only 128 MB and is not the only thing competing for it (the video decoder takes a share |
| 88 |
// on the real console), so a large off-screen target can legitimately fail to fit. LPDDR is slower |
| 89 |
// to render into but works, which is a much better outcome than refusing to create the target |
| 90 |
LOGW("CDRAM allocation of {} bytes for \"{}\" failed, falling back to main memory", size, name); |
| 91 |
block = Alloc(name, size, attribs); |
| 92 |
} |
| 93 |
return block; |
| 94 |
} |
| 95 |
|
| 96 |
GxmMemory::Block GxmMemory::AcquireSurface(const char* name, std::uint32_t stride, std::uint32_t height) |
| 97 |
{ |
| 98 |
if (stride == 0 || height == 0) { |
| 99 |
return Block(); |
| 100 |
} |
| 101 |
|
| 102 |
for (RetainedSurface& surface : _retainedSurfaces) { |
| 103 |
if (!surface.InUse && surface.Stride == stride && surface.Height == height) { |
| 104 |
surface.InUse = true; |
| 105 |
return surface.Memory; |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
Block block = AllocCdram(name, stride * height, SCE_GXM_MEMORY_ATTRIB_RW); |
| 110 |
if (block.IsValid()) { |
| 111 |
if (_retainedSurfaceCount < MaxRetainedSurfaces) { |
| 112 |
RetainedSurface& surface = _retainedSurfaces[_retainedSurfaceCount++]; |
| 113 |
surface.Stride = stride; |
| 114 |
surface.Height = height; |
| 115 |
surface.Memory = block; |
| 116 |
surface.InUse = true; |
| 117 |
} else { |
| 118 |
// Out of slots to keep addresses stable in, so this one goes back to the allocator when it is |
| 119 |
// released - it may then be handed to a target of another size, with the consequences above |
| 120 |
LOGW("More than {} distinct render-target sizes were used; {}x{} is not address-stable", |
| 121 |
MaxRetainedSurfaces, stride / 4u, height); |
| 122 |
} |
| 123 |
} |
| 124 |
return block; |
| 125 |
} |
| 126 |
|
| 127 |
void GxmMemory::ReleaseSurface(Block& block) |
| 128 |
{ |
| 129 |
if (!block.IsValid()) { |
| 130 |
return; |
| 131 |
} |
| 132 |
|
| 133 |
for (RetainedSurface& surface : _retainedSurfaces) { |
| 134 |
if (surface.Memory.Uid == block.Uid) { |
| 135 |
// Kept mapped and kept out of the allocator, so the next target of this geometry lands on the |
| 136 |
// very same address; the caller's handle goes away either way |
| 137 |
surface.InUse = false; |
| 138 |
block = Block(); |
| 139 |
return; |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
Free(block); |
| 144 |
} |
| 145 |
|
| 146 |
GxmMemory::Block GxmMemory::AllocVertexUsse(const char* name, std::uint32_t size) |
| 147 |
{ |
| 148 |
Block block; |
| 149 |
const std::uint32_t alignedSize = AlignUp(size, LpddrPageSize); |
| 150 |
SceUID uid = sceKernelAllocMemBlock(name, SCE_KERNEL_MEMBLOCK_TYPE_USER_RW_UNCACHE, alignedSize, nullptr); |
| 151 |
if (uid < 0) { |
| 152 |
LOGE("sceKernelAllocMemBlock(\"{}\", {} bytes) failed with 0x{:.8x}", name, alignedSize, std::uint32_t(uid)); |
| 153 |
return block; |
| 154 |
} |
| 155 |
|
| 156 |
void* base = nullptr; |
| 157 |
if (sceKernelGetMemBlockBase(uid, &base) < 0 || base == nullptr) { |
| 158 |
sceKernelFreeMemBlock(uid); |
| 159 |
return block; |
| 160 |
} |
| 161 |
|
| 162 |
unsigned int usseOffset = 0; |
| 163 |
std::int32_t result = sceGxmMapVertexUsseMemory(base, alignedSize, &usseOffset); |
| 164 |
if (result < 0) { |
| 165 |
LOGE("sceGxmMapVertexUsseMemory(\"{}\") failed with 0x{:.8x}", name, std::uint32_t(result)); |
| 166 |
sceKernelFreeMemBlock(uid); |
| 167 |
return block; |
| 168 |
} |
| 169 |
|
| 170 |
block.Uid = uid; |
| 171 |
block.Base = base; |
| 172 |
block.Size = alignedSize; |
| 173 |
block.UsseOffset = usseOffset; |
| 174 |
block.MappedAs = Kind::VertexUsse; |
| 175 |
_allocatedBytes += alignedSize; |
| 176 |
return block; |
| 177 |
} |
| 178 |
|
| 179 |
GxmMemory::Block GxmMemory::AllocFragmentUsse(const char* name, std::uint32_t size) |
| 180 |
{ |
| 181 |
Block block; |
| 182 |
const std::uint32_t alignedSize = AlignUp(size, LpddrPageSize); |
| 183 |
SceUID uid = sceKernelAllocMemBlock(name, SCE_KERNEL_MEMBLOCK_TYPE_USER_RW_UNCACHE, alignedSize, nullptr); |
| 184 |
if (uid < 0) { |
| 185 |
LOGE("sceKernelAllocMemBlock(\"{}\", {} bytes) failed with 0x{:.8x}", name, alignedSize, std::uint32_t(uid)); |
| 186 |
return block; |
| 187 |
} |
| 188 |
|
| 189 |
void* base = nullptr; |
| 190 |
if (sceKernelGetMemBlockBase(uid, &base) < 0 || base == nullptr) { |
| 191 |
sceKernelFreeMemBlock(uid); |
| 192 |
return block; |
| 193 |
} |
| 194 |
|
| 195 |
unsigned int usseOffset = 0; |
| 196 |
std::int32_t result = sceGxmMapFragmentUsseMemory(base, alignedSize, &usseOffset); |
| 197 |
if (result < 0) { |
| 198 |
LOGE("sceGxmMapFragmentUsseMemory(\"{}\") failed with 0x{:.8x}", name, std::uint32_t(result)); |
| 199 |
sceKernelFreeMemBlock(uid); |
| 200 |
return block; |
| 201 |
} |
| 202 |
|
| 203 |
block.Uid = uid; |
| 204 |
block.Base = base; |
| 205 |
block.Size = alignedSize; |
| 206 |
block.UsseOffset = usseOffset; |
| 207 |
block.MappedAs = Kind::FragmentUsse; |
| 208 |
_allocatedBytes += alignedSize; |
| 209 |
return block; |
| 210 |
} |
| 211 |
|
| 212 |
void GxmMemory::Free(Block& block) |
| 213 |
{ |
| 214 |
if (block.Uid == InvalidUid) { |
| 215 |
return; |
| 216 |
} |
| 217 |
|
| 218 |
if (block.Base != nullptr) { |
| 219 |
// The unmap call has to match the one the block was mapped with |
| 220 |
switch (block.MappedAs) { |
| 221 |
case Kind::VertexUsse: sceGxmUnmapVertexUsseMemory(block.Base); break; |
| 222 |
case Kind::FragmentUsse: sceGxmUnmapFragmentUsseMemory(block.Base); break; |
| 223 |
default: sceGxmUnmapMemory(block.Base); break; |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
sceKernelFreeMemBlock(block.Uid); |
| 228 |
if (_allocatedBytes >= block.Size) { |
| 229 |
_allocatedBytes -= block.Size; |
| 230 |
} |
| 231 |
|
| 232 |
block.Uid = InvalidUid; |
| 233 |
block.Base = nullptr; |
| 234 |
block.Size = 0; |
| 235 |
block.UsseOffset = 0; |
| 236 |
block.MappedAs = Kind::Mapped; |
| 237 |
} |
| 238 |
|
| 239 |
std::uint32_t GxmMemory::GetAllocatedBytes() |
| 240 |
{ |
| 241 |
return _allocatedBytes; |
| 242 |
} |
| 243 |
} |