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