Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions impeller/core/formats.h
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,8 @@ enum class IndexType {
kUnknown,
k16bit,
k32bit,
/// Does not use the index buffer.
kNone,
};

enum class PrimitiveType {
Expand Down
7 changes: 5 additions & 2 deletions impeller/core/vertex_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
};

Expand Down
6 changes: 3 additions & 3 deletions impeller/display_list/dl_vertices_geometry.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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()) *
Expand Down Expand Up @@ -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()) *
Expand Down Expand Up @@ -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()) *
Expand Down
20 changes: 7 additions & 13 deletions impeller/entity/geometry.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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(),
Expand Down Expand Up @@ -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{
Expand All @@ -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;
});
Expand Down Expand Up @@ -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()),
Expand Down Expand Up @@ -766,18 +763,15 @@ 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,
.vertex_buffer =
{
.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(),
Expand Down
2 changes: 1 addition & 1 deletion impeller/playground/imgui/imgui_impl_impeller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions impeller/renderer/backend/gles/formats_gles.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions impeller/renderer/backend/gles/proc_table_gles.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ struct GLProc {
PROC(DetachShader); \
PROC(Disable); \
PROC(DisableVertexAttribArray); \
PROC(DrawArrays); \
PROC(DrawElements); \
PROC(Enable); \
PROC(EnableVertexAttribArray); \
Expand Down
37 changes: 21 additions & 16 deletions impeller/renderer/backend/gles/render_pass_gles.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
32 changes: 26 additions & 6 deletions impeller/renderer/backend/metal/render_pass_mtl.mm
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ static bool Bind(PassBindingsCache& pass,

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) {
Expand Down Expand Up @@ -486,6 +486,28 @@ static bool Bind(PassBindingsCache& pass,
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;
}
Expand All @@ -503,9 +525,7 @@ static bool Bind(PassBindingsCache& pass,
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);

Expand All @@ -515,7 +535,7 @@ static bool Bind(PassBindingsCache& pass,
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
Expand All @@ -524,7 +544,7 @@ static bool Bind(PassBindingsCache& pass,
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];
Expand Down
2 changes: 2 additions & 0 deletions impeller/renderer/backend/vulkan/formats_vk.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
65 changes: 44 additions & 21 deletions impeller/renderer/backend/vulkan/render_pass_vk.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand All @@ -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));

// 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
);
if (command.index_type != IndexType::kNone) {
// Bind the index buffer.
auto index_buffer_view = command.index_buffer;
if (!index_buffer_view) {
return false;
}

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;
}

Expand Down
2 changes: 1 addition & 1 deletion impeller/renderer/command.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Loading