Cache transcoded textures

This commit is contained in:
Guillaume Meunier
2025-10-25 10:30:29 +02:00
parent 7d76367d2c
commit a9ad5195b7
13 changed files with 421 additions and 59 deletions
+4 -1
View File
@@ -238,8 +238,11 @@ FetchContent_Declare(spdlog EXCLUDE_FROM_ALL SYSTEM URL https://github.co
URL_HASH SHA256=15a04e69c222eb6c01094b5c7ff8a249b36bb22788d72519646fb85feb267e67) URL_HASH SHA256=15a04e69c222eb6c01094b5c7ff8a249b36bb22788d72519646fb85feb267e67)
FetchContent_Declare(stb EXCLUDE_FROM_ALL SYSTEM URL https://github.com/nothings/stb/archive/013ac3beddff3dbffafd5177e7972067cd2b5083.zip FetchContent_Declare(stb EXCLUDE_FROM_ALL SYSTEM URL https://github.com/nothings/stb/archive/013ac3beddff3dbffafd5177e7972067cd2b5083.zip
URL_HASH SHA256=b7f476902bbef1b30f8ecc2d9d95c459c32302c8b559d09b589b5955463b7af8) URL_HASH SHA256=b7f476902bbef1b30f8ecc2d9d95c459c32302c8b559d09b589b5955463b7af8)
file(GLOB LIBKTX_PATCHES CONFIGURE_DEPENDS patches/libktx/*)
FetchContent_Declare(libktx EXCLUDE_FROM_ALL SYSTEM URL https://github.com/KhronosGroup/KTX-Software/archive/refs/tags/v4.4.0.tar.gz FetchContent_Declare(libktx EXCLUDE_FROM_ALL SYSTEM URL https://github.com/KhronosGroup/KTX-Software/archive/refs/tags/v4.4.0.tar.gz
URL_HASH SHA256=3585d76edcdcbe3a671479686f8c81c1c10339f419e4b02a9a6f19cc6e4e0612) URL_HASH SHA256=3585d76edcdcbe3a671479686f8c81c1c10339f419e4b02a9a6f19cc6e4e0612
PATCH_COMMAND ${CMAKE_CURRENT_LIST_DIR}/patches/apply.sh ${LIBKTX_PATCHES}
)
FetchContent_Declare(implot EXCLUDE_FROM_ALL SYSTEM URL https://github.com/epezent/implot/archive/3da8bd34299965d3b0ab124df743fe3e076fa222.zip FetchContent_Declare(implot EXCLUDE_FROM_ALL SYSTEM URL https://github.com/epezent/implot/archive/3da8bd34299965d3b0ab124df743fe3e076fa222.zip
URL_HASH SHA256=e9b439a94aa6f09fcf12ed9c2cd784efe5e99e96242122aa683e690b29f7a59f) URL_HASH SHA256=e9b439a94aa6f09fcf12ed9c2cd784efe5e99e96242122aa683e690b29f7a59f)
file(GLOB IMGUI_PATCHES CONFIGURE_DEPENDS patches/imgui/*) file(GLOB IMGUI_PATCHES CONFIGURE_DEPENDS patches/imgui/*)
+1 -1
View File
@@ -105,7 +105,7 @@ target_link_libraries(wivrn
FreetypeHarfbuzz FreetypeHarfbuzz
glm::glm glm::glm
imspinner imspinner
ktx_read ktx
simdjson simdjson
spdlog::spdlog spdlog::spdlog
spirv-reflect-static spirv-reflect-static
+17 -6
View File
@@ -597,7 +597,7 @@ loaded_image image_loader::do_load_raw(const void * pixels, vk::Extent3D extent,
}; };
} }
loaded_image image_loader::do_load_ktx(std::span<const std::byte> bytes, bool srgb, const std::string & name) loaded_image image_loader::do_load_ktx(std::span<const std::byte> bytes, bool srgb, const std::string & name, const std::filesystem::path & output_file)
{ {
KTXTexture2 texture{}; KTXTexture2 texture{};
// KTX_TEXTURE_CREATE_CHECK_GLTF_BASISU_BIT checks that the file is compatible with the KHR_texture_basisu glTF extension // KTX_TEXTURE_CREATE_CHECK_GLTF_BASISU_BIT checks that the file is compatible with the KHR_texture_basisu glTF extension
@@ -615,14 +615,25 @@ loaded_image image_loader::do_load_ktx(std::span<const std::byte> bytes, bool sr
{ {
auto format = srgb ? supported_srgb_formats.front() : supported_linear_formats.front(); auto format = srgb ? supported_srgb_formats.front() : supported_linear_formats.front();
auto t0 = std::chrono::steady_clock::now();
if (ktxTexture2_TranscodeBasis(texture, format.second, 0) != KTX_SUCCESS) if (ktxTexture2_TranscodeBasis(texture, format.second, 0) != KTX_SUCCESS)
{ {
spdlog::warn("ktxTexture2_TranscodeBasis: error {}", ktxErrorString(err)); spdlog::warn("ktxTexture2_TranscodeBasis: error {}", ktxErrorString(err));
throw std::runtime_error("ktxTexture2_TranscodeBasis"); throw std::runtime_error("ktxTexture2_TranscodeBasis");
} }
auto dt = std::chrono::steady_clock::now() - t0;
spdlog::debug("{}: transcoded to {} in {}", name, vk::to_string(format.first), std::chrono::duration_cast<std::chrono::microseconds>(dt)); if (output_file != "")
{
spdlog::debug("Saving transcoded texture to {}", output_file.native());
std::string writer = "WiVRn";
ktxHashList_DeleteKVPair(&texture->kvDataHead, KTX_WRITER_KEY);
ktxHashList_AddKVPair(&texture->kvDataHead, KTX_WRITER_KEY, (ktx_uint32_t)writer.length() + 1, writer.c_str());
err = ktxTexture2_WriteToNamedFile(texture, output_file.c_str());
if (err != KTX_SUCCESS)
{
spdlog::warn("ktxTexture2_WriteToNamedFile: error {}", ktxErrorString(err));
}
}
} }
ktxVulkanTexture vk_texture; ktxVulkanTexture vk_texture;
@@ -711,13 +722,13 @@ loaded_image image_loader::do_load_image(std::span<const std::byte> bytes, bool
} }
// Load a PNG/JPEG/KTX2 file // Load a PNG/JPEG/KTX2 file
loaded_image image_loader::load(std::span<const std::byte> bytes, bool srgb, const std::string & name, bool premultiply) loaded_image image_loader::load(std::span<const std::byte> bytes, bool srgb, const std::string & name, bool premultiply, const std::filesystem::path & output_file)
{ {
const uint8_t ktx1_magic[] = {0xAB, 0x4B, 0x54, 0x58, 0x20, 0x31, 0x31, 0xBB, 0x0D, 0x0A, 0x1A, 0x0A}; const uint8_t ktx1_magic[] = {0xAB, 0x4B, 0x54, 0x58, 0x20, 0x31, 0x31, 0xBB, 0x0D, 0x0A, 0x1A, 0x0A};
const uint8_t ktx2_magic[] = {0xAB, 0x4B, 0x54, 0x58, 0x20, 0x32, 0x30, 0xBB, 0x0D, 0x0A, 0x1A, 0x0A}; const uint8_t ktx2_magic[] = {0xAB, 0x4B, 0x54, 0x58, 0x20, 0x32, 0x30, 0xBB, 0x0D, 0x0A, 0x1A, 0x0A};
if (starts_with(bytes, ktx1_magic) || starts_with(bytes, ktx2_magic)) if (starts_with(bytes, ktx1_magic) || starts_with(bytes, ktx2_magic))
return do_load_ktx(bytes, srgb, name); return do_load_ktx(bytes, srgb, name, output_file);
else else
return do_load_image(bytes, srgb, name, premultiply); return do_load_image(bytes, srgb, name, premultiply);
} }
+3 -2
View File
@@ -23,6 +23,7 @@
#include "vk/allocation.h" #include "vk/allocation.h"
#include "vk/fwd.h" #include "vk/fwd.h"
#include <cstddef> #include <cstddef>
#include <filesystem>
#include <glm/vec2.hpp> #include <glm/vec2.hpp>
#include <glm/vec3.hpp> #include <glm/vec3.hpp>
#include <glm/vec4.hpp> #include <glm/vec4.hpp>
@@ -52,7 +53,7 @@ struct image_loader
~image_loader(); ~image_loader();
// Load a PNG/JPEG/KTX2 file // Load a PNG/JPEG/KTX2 file
loaded_image load(std::span<const std::byte> bytes, bool srgb, const std::string & name = "", bool premultiply = false); loaded_image load(std::span<const std::byte> bytes, bool srgb, const std::string & name = "", bool premultiply = false, const std::filesystem::path & output_file = "");
// Load raw pixel data // Load raw pixel data
loaded_image load(const void * pixels, size_t size, vk::Extent3D extent, vk::Format format, const std::string & name = "", bool premultiply = false); loaded_image load(const void * pixels, size_t size, vk::Extent3D extent, vk::Format format, const std::string & name = "", bool premultiply = false);
@@ -94,5 +95,5 @@ private:
loaded_image do_load_raw(const void * pixels, vk::Extent3D extent, vk::Format format, const std::string & name, bool premultiply); loaded_image do_load_raw(const void * pixels, vk::Extent3D extent, vk::Format format, const std::string & name, bool premultiply);
loaded_image do_load_image(std::span<const std::byte> bytes, bool srgb, const std::string & name, bool premultiply); loaded_image do_load_image(std::span<const std::byte> bytes, bool srgb, const std::string & name, bool premultiply);
loaded_image do_load_ktx(std::span<const std::byte> bytes, bool srgb, const std::string & name); loaded_image do_load_ktx(std::span<const std::byte> bytes, bool srgb, const std::string & name, const std::filesystem::path & output_file = "");
}; };
+200 -21
View File
@@ -22,6 +22,8 @@
#include "image_loader.h" #include "image_loader.h"
#include "render/scene_components.h" #include "render/scene_components.h"
#include "render/vertex_layout.h" #include "render/vertex_layout.h"
#include "utils/files.h"
#include "utils/json_string.h"
#include "utils/mapped_file.h" #include "utils/mapped_file.h"
#include "utils/ranges.h" #include "utils/ranges.h"
#include "vk/shader.h" #include "vk/shader.h"
@@ -36,10 +38,13 @@
#include <fastgltf/tools.hpp> #include <fastgltf/tools.hpp>
#include <fastgltf/types.hpp> #include <fastgltf/types.hpp>
#include <fastgltf/util.hpp> #include <fastgltf/util.hpp>
#include <filesystem>
#include <fstream>
#include <glm/ext.hpp> #include <glm/ext.hpp>
#include <glm/fwd.hpp> #include <glm/fwd.hpp>
#include <limits> #include <limits>
#include <ranges> #include <ranges>
#include <simdjson/dom.h>
#include <spdlog/spdlog.h> #include <spdlog/spdlog.h>
#include <string> #include <string>
#include <vulkan/vulkan_format_traits.hpp> #include <vulkan/vulkan_format_traits.hpp>
@@ -224,7 +229,7 @@ std::vector<glm::dvec4> read_vertex_attribute_aux(const fastgltf::Asset & asset,
case 2: return read_vertex_attribute_aux2<glm::vec<2, T>>(asset, accessor); case 2: return read_vertex_attribute_aux2<glm::vec<2, T>>(asset, accessor);
case 3: return read_vertex_attribute_aux2<glm::vec<3, T>>(asset, accessor); case 3: return read_vertex_attribute_aux2<glm::vec<3, T>>(asset, accessor);
case 4: return read_vertex_attribute_aux2<glm::vec<4, T>>(asset, accessor); case 4: return read_vertex_attribute_aux2<glm::vec<4, T>>(asset, accessor);
//clang-format on // clang-format on
} }
return {}; return {};
} }
@@ -233,7 +238,7 @@ std::vector<glm::dvec4> read_vertex_attribute(const fastgltf::Asset & asset, con
{ {
switch (accessor.componentType) switch (accessor.componentType)
{ {
// clang-format off // clang-format off
case fastgltf::ComponentType::Byte: return read_vertex_attribute_aux<int8_t>(asset, accessor); case fastgltf::ComponentType::Byte: return read_vertex_attribute_aux<int8_t>(asset, accessor);
case fastgltf::ComponentType::UnsignedByte: return read_vertex_attribute_aux<uint8_t>(asset, accessor); case fastgltf::ComponentType::UnsignedByte: return read_vertex_attribute_aux<uint8_t>(asset, accessor);
case fastgltf::ComponentType::Short: return read_vertex_attribute_aux<int16_t>(asset, accessor); case fastgltf::ComponentType::Short: return read_vertex_attribute_aux<int16_t>(asset, accessor);
@@ -241,7 +246,7 @@ std::vector<glm::dvec4> read_vertex_attribute(const fastgltf::Asset & asset, con
case fastgltf::ComponentType::Int: return read_vertex_attribute_aux<int32_t>(asset, accessor); case fastgltf::ComponentType::Int: return read_vertex_attribute_aux<int32_t>(asset, accessor);
case fastgltf::ComponentType::UnsignedInt: return read_vertex_attribute_aux<uint32_t>(asset, accessor); case fastgltf::ComponentType::UnsignedInt: return read_vertex_attribute_aux<uint32_t>(asset, accessor);
case fastgltf::ComponentType::Float: return read_vertex_attribute_aux<float>(asset, accessor); case fastgltf::ComponentType::Float: return read_vertex_attribute_aux<float>(asset, accessor);
//clang-format on // clang-format on
case fastgltf::ComponentType::Double: // TODO case fastgltf::ComponentType::Double: // TODO
abort(); abort();
break; break;
@@ -402,7 +407,8 @@ loaded_image do_load_image(
vk::raii::CommandPool & cb_pool, vk::raii::CommandPool & cb_pool,
std::span<const std::byte> image_data, std::span<const std::byte> image_data,
bool srgb, bool srgb,
const std::pmr::string & name) const std::pmr::string & name,
const std::filesystem::path & output_path)
{ {
switch (guess_mime_type(image_data)) switch (guess_mime_type(image_data))
{ {
@@ -411,7 +417,7 @@ loaded_image do_load_image(
case fastgltf::MimeType::KTX2: { case fastgltf::MimeType::KTX2: {
// TODO reuse image loader // TODO reuse image loader
image_loader loader(physical_device, device, queue, cb_pool); image_loader loader(physical_device, device, queue, cb_pool);
auto image = loader.load(image_data, srgb, name.c_str()); auto image = loader.load(image_data, srgb, name.c_str(), false /*premultiply*/, output_path);
spdlog::debug("{}: {}x{}, format {}, {} mipmaps, {} bytes", name, image.extent.width, image.extent.height, vk::to_string(image.format), image.num_mipmaps, image.image.size()); spdlog::debug("{}: {}x{}, format {}, {} mipmaps, {} bytes", name, image.extent.width, image.extent.height, vk::to_string(image.format), image.num_mipmaps, image.image.size());
return image; return image;
@@ -572,7 +578,7 @@ public:
} }
std::unordered_map<int, std::shared_ptr<vk::raii::ImageView>> images; std::unordered_map<int, std::shared_ptr<vk::raii::ImageView>> images;
std::shared_ptr<vk::raii::ImageView> load_image(int index, bool srgb) std::shared_ptr<vk::raii::ImageView> load_image(int index, const std::filesystem::path & texture_cache, bool srgb)
{ {
auto it = images.find(index); auto it = images.find(index);
if (it != images.end()) if (it != images.end())
@@ -581,20 +587,42 @@ public:
if (index > gltf.images.size()) if (index > gltf.images.size())
throw std::runtime_error(std::format("Image index {} out of range", index)); throw std::runtime_error(std::format("Image index {} out of range", index));
auto [image_data, mime_type] = visit_source(gltf.images[index].data);
std::pmr::string image_name; std::pmr::string image_name;
if (gltf.images[index].name == "") if (gltf.images[index].name == "")
image_name = std::format("{}(image {})", name, index); image_name = std::format("{}(image {})", name, index);
else else
image_name = std::format("{}({})", name, gltf.images[index].name); image_name = std::format("{}({})", name, gltf.images[index].name);
auto image_ptr = std::make_shared<loaded_image>(do_load_image(physical_device, device, queue, cb_pool, image_data, srgb, image_name)); std::filesystem::path cached_texture;
if (texture_cache != "")
{
cached_texture = texture_cache / (std::to_string(index) + ".ktx");
if (std::filesystem::exists(cached_texture))
{
try
{
utils::mapped_file image_data{cached_texture};
auto image_ptr = std::make_shared<loaded_image>(do_load_image(physical_device, device, queue, cb_pool, image_data, srgb, image_name, ""));
return std::shared_ptr<vk::raii::ImageView>(image_ptr, &image_ptr->image_view);
}
catch (std::exception & e)
{
spdlog::warn("Cannot load cached image {}: {}, deleting it", cached_texture.native(), e.what());
std::error_code ec;
std::filesystem::remove(cached_texture);
}
}
}
auto [image_data, mime_type] = visit_source(gltf.images[index].data);
auto image_ptr = std::make_shared<loaded_image>(do_load_image(physical_device, device, queue, cb_pool, image_data, srgb, image_name, cached_texture));
return std::shared_ptr<vk::raii::ImageView>(image_ptr, &image_ptr->image_view); return std::shared_ptr<vk::raii::ImageView>(image_ptr, &image_ptr->image_view);
} }
std::vector<std::shared_ptr<renderer::texture>> load_all_textures(std::function<void(float)> progress_cb) std::vector<std::shared_ptr<renderer::texture>> load_all_textures(const std::filesystem::path & texture_cache, std::function<void(float)> progress_cb)
{ {
// Determine which texture is sRGB // Determine which texture is sRGB
std::vector<uint8_t> srgb_array; std::vector<uint8_t> srgb_array;
@@ -626,7 +654,7 @@ public:
{ {
try try
{ {
texture_ref.image_view = load_image(*gltf_texture.basisuImageIndex, srgb); texture_ref.image_view = load_image(*gltf_texture.basisuImageIndex, texture_cache, srgb);
loaded_textures++; loaded_textures++;
if (progress_cb) if (progress_cb)
progress_cb((float)loaded_textures / total_textures); progress_cb((float)loaded_textures / total_textures);
@@ -652,7 +680,7 @@ public:
{ {
try try
{ {
texture_ref.image_view = load_image(*gltf_texture.imageIndex, srgb); texture_ref.image_view = load_image(*gltf_texture.imageIndex, texture_cache, srgb);
loaded_textures++; loaded_textures++;
if (progress_cb) if (progress_cb)
progress_cb((float)loaded_textures / total_textures); progress_cb((float)loaded_textures / total_textures);
@@ -1048,7 +1076,23 @@ public:
} // namespace } // namespace
std::shared_ptr<entt::registry> scene_loader::operator()(std::span<const std::byte> data, const std::string & name, const std::filesystem::path & parent_path, std::function<void(float)> progress_cb) scene_loader::scene_loader(vk::raii::Device & device, vk::raii::PhysicalDevice physical_device, thread_safe<vk::raii::Queue> & queue, uint32_t queue_family_index, std::shared_ptr<renderer::material> default_material, std::filesystem::path texture_cache_) :
device(device),
physical_device(physical_device),
queue(queue),
queue_family_index(queue_family_index),
default_material(default_material),
texture_cache(std::move(texture_cache_))
{
clear_texture_cache();
}
std::shared_ptr<entt::registry> scene_loader::operator()(
std::span<const std::byte> data,
const std::string & name,
const std::filesystem::path & parent_path,
const std::filesystem::path & gltf_texture_cache,
std::function<void(float)> progress_cb)
{ {
vk::PhysicalDeviceProperties physical_device_properties = physical_device.getProperties(); vk::PhysicalDeviceProperties physical_device_properties = physical_device.getProperties();
vk::raii::CommandPool cb_pool{device, vk::CommandPoolCreateInfo{ vk::raii::CommandPool cb_pool{device, vk::CommandPoolCreateInfo{
@@ -1074,7 +1118,7 @@ std::shared_ptr<entt::registry> scene_loader::operator()(std::span<const std::by
gpu_buffer staging_buffer(physical_device_properties, asset); gpu_buffer staging_buffer(physical_device_properties, asset);
// Load all textures // Load all textures
auto textures = ctx.load_all_textures(progress_cb); auto textures = ctx.load_all_textures(gltf_texture_cache, progress_cb);
// Load all materials // Load all materials
auto materials = ctx.load_all_materials(textures, staging_buffer, *default_material); auto materials = ctx.load_all_materials(textures, staging_buffer, *default_material);
@@ -1102,15 +1146,150 @@ std::shared_ptr<entt::registry> scene_loader::operator()(std::span<const std::by
return loaded_scene; return loaded_scene;
} }
std::shared_ptr<entt::registry> scene_loader::operator()(const std::filesystem::path & gltf_path, std::function<void(float)> progress_cb) std::shared_ptr<entt::registry> scene_loader::operator()(
const std::filesystem::path & gltf_path,
std::function<void(float)> progress_cb)
{ {
spdlog::debug("Loading {}", gltf_path.native()); utils::mapped_file gltf_file(gltf_path);
std::filesystem::path gltf_texture_cache;
utils::mapped_file asset_file(gltf_path); if (texture_cache != "")
{
// FNV1a hash
static const uint64_t fnv_prime = 0x100000001b3;
static const uint64_t fnv_offset_basis = 0xcbf29ce484222325;
uint64_t hash = fnv_offset_basis;
for (char c: gltf_path.native())
hash = (hash ^ c) * fnv_prime;
auto data_buffer = fastgltf::GltfDataBuffer::FromBytes(asset_file.data(), asset_file.size()); gltf_texture_cache = texture_cache / fmt::format("{:016x}", hash);
if (auto error = data_buffer.error(); error != fastgltf::Error::None)
throw std::system_error((int)error, fastgltf_error_category);
return (*this)(asset_file, gltf_path.filename(), gltf_path.parent_path(), progress_cb); bool create_cache = false;
try
{
if (std::filesystem::exists(gltf_texture_cache))
{
if (not std::filesystem::is_directory(gltf_texture_cache))
{
std::filesystem::remove(gltf_texture_cache);
std::filesystem::create_directories(gltf_texture_cache);
spdlog::debug("\"{}\" is not a directory: creating cache", gltf_texture_cache.native());
create_cache = true;
}
}
else
{
std::filesystem::create_directories(gltf_texture_cache);
spdlog::debug("\"{}\" does not exist: creating cache", gltf_texture_cache.native());
create_cache = true;
}
}
catch (std::exception & e)
{
spdlog::warn("Cannot create texture cache directory {}: {}", gltf_texture_cache.native(), e.what());
gltf_texture_cache = "";
create_cache = false;
}
if (create_cache)
{
std::ofstream json{gltf_texture_cache / "index.json"};
json << "{\"filename\":" << json_string(gltf_path.native());
json << ",\"size\":" << gltf_file.size();
// TODO get a timestamp for assets
if (not gltf_path.native().starts_with("assets://"))
{
auto mtime = std::chrono::file_clock::to_sys(std::filesystem::last_write_time(gltf_path));
auto timestamp = std::chrono::duration_cast<std::chrono::seconds>(mtime.time_since_epoch()).count();
auto size = gltf_file.size();
json << ",\"timestamp\":" << timestamp;
}
json << "}";
}
}
return (*this)(gltf_file, gltf_path.filename(), gltf_path.parent_path(), gltf_texture_cache, progress_cb);
}
void scene_loader::clear_texture_cache()
{
try
{
for (const std::filesystem::directory_entry & entry: std::filesystem::directory_iterator{texture_cache})
{
if (not entry.is_directory())
continue;
std::string saved_filename;
int64_t saved_timestamp = 0;
int64_t saved_size = 0;
try
{
std::string json = utils::read_whole_file<std::string>(entry.path() / "index.json");
simdjson::dom::parser parser;
simdjson::dom::element root = parser.parse(json);
if (auto val = root["filename"]; val.is_string())
saved_filename = val.get_string().value();
if (auto val = root["size"]; val.is_int64())
saved_size = val.get_int64();
if (auto val = root["timestamp"]; val.is_int64())
saved_timestamp = val.get_int64();
}
catch (std::exception & e)
{
// If the index file is not found, remove the cache
spdlog::info("{}: cannot open index.json ({}), removing cache directory", entry.path().native(), e.what());
std::error_code ec;
std::filesystem::remove_all(entry.path(), ec);
continue;
}
int64_t real_size = 0;
try
{
utils::mapped_file file{saved_filename};
real_size = file.size();
}
catch (std::exception & e)
{
spdlog::info("{}: cannot open {} ({}), removing cache directory", entry.path().native(), saved_filename, e.what());
std::error_code ec;
std::filesystem::remove_all(entry.path(), ec);
continue;
}
if (real_size != saved_size)
{
spdlog::info("{}: wrong size for {} (cache: {}, current: {}), removing cache directory", entry.path().native(), saved_filename, saved_size, real_size);
std::error_code ec;
std::filesystem::remove_all(entry.path(), ec);
continue;
}
// TODO get a timestamp for assets
if (not saved_filename.starts_with("assets://"))
{
auto mtime = std::chrono::file_clock::to_sys(std::filesystem::last_write_time(saved_filename));
auto timestamp = std::chrono::duration_cast<std::chrono::seconds>(mtime.time_since_epoch()).count();
if (timestamp != saved_timestamp)
{
spdlog::info("{}: wrong timestamp for {} (cache: {}, current: {}), removing cache directory", entry.path().native(), saved_filename, saved_timestamp, timestamp);
std::error_code ec;
std::filesystem::remove_all(entry.path(), ec);
continue;
}
}
}
}
catch (...)
{
// Ignore errors if the texture cache directory does not exist
}
} }
+21 -9
View File
@@ -33,17 +33,29 @@ class scene_loader
thread_safe<vk::raii::Queue> & queue; thread_safe<vk::raii::Queue> & queue;
uint32_t queue_family_index; uint32_t queue_family_index;
std::shared_ptr<renderer::material> default_material; std::shared_ptr<renderer::material> default_material;
std::filesystem::path texture_cache;
std::shared_ptr<entt::registry> operator()(
std::span<const std::byte> data,
const std::string & name = "",
const std::filesystem::path & parent_path = "",
const std::filesystem::path & gltf_texture_cache = "",
std::function<void(float)> progress_cb = {});
public: public:
scene_loader(vk::raii::Device & device, vk::raii::PhysicalDevice physical_device, thread_safe<vk::raii::Queue> & queue, uint32_t queue_family_index, std::shared_ptr<renderer::material> default_material) : scene_loader(
device(device), vk::raii::Device & device,
physical_device(physical_device), vk::raii::PhysicalDevice physical_device,
queue(queue), thread_safe<vk::raii::Queue> & queue,
queue_family_index(queue_family_index), uint32_t queue_family_index,
default_material(default_material) std::shared_ptr<renderer::material> default_material,
{} std::filesystem::path texture_cache);
scene_loader(const scene_loader &) = default; scene_loader(const scene_loader &) = default;
std::shared_ptr<entt::registry> operator()(const std::filesystem::path & gltf_path, std::function<void(float)> progress_cb = {}); std::shared_ptr<entt::registry> operator()(
std::shared_ptr<entt::registry> operator()(std::span<const std::byte> data, const std::string & name = "", const std::filesystem::path & parent_path = "", std::function<void(float)> progress_cb = {}); const std::filesystem::path & gltf_path,
std::function<void(float)> progress_cb = {});
void clear_texture_cache();
}; };
+5 -10
View File
@@ -98,7 +98,7 @@ scene::scene(key, const meta & current_meta, std::span<const vk::Format> support
{ {
// TODO: make argument order more consistent // TODO: make argument order more consistent
renderer = std::make_shared<scene_renderer>(device, physical_device, queue, commandpool); renderer = std::make_shared<scene_renderer>(device, physical_device, queue, commandpool);
gltf_cache = std::make_shared<gltf_cache_type>(device, physical_device, queue, queue_family_index, renderer->get_default_material()); gltf_cache = std::make_shared<gltf_cache_type>(device, physical_device, queue, queue_family_index, renderer->get_default_material(), application::get_cache_path() / "textures");
image_cache = std::make_shared<image_cache_type>(physical_device, device, queue, commandpool); image_cache = std::make_shared<image_cache_type>(physical_device, device, queue, commandpool);
} }
} }
@@ -563,14 +563,14 @@ std::pair<entt::entity, components::node &> scene::add_gltf(std::shared_ptr<entt
return {root, node}; return {root, node};
} }
std::shared_ptr<entt::registry> scene::load_gltf(const std::filesystem::path & path) std::shared_ptr<entt::registry> scene::load_gltf(const std::filesystem::path & path, std::function<void(float)> progress_cb)
{ {
return gltf_cache->load(path.native(), path); return gltf_cache->load(path, path, progress_cb);
} }
std::shared_ptr<entt::registry> scene::load_gltf(std::span<const std::byte> data) void scene::unload_gltf(const std::filesystem::path & path)
{ {
return gltf_cache->load_uncached(data); gltf_cache->remove(path);
} }
std::pair<entt::entity, components::node &> scene::add_gltf(const std::filesystem::path & path, uint32_t layer_mask) std::pair<entt::entity, components::node &> scene::add_gltf(const std::filesystem::path & path, uint32_t layer_mask)
@@ -578,10 +578,6 @@ std::pair<entt::entity, components::node &> scene::add_gltf(const std::filesyste
return add_gltf(load_gltf(path), layer_mask); return add_gltf(load_gltf(path), layer_mask);
} }
std::pair<entt::entity, components::node &> scene::add_gltf(std::span<const std::byte> data, uint32_t layer_mask)
{
return add_gltf(load_gltf(data), layer_mask);
}
void scene::remove(entt::entity entity) void scene::remove(entt::entity entity)
{ {
@@ -591,7 +587,6 @@ void scene::remove(entt::entity entity)
{ {
entt::entity parent = to_be_removed.back(); entt::entity parent = to_be_removed.back();
to_be_removed.pop_back(); to_be_removed.pop_back();
spdlog::info("Destroying entity {}", (int)parent);
world.destroy(parent); world.destroy(parent);
for (auto [e, node]: world.view<components::node>().each()) for (auto [e, node]: world.view<components::node>().each())
+2 -3
View File
@@ -216,11 +216,10 @@ public:
entt::registry world; entt::registry world;
std::shared_ptr<entt::registry> load_gltf(const std::filesystem::path & path); std::shared_ptr<entt::registry> load_gltf(const std::filesystem::path & path, std::function<void(float)> progress_cb = {});
std::shared_ptr<entt::registry> load_gltf(std::span<const std::byte> data); void unload_gltf(const std::filesystem::path & path);
std::pair<entt::entity, components::node &> add_gltf(std::shared_ptr<entt::registry> gltf, uint32_t layer_mask = -1); std::pair<entt::entity, components::node &> add_gltf(std::shared_ptr<entt::registry> gltf, uint32_t layer_mask = -1);
std::pair<entt::entity, components::node &> add_gltf(const std::filesystem::path & path, uint32_t layer_mask = -1); std::pair<entt::entity, components::node &> add_gltf(const std::filesystem::path & path, uint32_t layer_mask = -1);
std::pair<entt::entity, components::node &> add_gltf(std::span<const std::byte> data, uint32_t layer_mask = -1);
void remove(entt::entity entity); void remove(entt::entity entity);
}; };
+6
View File
@@ -62,5 +62,11 @@ public:
auto _ = entries.lock(); auto _ = entries.lock();
_->clear(); _->clear();
} }
void remove(const Key & key)
{
auto _ = entries.lock();
_->erase(key);
}
}; };
} // namespace utils } // namespace utils
+19 -6
View File
@@ -21,12 +21,13 @@
#include <filesystem> #include <filesystem>
#include <fstream> #include <fstream>
#include <iterator> #include <iterator>
#include <type_traits>
#include <vector> #include <vector>
namespace utils namespace utils
{ {
template <typename T> template <typename T>
static std::vector<T> read_whole_file(std::filesystem::path filename) static std::conditional_t<std::is_same_v<T, std::string>, std::string, std::vector<T>> read_whole_file(std::filesystem::path filename)
{ {
std::ifstream file(filename, std::ios::binary | std::ios::ate); std::ifstream file(filename, std::ios::binary | std::ios::ate);
file.exceptions(std::ios_base::badbit | std::ios_base::failbit); file.exceptions(std::ios_base::badbit | std::ios_base::failbit);
@@ -34,14 +35,26 @@ static std::vector<T> read_whole_file(std::filesystem::path filename)
size_t size = file.tellg(); size_t size = file.tellg();
file.seekg(0); file.seekg(0);
if constexpr (sizeof(T) > 1) if constexpr (std::is_same_v<T, std::string>)
size = size - size % sizeof(T); {
std::string string;
string.resize(size);
std::vector<T> bytes(size / sizeof(T)); file.read(reinterpret_cast<char *>(string.data()), size);
file.read(reinterpret_cast<char *>(bytes.data()), size); return string;
}
else
{
if constexpr (sizeof(T) > 1)
size = size - size % sizeof(T);
return bytes; std::vector<T> bytes(size / sizeof(T));
file.read(reinterpret_cast<char *>(bytes.data()), size);
return bytes;
}
} }
template <typename T> template <typename T>
+70
View File
@@ -0,0 +1,70 @@
/*
* WiVRn VR streaming
* Copyright (C) 2024 Guillaume Meunier <guillaume.meunier@centraliens.net>
* Copyright (C) 2024 Patrick Nicolas <patricknicolas@laposte.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "json_string.h"
std::string json_string(const std::string & in)
{
std::string out;
out.reserve(in.size() + 2);
out += '"';
for (char c: in)
{
switch (c)
{
case '\b':
out += "\\b";
break;
case '\f':
out += "\\f";
break;
case '\n':
out += "\\n";
break;
case '\r':
out += "\\r";
break;
case '\t':
out += "\\t";
break;
case '"':
out += "\\\"";
break;
case '\\':
out += "\\\\";
break;
default:
out += c;
break;
}
}
out += '"';
return out;
}
+24
View File
@@ -0,0 +1,24 @@
/*
* WiVRn VR streaming
* Copyright (C) 2024 Guillaume Meunier <guillaume.meunier@centraliens.net>
* Copyright (C) 2024 Patrick Nicolas <patricknicolas@laposte.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include <string>
std::string json_string(const std::string & in);
@@ -0,0 +1,49 @@
From f3f6b3b6988a9bc13448b720f17ed3543da7600e Mon Sep 17 00:00:00 2001
From: alex <alexge50.x@gmail.com>
Date: Wed, 16 Apr 2025 06:36:45 +0100
Subject: [PATCH] GCC14/C++23 compatibility fix (#1014)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Fixes the following error, when compiling with GCC 14, using
`-std=c++23`:
```
<>/basisu/encoder/basisu_kernels_sse.cpp:41:
<>/basisu/encoder/cppspmd_sse.h: In member function ‘cppspmd_sse41::spmd_kernel::vfloat& cppspmd_sse41::spmd_kernel::store(vfloat&&, const vfloat&)’:
<>/basisu/encoder/cppspmd_sse.h:496:24: error: cannot bind non-const lvalue reference of type ‘cppspmd_sse41::spmd_kernel::vfloat&’ to an rvalue of type ‘cppspmd_sse41::spmd_kernel::vfloat’
496 | return dst;
| ^~~
<>/basisu/encoder/cppspmd_sse.h: In member function ‘cppspmd_sse41::spmd_kernel::vfloat& cppspmd_sse41::spmd_kernel::store_all(vfloat&&, const vfloat&)’:
<>/basisu/encoder/cppspmd_sse.h:508:24: error: cannot bind non-const lvalue reference of type ‘cppspmd_sse41::spmd_kernel::vfloat&’ to an rvalue of type ‘cppspmd_sse41::spmd_kernel::vfloat’
508 | return dst;
| ^~~
```
---
external/basisu/encoder/cppspmd_sse.h | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/external/basisu/encoder/cppspmd_sse.h b/external/basisu/encoder/cppspmd_sse.h
index 030824b7d0..0c77a2d8c7 100644
--- a/external/basisu/encoder/cppspmd_sse.h
+++ b/external/basisu/encoder/cppspmd_sse.h
@@ -493,7 +493,7 @@ struct spmd_kernel
CPPSPMD_FORCE_INLINE vfloat& store(vfloat&& dst, const vfloat& src)
{
dst.m_value = blendv_mask_ps(dst.m_value, src.m_value, _mm_castsi128_ps(m_exec.m_mask));
- return dst;
+ return { dst };
}
CPPSPMD_FORCE_INLINE vfloat& store_all(vfloat& dst, const vfloat& src)
@@ -505,7 +505,7 @@ struct spmd_kernel
CPPSPMD_FORCE_INLINE vfloat& store_all(vfloat&& dst, const vfloat& src)
{
dst.m_value = src.m_value;
- return dst;
+ return { dst };
}
// Linear ref to floats