Cleanup buffer management wire up transients allocator.

This commit is contained in:
Chinmay Garde
2021-06-07 17:37:18 -07:00
committed by Dan Field
parent 88cc8570da
commit 695c92c36b
16 changed files with 256 additions and 91 deletions

View File

@@ -18,13 +18,18 @@ enum class StorageMode {
};
class Context;
class Buffer;
class DeviceBuffer;
class Allocator {
public:
~Allocator();
std::shared_ptr<Buffer> CreateBuffer(size_t length, std::string label);
bool IsValid() const;
std::shared_ptr<DeviceBuffer> CreateBuffer(StorageMode mode, size_t length);
std::shared_ptr<DeviceBuffer> CreateBufferWithCopy(const uint8_t* buffer,
size_t length);
private:
friend class Context;
@@ -33,10 +38,10 @@ class Allocator {
// MTLDevice APIs. But, in the future, this could be backed by named heaps
// with specific limits.
id<MTLDevice> device_;
StorageMode mode_;
std::string allocator_label_;
bool is_valid_ = false;
Allocator(id<MTLDevice> device, StorageMode type, std::string label);
Allocator(id<MTLDevice> device, std::string label);
FML_DISALLOW_COPY_AND_ASSIGN(Allocator);
};

View File

@@ -4,16 +4,27 @@
#include "impeller/compositor/allocator.h"
#include "flutter/fml/logging.h"
#include "impeller/compositor/buffer.h"
#include "impeller/compositor/device_buffer.h"
namespace impeller {
Allocator::Allocator(id<MTLDevice> device, StorageMode type, std::string label)
: device_(device), mode_(type), allocator_label_(std::move(label)) {}
Allocator::Allocator(id<MTLDevice> device, std::string label)
: device_(device), allocator_label_(std::move(label)) {
if (!device_) {
return;
}
is_valid_ = true;
}
Allocator::~Allocator() = default;
bool Allocator::IsValid() const {
return is_valid_;
}
static MTLResourceOptions ResourceOptionsFromStorageType(StorageMode type) {
switch (type) {
case StorageMode::kHostCoherent:
@@ -23,16 +34,33 @@ static MTLResourceOptions ResourceOptionsFromStorageType(StorageMode type) {
}
}
std::shared_ptr<Buffer> Allocator::CreateBuffer(size_t length,
std::string label) {
std::shared_ptr<DeviceBuffer> Allocator::CreateBufferWithCopy(
const uint8_t* buffer,
size_t length) {
auto new_buffer = CreateBuffer(StorageMode::kHostCoherent, length);
if (!new_buffer) {
return nullptr;
}
auto entire_range = Range{0, length};
if (!new_buffer->CopyHostBuffer(buffer, entire_range)) {
return nullptr;
}
return new_buffer;
}
std::shared_ptr<DeviceBuffer> Allocator::CreateBuffer(StorageMode mode,
size_t length) {
auto buffer =
[device_ newBufferWithLength:length
options:ResourceOptionsFromStorageType(mode_)];
options:ResourceOptionsFromStorageType(mode)];
if (!buffer) {
return nullptr;
}
buffer.label = @(label.c_str());
return std::shared_ptr<Buffer>(new Buffer(buffer, length, mode_, label));
return std::shared_ptr<DeviceBuffer>(new DeviceBuffer(buffer, length, mode));
}
} // namespace impeller

View File

@@ -6,44 +6,17 @@
#include <memory>
#include "flutter/fml/macros.h"
#include "impeller/compositor/allocator.h"
#include "impeller/compositor/range.h"
namespace impeller {
class HostBuffer;
class DeviceBuffer;
class Allocator;
class BufferBase {
class Buffer {
public:
~BufferBase() = default;
virtual ~Buffer();
virtual uint8_t* GetMapping() const = 0;
virtual size_t GetLength() const = 0;
};
class BufferView final : public BufferBase {
public:
// |BufferBase|
~BufferView() = default;
// |BufferBase|
uint8_t* GetMapping() const override {
return parent_->GetMapping() + range_in_parent_.offset;
}
// |BufferBase|
size_t GetLength() const override { return range_in_parent_.length; }
private:
friend HostBuffer;
std::shared_ptr<BufferBase> parent_;
Range range_in_parent_;
BufferView(std::shared_ptr<BufferBase> parent, Range range_in_parent)
: parent_(std::move(parent)), range_in_parent_(range_in_parent) {}
virtual std::shared_ptr<const DeviceBuffer> GetDeviceBuffer(
Allocator& allocator) const = 0;
};
} // namespace impeller

View File

@@ -6,6 +6,6 @@
namespace impeller {
//
Buffer::~Buffer() = default;
} // namespace impeller

View File

@@ -0,0 +1,20 @@
// 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 "flutter/fml/macros.h"
#include "impeller/compositor/buffer.h"
#include "impeller/compositor/range.h"
namespace impeller {
struct BufferView {
std::shared_ptr<Buffer> buffer;
Range range;
constexpr operator bool() const { return static_cast<bool>(buffer); }
};
} // 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/buffer_view.h"
namespace impeller {
//
} // namespace impeller

View File

@@ -8,7 +8,7 @@
#include <memory>
#include "flutter/fml/macros.h"
#include "impeller/compositor/buffer.h"
#include "impeller/compositor/buffer_view.h"
#include "impeller/compositor/pipeline.h"
namespace impeller {

View File

@@ -15,6 +15,7 @@ namespace impeller {
class ShaderLibrary;
class CommandBuffer;
class Allocator;
class Context {
public:
@@ -24,6 +25,8 @@ class Context {
bool IsValid() const;
std::shared_ptr<Allocator> GetTransientsAllocator() const;
std::shared_ptr<ShaderLibrary> GetShaderLibrary() const;
std::shared_ptr<PipelineLibrary> GetPipelineLibrary() const;
@@ -36,6 +39,7 @@ class Context {
id<MTLCommandQueue> transfer_queue_ = nullptr;
std::shared_ptr<ShaderLibrary> shader_library_;
std::shared_ptr<PipelineLibrary> pipeline_library_;
std::shared_ptr<Allocator> transients_allocator_;
bool is_valid_ = false;
FML_DISALLOW_COPY_AND_ASSIGN(Context);

View File

@@ -6,6 +6,7 @@
#include "flutter/fml/logging.h"
#include "flutter/fml/paths.h"
#include "impeller/compositor/allocator.h"
#include "impeller/compositor/command_buffer.h"
#include "impeller/compositor/shader_library.h"
@@ -54,6 +55,14 @@ Context::Context(std::string shaders_directory)
std::shared_ptr<PipelineLibrary>(new PipelineLibrary(device_));
}
{
transients_allocator_ = std::shared_ptr<Allocator>(
new Allocator(device_, "Impeller Transients Allocator"));
if (!transients_allocator_) {
return;
}
}
is_valid_ = true;
}
@@ -84,4 +93,8 @@ std::shared_ptr<CommandBuffer> Context::CreateRenderCommandBuffer() const {
return buffer;
}
std::shared_ptr<Allocator> Context::GetTransientsAllocator() const {
return transients_allocator_;
}
} // namespace impeller

View File

@@ -6,16 +6,26 @@
#include <Metal/Metal.h>
#include <memory>
#include <string>
#include "flutter/fml/macros.h"
#include "impeller/compositor/allocator.h"
#include "impeller/compositor/buffer.h"
#include "impeller/compositor/range.h"
namespace impeller {
class Buffer {
class DeviceBuffer final : public Buffer,
public std::enable_shared_from_this<DeviceBuffer> {
public:
~Buffer();
~DeviceBuffer();
[[nodiscard]] bool CopyHostBuffer(const uint8_t* source,
Range source_range,
size_t offset = 0u);
id<MTLBuffer> GetMTLBuffer() const;
private:
friend class Allocator;
@@ -23,14 +33,14 @@ class Buffer {
const id<MTLBuffer> buffer_;
const size_t size_;
const StorageMode mode_;
const std::string label_;
Buffer(id<MTLBuffer> buffer,
size_t size,
StorageMode mode,
std::string label);
DeviceBuffer(id<MTLBuffer> buffer, size_t size, StorageMode mode);
FML_DISALLOW_COPY_AND_ASSIGN(Buffer);
// |Buffer|
std::shared_ptr<const DeviceBuffer> GetDeviceBuffer(
Allocator& allocator) const override;
FML_DISALLOW_COPY_AND_ASSIGN(DeviceBuffer);
};
} // namespace impeller

View File

@@ -4,14 +4,45 @@
#include "impeller/compositor/device_buffer.h"
#include <Foundation/Foundation.h>
namespace impeller {
Buffer::Buffer(id<MTLBuffer> buffer,
size_t size,
StorageMode mode,
std::string label)
: buffer_(buffer), size_(size), mode_(mode), label_(std::move(label)) {}
DeviceBuffer::DeviceBuffer(id<MTLBuffer> buffer, size_t size, StorageMode mode)
: buffer_(buffer), size_(size), mode_(mode) {}
Buffer::~Buffer() = default;
DeviceBuffer::~DeviceBuffer() = default;
id<MTLBuffer> DeviceBuffer::GetMTLBuffer() const {
return buffer_;
}
[[nodiscard]] bool DeviceBuffer::CopyHostBuffer(const uint8_t* source,
Range source_range,
size_t offset) {
if (offset + source_range.length > size_) {
// Out of bounds of this buffer.
return false;
}
auto dest = static_cast<uint8_t*>(buffer_.contents);
if (!dest) {
// Probably StorageMode::kDevicePrivate.
return false;
}
::memcpy(dest + offset, source + source_range.offset, source_range.length);
[buffer_ didModifyRange:NSMakeRange(offset, source_range.length)];
return true;
}
// |Buffer|
std::shared_ptr<const DeviceBuffer> DeviceBuffer::GetDeviceBuffer(
Allocator& allocator) const {
return shared_from_this();
}
} // namespace impeller

View File

@@ -8,31 +8,33 @@
#include "flutter/fml/macros.h"
#include "impeller/compositor/buffer.h"
#include "impeller/compositor/host_buffer.h"
#include "impeller/compositor/buffer_view.h"
namespace impeller {
class HostBuffer : public std::enable_shared_from_this<HostBuffer>,
public BufferBase {
class HostBuffer final : public std::enable_shared_from_this<HostBuffer>,
public Buffer {
public:
std::shared_ptr<HostBuffer> Create();
std::shared_ptr<BufferView> Emplace(size_t length);
// |Buffer|
virtual ~HostBuffer();
// |BufferBase|
uint8_t* GetMapping() const override { return buffer_; }
static std::shared_ptr<HostBuffer> Create();
// |BufferBase|
size_t GetLength() const override { return length_; }
BufferView Emplace(const void* buffer, size_t length);
[[nodiscard]] bool Truncate(size_t length);
private:
mutable std::shared_ptr<DeviceBuffer> device_buffer_;
uint8_t* buffer_ = nullptr;
size_t length_ = 0;
size_t reserved_ = 0;
size_t generation_ = 1u;
mutable size_t device_buffer_generation_ = 0u;
// |Buffer|
std::shared_ptr<const DeviceBuffer> GetDeviceBuffer(
Allocator& allocator) const override;
[[nodiscard]] bool Reserve(size_t reserved);

View File

@@ -6,6 +6,9 @@
#include "flutter/fml/logging.h"
#include "impeller/compositor/allocator.h"
#include "impeller/compositor/buffer_view.h"
namespace impeller {
std::shared_ptr<HostBuffer> HostBuffer::Create() {
@@ -18,13 +21,15 @@ HostBuffer::~HostBuffer() {
::free(buffer_);
}
std::shared_ptr<BufferView> HostBuffer::Emplace(size_t length) {
BufferView HostBuffer::Emplace(const void* buffer, size_t length) {
auto old_length = length_;
if (!Truncate(length_ + length)) {
return nullptr;
return {};
}
return std::shared_ptr<BufferView>(
new BufferView(shared_from_this(), Range{old_length, length}));
FML_DCHECK(buffer_);
generation_++;
::memmove(buffer_, buffer, length);
return BufferView{shared_from_this(), Range{old_length, length}};
}
bool HostBuffer::Truncate(size_t length) {
@@ -72,4 +77,18 @@ bool HostBuffer::Reserve(size_t reserved) {
return true;
}
std::shared_ptr<const DeviceBuffer> HostBuffer::GetDeviceBuffer(
Allocator& allocator) const {
if (generation_ == device_buffer_generation_) {
return device_buffer_;
}
auto new_buffer = allocator.CreateBufferWithCopy(buffer_, length_);
if (!new_buffer) {
return nullptr;
}
device_buffer_generation_ = generation_;
device_buffer_ = std::move(new_buffer);
return device_buffer_;
}
} // namespace impeller

View File

@@ -72,7 +72,7 @@ class RenderPass {
bool Record(Command command);
bool Encode() const;
bool Encode(Allocator& transients_allocator) const;
private:
friend class CommandBuffer;
@@ -84,7 +84,8 @@ class RenderPass {
RenderPass(id<MTLCommandBuffer> buffer, const RenderPassDescriptor& desc);
bool EncodeCommands(id<MTLRenderCommandEncoder> pass) const;
bool EncodeCommands(Allocator& transients_allocator,
id<MTLRenderCommandEncoder> pass) const;
FML_DISALLOW_COPY_AND_ASSIGN(RenderPass);
};

View File

@@ -6,6 +6,7 @@
#include "flutter/fml/closure.h"
#include "flutter/fml/logging.h"
#include "impeller/compositor/device_buffer.h"
#include "impeller/compositor/formats_metal.h"
#include "impeller/shader_glue/shader_types.h"
@@ -130,7 +131,7 @@ bool RenderPass::IsValid() const {
return is_valid_;
}
bool RenderPass::Encode() const {
bool RenderPass::Encode(Allocator& transients_allocator) const {
if (!IsValid()) {
return false;
}
@@ -142,38 +143,85 @@ bool RenderPass::Encode() const {
// at a time.
fml::ScopedCleanupClosure auto_end([pass]() { [pass endEncoding]; });
return EncodeCommands(pass);
return EncodeCommands(transients_allocator, pass);
}
void Bind(ShaderStage stage, const BufferView& view) {}
static bool Bind(id<MTLRenderCommandEncoder> pass,
Allocator& allocator,
ShaderStage stage,
size_t bind_index,
const BufferView& view) {
if (!view.buffer) {
return false;
}
void Bind(ShaderStage stage, const Texture& view) {}
auto device_buffer = view.buffer->GetDeviceBuffer(allocator);
if (!device_buffer) {
return false;
}
void Bind(ShaderStage stage, const Sampler& view) {}
auto buffer = device_buffer->GetMTLBuffer();
// The Metal call is a void return and we don't want to make it on nil.
if (!buffer) {
return false;
}
bool RenderPass::EncodeCommands(id<MTLRenderCommandEncoder> pass) const {
[pass setVertexBuffer:buffer offset:view.range.offset atIndex:bind_index];
return true;
}
static bool Bind(Allocator& allocator,
ShaderStage stage,
size_t bind_index,
const Texture& view) {
FML_CHECK(false);
return false;
}
static bool Bind(Allocator& allocator,
ShaderStage stage,
size_t bind_index,
const Sampler& view) {
FML_CHECK(false);
return false;
}
bool RenderPass::EncodeCommands(Allocator& allocator,
id<MTLRenderCommandEncoder> pass) const {
// There a numerous opportunities here to ensure bindings are not repeated.
// Stuff like setting the vertex buffer bindings over and over when just the
// offsets could be updated (as recommended in best practices).
auto bind_stage_resources = [](const Bindings& bindings, ShaderStage stage) {
auto bind_stage_resources = [&allocator, pass](const Bindings& bindings,
ShaderStage stage) -> bool {
for (const auto buffer : bindings.buffers) {
Bind(stage, buffer.second);
if (!Bind(pass, allocator, stage, buffer.first, buffer.second)) {
return false;
}
}
for (const auto texture : bindings.textures) {
Bind(stage, *texture.second);
if (!Bind(allocator, stage, texture.first, *texture.second)) {
return false;
}
}
for (const auto sampler : bindings.samplers) {
Bind(stage, *sampler.second);
if (!Bind(allocator, stage, sampler.first, *sampler.second)) {
return false;
}
}
return true;
};
for (const auto& command : commands_) {
[pass setRenderPipelineState:command.pipeline->GetMTLRenderPipelineState()];
[pass setDepthStencilState:command.pipeline->GetMTLDepthStencilState()];
[pass setFrontFacingWinding:MTLWindingClockwise];
[pass setCullMode:MTLCullModeBack];
bind_stage_resources(command.vertex_bindings, ShaderStage::kVertex);
bind_stage_resources(command.fragment_bindings, ShaderStage::kFragment);
if (!bind_stage_resources(command.vertex_bindings, ShaderStage::kVertex)) {
return false;
}
if (!bind_stage_resources(command.fragment_bindings,
ShaderStage::kFragment)) {
return false;
}
// [pass drawIndexedPrimitives:MTLPrimitiveTypeTriangleStrip
// indexCount:command.index_count
// indexType:MTLIndexTypeUInt32

View File

@@ -48,7 +48,7 @@ bool Renderer::Render(const Surface& surface) {
if (!render_pass) {
return false;
}
render_pass->Encode();
render_pass->Encode(*context_->GetTransientsAllocator());
::dispatch_semaphore_wait(frames_in_flight_sema_, DISPATCH_TIME_FOREVER);