[Impeller] Vulkan Rendering Prototype (flutter/engine#36415)

This commit is contained in:
Kaushik Iska
2022-09-26 15:40:07 -04:00
committed by GitHub
parent 886a417378
commit 14cc1d29ad
19 changed files with 302 additions and 133 deletions

View File

@@ -83,13 +83,6 @@ InlinePassContext::RenderPassResult InlinePassContext::GetRenderPass(
}
auto color0 = render_target_.GetColorAttachments().find(0)->second;
auto stencil = render_target_.GetStencilAttachment();
if (!stencil.has_value()) {
VALIDATION_LOG << "Stencil attachment unexpectedly missing from the "
"EntityPass render target.";
return {};
}
command_buffer_->SetLabel(
"EntityPass Command Buffer: Depth=" + std::to_string(pass_depth) +
" Count=" + std::to_string(pass_count_));
@@ -109,6 +102,14 @@ InlinePassContext::RenderPassResult InlinePassContext::GetRenderPass(
color0.store_action = StoreAction::kStore;
}
#ifndef IMPELLER_ENABLE_VULKAN
auto stencil = render_target_.GetStencilAttachment();
if (!stencil.has_value()) {
VALIDATION_LOG << "Stencil attachment unexpectedly missing from the "
"EntityPass render target.";
return {};
}
// Only clear the stencil if this is the very first pass of the
// layer.
stencil->load_action =
@@ -118,9 +119,14 @@ InlinePassContext::RenderPassResult InlinePassContext::GetRenderPass(
stencil->store_action = pass_count_ == total_pass_reads_
? StoreAction::kDontCare
: StoreAction::kStore;
render_target_.SetStencilAttachment(stencil.value());
#else
// Touch this variable to avoid a compiler warnings.
// This will go away once stencil support is added for vulkan.
total_pass_reads_ = 1;
#endif
render_target_.SetColorAttachment(color0, 0);
render_target_.SetStencilAttachment(stencil.value());
pass_ = command_buffer_->CreateRenderPass(render_target_);
if (!pass_) {

View File

@@ -108,7 +108,7 @@ PlaygroundImpl::WindowHandle PlaygroundImplVK::GetWindowHandle() const {
std::unique_ptr<Surface> PlaygroundImplVK::AcquireSurfaceFrame(
std::shared_ptr<Context> context) {
ContextVK* context_vk = reinterpret_cast<ContextVK*>(context_.get());
return context_vk->AcquireSurface();
return context_vk->AcquireSurface(current_frame_++);
}
} // namespace impeller

View File

@@ -21,6 +21,7 @@ class PlaygroundImplVK final : public PlaygroundImpl {
private:
std::shared_ptr<fml::ConcurrentMessageLoop> concurrent_loop_;
std::shared_ptr<Context> context_;
size_t current_frame_ = 0;
// windows
static void DestroyWindowHandle(WindowHandle handle);

View File

@@ -252,6 +252,7 @@ bool Playground::OpenPlaygroundHere(Renderer::RenderCallback render_callback) {
}
render_target.SetColorAttachment(color0, 0);
#ifndef IMPELLER_ENABLE_VULKAN
{
TextureDescriptor stencil0_tex;
stencil0_tex.storage_mode = StorageMode::kDeviceTransient;
@@ -279,6 +280,7 @@ bool Playground::OpenPlaygroundHere(Renderer::RenderCallback render_callback) {
render_target.SetStencilAttachment(stencil0);
}
#endif
auto pass = buffer->CreateRenderPass(render_target);
if (!pass) {

View File

@@ -83,9 +83,8 @@ std::shared_ptr<Texture> AllocatorVK::OnCreateTexture(
alloc_create_info.usage = VMA_MEMORY_USAGE_AUTO;
// docs recommend using `VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT` for image
// allocations, but setting them to be host visible for now.
alloc_create_info.flags =
VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT |
VMA_ALLOCATION_CREATE_MAPPED_BIT;
alloc_create_info.flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_RANDOM_BIT |
VMA_ALLOCATION_CREATE_MAPPED_BIT;
auto create_info_native =
static_cast<vk::ImageCreateInfo::NativeType>(image_create_info);
@@ -131,9 +130,8 @@ std::shared_ptr<DeviceBuffer> AllocatorVK::OnCreateBuffer(
VmaAllocationCreateInfo allocCreateInfo = {};
allocCreateInfo.usage = VMA_MEMORY_USAGE_AUTO;
allocCreateInfo.flags =
VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT |
VMA_ALLOCATION_CREATE_MAPPED_BIT;
allocCreateInfo.flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_RANDOM_BIT |
VMA_ALLOCATION_CREATE_MAPPED_BIT;
VkBuffer buffer;
VmaAllocation buffer_allocation;

View File

@@ -63,25 +63,20 @@ bool CommandBufferVK::IsValid() const {
}
bool CommandBufferVK::OnSubmitCommands(CompletionCallback callback) {
bool result = surface_producer_->Submit(*command_buffer_);
// TODO(https://github.com/flutter/flutter/issues/112387)
// This needs to be the place where the command buffer, renderpass,
// and the various descriptor sets in use by the command buffer are
// disposed of.
if (callback) {
callback(result ? CommandBuffer::Status::kCompleted
: CommandBuffer::Status::kError);
callback(CommandBuffer::Status::kCompleted);
}
return result;
return true;
}
std::shared_ptr<RenderPass> CommandBufferVK::OnCreateRenderPass(
RenderTarget target) {
vk::CommandBufferBeginInfo begin_info;
auto res = command_buffer_->begin(begin_info);
if (res != vk::Result::eSuccess) {
VALIDATION_LOG << "Failed to begin command buffer: " << vk::to_string(res);
return nullptr;
}
std::vector<vk::AttachmentDescription> color_attachments;
for (const auto& [k, attachment] : target.GetColorAttachments()) {
const TextureDescriptor& tex_desc =
@@ -95,7 +90,7 @@ std::shared_ptr<RenderPass> CommandBufferVK::OnCreateRenderPass(
color_attachment.setStencilLoadOp(vk::AttachmentLoadOp::eDontCare);
color_attachment.setStencilStoreOp(vk::AttachmentStoreOp::eDontCare);
color_attachment.setInitialLayout(vk::ImageLayout::eUndefined);
color_attachment.setInitialLayout(vk::ImageLayout::eColorAttachmentOptimal);
color_attachment.setFinalLayout(vk::ImageLayout::ePresentSrcKHR);
color_attachments.push_back(color_attachment);
@@ -126,11 +121,9 @@ std::shared_ptr<RenderPass> CommandBufferVK::OnCreateRenderPass(
return nullptr;
}
render_pass_ = std::move(render_pass_create_res.value);
return std::make_shared<RenderPassVK>(context_, device_, std::move(target),
command_pool_, *command_buffer_,
*render_pass_);
return std::make_shared<RenderPassVK>(
context_, device_, std::move(target), std::move(command_buffer_),
std::move(render_pass_create_res.value), surface_producer_);
}
std::shared_ptr<BlitPass> CommandBufferVK::OnCreateBlitPass() const {

View File

@@ -473,8 +473,8 @@ vk::Instance ContextVK::GetInstance() const {
return *instance_;
}
std::unique_ptr<Surface> ContextVK::AcquireSurface() {
return surface_producer_->AcquireSurface();
std::unique_ptr<Surface> ContextVK::AcquireSurface(size_t current_frame) {
return surface_producer_->AcquireSurface(current_frame);
}
void ContextVK::SetupSwapchain(vk::UniqueSurfaceKHR surface) {

View File

@@ -62,7 +62,7 @@ class ContextVK final : public Context, public BackendCast<ContextVK, Context> {
void SetupSwapchain(vk::UniqueSurfaceKHR surface);
std::unique_ptr<Surface> AcquireSurface();
std::unique_ptr<Surface> AcquireSurface(size_t current_frame);
std::shared_ptr<DescriptorPoolVK> GetDescriptorPool() const;

View File

@@ -21,7 +21,9 @@ DeviceBufferAllocationVK::DeviceBufferAllocationVK(
DeviceBufferAllocationVK::~DeviceBufferAllocationVK() {
if (buffer_) {
vmaDestroyBuffer(allocator_, buffer_, allocation_);
// https://github.com/flutter/flutter/issues/112387
// This buffer can be freed once the command buffer is disposed.
// vmaDestroyBuffer(allocator_, buffer_, allocation_);
}
}

View File

@@ -102,7 +102,8 @@ PipelineFuture<ComputePipelineDescriptor> PipelineLibraryVK::GetPipeline(
static vk::AttachmentDescription CreatePlaceholderAttachmentDescription(
vk::Format format,
SampleCount sample_count) {
SampleCount sample_count,
bool is_color) {
vk::AttachmentDescription desc;
// See
@@ -118,8 +119,14 @@ static vk::AttachmentDescription CreatePlaceholderAttachmentDescription(
desc.setStoreOp(vk::AttachmentStoreOp::eDontCare);
desc.setStencilLoadOp(vk::AttachmentLoadOp::eDontCare);
desc.setStencilStoreOp(vk::AttachmentStoreOp::eDontCare);
desc.setInitialLayout(vk::ImageLayout::eUndefined);
desc.setFinalLayout(vk::ImageLayout::eGeneral);
if (!is_color) {
desc.setInitialLayout(vk::ImageLayout::eGeneral);
desc.setFinalLayout(vk::ImageLayout::eGeneral);
} else {
desc.setInitialLayout(vk::ImageLayout::eColorAttachmentOptimal);
desc.setFinalLayout(vk::ImageLayout::ePresentSrcKHR);
}
return desc;
}
@@ -141,7 +148,7 @@ std::optional<vk::UniqueRenderPass> PipelineLibraryVK::CreateRenderPass(
const auto sample_count = desc.GetSampleCount();
// Set the color attachment.
render_pass_attachments.push_back(CreatePlaceholderAttachmentDescription(
vk::Format::eR8G8B8A8Unorm, sample_count));
vk::Format::eB8G8R8A8Unorm, sample_count, true));
std::vector<vk::AttachmentReference> color_attachment_references;
std::vector<vk::AttachmentReference> resolve_attachment_references;
@@ -152,24 +159,30 @@ std::optional<vk::UniqueRenderPass> PipelineLibraryVK::CreateRenderPass(
color_attachment_references.push_back(vk::AttachmentReference(
render_pass_attachments.size() - 1u, vk::ImageLayout::eGeneral));
#if false
// see: https://github.com/flutter/flutter/issues/112388
// Set the resolve attachment if MSAA is enabled.
if (sample_count != SampleCount::kCount1) {
render_pass_attachments.push_back(CreatePlaceholderAttachmentDescription(
vk::Format::eR8G8B8A8Unorm, SampleCount::kCount1));
vk::Format::eR8G8B8A8Unorm, SampleCount::kCount1, false));
resolve_attachment_references.push_back(vk::AttachmentReference(
render_pass_attachments.size() - 1u, vk::ImageLayout::eGeneral));
}
if (desc.HasStencilAttachmentDescriptors()) {
render_pass_attachments.push_back(CreatePlaceholderAttachmentDescription(
vk::Format::eS8Uint, sample_count));
vk::Format::eS8Uint, sample_count, false));
depth_stencil_attachment_reference = vk::AttachmentReference(
render_pass_attachments.size() - 1u, vk::ImageLayout::eGeneral);
}
#endif
vk::SubpassDescription subpass_info;
subpass_info.setPipelineBindPoint(vk::PipelineBindPoint::eGraphics);
subpass_info.setColorAttachments(color_attachment_references);
#if false
// see: https://github.com/flutter/flutter/issues/112388
if (sample_count != SampleCount::kCount1) {
subpass_info.setResolveAttachments(resolve_attachment_references);
}
@@ -177,6 +190,7 @@ std::optional<vk::UniqueRenderPass> PipelineLibraryVK::CreateRenderPass(
subpass_info.setPDepthStencilAttachment(
&depth_stencil_attachment_reference.value());
}
#endif
vk::RenderPassCreateInfo render_pass_info;
render_pass_info.setSubpasses(subpass_info);

View File

@@ -4,25 +4,34 @@
#include "impeller/renderer/backend/vulkan/render_pass_vk.h"
#include <array>
#include <vector>
#include "fml/logging.h"
#include "impeller/base/validation.h"
#include "impeller/renderer/backend/vulkan/context_vk.h"
#include "impeller/renderer/backend/vulkan/device_buffer_vk.h"
#include "impeller/renderer/backend/vulkan/formats_vk.h"
#include "impeller/renderer/backend/vulkan/pipeline_vk.h"
#include "impeller/renderer/backend/vulkan/surface_producer_vk.h"
#include "impeller/renderer/backend/vulkan/texture_vk.h"
#include "impeller/renderer/shader_types.h"
namespace impeller {
static uint32_t color_flash = 0;
RenderPassVK::RenderPassVK(std::weak_ptr<const Context> context,
vk::Device device,
RenderTarget target,
vk::CommandPool command_pool,
vk::CommandBuffer command_buffer,
vk::RenderPass render_pass)
vk::UniqueCommandBuffer command_buffer,
vk::UniqueRenderPass render_pass,
SurfaceProducerVK* surface_producer)
: RenderPass(context, target),
command_pool_(command_pool),
command_buffer_(command_buffer),
render_pass_(render_pass) {
device_(device),
command_buffer_(std::move(command_buffer)),
render_pass_(std::move(render_pass)),
surface_producer_(surface_producer) {
is_valid_ = true;
}
@@ -40,16 +49,14 @@ bool RenderPassVK::OnEncodeCommands(const Context& context) const {
if (!IsValid()) {
return false;
}
if (commands_.empty()) {
return true;
}
const auto& render_target = GetRenderTarget();
if (!render_target.HasColorAttachment(0u)) {
return false;
}
vk::CommandBufferBeginInfo begin_info;
auto res = command_buffer_.begin(begin_info);
auto res = command_buffer_->begin(begin_info);
if (res != vk::Result::eSuccess) {
VALIDATION_LOG << "Failed to begin command buffer: " << vk::to_string(res);
return false;
@@ -62,30 +69,86 @@ bool RenderPassVK::OnEncodeCommands(const Context& context) const {
auto& wrapped_texture = TextureVK::Cast(*color0.texture);
FML_CHECK(wrapped_texture.IsWrapped());
auto texture_info = wrapped_texture.GetTextureInfo()->wrapped_texture;
vk::UniqueFramebuffer framebuffer = CreateFrameBuffer(texture_info);
auto tex_info = wrapped_texture.GetTextureInfo()->wrapped_texture;
// TODO (https://github.com/flutter/flutter/issues/112387)
// this frame buffer has to be destroyed when the command buffer is destroyed.
vk::Framebuffer framebuffer = CreateFrameBuffer(tex_info);
const uint32_t frame_num = tex_info.frame_num;
// layout transition.
{
auto pool = command_buffer_.getPool();
vk::CommandBufferAllocateInfo alloc_info =
vk::CommandBufferAllocateInfo()
.setCommandPool(pool)
.setLevel(vk::CommandBufferLevel::ePrimary)
.setCommandBufferCount(1);
auto cmd_buf_res = device_.allocateCommandBuffersUnique(alloc_info);
if (cmd_buf_res.result != vk::Result::eSuccess) {
VALIDATION_LOG << "Failed to allocate command buffer: "
<< vk::to_string(cmd_buf_res.result);
return false;
}
auto transition_cmd = std::move(cmd_buf_res.value[0]);
vk::CommandBufferBeginInfo begin_info;
auto res = transition_cmd->begin(begin_info);
vk::ImageMemoryBarrier barrier =
vk::ImageMemoryBarrier()
.setSrcAccessMask(vk::AccessFlagBits::eColorAttachmentRead)
.setDstAccessMask(vk::AccessFlagBits::eColorAttachmentWrite)
.setOldLayout(vk::ImageLayout::eUndefined)
.setNewLayout(vk::ImageLayout::eColorAttachmentOptimal)
.setSrcQueueFamilyIndex(VK_QUEUE_FAMILY_IGNORED)
.setDstQueueFamilyIndex(VK_QUEUE_FAMILY_IGNORED)
.setImage(tex_info.swapchain_image->GetImage())
.setSubresourceRange(
vk::ImageSubresourceRange()
.setAspectMask(vk::ImageAspectFlagBits::eColor)
.setBaseMipLevel(0)
.setLevelCount(1)
.setBaseArrayLayer(0)
.setLayerCount(1));
transition_cmd->pipelineBarrier(
vk::PipelineStageFlagBits::eColorAttachmentOutput,
vk::PipelineStageFlagBits::eColorAttachmentOutput, {}, nullptr, nullptr,
barrier);
res = transition_cmd->end();
if (res != vk::Result::eSuccess) {
VALIDATION_LOG << "Failed to end command buffer: " << vk::to_string(res);
return false;
}
surface_producer_->QueueCommandBuffer(frame_num, std::move(transition_cmd));
}
vk::ClearValue clear_value;
clear_value.color =
vk::ClearColorValue(std::array<float, 4>{0.0f, 0.0f, 0.0, 0.0f});
std::array<vk::ImageView, 1> fbo_attachments = {
texture_info.swapchain_image->GetImageView(),
tex_info.swapchain_image->GetImageView(),
};
const auto& image_size = texture_info.swapchain_image->GetSize();
const auto& size = tex_info.swapchain_image->GetSize();
vk::Rect2D render_area =
vk::Rect2D()
.setOffset(vk::Offset2D(0, 0))
.setExtent(vk::Extent2D(image_size.width, image_size.height));
vk::RenderPassBeginInfo rp_begin_info = vk::RenderPassBeginInfo()
.setRenderPass(render_pass_)
.setFramebuffer(*framebuffer)
.setRenderArea(render_area)
.setClearValues(clear_value);
.setExtent(vk::Extent2D(size.width, size.height));
auto rp_begin_info = vk::RenderPassBeginInfo()
.setRenderPass(*render_pass_)
.setFramebuffer(framebuffer)
.setRenderArea(render_area)
.setClearValues(clear_value);
command_buffer_.beginRenderPass(rp_begin_info, vk::SubpassContents::eInline);
command_buffer_->beginRenderPass(rp_begin_info, vk::SubpassContents::eInline);
const auto& transients_allocator = context.GetResourceAllocator();
// encode the commands.
for (const auto& command : commands_) {
if (command.index_count == 0u) {
continue;
@@ -104,14 +167,25 @@ bool RenderPassVK::OnEncodeCommands(const Context& context) const {
}
}
command_buffer_.endRenderPass();
res = command_buffer_.end();
if (res != vk::Result::eSuccess) {
VALIDATION_LOG << "Failed to end command buffer: " << vk::to_string(res);
return false;
}
command_buffer_->endRenderPass();
return true;
return const_cast<RenderPassVK*>(this)->EndCommandBuffer(frame_num);
}
bool RenderPassVK::EndCommandBuffer(uint32_t frame_num) {
if (command_buffer_) {
auto res = command_buffer_->end();
if (res != vk::Result::eSuccess) {
VALIDATION_LOG << "Failed to end command buffer: " << vk::to_string(res);
return false;
}
surface_producer_->StashRP(frame_num, std::move(render_pass_));
return surface_producer_->QueueCommandBuffer(frame_num,
std::move(command_buffer_));
}
return false;
}
bool RenderPassVK::EncodeCommand(const Context& context,
@@ -125,8 +199,8 @@ bool RenderPassVK::EncodeCommand(const Context& context,
return false;
}
command_buffer_.bindPipeline(vk::PipelineBindPoint::eGraphics,
pipeline_create_info->GetVKPipeline());
command_buffer_->bindPipeline(vk::PipelineBindPoint::eGraphics,
pipeline_create_info->GetVKPipeline());
auto vertex_buffer_view = command.GetVertexBuffer();
auto index_buffer_view = command.index_buffer;
@@ -152,19 +226,19 @@ bool RenderPassVK::EncodeCommand(const Context& context,
DeviceBufferVK::Cast(*vertex_buffer).GetVKBufferHandle();
vk::Buffer vertex_buffers[] = {vertex_buffer_handle};
vk::DeviceSize vertex_buffer_offsets[] = {vertex_buffer_view.range.offset};
command_buffer_.bindVertexBuffers(0, 1, vertex_buffers,
vertex_buffer_offsets);
command_buffer_->bindVertexBuffers(0, 1, vertex_buffers,
vertex_buffer_offsets);
// index buffer
auto index_buffer_handle =
DeviceBufferVK::Cast(*index_buffer).GetVKBufferHandle();
command_buffer_.bindIndexBuffer(index_buffer_handle,
index_buffer_view.range.offset,
ToVKIndexType(command.index_type));
command_buffer_->bindIndexBuffer(index_buffer_handle,
index_buffer_view.range.offset,
ToVKIndexType(command.index_type));
// execute draw
command_buffer_.drawIndexed(command.index_count, command.instance_count, 0, 0,
0);
command_buffer_->drawIndexed(command.index_count, command.instance_count, 0,
0, 0);
return true;
}
@@ -206,8 +280,8 @@ bool RenderPassVK::AllocateAndBindDescriptorSets(
return false;
}
command_buffer_.bindDescriptorSets(vk::PipelineBindPoint::eGraphics,
pipeline_layout, 0, desc_sets, nullptr);
command_buffer_->bindDescriptorSets(vk::PipelineBindPoint::eGraphics,
pipeline_layout, 0, desc_sets, nullptr);
return true;
}
@@ -271,7 +345,7 @@ void RenderPassVK::SetViewportAndScissor(const Command& command) const {
.setHeight(vp.rect.size.height)
.setMinDepth(0.0f)
.setMaxDepth(1.0f);
command_buffer_.setViewport(0, 1, &viewport);
command_buffer_->setViewport(0, 1, &viewport);
// scissor
const auto& sc =
@@ -280,21 +354,21 @@ void RenderPassVK::SetViewportAndScissor(const Command& command) const {
vk::Rect2D()
.setOffset(vk::Offset2D(sc.origin.x, sc.origin.y))
.setExtent(vk::Extent2D(sc.size.width, sc.size.height));
command_buffer_.setScissor(0, 1, &scissor);
command_buffer_->setScissor(0, 1, &scissor);
}
vk::UniqueFramebuffer RenderPassVK::CreateFrameBuffer(
vk::Framebuffer RenderPassVK::CreateFrameBuffer(
const WrappedTextureInfoVK& wrapped_texture_info) const {
auto img_view = wrapped_texture_info.swapchain_image->GetImageView();
auto size = wrapped_texture_info.swapchain_image->GetSize();
vk::FramebufferCreateInfo fb_create_info = vk::FramebufferCreateInfo()
.setRenderPass(render_pass_)
.setRenderPass(*render_pass_)
.setAttachmentCount(1)
.setPAttachments(&img_view)
.setWidth(size.width)
.setHeight(size.height)
.setLayers(1);
auto res = device_.createFramebufferUnique(fb_create_info);
auto res = device_.createFramebuffer(fb_create_info);
FML_CHECK(res.result == vk::Result::eSuccess);
return std::move(res.value);
}

View File

@@ -4,12 +4,15 @@
#pragma once
#include <vector>
#include "flutter/fml/macros.h"
#include "impeller/renderer/backend/vulkan/surface_producer_vk.h"
#include "impeller/renderer/backend/vulkan/texture_vk.h"
#include "impeller/renderer/backend/vulkan/vk.h"
#include "impeller/renderer/command.h"
#include "impeller/renderer/render_pass.h"
#include "impeller/renderer/render_target.h"
#include "vulkan/vulkan_structs.hpp"
namespace impeller {
@@ -18,9 +21,9 @@ class RenderPassVK final : public RenderPass {
RenderPassVK(std::weak_ptr<const Context> context,
vk::Device device,
RenderTarget target,
vk::CommandPool command_pool,
vk::CommandBuffer command_buffer,
vk::RenderPass render_pass);
vk::UniqueCommandBuffer command_buffer,
vk::UniqueRenderPass render_pass,
SurfaceProducerVK* surface_producer);
// |RenderPass|
~RenderPassVK() override;
@@ -29,9 +32,10 @@ class RenderPassVK final : public RenderPass {
friend class CommandBufferVK;
vk::Device device_;
vk::CommandPool command_pool_;
vk::CommandBuffer command_buffer_;
vk::RenderPass render_pass_;
vk::UniqueCommandBuffer command_buffer_;
vk::UniqueRenderPass render_pass_;
SurfaceProducerVK* surface_producer_;
std::string label_ = "";
bool is_valid_ = false;
@@ -51,6 +55,8 @@ class RenderPassVK final : public RenderPass {
const Command& command,
PipelineCreateInfoVK* pipeline_create_info) const;
bool EndCommandBuffer(uint32_t frame_num);
bool UpdateDescriptorSets(const char* label,
const Bindings& bindings,
Allocator& allocator,
@@ -58,7 +64,7 @@ class RenderPassVK final : public RenderPass {
void SetViewportAndScissor(const Command& command) const;
vk::UniqueFramebuffer CreateFrameBuffer(
vk::Framebuffer CreateFrameBuffer(
const WrappedTextureInfoVK& wrapped_texture_info) const;
FML_DISALLOW_COPY_AND_ASSIGN(RenderPassVK);

View File

@@ -6,9 +6,9 @@
#include <array>
#include "fml/logging.h"
#include "impeller/base/validation.h"
#include "impeller/renderer/backend/vulkan/surface_vk.h"
#include "impeller/renderer/backend/vulkan/vk.h"
namespace impeller {
@@ -18,7 +18,7 @@ std::unique_ptr<SurfaceProducerVK> SurfaceProducerVK::Create(
auto surface_producer =
std::make_unique<SurfaceProducerVK>(context, create_info);
if (!surface_producer->SetupSyncObjects()) {
FML_LOG(ERROR) << "Failed to setup sync objects.";
VALIDATION_LOG << "Failed to setup sync objects.";
return nullptr;
}
@@ -32,16 +32,21 @@ SurfaceProducerVK::SurfaceProducerVK(
SurfaceProducerVK::~SurfaceProducerVK() = default;
std::unique_ptr<Surface> SurfaceProducerVK::AcquireSurface() {
auto fence_wait_res = create_info_.device.waitForFences({*in_flight_fence_},
VK_TRUE, UINT64_MAX);
std::unique_ptr<Surface> SurfaceProducerVK::AcquireSurface(
size_t current_frame) {
current_frame = current_frame % kMaxFramesInFlight;
const auto& sync_objects = sync_objects_[current_frame];
auto fence_wait_res = create_info_.device.waitForFences(
{*sync_objects->in_flight_fence}, VK_TRUE, UINT64_MAX);
if (fence_wait_res != vk::Result::eSuccess) {
VALIDATION_LOG << "Failed to wait for fence: "
<< vk::to_string(fence_wait_res);
return nullptr;
}
auto fence_reset_res = create_info_.device.resetFences({*in_flight_fence_});
auto fence_reset_res =
create_info_.device.resetFences({*sync_objects->in_flight_fence});
if (fence_reset_res != vk::Result::eSuccess) {
VALIDATION_LOG << "Failed to reset fence: "
<< vk::to_string(fence_reset_res);
@@ -51,7 +56,7 @@ std::unique_ptr<Surface> SurfaceProducerVK::AcquireSurface() {
uint32_t image_index;
auto acuire_image_res = create_info_.device.acquireNextImageKHR(
create_info_.swapchain->GetSwapchain(), UINT64_MAX,
*image_available_semaphore_, {}, &image_index);
*sync_objects->image_available_semaphore, {}, &image_index);
if (acuire_image_res != vk::Result::eSuccess &&
acuire_image_res != vk::Result::eSuboptimalKHR) {
@@ -64,92 +69,127 @@ std::unique_ptr<Surface> SurfaceProducerVK::AcquireSurface() {
VALIDATION_LOG << "Suboptimal image acquired.";
}
SurfaceVK::SwapCallback swap_callback = [this, image_index]() {
return Present(image_index);
SurfaceVK::SwapCallback swap_callback = [this, current_frame, image_index]() {
return Present(current_frame, image_index);
};
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));
current_frame, create_info_.swapchain->GetSwapchainImage(image_index),
context_vk, std::move(swap_callback));
} else {
return nullptr;
}
}
bool SurfaceProducerVK::SetupSyncObjects() {
std::unique_ptr<SurfaceSyncObjectsVK> SurfaceSyncObjectsVK::Create(
vk::Device device) {
auto sync_objects = std::make_unique<SurfaceSyncObjectsVK>();
vk::SemaphoreCreateInfo semaphore_create_info;
{
auto image_avail_res =
create_info_.device.createSemaphoreUnique(semaphore_create_info);
auto image_avail_res = device.createSemaphoreUnique(semaphore_create_info);
if (image_avail_res.result != vk::Result::eSuccess) {
VALIDATION_LOG << "Failed to create image available semaphore: "
<< vk::to_string(image_avail_res.result);
return false;
return nullptr;
}
image_available_semaphore_ = std::move(image_avail_res.value);
sync_objects->image_available_semaphore = std::move(image_avail_res.value);
}
{
auto render_finished_res =
create_info_.device.createSemaphoreUnique(semaphore_create_info);
device.createSemaphoreUnique(semaphore_create_info);
if (render_finished_res.result != vk::Result::eSuccess) {
FML_LOG(ERROR) << "Failed to create render finished semaphore: "
VALIDATION_LOG << "Failed to create render finished semaphore: "
<< vk::to_string(render_finished_res.result);
return false;
return nullptr;
}
render_finished_semaphore_ = std::move(render_finished_res.value);
sync_objects->render_finished_semaphore =
std::move(render_finished_res.value);
}
vk::FenceCreateInfo fence_create_info;
fence_create_info.flags = vk::FenceCreateFlagBits::eSignaled;
{
auto fence_res = create_info_.device.createFenceUnique(fence_create_info);
auto fence_res = device.createFenceUnique(fence_create_info);
if (fence_res.result != vk::Result::eSuccess) {
VALIDATION_LOG << "Failed to create fence: "
<< vk::to_string(fence_res.result);
return false;
return nullptr;
}
in_flight_fence_ = std::move(fence_res.value);
sync_objects->in_flight_fence = std::move(fence_res.value);
}
return sync_objects;
}
bool SurfaceProducerVK::SetupSyncObjects() {
for (size_t i = 0; i < kMaxFramesInFlight; i++) {
auto sync_objects = SurfaceSyncObjectsVK::Create(create_info_.device);
if (!sync_objects) {
return false;
}
sync_objects_[i] = std::move(sync_objects);
}
return true;
}
bool SurfaceProducerVK::Submit(vk::CommandBuffer buffer) {
bool SurfaceProducerVK::QueueCommandBuffer(uint32_t frame_num,
vk::UniqueCommandBuffer buffer) {
command_buffers_[frame_num].push_back(std::move(buffer));
return true;
}
bool SurfaceProducerVK::Submit(uint32_t frame_num) {
auto& sync_objects = sync_objects_[frame_num];
vk::SubmitInfo submit_info;
std::array<vk::PipelineStageFlags, 1> wait_stages = {
vk::PipelineStageFlagBits::eColorAttachmentOutput};
submit_info.setWaitDstStageMask(wait_stages);
std::array<vk::Semaphore, 1> wait_semaphores = {*image_available_semaphore_};
std::array<vk::Semaphore, 1> wait_semaphores = {
*sync_objects->image_available_semaphore};
submit_info.setWaitSemaphores(wait_semaphores);
std::array<vk::Semaphore, 1> signal_semaphores = {
*render_finished_semaphore_};
*sync_objects->render_finished_semaphore};
submit_info.setSignalSemaphores(signal_semaphores);
std::array<vk::CommandBuffer, 1> command_buffers = {buffer};
std::vector<vk::CommandBuffer> command_buffers = {};
for (auto& buf : command_buffers_[frame_num]) {
command_buffers.push_back(*buf);
}
submit_info.setCommandBuffers(command_buffers);
auto graphics_submit_res =
create_info_.graphics_queue.submit({submit_info}, *in_flight_fence_);
auto graphics_submit_res = create_info_.graphics_queue.submit(
{submit_info}, *sync_objects->in_flight_fence);
if (graphics_submit_res != vk::Result::eSuccess) {
FML_LOG(ERROR) << "Failed to submit graphics queue: "
VALIDATION_LOG << "Failed to submit graphics queue: "
<< vk::to_string(graphics_submit_res);
return false;
}
auto idle_wait_res = create_info_.graphics_queue.waitIdle();
if (idle_wait_res != vk::Result::eSuccess) {
VALIDATION_LOG << "Failed to wait for graphics queue idle: "
<< vk::to_string(idle_wait_res);
return false;
}
return true;
}
bool SurfaceProducerVK::Present(uint32_t image_index) {
bool SurfaceProducerVK::Present(size_t frame_num, uint32_t image_index) {
Submit(frame_num);
auto& sync_objects = sync_objects_[frame_num];
vk::PresentInfoKHR present_info;
std::array<vk::Semaphore, 1> signal_semaphores = {
*render_finished_semaphore_};
*sync_objects->render_finished_semaphore};
present_info.setWaitSemaphores(signal_semaphores);
std::array<vk::SwapchainKHR, 1> swapchains = {
@@ -160,11 +200,15 @@ bool SurfaceProducerVK::Present(uint32_t image_index) {
present_info.setImageIndices(image_indices);
auto present_res = create_info_.present_queue.presentKHR(present_info);
if (present_res != vk::Result::eSuccess) {
FML_LOG(ERROR) << "Failed to present: " << vk::to_string(present_res);
if ((present_res != vk::Result::eSuccess) &&
(present_res != vk::Result::eSuboptimalKHR)) {
command_buffers_[frame_num].clear();
stash_rp_[frame_num].clear();
return false;
}
command_buffers_[frame_num].clear();
stash_rp_[frame_num].clear();
return true;
}

View File

@@ -10,6 +10,7 @@
#include "impeller/renderer/backend/vulkan/swapchain_vk.h"
#include "impeller/renderer/backend/vulkan/vk.h"
#include "impeller/renderer/surface.h"
#include "vulkan/vulkan_handles.hpp"
namespace impeller {
@@ -20,6 +21,15 @@ struct SurfaceProducerCreateInfoVK {
SwapchainVK* swapchain;
};
class SurfaceSyncObjectsVK {
public:
vk::UniqueSemaphore image_available_semaphore;
vk::UniqueSemaphore render_finished_semaphore;
vk::UniqueFence in_flight_fence;
static std::unique_ptr<SurfaceSyncObjectsVK> Create(vk::Device device);
};
class SurfaceProducerVK {
public:
static std::unique_ptr<SurfaceProducerVK> Create(
@@ -31,23 +41,30 @@ class SurfaceProducerVK {
~SurfaceProducerVK();
std::unique_ptr<Surface> AcquireSurface();
std::unique_ptr<Surface> AcquireSurface(size_t current_frame);
bool Submit(vk::CommandBuffer buffer);
// take ownership of the command buffer until present.
bool QueueCommandBuffer(uint32_t frame_num, vk::UniqueCommandBuffer buffer);
void StashRP(uint32_t frame_num, vk::UniqueRenderPass data) {
stash_rp_[frame_num].push_back(std::move(data));
}
private:
std::weak_ptr<Context> context_;
bool SetupSyncObjects();
bool Present(uint32_t image_index);
bool Submit(uint32_t frame_num);
bool Present(size_t frame_num, uint32_t image_index);
const SurfaceProducerCreateInfoVK create_info_;
// sync objects
vk::UniqueSemaphore image_available_semaphore_;
vk::UniqueSemaphore render_finished_semaphore_;
vk::UniqueFence in_flight_fence_;
std::unique_ptr<SurfaceSyncObjectsVK> sync_objects_[kMaxFramesInFlight];
std::vector<vk::UniqueCommandBuffer> command_buffers_[kMaxFramesInFlight];
std::vector<vk::UniqueRenderPass> stash_rp_[kMaxFramesInFlight];
FML_DISALLOW_COPY_AND_ASSIGN(SurfaceProducerVK);
};

View File

@@ -9,6 +9,7 @@
namespace impeller {
std::unique_ptr<SurfaceVK> SurfaceVK::WrapSwapchainImage(
uint32_t frame_num,
SwapchainImageVK* swapchain_image,
ContextVK* context,
SwapCallback swap_callback) {
@@ -30,6 +31,7 @@ std::unique_ptr<SurfaceVK> SurfaceVK::WrapSwapchainImage(
.wrapped_texture =
{
.swapchain_image = swapchain_image,
.frame_num = frame_num,
},
});
color0.texture = std::make_shared<TextureVK>(std::move(color0_tex), context,
@@ -53,6 +55,7 @@ std::unique_ptr<SurfaceVK> SurfaceVK::WrapSwapchainImage(
.wrapped_texture =
{
.swapchain_image = swapchain_image,
.frame_num = frame_num,
},
});
stencil0.texture = std::make_shared<TextureVK>(

View File

@@ -18,6 +18,7 @@ class SurfaceVK final : public Surface {
using SwapCallback = std::function<bool(void)>;
static std::unique_ptr<SurfaceVK> WrapSwapchainImage(
uint32_t frame_num,
SwapchainImageVK* swapchain_image,
ContextVK* context,
SwapCallback swap_callback);

View File

@@ -60,6 +60,8 @@ bool TextureVK::OnSetContents(std::shared_ptr<const fml::Mapping> mapping,
bool TextureVK::IsValid() const {
switch (texture_info_->backing_type) {
case TextureBackingTypeVK::kUnknownType:
return false;
case TextureBackingTypeVK::kAllocatedTexture:
return texture_info_->allocated_texture.image;
case TextureBackingTypeVK::kWrappedTexture:
@@ -81,6 +83,8 @@ bool TextureVK::IsWrapped() const {
vk::Image TextureVK::GetImage() const {
switch (texture_info_->backing_type) {
case TextureBackingTypeVK::kUnknownType:
FML_CHECK(false) << "Unknown texture backing type";
case TextureBackingTypeVK::kAllocatedTexture:
return texture_info_->allocated_texture.image;
case TextureBackingTypeVK::kWrappedTexture:

View File

@@ -14,12 +14,14 @@
namespace impeller {
enum class TextureBackingTypeVK {
kUnknownType,
kAllocatedTexture,
kWrappedTexture,
};
struct WrappedTextureInfoVK {
SwapchainImageVK* swapchain_image = nullptr;
uint32_t frame_num = 0;
};
struct AllocatedTextureInfoVK {

View File

@@ -39,6 +39,8 @@ static_assert(VK_HEADER_VERSION >= 215,
namespace impeller {
const uint32_t kMaxFramesInFlight = 2;
struct QueueVK {
size_t family = 0;
size_t index = 0;