diff --git a/client/application.cpp b/client/application.cpp index 798010ac..b0433274 100644 --- a/client/application.cpp +++ b/client/application.cpp @@ -882,8 +882,7 @@ 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_queue.lock() = vk_device.getQueue(vk_queue_family_index, 0); vk::PipelineCacheCreateInfo pipeline_cache_info; std::vector pipeline_cache_bytes; diff --git a/client/application.h b/client/application.h index 377749c1..cb17f87e 100644 --- a/client/application.h +++ b/client/application.h @@ -19,7 +19,14 @@ #pragma once +#ifdef __ANDROID__ +#include +#endif + #include "configuration.h" +#include "utils/singleton.h" +#include "utils/thread_safe.h" +#include "vk/vk_allocator.h" #include "wifi_lock.h" #include "xr/fb_body_tracker.h" #include "xr/fb_face_tracker2.h" @@ -28,12 +35,6 @@ #include "xr/htc_face_tracker.h" #include "xr/pico_body_tracker.h" #include "xr/pico_face_tracker.h" -#ifdef __ANDROID__ -#include -#endif - -#include "utils/singleton.h" -#include "vk/vk_allocator.h" #include "xr/xr.h" #include #include @@ -105,7 +106,7 @@ class application : public singleton vk::raii::PhysicalDevice vk_physical_device = nullptr; vk::raii::Device vk_device = nullptr; uint32_t vk_queue_family_index; - vk::raii::Queue vk_queue = nullptr; + thread_safe vk_queue = nullptr; vk::raii::CommandPool vk_cmdpool = nullptr; vk::raii::PipelineCache pipeline_cache = nullptr; vk::PhysicalDeviceProperties physical_device_properties; @@ -360,7 +361,7 @@ public: return instance().vk_queue_family_index; } - static vk::raii::Queue & get_queue() + static thread_safe & get_queue() { return instance().vk_queue; } diff --git a/client/render/image_loader.cpp b/client/render/image_loader.cpp index 16dfe4cd..032cf462 100644 --- a/client/render/image_loader.cpp +++ b/client/render/image_loader.cpp @@ -20,12 +20,14 @@ #include "image_loader.h" #include "wivrn_config.h" -#include #if WIVRN_USE_LIBKTX #include #endif +#include +#include #include #include +#include #include #include #include @@ -160,21 +162,20 @@ int bytes_per_pixel(vk::Format format) } } // namespace -image_loader::image_loader(vk::raii::PhysicalDevice physical_device, vk::raii::Device & device, vk::raii::Queue & queue, vk::raii::CommandPool & cb_pool) : +image_loader::image_loader(vk::raii::PhysicalDevice physical_device, vk::raii::Device & device, thread_safe & queue, vk::raii::CommandPool & cb_pool) : device(device), queue(queue), cb_pool(cb_pool) { #if WIVRN_USE_LIBKTX - vdi = ktxVulkanDeviceInfo_Create(*physical_device, *device, *queue, *cb_pool, nullptr); + ktxVulkanDeviceInfo_Construct(&vdi, *physical_device, *device, *queue.get_unsafe(), *cb_pool, nullptr); #endif } image_loader::~image_loader() { #if WIVRN_USE_LIBKTX - if (vdi) - ktxVulkanDeviceInfo_Destroy(vdi); + ktxVulkanDeviceInfo_Destruct(&vdi); #endif } @@ -381,7 +382,7 @@ void image_loader::do_load_raw(const void * pixels, vk::Extent3D extent, vk::For vk::SubmitInfo info; info.setCommandBuffers(*cb); auto fence = device.createFence(vk::FenceCreateInfo{}); - queue.submit(info, *fence); + queue.lock()->submit(info, *fence); if (auto result = device.waitForFences(*fence, true, 1'000'000'000); result != vk::Result::eSuccess) throw std::runtime_error("vkWaitForfences: " + vk::to_string(result)); @@ -414,21 +415,28 @@ void image_loader::do_load_ktx(std::span bytes) if (err != KTX_SUCCESS) { - spdlog::info("ktxTexture_CreateFromMemory: error {}", (int)err); + spdlog::warn("ktxTexture_CreateFromMemory: error {}", (int)err); throw std::runtime_error("ktxTexture_CreateFromMemory"); } if (ktxTexture_NeedsTranscoding(texture)) { // TODO + spdlog::warn("ktxTexture_NeedsTranscoding"); + throw std::runtime_error("ktxTexture_NeedsTranscoding"); + } + + { + auto _ = queue.lock(); // ktxTexture_VkUploadEx uses the queue internally (in ktxTexture_VkUploadEx_WithSuballocator) + // TODO: patch vkloader.c to accept a mutex + err = ktxTexture_VkUploadEx(texture, &vdi, &vk_texture, VK_IMAGE_TILING_OPTIMAL, VK_IMAGE_USAGE_SAMPLED_BIT, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL); } - err = ktxTexture_VkUploadEx(texture, vdi, &vk_texture, VK_IMAGE_TILING_OPTIMAL, VK_IMAGE_USAGE_SAMPLED_BIT, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL); if (err != KTX_SUCCESS) { // TODO try uncompressing the texture ktxTexture_Destroy(texture); - spdlog::info("ktxTexture_CreateFromMemory: error {}", (int)err); + spdlog::warn("ktxTexture_CreateFromMemory: error {}", (int)err); throw std::runtime_error("ktxTexture_VkUploadEx"); } diff --git a/client/render/image_loader.h b/client/render/image_loader.h index c8bee22d..ab7d69a7 100644 --- a/client/render/image_loader.h +++ b/client/render/image_loader.h @@ -19,6 +19,7 @@ #pragma once +#include "utils/thread_safe.h" #include "vk/allocation.h" #include "vk/fwd.h" #include "wivrn_config.h" @@ -31,7 +32,7 @@ #include #if WIVRN_USE_LIBKTX -struct ktxVulkanDeviceInfo; +#include #endif struct image_loader @@ -45,7 +46,10 @@ struct image_loader uint32_t num_mipmaps; - image_loader(vk::raii::PhysicalDevice physical_device, vk::raii::Device & device, vk::raii::Queue & queue, vk::raii::CommandPool & cb_pool); + image_loader(vk::raii::PhysicalDevice physical_device, vk::raii::Device & device, thread_safe & queue, vk::raii::CommandPool & cb_pool); + image_loader(const image_loader &) = delete; + image_loader & operator=(const image_loader &) = delete; + ~image_loader(); // Load a PNG/JPEG/KTX2 file void load(std::span bytes, bool srgb); @@ -71,15 +75,13 @@ struct image_loader load(pixels.data(), pixels.size() * sizeof(T), extent, format); } - ~image_loader(); - private: #if WIVRN_USE_LIBKTX - ktxVulkanDeviceInfo * vdi = nullptr; + ktxVulkanDeviceInfo vdi; #endif vk::raii::Device & device; - vk::raii::Queue & queue; + thread_safe & queue; vk::raii::CommandPool & cb_pool; buffer_allocation staging_buffer; diff --git a/client/render/imgui_impl.cpp b/client/render/imgui_impl.cpp index 06945c2f..53d59862 100644 --- a/client/render/imgui_impl.cpp +++ b/client/render/imgui_impl.cpp @@ -297,7 +297,14 @@ static const std::array layout_bindings = { .stageFlags = vk::ShaderStageFlagBits::eFragment, }}; -imgui_context::imgui_context(vk::raii::PhysicalDevice physical_device, vk::raii::Device & device, uint32_t queue_family_index, vk::raii::Queue & queue, std::span controllers_, xr::swapchain & swapchain, std::vector layers) : +imgui_context::imgui_context( + vk::raii::PhysicalDevice physical_device, + vk::raii::Device & device, + uint32_t queue_family_index, + thread_safe & queue, + std::span controllers_, + xr::swapchain & swapchain, + std::vector layers) : physical_device(physical_device), device(device), queue_family_index(queue_family_index), @@ -341,7 +348,7 @@ imgui_context::imgui_context(vk::raii::PhysicalDevice physical_device, vk::raii: .PhysicalDevice = *application::get_physical_device(), .Device = *application::get_device(), .QueueFamily = application::queue_family_index(), - .Queue = *application::get_queue(), + .Queue = *queue.get_unsafe(), .RenderPass = *renderpass, .MinImageCount = 2, .ImageCount = (uint32_t)swapchain.images().size(), // used to cycle between VkBuffers in ImGui_ImplVulkan_RenderDrawData @@ -901,17 +908,21 @@ std::vector> imgui_context::end_frame() .pClearValues = &clear}, vk::SubpassContents::eInline); - ImGui_ImplVulkan_RenderDrawData(ImGui::GetDrawData(), *cb); + { + auto _ = queue.lock(); // ImGui_ImplVulkan_RenderDrawData uses the queue internally (in ImGui_ImplVulkan_UpdateTexture) + // TODO: patch imgui_impl_vulkan to accept a mutex + ImGui_ImplVulkan_RenderDrawData(ImGui::GetDrawData(), *cb); + } cb.endRenderPass(); cb.end(); - queue.submit(vk::SubmitInfo{ - .commandBufferCount = 1, - .pCommandBuffers = &*cb, - }, - *fence); + queue.lock()->submit(vk::SubmitInfo{ + .commandBufferCount = 1, + .pCommandBuffers = &*cb, + }, + *fence); swapchain.release(); diff --git a/client/render/imgui_impl.h b/client/render/imgui_impl.h index 025661ad..cd5b2b78 100644 --- a/client/render/imgui_impl.h +++ b/client/render/imgui_impl.h @@ -19,6 +19,7 @@ #pragma once #include "render/growable_descriptor_pool.h" +#include "utils/thread_safe.h" #include "wivrn_config.h" #include "xr/hand_tracker.h" #include "xr/space.h" @@ -119,7 +120,7 @@ private: vk::raii::PhysicalDevice physical_device; vk::raii::Device & device; uint32_t queue_family_index; - vk::raii::Queue & queue; + thread_safe & queue; vk::raii::Pipeline pipeline = nullptr; vk::raii::DescriptorSetLayout ds_layout; @@ -178,7 +179,7 @@ public: vk::raii::PhysicalDevice physical_device, vk::raii::Device & device, uint32_t queue_family_index, - vk::raii::Queue & queue, + thread_safe & queue, std::span controllers, xr::swapchain & swapchain, std::vector layers); diff --git a/client/render/scene_loader.cpp b/client/render/scene_loader.cpp index 8a00a5c2..66ba86a4 100644 --- a/client/render/scene_loader.cpp +++ b/client/render/scene_loader.cpp @@ -260,7 +260,7 @@ fastgltf::MimeType guess_mime_type(std::span image_data) std::shared_ptr do_load_image( vk::raii::PhysicalDevice & physical_device, vk::raii::Device & device, - vk::raii::Queue & queue, + thread_safe & queue, vk::raii::CommandPool & cb_pool, std::span image_data, bool srgb) @@ -316,7 +316,7 @@ class loader_context fastgltf::Asset & gltf; vk::raii::PhysicalDevice physical_device; vk::raii::Device & device; - vk::raii::Queue & queue; + thread_safe & queue; vk::raii::CommandPool & cb_pool; std::vector loaded_assets; @@ -330,7 +330,7 @@ public: fastgltf::Asset & gltf, vk::raii::PhysicalDevice physical_device, vk::raii::Device & device, - vk::raii::Queue & queue, + thread_safe & queue, vk::raii::CommandPool & cb_pool) : base_directory(base_directory), gltf(gltf), diff --git a/client/render/scene_loader.h b/client/render/scene_loader.h index e1b165a4..9c28bfff 100644 --- a/client/render/scene_loader.h +++ b/client/render/scene_loader.h @@ -22,17 +22,18 @@ #include #include "scene_components.h" +#include "utils/thread_safe.h" class scene_loader { vk::raii::Device & device; vk::raii::PhysicalDevice physical_device; - vk::raii::Queue & queue; + thread_safe & queue; uint32_t queue_family_index; std::shared_ptr default_material; public: - scene_loader(vk::raii::Device & device, vk::raii::PhysicalDevice physical_device, vk::raii::Queue & queue, uint32_t queue_family_index, std::shared_ptr default_material) : + scene_loader(vk::raii::Device & device, vk::raii::PhysicalDevice physical_device, thread_safe & queue, uint32_t queue_family_index, std::shared_ptr default_material) : device(device), physical_device(physical_device), queue(queue), diff --git a/client/render/scene_renderer.cpp b/client/render/scene_renderer.cpp index 279252e3..d944c559 100644 --- a/client/render/scene_renderer.cpp +++ b/client/render/scene_renderer.cpp @@ -202,7 +202,7 @@ static std::array layout_bindings_1{ scene_renderer::scene_renderer( vk::raii::Device & device, vk::raii::PhysicalDevice physical_device, - vk::raii::Queue & queue, + thread_safe & queue, vk::raii::CommandPool & cb_pool, int frames_in_flight) : physical_device(physical_device), @@ -706,11 +706,11 @@ void scene_renderer::end_frame() f.cb.writeTimestamp(vk::PipelineStageFlagBits::eTopOfPipe, *query_pool, current_frame_index * 2 + 1); f.cb.end(); - queue.submit(vk::SubmitInfo{ - .commandBufferCount = 1, - .pCommandBuffers = &*f.cb, - }, - *f.fence); + queue.lock()->submit(vk::SubmitInfo{ + .commandBufferCount = 1, + .pCommandBuffers = &*f.cb, + }, + *f.fence); f.query_pool_filled = true; } diff --git a/client/render/scene_renderer.h b/client/render/scene_renderer.h index 562bf945..5a4858c3 100644 --- a/client/render/scene_renderer.h +++ b/client/render/scene_renderer.h @@ -34,6 +34,7 @@ #include "render/growable_descriptor_pool.h" #include "render/scene_components.h" +#include "utils/thread_safe.h" struct renderpass_info { @@ -103,7 +104,7 @@ class scene_renderer vk::raii::PhysicalDevice physical_device; vk::raii::Device & device; vk::PhysicalDeviceProperties physical_device_properties; - vk::raii::Queue & queue; + thread_safe & queue; // Destination images struct output_image @@ -217,7 +218,7 @@ public: scene_renderer( vk::raii::Device & device, vk::raii::PhysicalDevice physical_device, - vk::raii::Queue & queue, + thread_safe & queue, vk::raii::CommandPool & cb_pool, int frames_in_flight = 2); diff --git a/client/scene.h b/client/scene.h index 65098358..e84bbcea 100644 --- a/client/scene.h +++ b/client/scene.h @@ -89,7 +89,7 @@ protected: vk::raii::Instance & vk_instance; vk::raii::Device & device; vk::raii::PhysicalDevice & physical_device; - vk::raii::Queue & queue; + thread_safe & queue; vk::raii::CommandPool & commandpool; uint32_t queue_family_index; diff --git a/client/scenes/stream.cpp b/client/scenes/stream.cpp index ca66f181..dc20f14e 100644 --- a/client/scenes/stream.cpp +++ b/client/scenes/stream.cpp @@ -1108,7 +1108,7 @@ void scenes::stream::render(const XrFrameState & frame_state) command_buffer.end(); vk::SubmitInfo submit_info; submit_info.setCommandBuffers(*command_buffer); - queue.submit(submit_info, *fence); + queue.lock()->submit(submit_info, *fence); swapchain.release(); std::vector layer_view(view_count); diff --git a/client/xr/session.cpp b/client/xr/session.cpp index b119a195..bc0cd2af 100644 --- a/client/xr/session.cpp +++ b/client/xr/session.cpp @@ -127,6 +127,7 @@ void xr::session::begin_frame() .type = XR_TYPE_FRAME_BEGIN_INFO, }; + auto lock = application::get_queue().lock(); CHECK_XR(xrBeginFrame(id, &begin_info)); } @@ -140,6 +141,7 @@ void xr::session::end_frame(XrTime display_time, const std::vector(value, mutex); } + + T & get_unsafe() + { + return value; + } + + const T & get_unsafe() const + { + return value; + } }; template diff --git a/common/wivrn_packets.h b/common/wivrn_packets.h index 68ccc398..1c40219c 100644 --- a/common/wivrn_packets.h +++ b/common/wivrn_packets.h @@ -41,7 +41,7 @@ namespace wivrn // Default port for server to listen, both TCP and UDP static const int default_port = 9757; -static constexpr int protocol_revision = 1; +static constexpr int protocol_revision = 2; enum class device_id : uint8_t { diff --git a/common/wivrn_serialization.h b/common/wivrn_serialization.h index 938801ef..8e451e51 100644 --- a/common/wivrn_serialization.h +++ b/common/wivrn_serialization.h @@ -89,15 +89,6 @@ struct hash_context }; } // namespace details -template -struct serialization_traits; - -template -size_t serialized_size(const T & x) -{ - return serialization_traits::size(x); -} - class deserialization_error : public std::runtime_error { static std::string hexdump(std::span raw_data) @@ -132,6 +123,25 @@ public: std::runtime_error("Serialization error") {} }; +template +struct serialization_traits; + +template +size_t serialized_size(const T & x) +{ + return serialization_traits::size(x); +} + +constexpr size_t serialized_size_of_size(size_t size) +{ + if (size < 0x7fff) + return sizeof(uint16_t); + else if (size < 0x7fff'ffff) + return 2 * sizeof(uint16_t); + else + throw serialization_error{}; +} + class serialization_packet { std::vector buffer; @@ -173,6 +183,23 @@ public: serialization_traits::serialize(value, *this); } + constexpr void serialize_size(size_t size) + { + if (size < 0x7fff) + { + serialize(size); + } + else if (size < 0x7fff'ffff) + { + serialize((size & 0x7fff) | 0x8000); + serialize(size >> 15); + } + else + { + throw serialization_error{}; + } + } + operator std::vector> *() { return &this->operator std::vector> &(); @@ -251,6 +278,16 @@ public: return serialization_traits::deserialize(*this); } + size_t deserialize_size() + { + size_t size = deserialize(); + + if (size & 0x8000) + size = (size & 0x7fff) | (deserialize() << 15); + + return size; + } + template void deserialize(T & v) { @@ -402,6 +439,7 @@ struct serialize_bits, Bi return (sizeof(boost::pfr::tuple_element_t) + ... + sizeof(boost::pfr::tuple_element_t)) + serialize_bits>::size(t); } }; + } // namespace details template @@ -555,8 +593,6 @@ struct serialization_traits && !is_st template <> struct serialization_traits { - using size_type = uint16_t; - static constexpr void type_hash(details::hash_context & h) { h.feed("string"); @@ -564,17 +600,14 @@ struct serialization_traits static void serialize(const std::string & value, serialization_packet & packet) { - if (value.size() > std::numeric_limits::max()) - throw serialization_error{}; - - packet.serialize(value.size()); + packet.serialize_size(value.size()); packet.write(value.data(), value.size()); } static std::string deserialize(deserialization_packet & packet) { std::string value; - size_t size = packet.deserialize(); + size_t size = packet.deserialize_size(); packet.check_remaining_size(size); @@ -589,15 +622,13 @@ struct serialization_traits } static size_t size(const std::string & value) { - return sizeof(size_type) + value.size(); + return serialized_size_of_size(value.size()) + value.size(); } }; template struct serialization_traits> { - using size_type = uint16_t; - static constexpr void type_hash(details::hash_context & h) { h.feed("vector<"); @@ -607,10 +638,7 @@ struct serialization_traits> static void serialize(const std::vector & value, serialization_packet & packet) { - if (value.size() > std::numeric_limits::max()) - throw serialization_error{}; - - packet.serialize(value.size()); + packet.serialize_size(value.size()); if constexpr (serialization_traits::is_trivially_serializable()) { @@ -626,7 +654,7 @@ struct serialization_traits> static std::vector deserialize(deserialization_packet & packet) { std::vector value; - size_t size = packet.deserialize(); + size_t size = packet.deserialize_size(); if constexpr (serialization_traits::is_trivially_serializable()) { @@ -653,10 +681,10 @@ struct serialization_traits> static size_t size(const std::vector & value) { if constexpr (is_trivially_serializable()) - return sizeof(size_type) + value.size() * sizeof(T); + return serialized_size_of_size(value.size()) + value.size() * sizeof(T); else { - size_t res = sizeof(size_type); + size_t res = serialized_size_of_size(value.size()); for (const auto & item: value) res += serialized_size(item); return res; @@ -871,8 +899,6 @@ struct serialization_traits> template <> struct serialization_traits> { - using size_type = uint16_t; - static constexpr void type_hash(details::hash_context & h) { h.feed("span"); @@ -880,16 +906,13 @@ struct serialization_traits> static void serialize(const std::span & value, serialization_packet & packet) { - if (value.size() > std::numeric_limits::max()) - throw serialization_error{}; - - packet.serialize(value.size()); + packet.serialize_size(value.size()); packet.write(value); } static std::span deserialize(deserialization_packet & packet) { - size_t size = packet.deserialize(); + size_t size = packet.deserialize_size(); return packet.read_span(size); } @@ -898,9 +921,9 @@ struct serialization_traits> return false; } - static size_t size(const std::span & x) + static size_t size(const std::span & value) { - return sizeof(size_type) + x.size_bytes(); + return serialized_size_of_size(value.size()) + value.size_bytes(); } }; @@ -957,7 +980,7 @@ struct serialization_traits static size_t size(const crypto::bignum & value) { - return sizeof(serialization_traits::size_type) + value.data_size(); + return serialized_size_of_size(value.data_size()) + value.data_size(); } }; diff --git a/common/wivrn_serialization_ut.cpp b/common/wivrn_serialization_ut.cpp index a0011504..2dadab17 100644 --- a/common/wivrn_serialization_ut.cpp +++ b/common/wivrn_serialization_ut.cpp @@ -34,21 +34,21 @@ static_assert(hash(1234) == 0x1fabbdf10314a21d); static_assert(hash(-1234) == 0x77fa1c9653db5a84); static_assert(hash(0) == 0xaf63ad4c86019caf); -static_assert(serialization_type_hash() == hash("uint8")); -static_assert(serialization_type_hash() == hash("int32")); -static_assert(serialization_type_hash() == hash("float32")); -static_assert(serialization_type_hash() == hash("float64")); -static_assert(serialization_type_hash() == hash("duration")); -static_assert(serialization_type_hash>() == hash("optional")); -static_assert(serialization_type_hash>() == hash("vector")); -static_assert(serialization_type_hash>() == hash("array")); -static_assert(serialization_type_hash() == hash("string")); -static_assert(serialization_type_hash>() == hash("variant")); +static_assert(serialization_type_hash(0) == hash("uint8")); +static_assert(serialization_type_hash(0) == hash("int32")); +static_assert(serialization_type_hash(0) == hash("float32")); +static_assert(serialization_type_hash(0) == hash("float64")); +static_assert(serialization_type_hash(0) == hash("duration")); +static_assert(serialization_type_hash>(0) == hash("optional")); +static_assert(serialization_type_hash>(0) == hash("vector")); +static_assert(serialization_type_hash>(0) == hash("array")); +static_assert(serialization_type_hash(0) == hash("string")); +static_assert(serialization_type_hash>(0) == hash("variant")); struct test { int x; float y; }; -static_assert(serialization_type_hash() == hash("structure{int32,float32}")); +static_assert(serialization_type_hash(0) == hash("structure{int32,float32}")); } // namespace