13#include "impeller/shader_bundle/shader_bundle_flatbuffers.h"
14#include "third_party/json/include/nlohmann/json.hpp"
20 const std::string& bundle_config_json,
21 std::ostream& error_stream) {
23 if (json.is_discarded() || !json.is_object()) {
24 error_stream <<
"The shader bundle is not a valid JSON object."
30 for (
auto& [shader_name, shader_value] : json.items()) {
31 if (bundle.find(shader_name) != bundle.end()) {
32 error_stream <<
"Duplicate shader \"" << shader_name <<
"\"."
36 if (!shader_value.is_object()) {
37 error_stream <<
"Invalid shader entry \"" << shader_name
38 <<
"\": Entry is not a JSON object." << std::endl;
44 if (!shader_value.contains(
"file")) {
45 error_stream <<
"Invalid shader entry \"" << shader_name
46 <<
"\": Missing required \"file\" field." << std::endl;
51 if (!shader_value.contains(
"type")) {
52 error_stream <<
"Invalid shader entry \"" << shader_name
53 <<
"\": Missing required \"type\" field." << std::endl;
58 error_stream <<
"Invalid shader entry \"" << shader_name
59 <<
"\": Shader type " << shader_value[
"type"]
60 <<
" is unknown." << std::endl;
64 shader.
language = shader_value.contains(
"language")
68 error_stream <<
"Invalid shader entry \"" << shader_name
69 <<
"\": Unknown language type " << shader_value[
"language"]
74 shader.
entry_point = shader_value.contains(
"entry_point")
75 ? shader_value[
"entry_point"]
78 bundle[shader_name] = shader;
84static std::unique_ptr<fb::shaderbundle::BackendShaderT>
87 const std::string& shader_name,
89 auto result = std::make_unique<fb::shaderbundle::BackendShaderT>();
91 std::shared_ptr<fml::FileMapping> source_file_mapping =
93 if (!source_file_mapping) {
94 std::cerr <<
"Could not open file for bundled shader \"" << shader_name
95 <<
"\"." << std::endl;
100 options.target_platform = target_platform;
101 options.file_name = shader_name;
115 std::cerr <<
"Compilation failed for bundled shader \"" << shader_name
116 <<
"\"." << std::endl;
117 std::cerr <<
compiler.GetErrorMessages() << std::endl;
121 auto reflector =
compiler.GetReflector();
122 if (reflector ==
nullptr) {
123 std::cerr <<
"Could not create reflector for bundled shader \""
124 << shader_name <<
"\"." << std::endl;
128 auto bundle_data = reflector->GetShaderBundleData();
130 std::cerr <<
"Bundled shader information was nil for \"" << shader_name
131 <<
"\"." << std::endl;
135 result = bundle_data->CreateFlatbuffer();
137 std::cerr <<
"Failed to create flatbuffer for bundled shader \""
138 << shader_name <<
"\"." << std::endl;
147 const std::string& shader_name,
149 auto result = std::make_unique<fb::shaderbundle::ShaderT>();
150 result->name = shader_name;
158 if (!
result->metal_desktop) {
168 if (!
result->opengl_desktop) {
172 shader_name, shader_config);
180 const std::string& bundle_config_json,
186 std::optional<ShaderBundleConfig> bundle_config =
188 if (!bundle_config) {
196 fb::shaderbundle::ShaderBundleT shader_bundle;
198 for (
const auto& [shader_name, shader_config] : bundle_config.value()) {
199 std::unique_ptr<fb::shaderbundle::ShaderT> shader =
204 shader_bundle.shaders.push_back(std::move(shader));
207 return shader_bundle;
217 if (!shader_bundle.has_value()) {
227 auto builder = std::make_shared<flatbuffers::FlatBufferBuilder>();
229 &shader_bundle.value()),
230 fb::shaderbundle::ShaderBundleIdentifier());
231 auto mapping = std::make_shared<fml::NonOwnedMapping>(
235 auto sl_file_name = std::filesystem::absolute(
236 std::filesystem::current_path() / switches.
sl_file_name);
242 std::cerr <<
"Could not write file to " << switches.
sl_file_name
static std::unique_ptr< FileMapping > CreateReadOnly(const std::string &path)
std::string shader_bundle
std::shared_ptr< fml::UniqueFD > working_directory
SourceOptions CreateSourceOptions(std::optional< TargetPlatform > target_platform=std::nullopt) const
static void * Pack(const T &ctx, SkArenaAlloc *alloc)
bool WriteAtomically(const fml::UniqueFD &base_directory, const char *file_name, const Mapping &mapping)
static std::unique_ptr< fb::shaderbundle::ShaderT > GenerateShaderFB(SourceOptions options, const std::string &shader_name, const ShaderConfig &shader_config)
bool SetPermissiveAccess(const std::filesystem::path &p)
Sets the file access mode of the file at path 'p' to 0644.
static std::unique_ptr< fb::shaderbundle::BackendShaderT > GenerateShaderBackendFB(TargetPlatform target_platform, SourceOptions &options, const std::string &shader_name, const ShaderConfig &shader_config)
std::optional< fb::shaderbundle::ShaderBundleT > GenerateShaderBundleFlatbuffer(const std::string &bundle_config_json, const SourceOptions &options)
Parses the JSON shader bundle configuration and invokes the compiler multiple times to produce a shad...
bool GenerateShaderBundle(Switches &switches)
Parses the JSON shader bundle configuration and invokes the compiler multiple times to produce a shad...
std::unordered_map< std::string, ShaderConfig > ShaderBundleConfig
SourceType SourceTypeFromString(std::string name)
std::optional< ShaderBundleConfig > ParseShaderBundleConfig(const std::string &bundle_config_json, std::ostream &error_stream)
Parse a shader bundle configuration from a given JSON string.
std::string EntryPointFunctionNameFromSourceName(const std::string &file_name, SourceType type, SourceLanguage source_language, const std::string &entry_point_name)
std::string Utf8FromPath(const std::filesystem::path &path)
Converts a native format path to a utf8 string.
SourceLanguage ToSourceLanguage(const std::string &source_language)
def parse(repo_root, recipes_cfg_path)
TargetPlatform target_platform
std::string entry_point_name
A shader config parsed as part of a ShaderBundleConfig.
std::string source_file_name