vsg  1.1.0
VulkanSceneGraph library
Image.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 
24  class VSG_DECLSPEC Image : public Inherit<Object, Image>
25  {
26  public:
28  Image(ref_ptr<Data> in_data = {});
29 
31  Image(VkImage image, Device* device);
32 
34  VkImage vk(uint32_t deviceID) const { return _vulkanData[deviceID].image; }
35 
38  VkImageCreateFlags flags = 0;
39  VkImageType imageType = VK_IMAGE_TYPE_2D;
40  VkFormat format = VK_FORMAT_UNDEFINED;
41  VkExtent3D extent = {0, 0, 0};
42  uint32_t mipLevels = 0;
43  uint32_t arrayLayers = 0;
44  VkSampleCountFlagBits samples = VK_SAMPLE_COUNT_1_BIT;
45  VkImageTiling tiling = VK_IMAGE_TILING_OPTIMAL;
46  VkImageUsageFlags usage = 0;
47  VkSharingMode sharingMode = VK_SHARING_MODE_EXCLUSIVE;
48  std::vector<uint32_t> queueFamilyIndices;
49  VkImageLayout initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
50 
51  int compare(const Object& rhs_object) const override;
52 
53  DeviceMemory* getDeviceMemory(uint32_t deviceID) { return _vulkanData[deviceID].deviceMemory; }
54  const DeviceMemory* getDeviceMemory(uint32_t deviceID) const { return _vulkanData[deviceID].deviceMemory; }
55 
56  VkDeviceSize getMemoryOffset(uint32_t deviceID) const { return _vulkanData[deviceID].memoryOffset; }
57 
58  VkMemoryRequirements getMemoryRequirements(uint32_t deviceID) const;
59 
60  VkResult allocateAndBindMemory(Device* device, VkMemoryPropertyFlags memoryProperties = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, void* pNextAllocInfo = nullptr);
61 
62  VkResult bind(DeviceMemory* deviceMemory, VkDeviceSize memoryOffset);
63 
65  bool requiresCopy(uint32_t deviceID) const { return data && data->differentModifiedCount(_vulkanData[deviceID].copiedModifiedCount); }
66 
68  bool syncModifiedCount(uint32_t deviceID) { return data && data->getModifiedCount(_vulkanData[deviceID].copiedModifiedCount); }
69 
70  virtual void compile(Device* device);
71  virtual void compile(Context& context);
72 
73  protected:
74  virtual ~Image();
75 
76  struct VulkanData
77  {
78  VkImage image = VK_NULL_HANDLE;
79  ref_ptr<DeviceMemory> deviceMemory;
80  VkDeviceSize memoryOffset = 0;
81  VkDeviceSize size = 0;
82  ref_ptr<Device> device;
83  bool requiresDataCopy = false;
84  ModifiedCount copiedModifiedCount;
85 
86  void release();
87  };
88 
89  vk_buffer<VulkanData> _vulkanData;
90  };
91  VSG_type_name(vsg::Image);
92 
93 } // namespace vsg
Definition: Context.h:67
Definition: DeviceMemory.h:28
Device encapsulates VkDevice, a logical handle to the PhysicalDevice with capabilities specified duri...
Definition: Device.h:37
Image class encapsulates VkImage and VkImageCreateInfo settings used to set it up.
Definition: Image.h:25
Image(VkImage image, Device *device)
create a vsg::Image wrapper for specified VkImage
VkImage vk(uint32_t deviceID) const
Vulkan VkImage handle.
Definition: Image.h:34
bool requiresCopy(uint32_t deviceID) const
return true if the Image's data has been modified and should be copied to the buffer.
Definition: Image.h:65
ref_ptr< Data > data
VkImageCreateInfo settings.
Definition: Image.h:37
int compare(const Object &rhs_object) const override
compare two objects, return -1 if this object is less than rhs, return 0 if it's equal,...
bool syncModifiedCount(uint32_t deviceID)
return true if the Image's data has been modified and should be copied to the buffer,...
Definition: Image.h:68
Image(ref_ptr< Data > in_data={})
create a vsg::Image, optional Data is used to initialize createInfo, delay VkImage creation to compil...
Definition: Inherit.h:28
Definition: Object.h:42
Definition: ref_ptr.h:22
Definition: Image.h:77
ModifiedCount provides a count value to keep track of modifications to data.
Definition: Data.h:29
vk_buffer that manages a single logical device supported.
Definition: vk_buffer.h:28