-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresource.h
174 lines (140 loc) · 5.54 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
// SPDX-License-Identifier: MIT
// SPDX-FileCopyrightText: Copyright (c) 2021-2024 Chris Dragan
#pragma once
#include "usage.h"
#include "vulkan_functions.h"
#include <assert.h>
class MemoryHeap;
class Resource {
public:
constexpr Resource() = default;
Resource(const Resource&) = delete;
Resource& operator=(const Resource&) = delete;
bool allocated() const { return !! alloc_size; }
VkDeviceSize size() const { return alloc_size; }
template<typename T>
T* get_ptr() {
assert(sizeof(T) <= alloc_size);
return static_cast<T*>(get_raw_ptr());
}
template<typename T>
const T* get_ptr() const {
assert(sizeof(T) <= alloc_size);
return static_cast<const T*>(get_raw_ptr());
}
template<typename T>
T* get_ptr(VkDeviceSize offset) {
assert(offset + sizeof(T) <= alloc_size);
return static_cast<T*>(get_raw_ptr(offset));
}
template<typename T>
const T* get_ptr(VkDeviceSize offset) const {
assert(offset + sizeof(T) <= alloc_size);
return static_cast<const T*>(get_raw_ptr(offset));
}
template<typename T>
T* get_ptr(VkDeviceSize idx, VkDeviceSize stride) {
assert(sizeof(T) <= stride);
return static_cast<T*>(get_raw_ptr(idx, stride));
}
template<typename T>
const T* get_ptr(VkDeviceSize idx, VkDeviceSize stride) const {
assert(sizeof(T) <= stride);
return static_cast<const T*>(get_raw_ptr(idx, stride));
}
protected:
void* get_raw_ptr() const;
void* get_raw_ptr(VkDeviceSize idx, VkDeviceSize stride) const;
void* get_raw_ptr(VkDeviceSize offset) const;
bool flush_range(VkDeviceSize offset, VkDeviceSize size);
bool flush_whole();
MemoryHeap* owning_heap = nullptr;
VkDeviceSize heap_offset = 0;
VkDeviceSize alloc_size = 0;
};
struct ImageInfo {
uint32_t width;
uint32_t height;
VkFormat format;
uint32_t mip_levels;
VkImageAspectFlags aspect;
VkImageUsageFlags usage;
Usage heap_usage;
};
class Image: public Resource {
public:
VkImageLayout layout = VK_IMAGE_LAYOUT_UNDEFINED;
constexpr Image() = default;
Image(const Image&) = delete;
Image& operator=(const Image&) = delete;
const VkImage& get_image() const { return image; }
VkImageView get_view() const { return view; }
uint32_t get_pitch() const { return pitch; }
bool allocate(const ImageInfo& image_info, Description desc);
bool flush() { return flush_whole(); }
void destroy();
void free(); // GUI only
struct Transition {
VkPipelineStageFlags src_stage;
VkAccessFlags src_access;
VkPipelineStageFlags dest_stage;
VkAccessFlags dest_access;
VkImageLayout new_layout;
};
void set_image_layout(VkCommandBuffer buf, const Transition& transition);
// Used with swapchains
void set_image(VkImage new_image) {
assert(image == VK_NULL_HANDLE);
image = new_image;
aspect = VK_IMAGE_ASPECT_COLOR_BIT;
}
void set_view(VkImageView new_view) {
assert(view == VK_NULL_HANDLE);
view = new_view;
}
private:
VkImage image = VK_NULL_HANDLE;
VkImageView view = VK_NULL_HANDLE;
VkFormat format = VK_FORMAT_UNDEFINED;
VkImageAspectFlags aspect = VK_IMAGE_ASPECT_COLOR_BIT;
Usage heap_usage = Usage::fixed;
uint32_t mip_levels = 0;
uint32_t pitch = 0;
};
class Buffer: public Resource {
public:
constexpr Buffer() = default;
Buffer(const Buffer&) = delete;
Buffer& operator=(const Buffer&) = delete;
const VkBuffer& get_buffer() const { return buffer; }
VkBufferView get_view() const { return view; }
bool allocate(Usage heap_usage,
uint32_t size,
VkFormat format,
VkBufferUsageFlags usage,
Description desc);
void cpu_fill(const void* data, uint32_t size);
bool flush() { return flush_whole(); }
bool flush(VkDeviceSize idx, VkDeviceSize stride);
void free(); // GUI only
private:
VkBuffer buffer = VK_NULL_HANDLE;
VkBufferView view = VK_NULL_HANDLE;
};
struct ImageWithHostCopy: public Image {
public:
constexpr ImageWithHostCopy() = default;
ImageWithHostCopy(const ImageWithHostCopy&) = delete;
ImageWithHostCopy& operator=(const ImageWithHostCopy&) = delete;
bool allocate(const ImageInfo& image_info, Description desc);
const Image& get_host_image() const { return host_image; }
Image& get_host_image() { dirty = true; return host_image; }
bool send_to_gpu(VkCommandBuffer cmdbuf);
uint32_t get_width() const { return width; }
uint32_t get_height() const { return height; }
private:
Image host_image;
uint32_t width = 0;
uint32_t height = 0;
bool dirty = false;
};