Wire up render pass creation.
This commit is contained in:
@@ -12,6 +12,8 @@ impeller_component("compositor") {
|
||||
"buffer.mm",
|
||||
"command.h",
|
||||
"command.mm",
|
||||
"command_buffer.h",
|
||||
"command_buffer.mm",
|
||||
"comparable.cc",
|
||||
"comparable.h",
|
||||
"context.h",
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
// 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/command_buffer.h"
|
||||
|
||||
namespace impeller {
|
||||
|
||||
Class::Class() = default;
|
||||
|
||||
Class::~Class() = default;
|
||||
|
||||
} // namespace impeller
|
||||
@@ -4,16 +4,24 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <Metal/Metal.h>
|
||||
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
|
||||
#include "flutter/fml/macros.h"
|
||||
#include "impeller/compositor/render_pass.h"
|
||||
|
||||
namespace impeller {
|
||||
|
||||
class Context;
|
||||
|
||||
class CommandBuffer {
|
||||
public:
|
||||
~CommandBuffer();
|
||||
|
||||
bool IsValid() const;
|
||||
|
||||
enum class CommitResult {
|
||||
kPending,
|
||||
kError,
|
||||
@@ -23,8 +31,16 @@ class CommandBuffer {
|
||||
using CommitCallback = std::function<void(CommitResult)>;
|
||||
void Commit(CommitCallback callback);
|
||||
|
||||
std::shared_ptr<RenderPass> CreateRenderPass(
|
||||
const RenderPassDescriptor& desc) const;
|
||||
|
||||
private:
|
||||
CommandBuffer();
|
||||
friend class Context;
|
||||
|
||||
id<MTLCommandBuffer> buffer_ = nullptr;
|
||||
bool is_valid_ = false;
|
||||
|
||||
CommandBuffer(id<MTLCommandQueue> queue);
|
||||
|
||||
FML_DISALLOW_COPY_AND_ASSIGN(CommandBuffer);
|
||||
};
|
||||
|
||||
62
engine/src/flutter/impeller/compositor/command_buffer.mm
Normal file
62
engine/src/flutter/impeller/compositor/command_buffer.mm
Normal file
@@ -0,0 +1,62 @@
|
||||
// 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/command_buffer.h"
|
||||
|
||||
namespace impeller {
|
||||
|
||||
CommandBuffer::CommandBuffer(id<MTLCommandQueue> queue)
|
||||
: buffer_([queue commandBuffer]) {}
|
||||
|
||||
CommandBuffer::~CommandBuffer() = default;
|
||||
|
||||
bool CommandBuffer::IsValid() const {
|
||||
return is_valid_;
|
||||
}
|
||||
|
||||
static CommandBuffer::CommitResult ToCommitResult(
|
||||
MTLCommandBufferStatus status) {
|
||||
switch (status) {
|
||||
case MTLCommandBufferStatusCompleted:
|
||||
return CommandBuffer::CommitResult::kCompleted;
|
||||
case MTLCommandBufferStatusEnqueued:
|
||||
return CommandBuffer::CommitResult::kPending;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return CommandBuffer::CommitResult::kError;
|
||||
}
|
||||
|
||||
void CommandBuffer::Commit(CommitCallback callback) {
|
||||
if (!callback) {
|
||||
callback = [](auto) {};
|
||||
}
|
||||
|
||||
if (!buffer_) {
|
||||
callback(CommitResult::kError);
|
||||
return;
|
||||
}
|
||||
|
||||
[buffer_ addCompletedHandler:^(id<MTLCommandBuffer> buffer) {
|
||||
callback(ToCommitResult(buffer.status));
|
||||
}];
|
||||
[buffer_ commit];
|
||||
buffer_ = nil;
|
||||
}
|
||||
|
||||
std::shared_ptr<RenderPass> CommandBuffer::CreateRenderPass(
|
||||
const RenderPassDescriptor& desc) const {
|
||||
if (!buffer_) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto pass = std::shared_ptr<RenderPass>(new RenderPass(buffer_, desc));
|
||||
if (!pass->IsValid()) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return pass;
|
||||
}
|
||||
|
||||
} // namespace impeller
|
||||
@@ -14,6 +14,7 @@
|
||||
namespace impeller {
|
||||
|
||||
class ShaderLibrary;
|
||||
class CommandBuffer;
|
||||
|
||||
class Context {
|
||||
public:
|
||||
@@ -23,14 +24,12 @@ class Context {
|
||||
|
||||
bool IsValid() const;
|
||||
|
||||
id<MTLCommandQueue> GetRenderQueue() const;
|
||||
|
||||
id<MTLCommandQueue> GetTransferQueue() const;
|
||||
|
||||
std::shared_ptr<ShaderLibrary> GetShaderLibrary() const;
|
||||
|
||||
std::shared_ptr<PipelineLibrary> GetPipelineLibrary() const;
|
||||
|
||||
std::shared_ptr<CommandBuffer> CreateRenderCommandBuffer() const;
|
||||
|
||||
private:
|
||||
id<MTLDevice> device_ = nullptr;
|
||||
id<MTLCommandQueue> render_queue_ = nullptr;
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
|
||||
#include "flutter/fml/logging.h"
|
||||
#include "flutter/fml/paths.h"
|
||||
#include "impeller/compositor/command_buffer.h"
|
||||
#include "impeller/compositor/shader_library.h"
|
||||
|
||||
namespace impeller {
|
||||
@@ -62,14 +63,6 @@ bool Context::IsValid() const {
|
||||
return is_valid_;
|
||||
}
|
||||
|
||||
id<MTLCommandQueue> Context::GetRenderQueue() const {
|
||||
return render_queue_;
|
||||
}
|
||||
|
||||
id<MTLCommandQueue> Context::GetTransferQueue() const {
|
||||
return transfer_queue_;
|
||||
}
|
||||
|
||||
std::shared_ptr<ShaderLibrary> Context::GetShaderLibrary() const {
|
||||
return shader_library_;
|
||||
}
|
||||
@@ -78,4 +71,17 @@ std::shared_ptr<PipelineLibrary> Context::GetPipelineLibrary() const {
|
||||
return pipeline_library_;
|
||||
}
|
||||
|
||||
std::shared_ptr<CommandBuffer> Context::CreateRenderCommandBuffer() const {
|
||||
if (!IsValid()) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto buffer =
|
||||
std::shared_ptr<CommandBuffer>(new CommandBuffer(render_queue_));
|
||||
if (!buffer->IsValid()) {
|
||||
return nullptr;
|
||||
}
|
||||
return buffer;
|
||||
}
|
||||
|
||||
} // namespace impeller
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
|
||||
#include "flutter/fml/macros.h"
|
||||
#include "impeller/compositor/formats.h"
|
||||
#include "impeller/geometry/color.h"
|
||||
|
||||
namespace impeller {
|
||||
|
||||
@@ -142,6 +143,33 @@ constexpr MTLStencilOperation ToMTLStencilOperation(StencilOperation op) {
|
||||
return MTLStencilOperationKeep;
|
||||
};
|
||||
|
||||
constexpr MTLLoadAction ToMTLLoadAction(LoadAction action) {
|
||||
switch (action) {
|
||||
case LoadAction::kDontCare:
|
||||
return MTLLoadActionDontCare;
|
||||
case LoadAction::kLoad:
|
||||
return MTLLoadActionLoad;
|
||||
case LoadAction::kClear:
|
||||
return MTLLoadActionClear;
|
||||
}
|
||||
|
||||
return MTLLoadActionDontCare;
|
||||
}
|
||||
|
||||
constexpr MTLStoreAction ToMTLStoreAction(StoreAction action) {
|
||||
switch (action) {
|
||||
case StoreAction::kDontCare:
|
||||
return MTLStoreActionDontCare;
|
||||
case StoreAction::kStore:
|
||||
return MTLStoreActionStore;
|
||||
}
|
||||
return MTLStoreActionDontCare;
|
||||
}
|
||||
|
||||
constexpr MTLClearColor ToMTLClearColor(const Color& color) {
|
||||
return MTLClearColorMake(color.red, color.green, color.blue, color.alpha);
|
||||
}
|
||||
|
||||
MTLRenderPipelineColorAttachmentDescriptor*
|
||||
ToMTLRenderPipelineColorAttachmentDescriptor(
|
||||
ColorAttachmentDescriptor descriptor);
|
||||
|
||||
@@ -4,6 +4,8 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <Metal/Metal.h>
|
||||
|
||||
#include <map>
|
||||
|
||||
#include "flutter/fml/macros.h"
|
||||
@@ -13,6 +15,8 @@
|
||||
|
||||
namespace impeller {
|
||||
|
||||
class CommandBuffer;
|
||||
|
||||
struct RenderPassAttachment {
|
||||
std::shared_ptr<Texture> texture;
|
||||
LoadAction load_action = LoadAction::kDontCare;
|
||||
@@ -48,20 +52,27 @@ class RenderPassDescriptor {
|
||||
RenderPassDescriptor& SetStencilAttachment(
|
||||
StencilRenderPassAttachment attachment);
|
||||
|
||||
MTLRenderPassDescriptor* ToMTLRenderPassDescriptor() const;
|
||||
|
||||
private:
|
||||
std::map<size_t, ColorRenderPassAttachment> color_;
|
||||
std::map<size_t, ColorRenderPassAttachment> colors_;
|
||||
std::optional<DepthRenderPassAttachment> depth_;
|
||||
std::optional<StencilRenderPassAttachment> stencil_;
|
||||
};
|
||||
|
||||
class RenderPass {
|
||||
public:
|
||||
RenderPass(RenderPassDescriptor desc);
|
||||
|
||||
~RenderPass();
|
||||
|
||||
bool IsValid() const;
|
||||
|
||||
private:
|
||||
RenderPassDescriptor desc_;
|
||||
friend class CommandBuffer;
|
||||
|
||||
id<MTLRenderCommandEncoder> pass_ = nil;
|
||||
bool is_valid_ = false;
|
||||
|
||||
RenderPass(id<MTLCommandBuffer> buffer, const RenderPassDescriptor& desc);
|
||||
|
||||
FML_DISALLOW_COPY_AND_ASSIGN(RenderPass);
|
||||
};
|
||||
|
||||
@@ -4,6 +4,9 @@
|
||||
|
||||
#include "impeller/compositor/render_pass.h"
|
||||
|
||||
#include "flutter/fml/logging.h"
|
||||
#include "impeller/compositor/formats_metal.h"
|
||||
|
||||
namespace impeller {
|
||||
|
||||
RenderPassDescriptor::RenderPassDescriptor() = default;
|
||||
@@ -14,7 +17,7 @@ RenderPassDescriptor& RenderPassDescriptor::SetColorAttachment(
|
||||
ColorRenderPassAttachment attachment,
|
||||
size_t index) {
|
||||
if (attachment) {
|
||||
color_[index] = attachment;
|
||||
colors_[index] = attachment;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
@@ -35,8 +38,93 @@ RenderPassDescriptor& RenderPassDescriptor::SetStencilAttachment(
|
||||
return *this;
|
||||
}
|
||||
|
||||
RenderPass::RenderPass(RenderPassDescriptor desc) : desc_(std::move(desc)) {}
|
||||
static bool ConfigureAttachment(const RenderPassAttachment& desc,
|
||||
MTLRenderPassAttachmentDescriptor* attachment) {
|
||||
if (!desc.texture) {
|
||||
return false;
|
||||
}
|
||||
|
||||
attachment.texture = desc.texture->GetMTLTexture();
|
||||
attachment.loadAction = ToMTLLoadAction(desc.load_action);
|
||||
attachment.storeAction = ToMTLStoreAction(desc.store_action);
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool ConfigureColorAttachment(
|
||||
const ColorRenderPassAttachment& desc,
|
||||
MTLRenderPassColorAttachmentDescriptor* attachment) {
|
||||
if (!ConfigureAttachment(desc, attachment)) {
|
||||
return false;
|
||||
}
|
||||
attachment.clearColor = ToMTLClearColor(desc.clear_color);
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool ConfigureDepthAttachment(
|
||||
const DepthRenderPassAttachment& desc,
|
||||
MTLRenderPassDepthAttachmentDescriptor* attachment) {
|
||||
if (!ConfigureAttachment(desc, attachment)) {
|
||||
return false;
|
||||
}
|
||||
attachment.clearDepth = desc.clear_depth;
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool ConfigureStencilAttachment(
|
||||
const StencilRenderPassAttachment& desc,
|
||||
MTLRenderPassStencilAttachmentDescriptor* attachment) {
|
||||
if (!ConfigureAttachment(desc, attachment)) {
|
||||
return false;
|
||||
}
|
||||
attachment.clearStencil = desc.clear_stencil;
|
||||
return true;
|
||||
}
|
||||
|
||||
MTLRenderPassDescriptor* RenderPassDescriptor::ToMTLRenderPassDescriptor()
|
||||
const {
|
||||
auto result = [MTLRenderPassDescriptor renderPassDescriptor];
|
||||
|
||||
for (const auto& color : colors_) {
|
||||
if (!ConfigureColorAttachment(color.second,
|
||||
result.colorAttachments[color.first])) {
|
||||
FML_LOG(ERROR) << "Could not configure color attachment at index "
|
||||
<< color.first;
|
||||
return nil;
|
||||
}
|
||||
}
|
||||
|
||||
if (depth_.has_value() &&
|
||||
!ConfigureDepthAttachment(depth_.value(), result.depthAttachment)) {
|
||||
return nil;
|
||||
}
|
||||
|
||||
if (stencil_.has_value() &&
|
||||
!ConfigureStencilAttachment(stencil_.value(), result.stencilAttachment)) {
|
||||
return nil;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
RenderPass::RenderPass(id<MTLCommandBuffer> buffer,
|
||||
const RenderPassDescriptor& desc) {
|
||||
auto descriptor = desc.ToMTLRenderPassDescriptor();
|
||||
if (!descriptor) {
|
||||
return;
|
||||
}
|
||||
|
||||
pass_ = [buffer renderCommandEncoderWithDescriptor:descriptor];
|
||||
if (!pass_) {
|
||||
return;
|
||||
}
|
||||
|
||||
is_valid_ = true;
|
||||
}
|
||||
|
||||
RenderPass::~RenderPass() = default;
|
||||
|
||||
bool RenderPass::IsValid() const {
|
||||
return is_valid_;
|
||||
}
|
||||
|
||||
} // namespace impeller
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include "impeller/compositor/surface.h"
|
||||
|
||||
#include "flutter/fml/logging.h"
|
||||
#include "impeller/compositor/command_buffer.h"
|
||||
|
||||
namespace impeller {
|
||||
|
||||
@@ -31,7 +32,7 @@ bool Surface::Render() const {
|
||||
return false;
|
||||
}
|
||||
|
||||
auto command_buffer = [context_->GetRenderQueue() commandBuffer];
|
||||
auto command_buffer = context_->CreateRenderCommandBuffer();
|
||||
|
||||
if (!command_buffer) {
|
||||
return false;
|
||||
@@ -39,12 +40,10 @@ bool Surface::Render() const {
|
||||
|
||||
::dispatch_semaphore_wait(frames_in_flight_sema_, DISPATCH_TIME_FOREVER);
|
||||
|
||||
__block dispatch_semaphore_t block_sema = frames_in_flight_sema_;
|
||||
[command_buffer addCompletedHandler:^(id<MTLCommandBuffer> buffer) {
|
||||
::dispatch_semaphore_signal(block_sema);
|
||||
}];
|
||||
|
||||
[command_buffer commit];
|
||||
command_buffer->Commit(
|
||||
[sema = frames_in_flight_sema_](CommandBuffer::CommitResult) {
|
||||
::dispatch_semaphore_signal(sema);
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -4,6 +4,8 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <Metal/Metal.h>
|
||||
|
||||
#include "flutter/fml/macros.h"
|
||||
|
||||
namespace impeller {
|
||||
@@ -14,7 +16,11 @@ class Texture {
|
||||
|
||||
~Texture();
|
||||
|
||||
id<MTLTexture> GetMTLTexture() const;
|
||||
|
||||
private:
|
||||
id<MTLTexture> texture_;
|
||||
|
||||
FML_DISALLOW_COPY_AND_ASSIGN(Texture);
|
||||
};
|
||||
|
||||
|
||||
@@ -10,4 +10,8 @@ Texture::Texture() = default;
|
||||
|
||||
Texture::~Texture() = default;
|
||||
|
||||
id<MTLTexture> Texture::GetMTLTexture() const {
|
||||
return texture_;
|
||||
}
|
||||
|
||||
} // namespace impeller
|
||||
|
||||
Reference in New Issue
Block a user