Flutter Impeller
impeller::ComputeTessellator Class Reference

A utility that generates triangles of the specified fill type given a path. More...

#include <compute_tessellator.h>

Public Types

enum  Status {
  Status::kCommandInvalid,
  Status::kTooManyComponents,
  Status::kOk
}
 
enum  Style { Style::kStroke }
 

Public Member Functions

 ComputeTessellator ()
 
 ~ComputeTessellator ()
 
ComputeTessellatorSetStyle (Style value)
 
ComputeTessellatorSetStrokeWidth (Scalar value)
 
ComputeTessellatorSetStrokeJoin (Join value)
 
ComputeTessellatorSetStrokeCap (Cap value)
 
ComputeTessellatorSetMiterLimit (Scalar value)
 
ComputeTessellatorSetCubicAccuracy (Scalar value)
 
ComputeTessellatorSetQuadraticTolerance (Scalar value)
 
Status Tessellate (const Path &path, const std::shared_ptr< Context > &context, BufferView vertex_buffer, BufferView vertex_buffer_count, const CommandBuffer::CompletionCallback &callback=nullptr) const
 Generates triangles from the path. If the data needs to be synchronized back to the CPU, e.g. because one of the buffer views are host visible and will be used without creating a blit pass to copy them back, the callback is used to determine when the GPU calculation is complete and its status. On Metal, no additional synchronization is needed as long as the buffers are not heap allocated, so no additional synchronization mechanism is provided. More...
 

Static Public Attributes

static constexpr size_t kMaxCubicCount = 512
 
static constexpr size_t kMaxQuadCount = 2048
 
static constexpr size_t kMaxLineCount = 4096
 
static constexpr size_t kMaxComponentCount
 

Detailed Description

A utility that generates triangles of the specified fill type given a path.

Definition at line 20 of file compute_tessellator.h.

Member Enumeration Documentation

◆ Status

Enumerator
kCommandInvalid 
kTooManyComponents 
kOk 

Definition at line 32 of file compute_tessellator.h.

32  {
33  kCommandInvalid,
34  kTooManyComponents,
35  kOk,
36  };

◆ Style

Enumerator
kStroke 

Definition at line 38 of file compute_tessellator.h.

38  {
39  kStroke,
40  // TODO(dnfield): Implement kFill.
41  };

Constructor & Destructor Documentation

◆ ComputeTessellator()

impeller::ComputeTessellator::ComputeTessellator ( )
default

◆ ~ComputeTessellator()

impeller::ComputeTessellator::~ComputeTessellator ( )
default

Member Function Documentation

◆ SetCubicAccuracy()

ComputeTessellator & impeller::ComputeTessellator::SetCubicAccuracy ( Scalar  value)

Definition at line 54 of file compute_tessellator.cc.

54  {
55  cubic_accuracy_ = value;
56  return *this;
57 }

◆ SetMiterLimit()

ComputeTessellator & impeller::ComputeTessellator::SetMiterLimit ( Scalar  value)

Definition at line 50 of file compute_tessellator.cc.

50  {
51  miter_limit_ = value;
52  return *this;
53 }

◆ SetQuadraticTolerance()

ComputeTessellator & impeller::ComputeTessellator::SetQuadraticTolerance ( Scalar  value)

Definition at line 58 of file compute_tessellator.cc.

58  {
59  quad_tolerance_ = value;
60  return *this;
61 }

◆ SetStrokeCap()

ComputeTessellator & impeller::ComputeTessellator::SetStrokeCap ( Cap  value)

Definition at line 46 of file compute_tessellator.cc.

46  {
47  stroke_cap_ = value;
48  return *this;
49 }

◆ SetStrokeJoin()

ComputeTessellator & impeller::ComputeTessellator::SetStrokeJoin ( Join  value)

Definition at line 42 of file compute_tessellator.cc.

42  {
43  stroke_join_ = value;
44  return *this;
45 }

◆ SetStrokeWidth()

ComputeTessellator & impeller::ComputeTessellator::SetStrokeWidth ( Scalar  value)

Definition at line 37 of file compute_tessellator.cc.

37  {
38  stroke_width_ = value;
39  return *this;
40 }

Referenced by impeller::testing::TEST_P().

◆ SetStyle()

ComputeTessellator & impeller::ComputeTessellator::SetStyle ( Style  value)

Definition at line 32 of file compute_tessellator.cc.

