[impeller] Wire renderpass for vulkan (flutter/engine#36356)
This commit is contained in:
@@ -822,6 +822,8 @@ FILE: ../../../flutter/impeller/renderer/backend/vulkan/command_pool_vk.cc
|
||||
FILE: ../../../flutter/impeller/renderer/backend/vulkan/command_pool_vk.h
|
||||
FILE: ../../../flutter/impeller/renderer/backend/vulkan/context_vk.cc
|
||||
FILE: ../../../flutter/impeller/renderer/backend/vulkan/context_vk.h
|
||||
FILE: ../../../flutter/impeller/renderer/backend/vulkan/descriptor_pool_vk.cc
|
||||
FILE: ../../../flutter/impeller/renderer/backend/vulkan/descriptor_pool_vk.h
|
||||
FILE: ../../../flutter/impeller/renderer/backend/vulkan/device_buffer_vk.cc
|
||||
FILE: ../../../flutter/impeller/renderer/backend/vulkan/device_buffer_vk.h
|
||||
FILE: ../../../flutter/impeller/renderer/backend/vulkan/formats_vk.cc
|
||||
|
||||
@@ -40,7 +40,7 @@ bool CommandBufferGLES::OnSubmitCommands(CompletionCallback callback) {
|
||||
|
||||
// |CommandBuffer|
|
||||
std::shared_ptr<RenderPass> CommandBufferGLES::OnCreateRenderPass(
|
||||
RenderTarget target) const {
|
||||
RenderTarget target) {
|
||||
if (!IsValid()) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@@ -35,8 +35,7 @@ class CommandBufferGLES final : public CommandBuffer {
|
||||
bool OnSubmitCommands(CompletionCallback callback) override;
|
||||
|
||||
// |CommandBuffer|
|
||||
std::shared_ptr<RenderPass> OnCreateRenderPass(
|
||||
RenderTarget target) const override;
|
||||
std::shared_ptr<RenderPass> OnCreateRenderPass(RenderTarget target) override;
|
||||
|
||||
// |CommandBuffer|
|
||||
std::shared_ptr<BlitPass> OnCreateBlitPass() const override;
|
||||
|
||||
@@ -34,8 +34,7 @@ class CommandBufferMTL final : public CommandBuffer {
|
||||
bool OnSubmitCommands(CompletionCallback callback) override;
|
||||
|
||||
// |CommandBuffer|
|
||||
std::shared_ptr<RenderPass> OnCreateRenderPass(
|
||||
RenderTarget target) const override;
|
||||
std::shared_ptr<RenderPass> OnCreateRenderPass(RenderTarget target) override;
|
||||
|
||||
// |CommandBuffer|
|
||||
std::shared_ptr<BlitPass> OnCreateBlitPass() const override;
|
||||
|
||||
@@ -170,7 +170,7 @@ bool CommandBufferMTL::OnSubmitCommands(CompletionCallback callback) {
|
||||
}
|
||||
|
||||
std::shared_ptr<RenderPass> CommandBufferMTL::OnCreateRenderPass(
|
||||
RenderTarget target) const {
|
||||
RenderTarget target) {
|
||||
if (!buffer_) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@@ -18,6 +18,8 @@ impeller_component("vulkan") {
|
||||
"command_pool_vk.h",
|
||||
"context_vk.cc",
|
||||
"context_vk.h",
|
||||
"descriptor_pool_vk.cc",
|
||||
"descriptor_pool_vk.h",
|
||||
"device_buffer_vk.cc",
|
||||
"device_buffer_vk.h",
|
||||
"formats_vk.cc",
|
||||
|
||||
@@ -124,7 +124,6 @@ std::shared_ptr<DeviceBuffer> AllocatorVK::OnCreateBuffer(
|
||||
.setUsage(vk::BufferUsageFlagBits::eVertexBuffer |
|
||||
vk::BufferUsageFlagBits::eIndexBuffer |
|
||||
vk::BufferUsageFlagBits::eUniformBuffer |
|
||||
vk::BufferUsageFlagBits::eStorageBuffer |
|
||||
vk::BufferUsageFlagBits::eTransferSrc |
|
||||
vk::BufferUsageFlagBits::eTransferDst)
|
||||
.setSize(desc.size)
|
||||
|
||||
@@ -33,15 +33,17 @@ std::shared_ptr<CommandBufferVK> CommandBufferVK::Create(
|
||||
|
||||
vk::UniqueCommandBuffer cmd = std::move(res.value[0]);
|
||||
return std::make_shared<CommandBufferVK>(context, device, surface_producer,
|
||||
std::move(cmd));
|
||||
command_pool, std::move(cmd));
|
||||
}
|
||||
|
||||
CommandBufferVK::CommandBufferVK(std::weak_ptr<const Context> context,
|
||||
vk::Device device,
|
||||
SurfaceProducerVK* surface_producer,
|
||||
vk::CommandPool command_pool,
|
||||
vk::UniqueCommandBuffer command_buffer)
|
||||
: CommandBuffer(context),
|
||||
device_(device),
|
||||
command_pool_(command_pool),
|
||||
command_buffer_(std::move(command_buffer)),
|
||||
surface_producer_(surface_producer) {
|
||||
is_valid_ = true;
|
||||
@@ -72,7 +74,7 @@ bool CommandBufferVK::OnSubmitCommands(CompletionCallback callback) {
|
||||
}
|
||||
|
||||
std::shared_ptr<RenderPass> CommandBufferVK::OnCreateRenderPass(
|
||||
RenderTarget target) const {
|
||||
RenderTarget target) {
|
||||
vk::CommandBufferBeginInfo begin_info;
|
||||
auto res = command_buffer_->begin(begin_info);
|
||||
if (res != vk::Result::eSuccess) {
|
||||
@@ -124,9 +126,11 @@ std::shared_ptr<RenderPass> CommandBufferVK::OnCreateRenderPass(
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return std::make_shared<RenderPassVK>(
|
||||
context_, std::move(target), *command_buffer_,
|
||||
std::move(render_pass_create_res.value));
|
||||
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_);
|
||||
}
|
||||
|
||||
std::shared_ptr<BlitPass> CommandBufferVK::OnCreateBlitPass() const {
|
||||
|
||||
@@ -22,6 +22,7 @@ class CommandBufferVK final : public CommandBuffer {
|
||||
CommandBufferVK(std::weak_ptr<const Context> context,
|
||||
vk::Device device,
|
||||
SurfaceProducerVK* surface_producer,
|
||||
vk::CommandPool command_pool,
|
||||
vk::UniqueCommandBuffer command_buffer);
|
||||
|
||||
// |CommandBuffer|
|
||||
@@ -31,7 +32,9 @@ class CommandBufferVK final : public CommandBuffer {
|
||||
friend class ContextVK;
|
||||
|
||||
vk::Device device_;
|
||||
vk::CommandPool command_pool_;
|
||||
vk::UniqueCommandBuffer command_buffer_;
|
||||
vk::UniqueRenderPass render_pass_;
|
||||
SurfaceProducerVK* surface_producer_;
|
||||
bool is_valid_ = false;
|
||||
|
||||
@@ -45,8 +48,7 @@ class CommandBufferVK final : public CommandBuffer {
|
||||
bool OnSubmitCommands(CompletionCallback callback) override;
|
||||
|
||||
// |CommandBuffer|
|
||||
std::shared_ptr<RenderPass> OnCreateRenderPass(
|
||||
RenderTarget target) const override;
|
||||
std::shared_ptr<RenderPass> OnCreateRenderPass(RenderTarget target) override;
|
||||
|
||||
// |CommandBuffer|
|
||||
std::shared_ptr<BlitPass> OnCreateBlitPass() const override;
|
||||
|
||||
@@ -432,6 +432,7 @@ ContextVK::ContextVK(
|
||||
device_->getQueue(transfer_queue->family, transfer_queue->index);
|
||||
graphics_command_pool_ =
|
||||
CommandPoolVK::Create(*device_, graphics_queue->index);
|
||||
descriptor_pool_ = std::make_shared<DescriptorPoolVK>(*device_);
|
||||
is_valid_ = true;
|
||||
}
|
||||
|
||||
@@ -505,4 +506,8 @@ bool ContextVK::SupportsOffscreenMSAA() const {
|
||||
return true;
|
||||
}
|
||||
|
||||
std::shared_ptr<DescriptorPoolVK> ContextVK::GetDescriptorPool() const {
|
||||
return descriptor_pool_;
|
||||
}
|
||||
|
||||
} // namespace impeller
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
#include "flutter/fml/mapping.h"
|
||||
#include "impeller/base/backend_cast.h"
|
||||
#include "impeller/renderer/backend/vulkan/command_pool_vk.h"
|
||||
#include "impeller/renderer/backend/vulkan/descriptor_pool_vk.h"
|
||||
#include "impeller/renderer/backend/vulkan/pipeline_library_vk.h"
|
||||
#include "impeller/renderer/backend/vulkan/sampler_library_vk.h"
|
||||
#include "impeller/renderer/backend/vulkan/shader_library_vk.h"
|
||||
@@ -63,6 +64,8 @@ class ContextVK final : public Context, public BackendCast<ContextVK, Context> {
|
||||
|
||||
std::unique_ptr<Surface> AcquireSurface();
|
||||
|
||||
std::shared_ptr<DescriptorPoolVK> GetDescriptorPool() const;
|
||||
|
||||
private:
|
||||
std::shared_ptr<fml::ConcurrentTaskRunner> worker_task_runner_;
|
||||
vk::UniqueInstance instance_;
|
||||
@@ -82,6 +85,7 @@ class ContextVK final : public Context, public BackendCast<ContextVK, Context> {
|
||||
std::unique_ptr<CommandPoolVK> graphics_command_pool_;
|
||||
std::unique_ptr<SurfaceProducerVK> surface_producer_;
|
||||
std::shared_ptr<WorkQueue> work_queue_;
|
||||
std::shared_ptr<DescriptorPoolVK> descriptor_pool_;
|
||||
bool is_valid_ = false;
|
||||
|
||||
ContextVK(
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
// 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 "impeller/renderer/backend/vulkan/descriptor_pool_vk.h"
|
||||
|
||||
#include "fml/logging.h"
|
||||
#include "impeller/base/validation.h"
|
||||
#include "vulkan/vulkan_enums.hpp"
|
||||
|
||||
namespace impeller {
|
||||
|
||||
DescriptorPoolVK::DescriptorPoolVK(vk::Device device) {
|
||||
constexpr size_t kPoolSize = 1024;
|
||||
|
||||
std::vector<vk::DescriptorPoolSize> pool_sizes = {
|
||||
{vk::DescriptorType::eSampler, kPoolSize},
|
||||
{vk::DescriptorType::eCombinedImageSampler, kPoolSize},
|
||||
{vk::DescriptorType::eSampledImage, kPoolSize},
|
||||
{vk::DescriptorType::eStorageImage, kPoolSize},
|
||||
{vk::DescriptorType::eUniformTexelBuffer, kPoolSize},
|
||||
{vk::DescriptorType::eStorageTexelBuffer, kPoolSize},
|
||||
{vk::DescriptorType::eUniformBuffer, kPoolSize},
|
||||
{vk::DescriptorType::eStorageBuffer, kPoolSize},
|
||||
{vk::DescriptorType::eUniformBufferDynamic, kPoolSize},
|
||||
{vk::DescriptorType::eStorageBufferDynamic, kPoolSize},
|
||||
{vk::DescriptorType::eInputAttachment, kPoolSize},
|
||||
};
|
||||
|
||||
vk::DescriptorPoolCreateInfo pool_info = {
|
||||
vk::DescriptorPoolCreateFlagBits::eFreeDescriptorSet, // flags
|
||||
static_cast<uint32_t>(pool_sizes.size() * kPoolSize), // max sets
|
||||
static_cast<uint32_t>(pool_sizes.size()), // pool sizes count
|
||||
pool_sizes.data() // pool sizes
|
||||
};
|
||||
|
||||
auto res = device.createDescriptorPoolUnique(pool_info);
|
||||
if (res.result != vk::Result::eSuccess) {
|
||||
VALIDATION_LOG << "Unable to create a descriptor pool";
|
||||
return;
|
||||
}
|
||||
|
||||
pool_ = std::move(res.value);
|
||||
is_valid_ = true;
|
||||
}
|
||||
|
||||
vk::DescriptorPool DescriptorPoolVK::GetPool() {
|
||||
return *pool_;
|
||||
}
|
||||
|
||||
DescriptorPoolVK::~DescriptorPoolVK() = default;
|
||||
|
||||
} // namespace impeller
|
||||
@@ -0,0 +1,31 @@
|
||||
// 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.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "flutter/fml/macros.h"
|
||||
#include "impeller/renderer/backend/vulkan/vk.h"
|
||||
#include "vulkan/vulkan_enums.hpp"
|
||||
#include "vulkan/vulkan_handles.hpp"
|
||||
|
||||
namespace impeller {
|
||||
|
||||
class DescriptorPoolVK {
|
||||
public:
|
||||
explicit DescriptorPoolVK(vk::Device device);
|
||||
|
||||
~DescriptorPoolVK();
|
||||
|
||||
vk::DescriptorPool GetPool();
|
||||
|
||||
private:
|
||||
vk::UniqueDescriptorPool pool_;
|
||||
bool is_valid_ = false;
|
||||
|
||||
FML_DISALLOW_COPY_AND_ASSIGN(DescriptorPoolVK);
|
||||
};
|
||||
|
||||
} // namespace impeller
|
||||
@@ -5,6 +5,7 @@
|
||||
#include "impeller/renderer/backend/vulkan/device_buffer_vk.h"
|
||||
|
||||
#include "fml/logging.h"
|
||||
#include "vulkan/vulkan_handles.hpp"
|
||||
|
||||
namespace impeller {
|
||||
|
||||
@@ -67,8 +68,8 @@ bool DeviceBufferVK::SetLabel(const std::string& label, Range range) {
|
||||
return SetLabel(label);
|
||||
}
|
||||
|
||||
DeviceBufferAllocationVK* DeviceBufferVK::GetAllocation() const {
|
||||
return device_allocation_.get();
|
||||
vk::Buffer DeviceBufferVK::GetVKBufferHandle() const {
|
||||
return device_allocation_->GetBufferHandle();
|
||||
}
|
||||
|
||||
} // namespace impeller
|
||||
|
||||
@@ -45,7 +45,7 @@ class DeviceBufferVK final : public DeviceBuffer,
|
||||
// |DeviceBuffer|
|
||||
~DeviceBufferVK() override;
|
||||
|
||||
DeviceBufferAllocationVK* GetAllocation() const;
|
||||
vk::Buffer GetVKBufferHandle() const;
|
||||
|
||||
private:
|
||||
friend class AllocatorVK;
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
#include "impeller/renderer/descriptor_set_layout.h"
|
||||
#include "impeller/renderer/formats.h"
|
||||
#include "impeller/renderer/shader_types.h"
|
||||
#include "vulkan/vulkan_enums.hpp"
|
||||
|
||||
namespace impeller {
|
||||
|
||||
@@ -289,4 +290,15 @@ constexpr vk::AttachmentStoreOp ToVKAttachmentStoreOp(
|
||||
}
|
||||
}
|
||||
|
||||
constexpr vk::IndexType ToVKIndexType(IndexType index_type) {
|
||||
switch (index_type) {
|
||||
case IndexType::k16bit:
|
||||
return vk::IndexType::eUint16;
|
||||
case IndexType::k32bit:
|
||||
return vk::IndexType::eUint32;
|
||||
case IndexType::kUnknown:
|
||||
return vk::IndexType::eUint32;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace impeller
|
||||
|
||||
@@ -23,7 +23,7 @@ bool PipelineCreateInfoVK::IsValid() const {
|
||||
return is_valid_;
|
||||
}
|
||||
|
||||
vk::Pipeline PipelineCreateInfoVK::GetPipeline() const {
|
||||
vk::Pipeline PipelineCreateInfoVK::GetVKPipeline() const {
|
||||
return *pipeline_;
|
||||
}
|
||||
|
||||
|
||||
@@ -22,7 +22,7 @@ class PipelineCreateInfoVK {
|
||||
|
||||
bool IsValid() const;
|
||||
|
||||
vk::Pipeline GetPipeline() const;
|
||||
vk::Pipeline GetVKPipeline() const;
|
||||
|
||||
vk::RenderPass GetRenderPass() const;
|
||||
|
||||
|
||||
@@ -5,17 +5,24 @@
|
||||
#include "impeller/renderer/backend/vulkan/render_pass_vk.h"
|
||||
|
||||
#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/texture_vk.h"
|
||||
|
||||
namespace impeller {
|
||||
|
||||
RenderPassVK::RenderPassVK(std::weak_ptr<const Context> context,
|
||||
vk::Device device,
|
||||
RenderTarget target,
|
||||
vk::CommandPool command_pool,
|
||||
vk::CommandBuffer command_buffer,
|
||||
vk::UniqueRenderPass render_pass)
|
||||
vk::RenderPass render_pass)
|
||||
: RenderPass(context, target),
|
||||
command_pool_(command_pool),
|
||||
command_buffer_(command_buffer),
|
||||
render_pass_(std::move(render_pass)) {
|
||||
render_pass_(render_pass) {
|
||||
is_valid_ = true;
|
||||
}
|
||||
|
||||
@@ -40,13 +47,256 @@ bool RenderPassVK::OnEncodeCommands(const Context& context) const {
|
||||
if (!render_target.HasColorAttachment(0u)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
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 false;
|
||||
}
|
||||
|
||||
const auto& color0 = render_target.GetColorAttachments().at(0u);
|
||||
const auto& depth0 = render_target.GetDepthAttachment();
|
||||
const auto& stencil0 = render_target.GetStencilAttachment();
|
||||
|
||||
auto& wrapped_texture = TextureVK::Cast(*color0.texture);
|
||||
FML_CHECK(wrapped_texture.IsWrapped());
|
||||
|
||||
FML_UNREACHABLE();
|
||||
auto texture_info = wrapped_texture.GetTextureInfo()->wrapped_texture;
|
||||
vk::UniqueFramebuffer framebuffer = CreateFrameBuffer(texture_info);
|
||||
|
||||
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(),
|
||||
};
|
||||
|
||||
const auto& image_size = texture_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);
|
||||
|
||||
command_buffer_.beginRenderPass(rp_begin_info, vk::SubpassContents::eInline);
|
||||
|
||||
for (const auto& command : commands_) {
|
||||
if (command.index_count == 0u) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (command.instance_count == 0u) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!command.pipeline) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!EncodeCommand(context, command)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool RenderPassVK::EncodeCommand(const Context& context,
|
||||
const Command& command) const {
|
||||
SetViewportAndScissor(command);
|
||||
|
||||
auto& pipeline_vk = PipelineVK::Cast(*command.pipeline);
|
||||
PipelineCreateInfoVK* pipeline_create_info = pipeline_vk.GetCreateInfo();
|
||||
|
||||
if (!AllocateAndBindDescriptorSets(context, command, pipeline_create_info)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
command_buffer_.bindPipeline(vk::PipelineBindPoint::eGraphics,
|
||||
pipeline_create_info->GetVKPipeline());
|
||||
|
||||
auto vertex_buffer_view = command.GetVertexBuffer();
|
||||
auto index_buffer_view = command.index_buffer;
|
||||
|
||||
if (!vertex_buffer_view || !index_buffer_view) {
|
||||
return false;
|
||||
}
|
||||
|
||||
auto& allocator = *context.GetResourceAllocator();
|
||||
const auto& pipeline_desc = command.pipeline->GetDescriptor();
|
||||
|
||||
auto vertex_buffer = vertex_buffer_view.buffer->GetDeviceBuffer(allocator);
|
||||
auto index_buffer = index_buffer_view.buffer->GetDeviceBuffer(allocator);
|
||||
|
||||
if (!vertex_buffer || !index_buffer) {
|
||||
VALIDATION_LOG << "Failed to acquire device buffers"
|
||||
<< " for vertex and index buffer views";
|
||||
return false;
|
||||
}
|
||||
|
||||
// bind vertex buffer
|
||||
auto vertex_buffer_handle =
|
||||
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);
|
||||
|
||||
// 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));
|
||||
|
||||
// execute draw
|
||||
command_buffer_.drawIndexed(command.index_count, command.instance_count, 0, 0,
|
||||
0);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool RenderPassVK::AllocateAndBindDescriptorSets(
|
||||
const Context& context,
|
||||
const Command& command,
|
||||
PipelineCreateInfoVK* pipeline_create_info) const {
|
||||
auto& allocator = *context.GetResourceAllocator();
|
||||
vk::PipelineLayout pipeline_layout =
|
||||
pipeline_create_info->GetPipelineLayout();
|
||||
|
||||
const auto& context_vk = ContextVK::Cast(context);
|
||||
const auto& pool = context_vk.GetDescriptorPool();
|
||||
|
||||
vk::DescriptorSetAllocateInfo alloc_info;
|
||||
std::array<vk::DescriptorSetLayout, 1> dsls = {
|
||||
pipeline_create_info->GetDescriptorSetLayout(),
|
||||
};
|
||||
|
||||
alloc_info.setDescriptorPool(pool->GetPool());
|
||||
alloc_info.setSetLayouts(dsls);
|
||||
|
||||
auto desc_sets_res = device_.allocateDescriptorSets(alloc_info);
|
||||
if (desc_sets_res.result != vk::Result::eSuccess) {
|
||||
VALIDATION_LOG << "Failed to allocate descriptor sets: "
|
||||
<< vk::to_string(desc_sets_res.result);
|
||||
return false;
|
||||
}
|
||||
|
||||
auto desc_sets = desc_sets_res.value;
|
||||
bool update_vertex_descriptors = UpdateDescriptorSets(
|
||||
"vertex_bindings", command.vertex_bindings, allocator, desc_sets[0]);
|
||||
if (!update_vertex_descriptors) {
|
||||
return false;
|
||||
}
|
||||
bool update_frag_descriptors = UpdateDescriptorSets(
|
||||
"fragment_bindings", command.fragment_bindings, allocator, desc_sets[0]);
|
||||
if (!update_frag_descriptors) {
|
||||
return false;
|
||||
}
|
||||
|
||||
command_buffer_.bindDescriptorSets(vk::PipelineBindPoint::eGraphics,
|
||||
pipeline_layout, 0, desc_sets, nullptr);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool RenderPassVK::UpdateDescriptorSets(const char* label,
|
||||
const Bindings& bindings,
|
||||
Allocator& allocator,
|
||||
vk::DescriptorSet desc_set) const {
|
||||
std::vector<vk::WriteDescriptorSet> writes;
|
||||
std::vector<vk::DescriptorBufferInfo> buffer_infos;
|
||||
for (const auto& [buffer_index, view] : bindings.buffers) {
|
||||
const auto& buffer_view = view.resource.buffer;
|
||||
|
||||
auto device_buffer = buffer_view->GetDeviceBuffer(allocator);
|
||||
if (!device_buffer) {
|
||||
VALIDATION_LOG << "Failed to get device buffer for vertex binding";
|
||||
return false;
|
||||
}
|
||||
|
||||
auto buffer = DeviceBufferVK::Cast(*device_buffer).GetVKBufferHandle();
|
||||
if (!buffer) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// reserved index used for per-vertex data.
|
||||
if (buffer_index == VertexDescriptor::kReservedVertexBufferIndex) {
|
||||
continue;
|
||||
}
|
||||
|
||||
uint32_t offset = view.resource.range.offset;
|
||||
|
||||
vk::DescriptorBufferInfo desc_buffer_info;
|
||||
desc_buffer_info.setBuffer(buffer);
|
||||
desc_buffer_info.setOffset(offset);
|
||||
desc_buffer_info.setRange(view.resource.range.length);
|
||||
buffer_infos.push_back(desc_buffer_info);
|
||||
|
||||
const ShaderUniformSlot& uniform = bindings.uniforms.at(buffer_index);
|
||||
|
||||
vk::WriteDescriptorSet setWrite;
|
||||
setWrite.setDstSet(desc_set);
|
||||
setWrite.setDstBinding(uniform.binding);
|
||||
setWrite.setDescriptorCount(1);
|
||||
setWrite.setDescriptorType(vk::DescriptorType::eUniformBuffer);
|
||||
setWrite.setPBufferInfo(&buffer_infos.back());
|
||||
|
||||
writes.push_back(setWrite);
|
||||
}
|
||||
|
||||
std::array<vk::CopyDescriptorSet, 0> copies;
|
||||
device_.updateDescriptorSets(writes, copies);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void RenderPassVK::SetViewportAndScissor(const Command& command) const {
|
||||
// set viewport.
|
||||
const auto& vp = command.viewport.value_or<Viewport>(
|
||||
{.rect = Rect::MakeSize(GetRenderTargetSize())});
|
||||
vk::Viewport viewport = vk::Viewport()
|
||||
.setWidth(vp.rect.size.width)
|
||||
.setHeight(vp.rect.size.height)
|
||||
.setMinDepth(0.0f)
|
||||
.setMaxDepth(1.0f);
|
||||
command_buffer_.setViewport(0, 1, &viewport);
|
||||
|
||||
// scissor
|
||||
const auto& sc =
|
||||
command.scissor.value_or(IRect::MakeSize(GetRenderTargetSize()));
|
||||
vk::Rect2D scissor =
|
||||
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);
|
||||
}
|
||||
|
||||
vk::UniqueFramebuffer 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_)
|
||||
.setAttachmentCount(1)
|
||||
.setPAttachments(&img_view)
|
||||
.setWidth(size.width)
|
||||
.setHeight(size.height)
|
||||
.setLayers(1);
|
||||
auto res = device_.createFramebufferUnique(fb_create_info);
|
||||
FML_CHECK(res.result == vk::Result::eSuccess);
|
||||
return std::move(res.value);
|
||||
}
|
||||
|
||||
} // namespace impeller
|
||||
|
||||
@@ -5,7 +5,9 @@
|
||||
#pragma once
|
||||
|
||||
#include "flutter/fml/macros.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"
|
||||
|
||||
@@ -14,9 +16,11 @@ namespace impeller {
|
||||
class RenderPassVK final : public RenderPass {
|
||||
public:
|
||||
RenderPassVK(std::weak_ptr<const Context> context,
|
||||
vk::Device device,
|
||||
RenderTarget target,
|
||||
vk::CommandPool command_pool,
|
||||
vk::CommandBuffer command_buffer,
|
||||
vk::UniqueRenderPass render_pass);
|
||||
vk::RenderPass render_pass);
|
||||
|
||||
// |RenderPass|
|
||||
~RenderPassVK() override;
|
||||
@@ -24,8 +28,10 @@ class RenderPassVK final : public RenderPass {
|
||||
private:
|
||||
friend class CommandBufferVK;
|
||||
|
||||
vk::Device device_;
|
||||
vk::CommandPool command_pool_;
|
||||
vk::CommandBuffer command_buffer_;
|
||||
vk::UniqueRenderPass render_pass_;
|
||||
vk::RenderPass render_pass_;
|
||||
std::string label_ = "";
|
||||
bool is_valid_ = false;
|
||||
|
||||
@@ -38,6 +44,23 @@ class RenderPassVK final : public RenderPass {
|
||||
// |RenderPass|
|
||||
bool OnEncodeCommands(const Context& context) const override;
|
||||
|
||||
bool EncodeCommand(const Context& context, const Command& command) const;
|
||||
|
||||
bool AllocateAndBindDescriptorSets(
|
||||
const Context& context,
|
||||
const Command& command,
|
||||
PipelineCreateInfoVK* pipeline_create_info) const;
|
||||
|
||||
bool UpdateDescriptorSets(const char* label,
|
||||
const Bindings& bindings,
|
||||
Allocator& allocator,
|
||||
vk::DescriptorSet desc_set) const;
|
||||
|
||||
void SetViewportAndScissor(const Command& command) const;
|
||||
|
||||
vk::UniqueFramebuffer CreateFrameBuffer(
|
||||
const WrappedTextureInfoVK& wrapped_texture_info) const;
|
||||
|
||||
FML_DISALLOW_COPY_AND_ASSIGN(RenderPassVK);
|
||||
};
|
||||
|
||||
|
||||
@@ -33,7 +33,7 @@ bool CommandBuffer::SubmitCommands() {
|
||||
}
|
||||
|
||||
std::shared_ptr<RenderPass> CommandBuffer::CreateRenderPass(
|
||||
RenderTarget render_target) const {
|
||||
RenderTarget render_target) {
|
||||
auto pass = OnCreateRenderPass(std::move(render_target));
|
||||
if (pass && pass->IsValid()) {
|
||||
pass->SetLabel("RenderPass");
|
||||
|
||||
@@ -73,8 +73,7 @@ class CommandBuffer {
|
||||
///
|
||||
/// @return A valid render pass or null.
|
||||
///
|
||||
std::shared_ptr<RenderPass> CreateRenderPass(
|
||||
RenderTarget render_target) const;
|
||||
std::shared_ptr<RenderPass> CreateRenderPass(RenderTarget render_target);
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
/// @brief Create a blit pass to record blit commands into.
|
||||
@@ -96,7 +95,7 @@ class CommandBuffer {
|
||||
explicit CommandBuffer(std::weak_ptr<const Context> context);
|
||||
|
||||
virtual std::shared_ptr<RenderPass> OnCreateRenderPass(
|
||||
RenderTarget render_target) const = 0;
|
||||
RenderTarget render_target) = 0;
|
||||
|
||||
virtual std::shared_ptr<BlitPass> OnCreateBlitPass() const = 0;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user