37#include <monopp/mono_jit.h>
49auto resolve_path(
const std::string& key) -> fs::path
54auto resolve_input_file(
const fs::path& key) -> fs::path
58 if(absolute_path.extension() ==
".meta")
60 absolute_path.replace_extension();
65auto escape_str(
const std::string& str) -> std::string
67 return "\"" + str +
"\"";
70auto run_process(
const std::string& process,
71 const std::vector<std::string>& args_array,
73 std::string& err) ->
bool
76 err = result.out_output;
78 if(!result.err_output.empty())
85 err += result.err_output;
88 if(err.find(
"error") != std::string::npos)
93 return result.retcode == 0;
96void copy_compiled_file(
const fs::path& from,
const fs::path& to,
const std::string& str_input)
107 APPLOG_ERROR(
"Failed compilation of {0} -> {1} with error: {2}", str_input, to.filename().string(), err.message());
112 const fs::path& extension,
117 return gfx::texture_format::Unknown;
122 if(extension ==
".hdr" || extension ==
".exr")
132 return gfx::texture_format::BC6H;
137 if(info.num_hannels == 1)
139 return gfx::texture_format::BC4;
144 if(info.num_hannels == 2)
146 return gfx::texture_format::BC5;
152 if(!info.has_alpha_channel)
158 return gfx::texture_format::BC1;
161 return gfx::texture_format::BC1;
165 return gfx::texture_format::BC1;
170 return gfx::texture_format::BC1;
181 return gfx::texture_format::BC3;
183 return gfx::texture_format::BC3;
187 return gfx::texture_format::BC3;
192 return gfx::texture_format::BC3;
196auto compile_texture_to_file(
const fs::path& input_path,
197 const fs::path& output_path,
199 const std::string& protocol) ->
bool
201 std::string str_input = input_path.string();
202 std::string str_output = output_path.string();
204 bool try_compress = protocol ==
"app";
206 auto quality = importer.
quality;
240 select_compressed_format(gfx::texture_format::RGBA8, input_path.extension(), quality.compression);
242 std::vector<std::string> args_array = {
251 if(try_compress && format != gfx::texture_format::Unknown)
253 args_array.emplace_back(
"-t");
256 if(format == gfx::texture_format::BC7 || format == gfx::texture_format::BC6H)
260 args_array.emplace_back(
"-q");
261 args_array.emplace_back(
"fastest");
265 args_array.emplace_back(
"-q");
266 args_array.emplace_back(
"highest");
272 args_array.emplace_back(
"-m");
275 switch(quality.max_size)
283 args_array.emplace_back(
"--max");
284 args_array.emplace_back(
"32");
289 args_array.emplace_back(
"--max");
290 args_array.emplace_back(
"64");
295 args_array.emplace_back(
"--max");
296 args_array.emplace_back(
"128");
301 args_array.emplace_back(
"--max");
302 args_array.emplace_back(
"256");
307 args_array.emplace_back(
"--max");
308 args_array.emplace_back(
"512");
313 args_array.emplace_back(
"--max");
314 args_array.emplace_back(
"1024");
319 args_array.emplace_back(
"--max");
320 args_array.emplace_back(
"2048");
325 args_array.emplace_back(
"--max");
326 args_array.emplace_back(
"4096");
331 args_array.emplace_back(
"--max");
332 args_array.emplace_back(
"8192");
337 args_array.emplace_back(
"--max");
338 args_array.emplace_back(
"16384");
343 switch(importer.
type)
347 args_array.emplace_back(
"--equirect");
353 args_array.emplace_back(
"--normalmap");
365 std::ofstream output_file(str_output);
372 if(!run_process(texturec.string(), args_array,
false, error))
374 APPLOG_ERROR(
"Failed compilation of {0} with error: {1}", str_input, error);
381auto compile_shader_to_file(
const fs::path& input_path,
382 const fs::path& output_path,
385 std::string str_input = input_path.string();
386 std::string str_output = output_path.string();
388 std::string file = input_path.stem().string();
389 fs::path dir = input_path.parent_path();
392 std::string str_include = include.string();
394 fs::path varying = dir / (file +
".io");
397 if(!fs::exists(varying, err))
399 varying = dir /
"varying.def.io";
401 if(!fs::exists(varying, err))
403 varying = dir /
"varying.def.sc";
406 std::string str_varying = varying.string();
408 std::string str_platform;
409 std::string str_profile;
410 std::string str_type;
411 std::string str_opt =
"3";
413 bool vs = hpp::string_view(file).starts_with(
"vs_");
414 bool fs = hpp::string_view(file).starts_with(
"fs_");
415 bool cs = hpp::string_view(file).starts_with(
"cs_");
417 if(
renderer == gfx::renderer_type::Vulkan)
419 str_platform =
"windows";
420 str_profile =
"spirv";
423 if(
renderer == gfx::renderer_type::Direct3D11 ||
renderer == gfx::renderer_type::Direct3D12)
425 str_platform =
"windows";
429 str_profile =
"s_5_0";
434 str_profile =
"s_5_0";
438 else if(
renderer == gfx::renderer_type::OpenGLES)
440 str_platform =
"android";
441 str_profile =
"100_es";
443 else if(
renderer == gfx::renderer_type::OpenGL)
445 str_platform =
"linux";
452 else if(
renderer == gfx::renderer_type::Metal)
454 str_platform =
"osx";
455 str_profile =
"metal";
461 str_type =
"fragment";
463 str_type =
"compute";
465 str_type =
"unknown";
467 std::vector<std::string> args_array = {
483 if(!str_platform.empty())
485 args_array.emplace_back(
"--platform");
486 args_array.emplace_back(str_platform);
489 if(!str_profile.empty())
491 args_array.emplace_back(
"-p");
492 args_array.emplace_back(str_profile);
497 args_array.emplace_back(
"-O");
498 args_array.emplace_back(str_opt);
505 std::ofstream output_file(str_output);
511 if(!run_process(shaderc.string(), args_array,
true, error))
513 APPLOG_ERROR(
"Failed compilation of {0} -> {1} with error: {2}", str_input, output_path.filename().string(), error);
525 auto absolute_path = resolve_input_file(key);
526 std::string str_input = absolute_path.string();
528 auto extension = output.extension();
535 compile_shader_to_file(
544 APPLOG_ERROR(
"Failed compilation of {0} -> {1} with error: {2}",
545 str_input, output.filename().string(), err.message());
561 meta.
importer = std::make_shared<texture_importer_meta>();
563 meta.
uid = am.add_asset_info_for_path(resolve_input_file(key), meta,
true);
587 auto importer = std::static_pointer_cast<texture_importer_meta>(base_importer);
590 auto absolute_path = resolve_input_file(key);
591 std::string str_input = absolute_path.string();
597 compile_texture_to_file(
607 APPLOG_ERROR(
"Failed compilation of {0} -> {1} with error: {2}",
608 str_input, output.filename().string(), err.message());
618 auto absolute_path = resolve_input_file(key);
620 std::string str_input = absolute_path.string();
647 meta.
importer = std::make_shared<mesh_importer_meta>();
649 meta.
uid = am.add_asset_info_for_path(resolve_input_file(key), meta,
true);
675 auto importer = std::static_pointer_cast<mesh_importer_meta>(base_importer);
677 auto absolute_path = resolve_input_file(key);
679 std::string str_input = absolute_path.string();
683 fs::path file = absolute_path.stem();
684 fs::path dir = absolute_path.parent_path();
687 std::vector<animation_clip> animations;
688 std::vector<importer::imported_material> materials;
689 std::vector<importer::imported_texture> textures;
705 for(
const auto& animation : animations)
707 fs::path anim_output;
708 if(animation.name.empty())
710 anim_output = (dir / file).
string() +
".anim";
714 anim_output = dir / (animation.name +
".anim");
725 for(
const auto&
material : materials)
732 mat_output = (dir / file).
string() +
".mat";
736 mat_output = dir / (
material.name +
".mat");
760 meta.
importer = std::make_shared<animation_importer_meta>();
762 meta.
uid = am.add_asset_info_for_path(resolve_input_file(key), meta,
true);
788 auto importer = std::static_pointer_cast<animation_importer_meta>(base_importer);
790 auto absolute_path = resolve_input_file(key);
792 std::string str_input = absolute_path.string();
818 auto absolute_path = resolve_input_file(key);
819 std::string str_input = absolute_path.string();
821 copy_compiled_file(absolute_path, output, str_input);
829 auto absolute_path = resolve_input_file(key);
830 std::string str_input = absolute_path.string();
832 copy_compiled_file(absolute_path, output, str_input);
840 auto absolute_path = resolve_input_file(key);
841 std::string str_input = absolute_path.string();
843 copy_compiled_file(absolute_path, output, str_input);
851 auto absolute_path = resolve_input_file(key);
853 std::string str_input = absolute_path.string();
857 auto material = std::make_shared<physics_material>();
873 auto absolute_path = resolve_input_file(key);
874 std::string str_input = absolute_path.string();
877 auto tree = std::make_shared<ui_tree>();
880 std::ifstream file(absolute_path);
883 std::stringstream buffer;
884 buffer << file.rdbuf();
885 tree->content = buffer.str();
901 auto absolute_path = resolve_input_file(key);
902 std::string str_input = absolute_path.string();
905 auto sheet = std::make_shared<style_sheet>();
908 std::ifstream file(absolute_path);
911 std::stringstream buffer;
912 buffer << file.rdbuf();
913 sheet->content = buffer.str();
935 meta.
importer = std::make_shared<audio_importer_meta>();
937 meta.
uid = am.add_asset_info_for_path(resolve_input_file(key), meta,
true);
963 auto importer = std::static_pointer_cast<audio_importer_meta>(base_importer);
965 auto absolute_path = resolve_input_file(key);
967 std::string str_input = absolute_path.string();
971 audio::sound_data clip;
976 APPLOG_ERROR(
"Failed compilation of {0} with error: {1}", str_input, error);
980 if(importer->force_to_mono)
982 clip.convert_to_mono();
1005 std::regex warning_regex(R
"((.*)\((\d+),\d+\): error .*)");
1006 std::vector<script_compilation_entry> entries;
1009 auto begin = std::sregex_iterator(log.begin(), log.end(), warning_regex);
1010 auto end = std::sregex_iterator();
1012 for(
auto it = begin; it != end; ++it)
1014 const std::smatch& match = *it;
1015 if(match.size() >= 3)
1018 entry.file = match[1].str();
1019 entry.line = std::stoi(match[2].str());
1020 entry.msg = match[0].str();
1021 entries.emplace_back(std::move(
entry));
1032 std::regex warning_regex(R
"((.*)\((\d+),\d+\): error .*)");
1033 std::vector<script_compilation_entry> entries;
1036 auto begin = std::sregex_iterator(log.begin(), log.end(), warning_regex);
1037 auto end = std::sregex_iterator();
1039 for(
auto it = begin; it != end; ++it)
1041 const std::smatch& match = *it;
1042 if(match.size() >= 3)
1045 entry.file = match[1].str();
1046 entry.line = std::stoi(match[2].str());
1047 entry.msg = match[0].str();
1048 entries.emplace_back(std::move(
entry));
1061 fs::path temp = fs::temp_directory_path(err);
1063 mono::compiler_params params;
1067 if(protocol !=
"engine")
1071 params.references.emplace_back(lib_compiled_key.filename().string());
1073 params.references_locations.emplace_back(lib_compiled_key.parent_path().string());
1076 auto assets = am.get_assets<
script>(protocol);
1077 for(
const auto& asset : assets)
1087 auto temp_xml = temp;
1088 temp_xml.replace_extension(
".xml");
1089 auto output_xml = output;
1090 output_xml.replace_extension(
".xml");
1092 auto temp_mdb = temp;
1093 temp_mdb.concat(
".mdb");
1094 auto output_mdb = output;
1095 output_mdb.concat(
".mdb");
1097 std::string str_output = temp.string();
1099 params.output_name = str_output;
1100 params.output_doc_name = temp_xml.string();
1101 if(params.files.empty())
1103 fs::remove(output, err);
1104 fs::remove(output_mdb, err);
1106 if(protocol ==
"engine")
1117 auto cmd = mono::create_compile_command_detailed(params);
1121 fs::remove(temp, err);
1122 fs::remove(temp_mdb, err);
1123 fs::remove(temp_xml, err);
1125 if(!run_process(cmd.cmd, cmd.args,
true, error))
1129 if(!parsed_errors.empty())
1131 for(
const auto& error : parsed_errors)
1138 APPLOG_ERROR(
"Failed compilation of {0} with error: {1}", output.string(), error);
1146 fs::remove(output_mdb, err);
1149 fs::create_directories(output.parent_path(), err);
1151 if(protocol !=
"engine")
1155 for(
const auto& warning : parsed_warnings)
1179 auto absolute_path = resolve_input_file(key);
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 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 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
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