forked from firka/flutter
[Impeller] batch up filter graph command buffers. (flutter/engine#51912)
The filter graph frequently creates and submits command buffers in serial. This has some unnecessary overhead, esp on Vulkan where submitting a command buffer has a non-trivial cost. While previously I had tried to batch up all submissions, doing this in a limited manner in the filter graph should be more straightforward. For gaussians this makes a big difference, as there is a mipmap generation, downsample, then 2 render passes, so we can compress the 4 command buffers into 1. https://github.com/flutter/flutter/issues/142545
This commit is contained in:
@@ -481,8 +481,9 @@ bool ContentContext::IsValid() const {
|
||||
}
|
||||
|
||||
fml::StatusOr<RenderTarget> ContentContext::MakeSubpass(
|
||||
const std::string& label,
|
||||
std::string_view label,
|
||||
ISize texture_size,
|
||||
const std::shared_ptr<CommandBuffer>& command_buffer,
|
||||
const SubpassCallback& subpass_callback,
|
||||
bool msaa_enabled,
|
||||
bool depth_stencil_enabled,
|
||||
@@ -497,20 +498,21 @@ fml::StatusOr<RenderTarget> ContentContext::MakeSubpass(
|
||||
if (context->GetCapabilities()->SupportsOffscreenMSAA() && msaa_enabled) {
|
||||
subpass_target = GetRenderTargetCache()->CreateOffscreenMSAA(
|
||||
*context, texture_size,
|
||||
/*mip_count=*/mip_count, SPrintF("%s Offscreen", label.c_str()),
|
||||
/*mip_count=*/mip_count, SPrintF("%s Offscreen", label.data()),
|
||||
RenderTarget::kDefaultColorAttachmentConfigMSAA, depth_stencil_config);
|
||||
} else {
|
||||
subpass_target = GetRenderTargetCache()->CreateOffscreen(
|
||||
*context, texture_size,
|
||||
/*mip_count=*/mip_count, SPrintF("%s Offscreen", label.c_str()),
|
||||
/*mip_count=*/mip_count, SPrintF("%s Offscreen", label.data()),
|
||||
RenderTarget::kDefaultColorAttachmentConfig, depth_stencil_config);
|
||||
}
|
||||
return MakeSubpass(label, subpass_target, subpass_callback);
|
||||
return MakeSubpass(label, subpass_target, command_buffer, subpass_callback);
|
||||
}
|
||||
|
||||
fml::StatusOr<RenderTarget> ContentContext::MakeSubpass(
|
||||
const std::string& label,
|
||||
std::string_view label,
|
||||
const RenderTarget& subpass_target,
|
||||
const std::shared_ptr<CommandBuffer>& command_buffer,
|
||||
const SubpassCallback& subpass_callback) const {
|
||||
const std::shared_ptr<Context>& context = GetContext();
|
||||
|
||||
@@ -519,17 +521,11 @@ fml::StatusOr<RenderTarget> ContentContext::MakeSubpass(
|
||||
return fml::Status(fml::StatusCode::kUnknown, "");
|
||||
}
|
||||
|
||||
auto sub_command_buffer = context->CreateCommandBuffer();
|
||||
sub_command_buffer->SetLabel(SPrintF("%s CommandBuffer", label.c_str()));
|
||||
if (!sub_command_buffer) {
|
||||
return fml::Status(fml::StatusCode::kUnknown, "");
|
||||
}
|
||||
|
||||
auto sub_renderpass = sub_command_buffer->CreateRenderPass(subpass_target);
|
||||
auto sub_renderpass = command_buffer->CreateRenderPass(subpass_target);
|
||||
if (!sub_renderpass) {
|
||||
return fml::Status(fml::StatusCode::kUnknown, "");
|
||||
}
|
||||
sub_renderpass->SetLabel(SPrintF("%s RenderPass", label.c_str()));
|
||||
sub_renderpass->SetLabel(SPrintF("%s RenderPass", label.data()));
|
||||
|
||||
if (!subpass_callback(*this, *sub_renderpass)) {
|
||||
return fml::Status(fml::StatusCode::kUnknown, "");
|
||||
@@ -543,16 +539,12 @@ fml::StatusOr<RenderTarget> ContentContext::MakeSubpass(
|
||||
subpass_target.GetRenderTargetTexture();
|
||||
if (target_texture->GetMipCount() > 1) {
|
||||
fml::Status mipmap_status =
|
||||
AddMipmapGeneration(sub_command_buffer, context, target_texture);
|
||||
AddMipmapGeneration(command_buffer, context, target_texture);
|
||||
if (!mipmap_status.ok()) {
|
||||
return mipmap_status;
|
||||
}
|
||||
}
|
||||
|
||||
if (!context->GetCommandQueue()->Submit({sub_command_buffer}).ok()) {
|
||||
return fml::Status(fml::StatusCode::kUnknown, "");
|
||||
}
|
||||
|
||||
return subpass_target;
|
||||
}
|
||||
|
||||
|
||||
@@ -783,8 +783,9 @@ class ContentContext {
|
||||
/// @brief Creates a new texture of size `texture_size` and calls
|
||||
/// `subpass_callback` with a `RenderPass` for drawing to the texture.
|
||||
fml::StatusOr<RenderTarget> MakeSubpass(
|
||||
const std::string& label,
|
||||
std::string_view label,
|
||||
ISize texture_size,
|
||||
const std::shared_ptr<CommandBuffer>& command_buffer,
|
||||
const SubpassCallback& subpass_callback,
|
||||
bool msaa_enabled = true,
|
||||
bool depth_stencil_enabled = false,
|
||||
@@ -792,8 +793,9 @@ class ContentContext {
|
||||
|
||||
/// Makes a subpass that will render to `subpass_target`.
|
||||
fml::StatusOr<RenderTarget> MakeSubpass(
|
||||
const std::string& label,
|
||||
std::string_view label,
|
||||
const RenderTarget& subpass_target,
|
||||
const std::shared_ptr<CommandBuffer>& command_buffer,
|
||||
const SubpassCallback& subpass_callback) const;
|
||||
|
||||
const std::shared_ptr<LazyGlyphAtlas>& GetLazyGlyphAtlas() const {
|
||||
|
||||
@@ -76,6 +76,12 @@ std::optional<Snapshot> Contents::RenderToSnapshot(
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::shared_ptr<CommandBuffer> command_buffer =
|
||||
renderer.GetContext()->CreateCommandBuffer();
|
||||
if (!command_buffer) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
// Pad Contents snapshots with 1 pixel borders to ensure correct sampling
|
||||
// behavior. Not doing so results in a coverage leak for filters that support
|
||||
// customizing the input sampling mode. Snapshots of contents should be
|
||||
@@ -91,7 +97,7 @@ std::optional<Snapshot> Contents::RenderToSnapshot(
|
||||
|
||||
ISize subpass_size = ISize::Ceil(coverage->GetSize());
|
||||
fml::StatusOr<RenderTarget> render_target = renderer.MakeSubpass(
|
||||
label, subpass_size,
|
||||
label, subpass_size, command_buffer,
|
||||
[&contents = *this, &entity, &coverage](const ContentContext& renderer,
|
||||
RenderPass& pass) -> bool {
|
||||
Entity sub_entity;
|
||||
@@ -107,6 +113,12 @@ std::optional<Snapshot> Contents::RenderToSnapshot(
|
||||
if (!render_target.ok()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
if (!renderer.GetContext()
|
||||
->GetCommandQueue()
|
||||
->Submit(/*buffers=*/{std::move(command_buffer)})
|
||||
.ok()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
auto snapshot = Snapshot{
|
||||
.texture = render_target.value().GetRenderTargetTexture(),
|
||||
|
||||
@@ -223,11 +223,23 @@ static std::optional<Entity> AdvancedBlend(
|
||||
return pass.Draw().ok();
|
||||
};
|
||||
|
||||
std::shared_ptr<CommandBuffer> command_buffer =
|
||||
renderer.GetContext()->CreateCommandBuffer();
|
||||
if (!command_buffer) {
|
||||
return std::nullopt;
|
||||
}
|
||||
fml::StatusOr<RenderTarget> render_target = renderer.MakeSubpass(
|
||||
"Advanced Blend Filter", ISize(subpass_coverage.GetSize()), callback);
|
||||
"Advanced Blend Filter", ISize(subpass_coverage.GetSize()),
|
||||
command_buffer, callback);
|
||||
if (!render_target.ok()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
if (!renderer.GetContext()
|
||||
->GetCommandQueue()
|
||||
->Submit(/*buffers=*/{std::move(command_buffer)})
|
||||
.ok()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
return Entity::FromSnapshot(
|
||||
Snapshot{
|
||||
@@ -525,13 +537,27 @@ static std::optional<Entity> PipelineBlend(
|
||||
return true;
|
||||
};
|
||||
|
||||
std::shared_ptr<CommandBuffer> command_buffer =
|
||||
renderer.GetContext()->CreateCommandBuffer();
|
||||
if (!command_buffer) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
fml::StatusOr<RenderTarget> render_target = renderer.MakeSubpass(
|
||||
"Pipeline Blend Filter", ISize(subpass_coverage.GetSize()), callback);
|
||||
"Pipeline Blend Filter", ISize(subpass_coverage.GetSize()),
|
||||
command_buffer, callback);
|
||||
|
||||
if (!render_target.ok()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
if (!renderer.GetContext()
|
||||
->GetCommandQueue()
|
||||
->Submit(/*buffers=*/{std::move(command_buffer)})
|
||||
.ok()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
return Entity::FromSnapshot(
|
||||
Snapshot{
|
||||
.texture = render_target.value().GetRenderTargetTexture(),
|
||||
|
||||
@@ -77,6 +77,7 @@ void SetTileMode(SamplerDescriptor* descriptor,
|
||||
/// transparent gutter required for the blur halo.
|
||||
fml::StatusOr<RenderTarget> MakeDownsampleSubpass(
|
||||
const ContentContext& renderer,
|
||||
const std::shared_ptr<CommandBuffer>& command_buffer,
|
||||
std::shared_ptr<Texture> input_texture,
|
||||
const SamplerDescriptor& sampler_descriptor,
|
||||
const Quad& uvs,
|
||||
@@ -118,12 +119,13 @@ fml::StatusOr<RenderTarget> MakeDownsampleSubpass(
|
||||
return pass.Draw().ok();
|
||||
};
|
||||
fml::StatusOr<RenderTarget> render_target = renderer.MakeSubpass(
|
||||
"Gaussian Blur Filter", subpass_size, subpass_callback);
|
||||
"Gaussian Blur Filter", subpass_size, command_buffer, subpass_callback);
|
||||
return render_target;
|
||||
}
|
||||
|
||||
fml::StatusOr<RenderTarget> MakeBlurSubpass(
|
||||
const ContentContext& renderer,
|
||||
const std::shared_ptr<CommandBuffer>& command_buffer,
|
||||
const RenderTarget& input_pass,
|
||||
const SamplerDescriptor& sampler_descriptor,
|
||||
Entity::TileMode tile_mode,
|
||||
@@ -184,10 +186,11 @@ fml::StatusOr<RenderTarget> MakeBlurSubpass(
|
||||
};
|
||||
if (destination_target.has_value()) {
|
||||
return renderer.MakeSubpass("Gaussian Blur Filter",
|
||||
destination_target.value(), subpass_callback);
|
||||
destination_target.value(), command_buffer,
|
||||
subpass_callback);
|
||||
} else {
|
||||
return renderer.MakeSubpass("Gaussian Blur Filter", subpass_size,
|
||||
subpass_callback);
|
||||
command_buffer, subpass_callback);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -459,9 +462,15 @@ std::optional<Entity> GaussianBlurFilterContents::RenderFilter(
|
||||
Quad uvs = CalculateUVs(inputs[0], entity, source_rect_padded,
|
||||
input_snapshot->texture->GetSize());
|
||||
|
||||
std::shared_ptr<CommandBuffer> command_buffer =
|
||||
renderer.GetContext()->CreateCommandBuffer();
|
||||
if (!command_buffer) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
fml::StatusOr<RenderTarget> pass1_out = MakeDownsampleSubpass(
|
||||
renderer, input_snapshot->texture, input_snapshot->sampler_descriptor,
|
||||
uvs, subpass_size, tile_mode_);
|
||||
renderer, command_buffer, input_snapshot->texture,
|
||||
input_snapshot->sampler_descriptor, uvs, subpass_size, tile_mode_);
|
||||
|
||||
if (!pass1_out.ok()) {
|
||||
return std::nullopt;
|
||||
@@ -494,7 +503,7 @@ std::optional<Entity> GaussianBlurFilterContents::RenderFilter(
|
||||
}
|
||||
|
||||
fml::StatusOr<RenderTarget> pass2_out = MakeBlurSubpass(
|
||||
renderer, /*input_pass=*/pass1_out.value(),
|
||||
renderer, command_buffer, /*input_pass=*/pass1_out.value(),
|
||||
input_snapshot->sampler_descriptor, tile_mode_,
|
||||
BlurParameters{
|
||||
.blur_uv_offset = Point(0.0, pass1_pixel_size.y),
|
||||
@@ -515,7 +524,7 @@ std::optional<Entity> GaussianBlurFilterContents::RenderFilter(
|
||||
: std::optional<RenderTarget>(std::nullopt);
|
||||
|
||||
fml::StatusOr<RenderTarget> pass3_out = MakeBlurSubpass(
|
||||
renderer, /*input_pass=*/pass2_out.value(),
|
||||
renderer, command_buffer, /*input_pass=*/pass2_out.value(),
|
||||
input_snapshot->sampler_descriptor, tile_mode_,
|
||||
BlurParameters{
|
||||
.blur_uv_offset = Point(pass1_pixel_size.x, 0.0),
|
||||
@@ -529,6 +538,13 @@ std::optional<Entity> GaussianBlurFilterContents::RenderFilter(
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
if (!renderer.GetContext()
|
||||
->GetCommandQueue()
|
||||
->Submit(/*buffers=*/{command_buffer})
|
||||
.ok()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
// The ping-pong approach requires that each render pass output has the same
|
||||
// size.
|
||||
FML_DCHECK((pass1_out.value().GetRenderTargetSize() ==
|
||||
|
||||
@@ -74,9 +74,24 @@ class GaussianBlurFilterContentsTest : public EntityPlayground {
|
||||
public:
|
||||
/// Create a texture that has been cleared to transparent black.
|
||||
std::shared_ptr<Texture> MakeTexture(ISize size) {
|
||||
std::shared_ptr<CommandBuffer> command_buffer =
|
||||
GetContentContext()->GetContext()->CreateCommandBuffer();
|
||||
if (!command_buffer) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto render_target = GetContentContext()->MakeSubpass(
|
||||
"Clear Subpass", size,
|
||||
"Clear Subpass", size, command_buffer,
|
||||
[](const ContentContext&, RenderPass&) { return true; });
|
||||
|
||||
if (!GetContentContext()
|
||||
->GetContext()
|
||||
->GetCommandQueue()
|
||||
->Submit(/*buffers=*/{command_buffer})
|
||||
.ok()) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (render_target.ok()) {
|
||||
return render_target.value().GetRenderTargetTexture();
|
||||
}
|
||||
|
||||
@@ -134,12 +134,24 @@ std::optional<Entity> DirectionalMorphologyFilterContents::RenderFilter(
|
||||
|
||||
return pass.Draw().ok();
|
||||
};
|
||||
std::shared_ptr<CommandBuffer> command_buffer =
|
||||
renderer.GetContext()->CreateCommandBuffer();
|
||||
if (command_buffer == nullptr) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
fml::StatusOr<RenderTarget> render_target = renderer.MakeSubpass(
|
||||
"Directional Morphology Filter", ISize(coverage.GetSize()), callback);
|
||||
fml::StatusOr<RenderTarget> render_target =
|
||||
renderer.MakeSubpass("Directional Morphology Filter",
|
||||
ISize(coverage.GetSize()), command_buffer, callback);
|
||||
if (!render_target.ok()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
if (!renderer.GetContext()
|
||||
->GetCommandQueue()
|
||||
->Submit(/*buffers=*/{std::move(command_buffer)})
|
||||
.ok()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
SamplerDescriptor sampler_desc;
|
||||
sampler_desc.min_filter = MinMagFilter::kLinear;
|
||||
|
||||
Reference in New Issue
Block a user