Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions impeller/base/base_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@
#include "impeller/base/thread.h"

namespace impeller {

enum class MyMaskBits : uint32_t {
kFoo = 0,
kBar = 1 << 0,
kBaz = 1 << 1,
kBang = 1 << 2,
};

using MyMask = Mask<MyMaskBits>;

IMPELLER_ENUM_IS_MASK(MyMaskBits);

namespace testing {

struct Foo {
Expand Down Expand Up @@ -251,15 +263,6 @@ TEST(BaseTest, NoExceptionPromiseEmpty) {
wrapper.reset();
}

enum class MyMaskBits : uint32_t {
kFoo = 0,
kBar = 1 << 0,
kBaz = 1 << 1,
kBang = 1 << 2,
};

using MyMask = Mask<MyMaskBits>;

TEST(BaseTest, CanUseTypedMasks) {
{
MyMask mask;
Expand Down
61 changes: 48 additions & 13 deletions impeller/base/mask.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,21 @@

namespace impeller {

template <typename EnumType_>
struct MaskTraits {
static constexpr bool kIsMask = false;
};

//------------------------------------------------------------------------------
/// @brief Declare this in the "impeller" namespace to make the enum
/// maskable.
///
#define IMPELLER_ENUM_IS_MASK(enum_name) \
template <> \
struct MaskTraits<enum_name> { \
static constexpr bool kIsMask = true; \
};

//------------------------------------------------------------------------------
/// @brief A mask of typed enums.
///
Expand Down Expand Up @@ -110,42 +125,56 @@ struct Mask {

// Construction from Enum Types

template <typename EnumType>
template <
typename EnumType,
typename std::enable_if<MaskTraits<EnumType>::kIsMask, bool>::type = true>
inline constexpr Mask<EnumType> operator|(const EnumType& lhs,
const EnumType& rhs) {
return Mask<EnumType>{lhs} | rhs;
}

template <typename EnumType>
template <
typename EnumType,
typename std::enable_if<MaskTraits<EnumType>::kIsMask, bool>::type = true>
inline constexpr Mask<EnumType> operator&(const EnumType& lhs,
const EnumType& rhs) {
return Mask<EnumType>{lhs} & rhs;
}

template <typename EnumType>
template <
typename EnumType,
typename std::enable_if<MaskTraits<EnumType>::kIsMask, bool>::type = true>
inline constexpr Mask<EnumType> operator^(const EnumType& lhs,
const EnumType& rhs) {
return Mask<EnumType>{lhs} ^ rhs;
}

template <typename EnumType>
template <
typename EnumType,
typename std::enable_if<MaskTraits<EnumType>::kIsMask, bool>::type = true>
inline constexpr Mask<EnumType> operator~(const EnumType& other) {
return ~Mask<EnumType>{other};
}

template <typename EnumType>
template <
typename EnumType,
typename std::enable_if<MaskTraits<EnumType>::kIsMask, bool>::type = true>
inline constexpr Mask<EnumType> operator|(const EnumType& lhs,
const Mask<EnumType>& rhs) {
return Mask<EnumType>{lhs} | rhs;
}

template <typename EnumType>
template <
typename EnumType,
typename std::enable_if<MaskTraits<EnumType>::kIsMask, bool>::type = true>
inline constexpr Mask<EnumType> operator&(const EnumType& lhs,
const Mask<EnumType>& rhs) {
return Mask<EnumType>{lhs} & rhs;
}

template <typename EnumType>
template <
typename EnumType,
typename std::enable_if<MaskTraits<EnumType>::kIsMask, bool>::type = true>
inline constexpr Mask<EnumType> operator^(const EnumType& lhs,
const Mask<EnumType>& rhs) {
return Mask<EnumType>{lhs} ^ rhs;
Expand All @@ -154,37 +183,43 @@ inline constexpr Mask<EnumType> operator^(const EnumType& lhs,
// Relational operators with EnumType promotion. These can be replaced by a
// defaulted spaceship operator post C++20.

template <typename EnumType>
template <typename EnumType,
typename std::enable_if_t<MaskTraits<EnumType>::kIsMask, bool> = true>
inline constexpr bool operator<(const EnumType& lhs,
const Mask<EnumType>& rhs) {
return Mask<EnumType>{lhs} < rhs;
}

template <typename EnumType>
template <typename EnumType,
typename std::enable_if_t<MaskTraits<EnumType>::kIsMask, bool> = true>
inline constexpr bool operator>(const EnumType& lhs,
const Mask<EnumType>& rhs) {
return Mask<EnumType>{lhs} > rhs;
}

template <typename EnumType>
template <typename EnumType,
typename std::enable_if_t<MaskTraits<EnumType>::kIsMask, bool> = true>
inline constexpr bool operator<=(const EnumType& lhs,
const Mask<EnumType>& rhs) {
return Mask<EnumType>{lhs} <= rhs;
}

template <typename EnumType>
template <typename EnumType,
typename std::enable_if_t<MaskTraits<EnumType>::kIsMask, bool> = true>
inline constexpr bool operator>=(const EnumType& lhs,
const Mask<EnumType>& rhs) {
return Mask<EnumType>{lhs} >= rhs;
}

template <typename EnumType>
template <typename EnumType,
typename std::enable_if_t<MaskTraits<EnumType>::kIsMask, bool> = true>
inline constexpr bool operator==(const EnumType& lhs,
const Mask<EnumType>& rhs) {
return Mask<EnumType>{lhs} == rhs;
}

template <typename EnumType>
template <typename EnumType,
typename std::enable_if_t<MaskTraits<EnumType>::kIsMask, bool> = true>
inline constexpr bool operator!=(const EnumType& lhs,
const Mask<EnumType>& rhs) {
return Mask<EnumType>{lhs} != rhs;
Expand Down
6 changes: 3 additions & 3 deletions impeller/core/formats.cc
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,13 @@ bool Attachment::IsValid() const {

std::string TextureUsageMaskToString(TextureUsageMask mask) {
std::vector<TextureUsage> usages;
if (mask & static_cast<TextureUsageMask>(TextureUsage::kShaderRead)) {
if (mask & TextureUsage::kShaderRead) {
usages.push_back(TextureUsage::kShaderRead);
}
if (mask & static_cast<TextureUsageMask>(TextureUsage::kShaderWrite)) {
if (mask & TextureUsage::kShaderWrite) {
usages.push_back(TextureUsage::kShaderWrite);
}
if (mask & static_cast<TextureUsageMask>(TextureUsage::kRenderTarget)) {
if (mask & TextureUsage::kRenderTarget) {
usages.push_back(TextureUsage::kRenderTarget);
}
std::stringstream stream;
Expand Down
26 changes: 13 additions & 13 deletions impeller/core/formats.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#include "flutter/fml/hash_combine.h"
#include "flutter/fml/logging.h"
#include "impeller/base/mask.h"
#include "impeller/geometry/color.h"
#include "impeller/geometry/rect.h"
#include "impeller/geometry/scalar.h"
Expand Down Expand Up @@ -297,18 +298,15 @@ enum class SampleCount : uint8_t {
kCount4 = 4,
};

using TextureUsageMask = uint64_t;

enum class TextureUsage : TextureUsageMask {
enum class TextureUsage {
kUnknown = 0,
kShaderRead = 1 << 0,
kShaderWrite = 1 << 1,
kRenderTarget = 1 << 2,
};
IMPELLER_ENUM_IS_MASK(TextureUsage);

constexpr bool TextureUsageIsRenderTarget(TextureUsageMask mask) {
return static_cast<TextureUsageMask>(TextureUsage::kRenderTarget) & mask;
}
using TextureUsageMask = Mask<TextureUsage>;

constexpr const char* TextureUsageToString(TextureUsage usage) {
switch (usage) {
Expand Down Expand Up @@ -435,14 +433,17 @@ enum class SamplerAddressMode {
kDecal,
};

enum class ColorWriteMask : uint64_t {
enum class ColorWriteMaskBits : uint64_t {
kNone = 0,
kRed = 1 << 0,
kGreen = 1 << 1,
kBlue = 1 << 2,
kAlpha = 1 << 3,
kAll = kRed | kGreen | kBlue | kAlpha,
};
IMPELLER_ENUM_IS_MASK(ColorWriteMaskBits);

using ColorWriteMask = Mask<ColorWriteMaskBits>;

constexpr size_t BytesPerPixelForPixelFormat(PixelFormat format) {
switch (format) {
Expand Down Expand Up @@ -508,8 +509,7 @@ struct ColorAttachmentDescriptor {
BlendOperation alpha_blend_op = BlendOperation::kAdd;
BlendFactor dst_alpha_blend_factor = BlendFactor::kOneMinusSourceAlpha;

std::underlying_type_t<ColorWriteMask> write_mask =
static_cast<uint64_t>(ColorWriteMask::kAll);
ColorWriteMask write_mask = ColorWriteMaskBits::kAll;

constexpr bool operator==(const ColorAttachmentDescriptor& o) const {
return format == o.format && //
Expand All @@ -524,10 +524,10 @@ struct ColorAttachmentDescriptor {
}

constexpr size_t Hash() const {
return fml::HashCombine(format, blending_enabled, src_color_blend_factor,
color_blend_op, dst_color_blend_factor,
src_alpha_blend_factor, alpha_blend_op,
dst_alpha_blend_factor, write_mask);
return fml::HashCombine(
format, blending_enabled, src_color_blend_factor, color_blend_op,
dst_color_blend_factor, src_alpha_blend_factor, alpha_blend_op,
dst_alpha_blend_factor, static_cast<uint64_t>(write_mask));
}
};

Expand Down
3 changes: 1 addition & 2 deletions impeller/core/texture_descriptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ struct TextureDescriptor {
PixelFormat format = PixelFormat::kUnknown;
ISize size;
size_t mip_count = 1u; // Size::MipCount is usually appropriate.
TextureUsageMask usage =
static_cast<TextureUsageMask>(TextureUsage::kShaderRead);
TextureUsageMask usage = TextureUsage::kShaderRead;
SampleCount sample_count = SampleCount::kCount1;
CompressionType compression_type = CompressionType::kLossless;

Expand Down
3 changes: 1 addition & 2 deletions impeller/entity/contents/content_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -458,8 +458,7 @@ ContentContext::ContentContext(
auto clip_color_attachments =
clip_pipeline_descriptor->GetColorAttachmentDescriptors();
for (auto& color_attachment : clip_color_attachments) {
color_attachment.second.write_mask =
static_cast<uint64_t>(ColorWriteMask::kNone);
color_attachment.second.write_mask = ColorWriteMaskBits::kNone;
}
clip_pipeline_descriptor->SetColorAttachmentDescriptors(
std::move(clip_color_attachments));
Expand Down
2 changes: 1 addition & 1 deletion impeller/entity/entity_pass_target_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ TEST_P(EntityPassTargetTest, SwapWithMSAAImplicitResolve) {
color0_tex_desc.sample_count = SampleCount::kCount4;
color0_tex_desc.format = pixel_format;
color0_tex_desc.size = ISize{100, 100};
color0_tex_desc.usage = static_cast<uint64_t>(TextureUsage::kRenderTarget);
color0_tex_desc.usage = TextureUsage::kRenderTarget;

auto color0_msaa_tex = allocator.CreateTexture(color0_tex_desc);

Expand Down
16 changes: 7 additions & 9 deletions impeller/renderer/backend/gles/render_pass_gles.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,16 @@ void ConfigureBlending(const ProcTableGLES& gl,
}

{
const auto is_set = [](std::underlying_type_t<ColorWriteMask> mask,
const auto is_set = [](ColorWriteMask mask,
ColorWriteMask check) -> GLboolean {
using RawType = decltype(mask);
return (static_cast<RawType>(mask) & static_cast<RawType>(check))
? GL_TRUE
: GL_FALSE;
return (mask & check) ? GL_TRUE : GL_FALSE;
};

gl.ColorMask(is_set(color->write_mask, ColorWriteMask::kRed), // red
is_set(color->write_mask, ColorWriteMask::kGreen), // green
is_set(color->write_mask, ColorWriteMask::kBlue), // blue
is_set(color->write_mask, ColorWriteMask::kAlpha) // alpha
gl.ColorMask(
is_set(color->write_mask, ColorWriteMaskBits::kRed), // red
is_set(color->write_mask, ColorWriteMaskBits::kGreen), // green
is_set(color->write_mask, ColorWriteMaskBits::kBlue), // blue
is_set(color->write_mask, ColorWriteMaskBits::kAlpha) // alpha
);
}
}
Expand Down
5 changes: 2 additions & 3 deletions impeller/renderer/backend/gles/surface_gles.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ std::unique_ptr<Surface> SurfaceGLES::WrapFBO(
color0_tex.type = TextureType::kTexture2D;
color0_tex.format = color_format;
color0_tex.size = fbo_size;
color0_tex.usage = static_cast<TextureUsageMask>(TextureUsage::kRenderTarget);
color0_tex.usage = TextureUsage::kRenderTarget;
color0_tex.sample_count = SampleCount::kCount1;
color0_tex.storage_mode = StorageMode::kDevicePrivate;

Expand All @@ -44,8 +44,7 @@ std::unique_ptr<Surface> SurfaceGLES::WrapFBO(
depth_stencil_texture_desc.type = TextureType::kTexture2D;
depth_stencil_texture_desc.format = color_format;
depth_stencil_texture_desc.size = fbo_size;
depth_stencil_texture_desc.usage =
static_cast<TextureUsageMask>(TextureUsage::kRenderTarget);
depth_stencil_texture_desc.usage = TextureUsage::kRenderTarget;
depth_stencil_texture_desc.sample_count = SampleCount::kCount1;

auto depth_stencil_tex = std::make_shared<TextureGLES>(
Expand Down
3 changes: 1 addition & 2 deletions impeller/renderer/backend/gles/texture_gles.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ static bool IsDepthStencilFormat(PixelFormat format) {
static TextureGLES::Type GetTextureTypeFromDescriptor(
const TextureDescriptor& desc) {
const auto usage = static_cast<TextureUsageMask>(desc.usage);
const auto render_target =
static_cast<TextureUsageMask>(TextureUsage::kRenderTarget);
const auto render_target = TextureUsage::kRenderTarget;
const auto is_msaa = desc.sample_count == SampleCount::kCount4;
if (usage == render_target && IsDepthStencilFormat(desc.format)) {
return is_msaa ? TextureGLES::Type::kRenderBufferMultisampled
Expand Down
13 changes: 5 additions & 8 deletions impeller/renderer/backend/metal/formats_mtl.h
Original file line number Diff line number Diff line change
Expand Up @@ -207,25 +207,22 @@ constexpr MTLBlendOperation ToMTLBlendOperation(BlendOperation type) {
return MTLBlendOperationAdd;
};

constexpr MTLColorWriteMask ToMTLColorWriteMask(
std::underlying_type_t<ColorWriteMask> type) {
using UnderlyingType = decltype(type);

constexpr MTLColorWriteMask ToMTLColorWriteMask(ColorWriteMask type) {
MTLColorWriteMask mask = MTLColorWriteMaskNone;

if (type & static_cast<UnderlyingType>(ColorWriteMask::kRed)) {
if (type & ColorWriteMaskBits::kRed) {
mask |= MTLColorWriteMaskRed;
}

if (type & static_cast<UnderlyingType>(ColorWriteMask::kGreen)) {
if (type & ColorWriteMaskBits::kGreen) {
mask |= MTLColorWriteMaskGreen;
}

if (type & static_cast<UnderlyingType>(ColorWriteMask::kBlue)) {
if (type & ColorWriteMaskBits::kBlue) {
mask |= MTLColorWriteMaskBlue;
}

if (type & static_cast<UnderlyingType>(ColorWriteMask::kAlpha)) {
if (type & ColorWriteMaskBits::kAlpha) {
mask |= MTLColorWriteMaskAlpha;
}

Expand Down
8 changes: 4 additions & 4 deletions impeller/renderer/backend/metal/formats_mtl.mm
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,16 @@
mtl_desc.height = desc.size.height;
mtl_desc.mipmapLevelCount = desc.mip_count;
mtl_desc.usage = MTLTextureUsageUnknown;
if (desc.usage & static_cast<TextureUsageMask>(TextureUsage::kUnknown)) {
if (desc.usage & TextureUsage::kUnknown) {
mtl_desc.usage |= MTLTextureUsageUnknown;
}
if (desc.usage & static_cast<TextureUsageMask>(TextureUsage::kShaderRead)) {
if (desc.usage & TextureUsage::kShaderRead) {
mtl_desc.usage |= MTLTextureUsageShaderRead;
}
if (desc.usage & static_cast<TextureUsageMask>(TextureUsage::kShaderWrite)) {
if (desc.usage & TextureUsage::kShaderWrite) {
mtl_desc.usage |= MTLTextureUsageShaderWrite;
}
if (desc.usage & static_cast<TextureUsageMask>(TextureUsage::kRenderTarget)) {
if (desc.usage & TextureUsage::kRenderTarget) {
mtl_desc.usage |= MTLTextureUsageRenderTarget;
}
return mtl_desc;
Expand Down
Loading