From d06c96df68f66ea091cca38492659d3e0a27c126 Mon Sep 17 00:00:00 2001 From: Patrick Nicolas Date: Sun, 5 May 2024 15:54:04 +0200 Subject: [PATCH] Skip reprojection if runtime does it correctly --- client/decoder/shard_accumulator.cpp | 1 - client/hardware.cpp | 32 +++++++++- client/hardware.h | 2 + client/scenes/stream.cpp | 35 +++++++---- client/scenes/stream_reprojection.cpp | 91 +++++++++++++++------------ client/shaders/reprojection.glsl | 12 +++- common/wivrn_packets.h | 3 - 7 files changed, 118 insertions(+), 58 deletions(-) diff --git a/client/decoder/shard_accumulator.cpp b/client/decoder/shard_accumulator.cpp index 240c80c3..db8756de 100644 --- a/client/decoder/shard_accumulator.cpp +++ b/client/decoder/shard_accumulator.cpp @@ -192,7 +192,6 @@ void shard_accumulator::try_submit_frame(uint16_t shard_idx) spdlog::warn("first shard has no view_info"); return; } - feedback.received_pose = data_shards.front()->view_info->pose; // Try to extract a frame decoder->frame_completed(feedback, timing_info, *data_shards.front()->view_info); diff --git a/client/hardware.cpp b/client/hardware.cpp index 67118eae..ebf23715 100644 --- a/client/hardware.cpp +++ b/client/hardware.cpp @@ -42,7 +42,7 @@ static std::string get_property(const char * property) } #endif -model guess_model() +static model guess_model_() { #ifdef __ANDROID__ const auto device = get_property("ro.product.device"); @@ -85,6 +85,12 @@ model guess_model() return model::unknown; } +model guess_model() +{ + static model m = guess_model_(); + return m; +} + static XrViewConfigurationView scale_view(XrViewConfigurationView view, uint32_t width) { double ratio = double(width) / view.recommendedImageRectWidth; @@ -123,3 +129,27 @@ XrViewConfigurationView override_view(XrViewConfigurationView view, model m) } throw std::range_error("invalid model " + std::to_string((int)m)); } + +static bool use_runtime_reprojection_() +{ + switch (guess_model()) + { + case model::oculus_quest: + return false; + case model::oculus_quest_2: + case model::meta_quest_pro: + case model::meta_quest_3: + case model::pico_neo_3: + case model::pico_4: + case model::htc_vive_focus_3: + case model::htc_vive_xr_elite: + case model::unknown: + return true; + } +} + +bool use_runtime_reprojection() +{ + static bool r = use_runtime_reprojection_(); + return r; +} diff --git a/client/hardware.h b/client/hardware.h index b470ba96..79bcc65c 100644 --- a/client/hardware.h +++ b/client/hardware.h @@ -36,3 +36,5 @@ enum class model model guess_model(); XrViewConfigurationView override_view(XrViewConfigurationView, model); + +bool use_runtime_reprojection(); diff --git a/client/scenes/stream.cpp b/client/scenes/stream.cpp index d7bd5d33..2cafe1cb 100644 --- a/client/scenes/stream.cpp +++ b/client/scenes/stream.cpp @@ -501,12 +501,8 @@ void scenes::stream::render(XrTime predicted_display_time, bool should_render) session.begin_frame(); - auto [flags, views] = session.locate_views(viewconfig, predicted_display_time, world_space); - assert(views.size() == swapchains.size()); - std::array image_indices; - assert(views.size() == view_count); - for (size_t swapchain_index = 0; swapchain_index < views.size(); swapchain_index++) + for (size_t swapchain_index = 0; swapchain_index < view_count; swapchain_index++) { int image_index = swapchains[swapchain_index].acquire(); swapchains[swapchain_index].wait(); @@ -526,8 +522,8 @@ void scenes::stream::render(XrTime predicted_display_time, bool should_render) command_buffer.resetQueryPool(*query_pool, 0, size_gpu_timestamps); command_buffer.writeTimestamp(vk::PipelineStageFlagBits::eTopOfPipe, *query_pool, 0); - std::array pose = {views[0].pose, views[1].pose}; - std::array fov = {views[0].fov, views[1].fov}; + std::array pose{}; + std::array fov{}; { std::unique_lock lock(decoder_mutex); // Search for frame with desired display time on all decoders @@ -548,8 +544,6 @@ void scenes::stream::render(XrTime predicted_display_time, bool should_render) state_ = stream::state::stalled; ++blit_handle->feedback.times_displayed; blit_handle->feedback.displayed = predicted_display_time; - blit_handle->feedback.real_pose[0] = views[0].pose; - blit_handle->feedback.real_pose[1] = views[1].pose; pose = blit_handle->view_info.pose; fov = blit_handle->view_info.fov; @@ -654,7 +648,24 @@ void scenes::stream::render(XrTime predicted_display_time, bool should_render) command_buffer.writeTimestamp(vk::PipelineStageFlagBits::eBottomOfPipe, *query_pool, 1); - // Reproject the image to the real pose + std::vector views(view_count); + if (use_runtime_reprojection()) + { + // In case of reprojection by runtime, give the original pose/fov + for (size_t i = 0; i < view_count; ++i) + { + views[i].pose = pose[i]; + views[i].fov = fov[i]; + } + } + else + { + // Fetch the latest view to reproject + views = session.locate_views(viewconfig, predicted_display_time, world_space).second; + assert(views.size() == swapchains.size()); + } + + // Unfoveate and reproject the image to the real pose for (size_t view = 0; view < view_count; view++) { size_t destination_index = view * swapchains[0].images().size() + image_indices[view]; @@ -887,7 +898,9 @@ void scenes::stream::setup_reprojection_swapchain() // Create a swapchain larger than video strem so that reprojection and foveation affect image quality less. const double max_oversample = std::min(double(view.maxImageRectWidth) / swapchain_width, double(view.maxImageRectHeight) / swapchain_height); - double oversample = std::min(1.4, max_oversample); + double oversample = std::min( + use_runtime_reprojection() ? 1 : 1.4, + max_oversample); XrExtent2Di extent{ .width = int32_t(swapchain_width * oversample), .height = int32_t(swapchain_height * oversample), diff --git a/client/scenes/stream_reprojection.cpp b/client/scenes/stream_reprojection.cpp index 8c20d96b..f717dbbe 100644 --- a/client/scenes/stream_reprojection.cpp +++ b/client/scenes/stream_reprojection.cpp @@ -19,6 +19,7 @@ #include "stream_reprojection.h" #include "application.h" +#include "hardware.h" #include "vk/allocation.h" #include "vk/pipeline.h" #include "vk/shader.h" @@ -241,11 +242,13 @@ stream_reprojection::stream_reprojection( layout = vk::raii::PipelineLayout(device, pipeline_layout_info); - int use_foveation[] = { + int specialization_constants[] = { foveation_parameters[0].x.scale < 1, foveation_parameters[0].y.scale < 1, nb_reprojection_vertices, - nb_reprojection_vertices}; + nb_reprojection_vertices, + not use_runtime_reprojection(), + }; std::array specialization_constants_desc{ vk::SpecializationMapEntry{ @@ -268,12 +271,17 @@ stream_reprojection::stream_reprojection( .offset = 3 * sizeof(int), .size = sizeof(int), }, + vk::SpecializationMapEntry{ + .constantID = 4, + .offset = 4 * sizeof(int), + .size = sizeof(int), + }, }; vk::SpecializationInfo specialization_info; specialization_info.setMapEntries(specialization_constants_desc); - specialization_info.setData(use_foveation); + specialization_info.setData(specialization_constants); vk::pipeline_builder pipeline_info{ .flags = {}, @@ -377,49 +385,52 @@ void stream_reprojection::reproject(vk::raii::CommandBuffer & command_buffer, in if (destination < 0 || destination >= (int)output_images.size()) throw std::runtime_error("Invalid destination image index"); - // Compute the reprojection matrix - float zn = 1; - float r = tan(dest_fov.angleRight); - float l = tan(dest_fov.angleLeft); - float t = tan(dest_fov.angleUp); - float b = tan(dest_fov.angleDown); + if (not use_runtime_reprojection()) + { + // Compute the reprojection matrix + float zn = 1; + float r = tan(dest_fov.angleRight); + float l = tan(dest_fov.angleLeft); + float t = tan(dest_fov.angleUp); + float b = tan(dest_fov.angleDown); - // clang-format off - glm::mat4 hmd_proj{ - { 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 } - }; - // clang-format on + // clang-format off + glm::mat4 hmd_proj{ + { 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 } + }; + // clang-format on - glm::mat4 hmd_view = glm::mat4_cast(glm::quat( - -dest_pose.w, - dest_pose.x, - dest_pose.y, - dest_pose.z)); + glm::mat4 hmd_view = glm::mat4_cast(glm::quat( + -dest_pose.w, + dest_pose.x, + dest_pose.y, + dest_pose.z)); - r = tan(source_fov.angleRight); - l = tan(source_fov.angleLeft); - t = tan(source_fov.angleUp); - b = tan(source_fov.angleDown); + r = tan(source_fov.angleRight); + l = tan(source_fov.angleLeft); + t = tan(source_fov.angleUp); + b = tan(source_fov.angleDown); - // clang-format off - glm::mat4 video_proj{ - { 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 } - }; - // clang-format on + // clang-format off + glm::mat4 video_proj{ + { 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 } + }; + // clang-format on - glm::mat4 video_unview = glm::mat4_cast(glm::quat( - source_pose.w, - source_pose.x, - source_pose.y, - source_pose.z)); + glm::mat4 video_unview = glm::mat4_cast(glm::quat( + source_pose.w, + source_pose.x, + source_pose.y, + source_pose.z)); - ubo[source]->reprojection = hmd_proj * hmd_view * video_unview * glm::inverse(video_proj); + ubo[source]->reprojection = hmd_proj * hmd_view * video_unview * glm::inverse(video_proj); + } if (foveation_parameters[source].x.scale < 1) { diff --git a/client/shaders/reprojection.glsl b/client/shaders/reprojection.glsl index 27fc55e8..c4431bd0 100644 --- a/client/shaders/reprojection.glsl +++ b/client/shaders/reprojection.glsl @@ -23,6 +23,7 @@ layout (constant_id = 0) const bool use_foveation_x = false; layout (constant_id = 1) const bool use_foveation_y = false; layout (constant_id = 2) const int nb_x = 64; layout (constant_id = 3) const int nb_y = 64; +layout (constant_id = 4) const bool reproject = false; layout(set = 0, binding = 1) uniform UniformBufferObject { @@ -53,8 +54,15 @@ vec2 reproject_and_unfoveate(vec2 uv) uv.y = (ubo.lambda * tan(ubo.a * uv + ubo.b) + ubo.xc).y; } } - vec4 tmp = ubo.reprojection * vec4(uv, 1.0, 1.0); - return vec2(tmp) / tmp.z; // between -1 and 1 + if (reproject) + { + vec4 tmp = ubo.reprojection * vec4(uv, 1.0, 1.0); + return vec2(tmp) / tmp.z; // between -1 and 1 + } + else + { + return uv; + } } #ifdef VERT_SHADER diff --git a/common/wivrn_packets.h b/common/wivrn_packets.h index d61bba17..baa6a596 100644 --- a/common/wivrn_packets.h +++ b/common/wivrn_packets.h @@ -213,9 +213,6 @@ struct feedback XrTime displayed; uint8_t times_displayed; - - std::array received_pose; - std::array real_pose; }; using packets = std::variant;