Skip to content

Commit

Permalink
vulkan: Use structured bindings for result where possible.
Browse files Browse the repository at this point in the history
  • Loading branch information
squidbus committed Sep 25, 2024
1 parent 50f217a commit 6cb1222
Show file tree
Hide file tree
Showing 15 changed files with 155 additions and 154 deletions.
10 changes: 5 additions & 5 deletions src/video_core/buffer_cache/buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,12 @@ vk::BufferView Buffer::View(u32 offset, u32 size, bool is_written, AmdGpu::DataF
.offset = offset,
.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));
const auto [view_result, 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.value); });
return view.value;
[view, device = instance->GetDevice()] { device.destroyBufferView(view); });
return view;
}

constexpr u64 WATCHES_INITIAL_RESERVE = 0x4000;
Expand Down
17 changes: 9 additions & 8 deletions src/video_core/renderer_vulkan/renderer_vulkan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,11 @@ 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];
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;
auto [fence_result, fence] =
device.createFence({.flags = vk::FenceCreateFlagBits::eSignaled});
ASSERT_MSG(fence_result == vk::Result::eSuccess, "Failed to create present done fence: {}",
vk::to_string(fence_result));
frame.present_done = fence;
free_queue.push(&frame);
}

Expand Down Expand Up @@ -160,10 +161,10 @@ void RendererVulkan::RecreateFrame(Frame* frame, u32 width, u32 height) {
.layerCount = 1,
},
};
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;
auto [view_result, view] = device.createImageView(view_info);
ASSERT_MSG(view_result == vk::Result::eSuccess, "Failed to create frame image view: {}",
vk::to_string(view_result));
frame->image_view = view;
frame->width = width;
frame->height = height;
}
Expand Down
2 changes: 2 additions & 0 deletions src/video_core/renderer_vulkan/vk_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#define VULKAN_HPP_NO_STRUCT_SETTERS
#define VULKAN_HPP_HAS_SPACESHIP_OPERATOR
#define VULKAN_HPP_NO_EXCEPTIONS
// Define assert-on-result to nothing to instead return the result for our handling.
#define VULKAN_HPP_ASSERT_ON_RESULT
#include <vulkan/vulkan.hpp>

#define VMA_STATIC_VULKAN_FUNCTIONS 0
Expand Down
24 changes: 12 additions & 12 deletions src/video_core/renderer_vulkan/vk_compute_pipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ ComputePipeline::ComputePipeline(const Instance& instance_, Scheduler& scheduler
.bindingCount = static_cast<u32>(bindings.size()),
.pBindings = bindings.data(),
};
auto descriptor_set_result =
auto [descriptor_set_result, descriptor_set] =
instance.GetDevice().createDescriptorSetLayoutUnique(desc_layout_ci);
ASSERT_MSG(descriptor_set_result.result == vk::Result::eSuccess,
ASSERT_MSG(descriptor_set_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);
vk::to_string(descriptor_set_result));
desc_layout = std::move(descriptor_set);

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

const vk::ComputePipelineCreateInfo compute_pipeline_ci = {
.stage = shader_ci,
.layout = *pipeline_layout,
};
auto pipeline_result =
auto [pipeline_result, pipe] =
instance.GetDevice().createComputePipelineUnique(pipeline_cache, compute_pipeline_ci);
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);
ASSERT_MSG(pipeline_result == vk::Result::eSuccess, "Failed to create compute pipeline: {}",
vk::to_string(pipeline_result));
pipeline = std::move(pipe);
}

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

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

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);
auto [pipeline_result, pipe] =
device.createGraphicsPipelineUnique(pipeline_cache, pipeline_info);
ASSERT_MSG(pipeline_result == vk::Result::eSuccess, "Failed to create graphics pipeline: {}",
vk::to_string(pipeline_result));
pipeline = std::move(pipe);
}

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

void GraphicsPipeline::BindResources(const Liverpool::Regs& regs,
Expand Down
43 changes: 21 additions & 22 deletions src/video_core/renderer_vulkan/vk_instance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,22 @@ 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);
auto [devices_result, devices] = instance->enumeratePhysicalDevices();
ASSERT_MSG(devices_result == vk::Result::eSuccess, "Failed to enumerate physical devices: {}",
vk::to_string(devices_result));
return std::move(devices);
}

