11 #include "flutter/fml/build_config.h"
12 #include "impeller/shader_archive/shader_archive_flatbuffers.h"
21 prefix_ = std::move(prefix);
25 const std::filesystem::path& path) {
29 if (path ==
".vert") {
31 }
else if (path ==
".frag") {
33 }
else if (path ==
".comp") {
44 std::filesystem::path path(std_path);
46 if (path.stem().empty()) {
47 FML_LOG(ERROR) <<
"File path stem was empty for " << path;
51 if (path.extension() !=
".gles" && path.extension() !=
".vkspv") {
52 FML_LOG(ERROR) <<
"File path doesn't have a known shader extension "
58 path = path.replace_extension();
62 if (!shader_type.has_value()) {
63 FML_LOG(ERROR) <<
"Could not infer shader type from file extension: "
64 << path.extension().string();
69 path = path.replace_extension();
71 const auto shader_name = prefix_ + path.stem().string();
72 if (shader_name.empty()) {
73 FML_LOG(ERROR) <<
"Shader name was empty.";
77 auto file_mapping = fml::FileMapping::CreateReadOnly(std_path);
79 FML_LOG(ERROR) <<
"File doesn't exist at path: " << path;
83 return AddShader(shader_type.value(), shader_name, std::move(file_mapping));
89 std::shared_ptr<fml::Mapping> mapping) {
90 if (name.empty() || !mapping || mapping->GetMapping() ==
nullptr) {
94 shader_descriptions_.emplace_back(
95 ShaderDescription{
type, std::move(name), std::move(mapping)});
102 return fb::Stage::kVertex;
104 return fb::Stage::kFragment;
106 return fb::Stage::kCompute;
112 fb::ShaderArchiveT shader_archive;
113 shader_archive.format_version =
114 static_cast<uint32_t
>(fb::ShaderArchiveFormatVersion::kVersion);
115 for (
const auto& shader_description : shader_descriptions_) {
116 auto mapping = shader_description.mapping;
120 auto desc = std::make_unique<fb::ShaderBlobT>();
121 desc->name = shader_description.name;
122 desc->stage =
ToStage(shader_description.type);
123 desc->mapping = {mapping->GetMapping(),
124 mapping->GetMapping() + mapping->GetSize()};
125 shader_archive.items.emplace_back(std::move(desc));
127 auto builder = std::make_shared<flatbuffers::FlatBufferBuilder>();
128 builder->Finish(fb::ShaderArchive::Pack(*builder.get(), &shader_archive),
129 fb::ShaderArchiveIdentifier());
130 return std::make_shared<fml::NonOwnedMapping>(builder->GetBufferPointer(),
132 [builder](
auto,
auto) {});
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
void SetEntryPointPrefix(std::string prefix)
std::optional< ArchiveShaderType > InferShaderTypefromFileExtension(const std::filesystem::path &path)
constexpr fb::Stage ToStage(ArchiveShaderType type)