| 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 | layout (constant_id = 0) const float eps = 0; |
| 26 | layout (constant_id = 1) const int affine = 0; |
| 27 | layout (constant_id = 2) const int w = 0; |
| 28 | |
| 29 | #if NCNN_image_shader |
| 30 | layout (binding = 0, imfmtc4) writeonly uniform unfp image3D coeffs_blob; |
| 31 | layout (binding = 1) uniform highp sampler3D mean_blob; |
| 32 | layout (binding = 2) uniform highp sampler3D var_blob; |
| 33 | layout (binding = 3) uniform unfp sampler3D gamma_blob; |
| 34 | layout (binding = 4) uniform unfp sampler3D beta_blob; |
| 35 | #else |
| 36 | layout (binding = 0) writeonly buffer coeffs_blob { sfpvec8 coeffs_blob_data[]; }; |
| 37 | layout (binding = 1) readonly buffer mean_blob { mat2x4 mean_data[]; }; |
| 38 | layout (binding = 2) readonly buffer var_blob { mat2x4 var_data[]; }; |
| 39 | layout (binding = 3) readonly buffer gamma_blob { sfpvec8 gamma_data[]; }; |
| 40 | layout (binding = 4) readonly buffer beta_blob { sfpvec8 beta_data[]; }; |
| 41 | #endif |
| 42 | |
| 43 | layout (push_constant) uniform parameter |
| 44 | { |
| 45 | int w; |
| 46 | } p; |
| 47 | |
| 48 | void main() |
| 49 | { |
| 50 | int gx = int(gl_GlobalInvocationID.x); |
| 51 | int gy = int(gl_GlobalInvocationID.y); |
| 52 | int gz = int(gl_GlobalInvocationID.z); |
| 53 | |
| 54 | if (gx >= psc(w) || gy >= 1 || gz >= 1) |
| 55 | return; |
| 56 | |
| 57 | #if NCNN_image_shader |
| 58 | mat2x4 mean = mat2x4(texelFetch(mean_blob, ivec3(gx * 2, 0, 0), 0), texelFetch(mean_blob, ivec3(gx * 2 + 1, 0, 0), 0)); |
| 59 | mat2x4 var = mat2x4(texelFetch(var_blob, ivec3(gx * 2, 0, 0), 0), texelFetch(var_blob, ivec3(gx * 2 + 1, 0, 0), 0)); |
| 60 | #else |
| 61 | mat2x4 mean = mean_data[gx]; |
| 62 | mat2x4 var = var_data[gx]; |
| 63 | #endif |
| 64 | |
| 65 | mat2x4 a; |
| 66 | mat2x4 b; |
| 67 | if (affine == 0) |
| 68 | { |
| 69 | a[0] = 1.f / (sqrt(var[0] + eps)); |
| 70 | a[1] = 1.f / (sqrt(var[1] + eps)); |
| 71 | b[0] = - mean[0] * a[0]; |
| 72 | b[1] = - mean[1] * a[1]; |
| 73 | } |
| 74 | else |
| 75 | { |
| 76 | #if NCNN_image_shader |
| 77 | mat2x4 gamma = mat2x4(image3d_ld8(gamma_blob, ivec3(gx, 0, 0))); |
| 78 | mat2x4 beta = mat2x4(image3d_ld8(beta_blob, ivec3(gx, 0, 0))); |
| 79 | #else |
| 80 | mat2x4 gamma = mat2x4(buffer_ld8(gamma_data, gx)); |
| 81 | mat2x4 beta = mat2x4(buffer_ld8(beta_data, gx)); |
| 82 | #endif |
| 83 | |
| 84 | a[0] = gamma[0] / (sqrt(var[0] + eps)); |
| 85 | a[1] = gamma[1] / (sqrt(var[1] + eps)); |
| 86 | b[0] = - mean[0] * a[0] + beta[0]; |
| 87 | b[1] = - mean[1] * a[1] + beta[1]; |
| 88 | } |
| 89 | |
| 90 | #if NCNN_image_shader |
| 91 | imageStore(coeffs_blob, ivec3((gx*2) * 2, 0, 0), a[0]); |
| 92 | imageStore(coeffs_blob, ivec3((gx*2) * 2 + 1, 0, 0), a[1]); |
| 93 | imageStore(coeffs_blob, ivec3((gx*2 +1) * 2, 0, 0), b[0]); |
| 94 | imageStore(coeffs_blob, ivec3((gx*2 +1) * 2 + 1, 0, 0), b[1]); |
| 95 | #else |
| 96 | buffer_st8(coeffs_blob_data, gx*2, afpvec8(a)); |
| 97 | buffer_st8(coeffs_blob_data, gx*2 +1, afpvec8(b)); |
| 98 | #endif |
| 99 | } |