Debian Codegraph @

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

raw
1 #pragma once
2
3 #include "../RhiTypes.h"
4
5 #include <cstdint>
6
7 #include <Containers/StringView.h>
8
9 #include <rsx/gcm_sys.h>
10
11 using namespace Death::Containers;
12
13 namespace nCine::RHI::RSX
14 {
15 class RsxTexture;
16
17 /**
18 @brief Renderbuffer stub of the RSX backend (aliased as `RHI::Renderbuffer`)
19
20 The engine only ever asks for depth/stencil renderbuffers, and this backend gives every target the
21 device's one shared depth surface (see @ref RsxRenderTarget), so nothing is allocated here.
22 */
23 class RsxRenderbuffer
24 {
25 public:
26 RsxRenderbuffer() = default;
27 void Create(DepthStencilFormat format, std::int32_t width, std::int32_t height) {
28 static_cast<void>(format);
29 _width = width;
30 _height = height;
31 }
32 inline std::uint32_t GetGLHandle() const {
33 return 0;
34 }
35
36 private:
37 std::int32_t _width = 0;
38 std::int32_t _height = 0;
39 };
40
41 /**
42 @brief Framebuffer stub of the RSX backend (aliased as `RHI::Framebuffer`)
43
44 The RSX has no framebuffer object; @ref RsxRenderTarget records the attachment directly.
45 */
46 class RsxFramebuffer
47 {
48 public:
49 RsxFramebuffer() = default;
50 inline std::uint32_t GetGLHandle() const {
51 return 0;
52 }
53 bool Bind() const {
54 return true;
55 }
56 static bool Unbind() {
57 return true;
58 }
59 };
60
61 /**
62 @brief Off-screen render target of the RSX backend (aliased as `RHI::RenderTarget`)
63
64 Records the colour texture the following draws render into and keeps the `gcmSurface` that describes
65 it to the hardware. @ref BindDraw() records the target on the device, which programs that surface
66 with `rsxSetSurface()`.
67
68 This is markedly simpler than the sceGxm backend it otherwise mirrors, for a structural reason: the
69 PowerVR SGX is a tile-based deferred renderer, so a pass over a surface is a *scene* with a begin and
70 an end, and switching targets mid-frame means closing one and opening another. The RSX is an
71 immediate-mode renderer - a surface is just a set of addresses in a state register - so switching
72 targets is a state change like any other, with no lifecycle to manage and no risk of discarding what
73 an earlier pass drew.
74
75 **One colour attachment.** The RSX can bind four, but the published `MAX_COLOR_ATTACHMENTS` is 1,
76 which is all the render pipeline uses (every off-screen pass here is a single-target one).
77
78 **No depth storage of its own.** The engine is a 2D renderer that never needs a depth buffer's
79 contents to survive a pass, so every target shares the one display-sized depth surface the device
80 allocates rather than carrying its own. That surface is only ever scratch, and its pitch covers any
81 target the pipeline creates.
82 */
83 class RsxRenderTarget
84 {
85 public:
86 /** @brief Number of colour attachments a target can carry (see the class documentation) */
87 static constexpr std::uint32_t MaxColorAttachments = 1;
88
89 RsxRenderTarget();
90 ~RsxRenderTarget();
91
92 RsxRenderTarget(const RsxRenderTarget&) = delete;
93 RsxRenderTarget& operator=(const RsxRenderTarget&) = delete;
94
95 /** @brief Attaches a texture as the colour attachment with the given index */
96 void AttachColorTexture(RsxTexture& texture, std::uint32_t index);
97 /** @brief Detaches any texture from the colour attachment with the given index */
98 void DetachColorTexture(std::uint32_t index);
99
100 /** @brief Records a depth/stencil buffer (no storage is created, see the class documentation) */
101 void AttachDepthStencil(DepthStencilFormat format, std::int32_t width, std::int32_t height);
102 /** @brief Clears the recorded depth/stencil buffer */
103 void DetachDepthStencil(DepthStencilFormat format);
104
105 /** @brief Binds the render target as the current draw target on the device */
106 void BindDraw();
107 /** @brief Unbinds any render target from the device */
108 static void UnbindDraw();
109 /** @brief Sets the number of colour attachments enabled for drawing */
110 bool SetDrawBuffers(std::uint32_t numColorAttachments);
111
112 /** @brief Returns `true` if the target has a usable colour attachment 0 */
113 bool IsStatusComplete();
114
115 /** @brief Hints that the depth/stencil contents are no longer needed (no-op: they are never stored) */
116 void InvalidateDepthStencil(DepthStencilFormat format);
117
118 /** @brief Sets a debug label for the render target (ignored) */
119 void SetObjectLabel(StringView label);
120
121 /** @brief Returns the texture attached at the given colour attachment index, or `nullptr` */
122 inline RsxTexture* GetColorTexture(std::uint32_t index) const {
123 return (index < MaxColorAttachments ? _colorTextures[index] : nullptr);
124 }
125 /** @brief Returns the number of colour attachments enabled for drawing (see @ref SetDrawBuffers()) */
126 inline std::uint32_t GetNumDrawBuffers() const {
127 return _numDrawBuffers;
128 }
129
130 /**
131 @brief Fills in the surface description the device programs to render into this target
132
133 Built on demand from the attached texture's GPU-visible storage and rebuilt when the attachment
134 or its size changes. The depth half is left to the caller, which fills in the shared depth
135 surface. @returns `false` if the target has no usable attachment.
136 */
137 bool GetDrawSurface(gcmSurface& surface, std::int32_t& width, std::int32_t& height);
138
139 private:
140 RsxTexture* _colorTextures[MaxColorAttachments];
141 std::uint32_t _numDrawBuffers;
142
143 // Description of the current attachment, rebuilt when it changes
144 RsxTexture* _surfaceTexture; // the texture the description was built over (to detect a change)
145 void* _surfaceData; // its GPU address at that point (a reallocation invalidates it)
146 std::uint32_t _surfaceOffset;
147 std::uint32_t _surfacePitch;
148 std::int32_t _surfaceWidth;
149 std::int32_t _surfaceHeight;
150 bool _surfaceValid;
151 };
152 }