Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

[Flutter GPU] Generate DescriptorSetLayouts for pipelines & export symbols on Android. #53184

Merged
merged 4 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion lib/gpu/render_pipeline.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,14 @@ RenderPipeline::RenderPipeline(
void RenderPipeline::BindToPipelineDescriptor(
impeller::ShaderLibrary& library,
impeller::PipelineDescriptor& desc) {
desc.SetVertexDescriptor(vertex_shader_->GetVertexDescriptor());
auto vertex_descriptor = vertex_shader_->CreateVertexDescriptor();
vertex_descriptor->RegisterDescriptorSetLayouts(
vertex_shader_->GetDescriptorSetLayouts().data(),
vertex_shader_->GetDescriptorSetLayouts().size());
vertex_descriptor->RegisterDescriptorSetLayouts(
fragment_shader_->GetDescriptorSetLayouts().data(),
fragment_shader_->GetDescriptorSetLayouts().size());
desc.SetVertexDescriptor(vertex_descriptor);

desc.AddStageEntrypoint(vertex_shader_->GetFunctionFromLibrary(library));
desc.AddStageEntrypoint(fragment_shader_->GetFunctionFromLibrary(library));
Expand Down
21 changes: 16 additions & 5 deletions lib/gpu/shader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,21 @@ fml::RefPtr<Shader> Shader::Make(
std::string entrypoint,
impeller::ShaderStage stage,
std::shared_ptr<fml::Mapping> code_mapping,
std::shared_ptr<impeller::VertexDescriptor> vertex_desc,
std::vector<impeller::ShaderStageIOSlot> inputs,
std::vector<impeller::ShaderStageBufferLayout> layouts,
std::unordered_map<std::string, UniformBinding> uniform_structs,
std::unordered_map<std::string, impeller::SampledImageSlot>
uniform_textures) {
uniform_textures,
std::vector<impeller::DescriptorSetLayout> descriptor_set_layouts) {
auto shader = fml::MakeRefCounted<Shader>();
shader->entrypoint_ = std::move(entrypoint);
shader->stage_ = stage;
shader->code_mapping_ = std::move(code_mapping);
shader->vertex_desc_ = std::move(vertex_desc);
shader->inputs_ = std::move(inputs);
shader->layouts_ = std::move(layouts);
shader->uniform_structs_ = std::move(uniform_structs);
shader->uniform_textures_ = std::move(uniform_textures);
shader->descriptor_set_layouts_ = std::move(descriptor_set_layouts);
return shader;
}

Expand Down Expand Up @@ -83,15 +87,22 @@ bool Shader::RegisterSync(Context& context) {
return true;
}

std::shared_ptr<impeller::VertexDescriptor> Shader::GetVertexDescriptor()
std::shared_ptr<impeller::VertexDescriptor> Shader::CreateVertexDescriptor()
const {
return vertex_desc_;
auto vertex_descriptor = std::make_shared<impeller::VertexDescriptor>();
vertex_descriptor->SetStageInputs(inputs_, layouts_);
return vertex_descriptor;
}

impeller::ShaderStage Shader::GetShaderStage() const {
return stage_;
}

const std::vector<impeller::DescriptorSetLayout>&
Shader::GetDescriptorSetLayouts() const {
return descriptor_set_layouts_;
}

const Shader::UniformBinding* Shader::GetUniformStruct(
const std::string& name) const {
auto uniform = uniform_structs_.find(name);
Expand Down
15 changes: 11 additions & 4 deletions lib/gpu/shader.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,12 @@ class Shader : public RefCountedDartWrappable<Shader> {
std::string entrypoint,
impeller::ShaderStage stage,
std::shared_ptr<fml::Mapping> code_mapping,
std::shared_ptr<impeller::VertexDescriptor> vertex_desc,
std::vector<impeller::ShaderStageIOSlot> inputs,
std::vector<impeller::ShaderStageBufferLayout> layouts,
std::unordered_map<std::string, UniformBinding> uniform_structs,
std::unordered_map<std::string, impeller::SampledImageSlot>
uniform_textures);
uniform_textures,
std::vector<impeller::DescriptorSetLayout> descriptor_set_layouts);

std::shared_ptr<const impeller::ShaderFunction> GetFunctionFromLibrary(
impeller::ShaderLibrary& library);
Expand All @@ -52,7 +54,10 @@ class Shader : public RefCountedDartWrappable<Shader> {

bool RegisterSync(Context& context);

std::shared_ptr<impeller::VertexDescriptor> GetVertexDescriptor() const;
std::shared_ptr<impeller::VertexDescriptor> CreateVertexDescriptor() const;

const std::vector<impeller::DescriptorSetLayout>& GetDescriptorSetLayouts()
const;

impeller::ShaderStage GetShaderStage() const;

Expand All @@ -67,9 +72,11 @@ class Shader : public RefCountedDartWrappable<Shader> {
std::string entrypoint_;
impeller::ShaderStage stage_;
std::shared_ptr<fml::Mapping> code_mapping_;
std::shared_ptr<impeller::VertexDescriptor> vertex_desc_;
std::vector<impeller::ShaderStageIOSlot> inputs_;
std::vector<impeller::ShaderStageBufferLayout> layouts_;
std::unordered_map<std::string, UniformBinding> uniform_structs_;
std::unordered_map<std::string, impeller::SampledImageSlot> uniform_textures_;
std::vector<impeller::DescriptorSetLayout> descriptor_set_layouts_;

FML_DISALLOW_COPY_AND_ASSIGN(Shader);
};
Expand Down
34 changes: 22 additions & 12 deletions lib/gpu/shader_library.cc
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@ fml::RefPtr<ShaderLibrary> ShaderLibrary::MakeFromFlatbuffer(
[payload = payload](auto, auto) {} //
);

std::vector<impeller::DescriptorSetLayout> descriptor_set_layouts;

std::unordered_map<std::string, Shader::UniformBinding> uniform_structs;
if (backend_shader->uniform_structs() != nullptr) {
for (const auto& uniform : *backend_shader->uniform_structs()) {
Expand Down Expand Up @@ -245,6 +247,12 @@ fml::RefPtr<ShaderLibrary> ShaderLibrary::MakeFromFlatbuffer(
},
.size_in_bytes = static_cast<size_t>(uniform->size_in_bytes()),
};

descriptor_set_layouts.push_back(impeller::DescriptorSetLayout{
static_cast<uint32_t>(uniform->binding()),
impeller::DescriptorType::kUniformBuffer,
ToShaderStage(backend_shader->stage()),
});
}
}

Expand All @@ -258,16 +266,21 @@ fml::RefPtr<ShaderLibrary> ShaderLibrary::MakeFromFlatbuffer(
.set = static_cast<size_t>(uniform->set()),
.binding = static_cast<size_t>(uniform->binding()),
};

descriptor_set_layouts.push_back(impeller::DescriptorSetLayout{
static_cast<uint32_t>(uniform->binding()),
impeller::DescriptorType::kSampledImage,
ToShaderStage(backend_shader->stage()),
});
}
}

