Support passthrough for stream

This commit is contained in:
Patrick Nicolas
2024-11-06 19:50:44 +01:00
parent 45412e3389
commit 410b112fcf
34 changed files with 571 additions and 260 deletions
+27 -21
View File
@@ -60,6 +60,29 @@ const std::filesystem::path & configuration::get_config_file()
return config_file;
}
configuration::encoder parse_encoder(const nlohmann::json & item)
{
configuration::encoder e;
if (item.contains("encoder"))
e.name = item["encoder"];
#define SET_IF(property) \
if (item.contains(#property)) \
e.property = item[#property];
SET_IF(width);
SET_IF(height);
SET_IF(offset_x);
SET_IF(offset_y);
SET_IF(group);
SET_IF(codec);
if (e.codec == wivrn::video_codec(-1))
throw std::runtime_error("invalid codec value " + item["codec"].get<std::string>());
SET_IF(options);
SET_IF(device);
return e;
}
configuration configuration::read_user_configuration()
{
configuration result;
@@ -86,29 +109,12 @@ configuration configuration::read_user_configuration()
if (json.contains("encoders"))
{
for (const auto & encoder: json["encoders"])
{
configuration::encoder e;
if (encoder.contains("encoder"))
e.name = encoder["encoder"];
#define SET_IF(property) \
if (encoder.contains(#property)) \
e.property = encoder[#property];
SET_IF(width);
SET_IF(height);
SET_IF(offset_x);
SET_IF(offset_y);
SET_IF(group);
SET_IF(codec);
if (e.codec == wivrn::video_codec(-1))
throw std::runtime_error("invalid codec value " + encoder["codec"].get<std::string>());
SET_IF(options);
SET_IF(device);
result.encoders.push_back(e);
}
result.encoders.push_back(parse_encoder(encoder));
}
if (json.contains("encoder-passthrough"))
result.encoder_passthrough = parse_encoder(json["encoder-passthrough"]);
if (json.contains("application"))
{
if (json["application"].is_string())
+1
View File
@@ -46,6 +46,7 @@ struct configuration
};
std::vector<encoder> encoders;
std::optional<encoder> encoder_passthrough;
std::optional<int> bitrate;
std::optional<std::array<double, 2>> scale;
std::vector<std::string> application;
+23 -16
View File
@@ -114,7 +114,7 @@ static void destroy_images(struct wivrn_comp_target * cn)
target_fini_semaphores(cn);
}
static void comp_wivrn_present_thread(std::stop_token stop_token, wivrn_comp_target * cn, int index, std::vector<std::shared_ptr<VideoEncoder>> encoders);
static void comp_wivrn_present_thread(std::stop_token stop_token, wivrn_comp_target * cn, int index, std::vector<std::shared_ptr<video_encoder>> encoders);
static void create_encoders(wivrn_comp_target * cn)
{
@@ -129,7 +129,7 @@ static void create_encoders(wivrn_comp_target * cn)
desc.height = cn->height;
desc.foveation = cn->cnx.set_foveated_size(desc.width, desc.height);
std::map<int, std::vector<std::shared_ptr<VideoEncoder>>> thread_params;
std::map<int, std::vector<std::shared_ptr<video_encoder>>> thread_params;
#if WIVRN_USE_VULKAN_ENCODE
if (std::ranges::any_of(cn->settings, [](const auto & item) { return item.encoder_name == encoder_vulkan; }))
@@ -162,7 +162,7 @@ static void create_encoders(wivrn_comp_target * cn)
{
uint8_t stream_index = cn->encoders.size();
auto & encoder = cn->encoders.emplace_back(
VideoEncoder::Create(*cn->wivrn_bundle, settings, stream_index, desc.width, desc.height, desc.fps));
video_encoder::create(*cn->wivrn_bundle, settings, stream_index, desc.width, desc.height, desc.fps));
desc.items.push_back(settings);
thread_params[settings.group].emplace_back(encoder);
@@ -193,7 +193,7 @@ static VkResult create_images(struct wivrn_comp_target * cn, vk::ImageUsageFlags
cn->images = U_TYPED_ARRAY_CALLOC(struct comp_target_image, cn->image_count);
#if WIVRN_USE_VULKAN_ENCODE
auto [video_profiles, encoder_flags] = VideoEncoder::get_create_image_info(cn->settings);
auto [video_profiles, encoder_flags] = video_encoder::get_create_image_info(cn->settings);
vk::VideoProfileListInfoKHR video_profile_list{
.profileCount = uint32_t(video_profiles.size()),
@@ -202,7 +202,6 @@ static VkResult create_images(struct wivrn_comp_target * cn, vk::ImageUsageFlags
#endif
cn->psc.images.resize(cn->image_count);
std::vector<vk::Image> rgb;
for (uint32_t i = 0; i < cn->image_count; i++)
{
std::array formats = {
@@ -233,7 +232,7 @@ static VkResult create_images(struct wivrn_comp_target * cn, vk::ImageUsageFlags
.depth = 1,
},
.mipLevels = 1,
.arrayLayers = 1,
.arrayLayers = 2, // colour then alpha
.samples = vk::SampleCountFlagBits::e1,
.tiling = vk::ImageTiling::eOptimal,
.usage = flags
@@ -247,7 +246,6 @@ static VkResult create_images(struct wivrn_comp_target * cn, vk::ImageUsageFlags
.usage = VMA_MEMORY_USAGE_AUTO,
});
cn->images[i].handle = image;
rgb.push_back(image);
}
for (uint32_t i = 0; i < cn->image_count; i++)
@@ -260,24 +258,24 @@ static VkResult create_images(struct wivrn_comp_target * cn, vk::ImageUsageFlags
{
.pNext = &usage,
.image = item.image,
.viewType = vk::ImageViewType::e2D,
.viewType = vk::ImageViewType::e2DArray,
.format = vk::Format::eR8Unorm,
.subresourceRange = {
.aspectMask = vk::ImageAspectFlagBits::ePlane0,
.levelCount = 1,
.layerCount = 1,
.layerCount = 2,
},
});
item.image_view_cbcr = vk::raii::ImageView(device,
{
.pNext = &usage,
.image = item.image,
.viewType = vk::ImageViewType::e2D,
.viewType = vk::ImageViewType::e2DArray,
.format = vk::Format::eR8G8Unorm,
.subresourceRange = {
.aspectMask = vk::ImageAspectFlagBits::ePlane1,
.levelCount = 1,
.layerCount = 1,
.layerCount = 2,
},
});
cn->images[i].view = *item.image_view_y;
@@ -460,7 +458,7 @@ static VkResult comp_wivrn_acquire(struct comp_target * ct, uint32_t * out_index
};
}
static void comp_wivrn_present_thread(std::stop_token stop_token, wivrn_comp_target * cn, int index, std::vector<std::shared_ptr<VideoEncoder>> encoders)
static void comp_wivrn_present_thread(std::stop_token stop_token, wivrn_comp_target * cn, int index, std::vector<std::shared_ptr<video_encoder>> encoders)
{
auto & vk = *cn->wivrn_bundle;
U_LOG_I("Starting encoder thread %d", index);
@@ -506,7 +504,8 @@ static void comp_wivrn_present_thread(std::stop_token stop_token, wivrn_comp_tar
{
for (auto & encoder: encoders)
{
encoder->Encode(cn->cnx, view_info, frame_index);
if (encoder->channels == to_headset::video_stream_description::channels_t::colour or view_info.alpha)
encoder->Encode(cn->cnx, view_info, frame_index);
}
}
catch (std::exception & e)
@@ -604,9 +603,12 @@ static VkResult comp_wivrn_present(struct comp_target * ct,
cn->wivrn_bundle->device.resetFences(*cn->psc.fence);
psc_image.status = pseudo_swapchain::status_t::encoding;
auto info = cn->pacer.present_to_info(desired_present_time_ns);
const bool do_alpha = cn->c->base.layer_accum.data.env_blend_mode == XRT_BLEND_MODE_ALPHA_BLEND;
for (auto & encoder: cn->encoders)
{
if (encoder->channels == to_headset::video_stream_description::channels_t::alpha and not do_alpha)
continue;
#if WIVRN_USE_VULKAN_ENCODE
encoder->present_image(psc_image.image, video_command_buffer, *cn->psc.images[index].video_fence, info.frame_id);
#endif
@@ -658,6 +660,9 @@ static VkResult comp_wivrn_present(struct comp_target * ct,
auto & view_info = cn->psc.view_info;
view_info.foveation = cn->cnx.get_foveation_parameters();
view_info.display_time = cn->cnx.get_offset().to_headset(info.predicted_display_time);
if (view_info.alpha != do_alpha)
cn->pacer.reset();
view_info.alpha = do_alpha;
for (int eye = 0; eye < 2; ++eye)
{
const auto & frame_params = cn->c->base.frame_params;
@@ -788,11 +793,13 @@ void wivrn_comp_target::on_feedback(const from_headset::feedback & feedback, con
{
if (not o)
return;
pacer.on_feedback(feedback, o);
uint8_t stream = feedback.stream_index;
if (psc.status & 1)
return;
if (feedback.stream_index < encoders.size())
encoders[feedback.stream_index]->on_feedback(feedback);
if (encoders.size() <= stream)
return;
encoders[stream]->on_feedback(feedback);
pacer.on_feedback(feedback, o);
}
void wivrn_comp_target::reset_encoders()
+3 -3
View File
@@ -24,6 +24,7 @@
#include "encoder/encoder_settings.h"
#include "utils/wivrn_vk_bundle.h"
#include "vk/allocation.h"
#include "wivrn_foveation.h"
#include "wivrn_pacer.h"
#include "wivrn_packets.h"
@@ -37,9 +38,8 @@
namespace wivrn
{
class wivrn_foveation_renderer;
class wivrn_session;
class VideoEncoder;
class video_encoder;
struct pseudo_swapchain
{
@@ -101,7 +101,7 @@ struct wivrn_comp_target : public comp_target
std::vector<encoder_settings> settings;
to_headset::video_stream_description desc{};
std::list<std::jthread> encoder_threads;
std::vector<std::shared_ptr<VideoEncoder>> encoders;
std::vector<std::shared_ptr<video_encoder>> encoders;
wivrn::wivrn_session & cnx;
std::unique_ptr<wivrn_foveation_renderer> foveation_renderer = nullptr;
+4 -2
View File
@@ -226,8 +226,10 @@ wivrn_hmd::wivrn_hmd(wivrn::wivrn_session * cnx,
// Setup info.
hmd->view_count = 2;
hmd->blend_modes[0] = XRT_BLEND_MODE_OPAQUE;
hmd->blend_mode_count = 1;
hmd->blend_modes[hmd->blend_mode_count++] = XRT_BLEND_MODE_OPAQUE;
if (info.passthrough)
hmd->blend_modes[hmd->blend_mode_count++] = XRT_BLEND_MODE_ALPHA_BLEND;
hmd->distortion.models = XRT_DISTORTION_MODEL_NONE;
hmd->distortion.preferred = XRT_DISTORTION_MODEL_NONE;
hmd->screens[0].w_pixels = eye_width * 2;