Skip to content

Commit

Permalink
[Impeller] split downsample shader into GLES variant. (#56376)
Browse files Browse the repository at this point in the history
This fixes a crash reported on a moto g4 where the on device shader compiler fails to process the ifdef.
  • Loading branch information
jonahwilliams authored Nov 6, 2024
1 parent 371c8fe commit 58ac1da
Show file tree
Hide file tree
Showing 9 changed files with 279 additions and 50 deletions.
4 changes: 4 additions & 0 deletions ci/licenses_golden/licenses_flutter
Original file line number Diff line number Diff line change
Expand Up @@ -43065,6 +43065,7 @@ ORIGIN: ../../../flutter/impeller/entity/shaders/blending/porter_duff_blend.vert
ORIGIN: ../../../flutter/impeller/entity/shaders/blending/vertices_uber.frag + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/shaders/clip.frag + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/shaders/clip.vert + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/shaders/downsample.glsl + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/shaders/filters/border_mask_blur.frag + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/shaders/filters/color_matrix_color_filter.frag + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/shaders/filters/filter_position.vert + ../../../flutter/LICENSE
Expand Down Expand Up @@ -43093,6 +43094,7 @@ ORIGIN: ../../../flutter/impeller/entity/shaders/runtime_effect.vert + ../../../
ORIGIN: ../../../flutter/impeller/entity/shaders/solid_fill.frag + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/shaders/solid_fill.vert + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/shaders/texture_downsample.frag + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/shaders/texture_downsample_gles.frag + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/shaders/texture_fill.frag + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/shaders/texture_fill.vert + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/shaders/texture_fill_strict_src.frag + ../../../flutter/LICENSE
Expand Down Expand Up @@ -45925,6 +45927,7 @@ FILE: ../../../flutter/impeller/entity/shaders/blending/porter_duff_blend.vert
FILE: ../../../flutter/impeller/entity/shaders/blending/vertices_uber.frag
FILE: ../../../flutter/impeller/entity/shaders/clip.frag
FILE: ../../../flutter/impeller/entity/shaders/clip.vert
FILE: ../../../flutter/impeller/entity/shaders/downsample.glsl
FILE: ../../../flutter/impeller/entity/shaders/filters/border_mask_blur.frag
FILE: ../../../flutter/impeller/entity/shaders/filters/color_matrix_color_filter.frag
FILE: ../../../flutter/impeller/entity/shaders/filters/filter_position.vert
Expand Down Expand Up @@ -45953,6 +45956,7 @@ FILE: ../../../flutter/impeller/entity/shaders/runtime_effect.vert
FILE: ../../../flutter/impeller/entity/shaders/solid_fill.frag
FILE: ../../../flutter/impeller/entity/shaders/solid_fill.vert
FILE: ../../../flutter/impeller/entity/shaders/texture_downsample.frag
FILE: ../../../flutter/impeller/entity/shaders/texture_downsample_gles.frag
FILE: ../../../flutter/impeller/entity/shaders/texture_fill.frag
FILE: ../../../flutter/impeller/entity/shaders/texture_fill.vert
FILE: ../../../flutter/impeller/entity/shaders/texture_fill_strict_src.frag
Expand Down
1 change: 1 addition & 0 deletions impeller/entity/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ impeller_shaders("entity_shaders") {
"shaders/gradients/fast_gradient.vert",
"shaders/gradients/fast_gradient.frag",
"shaders/texture_downsample.frag",
"shaders/texture_downsample_gles.frag",
]
}