std::shared_ptr<impeller::VertexDescriptor> vertex_descriptor = nullptr;
std::vector<impeller::ShaderStageIOSlot> inputs;
std::vector<impeller::ShaderStageBufferLayout> layouts;
if (backend_shader->stage() ==
impeller::fb::shaderbundle::ShaderStage::kVertex) {
vertex_descriptor = std::make_shared<impeller::VertexDescriptor>();
auto inputs_fb = backend_shader->inputs();

std::vector<impeller::ShaderStageIOSlot> inputs;
inputs.reserve(inputs_fb->size());
size_t default_stride = 0;
for (const auto& input : *inputs_fb) {
Expand All @@ -286,20 +299,17 @@ fml::RefPtr<ShaderLibrary> ShaderLibrary::MakeFromFlatbuffer(
default_stride +=
SizeOfInputType(input->type()) * slot.vec_size * slot.columns;
}
std::vector<impeller::ShaderStageBufferLayout> layouts = {
impeller::ShaderStageBufferLayout{
.stride = default_stride,
.binding = 0u,
}};

vertex_descriptor->SetStageInputs(inputs, layouts);
layouts = {impeller::ShaderStageBufferLayout{
.stride = default_stride,
.binding = 0u,
}};
}

auto shader = flutter::gpu::Shader::Make(
backend_shader->entrypoint()->str(),
ToShaderStage(backend_shader->stage()), std::move(code_mapping),
std::move(vertex_descriptor), std::move(uniform_structs),
std::move(uniform_textures));
std::move(inputs), std::move(layouts), std::move(uniform_structs),
std::move(uniform_textures), std::move(descriptor_set_layouts));
shader_map[bundled_shader->name()->str()] = std::move(shader);
}

Expand Down
2 changes: 2 additions & 0 deletions shell/platform/android/android_exports.lst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
JNI_OnLoad;
_binary_icudtl_dat_start;
_binary_icudtl_dat_size;
InternalFlutterGpu*;
kInternalFlutterGpu*;
local:
*;
};
4 changes: 3 additions & 1 deletion testing/symbols/verify_exported.dart
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,9 @@ int _checkAndroid(String outPath, String nmPath, Iterable<String> builds) {
};
final Map<String, String> badSymbols = <String, String>{};
for (final String key in entryMap.keys) {
if (entryMap[key] != expectedSymbols[key]) {
final bool isValidFlutterGpuSymbol =
key.startsWith('InternalFlutterGpu') && entryMap[key] == 'T';
if (!isValidFlutterGpuSymbol && entryMap[key] != expectedSymbols[key]) {
badSymbols[key] = entryMap[key]!;
}
}
Expand Down