Flutter Impeller
impeller::VerticesGeometry Class Referencefinal

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 ()=default
 
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) const override
 
GeometryResult GetPositionBuffer (const ContentContext &renderer, const Entity &entity, RenderPass &pass) const 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
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...
 
virtual bool IsAxisAlignedRect () const
 

Additional Inherited Members

- Static Public Member Functions inherited from impeller::Geometry
static std::shared_ptr< GeometryMakeFillPath (Path path, std::optional< Rect > inner_rect=std::nullopt)
 
static std::shared_ptr< GeometryMakeStrokePath (Path path, Scalar stroke_width=0.0, Scalar miter_limit=4.0, Cap stroke_cap=Cap::kButt, Join stroke_join=Join::kMiter)
 
static std::shared_ptr< GeometryMakeCover ()
 
static std::shared_ptr< GeometryMakeRect (const Rect &rect)
 
static std::shared_ptr< GeometryMakeOval (const Rect &rect)
 
static std::shared_ptr< GeometryMakeLine (const Point &p0, const Point &p1, Scalar width, Cap cap)
 
static std::shared_ptr< GeometryMakeCircle (const Point &center, Scalar radius)
 
static std::shared_ptr< GeometryMakeStrokedCircle (const Point &center, Scalar radius, Scalar stroke_width)
 
static std::shared_ptr< GeometryMakeRoundRect (const Rect &rect, const Size &radii)
 
static std::shared_ptr< GeometryMakePointField (std::vector< Point > points, Scalar radius, bool round)
 
- Static Protected Member Functions inherited from impeller::Geometry
static GeometryResult ComputePositionGeometry (const Tessellator::VertexGenerator &generator, const Entity &entity, RenderPass &pass)
 
static GeometryResult ComputePositionUVGeometry (const Tessellator::VertexGenerator &generator, const Matrix &uv_transform, const Entity &entity, RenderPass &pass)
 

Detailed Description

A geometry that is created from a vertices object.

Definition at line 13 of file vertices_geometry.h.

Member Enumeration Documentation

◆ VertexMode

Enumerator
kTriangles 
kTriangleStrip 
kTriangleFan 

Definition at line 15 of file vertices_geometry.h.

15  {
16  kTriangles,
18  kTriangleFan,
19  };

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 297 of file vertices_geometry.cc.

298  {
299  return bounds_.TransformBounds(transform);
300 }

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

◆ GetPositionBuffer()

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

Implements impeller::Geometry.

Definition at line 111 of file vertices_geometry.cc.

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

References impeller::ContentContext::GetContext(), impeller::RenderPass::GetOrthographicTransform(), impeller::Entity::GetTransform(), impeller::k16bit, impeller::kHostVisible, impeller::kNone, impeller::DeviceBufferDescriptor::size, and impeller::GeometryResult::type.

◆ GetPositionColorBuffer()

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

Definition at line 158 of file vertices_geometry.cc.

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

References impeller::ContentContext::GetContext(), impeller::RenderPass::GetOrthographicTransform(), impeller::Entity::GetTransform(), impeller::k16bit, impeller::kHostVisible, impeller::kNone, 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 
) const
overridevirtual

Implements impeller::Geometry.

Definition at line 216 of file vertices_geometry.cc.

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

References impeller::ContentContext::GetContext(), impeller::TRect< T >::GetNormalizingTransform(), impeller::RenderPass::GetOrthographicTransform(), impeller::Entity::GetTransform(), HasTextureCoordinates(), impeller::k16bit, impeller::kEhCloseEnough, impeller::kHostVisible, impeller::kNone, impeller::DeviceBufferDescriptor::size, and impeller::GeometryResult::type.

◆ GetTextureCoordinateCoverge()

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

Definition at line 98 of file vertices_geometry.cc.

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

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

◆ GetVertexType()

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

Implements impeller::Geometry.

Definition at line 286 of file vertices_geometry.cc.

286  {
287  if (HasVertexColors()) {
289  }
290  if (HasTextureCoordinates()) {
292  }
293 
295 }

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

◆ HasTextureCoordinates()

bool impeller::VerticesGeometry::HasTextureCoordinates ( ) const

Definition at line 94 of file vertices_geometry.cc.

94  {
95  return texture_coordinates_.size() > 0;
96 }

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

◆ HasVertexColors()

bool impeller::VerticesGeometry::HasVertexColors ( ) const

Definition at line 90 of file vertices_geometry.cc.

90  {
91  return colors_.size() > 0;
92 }

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:56
impeller::TRect::TransformBounds
constexpr TRect TransformBounds(const Matrix &transform) const
Creates a new bounding box that contains this transformed rectangle.
Definition: rect.h:264
impeller::StorageMode::kHostVisible
@ kHostVisible
impeller::TRect< Scalar >::MakePointBounds
constexpr static std::optional< TRect > MakePointBounds(const U &value)
Definition: rect.h:49
impeller::kColor
@ kColor
Definition: geometry.h:36
impeller::PrimitiveType::kTriangleStrip
@ kTriangleStrip
impeller::Point
TPoint< Scalar > Point
Definition: point.h:308
impeller::VerticesGeometry::HasVertexColors
bool HasVertexColors() const
Definition: vertices_geometry.cc:90
impeller::IndexType::kNone
@ kNone
Does not use the index buffer.
impeller::kUV
@ kUV
Definition: geometry.h:37
impeller::VerticesGeometry::HasTextureCoordinates
bool HasTextureCoordinates() const
Definition: vertices_geometry.cc:94
impeller::RenderPipelineT::VertexShader
VertexShader_ VertexShader
Definition: pipeline.h:92
impeller::kPosition
@ kPosition
Definition: geometry.h:35