|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +#version 450 core |
| 10 | + |
| 11 | +${define_required_extensions(STORAGE, DTYPE)} |
| 12 | +${define_required_extensions("buffer", DTYPE)} |
| 13 | + |
| 14 | +#define PRECISION ${PRECISION} |
| 15 | + |
| 16 | +#define VEC4_T ${texel_load_type(DTYPE, STORAGE)} |
| 17 | +#define T ${texel_load_component_type(DTYPE, "buffer")} |
| 18 | + |
| 19 | +${define_active_storage_type(STORAGE)} |
| 20 | + |
| 21 | +layout(std430) buffer; |
| 22 | + |
| 23 | +#include "indexing.glslh" |
| 24 | + |
| 25 | +${layout_declare_tensor(B, "w", "t_out", DTYPE, STORAGE)} |
| 26 | +${layout_declare_tensor(B, "r", "t_in", DTYPE, STORAGE)} |
| 27 | +// `t_grid` is always bound as a contiguous (width-packed) buffer of fp scalars |
| 28 | +// with logical shape [N, Hout, Wout, 2]. See add_grid_sampler_2d_node which |
| 29 | +// asserts this with `is_contiguous_buffer_tensor`. |
| 30 | +${layout_declare_tensor(B, "r", "t_grid", DTYPE, "buffer")} |
| 31 | + |
| 32 | +${layout_declare_ubo(B, "TextureMetadata", "outp")} |
| 33 | +${layout_declare_ubo(B, "TextureMetadata", "inp")} |
| 34 | + |
| 35 | +layout(local_size_x_id = 0, local_size_y_id = 1, local_size_z_id = 2) in; |
| 36 | + |
| 37 | +// `out_layout` is passed for forward compatibility and is currently asserted |
| 38 | +// to be the standard channels-packed layout by `add_grid_sampler_2d_node`. |
| 39 | +// All texel math below assumes packed_dim = C (channels-packed), so the four |
| 40 | +// fp components of a texel share the same (N, Hout, Wout) and differ only in |
| 41 | +// channel. This lets one bilinear interpolation produce all 4 output channels. |
| 42 | +${layout_declare_spec_const(C, "int", "out_layout", "CONTIG_LAYOUT_INT")} |
| 43 | + |
| 44 | +/* |
| 45 | + * Vulkan implementation of `aten.grid_sampler_2d.default` for the |
| 46 | + * specific configuration used by RIFE's `WarpModule`: |
| 47 | + * mode=bilinear, padding_mode=border, align_corners=true. |
| 48 | + * |
| 49 | + * Layout assumptions (validated in add_grid_sampler_2d_node): |
| 50 | + * - input : channels-packed texture3d, shape [N, C, Hin, Win] |
| 51 | + * - grid : contiguous (width-packed) buffer SSBO of fp scalars, |
| 52 | + * shape [N, Hout, Wout, 2] in normalized coords [-1, 1] |
| 53 | + * - output : channels-packed texture3d, shape [N, C, Hout, Wout] |
| 54 | + * |
| 55 | + * For channels-packed texture3d, the texel z extent is N * ceil(C/4), |
| 56 | + * laid out as z = n * num_z_per_n + c_slice. Both input and output share |
| 57 | + * the same N and C, so input z == output z. |
| 58 | + * |
| 59 | + * TextureMetadata layout (vtensor.md): sizes is WHCN order, so |
| 60 | + * outp.sizes.x = Wout, outp.sizes.y = Hout, outp.sizes.w = N. |
| 61 | + * outp.limits.z = N * ceil(C/4) (texel slices along z). |
| 62 | + */ |
| 63 | +void main() { |
| 64 | + const ivec3 pos = ivec3(gl_GlobalInvocationID); |
| 65 | + |
| 66 | + if (out_of_bounds(pos, outp)) { |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + // Derive batch index from texel z. Each batch occupies `num_z_per_n` |
| 71 | + // consecutive z-slices (one per 4-channel slice). Integer division by |
| 72 | + // num_z_per_n picks out the batch. |
| 73 | + const int N = outp.sizes.w; |
| 74 | + const int num_z_per_n = outp.limits.z / N; |
| 75 | + const int n = pos.z / num_z_per_n; |
| 76 | + |
| 77 | + // Look up the (gx, gy) for this output pixel from the grid SSBO. |
| 78 | + // The grid is a contiguous buffer of [N, Hout, Wout, 2], so the linear |
| 79 | + // index for (n, h, w, comp) is ((n*Hout + h)*Wout + w)*2 + comp. This |
| 80 | + // relies on `inputs_storage` in op_registry.py pinning grid to |
| 81 | + // CONTIGUOUS_BUFFER and the C++ dispatcher re-checking with |
| 82 | + // `is_contiguous_buffer_tensor` — see GridSampler2d.cpp. |
| 83 | + const int Wout = outp.sizes.x; |
| 84 | + const int Hout = outp.sizes.y; |
| 85 | + const int grid_base = ((n * Hout + pos.y) * Wout + pos.x) * 2; |
| 86 | + const float gx_norm = float(t_grid[grid_base + 0]); |
| 87 | + const float gy_norm = float(t_grid[grid_base + 1]); |
| 88 | + |
| 89 | + // Unnormalize for align_corners=true: |
| 90 | + // coord_pixel = (coord_norm + 1) * 0.5 * (size - 1) |
| 91 | + // Input W/H come from inp.sizes (WHCN), not inp.limits (texel space). |
| 92 | + const ivec2 max_in_xy = ivec2(inp.sizes.xy) - 1; |
| 93 | + const float gx_pixel = (gx_norm + 1.0) * 0.5 * float(max_in_xy.x); |
| 94 | + const float gy_pixel = (gy_norm + 1.0) * 0.5 * float(max_in_xy.y); |
| 95 | + |
| 96 | + // padding_mode=border: clamp coordinates to [0, size-1]. |
| 97 | + const float gx = clamp(gx_pixel, 0.0, float(max_in_xy.x)); |
| 98 | + const float gy = clamp(gy_pixel, 0.0, float(max_in_xy.y)); |
| 99 | + |
| 100 | + const ivec2 lower = ivec2(floor(vec2(gx, gy))); |
| 101 | + // Clamp ceil to valid range for samples on the border. |
| 102 | + const ivec2 upper = clamp(lower + ivec2(1), ivec2(0), max_in_xy); |
| 103 | + const vec2 w = vec2(gx, gy) - vec2(lower); |
| 104 | + |
| 105 | + // Fetch the four nearest texels (each carries 4 channels). Because input |
| 106 | + // is channels-packed, pos.z indexes the same channel slice in input as in |
| 107 | + // output, so we can reuse pos.z directly without remapping. |
| 108 | + VEC4_T s00 = texelFetch(t_in, ivec3(lower.x, lower.y, pos.z), 0); |
| 109 | + VEC4_T s10 = texelFetch(t_in, ivec3(upper.x, lower.y, pos.z), 0); |
| 110 | + VEC4_T s01 = texelFetch(t_in, ivec3(lower.x, upper.y, pos.z), 0); |
| 111 | + VEC4_T s11 = texelFetch(t_in, ivec3(upper.x, upper.y, pos.z), 0); |
| 112 | + |
| 113 | + // Bilinear interpolation. Weights are scalars; mix() acts on all 4 channels. |
| 114 | + VEC4_T out_tex = |
| 115 | + mix(mix(s00, s10, w.x), mix(s01, s11, w.x), w.y); |
| 116 | + |
| 117 | + imageStore(t_out, pos, out_tex); |
| 118 | +} |
0 commit comments