diff --git a/engine/src/flutter/impeller/entity/entity_pass.cc b/engine/src/flutter/impeller/entity/entity_pass.cc index f938f560e5..3c0eb5f03a 100644 --- a/engine/src/flutter/impeller/entity/entity_pass.cc +++ b/engine/src/flutter/impeller/entity/entity_pass.cc @@ -835,8 +835,9 @@ bool EntityPass::OnRender( TRACE_EVENT0("impeller", "EntityPass::OnRender"); auto context = renderer.GetContext(); - InlinePassContext pass_context( - context, pass_target, GetTotalPassReads(renderer), collapsed_parent_pass); + InlinePassContext pass_context(context, pass_target, + GetTotalPassReads(renderer), GetElementCount(), + collapsed_parent_pass); if (!pass_context.IsValid()) { VALIDATION_LOG << SPrintF("Pass context invalid (Depth=%d)", pass_depth); return false; diff --git a/engine/src/flutter/impeller/entity/inline_pass_context.cc b/engine/src/flutter/impeller/entity/inline_pass_context.cc index 96db4677ce..a1904b743b 100644 --- a/engine/src/flutter/impeller/entity/inline_pass_context.cc +++ b/engine/src/flutter/impeller/entity/inline_pass_context.cc @@ -18,9 +18,11 @@ InlinePassContext::InlinePassContext( std::shared_ptr context, EntityPassTarget& pass_target, uint32_t pass_texture_reads, + uint32_t entity_count, std::optional collapsed_parent_pass) : context_(std::move(context)), pass_target_(pass_target), + entity_count_(entity_count), is_collapsed_(collapsed_parent_pass.has_value()) { if (collapsed_parent_pass.has_value()) { pass_ = collapsed_parent_pass.value().pass; @@ -149,7 +151,10 @@ InlinePassContext::RenderPassResult InlinePassContext::GetRenderPass( VALIDATION_LOG << "Could not create render pass."; return {}; } - + // Commands are fairly large (500B) objects, so re-allocation of the command + // buffer while encoding can add a surprising amount of overhead. We make a + // conservative npot estimate to avoid this case. + pass_->ReserveCommands(Allocation::NextPowerOfTwoSize(entity_count_)); pass_->SetLabel( "EntityPass Render Pass: Depth=" + std::to_string(pass_depth) + " Count=" + std::to_string(pass_count_)); diff --git a/engine/src/flutter/impeller/entity/inline_pass_context.h b/engine/src/flutter/impeller/entity/inline_pass_context.h index cd18d4e744..4da816478a 100644 --- a/engine/src/flutter/impeller/entity/inline_pass_context.h +++ b/engine/src/flutter/impeller/entity/inline_pass_context.h @@ -24,14 +24,21 @@ class InlinePassContext { std::shared_ptr context, EntityPassTarget& pass_target, uint32_t pass_texture_reads, + uint32_t entity_count, std::optional collapsed_parent_pass = std::nullopt); + ~InlinePassContext(); bool IsValid() const; + bool IsActive() const; + std::shared_ptr GetTexture(); + bool EndPass(); + EntityPassTarget& GetPassTarget() const; + uint32_t GetPassCount() const; RenderPassResult GetRenderPass(uint32_t pass_depth); @@ -42,6 +49,7 @@ class InlinePassContext { std::shared_ptr command_buffer_; std::shared_ptr pass_; uint32_t pass_count_ = 0; + uint32_t entity_count_ = 0; // Whether this context is collapsed into a parent entity pass. bool is_collapsed_ = false; diff --git a/engine/src/flutter/impeller/renderer/command.h b/engine/src/flutter/impeller/renderer/command.h index 462f2710c6..dd101d4f66 100644 --- a/engine/src/flutter/impeller/renderer/command.h +++ b/engine/src/flutter/impeller/renderer/command.h @@ -92,10 +92,9 @@ struct Bindings { /// * Specify any stage bindings. /// * (Optional) Specify a debug label. /// -/// Command are very lightweight objects and can be created -/// frequently and on demand. The resources referenced in commands -/// views into buffers managed by other allocators and resource -/// managers. +/// Command can be created frequently and on demand. The resources +/// referenced in commands views into buffers managed by other +/// allocators and resource managers. /// struct Command : public ResourceBinder { //---------------------------------------------------------------------------- diff --git a/engine/src/flutter/impeller/renderer/render_pass.h b/engine/src/flutter/impeller/renderer/render_pass.h index 4b870a542e..f9e9629e3d 100644 --- a/engine/src/flutter/impeller/renderer/render_pass.h +++ b/engine/src/flutter/impeller/renderer/render_pass.h @@ -36,6 +36,13 @@ class RenderPass { void SetLabel(std::string label); + /// @brief Reserve [command_count] commands in the HAL command buffer. + /// + /// Note: this is not the native command buffer. + void ReserveCommands(size_t command_count) { + commands_.reserve(command_count); + } + HostBuffer& GetTransientsBuffer(); //---------------------------------------------------------------------------- diff --git a/engine/src/flutter/impeller/renderer/renderer_unittests.cc b/engine/src/flutter/impeller/renderer/renderer_unittests.cc index 749d611bb3..aa7eb82036 100644 --- a/engine/src/flutter/impeller/renderer/renderer_unittests.cc +++ b/engine/src/flutter/impeller/renderer/renderer_unittests.cc @@ -33,6 +33,7 @@ #include "impeller/renderer/command_buffer.h" #include "impeller/renderer/pipeline_builder.h" #include "impeller/renderer/pipeline_library.h" +#include "impeller/renderer/render_target.h" #include "impeller/renderer/renderer.h" #include "impeller/renderer/sampler_library.h" #include "impeller/renderer/surface.h" @@ -1257,6 +1258,21 @@ TEST_P(RendererTest, StencilMask) { OpenPlaygroundHere(callback); } +TEST_P(RendererTest, CanPreAllocateCommands) { + auto context = GetContext(); + auto cmd_buffer = context->CreateCommandBuffer(); + auto render_target_cache = std::make_shared( + GetContext()->GetResourceAllocator()); + + auto render_target = + RenderTarget::CreateOffscreen(*context, *render_target_cache, {100, 100}); + auto render_pass = cmd_buffer->CreateRenderPass(render_target); + + render_pass->ReserveCommands(100u); + + EXPECT_EQ(render_pass->GetCommands().capacity(), 100u); +} + } // namespace testing } // namespace impeller