Debian Codegraph @

Sources/nCine/Graphics/RHI/D3D11/D3D11RenderTarget.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 // Direct3D 11 interface referenced only as an opaque pointer here (definition pulled in by the .cpp)
12 struct ID3D11RenderTargetView;
13
14 namespace nCine::RHI::D3D11
15 {
16 class D3D11Texture;
17
18 /**
19 @brief Renderbuffer stub of the Direct3D 11 backend (aliased as `RHI::Renderbuffer`)
20
21 Carries no depth/stencil storage (the renderer is 2D); the class just records the format and
22 size to satisfy the contract alias. An `ID3D11Texture2D` depth/stencil could be added here when needed.
23 */
24 class D3D11Renderbuffer
25 {
26 public:
27 D3D11Renderbuffer() = 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 Direct3D 11 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 D3D11RenderTarget instead.
48 */
49 class D3D11Framebuffer
50 {
51 public:
52 D3D11Framebuffer() = 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 Direct3D 11 backend (aliased as `RHI::RenderTarget`)
66
67 Records the color textures addressed by attachment index and an optional depth/stencil (ignored for
68 2D). @ref BindDraw() records the target on the device so the following clears and draws are associated
69 with its contiguously attached color attachments (0..count-1, bounded by @ref SetDrawBuffers(), the
70 `glDrawBuffers` equivalent). One RTV per attachment is created lazily from the attached textures and
71 `OMSetRenderTargets` is routed through here.
72 */
73 class D3D11RenderTarget
74 {
75 public:
76 static constexpr std::uint32_t MaxColorAttachments = 8;
77
78 D3D11RenderTarget();
79 ~D3D11RenderTarget();
80
81 D3D11RenderTarget(const D3D11RenderTarget&) = delete;
82 D3D11RenderTarget& operator=(const D3D11RenderTarget&) = delete;
83
84 /** @brief Returns the render target view of the given color attachment (created lazily from its texture), or `nullptr` */
85 ID3D11RenderTargetView* GetRTV(std::uint32_t index = 0);
86 /**
87 @brief Fills @p outRtvs with the views of the contiguously attached color attachments enabled for drawing
88
89 The count is the contiguous attached run from attachment 0, bounded by @ref SetDrawBuffers()
90 (mirroring `glDrawBuffers`), and truncated at the first attachment whose view cannot be created.
91 @returns The number of views written (0 if attachment 0 is missing or unusable)
92 */
93 std::uint32_t GetRTVs(ID3D11RenderTargetView* outRtvs[MaxColorAttachments]);
94 /** @brief Returns the number of contiguously attached color textures starting at attachment 0 */
95 std::uint32_t GetAttachedCount() const;
96
97 /** @brief Attaches a texture as the color attachment with the given index */
98 void AttachColorTexture(D3D11Texture& texture, std::uint32_t index);
99 /** @brief Detaches any texture from the color attachment with the given index */
100 void DetachColorTexture(std::uint32_t index);
101
102 /** @brief Records a depth/stencil buffer (no storage is created) */
103 void AttachDepthStencil(DepthStencilFormat format, std::int32_t width, std::int32_t height);
104 /** @brief Clears the recorded depth/stencil buffer */
105 void DetachDepthStencil(DepthStencilFormat format);
106
107 /** @brief Binds the render target as the current draw target on the device */
108 void BindDraw();
109 /** @brief Unbinds any render target from the device */
110 static void UnbindDraw();
111 /** @brief Sets the number of color attachments enabled for drawing */
112 bool SetDrawBuffers(std::uint32_t numColorAttachments);
113
114 /** @brief Returns `true` if the target has a usable color attachment 0 */
115 bool IsStatusComplete();
116
117 /** @brief Hints that the depth/stencil contents are no longer needed (no-op) */
118 void InvalidateDepthStencil(DepthStencilFormat format);
119
120 /** @brief Sets a debug label for the render target (ignored) */
121 void SetObjectLabel(StringView label);
122
123 /** @brief Returns the texture attached at the given color attachment index, or `nullptr` */
124 inline D3D11Texture* GetColorTexture(std::uint32_t index) const {
125 return (index < MaxColorAttachments ? _colorTextures[index] : nullptr);
126 }
127 /** @brief Returns the number of color attachments enabled for drawing (see @ref SetDrawBuffers()) */
128 inline std::uint32_t GetNumDrawBuffers() const {
129 return _numDrawBuffers;
130 }
131
132 private:
133 D3D11Texture* _colorTextures[MaxColorAttachments];
134 std::uint32_t _numDrawBuffers;
135 // Render target view per color attachment, created lazily and invalidated when its attachment changes
136 ID3D11RenderTargetView* _rtvs[MaxColorAttachments];
137 D3D11Texture* _rtvTextures[MaxColorAttachments]; // the texture _rtvs[i] was built for (to detect attachment changes)
138
139 /** @brief Releases the view of one color attachment (on attachment change and destruction) */
140 void ReleaseRTV(std::uint32_t index);
141 };
142 }