Skip to content

Commit

Permalink
vulkan: Enable VULKAN_HPP_NO_EXCEPTIONS broadly.
Browse files Browse the repository at this point in the history
  • Loading branch information
squidbus committed Sep 23, 2024
1 parent ad9f137 commit 1eca1f1
Show file tree
Hide file tree
Showing 22 changed files with 212 additions and 114 deletions.
6 changes: 5 additions & 1 deletion src/imgui/renderer/imgui_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,11 @@ void OnResize() {
}

void Shutdown(const vk::Device& device) {
device.waitIdle();
auto result = device.waitIdle();
if (result != vk::Result::eSuccess) {
LOG_WARNING(ImGui, "Failed to wait for Vulkan device idle on shutdown: {}",
vk::to_string(result));
}

TextureManager::StopWorker();

Expand Down
1 change: 0 additions & 1 deletion src/imgui/renderer/imgui_impl_vulkan.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

#pragma once

#define VULKAN_HPP_NO_EXCEPTIONS
#include "common/types.h"
#include "video_core/renderer_vulkan/vk_common.h"

Expand Down
6 changes: 4 additions & 2 deletions src/video_core/buffer_cache/buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,11 @@ vk::BufferView Buffer::View(u32 offset, u32 size, bool is_written, AmdGpu::DataF
.range = size,
};
const auto view = instance->GetDevice().createBufferView(view_ci);
ASSERT_MSG(view.result == vk::Result::eSuccess, "Failed to create buffer view: {}",
vk::to_string(view.result));
scheduler->DeferOperation(
[view, device = instance->GetDevice()] { device.destroyBufferView(view); });
return view;
[view, device = instance->GetDevice()] { device.destroyBufferView(view.value); });
return view.value;
}

constexpr u64 WATCHES_INITIAL_RESERVE = 0x4000;
Expand Down
10 changes: 8 additions & 2 deletions src/video_core/renderer_vulkan/renderer_vulkan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,10 @@ RendererVulkan::RendererVulkan(Frontend::WindowSDL& window_, AmdGpu::Liverpool*
present_frames.resize(num_images);
for (u32 i = 0; i < num_images; i++) {
Frame& frame = present_frames[i];
frame.present_done = device.createFence({.flags = vk::FenceCreateFlagBits::eSignaled});
auto fence_result = device.createFence({.flags = vk::FenceCreateFlagBits::eSignaled});
ASSERT_MSG(fence_result.result == vk::Result::eSuccess,
"Failed to create present done fence: {}", vk::to_string(fence_result.result));
frame.present_done = fence_result.value;
free_queue.push(&frame);
}

Expand Down Expand Up @@ -157,7 +160,10 @@ void RendererVulkan::RecreateFrame(Frame* frame, u32 width, u32 height) {
.layerCount = 1,
},
};
frame->image_view = device.createImageView(view_info);
auto view_result = device.createImageView(view_info);
ASSERT_MSG(view_result.result == vk::Result::eSuccess, "Failed to create frame image view: {}",
vk::to_string(view_result.result));
frame->image_view = view_result.value;
frame->width = width;
frame->height = height;
}
Expand Down
1 change: 1 addition & 0 deletions src/video_core/renderer_vulkan/vk_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#define VULKAN_HPP_NO_CONSTRUCTORS
#define VULKAN_HPP_NO_STRUCT_SETTERS
#define VULKAN_HPP_HAS_SPACESHIP_OPERATOR
#define VULKAN_HPP_NO_EXCEPTIONS
#include <vulkan/vulkan.hpp>

#define VMA_STATIC_VULKAN_FUNCTIONS 0
Expand Down
22 changes: 14 additions & 8 deletions src/video_core/renderer_vulkan/vk_compute_pipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,12 @@ ComputePipeline::ComputePipeline(const Instance& instance_, Scheduler& scheduler
.bindingCount = static_cast<u32>(bindings.size()),
.pBindings = bindings.data(),
};
desc_layout = instance.GetDevice().createDescriptorSetLayoutUnique(desc_layout_ci);
auto descriptor_set_result =
instance.GetDevice().createDescriptorSetLayoutUnique(desc_layout_ci);
ASSERT_MSG(descriptor_set_result.result == vk::Result::eSuccess,
"Failed to create compute descriptor set layout: {}",
vk::to_string(descriptor_set_result.result));
desc_layout = std::move(descriptor_set_result.value);

