From 9e10ab5bc45b2c9ba49dabb740f3b85d512ceaf1 Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Wed, 23 Mar 2022 12:53:01 -0700 Subject: [PATCH] Batch all draw calls in the entire text frame. (flutter/engine#100) Also updates comments that were first written when this wasn't batched. For some reason, in the original implementation, I had assumed that styling information was not already in a uniform. --- .../impeller/entity/contents/text_contents.cc | 47 +++++++------------ 1 file changed, 18 insertions(+), 29 deletions(-) diff --git a/engine/src/flutter/impeller/entity/contents/text_contents.cc b/engine/src/flutter/impeller/entity/contents/text_contents.cc index d61c0d6a07..018cebb661 100644 --- a/engine/src/flutter/impeller/entity/contents/text_contents.cc +++ b/engine/src/flutter/impeller/entity/contents/text_contents.cc @@ -67,7 +67,7 @@ bool TextContents::Render(const ContentContext& renderer, // Information shared by all glyph draw calls. Command cmd; - cmd.label = "TextRun"; + cmd.label = "TextFrame"; cmd.primitive_type = PrimitiveType::kTriangle; cmd.pipeline = renderer.GetGlyphAtlasPipeline(OptionsFromPassAndEntity(pass, entity)); @@ -91,13 +91,11 @@ bool TextContents::Render(const ContentContext& renderer, ); // Common vertex information for all glyphs. - // Currently, glyphs are being drawn individually. This can be batched later. - // But we don't want to give each glyph unique vertex information. So all - // glyphs are given the same vertex information in the form of a unit-sized - // quad. The size of the glyph is specified in uniform data and the vertex - // shader uses this to size the glyph correctly. The interpolated vertex - // information is also used in the fragment shader to sample from the glyph - // atlas. + // All glyphs are given the same vertex information in the form of a + // unit-sized quad. The size of the glyph is specified in per instance data + // and the vertex shader uses this to size the glyph correctly. The + // interpolated vertex information is also used in the fragment shader to + // sample from the glyph atlas. { VertexBufferBuilder vertex_builder; if (!Tessellator{}.Tessellate( @@ -127,17 +125,9 @@ bool TextContents::Render(const ContentContext& renderer, std::vector atlas_positions; std::vector atlas_glyph_sizes; - // Iterate through all the runs in the blob. for (const auto& run : frame_.GetRuns()) { - instance_count = 0u; - glyph_positions.clear(); - glyph_sizes.clear(); - atlas_positions.clear(); - atlas_glyph_sizes.clear(); - auto font = run.GetFont(); auto glyph_size = ISize::Ceil(font.GetMetrics().GetBoundingBox().size); - // Draw each glyph individually. This should probably be batched. for (const auto& glyph_position : run.GetGlyphPositions()) { FontGlyphPair font_glyph_pair{font, glyph_position.glyph}; auto atlas_glyph_pos = atlas->FindFontGlyphPosition(font_glyph_pair); @@ -154,21 +144,20 @@ bool TextContents::Render(const ContentContext& renderer, atlas_glyph_sizes.emplace_back( Point{atlas_glyph_pos->size.width, atlas_glyph_pos->size.height}); } + } - cmd.instance_count = instance_count; - VS::BindGlyphPositions( - cmd, pass.GetTransientsBuffer().EmplaceStorageBuffer(glyph_positions)); - VS::BindGlyphSizes( - cmd, pass.GetTransientsBuffer().EmplaceStorageBuffer(glyph_sizes)); - VS::BindAtlasPositions( - cmd, pass.GetTransientsBuffer().EmplaceStorageBuffer(atlas_positions)); - VS::BindAtlasGlyphSizes( - cmd, - pass.GetTransientsBuffer().EmplaceStorageBuffer(atlas_glyph_sizes)); + cmd.instance_count = instance_count; + VS::BindGlyphPositions( + cmd, pass.GetTransientsBuffer().EmplaceStorageBuffer(glyph_positions)); + VS::BindGlyphSizes( + cmd, pass.GetTransientsBuffer().EmplaceStorageBuffer(glyph_sizes)); + VS::BindAtlasPositions( + cmd, pass.GetTransientsBuffer().EmplaceStorageBuffer(atlas_positions)); + VS::BindAtlasGlyphSizes( + cmd, pass.GetTransientsBuffer().EmplaceStorageBuffer(atlas_glyph_sizes)); - if (!pass.AddCommand(cmd)) { - return false; - } + if (!pass.AddCommand(cmd)) { + return false; } return true;