Flutter Impeller
shader_key.h
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef FLUTTER_IMPELLER_RENDERER_SHADER_KEY_H_
6 #define FLUTTER_IMPELLER_RENDERER_SHADER_KEY_H_
7 
8 #include <memory>
9 #include <string>
10 #include <string_view>
11 #include <unordered_map>
12 
13 #include "flutter/fml/hash_combine.h"
15 
16 namespace impeller {
17 
18 struct ShaderKey {
19  std::string name;
21 
22  ShaderKey(std::string_view p_name, ShaderStage p_stage)
23  : name({p_name.data(), p_name.size()}), stage(p_stage) {}
24 
25  struct Hash {
26  size_t operator()(const ShaderKey& key) const {
27  return fml::HashCombine(key.name, key.stage);
28  }
29  };
30 
31  struct Equal {
32  constexpr bool operator()(const ShaderKey& k1, const ShaderKey& k2) const {
33  return k1.stage == k2.stage && k1.name == k2.name;
34  }
35  };
36 
37  /// Build a registry name for a user-supplied shader so it cannot collide
38  /// with engine-internal names that share the same source-derived
39  /// entrypoint.
40  ///
41  /// `scope` is a short tag identifying the source kind (see kScope*
42  /// constants below). `library_id` is an opaque identifier that is stable
43  /// across reloads of the same logical user shader (typically the asset
44  /// path that loaded the shader). `entrypoint` is the original shader
45  /// entrypoint name as produced by impellerc.
46  ///
47  /// Engine-internal entrypoints generated by impellerc are valid
48  /// identifiers and therefore cannot contain ':', so the colon-separated
49  /// format makes user-scoped names unspoofable from the engine namespace.
50  static std::string MakeUserScopedName(std::string_view scope,
51  std::string_view library_id,
52  std::string_view entrypoint);
53 
54  /// Scope tag for `RuntimeStage`-backed shaders (FragmentProgram /
55  /// RuntimeEffect).
56  static constexpr std::string_view kScopeRuntimeEffect = "re";
57 
58  /// Scope tag for Flutter GPU user shader bundles.
59  static constexpr std::string_view kScopeFlutterGPU = "fg";
60 
61  /// Returns a process-unique library id for user shader sources that were
62  /// not constructed via an asset-bearing entry point (e.g. tests, future
63  /// in-memory APIs). Stable for the lifetime of the source but distinct
64  /// across sources, so the user-scoped registry name cannot collide.
65  static std::string MakeFallbackLibraryId();
66 };
67 
68 class ShaderFunction;
69 
71  std::unordered_map<ShaderKey,
72  std::shared_ptr<const ShaderFunction>,
75 
76 } // namespace impeller
77 
78 #endif // FLUTTER_IMPELLER_RENDERER_SHADER_KEY_H_
std::unordered_map< ShaderKey, std::shared_ptr< const ShaderFunction >, ShaderKey::Hash, ShaderKey::Equal > ShaderFunctionMap
Definition: shader_key.h:74
constexpr bool operator()(const ShaderKey &k1, const ShaderKey &k2) const
Definition: shader_key.h:32
size_t operator()(const ShaderKey &key) const
Definition: shader_key.h:26
static constexpr std::string_view kScopeFlutterGPU
Scope tag for Flutter GPU user shader bundles.
Definition: shader_key.h:59
static std::string MakeUserScopedName(std::string_view scope, std::string_view library_id, std::string_view entrypoint)
Definition: shader_key.cc:19
ShaderKey(std::string_view p_name, ShaderStage p_stage)
Definition: shader_key.h:22
static std::string MakeFallbackLibraryId()
Definition: shader_key.cc:14
static constexpr std::string_view kScopeRuntimeEffect
Definition: shader_key.h:56
ShaderStage stage
Definition: shader_key.h:20
std::string name
Definition: shader_key.h:19