Make the Metal drawable wrapping routine from the playground a utility.
This commit is contained in:
@@ -11,12 +11,12 @@
|
||||
#include "impeller/playground/playground.h"
|
||||
#include "impeller/renderer/allocator.h"
|
||||
#include "impeller/renderer/backend/metal/context_mtl.h"
|
||||
#include "impeller/renderer/backend/metal/surface_mtl.h"
|
||||
#include "impeller/renderer/backend/metal/texture_mtl.h"
|
||||
#include "impeller/renderer/context.h"
|
||||
#include "impeller/renderer/formats.h"
|
||||
#include "impeller/renderer/render_pass.h"
|
||||
#include "impeller/renderer/renderer.h"
|
||||
#include "impeller/renderer/surface.h"
|
||||
|
||||
#define GLFW_INCLUDE_NONE
|
||||
#import "third_party/glfw/include/GLFW/glfw3.h"
|
||||
@@ -138,81 +138,17 @@ bool Playground::OpenPlaygroundHere(Renderer::RenderCallback render_callback) {
|
||||
return true;
|
||||
}
|
||||
|
||||
auto current_drawable = [layer nextDrawable];
|
||||
|
||||
if (!current_drawable) {
|
||||
VALIDATION_LOG << "Could not acquire current drawable.";
|
||||
return false;
|
||||
}
|
||||
|
||||
TextureDescriptor msaa_tex_desc;
|
||||
msaa_tex_desc.type = TextureType::k2DMultisample;
|
||||
msaa_tex_desc.sample_count = SampleCount::kCount4;
|
||||
msaa_tex_desc.format = PixelFormat::kB8G8R8A8UNormInt;
|
||||
msaa_tex_desc.size = {
|
||||
static_cast<ISize::Type>(current_drawable.texture.width),
|
||||
static_cast<ISize::Type>(current_drawable.texture.height)};
|
||||
msaa_tex_desc.usage = static_cast<uint64_t>(TextureUsage::kRenderTarget);
|
||||
|
||||
auto msaa_tex =
|
||||
renderer_.GetContext()->GetPermanentsAllocator()->CreateTexture(
|
||||
StorageMode::kDeviceTransient, msaa_tex_desc);
|
||||
if (!msaa_tex) {
|
||||
FML_LOG(ERROR) << "Could not allocate MSAA resolve texture.";
|
||||
return false;
|
||||
}
|
||||
|
||||
msaa_tex->SetLabel("PlaygroundMainColor4xMSAA");
|
||||
|
||||
TextureDescriptor onscreen_tex_desc;
|
||||
onscreen_tex_desc.format = PixelFormat::kB8G8R8A8UNormInt;
|
||||
onscreen_tex_desc.size = msaa_tex_desc.size;
|
||||
onscreen_tex_desc.usage =
|
||||
static_cast<uint64_t>(TextureUsage::kRenderTarget);
|
||||
|
||||
ColorAttachment color0;
|
||||
color0.texture = msaa_tex;
|
||||
color0.clear_color = Color::DarkSlateGray();
|
||||
color0.load_action = LoadAction::kClear;
|
||||
color0.store_action = StoreAction::kMultisampleResolve;
|
||||
color0.resolve_texture = std::make_shared<TextureMTL>(
|
||||
onscreen_tex_desc, current_drawable.texture);
|
||||
|
||||
TextureDescriptor stencil0_tex;
|
||||
stencil0_tex.type = TextureType::k2DMultisample;
|
||||
stencil0_tex.sample_count = SampleCount::kCount4;
|
||||
stencil0_tex.format = PixelFormat::kD32FloatS8UNormInt;
|
||||
stencil0_tex.size = msaa_tex_desc.size;
|
||||
stencil0_tex.usage =
|
||||
static_cast<TextureUsageMask>(TextureUsage::kRenderTarget);
|
||||
auto stencil_texture =
|
||||
renderer_.GetContext()->GetPermanentsAllocator()->CreateTexture(
|
||||
StorageMode::kDeviceTransient, stencil0_tex);
|
||||
stencil_texture->SetLabel("PlaygroundMainStencil");
|
||||
|
||||
StencilAttachment stencil0;
|
||||
stencil0.texture = stencil_texture;
|
||||
stencil0.clear_stencil = 0;
|
||||
stencil0.load_action = LoadAction::kClear;
|
||||
stencil0.store_action = StoreAction::kDontCare;
|
||||
|
||||
RenderTarget desc;
|
||||
desc.SetColorAttachment(color0, 0u);
|
||||
desc.SetStencilAttachment(stencil0);
|
||||
|
||||
Surface surface(desc);
|
||||
|
||||
Renderer::RenderCallback wrapped_callback = [render_callback](auto& pass) {
|
||||
pass.SetLabel("Playground Main Render Pass");
|
||||
return render_callback(pass);
|
||||
};
|
||||
|
||||
if (!renderer_.Render(surface, wrapped_callback)) {
|
||||
if (!renderer_.Render(SurfaceMTL::WrapCurrentMetalLayerDrawable(
|
||||
renderer_.GetContext(), layer),
|
||||
wrapped_callback)) {
|
||||
VALIDATION_LOG << "Could not render into the surface.";
|
||||
return false;
|
||||
}
|
||||
|
||||
[current_drawable present];
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
@@ -31,6 +31,8 @@ impeller_component("renderer") {
|
||||
"backend/metal/shader_function_mtl.mm",
|
||||
"backend/metal/shader_library_mtl.h",
|
||||
"backend/metal/shader_library_mtl.mm",
|
||||
"backend/metal/surface_mtl.h",
|
||||
"backend/metal/surface_mtl.mm",
|
||||
"backend/metal/texture_mtl.h",
|
||||
"backend/metal/texture_mtl.mm",
|
||||
"backend/metal/vertex_descriptor_mtl.h",
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
// 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 <QuartzCore/CAMetalLayer.h>
|
||||
|
||||
#include "flutter/fml/macros.h"
|
||||
#include "impeller/renderer/context.h"
|
||||
#include "impeller/renderer/surface.h"
|
||||
|
||||
namespace impeller {
|
||||
|
||||
class SurfaceMTL final : public Surface {
|
||||
public:
|
||||
//----------------------------------------------------------------------------
|
||||
/// @brief Wraps the current drawable of the given Metal layer to create
|
||||
/// a surface Impeller can render to. The surface must be created
|
||||
/// as late as possible and discarded immediately after rendering
|
||||
/// to it.
|
||||
///
|
||||
/// @param[in] context The context
|
||||
/// @param[in] layer The layer whose current drawable to wrap to create a
|
||||
/// surface.
|
||||
///
|
||||
/// @return A pointer to the wrapped surface or null.
|
||||
///
|
||||
static std::unique_ptr<Surface> WrapCurrentMetalLayerDrawable(
|
||||
std::shared_ptr<Context> context,
|
||||
CAMetalLayer* layer);
|
||||
|
||||
// |Surface|
|
||||
~SurfaceMTL() override;
|
||||
|
||||
// |Surface|
|
||||
bool Present() const override;
|
||||
|
||||
private:
|
||||
id<MTLDrawable> drawable_ = nil;
|
||||
|
||||
SurfaceMTL(RenderTarget target, id<MTLDrawable> drawable);
|
||||
|
||||
FML_DISALLOW_COPY_AND_ASSIGN(SurfaceMTL);
|
||||
};
|
||||
|
||||
} // namespace impeller
|
||||
@@ -0,0 +1,103 @@
|
||||
// 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/renderer/backend/metal/surface_mtl.h"
|
||||
|
||||
#include "flutter/fml/trace_event.h"
|
||||
#include "impeller/base/validation.h"
|
||||
#include "impeller/renderer/backend/metal/texture_mtl.h"
|
||||
#include "impeller/renderer/render_target.h"
|
||||
|
||||
namespace impeller {
|
||||
|
||||
std::unique_ptr<Surface> SurfaceMTL::WrapCurrentMetalLayerDrawable(
|
||||
std::shared_ptr<Context> context,
|
||||
CAMetalLayer* layer) {
|
||||
TRACE_EVENT0("flutter", __FUNCTION__);
|
||||
|
||||
if (context == nullptr || !context->IsValid() || layer == nil) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto current_drawable = [layer nextDrawable];
|
||||
|
||||
if (!current_drawable) {
|
||||
VALIDATION_LOG << "Could not acquire current drawable.";
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
TextureDescriptor msaa_tex_desc;
|
||||
msaa_tex_desc.type = TextureType::k2DMultisample;
|
||||
msaa_tex_desc.sample_count = SampleCount::kCount4;
|
||||
msaa_tex_desc.format = PixelFormat::kB8G8R8A8UNormInt;
|
||||
msaa_tex_desc.size = {
|
||||
static_cast<ISize::Type>(current_drawable.texture.width),
|
||||
static_cast<ISize::Type>(current_drawable.texture.height)};
|
||||
msaa_tex_desc.usage = static_cast<uint64_t>(TextureUsage::kRenderTarget);
|
||||
|
||||
auto msaa_tex = context->GetPermanentsAllocator()->CreateTexture(
|
||||
StorageMode::kDeviceTransient, msaa_tex_desc);
|
||||
if (!msaa_tex) {
|
||||
FML_LOG(ERROR) << "Could not allocate MSAA resolve texture.";
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
msaa_tex->SetLabel("PlaygroundMainColor4xMSAA");
|
||||
|
||||
TextureDescriptor onscreen_tex_desc;
|
||||
onscreen_tex_desc.format = PixelFormat::kB8G8R8A8UNormInt;
|
||||
onscreen_tex_desc.size = msaa_tex_desc.size;
|
||||
onscreen_tex_desc.usage = static_cast<uint64_t>(TextureUsage::kRenderTarget);
|
||||
|
||||
ColorAttachment color0;
|
||||
color0.texture = msaa_tex;
|
||||
color0.clear_color = Color::DarkSlateGray();
|
||||
color0.load_action = LoadAction::kClear;
|
||||
color0.store_action = StoreAction::kMultisampleResolve;
|
||||
color0.resolve_texture =
|
||||
std::make_shared<TextureMTL>(onscreen_tex_desc, current_drawable.texture);
|
||||
|
||||
TextureDescriptor stencil0_tex;
|
||||
stencil0_tex.type = TextureType::k2DMultisample;
|
||||
stencil0_tex.sample_count = SampleCount::kCount4;
|
||||
stencil0_tex.format = PixelFormat::kD32FloatS8UNormInt;
|
||||
stencil0_tex.size = msaa_tex_desc.size;
|
||||
stencil0_tex.usage =
|
||||
static_cast<TextureUsageMask>(TextureUsage::kRenderTarget);
|
||||
auto stencil_texture = context->GetPermanentsAllocator()->CreateTexture(
|
||||
StorageMode::kDeviceTransient, stencil0_tex);
|
||||
stencil_texture->SetLabel("PlaygroundMainStencil");
|
||||
|
||||
StencilAttachment stencil0;
|
||||
stencil0.texture = stencil_texture;
|
||||
stencil0.clear_stencil = 0;
|
||||
stencil0.load_action = LoadAction::kClear;
|
||||
stencil0.store_action = StoreAction::kDontCare;
|
||||
|
||||
RenderTarget desc;
|
||||
desc.SetColorAttachment(color0, 0u);
|
||||
desc.SetStencilAttachment(stencil0);
|
||||
|
||||
// The constructor is private. So make_unique may not be used.
|
||||
return std::unique_ptr<SurfaceMTL>(
|
||||
new SurfaceMTL(std::move(desc), current_drawable));
|
||||
}
|
||||
|
||||
SurfaceMTL::SurfaceMTL(RenderTarget target, id<MTLDrawable> drawable)
|
||||
: Surface(std::move(target)), drawable_(drawable) {}
|
||||
|
||||
// |Surface|
|
||||
SurfaceMTL::~SurfaceMTL() = default;
|
||||
|
||||
// |Surface|
|
||||
bool SurfaceMTL::Present() const {
|
||||
if (drawable_ == nil) {
|
||||
return false;
|
||||
}
|
||||
|
||||
[drawable_ present];
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace impeller
|
||||
@@ -31,13 +31,13 @@ bool Renderer::IsValid() const {
|
||||
return is_valid_;
|
||||
}
|
||||
|
||||
bool Renderer::Render(const Surface& surface,
|
||||
bool Renderer::Render(std::unique_ptr<Surface> surface,
|
||||
RenderCallback render_callback) const {
|
||||
if (!IsValid()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!surface.IsValid()) {
|
||||
if (!surface || !surface->IsValid()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -49,8 +49,8 @@ bool Renderer::Render(const Surface& surface,
|
||||
|
||||
command_buffer->SetLabel("Onscreen Command Buffer");
|
||||
|
||||
auto render_pass =
|
||||
command_buffer->CreateRenderPass(surface.GetTargetRenderPassDescriptor());
|
||||
auto render_pass = command_buffer->CreateRenderPass(
|
||||
surface->GetTargetRenderPassDescriptor());
|
||||
if (!render_pass) {
|
||||
return false;
|
||||
}
|
||||
@@ -67,13 +67,17 @@ bool Renderer::Render(const Surface& surface,
|
||||
return false;
|
||||
}
|
||||
|
||||
return command_buffer->SubmitCommands(
|
||||
[sema = frames_in_flight_sema_](CommandBuffer::Status result) {
|
||||
sema->Signal();
|
||||
if (result != CommandBuffer::Status::kCompleted) {
|
||||
VALIDATION_LOG << "Could not commit command buffer.";
|
||||
}
|
||||
});
|
||||
if (!command_buffer->SubmitCommands(
|
||||
[sema = frames_in_flight_sema_](CommandBuffer::Status result) {
|
||||
sema->Signal();
|
||||
if (result != CommandBuffer::Status::kCompleted) {
|
||||
VALIDATION_LOG << "Could not commit command buffer.";
|
||||
}
|
||||
})) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return surface->Present();
|
||||
}
|
||||
|
||||
std::shared_ptr<Context> Renderer::GetContext() const {
|
||||
|
||||
@@ -32,7 +32,7 @@ class Renderer {
|
||||
|
||||
bool IsValid() const;
|
||||
|
||||
bool Render(const Surface& surface, RenderCallback callback) const;
|
||||
bool Render(std::unique_ptr<Surface> surface, RenderCallback callback) const;
|
||||
|
||||
std::shared_ptr<Context> GetContext() const;
|
||||
|
||||
|
||||
@@ -8,6 +8,8 @@
|
||||
|
||||
namespace impeller {
|
||||
|
||||
Surface::Surface() : Surface(RenderTarget{}) {}
|
||||
|
||||
Surface::Surface(RenderTarget target_desc) : desc_(std::move(target_desc)) {
|
||||
if (auto size = desc_.GetColorAttachmentSize(0u); size.has_value()) {
|
||||
size_ = size.value();
|
||||
@@ -32,4 +34,8 @@ const RenderTarget& Surface::GetTargetRenderPassDescriptor() const {
|
||||
return desc_;
|
||||
}
|
||||
|
||||
bool Surface::Present() const {
|
||||
return false;
|
||||
};
|
||||
|
||||
} // namespace impeller
|
||||
|
||||
@@ -16,9 +16,11 @@ namespace impeller {
|
||||
|
||||
class Surface {
|
||||
public:
|
||||
Surface();
|
||||
|
||||
Surface(RenderTarget target_desc);
|
||||
|
||||
~Surface();
|
||||
virtual ~Surface();
|
||||
|
||||
const ISize& GetSize() const;
|
||||
|
||||
@@ -26,6 +28,8 @@ class Surface {
|
||||
|
||||
const RenderTarget& GetTargetRenderPassDescriptor() const;
|
||||
|
||||
virtual bool Present() const;
|
||||
|
||||
private:
|
||||
RenderTarget desc_;
|
||||
ISize size_;
|
||||
|
||||
Reference in New Issue
Block a user