VulkanRenderer: Replace ObjectHandle by std::shared_ptr/raw pointers

This commit is contained in:
Lynix 2020-02-23 12:02:15 +01:00
parent 0c008236ba
commit 79f732ee8a
29 changed files with 80 additions and 81 deletions

View File

@ -139,14 +139,14 @@ int main()
Nz::VulkanDevice& device = vulkanWindow.GetDevice(); Nz::VulkanDevice& device = vulkanWindow.GetDevice();
Nz::Vk::ShaderModule vertexShader; Nz::Vk::ShaderModule vertexShader;
if (!vertexShader.Create(device.CreateHandle(), reinterpret_cast<Nz::UInt32*>(vertexShaderCode.data()), vertexShaderCode.size())) if (!vertexShader.Create(device.shared_from_this(), reinterpret_cast<Nz::UInt32*>(vertexShaderCode.data()), vertexShaderCode.size()))
{ {
NazaraError("Failed to create vertex shader"); NazaraError("Failed to create vertex shader");
return __LINE__; return __LINE__;
} }
Nz::Vk::ShaderModule fragmentShader; Nz::Vk::ShaderModule fragmentShader;
if (!fragmentShader.Create(device.CreateHandle(), reinterpret_cast<Nz::UInt32*>(fragmentShaderCode.data()), fragmentShaderCode.size())) if (!fragmentShader.Create(device.shared_from_this(), reinterpret_cast<Nz::UInt32*>(fragmentShaderCode.data()), fragmentShaderCode.size()))
{ {
NazaraError("Failed to create fragment shader"); NazaraError("Failed to create fragment shader");
return __LINE__; return __LINE__;
@ -205,7 +205,7 @@ int main()
Nz::UInt32 uniformSize = sizeof(ubo); Nz::UInt32 uniformSize = sizeof(ubo);
Nz::Vk::Buffer uniformBuffer; Nz::Vk::Buffer uniformBuffer;
if (!uniformBuffer.Create(device.CreateHandle(), 0, uniformSize, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT)) if (!uniformBuffer.Create(device.shared_from_this(), 0, uniformSize, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT))
{ {
NazaraError("Failed to create vertex buffer"); NazaraError("Failed to create vertex buffer");
return __LINE__; return __LINE__;
@ -214,7 +214,7 @@ int main()
VkMemoryRequirements memRequirement = uniformBuffer.GetMemoryRequirements(); VkMemoryRequirements memRequirement = uniformBuffer.GetMemoryRequirements();
Nz::Vk::DeviceMemory uniformBufferMemory; Nz::Vk::DeviceMemory uniformBufferMemory;
if (!uniformBufferMemory.Create(device.CreateHandle(), memRequirement.size, memRequirement.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT)) if (!uniformBufferMemory.Create(device.shared_from_this(), memRequirement.size, memRequirement.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT))
{ {
NazaraError("Failed to allocate vertex buffer memory"); NazaraError("Failed to allocate vertex buffer memory");
return __LINE__; return __LINE__;
@ -245,7 +245,7 @@ int main()
layoutBinding.pImmutableSamplers = nullptr; layoutBinding.pImmutableSamplers = nullptr;
Nz::Vk::DescriptorSetLayout descriptorLayout; Nz::Vk::DescriptorSetLayout descriptorLayout;
if (!descriptorLayout.Create(device.CreateHandle(), layoutBinding)) if (!descriptorLayout.Create(device.shared_from_this(), layoutBinding))
{ {
NazaraError("Failed to create descriptor set layout"); NazaraError("Failed to create descriptor set layout");
return __LINE__; return __LINE__;
@ -256,7 +256,7 @@ int main()
poolSize.type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; poolSize.type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
Nz::Vk::DescriptorPool descriptorPool; Nz::Vk::DescriptorPool descriptorPool;
if (!descriptorPool.Create(device.CreateHandle(), 1, poolSize, VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT)) if (!descriptorPool.Create(device.shared_from_this(), 1, poolSize, VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT))
{ {
NazaraError("Failed to create descriptor pool"); NazaraError("Failed to create descriptor pool");
return __LINE__; return __LINE__;
@ -405,7 +405,7 @@ int main()
}; };
Nz::Vk::PipelineLayout pipelineLayout; Nz::Vk::PipelineLayout pipelineLayout;
pipelineLayout.Create(device.CreateHandle(), layout_create_info); pipelineLayout.Create(device.shared_from_this(), layout_create_info);
std::array<VkDynamicState, 2> dynamicStates = { std::array<VkDynamicState, 2> dynamicStates = {
VK_DYNAMIC_STATE_SCISSOR, VK_DYNAMIC_STATE_SCISSOR,
@ -458,14 +458,14 @@ int main()
}; };
Nz::Vk::Pipeline pipeline; Nz::Vk::Pipeline pipeline;
if (!pipeline.CreateGraphics(device.CreateHandle(), pipeline_create_info)) if (!pipeline.CreateGraphics(device.shared_from_this(), pipeline_create_info))
{ {
NazaraError("Failed to create pipeline"); NazaraError("Failed to create pipeline");
return __LINE__; return __LINE__;
} }
Nz::Vk::CommandPool cmdPool; Nz::Vk::CommandPool cmdPool;
if (!cmdPool.Create(device.CreateHandle(), 0, VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT)) if (!cmdPool.Create(device.shared_from_this(), 0, VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT))
{ {
NazaraError("Failed to create rendering cmd pool"); NazaraError("Failed to create rendering cmd pool");
return __LINE__; return __LINE__;
@ -475,7 +475,7 @@ int main()
clearValues[0].color = {1.0f, 0.8f, 0.4f, 0.0f}; clearValues[0].color = {1.0f, 0.8f, 0.4f, 0.0f};
clearValues[1].depthStencil = {1.f, 0}; clearValues[1].depthStencil = {1.f, 0};
Nz::Vk::Queue graphicsQueue(device.CreateHandle(), device.GetEnabledQueues()[0].queues[0].queue); Nz::Vk::Queue graphicsQueue(device.shared_from_this(), device.GetEnabledQueues()[0].queues[0].queue);
Nz::UInt32 imageCount = vulkanWindow.GetFramebufferCount(); Nz::UInt32 imageCount = vulkanWindow.GetFramebufferCount();
std::vector<Nz::Vk::CommandBuffer> renderCmds = cmdPool.AllocateCommandBuffers(imageCount, VK_COMMAND_BUFFER_LEVEL_PRIMARY); std::vector<Nz::Vk::CommandBuffer> renderCmds = cmdPool.AllocateCommandBuffers(imageCount, VK_COMMAND_BUFFER_LEVEL_PRIMARY);

View File

@ -27,7 +27,7 @@ namespace Nz
bool BindBufferMemory(VkDeviceMemory memory, VkDeviceSize offset = 0); bool BindBufferMemory(VkDeviceMemory memory, VkDeviceSize offset = 0);
using DeviceObject::Create; using DeviceObject::Create;
inline bool Create(const DeviceHandle& device, VkBufferCreateFlags flags, VkDeviceSize size, VkBufferUsageFlags usage, const VkAllocationCallbacks* allocator = nullptr); inline bool Create(DeviceHandle device, VkBufferCreateFlags flags, VkDeviceSize size, VkBufferUsageFlags usage, const VkAllocationCallbacks* allocator = nullptr);
VkMemoryRequirements GetMemoryRequirements() const; VkMemoryRequirements GetMemoryRequirements() const;

View File

@ -21,7 +21,7 @@ namespace Nz
return true; return true;
} }
inline bool Buffer::Create(const DeviceHandle& device, VkBufferCreateFlags flags, VkDeviceSize size, VkBufferUsageFlags usage, const VkAllocationCallbacks* allocator) inline bool Buffer::Create(DeviceHandle device, VkBufferCreateFlags flags, VkDeviceSize size, VkBufferUsageFlags usage, const VkAllocationCallbacks* allocator)
{ {
VkBufferCreateInfo createInfo = { VkBufferCreateInfo createInfo = {
VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, // VkStructureType sType; VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, // VkStructureType sType;
@ -34,7 +34,7 @@ namespace Nz
nullptr // const uint32_t* pQueueFamilyIndices; nullptr // const uint32_t* pQueueFamilyIndices;
}; };
return Create(device, createInfo, allocator); return Create(std::move(device), createInfo, allocator);
} }
inline VkMemoryRequirements Buffer::GetMemoryRequirements() const inline VkMemoryRequirements Buffer::GetMemoryRequirements() const

View File

@ -86,7 +86,7 @@ namespace Nz
private: private:
inline CommandBuffer(CommandPool& pool, VkCommandBuffer handle); inline CommandBuffer(CommandPool& pool, VkCommandBuffer handle);
CommandPoolHandle m_pool; CommandPool* m_pool;
VkAllocationCallbacks m_allocator; VkAllocationCallbacks m_allocator;
VkCommandBuffer m_handle; VkCommandBuffer m_handle;
VkResult m_lastErrorCode; VkResult m_lastErrorCode;

View File

@ -5,6 +5,7 @@
#include <Nazara/VulkanRenderer/Wrapper/CommandBuffer.hpp> #include <Nazara/VulkanRenderer/Wrapper/CommandBuffer.hpp>
#include <Nazara/Core/Error.hpp> #include <Nazara/Core/Error.hpp>
#include <Nazara/VulkanRenderer/Wrapper/Instance.hpp> #include <Nazara/VulkanRenderer/Wrapper/Instance.hpp>
#include <cassert>
#include <Nazara/VulkanRenderer/Debug.hpp> #include <Nazara/VulkanRenderer/Debug.hpp>
namespace Nz namespace Nz
@ -12,7 +13,7 @@ namespace Nz
namespace Vk namespace Vk
{ {
inline CommandBuffer::CommandBuffer() : inline CommandBuffer::CommandBuffer() :
m_pool(), m_pool(nullptr),
m_handle(VK_NULL_HANDLE) m_handle(VK_NULL_HANDLE)
{ {
} }
@ -24,7 +25,7 @@ namespace Nz
} }
inline CommandBuffer::CommandBuffer(CommandBuffer&& commandBuffer) : inline CommandBuffer::CommandBuffer(CommandBuffer&& commandBuffer) :
m_pool(std::move(commandBuffer.m_pool)), m_pool(commandBuffer.m_pool),
m_allocator(commandBuffer.m_allocator), m_allocator(commandBuffer.m_allocator),
m_handle(commandBuffer.m_handle), m_handle(commandBuffer.m_handle),
m_lastErrorCode(commandBuffer.m_lastErrorCode) m_lastErrorCode(commandBuffer.m_lastErrorCode)
@ -223,7 +224,10 @@ namespace Nz
inline void CommandBuffer::Free() inline void CommandBuffer::Free()
{ {
if (m_handle) if (m_handle)
{
assert(m_pool);
m_pool->GetDevice()->vkFreeCommandBuffers(*m_pool->GetDevice(), *m_pool, 1, &m_handle); m_pool->GetDevice()->vkFreeCommandBuffers(*m_pool->GetDevice(), *m_pool, 1, &m_handle);
}
} }
inline void CommandBuffer::PipelineBarrier(VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags, const VkImageMemoryBarrier& imageMemoryBarrier) inline void CommandBuffer::PipelineBarrier(VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags, const VkImageMemoryBarrier& imageMemoryBarrier)
@ -401,7 +405,7 @@ namespace Nz
m_allocator = commandBuffer.m_allocator; m_allocator = commandBuffer.m_allocator;
m_handle = commandBuffer.m_handle; m_handle = commandBuffer.m_handle;
m_lastErrorCode = commandBuffer.m_lastErrorCode; m_lastErrorCode = commandBuffer.m_lastErrorCode;
m_pool = std::move(commandBuffer.m_pool); m_pool = commandBuffer.m_pool;
m_handle = commandBuffer.m_handle; m_handle = commandBuffer.m_handle;
commandBuffer.m_handle = VK_NULL_HANDLE; commandBuffer.m_handle = VK_NULL_HANDLE;

View File

@ -8,7 +8,6 @@
#define NAZARA_VULKANRENDERER_VKCOMMANDPOOL_HPP #define NAZARA_VULKANRENDERER_VKCOMMANDPOOL_HPP
#include <Nazara/Prerequisites.hpp> #include <Nazara/Prerequisites.hpp>
#include <Nazara/Core/HandledObject.hpp>
#include <Nazara/VulkanRenderer/Wrapper/DeviceObject.hpp> #include <Nazara/VulkanRenderer/Wrapper/DeviceObject.hpp>
namespace Nz namespace Nz
@ -16,11 +15,8 @@ namespace Nz
namespace Vk namespace Vk
{ {
class CommandBuffer; class CommandBuffer;
class CommandPool;
using CommandPoolHandle = ObjectHandle<CommandPool>; class NAZARA_VULKANRENDERER_API CommandPool : public DeviceObject<CommandPool, VkCommandPool, VkCommandPoolCreateInfo>
class NAZARA_VULKANRENDERER_API CommandPool : public DeviceObject<CommandPool, VkCommandPool, VkCommandPoolCreateInfo>, public HandledObject<CommandPool>
{ {
friend DeviceObject; friend DeviceObject;
@ -34,7 +30,7 @@ namespace Nz
std::vector<CommandBuffer> AllocateCommandBuffers(UInt32 commandBufferCount, VkCommandBufferLevel level); std::vector<CommandBuffer> AllocateCommandBuffers(UInt32 commandBufferCount, VkCommandBufferLevel level);
using DeviceObject::Create; using DeviceObject::Create;
inline bool Create(const DeviceHandle& device, UInt32 queueFamilyIndex, VkCommandPoolCreateFlags flags = 0, const VkAllocationCallbacks* allocator = nullptr); inline bool Create(DeviceHandle device, UInt32 queueFamilyIndex, VkCommandPoolCreateFlags flags = 0, const VkAllocationCallbacks* allocator = nullptr);
inline bool Reset(VkCommandPoolResetFlags flags); inline bool Reset(VkCommandPoolResetFlags flags);

View File

@ -11,7 +11,7 @@ namespace Nz
{ {
namespace Vk namespace Vk
{ {
inline bool CommandPool::Create(const DeviceHandle& device, UInt32 queueFamilyIndex, VkCommandPoolCreateFlags flags, const VkAllocationCallbacks* allocator) inline bool CommandPool::Create(DeviceHandle device, UInt32 queueFamilyIndex, VkCommandPoolCreateFlags flags, const VkAllocationCallbacks* allocator)
{ {
VkCommandPoolCreateInfo createInfo = VkCommandPoolCreateInfo createInfo =
{ {
@ -21,7 +21,7 @@ namespace Nz
queueFamilyIndex queueFamilyIndex
}; };
return Create(device, createInfo, allocator); return Create(std::move(device), createInfo, allocator);
} }
inline bool CommandPool::Reset(VkCommandPoolResetFlags flags) inline bool CommandPool::Reset(VkCommandPoolResetFlags flags)

View File

@ -8,19 +8,15 @@
#define NAZARA_VULKANRENDERER_VKDESCRIPTORPOOL_HPP #define NAZARA_VULKANRENDERER_VKDESCRIPTORPOOL_HPP
#include <Nazara/Prerequisites.hpp> #include <Nazara/Prerequisites.hpp>
#include <Nazara/Core/HandledObject.hpp>
#include <Nazara/VulkanRenderer/Wrapper/DeviceObject.hpp> #include <Nazara/VulkanRenderer/Wrapper/DeviceObject.hpp>
namespace Nz namespace Nz
{ {
namespace Vk namespace Vk
{ {
class DescriptorPool;
class DescriptorSet; class DescriptorSet;
using DescriptorPoolHandle = ObjectHandle<DescriptorPool>; class NAZARA_VULKANRENDERER_API DescriptorPool : public DeviceObject<DescriptorPool, VkDescriptorPool, VkDescriptorPoolCreateInfo>
class NAZARA_VULKANRENDERER_API DescriptorPool : public DeviceObject<DescriptorPool, VkDescriptorPool, VkDescriptorPoolCreateInfo>, public HandledObject<DescriptorPool>
{ {
friend DeviceObject; friend DeviceObject;
@ -34,8 +30,8 @@ namespace Nz
std::vector<DescriptorSet> AllocateDescriptorSets(UInt32 descriptorSetCount, const VkDescriptorSetLayout* setLayouts); std::vector<DescriptorSet> AllocateDescriptorSets(UInt32 descriptorSetCount, const VkDescriptorSetLayout* setLayouts);
using DeviceObject::Create; using DeviceObject::Create;
inline bool Create(const DeviceHandle& device, UInt32 maxSets, const VkDescriptorPoolSize& poolSize, VkDescriptorPoolCreateFlags flags = 0, const VkAllocationCallbacks* allocator = nullptr); inline bool Create(DeviceHandle device, UInt32 maxSets, const VkDescriptorPoolSize& poolSize, VkDescriptorPoolCreateFlags flags = 0, const VkAllocationCallbacks* allocator = nullptr);
inline bool Create(const DeviceHandle& device, UInt32 maxSets, UInt32 poolSizeCount, const VkDescriptorPoolSize* poolSize, VkDescriptorPoolCreateFlags flags = 0, const VkAllocationCallbacks* allocator = nullptr); inline bool Create(DeviceHandle device, UInt32 maxSets, UInt32 poolSizeCount, const VkDescriptorPoolSize* poolSize, VkDescriptorPoolCreateFlags flags = 0, const VkAllocationCallbacks* allocator = nullptr);
DescriptorPool& operator=(const DescriptorPool&) = delete; DescriptorPool& operator=(const DescriptorPool&) = delete;
DescriptorPool& operator=(DescriptorPool&&) = delete; DescriptorPool& operator=(DescriptorPool&&) = delete;

View File

@ -9,7 +9,7 @@ namespace Nz
{ {
namespace Vk namespace Vk
{ {
inline bool DescriptorPool::Create(const DeviceHandle& device, UInt32 maxSets, const VkDescriptorPoolSize& poolSize, VkDescriptorPoolCreateFlags flags, const VkAllocationCallbacks* allocator) inline bool DescriptorPool::Create(DeviceHandle device, UInt32 maxSets, const VkDescriptorPoolSize& poolSize, VkDescriptorPoolCreateFlags flags, const VkAllocationCallbacks* allocator)
{ {
VkDescriptorPoolCreateInfo createInfo = VkDescriptorPoolCreateInfo createInfo =
{ {
@ -21,10 +21,10 @@ namespace Nz
&poolSize // const VkDescriptorPoolSize* pPoolSizes; &poolSize // const VkDescriptorPoolSize* pPoolSizes;
}; };
return Create(device, createInfo, allocator); return Create(std::move(device), createInfo, allocator);
} }
inline bool DescriptorPool::Create(const DeviceHandle& device, UInt32 maxSets, UInt32 poolSizeCount, const VkDescriptorPoolSize* poolSize, VkDescriptorPoolCreateFlags flags, const VkAllocationCallbacks* allocator) inline bool DescriptorPool::Create(DeviceHandle device, UInt32 maxSets, UInt32 poolSizeCount, const VkDescriptorPoolSize* poolSize, VkDescriptorPoolCreateFlags flags, const VkAllocationCallbacks* allocator)
{ {
VkDescriptorPoolCreateInfo createInfo = VkDescriptorPoolCreateInfo createInfo =
{ {
@ -36,7 +36,7 @@ namespace Nz
poolSize // const VkDescriptorPoolSize* pPoolSizes; poolSize // const VkDescriptorPoolSize* pPoolSizes;
}; };
return Create(device, createInfo, allocator); return Create(std::move(device), createInfo, allocator);
} }
inline VkResult DescriptorPool::CreateHelper(const DeviceHandle& device, const VkDescriptorPoolCreateInfo* createInfo, const VkAllocationCallbacks* allocator, VkDescriptorPool* handle) inline VkResult DescriptorPool::CreateHelper(const DeviceHandle& device, const VkDescriptorPoolCreateInfo* createInfo, const VkAllocationCallbacks* allocator, VkDescriptorPool* handle)

View File

@ -45,7 +45,7 @@ namespace Nz
private: private:
inline DescriptorSet(DescriptorPool& pool, VkDescriptorSet handle); inline DescriptorSet(DescriptorPool& pool, VkDescriptorSet handle);
DescriptorPoolHandle m_pool; DescriptorPool* m_pool;
VkAllocationCallbacks m_allocator; VkAllocationCallbacks m_allocator;
VkDescriptorSet m_handle; VkDescriptorSet m_handle;
VkResult m_lastErrorCode; VkResult m_lastErrorCode;

View File

@ -5,6 +5,7 @@
#include <Nazara/VulkanRenderer/Wrapper/DescriptorSet.hpp> #include <Nazara/VulkanRenderer/Wrapper/DescriptorSet.hpp>
#include <Nazara/Core/Error.hpp> #include <Nazara/Core/Error.hpp>
#include <Nazara/VulkanRenderer/Wrapper/Instance.hpp> #include <Nazara/VulkanRenderer/Wrapper/Instance.hpp>
#include <cassert>
#include <Nazara/VulkanRenderer/Debug.hpp> #include <Nazara/VulkanRenderer/Debug.hpp>
namespace Nz namespace Nz
@ -12,7 +13,7 @@ namespace Nz
namespace Vk namespace Vk
{ {
inline DescriptorSet::DescriptorSet() : inline DescriptorSet::DescriptorSet() :
m_pool(), m_pool(nullptr),
m_handle(VK_NULL_HANDLE) m_handle(VK_NULL_HANDLE)
{ {
} }
@ -24,7 +25,7 @@ namespace Nz
} }
inline DescriptorSet::DescriptorSet(DescriptorSet&& descriptorSet) : inline DescriptorSet::DescriptorSet(DescriptorSet&& descriptorSet) :
m_pool(std::move(descriptorSet.m_pool)), m_pool(descriptorSet.m_pool),
m_allocator(descriptorSet.m_allocator), m_allocator(descriptorSet.m_allocator),
m_handle(descriptorSet.m_handle), m_handle(descriptorSet.m_handle),
m_lastErrorCode(descriptorSet.m_lastErrorCode) m_lastErrorCode(descriptorSet.m_lastErrorCode)
@ -40,7 +41,10 @@ namespace Nz
inline void DescriptorSet::Free() inline void DescriptorSet::Free()
{ {
if (m_handle) if (m_handle)
{
assert(m_pool);
m_pool->GetDevice()->vkFreeDescriptorSets(*m_pool->GetDevice(), *m_pool, 1, &m_handle); m_pool->GetDevice()->vkFreeDescriptorSets(*m_pool->GetDevice(), *m_pool, 1, &m_handle);
}
} }
inline VkResult DescriptorSet::GetLastErrorCode() const inline VkResult DescriptorSet::GetLastErrorCode() const
@ -104,7 +108,7 @@ namespace Nz
m_allocator = descriptorSet.m_allocator; m_allocator = descriptorSet.m_allocator;
m_handle = descriptorSet.m_handle; m_handle = descriptorSet.m_handle;
m_lastErrorCode = descriptorSet.m_lastErrorCode; m_lastErrorCode = descriptorSet.m_lastErrorCode;
m_pool = std::move(descriptorSet.m_pool); m_pool = descriptorSet.m_pool;
m_handle = descriptorSet.m_handle; m_handle = descriptorSet.m_handle;
descriptorSet.m_handle = VK_NULL_HANDLE; descriptorSet.m_handle = VK_NULL_HANDLE;

View File

@ -25,8 +25,8 @@ namespace Nz
~DescriptorSetLayout() = default; ~DescriptorSetLayout() = default;
using DeviceObject::Create; using DeviceObject::Create;
inline bool Create(const DeviceHandle& device, const VkDescriptorSetLayoutBinding& binding, VkDescriptorSetLayoutCreateFlags flags = 0, const VkAllocationCallbacks* allocator = nullptr); inline bool Create(DeviceHandle device, const VkDescriptorSetLayoutBinding& binding, VkDescriptorSetLayoutCreateFlags flags = 0, const VkAllocationCallbacks* allocator = nullptr);
inline bool Create(const DeviceHandle& device, UInt32 bindingCount, const VkDescriptorSetLayoutBinding* binding, VkDescriptorSetLayoutCreateFlags flags = 0, const VkAllocationCallbacks* allocator = nullptr); inline bool Create(DeviceHandle device, UInt32 bindingCount, const VkDescriptorSetLayoutBinding* binding, VkDescriptorSetLayoutCreateFlags flags = 0, const VkAllocationCallbacks* allocator = nullptr);
DescriptorSetLayout& operator=(const DescriptorSetLayout&) = delete; DescriptorSetLayout& operator=(const DescriptorSetLayout&) = delete;
DescriptorSetLayout& operator=(DescriptorSetLayout&&) = delete; DescriptorSetLayout& operator=(DescriptorSetLayout&&) = delete;

View File

@ -9,12 +9,12 @@ namespace Nz
{ {
namespace Vk namespace Vk
{ {
inline bool DescriptorSetLayout::Create(const DeviceHandle& device, const VkDescriptorSetLayoutBinding& binding, VkDescriptorSetLayoutCreateFlags flags, const VkAllocationCallbacks* allocator) inline bool DescriptorSetLayout::Create(DeviceHandle device, const VkDescriptorSetLayoutBinding& binding, VkDescriptorSetLayoutCreateFlags flags, const VkAllocationCallbacks* allocator)
{ {
return Create(device, 1U, &binding, flags, allocator); return Create(std::move(device), 1U, &binding, flags, allocator);
} }
inline bool DescriptorSetLayout::Create(const DeviceHandle& device, UInt32 bindingCount, const VkDescriptorSetLayoutBinding* binding, VkDescriptorSetLayoutCreateFlags flags, const VkAllocationCallbacks* allocator) inline bool DescriptorSetLayout::Create(DeviceHandle device, UInt32 bindingCount, const VkDescriptorSetLayoutBinding* binding, VkDescriptorSetLayoutCreateFlags flags, const VkAllocationCallbacks* allocator)
{ {
VkDescriptorSetLayoutCreateInfo createInfo = VkDescriptorSetLayoutCreateInfo createInfo =
{ {
@ -25,7 +25,7 @@ namespace Nz
binding // const VkDescriptorSetLayoutBinding* pBindings; binding // const VkDescriptorSetLayoutBinding* pBindings;
}; };
return Create(device, createInfo, allocator); return Create(std::move(device), createInfo, allocator);
} }
inline VkResult DescriptorSetLayout::CreateHelper(const DeviceHandle& device, const VkDescriptorSetLayoutCreateInfo* createInfo, const VkAllocationCallbacks* allocator, VkDescriptorSetLayout* handle) inline VkResult DescriptorSetLayout::CreateHelper(const DeviceHandle& device, const VkDescriptorSetLayoutCreateInfo* createInfo, const VkAllocationCallbacks* allocator, VkDescriptorSetLayout* handle)

View File

@ -8,11 +8,10 @@
#define NAZARA_VULKANRENDERER_VKDEVICE_HPP #define NAZARA_VULKANRENDERER_VKDEVICE_HPP
#include <Nazara/Prerequisites.hpp> #include <Nazara/Prerequisites.hpp>
#include <Nazara/Core/HandledObject.hpp>
#include <Nazara/Core/ObjectHandle.hpp>
#include <Nazara/VulkanRenderer/Config.hpp> #include <Nazara/VulkanRenderer/Config.hpp>
#include <Nazara/VulkanRenderer/Wrapper/Loader.hpp> #include <Nazara/VulkanRenderer/Wrapper/Loader.hpp>
#include <vulkan/vulkan.h> #include <vulkan/vulkan.h>
#include <memory>
#include <unordered_set> #include <unordered_set>
namespace Nz namespace Nz
@ -23,9 +22,9 @@ namespace Nz
class Instance; class Instance;
class Queue; class Queue;
using DeviceHandle = ObjectHandle<Device>; using DeviceHandle = std::shared_ptr<Device>;
class NAZARA_VULKANRENDERER_API Device : public HandledObject<Device> class NAZARA_VULKANRENDERER_API Device : public std::enable_shared_from_this<Device>
{ {
public: public:
struct QueueFamilyInfo; struct QueueFamilyInfo;

View File

@ -25,8 +25,8 @@ namespace Nz
~DeviceMemory() = default; ~DeviceMemory() = default;
using DeviceObject::Create; using DeviceObject::Create;
inline bool Create(const DeviceHandle& device, VkDeviceSize size, UInt32 memoryType, const VkAllocationCallbacks* allocator = nullptr); inline bool Create(DeviceHandle device, VkDeviceSize size, UInt32 memoryType, const VkAllocationCallbacks* allocator = nullptr);
inline bool Create(const DeviceHandle& device, VkDeviceSize size, UInt32 typeBits, VkFlags properties, const VkAllocationCallbacks* allocator = nullptr); inline bool Create(DeviceHandle device, VkDeviceSize size, UInt32 typeBits, VkFlags properties, const VkAllocationCallbacks* allocator = nullptr);
inline void* GetMappedPointer(); inline void* GetMappedPointer();

View File

@ -24,7 +24,7 @@ namespace Nz
memory.m_mappedPtr = nullptr; memory.m_mappedPtr = nullptr;
} }
inline bool DeviceMemory::Create(const DeviceHandle& device, VkDeviceSize size, UInt32 memoryType, const VkAllocationCallbacks* allocator) inline bool DeviceMemory::Create(DeviceHandle device, VkDeviceSize size, UInt32 memoryType, const VkAllocationCallbacks* allocator)
{ {
VkMemoryAllocateInfo allocInfo = VkMemoryAllocateInfo allocInfo =
{ {
@ -34,10 +34,10 @@ namespace Nz
memoryType // uint32_t memoryTypeIndex; memoryType // uint32_t memoryTypeIndex;
}; };
return Create(device, allocInfo, allocator); return Create(std::move(device), allocInfo, allocator);
} }
inline bool DeviceMemory::Create(const DeviceHandle& device, VkDeviceSize size, UInt32 typeBits, VkFlags properties, const VkAllocationCallbacks* allocator) inline bool DeviceMemory::Create(DeviceHandle device, VkDeviceSize size, UInt32 typeBits, VkFlags properties, const VkAllocationCallbacks* allocator)
{ {
const Vk::PhysicalDevice& deviceInfo = Vulkan::GetPhysicalDeviceInfo(device->GetPhysicalDevice()); const Vk::PhysicalDevice& deviceInfo = Vulkan::GetPhysicalDeviceInfo(device->GetPhysicalDevice());
@ -47,7 +47,7 @@ namespace Nz
if (typeBits & typeMask) if (typeBits & typeMask)
{ {
if ((deviceInfo.memoryProperties.memoryTypes[i].propertyFlags & properties) == properties) if ((deviceInfo.memoryProperties.memoryTypes[i].propertyFlags & properties) == properties)
return Create(device, size, i, allocator); return Create(std::move(device), size, i, allocator);
} }
typeMask <<= 1; typeMask <<= 1;

View File

@ -24,7 +24,7 @@ namespace Nz
inline DeviceObject(DeviceObject&& object); inline DeviceObject(DeviceObject&& object);
inline ~DeviceObject(); inline ~DeviceObject();
inline bool Create(const DeviceHandle& device, const CreateInfo& createInfo, const VkAllocationCallbacks* allocator = nullptr); inline bool Create(DeviceHandle device, const CreateInfo& createInfo, const VkAllocationCallbacks* allocator = nullptr);
inline void Destroy(); inline void Destroy();
inline bool IsValid() const; inline bool IsValid() const;

View File

@ -36,9 +36,9 @@ namespace Nz
} }
template<typename C, typename VkType, typename CreateInfo> template<typename C, typename VkType, typename CreateInfo>
inline bool DeviceObject<C, VkType, CreateInfo>::Create(const DeviceHandle& device, const CreateInfo& createInfo, const VkAllocationCallbacks* allocator) inline bool DeviceObject<C, VkType, CreateInfo>::Create(DeviceHandle device, const CreateInfo& createInfo, const VkAllocationCallbacks* allocator)
{ {
m_device = device; m_device = std::move(device);
m_lastErrorCode = C::CreateHelper(m_device, &createInfo, allocator, &m_handle); m_lastErrorCode = C::CreateHelper(m_device, &createInfo, allocator, &m_handle);
if (m_lastErrorCode != VkResult::VK_SUCCESS) if (m_lastErrorCode != VkResult::VK_SUCCESS)
{ {

View File

@ -35,7 +35,7 @@ namespace Nz
inline operator VkPipeline() const; inline operator VkPipeline() const;
protected: protected:
inline bool Create(const DeviceHandle& device, VkResult result, const VkAllocationCallbacks* allocator); inline bool Create(DeviceHandle device, VkResult result, const VkAllocationCallbacks* allocator);
DeviceHandle m_device; DeviceHandle m_device;
VkAllocationCallbacks m_allocator; VkAllocationCallbacks m_allocator;

View File

@ -31,12 +31,12 @@ namespace Nz
inline bool Pipeline::CreateCompute(const DeviceHandle& device, const VkComputePipelineCreateInfo& createInfo, VkPipelineCache cache, const VkAllocationCallbacks* allocator) inline bool Pipeline::CreateCompute(const DeviceHandle& device, const VkComputePipelineCreateInfo& createInfo, VkPipelineCache cache, const VkAllocationCallbacks* allocator)
{ {
return Create(device, device->vkCreateComputePipelines(*device, cache, 1U, &createInfo, allocator, &m_handle), allocator); return Create(std::move(device), device->vkCreateComputePipelines(*device, cache, 1U, &createInfo, allocator, &m_handle), allocator);
} }
inline bool Pipeline::CreateGraphics(const DeviceHandle& device, const VkGraphicsPipelineCreateInfo& createInfo, VkPipelineCache cache, const VkAllocationCallbacks* allocator) inline bool Pipeline::CreateGraphics(const DeviceHandle& device, const VkGraphicsPipelineCreateInfo& createInfo, VkPipelineCache cache, const VkAllocationCallbacks* allocator)
{ {
return Create(device, device->vkCreateGraphicsPipelines(*device, cache, 1U, &createInfo, allocator, &m_handle), allocator); return Create(std::move(device), device->vkCreateGraphicsPipelines(*device, cache, 1U, &createInfo, allocator, &m_handle), allocator);
} }
inline void Pipeline::Destroy() inline void Pipeline::Destroy()
@ -63,7 +63,7 @@ namespace Nz
return m_handle; return m_handle;
} }
inline bool Pipeline::Create(const DeviceHandle& device, VkResult result, const VkAllocationCallbacks* allocator) inline bool Pipeline::Create(DeviceHandle device, VkResult result, const VkAllocationCallbacks* allocator)
{ {
m_device = device; m_device = device;
m_lastErrorCode = result; m_lastErrorCode = result;

View File

@ -25,7 +25,7 @@ namespace Nz
~Semaphore() = default; ~Semaphore() = default;
using DeviceObject::Create; using DeviceObject::Create;
inline bool Create(const DeviceHandle& device, VkSemaphoreCreateFlags flags = 0, const VkAllocationCallbacks* allocator = nullptr); inline bool Create(DeviceHandle device, VkSemaphoreCreateFlags flags = 0, const VkAllocationCallbacks* allocator = nullptr);
Semaphore& operator=(const Semaphore&) = delete; Semaphore& operator=(const Semaphore&) = delete;
Semaphore& operator=(Semaphore&&) = delete; Semaphore& operator=(Semaphore&&) = delete;

View File

@ -9,7 +9,7 @@ namespace Nz
{ {
namespace Vk namespace Vk
{ {
inline bool Semaphore::Create(const DeviceHandle& device, VkSemaphoreCreateFlags flags, const VkAllocationCallbacks* allocator) inline bool Semaphore::Create(DeviceHandle device, VkSemaphoreCreateFlags flags, const VkAllocationCallbacks* allocator)
{ {
VkSemaphoreCreateInfo createInfo = VkSemaphoreCreateInfo createInfo =
{ {
@ -18,7 +18,7 @@ namespace Nz
flags flags
}; };
return Create(device, createInfo, allocator); return Create(std::move(device), createInfo, allocator);
} }
inline VkResult Semaphore::CreateHelper(const DeviceHandle& device, const VkSemaphoreCreateInfo* createInfo, const VkAllocationCallbacks* allocator, VkSemaphore* handle) inline VkResult Semaphore::CreateHelper(const DeviceHandle& device, const VkSemaphoreCreateInfo* createInfo, const VkAllocationCallbacks* allocator, VkSemaphore* handle)

View File

@ -25,7 +25,7 @@ namespace Nz
~ShaderModule() = default; ~ShaderModule() = default;
using DeviceObject::Create; using DeviceObject::Create;
inline bool Create(const DeviceHandle& device, const UInt32* code, std::size_t size, VkShaderModuleCreateFlags flags = 0, const VkAllocationCallbacks* allocator = nullptr); inline bool Create(DeviceHandle device, const UInt32* code, std::size_t size, VkShaderModuleCreateFlags flags = 0, const VkAllocationCallbacks* allocator = nullptr);
ShaderModule& operator=(const ShaderModule&) = delete; ShaderModule& operator=(const ShaderModule&) = delete;
ShaderModule& operator=(ShaderModule&&) = delete; ShaderModule& operator=(ShaderModule&&) = delete;

View File

@ -9,7 +9,7 @@ namespace Nz
{ {
namespace Vk namespace Vk
{ {
inline bool ShaderModule::Create(const DeviceHandle& device, const UInt32* code, std::size_t size, VkShaderModuleCreateFlags flags, const VkAllocationCallbacks* allocator) inline bool ShaderModule::Create(DeviceHandle device, const UInt32* code, std::size_t size, VkShaderModuleCreateFlags flags, const VkAllocationCallbacks* allocator)
{ {
VkShaderModuleCreateInfo createInfo = VkShaderModuleCreateInfo createInfo =
{ {
@ -20,7 +20,7 @@ namespace Nz
code code
}; };
return Create(device, createInfo, allocator); return Create(std::move(device), createInfo, allocator);
} }
inline VkResult ShaderModule::CreateHelper(const DeviceHandle& device, const VkShaderModuleCreateInfo* createInfo, const VkAllocationCallbacks* allocator, VkShaderModule* handle) inline VkResult ShaderModule::CreateHelper(const DeviceHandle& device, const VkShaderModuleCreateInfo* createInfo, const VkAllocationCallbacks* allocator, VkShaderModule* handle)

View File

@ -29,7 +29,7 @@ namespace Nz
inline bool AcquireNextImage(Nz::UInt64 timeout, VkSemaphore semaphore, VkFence fence, UInt32* imageIndex) const; inline bool AcquireNextImage(Nz::UInt64 timeout, VkSemaphore semaphore, VkFence fence, UInt32* imageIndex) const;
inline bool Create(const DeviceHandle& device, const VkSwapchainCreateInfoKHR& createInfo, const VkAllocationCallbacks* allocator = nullptr); inline bool Create(DeviceHandle device, const VkSwapchainCreateInfoKHR& createInfo, const VkAllocationCallbacks* allocator = nullptr);
inline const Buffer& GetBuffer(UInt32 index) const; inline const Buffer& GetBuffer(UInt32 index) const;
inline const std::vector<Buffer>& GetBuffers() const; inline const std::vector<Buffer>& GetBuffers() const;

View File

@ -29,9 +29,9 @@ namespace Nz
} }
} }
inline bool Swapchain::Create(const DeviceHandle& device, const VkSwapchainCreateInfoKHR& createInfo, const VkAllocationCallbacks* allocator) inline bool Swapchain::Create(DeviceHandle device, const VkSwapchainCreateInfoKHR& createInfo, const VkAllocationCallbacks* allocator)
{ {
if (!DeviceObject::Create(device, createInfo, allocator)) if (!DeviceObject::Create(std::move(device), createInfo, allocator))
return false; return false;
UInt32 imageCount = 0; UInt32 imageCount = 0;

View File

@ -187,14 +187,14 @@ namespace Nz
1U // uint32_t layers; 1U // uint32_t layers;
}; };
if (!m_frameBuffers[i].Create(m_device->CreateHandle(), frameBufferCreate)) if (!m_frameBuffers[i].Create(m_device, frameBufferCreate))
{ {
NazaraError("Failed to create framebuffer for image #" + String::Number(i)); NazaraError("Failed to create framebuffer for image #" + String::Number(i));
return false; return false;
} }
} }
m_imageReadySemaphore.Create(m_device->CreateHandle()); m_imageReadySemaphore.Create(m_device);
m_clock.Restart(); m_clock.Restart();
@ -221,14 +221,14 @@ namespace Nz
VK_IMAGE_LAYOUT_UNDEFINED, // VkImageLayout initialLayout; VK_IMAGE_LAYOUT_UNDEFINED, // VkImageLayout initialLayout;
}; };
if (!m_depthBuffer.Create(m_device->CreateHandle(), imageCreateInfo)) if (!m_depthBuffer.Create(m_device, imageCreateInfo))
{ {
NazaraError("Failed to create depth buffer"); NazaraError("Failed to create depth buffer");
return false; return false;
} }
VkMemoryRequirements memoryReq = m_depthBuffer.GetMemoryRequirements(); VkMemoryRequirements memoryReq = m_depthBuffer.GetMemoryRequirements();
if (!m_depthBufferMemory.Create(m_device->CreateHandle(), memoryReq.size, memoryReq.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT)) if (!m_depthBufferMemory.Create(m_device, memoryReq.size, memoryReq.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT))
{ {
NazaraError("Failed to allocate depth buffer memory"); NazaraError("Failed to allocate depth buffer memory");
return false; return false;
@ -262,7 +262,7 @@ namespace Nz
} }
}; };
if (!m_depthBufferView.Create(m_device->CreateHandle(), imageViewCreateInfo)) if (!m_depthBufferView.Create(m_device, imageViewCreateInfo))
{ {
NazaraError("Failed to create depth buffer view"); NazaraError("Failed to create depth buffer view");
return false; return false;
@ -356,7 +356,7 @@ namespace Nz
dependencies.data() // const VkSubpassDependency* pDependencies; dependencies.data() // const VkSubpassDependency* pDependencies;
}; };
return m_renderPass.Create(m_device->CreateHandle(), createInfo); return m_renderPass.Create(m_device, createInfo);
} }
bool VkRenderWindow::SetupSwapchain(Vk::Surface& surface, const Vector2ui& size) bool VkRenderWindow::SetupSwapchain(Vk::Surface& surface, const Vector2ui& size)
@ -421,7 +421,7 @@ namespace Nz
0 0
}; };
if (!m_swapchain.Create(m_device->CreateHandle(), swapchainInfo)) if (!m_swapchain.Create(m_device, swapchainInfo))
{ {
NazaraError("Failed to create swapchain"); NazaraError("Failed to create swapchain");
return false; return false;

View File

@ -11,6 +11,6 @@ namespace Nz
std::unique_ptr<AbstractBuffer> VulkanDevice::InstantiateBuffer(Buffer* parent, BufferType type) std::unique_ptr<AbstractBuffer> VulkanDevice::InstantiateBuffer(Buffer* parent, BufferType type)
{ {
return std::make_unique<VulkanBuffer>(CreateHandle(), parent, type); return std::make_unique<VulkanBuffer>(shared_from_this(), parent, type);
} }
} }

View File

@ -225,7 +225,7 @@ namespace Nz
VkQueue queue; VkQueue queue;
vkGetDeviceQueue(m_device, queueFamilyIndex, queueIndex, &queue); vkGetDeviceQueue(m_device, queueFamilyIndex, queueIndex, &queue);
return Queue(CreateHandle(), queue); return Queue(shared_from_this(), queue);
} }
} }