[Impeller] Make index buffer optional (flutter/engine#41962)

Makes the index buffer optional. For calls that were just filling the index buffer with e.g. `std::iota`, it was wasteful. @jonahwilliams has also found cases where the index buffer will run out of room for drawPoints with lots of points.

Fixes https://github.com/flutter/flutter/issues/126572
This commit is contained in:
Dan Field
2023-05-11 22:12:22 -07:00
committed by GitHub
parent 46f623176e
commit d1c3dac55d
18 changed files with 137 additions and 91 deletions

View File

@@ -308,6 +308,8 @@ enum class IndexType {
kUnknown,
k16bit,
k32bit,
/// Does not use the index buffer.
kNone,
};
enum class PrimitiveType {

View File

@@ -12,11 +12,14 @@ namespace impeller {
struct VertexBuffer {
BufferView vertex_buffer;
BufferView index_buffer;
size_t index_count = 0u;
// The total count of vertices, either in the vertex_buffer if the
// index_type is IndexType::kNone or in the index_buffer otherwise.
size_t vertex_count = 0u;
IndexType index_type = IndexType::kUnknown;
constexpr operator bool() const {
return static_cast<bool>(vertex_buffer) && static_cast<bool>(index_buffer);
return static_cast<bool>(vertex_buffer) &&
(index_type == IndexType::kNone || static_cast<bool>(index_buffer));
}
};

View File

@@ -175,7 +175,7 @@ GeometryResult DlVerticesGeometry::GetPositionBuffer(
.index_buffer = {.buffer = buffer,
.range =
Range{total_vtx_bytes, total_idx_bytes}},
.index_count = index_count,
.vertex_count = index_count,
.index_type = IndexType::k16bit,
},
.transform = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) *
@@ -244,7 +244,7 @@ GeometryResult DlVerticesGeometry::GetPositionColorBuffer(
.index_buffer = {.buffer = buffer,
.range =
Range{total_vtx_bytes, total_idx_bytes}},
.index_count = index_count,
.vertex_count = index_count,
.index_type = IndexType::k16bit,
},
.transform = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) *
@@ -321,7 +321,7 @@ GeometryResult DlVerticesGeometry::GetPositionUVBuffer(
.index_buffer = {.buffer = buffer,
.range =
Range{total_vtx_bytes, total_idx_bytes}},
.index_count = index_count,
.vertex_count = index_count,
.index_type = IndexType::k16bit,
},
.transform = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) *

View File

