From 5b67468e2168f78ec38d03900632766ae559bb87 Mon Sep 17 00:00:00 2001 From: Jonah Williams Date: Wed, 13 Dec 2023 11:57:06 -0800 Subject: [PATCH] [Impeller] Compute ContextContentOptions key via bit manipulating (instead of hashing each property). (flutter/engine#48902) The hash function we use for pipeline hashncash is actually pretty slow. Since all of the properties we hash on fit in 64 bits, we can compute a hash value directly. From local benchmarking this is about twice as fast, which still leaves quite a bit of room for future improvement (faster hashmap? not using a hashmap at all?) --- engine/src/flutter/impeller/core/formats.h | 10 +++---- .../entity/contents/content_context.h | 26 ++++++++++++++----- .../impeller/entity/entity_unittests.cc | 21 +++++++++++++++ engine/src/flutter/impeller/geometry/color.h | 2 +- 4 files changed, 46 insertions(+), 13 deletions(-) diff --git a/engine/src/flutter/impeller/core/formats.h b/engine/src/flutter/impeller/core/formats.h index 5d5d64299f..b5c2bff8bf 100644 --- a/engine/src/flutter/impeller/core/formats.h +++ b/engine/src/flutter/impeller/core/formats.h @@ -91,7 +91,7 @@ constexpr const char* StorageModeToString(StorageMode mode) { /// esoteric formats and use blit passes to convert to a /// non-esoteric pass. /// -enum class PixelFormat { +enum class PixelFormat : uint8_t { kUnknown, kA8UNormInt, kR8UNormInt, @@ -287,7 +287,7 @@ constexpr bool IsMultisampleCapable(TextureType type) { return false; } -enum class SampleCount { +enum class SampleCount : uint8_t { kCount1 = 1, kCount4 = 4, }; @@ -347,7 +347,7 @@ enum class IndexType { }; /// Decides how backend draws pixels based on input vertices. -enum class PrimitiveType { +enum class PrimitiveType : uint8_t { /// Draws a triage for each separate set of three vertices. /// /// Vertices [A, B, C, D, E, F] will produce triages @@ -526,7 +526,7 @@ struct ColorAttachmentDescriptor { } }; -enum class CompareFunction { +enum class CompareFunction : uint8_t { /// Comparison test never passes. kNever, /// Comparison test passes always passes. @@ -545,7 +545,7 @@ enum class CompareFunction { kGreaterEqual, }; -enum class StencilOperation { +enum class StencilOperation : uint8_t { /// Don't modify the current stencil value. kKeep, /// Reset the stencil value to zero. diff --git a/engine/src/flutter/impeller/entity/contents/content_context.h b/engine/src/flutter/impeller/entity/contents/content_context.h index b9ee0ffc93..12d6120608 100644 --- a/engine/src/flutter/impeller/entity/contents/content_context.h +++ b/engine/src/flutter/impeller/entity/contents/content_context.h @@ -11,9 +11,7 @@ #include #include "flutter/fml/build_config.h" -#include "flutter/fml/hash_combine.h" #include "flutter/fml/logging.h" -#include "flutter/fml/macros.h" #include "impeller/base/validation.h" #include "impeller/core/formats.h" #include "impeller/entity/entity.h" @@ -280,11 +278,25 @@ struct ContentContextOptions { bool is_for_rrect_blur_clear = false; struct Hash { - constexpr std::size_t operator()(const ContentContextOptions& o) const { - return fml::HashCombine( - o.sample_count, o.blend_mode, o.stencil_compare, o.stencil_operation, - o.primitive_type, o.color_attachment_pixel_format, - o.has_stencil_attachment, o.wireframe, o.is_for_rrect_blur_clear); + constexpr uint64_t operator()(const ContentContextOptions& o) const { + static_assert(sizeof(o.sample_count) == 1); + static_assert(sizeof(o.blend_mode) == 1); + static_assert(sizeof(o.sample_count) == 1); + static_assert(sizeof(o.stencil_compare) == 1); + static_assert(sizeof(o.stencil_operation) == 1); + static_assert(sizeof(o.primitive_type) == 1); + static_assert(sizeof(o.color_attachment_pixel_format) == 1); + + return (o.is_for_rrect_blur_clear ? 1llu : 0llu) << 0 | + (o.wireframe ? 1llu : 0llu) << 1 | + (o.has_stencil_attachment ? 1llu : 0llu) << 2 | + // enums + static_cast(o.color_attachment_pixel_format) << 16 | + static_cast(o.primitive_type) << 24 | + static_cast(o.stencil_operation) << 32 | + static_cast(o.stencil_compare) << 40 | + static_cast(o.blend_mode) << 48 | + static_cast(o.sample_count) << 56; } }; diff --git a/engine/src/flutter/impeller/entity/entity_unittests.cc b/engine/src/flutter/impeller/entity/entity_unittests.cc index 45a093857d..c265686491 100644 --- a/engine/src/flutter/impeller/entity/entity_unittests.cc +++ b/engine/src/flutter/impeller/entity/entity_unittests.cc @@ -2598,6 +2598,27 @@ TEST_P(EntityTest, PipelineDescriptorEqAndHash) { EXPECT_EQ(desc_1->GetHash(), desc_2->GetHash()); } +// This doesn't really tell you if the hashes will have frequent +// collisions, but since this type is only used to hash a bounded +// set of options, we can just compare benchmarks. +TEST_P(EntityTest, ContentContextOptionsHasReasonableHashFunctions) { + ContentContextOptions opts; + auto hash_a = ContentContextOptions::Hash{}(opts); + + opts.blend_mode = BlendMode::kColorBurn; + auto hash_b = ContentContextOptions::Hash{}(opts); + + opts.has_stencil_attachment = false; + auto hash_c = ContentContextOptions::Hash{}(opts); + + opts.primitive_type = PrimitiveType::kPoint; + auto hash_d = ContentContextOptions::Hash{}(opts); + + EXPECT_NE(hash_a, hash_b); + EXPECT_NE(hash_b, hash_c); + EXPECT_NE(hash_c, hash_d); +} + #ifdef FML_OS_LINUX TEST_P(EntityTest, FramebufferFetchVulkanBindingOffsetIsTheSame) { // Using framebuffer fetch on Vulkan requires that we maintain a subpass input diff --git a/engine/src/flutter/impeller/geometry/color.h b/engine/src/flutter/impeller/geometry/color.h index 6aa8b502ae..c90c696066 100644 --- a/engine/src/flutter/impeller/geometry/color.h +++ b/engine/src/flutter/impeller/geometry/color.h @@ -56,7 +56,7 @@ enum class YUVColorSpace { kBT601LimitedRange, kBT601FullRange }; /// All blend modes assume that both the source (fragment output) and /// destination (first color attachment) have colors with premultiplied alpha. -enum class BlendMode { +enum class BlendMode : uint8_t { // The following blend modes are able to be used as pipeline blend modes or // via `BlendFilterContents`. kClear = 0,