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