Expand Down
12 changes: 8 additions & 4 deletions impeller/entity/contents/content_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,8 @@ ContentContext::ContentContext(
clip_pipelines_.SetDefault(
options,
std::make_unique<ClipPipeline>(*context_, clip_pipeline_descriptor));
texture_downsample_pipelines_.CreateDefault(
*context_, options_trianglestrip, {supports_decal});
texture_downsample_pipelines_.CreateDefault(*context_,
options_trianglestrip);
rrect_blur_pipelines_.CreateDefault(*context_, options_trianglestrip);
texture_strict_src_pipelines_.CreateDefault(*context_, options);
tiled_texture_pipelines_.CreateDefault(*context_, options,
Expand Down Expand Up @@ -454,10 +454,14 @@ ContentContext::ContentContext(
options_trianglestrip);
yuv_to_rgb_filter_pipelines_.CreateDefault(*context_, options_trianglestrip);

// GLES only shader that is unsupported on macOS.
#if defined(IMPELLER_ENABLE_OPENGLES) && !defined(FML_OS_MACOSX)
#if defined(IMPELLER_ENABLE_OPENGLES)
if (GetContext()->GetBackendType() == Context::BackendType::kOpenGLES) {
#if !defined(FML_OS_MACOSX)
// GLES only shader that is unsupported on macOS.
tiled_texture_external_pipelines_.CreateDefault(*context_, options);
#endif // !defined(FML_OS_MACOSX)
texture_downsample_gles_pipelines_.CreateDefault(*context_,
options_trianglestrip);
}
#endif // IMPELLER_ENABLE_OPENGLES

Expand Down
11 changes: 11 additions & 0 deletions impeller/entity/contents/content_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
#include "impeller/entity/vertices_uber.frag.h"

#ifdef IMPELLER_ENABLE_OPENGLES
#include "impeller/entity/texture_downsample_gles.frag.h"
#include "impeller/entity/tiled_texture_fill_external.frag.h"
#endif // IMPELLER_ENABLE_OPENGLES

Expand Down Expand Up @@ -239,6 +240,9 @@ using VerticesUberShader = RenderPipelineHandle<PorterDuffBlendVertexShader,
using TiledTextureExternalPipeline =
RenderPipelineHandle<TextureFillVertexShader,
TiledTextureFillExternalFragmentShader>;
using TextureDownsampleGlesPipeline =
RenderPipelineHandle<TextureFillVertexShader,
TextureDownsampleGlesFragmentShader>;
#endif // IMPELLER_ENABLE_OPENGLES

/// Pipeline state configuration.
Expand Down Expand Up @@ -426,6 +430,11 @@ class ContentContext {
}

#ifdef IMPELLER_ENABLE_OPENGLES
std::shared_ptr<Pipeline<PipelineDescriptor>>
GetDownsampleTextureGlesPipeline(ContentContextOptions opts) const {
return GetPipeline(texture_downsample_gles_pipelines_, opts);
}

std::shared_ptr<Pipeline<PipelineDescriptor>> GetTiledTextureExternalPipeline(
ContentContextOptions opts) const {
FML_DCHECK(GetContext()->GetBackendType() ==
Expand Down Expand Up @@ -873,6 +882,8 @@ class ContentContext {
#ifdef IMPELLER_ENABLE_OPENGLES
mutable Variants<TiledTextureExternalPipeline>
tiled_texture_external_pipelines_;
mutable Variants<TextureDownsampleGlesPipeline>
texture_downsample_gles_pipelines_;
#endif // IMPELLER_ENABLE_OPENGLES
mutable Variants<TiledTexturePipeline> tiled_texture_pipelines_;
mutable Variants<GaussianBlurPipeline> gaussian_blur_pipelines_;
Expand Down
15 changes: 13 additions & 2 deletions impeller/entity/contents/filters/gaussian_blur_filter_contents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,20 @@ fml::StatusOr<RenderTarget> MakeDownsampleSubpass(
pass.SetCommandLabel("Gaussian blur downsample");
auto pipeline_options = OptionsFromPass(pass);
pipeline_options.primitive_type = PrimitiveType::kTriangleStrip;
#ifdef IMPELLER_ENABLE_OPENGLES
// The GLES backend conditionally supports decal tile mode, while
// decal is always supported for Vulkan and Metal.
if (renderer.GetDeviceCapabilities()
.SupportsDecalSamplerAddressMode() ||
tile_mode != Entity::TileMode::kDecal) {
pass.SetPipeline(renderer.GetDownsamplePipeline(pipeline_options));
} else {
pass.SetPipeline(
renderer.GetDownsampleTextureGlesPipeline(pipeline_options));
}
#else
pass.SetPipeline(renderer.GetDownsamplePipeline(pipeline_options));
#endif // IMPELLER_ENABLE_OPENGLES

TextureFillVertexShader::FrameInfo frame_info;
frame_info.mvp = Matrix::MakeOrthographic(ISize(1, 1));
Expand All @@ -407,8 +420,6 @@ fml::StatusOr<RenderTarget> MakeDownsampleSubpass(
frag_info.edge = edge;
frag_info.ratio = ratio;
frag_info.pixel_size = Vector2(1.0f / Size(input_texture->GetSize()));
frag_info.use_decal =
(tile_mode == Entity::TileMode::kDecal) ? 1.0 : 0.0;

const Quad& uvs = pass_args.uvs;
std::array<VS::PerVertexData, 4> vertices = {
Expand Down
44 changes: 44 additions & 0 deletions impeller/entity/shaders/downsample.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// 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.

precision mediump float;

#include <impeller/constants.glsl>
#include <impeller/types.glsl>

uniform f16sampler2D texture_sampler;

uniform FragInfo {
float edge;
float ratio;
vec2 pixel_size;
}
frag_info;

in highp vec2 v_texture_coords;

out vec4 frag_color;

vec4 Sample(vec2 uv) {
#ifdef SUPPORTS_DECAL
return texture(texture_sampler, uv, float16_t(kDefaultMipBias));
#else
if ((uv.x < 0 || uv.y < 0 || uv.x > 1 || uv.y > 1)) {
return vec4(0);
} else {
return texture(texture_sampler, uv, float16_t(kDefaultMipBias));
}
#endif
}

void main() {
vec4 total = vec4(0.0);
for (float i = -frag_info.edge; i <= frag_info.edge; i += 2) {
for (float j = -frag_info.edge; j <= frag_info.edge; j += 2) {
total += (Sample(v_texture_coords + frag_info.pixel_size * vec2(i, j)) *
frag_info.ratio);
}
}
frag_color = total;
}
45 changes: 2 additions & 43 deletions impeller/entity/shaders/texture_downsample.frag
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

precision mediump float;
#define SUPPORTS_DECAL 1

#include <impeller/constants.glsl>
#include <impeller/types.glsl>

layout(constant_id = 0) const float supports_decal = 1.0;

uniform f16sampler2D texture_sampler;

uniform FragInfo {
float edge;
float ratio;
float use_decal;
vec2 pixel_size;
}
frag_info;

in highp vec2 v_texture_coords;

out vec4 frag_color;

vec4 Sample(vec2 uv) {
if (supports_decal == 1.0) {
return texture(texture_sampler, uv, float16_t(kDefaultMipBias));
} else {
if (frag_info.use_decal == 1.0 &&
(uv.x < 0 || uv.y < 0 || uv.x > 1 || uv.y > 1)) {
return vec4(0);
} else {
return texture(texture_sampler, uv, float16_t(kDefaultMipBias));
}
}
}

void main() {
vec4 total = vec4(0.0);
for (float i = -frag_info.edge; i <= frag_info.edge; i += 2) {
for (float j = -frag_info.edge; j <= frag_info.edge; j += 2) {
total += (Sample(v_texture_coords + frag_info.pixel_size * vec2(i, j)) *
frag_info.ratio);
}
}
frag_color = total;
}
#include <downsample.glsl>
5 changes: 5 additions & 0 deletions impeller/entity/shaders/texture_downsample_gles.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// 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.

#include <downsample.glsl>
Loading

0 comments on commit 58ac1da

Please sign in to comment.