|
| 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 PRECISION ${PRECISION} |
| 12 | + |
| 13 | +#include "indexing_utils.h" |
| 14 | + |
| 15 | +layout(std430) buffer; |
| 16 | + |
| 17 | +layout(set = 0, binding = 0, ${IMAGE_FORMAT[DTYPE]}) uniform PRECISION restrict writeonly ${IMAGE_T[NDIM][DTYPE]} image_out; |
| 18 | +layout(set = 0, binding = 1) uniform PRECISION sampler3D image_in; |
| 19 | +layout(set = 0, binding = 2) uniform PRECISION sampler2D kernel_in; |
| 20 | +layout(set = 0, binding = 3) uniform PRECISION sampler2D bias_in; |
| 21 | + |
| 22 | +layout(set = 0, binding = 4) uniform PRECISION restrict OutExtents { |
| 23 | + uvec4 data; |
| 24 | +} |
| 25 | +out_extents; |
| 26 | + |
| 27 | +layout(set = 0, binding = 5) uniform PRECISION restrict InExtents { |
| 28 | + uvec4 data; |
| 29 | +} |
| 30 | +in_extents; |
| 31 | + |
| 32 | +layout(set = 0, binding = 6) uniform PRECISION restrict Params { |
| 33 | + ivec2 kernel_size; |
| 34 | + ivec2 stride; |
| 35 | + ivec2 padding; |
| 36 | + ivec2 dilation; |
| 37 | +} |
| 38 | +params; |
| 39 | + |
| 40 | +// If fields are separated, SwiftShader cannot identify in_group_size. |
| 41 | +layout(set = 0, binding = 7) uniform PRECISION restrict ExtraParams { |
| 42 | + ivec2 overlay_region; |
| 43 | + int in_group_size; |
| 44 | +} |
| 45 | +extra_params; |
| 46 | + |
| 47 | +layout(local_size_x_id = 0, local_size_y_id = 1, local_size_z_id = 2) in; |
| 48 | + |
| 49 | +/* |
| 50 | + * Computes a depthwise convolution. Each shader invocation calculates the |
| 51 | + * output at a single output location. |
| 52 | + */ |
| 53 | +void main() { |
| 54 | + const ivec3 pos = ivec3(gl_GlobalInvocationID); |
| 55 | + |
| 56 | + if (any(greaterThanEqual(pos, out_extents.data.xyz))) { |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + // Compute the index of the top-left element of the overlay region. Negative |
| 61 | + // indices indicate that the top-left element is in a region added by padding. |
| 62 | + const ivec2 ipos = pos.xy * params.stride - params.padding; |
| 63 | + |
| 64 | + // Compute the start and end of the input indices to load. Padding is assumed |
| 65 | + // to be constant 0 padding, so any reads from the padding region is skipped. |
| 66 | + const ivec2 start = ipos; |
| 67 | + const ivec2 end = ipos + extra_params.overlay_region.xy; |
| 68 | + |
| 69 | + ${VEC4_T[DTYPE]} sum = texelFetch(bias_in, ivec2(pos.z, 0), 0); |
| 70 | + int kx = 0; |
| 71 | + for (int y = start.y, i = 0; i < ${TILE_SIZE}; y += params.dilation.y, i++) { |
| 72 | + for (int x = start.x, j = 0; j < ${TILE_SIZE}; x += params.dilation.x, j++) { |
| 73 | + // The weight kernel was rearranged such that every NxN filter is |
| 74 | + // flattened to fit in one row. Each filter was then stacked on top of |
| 75 | + // each other vertically. |
| 76 | + const vec4 in_texel = texelFetch(image_in, ivec3(x, y, pos.z), 0); |
| 77 | + sum = fma(in_texel, texelFetch(kernel_in, ivec2(kx, pos.z), 0), sum); |
| 78 | + kx++; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + imageStore(image_out, pos, sum); |
| 83 | +} |
0 commit comments