From 8b3f5f4a2f7d9699c158fe8257d9dc4b0d7d49f7 Mon Sep 17 00:00:00 2001 From: gaaclarke <30870216+gaaclarke@users.noreply.github.com> Date: Wed, 26 Feb 2025 09:36:00 -0800 Subject: [PATCH] Added compilation failure if a max ubo size is exceeded (#164038) issue: https://github.com/flutter/flutter/issues/163580 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [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. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [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/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md --- .../src/flutter/impeller/compiler/compiler.cc | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/engine/src/flutter/impeller/compiler/compiler.cc b/engine/src/flutter/impeller/compiler/compiler.cc index 9d7fdbf152..a751f5f6dd 100644 --- a/engine/src/flutter/impeller/compiler/compiler.cc +++ b/engine/src/flutter/impeller/compiler/compiler.cc @@ -32,6 +32,11 @@ constexpr const char* kEGLImageExternalExtension300 = "GL_OES_EGL_image_external_essl3"; } // namespace +// This value should be <= 7372. UBOs can be larger on some devices but a +// performance cost will be paid. +// https://docs.qualcomm.com/bundle/publicresource/topics/80-78185-2/best_practices.html?product=1601111740035277#buffer-best-practices +static const uint32_t kMaxUniformBufferSize = 6208; + static uint32_t ParseMSLVersion(const std::string& msl_version) { std::stringstream sstream(msl_version); std::string version_part; @@ -257,6 +262,21 @@ static CompilerBackend CreateCompiler(const spirv_cross::ParsedIR& ir, return compiler; } +namespace { +uint32_t CalculateUBOSize(const spirv_cross::Compiler* compiler) { + spirv_cross::ShaderResources resources = compiler->get_shader_resources(); + uint32_t result = 0; + for (const spirv_cross::Resource& ubo : resources.uniform_buffers) { + const spirv_cross::SPIRType& ubo_type = + compiler->get_type(ubo.base_type_id); + uint32_t size = compiler->get_declared_struct_size(ubo_type); + result += size; + } + return result; +} + +} // namespace + Compiler::Compiler(const std::shared_ptr& source_mapping, const SourceOptions& source_options, Reflector::Options reflector_options) @@ -410,6 +430,13 @@ Compiler::Compiler(const std::shared_ptr& source_mapping, return; } + uint32_t ubo_size = CalculateUBOSize(sl_compiler.GetCompiler()); + if (ubo_size > kMaxUniformBufferSize) { + COMPILER_ERROR(error_stream_) << "Uniform buffer size exceeds max (" + << kMaxUniformBufferSize << "): " << ubo_size; + return; + } + // We need to invoke the compiler even if we don't use the SL mapping later // for Vulkan. The reflector needs information that is only valid after a // successful compilation call.