Select image to display based on predicted display time

This commit is contained in:
Patrick Nicolas
2024-04-04 17:42:14 +02:00
parent 3bfb1adbce
commit 63f4028d83
5 changed files with 45 additions and 22 deletions
@@ -391,7 +391,6 @@ void decoder::on_image_available(AImageReader * reader)
return; return;
} }
info->feedback.received_from_decoder = application::now();
assert(info->feedback.frame_index == frame_index); assert(info->feedback.frame_index == frame_index);
auto vk_data = map_hardware_buffer(image); auto vk_data = map_hardware_buffer(image);
+41 -18
View File
@@ -34,6 +34,7 @@
#include "wivrn_packets.h" #include "wivrn_packets.h"
#include <algorithm> #include <algorithm>
#include <mutex> #include <mutex>
#include <ranges>
#include <thread> #include <thread>
#include <vulkan/vulkan_raii.hpp> #include <vulkan/vulkan_raii.hpp>
@@ -244,6 +245,7 @@ void scenes::stream::push_blit_handle(shard_accumulator * decoder, std::shared_p
{ {
std::swap(i.latest_frames[j - 1], i.latest_frames[j]); std::swap(i.latest_frames[j - 1], i.latest_frames[j]);
} }
handle->feedback.received_from_decoder = application::now();
std::swap(i.latest_frames.back(), handle); std::swap(i.latest_frames.back(), handle);
break; break;
} }
@@ -276,24 +278,47 @@ std::vector<uint64_t> scenes::stream::accumulator_images::frames() const
return result; return result;
} }
std::optional<uint64_t> scenes::stream::accumulator_images::common_frame(const std::vector<accumulator_images> & sets, uint64_t preferred_frame) std::optional<uint64_t> scenes::stream::accumulator_images::common_frame(const std::vector<accumulator_images> & sets, XrTime display_time)
{ {
if (sets.empty()) if (sets.empty())
return {}; return {};
auto common_frames = sets[0].frames(); thread_local std::vector<shard_accumulator::blit_handle *> common_frames;
for (const auto & set: sets) thread_local std::vector<shard_accumulator::blit_handle *> tmp;
common_frames.clear();
auto proj = [](const shard_accumulator::blit_handle * h) { return h ? h->feedback.frame_index : -1; };
for (size_t i = 0; i < sets.size(); ++i)
{ {
std::vector<uint64_t> tmp; if (i == 0)
auto x = set.frames(); {
std::ranges::set_intersection(x, common_frames, std::back_inserter(tmp)); for (const auto & h: sets[i].latest_frames)
common_frames = tmp; if (h)
if (common_frames.empty()) common_frames.push_back(h.get());
return {}; }
else
{
tmp.clear();
std::ranges::set_intersection(
std::ranges::views::transform(sets[i].latest_frames, [](auto ptr) { return ptr.get(); }),
common_frames,
std::back_inserter(tmp),
std::ranges::less{},
proj,
proj);
std::swap(common_frames, tmp);
}
} }
assert(not common_frames.empty()); if (common_frames.empty())
if (std::ranges::find(common_frames, preferred_frame) != common_frames.end()) return {};
return preferred_frame; auto min = std::ranges::min_element(common_frames,
return common_frames.back(); std::ranges::less{},
[display_time](auto frame) {
if (not frame)
return std::numeric_limits<XrTime>::max();
return std::abs(frame->view_info.display_time - display_time);
});
assert(*min);
return (*min)->feedback.frame_index;
} }
std::shared_ptr<shard_accumulator::blit_handle> scenes::stream::accumulator_images::frame(std::optional<uint64_t> id) std::shared_ptr<shard_accumulator::blit_handle> scenes::stream::accumulator_images::frame(std::optional<uint64_t> id)
@@ -505,12 +530,9 @@ void scenes::stream::render(XrTime predicted_display_time, bool should_render)
std::array<XrFovf, 2> fov = {views[0].fov, views[1].fov}; std::array<XrFovf, 2> fov = {views[0].fov, views[1].fov};
{ {
std::unique_lock lock(decoder_mutex); std::unique_lock lock(decoder_mutex);
// Search for the most recent frame available on all decoders. // Search for frame with desired display time on all decoders
// If no such frame exists, use the latest frame for each decoder // If no such frame exists, use the latest frame for each decoder
auto common_frame = accumulator_images::common_frame(decoders, next_frame); auto common_frame = accumulator_images::common_frame(decoders, predicted_display_time);
if (common_frame)
next_frame = *common_frame + 1;
// Blit images from the decoders // Blit images from the decoders
for (auto & i: decoders) for (auto & i: decoders)
@@ -524,6 +546,7 @@ void scenes::stream::render(XrTime predicted_display_time, bool should_render)
blit_handle->feedback.blitted = application::now(); blit_handle->feedback.blitted = application::now();
if (blit_handle->feedback.blitted - blit_handle->feedback.received_from_decoder > 1'000'000'000) if (blit_handle->feedback.blitted - blit_handle->feedback.received_from_decoder > 1'000'000'000)
state_ = stream::state::stalled; state_ = stream::state::stalled;
++blit_handle->feedback.times_displayed;
blit_handle->feedback.displayed = predicted_display_time; blit_handle->feedback.displayed = predicted_display_time;
blit_handle->feedback.real_pose[0] = views[0].pose; blit_handle->feedback.real_pose[0] = views[0].pose;
blit_handle->feedback.real_pose[1] = views[1].pose; blit_handle->feedback.real_pose[1] = views[1].pose;
+1 -2
View File
@@ -58,7 +58,7 @@ private:
// latest frames from oldest to most recent // latest frames from oldest to most recent
std::array<std::shared_ptr<shard_accumulator::blit_handle>, 3> latest_frames; std::array<std::shared_ptr<shard_accumulator::blit_handle>, 3> latest_frames;
static std::optional<uint64_t> common_frame(const std::vector<accumulator_images> &, uint64_t preferred_index); static std::optional<uint64_t> common_frame(const std::vector<accumulator_images> &, XrTime display_time);
std::shared_ptr<shard_accumulator::blit_handle> frame(std::optional<uint64_t> id); std::shared_ptr<shard_accumulator::blit_handle> frame(std::optional<uint64_t> id);
std::vector<uint64_t> frames() const; std::vector<uint64_t> frames() const;
}; };
@@ -83,7 +83,6 @@ private:
std::mutex decoder_mutex; std::mutex decoder_mutex;
std::optional<to_headset::video_stream_description> video_stream_description; std::optional<to_headset::video_stream_description> video_stream_description;
uint64_t next_frame; // Preferred index for next frame
std::vector<accumulator_images> decoders; // Locked by decoder_mutex std::vector<accumulator_images> decoders; // Locked by decoder_mutex
vk::raii::DescriptorPool blit_descriptor_pool = nullptr; vk::raii::DescriptorPool blit_descriptor_pool = nullptr;
vk::raii::RenderPass blit_render_pass = nullptr; vk::raii::RenderPass blit_render_pass = nullptr;
+2
View File
@@ -206,6 +206,8 @@ struct feedback
XrTime blitted; XrTime blitted;
XrTime displayed; XrTime displayed;
uint8_t times_displayed;
std::array<XrPosef, 2> received_pose; std::array<XrPosef, 2> received_pose;
std::array<XrPosef, 2> real_pose; std::array<XrPosef, 2> real_pose;
}; };
+1 -1
View File
@@ -75,7 +75,7 @@ void wivrn_pacer::on_feedback(const xrt::drivers::wivrn::from_headset::feedback
} }
last = feedback; last = feedback;
if (feedback.blitted) if (feedback.blitted and feedback.times_displayed == 1)
{ {
auto & stream = streams[feedback.stream_index]; auto & stream = streams[feedback.stream_index];
if (stream.times.size() < num_wait_times) if (stream.times.size() < num_wait_times)