forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vulkan_device.cc
399 lines (324 loc) · 11.4 KB
/
vulkan_device.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
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
// 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_device.h"
#include <limits>
#include <map>
#include <vector>
#include "flutter/vulkan/procs/vulkan_proc_table.h"
#include "third_party/skia/include/gpu/vk/GrVkBackendContext.h"
#include "vulkan_surface.h"
#include "vulkan_utilities.h"
namespace vulkan {
constexpr auto kVulkanInvalidGraphicsQueueIndex =
std::numeric_limits<uint32_t>::max();
static uint32_t FindGraphicsQueueIndex(
const std::vector<VkQueueFamilyProperties>& properties) {
for (uint32_t i = 0, count = static_cast<uint32_t>(properties.size());
i < count; i++) {
if (properties[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) {
return i;
}
}
return kVulkanInvalidGraphicsQueueIndex;
}
VulkanDevice::VulkanDevice(VulkanProcTable& p_vk,
VulkanHandle<VkPhysicalDevice> physical_device,
bool enable_validation_layers)
: vk(p_vk),
physical_device_(std::move(physical_device)),
graphics_queue_index_(std::numeric_limits<uint32_t>::max()),
valid_(false) {
if (!physical_device_ || !vk.AreInstanceProcsSetup()) {
return;
}
graphics_queue_index_ = FindGraphicsQueueIndex(GetQueueFamilyProperties());
if (graphics_queue_index_ == kVulkanInvalidGraphicsQueueIndex) {
FML_DLOG(INFO) << "Could not find the graphics queue index.";
return;
}
const float priorities[1] = {1.0f};
const VkDeviceQueueCreateInfo queue_create = {
.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
.pNext = nullptr,
.flags = 0,
.queueFamilyIndex = graphics_queue_index_,
.queueCount = 1,
.pQueuePriorities = priorities,
};
const char* extensions[] = {
#if FML_OS_ANDROID
VK_KHR_SWAPCHAIN_EXTENSION_NAME,
#endif
#if OS_FUCHSIA
VK_KHR_EXTERNAL_MEMORY_EXTENSION_NAME,
VK_KHR_EXTERNAL_SEMAPHORE_EXTENSION_NAME,
VK_KHR_GET_MEMORY_REQUIREMENTS_2_EXTENSION_NAME,
VK_FUCHSIA_EXTERNAL_MEMORY_EXTENSION_NAME,
VK_FUCHSIA_EXTERNAL_SEMAPHORE_EXTENSION_NAME,
VK_FUCHSIA_BUFFER_COLLECTION_EXTENSION_NAME,
#endif
};
auto enabled_layers =
DeviceLayersToEnable(vk, physical_device_, enable_validation_layers);
const char* layers[enabled_layers.size()];
for (size_t i = 0; i < enabled_layers.size(); i++) {
layers[i] = enabled_layers[i].c_str();
}
const VkDeviceCreateInfo create_info = {
.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
.pNext = nullptr,
.flags = 0,
.queueCreateInfoCount = 1,
.pQueueCreateInfos = &queue_create,
.enabledLayerCount = static_cast<uint32_t>(enabled_layers.size()),
.ppEnabledLayerNames = layers,
.enabledExtensionCount = sizeof(extensions) / sizeof(const char*),
.ppEnabledExtensionNames = extensions,
.pEnabledFeatures = nullptr,
};
VkDevice device = VK_NULL_HANDLE;
if (VK_CALL_LOG_ERROR(vk.CreateDevice(physical_device_, &create_info, nullptr,
&device)) != VK_SUCCESS) {
FML_DLOG(INFO) << "Could not create device.";
return;
}
device_ = VulkanHandle<VkDevice>{
device, [this](VkDevice device) { vk.DestroyDevice(device, nullptr); }};
if (!vk.SetupDeviceProcAddresses(device_)) {
FML_DLOG(INFO) << "Could not set up device proc addresses.";
return;
}
VkQueue queue = VK_NULL_HANDLE;
vk.GetDeviceQueue(device_, graphics_queue_index_, 0, &queue);
if (queue == VK_NULL_HANDLE) {
FML_DLOG(INFO) << "Could not get the device queue handle.";
return;
}
queue_ = VulkanHandle<VkQueue>(queue);
if (!InitializeCommandPool()) {
return;
}
valid_ = true;
}
VulkanDevice::VulkanDevice(VulkanProcTable& p_vk,
VulkanHandle<VkPhysicalDevice> physical_device,
VulkanHandle<VkDevice> device,
uint32_t queue_family_index,
VulkanHandle<VkQueue> queue)
: vk(p_vk),
physical_device_(std::move(physical_device)),
device_(std::move(device)),
queue_(std::move(queue)),
graphics_queue_index_(queue_family_index),
valid_(false) {
if (!physical_device_ || !vk.AreInstanceProcsSetup()) {
return;
}
if (!InitializeCommandPool()) {
return;
}
valid_ = true;
}
bool VulkanDevice::InitializeCommandPool() {
const VkCommandPoolCreateInfo command_pool_create_info = {
.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
.pNext = nullptr,
.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT,
.queueFamilyIndex = 0,
};
VkCommandPool command_pool = VK_NULL_HANDLE;
if (VK_CALL_LOG_ERROR(vk.CreateCommandPool(device_, &command_pool_create_info,
nullptr, &command_pool)) !=
VK_SUCCESS) {
FML_DLOG(INFO) << "Could not create the command pool.";
return false;
}
command_pool_ = VulkanHandle<VkCommandPool>{
command_pool, [this](VkCommandPool pool) {
vk.DestroyCommandPool(device_, pool, nullptr);
}};
return true;
}
VulkanDevice::~VulkanDevice() {
FML_ALLOW_UNUSED_LOCAL(WaitIdle());
}
bool VulkanDevice::IsValid() const {
return valid_;
}
bool VulkanDevice::WaitIdle() const {
return VK_CALL_LOG_ERROR(vk.DeviceWaitIdle(device_)) == VK_SUCCESS;
}
const VulkanHandle<VkDevice>& VulkanDevice::GetHandle() const {
return device_;
}
void VulkanDevice::ReleaseDeviceOwnership() {
device_.ReleaseOwnership();
}
const VulkanHandle<VkPhysicalDevice>& VulkanDevice::GetPhysicalDeviceHandle()
const {
return physical_device_;
}
const VulkanHandle<VkQueue>& VulkanDevice::GetQueueHandle() const {
return queue_;
}
const VulkanHandle<VkCommandPool>& VulkanDevice::GetCommandPool() const {
return command_pool_;
}
uint32_t VulkanDevice::GetGraphicsQueueIndex() const {
return graphics_queue_index_;
}
bool VulkanDevice::GetSurfaceCapabilities(
const VulkanSurface& surface,
VkSurfaceCapabilitiesKHR* capabilities) const {
#if FML_OS_ANDROID
if (!surface.IsValid() || capabilities == nullptr) {
return false;
}
bool success =
VK_CALL_LOG_ERROR(vk.GetPhysicalDeviceSurfaceCapabilitiesKHR(
physical_device_, surface.Handle(), capabilities)) == VK_SUCCESS;
if (!success) {
return false;
}
// Check if the physical device surface capabilities are valid. If so, there
// is nothing more to do.
if (capabilities->currentExtent.width != 0xFFFFFFFF &&
capabilities->currentExtent.height != 0xFFFFFFFF) {
return true;
}
// Ask the native surface for its size as a fallback.
SkISize size = surface.GetSize();
if (size.width() == 0 || size.height() == 0) {
return false;
}
capabilities->currentExtent.width = size.width();
capabilities->currentExtent.height = size.height();
return true;
#else
return false;
#endif
}
bool VulkanDevice::GetPhysicalDeviceFeatures(
VkPhysicalDeviceFeatures* features) const {
if (features == nullptr || !physical_device_) {
return false;
}
vk.GetPhysicalDeviceFeatures(physical_device_, features);
return true;
}
bool VulkanDevice::GetPhysicalDeviceFeaturesSkia(uint32_t* sk_features) const {
if (sk_features == nullptr) {
return false;
}
VkPhysicalDeviceFeatures features;
if (!GetPhysicalDeviceFeatures(&features)) {
return false;
}
uint32_t flags = 0;
if (features.geometryShader) {
flags |= kGeometryShader_GrVkFeatureFlag;
}
if (features.dualSrcBlend) {
flags |= kDualSrcBlend_GrVkFeatureFlag;
}
if (features.sampleRateShading) {
flags |= kSampleRateShading_GrVkFeatureFlag;
}
*sk_features = flags;
return true;
}
std::vector<VkQueueFamilyProperties> VulkanDevice::GetQueueFamilyProperties()
const {
uint32_t count = 0;
vk.GetPhysicalDeviceQueueFamilyProperties(physical_device_, &count, nullptr);
std::vector<VkQueueFamilyProperties> properties;
properties.resize(count, {});
vk.GetPhysicalDeviceQueueFamilyProperties(physical_device_, &count,
properties.data());
return properties;
}
int VulkanDevice::ChooseSurfaceFormat(
const VulkanSurface& surface,
const std::vector<VkFormat>& desired_formats,
VkSurfaceFormatKHR* format) const {
#if FML_OS_ANDROID
if (!surface.IsValid() || format == nullptr) {
return -1;
}
uint32_t format_count = 0;
if (VK_CALL_LOG_ERROR(vk.GetPhysicalDeviceSurfaceFormatsKHR(
physical_device_, surface.Handle(), &format_count, nullptr)) !=
VK_SUCCESS) {
return -1;
}
if (format_count == 0) {
return -1;
}
std::vector<VkSurfaceFormatKHR> formats;
formats.resize(format_count);
if (VK_CALL_LOG_ERROR(vk.GetPhysicalDeviceSurfaceFormatsKHR(
physical_device_, surface.Handle(), &format_count, formats.data())) !=
VK_SUCCESS) {
return -1;
}
std::map<VkFormat, VkSurfaceFormatKHR> supported_formats;
for (uint32_t i = 0; i < format_count; i++) {
supported_formats[formats[i].format] = formats[i];
}
// Try to find the first supported format in the list of desired formats.
for (size_t i = 0; i < desired_formats.size(); ++i) {
auto found = supported_formats.find(desired_formats[i]);
if (found != supported_formats.end()) {
*format = found->second;
return static_cast<int>(i);
}
}
#endif
return -1;
}
bool VulkanDevice::ChoosePresentMode(const VulkanSurface& surface,
VkPresentModeKHR* present_mode) const {
if (!surface.IsValid() || present_mode == nullptr) {
return false;
}
// https://github.com/LunarG/VulkanSamples/issues/98 indicates that
// VK_PRESENT_MODE_FIFO_KHR is preferable on mobile platforms. The problems
// mentioned in the ticket w.r.t the application being faster that the refresh
// rate of the screen should not be faced by any Flutter platforms as they are
// powered by Vsync pulses instead of depending the submit to block.
// However, for platforms that don't have VSync providers set up, it is better
// to fall back to FIFO. For platforms that do have VSync providers, there
// should be little difference. In case there is a need for a mode other than
// FIFO, availability checks must be performed here before returning the
// result. FIFO is always present.
*present_mode = VK_PRESENT_MODE_FIFO_KHR;
return true;
}
bool VulkanDevice::QueueSubmit(
std::vector<VkPipelineStageFlags> wait_dest_pipeline_stages,
const std::vector<VkSemaphore>& wait_semaphores,
const std::vector<VkSemaphore>& signal_semaphores,
const std::vector<VkCommandBuffer>& command_buffers,
const VulkanHandle<VkFence>& fence) const {
if (wait_semaphores.size() != wait_dest_pipeline_stages.size()) {
return false;
}
const VkSubmitInfo submit_info = {
.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
.pNext = nullptr,
.waitSemaphoreCount = static_cast<uint32_t>(wait_semaphores.size()),
.pWaitSemaphores = wait_semaphores.data(),
.pWaitDstStageMask = wait_dest_pipeline_stages.data(),
.commandBufferCount = static_cast<uint32_t>(command_buffers.size()),
.pCommandBuffers = command_buffers.data(),
.signalSemaphoreCount = static_cast<uint32_t>(signal_semaphores.size()),
.pSignalSemaphores = signal_semaphores.data(),
};
if (VK_CALL_LOG_ERROR(vk.QueueSubmit(queue_, 1, &submit_info, fence)) !=
VK_SUCCESS) {
return false;
}
return true;
}
} // namespace vulkan