From 000ab23052c745ce82cb5937c1abe13fe1c5afe0 Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Wed, 18 Aug 2021 18:45:15 -0700 Subject: [PATCH] Add a tessellator. --- .../src/flutter/impeller/compositor/BUILD.gn | 3 + .../src/flutter/impeller/compositor/formats.h | 5 + .../impeller/compositor/tessellator.cc | 114 ++++++++++++++++++ .../flutter/impeller/compositor/tessellator.h | 43 +++++++ .../impeller/geometry/path_component.h | 8 +- .../flutter/impeller/playground/playground.mm | 3 +- .../primitives/primitives_unittests.mm | 72 +++++++++++ 7 files changed, 243 insertions(+), 5 deletions(-) create mode 100644 engine/src/flutter/impeller/compositor/tessellator.cc create mode 100644 engine/src/flutter/impeller/compositor/tessellator.h diff --git a/engine/src/flutter/impeller/compositor/BUILD.gn b/engine/src/flutter/impeller/compositor/BUILD.gn index 891141ee0a..62ee7c9040 100644 --- a/engine/src/flutter/impeller/compositor/BUILD.gn +++ b/engine/src/flutter/impeller/compositor/BUILD.gn @@ -58,6 +58,8 @@ impeller_component("compositor") { "shader_types.h", "surface.h", "surface.mm", + "tessellator.cc", + "tessellator.h", "texture.h", "texture.mm", "texture_descriptor.h", @@ -91,5 +93,6 @@ source_set("compositor_unittests") { ":compositor", "../playground", "//flutter/testing:testing_lib", + "//third_party/libtess2", ] } diff --git a/engine/src/flutter/impeller/compositor/formats.h b/engine/src/flutter/impeller/compositor/formats.h index 24a137ee3e..2f868733b4 100644 --- a/engine/src/flutter/impeller/compositor/formats.h +++ b/engine/src/flutter/impeller/compositor/formats.h @@ -100,6 +100,11 @@ enum class TextureUsage : TextureUsageMask { kRenderTarget, }; +enum class WindingOrder { + kClockwise, + kCounterClockwise, +}; + enum class PrimitiveType { kTriangle, kTriangleStrip, diff --git a/engine/src/flutter/impeller/compositor/tessellator.cc b/engine/src/flutter/impeller/compositor/tessellator.cc new file mode 100644 index 0000000000..386f72e7c3 --- /dev/null +++ b/engine/src/flutter/impeller/compositor/tessellator.cc @@ -0,0 +1,114 @@ +// 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/compositor/tessellator.h" + +#include "third_party/libtess2/Include/tesselator.h" + +namespace impeller { + +Tessellator::Tessellator() {} + +Tessellator::~Tessellator() {} + +void Tessellator::SetFillType(FillType winding) { + fill_type_ = winding; +} + +Tessellator::FillType Tessellator::GetFillType() const { + return fill_type_; +} + +static int ToTessWindingRule(Tessellator::FillType fill_type) { + switch (fill_type) { + case Tessellator::FillType::kOdd: + return TESS_WINDING_ODD; + case Tessellator::FillType::kNonZero: + return TESS_WINDING_NONZERO; + case Tessellator::FillType::kPositive: + return TESS_WINDING_POSITIVE; + case Tessellator::FillType::kNegative: + return TESS_WINDING_NEGATIVE; + case Tessellator::FillType::kAbsGeqTwo: + return TESS_WINDING_ABS_GEQ_TWO; + } + return TESS_WINDING_ODD; +} + +static void DestroyTessellator(TESStesselator* tessellator) { + if (tessellator != nullptr) { + ::tessDeleteTess(tessellator); + } +} + +bool Tessellator::Tessellate(const std::vector& contours, + VertexCallback callback) const { + if (!callback) { + return false; + } + + using CTessellator = + std::unique_ptr; + + CTessellator tessellator( + ::tessNewTess(nullptr /* the default ::malloc based allocator */), + DestroyTessellator); + + if (!tessellator) { + return false; + } + + constexpr int kVertexSize = 2; + constexpr int kPolygonSize = 3; + + //---------------------------------------------------------------------------- + /// Feed contour information to the tessellator. + /// + static_assert(sizeof(Point) == 2 * sizeof(float)); + ::tessAddContour(tessellator.get(), // the C tessellator + kVertexSize, // + contours.data(), // + sizeof(Point), // + contours.size() // + ); + + //---------------------------------------------------------------------------- + /// Let's tessellate. + /// + auto result = ::tessTesselate(tessellator.get(), // tessellator + ToTessWindingRule(fill_type_), // winding + TESS_POLYGONS, // element type + kPolygonSize, // polygon size + kVertexSize, // vertex size + nullptr // normal (null is automatic) + ); + + if (result != 1) { + return false; + } + + std::vector points; + std::vector indices; + + int vertexItemCount = tessGetVertexCount(tessellator.get()) * kVertexSize; + auto vertices = tessGetVertices(tessellator.get()); + for (int i = 0; i < vertexItemCount; i += 2) { + points.emplace_back(vertices[i], vertices[i + 1]); + } + + int elementItemCount = tessGetElementCount(tessellator.get()) * kPolygonSize; + auto elements = tessGetElements(tessellator.get()); + for (int i = 0; i < elementItemCount; i++) { + indices.emplace_back(elements[i]); + } + + for (auto index : indices) { + auto vtx = points[index]; + callback(vtx); + } + + return true; +} + +} // namespace impeller diff --git a/engine/src/flutter/impeller/compositor/tessellator.h b/engine/src/flutter/impeller/compositor/tessellator.h new file mode 100644 index 0000000000..24f05c3878 --- /dev/null +++ b/engine/src/flutter/impeller/compositor/tessellator.h @@ -0,0 +1,43 @@ +// 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. + +#pragma once + +#include +#include + +#include "flutter/fml/macros.h" +#include "impeller/geometry/point.h" + +namespace impeller { + +class Tessellator { + public: + enum class FillType { + kOdd, // The default winding order. + kNonZero, + kPositive, + kNegative, + kAbsGeqTwo, + }; + + Tessellator(); + + ~Tessellator(); + + void SetFillType(FillType winding); + + FillType GetFillType() const; + + using VertexCallback = std::function; + [[nodiscard]] bool Tessellate(const std::vector& vertices, + VertexCallback callback) const; + + private: + FillType fill_type_ = FillType::kOdd; + + FML_DISALLOW_COPY_AND_ASSIGN(Tessellator); +}; + +} // namespace impeller diff --git a/engine/src/flutter/impeller/geometry/path_component.h b/engine/src/flutter/impeller/geometry/path_component.h index effc7d9822..cda5258eaa 100644 --- a/engine/src/flutter/impeller/geometry/path_component.h +++ b/engine/src/flutter/impeller/geometry/path_component.h @@ -12,10 +12,10 @@ namespace impeller { struct SmoothingApproximation { - const Scalar scale; - const Scalar angle_tolerance; - const Scalar cusp_limit; - const Scalar distance_tolerance_square; + Scalar scale; + Scalar angle_tolerance; + Scalar cusp_limit; + Scalar distance_tolerance_square; SmoothingApproximation(/* default */) : SmoothingApproximation(1.0 /* scale */, diff --git a/engine/src/flutter/impeller/playground/playground.mm b/engine/src/flutter/impeller/playground/playground.mm index bef2d41a49..3373594c05 100644 --- a/engine/src/flutter/impeller/playground/playground.mm +++ b/engine/src/flutter/impeller/playground/playground.mm @@ -33,7 +33,8 @@ static std::string ShaderLibraryDirectory() { return fml::paths::JoinPaths({path_result.second, "shaders"}); } -Playground::Playground() : renderer_(ShaderLibraryDirectory()) {} +Playground::Playground() + : renderer_(ShaderLibraryDirectory(), "impeller.metallib") {} Playground::~Playground() = default; diff --git a/engine/src/flutter/impeller/primitives/primitives_unittests.mm b/engine/src/flutter/impeller/primitives/primitives_unittests.mm index a8e55faf0c..e9690fbfae 100644 --- a/engine/src/flutter/impeller/primitives/primitives_unittests.mm +++ b/engine/src/flutter/impeller/primitives/primitives_unittests.mm @@ -7,10 +7,13 @@ #include "impeller/compositor/command.h" #include "impeller/compositor/command_buffer.h" #include "impeller/compositor/pipeline_builder.h" +#include "impeller/compositor/pipeline_library.h" #include "impeller/compositor/renderer.h" #include "impeller/compositor/sampler_descriptor.h" #include "impeller/compositor/surface.h" +#include "impeller/compositor/tessellator.h" #include "impeller/compositor/vertex_buffer_builder.h" +#include "impeller/geometry/path_builder.h" #include "impeller/image/compressed_image.h" #include "impeller/image/decompressed_image.h" #include "impeller/playground/playground.h" @@ -248,5 +251,74 @@ TEST_F(PrimitivesTest, CanRenderToTexture) { ASSERT_TRUE(r2t_pass->Commit(*context->GetTransientsAllocator())); } +TEST_F(PrimitivesTest, CanRenderPath) { + auto path = PathBuilder{}.AddRect({10, 10, 100, 100}).CreatePath(); + ASSERT_FALSE(path.GetBoundingBox().IsZero()); + + using BoxPipeline = PipelineT; + using VS = BoxFadeVertexShader; + using FS = BoxFadeFragmentShader; + + BoxPipeline box_pipeline(*GetContext()); + + // Vertex buffer. + VertexBufferBuilder vertex_builder; + vertex_builder.SetLabel("Box"); + + Tessellator tessellator; + ASSERT_TRUE(tessellator.Tessellate( + path.SubdivideAdaptively({}), [&vertex_builder](Point point) { + VS::PerVertexData vtx; + vtx.vertex_position = {point.x, point.y, 0.0}; + vtx.texture_coordinates = {0.5, 0.5}; + vertex_builder.AppendVertex(vtx); + })); + + auto context = GetContext(); + + auto vertex_buffer = + vertex_builder.CreateVertexBuffer(*context->GetPermanentsAllocator()); + ASSERT_TRUE(vertex_buffer); + + auto bridge = CreateTextureForFixture("bay_bridge.jpg"); + auto boston = CreateTextureForFixture("boston.jpg"); + ASSERT_TRUE(bridge && boston); + auto sampler = context->GetSamplerLibrary()->GetSampler({}); + ASSERT_TRUE(sampler); + + Renderer::RenderCallback callback = [&](const Surface& surface, + RenderPass& pass) { + Command cmd; + cmd.label = "Box"; + cmd.pipeline = box_pipeline.WaitAndGet(); + + cmd.BindVertices(vertex_buffer); + + FS::FrameInfo frame_info; + frame_info.current_time = fml::TimePoint::Now().ToEpochDelta().ToSecondsF(); + frame_info.cursor_position = GetCursorPosition(); + frame_info.window_size.x = GetWindowSize().width; + frame_info.window_size.y = GetWindowSize().height; + + FS::BindFrameInfo(cmd, + pass.GetTransientsBuffer().EmplaceUniform(frame_info)); + FS::BindContents1(cmd, boston, sampler); + FS::BindContents2(cmd, bridge, sampler); + + cmd.primitive_type = PrimitiveType::kTriangle; + + VS::UniformBuffer uniforms; + uniforms.mvp = Matrix::MakeOrthographic(surface.GetSize()); + VS::BindUniformBuffer(cmd, + pass.GetTransientsBuffer().EmplaceUniform(uniforms)); + if (!pass.RecordCommand(cmd)) { + return false; + } + + return true; + }; + OpenPlaygroundHere(callback); +} + } // namespace testing } // namespace impeller