Skip to content

Commit 7fe7249

Browse files
committed
[Impeller] Synchronize compute dispatches and passes automatically
The Metal compute encoder is serial, so each dispatch sees the writes of the one before it. On Vulkan a barrier orders each dispatch after the previous one in the pass, and the barrier at the end of a compute pass now covers every later consumer: indirect, index and vertex reads, uniform and shader access in any stage, transfers and host reads. The outgoing dependency of a render pass and the shader-read transitions after blits include the compute stage. The texture-to-buffer copy now waits for earlier writes before reading, and blit-to-texture copies finish before their shader-read transition. AddBufferMemoryBarrier and AddTextureMemoryBarrier remain as a manual override. Fixes flutter#193869.
1 parent 98c2f80 commit 7fe7249

12 files changed

Lines changed: 398 additions & 61 deletions

‎engine/src/flutter/impeller/renderer/BUILD.gn‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,14 @@ if (impeller_enable_compute) {
2121

2222
shaders = [
2323
"compute_3d_test.comp",
24+
"computed_vertices_test.frag",
25+
"computed_vertices_test.vert",
2426
"increment_test.comp",
2527
"oversized_workgroup_test.comp",
2628
"prefix_sum_test.comp",
29+
"texture_reader_test.comp",
2730
"threadgroup_sizing_test.comp",
31+
"vertex_writer_test.comp",
2832
]
2933
}
3034
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@
3232
if (!buffer_) {
3333
return;
3434
}
35+
// A serial encoder runs each dispatch after the previous one finishes and
36+
// makes its writes visible, so dependent dispatches need no manual barrier.
3537
encoder_ = [buffer_ computeCommandEncoderWithDispatchType:
36-
MTLDispatchType::MTLDispatchTypeConcurrent];
38+
MTLDispatchType::MTLDispatchTypeSerial];
3739
if (!encoder_) {
3840
return;
3941
}

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

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,16 @@ bool BlitPassVK::OnCopyTextureToTextureCommand(
134134
return true;
135135
}
136136

137+
// The copy must complete before a later render or compute pass samples the
138+
// destination.
137139
BarrierVK barrier;
138140
barrier.cmd_buffer = cmd_buffer;
139141
barrier.new_layout = vk::ImageLayout::eShaderReadOnlyOptimal;
140-
barrier.src_access = {};
141-
barrier.src_stage = vk::PipelineStageFlagBits::eTopOfPipe;
142+
barrier.src_access = vk::AccessFlagBits::eTransferWrite;
143+
barrier.src_stage = vk::PipelineStageFlagBits::eTransfer;
142144
barrier.dst_access = vk::AccessFlagBits::eShaderRead;
143-
barrier.dst_stage = vk::PipelineStageFlagBits::eFragmentShader;
145+
barrier.dst_stage = vk::PipelineStageFlagBits::eFragmentShader |
146+
vk::PipelineStageFlagBits::eComputeShader;
144147

