Debian Codegraph @

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

raw
1 #pragma once
2
3 #include <cstddef>
4 #include <cstdint>
5
6 namespace nCine::RHI::RSX
7 {
8 /**
9 @brief GPU-visible memory of the RSX backend
10
11 Every address the RSX reads or writes - vertex and index streams, texture texels, fragment program
12 microcode, the display surfaces and the depth buffer - has to be expressed to the GPU as an *offset*
13 into one of its two address windows rather than as a PPE pointer, so it cannot come from the C++
14 heap. This wraps allocation and the address-to-offset translation into one call and keeps both halves
15 together, which is what the command builders need: `rsxSetSurface()`, `rsxBindVertexArrayAttrib()`
16 and `rsxLoadTexture()` all take an offset plus a location, never a pointer.
17
18 **Two locations, and the choice is not free.** `Local` is the 256 MB of GDDR3 on the far side of the
19 GPU: the RSX reads it at full speed, but the PPE reaches it over the bus with *uncached, write-combined*
20 access, so writing texels into it is fast and reading them back is catastrophically slow (a read is
21 roughly two orders of magnitude worse than main memory). `Main` is ordinary XDR that the GPU reads
22 over FlexIO at a fraction of local bandwidth. The split this backend uses follows from that: render
23 targets, the display buffers and the depth buffer are `Local` because only the GPU touches them;
24 streamed geometry and uniform staging are `Main` because the CPU writes them every frame; textures are
25 `Local` because they are written once and sampled many times.
26
27 This is also why nothing here ever reads back through a mapping. `RsxTexture::GetTexImage()` and the
28 streaming-texture path both write forward only - a caller that wants texels back keeps its own copy.
29
30 **Fragment programs are a third case that looks like a fourth.** The RSX fetches fragment microcode
31 by offset exactly like a texture, so it is allocated here too rather than through a shader-specific
32 allocator - but it MUST be `Local`, because the fragment engine cannot fetch microcode from main
33 memory at all. @ref AllocFragmentProgram() only names that constraint; it is otherwise @ref Alloc().
34
35 Unlike the sceGxm backend's `GxmMemory`, there is no kernel block to keep: PSL1GHT's `rsxMemalign()`
36 suballocates the RSX heap that `rsxInit()` set up, so a block is just its base pointer.
37 */
38 namespace RsxVram
39 {
40 /** @brief Which of the GPU's two address windows a block lives in (it selects the `GCM_LOCATION_*` a command takes) */
41 enum class Location
42 {
43 /** @brief GDDR3 video memory: full GPU bandwidth, uncached and effectively unreadable for the PPE */
44 Local,
45 /** @brief Main XDR memory mapped into the GPU's IO window: cached for the PPE, slower for the GPU */
46 Main
47 };
48
49 /** @brief A GPU-visible allocation: its CPU-side base address and the offset the RSX addresses it by */
50 struct Block
51 {
52 /** @brief Base address of the block, or `nullptr` when the allocation failed */
53 void* Base = nullptr;
54 /** @brief Offset of the block within its location's address window, as the RSX commands take it */
55 std::uint32_t Offset = 0;
56 /** @brief Number of bytes reserved (the request rounded up to the requested alignment) */
57 std::uint32_t Size = 0;
58 /** @brief Address window @ref Offset is relative to */
59 Location Where = Location::Local;
60
61 inline bool IsValid() const {
62 return (Base != nullptr);
63 }
64
65 /** @brief Returns the `GCM_LOCATION_RSX` / `GCM_LOCATION_CELL` value the RSX commands expect */
66 std::uint8_t GetGcmLocation() const;
67 };
68
69 /**
70 @brief Brings up the main-memory heap
71
72 Called once by `RsxDevice::CreateSwapchain()` after `rsxInit()`, which is what maps the IO window
73 this suballocates. The local heap needs nothing here - `rsxInit()` sets it up itself - so only
74 main memory is passed in. Returns `false` if the heap could not be set up, which the device
75 treats as a failed session rather than trying to run without one.
76
77 @param mainHeapBase Base of the host region handed to `rsxInit()` as its IO address
78 @param mainHeapSize Size of that region in bytes
79 */
80 bool Initialize(void* mainHeapBase, std::uint32_t mainHeapSize);
81 /** @brief Releases the main-memory heap (the RSX heap belongs to the context and goes with it) */
82 void Shutdown();
83
84 /**
85 @brief Allocates GPU-visible memory
86
87 @param size Requested size in bytes
88 @param alignment Required alignment; 64 is the smallest the hardware accepts for anything it
89 fetches, and surfaces want more (see @ref AllocSurface())
90 @param where Which address window to allocate from - see the class documentation for what
91 that costs on each side
92 */
93 Block Alloc(std::uint32_t size, std::uint32_t alignment, Location where);
94
95 /**
96 @brief Allocates a render-target or display surface in local memory
97
98 Surfaces are aligned far harder than ordinary allocations: the display controller scans out of a
99 buffer the hardware requires to be 64-byte aligned with a pitch that is a multiple of 64, and a
100 surface the GPU renders into wants the same so a tile can be written without a read-modify-write
101 at its edge. The pitch is derived here rather than passed in, so no caller can get the pair wrong.
102
103 @param width Surface width in pixels
104 @param height Surface height in pixels
105 @param bytesPerPixel Size of one pixel (4 for the X8R8G8B8 surfaces this backend renders into)
106 @param pitchOut Receives the row stride in bytes the surface was created with
107 */
108 Block AllocSurface(std::uint32_t width, std::uint32_t height, std::uint32_t bytesPerPixel, std::uint32_t& pitchOut);
109
110 /**
111 @brief Allocates storage for fragment program microcode
112
113 Local memory is not a preference here: the fragment engine fetches microcode over the same path
114 it fetches textures and cannot reach main memory, so a fragment program placed in XDR renders
115 nothing at all (and does so silently).
116 */
117 Block AllocFragmentProgram(std::uint32_t size);
118
119 /** @brief Releases a block obtained from any of the allocators above and invalidates it */
120 void Free(Block& block);
121
122 /** @brief Returns the number of bytes currently reserved in local memory (for the memory report) */
123 std::uint32_t GetLocalAllocatedBytes();
124 /** @brief Returns the number of bytes currently reserved in main memory (for the memory report) */
125 std::uint32_t GetMainAllocatedBytes();
126 }
127 }