Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

[Impeller] Fixed blit command missing tracking and added mock vulkan for tests #41408

Merged
merged 12 commits into from
Apr 25, 2023
Merged
2 changes: 2 additions & 0 deletions ci/licenses_golden/excluded_files
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@
../../../flutter/impeller/golden_tests_harvester/test
../../../flutter/impeller/image/README.md
../../../flutter/impeller/playground
../../../flutter/impeller/renderer/backend/vulkan/blit_command_vk_unittests.cc
../../../flutter/impeller/renderer/backend/vulkan/test
../../../flutter/impeller/renderer/capabilities_unittests.cc
../../../flutter/impeller/renderer/compute_subgroup_unittests.cc
../../../flutter/impeller/renderer/compute_unittests.cc
Expand Down
4 changes: 4 additions & 0 deletions impeller/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ impeller_component("impeller_unittests") {
]
}

if (impeller_enable_vulkan) {
deps += [ "//flutter/impeller/renderer/backend/vulkan:vulkan_unittests" ]
}

if (impeller_enable_compute) {
deps += [ "renderer:compute_tessellation_unittests" ]
}
Expand Down
13 changes: 13 additions & 0 deletions impeller/renderer/backend/vulkan/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,19 @@

import("../../../tools/impeller.gni")

impeller_component("vulkan_unittests") {
testonly = true
sources = [
"blit_command_vk_unittests.cc",
"test/mock_vulkan.cc",
"test/mock_vulkan.h",
]
deps = [
":vulkan",
"//flutter/testing:testing_lib",
]
}

