Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,7 @@ targets:
# Avoid using a Mac orchestrator to save ~5 minutes of Mac host time.
- name: Linux mac_clangd
recipe: engine_v2/engine_v2
timeout: 90
# Do not remove(https://github.com/flutter/flutter/issues/144644)
# Scheduler will fail to get the platform
drone_dimensions:
Expand Down
10 changes: 6 additions & 4 deletions impeller/renderer/backend/vulkan/command_buffer_vk.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,10 @@ namespace impeller {
CommandBufferVK::CommandBufferVK(
std::weak_ptr<const Context> context,
std::weak_ptr<const DeviceHolderVK> device_holder,
std::shared_ptr<TrackedObjectsVK> tracked_objects,
std::shared_ptr<FenceWaiterVK> fence_waiter)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

fence waiter is unused

std::shared_ptr<TrackedObjectsVK> tracked_objects)
: CommandBuffer(std::move(context)),
device_holder_(std::move(device_holder)),
tracked_objects_(std::move(tracked_objects)),
fence_waiter_(std::move(fence_waiter)) {}
tracked_objects_(std::move(tracked_objects)) {}

CommandBufferVK::~CommandBufferVK() = default;

Expand Down Expand Up @@ -212,4 +210,8 @@ void CommandBufferVK::InsertDebugMarker(std::string_view label) const {
}
}

DescriptorPoolVK& CommandBufferVK::GetDescriptorPool() const {
return tracked_objects_->GetDescriptorPool();
}

} // namespace impeller
8 changes: 5 additions & 3 deletions impeller/renderer/backend/vulkan/command_buffer_vk.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "fml/status_or.h"
#include "impeller/base/backend_cast.h"
#include "impeller/renderer/backend/vulkan/command_queue_vk.h"
#include "impeller/renderer/backend/vulkan/descriptor_pool_vk.h"
#include "impeller/renderer/backend/vulkan/device_holder_vk.h"
#include "impeller/renderer/backend/vulkan/texture_source_vk.h"
#include "impeller/renderer/backend/vulkan/tracked_objects_vk.h"
Expand Down Expand Up @@ -81,18 +82,19 @@ class CommandBufferVK final
// Visible for testing.
bool IsTracking(const std::shared_ptr<const Texture>& texture) const;

// Visible for testing.
DescriptorPoolVK& GetDescriptorPool() const;

private:
friend class ContextVK;
friend class CommandQueueVK;

std::weak_ptr<const DeviceHolderVK> device_holder_;
std::shared_ptr<TrackedObjectsVK> tracked_objects_;
std::shared_ptr<FenceWaiterVK> fence_waiter_;

CommandBufferVK(std::weak_ptr<const Context> context,
std::weak_ptr<const DeviceHolderVK> device_holder,
std::shared_ptr<TrackedObjectsVK> tracked_objects,
std::shared_ptr<FenceWaiterVK> fence_waiter);
std::shared_ptr<TrackedObjectsVK> tracked_objects);

// |CommandBuffer|
void SetLabel(std::string_view label) const override;
Expand Down
28 changes: 23 additions & 5 deletions impeller/renderer/backend/vulkan/context_vk.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
// found in the LICENSE file.

#include "impeller/renderer/backend/vulkan/context_vk.h"
#include <thread>
#include <unordered_map>

#include "fml/concurrent_message_loop.h"
#include "impeller/renderer/backend/vulkan/command_queue_vk.h"
#include "impeller/renderer/backend/vulkan/descriptor_pool_vk.h"
#include "impeller/renderer/backend/vulkan/render_pass_builder_vk.h"
#include "impeller/renderer/render_target.h"

Expand All @@ -30,6 +33,7 @@
#include "impeller/renderer/backend/vulkan/command_pool_vk.h"
#include "impeller/renderer/backend/vulkan/command_queue_vk.h"
#include "impeller/renderer/backend/vulkan/debug_report_vk.h"
#include "impeller/renderer/backend/vulkan/descriptor_pool_vk.h"
#include "impeller/renderer/backend/vulkan/fence_waiter_vk.h"
#include "impeller/renderer/backend/vulkan/gpu_tracer_vk.h"
#include "impeller/renderer/backend/vulkan/resource_manager_vk.h"
Expand Down Expand Up @@ -497,8 +501,22 @@ std::shared_ptr<CommandBuffer> ContextVK::CreateCommandBuffer() const {
return nullptr;
}

// look up a cached descriptor pool for the current frame and reuse it
Copy link
Member

Choose a reason for hiding this comment

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

