graphics: migrate Vulkan backend to Vulkan-Hpp

This commit is contained in:
nmzik
2026-07-18 12:33:21 +02:00
parent 587cb238f0
commit 7905f2d12f
64 changed files with 4842 additions and 4518 deletions
+4 -4
View File
@@ -99,12 +99,12 @@ static std::atomic<RenderDocState> g_state = RenderDocState::Idle;
static std::atomic_bool g_init_done = false;
static std::atomic_bool g_unavailable_log = false;
static RenderDocDevicePointer GetRenderDocDevicePointer(VkInstance instance) {
static RenderDocDevicePointer GetRenderDocDevicePointer(vk::Instance instance) {
if (instance == nullptr) {
return nullptr;
}
return reinterpret_cast<void*>(instance);
return VulkanHandleToPointer(instance);
}
static bool BindRenderDocApi(HMODULE module) {
@@ -191,7 +191,7 @@ bool RenderDocIsLoaded() {
return g_api != nullptr;
}
void RenderDocSetActiveWindow(VkInstance instance, SDL_Window* window) {
void RenderDocSetActiveWindow(vk::Instance instance, SDL_Window* window) {
if (g_api == nullptr) {
return;
}
@@ -284,7 +284,7 @@ void RenderDocInit() {}
bool RenderDocIsLoaded() {
return false;
}
void RenderDocSetActiveWindow(VkInstance /*instance*/, SDL_Window* /*window*/) {}
void RenderDocSetActiveWindow(vk::Instance /*instance*/, SDL_Window* /*window*/) {}
void RenderDocRequestCapture() {}
void RenderDocOnPresent() {}
+2 -3
View File
@@ -2,8 +2,7 @@
#define EMULATOR_INCLUDE_EMULATOR_GRAPHICS_RENDERDOC_H_
#include "common/common.h"
#include <vulkan/vulkan_core.h>
#include "graphics/host_gpu/vulkanCommon.h"
struct SDL_Window;
@@ -11,7 +10,7 @@ namespace Libs::Graphics {
void RenderDocInit();
bool RenderDocIsLoaded();
void RenderDocSetActiveWindow(VkInstance instance, SDL_Window* window);
void RenderDocSetActiveWindow(vk::Instance instance, SDL_Window* window);
void RenderDocRequestCapture();
void RenderDocOnPresent();
+2 -3
View File
@@ -3,8 +3,7 @@
#include "common/abi.h"
#include "common/common.h"
struct VkSurfaceCapabilitiesKHR;
#include "graphics/host_gpu/vulkanCommon.h"
namespace Libs::Graphics {
@@ -13,7 +12,7 @@ class CommandBuffer;
struct VideoOutVulkanImage;
struct PreparedFrame;
VkSurfaceCapabilitiesKHR* VulkanGetSurfaceCapabilities();
vk::SurfaceCapabilitiesKHR* VulkanGetSurfaceCapabilities();
GraphicContext* WindowGetGraphicContext();
+103 -98
View File
@@ -31,6 +31,7 @@
#include "graphics/host_gpu/renderer/renderContext.h"
#include "graphics/host_gpu/utils.h"
#include "graphics/host_gpu/vma.h"
#include "graphics/host_gpu/vulkanCommon.h"
#include "graphics/presentation/renderDoc.h"
#include "graphics/presentation/videoOut.h"
#include "graphics/presentation/window.h"
@@ -48,7 +49,6 @@
#include <memory>
#include <string>
#include <vector>
#include <vulkan/vk_enum_string_helper.h>
#include <vulkan/vk_platform.h>
// IWYU pragma: no_include <intrin.h>
@@ -66,8 +66,8 @@ struct PreparedFrame {
class PreparedFramePool {
public:
void EnsureCapacity(uint32_t count, VkFormat format) {
if (count == 0 || format == VK_FORMAT_UNDEFINED) {
void EnsureCapacity(uint32_t count, vk::Format format) {
if (count == 0 || format == vk::Format::eUndefined) {
EXIT("prepared-frame pool requires at least one frame\n");
}
Common::LockGuard lock(m_mutex);
@@ -79,9 +79,9 @@ public:
}
}
VkFormat GetFormat() {
vk::Format GetFormat() {
Common::LockGuard lock(m_mutex);
if (m_format == VK_FORMAT_UNDEFINED) {
if (m_format == vk::Format::eUndefined) {
EXIT("prepared-frame pool has no presentation format\n");
}
return m_format;
@@ -130,7 +130,7 @@ private:
Common::CondVar m_available;
std::vector<std::unique_ptr<PreparedFrame>> m_frames;
std::deque<PreparedFrame*> m_free;
VkFormat m_format = VK_FORMAT_UNDEFINED;
vk::Format m_format = vk::Format::eUndefined;
};
static PreparedFramePool* GetPreparedFramePool() {
@@ -138,10 +138,10 @@ static PreparedFramePool* GetPreparedFramePool() {
return pool;
}
static void ConfigurePreparedFrame(PreparedFrame* frame, VkExtent2D extent, VkFormat format) {
static void ConfigurePreparedFrame(PreparedFrame* frame, vk::Extent2D extent, vk::Format format) {
EXIT_IF(frame == nullptr);
EXIT_IF(g_window_ctx == nullptr);
if (extent.width == 0 || extent.height == 0 || format == VK_FORMAT_UNDEFINED) {
if (extent.width == 0 || extent.height == 0 || format == vk::Format::eUndefined) {
EXIT("unsupported prepared frame, extent=%ux%u format=%d\n", extent.width, extent.height,
static_cast<int>(format));
}
@@ -163,21 +163,21 @@ static void ConfigurePreparedFrame(PreparedFrame* frame, VkExtent2D extent, VkFo
dst.format = format;
dst.layers = 1;
dst.mip_levels = 1;
dst.layout = VK_IMAGE_LAYOUT_UNDEFINED;
dst.memory.property = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
dst.layout = vk::ImageLayout::eUndefined;
dst.memory.property = vk::MemoryPropertyFlagBits::eDeviceLocal;
VkImageCreateInfo create {};
create.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
create.imageType = VK_IMAGE_TYPE_2D;
vk::ImageCreateInfo create {};
create.sType = vk::StructureType::eImageCreateInfo;
create.imageType = vk::ImageType::e2D;
create.extent = {dst.extent.width, dst.extent.height, 1};
create.mipLevels = 1;
create.arrayLayers = 1;
create.format = dst.format;
create.tiling = VK_IMAGE_TILING_OPTIMAL;
create.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
create.usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
create.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
create.samples = VK_SAMPLE_COUNT_1_BIT;
create.tiling = vk::ImageTiling::eOptimal;
create.initialLayout = vk::ImageLayout::eUndefined;
create.usage = vk::ImageUsageFlagBits::eTransferSrc | vk::ImageUsageFlagBits::eTransferDst;
create.sharingMode = vk::SharingMode::eExclusive;
create.samples = vk::SampleCountFlagBits::e1;
if (!VulkanCreateImage(ctx, &create, &dst, &dst.memory)) {
EXIT("failed to allocate prepared presentation image, extent=%ux%u format=%d\n",
dst.extent.width, dst.extent.height, static_cast<int>(dst.format));
@@ -186,11 +186,11 @@ static void ConfigurePreparedFrame(PreparedFrame* frame, VkExtent2D extent, VkFo
VulkanSwapchain::~VulkanSwapchain() = default;
[[maybe_unused]] static VkSwapchainKHR VulkanCreateSwapchainInternal(
VkDevice device, VkSurfaceKHR surface, uint32_t width, uint32_t height, uint32_t image_count,
SurfaceCapabilities* r, VkFormat* swapchain_format, VkExtent2D* swapchain_extent,
std::unique_ptr<VkImage[]>* swapchain_images,
std::unique_ptr<VkImageView[]>* swapchain_image_views, uint32_t* swapchain_images_count) {
[[maybe_unused]] static vk::SwapchainKHR VulkanCreateSwapchainInternal(
vk::Device device, vk::SurfaceKHR surface, uint32_t width, uint32_t height,
uint32_t image_count, SurfaceCapabilities* r, vk::Format* swapchain_format,
vk::Extent2D* swapchain_extent, std::unique_ptr<vk::Image[]>* swapchain_images,
std::unique_ptr<vk::ImageView[]>* swapchain_image_views, uint32_t* swapchain_images_count) {
EXIT_IF(device == nullptr);
EXIT_IF(surface == nullptr);
EXIT_IF(r == nullptr);
@@ -202,7 +202,7 @@ VulkanSwapchain::~VulkanSwapchain() = default;
EXIT_NOT_IMPLEMENTED(r->formats.empty());
VkExtent2D extent {};
vk::Extent2D extent {};
extent.width = std::clamp(width, r->capabilities.minImageExtent.width,
r->capabilities.maxImageExtent.width);
extent.height = std::clamp(height, r->capabilities.minImageExtent.height,
@@ -211,19 +211,19 @@ VulkanSwapchain::~VulkanSwapchain() = default;
image_count =
std::clamp(image_count, r->capabilities.minImageCount, r->capabilities.maxImageCount);
VkSwapchainCreateInfoKHR create_info {};
create_info.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
vk::SwapchainCreateInfoKHR create_info {};
create_info.sType = vk::StructureType::eSwapchainCreateInfoKHR;
create_info.pNext = nullptr;
create_info.flags = 0;
create_info.flags = {};
create_info.surface = surface;
create_info.minImageCount = image_count;
if (r->format_unorm_bgra32) {
create_info.imageFormat = VK_FORMAT_B8G8R8A8_UNORM;
create_info.imageColorSpace = VK_COLOR_SPACE_SRGB_NONLINEAR_KHR;
create_info.imageFormat = vk::Format::eB8G8R8A8Unorm;
create_info.imageColorSpace = vk::ColorSpaceKHR::eSrgbNonlinear;
} else if (r->format_srgb_bgra32) {
create_info.imageFormat = VK_FORMAT_B8G8R8A8_SRGB;
create_info.imageColorSpace = VK_COLOR_SPACE_SRGB_NONLINEAR_KHR;
create_info.imageFormat = vk::Format::eB8G8R8A8Srgb;
create_info.imageColorSpace = vk::ColorSpaceKHR::eSrgbNonlinear;
} else {
const auto& format = r->formats[0];
create_info.imageFormat = format.format;
@@ -232,49 +232,50 @@ VulkanSwapchain::~VulkanSwapchain() = default;
create_info.imageExtent = extent;
create_info.imageArrayLayers = 1;
create_info.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
create_info.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
create_info.imageUsage =
vk::ImageUsageFlagBits::eColorAttachment | vk::ImageUsageFlagBits::eTransferDst;
create_info.imageSharingMode = vk::SharingMode::eExclusive;
create_info.queueFamilyIndexCount = 0;
create_info.pQueueFamilyIndices = nullptr;
create_info.preTransform = r->capabilities.currentTransform;
create_info.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
create_info.presentMode = VK_PRESENT_MODE_FIFO_KHR;
create_info.compositeAlpha = vk::CompositeAlphaFlagBitsKHR::eOpaque;
create_info.presentMode = vk::PresentModeKHR::eFifo;
create_info.clipped = VK_TRUE;
create_info.oldSwapchain = nullptr;
*swapchain_format = create_info.imageFormat;
*swapchain_extent = extent;
VkSwapchainKHR swapchain = nullptr;
vk::SwapchainKHR swapchain = nullptr;
vkCreateSwapchainKHR(device, &create_info, nullptr, &swapchain);
device.createSwapchainKHR(&create_info, nullptr, &swapchain);
vkGetSwapchainImagesKHR(device, swapchain, swapchain_images_count, nullptr);
device.getSwapchainImagesKHR(swapchain, swapchain_images_count, nullptr);
EXIT_NOT_IMPLEMENTED(*swapchain_images_count == 0);
*swapchain_images = std::make_unique<VkImage[]>(*swapchain_images_count);
vkGetSwapchainImagesKHR(device, swapchain, swapchain_images_count, swapchain_images->get());
*swapchain_images = std::make_unique<vk::Image[]>(*swapchain_images_count);
device.getSwapchainImagesKHR(swapchain, swapchain_images_count, swapchain_images->get());
*swapchain_image_views = std::make_unique<VkImageView[]>(*swapchain_images_count);
*swapchain_image_views = std::make_unique<vk::ImageView[]>(*swapchain_images_count);
for (uint32_t i = 0; i < *swapchain_images_count; i++) {
VkImageViewCreateInfo create_info {};
create_info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
vk::ImageViewCreateInfo create_info {};
create_info.sType = vk::StructureType::eImageViewCreateInfo;
create_info.pNext = nullptr;
create_info.flags = 0;
create_info.flags = {};
create_info.image = (*swapchain_images)[i];
create_info.viewType = VK_IMAGE_VIEW_TYPE_2D;
create_info.viewType = vk::ImageViewType::e2D;
create_info.format = *swapchain_format;
create_info.components.r = VK_COMPONENT_SWIZZLE_IDENTITY;
create_info.components.g = VK_COMPONENT_SWIZZLE_IDENTITY;
create_info.components.b = VK_COMPONENT_SWIZZLE_IDENTITY;
create_info.components.a = VK_COMPONENT_SWIZZLE_IDENTITY;
create_info.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
create_info.components.r = vk::ComponentSwizzle::eIdentity;
create_info.components.g = vk::ComponentSwizzle::eIdentity;
create_info.components.b = vk::ComponentSwizzle::eIdentity;
create_info.components.a = vk::ComponentSwizzle::eIdentity;
create_info.subresourceRange.aspectMask = vk::ImageAspectFlagBits::eColor;
create_info.subresourceRange.baseArrayLayer = 0;
create_info.subresourceRange.baseMipLevel = 0;
create_info.subresourceRange.layerCount = 1;
create_info.subresourceRange.levelCount = 1;
vkCreateImageView(device, &create_info, nullptr, &((*swapchain_image_views)[i]));
device.createImageView(&create_info, nullptr, &((*swapchain_image_views)[i]));
}
return swapchain;
@@ -302,21 +303,21 @@ VulkanSwapchain* VulkanCreateSwapchain(GraphicContext* ctx, uint32_t image_count
s->current_index = static_cast<uint32_t>(-1);
s->present_frame = 0;
VkSemaphoreCreateInfo semaphore_info {};
semaphore_info.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
vk::SemaphoreCreateInfo semaphore_info {};
semaphore_info.sType = vk::StructureType::eSemaphoreCreateInfo;
semaphore_info.pNext = nullptr;
semaphore_info.flags = 0;
semaphore_info.flags = {};
s->image_acquired_semaphores = std::make_unique<VkSemaphore[]>(s->swapchain_images_count);
s->render_complete_semaphores = std::make_unique<VkSemaphore[]>(s->swapchain_images_count);
s->image_acquired_semaphores = std::make_unique<vk::Semaphore[]>(s->swapchain_images_count);
s->render_complete_semaphores = std::make_unique<vk::Semaphore[]>(s->swapchain_images_count);
for (uint32_t i = 0; i < s->swapchain_images_count; i++) {
auto result = vkCreateSemaphore(ctx->device, &semaphore_info, nullptr,
&s->image_acquired_semaphores[i]);
EXIT_NOT_IMPLEMENTED(result != VK_SUCCESS);
auto result =
ctx->device.createSemaphore(&semaphore_info, nullptr, &s->image_acquired_semaphores[i]);
EXIT_NOT_IMPLEMENTED(result != vk::Result::eSuccess);
result = vkCreateSemaphore(ctx->device, &semaphore_info, nullptr,
&s->render_complete_semaphores[i]);
EXIT_NOT_IMPLEMENTED(result != VK_SUCCESS);
result = ctx->device.createSemaphore(&semaphore_info, nullptr,
&s->render_complete_semaphores[i]);
EXIT_NOT_IMPLEMENTED(result != vk::Result::eSuccess);
}
GetPreparedFramePool()->EnsureCapacity(s->swapchain_images_count, s->swapchain_format);
@@ -334,27 +335,27 @@ static void VulkanDeleteSwapchain(GraphicContext* ctx, VulkanSwapchain* s) {
if (s->image_acquired_semaphores != nullptr) {
for (uint32_t i = 0; i < s->swapchain_images_count; i++) {
if (s->image_acquired_semaphores[i] != nullptr) {
vkDestroySemaphore(ctx->device, s->image_acquired_semaphores[i], nullptr);
ctx->device.destroySemaphore(s->image_acquired_semaphores[i], nullptr);
}
}
}
if (s->render_complete_semaphores != nullptr) {
for (uint32_t i = 0; i < s->swapchain_images_count; i++) {
if (s->render_complete_semaphores[i] != nullptr) {
vkDestroySemaphore(ctx->device, s->render_complete_semaphores[i], nullptr);
ctx->device.destroySemaphore(s->render_complete_semaphores[i], nullptr);
}
}
}
if (s->swapchain_image_views != nullptr) {
for (uint32_t i = 0; i < s->swapchain_images_count; i++) {
if (s->swapchain_image_views[i] != nullptr) {
vkDestroyImageView(ctx->device, s->swapchain_image_views[i], nullptr);
ctx->device.destroyImageView(s->swapchain_image_views[i], nullptr);
}
}
}
if (s->swapchain != nullptr) {
vkDestroySwapchainKHR(ctx->device, s->swapchain, nullptr);
ctx->device.destroySwapchainKHR(s->swapchain, nullptr);
}
}
@@ -395,7 +396,7 @@ static void ValidatePreparedCommand(CommandBuffer* buffer) {
PreparedFrame* WindowPrepareFrame(CommandBuffer* buffer, VideoOutVulkanImage* image) {
KYTY_PROFILER_FUNCTION();
ValidatePreparedCommand(buffer);
if (image->format == VK_FORMAT_UNDEFINED) {
if (image->format == vk::Format::eUndefined) {
EXIT("unsupported presentation source, image=%p\n", static_cast<const void*>(image));
}
@@ -405,8 +406,7 @@ PreparedFrame* WindowPrepareFrame(CommandBuffer* buffer, VideoOutVulkanImage* im
ConfigurePreparedFrame(frame, image->extent, image->format);
const std::array copies {ImageImageCopy {
.src_image = image, .width = image->extent.width, .height = image->extent.height}};
UtilImageToImage(buffer, copies, &frame->image,
static_cast<uint64_t>(VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL));
UtilImageToImage(buffer, copies, &frame->image, vk::ImageLayout::eTransferSrcOptimal);
return frame;
}
@@ -419,7 +419,8 @@ PreparedFrame* WindowPrepareBlankFrame(CommandBuffer* buffer, uint32_t width, ui
auto* frame = pool->Acquire();
Common::LockGuard render_lock(g_render_ctx->GetMutex());
ConfigurePreparedFrame(frame, {width, height}, format);
const VkClearColorValue clear {{0.0f, 0.0f, 0.0f, opaque ? 1.0f : 0.0f}};
vk::ClearColorValue clear {};
clear.float32[3] = opaque ? 1.0f : 0.0f;
UtilClearColorImage(buffer, &frame->image, clear);
return frame;
}
@@ -452,18 +453,20 @@ void WindowPresentFrame(PreparedFrame* frame) {
swapchain->current_index = static_cast<uint32_t>(-1);
auto result = vkAcquireNextImageKHR(
g_window_ctx->graphic_ctx.device, swapchain->swapchain, UINT64_MAX,
swapchain->image_acquired_semaphores[present_frame], nullptr, &swapchain->current_index);
auto result = g_window_ctx->graphic_ctx.device.acquireNextImageKHR(
swapchain->swapchain, UINT64_MAX, swapchain->image_acquired_semaphores[present_frame],
nullptr, &swapchain->current_index);
switch (result) {
case VK_SUCCESS: break;
case VK_SUBOPTIMAL_KHR: LOGF("vkAcquireNextImageKHR returned VK_SUBOPTIMAL_KHR\n"); break;
case VK_ERROR_OUT_OF_DATE_KHR:
LOGF("vkAcquireNextImageKHR returned VK_ERROR_OUT_OF_DATE_KHR\n");
case vk::Result::eSuccess: break;
case vk::Result::eSuboptimalKHR:
LOGF("vkAcquireNextImageKHR returned vk::Result::eSuboptimalKHR\n");
break;
case vk::Result::eErrorOutOfDateKHR:
LOGF("vkAcquireNextImageKHR returned vk::Result::eErrorOutOfDateKHR\n");
VulkanRecreateSwapchain();
return;
default: EXIT("vkAcquireNextImageKHR failed: %s\n", string_VkResult(result));
default: EXIT("vkAcquireNextImageKHR failed: %s\n", VulkanToString(result).c_str());
}
EXIT_NOT_IMPLEMENTED(swapchain->current_index == static_cast<uint32_t>(-1));
if (frame->present_commands == nullptr) {
@@ -472,41 +475,41 @@ void WindowPresentFrame(PreparedFrame* frame) {
frame->present_commands->WaitForFenceAndReset();
auto& buffer = *frame->present_commands;
auto* vk_buffer = buffer.GetPool()->buffers[buffer.GetIndex()];
auto vk_buffer = buffer.GetPool()->buffers[buffer.GetIndex()];
buffer.Begin();
UtilBlitPreparedImage(&buffer, &frame->image, swapchain);
VkImageMemoryBarrier pre_present_barrier {};
pre_present_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
vk::ImageMemoryBarrier pre_present_barrier {};
pre_present_barrier.sType = vk::StructureType::eImageMemoryBarrier;
pre_present_barrier.pNext = nullptr;
pre_present_barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
pre_present_barrier.dstAccessMask = VK_ACCESS_MEMORY_READ_BIT;
pre_present_barrier.oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
pre_present_barrier.newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
pre_present_barrier.srcAccessMask = vk::AccessFlagBits::eTransferWrite;
pre_present_barrier.dstAccessMask = vk::AccessFlagBits::eMemoryRead;
pre_present_barrier.oldLayout = vk::ImageLayout::eTransferDstOptimal;
pre_present_barrier.newLayout = vk::ImageLayout::ePresentSrcKHR;
pre_present_barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
pre_present_barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
pre_present_barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
pre_present_barrier.subresourceRange.aspectMask = vk::ImageAspectFlagBits::eColor;
pre_present_barrier.subresourceRange.baseMipLevel = 0;
pre_present_barrier.subresourceRange.levelCount = 1;
pre_present_barrier.subresourceRange.baseArrayLayer = 0;
pre_present_barrier.subresourceRange.layerCount = 1;
pre_present_barrier.image = swapchain->swapchain_images[swapchain->current_index];
vkCmdPipelineBarrier(vk_buffer, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0, 0, nullptr, 0, nullptr, 1,
&pre_present_barrier);
vk_buffer.pipelineBarrier(vk::PipelineStageFlagBits::eAllCommands,
vk::PipelineStageFlagBits::eBottomOfPipe, vk::DependencyFlags {}, 0,
nullptr, 0, nullptr, 1, &pre_present_barrier);
buffer.End();
auto render_complete_semaphore =
swapchain->render_complete_semaphores[swapchain->current_index];
const VkPipelineStageFlags wait_stage = VK_PIPELINE_STAGE_TRANSFER_BIT;
const vk::PipelineStageFlags wait_stage = vk::PipelineStageFlagBits::eTransfer;
buffer.ExecuteWithSemaphore(swapchain->image_acquired_semaphores[present_frame], wait_stage,
render_complete_semaphore);
VkPresentInfoKHR present;
present.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
vk::PresentInfoKHR present;
present.sType = vk::StructureType::ePresentInfoKHR;
present.pNext = nullptr;
present.swapchainCount = 1;
present.pSwapchains = &swapchain->swapchain;
@@ -520,18 +523,20 @@ void WindowPresentFrame(PreparedFrame* frame) {
if (queue.mutex != nullptr) {
queue.mutex->Lock();
}
result = vkQueuePresentKHR(queue.vk_queue, &present);
result = queue.vk_queue.presentKHR(&present);
if (queue.mutex != nullptr) {
queue.mutex->Unlock();
}
switch (result) {
case VK_SUCCESS: break;
case VK_SUBOPTIMAL_KHR: LOGF("vkQueuePresentKHR returned VK_SUBOPTIMAL_KHR\n"); break;
case VK_ERROR_OUT_OF_DATE_KHR:
LOGF("vkQueuePresentKHR returned VK_ERROR_OUT_OF_DATE_KHR\n");
case vk::Result::eSuccess: break;
case vk::Result::eSuboptimalKHR:
LOGF("vkQueuePresentKHR returned vk::Result::eSuboptimalKHR\n");
break;
case vk::Result::eErrorOutOfDateKHR:
LOGF("vkQueuePresentKHR returned vk::Result::eErrorOutOfDateKHR\n");
VulkanRecreateSwapchain();
return;
default: EXIT("vkQueuePresentKHR failed: %s\n", string_VkResult(result));
default: EXIT("vkQueuePresentKHR failed: %s\n", VulkanToString(result).c_str());
}
swapchain->present_frame = (present_frame + 1u) % swapchain->swapchain_images_count;
+251 -245
View File
@@ -30,6 +30,7 @@
#include "graphics/host_gpu/renderer/render.h"
#include "graphics/host_gpu/utils.h"
#include "graphics/host_gpu/vma.h"
#include "graphics/host_gpu/vulkanCommon.h"
#include "graphics/presentation/renderDoc.h"
#include "graphics/presentation/videoOut.h"
#include "graphics/presentation/window.h"
@@ -45,7 +46,6 @@
#include <memory>
#include <string>
#include <vector>
#include <vulkan/vk_enum_string_helper.h>
#include <vulkan/vk_platform.h>
// IWYU pragma: no_include <intrin.h>
@@ -58,13 +58,13 @@ namespace Libs::Graphics {
struct VulkanExtensions {
bool enable_validation_layers = false;
std::vector<const char*> required_extensions;
std::vector<VkExtensionProperties> available_extensions;
std::vector<const char*> required_layers;
std::vector<VkLayerProperties> available_layers;
std::vector<const char*> required_extensions;
std::vector<vk::ExtensionProperties> available_extensions;
std::vector<const char*> required_layers;
std::vector<vk::LayerProperties> available_layers;
};
static bool HasExtension(const std::vector<VkExtensionProperties>& extensions, const char* name) {
static bool HasExtension(const std::vector<vk::ExtensionProperties>& extensions, const char* name) {
return std::any_of(extensions.begin(), extensions.end(),
[name](const auto& ext) { return strcmp(ext.extensionName, name) == 0; });
}
@@ -74,54 +74,52 @@ static bool HasExtension(const std::vector<const char*>& extensions, const char*
[name](const char* ext) { return strcmp(ext, name) == 0; });
}
static bool HasLayer(const std::vector<VkLayerProperties>& layers, const char* name) {
static bool HasLayer(const std::vector<vk::LayerProperties>& layers, const char* name) {
return std::any_of(layers.begin(), layers.end(),
[name](const auto& layer) { return strcmp(layer.layerName, name) == 0; });
}
void VulkanGetSurfaceCapabilities(VkPhysicalDevice physical_device, VkSurfaceKHR surface,
void VulkanGetSurfaceCapabilities(vk::PhysicalDevice physical_device, vk::SurfaceKHR surface,
SurfaceCapabilities* r) {
vkGetPhysicalDeviceSurfaceCapabilitiesKHR(physical_device, surface, &r->capabilities);
physical_device.getSurfaceCapabilitiesKHR(surface, &r->capabilities);
uint32_t formats_count = 0;
vkGetPhysicalDeviceSurfaceFormatsKHR(physical_device, surface, &formats_count, nullptr);
physical_device.getSurfaceFormatsKHR(surface, &formats_count, nullptr);
EXIT_NOT_IMPLEMENTED(formats_count == 0);
r->formats = std::vector<VkSurfaceFormatKHR>(formats_count); // @suppress("Ambiguous problem")
vkGetPhysicalDeviceSurfaceFormatsKHR(physical_device, surface, &formats_count,
r->formats.data());
r->formats = std::vector<vk::SurfaceFormatKHR>(formats_count); // @suppress("Ambiguous problem")
physical_device.getSurfaceFormatsKHR(surface, &formats_count, r->formats.data());
uint32_t present_modes_count = 0;
vkGetPhysicalDeviceSurfacePresentModesKHR(physical_device, surface, &present_modes_count,
nullptr);
physical_device.getSurfacePresentModesKHR(surface, &present_modes_count, nullptr);
EXIT_NOT_IMPLEMENTED(present_modes_count == 0);
r->present_modes =
std::vector<VkPresentModeKHR>(present_modes_count); // @suppress("Ambiguous problem")
vkGetPhysicalDeviceSurfacePresentModesKHR(physical_device, surface, &present_modes_count,
std::vector<vk::PresentModeKHR>(present_modes_count); // @suppress("Ambiguous problem")
physical_device.getSurfacePresentModesKHR(surface, &present_modes_count,
r->present_modes.data());
r->format_srgb_bgra32 = false;
for (const auto& f: r->formats) {
if (f.format == VK_FORMAT_B8G8R8A8_SRGB &&
f.colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR) {
if (f.format == vk::Format::eB8G8R8A8Srgb &&
f.colorSpace == vk::ColorSpaceKHR::eSrgbNonlinear) {
r->format_srgb_bgra32 = true;
break;
}
if (f.format == VK_FORMAT_B8G8R8A8_UNORM &&
f.colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR) {
if (f.format == vk::Format::eB8G8R8A8Unorm &&
f.colorSpace == vk::ColorSpaceKHR::eSrgbNonlinear) {
r->format_unorm_bgra32 = true;
break;
}
}
}
static bool CheckFormat(VkPhysicalDevice device, VkFormat format, bool tile,
VkFormatFeatureFlags features) {
VkFormatProperties format_props {};
vkGetPhysicalDeviceFormatProperties(device, format, &format_props);
static bool CheckFormat(vk::PhysicalDevice device, vk::Format format, bool tile,
vk::FormatFeatureFlags features) {
vk::FormatProperties format_props {};
device.getFormatProperties(format, &format_props);
const auto supported_features =
(tile ? format_props.optimalTilingFeatures : format_props.linearTilingFeatures);
@@ -175,7 +173,7 @@ static void VulkanDumpQueues(const VulkanQueues& qs) {
}
}
static VulkanQueues VulkanFindQueues(VkPhysicalDevice device, VkSurfaceKHR surface,
static VulkanQueues VulkanFindQueues(vk::PhysicalDevice device, vk::SurfaceKHR surface,
uint32_t graphics_num, uint32_t compute_num,
uint32_t transfer_num, uint32_t present_num) {
EXIT_IF(device == nullptr);
@@ -184,28 +182,28 @@ static VulkanQueues VulkanFindQueues(VkPhysicalDevice device, VkSurfaceKHR surfa
VulkanQueues qs;
uint32_t queue_family_count = 0;
vkGetPhysicalDeviceQueueFamilyProperties(device, &queue_family_count, nullptr);
std::vector<VkQueueFamilyProperties> queue_families(queue_family_count);
vkGetPhysicalDeviceQueueFamilyProperties(device, &queue_family_count, queue_families.data());
device.getQueueFamilyProperties(&queue_family_count, nullptr);
std::vector<vk::QueueFamilyProperties> queue_families(queue_family_count);
device.getQueueFamilyProperties(&queue_family_count, queue_families.data());
qs.family_count = queue_family_count;
uint32_t family = 0;
for (auto& f: queue_families) {
VkBool32 presentation_supported = VK_FALSE;
vkGetPhysicalDeviceSurfaceSupportKHR(device, family, surface, &presentation_supported);
vk::Bool32 presentation_supported = VK_FALSE;
device.getSurfaceSupportKHR(family, surface, &presentation_supported);
LOGF("\tqueue family: %s [count = %u], [present = %s]\n",
string_VkQueueFlags(f.queueFlags).c_str(), f.queueCount,
VulkanToString(f.queueFlags).c_str(), f.queueCount,
(presentation_supported == VK_TRUE ? "true" : "false"));
for (uint32_t i = 0; i < f.queueCount; i++) {
QueueInfo info;
info.family = family;
info.index = i;
info.graphics = (f.queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0;
info.compute = (f.queueFlags & VK_QUEUE_COMPUTE_BIT) != 0;
info.transfer = (f.queueFlags & VK_QUEUE_TRANSFER_BIT) != 0;
info.graphics = static_cast<bool>(f.queueFlags & vk::QueueFlagBits::eGraphics);
info.compute = static_cast<bool>(f.queueFlags & vk::QueueFlagBits::eCompute);
info.transfer = static_cast<bool>(f.queueFlags & vk::QueueFlagBits::eTransfer);
info.present = (presentation_supported == VK_TRUE);
qs.available.push_back(info);
@@ -253,10 +251,10 @@ static VulkanQueues VulkanFindQueues(VkPhysicalDevice device, VkSurfaceKHR surfa
}
// NOLINTNEXTLINE(readability-function-cognitive-complexity)
static void VulkanFindPhysicalDevice(VkInstance instance, VkSurfaceKHR surface,
static void VulkanFindPhysicalDevice(vk::Instance instance, vk::SurfaceKHR surface,
const std::vector<const char*>& device_extensions,
SurfaceCapabilities* out_capabilities,
VkPhysicalDevice* out_device, VulkanQueues* out_queues) {
vk::PhysicalDevice* out_device, VulkanQueues* out_queues) {
EXIT_IF(instance == nullptr);
EXIT_IF(surface == nullptr);
EXIT_IF(out_capabilities == nullptr);
@@ -264,23 +262,23 @@ static void VulkanFindPhysicalDevice(VkInstance instance, VkSurfaceKHR surface,
EXIT_IF(out_queues == nullptr);
uint32_t devices_count = 0;
vkEnumeratePhysicalDevices(instance, &devices_count, nullptr);
instance.enumeratePhysicalDevices(&devices_count, nullptr);
EXIT_NOT_IMPLEMENTED(devices_count == 0);
std::vector<VkPhysicalDevice> devices(devices_count);
vkEnumeratePhysicalDevices(instance, &devices_count, devices.data());
std::vector<vk::PhysicalDevice> devices(devices_count);
instance.enumeratePhysicalDevices(&devices_count, devices.data());
VkPhysicalDevice best_device = nullptr;
VulkanQueues best_queues;
vk::PhysicalDevice best_device = nullptr;
VulkanQueues best_queues;
for (const auto& device: devices) {
bool skip_device = false;
VkPhysicalDeviceProperties device_properties {};
vkGetPhysicalDeviceProperties(device, &device_properties);
vk::PhysicalDeviceProperties device_properties {};
device.getProperties(&device_properties);
LOGF("Vulkan device: %s\n", device_properties.deviceName);
LOGF("Vulkan device: %s\n", device_properties.deviceName.data());
if (device_properties.apiVersion < VULKAN_TARGET_API_VERSION) {
LOGF("Vulkan %u.%u is required, but device supports only %u.%u.%u\n",
VK_VERSION_MAJOR(VULKAN_TARGET_API_VERSION),
@@ -291,30 +289,29 @@ static void VulkanFindPhysicalDevice(VkInstance instance, VkSurfaceKHR surface,
continue;
}
VkPhysicalDeviceFeatures2 device_features2 {};
vk::PhysicalDeviceFeatures2 device_features2 {};
VkPhysicalDeviceVulkan13Features features13 {};
features13.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_3_FEATURES;
vk::PhysicalDeviceVulkan13Features features13 {};
features13.sType = vk::StructureType::ePhysicalDeviceVulkan13Features;
features13.pNext = nullptr;
VkPhysicalDeviceColorWriteEnableFeaturesEXT color_write_ext {};
color_write_ext.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_COLOR_WRITE_ENABLE_FEATURES_EXT;
vk::PhysicalDeviceColorWriteEnableFeaturesEXT color_write_ext {};
color_write_ext.sType = vk::StructureType::ePhysicalDeviceColorWriteEnableFeaturesEXT;
color_write_ext.pNext = nullptr;
VkPhysicalDeviceDepthClipControlFeaturesEXT depth_clip_control {};
depth_clip_control.sType =
VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DEPTH_CLIP_CONTROL_FEATURES_EXT;
vk::PhysicalDeviceDepthClipControlFeaturesEXT depth_clip_control {};
depth_clip_control.sType = vk::StructureType::ePhysicalDeviceDepthClipControlFeaturesEXT;
depth_clip_control.pNext = &color_write_ext;
VkPhysicalDeviceVulkan12Features features12 {};
features12.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES;
vk::PhysicalDeviceVulkan12Features features12 {};
features12.sType = vk::StructureType::ePhysicalDeviceVulkan12Features;
features12.pNext = &depth_clip_control;
features13.pNext = &features12;
device_features2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
device_features2.sType = vk::StructureType::ePhysicalDeviceFeatures2;
device_features2.pNext = &features13;
vkGetPhysicalDeviceFeatures2(device, &device_features2);
device.getFeatures2(&device_features2);
auto qs = VulkanFindQueues(
device, surface, GraphicContext::QUEUE_GFX_NUM, GraphicContext::QUEUE_COMPUTE_NUM,
@@ -391,13 +388,13 @@ static void VulkanFindPhysicalDevice(VkInstance instance, VkSurfaceKHR surface,
if (!skip_device) {
uint32_t extensions_count = 0;
vkEnumerateDeviceExtensionProperties(device, nullptr, &extensions_count, nullptr);
device.enumerateDeviceExtensionProperties(nullptr, &extensions_count, nullptr);
EXIT_NOT_IMPLEMENTED(extensions_count == 0);
std::vector<VkExtensionProperties> available_extensions(extensions_count);
vkEnumerateDeviceExtensionProperties(device, nullptr, &extensions_count,
available_extensions.data());
std::vector<vk::ExtensionProperties> available_extensions(extensions_count);
device.enumerateDeviceExtensionProperties(nullptr, &extensions_count,
available_extensions.data());
for (const char* ext: device_extensions) {
if (!HasExtension(available_extensions, ext)) {
@@ -408,7 +405,7 @@ static void VulkanFindPhysicalDevice(VkInstance instance, VkSurfaceKHR surface,
if (skip_device) {
for (const auto& ext: available_extensions) {
LOGF("Vulkan available extension: %s, version = %u\n", ext.extensionName,
LOGF("Vulkan available extension: %s, version = %u\n", ext.extensionName.data(),
ext.specVersion);
}
}
@@ -417,87 +414,87 @@ static void VulkanFindPhysicalDevice(VkInstance instance, VkSurfaceKHR surface,
if (!skip_device) {
VulkanGetSurfaceCapabilities(device, surface, out_capabilities);
if ((out_capabilities->capabilities.supportedUsageFlags &
VK_IMAGE_USAGE_TRANSFER_DST_BIT) == 0) {
if (!(out_capabilities->capabilities.supportedUsageFlags &
vk::ImageUsageFlagBits::eTransferDst)) {
LOGF("Surface cannot be destination of blit\n");
skip_device = true;
}
}
if (!skip_device &&
!CheckFormat(device, VK_FORMAT_R8G8B8A8_SRGB, false, VK_FORMAT_FEATURE_BLIT_SRC_BIT)) {
LOGF("Format VK_FORMAT_R8G8B8A8_SRGB cannot be used as transfer source\n");
if (!skip_device && !CheckFormat(device, vk::Format::eR8G8B8A8Srgb, false,
vk::FormatFeatureFlagBits::eBlitSrc)) {
LOGF("Format vk::Format::eR8G8B8A8Srgb cannot be used as transfer source\n");
skip_device = true;
}
if (!skip_device && !CheckFormat(device, VK_FORMAT_D32_SFLOAT, true,
VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT)) {
LOGF("Format VK_FORMAT_D32_SFLOAT cannot be used as depth buffer\n");
if (!skip_device && !CheckFormat(device, vk::Format::eD32Sfloat, true,
vk::FormatFeatureFlagBits::eDepthStencilAttachment)) {
LOGF("Format vk::Format::eD32Sfloat cannot be used as depth buffer\n");
skip_device = true;
}
if (!skip_device && !CheckFormat(device, VK_FORMAT_D32_SFLOAT_S8_UINT, true,
VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT)) {
LOGF("Format VK_FORMAT_D32_SFLOAT_S8_UINT cannot be used as depth buffer\n");
if (!skip_device && !CheckFormat(device, vk::Format::eD32SfloatS8Uint, true,
vk::FormatFeatureFlagBits::eDepthStencilAttachment)) {
LOGF("Format vk::Format::eD32SfloatS8Uint cannot be used as depth buffer\n");
skip_device = true;
}
if (!skip_device && !CheckFormat(device, VK_FORMAT_D16_UNORM, true,
VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT)) {
LOGF("Format VK_FORMAT_D16_UNORM cannot be used as depth buffer\n");
if (!skip_device && !CheckFormat(device, vk::Format::eD16Unorm, true,
vk::FormatFeatureFlagBits::eDepthStencilAttachment)) {
LOGF("Format vk::Format::eD16Unorm cannot be used as depth buffer\n");
skip_device = true;
}
if (!skip_device && !CheckFormat(device, VK_FORMAT_BC3_SRGB_BLOCK, true,
VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT |
VK_FORMAT_FEATURE_TRANSFER_DST_BIT)) {
LOGF("Format VK_FORMAT_BC3_SRGB_BLOCK cannot be used as texture\n");
if (!skip_device && !CheckFormat(device, vk::Format::eBc3SrgbBlock, true,
vk::FormatFeatureFlagBits::eSampledImage |
vk::FormatFeatureFlagBits::eTransferDst)) {
LOGF("Format vk::Format::eBc3SrgbBlock cannot be used as texture\n");
skip_device = true;
}
if (!skip_device && !CheckFormat(device, VK_FORMAT_R8G8B8A8_SRGB, true,
VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT |
VK_FORMAT_FEATURE_TRANSFER_DST_BIT)) {
LOGF("Format VK_FORMAT_R8G8B8A8_SRGB cannot be used as texture\n");
if (!skip_device && !CheckFormat(device, vk::Format::eR8G8B8A8Srgb, true,
vk::FormatFeatureFlagBits::eSampledImage |
vk::FormatFeatureFlagBits::eTransferDst)) {
LOGF("Format vk::Format::eR8G8B8A8Srgb cannot be used as texture\n");
skip_device = true;
}
if (!skip_device && !CheckFormat(device, VK_FORMAT_R8_UNORM, true,
VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT |
VK_FORMAT_FEATURE_TRANSFER_DST_BIT)) {
LOGF("Format VK_FORMAT_R8_UNORM cannot be used as texture\n");
if (!skip_device && !CheckFormat(device, vk::Format::eR8Unorm, true,
vk::FormatFeatureFlagBits::eSampledImage |
vk::FormatFeatureFlagBits::eTransferDst)) {
LOGF("Format vk::Format::eR8Unorm cannot be used as texture\n");
skip_device = true;
}
if (!skip_device && !CheckFormat(device, VK_FORMAT_R8G8_UNORM, true,
VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT |
VK_FORMAT_FEATURE_TRANSFER_DST_BIT)) {
LOGF("Format VK_FORMAT_R8G8_UNORM cannot be used as texture\n");
if (!skip_device && !CheckFormat(device, vk::Format::eR8G8Unorm, true,
vk::FormatFeatureFlagBits::eSampledImage |
vk::FormatFeatureFlagBits::eTransferDst)) {
LOGF("Format vk::Format::eR8G8Unorm cannot be used as texture\n");
skip_device = true;
}
if (!skip_device && !CheckFormat(device, VK_FORMAT_R8G8B8A8_SRGB, true,
VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT |
VK_FORMAT_FEATURE_TRANSFER_DST_BIT)) {
LOGF("Format VK_FORMAT_R8G8B8A8_SRGB cannot be used as texture\n");
if (!skip_device && !CheckFormat(device, vk::Format::eR8G8B8A8Srgb, true,
vk::FormatFeatureFlagBits::eStorageImage |
vk::FormatFeatureFlagBits::eTransferDst)) {
LOGF("Format vk::Format::eR8G8B8A8Srgb cannot be used as texture\n");
if (!skip_device && !CheckFormat(device, VK_FORMAT_R8G8B8A8_UNORM, true,
VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT |
VK_FORMAT_FEATURE_TRANSFER_DST_BIT)) {
LOGF("Format VK_FORMAT_R8G8B8A8_UNORM cannot be used as texture\n");
if (!skip_device && !CheckFormat(device, vk::Format::eR8G8B8A8Unorm, true,
vk::FormatFeatureFlagBits::eStorageImage |
vk::FormatFeatureFlagBits::eTransferDst)) {
LOGF("Format vk::Format::eR8G8B8A8Unorm cannot be used as texture\n");
skip_device = true;
}
}
if (!skip_device && !CheckFormat(device, VK_FORMAT_B8G8R8A8_SRGB, true,
VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT |
VK_FORMAT_FEATURE_TRANSFER_DST_BIT)) {
LOGF("Format VK_FORMAT_B8G8R8A8_SRGB cannot be used as texture\n");
if (!skip_device && !CheckFormat(device, vk::Format::eB8G8R8A8Srgb, true,
vk::FormatFeatureFlagBits::eStorageImage |
vk::FormatFeatureFlagBits::eTransferDst)) {
LOGF("Format vk::Format::eB8G8R8A8Srgb cannot be used as texture\n");
if (!skip_device && !CheckFormat(device, VK_FORMAT_B8G8R8A8_UNORM, true,
VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT |
VK_FORMAT_FEATURE_TRANSFER_DST_BIT)) {
LOGF("Format VK_FORMAT_B8G8R8A8_UNORM cannot be used as texture\n");
if (!skip_device && !CheckFormat(device, vk::Format::eB8G8R8A8Unorm, true,
vk::FormatFeatureFlagBits::eStorageImage |
vk::FormatFeatureFlagBits::eTransferDst)) {
LOGF("Format vk::Format::eB8G8R8A8Unorm cannot be used as texture\n");
skip_device = true;
}
}
@@ -512,7 +509,7 @@ static void VulkanFindPhysicalDevice(VkInstance instance, VkSurfaceKHR surface,
}
if (best_device == nullptr ||
device_properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU) {
device_properties.deviceType == vk::PhysicalDeviceType::eDiscreteGpu) {
best_device = device;
best_queues = qs;
}
@@ -522,34 +519,33 @@ static void VulkanFindPhysicalDevice(VkInstance instance, VkSurfaceKHR surface,
*out_queues = best_queues;
}
static void VulkanInitSubgroupSizeControl(VkPhysicalDevice physical_device, GraphicContext* ctx) {
static void VulkanInitSubgroupSizeControl(vk::PhysicalDevice physical_device, GraphicContext* ctx) {
EXIT_IF(physical_device == nullptr);
EXIT_IF(ctx == nullptr);
VkPhysicalDeviceSubgroupSizeControlProperties subgroup_size_control {};
subgroup_size_control.sType =
VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SUBGROUP_SIZE_CONTROL_PROPERTIES;
vk::PhysicalDeviceSubgroupSizeControlProperties subgroup_size_control {};
subgroup_size_control.sType = vk::StructureType::ePhysicalDeviceSubgroupSizeControlProperties;
subgroup_size_control.pNext = nullptr;
VkPhysicalDeviceVulkan11Properties properties11 {};
properties11.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_1_PROPERTIES;
vk::PhysicalDeviceVulkan11Properties properties11 {};
properties11.sType = vk::StructureType::ePhysicalDeviceVulkan11Properties;
properties11.pNext = &subgroup_size_control;
VkPhysicalDeviceProperties2 properties2 {};
properties2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2;
vk::PhysicalDeviceProperties2 properties2 {};
properties2.sType = vk::StructureType::ePhysicalDeviceProperties2;
properties2.pNext = &properties11;
vkGetPhysicalDeviceProperties2(physical_device, &properties2);
physical_device.getProperties2(&properties2);
VkPhysicalDeviceVulkan13Features features13 {};
features13.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_3_FEATURES;
vk::PhysicalDeviceVulkan13Features features13 {};
features13.sType = vk::StructureType::ePhysicalDeviceVulkan13Features;
features13.pNext = nullptr;
VkPhysicalDeviceFeatures2 features2 {};
features2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
vk::PhysicalDeviceFeatures2 features2 {};
features2.sType = vk::StructureType::ePhysicalDeviceFeatures2;
features2.pNext = &features13;
vkGetPhysicalDeviceFeatures2(physical_device, &features2);
physical_device.getFeatures2(&features2);
ctx->subgroup_size = properties11.subgroupSize;
ctx->min_subgroup_size = subgroup_size_control.minSubgroupSize;
@@ -561,21 +557,22 @@ static void VulkanInitSubgroupSizeControl(VkPhysicalDevice physical_device, Grap
LOGF("Vulkan subgroup: default=%u min=%u max=%u stages=0x%08x size_control=%s\n",
ctx->subgroup_size, ctx->min_subgroup_size, ctx->max_subgroup_size,
ctx->required_subgroup_size_stages, ctx->subgroup_size_control_enabled ? "true" : "false");
static_cast<vk::ShaderStageFlags::MaskType>(ctx->required_subgroup_size_stages),
ctx->subgroup_size_control_enabled ? "true" : "false");
}
static VkDevice VulkanCreateDevice(VkPhysicalDevice physical_device, VkSurfaceKHR surface,
const VulkanExtensions* r, const VulkanQueues& queues,
const std::vector<const char*>& device_extensions,
const GraphicContext* ctx) {
static vk::Device VulkanCreateDevice(vk::PhysicalDevice physical_device, vk::SurfaceKHR surface,
const VulkanExtensions* r, const VulkanQueues& queues,
const std::vector<const char*>& device_extensions,
const GraphicContext* ctx) {
EXIT_IF(physical_device == nullptr);
EXIT_IF(r == nullptr);
EXIT_IF(surface == nullptr);
EXIT_IF(ctx == nullptr);
std::vector<VkDeviceQueueCreateInfo> queue_create_info(queues.family_count);
std::vector<std::vector<float>> queue_priority(queues.family_count);
uint32_t queue_create_info_num = 0;
std::vector<vk::DeviceQueueCreateInfo> queue_create_info(queues.family_count);
std::vector<std::vector<float>> queue_priority(queues.family_count);
uint32_t queue_create_info_num = 0;
for (uint32_t i = 0; i < queues.family_count; i++) {
if (queues.family_used[i] != 0) {
@@ -584,9 +581,9 @@ static VkDevice VulkanCreateDevice(VkPhysicalDevice physical_device, VkSurfaceKH
}
queue_create_info[queue_create_info_num].sType =
VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
vk::StructureType::eDeviceQueueCreateInfo;
queue_create_info[queue_create_info_num].pNext = nullptr;
queue_create_info[queue_create_info_num].flags = 0;
queue_create_info[queue_create_info_num].flags = {};
queue_create_info[queue_create_info_num].queueFamilyIndex = i;
queue_create_info[queue_create_info_num].queueCount = queues.family_used[i];
queue_create_info[queue_create_info_num].pQueuePriorities =
@@ -596,45 +593,45 @@ static VkDevice VulkanCreateDevice(VkPhysicalDevice physical_device, VkSurfaceKH
}
}
VkPhysicalDeviceColorWriteEnableFeaturesEXT color_write_ext {};
color_write_ext.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_COLOR_WRITE_ENABLE_FEATURES_EXT;
vk::PhysicalDeviceColorWriteEnableFeaturesEXT color_write_ext {};
color_write_ext.sType = vk::StructureType::ePhysicalDeviceColorWriteEnableFeaturesEXT;
color_write_ext.pNext = nullptr;
color_write_ext.colorWriteEnable = VK_TRUE;
VkPhysicalDeviceDepthClipControlFeaturesEXT depth_clip_control {};
depth_clip_control.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DEPTH_CLIP_CONTROL_FEATURES_EXT;
vk::PhysicalDeviceDepthClipControlFeaturesEXT depth_clip_control {};
depth_clip_control.sType = vk::StructureType::ePhysicalDeviceDepthClipControlFeaturesEXT;
depth_clip_control.pNext = &color_write_ext;
depth_clip_control.depthClipControl = VK_TRUE;
VkPhysicalDeviceVulkan12Features features12 {};
features12.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES;
vk::PhysicalDeviceVulkan12Features features12 {};
features12.sType = vk::StructureType::ePhysicalDeviceVulkan12Features;
features12.pNext = &depth_clip_control;
features12.samplerMirrorClampToEdge = VK_TRUE;
VkPhysicalDeviceSubgroupSizeControlFeatures subgroup_size_control {};
subgroup_size_control.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SUBGROUP_SIZE_CONTROL_FEATURES;
vk::PhysicalDeviceSubgroupSizeControlFeatures subgroup_size_control {};
subgroup_size_control.sType = vk::StructureType::ePhysicalDeviceSubgroupSizeControlFeatures;
subgroup_size_control.pNext = &features12;
VkPhysicalDeviceVulkan13Features supported_features13 {};
supported_features13.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_3_FEATURES;
vk::PhysicalDeviceVulkan13Features supported_features13 {};
supported_features13.sType = vk::StructureType::ePhysicalDeviceVulkan13Features;
supported_features13.pNext = nullptr;
const auto robustness2_ext_enabled =
HasExtension(device_extensions, VK_EXT_ROBUSTNESS_2_EXTENSION_NAME);
VkPhysicalDeviceRobustness2FeaturesEXT supported_robustness2 {};
supported_robustness2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ROBUSTNESS_2_FEATURES_EXT;
vk::PhysicalDeviceRobustness2FeaturesEXT supported_robustness2 {};
supported_robustness2.sType = vk::StructureType::ePhysicalDeviceRobustness2FeaturesEXT;
supported_robustness2.pNext = nullptr;
if (robustness2_ext_enabled) {
supported_features13.pNext = &supported_robustness2;
}
VkPhysicalDeviceFeatures2 supported_features2 {};
supported_features2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
vk::PhysicalDeviceFeatures2 supported_features2 {};
supported_features2.sType = vk::StructureType::ePhysicalDeviceFeatures2;
supported_features2.pNext = &supported_features13;
vkGetPhysicalDeviceFeatures2(physical_device, &supported_features2);
physical_device.getFeatures2(&supported_features2);
VkPhysicalDeviceFeatures device_features {};
vk::PhysicalDeviceFeatures device_features {};
device_features.fragmentStoresAndAtomics = VK_TRUE;
device_features.samplerAnisotropy = VK_TRUE;
device_features.robustBufferAccess = VK_TRUE;
@@ -655,8 +652,8 @@ static VkDevice VulkanCreateDevice(VkPhysicalDevice physical_device, VkSurfaceKH
? static_cast<const void*>(&subgroup_size_control)
: static_cast<const void*>(&features12));
VkPhysicalDeviceRobustness2FeaturesEXT robustness2 {};
robustness2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ROBUSTNESS_2_FEATURES_EXT;
vk::PhysicalDeviceRobustness2FeaturesEXT robustness2 {};
robustness2.sType = vk::StructureType::ePhysicalDeviceRobustness2FeaturesEXT;
robustness2.pNext = const_cast<void*>(base_feature_chain);
if (robustness2_ext_enabled) {
robustness2.robustBufferAccess2 = supported_robustness2.robustBufferAccess2;
@@ -664,8 +661,8 @@ static VkDevice VulkanCreateDevice(VkPhysicalDevice physical_device, VkSurfaceKH
robustness2.nullDescriptor = supported_robustness2.nullDescriptor;
}
VkPhysicalDeviceVulkan13Features features13 {};
features13.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_3_FEATURES;
vk::PhysicalDeviceVulkan13Features features13 {};
features13.sType = vk::StructureType::ePhysicalDeviceVulkan13Features;
features13.pNext =
robustness2_ext_enabled ? &robustness2 : const_cast<void*>(base_feature_chain);
features13.robustImageAccess = supported_features13.robustImageAccess;
@@ -674,10 +671,10 @@ static VkDevice VulkanCreateDevice(VkPhysicalDevice physical_device, VkSurfaceKH
features13.robustImageAccess == VK_TRUE ? "true" : "false",
robustness2_ext_enabled && robustness2.robustImageAccess2 == VK_TRUE ? "true" : "false");
VkDeviceCreateInfo create_info {};
create_info.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
vk::DeviceCreateInfo create_info {};
create_info.sType = vk::StructureType::eDeviceCreateInfo;
create_info.pNext = &features13;
create_info.flags = 0;
create_info.flags = {};
create_info.pQueueCreateInfos = queue_create_info.data();
create_info.queueCreateInfoCount = queue_create_info_num;
create_info.enabledLayerCount =
@@ -688,11 +685,11 @@ static VkDevice VulkanCreateDevice(VkPhysicalDevice physical_device, VkSurfaceKH
create_info.ppEnabledExtensionNames = device_extensions.data();
create_info.pEnabledFeatures = &device_features;
VkDevice device = nullptr;
vk::Device device = nullptr;
auto result = vkCreateDevice(physical_device, &create_info, nullptr, &device);
if (result != VK_SUCCESS) {
LOGF("vkCreateDevice failed: %s\n", string_VkResult(result));
auto result = physical_device.createDevice(&create_info, nullptr, &device);
if (result != vk::Result::eSuccess) {
LOGF("vkCreateDevice failed: %s\n", VulkanToString(result).c_str());
return nullptr;
}
@@ -724,15 +721,15 @@ static void VulkanGetExtensions(SDL_Window* window, VulkanExtensions* r) {
EXIT_NOT_IMPLEMENTED(required_extensions_count == 0);
EXIT_NOT_IMPLEMENTED(required_extensions_count != r->required_extensions.size());
vkEnumerateInstanceExtensionProperties(nullptr, &available_extensions_count, nullptr);
vk::enumerateInstanceExtensionProperties(nullptr, &available_extensions_count, nullptr);
r->available_extensions = std::vector<VkExtensionProperties>(
r->available_extensions = std::vector<vk::ExtensionProperties>(
available_extensions_count); // @suppress("Ambiguous problem")
std::memset(r->available_extensions.data(), 0,
sizeof(VkExtensionProperties) * r->available_extensions.size());
sizeof(vk::ExtensionProperties) * r->available_extensions.size());
vkEnumerateInstanceExtensionProperties(nullptr, &available_extensions_count,
r->available_extensions.data());
vk::enumerateInstanceExtensionProperties(nullptr, &available_extensions_count,
r->available_extensions.data());
EXIT_NOT_IMPLEMENTED(available_extensions_count != r->available_extensions.size());
@@ -749,22 +746,23 @@ static void VulkanGetExtensions(SDL_Window* window, VulkanExtensions* r) {
}
for (const auto& ext: r->available_extensions) {
LOGF("Vulkan available extension: %s, version = %u\n", ext.extensionName, ext.specVersion);
LOGF("Vulkan available extension: %s, version = %u\n", ext.extensionName.data(),
ext.specVersion);
}
vkEnumerateInstanceLayerProperties(&available_layers_count, nullptr);
vk::enumerateInstanceLayerProperties(&available_layers_count, nullptr);
r->available_layers =
std::vector<VkLayerProperties>(available_layers_count); // @suppress("Ambiguous problem")
std::vector<vk::LayerProperties>(available_layers_count); // @suppress("Ambiguous problem")
std::memset(r->available_layers.data(), 0,
sizeof(VkLayerProperties) * r->available_layers.size());
vkEnumerateInstanceLayerProperties(&available_layers_count, r->available_layers.data());
sizeof(vk::LayerProperties) * r->available_layers.size());
vk::enumerateInstanceLayerProperties(&available_layers_count, r->available_layers.data());
EXIT_NOT_IMPLEMENTED(available_layers_count != r->available_layers.size());
for (const auto& l: r->available_layers) {
LOGF("Vulkan available layer: %s, specVersion = %u, implVersion = %u, %s\n", l.layerName,
l.specVersion, l.implementationVersion, l.description);
LOGF("Vulkan available layer: %s, specVersion = %u, implVersion = %u, %s\n",
l.layerName.data(), l.specVersion, l.implementationVersion, l.description.data());
}
r->required_layers = {"VK_LAYER_KHRONOS_validation"};
@@ -780,18 +778,18 @@ static void VulkanGetExtensions(SDL_Window* window, VulkanExtensions* r) {
}
if (r->enable_validation_layers) {
vkEnumerateInstanceExtensionProperties("VK_LAYER_KHRONOS_validation",
&available_extensions_count, nullptr);
vk::enumerateInstanceExtensionProperties("VK_LAYER_KHRONOS_validation",
&available_extensions_count, nullptr);
std::vector<VkExtensionProperties> available_extensions(available_extensions_count);
std::vector<vk::ExtensionProperties> available_extensions(available_extensions_count);
vkEnumerateInstanceExtensionProperties("VK_LAYER_KHRONOS_validation",
&available_extensions_count,
available_extensions.data());
vk::enumerateInstanceExtensionProperties("VK_LAYER_KHRONOS_validation",
&available_extensions_count,
available_extensions.data());
for (const auto& ext: available_extensions) {
LOGF("VK_LAYER_KHRONOS_validation available extension: %s, version = %u\n",
ext.extensionName, ext.specVersion);
ext.extensionName.data(), ext.specVersion);
}
if (HasExtension(available_extensions, VK_EXT_VALIDATION_FEATURES_EXTENSION_NAME)) {
@@ -802,10 +800,10 @@ static void VulkanGetExtensions(SDL_Window* window, VulkanExtensions* r) {
}
}
static VKAPI_ATTR VkBool32 VKAPI_CALL VulkanDebugMessengerCallback(
VkDebugUtilsMessageSeverityFlagBitsEXT message_severity,
VkDebugUtilsMessageTypeFlagsEXT message_types,
const VkDebugUtilsMessengerCallbackDataEXT* callback_data, void* /*user_data*/) {
static VKAPI_ATTR vk::Bool32 VKAPI_CALL VulkanDebugMessengerCallback(
vk::DebugUtilsMessageSeverityFlagBitsEXT message_severity,
vk::DebugUtilsMessageTypeFlagsEXT message_types,
const vk::DebugUtilsMessengerCallbackDataEXT* callback_data, void* /*user_data*/) {
EXIT_IF(callback_data == nullptr);
EXIT_IF(callback_data->pMessage == nullptr);
@@ -815,13 +813,13 @@ static VKAPI_ATTR VkBool32 VKAPI_CALL VulkanDebugMessengerCallback(
bool error = false;
bool debug_printf = false;
switch (message_severity) {
case VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT:
case vk::DebugUtilsMessageSeverityFlagBitsEXT::eVerbose:
severity_str = "V";
severity_style = Log::Color::BrightWhite;
skip = true;
break;
case VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT:
if ((message_types & VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT) != 0 &&
case vk::DebugUtilsMessageSeverityFlagBitsEXT::eInfo:
if ((message_types & vk::DebugUtilsMessageTypeFlagBitsEXT::eValidation) &&
Config::SpirvDebugPrintfEnabled() && callback_data->pMessageIdName != nullptr &&
strcmp(callback_data->pMessageIdName, "UNASSIGNED-DEBUG-PRINTF") == 0) {
debug_printf = true;
@@ -833,16 +831,17 @@ static VKAPI_ATTR VkBool32 VKAPI_CALL VulkanDebugMessengerCallback(
skip = true;
}
break;
case VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT:
case vk::DebugUtilsMessageSeverityFlagBitsEXT::eWarning:
severity_str = "W";
severity_style = Log::Color::Red;
break;
case VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT:
case vk::DebugUtilsMessageSeverityFlagBitsEXT::eError:
severity_str = "E";
severity_style = Log::Color::BrightRed;
// Only validation errors are fatal; GENERAL-type errors can come
// from unrelated loader/layer issues (e.g. a broken overlay).
error = (message_types & VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT) != 0;
error = static_cast<bool>(message_types &
vk::DebugUtilsMessageTypeFlagBitsEXT::eValidation);
break;
default: severity_str = "?";
}
@@ -867,17 +866,15 @@ static VKAPI_ATTR VkBool32 VKAPI_CALL VulkanDebugMessengerCallback(
return VK_FALSE;
}
static VKAPI_ATTR VkResult VKAPI_CALL VulkanCreateDebugUtilsMessengerEXT(
VkInstance instance, const VkDebugUtilsMessengerCreateInfoEXT* create_info,
const VkAllocationCallbacks* allocator, VkDebugUtilsMessengerEXT* messenger) {
static VKAPI_ATTR vk::Result VKAPI_CALL VulkanCreateDebugUtilsMessengerEXT(
vk::Instance instance, const vk::DebugUtilsMessengerCreateInfoEXT* create_info,
const vk::AllocationCallbacks* allocator, vk::DebugUtilsMessengerEXT* messenger) {
EXIT_IF(instance == nullptr);
if (auto func = reinterpret_cast<PFN_vkCreateDebugUtilsMessengerEXT>(
vkGetInstanceProcAddr(instance, "vkCreateDebugUtilsMessengerEXT"));
func != nullptr) {
return func(instance, create_info, allocator, messenger);
if (auto func = VULKAN_HPP_DEFAULT_DISPATCHER.vkCreateDebugUtilsMessengerEXT; func != nullptr) {
return instance.createDebugUtilsMessengerEXT(create_info, allocator, messenger);
}
return VK_ERROR_EXTENSION_NOT_PRESENT;
return vk::Result::eErrorExtensionNotPresent;
}
static void VulkanCreateQueues(GraphicContext* ctx, const VulkanQueues& queues) {
@@ -893,8 +890,8 @@ static void VulkanCreateQueues(GraphicContext* ctx, const VulkanQueues& queues)
ctx->queues[id].family = info.family;
ctx->queues[id].index = info.index;
EXIT_IF(ctx->queues[id].vk_queue != nullptr);
vkGetDeviceQueue(ctx->device, ctx->queues[id].family, ctx->queues[id].index,
&ctx->queues[id].vk_queue);
ctx->device.getQueue(ctx->queues[id].family, ctx->queues[id].index,
&ctx->queues[id].vk_queue);
EXIT_NOT_IMPLEMENTED(ctx->queues[id].vk_queue == nullptr);
if (with_mutex) {
ctx->queues[id].mutex = new Common::Mutex;
@@ -947,12 +944,10 @@ static void VulkanCreateQueues(GraphicContext* ctx, const VulkanQueues& queues)
static void VulkanCheckInstanceVersion() {
uint32_t version = VK_API_VERSION_1_0;
auto enumerate_instance_version = reinterpret_cast<PFN_vkEnumerateInstanceVersion>(
vkGetInstanceProcAddr(nullptr, "vkEnumerateInstanceVersion"));
if (enumerate_instance_version != nullptr) {
auto result = enumerate_instance_version(&version);
if (result != VK_SUCCESS) {
EXIT("Could not query Vulkan loader version: %s\n", string_VkResult(result));
if (VULKAN_HPP_DEFAULT_DISPATCHER.vkEnumerateInstanceVersion != nullptr) {
auto result = vk::enumerateInstanceVersion(&version);
if (result != vk::Result::eSuccess) {
EXIT("Could not query Vulkan loader version: %s\n", VulkanToString(result).c_str());
}
}
@@ -973,12 +968,19 @@ void VulkanCreate(WindowContext* ctx) {
EXIT_IF(ctx->graphic_ctx.device != nullptr);
EXIT_IF(ctx->surface_capabilities != nullptr);
auto get_instance_proc_addr =
reinterpret_cast<PFN_vkGetInstanceProcAddr>(SDL_Vulkan_GetVkGetInstanceProcAddr());
if (get_instance_proc_addr == nullptr) {
EXIT("Could not load Vulkan: %s\n", SDL_GetError());
}
VULKAN_HPP_DEFAULT_DISPATCHER.init(get_instance_proc_addr);
VulkanExtensions r;
VulkanGetExtensions(ctx->window, &r);
VulkanCheckInstanceVersion();
VkApplicationInfo app_info {};
app_info.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
vk::ApplicationInfo app_info {};
app_info.sType = vk::StructureType::eApplicationInfo;
app_info.pNext = nullptr;
app_info.pApplicationName = "Kyty";
app_info.applicationVersion = 1;
@@ -986,17 +988,17 @@ void VulkanCreate(WindowContext* ctx) {
app_info.engineVersion = 1;
app_info.apiVersion = VULKAN_TARGET_API_VERSION; // NOLINT
VkValidationFeatureEnableEXT enabled_features[] = {
vk::ValidationFeatureEnableEXT enabled_features[] = {
#ifdef KYTY_ENABLE_BEST_PRACTICES
VK_VALIDATION_FEATURE_ENABLE_BEST_PRACTICES_EXT,
vk::ValidationFeatureEnableEXT::eBestPractices,
#endif
#ifdef KYTY_ENABLE_DEBUG_PRINTF
VK_VALIDATION_FEATURE_ENABLE_DEBUG_PRINTF_EXT,
vk::ValidationFeatureEnableEXT::eDebugPrintf,
#endif
};
uint32_t enabled_features_count =
sizeof(enabled_features) / sizeof(VkValidationFeatureEnableEXT);
sizeof(enabled_features) / sizeof(vk::ValidationFeatureEnableEXT);
#ifdef KYTY_ENABLE_DEBUG_PRINTF
if (!Config::SpirvDebugPrintfEnabled()) {
@@ -1004,34 +1006,32 @@ void VulkanCreate(WindowContext* ctx) {
}
#endif
VkValidationFeaturesEXT validation_features {};
validation_features.sType = VK_STRUCTURE_TYPE_VALIDATION_FEATURES_EXT;
vk::ValidationFeaturesEXT validation_features {};
validation_features.sType = vk::StructureType::eValidationFeaturesEXT;
validation_features.pNext = nullptr;
validation_features.enabledValidationFeatureCount = enabled_features_count;
validation_features.pEnabledValidationFeatures = enabled_features;
validation_features.disabledValidationFeatureCount = 0;
validation_features.pDisabledValidationFeatures = nullptr;
VkDebugUtilsMessengerCreateInfoEXT dbg_create_info {};
dbg_create_info.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT;
dbg_create_info.pNext = &validation_features;
dbg_create_info.flags = 0;
dbg_create_info.messageSeverity =
static_cast<uint32_t>(VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT) |
static_cast<uint32_t>(VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT) |
static_cast<uint32_t>(VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT) |
static_cast<uint32_t>(VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT);
dbg_create_info.messageType =
static_cast<uint32_t>(VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT) |
static_cast<uint32_t>(VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT) |
static_cast<uint32_t>(VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT);
vk::DebugUtilsMessengerCreateInfoEXT dbg_create_info {};
dbg_create_info.sType = vk::StructureType::eDebugUtilsMessengerCreateInfoEXT;
dbg_create_info.pNext = &validation_features;
dbg_create_info.flags = {};
dbg_create_info.messageSeverity = vk::DebugUtilsMessageSeverityFlagBitsEXT::eVerbose |
vk::DebugUtilsMessageSeverityFlagBitsEXT::eInfo |
vk::DebugUtilsMessageSeverityFlagBitsEXT::eWarning |
vk::DebugUtilsMessageSeverityFlagBitsEXT::eError;
dbg_create_info.messageType = vk::DebugUtilsMessageTypeFlagBitsEXT::eGeneral |
vk::DebugUtilsMessageTypeFlagBitsEXT::eValidation |
vk::DebugUtilsMessageTypeFlagBitsEXT::ePerformance;
dbg_create_info.pfnUserCallback = VulkanDebugMessengerCallback;
dbg_create_info.pUserData = nullptr;
VkInstanceCreateInfo inst_info {};
inst_info.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
vk::InstanceCreateInfo inst_info {};
inst_info.sType = vk::StructureType::eInstanceCreateInfo;
inst_info.pNext = (r.enable_validation_layers ? &dbg_create_info : nullptr);
inst_info.flags = 0;
inst_info.flags = {};
inst_info.pApplicationInfo = &app_info;
inst_info.enabledExtensionCount = static_cast<uint32_t>(r.required_extensions.size());
inst_info.ppEnabledExtensionNames = r.required_extensions.data();
@@ -1040,25 +1040,31 @@ void VulkanCreate(WindowContext* ctx) {
inst_info.ppEnabledLayerNames =
(r.enable_validation_layers ? r.required_layers.data() : nullptr);
const VkResult result = vkCreateInstance(&inst_info, nullptr, &ctx->graphic_ctx.instance);
const vk::Result result = vk::createInstance(&inst_info, nullptr, &ctx->graphic_ctx.instance);
switch (result) {
case VK_SUCCESS: break;
case VK_ERROR_INCOMPATIBLE_DRIVER: EXIT("Unable to find a compatible Vulkan Driver");
case vk::Result::eSuccess: break;
case vk::Result::eErrorIncompatibleDriver:
EXIT("Unable to find a compatible Vulkan Driver");
default: EXIT("Could not create a Vulkan instance (for unknown reasons)");
}
VULKAN_HPP_DEFAULT_DISPATCHER.init(ctx->graphic_ctx.instance);
if (r.enable_validation_layers) {
dbg_create_info.pNext = nullptr;
if (VulkanCreateDebugUtilsMessengerEXT(ctx->graphic_ctx.instance, &dbg_create_info, nullptr,
&ctx->graphic_ctx.debug_messenger) != VK_SUCCESS) {
&ctx->graphic_ctx.debug_messenger) !=
vk::Result::eSuccess) {
EXIT("Could not create debug messenger");
}
}
if (SDL_Vulkan_CreateSurface(ctx->window, ctx->graphic_ctx.instance, &ctx->surface) ==
SDL_FALSE) {
vk::SurfaceKHR::CType native_surface = VK_NULL_HANDLE;
if (SDL_Vulkan_CreateSurface(ctx->window,
static_cast<vk::Instance::CType>(ctx->graphic_ctx.instance),
&native_surface) == SDL_FALSE) {
EXIT("Could not create a Vulkan surface");
}
ctx->surface = native_surface;
std::vector<const char*> device_extensions = {
VK_KHR_SWAPCHAIN_EXTENSION_NAME, VK_EXT_DEPTH_CLIP_ENABLE_EXTENSION_NAME,
@@ -1082,22 +1088,21 @@ void VulkanCreate(WindowContext* ctx) {
EXIT("Could not find suitable device");
}
vkGetPhysicalDeviceProperties(ctx->graphic_ctx.physical_device,
&ctx->graphic_ctx.physical_device_properties);
vkGetPhysicalDeviceMemoryProperties(ctx->graphic_ctx.physical_device,
&ctx->graphic_ctx.physical_device_memory_properties);
ctx->graphic_ctx.physical_device.getProperties(&ctx->graphic_ctx.physical_device_properties);
ctx->graphic_ctx.physical_device.getMemoryProperties(
&ctx->graphic_ctx.physical_device_memory_properties);
const auto& device_properties = ctx->graphic_ctx.GetPhysicalDeviceProperties();
LOGF("Select device: %s\n", device_properties.deviceName);
LOGF("Select device: %s\n", device_properties.deviceName.data());
{
uint32_t extensions_count = 0;
vkEnumerateDeviceExtensionProperties(ctx->graphic_ctx.physical_device, nullptr,
&extensions_count, nullptr);
ctx->graphic_ctx.physical_device.enumerateDeviceExtensionProperties(
nullptr, &extensions_count, nullptr);
std::vector<VkExtensionProperties> available_extensions(extensions_count);
vkEnumerateDeviceExtensionProperties(ctx->graphic_ctx.physical_device, nullptr,
&extensions_count, available_extensions.data());
std::vector<vk::ExtensionProperties> available_extensions(extensions_count);
ctx->graphic_ctx.physical_device.enumerateDeviceExtensionProperties(
nullptr, &extensions_count, available_extensions.data());
if (HasExtension(available_extensions, VK_EXT_MEMORY_BUDGET_EXTENSION_NAME)) {
device_extensions.push_back(VK_EXT_MEMORY_BUDGET_EXTENSION_NAME);
@@ -1119,6 +1124,7 @@ void VulkanCreate(WindowContext* ctx) {
if (ctx->graphic_ctx.device == nullptr) {
EXIT("Could not create device");
}
VULKAN_HPP_DEFAULT_DISPATCHER.init(ctx->graphic_ctx.device);
if (!VulkanCreateAllocator(&ctx->graphic_ctx)) {
EXIT("Could not create Vulkan memory allocator");
+2 -2
View File
@@ -32,6 +32,7 @@
#include "graphics/host_gpu/renderer/render.h"
#include "graphics/host_gpu/utils.h"
#include "graphics/host_gpu/vma.h"
#include "graphics/host_gpu/vulkanCommon.h"
#include "graphics/presentation/renderDoc.h"
#include "graphics/presentation/videoOut.h"
#include "graphics/presentation/window/windowInternal.h"
@@ -45,7 +46,6 @@
#include <memory>
#include <string>
#include <vector>
#include <vulkan/vk_enum_string_helper.h>
#include <vulkan/vk_platform.h>
#define STB_IMAGE_IMPLEMENTATION
@@ -1035,7 +1035,7 @@ void WindowRun() {
std::_Exit(0);
}
VkSurfaceCapabilitiesKHR* VulkanGetSurfaceCapabilities() {
vk::SurfaceCapabilitiesKHR* VulkanGetSurfaceCapabilities() {
EXIT_IF(g_window_ctx == nullptr);
Common::LockGuard lock(g_window_ctx->mutex);
@@ -4,21 +4,21 @@
#include "SDL_video.h"
#include "common/threads.h"
#include "graphics/host_gpu/graphicContext.h"
#include "graphics/host_gpu/vulkanCommon.h"
#include <cstdint>
#include <vector>
#include <vulkan/vulkan_core.h>
namespace Libs::Graphics {
struct WindowGame;
struct SurfaceCapabilities {
VkSurfaceCapabilitiesKHR capabilities {};
std::vector<VkSurfaceFormatKHR> formats;
std::vector<VkPresentModeKHR> present_modes;
bool format_srgb_bgra32 = false;
bool format_unorm_bgra32 = false;
vk::SurfaceCapabilitiesKHR capabilities {};
std::vector<vk::SurfaceFormatKHR> formats;
std::vector<vk::PresentModeKHR> present_modes;
bool format_srgb_bgra32 = false;
bool format_unorm_bgra32 = false;
};
struct WindowContext {
@@ -26,7 +26,7 @@ struct WindowContext {
VulkanSwapchain* swapchain = nullptr;
SDL_Window* window = nullptr;
bool window_hidden = true;
VkSurfaceKHR surface = nullptr;
vk::SurfaceKHR surface = nullptr;
SurfaceCapabilities* surface_capabilities = nullptr;
WindowGame* game = nullptr;
@@ -40,7 +40,7 @@ struct WindowContext {
extern WindowContext* g_window_ctx;
void VulkanGetSurfaceCapabilities(VkPhysicalDevice physical_device, VkSurfaceKHR surface,
void VulkanGetSurfaceCapabilities(vk::PhysicalDevice physical_device, vk::SurfaceKHR surface,
SurfaceCapabilities* capabilities);
VulkanSwapchain* VulkanCreateSwapchain(GraphicContext* ctx, uint32_t image_count);
void VulkanCreate(WindowContext* ctx);