[Impeller] Allow binding multiple vertex buffer views. (flutter/engine#55856)

Resolves https://github.com/flutter/flutter/issues/116168.
(Continuation of https://github.com/flutter/engine/pull/49670)

Makes it possible for us to use arbitrary vertex layouts, including SoA layouts with attributes stored in different DeviceBuffers. CanRenderPerspectiveCube was converted to an SoA attribute layout with two separate buffers as an example.

Works on all the backends!

OpenGLES:
<img width="1136" alt="image" src="https://github.com/user-attachments/assets/e2398fde-532f-402d-899a-39aaa556f24f">

Vulkan:
<img width="1136" alt="image" src="https://github.com/user-attachments/assets/1c1bf664-bec1-43cb-ab2e-eb2a74718bfd">

Metal:
<img width="1136" alt="image" src="https://github.com/user-attachments/assets/bf59da17-cf52-4913-88e4-ab6f0bd6fc96">
This commit is contained in:
Brandon DeRosier
2024-10-14 18:13:52 -07:00
committed by GitHub
parent 778e32ec0d
commit 50b5307332
18 changed files with 407 additions and 133 deletions

View File

@@ -10,6 +10,8 @@
namespace impeller {
constexpr size_t kMaxVertexBuffers = 16;
struct VertexBuffer {
BufferView vertex_buffer;

View File

@@ -74,7 +74,7 @@ void RecordingRenderPass::SetInstanceCount(size_t count) {
// |RenderPass|
bool RecordingRenderPass::SetVertexBuffer(VertexBuffer buffer) {
pending_.vertex_buffer = buffer;
pending_.BindVertices(buffer);
if (delegate_) {
return delegate_->SetVertexBuffer(buffer);
}

View File

@@ -256,12 +256,8 @@ bool TextContents::Render(const ContentContext& renderer,
}
});
pass.SetVertexBuffer({
.vertex_buffer = std::move(buffer_view),
.index_buffer = {},
.vertex_count = vertex_count,
.index_type = IndexType::kNone,
});
pass.SetVertexBuffer(std::move(buffer_view), vertex_count);
pass.SetIndexBuffer({}, IndexType::kNone);
return pass.Draw().ok();
}

View File

@@ -9,6 +9,7 @@
#include <memory>
#include <vector>
#include "impeller/core/buffer_view.h"
#include "impeller/core/host_buffer.h"
#include "impeller/core/platform.h"
#include "impeller/geometry/scalar.h"
@@ -251,7 +252,7 @@ void ImGui_ImplImpeller_RenderDrawData(ImDrawData* draw_data,
vertex_buffer_offset + pcmd->VtxOffset * sizeof(ImDrawVert);
impeller::VertexBuffer vertex_buffer;
vertex_buffer.vertex_buffer = {
vertex_buffer.vertex_buffer = impeller::BufferView{
.buffer = buffer,
.range = impeller::Range(vb_start, draw_list_vtx_bytes - vb_start)};
vertex_buffer.index_buffer = {

View File

@@ -29,26 +29,35 @@ bool BufferBindingsGLES::RegisterVertexStageInput(
const ProcTableGLES& gl,
const std::vector<ShaderStageIOSlot>& p_inputs,
const std::vector<ShaderStageBufferLayout>& layouts) {
std::vector<VertexAttribPointer> vertex_attrib_arrays;
for (auto i = 0u; i < p_inputs.size(); i++) {
const auto& input = p_inputs[i];
const auto& layout = layouts[input.binding];
VertexAttribPointer attrib;
attrib.index = input.location;
// Component counts must be 1, 2, 3 or 4. Do that validation now.
if (input.vec_size < 1u || input.vec_size > 4u) {
return false;
std::vector<std::vector<VertexAttribPointer>> vertex_attrib_arrays(
layouts.size());
// Every layout corresponds to a vertex binding.
// As we record, separate the attributes into buckets for each layout in
// ascending order. We do this because later on, we'll need to associate each
// of the attributes with bound buffers corresponding to the binding.
for (auto layout_i = 0u; layout_i < layouts.size(); layout_i++) {
const auto& layout = layouts[layout_i];
for (const auto& input : p_inputs) {
if (input.binding != layout_i) {
continue;
}
VertexAttribPointer attrib;
attrib.index = input.location;
// Component counts must be 1, 2, 3 or 4. Do that validation now.
if (input.vec_size < 1u || input.vec_size > 4u) {
return false;
}
attrib.size = input.vec_size;
auto type = ToVertexAttribType(input.type);
if (!type.has_value()) {
return false;
}
attrib.type = type.value();
attrib.normalized = GL_FALSE;
attrib.offset = input.offset;
attrib.stride = layout.stride;
vertex_attrib_arrays[layout_i].push_back(attrib);
}
attrib.size = input.vec_size;
auto type = ToVertexAttribType(input.type);
if (!type.has_value()) {
return false;
}
attrib.type = type.value();
attrib.normalized = GL_FALSE;
attrib.offset = input.offset;
attrib.stride = layout.stride;
vertex_attrib_arrays.emplace_back(attrib);
}
vertex_attrib_arrays_ = std::move(vertex_attrib_arrays);
return true;
@@ -143,8 +152,13 @@ bool BufferBindingsGLES::ReadUniformsBindings(const ProcTableGLES& gl,
}
bool BufferBindingsGLES::BindVertexAttributes(const ProcTableGLES& gl,
size_t binding,
size_t vertex_offset) const {
for (const auto& array : vertex_attrib_arrays_) {
if (binding >= vertex_attrib_arrays_.size()) {
return false;
}
for (const auto& array : vertex_attrib_arrays_[binding]) {
gl.EnableVertexAttribArray(array.index);
gl.VertexAttribPointer(array.index, // index
array.size, // size (must be 1, 2, 3, or 4)
@@ -191,8 +205,11 @@ bool BufferBindingsGLES::BindUniformData(const ProcTableGLES& gl,
bool BufferBindingsGLES::UnbindVertexAttributes(const ProcTableGLES& gl) const {
for (const auto& array : vertex_attrib_arrays_) {
gl.DisableVertexAttribArray(array.index);
for (const auto& attribute : array) {
gl.DisableVertexAttribArray(attribute.index);
}
}
return true;
}

View File

@@ -33,6 +33,7 @@ class BufferBindingsGLES {
bool ReadUniformsBindings(const ProcTableGLES& gl, GLuint program);
bool BindVertexAttributes(const ProcTableGLES& gl,
size_t binding,
size_t vertex_offset) const;
bool BindUniformData(const ProcTableGLES& gl,
@@ -54,7 +55,7 @@ class BufferBindingsGLES {
GLsizei stride = 0u;
GLsizei offset = 0u;
};
std::vector<VertexAttribPointer> vertex_attrib_arrays_;
std::vector<std::vector<VertexAttribPointer>> vertex_attrib_arrays_;
std::unordered_map<std::string, GLint> uniform_locations_;

View File

@@ -141,6 +141,37 @@ struct RenderPassData {
std::string label;
};
static bool BindVertexBuffer(const ProcTableGLES& gl,
BufferBindingsGLES* vertex_desc_gles,
const BufferView& vertex_buffer_view,
size_t buffer_index) {
if (!vertex_buffer_view) {
return false;
}
auto vertex_buffer = vertex_buffer_view.buffer;
if (!vertex_buffer) {
return false;
}
const auto& vertex_buffer_gles = DeviceBufferGLES::Cast(*vertex_buffer);
if (!vertex_buffer_gles.BindAndUploadDataIfNecessary(
DeviceBufferGLES::BindingType::kArrayBuffer)) {
return false;
}
//--------------------------------------------------------------------------
/// Bind the vertex attributes associated with vertex buffer.
///
if (!vertex_desc_gles->BindVertexAttributes(
gl, buffer_index, vertex_buffer_view.range.offset)) {
return false;
}
return true;
}
[[nodiscard]] bool EncodeCommandsInReactor(
const RenderPassData& pass_data,
const std::shared_ptr<Allocator>& transients_allocator,
@@ -370,31 +401,24 @@ struct RenderPassData {
break;
}
if (command.vertex_buffer.index_type == IndexType::kUnknown) {
return false;
}
auto vertex_desc_gles = pipeline.GetBufferBindings();
if (command.index_type == IndexType::kUnknown) {
return false;
}
//--------------------------------------------------------------------------
/// Bind vertex and index buffers.
/// Bind vertex buffers.
///
auto& vertex_buffer_view = command.vertex_buffer.vertex_buffer;
if (!vertex_buffer_view) {
return false;
}
auto vertex_buffer = vertex_buffer_view.buffer;
if (!vertex_buffer) {
return false;
}
const auto& vertex_buffer_gles = DeviceBufferGLES::Cast(*vertex_buffer);
if (!vertex_buffer_gles.BindAndUploadDataIfNecessary(
DeviceBufferGLES::BindingType::kArrayBuffer)) {
return false;
/// Note: There is no need to run `RenderPass::ValidateVertexBuffers` or
/// `RenderPass::ValidateIndexBuffer` here, as validation already runs
/// when the vertex/index buffers are set on the command.
///
for (size_t i = 0; i < command.vertex_buffer_count; i++) {
if (!BindVertexBuffer(gl, vertex_desc_gles, command.vertex_buffers[i],
i)) {
return false;
}
}
//--------------------------------------------------------------------------
@@ -404,14 +428,6 @@ struct RenderPassData {
return false;
}
//--------------------------------------------------------------------------
/// Bind vertex attribs.
///
if (!vertex_desc_gles->BindVertexAttributes(
gl, vertex_buffer_view.range.offset)) {
return false;
}
//--------------------------------------------------------------------------
/// Bind uniform data.
///
@@ -438,21 +454,20 @@ struct RenderPassData {
//--------------------------------------------------------------------------
/// Finally! Invoke the draw call.
///
if (command.vertex_buffer.index_type == IndexType::kNone) {
gl.DrawArrays(mode, command.base_vertex,
command.vertex_buffer.vertex_count);
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.vertex_buffer.index_buffer;
auto index_buffer_view = command.index_buffer;
auto index_buffer = index_buffer_view.buffer;
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_buffer.vertex_count, // count
ToIndexType(command.vertex_buffer.index_type), // type
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
);

View File

@@ -83,7 +83,12 @@ class RenderPassMTL final : public RenderPass {
void SetInstanceCount(size_t count) override;
// |RenderPass|
bool SetVertexBuffer(VertexBuffer buffer) override;
bool SetVertexBuffer(BufferView vertex_buffers[],
size_t vertex_buffer_count,
size_t vertex_count) override;
// |RenderPass|
bool SetIndexBuffer(BufferView index_buffer, IndexType index_type) override;
// |RenderPass|
fml::Status Draw() override;

View File

@@ -289,22 +289,38 @@ void RenderPassMTL::SetInstanceCount(size_t count) {
}
// |RenderPass|
bool RenderPassMTL::SetVertexBuffer(VertexBuffer buffer) {
if (buffer.index_type == IndexType::kUnknown) {
bool RenderPassMTL::SetVertexBuffer(BufferView vertex_buffers[],
size_t vertex_buffer_count,
size_t vertex_count) {
if (!ValidateVertexBuffers(vertex_buffers, vertex_buffer_count)) {
return false;
}
if (!Bind(pass_bindings_, ShaderStage::kVertex,
VertexDescriptor::kReservedVertexBufferIndex,
buffer.vertex_buffer)) {
for (size_t i = 0; i < vertex_buffer_count; i++) {
if (!Bind(pass_bindings_, ShaderStage::kVertex,
VertexDescriptor::kReservedVertexBufferIndex - i,
vertex_buffers[i])) {
return false;
}
}
vertex_count_ = vertex_count;
return true;
}
// |RenderPass|
bool RenderPassMTL::SetIndexBuffer(BufferView index_buffer,
IndexType index_type) {
if (!ValidateIndexBuffer(index_buffer, index_type)) {
return false;
}
vertex_count_ = buffer.vertex_count;
if (buffer.index_type != IndexType::kNone) {
index_type_ = ToMTLIndexType(buffer.index_type);
index_buffer_ = std::move(buffer.index_buffer);
if (index_type != IndexType::kNone) {
index_type_ = ToMTLIndexType(index_type);
index_buffer_ = std::move(index_buffer);
}
return true;
}

View File

@@ -10,9 +10,11 @@
#include "fml/status.h"
#include "impeller/base/validation.h"
#include "impeller/core/buffer_view.h"
#include "impeller/core/device_buffer.h"
#include "impeller/core/formats.h"
#include "impeller/core/texture.h"
#include "impeller/core/vertex_buffer.h"
#include "impeller/renderer/backend/vulkan/barrier_vk.h"
#include "impeller/renderer/backend/vulkan/command_buffer_vk.h"
#include "impeller/renderer/backend/vulkan/context_vk.h"
@@ -23,6 +25,7 @@
#include "impeller/renderer/backend/vulkan/sampler_vk.h"
#include "impeller/renderer/backend/vulkan/shared_object_vk.h"
#include "impeller/renderer/backend/vulkan/texture_vk.h"
#include "vulkan/vulkan.hpp"
#include "vulkan/vulkan_handles.hpp"
namespace impeller {
@@ -368,29 +371,41 @@ void RenderPassVK::SetInstanceCount(size_t count) {
}
// |RenderPass|
bool RenderPassVK::SetVertexBuffer(VertexBuffer buffer) {
vertex_count_ = buffer.vertex_count;
if (buffer.index_type == IndexType::kUnknown || !buffer.vertex_buffer) {
bool RenderPassVK::SetVertexBuffer(BufferView vertex_buffers[],
size_t vertex_buffer_count,
size_t vertex_count) {
if (!ValidateVertexBuffers(vertex_buffers, vertex_buffer_count)) {
return false;
}
if (!command_buffer_->Track(buffer.vertex_buffer.buffer)) {
return false;
vk::Buffer buffers[kMaxVertexBuffers];
vk::DeviceSize vertex_buffer_offsets[kMaxVertexBuffers];
for (size_t i = 0; i < vertex_buffer_count; i++) {
buffers[i] = DeviceBufferVK::Cast(*vertex_buffers[i].buffer).GetBuffer();
vertex_buffer_offsets[i] = vertex_buffers[i].range.offset;
command_buffer_->Track(vertex_buffers[i].buffer);
}
// Bind the vertex buffer.
vk::Buffer vertex_buffer_handle =
DeviceBufferVK::Cast(*buffer.vertex_buffer.buffer).GetBuffer();
vk::Buffer vertex_buffers[] = {vertex_buffer_handle};
vk::DeviceSize vertex_buffer_offsets[] = {buffer.vertex_buffer.range.offset};
command_buffer_vk_.bindVertexBuffers(0u, 1u, vertex_buffers,
// Bind the vertex buffers.
command_buffer_vk_.bindVertexBuffers(0u, vertex_buffer_count, buffers,
vertex_buffer_offsets);
// Bind the index buffer.
if (buffer.index_type != IndexType::kNone) {
vertex_count_ = vertex_count;
return true;
}
// |RenderPass|
bool RenderPassVK::SetIndexBuffer(BufferView index_buffer,
IndexType index_type) {
if (!ValidateIndexBuffer(index_buffer, index_type)) {
return false;
}
if (index_type != IndexType::kNone) {
has_index_buffer_ = true;
const BufferView& index_buffer_view = buffer.index_buffer;
const BufferView& index_buffer_view = index_buffer;
if (!index_buffer_view) {
return false;
}
@@ -411,10 +426,11 @@ bool RenderPassVK::SetVertexBuffer(VertexBuffer buffer) {
DeviceBufferVK::Cast(*index_buffer).GetBuffer();
command_buffer_vk_.bindIndexBuffer(index_buffer_handle,
index_buffer_view.range.offset,
ToVKIndexType(buffer.index_type));
ToVKIndexType(index_type));
} else {
has_index_buffer_ = false;
}
return true;
}

View File

@@ -80,7 +80,12 @@ class RenderPassVK final : public RenderPass {
void SetInstanceCount(size_t count) override;
// |RenderPass|
bool SetVertexBuffer(VertexBuffer buffer) override;
bool SetVertexBuffer(BufferView vertex_buffers[],
size_t vertex_buffer_count,
size_t vertex_count) override;
// |RenderPass|
bool SetIndexBuffer(BufferView index_buffer, IndexType index_type) override;
// |RenderPass|
fml::Status Draw() override;

View File

@@ -12,13 +12,17 @@
namespace impeller {
bool Command::BindVertices(VertexBuffer buffer) {
bool Command::BindVertices(const VertexBuffer& buffer) {
if (buffer.index_type == IndexType::kUnknown) {
VALIDATION_LOG << "Cannot bind vertex buffer with an unknown index type.";
return false;
}
vertex_buffer = std::move(buffer);
vertex_buffers = {buffer.vertex_buffer};
vertex_buffer_count = 1u;
vertex_count = buffer.vertex_count;
index_buffer = buffer.index_buffer;
index_type = buffer.index_type;
return true;
}

View File

@@ -147,8 +147,28 @@ struct Command : public ResourceBinder {
size_t instance_count = 1u;
//----------------------------------------------------------------------------
/// The bound per-vertex data and optional index buffer.
VertexBuffer vertex_buffer;
/// The vertex buffers used by the vertex shader stage.
std::array<BufferView, kMaxVertexBuffers> vertex_buffers;
//----------------------------------------------------------------------------
/// The number of vertex buffers in the vertex_buffers array. Must not exceed
/// kMaxVertexBuffers.
size_t vertex_buffer_count = 0u;
//----------------------------------------------------------------------------
/// The index buffer binding used by the vertex shader stage.
BufferView index_buffer;
//----------------------------------------------------------------------------
/// 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;
//----------------------------------------------------------------------------
/// The type of indices in the index buffer. The indices must be tightly
/// packed in the index buffer.
///
IndexType index_type = IndexType::kUnknown;
//----------------------------------------------------------------------------
/// @brief Specify the vertex and index buffer to use for this command.
@@ -158,7 +178,7 @@ struct Command : public ResourceBinder {
///
/// @return returns if the binding was updated.
///
bool BindVertices(VertexBuffer buffer);
bool BindVertices(const VertexBuffer& buffer);
// |ResourceBinder|
bool BindResource(ShaderStage stage,

View File

@@ -4,6 +4,8 @@
#include "impeller/renderer/render_pass.h"
#include "fml/status.h"
#include "impeller/base/validation.h"
#include "impeller/core/vertex_buffer.h"
namespace impeller {
@@ -70,8 +72,7 @@ bool RenderPass::AddCommand(Command&& command) {
}
}
if (command.vertex_buffer.vertex_count == 0u ||
command.instance_count == 0u) {
if (command.vertex_count == 0u || command.instance_count == 0u) {
// Essentially a no-op. Don't record the command but this is not necessary
// an error either.
return true;
@@ -121,7 +122,84 @@ void RenderPass::SetInstanceCount(size_t count) {
}
bool RenderPass::SetVertexBuffer(VertexBuffer buffer) {
return pending_.BindVertices(std::move(buffer));
if (!SetVertexBuffer(&buffer.vertex_buffer, 1u, buffer.vertex_count)) {
return false;
}
if (!SetIndexBuffer(buffer.index_buffer, buffer.index_type)) {
return false;
}
return true;
}
bool RenderPass::SetVertexBuffer(BufferView vertex_buffer,
size_t vertex_count) {
return SetVertexBuffer(&vertex_buffer, 1, vertex_count);
}
bool RenderPass::SetVertexBuffer(std::vector<BufferView> vertex_buffers,
size_t vertex_count) {
return SetVertexBuffer(vertex_buffers.data(), vertex_buffers.size(),
vertex_count);
}
bool RenderPass::SetVertexBuffer(BufferView vertex_buffers[],
size_t vertex_buffer_count,
size_t vertex_count) {
if (!ValidateVertexBuffers(vertex_buffers, vertex_buffer_count)) {
return false;
}
pending_.vertex_count = vertex_count;
pending_.vertex_buffer_count = vertex_buffer_count;
for (size_t i = 0; i < vertex_buffer_count; i++) {
pending_.vertex_buffers[i] = std::move(vertex_buffers[i]);
}
return true;
}
bool RenderPass::SetIndexBuffer(BufferView index_buffer, IndexType index_type) {
if (!ValidateIndexBuffer(index_buffer, index_type)) {
return false;
}
pending_.index_buffer = std::move(index_buffer);
pending_.index_type = index_type;
return true;
}
bool RenderPass::ValidateVertexBuffers(const BufferView vertex_buffers[],
size_t vertex_buffer_count) {
if (vertex_buffer_count > kMaxVertexBuffers) {
VALIDATION_LOG << "Attempted to bind " << vertex_buffer_count
<< " vertex buffers, but the maximum is "
<< kMaxVertexBuffers << ".";
return false;
}
for (size_t i = 0; i < vertex_buffer_count; i++) {
if (!vertex_buffers[i]) {
VALIDATION_LOG << "Attempted to bind an invalid vertex buffer.";
return false;
}
}
return true;
}
bool RenderPass::ValidateIndexBuffer(const BufferView& index_buffer,
IndexType index_type) {
if (index_type == IndexType::kUnknown) {
VALIDATION_LOG << "Cannot bind an index buffer with an unknown index type.";
return false;
}
if (index_type != IndexType::kNone && !index_buffer) {
VALIDATION_LOG << "Attempted to bind an invalid index buffer.";
return false;
}
return true;
}
fml::Status RenderPass::Draw() {

View File

@@ -5,6 +5,7 @@
#ifndef FLUTTER_IMPELLER_RENDERER_RENDER_PASS_H_
#define FLUTTER_IMPELLER_RENDERER_RENDER_PASS_H_
#include <cstddef>
#include <string>
#include "fml/status.h"
@@ -98,6 +99,8 @@ class RenderPass : public ResourceBinder {
virtual void SetInstanceCount(size_t count);
//----------------------------------------------------------------------------
/// @deprecated Use SetVertexBuffer(BufferView[], size_t, size_t) instead.
///
/// @brief Specify the vertex and index buffer to use for this command.
///
/// @param[in] buffer The vertex and index buffer definition. If possible,
@@ -107,6 +110,72 @@ class RenderPass : public ResourceBinder {
///
virtual bool SetVertexBuffer(VertexBuffer buffer);
//----------------------------------------------------------------------------
/// @brief Specify a vertex buffer to use for this command.
///
/// @param[in] vertex_buffer The buffer view to use for sourcing vertices.
///
/// @param[in] vertex_count The number of vertices to draw.
///
/// @return Returns false if the given buffer view is invalid.
///
bool SetVertexBuffer(BufferView vertex_buffer, size_t vertex_count);
//----------------------------------------------------------------------------
/// @brief Specify a set of vertex buffers to use for this command.
///
/// @warning This method takes ownership of each buffer view in the vector.
/// Attempting to use the given buffer views after this call is
/// invalid.
///
/// @param[in] vertex_buffers The array of vertex buffer views to use.
/// The maximum number of vertex buffers is 16.
///
/// @param[in] vertex_count The number of vertices to draw.
///
/// @return Returns false if any of the given buffer views are invalid.
///
bool SetVertexBuffer(std::vector<BufferView> vertex_buffers,
size_t vertex_count);
//----------------------------------------------------------------------------
/// @brief Specify a set of vertex buffers to use for this command.
///
/// @warning This method takes ownership of each buffer view in the vector.
/// Attempting to use the given buffer views after this call is
/// invalid.
///
/// @param[in] vertex_buffers Pointer to an array of vertex buffers to
/// be copied. The maximum number of vertex
/// buffers is 16.
///
/// @param[in] vertex_buffer_count The number of vertex buffers to copy from
/// the array (max 16).
///
/// @param[in] vertex_count The number of vertices to draw.
///
/// @return Returns false if any of the given buffer views are invalid.
///
virtual bool SetVertexBuffer(BufferView vertex_buffers[],
size_t vertex_buffer_count,
size_t vertex_count);
//----------------------------------------------------------------------------
/// @brief Specify an index buffer to use for this command.
/// To unset the index buffer, pass IndexType::kNone to
/// index_type.
///
/// @param[in] index_buffer The buffer view to use for sourcing indices. The
/// total number of indices is inferred from the
/// buffer view size and index type.
///
/// @param[in] index_type The size of each index in the index buffer. Pass
/// IndexType::kNone to unset the index buffer.
///
/// @return Returns false if the index buffer view is invalid.
///
virtual bool SetIndexBuffer(BufferView index_buffer, IndexType index_type);
/// Record the currently pending command.
virtual fml::Status Draw();
@@ -194,6 +263,12 @@ class RenderPass : public ResourceBinder {
RenderPass(std::shared_ptr<const Context> context,
const RenderTarget& target);
static bool ValidateVertexBuffers(const BufferView vertex_buffers[],
size_t vertex_buffer_count);
static bool ValidateIndexBuffer(const BufferView& index_buffer,
IndexType index_type);
virtual void OnSetLabel(std::string label) = 0;
virtual bool OnEncodeCommands(const Context& context) const = 0;

View File

@@ -183,23 +183,45 @@ TEST_P(RendererTest, CanRenderPerspectiveCube) {
desc->SetCullMode(CullMode::kBackFace);
desc->SetWindingOrder(WindingOrder::kCounterClockwise);
desc->SetSampleCount(SampleCount::kCount4);
desc->SetStencilAttachmentDescriptors(std::nullopt);
desc->ClearStencilAttachments();
// Setup the vertex layout to take two bindings. The first for positions and
// the second for colors.
auto vertex_desc = std::make_shared<VertexDescriptor>();
ShaderStageIOSlot position_slot = VS::kInputPosition;
ShaderStageIOSlot color_slot = VS::kInputColor;
position_slot.binding = 0;
position_slot.offset = 0;
color_slot.binding = 1;
color_slot.offset = 0;
const std::vector<ShaderStageIOSlot> io_slots = {position_slot, color_slot};
const std::vector<ShaderStageBufferLayout> layouts = {
ShaderStageBufferLayout{.stride = 12u, .binding = 0},
ShaderStageBufferLayout{.stride = 16u, .binding = 1}};
vertex_desc->RegisterDescriptorSetLayouts(VS::kDescriptorSetLayouts);
vertex_desc->RegisterDescriptorSetLayouts(FS::kDescriptorSetLayouts);
vertex_desc->SetStageInputs(io_slots, layouts);
desc->SetVertexDescriptor(std::move(vertex_desc));
auto pipeline =
context->GetPipelineLibrary()->GetPipeline(std::move(desc)).Get();
ASSERT_TRUE(pipeline);
struct Cube {
VS::PerVertexData vertices[8] = {
Vector3 positions[8] = {
// -Z
{{-1, -1, -1}, Color::Red()},
{{1, -1, -1}, Color::Yellow()},
{{1, 1, -1}, Color::Green()},
{{-1, 1, -1}, Color::Blue()},
{-1, -1, -1},
{1, -1, -1},
{1, 1, -1},
{-1, 1, -1},
// +Z
{{-1, -1, 1}, Color::Green()},
{{1, -1, 1}, Color::Blue()},
{{1, 1, 1}, Color::Red()},
{{-1, 1, 1}, Color::Yellow()},
{-1, -1, 1},
{1, -1, 1},
{1, 1, 1},
{-1, 1, 1},
};
Color colors[8] = {
Color::Red(), Color::Yellow(), Color::Green(), Color::Blue(),
Color::Green(), Color::Blue(), Color::Red(), Color::Yellow(),
};
uint16_t indices[36] = {
1, 5, 2, 2, 5, 6, // +X
@@ -211,19 +233,8 @@ TEST_P(RendererTest, CanRenderPerspectiveCube) {
};
} cube;
VertexBuffer vertex_buffer;
{
auto device_buffer = context->GetResourceAllocator()->CreateBufferWithCopy(
reinterpret_cast<uint8_t*>(&cube), sizeof(cube));
vertex_buffer.vertex_buffer = {
.buffer = device_buffer,
.range = Range(offsetof(Cube, vertices), sizeof(Cube::vertices))};
vertex_buffer.index_buffer = {
.buffer = device_buffer,
.range = Range(offsetof(Cube, indices), sizeof(Cube::indices))};
vertex_buffer.vertex_count = 36;
vertex_buffer.index_type = IndexType::k16bit;
}
auto device_buffer = context->GetResourceAllocator()->CreateBufferWithCopy(
reinterpret_cast<uint8_t*>(&cube), sizeof(cube));
const std::unique_ptr<const Sampler>& sampler =
context->GetSamplerLibrary()->GetSampler({});
@@ -242,7 +253,21 @@ TEST_P(RendererTest, CanRenderPerspectiveCube) {
pass.SetCommandLabel("Perspective Cube");
pass.SetPipeline(pipeline);
pass.SetVertexBuffer(vertex_buffer);
std::array<BufferView, 2> vertex_buffers = {
BufferView{
.buffer = device_buffer,
.range = Range(offsetof(Cube, positions), sizeof(Cube::positions))},
BufferView{
.buffer = device_buffer,
.range = Range(offsetof(Cube, colors), sizeof(Cube::colors))},
};
BufferView index_buffer = {
.buffer = device_buffer,
.range = Range(offsetof(Cube, indices), sizeof(Cube::indices))};
pass.SetVertexBuffer(vertex_buffers.data(), vertex_buffers.size(), 36);
pass.SetIndexBuffer(index_buffer, IndexType::k16bit);
VS::UniformBuffer uniforms;
Scalar time = GetSecondsElapsed();

View File

@@ -24,11 +24,10 @@ template <class VertexType, size_t size>
VertexBuffer CreateVertexBuffer(std::array<VertexType, size> input,
HostBuffer& host_buffer) {
return VertexBuffer{
.vertex_buffer =
host_buffer.Emplace(input.data(), sizeof(VertexType) * size,
alignof(VertexType)), //
.vertex_count = size, //
.index_type = IndexType::kNone, //
.vertex_buffer = host_buffer.Emplace(
input.data(), sizeof(VertexType) * size, alignof(VertexType)), //
.vertex_count = size, //
.index_type = IndexType::kNone, //
};
}

View File

@@ -176,7 +176,6 @@ impeller::Command RenderPass::ProvisionRasterCommand() {
impeller::Command result = command_;
result.pipeline = GetOrCreatePipeline();
result.BindVertices(vertex_buffer_);
return result;
}
@@ -195,7 +194,7 @@ bool RenderPass::Draw() {
if (result.scissor.has_value()) {
render_pass_->SetScissor(result.scissor.value());
}
render_pass_->SetVertexBuffer(result.vertex_buffer);
render_pass_->SetVertexBuffer(GetVertexBuffer());
for (const auto& buffer : result.vertex_bindings.buffers) {
render_pass_->BindResource(impeller::ShaderStage::kVertex,
impeller::DescriptorType::kUniformBuffer,
@@ -319,6 +318,7 @@ static void BindVertexBuffer(flutter::gpu::RenderPass* wrapper,
.buffer = buffer,
.range = impeller::Range(offset_in_bytes, length_in_bytes),
};
// If the index type is set, then the `vertex_count` becomes the index
// count... So don't overwrite the count if it's already been set when binding
// the index buffer.
@@ -502,7 +502,6 @@ bool InternalFlutterGpu_RenderPass_BindTexture(
void InternalFlutterGpu_RenderPass_ClearBindings(
flutter::gpu::RenderPass* wrapper) {
auto& command = wrapper->GetCommand();
command.vertex_buffer = {};
command.vertex_bindings = {};
command.fragment_bindings = {};
}