std::vector<std::string> GetSupportedExtensions(vk::PhysicalDevice physical) {
const auto extensions = physical.enumerateDeviceExtensionProperties();
if (extensions.result != vk::Result::eSuccess) {
const auto [extensions_result, extensions] = physical.enumerateDeviceExtensionProperties();
if (extensions_result != vk::Result::eSuccess) {
LOG_ERROR(Render_Vulkan, "Could not query supported extensions: {}",
vk::to_string(extensions.result));
vk::to_string(extensions_result));
return {};
}
std::vector<std::string> supported_extensions;
supported_extensions.reserve(extensions.value.size());
for (const auto& extension : extensions.value) {
supported_extensions.reserve(extensions.size());
for (const auto& extension : extensions) {
supported_extensions.emplace_back(extension.extensionName.data());
}
return supported_extensions;
Expand Down Expand Up @@ -433,23 +433,22 @@ bool Instance::CreateDevice() {
device_chain.unlink<vk::PhysicalDeviceVertexInputDynamicStateFeaturesEXT>();
}

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));
auto [device_result, dev] = physical_device.createDeviceUnique(device_chain.get());
if (device_result != vk::Result::eSuccess) {
LOG_CRITICAL(Render_Vulkan, "Failed to create device: {}", vk::to_string(device_result));
return false;
}
device = std::move(device_result.value);
device = std::move(dev);

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_result = physical_device.getCalibrateableTimeDomainsEXT();
if (time_domains_result.result == vk::Result::eSuccess) {
const auto& time_domains = time_domains_result.value;
const auto [time_domains_result, time_domains] =
physical_device.getCalibrateableTimeDomainsEXT();
if (time_domains_result == vk::Result::eSuccess) {
#if _WIN64
const bool has_host_time_domain =
std::find(time_domains.cbegin(), time_domains.cend(),
Expand All @@ -473,7 +472,7 @@ bool Instance::CreateDevice() {
}
} else {
LOG_WARNING(Render_Vulkan, "Could not query calibrated time domains for profiling: {}",
vk::to_string(time_domains_result.result));
vk::to_string(time_domains_result));
}
}

Expand Down Expand Up @@ -529,13 +528,13 @@ void Instance::CollectToolingInfo() {
if (!tooling_info) {
return;
}
const auto tools = physical_device.getToolPropertiesEXT();
if (tools.result != vk::Result::eSuccess) {
const auto [tools_result, tools] = physical_device.getToolPropertiesEXT();
if (tools_result != vk::Result::eSuccess) {
LOG_ERROR(Render_Vulkan, "Could not get Vulkan tool properties: {}",
vk::to_string(tools.result));
vk::to_string(tools_result));
return;
}
for (const vk::PhysicalDeviceToolProperties& tool : tools.value) {
for (const vk::PhysicalDeviceToolProperties& tool : tools) {
const std::string_view name = tool.name;
LOG_INFO(Render_Vulkan, "Attached debugging tool: {}", name);
has_renderdoc = has_renderdoc || name == "RenderDoc";
Expand Down
18 changes: 9 additions & 9 deletions src/video_core/renderer_vulkan/vk_master_semaphore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ MasterSemaphore::MasterSemaphore(const Instance& instance_) : instance{instance_
.initialValue = 0,
},
};
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);
auto [semaphore_result, sem] =
instance.GetDevice().createSemaphoreUnique(semaphore_chain.get());
ASSERT_MSG(semaphore_result == vk::Result::eSuccess, "Failed to create master semaphore: {}",
vk::to_string(semaphore_result));
semaphore = std::move(sem);
}

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

