[Impeller] [vulkan] Add support for wrapped surfaces (flutter/engine#35359)
This commit is contained in:
@@ -85,9 +85,11 @@ PlaygroundImplVK::PlaygroundImplVK()
|
||||
void PlaygroundImplVK::SetupSwapchain() {
|
||||
ContextVK* context_vk = reinterpret_cast<ContextVK*>(context_.get());
|
||||
auto window = reinterpret_cast<GLFWwindow*>(handle_.get());
|
||||
::glfwCreateWindowSurface(context_vk->GetInstance(), window, nullptr,
|
||||
&surface_);
|
||||
context_vk->SetupSwapchain(surface_);
|
||||
vk::Instance instance = context_vk->GetInstance();
|
||||
VkSurfaceKHR surface_tmp;
|
||||
::glfwCreateWindowSurface(instance, window, nullptr, &surface_tmp);
|
||||
vk::UniqueSurfaceKHR surface{surface_tmp, instance};
|
||||
context_vk->SetupSwapchain(std::move(surface));
|
||||
}
|
||||
|
||||
PlaygroundImplVK::~PlaygroundImplVK() = default;
|
||||
|
||||
@@ -27,9 +27,6 @@ class PlaygroundImplVK final : public PlaygroundImpl {
|
||||
using UniqueHandle = std::unique_ptr<void, decltype(&DestroyWindowHandle)>;
|
||||
UniqueHandle handle_;
|
||||
|
||||
// surface
|
||||
VkSurfaceKHR surface_;
|
||||
|
||||
// |PlaygroundImpl|
|
||||
std::shared_ptr<Context> GetContext() const override;
|
||||
|
||||
|
||||
@@ -102,8 +102,17 @@ std::shared_ptr<Texture> AllocatorVK::CreateTexture(
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return std::make_shared<TextureVK>(desc, context_, allocator_, img,
|
||||
allocation, allocation_info);
|
||||
auto texture_info = std::make_unique<TextureInfoVK>(TextureInfoVK{
|
||||
.backing_type = TextureBackingTypeVK::kAllocatedTexture,
|
||||
.allocated_texture =
|
||||
{
|
||||
.allocator = &allocator_,
|
||||
.allocation = allocation,
|
||||
.allocation_info = allocation_info,
|
||||
.image = img,
|
||||
},
|
||||
});
|
||||
return std::make_shared<TextureVK>(desc, &context_, std::move(texture_info));
|
||||
}
|
||||
|
||||
// |Allocator|
|
||||
|
||||
@@ -469,8 +469,9 @@ vk::Instance ContextVK::GetInstance() const {
|
||||
return *instance_;
|
||||
}
|
||||
|
||||
void ContextVK::SetupSwapchain(vk::SurfaceKHR surface) {
|
||||
auto present_queue_out = PickPresentQueue(physical_device_, surface);
|
||||
void ContextVK::SetupSwapchain(vk::UniqueSurfaceKHR surface) {
|
||||
surface_ = std::move(surface);
|
||||
auto present_queue_out = PickPresentQueue(physical_device_, *surface);
|
||||
if (!present_queue_out.has_value()) {
|
||||
return;
|
||||
}
|
||||
@@ -478,18 +479,19 @@ void ContextVK::SetupSwapchain(vk::SurfaceKHR surface) {
|
||||
device_->getQueue(present_queue_out->family, present_queue_out->index);
|
||||
|
||||
auto swapchain_details =
|
||||
SwapchainDetailsVK::Create(physical_device_, surface);
|
||||
SwapchainDetailsVK::Create(physical_device_, *surface);
|
||||
if (!swapchain_details) {
|
||||
return;
|
||||
}
|
||||
swapchain_ = SwapchainVK::Create(*device_, surface, *swapchain_details);
|
||||
|
||||
surface_producer_ = SurfaceProducerVK::Create({
|
||||
.device = *device_,
|
||||
.graphics_queue = graphics_queue_,
|
||||
.present_queue = present_queue_,
|
||||
.swapchain = swapchain_.get(),
|
||||
});
|
||||
swapchain_ = SwapchainVK::Create(*device_, *surface, *swapchain_details);
|
||||
auto weak_this = weak_from_this();
|
||||
surface_producer_ = SurfaceProducerVK::Create(
|
||||
weak_this, {
|
||||
.device = *device_,
|
||||
.graphics_queue = graphics_queue_,
|
||||
.present_queue = present_queue_,
|
||||
.swapchain = swapchain_.get(),
|
||||
});
|
||||
}
|
||||
|
||||
} // namespace impeller
|
||||
|
||||
@@ -59,7 +59,7 @@ class ContextVK final : public Context, public BackendCast<ContextVK, Context> {
|
||||
|
||||
vk::Instance GetInstance() const;
|
||||
|
||||
void SetupSwapchain(vk::SurfaceKHR surface);
|
||||
void SetupSwapchain(vk::UniqueSurfaceKHR surface);
|
||||
|
||||
private:
|
||||
std::shared_ptr<fml::ConcurrentTaskRunner> worker_task_runner_;
|
||||
@@ -75,6 +75,7 @@ class ContextVK final : public Context, public BackendCast<ContextVK, Context> {
|
||||
vk::Queue compute_queue_;
|
||||
vk::Queue transfer_queue_;
|
||||
vk::Queue present_queue_;
|
||||
vk::UniqueSurfaceKHR surface_;
|
||||
std::unique_ptr<SwapchainVK> swapchain_;
|
||||
std::unique_ptr<CommandPoolVK> graphics_command_pool_;
|
||||
std::unique_ptr<SurfaceProducerVK> surface_producer_;
|
||||
|
||||
@@ -12,8 +12,10 @@
|
||||
namespace impeller {
|
||||
|
||||
std::unique_ptr<SurfaceProducerVK> SurfaceProducerVK::Create(
|
||||
std::weak_ptr<Context> context,
|
||||
const SurfaceProducerCreateInfoVK& create_info) {
|
||||
auto surface_producer = std::make_unique<SurfaceProducerVK>(create_info);
|
||||
auto surface_producer =
|
||||
std::make_unique<SurfaceProducerVK>(context, create_info);
|
||||
if (!surface_producer->SetupSyncObjects()) {
|
||||
FML_LOG(ERROR) << "Failed to setup sync objects.";
|
||||
return nullptr;
|
||||
@@ -23,8 +25,9 @@ std::unique_ptr<SurfaceProducerVK> SurfaceProducerVK::Create(
|
||||
}
|
||||
|
||||
SurfaceProducerVK::SurfaceProducerVK(
|
||||
std::weak_ptr<Context> context,
|
||||
const SurfaceProducerCreateInfoVK& create_info)
|
||||
: create_info_(create_info) {}
|
||||
: context_(context), create_info_(create_info) {}
|
||||
|
||||
SurfaceProducerVK::~SurfaceProducerVK() = default;
|
||||
|
||||
@@ -63,9 +66,14 @@ std::unique_ptr<Surface> SurfaceProducerVK::AcquireSurface(
|
||||
return Present(image_index);
|
||||
};
|
||||
|
||||
return SurfaceVK::WrapSwapchainImage(
|
||||
create_info_.swapchain->GetSwapchainImage(image_index),
|
||||
std::move(swap_callback));
|
||||
if (auto context = context_.lock()) {
|
||||
ContextVK* context_vk = reinterpret_cast<ContextVK*>(context.get());
|
||||
return SurfaceVK::WrapSwapchainImage(
|
||||
create_info_.swapchain->GetSwapchainImage(image_index), context_vk,
|
||||
std::move(swap_callback));
|
||||
} else {
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
bool SurfaceProducerVK::SetupSyncObjects() {
|
||||
|
||||
@@ -23,15 +23,19 @@ struct SurfaceProducerCreateInfoVK {
|
||||
class SurfaceProducerVK {
|
||||
public:
|
||||
static std::unique_ptr<SurfaceProducerVK> Create(
|
||||
std::weak_ptr<Context> context,
|
||||
const SurfaceProducerCreateInfoVK& create_info);
|
||||
|
||||
explicit SurfaceProducerVK(const SurfaceProducerCreateInfoVK& create_info);
|
||||
SurfaceProducerVK(std::weak_ptr<Context> context,
|
||||
const SurfaceProducerCreateInfoVK& create_info);
|
||||
|
||||
~SurfaceProducerVK();
|
||||
|
||||
std::unique_ptr<Surface> AcquireSurface(vk::CommandBuffer command_buffer);
|
||||
|
||||
private:
|
||||
std::weak_ptr<Context> context_;
|
||||
|
||||
bool SetupSyncObjects();
|
||||
|
||||
bool Submit(vk::CommandBuffer buffer);
|
||||
|
||||
@@ -4,9 +4,12 @@
|
||||
|
||||
#include "impeller/renderer/backend/vulkan/surface_vk.h"
|
||||
|
||||
#include "impeller/renderer/backend/vulkan/texture_vk.h"
|
||||
|
||||
namespace impeller {
|
||||
std::unique_ptr<SurfaceVK> SurfaceVK::WrapSwapchainImage(
|
||||
SwapchainImageVK* swapchain_image,
|
||||
ContextVK* context,
|
||||
SwapCallback swap_callback) {
|
||||
if (!swapchain_image) {
|
||||
return nullptr;
|
||||
@@ -20,10 +23,15 @@ std::unique_ptr<SurfaceVK> SurfaceVK::WrapSwapchainImage(
|
||||
color0_tex.sample_count = SampleCount::kCount1;
|
||||
|
||||
ColorAttachment color0;
|
||||
// TODO (kaushikiska): this needs to be fixed.
|
||||
// color0.texture = std::make_shared<TextureGLES>(
|
||||
// gl_context.GetReactor(), std::move(color0_tex),
|
||||
// TextureGLES::IsWrapped::kWrapped);
|
||||
auto color_texture_info = std::make_unique<TextureInfoVK>(TextureInfoVK{
|
||||
.backing_type = TextureBackingTypeVK::kWrappedTexture,
|
||||
.wrapped_texture =
|
||||
{
|
||||
.swapchain_image = swapchain_image,
|
||||
},
|
||||
});
|
||||
color0.texture = std::make_shared<TextureVK>(std::move(color0_tex), context,
|
||||
std::move(color_texture_info));
|
||||
color0.clear_color = Color::DarkSlateGray();
|
||||
color0.load_action = LoadAction::kClear;
|
||||
color0.store_action = StoreAction::kStore;
|
||||
@@ -38,10 +46,15 @@ std::unique_ptr<SurfaceVK> SurfaceVK::WrapSwapchainImage(
|
||||
|
||||
StencilAttachment stencil0;
|
||||
stencil0.clear_stencil = 0;
|
||||
// TODO (kaushikiska): this needs to be fixed.
|
||||
// stencil0.texture = std::make_shared<TextureGLES>(
|
||||
// gl_context.GetReactor(), std::move(stencil0_tex),
|
||||
// TextureGLES::IsWrapped::kWrapped);
|
||||
auto stencil_texture_info = std::make_unique<TextureInfoVK>(TextureInfoVK{
|
||||
.backing_type = TextureBackingTypeVK::kWrappedTexture,
|
||||
.wrapped_texture =
|
||||
{
|
||||
.swapchain_image = swapchain_image,
|
||||
},
|
||||
});
|
||||
stencil0.texture = std::make_shared<TextureVK>(
|
||||
std::move(stencil0_tex), context, std::move(stencil_texture_info));
|
||||
stencil0.load_action = LoadAction::kClear;
|
||||
stencil0.store_action = StoreAction::kDontCare;
|
||||
|
||||
|
||||
@@ -19,6 +19,7 @@ class SurfaceVK final : public Surface {
|
||||
|
||||
static std::unique_ptr<SurfaceVK> WrapSwapchainImage(
|
||||
SwapchainImageVK* swapchain_image,
|
||||
ContextVK* context,
|
||||
SwapCallback swap_callback);
|
||||
|
||||
SurfaceVK(RenderTarget target,
|
||||
|
||||
@@ -138,6 +138,10 @@ ISize SwapchainImageVK::GetSize() const {
|
||||
return ISize(extent_.width, extent_.height);
|
||||
}
|
||||
|
||||
vk::Image SwapchainImageVK::GetImage() const {
|
||||
return image_;
|
||||
}
|
||||
|
||||
SwapchainImageVK::~SwapchainImageVK() = default;
|
||||
|
||||
} // namespace impeller
|
||||
|
||||
@@ -26,6 +26,8 @@ class SwapchainImageVK {
|
||||
|
||||
ISize GetSize() const;
|
||||
|
||||
vk::Image GetImage() const;
|
||||
|
||||
private:
|
||||
vk::Image image_;
|
||||
vk::UniqueImageView image_view_;
|
||||
|
||||
@@ -7,32 +7,32 @@
|
||||
namespace impeller {
|
||||
|
||||
TextureVK::TextureVK(TextureDescriptor desc,
|
||||
ContextVK& context,
|
||||
const VmaAllocator& allocator,
|
||||
VkImage image,
|
||||
VmaAllocation allocation,
|
||||
VmaAllocationInfo allocation_info)
|
||||
ContextVK* context,
|
||||
std::unique_ptr<TextureInfoVK> texture_info)
|
||||
: Texture(desc),
|
||||
context_(context),
|
||||
allocator_(allocator),
|
||||
image_(image),
|
||||
allocation_(allocation),
|
||||
allocation_info_(allocation_info) {}
|
||||
texture_info_(std::move(texture_info)) {}
|
||||
|
||||
TextureVK::~TextureVK() {
|
||||
if (image_) {
|
||||
vmaDestroyImage(allocator_, image_, allocation_);
|
||||
if (!IsWrapped() && IsValid()) {
|
||||
const auto& texture = texture_info_->allocated_texture;
|
||||
vmaDestroyImage(*texture.allocator, texture.image, texture.allocation);
|
||||
}
|
||||
}
|
||||
|
||||
void TextureVK::SetLabel(std::string_view label) {
|
||||
context_.SetDebugName(vk::Image{image_}, label);
|
||||
context_->SetDebugName(GetImage(), label);
|
||||
}
|
||||
|
||||
bool TextureVK::OnSetContents(const uint8_t* contents,
|
||||
size_t length,
|
||||
size_t slice) {
|
||||
if (!image_ || !contents) {
|
||||
if (IsWrapped()) {
|
||||
FML_LOG(ERROR) << "Cannot set contents of a wrapped texture";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!IsValid() || !contents) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -45,7 +45,8 @@ bool TextureVK::OnSetContents(const uint8_t* contents,
|
||||
}
|
||||
|
||||
// currently we are only supporting 2d textures, no cube textures etc.
|
||||
memcpy(allocation_info_.pMappedData, contents, length);
|
||||
auto mapping = texture_info_->allocated_texture.allocation_info.pMappedData;
|
||||
memcpy(mapping, contents, length);
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -58,11 +59,29 @@ bool TextureVK::OnSetContents(std::shared_ptr<const fml::Mapping> mapping,
|
||||
}
|
||||
|
||||
bool TextureVK::IsValid() const {
|
||||
return image_;
|
||||
switch (texture_info_->backing_type) {
|
||||
case TextureBackingTypeVK::kAllocatedTexture:
|
||||
return texture_info_->allocated_texture.image;
|
||||
case TextureBackingTypeVK::kWrappedTexture:
|
||||
return texture_info_->wrapped_texture.swapchain_image;
|
||||
}
|
||||
}
|
||||
|
||||
ISize TextureVK::GetSize() const {
|
||||
return GetTextureDescriptor().size;
|
||||
}
|
||||
|
||||
bool TextureVK::IsWrapped() const {
|
||||
return texture_info_->backing_type == TextureBackingTypeVK::kWrappedTexture;
|
||||
}
|
||||
|
||||
vk::Image TextureVK::GetImage() const {
|
||||
switch (texture_info_->backing_type) {
|
||||
case TextureBackingTypeVK::kAllocatedTexture:
|
||||
return texture_info_->allocated_texture.image;
|
||||
case TextureBackingTypeVK::kWrappedTexture:
|
||||
return texture_info_->wrapped_texture.swapchain_image->GetImage();
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace impeller
|
||||
|
||||
@@ -7,29 +7,52 @@
|
||||
#include "flutter/fml/macros.h"
|
||||
#include "impeller/base/backend_cast.h"
|
||||
#include "impeller/renderer/backend/vulkan/context_vk.h"
|
||||
#include "impeller/renderer/backend/vulkan/swapchain_vk.h"
|
||||
#include "impeller/renderer/backend/vulkan/vk.h"
|
||||
#include "impeller/renderer/texture.h"
|
||||
|
||||
namespace impeller {
|
||||
|
||||
enum class TextureBackingTypeVK {
|
||||
kAllocatedTexture,
|
||||
kWrappedTexture,
|
||||
};
|
||||
|
||||
struct WrappedTextureInfoVK {
|
||||
SwapchainImageVK* swapchain_image = nullptr;
|
||||
};
|
||||
|
||||
struct AllocatedTextureInfoVK {
|
||||
VmaAllocator* allocator = nullptr;
|
||||
VmaAllocation allocation = nullptr;
|
||||
VmaAllocationInfo allocation_info = {};
|
||||
VkImage image = nullptr;
|
||||
};
|
||||
|
||||
struct TextureInfoVK {
|
||||
TextureBackingTypeVK backing_type;
|
||||
union {
|
||||
WrappedTextureInfoVK wrapped_texture;
|
||||
AllocatedTextureInfoVK allocated_texture;
|
||||
};
|
||||
};
|
||||
|
||||
class TextureVK final : public Texture, public BackendCast<TextureVK, Texture> {
|
||||
public:
|
||||
TextureVK(TextureDescriptor desc,
|
||||
ContextVK& context,
|
||||
const VmaAllocator& allocator,
|
||||
VkImage image,
|
||||
VmaAllocation allocation,
|
||||
VmaAllocationInfo allocation_info);
|
||||
ContextVK* context,
|
||||
std::unique_ptr<TextureInfoVK> texture_info);
|
||||
|
||||
// |Texture|
|
||||
~TextureVK() override;
|
||||
|
||||
bool IsWrapped() const;
|
||||
|
||||
private:
|
||||
ContextVK& context_;
|
||||
const VmaAllocator& allocator_;
|
||||
VkImage image_;
|
||||
VmaAllocation allocation_;
|
||||
VmaAllocationInfo allocation_info_;
|
||||
ContextVK* context_;
|
||||
std::unique_ptr<TextureInfoVK> texture_info_;
|
||||
|
||||
vk::Image GetImage() const;
|
||||
|
||||
// |Texture|
|
||||
void SetLabel(std::string_view label) override;
|
||||
|
||||
Reference in New Issue
Block a user