Skip to content

Commit 0f24ad8

Browse files
committed
feat(linux): improve vulkan encoder, add rgb2nv12 shader
1 parent e76b2ff commit 0f24ad8

File tree

5 files changed

+782
-142
lines changed

5 files changed

+782
-142
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#version 450
2+
3+
layout(local_size_x = 16, local_size_y = 16) in;
4+
5+
layout(set = 0, binding = 0) uniform sampler2D rgb_in;
6+
layout(set = 0, binding = 1, r8) uniform writeonly image2D y_out;
7+
layout(set = 0, binding = 2, rg8) uniform writeonly image2D uv_out;
8+
9+
layout(push_constant) uniform PushConstants {
10+
vec4 color_vec_y;
11+
vec4 color_vec_u;
12+
vec4 color_vec_v;
13+
vec2 range_y;
14+
vec2 range_uv;
15+
ivec2 src_offset;
16+
ivec2 src_size;
17+
ivec2 dst_size;
18+
} pc;
19+
20+
void main() {
21+
ivec2 pos = ivec2(gl_GlobalInvocationID.xy);
22+
if (pos.x >= pc.dst_size.x || pos.y >= pc.dst_size.y)
23+
return;
24+
25+
// Precompute scaling factors
26+
vec2 inv_tex = 1.0 / vec2(textureSize(rgb_in, 0));
27+
vec2 scale = vec2(pc.src_size) / vec2(pc.dst_size);
28+
29+
// Map output pixel to input texture coordinate
30+
vec2 uv = (vec2(pc.src_offset) + (vec2(pos) + 0.5) * scale) * inv_tex;
31+
vec3 rgb = texture(rgb_in, uv).rgb;
32+
33+
// Y plane
34+
float y = dot(pc.color_vec_y.xyz, rgb) + pc.color_vec_y.w;
35+
imageStore(y_out, pos, vec4(y * pc.range_y.x + pc.range_y.y, 0, 0, 0));
36+
37+
// UV plane (half resolution, one thread per 2x2 block)
38+
if ((pos.x & 1) == 0 && (pos.y & 1) == 0) {
39+
vec2 step = scale * inv_tex;
40+
41+
// Average 2x2 block for chroma subsampling (reuse rgb as c00)
42+
vec3 avg = rgb
43+
+ texture(rgb_in, uv + vec2(step.x, 0)).rgb
44+
+ texture(rgb_in, uv + vec2(0, step.y)).rgb
45+
+ texture(rgb_in, uv + step).rgb;
46+
avg *= 0.25;
47+
48+
float cb = dot(pc.color_vec_u.xyz, avg) + pc.color_vec_u.w;
49+
float cr = dot(pc.color_vec_v.xyz, avg) + pc.color_vec_v.w;
50+
51+
imageStore(uv_out, pos >> 1, vec4(cb * pc.range_uv.x + pc.range_uv.y,
52+
cr * pc.range_uv.x + pc.range_uv.y, 0, 0));
53+
}
54+
}
3.71 KB
Binary file not shown.
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/**
2+
* @file src/platform/linux/shaders/rgb2nv12.spv.h
3+
* @brief Pre-compiled SPIR-V for RGB→NV12 compute shader.
4+
* @note Regenerate: glslc -O rgb2nv12.comp -o rgb2nv12.spv, then convert with xxd or python.
5+
*/
6+
#pragma once
7+
#include <cstdint>
8+
9+
static const uint32_t rgb2nv12_comp_spv[] = {
10+
0x07230203, 0x00010000, 0x000d000b, 0x000000e4, 0x00000000, 0x00020011, 0x00000001, 0x00020011,
11+
0x00000031, 0x00020011, 0x00000032, 0x0006000b, 0x00000001, 0x4c534c47, 0x6474732e, 0x3035342e,
12+
0x00000000, 0x0003000e, 0x00000000, 0x00000001, 0x0006000f, 0x00000005, 0x00000004, 0x6e69616d,
13+
0x00000000, 0x0000000d, 0x00060010, 0x00000004, 0x00000011, 0x00000010, 0x00000010, 0x00000001,
14+
0x00040047, 0x0000000d, 0x0000000b, 0x0000001c, 0x00030047, 0x0000001a, 0x00000002, 0x00050048,
15+
0x0000001a, 0x00000000, 0x00000023, 0x00000000, 0x00050048, 0x0000001a, 0x00000001, 0x00000023,
16+
0x00000010, 0x00050048, 0x0000001a, 0x00000002, 0x00000023, 0x00000020, 0x00050048, 0x0000001a,
17+
0x00000003, 0x00000023, 0x00000030, 0x00050048, 0x0000001a, 0x00000004, 0x00000023, 0x00000038,
18+
0x00050048, 0x0000001a, 0x00000005, 0x00000023, 0x00000040, 0x00050048, 0x0000001a, 0x00000006,
19+
0x00000023, 0x00000048, 0x00050048, 0x0000001a, 0x00000007, 0x00000023, 0x00000050, 0x00040047,
20+
0x00000035, 0x00000021, 0x00000000, 0x00040047, 0x00000035, 0x00000022, 0x00000000, 0x00030047,
21+
0x0000006d, 0x00000019, 0x00040047, 0x0000006d, 0x00000021, 0x00000001, 0x00040047, 0x0000006d,
22+
0x00000022, 0x00000000, 0x00030047, 0x000000be, 0x00000019, 0x00040047, 0x000000be, 0x00000021,
23+
0x00000002, 0x00040047, 0x000000be, 0x00000022, 0x00000000, 0x00040047, 0x000000d4, 0x0000000b,
24+
0x00000019, 0x00020013, 0x00000002, 0x00030021, 0x00000003, 0x00000002, 0x00040015, 0x00000006,
25+
0x00000020, 0x00000001, 0x00040017, 0x00000007, 0x00000006, 0x00000002, 0x00040015, 0x0000000a,
26+
0x00000020, 0x00000000, 0x00040017, 0x0000000b, 0x0000000a, 0x00000003, 0x00040020, 0x0000000c,
27+
0x00000001, 0x0000000b, 0x0004003b, 0x0000000c, 0x0000000d, 0x00000001, 0x00040017, 0x0000000e,
28+
0x0000000a, 0x00000002, 0x00020014, 0x00000012, 0x0004002b, 0x0000000a, 0x00000013, 0x00000000,
29+
0x00030016, 0x00000017, 0x00000020, 0x00040017, 0x00000018, 0x00000017, 0x00000004, 0x00040017,
30+
0x00000019, 0x00000017, 0x00000002, 0x000a001e, 0x0000001a, 0x00000018, 0x00000018, 0x00000018,
31+
0x00000019, 0x00000019, 0x00000007, 0x00000007, 0x00000007, 0x00040020, 0x0000001b, 0x00000009,
32+
0x0000001a, 0x0004003b, 0x0000001b, 0x0000001c, 0x00000009, 0x0004002b, 0x00000006, 0x0000001d,
33+
0x00000007, 0x00040020, 0x0000001e, 0x00000009, 0x00000006, 0x0004002b, 0x0000000a, 0x00000025,
34+
0x00000001, 0x0004002b, 0x00000017, 0x00000031, 0x3f800000, 0x00090019, 0x00000032, 0x00000017,
35+
0x00000001, 0x00000000, 0x00000000, 0x00000000, 0x00000001, 0x00000000, 0x0003001b, 0x00000033,
36+
0x00000032, 0x00040020, 0x00000034, 0x00000000, 0x00000033, 0x0004003b, 0x00000034, 0x00000035,
37+
0x00000000, 0x0004002b, 0x00000006, 0x00000037, 0x00000000, 0x0004002b, 0x00000006, 0x0000003e,
38+
0x00000006, 0x00040020, 0x0000003f, 0x00000009, 0x00000007, 0x0004002b, 0x00000006, 0x00000048,
39+
0x00000005, 0x0004002b, 0x00000017, 0x0000004e, 0x3f000000, 0x00040017, 0x00000056, 0x00000017,
40+
0x00000003, 0x0004002b, 0x00000017, 0x0000005b, 0x00000000, 0x00040020, 0x00000060, 0x00000009,
41+
0x00000018, 0x0004002b, 0x0000000a, 0x00000066, 0x00000003, 0x00040020, 0x00000067, 0x00000009,
42+
0x00000017, 0x00090019, 0x0000006b, 0x00000017, 0x00000001, 0x00000000, 0x00000000, 0x00000000,
43+
0x00000002, 0x0000000f, 0x00040020, 0x0000006c, 0x00000000, 0x0000006b, 0x0004003b, 0x0000006c,
44+
0x0000006d, 0x00000000, 0x0004002b, 0x00000006, 0x00000071, 0x00000003, 0x0004002b, 0x00000006,
45+
0x0000007b, 0x00000001, 0x0004002b, 0x00000017, 0x000000a6, 0x3e800000, 0x0004002b, 0x00000006,
46+
0x000000b3, 0x00000002, 0x00090019, 0x000000bc, 0x00000017, 0x00000001, 0x00000000, 0x00000000,
47+
0x00000000, 0x00000002, 0x0000000d, 0x00040020, 0x000000bd, 0x00000000, 0x000000bc, 0x0004003b,
48+
0x000000bd, 0x000000be, 0x00000000, 0x0004002b, 0x00000006, 0x000000c4, 0x00000004, 0x0004002b,
49+
0x0000000a, 0x000000d3, 0x00000010, 0x0006002c, 0x0000000b, 0x000000d4, 0x000000d3, 0x000000d3,
50+
0x00000025, 0x0005002c, 0x00000019, 0x000000e1, 0x00000031, 0x00000031, 0x0005002c, 0x00000019,
51+
0x000000e2, 0x0000004e, 0x0000004e, 0x0005002c, 0x00000007, 0x000000e3, 0x0000007b, 0x0000007b,
52+
0x00050036, 0x00000002, 0x00000004, 0x00000000, 0x00000003, 0x000200f8, 0x00000005, 0x000300f7,
53+
0x000000d5, 0x00000000, 0x000300fb, 0x00000013, 0x000000d6, 0x000200f8, 0x000000d6, 0x0004003d,
54+
0x0000000b, 0x0000000f, 0x0000000d, 0x0007004f, 0x0000000e, 0x00000010, 0x0000000f, 0x0000000f,
55+
0x00000000, 0x00000001, 0x0004007c, 0x00000007, 0x00000011, 0x00000010, 0x00050051, 0x00000006,
56+
0x00000016, 0x00000011, 0x00000000, 0x00060041, 0x0000001e, 0x0000001f, 0x0000001c, 0x0000001d,
57+
0x00000013, 0x0004003d, 0x00000006, 0x00000020, 0x0000001f, 0x000500af, 0x00000012, 0x00000021,
58+
0x00000016, 0x00000020, 0x000400a8, 0x00000012, 0x00000022, 0x00000021, 0x000300f7, 0x00000024,
59+
0x00000000, 0x000400fa, 0x00000022, 0x00000023, 0x00000024, 0x000200f8, 0x00000023, 0x00050051,
60+
0x00000006, 0x00000027, 0x00000011, 0x00000001, 0x00060041, 0x0000001e, 0x00000028, 0x0000001c,
61+
0x0000001d, 0x00000025, 0x0004003d, 0x00000006, 0x00000029, 0x00000028, 0x000500af, 0x00000012,
62+
0x0000002a, 0x00000027, 0x00000029, 0x000200f9, 0x00000024, 0x000200f8, 0x00000024, 0x000700f5,
63+
0x00000012, 0x0000002b, 0x00000021, 0x000000d6, 0x0000002a, 0x00000023, 0x000300f7, 0x0000002d,
64+
0x00000000, 0x000400fa, 0x0000002b, 0x0000002c, 0x0000002d, 0x000200f8, 0x0000002c, 0x000200f9,
65+
0x000000d5, 0x000200f8, 0x0000002d, 0x0004003d, 0x00000033, 0x00000036, 0x00000035, 0x00040064,
66+
0x00000032, 0x00000038, 0x00000036, 0x00050067, 0x00000007, 0x00000039, 0x00000038, 0x00000037,
67+
0x0004006f, 0x00000019, 0x0000003a, 0x00000039, 0x00050088, 0x00000019, 0x0000003c, 0x000000e1,
68+
0x0000003a, 0x00050041, 0x0000003f, 0x00000040, 0x0000001c, 0x0000003e, 0x0004003d, 0x00000007,
69+
0x00000041, 0x00000040, 0x0004006f, 0x00000019, 0x00000042, 0x00000041, 0x00050041, 0x0000003f,
70+
0x00000043, 0x0000001c, 0x0000001d, 0x0004003d, 0x00000007, 0x00000044, 0x00000043, 0x0004006f,
71+
0x00000019, 0x00000045, 0x00000044, 0x00050088, 0x00000019, 0x00000046, 0x00000042, 0x00000045,
72+
0x00050041, 0x0000003f, 0x00000049, 0x0000001c, 0x00000048, 0x0004003d, 0x00000007, 0x0000004a,
73+
0x00000049, 0x0004006f, 0x00000019, 0x0000004b, 0x0000004a, 0x0004006f, 0x00000019, 0x0000004d,
74+
0x00000011, 0x00050081, 0x00000019, 0x00000050, 0x0000004d, 0x000000e2, 0x00050085, 0x00000019,
75+
0x00000052, 0x00000050, 0x00000046, 0x00050081, 0x00000019, 0x00000053, 0x0000004b, 0x00000052,
76+
0x00050085, 0x00000019, 0x00000055, 0x00000053, 0x0000003c, 0x0004003d, 0x00000033, 0x00000059,
77+
0x00000035, 0x00070058, 0x00000018, 0x0000005c, 0x00000059, 0x00000055, 0x00000002, 0x0000005b,
78+
0x0008004f, 0x00000056, 0x0000005d, 0x0000005c, 0x0000005c, 0x00000000, 0x00000001, 0x00000002,
79+
0x00050041, 0x00000060, 0x00000061, 0x0000001c, 0x00000037, 0x0004003d, 0x00000018, 0x00000062,
80+
0x00000061, 0x0008004f, 0x00000056, 0x00000063, 0x00000062, 0x00000062, 0x00000000, 0x00000001,
81+
0x00000002, 0x00050094, 0x00000017, 0x00000065, 0x00000063, 0x0000005d, 0x00060041, 0x00000067,
82+
0x00000068, 0x0000001c, 0x00000037, 0x00000066, 0x0004003d, 0x00000017, 0x00000069, 0x00000068,
83+
0x00050081, 0x00000017, 0x0000006a, 0x00000065, 0x00000069, 0x0004003d, 0x0000006b, 0x0000006e,
84+
0x0000006d, 0x00060041, 0x00000067, 0x00000072, 0x0000001c, 0x00000071, 0x00000013, 0x0004003d,
85+
0x00000017, 0x00000073, 0x00000072, 0x00050085, 0x00000017, 0x00000074, 0x0000006a, 0x00000073,
86+
0x00060041, 0x00000067, 0x00000075, 0x0000001c, 0x00000071, 0x00000025, 0x0004003d, 0x00000017,
87+
0x00000076, 0x00000075, 0x00050081, 0x00000017, 0x00000077, 0x00000074, 0x00000076, 0x00070050,
88+
0x00000018, 0x00000078, 0x00000077, 0x0000005b, 0x0000005b, 0x0000005b, 0x00040063, 0x0000006e,
89+
0x00000011, 0x00000078, 0x000500c7, 0x00000006, 0x0000007c, 0x00000016, 0x0000007b, 0x000500aa,
90+
0x00000012, 0x0000007d, 0x0000007c, 0x00000037, 0x000300f7, 0x0000007f, 0x00000000, 0x000400fa,
91+
0x0000007d, 0x0000007e, 0x0000007f, 0x000200f8, 0x0000007e, 0x00050051, 0x00000006, 0x00000081,
92+
0x00000011, 0x00000001, 0x000500c7, 0x00000006, 0x00000082, 0x00000081, 0x0000007b, 0x000500aa,
93+
0x00000012, 0x00000083, 0x00000082, 0x00000037, 0x000200f9, 0x0000007f, 0x000200f8, 0x0000007f,
94+
0x000700f5, 0x00000012, 0x00000084, 0x0000007d, 0x0000002d, 0x00000083, 0x0000007e, 0x000300f7,
95+
0x00000086, 0x00000000, 0x000400fa, 0x00000084, 0x00000085, 0x00000086, 0x000200f8, 0x00000085,
96+
0x00050085, 0x00000019, 0x0000008a, 0x00000046, 0x0000003c, 0x0004003d, 0x00000033, 0x0000008d,
97+
0x00000035, 0x00050051, 0x00000017, 0x00000090, 0x0000008a, 0x00000000, 0x00050050, 0x00000019,
98+
0x00000091, 0x00000090, 0x0000005b, 0x00050081, 0x00000019, 0x00000092, 0x00000055, 0x00000091,
99+
0x00070058, 0x00000018, 0x00000093, 0x0000008d, 0x00000092, 0x00000002, 0x0000005b, 0x0008004f,
100+
0x00000056, 0x00000094, 0x00000093, 0x00000093, 0x00000000, 0x00000001, 0x00000002, 0x00050081,
101+
0x00000056, 0x00000095, 0x0000005d, 0x00000094, 0x0004003d, 0x00000033, 0x00000096, 0x00000035,
102+
0x00050051, 0x00000017, 0x00000099, 0x0000008a, 0x00000001, 0x00050050, 0x00000019, 0x0000009a,
103+
0x0000005b, 0x00000099, 0x00050081, 0x00000019, 0x0000009b, 0x00000055, 0x0000009a, 0x00070058,
104+
0x00000018, 0x0000009c, 0x00000096, 0x0000009b, 0x00000002, 0x0000005b, 0x0008004f, 0x00000056,
105+
0x0000009d, 0x0000009c, 0x0000009c, 0x00000000, 0x00000001, 0x00000002, 0x00050081, 0x00000056,
106+
0x0000009e, 0x00000095, 0x0000009d, 0x0004003d, 0x00000033, 0x0000009f, 0x00000035, 0x00050081,
107+
0x00000019, 0x000000a2, 0x00000055, 0x0000008a, 0x00070058, 0x00000018, 0x000000a3, 0x0000009f,
108+
0x000000a2, 0x00000002, 0x0000005b, 0x0008004f, 0x00000056, 0x000000a4, 0x000000a3, 0x000000a3,
109+
0x00000000, 0x00000001, 0x00000002, 0x00050081, 0x00000056, 0x000000a5, 0x0000009e, 0x000000a4,
110+
0x0005008e, 0x00000056, 0x000000a8, 0x000000a5, 0x000000a6, 0x00050041, 0x00000060, 0x000000aa,
111+
0x0000001c, 0x0000007b, 0x0004003d, 0x00000018, 0x000000ab, 0x000000aa, 0x0008004f, 0x00000056,
112+
0x000000ac, 0x000000ab, 0x000000ab, 0x00000000, 0x00000001, 0x00000002, 0x00050094, 0x00000017,
113+
0x000000ae, 0x000000ac, 0x000000a8, 0x00060041, 0x00000067, 0x000000af, 0x0000001c, 0x0000007b,
114+
0x00000066, 0x0004003d, 0x00000017, 0x000000b0, 0x000000af, 0x00050081, 0x00000017, 0x000000b1,
115+
0x000000ae, 0x000000b0, 0x00050041, 0x00000060, 0x000000b4, 0x0000001c, 0x000000b3, 0x0004003d,
116+
0x00000018, 0x000000b5, 0x000000b4, 0x0008004f, 0x00000056, 0x000000b6, 0x000000b5, 0x000000b5,
117+
0x00000000, 0x00000001, 0x00000002, 0x00050094, 0x00000017, 0x000000b8, 0x000000b6, 0x000000a8,
118+
0x00060041, 0x00000067, 0x000000b9, 0x0000001c, 0x000000b3, 0x00000066, 0x0004003d, 0x00000017,
119+
0x000000ba, 0x000000b9, 0x00050081, 0x00000017, 0x000000bb, 0x000000b8, 0x000000ba, 0x0004003d,
120+
0x000000bc, 0x000000bf, 0x000000be, 0x000500c3, 0x00000007, 0x000000c2, 0x00000011, 0x000000e3,
121+
0x00060041, 0x00000067, 0x000000c5, 0x0000001c, 0x000000c4, 0x00000013, 0x0004003d, 0x00000017,
122+
0x000000c6, 0x000000c5, 0x00050085, 0x00000017, 0x000000c7, 0x000000b1, 0x000000c6, 0x00060041,
123+
0x00000067, 0x000000c8, 0x0000001c, 0x000000c4, 0x00000025, 0x0004003d, 0x00000017, 0x000000c9,
124+
0x000000c8, 0x00050081, 0x00000017, 0x000000ca, 0x000000c7, 0x000000c9, 0x00050085, 0x00000017,
125+
0x000000ce, 0x000000bb, 0x000000c6, 0x00050081, 0x00000017, 0x000000d1, 0x000000ce, 0x000000c9,
126+
0x00070050, 0x00000018, 0x000000d2, 0x000000ca, 0x000000d1, 0x0000005b, 0x0000005b, 0x00040063,
127+
0x000000bf, 0x000000c2, 0x000000d2, 0x000200f9, 0x00000086, 0x000200f8, 0x00000086, 0x000200f9,
128+
0x000000d5, 0x000200f8, 0x000000d5, 0x000100fd, 0x00010038,
129+
};
130+
131+
static const size_t rgb2nv12_comp_spv_size = sizeof(rgb2nv12_comp_spv);

0 commit comments

Comments
 (0)