@@ -91,7 +91,6 @@ static GeometryResult ComputeUVGeometryForRect(Rect source_rect,
const ContentContext& renderer,
const Entity& entity,
RenderPass& pass) {
constexpr uint16_t kRectIndicies[4] = {0, 1, 2, 3};
auto& host_buffer = pass.GetTransientsBuffer();
std::vector<Point> data(8);
@@ -108,10 +107,8 @@ static GeometryResult ComputeUVGeometryForRect(Rect source_rect,
{
.vertex_buffer = host_buffer.Emplace(
data.data(), 16 * sizeof(float), alignof(float)),
.index_buffer = host_buffer.Emplace(
kRectIndicies, 4 * sizeof(uint16_t), alignof(uint16_t)),
.index_count = 4,
.index_type = IndexType::k16bit,
.vertex_count = 4,
.index_type = IndexType::kNone,
},
.transform = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) *
entity.GetTransformation(),
@@ -141,7 +138,7 @@ GeometryResult FillPathGeometry::GetPositionBuffer(
points.data(), points.size() * sizeof(Point), alignof(Point));
vertex_buffer.index_buffer = host_buffer.Emplace(
indices.data(), indices.size() * sizeof(uint16_t), alignof(uint16_t));
vertex_buffer.index_count = indices.size();
vertex_buffer.vertex_count = indices.size();
vertex_buffer.index_type = IndexType::k16bit;
return GeometryResult{
@@ -163,7 +160,7 @@ GeometryResult FillPathGeometry::GetPositionBuffer(
vertices, vertices_count * sizeof(float), alignof(float));
vertex_buffer.index_buffer = host_buffer.Emplace(
indices, indices_count * sizeof(uint16_t), alignof(uint16_t));
vertex_buffer.index_count = indices_count;
vertex_buffer.vertex_count = indices_count;
vertex_buffer.index_type = IndexType::k16bit;
return true;
});
@@ -729,7 +726,7 @@ GeometryResult CoverGeometry::GetPositionBuffer(const ContentContext& renderer,
rect.GetPoints().data(), 8 * sizeof(float), alignof(float)),
.index_buffer = host_buffer.Emplace(
kRectIndicies, 4 * sizeof(uint16_t), alignof(uint16_t)),
.index_count = 4,
.vertex_count = 4,
.index_type = IndexType::k16bit,
},
.transform = Matrix::MakeOrthographic(pass.GetRenderTargetSize()),
@@ -766,7 +763,6 @@ RectGeometry::~RectGeometry() = default;
GeometryResult RectGeometry::GetPositionBuffer(const ContentContext& renderer,
const Entity& entity,
RenderPass& pass) {
constexpr uint16_t kRectIndicies[4] = {0, 1, 2, 3};
auto& host_buffer = pass.GetTransientsBuffer();
return GeometryResult{
.type = PrimitiveType::kTriangleStrip,
@@ -774,10 +770,8 @@ GeometryResult RectGeometry::GetPositionBuffer(const ContentContext& renderer,
{
.vertex_buffer = host_buffer.Emplace(
rect_.GetPoints().data(), 8 * sizeof(float), alignof(float)),
.index_buffer = host_buffer.Emplace(
kRectIndicies, 4 * sizeof(uint16_t), alignof(uint16_t)),
.index_count = 4,
.index_type = IndexType::k16bit,
.vertex_count = 4,
.index_type = IndexType::kNone,
},
.transform = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) *
entity.GetTransformation(),

View File

@@ -256,7 +256,7 @@ void ImGui_ImplImpeller_RenderDrawData(ImDrawData* draw_data,
.range = impeller::Range(
index_buffer_offset + pcmd->IdxOffset * sizeof(ImDrawIdx),
pcmd->ElemCount * sizeof(ImDrawIdx))};
vertex_buffer.index_count = pcmd->ElemCount;
vertex_buffer.vertex_count = pcmd->ElemCount;
vertex_buffer.index_type = impeller::IndexType::k16bit;
cmd.BindVertices(vertex_buffer);
cmd.base_vertex = pcmd->VtxOffset;

View File

@@ -33,6 +33,7 @@ constexpr GLenum ToMode(PrimitiveType primitive_type) {
constexpr GLenum ToIndexType(IndexType type) {
switch (type) {
case IndexType::kUnknown:
case IndexType::kNone:
FML_UNREACHABLE();
case IndexType::k16bit:
return GL_UNSIGNED_SHORT;

View File

@@ -116,6 +116,7 @@ struct GLProc {
PROC(DetachShader); \
PROC(Disable); \
PROC(DisableVertexAttribArray); \
PROC(DrawArrays); \
PROC(DrawElements); \
PROC(Enable); \
PROC(EnableVertexAttribArray); \

View File

@@ -356,18 +356,15 @@ struct RenderPassData {
/// Bind vertex and index buffers.
///
auto vertex_buffer_view = command.GetVertexBuffer();
auto index_buffer_view = command.index_buffer;
if (!vertex_buffer_view || !index_buffer_view) {
if (!vertex_buffer_view) {
return false;
}
auto vertex_buffer =
vertex_buffer_view.buffer->GetDeviceBuffer(*transients_allocator);
auto index_buffer =
index_buffer_view.buffer->GetDeviceBuffer(*transients_allocator);
if (!vertex_buffer || !index_buffer) {
if (!vertex_buffer) {
return false;
}
@@ -376,11 +373,6 @@ struct RenderPassData {
DeviceBufferGLES::BindingType::kArrayBuffer)) {
return false;
}
const auto& index_buffer_gles = DeviceBufferGLES::Cast(*index_buffer);
if (!index_buffer_gles.BindAndUploadDataIfNecessary(
DeviceBufferGLES::BindingType::kElementArrayBuffer)) {
return false;
}
//--------------------------------------------------------------------------
/// Bind the pipeline program.
@@ -423,12 +415,25 @@ struct RenderPassData {
//--------------------------------------------------------------------------
/// Finally! Invoke the draw call.
///
gl.DrawElements(mode, // mode
command.index_count, // count
ToIndexType(command.index_type), // type
reinterpret_cast<const GLvoid*>(static_cast<GLsizei>(
index_buffer_view.range.offset)) // indices
);
if (command.index_type == IndexType::kNone) {
gl.DrawArrays(mode, command.base_vertex, command.vertex_count);
} else {
// Bind the index buffer if necessary.
auto index_buffer_view = command.index_buffer;
auto index_buffer =
index_buffer_view.buffer->GetDeviceBuffer(*transients_allocator);
const auto& index_buffer_gles = DeviceBufferGLES::Cast(*index_buffer);
if (!index_buffer_gles.BindAndUploadDataIfNecessary(
DeviceBufferGLES::BindingType::kElementArrayBuffer)) {
return false;
}
gl.DrawElements(mode, // mode
command.vertex_count, // count
ToIndexType(command.index_type), // type
reinterpret_cast<const GLvoid*>(static_cast<GLsizei>(
index_buffer_view.range.offset)) // indices
);
}
//--------------------------------------------------------------------------
/// Unbind vertex attribs.

View File

@@ -436,7 +436,7 @@ bool RenderPassMTL::EncodeCommands(const std::shared_ptr<Allocator>& allocator,
fml::closure pop_debug_marker = [encoder]() { [encoder popDebugGroup]; };
for (const auto& command : commands_) {
if (command.index_count == 0u) {
if (command.vertex_count == 0u) {
continue;
}
if (command.instance_count == 0u) {
@@ -486,6 +486,28 @@ bool RenderPassMTL::EncodeCommands(const std::shared_ptr<Allocator>& allocator,
ShaderStage::kFragment)) {
return false;
}
const PrimitiveType primitive_type = pipeline_desc.GetPrimitiveType();
if (command.index_type == IndexType::kNone) {
if (command.instance_count != 1u) {
#if TARGET_OS_SIMULATOR
VALIDATION_LOG << "iOS Simulator does not support instanced rendering.";
return false;
#endif
[encoder drawPrimitives:ToMTLPrimitiveType(primitive_type)
vertexStart:command.base_vertex
vertexCount:command.vertex_count
instanceCount:command.instance_count
baseInstance:0u];
} else {
[encoder drawPrimitives:ToMTLPrimitiveType(primitive_type)
vertexStart:command.base_vertex
vertexCount:command.vertex_count];
}
continue;
}
if (command.index_type == IndexType::kUnknown) {
return false;
}
@@ -503,9 +525,7 @@ bool RenderPassMTL::EncodeCommands(const std::shared_ptr<Allocator>& allocator,
return false;
}
const PrimitiveType primitive_type = pipeline_desc.GetPrimitiveType();
FML_DCHECK(command.index_count *
FML_DCHECK(command.vertex_count *
(command.index_type == IndexType::k16bit ? 2 : 4) ==
command.index_buffer.range.length);
@@ -515,7 +535,7 @@ bool RenderPassMTL::EncodeCommands(const std::shared_ptr<Allocator>& allocator,
return false;
#endif
[encoder drawIndexedPrimitives:ToMTLPrimitiveType(primitive_type)
indexCount:command.index_count
indexCount:command.vertex_count
indexType:ToMTLIndexType(command.index_type)
indexBuffer:mtl_index_buffer
indexBufferOffset:command.index_buffer.range.offset
@@ -524,7 +544,7 @@ bool RenderPassMTL::EncodeCommands(const std::shared_ptr<Allocator>& allocator,
baseInstance:0u];
} else {
[encoder drawIndexedPrimitives:ToMTLPrimitiveType(primitive_type)
indexCount:command.index_count
indexCount:command.vertex_count
indexType:ToMTLIndexType(command.index_type)
indexBuffer:mtl_index_buffer
indexBufferOffset:command.index_buffer.range.offset];

View File

@@ -330,6 +330,8 @@ constexpr vk::IndexType ToVKIndexType(IndexType index_type) {
return vk::IndexType::eUint32;
case IndexType::kUnknown:
return vk::IndexType::eUint32;
case IndexType::kNone:
FML_UNREACHABLE();
}
FML_UNREACHABLE();

View File

@@ -429,7 +429,7 @@ static bool EncodeCommand(const Context& context,
const Command& command,
CommandEncoderVK& encoder,
const ISize& target_size) {
if (command.index_count == 0u || command.instance_count == 0u) {
if (command.vertex_count == 0u || command.instance_count == 0u) {
return true;
}
@@ -466,24 +466,22 @@ static bool EncodeCommand(const Context& context,
// Configure vertex and index and buffers for binding.
auto vertex_buffer_view = command.GetVertexBuffer();
auto index_buffer_view = command.index_buffer;
if (!vertex_buffer_view || !index_buffer_view) {
if (!vertex_buffer_view) {
return false;
}
auto& allocator = *context.GetResourceAllocator();
auto vertex_buffer = vertex_buffer_view.buffer->GetDeviceBuffer(allocator);
auto index_buffer = index_buffer_view.buffer->GetDeviceBuffer(allocator);
if (!vertex_buffer || !index_buffer) {
VALIDATION_LOG << "Failed to acquire device buffers"
<< " for vertex and index buffer views";
if (!vertex_buffer) {
VALIDATION_LOG << "Failed to acquire device buffer"
<< " for vertex buffer view";
return false;
}
if (!encoder.Track(vertex_buffer) || !encoder.Track(index_buffer)) {
if (!encoder.Track(vertex_buffer)) {
return false;
}
@@ -493,19 +491,44 @@ static bool EncodeCommand(const Context& context,
vk::DeviceSize vertex_buffer_offsets[] = {vertex_buffer_view.range.offset};
cmd_buffer.bindVertexBuffers(0u, 1u, vertex_buffers, vertex_buffer_offsets);
// Bind the index buffer.
auto index_buffer_handle = DeviceBufferVK::Cast(*index_buffer).GetBuffer();
cmd_buffer.bindIndexBuffer(index_buffer_handle,
index_buffer_view.range.offset,
ToVKIndexType(command.index_type));
if (command.index_type != IndexType::kNone) {
// Bind the index buffer.
auto index_buffer_view = command.index_buffer;
if (!index_buffer_view) {
return false;
}
// Engage!
cmd_buffer.drawIndexed(command.index_count, // index count
command.instance_count, // instance count
0u, // first index
command.base_vertex, // vertex offset
0u // first instance
);
auto index_buffer = index_buffer_view.buffer->GetDeviceBuffer(allocator);
if (!index_buffer) {
VALIDATION_LOG << "Failed to acquire device buffer"
<< " for index buffer view";
return false;
}
if (!encoder.Track(index_buffer)) {
return false;
}
auto index_buffer_handle = DeviceBufferVK::Cast(*index_buffer).GetBuffer();
cmd_buffer.bindIndexBuffer(index_buffer_handle,
index_buffer_view.range.offset,
ToVKIndexType(command.index_type));
// Engage!
cmd_buffer.drawIndexed(command.vertex_count, // index count
command.instance_count, // instance count
0u, // first index
command.base_vertex, // vertex offset
0u // first instance
);
} else {
FML_LOG(ERROR) << "HERE!!!";
cmd_buffer.draw(command.vertex_count, // vertex count
command.instance_count, // instance count
command.base_vertex, // vertex offset
0u // first instance
);
}
return true;
}

View File

@@ -21,7 +21,7 @@ bool Command::BindVertices(const VertexBuffer& buffer) {
vertex_bindings.buffers[VertexDescriptor::kReservedVertexBufferIndex] = {
nullptr, buffer.vertex_buffer};
index_buffer = buffer.index_buffer;
index_count = buffer.index_count;
vertex_count = buffer.vertex_count;
index_type = buffer.index_type;
return true;
}

View File

@@ -102,12 +102,15 @@ struct Command : public ResourceBinder {
///
BufferView index_buffer;
//----------------------------------------------------------------------------
/// The number of indices to use from the index buffer. Set the vertex and
/// The number of vertices to draw.
///
/// If the index_type is `IndexType::kNone`, this is a count into the vertex
/// buffer. Otherwise, it is a count into the index buffer. Set the vertex and
/// index buffers as well as the index count using a call to `BindVertices`.
///
/// @see `BindVertices`
///
size_t index_count = 0u;
size_t vertex_count = 0u;
//----------------------------------------------------------------------------
/// The type of indices in the index buffer. The indices must be tightly
/// packed in the index buffer.

View File

@@ -45,7 +45,14 @@ TEST_P(ComputeSubgroupTest, PathPlayground) {
auto context = GetContext();
ASSERT_TRUE(context);
ASSERT_TRUE(context->GetCapabilities()->SupportsComputeSubgroups());
char svg_path_data[16384] = "M0 0 L20 20";
char svg_path_data[16384] =
"M140 20 "
"C73 20 20 74 20 140 "
"c0 135 136 170 228 303 "
"88-132 229-173 229-303 "
"0-66-54-120-120-120 "
"-48 0-90 28-109 69 "
"-19-41-60-69-108-69z";
size_t vertex_count = 0;
Scalar stroke_width = 1.0;
@@ -130,16 +137,11 @@ TEST_P(ComputeSubgroupTest, PathPlayground) {
auto count = reinterpret_cast<SS::VertexBufferCount*>(
vertex_buffer_count->AsBufferView().contents)
->count;
auto& host_buffer = pass.GetTransientsBuffer();
std::vector<uint16_t> indices(count);
std::iota(std::begin(indices), std::end(indices), 0);
VertexBuffer render_vertex_buffer{
.vertex_buffer = vertex_buffer->AsBufferView(),
.index_buffer = host_buffer.Emplace(
indices.data(), count * sizeof(uint16_t), alignof(uint16_t)),
.index_count = count,
.index_type = IndexType::k16bit};
.vertex_count = count,
.index_type = IndexType::kNone};
cmd.BindVertices(render_vertex_buffer);
VS::FrameInfo frame_info;
@@ -343,16 +345,11 @@ TEST_P(ComputeSubgroupTest, LargePath) {
auto count = reinterpret_cast<SS::VertexBufferCount*>(
vertex_buffer_count->AsBufferView().contents)
->count;
auto& host_buffer = pass.GetTransientsBuffer();
std::vector<uint16_t> indices(count);
std::iota(std::begin(indices), std::end(indices), 0);
VertexBuffer render_vertex_buffer{
.vertex_buffer = vertex_buffer->AsBufferView(),
.index_buffer = host_buffer.Emplace(
indices.data(), count * sizeof(uint16_t), alignof(uint16_t)),
.index_count = count,
.index_type = IndexType::k16bit};
.vertex_count = count,
.index_type = IndexType::kNone};
cmd.BindVertices(render_vertex_buffer);
VS::FrameInfo frame_info;
@@ -436,16 +433,11 @@ TEST_P(ComputeSubgroupTest, QuadAndCubicInOnePath) {
auto count = reinterpret_cast<SS::VertexBufferCount*>(
vertex_buffer_count->AsBufferView().contents)
->count;
auto& host_buffer = pass.GetTransientsBuffer();
std::vector<uint16_t> indices(count);
std::iota(std::begin(indices), std::end(indices), 0);
VertexBuffer render_vertex_buffer{
.vertex_buffer = vertex_buffer->AsBufferView(),
.index_buffer = host_buffer.Emplace(
indices.data(), count * sizeof(uint16_t), alignof(uint16_t)),
.index_count = count,
.index_type = IndexType::k16bit};
.vertex_count = count,
.index_type = IndexType::kNone};
cmd.BindVertices(render_vertex_buffer);
VS::FrameInfo frame_info;

View File

@@ -49,7 +49,7 @@ bool RenderPass::AddCommand(Command command) {
}
}
if (command.index_count == 0u) {
if (command.vertex_count == 0u) {
// Essentially a no-op. Don't record the command but this is not necessary
// an error either.
return true;

View File

@@ -163,7 +163,7 @@ TEST_P(RendererTest, CanRenderPerspectiveCube) {
vertex_buffer.index_buffer = {
.buffer = device_buffer,
.range = Range(offsetof(Cube, indices), sizeof(Cube::indices))};
vertex_buffer.index_count = 36;
vertex_buffer.vertex_count = 36;
vertex_buffer.index_type = IndexType::k16bit;
}

View File

@@ -76,7 +76,7 @@ class VertexBufferBuilder {
VertexBuffer buffer;
buffer.vertex_buffer = CreateVertexBufferView(host_buffer);
buffer.index_buffer = CreateIndexBufferView(host_buffer);
buffer.index_count = GetIndexCount();
buffer.vertex_count = GetIndexCount();
buffer.index_type = GetIndexType();
return buffer;
};
@@ -86,7 +86,7 @@ class VertexBufferBuilder {
// This can be merged into a single allocation.
buffer.vertex_buffer = CreateVertexBufferView(device_allocator);
buffer.index_buffer = CreateIndexBufferView(device_allocator);
buffer.index_count = GetIndexCount();
buffer.vertex_count = GetIndexCount();
buffer.index_type = GetIndexType();
return buffer;
};

View File

@@ -113,7 +113,7 @@ std::shared_ptr<Geometry> Geometry::MakeFromFlatbuffer(
.vertex_buffer = {.buffer = buffer, .range = Range(0, vertices_bytes)},
.index_buffer = {.buffer = buffer,
.range = Range(vertices_bytes, indices_bytes)},
.index_count = mesh.indices()->count(),
.vertex_count = mesh.indices()->count(),
.index_type = index_type,
};
return MakeVertexBuffer(std::move(vertex_buffer), is_skinned);