38#include <monopp/mono_jit.h>
50auto resolve_path(
const std::string& key) -> fs::path
55auto resolve_input_file(
const fs::path& key) -> fs::path
59 if(absolute_path.extension() ==
".meta")
61 absolute_path.replace_extension();
66auto escape_str(
const std::string& str) -> std::string
68 return "\"" + str +
"\"";
71auto run_process(
const std::string& process,
72 const std::vector<std::string>& args_array,
74 std::string& err) ->
bool
77 err = result.out_output;
79 if(!result.err_output.empty())
86 err += result.err_output;
89 if(err.find(
"error") != std::string::npos)
94 return result.retcode == 0;
97void copy_compiled_file(
const fs::path& from,
const fs::path& to,
const std::string& str_input)
108 APPLOG_ERROR(
"Failed compilation of {0} -> {1} with error: {2}", str_input, to.filename().string(), err.message());
113 const fs::path& extension,
118 return gfx::texture_format::Unknown;
123 if(extension ==
".hdr" || extension ==
".exr")
133 return gfx::texture_format::BC6H;
138 if(info.num_hannels == 1)
140 return gfx::texture_format::BC4;
145 if(info.num_hannels == 2)
147 return gfx::texture_format::BC5;
153 if(!info.has_alpha_channel)
159 return gfx::texture_format::BC1;
162 return gfx::texture_format::BC1;
166 return gfx::texture_format::BC1;
171 return gfx::texture_format::BC1;
182 return gfx::texture_format::BC3;
184 return gfx::texture_format::BC3;
188 return gfx::texture_format::BC3;
193 return gfx::texture_format::BC3;
197auto compile_texture_to_file(
const fs::path& input_path,
198 const fs::path& output_path,
200 const std::string& protocol) ->
bool
202 std::string str_input = input_path.string();
203 std::string str_output = output_path.string();
205 bool try_compress = protocol ==
"app";
207 auto quality = importer.
quality;
241 select_compressed_format(gfx::texture_format::RGBA8, input_path.extension(), quality.compression);
243 std::vector<std::string> args_array = {
252 if(try_compress && format != gfx::texture_format::Unknown)
254 args_array.emplace_back(
"-t");
257 if(format == gfx::texture_format::BC7 || format == gfx::texture_format::BC6H)
261 args_array.emplace_back(
"-q");
262 args_array.emplace_back(
"fastest");
266 args_array.emplace_back(
"-q");
267 args_array.emplace_back(
"highest");
273 args_array.emplace_back(
"-m");
276 switch(quality.max_size)
284 args_array.emplace_back(
"--max");
285 args_array.emplace_back(
"32");
290 args_array.emplace_back(
"--max");
291 args_array.emplace_back(
"64");
296 args_array.emplace_back(
"--max");
297 args_array.emplace_back(
"128");
302 args_array.emplace_back(
"--max");
303 args_array.emplace_back(
"256");
308 args_array.emplace_back(
"--max");
309 args_array.emplace_back(
"512");
314 args_array.emplace_back(
"--max");
315 args_array.emplace_back(
"1024");
320 args_array.emplace_back(
"--max");
321 args_array.emplace_back(
"2048");
326 args_array.emplace_back(
"--max");
327 args_array.emplace_back(
"4096");
332 args_array.emplace_back(
"--max");
333 args_array.emplace_back(
"8192");
338 args_array.emplace_back(
"--max");
339 args_array.emplace_back(
"16384");
344 switch(importer.
type)
348 args_array.emplace_back(
"--equirect");
354 args_array.emplace_back(
"--normalmap");
366 std::ofstream output_file(str_output);
373 if(!run_process(texturec.string(), args_array,
false, error))
375 APPLOG_ERROR(
"Failed compilation of {0} with error: {1}", str_input, error);
382auto compile_shader_to_file(
const fs::path& input_path,
383 const fs::path& output_path,
386 std::string str_input = input_path.string();
387 std::string str_output = output_path.string();
389 std::string file = input_path.stem().string();
390 fs::path dir = input_path.parent_path();
393 std::string str_include = include.string();
395 fs::path varying = dir / (file +
".io");
398 if(!fs::exists(varying, err))
400 varying = dir /
"varying.def.io";
402 if(!fs::exists(varying, err))
404 varying = dir /
"varying.def.sc";
407 std::string str_varying = varying.string();
409 std::string str_platform;
410 std::string str_profile;
411 std::string str_type;
412 std::string str_opt =
"3";
414 bool vs = hpp::string_view(file).starts_with(
"vs_");
415 bool fs = hpp::string_view(file).starts_with(
"fs_");
416 bool cs = hpp::string_view(file).starts_with(
"cs_");
418 if(
renderer == gfx::renderer_type::Vulkan)
420 str_platform =
"windows";
421 str_profile =
"spirv";
424 if(
renderer == gfx::renderer_type::Direct3D11 ||
renderer == gfx::renderer_type::Direct3D12)
426 str_platform =
"windows";
430 str_profile =
"s_5_0";
435 str_profile =
"s_5_0";
439 else if(
renderer == gfx::renderer_type::OpenGLES)
441 str_platform =
"android";
442 str_profile =
"100_es";
444 else if(
renderer == gfx::renderer_type::OpenGL)
446 str_platform =
"linux";
453 else if(
renderer == gfx::renderer_type::Metal)
455 str_platform =
"osx";
456 str_profile =
"metal";
462 str_type =
"fragment";
464 str_type =
"compute";
466 str_type =
"unknown";
468 std::vector<std::string> args_array = {
484 if(!str_platform.empty())
486 args_array.emplace_back(
"--platform");
487 args_array.emplace_back(str_platform);
490 if(!str_profile.empty())
492 args_array.emplace_back(
"-p");
493 args_array.emplace_back(str_profile);
498 args_array.emplace_back(
"-O");
499 args_array.emplace_back(str_opt);
506 std::ofstream output_file(str_output);
512 if(!run_process(shaderc.string(), args_array,
true, error))
514 APPLOG_ERROR(
"Failed compilation of {0} -> {1} with error: {2}", str_input, output_path.filename().string(), error);
521auto write_manifest_file(
const fs::path& input_path,
const fs::path& output_path) ->
bool
535 return !err && success;
543 auto absolute_path = resolve_input_file(key);
544 std::string str_input = absolute_path.string();
546 auto extension = output.extension();
553 compile_shader_to_file(
562 APPLOG_ERROR(
"Failed compilation of {0} -> {1} with error: {2}",
563 str_input, output.filename().string(), err.message());
567 if(!write_manifest_file(absolute_path, output))
569 APPLOG_ERROR(
"Failed to write manifest for compiled shader: {0}", output.string());
585 meta.
importer = std::make_shared<texture_importer_meta>();
587 meta.
uid = am.add_asset_info_for_path(resolve_input_file(key), meta,
true);
611 auto importer = std::static_pointer_cast<texture_importer_meta>(base_importer);
614 auto absolute_path = resolve_input_file(key);
615 std::string str_input = absolute_path.string();
621 compile_texture_to_file(
631 APPLOG_ERROR(
"Failed compilation of {0} -> {1} with error: {2}",
632 str_input, output.filename().string(), err.message());
636 if(!write_manifest_file(absolute_path, output))
638 APPLOG_ERROR(
"Failed to write manifest for compiled texture: {0}", output.string());
648 auto absolute_path = resolve_input_file(key);
650 std::string str_input = absolute_path.string();
666 APPLOG_ERROR(
"Failed compilation of {0} -> {1} with error: {2}",
667 str_input, output.filename().string(), err.message());
671 if(!write_manifest_file(absolute_path, output))
673 APPLOG_ERROR(
"Failed to write manifest for compiled material: {0}", output.string());
689 meta.
importer = std::make_shared<mesh_importer_meta>();
691 meta.
uid = am.add_asset_info_for_path(resolve_input_file(key), meta,
true);
717 auto importer = std::static_pointer_cast<mesh_importer_meta>(base_importer);
719 auto absolute_path = resolve_input_file(key);
721 std::string str_input = absolute_path.string();
725 fs::path file = absolute_path.stem();
726 fs::path dir = absolute_path.parent_path();
729 std::vector<animation_clip> animations;
730 std::vector<importer::imported_material> materials;
731 std::vector<importer::imported_texture> textures;
747 for(
const auto& animation : animations)
749 fs::path anim_output;
750 if(animation.name.empty())
752 anim_output = (dir / file).
string() +
".anim";
756 anim_output = dir / (animation.name +
".anim");
767 for(
const auto&
material : materials)
774 mat_output = (dir / file).
string() +
".mat";
778 mat_output = dir / (
material.name +
".mat");
792 APPLOG_ERROR(
"Failed compilation of {0} -> {1} with error: {2}",
793 str_input, output.filename().string(), err.message());
797 if(!write_manifest_file(absolute_path, output))
799 APPLOG_ERROR(
"Failed to write manifest for compiled mesh: {0}", output.string());
815 meta.
importer = std::make_shared<animation_importer_meta>();
817 meta.
uid = am.add_asset_info_for_path(resolve_input_file(key), meta,
true);
843 auto importer = std::static_pointer_cast<animation_importer_meta>(base_importer);
845 auto absolute_path = resolve_input_file(key);
847 std::string str_input = absolute_path.string();
869 APPLOG_ERROR(
"Failed compilation of {0} -> {1} with error: {2}",
870 str_input, output.filename().string(), err.message());
874 if(!write_manifest_file(absolute_path, output))
876 APPLOG_ERROR(
"Failed to write manifest for compiled animation: {0}", output.string());
886 auto absolute_path = resolve_input_file(key);
887 std::string str_input = absolute_path.string();
889 copy_compiled_file(absolute_path, output, str_input);
891 if(!write_manifest_file(absolute_path, output))
893 APPLOG_ERROR(
"Failed to write manifest for compiled font: {0}", output.string());
903 auto absolute_path = resolve_input_file(key);
904 std::string str_input = absolute_path.string();
906 copy_compiled_file(absolute_path, output, str_input);
908 if(!write_manifest_file(absolute_path, output))
910 APPLOG_ERROR(
"Failed to write manifest for compiled prefab: {0}", output.string());
920 auto absolute_path = resolve_input_file(key);
921 std::string str_input = absolute_path.string();
923 copy_compiled_file(absolute_path, output, str_input);
925 if(!write_manifest_file(absolute_path, output))
927 APPLOG_ERROR(
"Failed to write manifest for compiled scene_prefab: {0}", output.string());
937 auto absolute_path = resolve_input_file(key);
939 std::string str_input = absolute_path.string();
943 auto material = std::make_shared<physics_material>();
955 APPLOG_ERROR(
"Failed compilation of {0} -> {1} with error: {2}",
956 str_input, output.filename().string(), err.message());
960 if(!write_manifest_file(absolute_path, output))
962 APPLOG_ERROR(
"Failed to write manifest for compiled physics_material: {0}", output.string());
972 auto absolute_path = resolve_input_file(key);
973 std::string str_input = absolute_path.string();
976 auto tree = std::make_shared<ui_tree>();
979 std::ifstream file(absolute_path);
982 std::stringstream buffer;
983 buffer << file.rdbuf();
984 tree->content = buffer.str();
996 APPLOG_ERROR(
"Failed compilation of {0} -> {1} with error: {2}",
997 str_input, output.filename().string(), err.message());
1001 if(!write_manifest_file(absolute_path, output))
1003 APPLOG_ERROR(
"Failed to write manifest for compiled ui_tree: {0}", output.string());
1013 auto absolute_path = resolve_input_file(key);
1014 std::string str_input = absolute_path.string();
1017 auto sheet = std::make_shared<style_sheet>();
1020 std::ifstream file(absolute_path);
1023 std::stringstream buffer;
1024 buffer << file.rdbuf();
1025 sheet->content = buffer.str();
1037 APPLOG_ERROR(
"Failed compilation of {0} -> {1} with error: {2}",
1038 str_input, output.filename().string(), err.message());
1042 if(!write_manifest_file(absolute_path, output))
1044 APPLOG_ERROR(
"Failed to write manifest for compiled style_sheet: {0}", output.string());
1060 meta.
importer = std::make_shared<audio_importer_meta>();
1062 meta.
uid = am.add_asset_info_for_path(resolve_input_file(key), meta,
true);
1088 auto importer = std::static_pointer_cast<audio_importer_meta>(base_importer);
1090 auto absolute_path = resolve_input_file(key);
1092 std::string str_input = absolute_path.string();
1096 audio::sound_data clip;
1101 APPLOG_ERROR(
"Failed compilation of {0} with error: {1}", str_input, error);
1105 if(importer->force_to_mono)
1107 clip.convert_to_mono();
1118 APPLOG_ERROR(
"Failed compilation of {0} -> {1} with error: {2}",
1119 str_input, output.filename().string(), err.message());
1123 if(!write_manifest_file(absolute_path, output))
1125 APPLOG_ERROR(
"Failed to write manifest for compiled audio_clip: {0}", output.string());
1143 std::regex warning_regex(R
"((.*)\((\d+),\d+\): error .*)");
1144 std::vector<script_compilation_entry> entries;
1147 auto begin = std::sregex_iterator(log.begin(), log.end(), warning_regex);
1148 auto end = std::sregex_iterator();
1150 for(
auto it = begin; it != end; ++it)
1152 const std::smatch& match = *it;
1153 if(match.size() >= 3)
1156 entry.file = match[1].str();
1157 entry.line = std::stoi(match[2].str());
1158 entry.msg = match[0].str();
1159 entries.emplace_back(std::move(
entry));
1170 std::regex warning_regex(R
"((.*)\((\d+),\d+\): error .*)");
1171 std::vector<script_compilation_entry> entries;
1174 auto begin = std::sregex_iterator(log.begin(), log.end(), warning_regex);
1175 auto end = std::sregex_iterator();
1177 for(
auto it = begin; it != end; ++it)
1179 const std::smatch& match = *it;
1180 if(match.size() >= 3)
1183 entry.file = match[1].str();
1184 entry.line = std::stoi(match[2].str());
1185 entry.msg = match[0].str();
1186 entries.emplace_back(std::move(
entry));
1199 fs::path temp = fs::temp_directory_path(err);
1201 mono::compiler_params params;
1205 if(protocol !=
"engine")
1209 params.references.emplace_back(lib_compiled_key.filename().string());
1211 params.references_locations.emplace_back(lib_compiled_key.parent_path().string());
1214 auto assets = am.get_assets<
script>(protocol);
1215 for(
const auto& asset : assets)
1225 auto temp_xml = temp;
1226 temp_xml.replace_extension(
".xml");
1227 auto output_xml = output;
1228 output_xml.replace_extension(
".xml");
1230 auto temp_mdb = temp;
1231 temp_mdb.concat(
".mdb");
1232 auto output_mdb = output;
1233 output_mdb.concat(
".mdb");
1235 std::string str_output = temp.string();
1237 params.output_name = str_output;
1238 params.output_doc_name = temp_xml.string();
1239 if(params.files.empty())
1241 fs::remove(output, err);
1242 fs::remove(output_mdb, err);
1244 if(protocol ==
"engine")
1256 auto cmd = mono::create_compile_command_detailed_rsp(params, temp.string() +
".rsp");
1260 fs::remove(temp, err);
1261 fs::remove(temp_mdb, err);
1262 fs::remove(temp_xml, err);
1264 if(!run_process(cmd.cmd, cmd.args,
true, error))
1268 if(!parsed_errors.empty())
1270 for(
const auto& error : parsed_errors)
1277 APPLOG_ERROR(
"Failed compilation of {0} with error: {1}", output.string(), error);
1285 fs::remove(output_mdb, err);
1288 fs::create_directories(output.parent_path(), err);
1290 if(protocol !=
"engine")
1294 for(
const auto& warning : parsed_warnings)
1318 auto absolute_path = resolve_input_file(key);
1325 APPLOG_ERROR(
"Failed compilation of {0} -> {1} with error: {2}",
1326 absolute_path.string(), output.filename().string(), er.message());
1330 if(!write_manifest_file(absolute_path, output))
1332 APPLOG_ERROR(
"Failed to write manifest for compiled script: {0}", output.string());
Manages assets, including loading, unloading, and storage.
Base class for materials used in rendering.
#define APPLOG_ERROR(...)
#define APPLOG_WARNING_LOC(FILE_LOC, LINE_LOC, FUNC_LOC,...)
#define APPLOG_ERROR_LOC(FILE_LOC, LINE_LOC, FUNC_LOC,...)
auto get_data_directory(const std::string &prefix={}) -> std::string
auto get_meta_directory(const std::string &prefix={}) -> std::string
path extract_protocol(const path &_path)
Given the specified path/filename, resolve the final full filename. This will be based on either the ...
path resolve_protocol(const path &_path)
Given the specified path/filename, resolve the final full filename. This will be based on either the ...
path replace(const path &_path, const path &_sequence, const path &_new_sequence)
Replacing any occurences of the specified path sequence with another.
path convert_to_protocol(const path &_path)
Oposite of the resolve_protocol this function tries to convert to protocol path from an absolute one.
auto to_string(texture_format fmt) -> std::string
bgfx::TextureFormat::Enum texture_format
auto get_format_info(texture_format fmt) -> format_details
auto get_renderer_based_on_filename_extension(const std::string &_type) -> renderer_type
auto get_max_blend_transforms() -> uint32_t
bgfx::RendererType::Enum renderer_type
auto call(const std::vector< std::string > &args_array) -> call_result
auto get_manifest_path(const fs::path &compiled_asset_path) -> fs::path
Generate manifest file path from compiled asset path.
auto compile< style_sheet >(asset_manager &am, const fs::path &key, const fs::path &output, uint32_t flags) -> bool
auto compile< script >(asset_manager &am, const fs::path &key, const fs::path &output, uint32_t flags) -> bool
auto compile< material >(asset_manager &am, const fs::path &key, const fs::path &output, uint32_t flags) -> bool
auto read_importer< animation_clip >(asset_manager &am, const fs::path &key) -> std::shared_ptr< asset_importer_meta >
auto compile< gfx::texture >(asset_manager &am, const fs::path &key, const fs::path &output, uint32_t flags) -> bool
auto compile< gfx::shader >(asset_manager &am, const fs::path &key, const fs::path &output, uint32_t flags) -> bool
auto parse_compilation_warnings(const std::string &log) -> std::vector< script_compilation_entry >
auto compile< audio_clip >(asset_manager &am, const fs::path &key, const fs::path &output, uint32_t flags) -> bool
auto compile< font >(asset_manager &am, const fs::path &key, const fs::path &output, uint32_t flags) -> bool
auto save_manifest(const fs::path &manifest_path, const asset_manifest &manifest) -> bool
Save manifest to file.
auto read_importer< audio_clip >(asset_manager &am, const fs::path &key) -> std::shared_ptr< asset_importer_meta >
auto compile< prefab >(asset_manager &am, const fs::path &key, const fs::path &output, uint32_t flags) -> bool
auto parse_compilation_errors(const std::string &log) -> std::vector< script_compilation_entry >
auto compile< animation_clip >(asset_manager &am, const fs::path &key, const fs::path &output, uint32_t flags) -> bool
auto read_importer< mesh >(asset_manager &am, const fs::path &key) -> std::shared_ptr< asset_importer_meta >
auto read_importer< gfx::texture >(asset_manager &am, const fs::path &key) -> std::shared_ptr< asset_importer_meta >
auto compile< ui_tree >(asset_manager &am, const fs::path &key, const fs::path &output, uint32_t flags) -> bool
auto compile< physics_material >(asset_manager &am, const fs::path &key, const fs::path &output, uint32_t flags) -> bool
auto compile< mesh >(asset_manager &am, const fs::path &key, const fs::path &output, uint32_t flags) -> bool
auto compile< script_library >(asset_manager &am, const fs::path &key, const fs::path &output, uint32_t flags) -> bool
auto compile< scene_prefab >(asset_manager &am, const fs::path &key, const fs::path &output, uint32_t flags) -> bool
auto atomic_copy_file(const fs::path &src, const fs::path &dst, fs::error_code &ec) noexcept -> bool
void atomic_write_file(const fs::path &dst, const std::function< void(const fs::path &)> &callback, fs::error_code &ec) noexcept
auto load_mesh_data_from_file(asset_manager &am, const fs::path &path, const mesh_importer_meta &import_meta, mesh::load_data &load_data, std::vector< animation_clip > &animations, std::vector< imported_material > &materials, std::vector< imported_texture > &textures) -> bool
void save_to_file_bin(const std::string &absolute_path, const animation_clip &obj)
void load_from_file(const std::string &absolute_path, animation_clip &obj)
void save_to_file(const std::string &absolute_path, const animation_clip &obj)
Struct representing an animation.
root_motion_params root_motion
Manifest data for compiled assets.
void compute_source_sha()
static auto context() -> rtti::context &
Struct used for mesh construction.
std::vector< uint8_t > vertex_data
Total number of vertices.
static auto get_lib_name(const std::string &protocol) -> std::string
static auto get_lib_compiled_key(const std::string &protocol) -> std::string
static void copy_compiled_lib(const fs::path &from, const fs::path &to)
texture_importer_meta::compression_quality default_compression
texture_importer_meta::texture_size default_max_size
struct unravel::settings::asset_settings::texture_importer_settings texture
struct unravel::settings::asset_settings assets
struct unravel::texture_importer_meta::quality_meta quality