[Flutter GPU] Get the GLES backend/Windows working. (flutter/engine#55694)

Resolves https://github.com/flutter/flutter/issues/156305.

* Resolve pipelines and submit command buffers on the raster thread.
* Don't use desktop GL shader variation on Windows.
* Fix interpretation of `array_elements`.
* Fix texture binding metadata.

Gets Flutter GPU working on Windows!
![image](https://github.com/user-attachments/assets/9eecb67f-a980-4556-8060-b0c947713534)
![image](https://github.com/user-attachments/assets/c8e2071f-e7c0-411c-8f37-e1f3037916f4)
This commit is contained in:
Brandon DeRosier
2024-10-08 17:04:39 -07:00
committed by GitHub
parent 0ed39374df
commit e118869e40
13 changed files with 110 additions and 30 deletions

View File

@@ -307,6 +307,11 @@ PipelineFuture<ComputePipelineDescriptor> PipelineLibraryGLES::GetPipeline(
return {descriptor, promise->get_future()};
}
// |PipelineLibrary|
bool PipelineLibraryGLES::HasPipeline(const PipelineDescriptor& descriptor) {
return pipelines_.find(descriptor) != pipelines_.end();
}
// |PipelineLibrary|
void PipelineLibraryGLES::RemovePipelinesWithEntryPoint(
std::shared_ptr<const ShaderFunction> function) {

View File

@@ -106,6 +106,9 @@ class PipelineLibraryGLES final
ComputePipelineDescriptor descriptor,
bool async) override;
// |PipelineLibrary|
bool HasPipeline(const PipelineDescriptor& descriptor) override;
// |PipelineLibrary|
void RemovePipelinesWithEntryPoint(
std::shared_ptr<const ShaderFunction> function) override;

View File

@@ -41,6 +41,9 @@ class PipelineLibraryMTL final : public PipelineLibrary {
ComputePipelineDescriptor descriptor,
bool async) override;
// |PipelineLibrary|
bool HasPipeline(const PipelineDescriptor& descriptor) override;
// |PipelineLibrary|
void RemovePipelinesWithEntryPoint(
std::shared_ptr<const ShaderFunction> function) override;

View File

@@ -217,6 +217,11 @@ PipelineFuture<ComputePipelineDescriptor> PipelineLibraryMTL::GetPipeline(
return pipeline_future;
}
// |PipelineLibrary|
bool PipelineLibraryMTL::HasPipeline(const PipelineDescriptor& descriptor) {
return pipelines_.find(descriptor) != pipelines_.end();
}
// |PipelineLibrary|
void PipelineLibraryMTL::RemovePipelinesWithEntryPoint(
std::shared_ptr<const ShaderFunction> function) {

View File

@@ -259,6 +259,12 @@ PipelineFuture<ComputePipelineDescriptor> PipelineLibraryVK::GetPipeline(
return pipeline_future;
}
// |PipelineLibrary|
bool PipelineLibraryVK::HasPipeline(const PipelineDescriptor& descriptor) {
Lock lock(pipelines_mutex_);
return pipelines_.find(descriptor) != pipelines_.end();
}
// |PipelineLibrary|
void PipelineLibraryVK::RemovePipelinesWithEntryPoint(
std::shared_ptr<const ShaderFunction> function) {

View File

@@ -67,6 +67,9 @@ class PipelineLibraryVK final
ComputePipelineDescriptor descriptor,
bool async) override;
// |PipelineLibrary|
bool HasPipeline(const PipelineDescriptor& descriptor) override;
// |PipelineLibrary|
void RemovePipelinesWithEntryPoint(
std::shared_ptr<const ShaderFunction> function) override;

View File

@@ -48,6 +48,8 @@ class PipelineLibrary : public std::enable_shared_from_this<PipelineLibrary> {
ComputePipelineDescriptor descriptor,
bool async = true) = 0;
virtual bool HasPipeline(const PipelineDescriptor& descriptor) = 0;
virtual void RemovePipelinesWithEntryPoint(
std::shared_ptr<const ShaderFunction> function) = 0;

View File

@@ -34,11 +34,7 @@ void CommandBuffer::AddRenderPass(
}
bool CommandBuffer::Submit() {
for (auto& encodable : encodables_) {
encodable->EncodeCommands();
}
return context_->GetCommandQueue()->Submit({command_buffer_}).ok();
return CommandBuffer::Submit({});
}
bool CommandBuffer::Submit(
@@ -46,6 +42,23 @@ bool CommandBuffer::Submit(
for (auto& encodable : encodables_) {
encodable->EncodeCommands();
}
// For the GLES backend, command queue submission just flushes the reactor,
// which needs to happen on the raster thread.
if (context_->GetBackendType() == impeller::Context::BackendType::kOpenGLES) {
auto dart_state = flutter::UIDartState::Current();
auto& task_runners = dart_state->GetTaskRunners();
task_runners.GetRasterTaskRunner()->PostTask(fml::MakeCopyable(
[context = context_, command_buffer = command_buffer_,
completion_callback = completion_callback]() mutable {
context->GetCommandQueue()
->Submit({command_buffer}, completion_callback)
.ok();
}));
return true;
}
return context_->GetCommandQueue()
->Submit({command_buffer_}, completion_callback)
.ok();
@@ -91,7 +104,7 @@ Dart_Handle InternalFlutterGpu_CommandBuffer_Submit(
std::make_unique<tonic::DartPersistentValue>(dart_state,
completion_callback);
bool success = wrapper->Submit(fml::MakeCopyable(
auto ui_task_completion_callback = fml::MakeCopyable(
[callback = std::move(persistent_completion_callback),
task_runners](impeller::CommandBuffer::Status status) mutable {
bool success = status != impeller::CommandBuffer::Status::kError;
@@ -112,7 +125,8 @@ Dart_Handle InternalFlutterGpu_CommandBuffer_Submit(
callback.reset();
});
task_runners.GetUITaskRunner()->PostTask(ui_completion_task);
}));
});
bool success = wrapper->Submit(ui_task_completion_callback);
if (!success) {
return tonic::ToDart("Failed to submit CommandBuffer");
}

View File

@@ -9,6 +9,7 @@
#include "flutter/lib/gpu/export.h"
#include "flutter/lib/ui/dart_wrapper.h"
#include "impeller/renderer/command_buffer.h"
#include "impeller/renderer/context.h"
namespace flutter {
namespace gpu {

View File

@@ -3,10 +3,13 @@
// found in the LICENSE file.
#include "flutter/lib/gpu/render_pass.h"
#include <future>
#include <memory>
#include "flutter/lib/gpu/formats.h"
#include "flutter/lib/gpu/render_pipeline.h"
#include "flutter/lib/gpu/shader.h"
#include "fml/make_copyable.h"
#include "fml/memory/ref_ptr.h"
#include "impeller/core/buffer_view.h"
#include "impeller/core/formats.h"
@@ -14,7 +17,10 @@
#include "impeller/core/shader_types.h"
#include "impeller/core/vertex_buffer.h"
#include "impeller/geometry/color.h"
#include "impeller/renderer/pipeline.h"
#include "impeller/renderer/pipeline_descriptor.h"
#include "impeller/renderer/pipeline_library.h"
#include "lib/ui/ui_dart_state.h"
#include "tonic/converter/dart_converter.h"
namespace flutter {
@@ -134,8 +140,34 @@ RenderPass::GetOrCreatePipeline() {
render_pipeline_->BindToPipelineDescriptor(*context.GetShaderLibrary(),
pipeline_desc);
auto pipeline =
context.GetPipelineLibrary()->GetPipeline(pipeline_desc).Get();
std::shared_ptr<impeller::Pipeline<impeller::PipelineDescriptor>> pipeline;
if (context.GetBackendType() == impeller::Context::BackendType::kOpenGLES &&
!context.GetPipelineLibrary()->HasPipeline(pipeline_desc)) {
// For GLES, new pipeline creation must be done on the reactor (raster)
// thread. We're about the draw, so we need to synchronize with a raster
// task in order to get the new pipeline. Depending on how busy the raster
// thread is, this could hang the UI thread long enough to miss a frame.
// Note that this branch is only called if a new pipeline actually needs to
// be built.
auto dart_state = flutter::UIDartState::Current();
std::promise<
std::shared_ptr<impeller::Pipeline<impeller::PipelineDescriptor>>>
pipeline_promise;
auto pipeline_future = pipeline_promise.get_future();
fml::TaskRunner::RunNowOrPostTask(
dart_state->GetTaskRunners().GetRasterTaskRunner(),
fml::MakeCopyable([promise = std::move(pipeline_promise),
context = GetContext(), pipeline_desc]() mutable {
promise.set_value(
context->GetPipelineLibrary()->GetPipeline(pipeline_desc).Get());
}));
pipeline = pipeline_future.get();
} else {
pipeline = context.GetPipelineLibrary()->GetPipeline(pipeline_desc).Get();
}
FML_DCHECK(pipeline) << "Couldn't resolve render pipeline";
return pipeline;
}
@@ -443,11 +475,11 @@ bool InternalFlutterGpu_RenderPass_BindTexture(
auto& command = wrapper->GetCommand();
auto uniform_name = tonic::StdStringFromDart(uniform_name_handle);
const impeller::SampledImageSlot* image_slot =
const flutter::gpu::Shader::TextureBinding* texture_binding =
shader->GetUniformTexture(uniform_name);
// TODO(bdero): Return an error string stating that no uniform texture with
// this name exists and throw an exception.
if (!image_slot) {
if (!texture_binding) {
return false;
}
@@ -461,10 +493,10 @@ bool InternalFlutterGpu_RenderPass_BindTexture(
flutter::gpu::ToImpellerSamplerAddressMode(height_address_mode);
const std::unique_ptr<const impeller::Sampler>& sampler =
wrapper->GetContext()->GetSamplerLibrary()->GetSampler(sampler_desc);
return command.BindResource(
shader->GetShaderStage(), impeller::DescriptorType::kSampledImage,
*image_slot, impeller::ShaderMetadata{}, texture->GetTexture(), sampler);
return command.BindResource(shader->GetShaderStage(),
impeller::DescriptorType::kSampledImage,
texture_binding->slot, texture_binding->metadata,
texture->GetTexture(), sampler);
}
void InternalFlutterGpu_RenderPass_ClearBindings(

View File

@@ -42,8 +42,7 @@ fml::RefPtr<Shader> Shader::Make(
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::unordered_map<std::string, TextureBinding> uniform_textures,
std::vector<impeller::DescriptorSetLayout> descriptor_set_layouts) {
auto shader = fml::MakeRefCounted<Shader>();
shader->entrypoint_ = std::move(entrypoint);
@@ -112,7 +111,7 @@ const Shader::UniformBinding* Shader::GetUniformStruct(
return &uniform->second;
}
const impeller::SampledImageSlot* Shader::GetUniformTexture(
const Shader::TextureBinding* Shader::GetUniformTexture(
const std::string& name) const {
auto uniform = uniform_textures_.find(name);
if (uniform == uniform_textures_.end()) {

View File

@@ -34,6 +34,11 @@ class Shader : public RefCountedDartWrappable<Shader> {
const std::string& name) const;
};
struct TextureBinding {
impeller::SampledImageSlot slot;
impeller::ShaderMetadata metadata;
};
~Shader() override;
static fml::RefPtr<Shader> Make(
@@ -43,8 +48,7 @@ class Shader : public RefCountedDartWrappable<Shader> {
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::unordered_map<std::string, TextureBinding> uniform_textures,
std::vector<impeller::DescriptorSetLayout> descriptor_set_layouts);
std::shared_ptr<const impeller::ShaderFunction> GetFunctionFromLibrary(
@@ -63,7 +67,7 @@ class Shader : public RefCountedDartWrappable<Shader> {
const Shader::UniformBinding* GetUniformStruct(const std::string& name) const;
const impeller::SampledImageSlot* GetUniformTexture(
const Shader::TextureBinding* GetUniformTexture(
const std::string& name) const;
private:
@@ -75,7 +79,7 @@ class Shader : public RefCountedDartWrappable<Shader> {
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::unordered_map<std::string, TextureBinding> uniform_textures_;
std::vector<impeller::DescriptorSetLayout> descriptor_set_layouts_;
FML_DISALLOW_COPY_AND_ASSIGN(Shader);

View File

@@ -7,6 +7,7 @@
#include <optional>
#include <utility>
#include "common/graphics/texture.h"
#include "flutter/assets/asset_manager.h"
#include "flutter/lib/gpu/fixtures.h"
#include "flutter/lib/gpu/shader.h"
@@ -165,11 +166,7 @@ static const impeller::fb::shaderbundle::BackendShader* GetShaderBackend(
return shader->metal_desktop();
#endif
case impeller::Context::BackendType::kOpenGLES:
#ifdef FML_OS_ANDROID
return shader->opengl_es();
#else
return shader->opengl_desktop();
#endif
case impeller::Context::BackendType::kVulkan:
return shader->vulkan();
}
@@ -225,7 +222,7 @@ fml::RefPtr<ShaderLibrary> ShaderLibrary::MakeFromFlatbuffer(
.byte_length =
static_cast<size_t>(struct_member->total_size_in_bytes()),
.array_elements =
struct_member->array_elements() != 0
struct_member->array_elements() == 0
? std::optional<size_t>(std::nullopt)
: static_cast<size_t>(struct_member->array_elements()),
});
@@ -256,16 +253,22 @@ fml::RefPtr<ShaderLibrary> ShaderLibrary::MakeFromFlatbuffer(
}
}
std::unordered_map<std::string, impeller::SampledImageSlot>
uniform_textures;
std::unordered_map<std::string, Shader::TextureBinding> uniform_textures;
if (backend_shader->uniform_textures() != nullptr) {
for (const auto& uniform : *backend_shader->uniform_textures()) {
uniform_textures[uniform->name()->str()] = impeller::SampledImageSlot{
Shader::TextureBinding texture_binding;
texture_binding.slot = impeller::SampledImageSlot{
.name = uniform->name()->c_str(),
.texture_index = static_cast<size_t>(uniform->ext_res_0()),
.set = static_cast<size_t>(uniform->set()),
.binding = static_cast<size_t>(uniform->binding()),
};
texture_binding.metadata = impeller::ShaderMetadata{
.name = uniform->name()->c_str(),
.members = {},
};
uniform_textures[uniform->name()->str()] = texture_binding;
descriptor_set_layouts.push_back(impeller::DescriptorSetLayout{
static_cast<uint32_t>(uniform->binding()),