vsg: include/vsg/vk/DeviceMemory.h Source File

vsg 1.1.9 VulkanSceneGraph library |

Loading...
Searching...
No Matches
DeviceMemory.h
1#pragma once
2
3/* <editor-fold desc="MIT License">
4
5Copyright(c) 2018 Robert Osfield
6
7Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
8
9The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
10
11THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12
13</editor-fold> */
14
15#include <vsg/core/Array.h>
16#include <vsg/core/MemorySlots.h>
17#include <vsg/vk/Device.h>
18
19#include <map>
20
21namespace vsg
22{
23 class Buffer;
24 class Image;
25
28 class VSG_DECLSPEC DeviceMemory : public Inherit<Object, DeviceMemory>
29 {
30 public:
31 DeviceMemory(Device* device, const VkMemoryRequirements& memRequirements, VkMemoryPropertyFlags properties, void* pNextAllocInfo = nullptr);
32
33 operator VkDeviceMemory() const { return _deviceMemory; }
34 VkDeviceMemory vk() const { return _deviceMemory; }
35
36 void copy(VkDeviceSize offset, VkDeviceSize size, const void* src_data);
37 void copy(VkDeviceSize offset, const Data* data);
38
40 VkResult map(VkDeviceSize offset, VkDeviceSize size, VkMemoryMapFlags flags, void** ppData);
41 void unmap();
42
43 const VkMemoryRequirements& getMemoryRequirements() const { return _memoryRequirements; }
44 const VkMemoryPropertyFlags& getMemoryPropertyFlags() const { return _properties; }
45
46 MemorySlots::OptionalOffset reserve(VkDeviceSize size);
47 void release(VkDeviceSize offset, VkDeviceSize size);
48
49 bool full() const;
50 VkDeviceSize maximumAvailableSpace() const;
51 size_t totalAvailableSize() const;
52 size_t totalReservedSize() const;
53 size_t totalMemorySize() const;
54
55 Device* getDevice() { return _device; }
56 const Device* getDevice() const { return _device; }
57
58 protected:
59 virtual ~DeviceMemory();
60
61 VkDeviceMemory _deviceMemory;
62 VkMemoryRequirements _memoryRequirements;
63 VkMemoryPropertyFlags _properties;
64 ref_ptr<Device> _device;
65
66 mutable std::mutex _mutex;
67 MemorySlots _memorySlots;
68 };
69 VSG_type_name(vsg::DeviceMemory);
70
71 using DeviceMemoryList = std::list<ref_ptr<DeviceMemory>>;
72 extern VSG_DECLSPEC DeviceMemoryList getActiveDeviceMemoryList(VkMemoryPropertyFlagBits propertyFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
73
74 template<class T>
75 class MappedData : public T
76 {
77 public:
78 using value_type = typename T::value_type;
79
80 template<typename... Args>
81 MappedData(DeviceMemory* deviceMemory, VkDeviceSize offset, VkMemoryMapFlags flags, Args&... args) :
82 T(),
83 _deviceMemory(deviceMemory)
84 {
85 void* pData;
86 size_t numElements = (args * ...);
87 _deviceMemory->map(offset, numElements * sizeof(value_type), flags, &pData);
88 T::assign(args..., static_cast<value_type*>(pData));
89 }
90
91 template<typename... Args>
92 static ref_ptr<MappedData> create(DeviceMemory* deviceMemory, VkDeviceSize offset, VkMemoryMapFlags flags, Args... args)
93 {
94 return ref_ptr<MappedData>(new MappedData(deviceMemory, offset, flags, args...));
95 }
96
97 template<typename... Args>
98 static ref_ptr<MappedData> create(DeviceMemory* deviceMemory, VkDeviceSize offset, VkMemoryMapFlags flags, Data::Properties properties, Args... args)
99 {
100 auto data = ref_ptr<MappedData>(new MappedData(deviceMemory, offset, flags, args...));
101 data->properties = properties;
102 return data;
103 }
104
105 virtual ~MappedData()
106 {
107 T::dataRelease(); // make sure that the Array doesn't delete this memory
108 _deviceMemory->unmap();
109 }
110
111 protected:
112 ref_ptr<DeviceMemory> _deviceMemory;
113 };
114
115} // namespace vsg
vsg::Buffer
Definition Buffer.h:26
vsg::Data
Definition Data.h:110
vsg::Device
Device encapsulates VkDevice, a logical handle to the PhysicalDevice with capabilities specified duri...
Definition Device.h:39
vsg::DeviceMemory
Definition DeviceMemory.h:29
vsg::DeviceMemory::map
VkResult map(VkDeviceSize offset, VkDeviceSize size, VkMemoryMapFlags flags, void **ppData)
wrapper of vkMapMemory
vsg::Image
Image class encapsulates VkImage and VkImageCreateInfo settings used to set it up.
Definition Image.h:25
vsg::ref_ptr
Definition ref_ptr.h:22
vsg::Data::Properties
Definition Data.h:116