|
| 1 | +// where.glsl |
| 2 | + |
| 3 | +/* |
| 4 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 5 | + * All rights reserved. |
| 6 | + * |
| 7 | + * This source code is licensed under the BSD-style license found in the |
| 8 | + * LICENSE file in the root directory of this source tree. |
| 9 | + */ |
| 10 | + |
| 11 | + |
| 12 | +#version 450 core |
| 13 | + |
| 14 | +#define PRECISION ${PRECISION} |
| 15 | + |
| 16 | +#define VEC4_T ${texel_load_type(DTYPE, STORAGE)} |
| 17 | +#define T ${buffer_scalar_type(DTYPE)} |
| 18 | +#define COND_T ${buffer_scalar_type("bool")} |
| 19 | + |
| 20 | +${define_active_storage_type(STORAGE)} |
| 21 | +${define_required_extensions(DTYPE)} |
| 22 | +${define_required_extensions("bool")} |
| 23 | + |
| 24 | +layout(std430) buffer; |
| 25 | + |
| 26 | +${layout_declare_tensor(B, "w", "t_out", DTYPE, STORAGE)} |
| 27 | +${layout_declare_tensor(B, "r", "t_condition", "bool", STORAGE)} |
| 28 | +${layout_declare_tensor(B, "r", "t_self", DTYPE, STORAGE)} |
| 29 | +${layout_declare_tensor(B, "r", "t_other", DTYPE, STORAGE)} |
| 30 | + |
| 31 | + |
| 32 | +#include "indexing_utils.h" |
| 33 | + |
| 34 | +$if STORAGE == "buffer": |
| 35 | + ${layout_declare_ubo(B, "int", "out_numl")} |
| 36 | + ${layout_declare_ubo(B, "ivec4", "out_strides")} |
| 37 | + ${layout_declare_ubo(B, "ivec4", "cond_strides")} |
| 38 | + ${layout_declare_ubo(B, "ivec4", "self_strides")} |
| 39 | + ${layout_declare_ubo(B, "ivec4", "other_strides")} |
| 40 | + |
| 41 | + ${layout_declare_spec_const(C, "int", "out_packed_dim", "DEFAULT_LAYOUT")} |
| 42 | + ${layout_declare_spec_const(C, "int", "cond_packed_dim", "DEFAULT_LAYOUT")} |
| 43 | + ${layout_declare_spec_const(C, "int", "self_packed_dim", "DEFAULT_LAYOUT")} |
| 44 | + ${layout_declare_spec_const(C, "int", "other_packed_dim", "DEFAULT_LAYOUT")} |
| 45 | +$else: |
| 46 | + ${layout_declare_ubo(B, "ivec3", "out_limits")} |
| 47 | + |
| 48 | +layout(local_size_x_id = 0, local_size_y_id = 1, local_size_z_id = 2) in; |
| 49 | + |
| 50 | +#ifdef USING_BUFFER |
| 51 | + |
| 52 | +void main() { |
| 53 | + int out_bufi = int(gl_GlobalInvocationID.x); |
| 54 | + // ivec4 tidx = ivec4(gl_GlobalInvocationID, 0); |
| 55 | + // int out_bufi = tidx_to_bufi(tidx, out_strides); |
| 56 | + // int cond_bufi = tidx_to_bufi(tidx, cond_strides); |
| 57 | + // int self_bufi = tidx_to_bufi(tidx, self_strides); |
| 58 | + // int other_bufi = tidx_to_bufi(tidx, other_strides); |
| 59 | + if (out_bufi >= out_numl) { |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + const ivec4 out_tidx = bufi_to_tidx(out_bufi, out_strides, out_packed_dim); |
| 64 | + out_bufi = tidx_to_bufi(out_tidx, out_strides); |
| 65 | + |
| 66 | + const ivec4 cond_tidx = bufi_to_tidx(out_bufi, out_strides, out_packed_dim); |
| 67 | + const int cond_bufi = tidx_to_bufi(cond_tidx, cond_strides); |
| 68 | + |
| 69 | + const ivec4 self_tidx = bufi_to_tidx(out_bufi, out_strides, out_packed_dim); |
| 70 | + const int self_bufi = tidx_to_bufi(self_tidx, self_strides); |
| 71 | + |
| 72 | + const ivec4 other_tidx = bufi_to_tidx(out_bufi, out_strides, out_packed_dim); |
| 73 | + const int other_bufi = tidx_to_bufi(other_tidx, other_strides); |
| 74 | + |
| 75 | + COND_T cond = t_condition[cond_bufi] ; |
| 76 | + T v_self = t_self[self_bufi]; |
| 77 | + T v_other = t_other[other_bufi]; |
| 78 | + |
| 79 | + if (cond > 0) { |
| 80 | + t_out[out_bufi] = v_self; |
| 81 | + } else { |
| 82 | + t_out[out_bufi] = v_other; |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +#else // !USING_BUFFER |
| 87 | + |
| 88 | +void main() { |
| 89 | + const ivec3 pos = ivec3(gl_GlobalInvocationID); |
| 90 | + |
| 91 | + |
| 92 | + if (any(greaterThanEqual(pos, out_limits))) { |
| 93 | + return; |
| 94 | + } |
| 95 | + |
| 96 | + vec4 cond = load_texel(t_condition, pos); |
| 97 | + VEC4_T selftex = load_texel(t_self, pos); |
| 98 | + VEC4_T othertex = load_texel(t_other, pos); |
| 99 | + |
| 100 | + VEC4_T outtex; |
| 101 | + |
| 102 | + for (int idx = 0; idx < 4; ++idx) { |
| 103 | + if (cond[idx] == 1) { |
| 104 | + outtex[idx] = selftex[idx]; |
| 105 | + } else { |
| 106 | + outtex[idx] = othertex[idx]; |
| 107 | + } |
| 108 | + } |
| 109 | + write_texel(t_out, pos, outtex); |
| 110 | +} |
| 111 | + #endif // !USING_BUFFER |
0 commit comments