7#define GL_GLEXT_PROTOTYPES
12#include <QVarLengthArray>
13#include <QOpenGLContext>
14#include <QtGui/qopengl.h>
15#include <QtOpenGL/private/qvkconvenience_p.h>
27#define DECL_VK_FUNCTION(name)
28 PFN_ ## name name = nullptr;
30#define IMPL_VK_FUNCTION(name)
31 name = reinterpret_cast<PFN_ ## name>(f_glGetVkProcAddrNV(#name));
33 qCritical() << "ERROR in Vulkan proc lookup. Could not find " #name;
93 void initFunctions(PFNGLGETVKPROCADDRNVPROC f_glGetVkProcAddrNV) {
132 int findMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties);
134 VulkanImageWrapper *createImage(VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage, VkMemoryPropertyFlags properties,
const QSize &size,
int memSize);
135 bool transitionImageLayout(VkImage image, VkFormat , 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();
142 bool createLogicalDevice();
152 bool m_initFailed =
false;
166 VkPhysicalDeviceMemoryProperties memProperties;
167 vkGetPhysicalDeviceMemoryProperties(m_physicalDevice, &memProperties);
169 for (uint32_t i = 0; i < memProperties.memoryTypeCount; i++) {
170 if ((typeFilter & (1 << i)) && (memProperties.memoryTypes[i].propertyFlags & properties) == properties) {
175 qCritical(
"VulkanWrapper: failed to find suitable memory type!");
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;
197 VkImage image = VK_NULL_HANDLE;
199 if (vkCreateImage(m_device, &imageInfo,
nullptr, &image) != VK_SUCCESS) {
200 qCritical(
"VulkanWrapper: failed to create image!");
204 std::unique_ptr imageWrapper = std::make_unique<VulkanImageWrapper>();
205 imageWrapper->textureImage = image;
206 imageWrapper->imgMemSize = memSize;
207 imageWrapper->imgSize = size;
209 VkMemoryRequirements memRequirements;
210 vkGetImageMemoryRequirements(m_device, image, &memRequirements);
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;
216 VkMemoryAllocateInfo allocInfo = {};
217 allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
218 allocInfo.allocationSize = memRequirements.size;
219 int memoryType = findMemoryType(memRequirements.memoryTypeBits, properties);
222 allocInfo.memoryTypeIndex = memoryType;
223 allocInfo.pNext = &exportAllocInfo;
225 if (vkAllocateMemory(m_device, &allocInfo,
nullptr, &imageWrapper->textureImageMemory) != VK_SUCCESS) {
226 qCritical(
"VulkanWrapper: failed to allocate image memory!");
230 int res = vkBindImageMemory(m_device, image, imageWrapper->textureImageMemory, 0);
232 if (
vwExtraDebug) qDebug() <<
"vkBindImageMemory res" << res;
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;
239 res = vkGetMemoryFdKHR(m_device, &memoryFdInfo, &imageWrapper->imgFd);
240 if (
vwExtraDebug) qDebug() <<
"vkGetMemoryFdKHR res" << res <<
"fd" << imageWrapper->imgFd;
242 return imageWrapper.release();
246bool VulkanWrapperPrivate::transitionImageLayout(VkImage image, VkFormat , VkImageLayout oldLayout, VkImageLayout newLayout)
248 VkCommandBuffer commandBuffer = beginSingleTimeCommands();
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;
263 VkPipelineStageFlags sourceStage;
264 VkPipelineStageFlags destinationStage;
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;
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;
276 sourceStage = VK_PIPELINE_STAGE_TRANSFER_BIT;
277 destinationStage = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;
279 qCritical(
"VulkanWrapper: unsupported layout transition!");
283 vkCmdPipelineBarrier(
285 sourceStage, destinationStage,
292 endSingleTimeCommands(commandBuffer);
296bool VulkanWrapperPrivate::createBuffer(VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties, VkBuffer& buffer, VkDeviceMemory& bufferMemory)
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;
304 if (vkCreateBuffer(m_device, &bufferInfo,
nullptr, &buffer) != VK_SUCCESS) {
305 qCritical(
"VulkanWrapper: failed to create buffer!");
309 VkMemoryRequirements memRequirements;
310 vkGetBufferMemoryRequirements(m_device, buffer, &memRequirements);
312 VkMemoryAllocateInfo allocInfo = {};
313 allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
314 allocInfo.allocationSize = memRequirements.size;
315 allocInfo.memoryTypeIndex = findMemoryType(memRequirements.memoryTypeBits, properties);
317 if (vkAllocateMemory(m_device, &allocInfo,
nullptr, &bufferMemory) != VK_SUCCESS) {
318 qCritical(
"VulkanWrapper: failed to allocate buffer memory!");
322 vkBindBufferMemory(m_device, buffer, bufferMemory, 0);
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;
337 VkCommandBuffer commandBuffer;
338 int res = vkAllocateCommandBuffers(m_device, &allocInfo, &commandBuffer);
340 if (
vwExtraDebug) qDebug() <<
"vkAllocateCommandBuffers res" << res;
342 VkCommandBufferBeginInfo beginInfo = {};
343 beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
344 beginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
346 res = vkBeginCommandBuffer(commandBuffer, &beginInfo);
349 return commandBuffer;
354 int res = vkEndCommandBuffer(commandBuffer);
358 VkSubmitInfo submitInfo = {};
359 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
360 submitInfo.commandBufferCount = 1;
361 submitInfo.pCommandBuffers = &commandBuffer;
363 vkQueueSubmit(m_graphicsQueue, 1, &submitInfo, VK_NULL_HANDLE);
364 vkQueueWaitIdle(m_graphicsQueue);
366 vkFreeCommandBuffers(m_device, m_commandPool, 1, &commandBuffer);
369void VulkanWrapperPrivate::copyBufferToImage(VkBuffer buffer, VkImage image, uint32_t width, uint32_t height)
371 VkCommandBuffer commandBuffer = beginSingleTimeCommands();
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 = {
388 vkCmdCopyBufferToImage(commandBuffer, buffer, image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, ®ion);
390 endSingleTimeCommands(commandBuffer);
397 VkCommandPoolCreateInfo poolInfo = {};
398 poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
401 if (vkCreateCommandPool(m_device, &poolInfo,
nullptr, &m_commandPool) != VK_SUCCESS) {
403 qCritical(
"VulkanWrapperPrivate: could not create command pool");
411 uint32_t queueFamilyCount = 0;
412 vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount,
nullptr);
413 if (
vwExtraDebug) qDebug() <<
"queueFamilyCount" << queueFamilyCount;
416 std::vector<VkQueueFamilyProperties> queueFamilies(queueFamilyCount);
417 vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, queueFamilies.data());
419#ifdef VULKAN_SERVER_BUFFER_EXTRA_DEBUG
420 for (
const auto& queueFamily : queueFamilies) {
421 qDebug() <<
"....q" <<
"count" << queueFamily.queueCount << queueFamily.timestampValidBits << hex << queueFamily.queueFlags;
426 for (
const auto& queueFamily : queueFamilies) {
427 if (queueFamily.queueCount > 0 && queueFamily.queueFlags & VK_QUEUE_GRAPHICS_BIT) {
428 indices.graphicsFamily = i;
441 std::vector<VkDeviceQueueCreateInfo> queueCreateInfos;
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);
454 VkPhysicalDeviceFeatures deviceFeatures = {};
456 VkDeviceCreateInfo createInfo = {};
457 createInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
459 createInfo.queueCreateInfoCount =
static_cast<uint32_t>(queueCreateInfos.size());
460 createInfo.pQueueCreateInfos = queueCreateInfos.data();
462 createInfo.pEnabledFeatures = &deviceFeatures;
464 if (vkCreateDevice(m_physicalDevice, &createInfo,
nullptr, &m_device) != VK_SUCCESS) {
465 qCritical(
"VulkanWrapper: failed to create logical device!");
469 vkGetDeviceQueue(m_device, indices.graphicsFamily, 0, &m_graphicsQueue);
475 return createTextureImageFromData(img.constBits(), img.sizeInBytes(), img.size(), VK_FORMAT_R8G8B8A8_UNORM);
483 int texWidth = size.width();
484 int texHeight = size.height();
486 if (
vwExtraDebug) qDebug(
"image load %p %dx%d", pixels, texWidth, texHeight);
488 qCritical(
"VulkanWrapper: failed to load texture image!");
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);
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);
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));
513 const VkImage textureImage = imageWrapper->textureImage;
515 ok = transitionImageLayout(textureImage, vkFormat, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
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);
524 vkDestroyBuffer(m_device, stagingBuffer,
nullptr);
525 vkFreeMemory(m_device, stagingBufferMemory,
nullptr);
527 return imageWrapper.release();
539 vkDestroyImage(m_device, imageWrapper->textureImage,
nullptr);
540 vkFreeMemory(m_device, imageWrapper->textureImageMemory,
nullptr);
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);
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;
565 auto f_glGetVkProcAddrNV =
reinterpret_cast<PFNGLGETVKPROCADDRNVPROC>(glContext->getProcAddress(
"glGetVkProcAddrNV"));
567 if (!f_glGetVkProcAddrNV) {
568 qCritical(
"VulkanWrapper: Could not find Vulkan/GL interop function glGetVkProcAddrNV");
573 initFunctions(f_glGetVkProcAddrNV);
575 VkResult instanceCreationResult = vkCreateInstance(&instanceCreateInfo,
nullptr, &m_instance);
577 if (
vwExtraDebug) qDebug() <<
"result" << instanceCreationResult;
579 if (instanceCreationResult != VK_SUCCESS) {
580 qCritical() <<
"VulkanWrapper: Failed to create Vulkan instance: Error "
581 << instanceCreationResult;
588 auto res = vkEnumeratePhysicalDevices(m_instance, &devCount,
nullptr);
589 if (
vwExtraDebug) qDebug() <<
"vkEnumeratePhysicalDevices res =" << res <<
"count =" << devCount;
591 QVarLengthArray<VkPhysicalDevice, 5> dev(devCount);
593 res = vkEnumeratePhysicalDevices(m_instance, &devCount, dev.data());
594 if (
vwExtraDebug) qDebug() <<
"...devs res =" << res <<
"count =" << devCount;
596#ifdef VULKAN_SERVER_BUFFER_EXTRA_DEBUG
597 VkPhysicalDeviceProperties props;
599 vkGetPhysicalDeviceProperties(dev[0], &props);
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;
610 m_physicalDevice = dev[0];
612 bool ok = createLogicalDevice();
614 qCritical(
"VulkanWrapperPrivate: could not create logical device");
619 VkPhysicalDeviceMemoryProperties memProps;
622 vkGetPhysicalDeviceMemoryProperties(dev[0], &memProps);
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;
629 for (uint i = 0; i < memProps.memoryHeapCount; ++i)
630 qDebug() <<
" " << i <<
"size" << memProps.memoryHeaps[i].size <<
"flags" << hex << memProps.memoryHeaps[i].flags;
633 int gpuMemoryType = -1;
635 for (uint i = 0; i < memProps.memoryTypeCount; ++i) {
636 if (memProps.memoryTypes[i].propertyFlags & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) {
642 if (gpuMemoryType < 0) {
643 qCritical(
"VulkanWrapper: Could not find GPU memory!");
648#ifdef VULKAN_SERVER_BUFFER_EXTRA_DEBUG
649 qDebug() <<
"GPU memory type:" << gpuMemoryType <<
"heap:" << memProps.memoryTypes[gpuMemoryType].heapIndex;
651 for (
int f = 0; f <= VK_FORMAT_ASTC_12x12_SRGB_BLOCK; f++)
653 VkFormatProperties formatProps;
654 vkGetPhysicalDeviceFormatProperties(dev[0], VkFormat(f), &formatProps);
655 qDebug() <<
"format" << f <<
"features" << hex << formatProps.linearTilingFeatures << formatProps.optimalTilingFeatures << formatProps.bufferFeatures;
674 VkFormat vkFormat = VkFormat(QVkConvenience::vkFormatFromGlFormat(glInternalFormat));
675 if (vkFormat == VK_FORMAT_UNDEFINED)
678 return d_ptr->createTextureImageFromData(pixels, bufferSize, size, vkFormat);
686 *w = imgWrapper->imgSize.width();
688 *h = imgWrapper->imgSize.height();
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)