| 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/shader_archive.h"
14
15 namespace impeller {
16
17 static ShaderStage ToShaderStage(ArchiveShaderType type) {
18 switch (type) {
19 case ArchiveShaderType::kVertex:
20 return ShaderStage::kVertex;
21 case ArchiveShaderType::kFragment:
22 return ShaderStage::kFragment;
23 case ArchiveShaderType::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::kCompute:
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 return;
74 }
75 blob_library->IterateAllShaders(iterator);
76 }
77
78 functions_ = functions;
79 is_valid_ = true;
80 }
81
82 // |ShaderLibrary|
83 ShaderLibraryGLES::~ShaderLibraryGLES() = default;
84
85 // |ShaderLibrary|
86 bool ShaderLibraryGLES::IsValid() const {
87 return is_valid_;
88 }
89
90 // |ShaderLibrary|
91 std::shared_ptr<const ShaderFunction> ShaderLibraryGLES::GetFunction(
92 std::string_view name,
93 ShaderStage stage) {
94 ReaderLock lock(functions_mutex_);
95 const auto key = ShaderKey{name, stage};
96 if (auto found = functions_.find(key); found != functions_.end()) {
97 return found->second;
98 }
99 return nullptr;
100 }
101
102 // |ShaderLibrary|
103 void ShaderLibraryGLES::RegisterFunction(std::string name,
104 ShaderStage stage,
105 std::shared_ptr<fml::Mapping> code,
106 RegistrationCallback callback) {
107 if (!callback) {
108 callback = [](auto) {};
109 }
110 fml::ScopedCleanupClosure auto_fail([callback]() { callback(false); });
111 if (name.empty() || stage == ShaderStage::kUnknown || code == nullptr ||
112 code->GetMapping() == nullptr) {
113 VALIDATION_LOG << "Invalid runtime stage registration.";
114 return;
115 }
116 const auto key = ShaderKey{name, stage};
117 WriterLock lock(functions_mutex_);
118 if (functions_.count(key) != 0) {
119 VALIDATION_LOG << "Runtime stage named " << name
120 << " has already been registered.";
121 return;
122 }
123 functions_[key] = std::shared_ptr<ShaderFunctionGLES>(new ShaderFunctionGLES(
124 library_id_, //
125 stage, //
126 GLESShaderNameToShaderKeyName(name, stage), //
127 code //
128 ));
129 auto_fail.Release();
130 callback(true);
131 }
132
133 // |ShaderLibrary|
134 void ShaderLibraryGLES::UnregisterFunction(std::string name,
135 ShaderStage stage) {
136 ReaderLock lock(functions_mutex_);
137
138 const auto key = ShaderKey{name, stage};
139
140 auto found = functions_.find(key);
141 if (found == functions_.end()) {
142 VALIDATION_LOG << "Library function named " << name
143 << " was not found, so it couldn't be unregistered.";
144 return;
145 }
146
147 functions_.erase(found);
148
149 return;
150 }
151
152 } // namespace impeller
config.h
type
GLenum type
Definition: blit_command_gles.cc:153
impeller::ReaderLock
Definition: thread.h:92
impeller::ShaderArchive::Create
static absl::StatusOr< ShaderArchive > Create(std::shared_ptr< fml::Mapping > payload)
Definition: shader_archive.cc:29
impeller::ShaderLibraryGLES::~ShaderLibraryGLES
~ShaderLibraryGLES() override
impeller::ShaderLibraryGLES::IsValid
bool IsValid() const override
Definition: shader_library_gles.cc:86
impeller
Definition: allocation.cc:12
impeller::GLESShaderNameToShaderKeyName
static std::string GLESShaderNameToShaderKeyName(const std::string &name, ShaderStage stage)
Definition: shader_library_gles.cc:29
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::ArchiveShaderType
ArchiveShaderType
Definition: shader_archive_types.h:10
impeller::ArchiveShaderType::kCompute
@ kCompute
impeller::ArchiveShaderType::kFragment
@ kFragment
impeller::ArchiveShaderType::kVertex
@ kVertex
impeller::ShaderStage
ShaderStage
Definition: shader_types.h:22
impeller::ShaderStage::kUnknown
@ kUnknown
impeller::ShaderStage::kCompute
@ kCompute
impeller::ShaderStage::kFragment
@ kFragment
impeller::ShaderStage::kVertex
@ kVertex
shader_archive.h
shader_function_gles.h
shader_library_gles.h
impeller::ShaderKey
Definition: shader_key.h:17
validation.h
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:91