| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved. |
| 4 | // |
| 5 | // Licensed under the BSD 3-Clause License (the "License"); you may not use this file except |
| 6 | // in compliance with the License. You may obtain a copy of the License at |
| 7 | // |
| 8 | // https://opensource.org/licenses/BSD-3-Clause |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software distributed |
| 11 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 12 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 13 | // specific language governing permissions and limitations under the License. |
| 14 | |
| 15 | #version 450 |
| 16 | |
| 17 | #if NCNN_fp16_storage |
| 18 | #extension GL_EXT_shader_16bit_storage: require |
| 19 | struct sfpvec8 { f16vec4 abcd; f16vec4 efgh; }; |
| 20 | #endif |
| 21 | #if NCNN_fp16_arithmetic |
| 22 | #extension GL_EXT_shader_explicit_arithmetic_types_float16: require |
| 23 | #endif |
| 24 | |
| 25 | #if NCNN_image_shader |
| 26 | layout (binding = 0) uniform highp sampler3D square_blob; |
| 27 | layout (binding = 1, rgba32f) writeonly uniform highp image3D sqsum_blob; |
| 28 | #else |
| 29 | layout (binding = 0) readonly buffer square_blob { mat2x4 square_blob_data[]; }; |
| 30 | layout (binding = 1) writeonly buffer sqsum_blob { mat2x4 sqsum_blob_data[]; }; |
| 31 | #endif |
| 32 | |
| 33 | layout (push_constant) uniform parameter |
| 34 | { |
| 35 | int w; |
| 36 | int h; |
| 37 | int c; |
| 38 | int cstep; |
| 39 | |
| 40 | int outw; |
| 41 | int outh; |
| 42 | int outc; |
| 43 | int outcstep; |
| 44 | } p; |
| 45 | |
| 46 | void main() |
| 47 | { |
| 48 | int gx = int(gl_GlobalInvocationID.x); |
| 49 | int gy = int(gl_GlobalInvocationID.y); |
| 50 | int gz = int(gl_GlobalInvocationID.z); |
| 51 | |
| 52 | if (gx >= p.outw || gy >= p.outh || gz >= p.outc) |
| 53 | return; |
| 54 | |
| 55 | mat2x4 sum; |
| 56 | |
| 57 | #if NCNN_image_shader |
| 58 | int sx = gx * 2; |
| 59 | int sy = gy * 2; |
| 60 | |
| 61 | if (sy == p.h - 1) |
| 62 | { |
| 63 | if (sx == p.w - 1) |
| 64 | { |
| 65 | mat2x4 v0 = mat2x4(texelFetch(square_blob, ivec3(sx * 2, sy, gz), 0), texelFetch(square_blob, ivec3(sx * 2 + 1, sy, gz), 0)); |
| 66 | |
| 67 | sum = v0; |
| 68 | } |
| 69 | else |
| 70 | { |
| 71 | mat2x4 v0 = mat2x4(texelFetch(square_blob, ivec3(sx * 2, sy, gz), 0), texelFetch(square_blob, ivec3(sx * 2 + 1, sy, gz), 0)); |
| 72 | mat2x4 v1 = mat2x4(texelFetch(square_blob, ivec3((sx + 1) * 2, sy, gz), 0), texelFetch(square_blob, ivec3((sx + 1) * 2 + 1, sy, gz), 0)); |
| 73 | |
| 74 | sum = v0 + v1; |
| 75 | } |
| 76 | } |
| 77 | else |
| 78 | { |
| 79 | if (sx == p.w - 1) |
| 80 | { |
| 81 | mat2x4 v0 = mat2x4(texelFetch(square_blob, ivec3(sx * 2, sy, gz), 0), texelFetch(square_blob, ivec3(sx * 2 + 1, sy, gz), 0)); |
| 82 | mat2x4 v2 = mat2x4(texelFetch(square_blob, ivec3(sx * 2, sy + 1, gz), 0), texelFetch(square_blob, ivec3(sx * 2 + 1, sy + 1, gz), 0)); |
| 83 | |
| 84 | sum = v0 + v2; |
| 85 | } |
| 86 | else |
| 87 | { |
| 88 | mat2x4 v0 = mat2x4(texelFetch(square_blob, ivec3(sx * 2, sy, gz), 0), texelFetch(square_blob, ivec3(sx * 2 + 1, sy, gz), 0)); |
| 89 | mat2x4 v1 = mat2x4(texelFetch(square_blob, ivec3((sx + 1) * 2, sy, gz), 0), texelFetch(square_blob, ivec3((sx + 1) * 2 + 1, sy, gz), 0)); |
| 90 | mat2x4 v2 = mat2x4(texelFetch(square_blob, ivec3(sx * 2, sy + 1, gz), 0), texelFetch(square_blob, ivec3(sx * 2 + 1, sy + 1, gz), 0)); |
| 91 | mat2x4 v3 = mat2x4(texelFetch(square_blob, ivec3((sx + 1) * 2, sy + 1, gz), 0), texelFetch(square_blob, ivec3((sx + 1) * 2 + 1, sy + 1, gz), 0)); |
| 92 | |
| 93 | sum = v0 + v1 + v2 + v3; |
| 94 | } |
| 95 | } |
| 96 | #else |
| 97 | int sx = gx * 4; |
| 98 | |
| 99 | int v_offset = gz * p.cstep + sx; |
| 100 | |
| 101 | if (sx == p.w - 1) |
| 102 | { |
| 103 | sum = square_blob_data[v_offset]; |
| 104 | } |
| 105 | else if (sx == p.w - 2) |
| 106 | { |
| 107 | mat2x4 v0 = square_blob_data[v_offset]; |
| 108 | mat2x4 v1 = square_blob_data[v_offset + 1]; |
| 109 | |
| 110 | sum = v0 + v1; |
| 111 | } |
| 112 | else if (sx == p.w - 3) |
| 113 | { |
| 114 | mat2x4 v0 = square_blob_data[v_offset]; |
| 115 | mat2x4 v1 = square_blob_data[v_offset + 1]; |
| 116 | mat2x4 v2 = square_blob_data[v_offset + 2]; |
| 117 | |
| 118 | sum = v0 + v1 + v2; |
| 119 | } |
| 120 | else |
| 121 | { |
| 122 | mat2x4 v0 = square_blob_data[v_offset]; |
| 123 | mat2x4 v1 = square_blob_data[v_offset + 1]; |
| 124 | mat2x4 v2 = square_blob_data[v_offset + 2]; |
| 125 | mat2x4 v3 = square_blob_data[v_offset + 3]; |
| 126 | |
| 127 | sum = v0 + v1 + v2 + v3; |
| 128 | } |
| 129 | #endif |
| 130 | |
| 131 | #if NCNN_image_shader |
| 132 | imageStore(sqsum_blob, ivec3(gx * 2, gy, gz), sum[0]); |
| 133 | imageStore(sqsum_blob, ivec3(gx * 2 + 1, gy, gz), sum[1]); |
| 134 | #else |
| 135 | int gi = gz * p.outcstep + gx; |
| 136 | |
| 137 | sqsum_blob_data[gi] = sum; |
| 138 | #endif |
| 139 | } |