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