Skip to content

Commit c1a6be3

Browse files
committed
[Impeller] Settle the compute dispatch contract on Metal and Vulkan
Remove the Vulkan specialization constant that set constant_id 0 to the device's maximum workgroup width on every compute pipeline. Compute shaders declare a literal local_size, and pipeline creation on Metal and Vulkan now rejects a workgroup size of zero or one above the device and pipeline limits with a validation error. Capabilities report the maximum workgroup invocations, size, count and shared memory size. A dispatch with a zero count is a no-op that returns OK. The pipeline and bindings persist across dispatches in a pass on Vulkan as on Metal; a new descriptor set is written when they change, so a set an earlier dispatch uses is never rewritten. The Metal compute pass only pushes a command label debug group in debug builds, where it is popped. Fixes flutter#193868.
1 parent 5a42149 commit c1a6be3

24 files changed

Lines changed: 642 additions & 202 deletions

‎engine/src/flutter/impeller/compiler/code_gen_template.h‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ struct {{camel_case(shader_name)}}{{camel_case(shader_stage)}}Shader {
4141
static constexpr std::string_view kGeneratorName = "{{get_generator_name()}}";
4242
{% if shader_stage == "compute" %}
4343
// The workgroup (threadgroup) size declared by the shader. A dimension of 0
44-
// is sized by a specialization constant and resolved by the backend at
45-
// dispatch (for example, to the device maximum).
44+
// is sized by a specialization constant, which pipeline creation rejects.
4645
static constexpr std::array<uint32_t, 3> kWorkgroupSize = {
4746
{{workgroup_size_x}}u, {{workgroup_size_y}}u, {{workgroup_size_z}}u};
4847
{% endif %}

‎engine/src/flutter/impeller/compiler/reflector.cc‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,9 @@ std::optional<nlohmann::json> Reflector::GenerateTemplateArguments() const {
156156
}
157157

158158
// Compute shader workgroup (threadgroup) size. A value of 0 in any dimension
159-
// means that dimension is sized by a specialization constant and is resolved
160-
// by the backend at pipeline creation (for example, to the device maximum).
161-
// Only meaningful for compute shaders.
159+
// means that dimension is sized by a specialization constant, which the
160+
// runtime does not support: pipeline creation rejects it. Only meaningful for
161+
// compute shaders.
162162
{
163163
const std::array<uint32_t, 3> workgroup_size =
164164
ReflectWorkgroupSize().value_or(std::array<uint32_t, 3>{0u, 0u, 0u});
@@ -674,7 +674,7 @@ std::optional<std::array<uint32_t, 3>> Reflector::ReflectWorkgroupSize() const {
674674
const auto local_size = [&](const spirv_cross::SpecializationConstant& spec,
675675
uint32_t index) -> uint32_t {
676676
// A non-zero id means this dimension is driven by a specialization
677-
// constant; leave it as the runtime-resolved sentinel of 0.
677+
// constant; report it as 0.
678678
return spec.id != 0
679679
? 0u
680680
: compiler_->get_execution_mode_argument(

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ if (impeller_enable_compute) {
2121

2222
shaders = [
2323
"compute_3d_test.comp",
24+
"increment_test.comp",
25+
"oversized_workgroup_test.comp",
2426
"prefix_sum_test.comp",
2527
"threadgroup_sizing_test.comp",
2628
]

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,4 +426,25 @@ bool CapabilitiesGLES::NeedsPartitionedHostBuffer() const {
426426
#endif
427427
}
428428

429+
// The OpenGL ES backend does not support compute, so it reports no compute
430+
// limits.
431+
432+
uint32_t CapabilitiesGLES::GetMaximumComputeWorkgroupInvocations() const {
433+
return 0u;
434+
}
435+
436+
std::array<uint32_t, 3> CapabilitiesGLES::GetMaximumComputeWorkgroupSize()
437+
const {
438+
return {0u, 0u, 0u};
439+
}
440+
441+
std::array<uint32_t, 3> CapabilitiesGLES::GetMaximumComputeWorkgroupCount()
442+
const {
443+
return {0u, 0u, 0u};
444+
}
445+
446+
size_t CapabilitiesGLES::GetMaximumComputeSharedMemorySize() const {
447+
return 0u;
448+
}
449+
429450
} // namespace impeller

‎engine/src/flutter/impeller/renderer/backend/gles/capabilities_gles.h‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#ifndef FLUTTER_IMPELLER_RENDERER_BACKEND_GLES_CAPABILITIES_GLES_H_
66
#define FLUTTER_IMPELLER_RENDERER_BACKEND_GLES_CAPABILITIES_GLES_H_
77

8+
#include <array>
89
#include <cstddef>
910

1011
#include "impeller/base/backend_cast.h"
@@ -176,6 +177,18 @@ class CapabilitiesGLES final
176177
// |Capabilities|
177178
bool NeedsPartitionedHostBuffer() const override;
178179

180+
// |Capabilities|
181+
uint32_t GetMaximumComputeWorkgroupInvocations() const override;
182+
183+
// |Capabilities|
184+
std::array<uint32_t, 3> GetMaximumComputeWorkgroupSize() const override;
185+
186+
// |Capabilities|
187+
std::array<uint32_t, 3> GetMaximumComputeWorkgroupCount() const override;
188+
189+
// |Capabilities|
190+
size_t GetMaximumComputeSharedMemorySize() const override;
191+
179192
private:
180193
bool supports_texture_to_texture_blits_ = false;
181194
bool supports_framebuffer_fetch_ = false;

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,17 @@ class ComputePassMTL final : public ComputePass {
2828
id<MTLComputeCommandEncoder> encoder_ = nil;
2929
ComputePassBindingsCacheMTL pass_bindings_cache_ =
3030
ComputePassBindingsCacheMTL();
31-
// The workgroup size of the currently bound pipeline. A dimension of 0 is
32-
// resolved to the device maximum at dispatch.
31+
// The workgroup size of the currently bound pipeline.
3332
std::array<uint32_t, 3> workgroup_size_ = {0u, 0u, 0u};
3433
bool is_valid_ = false;
3534
bool has_label_ = false;
3635

3736
ComputePassMTL(std::shared_ptr<const Context> context,
3837
id<MTLCommandBuffer> buffer);
3938

39+
// Pops the debug group pushed by `SetCommandLabel`, if any.
40+
void PopCommandLabel();
41+
4042
// |ComputePass|
4143
bool IsValid() const override;
4244

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

Lines changed: 25 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#include "impeller/renderer/backend/metal/compute_pass_mtl.h"
66

77
#include <Metal/Metal.h>
8-
#include <algorithm>
98
#include <memory>
109

1110
#include "flutter/fml/backtrace.h"
@@ -58,8 +57,10 @@
5857
}
5958

6059
void ComputePassMTL::SetCommandLabel(std::string_view label) {
60+
#ifdef IMPELLER_DEBUG
6161
has_label_ = true;
62-
[encoder_ pushDebugGroup:@(label.data())];
62+
[encoder_ pushDebugGroup:@(std::string(label).c_str())];
63+
#endif // IMPELLER_DEBUG
6364
}
6465

6566
// |ComputePass|
@@ -124,58 +125,41 @@
124125
return true;
125126
}
126127

128+
void ComputePassMTL::PopCommandLabel() {
129+
#ifdef IMPELLER_DEBUG
130+
if (has_label_) {
131+
[encoder_ popDebugGroup];
132+
}
133+
has_label_ = false;
134+
#endif // IMPELLER_DEBUG
135+
}
136+
127137
fml::Status ComputePassMTL::Compute(std::array<uint32_t, 3> workgroup_count) {
138+
// A dispatch with no workgroups does nothing, like a draw with no vertices.
128139
if (workgroup_count[0] == 0u || workgroup_count[1] == 0u ||
129140
workgroup_count[2] == 0u) {
130-
return fml::Status(fml::StatusCode::kCancelled,
131-
"Invalid workgroup count for compute command.");
141+
PopCommandLabel();
142+
return fml::Status();
132143
}
133144

134-
const NSUInteger max_total =
135-
pass_bindings_cache_.GetPipeline().maxTotalThreadsPerThreadgroup;
136-
137-
// Unlike Vulkan and GLES, Metal does not bake the threadgroup size into the
138-
// shader; it is supplied here at dispatch. Honor the shader's declared local
139-
// size. A dimension of 0 means the shader sized it with a specialization
140-
// constant. In that case fill the remaining threadgroup capacity into x while
141-
// honoring any literal y and z, keeping the total within the device maximum.
142-
MTLSize threads_per_threadgroup;
143-
if (workgroup_size_[0] == 0u) {
144-
const NSUInteger size_y =
145-
workgroup_size_[1] == 0u ? 1u : workgroup_size_[1];
146-
const NSUInteger size_z =
147-
workgroup_size_[2] == 0u ? 1u : workgroup_size_[2];
148-
const NSUInteger size_x =
149-
std::max<NSUInteger>(1u, max_total / (size_y * size_z));
150-
threads_per_threadgroup = MTLSizeMake(size_x, size_y, size_z);
151-
} else {
152-
threads_per_threadgroup = MTLSizeMake(
153-
workgroup_size_[0], workgroup_size_[1] == 0u ? 1 : workgroup_size_[1],
154-
workgroup_size_[2] == 0u ? 1 : workgroup_size_[2]);
155-
}
156-
157-
// Metal aborts at dispatch if the threadgroup size exceeds the device
158-
// maximum. Vulkan rejects an oversized local size at pipeline creation, so do
159-
// the equivalent here and fail gracefully instead.
160-
if (threads_per_threadgroup.width * threads_per_threadgroup.height *
161-
threads_per_threadgroup.depth >
162-
max_total) {
145+
if (!pass_bindings_cache_.GetPipeline()) {
146+
PopCommandLabel();
163147
return fml::Status(fml::StatusCode::kCancelled,
164-
"Compute shader workgroup size exceeds the device's "
165-
"maximum threads per threadgroup.");
148+
"No compute pipeline is bound.");
166149
}
167150

151+
// Unlike Vulkan and GLES, Metal does not bake the threadgroup size into the
152+
// shader; it is supplied here at dispatch. Pipeline creation checked the
153+
// shader's declared size against the device and pipeline limits.
154+
const MTLSize threads_per_threadgroup =
155+
MTLSizeMake(workgroup_size_[0], workgroup_size_[1], workgroup_size_[2]);
156+
168157
[encoder_
169158
dispatchThreadgroups:MTLSizeMake(workgroup_count[0], workgroup_count[1],
170159
workgroup_count[2])
171160
threadsPerThreadgroup:threads_per_threadgroup];
172161

173-
#ifdef IMPELLER_DEBUG
174-
if (has_label_) {
175-
[encoder_ popDebugGroup];
176-
}
177-
has_label_ = false;
178-
#endif // IMPELLER_DEBUG
162+
PopCommandLabel();
179163
return fml::Status();
180164
}
181165

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include "impeller/renderer/backend/metal/context_mtl.h"
66
#include <Metal/Metal.h>
77

8+
#include <array>
9+
#include <limits>
810
#include <memory>
911

1012
#include "flutter/fml/concurrent_message_loop.h"
@@ -69,6 +71,17 @@ static bool DeviceSupportsTextureCompressionAstcHdr(id<MTLDevice> device) {
6971
return [device supportsFamily:MTLGPUFamilyApple6];
7072
}
7173

74+
// Metal reports the threadgroup size limit per dimension. The limit on the
75+
// total number of threads in a threadgroup equals the x limit on every Metal
76+
// GPU family (1024, or 512 before A11). A pipeline can lower it further, which
77+
// pipeline creation checks.
78+
static std::array<uint32_t, 3> DeviceMaxThreadsPerThreadgroup(
79+
id<MTLDevice> device) {
80+
const MTLSize size = device.maxThreadsPerThreadgroup;
81+
return {static_cast<uint32_t>(size.width), static_cast<uint32_t>(size.height),
82+
static_cast<uint32_t>(size.depth)};
83+
}
84+
7285
static std::unique_ptr<Capabilities> InferMetalCapabilities(
7386
id<MTLDevice> device,
7487
PixelFormat color_format) {
@@ -104,6 +117,15 @@ static bool DeviceSupportsTextureCompressionAstcHdr(id<MTLDevice> device) {
104117
.SetSupportsTextureCompression(
105118
CompressedTextureFamily::kASTCHDR,
106119
DeviceSupportsTextureCompressionAstcHdr(device))
120+
.SetMaximumComputeWorkgroupInvocations(
121+
DeviceMaxThreadsPerThreadgroup(device)[0])
122+
.SetMaximumComputeWorkgroupSize(DeviceMaxThreadsPerThreadgroup(device))
123+
// Metal does not limit the threadgroup count of a dispatch beyond the
124+
// 32-bit grid coordinates a shader sees.
125+
.SetMaximumComputeWorkgroupCount({std::numeric_limits<uint32_t>::max(),
126+
std::numeric_limits<uint32_t>::max(),
127+
std::numeric_limits<uint32_t>::max()})
128+
.SetMaximumComputeSharedMemorySize(device.maxThreadgroupMemoryLength)
107129
#if FML_OS_IOS && !TARGET_OS_SIMULATOR
108130
.SetMinimumUniformAlignment(16)
109131
#else

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ static void GetMTLRenderPipelineDescriptor(const PipelineDescriptor& desc,
243243
}
244244
}
245245
auto weak_this = weak_from_this();
246+
const MTLSize max_threads_per_threadgroup = device_.maxThreadsPerThreadgroup;
246247

247248
auto completion_handler =
248249
^(id<MTLComputePipelineState> _Nullable compute_pipeline_state,
@@ -255,6 +256,19 @@ static void GetMTLRenderPipelineDescriptor(const PipelineDescriptor& desc,
255256
return;
256257
}
257258

259+
// Metal supplies the threadgroup size at dispatch and aborts if it
260+
// exceeds the device or pipeline limit, so check the shader's declared
261+
// size here. The pipeline's total limit can be below the device's, for
262+
// example when the shader uses many registers.
263+
if (!descriptor.ValidateWorkgroupSize(
264+
{static_cast<uint32_t>(max_threads_per_threadgroup.width),
265+
static_cast<uint32_t>(max_threads_per_threadgroup.height),
266+
static_cast<uint32_t>(max_threads_per_threadgroup.depth)},
267+
compute_pipeline_state.maxTotalThreadsPerThreadgroup)) {
268+
promise->set_value(nullptr);
269+
return;
270+
}
271+
258272
auto strong_this = weak_this.lock();
259273
if (!strong_this) {
260274
VALIDATION_LOG << "Library was collected before a pending pipeline "

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,25 @@ bool CapabilitiesVK::NeedsPartitionedHostBuffer() const {
814814
return false;
815815
}
816816

817+
uint32_t CapabilitiesVK::GetMaximumComputeWorkgroupInvocations() const {
818+
return device_properties_.limits.maxComputeWorkGroupInvocations;
819+
}
820+
821+
std::array<uint32_t, 3> CapabilitiesVK::GetMaximumComputeWorkgroupSize() const {
822+
const auto& size = device_properties_.limits.maxComputeWorkGroupSize;
823+
return {size[0], size[1], size[2]};
824+
}
825+
826+
std::array<uint32_t, 3> CapabilitiesVK::GetMaximumComputeWorkgroupCount()
827+
const {
828+
const auto& count = device_properties_.limits.maxComputeWorkGroupCount;
829+
return {count[0], count[1], count[2]};
830+
}
831+
832+
size_t CapabilitiesVK::GetMaximumComputeSharedMemorySize() const {
833+
return device_properties_.limits.maxComputeSharedMemorySize;
834+
}
835+
817836
bool CapabilitiesVK::HasExtension(RequiredCommonDeviceExtensionVK ext) const {
818837
return required_common_device_extensions_.find(ext) !=
819838
required_common_device_extensions_.end();

0 commit comments

Comments
 (0)