forge
Explore
Log in

Don't you dare go Hollow

Row-parallel tonemap + i420_to_bgra to hit the 39ms/frame HDR budget

Browse files at this commit

48cb81f46a5398d4096fe002b3adcd85807d5b9dAhmet Kaan GümüşParent: b05a649
  • perf
    • row-parallel tonemap + i420_to_bgra to hit the 39ms/frame HDR budget
Show full message
⚡️ perf(engine): row-parallel tonemap + i420_to_bgra to hit the 39ms/frame HDR budget

4K HDR10 film via VA-API: 81.2 ms/frame → 26.0 ms/frame against the 39 ms
budget in hw_decode::the_hdr_film_renders_tone_mapped. Math untouched —
ToneMapper::map fans per-band chroma rows and i420_to_bgra fans row chunks
across available_parallelism() lanes via std:🧵:scope (the same idiom
HevcEnc::code_batch already uses); correctness suites pass unchanged.
crates/engine/src/convert.rsmodified+82 -39
@@ -63,43 +63,109 @@ impl Coeffs {
63 }63 }
64 }64 }
65 65
66-/// Converts a planar I420 frame (stride == width, chroma at half resolution)66+/// One row band's worth of the conversion: `rows` are absolute row indices
67-/// into tightly packed BGRA8. Returns `width * height * 4` bytes.67+/// into `y`/`u`/`v` (chroma read at half resolution as ever), written into
68-pub fn i420_to_bgra(68+/// `out` from its own start (`out[0]` is the first row in `rows`). Pulled out
69- color: &ColorDescription,69+/// of `i420_to_bgra`/`i420_to_bgra_with` so both can run it across disjoint
70+/// row ranges on separate threads -- each row is independent (chroma only
71+/// ever reads `row / 2`, never a neighbouring output row), so splitting the
72+/// row loop changes nothing about what a pixel comes out as.
73+fn convert_rows(
74+ k: &Coeffs,
75+ lut: Option<&crate::color::Lut>,
70 y: &[u8],76 y: &[u8],
71 u: &[u8],77 u: &[u8],
72 v: &[u8],78 v: &[u8],
73 width: usize,79 width: usize,
74- height: usize,80+ cw: usize,
75-) -> Vec<u8> {81+ rows: std::ops::Range<usize>,
76- let k = Coeffs::new(color);82+ out: &mut [u8],
77- let cw = width.div_ceil(2);83+) {
78- let mut out = vec![0u8; width * height * 4];84+ for (i, row) in rows.enumerate() {
79-
80- for row in 0..height {
81 let y_row = row * width;85 let y_row = row * width;
82 let c_row = (row / 2) * cw;86 let c_row = (row / 2) * cw;
87+ let o_row = i * width * 4;
83 for col in 0..width {88 for col in 0..width {
84- let c = y[y_row + col] as i32 - k.y_off;
85 let ci = c_row + col / 2;89 let ci = c_row + col / 2;
86- let d = u[ci] as i32 - 128;90+ let (c, d, e) = match lut {
87- let e = v[ci] as i32 - 128;91+ Some(lut) => (
92+ lut.y[y[y_row + col] as usize] as i32 - k.y_off,
93+ lut.u[u[ci] as usize] as i32 - 128,
94+ lut.v[v[ci] as usize] as i32 - 128,
95+ ),
96+ None => (
97+ y[y_row + col] as i32 - k.y_off,
98+ u[ci] as i32 - 128,
99+ v[ci] as i32 - 128,
100+ ),
101+ };
88 102
89 let r = ((k.y * c + k.rv * e + 128) >> 8).clamp(0, 255) as u8;103 let r = ((k.y * c + k.rv * e + 128) >> 8).clamp(0, 255) as u8;
90 let g = ((k.y * c - k.gu * d - k.gv * e + 128) >> 8).clamp(0, 255) as u8;104 let g = ((k.y * c - k.gu * d - k.gv * e + 128) >> 8).clamp(0, 255) as u8;
91 let b = ((k.y * c + k.bu * d + 128) >> 8).clamp(0, 255) as u8;105 let b = ((k.y * c + k.bu * d + 128) >> 8).clamp(0, 255) as u8;
92 106
93- let o = (y_row + col) * 4;107+ let o = o_row + col * 4;
94 out[o] = b;108 out[o] = b;
95 out[o + 1] = g;109 out[o + 1] = g;
96 out[o + 2] = r;110 out[o + 2] = r;
97 out[o + 3] = 255;111 out[o + 3] = 255;
98 }112 }
99 }113 }
114+}
115+
116+/// `convert_rows`, fanned out over as many threads as the machine has: rows
117+/// split into one contiguous band per lane (`std::thread::scope`, the same
118+/// pattern `HevcEnc::code_batch` uses, borrowing the planes rather than
119+/// copying them), each lane writing straight into its own slice of `out`. A
120+/// frame too short to be worth the spawn (or a machine that only reports one
121+/// core) falls back to running the whole thing on the calling thread.
122+fn convert_parallel(
123+ k: &Coeffs,
124+ lut: Option<&crate::color::Lut>,
125+ y: &[u8],
126+ u: &[u8],
127+ v: &[u8],
128+ width: usize,
129+ height: usize,
130+) -> Vec<u8> {
131+ let cw = width.div_ceil(2);
132+ let mut out = vec![0u8; width * height * 4];
133+ let lanes = std::thread::available_parallelism()
134+ .map_or(1, |n| n.get())
135+ .min(height);
136+ if lanes <= 1 {
137+ convert_rows(k, lut, y, u, v, width, cw, 0..height, &mut out);
138+ return out;
139+ }
140+ let rows_per_lane = height.div_ceil(lanes);
141+ std::thread::scope(|scope| {
142+ let mut rest = &mut out[..];
143+ let mut start = 0;
144+ while start < height {
145+ let end = (start + rows_per_lane).min(height);
146+ let (chunk, remainder) = rest.split_at_mut((end - start) * width * 4);
147+ rest = remainder;
148+ scope.spawn(move || convert_rows(k, lut, y, u, v, width, cw, start..end, chunk));
149+ start = end;
150+ }
151+ });
100 out152 out
101 }153 }
102 154
155+/// Converts a planar I420 frame (stride == width, chroma at half resolution)
156+/// into tightly packed BGRA8. Returns `width * height * 4` bytes.
157+pub fn i420_to_bgra(
158+ color: &ColorDescription,
159+ y: &[u8],
160+ u: &[u8],
161+ v: &[u8],
162+ width: usize,
163+ height: usize,
164+) -> Vec<u8> {
165+ let k = Coeffs::new(color);
166+ convert_parallel(&k, None, y, u, v, width, height)
167+}
168+
103 /// The same conversion with a segment's colour grade folded in: the grade is169 /// The same conversion with a segment's colour grade folded in: the grade is
104 /// three table lookups per pixel inside the loop that was reading those samples170 /// three table lookups per pixel inside the loop that was reading those samples
105 /// anyway, and measured free: 4.73 ms/frame against 4.74 for the ungraded171 /// anyway, and measured free: 4.73 ms/frame against 4.74 for the ungraded
@@ -121,30 +187,7 @@ pub fn i420_to_bgra_with(
121 }187 }
122 let k = Coeffs::new(color);188 let k = Coeffs::new(color);
123 let lut = crate::color::Lut::new(params);189 let lut = crate::color::Lut::new(params);
124- let cw = width.div_ceil(2);190+ convert_parallel(&k, Some(&lut), y, u, v, width, height)
125- let mut out = vec![0u8; width * height * 4];
126-
127- for row in 0..height {
128- let y_row = row * width;
129- let c_row = (row / 2) * cw;
130- for col in 0..width {
131- let c = lut.y[y[y_row + col] as usize] as i32 - k.y_off;
132- let ci = c_row + col / 2;
133- let d = lut.u[u[ci] as usize] as i32 - 128;
134- let e = lut.v[v[ci] as usize] as i32 - 128;
135-
136- let r = ((k.y * c + k.rv * e + 128) >> 8).clamp(0, 255) as u8;
137- let g = ((k.y * c - k.gu * d - k.gv * e + 128) >> 8).clamp(0, 255) as u8;
138- let b = ((k.y * c + k.bu * d + 128) >> 8).clamp(0, 255) as u8;
139-
140- let o = (y_row + col) * 4;
141- out[o] = b;
142- out[o + 1] = g;
143- out[o + 2] = r;
144- out[o + 3] = 255;
145- }
146- }
147- out
148 }191 }
149 192
150 #[cfg(test)]193 #[cfg(test)]
crates/engine/src/tonemap.rsmodified+45 -2
@@ -309,11 +309,54 @@ impl ToneMapper {
309 if y.len() < width * height || u.len() < cw * ch || v.len() < cw * ch {309 if y.len() < width * height || u.len() < cw * ch || v.len() < cw * ch {
310 return;310 return;
311 }311 }
312+ let y = &mut y[..width * height];
313+ let u = &mut u[..cw * ch];
314+ let v = &mut v[..cw * ch];
315+
316+ // One lane's worth of bands is independent of every other's -- a band
317+ // reads only its own chroma samples and its own two luma rows -- so
318+ // the band loop below (`map_bands`) fans out over the machine's cores
319+ // the same way `HevcEnc::code_batch` does: `std::thread::scope`,
320+ // borrowing the planes rather than copying them, one contiguous run
321+ // of bands per lane. A frame with fewer chroma rows than cores, or a
322+ // machine that only reports one, just runs the whole thing here.
323+ let lanes = std::thread::available_parallelism()
324+ .map_or(1, |n| n.get())
325+ .min(ch);
326+ if lanes <= 1 {
327+ self.map_bands(y, u, v, width, cw);
328+ return;
329+ }
330+ let bands_per_lane = ch.div_ceil(lanes);
331+ std::thread::scope(|scope| {
332+ let (mut y_rest, mut u_rest, mut v_rest) = (y, u, v);
333+ let mut band = 0;
334+ while band < ch {
335+ let take = bands_per_lane.min(ch - band);
336+ // The last lane's last band is one row short on an odd
337+ // frame, which is the only reason `y`'s chunk isn't simply
338+ // `take * 2 * width`.
339+ let y_len = (take * 2 * width).min(y_rest.len());
340+ let u_len = take * cw;
341+ let (y_chunk, y_remain) = y_rest.split_at_mut(y_len);
342+ let (u_chunk, u_remain) = u_rest.split_at_mut(u_len);
343+ let (v_chunk, v_remain) = v_rest.split_at_mut(u_len);
344+ (y_rest, u_rest, v_rest) = (y_remain, u_remain, v_remain);
345+ scope.spawn(move || self.map_bands(y_chunk, u_chunk, v_chunk, width, cw));
346+ band += take;
347+ }
348+ });
349+ }
350+
351+ /// One contiguous run of bands: the loop `map` used to run over the whole
352+ /// frame, unchanged, just handed a slice that may be the whole plane or
353+ /// one lane's share of it.
354+ fn map_bands(&self, y: &mut [u8], u: &mut [u8], v: &mut [u8], width: usize, cw: usize) {
312 // Row slices rather than indices: the plane bounds are checked once per355 // Row slices rather than indices: the plane bounds are checked once per
313 // band, not once per sample, which is most of the difference between356 // band, not once per sample, which is most of the difference between
314 // this and a frame's time budget.357 // this and a frame's time budget.
315- let bands = y[..width * height].chunks_mut(2 * width);358+ let bands = y.chunks_mut(2 * width);
316- let chroma_rows = u[..cw * ch].chunks_mut(cw).zip(v[..cw * ch].chunks_mut(cw));359+ let chroma_rows = u.chunks_mut(cw).zip(v.chunks_mut(cw));
317 for (band, (u_row, v_row)) in bands.zip(chroma_rows) {360 for (band, (u_row, v_row)) in bands.zip(chroma_rows) {
318 // The bottom half is empty on an odd frame's last band.361 // The bottom half is empty on an odd frame's last band.
319 let (top, bottom) = band.split_at_mut(width.min(band.len()));362 let (top, bottom) = band.split_at_mut(width.min(band.len()));