Add a tessellator.
This commit is contained in:
@@ -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",
|
||||
]
|
||||
}
|
||||
|
||||
@@ -100,6 +100,11 @@ enum class TextureUsage : TextureUsageMask {
|
||||
kRenderTarget,
|
||||
};
|
||||
|
||||
enum class WindingOrder {
|
||||
kClockwise,
|
||||
kCounterClockwise,
|
||||
};
|
||||
|
||||
enum class PrimitiveType {
|
||||
kTriangle,
|
||||
kTriangleStrip,
|
||||
|
||||
114
engine/src/flutter/impeller/compositor/tessellator.cc
Normal file
114
engine/src/flutter/impeller/compositor/tessellator.cc
Normal file
@@ -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<Point>& contours,
|
||||
VertexCallback callback) const {
|
||||
if (!callback) {
|
||||
return false;
|
||||
}
|
||||
|
||||
using CTessellator =
|
||||
std::unique_ptr<TESStesselator, decltype(&DestroyTessellator)>;
|
||||
|
||||
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<Point> points;
|
||||
std::vector<uint32_t> 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
|
||||
43
engine/src/flutter/impeller/compositor/tessellator.h
Normal file
43
engine/src/flutter/impeller/compositor/tessellator.h
Normal file
@@ -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 <functional>
|
||||
#include <vector>
|
||||
|
||||
#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<void(Point)>;
|
||||
[[nodiscard]] bool Tessellate(const std::vector<Point>& vertices,
|
||||
VertexCallback callback) const;
|
||||
|
||||
private:
|
||||
FillType fill_type_ = FillType::kOdd;
|
||||
|
||||
FML_DISALLOW_COPY_AND_ASSIGN(Tessellator);
|
||||
};
|
||||
|
||||
} // namespace impeller
|
||||
@@ -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 */,
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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<BoxFadeVertexShader, BoxFadeFragmentShader>;
|
||||
using VS = BoxFadeVertexShader;
|
||||
using FS = BoxFadeFragmentShader;
|
||||
|
||||
BoxPipeline box_pipeline(*GetContext());
|
||||
|
||||
// Vertex buffer.
|
||||
VertexBufferBuilder<VS::PerVertexData> 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
|
||||
|
||||
Reference in New Issue
Block a user