diff --git a/client/CMakeLists.txt b/client/CMakeLists.txt index ef10ecda..82c8d233 100644 --- a/client/CMakeLists.txt +++ b/client/CMakeLists.txt @@ -86,12 +86,12 @@ target_compile_definitions(wivrn-imgui PUBLIC IMGUI_USE_WCHAR32) target_compile_definitions(wivrn-imgui PUBLIC IMGUI_ENABLE_FREETYPE) # target_compile_definitions(wivrn-imgui PUBLIC IMGUI_DISABLE_OBSOLETE_FUNCTIONS) # incompatible with implot # target_compile_definitions(wivrn-imgui PUBLIC IMGUI_ENABLE_FREETYPE_LUNASVG) -target_compile_features(wivrn-imgui PRIVATE cxx_std_20) +target_compile_features(wivrn-imgui PRIVATE cxx_std_23) target_link_libraries(wivrn-imgui FreetypeHarfbuzz) target_link_libraries(wivrn wivrn-imgui) -target_compile_features(wivrn PRIVATE cxx_std_20) +target_compile_features(wivrn PRIVATE cxx_std_23) target_compile_definitions(wivrn PRIVATE "XR_USE_TIMESPEC=1") set_target_properties(wivrn PROPERTIES CXX_VISIBILITY_PRESET hidden) diff --git a/client/application.cpp b/client/application.cpp index 7c713537..e9eddd30 100644 --- a/client/application.cpp +++ b/client/application.cpp @@ -756,6 +756,8 @@ void application::initialize() opt_extensions.push_back(XR_HTC_PASSTHROUGH_EXTENSION_NAME); opt_extensions.push_back(XR_FB_FACE_TRACKING2_EXTENSION_NAME); opt_extensions.push_back(XR_EXT_PALM_POSE_EXTENSION_NAME); + opt_extensions.push_back(XR_KHR_COMPOSITION_LAYER_DEPTH_EXTENSION_NAME); + opt_extensions.push_back(XR_FB_COMPOSITION_LAYER_DEPTH_TEST_EXTENSION_NAME); for (const auto & i: interaction_profiles) { diff --git a/client/decoder/android/android_decoder.h b/client/decoder/android/android_decoder.h index 23c35a29..b538f0f4 100644 --- a/client/decoder/android/android_decoder.h +++ b/client/decoder/android/android_decoder.h @@ -21,6 +21,7 @@ #include "utils/sync_queue.h" #include "wivrn_packets.h" +#include #include #include #include diff --git a/client/render/imgui_impl.cpp b/client/render/imgui_impl.cpp index f71efef9..900f71b2 100644 --- a/client/render/imgui_impl.cpp +++ b/client/render/imgui_impl.cpp @@ -38,6 +38,7 @@ #include #include #include +#include #include #include @@ -617,7 +618,7 @@ void imgui_context::new_frame(XrTime display_time) } } - for (auto [new_state, controller]: utils::zip(new_states, controllers)) + for (auto [new_state, controller]: std::views::zip(new_states, controllers)) { if (new_state.hover_distance < 0.02 && controller.second.hover_distance >= 0.02) new_state.fingertip_touched = true; @@ -685,7 +686,7 @@ void imgui_context::new_frame(XrTime display_time) } focused_controller = new_focused_controller; - for (auto && [controller, next_state]: utils::zip(controllers, new_states)) + for (auto && [controller, next_state]: std::views::zip(controllers, new_states)) { controller.second = next_state; } diff --git a/client/render/scene_data.cpp b/client/render/scene_data.cpp index 9040e830..5cffdde1 100644 --- a/client/render/scene_data.cpp +++ b/client/render/scene_data.cpp @@ -31,6 +31,7 @@ #include #include #include +#include #include #include #include @@ -492,7 +493,7 @@ public: std::vector> textures; textures.reserve(gltf.textures.size()); - for (auto && [srgb, gltf_texture]: utils::zip(srgb_array, gltf.textures)) + for (auto && [srgb, gltf_texture]: std::views::zip(srgb_array, gltf.textures)) { auto & texture_ref = *textures.emplace_back(std::make_shared()); diff --git a/client/render/scene_renderer.cpp b/client/render/scene_renderer.cpp index 2a42c0e0..c580a705 100644 --- a/client/render/scene_renderer.cpp +++ b/client/render/scene_renderer.cpp @@ -34,19 +34,21 @@ #include #include #include +#include #include #include extern const std::map> shaders; -static vk::Format find_usable_image_format( +// TODO move in lobby? +vk::Format scene_renderer::find_usable_image_format( vk::raii::PhysicalDevice physical_device, - std::span formats, + std::span formats, vk::Extent3D min_extent, vk::ImageUsageFlags usage, - vk::ImageType type = vk::ImageType::e2D, - vk::ImageTiling tiling = vk::ImageTiling::eOptimal, - vk::ImageCreateFlags flags = {}) + vk::ImageType type, + vk::ImageTiling tiling, + vk::ImageCreateFlags flags) { for (vk::Format format: formats) { @@ -176,27 +178,21 @@ scene_renderer::scene_renderer( vk::raii::CommandPool & cb_pool, vk::Extent2D output_size, vk::Format output_format, - std::span depth_formats, - int frames_in_flight) : + // std::span depth_formats, + vk::Format depth_format, + int frames_in_flight, + bool keep_depth_buffer) : physical_device(physical_device), device(device), physical_device_properties(physical_device.getProperties()), queue(queue), output_size(output_size), output_format(output_format), - depth_format( - find_usable_image_format( - physical_device, - depth_formats, - { - output_size.width, - output_size.height, - 1, - }, - vk::ImageUsageFlagBits::eDepthStencilAttachment)), + depth_format(depth_format), layout_0(create_descriptor_set_layout(layout_bindings_0, vk::DescriptorSetLayoutCreateFlagBits::ePushDescriptorKHR)), layout_1(create_descriptor_set_layout(layout_bindings_1)), - ds_pool_material(device, layout_1, layout_bindings_1, 100) // TODO tunable + ds_pool_material(device, layout_1, layout_bindings_1, 100), // TODO tunable + keep_depth_buffer(keep_depth_buffer) { // Create the default material default_material = create_default_material(cb_pool); @@ -211,7 +207,7 @@ scene_renderer::scene_renderer( .commandBufferCount = (uint32_t)frame_resources.size(), }); - for (auto && [res, cb]: utils::zip(frame_resources, command_buffers)) + for (auto && [res, cb]: std::views::zip(frame_resources, command_buffers)) { res.fence = device.createFence({.flags = vk::FenceCreateFlagBits::eSignaled}); res.cb = std::move(cb); @@ -269,12 +265,11 @@ vk::raii::RenderPass scene_renderer::create_renderpass() .format = depth_format, .samples = MSAA_SAMPLES, .loadOp = vk::AttachmentLoadOp::eClear, - .storeOp = vk::AttachmentStoreOp::eDontCare, + .storeOp = keep_depth_buffer ? vk::AttachmentStoreOp::eStore : vk::AttachmentStoreOp::eDontCare, .stencilLoadOp = vk::AttachmentLoadOp::eClear, .stencilStoreOp = vk::AttachmentStoreOp::eDontCare, .initialLayout = vk::ImageLayout::eUndefined, .finalLayout = vk::ImageLayout::eDepthStencilAttachmentOptimal, - }, #ifdef MSAA_4x vk::AttachmentDescription{ @@ -342,23 +337,24 @@ vk::raii::RenderPass scene_renderer::create_renderpass() return vk::raii::RenderPass(device, info); } -scene_renderer::output_image & scene_renderer::get_output_image_data(vk::Image output) +scene_renderer::output_image & scene_renderer::get_output_image_data(vk::Image output_color, vk::Image output_depth) { - auto it = output_images.find(output); + std::pair images{output_color, output_depth}; + auto it = output_images.find(images); if (it != output_images.end()) return it->second; - return output_images.emplace(output, create_output_image_data(output)).first->second; + return output_images.emplace(images, create_output_image_data(output_color, output_depth)).first->second; } -scene_renderer::output_image scene_renderer::create_output_image_data(vk::Image output) +scene_renderer::output_image scene_renderer::create_output_image_data(vk::Image output_color, vk::Image output_depth) { output_image out; // TODO: use image view from xr::swapchain out.image_view = vk::raii::ImageView( device, vk::ImageViewCreateInfo{ - .image = output, + .image = output_color, .viewType = vk::ImageViewType::e2D, .format = output_format, .components{}, @@ -371,30 +367,33 @@ scene_renderer::output_image scene_renderer::create_output_image_data(vk::Image }, }); - out.depth_buffer = image_allocation{ - device, - vk::ImageCreateInfo{ - .imageType = vk::ImageType::e2D, - .format = depth_format, - .extent = { - .width = output_size.width, - .height = output_size.height, - .depth = 1, - }, - .mipLevels = 1, - .arrayLayers = 1, - .samples = MSAA_SAMPLES, - .tiling = vk::ImageTiling::eOptimal, - .usage = vk::ImageUsageFlagBits::eDepthStencilAttachment | vk::ImageUsageFlagBits::eTransientAttachment}, - VmaAllocationCreateInfo{ - .flags = VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT, .usage = VMA_MEMORY_USAGE_AUTO, - .requiredFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, // TODO: check - }}; + // TODO: check requiredFlags + if (not output_depth) + out.depth_buffer = image_allocation{ + device, + vk::ImageCreateInfo{ + .imageType = vk::ImageType::e2D, + .format = depth_format, + .extent = { + .width = output_size.width, + .height = output_size.height, + .depth = 1, + }, + .mipLevels = 1, + .arrayLayers = 1, + .samples = MSAA_SAMPLES, + .tiling = vk::ImageTiling::eOptimal, + .usage = vk::ImageUsageFlagBits::eDepthStencilAttachment | vk::ImageUsageFlagBits::eTransientAttachment}, + VmaAllocationCreateInfo{ + .flags = VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT, + .usage = VMA_MEMORY_USAGE_AUTO, + .requiredFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, + }}; out.depth_view = vk::raii::ImageView( device, vk::ImageViewCreateInfo{ - .image = out.depth_buffer, + .image = output_depth ? output_depth : out.depth_buffer, .viewType = vk::ImageViewType::e2D, .format = depth_format, .components{}, @@ -586,7 +585,7 @@ vk::raii::Pipeline scene_renderer::create_pipeline(const pipeline_info & info) .DepthStencilState = {vk::PipelineDepthStencilStateCreateInfo{ .depthTestEnable = true, .depthWriteEnable = true, - .depthCompareOp = vk::CompareOp::eLess, + .depthCompareOp = vk::CompareOp::eGreater, .depthBoundsTestEnable = false, .minDepthBounds = 0.0f, .maxDepthBounds = 1.0f, @@ -777,7 +776,7 @@ void scene_renderer::render(scene_data & scene, const std::array & cle std::array clear_values{ vk::ClearColorValue{clear_color}, - vk::ClearDepthStencilValue{1.0, 0}, + vk::ClearDepthStencilValue{0.0, 0}, }; auto vertex_layout = scene_data::vertex::describe(); @@ -814,7 +813,7 @@ void scene_renderer::render(scene_data & scene, const std::array & cle for (const auto && [frame_index, frame]: utils::enumerate(frames)) { - scene_renderer::output_image & output = get_output_image_data(frame.destination); + scene_renderer::output_image & output = get_output_image_data(frame.destination, frame.depth_buffer); glm::mat4 viewproj = frame.projection * frame.view; vk::DeviceSize frame_ubo_offset = resources.uniform_buffer_offset; diff --git a/client/render/scene_renderer.h b/client/render/scene_renderer.h index 0a7b4983..aea28fa1 100644 --- a/client/render/scene_renderer.h +++ b/client/render/scene_renderer.h @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include @@ -82,12 +83,12 @@ class scene_renderer image_allocation depth_buffer; vk::raii::ImageView depth_view = nullptr; - image_allocation multisample_image; - vk::raii::ImageView multisample_view = nullptr; + // image_allocation multisample_image; + // vk::raii::ImageView multisample_view = nullptr; }; // Initialization functions - output_image create_output_image_data(vk::Image output); + output_image create_output_image_data(vk::Image output_color, vk::Image output_depth); vk::raii::RenderPass create_renderpass(); vk::raii::PipelineLayout create_pipeline_layout(std::span layouts); vk::raii::Pipeline create_pipeline(const pipeline_info & info); @@ -97,10 +98,10 @@ class scene_renderer vk::raii::DescriptorSetLayout create_descriptor_set_layout(std::span bindings, vk::DescriptorSetLayoutCreateFlags flags = {}); // Caches - std::unordered_map output_images; + std::/*unordered_*/ map, output_image> output_images; // TODO: use an unordered_map, create a hash for std::pair std::unordered_map pipelines; - output_image & get_output_image_data(vk::Image output); + output_image & get_output_image_data(vk::Image output_color, vk::Image output_depth); vk::raii::Pipeline & get_pipeline(const pipeline_info & info); vk::raii::DescriptorSetLayout layout_0; // Descriptor set 0: per-frame/view data (UBO) and per-instance data (UBO + SSBO) @@ -167,12 +168,22 @@ class scene_renderer int current_frame_index; vk::raii::QueryPool query_pool = nullptr; double gpu_time_s = 0; + bool keep_depth_buffer = false; per_frame_resources & current_frame(); void update_material_descriptor_set(scene_data::material & material); public: + static vk::Format find_usable_image_format( + vk::raii::PhysicalDevice physical_device, + std::span formats, + vk::Extent3D min_extent, + vk::ImageUsageFlags usage, + vk::ImageType type = vk::ImageType::e2D, + vk::ImageTiling tiling = vk::ImageTiling::eOptimal, + vk::ImageCreateFlags flags = {}); + scene_renderer( vk::raii::Device & device, vk::raii::PhysicalDevice physical_device, @@ -180,14 +191,16 @@ public: vk::raii::CommandPool & cb_pool, vk::Extent2D output_size, vk::Format output_format, - std::span depth_formats, - int frames_in_flight = 2); + vk::Format depth_format, + int frames_in_flight = 2, + bool keep_depth_buffer = false); ~scene_renderer(); struct frame_info { vk::Image destination; + vk::Image depth_buffer; glm::mat4 projection; glm::mat4 view; }; diff --git a/client/scenes/hand_model.cpp b/client/scenes/hand_model.cpp index 6a3f0457..3872a778 100644 --- a/client/scenes/hand_model.cpp +++ b/client/scenes/hand_model.cpp @@ -18,7 +18,7 @@ #include "hand_model.h" #include "openxr/openxr.h" -#include "utils/ranges.h" +#include #include hand_model::hand_model(const std::filesystem::path & gltf_path, scene_loader & loader, scene_data & scene) @@ -65,7 +65,7 @@ void hand_model::apply(const std::optionalvisible = true; - for (auto && [joint, loc]: utils::zip(joints, *joints_location)) + for (auto && [joint, loc]: std::views::zip(joints, *joints_location)) { if (!joint) continue; diff --git a/client/scenes/lobby.cpp b/client/scenes/lobby.cpp index 16804ab8..4be51071 100644 --- a/client/scenes/lobby.cpp +++ b/client/scenes/lobby.cpp @@ -37,6 +37,7 @@ #include #include #include +#include #include #include #include @@ -89,11 +90,16 @@ static std::string choose_webxr_profile() __builtin_unreachable(); } -static const std::array supported_formats = { +static const std::array supported_color_formats = { vk::Format::eR8G8B8A8Srgb, vk::Format::eB8G8R8A8Srgb, }; +static const std::array supported_depth_formats{ + vk::Format::eD32Sfloat, + vk::Format::eX8D24UnormPack32, +}; + void scenes::lobby::move_gui(glm::vec3 head_position, glm::vec3 new_gui_position) { const float gui_pitch = -0.2; @@ -129,9 +135,10 @@ scenes::lobby::lobby() { spdlog::info(" {}", vk::to_string(format)); } + for (auto format: session.get_swapchain_formats()) { - if (std::find(supported_formats.begin(), supported_formats.end(), format) != supported_formats.end()) + if (std::find(supported_color_formats.begin(), supported_color_formats.end(), format) != supported_color_formats.end()) { swapchain_format = format; break; @@ -141,7 +148,31 @@ scenes::lobby::lobby() if (swapchain_format == vk::Format::eUndefined) throw std::runtime_error(_("No supported swapchain format")); - spdlog::info("Using format {}", vk::to_string(swapchain_format)); + auto views = system.view_configuration_views(viewconfig); + stream_view = override_view(views[0], guess_model()); + uint32_t width = views[0].recommendedImageRectWidth; + uint32_t height = views[0].recommendedImageRectHeight; + + depth_format = scene_renderer::find_usable_image_format( + physical_device, + supported_depth_formats, + { + width, + height, + 1, + }, + vk::ImageUsageFlagBits::eDepthStencilAttachment); + + spdlog::info("Using formats {} and {}", vk::to_string(swapchain_format), vk::to_string(depth_format)); + + composition_layer_depth_test_supported = + instance.has_extension(XR_KHR_COMPOSITION_LAYER_DEPTH_EXTENSION_NAME) and + instance.has_extension(XR_FB_COMPOSITION_LAYER_DEPTH_TEST_EXTENSION_NAME); + + if (composition_layer_depth_test_supported) + spdlog::info("Composition layer depth test supported"); + else + spdlog::info("Composition layer depth test NOT supported"); } static std::string ip_address_to_string(const in_addr & addr) @@ -248,12 +279,14 @@ static glm::mat4 projection_matrix(XrFovf fov, float zn = 0.02) float t = tan(fov.angleUp); float b = tan(fov.angleDown); + // reversed Z projection, infinite far plane + // clang-format off return glm::mat4{ { 2/(r-l), 0, 0, 0 }, { 0, 2/(b-t), 0, 0 }, - { (l+r)/(r-l), (t+b)/(b-t), -1, -1 }, - { 0, 0, -2*zn, 0 } + { (l+r)/(r-l), (t+b)/(b-t), 0, -1 }, + { 0, 0, zn, 0 } }; // clang-format on } @@ -428,42 +461,81 @@ std::optional scenes::lobby::check_recenter_gui(glm::vec3 head_positi return std::nullopt; } -static std::vector render_layer(std::vector & views, std::vector & swapchains, scene_renderer & renderer, scene_data & data, const std::array & clear_color) +static std::pair, std::vector> render_layer( + std::vector & views, + std::vector & color_swapchains, + std::vector & depth_swapchains, + scene_renderer & renderer, + scene_data & data, + const std::array & clear_color) { std::vector frames; frames.reserve(views.size()); - std::vector layer_views; - layer_views.reserve(views.size()); + std::vector proj_layer_views; + std::vector depth_layer_views; + proj_layer_views.reserve(views.size()); + depth_layer_views.reserve(views.size()); - for (auto && [view, swapchain]: utils::zip(views, swapchains)) + for (auto && [view, color_swapchain, depth_swapchain]: std::views::zip(views, color_swapchains, depth_swapchains)) { - int image_index = swapchain.acquire(); - swapchain.wait(); + int color_image_index = color_swapchain.acquire(); + color_swapchain.wait(); + + int depth_image_index = depth_swapchain ? depth_swapchain.acquire() : 0; + if (depth_swapchain) + depth_swapchain.wait(); + + float zn = 0.02; frames.push_back({ - .destination = swapchain.images()[image_index].image, - .projection = projection_matrix(view.fov), + .destination = color_swapchain.images()[color_image_index].image, + .depth_buffer = depth_swapchain ? depth_swapchain.images()[depth_image_index].image : vk::Image{}, + .projection = projection_matrix(view.fov, zn), .view = view_matrix(view.pose), }); - layer_views.push_back({ + proj_layer_views.push_back({ .type = XR_TYPE_COMPOSITION_LAYER_PROJECTION_VIEW, .pose = view.pose, .fov = view.fov, .subImage = { - .swapchain = swapchain, + .swapchain = color_swapchain, .imageRect = { .offset = {0, 0}, - .extent = swapchain.extent(), + .extent = color_swapchain.extent(), }, }, }); + + // TODO: check that the projection matrix is correct wrt https://registry.khronos.org/OpenXR/specs/1.1/html/xrspec.html#XR_KHR_composition_layer_depth + depth_layer_views.push_back({ + .type = XR_TYPE_COMPOSITION_LAYER_DEPTH_INFO_KHR, + .subImage = { + .swapchain = depth_swapchain, + .imageRect = { + .offset = {0, 0}, + .extent = depth_swapchain.extent(), + }, + }, + .minDepth = 0, + .maxDepth = 1, + .nearZ = std::numeric_limits::infinity(), + .farZ = zn, + }); } renderer.render(data, clear_color, frames); - return layer_views; + return {proj_layer_views, depth_layer_views}; +} + +static std::vector render_layer(std::vector & views, std::vector & color_swapchains, scene_renderer & renderer, scene_data & data, const std::array & clear_color) +{ + std::vector depth_swapchains; + depth_swapchains.resize(color_swapchains.size()); + + return std::get<0>(render_layer(views, color_swapchains, depth_swapchains, renderer, data, clear_color)); } namespace @@ -592,22 +664,57 @@ void scenes::lobby::render(const XrFrameState & frame_state) assert(renderer); renderer->start_frame(); + std::vector lobby_layer_views; - if (not application::get_config().passthrough_enabled) - lobby_layer_views = render_layer(views, swapchains_lobby, *renderer, *lobby_scene, {0, 0.25, 0.5, 1}); + std::vector depthinfo_layer_views; + std::vector depthtest_layer_views; + std::vector controllers_layer_views; - auto controllers_layer_views = render_layer(views, swapchains_controllers, *renderer, *controllers_scene, {0, 0, 0, 0}); - renderer->end_frame(); - - // After end_frame because the command buffers are submitted in end_frame - if (not application::get_config().passthrough_enabled) + if (composition_layer_depth_test_supported) { + std::array clear_color; + + lobby_handle->visible = not application::get_config().passthrough_enabled; + + if (application::get_config().passthrough_enabled) + clear_color = {0, 0, 0, 0}; + else + clear_color = {0, 0.25, 0.5, 1}; + + std::tie(lobby_layer_views, depthinfo_layer_views) = render_layer(views, swapchains_lobby, swapchains_depth, *renderer, *lobby_scene, clear_color); + + for (auto [color, depth]: std::views::zip(lobby_layer_views, depthinfo_layer_views)) + { + color.next = &depth; + } + + renderer->end_frame(); + + // After end_frame because the command buffers are submitted in end_frame for (auto & swapchain: swapchains_lobby) swapchain.release(); - } - for (auto & swapchain: swapchains_controllers) - swapchain.release(); + for (auto & swapchain: swapchains_depth) + swapchain.release(); + } + else + { + if (not application::get_config().passthrough_enabled) + lobby_layer_views = render_layer(views, swapchains_lobby, *renderer, *lobby_scene, {0, 0.25, 0.5, 1}); + + controllers_layer_views = render_layer(views, swapchains_controllers, *renderer, *controllers_scene, {0, 0, 0, 0}); + renderer->end_frame(); + + // After end_frame because the command buffers are submitted in end_frame + if (not application::get_config().passthrough_enabled) + { + for (auto & swapchain: swapchains_lobby) + swapchain.release(); + } + + for (auto & swapchain: swapchains_controllers) + swapchain.release(); + } XrCompositionLayerProjection lobby_layer{ .type = XR_TYPE_COMPOSITION_LAYER_PROJECTION, @@ -643,14 +750,39 @@ void scenes::lobby::render(const XrFrameState & frame_state) }}, passthrough); } - else + + if (composition_layer_depth_test_supported) { layers_base.push_back(reinterpret_cast(&lobby_layer)); - } - layers_base.push_back(reinterpret_cast(&imgui_layer)); - layers_base.push_back(reinterpret_cast(&controllers_layer)); + layers_base.push_back(reinterpret_cast(&imgui_layer)); - session.end_frame(frame_state.predictedDisplayTime, layers_base, blend_mode); + // Add XrCompositionLayerDepthTestFB to lobby_layer and imgui_layer + // The sky in the lobby layer and the passthrough layer have the same depth, so the operation must be: + // - LESS when passthrough is enabled (to avoid overwriting the passthrough) + // - LESS_OR_EQUAL when passthrough is disabled (so that the sky is visible) + XrCompositionLayerDepthTestFB layer_depth_test{ + .type = XR_TYPE_COMPOSITION_LAYER_DEPTH_TEST_FB, + .next = nullptr, + .depthMask = true, + .compareOp = application::get_config().passthrough_enabled ? XR_COMPARE_OP_LESS_FB : XR_COMPARE_OP_LESS_OR_EQUAL_FB}; + + lobby_layer.next = &layer_depth_test; + imgui_layer.next = &layer_depth_test; + + session.end_frame(frame_state.predictedDisplayTime, layers_base, blend_mode); + } + else + { + if (not application::get_config().passthrough_enabled) + layers_base.push_back(reinterpret_cast(&lobby_layer)); + + layers_base.push_back(reinterpret_cast(&imgui_layer)); + + if (not composition_layer_depth_test_supported) + layers_base.push_back(reinterpret_cast(&controllers_layer)); + + session.end_frame(frame_state.predictedDisplayTime, layers_base, blend_mode); + } } void scenes::lobby::on_focused() @@ -663,40 +795,47 @@ void scenes::lobby::on_focused() uint32_t height = views[0].recommendedImageRectHeight; swapchains_lobby.reserve(views.size()); - swapchains_controllers.reserve(views.size()); + + if (composition_layer_depth_test_supported) + swapchains_depth.reserve(views.size()); + else + swapchains_controllers.reserve(views.size()); + for ([[maybe_unused]] auto view: views) { assert(view.recommendedImageRectWidth == width); assert(view.recommendedImageRectHeight == height); swapchains_lobby.emplace_back(session, device, swapchain_format, width, height); - swapchains_controllers.emplace_back(session, device, swapchain_format, width, height); + if (composition_layer_depth_test_supported) + swapchains_depth.emplace_back(session, device, depth_format, width, height); + else + swapchains_controllers.emplace_back(session, device, swapchain_format, width, height); } spdlog::info("Created lobby swapchains: {}x{}", width, height); vk::Extent2D output_size{width, height}; - std::array depth_formats{ - vk::Format::eX8D24UnormPack32, - vk::Format::eD32Sfloat, - }; - - renderer.emplace(device, physical_device, queue, commandpool, output_size, swapchain_format, depth_formats); + renderer.emplace(device, physical_device, queue, commandpool, output_size, swapchain_format, depth_format, 2, composition_layer_depth_test_supported); scene_loader loader(device, physical_device, queue, application::queue_family_index(), renderer->get_default_material()); lobby_scene.emplace(); - lobby_scene->import(loader("ground.gltf")); + lobby_handle = lobby_scene->new_node(); + lobby_scene->import(loader("ground.gltf"), lobby_handle); - controllers_scene.emplace(); - input = input_profile("controllers/" + choose_webxr_profile() + "/profile.json", loader, *controllers_scene); + if (not composition_layer_depth_test_supported) + controllers_scene.emplace(); + + scene_data & controllers_scene_data = composition_layer_depth_test_supported ? *lobby_scene : *controllers_scene; + input = input_profile("controllers/" + choose_webxr_profile() + "/profile.json", loader, controllers_scene_data); spdlog::info("Loaded input profile {}", input->id); if (application::get_hand_tracking_supported()) { - left_hand.emplace("left-hand.glb", loader, *controllers_scene); - right_hand.emplace("right-hand.glb", loader, *controllers_scene); + left_hand.emplace("left-hand.glb", loader, controllers_scene_data); + right_hand.emplace("right-hand.glb", loader, controllers_scene_data); } recenter_left_action = get_action("recenter_left").first; @@ -789,6 +928,7 @@ void scenes::lobby::on_unfocused() renderer.reset(); swapchains_lobby.clear(); swapchains_controllers.clear(); + swapchains_depth.clear(); swapchain_imgui = xr::swapchain(); passthrough.emplace(); multicast.reset(); diff --git a/client/scenes/lobby.h b/client/scenes/lobby.h index c39f6360..a027d426 100644 --- a/client/scenes/lobby.h +++ b/client/scenes/lobby.h @@ -65,6 +65,8 @@ class lobby : public scene_impl std::optional input; std::optional left_hand; std::optional right_hand; + node_handle lobby_handle; + bool composition_layer_depth_test_supported; std::optional imgui_ctx; std::array haptic_output; @@ -75,8 +77,10 @@ class lobby : public scene_impl std::vector swapchains_lobby; std::vector swapchains_controllers; + std::vector swapchains_depth; xr::swapchain swapchain_imgui; vk::Format swapchain_format; + vk::Format depth_format; xr::system::passthrough_type passthrough_supported; xr::passthrough passthrough; XrViewConfigurationView stream_view; diff --git a/client/scenes/lobby_keyboard.cpp b/client/scenes/lobby_keyboard.cpp index 42313af5..3118e0dc 100644 --- a/client/scenes/lobby_keyboard.cpp +++ b/client/scenes/lobby_keyboard.cpp @@ -23,8 +23,8 @@ #include "imgui_internal.h" #include "lobby.h" -#include "utils/ranges.h" #include +#include #include // u_toupper struct key @@ -364,7 +364,7 @@ void scenes::lobby::gui_keyboard(ImVec2 size) } int id = 0; - for (const auto & [row, width]: utils::zip(layout, keys_width)) + for (const auto & [row, width]: std::views::zip(layout, keys_width)) { ImVec2 key_position = position; diff --git a/client/scenes/stream.cpp b/client/scenes/stream.cpp index 6f3dd0e4..d1354b2a 100644 --- a/client/scenes/stream.cpp +++ b/client/scenes/stream.cpp @@ -98,7 +98,7 @@ std::shared_ptr scenes::stream::create(std::unique_ptr #include +#include #include namespace @@ -106,7 +107,7 @@ void scenes::stream::accumulate_metrics(XrTime predicted_display_time, const std min_encode_begin = std::min(min_encode_begin, bh->timing_info.encode_begin); } - for (auto && [metrics, bh]: utils::zip(decoder_metrics, blit_handles)) + for (auto && [metrics, bh]: std::views::zip(decoder_metrics, blit_handles)) { if (metrics.size() != global_metrics.size()) metrics.resize(global_metrics.size()); diff --git a/client/scenes/stream_tracking.cpp b/client/scenes/stream_tracking.cpp index da84aa8c..63cd2fd5 100644 --- a/client/scenes/stream_tracking.cpp +++ b/client/scenes/stream_tracking.cpp @@ -19,7 +19,7 @@ #include "application.h" #include "stream.h" -#include "utils/ranges.h" +#include #include #include @@ -245,7 +245,7 @@ void scenes::stream::tracking() auto [flags, views] = session.locate_views(XR_VIEW_CONFIGURATION_TYPE_PRIMARY_STEREO, t0 + Δt, view_space); assert(views.size() == packet.views.size()); - for (auto [i, j]: utils::zip(views, packet.views)) + for (auto [i, j]: std::views::zip(views, packet.views)) { j.pose = i.pose; j.fov = i.fov; diff --git a/client/utils/ranges.h b/client/utils/ranges.h index 5631c7de..f8c336b0 100644 --- a/client/utils/ranges.h +++ b/client/utils/ranges.h @@ -116,60 +116,4 @@ enumerate_range enumerate(T & t) return {t}; } -template -struct zip_range -{ - struct iterator - { - using iterator1 = decltype(std::begin(std::declval())); - using iterator2 = decltype(std::begin(std::declval())); - - using value1 = decltype(*std::declval()); - using value2 = decltype(*std::declval()); - - iterator1 iter1; - iterator2 iter2; - - std::pair operator*() const - { - return {*iter1, *iter2}; - } - - iterator & operator++() - { - ++iter1; - ++iter2; - return *this; - } - - bool operator==(const iterator & other) const - { - return iter1 == other.iter1 || iter2 == other.iter2; - } - - bool operator!=(const iterator & other) const - { - return !(*this == other); - } - }; - - T1 & range1; - T2 & range2; - - iterator begin() const - { - return iterator{std::begin(range1), std::begin(range2)}; - } - - iterator end() const - { - return iterator{std::end(range1), std::end(range2)}; - } -}; - -template -zip_range zip(T1 & range1, T2 & range2) -{ - return zip_range{range1, range2}; -} } // namespace utils diff --git a/client/wivrn_client.h b/client/wivrn_client.h index 6fd8394d..d02cd3d0 100644 --- a/client/wivrn_client.h +++ b/client/wivrn_client.h @@ -51,6 +51,13 @@ public: { control.send(std::forward(packet)); } + + template + void serialize_control(serialization_packet & p, const T & data) + { + control.serialize(p, data); + } + template void send_stream(T && packet) { @@ -60,6 +67,12 @@ public: control.send(std::forward(packet)); } + template + void serialize_stream(serialization_packet & p, const T & data) + { + stream.serialize(p, data); + } + template int poll(T && visitor, std::chrono::milliseconds timeout) { diff --git a/client/xr/session.cpp b/client/xr/session.cpp index bf5f3bce..b4741bdf 100644 --- a/client/xr/session.cpp +++ b/client/xr/session.cpp @@ -20,9 +20,9 @@ #include "session.h" #include "details/enumerate.h" -#include "utils/ranges.h" #include "xr/instance.h" #include "xr/system.h" +#include #include #include @@ -308,7 +308,7 @@ void xr::session::sync_actions(std::span action_sets) { std::vector active_action_sets(action_sets.size()); - for (auto && [i, j]: utils::zip(active_action_sets, action_sets)) + for (auto && [i, j]: std::views::zip(active_action_sets, action_sets)) { i.actionSet = j; i.subactionPath = XR_NULL_PATH; diff --git a/client/xr/swapchain.cpp b/client/xr/swapchain.cpp index dc7b0653..f26f1308 100644 --- a/client/xr/swapchain.cpp +++ b/client/xr/swapchain.cpp @@ -25,10 +25,27 @@ xr::swapchain::swapchain(xr::session & s, vk::raii::Device & device, vk::Format { assert(sample_count == 1); + XrSwapchainUsageFlags usage_flags; + + switch (format) + { + case vk::Format::eD16Unorm: + case vk::Format::eD16UnormS8Uint: + case vk::Format::eD24UnormS8Uint: + case vk::Format::eX8D24UnormPack32: + case vk::Format::eD32Sfloat: + case vk::Format::eD32SfloatS8Uint: + usage_flags = XR_SWAPCHAIN_USAGE_SAMPLED_BIT | XR_SWAPCHAIN_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT; + break; + default: + usage_flags = XR_SWAPCHAIN_USAGE_SAMPLED_BIT | XR_SWAPCHAIN_USAGE_COLOR_ATTACHMENT_BIT; + break; + } + XrSwapchainCreateInfo create_info{ .type = XR_TYPE_SWAPCHAIN_CREATE_INFO, .createFlags = 0, - .usageFlags = XR_SWAPCHAIN_USAGE_SAMPLED_BIT | XR_SWAPCHAIN_USAGE_COLOR_ATTACHMENT_BIT, + .usageFlags = usage_flags, .format = static_cast(format), .sampleCount = (uint32_t)sample_count, .width = (uint32_t)width,