use eyre::{Result, ensure};
use crate::metal::gpu::{DType, Gpu, Tensor};
use crate::metal::vision::GRID;
pub const LOW_RES: usize = 4 * GRID;
const LAUNCH_PIXELS: usize = 1 << 31;
pub fn upsample_masks(
gpu: &Gpu,
low_res: &Tensor,
first: usize,
n: usize,
width: usize,
height: usize,
) -> Result<Tensor> {
let side = LOW_RES * LOW_RES;
ensure!((first + n) * side <= low_res.len(), "masks {first}..{} of {:?}", first + n, low_res.shape());
let plane = width * height;
let out = gpu.empty(&[n, plane], DType::U8)?;
let per_launch = (LAUNCH_PIXELS / plane.max(1)).max(1);
for start in (0..n).step_by(per_launch) {
let count = per_launch.min(n - start);
let args = [
out.at(start * plane),
low_res.at((first + start) * side),
LOW_RES.into(),
LOW_RES.into(),
height.into(),
width.into(),
];
gpu.dispatch_1d("resize_threshold_u8", count * plane, &args)?;
}
Ok(out)
}