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
476 lines (414 loc) · 16.1 KB
/
swapchain_impl_vk.cc
File metadata and controls
476 lines (414 loc) · 16.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
// 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 "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/surface_vk.h"
#include "impeller/renderer/backend/vulkan/swapchain_image_vk.h"
namespace impeller {
static constexpr size_t kMaxFramesInFlight = 3u;
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;
}
static std::optional<vk::Queue> ChoosePresentQueue(
const vk::PhysicalDevice& physical_device,
const vk::Device& device,
const vk::SurfaceKHR& surface) {
const auto families = physical_device.getQueueFamilyProperties();
for (size_t family_index = 0u; family_index < families.size();
family_index++) {
auto [result, supported] =
physical_device.getSurfaceSupportKHR(family_index, surface);
if (result == vk::Result::eSuccess && supported) {
return device.getQueue(family_index, 0u);
}
}
return std::nullopt;
}
std::shared_ptr<SwapchainImplVK> SwapchainImplVK::Create(
const std::shared_ptr<Context>& context,
vk::UniqueSurfaceKHR surface,
bool was_rotated,
vk::SwapchainKHR old_swapchain) {
return std::shared_ptr<SwapchainImplVK>(new SwapchainImplVK(
context, std::move(surface), was_rotated, old_swapchain));
}
SwapchainImplVK::SwapchainImplVK(const std::shared_ptr<Context>& context,
vk::UniqueSurfaceKHR surface,
bool was_rotated,
vk::SwapchainKHR old_swapchain) {
if (!context) {
return;
}
auto& vk_context = ContextVK::Cast(*context);
auto [caps_result, 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(caps.supportedCompositeAlpha);
if (!composite.has_value()) {
VALIDATION_LOG << "No composition mode supported.";
return;
}
auto present_queue = ChoosePresentQueue(vk_context.GetPhysicalDevice(), //
vk_context.GetDevice(), //
*surface //
);
if (!present_queue.has_value()) {
VALIDATION_LOG << "Could not pick present queue.";
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(caps.currentExtent.width, caps.minImageExtent.width,
caps.maxImageExtent.width),
std::clamp(caps.currentExtent.height, caps.minImageExtent.height,
caps.maxImageExtent.height),
};
swapchain_info.minImageCount = std::clamp(
caps.minImageCount + 1u, // preferred image count
caps.minImageCount, // min count cannot be zero
caps.maxImageCount == 0u ? caps.minImageCount + 1u
: caps.maxImageCount // max zero means no limit
);
swapchain_info.imageArrayLayers = 1u;
swapchain_info.imageUsage = vk::ImageUsageFlagBits::eColorAttachment;
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);
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;
}
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);
present_queue_ = present_queue.value();
surface_format_ = swapchain_info.imageFormat;
swapchain_ = std::move(swapchain);
images_ = std::move(swapchain_images);
synchronizers_ = std::move(synchronizers);
current_frame_ = synchronizers_.size() - 1u;
is_valid_ = true;
was_rotated_ = was_rotated;
is_rotated_ = was_rotated;
}
SwapchainImplVK::~SwapchainImplVK() {
DestroySwapchain();
}
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 {};
}
if (was_rotated_ != is_rotated_) {
return AcquireResult{true /* out of date */};
}
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 {};
}
//----------------------------------------------------------------------------
/// Get the next image index.
///
auto [acq_result, index] = context.GetDevice().acquireNextImageKHR(
*swapchain_, // swapchain
std::numeric_limits<uint64_t>::max(), // timeout (nanoseconds)
*sync->render_ready, // signal semaphore
nullptr // fence
);
if (acq_result == vk::Result::eSuboptimalKHR) {
is_rotated_ = true;
return AcquireResult{true /* out of date */};
}
if (acq_result == vk::Result::eErrorOutOfDateKHR) {
return AcquireResult{true /* out of date */};
}
if (acq_result != vk::Result::eSuccess) {
VALIDATION_LOG << "Could not acquire next swapchain image: "
<< vk::to_string(acq_result);
return {};
}
if (index >= images_.size()) {
VALIDATION_LOG << "Swapchain returned an invalid image index.";
return {};
}
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
)};
}
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_];
//----------------------------------------------------------------------------
/// 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();
{
LayoutTransition transition;
transition.new_layout = vk::ImageLayout::ePresentSrcKHR;
transition.cmd_buffer = vk_final_cmd_buffer;
transition.src_access = vk::AccessFlagBits::eColorAttachmentWrite;
transition.src_stage = vk::PipelineStageFlagBits::eColorAttachmentOutput;
transition.dst_access = {};
transition.dst_stage = vk::PipelineStageFlagBits::eBottomOfPipe;
if (!image->SetLayout(transition)) {
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);
switch (auto result = present_queue_.presentKHR(present_info)) {
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::eSuccess:
is_rotated_ = false;
return true;
case vk::Result::eSuboptimalKHR:
is_rotated_ = true;
return true;
default:
VALIDATION_LOG << "Could not present queue: " << vk::to_string(result);
return false;
}
FML_UNREACHABLE();
return false;
}
} // namespace impeller