32  {
33  style_ = value;
34  return *this;
35 }

◆ Tessellate()

ComputeTessellator::Status impeller::ComputeTessellator::Tessellate ( const Path path,
const std::shared_ptr< Context > &  context,
BufferView  vertex_buffer,
BufferView  vertex_buffer_count,
const CommandBuffer::CompletionCallback callback = nullptr 
) const

Generates triangles from the path. If the data needs to be synchronized back to the CPU, e.g. because one of the buffer views are host visible and will be used without creating a blit pass to copy them back, the callback is used to determine when the GPU calculation is complete and its status. On Metal, no additional synchronization is needed as long as the buffers are not heap allocated, so no additional synchronization mechanism is provided.

Returns
A |Status| value indicating success or failure of the submission.

Definition at line 63 of file compute_tessellator.cc.

68  {
69  FML_DCHECK(style_ == Style::kStroke);
70  using PS = PathPolylineComputeShader;
71  using SS = StrokeComputeShader;
72 
73  auto cubic_count = path.GetComponentCount(Path::ComponentType::kCubic);
74  auto quad_count = path.GetComponentCount(Path::ComponentType::kQuadratic) +
75  (cubic_count * 6);
76  auto line_count =
77  path.GetComponentCount(Path::ComponentType::kLinear) + (quad_count * 6);
78  if (cubic_count > kMaxCubicCount || quad_count > kMaxQuadCount ||
79  line_count > kMaxLineCount) {
81  }
82  PS::Cubics<kMaxCubicCount> cubics{.count = 0};
83  PS::Quads<kMaxQuadCount> quads{.count = 0};
84  PS::Lines<kMaxLineCount> lines{.count = 0};
85  PS::Components<kMaxComponentCount> components{.count = 0};
86  PS::Config config{.cubic_accuracy = cubic_accuracy_,
87  .quad_tolerance = quad_tolerance_};
88 
89  path.EnumerateComponents(
90  [&lines, &components](size_t index, const LinearPathComponent& linear) {
91  ::memcpy(&lines.data[lines.count], &linear,
92  sizeof(LinearPathComponent));
93  components.data[components.count++] = {lines.count++, 2};
94  },
95  [&quads, &components](size_t index, const QuadraticPathComponent& quad) {
96  ::memcpy(&quads.data[quads.count], &quad,
97  sizeof(QuadraticPathComponent));
98  components.data[components.count++] = {quads.count++, 3};
99  },
100  [&cubics, &components](size_t index, const CubicPathComponent& cubic) {
101  ::memcpy(&cubics.data[cubics.count], &cubic,
102  sizeof(CubicPathComponent));
103  components.data[components.count++] = {cubics.count++, 4};
104  },
105  [](size_t index, const ContourComponent& contour) {});
106 
107  auto polyline_buffer =
108  CreateDeviceBuffer<PS::Polyline<2048>>(context, "Polyline");
109 
110  auto cmd_buffer = context->CreateCommandBuffer();
111  auto pass = cmd_buffer->CreateComputePass();
112  FML_DCHECK(pass && pass->IsValid());
113 
114  {
115  using PathPolylinePipelineBuilder = ComputePipelineBuilder<PS>;
116  auto pipeline_desc =
117  PathPolylinePipelineBuilder::MakeDefaultPipelineDescriptor(*context);
118  FML_DCHECK(pipeline_desc.has_value());
119  auto compute_pipeline =
120  context->GetPipelineLibrary()->GetPipeline(pipeline_desc).Get();
121  FML_DCHECK(compute_pipeline);
122 
123  pass->SetGridSize(ISize(line_count, 1));
124  pass->SetThreadGroupSize(ISize(line_count, 1));
125 
126  ComputeCommand cmd;
127  DEBUG_COMMAND_INFO(cmd, "Generate Polyline");
128  cmd.pipeline = compute_pipeline;
129 
130  PS::BindConfig(cmd, pass->GetTransientsBuffer().EmplaceUniform(config));
131  PS::BindCubics(cmd,
132  pass->GetTransientsBuffer().EmplaceStorageBuffer(cubics));
133  PS::BindQuads(cmd, pass->GetTransientsBuffer().EmplaceStorageBuffer(quads));
134  PS::BindLines(cmd, pass->GetTransientsBuffer().EmplaceStorageBuffer(lines));
135  PS::BindComponents(
136  cmd, pass->GetTransientsBuffer().EmplaceStorageBuffer(components));
137  PS::BindPolyline(cmd, polyline_buffer->AsBufferView());
138 
139  if (!pass->AddCommand(std::move(cmd))) {
141  }
142  }
143 
144  {
145  using StrokePipelineBuilder = ComputePipelineBuilder<SS>;
146  auto pipeline_desc =
147  StrokePipelineBuilder::MakeDefaultPipelineDescriptor(*context);
148  FML_DCHECK(pipeline_desc.has_value());
149  auto compute_pipeline =
150  context->GetPipelineLibrary()->GetPipeline(pipeline_desc).Get();
151  FML_DCHECK(compute_pipeline);
152 
153  pass->SetGridSize(ISize(line_count, 1));
154  pass->SetThreadGroupSize(ISize(line_count, 1));
155 
156  ComputeCommand cmd;
157  DEBUG_COMMAND_INFO(cmd, "Compute Stroke");
158  cmd.pipeline = compute_pipeline;
159 
160  SS::Config config{
161  .width = stroke_width_,
162  .cap = static_cast<uint32_t>(stroke_cap_),
163  .join = static_cast<uint32_t>(stroke_join_),
164  .miter_limit = miter_limit_,
165  };
166  SS::BindConfig(cmd, pass->GetTransientsBuffer().EmplaceUniform(config));
167 
168  SS::BindPolyline(cmd, polyline_buffer->AsBufferView());
169  SS::BindVertexBufferCount(cmd, std::move(vertex_buffer_count));
170  SS::BindVertexBuffer(cmd, std::move(vertex_buffer));
171 
172  if (!pass->AddCommand(std::move(cmd))) {
174  }
175  }
176 
177  if (!pass->EncodeCommands()) {
179  }
180 
181  if (!cmd_buffer->SubmitCommands(callback)) {
183  }
184 
185  return Status::kOk;
186 }

