Skip to content

Commit

Permalink
Turn on full-screen 4xMSAA by default and additional render target va…
Browse files Browse the repository at this point in the history
…lidations.
  • Loading branch information
chinmaygarde authored and dnfield committed Apr 27, 2022
1 parent ed8b78a commit 026dcd1
Show file tree
Hide file tree
Showing 18 changed files with 310 additions and 30 deletions.
11 changes: 11 additions & 0 deletions impeller/aiks/aiks_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,17 @@ TEST_F(AiksTest, CanRenderGroupOpacity) {

canvas.Restore();

// ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture()));
}

TEST_F(AiksTest, CanPerformFullScreenMSAA) {
Canvas canvas;

Paint red;
red.color = Color::Red();

canvas.DrawCircle({250, 250}, 125, red);

ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture()));
}

Expand Down
13 changes: 9 additions & 4 deletions impeller/aiks/canvas.cc
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,15 @@ void Canvas::DrawPath(Path path, Paint paint) {
GetCurrentPass().AddEntity(std::move(entity));
}

void Canvas::DrawRect(Rect rect, Paint paint) {
DrawPath(PathBuilder{}.AddRect(rect).CreatePath(), std::move(paint));
}

void Canvas::DrawCircle(Point center, Scalar radius, Paint paint) {
DrawPath(PathBuilder{}.AddCircle(center, radius).CreatePath(),
std::move(paint));
}