In cases where we operate on a thread pool where threads are being created and collected, thread ID reuse may cause us issues. This is just a hypothetical though. Our existing pools don't work that way. Not sure we should care.

Copy link
Member

Choose a reason for hiding this comment

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

Thinking about this some more, perhaps we should steal NSNotificationCenter from Foundation. The thread locals can add listeners (by object) to certain lifecycle events where they can collect resources when the context shuts down in addition to collection those same resources when the thread itself shuts down. In any case, what you have here should work well for our use.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can you file a bug for this and describe how it would work?

// if it exists, otherwise create a new pool.
DescriptorPoolMap::iterator current_pool =
cached_descriptor_pool_.find(std::this_thread::get_id());
std::shared_ptr<DescriptorPoolVK> descriptor_pool;
if (current_pool == cached_descriptor_pool_.end()) {
descriptor_pool =
(cached_descriptor_pool_[std::this_thread::get_id()] =
std::make_shared<DescriptorPoolVK>(weak_from_this()));
} else {
descriptor_pool = current_pool->second;
}

auto tracked_objects = std::make_shared<TrackedObjectsVK>(
weak_from_this(), std::move(tls_pool), GetGPUTracer()->CreateGPUProbe());
weak_from_this(), std::move(tls_pool), std::move(descriptor_pool),
GetGPUTracer()->CreateGPUProbe());
auto queue = GetGraphicsQueue();

if (!tracked_objects || !tracked_objects->IsValid() || !queue) {
Expand All @@ -517,10 +535,9 @@ std::shared_ptr<CommandBuffer> ContextVK::CreateCommandBuffer() const {
tracked_objects->GetCommandBuffer());

return std::shared_ptr<CommandBufferVK>(new CommandBufferVK(
shared_from_this(), //
GetDeviceHolder(), //
std::move(tracked_objects), //
GetFenceWaiter() //
shared_from_this(), //
GetDeviceHolder(), //
std::move(tracked_objects) //
));
}

Expand Down Expand Up @@ -651,6 +668,7 @@ void ContextVK::InitializeCommonlyUsedShadersIfNeeded() const {
}

void ContextVK::DisposeThreadLocalCachedResources() {
cached_descriptor_pool_.erase(std::this_thread::get_id());
command_pool_recycler_->Dispose();
}

Expand Down
9 changes: 7 additions & 2 deletions impeller/renderer/backend/vulkan/context_vk.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include "flutter/fml/concurrent_message_loop.h"
#include "flutter/fml/mapping.h"
#include "flutter/fml/unique_fd.h"
#include "fml/thread.h"
#include "impeller/base/backend_cast.h"
#include "impeller/base/strings.h"
#include "impeller/core/formats.h"
Expand Down Expand Up @@ -40,6 +39,7 @@ class SurfaceContextVK;
class GPUTracerVK;
class DescriptorPoolRecyclerVK;
class CommandQueueVK;
class DescriptorPoolVK;

class ContextVK final : public Context,
public BackendCast<ContextVK, Context>,
Expand Down Expand Up @@ -224,12 +224,17 @@ class ContextVK final : public Context,
std::shared_ptr<const Capabilities> device_capabilities_;
std::shared_ptr<FenceWaiterVK> fence_waiter_;
std::shared_ptr<ResourceManagerVK> resource_manager_;
std::shared_ptr<DescriptorPoolRecyclerVK> descriptor_pool_recycler_;
std::shared_ptr<CommandPoolRecyclerVK> command_pool_recycler_;
std::string device_name_;
std::shared_ptr<fml::ConcurrentMessageLoop> raster_message_loop_;
std::shared_ptr<GPUTracerVK> gpu_tracer_;
std::shared_ptr<DescriptorPoolRecyclerVK> descriptor_pool_recycler_;
std::shared_ptr<CommandQueue> command_queue_vk_;

using DescriptorPoolMap =
std::unordered_map<std::thread::id, std::shared_ptr<DescriptorPoolVK>>;

mutable DescriptorPoolMap cached_descriptor_pool_;
bool should_disable_surface_control_ = false;
bool should_batch_cmd_buffers_ = false;
std::vector<std::shared_ptr<CommandBuffer>> pending_command_buffers_;
Expand Down
23 changes: 23 additions & 0 deletions impeller/renderer/backend/vulkan/descriptor_pool_vk_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "flutter/testing/testing.h" // IWYU pragma: keep.
#include "fml/closure.h"
#include "fml/synchronization/waitable_event.h"
#include "impeller/renderer/backend/vulkan/command_buffer_vk.h"
#include "impeller/renderer/backend/vulkan/descriptor_pool_vk.h"
#include "impeller/renderer/backend/vulkan/resource_manager_vk.h"
#include "impeller/renderer/backend/vulkan/test/mock_vulkan.h"
Expand Down Expand Up @@ -123,5 +124,27 @@ TEST(DescriptorPoolRecyclerVKTest, ReclaimDropsDescriptorPoolIfSizeIsExceeded) {
context->Shutdown();
}

