Flutter Impeller
impeller::VerticesGeometry Class Reference

A geometry that is created from a vertices object. More...

#include <vertices_geometry.h>

Inheritance diagram for impeller::VerticesGeometry:
impeller::Geometry

Public Types

enum  VertexMode {
  VertexMode::kTriangles,
  VertexMode::kTriangleStrip,
  VertexMode::kTriangleFan
}
 

Public Member Functions

 VerticesGeometry (std::vector< Point > vertices, std::vector< uint16_t > indices, std::vector< Point > texture_coordinates, std::vector< Color > colors, Rect bounds, VerticesGeometry::VertexMode vertex_mode)
 
 ~VerticesGeometry ()
 
GeometryResult GetPositionColorBuffer (const ContentContext &renderer, const Entity &entity, RenderPass &pass)
 
GeometryResult GetPositionUVBuffer (Rect texture_coverage, Matrix effect_transform, const ContentContext &renderer, const Entity &entity, RenderPass &pass) override
 
GeometryResult GetPositionBuffer (const ContentContext &renderer, const Entity &entity, RenderPass &pass) override
 
std::optional< RectGetCoverage (const Matrix &transform) const override
 
GeometryVertexType GetVertexType () const override
 
bool HasVertexColors () const
 
bool HasTextureCoordinates () const
 
std::optional< RectGetTextureCoordinateCoverge () const
 
- Public Member Functions inherited from impeller::Geometry
 Geometry ()
 
virtual ~Geometry ()
 
virtual bool CoversArea (const Matrix &transform, const Rect &rect) const
 Determines if this geometry, transformed by the given transform, will completely cover all surface area of the given rect. More...
 

Additional Inherited Members

- Static Public Member Functions inherited from impeller::Geometry
static std::unique_ptr< GeometryMakeFillPath (const Path &path, std::optional< Rect > inner_rect=std::nullopt)
 
static std::unique_ptr< GeometryMakeStrokePath (const Path &path, Scalar stroke_width=0.0, Scalar miter_limit=4.0, Cap stroke_cap=Cap::kButt, Join stroke_join=Join::kMiter)
 
static std::unique_ptr< GeometryMakeCover ()
 
static std::unique_ptr< GeometryMakeRect (Rect rect)
 
static std::unique_ptr< GeometryMakePointField (std::vector< Point > points, Scalar radius, bool round)
 

Detailed Description

A geometry that is created from a vertices object.

Definition at line 12 of file vertices_geometry.h.

Member Enumeration Documentation

◆ VertexMode

Enumerator
kTriangles 
kTriangleStrip 
kTriangleFan 

Definition at line 14 of file vertices_geometry.h.

14  {
15  kTriangles,
17  kTriangleFan,
18  };

Constructor & Destructor Documentation

◆ VerticesGeometry()

impeller::VerticesGeometry::VerticesGeometry ( std::vector< Point vertices,
std::vector< uint16_t >  indices,
std::vector< Point texture_coordinates,
std::vector< Color colors,
Rect  bounds,
VerticesGeometry::VertexMode  vertex_mode 
)

Definition at line 55 of file vertices_geometry.cc.

61  : vertices_(std::move(vertices)),
62  colors_(std::move(colors)),
63  texture_coordinates_(std::move(texture_coordinates)),
64  indices_(std::move(indices)),
65  bounds_(bounds),
66  vertex_mode_(vertex_mode) {
67  NormalizeIndices();
68 }

◆ ~VerticesGeometry()

impeller::VerticesGeometry::~VerticesGeometry ( )
default

Member Function Documentation

◆ GetCoverage()

std::optional< Rect > impeller::VerticesGeometry::GetCoverage ( const Matrix transform) const
overridevirtual

Implements impeller::Geometry.

Definition at line 304 of file vertices_geometry.cc.

305  {
306  return bounds_.TransformBounds(transform);
307 }

References impeller::TRect< T >::TransformBounds().

◆ GetPositionBuffer()

