From 2773965a3c0e1bf4e59d2dcf8f02ca3c98c80f3c Mon Sep 17 00:00:00 2001 From: Jonah Williams Date: Wed, 11 Jun 2025 16:06:05 -0700 Subject: [PATCH] [impellerc] add GLES shader define. (#170375) Fixes https://github.com/flutter/flutter/issues/169429 For ImageShader.filter we need a way to distinguish between the coordinate system of GL vs Metal/Vulkan. I documented in https://github.com/flutter/flutter/pull/169761 how to do it but neglected to wire up the define. This fixes that. --- .../src/flutter/impeller/compiler/compiler.cc | 5 ++++ .../impeller/compiler/compiler_unittests.cc | 24 +++++++++++++++++++ engine/src/flutter/impeller/fixtures/BUILD.gn | 1 + .../fixtures/check_gles_definition.frag | 13 ++++++++++ 4 files changed, 43 insertions(+) create mode 100644 engine/src/flutter/impeller/fixtures/check_gles_definition.frag diff --git a/engine/src/flutter/impeller/compiler/compiler.cc b/engine/src/flutter/impeller/compiler/compiler.cc index a751f5f6dd3..739da4dce15 100644 --- a/engine/src/flutter/impeller/compiler/compiler.cc +++ b/engine/src/flutter/impeller/compiler/compiler.cc @@ -363,6 +363,11 @@ Compiler::Compiler(const std::shared_ptr& source_mapping, spirv_options.target = target; spirv_options.macro_definitions.push_back("IMPELLER_GRAPHICS_BACKEND"); + if (source_options.target_platform == TargetPlatform::kRuntimeStageGLES || + source_options.target_platform == + TargetPlatform::kRuntimeStageGLES3) { + spirv_options.macro_definitions.push_back("IMPELLER_TARGET_OPENGLES"); + } } break; case TargetPlatform::kSkSL: { SPIRVCompilerTargetEnv target; diff --git a/engine/src/flutter/impeller/compiler/compiler_unittests.cc b/engine/src/flutter/impeller/compiler/compiler_unittests.cc index afea65674c0..981dcfa6c8f 100644 --- a/engine/src/flutter/impeller/compiler/compiler_unittests.cc +++ b/engine/src/flutter/impeller/compiler/compiler_unittests.cc @@ -15,6 +15,30 @@ namespace impeller { namespace compiler { namespace testing { +TEST(CompilerTest, Defines) { + std::shared_ptr fixture = + flutter::testing::OpenFixtureAsMapping("check_gles_definition.frag"); + + SourceOptions options; + options.source_language = SourceLanguage::kGLSL; + options.target_platform = TargetPlatform::kRuntimeStageGLES; + options.entry_point_name = "main"; + options.type = SourceType::kFragmentShader; + + Reflector::Options reflector_options; + reflector_options.target_platform = TargetPlatform::kRuntimeStageGLES; + Compiler compiler = Compiler(fixture, options, reflector_options); + + // Should fail as the shader has a compilation error in it. + EXPECT_EQ(compiler.GetSPIRVAssembly(), nullptr); + + // Should succeed as the compilation error is ifdef'd out. + options.target_platform = TargetPlatform::kRuntimeStageVulkan; + reflector_options.target_platform = TargetPlatform::kRuntimeStageVulkan; + Compiler compiler_2 = Compiler(fixture, options, reflector_options); + EXPECT_NE(compiler_2.GetSPIRVAssembly(), nullptr); +} + TEST(CompilerTest, ShaderKindMatchingIsSuccessful) { ASSERT_EQ(SourceTypeFromFileName("hello.vert"), SourceType::kVertexShader); ASSERT_EQ(SourceTypeFromFileName("hello.frag"), SourceType::kFragmentShader); diff --git a/engine/src/flutter/impeller/fixtures/BUILD.gn b/engine/src/flutter/impeller/fixtures/BUILD.gn index 212ef7e44b6..6135eac0db7 100644 --- a/engine/src/flutter/impeller/fixtures/BUILD.gn +++ b/engine/src/flutter/impeller/fixtures/BUILD.gn @@ -131,6 +131,7 @@ test_fixtures("file_fixtures") { "sample.comp", "sample.frag", "sample.vert", + "check_gles_definition.frag", "sample_with_binding.vert", "simple.vert.hlsl", "sa%m#ple.vert", diff --git a/engine/src/flutter/impeller/fixtures/check_gles_definition.frag b/engine/src/flutter/impeller/fixtures/check_gles_definition.frag new file mode 100644 index 00000000000..0d13097a71c --- /dev/null +++ b/engine/src/flutter/impeller/fixtures/check_gles_definition.frag @@ -0,0 +1,13 @@ +// 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 out_color; + +void main() { +#ifdef IMPELLER_TARGET_OPENGLES + fail +#else + out_color = vec4(1, 0, 0, 0); +#endif +}