Merge remote-tracking branch 'upstream/master'
This commit is contained in:
@@ -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<std::byte> pipeline_cache_bytes;
|
||||
|
||||
@@ -19,7 +19,14 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifdef __ANDROID__
|
||||
#include <android_native_app_glue.h>
|
||||
#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 <android_native_app_glue.h>
|
||||
#endif
|
||||
|
||||
#include "utils/singleton.h"
|
||||
#include "vk/vk_allocator.h"
|
||||
#include "xr/xr.h"
|
||||
#include <atomic>
|
||||
#include <boost/locale/generator.hpp>
|
||||
@@ -105,7 +106,7 @@ class application : public singleton<application>
|
||||
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::raii::Queue> 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<vk::raii::Queue> & get_queue()
|
||||
{
|
||||
return instance().vk_queue;
|
||||
}
|
||||
|
||||
@@ -20,12 +20,14 @@
|
||||
#include "image_loader.h"
|
||||
#include "wivrn_config.h"
|
||||
|
||||
#include <cstdint>
|
||||
#if WIVRN_USE_LIBKTX
|
||||
#include <ktxvulkan.h>
|
||||
#endif
|
||||
#include <chrono>
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <span>
|
||||
#include <spdlog/fmt/chrono.h>
|
||||
#include <spdlog/spdlog.h>
|
||||
#include <stdexcept>
|
||||
#include <vulkan/vulkan_raii.hpp>
|
||||
@@ -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<vk::raii::Queue> & 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<const std::byte> 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");
|
||||
}
|
||||
|
||||
|
||||
@@ -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 <vulkan/vulkan.hpp>
|
||||
|
||||
#if WIVRN_USE_LIBKTX
|
||||
struct ktxVulkanDeviceInfo;
|
||||
#include <ktxvulkan.h>
|
||||
#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<vk::raii::Queue> & 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<const std::byte> 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<vk::raii::Queue> & queue;
|
||||
vk::raii::CommandPool & cb_pool;
|
||||
|
||||
buffer_allocation staging_buffer;
|
||||
|
||||
@@ -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<controller> controllers_, xr::swapchain & swapchain, std::vector<viewport> layers) :
|
||||
imgui_context::imgui_context(
|
||||
vk::raii::PhysicalDevice physical_device,
|
||||
vk::raii::Device & device,
|
||||
uint32_t queue_family_index,
|
||||
thread_safe<vk::raii::Queue> & queue,
|
||||
std::span<controller> controllers_,
|
||||
xr::swapchain & swapchain,
|
||||
std::vector<viewport> 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<std::pair<int, XrCompositionLayerQuad>> 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();
|
||||
|
||||
|
||||
@@ -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<vk::raii::Queue> & 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<vk::raii::Queue> & queue,
|
||||
std::span<controller> controllers,
|
||||
xr::swapchain & swapchain,
|
||||
std::vector<viewport> layers);
|
||||
|
||||
@@ -260,7 +260,7 @@ fastgltf::MimeType guess_mime_type(std::span<const std::byte> image_data)
|
||||
std::shared_ptr<vk::raii::ImageView> do_load_image(
|
||||
vk::raii::PhysicalDevice & physical_device,
|
||||
vk::raii::Device & device,
|
||||
vk::raii::Queue & queue,
|
||||
thread_safe<vk::raii::Queue> & queue,
|
||||
vk::raii::CommandPool & cb_pool,
|
||||
std::span<const std::byte> 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<vk::raii::Queue> & queue;
|
||||
vk::raii::CommandPool & cb_pool;
|
||||
|
||||
std::vector<asset> 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<vk::raii::Queue> & queue,
|
||||
vk::raii::CommandPool & cb_pool) :
|
||||
base_directory(base_directory),
|
||||
gltf(gltf),
|
||||
|
||||
@@ -22,17 +22,18 @@
|
||||
#include <vulkan/vulkan_raii.hpp>
|
||||
|
||||
#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<vk::raii::Queue> & queue;
|
||||
uint32_t queue_family_index;
|
||||
std::shared_ptr<renderer::material> 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<renderer::material> default_material) :
|
||||
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) :
|
||||
device(device),
|
||||
physical_device(physical_device),
|
||||
queue(queue),
|
||||
|
||||
@@ -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<vk::raii::Queue> & 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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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<vk::raii::Queue> & 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<vk::raii::Queue> & queue,
|
||||
vk::raii::CommandPool & cb_pool,
|
||||
int frames_in_flight = 2);
|
||||
|
||||
|
||||
+1
-1
@@ -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<vk::raii::Queue> & queue;
|
||||
vk::raii::CommandPool & commandpool;
|
||||
uint32_t queue_family_index;
|
||||
|
||||
|
||||
@@ -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<XrCompositionLayerProjectionView> layer_view(view_count);
|
||||
|
||||
@@ -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<XrComposition
|
||||
.layers = layers.data(),
|
||||
};
|
||||
|
||||
auto lock = application::get_queue().lock();
|
||||
CHECK_XR(xrEndFrame(id, &end_info));
|
||||
}
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
*/
|
||||
|
||||
#include "swapchain.h"
|
||||
#include "application.h"
|
||||
#include "details/enumerate.h"
|
||||
#include "session.h"
|
||||
|
||||
@@ -95,6 +96,7 @@ int xr::swapchain::acquire()
|
||||
.type = XR_TYPE_SWAPCHAIN_IMAGE_ACQUIRE_INFO,
|
||||
};
|
||||
|
||||
auto lock = application::get_queue().lock();
|
||||
CHECK_XR(xrAcquireSwapchainImage(id, &acquire_info, &index));
|
||||
|
||||
return index;
|
||||
@@ -118,5 +120,6 @@ void xr::swapchain::release()
|
||||
.type = XR_TYPE_SWAPCHAIN_IMAGE_RELEASE_INFO,
|
||||
};
|
||||
|
||||
auto lock = application::get_queue().lock();
|
||||
CHECK_XR(xrReleaseSwapchainImage(id, &release_info));
|
||||
}
|
||||
|
||||
@@ -76,6 +76,16 @@ public:
|
||||
{
|
||||
return locked<T, Mutex>(value, mutex);
|
||||
}
|
||||
|
||||
T & get_unsafe()
|
||||
{
|
||||
return value;
|
||||
}
|
||||
|
||||
const T & get_unsafe() const
|
||||
{
|
||||
return value;
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
|
||||
@@ -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
|
||||
{
|
||||
|
||||
@@ -89,15 +89,6 @@ struct hash_context
|
||||
};
|
||||
} // namespace details
|
||||
|
||||
template <typename T, typename Enable = void>
|
||||
struct serialization_traits;
|
||||
|
||||
template <typename T>
|
||||
size_t serialized_size(const T & x)
|
||||
{
|
||||
return serialization_traits<T>::size(x);
|
||||
}
|
||||
|
||||
class deserialization_error : public std::runtime_error
|
||||
{
|
||||
static std::string hexdump(std::span<uint8_t> raw_data)
|
||||
@@ -132,6 +123,25 @@ public:
|
||||
std::runtime_error("Serialization error") {}
|
||||
};
|
||||
|
||||
template <typename T, typename Enable = void>
|
||||
struct serialization_traits;
|
||||
|
||||
template <typename T>
|
||||
size_t serialized_size(const T & x)
|
||||
{
|
||||
return serialization_traits<T>::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<uint8_t> buffer;
|
||||
@@ -173,6 +183,23 @@ public:
|
||||
serialization_traits<T>::serialize(value, *this);
|
||||
}
|
||||
|
||||
constexpr void serialize_size(size_t size)
|
||||
{
|
||||
if (size < 0x7fff)
|
||||
{
|
||||
serialize<uint16_t>(size);
|
||||
}
|
||||
else if (size < 0x7fff'ffff)
|
||||
{
|
||||
serialize<uint16_t>((size & 0x7fff) | 0x8000);
|
||||
serialize<uint16_t>(size >> 15);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw serialization_error{};
|
||||
}
|
||||
}
|
||||
|
||||
operator std::vector<std::span<uint8_t>> *()
|
||||
{
|
||||
return &this->operator std::vector<std::span<uint8_t>> &();
|
||||
@@ -251,6 +278,16 @@ public:
|
||||
return serialization_traits<T>::deserialize(*this);
|
||||
}
|
||||
|
||||
size_t deserialize_size()
|
||||
{
|
||||
size_t size = deserialize<uint16_t>();
|
||||
|
||||
if (size & 0x8000)
|
||||
size = (size & 0x7fff) | (deserialize<uint16_t>() << 15);
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void deserialize(T & v)
|
||||
{
|
||||
@@ -402,6 +439,7 @@ struct serialize_bits<T, std::tuple<std::integer_sequence<size_t, i, mid...>, Bi
|
||||
return (sizeof(boost::pfr::tuple_element_t<mid, T>) + ... + sizeof(boost::pfr::tuple_element_t<i, T>)) + serialize_bits<T, std::tuple<Bits...>>::size(t);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace details
|
||||
|
||||
template <typename T>
|
||||
@@ -555,8 +593,6 @@ struct serialization_traits<T, std::enable_if_t<std::is_aggregate_v<T> && !is_st
|
||||
template <>
|
||||
struct serialization_traits<std::string>
|
||||
{
|
||||
using size_type = uint16_t;
|
||||
|
||||
static constexpr void type_hash(details::hash_context & h)
|
||||
{
|
||||
h.feed("string");
|
||||
@@ -564,17 +600,14 @@ struct serialization_traits<std::string>
|
||||
|
||||
static void serialize(const std::string & value, serialization_packet & packet)
|
||||
{
|
||||
if (value.size() > std::numeric_limits<size_type>::max())
|
||||
throw serialization_error{};
|
||||
|
||||
packet.serialize<size_type>(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_type>();
|
||||
size_t size = packet.deserialize_size();
|
||||
|
||||
packet.check_remaining_size(size);
|
||||
|
||||
@@ -589,15 +622,13 @@ struct serialization_traits<std::string>
|
||||
}
|
||||
static size_t size(const std::string & value)
|
||||
{
|
||||
return sizeof(size_type) + value.size();
|
||||
return serialized_size_of_size(value.size()) + value.size();
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct serialization_traits<std::vector<T>>
|
||||
{
|
||||
using size_type = uint16_t;
|
||||
|
||||
static constexpr void type_hash(details::hash_context & h)
|
||||
{
|
||||
h.feed("vector<");
|
||||
@@ -607,10 +638,7 @@ struct serialization_traits<std::vector<T>>
|
||||
|
||||
static void serialize(const std::vector<T> & value, serialization_packet & packet)
|
||||
{
|
||||
if (value.size() > std::numeric_limits<size_type>::max())
|
||||
throw serialization_error{};
|
||||
|
||||
packet.serialize<size_type>(value.size());
|
||||
packet.serialize_size(value.size());
|
||||
|
||||
if constexpr (serialization_traits<T>::is_trivially_serializable())
|
||||
{
|
||||
@@ -626,7 +654,7 @@ struct serialization_traits<std::vector<T>>
|
||||
static std::vector<T> deserialize(deserialization_packet & packet)
|
||||
{
|
||||
std::vector<T> value;
|
||||
size_t size = packet.deserialize<size_type>();
|
||||
size_t size = packet.deserialize_size();
|
||||
|
||||
if constexpr (serialization_traits<T>::is_trivially_serializable())
|
||||
{
|
||||
@@ -653,10 +681,10 @@ struct serialization_traits<std::vector<T>>
|
||||
static size_t size(const std::vector<T> & 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<std::chrono::duration<Rep, Period>>
|
||||
template <>
|
||||
struct serialization_traits<std::span<uint8_t>>
|
||||
{
|
||||
using size_type = uint16_t;
|
||||
|
||||
static constexpr void type_hash(details::hash_context & h)
|
||||
{
|
||||
h.feed("span<uint8_t>");
|
||||
@@ -880,16 +906,13 @@ struct serialization_traits<std::span<uint8_t>>
|
||||
|
||||
static void serialize(const std::span<uint8_t> & value, serialization_packet & packet)
|
||||
{
|
||||
if (value.size() > std::numeric_limits<size_type>::max())
|
||||
throw serialization_error{};
|
||||
|
||||
packet.serialize<size_type>(value.size());
|
||||
packet.serialize_size(value.size());
|
||||
packet.write(value);
|
||||
}
|
||||
|
||||
static std::span<uint8_t> deserialize(deserialization_packet & packet)
|
||||
{
|
||||
size_t size = packet.deserialize<size_type>();
|
||||
size_t size = packet.deserialize_size();
|
||||
return packet.read_span(size);
|
||||
}
|
||||
|
||||
@@ -898,9 +921,9 @@ struct serialization_traits<std::span<uint8_t>>
|
||||
return false;
|
||||
}
|
||||
|
||||
static size_t size(const std::span<uint8_t> & x)
|
||||
static size_t size(const std::span<uint8_t> & 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<crypto::bignum>
|
||||
|
||||
static size_t size(const crypto::bignum & value)
|
||||
{
|
||||
return sizeof(serialization_traits<std::string>::size_type) + value.data_size();
|
||||
return serialized_size_of_size(value.data_size()) + value.data_size();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -34,21 +34,21 @@ static_assert(hash(1234) == 0x1fabbdf10314a21d);
|
||||
static_assert(hash(-1234) == 0x77fa1c9653db5a84);
|
||||
static_assert(hash(0) == 0xaf63ad4c86019caf);
|
||||
|
||||
static_assert(serialization_type_hash<bool>() == hash("uint8"));
|
||||
static_assert(serialization_type_hash<int>() == hash("int32"));
|
||||
static_assert(serialization_type_hash<float>() == hash("float32"));
|
||||
static_assert(serialization_type_hash<double>() == hash("float64"));
|
||||
static_assert(serialization_type_hash<std::chrono::nanoseconds>() == hash("duration<int64,1/1000000000>"));
|
||||
static_assert(serialization_type_hash<std::optional<int>>() == hash("optional<int32>"));
|
||||
static_assert(serialization_type_hash<std::vector<int>>() == hash("vector<int32>"));
|
||||
static_assert(serialization_type_hash<std::array<int, 42>>() == hash("array<int32,42>"));
|
||||
static_assert(serialization_type_hash<std::string>() == hash("string"));
|
||||
static_assert(serialization_type_hash<std::variant<int, float>>() == hash("variant<int32,float32>"));
|
||||
static_assert(serialization_type_hash<bool>(0) == hash("uint8"));
|
||||
static_assert(serialization_type_hash<int>(0) == hash("int32"));
|
||||
static_assert(serialization_type_hash<float>(0) == hash("float32"));
|
||||
static_assert(serialization_type_hash<double>(0) == hash("float64"));
|
||||
static_assert(serialization_type_hash<std::chrono::nanoseconds>(0) == hash("duration<int64,1/1000000000>"));
|
||||
static_assert(serialization_type_hash<std::optional<int>>(0) == hash("optional<int32>"));
|
||||
static_assert(serialization_type_hash<std::vector<int>>(0) == hash("vector<int32>"));
|
||||
static_assert(serialization_type_hash<std::array<int, 42>>(0) == hash("array<int32,42>"));
|
||||
static_assert(serialization_type_hash<std::string>(0) == hash("string"));
|
||||
static_assert(serialization_type_hash<std::variant<int, float>>(0) == hash("variant<int32,float32>"));
|
||||
|
||||
struct test
|
||||
{
|
||||
int x;
|
||||
float y;
|
||||
};
|
||||
static_assert(serialization_type_hash<test>() == hash("structure{int32,float32}"));
|
||||
static_assert(serialization_type_hash<test>(0) == hash("structure{int32,float32}"));
|
||||
} // namespace
|
||||
|
||||
Reference in New Issue
Block a user