GeometryResult impeller::VerticesGeometry::GetPositionBuffer ( const ContentContext renderer,
const Entity entity,
RenderPass pass 
)
overridevirtual

Implements impeller::Geometry.

Definition at line 113 of file vertices_geometry.cc.

116  {
117  auto index_count = indices_.size();
118  auto vertex_count = vertices_.size();
119 
120  size_t total_vtx_bytes = vertex_count * sizeof(float) * 2;
121  size_t total_idx_bytes = index_count * sizeof(uint16_t);
122 
123  DeviceBufferDescriptor buffer_desc;
124  buffer_desc.size = total_vtx_bytes + total_idx_bytes;
125  buffer_desc.storage_mode = StorageMode::kHostVisible;
126 
127  auto buffer =
128  renderer.GetContext()->GetResourceAllocator()->CreateBuffer(buffer_desc);
129 
130  if (!buffer->CopyHostBuffer(
131  reinterpret_cast<const uint8_t*>(vertices_.data()),
132  Range{0, total_vtx_bytes}, 0)) {
133  return {};
134  }
135  if (index_count > 0 &&
136  !buffer->CopyHostBuffer(
137  reinterpret_cast<uint8_t*>(const_cast<uint16_t*>(indices_.data())),
138  Range{0, total_idx_bytes}, total_vtx_bytes)) {
139  return {};
140  }
141 
142  return GeometryResult{
143  .type = GetPrimitiveType(),
144  .vertex_buffer =
145  {
146  .vertex_buffer = {.buffer = buffer,
147  .range = Range{0, total_vtx_bytes}},
148  .index_buffer = {.buffer = buffer,
149  .range =
150  Range{total_vtx_bytes, total_idx_bytes}},
151  .vertex_count = index_count > 0 ? index_count : vertex_count,
152  .index_type =
153  index_count > 0 ? IndexType::k16bit : IndexType::kNone,
154  },
155  .transform = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) *
156  entity.GetTransformation(),
157  .prevent_overdraw = false,
158  };
159 }

References impeller::ContentContext::GetContext(), impeller::RenderPass::GetRenderTargetSize(), impeller::Entity::GetTransformation(), impeller::k16bit, impeller::kHostVisible, impeller::kNone, impeller::Matrix::MakeOrthographic(), impeller::DeviceBufferDescriptor::size, and impeller::GeometryResult::type.

◆ GetPositionColorBuffer()

GeometryResult impeller::VerticesGeometry::GetPositionColorBuffer ( const ContentContext renderer,
const Entity entity,
RenderPass pass 
)

Definition at line 161 of file vertices_geometry.cc.

164  {
166 
167  auto index_count = indices_.size();
168  auto vertex_count = vertices_.size();
169 
170  std::vector<VS::PerVertexData> vertex_data(vertex_count);
171  {
172  for (auto i = 0u; i < vertex_count; i++) {
173  vertex_data[i] = {
174  .position = vertices_[i],
175  .color = colors_[i],
176  };
177  }
178  }
179 
180  size_t total_vtx_bytes = vertex_data.size() * sizeof(VS::PerVertexData);
181  size_t total_idx_bytes = index_count * sizeof(uint16_t);
182 
183  DeviceBufferDescriptor buffer_desc;
184  buffer_desc.size = total_vtx_bytes + total_idx_bytes;
185  buffer_desc.storage_mode = StorageMode::kHostVisible;
186 
187  auto buffer =
188  renderer.GetContext()->GetResourceAllocator()->CreateBuffer(buffer_desc);
189 
190  if (!buffer->CopyHostBuffer(reinterpret_cast<uint8_t*>(vertex_data.data()),
191  Range{0, total_vtx_bytes}, 0)) {
192  return {};
193  }
194  if (index_count > 0 &&
195  !buffer->CopyHostBuffer(
196  reinterpret_cast<uint8_t*>(const_cast<uint16_t*>(indices_.data())),
197  Range{0, total_idx_bytes}, total_vtx_bytes)) {
198  return {};
199  }
200 
201  return GeometryResult{
202  .type = GetPrimitiveType(),
203  .vertex_buffer =
204  {
205  .vertex_buffer = {.buffer = buffer,
206  .range = Range{0, total_vtx_bytes}},
207  .index_buffer = {.buffer = buffer,
208  .range =
209  Range{total_vtx_bytes, total_idx_bytes}},
210  .vertex_count = index_count > 0 ? index_count : vertex_count,
211  .index_type =
212  index_count > 0 ? IndexType::k16bit : IndexType::kNone,
213  },
214  .transform = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) *
215  entity.GetTransformation(),
216  .prevent_overdraw = false,
217  };
218 }

