Add GPU text overlays and streamline SDK conversion paths
- add an internal DirectWrite/Direct2D OSD core for YUY2 and Y210 with configurable position, font size, RGB color, and up to 32 printable ASCII characters - expose per-channel OSD set/clear APIs and blend only the visible overlay rectangle on the GPU while preserving the frame read/release contract and tracking native versus processed buffer ownership - convert native YUY2/Y210 directly to BGRA8, RGB10A2, or NV12 with reusable output resources, avoiding unnecessary FP16 intermediates and scene copies - draw native-size and reduced previews directly to the swapchain and allocate an FP16 intermediate only for enlarged previews while retaining the existing upload queue and format support - bump the SDK version to 1.0.2, fix the installed customer API reference path, update conversion and OSD documentation, and remove the old 0922 SDK archive Validation: git diff --cached --check passed before commit. No SDK rebuild or hardware runtime validation was performed in this commit pass.
This commit is contained in:
@@ -16,6 +16,8 @@ add_library(gvfg SHARED
|
||||
src/gpu/d3d_conversion_pipeline.cpp
|
||||
src/gpu/d3d_conversion_pipeline.h
|
||||
src/gpu/gvfg_gpu_convert.cpp
|
||||
src/gpu/gvfg_gpu_osd.cpp
|
||||
src/gpu/gvfg_gpu_osd.h
|
||||
src/gvfg_error_state.h
|
||||
src/gvfg_capture.cpp
|
||||
)
|
||||
@@ -46,6 +48,7 @@ target_compile_definitions(gvfg PRIVATE
|
||||
)
|
||||
|
||||
target_link_libraries(gvfg PRIVATE
|
||||
gvfg_osd_core
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/../../third_party/GigabyteLib/GvfgSdk.lib"
|
||||
setupapi
|
||||
user32
|
||||
|
||||
@@ -8,6 +8,8 @@ EXPORTS
|
||||
gvfg_set_channel_zero_copy_enabled
|
||||
gvfg_get_channel_zero_copy_enabled
|
||||
gvfg_set_channel_video_format
|
||||
gvfg_set_channel_osd
|
||||
gvfg_clear_channel_osd
|
||||
gvfg_set_channel_audio_enabled
|
||||
gvfg_get_channel_audio_format
|
||||
gvfg_start_channel
|
||||
|
||||
@@ -207,6 +207,23 @@ extern "C"
|
||||
uint64_t timestamp_ns; /* Monotonic SDK delivery time; same clock domain as audio. */
|
||||
} gvfg_frame_t;
|
||||
|
||||
/*
|
||||
* Text overlay applied by the SDK to subsequently delivered video frames.
|
||||
* The text is copied by gvfg_set_channel_osd(); the caller does not need to
|
||||
* keep the pointer alive after that function returns.
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
const char *text; /* Printable English ASCII text (0x20-0x7e). */
|
||||
uint32_t text_length; /* 1 through GVFG_OSD_MAX_TEXT_CHARACTERS. */
|
||||
int32_t x; /* Left position in output-frame pixels. */
|
||||
int32_t y; /* Top position in output-frame pixels. */
|
||||
uint32_t font_size_px; /* Font size in pixels; must be non-zero. */
|
||||
uint32_t color_rgb; /* 0x00RRGGBB. */
|
||||
} gvfg_channel_osd_config_t;
|
||||
|
||||
#define GVFG_OSD_MAX_TEXT_CHARACTERS 32u
|
||||
|
||||
typedef struct
|
||||
{
|
||||
const void *data; /* PCM data. Valid until gvfg_release_channel_audio_frame(). */
|
||||
@@ -321,13 +338,15 @@ extern "C"
|
||||
/* A handle may open both channels of the same device. */
|
||||
|
||||
/*
|
||||
* Select zero-copy frame delivery.
|
||||
* Select zero-copy frame acquisition.
|
||||
*
|
||||
* Call after gvfg_create() and before opening the selected channel. The
|
||||
* default is disabled independently for each channel. Once that channel is
|
||||
* open its mode cannot be changed. In zero-copy
|
||||
* mode, gvfg_read_channel_frame() returns borrowed memory and every
|
||||
* successful read must be paired with gvfg_release_channel_frame().
|
||||
* open its mode cannot be changed. This setting controls how the SDK
|
||||
* acquires native frames from the capture backend. SDK processing such as
|
||||
* OSD may return an SDK-owned output buffer even when acquisition uses
|
||||
* zero-copy. Every successful read must still be paired with
|
||||
* gvfg_release_channel_frame().
|
||||
*/
|
||||
GVFG_API gvfg_status_t gvfg_set_channel_zero_copy_enabled(
|
||||
GVFG_PARAM_IN gvfg_handle handle,
|
||||
@@ -349,6 +368,21 @@ extern "C"
|
||||
GVFG_PARAM_IN int channel_index,
|
||||
GVFG_PARAM_IN gvfg_pixel_format_t format);
|
||||
|
||||
/*
|
||||
* Enable or update SDK-side OSD for an opened channel. The configuration
|
||||
* is copied synchronously and applies to subsequent YUY2 or Y210 frames.
|
||||
* Text is rendered as BT.709 limited-range video and blended by the GPU.
|
||||
*/
|
||||
GVFG_API gvfg_status_t gvfg_set_channel_osd(
|
||||
GVFG_PARAM_IN gvfg_handle handle,
|
||||
GVFG_PARAM_IN int channel_index,
|
||||
GVFG_PARAM_IN const gvfg_channel_osd_config_t *config);
|
||||
|
||||
/* Disable SDK-side OSD for subsequently delivered frames. */
|
||||
GVFG_API gvfg_status_t gvfg_clear_channel_osd(
|
||||
GVFG_PARAM_IN gvfg_handle handle,
|
||||
GVFG_PARAM_IN int channel_index);
|
||||
|
||||
/*
|
||||
* Enable or disable audio for the next channel start. Video remains
|
||||
* enabled; applications do not need to construct stream flag masks.
|
||||
@@ -403,10 +437,10 @@ extern "C"
|
||||
* - GVFG_EBUSY if the device or capture resource is busy.
|
||||
* - GVFG_EIO for other capture backend failures.
|
||||
*
|
||||
* In copy mode the returned data pointer is owned by the SDK. In zero-copy
|
||||
* mode it points to driver-owned memory. In both modes it remains valid
|
||||
* until gvfg_release_channel_frame() is called, and each channel may hold
|
||||
* only one frame.
|
||||
* The returned data pointer is read-only and may refer to native capture
|
||||
* memory or an SDK-processed buffer. Applications must not free or modify
|
||||
* it. It remains valid until gvfg_release_channel_frame() is called, and
|
||||
* each channel may hold only one frame.
|
||||
*/
|
||||
GVFG_API gvfg_status_t gvfg_read_channel_frame(
|
||||
GVFG_PARAM_IN gvfg_handle handle,
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,154 +1,75 @@
|
||||
#pragma once
|
||||
|
||||
#include "../../include/gvfg_capture.h"
|
||||
#include <cstdint>
|
||||
namespace gvfg::internal {
|
||||
enum gvfg_render_pixfmt_t { GVFG_RENDER_FMT_YUY2=1, GVFG_RENDER_FMT_Y210=2,
|
||||
GVFG_RENDER_FMT_GRAY16=4, GVFG_RENDER_FMT_RGBA16=5, GVFG_RENDER_FMT_DICOM_YBR_FULL_422=6 };
|
||||
enum gvfg_render_preview_bitdepth_t { GVFG_RENDER_PREVIEW_BITDEPTH_8BIT=0,
|
||||
GVFG_RENDER_PREVIEW_BITDEPTH_10BIT=1, GVFG_RENDER_PREVIEW_BITDEPTH_AUTO=2 };
|
||||
enum gvfg_preview_present_result_t { GVFG_PREVIEW_PRESENT_FAILED=0,
|
||||
GVFG_PREVIEW_PRESENTED=1, GVFG_PREVIEW_PRESENT_SKIPPED=2 };
|
||||
struct gvfg_render_preview_desc_t { void *hwnd; int enable_preview; int swapchain_10bit; };
|
||||
}
|
||||
#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_YUY2,
|
||||
GVFG_RENDER_FMT_GRAY16,
|
||||
GVFG_RENDER_FMT_RGBA16,
|
||||
GVFG_RENDER_FMT_DICOM_YBR_FULL_422
|
||||
} 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
|
||||
{
|
||||
#include <wrl/client.h>
|
||||
#include <array>
|
||||
namespace gvfg::internal {
|
||||
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);
|
||||
bool initialize(ID3D11Device *, ID3D11DeviceContext *);
|
||||
bool ensure_states();
|
||||
bool ensure_export_target(int width, int height, int output_format);
|
||||
bool draw_rgb(ID3D11Texture2D *, ID3D11ShaderResourceView *, gvfg_render_pixfmt_t,
|
||||
int width, int height, int bits, ID3D11RenderTargetView *, const D3D11_VIEWPORT &);
|
||||
bool draw_uploaded_rgb(gvfg_render_pixfmt_t, int width, int height, DXGI_FORMAT);
|
||||
bool blit_uploaded_422_to_nv12(gvfg_render_pixfmt_t, int width, int height);
|
||||
bool upload_packed_422_frame(const uint8_t *, int stride, int width, int height);
|
||||
bool upload_y210_frame(const uint8_t *, int stride, int width, int height);
|
||||
bool readback_to_buffer(void *, uint64_t, int stride, DXGI_FORMAT, int width, int height);
|
||||
bool readback_nv12_to_buffer(void *, uint64_t, int stride, int width, int height);
|
||||
bool configurePreview(const gvfg_render_preview_desc_t &);
|
||||
void set_source_bit_depth(int);
|
||||
void release_preview_swapchain();
|
||||
|
||||
bool create_shaders_and_states();
|
||||
bool ensure_rt_and_pipeline(int w, int h, bool previewOnly = false);
|
||||
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);
|
||||
bool ensure_preview_swapchain(int width, int height);
|
||||
gvfg_preview_present_result_t present_texture(ID3D11Texture2D *, ID3D11ShaderResourceView *, gvfg_render_pixfmt_t, int width, int height, int bits);
|
||||
bool clear_preview_black();
|
||||
bool preview_swapchain_10bit() const { return preview_swapchain_10bit_; }
|
||||
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 blit_fp16_to_nv12(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 render_texture_to_fp16(ID3D11Texture2D *texture,
|
||||
gvfg_render_pixfmt_t fmt,
|
||||
int frame_w,
|
||||
int frame_h, ID3D11ShaderResourceView *cachedView = nullptr);
|
||||
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);
|
||||
bool readback_nv12_to_buffer(void *destination,
|
||||
uint64_t destination_size,
|
||||
int destination_row_bytes,
|
||||
int frame_w,
|
||||
int frame_h);
|
||||
|
||||
private:
|
||||
bool ensure_target(Microsoft::WRL::ComPtr<ID3D11Texture2D> &,
|
||||
Microsoft::WRL::ComPtr<ID3D11RenderTargetView> &, int, int, DXGI_FORMAT);
|
||||
bool ensure_input_view(ID3D11Texture2D *, Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> &);
|
||||
void bind_quad();
|
||||
bool upscale_rgb(ID3D11Texture2D *, ID3D11ShaderResourceView *, gvfg_render_pixfmt_t,
|
||||
int width, int height, int bits, ID3D11RenderTargetView *, const D3D11_VIEWPORT &);
|
||||
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> rt_nv12_y_;
|
||||
Microsoft::WRL::ComPtr<ID3D11RenderTargetView> rtv_nv12_y_;
|
||||
Microsoft::WRL::ComPtr<ID3D11Texture2D> rt_nv12_uv_;
|
||||
Microsoft::WRL::ComPtr<ID3D11RenderTargetView> rtv_nv12_uv_;
|
||||
Microsoft::WRL::ComPtr<ID3D11Texture2D> readback_nv12_y_;
|
||||
Microsoft::WRL::ComPtr<ID3D11Texture2D> readback_nv12_uv_;
|
||||
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_gray16_;
|
||||
Microsoft::WRL::ComPtr<ID3D11PixelShader> ps_rgba16_;
|
||||
Microsoft::WRL::ComPtr<ID3D11PixelShader> ps_dicom_ybr_full_422_;
|
||||
Microsoft::WRL::ComPtr<ID3D11PixelShader> ps_fp16_to_rgba8_;
|
||||
Microsoft::WRL::ComPtr<ID3D11PixelShader> ps_fp16_to_nv12_y_;
|
||||
Microsoft::WRL::ComPtr<ID3D11PixelShader> ps_fp16_to_nv12_uv_;
|
||||
Microsoft::WRL::ComPtr<ID3D11PixelShader> ps_fp16_to_preview_;
|
||||
Microsoft::WRL::ComPtr<ID3D11InputLayout> il_;
|
||||
Microsoft::WRL::ComPtr<ID3D11Buffer> vb_;
|
||||
Microsoft::WRL::ComPtr<ID3D11SamplerState> samp_;
|
||||
|
||||
Microsoft::WRL::ComPtr<ID3D11Buffer> vb_, cs_params_;
|
||||
Microsoft::WRL::ComPtr<ID3D11PixelShader> ps_rgb_[7][2];
|
||||
Microsoft::WRL::ComPtr<ID3D11PixelShader> ps_422_to_nv12_[2][2];
|
||||
Microsoft::WRL::ComPtr<ID3D11PixelShader> ps_scaled_rgb_;
|
||||
Microsoft::WRL::ComPtr<ID3D11SamplerState> linear_sampler_;
|
||||
Microsoft::WRL::ComPtr<ID3D11Texture2D> scaled_rgb_;
|
||||
Microsoft::WRL::ComPtr<ID3D11RenderTargetView> rtv_scaled_rgb_;
|
||||
Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> srv_scaled_rgb_;
|
||||
std::array<uint32_t,4> native_params_{};
|
||||
Microsoft::WRL::ComPtr<ID3D11Texture2D> upload_yuy2_packed_, upload_y210_packed_;
|
||||
Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> srv_uploaded_422_[2], native_view_;
|
||||
Microsoft::WRL::ComPtr<ID3D11Texture2D> rt_rgba_, rt_rgb10_, rt_nv12_y_, rt_nv12_uv_;
|
||||
Microsoft::WRL::ComPtr<ID3D11RenderTargetView> rtv_rgba_, rtv_rgb10_, rtv_nv12_y_, rtv_nv12_uv_;
|
||||
Microsoft::WRL::ComPtr<ID3D11Texture2D> readback_bgra8_, readback_rgb10_, readback_nv12_y_, readback_nv12_uv_;
|
||||
void *preview_hwnd_ = nullptr;
|
||||
bool preview_enabled_ = false;
|
||||
int preview_swapchain_mode_ = GVFG_RENDER_PREVIEW_BITDEPTH_10BIT;
|
||||
int preview_swapchain_mode_ = GVFG_RENDER_PREVIEW_BITDEPTH_AUTO;
|
||||
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;
|
||||
bool preview_only_ = false;
|
||||
int params_w_ = 0;
|
||||
int params_h_ = 0;
|
||||
int params_source_bit_depth_ = 0;
|
||||
|
||||
int preview_w_ = 0, preview_h_ = 0;
|
||||
Microsoft::WRL::ComPtr<IDXGISwapChain1> preview_swapchain_;
|
||||
Microsoft::WRL::ComPtr<ID3D11Texture2D> preview_backbuf_;
|
||||
Microsoft::WRL::ComPtr<ID3D11RenderTargetView> preview_rtv_;
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -37,7 +37,7 @@ public:
|
||||
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))
|
||||
!pipeline_->ensure_export_target(source.width, source.height, output.pixel_format))
|
||||
return GVFG_EIO;
|
||||
|
||||
gvfg::internal::gvfg_render_pixfmt_t renderFormat{};
|
||||
@@ -63,10 +63,16 @@ public:
|
||||
return GVFG_ENOTSUP;
|
||||
}
|
||||
|
||||
if (!uploaded ||
|
||||
!pipeline_->render_uploaded_yuv_to_fp16(renderFormat, source.width, source.height) ||
|
||||
!pipeline_->copy_fp16_to_scene())
|
||||
return GVFG_EIO;
|
||||
if (!uploaded) return GVFG_EIO;
|
||||
|
||||
if (output.pixel_format == GVFG_GPU_OUTPUT_NV12)
|
||||
{
|
||||
if (!pipeline_->blit_uploaded_422_to_nv12(renderFormat, source.width, source.height) ||
|
||||
!pipeline_->readback_nv12_to_buffer(output.data, output.data_size, output.row_bytes,
|
||||
source.width, source.height))
|
||||
return GVFG_EIO;
|
||||
return GVFG_OK;
|
||||
}
|
||||
|
||||
DXGI_FORMAT outputFormat = DXGI_FORMAT_UNKNOWN;
|
||||
bool rendered = false;
|
||||
@@ -74,22 +80,12 @@ public:
|
||||
{
|
||||
case GVFG_GPU_OUTPUT_BGRA8:
|
||||
outputFormat = DXGI_FORMAT_B8G8R8A8_UNORM;
|
||||
rendered = pipeline_->blit_fp16_to_rgba8(source.width, source.height);
|
||||
rendered = pipeline_->draw_uploaded_rgb(renderFormat, source.width, source.height, outputFormat);
|
||||
break;
|
||||
case GVFG_GPU_OUTPUT_RGB10A2:
|
||||
outputFormat = DXGI_FORMAT_R10G10B10A2_UNORM;
|
||||
rendered = pipeline_->blit_fp16_to_rgb10a2(source.width, source.height);
|
||||
rendered = pipeline_->draw_uploaded_rgb(renderFormat, source.width, source.height, outputFormat);
|
||||
break;
|
||||
case GVFG_GPU_OUTPUT_NV12:
|
||||
rendered = pipeline_->blit_fp16_to_nv12(source.width, source.height);
|
||||
if (!rendered ||
|
||||
!pipeline_->readback_nv12_to_buffer(output.data,
|
||||
output.data_size,
|
||||
output.row_bytes,
|
||||
source.width,
|
||||
source.height))
|
||||
return GVFG_EIO;
|
||||
return GVFG_OK;
|
||||
default:
|
||||
return GVFG_ENOTSUP;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,543 @@
|
||||
#include "gvfg_gpu_osd.h"
|
||||
|
||||
#include "gvfg_osd.h"
|
||||
|
||||
#include <d3d11_4.h>
|
||||
#include <d3dcompiler.h>
|
||||
#include <wrl/client.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <iterator>
|
||||
#include <limits>
|
||||
#include <mutex>
|
||||
#include <utility>
|
||||
|
||||
using Microsoft::WRL::ComPtr;
|
||||
|
||||
namespace gvfg::internal
|
||||
{
|
||||
namespace
|
||||
{
|
||||
constexpr char kOsdBlendShader[] = R"(
|
||||
Texture2D<uint4> sourceTexture : register(t0);
|
||||
Texture2D<uint4> osdTexture : register(t1);
|
||||
Texture2D<uint2> maskTexture : register(t2);
|
||||
RWTexture2D<uint4> outputTexture : register(u0);
|
||||
|
||||
cbuffer BlendParameters : register(b0)
|
||||
{
|
||||
uint outputValueMask;
|
||||
uint3 padding;
|
||||
};
|
||||
|
||||
uint blendComponent(uint sourceValue, uint osdValue, uint alpha)
|
||||
{
|
||||
return ((sourceValue * (255u - alpha) + osdValue * alpha + 127u) / 255u) &
|
||||
outputValueMask;
|
||||
}
|
||||
|
||||
[numthreads(16, 16, 1)]
|
||||
void main(uint3 id : SV_DispatchThreadID)
|
||||
{
|
||||
uint width = 0;
|
||||
uint height = 0;
|
||||
outputTexture.GetDimensions(width, height);
|
||||
if (id.x >= width || id.y >= height)
|
||||
return;
|
||||
|
||||
const uint2 location = id.xy;
|
||||
const uint4 sourceValue = sourceTexture.Load(uint3(location, 0));
|
||||
const uint2 maskValue = maskTexture.Load(uint3(location, 0));
|
||||
const uint pairAlpha = max(maskValue.x, maskValue.y);
|
||||
if (pairAlpha == 0u)
|
||||
{
|
||||
outputTexture[location] = sourceValue;
|
||||
return;
|
||||
}
|
||||
const uint4 osdValue = osdTexture.Load(uint3(location, 0));
|
||||
|
||||
uint4 result;
|
||||
result.x = blendComponent(sourceValue.x, osdValue.x, maskValue.x);
|
||||
result.y = blendComponent(sourceValue.y, osdValue.y, pairAlpha);
|
||||
result.z = blendComponent(sourceValue.z, osdValue.z, maskValue.y);
|
||||
result.w = blendComponent(sourceValue.w, osdValue.w, pairAlpha);
|
||||
outputTexture[location] = result;
|
||||
}
|
||||
)";
|
||||
|
||||
bool copyRowsToMapped(const uint8_t *source, int sourceStride,
|
||||
uint32_t rowBytes, uint32_t height,
|
||||
const D3D11_MAPPED_SUBRESOURCE &mapped)
|
||||
{
|
||||
if (!source || sourceStride < 0 || static_cast<uint32_t>(sourceStride) < rowBytes ||
|
||||
mapped.RowPitch < rowBytes)
|
||||
return false;
|
||||
if (static_cast<uint32_t>(sourceStride) == rowBytes && mapped.RowPitch == rowBytes)
|
||||
{
|
||||
std::memcpy(mapped.pData, source, static_cast<size_t>(rowBytes) * height);
|
||||
return true;
|
||||
}
|
||||
for (uint32_t y = 0; y < height; ++y)
|
||||
{
|
||||
std::memcpy(static_cast<uint8_t *>(mapped.pData) + static_cast<size_t>(y) * mapped.RowPitch,
|
||||
source + static_cast<size_t>(y) * static_cast<size_t>(sourceStride),
|
||||
rowBytes);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
struct OsdRegion
|
||||
{
|
||||
uint32_t left = 0;
|
||||
uint32_t top = 0;
|
||||
uint32_t width = 0;
|
||||
uint32_t height = 0;
|
||||
|
||||
bool empty() const { return width == 0 || height == 0; }
|
||||
};
|
||||
|
||||
OsdRegion findCoveredRegion(const std::vector<uint8_t> &mask,
|
||||
uint32_t frameWidth, uint32_t frameHeight)
|
||||
{
|
||||
uint32_t left = frameWidth;
|
||||
uint32_t top = frameHeight;
|
||||
uint32_t right = 0;
|
||||
uint32_t bottom = 0;
|
||||
for (uint32_t row = 0; row < frameHeight; ++row)
|
||||
{
|
||||
const uint8_t *maskRow = mask.data() + static_cast<size_t>(row) * frameWidth;
|
||||
for (uint32_t column = 0; column < frameWidth; column += 2u)
|
||||
{
|
||||
// Keep both luma samples and their shared U/V inside the region.
|
||||
if (maskRow[column] == 0 && maskRow[column + 1u] == 0)
|
||||
continue;
|
||||
left = (std::min)(left, column);
|
||||
right = (std::max)(right, column + 2u);
|
||||
top = (std::min)(top, row);
|
||||
bottom = row + 1u;
|
||||
}
|
||||
}
|
||||
return right > left ? OsdRegion{left, top, right - left, bottom - top}
|
||||
: OsdRegion{};
|
||||
}
|
||||
|
||||
void copyFrameRows(const gvfg_frame_t &source, uint8_t *destination,
|
||||
uint32_t rowBytes)
|
||||
{
|
||||
const auto *sourceData = static_cast<const uint8_t *>(source.data);
|
||||
if (static_cast<uint32_t>(source.row_stride_bytes) == rowBytes)
|
||||
{
|
||||
std::memcpy(destination, sourceData,
|
||||
static_cast<size_t>(rowBytes) * static_cast<size_t>(source.height));
|
||||
return;
|
||||
}
|
||||
for (int row = 0; row < source.height; ++row)
|
||||
{
|
||||
std::memcpy(destination + static_cast<size_t>(row) * rowBytes,
|
||||
sourceData + static_cast<size_t>(row) * source.row_stride_bytes,
|
||||
rowBytes);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct GpuOsdProcessor::Impl
|
||||
{
|
||||
gvfg_status_t configure(const gvfg_channel_osd_config_t &value)
|
||||
{
|
||||
if (!value.text || value.text_length == 0 ||
|
||||
value.text_length > GVFG_OSD_MAX_TEXT_CHARACTERS ||
|
||||
value.font_size_px == 0)
|
||||
return GVFG_EINVAL;
|
||||
for (uint32_t i = 0; i < value.text_length; ++i)
|
||||
{
|
||||
const unsigned char c = static_cast<unsigned char>(value.text[i]);
|
||||
if (c < 0x20 || c > 0x7e)
|
||||
return GVFG_EINVAL;
|
||||
}
|
||||
|
||||
std::lock_guard<std::mutex> lock(mutex);
|
||||
text.assign(value.text, value.text + value.text_length);
|
||||
x = value.x;
|
||||
y = value.y;
|
||||
fontSize = value.font_size_px;
|
||||
colorRgb = value.color_rgb & 0x00ffffffu;
|
||||
active = true;
|
||||
surfaceDirty = true;
|
||||
return GVFG_OK;
|
||||
}
|
||||
|
||||
void clear()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex);
|
||||
active = false;
|
||||
text.clear();
|
||||
surfaceDirty = true;
|
||||
}
|
||||
|
||||
bool ensureDevice()
|
||||
{
|
||||
if (device && context && shader && blendParameters)
|
||||
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<ID3DBlob> bytecode;
|
||||
ComPtr<ID3DBlob> errors;
|
||||
hr = D3DCompile(kOsdBlendShader, sizeof(kOsdBlendShader) - 1,
|
||||
"gvfg_osd_blend", nullptr, nullptr, "main", "cs_5_0",
|
||||
D3DCOMPILE_ENABLE_STRICTNESS, 0, &bytecode, &errors);
|
||||
if (FAILED(hr) || !bytecode)
|
||||
return false;
|
||||
if (FAILED(device->CreateComputeShader(bytecode->GetBufferPointer(),
|
||||
bytecode->GetBufferSize(),
|
||||
nullptr, &shader)))
|
||||
return false;
|
||||
|
||||
D3D11_BUFFER_DESC parameterDesc{};
|
||||
parameterDesc.ByteWidth = 16;
|
||||
parameterDesc.Usage = D3D11_USAGE_DEFAULT;
|
||||
parameterDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
|
||||
return SUCCEEDED(device->CreateBuffer(¶meterDesc, nullptr, &blendParameters));
|
||||
}
|
||||
|
||||
bool ensureTexture(ComPtr<ID3D11Texture2D> &texture,
|
||||
uint32_t textureWidth, uint32_t textureHeight,
|
||||
DXGI_FORMAT format, D3D11_USAGE usage,
|
||||
UINT bindFlags, UINT cpuAccess)
|
||||
{
|
||||
if (texture)
|
||||
{
|
||||
D3D11_TEXTURE2D_DESC current{};
|
||||
texture->GetDesc(¤t);
|
||||
if (current.Width == textureWidth && current.Height == textureHeight &&
|
||||
current.Format == format && current.Usage == usage &&
|
||||
current.BindFlags == bindFlags && current.CPUAccessFlags == cpuAccess)
|
||||
return true;
|
||||
texture.Reset();
|
||||
}
|
||||
D3D11_TEXTURE2D_DESC desc{};
|
||||
desc.Width = textureWidth;
|
||||
desc.Height = textureHeight;
|
||||
desc.MipLevels = 1;
|
||||
desc.ArraySize = 1;
|
||||
desc.Format = format;
|
||||
desc.SampleDesc.Count = 1;
|
||||
desc.Usage = usage;
|
||||
desc.BindFlags = bindFlags;
|
||||
desc.CPUAccessFlags = cpuAccess;
|
||||
return SUCCEEDED(device->CreateTexture2D(&desc, nullptr, &texture));
|
||||
}
|
||||
|
||||
bool ensureResources(uint32_t regionWidth, uint32_t regionHeight, int pixelFormat)
|
||||
{
|
||||
if ((regionWidth & 1u) != 0 || regionWidth == 0 || regionHeight == 0)
|
||||
return false;
|
||||
const DXGI_FORMAT packedFormat = pixelFormat == GVFG_PIXFMT_YUY2
|
||||
? DXGI_FORMAT_R8G8B8A8_UINT
|
||||
: pixelFormat == GVFG_PIXFMT_Y210
|
||||
? DXGI_FORMAT_R16G16B16A16_UINT
|
||||
: DXGI_FORMAT_UNKNOWN;
|
||||
if (packedFormat == DXGI_FORMAT_UNKNOWN)
|
||||
return false;
|
||||
if (resourceWidth != regionWidth || resourceHeight != regionHeight ||
|
||||
resourceFormat != pixelFormat || !sourceSrv || !osdSrv || !maskSrv || !outputUav)
|
||||
{
|
||||
// Failed partial recreation must never leave views cached against
|
||||
// old textures, including when the next request returns to an old size.
|
||||
sourceSrv.Reset();
|
||||
osdSrv.Reset();
|
||||
maskSrv.Reset();
|
||||
outputUav.Reset();
|
||||
resourceWidth = 0;
|
||||
resourceHeight = 0;
|
||||
resourceFormat = GVFG_PIXFMT_UNKNOWN;
|
||||
}
|
||||
const uint32_t pairWidth = regionWidth / 2u;
|
||||
if (!ensureTexture(sourceTexture, pairWidth, regionHeight, packedFormat,
|
||||
D3D11_USAGE_DYNAMIC, D3D11_BIND_SHADER_RESOURCE,
|
||||
D3D11_CPU_ACCESS_WRITE) ||
|
||||
!ensureTexture(osdTexture, pairWidth, regionHeight, packedFormat,
|
||||
D3D11_USAGE_DYNAMIC, D3D11_BIND_SHADER_RESOURCE,
|
||||
D3D11_CPU_ACCESS_WRITE) ||
|
||||
!ensureTexture(maskTexture, pairWidth, regionHeight, DXGI_FORMAT_R8G8_UINT,
|
||||
D3D11_USAGE_DYNAMIC, D3D11_BIND_SHADER_RESOURCE,
|
||||
D3D11_CPU_ACCESS_WRITE) ||
|
||||
!ensureTexture(outputTexture, pairWidth, regionHeight, packedFormat,
|
||||
D3D11_USAGE_DEFAULT, D3D11_BIND_UNORDERED_ACCESS, 0) ||
|
||||
!ensureTexture(readbackTexture, pairWidth, regionHeight, packedFormat,
|
||||
D3D11_USAGE_STAGING, 0, D3D11_CPU_ACCESS_READ))
|
||||
return false;
|
||||
|
||||
if (!sourceSrv || resourceWidth != regionWidth || resourceHeight != regionHeight ||
|
||||
resourceFormat != pixelFormat)
|
||||
{
|
||||
sourceSrv.Reset();
|
||||
osdSrv.Reset();
|
||||
maskSrv.Reset();
|
||||
outputUav.Reset();
|
||||
if (FAILED(device->CreateShaderResourceView(sourceTexture.Get(), nullptr, &sourceSrv)) ||
|
||||
FAILED(device->CreateShaderResourceView(osdTexture.Get(), nullptr, &osdSrv)) ||
|
||||
FAILED(device->CreateShaderResourceView(maskTexture.Get(), nullptr, &maskSrv)) ||
|
||||
FAILED(device->CreateUnorderedAccessView(outputTexture.Get(), nullptr, &outputUav)))
|
||||
return false;
|
||||
resourceWidth = regionWidth;
|
||||
resourceHeight = regionHeight;
|
||||
resourceFormat = pixelFormat;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool rebuildSurface(uint32_t frameWidth, uint32_t frameHeight, int pixelFormat)
|
||||
{
|
||||
// Any failed rebuild must be retried, even if input dimensions then
|
||||
// return to the previously cached frame size.
|
||||
surfaceDirty = true;
|
||||
const uint32_t yuvStride = frameWidth * (pixelFormat == GVFG_PIXFMT_YUY2 ? 2u : 4u);
|
||||
const uint32_t maskStride = frameWidth;
|
||||
yuvSurface.resize(static_cast<size_t>(yuvStride) * frameHeight);
|
||||
maskSurface.resize(static_cast<size_t>(maskStride) * frameHeight);
|
||||
|
||||
gvfg_osd_handle handle = nullptr;
|
||||
if (gvfg_osd_create(&handle) != GVFG_OSD_OK)
|
||||
return false;
|
||||
gvfg_osd_config_t config{};
|
||||
config.struct_size = sizeof(config);
|
||||
config.width = frameWidth;
|
||||
config.height = frameHeight;
|
||||
config.pixel_format = pixelFormat == GVFG_PIXFMT_YUY2
|
||||
? GVFG_OSD_PIXFMT_YUY2
|
||||
: GVFG_OSD_PIXFMT_Y210;
|
||||
config.text = text.c_str();
|
||||
config.text_length = static_cast<uint32_t>(text.size());
|
||||
config.x = x;
|
||||
config.y = y;
|
||||
config.font_size_px = fontSize;
|
||||
config.color_rgb = colorRgb;
|
||||
|
||||
gvfg_osd_output_t output{};
|
||||
output.struct_size = sizeof(output);
|
||||
output.yuv_data = yuvSurface.data();
|
||||
output.yuv_size = yuvSurface.size();
|
||||
output.yuv_stride = yuvStride;
|
||||
output.mask_data = maskSurface.data();
|
||||
output.mask_size = maskSurface.size();
|
||||
output.mask_stride = maskStride;
|
||||
const gvfg_osd_status_t status = gvfg_osd_render(handle, &config, &output);
|
||||
gvfg_osd_destroy(handle);
|
||||
if (status != GVFG_OSD_OK)
|
||||
return false;
|
||||
|
||||
region = findCoveredRegion(maskSurface, frameWidth, frameHeight);
|
||||
if (region.empty())
|
||||
{
|
||||
surfaceWidth = frameWidth;
|
||||
surfaceHeight = frameHeight;
|
||||
surfaceFormat = pixelFormat;
|
||||
surfaceDirty = false;
|
||||
return true;
|
||||
}
|
||||
if (!ensureDevice() || !ensureResources(region.width, region.height, pixelFormat))
|
||||
return false;
|
||||
|
||||
const uint32_t bytesPerPixel = pixelFormat == GVFG_PIXFMT_YUY2 ? 2u : 4u;
|
||||
const uint32_t regionRowBytes = region.width * bytesPerPixel;
|
||||
const size_t yuvOffset = static_cast<size_t>(region.top) * yuvStride +
|
||||
static_cast<size_t>(region.left) * bytesPerPixel;
|
||||
const size_t maskOffset = static_cast<size_t>(region.top) * maskStride + region.left;
|
||||
|
||||
D3D11_MAPPED_SUBRESOURCE mapped{};
|
||||
if (FAILED(context->Map(osdTexture.Get(), 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped)))
|
||||
return false;
|
||||
const bool copiedYuv = copyRowsToMapped(
|
||||
yuvSurface.data() + yuvOffset, static_cast<int>(yuvStride),
|
||||
regionRowBytes, region.height, mapped);
|
||||
context->Unmap(osdTexture.Get(), 0);
|
||||
if (!copiedYuv)
|
||||
return false;
|
||||
|
||||
if (FAILED(context->Map(maskTexture.Get(), 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped)))
|
||||
return false;
|
||||
const bool copiedMask = copyRowsToMapped(
|
||||
maskSurface.data() + maskOffset, static_cast<int>(maskStride),
|
||||
region.width, region.height, mapped);
|
||||
context->Unmap(maskTexture.Get(), 0);
|
||||
if (!copiedMask)
|
||||
return false;
|
||||
|
||||
const uint32_t blendValues[4] = {
|
||||
pixelFormat == GVFG_PIXFMT_Y210 ? 0xffc0u : 0x00ffu,
|
||||
0u, 0u, 0u};
|
||||
context->UpdateSubresource(blendParameters.Get(), 0, nullptr, blendValues, 0, 0);
|
||||
surfaceWidth = frameWidth;
|
||||
surfaceHeight = frameHeight;
|
||||
surfaceFormat = pixelFormat;
|
||||
surfaceDirty = false;
|
||||
return true;
|
||||
}
|
||||
|
||||
gvfg_status_t process(const gvfg_frame_t &source,
|
||||
std::vector<uint8_t> &destination,
|
||||
int &destinationStride,
|
||||
bool &applied)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex);
|
||||
applied = false;
|
||||
if (!active)
|
||||
return GVFG_OK;
|
||||
if (!source.data || source.width <= 0 || source.height <= 0 ||
|
||||
(source.width & 1) != 0 || source.row_stride_bytes <= 0 ||
|
||||
(source.pixel_format != GVFG_PIXFMT_YUY2 &&
|
||||
source.pixel_format != GVFG_PIXFMT_Y210))
|
||||
return GVFG_EINVAL;
|
||||
const uint64_t rowBytes64 = static_cast<uint64_t>(source.width) *
|
||||
(source.pixel_format == GVFG_PIXFMT_YUY2 ? 2u : 4u);
|
||||
if (rowBytes64 > static_cast<uint64_t>((std::numeric_limits<int>::max)()) ||
|
||||
static_cast<uint64_t>(source.row_stride_bytes) < rowBytes64)
|
||||
return GVFG_EINVAL;
|
||||
const uint64_t requiredSource =
|
||||
static_cast<uint64_t>(source.row_stride_bytes) *
|
||||
static_cast<uint64_t>(source.height - 1) +
|
||||
rowBytes64;
|
||||
if (source.data_size < requiredSource)
|
||||
return GVFG_EINVAL;
|
||||
|
||||
if ((surfaceDirty || surfaceWidth != static_cast<uint32_t>(source.width) ||
|
||||
surfaceHeight != static_cast<uint32_t>(source.height) ||
|
||||
surfaceFormat != source.pixel_format) &&
|
||||
!rebuildSurface(static_cast<uint32_t>(source.width),
|
||||
static_cast<uint32_t>(source.height), source.pixel_format))
|
||||
return GVFG_EIO;
|
||||
// Invisible text needs no processed buffer or GPU work.
|
||||
if (region.empty())
|
||||
return GVFG_OK;
|
||||
|
||||
const uint32_t rowBytes = static_cast<uint32_t>(rowBytes64);
|
||||
const uint32_t bytesPerPixel = source.pixel_format == GVFG_PIXFMT_YUY2 ? 2u : 4u;
|
||||
const uint32_t regionRowBytes = region.width * bytesPerPixel;
|
||||
const size_t sourceOffset = static_cast<size_t>(region.top) * source.row_stride_bytes +
|
||||
static_cast<size_t>(region.left) * bytesPerPixel;
|
||||
destination.resize(static_cast<size_t>(rowBytes) * static_cast<size_t>(source.height));
|
||||
D3D11_MAPPED_SUBRESOURCE mapped{};
|
||||
if (FAILED(context->Map(sourceTexture.Get(), 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped)))
|
||||
return GVFG_EIO;
|
||||
const bool copiedSource = copyRowsToMapped(
|
||||
static_cast<const uint8_t *>(source.data) + sourceOffset,
|
||||
source.row_stride_bytes, regionRowBytes, region.height, mapped);
|
||||
context->Unmap(sourceTexture.Get(), 0);
|
||||
if (!copiedSource)
|
||||
return GVFG_EIO;
|
||||
|
||||
ID3D11ShaderResourceView *srvs[] = {sourceSrv.Get(), osdSrv.Get(), maskSrv.Get()};
|
||||
ID3D11UnorderedAccessView *uavs[] = {outputUav.Get()};
|
||||
context->CSSetShader(shader.Get(), nullptr, 0);
|
||||
ID3D11Buffer *constantBuffers[] = {blendParameters.Get()};
|
||||
context->CSSetConstantBuffers(0, 1, constantBuffers);
|
||||
context->CSSetShaderResources(0, 3, srvs);
|
||||
context->CSSetUnorderedAccessViews(0, 1, uavs, nullptr);
|
||||
context->Dispatch((region.width / 2u + 15u) / 16u,
|
||||
(region.height + 15u) / 16u, 1);
|
||||
ID3D11ShaderResourceView *nullSrvs[] = {nullptr, nullptr, nullptr};
|
||||
ID3D11UnorderedAccessView *nullUav = nullptr;
|
||||
context->CSSetShaderResources(0, 3, nullSrvs);
|
||||
context->CSSetUnorderedAccessViews(0, 1, &nullUav, nullptr);
|
||||
ID3D11Buffer *nullBuffer = nullptr;
|
||||
context->CSSetConstantBuffers(0, 1, &nullBuffer);
|
||||
context->CSSetShader(nullptr, nullptr, 0);
|
||||
|
||||
context->CopyResource(readbackTexture.Get(), outputTexture.Get());
|
||||
// CPU only preserves the native frame; all blending remains on the GPU.
|
||||
// Copy before Map READ so it can overlap queued GPU work.
|
||||
copyFrameRows(source, destination.data(), rowBytes);
|
||||
if (FAILED(context->Map(readbackTexture.Get(), 0, D3D11_MAP_READ, 0, &mapped)))
|
||||
return GVFG_EIO;
|
||||
if (mapped.RowPitch < regionRowBytes)
|
||||
{
|
||||
context->Unmap(readbackTexture.Get(), 0);
|
||||
return GVFG_EIO;
|
||||
}
|
||||
const size_t destinationOffset = static_cast<size_t>(region.top) * rowBytes +
|
||||
static_cast<size_t>(region.left) * bytesPerPixel;
|
||||
for (uint32_t row = 0; row < region.height; ++row)
|
||||
{
|
||||
std::memcpy(destination.data() + destinationOffset + static_cast<size_t>(row) * rowBytes,
|
||||
static_cast<const uint8_t *>(mapped.pData) +
|
||||
static_cast<size_t>(row) * mapped.RowPitch,
|
||||
regionRowBytes);
|
||||
}
|
||||
context->Unmap(readbackTexture.Get(), 0);
|
||||
destinationStride = static_cast<int>(rowBytes);
|
||||
applied = true;
|
||||
return GVFG_OK;
|
||||
}
|
||||
|
||||
mutable std::mutex mutex;
|
||||
bool active = false;
|
||||
bool surfaceDirty = true;
|
||||
std::string text;
|
||||
int32_t x = 0;
|
||||
int32_t y = 0;
|
||||
uint32_t fontSize = 0;
|
||||
uint32_t colorRgb = 0xffffffu;
|
||||
OsdRegion region;
|
||||
uint32_t surfaceWidth = 0;
|
||||
uint32_t surfaceHeight = 0;
|
||||
int surfaceFormat = GVFG_PIXFMT_UNKNOWN;
|
||||
uint32_t resourceWidth = 0;
|
||||
uint32_t resourceHeight = 0;
|
||||
int resourceFormat = GVFG_PIXFMT_UNKNOWN;
|
||||
std::vector<uint8_t> yuvSurface;
|
||||
std::vector<uint8_t> maskSurface;
|
||||
ComPtr<ID3D11Device> device;
|
||||
ComPtr<ID3D11DeviceContext> context;
|
||||
ComPtr<ID3D11ComputeShader> shader;
|
||||
ComPtr<ID3D11Buffer> blendParameters;
|
||||
ComPtr<ID3D11Texture2D> sourceTexture;
|
||||
ComPtr<ID3D11Texture2D> osdTexture;
|
||||
ComPtr<ID3D11Texture2D> maskTexture;
|
||||
ComPtr<ID3D11Texture2D> outputTexture;
|
||||
ComPtr<ID3D11Texture2D> readbackTexture;
|
||||
ComPtr<ID3D11ShaderResourceView> sourceSrv;
|
||||
ComPtr<ID3D11ShaderResourceView> osdSrv;
|
||||
ComPtr<ID3D11ShaderResourceView> maskSrv;
|
||||
ComPtr<ID3D11UnorderedAccessView> outputUav;
|
||||
};
|
||||
|
||||
GpuOsdProcessor::GpuOsdProcessor() : impl_(std::make_unique<Impl>()) {}
|
||||
GpuOsdProcessor::~GpuOsdProcessor() = default;
|
||||
gvfg_status_t GpuOsdProcessor::configure(const gvfg_channel_osd_config_t &config)
|
||||
{
|
||||
return impl_->configure(config);
|
||||
}
|
||||
void GpuOsdProcessor::clear() { impl_->clear(); }
|
||||
gvfg_status_t GpuOsdProcessor::process(const gvfg_frame_t &source,
|
||||
std::vector<uint8_t> &destination,
|
||||
int &destinationStride,
|
||||
bool &applied)
|
||||
{
|
||||
return impl_->process(source, destination, destinationStride, applied);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
#pragma once
|
||||
|
||||
#include "gvfg_capture.h"
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace gvfg::internal
|
||||
{
|
||||
class GpuOsdProcessor
|
||||
{
|
||||
public:
|
||||
GpuOsdProcessor();
|
||||
~GpuOsdProcessor();
|
||||
|
||||
gvfg_status_t configure(const gvfg_channel_osd_config_t &config);
|
||||
void clear();
|
||||
gvfg_status_t process(const gvfg_frame_t &source,
|
||||
std::vector<uint8_t> &destination,
|
||||
int &destinationStride,
|
||||
bool &applied);
|
||||
|
||||
private:
|
||||
struct Impl;
|
||||
std::unique_ptr<Impl> impl_;
|
||||
};
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
#include "gvfg_debug.h"
|
||||
#include "gigabyte_capture_session.h"
|
||||
#include "gpu/gvfg_gpu_osd.h"
|
||||
#include "gvfgframework.h"
|
||||
|
||||
#include <algorithm>
|
||||
@@ -398,7 +399,7 @@ struct gvfg_channel_session_t
|
||||
return st;
|
||||
}
|
||||
|
||||
if (!frame.data || frame.width == 0 || frame.height == 0)
|
||||
if (!frame.data || frame.width <= 0 || frame.height <= 0)
|
||||
{
|
||||
session->release_frame();
|
||||
std::lock_guard<std::mutex> lock(frameMutex);
|
||||
@@ -406,6 +407,47 @@ struct gvfg_channel_session_t
|
||||
return reject(GVFG_EIO, "gvfg_read_channel_frame received an invalid frame descriptor");
|
||||
}
|
||||
|
||||
int nativeStride = 0;
|
||||
const gvfg_status_t strideStatus =
|
||||
native_row_stride_bytes(static_cast<uint32_t>(frame.width),
|
||||
frame.pixel_format, nativeStride);
|
||||
if (strideStatus != GVFG_OK || frame.row_stride_bytes < nativeStride)
|
||||
{
|
||||
session->release_frame();
|
||||
std::lock_guard<std::mutex> lock(frameMutex);
|
||||
readInProgress = false;
|
||||
return reject(strideStatus == GVFG_OK ? GVFG_EIO : strideStatus,
|
||||
"gvfg_read_channel_frame received an invalid row stride");
|
||||
}
|
||||
|
||||
bool osdApplied = false;
|
||||
int processedStride = 0;
|
||||
const gvfg_status_t osdStatus =
|
||||
osdProcessor.process(frame, processedVideoBuffer, processedStride, osdApplied);
|
||||
if (osdStatus != GVFG_OK)
|
||||
{
|
||||
session->release_frame();
|
||||
std::lock_guard<std::mutex> lock(frameMutex);
|
||||
readInProgress = false;
|
||||
return reject(osdStatus, "gvfg_read_channel_frame failed to apply OSD");
|
||||
}
|
||||
|
||||
bool backendFrameHeld = true;
|
||||
if (osdApplied)
|
||||
{
|
||||
const gvfg_status_t releaseStatus = session->release_frame();
|
||||
if (releaseStatus != GVFG_OK)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(frameMutex);
|
||||
readInProgress = false;
|
||||
return releaseStatus;
|
||||
}
|
||||
backendFrameHeld = false;
|
||||
frame.data = processedVideoBuffer.data();
|
||||
frame.data_size = static_cast<uint64_t>(processedVideoBuffer.size());
|
||||
frame.row_stride_bytes = processedStride;
|
||||
}
|
||||
|
||||
bool stoppedWhileWaiting = false;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(frameMutex);
|
||||
@@ -418,13 +460,15 @@ struct gvfg_channel_session_t
|
||||
{
|
||||
frame.frame_id = ++videoFrameId;
|
||||
heldSessionFrame = frame;
|
||||
heldFrameNeedsBackendRelease = backendFrameHeld;
|
||||
readInProgress = false;
|
||||
frameHeld = true;
|
||||
}
|
||||
}
|
||||
if (stoppedWhileWaiting)
|
||||
{
|
||||
session->release_frame();
|
||||
if (backendFrameHeld)
|
||||
session->release_frame();
|
||||
return GVFG_ESTATE;
|
||||
}
|
||||
|
||||
@@ -434,16 +478,7 @@ struct gvfg_channel_session_t
|
||||
out.data_size = static_cast<uint64_t>(frame.data_size);
|
||||
out.width = static_cast<int>(frame.width);
|
||||
out.height = static_cast<int>(frame.height);
|
||||
const gvfg_status_t strideStatus =
|
||||
native_row_stride_bytes(frame.width, frame.pixel_format, out.row_stride_bytes);
|
||||
if (strideStatus != GVFG_OK)
|
||||
{
|
||||
session->release_frame();
|
||||
std::lock_guard<std::mutex> lock(frameMutex);
|
||||
heldSessionFrame = {};
|
||||
frameHeld = false;
|
||||
return reject(strideStatus, "gvfg_read_channel_frame received an invalid row stride");
|
||||
}
|
||||
out.row_stride_bytes = frame.row_stride_bytes;
|
||||
out.pixel_format = frame.pixel_format;
|
||||
out.bit_depth = static_cast<int>(frame.bit_depth);
|
||||
out.frame_id = frame.frame_id;
|
||||
@@ -467,11 +502,15 @@ struct gvfg_channel_session_t
|
||||
frameToken.frame_id != heldSessionFrame.frame_id)
|
||||
return reject(GVFG_EINVAL, "gvfg_release_channel_frame rejected: frame token does not match the held frame");
|
||||
|
||||
const gvfg_status_t st = session->release_frame();
|
||||
if (st != GVFG_OK)
|
||||
return st;
|
||||
if (heldFrameNeedsBackendRelease)
|
||||
{
|
||||
const gvfg_status_t st = session->release_frame();
|
||||
if (st != GVFG_OK)
|
||||
return st;
|
||||
}
|
||||
|
||||
heldSessionFrame = {};
|
||||
heldFrameNeedsBackendRelease = false;
|
||||
frameHeld = false;
|
||||
}
|
||||
|
||||
@@ -483,20 +522,19 @@ struct gvfg_channel_session_t
|
||||
if (!session)
|
||||
return;
|
||||
|
||||
gvfg_frame_t frame{};
|
||||
bool shouldRelease = false;
|
||||
bool shouldReleaseBackend = false;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(frameMutex);
|
||||
if (frameHeld)
|
||||
{
|
||||
frame = heldSessionFrame;
|
||||
heldSessionFrame = {};
|
||||
frameHeld = false;
|
||||
shouldRelease = true;
|
||||
shouldReleaseBackend = heldFrameNeedsBackendRelease;
|
||||
heldFrameNeedsBackendRelease = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (shouldRelease)
|
||||
if (shouldReleaseBackend)
|
||||
session->release_frame();
|
||||
}
|
||||
|
||||
@@ -583,6 +621,24 @@ struct gvfg_channel_session_t
|
||||
return session->set_video_format(format);
|
||||
}
|
||||
|
||||
gvfg_status_t setOsd(const gvfg_channel_osd_config_t &config)
|
||||
{
|
||||
if (!session)
|
||||
return reject(GVFG_ESTATE, "gvfg_set_channel_osd rejected: channel is not open");
|
||||
const gvfg_status_t status = osdProcessor.configure(config);
|
||||
return status == GVFG_OK
|
||||
? GVFG_OK
|
||||
: reject(status, "gvfg_set_channel_osd rejected: configuration is invalid");
|
||||
}
|
||||
|
||||
gvfg_status_t clearOsd()
|
||||
{
|
||||
if (!session)
|
||||
return reject(GVFG_ESTATE, "gvfg_clear_channel_osd rejected: channel is not open");
|
||||
osdProcessor.clear();
|
||||
return GVFG_OK;
|
||||
}
|
||||
|
||||
gvfg_status_t setAudioEnabled(bool enabled)
|
||||
{
|
||||
if (!session)
|
||||
@@ -765,8 +821,11 @@ struct gvfg_channel_session_t
|
||||
std::mutex frameMutex;
|
||||
bool readInProgress = false;
|
||||
bool frameHeld = false;
|
||||
bool heldFrameNeedsBackendRelease = false;
|
||||
uint64_t videoFrameId = 0;
|
||||
gvfg_frame_t heldSessionFrame{};
|
||||
gvfg::internal::GpuOsdProcessor osdProcessor;
|
||||
std::vector<uint8_t> processedVideoBuffer;
|
||||
std::mutex audioMutex;
|
||||
std::vector<uint8_t> audioBuffer;
|
||||
bool audioReadInProgress = false;
|
||||
@@ -1049,6 +1108,31 @@ extern "C"
|
||||
return handle->setChannelVideoFormat(channel_index, format);
|
||||
}
|
||||
|
||||
gvfg_status_t gvfg_set_channel_osd(gvfg_handle handle,
|
||||
int channel_index,
|
||||
const gvfg_channel_osd_config_t *config)
|
||||
{
|
||||
if (!handle)
|
||||
return GVFG_EINVAL;
|
||||
if (!config)
|
||||
return handle->rejectChannel(channel_index, GVFG_EINVAL,
|
||||
"gvfg_set_channel_osd rejected: config is null");
|
||||
gvfg_channel_session_t *channel = handle->findChannel(channel_index);
|
||||
return channel ? channel->setOsd(*config)
|
||||
: handle->rejectChannel(channel_index, GVFG_ESTATE,
|
||||
"gvfg_set_channel_osd rejected: channel is not open");
|
||||
}
|
||||
|
||||
gvfg_status_t gvfg_clear_channel_osd(gvfg_handle handle, int channel_index)
|
||||
{
|
||||
if (!handle)
|
||||
return GVFG_EINVAL;
|
||||
gvfg_channel_session_t *channel = handle->findChannel(channel_index);
|
||||
return channel ? channel->clearOsd()
|
||||
: handle->rejectChannel(channel_index, GVFG_ESTATE,
|
||||
"gvfg_clear_channel_osd rejected: channel is not open");
|
||||
}
|
||||
|
||||
gvfg_status_t gvfg_set_channel_audio_enabled(gvfg_handle handle,
|
||||
int channel_index,
|
||||
int enabled)
|
||||
|
||||
Reference in New Issue
Block a user