void Canvas::SaveLayer(Paint paint, std::optional<Rect> bounds) {
GetCurrentPass().SetDelegate(
std::make_unique<PaintPassDelegate>(std::move(paint)));
Expand Down Expand Up @@ -189,10 +198,6 @@ size_t Canvas::GetStencilDepth() const {
return xformation_stack_.back().stencil_depth;
}

void Canvas::DrawRect(Rect rect, Paint paint) {
DrawPath(PathBuilder{}.AddRect(rect).CreatePath(), std::move(paint));
}

void Canvas::Save(bool create_subpass) {
auto entry = CanvasStackEntry{};
if (create_subpass) {
Expand Down
2 changes: 2 additions & 0 deletions impeller/aiks/canvas.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ class Canvas {

void DrawRect(Rect rect, Paint paint);

void DrawCircle(Point center, Scalar radius, Paint paint);

void DrawImage(std::shared_ptr<Image> image, Point offset, Paint paint);

void DrawImageRect(std::shared_ptr<Image> image,
Expand Down
2 changes: 2 additions & 0 deletions impeller/base/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ impeller_component("base") {
"config.h",
"strings.cc",
"strings.h",
"validation.cc",
"validation.h",
]
}

Expand Down
1 change: 1 addition & 0 deletions impeller/base/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
#pragma once

#include "impeller/base/strings.h"
#include "impeller/base/validation.h"
28 changes: 28 additions & 0 deletions impeller/base/validation.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// 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 "flutter/impeller/base/validation.h"

#include "flutter/fml/logging.h"

namespace impeller {

ValidationLog::ValidationLog() = default;

ValidationLog::~ValidationLog() {
FML_LOG(ERROR) << stream_.str();
ImpellerValidationBreak();
}

std::ostream& ValidationLog::GetStream() {
return stream_;
}

void ImpellerValidationBreak() {
// Nothing to do. Exists for the debugger.
FML_LOG(ERROR) << "Break on " << __FUNCTION__
<< " to inspect point of failure.";
}

} // namespace impeller
31 changes: 31 additions & 0 deletions impeller/base/validation.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// 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 <sstream>

#include "flutter/fml/macros.h"

namespace impeller {

class ValidationLog {
public:
ValidationLog();

~ValidationLog();

std::ostream& GetStream();

private:
std::ostringstream stream_;

FML_DISALLOW_COPY_ASSIGN_AND_MOVE(ValidationLog);
};

void ImpellerValidationBreak();

} // namespace impeller

#define VALIDATION_LOG ::impeller::ValidationLog{}.GetStream()
37 changes: 29 additions & 8 deletions impeller/playground/playground.mm
Original file line number Diff line number Diff line change
Expand Up @@ -144,23 +144,44 @@ static void PlaygroundKeyCallback(GLFWwindow* window,
return false;
}

TextureDescriptor color0_tex;
color0_tex.format = PixelFormat::kB8G8R8A8UNormInt;
color0_tex.size = {
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)};
color0_tex.usage = static_cast<uint64_t>(TextureUsage::kRenderTarget);
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 =
std::make_shared<TextureMTL>(color0_tex, current_drawable.texture);
color0.texture = msaa_tex;
color0.clear_color = Color::DarkSlateGray();
color0.load_action = LoadAction::kClear;
color0.store_action = StoreAction::kStore;
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 = color0_tex.size;
stencil0_tex.size = msaa_tex_desc.size;
stencil0_tex.usage =
static_cast<TextureUsageMask>(TextureUsage::kRenderTarget);
auto stencil_texture =
Expand Down
6 changes: 6 additions & 0 deletions impeller/renderer/backend/metal/allocator_mtl.mm
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ static MTLStorageMode ToMTLStorageMode(StorageMode mode) {
}

auto mtl_texture_desc = ToMTLTextureDescriptor(desc);

if (!mtl_texture_desc) {
FML_DLOG(ERROR) << "Texture descriptor was invalid.";
return nullptr;
}

mtl_texture_desc.storageMode = ToMTLStorageMode(mode);
auto texture = [device_ newTextureWithDescriptor:mtl_texture_desc];
if (!texture) {
Expand Down
14 changes: 14 additions & 0 deletions impeller/renderer/backend/metal/formats_mtl.h
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@ constexpr MTLStoreAction ToMTLStoreAction(StoreAction action) {
return MTLStoreActionDontCare;
case StoreAction::kStore:
return MTLStoreActionStore;
case StoreAction::kMultisampleResolve:
return MTLStoreActionMultisampleResolve;
}
return MTLStoreActionDontCare;
}
Expand All @@ -216,6 +218,8 @@ constexpr StoreAction FromMTLStoreAction(MTLStoreAction action) {
return StoreAction::kDontCare;
case MTLStoreActionStore:
return StoreAction::kStore;
case MTLStoreActionMultisampleResolve:
return StoreAction::kMultisampleResolve;
default:
break;
}
Expand Down Expand Up @@ -249,6 +253,16 @@ constexpr MTLClearColor ToMTLClearColor(const Color& color) {
return MTLClearColorMake(color.red, color.green, color.blue, color.alpha);
}

constexpr MTLTextureType ToMTLTextureType(TextureType type) {
switch (type) {
case TextureType::k2D:
return MTLTextureType2D;
case TextureType::k2DMultisample:
return MTLTextureType2DMultisample;
}
return MTLTextureType2D;
}

MTLRenderPipelineColorAttachmentDescriptor*
ToMTLRenderPipelineColorAttachmentDescriptor(
ColorAttachmentDescriptor descriptor);
Expand Down
5 changes: 5 additions & 0 deletions impeller/renderer/backend/metal/formats_mtl.mm
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,13 @@
}

MTLTextureDescriptor* ToMTLTextureDescriptor(const TextureDescriptor& desc) {
if (!desc.IsValid()) {
return nil;
}
auto mtl_desc = [[MTLTextureDescriptor alloc] init];
mtl_desc.textureType = ToMTLTextureType(desc.type);
mtl_desc.pixelFormat = ToMTLPixelFormat(desc.format);
mtl_desc.sampleCount = static_cast<NSUInteger>(desc.sample_count);
mtl_desc.width = desc.size.width;
mtl_desc.height = desc.size.height;
mtl_desc.mipmapLevelCount = desc.mip_count;
Expand Down
2 changes: 2 additions & 0 deletions impeller/renderer/backend/metal/pipeline_library_mtl.mm
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
descriptor.stencilAttachmentPixelFormat =
ToMTLPixelFormat(desc.GetStencilPixelFormat());

descriptor.sampleCount = 4u;

return descriptor;
}

Expand Down
53 changes: 42 additions & 11 deletions impeller/renderer/backend/metal/render_pass_mtl.mm
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,33 @@

namespace impeller {

static bool ConfigureResolveTextureAttachment(
const Attachment& desc,
MTLRenderPassAttachmentDescriptor* attachment) {
if (desc.store_action == StoreAction::kMultisampleResolve &&
!desc.resolve_texture) {
VALIDATION_LOG << "Resolve store action specified on attachment but no "
"resolve texture was specified.";
return false;
}

if (desc.resolve_texture &&
desc.store_action != StoreAction::kMultisampleResolve) {
VALIDATION_LOG << "Resolve store action specified but there was no "
"resolve attachment.";
return false;
}

if (!desc.resolve_texture) {
return true;
}

attachment.resolveTexture =
TextureMTL::Cast(*desc.resolve_texture).GetMTLTexture();

return true;
}

static bool ConfigureAttachment(const Attachment& desc,
MTLRenderPassAttachmentDescriptor* attachment) {
if (!desc.texture) {
Expand All @@ -26,6 +53,11 @@ static bool ConfigureAttachment(const Attachment& desc,
attachment.texture = TextureMTL::Cast(*desc.texture).GetMTLTexture();
attachment.loadAction = ToMTLLoadAction(desc.load_action);
attachment.storeAction = ToMTLStoreAction(desc.store_action);

if (!ConfigureResolveTextureAttachment(desc, attachment)) {
return false;
}

return true;
}

Expand Down Expand Up @@ -69,8 +101,8 @@ static bool ConfigureStencilAttachment(
for (const auto& color : colors) {
if (!ConfigureColorAttachment(color.second,
result.colorAttachments[color.first])) {
FML_DLOG(ERROR) << "Could not configure color attachment at index "
<< color.first;
VALIDATION_LOG << "Could not configure color attachment at index "
<< color.first;
return nil;
}
}
Expand All @@ -79,15 +111,15 @@ static bool ConfigureStencilAttachment(

if (depth.has_value() &&
!ConfigureDepthAttachment(depth.value(), result.depthAttachment)) {
FML_DLOG(ERROR) << "Could not configure depth attachment.";
VALIDATION_LOG << "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.";
VALIDATION_LOG << "Could not configure stencil attachment.";
return nil;
}

Expand All @@ -99,7 +131,7 @@ static bool ConfigureStencilAttachment(
buffer_(buffer),
desc_(ToMTLRenderPassDescriptor(GetRenderTarget())),
transients_buffer_(HostBuffer::Create()) {
if (!buffer_ || !desc_) {
if (!buffer_ || !desc_ || !render_target_.IsValid()) {
return;
}
is_valid_ = true;
Expand Down Expand Up @@ -202,8 +234,7 @@ bool SetBuffer(ShaderStage stage,
[encoder_ setFragmentBufferOffset:offset atIndex:index];
return true;
default:
FML_DCHECK(false)
<< "Cannot update buffer offset of an unknown stage.";
VALIDATION_LOG << "Cannot update buffer offset of an unknown stage.";
return false;
}
return true;
Expand All @@ -217,7 +248,7 @@ bool SetBuffer(ShaderStage stage,
[encoder_ setFragmentBuffer:buffer offset:offset atIndex:index];
return true;
default:
FML_DCHECK(false) << "Cannot bind buffer to unknown shader stage.";
VALIDATION_LOG << "Cannot bind buffer to unknown shader stage.";
return false;
}
return false;
Expand All @@ -239,7 +270,7 @@ bool SetTexture(ShaderStage stage, uint64_t index, id<MTLTexture> texture) {
[encoder_ setFragmentTexture:texture atIndex:index];
return true;
default:
FML_DCHECK(false) << "Cannot bind buffer to unknown shader stage.";
VALIDATION_LOG << "Cannot bind buffer to unknown shader stage.";
return false;
}
return false;
Expand All @@ -263,7 +294,7 @@ bool SetSampler(ShaderStage stage,
[encoder_ setFragmentSamplerState:sampler atIndex:index];
return true;
default:
FML_DCHECK(false) << "Cannot bind buffer to unknown shader stage.";
VALIDATION_LOG << "Cannot bind buffer to unknown shader stage.";
return false;
}
return false;
Expand Down Expand Up @@ -360,7 +391,7 @@ static bool Bind(PassBindingsCache& pass,
fml::closure pop_debug_marker = [encoder]() { [encoder popDebugGroup]; };
for (const auto& command : commands_) {
if (command.index_count == 0u) {
FML_DLOG(ERROR) << "Zero index count in render pass command.";
VALIDATION_LOG << "Zero index count in render pass command.";
continue;
}

Expand Down
Loading

0 comments on commit 026dcd1

Please sign in to comment.