References impeller::ContentContext::GetContext(), impeller::RenderPass::GetRenderTargetSize(), impeller::Entity::GetTransformation(), impeller::k16bit, impeller::kHostVisible, impeller::kNone, impeller::Matrix::MakeOrthographic(), impeller::DeviceBufferDescriptor::size, and impeller::GeometryResult::type.

◆ GetPositionUVBuffer()

GeometryResult impeller::VerticesGeometry::GetPositionUVBuffer ( Rect  texture_coverage,
Matrix  effect_transform,
const ContentContext renderer,
const Entity entity,
RenderPass pass 
)
overridevirtual

Reimplemented from impeller::Geometry.

Definition at line 220 of file vertices_geometry.cc.

225  {
227 
228  auto index_count = indices_.size();
229  auto vertex_count = vertices_.size();
230  auto size = texture_coverage.size;
231  auto origin = texture_coverage.origin;
232  auto has_texture_coordinates = HasTextureCoordinates();
233  std::vector<VS::PerVertexData> vertex_data(vertex_count);
234  {
235  for (auto i = 0u; i < vertex_count; i++) {
236  auto vertex = vertices_[i];
237  auto texture_coord =
238  has_texture_coordinates ? texture_coordinates_[i] : vertices_[i];
239  auto uv =
240  effect_transform * Point((texture_coord.x - origin.x) / size.width,
241  (texture_coord.y - origin.y) / size.height);
242  // From experimentation we need to clamp these values to < 1.0 or else
243  // there can be flickering.
244  vertex_data[i] = {
245  .position = vertex,
246  .texture_coords =
247  Point(std::clamp(uv.x, 0.0f, 1.0f - kEhCloseEnough),
248  std::clamp(uv.y, 0.0f, 1.0f - kEhCloseEnough)),
249  };
250  }
251  }
252 
253  size_t total_vtx_bytes = vertex_data.size() * sizeof(VS::PerVertexData);
254  size_t total_idx_bytes = index_count * sizeof(uint16_t);
255 
256  DeviceBufferDescriptor buffer_desc;
257  buffer_desc.size = total_vtx_bytes + total_idx_bytes;
258  buffer_desc.storage_mode = StorageMode::kHostVisible;
259 
260  auto buffer =
261  renderer.GetContext()->GetResourceAllocator()->CreateBuffer(buffer_desc);
262 
263  if (!buffer->CopyHostBuffer(reinterpret_cast<uint8_t*>(vertex_data.data()),
264  Range{0, total_vtx_bytes}, 0)) {
265  return {};
266  }
267  if (index_count > 0u &&
268  !buffer->CopyHostBuffer(
269  reinterpret_cast<uint8_t*>(const_cast<uint16_t*>(indices_.data())),
270  Range{0, total_idx_bytes}, total_vtx_bytes)) {
271  return {};
272  }
273 
274  return GeometryResult{
275  .type = GetPrimitiveType(),
276  .vertex_buffer =
277  {
278  .vertex_buffer = {.buffer = buffer,
279  .range = Range{0, total_vtx_bytes}},
280  .index_buffer = {.buffer = buffer,
281  .range =
282  Range{total_vtx_bytes, total_idx_bytes}},
283  .vertex_count = index_count > 0 ? index_count : vertex_count,
284  .index_type =
285  index_count > 0 ? IndexType::k16bit : IndexType::kNone,
286  },
287  .transform = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) *
288  entity.GetTransformation(),
289  .prevent_overdraw = false,
290  };
291 }

