vsg  1.1.0
VulkanSceneGraph library
Buffer.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/vk/DeviceMemory.h>
16 #include <vsg/vk/vk_buffer.h>
17 
18 namespace vsg
19 {
20  // forward declare
21  class Context;
22 
25  class VSG_DECLSPEC Buffer : public Inherit<Object, Buffer>
26  {
27  public:
28  Buffer(VkDeviceSize in_size, VkBufferUsageFlags in_usage, VkSharingMode in_sharingMode);
29 
31  VkBuffer vk(uint32_t deviceID) const { return _vulkanData[deviceID].buffer; }
32 
33  // VkBufferCreateInfo settings
34  VkBufferCreateFlags flags = 0;
35  VkDeviceSize size;
36  VkBufferUsageFlags usage;
37  VkSharingMode sharingMode;
38 
40  uint32_t sizeVulkanData() const { return _vulkanData.size(); }
41 
42  VkResult bind(DeviceMemory* deviceMemory, VkDeviceSize memoryOffset);
43 
44  MemorySlots::OptionalOffset reserve(VkDeviceSize in_size, VkDeviceSize alignment);
45  void release(VkDeviceSize offset, VkDeviceSize in_size);
46 
47  bool full() const;
48  size_t maximumAvailableSpace() const;
49  size_t totalAvailableSize() const;
50  size_t totalReservedSize() const;
51 
52  VkMemoryRequirements getMemoryRequirements(uint32_t deviceID) const;
53 
54  DeviceMemory* getDeviceMemory(uint32_t deviceID) { return _vulkanData[deviceID].deviceMemory; }
55  const DeviceMemory* getDeviceMemory(uint32_t deviceID) const { return _vulkanData[deviceID].deviceMemory; }
56 
57  VkDeviceSize getMemoryOffset(uint32_t deviceID) const { return _vulkanData[deviceID].memoryOffset; }
58 
59  virtual bool compile(Device* device);
60  virtual bool compile(Context& context);
61 
62  protected:
63  virtual ~Buffer();
64 
65  struct VulkanData
66  {
67  VkBuffer buffer = VK_NULL_HANDLE;
68  ref_ptr<DeviceMemory> deviceMemory;
69  VkDeviceSize memoryOffset = 0;
70  VkDeviceSize size = 0;
71  ref_ptr<Device> device;
72 
73  void release();
74  };
75 
76  vk_buffer<VulkanData> _vulkanData;
77 
78  mutable std::mutex _mutex;
79  MemorySlots _memorySlots;
80  };
81  VSG_type_name(vsg::Buffer);
82 
83  extern VSG_DECLSPEC ref_ptr<Buffer> createBufferAndMemory(Device* device, VkDeviceSize in_size, VkBufferUsageFlags in_usage, VkSharingMode in_sharingMode, VkMemoryPropertyFlags memoryProperties);
84 
85 } // namespace vsg
Definition: Buffer.h:26
VkBuffer vk(uint32_t deviceID) const
Vulkan VkImage handle.
Definition: Buffer.h:31
uint32_t sizeVulkanData() const
return the number of VulkanData entries.
Definition: Buffer.h:40
Definition: DeviceMemory.h:28
Device encapsulates VkDevice, a logical handle to the PhysicalDevice with capabilities specified duri...
Definition: Device.h:37
Definition: Inherit.h:28
Definition: MemorySlots.h:36
Definition: ref_ptr.h:22
Definition: Buffer.h:66
vk_buffer that manages a single logical device supported.
Definition: vk_buffer.h:28