Add pipeline cache

This commit is contained in:
Guillaume Meunier
2024-01-28 11:33:32 +01:00
parent d0047d6da5
commit 1ce83568eb
7 changed files with 112 additions and 32 deletions
+41 -20
View File
@@ -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 <algorithm>
#include "utils/contains.h"
@@ -50,6 +51,8 @@
#include <sys/system_properties.h>
#include "jnipp.h"
#else
#include <signal.h>
#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<std::byte> 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<std::byte>(cache_path / "pipeline_cache");
pipeline_cache_info.setInitialData<std::byte>(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<jni::string>("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();
+7
View File
@@ -88,6 +88,7 @@ class application : public singleton<application>
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<application>
std::vector<std::string> xr_extensions;
std::atomic<bool> 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;
}
};
+2 -9
View File
@@ -19,6 +19,7 @@
#include "asset.h"
#include "application.h"
#include "utils/files.h"
#include <filesystem>
#include <fstream>
#include <spdlog/spdlog.h>
@@ -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<char *>(bytes.data()), size);
bytes = utils::read_whole_file<std::byte>(asset_root() / path);
}
#endif
+1 -1
View File
@@ -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);
}
};
+3 -1
View File
@@ -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 <map>
#include <memory>
#include <spdlog/spdlog.h>
#include "utils/fmt_glm.h"
extern const std::map<std::string, std::vector<uint32_t>> 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"},
+2 -1
View File
@@ -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());
+56
View File
@@ -0,0 +1,56 @@
/*
* WiVRn VR streaming
* Copyright (C) 2023-2024 Guillaume Meunier <guillaume.meunier@centraliens.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 <fstream>
#include <vector>
#include <span>
#include <concepts>
namespace utils
{
template<typename T>
static std::vector<T> 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<T> bytes(size / sizeof(T));
file.read(reinterpret_cast<char *>(bytes.data()), size);
return bytes;
}
template<typename T>
requires std::contiguous_iterator<typename T::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<const char *>(bytes.data()), bytes.size() * sizeof(typename T::value_type));
}
}