forked from Shell4026/ShellEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVulkanImageBuffer.cpp
More file actions
223 lines (198 loc) · 7.28 KB
/
Copy pathVulkanImageBuffer.cpp
File metadata and controls
223 lines (198 loc) · 7.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include "VulkanImageBuffer.h"
#include "VulkanBuffer.h"
#include <cassert>
namespace sh::render::vk
{
SH_RENDER_API VulkanImageBuffer::VulkanImageBuffer(VkDevice device, VkPhysicalDevice gpu, VmaAllocator allocator) :
device(device), gpu(gpu), allocator(allocator),
img(nullptr), imgMem(nullptr), imgView(nullptr), sampler(nullptr),
bUseAnisotropy(false)
{
}
SH_RENDER_API VulkanImageBuffer::VulkanImageBuffer(VulkanImageBuffer&& other) noexcept :
device(other.device), gpu(other.gpu), allocator(other.allocator),
img(other.img), imgMem(other.imgMem), imgView(other.imgView), sampler(other.sampler),
bUseAnisotropy(other.bUseAnisotropy)
{
other.img = nullptr;
other.imgMem = nullptr;
other.imgView = nullptr;
other.sampler = nullptr;
}
SH_RENDER_API auto VulkanImageBuffer::operator=(VulkanImageBuffer&& other) noexcept -> VulkanImageBuffer&
{
device = other.device;
gpu = other.gpu;
allocator = other.allocator;
img = other.img;
imgMem = other.imgMem;
imgView = other.imgView;
sampler = other.sampler;
other.img = nullptr;
other.imgMem = nullptr;
other.imgView = nullptr;
other.sampler = nullptr;
bUseAnisotropy = other.bUseAnisotropy;
return *this;
}
SH_RENDER_API VulkanImageBuffer::~VulkanImageBuffer()
{
Clean();
}
SH_RENDER_API void VulkanImageBuffer::Clean()
{
if (sampler)
{
vkDestroySampler(device, sampler, nullptr);
sampler = nullptr;
}
if (imgView)
{
vkDestroyImageView(device, imgView, nullptr);
imgView = nullptr;
}
if (img)
{
vmaDestroyImage(allocator, img, imgMem);
img = nullptr;
imgMem = nullptr;
}
}
SH_RENDER_API void VulkanImageBuffer::UseAnisotropy(bool bUse)
{
bUseAnisotropy = bUse;
}
SH_RENDER_API auto VulkanImageBuffer::Create(uint32_t width, uint32_t height, VkFormat format, VkImageUsageFlags usage, VkImageAspectFlags aspectFlag, VkPhysicalDeviceProperties* gpuProp) -> VkResult
{
VkImageCreateInfo info{};
info.sType = VkStructureType::VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
info.imageType = VkImageType::VK_IMAGE_TYPE_2D;
info.extent.width = width;
info.extent.height = height;
info.extent.depth = 1;
info.mipLevels = 1;
info.arrayLayers = 1;
info.format = format;
info.tiling = VkImageTiling::VK_IMAGE_TILING_OPTIMAL;
info.initialLayout = VkImageLayout::VK_IMAGE_LAYOUT_UNDEFINED;
info.usage = usage;
info.sharingMode = VkSharingMode::VK_SHARING_MODE_EXCLUSIVE;
info.samples = VkSampleCountFlagBits::VK_SAMPLE_COUNT_1_BIT;
info.flags = 0;
VmaAllocationCreateInfo allocCreateInfo{};
allocCreateInfo.usage = VmaMemoryUsage::VMA_MEMORY_USAGE_AUTO;
auto result = vmaCreateImage(allocator, &info, &allocCreateInfo, &img, &imgMem, nullptr);
assert(result == VkResult::VK_SUCCESS);
if (result != VkResult::VK_SUCCESS)
return result;
VkImageViewCreateInfo viewCreateInfo{};
viewCreateInfo.sType = VkStructureType::VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
viewCreateInfo.image = img;
viewCreateInfo.viewType = VkImageViewType::VK_IMAGE_VIEW_TYPE_2D;
viewCreateInfo.format = format;
viewCreateInfo.subresourceRange.aspectMask = aspectFlag;
viewCreateInfo.subresourceRange.baseMipLevel = 0;
viewCreateInfo.subresourceRange.levelCount = 1;
viewCreateInfo.subresourceRange.baseArrayLayer = 0;
viewCreateInfo.subresourceRange.layerCount = 1;
result = vkCreateImageView(device, &viewCreateInfo, nullptr, &imgView);
assert(result == VkResult::VK_SUCCESS);
if (result != VkResult::VK_SUCCESS)
return result;
VkSamplerCreateInfo samplerInfo{};
samplerInfo.sType = VkStructureType::VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
samplerInfo.magFilter = VkFilter::VK_FILTER_LINEAR;
samplerInfo.minFilter = VkFilter::VK_FILTER_LINEAR;
samplerInfo.addressModeU = VkSamplerAddressMode::VK_SAMPLER_ADDRESS_MODE_REPEAT;
samplerInfo.addressModeV = VkSamplerAddressMode::VK_SAMPLER_ADDRESS_MODE_REPEAT;
samplerInfo.addressModeW = VkSamplerAddressMode::VK_SAMPLER_ADDRESS_MODE_REPEAT;
samplerInfo.anisotropyEnable = bUseAnisotropy;
samplerInfo.maxAnisotropy = 0;
if (bUseAnisotropy)
{
if(gpuProp)
samplerInfo.maxAnisotropy = gpuProp->limits.maxSamplerAnisotropy;
else
{
VkPhysicalDeviceProperties properties{};
vkGetPhysicalDeviceProperties(gpu, &properties);
samplerInfo.maxAnisotropy = properties.limits.maxSamplerAnisotropy;
}
}
samplerInfo.borderColor = VkBorderColor::VK_BORDER_COLOR_INT_OPAQUE_BLACK;
samplerInfo.unnormalizedCoordinates = false;
samplerInfo.compareEnable = false;
samplerInfo.compareOp = VkCompareOp::VK_COMPARE_OP_ALWAYS;
samplerInfo.mipmapMode = VkSamplerMipmapMode::VK_SAMPLER_MIPMAP_MODE_LINEAR;
samplerInfo.mipLodBias = 0.0f;
samplerInfo.minLod = 0.0f;
samplerInfo.maxLod = 0.0f;
result = vkCreateSampler(device, &samplerInfo, nullptr, &sampler);
assert(result == VkResult::VK_SUCCESS);
return result;
}
SH_RENDER_API void VulkanImageBuffer::TransitionImageLayout(VkQueue queue, VulkanCommandBuffer* cmd, VkFormat format, VkImageLayout oldLayout, VkImageLayout newLayout)
{
VkImageMemoryBarrier barrier{};
barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
barrier.oldLayout = oldLayout;
barrier.newLayout = newLayout;
barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
barrier.image = img;
barrier.subresourceRange.aspectMask = VkImageAspectFlagBits::VK_IMAGE_ASPECT_COLOR_BIT;
barrier.subresourceRange.baseMipLevel = 0;
barrier.subresourceRange.levelCount = 1;
barrier.subresourceRange.baseArrayLayer = 0;
barrier.subresourceRange.layerCount = 1;
barrier.srcAccessMask = 0; // TODO
barrier.dstAccessMask = 0; // TODO
VkPipelineStageFlags sourceStage;
VkPipelineStageFlags destinationStage;
if (oldLayout == VkImageLayout::VK_IMAGE_LAYOUT_UNDEFINED && newLayout == VkImageLayout::VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL)
{
barrier.srcAccessMask = 0;
barrier.dstAccessMask = VkAccessFlagBits::VK_ACCESS_TRANSFER_WRITE_BIT;
sourceStage = VkPipelineStageFlagBits::VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
destinationStage = VkPipelineStageFlagBits::VK_PIPELINE_STAGE_TRANSFER_BIT;
}
else if (oldLayout == VkImageLayout::VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL && newLayout == VkImageLayout::VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL)
{
barrier.srcAccessMask = VkAccessFlagBits::VK_ACCESS_TRANSFER_WRITE_BIT;
barrier.dstAccessMask = VkAccessFlagBits::VK_ACCESS_SHADER_READ_BIT;
sourceStage = VkPipelineStageFlagBits::VK_PIPELINE_STAGE_TRANSFER_BIT;
destinationStage = VkPipelineStageFlagBits::VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;
}
else
{
throw std::invalid_argument("unsupported layout transition!");
}
VkCommandBufferBeginInfo beginInfo{};
beginInfo.sType = VkStructureType::VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
beginInfo.flags = VkCommandBufferUsageFlagBits::VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
cmd->Submit(queue, [&]()
{
vkCmdPipelineBarrier(
cmd->GetCommandBuffer(),
sourceStage, destinationStage,
0,
0, nullptr,
0, nullptr,
1, &barrier
);
},
&beginInfo);
}
SH_RENDER_API auto VulkanImageBuffer::GetImage() const -> VkImage
{
return img;
}
SH_RENDER_API auto VulkanImageBuffer::GetImageView() const -> VkImageView
{
return imgView;
}
SH_RENDER_API auto VulkanImageBuffer::GetSampler() const -> VkSampler
{
return sampler;
}
}