References DEBUG_COMMAND_INFO, impeller::Path::EnumerateComponents(), impeller::Path::GetComponentCount(), kCommandInvalid, impeller::Path::kCubic, impeller::Path::kLinear, kMaxCubicCount, kMaxLineCount, kMaxQuadCount, kOk, impeller::Path::kQuadratic, kStroke, kTooManyComponents, and impeller::ComputeCommand::pipeline.

Referenced by impeller::testing::TEST_P().

Member Data Documentation

◆ kMaxComponentCount

constexpr size_t impeller::ComputeTessellator::kMaxComponentCount
staticconstexpr
Initial value:

Definition at line 29 of file compute_tessellator.h.

◆ kMaxCubicCount

constexpr size_t impeller::ComputeTessellator::kMaxCubicCount = 512
staticconstexpr

Definition at line 26 of file compute_tessellator.h.

Referenced by Tessellate().

◆ kMaxLineCount

constexpr size_t impeller::ComputeTessellator::kMaxLineCount = 4096
staticconstexpr

Definition at line 28 of file compute_tessellator.h.

Referenced by Tessellate().

◆ kMaxQuadCount

constexpr size_t impeller::ComputeTessellator::kMaxQuadCount = 2048
staticconstexpr

Definition at line 27 of file compute_tessellator.h.

Referenced by Tessellate().


The documentation for this class was generated from the following files:
impeller::ComputeTessellator::Status::kCommandInvalid
@ kCommandInvalid
DEBUG_COMMAND_INFO
#define DEBUG_COMMAND_INFO(obj, arg)
Definition: command.h:28
impeller::ComputeTessellator::Status::kTooManyComponents
@ kTooManyComponents
impeller::Path::ComponentType::kLinear
@ kLinear
impeller::Path::ComponentType::kCubic
@ kCubic
impeller::Path::ComponentType::kQuadratic
@ kQuadratic
impeller::ComputeTessellator::kMaxCubicCount
static constexpr size_t kMaxCubicCount
Definition: compute_tessellator.h:26
impeller::ComputeTessellator::Style::kStroke
@ kStroke
impeller::ComputeTessellator::Status::kOk
@ kOk
impeller::ComputeTessellator::kMaxQuadCount
static constexpr size_t kMaxQuadCount
Definition: compute_tessellator.h:27
impeller::ISize
TSize< int64_t > ISize
Definition: size.h:138
impeller::ComputeTessellator::kMaxLineCount
static constexpr size_t kMaxLineCount
Definition: compute_tessellator.h:28