Debian Codegraph @

Sources/nCine/Graphics/RHI/Vulkan/VulkanRenderTarget.cpp

raw
1 #include "VulkanRenderTarget.h"
2 #include "VulkanDevice.h"
3 #include "VulkanTexture.h"
4 #include "VulkanCommon.h"
5
6 #include <cstring>
7
8 namespace nCine::RHI::Vulkan
9 {
10 VulkanRenderTarget::VulkanRenderTarget()
11 : _numDrawBuffers(1), _framebuffer(0), _framebufferViewCount(0)
12 {
13 for (std::uint32_t i = 0; i < MaxColorAttachments; i++) {
14 _colorTextures[i] = nullptr;
15 _framebufferViews[i] = 0;
16 }
17 }
18
19 VulkanRenderTarget::~VulkanRenderTarget()
20 {
21 // Clear from the device first so a destroyed render target can't dangle as the current one
22 VulkanDevice::UnbindRenderTarget(this);
23 ReleaseFramebuffer();
24 }
25
26 void VulkanRenderTarget::ReleaseFramebuffer()
27 {
28 // Defer the framebuffer free: an in-flight frame's command buffer may still have a render pass begun on it
29 // (up to MaxFramesInFlight frames run concurrently), and this also runs on an attachment rebuild; freeing
30 // now would be a GPU use-after-free. The device frees it once every referencing frame has completed.
31 // VkEnqueueDestroy no-ops if the device is already gone (teardown).
32 VkEnqueueDestroy(VkDeferredResource::Framebuffer, _framebuffer);
33 _framebuffer = 0;
34 for (std::uint32_t i = 0; i < MaxColorAttachments; i++) {
35 _framebufferViews[i] = 0;
36 }
37 _framebufferViewCount = 0;
38 }
39
40 std::uint32_t VulkanRenderTarget::GetAttachedCount() const
41 {
42 std::uint32_t count = 0;
43 while (count < MaxColorAttachments && _colorTextures[count] != nullptr) {
44 count++;
45 }
46 return count;
47 }
48
49 std::uint64_t VulkanRenderTarget::GetFramebuffer()
50 {
51 VulkanTexture* color = _colorTextures[0];
52 VkDevice device = VkDeviceHandle();
53 if (color == nullptr || device == VK_NULL_HANDLE || color->GetWidth() <= 0 || color->GetHeight() <= 0) {
54 return 0;
55 }
56
57 // The framebuffer spans every contiguously attached color texture (attachment 0..count-1); ensure each
58 // attachment's image / view exist, then (re)build the framebuffer if any view (or the count) changed
59 const std::uint32_t count = GetAttachedCount();
60 std::uint64_t views[MaxColorAttachments];
61 VkImageView attachments[MaxColorAttachments];
62 VkFormat formats[MaxColorAttachments];
63 for (std::uint32_t i = 0; i < count; i++) {
64 views[i] = _colorTextures[i]->GpuView();
65 if (views[i] == 0) {
66 return 0;
67 }
68 attachments[i] = reinterpret_cast<VkImageView>(views[i]);
69 formats[i] = VkFormat(_colorTextures[i]->GpuFormat());
70 }
71 if (_framebuffer != 0 && _framebufferViewCount == count &&
72 std::memcmp(_framebufferViews, views, count * sizeof(std::uint64_t)) == 0) {
73 return _framebuffer;
74 }
75 ReleaseFramebuffer();
76
77 VkRenderPass renderPass = VkGetColorRenderPassMrt(formats, count);
78 if (renderPass == VK_NULL_HANDLE) {
79 return 0;
80 }
81 VkFramebufferCreateInfo fci = {};
82 fci.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
83 fci.renderPass = renderPass;
84 fci.attachmentCount = count;
85 fci.pAttachments = attachments;
86 fci.width = std::uint32_t(color->GetWidth());
87 fci.height = std::uint32_t(color->GetHeight());
88 fci.layers = 1;
89 VkFramebuffer framebuffer = VK_NULL_HANDLE;
90 if (vkCreateFramebuffer(device, &fci, nullptr, &framebuffer) != VK_SUCCESS) {
91 return 0;
92 }
93 _framebuffer = reinterpret_cast<std::uint64_t>(framebuffer);
94 for (std::uint32_t i = 0; i < count; i++) {
95 _framebufferViews[i] = views[i];
96 }
97 _framebufferViewCount = count;
98 return _framebuffer;
99 }
100
101 void VulkanRenderTarget::AttachColorTexture(VulkanTexture& texture, std::uint32_t index)
102 {
103 if (index < MaxColorAttachments) {
104 _colorTextures[index] = &texture;
105 texture.SetRenderTarget(true);
106 // Any attachment is (potentially) part of the framebuffer now, so a change to any slot rebuilds it
107 ReleaseFramebuffer();
108 }
109 }
110
111 void VulkanRenderTarget::DetachColorTexture(std::uint32_t index)
112 {
113 if (index < MaxColorAttachments) {
114 _colorTextures[index] = nullptr;
115 ReleaseFramebuffer();
116 }
117 }
118
119 void VulkanRenderTarget::AttachDepthStencil(DepthStencilFormat format, std::int32_t width, std::int32_t height)
120 {
121 static_cast<void>(format);
122 static_cast<void>(width);
123 static_cast<void>(height);
124 }
125
126 void VulkanRenderTarget::DetachDepthStencil(DepthStencilFormat format)
127 {
128 static_cast<void>(format);
129 }
130
131 void VulkanRenderTarget::BindDraw()
132 {
133 VulkanDevice::SetRenderTarget(this);
134 }
135
136 void VulkanRenderTarget::UnbindDraw()
137 {
138 VulkanDevice::SetRenderTarget(nullptr);
139 }
140
141 bool VulkanRenderTarget::SetDrawBuffers(std::uint32_t numColorAttachments)
142 {
143 _numDrawBuffers = numColorAttachments;
144 return true;
145 }
146
147 bool VulkanRenderTarget::IsStatusComplete()
148 {
149 return (_colorTextures[0] != nullptr);
150 }
151
152 void VulkanRenderTarget::InvalidateDepthStencil(DepthStencilFormat format)
153 {
154 static_cast<void>(format);
155 }
156
157 void VulkanRenderTarget::SetObjectLabel(StringView label)
158 {
159 static_cast<void>(label);
160 }
161 }