| 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 << 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|
84 ShaderLibraryGLES::~ShaderLibraryGLES() = default;
85
86 // |ShaderLibrary|
87 bool ShaderLibraryGLES::IsValid() const {
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
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:87
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