Debian Codegraph @

Sources/nCine/Graphics/RHI/Vulkan/VulkanRenderTarget.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::Vulkan
12 {
13 class VulkanTexture;
14
15 /**
16 @brief Renderbuffer stub of the Vulkan backend (aliased as `RHI::Renderbuffer`)
17
18 Carries no depth/stencil storage (the renderer is 2D); the class just records the format and
19 size to satisfy the contract alias. A depth/stencil `VkImage` could be added here when needed.
20 */
21 class VulkanRenderbuffer
22 {
23 public:
24 VulkanRenderbuffer() = default;
25 void Create(DepthStencilFormat format, std::int32_t width, std::int32_t height) {
26 _format = format;
27 _width = width;
28 _height = height;
29 }
30 inline std::uint32_t GetGLHandle() const {
31 return 0;
32 }
33
34 private:
35 DepthStencilFormat _format = DepthStencilFormat::None;
36 std::int32_t _width = 0;
37 std::int32_t _height = 0;
38 };
39
40 /**
41 @brief Framebuffer stub of the Vulkan backend (aliased as `RHI::Framebuffer`)
42
43 Provided only for the contract alias (the default-framebuffer rebinding some window backends use).
44 Off-screen rendering is routed through @ref VulkanRenderTarget instead.
45 */
46 class VulkanFramebuffer
47 {
48 public:
49 VulkanFramebuffer() = 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 Vulkan backend (aliased as `RHI::RenderTarget`)
63
64 Records the color textures addressed by attachment index and an optional depth/stencil (ignored for
65 2D). @ref BindDraw() records the target on the device so the following clears and draws are associated
66 with its color attachments. The backend builds a `VkFramebuffer` over the image views of every
67 contiguously attached color texture (against a device-provided render pass cached by the full
68 attachment-format signature, so multiple render targets work); the device begins a render pass on it
69 for the draws routed here.
70 */
71 class VulkanRenderTarget
72 {
73 public:
74 static constexpr std::uint32_t MaxColorAttachments = 8;
75
76 VulkanRenderTarget();
77 ~VulkanRenderTarget();
78
79 VulkanRenderTarget(const VulkanRenderTarget&) = delete;
80 VulkanRenderTarget& operator=(const VulkanRenderTarget&) = delete;
81
82 /** @brief Returns (building/caching on demand) the `VkFramebuffer` over all contiguously attached color textures, or `0` if incomplete. Opaque `std::uint64_t` to keep this header free of `vulkan.h`. */
83 std::uint64_t GetFramebuffer();
84 /** @brief Releases the cached framebuffer (on attachment change / destruction) */
85 void ReleaseFramebuffer();
86
87 /** @brief Attaches a texture as the color attachment with the given index */
88 void AttachColorTexture(VulkanTexture& texture, std::uint32_t index);
89 /** @brief Detaches any texture from the color attachment with the given index */
90 void DetachColorTexture(std::uint32_t index);
91
92 /** @brief Records a depth/stencil buffer (no storage is created) */
93 void AttachDepthStencil(DepthStencilFormat format, std::int32_t width, std::int32_t height);
94 /** @brief Clears the recorded depth/stencil buffer */
95 void DetachDepthStencil(DepthStencilFormat format);
96
97 /** @brief Binds the render target as the current draw target on the device */
98 void BindDraw();
99 /** @brief Unbinds any render target from the device */
100 static void UnbindDraw();
101 /** @brief Sets the number of color attachments enabled for drawing */
102 bool SetDrawBuffers(std::uint32_t numColorAttachments);
103
104 /** @brief Returns `true` if the target has a usable color attachment 0 */
105 bool IsStatusComplete();
106
107 /** @brief Hints that the depth/stencil contents are no longer needed (no-op) */
108 void InvalidateDepthStencil(DepthStencilFormat format);
109
110 /** @brief Sets a debug label for the render target (ignored) */
111 void SetObjectLabel(StringView label);
112
113 /** @brief Returns the texture attached at the given color attachment index, or `nullptr` */
114 inline VulkanTexture* GetColorTexture(std::uint32_t index) const {
115 return (index < MaxColorAttachments ? _colorTextures[index] : nullptr);
116 }
117
118 /** @brief Returns the number of contiguously attached color textures starting from attachment 0 (the framebuffer / render-pass attachment count) */
119 std::uint32_t GetAttachedCount() const;
120
121 /** @brief Returns the number of color attachments enabled for drawing (see @ref SetDrawBuffers()) */
122 inline std::uint32_t GetNumDrawBuffers() const {
123 return _numDrawBuffers;
124 }
125
126 private:
127 VulkanTexture* _colorTextures[MaxColorAttachments];
128 std::uint32_t _numDrawBuffers;
129
130 // Cached VkFramebuffer over all attached color textures (opaque; rebuilt when any attachment changes)
131 std::uint64_t _framebuffer;
132 std::uint64_t _framebufferViews[MaxColorAttachments]; // the image views the framebuffer was built for
133 std::uint32_t _framebufferViewCount; // their count (the framebuffer's attachment count)
134 };
135 }