[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?)
This commit is contained in:
Jonah Williams
2023-12-13 11:57:06 -08:00
committed by GitHub
parent be639e108f
commit 5b67468e21
4 changed files with 46 additions and 13 deletions

View File

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

View File

@@ -11,9 +11,7 @@
#include <unordered_map>
#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<uint64_t>(o.color_attachment_pixel_format) << 16 |
static_cast<uint64_t>(o.primitive_type) << 24 |
static_cast<uint64_t>(o.stencil_operation) << 32 |
static_cast<uint64_t>(o.stencil_compare) << 40 |
static_cast<uint64_t>(o.blend_mode) << 48 |
static_cast<uint64_t>(o.sample_count) << 56;
}
};

View File

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

View File

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