const vk::DescriptorSetLayout set_layout = *desc_layout;
const vk::PipelineLayoutCreateInfo layout_info = {
Expand All @@ -87,19 +92,20 @@ ComputePipeline::ComputePipeline(const Instance& instance_, Scheduler& scheduler
.pushConstantRangeCount = 1U,
.pPushConstantRanges = &push_constants,
};
pipeline_layout = instance.GetDevice().createPipelineLayoutUnique(layout_info);
auto layout_result = instance.GetDevice().createPipelineLayoutUnique(layout_info);
ASSERT_MSG(layout_result.result == vk::Result::eSuccess,
"Failed to create compute pipeline layout: {}", vk::to_string(layout_result.result));
pipeline_layout = std::move(layout_result.value);

const vk::ComputePipelineCreateInfo compute_pipeline_ci = {
.stage = shader_ci,
.layout = *pipeline_layout,
};
auto result =
auto pipeline_result =
instance.GetDevice().createComputePipelineUnique(pipeline_cache, compute_pipeline_ci);
if (result.result == vk::Result::eSuccess) {
pipeline = std::move(result.value);
} else {
UNREACHABLE_MSG("Graphics pipeline creation failed!");
}
ASSERT_MSG(pipeline_result.result == vk::Result::eSuccess,
"Failed to create compute pipeline: {}", vk::to_string(pipeline_result.result));
pipeline = std::move(pipeline_result.value);
}

ComputePipeline::~ComputePipeline() = default;
Expand Down
21 changes: 13 additions & 8 deletions src/video_core/renderer_vulkan/vk_graphics_pipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ GraphicsPipeline::GraphicsPipeline(const Instance& instance_, Scheduler& schedul
.pushConstantRangeCount = 1,
.pPushConstantRanges = &push_constants,
};
pipeline_layout = instance.GetDevice().createPipelineLayoutUnique(layout_info);
auto layout_result = instance.GetDevice().createPipelineLayoutUnique(layout_info);
ASSERT_MSG(layout_result.result == vk::Result::eSuccess,
"Failed to create graphics pipeline layout: {}",
vk::to_string(layout_result.result));
pipeline_layout = std::move(layout_result.value);

boost::container::static_vector<vk::VertexInputBindingDescription, 32> vertex_bindings;
boost::container::static_vector<vk::VertexInputAttributeDescription, 32> vertex_attributes;
Expand Down Expand Up @@ -281,12 +285,10 @@ GraphicsPipeline::GraphicsPipeline(const Instance& instance_, Scheduler& schedul
.layout = *pipeline_layout,
};

auto result = device.createGraphicsPipelineUnique(pipeline_cache, pipeline_info);
if (result.result == vk::Result::eSuccess) {
pipeline = std::move(result.value);
} else {
UNREACHABLE_MSG("Graphics pipeline creation failed!");
}
auto pipeline_result = device.createGraphicsPipelineUnique(pipeline_cache, pipeline_info);
ASSERT_MSG(pipeline_result.result == vk::Result::eSuccess,
"Failed to create graphics pipeline: {}", vk::to_string(pipeline_result.result));
pipeline = std::move(pipeline_result.value);
}

GraphicsPipeline::~GraphicsPipeline() = default;
Expand Down Expand Up @@ -345,7 +347,10 @@ void GraphicsPipeline::BuildDescSetLayout() {
.bindingCount = static_cast<u32>(bindings.size()),
.pBindings = bindings.data(),
};
desc_layout = instance.GetDevice().createDescriptorSetLayoutUnique(desc_layout_ci);
auto result = instance.GetDevice().createDescriptorSetLayoutUnique(desc_layout_ci);
ASSERT_MSG(result.result == vk::Result::eSuccess,
"Failed to create graphics descriptor set layout: {}", vk::to_string(result.result));
desc_layout = std::move(result.value);
}

