Debian Codegraph @

Source/Core/VideoBackends/Vulkan/VKTexture.h

raw symbols SCIP
1 // Copyright 2017 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #pragma once
5
6 #include <memory>
7 #include <string>
8 #include <string_view>
9
10 #include "VideoBackends/Vulkan/VulkanLoader.h"
11 #include "VideoCommon/AbstractFramebuffer.h"
12 #include "VideoCommon/AbstractStagingTexture.h"
13 #include "VideoCommon/AbstractTexture.h"
14
15 namespace Vulkan
16 {
17 class StagingBuffer;
18 class Texture2D;
19
20 class VKTexture final : public AbstractTexture
21 {
22 public:
23 // Custom image layouts, mainly used for switching to/from compute
24 enum class ComputeImageLayout
25 {
26 Undefined,
27 ReadOnly,
28 WriteOnly,
29 ReadWrite
30 };
31
32 VKTexture() = delete;
33 VKTexture(const TextureConfig& tex_config, VmaAllocation alloc, VkImage image,
34 std::string_view name, VkImageLayout layout = VK_IMAGE_LAYOUT_UNDEFINED,
35 ComputeImageLayout compute_layout = ComputeImageLayout::Undefined);
36 ~VKTexture() override;
37
38 static VkFormat GetLinearFormat(VkFormat format);
39 static VkFormat GetVkFormatForHostTextureFormat(AbstractTextureFormat format);
40 static VkImageAspectFlags GetImageAspectForFormat(AbstractTextureFormat format);
41 static VkImageAspectFlags GetImageViewAspectForFormat(AbstractTextureFormat format);
42
43 void CopyRectangleFromTexture(const AbstractTexture* src,
44 const MathUtil::Rectangle<int>& src_rect, u32 src_layer,
45 u32 src_level, const MathUtil::Rectangle<int>& dst_rect,
46 u32 dst_layer, u32 dst_level) override;
47 void ResolveFromTexture(const AbstractTexture* src, const MathUtil::Rectangle<int>& rect,
48 u32 layer, u32 level) override;
49 void Load(u32 level, u32 width, u32 height, u32 row_length, const u8* buffer, size_t buffer_size,
50 u32 layer) override;
51 void FinishedRendering() override;
52
53 VkImage GetImage() const { return m_image; }
54 VkImageView GetView() const { return m_view; }
55 VkImageLayout GetLayout() const { return m_layout; }
56 VkFormat GetVkFormat() const { return GetVkFormatForHostTextureFormat(m_config.format); }
57 bool IsAdopted() const { return m_alloc != VmaAllocation(VK_NULL_HANDLE); }
58
59 static std::unique_ptr<VKTexture> Create(const TextureConfig& tex_config, std::string_view name);
60 static std::unique_ptr<VKTexture>
61 CreateAdopted(const TextureConfig& tex_config, VkImage image,
62 VkImageViewType view_type = VK_IMAGE_VIEW_TYPE_2D_ARRAY,
63 VkImageLayout layout = VK_IMAGE_LAYOUT_UNDEFINED);
64
65 // Used when the render pass is changing the image layout, or to force it to
66 // VK_IMAGE_LAYOUT_UNDEFINED, if the existing contents of the image is
67 // irrelevant and will not be loaded.
68 void OverrideImageLayout(VkImageLayout new_layout);
69
70 void TransitionToLayout(VkCommandBuffer command_buffer, VkImageLayout new_layout) const;
71 void TransitionToLayout(VkCommandBuffer command_buffer, ComputeImageLayout new_layout) const;
72 void PrepareForRenderPass(VkCommandBuffer command_buffer) const;
73
74 private:
75 bool CreateView(VkImageViewType type);
76
77 VmaAllocation m_alloc;
78 VkImage m_image;
79 VkImageView m_view = VK_NULL_HANDLE;
80 mutable VkImageLayout m_layout = VK_IMAGE_LAYOUT_UNDEFINED;
81 mutable ComputeImageLayout m_compute_layout = ComputeImageLayout::Undefined;
82 mutable bool m_written_since_last_layout_change = false;
83 std::string m_name;
84 };
85
86 class VKStagingTexture final : public AbstractStagingTexture
87 {
88 struct PrivateTag
89 {
90 };
91
92 public:
93 VKStagingTexture() = delete;
94 VKStagingTexture(PrivateTag, StagingTextureType type, const TextureConfig& config,
95 std::unique_ptr<StagingBuffer> buffer, VkImage linear_image,
96 VmaAllocation linear_image_alloc);
97
98 ~VKStagingTexture() override;
99
100 void CopyFromTexture(const AbstractTexture* src, const MathUtil::Rectangle<int>& src_rect,
101 u32 src_layer, u32 src_level,
102 const MathUtil::Rectangle<int>& dst_rect) override;
103 void CopyToTexture(const MathUtil::Rectangle<int>& src_rect, AbstractTexture* dst,
104 const MathUtil::Rectangle<int>& dst_rect, u32 dst_layer,
105 u32 dst_level) override;
106
107 bool Map() override;
108 void Unmap() override;
109 void Flush() override;
110
111 static std::unique_ptr<VKStagingTexture> Create(StagingTextureType type,
112 const TextureConfig& config);
113
114 static std::pair<VkImage, VmaAllocation> CreateLinearImage(StagingTextureType type,
115 const TextureConfig& config);
116
117 private:
118 void CopyFromTextureToLinearImage(const VKTexture* src_tex,
119 const MathUtil::Rectangle<int>& src_rect, u32 src_layer,
120 u32 src_level, const MathUtil::Rectangle<int>& dst_rect);
121
122 std::unique_ptr<StagingBuffer> m_staging_buffer;
123 VkImage m_linear_image = VK_NULL_HANDLE;
124 VmaAllocation m_linear_image_alloc = VK_NULL_HANDLE;
125 u64 m_flush_fence_counter = 0;
126 };
127
128 class VKFramebuffer final : public AbstractFramebuffer
129 {
130 public:
131 VKFramebuffer(VKTexture* color_attachment, VKTexture* depth_attachment,
132 std::vector<AbstractTexture*> additional_color_attachments, u32 width, u32 height,
133 u32 layers, u32 samples, VkFramebuffer fb, VkRenderPass load_render_pass,
134 VkRenderPass discard_render_pass, VkRenderPass clear_render_pass);
135 ~VKFramebuffer() override;
136
137 VkFramebuffer GetFB() const { return m_fb; }
138 VkRect2D GetRect() const { return VkRect2D{{0, 0}, {m_width, m_height}}; }
139
140 VkRenderPass GetLoadRenderPass() const { return m_load_render_pass; }
141 VkRenderPass GetDiscardRenderPass() const { return m_discard_render_pass; }
142 VkRenderPass GetClearRenderPass() const { return m_clear_render_pass; }
143
144 void Unbind();
145 void TransitionForRender();
146 void PrepareForRenderPass();
147
148 void SetAndClear(const VkRect2D& rect, const VkClearValue& color_value,
149 const VkClearValue& depth_value);
150 std::size_t GetNumberOfAdditonalAttachments() const
151 {
152 return m_additional_color_attachments.size();
153 }
154
155 static std::unique_ptr<VKFramebuffer>
156 Create(VKTexture* color_attachments, VKTexture* depth_attachment,
157 std::vector<AbstractTexture*> additional_color_attachments);
158
159 protected:
160 VkFramebuffer m_fb;
161 VkRenderPass m_load_render_pass;
162 VkRenderPass m_discard_render_pass;
163 VkRenderPass m_clear_render_pass;
164 };
165
166 } // namespace Vulkan