From 88f4c24ac908c9d94ff018062d62077c4e453dfb Mon Sep 17 00:00:00 2001 From: Brandon DeRosier Date: Wed, 16 Feb 2022 14:35:08 -0800 Subject: [PATCH] Support 16bit index buffers (#11) --- impeller/renderer/backend/metal/formats_mtl.h | 9 +++++++++ impeller/renderer/backend/metal/render_pass_mtl.mm | 6 +++++- impeller/renderer/command.cc | 6 ++++++ impeller/renderer/command.h | 1 + impeller/renderer/formats.h | 6 ++++++ impeller/renderer/vertex_buffer.h | 2 ++ impeller/renderer/vertex_buffer_builder.h | 12 ++++++++++++ 7 files changed, 41 insertions(+), 1 deletion(-) diff --git a/impeller/renderer/backend/metal/formats_mtl.h b/impeller/renderer/backend/metal/formats_mtl.h index 47e17843132a4..6d1b47e69dae1 100644 --- a/impeller/renderer/backend/metal/formats_mtl.h +++ b/impeller/renderer/backend/metal/formats_mtl.h @@ -107,6 +107,15 @@ constexpr MTLPrimitiveType ToMTLPrimitiveType(PrimitiveType type) { return MTLPrimitiveTypePoint; } +constexpr MTLIndexType ToMTLIndexType(IndexType type) { + switch (type) { + case IndexType::k16bit: + return MTLIndexTypeUInt16; + default: + return MTLIndexTypeUInt32; + } +} + constexpr MTLBlendOperation ToMTLBlendOperation(BlendOperation type) { switch (type) { case BlendOperation::kAdd: diff --git a/impeller/renderer/backend/metal/render_pass_mtl.mm b/impeller/renderer/backend/metal/render_pass_mtl.mm index 6a685d975c271..f50389a2d7d04 100644 --- a/impeller/renderer/backend/metal/render_pass_mtl.mm +++ b/impeller/renderer/backend/metal/render_pass_mtl.mm @@ -13,6 +13,7 @@ #include "impeller/renderer/backend/metal/pipeline_mtl.h" #include "impeller/renderer/backend/metal/sampler_mtl.h" #include "impeller/renderer/backend/metal/texture_mtl.h" +#include "impeller/renderer/formats.h" #include "impeller/renderer/host_buffer.h" #include "impeller/renderer/shader_types.h" @@ -434,6 +435,9 @@ static bool Bind(PassBindingsCache& pass, ShaderStage::kFragment)) { return false; } + if (command.index_type == IndexType::kUnknown) { + return false; + } auto index_buffer = command.index_buffer.buffer; if (!index_buffer) { return false; @@ -452,7 +456,7 @@ static bool Bind(PassBindingsCache& pass, // Returns void. All error checking must be done by this point. [encoder drawIndexedPrimitives:ToMTLPrimitiveType(command.primitive_type) indexCount:command.index_count - indexType:MTLIndexTypeUInt32 + indexType:ToMTLIndexType(command.index_type) indexBuffer:mtl_index_buffer indexBufferOffset:command.index_buffer.range.offset instanceCount:1u diff --git a/impeller/renderer/command.cc b/impeller/renderer/command.cc index aca7d2b31cecb..cbdb529c8467b 100644 --- a/impeller/renderer/command.cc +++ b/impeller/renderer/command.cc @@ -4,15 +4,21 @@ #include "impeller/renderer/command.h" +#include "impeller/renderer/formats.h" #include "impeller/renderer/vertex_descriptor.h" namespace impeller { bool Command::BindVertices(const VertexBuffer& buffer) { + if (index_type == IndexType::kUnknown) { + return false; + } + vertex_bindings.buffers[VertexDescriptor::kReservedVertexBufferIndex] = buffer.vertex_buffer; index_buffer = buffer.index_buffer; index_count = buffer.index_count; + index_type = buffer.index_type; return true; } diff --git a/impeller/renderer/command.h b/impeller/renderer/command.h index e0d18193ad32b..4890d6ffaefe1 100644 --- a/impeller/renderer/command.h +++ b/impeller/renderer/command.h @@ -64,6 +64,7 @@ struct Command { /// BufferView index_buffer; size_t index_count = 0u; + IndexType index_type = IndexType::kUnknown; std::string label; PrimitiveType primitive_type = PrimitiveType::kTriangle; WindingOrder winding = WindingOrder::kClockwise; diff --git a/impeller/renderer/formats.h b/impeller/renderer/formats.h index 796c6258387c7..0d420ee1097da 100644 --- a/impeller/renderer/formats.h +++ b/impeller/renderer/formats.h @@ -133,6 +133,12 @@ enum class WindingOrder { kCounterClockwise, }; +enum class IndexType { + kUnknown, + k16bit, + k32bit, +}; + enum class PrimitiveType { kTriangle, kTriangleStrip, diff --git a/impeller/renderer/vertex_buffer.h b/impeller/renderer/vertex_buffer.h index 596731fa72bc0..35b9743db8621 100644 --- a/impeller/renderer/vertex_buffer.h +++ b/impeller/renderer/vertex_buffer.h @@ -5,6 +5,7 @@ #pragma once #include "impeller/renderer/buffer_view.h" +#include "impeller/renderer/formats.h" namespace impeller { @@ -12,6 +13,7 @@ struct VertexBuffer { BufferView vertex_buffer; BufferView index_buffer; size_t index_count = 0u; + IndexType index_type = IndexType::kUnknown; constexpr operator bool() const { return static_cast(vertex_buffer) && static_cast(index_buffer); diff --git a/impeller/renderer/vertex_buffer_builder.h b/impeller/renderer/vertex_buffer_builder.h index c217241e4861c..1caac9032ae8f 100644 --- a/impeller/renderer/vertex_buffer_builder.h +++ b/impeller/renderer/vertex_buffer_builder.h @@ -29,6 +29,16 @@ class VertexBufferBuilder { ~VertexBufferBuilder() = default; + constexpr impeller::IndexType GetIndexType() const { + if constexpr (sizeof(IndexType) == 2) { + return impeller::IndexType::k16bit; + } else if (sizeof(IndexType) == 4) { + return impeller::IndexType::k32bit; + } else { + return impeller::IndexType::kUnknown; + } + } + void SetLabel(std::string label) { label_ = std::move(label); } void Reserve(size_t count) { return vertices_.reserve(count); } @@ -56,6 +66,7 @@ class VertexBufferBuilder { buffer.vertex_buffer = CreateVertexBufferView(host_buffer); buffer.index_buffer = CreateIndexBufferView(host_buffer); buffer.index_count = GetIndexCount(); + buffer.index_type = GetIndexType(); return buffer; }; @@ -65,6 +76,7 @@ class VertexBufferBuilder { buffer.vertex_buffer = CreateVertexBufferView(device_allocator); buffer.index_buffer = CreateIndexBufferView(device_allocator); buffer.index_count = GetIndexCount(); + buffer.index_type = GetIndexType(); return buffer; };