-
Notifications
You must be signed in to change notification settings - Fork 0
/
Resource.h
377 lines (294 loc) · 7.17 KB
/
Resource.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#pragma once
#include <ATen/native/vulkan/api/Common.h>
#include <ATen/native/vulkan/api/Allocator.h>
#include <ATen/native/vulkan/api/Cache.h>
#include <c10/util/hash.h>
namespace at {
namespace native {
namespace vulkan {
namespace api {
struct Resource final {
class Pool;
//
// Memory
//
struct Memory final {
/*
Barrier
*/
struct Barrier final {
VkAccessFlags src;
VkAccessFlags dst;
};
VmaAllocator allocator;
VmaAllocation allocation;
class Scope;
template<typename Type>
using Data = Handle<Type, Scope>;
template<
typename Type,
typename Pointer = std::add_pointer_t<std::add_const_t<Type>>>
Data<Pointer> map() const &;
template<
typename Type,
typename Pointer = std::add_pointer_t<Type>>
Data<Pointer> map() &;
private:
// Intentionally disabed to ensure memory access is always properly
// encapsualted in a scoped map-unmap region. Allowing below overloads
// to be invoked on a temporary would open the door to the possibility
// of accessing the underlying memory out of the expected scope making
// for seemingly ineffective memory writes and hard to hunt down bugs.
template<typename Type, typename Pointer>
Data<Pointer> map() const && = delete;
template<typename Type, typename Pointer>
Data<Pointer> map() && = delete;
};
//
// Buffer
//
struct Buffer final {
/*
Descriptor
*/
struct Descriptor final {
VkDeviceSize size;
struct {
VkBufferUsageFlags buffer;
VmaMemoryUsage memory;
} usage;
};
/*
Object
*/
struct Object final {
VkBuffer handle;
VkDeviceSize offset;
VkDeviceSize range;
operator bool() const;
};
/*
Barrier
*/
struct Barrier final {
Object object;
Memory::Barrier memory;
};
Object object;
Memory memory;
operator bool() const;
};
//
// Image
//
struct Image final {
//
// Sampler
//
struct Sampler final {
/*
Descriptor
*/
struct Descriptor final {
VkFilter filter;
VkSamplerMipmapMode mipmap_mode;
VkSamplerAddressMode address_mode;
VkBorderColor border;
};
/*
Factory
*/
class Factory final {
public:
explicit Factory(const GPU& gpu);
typedef Sampler::Descriptor Descriptor;
typedef VK_DELETER(Sampler) Deleter;
typedef Handle<VkSampler, Deleter> Handle;
struct Hasher {
size_t operator()(const Descriptor& descriptor) const;
};
Handle operator()(const Descriptor& descriptor) const;
private:
VkDevice device_;
};
/*
Cache
*/
typedef api::Cache<Factory> Cache;
Cache cache;
explicit Sampler(const GPU& gpu)
: cache(Factory(gpu)) {
}
};
/*
Descriptor
*/
struct Descriptor final {
VkImageType type;
VkFormat format;
VkExtent3D extent;
struct {
VkImageUsageFlags image;
VmaMemoryUsage memory;
} usage;
struct {
VkImageViewType type;
VkFormat format;
} view;
Sampler::Descriptor sampler;
};
/*
Object
*/
struct Object final {
VkImage handle;
VkImageLayout layout;
VkImageView view;
VkSampler sampler;
operator bool() const;
};
/*
Barrier
*/
struct Barrier final {
Object object;
Memory::Barrier memory;
struct {
VkImageLayout src;
VkImageLayout dst;
} layout;
};
Object object;
Memory memory;
operator bool() const;
};
//
// Fence
//
struct Fence final {
Pool* pool;
size_t id;
operator bool() const;
VkFence handle(bool add_to_waitlist = true) const;
void wait(uint64_t timeout_nanoseconds = UINT64_MAX);
};
//
// Pool
//
class Pool final {
public:
explicit Pool(const GPU& gpu);
Pool(const Pool&) = delete;
Pool& operator=(const Pool&) = delete;
Pool(Pool&&);
Pool& operator=(Pool&&);
~Pool();
Buffer buffer(const Buffer::Descriptor& descriptor);
Image image(const Image::Descriptor& descriptor);
Fence fence();
void purge();
private:
friend struct Fence;
private:
struct Configuration final {
static constexpr uint32_t kReserve = 256u;
};
VkDevice device_;
Handle<VmaAllocator, void(*)(VmaAllocator)> allocator_;
struct {
std::vector<Handle<Buffer, void(*)(const Buffer&)>> pool;
} buffer_;
struct {
std::vector<Handle<Image, void(*)(const Image&)>> pool;
Image::Sampler sampler;
} image_;
struct {
std::vector<Handle<VkFence, VK_DELETER(Fence)>> pool;
mutable std::vector<VkFence> waitlist;
size_t in_use;
} fence_;
} pool;
explicit Resource(const GPU& gpu)
: pool(gpu) {
}
};
//
// Impl
//
class Resource::Memory::Scope final {
public:
enum class Access {
Read,
Write,
};
Scope(VmaAllocator allocator, VmaAllocation allocation, Access access);
void operator()(const void* data) const;
private:
VmaAllocator allocator_;
VmaAllocation allocation_;
Access access_;
};
template<typename, typename Pointer>
inline Resource::Memory::Data<Pointer> Resource::Memory::map() const & {
void* map(const Memory& memory);
return Data<Pointer>{
reinterpret_cast<Pointer>(map(*this)),
Scope(allocator, allocation, Scope::Access::Read),
};
}
template<typename, typename Pointer>
inline Resource::Memory::Data<Pointer> Resource::Memory::map() & {
void* map(const Memory& memory);
return Data<Pointer>{
reinterpret_cast<Pointer>(map(*this)),
Scope(allocator, allocation, Scope::Access::Write),
};
}
inline Resource::Buffer::Object::operator bool() const {
return VK_NULL_HANDLE != handle;
}
inline Resource::Buffer::operator bool() const {
return object;
}
inline bool operator==(
const Resource::Image::Sampler::Descriptor& _1,
const Resource::Image::Sampler::Descriptor& _2) {
return (_1.filter == _2.filter) &&
(_1.mipmap_mode == _2.mipmap_mode) &&
(_1.address_mode == _2.address_mode) &&
(_1.border == _2.border);
}
inline size_t Resource::Image::Sampler::Factory::Hasher::operator()(
const Descriptor& descriptor) const {
return c10::get_hash(
descriptor.filter,
descriptor.mipmap_mode,
descriptor.address_mode,
descriptor.border);
}
inline Resource::Image::Object::operator bool() const {
return VK_NULL_HANDLE != handle;
}
inline Resource::Image::operator bool() const {
return object;
}
inline Resource::Fence::operator bool() const {
return pool;
}
inline VkFence Resource::Fence::handle(const bool add_to_waitlist) const {
if (!pool) {
return VK_NULL_HANDLE;
}
TORCH_INTERNAL_ASSERT_DEBUG_ONLY(
id < pool->fence_.pool.size(),
"Invalid Vulkan fence!");
const VkFence fence = pool->fence_.pool[id].get();
if (add_to_waitlist) {
pool->fence_.waitlist.push_back(fence);
}
return fence;
}
} // namespace api
} // namespace vulkan
} // namespace native
} // namespace at