PipelineCache::~PipelineCache() = default;
Expand Down
43 changes: 21 additions & 22 deletions src/video_core/renderer_vulkan/vk_platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,12 @@ vk::SurfaceKHR CreateSurface(vk::Instance instance, const Frontend::WindowSDL& e

std::vector<const char*> GetInstanceExtensions(Frontend::WindowSystemType window_type,
bool enable_debug_utils) {
const auto properties_result = vk::enumerateInstanceExtensionProperties();
if (properties_result.result != vk::Result::eSuccess || properties_result.value.empty()) {
const auto [properties_result, properties] = vk::enumerateInstanceExtensionProperties();
if (properties_result != vk::Result::eSuccess || properties.empty()) {
LOG_ERROR(Render_Vulkan, "Failed to query extension properties: {}",
vk::to_string(properties_result.result));
vk::to_string(properties_result));
return {};
}
const auto& properties = properties_result.value;

// Add the windowing system specific extension
std::vector<const char*> extensions;
Expand Down Expand Up @@ -209,16 +208,16 @@ vk::UniqueInstance CreateInstance(Frontend::WindowSystemType window_type, bool e
#endif
VULKAN_HPP_DEFAULT_DISPATCHER.init(vkGetInstanceProcAddr);

const auto available_version = VULKAN_HPP_DEFAULT_DISPATCHER.vkEnumerateInstanceVersion
? vk::enumerateInstanceVersion()
: vk::ResultValue(vk::Result::eSuccess, VK_API_VERSION_1_0);
ASSERT_MSG(available_version.result == vk::Result::eSuccess,
"Failed to query Vulkan API version: {}", vk::to_string(available_version.result));
ASSERT_MSG(available_version.value >= TargetVulkanApiVersion,
const auto [available_version_result, available_version] =
VULKAN_HPP_DEFAULT_DISPATCHER.vkEnumerateInstanceVersion
? vk::enumerateInstanceVersion()
: vk::ResultValue(vk::Result::eSuccess, VK_API_VERSION_1_0);
ASSERT_MSG(available_version_result == vk::Result::eSuccess,
"Failed to query Vulkan API version: {}", vk::to_string(available_version_result));
ASSERT_MSG(available_version >= TargetVulkanApiVersion,
"Vulkan {}.{} is required, but only {}.{} is supported by instance!",
VK_VERSION_MAJOR(TargetVulkanApiVersion), VK_VERSION_MINOR(TargetVulkanApiVersion),
VK_VERSION_MAJOR(available_version.value),
VK_VERSION_MINOR(available_version.value));
VK_VERSION_MAJOR(available_version), VK_VERSION_MINOR(available_version));

const auto extensions = GetInstanceExtensions(window_type, true);

Expand All @@ -227,7 +226,7 @@ vk::UniqueInstance CreateInstance(Frontend::WindowSystemType window_type, bool e
.applicationVersion = VK_MAKE_VERSION(1, 0, 0),
.pEngineName = "shadPS4 Vulkan",
.engineVersion = VK_MAKE_VERSION(1, 0, 0),
.apiVersion = available_version.value,
.apiVersion = available_version,
};

u32 num_layers = 0;
Expand Down Expand Up @@ -345,13 +344,13 @@ vk::UniqueInstance CreateInstance(Frontend::WindowSystemType window_type, bool e
},
};

auto instance_result = vk::createInstanceUnique(instance_ci_chain.get());
ASSERT_MSG(instance_result.result == vk::Result::eSuccess, "Failed to create instance: {}",
vk::to_string(instance_result.result));
auto [instance_result, instance] = vk::createInstanceUnique(instance_ci_chain.get());
ASSERT_MSG(instance_result == vk::Result::eSuccess, "Failed to create instance: {}",
vk::to_string(instance_result));

VULKAN_HPP_DEFAULT_DISPATCHER.init(*instance_result.value);
VULKAN_HPP_DEFAULT_DISPATCHER.init(*instance);

return std::move(instance_result.value);
return std::move(instance);
}

vk::UniqueDebugUtilsMessengerEXT CreateDebugCallback(vk::Instance instance) {
Expand All @@ -365,10 +364,10 @@ vk::UniqueDebugUtilsMessengerEXT CreateDebugCallback(vk::Instance instance) {
vk::DebugUtilsMessageTypeFlagBitsEXT::ePerformance,
.pfnUserCallback = DebugUtilsCallback,
};
auto result = instance.createDebugUtilsMessengerEXTUnique(msg_ci);
ASSERT_MSG(result.result == vk::Result::eSuccess, "Failed to create debug callback: {}",
vk::to_string(result.result));
return std::move(result.value);
auto [messenger_result, messenger] = instance.createDebugUtilsMessengerEXTUnique(msg_ci);
ASSERT_MSG(messenger_result == vk::Result::eSuccess, "Failed to create debug callback: {}",
vk::to_string(messenger_result));
return std::move(messenger);
}

} // namespace Vulkan
Loading

0 comments on commit 6cb1222

Please sign in to comment.