From 2bc62b42f112a6a247dc1dcd7d49a212c624bcaa Mon Sep 17 00:00:00 2001 From: Jason Simmons Date: Thu, 25 Jan 2024 11:48:57 -0800 Subject: [PATCH] [Impeller] Do not emit metadata for structs that are not part of the shader's interface (flutter/engine#50029) Fixes https://github.com/flutter/flutter/issues/138404 --- .../impeller/compiler/compiler_unittests.cc | 8 ++++++++ .../src/flutter/impeller/compiler/reflector.cc | 12 ++++++++++++ engine/src/flutter/impeller/fixtures/BUILD.gn | 1 + .../impeller/fixtures/struct_internal.frag | 16 ++++++++++++++++ 4 files changed, 37 insertions(+) create mode 100644 engine/src/flutter/impeller/fixtures/struct_internal.frag diff --git a/engine/src/flutter/impeller/compiler/compiler_unittests.cc b/engine/src/flutter/impeller/compiler/compiler_unittests.cc index 44ff9ebba8..3e916ff58a 100644 --- a/engine/src/flutter/impeller/compiler/compiler_unittests.cc +++ b/engine/src/flutter/impeller/compiler/compiler_unittests.cc @@ -142,6 +142,14 @@ TEST_P(CompilerTest, SkSLTextureLookUpOrderOfOperations) { "flutter_FragCoord.xy));") != -1); } +TEST_P(CompilerTest, CanCompileStructs) { + if (GetParam() != TargetPlatform::kSkSL) { + GTEST_SKIP() << "Only supported on SkSL"; + } + ASSERT_TRUE(CanCompileAndReflect("struct_internal.frag", + SourceType::kFragmentShader)); +} + #define INSTANTIATE_TARGET_PLATFORM_TEST_SUITE_P(suite_name) \ INSTANTIATE_TEST_SUITE_P( \ suite_name, CompilerTest, \ diff --git a/engine/src/flutter/impeller/compiler/reflector.cc b/engine/src/flutter/impeller/compiler/reflector.cc index 9638c7559b..dd085b25de 100644 --- a/engine/src/flutter/impeller/compiler/reflector.cc +++ b/engine/src/flutter/impeller/compiler/reflector.cc @@ -258,6 +258,18 @@ std::optional Reflector::GenerateTemplateArguments() const { std::set known_structs; ir_->for_each_typed_id( [&](uint32_t, const spirv_cross::SPIRType& type) { + if (type.basetype != spirv_cross::SPIRType::BaseType::Struct) { + return; + } + // Skip structs that do not have layout offset decorations. + // These structs are used internally within the shader and are not + // part of the shader's interface. + for (size_t i = 0; i < type.member_types.size(); i++) { + if (!compiler_->has_member_decoration(type.self, i, + spv::DecorationOffset)) { + return; + } + } if (known_structs.find(type.self) != known_structs.end()) { // Iterating over types this way leads to duplicates which may cause // duplicate struct definitions. diff --git a/engine/src/flutter/impeller/fixtures/BUILD.gn b/engine/src/flutter/impeller/fixtures/BUILD.gn index 0ec39d3c7e..16081a9ba4 100644 --- a/engine/src/flutter/impeller/fixtures/BUILD.gn +++ b/engine/src/flutter/impeller/fixtures/BUILD.gn @@ -101,6 +101,7 @@ test_fixtures("file_fixtures") { "stage1.comp", "stage2.comp", "struct_def_bug.vert", + "struct_internal.frag", "table_mountain_nx.png", "table_mountain_ny.png", "table_mountain_nz.png", diff --git a/engine/src/flutter/impeller/fixtures/struct_internal.frag b/engine/src/flutter/impeller/fixtures/struct_internal.frag new file mode 100644 index 0000000000..5cc75950ee --- /dev/null +++ b/engine/src/flutter/impeller/fixtures/struct_internal.frag @@ -0,0 +1,16 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +out vec4 frag_color; + +struct TestStruct { + float a; + float b; + float c; +}; + +void main() { + TestStruct ts = TestStruct(1.0, 0.5, 0.25); + frag_color = vec4(ts.c, ts.b, ts.a, 1.0); +}