Split GVFG customer API and preview helper

Refactor GVFG into a pull-based customer API with internal debug diagnostics in gvfg_debug.h. Move preview rendering into the separate gvfg_preview helper DLL and update the Qt sample to link it explicitly. Clean legacy callback/preview docs and removed SDK-internal render/log helper sources from the core GVFG DLL.
This commit is contained in:
Mark.lin
2026-06-15 17:06:12 +08:00
parent 97971e6242
commit d2657a69cb
29 changed files with 1066 additions and 1029 deletions
+37
View File
@@ -0,0 +1,37 @@
project(gvfg_preview LANGUAGES CXX)
add_library(gvfg_preview SHARED
include/gvfg_preview.h
src/d3d_preview_pipeline.cpp
src/d3d_preview_pipeline.h
src/gvfg_preview.cpp
)
set_target_properties(gvfg_preview PROPERTIES
AUTOMOC OFF
AUTOUIC OFF
AUTORCC OFF
)
target_include_directories(gvfg_preview
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/include
${CMAKE_SOURCE_DIR}/sdk/gvfg/include
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/src
)
target_compile_definitions(gvfg_preview PRIVATE
GVFG_PREVIEW_BUILD
)
target_link_libraries(gvfg_preview PRIVATE
user32
dxgi
d3d11
D3DCompiler
)
if (MSVC)
target_compile_options(gvfg_preview PRIVATE /utf-8 /EHsc)
endif()
@@ -0,0 +1,78 @@
#pragma once
/*
* Optional GVFG preview helper API.
*
* This helper renders frames returned by gvfg_read_frame(). It is intentionally
* separate from gvfg.dll: applications own the read loop and decide whether to
* render through this DLL, render themselves, record, snapshot, or process data.
*/
#include <gvfg_capture.h>
#include <stdint.h>
#ifdef _WIN32
#ifdef GVFG_PREVIEW_BUILD
#define GVFG_PREVIEW_API __declspec(dllexport)
#else
#define GVFG_PREVIEW_API __declspec(dllimport)
#endif
#else
#define GVFG_PREVIEW_API
#endif
#ifdef __cplusplus
extern "C" {
#endif
typedef enum
{
GVFG_PREVIEW_OK = 0,
GVFG_PREVIEW_EINVAL = -1,
GVFG_PREVIEW_ESTATE = -2,
GVFG_PREVIEW_ENOTSUP = -3,
GVFG_PREVIEW_ERENDER = -4
} gvfg_preview_status_t;
typedef struct gvfg_preview_handle_t *gvfg_preview_handle;
typedef struct
{
int active;
int width;
int height;
int bit_depth;
char pixel_format[32];
} gvfg_preview_info_t;
GVFG_PREVIEW_API gvfg_preview_status_t gvfg_preview_create(gvfg_preview_handle *out_handle);
GVFG_PREVIEW_API gvfg_preview_status_t gvfg_preview_destroy(gvfg_preview_handle handle);
/*
* Attach a native window handle for preview output.
*
* On Windows, native_window_handle is an HWND.
*/
GVFG_PREVIEW_API gvfg_preview_status_t gvfg_preview_attach_window(gvfg_preview_handle handle,
void *native_window_handle);
/*
* Synchronously render one frame.
*
* The frame remains owned by gvfg.dll. The caller should call this between
* gvfg_read_frame() and gvfg_release_frame().
*/
GVFG_PREVIEW_API gvfg_preview_status_t gvfg_preview_render_frame(gvfg_preview_handle handle,
const gvfg_frame_t *frame);
GVFG_PREVIEW_API gvfg_preview_status_t gvfg_preview_get_info(gvfg_preview_handle handle,
gvfg_preview_info_t *out_info);
GVFG_PREVIEW_API gvfg_preview_status_t gvfg_preview_shutdown(gvfg_preview_handle handle);
GVFG_PREVIEW_API const char *gvfg_preview_strerror(gvfg_preview_status_t status);
#ifdef __cplusplus
}
#endif
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,133 @@
#pragma once
#include <d3d11_1.h>
#include <dxgi1_2.h>
#include <stdint.h>
#include <wrl.h>
namespace gvfg::internal
{
typedef enum
{
GVFG_RENDER_FMT_NV12,
GVFG_RENDER_FMT_YUY2,
GVFG_RENDER_FMT_ARGB,
GVFG_RENDER_FMT_P010,
GVFG_RENDER_FMT_Y210
} gvfg_render_pixfmt_t;
typedef struct
{
const void *data[3];
int stride[3];
int plane_count;
int width;
int height;
gvfg_render_pixfmt_t format;
uint64_t pts_ns;
uint64_t frame_id;
} gvfg_render_frame_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 struct
{
void *hwnd;
int enable_preview;
int use_fp16_pipeline;
int swapchain_10bit;
} gvfg_render_preview_desc_t;
class D3DPreviewPipeline
{
public:
D3DPreviewPipeline() = default;
~D3DPreviewPipeline() = default;
bool initialize(ID3D11Device *d3d,
ID3D11DeviceContext *ctx);
bool configurePreview(const gvfg_render_preview_desc_t &desc);
void set_source_bit_depth(int bits);
void release_preview_swapchain();
bool create_shaders_and_states();
bool ensure_rt_and_pipeline(int w, int h);
bool ensure_preview_swapchain(int w, int h);
bool preview_swapchain_10bit() const { return preview_swapchain_10bit_; }
bool present_preview(int src_w, int src_h);
DXGI_FORMAT preview_backbuffer_format() const;
DXGI_FORMAT scene_texture_format() const;
DXGI_FORMAT linear_fp16_texture_format() const;
bool blit_fp16_to_rgba8(int frame_w, int frame_h);
bool upload_nv12_frame(const uint8_t *y, int stride_y, const uint8_t *uv, int stride_uv, int frame_w, int frame_h);
bool upload_p010_frame(const uint8_t *y, int stride_y, const uint8_t *uv, int stride_uv, int frame_w, int frame_h);
bool upload_yuy2_frame(const uint8_t *data, int src_stride, int frame_w, int frame_h);
bool upload_y210_frame(const uint8_t *data, int src_stride, int frame_w, int frame_h);
bool render_uploaded_yuv_to_fp16(gvfg_render_pixfmt_t fmt, int frame_w, int frame_h);
bool copy_fp16_to_scene();
bool readback_to_frame(int frame_w, int frame_h, uint64_t pts_ns, uint64_t frame_id,
gvfg_render_frame_t *out);
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> upload_nv12_;
Microsoft::WRL::ComPtr<ID3D11Texture2D> upload_yuy2_packed_;
Microsoft::WRL::ComPtr<ID3D11Texture2D> upload_y210_packed_;
Microsoft::WRL::ComPtr<ID3D11RenderTargetView> rtv_rgba_;
Microsoft::WRL::ComPtr<ID3D11Texture2D> rt_stage_;
Microsoft::WRL::ComPtr<ID3D11Buffer> cs_params_;
Microsoft::WRL::ComPtr<ID3D11UnorderedAccessView> rt_uav_;
Microsoft::WRL::ComPtr<ID3D11VertexShader> vs_;
Microsoft::WRL::ComPtr<ID3D11PixelShader> ps_nv12_;
Microsoft::WRL::ComPtr<ID3D11PixelShader> ps_p010_;
Microsoft::WRL::ComPtr<ID3D11PixelShader> ps_yuy2_;
Microsoft::WRL::ComPtr<ID3D11PixelShader> ps_y210_;
Microsoft::WRL::ComPtr<ID3D11PixelShader> ps_fp16_to_rgba8_;
Microsoft::WRL::ComPtr<ID3D11PixelShader> ps_fp16_to_preview_;
Microsoft::WRL::ComPtr<ID3D11InputLayout> il_;
Microsoft::WRL::ComPtr<ID3D11Buffer> vb_;
Microsoft::WRL::ComPtr<ID3D11SamplerState> samp_;
void *preview_hwnd_ = nullptr;
bool preview_enabled_ = false;
bool preview_use_fp16_ = false;
int preview_swapchain_mode_ = GVFG_RENDER_PREVIEW_BITDEPTH_10BIT;
int preview_source_bit_depth_ = 0;
bool preview_swapchain_10bit_ = false;
DXGI_FORMAT preview_swapchain_format_ = DXGI_FORMAT_UNKNOWN;
int preview_w_ = 0;
int preview_h_ = 0;
int rt_w_ = 0;
int rt_h_ = 0;
Microsoft::WRL::ComPtr<IDXGISwapChain1> preview_swapchain_;
Microsoft::WRL::ComPtr<ID3D11Texture2D> preview_backbuf_;
Microsoft::WRL::ComPtr<ID3D11RenderTargetView> preview_rtv_;
};
}
+317
View File
@@ -0,0 +1,317 @@
#include "gvfg_preview.h"
#include "d3d_preview_pipeline.h"
#include <d3d11_4.h>
#include <cstring>
#include <memory>
#include <mutex>
#include <wrl/client.h>
using Microsoft::WRL::ComPtr;
class PreviewRenderer
{
public:
PreviewRenderer() = default;
~PreviewRenderer() { shutdown(); }
bool configure(void *hwnd)
{
std::lock_guard<std::mutex> lock(mutex_);
hwnd_ = hwnd;
configured_ = hwnd_ != nullptr;
if (pipeline_)
{
gvfg::internal::gvfg_render_preview_desc_t desc{};
desc.hwnd = hwnd_;
desc.enable_preview = configured_ ? 1 : 0;
desc.use_fp16_pipeline = 1;
desc.swapchain_10bit = gvfg::internal::GVFG_RENDER_PREVIEW_BITDEPTH_AUTO;
pipeline_->configurePreview(desc);
}
return configured_;
}
bool render(const gvfg_frame_t &frame)
{
std::lock_guard<std::mutex> lock(mutex_);
if (!configured_ || !hwnd_ || !frame.data || frame.width <= 0 || frame.height <= 0)
return false;
if (!ensureDevice() || !ensurePipeline(frame.width, frame.height, frame.bit_depth > 0 ? frame.bit_depth : 8))
return false;
const auto *base = static_cast<const uint8_t *>(frame.data);
gvfg::internal::gvfg_render_pixfmt_t renderFmt = gvfg::internal::GVFG_RENDER_FMT_YUY2;
bool uploaded = false;
switch (frame.pixel_format)
{
case GVFG_PIXFMT_YUY2:
renderFmt = gvfg::internal::GVFG_RENDER_FMT_YUY2;
uploaded = pipeline_->upload_yuy2_frame(base, frame.width * 2, frame.width, frame.height);
break;
case GVFG_PIXFMT_Y210:
renderFmt = gvfg::internal::GVFG_RENDER_FMT_Y210;
uploaded = pipeline_->upload_y210_frame(base, frame.width * 4, frame.width, frame.height);
break;
default:
return false;
}
if (!uploaded ||
!pipeline_->render_uploaded_yuv_to_fp16(renderFmt, frame.width, frame.height) ||
!pipeline_->copy_fp16_to_scene())
return false;
bool ok = true;
if (!pipeline_->preview_swapchain_10bit())
ok = pipeline_->blit_fp16_to_rgba8(frame.width, frame.height);
if (!ok)
return false;
pipeline_->present_preview(frame.width, frame.height);
width_ = pipeline_->preview_w_;
height_ = pipeline_->preview_h_;
bitDepth_ = pipeline_->preview_swapchain_10bit() ? 10 : 8;
swapchain10Bit_ = pipeline_->preview_swapchain_10bit();
return true;
}
void shutdown()
{
std::lock_guard<std::mutex> lock(mutex_);
if (pipeline_)
pipeline_->release_preview_swapchain();
pipeline_.reset();
d3d_.reset();
configured_ = false;
hwnd_ = nullptr;
width_ = 0;
height_ = 0;
bitDepth_ = 0;
swapchain10Bit_ = false;
}
bool active() const
{
std::lock_guard<std::mutex> lock(mutex_);
return configured_ && pipeline_ && width_ > 0 && height_ > 0;
}
int width() const
{
std::lock_guard<std::mutex> lock(mutex_);
return width_;
}
int height() const
{
std::lock_guard<std::mutex> lock(mutex_);
return height_;
}
int bitDepth() const
{
std::lock_guard<std::mutex> lock(mutex_);
return bitDepth_;
}
const char *pixelFormat() const
{
std::lock_guard<std::mutex> lock(mutex_);
return swapchain10Bit_ ? "RGB10A2" : "BGRA8";
}
private:
struct D3DState
{
ComPtr<ID3D11Device> device;
ComPtr<ID3D11DeviceContext> context;
};
bool ensureDevice()
{
if (d3d_ && d3d_->device && d3d_->context)
return true;
auto state = std::make_unique<D3DState>();
UINT flags = D3D11_CREATE_DEVICE_BGRA_SUPPORT;
#ifdef _DEBUG
flags |= D3D11_CREATE_DEVICE_DEBUG;
#endif
D3D_FEATURE_LEVEL levels[] = {D3D_FEATURE_LEVEL_11_1, D3D_FEATURE_LEVEL_11_0};
D3D_FEATURE_LEVEL got{};
HRESULT hr = D3D11CreateDevice(nullptr,
D3D_DRIVER_TYPE_HARDWARE,
nullptr,
flags,
levels,
_countof(levels),
D3D11_SDK_VERSION,
&state->device,
&got,
&state->context);
#ifdef _DEBUG
if (FAILED(hr))
{
flags &= ~D3D11_CREATE_DEVICE_DEBUG;
hr = D3D11CreateDevice(nullptr,
D3D_DRIVER_TYPE_HARDWARE,
nullptr,
flags,
levels,
_countof(levels),
D3D11_SDK_VERSION,
&state->device,
&got,
&state->context);
}
#endif
if (FAILED(hr) || !state->device || !state->context)
return false;
ComPtr<ID3D11Multithread> mt;
if (SUCCEEDED(state->device.As(&mt)) && mt)
mt->SetMultithreadProtected(TRUE);
d3d_ = std::move(state);
return true;
}
bool ensurePipeline(int width, int height, int sourceBitDepth)
{
if (!pipeline_)
pipeline_ = std::make_unique<gvfg::internal::D3DPreviewPipeline>();
if (!pipeline_->initialize(d3d_->device.Get(), d3d_->context.Get()))
return false;
gvfg::internal::gvfg_render_preview_desc_t desc{};
desc.hwnd = hwnd_;
desc.enable_preview = configured_ ? 1 : 0;
desc.use_fp16_pipeline = 1;
desc.swapchain_10bit = gvfg::internal::GVFG_RENDER_PREVIEW_BITDEPTH_AUTO;
pipeline_->configurePreview(desc);
pipeline_->set_source_bit_depth(sourceBitDepth > 0 ? sourceBitDepth : 8);
return pipeline_->ensure_rt_and_pipeline(width, height) &&
pipeline_->ensure_preview_swapchain(width, height);
}
mutable std::mutex mutex_;
void *hwnd_ = nullptr;
bool configured_ = false;
int width_ = 0;
int height_ = 0;
int bitDepth_ = 0;
bool swapchain10Bit_ = false;
std::unique_ptr<D3DState> d3d_;
std::unique_ptr<gvfg::internal::D3DPreviewPipeline> pipeline_;
};
struct gvfg_preview_handle_t
{
PreviewRenderer renderer;
};
namespace
{
void copy_cstr(char *dst, size_t dstSize, const char *src)
{
if (!dst || dstSize == 0)
return;
dst[0] = '\0';
if (src)
strncpy_s(dst, dstSize, src, _TRUNCATE);
}
}
extern "C"
{
gvfg_preview_status_t gvfg_preview_create(gvfg_preview_handle *out_handle)
{
if (!out_handle)
return GVFG_PREVIEW_EINVAL;
*out_handle = nullptr;
auto handle = std::make_unique<gvfg_preview_handle_t>();
*out_handle = handle.release();
return GVFG_PREVIEW_OK;
}
gvfg_preview_status_t gvfg_preview_destroy(gvfg_preview_handle handle)
{
if (!handle)
return GVFG_PREVIEW_OK;
delete handle;
return GVFG_PREVIEW_OK;
}
gvfg_preview_status_t gvfg_preview_attach_window(gvfg_preview_handle handle,
void *native_window_handle)
{
if (!handle)
return GVFG_PREVIEW_EINVAL;
if (!native_window_handle)
{
handle->renderer.shutdown();
return GVFG_PREVIEW_EINVAL;
}
return handle->renderer.configure(native_window_handle) ? GVFG_PREVIEW_OK : GVFG_PREVIEW_ESTATE;
}
gvfg_preview_status_t gvfg_preview_render_frame(gvfg_preview_handle handle,
const gvfg_frame_t *frame)
{
if (!handle || !frame)
return GVFG_PREVIEW_EINVAL;
if (!frame->data || frame->width <= 0 || frame->height <= 0)
return GVFG_PREVIEW_EINVAL;
if (frame->pixel_format != GVFG_PIXFMT_YUY2 && frame->pixel_format != GVFG_PIXFMT_Y210)
return GVFG_PREVIEW_ENOTSUP;
return handle->renderer.render(*frame) ? GVFG_PREVIEW_OK : GVFG_PREVIEW_ERENDER;
}
gvfg_preview_status_t gvfg_preview_get_info(gvfg_preview_handle handle,
gvfg_preview_info_t *out_info)
{
if (!handle || !out_info)
return GVFG_PREVIEW_EINVAL;
std::memset(out_info, 0, sizeof(*out_info));
out_info->active = handle->renderer.active() ? 1 : 0;
out_info->width = handle->renderer.width();
out_info->height = handle->renderer.height();
out_info->bit_depth = handle->renderer.bitDepth();
copy_cstr(out_info->pixel_format,
sizeof(out_info->pixel_format),
handle->renderer.pixelFormat());
return GVFG_PREVIEW_OK;
}
gvfg_preview_status_t gvfg_preview_shutdown(gvfg_preview_handle handle)
{
if (!handle)
return GVFG_PREVIEW_EINVAL;
handle->renderer.shutdown();
return GVFG_PREVIEW_OK;
}
const char *gvfg_preview_strerror(gvfg_preview_status_t status)
{
switch (status)
{
case GVFG_PREVIEW_OK:
return "OK";
case GVFG_PREVIEW_EINVAL:
return "Invalid argument";
case GVFG_PREVIEW_ESTATE:
return "Invalid state";
case GVFG_PREVIEW_ENOTSUP:
return "Unsupported frame format";
case GVFG_PREVIEW_ERENDER:
return "Render failed";
default:
return "Unknown";
}
}
}