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:
Binary file not shown.
@@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.16)
|
||||
|
||||
project(gvfg_sdk LANGUAGES CXX)
|
||||
|
||||
set(GVFG_SDK_VERSION "1.0.1")
|
||||
set(GVFG_SDK_VERSION "1.0.2")
|
||||
|
||||
include(GNUInstallDirs)
|
||||
|
||||
@@ -44,12 +44,13 @@ if (NOT WIN32)
|
||||
message(FATAL_ERROR "GVFG SDK currently supports Windows only.")
|
||||
endif()
|
||||
|
||||
add_subdirectory(helpers/gvfg_osd)
|
||||
add_subdirectory(sdk/gvfg)
|
||||
add_subdirectory(helpers/gvfg_preview)
|
||||
add_subdirectory(helpers/gvfg_audio_playback)
|
||||
|
||||
install(FILES
|
||||
../docs/GVFG_CUSTOMER_API.md
|
||||
../docs/GVFG_CUSTOMER_API_REFERENCE.md
|
||||
../docs/GVFG_Customer_doc/GVFG_CUSTOMER_API_REFERENCE.md
|
||||
DESTINATION ${CMAKE_INSTALL_DOCDIR}
|
||||
)
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
project(gvfg_osd_core LANGUAGES CXX)
|
||||
|
||||
add_library(gvfg_osd_core STATIC
|
||||
include/gvfg_osd.h
|
||||
src/gvfg_osd.cpp
|
||||
)
|
||||
|
||||
set_target_properties(gvfg_osd_core PROPERTIES
|
||||
AUTOMOC OFF
|
||||
AUTOUIC OFF
|
||||
AUTORCC OFF
|
||||
)
|
||||
|
||||
target_include_directories(gvfg_osd_core
|
||||
PUBLIC
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/include
|
||||
)
|
||||
|
||||
target_link_libraries(gvfg_osd_core PUBLIC
|
||||
d2d1
|
||||
dwrite
|
||||
windowscodecs
|
||||
ole32
|
||||
)
|
||||
|
||||
if (MSVC)
|
||||
target_compile_options(gvfg_osd_core PRIVATE /utf-8 /EHsc)
|
||||
endif()
|
||||
@@ -0,0 +1,27 @@
|
||||
# GVFG OSD core
|
||||
|
||||
`gvfg_osd_core` is an internal static library that generates a full-frame
|
||||
YUV422 OSD surface when text settings change. It does not capture, process
|
||||
live video frames, communicate with the driver, or present a preview. The
|
||||
library is linked into `gvfg.dll`; it is not installed or shipped as a
|
||||
separate customer DLL.
|
||||
|
||||
The initial API supports:
|
||||
|
||||
- YUY2 and Y210 output
|
||||
- printable ASCII text, up to 32 characters
|
||||
- Arial-preferred font with DirectWrite fallback, configurable pixel size,
|
||||
RGB color, and `(x, y)` position
|
||||
- BT.709 limited-range RGB-to-YUV conversion
|
||||
- an 8-bit coverage mask (`0` transparent, `255` fully covered)
|
||||
|
||||
The caller owns all output memory. Width must be even. YUY2 requires at least
|
||||
`width * 2` bytes per output row; Y210 requires `width * 4`. The mask requires
|
||||
at least `width` bytes per row.
|
||||
|
||||
For live SDK OSD, `GpuOsdProcessor` caches the non-zero mask bounds, aligned
|
||||
to complete YUV422 pixel pairs. Only this rectangle is uploaded, blended,
|
||||
and read back each frame. The SDK preserves the complete native frame with
|
||||
memory copies and replaces the processed rectangle; CPU code does not blend
|
||||
pixels. Text with no visible coverage bypasses processing. The public frame
|
||||
format and read/release contract remain unchanged.
|
||||
@@ -0,0 +1,79 @@
|
||||
#ifndef GVFG_OSD_H
|
||||
#define GVFG_OSD_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define GVFG_OSD_MAX_TEXT_CHARACTERS 32u
|
||||
|
||||
typedef void *gvfg_osd_handle;
|
||||
|
||||
typedef enum gvfg_osd_status_t
|
||||
{
|
||||
GVFG_OSD_OK = 0,
|
||||
GVFG_OSD_EINVAL = -1,
|
||||
GVFG_OSD_ENOMEM = -2,
|
||||
GVFG_OSD_ERENDER = -3
|
||||
} gvfg_osd_status_t;
|
||||
|
||||
typedef enum gvfg_osd_pixel_format_t
|
||||
{
|
||||
GVFG_OSD_PIXFMT_YUY2 = 1,
|
||||
GVFG_OSD_PIXFMT_Y210 = 2
|
||||
} gvfg_osd_pixel_format_t;
|
||||
|
||||
/*
|
||||
* Generates an off-line OSD surface using the Arial system font. Text is
|
||||
* restricted to printable ASCII and at most GVFG_OSD_MAX_TEXT_CHARACTERS
|
||||
* characters. color_rgb is 0xRRGGBB.
|
||||
* Output YUV uses BT.709 limited range. The mask is one coverage byte per
|
||||
* pixel: 0 is transparent and 255 is fully covered.
|
||||
*/
|
||||
typedef struct gvfg_osd_config_t
|
||||
{
|
||||
uint32_t struct_size;
|
||||
uint32_t width;
|
||||
uint32_t height;
|
||||
uint32_t pixel_format;
|
||||
const char *text;
|
||||
uint32_t text_length;
|
||||
int32_t x;
|
||||
int32_t y;
|
||||
uint32_t font_size_px;
|
||||
uint32_t color_rgb;
|
||||
} gvfg_osd_config_t;
|
||||
|
||||
/* All buffers are caller-owned and are used only for the duration of render. */
|
||||
typedef struct gvfg_osd_output_t
|
||||
{
|
||||
uint32_t struct_size;
|
||||
void *yuv_data;
|
||||
uint64_t yuv_size;
|
||||
uint32_t yuv_stride;
|
||||
void *mask_data;
|
||||
uint64_t mask_size;
|
||||
uint32_t mask_stride;
|
||||
} gvfg_osd_output_t;
|
||||
|
||||
gvfg_osd_status_t gvfg_osd_create(
|
||||
gvfg_osd_handle *out_handle);
|
||||
|
||||
void gvfg_osd_destroy(
|
||||
gvfg_osd_handle handle);
|
||||
|
||||
gvfg_osd_status_t gvfg_osd_render(
|
||||
gvfg_osd_handle handle,
|
||||
const gvfg_osd_config_t *config,
|
||||
const gvfg_osd_output_t *output);
|
||||
|
||||
const char *gvfg_osd_strerror(
|
||||
gvfg_osd_status_t status);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,310 @@
|
||||
#include "gvfg_osd.h"
|
||||
|
||||
#include <d2d1.h>
|
||||
#include <dwrite.h>
|
||||
#include <wincodec.h>
|
||||
#include <wrl/client.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <cstring>
|
||||
#include <limits>
|
||||
#include <new>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
using Microsoft::WRL::ComPtr;
|
||||
|
||||
namespace
|
||||
{
|
||||
struct OsdContext
|
||||
{
|
||||
ComPtr<IWICImagingFactory> wic;
|
||||
ComPtr<ID2D1Factory> d2d;
|
||||
ComPtr<IDWriteFactory> dwrite;
|
||||
bool ownsComInitialization = false;
|
||||
|
||||
~OsdContext()
|
||||
{
|
||||
dwrite.Reset();
|
||||
d2d.Reset();
|
||||
wic.Reset();
|
||||
if (ownsComInitialization)
|
||||
CoUninitialize();
|
||||
}
|
||||
};
|
||||
|
||||
uint8_t clampByte(double value)
|
||||
{
|
||||
return static_cast<uint8_t>(std::clamp(std::lround(value), 0L, 255L));
|
||||
}
|
||||
|
||||
struct Yuv8
|
||||
{
|
||||
uint8_t y;
|
||||
uint8_t u;
|
||||
uint8_t v;
|
||||
};
|
||||
|
||||
Yuv8 rgbToBt709Limited(uint32_t rgb)
|
||||
{
|
||||
const double r = static_cast<double>((rgb >> 16) & 0xffu);
|
||||
const double g = static_cast<double>((rgb >> 8) & 0xffu);
|
||||
const double b = static_cast<double>(rgb & 0xffu);
|
||||
return {
|
||||
clampByte(16.0 + 0.182586 * r + 0.614231 * g + 0.062007 * b),
|
||||
clampByte(128.0 - 0.100644 * r - 0.338572 * g + 0.439216 * b),
|
||||
clampByte(128.0 + 0.439216 * r - 0.398942 * g - 0.040274 * b)};
|
||||
}
|
||||
|
||||
bool checkedImageBytes(uint32_t stride, uint32_t height, uint64_t &bytes)
|
||||
{
|
||||
bytes = static_cast<uint64_t>(stride) * height;
|
||||
return height == 0 || bytes / height == stride;
|
||||
}
|
||||
|
||||
bool validAscii(const char *text, uint32_t length)
|
||||
{
|
||||
if (!text || length == 0 || length > GVFG_OSD_MAX_TEXT_CHARACTERS)
|
||||
return false;
|
||||
for (uint32_t i = 0; i < length; ++i)
|
||||
{
|
||||
const unsigned char c = static_cast<unsigned char>(text[i]);
|
||||
if (c < 0x20 || c > 0x7e)
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
std::wstring widenAscii(const char *text, uint32_t length)
|
||||
{
|
||||
std::wstring result;
|
||||
result.reserve(length);
|
||||
for (uint32_t i = 0; i < length; ++i)
|
||||
result.push_back(static_cast<wchar_t>(static_cast<unsigned char>(text[i])));
|
||||
return result;
|
||||
}
|
||||
|
||||
gvfg_osd_status_t renderMask(OsdContext &context,
|
||||
const gvfg_osd_config_t &config,
|
||||
uint8_t *destination,
|
||||
uint32_t destinationStride)
|
||||
{
|
||||
ComPtr<IWICBitmap> bitmap;
|
||||
HRESULT hr = context.wic->CreateBitmap(config.width, config.height,
|
||||
GUID_WICPixelFormat32bppPBGRA,
|
||||
WICBitmapCacheOnLoad, &bitmap);
|
||||
if (FAILED(hr))
|
||||
return GVFG_OSD_ERENDER;
|
||||
|
||||
D2D1_RENDER_TARGET_PROPERTIES properties = D2D1::RenderTargetProperties(
|
||||
D2D1_RENDER_TARGET_TYPE_SOFTWARE,
|
||||
D2D1::PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM,
|
||||
D2D1_ALPHA_MODE_PREMULTIPLIED));
|
||||
ComPtr<ID2D1RenderTarget> target;
|
||||
hr = context.d2d->CreateWicBitmapRenderTarget(bitmap.Get(), properties, &target);
|
||||
if (FAILED(hr))
|
||||
return GVFG_OSD_ERENDER;
|
||||
|
||||
const std::wstring text = widenAscii(config.text, config.text_length);
|
||||
|
||||
ComPtr<IDWriteTextFormat> format;
|
||||
hr = context.dwrite->CreateTextFormat(
|
||||
L"Arial", nullptr, DWRITE_FONT_WEIGHT_NORMAL,
|
||||
DWRITE_FONT_STYLE_NORMAL, DWRITE_FONT_STRETCH_NORMAL,
|
||||
static_cast<float>(config.font_size_px), L"en-us", &format);
|
||||
if (FAILED(hr))
|
||||
return GVFG_OSD_ERENDER;
|
||||
|
||||
ComPtr<ID2D1SolidColorBrush> brush;
|
||||
hr = target->CreateSolidColorBrush(D2D1::ColorF(1.0f, 1.0f, 1.0f, 1.0f), &brush);
|
||||
if (FAILED(hr))
|
||||
return GVFG_OSD_ERENDER;
|
||||
|
||||
target->BeginDraw();
|
||||
target->Clear(D2D1::ColorF(0.0f, 0.0f));
|
||||
const D2D1_RECT_F layout = D2D1::RectF(
|
||||
static_cast<float>(config.x), static_cast<float>(config.y),
|
||||
static_cast<float>(config.width), static_cast<float>(config.height));
|
||||
target->DrawText(text.c_str(), config.text_length, format.Get(), layout,
|
||||
brush.Get(), D2D1_DRAW_TEXT_OPTIONS_CLIP,
|
||||
DWRITE_MEASURING_MODE_NATURAL);
|
||||
hr = target->EndDraw();
|
||||
if (FAILED(hr))
|
||||
return GVFG_OSD_ERENDER;
|
||||
|
||||
WICRect lockRect{0, 0, static_cast<INT>(config.width), static_cast<INT>(config.height)};
|
||||
ComPtr<IWICBitmapLock> lock;
|
||||
hr = bitmap->Lock(&lockRect, WICBitmapLockRead, &lock);
|
||||
if (FAILED(hr))
|
||||
return GVFG_OSD_ERENDER;
|
||||
|
||||
UINT sourceStride = 0;
|
||||
UINT sourceSize = 0;
|
||||
BYTE *source = nullptr;
|
||||
if (FAILED(lock->GetStride(&sourceStride)) ||
|
||||
FAILED(lock->GetDataPointer(&sourceSize, &source)) || !source)
|
||||
return GVFG_OSD_ERENDER;
|
||||
|
||||
for (uint32_t y = 0; y < config.height; ++y)
|
||||
{
|
||||
const uint8_t *sourceRow = source + static_cast<size_t>(y) * sourceStride;
|
||||
uint8_t *destinationRow = destination + static_cast<size_t>(y) * destinationStride;
|
||||
for (uint32_t x = 0; x < config.width; ++x)
|
||||
destinationRow[x] = sourceRow[x * 4u + 3u];
|
||||
}
|
||||
return GVFG_OSD_OK;
|
||||
}
|
||||
|
||||
void writeYuy2(const gvfg_osd_config_t &config,
|
||||
const gvfg_osd_output_t &output,
|
||||
const Yuv8 color)
|
||||
{
|
||||
auto *base = static_cast<uint8_t *>(output.yuv_data);
|
||||
const auto *mask = static_cast<const uint8_t *>(output.mask_data);
|
||||
constexpr Yuv8 black{16, 128, 128};
|
||||
for (uint32_t y = 0; y < config.height; ++y)
|
||||
{
|
||||
uint8_t *row = base + static_cast<size_t>(y) * output.yuv_stride;
|
||||
const uint8_t *maskRow = mask + static_cast<size_t>(y) * output.mask_stride;
|
||||
for (uint32_t x = 0; x < config.width; x += 2)
|
||||
{
|
||||
const bool first = maskRow[x] != 0;
|
||||
const bool second = maskRow[x + 1] != 0;
|
||||
row[x * 2u + 0u] = first ? color.y : black.y;
|
||||
row[x * 2u + 1u] = (first || second) ? color.u : black.u;
|
||||
row[x * 2u + 2u] = second ? color.y : black.y;
|
||||
row[x * 2u + 3u] = (first || second) ? color.v : black.v;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void writeY210(const gvfg_osd_config_t &config,
|
||||
const gvfg_osd_output_t &output,
|
||||
const Yuv8 color)
|
||||
{
|
||||
auto *base = static_cast<uint8_t *>(output.yuv_data);
|
||||
const auto *mask = static_cast<const uint8_t *>(output.mask_data);
|
||||
const uint16_t blackY = static_cast<uint16_t>(16u * 4u) << 6u;
|
||||
const uint16_t blackC = static_cast<uint16_t>(128u * 4u) << 6u;
|
||||
const uint16_t colorY = static_cast<uint16_t>(color.y * 4u) << 6u;
|
||||
const uint16_t colorU = static_cast<uint16_t>(color.u * 4u) << 6u;
|
||||
const uint16_t colorV = static_cast<uint16_t>(color.v * 4u) << 6u;
|
||||
for (uint32_t y = 0; y < config.height; ++y)
|
||||
{
|
||||
auto *row = reinterpret_cast<uint16_t *>(base + static_cast<size_t>(y) * output.yuv_stride);
|
||||
const uint8_t *maskRow = mask + static_cast<size_t>(y) * output.mask_stride;
|
||||
for (uint32_t x = 0; x < config.width; x += 2)
|
||||
{
|
||||
const bool first = maskRow[x] != 0;
|
||||
const bool second = maskRow[x + 1] != 0;
|
||||
row[x * 2u + 0u] = first ? colorY : blackY;
|
||||
row[x * 2u + 1u] = (first || second) ? colorU : blackC;
|
||||
row[x * 2u + 2u] = second ? colorY : blackY;
|
||||
row[x * 2u + 3u] = (first || second) ? colorV : blackC;
|
||||
}
|
||||
}
|
||||
}
|
||||
} // namespace
|
||||
|
||||
extern "C"
|
||||
{
|
||||
gvfg_osd_status_t gvfg_osd_create(gvfg_osd_handle *out_handle)
|
||||
{
|
||||
if (!out_handle)
|
||||
return GVFG_OSD_EINVAL;
|
||||
*out_handle = nullptr;
|
||||
|
||||
auto *context = new (std::nothrow) OsdContext();
|
||||
if (!context)
|
||||
return GVFG_OSD_ENOMEM;
|
||||
|
||||
HRESULT hr = CoInitializeEx(nullptr, COINIT_MULTITHREADED);
|
||||
if (FAILED(hr) && hr != RPC_E_CHANGED_MODE)
|
||||
{
|
||||
delete context;
|
||||
return GVFG_OSD_ERENDER;
|
||||
}
|
||||
context->ownsComInitialization = SUCCEEDED(hr);
|
||||
hr = CoCreateInstance(CLSID_WICImagingFactory, nullptr, CLSCTX_INPROC_SERVER,
|
||||
IID_PPV_ARGS(&context->wic));
|
||||
if (SUCCEEDED(hr))
|
||||
hr = D2D1CreateFactory(D2D1_FACTORY_TYPE_MULTI_THREADED,
|
||||
context->d2d.ReleaseAndGetAddressOf());
|
||||
if (SUCCEEDED(hr))
|
||||
hr = DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED,
|
||||
__uuidof(IDWriteFactory),
|
||||
reinterpret_cast<IUnknown **>(
|
||||
context->dwrite.ReleaseAndGetAddressOf()));
|
||||
if (FAILED(hr))
|
||||
{
|
||||
delete context;
|
||||
return GVFG_OSD_ERENDER;
|
||||
}
|
||||
|
||||
*out_handle = context;
|
||||
return GVFG_OSD_OK;
|
||||
}
|
||||
|
||||
void gvfg_osd_destroy(gvfg_osd_handle handle)
|
||||
{
|
||||
delete static_cast<OsdContext *>(handle);
|
||||
}
|
||||
|
||||
gvfg_osd_status_t gvfg_osd_render(gvfg_osd_handle handle,
|
||||
const gvfg_osd_config_t *config,
|
||||
const gvfg_osd_output_t *output)
|
||||
{
|
||||
if (!handle || !config || !output ||
|
||||
config->struct_size != sizeof(gvfg_osd_config_t) ||
|
||||
output->struct_size != sizeof(gvfg_osd_output_t) ||
|
||||
!validAscii(config->text, config->text_length) ||
|
||||
config->width == 0 || config->height == 0 || (config->width & 1u) != 0 ||
|
||||
config->font_size_px == 0 || !output->yuv_data || !output->mask_data)
|
||||
return GVFG_OSD_EINVAL;
|
||||
|
||||
uint32_t minimumYuvStride = 0;
|
||||
if (config->pixel_format == GVFG_OSD_PIXFMT_YUY2)
|
||||
minimumYuvStride = config->width * 2u;
|
||||
else if (config->pixel_format == GVFG_OSD_PIXFMT_Y210)
|
||||
minimumYuvStride = config->width * 4u;
|
||||
else
|
||||
return GVFG_OSD_EINVAL;
|
||||
|
||||
if (output->yuv_stride < minimumYuvStride || output->mask_stride < config->width)
|
||||
return GVFG_OSD_EINVAL;
|
||||
|
||||
uint64_t requiredYuv = 0;
|
||||
uint64_t requiredMask = 0;
|
||||
if (!checkedImageBytes(output->yuv_stride, config->height, requiredYuv) ||
|
||||
!checkedImageBytes(output->mask_stride, config->height, requiredMask) ||
|
||||
output->yuv_size < requiredYuv || output->mask_size < requiredMask)
|
||||
return GVFG_OSD_EINVAL;
|
||||
|
||||
std::memset(output->mask_data, 0, static_cast<size_t>(requiredMask));
|
||||
const gvfg_osd_status_t maskStatus = renderMask(
|
||||
*static_cast<OsdContext *>(handle), *config,
|
||||
static_cast<uint8_t *>(output->mask_data), output->mask_stride);
|
||||
if (maskStatus != GVFG_OSD_OK)
|
||||
return maskStatus;
|
||||
|
||||
const Yuv8 color = rgbToBt709Limited(config->color_rgb);
|
||||
if (config->pixel_format == GVFG_OSD_PIXFMT_YUY2)
|
||||
writeYuy2(*config, *output, color);
|
||||
else
|
||||
writeY210(*config, *output, color);
|
||||
return GVFG_OSD_OK;
|
||||
}
|
||||
|
||||
const char *gvfg_osd_strerror(gvfg_osd_status_t status)
|
||||
{
|
||||
switch (status)
|
||||
{
|
||||
case GVFG_OSD_OK: return "success";
|
||||
case GVFG_OSD_EINVAL: return "invalid argument";
|
||||
case GVFG_OSD_ENOMEM: return "out of memory";
|
||||
case GVFG_OSD_ERENDER: return "text rendering failed";
|
||||
default: return "unknown gvfg_osd status";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,30 +1,34 @@
|
||||
# Preview GPU path
|
||||
# Preview GPU conversion paths
|
||||
|
||||
The preview worker now runs:
|
||||
`gvfg_preview.dll` remains independent of `gvfg.dll`. Both targets compile
|
||||
one shared private conversion source, `d3d_conversion_pipeline.cpp`, into
|
||||
their own DLLs. There is no cross-DLL bridge, new public texture API, or
|
||||
runtime SDK dependency in the preview helper.
|
||||
|
||||
1. Execute the upload slot's command list (the SDK frame was already released by the caller).
|
||||
2. Convert packed YUY2/Y210 into a full-resolution FP16 target.
|
||||
3. Sample that FP16 target directly into the 8-bit or 10-bit swapchain, preserving aspect ratio.
|
||||
4. Present(0, 0). DXGI handles presentation waits; no application retry loop.
|
||||
CPU-buffer conversion uses dedicated outputs. YUY2/Y210 to NV12 preserves
|
||||
native luma and averages chroma vertically; Y210 codes are reduced to 8 bits.
|
||||
BGRA8 and RGB10A2 use one native RGB draw. No export path requires FP16 or a
|
||||
scene copy. Output targets and staging textures are allocated on demand and
|
||||
reused. Both NV12 plane copies are submitted before either CPU Map.
|
||||
|
||||
Compared with the previous preview, this removes one full-frame FP16 CopyResource per image. The 8-bit path also removes an intermediate BGRA8 draw and quantizes only at the swapchain. FP16 precision and the requested swapchain bit depth remain; no capture-frame decimation was added. Scaled 8-bit output may differ slightly because rounding now occurs after scaling instead of before it.
|
||||
Preview uploads remain in the existing four slots, with three FIFO pending
|
||||
frames and a 50 ms queue-age limit. Native-size and reduced-size preview draw
|
||||
directly into the swapchain. Reduction interpolates decoded RGB. Enlargement
|
||||
lazily allocates one FP16 target and uses hardware bilinear sampling, avoiding
|
||||
repeated native decoding for each enlarged output sample. All five existing
|
||||
formats retain their color/bit-depth rules. Preview has no CPU readback.
|
||||
|
||||
Each of the four upload slots owns its SRV; the worker no longer creates an SRV for every preview frame. YUY2/Y210 shaders use one packed texture load per output pixel. The old ProcAmp settings were fixed at neutral, so unused neighbor sharpness reads and neutral color adjustments were removed; BT.709 coefficients, packing, Y210 word shifts and odd-tail chroma behavior remain. Constant-buffer data updates only when source dimensions change, but its binding is restored each draw.
|
||||
## Verification (2026-10-12)
|
||||
|
||||
Preview-only resource initialization allocates only the FP16 conversion target, omitting scene, BGRA8 export, RGB10 export and NV12 planes. Export conversion still uses the default full resource path; no public capture or buffer API signature changed. Export shaders are not compiled for preview-only initialization.
|
||||
Rebuilt Release DLLs passed public YUY2/Y210 to BGRA8/RGB10A2/NV12 checks
|
||||
against independent CPU reference values, padded strides, chroma averaging,
|
||||
size/format switching, and odd-NV12 rejection. WARP and hardware tests of the
|
||||
private conversion source passed all five formats, 8/10-bit output, native,
|
||||
reduced and enlarged sizes, and cached/uncached source views. Actual independent
|
||||
preview DLL hidden-HWND tests passed format and precision switches, queue
|
||||
drain, clear and destruction. DLL imports and exports are checked separately.
|
||||
|
||||
Swapchain resize/release unbinds the render target before releasing local references. Preview configuration/resource changes and drawing share the D3D mutex. No GPU mutex is held while subsequently acquiring the renderer mutex in the worker completion path.
|
||||
|
||||
## Validation boundary
|
||||
|
||||
Source checks only in this change: no application/shader build, GPU timing or hardware playback test. These changes remove identifiable work but do not establish a GPU utilization reduction or identify the reported 10-bit startup failure.
|
||||
|
||||
User validation reported after the source change: GPU utilization decreased by approximately 15%. This is a user-reported observation, not an agent-run benchmark; baseline/final readings and whether the difference is relative or in percentage points were not specified. Resolution of the 10-bit startup failure was not reported.
|
||||
|
||||
Build the preview DLL with the changed shared pipeline source. If distributing the SDK GPU buffer converter, rebuild that DLL too because it shares the shader source. Keep the APP and preview DLL from the same delivery-statistics API revision.
|
||||
|
||||
On the target Ryzen 7 7700 system, compare identical resolution/FPS/window size and input formats before/after. Check 8-bit and 10-bit colors (including gradients), resize/fullscreen, repeated Start/Stop, Present skip counts and GPU engine utilization. Also verify public BGRA8/RGB10A2/NV12 buffer conversion if shipping its rebuilt DLL. Capture HRESULT details if 10-bit still fails; do not infer insufficient GPU capacity solely from utilization.
|
||||
|
||||
Expected preview pacing skips and audio queue evictions are not reported by the Sample. Preview/render failures, frame-ID anomalies, and audio write failures are logged as errors, aggregated at most once per five seconds. Mutex/drain/join waits service synchronous Windows sent messages without dispatching queued application input. Blocking Present still has no application timeout; device hangs and window transitions require hardware validation.
|
||||
|
||||
Preview queue policy: three pending/upload-reserved slots plus one presenting slot. Consume in enqueue order, keep frames within a 50 ms queue-age limit, and skip incoming frames only when capacity is full. Expired queued frames are counted in the existing replaced field (APP label: expired). This is not a 50 ms bound on blocking Present or physical scanout.
|
||||
These component tests do not establish long-run capture/recording stability,
|
||||
visible fullscreen behavior, physical monitor scanout, or Task Manager GPU
|
||||
percentages. Previous performance measurements motivated the adaptive scaling
|
||||
choice; this verification does not claim a new utilization benchmark.
|
||||
|
||||
@@ -643,10 +643,9 @@ private:
|
||||
renderFmt = gvfg::internal::GVFG_RENDER_FMT_RGBA16;
|
||||
else if (work.pixelFormat == GVFG_PREVIEW_PIXFMT_DICOM_YBR_FULL_422)
|
||||
renderFmt = gvfg::internal::GVFG_RENDER_FMT_DICOM_YBR_FULL_422;
|
||||
rendered = pipeline_->render_texture_to_fp16(work.texture.Get(), renderFmt,
|
||||
work.width, work.height, work.view.Get());
|
||||
if (rendered)
|
||||
presentResult = pipeline_->present_preview(work.width, work.height);
|
||||
presentResult = pipeline_->present_texture(work.texture.Get(), work.view.Get(), renderFmt,
|
||||
work.width, work.height, work.bitDepth);
|
||||
rendered = presentResult != gvfg::internal::GVFG_PREVIEW_PRESENT_FAILED;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -708,7 +707,7 @@ private:
|
||||
desc.swapchain_10bit = gvfg::internal::GVFG_RENDER_PREVIEW_BITDEPTH_AUTO;
|
||||
pipeline_->configurePreview(desc);
|
||||
pipeline_->set_source_bit_depth(sourceBitDepth > 0 ? sourceBitDepth : 8);
|
||||
const bool ready = pipeline_->ensure_rt_and_pipeline(width, height, true) &&
|
||||
const bool ready = pipeline_->ensure_states() &&
|
||||
pipeline_->ensure_preview_swapchain(width, height);
|
||||
return ready;
|
||||
}
|
||||
|
||||
@@ -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())
|
||||
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,12 +460,14 @@ struct gvfg_channel_session_t
|
||||
{
|
||||
frame.frame_id = ++videoFrameId;
|
||||
heldSessionFrame = frame;
|
||||
heldFrameNeedsBackendRelease = backendFrameHeld;
|
||||
readInProgress = false;
|
||||
frameHeld = true;
|
||||
}
|
||||
}
|
||||
if (stoppedWhileWaiting)
|
||||
{
|
||||
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");
|
||||
|
||||
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)
|
||||
|
||||
@@ -599,7 +599,7 @@ gvfg_status_t gvfg_get_channel_runtime_info(
|
||||
const char *gvfg_get_version(void);
|
||||
```
|
||||
|
||||
- 回傳目前實際載入的 GVFG runtime DLL 版本,例如 `"1.0.1"`。
|
||||
- 回傳目前實際載入的 GVFG runtime DLL 版本,例如 `"1.0.2"`。
|
||||
- 回傳值是靜態 null-terminated 字串,caller 不可 free。
|
||||
- 可用於 log、問題回報,以及確認 header、DLL 是否來自同一版本。
|
||||
|
||||
|
||||
Reference in New Issue
Block a user