Correlation 4.10.0
An Analysis Tool for Liquids and for Amorphous Solids
Loading...
Searching...
No Matches
VulkanOffscreenTarget.hpp
Go to the documentation of this file.
1
8
9#pragma once
10
11#ifdef CORRELATION_USE_VULKAN
12#include <vulkan/vulkan.h>
13#endif
14
16#include <array>
17#include <cstddef>
18#include <cstdint>
19#include <span>
20
22
23#ifdef CORRELATION_USE_VULKAN
24
30class VulkanOffscreenTarget {
31public:
32 static constexpr std::size_t STAGING_BUFFER_COUNT = 2;
33
34 VulkanOffscreenTarget() noexcept = default;
35 ~VulkanOffscreenTarget() noexcept { destroy(); }
36
37 VulkanOffscreenTarget(const VulkanOffscreenTarget &) = delete;
38 VulkanOffscreenTarget &operator=(const VulkanOffscreenTarget &) = delete;
39
40 VulkanOffscreenTarget(VulkanOffscreenTarget &&other) noexcept { moveFrom(other); }
41 VulkanOffscreenTarget &operator=(VulkanOffscreenTarget &&other) noexcept {
42 if (this != &other) {
43 destroy();
44 moveFrom(other);
45 }
46 return *this;
47 }
48
56 void create(VkDevice device, VkPhysicalDevice physical_device, uint32_t width, uint32_t height);
57
63 void resize(uint32_t width, uint32_t height);
64
68 void destroy() noexcept;
69
75 void beginRenderPass(VkCommandBuffer cmd,
76 VkClearColorValue clear_color = {{0.1F, 0.1F, 0.12F, 1.0F}});
77
82 static void endRenderPass(VkCommandBuffer cmd);
83
90 void copyToStaging(VkCommandBuffer cmd, uint32_t staging_index, bool copy_depth = false);
91
97 [[nodiscard]] std::span<const uint8_t> readoutStagingSpan(uint32_t staging_index) const noexcept;
98
102 [[nodiscard]] std::span<uint8_t> readoutStagingMutableSpan(uint32_t staging_index) noexcept;
103
107 [[nodiscard]] std::span<const float>
108 readoutDepthStagingSpan(uint32_t staging_index) const noexcept;
109
110 [[nodiscard]] bool hasDepthReadout() const noexcept {
111 return depth_format_ == VK_FORMAT_D32_SFLOAT;
112 }
113
114 [[nodiscard]] VkRenderPass renderPass() const noexcept { return render_pass_; }
115 [[nodiscard]] VkFramebuffer framebuffer() const noexcept { return framebuffer_; }
116 [[nodiscard]] VkExtent2D extent() const noexcept { return extent_; }
117 [[nodiscard]] uint32_t width() const noexcept { return extent_.width; }
118 [[nodiscard]] uint32_t height() const noexcept { return extent_.height; }
119 [[nodiscard]] VkFormat colorFormat() const noexcept { return color_format_; }
120 [[nodiscard]] VkFormat depthFormat() const noexcept { return depth_format_; }
121 [[nodiscard]] VkImage colorImage() const noexcept { return color_image_; }
122 [[nodiscard]] VkImage depthImage() const noexcept { return depth_image_; }
123 [[nodiscard]] bool isValid() const noexcept { return framebuffer_ != VK_NULL_HANDLE; }
124
125private:
126 void moveFrom(VulkanOffscreenTarget &other) noexcept;
127 void createRenderPass();
128 void createColorResources();
129 void createDepthResources();
130 void createFramebuffer();
131 void createStagingBuffers();
132 void destroyFrameResources() noexcept;
133
134 [[nodiscard]] VkFormat findDepthFormat() const;
135 void createImage(uint32_t width, uint32_t height, VkFormat format, VkImageTiling tiling,
136 VkImageUsageFlags usage, VkMemoryPropertyFlags properties, VkImage &image,
137 VkDeviceMemory &memory);
138 [[nodiscard]] VkImageView createImageView(VkImage image, VkFormat format,
139 VkImageAspectFlags aspect_flags) const;
140
141 VkDevice device_{VK_NULL_HANDLE};
142 VkPhysicalDevice physical_device_{VK_NULL_HANDLE};
143 VkExtent2D extent_{.width = 0, .height = 0};
144
145 VkFormat color_format_{VK_FORMAT_R8G8B8A8_UNORM};
146 VkFormat depth_format_{VK_FORMAT_UNDEFINED};
147
148 VkRenderPass render_pass_{VK_NULL_HANDLE};
149 VkImage color_image_{VK_NULL_HANDLE};
150 VkDeviceMemory color_memory_{VK_NULL_HANDLE};
151 VkImageView color_image_view_{VK_NULL_HANDLE};
152
153 VkImage depth_image_{VK_NULL_HANDLE};
154 VkDeviceMemory depth_memory_{VK_NULL_HANDLE};
155 VkImageView depth_image_view_{VK_NULL_HANDLE};
156
157 VkFramebuffer framebuffer_{VK_NULL_HANDLE};
158
159 std::array<VulkanBuffer, STAGING_BUFFER_COUNT> staging_buffers_{};
160 std::array<VulkanBuffer, STAGING_BUFFER_COUNT> depth_staging_buffers_{};
161};
162
163#endif // CORRELATION_USE_VULKAN
164
165} // namespace correlation::render3d::vulkan
RAII encapsulation of a Vulkan buffer and its associated device memory.
Definition VulkanComputeService.hpp:24