From a5690a0aef984920fc8ab2e69695837859f1d40e Mon Sep 17 00:00:00 2001 From: Lynix Date: Wed, 13 Jul 2016 18:08:56 +0200 Subject: [PATCH] I can now render meshes ! Former-commit-id: 5f292c0f81730f8fa26d7c930130671da6c8a26d [formerly 2145d599592770fe9a6cf65c46e10400417c4726] Former-commit-id: c0740828e97fcb2a704d55f9d769a371f8d6a4d3 --- include/Nazara/Vulkan/VkBuffer.hpp | 5 +++ include/Nazara/Vulkan/VkBuffer.inl | 28 +++++++++++++++++ include/Nazara/Vulkan/VkCommandBuffer.hpp | 4 +++ include/Nazara/Vulkan/VkCommandBuffer.inl | 20 ++++++++++++ include/Nazara/Vulkan/VkDeviceMemory.hpp | 12 ++++++-- include/Nazara/Vulkan/VkDeviceMemory.inl | 37 +++++++++++++++++++++++ 6 files changed, 104 insertions(+), 2 deletions(-) diff --git a/include/Nazara/Vulkan/VkBuffer.hpp b/include/Nazara/Vulkan/VkBuffer.hpp index cf1399de4..389502b7b 100644 --- a/include/Nazara/Vulkan/VkBuffer.hpp +++ b/include/Nazara/Vulkan/VkBuffer.hpp @@ -24,6 +24,11 @@ namespace Nz Buffer(Buffer&&) = default; ~Buffer() = default; + bool BindBufferMemory(VkDeviceMemory memory, VkDeviceSize offset = 0); + + using DeviceObject::Create; + inline bool Create(const DeviceHandle& device, VkBufferCreateFlags flags, VkDeviceSize size, VkBufferUsageFlags usage, const VkAllocationCallbacks* allocator = nullptr); + VkMemoryRequirements GetMemoryRequirements() const; Buffer& operator=(const Buffer&) = delete; diff --git a/include/Nazara/Vulkan/VkBuffer.inl b/include/Nazara/Vulkan/VkBuffer.inl index b2f0f07d4..088c7cab3 100644 --- a/include/Nazara/Vulkan/VkBuffer.inl +++ b/include/Nazara/Vulkan/VkBuffer.inl @@ -9,6 +9,34 @@ namespace Nz { namespace Vk { + inline bool Buffer::BindBufferMemory(VkDeviceMemory memory, VkDeviceSize offset) + { + m_lastErrorCode = m_device->vkBindBufferMemory(*m_device, m_handle, memory, offset); + if (m_lastErrorCode != VK_SUCCESS) + { + NazaraError("Failed to bind buffer memory"); + return false; + } + + return true; + } + + inline bool Buffer::Create(const DeviceHandle& device, VkBufferCreateFlags flags, VkDeviceSize size, VkBufferUsageFlags usage, const VkAllocationCallbacks* allocator) + { + VkBufferCreateInfo createInfo = { + VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, // VkStructureType sType; + nullptr, // const void* pNext; + flags, // VkBufferCreateFlags flags; + size, // VkDeviceSize size; + usage, // VkBufferUsageFlags usage; + VK_SHARING_MODE_EXCLUSIVE, // VkSharingMode sharingMode; + 0, // uint32_t queueFamilyIndexCount; + nullptr // const uint32_t* pQueueFamilyIndices; + }; + + return Create(device, createInfo, allocator); + } + inline VkMemoryRequirements Buffer::GetMemoryRequirements() const { NazaraAssert(IsValid(), "Invalid buffer"); diff --git a/include/Nazara/Vulkan/VkCommandBuffer.hpp b/include/Nazara/Vulkan/VkCommandBuffer.hpp index f496f90f2..639373d45 100644 --- a/include/Nazara/Vulkan/VkCommandBuffer.hpp +++ b/include/Nazara/Vulkan/VkCommandBuffer.hpp @@ -34,9 +34,13 @@ namespace Nz inline void BeginRenderPass(const VkRenderPassBeginInfo& beginInfo, VkSubpassContents contents = VK_SUBPASS_CONTENTS_INLINE); + inline void BindIndexBuffer(VkBuffer buffer, VkDeviceSize offset, VkIndexType indexType); inline void BindPipeline(VkPipelineBindPoint pipelineBindPoint, VkPipeline pipeline); + inline void BindVertexBuffer(UInt32 binding, const VkBuffer buffer, const VkDeviceSize offset); + inline void BindVertexBuffers(UInt32 firstBinding, UInt32 bindingCount, const VkBuffer* buffer, const VkDeviceSize* offset); inline void Draw(UInt32 vertexCount, UInt32 instanceCount = 1, UInt32 firstVertex = 0, UInt32 firstInstance = 0); + inline void DrawIndexed(UInt32 indexCount, UInt32 instanceCount = 1, UInt32 firstVertex = 0, Int32 vertexOffset = 0, UInt32 firstInstance = 0); inline bool End(); diff --git a/include/Nazara/Vulkan/VkCommandBuffer.inl b/include/Nazara/Vulkan/VkCommandBuffer.inl index ee543fd86..11cb402b7 100644 --- a/include/Nazara/Vulkan/VkCommandBuffer.inl +++ b/include/Nazara/Vulkan/VkCommandBuffer.inl @@ -128,16 +128,36 @@ namespace Nz return m_pool->GetDevice()->vkCmdBeginRenderPass(m_handle, &beginInfo, contents); } + inline void CommandBuffer::BindIndexBuffer(VkBuffer buffer, VkDeviceSize offset, VkIndexType indexType) + { + return m_pool->GetDevice()->vkCmdBindIndexBuffer(m_handle, buffer, offset, indexType); + } + inline void CommandBuffer::BindPipeline(VkPipelineBindPoint pipelineBindPoint, VkPipeline pipeline) { return m_pool->GetDevice()->vkCmdBindPipeline(m_handle, pipelineBindPoint, pipeline); } + inline void CommandBuffer::BindVertexBuffer(UInt32 binding, const VkBuffer buffer, const VkDeviceSize offset) + { + return BindVertexBuffers(binding, 1, &buffer, &offset); + } + + inline void CommandBuffer::BindVertexBuffers(UInt32 firstBinding, UInt32 bindingCount, const VkBuffer* buffer, const VkDeviceSize* offset) + { + return m_pool->GetDevice()->vkCmdBindVertexBuffers(m_handle, firstBinding, bindingCount, buffer, offset); + } + inline void CommandBuffer::Draw(UInt32 vertexCount, UInt32 instanceCount, UInt32 firstVertex, UInt32 firstInstance) { return m_pool->GetDevice()->vkCmdDraw(m_handle, vertexCount, instanceCount, firstVertex, firstInstance); } + inline void CommandBuffer::DrawIndexed(UInt32 indexCount, UInt32 instanceCount, UInt32 firstVertex, Int32 vertexOffset, UInt32 firstInstance) + { + return m_pool->GetDevice()->vkCmdDrawIndexed(m_handle, indexCount, instanceCount, firstVertex, vertexOffset, firstInstance); + } + inline bool CommandBuffer::End() { m_lastErrorCode = m_pool->GetDevice()->vkEndCommandBuffer(m_handle); diff --git a/include/Nazara/Vulkan/VkDeviceMemory.hpp b/include/Nazara/Vulkan/VkDeviceMemory.hpp index 67e37d4c3..c35ffc07d 100644 --- a/include/Nazara/Vulkan/VkDeviceMemory.hpp +++ b/include/Nazara/Vulkan/VkDeviceMemory.hpp @@ -19,21 +19,29 @@ namespace Nz friend DeviceObject; public: - DeviceMemory() = default; + DeviceMemory(); DeviceMemory(const DeviceMemory&) = delete; - DeviceMemory(DeviceMemory&&) = default; + DeviceMemory(DeviceMemory&& memory); ~DeviceMemory() = default; using DeviceObject::Create; inline bool Create(const 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 void* GetMappedPointer(); + + inline bool Map(VkDeviceSize offset, VkDeviceSize size, VkMemoryMapFlags flags = 0); + + inline void Unmap(); + DeviceMemory& operator=(const DeviceMemory&) = delete; DeviceMemory& operator=(DeviceMemory&&) = delete; private: static inline VkResult CreateHelper(const DeviceHandle& device, const VkMemoryAllocateInfo* allocInfo, const VkAllocationCallbacks* allocator, VkDeviceMemory* handle); static inline void DestroyHelper(const DeviceHandle& device, VkDeviceMemory handle, const VkAllocationCallbacks* allocator); + + void* m_mappedPtr; }; } } diff --git a/include/Nazara/Vulkan/VkDeviceMemory.inl b/include/Nazara/Vulkan/VkDeviceMemory.inl index 2139c9e22..5327ff6bc 100644 --- a/include/Nazara/Vulkan/VkDeviceMemory.inl +++ b/include/Nazara/Vulkan/VkDeviceMemory.inl @@ -11,6 +11,18 @@ namespace Nz { namespace Vk { + inline DeviceMemory::DeviceMemory() : + m_mappedPtr(nullptr) + { + } + + DeviceMemory::DeviceMemory(DeviceMemory&& memory) : + DeviceObject(std::move(memory)) + { + m_mappedPtr = memory.m_mappedPtr; + memory.m_mappedPtr = nullptr; + } + inline bool DeviceMemory::Create(const DeviceHandle& device, VkDeviceSize size, UInt32 memoryType, const VkAllocationCallbacks* allocator) { VkMemoryAllocateInfo allocInfo = @@ -44,6 +56,31 @@ namespace Nz return false; } + inline void* DeviceMemory::GetMappedPointer() + { + return m_mappedPtr; + } + + inline bool DeviceMemory::Map(VkDeviceSize offset, VkDeviceSize size, VkMemoryMapFlags flags) + { + m_lastErrorCode = m_device->vkMapMemory(*m_device, m_handle, offset, size, flags, &m_mappedPtr); + if (m_lastErrorCode != VK_SUCCESS) + { + NazaraError("Failed to map device memory"); + return false; + } + + return true; + } + + inline void DeviceMemory::Unmap() + { + NazaraAssert(m_mappedPtr != nullptr, "Memory is not mapped"); + + m_device->vkUnmapMemory(*m_device, m_handle); + m_mappedPtr = nullptr; + } + inline VkResult DeviceMemory::CreateHelper(const DeviceHandle& device, const VkMemoryAllocateInfo* allocInfo, const VkAllocationCallbacks* allocator, VkDeviceMemory* handle) { return device->vkAllocateMemory(*device, allocInfo, allocator, handle);