Skip to content

Commit 349396b

Browse files
committed
fix: small edge case bugs
1 parent c45eb79 commit 349396b

6 files changed

Lines changed: 69 additions & 75 deletions

File tree

include/quark/vk/presentation/details/swapchain.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
#include <cstdint>
44
#include <quark/platform/window/IWindow.hpp>
5-
#include <quark/utils/raii.hpp>
5+
#include <quark/utils/result.hpp>
66
#include <vector>
77
#include <vulkan/vulkan_core.h>
88

@@ -30,11 +30,12 @@ class Swapchain final {
3030
Swapchain() = default;
3131
~Swapchain() { reset(); }
3232

33-
QUARK_NO_COPY(Swapchain);
33+
Swapchain(const Swapchain &) = delete;
34+
Swapchain &operator=(const Swapchain &) = delete;
3435
Swapchain(Swapchain &&other) noexcept;
3536
Swapchain &operator=(Swapchain &&other) noexcept;
3637

37-
void create(const CreateInfo &ci);
38+
[[nodiscard]] util::Status create(const CreateInfo &ci);
3839
void reset() noexcept;
3940

4041
[[nodiscard]] VkSwapchainKHR handle() const noexcept { return swapchain_; }

include/quark/vk/presentation/presenter_bundle.hpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,6 @@ class PresenterBundle final {
4545
[[nodiscard]] Swapchain &swapchain() noexcept { return swapchain_; }
4646

4747
private:
48-
[[nodiscard]] util::Result<Swapchain>
49-
create_swapchain_(VkSwapchainKHR old_swapchain) const;
50-
5148
CreateInfo create_info_{};
5249
VkSurfaceKHR surface_{VK_NULL_HANDLE};
5350
Swapchain swapchain_;

src/backend/vulkan/device/device.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,9 @@ Device::build_device_extensions_(VkPhysicalDevice physical_device,
248248
QUARK_ERR(util::Errc::Unsupported,
249249
"Required device extension unsupported {}", name));
250250

251-
if (!std::ranges::contains(enabled, name)) {
251+
if (std::ranges::none_of(enabled, [name](const char *e) {
252+
return std::strcmp(e, name) == 0;
253+
})) {
252254
enabled.push_back(name);
253255
QUARK_LOG_INFO("device extension enabled: '{}'", name);
254256
}
@@ -263,7 +265,9 @@ Device::build_device_extensions_(VkPhysicalDevice physical_device,
263265
// MoltenVK usually needs portability subset;
264266
constexpr const char *kPortabilitySubset = "VK_KHR_portability_subset";
265267
if (has_device_extension_props_(props, kPortabilitySubset) &&
266-
!std::ranges::contains(enabled, kPortabilitySubset)) {
268+
std::ranges::none_of(enabled, [](const char *e) {
269+
return std::strcmp(e, kPortabilitySubset) == 0;
270+
})) {
267271
enabled.push_back(kPortabilitySubset);
268272
QUARK_LOG_INFO("portability subset: enabled");
269273
}

src/backend/vulkan/presentation/presenter_bundle.cpp

Lines changed: 28 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
#include <quark/vk/presentation/presenter_bundle.hpp>
44
#include <quark/vk/surface_source.hpp>
55

6-
#include <exception>
7-
86
namespace quark::vk {
97

108
namespace {
@@ -53,52 +51,43 @@ util::Status PresenterBundle::create(const CreateInfo &ci) {
5351

5452
QUARK_TRY_ASSIGN(surface_, create_surface(ci.instance, ci.window));
5553

56-
auto swapchain_res = create_swapchain_(VK_NULL_HANDLE);
57-
if (!swapchain_res) {
54+
Swapchain::CreateInfo sci{};
55+
sci.physical_device = ci.physical_device;
56+
sci.device = ci.device;
57+
sci.surface = surface_;
58+
sci.graphics_queue_family_index = ci.graphics_queue_family_index;
59+
sci.present_queue_family_index = ci.present_queue_family_index;
60+
sci.window = ci.window;
61+
sci.old_swapchain = VK_NULL_HANDLE;
62+
sci.preferred_present_mode = ci.preferred_present_mode;
63+
sci.preferred_format = ci.preferred_format;
64+
sci.preferred_color_space = ci.preferred_color_space;
65+
66+
if (auto res = swapchain_.create(sci); !res) {
5867
destroy_surface(ci.instance, surface_);
59-
return util::unexpected(std::move(swapchain_res.error()));
68+
return util::unexpected(std::move(res.error()));
6069
}
6170

62-
swapchain_ = std::move(swapchain_res.value());
6371
QUARK_OK();
6472
}
6573

66-
util::Result<Swapchain>
67-
PresenterBundle::create_swapchain_(VkSwapchainKHR old_swapchain) const {
68-
QUARK_ENSURE(
69-
surface_ != VK_NULL_HANDLE,
70-
QUARK_ERR(util::Errc::InvalidState, "presenter surface is null"));
71-
72-
Swapchain swapchain;
73-
Swapchain::CreateInfo ci{};
74-
ci.physical_device = create_info_.physical_device;
75-
ci.device = create_info_.device;
76-
ci.surface = surface_;
77-
ci.graphics_queue_family_index = create_info_.graphics_queue_family_index;
78-
ci.present_queue_family_index = create_info_.present_queue_family_index;
79-
ci.window = create_info_.window;
80-
ci.old_swapchain = old_swapchain;
81-
ci.preferred_present_mode = create_info_.preferred_present_mode;
82-
ci.preferred_format = create_info_.preferred_format;
83-
ci.preferred_color_space = create_info_.preferred_color_space;
84-
85-
try {
86-
swapchain.create(ci);
87-
} catch (const std::exception &e) {
88-
QUARK_FAIL(QUARK_ERR(util::Errc::ApiError,
89-
"PresenterBundle::create_swapchain_ failed: {}",
90-
e.what()));
91-
}
92-
93-
return swapchain;
94-
}
95-
9674
util::Status PresenterBundle::recreate_swapchain() {
97-
auto *old_swapchain = swapchain_.handle();
75+
VkSwapchainKHR old_handle = swapchain_.handle();
76+
77+
Swapchain::CreateInfo sci{};
78+
sci.physical_device = create_info_.physical_device;
79+
sci.device = create_info_.device;
80+
sci.surface = surface_;
81+
sci.graphics_queue_family_index = create_info_.graphics_queue_family_index;
82+
sci.present_queue_family_index = create_info_.present_queue_family_index;
83+
sci.window = create_info_.window;
84+
sci.old_swapchain = old_handle;
85+
sci.preferred_present_mode = create_info_.preferred_present_mode;
86+
sci.preferred_format = create_info_.preferred_format;
87+
sci.preferred_color_space = create_info_.preferred_color_space;
9888

9989
Swapchain next_swapchain;
100-
QUARK_TRY_ASSIGN(next_swapchain, create_swapchain_(old_swapchain));
101-
90+
QUARK_TRY_STATUS(next_swapchain.create(sci));
10291
swapchain_ = std::move(next_swapchain);
10392
QUARK_OK();
10493
}

src/backend/vulkan/presentation/swapchain.cpp

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
#include <array>
33
#include <limits>
44
#include <quark/platform/window/IWindow.hpp>
5+
#include <quark/utils/diagnostic.hpp>
56
#include <quark/vk/presentation/details/swapchain.hpp>
6-
#include <stdexcept>
77
#include <utility>
88
#include <vector>
99
#include <vulkan/vulkan_core.h>
@@ -122,33 +122,30 @@ Swapchain &Swapchain::operator=(Swapchain &&other) noexcept {
122122
return *this;
123123
}
124124

125-
void Swapchain::create(const CreateInfo &ci) {
125+
util::Status Swapchain::create(const CreateInfo &ci) {
126126
reset();
127127

128128
// TODO: Replace with device valid()
129-
if (ci.physical_device == VK_NULL_HANDLE) {
130-
throw std::runtime_error("Swapchain::create: physical_device is null");
131-
}
132-
133-
if (ci.device == VK_NULL_HANDLE) {
134-
throw std::runtime_error("Swapchain::create: device is null");
135-
}
136-
137-
if (ci.surface == VK_NULL_HANDLE) {
138-
throw std::runtime_error("Swapchain::create: surface is null");
139-
}
140-
129+
QUARK_ENSURE(ci.physical_device != VK_NULL_HANDLE,
130+
QUARK_ERR(util::Errc::InvalidArg,
131+
"Swapchain::create: physical_device is null"));
132+
QUARK_ENSURE(
133+
ci.device != VK_NULL_HANDLE,
134+
QUARK_ERR(util::Errc::InvalidArg, "Swapchain::create: device is null"));
135+
QUARK_ENSURE(
136+
ci.surface != VK_NULL_HANDLE,
137+
QUARK_ERR(util::Errc::InvalidArg, "Swapchain::create: surface is null"));
141138
// TODO: Replace with window valid
142-
if (ci.window == nullptr) {
143-
throw std::runtime_error("Swapchain::create: window is null");
144-
}
139+
QUARK_ENSURE(
140+
ci.window != nullptr,
141+
QUARK_ERR(util::Errc::InvalidArg, "Swapchain::create: window is null"));
145142

146143
device_ = ci.device;
147144

148145
const SupportDetails support = query_support(ci.physical_device, ci.surface);
149146
if (support.formats.empty() || support.present_modes.empty()) {
150-
throw std::runtime_error(
151-
"Swapchain::create: swapchain support is incomplete");
147+
QUARK_FAIL(QUARK_ERR(util::Errc::Unsupported,
148+
"Swapchain::create: swapchain support is incomplete"));
152149
}
153150

154151
const VkSurfaceFormatKHR surface_format = choose_format(
@@ -191,9 +188,8 @@ void Swapchain::create(const CreateInfo &ci) {
191188

192189
VkResult r = vkCreateSwapchainKHR(ci.device, &sci, /*pAllocator=*/nullptr,
193190
&swapchain_);
194-
if (r != VK_SUCCESS) {
195-
throw std::runtime_error("vkCreateSwapchainKHR failed");
196-
}
191+
QUARK_ENSURE(r == VK_SUCCESS,
192+
QUARK_ERR(util::Errc::ApiError, "vkCreateSwapchainKHR failed"));
197193

198194
vkGetSwapchainImagesKHR(ci.device, swapchain_, &image_count,
199195
/*pSwapchainImages=*/nullptr);
@@ -218,10 +214,11 @@ void Swapchain::create(const CreateInfo &ci) {
218214

219215
r = vkCreateImageView(ci.device, &ivci, /*pAllocator=*/nullptr,
220216
&image_views_[index]);
221-
if (r != VK_SUCCESS) {
222-
throw std::runtime_error("vkCreateImageView failed");
223-
}
217+
QUARK_ENSURE(r == VK_SUCCESS,
218+
QUARK_ERR(util::Errc::ApiError, "vkCreateImageView failed"));
224219
}
220+
221+
QUARK_OK();
225222
}
226223

227224
void Swapchain::reset() noexcept {

src/utils/diagnostics/diagnostic.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,18 @@ std::shared_ptr<const SinkList> g_sinks;
1515

1616
void set_diagnostic_sinks(std::span<const DiagnosticSink> sinks) noexcept {
1717
auto list = std::make_shared<SinkList>(sinks.begin(), sinks.end());
18-
std::atomic_store_explicit(&g_sinks,
19-
std::shared_ptr<const SinkList>(std::move(list)),
20-
std::memory_order_release);
18+
std::atomic_store_explicit( // NOLINT deprecated-declarations
19+
&g_sinks,
20+
// the suggestion to use std::atomic::<std::shared_ptr<T>>
21+
std::shared_ptr<const SinkList>(
22+
std::move(list)), // This may not require a move
23+
std::memory_order_release);
24+
// lint from clang tidy does not work on msvc(it works on GCC, at least!), we
25+
// should look into this more! TODO
2126
}
2227

2328
std::shared_ptr<const SinkList> diagnostic_sinks_snapshot() noexcept {
29+
// NOLINTNEXTLINE (same as above)
2430
return std::atomic_load_explicit(std::addressof(g_sinks),
2531
std::memory_order_acquire);
2632
}

0 commit comments

Comments
 (0)