vsg: include/vsg/core/Data.h Source File

vsg 1.1.13 VulkanSceneGraph library

Loading...
Searching...
No Matches
Data.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/Allocator.h>
16#include <vsg/core/Object.h>
17#include <vsg/core/compare.h>
18#include <vsg/core/type_name.h>
19#include <vsg/vk/vulkan.h>
20
21#include <cstring>
22#include <vector>
23
24namespace vsg
25{
26
27 // forward declaer MipmapLayout
28 class MipmapLayout;
29
31 struct ModifiedCount
32 {
33 uint32_t count = 0;
34
35 bool operator==(const ModifiedCount& rhs) const { return count == rhs.count; }
36 bool operator!=(const ModifiedCount& rhs) const { return count != rhs.count; }
37
38 void operator++() { ++count; }
39 };
40
42 struct block64
43 {
44 uint8_t value[8];
45 };
46
48 struct block128
49 {
50 uint8_t value[16];
51 };
52
53 enum Origin : uint8_t
54 {
55 TOP_LEFT = 0,
56 BOTTOM_LEFT = 2
57 };
58
59 enum DataVariance : uint8_t
60 {
61 STATIC_DATA = 0,
62 STATIC_DATA_UNREF_AFTER_TRANSFER = 1,
63 DYNAMIC_DATA = 2,
64 DYNAMIC_DATA_TRANSFER_AFTER_RECORD = 3
65 };
66
67 template<typename T>
68 struct stride_iterator
69 {
70 using value_type = T;
71 using iterator_category = std::forward_iterator_tag;
72 using difference_type = std::ptrdiff_t;
73 using pointer = T*;
74 using reference = T&;
75
76 value_type* ptr;
77 uint32_t stride; // stride in bytes
78
79 inline void advance()
80 {
81 if constexpr (std::is_const<value_type>::value)
82 ptr = reinterpret_cast<value_type*>(reinterpret_cast<const uint8_t*>(ptr) + stride);
83 else
84 ptr = reinterpret_cast<value_type*>(reinterpret_cast<uint8_t*>(ptr) + stride);
85 }
86
87 stride_iterator& operator++()
88 {
89 advance();
90 return *this;
91 }
92 stride_iterator operator++(int)
93 {
94 stride_iterator reval(*this);
95 advance();
96 return reval;
97 }
98
99 bool operator==(stride_iterator rhs) const { return ptr == rhs.ptr; }
100 bool operator!=(stride_iterator rhs) const { return ptr != rhs.ptr; }
101 bool operator<(stride_iterator rhs) const { return ptr < rhs.ptr; }
102 bool operator<=(stride_iterator rhs) const { return ptr <= rhs.ptr; }
103 bool operator>(stride_iterator rhs) const { return ptr > rhs.ptr; }
104 bool operator>=(stride_iterator rhs) const { return ptr >= rhs.ptr; }
105
106 value_type& operator*() { return *reinterpret_cast<value_type*>(ptr); }
107 value_type* operator->() { return reinterpret_cast<value_type*>(ptr); }
108 };
109
110 class MipmapLayout;
111
114 class VSG_DECLSPEC Data : public Object
115 {
116 public:
117 /* Properties used for specifying the format of the data, use of mipmaps, block compressed data and origin.
118 * Default of no mipmapping and {1,1,1} is uncompressed.
119 * A single block (Block64/Block128) is stored as a single value with the Data object. */
120 struct VSG_DECLSPEC Properties
121 {
122 Properties() = default;
123 Properties(const Properties& rhs) = default;
124 explicit Properties(VkFormat in_format) :
125 format(in_format) {}
126
127 VkFormat format = VK_FORMAT_UNDEFINED;
128 uint32_t stride = 0;
129 uint8_t mipLevels = 0;
130 uint8_t blockWidth = 1;
131 uint8_t blockHeight = 1;
132 uint8_t blockDepth = 1;
133 uint8_t origin = TOP_LEFT;
134 int8_t imageViewType = -1;
135 DataVariance dataVariance = STATIC_DATA;
136 AllocatorType allocatorType = ALLOCATOR_TYPE_VSG_ALLOCATOR;
137
138 int compare(const Properties& rhs) const;
139 Properties& operator=(const Properties& rhs);
140 };
141
142 Data() {}
143
144 Data(const Data& data, const CopyOp& copyop = {}) :
145 Object(data, copyop), properties(data.properties) {}
146
147 explicit Data(Properties layout) :
148 properties(layout) {}
149
150 Data(Properties layout, uint32_t min_stride) :
151 properties(layout)
152 {
153 if (properties.stride < min_stride) properties.stride = min_stride;
154 }
155
157 static void* operator new(size_t count);
158 static void operator delete(void* ptr);
159
160 size_t sizeofObject() const noexcept override { return sizeof(Data); }
161 bool is_compatible(const std::type_info& type) const noexcept override { return typeid(Data) == type || Object::is_compatible(type); }
162
163 int compare(const Object& rhs_object) const override;
164
165 void read(Input& input) override;
166 void write(Output& output) const override;
167
169 Properties properties;
170
171 bool dynamic() const { return properties.dataVariance >= DYNAMIC_DATA; }
172
173 virtual size_t valueSize() const = 0;
174 virtual size_t valueCount() const = 0;
175
176 virtual bool dataAvailable() const = 0;
177 virtual size_t dataSize() const = 0;
178
179 virtual void* dataPointer() = 0;
180 virtual const void* dataPointer() const = 0;
181
182 virtual void* dataPointer(size_t index) = 0;
183 virtual const void* dataPointer(size_t index) const = 0;
184
185 virtual void* dataRelease() = 0;
186
187 virtual uint32_t dimensions() const = 0;
188
189 virtual uint32_t width() const = 0;
190 virtual uint32_t height() const = 0;
191 virtual uint32_t depth() const = 0;
192
194 std::tuple<uint32_t, uint32_t, uint32_t> pixelExtents() const;
195
196 bool contiguous() const { return valueSize() == properties.stride; }
197
198 uint32_t stride() const { return properties.stride ? properties.stride : static_cast<uint32_t>(valueSize()); }
199
200 size_t computeValueCountIncludingMipmaps() const;
201
203 void dirty() { ++_modifiedCount; }
204
206 bool getModifiedCount(ModifiedCount& mc) const
207 {
208 if (_modifiedCount != mc)
209 {
210 mc = _modifiedCount;
211 return true;
212 }
213 else
214 return false;
215 }
216
218 bool differentModifiedCount(const ModifiedCount& mc) const { return _modifiedCount != mc; }
219
221 void setMipmapLayout(MipmapLayout* mipmapData);
222
224 const MipmapLayout* getMipmapLayout() const;
225
226 protected:
227 virtual ~Data() {}
228
229 void _copy(const Data& rhs);
230 void _clear();
231
232 ModifiedCount _modifiedCount;
233
234#if 1
235 public:
237 using Layout = Properties;
238
240 void setLayout(Layout layout)
241 {
242 VkFormat previous_format = properties.format; // temporary hack to keep applications that call setFormat(..) before setLayout(..) working
243 uint32_t previous_stride = properties.stride;
244 properties = layout;
245 if (properties.format == 0 && previous_format != 0) properties.format = previous_format; // temporary hack to keep existing applications working
246 if (properties.stride == 0 && previous_stride != 0) properties.stride = previous_stride; // make sure the layout has a valid stride.
247 }
248
249 Layout& getLayout() { return properties; }
251 Layout getLayout() const { return properties; }
252#endif
253 };
254 VSG_type_name(vsg::Data);
255
256 using DataList = std::vector<ref_ptr<Data>>;
257
258} // namespace vsg
vsg::CopyOp
Definition Object.h:42
vsg::Data
Definition Data.h:115
vsg::Data::properties
Properties properties
properties of the data such as format, origin, stride, dataVariance etc.
Definition Data.h:169
vsg::Data::Layout
Properties Layout
deprecated: provided for backwards compatibility, use Properties instead.
Definition Data.h:237
vsg::Data::differentModifiedCount
bool differentModifiedCount(const ModifiedCount &mc) const
return true if Data's ModifiedCount is different from the specified ModifiedCount
Definition Data.h:218
vsg::Data::setMipmapLayout
void setMipmapLayout(MipmapLayout *mipmapData)
set the MipmapLayout, only required when the data contains mipmaps that use block compressed formats ...
vsg::Data::getLayout
Layout getLayout() const
deprecated: use data->properties
Definition Data.h:251
vsg::Data::getLayout
Layout & getLayout()
deprecated: use data->properties
Definition Data.h:249
vsg::Data::setLayout
void setLayout(Layout layout)
deprecated: use data->properties = properties instead.
Definition Data.h:240
vsg::Data::getMipmapLayout
const MipmapLayout * getMipmapLayout() const
get the MipmapLayout if assigned.
vsg::Data::dirty
void dirty()
increment the ModifiedCount to signify the data has been modified
Definition Data.h:203
vsg::Data::pixelExtents
std::tuple< uint32_t, uint32_t, uint32_t > pixelExtents() const
return the {width, height, depth} pixel extents of an image accounting for blockWidth and any mipmapD...
vsg::Data::getModifiedCount
bool getModifiedCount(ModifiedCount &mc) const
get the Data's ModifiedCount and return true if this changes the specified ModifiedCount
Definition Data.h:206
vsg::Data::compare
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,...
vsg::Input
Definition Input.h:44
vsg::MipmapLayout
Definition MipmapLayout.h:24
vsg::Output
Definition Output.h:41
vsg::Data::Properties
Definition Data.h:121
vsg::Data::Properties::dataVariance
DataVariance dataVariance
-1 signifies undefined VkImageViewType, if value >=0 then value should be treated as valid VkImageVie...
Definition Data.h:135
vsg::Data::Properties::allocatorType
AllocatorType allocatorType
hint as to how the data values may change during the lifetime of the vsg::Data.
Definition Data.h:136
vsg::Data::Properties::imageViewType
int8_t imageViewType
Hint for setting up texture coordinates, bit 0 x/width axis, bit 1 y/height axis, bit 2 z/depth axis....
Definition Data.h:134
vsg::ModifiedCount
ModifiedCount provides a count value to keep track of modifications to data.
Definition Data.h:32
vsg::block128
Definition Data.h:49
vsg::block64
Definition Data.h:43
vsg::stride_iterator
Definition Data.h:69