impeller_component("vulkan") {
sources = [
"allocator_vk.cc",
Expand Down
2 changes: 1 addition & 1 deletion impeller/renderer/backend/vulkan/blit_command_vk.cc
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ bool BlitCopyTextureToBufferCommandVK::Encode(CommandEncoderVK& encoder) const {
// cast source and destination to TextureVK
const auto& src = TextureVK::Cast(*source);

if (!encoder.Track(source)) {
if (!encoder.Track(source) || !encoder.Track(destination)) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a tracking I missed until I wrote the test.

return false;
}

Expand Down
65 changes: 65 additions & 0 deletions impeller/renderer/backend/vulkan/blit_command_vk_unittests.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// 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 "flutter/testing/testing.h"
#include "impeller/renderer/backend/vulkan/blit_command_vk.h"
#include "impeller/renderer/backend/vulkan/command_encoder_vk.h"
#include "impeller/renderer/backend/vulkan/test/mock_vulkan.h"

namespace impeller {
namespace testing {

TEST(BlitCommandVkTest, BlitCopyTextureToTextureCommandVK) {
auto context = CreateMockVulkanContext();
auto pool = CommandPoolVK::GetThreadLocal(context.get());
CommandEncoderVK encoder(context->GetDevice(), context->GetGraphicsQueue(),
pool, context->GetFenceWaiter());
BlitCopyTextureToTextureCommandVK cmd;
cmd.source = context->GetResourceAllocator()->CreateTexture({
.size = ISize(100, 100),
});
cmd.destination = context->GetResourceAllocator()->CreateTexture({
.size = ISize(100, 100),
});
bool result = cmd.Encode(encoder);
EXPECT_TRUE(result);
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->GetDevice(), context->GetGraphicsQueue(),
pool, context->GetFenceWaiter());
BlitCopyTextureToBufferCommandVK cmd;
cmd.source = context->GetResourceAllocator()->CreateTexture({
.size = ISize(100, 100),
});
cmd.destination = context->GetResourceAllocator()->CreateBuffer({
.size = 1,
});
bool result = cmd.Encode(encoder);
EXPECT_TRUE(result);
EXPECT_TRUE(encoder.IsTracking(cmd.source));
EXPECT_TRUE(encoder.IsTracking(cmd.destination));
}

TEST(BlitCommandVkTest, BlitGenerateMipmapCommandVK) {
auto context = CreateMockVulkanContext();
auto pool = CommandPoolVK::GetThreadLocal(context.get());
CommandEncoderVK encoder(context->GetDevice(), context->GetGraphicsQueue(),
pool, context->GetFenceWaiter());
BlitGenerateMipmapCommandVK cmd;
cmd.texture = context->GetResourceAllocator()->CreateTexture({
.size = ISize(100, 100),
.mip_count = 2,
});
bool result = cmd.Encode(encoder);
EXPECT_TRUE(result);
EXPECT_TRUE(encoder.IsTracking(cmd.texture));
}

} // namespace testing
} // namespace impeller
32 changes: 32 additions & 0 deletions impeller/renderer/backend/vulkan/command_encoder_vk.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,27 @@ class TrackedObjectsVK {
tracked_buffers_.insert(std::move(buffer));
}

bool IsTracking(const std::shared_ptr<const DeviceBuffer>& buffer) const {
if (!buffer) {
return false;
}
return tracked_buffers_.find(buffer) != tracked_buffers_.end();
}

void Track(std::shared_ptr<const TextureSourceVK> texture) {
if (!texture) {
return;
}
tracked_textures_.insert(std::move(texture));
}

bool IsTracking(const std::shared_ptr<const TextureSourceVK>& texture) const {
if (!texture) {
return false;
}
return tracked_textures_.find(texture) != tracked_textures_.end();
}

vk::CommandBuffer GetCommandBuffer() const { return *buffer_; }

DescriptorPoolVK& GetDescriptorPool() { return desc_pool_; }
Expand Down Expand Up @@ -172,6 +186,14 @@ bool CommandEncoderVK::Track(std::shared_ptr<const DeviceBuffer> buffer) {
return true;
}

bool CommandEncoderVK::IsTracking(
const std::shared_ptr<const DeviceBuffer>& buffer) const {
if (!IsValid()) {
return false;
}
return tracked_objects_->IsTracking(buffer);
}

bool CommandEncoderVK::Track(std::shared_ptr<const TextureSourceVK> texture) {
if (!IsValid()) {
return false;
Expand All @@ -190,6 +212,16 @@ bool CommandEncoderVK::Track(const std::shared_ptr<const Texture>& texture) {
return Track(TextureVK::Cast(*texture).GetTextureSource());
}

bool CommandEncoderVK::IsTracking(
const std::shared_ptr<const Texture>& texture) const {
if (!IsValid()) {
return false;
}
std::shared_ptr<const TextureSourceVK> source =
TextureVK::Cast(*texture).GetTextureSource();
return tracked_objects_->IsTracking(source);
}

std::optional<vk::DescriptorSet> CommandEncoderVK::AllocateDescriptorSet(
const vk::DescriptorSetLayout& layout) {
if (!IsValid()) {
Expand Down
16 changes: 16 additions & 0 deletions impeller/renderer/backend/vulkan/command_encoder_vk.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@

namespace impeller {

namespace testing {
class BlitCommandVkTest_BlitCopyTextureToTextureCommandVK_Test;
class BlitCommandVkTest_BlitCopyTextureToBufferCommandVK_Test;
class BlitCommandVkTest_BlitGenerateMipmapCommandVK_Test;
} // namespace testing

class ContextVK;
class DeviceBuffer;
class Texture;
Expand All @@ -35,8 +41,12 @@ class CommandEncoderVK {

bool Track(std::shared_ptr<const DeviceBuffer> buffer);

bool IsTracking(const std::shared_ptr<const DeviceBuffer>& texture) const;

bool Track(const std::shared_ptr<const Texture>& texture);

bool IsTracking(const std::shared_ptr<const Texture>& texture) const;

bool Track(std::shared_ptr<const TextureSourceVK> texture);

vk::CommandBuffer GetCommandBuffer() const;
Expand All @@ -52,6 +62,12 @@ class CommandEncoderVK {

private:
friend class ContextVK;
friend class ::impeller::testing::
BlitCommandVkTest_BlitCopyTextureToTextureCommandVK_Test;
friend class ::impeller::testing::
BlitCommandVkTest_BlitCopyTextureToBufferCommandVK_Test;
friend class ::impeller::testing::
BlitCommandVkTest_BlitGenerateMipmapCommandVK_Test;
Comment on lines +65 to +70
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should just make the constructor public?


vk::Device device_ = {};
std::shared_ptr<QueueVK> queue_;
Expand Down
Loading