[Impeller] defer construction of VkCommandBuffer until encoding begins. (flutter/engine#43605)
VkCommandBuffers are allocated out of a thread local VkCommandPool. THis command buffer is only safe to use on the thread which allocates it. We'd like to attempt concurrent encoding on Vulkan like we've done for iOS, which means the worker threads will be accessing the VkCommandBuffer. In order for this to be safe, the VkCommandBuffer must be allocated out of a VkCommandPool from that worker thread. As a simple fix, we make the Impeller CommandBuffer defer construction of the VkCommandBuffer object, allowing us to record impeller commands on the raster thread, move the command buffer to a worker thread, create the VkCommandBuffer, and then begin encoding.
This commit is contained in:
@@ -36,7 +36,7 @@ class CommandBufferMock : public CommandBuffer {
|
||||
return command_buffer->OnCreateRenderPass(render_target);
|
||||
}
|
||||
|
||||
MOCK_CONST_METHOD0(OnCreateBlitPass, std::shared_ptr<BlitPass>());
|
||||
MOCK_METHOD0(OnCreateBlitPass, std::shared_ptr<BlitPass>());
|
||||
static std::shared_ptr<BlitPass> ForwardOnCreateBlitPass(
|
||||
CommandBuffer* command_buffer) {
|
||||
return command_buffer->OnCreateBlitPass();
|
||||
@@ -53,7 +53,7 @@ class CommandBufferMock : public CommandBuffer {
|
||||
return command_buffer->OnWaitUntilScheduled();
|
||||
}
|
||||
|
||||
MOCK_CONST_METHOD0(OnCreateComputePass, std::shared_ptr<ComputePass>());
|
||||
MOCK_METHOD0(OnCreateComputePass, std::shared_ptr<ComputePass>());
|
||||
static std::shared_ptr<ComputePass> ForwardOnCreateComputePass(
|
||||
CommandBuffer* command_buffer) {
|
||||
return command_buffer->OnCreateComputePass();
|
||||
|
||||
@@ -58,7 +58,7 @@ std::shared_ptr<RenderPass> CommandBufferGLES::OnCreateRenderPass(
|
||||
}
|
||||
|
||||
// |CommandBuffer|
|
||||
std::shared_ptr<BlitPass> CommandBufferGLES::OnCreateBlitPass() const {
|
||||
std::shared_ptr<BlitPass> CommandBufferGLES::OnCreateBlitPass() {
|
||||
if (!IsValid()) {
|
||||
return nullptr;
|
||||
}
|
||||
@@ -70,7 +70,7 @@ std::shared_ptr<BlitPass> CommandBufferGLES::OnCreateBlitPass() const {
|
||||
}
|
||||
|
||||
// |CommandBuffer|
|
||||
std::shared_ptr<ComputePass> CommandBufferGLES::OnCreateComputePass() const {
|
||||
std::shared_ptr<ComputePass> CommandBufferGLES::OnCreateComputePass() {
|
||||
// Compute passes aren't supported until GLES 3.2, at which point Vulkan is
|
||||
// available anyway.
|
||||
return nullptr;
|
||||
|
||||
@@ -41,10 +41,10 @@ class CommandBufferGLES final : public CommandBuffer {
|
||||
std::shared_ptr<RenderPass> OnCreateRenderPass(RenderTarget target) override;
|
||||
|
||||
// |CommandBuffer|
|
||||
std::shared_ptr<BlitPass> OnCreateBlitPass() const override;
|
||||
std::shared_ptr<BlitPass> OnCreateBlitPass() override;
|
||||
|
||||
// |CommandBuffer|
|
||||
std::shared_ptr<ComputePass> OnCreateComputePass() const override;
|
||||
std::shared_ptr<ComputePass> OnCreateComputePass() override;
|
||||
|
||||
FML_DISALLOW_COPY_AND_ASSIGN(CommandBufferGLES);
|
||||
};
|
||||
|
||||
@@ -43,10 +43,10 @@ class CommandBufferMTL final : public CommandBuffer {
|
||||
std::shared_ptr<RenderPass> OnCreateRenderPass(RenderTarget target) override;
|
||||
|
||||
// |CommandBuffer|
|
||||
std::shared_ptr<BlitPass> OnCreateBlitPass() const override;
|
||||
std::shared_ptr<BlitPass> OnCreateBlitPass() override;
|
||||
|
||||
// |CommandBuffer|
|
||||
std::shared_ptr<ComputePass> OnCreateComputePass() const override;
|
||||
std::shared_ptr<ComputePass> OnCreateComputePass() override;
|
||||
|
||||
FML_DISALLOW_COPY_AND_ASSIGN(CommandBufferMTL);
|
||||
};
|
||||
|
||||
@@ -256,7 +256,7 @@ std::shared_ptr<RenderPass> CommandBufferMTL::OnCreateRenderPass(
|
||||
return pass;
|
||||
}
|
||||
|
||||
std::shared_ptr<BlitPass> CommandBufferMTL::OnCreateBlitPass() const {
|
||||
std::shared_ptr<BlitPass> CommandBufferMTL::OnCreateBlitPass() {
|
||||
if (!buffer_) {
|
||||
return nullptr;
|
||||
}
|
||||
@@ -269,7 +269,7 @@ std::shared_ptr<BlitPass> CommandBufferMTL::OnCreateBlitPass() const {
|
||||
return pass;
|
||||
}
|
||||
|
||||
std::shared_ptr<ComputePass> CommandBufferMTL::OnCreateComputePass() const {
|
||||
std::shared_ptr<ComputePass> CommandBufferMTL::OnCreateComputePass() {
|
||||
if (!buffer_) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@@ -13,9 +13,7 @@ namespace testing {
|
||||
TEST(BlitCommandVkTest, BlitCopyTextureToTextureCommandVK) {
|
||||
auto context = CreateMockVulkanContext();
|
||||
auto pool = CommandPoolVK::GetThreadLocal(context.get());
|
||||
CommandEncoderVK encoder(context->GetDeviceHolder(),
|
||||
context->GetGraphicsQueue(), pool,
|
||||
context->GetFenceWaiter());
|
||||
auto encoder = std::make_unique<CommandEncoderFactoryVK>(context)->Create();
|
||||
BlitCopyTextureToTextureCommandVK cmd;
|
||||
cmd.source = context->GetResourceAllocator()->CreateTexture({
|
||||
.size = ISize(100, 100),
|
||||
@@ -23,18 +21,15 @@ TEST(BlitCommandVkTest, BlitCopyTextureToTextureCommandVK) {
|
||||
cmd.destination = context->GetResourceAllocator()->CreateTexture({
|
||||
.size = ISize(100, 100),
|
||||
});
|
||||
bool result = cmd.Encode(encoder);
|
||||
bool result = cmd.Encode(*encoder.get());
|
||||
EXPECT_TRUE(result);
|
||||
EXPECT_TRUE(encoder.IsTracking(cmd.source));
|
||||
EXPECT_TRUE(encoder.IsTracking(cmd.destination));
|
||||
EXPECT_TRUE(encoder->IsTracking(cmd.source));
|
||||
EXPECT_TRUE(encoder->IsTracking(cmd.destination));
|
||||
}
|
||||
|
||||
TEST(BlitCommandVkTest, BlitCopyTextureToBufferCommandVK) {
|
||||
auto context = CreateMockVulkanContext();
|
||||
auto pool = CommandPoolVK::GetThreadLocal(context.get());
|
||||
CommandEncoderVK encoder(context->GetDeviceHolder(),
|
||||
context->GetGraphicsQueue(), pool,
|
||||
context->GetFenceWaiter());
|
||||
auto encoder = std::make_unique<CommandEncoderFactoryVK>(context)->Create();
|
||||
BlitCopyTextureToBufferCommandVK cmd;
|
||||
cmd.source = context->GetResourceAllocator()->CreateTexture({
|
||||
.size = ISize(100, 100),
|
||||
@@ -42,18 +37,15 @@ TEST(BlitCommandVkTest, BlitCopyTextureToBufferCommandVK) {
|
||||
cmd.destination = context->GetResourceAllocator()->CreateBuffer({
|
||||
.size = 1,
|
||||
});
|
||||
bool result = cmd.Encode(encoder);
|
||||
bool result = cmd.Encode(*encoder.get());
|
||||
EXPECT_TRUE(result);
|
||||
EXPECT_TRUE(encoder.IsTracking(cmd.source));
|
||||
EXPECT_TRUE(encoder.IsTracking(cmd.destination));
|
||||
EXPECT_TRUE(encoder->IsTracking(cmd.source));
|
||||
EXPECT_TRUE(encoder->IsTracking(cmd.destination));
|
||||
}
|
||||
|
||||
TEST(BlitCommandVkTest, BlitCopyBufferToTextureCommandVK) {
|
||||
auto context = CreateMockVulkanContext();
|
||||
auto pool = CommandPoolVK::GetThreadLocal(context.get());
|
||||
CommandEncoderVK encoder(context->GetDeviceHolder(),
|
||||
context->GetGraphicsQueue(), pool,
|
||||
context->GetFenceWaiter());
|
||||
auto encoder = std::make_unique<CommandEncoderFactoryVK>(context)->Create();
|
||||
BlitCopyBufferToTextureCommandVK cmd;
|
||||
cmd.destination = context->GetResourceAllocator()->CreateTexture({
|
||||
.size = ISize(100, 100),
|
||||
@@ -63,26 +55,23 @@ TEST(BlitCommandVkTest, BlitCopyBufferToTextureCommandVK) {
|
||||
.size = 1,
|
||||
})
|
||||
->AsBufferView();
|
||||
bool result = cmd.Encode(encoder);
|
||||
bool result = cmd.Encode(*encoder.get());
|
||||
EXPECT_TRUE(result);
|
||||
EXPECT_TRUE(encoder.IsTracking(cmd.source.buffer));
|
||||
EXPECT_TRUE(encoder.IsTracking(cmd.destination));
|
||||
EXPECT_TRUE(encoder->IsTracking(cmd.source.buffer));
|
||||
EXPECT_TRUE(encoder->IsTracking(cmd.destination));
|
||||
}
|
||||
|
||||
TEST(BlitCommandVkTest, BlitGenerateMipmapCommandVK) {
|
||||
auto context = CreateMockVulkanContext();
|
||||
auto pool = CommandPoolVK::GetThreadLocal(context.get());
|
||||
CommandEncoderVK encoder(context->GetDeviceHolder(),
|
||||
context->GetGraphicsQueue(), pool,
|
||||
context->GetFenceWaiter());
|
||||
auto encoder = std::make_unique<CommandEncoderFactoryVK>(context)->Create();
|
||||
BlitGenerateMipmapCommandVK cmd;
|
||||
cmd.texture = context->GetResourceAllocator()->CreateTexture({
|
||||
.size = ISize(100, 100),
|
||||
.mip_count = 2,
|
||||
});
|
||||
bool result = cmd.Encode(encoder);
|
||||
bool result = cmd.Encode(*encoder.get());
|
||||
EXPECT_TRUE(result);
|
||||
EXPECT_TRUE(encoder.IsTracking(cmd.texture));
|
||||
EXPECT_TRUE(encoder->IsTracking(cmd.texture));
|
||||
}
|
||||
|
||||
} // namespace testing
|
||||
|
||||
@@ -6,11 +6,12 @@
|
||||
|
||||
#include "flutter/fml/logging.h"
|
||||
#include "flutter/fml/trace_event.h"
|
||||
#include "impeller/renderer/backend/vulkan/command_buffer_vk.h"
|
||||
|
||||
namespace impeller {
|
||||
|
||||
BlitPassVK::BlitPassVK(std::weak_ptr<CommandEncoderVK> encoder)
|
||||
: encoder_(std::move(encoder)) {}
|
||||
BlitPassVK::BlitPassVK(std::weak_ptr<CommandBufferVK> command_buffer)
|
||||
: command_buffer_(std::move(command_buffer)) {}
|
||||
|
||||
BlitPassVK::~BlitPassVK() = default;
|
||||
|
||||
@@ -35,7 +36,11 @@ bool BlitPassVK::EncodeCommands(
|
||||
return false;
|
||||
}
|
||||
|
||||
auto encoder = encoder_.lock();
|
||||
auto command_buffer = command_buffer_.lock();
|
||||
if (!command_buffer) {
|
||||
return false;
|
||||
}
|
||||
auto encoder = command_buffer->GetEncoder();
|
||||
if (!encoder) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
namespace impeller {
|
||||
|
||||
class CommandEncoderVK;
|
||||
class CommandBufferVK;
|
||||
|
||||
class BlitPassVK final : public BlitPass {
|
||||
public:
|
||||
@@ -21,11 +22,11 @@ class BlitPassVK final : public BlitPass {
|
||||
private:
|
||||
friend class CommandBufferVK;
|
||||
|
||||
std::weak_ptr<CommandEncoderVK> encoder_;
|
||||
std::weak_ptr<CommandBufferVK> command_buffer_;
|
||||
std::vector<std::unique_ptr<BlitEncodeVK>> commands_;
|
||||
std::string label_;
|
||||
|
||||
BlitPassVK(std::weak_ptr<CommandEncoderVK> encoder);
|
||||
BlitPassVK(std::weak_ptr<CommandBufferVK> command_buffer);
|
||||
|
||||
// |BlitPass|
|
||||
bool IsValid() const override;
|
||||
|
||||
@@ -20,30 +20,34 @@
|
||||
|
||||
namespace impeller {
|
||||
|
||||
CommandBufferVK::CommandBufferVK(std::weak_ptr<const Context> context,
|
||||
std::shared_ptr<CommandEncoderVK> encoder)
|
||||
: CommandBuffer(std::move(context)), encoder_(std::move(encoder)) {
|
||||
if (!encoder_ || !encoder_->IsValid()) {
|
||||
return;
|
||||
}
|
||||
is_valid_ = true;
|
||||
}
|
||||
CommandBufferVK::CommandBufferVK(
|
||||
std::weak_ptr<const Context> context,
|
||||
std::shared_ptr<CommandEncoderFactoryVK> encoder_factory)
|
||||
: CommandBuffer(std::move(context)),
|
||||
encoder_factory_(std::move(encoder_factory)) {}
|
||||
|
||||
CommandBufferVK::~CommandBufferVK() = default;
|
||||
|
||||
void CommandBufferVK::SetLabel(const std::string& label) const {
|
||||
auto context = context_.lock();
|
||||
if (!context || !encoder_) {
|
||||
return;
|
||||
if (!encoder_) {
|
||||
encoder_factory_->SetLabel(label);
|
||||
} else {
|
||||
auto context = context_.lock();
|
||||
if (!context || !encoder_) {
|
||||
return;
|
||||
}
|
||||
ContextVK::Cast(*context).SetDebugName(encoder_->GetCommandBuffer(), label);
|
||||
}
|
||||
ContextVK::Cast(*context).SetDebugName(encoder_->GetCommandBuffer(), label);
|
||||
}
|
||||
|
||||
bool CommandBufferVK::IsValid() const {
|
||||
return is_valid_;
|
||||
return true;
|
||||
}
|
||||
|
||||
const std::shared_ptr<CommandEncoderVK>& CommandBufferVK::GetEncoder() const {
|
||||
const std::shared_ptr<CommandEncoderVK>& CommandBufferVK::GetEncoder() {
|
||||
if (!encoder_) {
|
||||
encoder_ = encoder_factory_->Create();
|
||||
}
|
||||
return encoder_;
|
||||
}
|
||||
|
||||
@@ -65,28 +69,29 @@ std::shared_ptr<RenderPass> CommandBufferVK::OnCreateRenderPass(
|
||||
if (!context) {
|
||||
return nullptr;
|
||||
}
|
||||
auto pass = std::shared_ptr<RenderPassVK>(new RenderPassVK(context, //
|
||||
target, //
|
||||
encoder_ //
|
||||
));
|
||||
auto pass =
|
||||
std::shared_ptr<RenderPassVK>(new RenderPassVK(context, //
|
||||
target, //
|
||||
weak_from_this() //
|
||||
));
|
||||
if (!pass->IsValid()) {
|
||||
return nullptr;
|
||||
}
|
||||
return pass;
|
||||
}
|
||||
|
||||
std::shared_ptr<BlitPass> CommandBufferVK::OnCreateBlitPass() const {
|
||||
std::shared_ptr<BlitPass> CommandBufferVK::OnCreateBlitPass() {
|
||||
if (!IsValid()) {
|
||||
return nullptr;
|
||||
}
|
||||
auto pass = std::shared_ptr<BlitPassVK>(new BlitPassVK(encoder_));
|
||||
auto pass = std::shared_ptr<BlitPassVK>(new BlitPassVK(weak_from_this()));
|
||||
if (!pass->IsValid()) {
|
||||
return nullptr;
|
||||
}
|
||||
return pass;
|
||||
}
|
||||
|
||||
std::shared_ptr<ComputePass> CommandBufferVK::OnCreateComputePass() const {
|
||||
std::shared_ptr<ComputePass> CommandBufferVK::OnCreateComputePass() {
|
||||
if (!IsValid()) {
|
||||
return nullptr;
|
||||
}
|
||||
@@ -94,9 +99,10 @@ std::shared_ptr<ComputePass> CommandBufferVK::OnCreateComputePass() const {
|
||||
if (!context) {
|
||||
return nullptr;
|
||||
}
|
||||
auto pass = std::shared_ptr<ComputePassVK>(new ComputePassVK(context, //
|
||||
encoder_ //
|
||||
));
|
||||
auto pass =
|
||||
std::shared_ptr<ComputePassVK>(new ComputePassVK(context, //
|
||||
weak_from_this() //
|
||||
));
|
||||
if (!pass->IsValid()) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@@ -12,25 +12,27 @@
|
||||
namespace impeller {
|
||||
|
||||
class ContextVK;
|
||||
class CommandEncoderFactoryVK;
|
||||
class CommandEncoderVK;
|
||||
|
||||
class CommandBufferVK final
|
||||
: public CommandBuffer,
|
||||
public BackendCast<CommandBufferVK, CommandBuffer> {
|
||||
public BackendCast<CommandBufferVK, CommandBuffer>,
|
||||
public std::enable_shared_from_this<CommandBufferVK> {
|
||||
public:
|
||||
// |CommandBuffer|
|
||||
~CommandBufferVK() override;
|
||||
|
||||
const std::shared_ptr<CommandEncoderVK>& GetEncoder() const;
|
||||
const std::shared_ptr<CommandEncoderVK>& GetEncoder();
|
||||
|
||||
private:
|
||||
friend class ContextVK;
|
||||
|
||||
std::shared_ptr<CommandEncoderVK> encoder_;
|
||||
bool is_valid_ = false;
|
||||
std::shared_ptr<CommandEncoderFactoryVK> encoder_factory_;
|
||||
|
||||
CommandBufferVK(std::weak_ptr<const Context> context,
|
||||
std::shared_ptr<CommandEncoderVK> encoder);
|
||||
std::shared_ptr<CommandEncoderFactoryVK> encoder_factory);
|
||||
|
||||
// |CommandBuffer|
|
||||
void SetLabel(const std::string& label) const override;
|
||||
@@ -48,10 +50,10 @@ class CommandBufferVK final
|
||||
std::shared_ptr<RenderPass> OnCreateRenderPass(RenderTarget target) override;
|
||||
|
||||
// |CommandBuffer|
|
||||
std::shared_ptr<BlitPass> OnCreateBlitPass() const override;
|
||||
std::shared_ptr<BlitPass> OnCreateBlitPass() override;
|
||||
|
||||
// |CommandBuffer|
|
||||
std::shared_ptr<ComputePass> OnCreateComputePass() const override;
|
||||
std::shared_ptr<ComputePass> OnCreateComputePass() override;
|
||||
|
||||
FML_DISALLOW_COPY_AND_ASSIGN(CommandBufferVK);
|
||||
};
|
||||
|
||||
@@ -96,29 +96,61 @@ class TrackedObjectsVK {
|
||||
FML_DISALLOW_COPY_AND_ASSIGN(TrackedObjectsVK);
|
||||
};
|
||||
|
||||
CommandEncoderVK::CommandEncoderVK(
|
||||
const std::weak_ptr<const DeviceHolder>& device_holder,
|
||||
const std::shared_ptr<QueueVK>& queue,
|
||||
const std::shared_ptr<CommandPoolVK>& pool,
|
||||
std::shared_ptr<FenceWaiterVK> fence_waiter)
|
||||
: fence_waiter_(std::move(fence_waiter)),
|
||||
tracked_objects_(
|
||||
std::make_shared<TrackedObjectsVK>(device_holder, pool)) {
|
||||
if (!fence_waiter_ || !tracked_objects_->IsValid() || !queue) {
|
||||
return;
|
||||
CommandEncoderFactoryVK::CommandEncoderFactoryVK(
|
||||
const std::weak_ptr<const ContextVK>& context)
|
||||
: context_(context) {}
|
||||
|
||||
void CommandEncoderFactoryVK::SetLabel(const std::string& label) {
|
||||
label_ = label;
|
||||
}
|
||||
|
||||
std::shared_ptr<CommandEncoderVK> CommandEncoderFactoryVK::Create() {
|
||||
auto context = context_.lock();
|
||||
if (!context) {
|
||||
return nullptr;
|
||||
}
|
||||
auto& context_vk = ContextVK::Cast(*context);
|
||||
auto tls_pool = CommandPoolVK::GetThreadLocal(&context_vk);
|
||||
if (!tls_pool) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto tracked_objects = std::make_shared<TrackedObjectsVK>(
|
||||
context_vk.GetDeviceHolder(), tls_pool);
|
||||
auto queue = context_vk.GetGraphicsQueue();
|
||||
|
||||
if (!tracked_objects || !tracked_objects->IsValid() || !queue) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
vk::CommandBufferBeginInfo begin_info;
|
||||
begin_info.flags = vk::CommandBufferUsageFlagBits::eOneTimeSubmit;
|
||||
if (tracked_objects_->GetCommandBuffer().begin(begin_info) !=
|
||||
if (tracked_objects->GetCommandBuffer().begin(begin_info) !=
|
||||
vk::Result::eSuccess) {
|
||||
VALIDATION_LOG << "Could not begin command buffer.";
|
||||
return;
|
||||
return nullptr;
|
||||
}
|
||||
device_holder_ = device_holder;
|
||||
queue_ = queue;
|
||||
is_valid_ = true;
|
||||
|
||||
if (label_.has_value()) {
|
||||
context_vk.SetDebugName(tracked_objects->GetCommandBuffer(),
|
||||
label_.value());
|
||||
}
|
||||
|
||||
return std::make_shared<CommandEncoderVK>(context_vk.GetDeviceHolder(),
|
||||
tracked_objects, queue,
|
||||
context_vk.GetFenceWaiter());
|
||||
}
|
||||
|
||||
CommandEncoderVK::CommandEncoderVK(
|
||||
std::weak_ptr<const DeviceHolder> device_holder,
|
||||
std::shared_ptr<TrackedObjectsVK> tracked_objects,
|
||||
const std::shared_ptr<QueueVK>& queue,
|
||||
std::shared_ptr<FenceWaiterVK> fence_waiter)
|
||||
: device_holder_(std::move(device_holder)),
|
||||
tracked_objects_(std::move(tracked_objects)),
|
||||
queue_(queue),
|
||||
fence_waiter_(std::move(fence_waiter)) {}
|
||||
|
||||
CommandEncoderVK::~CommandEncoderVK() = default;
|
||||
|
||||
bool CommandEncoderVK::IsValid() const {
|
||||
@@ -169,7 +201,6 @@ bool CommandEncoderVK::Submit(SubmitCallback callback) {
|
||||
// Submit will proceed, call callback with true when it is done and do not
|
||||
// call when `reset` is collected.
|
||||
fail_callback = false;
|
||||
|
||||
return fence_waiter_->AddFence(
|
||||
std::move(fence),
|
||||
[callback, tracked_objects = std::move(tracked_objects_)] {
|
||||
@@ -190,7 +221,6 @@ void CommandEncoderVK::Reset() {
|
||||
tracked_objects_.reset();
|
||||
|
||||
queue_ = nullptr;
|
||||
device_holder_ = {};
|
||||
is_valid_ = false;
|
||||
}
|
||||
|
||||
@@ -251,6 +281,7 @@ std::optional<vk::DescriptorSet> CommandEncoderVK::AllocateDescriptorSet(
|
||||
if (!IsValid()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
return tracked_objects_->GetDescriptorPool().AllocateDescriptorSet(layout);
|
||||
}
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
|
||||
#include "flutter/fml/macros.h"
|
||||
#include "impeller/renderer/backend/vulkan/command_pool_vk.h"
|
||||
#include "impeller/renderer/backend/vulkan/context_vk.h"
|
||||
#include "impeller/renderer/backend/vulkan/descriptor_pool_vk.h"
|
||||
#include "impeller/renderer/backend/vulkan/device_holder.h"
|
||||
#include "impeller/renderer/backend/vulkan/queue_vk.h"
|
||||
@@ -26,14 +27,29 @@ class TextureSourceVK;
|
||||
class TrackedObjectsVK;
|
||||
class FenceWaiterVK;
|
||||
|
||||
class CommandEncoderFactoryVK {
|
||||
public:
|
||||
CommandEncoderFactoryVK(const std::weak_ptr<const ContextVK>& context);
|
||||
|
||||
std::shared_ptr<CommandEncoderVK> Create();
|
||||
|
||||
void SetLabel(const std::string& label);
|
||||
|
||||
private:
|
||||
std::weak_ptr<const ContextVK> context_;
|
||||
std::optional<std::string> label_;
|
||||
|
||||
FML_DISALLOW_COPY_AND_ASSIGN(CommandEncoderFactoryVK);
|
||||
};
|
||||
|
||||
class CommandEncoderVK {
|
||||
public:
|
||||
using SubmitCallback = std::function<void(bool)>;
|
||||
|
||||
// Visible for testing.
|
||||
CommandEncoderVK(const std::weak_ptr<const DeviceHolder>& device_holder,
|
||||
CommandEncoderVK(std::weak_ptr<const DeviceHolder> device_holder,
|
||||
std::shared_ptr<TrackedObjectsVK> tracked_objects,
|
||||
const std::shared_ptr<QueueVK>& queue,
|
||||
const std::shared_ptr<CommandPoolVK>& pool,
|
||||
std::shared_ptr<FenceWaiterVK> fence_waiter);
|
||||
|
||||
~CommandEncoderVK();
|
||||
@@ -69,10 +85,10 @@ class CommandEncoderVK {
|
||||
friend class ContextVK;
|
||||
|
||||
std::weak_ptr<const DeviceHolder> device_holder_;
|
||||
std::shared_ptr<QueueVK> queue_;
|
||||
std::shared_ptr<FenceWaiterVK> fence_waiter_;
|
||||
std::shared_ptr<TrackedObjectsVK> tracked_objects_;
|
||||
bool is_valid_ = false;
|
||||
std::shared_ptr<QueueVK> queue_;
|
||||
const std::shared_ptr<FenceWaiterVK> fence_waiter_;
|
||||
bool is_valid_ = true;
|
||||
|
||||
void Reset();
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include "impeller/renderer/backend/vulkan/compute_pass_vk.h"
|
||||
|
||||
#include "flutter/fml/trace_event.h"
|
||||
#include "impeller/renderer/backend/vulkan/command_buffer_vk.h"
|
||||
#include "impeller/renderer/backend/vulkan/compute_pipeline_vk.h"
|
||||
#include "impeller/renderer/backend/vulkan/sampler_vk.h"
|
||||
#include "impeller/renderer/backend/vulkan/texture_vk.h"
|
||||
@@ -12,8 +13,9 @@
|
||||
namespace impeller {
|
||||
|
||||
ComputePassVK::ComputePassVK(std::weak_ptr<const Context> context,
|
||||
std::weak_ptr<CommandEncoderVK> encoder)
|
||||
: ComputePass(std::move(context)), encoder_(std::move(encoder)) {
|
||||
std::weak_ptr<CommandBufferVK> command_buffer)
|
||||
: ComputePass(std::move(context)),
|
||||
command_buffer_(std::move(command_buffer)) {
|
||||
is_valid_ = true;
|
||||
}
|
||||
|
||||
@@ -203,9 +205,13 @@ bool ComputePassVK::OnEncodeCommands(const Context& context,
|
||||
FML_DCHECK(!grid_size.IsEmpty() && !thread_group_size.IsEmpty());
|
||||
|
||||
const auto& vk_context = ContextVK::Cast(context);
|
||||
auto encoder = encoder_.lock();
|
||||
auto command_buffer = command_buffer_.lock();
|
||||
if (!command_buffer) {
|
||||
VALIDATION_LOG << "Command buffer died before commands could be encoded.";
|
||||
return false;
|
||||
}
|
||||
auto encoder = command_buffer->GetEncoder();
|
||||
if (!encoder) {
|
||||
VALIDATION_LOG << "Command encoder died before commands could be encoded.";
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -10,6 +10,8 @@
|
||||
|
||||
namespace impeller {
|
||||
|
||||
class CommandBufferVK;
|
||||
|
||||
class ComputePassVK final : public ComputePass {
|
||||
public:
|
||||
// |ComputePass|
|
||||
@@ -18,12 +20,12 @@ class ComputePassVK final : public ComputePass {
|
||||
private:
|
||||
friend class CommandBufferVK;
|
||||
|
||||
std::weak_ptr<CommandEncoderVK> encoder_;
|
||||
std::weak_ptr<CommandBufferVK> command_buffer_;
|
||||
std::string label_;
|
||||
bool is_valid_ = false;
|
||||
|
||||
ComputePassVK(std::weak_ptr<const Context> context,
|
||||
std::weak_ptr<CommandEncoderVK> encoder);
|
||||
std::weak_ptr<CommandBufferVK> command_buffer);
|
||||
|
||||
// |ComputePass|
|
||||
bool IsValid() const override;
|
||||
|
||||
@@ -452,13 +452,9 @@ std::shared_ptr<PipelineLibrary> ContextVK::GetPipelineLibrary() const {
|
||||
}
|
||||
|
||||
std::shared_ptr<CommandBuffer> ContextVK::CreateCommandBuffer() const {
|
||||
auto encoder = CreateGraphicsCommandEncoder();
|
||||
if (!encoder) {
|
||||
return nullptr;
|
||||
}
|
||||
return std::shared_ptr<CommandBufferVK>(
|
||||
new CommandBufferVK(shared_from_this(), //
|
||||
std::move(encoder)) //
|
||||
new CommandBufferVK(shared_from_this(), //
|
||||
CreateGraphicsCommandEncoderFactory()) //
|
||||
);
|
||||
}
|
||||
|
||||
@@ -544,22 +540,9 @@ std::shared_ptr<ResourceManagerVK> ContextVK::GetResourceManager() const {
|
||||
return resource_manager_;
|
||||
}
|
||||
|
||||
std::unique_ptr<CommandEncoderVK> ContextVK::CreateGraphicsCommandEncoder()
|
||||
const {
|
||||
auto tls_pool = CommandPoolVK::GetThreadLocal(this);
|
||||
if (!tls_pool) {
|
||||
return nullptr;
|
||||
}
|
||||
auto encoder = std::unique_ptr<CommandEncoderVK>(new CommandEncoderVK(
|
||||
device_holder_, //
|
||||
queues_.graphics_queue, //
|
||||
tls_pool, //
|
||||
fence_waiter_ //
|
||||
));
|
||||
if (!encoder->IsValid()) {
|
||||
return nullptr;
|
||||
}
|
||||
return encoder;
|
||||
std::unique_ptr<CommandEncoderFactoryVK>
|
||||
ContextVK::CreateGraphicsCommandEncoderFactory() const {
|
||||
return std::make_unique<CommandEncoderFactoryVK>(weak_from_this());
|
||||
}
|
||||
|
||||
} // namespace impeller
|
||||
|
||||
@@ -27,6 +27,7 @@ namespace impeller {
|
||||
|
||||
bool HasValidationLayers();
|
||||
|
||||
class CommandEncoderFactoryVK;
|
||||
class CommandEncoderVK;
|
||||
class DebugReportVK;
|
||||
class FenceWaiterVK;
|
||||
@@ -174,7 +175,8 @@ class ContextVK final : public Context,
|
||||
|
||||
void Setup(Settings settings);
|
||||
|
||||
std::unique_ptr<CommandEncoderVK> CreateGraphicsCommandEncoder() const;
|
||||
std::unique_ptr<CommandEncoderFactoryVK> CreateGraphicsCommandEncoderFactory()
|
||||
const;
|
||||
|
||||
FML_DISALLOW_COPY_AND_ASSIGN(ContextVK);
|
||||
};
|
||||
|
||||
@@ -26,11 +26,8 @@ int32_t CountStringViewInstances(const std::vector<std::string>& strings,
|
||||
TEST(PassBindingsCacheTest, bindPipeline) {
|
||||
auto context = CreateMockVulkanContext();
|
||||
PassBindingsCache cache;
|
||||
auto pool = CommandPoolVK::GetThreadLocal(context.get());
|
||||
CommandEncoderVK encoder(context->GetDeviceHolder(),
|
||||
context->GetGraphicsQueue(), pool,
|
||||
context->GetFenceWaiter());
|
||||
auto buffer = encoder.GetCommandBuffer();
|
||||
auto encoder = std::make_unique<CommandEncoderFactoryVK>(context)->Create();
|
||||
auto buffer = encoder->GetCommandBuffer();
|
||||
VkPipeline vk_pipeline = reinterpret_cast<VkPipeline>(0xfeedface);
|
||||
vk::Pipeline pipeline(vk_pipeline);
|
||||
cache.BindPipeline(buffer, vk::PipelineBindPoint::eGraphics, pipeline);
|
||||
@@ -43,11 +40,8 @@ TEST(PassBindingsCacheTest, bindPipeline) {
|
||||
TEST(PassBindingsCacheTest, setStencilReference) {
|
||||
auto context = CreateMockVulkanContext();
|
||||
PassBindingsCache cache;
|
||||
auto pool = CommandPoolVK::GetThreadLocal(context.get());
|
||||
CommandEncoderVK encoder(context->GetDeviceHolder(),
|
||||
context->GetGraphicsQueue(), pool,
|
||||
context->GetFenceWaiter());
|
||||
auto buffer = encoder.GetCommandBuffer();
|
||||
auto encoder = std::make_unique<CommandEncoderFactoryVK>(context)->Create();
|
||||
auto buffer = encoder->GetCommandBuffer();
|
||||
cache.SetStencilReference(
|
||||
buffer, vk::StencilFaceFlagBits::eVkStencilFrontAndBack, 123);
|
||||
cache.SetStencilReference(
|
||||
@@ -61,11 +55,8 @@ TEST(PassBindingsCacheTest, setStencilReference) {
|
||||
TEST(PassBindingsCacheTest, setScissor) {
|
||||
auto context = CreateMockVulkanContext();
|
||||
PassBindingsCache cache;
|
||||
auto pool = CommandPoolVK::GetThreadLocal(context.get());
|
||||
CommandEncoderVK encoder(context->GetDeviceHolder(),
|
||||
context->GetGraphicsQueue(), pool,
|
||||
context->GetFenceWaiter());
|
||||
auto buffer = encoder.GetCommandBuffer();
|
||||
auto encoder = std::make_unique<CommandEncoderFactoryVK>(context)->Create();
|
||||
auto buffer = encoder->GetCommandBuffer();
|
||||
vk::Rect2D scissors;
|
||||
cache.SetScissor(buffer, 0, 1, &scissors);
|
||||
cache.SetScissor(buffer, 0, 1, &scissors);
|
||||
@@ -77,11 +68,8 @@ TEST(PassBindingsCacheTest, setScissor) {
|
||||
TEST(PassBindingsCacheTest, setViewport) {
|
||||
auto context = CreateMockVulkanContext();
|
||||
PassBindingsCache cache;
|
||||
auto pool = CommandPoolVK::GetThreadLocal(context.get());
|
||||
CommandEncoderVK encoder(context->GetDeviceHolder(),
|
||||
context->GetGraphicsQueue(), pool,
|
||||
context->GetFenceWaiter());
|
||||
auto buffer = encoder.GetCommandBuffer();
|
||||
auto encoder = std::make_unique<CommandEncoderFactoryVK>(context)->Create();
|
||||
auto buffer = encoder->GetCommandBuffer();
|
||||
vk::Viewport viewports;
|
||||
cache.SetViewport(buffer, 0, 1, &viewports);
|
||||
cache.SetViewport(buffer, 0, 1, &viewports);
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
#include "impeller/core/formats.h"
|
||||
#include "impeller/core/sampler.h"
|
||||
#include "impeller/core/shader_types.h"
|
||||
#include "impeller/renderer/backend/vulkan/command_buffer_vk.h"
|
||||
#include "impeller/renderer/backend/vulkan/command_encoder_vk.h"
|
||||
#include "impeller/renderer/backend/vulkan/context_vk.h"
|
||||
#include "impeller/renderer/backend/vulkan/device_buffer_vk.h"
|
||||
@@ -137,8 +138,8 @@ SharedHandleVK<vk::RenderPass> RenderPassVK::CreateVKRenderPass(
|
||||
|
||||
RenderPassVK::RenderPassVK(const std::shared_ptr<const Context>& context,
|
||||
const RenderTarget& target,
|
||||
std::weak_ptr<CommandEncoderVK> encoder)
|
||||
: RenderPass(context, target), encoder_(std::move(encoder)) {
|
||||
std::weak_ptr<CommandBufferVK> command_buffer)
|
||||
: RenderPass(context, target), command_buffer_(std::move(command_buffer)) {
|
||||
is_valid_ = true;
|
||||
}
|
||||
|
||||
@@ -555,9 +556,13 @@ bool RenderPassVK::OnEncodeCommands(const Context& context) const {
|
||||
|
||||
const auto& vk_context = ContextVK::Cast(context);
|
||||
|
||||
auto encoder = encoder_.lock();
|
||||
auto command_buffer = command_buffer_.lock();
|
||||
if (!command_buffer) {
|
||||
VALIDATION_LOG << "Command buffer died before commands could be encoded.";
|
||||
return false;
|
||||
}
|
||||
auto encoder = command_buffer->GetEncoder();
|
||||
if (!encoder) {
|
||||
VALIDATION_LOG << "Command encoder died before commands could be encoded.";
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -16,6 +16,8 @@
|
||||
|
||||
namespace impeller {
|
||||
|
||||
class CommandBufferVK;
|
||||
|
||||
class RenderPassVK final : public RenderPass {
|
||||
public:
|
||||
// |RenderPass|
|
||||
@@ -24,14 +26,14 @@ class RenderPassVK final : public RenderPass {
|
||||
private:
|
||||
friend class CommandBufferVK;
|
||||
|
||||
std::weak_ptr<CommandEncoderVK> encoder_;
|
||||
std::weak_ptr<CommandBufferVK> command_buffer_;
|
||||
std::string debug_label_;
|
||||
bool is_valid_ = false;
|
||||
mutable PassBindingsCache pass_bindings_cache_;
|
||||
|
||||
RenderPassVK(const std::shared_ptr<const Context>& context,
|
||||
const RenderTarget& target,
|
||||
std::weak_ptr<CommandEncoderVK> encoder);
|
||||
std::weak_ptr<CommandBufferVK> command_buffer);
|
||||
|
||||
// |RenderPass|
|
||||
bool IsValid() const override;
|
||||
|
||||
@@ -61,7 +61,7 @@ std::shared_ptr<RenderPass> CommandBuffer::CreateRenderPass(
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::shared_ptr<BlitPass> CommandBuffer::CreateBlitPass() const {
|
||||
std::shared_ptr<BlitPass> CommandBuffer::CreateBlitPass() {
|
||||
auto pass = OnCreateBlitPass();
|
||||
if (pass && pass->IsValid()) {
|
||||
pass->SetLabel("BlitPass");
|
||||
@@ -70,7 +70,7 @@ std::shared_ptr<BlitPass> CommandBuffer::CreateBlitPass() const {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::shared_ptr<ComputePass> CommandBuffer::CreateComputePass() const {
|
||||
std::shared_ptr<ComputePass> CommandBuffer::CreateComputePass() {
|
||||
if (!IsValid()) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@@ -104,14 +104,14 @@ class CommandBuffer {
|
||||
///
|
||||
/// @return A valid blit pass or null.
|
||||
///
|
||||
std::shared_ptr<BlitPass> CreateBlitPass() const;
|
||||
std::shared_ptr<BlitPass> CreateBlitPass();
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
/// @brief Create a compute pass to record compute commands into.
|
||||
///
|
||||
/// @return A valid compute pass or null.
|
||||
///
|
||||
std::shared_ptr<ComputePass> CreateComputePass() const;
|
||||
std::shared_ptr<ComputePass> CreateComputePass();
|
||||
|
||||
protected:
|
||||
std::weak_ptr<const Context> context_;
|
||||
@@ -121,13 +121,13 @@ class CommandBuffer {
|
||||
virtual std::shared_ptr<RenderPass> OnCreateRenderPass(
|
||||
RenderTarget render_target) = 0;
|
||||
|
||||
virtual std::shared_ptr<BlitPass> OnCreateBlitPass() const = 0;
|
||||
virtual std::shared_ptr<BlitPass> OnCreateBlitPass() = 0;
|
||||
|
||||
[[nodiscard]] virtual bool OnSubmitCommands(CompletionCallback callback) = 0;
|
||||
|
||||
virtual void OnWaitUntilScheduled() = 0;
|
||||
|
||||
virtual std::shared_ptr<ComputePass> OnCreateComputePass() const = 0;
|
||||
virtual std::shared_ptr<ComputePass> OnCreateComputePass() = 0;
|
||||
|
||||
private:
|
||||
FML_DISALLOW_COPY_AND_ASSIGN(CommandBuffer);
|
||||
|
||||
@@ -76,10 +76,10 @@ class MockCommandBuffer : public CommandBuffer {
|
||||
: CommandBuffer(context) {}
|
||||
MOCK_CONST_METHOD0(IsValid, bool());
|
||||
MOCK_CONST_METHOD1(SetLabel, void(const std::string& label));
|
||||
MOCK_CONST_METHOD0(OnCreateBlitPass, std::shared_ptr<BlitPass>());
|
||||
MOCK_METHOD0(OnCreateBlitPass, std::shared_ptr<BlitPass>());
|
||||
MOCK_METHOD1(OnSubmitCommands, bool(CompletionCallback callback));
|
||||
MOCK_METHOD0(OnWaitUntilScheduled, void());
|
||||
MOCK_CONST_METHOD0(OnCreateComputePass, std::shared_ptr<ComputePass>());
|
||||
MOCK_METHOD0(OnCreateComputePass, std::shared_ptr<ComputePass>());
|
||||
MOCK_METHOD1(OnCreateRenderPass,
|
||||
std::shared_ptr<RenderPass>(RenderTarget render_target));
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user