References impeller::ContentContext::GetContext(), impeller::RenderPass::GetRenderTargetSize(), impeller::Entity::GetTransformation(), HasTextureCoordinates(), impeller::k16bit, impeller::kEhCloseEnough, impeller::kHostVisible, impeller::kNone, impeller::Matrix::MakeOrthographic(), impeller::TRect< T >::origin, impeller::DeviceBufferDescriptor::size, impeller::TRect< T >::size, and impeller::GeometryResult::type.

◆ GetTextureCoordinateCoverge()

std::optional< Rect > impeller::VerticesGeometry::GetTextureCoordinateCoverge ( ) const

Definition at line 100 of file vertices_geometry.cc.

100  {
101  if (!HasTextureCoordinates()) {
102  return std::nullopt;
103  }
104  auto vertex_count = vertices_.size();
105  if (vertex_count == 0) {
106  return std::nullopt;
107  }
108 
109  return Rect::MakePointBounds(texture_coordinates_.begin(),
110  texture_coordinates_.end());
111 }

References HasTextureCoordinates(), and impeller::TRect< Scalar >::MakePointBounds().

◆ GetVertexType()

GeometryVertexType impeller::VerticesGeometry::GetVertexType ( ) const
overridevirtual

Implements impeller::Geometry.

Definition at line 293 of file vertices_geometry.cc.

293  {
294  if (HasVertexColors()) {
296  }
297  if (HasTextureCoordinates()) {
299  }
300 
302 }

References HasTextureCoordinates(), HasVertexColors(), impeller::kColor, impeller::kPosition, and impeller::kUV.

◆ HasTextureCoordinates()

bool impeller::VerticesGeometry::HasTextureCoordinates ( ) const

Definition at line 96 of file vertices_geometry.cc.

96  {
97  return texture_coordinates_.size() > 0;
98 }

Referenced by GetPositionUVBuffer(), GetTextureCoordinateCoverge(), and GetVertexType().

◆ HasVertexColors()

bool impeller::VerticesGeometry::HasVertexColors ( ) const

Definition at line 92 of file vertices_geometry.cc.

92  {
93  return colors_.size() > 0;
94 }

Referenced by GetVertexType().


The documentation for this class was generated from the following files:
impeller::IndexType::k16bit
@ k16bit
impeller::kEhCloseEnough
constexpr float kEhCloseEnough
Definition: constants.h:55
impeller::TRect::TransformBounds
constexpr TRect TransformBounds(const Matrix &transform) const
Creates a new bounding box that contains this transformed rectangle.
Definition: rect.h:204
impeller::StorageMode::kHostVisible
@ kHostVisible
impeller::PrimitiveType::kTriangleStrip
@ kTriangleStrip
impeller::kColor
@ kColor
Definition: geometry.h:27
impeller::Point
TPoint< Scalar > Point
Definition: point.h:306
impeller::VerticesGeometry::HasVertexColors
bool HasVertexColors() const
Definition: vertices_geometry.cc:92
impeller::IndexType::kNone
@ kNone
Does not use the index buffer.
impeller::TRect< Scalar >::MakePointBounds
constexpr static std::optional< TRect > MakePointBounds(const PointIter first, const PointIter last)
Definition: rect.h:57
impeller::kUV
@ kUV
Definition: geometry.h:28
impeller::Matrix::MakeOrthographic
static constexpr Matrix MakeOrthographic(TSize< T > size)
Definition: matrix.h:448
impeller::VerticesGeometry::HasTextureCoordinates
bool HasTextureCoordinates() const
Definition: vertices_geometry.cc:96
impeller::RenderPipelineT::VertexShader
VertexShader_ VertexShader
Definition: pipeline.h:90
impeller::kPosition
@ kPosition
Definition: geometry.h:26