Integrate GPU conversion and update capture pipeline
This commit is contained in:
+11
-7
@@ -2,8 +2,6 @@ if (NOT DEFINED GVFG_SDK_VERSION)
|
||||
set(GVFG_SDK_VERSION "0.1.0" CACHE STRING "GVFG SDK version")
|
||||
endif()
|
||||
|
||||
option(GVFG_PCIES2MM_DEBUG_LOG "Write verbose PCIES2MM backend trace logs" OFF)
|
||||
|
||||
project(gvfg VERSION ${GVFG_SDK_VERSION} LANGUAGES CXX)
|
||||
|
||||
add_library(gvfg SHARED
|
||||
@@ -18,6 +16,9 @@ add_library(gvfg SHARED
|
||||
src/backend/pcies2mm/pcies2mm_reg.h
|
||||
src/backend/pcies2mm/pcies2mm_video_format.cpp
|
||||
src/backend/pcies2mm/pcies2mm_video_format.h
|
||||
src/gpu/d3d_conversion_pipeline.cpp
|
||||
src/gpu/d3d_conversion_pipeline.h
|
||||
src/gpu/gvfg_gpu_convert.cpp
|
||||
src/gvfg_capture.cpp
|
||||
)
|
||||
|
||||
@@ -32,6 +33,7 @@ target_include_directories(gvfg
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/include
|
||||
PRIVATE
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/backend/pcies2mm
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/gpu
|
||||
)
|
||||
|
||||
target_compile_definitions(gvfg PRIVATE
|
||||
@@ -39,14 +41,16 @@ target_compile_definitions(gvfg PRIVATE
|
||||
GVFG_SDK_VERSION="${GVFG_SDK_VERSION}"
|
||||
)
|
||||
|
||||
if (GVFG_PCIES2MM_DEBUG_LOG)
|
||||
target_compile_definitions(gvfg PRIVATE GVFG_PCIES2MM_DEBUG_LOG=1)
|
||||
else()
|
||||
target_compile_definitions(gvfg PRIVATE GVFG_PCIES2MM_DEBUG_LOG=0)
|
||||
endif()
|
||||
target_compile_definitions(gvfg PRIVATE
|
||||
GVFG_INTERNAL_DIAGNOSTICS=$<IF:$<CONFIG:Debug>,1,0>
|
||||
)
|
||||
|
||||
target_link_libraries(gvfg PRIVATE
|
||||
setupapi
|
||||
user32
|
||||
dxgi
|
||||
d3d11
|
||||
D3DCompiler
|
||||
)
|
||||
|
||||
if (MSVC)
|
||||
|
||||
@@ -99,8 +99,8 @@ extern "C"
|
||||
typedef enum
|
||||
{
|
||||
GVFG_PIXFMT_UNKNOWN = 0,
|
||||
GVFG_PIXFMT_YUY2 = 1,
|
||||
GVFG_PIXFMT_Y210 = 2
|
||||
GVFG_PIXFMT_Y210 = 2,
|
||||
GVFG_PIXFMT_YVYU = 3
|
||||
} gvfg_pixel_format_t;
|
||||
|
||||
typedef enum
|
||||
@@ -117,12 +117,12 @@ extern "C"
|
||||
|
||||
typedef struct
|
||||
{
|
||||
int connected; /* Non-zero while the selected channel has a valid input signal. */
|
||||
int channel; /* gvfg_channel_t selected when the device was opened. */
|
||||
int width; /* Signal width in pixels when connected. */
|
||||
int height; /* Signal height in pixels when connected. */
|
||||
int pixel_format; /* gvfg_pixel_format_t value for the actual DMA payload. */
|
||||
int bit_depth; /* Signal bit depth derived from the payload format. */
|
||||
int connected; /* Non-zero while the selected channel has a valid input signal. */
|
||||
int channel; /* gvfg_channel_t selected when the device was opened. */
|
||||
int width; /* Signal width in pixels when connected. */
|
||||
int height; /* Signal height in pixels when connected. */
|
||||
int pixel_format; /* gvfg_pixel_format_t value for the actual DMA payload. */
|
||||
int bit_depth; /* Signal bit depth derived from the payload format. */
|
||||
} gvfg_signal_status_t;
|
||||
|
||||
typedef struct
|
||||
@@ -133,16 +133,33 @@ extern "C"
|
||||
|
||||
typedef struct
|
||||
{
|
||||
const void *data; /* Native frame buffer. Valid until gvfg_release_frame() is called. */
|
||||
uint64_t data_size; /* Total bytes available from data. */
|
||||
int width; /* Frame width in pixels. */
|
||||
int height; /* Frame height in pixels. */
|
||||
const void *data; /* Native frame buffer. Valid until gvfg_release_frame() is called. */
|
||||
uint64_t data_size; /* Total bytes available from data. */
|
||||
int width; /* Frame width in pixels. */
|
||||
int height; /* Frame height in pixels. */
|
||||
int row_stride_bytes; /* Byte distance between the starts of adjacent rows. */
|
||||
int pixel_format; /* gvfg_pixel_format_t value. */
|
||||
int bit_depth; /* Bits per color channel of the native frame. */
|
||||
uint64_t frame_id; /* Monotonic identifier within the current gvfg_start()/stop() run. */
|
||||
int pixel_format; /* gvfg_pixel_format_t value. */
|
||||
int bit_depth; /* Bits per color channel of the native frame. */
|
||||
uint64_t frame_id; /* Monotonic identifier within the current gvfg_start()/stop() run. */
|
||||
} gvfg_frame_t;
|
||||
|
||||
/* Packed RGB formats produced by gvfg_gpu_convert_to_buffer(). */
|
||||
typedef enum
|
||||
{
|
||||
/* DXGI-compatible B8G8R8A8_UNORM byte layout, alpha is 255. */
|
||||
GVFG_GPU_OUTPUT_BGRA8 = 1,
|
||||
/* DXGI-compatible R10G10B10A2_UNORM packed uint32 layout, alpha is 3. */
|
||||
GVFG_GPU_OUTPUT_RGB10A2 = 2
|
||||
} gvfg_gpu_output_format_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
void *data; /* Caller-owned destination memory. */
|
||||
uint64_t data_size; /* Bytes available from data. */
|
||||
int row_bytes; /* Destination stride; must be at least width * 4. */
|
||||
int pixel_format; /* gvfg_gpu_output_format_t. */
|
||||
} gvfg_gpu_output_buffer_t;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
GVFG_EVENT_UNKNOWN = 0,
|
||||
@@ -288,6 +305,22 @@ extern "C"
|
||||
_In_ gvfg_handle handle,
|
||||
_In_ const gvfg_frame_t *frame);
|
||||
|
||||
/*
|
||||
* Convert a captured YVYU or Y210 frame with the GPU and copy the packed
|
||||
* RGB result into caller-owned memory.
|
||||
*
|
||||
* This call is synchronous. The source frame and destination buffer must
|
||||
* remain valid until it returns; the SDK retains neither pointer. A frame
|
||||
* returned by gvfg_read_frame() must therefore be converted before
|
||||
* gvfg_release_frame().
|
||||
*
|
||||
* BGRA8 and RGB10A2 both require at least width * 4 bytes per row and
|
||||
* output->data_size of at least output->row_bytes * height bytes.
|
||||
*/
|
||||
GVFG_API gvfg_status_t gvfg_gpu_convert_to_buffer(
|
||||
_In_ const gvfg_frame_t *source,
|
||||
_In_ const gvfg_gpu_output_buffer_t *output);
|
||||
|
||||
/*
|
||||
* Poll one capture event.
|
||||
*
|
||||
|
||||
@@ -20,7 +20,7 @@ typedef struct
|
||||
int width; /* Width of the most recent frame returned by gvfg_read_frame(). */
|
||||
int height; /* Height of the most recent frame returned by gvfg_read_frame(). */
|
||||
int bit_depth; /* Bits per color channel of the frame buffer. */
|
||||
int pixel_format; /* gvfg_pixel_format_t value: YUY2 or Y210. */
|
||||
int pixel_format; /* gvfg_pixel_format_t value: YVYU or Y210. */
|
||||
int valid; /* Non-zero after a frame is returned during the current running session. */
|
||||
} gvfg_debug_last_frame_info_t;
|
||||
|
||||
|
||||
@@ -17,8 +17,8 @@ typedef enum pcies2mm_status_t
|
||||
typedef enum pcies2mm_pixel_format_t
|
||||
{
|
||||
PCIES2MM_PIXFMT_UNKNOWN = 0,
|
||||
PCIES2MM_PIXFMT_YUY2 = 1,
|
||||
PCIES2MM_PIXFMT_Y210 = 2
|
||||
PCIES2MM_PIXFMT_Y210 = 2,
|
||||
PCIES2MM_PIXFMT_YVYU = 3
|
||||
} pcies2mm_pixel_format_t;
|
||||
|
||||
typedef enum pcies2mm_stream_state_t
|
||||
|
||||
@@ -71,7 +71,7 @@ namespace
|
||||
|
||||
static void trace_log(bool forceDebugOutput, const char *tag, const char *fmt, ...)
|
||||
{
|
||||
#if GVFG_PCIES2MM_DEBUG_LOG
|
||||
#if GVFG_INTERNAL_DIAGNOSTICS
|
||||
(void)forceDebugOutput;
|
||||
#else
|
||||
if (!forceDebugOutput)
|
||||
@@ -126,7 +126,7 @@ namespace gvfg::internal
|
||||
stream_desc_.channel = 0;
|
||||
stream_desc_.width = kDefaultWidth;
|
||||
stream_desc_.height = kDefaultHeight;
|
||||
stream_desc_.pixel_format = PCIES2MM_PIXFMT_YUY2;
|
||||
stream_desc_.pixel_format = PCIES2MM_PIXFMT_YVYU;
|
||||
stream_desc_.buffer_count = kDefaultRingBufferCount;
|
||||
stream_bit_depth_ = 8;
|
||||
reset_stats(stats_, PCIES2MM_STREAM_STOPPED);
|
||||
@@ -268,10 +268,10 @@ namespace gvfg::internal
|
||||
if (desc.width == 0 || desc.height == 0)
|
||||
return PCIES2MM_EINVAL;
|
||||
|
||||
pcies2mm_pixel_format_t fmt = desc.pixel_format == PCIES2MM_PIXFMT_UNKNOWN ? PCIES2MM_PIXFMT_YUY2 : desc.pixel_format;
|
||||
pcies2mm_pixel_format_t fmt = desc.pixel_format == PCIES2MM_PIXFMT_UNKNOWN ? PCIES2MM_PIXFMT_YVYU : desc.pixel_format;
|
||||
switch (fmt)
|
||||
{
|
||||
case PCIES2MM_PIXFMT_YUY2:
|
||||
case PCIES2MM_PIXFMT_YVYU:
|
||||
case PCIES2MM_PIXFMT_Y210:
|
||||
break;
|
||||
default:
|
||||
@@ -347,6 +347,18 @@ namespace gvfg::internal
|
||||
close_event_handles();
|
||||
return fail(PCIES2MM_EIO, "enable video capture", err);
|
||||
}
|
||||
|
||||
uint32_t dmaEnableReadback = 0;
|
||||
uint32_t videoEnableReadback = 0;
|
||||
const bool dmaReadOk = read_reg(video_base() + VIDEO_DMA_EN_OFFSET, dmaEnableReadback);
|
||||
const bool videoReadOk = read_reg(video_base() + VIDEO_EN_OFFSET, videoEnableReadback);
|
||||
PCIES2MM_LOG("capture enable: dma_write=%d video_write=%d dma_read=%s0x%08X video_read=%s0x%08X",
|
||||
dmaEnableOk ? 1 : 0,
|
||||
videoEnableOk ? 1 : 0,
|
||||
dmaReadOk ? "" : "FAILED/",
|
||||
dmaEnableReadback,
|
||||
videoReadOk ? "" : "FAILED/",
|
||||
videoEnableReadback);
|
||||
}
|
||||
|
||||
running_ = true;
|
||||
@@ -724,7 +736,15 @@ namespace gvfg::internal
|
||||
void PcieS2mmCaptureSession::capture_thread_proc()
|
||||
{
|
||||
const uint32_t channel = active_channel();
|
||||
write_reg(INTERRUPT_BASE + IRQ_MASK_W1S_OFFSET, video_irq_mask_bit());
|
||||
const uint32_t irqBit = video_irq_mask_bit();
|
||||
const bool irqMaskWriteOk = write_reg(INTERRUPT_BASE + IRQ_MASK_W1S_OFFSET, irqBit);
|
||||
uint32_t irqMaskReadback = 0;
|
||||
const bool irqMaskReadOk = read_reg(INTERRUPT_BASE + IRQ_MASK_STATUS_OFFSET, irqMaskReadback);
|
||||
PCIES2MM_LOG("capture wait start: irq_bit=0x%08X mask_write=%d mask_read=%s0x%08X",
|
||||
irqBit,
|
||||
irqMaskWriteOk ? 1 : 0,
|
||||
irqMaskReadOk ? "" : "FAILED/",
|
||||
irqMaskReadback);
|
||||
HANDLE waitHandles[] = {
|
||||
dma_event_,
|
||||
format_change_event_,
|
||||
@@ -732,6 +752,7 @@ namespace gvfg::internal
|
||||
plug_out_event_,
|
||||
};
|
||||
constexpr DWORD waitHandleCount = 4;
|
||||
uint64_t diagnosticTimeouts = 0;
|
||||
|
||||
while (running_)
|
||||
{
|
||||
@@ -743,6 +764,37 @@ namespace gvfg::internal
|
||||
break;
|
||||
if (waitResult == WAIT_TIMEOUT)
|
||||
{
|
||||
++diagnosticTimeouts;
|
||||
if (diagnosticTimeouts <= 5 || (diagnosticTimeouts % 10) == 0)
|
||||
{
|
||||
uint32_t videoEnable = 0;
|
||||
uint32_t dmaEnable = 0;
|
||||
uint32_t videoIrqStatus = 0;
|
||||
uint32_t dmaIrqStatus = 0;
|
||||
uint32_t irqPending = 0;
|
||||
uint32_t irqMasked = 0;
|
||||
uint32_t irqMask = 0;
|
||||
const bool registersOk =
|
||||
read_reg(video_base() + VIDEO_EN_OFFSET, videoEnable) &&
|
||||
read_reg(video_base() + VIDEO_DMA_EN_OFFSET, dmaEnable) &&
|
||||
read_reg(video_base() + VIDEO_IRQ_STATUS_OFFSET, videoIrqStatus) &&
|
||||
read_reg(video_base() + VIDEO_DMA_IRQ_STATUS_OFFSET, dmaIrqStatus) &&
|
||||
read_reg(INTERRUPT_BASE + IRQ_PENDING_STATUS_OFFSET, irqPending) &&
|
||||
read_reg(INTERRUPT_BASE + IRQ_MASKED_STATUS_OFFSET, irqMasked) &&
|
||||
read_reg(INTERRUPT_BASE + IRQ_MASK_STATUS_OFFSET, irqMask);
|
||||
PCIES2MM_LOG("wait timeout #%llu: regs_ok=%d capture=%d probe=%d video_en=0x%08X dma_en=0x%08X video_irq=0x%08X dma_irq=0x%08X irq_pending=0x%08X irq_masked=0x%08X irq_mask=0x%08X",
|
||||
static_cast<unsigned long long>(diagnosticTimeouts),
|
||||
registersOk ? 1 : 0,
|
||||
capture_active_.load(std::memory_order_acquire) ? 1 : 0,
|
||||
signal_probe_active_.load(std::memory_order_acquire) ? 1 : 0,
|
||||
videoEnable,
|
||||
dmaEnable,
|
||||
videoIrqStatus,
|
||||
dmaIrqStatus,
|
||||
irqPending,
|
||||
irqMasked,
|
||||
irqMask);
|
||||
}
|
||||
if (!signal_presence_known_.load(std::memory_order_acquire) &&
|
||||
!capture_active_.load(std::memory_order_acquire) &&
|
||||
refresh_stream_from_registers(true))
|
||||
@@ -778,15 +830,19 @@ namespace gvfg::internal
|
||||
switch (waitResult - WAIT_OBJECT_0)
|
||||
{
|
||||
case 0:
|
||||
PCIES2MM_LOG("event: DMA");
|
||||
handle_dma_event(channel);
|
||||
break;
|
||||
case 1:
|
||||
PCIES2MM_LOG("event: FORMAT_CHANGE");
|
||||
handle_format_change_event(channel);
|
||||
break;
|
||||
case 2:
|
||||
PCIES2MM_LOG("event: PLUG_IN");
|
||||
handle_plugin_event(channel);
|
||||
break;
|
||||
case 3:
|
||||
PCIES2MM_LOG("event: PLUG_OUT");
|
||||
handle_unplug_event(channel);
|
||||
break;
|
||||
default:
|
||||
@@ -805,9 +861,13 @@ namespace gvfg::internal
|
||||
uint32_t doneIndex = 0;
|
||||
if (!get_video_done_index(channel, doneIndex))
|
||||
{
|
||||
const DWORD err = GetLastError();
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
++stats_.dma_errors;
|
||||
frame_cv_.notify_all();
|
||||
PCIES2MM_ERROR_LOG("get_video_done_index failed channel=%u win32=%lu",
|
||||
channel,
|
||||
static_cast<unsigned long>(err));
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1006,6 +1066,38 @@ namespace gvfg::internal
|
||||
return false;
|
||||
|
||||
const pcies2mm_pixel_format_t pixelFormat = decode_pixel_format(rawFormat);
|
||||
const char fourccLe[5] = {
|
||||
static_cast<char>(rawFormat & 0xffu),
|
||||
static_cast<char>((rawFormat >> 8) & 0xffu),
|
||||
static_cast<char>((rawFormat >> 16) & 0xffu),
|
||||
static_cast<char>((rawFormat >> 24) & 0xffu),
|
||||
'\0'};
|
||||
const char *decodedFormat = "UNKNOWN";
|
||||
switch (pixelFormat)
|
||||
{
|
||||
case PCIES2MM_PIXFMT_YVYU:
|
||||
decodedFormat = "YVYU";
|
||||
break;
|
||||
case PCIES2MM_PIXFMT_Y210:
|
||||
decodedFormat = "Y210";
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
PCIES2MM_LOG("stream registers: width=%u height=%u VIDEO_FORMAT=0x%08X register_text='%c%c%c%c' fourcc_le='%c%c%c%c' decoded=%s frame_bytes=%zu",
|
||||
width,
|
||||
height,
|
||||
rawFormat,
|
||||
static_cast<char>((rawFormat >> 24) & 0xffu),
|
||||
static_cast<char>((rawFormat >> 16) & 0xffu),
|
||||
static_cast<char>((rawFormat >> 8) & 0xffu),
|
||||
static_cast<char>(rawFormat & 0xffu),
|
||||
fourccLe[0],
|
||||
fourccLe[1],
|
||||
fourccLe[2],
|
||||
fourccLe[3],
|
||||
decodedFormat,
|
||||
bytes_per_frame(width, height, pixelFormat));
|
||||
if (pixelFormat == PCIES2MM_PIXFMT_UNKNOWN)
|
||||
return false;
|
||||
|
||||
|
||||
@@ -3,12 +3,10 @@
|
||||
namespace
|
||||
{
|
||||
// This board revision can report v210 even though its DMA payload is Y210
|
||||
// (16-bit containers with 10 valid bits). Legacy register value 0 has the
|
||||
// same Y210 payload.
|
||||
constexpr uint32_t kLegacyY210Format = 0;
|
||||
constexpr uint32_t kFpgaFourccYuy2 = 0x59555932u;
|
||||
constexpr uint32_t kFpgaFourccV210 = 0x76323130u;
|
||||
constexpr uint32_t kFpgaFourccY210 = 0x59323130u;
|
||||
// (16-bit containers with 10 valid bits).
|
||||
constexpr uint32_t kRegisterDisplayYvyu = 0x59565955u;
|
||||
constexpr uint32_t kRegisterDisplayV210 = 0x76323130u;
|
||||
constexpr uint32_t kRegisterDisplayY210 = 0x59323130u;
|
||||
|
||||
constexpr uint32_t fourcc(char a, char b, char c, char d)
|
||||
{
|
||||
@@ -23,19 +21,18 @@ namespace gvfg::internal
|
||||
{
|
||||
pcies2mm_pixel_format_t decode_pixel_format(uint32_t registerValue)
|
||||
{
|
||||
if (registerValue == kLegacyY210Format)
|
||||
return PCIES2MM_PIXFMT_Y210;
|
||||
|
||||
// CaptureDemo compares these display-order FPGA register values.
|
||||
if (registerValue == kFpgaFourccYuy2)
|
||||
return PCIES2MM_PIXFMT_YUY2;
|
||||
if (registerValue == kFpgaFourccV210 || registerValue == kFpgaFourccY210)
|
||||
// Current firmware writes character codes in display order. Normalize
|
||||
// those values here so no register naming leaks into the SDK formats.
|
||||
if (registerValue == kRegisterDisplayYvyu)
|
||||
return PCIES2MM_PIXFMT_YVYU;
|
||||
if (registerValue == kRegisterDisplayV210 || registerValue == kRegisterDisplayY210)
|
||||
return PCIES2MM_PIXFMT_Y210;
|
||||
|
||||
// Also accept corrected Windows FOURCC values from future firmware.
|
||||
switch (registerValue)
|
||||
{
|
||||
case fourcc('Y', 'U', 'Y', '2'):
|
||||
return PCIES2MM_PIXFMT_YUY2;
|
||||
case fourcc('Y', 'V', 'Y', 'U'):
|
||||
return PCIES2MM_PIXFMT_YVYU;
|
||||
case fourcc('v', '2', '1', '0'):
|
||||
case fourcc('Y', '2', '1', '0'):
|
||||
return PCIES2MM_PIXFMT_Y210;
|
||||
@@ -46,7 +43,7 @@ namespace gvfg::internal
|
||||
|
||||
uint32_t bit_depth_for_pixfmt(pcies2mm_pixel_format_t format)
|
||||
{
|
||||
if (format == PCIES2MM_PIXFMT_YUY2)
|
||||
if (format == PCIES2MM_PIXFMT_YVYU)
|
||||
return 8;
|
||||
if (format == PCIES2MM_PIXFMT_Y210)
|
||||
return 10;
|
||||
@@ -56,7 +53,7 @@ namespace gvfg::internal
|
||||
size_t bytes_per_frame(uint32_t width, uint32_t height, pcies2mm_pixel_format_t format)
|
||||
{
|
||||
const size_t pixels = static_cast<size_t>(width) * static_cast<size_t>(height);
|
||||
if (format == PCIES2MM_PIXFMT_YUY2)
|
||||
if (format == PCIES2MM_PIXFMT_YVYU)
|
||||
return pixels * 2u;
|
||||
if (format == PCIES2MM_PIXFMT_Y210)
|
||||
return pixels * 4u;
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,125 @@
|
||||
#pragma once
|
||||
|
||||
#include <d3d11_1.h>
|
||||
#include <dxgi1_2.h>
|
||||
#include <stdint.h>
|
||||
#include <wrl.h>
|
||||
|
||||
namespace gvfg::internal
|
||||
{
|
||||
|
||||
typedef enum
|
||||
{
|
||||
GVFG_RENDER_FMT_Y210,
|
||||
GVFG_RENDER_FMT_YVYU
|
||||
} gvfg_render_pixfmt_t;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
GVFG_RENDER_PREVIEW_BITDEPTH_8BIT = 0,
|
||||
GVFG_RENDER_PREVIEW_BITDEPTH_10BIT = 1,
|
||||
GVFG_RENDER_PREVIEW_BITDEPTH_AUTO = 2
|
||||
} gvfg_render_preview_bitdepth_t;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
GVFG_PREVIEW_PRESENT_FAILED = 0,
|
||||
GVFG_PREVIEW_PRESENTED = 1,
|
||||
GVFG_PREVIEW_PRESENT_SKIPPED = 2
|
||||
} gvfg_preview_present_result_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
void *hwnd;
|
||||
int enable_preview;
|
||||
int swapchain_10bit;
|
||||
} gvfg_render_preview_desc_t;
|
||||
|
||||
class D3DPreviewPipeline
|
||||
{
|
||||
public:
|
||||
D3DPreviewPipeline() = default;
|
||||
~D3DPreviewPipeline() = default;
|
||||
|
||||
bool initialize(ID3D11Device *d3d,
|
||||
ID3D11DeviceContext *ctx);
|
||||
|
||||
bool configurePreview(const gvfg_render_preview_desc_t &desc);
|
||||
void set_source_bit_depth(int bits);
|
||||
void release_preview_swapchain();
|
||||
|
||||
bool create_shaders_and_states();
|
||||
bool ensure_rt_and_pipeline(int w, int h);
|
||||
bool ensure_preview_swapchain(int w, int h);
|
||||
bool preview_swapchain_10bit() const { return preview_swapchain_10bit_; }
|
||||
gvfg_preview_present_result_t present_preview(int src_w, int src_h);
|
||||
DXGI_FORMAT preview_backbuffer_format() const;
|
||||
DXGI_FORMAT scene_texture_format() const;
|
||||
DXGI_FORMAT linear_fp16_texture_format() const;
|
||||
bool blit_fp16_to_rgba8(int frame_w, int frame_h);
|
||||
bool blit_fp16_to_rgb10a2(int frame_w, int frame_h);
|
||||
bool upload_packed_422_frame(const uint8_t *data, int src_stride, int frame_w, int frame_h);
|
||||
bool upload_y210_frame(const uint8_t *data, int src_stride, int frame_w, int frame_h);
|
||||
bool render_uploaded_yuv_to_fp16(gvfg_render_pixfmt_t fmt, int frame_w, int frame_h);
|
||||
bool copy_fp16_to_scene();
|
||||
bool readback_to_buffer(void *destination,
|
||||
uint64_t destination_size,
|
||||
int destination_row_bytes,
|
||||
DXGI_FORMAT destination_format,
|
||||
int frame_w,
|
||||
int frame_h);
|
||||
|
||||
ID3D11Device *d3d_ = nullptr;
|
||||
ID3D11DeviceContext *ctx_ = nullptr;
|
||||
|
||||
Microsoft::WRL::ComPtr<ID3D11Texture2D> rt_fp16_;
|
||||
Microsoft::WRL::ComPtr<ID3D11RenderTargetView> rtv_fp16_;
|
||||
Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> srv_fp16_;
|
||||
|
||||
Microsoft::WRL::ComPtr<ID3D11Texture2D> rt_scene_fp16_;
|
||||
Microsoft::WRL::ComPtr<ID3D11RenderTargetView> rtv_scene_fp16_;
|
||||
Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> srv_scene_fp16_;
|
||||
|
||||
Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> srv_rgba_;
|
||||
Microsoft::WRL::ComPtr<ID3D11PixelShader> ps_rgba8_to_preview_;
|
||||
|
||||
Microsoft::WRL::ComPtr<ID3D11Texture2D> rt_rgba_;
|
||||
Microsoft::WRL::ComPtr<ID3D11Texture2D> rt_rgb10_;
|
||||
Microsoft::WRL::ComPtr<ID3D11RenderTargetView> rtv_rgb10_;
|
||||
Microsoft::WRL::ComPtr<ID3D11Texture2D> readback_bgra8_;
|
||||
Microsoft::WRL::ComPtr<ID3D11Texture2D> readback_rgb10_;
|
||||
Microsoft::WRL::ComPtr<ID3D11Texture2D> upload_yuy2_packed_;
|
||||
Microsoft::WRL::ComPtr<ID3D11Texture2D> upload_y210_packed_;
|
||||
Microsoft::WRL::ComPtr<ID3D11RenderTargetView> rtv_rgba_;
|
||||
|
||||
Microsoft::WRL::ComPtr<ID3D11Buffer> cs_params_;
|
||||
|
||||
Microsoft::WRL::ComPtr<ID3D11VertexShader> vs_;
|
||||
Microsoft::WRL::ComPtr<ID3D11PixelShader> ps_yuy2_;
|
||||
Microsoft::WRL::ComPtr<ID3D11PixelShader> ps_y210_;
|
||||
Microsoft::WRL::ComPtr<ID3D11PixelShader> ps_fp16_to_rgba8_;
|
||||
Microsoft::WRL::ComPtr<ID3D11PixelShader> ps_fp16_to_preview_;
|
||||
Microsoft::WRL::ComPtr<ID3D11InputLayout> il_;
|
||||
Microsoft::WRL::ComPtr<ID3D11Buffer> vb_;
|
||||
Microsoft::WRL::ComPtr<ID3D11SamplerState> samp_;
|
||||
|
||||
void *preview_hwnd_ = nullptr;
|
||||
bool preview_enabled_ = false;
|
||||
int preview_swapchain_mode_ = GVFG_RENDER_PREVIEW_BITDEPTH_10BIT;
|
||||
int preview_source_bit_depth_ = 0;
|
||||
bool preview_swapchain_10bit_ = false;
|
||||
DXGI_FORMAT preview_swapchain_format_ = DXGI_FORMAT_UNKNOWN;
|
||||
int preview_w_ = 0;
|
||||
int preview_h_ = 0;
|
||||
|
||||
int rt_w_ = 0;
|
||||
int rt_h_ = 0;
|
||||
|
||||
Microsoft::WRL::ComPtr<IDXGISwapChain1> preview_swapchain_;
|
||||
Microsoft::WRL::ComPtr<ID3D11Texture2D> preview_backbuf_;
|
||||
Microsoft::WRL::ComPtr<ID3D11RenderTargetView> preview_rtv_;
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,214 @@
|
||||
#include "gvfg_capture.h"
|
||||
|
||||
#include "d3d_conversion_pipeline.h"
|
||||
|
||||
#include <d3d11_4.h>
|
||||
#include <dxgi.h>
|
||||
#include <cstdint>
|
||||
#include <cstddef>
|
||||
#include <iterator>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <wrl/client.h>
|
||||
|
||||
using Microsoft::WRL::ComPtr;
|
||||
|
||||
#if INTPTR_MAX == INT64_MAX
|
||||
static_assert(sizeof(gvfg_gpu_output_buffer_t) == 24,
|
||||
"gvfg_gpu_output_buffer_t x64 ABI must remain frozen");
|
||||
static_assert(offsetof(gvfg_gpu_output_buffer_t, data) == 0);
|
||||
static_assert(offsetof(gvfg_gpu_output_buffer_t, data_size) == 8);
|
||||
static_assert(offsetof(gvfg_gpu_output_buffer_t, row_bytes) == 16);
|
||||
static_assert(offsetof(gvfg_gpu_output_buffer_t, pixel_format) == 20);
|
||||
#endif
|
||||
|
||||
namespace
|
||||
{
|
||||
class GpuBufferConverter
|
||||
{
|
||||
public:
|
||||
gvfg_status_t convert(const gvfg_frame_t &source,
|
||||
const gvfg_gpu_output_buffer_t &output)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
if (!ensureDevice())
|
||||
return GVFG_EIO;
|
||||
|
||||
if (!pipeline_)
|
||||
pipeline_ = std::make_unique<gvfg::internal::D3DPreviewPipeline>();
|
||||
if (!pipeline_->initialize(device_.Get(), context_.Get()) ||
|
||||
!pipeline_->ensure_rt_and_pipeline(source.width, source.height))
|
||||
return GVFG_EIO;
|
||||
|
||||
gvfg::internal::gvfg_render_pixfmt_t renderFormat{};
|
||||
bool uploaded = false;
|
||||
const auto *data = static_cast<const uint8_t *>(source.data);
|
||||
switch (source.pixel_format)
|
||||
{
|
||||
case GVFG_PIXFMT_YVYU:
|
||||
renderFormat = gvfg::internal::GVFG_RENDER_FMT_YVYU;
|
||||
uploaded = pipeline_->upload_packed_422_frame(data,
|
||||
source.row_stride_bytes,
|
||||
source.width,
|
||||
source.height);
|
||||
break;
|
||||
case GVFG_PIXFMT_Y210:
|
||||
renderFormat = gvfg::internal::GVFG_RENDER_FMT_Y210;
|
||||
uploaded = pipeline_->upload_y210_frame(data,
|
||||
source.row_stride_bytes,
|
||||
source.width,
|
||||
source.height);
|
||||
break;
|
||||
default:
|
||||
return GVFG_ENOTSUP;
|
||||
}
|
||||
|
||||
if (!uploaded ||
|
||||
!pipeline_->render_uploaded_yuv_to_fp16(renderFormat, source.width, source.height) ||
|
||||
!pipeline_->copy_fp16_to_scene())
|
||||
return GVFG_EIO;
|
||||
|
||||
DXGI_FORMAT outputFormat = DXGI_FORMAT_UNKNOWN;
|
||||
bool rendered = false;
|
||||
switch (output.pixel_format)
|
||||
{
|
||||
case GVFG_GPU_OUTPUT_BGRA8:
|
||||
outputFormat = DXGI_FORMAT_B8G8R8A8_UNORM;
|
||||
rendered = pipeline_->blit_fp16_to_rgba8(source.width, source.height);
|
||||
break;
|
||||
case GVFG_GPU_OUTPUT_RGB10A2:
|
||||
outputFormat = DXGI_FORMAT_R10G10B10A2_UNORM;
|
||||
rendered = pipeline_->blit_fp16_to_rgb10a2(source.width, source.height);
|
||||
break;
|
||||
default:
|
||||
return GVFG_ENOTSUP;
|
||||
}
|
||||
|
||||
if (!rendered ||
|
||||
!pipeline_->readback_to_buffer(output.data,
|
||||
output.data_size,
|
||||
output.row_bytes,
|
||||
outputFormat,
|
||||
source.width,
|
||||
source.height))
|
||||
return GVFG_EIO;
|
||||
return GVFG_OK;
|
||||
}
|
||||
|
||||
private:
|
||||
bool ensureDevice()
|
||||
{
|
||||
if (device_ && context_)
|
||||
return true;
|
||||
|
||||
UINT flags = D3D11_CREATE_DEVICE_BGRA_SUPPORT;
|
||||
#ifdef _DEBUG
|
||||
flags |= D3D11_CREATE_DEVICE_DEBUG;
|
||||
#endif
|
||||
const D3D_FEATURE_LEVEL levels[] = {
|
||||
D3D_FEATURE_LEVEL_11_1,
|
||||
D3D_FEATURE_LEVEL_11_0,
|
||||
};
|
||||
D3D_FEATURE_LEVEL actual{};
|
||||
HRESULT hr = D3D11CreateDevice(nullptr,
|
||||
D3D_DRIVER_TYPE_HARDWARE,
|
||||
nullptr,
|
||||
flags,
|
||||
levels,
|
||||
static_cast<UINT>(std::size(levels)),
|
||||
D3D11_SDK_VERSION,
|
||||
&device_,
|
||||
&actual,
|
||||
&context_);
|
||||
#ifdef _DEBUG
|
||||
if (FAILED(hr))
|
||||
{
|
||||
flags &= ~D3D11_CREATE_DEVICE_DEBUG;
|
||||
hr = D3D11CreateDevice(nullptr,
|
||||
D3D_DRIVER_TYPE_HARDWARE,
|
||||
nullptr,
|
||||
flags,
|
||||
levels,
|
||||
static_cast<UINT>(std::size(levels)),
|
||||
D3D11_SDK_VERSION,
|
||||
&device_,
|
||||
&actual,
|
||||
&context_);
|
||||
}
|
||||
#endif
|
||||
if (FAILED(hr) || !device_ || !context_)
|
||||
return false;
|
||||
|
||||
ComPtr<ID3D11Multithread> multithread;
|
||||
if (SUCCEEDED(device_.As(&multithread)) && multithread)
|
||||
multithread->SetMultithreadProtected(TRUE);
|
||||
return true;
|
||||
}
|
||||
|
||||
std::mutex mutex_;
|
||||
ComPtr<ID3D11Device> device_;
|
||||
ComPtr<ID3D11DeviceContext> context_;
|
||||
std::unique_ptr<gvfg::internal::D3DPreviewPipeline> pipeline_;
|
||||
};
|
||||
|
||||
bool checkedFrameLayout(const gvfg_frame_t &source)
|
||||
{
|
||||
if (!source.data || source.width <= 0 || source.height <= 0 ||
|
||||
source.row_stride_bytes <= 0)
|
||||
return false;
|
||||
|
||||
uint64_t minimumRowBytes = 0;
|
||||
switch (source.pixel_format)
|
||||
{
|
||||
case GVFG_PIXFMT_YVYU:
|
||||
minimumRowBytes = static_cast<uint64_t>(source.width) * 2u;
|
||||
break;
|
||||
case GVFG_PIXFMT_Y210:
|
||||
minimumRowBytes = static_cast<uint64_t>(source.width) * 4u;
|
||||
break;
|
||||
default:
|
||||
return true;
|
||||
}
|
||||
if (static_cast<uint64_t>(source.row_stride_bytes) < minimumRowBytes)
|
||||
return false;
|
||||
const uint64_t rowOffset = static_cast<uint64_t>(source.row_stride_bytes) *
|
||||
static_cast<uint64_t>(source.height - 1);
|
||||
return rowOffset <= UINT64_MAX - minimumRowBytes &&
|
||||
source.data_size >= rowOffset + minimumRowBytes;
|
||||
}
|
||||
|
||||
GpuBufferConverter &sharedConverter()
|
||||
{
|
||||
static GpuBufferConverter converter;
|
||||
return converter;
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" GVFG_API gvfg_status_t gvfg_gpu_convert_to_buffer(
|
||||
const gvfg_frame_t *source,
|
||||
const gvfg_gpu_output_buffer_t *output)
|
||||
{
|
||||
if (!source || !output || !output->data ||
|
||||
source->width <= 0 || source->height <= 0 ||
|
||||
output->row_bytes <= 0)
|
||||
return GVFG_EINVAL;
|
||||
|
||||
if (source->pixel_format != GVFG_PIXFMT_YVYU &&
|
||||
source->pixel_format != GVFG_PIXFMT_Y210)
|
||||
return GVFG_ENOTSUP;
|
||||
if (output->pixel_format != GVFG_GPU_OUTPUT_BGRA8 &&
|
||||
output->pixel_format != GVFG_GPU_OUTPUT_RGB10A2)
|
||||
return GVFG_ENOTSUP;
|
||||
if (!checkedFrameLayout(*source))
|
||||
return GVFG_EINVAL;
|
||||
|
||||
const uint64_t minimumRowBytes = static_cast<uint64_t>(source->width) * 4u;
|
||||
if (static_cast<uint64_t>(output->row_bytes) < minimumRowBytes)
|
||||
return GVFG_EINVAL;
|
||||
const uint64_t requiredSize = static_cast<uint64_t>(output->row_bytes) *
|
||||
static_cast<uint64_t>(source->height);
|
||||
if (output->data_size < requiredSize)
|
||||
return GVFG_EINVAL;
|
||||
|
||||
return sharedConverter().convert(*source, *output);
|
||||
}
|
||||
@@ -135,8 +135,8 @@ namespace
|
||||
{
|
||||
switch (fmt)
|
||||
{
|
||||
case PCIES2MM_PIXFMT_YUY2:
|
||||
return GVFG_PIXFMT_YUY2;
|
||||
case PCIES2MM_PIXFMT_YVYU:
|
||||
return GVFG_PIXFMT_YVYU;
|
||||
case PCIES2MM_PIXFMT_Y210:
|
||||
return GVFG_PIXFMT_Y210;
|
||||
default:
|
||||
@@ -148,8 +148,8 @@ namespace
|
||||
{
|
||||
switch (fmt)
|
||||
{
|
||||
case GVFG_PIXFMT_YUY2:
|
||||
return "YUY2";
|
||||
case GVFG_PIXFMT_YVYU:
|
||||
return "YVYU";
|
||||
case GVFG_PIXFMT_Y210:
|
||||
return "Y210";
|
||||
default:
|
||||
@@ -170,7 +170,7 @@ namespace
|
||||
int &outStride)
|
||||
{
|
||||
uint64_t bytesPerPixel = 0;
|
||||
if (pixelFormat == PCIES2MM_PIXFMT_YUY2)
|
||||
if (pixelFormat == PCIES2MM_PIXFMT_YVYU)
|
||||
bytesPerPixel = 2;
|
||||
else if (pixelFormat == PCIES2MM_PIXFMT_Y210)
|
||||
bytesPerPixel = 4;
|
||||
@@ -368,7 +368,7 @@ struct gvfg_handle_t
|
||||
const uint32_t configureWidth = waitingForSignal ? 2 : width;
|
||||
const uint32_t configureHeight = waitingForSignal ? 1 : height;
|
||||
const pcies2mm_pixel_format_t configureFormat =
|
||||
waitingForSignal || pixelFormat == PCIES2MM_PIXFMT_UNKNOWN ? PCIES2MM_PIXFMT_YUY2 : pixelFormat;
|
||||
waitingForSignal || pixelFormat == PCIES2MM_PIXFMT_UNKNOWN ? PCIES2MM_PIXFMT_YVYU : pixelFormat;
|
||||
|
||||
pcies2mm_stream_desc_t desc{};
|
||||
desc.channel = selectedChannel;
|
||||
|
||||
Reference in New Issue
Block a user