void GraphicsPipeline::BindResources(const Liverpool::Regs& regs,
Expand Down
81 changes: 51 additions & 30 deletions src/video_core/renderer_vulkan/vk_instance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,23 @@ namespace Vulkan {

namespace {

std::vector<vk::PhysicalDevice> EnumeratePhysicalDevices(vk::UniqueInstance& instance) {
auto devices_result = instance->enumeratePhysicalDevices();
ASSERT_MSG(devices_result.result == vk::Result::eSuccess,
"Failed to enumerate physical devices: {}", vk::to_string(devices_result.result));
return std::move(devices_result.value);
}

std::vector<std::string> GetSupportedExtensions(vk::PhysicalDevice physical) {
const std::vector extensions = physical.enumerateDeviceExtensionProperties();
const auto extensions = physical.enumerateDeviceExtensionProperties();
if (extensions.result != vk::Result::eSuccess) {
LOG_ERROR(Render_Vulkan, "Could not query supported extensions: {}",
vk::to_string(extensions.result));
return {};
}
std::vector<std::string> supported_extensions;
supported_extensions.reserve(extensions.size());
for (const auto& extension : extensions) {
supported_extensions.reserve(extensions.value.size());
for (const auto& extension : extensions.value) {
supported_extensions.emplace_back(extension.extensionName.data());
}
return supported_extensions;
Expand Down Expand Up @@ -57,13 +69,13 @@ std::string GetReadableVersion(u32 version) {
Instance::Instance(bool enable_validation, bool enable_crash_diagnostic)
: instance{CreateInstance(Frontend::WindowSystemType::Headless, enable_validation,
enable_crash_diagnostic)},
physical_devices{instance->enumeratePhysicalDevices()} {}
physical_devices{EnumeratePhysicalDevices(instance)} {}

Instance::Instance(Frontend::WindowSDL& window, s32 physical_device_index,
bool enable_validation /*= false*/, bool enable_crash_diagnostic /*= false*/)
: instance{CreateInstance(window.getWindowInfo().type, enable_validation,
enable_crash_diagnostic)},
physical_devices{instance->enumeratePhysicalDevices()} {
physical_devices{EnumeratePhysicalDevices(instance)} {
if (enable_validation) {
debug_callback = CreateDebugCallback(*instance);
}
Expand Down Expand Up @@ -395,43 +407,47 @@ bool Instance::CreateDevice() {
device_chain.unlink<vk::PhysicalDeviceVertexInputDynamicStateFeaturesEXT>();
}

try {
device = physical_device.createDeviceUnique(device_chain.get());
} catch (vk::ExtensionNotPresentError& err) {
LOG_CRITICAL(Render_Vulkan, "Some required extensions are not available {}", err.what());
return false;
} catch (vk::FeatureNotPresentError& err) {
LOG_CRITICAL(Render_Vulkan, "Some required features are not available {}", err.what());
auto device_result = physical_device.createDeviceUnique(device_chain.get());
if (device_result.result != vk::Result::eSuccess) {
LOG_CRITICAL(Render_Vulkan, "Failed to create device: {}",
vk::to_string(device_result.result));
return false;
}
device = std::move(device_result.value);

VULKAN_HPP_DEFAULT_DISPATCHER.init(*device);

graphics_queue = device->getQueue(queue_family_index, 0);
present_queue = device->getQueue(queue_family_index, 0);

if (calibrated_timestamps) {
const auto& time_domains = physical_device.getCalibrateableTimeDomainsEXT();
const auto& time_domains_result = physical_device.getCalibrateableTimeDomainsEXT();
if (time_domains_result.result == vk::Result::eSuccess) {
const auto& time_domains = time_domains_result.value;
#if _WIN64
const bool has_host_time_domain =
std::find(time_domains.cbegin(), time_domains.cend(),
vk::TimeDomainEXT::eQueryPerformanceCounter) != time_domains.cend();
const bool has_host_time_domain =
std::find(time_domains.cbegin(), time_domains.cend(),
vk::TimeDomainEXT::eQueryPerformanceCounter) != time_domains.cend();
#elif __linux__
const bool has_host_time_domain =
std::find(time_domains.cbegin(), time_domains.cend(),
vk::TimeDomainEXT::eClockMonotonicRaw) != time_domains.cend();
const bool has_host_time_domain =
std::find(time_domains.cbegin(), time_domains.cend(),
vk::TimeDomainEXT::eClockMonotonicRaw) != time_domains.cend();
#else
// Tracy limitation means only Windows and Linux can use host time domain.
// https://github.com/shadps4-emu/tracy/blob/c6d779d78508514102fbe1b8eb28bda10d95bb2a/public/tracy/TracyVulkan.hpp#L384-L389
const bool has_host_time_domain = false;
// Tracy limitation means only Windows and Linux can use host time domain.
// https://github.com/shadps4-emu/tracy/blob/c6d779d78508514102fbe1b8eb28bda10d95bb2a/public/tracy/TracyVulkan.hpp#L384-L389
const bool has_host_time_domain = false;
#endif
if (has_host_time_domain) {
static constexpr std::string_view context_name{"vk_rasterizer"};
profiler_context =
TracyVkContextHostCalibrated(*instance, physical_device, *device,
VULKAN_HPP_DEFAULT_DISPATCHER.vkGetInstanceProcAddr,
VULKAN_HPP_DEFAULT_DISPATCHER.vkGetDeviceProcAddr);
TracyVkContextName(profiler_context, context_name.data(), context_name.size());
if (has_host_time_domain) {
static constexpr std::string_view context_name{"vk_rasterizer"};
profiler_context = TracyVkContextHostCalibrated(
*instance, physical_device, *device,
VULKAN_HPP_DEFAULT_DISPATCHER.vkGetInstanceProcAddr,
VULKAN_HPP_DEFAULT_DISPATCHER.vkGetDeviceProcAddr);
TracyVkContextName(profiler_context, context_name.data(), context_name.size());
}
} else {
LOG_WARNING(Render_Vulkan, "Could not query calibrated time domains for profiling: {}",
vk::to_string(time_domains_result.result));
}
}

Expand Down Expand Up @@ -488,7 +504,12 @@ void Instance::CollectToolingInfo() {
return;
}
const auto tools = physical_device.getToolPropertiesEXT();
for (const vk::PhysicalDeviceToolProperties& tool : tools) {
if (tools.result != vk::Result::eSuccess) {
LOG_ERROR(Render_Vulkan, "Could not get Vulkan tool properties: {}",
vk::to_string(tools.result));
return;
}
for (const vk::PhysicalDeviceToolProperties& tool : tools.value) {
const std::string_view name = tool.name;
LOG_INFO(Render_Vulkan, "Attached debugging tool: {}", name);
has_renderdoc = has_renderdoc || name == "RenderDoc";
Expand Down
13 changes: 11 additions & 2 deletions src/video_core/renderer_vulkan/vk_master_semaphore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include "video_core/renderer_vulkan/vk_instance.h"
#include "video_core/renderer_vulkan/vk_master_semaphore.h"

#include "common/assert.h"

namespace Vulkan {

constexpr u64 WAIT_TIMEOUT = std::numeric_limits<u64>::max();
Expand All @@ -17,7 +19,10 @@ MasterSemaphore::MasterSemaphore(const Instance& instance_) : instance{instance_
.initialValue = 0,
},
};
semaphore = instance.GetDevice().createSemaphoreUnique(semaphore_chain.get());
auto result = instance.GetDevice().createSemaphoreUnique(semaphore_chain.get());
ASSERT_MSG(result.result == vk::Result::eSuccess, "Failed to create master semaphore: {}",
vk::to_string(result.result));
semaphore = std::move(result.value);
}

MasterSemaphore::~MasterSemaphore() = default;
Expand All @@ -27,7 +32,11 @@ void MasterSemaphore::Refresh() {
u64 counter{};
do {
this_tick = gpu_tick.load(std::memory_order_acquire);
counter = instance.GetDevice().getSemaphoreCounterValue(*semaphore);
auto counter_result = instance.GetDevice().getSemaphoreCounterValue(*semaphore);
ASSERT_MSG(counter_result.result == vk::Result::eSuccess,
"Failed to get master semaphore value: {}",
vk::to_string(counter_result.result));
counter = counter_result.value;
if (counter < this_tick) {
return;
}
Expand Down
5 changes: 4 additions & 1 deletion src/video_core/renderer_vulkan/vk_pipeline_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,10 @@ PipelineCache::PipelineCache(const Instance& instance_, Scheduler& scheduler_,
.subgroup_size = instance.SubgroupSize(),
.support_explicit_workgroup_layout = true,
};
pipeline_cache = instance.GetDevice().createPipelineCacheUnique({});
auto cache_result = instance.GetDevice().createPipelineCacheUnique({});
ASSERT_MSG(cache_result.result == vk::Result::eSuccess, "Failed to create pipeline cache: {}",
vk::to_string(cache_result.result));
pipeline_cache = std::move(cache_result.value);
}

PipelineCache::~PipelineCache() = default;
Expand Down
Loading

0 comments on commit 1eca1f1

Please sign in to comment.