Move render pass descriptor to their own TU

This commit is contained in:
Chinmay Garde 2021-07-18 14:29:24 -07:00 committed by Dan Field
parent 4192cb340a
commit cd71fe879f
9 changed files with 148 additions and 112 deletions

View File

@ -42,6 +42,8 @@ impeller_component("compositor") {
"range.h",
"render_pass.h",
"render_pass.mm",
"render_pass_descriptor.h",
"render_pass_descriptor.mm",
"renderer.h",
"renderer.mm",
"sampler.h",

View File

@ -6,20 +6,24 @@
#include <cstdint>
#include <functional>
#include <memory>
#include <type_traits>
#include "flutter/fml/hash_combine.h"
#include "flutter/fml/macros.h"
#include "impeller/geometry/color.h"
namespace impeller {
class Texture;
enum class PixelFormat {
kUnknown,
kPixelFormat_R8G8B8A8_UNormInt,
kPixelFormat_R8G8B8A8_UNormInt_SRGB,
kPixelFormat_B8G8R8A8_UNormInt,
kPixelFormat_B8G8R8A8_UNormInt_SRGB,
kPixelFormat_D32_Float_S8_UNormInt,
kR8G8B8A8_UNormInt,
kR8G8B8A8_UNormInt_SRGB,
kB8G8R8A8_UNormInt,
kB8G8R8A8_UNormInt_SRGB,
kD32_Float_S8_UNormInt,
};
enum class BlendFactor {
@ -108,12 +112,12 @@ constexpr size_t BytesPerPixelForPixelFormat(PixelFormat format) {
switch (format) {
case PixelFormat::kUnknown:
return 0u;
case PixelFormat::kPixelFormat_R8G8B8A8_UNormInt:
case PixelFormat::kPixelFormat_R8G8B8A8_UNormInt_SRGB:
case PixelFormat::kPixelFormat_B8G8R8A8_UNormInt:
case PixelFormat::kPixelFormat_B8G8R8A8_UNormInt_SRGB:
case PixelFormat::kR8G8B8A8_UNormInt:
case PixelFormat::kR8G8B8A8_UNormInt_SRGB:
case PixelFormat::kB8G8R8A8_UNormInt:
case PixelFormat::kB8G8R8A8_UNormInt_SRGB:
return 4u;
case PixelFormat::kPixelFormat_D32_Float_S8_UNormInt:
case PixelFormat::kD32_Float_S8_UNormInt:
// This is an esoteric format and implementations may use 64 bits.
// Impeller doesn't work with these natively and this return is only here
// for completeness. The 40 bits is as documented.
@ -278,6 +282,26 @@ struct StencilAttachmentDescriptor {
}
};
struct RenderPassAttachment {
std::shared_ptr<Texture> texture = nullptr;
LoadAction load_action = LoadAction::kDontCare;
StoreAction store_action = StoreAction::kStore;
constexpr operator bool() const { return static_cast<bool>(texture); }
};
struct ColorRenderPassAttachment : public RenderPassAttachment {
Color clear_color = Color::BlackTransparent();
};
struct DepthRenderPassAttachment : public RenderPassAttachment {
double clear_depth = 0.0;
};
struct StencilRenderPassAttachment : public RenderPassAttachment {
uint32_t clear_stencil = 0;
};
} // namespace impeller
namespace std {

View File

@ -21,15 +21,15 @@ constexpr MTLPixelFormat ToMTLPixelFormat(PixelFormat format) {
switch (format) {
case PixelFormat::kUnknown:
return MTLPixelFormatInvalid;
case PixelFormat::kPixelFormat_B8G8R8A8_UNormInt:
case PixelFormat::kB8G8R8A8_UNormInt:
return MTLPixelFormatBGRA8Unorm;
case PixelFormat::kPixelFormat_B8G8R8A8_UNormInt_SRGB:
case PixelFormat::kB8G8R8A8_UNormInt_SRGB:
return MTLPixelFormatBGRA8Unorm_sRGB;
case PixelFormat::kPixelFormat_D32_Float_S8_UNormInt:
case PixelFormat::kD32_Float_S8_UNormInt:
return MTLPixelFormatDepth32Float_Stencil8;
case PixelFormat::kPixelFormat_R8G8B8A8_UNormInt:
case PixelFormat::kR8G8B8A8_UNormInt:
return MTLPixelFormatRGBA8Unorm;
case PixelFormat::kPixelFormat_R8G8B8A8_UNormInt_SRGB:
case PixelFormat::kR8G8B8A8_UNormInt_SRGB:
return MTLPixelFormatRGBA8Unorm_sRGB;
}
return MTLPixelFormatInvalid;

View File

@ -85,7 +85,7 @@ struct PipelineBuilder {
// TODO(csg): This can be easily reflected but we are sticking to the
// convention that the first stage output is the color output.
ColorAttachmentDescriptor color0;
color0.format = PixelFormat::kPixelFormat_B8G8R8A8_UNormInt;
color0.format = PixelFormat::kB8G8R8A8_UNormInt;
desc.SetColorAttachmentDescriptor(0u, std::move(color0));
}

View File

@ -13,6 +13,7 @@
#include "impeller/compositor/command.h"
#include "impeller/compositor/formats.h"
#include "impeller/compositor/host_buffer.h"
#include "impeller/compositor/render_pass_descriptor.h"
#include "impeller/compositor/texture.h"
#include "impeller/geometry/color.h"
#include "impeller/geometry/size.h"
@ -21,53 +22,6 @@ namespace impeller {
class CommandBuffer;
struct RenderPassAttachment {
std::shared_ptr<Texture> texture;
LoadAction load_action = LoadAction::kDontCare;
StoreAction store_action = StoreAction::kStore;
constexpr operator bool() const { return static_cast<bool>(texture); }
};
struct ColorRenderPassAttachment : public RenderPassAttachment {
Color clear_color = Color::BlackTransparent();
};
struct DepthRenderPassAttachment : public RenderPassAttachment {
double clear_depth = 0.0;
};
struct StencilRenderPassAttachment : public RenderPassAttachment {
uint32_t clear_stencil = 0;
};
class RenderPassDescriptor {
public:
RenderPassDescriptor();
~RenderPassDescriptor();
bool HasColorAttachment(size_t index) const;
std::optional<ISize> GetColorAttachmentSize(size_t index) const;
RenderPassDescriptor& SetColorAttachment(ColorRenderPassAttachment attachment,
size_t index);
RenderPassDescriptor& SetDepthAttachment(
DepthRenderPassAttachment attachment);
RenderPassDescriptor& SetStencilAttachment(
StencilRenderPassAttachment attachment);
MTLRenderPassDescriptor* ToMTLRenderPassDescriptor() const;
private:
std::map<size_t, ColorRenderPassAttachment> colors_;
std::optional<DepthRenderPassAttachment> depth_;
std::optional<StencilRenderPassAttachment> stencil_;
};
class RenderPass {
public:
~RenderPass();

View File

@ -17,53 +17,6 @@
namespace impeller {
RenderPassDescriptor::RenderPassDescriptor() = default;
RenderPassDescriptor::~RenderPassDescriptor() = default;
bool RenderPassDescriptor::HasColorAttachment(size_t index) const {
if (auto found = colors_.find(index); found != colors_.end()) {
return true;
}
return false;
}
std::optional<ISize> RenderPassDescriptor::GetColorAttachmentSize(
size_t index) const {
auto found = colors_.find(index);
if (found == colors_.end()) {
return std::nullopt;
}
return found->second.texture->GetSize();
}
RenderPassDescriptor& RenderPassDescriptor::SetColorAttachment(
ColorRenderPassAttachment attachment,
size_t index) {
if (attachment) {
colors_[index] = attachment;
}
return *this;
}
RenderPassDescriptor& RenderPassDescriptor::SetDepthAttachment(
DepthRenderPassAttachment attachment) {
if (attachment) {
depth_ = std::move(attachment);
}
return *this;
}
RenderPassDescriptor& RenderPassDescriptor::SetStencilAttachment(
StencilRenderPassAttachment attachment) {
if (attachment) {
stencil_ = std::move(attachment);
}
return *this;
}
static bool ConfigureAttachment(const RenderPassAttachment& desc,
MTLRenderPassAttachmentDescriptor* attachment) {
if (!desc.texture) {

View File

@ -0,0 +1,45 @@
// 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 <map>
#include <optional>
#include <Metal/Metal.h>
#include "flutter/fml/macros.h"
#include "impeller/compositor/formats.h"
#include "impeller/geometry/size.h"
namespace impeller {
class RenderPassDescriptor {
public:
RenderPassDescriptor();
~RenderPassDescriptor();
bool HasColorAttachment(size_t index) const;
std::optional<ISize> GetColorAttachmentSize(size_t index) const;
RenderPassDescriptor& SetColorAttachment(ColorRenderPassAttachment attachment,
size_t index);
RenderPassDescriptor& SetDepthAttachment(
DepthRenderPassAttachment attachment);
RenderPassDescriptor& SetStencilAttachment(
StencilRenderPassAttachment attachment);
MTLRenderPassDescriptor* ToMTLRenderPassDescriptor() const;
private:
std::map<size_t, ColorRenderPassAttachment> colors_;
std::optional<DepthRenderPassAttachment> depth_;
std::optional<StencilRenderPassAttachment> stencil_;
};
} // namespace impeller

View File

@ -0,0 +1,58 @@
// 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 "impeller/compositor/render_pass_descriptor.h"
#include "impeller/compositor/texture.h"
namespace impeller {
RenderPassDescriptor::RenderPassDescriptor() = default;
RenderPassDescriptor::~RenderPassDescriptor() = default;
bool RenderPassDescriptor::HasColorAttachment(size_t index) const {
if (auto found = colors_.find(index); found != colors_.end()) {
return true;
}
return false;
}
std::optional<ISize> RenderPassDescriptor::GetColorAttachmentSize(
size_t index) const {
auto found = colors_.find(index);
if (found == colors_.end()) {
return std::nullopt;
}
return found->second.texture->GetSize();
}
RenderPassDescriptor& RenderPassDescriptor::SetColorAttachment(
ColorRenderPassAttachment attachment,
size_t index) {
if (attachment) {
colors_[index] = attachment;
}
return *this;
}
RenderPassDescriptor& RenderPassDescriptor::SetDepthAttachment(
DepthRenderPassAttachment attachment) {
if (attachment) {
depth_ = std::move(attachment);
}
return *this;
}
RenderPassDescriptor& RenderPassDescriptor::SetStencilAttachment(
StencilRenderPassAttachment attachment) {
if (attachment) {
stencil_ = std::move(attachment);
}
return *this;
}
} // namespace impeller

View File

@ -131,7 +131,7 @@ bool Playground::OpenPlaygroundHere(Renderer::RenderCallback render_callback) {
}
TextureDescriptor color0_desc;
color0_desc.format = PixelFormat::kPixelFormat_B8G8R8A8_UNormInt;
color0_desc.format = PixelFormat::kB8G8R8A8_UNormInt;
color0_desc.size = {
static_cast<ISize::Type>(current_drawable.texture.width),
static_cast<ISize::Type>(current_drawable.texture.height)};
@ -181,7 +181,7 @@ std::shared_ptr<Texture> Playground::CreateTextureForFixture(
}
auto texture_descriptor = TextureDescriptor{};
texture_descriptor.format = PixelFormat::kPixelFormat_R8G8B8A8_UNormInt;
texture_descriptor.format = PixelFormat::kR8G8B8A8_UNormInt;
texture_descriptor.size = image.GetSize();
texture_descriptor.mip_count = 1u;