Skip to content

Commit 9b6b8f7

Browse files
committed
fix: forget to include allocator files
1 parent 56f812d commit 9b6b8f7

2 files changed

Lines changed: 104 additions & 0 deletions

File tree

include/quark/vk/allocator.hpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#pragma once
2+
3+
#include <quark/utils/raii.hpp>
4+
#include <quark/utils/result.hpp>
5+
#include <vk_mem_alloc.h>
6+
#include <vulkan/vulkan_core.h>
7+
8+
namespace quark::vk {
9+
10+
class Allocator final {
11+
public:
12+
struct CreateInfo {
13+
VkInstance instance{VK_NULL_HANDLE};
14+
VkPhysicalDevice physical_device{VK_NULL_HANDLE};
15+
VkDevice device{VK_NULL_HANDLE};
16+
const VkAllocationCallbacks *allocation_callbacks{nullptr};
17+
uint32_t vulkan_api_version{VK_API_VERSION_1_0};
18+
};
19+
20+
Allocator() = default;
21+
~Allocator() { destroy(); }
22+
23+
QUARK_NO_COPY(Allocator);
24+
25+
[[nodiscard]] constexpr Allocator(Allocator &&other) noexcept
26+
: allocator_(other.allocator_) {
27+
other.allocator_ = nullptr;
28+
}
29+
30+
[[nodiscard]] constexpr Allocator &operator=(Allocator &&other) noexcept {
31+
if (this == &other) {
32+
return *this;
33+
}
34+
35+
destroy();
36+
allocator_ = other.allocator_;
37+
other.allocator_ = nullptr;
38+
return *this;
39+
}
40+
41+
[[nodiscard]] util::Status create(const CreateInfo &ci);
42+
void destroy() noexcept;
43+
44+
[[nodiscard]] bool valid() const noexcept { return allocator_ != nullptr; }
45+
[[nodiscard]] VmaAllocator handle() const noexcept { return allocator_; }
46+
47+
private:
48+
VmaAllocator allocator_ = nullptr;
49+
};
50+
51+
} // namespace quark::vk
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include <quark/utils/diagnostic.hpp>
2+
#include <quark/vk/allocator.hpp>
3+
#include <quark/vk/vk_error.hpp>
4+
5+
namespace quark::vk {
6+
7+
util::Status Allocator::create(const Allocator::CreateInfo &ci) {
8+
destroy();
9+
10+
QUARK_ENSURE(
11+
ci.instance != VK_NULL_HANDLE,
12+
QUARK_ERR(util::Errc::InvalidArg, "Allocator instance must be valid"));
13+
QUARK_ENSURE(ci.physical_device != VK_NULL_HANDLE,
14+
QUARK_ERR(util::Errc::InvalidArg,
15+
"Allocator physical device must be valid"));
16+
QUARK_ENSURE(ci.device != VK_NULL_HANDLE,
17+
QUARK_ERR(util::Errc::InvalidArg,
18+
"Allocator logical device must be valid"));
19+
20+
const VmaAllocatorCreateInfo allocator_info{
21+
.flags = 0,
22+
.physicalDevice = ci.physical_device,
23+
.device = ci.device,
24+
.preferredLargeHeapBlockSize = 0,
25+
.pAllocationCallbacks = ci.allocation_callbacks,
26+
.pDeviceMemoryCallbacks = nullptr,
27+
.pHeapSizeLimit = nullptr,
28+
.pVulkanFunctions = nullptr,
29+
.instance = ci.instance,
30+
.vulkanApiVersion = ci.vulkan_api_version,
31+
#if VMA_EXTERNAL_MEMORY
32+
.pTypeExternalMemoryHandleTypes = nullptr,
33+
#endif
34+
};
35+
36+
const VkResult result = vmaCreateAllocator(&allocator_info, &allocator_);
37+
if (result != VK_SUCCESS) {
38+
return util::unexpected(vk_error(result, "vmaCreateAllocator"));
39+
}
40+
41+
QUARK_OK();
42+
}
43+
44+
void Allocator::destroy() noexcept {
45+
if (allocator_ == nullptr) {
46+
return;
47+
}
48+
49+
vmaDestroyAllocator(allocator_);
50+
allocator_ = nullptr;
51+
}
52+
53+
} // namespace quark::vk

0 commit comments

Comments
 (0)