Skip to content

Commit

Permalink
work on refactoring command buffers and images
Browse files Browse the repository at this point in the history
  • Loading branch information
WinteryFox committed Dec 22, 2023
1 parent 5066848 commit f184ff1
Show file tree
Hide file tree
Showing 19 changed files with 410 additions and 454 deletions.
8 changes: 0 additions & 8 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions src/engine/Buffer.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <cstdint>
#include <functional>

namespace Vixen {
/**
Expand All @@ -24,14 +25,14 @@ namespace Vixen {
* @param dataSize The size of the data.
* @param offset The offset within this buffer to start writing from.
*/
virtual void write(const char* data, std::size_t dataSize, std::size_t offset) = 0;
virtual void write(const std::byte* data, std::size_t dataSize, std::size_t offset) = 0;

[[nodiscard]] std::size_t getSize() const;

[[nodiscard]] Usage getBufferUsage() const;

private:
virtual char* map() = 0;
virtual std::byte* map() = 0;

virtual void unmap() = 0;

Expand Down
2 changes: 1 addition & 1 deletion src/engine/vk/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ target_link_libraries(
freeimage
)

target_compile_definitions(VkVixen PRIVATE -DVK_NO_PROTOTYPES)
target_compile_definitions(VkVixen PUBLIC -DVK_NO_PROTOTYPES)

find_package(glslang QUIET)
if (${glslang_FOUND})
Expand Down
153 changes: 66 additions & 87 deletions src/engine/vk/Device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

#include "Device.h"

#include <set>

namespace Vixen::Vk {
Device::Device(
const Instance &instance,
const std::vector<const char *> &extensions,
GraphicsCard gpu,
VkSurfaceKHR surface
const Instance& instance,
const std::vector<const char*>& extensions,
GraphicsCard gpu,
VkSurfaceKHR surface
) : gpu(gpu),
device(VK_NULL_HANDLE),
allocator(VK_NULL_HANDLE),
Expand All @@ -17,14 +19,14 @@ namespace Vixen::Vk {
transferQueueFamily = gpu.getQueueFamilyWithFlags(VK_QUEUE_TRANSFER_BIT)[0];

std::set queueFamilies = {
graphicsQueueFamily.index,
presentQueueFamily.index,
transferQueueFamily.index,
graphicsQueueFamily.index,
presentQueueFamily.index,
transferQueueFamily.index,
};

// TODO: Detect and select best graphics and present queues
std::vector<VkDeviceQueueCreateInfo> queueInfos;
for (const auto &family: queueFamilies) {
for (const auto& family : queueFamilies) {
VkDeviceQueueCreateInfo queueInfo{};
queueInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
queueInfo.queueFamilyIndex = family;
Expand All @@ -45,51 +47,51 @@ namespace Vixen::Vk {
deviceInfo.ppEnabledExtensionNames = extensions.data();

spdlog::info(
"Creating new Vulkan device using GPU \"{}\" Vulkan {}",
gpu.properties.deviceName,
getVersionString(gpu.properties.apiVersion)
"Creating new Vulkan device using GPU \"{}\" Vulkan {}",
gpu.properties.deviceName,
getVersionString(gpu.properties.apiVersion)
);
checkVulkanResult(
vkCreateDevice(gpu.device, &deviceInfo, nullptr, &device),
"Failed to create Vulkan device"
vkCreateDevice(gpu.device, &deviceInfo, nullptr, &device),
"Failed to create Vulkan device"
);
volkLoadDevice(device);

VmaVulkanFunctions vulkanFunctions{
.vkGetInstanceProcAddr = vkGetInstanceProcAddr,
.vkGetDeviceProcAddr = vkGetDeviceProcAddr,
.vkGetPhysicalDeviceProperties = vkGetPhysicalDeviceProperties,
.vkGetPhysicalDeviceMemoryProperties = vkGetPhysicalDeviceMemoryProperties,
.vkAllocateMemory = vkAllocateMemory,
.vkFreeMemory = vkFreeMemory,
.vkMapMemory = vkMapMemory,
.vkUnmapMemory = vkUnmapMemory,
.vkFlushMappedMemoryRanges = vkFlushMappedMemoryRanges,
.vkInvalidateMappedMemoryRanges = vkInvalidateMappedMemoryRanges,
.vkBindBufferMemory = vkBindBufferMemory,
.vkBindImageMemory = vkBindImageMemory,
.vkGetBufferMemoryRequirements = vkGetBufferMemoryRequirements,
.vkGetImageMemoryRequirements = vkGetImageMemoryRequirements,
.vkCreateBuffer = vkCreateBuffer,
.vkDestroyBuffer = vkDestroyBuffer,
.vkCreateImage = vkCreateImage,
.vkDestroyImage = vkDestroyImage,
.vkCmdCopyBuffer = vkCmdCopyBuffer,
.vkGetBufferMemoryRequirements2KHR = vkGetBufferMemoryRequirements2,
.vkGetImageMemoryRequirements2KHR = vkGetImageMemoryRequirements2,
.vkBindBufferMemory2KHR = vkBindBufferMemory2,
.vkBindImageMemory2KHR = vkBindImageMemory2,
.vkGetPhysicalDeviceMemoryProperties2KHR = vkGetPhysicalDeviceMemoryProperties2,
.vkGetDeviceBufferMemoryRequirements = vkGetDeviceBufferMemoryRequirements,
.vkGetDeviceImageMemoryRequirements = vkGetDeviceImageMemoryRequirements,
.vkGetInstanceProcAddr = vkGetInstanceProcAddr,
.vkGetDeviceProcAddr = vkGetDeviceProcAddr,
.vkGetPhysicalDeviceProperties = vkGetPhysicalDeviceProperties,
.vkGetPhysicalDeviceMemoryProperties = vkGetPhysicalDeviceMemoryProperties,
.vkAllocateMemory = vkAllocateMemory,
.vkFreeMemory = vkFreeMemory,
.vkMapMemory = vkMapMemory,
.vkUnmapMemory = vkUnmapMemory,
.vkFlushMappedMemoryRanges = vkFlushMappedMemoryRanges,
.vkInvalidateMappedMemoryRanges = vkInvalidateMappedMemoryRanges,
.vkBindBufferMemory = vkBindBufferMemory,
.vkBindImageMemory = vkBindImageMemory,
.vkGetBufferMemoryRequirements = vkGetBufferMemoryRequirements,
.vkGetImageMemoryRequirements = vkGetImageMemoryRequirements,
.vkCreateBuffer = vkCreateBuffer,
.vkDestroyBuffer = vkDestroyBuffer,
.vkCreateImage = vkCreateImage,
.vkDestroyImage = vkDestroyImage,
.vkCmdCopyBuffer = vkCmdCopyBuffer,
.vkGetBufferMemoryRequirements2KHR = vkGetBufferMemoryRequirements2,
.vkGetImageMemoryRequirements2KHR = vkGetImageMemoryRequirements2,
.vkBindBufferMemory2KHR = vkBindBufferMemory2,
.vkBindImageMemory2KHR = vkBindImageMemory2,
.vkGetPhysicalDeviceMemoryProperties2KHR = vkGetPhysicalDeviceMemoryProperties2,
.vkGetDeviceBufferMemoryRequirements = vkGetDeviceBufferMemoryRequirements,
.vkGetDeviceImageMemoryRequirements = vkGetDeviceImageMemoryRequirements,
};

VmaAllocatorCreateInfo allocatorInfo{
.physicalDevice = gpu.device,
.device = device,
.pVulkanFunctions = &vulkanFunctions,
.instance = instance.instance,
.vulkanApiVersion = VK_API_VERSION_1_3,
.physicalDevice = gpu.device,
.device = device,
.pVulkanFunctions = &vulkanFunctions,
.instance = instance.instance,
.vulkanApiVersion = VK_API_VERSION_1_3,
};
vmaCreateAllocator(&allocatorInfo, &allocator);

Expand All @@ -98,23 +100,26 @@ namespace Vixen::Vk {
presentQueue = getQueueHandle(presentQueueFamily.index, 0);

transferQueue = getQueueHandle(transferQueueFamily.index, 0);
transferCommandPool = std::make_unique<VkCommandPool>(
device,
transferQueueFamily.index,
VkCommandPool::Usage::TRANSIENT,
true
transferCommandPool = std::make_shared<VkCommandPool>(
shared_from_this(),
transferQueueFamily.index,
VkCommandPool::Usage::TRANSIENT,
true
);
}

Device::~Device() {
waitIdle();
graphicsQueue = nullptr;
presentQueue = nullptr;
transferCommandPool = nullptr;
vmaDestroyAllocator(allocator);
vkDestroyDevice(device, nullptr);
}

void Device::waitIdle() const {
vkDeviceWaitIdle(device);
}

VkQueue Device::getQueueHandle(uint32_t queueFamilyIndex, uint32_t queueIndex) const {
VkQueue queue = VK_NULL_HANDLE;
vkGetDeviceQueue(device, queueFamilyIndex, queueIndex, &queue);
Expand All @@ -124,51 +129,25 @@ namespace Vixen::Vk {
return queue;
}

VkDevice Device::getDevice() const {
return device;
}
VkDevice Device::getDevice() const { return device; }

const GraphicsCard &Device::getGpu() const {
return gpu;
}
const GraphicsCard& Device::getGpu() const { return gpu; }

VkSurfaceKHR Device::getSurface() const {
return surface;
}

const QueueFamily &Device::getGraphicsQueueFamily() const {
return graphicsQueueFamily;
}
VkSurfaceKHR Device::getSurface() const { return surface; }

VkQueue Device::getGraphicsQueue() const {
return graphicsQueue;
}
const QueueFamily& Device::getGraphicsQueueFamily() const { return graphicsQueueFamily; }

const QueueFamily &Device::getTransferQueueFamily() const {
return transferQueueFamily;
}
VkQueue Device::getGraphicsQueue() const { return graphicsQueue; }

VkQueue Device::getTransferQueue() const {
return transferQueue;
}
const QueueFamily& Device::getTransferQueueFamily() const { return transferQueueFamily; }

std::unique_ptr<VkCommandPool> &Device::getTransferCommandPool() {
return transferCommandPool;
}
VkQueue Device::getTransferQueue() const { return transferQueue; }

const QueueFamily &Device::getPresentQueueFamily() const {
return presentQueueFamily;
}
const QueueFamily& Device::getPresentQueueFamily() const { return presentQueueFamily; }

VkQueue Device::getPresentQueue() const {
return presentQueue;
}
VkQueue Device::getPresentQueue() const { return presentQueue; }

VmaAllocator Device::getAllocator() const {
return allocator;
}
std::shared_ptr<VkCommandPool> Device::getTransferCommandPool() const { return transferCommandPool; }

void Device::waitIdle() const {
vkDeviceWaitIdle(device);
}
VmaAllocator Device::getAllocator() const { return allocator; }
}
26 changes: 12 additions & 14 deletions src/engine/vk/Device.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
#pragma once

#include <memory>
#include <set>
#include <vk_mem_alloc.h>
#include "Instance.h"
#include "VkCommandPool.h"

namespace Vixen::Vk {
class Device {
class Device : public std::enable_shared_from_this<Device> {
GraphicsCard gpu;

::VkDevice device;
Expand All @@ -28,14 +26,14 @@ namespace Vixen::Vk {

VkQueue transferQueue;

std::unique_ptr<VkCommandPool> transferCommandPool;
std::shared_ptr<VkCommandPool> transferCommandPool;

public:
Device(
const Instance &instance,
const std::vector<const char *> &extensions,
GraphicsCard gpu,
VkSurfaceKHR surface
const Instance& instance,
const std::vector<const char*>& extensions,
GraphicsCard gpu,
VkSurfaceKHR surface
);

~Device();
Expand All @@ -44,24 +42,24 @@ namespace Vixen::Vk {

[[nodiscard]] VkDevice getDevice() const;

[[nodiscard]] const GraphicsCard &getGpu() const;
[[nodiscard]] const GraphicsCard& getGpu() const;

[[nodiscard]] VkSurfaceKHR getSurface() const;

[[nodiscard]] const QueueFamily &getGraphicsQueueFamily() const;
[[nodiscard]] const QueueFamily& getGraphicsQueueFamily() const;

[[nodiscard]] VkQueue getGraphicsQueue() const;

[[nodiscard]] const QueueFamily &getTransferQueueFamily() const;
[[nodiscard]] const QueueFamily& getTransferQueueFamily() const;

[[nodiscard]] VkQueue getTransferQueue() const;

[[nodiscard]] std::unique_ptr<VkCommandPool> &getTransferCommandPool();

[[nodiscard]] const QueueFamily &getPresentQueueFamily() const;
[[nodiscard]] const QueueFamily& getPresentQueueFamily() const;

[[nodiscard]] VkQueue getPresentQueue() const;

[[nodiscard]] std::shared_ptr<VkCommandPool> getTransferCommandPool() const;

[[nodiscard]] VkQueue getQueueHandle(uint32_t queueFamilyIndex, uint32_t queueIndex = 0) const;

[[nodiscard]] VmaAllocator getAllocator() const;
Expand Down
Loading

0 comments on commit f184ff1

Please sign in to comment.