Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit bf0b4f8

Browse files
authored
[Flutter GPU] Remove Command/VertexBuffer usage from Flutter GPU. (#55893)
Resolves flutter/flutter#156764. Resolves flutter/flutter#155335. - Remove Command/VertexBuffer from Flutter GPU. - Separate vertex/index count into `impeller::RenderPass::SetElementCount(size_t count)` & accurately document its behavior. - Make Flutter GPU uniform/texture bindings re-assignable. - Remove unnecessary templates.
1 parent 09598f6 commit bf0b4f8

File tree

15 files changed

+210
-173
lines changed

15 files changed

+210
-173
lines changed

impeller/entity/contents/text_contents.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,9 @@ bool TextContents::Render(const ContentContext& renderer,
256256
}
257257
});
258258

259-
pass.SetVertexBuffer(std::move(buffer_view), vertex_count);
259+
pass.SetVertexBuffer(std::move(buffer_view));
260260
pass.SetIndexBuffer({}, IndexType::kNone);
261+
pass.SetElementCount(vertex_count);
261262

262263
return pass.Draw().ok();
263264
}

impeller/renderer/backend/gles/buffer_bindings_gles.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ std::optional<size_t> BufferBindingsGLES::BindTextures(
415415
/// If there is a sampler for the texture at the same index, configure the
416416
/// bound texture using that sampler.
417417
///
418-
const auto& sampler_gles = SamplerGLES::Cast(*data.sampler);
418+
const auto& sampler_gles = SamplerGLES::Cast(**data.sampler);
419419
if (!sampler_gles.ConfigureBoundTexture(texture_gles, gl)) {
420420
return std::nullopt;
421421
}

impeller/renderer/backend/gles/render_pass_gles.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ static bool BindVertexBuffer(const ProcTableGLES& gl,
455455
/// Finally! Invoke the draw call.
456456
///
457457
if (command.index_type == IndexType::kNone) {
458-
gl.DrawArrays(mode, command.base_vertex, command.vertex_count);
458+
gl.DrawArrays(mode, command.base_vertex, command.element_count);
459459
} else {
460460
// Bind the index buffer if necessary.
461461
auto index_buffer_view = command.index_buffer;
@@ -466,7 +466,7 @@ static bool BindVertexBuffer(const ProcTableGLES& gl,
466466
return false;
467467
}
468468
gl.DrawElements(mode, // mode
469-
command.vertex_count, // count
469+
command.element_count, // count
470470
ToIndexType(command.index_type), // type
471471
reinterpret_cast<const GLvoid*>(static_cast<GLsizei>(
472472
index_buffer_view.range.offset)) // indices

impeller/renderer/backend/metal/render_pass_mtl.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,15 @@ class RenderPassMTL final : public RenderPass {
7979
// |RenderPass|
8080
void SetScissor(IRect scissor) override;
8181

82+
// |RenderPass|
83+
void SetElementCount(size_t count) override;
84+
8285
// |RenderPass|
8386
void SetInstanceCount(size_t count) override;
8487

8588
// |RenderPass|
8689
bool SetVertexBuffer(BufferView vertex_buffers[],
87-
size_t vertex_buffer_count,
88-
size_t vertex_count) override;
90+
size_t vertex_buffer_count) override;
8991

9092
// |RenderPass|
9193
bool SetIndexBuffer(BufferView index_buffer, IndexType index_type) override;

impeller/renderer/backend/metal/render_pass_mtl.mm

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -283,15 +283,19 @@ static bool Bind(PassBindingsCacheMTL& pass,
283283
pass_bindings_.SetScissor(scissor);
284284
}
285285

286+
// |RenderPass|
287+
void RenderPassMTL::SetElementCount(size_t count) {
288+
vertex_count_ = count;
289+
}
290+
286291
// |RenderPass|
287292
void RenderPassMTL::SetInstanceCount(size_t count) {
288293
instance_count_ = count;
289294
}
290295

291296
// |RenderPass|
292297
bool RenderPassMTL::SetVertexBuffer(BufferView vertex_buffers[],
293-
size_t vertex_buffer_count,
294-
size_t vertex_count) {
298+
size_t vertex_buffer_count) {
295299
if (!ValidateVertexBuffers(vertex_buffers, vertex_buffer_count)) {
296300
return false;
297301
}
@@ -304,8 +308,6 @@ static bool Bind(PassBindingsCacheMTL& pass,
304308
}
305309
}
306310

307-
vertex_count_ = vertex_count;
308-
309311
return true;
310312
}
311313

impeller/renderer/backend/vulkan/render_pass_vk.cc

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -364,15 +364,19 @@ void RenderPassVK::SetScissor(IRect scissor) {
364364
command_buffer_vk_.setScissor(0, 1, &scissor_vk);
365365
}
366366

367+
// |RenderPass|
368+
void RenderPassVK::SetElementCount(size_t count) {
369+
element_count_ = count;
370+
}
371+
367372
// |RenderPass|
368373
void RenderPassVK::SetInstanceCount(size_t count) {
369374
instance_count_ = count;
370375
}
371376

372377
// |RenderPass|
373378
bool RenderPassVK::SetVertexBuffer(BufferView vertex_buffers[],
374-
size_t vertex_buffer_count,
375-
size_t vertex_count) {
379+
size_t vertex_buffer_count) {
376380
if (!ValidateVertexBuffers(vertex_buffers, vertex_buffer_count)) {
377381
return false;
378382
}
@@ -389,8 +393,6 @@ bool RenderPassVK::SetVertexBuffer(BufferView vertex_buffers[],
389393
command_buffer_vk_.bindVertexBuffers(0u, vertex_buffer_count, buffers,
390394
vertex_buffer_offsets);
391395

392-
vertex_count_ = vertex_count;
393-
394396
return true;
395397
}
396398

@@ -506,14 +508,14 @@ fml::Status RenderPassVK::Draw() {
506508
}
507509

508510
if (has_index_buffer_) {
509-
command_buffer_vk_.drawIndexed(vertex_count_, // index count
511+
command_buffer_vk_.drawIndexed(element_count_, // index count
510512
instance_count_, // instance count
511513
0u, // first index
512514
base_vertex_, // vertex offset
513515
0u // first instance
514516
);
515517
} else {
516-
command_buffer_vk_.draw(vertex_count_, // vertex count
518+
command_buffer_vk_.draw(element_count_, // vertex count
517519
instance_count_, // instance count
518520
base_vertex_, // vertex offset
519521
0u // first instance
@@ -532,7 +534,7 @@ fml::Status RenderPassVK::Draw() {
532534
descriptor_write_offset_ = 0u;
533535
instance_count_ = 1u;
534536
base_vertex_ = 0u;
535-
vertex_count_ = 0u;
537+
element_count_ = 0u;
536538
pipeline_ = nullptr;
537539
pipeline_uses_input_attachments_ = false;
538540
immutable_sampler_ = nullptr;

impeller/renderer/backend/vulkan/render_pass_vk.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class RenderPassVK final : public RenderPass {
4646
size_t descriptor_write_offset_ = 0u;
4747
size_t instance_count_ = 1u;
4848
size_t base_vertex_ = 0u;
49-
size_t vertex_count_ = 0u;
49+
size_t element_count_ = 0u;
5050
bool has_index_buffer_ = false;
5151
bool has_label_ = false;
5252
const Pipeline<PipelineDescriptor>* pipeline_;
@@ -76,13 +76,15 @@ class RenderPassVK final : public RenderPass {
7676
// |RenderPass|
7777
void SetScissor(IRect scissor) override;
7878

79+
// |RenderPass|
80+
void SetElementCount(size_t count) override;
81+
7982
// |RenderPass|
8083
void SetInstanceCount(size_t count) override;
8184

8285
// |RenderPass|
8386
bool SetVertexBuffer(BufferView vertex_buffers[],
84-
size_t vertex_buffer_count,
85-
size_t vertex_count) override;
87+
size_t vertex_buffer_count) override;
8688

8789
// |RenderPass|
8890
bool SetIndexBuffer(BufferView index_buffer, IndexType index_type) override;

impeller/renderer/command.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ bool Command::BindVertices(const VertexBuffer& buffer) {
2020

2121
vertex_buffers = {buffer.vertex_buffer};
2222
vertex_buffer_count = 1u;
23-
vertex_count = buffer.vertex_count;
23+
element_count = buffer.vertex_count;
2424
index_buffer = buffer.index_buffer;
2525
index_type = buffer.index_type;
2626
return true;
@@ -89,14 +89,14 @@ bool Command::BindResource(ShaderStage stage,
8989
vertex_bindings.sampled_images.emplace_back(TextureAndSampler{
9090
.slot = slot,
9191
.texture = {&metadata, std::move(texture)},
92-
.sampler = sampler,
92+
.sampler = &sampler,
9393
});
9494
return true;
9595
case ShaderStage::kFragment:
9696
fragment_bindings.sampled_images.emplace_back(TextureAndSampler{
9797
.slot = slot,
9898
.texture = {&metadata, std::move(texture)},
99-
.sampler = sampler,
99+
.sampler = &sampler,
100100
});
101101
return true;
102102
case ShaderStage::kCompute:

impeller/renderer/command.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ using TextureResource = Resource<std::shared_ptr<const Texture>>;
6161
struct TextureAndSampler {
6262
SampledImageSlot slot;
6363
TextureResource texture;
64-
const std::unique_ptr<const Sampler>& sampler;
64+
const std::unique_ptr<const Sampler>* sampler;
6565
};
6666

6767
/// @brief combines the buffer resource and its uniform slot information.
@@ -160,14 +160,13 @@ struct Command : public ResourceBinder {
160160
BufferView index_buffer;
161161

162162
//----------------------------------------------------------------------------
163-
/// The total count of vertices, either in the vertex_buffer if the
164-
/// index_type is IndexType::kNone or in the index_buffer otherwise.
165-
size_t vertex_count = 0u;
163+
/// The number of elements to draw. When only a vertex buffer is set, this is
164+
/// the vertex count. When an index buffer is set, this is the index count.
165+
size_t element_count = 0u;
166166

167167
//----------------------------------------------------------------------------
168168
/// The type of indices in the index buffer. The indices must be tightly
169169
/// packed in the index buffer.
170-
///
171170
IndexType index_type = IndexType::kUnknown;
172171

173172
//----------------------------------------------------------------------------

impeller/renderer/render_pass.cc

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ bool RenderPass::AddCommand(Command&& command) {
7272
}
7373
}
7474

75-
if (command.vertex_count == 0u || command.instance_count == 0u) {
75+
if (command.element_count == 0u || command.instance_count == 0u) {
7676
// Essentially a no-op. Don't record the command but this is not necessary
7777
// an error either.
7878
return true;
@@ -117,40 +117,40 @@ void RenderPass::SetScissor(IRect scissor) {
117117
pending_.scissor = scissor;
118118
}
119119

120+
void RenderPass::SetElementCount(size_t count) {
121+
pending_.element_count = count;
122+
}
123+
120124
void RenderPass::SetInstanceCount(size_t count) {
121125
pending_.instance_count = count;
122126
}
123127

124128
bool RenderPass::SetVertexBuffer(VertexBuffer buffer) {
125-
if (!SetVertexBuffer(&buffer.vertex_buffer, 1u, buffer.vertex_count)) {
129+
if (!SetVertexBuffer(&buffer.vertex_buffer, 1u)) {
126130
return false;
127131
}
128132
if (!SetIndexBuffer(buffer.index_buffer, buffer.index_type)) {
129133
return false;
130134
}
135+
SetElementCount(buffer.vertex_count);
131136

132137
return true;
133138
}
134139

135-
bool RenderPass::SetVertexBuffer(BufferView vertex_buffer,
136-
size_t vertex_count) {
137-
return SetVertexBuffer(&vertex_buffer, 1, vertex_count);
140+
bool RenderPass::SetVertexBuffer(BufferView vertex_buffer) {
141+
return SetVertexBuffer(&vertex_buffer, 1);
138142
}
139143

140-
bool RenderPass::SetVertexBuffer(std::vector<BufferView> vertex_buffers,
141-
size_t vertex_count) {
142-
return SetVertexBuffer(vertex_buffers.data(), vertex_buffers.size(),
143-
vertex_count);
144+
bool RenderPass::SetVertexBuffer(std::vector<BufferView> vertex_buffers) {
145+
return SetVertexBuffer(vertex_buffers.data(), vertex_buffers.size());
144146
}
145147

146148
bool RenderPass::SetVertexBuffer(BufferView vertex_buffers[],
147-
size_t vertex_buffer_count,
148-
size_t vertex_count) {
149+
size_t vertex_buffer_count) {
149150
if (!ValidateVertexBuffers(vertex_buffers, vertex_buffer_count)) {
150151
return false;
151152
}
152153

153-
pending_.vertex_count = vertex_count;
154154
pending_.vertex_buffer_count = vertex_buffer_count;
155155
for (size_t i = 0; i < vertex_buffer_count; i++) {
156156
pending_.vertex_buffers[i] = std::move(vertex_buffers[i]);

0 commit comments

Comments
 (0)