TEST(DescriptorPoolRecyclerVKTest, MultipleCommandBuffersShareDescriptorPool) {
auto const context = MockVulkanContextBuilder().Build();

auto cmd_buffer_1 = context->CreateCommandBuffer();
auto cmd_buffer_2 = context->CreateCommandBuffer();

CommandBufferVK& vk_1 = CommandBufferVK::Cast(*cmd_buffer_1);
CommandBufferVK& vk_2 = CommandBufferVK::Cast(*cmd_buffer_2);

EXPECT_EQ(&vk_1.GetDescriptorPool(), &vk_2.GetDescriptorPool());

// Resetting resources creates a new pool.
context->DisposeThreadLocalCachedResources();

auto cmd_buffer_3 = context->CreateCommandBuffer();
CommandBufferVK& vk_3 = CommandBufferVK::Cast(*cmd_buffer_3);

EXPECT_NE(&vk_1.GetDescriptorPool(), &vk_3.GetDescriptorPool());

context->Shutdown();
}

} // namespace testing
} // namespace impeller
2 changes: 1 addition & 1 deletion impeller/renderer/backend/vulkan/surface_context_vk.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ std::unique_ptr<Surface> SurfaceContextVK::AcquireNextSurface() {
impeller::PipelineLibraryVK::Cast(*pipeline_library)
.DidAcquireSurfaceFrame();
}
parent_->GetCommandPoolRecycler()->Dispose();
parent_->DisposeThreadLocalCachedResources();
parent_->GetResourceAllocator()->DebugTraceMemoryStatistics();
return surface;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "flutter/testing/testing.h" // IWYU pragma: keep
#include "gtest/gtest.h"
#include "impeller/renderer/backend/vulkan/command_pool_vk.h"
#include "impeller/renderer/backend/vulkan/test/mock_vulkan.h"
#include "vulkan/vulkan_enums.hpp"

Expand Down
6 changes: 4 additions & 2 deletions impeller/renderer/backend/vulkan/tracked_objects_vk.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@

#include "impeller/renderer/backend/vulkan/tracked_objects_vk.h"

#include "impeller/renderer/backend/vulkan/command_pool_vk.h"
#include "impeller/renderer/backend/vulkan/gpu_tracer_vk.h"

namespace impeller {

TrackedObjectsVK::TrackedObjectsVK(
const std::weak_ptr<const ContextVK>& context,
const std::shared_ptr<CommandPoolVK>& pool,
std::shared_ptr<DescriptorPoolVK> descriptor_pool,
std::unique_ptr<GPUProbe> probe)
: desc_pool_(context), probe_(std::move(probe)) {
: desc_pool_(std::move(descriptor_pool)), probe_(std::move(probe)) {
if (!pool) {
return;
}
Expand Down Expand Up @@ -78,7 +80,7 @@ vk::CommandBuffer TrackedObjectsVK::GetCommandBuffer() const {
}

DescriptorPoolVK& TrackedObjectsVK::GetDescriptorPool() {
return desc_pool_;
return *desc_pool_;
}

GPUProbe& TrackedObjectsVK::GetGPUProbe() const {
Expand Down
3 changes: 2 additions & 1 deletion impeller/renderer/backend/vulkan/tracked_objects_vk.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class TrackedObjectsVK {
public:
explicit TrackedObjectsVK(const std::weak_ptr<const ContextVK>& context,
const std::shared_ptr<CommandPoolVK>& pool,
std::shared_ptr<DescriptorPoolVK> descriptor_pool,
std::unique_ptr<GPUProbe> probe);

~TrackedObjectsVK();
Expand All @@ -43,7 +44,7 @@ class TrackedObjectsVK {
GPUProbe& GetGPUProbe() const;

private:
DescriptorPoolVK desc_pool_;
std::shared_ptr<DescriptorPoolVK> desc_pool_;
// `shared_ptr` since command buffers have a link to the command pool.
std::shared_ptr<CommandPoolVK> pool_;
vk::UniqueCommandBuffer buffer_;
Expand Down