@@ -104,20 +104,21 @@ static PrimitiveType GetPrimitiveType(const flutter::DlVertices* vertices) {
104104 }
105105}
106106
107- GeometryResult DLVerticesGeometry::GetPositionBuffer (
108- const ContentContext& renderer,
109- const Entity& entity,
110- RenderPass& pass) {
111- auto index_count = normalized_indices_.size () == 0
112- ? vertices_->index_count ()
113- : normalized_indices_.size ();
114- auto vertex_count = vertices_->vertex_count ();
115- auto * dl_indices = normalized_indices_.size () == 0
116- ? vertices_->indices ()
117- : normalized_indices_.data ();
118- auto * dl_vertices = vertices_->vertices ();
107+ bool DLVerticesGeometry::HasTextureCoordinates () const {
108+ return vertices_->texture_coordinates () != nullptr ;
109+ }
110+
111+ bool DLVerticesGeometry::HasVertexColors () const {
112+ return vertices_->colors () != nullptr ;
113+ }
119114
120- size_t total_vtx_bytes = vertex_count * sizeof (float ) * 2 ;
115+ static VertexBuffer CreateVertexBuffer (const ContentContext& renderer,
116+ size_t vertex_count,
117+ size_t index_count,
118+ size_t vertex_size,
119+ const uint8_t * vertex_data,
120+ const uint8_t * index_data) {
121+ size_t total_vtx_bytes = vertex_count * vertex_size;
121122 size_t total_idx_bytes = index_count * sizeof (uint16_t );
122123
123124 DeviceBufferDescriptor buffer_desc;
@@ -127,28 +128,41 @@ GeometryResult DLVerticesGeometry::GetPositionBuffer(
127128 auto buffer =
128129 renderer.GetContext ()->GetResourceAllocator ()->CreateBuffer (buffer_desc);
129130
130- if (!buffer->CopyHostBuffer (reinterpret_cast <const uint8_t *>(dl_vertices),
131- Range{0 , total_vtx_bytes}, 0 )) {
131+ if (!buffer->CopyHostBuffer (vertex_data, Range{0 , total_vtx_bytes}, 0 )) {
132132 return {};
133133 }
134- if (!buffer->CopyHostBuffer (
135- reinterpret_cast <uint8_t *>(const_cast <uint16_t *>(dl_indices)),
136- Range{0 , total_idx_bytes}, total_vtx_bytes)) {
134+ if (!buffer->CopyHostBuffer (index_data, Range{0 , total_idx_bytes},
135+ total_vtx_bytes)) {
137136 return {};
138137 }
138+ return {
139+ .vertex_buffer = {.buffer = buffer, .range = Range{0 , total_vtx_bytes}},
140+ .index_buffer = {.buffer = buffer,
141+ .range = Range{total_vtx_bytes, total_idx_bytes}},
142+ .index_count = index_count,
143+ .index_type = IndexType::k16bit,
144+ };
145+ }
146+
147+ GeometryResult DLVerticesGeometry::GetPositionBuffer (
148+ const ContentContext& renderer,
149+ const Entity& entity,
150+ RenderPass& pass) {
151+ auto index_count = normalized_indices_.size () == 0
152+ ? vertices_->index_count ()
153+ : normalized_indices_.size ();
154+ auto vertex_count = vertices_->vertex_count ();
155+ auto * dl_indices = normalized_indices_.size () == 0
156+ ? vertices_->indices ()
157+ : normalized_indices_.data ();
158+ auto * dl_vertices = vertices_->vertices ();
139159
140160 return GeometryResult{
141161 .type = GetPrimitiveType (vertices_),
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- .index_count = index_count,
150- .index_type = IndexType::k16bit,
151- },
162+ .vertex_buffer = CreateVertexBuffer (
163+ renderer, vertex_count, index_count, sizeof (float ) * 2 ,
164+ reinterpret_cast <const uint8_t *>(dl_vertices),
165+ reinterpret_cast <const uint8_t *>(dl_indices)),
152166 .transform = Matrix::MakeOrthographic (pass.GetRenderTargetSize ()) *
153167 entity.GetTransformation (),
154168 .prevent_overdraw = false ,
@@ -185,45 +199,65 @@ GeometryResult DLVerticesGeometry::GetPositionColorBuffer(
185199 }
186200 }
187201
188- size_t total_vtx_bytes = vertex_data.size () * sizeof (VS::PerVertexData);
189- size_t total_idx_bytes = index_count * sizeof (uint16_t );
202+ return GeometryResult{
203+ .type = GetPrimitiveType (vertices_),
204+ .vertex_buffer = CreateVertexBuffer (
205+ renderer, vertex_count, index_count, sizeof (VS::PerVertexData),
206+ reinterpret_cast <const uint8_t *>(vertex_data.data ()),
207+ reinterpret_cast <const uint8_t *>(dl_indices)),
208+ .transform = Matrix::MakeOrthographic (pass.GetRenderTargetSize ()) *
209+ entity.GetTransformation (),
210+ .prevent_overdraw = false ,
211+ };
212+ }
190213
191- DeviceBufferDescriptor buffer_desc;
192- buffer_desc.size = total_vtx_bytes + total_idx_bytes;
193- buffer_desc.storage_mode = StorageMode::kHostVisible ;
214+ GeometryResult DLVerticesGeometry::GetPositionUVBuffer (
215+ const ContentContext& renderer,
216+ const Entity& entity,
217+ RenderPass& pass) {
218+ using VS = TexturePipeline::VertexShader;
194219
195- auto buffer =
196- renderer.GetContext ()->GetResourceAllocator ()->CreateBuffer (buffer_desc);
220+ auto index_count = normalized_indices_.size () == 0
221+ ? vertices_->index_count ()
222+ : normalized_indices_.size ();
223+ auto vertex_count = vertices_->vertex_count ();
224+ auto * dl_indices = normalized_indices_.size () == 0
225+ ? vertices_->indices ()
226+ : normalized_indices_.data ();
227+ auto * dl_vertices = vertices_->vertices ();
228+ auto * dl_tex_coords = vertices_->texture_coordinates ();
197229
198- if (!buffer->CopyHostBuffer (reinterpret_cast <uint8_t *>(vertex_data.data ()),
199- Range{0 , total_vtx_bytes}, 0 )) {
200- return {};
201- }
202- if (!buffer->CopyHostBuffer (
203- reinterpret_cast <uint8_t *>(const_cast <uint16_t *>(dl_indices)),
204- Range{0 , total_idx_bytes}, total_vtx_bytes)) {
205- return {};
230+ auto coverage_rect = ToRect (vertices_->bounds ());
231+
232+ std::vector<VS::PerVertexData> vertex_data (vertex_count);
233+ for (auto i = 0 ; i < vertex_count; i++) {
234+ auto sk_point = dl_vertices[i];
235+ auto vertex = Point (sk_point.x (), sk_point.y ());
236+
237+ // TODO(jonahwilliams): Instead of the coverage rect, this should use the size
238+ // of what is being sampled, such as the image size or gradient bounds.
239+ auto coverage_coords = ToPoint (dl_tex_coords[i]) / coverage_rect.size ;
240+
241+ vertex_data[i] = {
242+ .position = vertex,
243+ .texture_coords = coverage_coords,
244+ };
206245 }
207246
208247 return GeometryResult{
209248 .type = GetPrimitiveType (vertices_),
210- .vertex_buffer =
211- {
212- .vertex_buffer = {.buffer = buffer,
213- .range = Range{0 , total_vtx_bytes}},
214- .index_buffer = {.buffer = buffer,
215- .range =
216- Range{total_vtx_bytes, total_idx_bytes}},
217- .index_count = index_count,
218- .index_type = IndexType::k16bit,
219- },
249+ .vertex_buffer = CreateVertexBuffer (
250+ renderer, vertex_count, index_count, sizeof (VS::PerVertexData),
251+ reinterpret_cast <const uint8_t *>(vertex_data.data ()),
252+ reinterpret_cast <const uint8_t *>(dl_indices)),
220253 .transform = Matrix::MakeOrthographic (pass.GetRenderTargetSize ()) *
221254 entity.GetTransformation (),
222255 .prevent_overdraw = false ,
223256 };
224257}
225258
226- GeometryResult DLVerticesGeometry::GetPositionUVBuffer (
259+ // This method is used even if there are no user-provided texture coordinates.
260+ GeometryResult DLVerticesGeometry::GetPositionUVColorBuffer (
227261 const ContentContext& renderer,
228262 const Entity& entity,
229263 RenderPass& pass) {
@@ -249,10 +283,13 @@ GeometryResult DLVerticesGeometry::GetPositionUVBuffer(
249283 dl_color.getBlueF (), dl_color.getAlphaF ());
250284 auto sk_point = dl_vertices[i];
251285 auto vertex = Point (sk_point.x (), sk_point.y ());
252- auto coverage_coords =
253- (dl_tex_coords == nullptr )
254- ? (vertex - coverage_rect.origin ) / coverage_rect.size
255- : ToPoint (dl_tex_coords[i]);
286+
287+ // TODO(jonahwilliams): Instead of the coverage rect, this should use the size
288+ // of what is being sampled, such as the image size or gradient bounds.
289+ auto coverage_coords = (vertex - coverage_rect.origin ) / coverage_rect.size ;
290+ if (dl_tex_coords != nullptr ) {
291+ coverage_coords = ToPoint (dl_tex_coords[i]) / coverage_rect.size ;
292+ }
256293
257294 vertex_data[i] = {
258295 .vertices = vertex,
@@ -261,43 +298,17 @@ GeometryResult DLVerticesGeometry::GetPositionUVBuffer(
261298 };
262299 }
263300
264- size_t total_vtx_bytes = vertex_data.size () * sizeof (VS::PerVertexData);
265- size_t total_idx_bytes = index_count * sizeof (uint16_t );
266-
267- DeviceBufferDescriptor buffer_desc;
268- buffer_desc.size = total_vtx_bytes + total_idx_bytes;
269- buffer_desc.storage_mode = StorageMode::kHostVisible ;
270-
271- auto buffer =
272- renderer.GetContext ()->GetResourceAllocator ()->CreateBuffer (buffer_desc);
273-
274- if (!buffer->CopyHostBuffer (reinterpret_cast <uint8_t *>(vertex_data.data ()),
275- Range{0 , total_vtx_bytes}, 0 )) {
276- return {};
277- }
278- if (!buffer->CopyHostBuffer (
279- reinterpret_cast <uint8_t *>(const_cast <uint16_t *>(dl_indices)),
280- Range{0 , total_idx_bytes}, total_vtx_bytes)) {
281- return {};
282- }
283-
284301 return GeometryResult{
285302 .type = GetPrimitiveType (vertices_),
286- .vertex_buffer =
287- {
288- .vertex_buffer = {.buffer = buffer,
289- .range = Range{0 , total_vtx_bytes}},
290- .index_buffer = {.buffer = buffer,
291- .range =
292- Range{total_vtx_bytes, total_idx_bytes}},
293- .index_count = index_count,
294- .index_type = IndexType::k16bit,
295- },
303+ .vertex_buffer = CreateVertexBuffer (
304+ renderer, vertex_count, index_count, sizeof (VS::PerVertexData),
305+ reinterpret_cast <const uint8_t *>(vertex_data.data ()),
306+ reinterpret_cast <const uint8_t *>(dl_indices)),
296307 .transform = Matrix::MakeOrthographic (pass.GetRenderTargetSize ()) *
297308 entity.GetTransformation (),
298309 .prevent_overdraw = false ,
299310 };
300- } // namespace impeller
311+ }
301312
302313GeometryVertexType DLVerticesGeometry::GetVertexType () const {
303314 auto * dl_colors = vertices_->colors ();
0 commit comments