From 1ce83568eb7f821f6d2487d30357573253fdf2c9 Mon Sep 17 00:00:00 2001 From: Guillaume Meunier Date: Fri, 19 Jan 2024 10:14:52 +0100 Subject: [PATCH] Add pipeline cache --- client/application.cpp | 61 +++++++++++++++------- client/application.h | 7 +++ client/asset.cpp | 11 +--- client/decoder/android/android_decoder.cpp | 2 +- client/render/scene_renderer.cpp | 4 +- client/scenes/stream_reprojection.cpp | 3 +- client/utils/files.h | 56 ++++++++++++++++++++ 7 files changed, 112 insertions(+), 32 deletions(-) create mode 100644 client/utils/files.h diff --git a/client/application.cpp b/client/application.cpp index b055938f..8718fe4a 100644 --- a/client/application.cpp +++ b/client/application.cpp @@ -25,6 +25,7 @@ #include "spdlog/common.h" #include "spdlog/spdlog.h" #include "utils/check.h" +#include "utils/files.h" #include "xr/xr.h" #include #include "utils/contains.h" @@ -50,6 +51,8 @@ #include #include "jnipp.h" +#else +#include #endif using namespace std::chrono_literals; @@ -255,6 +258,17 @@ static std::filesystem::path get_config_base_dir() return std::filesystem::path(home) / ".config"; return "."; } + +static std::filesystem::path get_cache_base_dir() +{ + const char * xdg_cache_home = std::getenv("XDG_CACHE_HOME"); + if (xdg_cache_home) + return xdg_cache_home; + const char * home = std::getenv("HOME"); + if (home) + return std::filesystem::path(home) / ".cache"; + return "."; +} #endif VkBool32 application::vulkan_debug_report_callback( @@ -490,11 +504,24 @@ void application::initialize_vulkan() } }; - vk_device = xr_system_id.create_device(vk_physical_device, device_create_info.get()); vk_queue = vk_device.getQueue(vk_queue_family_index, 0); + vk::PipelineCacheCreateInfo pipeline_cache_info; + std::vector pipeline_cache_bytes; + + try { + // TODO Robust pipeline cache serialization + // https://zeux.io/2019/07/17/serializing-pipeline-cache/ + pipeline_cache_bytes = utils::read_whole_file(cache_path / "pipeline_cache"); + + pipeline_cache_info.setInitialData(pipeline_cache_bytes); + } catch (...) { + } + + pipeline_cache = vk::raii::PipelineCache(vk_device, pipeline_cache_info); + VmaAllocatorCreateInfo info{ .physicalDevice = *vk_physical_device, .device = *vk_device, @@ -677,8 +704,6 @@ void application::initialize() spdlog::info("Using format {}", vk::to_string(swapchain_format)); - // XrViewConfigurationProperties view_props = xr_system_id.view_configuration_properties(viewconfig); - auto views = xr_system_id.view_configuration_views(app_info.viewconfig); xr_swapchains.reserve(views.size()); @@ -744,6 +769,7 @@ application::application(application_info info) : if (auto files_dir_path = files_dir.call("getAbsolutePath")) { config_path = files_dir_path; + cache_path = files_dir_path; } } @@ -789,29 +815,15 @@ application::application(application_info info) : initializeLoader((const XrLoaderInitInfoBaseHeaderKHR *)&loaderInitInfoAndroid); } - - { - spdlog::info("trying stuff"); - AAssetDir * dir = AAssetManager_openDir(asset_manager(), ""); - const char * filename; - while((filename = AAssetDir_getNextFileName(dir)) != nullptr) - { - spdlog::info("Found asset file: {}", filename); - } - - dir = AAssetManager_openDir(asset_manager(), "assets"); - while((filename = AAssetDir_getNextFileName(dir)) != nullptr) - { - spdlog::info("Found asset file: assets/{}", filename); - } - }; - #else config_path = get_config_base_dir() / "wivrn"; + cache_path = get_cache_base_dir() / "wivrn"; #endif std::filesystem::create_directories(config_path); + std::filesystem::create_directories(cache_path); spdlog::info("Config path: {}", config_path.native()); + spdlog::info("Cache path: {}", cache_path.native()); try { @@ -884,6 +896,9 @@ void application::cleanup() application::~application() { + auto pipeline_cache_bytes = pipeline_cache.getData(); + utils::write_whole_file(cache_path / "pipeline_cache", pipeline_cache_bytes); + cleanup(); } @@ -972,6 +987,12 @@ void application::run() #else void application::run() { + struct sigaction act{}; + act.sa_handler = [](int){ + instance().exit_requested = true; + }; + sigaction(SIGINT, &act, nullptr); + while (!is_exit_requested()) { loop(); diff --git a/client/application.h b/client/application.h index 7f5d2cc7..ce6584e5 100644 --- a/client/application.h +++ b/client/application.h @@ -88,6 +88,7 @@ class application : public singleton vk::raii::Queue vk_queue = nullptr; vk::raii::CommandPool vk_cmdpool = nullptr; vk::Format swapchain_format; + vk::raii::PipelineCache pipeline_cache = nullptr; // OpenXR stuff void initialize_actions(); @@ -118,6 +119,7 @@ class application : public singleton std::vector xr_extensions; std::atomic exit_requested = false; std::filesystem::path config_path; + std::filesystem::path cache_path; std::string server_address; @@ -305,4 +307,9 @@ public: { return instance().vk_device; } + + static vk::raii::PipelineCache& get_pipeline_cache() + { + return instance().pipeline_cache; + } }; diff --git a/client/asset.cpp b/client/asset.cpp index 0f01073d..7bce1c56 100644 --- a/client/asset.cpp +++ b/client/asset.cpp @@ -19,6 +19,7 @@ #include "asset.h" #include "application.h" +#include "utils/files.h" #include #include #include @@ -84,15 +85,7 @@ asset::asset(const std::filesystem::path& path) spdlog::debug("Loading file asset {}", path.string()); - std::ifstream file(asset_root() / path, std::ios::binary | std::ios::ate); - file.exceptions(std::ios_base::badbit | std::ios_base::failbit); - - size_t size = file.tellg(); - file.seekg(0); - - bytes.resize(size); - - file.read(reinterpret_cast(bytes.data()), size); + bytes = utils::read_whole_file(asset_root() / path); } #endif diff --git a/client/decoder/android/android_decoder.cpp b/client/decoder/android/android_decoder.cpp index 2a906ed5..bccefaa1 100644 --- a/client/decoder/android/android_decoder.cpp +++ b/client/decoder/android/android_decoder.cpp @@ -232,7 +232,7 @@ struct wivrn::android::decoder::pipeline_context .subpass = 0, }; - pipeline = vk::raii::Pipeline(device, nullptr, pipeline_info); + pipeline = vk::raii::Pipeline(device, application::get_pipeline_cache(), pipeline_info); } }; diff --git a/client/render/scene_renderer.cpp b/client/render/scene_renderer.cpp index 3d24b69b..893868be 100644 --- a/client/render/scene_renderer.cpp +++ b/client/render/scene_renderer.cpp @@ -18,6 +18,7 @@ #include "render/scene_renderer.h" +#include "application.h" #include "render/growable_descriptor_pool.h" #include "render/image_loader.h" #include "render/scene_data.h" @@ -34,6 +35,7 @@ #include #include #include +#include "utils/fmt_glm.h" extern const std::map> shaders; @@ -378,7 +380,7 @@ vk::raii::Pipeline scene_renderer::create_pipeline(const pipeline_info & info) auto vertex_shader = load_shader(device, info.shader_name + ".vert"); auto fragment_shader = load_shader(device, info.shader_name + ".frag"); - return vk::raii::Pipeline{device, nullptr, vk::pipeline_builder{ + return vk::raii::Pipeline{device, application::get_pipeline_cache(), vk::pipeline_builder{ .Stages{ vk::PipelineShaderStageCreateInfo{.stage = vk::ShaderStageFlagBits::eVertex, .module = *vertex_shader, .pName = "main"}, vk::PipelineShaderStageCreateInfo{.stage = vk::ShaderStageFlagBits::eFragment, .module = *fragment_shader, .pName = "main"}, diff --git a/client/scenes/stream_reprojection.cpp b/client/scenes/stream_reprojection.cpp index 66004b0d..d13049f5 100644 --- a/client/scenes/stream_reprojection.cpp +++ b/client/scenes/stream_reprojection.cpp @@ -18,6 +18,7 @@ */ #include "stream_reprojection.h" +#include "application.h" #include "vk/allocation.h" #include "vk/shader.h" #include "vk/pipeline.h" @@ -305,7 +306,7 @@ stream_reprojection::stream_reprojection(vk::raii::Device& device, vk::raii::Phy .subpass = 0, }; - pipeline = vk::raii::Pipeline(device, nullptr, pipeline_info); + pipeline = vk::raii::Pipeline(device, application::get_pipeline_cache(), pipeline_info); // Create image views and framebuffers output_image_views.reserve(output_images.size()); diff --git a/client/utils/files.h b/client/utils/files.h new file mode 100644 index 00000000..14a0cfdb --- /dev/null +++ b/client/utils/files.h @@ -0,0 +1,56 @@ +/* + * WiVRn VR streaming + * Copyright (C) 2023-2024 Guillaume Meunier + * + * 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 . + */ + +#pragma once + +#include +#include +#include +#include + +namespace utils +{ +template +static std::vector read_whole_file(std::filesystem::path filename) +{ + std::ifstream file(filename, std::ios::binary | std::ios::ate); + file.exceptions(std::ios_base::badbit | std::ios_base::failbit); + + size_t size = file.tellg(); + file.seekg(0); + + if constexpr(sizeof(T) > 1) + size = size - size % sizeof(T); + + std::vector bytes(size / sizeof(T)); + + file.read(reinterpret_cast(bytes.data()), size); + + return bytes; +} + +template +requires std::contiguous_iterator +static void write_whole_file(std::filesystem::path filename, T bytes) +{ + std::ofstream file(filename, std::ios::binary); + file.exceptions(std::ios_base::badbit | std::ios_base::failbit); + + file.write(reinterpret_cast(bytes.data()), bytes.size() * sizeof(typename T::value_type)); +} +}