From 7bbc2f69929fbe139afa2efb38e360be1ccd6c42 Mon Sep 17 00:00:00 2001 From: Lynix Date: Wed, 13 Jul 2016 12:24:46 +0200 Subject: [PATCH] Vulkan: Add VkDeviceMemory wrapper Former-commit-id: a7ba192880bb034718a2a3d37022343ff32aae1a [formerly e6911d0772f7c2d4612bdbd97da195873f5a30f6] Former-commit-id: bf235ba9b59bfeb39da3b0fd0b89934cb21061b6 --- include/Nazara/Vulkan.hpp | 1 + include/Nazara/Vulkan/VkDeviceMemory.hpp | 43 +++++++++++++++++ include/Nazara/Vulkan/VkDeviceMemory.inl | 59 ++++++++++++++++++++++++ 3 files changed, 103 insertions(+) create mode 100644 include/Nazara/Vulkan/VkDeviceMemory.hpp create mode 100644 include/Nazara/Vulkan/VkDeviceMemory.inl diff --git a/include/Nazara/Vulkan.hpp b/include/Nazara/Vulkan.hpp index dc72ba4c5..7a158c6f5 100644 --- a/include/Nazara/Vulkan.hpp +++ b/include/Nazara/Vulkan.hpp @@ -36,6 +36,7 @@ #include #include #include +#include #include #include #include diff --git a/include/Nazara/Vulkan/VkDeviceMemory.hpp b/include/Nazara/Vulkan/VkDeviceMemory.hpp new file mode 100644 index 000000000..67e37d4c3 --- /dev/null +++ b/include/Nazara/Vulkan/VkDeviceMemory.hpp @@ -0,0 +1,43 @@ +// Copyright (C) 2016 Jérôme Leclercq +// This file is part of the "Nazara Engine - Vulkan" +// For conditions of distribution and use, see copyright notice in Config.hpp + +#pragma once + +#ifndef NAZARA_VULKAN_VKDEVICEMEMORY_HPP +#define NAZARA_VULKAN_VKDEVICEMEMORY_HPP + +#include +#include + +namespace Nz +{ + namespace Vk + { + class DeviceMemory : public DeviceObject + { + friend DeviceObject; + + public: + DeviceMemory() = default; + DeviceMemory(const DeviceMemory&) = delete; + DeviceMemory(DeviceMemory&&) = default; + ~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); + + 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); + }; + } +} + +#include + +#endif // NAZARA_VULKAN_VKDEVICEMEMORY_HPP diff --git a/include/Nazara/Vulkan/VkDeviceMemory.inl b/include/Nazara/Vulkan/VkDeviceMemory.inl new file mode 100644 index 000000000..2139c9e22 --- /dev/null +++ b/include/Nazara/Vulkan/VkDeviceMemory.inl @@ -0,0 +1,59 @@ +// Copyright (C) 2016 Jérôme Leclercq +// This file is part of the "Nazara Engine - Vulkan" +// For conditions of distribution and use, see copyright notice in Config.hpp + +#include +#include +#include +#include + +namespace Nz +{ + namespace Vk + { + inline bool DeviceMemory::Create(const DeviceHandle& device, VkDeviceSize size, UInt32 memoryType, const VkAllocationCallbacks* allocator) + { + VkMemoryAllocateInfo allocInfo = + { + VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, // VkStructureType sType; + nullptr, // const void* pNext; + size, // VkDeviceSize allocationSize; + memoryType // uint32_t memoryTypeIndex; + }; + + return Create(device, allocInfo, allocator); + } + + inline bool DeviceMemory::Create(const DeviceHandle& device, VkDeviceSize size, UInt32 typeBits, VkFlags properties, const VkAllocationCallbacks* allocator) + { + const Vk::PhysicalDevice& deviceInfo = Vulkan::GetPhysicalDeviceInfo(device->GetPhysicalDevice()); + + UInt32 typeMask = 1; + for (UInt32 i = 0; i < VK_MAX_MEMORY_TYPES; ++i) + { + if (typeBits & typeMask) + { + if ((deviceInfo.memoryProperties.memoryTypes[i].propertyFlags & properties) == properties) + return Create(device, size, i, allocator); + } + + typeMask <<= 1; + } + + NazaraError("Failed to find a memory type suitable for typeBits: " + String::Number(typeBits) + " and properties: 0x" + String::Number(properties, 16)); + return false; + } + + inline VkResult DeviceMemory::CreateHelper(const DeviceHandle& device, const VkMemoryAllocateInfo* allocInfo, const VkAllocationCallbacks* allocator, VkDeviceMemory* handle) + { + return device->vkAllocateMemory(*device, allocInfo, allocator, handle); + } + + inline void DeviceMemory::DestroyHelper(const DeviceHandle& device, VkDeviceMemory handle, const VkAllocationCallbacks* allocator) + { + return device->vkFreeMemory(*device, handle, allocator); + } + } +} + +#include