// Replicate-mode pad for i32
const WORKGROUP_SIZE: u32 = 256u;
const MAX_DIMS: u32 = 8u;
// Use vec4<u32> for 16-byte alignment in uniform buffer
struct PadReplicateParams {
ndim: u32,
total_elements: u32,
_pad0: u32,
_pad1: u32,
src_shape: array<vec4<u32>, 2>, // 8 u32 values packed into 2 vec4
out_shape: array<vec4<u32>, 2>,
pad_before: array<vec4<u32>, 2>,
}
// Helper to access packed array<vec4<u32>, 2> by index
fn get_packed_value(arr: array<vec4<u32>, 2>, d: i32) -> u32 {
let vec_idx = u32(d) / 4u;
let comp_idx = u32(d) % 4u;
if (vec_idx == 0u) {
if (comp_idx == 0u) { return arr[0].x; }
else if (comp_idx == 1u) { return arr[0].y; }
else if (comp_idx == 2u) { return arr[0].z; }
else { return arr[0].w; }
} else {
if (comp_idx == 0u) { return arr[1].x; }
else if (comp_idx == 1u) { return arr[1].y; }
else if (comp_idx == 2u) { return arr[1].z; }
else { return arr[1].w; }
}
}
// Clamp an output coordinate to the nearest edge element of the source.
// Any pad size is valid; `validate_replicate_pad` guarantees `size >= 1`.
fn replicate_coord(out_coord: i32, before: i32, size: i32) -> i32 {
return clamp(out_coord - before, 0, size - 1);
}
@group(0) @binding(0) var<storage, read_write> pad_replicate_src: array<i32>;
@group(0) @binding(1) var<storage, read_write> pad_replicate_dst: array<i32>;
@group(0) @binding(2) var<uniform> pad_replicate_params: PadReplicateParams;
@compute @workgroup_size(256)
fn pad_replicate_i32(
@builtin(global_invocation_id) gid: vec3<u32>,
@builtin(num_workgroups) nwg: vec3<u32>,
) {
let idx = gid.x + gid.y * nwg.x * WORKGROUP_SIZE;
if (idx >= pad_replicate_params.total_elements) {
return;
}
var remaining = idx;
var coords: array<u32, 8>;
for (var d = i32(pad_replicate_params.ndim) - 1; d >= 0; d = d - 1) {
let out_dim = get_packed_value(pad_replicate_params.out_shape, d);
coords[d] = remaining % out_dim;
remaining = remaining / out_dim;
}
var src_idx = 0u;
var src_stride = 1u;
for (var d = i32(pad_replicate_params.ndim) - 1; d >= 0; d = d - 1) {
let size = i32(get_packed_value(pad_replicate_params.src_shape, d));
let before = i32(get_packed_value(pad_replicate_params.pad_before, d));
let src_coord = u32(replicate_coord(i32(coords[d]), before, size));
src_idx = src_idx + src_coord * src_stride;
src_stride = src_stride * get_packed_value(pad_replicate_params.src_shape, d);
}
pad_replicate_dst[idx] = pad_replicate_src[src_idx];
}