Reduce locking in push_blit_handle

This commit is contained in:
Patrick Nicolas
2024-07-14 21:57:56 +02:00
parent 347d6c4f41
commit 0f6b5df4ae
2 changed files with 37 additions and 28 deletions
+32 -26
View File
@@ -242,7 +242,8 @@ void scenes::stream::push_blit_handle(shard_accumulator * decoder, std::shared_p
return; return;
{ {
std::unique_lock lock(decoder_mutex); std::shared_lock lock(decoder_mutex);
std::unique_lock frame_lock(frames_mutex);
const auto stream = handle->feedback.stream_index; const auto stream = handle->feedback.stream_index;
if (stream < decoders.size()) if (stream < decoders.size())
{ {
@@ -277,19 +278,20 @@ 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, XrTime display_time) std::vector<std::shared_ptr<shard_accumulator::blit_handle>> scenes::stream::common_frame(XrTime display_time)
{ {
if (sets.empty()) if (decoders.empty())
return {}; return {};
std::unique_lock lock(frames_mutex);
thread_local std::vector<shard_accumulator::blit_handle *> common_frames; thread_local std::vector<shard_accumulator::blit_handle *> common_frames;
thread_local std::vector<shard_accumulator::blit_handle *> tmp; thread_local std::vector<shard_accumulator::blit_handle *> tmp;
common_frames.clear(); common_frames.clear();
auto proj = [](const shard_accumulator::blit_handle * h) { return h ? h->feedback.frame_index : -1; }; auto proj = [](const shard_accumulator::blit_handle * h) { return h ? h->feedback.frame_index : -1; };
for (size_t i = 0; i < sets.size(); ++i) for (size_t i = 0; i < decoders.size(); ++i)
{ {
if (i == 0) if (i == 0)
{ {
for (const auto & h: sets[i].latest_frames) for (const auto & h: decoders[i].latest_frames)
if (h) if (h)
common_frames.push_back(h.get()); common_frames.push_back(h.get());
} }
@@ -297,7 +299,7 @@ std::optional<uint64_t> scenes::stream::accumulator_images::common_frame(const s
{ {
tmp.clear(); tmp.clear();
std::ranges::set_intersection( std::ranges::set_intersection(
std::ranges::views::transform(sets[i].latest_frames, [](auto ptr) { return ptr.get(); }), std::ranges::views::transform(decoders[i].latest_frames, [](auto ptr) { return ptr.get(); }),
common_frames, common_frames,
std::back_inserter(tmp), std::back_inserter(tmp),
std::ranges::less{}, std::ranges::less{},
@@ -306,21 +308,28 @@ std::optional<uint64_t> scenes::stream::accumulator_images::common_frame(const s
std::swap(common_frames, tmp); std::swap(common_frames, tmp);
} }
} }
if (common_frames.empty()) std::optional<uint64_t> frame_index;
return {}; if (not common_frames.empty())
auto min = std::ranges::min_element(common_frames, {
std::ranges::less{}, auto min = std::ranges::min_element(common_frames,
[display_time](auto frame) { std::ranges::less{},
if (not frame) [display_time](auto frame) {
return std::numeric_limits<XrTime>::max(); if (not frame)
return std::abs(frame->view_info.display_time - display_time); return std::numeric_limits<XrTime>::max();
}); return std::abs(frame->view_info.display_time - display_time);
});
assert(*min); assert(*min);
return (*min)->feedback.frame_index; frame_index = (*min)->feedback.frame_index;
}
std::vector<std::shared_ptr<shard_accumulator::blit_handle>> result;
result.reserve(decoders.size());
for (const auto & decoder: decoders)
result.push_back(decoder.frame(frame_index));
return result;
} }
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) const
{ {
for (auto it = latest_frames.rbegin(); it != latest_frames.rend(); ++it) for (auto it = latest_frames.rbegin(); it != latest_frames.rend(); ++it)
{ {
@@ -514,7 +523,7 @@ void scenes::stream::render(const XrFrameState & frame_state)
command_buffer.begin(begin_info); command_buffer.begin(begin_info);
// Keep a reference to the resources needed to blit the images until vkWaitForFences // Keep a reference to the resources needed to blit the images until vkWaitForFences
std::vector<std::shared_ptr<shard_accumulator::blit_handle>> current_blit_handles; std::vector<std::shared_ptr<shard_accumulator::blit_handle>> blit_handles;
command_buffer.resetQueryPool(*query_pool, 0, size_gpu_timestamps); command_buffer.resetQueryPool(*query_pool, 0, size_gpu_timestamps);
command_buffer.writeTimestamp(vk::PipelineStageFlagBits::eTopOfPipe, *query_pool, 0); command_buffer.writeTimestamp(vk::PipelineStageFlagBits::eTopOfPipe, *query_pool, 0);
@@ -524,17 +533,14 @@ void scenes::stream::render(const XrFrameState & frame_state)
{ {
// Search for frame with desired display time 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, frame_state.predictedDisplayTime); blit_handles = common_frame(frame_state.predictedDisplayTime);
// Blit images from the decoders // Blit images from the decoders
for (auto & i: decoders) for (auto [i, blit_handle]: utils::zip(decoders, blit_handles))
{ {
auto blit_handle = i.frame(common_frame);
if (not blit_handle) if (not blit_handle)
continue; continue;
current_blit_handles.push_back(blit_handle);
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;
@@ -695,7 +701,7 @@ void scenes::stream::render(const XrFrameState & frame_state)
XrCompositionLayerQuad imgui_layer; XrCompositionLayerQuad imgui_layer;
if (imgui_ctx and plots_visible) if (imgui_ctx and plots_visible)
{ {
accumulate_metrics(frame_state.predictedDisplayTime, current_blit_handles, timestamps); accumulate_metrics(frame_state.predictedDisplayTime, blit_handles, timestamps);
imgui_layer = plot_performance_metrics(frame_state.predictedDisplayTime); imgui_layer = plot_performance_metrics(frame_state.predictedDisplayTime);
} }
@@ -707,7 +713,7 @@ void scenes::stream::render(const XrFrameState & frame_state)
session.end_frame(frame_state.predictedDisplayTime, layers_base); session.end_frame(frame_state.predictedDisplayTime, layers_base);
// Network operations may be blocking, do them once everything was submitted // Network operations may be blocking, do them once everything was submitted
for (const auto & handle: current_blit_handles) for (const auto & handle: blit_handles)
send_feedback(handle->feedback); send_feedback(handle->feedback);
read_actions(); read_actions();
+5 -2
View File
@@ -58,11 +58,14 @@ 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> &, XrTime display_time); std::shared_ptr<shard_accumulator::blit_handle> frame(std::optional<uint64_t> id) const;
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;
}; };
// for frames inside accumulator images
std::mutex frames_mutex;
std::vector<std::shared_ptr<shard_accumulator::blit_handle>> common_frame(XrTime display_time);
struct renderpass_output struct renderpass_output
{ {
vk::Extent2D size; vk::Extent2D size;