forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Setup a vulkan window with device selection and swapchain setup. (flu…
- Loading branch information
1 parent
e4121f8
commit 1b67eb2
Showing
12 changed files
with
985 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Copyright 2015 The Chromium Authors. All rights reserved. | ||
# Use of this source code is governed by a BSD-style license that can be | ||
# found in the LICENSE file. | ||
|
||
source_set("vulkan") { | ||
sources = [ | ||
"vulkan_backbuffer.cc", | ||
"vulkan_backbuffer.h", | ||
"vulkan_handle.cc", | ||
"vulkan_handle.h", | ||
"vulkan_interface.h", | ||
"vulkan_proc_table.cc", | ||
"vulkan_proc_table.h", | ||
"vulkan_window.cc", | ||
"vulkan_window.h", | ||
] | ||
|
||
deps = [ | ||
"//lib/ftl", | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
// Copyright 2016 The Chromium 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> | ||
|
||
namespace vulkan { | ||
|
||
VulkanBackbuffer::VulkanBackbuffer(VulkanProcTable& p_vk, | ||
VulkanHandle<VkDevice>& device, | ||
VulkanHandle<VkCommandPool>& pool, | ||
VulkanHandle<VkImage> image) | ||
: vk(p_vk), | ||
device_(device), | ||
pool_(pool), | ||
image_(std::move(image)), | ||
valid_(false) { | ||
if (!device_ || !pool_ || !image_) { | ||
return; | ||
} | ||
|
||
if (!CreateSemaphores()) { | ||
return; | ||
} | ||
|
||
if (!CreateTransitionBuffers()) { | ||
return; | ||
} | ||
|
||
if (!CreateFences()) { | ||
return; | ||
} | ||
|
||
valid_ = true; | ||
} | ||
|
||
VulkanBackbuffer::~VulkanBackbuffer() { | ||
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.createSemaphore(device_, &create_info, nullptr, &semaphore) != | ||
VK_SUCCESS) { | ||
return false; | ||
} | ||
|
||
semaphores_[i] = {semaphore, semaphore_collect}; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
bool VulkanBackbuffer::CreateTransitionBuffers() { | ||
const VkCommandBufferAllocateInfo allocate_info = { | ||
.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, | ||
.pNext = nullptr, | ||
.commandPool = pool_, | ||
.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY, | ||
.commandBufferCount = 1, | ||
}; | ||
|
||
auto buffer_collect = [this](VkCommandBuffer buffer) { | ||
vk.freeCommandBuffers(device_, pool_, 1, &buffer); | ||
}; | ||
|
||
for (size_t i = 0; i < transition_buffers_.size(); i++) { | ||
VkCommandBuffer buffer = VK_NULL_HANDLE; | ||
|
||
if (vk.allocateCommandBuffers(device_, &allocate_info, &buffer) != | ||
VK_SUCCESS) { | ||
return false; | ||
} | ||
|
||
transition_buffers_[i] = {buffer, buffer_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.createFence(device_, &create_info, nullptr, &fence) != VK_SUCCESS) { | ||
return false; | ||
} | ||
|
||
use_fences_[i] = {fence, fence_collect}; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
void VulkanBackbuffer::WaitFences() { | ||
VkFence fences[use_fences_.size()]; | ||
|
||
for (size_t i = 0; i < use_fences_.size(); i++) { | ||
fences[i] = use_fences_[i]; | ||
} | ||
|
||
vk.waitForFences(device_, static_cast<uint32_t>(use_fences_.size()), fences, | ||
true, std::numeric_limits<uint64_t>::max()); | ||
} | ||
|
||
} // namespace vulkan |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Copyright 2016 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#ifndef FLUTTER_VULKAN_VULKAN_BACKBUFFER_H_ | ||
#define FLUTTER_VULKAN_VULKAN_BACKBUFFER_H_ | ||
|
||
#include <array> | ||
|
||
#include "lib/ftl/macros.h" | ||
#include "vulkan_proc_table.h" | ||
#include "vulkan_handle.h" | ||
|
||
namespace vulkan { | ||
|
||
class VulkanBackbuffer { | ||
public: | ||
VulkanBackbuffer(VulkanProcTable& vk, | ||
VulkanHandle<VkDevice>& device, | ||
VulkanHandle<VkCommandPool>& pool, | ||
VulkanHandle<VkImage> image); | ||
|
||
~VulkanBackbuffer(); | ||
|
||
bool IsValid() const; | ||
|
||
private: | ||
VulkanProcTable& vk; | ||
VulkanHandle<VkDevice>& device_; | ||
VulkanHandle<VkCommandPool>& pool_; | ||
VulkanHandle<VkImage> image_; | ||
|
||
bool valid_; | ||
|
||
std::array<VulkanHandle<VkSemaphore>, 2> semaphores_; | ||
std::array<VulkanHandle<VkCommandBuffer>, 2> transition_buffers_; | ||
std::array<VulkanHandle<VkFence>, 2> use_fences_; | ||
|
||
bool CreateSemaphores(); | ||
|
||
bool CreateTransitionBuffers(); | ||
|
||
bool CreateFences(); | ||
|
||
void WaitFences(); | ||
|
||
FTL_DISALLOW_COPY_AND_ASSIGN(VulkanBackbuffer); | ||
}; | ||
|
||
} // namespace vulkan | ||
|
||
#endif // FLUTTER_VULKAN_VULKAN_BACKBUFFER_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// Copyright 2016 The Chromium 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_handle.h" | ||
|
||
namespace vulkan { | ||
|
||
// | ||
|
||
} // namespace vulkan |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Copyright 2016 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#ifndef FLUTTER_VULKAN_VULKAN_HANDLE_H_ | ||
#define FLUTTER_VULKAN_VULKAN_HANDLE_H_ | ||
|
||
#include <functional> | ||
|
||
#include "lib/ftl/macros.h" | ||
#include "vulkan_interface.h" | ||
#include "lib/ftl/logging.h" | ||
|
||
namespace vulkan { | ||
|
||
template <class T> | ||
class VulkanHandle { | ||
public: | ||
using Handle = T; | ||
using Disposer = std::function<void(Handle)>; | ||
|
||
VulkanHandle() : handle_(VK_NULL_HANDLE) {} | ||
|
||
VulkanHandle(Handle handle, Disposer disposer) | ||
: handle_(handle), disposer_(disposer) {} | ||
|
||
VulkanHandle(VulkanHandle&& other) | ||
: handle_(other.handle_), disposer_(other.disposer_) { | ||
other.handle_ = VK_NULL_HANDLE; | ||
other.disposer_ = nullptr; | ||
} | ||
|
||
~VulkanHandle() { DisposeIfNecessary(); } | ||
|
||
VulkanHandle& operator=(VulkanHandle&& other) { | ||
if (handle_ != other.handle_) { | ||
DisposeIfNecessary(); | ||
} | ||
|
||
handle_ = other.handle_; | ||
disposer_ = other.disposer_; | ||
|
||
other.handle_ = VK_NULL_HANDLE; | ||
other.disposer_ = nullptr; | ||
|
||
return *this; | ||
} | ||
|
||
operator bool() const { return handle_ != VK_NULL_HANDLE; } | ||
|
||
operator Handle() const { return handle_; } | ||
|
||
private: | ||
Handle handle_; | ||
Disposer disposer_; | ||
|
||
void DisposeIfNecessary() { | ||
if (handle_ == VK_NULL_HANDLE) { | ||
return; | ||
} | ||
disposer_(handle_); | ||
handle_ = VK_NULL_HANDLE; | ||
disposer_ = nullptr; | ||
} | ||
|
||
FTL_DISALLOW_COPY_AND_ASSIGN(VulkanHandle); | ||
}; | ||
|
||
} // namespace vulkan | ||
|
||
#endif // FLUTTER_VULKAN_VULKAN_HANDLE_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Copyright 2016 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#ifndef FLUTTER_VULKAN_VULKAN_INTERFACE_H_ | ||
#define FLUTTER_VULKAN_VULKAN_INTERFACE_H_ | ||
|
||
#define VK_NO_PROTOTYPES 1 | ||
|
||
#include "third_party/vulkan/src/vulkan/vulkan.h" | ||
|
||
#define VK_KHR_ANDROID_SURFACE_EXTENSION_NAME "VK_KHR_android_surface" | ||
|
||
typedef VkFlags VkAndroidSurfaceCreateFlagsKHR; | ||
|
||
typedef struct VkAndroidSurfaceCreateInfoKHR { | ||
VkStructureType sType; | ||
const void* pNext; | ||
VkAndroidSurfaceCreateFlagsKHR flags; | ||
void* window; | ||
} VkAndroidSurfaceCreateInfoKHR; | ||
|
||
typedef VkResult(VKAPI_PTR* PFN_vkCreateAndroidSurfaceKHR)( | ||
VkInstance instance, | ||
const VkAndroidSurfaceCreateInfoKHR* pCreateInfo, | ||
const VkAllocationCallbacks* pAllocator, | ||
VkSurfaceKHR* pSurface); | ||
|
||
#endif // FLUTTER_VULKAN_VULKAN_INTERFACE_H_ |
Oops, something went wrong.