Debian Codegraph @

Sources/nCine/Graphics/RHI/GXM/GxmRenderTarget.h

raw
1 #pragma once
2
3 #include "../RhiTypes.h"
4
5 #include <cstdint>
6
7 #include <Containers/StringView.h>
8
9 #include <psp2/gxm.h>
10
11 using namespace Death::Containers;
12
13 namespace nCine::RHI::GXM
14 {
15 class GxmTexture;
16
17 /**
18 @brief Renderbuffer stub of the sceGxm backend (aliased as `RHI::Renderbuffer`)
19
20 Carries no depth/stencil storage of its own (the renderer is 2D, and the backend shares one
21 depth/stencil surface across every scene - see @ref GxmRenderTarget); the class only records the
22 format and size to satisfy the contract alias.
23 */
24 class GxmRenderbuffer
25 {
26 public:
27 GxmRenderbuffer() = default;
28 void Create(DepthStencilFormat format, std::int32_t width, std::int32_t height) {
29 _format = format;
30 _width = width;
31 _height = height;
32 }
33 inline std::uint32_t GetGLHandle() const {
34 return 0;
35 }
36
37 private:
38 DepthStencilFormat _format = DepthStencilFormat::None;
39 std::int32_t _width = 0;
40 std::int32_t _height = 0;
41 };
42
43 /**
44 @brief Framebuffer stub of the sceGxm backend (aliased as `RHI::Framebuffer`)
45
46 Provided only for the contract alias (the default-framebuffer rebinding some window backends use).
47 Off-screen rendering is routed through @ref GxmRenderTarget instead.
48 */
49 class GxmFramebuffer
50 {
51 public:
52 GxmFramebuffer() = default;
53 inline std::uint32_t GetGLHandle() const {
54 return 0;
55 }
56 bool Bind() const {
57 return true;
58 }
59 static bool Unbind() {
60 return true;
61 }
62 };
63
64 /**
65 @brief Off-screen render target of the sceGxm backend (aliased as `RHI::RenderTarget`)
66
67 Records the colour texture the following scene renders into and owns the `SceGxmRenderTarget` that
68 describes how that surface is tiled. @ref BindDraw() records the target on the device, which closes
69 the scene in progress and opens a new one over this surface (sceGxm has no "switch the framebuffer"
70 operation - a scene *is* a pass over one surface).
71
72 **One colour attachment.** sceGxm binds a single colour surface per scene, so unlike the desktop
73 backends there is no multi-attachment case to bound: the published `MAX_COLOR_ATTACHMENTS` is 1,
74 which is also all the render pipeline uses (every off-screen pass here is a single-target one).
75
76 **No depth storage.** The engine is a 2D renderer that never needs a depth buffer's contents to
77 survive a pass, and a tile-based architecture gives depth testing *within* a scene from the on-chip
78 tile buffer for free. Every scene therefore shares the one panel-sized depth/stencil surface the
79 device allocates, rather than each target carrying its own - the surface is only ever a scratch area,
80 and its stride covers any target the pipeline creates.
81 */
82 class GxmRenderTarget
83 {
84 public:
85 /** @brief Number of colour attachments a target can carry (see the class documentation) */
86 static constexpr std::uint32_t MaxColorAttachments = 1;
87
88 GxmRenderTarget();
89 ~GxmRenderTarget();
90
91 GxmRenderTarget(const GxmRenderTarget&) = delete;
92 GxmRenderTarget& operator=(const GxmRenderTarget&) = delete;
93
94 /** @brief Attaches a texture as the colour attachment with the given index */
95 void AttachColorTexture(GxmTexture& texture, std::uint32_t index);
96 /** @brief Detaches any texture from the colour attachment with the given index */
97 void DetachColorTexture(std::uint32_t index);
98
99 /** @brief Records a depth/stencil buffer (no storage is created, see the class documentation) */
100 void AttachDepthStencil(DepthStencilFormat format, std::int32_t width, std::int32_t height);
101 /** @brief Clears the recorded depth/stencil buffer */
102 void DetachDepthStencil(DepthStencilFormat format);
103
104 /** @brief Binds the render target as the current draw target on the device */
105 void BindDraw();
106 /** @brief Unbinds any render target from the device */
107 static void UnbindDraw();
108 /** @brief Sets the number of colour attachments enabled for drawing */
109 bool SetDrawBuffers(std::uint32_t numColorAttachments);
110
111 /** @brief Returns `true` if the target has a usable colour attachment 0 */
112 bool IsStatusComplete();
113
114 /** @brief Hints that the depth/stencil contents are no longer needed (no-op: they are never stored) */
115 void InvalidateDepthStencil(DepthStencilFormat format);
116
117 /** @brief Sets a debug label for the render target (ignored) */
118 void SetObjectLabel(StringView label);
119
120 /** @brief Returns the texture attached at the given colour attachment index, or `nullptr` */
121 inline GxmTexture* GetColorTexture(std::uint32_t index) const {
122 return (index < MaxColorAttachments ? _colorTextures[index] : nullptr);
123 }
124 /** @brief Returns the number of colour attachments enabled for drawing (see @ref SetDrawBuffers()) */
125 inline std::uint32_t GetNumDrawBuffers() const {
126 return _numDrawBuffers;
127 }
128
129 /**
130 @brief Returns the sceGxm render target, colour surface and sync object a scene over this target begins with
131
132 All three are built on demand from the attached texture's GPU-visible surface, and rebuilt when the
133 attachment or its size changes. @returns `false` if the target has no usable attachment.
134
135 The sync object is what serializes operations on these texels against each other. sceGxm pipelines
136 scenes, so a scene that renders into a surface and a later one that samples it are not otherwise
137 ordered - which is the whole reason `sceGxmBeginScene()` takes one, and passing `nullptr` (as this
138 backend did) leaves a render-to-texture hand-off with nothing guaranteeing the write finished before
139 the read. The display buffers have always had theirs for the same reason, against the scan-out.
140 */
141 bool GetSceneTarget(SceGxmRenderTarget*& renderTarget, SceGxmColorSurface*& colorSurface,
142 SceGxmSyncObject*& syncObject, std::int32_t& width, std::int32_t& height);
143
144 private:
145 GxmTexture* _colorTextures[MaxColorAttachments];
146 std::uint32_t _numDrawBuffers;
147
148 // The sceGxm objects describing the current attachment, rebuilt when it changes
149 SceGxmRenderTarget* _gxmRenderTarget;
150 SceGxmColorSurface _colorSurface;
151 SceGxmSyncObject* _syncObject; // serializes writing these texels against sampling them
152 GxmTexture* _surfaceTexture; // the texture _colorSurface was built over (to detect a change)
153 void* _surfaceData; // its GPU address at that point (a reallocation invalidates the surface)
154 std::int32_t _surfaceWidth;
155 std::int32_t _surfaceHeight;
156 bool _surfaceValid;
157
158 /** @brief Releases the sceGxm render target and invalidates the colour surface */
159 void ReleaseSceneTarget();
160 };
161 }