|
- // Tencent is pleased to support the open source community by making ncnn available.
- //
- // Copyright (C) 2022 THL A29 Limited, a Tencent company. All rights reserved.
- //
- // Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
- // in compliance with the License. You may obtain a copy of the License at
- //
- // https://opensource.org/licenses/BSD-3-Clause
- //
- // Unless required by applicable law or agreed to in writing, software distributed
- // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
- // CONDITIONS OF ANY KIND, either express or implied. See the License for the
- // specific language governing permissions and limitations under the License.
-
- #version 450
-
- #if NCNN_fp16_storage
- #extension GL_EXT_shader_16bit_storage: require
- struct sfpvec8 { f16vec4 abcd; f16vec4 efgh; };
- #endif
- #if NCNN_fp16_arithmetic
- #extension GL_EXT_shader_explicit_arithmetic_types_float16: require
- #endif
-
- #define shape_constant_id_offset 0
- layout (constant_id = shape_constant_id_offset + 0) const int w = 0;
- layout (constant_id = shape_constant_id_offset + 1) const int h = 0;
- layout (constant_id = shape_constant_id_offset + 2) const int c = 0;
- layout (constant_id = shape_constant_id_offset + 3) const int cstep = 0;
-
- layout (constant_id = shape_constant_id_offset + 4) const int outw = 0;
- layout (constant_id = shape_constant_id_offset + 5) const int outh = 0;
-
- #if NCNN_image_shader
- layout (binding = 0) uniform unfp sampler3D bottom_blob;
- layout (binding = 1, imfmtc1) writeonly uniform unfp image3D col_blob;
- layout (binding = 2) uniform unfp sampler3D weight_blob;
- #else
- layout (binding = 0) readonly buffer bottom_blob { sfpvec8 bottom_blob_data[]; };
- layout (binding = 1) writeonly buffer col_blob { sfp col_blob_data[]; };
- layout (binding = 2) readonly buffer weight_blob { sfpvec8 weight_data[]; };
- #endif
-
- layout (push_constant) uniform parameter
- {
- int w;
- int h;
- int c;
- int cstep;
-
- int outw;
- int outh;
- } p;
-
- void main()
- {
- int gx = int(gl_GlobalInvocationID.x) * 4;
- int gy = int(gl_GlobalInvocationID.y);
-
- if (gx >= psc(outw) || gy >= psc(outh))
- return;
-
- afp sum0 = afp(0.f);
- afp sum1 = afp(0.f);
- afp sum2 = afp(0.f);
- afp sum3 = afp(0.f);
-
- #if NCNN_image_shader
- ivec4 gx4 = gx + ivec4(0, 1, 2, 3);
-
- ivec4 sy4 = gx4 / psc(w);
- ivec4 sx4 = gx4 % psc(w);
-
- for (int z = 0; z < psc(c); z++)
- {
- afpvec8 v0 = image3d_ld8(bottom_blob, ivec3(sx4.r, sy4.r, z));
- afpvec8 v1 = image3d_ld8(bottom_blob, ivec3(sx4.g, sy4.g, z));
- afpvec8 v2 = image3d_ld8(bottom_blob, ivec3(sx4.b, sy4.b, z));
- afpvec8 v3 = image3d_ld8(bottom_blob, ivec3(sx4.a, sy4.a, z));
-
- afpvec8 k = image3d_ld8(weight_blob, ivec3(z, gy, 0));
-
- // sum += dot(v, k);
- sum0 += dot(v0[0], k[0]) + dot(v0[1], k[1]);
- sum1 += dot(v1[0], k[0]) + dot(v1[1], k[1]);
- sum2 += dot(v2[0], k[0]) + dot(v2[1], k[1]);
- sum3 += dot(v3[0], k[0]) + dot(v3[1], k[1]);
- }
- #else
- int v_offset = gx;
- int w_offset = gy * psc(c);
-
- for (int z = 0; z < psc(c); z++)
- {
- afpvec8 v0 = buffer_ld8(bottom_blob_data, v_offset + 0);
- afpvec8 v1 = buffer_ld8(bottom_blob_data, v_offset + 1);
- afpvec8 v2 = buffer_ld8(bottom_blob_data, v_offset + 2);
- afpvec8 v3 = buffer_ld8(bottom_blob_data, v_offset + 3);
-
- afpvec8 k = buffer_ld8(weight_data, w_offset);
-
- // sum += dot(v, k);
- sum0 += dot(v0[0], k[0]) + dot(v0[1], k[1]);
- sum1 += dot(v1[0], k[0]) + dot(v1[1], k[1]);
- sum2 += dot(v2[0], k[0]) + dot(v2[1], k[1]);
- sum3 += dot(v3[0], k[0]) + dot(v3[1], k[1]);
-
- v_offset += psc(cstep);
- w_offset += 1;
- }
- #endif
-
- #if NCNN_image_shader
- image3d_st1(col_blob, ivec3(gx4.r, gy, 0), sum0);
- image3d_st1(col_blob, ivec3(gx4.g, gy, 0), sum1);
- image3d_st1(col_blob, ivec3(gx4.b, gy, 0), sum2);
- image3d_st1(col_blob, ivec3(gx4.a, gy, 0), sum3);
- #else
- const int gi = gy * psc(outw) + gx;
-
- buffer_st1(col_blob_data, gi, sum0);
- if (gx + 1 < psc(outw)) buffer_st1(col_blob_data, gi + 1, sum1);
- if (gx + 2 < psc(outw)) buffer_st1(col_blob_data, gi + 2, sum2);
- if (gx + 3 < psc(outw)) buffer_st1(col_blob_data, gi + 3, sum3);
- #endif
- }
|