From dc0b4607925807ba60abba2cc9958e55a1777cb2 Mon Sep 17 00:00:00 2001 From: "Mark.lin" Date: Fri, 7 Aug 2026 09:50:19 +0800 Subject: [PATCH] Add GPU NV12 conversion APIs --- sdk/gvfg/include/gvfg_capture.h | 50 +++++- sdk/gvfg/src/gpu/d3d_conversion_pipeline.cpp | 165 ++++++++++++++++++- sdk/gvfg/src/gpu/d3d_conversion_pipeline.h | 14 ++ sdk/gvfg/src/gpu/gvfg_gpu_convert.cpp | 75 ++++++++- 4 files changed, 292 insertions(+), 12 deletions(-) diff --git a/sdk/gvfg/include/gvfg_capture.h b/sdk/gvfg/include/gvfg_capture.h index 8cac946..2ab851d 100644 --- a/sdk/gvfg/include/gvfg_capture.h +++ b/sdk/gvfg/include/gvfg_capture.h @@ -143,20 +143,22 @@ extern "C" 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(). */ + /* 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_RGB10A2 = 2, + /* 8-bit BT.709 limited-range 4:2:0: Y plane followed by interleaved UV. */ + GVFG_GPU_OUTPUT_NV12 = 3 } 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 row_bytes; /* Destination stride. For NV12, shared by Y and UV planes. */ int pixel_format; /* gvfg_gpu_output_format_t. */ } gvfg_gpu_output_buffer_t; @@ -306,21 +308,55 @@ extern "C" _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. + * Convert a captured YVYU or Y210 frame with the GPU and copy the 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. + * BGRA8 and RGB10A2 require at least width * 4 bytes per row and + * output->data_size >= output->row_bytes * height. + * NV12 requires even width and height, row_bytes >= width, and + * output->data_size >= output->row_bytes * (height + height / 2). Its + * layout is height rows of Y followed by height / 2 rows of interleaved UV. */ GVFG_API gvfg_status_t gvfg_gpu_convert_to_buffer( _In_ const gvfg_frame_t *source, _In_ const gvfg_gpu_output_buffer_t *output); + /* + * Convenience wrappers for the corresponding gvfg_gpu_output_format_t. + * These calls have the same synchronous lifetime rules as + * gvfg_gpu_convert_to_buffer(). + * + * BGRA8 and RGB10A2 require row_bytes >= source->width * 4 and + * destination_size >= row_bytes * source->height. + */ + GVFG_API gvfg_status_t gvfg_gpu_convert_to_bgra8( + _In_ const gvfg_frame_t *source, + _Out_writes_bytes_(destination_size) void *destination, + _In_ uint64_t destination_size, + _In_ int row_bytes); + + GVFG_API gvfg_status_t gvfg_gpu_convert_to_rgb10a2( + _In_ const gvfg_frame_t *source, + _Out_writes_bytes_(destination_size) void *destination, + _In_ uint64_t destination_size, + _In_ int row_bytes); + + /* + * Convert to BT.709 limited-range NV12. Width and height must be even. + * row_bytes must be at least source->width and is shared by both planes. + * destination_size must be at least row_bytes * (height + height / 2). + */ + GVFG_API gvfg_status_t gvfg_gpu_convert_to_nv12( + _In_ const gvfg_frame_t *source, + _Out_writes_bytes_(destination_size) void *destination, + _In_ uint64_t destination_size, + _In_ int row_bytes); + /* * Poll one capture event. * diff --git a/sdk/gvfg/src/gpu/d3d_conversion_pipeline.cpp b/sdk/gvfg/src/gpu/d3d_conversion_pipeline.cpp index d2ed025..2ed99c1 100644 --- a/sdk/gvfg/src/gpu/d3d_conversion_pipeline.cpp +++ b/sdk/gvfg/src/gpu/d3d_conversion_pipeline.cpp @@ -294,6 +294,31 @@ float4 main(PSIn i) : SV_Target } )"; +// RGB to studio-range BT.709 NV12. Rendering the UV target at half resolution +// makes the linear sample at each output texel average the corresponding 2x2 +// source pixels. +static const char *g_ps_fp16_to_nv12_y = R"( +Texture2D tex0 : register(t0); +SamplerState samL : register(s0); +float main(float4 pos : SV_Position, float2 uv : TEXCOORD0) : SV_Target +{ + float3 rgb = saturate(tex0.Sample(samL, uv).rgb); + return saturate(16.0 / 255.0 + dot(rgb, float3(0.182586, 0.614231, 0.062007))); +} +)"; + +static const char *g_ps_fp16_to_nv12_uv = R"( +Texture2D tex0 : register(t0); +SamplerState samL : register(s0); +float2 main(float4 pos : SV_Position, float2 uv : TEXCOORD0) : SV_Target +{ + float3 rgb = saturate(tex0.Sample(samL, uv).rgb); + float u = 128.0 / 255.0 + dot(rgb, float3(-0.100644, -0.338572, 0.439216)); + float v = 128.0 / 255.0 + dot(rgb, float3( 0.439216, -0.398942, -0.040274)); + return saturate(float2(u, v)); +} +)"; + static const char *g_ps_fp16_to_preview = R"( Texture2D tex0 : register(t0); SamplerState samL : register(s0); @@ -414,6 +439,18 @@ bool D3DPreviewPipeline::create_shaders_and_states() if (FAILED(d3d_->CreatePixelShader(psb4->GetBufferPointer(), psb4->GetBufferSize(), nullptr, &ps_fp16_to_rgba8_))) return false; + ComPtr psbNv12Y, psbNv12Uv; + if (FAILED(D3DCompile(g_ps_fp16_to_nv12_y, strlen(g_ps_fp16_to_nv12_y), nullptr, nullptr, nullptr, + "main", "ps_5_0", 0, 0, &psbNv12Y, &err)) || + FAILED(d3d_->CreatePixelShader(psbNv12Y->GetBufferPointer(), psbNv12Y->GetBufferSize(), + nullptr, &ps_fp16_to_nv12_y_))) + return false; + if (FAILED(D3DCompile(g_ps_fp16_to_nv12_uv, strlen(g_ps_fp16_to_nv12_uv), nullptr, nullptr, nullptr, + "main", "ps_5_0", 0, 0, &psbNv12Uv, &err)) || + FAILED(d3d_->CreatePixelShader(psbNv12Uv->GetBufferPointer(), psbNv12Uv->GetBufferSize(), + nullptr, &ps_fp16_to_nv12_uv_))) + return false; + if (FAILED(D3DCompile(g_ps_fp16_to_preview, strlen(g_ps_fp16_to_preview), nullptr, nullptr, nullptr, "main", "ps_5_0", 0, 0, &psb5, &err))) return false; @@ -470,7 +507,8 @@ bool D3DPreviewPipeline::ensure_rt_and_pipeline(int w, int h) const bool hasAllTargets = rt_fp16_ && rtv_fp16_ && srv_fp16_ && rt_scene_fp16_ && rtv_scene_fp16_ && srv_scene_fp16_ && rt_rgba_ && rtv_rgba_ && srv_rgba_ && - rt_rgb10_ && rtv_rgb10_; + rt_rgb10_ && rtv_rgb10_ && + rt_nv12_y_ && rtv_nv12_y_ && rt_nv12_uv_ && rtv_nv12_uv_; if (hasAllTargets && rt_w_ == w && rt_h_ == h) return true; @@ -488,6 +526,12 @@ bool D3DPreviewPipeline::ensure_rt_and_pipeline(int w, int h) rtv_rgb10_.Reset(); readback_bgra8_.Reset(); readback_rgb10_.Reset(); + rt_nv12_y_.Reset(); + rtv_nv12_y_.Reset(); + rt_nv12_uv_.Reset(); + rtv_nv12_uv_.Reset(); + readback_nv12_y_.Reset(); + readback_nv12_uv_.Reset(); // 1) High precision intermediate target D3D11_TEXTURE2D_DESC td{}; @@ -537,6 +581,22 @@ bool D3DPreviewPipeline::ensure_rt_and_pipeline(int w, int h) if (FAILED(d3d_->CreateRenderTargetView(rt_rgb10_.Get(), nullptr, &rtv_rgb10_))) return false; + // 5) Separate render targets matching the two NV12 planes. D3D11 planar + // render-target support varies by adapter; R8/RG8 targets are portable and + // are packed into the caller's NV12 buffer during readback. + td.Width = static_cast(w); + td.Height = static_cast(h); + td.Format = DXGI_FORMAT_R8_UNORM; + if (FAILED(d3d_->CreateTexture2D(&td, nullptr, &rt_nv12_y_)) || + FAILED(d3d_->CreateRenderTargetView(rt_nv12_y_.Get(), nullptr, &rtv_nv12_y_))) + return false; + td.Width = static_cast((w + 1) / 2); + td.Height = static_cast((h + 1) / 2); + td.Format = DXGI_FORMAT_R8G8_UNORM; + if (FAILED(d3d_->CreateTexture2D(&td, nullptr, &rt_nv12_uv_)) || + FAILED(d3d_->CreateRenderTargetView(rt_nv12_uv_.Get(), nullptr, &rtv_nv12_uv_))) + return false; + const bool ok = create_shaders_and_states(); if (ok) { @@ -626,6 +686,47 @@ bool D3DPreviewPipeline::blit_fp16_to_rgb10a2(int frame_w, int frame_h) return true; } +bool D3DPreviewPipeline::blit_fp16_to_nv12(int frame_w, int frame_h) +{ + if (!rtv_nv12_y_ || !rtv_nv12_uv_ || !srv_scene_fp16_ || !vs_ || + !ps_fp16_to_nv12_y_ || !ps_fp16_to_nv12_uv_ || !ctx_ || + (frame_w & 1) != 0 || (frame_h & 1) != 0) + return false; + + UINT stride = sizeof(float) * 4, offset = 0; + ID3D11Buffer *vertexBuffer = vb_.Get(); + ctx_->IASetVertexBuffers(0, 1, &vertexBuffer, &stride, &offset); + ctx_->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); + ctx_->IASetInputLayout(il_.Get()); + ctx_->VSSetShader(vs_.Get(), nullptr, 0); + ID3D11ShaderResourceView *srv = srv_scene_fp16_.Get(); + ctx_->PSSetShaderResources(0, 1, &srv); + ID3D11SamplerState *sampler = samp_.Get(); + ctx_->PSSetSamplers(0, 1, &sampler); + + D3D11_VIEWPORT viewport{}; + viewport.Width = static_cast(frame_w); + viewport.Height = static_cast(frame_h); + viewport.MaxDepth = 1.0f; + ID3D11RenderTargetView *target = rtv_nv12_y_.Get(); + ctx_->OMSetRenderTargets(1, &target, nullptr); + ctx_->RSSetViewports(1, &viewport); + ctx_->PSSetShader(ps_fp16_to_nv12_y_.Get(), nullptr, 0); + ctx_->Draw(6, 0); + + viewport.Width = static_cast(frame_w / 2); + viewport.Height = static_cast(frame_h / 2); + target = rtv_nv12_uv_.Get(); + ctx_->OMSetRenderTargets(1, &target, nullptr); + ctx_->RSSetViewports(1, &viewport); + ctx_->PSSetShader(ps_fp16_to_nv12_uv_.Get(), nullptr, 0); + ctx_->Draw(6, 0); + + ID3D11ShaderResourceView *nullSrv = nullptr; + ctx_->PSSetShaderResources(0, 1, &nullSrv); + return true; +} + bool D3DPreviewPipeline::readback_to_buffer(void *destination, uint64_t destination_size, int destination_row_bytes, @@ -702,6 +803,68 @@ bool D3DPreviewPipeline::readback_to_buffer(void *destination, return true; } +bool D3DPreviewPipeline::readback_nv12_to_buffer(void *destination, + uint64_t destination_size, + int destination_row_bytes, + int frame_w, + int frame_h) +{ + if (!d3d_ || !ctx_ || !destination || !rt_nv12_y_ || !rt_nv12_uv_ || + frame_w <= 0 || frame_h <= 0 || (frame_w & 1) != 0 || (frame_h & 1) != 0 || + destination_row_bytes < frame_w) + return false; + const uint64_t rows = static_cast(frame_h) + static_cast(frame_h / 2); + if (destination_size < static_cast(destination_row_bytes) * rows) + return false; + + auto ensureStaging = [this](ComPtr &texture, UINT width, UINT height, + DXGI_FORMAT format) { + if (texture) + { + D3D11_TEXTURE2D_DESC current{}; + texture->GetDesc(¤t); + if (current.Width == width && current.Height == height && current.Format == format) + return true; + texture.Reset(); + } + D3D11_TEXTURE2D_DESC desc{}; + desc.Width = width; + desc.Height = height; + desc.MipLevels = 1; + desc.ArraySize = 1; + desc.Format = format; + desc.SampleDesc.Count = 1; + desc.Usage = D3D11_USAGE_STAGING; + desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ; + return SUCCEEDED(d3d_->CreateTexture2D(&desc, nullptr, &texture)); + }; + if (!ensureStaging(readback_nv12_y_, static_cast(frame_w), static_cast(frame_h), + DXGI_FORMAT_R8_UNORM) || + !ensureStaging(readback_nv12_uv_, static_cast(frame_w / 2), static_cast(frame_h / 2), + DXGI_FORMAT_R8G8_UNORM)) + return false; + + auto *dst = static_cast(destination); + auto copyPlane = [this, dst, destination_row_bytes](ID3D11Texture2D *source, + ID3D11Texture2D *staging, + int rows, int copyBytes, + size_t destinationOffset) { + ctx_->CopyResource(staging, source); + D3D11_MAPPED_SUBRESOURCE mapped{}; + if (FAILED(ctx_->Map(staging, 0, D3D11_MAP_READ, 0, &mapped))) + return false; + for (int row = 0; row < rows; ++row) + std::memcpy(dst + destinationOffset + static_cast(row) * destination_row_bytes, + static_cast(mapped.pData) + static_cast(row) * mapped.RowPitch, + static_cast(copyBytes)); + ctx_->Unmap(staging, 0); + return true; + }; + return copyPlane(rt_nv12_y_.Get(), readback_nv12_y_.Get(), frame_h, frame_w, 0) && + copyPlane(rt_nv12_uv_.Get(), readback_nv12_uv_.Get(), frame_h / 2, frame_w, + static_cast(destination_row_bytes) * static_cast(frame_h)); +} + bool D3DPreviewPipeline::upload_packed_422_frame(const uint8_t *data, int src_stride, int frame_w, int frame_h) { if (!d3d_ || !ctx_ || !data || frame_w <= 0 || frame_h <= 0 || src_stride <= 0) diff --git a/sdk/gvfg/src/gpu/d3d_conversion_pipeline.h b/sdk/gvfg/src/gpu/d3d_conversion_pipeline.h index 74d68cc..1246351 100644 --- a/sdk/gvfg/src/gpu/d3d_conversion_pipeline.h +++ b/sdk/gvfg/src/gpu/d3d_conversion_pipeline.h @@ -58,6 +58,7 @@ public: 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); @@ -68,6 +69,11 @@ public: 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); ID3D11Device *d3d_ = nullptr; ID3D11DeviceContext *ctx_ = nullptr; @@ -88,6 +94,12 @@ public: Microsoft::WRL::ComPtr rtv_rgb10_; Microsoft::WRL::ComPtr readback_bgra8_; Microsoft::WRL::ComPtr readback_rgb10_; + Microsoft::WRL::ComPtr rt_nv12_y_; + Microsoft::WRL::ComPtr rtv_nv12_y_; + Microsoft::WRL::ComPtr rt_nv12_uv_; + Microsoft::WRL::ComPtr rtv_nv12_uv_; + Microsoft::WRL::ComPtr readback_nv12_y_; + Microsoft::WRL::ComPtr readback_nv12_uv_; Microsoft::WRL::ComPtr upload_yuy2_packed_; Microsoft::WRL::ComPtr upload_y210_packed_; Microsoft::WRL::ComPtr rtv_rgba_; @@ -98,6 +110,8 @@ public: Microsoft::WRL::ComPtr ps_yuy2_; Microsoft::WRL::ComPtr ps_y210_; Microsoft::WRL::ComPtr ps_fp16_to_rgba8_; + Microsoft::WRL::ComPtr ps_fp16_to_nv12_y_; + Microsoft::WRL::ComPtr ps_fp16_to_nv12_uv_; Microsoft::WRL::ComPtr ps_fp16_to_preview_; Microsoft::WRL::ComPtr il_; Microsoft::WRL::ComPtr vb_; diff --git a/sdk/gvfg/src/gpu/gvfg_gpu_convert.cpp b/sdk/gvfg/src/gpu/gvfg_gpu_convert.cpp index 67feb81..bf081e9 100644 --- a/sdk/gvfg/src/gpu/gvfg_gpu_convert.cpp +++ b/sdk/gvfg/src/gpu/gvfg_gpu_convert.cpp @@ -80,6 +80,16 @@ public: outputFormat = DXGI_FORMAT_R10G10B10A2_UNORM; rendered = pipeline_->blit_fp16_to_rgb10a2(source.width, source.height); 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; } @@ -197,18 +207,75 @@ extern "C" GVFG_API gvfg_status_t gvfg_gpu_convert_to_buffer( source->pixel_format != GVFG_PIXFMT_Y210) return GVFG_ENOTSUP; if (output->pixel_format != GVFG_GPU_OUTPUT_BGRA8 && - output->pixel_format != GVFG_GPU_OUTPUT_RGB10A2) + output->pixel_format != GVFG_GPU_OUTPUT_RGB10A2 && + output->pixel_format != GVFG_GPU_OUTPUT_NV12) return GVFG_ENOTSUP; if (!checkedFrameLayout(*source)) return GVFG_EINVAL; - const uint64_t minimumRowBytes = static_cast(source->width) * 4u; + if (output->pixel_format == GVFG_GPU_OUTPUT_NV12 && + ((source->width & 1) != 0 || (source->height & 1) != 0)) + return GVFG_EINVAL; + + const uint64_t minimumRowBytes = output->pixel_format == GVFG_GPU_OUTPUT_NV12 + ? static_cast(source->width) + : static_cast(source->width) * 4u; if (static_cast(output->row_bytes) < minimumRowBytes) return GVFG_EINVAL; - const uint64_t requiredSize = static_cast(output->row_bytes) * - static_cast(source->height); + const uint64_t outputRows = output->pixel_format == GVFG_GPU_OUTPUT_NV12 + ? static_cast(source->height) + + static_cast(source->height / 2) + : static_cast(source->height); + const uint64_t requiredSize = static_cast(output->row_bytes) * outputRows; if (output->data_size < requiredSize) return GVFG_EINVAL; return sharedConverter().convert(*source, *output); } + +namespace +{ +gvfg_status_t convertToFormat(const gvfg_frame_t *source, + void *destination, + uint64_t destinationSize, + int rowBytes, + gvfg_gpu_output_format_t format) +{ + gvfg_gpu_output_buffer_t output{}; + output.data = destination; + output.data_size = destinationSize; + output.row_bytes = rowBytes; + output.pixel_format = static_cast(format); + return gvfg_gpu_convert_to_buffer(source, &output); +} +} + +extern "C" GVFG_API gvfg_status_t gvfg_gpu_convert_to_bgra8( + const gvfg_frame_t *source, + void *destination, + uint64_t destination_size, + int row_bytes) +{ + return convertToFormat(source, destination, destination_size, row_bytes, + GVFG_GPU_OUTPUT_BGRA8); +} + +extern "C" GVFG_API gvfg_status_t gvfg_gpu_convert_to_rgb10a2( + const gvfg_frame_t *source, + void *destination, + uint64_t destination_size, + int row_bytes) +{ + return convertToFormat(source, destination, destination_size, row_bytes, + GVFG_GPU_OUTPUT_RGB10A2); +} + +extern "C" GVFG_API gvfg_status_t gvfg_gpu_convert_to_nv12( + const gvfg_frame_t *source, + void *destination, + uint64_t destination_size, + int row_bytes) +{ + return convertToFormat(source, destination, destination_size, row_bytes, + GVFG_GPU_OUTPUT_NV12); +}