Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

[Impeller] Use separate atlases and shaders for color and alpha #41780

Merged
merged 13 commits into from
May 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions ci/licenses_golden/licenses_flutter
Original file line number Diff line number Diff line change
Expand Up @@ -1297,8 +1297,7 @@ ORIGIN: ../../../flutter/impeller/entity/shaders/gaussian_blur/gaussian_blur_noa
ORIGIN: ../../../flutter/impeller/entity/shaders/gaussian_blur/gaussian_blur_noalpha_nodecal.frag + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/shaders/glyph_atlas.frag + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/shaders/glyph_atlas.vert + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/shaders/glyph_atlas_sdf.frag + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/shaders/glyph_atlas_sdf.vert + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/shaders/glyph_atlas_color.frag + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/shaders/gradient_fill.vert + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/shaders/linear_gradient_fill.frag + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/shaders/linear_gradient_ssbo_fill.frag + ../../../flutter/LICENSE
Expand Down Expand Up @@ -3898,8 +3897,7 @@ FILE: ../../../flutter/impeller/entity/shaders/gaussian_blur/gaussian_blur_noalp
FILE: ../../../flutter/impeller/entity/shaders/gaussian_blur/gaussian_blur_noalpha_nodecal.frag
FILE: ../../../flutter/impeller/entity/shaders/glyph_atlas.frag
FILE: ../../../flutter/impeller/entity/shaders/glyph_atlas.vert
FILE: ../../../flutter/impeller/entity/shaders/glyph_atlas_sdf.frag
FILE: ../../../flutter/impeller/entity/shaders/glyph_atlas_sdf.vert
FILE: ../../../flutter/impeller/entity/shaders/glyph_atlas_color.frag
FILE: ../../../flutter/impeller/entity/shaders/gradient_fill.vert
FILE: ../../../flutter/impeller/entity/shaders/linear_gradient_fill.frag
FILE: ../../../flutter/impeller/entity/shaders/linear_gradient_ssbo_fill.frag
Expand Down
3 changes: 1 addition & 2 deletions impeller/entity/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,8 @@ impeller_shaders("entity_shaders") {
"shaders/gaussian_blur/gaussian_blur_noalpha_decal.frag",
"shaders/gaussian_blur/gaussian_blur_noalpha_nodecal.frag",
"shaders/glyph_atlas.frag",
"shaders/glyph_atlas_color.frag",
"shaders/glyph_atlas.vert",
"shaders/glyph_atlas_sdf.frag",
"shaders/glyph_atlas_sdf.vert",
"shaders/gradient_fill.vert",
"shaders/linear_to_srgb_filter.frag",
"shaders/linear_to_srgb_filter.vert",
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 @@ -164,7 +164,8 @@ static std::unique_ptr<PipelineT> CreateDefaultPipeline(
ContentContext::ContentContext(std::shared_ptr<Context> context)
: context_(std::move(context)),
tessellator_(std::make_shared<Tessellator>()),
glyph_atlas_context_(std::make_shared<GlyphAtlasContext>()),
alpha_glyph_atlas_context_(std::make_shared<GlyphAtlasContext>()),
color_glyph_atlas_context_(std::make_shared<GlyphAtlasContext>()),
scene_context_(std::make_shared<scene::SceneContext>(context_)) {
if (!context_ || !context_->IsValid()) {
return;
Expand Down Expand Up @@ -286,6 +287,8 @@ ContentContext::ContentContext(std::shared_ptr<Context> context)
CreateDefaultPipeline<SrgbToLinearFilterPipeline>(*context_);
glyph_atlas_pipelines_[{}] =
CreateDefaultPipeline<GlyphAtlasPipeline>(*context_);
glyph_atlas_color_pipelines_[{}] =
CreateDefaultPipeline<GlyphAtlasColorPipeline>(*context_);
geometry_color_pipelines_[{}] =
CreateDefaultPipeline<GeometryColorPipeline>(*context_);
yuv_to_rgb_filter_pipelines_[{}] =
Expand Down Expand Up @@ -378,9 +381,10 @@ std::shared_ptr<Tessellator> ContentContext::GetTessellator() const {
return tessellator_;
}

std::shared_ptr<GlyphAtlasContext> ContentContext::GetGlyphAtlasContext()
const {
return glyph_atlas_context_;
std::shared_ptr<GlyphAtlasContext> ContentContext::GetGlyphAtlasContext(
GlyphAtlas::Type type) const {
return type == GlyphAtlas::Type::kAlphaBitmap ? alpha_glyph_atlas_context_
: color_glyph_atlas_context_;
}

std::shared_ptr<Context> ContentContext::GetContext() const {
Expand Down
15 changes: 13 additions & 2 deletions impeller/entity/contents/content_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "impeller/entity/conical_gradient_fill.frag.h"
#include "impeller/entity/glyph_atlas.frag.h"
#include "impeller/entity/glyph_atlas.vert.h"
#include "impeller/entity/glyph_atlas_color.frag.h"
#include "impeller/entity/gradient_fill.vert.h"
#include "impeller/entity/linear_gradient_fill.frag.h"
#include "impeller/entity/linear_to_srgb_filter.frag.h"
Expand Down Expand Up @@ -171,6 +172,8 @@ using SrgbToLinearFilterPipeline =
SrgbToLinearFilterFragmentShader>;
using GlyphAtlasPipeline =
RenderPipelineT<GlyphAtlasVertexShader, GlyphAtlasFragmentShader>;
using GlyphAtlasColorPipeline =
RenderPipelineT<GlyphAtlasVertexShader, GlyphAtlasColorFragmentShader>;
using PorterDuffBlendPipeline =
RenderPipelineT<BlendVertexShader, PorterDuffBlendFragmentShader>;
// Instead of requiring new shaders for clips, the solid fill stages are used
Expand Down Expand Up @@ -469,6 +472,11 @@ class ContentContext {
return GetPipeline(glyph_atlas_pipelines_, opts);
}

std::shared_ptr<Pipeline<PipelineDescriptor>> GetGlyphAtlasColorPipeline(
ContentContextOptions opts) const {
return GetPipeline(glyph_atlas_color_pipelines_, opts);
}

std::shared_ptr<Pipeline<PipelineDescriptor>> GetGeometryColorPipeline(
ContentContextOptions opts) const {
return GetPipeline(geometry_color_pipelines_, opts);
Expand Down Expand Up @@ -654,7 +662,8 @@ class ContentContext {

std::shared_ptr<Context> GetContext() const;

std::shared_ptr<GlyphAtlasContext> GetGlyphAtlasContext() const;
std::shared_ptr<GlyphAtlasContext> GetGlyphAtlasContext(
GlyphAtlas::Type type) const;

const Capabilities& GetDeviceCapabilities() const;

Expand Down Expand Up @@ -722,6 +731,7 @@ class ContentContext {
mutable Variants<SrgbToLinearFilterPipeline> srgb_to_linear_filter_pipelines_;
mutable Variants<ClipPipeline> clip_pipelines_;
mutable Variants<GlyphAtlasPipeline> glyph_atlas_pipelines_;
mutable Variants<GlyphAtlasColorPipeline> glyph_atlas_color_pipelines_;
mutable Variants<GeometryColorPipeline> geometry_color_pipelines_;
mutable Variants<YUVToRGBFilterPipeline> yuv_to_rgb_filter_pipelines_;
mutable Variants<PorterDuffBlendPipeline> porter_duff_blend_pipelines_;
Expand Down Expand Up @@ -813,7 +823,8 @@ class ContentContext {

bool is_valid_ = false;
std::shared_ptr<Tessellator> tessellator_;
std::shared_ptr<GlyphAtlasContext> glyph_atlas_context_;
std::shared_ptr<GlyphAtlasContext> alpha_glyph_atlas_context_;
std::shared_ptr<GlyphAtlasContext> color_glyph_atlas_context_;
std::shared_ptr<scene::SceneContext> scene_context_;
bool wireframe_ = false;

Expand Down
23 changes: 8 additions & 15 deletions impeller/entity/contents/text_contents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ static bool CommonRender(
frag_info.text_color = ToVector(color.Premultiply());
FS::BindFragInfo(cmd, pass.GetTransientsBuffer().EmplaceUniform(frag_info));

// Common fragment uniforms for all glyphs.
FS::BindGlyphAtlasSampler(
cmd, // command
atlas->GetTexture(), // texture
Expand Down Expand Up @@ -198,9 +197,6 @@ static bool CommonRender(
point * glyph_position.glyph.bounds.size);
}
vtx.uv = uv_origin + point * uv_size;
vtx.has_color =
glyph_position.glyph.type == Glyph::Type::kBitmap ? 1.0 : 0.0;

vertex_builder.AppendVertex(vtx);
}
}
Expand All @@ -224,16 +220,9 @@ bool TextContents::Render(const ContentContext& renderer,
return true;
}

// This TextContents may be for a frame that doesn't have color, but the
// lazy atlas for this scene already does have color.
// Benchmarks currently show that creating two atlases per pass regresses
// render time. This should get re-evaluated if we start caching atlases
// between frames or get significantly faster at creating atlases, because
// we're potentially trading memory for time here.
auto atlas =
ResolveAtlas(lazy_atlas_->HasColor() ? GlyphAtlas::Type::kColorBitmap
: GlyphAtlas::Type::kAlphaBitmap,
renderer.GetGlyphAtlasContext(), renderer.GetContext());
auto type = frame_.GetAtlasType();
auto atlas = ResolveAtlas(type, renderer.GetGlyphAtlasContext(type),
renderer.GetContext());

if (!atlas || !atlas->IsValid()) {
VALIDATION_LOG << "Cannot render glyphs without prepared atlas.";
Expand All @@ -245,7 +234,11 @@ bool TextContents::Render(const ContentContext& renderer,
cmd.label = "TextFrame";
auto opts = OptionsFromPassAndEntity(pass, entity);
opts.primitive_type = PrimitiveType::kTriangle;
cmd.pipeline = renderer.GetGlyphAtlasPipeline(opts);
if (type == GlyphAtlas::Type::kAlphaBitmap) {
cmd.pipeline = renderer.GetGlyphAtlasPipeline(opts);
} else {
cmd.pipeline = renderer.GetGlyphAtlasColorPipeline(opts);
}
cmd.stencil_reference = entity.GetStencilDepth();

return CommonRender(renderer, entity, pass, color, frame_, offset_, atlas,
Expand Down
15 changes: 5 additions & 10 deletions impeller/entity/shaders/glyph_atlas.frag
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,18 @@

#include <impeller/types.glsl>

uniform sampler2D glyph_atlas_sampler;
uniform f16sampler2D glyph_atlas_sampler;

uniform FragInfo {
vec4 text_color;
f16vec4 text_color;
}
frag_info;

in vec2 v_uv;
in float v_has_color;

out vec4 frag_color;
out f16vec4 frag_color;

void main() {
vec4 value = texture(glyph_atlas_sampler, v_uv);
if (v_has_color != 1.0) {
frag_color = value.aaaa * frag_info.text_color;
} else {
frag_color = value * frag_info.text_color.a;
}
f16vec4 value = texture(glyph_atlas_sampler, v_uv);
frag_color = value.aaaa * frag_info.text_color;
}
5 changes: 1 addition & 4 deletions impeller/entity/shaders/glyph_atlas.vert
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,12 @@ uniform FrameInfo {
}
frame_info;

in vec4 position;
in highp vec4 position;
in vec2 uv;
in float has_color;

out vec2 v_uv;
out float v_has_color;

void main() {
gl_Position = frame_info.mvp * position;
v_uv = uv;
v_has_color = has_color;
}
21 changes: 21 additions & 0 deletions impeller/entity/shaders/glyph_atlas_color.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// 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 <impeller/types.glsl>

uniform f16sampler2D glyph_atlas_sampler;

uniform FragInfo {
f16vec4 text_color;
}
frag_info;

in vec2 v_uv;

out f16vec4 frag_color;

void main() {
f16vec4 value = texture(glyph_atlas_sampler, v_uv);
frag_color = value * frag_info.text_color.aaaa;
}
35 changes: 0 additions & 35 deletions impeller/entity/shaders/glyph_atlas_sdf.frag

This file was deleted.

22 changes: 0 additions & 22 deletions impeller/entity/shaders/glyph_atlas_sdf.vert

This file was deleted.

Loading