forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathswapchain_impl_vk.cc
More file actions
513 lines (449 loc) · 18.1 KB
/
swapchain_impl_vk.cc
File metadata and controls
513 lines (449 loc) · 18.1 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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "impeller/renderer/backend/vulkan/swapchain_impl_vk.h"
#include "fml/synchronization/semaphore.h"
#include "impeller/base/validation.h"
#include "impeller/core/formats.h"
#include "impeller/renderer/backend/vulkan/command_buffer_vk.h"
#include "impeller/renderer/backend/vulkan/command_encoder_vk.h"
#include "impeller/renderer/backend/vulkan/context_vk.h"
#include "impeller/renderer/backend/vulkan/formats_vk.h"
#include "impeller/renderer/backend/vulkan/gpu_tracer_vk.h"
#include "impeller/renderer/backend/vulkan/surface_vk.h"
#include "impeller/renderer/backend/vulkan/swapchain_image_vk.h"
#include "impeller/renderer/context.h"
#include "vulkan/vulkan_structs.hpp"
namespace impeller {
static constexpr size_t kMaxFramesInFlight = 3u;
// Number of frames to poll for orientation changes. For example `1u` means
// that the orientation will be polled every frame, while `2u` means that the
// orientation will be polled every other frame.
static constexpr size_t kPollFramesForOrientation = 1u;
struct FrameSynchronizer {
vk::UniqueFence acquire;
vk::UniqueSemaphore render_ready;
vk::UniqueSemaphore present_ready;
std::shared_ptr<CommandBuffer> final_cmd_buffer;
bool is_valid = false;
explicit FrameSynchronizer(const vk::Device& device) {
auto acquire_res = device.createFenceUnique(
vk::FenceCreateInfo{vk::FenceCreateFlagBits::eSignaled});
auto render_res = device.createSemaphoreUnique({});
auto present_res = device.createSemaphoreUnique({});
if (acquire_res.result != vk::Result::eSuccess ||
render_res.result != vk::Result::eSuccess ||
present_res.result != vk::Result::eSuccess) {
VALIDATION_LOG << "Could not create synchronizer.";
return;
}
acquire = std::move(acquire_res.value);
render_ready = std::move(render_res.value);
present_ready = std::move(present_res.value);
is_valid = true;
}
~FrameSynchronizer() = default;
bool WaitForFence(const vk::Device& device) {
if (auto result = device.waitForFences(
*acquire, // fence
true, // wait all
std::numeric_limits<uint64_t>::max() // timeout (ns)
);
result != vk::Result::eSuccess) {
VALIDATION_LOG << "Fence wait failed: " << vk::to_string(result);
return false;
}
if (auto result = device.resetFences(*acquire);
result != vk::Result::eSuccess) {
VALIDATION_LOG << "Could not reset fence: " << vk::to_string(result);
return false;
}
return true;
}
};
static bool ContainsFormat(const std::vector<vk::SurfaceFormatKHR>& formats,
vk::SurfaceFormatKHR format) {
return std::find(formats.begin(), formats.end(), format) != formats.end();
}
static std::optional<vk::SurfaceFormatKHR> ChooseSurfaceFormat(
const std::vector<vk::SurfaceFormatKHR>& formats,
PixelFormat preference) {
const auto colorspace = vk::ColorSpaceKHR::eSrgbNonlinear;
const auto vk_preference =
vk::SurfaceFormatKHR{ToVKImageFormat(preference), colorspace};
if (ContainsFormat(formats, vk_preference)) {
return vk_preference;
}
std::vector<vk::SurfaceFormatKHR> options = {
{vk::Format::eB8G8R8A8Unorm, colorspace},
{vk::Format::eR8G8B8A8Unorm, colorspace}};
for (const auto& format : options) {
if (ContainsFormat(formats, format)) {
return format;
}
}
return std::nullopt;
}
static std::optional<vk::CompositeAlphaFlagBitsKHR> ChooseAlphaCompositionMode(
vk::CompositeAlphaFlagsKHR flags) {
if (flags & vk::CompositeAlphaFlagBitsKHR::eInherit) {
return vk::CompositeAlphaFlagBitsKHR::eInherit;
}
if (flags & vk::CompositeAlphaFlagBitsKHR::ePreMultiplied) {
return vk::CompositeAlphaFlagBitsKHR::ePreMultiplied;
}
if (flags & vk::CompositeAlphaFlagBitsKHR::ePostMultiplied) {
return vk::CompositeAlphaFlagBitsKHR::ePostMultiplied;
}
if (flags & vk::CompositeAlphaFlagBitsKHR::eOpaque) {
return vk::CompositeAlphaFlagBitsKHR::eOpaque;
}
return std::nullopt;
}
std::shared_ptr<SwapchainImplVK> SwapchainImplVK::Create(
const std::shared_ptr<Context>& context,
vk::UniqueSurfaceKHR surface,
const ISize& size,
bool enable_msaa,
vk::SwapchainKHR old_swapchain) {
return std::shared_ptr<SwapchainImplVK>(new SwapchainImplVK(
context, std::move(surface), size, enable_msaa, old_swapchain));
}
SwapchainImplVK::SwapchainImplVK(const std::shared_ptr<Context>& context,
vk::UniqueSurfaceKHR surface,
const ISize& size,
bool enable_msaa,
vk::SwapchainKHR old_swapchain) {
if (!context) {
VALIDATION_LOG << "Cannot create a swapchain without a context.";
return;
}
auto& vk_context = ContextVK::Cast(*context);
const auto [caps_result, surface_caps] =
vk_context.GetPhysicalDevice().getSurfaceCapabilitiesKHR(*surface);
if (caps_result != vk::Result::eSuccess) {
VALIDATION_LOG << "Could not get surface capabilities: "
<< vk::to_string(caps_result);
return;
}
auto [formats_result, formats] =
vk_context.GetPhysicalDevice().getSurfaceFormatsKHR(*surface);
if (formats_result != vk::Result::eSuccess) {
VALIDATION_LOG << "Could not get surface formats: "
<< vk::to_string(formats_result);
return;
}
const auto format = ChooseSurfaceFormat(
formats, vk_context.GetCapabilities()->GetDefaultColorFormat());
if (!format.has_value()) {
VALIDATION_LOG << "Swapchain has no supported formats.";
return;
}
vk_context.SetOffscreenFormat(ToPixelFormat(format.value().format));
const auto composite =
ChooseAlphaCompositionMode(surface_caps.supportedCompositeAlpha);
if (!composite.has_value()) {
VALIDATION_LOG << "No composition mode supported.";
return;
}
vk::SwapchainCreateInfoKHR swapchain_info;
swapchain_info.surface = *surface;
swapchain_info.imageFormat = format.value().format;
swapchain_info.imageColorSpace = format.value().colorSpace;
swapchain_info.presentMode = vk::PresentModeKHR::eFifo;
swapchain_info.imageExtent = vk::Extent2D{
std::clamp(static_cast<uint32_t>(size.width),
surface_caps.minImageExtent.width,
surface_caps.maxImageExtent.width),
std::clamp(static_cast<uint32_t>(size.height),
surface_caps.minImageExtent.height,
surface_caps.maxImageExtent.height),
};
swapchain_info.minImageCount =
std::clamp(surface_caps.minImageCount + 1u, // preferred image count
surface_caps.minImageCount, // min count cannot be zero
surface_caps.maxImageCount == 0u
? surface_caps.minImageCount + 1u
: surface_caps.maxImageCount // max zero means no limit
);
swapchain_info.imageArrayLayers = 1u;
// Swapchain images are primarily used as color attachments (via resolve),
// blit targets, or input attachments.
swapchain_info.imageUsage = vk::ImageUsageFlagBits::eColorAttachment |
vk::ImageUsageFlagBits::eTransferDst |
vk::ImageUsageFlagBits::eInputAttachment;
swapchain_info.preTransform = vk::SurfaceTransformFlagBitsKHR::eIdentity;
swapchain_info.compositeAlpha = composite.value();
// If we set the clipped value to true, Vulkan expects we will never read back
// from the buffer. This is analogous to [CAMetalLayer framebufferOnly] in
// Metal.
swapchain_info.clipped = true;
// Setting queue family indices is irrelevant since the present mode is
// exclusive.
swapchain_info.imageSharingMode = vk::SharingMode::eExclusive;
swapchain_info.oldSwapchain = old_swapchain;
auto [swapchain_result, swapchain] =
vk_context.GetDevice().createSwapchainKHRUnique(swapchain_info);
if (swapchain_result != vk::Result::eSuccess) {
VALIDATION_LOG << "Could not create swapchain: "
<< vk::to_string(swapchain_result);
return;
}
auto [images_result, images] =
vk_context.GetDevice().getSwapchainImagesKHR(*swapchain);
if (images_result != vk::Result::eSuccess) {
VALIDATION_LOG << "Could not get swapchain images.";
return;
}
TextureDescriptor texture_desc;
texture_desc.usage =
static_cast<decltype(texture_desc.usage)>(TextureUsage::kRenderTarget);
texture_desc.storage_mode = StorageMode::kDevicePrivate;
texture_desc.format = ToPixelFormat(swapchain_info.imageFormat);
texture_desc.size = ISize::MakeWH(swapchain_info.imageExtent.width,
swapchain_info.imageExtent.height);
// Allocate a single onscreen MSAA texture and Depth+Stencil Texture to
// be shared by all swapchain images.
TextureDescriptor msaa_desc;
msaa_desc.storage_mode = StorageMode::kDeviceTransient;
msaa_desc.type = TextureType::kTexture2DMultisample;
msaa_desc.sample_count = SampleCount::kCount4;
msaa_desc.format = texture_desc.format;
msaa_desc.size = texture_desc.size;
msaa_desc.usage = static_cast<uint64_t>(TextureUsage::kRenderTarget);
TextureDescriptor depth_stencil_desc;
depth_stencil_desc.storage_mode = StorageMode::kDeviceTransient;
if (enable_msaa) {
depth_stencil_desc.type = TextureType::kTexture2DMultisample;
depth_stencil_desc.sample_count = SampleCount::kCount4;
} else {
depth_stencil_desc.type = TextureType::kTexture2D;
depth_stencil_desc.sample_count = SampleCount::kCount1;
}
depth_stencil_desc.format =
context->GetCapabilities()->GetDefaultDepthStencilFormat();
depth_stencil_desc.size = texture_desc.size;
depth_stencil_desc.usage = static_cast<uint64_t>(TextureUsage::kRenderTarget);
std::shared_ptr<Texture> msaa_texture;
if (enable_msaa) {
msaa_texture = context->GetResourceAllocator()->CreateTexture(msaa_desc);
}
std::shared_ptr<Texture> depth_stencil_texture =
context->GetResourceAllocator()->CreateTexture(depth_stencil_desc);
std::vector<std::shared_ptr<SwapchainImageVK>> swapchain_images;
for (const auto& image : images) {
auto swapchain_image =
std::make_shared<SwapchainImageVK>(texture_desc, // texture descriptor
vk_context.GetDevice(), // device
image // image
);
if (!swapchain_image->IsValid()) {
VALIDATION_LOG << "Could not create swapchain image.";
return;
}
swapchain_image->SetMSAATexture(msaa_texture);
swapchain_image->SetDepthStencilTexture(depth_stencil_texture);
ContextVK::SetDebugName(
vk_context.GetDevice(), swapchain_image->GetImage(),
"SwapchainImage" + std::to_string(swapchain_images.size()));
ContextVK::SetDebugName(
vk_context.GetDevice(), swapchain_image->GetImageView(),
"SwapchainImageView" + std::to_string(swapchain_images.size()));
swapchain_images.emplace_back(swapchain_image);
}
std::vector<std::unique_ptr<FrameSynchronizer>> synchronizers;
for (size_t i = 0u; i < kMaxFramesInFlight; i++) {
auto sync = std::make_unique<FrameSynchronizer>(vk_context.GetDevice());
if (!sync->is_valid) {
VALIDATION_LOG << "Could not create frame synchronizers.";
return;
}
synchronizers.emplace_back(std::move(sync));
}
FML_DCHECK(!synchronizers.empty());
context_ = context;
surface_ = std::move(surface);
surface_format_ = swapchain_info.imageFormat;
swapchain_ = std::move(swapchain);
images_ = std::move(swapchain_images);
synchronizers_ = std::move(synchronizers);
current_frame_ = synchronizers_.size() - 1u;
size_ = size;
enable_msaa_ = enable_msaa;
is_valid_ = true;
}
SwapchainImplVK::~SwapchainImplVK() {
DestroySwapchain();
}
const ISize& SwapchainImplVK::GetSize() const {
return size_;
}
bool SwapchainImplVK::IsValid() const {
return is_valid_;
}
void SwapchainImplVK::WaitIdle() const {
if (auto context = context_.lock()) {
[[maybe_unused]] auto result =
ContextVK::Cast(*context).GetDevice().waitIdle();
}
}
std::pair<vk::UniqueSurfaceKHR, vk::UniqueSwapchainKHR>
SwapchainImplVK::DestroySwapchain() {
WaitIdle();
is_valid_ = false;
synchronizers_.clear();
images_.clear();
context_.reset();
return {std::move(surface_), std::move(swapchain_)};
}
vk::Format SwapchainImplVK::GetSurfaceFormat() const {
return surface_format_;
}
std::shared_ptr<Context> SwapchainImplVK::GetContext() const {
return context_.lock();
}
SwapchainImplVK::AcquireResult SwapchainImplVK::AcquireNextDrawable() {
auto context_strong = context_.lock();
if (!context_strong) {
return SwapchainImplVK::AcquireResult{};
}
const auto& context = ContextVK::Cast(*context_strong);
current_frame_ = (current_frame_ + 1u) % synchronizers_.size();
const auto& sync = synchronizers_[current_frame_];
//----------------------------------------------------------------------------
/// Wait on the host for the synchronizer fence.
///
if (!sync->WaitForFence(context.GetDevice())) {
VALIDATION_LOG << "Could not wait for fence.";
return SwapchainImplVK::AcquireResult{};
}
//----------------------------------------------------------------------------
/// Get the next image index.
///
auto [acq_result, index] = context.GetDevice().acquireNextImageKHR(
*swapchain_, // swapchain
1'000'000'000, // timeout (ns) 1000ms
*sync->render_ready, // signal semaphore
nullptr // fence
);
switch (acq_result) {
case vk::Result::eSuccess:
// Keep going.
break;
case vk::Result::eSuboptimalKHR:
case vk::Result::eErrorOutOfDateKHR:
// A recoverable error. Just say we are out of date.
return AcquireResult{true /* out of date */};
break;
default:
// An unrecoverable error.
VALIDATION_LOG << "Could not acquire next swapchain image: "
<< vk::to_string(acq_result);
return AcquireResult{false /* out of date */};
}
if (index >= images_.size()) {
VALIDATION_LOG << "Swapchain returned an invalid image index.";
return SwapchainImplVK::AcquireResult{};
}
/// Record all subsequent cmd buffers as part of the current frame.
context.GetGPUTracer()->MarkFrameStart();
auto image = images_[index % images_.size()];
uint32_t image_index = index;
return AcquireResult{SurfaceVK::WrapSwapchainImage(
context_strong, // context
image, // swapchain image
[weak_swapchain = weak_from_this(), image, image_index]() -> bool {
auto swapchain = weak_swapchain.lock();
if (!swapchain) {
return false;
}
return swapchain->Present(image, image_index);
}, // swap callback
enable_msaa_ //
)};
}
bool SwapchainImplVK::Present(const std::shared_ptr<SwapchainImageVK>& image,
uint32_t index) {
auto context_strong = context_.lock();
if (!context_strong) {
return false;
}
const auto& context = ContextVK::Cast(*context_strong);
const auto& sync = synchronizers_[current_frame_];
context.GetGPUTracer()->MarkFrameEnd();
//----------------------------------------------------------------------------
/// Transition the image to color-attachment-optimal.
///
sync->final_cmd_buffer = context.CreateCommandBuffer();
if (!sync->final_cmd_buffer) {
return false;
}
auto vk_final_cmd_buffer = CommandBufferVK::Cast(*sync->final_cmd_buffer)
.GetEncoder()
->GetCommandBuffer();
{
BarrierVK barrier;
barrier.new_layout = vk::ImageLayout::ePresentSrcKHR;
barrier.cmd_buffer = vk_final_cmd_buffer;
barrier.src_access = vk::AccessFlagBits::eColorAttachmentWrite;
barrier.src_stage = vk::PipelineStageFlagBits::eColorAttachmentOutput;
barrier.dst_access = {};
barrier.dst_stage = vk::PipelineStageFlagBits::eBottomOfPipe;
if (!image->SetLayout(barrier).ok()) {
return false;
}
if (vk_final_cmd_buffer.end() != vk::Result::eSuccess) {
return false;
}
}
//----------------------------------------------------------------------------
/// Signal that the presentation semaphore is ready.
///
{
vk::SubmitInfo submit_info;
vk::PipelineStageFlags wait_stage =
vk::PipelineStageFlagBits::eColorAttachmentOutput;
submit_info.setWaitDstStageMask(wait_stage);
submit_info.setWaitSemaphores(*sync->render_ready);
submit_info.setSignalSemaphores(*sync->present_ready);
submit_info.setCommandBuffers(vk_final_cmd_buffer);
auto result =
context.GetGraphicsQueue()->Submit(submit_info, *sync->acquire);
if (result != vk::Result::eSuccess) {
VALIDATION_LOG << "Could not wait on render semaphore: "
<< vk::to_string(result);
return false;
}
}
//----------------------------------------------------------------------------
/// Present the image.
///
uint32_t indices[] = {static_cast<uint32_t>(index)};
vk::PresentInfoKHR present_info;
present_info.setSwapchains(*swapchain_);
present_info.setImageIndices(indices);
present_info.setWaitSemaphores(*sync->present_ready);
auto result = context.GetGraphicsQueue()->Present(present_info);
switch (result) {
case vk::Result::eErrorOutOfDateKHR:
// Caller will recreate the impl on acquisition, not submission.
[[fallthrough]];
case vk::Result::eErrorSurfaceLostKHR:
// Vulkan guarantees that the set of queue operations will still
// complete successfully.
[[fallthrough]];
case vk::Result::eSuboptimalKHR:
// Even though we're handling rotation changes via polling, we
// still need to handle the case where the swapchain signals that
// it's suboptimal (i.e. every frame when we are rotated given we
// aren't doing Vulkan pre-rotation).
[[fallthrough]];
case vk::Result::eSuccess:
break;
default:
VALIDATION_LOG << "Could not present queue: " << vk::to_string(result);
break;
}
return true;
}
} // namespace impeller