-
-
Notifications
You must be signed in to change notification settings - Fork 452
/
FormatConverter.cpp
278 lines (239 loc) · 12.9 KB
/
FormatConverter.cpp
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#include "FormatConverter.h"
#include "alvr_server/bindings.h"
FormatConverter::FormatConverter(Renderer *render)
: r(render)
{
}
FormatConverter::~FormatConverter()
{
for (const OutputImage &image : m_images) {
vkUnmapMemory(r->m_dev, image.memory);
vkDestroyImageView(r->m_dev, image.view, nullptr);
vkDestroyImage(r->m_dev, image.image, nullptr);
vkFreeMemory(r->m_dev, image.memory, nullptr);
}
vkDestroySampler(r->m_dev, m_sampler, nullptr);
vkDestroyQueryPool(r->m_dev, m_queryPool, nullptr);
vkDestroyCommandPool(r->m_dev, m_commandPool, nullptr);
vkDestroyDescriptorSetLayout(r->m_dev, m_descriptorLayout, nullptr);
vkDestroyImageView(r->m_dev, m_view, nullptr);
vkDestroyShaderModule(r->m_dev, m_shader, nullptr);
vkDestroyPipeline(r->m_dev, m_pipeline, nullptr);
vkDestroyPipelineLayout(r->m_dev, m_pipelineLayout, nullptr);
}
void FormatConverter::init(VkImage image, VkImageCreateInfo imageCreateInfo, int count, const unsigned char *shaderData, unsigned shaderLen)
{
m_images.resize(count);
// Sampler
VkSamplerCreateInfo samplerInfo = {};
samplerInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
samplerInfo.magFilter = VK_FILTER_NEAREST;
samplerInfo.minFilter = VK_FILTER_NEAREST;
samplerInfo.mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST;
samplerInfo.addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
samplerInfo.addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
samplerInfo.addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
samplerInfo.borderColor = VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK;
VK_CHECK(vkCreateSampler(r->m_dev, &samplerInfo, nullptr, &m_sampler));
// Timestamp query
VkQueryPoolCreateInfo queryPoolInfo = {};
queryPoolInfo.sType = VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO;
queryPoolInfo.queryType = VK_QUERY_TYPE_TIMESTAMP;
queryPoolInfo.queryCount = 1;
VK_CHECK(vkCreateQueryPool(r->m_dev, &queryPoolInfo, nullptr, &m_queryPool));
// Command buffer
VkCommandPoolCreateInfo cmdPoolInfo = {};
cmdPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
cmdPoolInfo.queueFamilyIndex = r->m_queueFamilyIndexCompute;
cmdPoolInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
VK_CHECK(vkCreateCommandPool(r->m_dev, &cmdPoolInfo, nullptr, &m_commandPool));
VkCommandBufferAllocateInfo commandBufferInfo = {};
commandBufferInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
commandBufferInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
commandBufferInfo.commandPool = m_commandPool;
commandBufferInfo.commandBufferCount = 1;
VK_CHECK(vkAllocateCommandBuffers(r->m_dev, &commandBufferInfo, &m_commandBuffer));
// Descriptors
VkDescriptorSetLayoutBinding descriptorBindings[2];
descriptorBindings[0] = {};
descriptorBindings[0].binding = 0;
descriptorBindings[0].stageFlags = VK_SHADER_STAGE_COMPUTE_BIT;
descriptorBindings[0].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
descriptorBindings[0].descriptorCount = 1;
descriptorBindings[0].pImmutableSamplers = &m_sampler;
descriptorBindings[1] = {};
descriptorBindings[1].binding = 1;
descriptorBindings[1].stageFlags = VK_SHADER_STAGE_COMPUTE_BIT;
descriptorBindings[1].descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE;
descriptorBindings[1].descriptorCount = count;
VkDescriptorSetLayoutCreateInfo descriptorSetLayoutInfo = {};
descriptorSetLayoutInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
descriptorSetLayoutInfo.bindingCount = 2;
descriptorSetLayoutInfo.pBindings = descriptorBindings;
VK_CHECK(vkCreateDescriptorSetLayout(r->m_dev, &descriptorSetLayoutInfo, nullptr, &m_descriptorLayout));
VkDescriptorSetAllocateInfo descriptorAllocInfo = {};
descriptorAllocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
descriptorAllocInfo.descriptorSetCount = 1;
descriptorAllocInfo.pSetLayouts = &m_descriptorLayout;
descriptorAllocInfo.descriptorPool = r->m_descriptorPool;
VK_CHECK(vkAllocateDescriptorSets(r->m_dev, &descriptorAllocInfo, &m_descriptor));
// Input image
VkImageViewCreateInfo viewInfo = {};
viewInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
viewInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
viewInfo.format = imageCreateInfo.format;
viewInfo.image = image;
viewInfo.subresourceRange = {};
viewInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
viewInfo.subresourceRange.baseMipLevel = 0;
viewInfo.subresourceRange.levelCount = 1;
viewInfo.subresourceRange.baseArrayLayer = 0;
viewInfo.subresourceRange.layerCount = 1;
viewInfo.components.r = VK_COMPONENT_SWIZZLE_IDENTITY;
viewInfo.components.g = VK_COMPONENT_SWIZZLE_IDENTITY;
viewInfo.components.b = VK_COMPONENT_SWIZZLE_IDENTITY;
viewInfo.components.a = VK_COMPONENT_SWIZZLE_IDENTITY;
VK_CHECK(vkCreateImageView(r->m_dev, &viewInfo, nullptr, &m_view));
VkDescriptorImageInfo descriptorImageInfo = {};
descriptorImageInfo.imageView = m_view;
descriptorImageInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
VkWriteDescriptorSet descriptorWriteSet = {};
descriptorWriteSet.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
descriptorWriteSet.descriptorCount = 1;
descriptorWriteSet.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
descriptorWriteSet.dstSet = m_descriptor;
descriptorWriteSet.dstBinding = 0;
descriptorWriteSet.pImageInfo = &descriptorImageInfo;
vkUpdateDescriptorSets(r->m_dev, 1, &descriptorWriteSet, 0, nullptr);
// Output images
for (int i = 0; i < count; ++i) {
VkImageCreateInfo imageInfo = {};
imageInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
imageInfo.imageType = VK_IMAGE_TYPE_2D;
imageInfo.format = VK_FORMAT_R8_UNORM;
imageInfo.extent.width = imageCreateInfo.extent.width;
imageInfo.extent.height = imageCreateInfo.extent.height;
imageInfo.extent.depth = 1;
imageInfo.arrayLayers = 1;
imageInfo.mipLevels = 1;
imageInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
imageInfo.samples = VK_SAMPLE_COUNT_1_BIT;
imageInfo.tiling = VK_IMAGE_TILING_LINEAR;
imageInfo.usage = VK_IMAGE_USAGE_STORAGE_BIT;
VK_CHECK(vkCreateImage(r->m_dev, &imageInfo, nullptr, &m_images[i].image));
VkMemoryRequirements memReqs;
VkMemoryAllocateInfo memAllocInfo {};
memAllocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
vkGetImageMemoryRequirements(r->m_dev, m_images[i].image, &memReqs);
memAllocInfo.allocationSize = memReqs.size;
VkMemoryPropertyFlags memType = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_CACHED_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
memAllocInfo.memoryTypeIndex = r->memoryTypeIndex(memType, memReqs.memoryTypeBits);
VK_CHECK(vkAllocateMemory(r->m_dev, &memAllocInfo, nullptr, &m_images[i].memory));
VK_CHECK(vkBindImageMemory(r->m_dev, m_images[i].image, m_images[i].memory, 0));
VkImageViewCreateInfo viewInfo = {};
viewInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
viewInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
viewInfo.format = imageInfo.format;
viewInfo.image = m_images[i].image;
viewInfo.subresourceRange = {};
viewInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
viewInfo.subresourceRange.baseMipLevel = 0;
viewInfo.subresourceRange.levelCount = 1;
viewInfo.subresourceRange.baseArrayLayer = 0;
viewInfo.subresourceRange.layerCount = 1;
viewInfo.components.r = VK_COMPONENT_SWIZZLE_IDENTITY;
viewInfo.components.g = VK_COMPONENT_SWIZZLE_IDENTITY;
viewInfo.components.b = VK_COMPONENT_SWIZZLE_IDENTITY;
viewInfo.components.a = VK_COMPONENT_SWIZZLE_IDENTITY;
VK_CHECK(vkCreateImageView(r->m_dev, &viewInfo, nullptr, &m_images[i].view));
VkDescriptorImageInfo descriptorImageInfo = {};
descriptorImageInfo.imageView = m_images[i].view;
descriptorImageInfo.imageLayout = VK_IMAGE_LAYOUT_GENERAL;
VkWriteDescriptorSet descriptorWriteSet = {};
descriptorWriteSet.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
descriptorWriteSet.descriptorCount = 1;
descriptorWriteSet.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE;
descriptorWriteSet.dstSet = m_descriptor;
descriptorWriteSet.dstBinding = 1;
descriptorWriteSet.dstArrayElement = i;
descriptorWriteSet.pImageInfo = &descriptorImageInfo;
vkUpdateDescriptorSets(r->m_dev, 1, &descriptorWriteSet, 0, nullptr);
VkImageMemoryBarrier imageBarrier = {};
imageBarrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
imageBarrier.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED;
imageBarrier.newLayout = VK_IMAGE_LAYOUT_GENERAL;
imageBarrier.image = m_images[i].image;
imageBarrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
imageBarrier.subresourceRange.layerCount = 1;
imageBarrier.subresourceRange.levelCount = 1;
imageBarrier.srcAccessMask = 0;
imageBarrier.dstAccessMask = VK_ACCESS_SHADER_WRITE_BIT;
r->commandBufferBegin();
vkCmdPipelineBarrier(r->m_commandBuffer, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0, 0, nullptr, 0, nullptr, 1, &imageBarrier);
r->commandBufferSubmit();
VkImageSubresource subresource = {};
subresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
VkSubresourceLayout layout;
vkGetImageSubresourceLayout(r->m_dev, m_images[i].image, &subresource, &layout);
m_images[i].linesize = layout.rowPitch;
VK_CHECK(vkMapMemory(r->m_dev, m_images[i].memory, 0, VK_WHOLE_SIZE, 0, reinterpret_cast<void**>(&m_images[i].mapped)));
}
// Shader
VkShaderModuleCreateInfo moduleInfo = {};
moduleInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
moduleInfo.codeSize = shaderLen;
moduleInfo.pCode = (uint32_t*)shaderData;
VK_CHECK(vkCreateShaderModule(r->m_dev, &moduleInfo, nullptr, &m_shader));
// Pipeline
VkPipelineLayoutCreateInfo pipelineLayoutInfo = {};
pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
pipelineLayoutInfo.setLayoutCount = 1;
pipelineLayoutInfo.pSetLayouts = &m_descriptorLayout;
VK_CHECK(vkCreatePipelineLayout(r->m_dev, &pipelineLayoutInfo, nullptr, &m_pipelineLayout));
VkPipelineShaderStageCreateInfo stageInfo = {};
stageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
stageInfo.stage = VK_SHADER_STAGE_COMPUTE_BIT;
stageInfo.pName = "main";
stageInfo.module = m_shader;
VkComputePipelineCreateInfo pipelineInfo = {};
pipelineInfo.sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO;
pipelineInfo.layout = m_pipelineLayout;
pipelineInfo.stage = stageInfo;
VK_CHECK(vkCreateComputePipelines(r->m_dev, nullptr, 1, &pipelineInfo, nullptr, &m_pipeline));
m_groupCountX = (imageCreateInfo.extent.width + (imageCreateInfo.extent.width & 31)) / 32;
m_groupCountY = (imageCreateInfo.extent.height + (imageCreateInfo.extent.height & 31)) / 32;
}
void FormatConverter::Convert(uint8_t **data, int *linesize)
{
VkCommandBufferBeginInfo commandBufferBegin = {};
commandBufferBegin.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
VK_CHECK(vkBeginCommandBuffer(m_commandBuffer, &commandBufferBegin));
vkCmdResetQueryPool(m_commandBuffer, m_queryPool, 0, 1);
vkCmdBindPipeline(m_commandBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, m_pipeline);
vkCmdBindDescriptorSets(m_commandBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, m_pipelineLayout, 0, 1, &m_descriptor, 0, nullptr);
vkCmdDispatch(m_commandBuffer, m_groupCountX, m_groupCountY, 1);
vkCmdWriteTimestamp(m_commandBuffer, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, m_queryPool, 0);
vkEndCommandBuffer(m_commandBuffer);
VkSubmitInfo submitInfo = {};
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
submitInfo.commandBufferCount = 1;
submitInfo.pCommandBuffers = &m_commandBuffer;
VK_CHECK(vkQueueSubmit(r->m_queueCompute, 1, &submitInfo, r->m_fence));
VK_CHECK(vkWaitForFences(r->m_dev, 1, &r->m_fence, VK_TRUE, UINT64_MAX));
VK_CHECK(vkResetFences(r->m_dev, 1, &r->m_fence));
for (size_t i = 0; i < m_images.size(); ++i) {
data[i] = m_images[i].mapped;
linesize[i] = m_images[i].linesize;
}
}
uint64_t FormatConverter::GetTimestamp()
{
uint64_t query;
VK_CHECK(vkGetQueryPoolResults(r->m_dev, m_queryPool, 0, 1, sizeof(uint64_t), &query, sizeof(uint64_t), VK_QUERY_RESULT_64_BIT));
return query * r->m_timestampPeriod;
}
RgbToYuv420::RgbToYuv420(Renderer *render, VkImage image, VkImageCreateInfo imageInfo)
: FormatConverter(render)
{
init(image, imageInfo, 3, RGBTOYUV420_SHADER_COMP_SPV_PTR, RGBTOYUV420_SHADER_COMP_SPV_LEN);
}