Skip to content

Commit 5ce9165

Browse files
committed
Added support for multi-plane rendering (YUV)
- kmscap_ctx_t: Added texture_uv (UV plane), nv12_effect (compiled shader), and yuv_color_space (BT.709 / BT.601 setting). - kmscap_build_color_matrix(): New helper that computes the correct limited-range YUV→RGB 4×4 matrix for either BT.709 or BT.601 at runtime. - kmscap_destroy_texture(): Now destroys texture_uv alongside the Y texture in a single obs_enter_graphics() block. - kmscap_import_fb(): NV12/NV21 now take a separate code path — Y is imported as GS_R8 (full res) and UV as GS_R8G8 (half res), each as a single-plane DMA-BUF call. - kmscap_video_render(): When texture_uv is set, dispatches through the nv12_effect shader instead of OBS_EFFECT_DEFAULT; then overlays the cursor with the default effect on top. - kmscap_create/destroy: Loads/destroys the NV12 effect file via obs_module_file(). - kmscap_update/get_defaults/get_properties: Adds the YUV Color Space dropdown (BT.709 default, BT.601 option).
1 parent 1e4c82e commit 5ce9165

4 files changed

Lines changed: 304 additions & 31 deletions

File tree

CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ if(OS_LINUX)
5555
src/kmscap-source.c
5656
)
5757

58+
# Install the NV12 conversion effect alongside the plugin data
59+
set_target_properties(${CMAKE_PROJECT_NAME} PROPERTIES
60+
OBS_MODULE_INSTALL_DATA "${CMAKE_CURRENT_SOURCE_DIR}/data"
61+
)
62+
5863
# Build the privileged helper daemon
5964
add_executable(obs-kmscap-helper src/kmscap-helper.c src/kms-utils.c src/kms-ipc.c)
6065
target_link_libraries(obs-kmscap-helper PRIVATE PkgConfig::Libdrm)

data/effects/kmscap-nv12.effect

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* obs-kmscap - NV12 YUV-to-RGB conversion effect
3+
*
4+
* Converts a two-plane NV12 DMA-BUF into RGB for display.
5+
* image – Y plane (GS_R8, width x height)
6+
* image_uv – UV plane (GS_R8G8, width/2 x height/2)
7+
*
8+
* The color_matrix uniform is a row-major 4x4 that maps [Y, Cb, Cr, 1] to
9+
* [R, G, B, 1]. Pre-built matrices for BT.601 and BT.709 are set from C.
10+
*
11+
* OBS shader dialect: HLSL-like (compiled via libobs cross-compiler).
12+
*/
13+
14+
uniform float4x4 color_matrix;
15+
16+
uniform texture2d image; /* Y plane */
17+
uniform texture2d image_uv; /* UV plane */
18+
19+
sampler_state texSampler {
20+
Filter = Linear;
21+
AddressU = Clamp;
22+
AddressV = Clamp;
23+
};
24+
25+
struct VertData {
26+
float4 pos : POSITION;
27+
float2 uv : TEXCOORD0;
28+
};
29+
30+
VertData VSDefault(VertData v_in)
31+
{
32+
VertData v_out;
33+
v_out.pos = mul(float4(v_in.pos.xyz, 1.0), ViewProj);
34+
v_out.uv = v_in.uv;
35+
return v_out;
36+
}
37+
38+
float4 PSConvertNV12(VertData v_in) : TARGET
39+
{
40+
float y = image.Sample(texSampler, v_in.uv).r;
41+
float2 uv = image_uv.Sample(texSampler, v_in.uv).rg;
42+
43+
/* color_matrix maps [Y, Cb, Cr, 1] → [R, G, B, A] */
44+
float4 yuv1 = float4(y, uv.x, uv.y, 1.0);
45+
float4 rgba = mul(color_matrix, yuv1);
46+
return float4(clamp(rgba.rgb, 0.0, 1.0), 1.0);
47+
}
48+
49+
technique Draw
50+
{
51+
pass
52+
{
53+
vertex_shader = VSDefault(v_in);
54+
pixel_shader = PSConvertNV12(v_in);
55+
}
56+
}

data/locale/en-US.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@ KMSCapture.DRICard="DRI Card"
33
KMSCapture.CRTC="Display Output (CRTC)"
44
KMSCapture.NoCRTC="No active display found"
55
KMSCapture.CaptureCursor="Capture Cursor"
6+
KMSCapture.ColorSpace="YUV Color Space (NV12)"
7+
KMSCapture.ColorSpace.BT709="BT.709 (HD, default)"
8+
KMSCapture.ColorSpace.BT601="BT.601 (SD/legacy)"

0 commit comments

Comments
 (0)