Elffuss BraidHub

kikocis/ncnn public

master · 4.2 KiBRawPermalink
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
19struct 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
25layout (constant_id = 0) const int across_spatial = 0;
26layout (constant_id = 1) const int across_channel = 0;
27layout (constant_id = 2) const int channel_shared = 0;
28layout (constant_id = 3) const int scale_term = 0;
29layout (constant_id = 4) const float channel_shared_scale = 1.f;
30
31#define shape_constant_id_offset 5
32layout (constant_id = shape_constant_id_offset + 0) const int dims = 0;
33layout (constant_id = shape_constant_id_offset + 1) const int w = 0;
34layout (constant_id = shape_constant_id_offset + 2) const int h = 0;
35layout (constant_id = shape_constant_id_offset + 3) const int c = 0;
36layout (constant_id = shape_constant_id_offset + 4) const int cstep = 0;
37
38#if NCNN_image_shader
39layout (binding = 0) uniform unfp sampler3D bottom_blob;
40layout (binding = 1, imfmtc4) writeonly uniform unfp image3D top_blob;
41layout (binding = 2) uniform unfp sampler3D coeffs_blob;
42layout (binding = 3) uniform unfp sampler3D scale_blob;
43#else
44layout (binding = 0) buffer bottom_top_blob { sfpvec8 bottom_top_blob_data[]; };
45layout (binding = 1) readonly buffer coeffs_blob { sfpvec8 coeffs_blob_data[]; };
46layout (binding = 2) readonly buffer scale_blob { sfpvec8 scale_blob_data[]; };
47#endif
48
49layout (push_constant) uniform parameter
50{
51 int dims;
52 int w;
53 int h;
54 int c;
55 int cstep;
56} p;
57
58void main()
59{
60 int gx = int(gl_GlobalInvocationID.x);
61 int gy = int(gl_GlobalInvocationID.y);
62 int gz = int(gl_GlobalInvocationID.z);
63
64 if (gx >= psc(w) || gy >= psc(h) || gz >= psc(c))
65 return;
66
67#if NCNN_image_shader
68 afpvec8 v = image3d_ld8(bottom_blob, ivec3(gx, gy, gz));
69#else
70 const int gi = gz * psc(cstep) + gy * psc(w) + gx;
71
72 afpvec8 v = buffer_ld8(bottom_top_blob_data, gi);
73#endif
74
75 afpvec8 a;
76
77 if (across_spatial == 1 && across_channel == 1)
78 {
79#if NCNN_image_shader
80 a = image3d_ld8(coeffs_blob, ivec3(0, 0, 0));
81#else
82 a = buffer_ld8(coeffs_blob_data, 0);
83#endif
84 }
85
86 if (across_spatial == 1 && across_channel == 0)
87 {
88#if NCNN_image_shader
89 a = image3d_ld8(coeffs_blob, ivec3(gz, 0, 0));
90#else
91 a = buffer_ld8(coeffs_blob_data, gz);
92#endif
93 }
94
95 if (across_spatial == 0 && across_channel == 1)
96 {
97#if NCNN_image_shader
98 a = image3d_ld8(coeffs_blob, ivec3(gy * psc(w) + gx, 0, 0));
99#else
100 a = buffer_ld8(coeffs_blob_data, gy * psc(w) + gx);
101#endif
102 }
103
104#if !NCNN_image_shader && (NCNN_fp16_packed || NCNN_fp16_storage)
105 // NOTE coeffs may produce (X, undef, X, undef) on nvidia fp16p/fp16s
106 // TODO only enable this workaround for some nvidia driver
107 if (across_channel == 1)
108 {
109 a = afpvec8(afpvec4(a[0].r), afpvec4(a[0].r));
110 }
111#endif
112
113 v[0] = v[0] * a[0];
114 v[1] = v[1] * a[1];
115
116 if (scale_term == 1)
117 {
118 if (channel_shared == 1)
119 {
120 v[0] = v[0] * afp(channel_shared_scale);
121 v[1] = v[1] * afp(channel_shared_scale);
122 }
123 else
124 {
125#if NCNN_image_shader
126 afpvec8 scale = image3d_ld8(scale_blob, ivec3(gz, 0, 0));
127#else
128 afpvec8 scale = buffer_ld8(scale_blob_data, gz);
129#endif
130
131 v[0] = v[0] * scale[0];
132 v[1] = v[1] * scale[1];
133 }
134 }
135
136#if NCNN_image_shader
137 image3d_st8(top_blob, ivec3(gx, gy, gz), v);
138#else
139 buffer_st8(bottom_top_blob_data, gi, v);
140#endif
141}