Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
vulkanwrapper.cpp
Go to the documentation of this file.
1// Copyright (C) 2019 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5// NOTE: Some of the code below is adapted from the public domain code at https://vulkan-tutorial.com/
6
7#define GL_GLEXT_PROTOTYPES
8
10
11#include <QImage>
12#include <QVarLengthArray>
13#include <QOpenGLContext>
14#include <QtGui/qopengl.h>
15#include <QtOpenGL/private/qvkconvenience_p.h>
16
17#include <set>
18
19#include <unistd.h>
20
21#include <QDebug>
22
24
25static constexpr bool vwExtraDebug = false;
26
27#define DECL_VK_FUNCTION(name)
28 PFN_ ## name name = nullptr;
29
30#define IMPL_VK_FUNCTION(name)
31 name = reinterpret_cast<PFN_ ## name>(f_glGetVkProcAddrNV(#name));
32 if (!name) {
33 qCritical() << "ERROR in Vulkan proc lookup. Could not find " #name;
34 }
35
38 int presentFamily = -1;
39
40 bool isComplete() {
41 return graphicsFamily >= 0 && presentFamily >= 0;
42 }
43};
44
46{
47public:
48 explicit VulkanWrapperPrivate(QOpenGLContext *glContext);
49
51 VulkanImageWrapper *createTextureImageFromData(const uchar *pixels, uint bufferSize, const QSize &size, VkFormat vkFormat);
52
54
55private:
56 DECL_VK_FUNCTION(vkAllocateCommandBuffers);
57 DECL_VK_FUNCTION(vkAllocateMemory);
58 DECL_VK_FUNCTION(vkBeginCommandBuffer);
59 DECL_VK_FUNCTION(vkBindImageMemory);
60 DECL_VK_FUNCTION(vkCmdCopyBufferToImage);
61 DECL_VK_FUNCTION(vkCmdPipelineBarrier);
62 DECL_VK_FUNCTION(vkCreateImage);
63 DECL_VK_FUNCTION(vkDestroyImage);
64 DECL_VK_FUNCTION(vkDestroyBuffer);
65 DECL_VK_FUNCTION(vkEndCommandBuffer);
66 DECL_VK_FUNCTION(vkFreeCommandBuffers);
67 DECL_VK_FUNCTION(vkFreeMemory);
68 DECL_VK_FUNCTION(vkGetImageMemoryRequirements);
69 DECL_VK_FUNCTION(vkGetPhysicalDeviceMemoryProperties);
70 DECL_VK_FUNCTION(vkMapMemory);
71 DECL_VK_FUNCTION(vkQueueSubmit);
72 DECL_VK_FUNCTION(vkQueueWaitIdle);
73 DECL_VK_FUNCTION(vkUnmapMemory);
74 DECL_VK_FUNCTION(vkCreateBuffer);
75 DECL_VK_FUNCTION(vkGetBufferMemoryRequirements);
76 DECL_VK_FUNCTION(vkBindBufferMemory);
77
78 DECL_VK_FUNCTION(vkCreateInstance);
79 DECL_VK_FUNCTION(vkEnumeratePhysicalDevices);
80 DECL_VK_FUNCTION(vkGetPhysicalDeviceProperties);
81 DECL_VK_FUNCTION(vkCreateDevice);
82 DECL_VK_FUNCTION(vkGetPhysicalDeviceFormatProperties);
83
84 DECL_VK_FUNCTION(vkGetPhysicalDeviceQueueFamilyProperties);
85 DECL_VK_FUNCTION(vkCreateCommandPool);
86
87 DECL_VK_FUNCTION(vkGetDeviceQueue);
88 DECL_VK_FUNCTION(vkGetImageMemoryRequirements2KHR);
89 DECL_VK_FUNCTION(vkGetMemoryFdKHR);
90
91 //DECL_VK_FUNCTION(vkGetPhysicalDeviceSurfaceSupportKHR);
92
93 void initFunctions(PFNGLGETVKPROCADDRNVPROC f_glGetVkProcAddrNV) {
94 IMPL_VK_FUNCTION(vkAllocateCommandBuffers);
95 IMPL_VK_FUNCTION(vkAllocateMemory);
96 IMPL_VK_FUNCTION(vkBeginCommandBuffer);
97 IMPL_VK_FUNCTION(vkBindImageMemory);
98 IMPL_VK_FUNCTION(vkCmdCopyBufferToImage);
99 IMPL_VK_FUNCTION(vkCmdPipelineBarrier);
100 IMPL_VK_FUNCTION(vkCreateImage);
101 IMPL_VK_FUNCTION(vkDestroyImage);
102 IMPL_VK_FUNCTION(vkDestroyBuffer);
103 IMPL_VK_FUNCTION(vkEndCommandBuffer);
104 IMPL_VK_FUNCTION(vkFreeCommandBuffers);
105 IMPL_VK_FUNCTION(vkFreeMemory);
106 IMPL_VK_FUNCTION(vkGetImageMemoryRequirements);
107 IMPL_VK_FUNCTION(vkGetPhysicalDeviceMemoryProperties);
108 IMPL_VK_FUNCTION(vkMapMemory);
109 IMPL_VK_FUNCTION(vkQueueSubmit);
110 IMPL_VK_FUNCTION(vkQueueWaitIdle);
111 IMPL_VK_FUNCTION(vkUnmapMemory);
112 IMPL_VK_FUNCTION(vkCreateBuffer);
113 IMPL_VK_FUNCTION(vkGetBufferMemoryRequirements);
114 IMPL_VK_FUNCTION(vkBindBufferMemory);
115
116 IMPL_VK_FUNCTION(vkCreateInstance);
117 IMPL_VK_FUNCTION(vkEnumeratePhysicalDevices);
118 IMPL_VK_FUNCTION(vkGetPhysicalDeviceProperties);
119 IMPL_VK_FUNCTION(vkCreateDevice);
120 IMPL_VK_FUNCTION(vkGetPhysicalDeviceFormatProperties);
121
122 IMPL_VK_FUNCTION(vkGetPhysicalDeviceQueueFamilyProperties);
123 IMPL_VK_FUNCTION(vkCreateCommandPool);
124
125 IMPL_VK_FUNCTION(vkGetDeviceQueue);
126 IMPL_VK_FUNCTION(vkGetImageMemoryRequirements2KHR);
127 IMPL_VK_FUNCTION(vkGetMemoryFdKHR);
128
129 //IMPL_VK_FUNCTION(vkGetPhysicalDeviceSurfaceSupportKHR);
130 }
131
132 int findMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties);
133
134 VulkanImageWrapper *createImage(VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage, VkMemoryPropertyFlags properties, const QSize &size, int memSize);
135 bool transitionImageLayout(VkImage image, VkFormat /*format*/, VkImageLayout oldLayout, VkImageLayout newLayout);
136 bool createBuffer(VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties, VkBuffer& buffer, VkDeviceMemory& bufferMemory);
137 VkCommandBuffer beginSingleTimeCommands();
138 void endSingleTimeCommands(VkCommandBuffer commandBuffer);
139 void copyBufferToImage(VkBuffer buffer, VkImage image, uint32_t width, uint32_t height);
140 void createCommandPool();
141 QueueFamilyIndices findQueueFamilies(VkPhysicalDevice device);
142 bool createLogicalDevice();
143
144private:
145 VkInstance m_instance = VK_NULL_HANDLE;
146 VkPhysicalDevice m_physicalDevice = VK_NULL_HANDLE;
147 VkDevice m_device = VK_NULL_HANDLE;
148 VkCommandPool m_commandPool = VK_NULL_HANDLE;
149
150 VkQueue m_graphicsQueue = VK_NULL_HANDLE;
151
152 bool m_initFailed = false;
153};
154
163
164int VulkanWrapperPrivate::findMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties)
165{
166 VkPhysicalDeviceMemoryProperties memProperties;
167 vkGetPhysicalDeviceMemoryProperties(m_physicalDevice, &memProperties);
168
169 for (uint32_t i = 0; i < memProperties.memoryTypeCount; i++) {
170 if ((typeFilter & (1 << i)) && (memProperties.memoryTypes[i].propertyFlags & properties) == properties) {
171 return i;
172 }
173 }
174
175 qCritical("VulkanWrapper: failed to find suitable memory type!");
176 return -1;
177}
178
179
180VulkanImageWrapper *VulkanWrapperPrivate::createImage(VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage, VkMemoryPropertyFlags properties, const QSize &size, int memSize)
181{
182 VkImageCreateInfo imageInfo = {};
183 imageInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
184 imageInfo.imageType = VK_IMAGE_TYPE_2D;
185 imageInfo.extent.width = size.width();
186 imageInfo.extent.height = size.height();
187 imageInfo.extent.depth = 1;
188 imageInfo.mipLevels = 1;
189 imageInfo.arrayLayers = 1;
190 imageInfo.format = format;
191 imageInfo.tiling = tiling;
192 imageInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
193 imageInfo.usage = usage;
194 imageInfo.samples = VK_SAMPLE_COUNT_1_BIT;
195 imageInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
196
197 VkImage image = VK_NULL_HANDLE;
198
199 if (vkCreateImage(m_device, &imageInfo, nullptr, &image) != VK_SUCCESS) {
200 qCritical("VulkanWrapper: failed to create image!");
201 return nullptr;
202 }
203
204 std::unique_ptr imageWrapper = std::make_unique<VulkanImageWrapper>();
205 imageWrapper->textureImage = image;
206 imageWrapper->imgMemSize = memSize;
207 imageWrapper->imgSize = size;
208
209 VkMemoryRequirements memRequirements;
210 vkGetImageMemoryRequirements(m_device, image, &memRequirements);
211
212 VkExportMemoryAllocateInfoKHR exportAllocInfo = {};
213 exportAllocInfo.sType = VK_STRUCTURE_TYPE_EXPORT_MEMORY_ALLOCATE_INFO_KHR;
214 exportAllocInfo.handleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT_KHR;
215
216 VkMemoryAllocateInfo allocInfo = {};
217 allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
218 allocInfo.allocationSize = memRequirements.size;
219 int memoryType = findMemoryType(memRequirements.memoryTypeBits, properties);
220 if (memoryType < 0)
221 return nullptr;
222 allocInfo.memoryTypeIndex = memoryType;
223 allocInfo.pNext = &exportAllocInfo;
224
225 if (vkAllocateMemory(m_device, &allocInfo, nullptr, &imageWrapper->textureImageMemory) != VK_SUCCESS) {
226 qCritical("VulkanWrapper: failed to allocate image memory!");
227 return nullptr;
228 }
229
230 int res = vkBindImageMemory(m_device, image, imageWrapper->textureImageMemory, 0);
231 Q_UNUSED(res);
232 if (vwExtraDebug) qDebug() << "vkBindImageMemory res" << res;
233
234 VkMemoryGetFdInfoKHR memoryFdInfo = {};
235 memoryFdInfo.sType = VK_STRUCTURE_TYPE_MEMORY_GET_FD_INFO_KHR;
236 memoryFdInfo.memory = imageWrapper->textureImageMemory;
237 memoryFdInfo.handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT_KHR;
238
239 res = vkGetMemoryFdKHR(m_device, &memoryFdInfo, &imageWrapper->imgFd);
240 if (vwExtraDebug) qDebug() << "vkGetMemoryFdKHR res" << res << "fd" << imageWrapper->imgFd;
241
242 return imageWrapper.release();
243}
244
245
246bool VulkanWrapperPrivate::transitionImageLayout(VkImage image, VkFormat /*format*/, VkImageLayout oldLayout, VkImageLayout newLayout)
247{
248 VkCommandBuffer commandBuffer = beginSingleTimeCommands();
249
250 VkImageMemoryBarrier barrier = {};
251 barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
252 barrier.oldLayout = oldLayout;
253 barrier.newLayout = newLayout;
254 barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
255 barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
256 barrier.image = image;
257 barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
258 barrier.subresourceRange.baseMipLevel = 0;
259 barrier.subresourceRange.levelCount = 1;
260 barrier.subresourceRange.baseArrayLayer = 0;
261 barrier.subresourceRange.layerCount = 1;
262
263 VkPipelineStageFlags sourceStage;
264 VkPipelineStageFlags destinationStage;
265
266 if (oldLayout == VK_IMAGE_LAYOUT_UNDEFINED && newLayout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL) {
267 barrier.srcAccessMask = 0;
268 barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
269
270 sourceStage = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
271 destinationStage = VK_PIPELINE_STAGE_TRANSFER_BIT;
272 } else if (oldLayout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL && newLayout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
273 barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
274 barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
275
276 sourceStage = VK_PIPELINE_STAGE_TRANSFER_BIT;
277 destinationStage = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;
278 } else {
279 qCritical("VulkanWrapper: unsupported layout transition!");
280 return false;
281 }
282
283 vkCmdPipelineBarrier(
284 commandBuffer,
285 sourceStage, destinationStage,
286 0,
287 0, nullptr,
288 0, nullptr,
289 1, &barrier
290 );
291
292 endSingleTimeCommands(commandBuffer);
293 return true;
294}
295
296bool VulkanWrapperPrivate::createBuffer(VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties, VkBuffer& buffer, VkDeviceMemory& bufferMemory)
297{
298 VkBufferCreateInfo bufferInfo = {};
299 bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
300 bufferInfo.size = size;
301 bufferInfo.usage = usage;
302 bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
303
304 if (vkCreateBuffer(m_device, &bufferInfo, nullptr, &buffer) != VK_SUCCESS) {
305 qCritical("VulkanWrapper: failed to create buffer!");
306 return false;
307 }
308
309 VkMemoryRequirements memRequirements;
310 vkGetBufferMemoryRequirements(m_device, buffer, &memRequirements);
311
312 VkMemoryAllocateInfo allocInfo = {};
313 allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
314 allocInfo.allocationSize = memRequirements.size;
315 allocInfo.memoryTypeIndex = findMemoryType(memRequirements.memoryTypeBits, properties);
316
317 if (vkAllocateMemory(m_device, &allocInfo, nullptr, &bufferMemory) != VK_SUCCESS) {
318 qCritical("VulkanWrapper: failed to allocate buffer memory!");
319 return false;
320 }
321
322 vkBindBufferMemory(m_device, buffer, bufferMemory, 0);
323 return true;
324}
325
326
327VkCommandBuffer VulkanWrapperPrivate::beginSingleTimeCommands()
328{
329 VkCommandBufferAllocateInfo allocInfo = {};
330 allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
331 allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
332 allocInfo.commandPool = m_commandPool;
333 allocInfo.commandBufferCount = 1;
334
335 if (vwExtraDebug) qDebug() << "allocating...";
336
337 VkCommandBuffer commandBuffer;
338 int res = vkAllocateCommandBuffers(m_device, &allocInfo, &commandBuffer);
339 Q_UNUSED(res);
340 if (vwExtraDebug) qDebug() << "vkAllocateCommandBuffers res" << res;
341
342 VkCommandBufferBeginInfo beginInfo = {};
343 beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
344 beginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
345
346 res = vkBeginCommandBuffer(commandBuffer, &beginInfo);
347 if (vwExtraDebug) qDebug() << "BEGIN res" << res;
348
349 return commandBuffer;
350}
351
352void VulkanWrapperPrivate::endSingleTimeCommands(VkCommandBuffer commandBuffer)
353{
354 int res = vkEndCommandBuffer(commandBuffer);
355 Q_UNUSED(res);
356 if (vwExtraDebug) qDebug() << "END res" << res;
357
358 VkSubmitInfo submitInfo = {};
359 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
360 submitInfo.commandBufferCount = 1;
361 submitInfo.pCommandBuffers = &commandBuffer;
362
363 vkQueueSubmit(m_graphicsQueue, 1, &submitInfo, VK_NULL_HANDLE);
364 vkQueueWaitIdle(m_graphicsQueue);
365
366 vkFreeCommandBuffers(m_device, m_commandPool, 1, &commandBuffer);
367}
368
369void VulkanWrapperPrivate::copyBufferToImage(VkBuffer buffer, VkImage image, uint32_t width, uint32_t height)
370{
371 VkCommandBuffer commandBuffer = beginSingleTimeCommands();
372
373 VkBufferImageCopy region = {};
374 region.bufferOffset = 0;
375 region.bufferRowLength = 0;
376 region.bufferImageHeight = 0;
377 region.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
378 region.imageSubresource.mipLevel = 0;
379 region.imageSubresource.baseArrayLayer = 0;
380 region.imageSubresource.layerCount = 1;
381 region.imageOffset = {0, 0, 0};
382 region.imageExtent = {
383 width,
384 height,
385 1
386 };
387
388 vkCmdCopyBufferToImage(commandBuffer, buffer, image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &region);
389
390 endSingleTimeCommands(commandBuffer);
391}
392
393void VulkanWrapperPrivate::createCommandPool()
394{
395 QueueFamilyIndices queueFamilyIndices = findQueueFamilies(m_physicalDevice);
396
397 VkCommandPoolCreateInfo poolInfo = {};
398 poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
399 poolInfo.queueFamilyIndex = queueFamilyIndices.graphicsFamily;
400
401 if (vkCreateCommandPool(m_device, &poolInfo, nullptr, &m_commandPool) != VK_SUCCESS) {
402 m_initFailed = true;
403 qCritical("VulkanWrapperPrivate: could not create command pool");
404 }
405}
406
407QueueFamilyIndices VulkanWrapperPrivate::findQueueFamilies(VkPhysicalDevice device)
408{
409 QueueFamilyIndices indices;
410
411 uint32_t queueFamilyCount = 0;
412 vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, nullptr);
413 if (vwExtraDebug) qDebug() << "queueFamilyCount" << queueFamilyCount;
414
415
416 std::vector<VkQueueFamilyProperties> queueFamilies(queueFamilyCount);
417 vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, queueFamilies.data());
418
419#ifdef VULKAN_SERVER_BUFFER_EXTRA_DEBUG
420 for (const auto& queueFamily : queueFamilies) {
421 qDebug() << "....q" << "count" << queueFamily.queueCount << queueFamily.timestampValidBits << hex << queueFamily.queueFlags;
422 }
423#endif
424
425 int i = 0;
426 for (const auto& queueFamily : queueFamilies) {
427 if (queueFamily.queueCount > 0 && queueFamily.queueFlags & VK_QUEUE_GRAPHICS_BIT) {
428 indices.graphicsFamily = i;
429 break;
430 }
431 i++;
432 }
433
434 return indices;
435}
436
437bool VulkanWrapperPrivate::createLogicalDevice()
438{
439 QueueFamilyIndices indices = findQueueFamilies(m_physicalDevice);
440
441 std::vector<VkDeviceQueueCreateInfo> queueCreateInfos;
442 std::set<int> uniqueQueueFamilies = {indices.graphicsFamily}; //////, indices.presentFamily};
443
444 float queuePriority = 1.0f;
445 for (int queueFamily : uniqueQueueFamilies) {
446 VkDeviceQueueCreateInfo queueCreateInfo = {};
447 queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
448 queueCreateInfo.queueFamilyIndex = queueFamily;
449 queueCreateInfo.queueCount = 1;
450 queueCreateInfo.pQueuePriorities = &queuePriority;
451 queueCreateInfos.push_back(queueCreateInfo);
452 }
453
454 VkPhysicalDeviceFeatures deviceFeatures = {};
455
456 VkDeviceCreateInfo createInfo = {};
457 createInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
458
459 createInfo.queueCreateInfoCount = static_cast<uint32_t>(queueCreateInfos.size());
460 createInfo.pQueueCreateInfos = queueCreateInfos.data();
461
462 createInfo.pEnabledFeatures = &deviceFeatures;
463
464 if (vkCreateDevice(m_physicalDevice, &createInfo, nullptr, &m_device) != VK_SUCCESS) {
465 qCritical("VulkanWrapper: failed to create logical device!");
466 return false;
467 }
468
469 vkGetDeviceQueue(m_device, indices.graphicsFamily, 0, &m_graphicsQueue);
470 return true;
471}
472
474{
475 return createTextureImageFromData(img.constBits(), img.sizeInBytes(), img.size(), VK_FORMAT_R8G8B8A8_UNORM);
476}
477
478VulkanImageWrapper *VulkanWrapperPrivate::createTextureImageFromData(const uchar *pixels, uint bufferSize, const QSize &size, VkFormat vkFormat)
479{
480 if (m_initFailed)
481 return nullptr;
482
483 int texWidth = size.width();
484 int texHeight = size.height();
485 bool ok;
486 if (vwExtraDebug) qDebug("image load %p %dx%d", pixels, texWidth, texHeight);
487 if (!pixels) {
488 qCritical("VulkanWrapper: failed to load texture image!");
489 return nullptr;
490 }
491
492 VkBuffer stagingBuffer;
493 VkDeviceMemory stagingBufferMemory;
494 ok = createBuffer(bufferSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, stagingBuffer, stagingBufferMemory);
495
496 if (!ok)
497 return nullptr;
498
499 void* data;
500 vkMapMemory(m_device, stagingBufferMemory, 0, bufferSize, 0, &data);
501 if (vwExtraDebug) qDebug() << "mapped" << data << bufferSize;
502 memcpy(data, pixels, static_cast<size_t>(bufferSize));
503 vkUnmapMemory(m_device, stagingBufferMemory);
504
505 if (vwExtraDebug) qDebug() << "creating image...";
506
507 std::unique_ptr<VulkanImageWrapper> imageWrapper(createImage(vkFormat, VK_IMAGE_TILING_OPTIMAL, VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, size, bufferSize));
508 if (!imageWrapper)
509 return nullptr;
510
511 if (vwExtraDebug) qDebug() << "transition...";
512
513 const VkImage textureImage = imageWrapper->textureImage;
514
515 ok = transitionImageLayout(textureImage, vkFormat, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
516
517 if (!ok)
518 return nullptr;
519
520 if (vwExtraDebug) qDebug() << "copyBufferToImage...";
521 copyBufferToImage(stagingBuffer, textureImage, static_cast<uint32_t>(texWidth), static_cast<uint32_t>(texHeight));
522 transitionImageLayout(textureImage, vkFormat, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
523
524 vkDestroyBuffer(m_device, stagingBuffer, nullptr);
525 vkFreeMemory(m_device, stagingBufferMemory, nullptr);
526
527 return imageWrapper.release();
528}
529
531{
532 if (!imageWrapper)
533 return;
534
535 //"To avoid leaking resources, the application must release ownership of the file descriptor using the close system call"
536 ::close(imageWrapper->imgFd);
537
538 // clean up the image memory
539 vkDestroyImage(m_device, imageWrapper->textureImage, nullptr);
540 vkFreeMemory(m_device, imageWrapper->textureImageMemory, nullptr);
541}
542
544{
545 if (vwExtraDebug) qDebug("Creating Vulkan instance");
546 VkApplicationInfo applicationInfo = {};
547 applicationInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
548 applicationInfo.pNext = nullptr;
549 applicationInfo.pApplicationName = nullptr;
550 applicationInfo.applicationVersion = 0;
551 applicationInfo.pEngineName = nullptr;
552 applicationInfo.engineVersion = VK_MAKE_VERSION(1, 0, 0);
553 applicationInfo.apiVersion = VK_MAKE_VERSION(1, 0, 5);
554
555 VkInstanceCreateInfo instanceCreateInfo = {};
556 instanceCreateInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
557 instanceCreateInfo.pNext = nullptr;
558 instanceCreateInfo.flags = 0;
559 instanceCreateInfo.pApplicationInfo = &applicationInfo;
560 instanceCreateInfo.enabledLayerCount = 0;
561 instanceCreateInfo.ppEnabledLayerNames = nullptr;
562 instanceCreateInfo.enabledExtensionCount = 0;
563 instanceCreateInfo.ppEnabledExtensionNames = nullptr;
564
565 auto f_glGetVkProcAddrNV = reinterpret_cast<PFNGLGETVKPROCADDRNVPROC>(glContext->getProcAddress("glGetVkProcAddrNV"));
566
567 if (!f_glGetVkProcAddrNV) {
568 qCritical("VulkanWrapper: Could not find Vulkan/GL interop function glGetVkProcAddrNV");
569 m_initFailed = true;
570 return;
571 }
572
573 initFunctions(f_glGetVkProcAddrNV);
574
575 VkResult instanceCreationResult = vkCreateInstance(&instanceCreateInfo, nullptr, &m_instance);
576
577 if (vwExtraDebug) qDebug() << "result" << instanceCreationResult;
578
579 if (instanceCreationResult != VK_SUCCESS) {
580 qCritical() << "VulkanWrapper: Failed to create Vulkan instance: Error "
581 << instanceCreationResult;
582 m_initFailed = true;
583 return;
584 }
585
586 uint32_t devCount;
587
588 auto res = vkEnumeratePhysicalDevices(m_instance, &devCount, nullptr);
589 if (vwExtraDebug) qDebug() << "vkEnumeratePhysicalDevices res =" << res << "count =" << devCount;
590
591 QVarLengthArray<VkPhysicalDevice, 5> dev(devCount);
592
593 res = vkEnumeratePhysicalDevices(m_instance, &devCount, dev.data());
594 if (vwExtraDebug) qDebug() << "...devs res =" << res << "count =" << devCount;
595
596#ifdef VULKAN_SERVER_BUFFER_EXTRA_DEBUG
597 VkPhysicalDeviceProperties props;
598
599 vkGetPhysicalDeviceProperties(dev[0], &props);
600
601 qDebug() << "Properties " << hex
602 << "apiVersion" << props.apiVersion
603 << "driverVersion" << props.driverVersion
604 << "vendorID" << props.vendorID
605 << "deviceID" << props.deviceID
606 << "deviceType" << props.deviceType
607 << "deviceName" << props.deviceName;
608#endif
609
610 m_physicalDevice = dev[0]; //TODO handle the case of multiple GPUs where only some support Vulkan
611
612 bool ok = createLogicalDevice();
613 if (!ok) {
614 qCritical("VulkanWrapperPrivate: could not create logical device");
615 m_initFailed = true;
616 return;
617 }
618
619 VkPhysicalDeviceMemoryProperties memProps;
620
621
622 vkGetPhysicalDeviceMemoryProperties(dev[0], &memProps);
623
624#ifdef VULKAN_SERVER_BUFFER_EXTRA_DEBUG
625 qDebug() << "Physical memory properties:\n" << "types:" << memProps.memoryTypeCount << "heaps:" << memProps.memoryHeapCount;
626 for (uint i = 0; i < memProps.memoryTypeCount; ++i)
627 qDebug() << " " << i << "heap" << memProps.memoryTypes[i].heapIndex << "flags" << hex << memProps.memoryTypes[i].propertyFlags;
628
629 for (uint i = 0; i < memProps.memoryHeapCount; ++i)
630 qDebug() << " " << i << "size" << memProps.memoryHeaps[i].size << "flags" << hex << memProps.memoryHeaps[i].flags;
631#endif
632
633 int gpuMemoryType = -1;
634
635 for (uint i = 0; i < memProps.memoryTypeCount; ++i) {
636 if (memProps.memoryTypes[i].propertyFlags & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) {
637 gpuMemoryType = i;
638 break;
639 }
640 }
641
642 if (gpuMemoryType < 0) {
643 qCritical("VulkanWrapper: Could not find GPU memory!");
644 m_initFailed = true;
645 return;
646 }
647
648#ifdef VULKAN_SERVER_BUFFER_EXTRA_DEBUG
649 qDebug() << "GPU memory type:" << gpuMemoryType << "heap:" << memProps.memoryTypes[gpuMemoryType].heapIndex;
650
651 for (int f = 0; f <= VK_FORMAT_ASTC_12x12_SRGB_BLOCK; f++)
652 {
653 VkFormatProperties formatProps;
654 vkGetPhysicalDeviceFormatProperties(dev[0], VkFormat(f), &formatProps);
655 qDebug() << "format" << f << "features" << hex << formatProps.linearTilingFeatures << formatProps.optimalTilingFeatures << formatProps.bufferFeatures;
656 }
657#endif
658 createCommandPool();
659}
660
661
662VulkanWrapper::VulkanWrapper(QOpenGLContext *glContext)
663 : d_ptr(new VulkanWrapperPrivate(glContext))
664{
665}
666
668{
669 return d_ptr->createTextureImage(img);
670}
671
672VulkanImageWrapper *VulkanWrapper::createTextureImageFromData(const uchar *pixels, uint bufferSize, const QSize &size, uint glInternalFormat)
673{
674 VkFormat vkFormat = VkFormat(QVkConvenience::vkFormatFromGlFormat(glInternalFormat));
675 if (vkFormat == VK_FORMAT_UNDEFINED)
676 return nullptr;
677
678 return d_ptr->createTextureImageFromData(pixels, bufferSize, size, vkFormat);
679}
680
681int VulkanWrapper::getImageInfo(const VulkanImageWrapper *imgWrapper, int *memSize, int *w, int *h)
682{
683 if (memSize)
684 *memSize = imgWrapper->imgMemSize;
685 if (w)
686 *w = imgWrapper->imgSize.width();
687 if (h)
688 *h = imgWrapper->imgSize.height();
689 return imgWrapper->imgFd;
690}
691
693{
694 d_ptr->freeTextureImage(imageWrapper);
695}
696
697QT_END_NAMESPACE
VulkanImageWrapper * createTextureImage(const QImage &img)
VulkanImageWrapper * createTextureImageFromData(const uchar *pixels, uint bufferSize, const QSize &size, VkFormat vkFormat)
VulkanWrapperPrivate(QOpenGLContext *glContext)
void freeTextureImage(VulkanImageWrapper *imageWrapper)
int getImageInfo(const VulkanImageWrapper *imgWrapper, int *memSize, int *w=nullptr, int *h=nullptr)
VulkanWrapper(QOpenGLContext *glContext)
VulkanImageWrapper * createTextureImageFromData(const uchar *pixels, uint bufferSize, const QSize &size, uint glInternalFormat)
void freeTextureImage(VulkanImageWrapper *imageWrapper)
VulkanImageWrapper * createTextureImage(const QImage &img)
VkDeviceMemory textureImageMemory
#define IMPL_VK_FUNCTION(name)
static QT_BEGIN_NAMESPACE constexpr bool vwExtraDebug
#define DECL_VK_FUNCTION(name)