forked from KhronosGroup/Vulkan-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.cpp
More file actions
318 lines (259 loc) · 9.72 KB
/
utils.cpp
File metadata and controls
318 lines (259 loc) · 9.72 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
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
/* Copyright (c) 2018-2026, Arm Limited and Contributors
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 the "License";
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "utils.h"
#include <queue>
#include <stdexcept>
#include "core/command_buffer.h"
#include "rendering/render_frame.h"
#include "scene_graph/components/material.h"
#include "scene_graph/components/perspective_camera.h"
#include "scene_graph/components/sub_mesh.h"
#include "scene_graph/node.h"
#include "scene_graph/script.h"
#include "scene_graph/scripts/free_camera.h"
namespace vkb
{
std::string get_extension(const std::string &uri)
{
auto dot_pos = uri.find_last_of('.');
if (dot_pos == std::string::npos)
{
throw std::runtime_error{"Uri has no extension"};
}
return uri.substr(dot_pos + 1);
}
void screenshot(vkb::rendering::RenderContextC &render_context, const std::string &filename)
{
assert(render_context.get_format() == VK_FORMAT_R8G8B8A8_UNORM ||
render_context.get_format() == VK_FORMAT_B8G8R8A8_UNORM ||
render_context.get_format() == VK_FORMAT_R8G8B8A8_SRGB ||
render_context.get_format() == VK_FORMAT_B8G8R8A8_SRGB);
// We want the last completed frame since we don't want to be reading from an incomplete framebuffer
auto &frame = render_context.get_last_rendered_frame();
assert(!frame.get_render_target().get_views().empty());
auto &src_image_view = frame.get_render_target().get_views()[0];
auto width = render_context.get_surface_extent().width;
auto height = render_context.get_surface_extent().height;
auto dst_size = width * height * 4;
vkb::core::BufferC dst_buffer{render_context.get_device(),
dst_size,
VK_BUFFER_USAGE_TRANSFER_DST_BIT,
VMA_MEMORY_USAGE_GPU_TO_CPU,
VMA_ALLOCATION_CREATE_MAPPED_BIT};
const auto &queue = render_context.get_device().get_queue_by_flags(VK_QUEUE_GRAPHICS_BIT, 0);
auto cmd_buf = render_context.get_device().get_command_pool().request_command_buffer();
cmd_buf->begin(VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT);
// Enable destination buffer to be written to
{
BufferMemoryBarrier memory_barrier{};
memory_barrier.src_access_mask = 0;
memory_barrier.dst_access_mask = VK_ACCESS_TRANSFER_WRITE_BIT;
memory_barrier.src_stage_mask = VK_PIPELINE_STAGE_TRANSFER_BIT;
memory_barrier.dst_stage_mask = VK_PIPELINE_STAGE_TRANSFER_BIT;
cmd_buf->buffer_memory_barrier(dst_buffer, 0, dst_size, memory_barrier);
}
// Enable framebuffer image view to be read from
{
ImageMemoryBarrier memory_barrier{};
memory_barrier.old_layout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
memory_barrier.new_layout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL;
memory_barrier.src_stage_mask = VK_PIPELINE_STAGE_TRANSFER_BIT;
memory_barrier.dst_stage_mask = VK_PIPELINE_STAGE_TRANSFER_BIT;
cmd_buf->image_memory_barrier(src_image_view, memory_barrier);
}
// Check if framebuffer images are in a BGR format
auto bgr_formats = {VK_FORMAT_B8G8R8A8_SRGB, VK_FORMAT_B8G8R8A8_UNORM, VK_FORMAT_B8G8R8A8_SNORM};
bool swizzle = std::ranges::find(bgr_formats, src_image_view.get_format()) != bgr_formats.end();
// Copy framebuffer image memory
VkBufferImageCopy image_copy_region{};
image_copy_region.bufferRowLength = width;
image_copy_region.bufferImageHeight = height;
image_copy_region.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
image_copy_region.imageSubresource.layerCount = 1;
image_copy_region.imageExtent.width = width;
image_copy_region.imageExtent.height = height;
image_copy_region.imageExtent.depth = 1;
cmd_buf->copy_image_to_buffer(src_image_view.get_image(), VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, dst_buffer, {image_copy_region});
// Enable destination buffer to map memory
{
BufferMemoryBarrier memory_barrier{};
memory_barrier.src_access_mask = VK_ACCESS_TRANSFER_WRITE_BIT;
memory_barrier.dst_access_mask = VK_ACCESS_HOST_READ_BIT;
memory_barrier.src_stage_mask = VK_PIPELINE_STAGE_TRANSFER_BIT;
memory_barrier.dst_stage_mask = VK_PIPELINE_STAGE_HOST_BIT;
cmd_buf->buffer_memory_barrier(dst_buffer, 0, dst_size, memory_barrier);
}
// Revert back the framebuffer image view from transfer to present
{
ImageMemoryBarrier memory_barrier{};
memory_barrier.old_layout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL;
memory_barrier.new_layout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
memory_barrier.src_stage_mask = VK_PIPELINE_STAGE_TRANSFER_BIT;
memory_barrier.dst_stage_mask = VK_PIPELINE_STAGE_TRANSFER_BIT;
cmd_buf->image_memory_barrier(src_image_view, memory_barrier);
}
cmd_buf->end();
queue.submit(*cmd_buf, frame.get_fence_pool().request_fence());
queue.wait_idle();
auto raw_data = dst_buffer.map();
// Creates a pointer to the address of the first byte of the image data
// Replace the A component with 255 (remove transparency)
// If swapchain format is BGR, swapping the R and B components
uint8_t *data = raw_data;
if (swizzle)
{
for (size_t i = 0; i < height; ++i)
{
// Iterate over each pixel, swapping R and B components and writing the max value for alpha
for (size_t j = 0; j < width; ++j)
{
auto temp = *(data + 2);
*(data + 2) = *(data);
*(data) = temp;
*(data + 3) = 255;
// Get next pixel
data += 4;
}
}
}
else
{
for (size_t i = 0; i < height; ++i)
{
// Iterate over each pixel, writing the max value for alpha
for (size_t j = 0; j < width; ++j)
{
*(data + 3) = 255;
// Get next pixel
data += 4;
}
}
}
vkb::fs::write_image(raw_data,
filename,
width,
height,
4,
width * 4);
dst_buffer.unmap();
} // namespace vkb
std::string to_snake_case(const std::string &text)
{
std::stringstream result;
for (const auto ch : text)
{
if (std::isalpha(ch))
{
if (std::isspace(ch))
{
result << "_";
}
else
{
if (std::isupper(ch))
{
result << "_";
}
result << static_cast<char>(std::tolower(ch));
}
}
else
{
result << ch;
}
}
return result.str();
}
sg::Light &add_light(vkb::scene_graph::SceneC &scene,
sg::LightType type,
const glm::vec3 &position,
const glm::quat &rotation,
const sg::LightProperties &props,
vkb::scene_graph::NodeC *parent_node)
{
auto light_ptr = std::make_unique<sg::Light>("light");
auto node = std::make_unique<vkb::scene_graph::NodeC>(-1, "light node");
if (parent_node)
{
node->set_parent(*parent_node);
}
light_ptr->set_node(*node);
light_ptr->set_light_type(type);
light_ptr->set_properties(props);
auto &t = node->get_transform();
t.set_translation(position);
t.set_rotation(rotation);
// Storing the light component because the unique_ptr will be moved to the scene
auto &light = *light_ptr;
node->set_component(light);
scene.add_child(*node);
scene.add_component(std::move(light_ptr));
scene.add_node(std::move(node));
return light;
}
sg::Light &add_point_light(vkb::scene_graph::SceneC &scene, const glm::vec3 &position, const sg::LightProperties &props, vkb::scene_graph::NodeC *parent_node)
{
return add_light(scene, sg::LightType::Point, position, {}, props, parent_node);
}
sg::Light &add_directional_light(vkb::scene_graph::SceneC &scene, const glm::quat &rotation, const sg::LightProperties &props, vkb::scene_graph::NodeC *parent_node)
{
return add_light(scene, sg::LightType::Directional, {}, rotation, props, parent_node);
}
sg::Light &add_spot_light(vkb::scene_graph::SceneC &scene, const glm::vec3 &position, const glm::quat &rotation, const sg::LightProperties &props, vkb::scene_graph::NodeC *parent_node)
{
return add_light(scene, sg::LightType::Spot, position, rotation, props, parent_node);
}
vkb::scene_graph::NodeC &add_free_camera(vkb::scene_graph::SceneC &scene, const std::string &node_name, VkExtent2D extent)
{
auto camera_node = scene.find_node(node_name);
if (!camera_node)
{
LOGW("Camera node `{}` not found. Looking for `default_camera` node.", node_name.c_str());
camera_node = scene.find_node("default_camera");
}
if (!camera_node)
{
throw std::runtime_error("Camera node with name `" + node_name + "` not found.");
}
if (!camera_node->has_component<sg::Camera>())
{
throw std::runtime_error("No camera component found for `" + node_name + "` node.");
}
auto free_camera_script = std::make_unique<sg::FreeCamera>(*camera_node);
free_camera_script->resize(extent.width, extent.height);
scene.add_component(std::move(free_camera_script), *camera_node);
return *camera_node;
}
size_t calculate_hash(const std::vector<uint8_t> &data)
{
static_assert(sizeof(data[0]) == 1);
constexpr size_t chunk_size = sizeof(size_t) / sizeof(data[0]);
size_t data_hash = 0;
size_t offset = 0;
for (; offset + chunk_size < data.size(); offset += chunk_size)
{
glm::detail::hash_combine(data_hash, *reinterpret_cast<size_t const *>(&data[offset]));
}
if (offset < data.size())
{
size_t it = 0;
std::memcpy(&it, &data[offset], data.size() - offset);
glm::detail::hash_combine(data_hash, it);
}
return data_hash;
}
} // namespace vkb