Project
Loading...
Searching...
No Matches
GPUDisplayBackendVulkan.cxx
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
14
15#define VULKAN_HPP_DISPATCH_LOADER_DYNAMIC 1
16#include <vulkan/vulkan.hpp>
17VULKAN_HPP_DEFAULT_DISPATCH_LOADER_DYNAMIC_STORAGE
18
19#include "GPUCommonDef.h"
21#include "GPUDisplay.h"
22#include "GPULogging.h"
23#include "GPUParam.h"
24
25#include <mutex>
26
27using namespace o2::gpu;
28
30QGET_LD_BINARY_SYMBOLS(shaders_shaders_vertex_vert_spv);
31QGET_LD_BINARY_SYMBOLS(shaders_shaders_fragment_frag_spv);
32QGET_LD_BINARY_SYMBOLS(shaders_shaders_vertexPoint_vert_spv);
33QGET_LD_BINARY_SYMBOLS(shaders_shaders_vertexTexture_vert_spv);
34QGET_LD_BINARY_SYMBOLS(shaders_shaders_fragmentTexture_frag_spv);
35QGET_LD_BINARY_SYMBOLS(shaders_shaders_fragmentText_frag_spv);
36
37// #define CHKERR(cmd) {cmd;}
38#define CHKERR(cmd) \
39 do { \
40 auto tmp_internal_retVal = cmd; \
41 if ((int32_t)tmp_internal_retVal < 0) { \
42 GPUError("VULKAN ERROR: %d: %s (%s: %d)", (int32_t)tmp_internal_retVal, "ERROR", __FILE__, __LINE__); \
43 throw std::runtime_error("Vulkan Failure"); \
44 } \
45 } while (false)
46
53
54// ---------------------------- VULKAN HELPERS ----------------------------
55
56static int32_t checkVulkanLayersSupported(const std::vector<const char*>& validationLayers)
57{
58 std::vector<vk::LayerProperties> availableLayers = vk::enumerateInstanceLayerProperties();
59 for (const char* layerName : validationLayers) {
60 bool layerFound = false;
61
62 for (const auto& layerProperties : availableLayers) {
63 if (strcmp(layerName, layerProperties.layerName) == 0) {
64 layerFound = true;
65 break;
66 }
67 }
68
69 if (!layerFound) {
70 return 1;
71 }
72 }
73 return 0;
74}
75
76static uint32_t findMemoryType(uint32_t typeFilter, vk::MemoryPropertyFlags properties, vk::PhysicalDevice physDev)
77{
78 vk::PhysicalDeviceMemoryProperties memProperties = physDev.getMemoryProperties();
79
80 for (uint32_t i = 0; i < memProperties.memoryTypeCount; i++) {
81 if ((typeFilter & (1 << i)) && (memProperties.memoryTypes[i].propertyFlags & properties) == properties) {
82 return i;
83 }
84 }
85
86 throw std::runtime_error("failed to find suitable memory type!");
87}
88
89static vk::SurfaceFormatKHR chooseSwapSurfaceFormat(const std::vector<vk::SurfaceFormatKHR>& availableFormats)
90{
91 for (const auto& availableFormat : availableFormats) {
92 if (availableFormat.format == vk::Format::eB8G8R8A8Unorm && availableFormat.colorSpace == vk::ColorSpaceKHR::eSrgbNonlinear) {
93 return availableFormat;
94 }
95 }
96 return availableFormats[0];
97}
98
99static vk::PresentModeKHR chooseSwapPresentMode(const std::vector<vk::PresentModeKHR>& availablePresentModes, vk::PresentModeKHR desiredMode = vk::PresentModeKHR::eMailbox)
100{
101 for (const auto& availablePresentMode : availablePresentModes) {
102 if (availablePresentMode == desiredMode) {
103 return availablePresentMode;
104 }
105 }
106 static bool errorShown = false;
107 if (!errorShown) {
108 errorShown = true;
109 GPUError("VULKAN ERROR: Desired present mode not available, using FIFO mode");
110 }
111 return vk::PresentModeKHR::eFifo;
112}
113
114vk::Extent2D GPUDisplayBackendVulkan::chooseSwapExtent(const vk::SurfaceCapabilitiesKHR& capabilities)
115{
116 if (capabilities.currentExtent.width != std::numeric_limits<uint32_t>::max()) {
117 return capabilities.currentExtent;
118 } else {
119 int32_t width, height;
121 vk::Extent2D actualExtent = {(uint32_t)width, (uint32_t)height};
122 actualExtent.width = std::clamp(actualExtent.width, capabilities.minImageExtent.width, capabilities.maxImageExtent.width);
123 actualExtent.height = std::clamp(actualExtent.height, capabilities.minImageExtent.height, capabilities.maxImageExtent.height);
124 return actualExtent;
125 }
126}
127
128static vk::ShaderModule createShaderModule(const char* code, size_t size, vk::Device device)
129{
130 vk::ShaderModuleCreateInfo createInfo{};
131 createInfo.codeSize = size;
132 createInfo.pCode = reinterpret_cast<const uint32_t*>(code);
133 return device.createShaderModule(createInfo, nullptr);
134}
135
136static void cmdImageMemoryBarrier(vk::CommandBuffer cmdbuffer, vk::Image image, vk::AccessFlags srcAccessMask, vk::AccessFlags dstAccessMask, vk::ImageLayout oldLayout, vk::ImageLayout newLayout, vk::PipelineStageFlags srcStageMask, vk::PipelineStageFlags dstStageMask)
137{
138 vk::ImageSubresourceRange range{vk::ImageAspectFlagBits::eColor, 0, 1, 0, 1};
139 vk::ImageMemoryBarrier barrier{};
140 barrier.srcAccessMask = srcAccessMask;
141 barrier.dstAccessMask = dstAccessMask;
142 barrier.oldLayout = oldLayout;
143 barrier.newLayout = newLayout;
144 barrier.image = image;
145 barrier.subresourceRange = range;
146 cmdbuffer.pipelineBarrier(srcStageMask, dstStageMask, {}, 0, nullptr, 0, nullptr, 1, &barrier);
147}
148
149void GPUDisplayBackendVulkan::updateSwapChainDetails(const vk::PhysicalDevice& device)
150{
151 mSwapChainDetails.capabilities = device.getSurfaceCapabilitiesKHR(mSurface);
152 mSwapChainDetails.formats = device.getSurfaceFormatsKHR(mSurface);
153 mSwapChainDetails.presentModes = device.getSurfacePresentModesKHR(mSurface);
154}
155
157{
158 vk::CommandBufferAllocateInfo allocInfo{};
159 allocInfo.level = vk::CommandBufferLevel::ePrimary;
160 allocInfo.commandPool = mCommandPool;
161 allocInfo.commandBufferCount = 1;
162 vk::CommandBuffer commandBuffer = mDevice.allocateCommandBuffers(allocInfo)[0];
163 vk::CommandBufferBeginInfo beginInfo{};
164 beginInfo.flags = vk::CommandBufferUsageFlagBits::eOneTimeSubmit;
165 commandBuffer.begin(beginInfo);
166 return commandBuffer;
167}
168
170{
171 commandBuffer.end();
172 vk::SubmitInfo submitInfo{};
173 submitInfo.commandBufferCount = 1;
174 submitInfo.pCommandBuffers = &commandBuffer;
175 static std::mutex fenceMutex;
176 {
177 std::lock_guard<std::mutex> guard(fenceMutex);
178 CHKERR(mGraphicsQueue.submit(1, &submitInfo, mSingleCommitFence));
179 CHKERR(mDevice.waitForFences(1, &mSingleCommitFence, true, UINT64_MAX));
180 CHKERR(mDevice.resetFences(1, &mSingleCommitFence));
181 }
182 mDevice.freeCommandBuffers(mCommandPool, 1, &commandBuffer);
183}
184
185static vk::ImageView createImageViewI(vk::Device device, vk::Image image, vk::Format format, vk::ImageAspectFlags aspectFlags = vk::ImageAspectFlagBits::eColor, uint32_t mipLevels = 1)
186{
187 vk::ImageViewCreateInfo viewInfo{};
188 viewInfo.image = image;
189 viewInfo.viewType = vk::ImageViewType::e2D;
190 viewInfo.format = format;
191 viewInfo.subresourceRange.aspectMask = aspectFlags;
192 viewInfo.subresourceRange.baseMipLevel = 0;
193 viewInfo.subresourceRange.levelCount = mipLevels;
194 viewInfo.subresourceRange.baseArrayLayer = 0;
195 viewInfo.subresourceRange.layerCount = 1;
196 return device.createImageView(viewInfo, nullptr);
197}
198
199static void createImageI(vk::Device device, vk::PhysicalDevice physicalDevice, vk::Image& image, vk::DeviceMemory& imageMemory, uint32_t width, uint32_t height, vk::Format format, vk::ImageUsageFlags usage, vk::MemoryPropertyFlags properties, vk::ImageTiling tiling = vk::ImageTiling::eOptimal, vk::SampleCountFlagBits numSamples = vk::SampleCountFlagBits::e1, vk::ImageLayout layout = vk::ImageLayout::eUndefined, uint32_t mipLevels = 1)
200{
201 vk::ImageCreateInfo imageInfo{};
202 imageInfo.imageType = vk::ImageType::e2D;
203 imageInfo.extent.width = width;
204 imageInfo.extent.height = height;
205 imageInfo.extent.depth = 1;
206 imageInfo.mipLevels = mipLevels;
207 imageInfo.arrayLayers = 1;
208 imageInfo.format = format;
209 imageInfo.tiling = tiling;
210 imageInfo.initialLayout = layout;
211 imageInfo.usage = usage;
212 imageInfo.samples = numSamples;
213 imageInfo.sharingMode = vk::SharingMode::eExclusive;
214 image = device.createImage(imageInfo);
215
216 vk::MemoryRequirements memRequirements;
217 memRequirements = device.getImageMemoryRequirements(image);
218
219 vk::MemoryAllocateInfo allocInfo{};
220 allocInfo.allocationSize = memRequirements.size;
221 allocInfo.memoryTypeIndex = findMemoryType(memRequirements.memoryTypeBits, properties, physicalDevice);
222 imageMemory = device.allocateMemory(allocInfo, nullptr);
223
224 device.bindImageMemory(image, imageMemory, 0);
225}
226
227static uint32_t getMaxUsableSampleCount(vk::PhysicalDeviceProperties& physicalDeviceProperties)
228{
229 vk::SampleCountFlags counts = physicalDeviceProperties.limits.framebufferColorSampleCounts & physicalDeviceProperties.limits.framebufferDepthSampleCounts;
230 if (counts & vk::SampleCountFlagBits::e64) {
231 return 64;
232 } else if (counts & vk::SampleCountFlagBits::e32) {
233 return 32;
234 } else if (counts & vk::SampleCountFlagBits::e16) {
235 return 16;
236 } else if (counts & vk::SampleCountFlagBits::e8) {
237 return 8;
238 } else if (counts & vk::SampleCountFlagBits::e4) {
239 return 4;
240 } else if (counts & vk::SampleCountFlagBits::e2) {
241 return 2;
242 }
243 return 1;
244}
245
246static vk::SampleCountFlagBits getMSAASamplesFlag(uint32_t msaa)
247{
248 if (msaa == 2) {
249 return vk::SampleCountFlagBits::e2;
250 } else if (msaa == 4) {
251 return vk::SampleCountFlagBits::e4;
252 } else if (msaa == 8) {
253 return vk::SampleCountFlagBits::e8;
254 } else if (msaa == 16) {
255 return vk::SampleCountFlagBits::e16;
256 } else if (msaa == 32) {
257 return vk::SampleCountFlagBits::e32;
258 } else if (msaa == 64) {
259 return vk::SampleCountFlagBits::e64;
260 }
261 return vk::SampleCountFlagBits::e1;
262}
263
264template <class T, class S>
265static inline void clearVector(T& v, S func, bool downsize = true)
266{
267 std::for_each(v.begin(), v.end(), func);
268 if (downsize) {
269 v.clear();
270 }
271}
272
273// ---------------------------- VULKAN DEVICE MANAGEMENT ----------------------------
274
275double GPUDisplayBackendVulkan::checkDevice(vk::PhysicalDevice device, const std::vector<const char*>& reqDeviceExtensions)
276{
277 double score = -1.;
278 vk::PhysicalDeviceProperties deviceProperties = device.getProperties();
279 vk::PhysicalDeviceFeatures deviceFeatures = device.getFeatures();
280 vk::PhysicalDeviceMemoryProperties memoryProperties = device.getMemoryProperties();
281 if (!deviceFeatures.geometryShader || !deviceFeatures.wideLines || !deviceFeatures.largePoints) {
282 return (-1);
283 }
284
285 std::vector<vk::QueueFamilyProperties> queueFamilies = device.getQueueFamilyProperties();
286 bool found = false;
287 for (uint32_t i = 0; i < queueFamilies.size(); i++) {
288 if (!(queueFamilies[i].queueFlags & vk::QueueFlagBits::eGraphics)) {
289 return (-1);
290 }
291 vk::Bool32 presentSupport = device.getSurfaceSupportKHR(i, mSurface);
292 if (!presentSupport) {
293 return (-1);
294 }
296 found = true;
297 break;
298 }
299 if (!found) {
300 GPUInfo("%s ignored due to missing queue properties", &deviceProperties.deviceName[0]);
301 return (-1);
302 }
303
304 std::vector<vk::ExtensionProperties> availableExtensions = device.enumerateDeviceExtensionProperties(nullptr);
305 uint32_t extensionsFound = 0;
306 for (uint32_t i = 0; i < reqDeviceExtensions.size(); i++) {
307 for (uint32_t j = 0; j < availableExtensions.size(); j++) {
308 if (strcmp(reqDeviceExtensions[i], availableExtensions[j].extensionName) == 0) {
309 extensionsFound++;
310 break;
311 }
312 }
313 }
314 if (extensionsFound < reqDeviceExtensions.size()) {
315 GPUInfo("%s ignored due to missing extensions", &deviceProperties.deviceName[0]);
316 return (-1);
317 }
318
321 GPUInfo("%s ignored due to incompatible swap chain", &deviceProperties.deviceName[0]);
322 return (-1);
323 }
324
325 score = 1;
326 if (deviceProperties.deviceType == vk::PhysicalDeviceType::eDiscreteGpu) {
327 score += 1e12;
328 } else if (deviceProperties.deviceType == vk::PhysicalDeviceType::eIntegratedGpu) {
329 score += 1e11;
330 }
331
332 for (uint32_t i = 0; i < memoryProperties.memoryHeapCount; i++) {
333 if (memoryProperties.memoryHeaps[i].flags & vk::MemoryHeapFlagBits::eDeviceLocal) {
334 score += memoryProperties.memoryHeaps[i].size;
335 }
336 }
337
338 return score;
339}
340
342{
343 VULKAN_HPP_DEFAULT_DISPATCHER.init();
344 vk::ApplicationInfo appInfo{};
345 appInfo.pApplicationName = "GPU CA Standalone display";
346 appInfo.applicationVersion = VK_MAKE_VERSION(1, 0, 0);
347 appInfo.pEngineName = "GPU CI Standalone Engine";
348 appInfo.engineVersion = VK_MAKE_VERSION(1, 0, 0);
349 appInfo.apiVersion = VK_API_VERSION_1_0;
350
351 vk::InstanceCreateInfo instanceCreateInfo;
352 instanceCreateInfo.pApplicationInfo = &appInfo;
353
354 const char** frontendExtensions;
355 uint32_t frontendExtensionCount = mDisplay->frontend()->getReqVulkanExtensions(frontendExtensions);
356 std::vector<const char*> reqInstanceExtensions(frontendExtensions, frontendExtensions + frontendExtensionCount);
357
358 const std::vector<const char*> reqValidationLayers = {
359 "VK_LAYER_KHRONOS_validation"};
360 auto debugCallback = [](vk::DebugUtilsMessageSeverityFlagBitsEXT messageSeverity, vk::DebugUtilsMessageTypeFlagsEXT messageType, const vk::DebugUtilsMessengerCallbackDataEXT* pCallbackData, void* pUserData) -> VkBool32 {
361 static int32_t throwOnError = getenv("GPUCA_VULKAN_VALIDATION_THROW") ? atoi(getenv("GPUCA_VULKAN_VALIDATION_THROW")) : 0;
362 static bool showVulkanValidationInfo = getenv("GPUCA_VULKAN_VALIDATION_INFO") && atoi(getenv("GPUCA_VULKAN_VALIDATION_INFO"));
363 switch (messageSeverity) {
364 case vk::DebugUtilsMessageSeverityFlagBitsEXT::eVerbose:
365 if (showVulkanValidationInfo) {
366 GPUInfo("%s", pCallbackData->pMessage);
367 }
368 break;
369 case vk::DebugUtilsMessageSeverityFlagBitsEXT::eWarning:
370 GPUWarning("%s", pCallbackData->pMessage);
371 if (throwOnError > 1) {
372 throw std::logic_error("break_on_validation_warning");
373 }
374 break;
375 case vk::DebugUtilsMessageSeverityFlagBitsEXT::eError:
376 GPUError("%s", pCallbackData->pMessage);
377 if (throwOnError) {
378 throw std::logic_error("break_on_validation_error");
379 }
380 break;
381 case vk::DebugUtilsMessageSeverityFlagBitsEXT::eInfo:
382 default:
383 GPUInfo("%s", pCallbackData->pMessage);
384 break;
385 }
386 return false;
387 };
388 vk::DebugUtilsMessengerCreateInfoEXT debugCreateInfo{};
390 if (checkVulkanLayersSupported(reqValidationLayers)) {
391 throw std::runtime_error("Requested validation layer support not available");
392 }
393 reqInstanceExtensions.push_back(VK_EXT_DEBUG_UTILS_EXTENSION_NAME);
394 instanceCreateInfo.enabledLayerCount = static_cast<uint32_t>(reqValidationLayers.size());
395 instanceCreateInfo.ppEnabledLayerNames = reqValidationLayers.data();
396 instanceCreateInfo.pNext = (VkDebugUtilsMessengerCreateInfoEXT*)&debugCreateInfo;
397
398 debugCreateInfo.messageSeverity = vk::DebugUtilsMessageSeverityFlagBitsEXT::eVerbose | vk::DebugUtilsMessageSeverityFlagBitsEXT::eWarning | vk::DebugUtilsMessageSeverityFlagBitsEXT::eError;
399 debugCreateInfo.messageType = vk::DebugUtilsMessageTypeFlagBitsEXT::eGeneral | vk::DebugUtilsMessageTypeFlagBitsEXT::eValidation | vk::DebugUtilsMessageTypeFlagBitsEXT::ePerformance;
400 debugCreateInfo.pfnUserCallback = debugCallback;
401 debugCreateInfo.pUserData = nullptr;
402 } else {
403 instanceCreateInfo.enabledLayerCount = 0;
404 }
405
406 instanceCreateInfo.enabledExtensionCount = static_cast<uint32_t>(reqInstanceExtensions.size());
407 instanceCreateInfo.ppEnabledExtensionNames = reqInstanceExtensions.data();
408
409 mInstance = vk::createInstance(instanceCreateInfo, nullptr);
410 VULKAN_HPP_DEFAULT_DISPATCHER.init(mInstance);
411
413 GPUInfo("Enabling Vulkan Validation Layers");
414 mDebugMessenger = mInstance.createDebugUtilsMessengerEXT(debugCreateInfo, nullptr);
415 }
416 std::vector<vk::ExtensionProperties> extensions = vk::enumerateInstanceExtensionProperties(nullptr);
417 if (mDisplay->param()->par.debugLevel >= 3) {
418 std::cout << "available instance extensions: " << extensions.size() << "\n";
419 for (const auto& extension : extensions) {
420 std::cout << '\t' << extension.extensionName << '\n';
421 }
422 }
423
425 throw std::runtime_error("Frontend does not provide Vulkan surface");
426 }
427
428 const std::vector<const char*> reqDeviceExtensions = {
429 VK_KHR_SWAPCHAIN_EXTENSION_NAME};
430
431 mPhysicalDevice = VkPhysicalDevice(VK_NULL_HANDLE);
432 std::vector<vk::PhysicalDevice> devices = mInstance.enumeratePhysicalDevices();
433 if (devices.size() == 0) {
434 throw std::runtime_error("No Vulkan device present!");
435 }
436 double bestScore = -1.;
437 for (uint32_t i = 0; i < devices.size(); i++) {
438 double score = checkDevice(devices[i], reqDeviceExtensions);
439 if (mDisplay->param()->par.debugLevel >= 2) {
440 vk::PhysicalDeviceProperties deviceProperties = devices[i].getProperties();
441 GPUInfo("Available Vulkan device %d: %s - Score %f", i, &deviceProperties.deviceName[0], score);
442 }
443 if (score > bestScore && score > 0) {
444 mPhysicalDevice = devices[i];
445 bestScore = score;
446 }
447 }
448 if (mDisplay->cfg().vulkan.forceDevice != -1) {
449 if (mDisplay->cfg().vulkan.forceDevice < 0 || mDisplay->cfg().vulkan.forceDevice >= (int32_t)devices.size()) {
450 throw std::runtime_error("Invalid Vulkan device selected");
451 }
452 mPhysicalDevice = devices[mDisplay->cfg().vulkan.forceDevice];
453 }
454 if (!mPhysicalDevice) {
455 throw std::runtime_error("All available Vulkan devices unsuited");
456 }
457
459 vk::PhysicalDeviceProperties deviceProperties = mPhysicalDevice.getProperties();
460 vk::PhysicalDeviceFeatures deviceFeatures = mPhysicalDevice.getFeatures();
461 vk::FormatProperties depth32FormatProperties = mPhysicalDevice.getFormatProperties(vk::Format::eD32Sfloat);
462 vk::FormatProperties depth64FormatProperties = mPhysicalDevice.getFormatProperties(vk::Format::eD32SfloatS8Uint);
463 vk::FormatProperties formatProperties = mPhysicalDevice.getFormatProperties(mSurfaceFormat.format);
464 GPUInfo("Using physical Vulkan device %s", &deviceProperties.deviceName[0]);
465 mMaxMSAAsupported = getMaxUsableSampleCount(deviceProperties);
466 mZSupported = (bool)(depth32FormatProperties.optimalTilingFeatures & vk::FormatFeatureFlagBits::eDepthStencilAttachment);
467 mStencilSupported = (bool)(depth64FormatProperties.optimalTilingFeatures & vk::FormatFeatureFlagBits::eDepthStencilAttachment);
468 mCubicFilterSupported = (bool)(formatProperties.optimalTilingFeatures & vk::FormatFeatureFlagBits::eSampledImageFilterCubicEXT);
469 bool mailboxSupported = std::find(mSwapChainDetails.presentModes.begin(), mSwapChainDetails.presentModes.end(), vk::PresentModeKHR::eMailbox) != mSwapChainDetails.presentModes.end();
470 if (mDisplay->param()->par.debugLevel >= 2) {
471 GPUInfo("Max MSAA: %d, 32 bit Z buffer %d, 32 bit Z buffer + stencil buffer %d, Cubic Filtering %d, Mailbox present mode %d\n", (int32_t)mMaxMSAAsupported, (int32_t)mZSupported, (int32_t)mStencilSupported, (int32_t)mCubicFilterSupported, (int32_t)mailboxSupported);
472 }
473
474 vk::DeviceQueueCreateInfo queueCreateInfo{};
475 queueCreateInfo.queueFamilyIndex = mGraphicsFamily;
476 queueCreateInfo.queueCount = 1;
477 float queuePriority = 1.0f;
478 queueCreateInfo.pQueuePriorities = &queuePriority;
479 vk::DeviceCreateInfo deviceCreateInfo{};
480 deviceCreateInfo.pQueueCreateInfos = &queueCreateInfo;
481 deviceCreateInfo.queueCreateInfoCount = 1;
482 deviceCreateInfo.pEnabledFeatures = &deviceFeatures;
483 deviceCreateInfo.enabledExtensionCount = static_cast<uint32_t>(reqDeviceExtensions.size());
484 deviceCreateInfo.ppEnabledExtensionNames = reqDeviceExtensions.data();
485 deviceCreateInfo.enabledLayerCount = instanceCreateInfo.enabledLayerCount;
486 deviceCreateInfo.ppEnabledLayerNames = instanceCreateInfo.ppEnabledLayerNames;
487 mDevice = mPhysicalDevice.createDevice(deviceCreateInfo, nullptr);
488 VULKAN_HPP_DEFAULT_DISPATCHER.init(mDevice);
490
491 vk::CommandPoolCreateInfo poolInfo{};
492 poolInfo.flags = vk::CommandPoolCreateFlagBits::eResetCommandBuffer;
493 poolInfo.queueFamilyIndex = mGraphicsFamily;
494 mCommandPool = mDevice.createCommandPool(poolInfo, nullptr);
495}
496
498{
499 mDevice.destroyCommandPool(mCommandPool, nullptr);
500 mDevice.destroy(nullptr);
501 mInstance.destroySurfaceKHR(mSurface, nullptr);
503 mInstance.destroyDebugUtilsMessengerEXT(mDebugMessenger, nullptr);
504 }
505}
506
507// ---------------------------- VULKAN COMMAND BUFFERS ----------------------------
508
510{
511 vk::CommandBufferAllocateInfo allocInfo{};
512 allocInfo.commandPool = mCommandPool;
513 allocInfo.level = vk::CommandBufferLevel::ePrimary;
514 allocInfo.commandBufferCount = mFramesInFlight;
516 mCommandBuffers = mDevice.allocateCommandBuffers(allocInfo);
517 mCommandBuffersText = mDevice.allocateCommandBuffers(allocInfo);
518 mCommandBuffersTexture = mDevice.allocateCommandBuffers(allocInfo);
519 mCommandBuffersDownsample = mDevice.allocateCommandBuffers(allocInfo);
520 mCommandBuffersMix = mDevice.allocateCommandBuffers(allocInfo);
521}
522
524{
525 mDevice.freeCommandBuffers(mCommandPool, mCommandBuffers.size(), mCommandBuffers.data());
526 mDevice.freeCommandBuffers(mCommandPool, mCommandBuffersText.size(), mCommandBuffersText.data());
527 mDevice.freeCommandBuffers(mCommandPool, mCommandBuffersTexture.size(), mCommandBuffersTexture.data());
529 mDevice.freeCommandBuffers(mCommandPool, mCommandBuffersMix.size(), mCommandBuffersMix.data());
530}
531
532// ---------------------------- VULKAN SEMAPHORES AND FENCES ----------------------------
533
535{
536 vk::SemaphoreCreateInfo semaphoreInfo{};
537 vk::FenceCreateInfo fenceInfo{};
538 fenceInfo.flags = vk::FenceCreateFlagBits::eSignaled;
545 for (uint32_t i = 0; i < mFramesInFlight; i++) {
546 mImageAvailableSemaphore[i] = mDevice.createSemaphore(semaphoreInfo, nullptr);
547 mRenderFinishedSemaphore[i] = mDevice.createSemaphore(semaphoreInfo, nullptr);
548 mTextFinishedSemaphore[i] = mDevice.createSemaphore(semaphoreInfo, nullptr);
549 mMixFinishedSemaphore[i] = mDevice.createSemaphore(semaphoreInfo, nullptr);
550 mDownsampleFinishedSemaphore[i] = mDevice.createSemaphore(semaphoreInfo, nullptr);
551 mInFlightFence[i] = mDevice.createFence(fenceInfo, nullptr);
552 }
553 fenceInfo.flags = {};
554 mSingleCommitFence = mDevice.createFence(fenceInfo, nullptr);
555}
556
558{
559 clearVector(mImageAvailableSemaphore, [&](auto& x) { mDevice.destroySemaphore(x, nullptr); });
560 clearVector(mRenderFinishedSemaphore, [&](auto& x) { mDevice.destroySemaphore(x, nullptr); });
561 clearVector(mTextFinishedSemaphore, [&](auto& x) { mDevice.destroySemaphore(x, nullptr); });
562 clearVector(mMixFinishedSemaphore, [&](auto& x) { mDevice.destroySemaphore(x, nullptr); });
563 clearVector(mDownsampleFinishedSemaphore, [&](auto& x) { mDevice.destroySemaphore(x, nullptr); });
564 clearVector(mInFlightFence, [&](auto& x) { mDevice.destroyFence(x, nullptr); });
565 mDevice.destroyFence(mSingleCommitFence, nullptr);
566}
567
568// ---------------------------- VULKAN UNIFORM LAYOUTS AND BUFFERS ----------------------------
569
571{
572 for (int32_t j = 0; j < 3; j++) {
575 for (uint32_t i = 0; i < mFramesInFlight; i++) {
576 mUniformBuffersMat[j][i] = createBuffer(sizeof(hmm_mat4), nullptr, vk::BufferUsageFlagBits::eUniformBuffer, mDisplay->cfg().vulkan.uniformBuffersInDeviceMemory ? 2 : 0);
577 mUniformBuffersCol[j][i] = createBuffer(sizeof(float) * 4, nullptr, vk::BufferUsageFlagBits::eUniformBuffer, mDisplay->cfg().vulkan.uniformBuffersInDeviceMemory ? 2 : 0);
578 }
579 }
580
581 std::array<vk::DescriptorPoolSize, 2> poolSizes{};
582 poolSizes[0].type = vk::DescriptorType::eUniformBuffer;
583 poolSizes[0].descriptorCount = (uint32_t)mFramesInFlight * (2 * 3);
584 poolSizes[1].type = vk::DescriptorType::eCombinedImageSampler;
585 poolSizes[1].descriptorCount = (uint32_t)mFramesInFlight * 2;
586 vk::DescriptorPoolCreateInfo poolInfo{};
587 poolInfo.poolSizeCount = poolSizes.size();
588 poolInfo.pPoolSizes = poolSizes.data();
589 poolInfo.maxSets = (uint32_t)mFramesInFlight * 3;
590 mDescriptorPool = mDevice.createDescriptorPool(poolInfo, nullptr);
591
592 vk::DescriptorSetLayoutBinding uboLayoutBindingMat{};
593 uboLayoutBindingMat.binding = 0;
594 uboLayoutBindingMat.descriptorType = vk::DescriptorType::eUniformBuffer;
595 uboLayoutBindingMat.descriptorCount = 1;
596 uboLayoutBindingMat.stageFlags = vk::ShaderStageFlagBits::eVertex;
597 vk::DescriptorSetLayoutBinding uboLayoutBindingCol = uboLayoutBindingMat;
598 uboLayoutBindingCol.binding = 1;
599 uboLayoutBindingCol.stageFlags = vk::ShaderStageFlagBits::eFragment;
600 vk::DescriptorSetLayoutBinding samplerLayoutBinding{};
601 samplerLayoutBinding.binding = 2;
602 samplerLayoutBinding.descriptorCount = 1;
603 samplerLayoutBinding.descriptorType = vk::DescriptorType::eCombinedImageSampler;
604 samplerLayoutBinding.stageFlags = vk::ShaderStageFlagBits::eFragment;
605 vk::DescriptorSetLayoutBinding bindings[3] = {uboLayoutBindingMat, uboLayoutBindingCol, samplerLayoutBinding};
606
607 vk::DescriptorSetLayoutCreateInfo layoutInfo{};
608 layoutInfo.bindingCount = 2;
609 layoutInfo.pBindings = bindings;
610 mUniformDescriptor = mDevice.createDescriptorSetLayout(layoutInfo, nullptr);
611 layoutInfo.bindingCount = 3;
612 mUniformDescriptorTexture = mDevice.createDescriptorSetLayout(layoutInfo, nullptr);
613
614 vk::DescriptorSetAllocateInfo allocInfo{};
615 allocInfo.descriptorPool = mDescriptorPool;
616 allocInfo.descriptorSetCount = (uint32_t)mFramesInFlight;
617 for (int32_t j = 0; j < 3; j++) { // 0 = Render, 1 = Text, 2 = Texture
618 std::vector<vk::DescriptorSetLayout> layouts(mFramesInFlight, j ? mUniformDescriptorTexture : mUniformDescriptor);
619 allocInfo.pSetLayouts = layouts.data();
620 mDescriptorSets[j] = mDevice.allocateDescriptorSets(allocInfo);
621
622 for (int32_t k = 0; k < 2; k++) {
623 auto& mUniformBuffers = k ? mUniformBuffersCol[j] : mUniformBuffersMat[j];
624 for (uint32_t i = 0; i < mFramesInFlight; i++) {
625 vk::DescriptorBufferInfo bufferInfo{};
626 bufferInfo.buffer = mUniformBuffers[i].buffer;
627 bufferInfo.offset = 0;
628 bufferInfo.range = mUniformBuffers[i].size;
629
630 vk::WriteDescriptorSet descriptorWrite{};
631 descriptorWrite.dstSet = mDescriptorSets[j][i];
632 descriptorWrite.dstBinding = k;
633 descriptorWrite.dstArrayElement = 0;
634 descriptorWrite.descriptorType = vk::DescriptorType::eUniformBuffer;
635 descriptorWrite.descriptorCount = 1;
636 descriptorWrite.pBufferInfo = &bufferInfo;
637 descriptorWrite.pImageInfo = nullptr;
638 descriptorWrite.pTexelBufferView = nullptr;
639 mDevice.updateDescriptorSets(1, &descriptorWrite, 0, nullptr);
640 }
641 }
642 }
643
646 }
647}
648
650{
651 mDevice.destroyDescriptorSetLayout(mUniformDescriptor, nullptr);
652 mDevice.destroyDescriptorSetLayout(mUniformDescriptorTexture, nullptr);
653 mDevice.destroyDescriptorPool(mDescriptorPool, nullptr);
654 for (int32_t j = 0; j < 3; j++) {
655 clearVector(mUniformBuffersMat[j], [&](auto& x) { clearBuffer(x); });
656 clearVector(mUniformBuffersCol[j], [&](auto& x) { clearBuffer(x); });
657 }
658}
659
660void GPUDisplayBackendVulkan::setMixDescriptor(int32_t descriptorIndex, int32_t imageIndex)
661{
662 vk::DescriptorImageInfo imageInfo{};
663 imageInfo.imageLayout = vk::ImageLayout::eShaderReadOnlyOptimal;
664 imageInfo.sampler = mTextureSampler;
665 imageInfo.imageView = *mRenderTargetView[imageIndex + mImageCount];
666 vk::WriteDescriptorSet descriptorWrite{};
667 descriptorWrite.dstSet = mDescriptorSets[2][descriptorIndex];
668 descriptorWrite.dstBinding = 2;
669 descriptorWrite.dstArrayElement = 0;
670 descriptorWrite.descriptorType = vk::DescriptorType::eCombinedImageSampler;
671 descriptorWrite.descriptorCount = 1;
672 descriptorWrite.pImageInfo = &imageInfo;
673 mDevice.updateDescriptorSets(1, &descriptorWrite, 0, nullptr);
674}
675
676// ---------------------------- VULKAN TEXTURE SAMPLER ----------------------------
677
679{
680 vk::SamplerCreateInfo samplerInfo{};
681 samplerInfo.magFilter = vk::Filter::eLinear;
682 samplerInfo.minFilter = vk::Filter::eLinear;
683 samplerInfo.addressModeU = vk::SamplerAddressMode::eRepeat;
684 samplerInfo.addressModeV = vk::SamplerAddressMode::eRepeat;
685 samplerInfo.addressModeW = vk::SamplerAddressMode::eRepeat;
686 samplerInfo.compareEnable = false;
687 samplerInfo.compareOp = vk::CompareOp::eAlways;
688 samplerInfo.borderColor = vk::BorderColor::eIntOpaqueBlack;
689 samplerInfo.unnormalizedCoordinates = false;
690 samplerInfo.mipmapMode = vk::SamplerMipmapMode::eLinear;
691 samplerInfo.mipLodBias = 0.0f;
692 samplerInfo.minLod = 0.0f;
693 samplerInfo.maxLod = 0.0f;
694 mTextureSampler = mDevice.createSampler(samplerInfo, nullptr);
695}
696
698{
699 mDevice.destroySampler(mTextureSampler, nullptr);
700}
701
702// ---------------------------- VULKAN SWAPCHAIN MANAGEMENT ----------------------------
703
704void GPUDisplayBackendVulkan::createSwapChain(bool forScreenshot, bool forMixing)
705{
706 mDownsampleFactor = getDownsampleFactor(forScreenshot);
708 mSwapchainImageReadable = forScreenshot;
709
711 mSurfaceFormat = chooseSwapSurfaceFormat(mSwapChainDetails.formats);
712 mPresentMode = chooseSwapPresentMode(mSwapChainDetails.presentModes, mDisplay->cfgR().drawQualityVSync ? vk::PresentModeKHR::eMailbox : vk::PresentModeKHR::eImmediate);
713 vk::Extent2D extent = chooseSwapExtent(mSwapChainDetails.capabilities);
714
715 uint32_t imageCount = mSwapChainDetails.capabilities.minImageCount + 1;
716 if (mSwapChainDetails.capabilities.maxImageCount > 0 && imageCount > mSwapChainDetails.capabilities.maxImageCount) {
717 imageCount = mSwapChainDetails.capabilities.maxImageCount;
718 }
719
720 mScreenWidth = extent.width;
721 mScreenHeight = extent.height;
724
725 vk::SwapchainCreateInfoKHR swapCreateInfo{};
726 swapCreateInfo.surface = mSurface;
727 swapCreateInfo.minImageCount = imageCount;
728 swapCreateInfo.imageFormat = mSurfaceFormat.format;
729 swapCreateInfo.imageColorSpace = mSurfaceFormat.colorSpace;
730 swapCreateInfo.imageExtent = extent;
731 swapCreateInfo.imageArrayLayers = 1;
732 swapCreateInfo.imageUsage = vk::ImageUsageFlagBits::eColorAttachment;
733 swapCreateInfo.imageSharingMode = vk::SharingMode::eExclusive;
734 swapCreateInfo.queueFamilyIndexCount = 0; // Optional
735 swapCreateInfo.pQueueFamilyIndices = nullptr; // Optional
736 swapCreateInfo.preTransform = mSwapChainDetails.capabilities.currentTransform;
737 swapCreateInfo.compositeAlpha = vk::CompositeAlphaFlagBitsKHR::eOpaque;
738 swapCreateInfo.presentMode = mPresentMode;
739 swapCreateInfo.clipped = true;
740 swapCreateInfo.oldSwapchain = VkSwapchainKHR(VK_NULL_HANDLE);
742 swapCreateInfo.imageUsage |= vk::ImageUsageFlagBits::eTransferSrc;
743 }
744 if (mDownsampleFSAA) {
745 swapCreateInfo.imageUsage |= vk::ImageUsageFlagBits::eTransferDst;
746 }
747 mSwapChain = mDevice.createSwapchainKHR(swapCreateInfo, nullptr);
748
749 mSwapChainImages = mDevice.getSwapchainImagesKHR(mSwapChain);
750 uint32_t oldFramesInFlight = mFramesInFlight;
752 mFramesInFlight = mDisplay->cfg().vulkan.nFramesInFlight == 0 ? mImageCount : mDisplay->cfg().vulkan.nFramesInFlight;
754
755 if (mFramesInFlight > oldFramesInFlight || !mCommandInfrastructureCreated) {
760 }
765 }
766
768 for (uint32_t i = 0; i < mImageCount; i++) {
769 mSwapChainImageViews[i] = createImageViewI(mDevice, mSwapChainImages[i], mSurfaceFormat.format);
770 }
771}
772
774{
775 clearVector(mSwapChainImageViews, [&](auto& x) { mDevice.destroyImageView(x, nullptr); });
776 mDevice.destroySwapchainKHR(mSwapChain, nullptr);
777}
778
779void GPUDisplayBackendVulkan::recreateRendering(bool forScreenshot, bool forMixing)
780{
781 mDevice.waitIdle();
782 bool needUpdateSwapChain = mMustUpdateSwapChain || mDownsampleFactor != getDownsampleFactor(forScreenshot) || mSwapchainImageReadable != forScreenshot;
783 bool needUpdateOffscreenBuffers = needUpdateSwapChain || mMSAASampleCount != getMSAASamplesFlag(std::min<uint32_t>(mMaxMSAAsupported, mDisplay->cfgR().drawQualityMSAA)) || mZActive != (mZSupported && mDisplay->cfgL().depthBuffer) || mMixingSupported != forMixing;
785 if (needUpdateOffscreenBuffers) {
787 if (needUpdateSwapChain) {
789 createSwapChain(forScreenshot, forMixing);
790 }
791 createOffscreenBuffers(forScreenshot, forMixing);
792 }
795}
796
797// ---------------------------- VULKAN OFFSCREEN BUFFERS ----------------------------
798
799void GPUDisplayBackendVulkan::createOffscreenBuffers(bool forScreenshot, bool forMixing)
800{
801 mMSAASampleCount = getMSAASamplesFlag(std::min<uint32_t>(mMaxMSAAsupported, mDisplay->cfgR().drawQualityMSAA));
802 mZActive = mZSupported && mDisplay->cfgL().depthBuffer;
803 mMixingSupported = forMixing;
804
805 vk::AttachmentDescription colorAttachment{};
806 colorAttachment.format = mSurfaceFormat.format;
807 colorAttachment.samples = mMSAASampleCount;
808 colorAttachment.loadOp = vk::AttachmentLoadOp::eClear;
809 colorAttachment.storeOp = vk::AttachmentStoreOp::eStore;
810 colorAttachment.stencilLoadOp = vk::AttachmentLoadOp::eDontCare;
811 colorAttachment.stencilStoreOp = vk::AttachmentStoreOp::eDontCare;
812 colorAttachment.initialLayout = vk::ImageLayout::eUndefined;
813 colorAttachment.finalLayout = (mMSAASampleCount != vk::SampleCountFlagBits::e1 || mDownsampleFSAA) ? vk::ImageLayout::eColorAttachmentOptimal : vk::ImageLayout::ePresentSrcKHR;
814 vk::AttachmentDescription depthAttachment{};
815 depthAttachment.format = vk::Format::eD32Sfloat;
816 depthAttachment.samples = mMSAASampleCount;
817 depthAttachment.loadOp = vk::AttachmentLoadOp::eClear;
818 depthAttachment.storeOp = vk::AttachmentStoreOp::eDontCare;
819 depthAttachment.stencilLoadOp = vk::AttachmentLoadOp::eDontCare;
820 depthAttachment.stencilStoreOp = vk::AttachmentStoreOp::eDontCare;
821 depthAttachment.initialLayout = vk::ImageLayout::eUndefined;
822 depthAttachment.finalLayout = vk::ImageLayout::eDepthStencilAttachmentOptimal;
823 vk::AttachmentDescription colorAttachmentResolve{};
824 colorAttachmentResolve.format = mSurfaceFormat.format;
825 colorAttachmentResolve.samples = vk::SampleCountFlagBits::e1;
826 colorAttachmentResolve.loadOp = vk::AttachmentLoadOp::eDontCare;
827 colorAttachmentResolve.storeOp = vk::AttachmentStoreOp::eStore;
828 colorAttachmentResolve.stencilLoadOp = vk::AttachmentLoadOp::eDontCare;
829 colorAttachmentResolve.stencilStoreOp = vk::AttachmentStoreOp::eDontCare;
830 colorAttachmentResolve.initialLayout = vk::ImageLayout::eUndefined;
831 colorAttachmentResolve.finalLayout = mDownsampleFSAA ? vk::ImageLayout::eColorAttachmentOptimal : vk::ImageLayout::ePresentSrcKHR;
832 int32_t nAttachments = 0;
833 vk::AttachmentReference colorAttachmentRef{};
834 colorAttachmentRef.attachment = nAttachments++;
835 colorAttachmentRef.layout = vk::ImageLayout::eColorAttachmentOptimal;
836 vk::AttachmentReference depthAttachmentRef{};
837 // depthAttachmentRef.attachment // below
838 depthAttachmentRef.layout = vk::ImageLayout::eDepthStencilAttachmentOptimal;
839 vk::AttachmentReference colorAttachmentResolveRef{};
840 // colorAttachmentResolveRef.attachment // below
841 colorAttachmentResolveRef.layout = vk::ImageLayout::eColorAttachmentOptimal;
842 vk::SubpassDescription subpass{};
843 subpass.pipelineBindPoint = vk::PipelineBindPoint::eGraphics;
844 subpass.colorAttachmentCount = 1;
845 subpass.pColorAttachments = &colorAttachmentRef;
846 vk::SubpassDependency dependency{};
847 dependency.srcSubpass = VK_SUBPASS_EXTERNAL;
848 dependency.dstSubpass = 0;
849 dependency.srcStageMask = vk::PipelineStageFlagBits::eColorAttachmentOutput | vk::PipelineStageFlagBits::eEarlyFragmentTests;
850 dependency.srcAccessMask = {};
851 dependency.dstStageMask = vk::PipelineStageFlagBits::eColorAttachmentOutput | vk::PipelineStageFlagBits::eEarlyFragmentTests;
852 dependency.dstAccessMask = vk::AccessFlagBits::eColorAttachmentWrite | vk::AccessFlagBits::eDepthStencilAttachmentWrite;
853
854 std::vector<vk::AttachmentDescription> attachments = {colorAttachment};
855 if (mZActive) {
856 attachments.emplace_back(depthAttachment);
857 depthAttachmentRef.attachment = nAttachments++;
858 subpass.pDepthStencilAttachment = &depthAttachmentRef;
859 }
860 if (mMSAASampleCount != vk::SampleCountFlagBits::e1) {
861 attachments.emplace_back(colorAttachmentResolve);
862 colorAttachmentResolveRef.attachment = nAttachments++;
863 subpass.pResolveAttachments = &colorAttachmentResolveRef;
864 }
865
866 vk::RenderPassCreateInfo renderPassInfo{};
867 renderPassInfo.attachmentCount = attachments.size();
868 renderPassInfo.pAttachments = attachments.data();
869 renderPassInfo.subpassCount = 1;
870 renderPassInfo.pSubpasses = &subpass;
871 renderPassInfo.dependencyCount = 1;
872 renderPassInfo.pDependencies = &dependency;
873 mRenderPass = mDevice.createRenderPass(renderPassInfo, nullptr);
874
875 const uint32_t imageCountWithMixImages = mImageCount * (mMixingSupported ? 2 : 1);
876 mRenderTargetView.resize(imageCountWithMixImages);
877 mFramebuffers.resize(imageCountWithMixImages);
878 if (mDownsampleFSAA) {
879 mDownsampleImages.resize(imageCountWithMixImages);
880 }
881 if (mMSAASampleCount != vk::SampleCountFlagBits::e1) {
882 mMSAAImages.resize(imageCountWithMixImages);
883 }
884 if (mZActive) {
885 mZImages.resize(imageCountWithMixImages);
886 }
887 if (mMSAASampleCount != vk::SampleCountFlagBits::e1 || mZActive || mDownsampleFSAA) {
889 }
890 if (mMixingSupported) {
891 if (mMSAASampleCount != vk::SampleCountFlagBits::e1 || mZActive || mDownsampleFSAA) {
893 }
894 if (!mDownsampleFSAA) {
895 mMixImages.resize(mImageCount);
896 }
897 }
898
899 // Text overlay goes as extra rendering path
900 renderPassInfo.attachmentCount = 1; // Remove depth and MSAA attachments
901 renderPassInfo.pAttachments = &colorAttachment;
902 subpass.pDepthStencilAttachment = nullptr;
903 subpass.pResolveAttachments = nullptr;
904 if (mFramebuffersText.size()) {
905 dependency.srcStageMask = vk::PipelineStageFlagBits::eColorAttachmentOutput; // Remove early fragment test
906 dependency.dstStageMask = vk::PipelineStageFlagBits::eColorAttachmentOutput;
907 dependency.dstAccessMask = vk::AccessFlagBits::eColorAttachmentWrite; // Remove depth/stencil dependencies
908 }
909 colorAttachment.loadOp = vk::AttachmentLoadOp::eLoad; // Don't clear the frame buffer
910 colorAttachment.initialLayout = vk::ImageLayout::ePresentSrcKHR; // Initial layout is not undefined after 1st pass
911 colorAttachment.samples = vk::SampleCountFlagBits::e1; // No MSAA for Text
912 colorAttachment.finalLayout = vk::ImageLayout::ePresentSrcKHR; // Might have been overwritten above for 1st pass in case of MSAA
913 mRenderPassText = mDevice.createRenderPass(renderPassInfo, nullptr);
914
915 if (mMixingSupported) {
916 if (mDownsampleFSAA) {
917 colorAttachment.initialLayout = vk::ImageLayout::eColorAttachmentOptimal;
918 colorAttachment.finalLayout = mDownsampleFSAA ? vk::ImageLayout::eColorAttachmentOptimal : vk::ImageLayout::ePresentSrcKHR;
919 }
920 if (mFramebuffersTexture.size()) {
921 dependency.srcStageMask = vk::PipelineStageFlagBits::eColorAttachmentOutput; // Remove early fragment test
922 dependency.dstStageMask = vk::PipelineStageFlagBits::eColorAttachmentOutput;
923 dependency.dstAccessMask = vk::AccessFlagBits::eColorAttachmentWrite; // Remove depth/stencil dependencies
924 }
925 mRenderPassTexture = mDevice.createRenderPass(renderPassInfo, nullptr);
926 }
927
928 for (uint32_t i = 0; i < imageCountWithMixImages; i++) {
929 if (i < mImageCount) { // Main render chain
930 // primary buffer mSwapChainImageViews[i] created as part of createSwapChain, not here
931 } else if (!mDownsampleFSAA) { // for rendering to mixBuffer
932 createImageI(mDevice, mPhysicalDevice, mMixImages[i - mImageCount].image, mMixImages[i - mImageCount].memory, mRenderWidth, mRenderHeight, mSurfaceFormat.format, vk::ImageUsageFlagBits::eColorAttachment | vk::ImageUsageFlagBits::eSampled, vk::MemoryPropertyFlagBits::eDeviceLocal, vk::ImageTiling::eOptimal);
933 mMixImages[i - mImageCount].view = createImageViewI(mDevice, mMixImages[i - mImageCount].image, mSurfaceFormat.format, vk::ImageAspectFlagBits::eColor, 1);
934 }
935 std::vector<vk::ImageView> att;
936 if (mDownsampleFSAA) {
937 vk::ImageUsageFlags usage = vk::ImageUsageFlagBits::eColorAttachment | (i >= mImageCount ? vk::ImageUsageFlagBits::eSampled : vk::ImageUsageFlagBits::eTransferSrc);
938 createImageI(mDevice, mPhysicalDevice, mDownsampleImages[i].image, mDownsampleImages[i].memory, mRenderWidth, mRenderHeight, mSurfaceFormat.format, usage, vk::MemoryPropertyFlagBits::eDeviceLocal, vk::ImageTiling::eOptimal);
939 mDownsampleImages[i].view = createImageViewI(mDevice, mDownsampleImages[i].image, mSurfaceFormat.format, vk::ImageAspectFlagBits::eColor, 1);
941 } else {
943 }
944 if (mMSAASampleCount != vk::SampleCountFlagBits::e1) { // First attachment is the render target, either the MSAA buffer or the framebuffer
945 createImageI(mDevice, mPhysicalDevice, mMSAAImages[i].image, mMSAAImages[i].memory, mRenderWidth, mRenderHeight, mSurfaceFormat.format, vk::ImageUsageFlagBits::eColorAttachment, vk::MemoryPropertyFlagBits::eDeviceLocal, vk::ImageTiling::eOptimal, mMSAASampleCount);
946 mMSAAImages[i].view = createImageViewI(mDevice, mMSAAImages[i].image, mSurfaceFormat.format, vk::ImageAspectFlagBits::eColor, 1);
947 att.emplace_back(mMSAAImages[i].view);
948 } else {
949 att.emplace_back(*mRenderTargetView[i]);
950 }
951 if (mZActive) {
952 createImageI(mDevice, mPhysicalDevice, mZImages[i].image, mZImages[i].memory, mRenderWidth, mRenderHeight, vk::Format::eD32Sfloat, vk::ImageUsageFlagBits::eDepthStencilAttachment, vk::MemoryPropertyFlagBits::eDeviceLocal, vk::ImageTiling::eOptimal, mMSAASampleCount);
953 mZImages[i].view = createImageViewI(mDevice, mZImages[i].image, vk::Format::eD32Sfloat, vk::ImageAspectFlagBits::eDepth, 1);
954 att.emplace_back(mZImages[i].view);
955 }
956 if (mMSAASampleCount != vk::SampleCountFlagBits::e1) { // If we use MSAA, we have to resolve to the framebuffer as the last target
957 att.emplace_back(*mRenderTargetView[i]);
958 }
959
960 vk::FramebufferCreateInfo framebufferInfo{};
961 framebufferInfo.renderPass = mRenderPass;
962 framebufferInfo.attachmentCount = att.size();
963 framebufferInfo.pAttachments = att.data();
964 framebufferInfo.width = mRenderWidth;
965 framebufferInfo.height = mRenderHeight;
966 framebufferInfo.layers = 1;
967 mFramebuffers[i] = mDevice.createFramebuffer(framebufferInfo, nullptr);
968
969 if (i < mImageCount && mFramebuffersText.size()) {
970 framebufferInfo.attachmentCount = 1;
971 framebufferInfo.pAttachments = &mSwapChainImageViews[i];
972 framebufferInfo.renderPass = mRenderPassText;
973 framebufferInfo.width = mScreenWidth;
974 framebufferInfo.height = mScreenHeight;
975 mFramebuffersText[i] = mDevice.createFramebuffer(framebufferInfo, nullptr);
976 }
977
978 if (i >= mImageCount && mFramebuffersTexture.size()) {
979 framebufferInfo.attachmentCount = 1;
980 framebufferInfo.pAttachments = mRenderTargetView[i - mImageCount];
981 framebufferInfo.renderPass = mRenderPassTexture;
982 framebufferInfo.width = mRenderWidth;
983 framebufferInfo.height = mRenderHeight;
984 mFramebuffersTexture[i - mImageCount] = mDevice.createFramebuffer(framebufferInfo, nullptr);
985 }
986 }
987
988 if (mMixingSupported) {
989 float vertices[6][4] = {
990 {0, (float)mRenderHeight, 0.0f, 1.0f},
991 {0, 0, 0.0f, 0.0f},
992 {(float)mRenderWidth, 0, 1.0f, 0.0f},
993 {0, (float)mRenderHeight, 0.0f, 1.0f},
994 {(float)mRenderWidth, 0, 1.0f, 0.0f},
995 {(float)mRenderWidth, (float)mRenderHeight, 1.0f, 1.0f}};
996 mMixingTextureVertexArray = createBuffer(sizeof(vertices), &vertices[0][0], vk::BufferUsageFlagBits::eVertexBuffer, 1);
997
999 for (uint32_t i = 0; i < mFramesInFlight; i++) {
1001 }
1002 }
1003 }
1004
1006}
1007
1009{
1010 clearVector(mFramebuffers, [&](auto& x) { mDevice.destroyFramebuffer(x, nullptr); });
1011 clearVector(mMSAAImages, [&](auto& x) { clearImage(x); });
1012 clearVector(mDownsampleImages, [&](auto& x) { clearImage(x); });
1013 clearVector(mZImages, [&](auto& x) { clearImage(x); });
1014 clearVector(mMixImages, [&](auto& x) { clearImage(x); });
1015 clearVector(mFramebuffersText, [&](auto& x) { mDevice.destroyFramebuffer(x, nullptr); });
1016 clearVector(mFramebuffersTexture, [&](auto& x) { mDevice.destroyFramebuffer(x, nullptr); });
1017 mDevice.destroyRenderPass(mRenderPass, nullptr);
1018 mDevice.destroyRenderPass(mRenderPassText, nullptr);
1019 if (mMixingSupported) {
1020 mDevice.destroyRenderPass(mRenderPassTexture, nullptr);
1022 }
1023}
1024
1025// ---------------------------- VULKAN PIPELINE ----------------------------
1026
1028{
1029 vk::PipelineShaderStageCreateInfo shaderStages[2] = {vk::PipelineShaderStageCreateInfo{}, vk::PipelineShaderStageCreateInfo{}};
1030 vk::PipelineShaderStageCreateInfo& vertShaderStageInfo = shaderStages[0];
1031 vertShaderStageInfo.stage = vk::ShaderStageFlagBits::eVertex;
1032 // vertShaderStageInfo.module // below
1033 vertShaderStageInfo.pName = "main";
1034 vk::PipelineShaderStageCreateInfo& fragShaderStageInfo = shaderStages[1];
1035 fragShaderStageInfo.stage = vk::ShaderStageFlagBits::eFragment;
1036 // fragShaderStageInfo.module // below
1037 fragShaderStageInfo.pName = "main";
1038
1039 vk::VertexInputBindingDescription bindingDescription{};
1040 bindingDescription.binding = 0;
1041 // bindingDescription.stride // below
1042 bindingDescription.inputRate = vk::VertexInputRate::eVertex;
1043
1044 vk::VertexInputAttributeDescription attributeDescriptions{};
1045 attributeDescriptions.binding = 0;
1046 attributeDescriptions.location = 0;
1047 // attributeDescriptions.format // below
1048 attributeDescriptions.offset = 0;
1049
1050 vk::PipelineVertexInputStateCreateInfo vertexInputInfo{};
1051 vertexInputInfo.vertexBindingDescriptionCount = 1;
1052 vertexInputInfo.pVertexBindingDescriptions = &bindingDescription;
1053 vertexInputInfo.vertexAttributeDescriptionCount = 1;
1054 vertexInputInfo.pVertexAttributeDescriptions = &attributeDescriptions;
1055 vk::PipelineInputAssemblyStateCreateInfo inputAssembly{};
1056 // inputAssembly.topology // below
1057 inputAssembly.primitiveRestartEnable = false;
1058
1059 vk::Viewport viewport{};
1060 viewport.x = 0.0f;
1061 viewport.y = 0.0f;
1062 // viewport.width // below
1063 // viewport.height // below
1064 viewport.minDepth = 0.0f;
1065 viewport.maxDepth = 1.0f;
1066
1067 vk::Rect2D scissor{};
1068 scissor.offset = vk::Offset2D{0, 0};
1069 // scissor.extent // below
1070
1071 vk::PipelineViewportStateCreateInfo viewportState{};
1072 viewportState.viewportCount = 1;
1073 viewportState.pViewports = &viewport;
1074 viewportState.scissorCount = 1;
1075 viewportState.pScissors = &scissor;
1076
1077 vk::PipelineRasterizationStateCreateInfo rasterizer{};
1078 rasterizer.depthClampEnable = false;
1079 rasterizer.rasterizerDiscardEnable = false;
1080 rasterizer.polygonMode = vk::PolygonMode::eFill;
1081 rasterizer.lineWidth = mDisplay->cfgL().lineWidth;
1082 rasterizer.cullMode = vk::CullModeFlagBits::eBack;
1083 rasterizer.frontFace = vk::FrontFace::eClockwise;
1084 rasterizer.depthBiasEnable = false;
1085 rasterizer.depthBiasConstantFactor = 0.0f; // Optional
1086 rasterizer.depthBiasClamp = 0.0f; // Optional
1087 rasterizer.depthBiasSlopeFactor = 0.0f; // Optional
1088
1089 vk::PipelineMultisampleStateCreateInfo multisampling{};
1090 multisampling.sampleShadingEnable = false;
1091 // multisampling.rasterizationSamples // below
1092 multisampling.minSampleShading = 1.0f; // Optional
1093 multisampling.pSampleMask = nullptr; // Optional
1094 multisampling.alphaToCoverageEnable = false; // Optional
1095 multisampling.alphaToOneEnable = false; // Optional
1096
1097 vk::PipelineColorBlendAttachmentState colorBlendAttachment{};
1098 colorBlendAttachment.colorWriteMask = vk::ColorComponentFlagBits::eR | vk::ColorComponentFlagBits::eG | vk::ColorComponentFlagBits::eB | vk::ColorComponentFlagBits::eA;
1099 // colorBlendAttachment.blendEnable // below
1100 colorBlendAttachment.colorWriteMask = vk::ColorComponentFlagBits::eR | vk::ColorComponentFlagBits::eG | vk::ColorComponentFlagBits::eB | vk::ColorComponentFlagBits::eA;
1101 colorBlendAttachment.srcColorBlendFactor = vk::BlendFactor::eSrcAlpha;
1102 colorBlendAttachment.dstColorBlendFactor = vk::BlendFactor::eOneMinusSrcAlpha;
1103 colorBlendAttachment.colorBlendOp = vk::BlendOp::eAdd;
1104 colorBlendAttachment.srcAlphaBlendFactor = vk::BlendFactor::eOneMinusSrcAlpha;
1105 colorBlendAttachment.dstAlphaBlendFactor = vk::BlendFactor::eZero;
1106 colorBlendAttachment.alphaBlendOp = vk::BlendOp::eAdd;
1107
1108 vk::PipelineColorBlendStateCreateInfo colorBlending{};
1109 colorBlending.logicOpEnable = false;
1110 colorBlending.logicOp = vk::LogicOp::eCopy;
1111 colorBlending.attachmentCount = 1;
1112 colorBlending.pAttachments = &colorBlendAttachment;
1113 colorBlending.blendConstants[0] = 0.0f;
1114 colorBlending.blendConstants[1] = 0.0f;
1115 colorBlending.blendConstants[2] = 0.0f;
1116 colorBlending.blendConstants[3] = 0.0f;
1117
1118 vk::PipelineDepthStencilStateCreateInfo depthStencil{};
1119 depthStencil.depthTestEnable = true;
1120 depthStencil.depthWriteEnable = true;
1121 depthStencil.depthCompareOp = vk::CompareOp::eLess;
1122 depthStencil.depthBoundsTestEnable = false;
1123 depthStencil.stencilTestEnable = false;
1124
1125 vk::DynamicState dynamicStates[] = {vk::DynamicState::eLineWidth};
1126 vk::PipelineDynamicStateCreateInfo dynamicState{};
1127 dynamicState.dynamicStateCount = 1;
1128 dynamicState.pDynamicStates = dynamicStates;
1129
1130 vk::PushConstantRange pushConstantRanges[2] = {vk::PushConstantRange{}, vk::PushConstantRange{}};
1131 pushConstantRanges[0].stageFlags = vk::ShaderStageFlagBits::eFragment;
1132 pushConstantRanges[0].offset = 0;
1133 pushConstantRanges[0].size = sizeof(float) * 4;
1134 pushConstantRanges[1].stageFlags = vk::ShaderStageFlagBits::eVertex;
1135 pushConstantRanges[1].offset = pushConstantRanges[0].size;
1136 pushConstantRanges[1].size = sizeof(float);
1137 vk::PipelineLayoutCreateInfo pipelineLayoutInfo{};
1138 pipelineLayoutInfo.setLayoutCount = 1;
1139 pipelineLayoutInfo.pSetLayouts = &mUniformDescriptor;
1140 pipelineLayoutInfo.pushConstantRangeCount = 2;
1141 pipelineLayoutInfo.pPushConstantRanges = pushConstantRanges;
1142 mPipelineLayout = mDevice.createPipelineLayout(pipelineLayoutInfo, nullptr);
1143 pipelineLayoutInfo.setLayoutCount = 1;
1144 pipelineLayoutInfo.pSetLayouts = &mUniformDescriptorTexture;
1145 mPipelineLayoutTexture = mDevice.createPipelineLayout(pipelineLayoutInfo, nullptr);
1146
1147 vk::GraphicsPipelineCreateInfo pipelineInfo{};
1148 pipelineInfo.stageCount = 2;
1149 pipelineInfo.pVertexInputState = &vertexInputInfo;
1150 pipelineInfo.pInputAssemblyState = &inputAssembly;
1151 pipelineInfo.pViewportState = &viewportState;
1152 pipelineInfo.pRasterizationState = &rasterizer;
1153 pipelineInfo.pMultisampleState = &multisampling;
1154 // pipelineInfo.pDepthStencilState // below
1155 pipelineInfo.pColorBlendState = &colorBlending;
1156 pipelineInfo.pDynamicState = &dynamicState;
1157 // pipelineInfo.layout // below
1158 // pipelineInfo.renderPass // below
1159 pipelineInfo.subpass = 0;
1160 pipelineInfo.pStages = shaderStages;
1161 pipelineInfo.basePipelineHandle = VkPipeline(VK_NULL_HANDLE); // Optional
1162 pipelineInfo.basePipelineIndex = -1; // Optional
1163
1164 mPipelines.resize(mMixingSupported ? 5 : 4);
1165 static constexpr vk::PrimitiveTopology types[3] = {vk::PrimitiveTopology::ePointList, vk::PrimitiveTopology::eLineList, vk::PrimitiveTopology::eLineStrip};
1166 for (uint32_t i = 0; i < mPipelines.size(); i++) {
1167 if (i == 4) { // Texture rendering
1168 bindingDescription.stride = 4 * sizeof(float);
1169 attributeDescriptions.format = vk::Format::eR32G32B32A32Sfloat;
1170 inputAssembly.topology = vk::PrimitiveTopology::eTriangleList;
1171 vertShaderStageInfo.module = mShaders["vertexTexture"];
1172 fragShaderStageInfo.module = mShaders["fragmentTexture"];
1173 pipelineInfo.layout = mPipelineLayoutTexture;
1174 pipelineInfo.renderPass = mRenderPassTexture;
1175 pipelineInfo.pDepthStencilState = nullptr;
1176 colorBlendAttachment.blendEnable = true;
1177 multisampling.rasterizationSamples = vk::SampleCountFlagBits::e1;
1178 viewport.width = scissor.extent.width = mRenderWidth;
1179 viewport.height = scissor.extent.height = mRenderHeight;
1180 } else if (i == 3) { // Text rendering
1181 bindingDescription.stride = 4 * sizeof(float);
1182 attributeDescriptions.format = vk::Format::eR32G32B32A32Sfloat;
1183 inputAssembly.topology = vk::PrimitiveTopology::eTriangleList;
1184 vertShaderStageInfo.module = mShaders["vertexTexture"];
1185 fragShaderStageInfo.module = mShaders["fragmentText"];
1186 pipelineInfo.layout = mPipelineLayoutTexture;
1187 pipelineInfo.renderPass = mRenderPassText;
1188 pipelineInfo.pDepthStencilState = nullptr;
1189 colorBlendAttachment.blendEnable = true;
1190 multisampling.rasterizationSamples = vk::SampleCountFlagBits::e1;
1191 viewport.width = scissor.extent.width = mScreenWidth;
1192 viewport.height = scissor.extent.height = mScreenHeight;
1193 } else { // Point / line / line-strip rendering
1194 bindingDescription.stride = 3 * sizeof(float);
1195 attributeDescriptions.format = vk::Format::eR32G32B32Sfloat;
1196 inputAssembly.topology = types[i];
1197 vertShaderStageInfo.module = mShaders[types[i] == vk::PrimitiveTopology::ePointList ? "vertexPoint" : "vertex"];
1198 fragShaderStageInfo.module = mShaders["fragment"];
1199 pipelineInfo.layout = mPipelineLayout;
1200 pipelineInfo.renderPass = mRenderPass;
1201 pipelineInfo.pDepthStencilState = mZActive ? &depthStencil : nullptr;
1202 colorBlendAttachment.blendEnable = true;
1203 multisampling.rasterizationSamples = mMSAASampleCount;
1204 viewport.width = scissor.extent.width = mRenderWidth;
1205 viewport.height = scissor.extent.height = mRenderHeight;
1206 }
1207
1208 CHKERR(mDevice.createGraphicsPipelines(VkPipelineCache(VK_NULL_HANDLE), 1, &pipelineInfo, nullptr, &mPipelines[i])); // TODO: multiple at once + cache?
1209 }
1210}
1211
1212void GPUDisplayBackendVulkan::startFillCommandBuffer(vk::CommandBuffer& commandBuffer, uint32_t imageIndex, bool toMixBuffer)
1213{
1214 commandBuffer.reset({});
1215
1216 vk::CommandBufferBeginInfo beginInfo{};
1217 beginInfo.flags = {};
1218 commandBuffer.begin(beginInfo);
1219
1220 vk::ClearValue clearValues[2];
1221 clearValues[0].color = mDisplay->cfgL().invertColors ? vk::ClearColorValue{std::array<float, 4>{1.0f, 1.0f, 1.0f, 1.0f}} : vk::ClearColorValue{std::array<float, 4>{0.0f, 0.0f, 0.0f, 1.0f}};
1222 clearValues[1].depthStencil = vk::ClearDepthStencilValue{{1.0f, 0}};
1223
1224 vk::RenderPassBeginInfo renderPassInfo{};
1225 renderPassInfo.renderPass = mRenderPass;
1226 renderPassInfo.framebuffer = toMixBuffer ? mFramebuffers[imageIndex + mImageCount] : mFramebuffers[imageIndex];
1227 renderPassInfo.renderArea.offset = vk::Offset2D{0, 0};
1228 renderPassInfo.renderArea.extent = vk::Extent2D{mRenderWidth, mRenderHeight};
1229 renderPassInfo.clearValueCount = mZActive ? 2 : 1;
1230 renderPassInfo.pClearValues = clearValues;
1231 commandBuffer.beginRenderPass(&renderPassInfo, vk::SubpassContents::eInline);
1232
1233 vk::DeviceSize offsets[] = {0};
1234 commandBuffer.bindVertexBuffers(0, 1, &mVBO.buffer, offsets);
1235 commandBuffer.bindDescriptorSets(vk::PipelineBindPoint::eGraphics, mPipelineLayout, 0, 1, &mDescriptorSets[0][mCurrentBufferSet], 0, nullptr);
1236}
1237
1238void GPUDisplayBackendVulkan::endFillCommandBuffer(vk::CommandBuffer& commandBuffer)
1239{
1240 commandBuffer.endRenderPass();
1241 commandBuffer.end();
1242}
1243
1245{
1246 clearVector(mPipelines, [&](auto& x) { mDevice.destroyPipeline(x, nullptr); });
1247 mDevice.destroyPipelineLayout(mPipelineLayout, nullptr);
1248 mDevice.destroyPipelineLayout(mPipelineLayoutTexture, nullptr);
1249}
1250
1251// ---------------------------- VULKAN SHADERS ----------------------------
1252
1253#define LOAD_SHADER(file, ext) \
1254 mShaders[#file] = createShaderModule(_binary_shaders_shaders_##file##_##ext##_spv_start, _binary_shaders_shaders_##file##_##ext##_spv_len, mDevice)
1255
1257{
1258 LOAD_SHADER(vertex, vert);
1259 LOAD_SHADER(fragment, frag);
1260 LOAD_SHADER(vertexPoint, vert);
1261 LOAD_SHADER(vertexTexture, vert);
1262 LOAD_SHADER(fragmentTexture, frag);
1263 LOAD_SHADER(fragmentText, frag);
1264}
1265
1267{
1268 clearVector(mShaders, [&](auto& x) { mDevice.destroyShaderModule(x.second, nullptr); });
1269}
1270
1271// ---------------------------- VULKAN BUFFERS ----------------------------
1272
1274{
1275 if (buffer.deviceMemory != 1) {
1276 void* dstData;
1277 CHKERR(mDevice.mapMemory(buffer.memory, 0, buffer.size, {}, &dstData));
1278 memcpy(dstData, srcData, size);
1279 mDevice.unmapMemory(buffer.memory);
1280 } else {
1281 auto tmp = createBuffer(size, srcData, vk::BufferUsageFlagBits::eTransferSrc, 0);
1282
1283 vk::CommandBuffer commandBuffer = getSingleTimeCommandBuffer();
1284 vk::BufferCopy copyRegion{};
1285 copyRegion.size = size;
1286 commandBuffer.copyBuffer(tmp.buffer, buffer.buffer, 1, &copyRegion);
1287 submitSingleTimeCommandBuffer(commandBuffer);
1288
1289 clearBuffer(tmp);
1290 }
1291}
1292
1293GPUDisplayBackendVulkan::VulkanBuffer GPUDisplayBackendVulkan::createBuffer(size_t size, const void* srcData, vk::BufferUsageFlags type, int32_t deviceMemory)
1294{
1295 vk::MemoryPropertyFlags properties;
1296 if (deviceMemory) {
1297 properties |= vk::MemoryPropertyFlagBits::eDeviceLocal;
1298 }
1299 if (deviceMemory == 0 || deviceMemory == 2) {
1300 properties |= (vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent);
1301 }
1302 if (deviceMemory == 1) {
1303 type |= vk::BufferUsageFlagBits::eTransferDst;
1304 }
1305
1307 vk::BufferCreateInfo bufferInfo{};
1308 bufferInfo.size = size;
1309 bufferInfo.usage = type;
1310 bufferInfo.sharingMode = vk::SharingMode::eExclusive;
1311 buffer.buffer = mDevice.createBuffer(bufferInfo, nullptr);
1312
1313 vk::MemoryRequirements memRequirements;
1314 memRequirements = mDevice.getBufferMemoryRequirements(buffer.buffer);
1315 vk::MemoryAllocateInfo allocInfo{};
1316 allocInfo.allocationSize = memRequirements.size;
1317 allocInfo.memoryTypeIndex = findMemoryType(memRequirements.memoryTypeBits, properties, mPhysicalDevice);
1318 buffer.memory = mDevice.allocateMemory(allocInfo, nullptr);
1319
1320 mDevice.bindBufferMemory(buffer.buffer, buffer.memory, 0);
1321
1322 buffer.size = size;
1323 buffer.deviceMemory = deviceMemory;
1324
1325 if (srcData != nullptr) {
1326 writeToBuffer(buffer, size, srcData);
1327 }
1328
1329 return buffer;
1330}
1331
1333{
1334 mDevice.destroyBuffer(buffer.buffer, nullptr);
1335 mDevice.freeMemory(buffer.memory, nullptr);
1336}
1337
1339{
1340 if (mVBO.size) {
1342 mVBO.size = 0;
1343 }
1347 }
1348 for (auto& buf : mFontVertexBuffer) {
1349 if (buf.size) {
1351 }
1352 buf.size = 0;
1353 }
1354}
1355
1356// ---------------------------- VULKAN TEXTURES ----------------------------
1357
1358void GPUDisplayBackendVulkan::writeToImage(VulkanImage& image, const void* srcData, size_t srcSize)
1359{
1360 auto tmp = createBuffer(srcSize, srcData, vk::BufferUsageFlagBits::eTransferSrc, 0);
1361
1362 vk::CommandBuffer commandBuffer = getSingleTimeCommandBuffer();
1363 cmdImageMemoryBarrier(commandBuffer, image.image, {}, vk::AccessFlagBits::eTransferWrite, vk::ImageLayout::eUndefined, vk::ImageLayout::eTransferDstOptimal, vk::PipelineStageFlagBits::eTopOfPipe, vk::PipelineStageFlagBits::eTransfer);
1364 vk::BufferImageCopy region{};
1365 region.bufferOffset = 0;
1366 region.bufferRowLength = 0;
1367 region.bufferImageHeight = 0;
1368 region.imageSubresource.aspectMask = vk::ImageAspectFlagBits::eColor;
1369 region.imageSubresource.mipLevel = 0;
1370 region.imageSubresource.baseArrayLayer = 0;
1371 region.imageSubresource.layerCount = 1;
1372 region.imageOffset = vk::Offset3D{0, 0, 0};
1373 region.imageExtent = vk::Extent3D{image.sizex, image.sizey, 1};
1374 commandBuffer.copyBufferToImage(tmp.buffer, image.image, vk::ImageLayout::eTransferDstOptimal, 1, &region);
1375 cmdImageMemoryBarrier(commandBuffer, image.image, vk::AccessFlagBits::eTransferWrite, vk::AccessFlagBits::eShaderRead, vk::ImageLayout::eTransferDstOptimal, vk::ImageLayout::eShaderReadOnlyOptimal, vk::PipelineStageFlagBits::eTransfer, vk::PipelineStageFlagBits::eFragmentShader);
1376 submitSingleTimeCommandBuffer(commandBuffer);
1377
1378 clearBuffer(tmp);
1379}
1380
1381GPUDisplayBackendVulkan::VulkanImage GPUDisplayBackendVulkan::createImage(uint32_t sizex, uint32_t sizey, const void* srcData, size_t srcSize, vk::Format format)
1382{
1384 createImageI(mDevice, mPhysicalDevice, image.image, image.memory, sizex, sizey, format, vk::ImageUsageFlagBits::eTransferDst | vk::ImageUsageFlagBits::eSampled, vk::MemoryPropertyFlagBits::eDeviceLocal, vk::ImageTiling::eOptimal, vk::SampleCountFlagBits::e1);
1385
1386 image.view = createImageViewI(mDevice, image.image, format);
1387
1388 image.sizex = sizex;
1389 image.sizey = sizey;
1390 image.format = format;
1391
1392 if (srcData) {
1393 writeToImage(image, srcData, srcSize);
1394 }
1395 return image;
1396}
1397
1399{
1400 mDevice.destroyImageView(image.view, nullptr);
1401 mDevice.destroyImage(image.image, nullptr);
1402 mDevice.freeMemory(image.memory, nullptr);
1403}
1404
1405// ---------------------------- VULKAN INIT EXIT ----------------------------
1406
1408{
1409 mEnableValidationLayers = mDisplay->param() && mDisplay->param()->par.debugLevel >= 2;
1410 mFramesInFlight = 2;
1411
1412 createDevice();
1413 createShaders();
1418
1419 return (0);
1420}
1421
1442
1443// ---------------------------- USER CODE ----------------------------
1444
1446{
1447 if (mScreenWidth == width && mScreenHeight == height) {
1448 return;
1449 }
1451 vk::Extent2D extent = chooseSwapExtent(mSwapChainDetails.capabilities);
1452 if (extent.width != mScreenWidth || extent.height != mScreenHeight) {
1453 mMustUpdateSwapChain = true;
1454 }
1455}
1456
1458{
1459 mDevice.waitIdle();
1461 mVBO = createBuffer(totalVertizes * sizeof(mDisplay->vertexBuffer()[0][0]), mDisplay->vertexBuffer()[0].data(), vk::BufferUsageFlagBits::eVertexBuffer, 1);
1462 if (mDisplay->cfgR().useGLIndirectDraw) {
1464 mIndirectCommandBuffer = createBuffer(mCmdBuffer.size() * sizeof(mCmdBuffer[0]), mCmdBuffer.data(), vk::BufferUsageFlagBits::eIndirectBuffer, 1);
1465 mCmdBuffer.clear();
1466 }
1468}
1469
1471{
1472 auto first = std::get<0>(v);
1473 auto count = std::get<1>(v);
1474 auto iSector = std::get<2>(v);
1475 if (count == 0) {
1476 return 0;
1477 }
1479 return count;
1480 }
1481
1483 mCurrentCommandBuffer.bindPipeline(vk::PipelineBindPoint::eGraphics, mPipelines[tt]);
1485 }
1486 if (mDisplay->cfgR().useGLIndirectDraw) {
1488 } else {
1489 for (uint32_t k = 0; k < count; k++) {
1490 mCurrentCommandBuffer.draw(mDisplay->vertexBufferCount()[iSector][first + k], 1, mDisplay->vertexBufferStart()[iSector][first + k], 0);
1491 }
1492 }
1493
1494 return count;
1495}
1496
1497void GPUDisplayBackendVulkan::prepareDraw(const hmm_mat4& proj, const hmm_mat4& view, bool requestScreenshot, bool toMixBuffer, float includeMixImage)
1498{
1499 if (mDisplay->updateDrawCommands() || toMixBuffer || includeMixImage > 0) {
1501 }
1502
1503 if (includeMixImage == 0.f) {
1505 CHKERR(mDevice.waitForFences(1, &mInFlightFence[mCurrentFrame], true, UINT64_MAX));
1506 auto getImage = [&]() {
1507 vk::Fence fen = VkFence(VK_NULL_HANDLE);
1508 vk::Semaphore sem = VkSemaphore(VK_NULL_HANDLE);
1511 CHKERR(mDevice.resetFences(1, &fen));
1512 } else {
1514 }
1515 return mDevice.acquireNextImageKHR(mSwapChain, UINT64_MAX, sem, fen, &mCurrentImageIndex);
1516 };
1517
1518 vk::Result retVal = vk::Result::eSuccess;
1519 bool mustUpdateRendering = mMustUpdateSwapChain;
1520 if (mDisplay->updateRenderPipeline() || (requestScreenshot && !mSwapchainImageReadable) || (toMixBuffer && !mMixingSupported) || mDownsampleFactor != getDownsampleFactor(requestScreenshot)) {
1521 mustUpdateRendering = true;
1522 } else if (!mMustUpdateSwapChain) {
1523 retVal = getImage();
1524 }
1525 if (mMustUpdateSwapChain || mustUpdateRendering || retVal == vk::Result::eErrorOutOfDateKHR || retVal == vk::Result::eSuboptimalKHR) {
1526 if (!mustUpdateRendering) {
1527 GPUInfo("Pipeline out of data / suboptimal, recreating");
1528 }
1529 recreateRendering(requestScreenshot, toMixBuffer);
1530 retVal = getImage();
1531 }
1532 CHKERR(retVal);
1534 CHKERR(mDevice.waitForFences(1, &mInFlightFence[mCurrentFrame], true, UINT64_MAX));
1535 }
1536 CHKERR(mDevice.resetFences(1, &mInFlightFence[mCurrentFrame]));
1537 mMustUpdateSwapChain = false;
1538 mHasDrawnText = false;
1540
1541 const hmm_mat4 modelViewProj = proj * view;
1542 writeToBuffer(mUniformBuffersMat[0][mCurrentBufferSet], sizeof(modelViewProj), &modelViewProj);
1543 }
1544
1549 }
1550}
1551
1552void GPUDisplayBackendVulkan::finishDraw(bool doScreenshot, bool toMixBuffer, float includeMixImage)
1553{
1556 if (!toMixBuffer && includeMixImage == 0.f && mCommandBufferPerImage) {
1558 }
1559 }
1560}
1561
1562void GPUDisplayBackendVulkan::finishFrame(bool doScreenshot, bool toMixBuffer, float includeMixImage)
1563{
1564 vk::Semaphore* stageFinishedSemaphore = &mRenderFinishedSemaphore[mCurrentFrame];
1565 const vk::Fence noFence = VkFence(VK_NULL_HANDLE);
1566
1567 vk::SubmitInfo submitInfo{};
1568 vk::PipelineStageFlags waitStages[] = {vk::PipelineStageFlagBits::eColorAttachmentOutput};
1569 submitInfo.pWaitSemaphores = includeMixImage > 0.f ? &mRenderFinishedSemaphore[mCurrentFrame] : (!mCommandBufferPerImage ? &mImageAvailableSemaphore[mCurrentFrame] : nullptr);
1570 submitInfo.waitSemaphoreCount = submitInfo.pWaitSemaphores != nullptr ? 1 : 0;
1571 submitInfo.pWaitDstStageMask = waitStages;
1572 submitInfo.commandBufferCount = 1;
1573 submitInfo.pCommandBuffers = &mCurrentCommandBuffer;
1574 submitInfo.signalSemaphoreCount = 1;
1575 submitInfo.pSignalSemaphores = stageFinishedSemaphore;
1576 CHKERR(mGraphicsQueue.submit(1, &submitInfo, includeMixImage > 0 || toMixBuffer || mHasDrawnText || mDownsampleFSAA ? noFence : mInFlightFence[mCurrentFrame]));
1577 if (!toMixBuffer) {
1578 if (includeMixImage > 0.f) {
1580 submitInfo.pWaitSemaphores = stageFinishedSemaphore;
1581 waitStages[0] = {vk::PipelineStageFlagBits::eColorAttachmentOutput};
1582 submitInfo.waitSemaphoreCount = 1;
1583 submitInfo.pCommandBuffers = &mCommandBuffersTexture[mCurrentBufferSet];
1584 stageFinishedSemaphore = &mMixFinishedSemaphore[mCurrentFrame];
1585 submitInfo.pSignalSemaphores = stageFinishedSemaphore;
1586 CHKERR(mGraphicsQueue.submit(1, &submitInfo, mHasDrawnText || mDownsampleFSAA ? noFence : mInFlightFence[mCurrentFrame]));
1587 }
1588
1589 if (mDownsampleFSAA) {
1591 submitInfo.pCommandBuffers = &mCommandBuffersDownsample[mCurrentBufferSet];
1592 submitInfo.pWaitSemaphores = stageFinishedSemaphore;
1593 waitStages[0] = {vk::PipelineStageFlagBits::eTransfer};
1594 submitInfo.waitSemaphoreCount = 1;
1595 stageFinishedSemaphore = &mDownsampleFinishedSemaphore[mCurrentFrame];
1596 submitInfo.pSignalSemaphores = stageFinishedSemaphore;
1597 CHKERR(mGraphicsQueue.submit(1, &submitInfo, mHasDrawnText ? noFence : mInFlightFence[mCurrentFrame]));
1598 }
1599
1600 if (doScreenshot) {
1601 mDevice.waitIdle();
1602 if (mDisplay->cfgR().screenshotScaleFactor != 1) {
1603 readImageToPixels(mDownsampleImages[mCurrentImageIndex].image, vk::ImageLayout::eColorAttachmentOptimal, mScreenshotPixels);
1604 } else {
1605 readImageToPixels(mSwapChainImages[mCurrentImageIndex], vk::ImageLayout::ePresentSrcKHR, mScreenshotPixels);
1606 }
1607 }
1608
1609 if (mHasDrawnText) {
1610 submitInfo.pWaitSemaphores = stageFinishedSemaphore;
1611 waitStages[0] = {vk::PipelineStageFlagBits::eColorAttachmentOutput};
1612 submitInfo.waitSemaphoreCount = 1;
1613 submitInfo.pCommandBuffers = &mCommandBuffersText[mCurrentBufferSet];
1614 stageFinishedSemaphore = &mTextFinishedSemaphore[mCurrentFrame];
1615 submitInfo.pSignalSemaphores = stageFinishedSemaphore;
1616 CHKERR(mGraphicsQueue.submit(1, &submitInfo, mInFlightFence[mCurrentFrame]));
1617 }
1618
1619 CHKERR(mDevice.waitForFences(1, &mInFlightFence[mCurrentFrame], true, UINT64_MAX)); // TODO: I think we need to wait for the fence, so that the image was acquired before we present. Perhaps we can present later to avoid delays
1620 vk::PresentInfoKHR presentInfo{};
1621 presentInfo.waitSemaphoreCount = 1;
1622 presentInfo.pWaitSemaphores = stageFinishedSemaphore;
1623 presentInfo.swapchainCount = 1;
1624 presentInfo.pSwapchains = &mSwapChain;
1625 presentInfo.pImageIndices = &mCurrentImageIndex;
1626 presentInfo.pResults = nullptr;
1627 vk::Result retVal = mGraphicsQueue.presentKHR(&presentInfo);
1628 if (retVal == vk::Result::eErrorOutOfDateKHR) {
1629 mMustUpdateSwapChain = true;
1630 } else {
1631 CHKERR(retVal);
1632 }
1633 }
1634}
1635
1636void GPUDisplayBackendVulkan::downsampleToFramebuffer(vk::CommandBuffer& commandBuffer)
1637{
1638 commandBuffer.reset({});
1639 vk::CommandBufferBeginInfo beginInfo{};
1640 beginInfo.flags = vk::CommandBufferUsageFlagBits::eOneTimeSubmit;
1641 commandBuffer.begin(beginInfo);
1642
1643 cmdImageMemoryBarrier(commandBuffer, mSwapChainImages[mCurrentImageIndex], {}, vk::AccessFlagBits::eTransferWrite, vk::ImageLayout::eUndefined, vk::ImageLayout::eTransferDstOptimal, vk::PipelineStageFlagBits::eTransfer, vk::PipelineStageFlagBits::eTransfer);
1644 cmdImageMemoryBarrier(commandBuffer, mDownsampleImages[mCurrentImageIndex].image, vk::AccessFlagBits::eMemoryRead, vk::AccessFlagBits::eTransferRead, vk::ImageLayout::eColorAttachmentOptimal, vk::ImageLayout::eTransferSrcOptimal, vk::PipelineStageFlagBits::eTransfer, vk::PipelineStageFlagBits::eTransfer);
1645
1646 vk::Offset3D blitSizeSrc;
1647 blitSizeSrc.x = mRenderWidth;
1648 blitSizeSrc.y = mRenderHeight;
1649 blitSizeSrc.z = 1;
1650 vk::Offset3D blitSizeDst;
1651 blitSizeDst.x = mScreenWidth;
1652 blitSizeDst.y = mScreenHeight;
1653 blitSizeDst.z = 1;
1654 vk::ImageBlit imageBlitRegion{};
1655 imageBlitRegion.srcSubresource.aspectMask = vk::ImageAspectFlagBits::eColor;
1656 imageBlitRegion.srcSubresource.layerCount = 1;
1657 imageBlitRegion.srcOffsets[1] = blitSizeSrc;
1658 imageBlitRegion.dstSubresource.aspectMask = vk::ImageAspectFlagBits::eColor;
1659 imageBlitRegion.dstSubresource.layerCount = 1;
1660 imageBlitRegion.dstOffsets[1] = blitSizeDst;
1661 commandBuffer.blitImage(mDownsampleImages[mCurrentImageIndex].image, vk::ImageLayout::eTransferSrcOptimal, mSwapChainImages[mCurrentImageIndex], vk::ImageLayout::eTransferDstOptimal, 1, &imageBlitRegion, vk::Filter::eLinear);
1662
1663 cmdImageMemoryBarrier(commandBuffer, mSwapChainImages[mCurrentImageIndex], vk::AccessFlagBits::eTransferWrite, vk::AccessFlagBits::eMemoryRead, vk::ImageLayout::eTransferDstOptimal, vk::ImageLayout::ePresentSrcKHR, vk::PipelineStageFlagBits::eTransfer, vk::PipelineStageFlagBits::eTransfer);
1664 cmdImageMemoryBarrier(commandBuffer, mDownsampleImages[mCurrentImageIndex].image, vk::AccessFlagBits::eTransferRead, vk::AccessFlagBits::eMemoryRead, vk::ImageLayout::eUndefined, vk::ImageLayout::eColorAttachmentOptimal, vk::PipelineStageFlagBits::eTransfer, vk::PipelineStageFlagBits::eTransfer);
1665
1666 commandBuffer.end();
1667}
1668
1670{
1671 hmm_mat4 proj = HMM_Orthographic(0.f, mScreenWidth, 0.f, mScreenHeight, -1, 1);
1672 writeToBuffer(mUniformBuffersMat[1][mCurrentBufferSet], sizeof(proj), &proj);
1673
1674 mFontVertexBufferHost.clear();
1675 mTextDrawCommands.clear();
1676}
1677
1679{
1680 if (!mHasDrawnText) {
1681 return;
1682 }
1683
1685
1686 vk::CommandBufferBeginInfo beginInfo{};
1687 beginInfo.flags = vk::CommandBufferUsageFlagBits::eOneTimeSubmit;
1688 mCommandBuffersText[mCurrentBufferSet].begin(beginInfo);
1689
1690 vk::RenderPassBeginInfo renderPassInfo{};
1691 renderPassInfo.renderPass = mRenderPassText;
1693 renderPassInfo.renderArea.offset = vk::Offset2D{0, 0};
1694 renderPassInfo.renderArea.extent = vk::Extent2D{mScreenWidth, mScreenHeight};
1695 renderPassInfo.clearValueCount = 0;
1696 mCommandBuffersText[mCurrentBufferSet].beginRenderPass(renderPassInfo, vk::SubpassContents::eInline);
1697
1700 }
1701 mFontVertexBuffer[mCurrentBufferSet] = createBuffer(mFontVertexBufferHost.size() * sizeof(float), mFontVertexBufferHost.data(), vk::BufferUsageFlagBits::eVertexBuffer, 0);
1702
1703 mCommandBuffersText[mCurrentBufferSet].bindPipeline(vk::PipelineBindPoint::eGraphics, mPipelines[3]);
1704 mCommandBuffersText[mCurrentBufferSet].bindDescriptorSets(vk::PipelineBindPoint::eGraphics, mPipelineLayoutTexture, 0, 1, &mDescriptorSets[1][mCurrentBufferSet], 0, nullptr);
1705 vk::DeviceSize offsets[] = {0};
1707
1708 for (const auto& cmd : mTextDrawCommands) {
1709 mCommandBuffersText[mCurrentBufferSet].pushConstants(mPipelineLayoutTexture, vk::ShaderStageFlagBits::eFragment, 0, sizeof(cmd.color), cmd.color);
1710 mCommandBuffersText[mCurrentBufferSet].draw(cmd.nVertices, 1, cmd.firstVertex, 0);
1711 }
1712
1713 mFontVertexBufferHost.clear();
1714
1715 mCommandBuffersText[mCurrentBufferSet].endRenderPass();
1717}
1718
1719void GPUDisplayBackendVulkan::mixImages(vk::CommandBuffer commandBuffer, float mixSlaveImage)
1720{
1721 hmm_mat4 proj = HMM_Orthographic(0.f, mRenderWidth, 0.f, mRenderHeight, -1, 1);
1722 writeToBuffer(mUniformBuffersMat[2][mCurrentBufferSet], sizeof(proj), &proj);
1723
1724 commandBuffer.reset({});
1725 vk::CommandBufferBeginInfo beginInfo{};
1726 beginInfo.flags = vk::CommandBufferUsageFlagBits::eOneTimeSubmit;
1727 commandBuffer.begin(beginInfo);
1728
1730 vk::ImageLayout srcLayout = mDownsampleFSAA ? vk::ImageLayout::eColorAttachmentOptimal : vk::ImageLayout::ePresentSrcKHR;
1731 cmdImageMemoryBarrier(commandBuffer, image, {}, vk::AccessFlagBits::eMemoryRead, srcLayout, vk::ImageLayout::eShaderReadOnlyOptimal, vk::PipelineStageFlagBits::eAllCommands, vk::PipelineStageFlagBits::eFragmentShader);
1732
1733 vk::RenderPassBeginInfo renderPassInfo{};
1734 renderPassInfo.renderPass = mRenderPassTexture;
1736 renderPassInfo.renderArea.offset = vk::Offset2D{0, 0};
1737 renderPassInfo.renderArea.extent = vk::Extent2D{mRenderWidth, mRenderHeight};
1738 renderPassInfo.clearValueCount = 0;
1739 commandBuffer.beginRenderPass(renderPassInfo, vk::SubpassContents::eInline);
1740
1741 commandBuffer.bindPipeline(vk::PipelineBindPoint::eGraphics, mPipelines[4]);
1744 }
1745 commandBuffer.bindDescriptorSets(vk::PipelineBindPoint::eGraphics, mPipelineLayoutTexture, 0, 1, &mDescriptorSets[2][mCurrentBufferSet], 0, nullptr);
1746 vk::DeviceSize offsets[] = {0};
1747 commandBuffer.bindVertexBuffers(0, 1, &mMixingTextureVertexArray.buffer, offsets);
1748
1749 commandBuffer.pushConstants(mPipelineLayoutTexture, vk::ShaderStageFlagBits::eFragment, 0, sizeof(mixSlaveImage), &mixSlaveImage);
1750 commandBuffer.draw(6, 1, 0, 0);
1751
1752 commandBuffer.endRenderPass();
1753 commandBuffer.end();
1754}
1755
1757{
1759 return;
1760 }
1761 mCurrentCommandBuffer.pushConstants(mPipelineLayout, vk::ShaderStageFlagBits::eFragment, 0, sizeof(color), color.data());
1762}
1763
1765{
1767 return;
1768 }
1769 float size = mDisplay->cfgL().pointSize * mDownsampleFactor * factor;
1770 mCurrentCommandBuffer.pushConstants(mPipelineLayout, vk::ShaderStageFlagBits::eVertex, sizeof(std::array<float, 4>), sizeof(size), &size);
1771}
1772
1774{
1776 return;
1777 }
1778 mCurrentCommandBuffer.setLineWidth(mDisplay->cfgL().lineWidth * mDownsampleFactor * factor);
1779}
1780
1785
1786void GPUDisplayBackendVulkan::addFontSymbol(int32_t symbol, int32_t sizex, int32_t sizey, int32_t offsetx, int32_t offsety, int32_t advance, void* data)
1787{
1788 if (symbol != (int32_t)mFontSymbols.size()) {
1789 throw std::runtime_error("Incorrect symbol ID");
1790 }
1791 mFontSymbols.emplace_back(FontSymbolVulkan{{{sizex, sizey}, {offsetx, offsety}, advance}, nullptr, 0.f, 0.f, 0.f, 0.f});
1792 auto& buffer = mFontSymbols.back().data;
1793 if (sizex && sizey) {
1794 buffer.reset(new char[sizex * sizey]);
1795 memcpy(buffer.get(), data, sizex * sizey);
1796 }
1797}
1798
1800{
1801 int32_t maxSizeX = 0, maxSizeY = 0, maxBigX = 0, maxBigY = 0, maxRowY = 0;
1802 bool smooth = smoothFont();
1803 // Build a mega texture containing all fonts
1804 for (auto& symbol : mFontSymbols) {
1805 maxSizeX = std::max(maxSizeX, symbol.size[0]);
1806 maxSizeY = std::max(maxSizeY, symbol.size[1]);
1807 }
1808 uint32_t nn = ceil(std::sqrt(mFontSymbols.size()));
1809 int32_t sizex = nn * maxSizeX;
1810 int32_t sizey = nn * maxSizeY;
1811 std::unique_ptr<char[]> bigImage{new char[sizex * sizey]};
1812 memset(bigImage.get(), 0, sizex * sizey);
1813 int32_t rowy = 0, colx = 0;
1814 for (uint32_t i = 0; i < mFontSymbols.size(); i++) {
1815 auto& s = mFontSymbols[i];
1816 if (colx + s.size[0] > sizex) {
1817 colx = 0;
1818 rowy += maxRowY;
1819 maxRowY = 0;
1820 }
1821 for (int32_t k = 0; k < s.size[1]; k++) {
1822 for (int32_t j = 0; j < s.size[0]; j++) {
1823 int8_t val = s.data.get()[j + k * s.size[0]];
1824 if (!smooth) {
1825 val = val < 0 ? 0xFF : 0;
1826 }
1827 bigImage.get()[(colx + j) + (rowy + k) * sizex] = val;
1828 }
1829 }
1830 s.data.reset();
1831 s.x0 = colx;
1832 s.x1 = colx + s.size[0];
1833 s.y0 = rowy;
1834 s.y1 = rowy + s.size[1];
1835 maxBigX = std::max(maxBigX, colx + s.size[0]);
1836 maxBigY = std::max(maxBigY, rowy + s.size[1]);
1837 maxRowY = std::max(maxRowY, s.size[1]);
1838 colx += s.size[0];
1839 }
1840 if (maxBigX != sizex) {
1841 for (int32_t y = 1; y < maxBigY; y++) {
1842 memmove(bigImage.get() + y * maxBigX, bigImage.get() + y * sizex, maxBigX);
1843 }
1844 }
1845 sizex = maxBigX;
1846 sizey = maxBigY;
1847 for (uint32_t i = 0; i < mFontSymbols.size(); i++) {
1848 auto& s = mFontSymbols[i];
1849 s.x0 /= sizex;
1850 s.x1 /= sizex;
1851 s.y0 /= sizey;
1852 s.y1 /= sizey;
1853 }
1854
1855 mFontImage = createImage(sizex, sizey, bigImage.get(), sizex * sizey, vk::Format::eR8Unorm);
1857}
1858
1860{
1861 vk::DescriptorImageInfo imageInfo{};
1862 imageInfo.imageLayout = vk::ImageLayout::eShaderReadOnlyOptimal;
1863 imageInfo.imageView = mFontImage.view;
1864 imageInfo.sampler = mTextureSampler;
1865 for (uint32_t i = 0; i < mFramesInFlight; i++) {
1866 vk::WriteDescriptorSet descriptorWrite{};
1867 descriptorWrite.dstSet = mDescriptorSets[1][i];
1868 descriptorWrite.dstBinding = 2;
1869 descriptorWrite.dstArrayElement = 0;
1870 descriptorWrite.descriptorType = vk::DescriptorType::eCombinedImageSampler;
1871 descriptorWrite.descriptorCount = 1;
1872 descriptorWrite.pImageInfo = &imageInfo;
1873 mDevice.updateDescriptorSets(1, &descriptorWrite, 0, nullptr);
1874 }
1875}
1876
1877void GPUDisplayBackendVulkan::OpenGLPrint(const char* s, float x, float y, float* color, float scale)
1878{
1880 return;
1881 }
1882
1883 size_t firstVertex = mFontVertexBufferHost.size() / 4;
1884 if (smoothFont()) {
1885 scale *= 0.25f; // Font size is 48 to have nice bitmap, scale to size 12
1886 }
1887
1888 for (const char* c = s; *c; c++) {
1889 if ((int32_t)*c > (int32_t)mFontSymbols.size()) {
1890 GPUError("Trying to draw unsupported symbol: %d > %d\n", (int32_t)*c, (int32_t)mFontSymbols.size());
1891 continue;
1892 }
1893 const FontSymbolVulkan& sym = mFontSymbols[*c];
1894 if (sym.size[0] && sym.size[1]) {
1895 mHasDrawnText = true;
1896 float xpos = x + sym.offset[0] * scale;
1897 float ypos = y - (sym.size[1] - sym.offset[1]) * scale;
1898 float w = sym.size[0] * scale;
1899 float h = sym.size[1] * scale;
1900 float vertices[6][4] = {
1901 {xpos, mScreenHeight - 1 - ypos, sym.x0, sym.y1},
1902 {xpos, mScreenHeight - 1 - (ypos + h), sym.x0, sym.y0},
1903 {xpos + w, mScreenHeight - 1 - ypos, sym.x1, sym.y1},
1904 {xpos + w, mScreenHeight - 1 - ypos, sym.x1, sym.y1},
1905 {xpos, mScreenHeight - 1 - (ypos + h), sym.x0, sym.y0},
1906 {xpos + w, mScreenHeight - 1 - (ypos + h), sym.x1, sym.y0}};
1907 size_t oldSize = mFontVertexBufferHost.size();
1908 mFontVertexBufferHost.resize(oldSize + 4 * 6);
1909 memcpy(&mFontVertexBufferHost[oldSize], &vertices[0][0], sizeof(vertices));
1910 }
1911 x += (sym.advance >> 6) * scale; // shift is in 1/64th of a pixel
1912 }
1913
1914 size_t nVertices = mFontVertexBufferHost.size() / 4 - firstVertex;
1915
1916 if (nVertices) {
1917 auto& c = mTextDrawCommands;
1918 if (c.size() && c.back().color[0] == color[0] && c.back().color[1] == color[1] && c.back().color[2] == color[2] && c.back().color[3] == color[3]) {
1919 c.back().nVertices += nVertices;
1920 } else {
1921 c.emplace_back(TextDrawCommand{firstVertex, nVertices, {color[0], color[1], color[2], color[3]}});
1922 }
1923 }
1924}
1925
1926void GPUDisplayBackendVulkan::readImageToPixels(vk::Image image, vk::ImageLayout layout, std::vector<char>& pixels)
1927{
1928 uint32_t width = mScreenWidth * mDisplay->cfgR().screenshotScaleFactor;
1929 uint32_t height = mScreenHeight * mDisplay->cfgR().screenshotScaleFactor;
1930 static constexpr int32_t bytesPerPixel = 4;
1931 pixels.resize(width * height * bytesPerPixel);
1932
1933 vk::Image dstImage, dstImage2, src2;
1934 vk::DeviceMemory dstImageMemory, dstImageMemory2;
1935 createImageI(mDevice, mPhysicalDevice, dstImage, dstImageMemory, width, height, vk::Format::eR8G8B8A8Unorm, vk::ImageUsageFlagBits::eTransferDst, vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent, vk::ImageTiling::eLinear);
1936 vk::CommandBuffer cmdBuffer = getSingleTimeCommandBuffer();
1937 cmdImageMemoryBarrier(cmdBuffer, image, vk::AccessFlagBits::eMemoryRead, vk::AccessFlagBits::eTransferRead, layout, vk::ImageLayout::eTransferSrcOptimal, vk::PipelineStageFlagBits::eTransfer, vk::PipelineStageFlagBits::eTransfer);
1938 if (mDisplay->cfgR().screenshotScaleFactor != 1) {
1939 createImageI(mDevice, mPhysicalDevice, dstImage2, dstImageMemory2, width, height, mSurfaceFormat.format, vk::ImageUsageFlagBits::eColorAttachment | vk::ImageUsageFlagBits::eTransferSrc | vk::ImageUsageFlagBits::eTransferDst, vk::MemoryPropertyFlagBits::eDeviceLocal, vk::ImageTiling::eOptimal);
1940 cmdImageMemoryBarrier(cmdBuffer, dstImage2, {}, vk::AccessFlagBits::eTransferWrite, vk::ImageLayout::eUndefined, vk::ImageLayout::eTransferDstOptimal, vk::PipelineStageFlagBits::eTransfer, vk::PipelineStageFlagBits::eTransfer);
1941 vk::Offset3D blitSizeSrc = {(int32_t)mRenderWidth, (int32_t)mRenderHeight, 1};
1942 vk::Offset3D blitSizeDst = {(int32_t)width, (int32_t)height, 1};
1943 vk::ImageBlit imageBlitRegion{};
1944 imageBlitRegion.srcSubresource.aspectMask = vk::ImageAspectFlagBits::eColor;
1945 imageBlitRegion.srcSubresource.layerCount = 1;
1946 imageBlitRegion.srcOffsets[1] = blitSizeSrc;
1947 imageBlitRegion.dstSubresource.aspectMask = vk::ImageAspectFlagBits::eColor;
1948 imageBlitRegion.dstSubresource.layerCount = 1;
1949 imageBlitRegion.dstOffsets[1] = blitSizeDst;
1950 cmdBuffer.blitImage(image, vk::ImageLayout::eTransferSrcOptimal, dstImage2, vk::ImageLayout::eTransferDstOptimal, 1, &imageBlitRegion, vk::Filter::eLinear);
1951 src2 = dstImage2;
1952 cmdImageMemoryBarrier(cmdBuffer, dstImage2, vk::AccessFlagBits::eMemoryRead, vk::AccessFlagBits::eTransferRead, vk::ImageLayout::eTransferDstOptimal, vk::ImageLayout::eTransferSrcOptimal, vk::PipelineStageFlagBits::eTransfer, vk::PipelineStageFlagBits::eTransfer);
1953 } else {
1954 src2 = image;
1955 }
1956
1957 cmdImageMemoryBarrier(cmdBuffer, dstImage, {}, vk::AccessFlagBits::eTransferWrite, vk::ImageLayout::eUndefined, vk::ImageLayout::eTransferDstOptimal, vk::PipelineStageFlagBits::eTransfer, vk::PipelineStageFlagBits::eTransfer);
1958 vk::ImageCopy imageCopyRegion{};
1959 imageCopyRegion.srcSubresource.aspectMask = vk::ImageAspectFlagBits::eColor;
1960 imageCopyRegion.srcSubresource.layerCount = 1;
1961 imageCopyRegion.dstSubresource.aspectMask = vk::ImageAspectFlagBits::eColor;
1962 imageCopyRegion.dstSubresource.layerCount = 1;
1963 imageCopyRegion.extent.width = width;
1964 imageCopyRegion.extent.height = height;
1965 imageCopyRegion.extent.depth = 1;
1966 cmdBuffer.copyImage(src2, vk::ImageLayout::eTransferSrcOptimal, dstImage, vk::ImageLayout::eTransferDstOptimal, 1, &imageCopyRegion);
1967
1968 cmdImageMemoryBarrier(cmdBuffer, dstImage, vk::AccessFlagBits::eTransferWrite, vk::AccessFlagBits::eMemoryRead, vk::ImageLayout::eTransferDstOptimal, vk::ImageLayout::eGeneral, vk::PipelineStageFlagBits::eTransfer, vk::PipelineStageFlagBits::eTransfer);
1969 cmdImageMemoryBarrier(cmdBuffer, image, vk::AccessFlagBits::eTransferRead, vk::AccessFlagBits::eMemoryRead, vk::ImageLayout::eTransferSrcOptimal, layout, vk::PipelineStageFlagBits::eTransfer, vk::PipelineStageFlagBits::eTransfer);
1971
1972 vk::ImageSubresource subResource{vk::ImageAspectFlagBits::eColor, 0, 0};
1973 vk::SubresourceLayout subResourceLayout = mDevice.getImageSubresourceLayout(dstImage, subResource);
1974 const char* data;
1975 CHKERR(mDevice.mapMemory(dstImageMemory, 0, VK_WHOLE_SIZE, {}, (void**)&data));
1976 data += subResourceLayout.offset;
1977 for (uint32_t i = 0; i < height; i++) {
1978 memcpy(pixels.data() + i * width * bytesPerPixel, data + (height - i - 1) * width * bytesPerPixel, width * bytesPerPixel);
1979 }
1980 mDevice.unmapMemory(dstImageMemory);
1981 mDevice.freeMemory(dstImageMemory, nullptr);
1982 mDevice.destroyImage(dstImage, nullptr);
1983 if (mDisplay->cfgR().screenshotScaleFactor != 1) {
1984 mDevice.freeMemory(dstImageMemory2, nullptr);
1985 mDevice.destroyImage(dstImage2, nullptr);
1986 }
1987}
1988
1990{
1991 return 32;
1992}
1993
uint64_t vertex
Definition RawEventData.h:9
int32_t i
#define CHKERR(cmd)
#define LOAD_SHADER(file, ext)
int32_t retVal
HMM_INLINE hmm_mat4 HMM_Orthographic(float Left, float Right, float Bottom, float Top, float Near, float Far)
uint32_t j
Definition RawData.h:0
uint32_t c
Definition RawData.h:2
Class for time synchronization of RawReader instances.
void OpenGLPrint(const char *s, float x, float y, float *color, float scale) override
std::vector< vk::CommandBuffer > mCommandBuffersMix
std::vector< vk::CommandBuffer > mCommandBuffers
void setMixDescriptor(int32_t descriptorIndex, int32_t imageIndex)
std::vector< vk::Semaphore > mRenderFinishedSemaphore
void prepareDraw(const hmm_mat4 &proj, const hmm_mat4 &view, bool requestScreenshot, bool toMixBuffer, float includeMixImage) override
std::vector< vk::Pipeline > mPipelines
void resizeScene(uint32_t width, uint32_t height) override
void submitSingleTimeCommandBuffer(vk::CommandBuffer commandBuffer)
std::vector< VulkanImage > mDownsampleImages
void writeToBuffer(VulkanBuffer &buffer, size_t size, const void *srcData)
std::vector< vk::ImageView > mSwapChainImageViews
void pointSizeFactor(float factor) override
std::vector< FontSymbolVulkan > mFontSymbols
std::vector< vk::ImageView * > mRenderTargetView
std::vector< vk::Framebuffer > mFramebuffersTexture
void addFontSymbol(int32_t symbol, int32_t sizex, int32_t sizey, int32_t offsetx, int32_t offsety, int32_t advance, void *data) override
std::vector< vk::Framebuffer > mFramebuffersText
void mixImages(vk::CommandBuffer cmdBuffer, float mixSlaveImage)
void recreateRendering(bool forScreenshot=false, bool forMixing=false)
std::vector< VulkanBuffer > mFontVertexBuffer
void endFillCommandBuffer(vk::CommandBuffer &commandBuffer)
uint32_t drawVertices(const vboList &v, const drawType t) override
std::vector< vk::CommandBuffer > mCommandBuffersDownsample
void ActivateColor(std::array< float, 4 > &color) override
std::vector< vk::Semaphore > mMixFinishedSemaphore
std::vector< vk::Semaphore > mTextFinishedSemaphore
std::vector< VulkanBuffer > mUniformBuffersMat[3]
double checkDevice(vk::PhysicalDevice device, const std::vector< const char * > &reqDeviceExtensions)
void updateSwapChainDetails(const vk::PhysicalDevice &device)
void finishDraw(bool doScreenshot, bool toMixBuffer, float includeMixImage) override
void writeToImage(VulkanImage &image, const void *srcData, size_t srcSize)
std::vector< vk::DescriptorSet > mDescriptorSets[3]
vk::DescriptorSetLayout mUniformDescriptorTexture
void downsampleToFramebuffer(vk::CommandBuffer &commandBuffer)
vk::Extent2D chooseSwapExtent(const vk::SurfaceCapabilitiesKHR &capabilities)
std::vector< vk::Framebuffer > mFramebuffers
void readImageToPixels(vk::Image image, vk::ImageLayout layout, std::vector< char > &pixels)
VulkanImage createImage(uint32_t sizex, uint32_t sizey, const void *srcData=nullptr, size_t srcSize=0, vk::Format format=vk::Format::eR8G8B8A8Srgb)
std::vector< vk::Semaphore > mDownsampleFinishedSemaphore
std::vector< TextDrawCommand > mTextDrawCommands
std::unordered_map< std::string, vk::ShaderModule > mShaders
void finishFrame(bool doScreenshot, bool toMixBuffer, float includeMixImage) override
void createSwapChain(bool forScreenshot=false, bool forMixing=false)
std::vector< vk::Semaphore > mImageAvailableSemaphore
void startFillCommandBuffer(vk::CommandBuffer &commandBuffer, uint32_t imageIndex, bool toMixBuffer=false)
std::vector< vk::CommandBuffer > mCommandBuffersTexture
vk::DebugUtilsMessengerEXT mDebugMessenger
VulkanBuffer createBuffer(size_t size, const void *srcData=nullptr, vk::BufferUsageFlags type=vk::BufferUsageFlagBits::eVertexBuffer, int32_t deviceMemory=1)
void createOffscreenBuffers(bool forScreenshot=false, bool forMixing=false)
void lineWidthFactor(float factor) override
std::vector< vk::CommandBuffer > mCommandBuffersText
void loadDataToGPU(size_t totalVertizes) override
std::vector< VulkanBuffer > mUniformBuffersCol[3]
vecpod< DrawArraysIndirectCommand > mCmdBuffer
std::vector< int32_t > mIndirectSectorOffset
std::vector< char > mScreenshotPixels
float getDownsampleFactor(bool screenshot=false)
std::tuple< uint32_t, uint32_t, int32_t > vboList
virtual uint32_t getReqVulkanExtensions(const char **&p)
virtual void getSize(int32_t &width, int32_t &height)
virtual int32_t getVulkanSurface(void *instance, void *surface)
const GPUSettingsDisplayLight & cfgL() const
Definition GPUDisplay.h:59
const GPUParam * param()
Definition GPUDisplay.h:73
int32_t updateRenderPipeline() const
Definition GPUDisplay.h:64
const GPUSettingsDisplayRenderer & cfgR() const
Definition GPUDisplay.h:58
vecpod< vtx > * vertexBuffer()
Definition GPUDisplay.h:72
bool drawTextInCompatMode() const
Definition GPUDisplay.h:75
int32_t updateDrawCommands() const
Definition GPUDisplay.h:63
GPUDisplayFrontend * frontend()
Definition GPUDisplay.h:74
const vecpod< uint32_t > * vertexBufferCount() const
Definition GPUDisplay.h:67
const GPUSettingsDisplay & cfg() const
Definition GPUDisplay.h:61
vecpod< int32_t > * vertexBufferStart()
Definition GPUDisplay.h:66
GLeglImageOES image
Definition glcorearb.h:4021
GLint GLenum GLint x
Definition glcorearb.h:403
GLenum func
Definition glcorearb.h:778
GLint GLsizei count
Definition glcorearb.h:399
GLuint buffer
Definition glcorearb.h:655
GLsizeiptr size
Definition glcorearb.h:659
GLuint color
Definition glcorearb.h:1272
GLuint GLsizei const GLuint const GLintptr * offsets
Definition glcorearb.h:2595
const GLdouble * v
Definition glcorearb.h:832
GLsizei GLenum GLenum * types
Definition glcorearb.h:2516
GLint GLsizei GLsizei height
Definition glcorearb.h:270
GLint GLsizei width
Definition glcorearb.h:270
GLenum GLint * range
Definition glcorearb.h:1899
GLint GLint GLsizei GLint GLenum GLenum type
Definition glcorearb.h:275
GLboolean * data
Definition glcorearb.h:298
GLint GLint GLsizei GLint GLenum GLenum const void * pixels
Definition glcorearb.h:275
GLuint GLfloat * val
Definition glcorearb.h:1582
GLsizei const GLenum * attachments
Definition glcorearb.h:2492
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition glcorearb.h:2514
GLubyte GLubyte GLubyte GLubyte w
Definition glcorearb.h:852
GLint GLint GLsizei GLint GLenum format
Definition glcorearb.h:275
GLuint memory
Definition glcorearb.h:5234
GLsizeiptr const void GLenum usage
Definition glcorearb.h:659
#define QGET_LD_BINARY_SYMBOLS(filename)