Integrate YCbCr conversion in monado

Hacks monado's distortion shader to write into Y and CbCr planes,
eliminating the intermediate RGBA image

Note: change monado's version to enforce patch application
This commit is contained in:
Patrick Nicolas
2024-09-11 23:41:10 +02:00
parent 9344c6c012
commit 517b572c75
15 changed files with 791 additions and 606 deletions
+7 -6
View File
@@ -22,7 +22,6 @@
#include "video_encoder_va.h"
#include "encoder/yuv_converter.h"
#include "util/u_logging.h"
#include "utils/wivrn_vk_bundle.h"
@@ -357,7 +356,7 @@ video_encoder_va::video_encoder_va(wivrn_vk_bundle & vk, xrt::drivers::wivrn::en
}
}
void video_encoder_va::PresentImage(yuv_converter & src_yuv, vk::raii::CommandBuffer & cmd_buf)
void video_encoder_va::PresentImage(vk::Image y_cbcr, vk::raii::CommandBuffer & cmd_buf)
{
std::array im_barriers = {
vk::ImageMemoryBarrier{
@@ -394,13 +393,13 @@ void video_encoder_va::PresentImage(yuv_converter & src_yuv, vk::raii::CommandBu
im_barriers);
cmd_buf.copyImage(
src_yuv.luma,
y_cbcr,
vk::ImageLayout::eTransferSrcOptimal,
*luma,
vk::ImageLayout::eTransferDstOptimal,
vk::ImageCopy{
.srcSubresource = {
.aspectMask = vk::ImageAspectFlagBits::eColor,
.aspectMask = vk::ImageAspectFlagBits::ePlane0,
.layerCount = 1,
},
.srcOffset = {
@@ -418,13 +417,13 @@ void video_encoder_va::PresentImage(yuv_converter & src_yuv, vk::raii::CommandBu
}});
cmd_buf.copyImage(
src_yuv.chroma,
y_cbcr,
vk::ImageLayout::eTransferSrcOptimal,
*chroma,
vk::ImageLayout::eTransferDstOptimal,
vk::ImageCopy{
.srcSubresource = {
.aspectMask = vk::ImageAspectFlagBits::eColor,
.aspectMask = vk::ImageAspectFlagBits::ePlane1,
.layerCount = 1,
},
.srcOffset = {
@@ -443,6 +442,8 @@ void video_encoder_va::PresentImage(yuv_converter & src_yuv, vk::raii::CommandBu
for (auto & b: im_barriers)
{
b.srcAccessMask = vk::AccessFlagBits::eMemoryWrite;
b.dstAccessMask = vk::AccessFlagBits::eNone;
b.oldLayout = vk::ImageLayout::eTransferDstOptimal;
b.newLayout = vk::ImageLayout::eGeneral;
}
+1 -1
View File
@@ -43,7 +43,7 @@ class video_encoder_va : public VideoEncoderFFMPEG
public:
video_encoder_va(wivrn_vk_bundle &, xrt::drivers::wivrn::encoder_settings & settings, float fps);
void PresentImage(yuv_converter & src_yuv, vk::raii::CommandBuffer & cmd_buf) override;
void PresentImage(vk::Image y_cbcr, vk::raii::CommandBuffer & cmd_buf) override;
protected:
void PushFrame(bool idr, std::chrono::steady_clock::time_point pts) override;
+1 -2
View File
@@ -33,7 +33,6 @@
#include "encoder_settings.h"
#include "wivrn_packets.h"
class yuv_converter;
struct wivrn_vk_bundle;
namespace xrt::drivers::wivrn
@@ -106,7 +105,7 @@ public:
virtual ~VideoEncoder();
// called on present to submit command buffers for the image.
virtual void PresentImage(yuv_converter & src_yuv, vk::raii::CommandBuffer & cmd_buf) = 0;
virtual void PresentImage(vk::Image y_cbcr, vk::raii::CommandBuffer & cmd_buf) = 0;
// The other end lost a frame and needs to resynchronize
void SyncNeeded();
+5 -6
View File
@@ -18,7 +18,6 @@
*/
#include "video_encoder_nvenc.h"
#include "encoder/yuv_converter.h"
#include "util/u_logging.h"
#include "utils/wivrn_vk_bundle.h"
@@ -323,16 +322,16 @@ VideoEncoderNvenc::~VideoEncoderNvenc()
fn.nvEncDestroyEncoder(session_handle);
}
void VideoEncoderNvenc::PresentImage(yuv_converter & src_yuv, vk::raii::CommandBuffer & cmd_buf)
void VideoEncoderNvenc::PresentImage(vk::Image y_cbcr, vk::raii::CommandBuffer & cmd_buf)
{
cmd_buf.copyImageToBuffer(
src_yuv.luma,
y_cbcr,
vk::ImageLayout::eTransferSrcOptimal,
*yuv_buffer,
vk::BufferImageCopy{
.bufferRowLength = width,
.imageSubresource = {
.aspectMask = vk::ImageAspectFlagBits::eColor,
.aspectMask = vk::ImageAspectFlagBits::ePlane0,
.layerCount = 1,
},
.imageOffset = {
@@ -345,14 +344,14 @@ void VideoEncoderNvenc::PresentImage(yuv_converter & src_yuv, vk::raii::CommandB
.depth = 1,
}});
cmd_buf.copyImageToBuffer(
src_yuv.chroma,
y_cbcr,
vk::ImageLayout::eTransferSrcOptimal,
*yuv_buffer,
vk::BufferImageCopy{
.bufferOffset = width * height,
.bufferRowLength = uint32_t(width / 2),
.imageSubresource = {
.aspectMask = vk::ImageAspectFlagBits::eColor,
.aspectMask = vk::ImageAspectFlagBits::ePlane1,
.layerCount = 1,
},
.imageOffset = {
+1 -3
View File
@@ -54,8 +54,6 @@ private:
vk::raii::DeviceMemory mem = nullptr;
CUexternalMemory extmem;
vk::Image luma;
vk::Image chroma;
CUdeviceptr frame;
uint32_t width;
uint32_t height;
@@ -67,7 +65,7 @@ public:
VideoEncoderNvenc(wivrn_vk_bundle & vk, encoder_settings & settings, float fps);
~VideoEncoderNvenc();
void PresentImage(yuv_converter & src_yuv, vk::raii::CommandBuffer & cmd_buf) override;
void PresentImage(vk::Image y_cbcr, vk::raii::CommandBuffer & cmd_buf) override;
std::optional<data> encode(bool idr, std::chrono::steady_clock::time_point pts) override;
static std::array<int, 2> get_max_size(video_codec);
+5 -6
View File
@@ -21,7 +21,6 @@
#include "util/u_logging.h"
#include "utils/wivrn_vk_bundle.h"
#include "yuv_converter.h"
#include <stdexcept>
namespace xrt::drivers::wivrn
@@ -179,16 +178,16 @@ VideoEncoderX264::VideoEncoderX264(
pic.img.plane[1] = (uint8_t *)chroma.map();
}
void VideoEncoderX264::PresentImage(yuv_converter & src_yuv, vk::raii::CommandBuffer & cmd_buf)
void VideoEncoderX264::PresentImage(vk::Image y_cbcr, vk::raii::CommandBuffer & cmd_buf)
{
cmd_buf.copyImageToBuffer(
src_yuv.luma,
y_cbcr,
vk::ImageLayout::eTransferSrcOptimal,
luma,
vk::BufferImageCopy{
.bufferRowLength = chroma_width * 2,
.imageSubresource = {
.aspectMask = vk::ImageAspectFlagBits::eColor,
.aspectMask = vk::ImageAspectFlagBits::ePlane0,
.layerCount = 1,
},
.imageOffset = {
@@ -201,13 +200,13 @@ void VideoEncoderX264::PresentImage(yuv_converter & src_yuv, vk::raii::CommandBu
.depth = 1,
}});
cmd_buf.copyImageToBuffer(
src_yuv.chroma,
y_cbcr,
vk::ImageLayout::eTransferSrcOptimal,
chroma,
vk::BufferImageCopy{
.bufferRowLength = chroma_width,
.imageSubresource = {
.aspectMask = vk::ImageAspectFlagBits::eColor,
.aspectMask = vk::ImageAspectFlagBits::ePlane1,
.layerCount = 1,
},
.imageOffset = {
+1 -1
View File
@@ -59,7 +59,7 @@ class VideoEncoderX264 : public VideoEncoder
public:
VideoEncoderX264(wivrn_vk_bundle & vk, encoder_settings & settings, float fps);
void PresentImage(yuv_converter & src_yuv, vk::raii::CommandBuffer & cmd_buf) override;
void PresentImage(vk::Image y_cbcr, vk::raii::CommandBuffer & cmd_buf) override;
std::optional<data> encode(bool idr, std::chrono::steady_clock::time_point pts) override;
-410
View File
@@ -1,410 +0,0 @@
/*
* WiVRn VR streaming
* Copyright (C) 2022-2024 Guillaume Meunier <guillaume.meunier@centraliens.net>
* Copyright (C) 2022-2024 Patrick Nicolas <patricknicolas@laposte.net>
*
* 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
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* 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 "yuv_converter.h"
#include <map>
#include <span>
#include <vector>
extern const std::map<std::string, std::vector<uint32_t>> shaders;
// clang-format off
#define SHADER_BGRA 1
#if SHADER_BGRA
const float COLORSPACE_BT709[3][4] = {
// B G R A
/* Y */{ 0.0722, 0.7152, 0.2126, 0.0},
/* Cb*/{ 0.5 , -0.3854, -0.1146, 0.0},
/* Cr*/{-0.0458, -0.4542, 0.5 , 0.0},
//clang-format: on
};
#else
const float COLORSPACE_BT709[3][4] = {
// R G B A
/* Y */ { 0.2126, 0.7152, 0.0722, 0.0},
/* Cb*/ {-0.1146, -0.3854, 0.5 , 0.0},
/* Cr*/ { 0.5 , -0.4542, -0.0458, 0.0},
};
#endif
static vk::Format view_format(vk::Format image_format)
{
switch (image_format)
{
case vk::Format::eB8G8R8A8Srgb:
case vk::Format::eB8G8R8A8Unorm:
// Intel anv appears to get the order wrong
// swizzle components in the colorspace
return vk::Format::eR8G8B8A8Unorm;
default:
throw std::runtime_error("YUV conversion not implemented for " + vk::to_string(image_format));
};
}
yuv_converter::yuv_converter() {}
yuv_converter::yuv_converter(
vk::PhysicalDevice physical_device,
vk::raii::Device & device,
std::span<vk::Image> rgb,
vk::Format fmt,
vk::Extent2D extent) :
device(*device), extent(extent), rgb(rgb.begin(), rgb.end())
{
auto view_fmt = view_format(fmt);
struct plane_t
{
vk::Format format;
vk::Extent3D extent;
image_allocation & image;
vk::raii::ImageView & view;
};
// Description of planes for 4:2:0 subsampling
std::array planes = {
plane_t{
.format = vk::Format::eR8Unorm,
.extent = {extent.width, extent.height, 1},
.image = luma,
.view = view_luma,
},
plane_t{
.format = vk::Format::eR8G8Unorm,
.extent = {extent.width / 2, extent.height / 2, 1},
.image = chroma,
.view = view_chroma,
},
};
// Input image view
for(vk::Image rgb_image: rgb)
{
vk::ImageViewUsageCreateInfo usage{
.usage = vk::ImageUsageFlagBits::eStorage,
};
views_rgb.push_back(device.createImageView(
{
.pNext = &usage,
.image = rgb_image,
.viewType = vk::ImageViewType::e2D,
.format = view_fmt,
.subresourceRange = {
.aspectMask = vk::ImageAspectFlagBits::eColor,
.levelCount = 1,
.layerCount = 1,
},
}));
}
// Output images
for (auto & plane: planes)
{
plane.image = image_allocation(
device,
{
.imageType = vk::ImageType::e2D,
.format = plane.format,
.extent = plane.extent,
.mipLevels = 1,
.arrayLayers = 1,
.samples = vk::SampleCountFlagBits::e1,
.tiling = vk::ImageTiling::eOptimal,
.usage = vk::ImageUsageFlagBits::eStorage |
vk::ImageUsageFlagBits::eTransferSrc,
.sharingMode = vk::SharingMode::eExclusive,
},
{
.usage = VMA_MEMORY_USAGE_AUTO,
});
}
// Output image views
for (auto & plane: planes)
{
plane.view = device.createImageView(
{
.image = plane.image,
.viewType = vk::ImageViewType::e2D,
.format = plane.format,
.subresourceRange = {.aspectMask = vk::ImageAspectFlagBits::eColor,
.baseMipLevel = 0,
.levelCount = 1,
.baseArrayLayer = 0,
.layerCount = 1},
});
}
// Descriptor sets
{
std::array ds_layout_binding{
vk::DescriptorSetLayoutBinding{
.binding = 0,
.descriptorType = vk::DescriptorType::eStorageImage,
.descriptorCount = 1,
.stageFlags = vk::ShaderStageFlagBits::eCompute,
},
vk::DescriptorSetLayoutBinding{
.binding = 1,
.descriptorType = vk::DescriptorType::eStorageImage,
.descriptorCount = 1,
.stageFlags = vk::ShaderStageFlagBits::eCompute,
},
vk::DescriptorSetLayoutBinding{
.binding = 2,
.descriptorType = vk::DescriptorType::eStorageImage,
.descriptorCount = 1,
.stageFlags = vk::ShaderStageFlagBits::eCompute,
},
};
ds_layout = device.createDescriptorSetLayout({
.bindingCount = ds_layout_binding.size(),
.pBindings = ds_layout_binding.data(),
});
}
// Pipeline layout
{
vk::PushConstantRange push_constant_range{
.stageFlags = vk::ShaderStageFlagBits::eCompute,
.offset = 0,
.size = sizeof(COLORSPACE_BT709),
};
layout = device.createPipelineLayout({
.setLayoutCount = 1,
.pSetLayouts = &*ds_layout,
.pushConstantRangeCount = 1,
.pPushConstantRanges = &push_constant_range,
});
}
// Pipeline
{
auto & spirv = shaders.at("yuv.comp");
vk::raii::ShaderModule shader(device, {
.codeSize = spirv.size() * sizeof(uint32_t),
.pCode = spirv.data(),
});
pipeline = vk::raii::Pipeline(device, nullptr, vk::ComputePipelineCreateInfo{
.stage = {
.stage = vk::ShaderStageFlagBits::eCompute,
.module = *shader,
.pName = "main",
},
.layout = *layout,
});
}
// Descriptor pool
{
std::array pool_size{
vk::DescriptorPoolSize{
.type = vk::DescriptorType::eSampledImage,
.descriptorCount = 1,
},
vk::DescriptorPoolSize{
.type = vk::DescriptorType::eStorageImage,
.descriptorCount = 2,
}};
dp = device.createDescriptorPool({
.maxSets = 2,
.poolSizeCount = pool_size.size(),
.pPoolSizes = pool_size.data(),
});
}
// Descriptor set
ds = device.allocateDescriptorSets({
.descriptorPool = *dp,
.descriptorSetCount = 1,
.pSetLayouts = &*ds_layout,
})[0].release();
vk::DescriptorImageInfo luma_desc_img_info_{
.imageView = *view_luma,
.imageLayout = vk::ImageLayout::eGeneral,
};
vk::DescriptorImageInfo chroma_desc_img_info{
.imageView = *view_chroma,
.imageLayout = vk::ImageLayout::eGeneral,
};
device.updateDescriptorSets(
{
vk::WriteDescriptorSet{
.dstSet = ds,
.dstBinding = 1,
.descriptorCount = 1,
.descriptorType = vk::DescriptorType::eStorageImage,
.pImageInfo = &luma_desc_img_info_,
},
vk::WriteDescriptorSet{
.dstSet = ds,
.dstBinding = 2,
.descriptorCount = 1,
.descriptorType = vk::DescriptorType::eStorageImage,
.pImageInfo = &chroma_desc_img_info,
},
},
nullptr);
}
void yuv_converter::record_draw_commands(
vk::raii::CommandBuffer & cmd_buf, size_t index)
{
assert(index < rgb.size());
vk::DescriptorImageInfo rgb_desc_image_info{
.imageView = *views_rgb[index],
.imageLayout = vk::ImageLayout::eGeneral,
};
device.updateDescriptorSets(
vk::WriteDescriptorSet{
.dstSet = ds,
.dstBinding = 0,
.descriptorCount = 1,
.descriptorType = vk::DescriptorType::eStorageImage,
.pImageInfo = &rgb_desc_image_info,
}, nullptr);
std::array im_barriers = {
vk::ImageMemoryBarrier{
.srcAccessMask = vk::AccessFlagBits::eNone,
.dstAccessMask = vk::AccessFlagBits::eMemoryWrite,
.oldLayout = vk::ImageLayout::eUndefined,
.newLayout = vk::ImageLayout::eGeneral,
.image = luma,
.subresourceRange = {.aspectMask = vk::ImageAspectFlagBits::eColor,
.baseMipLevel = 0,
.levelCount = 1,
.baseArrayLayer = 0,
.layerCount = 1},
},
vk::ImageMemoryBarrier{
.srcAccessMask = vk::AccessFlagBits::eNone,
.dstAccessMask = vk::AccessFlagBits::eMemoryWrite,
.oldLayout = vk::ImageLayout::eUndefined,
.newLayout = vk::ImageLayout::eGeneral,
.image = chroma,
.subresourceRange = {.aspectMask = vk::ImageAspectFlagBits::eColor,
.baseMipLevel = 0,
.levelCount = 1,
.baseArrayLayer = 0,
.layerCount = 1},
},
vk::ImageMemoryBarrier{
.srcAccessMask = vk::AccessFlagBits::eMemoryWrite,
.dstAccessMask = vk::AccessFlagBits::eMemoryRead,
.oldLayout = vk::ImageLayout::ePresentSrcKHR,
.newLayout = vk::ImageLayout::eGeneral,
.image = rgb[index],
.subresourceRange = {.aspectMask = vk::ImageAspectFlagBits::eColor,
.baseMipLevel = 0,
.levelCount = 1,
.baseArrayLayer = 0,
.layerCount = 1},
},
};
cmd_buf.pipelineBarrier(
vk::PipelineStageFlagBits::eAllCommands,
vk::PipelineStageFlagBits::eComputeShader,
{},
nullptr,
nullptr,
im_barriers);
cmd_buf.bindPipeline(vk::PipelineBindPoint::eCompute, *pipeline);
cmd_buf.bindDescriptorSets(vk::PipelineBindPoint::eCompute, *layout, 0, ds, {});
cmd_buf.pushConstants<float[3][4]>(*layout, vk::ShaderStageFlagBits::eCompute, 0, COLORSPACE_BT709);
cmd_buf.dispatch(extent.width / 16, extent.height / 16, 1);
for (auto & barrier: im_barriers)
{
barrier.srcAccessMask = vk::AccessFlagBits::eMemoryWrite;
barrier.dstAccessMask = vk::AccessFlagBits::eTransferRead;
barrier.oldLayout = vk::ImageLayout::eGeneral;
barrier.newLayout = vk::ImageLayout::eTransferSrcOptimal;
}
cmd_buf.pipelineBarrier(
vk::PipelineStageFlagBits::eComputeShader,
vk::PipelineStageFlagBits::eTransfer,
{},
nullptr,
nullptr,
std::span{im_barriers.begin(), 2});
}
void yuv_converter::assemble_planes(vk::Rect2D rect, vk::raii::CommandBuffer & cmd_buf, vk::Image target)
{
vk::ImageMemoryBarrier target_barrier{
.srcAccessMask = vk::AccessFlagBits::eNone,
.dstAccessMask = vk::AccessFlagBits::eMemoryWrite,
.oldLayout = vk::ImageLayout::eUndefined,
.newLayout = vk::ImageLayout::eTransferDstOptimal,
.image = target,
.subresourceRange = {.aspectMask = vk::ImageAspectFlagBits::eColor,
.baseMipLevel = 0,
.levelCount = 1,
.baseArrayLayer = 0,
.layerCount = 1},
};
cmd_buf.pipelineBarrier(
vk::PipelineStageFlagBits::eNone,
vk::PipelineStageFlagBits::eTransfer,
{},
nullptr,
nullptr,
target_barrier);
cmd_buf.copyImage(
luma,
vk::ImageLayout::eTransferSrcOptimal,
target,
vk::ImageLayout::eTransferDstOptimal,
vk::ImageCopy{
.srcSubresource = {
.aspectMask = vk::ImageAspectFlagBits::eColor,
.layerCount = 1,
},
.srcOffset = {rect.offset.x, rect.offset.y, 0},
.dstSubresource = {
.aspectMask = vk::ImageAspectFlagBits::ePlane0,
.layerCount = 1,
},
.extent = {extent.width, extent.height, 1},
});
cmd_buf.copyImage(
chroma,
vk::ImageLayout::eTransferSrcOptimal,
target,
vk::ImageLayout::eTransferDstOptimal,
vk::ImageCopy{
.srcSubresource = {
.aspectMask = vk::ImageAspectFlagBits::eColor,
.layerCount = 1,
},
.srcOffset = {rect.offset.x / 2, rect.offset.y / 2, 0},
.dstSubresource = {
.aspectMask = vk::ImageAspectFlagBits::ePlane1,
.layerCount = 1,
},
.extent = {extent.width / 2, extent.height / 2, 1},
});
}
-56
View File
@@ -1,56 +0,0 @@
/*
* WiVRn VR streaming
* Copyright (C) 2022-2024 Guillaume Meunier <guillaume.meunier@centraliens.net>
* Copyright (C) 2022-2024 Patrick Nicolas <patricknicolas@laposte.net>
*
* 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
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include "vk/allocation.h"
#include <span>
#include <vector>
#include <vulkan/vulkan_raii.hpp>
class yuv_converter
{
public:
image_allocation luma;
image_allocation chroma;
private:
vk::Device device;
vk::Extent2D extent;
std::vector<vk::Image> rgb;
std::vector<vk::raii::ImageView> views_rgb;
vk::raii::ImageView view_luma = nullptr;
vk::raii::ImageView view_chroma = nullptr;
vk::raii::DescriptorSetLayout ds_layout = nullptr;
vk::raii::PipelineLayout layout = nullptr;
vk::raii::Pipeline pipeline = nullptr;
vk::raii::DescriptorPool dp = nullptr;
vk::DescriptorSet ds = nullptr;
public:
yuv_converter();
yuv_converter(vk::PhysicalDevice, vk::raii::Device & device, std::span<vk::Image> rgb, vk::Format format, vk::Extent2D extent);
// Converts the given image to yuv, stored in luma and chroma images.
// The output images will be in transfer src optimal layout
void record_draw_commands(vk::raii::CommandBuffer & cmd_buf, size_t index);
void assemble_planes(vk::Rect2D, vk::raii::CommandBuffer &, vk::Image target);
};