Wire up vertex buffers.

This commit is contained in:
Chinmay Garde
2021-06-19 17:00:33 -07:00
committed by Dan Field
parent ca5b3ac286
commit c23d48d7a5
8 changed files with 124 additions and 20 deletions

View File

@@ -50,6 +50,8 @@ impeller_component("compositor") {
"surface.mm",
"texture.h",
"texture.mm",
"vertex_buffer.h",
"vertex_buffer.mm",
"vertex_descriptor.h",
"vertex_descriptor.mm",
]

View File

@@ -30,6 +30,10 @@ class HostBuffer final : public std::enable_shared_from_this<HostBuffer>,
return Emplace(reinterpret_cast<const void*>(&t), sizeof(T), alignof(T));
}
[[nodiscard]] BufferView Emplace(const void* buffer,
size_t length,
size_t align);
[[nodiscard]] bool Truncate(size_t length);
private:
@@ -44,10 +48,6 @@ class HostBuffer final : public std::enable_shared_from_this<HostBuffer>,
std::shared_ptr<const DeviceBuffer> GetDeviceBuffer(
Allocator& allocator) const override;
[[nodiscard]] BufferView Emplace(const void* buffer,
size_t length,
size_t align);
[[nodiscard]] BufferView Emplace(const void* buffer, size_t length);
[[nodiscard]] bool Reserve(size_t reserved);

View File

@@ -228,14 +228,26 @@ bool RenderPass::EncodeCommands(Allocator& allocator,
ShaderStage::kFragment)) {
return false;
}
// [pass drawIndexedPrimitives:MTLPrimitiveTypeTriangleStrip
// indexCount:command.index_count
// indexType:MTLIndexTypeUInt32
// indexBuffer:command.index_buffer
// indexBufferOffset:(NSUInteger)indexBufferOffset
// instanceCount:1u
// baseVertex:0u
// baseInstance:0u];
auto index_buffer = command.index_buffer.buffer;
if (!index_buffer) {
return false;
}
auto device_buffer = index_buffer->GetDeviceBuffer(allocator);
if (!device_buffer) {
return false;
}
auto mtl_index_buffer = device_buffer->GetMTLBuffer();
if (!mtl_index_buffer) {
return false;
}
[pass drawIndexedPrimitives:MTLPrimitiveTypeTriangleStrip
indexCount:command.index_count
indexType:MTLIndexTypeUInt32
indexBuffer:mtl_index_buffer
indexBufferOffset:command.index_buffer.range.offset
instanceCount:1u
baseVertex:0u
baseInstance:0u];
}
return true;
}

View File

@@ -0,0 +1,39 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#pragma once
#include <initializer_list>
#include <map>
#include <vector>
#include "flutter/fml/macros.h"
#include "impeller/compositor/host_buffer.h"
#include "impeller/geometry/vector.h"
namespace impeller {
class VertexBufferBuilder {
public:
VertexBufferBuilder();
~VertexBufferBuilder();
VertexBufferBuilder& AddVertices(std::initializer_list<Vector3> vertices);
BufferView CreateVertexBuffer(HostBuffer& buffer) const;
BufferView CreateIndexBuffer(HostBuffer& buffer) const;
size_t GetIndexCount() const;
private:
// This is a placeholder till vertex de-duplication can be implemented. The
// current implementation is a very dumb placeholder.
std::vector<Vector3> vertices_;
FML_DISALLOW_COPY_AND_ASSIGN(VertexBufferBuilder);
};
} // namespace impeller

View File

@@ -0,0 +1,45 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "impeller/compositor/vertex_buffer.h"
namespace impeller {
VertexBufferBuilder::VertexBufferBuilder() = default;
VertexBufferBuilder::~VertexBufferBuilder() = default;
size_t VertexBufferBuilder::GetIndexCount() const {
return vertices_.size();
}
VertexBufferBuilder& VertexBufferBuilder::AddVertices(
std::initializer_list<Vector3> vertices) {
for (const auto& vertex : vertices) {
vertices_.push_back(vertex);
}
return *this;
}
BufferView VertexBufferBuilder::CreateVertexBuffer(HostBuffer& buffer) const {
return buffer.Emplace(
vertices_.data(),
vertices_.size() * sizeof(decltype(vertices_)::value_type),
alignof(decltype(vertices_)::value_type));
}
BufferView VertexBufferBuilder::CreateIndexBuffer(HostBuffer& buffer) const {
// Soooo dumb! We don't actually need an index buffer right now. But we will
// once de-duplication is done.
std::vector<uint32_t> index_buffer;
for (size_t i = 0; i < vertices_.size(); i++) {
index_buffer.push_back(i);
}
return buffer.Emplace(
index_buffer.data(),
index_buffer.size() * sizeof(decltype(index_buffer)::value_type),
alignof(decltype(index_buffer)::value_type));
}
} // namespace impeller

View File

@@ -4,6 +4,7 @@
#include "impeller/entity/entity_renderer.h"
#include "impeller/compositor/command.h"
#include "impeller/compositor/vertex_buffer.h"
#include "impeller/primitives/box.frag.h"
#include "impeller/primitives/box.vert.h"
@@ -37,12 +38,24 @@ bool EntityRenderer::OnIsValid() const {
bool EntityRenderer::OnRender(RenderPass& pass) {
shader::BoxVertexInfo::UniformBuffer uniforms;
uniforms.mvp = Matrix::MakeOrthographic({800, 600});
VertexBufferBuilder vertices;
vertices.AddVertices({
{-0.5, 0.5}, //
{0.5, 0.5}, //
{0.5, -0.5}, //
{-0.5, 0.5}, //
});
Command cmd;
cmd.pipeline = box_primitive_->GetPipeline();
cmd.vertex_bindings
.buffers[shader::BoxVertexInfo::kUniformUniformBuffer.location] =
pass.GetTransientsBuffer().Emplace(uniforms);
cmd.vertex_bindings
.buffers[shader::BoxVertexInfo::kInputVertexPosition.location] =
vertices.CreateVertexBuffer(pass.GetTransientsBuffer());
cmd.index_buffer = vertices.CreateIndexBuffer(pass.GetTransientsBuffer());
cmd.index_count = vertices.GetIndexCount();
if (!pass.RecordCommand(std::move(cmd))) {
return false;
}

View File

@@ -2,11 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
in vec3 color;
in vec3 color2;
layout(location = 0) out vec4 fragColor;
void main() {
fragColor = vec4(color * color2, 1.0);
fragColor = vec4(1.0, 1.0, 1.0, 1.0);
}

View File

@@ -8,10 +8,6 @@ uniform UniformBuffer {
in vec3 vertexPosition;
out vec3 color;
out vec3 color2;
void main() {
gl_Position = uniforms.mvp * vec4(vertexPosition, 1.0);
color = vec3(1.0, 1.0, 1.0);
}