From 64b446c0d8ea7ee44fc7531a778888d86a6a0428 Mon Sep 17 00:00:00 2001 From: Patrick Nicolas Date: Tue, 3 Jun 2025 22:04:24 +0200 Subject: [PATCH] Rewrite foveation to enforce pixel alignment foveation now uses integer pixel ratios, and tries to follow the previous tangent function. --- client/scenes/stream.cpp | 24 +- client/scenes/stream_reprojection.cpp | 273 +++---- client/scenes/stream_reprojection.h | 25 +- client/shaders/reprojection.glsl | 49 +- common/wivrn_packets.h | 25 +- ...001-c-multi-early-wake-of-compositor.patch | 4 +- ...02-ipc-server-Always-listen-to-stdin.patch | 4 +- .../monado/0003-Use-extern-socket-fd.patch | 4 +- ...Add-storage-usage-to-distortion-mesh.patch | 4 +- ...vironment-blend-mode-selection-logic.patch | 4 +- .../0006-Convert-to-YCbCr-in-Monado.patch | 4 +- .../0007-store-alpha-channel-in-layer-1.patch | 4 +- ...t-oxr-bind-to-dynamic-role-s-profile.patch | 4 +- ...main-allow-custom-pacing-app-factory.patch | 4 +- .../0010-st-oxr-forward-0-refresh-rate.patch | 4 +- ...tortion-with-pixel-aligned-foveation.patch | 700 ++++++++++++++++++ server/driver/wivrn_comp_target.cpp | 80 +- server/driver/wivrn_comp_target.h | 5 +- server/driver/wivrn_foveation.cpp | 473 +++++++----- server/driver/wivrn_foveation.h | 60 +- server/driver/wivrn_hmd.cpp | 166 +---- server/driver/wivrn_hmd.h | 14 +- server/driver/wivrn_session.cpp | 39 +- server/driver/wivrn_session.h | 18 +- 24 files changed, 1291 insertions(+), 700 deletions(-) create mode 100644 patches/monado/0011-Replace-distortion-with-pixel-aligned-foveation.patch diff --git a/client/scenes/stream.cpp b/client/scenes/stream.cpp index eb03c4eb..010cc84a 100644 --- a/client/scenes/stream.cpp +++ b/client/scenes/stream.cpp @@ -860,10 +860,9 @@ void scenes::stream::render(const XrFrameState & frame_state) } command_buffer.writeTimestamp(vk::PipelineStageFlagBits::eBottomOfPipe, *query_pool, 1); - reprojector->set_foveation(foveation); - // Unfoveate the image to the real pose - reprojector->reproject(command_buffer, image_index); + // defoveate the image + auto extents = reprojector->reproject(command_buffer, foveation, image_index); command_buffer.writeTimestamp(vk::PipelineStageFlagBits::eBottomOfPipe, *query_pool, 2); @@ -908,10 +907,7 @@ void scenes::stream::render(const XrFrameState & frame_state) .swapchain = swapchain, .imageRect = { .offset = {0, 0}, - .extent = { - swapchain.width(), - swapchain.height(), - }, + .extent = extents[view], }, .imageArrayIndex = view, }, @@ -1161,8 +1157,8 @@ void scenes::stream::setup_reprojection_swapchain() const uint32_t video_width = video_stream_description->width / view_count; const uint32_t video_height = video_stream_description->height; - uint32_t swapchain_width = video_width / video_stream_description->foveation[0].x.scale; - uint32_t swapchain_height = video_height / video_stream_description->foveation[0].y.scale; + uint32_t swapchain_width = video_stream_description->defoveated_width / view_count; + uint32_t swapchain_height = video_stream_description->defoveated_height; const configuration::sgsr_settings sgsr = application::get_config().sgsr; if (sgsr.enabled) @@ -1189,7 +1185,15 @@ void scenes::stream::setup_reprojection_swapchain() for (auto & image: swapchain.images()) swapchain_images.push_back(image.image); - reprojector.emplace(device, physical_device, decoder_out_image, 2, swapchain_images, extent, swapchain.format(), *video_stream_description); + reprojector.emplace( + device, + physical_device, + decoder_out_image, + vk::Extent2D{.width = video_width, .height = video_height}, + 2, + swapchain_images, + extent, + swapchain.format()); } scene::meta & scenes::stream::get_meta_scene() diff --git a/client/scenes/stream_reprojection.cpp b/client/scenes/stream_reprojection.cpp index fc701634..5600de02 100644 --- a/client/scenes/stream_reprojection.cpp +++ b/client/scenes/stream_reprojection.cpp @@ -20,30 +20,24 @@ #include "stream_reprojection.h" #include "application.h" #include "utils/contains.h" +#include "utils/ranges.h" #include "vk/allocation.h" #include "vk/pipeline.h" #include "vk/shader.h" #include #include #include -#include #include #include #include -namespace +struct stream_reprojection::vertex { -struct uniform -{ - // Foveation parameters - alignas(8) glm::vec2 a; - alignas(8) glm::vec2 b; - alignas(8) glm::vec2 lambda; - alignas(8) glm::vec2 xc; + // output image position + alignas(8) glm::vec2 position; + // input texture coordinates + alignas(8) glm::vec2 uv; }; -} // namespace - -const int nb_reprojection_vertices = 128; struct SgsrSpecializationConstants { @@ -52,24 +46,47 @@ struct SgsrSpecializationConstants float edge_sharpness; }; +void stream_reprojection::ensure_vertices(size_t num_vertices) +{ + vk::BufferCreateInfo create_info{ + .size = num_vertices * sizeof(vertex) * view_count, + .usage = vk::BufferUsageFlagBits::eVertexBuffer, + .sharingMode = vk::SharingMode::eExclusive, + }; + + if (create_info.size <= buffer.info().size) + return; + + VmaAllocationCreateInfo alloc_info{ + .requiredFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, + }; + + buffer = buffer_allocation(device, create_info, alloc_info); + vertices_size = num_vertices * sizeof(vertex); +} + +stream_reprojection::vertex * stream_reprojection::get_vertices(size_t view) +{ + assert(buffer); + return reinterpret_cast(reinterpret_cast(buffer.map()) + view * vertices_size); +} + stream_reprojection::stream_reprojection( vk::raii::Device & device, vk::raii::PhysicalDevice & physical_device, vk::Image input_image_, + vk::Extent2D input_extent, uint32_t view_count, std::vector output_images_, - vk::Extent2D extent, - vk::Format format, - const wivrn::to_headset::video_stream_description & description) : + vk::Extent2D output_extent, + vk::Format format) : view_count(view_count), + device(device), input_image(input_image_), + input_extent(input_extent), output_images(std::move(output_images_)), - extent(extent) + output_extent(output_extent) { - foveation_parameters = description.foveation; - - vk::PhysicalDeviceProperties properties = physical_device.getProperties(); - vk::SamplerCreateInfo sampler_info{ .magFilter = vk::Filter::eLinear, .minFilter = vk::Filter::eLinear, @@ -93,35 +110,12 @@ stream_reprojection::stream_reprojection( sampler = vk::raii::Sampler(device, sampler_info); - uniform_size = sizeof(uniform) + properties.limits.minUniformBufferOffsetAlignment - 1; - uniform_size = uniform_size - uniform_size % properties.limits.minUniformBufferOffsetAlignment; - - vk::BufferCreateInfo create_info{ - .size = uniform_size * view_count, - .usage = vk::BufferUsageFlagBits::eUniformBuffer, - .sharingMode = vk::SharingMode::eExclusive, - }; - - VmaAllocationCreateInfo alloc_info{ - .requiredFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, - }; - - buffer = buffer_allocation(device, create_info, alloc_info); - // Create VkDescriptorSetLayout - std::array layout_binding{ - vk::DescriptorSetLayoutBinding{ - .binding = 0, - .descriptorType = vk::DescriptorType::eCombinedImageSampler, - .descriptorCount = 1, - .stageFlags = vk::ShaderStageFlagBits::eFragment, - }, - vk::DescriptorSetLayoutBinding{ - .binding = 1, - .descriptorType = vk::DescriptorType::eUniformBuffer, - .descriptorCount = 1, - .stageFlags = vk::ShaderStageFlagBits::eVertex | vk::ShaderStageFlagBits::eFragment, - }, + vk::DescriptorSetLayoutBinding layout_binding{ + .binding = 0, + .descriptorType = vk::DescriptorType::eCombinedImageSampler, + .descriptorCount = 1, + .stageFlags = vk::ShaderStageFlagBits::eFragment, }; vk::DescriptorSetLayoutCreateInfo layout_info; @@ -134,10 +128,6 @@ stream_reprojection::stream_reprojection( .type = vk::DescriptorType::eCombinedImageSampler, .descriptorCount = (uint32_t)view_count, }, - vk::DescriptorPoolSize{ - .type = vk::DescriptorType::eUniformBuffer, - .descriptorCount = (uint32_t)view_count, - }, }; vk::DescriptorPoolCreateInfo pool_info{ @@ -186,13 +176,7 @@ stream_reprojection::stream_reprojection( .imageLayout = vk::ImageLayout::eShaderReadOnlyOptimal, }; - vk::DescriptorBufferInfo buffer_info{ - .buffer = buffer, - .offset = uniform_size * view, - .range = sizeof(uniform), - }; - - std::array write{ + device.updateDescriptorSets( vk::WriteDescriptorSet{ .dstSet = descriptor_set, .dstBinding = 0, @@ -201,17 +185,7 @@ stream_reprojection::stream_reprojection( .descriptorType = vk::DescriptorType::eCombinedImageSampler, .pImageInfo = &image_info, }, - vk::WriteDescriptorSet{ - .dstSet = descriptor_set, - .dstBinding = 1, - .dstArrayElement = 0, - .descriptorCount = 1, - .descriptorType = vk::DescriptorType::eUniformBuffer, - .pBufferInfo = &buffer_info, - }, - }; - - device.updateDescriptorSets(write, {}); + {}); } // Create renderpass @@ -248,40 +222,6 @@ stream_reprojection::stream_reprojection( // Vertex shader vk::raii::ShaderModule vertex_shader = load_shader(device, "reprojection.vert"); - int vertex_specialization_constants[] = { - foveation_parameters[0].x.scale < 1, - foveation_parameters[0].y.scale < 1, - nb_reprojection_vertices, - nb_reprojection_vertices, - }; - - std::array vertex_specialization_constants_desc{ - vk::SpecializationMapEntry{ - .constantID = 0, - .offset = 0, - .size = sizeof(int), - }, - vk::SpecializationMapEntry{ - .constantID = 1, - .offset = sizeof(int), - .size = sizeof(int), - }, - vk::SpecializationMapEntry{ - .constantID = 2, - .offset = 2 * sizeof(int), - .size = sizeof(int), - }, - vk::SpecializationMapEntry{ - .constantID = 3, - .offset = 3 * sizeof(int), - .size = sizeof(int), - }, - }; - - vk::SpecializationInfo vertex_specialization_info; - vertex_specialization_info.setMapEntries(vertex_specialization_constants_desc); - vertex_specialization_info.setData(vertex_specialization_constants); - // Fragment shader std::string fragment_shader_name = "reprojection.frag"; @@ -337,7 +277,6 @@ stream_reprojection::stream_reprojection( .stage = vk::ShaderStageFlagBits::eVertex, .module = *vertex_shader, .pName = "main", - .pSpecializationInfo = &vertex_specialization_info, }, { .stage = vk::ShaderStageFlagBits::eFragment, @@ -346,22 +285,41 @@ stream_reprojection::stream_reprojection( .pSpecializationInfo = &fragment_specialization_info, }, }, - .VertexBindingDescriptions = {}, - .VertexAttributeDescriptions = {}, + .VertexBindingDescriptions = { + { + .binding = 0, + .stride = sizeof(vertex), + .inputRate = vk::VertexInputRate::eVertex, + }, + }, + .VertexAttributeDescriptions = { + { + .location = 0, + .binding = 0, + .format = vk::Format::eR32G32Sfloat, + .offset = offsetof(vertex, position), + }, + { + .location = 1, + .binding = 0, + .format = vk::Format::eR32G32Sfloat, + .offset = offsetof(vertex, uv), + }, + }, .InputAssemblyState = {{ - .topology = vk::PrimitiveTopology::eTriangleList, + .topology = vk::PrimitiveTopology::eTriangleStrip, }}, .Viewports = {{ .x = 0, .y = 0, - .width = (float)extent.width, - .height = (float)extent.height, + .width = (float)output_extent.width, + .height = (float)output_extent.height, .minDepth = 0, .maxDepth = 1, }}, .Scissors = {{ .offset = {.x = 0, .y = 0}, - .extent = extent, + .extent = output_extent, }}, .RasterizationState = {{ .polygonMode = vk::PolygonMode::eFill, @@ -411,8 +369,8 @@ stream_reprojection::stream_reprojection( vk::FramebufferCreateInfo fb_create_info{ .renderPass = *renderpass, - .width = extent.width, - .height = extent.height, + .width = output_extent.width, + .height = output_extent.height, .layers = 1, }; fb_create_info.setAttachments(*output_image_views.back()); @@ -422,28 +380,82 @@ stream_reprojection::stream_reprojection( } } -void stream_reprojection::reproject(vk::raii::CommandBuffer & command_buffer, int destination) +static size_t required_vertices(const wivrn::to_headset::foveation_parameter & p) +{ + // strips are constructed like this: + // 0 2 4 + // 1 3 5 5* + // there is one such line per value in y + // the last element is repeated to break the line + return (2 * (p.x.size() + 1) + 1) * p.y.size(); +} + +std::vector stream_reprojection::reproject(vk::raii::CommandBuffer & command_buffer, + const std::array & foveation, + int destination) { if (destination < 0 || destination >= (int)output_images.size()) throw std::runtime_error("Invalid destination image index"); + ensure_vertices(std::max(required_vertices(foveation[0]), required_vertices(foveation[1]))); + std::vector out_extents(view_count, XrExtent2Di{}); + for (size_t view = 0; view < view_count; ++view) { - auto & ubo = *reinterpret_cast(reinterpret_cast(buffer.map()) + view * uniform_size); - if (foveation_parameters[view].x.scale < 1) + auto vertices = get_vertices(view); + const auto & [px, py] = foveation[view]; + assert(px.size() % 2 == 1); + assert(py.size() % 2 == 1); + const int n_ratio_y = (py.size() - 1) / 2; + const int n_ratio_x = (px.size() - 1) / 2; + + for (auto [ix, n_out_x]: utils::enumerate_range(px)) { - ubo.a.x = foveation_parameters[view].x.a; - ubo.b.x = foveation_parameters[view].x.b; - ubo.lambda.x = foveation_parameters[view].x.scale / foveation_parameters[view].x.a; - ubo.xc.x = foveation_parameters[view].x.center; + // number of output pixels per source pixels + const int ratio_x = std::abs(n_ratio_x - int(ix)) + 1; + out_extents[view].width += ratio_x * n_out_x; } - if (foveation_parameters[view].y.scale < 1) + glm::vec2 in(0), out(0); // pixel coordinates + glm::vec2 in_pixel_size(1. / input_extent.width, + 1. / input_extent.height); + glm::vec2 out_pixel_size(2. / output_extent.width, + 2. / output_extent.height); + for (auto [iy, n_out_y]: utils::enumerate_range(py)) { - ubo.a.y = foveation_parameters[view].y.a; - ubo.b.y = foveation_parameters[view].y.b; - ubo.lambda.y = foveation_parameters[view].y.scale / foveation_parameters[view].y.a; - ubo.xc.y = foveation_parameters[view].y.center; + // number of output pixels per source pixels + const int ratio_y = std::abs(n_ratio_y - int(iy)) + 1; + out_extents[view].height += ratio_y * n_out_y; + in.x = 0; + out.x = 0; + for (auto [ix, n_out_x]: utils::enumerate_range(px)) + { + const int ratio_x = std::abs(n_ratio_x - int(ix)) + 1; + *vertices++ = { + .position = out * out_pixel_size - glm::vec2(1), + .uv = in * in_pixel_size, + }; + *vertices++ = { + .position = (out + glm::vec2(0, n_out_y * ratio_y)) * out_pixel_size - glm::vec2(1), + .uv = (in + glm::vec2(0, n_out_y)) * in_pixel_size, + }; + in.x += n_out_x; + out.x += n_out_x * ratio_x; + } + *vertices++ = { + .position = out * out_pixel_size - glm::vec2(1), + .uv = in * in_pixel_size, + }; + *vertices++ = { + .position = (out + glm::vec2(0, n_out_y * ratio_y)) * out_pixel_size - glm::vec2(1), + .uv = (in + glm::vec2(0, n_out_y)) * in_pixel_size, + }; + *vertices++ = { + .position = (out + glm::vec2(0, n_out_y * ratio_y)) * out_pixel_size - glm::vec2(1), + .uv = (in + glm::vec2(0, n_out_y)) * in_pixel_size, + }; + in.y += n_out_y; + out.y += n_out_y * ratio_y; } } @@ -473,19 +485,16 @@ void stream_reprojection::reproject(vk::raii::CommandBuffer & command_buffer, in .framebuffer = *framebuffers[destination * view_count + view], .renderArea = { .offset = {0, 0}, - .extent = extent, + .extent = output_extent, }, }; command_buffer.beginRenderPass(begin_info, vk::SubpassContents::eInline); command_buffer.bindPipeline(vk::PipelineBindPoint::eGraphics, *pipeline); command_buffer.bindDescriptorSets(vk::PipelineBindPoint::eGraphics, *layout, 0, descriptor_sets[view], {}); - command_buffer.draw(6 * nb_reprojection_vertices * nb_reprojection_vertices, 1, 0, 0); + command_buffer.bindVertexBuffers(0, vk::Buffer(buffer), vertices_size * view); + command_buffer.draw(required_vertices(foveation[view]), 1, 0, 0); command_buffer.endRenderPass(); } -} - -void stream_reprojection::set_foveation(std::array foveation) -{ - foveation_parameters = foveation; + return out_extents; } diff --git a/client/scenes/stream_reprojection.h b/client/scenes/stream_reprojection.h index ee755b07..72ef1ad8 100644 --- a/client/scenes/stream_reprojection.h +++ b/client/scenes/stream_reprojection.h @@ -26,10 +26,13 @@ class stream_reprojection { + struct vertex; const uint32_t view_count; - // Uniform buffer + // Vertex buffer buffer_allocation buffer; - size_t uniform_size; + size_t vertices_size = 0; + + vk::raii::Device & device; // Graphic pipeline vk::raii::DescriptorSetLayout descriptor_set_layout = nullptr; @@ -43,32 +46,32 @@ class stream_reprojection vk::Image input_image; std::vector input_image_views; std::vector descriptor_sets; + vk::Extent2D input_extent; // Destination images std::vector output_images; std::vector output_image_views; std::vector framebuffers; - vk::Extent2D extent; + vk::Extent2D output_extent; - // Foveation - std::array foveation_parameters; + void ensure_vertices(size_t num_vertices); + vertex * get_vertices(size_t view); public: stream_reprojection( vk::raii::Device & device, vk::raii::PhysicalDevice & physical_device, vk::Image input_image, + vk::Extent2D input_extent, uint32_t view_count, std::vector output_images, - vk::Extent2D extent, - vk::Format format, - const wivrn::to_headset::video_stream_description & description); + vk::Extent2D output_extent, + vk::Format format); stream_reprojection(const stream_reprojection &) = delete; - void reproject( + std::vector reproject( vk::raii::CommandBuffer & command_buffer, + const std::array & foveation, int destination); - - void set_foveation(std::array foveation); }; diff --git a/client/shaders/reprojection.glsl b/client/shaders/reprojection.glsl index 6ab6dbda..119ec2c1 100644 --- a/client/shaders/reprojection.glsl +++ b/client/shaders/reprojection.glsl @@ -21,56 +21,15 @@ #ifdef VERT_SHADER -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(set = 0, binding = 1) uniform UniformBufferObject -{ - vec2 a; - vec2 b; - vec2 lambda; - vec2 xc; -} -ubo; +layout (location = 0) in vec2 vPosition; +layout (location = 1) in vec2 vUV; layout(location = 0) out vec2 outUV; -vec2 positions[6] = vec2[]( - vec2(0, 0), vec2(1, 0), vec2(0, 1), - vec2(1, 0), vec2(0, 1), vec2(1, 1)); - -vec2 unfoveate(vec2 uv) -{ - uv = 2 * uv - 1; - if (use_foveation_x && use_foveation_y) - { - uv = ubo.lambda * tan(ubo.a * uv + ubo.b) + ubo.xc; - } - else - { - if (use_foveation_x) - { - uv.x = (ubo.lambda * tan(ubo.a * uv + ubo.b) + ubo.xc).y; - } - if (use_foveation_y) - { - uv.y = (ubo.lambda * tan(ubo.a * uv + ubo.b) + ubo.xc).y; - } - } - return uv; -} - void main() { - vec2 quad_size = 1 / vec2(nb_x, nb_y); - int cell_id = gl_VertexIndex / 6; - - vec2 top_left = quad_size * vec2(cell_id % nb_x, cell_id / nb_x); - outUV = top_left + positions[gl_VertexIndex % 6] * quad_size; - - gl_Position = vec4(unfoveate(outUV), 0.0, 1.0); + gl_Position = vec4(vPosition, 0.0, 1.0); + outUV = vUV; } #endif diff --git a/common/wivrn_packets.h b/common/wivrn_packets.h index 26fb076f..a44a3f64 100644 --- a/common/wivrn_packets.h +++ b/common/wivrn_packets.h @@ -439,18 +439,20 @@ struct handshake int stream_port; }; -struct foveation_parameter_item -{ - float center; - float scale; - float a; - float b; -}; - struct foveation_parameter { - foveation_parameter_item x; - foveation_parameter_item y; + // The number of source pixels for each ratio, + // the middle one is 1:1 + // + // how to read it: + // 1, 4, 5, 3, 1 means: + // the first output pixel has 3 source pixels + // the 4 that come after have 2 source pixels + // then 5 with 1 source pixel + // 3 with 2 source pixels + // 1 with 3 source pixels + std::vector x; + std::vector y; }; struct audio_stream_description @@ -490,7 +492,8 @@ struct video_stream_description uint16_t width; uint16_t height; float fps; - std::array foveation; + uint16_t defoveated_width; + uint16_t defoveated_height; std::vector items; }; diff --git a/patches/monado/0001-c-multi-early-wake-of-compositor.patch b/patches/monado/0001-c-multi-early-wake-of-compositor.patch index 70868313..81ae48b4 100644 --- a/patches/monado/0001-c-multi-early-wake-of-compositor.patch +++ b/patches/monado/0001-c-multi-early-wake-of-compositor.patch @@ -1,7 +1,7 @@ -From 239084ca0a8c5fbc9b3d9efdf9121a60be399650 Mon Sep 17 00:00:00 2001 +From 75d2fe061edd2a268ea9f5fbedc90efd8ffea519 Mon Sep 17 00:00:00 2001 From: Patrick Nicolas Date: Tue, 22 Oct 2024 15:23:19 +0200 -Subject: [PATCH 01/10] c/multi: early wake of compositor +Subject: [PATCH 01/11] c/multi: early wake of compositor --- .../compositor/multi/comp_multi_compositor.c | 14 +++++++++ diff --git a/patches/monado/0002-ipc-server-Always-listen-to-stdin.patch b/patches/monado/0002-ipc-server-Always-listen-to-stdin.patch index 71134c2d..aa92ea9a 100644 --- a/patches/monado/0002-ipc-server-Always-listen-to-stdin.patch +++ b/patches/monado/0002-ipc-server-Always-listen-to-stdin.patch @@ -1,7 +1,7 @@ -From 86c09de420ebd47d57bfedc2374c57df8d82aa5a Mon Sep 17 00:00:00 2001 +From 3a8d64c0f569425e2f07b4c2a7803c9bfbc224df Mon Sep 17 00:00:00 2001 From: Patrick Nicolas Date: Tue, 2 Apr 2024 17:14:42 +0200 -Subject: [PATCH 02/10] ipc/server: Always listen to stdin +Subject: [PATCH 02/11] ipc/server: Always listen to stdin --- src/xrt/ipc/server/ipc_server_mainloop_linux.c | 2 +- diff --git a/patches/monado/0003-Use-extern-socket-fd.patch b/patches/monado/0003-Use-extern-socket-fd.patch index f2ffdf3d..91dac0c2 100644 --- a/patches/monado/0003-Use-extern-socket-fd.patch +++ b/patches/monado/0003-Use-extern-socket-fd.patch @@ -1,7 +1,7 @@ -From 49c6eaabb908fca0614f11cd23b82db3d57f514e Mon Sep 17 00:00:00 2001 +From 1a8f4aada945314ac852b19ce20c2ed0345f5bb2 Mon Sep 17 00:00:00 2001 From: Patrick Nicolas Date: Tue, 8 Oct 2024 22:13:15 +0200 -Subject: [PATCH 03/10] Use extern socket fd +Subject: [PATCH 03/11] Use extern socket fd --- src/xrt/ipc/server/ipc_server_mainloop_linux.c | 8 ++++++++ diff --git a/patches/monado/0004-c-render-Add-storage-usage-to-distortion-mesh.patch b/patches/monado/0004-c-render-Add-storage-usage-to-distortion-mesh.patch index 56b60c28..ab7de0cc 100644 --- a/patches/monado/0004-c-render-Add-storage-usage-to-distortion-mesh.patch +++ b/patches/monado/0004-c-render-Add-storage-usage-to-distortion-mesh.patch @@ -1,7 +1,7 @@ -From 47c5d07b6974f42714db8433677c158cd5828a94 Mon Sep 17 00:00:00 2001 +From 2f38475c9b51f2d2e92f2fd3d51dcef6d7a53e83 Mon Sep 17 00:00:00 2001 From: galister <3123227-galister@users.noreply.gitlab.com> Date: Mon, 19 Aug 2024 18:51:45 +0900 -Subject: [PATCH 04/10] c/render: Add storage usage to distortion mesh +Subject: [PATCH 04/11] c/render: Add storage usage to distortion mesh --- src/xrt/compositor/render/render_distortion.c | 2 +- diff --git a/patches/monado/0005-change-environment-blend-mode-selection-logic.patch b/patches/monado/0005-change-environment-blend-mode-selection-logic.patch index c59774a1..1dddb321 100644 --- a/patches/monado/0005-change-environment-blend-mode-selection-logic.patch +++ b/patches/monado/0005-change-environment-blend-mode-selection-logic.patch @@ -1,7 +1,7 @@ -From d0c421a582c8e9429b237bc08829f6882185a6fe Mon Sep 17 00:00:00 2001 +From 15eec19c226eb78cb9bc7ebd0021ab226402245a Mon Sep 17 00:00:00 2001 From: galister <85970-galister@users.noreply.gitlab.freedesktop.org> Date: Sat, 23 Nov 2024 02:42:12 +0900 -Subject: [PATCH 05/10] change environment blend mode selection logic +Subject: [PATCH 05/11] change environment blend mode selection logic --- src/xrt/compositor/multi/comp_multi_system.c | 26 +++++++++++++------- diff --git a/patches/monado/0006-Convert-to-YCbCr-in-Monado.patch b/patches/monado/0006-Convert-to-YCbCr-in-Monado.patch index f7f74679..69665f7c 100644 --- a/patches/monado/0006-Convert-to-YCbCr-in-Monado.patch +++ b/patches/monado/0006-Convert-to-YCbCr-in-Monado.patch @@ -1,7 +1,7 @@ -From dc2780aa2e27e00ec38442490e1b24728b30c6c1 Mon Sep 17 00:00:00 2001 +From 06497b57fc56d48611ffc9b6f1bd431141097174 Mon Sep 17 00:00:00 2001 From: Patrick Nicolas Date: Wed, 11 Sep 2024 22:56:29 +0200 -Subject: [PATCH 06/10] Convert to YCbCr in Monado +Subject: [PATCH 06/11] Convert to YCbCr in Monado --- src/xrt/compositor/main/comp_renderer.c | 6 +- diff --git a/patches/monado/0007-store-alpha-channel-in-layer-1.patch b/patches/monado/0007-store-alpha-channel-in-layer-1.patch index 1e73aeec..fe6754ae 100644 --- a/patches/monado/0007-store-alpha-channel-in-layer-1.patch +++ b/patches/monado/0007-store-alpha-channel-in-layer-1.patch @@ -1,7 +1,7 @@ -From b40c83a38d13fe511c9dceadeb23dd4fd2cc77f4 Mon Sep 17 00:00:00 2001 +From 2be6264cd7091d593cfec71b7952ff2409ceae4d Mon Sep 17 00:00:00 2001 From: Patrick Nicolas Date: Sun, 3 Nov 2024 13:40:00 +0100 -Subject: [PATCH 07/10] store alpha channel in layer 1 +Subject: [PATCH 07/11] store alpha channel in layer 1 --- src/xrt/compositor/shaders/clear.comp | 4 ++-- diff --git a/patches/monado/0008-st-oxr-bind-to-dynamic-role-s-profile.patch b/patches/monado/0008-st-oxr-bind-to-dynamic-role-s-profile.patch index 686308ac..ed9b82ce 100644 --- a/patches/monado/0008-st-oxr-bind-to-dynamic-role-s-profile.patch +++ b/patches/monado/0008-st-oxr-bind-to-dynamic-role-s-profile.patch @@ -1,7 +1,7 @@ -From ee2d823a7959b01b47cdfc03287ffd75361d5b7d Mon Sep 17 00:00:00 2001 +From f5f2fafe497c688170e176ee4e47b9d9dce8ab7b Mon Sep 17 00:00:00 2001 From: Patrick Nicolas Date: Wed, 19 Feb 2025 10:13:36 +0100 -Subject: [PATCH 08/10] st/oxr: bind to dynamic role's profile +Subject: [PATCH 08/11] st/oxr: bind to dynamic role's profile --- src/xrt/state_trackers/oxr/oxr_binding.c | 17 +++++++++++++++++ diff --git a/patches/monado/0009-c-main-allow-custom-pacing-app-factory.patch b/patches/monado/0009-c-main-allow-custom-pacing-app-factory.patch index 298cf2d5..72e86a6d 100644 --- a/patches/monado/0009-c-main-allow-custom-pacing-app-factory.patch +++ b/patches/monado/0009-c-main-allow-custom-pacing-app-factory.patch @@ -1,7 +1,7 @@ -From 8c1dc166ecef3c51c37d78859690cbfd8338d636 Mon Sep 17 00:00:00 2001 +From a906c4d2bc48306a60cd38ceef7dda9d3c7ab0ab Mon Sep 17 00:00:00 2001 From: Patrick Nicolas Date: Thu, 27 Feb 2025 22:45:36 +0100 -Subject: [PATCH 09/10] c/main: allow custom pacing app factory +Subject: [PATCH 09/11] c/main: allow custom pacing app factory --- src/xrt/compositor/main/comp_compositor.c | 10 ++++++---- diff --git a/patches/monado/0010-st-oxr-forward-0-refresh-rate.patch b/patches/monado/0010-st-oxr-forward-0-refresh-rate.patch index 9791dc4f..a00455f9 100644 --- a/patches/monado/0010-st-oxr-forward-0-refresh-rate.patch +++ b/patches/monado/0010-st-oxr-forward-0-refresh-rate.patch @@ -1,7 +1,7 @@ -From 837e5f090583f5c0f7f8c1a06ecc032b4b3733a4 Mon Sep 17 00:00:00 2001 +From aab3a63932eafc7781d6ebbcbb3e825beb0ef4de Mon Sep 17 00:00:00 2001 From: Patrick Nicolas Date: Sat, 15 Mar 2025 22:19:48 +0100 -Subject: [PATCH 10/10] st/oxr: forward 0 refresh rate +Subject: [PATCH 10/11] st/oxr: forward 0 refresh rate --- src/xrt/state_trackers/oxr/oxr_api_session.c | 6 +----- diff --git a/patches/monado/0011-Replace-distortion-with-pixel-aligned-foveation.patch b/patches/monado/0011-Replace-distortion-with-pixel-aligned-foveation.patch new file mode 100644 index 00000000..3af6b980 --- /dev/null +++ b/patches/monado/0011-Replace-distortion-with-pixel-aligned-foveation.patch @@ -0,0 +1,700 @@ +From e3efcbec31cfc515be3316d51d9e65bd51f4fe07 Mon Sep 17 00:00:00 2001 +From: Patrick Nicolas +Date: Mon, 2 Jun 2025 14:51:11 +0200 +Subject: [PATCH 11/11] Replace distortion with pixel-aligned foveation + +--- + src/xrt/compositor/CMakeLists.txt | 2 +- + src/xrt/compositor/render/render_compute.c | 181 ++----------------- + src/xrt/compositor/render/render_interface.h | 33 ++-- + src/xrt/compositor/render/render_resources.c | 71 +------- + src/xrt/compositor/shaders/distortion.comp | 134 ++++---------- + src/xrt/compositor/util/comp_render_cs.c | 33 +--- + 6 files changed, 76 insertions(+), 378 deletions(-) + +diff --git a/src/xrt/compositor/CMakeLists.txt b/src/xrt/compositor/CMakeLists.txt +index f1c54ed4d..a2f7ba5cc 100644 +--- a/src/xrt/compositor/CMakeLists.txt ++++ b/src/xrt/compositor/CMakeLists.txt +@@ -115,7 +115,7 @@ if(XRT_HAVE_VULKAN) + ${SHADER_HEADERS} + render/render_buffer.c + render/render_compute.c +- render/render_distortion.c ++ # render/render_distortion.c implemented in wivrn + render/render_gfx.c + render/render_interface.h + render/render_resources.c +diff --git a/src/xrt/compositor/render/render_compute.c b/src/xrt/compositor/render/render_compute.c +index fd04801d4..5cd5a1726 100644 +--- a/src/xrt/compositor/render/render_compute.c ++++ b/src/xrt/compositor/render/render_compute.c +@@ -167,8 +167,8 @@ update_compute_shared_descriptor_set(struct vk_bundle *vk, + VkSampler src_samplers[XRT_MAX_VIEWS], + VkImageView src_image_views[XRT_MAX_VIEWS], + uint32_t distortion_binding, +- VkSampler distortion_samplers[3 * XRT_MAX_VIEWS], +- VkImageView distortion_image_views[3 * XRT_MAX_VIEWS], ++ VkDeviceSize foveation_size, ++ VkBuffer foveation_buffer, + uint32_t target_binding_y, + uint32_t target_binding_cbcr, + VkImageView target_image_view_y, +@@ -186,12 +186,11 @@ update_compute_shared_descriptor_set(struct vk_bundle *vk, + src_image_info[i].imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; + } + +- VkDescriptorImageInfo distortion_image_info[3 * XRT_MAX_VIEWS]; +- for (uint32_t i = 0; i < 3 * view_count; ++i) { +- distortion_image_info[i].sampler = distortion_samplers[i]; +- distortion_image_info[i].imageView = distortion_image_views[i]; +- distortion_image_info[i].imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; +- } ++ VkDescriptorBufferInfo foveation_buffer_info = { ++ .buffer = foveation_buffer, ++ .offset = 0, ++ .range = foveation_size, ++ }; + + VkDescriptorImageInfo target_image_y_info = { + .imageView = target_image_view_y, +@@ -222,9 +221,9 @@ update_compute_shared_descriptor_set(struct vk_bundle *vk, + .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, + .dstSet = descriptor_set, + .dstBinding = distortion_binding, +- .descriptorCount = 3 * view_count, +- .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, +- .pImageInfo = distortion_image_info, ++ .descriptorCount = 1, ++ .descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, ++ .pBufferInfo = &foveation_buffer_info, + }, + { + .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, +@@ -483,146 +482,6 @@ render_compute_layers(struct render_compute *render, + 1); // groupCountZ + } + +-void +-render_compute_projection_timewarp(struct render_compute *render, +- VkSampler src_samplers[XRT_MAX_VIEWS], +- VkImageView src_image_views[XRT_MAX_VIEWS], +- const struct xrt_normalized_rect src_norm_rects[XRT_MAX_VIEWS], +- const struct xrt_pose src_poses[XRT_MAX_VIEWS], +- const struct xrt_fov src_fovs[XRT_MAX_VIEWS], +- const struct xrt_pose new_poses[XRT_MAX_VIEWS], +- VkImage target_image, +- VkImageView target_image_view_y, +- VkImageView target_image_view_cbcr, +- const struct render_viewport_data views[XRT_MAX_VIEWS]) +-{ +- assert(render->r != NULL); +- +- struct vk_bundle *vk = vk_from_render(render); +- struct render_resources *r = render->r; +- +- +- /* +- * UBO +- */ +- +- struct xrt_matrix_4x4 time_warp_matrix[XRT_MAX_VIEWS]; +- for (uint32_t i = 0; i < render->r->view_count; ++i) { +- render_calc_time_warp_matrix( // +- &src_poses[i], // +- &src_fovs[i], // +- &new_poses[i], // +- &time_warp_matrix[i]); // +- } +- +- struct render_compute_distortion_ubo_data *data = +- (struct render_compute_distortion_ubo_data *)r->compute.distortion.ubo.mapped; +- for (uint32_t i = 0; i < render->r->view_count; ++i) { +- data->views[i] = views[i]; +- data->pre_transforms[i] = r->distortion.uv_to_tanangle[i]; +- data->transforms[i] = time_warp_matrix[i]; +- data->post_transforms[i] = src_norm_rects[i]; +- } +- +- /* +- * Source, target and distortion images. +- */ +- +- VkImageSubresourceRange subresource_range = { +- .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, +- .baseMipLevel = 0, +- .levelCount = VK_REMAINING_MIP_LEVELS, +- .baseArrayLayer = 0, +- .layerCount = VK_REMAINING_ARRAY_LAYERS, +- }; +- +- vk_cmd_image_barrier_gpu_locked( // +- vk, // +- r->cmd, // +- target_image, // +- 0, // +- VK_ACCESS_SHADER_WRITE_BIT, // +- VK_IMAGE_LAYOUT_UNDEFINED, // +- VK_IMAGE_LAYOUT_GENERAL, // +- subresource_range); // +- +- VkSampler sampler = r->samplers.clamp_to_edge; +- VkSampler distortion_samplers[3 * XRT_MAX_VIEWS]; +- for (uint32_t i = 0; i < render->r->view_count; ++i) { +- distortion_samplers[3 * i + 0] = sampler; +- distortion_samplers[3 * i + 1] = sampler; +- distortion_samplers[3 * i + 2] = sampler; +- } +- +- update_compute_shared_descriptor_set( // +- vk, // +- r->compute.src_binding, // +- src_samplers, // +- src_image_views, // +- r->compute.distortion_binding, // +- distortion_samplers, // +- r->distortion.image_views, // +- r->compute.target_binding_y, // +- r->compute.target_binding_cbcr, // +- target_image_view_y, // +- target_image_view_cbcr, // +- r->compute.ubo_binding, // +- r->compute.distortion.ubo.buffer, // +- VK_WHOLE_SIZE, // +- render->shared_descriptor_set, // +- render->r->view_count); // +- +- vk->vkCmdBindPipeline( // +- r->cmd, // +- VK_PIPELINE_BIND_POINT_COMPUTE, // pipelineBindPoint +- r->compute.distortion.timewarp_pipeline); // pipeline +- +- vk->vkCmdBindDescriptorSets( // +- r->cmd, // +- VK_PIPELINE_BIND_POINT_COMPUTE, // pipelineBindPoint +- r->compute.distortion.pipeline_layout, // layout +- 0, // firstSet +- 1, // descriptorSetCount +- &render->shared_descriptor_set, // pDescriptorSets +- 0, // dynamicOffsetCount +- NULL); // pDynamicOffsets +- +- +- uint32_t w = 0, h = 0; +- calc_dispatch_dims_views(views, render->r->view_count, &w, &h); +- assert(w != 0 && h != 0); +- +- vk->vkCmdDispatch( // +- r->cmd, // +- w, // groupCountX +- h, // groupCountY +- 2); // groupCountZ +- +- VkImageMemoryBarrier memoryBarrier = { +- .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, +- .srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT, +- .dstAccessMask = VK_ACCESS_MEMORY_READ_BIT, +- .oldLayout = VK_IMAGE_LAYOUT_GENERAL, +- .newLayout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, +- .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, +- .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, +- .image = target_image, +- .subresourceRange = subresource_range, +- }; +- +- vk->vkCmdPipelineBarrier( // +- r->cmd, // +- VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, // +- VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, // +- 0, // +- 0, // +- NULL, // +- 0, // +- NULL, // +- 1, // +- &memoryBarrier); // +-} +- + void + render_compute_projection(struct render_compute *render, + VkSampler src_samplers[XRT_MAX_VIEWS], +@@ -673,22 +532,14 @@ render_compute_projection(struct render_compute *render, + VK_IMAGE_LAYOUT_GENERAL, // + subresource_range); // + +- VkSampler sampler = r->samplers.clamp_to_edge; +- VkSampler distortion_samplers[3 * XRT_MAX_VIEWS]; +- for (uint32_t i = 0; i < render->r->view_count; ++i) { +- distortion_samplers[3 * i + 0] = sampler; +- distortion_samplers[3 * i + 1] = sampler; +- distortion_samplers[3 * i + 2] = sampler; +- } +- + update_compute_shared_descriptor_set( // + vk, // + r->compute.src_binding, // + src_samplers, // + src_image_views, // + r->compute.distortion_binding, // +- distortion_samplers, // +- r->distortion.image_views, // ++ VK_WHOLE_SIZE, // ++ r->distortion.buffer, // + r->compute.target_binding_y, // + r->compute.target_binding_cbcr, // + target_image_view_y, // +@@ -804,13 +655,9 @@ render_compute_clear(struct render_compute *render, + VkSampler sampler = r->samplers.mock; + VkSampler src_samplers[XRT_MAX_VIEWS]; + VkImageView src_image_views[XRT_MAX_VIEWS]; +- VkSampler distortion_samplers[3 * XRT_MAX_VIEWS]; + for (uint32_t i = 0; i < render->r->view_count; ++i) { + src_samplers[i] = sampler; + src_image_views[i] = r->mock.color.image_view; +- distortion_samplers[3 * i + 0] = sampler; +- distortion_samplers[3 * i + 1] = sampler; +- distortion_samplers[3 * i + 2] = sampler; + } + + update_compute_shared_descriptor_set( // +@@ -819,8 +666,8 @@ render_compute_clear(struct render_compute *render, + src_samplers, // + src_image_views, // + r->compute.distortion_binding, // +- distortion_samplers, // +- r->distortion.image_views, // ++ VK_WHOLE_SIZE, // ++ r->distortion.buffer, // + r->compute.target_binding_y, // + r->compute.target_binding_cbcr, // + target_image_view_y, // +diff --git a/src/xrt/compositor/render/render_interface.h b/src/xrt/compositor/render/render_interface.h +index 2f8ce5287..d74ec78dd 100644 +--- a/src/xrt/compositor/render/render_interface.h ++++ b/src/xrt/compositor/render/render_interface.h +@@ -74,12 +74,8 @@ extern "C" { + #define RENDER_MAX_LAYER_RUNS_SIZE (XRT_MAX_VIEWS) + #define RENDER_MAX_LAYER_RUNS_COUNT(RENDER_RESOURCES) (RENDER_RESOURCES->view_count) + +-//! Distortion image dimension in pixels +-#define RENDER_DISTORTION_IMAGE_DIMENSIONS (128) +- +-//! How many distortion images we have, one for each channel (3 rgb) and per view. +-#define RENDER_DISTORTION_IMAGES_SIZE (3 * XRT_MAX_VIEWS) +-#define RENDER_DISTORTION_IMAGES_COUNT(RENDER_RESOURCES) (3 * RENDER_RESOURCES->view_count) ++//! Foveation buffer dimension in pixels (including past-the end pixel) ++#define RENDER_FOVEATION_BUFFER_DIMENSIONS (4096 + 1) + + //! The binding that the layer projection and quad shader have their UBO on. + #define RENDER_BINDING_LAYER_SHARED_UBO 0 +@@ -519,9 +515,6 @@ struct render_resources + //! Doesn't depend on target so is static. + VkPipeline pipeline; + +- //! Doesn't depend on target so is static. +- VkPipeline timewarp_pipeline; +- + //! Target info. + struct render_buffer ubo; + } distortion; +@@ -543,17 +536,11 @@ struct render_resources + //! Transform to go from UV to tangle angles. + struct xrt_normalized_rect uv_to_tanangle[XRT_MAX_VIEWS]; + +- //! Backing memory to distortion images. +- VkDeviceMemory device_memories[RENDER_DISTORTION_IMAGES_SIZE]; +- +- //! Distortion images. +- VkImage images[RENDER_DISTORTION_IMAGES_SIZE]; ++ //! Backing memory to foveation buffers. ++ VkDeviceMemory device_memory; + +- //! The views into the distortion images. +- VkImageView image_views[RENDER_DISTORTION_IMAGES_SIZE]; +- +- //! Whether distortion images have been pre-rotated 90 degrees. +- bool pre_rotated; ++ //! Foveation buffer. ++ VkBuffer buffer; + } distortion; + }; + +@@ -1298,6 +1285,14 @@ struct render_compute_distortion_ubo_data + struct xrt_matrix_4x4 transforms[XRT_MAX_VIEWS]; + }; + ++struct render_compute_distortion_foveation_data ++{ ++ // For each output pixel: index of the first pixel to read. ++ // contains 1 additional value at the end for past-the end pixel ++ uint32_t x[XRT_MAX_VIEWS * RENDER_FOVEATION_BUFFER_DIMENSIONS]; ++ uint32_t y[XRT_MAX_VIEWS * RENDER_FOVEATION_BUFFER_DIMENSIONS]; ++}; ++ + /*! + * Init struct and create resources needed for compute rendering. + * +diff --git a/src/xrt/compositor/render/render_resources.c b/src/xrt/compositor/render/render_resources.c +index 5b9addd59..7d4d9cddb 100644 +--- a/src/xrt/compositor/render/render_resources.c ++++ b/src/xrt/compositor/render/render_resources.c +@@ -258,8 +258,8 @@ create_compute_distortion_descriptor_set_layout(struct vk_bundle *vk, + }, + { + .binding = distortion_binding, +- .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, +- .descriptorCount = 6, ++ .descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, ++ .descriptorCount = 1, + .stageFlags = VK_SHADER_STAGE_COMPUTE_BIT, + }, + { +@@ -309,12 +309,6 @@ struct compute_layer_params + uint32_t image_array_size; + }; + +-struct compute_distortion_params +-{ +- uint32_t distortion_texel_count; +- VkBool32 do_timewarp; +-}; +- + XRT_CHECK_RESULT static VkResult + create_compute_layer_pipeline(struct vk_bundle *vk, + VkPipelineCache pipeline_cache, +@@ -359,35 +353,15 @@ create_compute_distortion_pipeline(struct vk_bundle *vk, + VkPipelineCache pipeline_cache, + VkShaderModule shader, + VkPipelineLayout pipeline_layout, +- const struct compute_distortion_params *params, + VkPipeline *out_compute_pipeline) + { +-#define ENTRY(ID, FIELD) \ +- { \ +- .constantID = ID, \ +- .offset = offsetof(struct compute_distortion_params, FIELD), \ +- sizeof(params->FIELD), \ +- } +- +- VkSpecializationMapEntry entries[2] = { +- ENTRY(0, distortion_texel_count), +- ENTRY(1, do_timewarp), +- }; +-#undef ENTRY +- +- VkSpecializationInfo specialization_info = { +- .mapEntryCount = ARRAY_SIZE(entries), +- .pMapEntries = entries, +- .dataSize = sizeof(*params), +- .pData = params, +- }; + + return vk_create_compute_pipeline( // + vk, // vk_bundle + pipeline_cache, // pipeline_cache + shader, // shader + pipeline_layout, // pipeline_layout +- &specialization_info, // specialization_info ++ NULL, // specialization_info + out_compute_pipeline); // out_compute_pipeline + } + +@@ -849,9 +823,9 @@ render_resources_init(struct render_resources *r, + struct vk_descriptor_pool_info compute_pool_info = { + .uniform_per_descriptor_count = 1, + // layer images +- .sampler_per_descriptor_count = r->compute.layer.image_array_size + RENDER_DISTORTION_IMAGES_COUNT(r), ++ .sampler_per_descriptor_count = r->compute.layer.image_array_size, + .storage_image_per_descriptor_count = 1, +- .storage_buffer_per_descriptor_count = 0, ++ .storage_buffer_per_descriptor_count = 1, + .descriptor_count = compute_descriptor_count, + .freeable = false, + }; +@@ -971,39 +945,16 @@ render_resources_init(struct render_resources *r, + VK_NAME_PIPELINE_LAYOUT(vk, r->compute.distortion.pipeline_layout, + "render_resources compute distortion pipeline layout"); + +- struct compute_distortion_params distortion_params = { +- .distortion_texel_count = RENDER_DISTORTION_IMAGE_DIMENSIONS, +- .do_timewarp = false, +- }; +- + ret = create_compute_distortion_pipeline( // + vk, // vk_bundle + r->pipeline_cache, // pipeline_cache + r->shaders->distortion_comp, // shader + r->compute.distortion.pipeline_layout, // pipeline_layout +- &distortion_params, // params + &r->compute.distortion.pipeline); // out_compute_pipeline + VK_CHK_WITH_RET(ret, "create_compute_distortion_pipeline", false); + + VK_NAME_PIPELINE(vk, r->compute.distortion.pipeline, "render_resources compute distortion pipeline"); + +- struct compute_distortion_params distortion_timewarp_params = { +- .distortion_texel_count = RENDER_DISTORTION_IMAGE_DIMENSIONS, +- .do_timewarp = true, +- }; +- +- ret = create_compute_distortion_pipeline( // +- vk, // vk_bundle +- r->pipeline_cache, // pipeline_cache +- r->shaders->distortion_comp, // shader +- r->compute.distortion.pipeline_layout, // pipeline_layout +- &distortion_timewarp_params, // params +- &r->compute.distortion.timewarp_pipeline); // out_compute_pipeline +- VK_CHK_WITH_RET(ret, "create_compute_distortion_pipeline", false); +- +- VK_NAME_PIPELINE(vk, r->compute.distortion.timewarp_pipeline, +- "render_resources compute distortion timewarp pipeline"); +- + size_t distortion_ubo_size = sizeof(struct render_compute_distortion_ubo_data); + + ret = render_buffer_init( // +@@ -1056,15 +1007,8 @@ render_resources_init(struct render_resources *r, + * Compute distortion textures, not created until later. + */ + +- for (uint32_t i = 0; i < RENDER_DISTORTION_IMAGES_COUNT(r); i++) { +- r->distortion.image_views[i] = VK_NULL_HANDLE; +- } +- for (uint32_t i = 0; i < RENDER_DISTORTION_IMAGES_COUNT(r); i++) { +- r->distortion.images[i] = VK_NULL_HANDLE; +- } +- for (uint32_t i = 0; i < RENDER_DISTORTION_IMAGES_COUNT(r); i++) { +- r->distortion.device_memories[i] = VK_NULL_HANDLE; +- } ++ r->distortion.buffer = VK_NULL_HANDLE; ++ r->distortion.device_memory = VK_NULL_HANDLE; + + + /* +@@ -1141,7 +1085,6 @@ render_resources_fini(struct render_resources *r) + + D(DescriptorSetLayout, r->compute.distortion.descriptor_set_layout); + D(Pipeline, r->compute.distortion.pipeline); +- D(Pipeline, r->compute.distortion.timewarp_pipeline); + D(PipelineLayout, r->compute.distortion.pipeline_layout); + + D(Pipeline, r->compute.clear.pipeline); +diff --git a/src/xrt/compositor/shaders/distortion.comp b/src/xrt/compositor/shaders/distortion.comp +index 9ce46fe6f..f3be617bd 100644 +--- a/src/xrt/compositor/shaders/distortion.comp ++++ b/src/xrt/compositor/shaders/distortion.comp +@@ -7,17 +7,17 @@ + + #include "srgb.inc.glsl" + +- +-// The size of the distortion texture dimensions in texels. +-layout(constant_id = 0) const int distortion_texel_count = 2; +- +-// Should we do timewarp. +-layout(constant_id = 1) const bool do_timewarp = false; +- + layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in; + ++#define MAX_DIM 4097 ++ + layout(set = 0, binding = 0) uniform sampler2D source[2]; +-layout(set = 0, binding = 1) uniform sampler2D distortion[6]; ++layout(set = 0, binding = 1, std430) buffer restrict Foveation ++{ ++ uint x[2*MAX_DIM]; ++ uint y[2*MAX_DIM]; ++} foveation; ++ + layout(set = 0, binding = 2) uniform writeonly restrict image2DArray luma; + layout(set = 0, binding = 3, std140) uniform restrict Config + { +@@ -41,109 +41,40 @@ vec3 rgb_to_ycbcr(vec3 color) + return transpose(color_space) * color + vec3(0, 0.5, 0.5); + } + +-vec2 position_to_uv(ivec2 extent, uint ix, uint iy) +-{ +- // Turn the index into floating point. +- vec2 xy = vec2(float(ix), float(iy)); +- +- // The inverse of the extent of the target image is the pixel size in [0 .. 1] space. +- vec2 extent_pixel_size = vec2(1.0 / float(extent.x), 1.0 / float(extent.y)); +- +- // Per-target pixel we move the size of the pixels. +- vec2 dist_uv = xy * extent_pixel_size; +- +- // Emulate a triangle sample position by offset half target pixel size. +- dist_uv = dist_uv + extent_pixel_size / 2.0; +- +- +- // To correctly sample we need to put position (0, 0) in the +- // middle of the (0, 0) texel in the distortion textures. That's why we +- // offset with half the texel size, pushing all samples into the middle +- // of each texels, a kin to a vertex buffer. We need to put uv coord +- // (1, 1) in the middle of the last texel, that pixel is (size - 1) +- // texels away from the first texel. So we need to scale [0 .. 1] to +- // [0 .. size - 1]. +- +-#define DIM (float(distortion_texel_count)) +-#define STRETCH ((DIM - 1.0) / DIM) +-#define OFFSET (1.0 / (DIM * 2.0)) +- +- dist_uv = (dist_uv * STRETCH) + OFFSET; +- +- return dist_uv; +-} +- +-vec2 transform_uv_subimage(vec2 uv, uint iz) +-{ +- vec2 values = uv; +- +- // To deal with OpenGL flip and sub image view. +- values.xy = values.xy * ubo.post_transform[iz].zw + ubo.post_transform[iz].xy; +- +- // Ready to be used. +- return values.xy; +-} +- +-vec2 transform_uv_timewarp(vec2 uv, uint iz) +-{ +- vec4 values = vec4(uv, -1, 1); +- +- // From uv to tan angle (tangent space). +- values.xy = values.xy * ubo.pre_transform[iz].zw + ubo.pre_transform[iz].xy; +- values.y = -values.y; // Flip to OpenXR coordinate system. +- +- // Timewarp. +- values = ubo.transform[iz] * values; +- values.xy = values.xy * (1.0 / max(values.w, 0.00001)); +- +- // From [-1, 1] to [0, 1] +- values.xy = values.xy * 0.5 + 0.5; +- +- // To deal with OpenGL flip and sub image view. +- values.xy = values.xy * ubo.post_transform[iz].zw + ubo.post_transform[iz].xy; +- +- // Done. +- return values.xy; +-} +- +-vec2 transform_uv(vec2 uv, uint iz) +-{ +- if (do_timewarp) { +- return transform_uv_timewarp(uv, iz); +- } else { +- return transform_uv_subimage(uv, iz); +- } +-} +- + void main() + { + uint iz = gl_GlobalInvocationID.z; + + ivec2 offset = ivec2(ubo.views[iz].xy); + ivec2 extent = ivec2(ubo.views[iz].zw); +- vec2 cbcrs[4]; +- vec4 alpha; ++ vec3 cbcra = vec3(0); + for (int k = 0; k < 2; k += 1) { + uint iy = gl_GlobalInvocationID.y * 2 + k; ++ uint ymin = foveation.y[MAX_DIM * iz + iy]; ++ uint ymax = foveation.y[MAX_DIM * iz + iy + 1]; ++ if (ymin > ymax) ++ { ++ uint tmp = ymin; ++ ymin = ymax; ++ ymax = tmp; ++ } + for (int j = 0; j < 2; j += 1) { + uint ix = gl_GlobalInvocationID.x * 2 + j; + +- vec2 dist_uv = position_to_uv(extent, ix, iy); ++ uint xmin = foveation.x[MAX_DIM * iz + ix]; ++ uint xmax = foveation.x[MAX_DIM * iz + ix + 1]; + +- vec2 uv = texture(distortion[iz + 0], dist_uv).xy; +- vec2 lod = texture(distortion[iz + 2], dist_uv).xy; +- +- // Do any transformation needed. +- uv = transform_uv(uv, iz); +- +- // Sample the source +- vec4 colour = textureLod(source[iz], uv, mix(lod.x, lod.y, 0.5)); +- +- colour.rgb = rgb_to_ycbcr(from_linear_to_srgb(colour.rgb)); ++ vec4 colour = vec4(0); ++ for (uint y = ymin ; y < ymax; y += 1) { ++ for (uint x = xmin ; x < xmax; x += 1) { ++ colour += texelFetch(source[iz], ivec2(x, y), 0); ++ } ++ } ++ colour /= (xmax - xmin) * (ymax - ymin); ++ colour.xyz = rgb_to_ycbcr(from_linear_to_srgb(colour.rgb)); + +- int i = k * 2 + j; +- cbcrs[i] = colour.yz; +- alpha[i] = colour.a; ++ cbcra.xy += colour.yz; ++ cbcra.z += colour.a; + + if (ix < extent.x && iy < extent.y) { + imageStore(luma, ivec3(offset.x + ix, offset.y + iy, 0), vec4(colour.x)); +@@ -153,10 +84,7 @@ void main() + if (gl_GlobalInvocationID.x >= extent.x || gl_GlobalInvocationID.y >= extent.y) { + return; + } +- vec2 cbcr = mix(mix(cbcrs[0], cbcrs[1], 0.5), mix(cbcrs[2], cbcrs[3], 0.5), 0.5); +- imageStore(chroma, ivec3(offset.xy/2 + gl_GlobalInvocationID.xy, 0), vec4(cbcr.x, cbcr.y, 0, 0)); +- ++ imageStore(chroma, ivec3(offset.xy/2 + gl_GlobalInvocationID.xy, 0), vec4(cbcra.x, cbcra.y, 0, 0) / 4); ++ imageStore(luma, ivec3(offset.xy/2 + gl_GlobalInvocationID.xy, 1), vec4(cbcra.z, 0, 0, 0) / 4); + imageStore(chroma, ivec3(offset.xy/2 + gl_GlobalInvocationID.xy, 1), vec4(0.5, 0.5, 0, 0)); +- alpha = vec4(mix(mix(alpha[0], alpha[1], 0.5), mix(alpha[2], alpha[3], 0.5), 0.5)); +- imageStore(luma, ivec3(offset.xy/2 + gl_GlobalInvocationID.xy, 1), alpha); + } +diff --git a/src/xrt/compositor/util/comp_render_cs.c b/src/xrt/compositor/util/comp_render_cs.c +index c06dda63d..a4934890f 100644 +--- a/src/xrt/compositor/util/comp_render_cs.c ++++ b/src/xrt/compositor/util/comp_render_cs.c +@@ -465,30 +465,15 @@ crc_distortion_fast_path(struct render_compute *render, + world_poses[i] = world_pose; + } + +- if (!d->do_timewarp) { +- render_compute_projection( // +- render, // +- src_samplers, // +- src_image_views, // +- src_norm_rects, // +- d->cs.target_image, // +- d->cs.target_unorm_view_y, // +- d->cs.target_unorm_view_cbcr, // +- target_viewport_datas); // +- } else { +- render_compute_projection_timewarp( // +- render, // +- src_samplers, // +- src_image_views, // +- src_norm_rects, // +- src_poses, // +- src_fovs, // +- world_poses, // +- d->cs.target_image, // +- d->cs.target_unorm_view_y, // +- d->cs.target_unorm_view_cbcr, // +- target_viewport_datas); // +- } ++ render_compute_projection( // ++ render, // ++ src_samplers, // ++ src_image_views, // ++ src_norm_rects, // ++ d->cs.target_image, // ++ d->cs.target_unorm_view_y, // ++ d->cs.target_unorm_view_cbcr, // ++ target_viewport_datas); // + } + + +-- +2.49.0 + diff --git a/server/driver/wivrn_comp_target.cpp b/server/driver/wivrn_comp_target.cpp index 0c7184d8..314e9a9b 100644 --- a/server/driver/wivrn_comp_target.cpp +++ b/server/driver/wivrn_comp_target.cpp @@ -109,6 +109,9 @@ static void destroy_images(struct wivrn_comp_target * cn) if (cn->images == nullptr) return; + if (cn->wivrn_bundle) + cn->wivrn_bundle->device.waitIdle(); + cn->psc.status = 1; cn->psc.status.notify_all(); cn->encoder_threads.clear(); @@ -135,7 +138,9 @@ static void create_encoders(wivrn_comp_target * cn) to_headset::video_stream_description & desc = cn->desc; desc.width = cn->width; desc.height = cn->height; - desc.foveation = cn->cnx.set_foveated_size(desc.width, desc.height); + const auto & hmd = cn->cnx.get_hmd().hmd; + desc.defoveated_width = hmd->views[0].display.w_pixels * 2; + desc.defoveated_height = hmd->views[0].display.h_pixels; std::map>> thread_params; @@ -316,11 +321,8 @@ static bool comp_wivrn_init_post_vulkan(struct comp_target * ct, uint32_t prefer U_LOG_E("Failed to create video encoder: %s", e.what()); return false; } - - if (cn->cnx.has_dynamic_foveation()) - { - cn->foveation_renderer = std::make_unique(*cn->wivrn_bundle, cn->command_pool); - } + cn->cnx.set_foveated_size(cn->c->settings.preferred.width, cn->c->settings.preferred.height); + cn->foveation.emplace(*cn->wivrn_bundle, *cn->cnx.get_hmd().hmd); return true; } @@ -609,7 +611,7 @@ static VkResult comp_wivrn_present(struct comp_target * ct, #endif auto & view_info = cn->psc.view_info; - view_info.foveation = cn->cnx.get_foveation_parameters(); + view_info.foveation = cn->foveation->get_parameters(); view_info.display_time = cn->cnx.get_offset().to_headset(info.predicted_display_time); if (view_info.alpha != do_alpha) cn->pacer.reset(); @@ -651,16 +653,47 @@ static void comp_wivrn_flush(struct comp_target * ct) r->StartFrameCapture(NULL, NULL); #endif + vk::CommandBuffer cmd; // apply foveation for current frame - if (cn->cnx.apply_dynamic_foveation()) - // foveation renderer already signaled the semaphore; nothing to do - return; + if (cn->c->base.frame_params.one_projection_layer_fast_path) + { + const auto & data = cn->c->base.layer_accum.layers[0].data; + const auto & hmd = cn->cnx.get_hmd().hmd; + xrt_rect rect[] = {data.proj.v[0].sub.rect, data.proj.v[1].sub.rect}; + xrt_fov fov[] = {data.proj.v[0].fov, data.proj.v[1].fov}; + cmd = cn->foveation->update_foveation_buffer( + ct->c->nr.distortion.buffer, + data.flip_y, + rect, + fov); + } + else + { + const auto & hmd = cn->cnx.get_hmd().hmd; + xrt_rect rect[] = { + {.extent = { + .w = int(hmd->views[0].display.w_pixels), + .h = int(hmd->views[0].display.h_pixels), + }}, + {.extent = { + .w = int(hmd->views[1].display.w_pixels), + .h = int(hmd->views[1].display.h_pixels), + }}, + }; + cmd = cn->foveation->update_foveation_buffer( + ct->c->nr.distortion.buffer, + false, + rect, + hmd->distortion.fov); + } struct vk_bundle * vk = get_vk(cn); vk::Semaphore sem(cn->semaphores.present_complete); vk::SubmitInfo submit_info{ + .commandBufferCount = cmd ? 1u : 0, + .pCommandBuffers = &cmd, .signalSemaphoreCount = 1, .pSignalSemaphores = &sem, }; @@ -753,14 +786,14 @@ static xrt_result_t comp_wivrn_request_refresh_rate(struct comp_target * ct, flo wivrn_comp_target::~wivrn_comp_target() { - if (wivrn_bundle) - wivrn_bundle->device.waitIdle(); destroy_images(this); } static void comp_wivrn_destroy(struct comp_target * ct) { - delete (struct wivrn_comp_target *)ct; + auto cn = (struct wivrn_comp_target *)ct; + destroy_images(cn); + cn->self = nullptr; } static void comp_wivrn_info_gpu(struct comp_target * ct, int64_t frame_id, int64_t gpu_start_ns, int64_t gpu_end_ns, int64_t when_ns) @@ -800,27 +833,6 @@ void wivrn_comp_target::set_bitrate(int bitrate_bps) } } -void wivrn_comp_target::render_dynamic_foveation(std::array foveation) -{ - assert(foveation_renderer); - - vk::Semaphore sem(semaphores.present_complete); - - vk::SubmitInfo submit_info{ - .commandBufferCount = 1, - .pCommandBuffers = &*foveation_renderer->cmd_buf, - .signalSemaphoreCount = 1, - .pSignalSemaphores = &sem, - }; - - foveation_renderer->render_distortion_images(foveation, c->nr.distortion.images, c->nr.distortion.image_views); - - { - scoped_lock lock(c->base.vk.queue_mutex); - wivrn_bundle->queue.submit(submit_info); - } -} - void wivrn_comp_target::set_refresh_rate(float refresh_rate_hz) { U_LOG_I("Refresh rate change from %.0f to %.0f", desc.fps, refresh_rate_hz); diff --git a/server/driver/wivrn_comp_target.h b/server/driver/wivrn_comp_target.h index 9ed0a7a4..76bedf2d 100644 --- a/server/driver/wivrn_comp_target.h +++ b/server/driver/wivrn_comp_target.h @@ -78,6 +78,7 @@ struct pseudo_swapchain struct wivrn_comp_target : public comp_target { + std::shared_ptr self; to_headset::video_stream_description desc{}; wivrn_pacer pacer; @@ -98,7 +99,7 @@ struct wivrn_comp_target : public comp_target std::vector> encoders; wivrn::wivrn_session & cnx; - std::unique_ptr foveation_renderer = nullptr; + std::optional foveation; std::atomic requested_refresh_rate; @@ -109,8 +110,6 @@ struct wivrn_comp_target : public comp_target void reset_encoders(); void set_bitrate(int bitrate_bps); - void render_dynamic_foveation(std::array foveation); - void set_refresh_rate(float); }; diff --git a/server/driver/wivrn_foveation.cpp b/server/driver/wivrn_foveation.cpp index c3673bc6..80158abf 100644 --- a/server/driver/wivrn_foveation.cpp +++ b/server/driver/wivrn_foveation.cpp @@ -1,6 +1,7 @@ /* * WiVRn VR streaming * Copyright (C) 2024 galister + * Copyright (C) 2025 Patrick Nicolas * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -17,6 +18,7 @@ */ #include "render/render_interface.h" +#include "vk/vk_mini_helpers.h" #include "wivrn_foveation.h" @@ -29,200 +31,113 @@ #include #include #include +#include #include #include #include extern const std::map> shaders; -namespace wivrn +bool render_distortion_images_ensure(struct render_resources * r, + struct vk_bundle * vk, + struct xrt_device * xdev, + bool pre_rotate) { - -const uint32_t dispatch_group_count = RENDER_DISTORTION_IMAGE_DIMENSIONS / 8; - -struct FoveationParamsPcs -{ - float a[2]; - float b[2]; - float scale[2]; - float center[2]; -}; - -static const std::array pool_sizes = - { - vk::DescriptorPoolSize{ - .type = vk::DescriptorType::eStorageImage, - .descriptorCount = 2, - }}; - -static const std::array layout_bindings{ - vk::DescriptorSetLayoutBinding{.binding = 0, - .descriptorType = vk::DescriptorType::eStorageImage, - .descriptorCount = 1, - .stageFlags = vk::ShaderStageFlagBits::eCompute}, -}; - -wivrn_foveation_renderer::wivrn_foveation_renderer(wivrn_vk_bundle & vk, vk::raii::CommandPool & cmd_pool) : - vk(vk), - dp(vk.device, - vk::DescriptorPoolCreateInfo{ - .flags = vk::DescriptorPoolCreateFlagBits::eFreeDescriptorSet, - .maxSets = pool_sizes[0].descriptorCount, - .poolSizeCount = pool_sizes.size(), - .pPoolSizes = pool_sizes.data(), - }), - ds_layout(vk.device, vk::DescriptorSetLayoutCreateInfo{.bindingCount = layout_bindings.size(), .pBindings = layout_bindings.data()}) -{ - cmd_buf = std::move(vk.device.allocateCommandBuffers( - {.commandPool = *cmd_pool, - .commandBufferCount = 1})[0]); - - // Pipeline layout + if (r->distortion.buffer == VK_NULL_HANDLE) { - vk::PushConstantRange push_constant_range{ - .stageFlags = vk::ShaderStageFlagBits::eCompute, - .offset = 0, - .size = sizeof(FoveationParamsPcs), - }; - layout = vk.device.createPipelineLayout({ - .setLayoutCount = 1, - .pSetLayouts = &*ds_layout, - .pushConstantRangeCount = 1, - .pPushConstantRanges = &push_constant_range, - }); + return vk_buffer_init(vk, + sizeof(render_compute_distortion_foveation_data), + VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, + VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, + &r->distortion.buffer, + &r->distortion.device_memory); } - - // Pipeline - { - auto & spirv = shaders.at("foveate.comp"); - vk::raii::ShaderModule shader(vk.device, { - .codeSize = spirv.size() * sizeof(uint32_t), - .pCode = spirv.data(), - }); - - float one_over_dim = 1.0 / (RENDER_DISTORTION_IMAGE_DIMENSIONS - 1); - - vk::SpecializationMapEntry specMapEntry{ - .constantID = 0, - .offset = 0, - .size = sizeof(float), - }; - - vk::SpecializationInfo specInfo{ - .mapEntryCount = 1, - .pMapEntries = &specMapEntry, - .dataSize = sizeof(float), - .pData = &one_over_dim, - }; - - pipeline = vk::raii::Pipeline(vk.device, nullptr, vk::ComputePipelineCreateInfo{ - .stage = { - .stage = vk::ShaderStageFlagBits::eCompute, - .module = *shader, - .pName = "main", - .pSpecializationInfo = &specInfo, - }, - .layout = *layout, - }); - } - - std::array layouts = {*ds_layout, *ds_layout}; - - auto my_ds = vk.device.allocateDescriptorSets({ - .descriptorPool = *dp, - .descriptorSetCount = 2, - .pSetLayouts = layouts.data(), - }); - - ds = { - my_ds[0].release(), - my_ds[1].release(), - }; + return true; } -void wivrn_foveation_renderer::render_distortion_images(std::array foveations, const VkImage * images, const VkImageView * image_views) +void render_distortion_images_fini(struct render_resources * r) { - cmd_buf.reset(); - cmd_buf.begin(vk::CommandBufferBeginInfo{}); - - // images: left position, right position, left derivate, right derivate - std::array im_barriers; - im_barriers.fill({ - .srcAccessMask = vk::AccessFlagBits::eNone, - .dstAccessMask = vk::AccessFlagBits::eMemoryWrite, - .oldLayout = vk::ImageLayout::eShaderReadOnlyOptimal, - .newLayout = vk::ImageLayout::eGeneral, - .subresourceRange = {.aspectMask = vk::ImageAspectFlagBits::eColor, - .baseMipLevel = 0, - .levelCount = 1, - .baseArrayLayer = 0, - .layerCount = 1}, - }); - for (size_t i = 0; i < im_barriers.size(); ++i) - im_barriers[i].image = vk::Image(images[i]); - - cmd_buf.pipelineBarrier( - vk::PipelineStageFlagBits::eTopOfPipe, - vk::PipelineStageFlagBits::eComputeShader, - {}, - nullptr, - nullptr, - im_barriers); - - // Monado's distortion shader is patched to read red as position and green as derivate - for (int eye = 0; eye < 2; ++eye) - { - // Descriptor set - - vk::DescriptorImageInfo image_info{ - .imageView = vk::ImageView(image_views[eye]), - .imageLayout = vk::ImageLayout::eGeneral, - }; - vk.device.updateDescriptorSets( - vk::WriteDescriptorSet{ - .dstSet = ds[eye], - .dstBinding = 0, - .descriptorCount = 1, - .descriptorType = vk::DescriptorType::eStorageImage, - .pImageInfo = &image_info, - }, - nullptr); - - auto foveation = foveations[eye]; - - FoveationParamsPcs f_params{ - .a = {foveation.x.a, foveation.y.a}, - .b = {foveation.x.b, foveation.y.b}, - .scale = {foveation.x.scale, foveation.y.scale}, - .center = {foveation.x.center, foveation.y.center}, - }; - - cmd_buf.bindPipeline(vk::PipelineBindPoint::eCompute, *pipeline); - cmd_buf.bindDescriptorSets(vk::PipelineBindPoint::eCompute, *layout, 0, ds[eye], {}); - cmd_buf.pushConstants(*layout, vk::ShaderStageFlagBits::eCompute, 0, f_params); - cmd_buf.dispatch(dispatch_group_count, dispatch_group_count, 1); - } - - for (auto & barrier: im_barriers) - { - barrier.srcAccessMask = vk::AccessFlagBits::eMemoryWrite; - barrier.dstAccessMask = vk::AccessFlagBits::eNone; - barrier.oldLayout = vk::ImageLayout::eGeneral; - barrier.newLayout = vk::ImageLayout::eShaderReadOnlyOptimal; - } - - cmd_buf.pipelineBarrier( - vk::PipelineStageFlagBits::eComputeShader, - vk::PipelineStageFlagBits::eBottomOfPipe, - {}, - nullptr, - nullptr, - im_barriers); - - cmd_buf.end(); + auto * vk = r->vk; + D(Buffer, r->distortion.buffer); + DF(Memory, r->distortion.device_memory); } -xrt_vec2 yaw_pitch(xrt_quat q) +static double foveate(double a, double b, double λ, 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(a * x + b) + c; + // 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 = 1/scale for x = c + + // We then ensure that source and destination pixel grids match by + // rounding to integer pixel ratios: 1:1, 1:2 etc. + // Finally, pixel spans are sorted so that we only have increasing + // ratios going out from the center. +} + +static std::tuple solve_foveation(float λ, 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) + // + // Use eq. 2 to express a as function of b, then replace in eq. 1 + // equation that needs to be null is: + auto b = [λ, c](double a) { return atan(a * (1 - c) / λ) - a; }; + auto eq = [λ, c](double a) { return atan(a * (1 - c) / λ) + atan(a * (1 + c) / λ) - 2 * a; }; // (eq. 3) + + // function starts positive, reaches a maximum then decreases to -∞ + double a0 = 0; + // Find a negative value by computing eq(2^n) + double a1 = 1; + while (eq(a1) > 0) + a1 *= 2; + + // last computed values for f(a0) and f(a1) + std::optional f_a0; + double f_a1 = eq(a1); + + int n = 0; + double a = 0; + while (std::abs(a1 - a0) > 0.0000001 && n++ < 100) + { + if (not f_a0) + { + // use binary search + a = 0.5 * (a0 + a1); + double val = eq(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 = eq(a); + } + } + + return {a, b(a)}; +} + +static xrt_vec2 yaw_pitch(xrt_quat q) { float sine_theta = std::clamp(-2.0f * (q.y * q.z - q.w * q.x), -1.0f, 1.0f); @@ -240,20 +155,108 @@ xrt_vec2 yaw_pitch(xrt_quat q) pitch}; } -std::array wivrn_foveation::get_center() +void fill_param_2d( + float angle_min, + float angle_max, + size_t foveated_dim, + size_t source_dim, + std::vector & out) +{ + float scale = float(foveated_dim) / source_dim; + const float l = tan(angle_min); + const float r = tan(angle_max); + // FIXME: use tan_center + float c = (r + l) / (l - r); + auto [a, b] = solve_foveation(scale, c); + + uint16_t last = 0; + std::vector left; // index 0: 1:1 ratio, then 2:1 etc. + std::vector right; + for (size_t i = 1; i < foveated_dim; ++i) + { + double u = (i * 2.) / foveated_dim - 1; + auto f = foveate(a, b, scale, c, u); + uint16_t n = std::clamp((f * 0.5 + 0.5) * source_dim + 0.5, 0, source_dim); + assert(n > last); + size_t count = n - last; + auto & vec = u < c ? left : right; + if (count > vec.size()) + vec.resize(count); + vec[count - 1]++; + last = n; + } + assert(last < source_dim); + size_t count = source_dim - last; + if (count > right.size()) + right.resize(count); + right[count - 1]++; + + count = std::max(left.size(), right.size()); + out.clear(); + out.resize(count - left.size()); + out.insert(out.end(), left.rbegin(), left.rend()); + if (not right.empty()) + out.back() += right.front(); + if (right.size() > 1) + out.insert(out.end(), right.begin() + 1, right.end()); + out.resize(count * 2 - 1); +} + +namespace wivrn +{ + +void wivrn_foveation::compute_params( + xrt_rect src[2], + const xrt_fov fovs[2]) { std::lock_guard lock(mutex); - std::array uvs; - auto e = yaw_pitch(last_gaze); - - for (int i = 0; i < 2; i++) + xrt_vec2 tan_center{}; + if (gaze.x != 0 or gaze.y != 0 or gaze.z != 0 or gaze.w != 0) { - uvs[i].x = (e.x - views[i].fov.angleLeft) / (views[i].fov.angleRight - views[i].fov.angleLeft) * 2 - 1 + center_offset[i].x; - uvs[i].y = (e.y - views[i].fov.angleDown) / (views[i].fov.angleUp - views[i].fov.angleDown) * 2 - 1 + center_offset[i].y; + auto e = yaw_pitch(gaze); + for (size_t i = 0; i < 2; ++i) + { + (void)e; + // TODO: implement + // uvs[i].x = (e.x - views[i].fov.angleLeft) / (views[i].fov.angleRight - views[i].fov.angleLeft) * 2 - 1 + center_offset[i].x; + // uvs[i].y = (e.y - views[i].fov.angleDown) / (views[i].fov.angleUp - views[i].fov.angleDown) * 2 - 1 + center_offset[i].y; + } } - return uvs; + for (int i = 0; i < 2; ++i) + { + const auto & fov = fovs[i]; + if (foveated_width < src[i].extent.w) + fill_param_2d(fov.angle_left, fov.angle_right, foveated_width, src[i].extent.w, params[i].x); + else + params[i].x = {uint16_t(src[i].extent.w)}; + if (foveated_height < src[i].extent.h) + fill_param_2d(fov.angle_up, fov.angle_down, foveated_height, src[i].extent.h, params[i].y); + else + params[i].y = {uint16_t(src[i].extent.h)}; + } +} + +wivrn_foveation::wivrn_foveation(wivrn_vk_bundle & bundle, const xrt_hmd_parts & hmd) : + foveated_width(hmd.screens[0].w_pixels / 2), + foveated_height(hmd.screens[0].h_pixels), + command_pool(bundle.device, vk::CommandPoolCreateInfo{.queueFamilyIndex = bundle.queue_family_index}), + cmd(std::move(bundle.device.allocateCommandBuffers({ + .commandPool = command_pool, + .commandBufferCount = 1, + })[0])), + host_buffer( + bundle.device, + { + .size = sizeof(render_compute_distortion_foveation_data), + .usage = vk::BufferUsageFlagBits::eTransferSrc, + }, + VmaAllocationCreateInfo{ + .flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_RANDOM_BIT, + .usage = VMA_MEMORY_USAGE_AUTO, + }) +{ } void wivrn_foveation::update_tracking(const from_headset::tracking & tracking, const clock_offset & offset) @@ -290,16 +293,102 @@ void wivrn_foveation::update_tracking(const from_headset::tracking & tracking, c return; xrt_quat qgaze = xrt_cast(pose.pose.orientation); - math_quat_unrotate(&qgaze, &head.value(), &last_gaze); + math_quat_unrotate(&qgaze, &head.value(), &gaze); break; } } -void wivrn_foveation::set_initial_parameters(std::array p) +std::array wivrn_foveation::get_parameters() { std::lock_guard lock(mutex); + return params; +} - center_offset[0] = {p[0].x.center, p[0].y.center}; - center_offset[1] = {p[1].x.center, p[1].y.center}; +static void fill_ubo( + uint32_t * ubo, + const std::vector & params, + bool flip, + size_t offset, + size_t size, + int count) +{ + assert(params.size() % 2 == 1); + const int n_ratio = (params.size() - 1) / 2; + *ubo = offset; + if (flip) + *ubo += size; + for (auto [i, n]: std::ranges::enumerate_view(params)) + { + const int n_source = std::abs(n_ratio - int(i)) + 1; + for (size_t j = 0; j < n; ++j) + { + if (flip) + ubo[1] = ubo[0] - n_source; + else + ubo[1] = ubo[0] + n_source; + ++ubo; + --count; + } + } + if (count > 0) + std::fill(ubo + 1, ubo + count + 1, *ubo); +} + +template +static bool operator==(const T & a, const T & b) +{ + static_assert(std::has_unique_object_representations_v); + return std::memcmp(&a, &b, sizeof(T)) == 0; +} +bool operator==(const xrt_quat & a, const xrt_quat & b) +{ + return a.x == b.x and a.y == b.y and a.z == b.z and a.w == b.w; +} +bool operator==(const xrt_fov & a, const xrt_fov & b) +{ + return a.angle_left == b.angle_left and a.angle_right == b.angle_right and a.angle_up == b.angle_up and a.angle_down == b.angle_down; +} + +vk::CommandBuffer wivrn_foveation::update_foveation_buffer( + vk::Buffer target, + bool flip_y, + xrt_rect source[2], + xrt_fov fovs[2]) +{ + if (not target) + return nullptr; + + if (target != gpu_buffer) + { + command_pool.reset(); + cmd.begin({}); + cmd.copyBuffer(host_buffer, target, vk::BufferCopy{.size = sizeof(render_compute_distortion_foveation_data)}); + cmd.end(); + } + + // Check if the last value is still valid + if (last.flip_y == flip_y and last.src[0] == source[0] and last.src[1] == source[1] and last.fovs[0] == fovs[0]) + { + std::lock_guard lock(mutex); + if (last.gaze == gaze) + return nullptr; + } + + last = { + .gaze = gaze, + .flip_y = flip_y, + .src = {source[0], source[1]}, + .fovs = {fovs[0], fovs[1]}, + }; + + compute_params(source, fovs); + + auto ubo = (render_compute_distortion_foveation_data *)host_buffer.map(); + for (size_t view = 0; view < 2; ++view) + { + fill_ubo(ubo->x + view * RENDER_FOVEATION_BUFFER_DIMENSIONS, params[view].x, false, source[view].offset.w, source[view].extent.w, foveated_width); + fill_ubo(ubo->y + view * RENDER_FOVEATION_BUFFER_DIMENSIONS, params[view].y, flip_y, source[view].offset.h, source[view].extent.h, foveated_height); + } + return cmd; } } // namespace wivrn diff --git a/server/driver/wivrn_foveation.h b/server/driver/wivrn_foveation.h index 0f80da17..a638a102 100644 --- a/server/driver/wivrn_foveation.h +++ b/server/driver/wivrn_foveation.h @@ -1,6 +1,7 @@ /* * WiVRn VR streaming * Copyright (C) 2024 galister + * Copyright (C) 2025 Patrick Nicolas * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -18,50 +19,61 @@ #pragma once +#include "vk/allocation.h" #include "wivrn_packets.h" #include "xrt/xrt_defines.h" +#include "xrt/xrt_device.h" #include +struct render_resources; + namespace wivrn { struct clock_offset; struct wivrn_vk_bundle; -// Calculates the center parameter from the received eye tracking data. class wivrn_foveation { std::mutex mutex; - std::array center_offset = {}; + const size_t foveated_width; // per eye + const size_t foveated_height; + std::array views = {}; - xrt_quat last_gaze = {}; + xrt_quat gaze = {}; + std::array params; + + vk::raii::CommandPool command_pool; + vk::raii::CommandBuffer cmd; + buffer_allocation host_buffer; + vk::Buffer gpu_buffer = nullptr; + + // parameters used for last computation + struct P + { + xrt_quat gaze = {}; + bool flip_y = false; + xrt_rect src[2] = {}; + xrt_fov fovs[2] = {}; + }; + P last; + + void compute_params( + xrt_rect src[2], + const xrt_fov fovs[2]); public: - wivrn_foveation() {} + wivrn_foveation(wivrn_vk_bundle &, const xrt_hmd_parts &); - void set_initial_parameters(std::array p); void update_tracking(const from_headset::tracking &, const clock_offset &); - std::array get_center(); -}; + std::array get_parameters(); -// Renders foveation parameters to Monado's distortion images using a compute shader -class wivrn_foveation_renderer -{ - wivrn_vk_bundle & vk; - - vk::raii::DescriptorPool dp = nullptr; - vk::raii::DescriptorSetLayout ds_layout = nullptr; - vk::raii::PipelineLayout layout = nullptr; - vk::raii::Pipeline pipeline = nullptr; - std::array ds; - -public: - wivrn_foveation_renderer(wivrn_vk_bundle & vk, vk::raii::CommandPool & cmd_pool); - - vk::raii::CommandBuffer cmd_buf = nullptr; - - void render_distortion_images(std::array foveation_arr, const VkImage * images, const VkImageView * image_views); + vk::CommandBuffer update_foveation_buffer( + vk::Buffer target, + bool flip_y, + xrt_rect src[2], + xrt_fov fovs[2]); }; } // namespace wivrn diff --git a/server/driver/wivrn_hmd.cpp b/server/driver/wivrn_hmd.cpp index 401381f5..9d3a330c 100644 --- a/server/driver/wivrn_hmd.cpp +++ b/server/driver/wivrn_hmd.cpp @@ -28,8 +28,6 @@ #include "utils/method.h" #include "xrt_cast.h" -#include -#include #include #include #include @@ -39,114 +37,6 @@ namespace wivrn { -static double foveate(double a, double b, double λ, 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(a * x + b) + c; - // 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 = 1/scale for x = c -} - -static std::tuple solve_foveation(float λ, 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) - // - // Use eq. 2 to express a as function of b, then replace in eq. 1 - // equation that needs to be null is: - auto b = [λ, c](double a) { return atan(a * (1 - c) / λ) - a; }; - auto eq = [λ, c](double a) { return atan(a * (1 - c) / λ) + atan(a * (1 + c) / λ) - 2 * a; }; // (eq. 3) - - // function starts positive, reaches a maximum then decreases to -∞ - double a0 = 0; - // Find a negative value by computing eq(2^n) - double a1 = 1; - while (eq(a1) > 0) - a1 *= 2; - - // last computed values for f(a0) and f(a1) - std::optional f_a0; - double f_a1 = eq(a1); - - int n = 0; - double a = 0; - while (std::abs(a1 - a0) > 0.0000001 && n++ < 100) - { - if (not f_a0) - { - // use binary search - a = 0.5 * (a0 + a1); - double val = eq(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 = eq(a); - } - } - - return {a, b(a)}; -} - -bool wivrn_hmd::wivrn_hmd_compute_distortion(xrt_device * xdev, uint32_t 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 = foveate(param.x.a, param.x.b, param.x.scale, param.x.center, u); - 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 = foveate(param.y.a, param.y.b, param.y.scale, param.y.center, v); - 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; -} - xrt_result_t wivrn_hmd::get_visibility_mask(xrt_visibility_mask_type type, uint32_t view_index, xrt_visibility_mask ** mask) { static_assert(sizeof(uint32_t) == sizeof(decltype(from_headset::visibility_mask_changed::mask::indices)::value_type)); @@ -304,14 +194,10 @@ xrt_result_t wivrn_hmd::get_battery_status(bool * out_present, return XRT_SUCCESS; } -decltype(wivrn_hmd::foveation_parameters) wivrn_hmd::set_foveated_size(uint32_t width, uint32_t height) +void wivrn_hmd::set_foveated_size(uint32_t width, uint32_t height) { assert(width % 2 == 0); uint32_t eye_width = width / 2; - std::array scale{ - double(eye_width) / hmd->views[0].display.w_pixels, - double(height) / hmd->views[0].display.h_pixels, - }; hmd->screens[0].w_pixels = width; hmd->screens[0].h_pixels = height; @@ -324,56 +210,6 @@ decltype(wivrn_hmd::foveation_parameters) wivrn_hmd::set_foveated_size(uint32_t view.viewport.w_pixels = eye_width; view.viewport.h_pixels = height; - - foveation_parameters[i].x.scale = scale[0]; - foveation_parameters[i].y.scale = scale[1]; - - 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; - hmd->distortion.models = XRT_DISTORTION_MODEL_COMPUTE; - hmd->distortion.preferred = XRT_DISTORTION_MODEL_COMPUTE; - return foveation_parameters; -} - -void wivrn_hmd::set_foveation_center(std::array center) -{ - for (int i = 0; i < 2; ++i) - { - foveation_parameters[i].x.center = center[i].x; - foveation_parameters[i].y.center = center[i].y; - - if (foveation_parameters[i].x.scale < 1) - { - std::tie(foveation_parameters[i].x.a, foveation_parameters[i].x.b) = - solve_foveation(foveation_parameters[i].x.scale, foveation_parameters[i].x.center); - } - if (foveation_parameters[i].y.scale < 1) - { - std::tie(foveation_parameters[i].y.a, foveation_parameters[i].y.b) = - solve_foveation(foveation_parameters[i].y.scale, foveation_parameters[i].y.center); - } } } diff --git a/server/driver/wivrn_hmd.h b/server/driver/wivrn_hmd.h index 006b85a7..f3cb0ebf 100644 --- a/server/driver/wivrn_hmd.h +++ b/server/driver/wivrn_hmd.h @@ -51,15 +51,12 @@ class wivrn_hmd : public xrt_device }; view_list views; - std::array foveation_parameters{}; from_headset::battery battery{}; thread_safe, 2>> visibility_mask; wivrn::wivrn_session * cnx; - static bool wivrn_hmd_compute_distortion(xrt_device * xdev, uint32_t view_index, float u, float v, xrt_uv_triplet * result); - xrt_result_t get_visibility_mask(xrt_visibility_mask_type, uint32_t view_index, xrt_visibility_mask **); public: @@ -67,6 +64,8 @@ public: wivrn_hmd(wivrn::wivrn_session * cnx, const from_headset::headset_info_packet & info); + void set_foveated_size(uint32_t width, uint32_t height); + xrt_result_t get_tracked_pose(xrt_input_name name, int64_t at_timestamp_ns, xrt_space_relation *); void get_view_poses(const xrt_vec3 * default_eye_relation, int64_t at_timestamp_ns, @@ -80,15 +79,6 @@ public: void update_battery(const from_headset::battery &); void update_tracking(const from_headset::tracking &, const clock_offset &); - - decltype(foveation_parameters) set_foveated_size(uint32_t width, uint32_t height); - void set_foveation_center(std::array center); - - std::array get_foveation_parameters() - { - return foveation_parameters; - } - void update_visibility_mask(const from_headset::visibility_mask_changed &); }; } // namespace wivrn diff --git a/server/driver/wivrn_session.cpp b/server/driver/wivrn_session.cpp index 6ed67c81..187bce17 100644 --- a/server/driver/wivrn_session.cpp +++ b/server/driver/wivrn_session.cpp @@ -20,7 +20,6 @@ #include "wivrn_session.h" #include "accept_connection.h" -#include "configuration.h" #include "driver/app_pacer.h" #include "main/comp_compositor.h" #include "main/comp_main_interface.h" @@ -35,20 +34,19 @@ #include "wivrn_config.h" #include "wivrn_eye_tracker.h" #include "wivrn_fb_face2_tracker.h" -#include "wivrn_foveation.h" #include "wivrn_htc_face_tracker.h" #include "wivrn_ipc.h" #include "wivrn_packets.h" #include "xrt/xrt_device.h" #include "xrt/xrt_session.h" -#include #include #include #include #include #if WIVRN_FEATURE_STEAMVR_LIGHTHOUSE +#include "configuration.h" #include "steamvr_lh_interface.h" #endif @@ -88,8 +86,9 @@ struct wivrn_comp_target_factory : public comp_target_factory static bool create_target(const struct comp_target_factory * ctf, struct comp_compositor * c, struct comp_target ** out_ct) { auto self = (wivrn_comp_target_factory *)ctf; - self->session.comp_target = new wivrn_comp_target(self->session, c); - *out_ct = self->session.comp_target; + self->session.comp_target = std::make_shared(self->session, c); + self->session.comp_target->self = self->session.comp_target; + *out_ct = self->session.comp_target.get(); return true; } }; @@ -198,7 +197,6 @@ wivrn::wivrn_session::wivrn_session(std::unique_ptr connection if (get_info().eye_gaze || is_forced_extension("EXT_eye_gaze_interaction")) { eye_tracker = std::make_unique(&hmd); - foveation = std::make_unique(); static_roles.eyes = eye_tracker.get(); xdevs[xdev_count++] = eye_tracker.get(); } @@ -406,8 +404,9 @@ void wivrn_session::operator()(const from_headset::tracking & tracking) right_hand.update_tracking(tracking, offset); if (eye_tracker) eye_tracker->update_tracking(tracking, offset); - if (foveation) - foveation->update_tracking(tracking, offset); + assert(comp_target); + assert(comp_target->foveation); + comp_target->foveation->update_tracking(tracking, offset); if (fb_face2_tracker) fb_face2_tracker->update_tracking(tracking, offset); @@ -618,29 +617,9 @@ xrt_result_t wivrn_session::push_event(const xrt_session_event & event) return xrt_session_event_sink_push(&xrt_system.broadcast, &event); } -std::array wivrn_session::set_foveated_size(uint32_t width, uint32_t height) +void wivrn_session::set_foveated_size(uint32_t width, uint32_t height) { - auto p = hmd.set_foveated_size(width, height); - - if (foveation) - foveation->set_initial_parameters(p); - - return p; -} - -bool wivrn_session::apply_dynamic_foveation() -{ - if (!foveation) - return false; - - hmd.set_foveation_center(foveation->get_center()); - comp_target->render_dynamic_foveation(hmd.get_foveation_parameters()); - return true; -} - -std::array wivrn_session::get_foveation_parameters() -{ - return hmd.get_foveation_parameters(); + hmd.set_foveated_size(width, height); } void wivrn_session::dump_time(const std::string & event, uint64_t frame, int64_t time, uint8_t stream, const char * extra) diff --git a/server/driver/wivrn_session.h b/server/driver/wivrn_session.h index 95584f23..4fe0ddce 100644 --- a/server/driver/wivrn_session.h +++ b/server/driver/wivrn_session.h @@ -44,8 +44,6 @@ namespace wivrn class wivrn_eye_tracker; class wivrn_fb_face2_tracker; class wivrn_htc_face_tracker; -class wivrn_foveation; -class wivrn_foveation_renderer; struct audio_device; struct wivrn_comp_target; struct wivrn_comp_target_factory; @@ -109,8 +107,7 @@ class wivrn_session : public xrt_system_devices std::unique_ptr eye_tracker; std::unique_ptr fb_face2_tracker; std::unique_ptr htc_face_tracker; - std::unique_ptr foveation; - wivrn_comp_target * comp_target; + std::shared_ptr comp_target; clock_offset_estimator offset_est; @@ -143,6 +140,11 @@ public: return connection->info(); }; + wivrn_hmd & get_hmd() + { + return hmd; + } + void add_predict_offset(std::chrono::nanoseconds off) { tracking_control.add(off); @@ -185,13 +187,7 @@ public: xrt_result_t push_event(const xrt_session_event &); - std::array set_foveated_size(uint32_t width, uint32_t height); - std::array get_foveation_parameters(); - bool apply_dynamic_foveation(); - bool has_dynamic_foveation() - { - return (bool)foveation; - } + void set_foveated_size(uint32_t width, uint32_t height); void dump_time(const std::string & event, uint64_t frame, int64_t time, uint8_t stream = -1, const char * extra = "");