MapLibre Native Core
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
shader_registry.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <mbgl/gfx/shader.hpp>
4 
5 #include <memory>
6 #include <mutex>
7 #include <shared_mutex>
8 #include <unordered_map>
9 
10 namespace mbgl {
11 namespace gfx {
12 
17  public:
18  explicit ShaderRegistry();
19  ShaderRegistry(const ShaderRegistry&) = delete;
20  ShaderRegistry(ShaderRegistry&&) noexcept = delete;
21  ShaderRegistry& operator=(const ShaderRegistry&) = delete;
22  ShaderRegistry& operator=(ShaderRegistry&&) noexcept = delete;
23  virtual ~ShaderRegistry() = default;
24 
28  [[nodiscard]] virtual bool isShader(const std::string& shaderName)
29  const noexcept;
30 
35  [[nodiscard]] virtual const std::shared_ptr<gfx::Shader> getShader(
36  const std::string& shaderName) const noexcept;
37 
43  [[nodiscard]] virtual bool replaceShader(
44  std::shared_ptr<Shader>&& shader) noexcept;
45 
53  [[nodiscard]] virtual bool replaceShader(
54  std::shared_ptr<Shader>&& shader, const std::string& shaderName) noexcept;
55 
62  [[nodiscard]] virtual bool registerShader(
63  std::shared_ptr<Shader>&& shader) noexcept;
64 
73  [[nodiscard]] virtual bool registerShader(
74  std::shared_ptr<Shader>&& shader, const std::string& shaderName) noexcept;
75 
80  template<typename T,
81  typename std::enable_if_t<is_shader_v<T>, bool>* = nullptr>
82  std::shared_ptr<T> get(const std::string& shaderName) noexcept {
83  auto shader = getShader(shaderName);
84  if (!shader || shader->typeName() != T::Name) {
85  return nullptr;
86  }
87  return std::static_pointer_cast<T>(shader);
88  }
89 
94  template<typename T,
95  typename std::enable_if_t<is_shader_v<T>, bool>* = nullptr>
96  std::shared_ptr<T> get() noexcept {
97  auto shader = getShader(std::string(T::Name));
98  if (!shader || shader->typeName() != T::Name) {
99  return nullptr;
100  }
101  return std::static_pointer_cast<T>(shader);
102  }
103 
110  template<typename T,
111  typename std::enable_if_t<is_shader_v<T>, bool>* = nullptr>
112  bool populate(std::shared_ptr<T>& to, const std::string& shaderName) noexcept {
113  if (to) {
114  return true;
115  }
116 
117  auto shader = getShader(shaderName);
118  if (!shader || shader->typeName() != T::Name) {
119  return false;
120  }
121  to = std::static_pointer_cast<T>(shader);
122  return true;
123  }
124 
131  template<typename T,
132  typename std::enable_if_t<is_shader_v<T>, bool>* = nullptr>
133  bool populate(std::shared_ptr<T>& to) noexcept {
134  if (to) {
135  return true;
136  }
137 
138  auto shader = getShader(std::string(T::Name));
139  if (!shader || shader->typeName() != T::Name) {
140  return false;
141  }
142  to = std::static_pointer_cast<T>(shader);
143  return true;
144  }
145 
146  private:
147  std::unordered_map<
148  std::string,
149  std::shared_ptr<gfx::Shader>
150  > programs;
151  mutable std::shared_mutex programLock;
152 };
153 
154 } // namespace gfx
155 } // namespace mbgl
A ShaderRegistry contains a collection of gfx::Shader instances. Using the registry,...
ShaderRegistry(ShaderRegistry &&) noexcept=delete
virtual bool isShader(const std::string &shaderName) const noexcept
Checks if a shader exists in the registry for the given name.
virtual bool registerShader(std::shared_ptr< Shader > &&shader) noexcept
Register a new shader with the registry. If a shader is present in the registry with a conflicting na...
bool populate(std::shared_ptr< T > &to, const std::string &shaderName) noexcept
Ensure the destination 'to' is populated with the requested shader. If already non-null,...
std::shared_ptr< T > get() noexcept
Shorthand helper to quickly get a derived type from the registry. This variant looks up shaders only ...
bool populate(std::shared_ptr< T > &to) noexcept
Ensure the destination 'to' is populated with the requested shader. If already non-null,...
ShaderRegistry(const ShaderRegistry &)=delete
virtual bool replaceShader(std::shared_ptr< Shader > &&shader) noexcept
Replace a matching shader in the registry with the provided instance. Shader type-names must match.
virtual const std::shared_ptr< gfx::Shader > getShader(const std::string &shaderName) const noexcept
Get a shader from the registry by name.
A shader is used as the base class for all programs across any supported backend API....
Definition: shader.hpp:27
constexpr bool is_shader_v
Definition: shader.hpp:17
std::unique_ptr< Expression > string(std::unique_ptr< Expression >, std::unique_ptr< Expression > def=nullptr)
Definition: actor.hpp:15
Definition: tile_id.hpp:256