Flutter Impeller
shader_library_gles.cc
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 
6 
7 #include <sstream>
8 
9 #include "flutter/fml/closure.h"
10 #include "impeller/base/config.h"
14 
15 namespace impeller {
16 
18  switch (type) {
20  return ShaderStage::kVertex;
24  return ShaderStage::kCompute;
25  }
26  FML_UNREACHABLE();
27 }
28 
29 static std::string GLESShaderNameToShaderKeyName(const std::string& name,
30  ShaderStage stage) {
31  std::stringstream stream;
32  stream << name;
33  switch (stage) {
35  stream << "_unknown_";
36  break;
38  stream << "_vertex_";
39  break;
41  stream << "_fragment_";
42  break;
44  stream << "_compute_";
45  break;
46  }
47  stream << "main";
48  return stream.str();
49 }
50 
51 ShaderLibraryGLES::ShaderLibraryGLES(
52  const std::vector<std::shared_ptr<fml::Mapping>>& shader_libraries) {
53  ShaderFunctionMap functions;
54  auto iterator = [&functions, library_id = library_id_](auto type, //
55  const auto& name, //
56  const auto& mapping //
57  ) -> bool {
58  const auto stage = ToShaderStage(type);
59  const auto key_name = GLESShaderNameToShaderKeyName(name, stage);
60  functions[ShaderKey{key_name, stage}] = std::shared_ptr<ShaderFunctionGLES>{
61  new ShaderFunctionGLES(library_id, //
62  stage, //
63  key_name, //
64  mapping //
65  )};
66 
67  return true;
68  };
69  for (auto library : shader_libraries) {
70  auto blob_library = ShaderArchive::Create(std::move(library));
71  if (!blob_library.ok()) {
72  VALIDATION_LOG << "Could not construct blob library for shaders: "
73  << blob_library.status().ToString();
74  return;
75  }
76  blob_library->IterateAllShaders(iterator);
77  }
78 
79  functions_ = functions;
80  is_valid_ = true;
81 }
82 
83 // |ShaderLibrary|
85 
86 // |ShaderLibrary|
88  return is_valid_;
89 }
90 
91 // |ShaderLibrary|
92 std::shared_ptr<const ShaderFunction> ShaderLibraryGLES::GetFunction(
93  std::string_view name,
94  ShaderStage stage) {
95  ReaderLock lock(functions_mutex_);
96  const auto key = ShaderKey{name, stage};
97  if (auto found = functions_.find(key); found != functions_.end()) {
98  return found->second;
99  }
100  return nullptr;
101 }
102 
103 // |ShaderLibrary|
104 void ShaderLibraryGLES::RegisterFunction(std::string name,
105  ShaderStage stage,
106  std::shared_ptr<fml::Mapping> code,
107  RegistrationCallback callback) {
108  if (!callback) {
109  callback = [](auto) {};
110  }
111  fml::ScopedCleanupClosure auto_fail([callback]() { callback(false); });
112  if (name.empty() || stage == ShaderStage::kUnknown || code == nullptr ||
113  code->GetMapping() == nullptr) {
114  VALIDATION_LOG << "Invalid runtime stage registration.";
115  return;
116  }
117  const auto key = ShaderKey{name, stage};
118  WriterLock lock(functions_mutex_);
119  if (functions_.count(key) != 0) {
120  VALIDATION_LOG << "Runtime stage named " << name
121  << " has already been registered.";
122  return;
123  }
124  functions_[key] = std::shared_ptr<ShaderFunctionGLES>(new ShaderFunctionGLES(
125  library_id_, //
126  stage, //
127  GLESShaderNameToShaderKeyName(name, stage), //
128  code //
129  ));
130  auto_fail.Release();
131  callback(true);
132 }
133 
134 // |ShaderLibrary|
135 void ShaderLibraryGLES::UnregisterFunction(std::string name,
136  ShaderStage stage) {
137  ReaderLock lock(functions_mutex_);
138 
139  const auto key = ShaderKey{name, stage};
140 
141  auto found = functions_.find(key);
142  if (found == functions_.end()) {
143  VALIDATION_LOG << "Library function named " << name
144  << " was not found, so it couldn't be unregistered.";
145  return;
146  }
147 
148  functions_.erase(found);
149 
150  return;
151 }
152 
153 } // namespace impeller
GLenum type
static absl::StatusOr< ShaderArchive > Create(std::shared_ptr< fml::Mapping > payload)
bool IsValid() const override
static std::string GLESShaderNameToShaderKeyName(const std::string &name, ShaderStage stage)
constexpr ShaderStage ToShaderStage(RuntimeShaderStage stage)
Definition: shader_types.h:29
std::unordered_map< ShaderKey, std::shared_ptr< const ShaderFunction >, ShaderKey::Hash, ShaderKey::Equal > ShaderFunctionMap
Definition: shader_key.h:43
#define VALIDATION_LOG
Definition: validation.h:91