vsg 1.1.3
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
29 {
30 uint32_t count = 0;
31
32 bool operator==(const ModifiedCount& rhs) const { return count == rhs.count; }
33 bool operator!=(const ModifiedCount& rhs) const { return count != rhs.count; }
34
35 void operator++() { ++count; }
36 };
37
39 struct block64
40 {
41 uint8_t value[8];
42 };
43
45 struct block128
46 {
47 uint8_t value[16];
48 };
49
50 enum Origin : uint8_t
51 {
52 TOP_LEFT = 0,
53 BOTTOM_LEFT = 2
54 };
55
56 enum DataVariance : uint8_t
57 {
58 STATIC_DATA = 0,
59 STATIC_DATA_UNREF_AFTER_TRANSFER = 1,
60 DYNAMIC_DATA = 2,
61 DYNAMIC_DATA_TRANSFER_AFTER_RECORD = 3
62 };
63
64 template<typename T>
66 {
67 using value_type = T;
68 using iterator_category = std::forward_iterator_tag;
69 using difference_type = std::ptrdiff_t;
70 using pointer = T*;
71 using reference = T&;
72
73 value_type* ptr;
74 uint32_t stride; // stride in bytes
75
76 inline void advance()
77 {
78 if constexpr (std::is_const<value_type>::value)
79 ptr = reinterpret_cast<value_type*>(reinterpret_cast<const uint8_t*>(ptr) + stride);
80 else
81 ptr = reinterpret_cast<value_type*>(reinterpret_cast<uint8_t*>(ptr) + stride);
82 }
83
84 stride_iterator& operator++()
85 {
86 advance();
87 return *this;
88 }
89 stride_iterator operator++(int)
90 {
91 stride_iterator reval(*this);
92 advance();
93 return reval;
94 }
95
96 bool operator==(stride_iterator rhs) const { return ptr == rhs.ptr; }
97 bool operator!=(stride_iterator rhs) const { return ptr != rhs.ptr; }
98 bool operator<(stride_iterator rhs) const { return ptr < rhs.ptr; }
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
103 value_type& operator*() { return *reinterpret_cast<value_type*>(ptr); }
104 value_type* operator->() { return reinterpret_cast<value_type*>(ptr); }
105 };
106
109 class VSG_DECLSPEC Data : public Object
110 {
111 public:
112 /* Properties used for specifying the format of the data, use of mipmaps, block compressed data and origin.
113 * Default of no mipmapping and {1,1,1} is uncompressed.
114 * A single block (Block64/Block128) is stored as a single value with the Data object. */
115 struct VSG_DECLSPEC Properties
116 {
117 Properties() = default;
118 Properties(const Properties& rhs) = default;
119 explicit Properties(VkFormat in_format) :
120 format(in_format) {}
121
122 VkFormat format = VK_FORMAT_UNDEFINED;
123 uint32_t stride = 0;
124 uint8_t maxNumMipmaps = 0;
125 uint8_t blockWidth = 1;
126 uint8_t blockHeight = 1;
127 uint8_t blockDepth = 1;
128 uint8_t origin = TOP_LEFT;
129 int8_t imageViewType = -1;
130 DataVariance dataVariance = STATIC_DATA;
131 AllocatorType allocatorType = ALLOCATOR_TYPE_VSG_ALLOCATOR;
132
133 int compare(const Properties& rhs) const;
134 Properties& operator=(const Properties& rhs);
135 };
136
137 Data() {}
138
139 Data(const Data& data, const CopyOp& copyop = {}) :
140 Object(data, copyop), properties(data.properties) {}
141
142 explicit Data(Properties layout) :
143 properties(layout) {}
144
145 Data(Properties layout, uint32_t min_stride) :
146 properties(layout)
147 {
148 if (properties.stride < min_stride) properties.stride = min_stride;
149 }
150
152 static void* operator new(size_t count);
153 static void operator delete(void* ptr);
154
155 size_t sizeofObject() const noexcept override { return sizeof(Data); }
156 bool is_compatible(const std::type_info& type) const noexcept override { return typeid(Data) == type || Object::is_compatible(type); }
157
158 int compare(const Object& rhs_object) const override;
159
160 void read(Input& input) override;
161 void write(Output& output) const override;
162
165
166 bool dynamic() const { return properties.dataVariance >= DYNAMIC_DATA; }
167
168 virtual size_t valueSize() const = 0;
169 virtual size_t valueCount() const = 0;
170
171 virtual bool dataAvailable() const = 0;
172 virtual size_t dataSize() const = 0;
173
174 virtual void* dataPointer() = 0;
175 virtual const void* dataPointer() const = 0;
176
177 virtual void* dataPointer(size_t index) = 0;
178 virtual const void* dataPointer(size_t index) const = 0;
179
180 virtual void* dataRelease() = 0;
181
182 virtual uint32_t dimensions() const = 0;
183
184 virtual uint32_t width() const = 0;
185 virtual uint32_t height() const = 0;
186 virtual uint32_t depth() const = 0;
187
188 bool contiguous() const { return valueSize() == properties.stride; }
189
190 uint32_t stride() const { return properties.stride ? properties.stride : static_cast<uint32_t>(valueSize()); }
191
192 using MipmapOffsets = std::vector<size_t>;
193 MipmapOffsets computeMipmapOffsets() const;
194 static size_t computeValueCountIncludingMipmaps(size_t w, size_t h, size_t d, uint32_t maxNumMipmaps);
195
197 void dirty() { ++_modifiedCount; }
198
201 {
202 if (_modifiedCount != mc)
203 {
204 mc = _modifiedCount;
205 return true;
206 }
207 else
208 return false;
209 }
210
212 bool differentModifiedCount(const ModifiedCount& mc) const { return _modifiedCount != mc; }
213
214 protected:
215 virtual ~Data() {}
216
217 ModifiedCount _modifiedCount;
218
219#if 1
220 public:
223
225 void setLayout(Layout layout)
226 {
227 VkFormat previous_format = properties.format; // temporary hack to keep applications that call setFormat(..) before setLayout(..) working
228 uint32_t previous_stride = properties.stride;
229 properties = layout;
230 if (properties.format == 0 && previous_format != 0) properties.format = previous_format; // temporary hack to keep existing applications working
231 if (properties.stride == 0 && previous_stride != 0) properties.stride = previous_stride; // make sure the layout has a valid stride.
232 }
234 Layout& getLayout() { return properties; }
236 Layout getLayout() const { return properties; }
237#endif
238 };
239 VSG_type_name(vsg::Data);
240
241 using DataList = std::vector<ref_ptr<Data>>;
242
243} // namespace vsg
Definition Object.h:42
Definition Data.h:110
Properties properties
properties of the data such as format, origin, stride, dataVariance etc.
Definition Data.h:164
bool differentModifiedCount(const ModifiedCount &mc) const
return true if Data's ModifiedCount is different from the specified ModifiedCount
Definition Data.h:212
Layout getLayout() const
deprecated: use data->properties
Definition Data.h:236
Layout & getLayout()
deprecated: use data->properties
Definition Data.h:234
void setLayout(Layout layout)
deprecated: use data->properties = properties instead.
Definition Data.h:225
void dirty()
increment the ModifiedCount to signify the data has been modified
Definition Data.h:197
bool getModifiedCount(ModifiedCount &mc) const
get the Data's ModifiedCount and return true if this changes the specified ModifiedCount
Definition Data.h:200
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,...
Definition Input.h:44
Definition Object.h:60
Definition Output.h:41
Definition Data.h:116
DataVariance dataVariance
-1 signifies undefined VkImageViewType, if value >=0 then value should be treated as valid VkImageVie...
Definition Data.h:130
ModifiedCount provides a count value to keep track of modifications to data.
Definition Data.h:29
Definition Data.h:46
Definition Data.h:40
Definition Data.h:66