Fix uniform alignment on a per platform basis.

This commit is contained in:
Chinmay Garde 2021-06-21 13:25:08 -07:00 committed by Dan Field
parent fe94b51880
commit b36aba2dbb
7 changed files with 63 additions and 4 deletions

View File

@ -34,6 +34,8 @@ impeller_component("compositor") {
"pipeline_descriptor.mm",
"pipeline_library.h",
"pipeline_library.mm",
"platform.h",
"platform.mm",
"range.cc",
"range.h",
"render_pass.h",

View File

@ -12,6 +12,7 @@
#include "flutter/fml/macros.h"
#include "impeller/compositor/buffer.h"
#include "impeller/compositor/buffer_view.h"
#include "impeller/compositor/platform.h"
namespace impeller {
@ -29,6 +30,12 @@ class HostBuffer final : public std::enable_shared_from_this<HostBuffer>,
size_t GetReservedLength() const;
template <class T, class = std::enable_if_t<std::is_standard_layout_v<T>>>
[[nodiscard]] BufferView EmplaceUniform(const T& t) {
return Emplace(reinterpret_cast<const void*>(&t), sizeof(T),
std::max(alignof(T), DefaultUniformAlignment()));
}
template <class T, class = std::enable_if_t<std::is_standard_layout_v<T>>>
[[nodiscard]] BufferView Emplace(const T& t) {
return Emplace(reinterpret_cast<const void*>(&t), sizeof(T), alignof(T));

View File

@ -0,0 +1,24 @@
// 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 <cstddef>
#include "flutter/fml/build_config.h"
#include "flutter/fml/macros.h"
namespace impeller {
constexpr size_t DefaultUniformAlignment() {
#if OS_IOS
return 16u;
#elif OS_MACOSX
return 256u;
#else
#error "Unsupported platform".
#endif
}
} // namespace impeller

View File

@ -0,0 +1,11 @@
// 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/platform.h"
namespace impeller {
//
} // namespace impeller

View File

@ -71,6 +71,8 @@ class RenderPass {
bool IsValid() const;
void SetLabel(std::string label);
HostBuffer& GetTransientsBuffer();
[[nodiscard]] bool RecordCommand(Command command);
@ -84,6 +86,7 @@ class RenderPass {
MTLRenderPassDescriptor* desc_ = nil;
std::vector<Command> commands_;
std::shared_ptr<HostBuffer> transients_buffer_;
std::string label_;
bool is_valid_ = false;
RenderPass(id<MTLCommandBuffer> buffer, const RenderPassDescriptor& desc);

View File

@ -137,14 +137,24 @@ bool RenderPass::IsValid() const {
return is_valid_;
}
void RenderPass::SetLabel(std::string label) {
label_ = std::move(label);
}
bool RenderPass::FinishEncoding(Allocator& transients_allocator) const {
if (!IsValid()) {
return false;
}
auto pass = [buffer_ renderCommandEncoderWithDescriptor:desc_];
if (!pass) {
return false;
}
if (!label_.empty()) {
[pass setLabel:@(label_.c_str())];
}
// Success or failure, the pass must end. The buffer can only process one pass
// at a time.
fml::ScopedCleanupClosure auto_end([pass]() { [pass endEncoding]; });

View File

@ -36,6 +36,8 @@ bool EntityRenderer::OnIsValid() const {
}
bool EntityRenderer::OnRender(RenderPass& pass) {
pass.SetLabel("EntityRenderer");
shader::BoxVertexInfo::UniformBuffer uniforms;
uniforms.mvp = Matrix::MakeOrthographic({800, 600});
VertexBufferBuilder vertices;
@ -47,14 +49,14 @@ bool EntityRenderer::OnRender(RenderPass& pass) {
});
Command cmd;
cmd.label = "Simple Box";
cmd.label = "Box";
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.vertex_bindings
.buffers[shader::BoxVertexInfo::kUniformUniformBuffer.location] =
pass.GetTransientsBuffer().EmplaceUniform(uniforms);
cmd.index_buffer = vertices.CreateIndexBuffer(pass.GetTransientsBuffer());
cmd.index_count = vertices.GetIndexCount();
if (!pass.RecordCommand(std::move(cmd))) {