vsg  1.1.0
VulkanSceneGraph library
DeviceMemory.h
1 #pragma once
2 
3 /* <editor-fold desc="MIT License">
4 
5 Copyright(c) 2018 Robert Osfield
6 
7 Permission 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 
9 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
10 
11 THE 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/vk/Device.h>
17 
18 #include <map>
19 
20 namespace vsg
21 {
22  class Buffer;
23  class Image;
24 
27  class VSG_DECLSPEC DeviceMemory : public Inherit<Object, DeviceMemory>
28  {
29  public:
30  DeviceMemory(Device* device, const VkMemoryRequirements& memRequirements, VkMemoryPropertyFlags properties, void* pNextAllocInfo = nullptr);
31 
32  operator VkDeviceMemory() const { return _deviceMemory; }
33  VkDeviceMemory vk() const { return _deviceMemory; }
34 
35  void copy(VkDeviceSize offset, VkDeviceSize size, const void* src_data);
36  void copy(VkDeviceSize offset, const Data* data);
37 
39  VkResult map(VkDeviceSize offset, VkDeviceSize size, VkMemoryMapFlags flags, void** ppData);
40  void unmap();
41 
42  const VkMemoryRequirements& getMemoryRequirements() const { return _memoryRequirements; }
43  const VkMemoryPropertyFlags& getMemoryPropertyFlags() const { return _properties; }
44 
45  MemorySlots::OptionalOffset reserve(VkDeviceSize size);
46  void release(VkDeviceSize offset, VkDeviceSize size);
47 
48  bool full() const;
49  VkDeviceSize maximumAvailableSpace() const;
50  size_t totalAvailableSize() const;
51  size_t totalReservedSize() const;
52 
53  Device* getDevice() { return _device; }
54  const Device* getDevice() const { return _device; }
55 
56  protected:
57  virtual ~DeviceMemory();
58 
59  VkDeviceMemory _deviceMemory;
60  VkMemoryRequirements _memoryRequirements;
61  VkMemoryPropertyFlags _properties;
62  ref_ptr<Device> _device;
63 
64  mutable std::mutex _mutex;
65  MemorySlots _memorySlots;
66  };
67  VSG_type_name(vsg::DeviceMemory);
68 
69  template<class T>
70  class MappedData : public T
71  {
72  public:
73  using value_type = typename T::value_type;
74 
75  template<typename... Args>
76  MappedData(DeviceMemory* deviceMemory, VkDeviceSize offset, VkMemoryMapFlags flags, Args&... args) :
77  T(),
78  _deviceMemory(deviceMemory)
79  {
80  void* pData;
81  size_t numElements = (args * ...);
82  _deviceMemory->map(offset, numElements * sizeof(value_type), flags, &pData);
83  T::assign(args..., static_cast<value_type*>(pData));
84  }
85 
86  template<typename... Args>
87  static ref_ptr<MappedData> create(DeviceMemory* deviceMemory, VkDeviceSize offset, VkMemoryMapFlags flags, Args... args)
88  {
89  return ref_ptr<MappedData>(new MappedData(deviceMemory, offset, flags, args...));
90  }
91 
92  template<typename... Args>
93  static ref_ptr<MappedData> create(DeviceMemory* deviceMemory, VkDeviceSize offset, VkMemoryMapFlags flags, Data::Properties properties, Args... args)
94  {
95  auto data = ref_ptr<MappedData>(new MappedData(deviceMemory, offset, flags, args...));
96  data->properties = properties;
97  return data;
98  }
99 
100  virtual ~MappedData()
101  {
102  T::dataRelease(); // make sure that the Array doesn't delete this memory
103  _deviceMemory->unmap();
104  }
105 
106  protected:
107  ref_ptr<DeviceMemory> _deviceMemory;
108  };
109 
110 } // namespace vsg
Definition: Data.h:110
Definition: DeviceMemory.h:28
VkResult map(VkDeviceSize offset, VkDeviceSize size, VkMemoryMapFlags flags, void **ppData)
wrapper of vkMapMemory
Device encapsulates VkDevice, a logical handle to the PhysicalDevice with capabilities specified duri...
Definition: Device.h:37
Definition: Inherit.h:28
Definition: DeviceMemory.h:71
Definition: ref_ptr.h:22
Definition: Data.h:116