Skip to content

Commit

Permalink
Fix alpha writes while rendering to texture.
Browse files Browse the repository at this point in the history
  • Loading branch information
chinmaygarde authored and dnfield committed Apr 27, 2022
1 parent 398b74f commit bb633ca
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 23 deletions.
6 changes: 3 additions & 3 deletions impeller/aiks/aiks_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ TEST_F(AiksTest, CanRenderImage) {
auto image = std::make_shared<Image>(CreateTextureForFixture("kalimba.jpg"));
paint.color = Color::Red();
canvas.DrawImage(image, Point::MakeXY(100.0, 100.0), paint);
// ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture()));
ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture()));
}

TEST_F(AiksTest, DISABLED_CanRenderImageRect) {
Expand Down Expand Up @@ -122,8 +122,8 @@ TEST_F(AiksTest, CanRenderGroupOpacity) {
canvas.SaveLayer(alpha);

canvas.DrawRect({000, 000, 100, 100}, red);
// canvas.DrawRect({020, 020, 100, 100}, green);
// canvas.DrawRect({040, 040, 100, 100}, blue);
canvas.DrawRect({020, 020, 100, 100}, green);
canvas.DrawRect({040, 040, 100, 100}, blue);

canvas.Restore();

Expand Down
6 changes: 5 additions & 1 deletion impeller/aiks/canvas_pass.cc
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ bool CanvasPass::Render(ContentRenderer& renderer,

auto sub_command_buffer = context->CreateRenderCommandBuffer();

sub_command_buffer->SetLabel("Offscreen Command Buffer");

if (!sub_command_buffer) {
return false;
}
Expand All @@ -124,7 +126,9 @@ bool CanvasPass::Render(ContentRenderer& renderer,
return false;
}

sub_command_buffer->SubmitCommands();
if (!sub_command_buffer->SubmitCommands()) {
return false;
}

auto offscreen_texture_contents = std::make_shared<TextureContents>();
offscreen_texture_contents->SetTexture(
Expand Down
4 changes: 2 additions & 2 deletions impeller/entity/contents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ bool LinearGradientContents::Render(const ContentRenderer& renderer,
cmd.label = "LinearGradientFill";
cmd.pipeline = renderer.GetGradientFillPipeline();
cmd.stencil_reference = entity.GetStencilDepth();
cmd.BindVertices(vertices_builder.CreateVertexBuffer(
*renderer.GetContext()->GetPermanentsAllocator()));
cmd.BindVertices(
vertices_builder.CreateVertexBuffer(pass.GetTransientsBuffer()));
cmd.primitive_type = PrimitiveType::kTriangle;
FS::BindGradientInfo(
cmd, pass.GetTransientsBuffer().EmplaceUniform(gradient_info));
Expand Down
3 changes: 2 additions & 1 deletion impeller/entity/shaders/texture_fill.frag
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ in vec2 v_texture_coords;
out vec4 frag_color;

void main() {
frag_color = texture(texture_sampler, v_texture_coords);
vec4 sampled = texture(texture_sampler, v_texture_coords);
frag_color = sampled;
}
7 changes: 6 additions & 1 deletion impeller/geometry/size.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,15 @@ struct TSize {
std::numeric_limits<Type>::max()};
}

constexpr TSize operator*(Type scale) const {
constexpr TSize operator*(Scalar scale) const {
return {width * scale, height * scale};
}

constexpr TSize operator/(Scalar scale) const {
return {static_cast<Scalar>(width) / scale,
static_cast<Scalar>(height) / scale};
}

constexpr bool operator==(const TSize& s) const {
return s.width == width && s.height == height;
}
Expand Down
5 changes: 4 additions & 1 deletion impeller/renderer/backend/metal/command_buffer_mtl.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,14 @@ class CommandBufferMTL final : public CommandBuffer {

CommandBufferMTL(id<MTLCommandQueue> queue);

// |CommandBuffer|
void SetLabel(const std::string& label) const override;

// |CommandBuffer|
bool IsValid() const override;

// |CommandBuffer|
void SubmitCommands(CompletionCallback callback) override;
bool SubmitCommands(CompletionCallback callback) override;

// |CommandBuffer|
void ReserveSpotInQueue() override;
Expand Down
13 changes: 11 additions & 2 deletions impeller/renderer/backend/metal/command_buffer_mtl.mm
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@
return is_valid_;
}

void CommandBufferMTL::SetLabel(const std::string& label) const {
if (label.empty()) {
return;
}

[buffer_ setLabel:@(label.data())];
}

static CommandBuffer::Status ToCommitResult(MTLCommandBufferStatus status) {
switch (status) {
case MTLCommandBufferStatusCompleted:
Expand All @@ -34,13 +42,13 @@
return CommandBufferMTL::Status::kError;
}

void CommandBufferMTL::SubmitCommands(CompletionCallback callback) {
bool CommandBufferMTL::SubmitCommands(CompletionCallback callback) {
if (!buffer_) {
// Already committed. This is caller error.
if (callback) {
callback(Status::kError);
}
return;
return false;
}

if (callback) {
Expand All @@ -51,6 +59,7 @@

[buffer_ commit];
buffer_ = nil;
return true;
}

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

CommandBuffer::~CommandBuffer() = default;

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

} // namespace impeller
6 changes: 4 additions & 2 deletions impeller/renderer/command_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ class CommandBuffer {

virtual bool IsValid() const = 0;

virtual void SetLabel(const std::string& label) const = 0;

//----------------------------------------------------------------------------
/// @brief Schedule the command encoded by render passes within this
/// command buffer on the GPU.
Expand All @@ -49,9 +51,9 @@ class CommandBuffer {
///
/// @param[in] callback The completion callback.
///
virtual void SubmitCommands(CompletionCallback callback) = 0;
[[nodiscard]] virtual bool SubmitCommands(CompletionCallback callback) = 0;

void SubmitCommands();
[[nodiscard]] bool SubmitCommands();

virtual void ReserveSpotInQueue() = 0;

Expand Down
6 changes: 3 additions & 3 deletions impeller/renderer/formats.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ enum class ColorWriteMask : uint64_t {
kGreen = 1 << 1,
kBlue = 1 << 2,
kAlpha = 1 << 3,
kAll = kRed | kGreen | kBlue,
kAll = kRed | kGreen | kBlue | kAlpha,
};

constexpr size_t BytesPerPixelForPixelFormat(PixelFormat format) {
Expand All @@ -163,7 +163,7 @@ constexpr size_t BytesPerPixelForPixelFormat(PixelFormat format) {
/// @brief Describe the color attachment that will be used with this
/// pipeline.
///
/// Blending at specific color attachments follows the pseudocode:
/// Blending at specific color attachments follows the pseudo-code:
/// ```
/// if (blending_enabled) {
/// final_color.rgb = (src_color_blend_factor * new_color.rgb)
Expand Down Expand Up @@ -248,7 +248,7 @@ enum class StencilOperation {
kDecrementClamp,
/// Perform a logical bitwise invert on the current stencil value.
kInvert,
/// Increment the current stencil value by 1. If at maxium, set to zero.
/// Increment the current stencil value by 1. If at maximum, set to zero.
kIncrementWrap,
/// Decrement the current stencil value by 1. If at zero, set to maximum.
kDecrementWrap,
Expand Down
5 changes: 3 additions & 2 deletions impeller/renderer/render_target.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "impeller/renderer/render_target.h"

#include "impeller/base/strings.h"
#include "impeller/renderer/allocator.h"
#include "impeller/renderer/context.h"
#include "impeller/renderer/texture.h"
Expand Down Expand Up @@ -110,7 +111,7 @@ RenderTarget RenderTarget::CreateOffscreen(const Context& context,
return {};
}

color0.texture->SetLabel(label);
color0.texture->SetLabel(SPrintF("%sColorTexture", label.c_str()));

StencilAttachment stencil0;
stencil0.load_action = LoadAction::kClear;
Expand All @@ -123,7 +124,7 @@ RenderTarget RenderTarget::CreateOffscreen(const Context& context,
return {};
}

stencil0.texture->SetLabel(label);
stencil0.texture->SetLabel(SPrintF("%sStencilTexture", label.c_str()));

RenderTarget target;
target.SetColorAttachment(std::move(color0), 0u);
Expand Down
6 changes: 3 additions & 3 deletions impeller/renderer/renderer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ bool Renderer::Render(const Surface& surface,
return false;
}

command_buffer->SetLabel("Onscreen Command Buffer");

auto render_pass =
command_buffer->CreateRenderPass(surface.GetTargetRenderPassDescriptor());
if (!render_pass) {
Expand All @@ -63,15 +65,13 @@ bool Renderer::Render(const Surface& surface,
return false;
}

command_buffer->SubmitCommands(
return command_buffer->SubmitCommands(
[sema = frames_in_flight_sema_](CommandBuffer::Status result) {
sema->Signal();
if (result != CommandBuffer::Status::kCompleted) {
FML_LOG(ERROR) << "Could not commit command buffer.";
}
});

return true;
}

std::shared_ptr<Context> Renderer::GetContext() const {
Expand Down

0 comments on commit bb633ca

Please sign in to comment.