Debian Codegraph @

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

raw
1 #pragma once
2
3 #include "../RhiTypes.h"
4
5 #include <cstdint>
6
7 #include <Containers/StringView.h>
8
9 using namespace Death::Containers;
10
11 namespace nCine::RHI::GS
12 {
13 class GsTexture;
14
15 /**
16 @brief Renderbuffer stub of the GS backend (aliased as `RHI::Renderbuffer`)
17
18 The GS backend renders 2D only and runs without a Z buffer at all (the depth test is set to ALWAYS
19 with writes masked, so the pointer is never touched and its pages stay free), so depth/stencil
20 renderbuffers carry no storage; the class just records the format and size to satisfy the alias.
21 */
22 class GsRenderbuffer
23 {
24 public:
25 GsRenderbuffer() = default;
26 void Create(DepthStencilFormat format, std::int32_t width, std::int32_t height) {
27 _format = format;
28 _width = width;
29 _height = height;
30 }
31 inline std::uint32_t GetGLHandle() const {
32 return 0;
33 }
34
35 private:
36 DepthStencilFormat _format = DepthStencilFormat::None;
37 std::int32_t _width = 0;
38 std::int32_t _height = 0;
39 };
40
41 /**
42 @brief Framebuffer stub of the GS backend (aliased as `RHI::Framebuffer`)
43
44 Provided only for the contract alias (the default-framebuffer rebinding some window backends use).
45 The GS backend routes off-screen rendering through @ref GsRenderTarget instead.
46 */
47 class GsFramebuffer
48 {
49 public:
50 GsFramebuffer() = default;
51 inline std::uint32_t GetGLHandle() const {
52 return 0;
53 }
54 bool Bind() const {
55 return true;
56 }
57 static bool Unbind() {
58 return true;
59 }
60 };
61
62 /**
63 @brief Off-screen render target of the GS backend (aliased as `RHI::RenderTarget`)
64
65 Holds the color textures the rasterizer writes into (addressed by attachment index) and an optional
66 depth/stencil (ignored for 2D). @ref BindDraw() records the target on the device, which points
67 `FRAME.FBP` at the attachment's pages for the following clears and draws - on the GS switching render
68 target really is nothing more than that.
69
70 The pages themselves are allocated by the attached texture (see `GsTexture::SetRenderTarget()`) out of
71 the layout's render-target reserve rather than the streaming cache, because a render target has no
72 host copy to be rebuilt from and so must never be evicted.
73 */
74 class GsRenderTarget
75 {
76 public:
77 static constexpr std::uint32_t MaxColorAttachments = 8;
78
79 GsRenderTarget();
80 ~GsRenderTarget();
81
82 GsRenderTarget(const GsRenderTarget&) = delete;
83 GsRenderTarget& operator=(const GsRenderTarget&) = delete;
84
85 /** @brief Attaches a texture as the color attachment with the given index */
86 void AttachColorTexture(GsTexture& texture, std::uint32_t index);
87 /** @brief Detaches any texture from the color attachment with the given index */
88 void DetachColorTexture(std::uint32_t index);
89
90 /** @brief Records a depth/stencil buffer (no storage - the GS backend is 2D) */
91 void AttachDepthStencil(DepthStencilFormat format, std::int32_t width, std::int32_t height);
92 /** @brief Clears the recorded depth/stencil buffer */
93 void DetachDepthStencil(DepthStencilFormat format);
94
95 /** @brief Binds the render target as the current draw target on the device */
96 void BindDraw();
97 /** @brief Unbinds any render target from the device */
98 static void UnbindDraw();
99 /** @brief Sets the number of color attachments enabled for drawing */
100 bool SetDrawBuffers(std::uint32_t numColorAttachments);
101
102 /** @brief Returns `true` if the target has a usable color attachment 0 */
103 bool IsStatusComplete();
104
105 /** @brief Hints that the depth/stencil contents are no longer needed (no-op) */
106 void InvalidateDepthStencil(DepthStencilFormat format);
107
108 /** @brief Sets a debug label for the render target (ignored) */
109 void SetObjectLabel(StringView label);
110
111 /** @brief Returns the texture attached at the given color attachment index, or `nullptr` */
112 inline GsTexture* GetColorTexture(std::uint32_t index) const {
113 return (index < MaxColorAttachments ? _colorTextures[index] : nullptr);
114 }
115
116 private:
117 GsTexture* _colorTextures[MaxColorAttachments];
118 std::uint32_t _numDrawBuffers;
119 };
120 }