|
| 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 | + |
| 13 | +#define PRECISION ${PRECISION} |
| 14 | + |
| 15 | +#define VEC4_T ${texel_load_type(DTYPE, STORAGE)} |
| 16 | +#define T ${texel_load_component_type(DTYPE, STORAGE)} |
| 17 | + |
| 18 | +${define_active_storage_type(STORAGE)} |
| 19 | + |
| 20 | +#extension GL_EXT_control_flow_attributes : require |
| 21 | + |
| 22 | +layout(std430) buffer; |
| 23 | + |
| 24 | +#include "indexing.glslh" |
| 25 | + |
| 26 | +${layout_declare_tensor(B, "w", "t_out", DTYPE, STORAGE)} |
| 27 | +${layout_declare_tensor(B, "r", "t_in", DTYPE, STORAGE)} |
| 28 | + |
| 29 | +${layout_declare_ubo(B, "TextureMetadata", "outp")} |
| 30 | +${layout_declare_ubo(B, "TextureMetadata", "inp")} |
| 31 | + |
| 32 | +layout(local_size_x_id = 0, local_size_y_id = 1, local_size_z_id = 2) in; |
| 33 | + |
| 34 | +${layout_declare_spec_const(C, "int", "out_layout", "CONTIG_LAYOUT_INT")} |
| 35 | +${layout_declare_spec_const(C, "int", "in_layout", "CONTIG_LAYOUT_INT")} |
| 36 | +${layout_declare_spec_const(C, "int", "upscale_factor", "1")} |
| 37 | + |
| 38 | +const int out_packed_dim = get_packed_dim(out_layout); |
| 39 | + |
| 40 | +/* |
| 41 | + * pixel_shuffle: rearranges (N, C*r*r, H, W) -> (N, C, H*r, W*r). |
| 42 | + * |
| 43 | + * For output element at NCHW index (n, c, h_out, w_out): |
| 44 | + * w_in = w_out / r |
| 45 | + * h_in = h_out / r |
| 46 | + * c_in = c * r * r + (h_out % r) * r + (w_out % r) |
| 47 | + * |
| 48 | + * Each thread writes one output texel of 4 components along the packed dim. |
| 49 | + * Each component may map to a different input texel, so we resolve per- |
| 50 | + * component and use texelFetch on the input. |
| 51 | + */ |
| 52 | +void main() { |
| 53 | + const ivec3 out_pos = ivec3(gl_GlobalInvocationID); |
| 54 | + |
| 55 | + if (out_of_bounds(out_pos, outp)) { |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + TensorIndex4D out_tidx = |
| 60 | + texture_pos_to_tensor4d_idx_simple(outp, out_pos, out_layout); |
| 61 | + |
| 62 | + // safe_idx() avoids dynamic UBO-vector indexing, which crashes Adreno 740. |
| 63 | + // The output may not span a full block of 4 along the packed dim if the |
| 64 | + // packed-dim size is not a multiple of 4, so clamp the loop. |
| 65 | + const int limit = min( |
| 66 | + 4, |
| 67 | + safe_idx(outp.sizes, out_packed_dim) - |
| 68 | + safe_idx(out_tidx.data, out_packed_dim)); |
| 69 | + |
| 70 | + const int r = upscale_factor; |
| 71 | + |
| 72 | + VEC4_T out_texel = VEC4_T(0); |
| 73 | + for (int comp = 0; comp < 4; comp++) { |
| 74 | + if (comp >= limit) { |
| 75 | + break; |
| 76 | + } |
| 77 | + |
| 78 | + // Build the per-component output tensor index. tidx.data is a local |
| 79 | + // ivec4 in WHCN order ([0]=W, [1]=H, [2]=C, [3]=N), so dynamic indexing |
| 80 | + // here is safe (not UBO-backed). |
| 81 | + TensorIndex4D out_tidx_c = out_tidx; |
| 82 | + safe_set( |
| 83 | + out_tidx_c.data, |
| 84 | + out_packed_dim, |
| 85 | + safe_idx(out_tidx.data, out_packed_dim) + comp); |
| 86 | + |
| 87 | + const int w_out = out_tidx_c.data.x; |
| 88 | + const int h_out = out_tidx_c.data.y; |
| 89 | + const int c_out = out_tidx_c.data.z; |
| 90 | + |
| 91 | + const int w_in = w_out / r; |
| 92 | + const int h_in = h_out / r; |
| 93 | + const int c_in = c_out * r * r + (h_out % r) * r + (w_out % r); |
| 94 | + |
| 95 | + TensorIndex4D in_tidx; |
| 96 | + in_tidx.data = ivec4(w_in, h_in, c_in, out_tidx_c.data.w); |
| 97 | + |
| 98 | + TextureElementIndex in_elem = |
| 99 | + tensor4d_idx_to_texture_element_idx_simple(inp, in_tidx, in_layout); |
| 100 | + VEC4_T in_texel = texelFetch(t_in, in_elem.pos, 0); |
| 101 | + out_texel[comp] = in_texel[in_elem.comp]; |
| 102 | + } |
| 103 | + |
| 104 | + imageStore(t_out, out_pos, out_texel); |
| 105 | +} |
0 commit comments