Work towards making canvas subpasses configure postprocessing effects.

This commit is contained in:
Chinmay Garde
2021-11-26 15:19:47 -08:00
committed by Dan Field
parent c694675197
commit 26e45c2c48
9 changed files with 90 additions and 58 deletions

View File

@@ -121,9 +121,9 @@ TEST_F(AiksTest, CanRenderGroupOpacity) {
canvas.SaveLayer(alpha);
canvas.DrawRect({100, 100, 100, 100}, red);
canvas.DrawRect({120, 120, 100, 100}, green);
canvas.DrawRect({140, 140, 100, 100}, blue);
canvas.DrawRect({000, 000, 100, 100}, red);
// canvas.DrawRect({020, 020, 100, 100}, green);
// canvas.DrawRect({040, 040, 100, 100}, blue);
canvas.Restore();

View File

@@ -22,7 +22,7 @@ void Canvas::Initialize() {
current_pass_ = base_pass_.get();
xformation_stack_.emplace_back(CanvasStackEntry{});
FML_DCHECK(GetSaveCount() == 1u);
FML_DCHECK(base_pass_->GetDepth() == 1u);
FML_DCHECK(base_pass_->GetSubpassesDepth() == 1u);
}
void Canvas::Reset() {
@@ -192,14 +192,15 @@ void Canvas::DrawRect(Rect rect, Paint paint) {
void Canvas::Save(bool create_subpass) {
auto entry = CanvasStackEntry{};
entry.xformation = xformation_stack_.back().xformation;
entry.stencil_depth = xformation_stack_.back().stencil_depth;
entry.is_subpass = create_subpass;
if (create_subpass) {
entry.is_subpass = true;
current_pass_ = GetCurrentPass().AddSubpass(std::make_unique<CanvasPass>());
current_pass_->SetTransformation(xformation_stack_.back().xformation);
current_pass_->SetStencilDepth(xformation_stack_.back().stencil_depth);
} else {
entry.xformation = xformation_stack_.back().xformation;
entry.stencil_depth = xformation_stack_.back().stencil_depth;
}
xformation_stack_.emplace_back(std::move(entry));
}

View File

@@ -28,10 +28,11 @@ void CanvasPass::SetEntities(Entities entities) {
entities_ = std::move(entities);
}
size_t CanvasPass::GetDepth() const {
size_t CanvasPass::GetSubpassesDepth() const {
size_t max_subpass_depth = 0u;
for (const auto& subpass : subpasses_) {
max_subpass_depth = std::max(max_subpass_depth, subpass->GetDepth());
max_subpass_depth =
std::max(max_subpass_depth, subpass->GetSubpassesDepth());
}
return max_subpass_depth + 1u;
}
@@ -84,6 +85,10 @@ bool CanvasPass::Render(ContentRenderer& renderer,
}
}
for (const auto& subpass : subpasses_) {
if (!subpass) {
return false;
}
const auto subpass_coverage = subpass->GetCoverageRect();
if (subpass_coverage.IsEmpty()) {
@@ -109,9 +114,7 @@ bool CanvasPass::Render(ContentRenderer& renderer,
return false;
}
if (!subpass) {
return false;
}
sub_renderpass->SetLabel("OffscreenPass");
if (!subpass->Render(renderer, *sub_renderpass)) {
return false;
@@ -132,6 +135,8 @@ bool CanvasPass::Render(ContentRenderer& renderer,
Entity entity;
entity.SetPath(PathBuilder{}.AddRect(subpass_coverage).CreatePath());
entity.SetContents(std::move(offscreen_texture_contents));
entity.SetStencilDepth(stencil_depth_);
entity.SetTransformation(xformation_);
if (!entity.Render(renderer, parent_pass)) {
return false;
}
@@ -165,4 +170,12 @@ std::unique_ptr<CanvasPass> CanvasPass::Clone() const {
return pass;
}
void CanvasPass::SetTransformation(Matrix xformation) {
xformation_ = std::move(xformation);
}
void CanvasPass::SetStencilDepth(size_t stencil_depth) {
stencil_depth_ = stencil_depth;
}
} // namespace impeller

View File

@@ -25,7 +25,7 @@ class CanvasPass {
~CanvasPass();
size_t GetDepth() const;
size_t GetSubpassesDepth() const;
std::unique_ptr<CanvasPass> Clone() const;
@@ -47,10 +47,16 @@ class CanvasPass {
void IterateAllEntities(std::function<bool(Entity&)> iterator);
void SetTransformation(Matrix xformation);
void SetStencilDepth(size_t stencil_depth);
private:
Entities entities_;
Subpasses subpasses_;
CanvasPass* superpass_ = nullptr;
Matrix xformation_;
size_t stencil_depth_ = 0u;
FML_DISALLOW_COPY_AND_ASSIGN(CanvasPass);
};

View File

@@ -154,7 +154,7 @@ bool Playground::OpenPlaygroundHere(Renderer::RenderCallback render_callback) {
ColorAttachment color0;
color0.texture =
std::make_shared<TextureMTL>(color0_tex, current_drawable.texture);
color0.clear_color = Color::SkyBlue();
color0.clear_color = Color::DarkSlateGray();
color0.load_action = LoadAction::kClear;
color0.store_action = StoreAction::kStore;

View File

@@ -32,6 +32,9 @@ class CommandBufferMTL final : public CommandBuffer {
// |CommandBuffer|
void SubmitCommands(CompletionCallback callback) override;
// |CommandBuffer|
void ReserveSpotInQueue() override;
// |CommandBuffer|
std::shared_ptr<RenderPass> CreateRenderPass(
RenderTarget target) const override;

View File

@@ -35,23 +35,28 @@ static CommandBuffer::Status ToCommitResult(MTLCommandBufferStatus status) {
}
void CommandBufferMTL::SubmitCommands(CompletionCallback callback) {
if (!callback) {
callback = [](auto) {};
}
if (!buffer_) {
// Already committed. This is caller error.
callback(Status::kError);
if (callback) {
callback(Status::kError);
}
return;
}
[buffer_ addCompletedHandler:^(id<MTLCommandBuffer> buffer) {
callback(ToCommitResult(buffer.status));
}];
if (callback) {
[buffer_ addCompletedHandler:^(id<MTLCommandBuffer> buffer) {
callback(ToCommitResult(buffer.status));
}];
}
[buffer_ commit];
buffer_ = nil;
}
void CommandBufferMTL::ReserveSpotInQueue() {
[buffer_ enqueue];
}
std::shared_ptr<RenderPass> CommandBufferMTL::CreateRenderPass(
RenderTarget target) const {
if (!buffer_) {

View File

@@ -127,21 +127,23 @@ bool RenderPassMTL::EncodeCommands(Allocator& transients_allocator) const {
if (!IsValid()) {
return false;
}
auto pass = [buffer_ renderCommandEncoderWithDescriptor:desc_];
auto render_command_encoder =
[buffer_ renderCommandEncoderWithDescriptor:desc_];
if (!pass) {
if (!render_command_encoder) {
return false;
}
if (!label_.empty()) {
[pass setLabel:@(label_.c_str())];
[render_command_encoder setLabel:@(label_.c_str())];
}
// Success or failure, the pass must end. The buffer can only process one pass
// at a time.
fml::ScopedCleanupClosure auto_end([pass]() { [pass endEncoding]; });
fml::ScopedCleanupClosure auto_end(
[render_command_encoder]() { [render_command_encoder endEncoding]; });
return EncodeCommands(transients_allocator, pass);
return EncodeCommands(transients_allocator, render_command_encoder);
}
//-----------------------------------------------------------------------------
@@ -154,7 +156,7 @@ bool RenderPassMTL::EncodeCommands(Allocator& transients_allocator) const {
/// absent.
///
struct PassBindingsCache {
PassBindingsCache(id<MTLRenderCommandEncoder> pass) : pass_(pass) {}
PassBindingsCache(id<MTLRenderCommandEncoder> encoder) : encoder_(encoder) {}
PassBindingsCache(const PassBindingsCache&) = delete;
@@ -165,7 +167,7 @@ struct PassBindingsCache {
return;
}
pipeline_ = pipeline;
[pass_ setRenderPipelineState:pipeline_];
[encoder_ setRenderPipelineState:pipeline_];
}
void SetDepthStencilState(id<MTLDepthStencilState> depth_stencil) {
@@ -173,7 +175,7 @@ struct PassBindingsCache {
return;
}
depth_stencil_ = depth_stencil;
[pass_ setDepthStencilState:depth_stencil_];
[encoder_ setDepthStencilState:depth_stencil_];
}
bool SetBuffer(ShaderStage stage,
@@ -194,10 +196,10 @@ struct PassBindingsCache {
switch (stage) {
case ShaderStage::kVertex:
[pass_ setVertexBufferOffset:offset atIndex:index];
[encoder_ setVertexBufferOffset:offset atIndex:index];
return true;
case ShaderStage::kFragment:
[pass_ setFragmentBufferOffset:offset atIndex:index];
[encoder_ setFragmentBufferOffset:offset atIndex:index];
return true;
default:
FML_DCHECK(false)
@@ -209,10 +211,10 @@ struct PassBindingsCache {
buffers_map[index] = {buffer, offset};
switch (stage) {
case ShaderStage::kVertex:
[pass_ setVertexBuffer:buffer offset:offset atIndex:index];
[encoder_ setVertexBuffer:buffer offset:offset atIndex:index];
return true;
case ShaderStage::kFragment:
[pass_ setFragmentBuffer:buffer offset:offset atIndex:index];
[encoder_ setFragmentBuffer:buffer offset:offset atIndex:index];
return true;
default:
FML_DCHECK(false) << "Cannot bind buffer to unknown shader stage.";
@@ -231,10 +233,10 @@ struct PassBindingsCache {
texture_map[index] = texture;
switch (stage) {
case ShaderStage::kVertex:
[pass_ setVertexTexture:texture atIndex:index];
[encoder_ setVertexTexture:texture atIndex:index];
return true;
case ShaderStage::kFragment:
[pass_ setFragmentTexture:texture atIndex:index];
[encoder_ setFragmentTexture:texture atIndex:index];
return true;
default:
FML_DCHECK(false) << "Cannot bind buffer to unknown shader stage.";
@@ -255,10 +257,10 @@ struct PassBindingsCache {
sampler_map[index] = sampler;
switch (stage) {
case ShaderStage::kVertex:
[pass_ setVertexSamplerState:sampler atIndex:index];
[encoder_ setVertexSamplerState:sampler atIndex:index];
return true;
case ShaderStage::kFragment:
[pass_ setFragmentSamplerState:sampler atIndex:index];
[encoder_ setFragmentSamplerState:sampler atIndex:index];
return true;
default:
FML_DCHECK(false) << "Cannot bind buffer to unknown shader stage.";
@@ -276,7 +278,7 @@ struct PassBindingsCache {
using TextureMap = std::map<uint64_t, id<MTLTexture>>;
using SamplerMap = std::map<uint64_t, id<MTLSamplerState>>;
const id<MTLRenderCommandEncoder> pass_;
const id<MTLRenderCommandEncoder> encoder_;
id<MTLRenderPipelineState> pipeline_ = nullptr;
id<MTLDepthStencilState> depth_stencil_ = nullptr;
std::map<ShaderStage, BufferMap> buffers_;
@@ -332,8 +334,8 @@ static bool Bind(PassBindingsCache& pass,
}
bool RenderPassMTL::EncodeCommands(Allocator& allocator,
id<MTLRenderCommandEncoder> pass) const {
PassBindingsCache pass_bindings(pass);
id<MTLRenderCommandEncoder> encoder) const {
PassBindingsCache pass_bindings(encoder);
auto bind_stage_resources = [&allocator, &pass_bindings](
const Bindings& bindings,
ShaderStage stage) -> bool {
@@ -355,7 +357,7 @@ bool RenderPassMTL::EncodeCommands(Allocator& allocator,
return true;
};
fml::closure pop_debug_marker = [pass]() { [pass popDebugGroup]; };
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.";
@@ -364,7 +366,7 @@ bool RenderPassMTL::EncodeCommands(Allocator& allocator,
fml::ScopedCleanupClosure auto_pop_debug_marker(pop_debug_marker);
if (!command.label.empty()) {
[pass pushDebugGroup:@(command.label.c_str())];
[encoder pushDebugGroup:@(command.label.c_str())];
} else {
auto_pop_debug_marker.Release();
}
@@ -372,11 +374,11 @@ bool RenderPassMTL::EncodeCommands(Allocator& allocator,
PipelineMTL::Cast(*command.pipeline).GetMTLRenderPipelineState());
pass_bindings.SetDepthStencilState(
PipelineMTL::Cast(*command.pipeline).GetMTLDepthStencilState());
[pass setFrontFacingWinding:command.winding == WindingOrder::kClockwise
? MTLWindingClockwise
: MTLWindingCounterClockwise];
[pass setCullMode:MTLCullModeNone];
[pass setStencilReferenceValue:command.stencil_reference];
[encoder setFrontFacingWinding:command.winding == WindingOrder::kClockwise
? MTLWindingClockwise
: MTLWindingCounterClockwise];
[encoder setCullMode:MTLCullModeNone];
[encoder setStencilReferenceValue:command.stencil_reference];
if (!bind_stage_resources(command.vertex_bindings, ShaderStage::kVertex)) {
return false;
}
@@ -400,14 +402,14 @@ bool RenderPassMTL::EncodeCommands(Allocator& allocator,
FML_DCHECK(command.index_count * sizeof(uint32_t) ==
command.index_buffer.range.length);
// Returns void. All error checking must be done by this point.
[pass drawIndexedPrimitives:ToMTLPrimitiveType(command.primitive_type)
indexCount:command.index_count
indexType:MTLIndexTypeUInt32
indexBuffer:mtl_index_buffer
indexBufferOffset:command.index_buffer.range.offset
instanceCount:1u
baseVertex:0u
baseInstance:0u];
[encoder drawIndexedPrimitives:ToMTLPrimitiveType(command.primitive_type)
indexCount:command.index_count
indexType:MTLIndexTypeUInt32
indexBuffer:mtl_index_buffer
indexBufferOffset:command.index_buffer.range.offset
instanceCount:1u
baseVertex:0u
baseInstance:0u];
}
return true;
}

View File

@@ -53,6 +53,8 @@ class CommandBuffer {
void SubmitCommands();
virtual void ReserveSpotInQueue() = 0;
//----------------------------------------------------------------------------
/// @brief Create a render pass to record render commands into.
///