-
-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathtexture.hpp
More file actions
247 lines (204 loc) · 5.12 KB
/
Copy pathtexture.hpp
File metadata and controls
247 lines (204 loc) · 5.12 KB
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
/**
* @file liblava/resource/texture.hpp
* @brief Vulkan texture
* @authors Lava Block OÜ and contributors
* @copyright Copyright (c) 2018-present, MIT License
*/
#pragma once
#include "liblava/resource/buffer.hpp"
#include "liblava/resource/image.hpp"
namespace lava {
/**
* @brief Texture types
*/
enum class texture_type : index {
none = 0,
tex_2d,
array,
cube_map
};
/**
* @brief Texture file path with format
*/
struct texture_file {
/// List of texture files
using list = std::vector<texture_file>;
/// File path
string path;
/// File format
VkFormat format = VK_FORMAT_UNDEFINED;
};
/**
* @brief Texture
*/
struct texture : entity {
/// Shared pointer to texture
using s_ptr = std::shared_ptr<texture>;
/// Map of textures
using s_map = std::map<id, s_ptr>;
/// List of textures
using s_list = std::vector<s_ptr>;
/**
* @brief Texture mip level
*/
struct mip_level {
/// List of mip levels
using list = std::vector<mip_level>;
/// Mip level extent
uv2 extent{};
/// Mip level size
ui32 size = 0;
};
/**
* @brief Texture layer
*/
struct layer {
/// List of layers
using list = std::vector<layer>;
/// List of mip levels
mip_level::list levels;
};
/**
* @brief Make a new texture
* @return s_ptr Shared pointer to texture
*/
static s_ptr make() {
return std::make_shared<texture>();
}
/**
* @brief Destroy the texture
*/
~texture() {
destroy();
}
/**
* @brief Create a new texture
* @param device Vulkan device
* @param size Texture size
* @param format Texture format
* @param layers List of layers
* @param type Texture type
* @return Create was successful or failed
*/
bool create(device::ptr device,
uv2 size,
VkFormat format,
layer::list const& layers = {},
texture_type type = texture_type::tex_2d);
/**
* @brief Destroy the texture
*/
void destroy();
/**
* @brief Upload data to texture
* @param data Data to upload
* @param data_size Size of data
* @return Upload was successful or failed
*/
bool upload(void const* data,
size_t data_size);
/**
* @brief Stage the texture
* @param cmd_buffer Command buffer
* @return Stage was successful or failed
*/
bool stage(VkCommandBuffer cmd_buffer);
/**
* @brief Destroy the upload buffer
*/
void destroy_upload_buffer();
/**
* @brief Get the descriptor information
* @return VkDescriptorImageInfo const* Descriptor image information
*/
VkDescriptorImageInfo const* get_descriptor_info() const {
return &m_descriptor;
}
/**
* @brief Get the image of the texture
* @return image::s_ptr Shared pointer to image
*/
image::s_ptr get_image() {
return m_img;
}
/**
* @brief Get the size of the texture
* @return uv2 Texture size
*/
uv2 get_size() const {
return m_img ? m_img->get_size() : uv2();
}
/**
* @brief Get the type of the texture
* @return texture_type Texture type
*/
texture_type get_type() const {
return m_type;
}
/**
* @brief Get the format of the texture
* @return VkFormat Texture format
*/
VkFormat get_format() const {
return m_img ? m_img->get_format() : VK_FORMAT_UNDEFINED;
}
private:
/// Texture image
image::s_ptr m_img;
/// Texture type
texture_type m_type = texture_type::none;
/// List of layers
layer::list m_layers;
/// Texture sampler
VkSampler m_sampler = 0;
/// Descriptor image information
VkDescriptorImageInfo m_descriptor = {};
/// Upload buffer
buffer::s_ptr m_upload_buffer;
};
/**
* @brief Texture staging
*/
struct staging {
/// Pointer to staging
using ptr = staging*;
/**
* @brief Add texture for staging
* @param texture Texture to stage
*/
void add(texture::s_ptr texture) {
m_todo.push_back(texture);
}
/**
* @brief Stage textures
* @param cmd_buf Command buffer
* @param frame Frame index
* @return Stage was successful or failed
*/
bool stage(VkCommandBuffer cmd_buf,
index frame);
/**
* @brief Clear staging
*/
void clear() {
m_todo.clear();
m_staged.clear();
}
/**
* @brief Check if staging is busy
* @return Staging is busy or not
*/
bool busy() const {
return !m_todo.empty() || !m_staged.empty();
}
private:
/// List of textures to stage
texture::s_list m_todo;
/// Map of textures by frame index
using frame_stage_map = std::map<index, texture::s_list>;
/// Map of staged textures
frame_stage_map m_staged;
};
/// Texture registry
using texture_registry = id_registry<texture, texture_file>;
} // namespace lava