Debian Codegraph @

Sources/nCine/Graphics/RHI/GS/GsStagingBuffer.h

raw
1 #pragma once
2
3 #include <cstddef>
4 #include <cstdint>
5 #include <cstdlib>
6
7 namespace nCine::RHI::GS
8 {
9 /**
10 @brief Main-memory scratch a GIF transfer can be issued from
11
12 Every path that puts something in the Graphics Synthesizer's local memory - a texture store, a palette
13 bake, a CLUT, the lighting compositor's surface - fills a contiguous image here first and hands its
14 address to `draw_texture_transfer()`. That builds a DMA **chain** whose `REF` tags point back at this
15 memory rather than copying it, which puts two requirements on the buffer that an ordinary container
16 does not meet:
17
18 - **Qword alignment.** A DMA address ignores its low four bits. A buffer that starts 8 bytes into a
19 qword is therefore not transferred from where it starts - it is transferred from 8 bytes *before*
20 it, and the whole image lands shifted. `std::malloc()` promises only the alignment of the largest
21 fundamental type, which on this target is 8; whether a given allocation happens to be qword-aligned
22 as well is down to where it fell in the heap. `alignas` on a container does not help, because it
23 aligns the container OBJECT and not the block it points at.
24 - **A cache line of its own at each end.** The caller has to write this back out of the EE's
25 write-back data cache before the DMA reads it (@ref GsDevice::WritebackForDma()). Writing back a
26 range that shares its first or last 64-byte line with an unrelated object is harmless but
27 imprecise; owning whole lines keeps the operation to exactly this buffer.
28
29 Allocated with one over-sized `std::malloc()` and an aligned pointer inside it, rather than
30 `std::aligned_alloc()` or `memalign()`, so the buffer has no dependency on which of the two the
31 target's C library actually provides.
32
33 Only ever grows: the renderer is single-threaded and these are reused every frame, so shrinking would
34 trade a reallocation for memory the next frame asks for again.
35 */
36 class GsStagingBuffer
37 {
38 public:
39 /** @brief Alignment of the returned pointer - a whole EE data-cache line, which is also qword-aligned */
40 static constexpr std::size_t Alignment = 64;
41
42 GsStagingBuffer() = default;
43
44 ~GsStagingBuffer()
45 {
46 std::free(_block);
47 }
48
49 GsStagingBuffer(const GsStagingBuffer&) = delete;
50 GsStagingBuffer& operator=(const GsStagingBuffer&) = delete;
51
52 /** @brief Returns the base of the buffer, aligned to @ref Alignment (`nullptr` before a reserve) */
53 inline std::uint8_t* Data() const {
54 return _data;
55 }
56 /** @brief Returns the number of usable bytes */
57 inline std::size_t GetSize() const {
58 return _size;
59 }
60
61 /**
62 @brief Makes the buffer at least @p bytes long, without preserving what was in it
63
64 Returns `nullptr` if the allocation failed, in which case the previous buffer is kept - a caller
65 that checks the result therefore never writes into a buffer shorter than it asked for.
66 */
67 std::uint8_t* Reserve(std::size_t bytes)
68 {
69 if (bytes <= _size) {
70 return _data;
71 }
72
73 // Over-allocate by the alignment so there is always an aligned address inside the block
74 void* block = std::malloc(bytes + Alignment);
75 if (block == nullptr) {
76 return nullptr;
77 }
78
79 std::free(_block);
80 _block = block;
81 const std::uintptr_t address = reinterpret_cast<std::uintptr_t>(block);
82 _data = reinterpret_cast<std::uint8_t*>((address + (Alignment - 1)) & ~std::uintptr_t(Alignment - 1));
83 _size = bytes;
84 return _data;
85 }
86
87 private:
88 void* _block = nullptr;
89 std::uint8_t* _data = nullptr;
90 std::size_t _size = 0;
91 };
92 }