diff --git a/client/scenes/stream.cpp b/client/scenes/stream.cpp index 3a722671..b4ad3017 100644 --- a/client/scenes/stream.cpp +++ b/client/scenes/stream.cpp @@ -211,7 +211,6 @@ void scenes::stream::render() application::pop_scene(); XrFrameState framestate = session.wait_frame(); - // auto t0 = application::now(); if (decoders.empty()) framestate.shouldRender = false; @@ -233,7 +232,6 @@ void scenes::stream::render() } session.begin_frame(); - // auto dt2 = application::now() - t0; auto [flags, views] = session.locate_views(viewconfig, framestate.predictedDisplayTime, world_space); assert(views.size() == swapchains.size()); @@ -354,7 +352,6 @@ void scenes::stream::render() .pCommandBuffers = &command_buffer, }; CHECK_VK(vkQueueSubmit(queue, 1, &submit_info, fence)); - // auto dt4 = application::now() - t0; std::vector layers_base; std::vector layer_view; @@ -396,17 +393,13 @@ void scenes::stream::render() layers_base.push_back(reinterpret_cast(&layer)); session.end_frame(/*timestamp*/ framestate.predictedDisplayTime, layers_base); - // auto dt5 = application::now() - t0; CHECK_VK(vkWaitForFences(device, 1, &fence, VK_TRUE, UINT64_MAX)); CHECK_VK(vkResetFences(device, 1, &fence)); // We don't need those after vkWaitForFences current_blit_handles.clear(); - // auto dt6 = application::now() - t0; read_actions(); - - // spdlog::info("Render timing: begin frame {:.3f}ms, submit commands {:.3f}ms, end frame {:.3f}ms, wait fence {:.3f}ms, predicted display time {:.3f}ms", dt2/1.e6, dt4/1.e6, dt5/1.e6, dt6/1.e6, (framestate.predictedDisplayTime - t0) / 1.e6); } void scenes::stream::cleanup() @@ -528,7 +521,7 @@ void scenes::stream::setup(const to_headset::video_stream_description & descript images.push_back(i.image); } - reprojector.init(device, physical_device, images, swapchain_images, extent, swapchains[0].format()); + reprojector.init(device, physical_device, images, swapchain_images, extent, swapchains[0].format(), description); } void scenes::stream::video() diff --git a/client/scenes/stream_reprojection.cpp b/client/scenes/stream_reprojection.cpp index 54e6f617..4956ec6e 100644 --- a/client/scenes/stream_reprojection.cpp +++ b/client/scenes/stream_reprojection.cpp @@ -28,6 +28,12 @@ struct stream_reprojection::uniform { alignas(16) glm::mat4 reprojection; + + // Foveation parameters + alignas(8) glm::vec2 a; + alignas(8) glm::vec2 b; + alignas(8) glm::vec2 lambda; + alignas(8) glm::vec2 xc; }; stream_reprojection::~stream_reprojection() @@ -67,7 +73,7 @@ void stream_reprojection::cleanup() } } -void stream_reprojection::init(VkDevice device, VkPhysicalDevice physical_device, std::vector input_images_, std::vector output_images_, VkExtent2D extent, VkFormat format) +void stream_reprojection::init(VkDevice device, VkPhysicalDevice physical_device, std::vector input_images_, std::vector output_images_, VkExtent2D extent, VkFormat format, const xrt::drivers::wivrn::to_headset::video_stream_description & description) { cleanup(); this->device = device; @@ -76,6 +82,7 @@ void stream_reprojection::init(VkDevice device, VkPhysicalDevice physical_device this->output_images = std::move(output_images_); this->extent = extent; this->format = format; + foveation_parameters = description.foveation; VkPhysicalDeviceProperties properties; vkGetPhysicalDeviceProperties(physical_device, &properties); @@ -253,10 +260,26 @@ void stream_reprojection::init(VkDevice device, VkPhysicalDevice physical_device layout = vk::pipeline_layout(device, {.descriptor_set_layouts = {descriptor_set_layout}}); + VkBool32 use_foveation[] = { + foveation_parameters[0].x.scale < 1, + foveation_parameters[0].y.scale < 1 + }; + + std::vector specialization_constants_desc = { + {.constantID = 0, .offset = 0, .size = sizeof(VkBool32)}, + {.constantID = 1, .offset = sizeof(VkBool32), .size = sizeof(VkBool32)} + }; + + VkSpecializationInfo specialization_info{ + .mapEntryCount = (uint32_t)specialization_constants_desc.size(), + .pMapEntries = specialization_constants_desc.data(), + .dataSize = sizeof(use_foveation), + .pData = use_foveation}; + vk::pipeline::graphics_info pipeline_info{ .shader_stages = {{.stage = VK_SHADER_STAGE_VERTEX_BIT, .module = vertex_shader, .pName = "main"}, - {.stage = VK_SHADER_STAGE_FRAGMENT_BIT, .module = fragment_shader, .pName = "main"}}, + {.stage = VK_SHADER_STAGE_FRAGMENT_BIT, .module = fragment_shader, .pName = "main", .pSpecializationInfo = &specialization_info}}, .vertex_input_bindings = {}, .vertex_input_attributes = {}, .InputAssemblyState = {.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP}, @@ -366,6 +389,16 @@ void stream_reprojection::reproject(VkCommandBuffer command_buffer, int source, ubo[source]->reprojection = video_proj * video_view * hmd_unview * glm::inverse(hmd_proj); + ubo[source]->a.x = foveation_parameters[source].x.a; + ubo[source]->b.x = foveation_parameters[source].x.b; + ubo[source]->lambda.x = foveation_parameters[source].x.a / foveation_parameters[source].x.scale; + ubo[source]->xc.x = foveation_parameters[source].x.center; + + ubo[source]->a.y = foveation_parameters[source].y.a; + ubo[source]->b.y = foveation_parameters[source].y.b; + ubo[source]->lambda.y = foveation_parameters[source].y.a / foveation_parameters[source].y.scale; + ubo[source]->xc.y = foveation_parameters[source].y.center; + VkClearValue clear_color{}; VkRenderPassBeginInfo begin_info{ diff --git a/client/scenes/stream_reprojection.h b/client/scenes/stream_reprojection.h index c2020793..0b3137b1 100644 --- a/client/scenes/stream_reprojection.h +++ b/client/scenes/stream_reprojection.h @@ -25,6 +25,7 @@ #include "vk/pipeline_layout.h" #include "vk/renderpass.h" #include "vk/shader.h" +#include "wivrn_packets.h" #include #include @@ -60,12 +61,15 @@ class stream_reprojection vk::pipeline pipeline; vk::renderpass renderpass; + // Foveation + std::array foveation_parameters; + void cleanup(); public: stream_reprojection() = default; stream_reprojection(const stream_reprojection &) = delete; - void init(VkDevice device, VkPhysicalDevice physical_device, std::vector input_images, std::vector output_images, VkExtent2D extent, VkFormat format); + void init(VkDevice device, VkPhysicalDevice physical_device, std::vector input_images, std::vector output_images, VkExtent2D extent, VkFormat format, const xrt::drivers::wivrn::to_headset::video_stream_description & description); ~stream_reprojection(); diff --git a/client/shaders/reprojection.glsl b/client/shaders/reprojection.glsl index 3cd5f16c..0b9055be 100644 --- a/client/shaders/reprojection.glsl +++ b/client/shaders/reprojection.glsl @@ -36,7 +36,6 @@ ubo; void main() { - // outUV = vec2(ubo.reprojection * vec4(positions[gl_VertexIndex], 1.0, 1.0)); outUV = positions[gl_VertexIndex]; gl_Position = vec4(positions[gl_VertexIndex], 0.0, 1.0); } @@ -48,9 +47,17 @@ layout(set = 0, binding = 0) uniform sampler2D texSampler; layout(set = 0, binding = 1) uniform UniformBufferObject { mat4 reprojection; + + vec2 a; + vec2 b; + vec2 lambda; + vec2 xc; } ubo; +layout (constant_id = 0) const bool use_foveation_x = false; +layout (constant_id = 1) const bool use_foveation_y = false; + layout(location = 0) in vec2 inUV; layout(location = 0) out vec4 outColor; @@ -58,7 +65,24 @@ layout(location = 0) out vec4 outColor; void main() { vec4 tmp = ubo.reprojection * vec4(inUV, 1.0, 1.0); + vec2 uv = vec2(tmp) / tmp.z; // between -1 and 1 - outColor = texture(texSampler, vec2(tmp) / tmp.z * 0.5 + vec2(0.5, 0.5)); + if (use_foveation_x && use_foveation_y) + { + uv = ubo.xc + ubo.lambda * atan((uv - ubo.b) / ubo.a); + } + else + { + if (use_foveation_x) + { + uv.x = (ubo.xc + ubo.lambda * atan((uv - ubo.b) / ubo.a)).x; + } + if (use_foveation_y) + { + uv.y = (ubo.xc + ubo.lambda * atan((uv - ubo.b) / ubo.a)).y; + } + } + + outColor = texture(texSampler, uv * 0.5 + vec2(0.5, 0.5)); } #endif diff --git a/common/wivrn_packets.h b/common/wivrn_packets.h index d61e33f5..732a7466 100644 --- a/common/wivrn_packets.h +++ b/common/wivrn_packets.h @@ -190,9 +190,22 @@ struct video_stream_description std::optional range; // VkSamplerYcbcrRange std::optional color_model; // VkSamplerYcbcrModelConversion }; + struct foveation_parameter_item + { + double center; + double scale; + double a; + double b; + }; + struct foveation_parameter + { + foveation_parameter_item x; + foveation_parameter_item y; + }; uint16_t width; uint16_t height; float fps; + std::array foveation; std::vector items; }; diff --git a/server/driver/configuration.cpp b/server/driver/configuration.cpp index cfbc6770..e5571443 100644 --- a/server/driver/configuration.cpp +++ b/server/driver/configuration.cpp @@ -68,7 +68,12 @@ configuration configuration::read_user_configuration() auto json = nlohmann::json::parse(file); if (json.contains("scale")) - result.scale = json["scale"]; + { + if (json["scale"].is_number()) + result.scale = std::array{json["scale"], json["scale"]}; + else + result.scale = json["scale"]; + } if (json.contains("encoders")) { diff --git a/server/driver/configuration.h b/server/driver/configuration.h index 0b0deb1d..b41ee0e5 100644 --- a/server/driver/configuration.h +++ b/server/driver/configuration.h @@ -19,6 +19,7 @@ #pragma once +#include #include #include #include @@ -41,8 +42,7 @@ struct configuration }; std::vector encoders; - std::optional scale; + std::optional> scale; static configuration read_user_configuration(); - }; diff --git a/server/driver/wivrn_comp_target.cpp b/server/driver/wivrn_comp_target.cpp index e70d1d04..18647732 100644 --- a/server/driver/wivrn_comp_target.cpp +++ b/server/driver/wivrn_comp_target.cpp @@ -144,6 +144,7 @@ static void create_encoders(wivrn_comp_target * cn, std::vectorwidth; desc.height = cn->height; desc.fps = cn->fps; + desc.foveation = cn->cnx->get_foveation_parameters(); std::map thread_params; diff --git a/server/driver/wivrn_hmd.cpp b/server/driver/wivrn_hmd.cpp index 6b2a0fa3..d4424cca 100644 --- a/server/driver/wivrn_hmd.cpp +++ b/server/driver/wivrn_hmd.cpp @@ -35,6 +35,7 @@ #include "util/u_time.h" #include "util/u_var.h" +#include #include #include @@ -80,6 +81,147 @@ static void wivrn_hmd_create_compositor_target(struct xrt_device * xdev, struct comp_compositor * comp, struct comp_target ** out_target); +static double foveate(double a, double b, double scale, double c, double x) +{ + // In order to save encoding, transmit and decoding time, only a portion of the image is encoded in full resolution. + // on each axis, foveated coordinates are defined by the following formula. + return a * tan(scale / a * (x - c)) + b; + // a and b are defined such as: + // edges of the image are not moved + // f(-1) = -1 + // f( 1) = 1 + // the function also enforces pixel ratio 1:1 at fovea + // df(x)/dx = scale for x = c +} + +static std::tuple solve_foveation(float scale, float c) +{ + // Compute a and b for the foveation function such that: + // foveate(a, b, scale, c, -1) = -1 (eq. 1) + // foveate(a, b, scale, c, 1) = 1 (eq. 2) + // + // The first step is to solve for a by subtracting equation 1 and 2: + // foveate(a, b, scale, c, 1) - foveate(a, b, scale, c, -1) = 2 (eq. 3) + // + // Where b is cancelled by the subtraction, so the equation to solve becomes: + // f(a) = 0 where: + auto f = [scale, c](double a) { return foveate(a, 0, scale, c, 1) - foveate(a, 0, scale, c, -1) - 2; }; + + // b is computed rewriting equation 2 as: + // foveate(a, 0, scale, c, 1) + b = 1 + // Therefore: + // b = 1 - foveate(a, 0, scale, c, 1) + // + // Note that there are infinitely many solutions to equation 3, but we want + // to have a value of a such that: + // ∀ x ∈ [-1, 1], abs(scale / a * (x - c)) < π / 2 (eq. 4) + // So that foveate(x) is defined over [-1, 1] + // + // Equation 4 can be rewritten as: + // a > 2 * scale / π * abs(x - c) + // + // The minimum value of abs(x - c) for x ∈ [-1, 1] is 1 + abs(c) + // so a must be larger than a0 with: + double a0 = 2 * scale / M_PI * (1 + std::abs(c)); + + // f is monotonically decreasing over (a0, +∞) with: + // lim f(a) = +∞ + // a→a0+ + // + // lim f(a) = 2 * scale - 2 + // a→∞ + // + // Therefore there is one solution iff scale < 1 + // + // a0 is the lowermost value for a, f(a0) is undefined and f(a0 + ε) > 0 + // We want an upper bound a1 for a, f(a1) < 0: + // + // Find the value by computing f(a0*2^n) until negative + double a1 = a0 * 2; + while (f(a1) > 0) + a1 *= 2; + + // Solve f(a) = 0 + + // last computed values for f(a0) and f(a1) + std::optional f_a0; + double f_a1 = f(a1); + + int n = 0; + double a; + while (std::abs(a1 - a0) > 0.0000001 && n++ < 100) + { + if (not f_a0) + { + // use binary search + a = 0.5 * (a0 + a1); + double val = f(a); + if (val > 0) + { + a0 = a; + f_a0 = val; + } + else + { + a1 = a; + f_a1 = val; + } + } + else + { + // f(a1) is always defined + // when f(a0) is defined, use secant method + a = a1 - f_a1 * (a1 - a0) / (f_a1 - *f_a0); + a0 = a1; + a1 = a; + f_a0 = f_a1; + f_a1 = f(a); + } + } + + double b = 1 - foveate(a, 0, scale, c, 1); + + return {a, b}; +} + +bool wivrn_hmd::wivrn_hmd_compute_distortion(xrt_device * xdev, int view_index, float u, float v, xrt_uv_triplet * result) +{ + // u,v are in the output coordinates (sent to the encoder) + // result is in the input coordinates (from the application) + const auto & param = ((wivrn_hmd *)xdev)->foveation_parameters[view_index]; + xrt_vec2 out; + + if (param.x.scale < 1) + { + u = 2 * u - 1; + + out.x = param.x.a * tan(param.x.scale / param.x.a * (u - param.x.center)) + param.x.b; + out.x = std::clamp((1 + out.x) / 2, 0, 1); + } + else + { + out.x = u; + } + + if (param.y.scale < 1) + { + v = 2 * v - 1; + + out.y = param.y.a * tan(param.y.scale / param.y.a * (v - param.y.center)) + param.y.b; + out.y = std::clamp((1 + out.y) / 2, 0, 1); + } + else + { + out.y = v; + } + + result->r = out; + result->g = out; + result->b = out; + + return true; +} + wivrn_hmd::wivrn_hmd(std::shared_ptr cnx, const from_headset::headset_info_packet & info) : xrt_device{}, cnx(cnx) @@ -115,17 +257,20 @@ wivrn_hmd::wivrn_hmd(std::shared_ptr cnx, const auto config = configuration::read_user_configuration(); - auto eye_width = info.recommended_eye_width * config.scale.value_or(1); - auto eye_height = info.recommended_eye_height * config.scale.value_or(1); + auto eye_width = info.recommended_eye_width; // * config.scale.value_or(1); + auto eye_height = info.recommended_eye_height; // * config.scale.value_or(1); + auto scale = config.scale.value_or(std::array{1., 1.}); + auto foveated_eye_width = info.recommended_eye_width * scale[0]; + auto foveated_eye_height = info.recommended_eye_height * scale[1]; fps = info.preferred_refresh_rate; // Setup info. hmd->blend_modes[0] = XRT_BLEND_MODE_OPAQUE; hmd->blend_mode_count = 1; - hmd->distortion.models = XRT_DISTORTION_MODEL_NONE; - hmd->distortion.preferred = XRT_DISTORTION_MODEL_NONE; - hmd->screens[0].w_pixels = eye_width * 2; - hmd->screens[0].h_pixels = eye_height; + hmd->distortion.models = XRT_DISTORTION_MODEL_COMPUTE; + hmd->distortion.preferred = XRT_DISTORTION_MODEL_COMPUTE; + hmd->screens[0].w_pixels = foveated_eye_width * 2; + hmd->screens[0].h_pixels = foveated_eye_height; hmd->screens[0].nominal_frame_interval_ns = 1000000000 / fps; // Left @@ -133,17 +278,17 @@ wivrn_hmd::wivrn_hmd(std::shared_ptr cnx, hmd->views[0].display.h_pixels = eye_height; hmd->views[0].viewport.x_pixels = 0; hmd->views[0].viewport.y_pixels = 0; - hmd->views[0].viewport.w_pixels = eye_width; - hmd->views[0].viewport.h_pixels = eye_height; + hmd->views[0].viewport.w_pixels = foveated_eye_width; + hmd->views[0].viewport.h_pixels = foveated_eye_height; hmd->views[0].rot = u_device_rotation_ident; // Right hmd->views[1].display.w_pixels = eye_width; hmd->views[1].display.h_pixels = eye_height; - hmd->views[1].viewport.x_pixels = eye_width; + hmd->views[1].viewport.x_pixels = foveated_eye_width; hmd->views[1].viewport.y_pixels = 0; - hmd->views[1].viewport.w_pixels = eye_width; - hmd->views[1].viewport.h_pixels = eye_height; + hmd->views[1].viewport.w_pixels = foveated_eye_width; + hmd->views[1].viewport.h_pixels = foveated_eye_height; hmd->views[1].rot = u_device_rotation_ident; // Default FOV from Oculus Quest @@ -157,8 +302,36 @@ wivrn_hmd::wivrn_hmd(std::shared_ptr cnx, hmd->distortion.fov[1].angle_up = 47 * M_PI / 180; hmd->distortion.fov[1].angle_down = -53 * M_PI / 180; - // Distortion information, fills in xdev->compute_distortion(). - u_distortion_mesh_set_none(this); + for (int i = 0; i < 2; ++i) + { + foveation_parameters[i].x.scale = scale[0]; + foveation_parameters[i].y.scale = scale[1]; + const auto & view = hmd->views[i]; + const auto & fov = hmd->distortion.fov[i]; + float l = tan(fov.angle_left); + float r = tan(fov.angle_right); + float t = tan(fov.angle_up); + float b = tan(fov.angle_down); + if (scale[0] < 1) + { + float cu = (r + l) / (l - r); + foveation_parameters[i].x.center = cu; + + std::tie(foveation_parameters[i].x.a, foveation_parameters[i].x.b) = solve_foveation(scale[0], cu); + } + + if (scale[1] < 1) + { + float cv = (t + b) / (t - b); + foveation_parameters[i].y.center = cv; + + std::tie(foveation_parameters[i].y.a, foveation_parameters[i].y.b) = solve_foveation(scale[1], cv); + } + } + + // Distortion information + compute_distortion = wivrn_hmd_compute_distortion; + u_distortion_mesh_fill_in_compute(this); } void wivrn_hmd::update_inputs() diff --git a/server/driver/wivrn_hmd.h b/server/driver/wivrn_hmd.h index 66024f55..fd9f0326 100644 --- a/server/driver/wivrn_hmd.h +++ b/server/driver/wivrn_hmd.h @@ -39,9 +39,12 @@ class wivrn_hmd : public xrt_device xrt_tracking_origin tracking_origin; view_list views; + std::array foveation_parameters{}; std::shared_ptr cnx; + static bool wivrn_hmd_compute_distortion(xrt_device * xdev, int view_index, float u, float v, xrt_uv_triplet * result); + public: wivrn_hmd(std::shared_ptr cnx, const from_headset::headset_info_packet & info); @@ -61,4 +64,9 @@ public: xrt_pose * out_poses); void update_tracking(const from_headset::tracking &, const clock_offset &); + + const auto & get_foveation_parameters() + { + return foveation_parameters; + } }; diff --git a/server/driver/wivrn_session.cpp b/server/driver/wivrn_session.cpp index f9cf20b6..c79a3cb7 100644 --- a/server/driver/wivrn_session.cpp +++ b/server/driver/wivrn_session.cpp @@ -202,6 +202,11 @@ void wivrn_session::run(std::weak_ptr weak_self) } } +std::array wivrn_session::get_foveation_parameters() +{ + return hmd->get_foveation_parameters(); +} + void wivrn_session::dump_time(const std::string &event, uint64_t frame, uint64_t time, uint8_t stream) { if (feedback_csv) diff --git a/server/driver/wivrn_session.h b/server/driver/wivrn_session.h index 8a40c1eb..a5f3322a 100644 --- a/server/driver/wivrn_session.h +++ b/server/driver/wivrn_session.h @@ -89,6 +89,8 @@ public: connection.send_control(packet); } + std::array get_foveation_parameters(); + void dump_time(const std::string& event, uint64_t frame, uint64_t time, uint8_t stream = -1); private: