Skip to content

Commit a3a21c8

Browse files
authored
[Flutter GPU] Compute shaders on Metal and Vulkan (flutter#193805)
Fixes flutter#188477, fixes flutter#188479, fixes flutter#188480, fixes flutter#193868, fixes flutter#193869, fixes flutter#193872. Part of flutter#188474 and flutter#188481. Adds compute to Flutter GPU on Metal and Vulkan. Stacked on flutter#194167. - Shader bundles (format version 3) carry each shader's storage buffers and compute workgroup size. - `gpuContext.createComputePipeline`, `CommandBuffer.createComputePass` and `ComputePass`, with storage, uniform and texture bindings, binding sets and `dispatch`. `gpuContext.supportsCompute` and the compute limits are exposed; on OpenGL ES compute is unsupported and the compute calls throw. - Impeller: workgroup sizes are checked against device limits when a pipeline is created, bindings persist across dispatches on Vulkan, dispatches and passes synchronize automatically, and reloading a compute shader no longer crashes. - End-to-end tests on Metal and Vulkan, including a compute pass writing vertices that a render pass draws. ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [ ] All existing and new tests are passing. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
1 parent 322fc24 commit a3a21c8

76 files changed

Lines changed: 6059 additions & 359 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎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: 73 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -156,33 +156,15 @@ 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
{
163-
uint32_t workgroup_size_x = 0u;
164-
uint32_t workgroup_size_y = 0u;
165-
uint32_t workgroup_size_z = 0u;
166-
if (execution_model == spv::ExecutionModel::ExecutionModelGLCompute) {
167-
spirv_cross::SpecializationConstant spec_x, spec_y, spec_z;
168-
compiler_->get_work_group_size_specialization_constants(spec_x, spec_y,
169-
spec_z);
170-
const auto local_size = [&](spirv_cross::SpecializationConstant& spec,
171-
uint32_t index) -> uint32_t {
172-
// A non-zero id means this dimension is driven by a specialization
173-
// constant; leave it as the runtime-resolved sentinel of 0.
174-
return spec.id != 0
175-
? 0u
176-
: compiler_->get_execution_mode_argument(
177-
spv::ExecutionMode::ExecutionModeLocalSize, index);
178-
};
179-
workgroup_size_x = local_size(spec_x, 0u);
180-
workgroup_size_y = local_size(spec_y, 1u);
181-
workgroup_size_z = local_size(spec_z, 2u);
182-
}
183-
root["workgroup_size_x"] = workgroup_size_x;
184-
root["workgroup_size_y"] = workgroup_size_y;
185-
root["workgroup_size_z"] = workgroup_size_z;
163+
const std::array<uint32_t, 3> workgroup_size =
164+
ReflectWorkgroupSize().value_or(std::array<uint32_t, 3>{0u, 0u, 0u});
165+
root["workgroup_size_x"] = workgroup_size[0];
166+
root["workgroup_size_y"] = workgroup_size[1];
167+
root["workgroup_size_z"] = workgroup_size[2];
186168
}
187169

188170
const auto shader_resources = compiler_->get_shader_resources();
@@ -608,6 +590,48 @@ std::shared_ptr<ShaderBundleData> Reflector::GenerateShaderBundleData() const {
608590
data->AddUniformTexture(uniform_texture);
609591
}
610592

593+
const auto storage_buffers =
594+
compiler_->get_shader_resources().storage_buffers;
595+
for (const auto& buffer : storage_buffers) {
596+
ShaderBundleData::ShaderStorageBuffer storage_buffer;
597+
storage_buffer.name = buffer.name;
598+
storage_buffer.ext_res_0 = compiler_.GetExtendedMSLResourceBinding(
599+
CompilerBackend::ExtendedResourceIndex::kPrimary, buffer.id);
600+
storage_buffer.set = compiler_->get_decoration(
601+
buffer.id, spv::Decoration::DecorationDescriptorSet);
602+
storage_buffer.binding = compiler_->get_decoration(
603+
buffer.id, spv::Decoration::DecorationBinding);
604+
605+
// `readonly` and `writeonly` may decorate the block variable or every
606+
// member of the block. `get_buffer_block_flags` folds both cases together.
607+
const auto flags = compiler_->get_buffer_block_flags(buffer.id);
608+
const bool non_writable = flags.get(spv::Decoration::DecorationNonWritable);
609+
const bool non_readable = flags.get(spv::Decoration::DecorationNonReadable);
610+
if (non_writable && !non_readable) {
611+
storage_buffer.access = fb::shaderbundle::ShaderResourceAccess::kReadOnly;
612+
} else if (non_readable && !non_writable) {
613+
storage_buffer.access =
614+
fb::shaderbundle::ShaderResourceAccess::kWriteOnly;
615+
} else {
616+
storage_buffer.access =
617+
fb::shaderbundle::ShaderResourceAccess::kReadWrite;
618+
}
619+
620+
const auto& type = compiler_->get_type(buffer.base_type_id);
621+
const size_t empty_size =
622+
compiler_->get_declared_struct_size_runtime_array(type, 0);
623+
storage_buffer.size_in_bytes = empty_size;
624+
storage_buffer.runtime_array_stride =
625+
compiler_->get_declared_struct_size_runtime_array(type, 1) - empty_size;
626+
627+
data->AddStorageBuffer(std::move(storage_buffer));
628+
}
629+
630+
if (auto workgroup_size = ReflectWorkgroupSize();
631+
workgroup_size.has_value()) {
632+
data->SetWorkgroupSize(workgroup_size.value());
633+
}
634+
611635
// We only need to worry about storing vertex attributes.
612636
if (entrypoints.front().execution_model == spv::ExecutionModelVertex) {
613637
const auto inputs = compiler_->get_shader_resources().stage_inputs;
@@ -637,6 +661,29 @@ std::shared_ptr<ShaderBundleData> Reflector::GenerateShaderBundleData() const {
637661
return data;
638662
}
639663

664+
std::optional<std::array<uint32_t, 3>> Reflector::ReflectWorkgroupSize() const {
665+
const auto& entrypoints = compiler_->get_entry_points_and_stages();
666+
if (entrypoints.size() != 1u ||
667+
entrypoints.front().execution_model !=
668+
spv::ExecutionModel::ExecutionModelGLCompute) {
669+
return std::nullopt;
670+
}
671+
spirv_cross::SpecializationConstant spec_x, spec_y, spec_z;
672+
compiler_->get_work_group_size_specialization_constants(spec_x, spec_y,
673+
spec_z);
674+
const auto local_size = [&](const spirv_cross::SpecializationConstant& spec,
675+
uint32_t index) -> uint32_t {
676+
// A non-zero id means this dimension is driven by a specialization
677+
// constant; report it as 0.
678+
return spec.id != 0
679+
? 0u
680+
: compiler_->get_execution_mode_argument(
681+
spv::ExecutionMode::ExecutionModeLocalSize, index);
682+
};
683+
return std::array<uint32_t, 3>{local_size(spec_x, 0u), local_size(spec_y, 1u),
684+
local_size(spec_z, 2u)};
685+
}
686+
640687
std::optional<uint32_t> Reflector::GetArrayElements(
641688
const spirv_cross::SPIRType& type) const {
642689
if (type.array.empty()) {

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

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

8+
#include <array>
89
#include <cstdint>
910
#include <memory>
1011
#include <optional>
@@ -246,6 +247,11 @@ class Reflector {
246247

247248
std::shared_ptr<ShaderBundleData> GenerateShaderBundleData() const;
248249

250+
/// The workgroup size a compute shader declares, or std::nullopt for any
251+
/// other stage. A dimension of 0 means the shader sizes it with a
252+
/// specialization constant.
253+
std::optional<std::array<uint32_t, 3>> ReflectWorkgroupSize() const;
254+
249255
std::shared_ptr<fml::Mapping> InflateTemplate(std::string_view tmpl) const;
250256

251257
std::optional<nlohmann::json::object_t> ReflectResource(

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,21 @@ GenerateShaderBackendFB(TargetPlatform target_platform,
190190
return nullptr;
191191
}
192192

193+
// The runtime needs the workgroup size to dispatch on Metal and to validate
194+
// it against device limits, so a bundled compute shader must declare it with
195+
// a literal `local_size`.
196+
if (const auto& workgroup_size = bundle_data->GetWorkgroupSize();
197+
workgroup_size.has_value() &&
198+
((*workgroup_size)[0] == 0u || (*workgroup_size)[1] == 0u ||
199+
(*workgroup_size)[2] == 0u)) {
200+
std::cerr << "Compute shader \"" << shader_name
201+
<< "\" sizes its workgroup with a specialization constant "
202+
"(local_size_x_id, local_size_y_id or local_size_z_id). "
203+
"Bundled compute shaders must declare a literal local_size."
204+
<< std::endl;
205+
return nullptr;
206+
}
207+
193208
result = bundle_data->CreateFlatbuffer();
194209
if (!result) {
195210
std::cerr << "Failed to create flatbuffer for bundled shader \""

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,24 @@ void ShaderBundleData::AddUniformTexture(ShaderUniformTexture uniform_texture) {
3030
uniform_textures_.emplace_back(std::move(uniform_texture));
3131
}
3232

33+
void ShaderBundleData::AddStorageBuffer(ShaderStorageBuffer storage_buffer) {
34+
storage_buffers_.emplace_back(std::move(storage_buffer));
35+
}
36+
3337
void ShaderBundleData::AddInputDescription(InputDescription input) {
3438
inputs_.emplace_back(std::move(input));
3539
}
3640

41+
void ShaderBundleData::SetWorkgroupSize(
42+
std::array<uint32_t, 3> workgroup_size) {
43+
workgroup_size_ = workgroup_size;
44+
}
45+
46+
const std::optional<std::array<uint32_t, 3>>&
47+
ShaderBundleData::GetWorkgroupSize() const {
48+
return workgroup_size_;
49+
}
50+
3751
void ShaderBundleData::SetShaderData(std::shared_ptr<fml::Mapping> shader) {
3852
shader_ = std::move(shader);
3953
}
@@ -210,6 +224,29 @@ ShaderBundleData::CreateFlatbuffer() const {
210224
shader_bundle->uniform_textures.emplace_back(std::move(desc));
211225
}
212226

227+
for (const auto& storage_buffer : storage_buffers_) {
228+
auto desc = std::make_unique<fb::shaderbundle::ShaderStorageBufferT>();
229+
desc->name = storage_buffer.name;
230+
if (desc->name.empty()) {
231+
VALIDATION_LOG << "Storage buffer name cannot be empty.";
232+
return nullptr;
233+
}
234+
desc->ext_res_0 = storage_buffer.ext_res_0;
235+
desc->set = storage_buffer.set;
236+
desc->binding = storage_buffer.binding;
237+
desc->access = storage_buffer.access;
238+
desc->size_in_bytes = storage_buffer.size_in_bytes;
239+
desc->runtime_array_stride = storage_buffer.runtime_array_stride;
240+
shader_bundle->storage_buffers.emplace_back(std::move(desc));
241+
}
242+
243+
if (workgroup_size_.has_value()) {
244+
const auto& size = workgroup_size_.value();
245+
shader_bundle->workgroup_size =
246+
std::make_unique<fb::shaderbundle::WorkgroupSize>(size[0], size[1],
247+
size[2]);
248+
}
249+
213250
for (const auto& input : inputs_) {
214251
auto desc = std::make_unique<fb::shaderbundle::ShaderInputT>();
215252

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

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

8+
#include <array>
9+
#include <cstdint>
810
#include <memory>
11+
#include <optional>
912
#include <vector>
1013

1114
#include "flutter/fml/mapping.h"
@@ -48,6 +51,30 @@ class ShaderBundleData {
4851
size_t binding = 0u;
4952
};
5053

54+
/// Reflection data for a storage buffer (an SSBO) that a compute shader
55+
/// declares, serialized as a `ShaderStorageBuffer` in the bundle.
56+
struct ShaderStorageBuffer {
57+
/// The name of the buffer block.
58+
std::string name;
59+
/// The backend's index for the buffer: the Metal buffer index, or the
60+
/// binding on Vulkan. `kOptimizedOutBinding` if the backend compiler
61+
/// removed the buffer because the shader never uses it.
62+
size_t ext_res_0 = 0u;
63+
/// The descriptor set the shader declares the buffer in.
64+
size_t set = 0u;
65+
/// The binding the shader declares the buffer at.
66+
size_t binding = 0u;
67+
/// How the shader accesses the buffer, from its `readonly` or `writeonly`
68+
/// qualifier.
69+
fb::shaderbundle::ShaderResourceAccess access =
70+
fb::shaderbundle::ShaderResourceAccess::kReadWrite;
71+
/// The size of the block, counting a trailing runtime-sized array as empty.
72+
size_t size_in_bytes = 0u;
73+
/// The stride of the block's trailing runtime-sized array, or zero if the
74+
/// block does not end in one.
75+
size_t runtime_array_stride = 0u;
76+
};
77+
5178
ShaderBundleData(std::string entrypoint,
5279
spv::ExecutionModel stage,
5380
TargetPlatform target_platform);
@@ -58,8 +85,16 @@ class ShaderBundleData {
5885

5986
void AddUniformTexture(ShaderUniformTexture uniform_texture);
6087

88+
void AddStorageBuffer(ShaderStorageBuffer storage_buffer);
89+
6190
void AddInputDescription(InputDescription input);
6291

92+
/// Sets the workgroup size of a compute shader. A dimension of 0 means the
93+
/// shader sizes it with a specialization constant.
94+
void SetWorkgroupSize(std::array<uint32_t, 3> workgroup_size);
95+
96+
const std::optional<std::array<uint32_t, 3>>& GetWorkgroupSize() const;
97+
6398
void SetShaderData(std::shared_ptr<fml::Mapping> shader);
6499

65100
void SetSkSLData(std::shared_ptr<fml::Mapping> sksl);
@@ -72,6 +107,8 @@ class ShaderBundleData {
72107
const TargetPlatform target_platform_;
73108
std::vector<ShaderUniformStruct> uniform_structs_;
74109
std::vector<ShaderUniformTexture> uniform_textures_;
110+
std::vector<ShaderStorageBuffer> storage_buffers_;
111+
std::optional<std::array<uint32_t, 3>> workgroup_size_;
75112
std::vector<InputDescription> inputs_;
76113
std::shared_ptr<fml::Mapping> shader_;
77114
std::shared_ptr<fml::Mapping> sksl_;

0 commit comments

Comments
 (0)