Skip to content

Commit

Permalink
Canvas subpasses rendering into their own command buffers.
Browse files Browse the repository at this point in the history
  • Loading branch information
chinmaygarde authored and dnfield committed Apr 27, 2022
1 parent ab16b0b commit 3a8fb57
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 4 deletions.
55 changes: 54 additions & 1 deletion impeller/aiks/canvas_pass.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
#include "impeller/aiks/canvas_pass.h"

#include "impeller/entity/content_renderer.h"
#include "impeller/geometry/path_builder.h"
#include "impeller/renderer/command_buffer.h"
#include "impeller/renderer/render_pass.h"
#include "impeller/renderer/render_target.h"

namespace impeller {

Expand Down Expand Up @@ -80,10 +84,59 @@ bool CanvasPass::Render(ContentRenderer& renderer,
}
}
for (const auto& subpass : subpasses_) {
if (!subpass->Render(renderer, parent_pass)) {
const auto subpass_coverage = subpass->GetCoverageRect();

if (subpass_coverage.IsEmpty()) {
// It is not an error to have an empty subpass. But subpasses that can't
// create their intermediates must trip errors.
continue;
}

auto context = renderer.GetContext();

auto subpass_target = RenderTarget::CreateOffscreen(
*context, ISize::Ceil(subpass_coverage.size));

auto sub_command_buffer = context->CreateRenderCommandBuffer();

if (!sub_command_buffer) {
return false;
}

auto sub_renderpass = sub_command_buffer->CreateRenderPass(subpass_target);

if (!sub_renderpass) {
return false;
}

if (!subpass) {
return false;
}

if (!subpass->Render(renderer, *sub_renderpass)) {
return false;
}

if (!sub_renderpass->EncodeCommands(*context->GetTransientsAllocator())) {
return false;
}

sub_command_buffer->SubmitCommands();

auto offscreen_texture_contents = std::make_shared<TextureContents>();
offscreen_texture_contents->SetTexture(
subpass_target.GetRenderTargetTexture());
offscreen_texture_contents->SetSourceRect(
IRect::MakeSize(subpass_target.GetRenderTargetTexture()->GetSize()));

Entity entity;
entity.SetPath(PathBuilder{}.AddRect(subpass_coverage).CreatePath());
entity.SetContents(std::move(offscreen_texture_contents));
if (!entity.Render(renderer, parent_pass)) {
return false;
}
}

return true;
}

Expand Down
6 changes: 6 additions & 0 deletions impeller/geometry/size.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ struct TSize {

constexpr bool IsEmpty() const { return !IsPositive(); }

template <class U>
static constexpr TSize Ceil(const TSize<U>& other) {
return TSize{static_cast<Type>(std::ceil(other.width)),
static_cast<Type>(std::ceil(other.height))};
}

constexpr size_t MipCount() const {
if (!IsPositive()) {
return 1u;
Expand Down
6 changes: 4 additions & 2 deletions impeller/renderer/backend/metal/render_pass_mtl.mm
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ static bool ConfigureStencilAttachment(
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;
FML_DLOG(ERROR) << "Could not configure color attachment at index "
<< color.first;
return nil;
}
}
Expand All @@ -79,13 +79,15 @@ static bool ConfigureStencilAttachment(

if (depth.has_value() &&
!ConfigureDepthAttachment(depth.value(), result.depthAttachment)) {
FML_DLOG(ERROR) << "Could not configure depth attachment.";
return nil;
}

const auto& stencil = desc.GetStencilAttachment();

if (stencil.has_value() &&
!ConfigureStencilAttachment(stencil.value(), result.stencilAttachment)) {
FML_DLOG(ERROR) << "Could not configure stencil attachment.";
return nil;
}

Expand Down
4 changes: 4 additions & 0 deletions impeller/renderer/command_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ CommandBuffer::CommandBuffer() = default;

CommandBuffer::~CommandBuffer() = default;

void CommandBuffer::SubmitCommands() {
SubmitCommands(nullptr);
}

} // namespace impeller
2 changes: 2 additions & 0 deletions impeller/renderer/command_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class CommandBuffer {
///
virtual void SubmitCommands(CompletionCallback callback) = 0;

void SubmitCommands();

//----------------------------------------------------------------------------
/// @brief Create a render pass to record render commands into.
///
Expand Down
15 changes: 14 additions & 1 deletion impeller/renderer/render_target.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ ISize RenderTarget::GetRenderTargetSize() const {
return size.has_value() ? size.value() : ISize{};
}

std::shared_ptr<Texture> RenderTarget::GetRenderTargetTexture() const {
auto found = colors_.find(0u);
if (found == colors_.end()) {
return nullptr;
}
return found->second.texture;
}

RenderTarget& RenderTarget::SetColorAttachment(ColorAttachment attachment,
size_t index) {
if (attachment) {
Expand Down Expand Up @@ -75,10 +83,15 @@ const std::optional<StencilAttachment>& RenderTarget::GetStencilAttachment()
RenderTarget RenderTarget::CreateOffscreen(const Context& context,
ISize size,
std::string label) {
if (size.IsEmpty()) {
return {};
}

TextureDescriptor color_tex0;
color_tex0.format = PixelFormat::kB8G8R8A8UNormInt;
color_tex0.size = size;
color_tex0.usage = static_cast<uint64_t>(TextureUsage::kRenderTarget);
color_tex0.usage = static_cast<uint64_t>(TextureUsage::kRenderTarget) |
static_cast<uint64_t>(TextureUsage::kShaderRead);

TextureDescriptor stencil_tex0;
stencil_tex0.format = PixelFormat::kD32FloatS8UNormInt;
Expand Down
2 changes: 2 additions & 0 deletions impeller/renderer/render_target.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class RenderTarget {

ISize GetRenderTargetSize() const;

std::shared_ptr<Texture> GetRenderTargetTexture() const;

std::optional<ISize> GetColorAttachmentSize(size_t index) const;

RenderTarget& SetColorAttachment(ColorAttachment attachment, size_t index);
Expand Down

0 comments on commit 3a8fb57

Please sign in to comment.