Debian Codegraph @

Sources/nCine/Graphics/RHI/RSX/RsxVram.cpp

raw
1 #if defined(WITH_RHI_RSX)
2
3 #include "RsxVram.h"
4 #include "../../../../Main.h"
5
6 #include <rsx/rsx.h>
7 #include <rsx/mm.h>
8 #include <rsx/gcm_sys.h>
9 #include <sys/heap.h>
10
11 namespace nCine::RHI::RSX
12 {
13 namespace RsxVram
14 {
15 namespace
16 {
17 /**
18 @brief Suballocator for the GPU's IO window into main memory
19
20 `rsxMemalign()` covers local memory, but PSL1GHT has no counterpart for the main-memory region
21 `rsxInit()` mapped - the samples simply carve it up by hand. The same `heapInit()` allocator
22 librsx uses for the local heap is used here so both sides behave identically (aligned
23 allocation, real frees, no fragmentation surprises that one and not the other would have).
24 */
25 heap_cntrl _mainHeap;
26 bool _mainHeapReady = false;
27
28 std::uint32_t _localAllocated = 0;
29 std::uint32_t _mainAllocated = 0;
30
31 /** @brief Smallest alignment the RSX accepts for anything it fetches by offset */
32 constexpr std::uint32_t MinAlignment = 64;
33 /**
34 @brief Row-stride granularity of a surface
35
36 Required by the display controller for a scan-out buffer, and what lets the GPU write a tile
37 at the edge of a render target without a read-modify-write.
38 */
39 constexpr std::uint32_t SurfacePitchAlignment = 64;
40 }
41
42 std::uint8_t Block::GetGcmLocation() const
43 {
44 return (Where == Location::Local ? GCM_LOCATION_RSX : GCM_LOCATION_CELL);
45 }
46
47 bool Initialize(void* mainHeapBase, std::uint32_t mainHeapSize)
48 {
49 if (mainHeapBase == nullptr || mainHeapSize == 0) {
50 LOGE("Cannot initialize the main memory heap: no IO region was reserved");
51 return false;
52 }
53
54 // The local heap belongs to the context and rsxInit() has already set it up; only the main-memory
55 // side needs an allocator of its own
56 heapInit(&_mainHeap, mainHeapBase, mainHeapSize);
57 _mainHeapReady = true;
58 _localAllocated = 0;
59 _mainAllocated = 0;
60
61 LOGI("RSX memory initialized: {} KB of main memory mapped into the GPU's IO window", mainHeapSize / 1024);
62 return true;
63 }
64
65 void Shutdown()
66 {
67 // Nothing to release: the main heap is a view over memory the device owns, and the local heap goes
68 // away with the context. Only the bookkeeping is reset, so a session that comes back up reports
69 // its own usage rather than the previous one's.
70 _mainHeapReady = false;
71 _localAllocated = 0;
72 _mainAllocated = 0;
73 }
74
75 Block Alloc(std::uint32_t size, std::uint32_t alignment, Location where)
76 {
77 Block block;
78 if (size == 0) {
79 return block;
80 }
81 if (alignment < MinAlignment) {
82 alignment = MinAlignment;
83 }
84
85 void* base;
86 if (where == Location::Local) {
87 base = rsxMemalign(alignment, size);
88 } else {
89 base = (_mainHeapReady ? heapAllocateAligned(&_mainHeap, size, alignment) : nullptr);
90 }
91 if (base == nullptr) {
92 LOGE("Cannot allocate {} bytes of {} memory", size, where == Location::Local ? "local" : "main");
93 return block;
94 }
95
96 // The offset is what every RSX command actually takes; a block that has a base but no offset is
97 // unusable, so the translation failing is treated as the allocation failing
98 std::uint32_t offset = 0;
99 if (rsxAddressToOffset(base, &offset) != 0) {
100 // The format library has no void* formatter, so the address is reported as an integer
101 LOGE("Cannot translate {} memory at 0x{:.8x} into an RSX offset",
102 where == Location::Local ? "local" : "main",
103 std::uint32_t(reinterpret_cast<std::uintptr_t>(base)));
104 if (where == Location::Local) {
105 rsxFree(base);
106 } else {
107 heapFree(&_mainHeap, base);
108 }
109 return block;
110 }
111
112 block.Base = base;
113 block.Offset = offset;
114 block.Size = size;
115 block.Where = where;
116
117 if (where == Location::Local) {
118 _localAllocated += size;
119 } else {
120 _mainAllocated += size;
121 }
122 return block;
123 }
124
125 Block AllocSurface(std::uint32_t width, std::uint32_t height, std::uint32_t bytesPerPixel, std::uint32_t& pitchOut)
126 {
127 const std::uint32_t pitch = (width * bytesPerPixel + SurfacePitchAlignment - 1) & ~(SurfacePitchAlignment - 1);
128 pitchOut = pitch;
129 return Alloc(pitch * height, SurfacePitchAlignment, Location::Local);
130 }
131
132 Block AllocFragmentProgram(std::uint32_t size)
133 {
134 // Local is mandatory here rather than preferred - see the header
135 return Alloc(size, MinAlignment, Location::Local);
136 }
137
138 void Free(Block& block)
139 {
140 if (block.Base == nullptr) {
141 return;
142 }
143
144 if (block.Where == Location::Local) {
145 rsxFree(block.Base);
146 _localAllocated -= block.Size;
147 } else if (_mainHeapReady) {
148 heapFree(&_mainHeap, block.Base);
149 _mainAllocated -= block.Size;
150 }
151
152 block.Base = nullptr;
153 block.Offset = 0;
154 block.Size = 0;
155 }
156
157 std::uint32_t GetLocalAllocatedBytes()
158 {
159 return _localAllocated;
160 }
161
162 std::uint32_t GetMainAllocatedBytes()
163 {
164 return _mainAllocated;
165 }
166 }
167 }
168
169 #endif