forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vulkan_backbuffer.cc
147 lines (112 loc) · 3.81 KB
/
vulkan_backbuffer.cc
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
// 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 "vulkan_backbuffer.h"
#include <limits>
#include "flutter/vulkan/procs/vulkan_proc_table.h"
#include "third_party/skia/include/gpu/vk/GrVkTypes.h"
#include "vulkan/vulkan.h"
namespace vulkan {
VulkanBackbuffer::VulkanBackbuffer(const VulkanProcTable& p_vk,
const VulkanHandle<VkDevice>& device,
const VulkanHandle<VkCommandPool>& pool)
: vk(p_vk),
device_(device),
usage_command_buffer_(p_vk, device, pool),
render_command_buffer_(p_vk, device, pool),
valid_(false) {
if (!usage_command_buffer_.IsValid() || !render_command_buffer_.IsValid()) {
FML_DLOG(INFO) << "Command buffers were not valid.";
return;
}
if (!CreateSemaphores()) {
FML_DLOG(INFO) << "Could not create semaphores.";
return;
}
if (!CreateFences()) {
FML_DLOG(INFO) << "Could not create fences.";
return;
}
valid_ = true;
}
VulkanBackbuffer::~VulkanBackbuffer() {
FML_ALLOW_UNUSED_LOCAL(WaitFences());
}
bool VulkanBackbuffer::IsValid() const {
return valid_;
}
bool VulkanBackbuffer::CreateSemaphores() {
const VkSemaphoreCreateInfo create_info = {
.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
.pNext = nullptr,
.flags = 0,
};
auto semaphore_collect = [this](VkSemaphore semaphore) {
vk.DestroySemaphore(device_, semaphore, nullptr);
};
for (size_t i = 0; i < semaphores_.size(); i++) {
VkSemaphore semaphore = VK_NULL_HANDLE;
if (VK_CALL_LOG_ERROR(vk.CreateSemaphore(device_, &create_info, nullptr,
&semaphore)) != VK_SUCCESS) {
return false;
}
semaphores_[i] = VulkanHandle<VkSemaphore>{semaphore, semaphore_collect};
}
return true;
}
bool VulkanBackbuffer::CreateFences() {
const VkFenceCreateInfo create_info = {
.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
.pNext = nullptr,
.flags = VK_FENCE_CREATE_SIGNALED_BIT,
};
auto fence_collect = [this](VkFence fence) {
vk.DestroyFence(device_, fence, nullptr);
};
for (size_t i = 0; i < use_fences_.size(); i++) {
VkFence fence = VK_NULL_HANDLE;
if (VK_CALL_LOG_ERROR(vk.CreateFence(device_, &create_info, nullptr,
&fence)) != VK_SUCCESS) {
return false;
}
use_fences_[i] = VulkanHandle<VkFence>{fence, fence_collect};
}
return true;
}
bool VulkanBackbuffer::WaitFences() {
VkFence fences[use_fences_.size()];
for (size_t i = 0; i < use_fences_.size(); i++) {
fences[i] = use_fences_[i];
}
return VK_CALL_LOG_ERROR(vk.WaitForFences(
device_, static_cast<uint32_t>(use_fences_.size()), fences, true,
std::numeric_limits<uint64_t>::max())) == VK_SUCCESS;
}
bool VulkanBackbuffer::ResetFences() {
VkFence fences[use_fences_.size()];
for (size_t i = 0; i < use_fences_.size(); i++) {
fences[i] = use_fences_[i];
}
return VK_CALL_LOG_ERROR(vk.ResetFences(
device_, static_cast<uint32_t>(use_fences_.size()), fences)) ==
VK_SUCCESS;
}
const VulkanHandle<VkFence>& VulkanBackbuffer::GetUsageFence() const {
return use_fences_[0];
}
const VulkanHandle<VkFence>& VulkanBackbuffer::GetRenderFence() const {
return use_fences_[1];
}
const VulkanHandle<VkSemaphore>& VulkanBackbuffer::GetUsageSemaphore() const {
return semaphores_[0];
}
const VulkanHandle<VkSemaphore>& VulkanBackbuffer::GetRenderSemaphore() const {
return semaphores_[1];
}
VulkanCommandBuffer& VulkanBackbuffer::GetUsageCommandBuffer() {
return usage_command_buffer_;
}
VulkanCommandBuffer& VulkanBackbuffer::GetRenderCommandBuffer() {
return render_command_buffer_;
}
} // namespace vulkan