| Flutter Impeller |

Classes | Public Member Functions | List of all members
impeller::ShaderArchiveWriter Class Reference

`#include <shader_archive_writer.h>`

## Public Member Functions
| ShaderArchiveWriter ()
| ~ShaderArchiveWriter ()
bool | AddShaderAtPath (const std::string &path)
bool | AddShader (ArchiveShaderType type, std::string name, std::shared_ptr< fml::Mapping > mapping)
std::shared_ptr< fml::Mapping > | CreateMapping () const

## Detailed Description

Definition at line 18 of file shader_archive_writer.h.

## Constructor & Destructor Documentation

## ◆ ShaderArchiveWriter()

impeller::ShaderArchiveWriter::ShaderArchiveWriter | ( |  | ) |
| default

## ◆ ~ShaderArchiveWriter()

impeller::ShaderArchiveWriter::~ShaderArchiveWriter | ( |  | ) |
| default

## Member Function Documentation

## ◆ AddShader()

bool impeller::ShaderArchiveWriter::AddShader | ( | ArchiveShaderType | type,
|  | std::string | name,
|  | std::shared_ptr< fml::Mapping > | mapping
| ) |  |

Definition at line 74 of file shader_archive_writer.cc.

76 {
77 if (name.empty() || !mapping || mapping->GetMapping() == nullptr) {
78 return false;
79 }
80
81 shader_descriptions_.emplace_back(
82 ShaderDescription{type, std::move(name), std::move(mapping)});
83 return true;
84 }

Referenced by AddShaderAtPath(), and impeller::testing::TEST().

## ◆ AddShaderAtPath()

bool impeller::ShaderArchiveWriter::AddShaderAtPath | ( | const std::string & | path | ) |

Definition at line 31 of file shader_archive_writer.cc.

31 {
32 std::filesystem::path path(std_path);
33
34 if (path.stem().empty()) {
35 FML_LOG(ERROR) << "File path stem was empty for " << path;
36 return false;
37 }
38
39 if (path.extension() != ".gles" && path.extension() != ".vkspv") {
40 FML_LOG(ERROR) << "File path doesn't have a known shader extension "
41 << path;
42 return false;
43 }
44
45 // Get rid of .gles
46 path = path.replace_extension();
47
48 auto shader_type = InferShaderTypefromFileExtension(path.extension());
49
50 if (!shader_type.has_value()) {
51 FML_LOG(ERROR) << "Could not infer shader type from file extension: "
52 << path.extension().string();
53 return false;
54 }
55
56 // Get rid of the shader type extension (.vert, .frag, etc..).
57 path = path.replace_extension();
58
59 const auto shader_name = path.stem().string();
60 if (shader_name.empty()) {
61 FML_LOG(ERROR) << "Shader name was empty.";
62 return false;
63 }
64
65 auto file_mapping = fml::FileMapping::CreateReadOnly(std_path);
66 if (!file_mapping) {
67 FML_LOG(ERROR) << "File doesn't exist at path: " << path;
68 return false;
69 }
70
71 return AddShader(shader_type.value(), shader_name, std::move(file_mapping));
72 }

References AddShader(), and impeller::InferShaderTypefromFileExtension().

Referenced by impeller::Main().

## ◆ CreateMapping()

std::shared_ptr< fml::Mapping > impeller::ShaderArchiveWriter::CreateMapping | ( |  | ) | const

Definition at line 98 of file shader_archive_writer.cc.

98 {
99 fb::ShaderArchiveT shader_archive;
100 for (const auto& shader_description : shader_descriptions_) {
101 auto mapping = shader_description.mapping;
102 if (!mapping) {
103 return nullptr;
104 }
105 auto desc = std::make_unique<fb::ShaderBlobT>();
106 desc->name = shader_description.name;
107 desc->stage = ToStage(shader_description.type);
108 desc->mapping = {mapping->GetMapping(),
109 mapping->GetMapping() + mapping->GetSize()};
110 shader_archive.items.emplace_back(std::move(desc));
111 }
112 auto builder = std::make_shared<flatbuffers::FlatBufferBuilder>();
113 builder->Finish(fb::ShaderArchive::Pack(*builder.get(), &shader_archive),
114 fb::ShaderArchiveIdentifier());
115 return std::make_shared<fml::NonOwnedMapping>(builder->GetBufferPointer(),
116 builder->GetSize(),
117 [builder](auto, auto) {});
118 }

References impeller::ToStage().

Referenced by impeller::Main(), and impeller::testing::TEST().

The documentation for this class was generated from the following files:

- impeller/shader_archive/shader_archive_writer.h
- impeller/shader_archive/shader_archive_writer.cc

impeller::ToStage
constexpr fb::Stage ToStage(ArchiveShaderType type)
Definition: shader_archive_writer.cc:86
impeller::InferShaderTypefromFileExtension
std::optional< ArchiveShaderType > InferShaderTypefromFileExtension(const std::filesystem::path &path)
Definition: shader_archive_writer.cc:19
impeller::ShaderArchiveWriter::AddShader
bool AddShader(ArchiveShaderType type, std::string name, std::shared_ptr< fml::Mapping > mapping)
Definition: shader_archive_writer.cc:74