forked from vsg-dev/VulkanSceneGraph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Data.h
228 lines (173 loc) · 8.64 KB
/
Data.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#pragma once
/* <editor-fold desc="MIT License">
Copyright(c) 2018 Robert Osfield
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:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
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.
</editor-fold> */
#include <vsg/core/Allocator.h>
#include <vsg/core/Object.h>
#include <vsg/core/compare.h>
#include <vsg/core/type_name.h>
#include <vsg/vk/vulkan.h>
#include <cstring>
#include <vector>
namespace vsg
{
struct ModifiedCount
{
uint32_t count = 0;
bool operator==(const ModifiedCount& rhs) const { return count == rhs.count; }
bool operator!=(const ModifiedCount& rhs) const { return count != rhs.count; }
void operator++() { ++count; }
};
/** 64 bit block of compressed texel data.*/
using block64 = uint8_t[8];
/** 128 bit block of compressed texel data.*/
using block128 = uint8_t[16];
enum Origin : uint8_t
{
TOP_LEFT = 0,
BOTTOM_LEFT = 2
};
template<typename T>
struct stride_iterator
{
using value_type = T;
using iterator_category = std::forward_iterator_tag;
using difference_type = std::ptrdiff_t;
using pointer = T*;
using reference = T&;
value_type* ptr;
uint32_t stride; // stride in bytes
inline void advance()
{
if constexpr (std::is_const<value_type>::value)
ptr = reinterpret_cast<value_type*>(reinterpret_cast<const uint8_t*>(ptr) + stride);
else
ptr = reinterpret_cast<value_type*>(reinterpret_cast<uint8_t*>(ptr) + stride);
}
stride_iterator& operator++()
{
advance();
return *this;
}
stride_iterator operator++(int)
{
stride_iterator reval(*this);
advance();
return reval;
}
bool operator==(stride_iterator rhs) const { return ptr == rhs.ptr; }
bool operator!=(stride_iterator rhs) const { return ptr != rhs.ptr; }
bool operator<(stride_iterator rhs) const { return ptr < rhs.ptr; }
bool operator<=(stride_iterator rhs) const { return ptr <= rhs.ptr; }
bool operator>(stride_iterator rhs) const { return ptr > rhs.ptr; }
bool operator>=(stride_iterator rhs) const { return ptr >= rhs.ptr; }
value_type& operator*() { return *reinterpret_cast<value_type*>(ptr); }
value_type* operator->() { return reinterpret_cast<value_type*>(ptr); }
};
class VSG_DECLSPEC Data : public Object
{
public:
/* Layout used for specifying the format of the data, use of mipmaps, block compressed data and origin.
* Default of no mipmapping and {1,1,1} is uncompressed.
* A single block (Block64/Block128) is stored as a single value with the Data object. */
struct Layout
{
VkFormat format = VK_FORMAT_UNDEFINED;
uint32_t stride = 0;
uint8_t maxNumMipmaps = 0;
uint8_t blockWidth = 1;
uint8_t blockHeight = 1;
uint8_t blockDepth = 1;
uint8_t origin = TOP_LEFT; /// Hint for setting up texture coordinates, bit 0 x/width axis, bit 1 y/height axis, bit 2 z/depth axis. Vulkan origin for images is top left, which is denoted as 0 here.
int8_t imageViewType = -1; /// -1 signifies undefined VkImageViewType, if value >=0 then value should be treated as valid VkImageViewType
AllocatorType allocatorType = ALLOCATOR_TYPE_VSG_ALLOCATOR;
int compare(const Layout& rhs) const
{
return compare_region(format, allocatorType, rhs.format);
}
};
Data() {}
explicit Data(Layout layout) :
_layout(layout) {}
Data(Layout layout, uint32_t min_stride) :
_layout(layout)
{
if (_layout.stride < min_stride) _layout.stride = min_stride;
}
/// provide new and delete to enable custom memory management via the vsg::Allocator singleton, using the MEMORY_AFFINTY_DATA
static void* operator new(std::size_t count);
static void operator delete(void* ptr);
std::size_t sizeofObject() const noexcept override { return sizeof(Data); }
bool is_compatible(const std::type_info& type) const noexcept override { return typeid(Data) == type ? true : Object::is_compatible(type); }
int compare(const Object& rhs_object) const override
{
int result = Object::compare(rhs_object);
if (result != 0) return result;
auto& rhs = static_cast<decltype(*this)>(rhs_object);
if ((result = _layout.compare(rhs._layout))) return result;
// the shorter data is less
if (dataSize() < rhs.dataSize()) return -1;
if (dataSize() > rhs.dataSize()) return 1;
// if both empty then they must be equal
if (dataSize() == 0) return 0;
// use memcpy to compare the contents of the data
return std::memcmp(dataPointer(), rhs.dataPointer(), dataSize());
}
void read(Input& input) override;
void write(Output& output) const override;
/** Set Layout */
void setLayout(Layout layout)
{
VkFormat previous_format = _layout.format; // temporary hack to keep applications that call setFormat(..) before setLayout(..) working
uint32_t previous_stride = _layout.stride;
_layout = layout;
if (_layout.format == 0 && previous_format != 0) _layout.format = previous_format; // temporary hack to keep existing applications working
if (_layout.stride == 0 && previous_stride != 0) _layout.stride = previous_stride; // make sure the layout as a valid stride.
}
/** Get the Layout.*/
Layout& getLayout() { return _layout; }
/** Get the Layout.*/
Layout getLayout() const { return _layout; }
virtual std::size_t valueSize() const = 0;
virtual std::size_t valueCount() const = 0;
virtual std::size_t dataSize() const = 0;
virtual void* dataPointer() = 0;
virtual const void* dataPointer() const = 0;
virtual void* dataPointer(size_t index) = 0;
virtual const void* dataPointer(size_t index) const = 0;
virtual void* dataRelease() = 0;
virtual std::uint32_t dimensions() const = 0;
virtual std::uint32_t width() const = 0;
virtual std::uint32_t height() const = 0;
virtual std::uint32_t depth() const = 0;
bool contiguous() const { return valueSize() == _layout.stride; }
uint32_t stride() const { return _layout.stride ? _layout.stride : static_cast<uint32_t>(valueSize()); }
using MipmapOffsets = std::vector<std::size_t>;
MipmapOffsets computeMipmapOffsets() const;
static std::size_t computeValueCountIncludingMipmaps(std::size_t w, std::size_t h, std::size_t d, uint32_t maxNumMipmaps);
/// increment the ModifiedCount to signify the data has been modified
void dirty() { ++_modifiedCount; }
/// get the Data's ModifiedCount and return true if this changes the specified ModifiedCount
bool getModifiedCount(ModifiedCount& mc) const
{
if (_modifiedCount != mc)
{
mc = _modifiedCount;
return true;
}
else
return false;
}
/// return true if Data's ModifiedCount is different than the specified ModifiedCount
bool differentModifiedCount(const ModifiedCount& mc) const { return _modifiedCount != mc; }
protected:
virtual ~Data() {}
Layout _layout;
ModifiedCount _modifiedCount;
};
VSG_type_name(vsg::Data);
using DataList = std::vector<ref_ptr<Data>>;
} // namespace vsg