Fix alpha writes while rendering to texture.

This commit is contained in:
Chinmay Garde
2021-11-27 16:01:33 -08:00
committed by Dan Field
parent 26e45c2c48
commit 8ac160af41
12 changed files with 48 additions and 23 deletions

View File

@@ -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) {
@@ -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();

View File

@@ -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;
}
@@ -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(

View File

@@ -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));

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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;

View File

@@ -22,6 +22,14 @@ bool CommandBufferMTL::IsValid() const {
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:
@@ -34,13 +42,13 @@ static CommandBuffer::Status ToCommitResult(MTLCommandBufferStatus status) {
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) {
@@ -51,6 +59,7 @@ void CommandBufferMTL::SubmitCommands(CompletionCallback callback) {
[buffer_ commit];
buffer_ = nil;
return true;
}
void CommandBufferMTL::ReserveSpotInQueue() {

View File

@@ -10,8 +10,8 @@ CommandBuffer::CommandBuffer() = default;
CommandBuffer::~CommandBuffer() = default;
void CommandBuffer::SubmitCommands() {
SubmitCommands(nullptr);
bool CommandBuffer::SubmitCommands() {
return SubmitCommands(nullptr);
}
} // namespace impeller

View File

@@ -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.
@@ -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;

View File

@@ -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) {
@@ -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)
@@ -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,

View File

@@ -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"
@@ -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;
@@ -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);

View File

@@ -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) {
@@ -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 {