forked from KhronosGroup/Vulkan-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubpasses.cpp
More file actions
446 lines (354 loc) · 14.6 KB
/
subpasses.cpp
File metadata and controls
446 lines (354 loc) · 14.6 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
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/* Copyright (c) 2019-2020, 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 "subpasses.h"
#include "common/vk_common.h"
#include "platform/platform.h"
#include "rendering/pipeline_state.h"
#include "rendering/render_context.h"
#include "rendering/render_pipeline.h"
#include "rendering/subpasses/geometry_subpass.h"
#include "rendering/subpasses/lighting_subpass.h"
#include "scene_graph/node.h"
Subpasses::Subpasses()
{
auto &config = get_configuration();
// Good settings
config.insert<vkb::IntSetting>(0, configs[Config::RenderTechnique].value, 0);
config.insert<vkb::IntSetting>(0, configs[Config::TransientAttachments].value, 0);
config.insert<vkb::IntSetting>(0, configs[Config::GBufferSize].value, 0);
// Use two render passes
config.insert<vkb::IntSetting>(1, configs[Config::RenderTechnique].value, 1);
config.insert<vkb::IntSetting>(1, configs[Config::TransientAttachments].value, 0);
config.insert<vkb::IntSetting>(1, configs[Config::GBufferSize].value, 0);
// Disable transient attachments
config.insert<vkb::IntSetting>(2, configs[Config::RenderTechnique].value, 0);
config.insert<vkb::IntSetting>(2, configs[Config::TransientAttachments].value, 1);
config.insert<vkb::IntSetting>(2, configs[Config::GBufferSize].value, 0);
// Increase G-buffer size
config.insert<vkb::IntSetting>(3, configs[Config::RenderTechnique].value, 0);
config.insert<vkb::IntSetting>(3, configs[Config::TransientAttachments].value, 0);
config.insert<vkb::IntSetting>(3, configs[Config::GBufferSize].value, 1);
}
std::unique_ptr<vkb::RenderTarget> Subpasses::create_render_target(vkb::core::Image &&swapchain_image)
{
auto &device = swapchain_image.get_device();
auto &extent = swapchain_image.get_extent();
// G-Buffer should fit 128-bit budget for buffer color storage
// in order to enable subpasses merging by the driver
// Light (swapchain_image) RGBA8_UNORM (32-bit)
// Albedo RGBA8_UNORM (32-bit)
// Normal RGB10A2_UNORM (32-bit)
vkb::core::Image depth_image{device,
extent,
vkb::get_suitable_depth_format(swapchain_image.get_device().get_gpu().get_handle()),
VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | rt_usage_flags,
VMA_MEMORY_USAGE_GPU_ONLY};
vkb::core::Image albedo_image{device,
extent,
albedo_format,
VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | rt_usage_flags,
VMA_MEMORY_USAGE_GPU_ONLY};
vkb::core::Image normal_image{device,
extent,
normal_format,
VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | rt_usage_flags,
VMA_MEMORY_USAGE_GPU_ONLY};
std::vector<vkb::core::Image> images;
// Attachment 0
images.push_back(std::move(swapchain_image));
// Attachment 1
images.push_back(std::move(depth_image));
// Attachment 2
images.push_back(std::move(albedo_image));
// Attachment 3
images.push_back(std::move(normal_image));
return std::make_unique<vkb::RenderTarget>(std::move(images));
}
void Subpasses::prepare_render_context()
{
get_render_context().prepare(1, [this](vkb::core::Image &&swapchain_image) { return create_render_target(std::move(swapchain_image)); });
}
bool Subpasses::prepare(vkb::Platform &platform)
{
if (!VulkanSample::prepare(platform))
{
return false;
}
std::set<VkImageUsageFlagBits> usage = {VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT};
get_render_context().update_swapchain(usage);
load_scene("scenes/sponza/Sponza01.gltf");
scene->clear_components<vkb::sg::Light>();
auto light_pos = glm::vec3(0.0f, 128.0f, -225.0f);
auto light_color = glm::vec3(1.0, 1.0, 1.0);
// Magic numbers used to offset lights in the Sponza scene
for (int i = -4; i < 4; ++i)
{
for (int j = 0; j < 2; ++j)
{
glm::vec3 pos = light_pos;
pos.x += i * 400;
pos.z += j * (225 + 140);
pos.y = 8;
for (int k = 0; k < 3; ++k)
{
pos.y = pos.y + (k * 100);
light_color.x = static_cast<float>(rand()) / (RAND_MAX);
light_color.y = static_cast<float>(rand()) / (RAND_MAX);
light_color.z = static_cast<float>(rand()) / (RAND_MAX);
vkb::sg::LightProperties props;
props.color = light_color;
props.intensity = 0.2f;
vkb::add_point_light(*scene, pos, props);
}
}
}
auto &camera_node = vkb::add_free_camera(*scene, "main_camera", get_render_context().get_surface_extent());
camera = dynamic_cast<vkb::sg::PerspectiveCamera *>(&camera_node.get_component<vkb::sg::Camera>());
render_pipeline = create_one_renderpass_two_subpasses();
geometry_render_pipeline = create_geometry_renderpass();
lighting_render_pipeline = create_lighting_renderpass();
// Enable stats
stats->request_stats({vkb::StatIndex::frame_times,
vkb::StatIndex::gpu_fragment_jobs,
vkb::StatIndex::gpu_tiles,
vkb::StatIndex::gpu_ext_read_bytes,
vkb::StatIndex::gpu_ext_write_bytes});
// Enable gui
gui = std::make_unique<vkb::Gui>(*this, platform.get_window(), stats.get());
return true;
}
void Subpasses::update(float delta_time)
{
// Check whether the user changed the render technique
if (configs[Config::RenderTechnique].value != last_render_technique)
{
LOGI("Changing render technique");
last_render_technique = configs[Config::RenderTechnique].value;
// Reset frames, their synchronization objects and their command buffers
for (auto &frame : get_render_context().get_render_frames())
{
frame->reset();
}
}
// Check whether the user switched the attachment or the G-buffer option
if (configs[Config::TransientAttachments].value != last_transient_attachment ||
configs[Config::GBufferSize].value != last_g_buffer_size)
{
// If attachment option has changed
if (configs[Config::TransientAttachments].value != last_transient_attachment)
{
rt_usage_flags = VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
// If attachment should be transient
if (configs[Config::TransientAttachments].value == 0)
{
rt_usage_flags |= VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT;
}
else
{
LOGI("Creating non transient attachments");
}
last_transient_attachment = configs[Config::TransientAttachments].value;
}
// It G-buffer option has changed
if (configs[Config::GBufferSize].value != last_g_buffer_size)
{
if (configs[Config::GBufferSize].value == 0)
{
// Use less bits
albedo_format = VK_FORMAT_R8G8B8A8_UNORM; // 32-bit
normal_format = VK_FORMAT_A2B10G10R10_UNORM_PACK32; // 32-bit
}
else
{
// Use more bits
albedo_format = VK_FORMAT_R32G32B32A32_SFLOAT; // 128-bit
normal_format = VK_FORMAT_R32G32B32A32_SFLOAT; // 128-bit
}
last_g_buffer_size = configs[Config::GBufferSize].value;
}
// Reset frames, their synchronization objects and their command buffers
for (auto &frame : get_render_context().get_render_frames())
{
frame->reset();
}
LOGI("Recreating render target");
render_context->recreate();
}
VulkanSample::update(delta_time);
}
void Subpasses::draw_gui()
{
auto lines = configs.size();
if (camera->get_aspect_ratio() < 1.0f)
{
// In portrait, show buttons below heading
lines = lines * 2;
}
gui->show_options_window(
/* body = */ [this, lines]() {
// Create a line for every config
for (size_t i = 0; i < configs.size(); ++i)
{
// Avoid conflicts between buttons with identical labels
ImGui::PushID(vkb::to_u32(i));
auto &config = configs[i];
ImGui::Text("%s: ", config.description);
if (camera->get_aspect_ratio() > 1.0f)
{
// In landscape, show all options following the heading
ImGui::SameLine();
}
// Create a radio button for every option
for (size_t j = 0; j < config.options.size(); ++j)
{
ImGui::RadioButton(config.options[j], &config.value, vkb::to_u32(j));
// Keep it on the same line til the last one
if (j < config.options.size() - 1)
{
ImGui::SameLine();
}
}
ImGui::PopID();
}
},
/* lines = */ vkb::to_u32(lines));
}
std::unique_ptr<vkb::RenderPipeline> Subpasses::create_one_renderpass_two_subpasses()
{
// Geometry subpass
auto geometry_vs = vkb::ShaderSource{"deferred/geometry.vert"};
auto geometry_fs = vkb::ShaderSource{"deferred/geometry.frag"};
auto scene_subpass = std::make_unique<vkb::GeometrySubpass>(get_render_context(), std::move(geometry_vs), std::move(geometry_fs), *scene, *camera);
// Outputs are depth, albedo, and normal
scene_subpass->set_output_attachments({1, 2, 3});
// Lighting subpass
auto lighting_vs = vkb::ShaderSource{"deferred/lighting.vert"};
auto lighting_fs = vkb::ShaderSource{"deferred/lighting.frag"};
auto lighting_subpass = std::make_unique<vkb::LightingSubpass>(get_render_context(), std::move(lighting_vs), std::move(lighting_fs), *camera, *scene);
// Inputs are depth, albedo, and normal from the geometry subpass
lighting_subpass->set_input_attachments({1, 2, 3});
// Create subpasses pipeline
std::vector<std::unique_ptr<vkb::Subpass>> subpasses{};
subpasses.push_back(std::move(scene_subpass));
subpasses.push_back(std::move(lighting_subpass));
auto render_pipeline = std::make_unique<vkb::RenderPipeline>(std::move(subpasses));
render_pipeline->set_load_store(vkb::gbuffer::get_clear_all_store_swapchain());
render_pipeline->set_clear_value(vkb::gbuffer::get_clear_value());
return render_pipeline;
}
std::unique_ptr<vkb::RenderPipeline> Subpasses::create_geometry_renderpass()
{
// Geometry subpass
auto geometry_vs = vkb::ShaderSource{"deferred/geometry.vert"};
auto geometry_fs = vkb::ShaderSource{"deferred/geometry.frag"};
auto scene_subpass = std::make_unique<vkb::GeometrySubpass>(get_render_context(), std::move(geometry_vs), std::move(geometry_fs), *scene, *camera);
// Outputs are depth, albedo, and normal
scene_subpass->set_output_attachments({1, 2, 3});
// Create geomtry pipeline
std::vector<std::unique_ptr<vkb::Subpass>> scene_subpasses{};
scene_subpasses.push_back(std::move(scene_subpass));
auto geometry_render_pipeline = std::make_unique<vkb::RenderPipeline>(std::move(scene_subpasses));
geometry_render_pipeline->set_load_store(vkb::gbuffer::get_clear_store_all());
geometry_render_pipeline->set_clear_value(vkb::gbuffer::get_clear_value());
return geometry_render_pipeline;
}
std::unique_ptr<vkb::RenderPipeline> Subpasses::create_lighting_renderpass()
{
// Lighting subpass
auto lighting_vs = vkb::ShaderSource{"deferred/lighting.vert"};
auto lighting_fs = vkb::ShaderSource{"deferred/lighting.frag"};
auto lighting_subpass = std::make_unique<vkb::LightingSubpass>(get_render_context(), std::move(lighting_vs), std::move(lighting_fs), *camera, *scene);
// Inputs are depth, albedo, and normal from the geometry subpass
lighting_subpass->set_input_attachments({1, 2, 3});
// Create lighting pipeline
std::vector<std::unique_ptr<vkb::Subpass>> lighting_subpasses{};
lighting_subpasses.push_back(std::move(lighting_subpass));
auto lighting_render_pipeline = std::make_unique<vkb::RenderPipeline>(std::move(lighting_subpasses));
lighting_render_pipeline->set_load_store(vkb::gbuffer::get_load_all_store_swapchain());
lighting_render_pipeline->set_clear_value(vkb::gbuffer::get_clear_value());
return lighting_render_pipeline;
}
void draw_pipeline(vkb::CommandBuffer &command_buffer, vkb::RenderTarget &render_target, vkb::RenderPipeline &render_pipeline, vkb::Gui *gui = nullptr)
{
auto &extent = render_target.get_extent();
VkViewport viewport{};
viewport.width = static_cast<float>(extent.width);
viewport.height = static_cast<float>(extent.height);
viewport.minDepth = 0.0f;
viewport.maxDepth = 1.0f;
command_buffer.set_viewport(0, {viewport});
VkRect2D scissor{};
scissor.extent = extent;
command_buffer.set_scissor(0, {scissor});
render_pipeline.draw(command_buffer, render_target);
if (gui)
{
gui->draw(command_buffer);
}
command_buffer.end_render_pass();
}
void Subpasses::draw_subpasses(vkb::CommandBuffer &command_buffer, vkb::RenderTarget &render_target)
{
draw_pipeline(command_buffer, render_target, *render_pipeline, gui.get());
}
void Subpasses::draw_renderpasses(vkb::CommandBuffer &command_buffer, vkb::RenderTarget &render_target)
{
// First render pass (no gui)
draw_pipeline(command_buffer, render_target, *geometry_render_pipeline);
// Memory barriers needed
for (size_t i = 1; i < render_target.get_views().size(); ++i)
{
auto &view = render_target.get_views().at(i);
vkb::ImageMemoryBarrier barrier;
if (i == 1)
{
barrier.old_layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
barrier.new_layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL;
barrier.src_stage_mask = VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT;
barrier.src_access_mask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
}
else
{
barrier.old_layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
barrier.new_layout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
barrier.src_stage_mask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
barrier.src_access_mask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
}
barrier.dst_stage_mask = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;
barrier.dst_access_mask = VK_ACCESS_INPUT_ATTACHMENT_READ_BIT;
command_buffer.image_memory_barrier(view, barrier);
}
// Second render pass
draw_pipeline(command_buffer, render_target, *lighting_render_pipeline, gui.get());
}
void Subpasses::draw_renderpass(vkb::CommandBuffer &command_buffer, vkb::RenderTarget &render_target)
{
if (configs[Config::RenderTechnique].value == 0)
{
// Efficient way
draw_subpasses(command_buffer, render_target);
}
else
{
// Inefficient way
draw_renderpasses(command_buffer, render_target);
}
}
std::unique_ptr<vkb::VulkanSample> create_subpasses()
{
return std::make_unique<Subpasses>();
}