Merge remote-tracking branch 'upstream/master'
This commit is contained in:
@@ -23,12 +23,14 @@
|
||||
#include "encoder/video_encoder.h"
|
||||
#include "util/u_logging.h"
|
||||
#include "utils/scoped_lock.h"
|
||||
#include "wivrn_config.h"
|
||||
#include "wivrn_foveation.h"
|
||||
|
||||
#include "main/comp_compositor.h"
|
||||
#include "math/m_space.h"
|
||||
#include "xrt_cast.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <vector>
|
||||
#include <vulkan/vulkan.hpp>
|
||||
#include <vulkan/vulkan_core.h>
|
||||
@@ -58,6 +60,9 @@ std::vector<const char *> wivrn_comp_target::wanted_device_extensions = {
|
||||
#ifdef VK_KHR_video_encode_queue
|
||||
VK_KHR_VIDEO_ENCODE_QUEUE_EXTENSION_NAME,
|
||||
#endif
|
||||
#ifdef VK_KHR_video_maintenance1
|
||||
VK_KHR_VIDEO_MAINTENANCE_1_EXTENSION_NAME,
|
||||
#endif
|
||||
#ifdef VK_KHR_video_encode_h264
|
||||
VK_KHR_VIDEO_ENCODE_H264_EXTENSION_NAME,
|
||||
#endif
|
||||
@@ -188,54 +193,46 @@ 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] = video_encoder::get_create_image_info(cn->settings);
|
||||
|
||||
vk::VideoProfileListInfoKHR video_profile_list{
|
||||
.profileCount = uint32_t(video_profiles.size()),
|
||||
.pProfiles = video_profiles.data(),
|
||||
vk::StructureChain image_info{
|
||||
vk::ImageCreateInfo{
|
||||
.flags = vk::ImageCreateFlagBits::eExtendedUsage | vk::ImageCreateFlagBits::eMutableFormat,
|
||||
.imageType = vk::ImageType::e2D,
|
||||
.format = format,
|
||||
.extent = {
|
||||
.width = cn->width,
|
||||
.height = cn->height,
|
||||
.depth = 1,
|
||||
},
|
||||
.mipLevels = 1,
|
||||
.arrayLayers = 2, // colour then alpha
|
||||
.samples = vk::SampleCountFlagBits::e1,
|
||||
.tiling = vk::ImageTiling::eOptimal,
|
||||
.usage = flags | vk::ImageUsageFlagBits::eStorage | vk::ImageUsageFlagBits::eTransferSrc,
|
||||
.sharingMode = vk::SharingMode::eExclusive,
|
||||
},
|
||||
vk::ImageFormatListCreateInfo{
|
||||
.viewFormatCount = formats.size(),
|
||||
.pViewFormats = formats.data(),
|
||||
},
|
||||
};
|
||||
#if WIVRN_USE_VULKAN_ENCODE
|
||||
if (
|
||||
vk->features.video_maintenance_1 and
|
||||
std::ranges::any_of(
|
||||
cn->settings,
|
||||
[](const auto & item) { return item.encoder_name == encoder_vulkan //
|
||||
and item.offset_x == 0 and item.offset_y == 0; }))
|
||||
{
|
||||
image_info.get().flags |= vk::ImageCreateFlagBits::eVideoProfileIndependentKHR;
|
||||
image_info.get().usage |= vk::ImageUsageFlagBits::eVideoEncodeSrcKHR;
|
||||
}
|
||||
#endif
|
||||
|
||||
cn->psc.images.resize(cn->image_count);
|
||||
for (uint32_t i = 0; i < cn->image_count; i++)
|
||||
{
|
||||
vk::ImageFormatListCreateInfo formats_info{
|
||||
.viewFormatCount = formats.size(),
|
||||
.pViewFormats = formats.data(),
|
||||
};
|
||||
|
||||
#if WIVRN_USE_VULKAN_ENCODE
|
||||
if (video_profile_list.profileCount)
|
||||
formats_info.pNext = &video_profile_list;
|
||||
#endif
|
||||
|
||||
auto & image = cn->psc.images[i].image;
|
||||
image = image_allocation(
|
||||
device, {
|
||||
.pNext = &formats_info,
|
||||
.flags = vk::ImageCreateFlagBits::eExtendedUsage | vk::ImageCreateFlagBits::eMutableFormat,
|
||||
.imageType = vk::ImageType::e2D,
|
||||
.format = format,
|
||||
.extent = {
|
||||
.width = cn->width,
|
||||
.height = cn->height,
|
||||
.depth = 1,
|
||||
},
|
||||
.mipLevels = 1,
|
||||
.arrayLayers = 2, // colour then alpha
|
||||
.samples = vk::SampleCountFlagBits::e1,
|
||||
.tiling = vk::ImageTiling::eOptimal,
|
||||
.usage = flags
|
||||
#if WIVRN_USE_VULKAN_ENCODE
|
||||
| encoder_flags
|
||||
#endif
|
||||
| vk::ImageUsageFlagBits::eStorage | vk::ImageUsageFlagBits::eTransferSrc,
|
||||
.sharingMode = vk::SharingMode::eExclusive,
|
||||
},
|
||||
{
|
||||
.usage = VMA_MEMORY_USAGE_AUTO,
|
||||
});
|
||||
image = image_allocation(device, image_info.get(), {.usage = VMA_MEMORY_USAGE_AUTO});
|
||||
cn->images[i].handle = image;
|
||||
cn->wivrn_bundle->name(vk::Image(image), "comp target image");
|
||||
}
|
||||
|
||||
@@ -180,32 +180,6 @@ std::unique_ptr<video_encoder> video_encoder::create(
|
||||
return res;
|
||||
}
|
||||
|
||||
#if WIVRN_USE_VULKAN_ENCODE
|
||||
std::pair<std::vector<vk::VideoProfileInfoKHR>, vk::ImageUsageFlags> video_encoder::get_create_image_info(const std::vector<encoder_settings> & settings)
|
||||
{
|
||||
std::pair<std::vector<vk::VideoProfileInfoKHR>, vk::ImageUsageFlags> result;
|
||||
for (const auto & item: settings)
|
||||
{
|
||||
if (item.encoder_name == encoder_vulkan)
|
||||
{
|
||||
result.second |= vk::ImageUsageFlagBits::eVideoEncodeSrcKHR;
|
||||
switch (item.codec)
|
||||
{
|
||||
case h264:
|
||||
result.first.push_back(video_encoder_vulkan_h264::video_profile_info.get());
|
||||
break;
|
||||
case h265:
|
||||
// result.first.push_back(video_encoder_vulkan_h265::video_profile_info.get());
|
||||
break;
|
||||
case av1:
|
||||
throw std::runtime_error("av1 not supported for vulkan video encode");
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
#endif
|
||||
|
||||
static const uint64_t idr_throttle = 100;
|
||||
|
||||
video_encoder::video_encoder(uint8_t stream_idx, to_headset::video_stream_description::channels_t channels, double bitrate_multiplier, bool async_send) :
|
||||
|
||||
@@ -20,7 +20,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "driver/clock_offset.h"
|
||||
#include "wivrn_config.h"
|
||||
#include "wivrn_packets.h"
|
||||
|
||||
#include <atomic>
|
||||
@@ -114,10 +113,6 @@ public:
|
||||
int input_height,
|
||||
float fps);
|
||||
|
||||
#if WIVRN_USE_VULKAN_ENCODE
|
||||
static std::pair<std::vector<vk::VideoProfileInfoKHR>, vk::ImageUsageFlags> get_create_image_info(const std::vector<encoder_settings> &);
|
||||
#endif
|
||||
|
||||
video_encoder(uint8_t stream_idx, to_headset::video_stream_description::channels_t channels, double bitrate_multiplier, bool async_send);
|
||||
virtual ~video_encoder();
|
||||
|
||||
|
||||
@@ -15,6 +15,8 @@
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include "vk/vk_helpers.h"
|
||||
|
||||
#include "video_encoder_vulkan.h"
|
||||
|
||||
#include "encoder/encoder_settings.h"
|
||||
@@ -266,7 +268,7 @@ void wivrn::video_encoder_vulkan::init(const vk::VideoCapabilitiesKHR & video_ca
|
||||
.layerCount = 1},
|
||||
};
|
||||
|
||||
if (rect.offset != vk::Offset2D{0, 0})
|
||||
if (rect.offset != vk::Offset2D{0, 0} or not vk.vk.features.video_maintenance_1)
|
||||
{
|
||||
image_view_template.subresourceRange.baseArrayLayer = 0;
|
||||
for (size_t i = 0; i < num_slots; ++i)
|
||||
@@ -478,7 +480,7 @@ std::pair<bool, vk::Semaphore> wivrn::video_encoder_vulkan::present_image(vk::Im
|
||||
video_cmd_buf.begin({.flags = vk::CommandBufferUsageFlagBits::eOneTimeSubmit});
|
||||
|
||||
// If we encode from top left corner, encode from the source image directly
|
||||
bool encode_direct = rect.offset == vk::Offset2D{0, 0};
|
||||
bool encode_direct = not slot_item.tmp_image;
|
||||
|
||||
vk::ImageView image_view;
|
||||
|
||||
|
||||
@@ -80,10 +80,9 @@ class video_encoder_vulkan : public video_encoder
|
||||
bool session_initialized = false;
|
||||
const vk::Rect2D rect;
|
||||
|
||||
vk::VideoEncodeRateControlLayerInfoKHR rate_control_layer;
|
||||
|
||||
protected:
|
||||
const uint8_t num_dpb_slots;
|
||||
vk::VideoEncodeRateControlLayerInfoKHR rate_control_layer;
|
||||
std::optional<vk::VideoEncodeRateControlInfoKHR> rate_control;
|
||||
|
||||
video_encoder_vulkan(wivrn_vk_bundle & vk,
|
||||
|
||||
@@ -169,6 +169,47 @@ std::vector<void *> wivrn::video_encoder_vulkan_h264::setup_slot_info(size_t dpb
|
||||
return res;
|
||||
}
|
||||
|
||||
auto get_video_caps(vk::raii::PhysicalDevice & phys_dev)
|
||||
{
|
||||
vk::StructureChain video_profile_info{
|
||||
vk::VideoProfileInfoKHR{
|
||||
.videoCodecOperation =
|
||||
vk::VideoCodecOperationFlagBitsKHR::eEncodeH264,
|
||||
.chromaSubsampling = vk::VideoChromaSubsamplingFlagBitsKHR::e420,
|
||||
.lumaBitDepth = vk::VideoComponentBitDepthFlagBitsKHR::e8,
|
||||
.chromaBitDepth = vk::VideoComponentBitDepthFlagBitsKHR::e8,
|
||||
},
|
||||
vk::VideoEncodeH264ProfileInfoKHR{
|
||||
.stdProfileIdc = STD_VIDEO_H264_PROFILE_IDC_BASELINE,
|
||||
},
|
||||
vk::VideoEncodeUsageInfoKHR{
|
||||
.videoUsageHints = vk::VideoEncodeUsageFlagBitsKHR::eStreaming,
|
||||
.videoContentHints = vk::VideoEncodeContentFlagBitsKHR::eRendered,
|
||||
.tuningMode = vk::VideoEncodeTuningModeKHR::eUltraLowLatency,
|
||||
}};
|
||||
|
||||
try
|
||||
{
|
||||
auto [video_caps, encode_caps, encode_h264_caps] =
|
||||
phys_dev.getVideoCapabilitiesKHR<
|
||||
vk::VideoCapabilitiesKHR,
|
||||
vk::VideoEncodeCapabilitiesKHR,
|
||||
vk::VideoEncodeH264CapabilitiesKHR>(video_profile_info.get());
|
||||
|
||||
return std::make_tuple(video_caps, encode_caps, encode_h264_caps, video_profile_info);
|
||||
}
|
||||
catch (...)
|
||||
{}
|
||||
// NVIDIA fails if the structure is there
|
||||
video_profile_info.unlink<vk::VideoEncodeUsageInfoKHR>();
|
||||
auto [video_caps, encode_caps, encode_h264_caps] =
|
||||
phys_dev.getVideoCapabilitiesKHR<
|
||||
vk::VideoCapabilitiesKHR,
|
||||
vk::VideoEncodeCapabilitiesKHR,
|
||||
vk::VideoEncodeH264CapabilitiesKHR>(video_profile_info.get());
|
||||
return std::make_tuple(video_caps, encode_caps, encode_h264_caps, video_profile_info);
|
||||
}
|
||||
|
||||
std::unique_ptr<wivrn::video_encoder_vulkan_h264> wivrn::video_encoder_vulkan_h264::create(
|
||||
wivrn_vk_bundle & vk,
|
||||
encoder_settings & settings,
|
||||
@@ -186,15 +227,11 @@ std::unique_ptr<wivrn::video_encoder_vulkan_h264> wivrn::video_encoder_vulkan_h2
|
||||
},
|
||||
};
|
||||
|
||||
auto [video_caps, encode_caps, encode_h264_caps] =
|
||||
vk.physical_device.getVideoCapabilitiesKHR<
|
||||
vk::VideoCapabilitiesKHR,
|
||||
vk::VideoEncodeCapabilitiesKHR,
|
||||
vk::VideoEncodeH264CapabilitiesKHR>(video_profile_info.get());
|
||||
|
||||
if (settings.bit_depth != 8)
|
||||
throw std::runtime_error("h264 codec only supports 8-bit encoding");
|
||||
|
||||
auto [video_caps, encode_caps, encode_h264_caps, video_profile_info] = get_video_caps(vk.physical_device);
|
||||
|
||||
std::unique_ptr<video_encoder_vulkan_h264> self(new video_encoder_vulkan_h264(vk, rect, video_caps, encode_caps, fps, stream_idx, settings));
|
||||
|
||||
vk::VideoEncodeH264SessionParametersAddInfoKHR h264_add_info{};
|
||||
@@ -227,6 +264,8 @@ std::unique_ptr<wivrn::video_encoder_vulkan_h264> wivrn::video_encoder_vulkan_h2
|
||||
self->rate_control->pNext = &self->rate_control_h264;
|
||||
}
|
||||
|
||||
self->rate_control_layer.pNext = &self->rate_control_layer_h264;
|
||||
|
||||
self->init(video_caps, video_profile_info.get(), &session_create_info, &h264_session_params);
|
||||
|
||||
return self;
|
||||
@@ -359,20 +398,3 @@ vk::ExtensionProperties wivrn::video_encoder_vulkan_h264::std_header_version()
|
||||
VK_STD_VULKAN_VIDEO_CODEC_H264_ENCODE_EXTENSION_NAME);
|
||||
return std_header_version;
|
||||
}
|
||||
|
||||
decltype(wivrn::video_encoder_vulkan_h264::video_profile_info) wivrn::video_encoder_vulkan_h264::video_profile_info = vk::StructureChain{
|
||||
vk::VideoProfileInfoKHR{
|
||||
.videoCodecOperation =
|
||||
vk::VideoCodecOperationFlagBitsKHR::eEncodeH264,
|
||||
.chromaSubsampling = vk::VideoChromaSubsamplingFlagBitsKHR::e420,
|
||||
.lumaBitDepth = vk::VideoComponentBitDepthFlagBitsKHR::e8,
|
||||
.chromaBitDepth = vk::VideoComponentBitDepthFlagBitsKHR::e8,
|
||||
},
|
||||
vk::VideoEncodeH264ProfileInfoKHR{
|
||||
.stdProfileIdc = STD_VIDEO_H264_PROFILE_IDC_BASELINE,
|
||||
},
|
||||
vk::VideoEncodeUsageInfoKHR{
|
||||
.videoUsageHints = vk::VideoEncodeUsageFlagBitsKHR::eStreaming,
|
||||
.videoContentHints = vk::VideoEncodeContentFlagBitsKHR::eRendered,
|
||||
.tuningMode = vk::VideoEncodeTuningModeKHR::eUltraLowLatency,
|
||||
}};
|
||||
|
||||
@@ -46,6 +46,7 @@ class video_encoder_vulkan_h264 : public video_encoder_vulkan
|
||||
|
||||
vk::VideoEncodeH264GopRemainingFrameInfoKHR gop_info;
|
||||
vk::VideoEncodeH264RateControlInfoKHR rate_control_h264;
|
||||
vk::VideoEncodeH264RateControlLayerInfoKHR rate_control_layer_h264;
|
||||
|
||||
video_encoder_vulkan_h264(wivrn_vk_bundle & vk,
|
||||
vk::Rect2D rect,
|
||||
@@ -70,7 +71,5 @@ public:
|
||||
uint8_t stream_idx);
|
||||
|
||||
std::vector<uint8_t> get_sps_pps();
|
||||
|
||||
static const vk::StructureChain<vk::VideoProfileInfoKHR, vk::VideoEncodeH264ProfileInfoKHR, vk::VideoEncodeUsageInfoKHR> video_profile_info;
|
||||
};
|
||||
} // namespace wivrn
|
||||
|
||||
Reference in New Issue
Block a user