[Impeller] Dont synthesize fake index buffer if none was provided to VBB (flutter/engine#41969)

Followup from https://github.com/flutter/flutter/issues/126572
This commit is contained in:
Jonah Williams
2023-05-11 22:58:04 -07:00
committed by GitHub
parent d1c3dac55d
commit e8cf388701
2 changed files with 23 additions and 17 deletions

View File

@@ -1007,9 +1007,18 @@ TEST_P(RendererTest, DefaultIndexSize) {
// Default to 16bit index buffer size, as this is a reasonable default and
// supported on all backends without extensions.
VertexBufferBuilder<VS::PerVertexData> vertex_builder;
vertex_builder.AppendIndex(0u);
ASSERT_EQ(vertex_builder.GetIndexType(), IndexType::k16bit);
}
TEST_P(RendererTest, DefaultIndexBehavior) {
using VS = BoxFadeVertexShader;
// Do not create any index buffer if no indices were provided.
VertexBufferBuilder<VS::PerVertexData> vertex_builder;
ASSERT_EQ(vertex_builder.GetIndexType(), IndexType::kNone);
}
TEST_P(RendererTest, VertexBufferBuilder) {
// Does not create index buffer if one is provided.
using VS = BoxFadeVertexShader;

View File

@@ -30,13 +30,16 @@ class VertexBufferBuilder {
~VertexBufferBuilder() = default;
constexpr impeller::IndexType GetIndexType() const {
if (indices_.size() == 0) {
return impeller::IndexType::kNone;
}
if constexpr (sizeof(IndexType) == 2) {
return impeller::IndexType::k16bit;
} else if (sizeof(IndexType) == 4) {
return impeller::IndexType::k32bit;
} else {
return impeller::IndexType::kUnknown;
}
if (sizeof(IndexType) == 4) {
return impeller::IndexType::k32bit;
}
return impeller::IndexType::kUnknown;
}
void SetLabel(std::string label) { label_ = std::move(label); }
@@ -121,22 +124,13 @@ class VertexBufferBuilder {
return buffer->AsBufferView();
}
std::vector<IndexType> CreateIndexBuffer() const {
if (indices_.size() > 0) {
return indices_;
}
// So dumb! We don't actually need an index buffer right now. But we will
// once de-duplication is done. So assume this is always done.
std::vector<IndexType> index_buffer;
for (size_t i = 0; i < vertices_.size(); i++) {
index_buffer.push_back(i);
}
return index_buffer;
}
std::vector<IndexType> CreateIndexBuffer() const { return indices_; }
BufferView CreateIndexBufferView(HostBuffer& buffer) const {
const auto index_buffer = CreateIndexBuffer();
if (index_buffer.size() == 0) {
return {};
}
return buffer.Emplace(index_buffer.data(),
index_buffer.size() * sizeof(IndexType),
alignof(IndexType));
@@ -144,6 +138,9 @@ class VertexBufferBuilder {
BufferView CreateIndexBufferView(Allocator& allocator) const {
const auto index_buffer = CreateIndexBuffer();
if (index_buffer.size() == 0) {
return {};
}
auto buffer = allocator.CreateBufferWithCopy(
reinterpret_cast<const uint8_t*>(index_buffer.data()),
index_buffer.size() * sizeof(IndexType));