vsg  1.0.4
VulkanSceneGraph library
ImageInfo.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/state/ImageView.h>
16 #include <vsg/state/Sampler.h>
17 
18 namespace vsg
19 {
20 
22  class VSG_DECLSPEC ImageInfo : public Inherit<Object, ImageInfo>
23  {
24  public:
25  ImageInfo() :
26  imageLayout(VK_IMAGE_LAYOUT_UNDEFINED) {}
27 
28  ImageInfo(ref_ptr<Sampler> in_sampler, ref_ptr<ImageView> in_imageView, VkImageLayout in_imageLayout = VK_IMAGE_LAYOUT_UNDEFINED) :
29  sampler(in_sampler),
30  imageView(in_imageView),
31  imageLayout(in_imageLayout)
32  {
33  computeNumMipMapLevels();
34  }
35 
36  // Convenience constructor that creates a vsg::ImageView and vsg::Image to represent the data on the GPU.
37  template<typename T>
38  ImageInfo(ref_ptr<Sampler> in_sampler, ref_ptr<T> in_data, VkImageLayout in_imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) :
39  sampler(in_sampler),
40  imageLayout(in_imageLayout)
41  {
42  auto image = Image::create(in_data);
43  image->usage |= (VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT);
44 
45  imageView = ImageView::create(image);
46 
47  computeNumMipMapLevels();
48  }
49 
50  ImageInfo(const ImageInfo&) = delete;
51  ImageInfo& operator=(const ImageInfo&) = delete;
52 
53  explicit operator bool() const { return sampler.valid() && imageView.valid(); }
54 
55  int compare(const Object& rhs_object) const override;
56 
57  void computeNumMipMapLevels();
58 
59  ref_ptr<Sampler> sampler;
60  ref_ptr<ImageView> imageView;
61  VkImageLayout imageLayout = VK_IMAGE_LAYOUT_UNDEFINED;
62 
64  bool requiresCopy(uint32_t deviceID) const
65  {
66  if (!imageView || !imageView->image) return false;
67  auto& data = imageView->image->data;
68  return data && data->differentModifiedCount(copiedModifiedCounts[deviceID]);
69  }
70 
72  bool syncModifiedCounts(uint32_t deviceID)
73  {
74  if (!imageView || !imageView->image) return false;
75  auto& data = imageView->image->data;
76  return data && data->getModifiedCount(copiedModifiedCounts[deviceID]);
77  }
78 
79  vk_buffer<ModifiedCount> copiedModifiedCounts;
80 
81  protected:
82  virtual ~ImageInfo();
83  };
84  VSG_type_name(vsg::ImageInfo);
85 
86  using ImageInfoList = std::vector<ref_ptr<ImageInfo>>;
87 
89  struct FormatTraits
90  {
91  int size = 0;
92  int numBitsPerComponent = 0;
93  int numComponents = 0;
94  bool packed = false;
95  int blockWidth = 1;
96  int blockHeight = 1;
97  int blockDepth = 1;
98  uint8_t defaultValue[32];
99 
100  template<typename T>
101  void assign4(T value)
102  {
103  T* ptr = reinterpret_cast<T*>(defaultValue);
104  (*ptr++) = value;
105  (*ptr++) = value;
106  (*ptr++) = value;
107  (*ptr++) = value;
108  }
109  };
110 
112  extern VSG_DECLSPEC FormatTraits getFormatTraits(VkFormat format, bool default_one = true);
113 
115  extern VSG_DECLSPEC uint32_t computeNumMipMapLevels(const Data* data, const Sampler* sampler);
116 
117 } // namespace vsg
Definition: Data.h:104
ImageInfo class provides the VkDescriptorImageInfo settings used when setting up vsg::/vkDescriptorIm...
Definition: ImageInfo.h:23
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 syncModifiedCounts(uint32_t deviceID)
return true if the ImageInfo's data has been modified and should be copied to the buffer,...
Definition: ImageInfo.h:72
bool requiresCopy(uint32_t deviceID) const
return true if the ImageInfo's data has been modified and should be copied to the buffer
Definition: ImageInfo.h:64
Definition: Inherit.h:28
Definition: Object.h:42
Sampler encapsulates the VkSampler and the VkSamplerCreateInfo settings used to set it up.
Definition: Sampler.h:24
Definition: ref_ptr.h:22
format traits hints that can be used when initialize image data
Definition: ImageInfo.h:90
vk_buffer that manages a single logical device support.
Definition: vk_buffer.h:28