| Flutter Impeller |

shader_archive.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/shader_archive/shader_archive.h"
6
7 #include <array>
8 #include <string>
9 #include <utility>
10
11 #include "impeller/base/validation.h"
12 #include "impeller/shader_archive/shader_archive_flatbuffers.h"
13
14 namespace impeller {
15
16 constexpr ArchiveShaderType ToShaderType(fb::Stage stage) {
17 switch (stage) {
18 case fb::Stage::kVertex:
19 return ArchiveShaderType::kVertex;
20 case fb::Stage::kFragment:
21 return ArchiveShaderType::kFragment;
22 case fb::Stage::kCompute:
23 return ArchiveShaderType::kCompute;
24 }
25 FML_UNREACHABLE();
26 }
27
28 ShaderArchive::ShaderArchive(std::shared_ptr<fml::Mapping> payload)
29 : payload_(std::move(payload)) {
30 if (!payload_ || payload_->GetMapping() == nullptr) {
31 VALIDATION_LOG << "Shader mapping was absent.";
32 return;
33 }
34
35 if (!fb::ShaderArchiveBufferHasIdentifier(payload_->GetMapping())) {
36 VALIDATION_LOG << "Invalid shader magic.";
37 return;
38 }
39
40 auto shader_archive = fb::GetShaderArchive(payload_->GetMapping());
41 if (!shader_archive) {
42 return;
43 }
44
45 if (auto items = shader_archive->items()) {
46 for (auto i = items->begin(), end = items->end(); i != end; i++) {
47 ShaderKey key;
48 key.name = i->name()->str();
49 key.type = ToShaderType(i->stage());
50 shaders_[key] = std::make_shared<fml::NonOwnedMapping>(
51 i->mapping()->Data(), i->mapping()->size(),
52 [payload = payload_](auto, auto) {
53 // The pointers are into the base payload. Instead of copying the
54 // data, just hold onto the payload.
55 });
56 }
57 }
58
59 is_valid_ = true;
60 }
61
62 ShaderArchive::ShaderArchive(ShaderArchive&&) = default;
63
64 ShaderArchive::~ShaderArchive() = default;
65
66 bool ShaderArchive::IsValid() const {
67 return is_valid_;
68 }
69
70 size_t ShaderArchive::GetShaderCount() const {
71 return shaders_.size();
72 }
73
74 std::shared_ptr<fml::Mapping> ShaderArchive::GetMapping(
75 ArchiveShaderType type,
76 std::string name) const {
77 ShaderKey key;
78 key.type = type;
79 key.name = std::move(name);
80 auto found = shaders_.find(key);
81 return found == shaders_.end() ? nullptr : found->second;
82 }
83
84 size_t ShaderArchive::IterateAllShaders(
85 const std::function<bool(ArchiveShaderType type,
86 const std::string& name,
87 const std::shared_ptr<fml::Mapping>& mapping)>&
88 callback) const {
89 if (!IsValid() || !callback) {
90 return 0u;
91 }
92 size_t count = 0u;
93 for (const auto& shader : shaders_) {
94 count++;
95 if (!callback(shader.first.type, shader.first.name, shader.second)) {
96 break;
97 }
98 }
99 return count;
100 }
101
102 } // namespace impeller
type
GLenum type
Definition: blit_command_gles.cc:153
impeller::ShaderArchive
Definition: shader_archive.h:18
impeller::ShaderArchive::~ShaderArchive
~ShaderArchive()
impeller::ShaderArchive::IterateAllShaders
size_t IterateAllShaders(const std::function< bool(ArchiveShaderType type, const std::string &name, const std::shared_ptr< fml::Mapping > &mapping)> &) const
Definition: shader_archive.cc:84
impeller::ShaderArchive::IsValid
bool IsValid() const
Definition: shader_archive.cc:66
impeller::ShaderArchive::ShaderArchive
ShaderArchive(std::shared_ptr< fml::Mapping > payload)
Definition: shader_archive.cc:28
impeller::ShaderArchive::GetShaderCount
size_t GetShaderCount() const
Definition: shader_archive.cc:70
impeller::ShaderArchive::GetMapping
std::shared_ptr< fml::Mapping > GetMapping(ArchiveShaderType type, std::string name) const
Definition: shader_archive.cc:74
impeller
Definition: allocation.cc:12
impeller::ArchiveShaderType
ArchiveShaderType
Definition: shader_archive_types.h:10
impeller::ArchiveShaderType::kCompute
@ kCompute
impeller::ArchiveShaderType::kFragment
@ kFragment
impeller::ArchiveShaderType::kVertex
@ kVertex
impeller::ToShaderType
constexpr ArchiveShaderType ToShaderType(fb::Stage stage)
Definition: shader_archive.cc:16
std
Definition: comparable.h:95
shader_archive.h
end
const size_t end
Definition: tessellator_libtess.cc:63
validation.h
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:91