blob: 35124691b35e96ef2dfe9da97c70652d77c99151 [file]
// 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.
#ifndef FLUTTER_IMPELLER_RENDERER_SHADER_KEY_H_
#define FLUTTER_IMPELLER_RENDERER_SHADER_KEY_H_
#include <memory>
#include <string>
#include <string_view>
#include <unordered_map>
#include "flutter/fml/hash_combine.h"
#include "impeller/core/shader_types.h"
namespace impeller {
struct ShaderKey {
std::string name;
ShaderStage stage = ShaderStage::kUnknown;
ShaderKey(std::string_view p_name, ShaderStage p_stage)
: name({p_name.data(), p_name.size()}), stage(p_stage) {}
struct Hash {
size_t operator()(const ShaderKey& key) const {
return fml::HashCombine(key.name, key.stage);
}
};
struct Equal {
constexpr bool operator()(const ShaderKey& k1, const ShaderKey& k2) const {
return k1.stage == k2.stage && k1.name == k2.name;
}
};
/// Build a registry name for a user-supplied shader so it cannot collide
/// with engine-internal names that share the same source-derived
/// entrypoint.
///
/// `scope` is a short tag identifying the source kind (see kScope*
/// constants below). `library_id` is an opaque identifier that is stable
/// across reloads of the same logical user shader (typically the asset
/// path that loaded the shader). `entrypoint` is the original shader
/// entrypoint name as produced by impellerc.
///
/// Engine-internal entrypoints generated by impellerc are valid
/// identifiers and therefore cannot contain ':', so the colon-separated
/// format makes user-scoped names unspoofable from the engine namespace.
static std::string MakeUserScopedName(std::string_view scope,
std::string_view library_id,
std::string_view entrypoint);
/// Scope tag for `RuntimeStage`-backed shaders (FragmentProgram /
/// RuntimeEffect).
static constexpr std::string_view kScopeRuntimeEffect = "re";
/// Scope tag for Flutter GPU user shader bundles.
static constexpr std::string_view kScopeFlutterGPU = "fg";
/// Returns a process-unique library id for user shader sources that were
/// not constructed via an asset-bearing entry point (e.g. tests, future
/// in-memory APIs). Stable for the lifetime of the source but distinct
/// across sources, so the user-scoped registry name cannot collide.
static std::string MakeFallbackLibraryId();
};
class ShaderFunction;
using ShaderFunctionMap =
std::unordered_map<ShaderKey,
std::shared_ptr<const ShaderFunction>,
ShaderKey::Hash,
ShaderKey::Equal>;
} // namespace impeller
#endif // FLUTTER_IMPELLER_RENDERER_SHADER_KEY_H_