Skip to content

Commit

Permalink
Support 16bit index buffers (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdero authored and dnfield committed Apr 27, 2022
1 parent e768f4d commit 88f4c24
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 1 deletion.
9 changes: 9 additions & 0 deletions impeller/renderer/backend/metal/formats_mtl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 5 additions & 1 deletion impeller/renderer/backend/metal/render_pass_mtl.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down
6 changes: 6 additions & 0 deletions impeller/renderer/command.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
1 change: 1 addition & 0 deletions impeller/renderer/command.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions impeller/renderer/formats.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,12 @@ enum class WindingOrder {
kCounterClockwise,
};

enum class IndexType {
kUnknown,
k16bit,
k32bit,
};

enum class PrimitiveType {
kTriangle,
kTriangleStrip,
Expand Down
2 changes: 2 additions & 0 deletions impeller/renderer/vertex_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
#pragma once

#include "impeller/renderer/buffer_view.h"
#include "impeller/renderer/formats.h"

namespace impeller {

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<bool>(vertex_buffer) && static_cast<bool>(index_buffer);
Expand Down
12 changes: 12 additions & 0 deletions impeller/renderer/vertex_buffer_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -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); }
Expand Down Expand Up @@ -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;
};

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

Expand Down

0 comments on commit 88f4c24

Please sign in to comment.