145148
return dst.SetLayout(barrier);
146149
}
@@ -170,9 +173,9 @@ bool BlitPassVK::OnCopyTextureToBufferCommand(
170173
barrier.src_stage = vk::PipelineStageFlagBits::eFragmentShader |
171174
vk::PipelineStageFlagBits::eTransfer |
172175
vk::PipelineStageFlagBits::eColorAttachmentOutput;
173-
barrier.dst_access = vk::AccessFlagBits::eShaderRead;
174-
barrier.dst_stage = vk::PipelineStageFlagBits::eVertexShader |
175-
vk::PipelineStageFlagBits::eFragmentShader;
176+
// The copy below reads the image.
177+
barrier.dst_access = vk::AccessFlagBits::eTransferRead;
178+
barrier.dst_stage = vk::PipelineStageFlagBits::eTransfer;
176179

177180
const auto& dst = DeviceBufferVK::Cast(*destination);
178181

@@ -198,16 +201,21 @@ bool BlitPassVK::OnCopyTextureToBufferCommand(
198201
image_copy //
199202
);
200203

201-
// If the buffer is used for readback, then apply a transfer -> host memory
202-
// barrier.
203-
if (destination->GetDeviceBufferDescriptor().readback) {
204+
// The copy must complete before a later compute pass reads the buffer. If
205+
// the buffer is used for readback, the host reads it too.
206+
{
204207
vk::MemoryBarrier barrier;
205208
barrier.srcAccessMask = vk::AccessFlagBits::eTransferWrite;
206-
barrier.dstAccessMask = vk::AccessFlagBits::eHostRead;
209+
barrier.dstAccessMask = vk::AccessFlagBits::eShaderRead;
210+
vk::PipelineStageFlags dst_stage =
211+
vk::PipelineStageFlagBits::eComputeShader;
212+
if (destination->GetDeviceBufferDescriptor().readback) {
213+
barrier.dstAccessMask |= vk::AccessFlagBits::eHostRead;
214+
dst_stage |= vk::PipelineStageFlagBits::eHost;
215+
}
207216

208-
cmd_buffer.pipelineBarrier(vk::PipelineStageFlagBits::eTransfer,
209-
vk::PipelineStageFlagBits::eHost, {}, 1,
210-
&barrier, 0, {}, 0, {});
217+
cmd_buffer.pipelineBarrier(vk::PipelineStageFlagBits::eTransfer, dst_stage,
218+
{}, 1, &barrier, 0, {}, 0, {});
211219
}
212220

213221
return true;
@@ -222,7 +230,8 @@ bool BlitPassVK::ConvertTextureToShaderRead(
222230
barrier.src_access = vk::AccessFlagBits::eTransferWrite;
223231
barrier.src_stage = vk::PipelineStageFlagBits::eTransfer;
224232
barrier.dst_access = vk::AccessFlagBits::eShaderRead;
225-
barrier.dst_stage = vk::PipelineStageFlagBits::eFragmentShader;
233+
barrier.dst_stage = vk::PipelineStageFlagBits::eFragmentShader |
234+
vk::PipelineStageFlagBits::eComputeShader;
226235

227236
barrier.new_layout = vk::ImageLayout::eShaderReadOnlyOptimal;
228237

@@ -300,7 +309,8 @@ bool BlitPassVK::OnCopyBufferToTextureCommand(
300309
barrier.src_access = vk::AccessFlagBits::eTransferWrite;
301310
barrier.src_stage = vk::PipelineStageFlagBits::eTransfer;
302311
barrier.dst_access = vk::AccessFlagBits::eShaderRead;
303-
barrier.dst_stage = vk::PipelineStageFlagBits::eFragmentShader;
312+
barrier.dst_stage = vk::PipelineStageFlagBits::eFragmentShader |
313+
vk::PipelineStageFlagBits::eComputeShader;
304314

305315
barrier.new_layout = vk::ImageLayout::eShaderReadOnlyOptimal;
306316

@@ -382,15 +392,17 @@ bool BlitPassVK::ResizeTexture(const std::shared_ptr<Texture>& source,
382392

383393
);
384394

385-
// Convert back to shader read
395+
// Convert back to shader read once the blit completes, for a later render or
396+
// compute pass.
386397

387398
BarrierVK barrier;
388399
barrier.cmd_buffer = cmd_buffer;
389400
barrier.new_layout = vk::ImageLayout::eShaderReadOnlyOptimal;
390-
barrier.src_access = {};
391-
barrier.src_stage = vk::PipelineStageFlagBits::eTopOfPipe;
401+
barrier.src_access = vk::AccessFlagBits::eTransferWrite;
402+
barrier.src_stage = vk::PipelineStageFlagBits::eTransfer;
392403
barrier.dst_access = vk::AccessFlagBits::eShaderRead;
393-
barrier.dst_stage = vk::PipelineStageFlagBits::eFragmentShader;
404+
barrier.dst_stage = vk::PipelineStageFlagBits::eFragmentShader |
405+
vk::PipelineStageFlagBits::eComputeShader;
394406

395407
return dst.SetLayout(barrier);
396408
}
@@ -520,8 +532,9 @@ bool BlitPassVK::OnGenerateMipmapCommand(std::shared_ptr<Texture> texture,
520532
barrier.dstAccessMask = vk::AccessFlagBits::eShaderRead;
521533

522534
cmd.pipelineBarrier(vk::PipelineStageFlagBits::eTransfer,
523-
vk::PipelineStageFlagBits::eFragmentShader, {}, {}, {},
524-
{barrier});
535+
vk::PipelineStageFlagBits::eFragmentShader |
536+
vk::PipelineStageFlagBits::eComputeShader,
537+
{}, {}, {}, {barrier});
525538

526539
// We modified the layouts of this image from underneath it. Tell it its new
527540
// state so it doesn't try to perform redundant transitions under the hood.

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

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,20 @@ fml::Status ComputePassVK::Compute(std::array<uint32_t, 3> workgroup_count) {
139139
}
140140
}
141141

142+
// Make the writes of earlier dispatches in the pass visible to this one,
143+
// and order this dispatch's writes after their reads and writes.
144+
if (has_dispatched_) {
145+
vk::MemoryBarrier barrier;
146+
barrier.srcAccessMask = vk::AccessFlagBits::eShaderWrite;
147+
barrier.dstAccessMask =
148+
vk::AccessFlagBits::eShaderRead | vk::AccessFlagBits::eShaderWrite;
149+
command_buffer_->GetCommandBuffer().pipelineBarrier(
150+
vk::PipelineStageFlagBits::eComputeShader,
151+
vk::PipelineStageFlagBits::eComputeShader, {}, 1, &barrier, 0, {}, 0,
152+
{});
153+
}
154+
has_dispatched_ = true;
155+
142156
// The arguments are workgroup counts. The per-workgroup invocation count (the
143157
// local size) is baked into the shader module, so dispatch the counts
144158
// directly.
@@ -243,22 +257,33 @@ void ComputePassVK::AddTextureMemoryBarrier() {
243257

244258
// |ComputePass|
245259
bool ComputePassVK::EncodeCommands() const {
246-
// Since we only use global memory barrier, we don't have to worry about
247-
// compute to compute dependencies across cmd buffers. Instead, we pessimize
248-
// here and assume that we wrote to a storage image or buffer and that a
249-
// render pass will read from it. if there are ever scenarios where we end up
250-
// with compute to compute dependencies this should be revisited.
251-
252-
// This does not currently handle image barriers as we do not use them
253-
// for anything.
260+
// Make the pass's writes visible to every later consumer in queue order,
261+
// whether in this command buffer or a later one: draws reading vertex,
262+
// index, indirect or uniform data, any shader stage, copies and host reads.
263+
// Order later writes after the pass's reads and writes too.
264+
//
265+
// This does not currently handle image layout transitions, since compute
266+
// passes do not write images.
254267
vk::MemoryBarrier barrier;
255268
barrier.srcAccessMask = vk::AccessFlagBits::eShaderWrite;
256269
barrier.dstAccessMask =
257-
vk::AccessFlagBits::eIndexRead | vk::AccessFlagBits::eVertexAttributeRead;
270+
vk::AccessFlagBits::eIndirectCommandRead |
271+
vk::AccessFlagBits::eIndexRead |
272+
vk::AccessFlagBits::eVertexAttributeRead |
273+
vk::AccessFlagBits::eUniformRead | vk::AccessFlagBits::eShaderRead |
274+
vk::AccessFlagBits::eShaderWrite | vk::AccessFlagBits::eTransferRead |
275+
vk::AccessFlagBits::eTransferWrite | vk::AccessFlagBits::eHostRead;
258276

259277
command_buffer_->GetCommandBuffer().pipelineBarrier(
260278
vk::PipelineStageFlagBits::eComputeShader,
261-
vk::PipelineStageFlagBits::eVertexInput, {}, 1, &barrier, 0, {}, 0, {});
279+
vk::PipelineStageFlagBits::eDrawIndirect |
280+
vk::PipelineStageFlagBits::eVertexInput |
281+
vk::PipelineStageFlagBits::eVertexShader |
282+
vk::PipelineStageFlagBits::eFragmentShader |
283+
vk::PipelineStageFlagBits::eComputeShader |
284+
vk::PipelineStageFlagBits::eTransfer |
285+
vk::PipelineStageFlagBits::eHost,
286+
{}, 1, &barrier, 0, {}, 0, {});
262287

263288
return true;
264289
}

‎engine/src/flutter/impeller/renderer/backend/vulkan/compute_pass_vk.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ class ComputePassVK final : public ComputePass {
4242
// next dispatch needs a new descriptor set. A descriptor set that an earlier
4343
// dispatch in this command buffer uses is never rewritten.
4444
bool descriptor_set_dirty_ = true;
45+
// Whether a dispatch was recorded, so the next one needs a barrier.
46+
bool has_dispatched_ = false;
4547
bool has_label_ = false;
4648

4749
ComputePassVK(std::shared_ptr<const Context> context,

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -215,14 +215,15 @@ vk::UniqueRenderPass RenderPassBuilderVK::Build(
215215
deps[1].dependencyFlags = kSelfDependencyFlags;
216216

217217
// Outgoing dependency. The resolve step or color attachment must complete
218-
// before we can sample from the image. This dependency is ignored for the
219-
// onscreen as we will already insert a barrier before presenting the
220-
// swapchain.
218+
// before we can sample from the image, in a later render pass or compute
219+
// pass. This dependency is ignored for the onscreen as we will already insert
220+
// a barrier before presenting the swapchain.
221221
deps[2].srcSubpass = 0u; // first subpass
222222
deps[2].dstSubpass = VK_SUBPASS_EXTERNAL;
223223
deps[2].srcStageMask = vk::PipelineStageFlagBits::eColorAttachmentOutput;
224224
deps[2].srcAccessMask = vk::AccessFlagBits::eColorAttachmentWrite;
225-
deps[2].dstStageMask = vk::PipelineStageFlagBits::eFragmentShader;
225+
deps[2].dstStageMask = vk::PipelineStageFlagBits::eFragmentShader |
226+
vk::PipelineStageFlagBits::eComputeShader;
226227
deps[2].dstAccessMask = vk::AccessFlagBits::eShaderRead;
227228
deps[2].dependencyFlags = kSelfDependencyFlags;
228229

‎engine/src/flutter/impeller/renderer/compute_pass.h‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,21 +48,28 @@ class ComputePass : public ResourceBinder {
4848
/// in the pass. A later dispatch reuses them until they are
4949
/// replaced.
5050
///
51+
/// Writes by earlier dispatches in the pass and by earlier passes
52+
/// are visible to this dispatch, and its writes are visible to
53+
/// later dispatches, passes and host reads, without a manual
54+
/// barrier.
55+
///
5156
/// @return OK without dispatching anything if any dimension is zero. A
5257
/// cancelled status if no pipeline is bound.
5358
///
5459
virtual fml::Status Compute(std::array<uint32_t, 3> workgroup_count) = 0;
5560

5661
/// @brief Ensures all previously encoded compute command's buffer writes are
57-
/// visible to any subsequent compute commands.
62+
/// visible to any subsequent compute commands. Dispatches are already
63+
/// synchronized automatically, so this is only a manual override.
5864
///
5965
/// On Vulkan, it does not matter if the compute command is in a
6066
/// different command buffer, only that it is executed later in queue
6167
/// order.
6268
virtual void AddBufferMemoryBarrier() = 0;
6369

6470
/// @brief Ensures all previously encoded compute command's texture writes are
65-
/// visible to any subsequent compute commands.
71+
/// visible to any subsequent compute commands. Dispatches are already
72+
/// synchronized automatically, so this is only a manual override.
6673
///
6774
/// On Vulkan, it does not matter if the compute command is in a
6875
/// different command buffer, only that it is executed later in queue

0 commit comments

Comments
 (0)