Integrate GPU conversion and update capture pipeline

This commit is contained in:
Mark.lin
2026-08-06 10:03:32 +08:00
parent 300cbdfd9b
commit 3309f822ec
33 changed files with 795 additions and 930 deletions
File diff suppressed because it is too large Load Diff
+125
View File
@@ -0,0 +1,125 @@
#pragma once
#include <d3d11_1.h>
#include <dxgi1_2.h>
#include <stdint.h>
#include <wrl.h>
namespace gvfg::internal
{
typedef enum
{
GVFG_RENDER_FMT_Y210,
GVFG_RENDER_FMT_YVYU
} gvfg_render_pixfmt_t;
typedef enum
{
GVFG_RENDER_PREVIEW_BITDEPTH_8BIT = 0,
GVFG_RENDER_PREVIEW_BITDEPTH_10BIT = 1,
GVFG_RENDER_PREVIEW_BITDEPTH_AUTO = 2
} gvfg_render_preview_bitdepth_t;
typedef enum
{
GVFG_PREVIEW_PRESENT_FAILED = 0,
GVFG_PREVIEW_PRESENTED = 1,
GVFG_PREVIEW_PRESENT_SKIPPED = 2
} gvfg_preview_present_result_t;
typedef struct
{
void *hwnd;
int enable_preview;
int swapchain_10bit;
} gvfg_render_preview_desc_t;
class D3DPreviewPipeline
{
public:
D3DPreviewPipeline() = default;
~D3DPreviewPipeline() = default;
bool initialize(ID3D11Device *d3d,
ID3D11DeviceContext *ctx);
bool configurePreview(const gvfg_render_preview_desc_t &desc);
void set_source_bit_depth(int bits);
void release_preview_swapchain();
bool create_shaders_and_states();
bool ensure_rt_and_pipeline(int w, int h);
bool ensure_preview_swapchain(int w, int h);
bool preview_swapchain_10bit() const { return preview_swapchain_10bit_; }
gvfg_preview_present_result_t present_preview(int src_w, int src_h);
DXGI_FORMAT preview_backbuffer_format() const;
DXGI_FORMAT scene_texture_format() const;
DXGI_FORMAT linear_fp16_texture_format() const;
bool blit_fp16_to_rgba8(int frame_w, int frame_h);
bool blit_fp16_to_rgb10a2(int frame_w, int frame_h);
bool upload_packed_422_frame(const uint8_t *data, int src_stride, int frame_w, int frame_h);
bool upload_y210_frame(const uint8_t *data, int src_stride, int frame_w, int frame_h);
bool render_uploaded_yuv_to_fp16(gvfg_render_pixfmt_t fmt, int frame_w, int frame_h);
bool copy_fp16_to_scene();
bool readback_to_buffer(void *destination,
uint64_t destination_size,
int destination_row_bytes,
DXGI_FORMAT destination_format,
int frame_w,
int frame_h);
ID3D11Device *d3d_ = nullptr;
ID3D11DeviceContext *ctx_ = nullptr;
Microsoft::WRL::ComPtr<ID3D11Texture2D> rt_fp16_;
Microsoft::WRL::ComPtr<ID3D11RenderTargetView> rtv_fp16_;
Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> srv_fp16_;
Microsoft::WRL::ComPtr<ID3D11Texture2D> rt_scene_fp16_;
Microsoft::WRL::ComPtr<ID3D11RenderTargetView> rtv_scene_fp16_;
Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> srv_scene_fp16_;
Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> srv_rgba_;
Microsoft::WRL::ComPtr<ID3D11PixelShader> ps_rgba8_to_preview_;
Microsoft::WRL::ComPtr<ID3D11Texture2D> rt_rgba_;
Microsoft::WRL::ComPtr<ID3D11Texture2D> rt_rgb10_;
Microsoft::WRL::ComPtr<ID3D11RenderTargetView> rtv_rgb10_;
Microsoft::WRL::ComPtr<ID3D11Texture2D> readback_bgra8_;
Microsoft::WRL::ComPtr<ID3D11Texture2D> readback_rgb10_;
Microsoft::WRL::ComPtr<ID3D11Texture2D> upload_yuy2_packed_;
Microsoft::WRL::ComPtr<ID3D11Texture2D> upload_y210_packed_;
Microsoft::WRL::ComPtr<ID3D11RenderTargetView> rtv_rgba_;
Microsoft::WRL::ComPtr<ID3D11Buffer> cs_params_;
Microsoft::WRL::ComPtr<ID3D11VertexShader> vs_;
Microsoft::WRL::ComPtr<ID3D11PixelShader> ps_yuy2_;
Microsoft::WRL::ComPtr<ID3D11PixelShader> ps_y210_;
Microsoft::WRL::ComPtr<ID3D11PixelShader> ps_fp16_to_rgba8_;
Microsoft::WRL::ComPtr<ID3D11PixelShader> ps_fp16_to_preview_;
Microsoft::WRL::ComPtr<ID3D11InputLayout> il_;
Microsoft::WRL::ComPtr<ID3D11Buffer> vb_;
Microsoft::WRL::ComPtr<ID3D11SamplerState> samp_;
void *preview_hwnd_ = nullptr;
bool preview_enabled_ = false;
int preview_swapchain_mode_ = GVFG_RENDER_PREVIEW_BITDEPTH_10BIT;
int preview_source_bit_depth_ = 0;
bool preview_swapchain_10bit_ = false;
DXGI_FORMAT preview_swapchain_format_ = DXGI_FORMAT_UNKNOWN;
int preview_w_ = 0;
int preview_h_ = 0;
int rt_w_ = 0;
int rt_h_ = 0;
Microsoft::WRL::ComPtr<IDXGISwapChain1> preview_swapchain_;
Microsoft::WRL::ComPtr<ID3D11Texture2D> preview_backbuf_;
Microsoft::WRL::ComPtr<ID3D11RenderTargetView> preview_rtv_;
};
}
+214
View File
@@ -0,0 +1,214 @@
#include "gvfg_capture.h"
#include "d3d_conversion_pipeline.h"
#include <d3d11_4.h>
#include <dxgi.h>
#include <cstdint>
#include <cstddef>
#include <iterator>
#include <memory>
#include <mutex>
#include <wrl/client.h>
using Microsoft::WRL::ComPtr;
#if INTPTR_MAX == INT64_MAX
static_assert(sizeof(gvfg_gpu_output_buffer_t) == 24,
"gvfg_gpu_output_buffer_t x64 ABI must remain frozen");
static_assert(offsetof(gvfg_gpu_output_buffer_t, data) == 0);
static_assert(offsetof(gvfg_gpu_output_buffer_t, data_size) == 8);
static_assert(offsetof(gvfg_gpu_output_buffer_t, row_bytes) == 16);
static_assert(offsetof(gvfg_gpu_output_buffer_t, pixel_format) == 20);
#endif
namespace
{
class GpuBufferConverter
{
public:
gvfg_status_t convert(const gvfg_frame_t &source,
const gvfg_gpu_output_buffer_t &output)
{
std::lock_guard<std::mutex> lock(mutex_);
if (!ensureDevice())
return GVFG_EIO;
if (!pipeline_)
pipeline_ = std::make_unique<gvfg::internal::D3DPreviewPipeline>();
if (!pipeline_->initialize(device_.Get(), context_.Get()) ||
!pipeline_->ensure_rt_and_pipeline(source.width, source.height))
return GVFG_EIO;
gvfg::internal::gvfg_render_pixfmt_t renderFormat{};
bool uploaded = false;
const auto *data = static_cast<const uint8_t *>(source.data);
switch (source.pixel_format)
{
case GVFG_PIXFMT_YVYU:
renderFormat = gvfg::internal::GVFG_RENDER_FMT_YVYU;
uploaded = pipeline_->upload_packed_422_frame(data,
source.row_stride_bytes,
source.width,
source.height);
break;
case GVFG_PIXFMT_Y210:
renderFormat = gvfg::internal::GVFG_RENDER_FMT_Y210;
uploaded = pipeline_->upload_y210_frame(data,
source.row_stride_bytes,
source.width,
source.height);
break;
default:
return GVFG_ENOTSUP;
}
if (!uploaded ||
!pipeline_->render_uploaded_yuv_to_fp16(renderFormat, source.width, source.height) ||
!pipeline_->copy_fp16_to_scene())
return GVFG_EIO;
DXGI_FORMAT outputFormat = DXGI_FORMAT_UNKNOWN;
bool rendered = false;
switch (output.pixel_format)
{
case GVFG_GPU_OUTPUT_BGRA8:
outputFormat = DXGI_FORMAT_B8G8R8A8_UNORM;
rendered = pipeline_->blit_fp16_to_rgba8(source.width, source.height);
break;
case GVFG_GPU_OUTPUT_RGB10A2:
outputFormat = DXGI_FORMAT_R10G10B10A2_UNORM;
rendered = pipeline_->blit_fp16_to_rgb10a2(source.width, source.height);
break;
default:
return GVFG_ENOTSUP;
}
if (!rendered ||
!pipeline_->readback_to_buffer(output.data,
output.data_size,
output.row_bytes,
outputFormat,
source.width,
source.height))
return GVFG_EIO;
return GVFG_OK;
}
private:
bool ensureDevice()
{
if (device_ && context_)
return true;
UINT flags = D3D11_CREATE_DEVICE_BGRA_SUPPORT;
#ifdef _DEBUG
flags |= D3D11_CREATE_DEVICE_DEBUG;
#endif
const D3D_FEATURE_LEVEL levels[] = {
D3D_FEATURE_LEVEL_11_1,
D3D_FEATURE_LEVEL_11_0,
};
D3D_FEATURE_LEVEL actual{};
HRESULT hr = D3D11CreateDevice(nullptr,
D3D_DRIVER_TYPE_HARDWARE,
nullptr,
flags,
levels,
static_cast<UINT>(std::size(levels)),
D3D11_SDK_VERSION,
&device_,
&actual,
&context_);
#ifdef _DEBUG
if (FAILED(hr))
{
flags &= ~D3D11_CREATE_DEVICE_DEBUG;
hr = D3D11CreateDevice(nullptr,
D3D_DRIVER_TYPE_HARDWARE,
nullptr,
flags,
levels,
static_cast<UINT>(std::size(levels)),
D3D11_SDK_VERSION,
&device_,
&actual,
&context_);
}
#endif
if (FAILED(hr) || !device_ || !context_)
return false;
ComPtr<ID3D11Multithread> multithread;
if (SUCCEEDED(device_.As(&multithread)) && multithread)
multithread->SetMultithreadProtected(TRUE);
return true;
}
std::mutex mutex_;
ComPtr<ID3D11Device> device_;
ComPtr<ID3D11DeviceContext> context_;
std::unique_ptr<gvfg::internal::D3DPreviewPipeline> pipeline_;
};
bool checkedFrameLayout(const gvfg_frame_t &source)
{
if (!source.data || source.width <= 0 || source.height <= 0 ||
source.row_stride_bytes <= 0)
return false;
uint64_t minimumRowBytes = 0;
switch (source.pixel_format)
{
case GVFG_PIXFMT_YVYU:
minimumRowBytes = static_cast<uint64_t>(source.width) * 2u;
break;
case GVFG_PIXFMT_Y210:
minimumRowBytes = static_cast<uint64_t>(source.width) * 4u;
break;
default:
return true;
}
if (static_cast<uint64_t>(source.row_stride_bytes) < minimumRowBytes)
return false;
const uint64_t rowOffset = static_cast<uint64_t>(source.row_stride_bytes) *
static_cast<uint64_t>(source.height - 1);
return rowOffset <= UINT64_MAX - minimumRowBytes &&
source.data_size >= rowOffset + minimumRowBytes;
}
GpuBufferConverter &sharedConverter()
{
static GpuBufferConverter converter;
return converter;
}
}
extern "C" GVFG_API gvfg_status_t gvfg_gpu_convert_to_buffer(
const gvfg_frame_t *source,
const gvfg_gpu_output_buffer_t *output)
{
if (!source || !output || !output->data ||
source->width <= 0 || source->height <= 0 ||
output->row_bytes <= 0)
return GVFG_EINVAL;
if (source->pixel_format != GVFG_PIXFMT_YVYU &&
source->pixel_format != GVFG_PIXFMT_Y210)
return GVFG_ENOTSUP;
if (output->pixel_format != GVFG_GPU_OUTPUT_BGRA8 &&
output->pixel_format != GVFG_GPU_OUTPUT_RGB10A2)
return GVFG_ENOTSUP;
if (!checkedFrameLayout(*source))
return GVFG_EINVAL;
const uint64_t minimumRowBytes = static_cast<uint64_t>(source->width) * 4u;
if (static_cast<uint64_t>(output->row_bytes) < minimumRowBytes)
return GVFG_EINVAL;
const uint64_t requiredSize = static_cast<uint64_t>(output->row_bytes) *
static_cast<uint64_t>(source->height);
if (output->data_size < requiredSize)
return GVFG_EINVAL;
return sharedConverter().convert(*source, *output);
}