Skip to content

Commit 245b522

Browse files
committed
[Impeller] Evict compute pipelines when their shader is reloaded
RemovePipelinesWithEntryPoint dereferenced the entry point for the removed function's stage on every cached render pipeline, which is null when a compute function is removed. Skip pipelines without an entry point for that stage on Metal, Vulkan and OpenGL ES, and evict the cached compute pipelines that use the function on Metal and Vulkan. Fixes flutter#193872.
1 parent 0b9426b commit 245b522

4 files changed

Lines changed: 79 additions & 5 deletions

File tree

‎engine/src/flutter/impeller/renderer/backend/gles/pipeline_library_gles.cc‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,11 @@ void PipelineLibraryGLES::RemovePipelinesWithEntryPoint(
459459
PipelineMap::iterator it = pipelines_.begin();
460460
while (it != pipelines_.end()) {
461461
const PipelineDescriptor& desc = it->first;
462-
if (desc.GetEntrypointForStage(function->GetStage())->IsEqual(*function)) {
462+
// Pipelines without an entry point for the function's stage, such as every
463+
// render pipeline when a compute function is removed, never use it.
464+
const std::shared_ptr<const ShaderFunction> entrypoint =
465+
desc.GetEntrypointForStage(function->GetStage());
466+
if (entrypoint && entrypoint->IsEqual(*function)) {
463467
const std::shared_ptr<const ShaderFunction>& vert_function =
464468
desc.GetEntrypointForStage(ShaderStage::kVertex);
465469
const std::shared_ptr<const ShaderFunction>& frag_function =

‎engine/src/flutter/impeller/renderer/backend/metal/pipeline_library_mtl.mm‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,9 +302,18 @@ new ComputePipelineMTL(weak_this,
302302
void PipelineLibraryMTL::RemovePipelinesWithEntryPoint(
303303
std::shared_ptr<const ShaderFunction> function) {
304304
Lock lock(pipelines_mutex_);
305+
// Skip pipelines without an entry point for the function's stage: render
306+
// pipelines have none for the compute stage. Compute pipelines are cached
307+
// separately.
305308
fml::erase_if(pipelines_, [&](auto item) {
306-
return item->first.GetEntrypointForStage(function->GetStage())
307-
->IsEqual(*function);
309+
const std::shared_ptr<const ShaderFunction> entrypoint =
310+
item->first.GetEntrypointForStage(function->GetStage());
311+
return entrypoint && entrypoint->IsEqual(*function);
312+
});
313+
fml::erase_if(compute_pipelines_, [&](auto item) {
314+
const std::shared_ptr<const ShaderFunction> entrypoint =
315+
item->first.GetStageEntrypoint();
316+
return entrypoint && entrypoint->IsEqual(*function);
308317
});
309318
}
310319

‎engine/src/flutter/impeller/renderer/backend/vulkan/pipeline_library_vk.cc‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,18 @@ void PipelineLibraryVK::RemovePipelinesWithEntryPoint(
262262
std::shared_ptr<const ShaderFunction> function) {
263263
Lock lock(pipelines_mutex_);
264264

265+
// Skip pipelines without an entry point for the function's stage: render
266+
// pipelines have none for the compute stage. Compute pipelines are cached
267+
// separately.
265268
fml::erase_if(pipelines_, [&](auto item) {
266-
return item->first.GetEntrypointForStage(function->GetStage())
267-
->IsEqual(*function);
269+
const std::shared_ptr<const ShaderFunction> entrypoint =
270+
item->first.GetEntrypointForStage(function->GetStage());
271+
return entrypoint && entrypoint->IsEqual(*function);
272+
});
273+
fml::erase_if(compute_pipelines_, [&](auto item) {
274+
const std::shared_ptr<const ShaderFunction> entrypoint =
275+
item->first.GetStageEntrypoint();
276+
return entrypoint && entrypoint->IsEqual(*function);
268277
});
269278
}
270279

‎engine/src/flutter/impeller/renderer/compute_unittests.cc‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -948,5 +948,57 @@ TEST_P(ComputeTest, ComputePassReadsTextureWrittenByRenderPass) {
948948
latch.Wait();
949949
}
950950

951+
TEST_P(ComputeTest, RemovingComputeEntryPointEvictsOnlyItsPipelines) {
952+
using CS = IncrementTestComputeShader;
953+
using OtherCS = ThreadgroupSizingTestComputeShader;
954+
using VS = ComputedVerticesTestVertexShader;
955+
using FS = ComputedVerticesTestFragmentShader;
956+
auto context = GetContext();
957+
ASSERT_TRUE(context);
958+
ASSERT_TRUE(context->GetCapabilities()->SupportsCompute());
959+
auto library = context->GetPipelineLibrary();
960+
961+
auto render_desc =
962+
PipelineBuilder<VS, FS>::MakeDefaultPipelineDescriptor(*context);
963+
ASSERT_TRUE(render_desc.has_value());
964+
render_desc->SetSampleCount(SampleCount::kCount1);
965+
render_desc->ClearStencilAttachments();
966+
render_desc->ClearDepthAttachment();
967+
auto render_pipeline = library->GetPipeline(render_desc).Get();
968+
ASSERT_TRUE(render_pipeline);
969+
970+
auto compute_desc =
971+
ComputePipelineBuilder<CS>::MakeDefaultPipelineDescriptor(*context);
972+
ASSERT_TRUE(compute_desc.has_value());
973+
auto compute_pipeline = library->GetPipeline(compute_desc).Get();
974+
ASSERT_TRUE(compute_pipeline);
975+
976+
auto other_desc =
977+
ComputePipelineBuilder<OtherCS>::MakeDefaultPipelineDescriptor(*context);
978+
ASSERT_TRUE(other_desc.has_value());
979+
auto other_pipeline = library->GetPipeline(other_desc).Get();
980+
ASSERT_TRUE(other_pipeline);
981+
982+
// Removing a compute function, as a hot reload does, must skip the cached
983+
// render pipelines, which have no compute entry point, and evict only the
984+
// compute pipelines that use that function.
985+
library->RemovePipelinesWithEntryPoint(compute_desc->GetStageEntrypoint());
986+
987+
EXPECT_TRUE(library->HasPipeline(*render_desc));
988+
EXPECT_EQ(library->GetPipeline(render_desc).Get(), render_pipeline);
989+
EXPECT_EQ(library->GetPipeline(other_desc).Get(), other_pipeline);
990+
auto recreated_pipeline = library->GetPipeline(compute_desc).Get();
991+
ASSERT_TRUE(recreated_pipeline);
992+
EXPECT_NE(recreated_pipeline, compute_pipeline);
993+
994+
// Removing a vertex function evicts its render pipelines and leaves the
995+
// compute pipelines.
996+
library->RemovePipelinesWithEntryPoint(
997+
render_desc->GetEntrypointForStage(ShaderStage::kVertex));
998+
EXPECT_FALSE(library->HasPipeline(*render_desc));
999+
EXPECT_EQ(library->GetPipeline(compute_desc).Get(), recreated_pipeline);
1000+
EXPECT_EQ(library->GetPipeline(other_desc).Get(), other_pipeline);
1001+
}
1002+
9511003
} // namespace testing
9521004
} // namespace impeller

0 commit comments

Comments
 (0)