Elffuss BraidHub

kikocis/ncnn public

master · 5.1 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2019 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#endif
20#if NCNN_fp16_arithmetic
21#extension GL_EXT_shader_explicit_arithmetic_types_float16: require
22#endif
23
24#define shape_constant_id_offset 0
25layout (constant_id = shape_constant_id_offset + 0) const int dims = 0;
26layout (constant_id = shape_constant_id_offset + 1) const int w = 0;
27layout (constant_id = shape_constant_id_offset + 2) const int h = 0;
28layout (constant_id = shape_constant_id_offset + 3) const int c = 0;
29layout (constant_id = shape_constant_id_offset + 4) const int cstep = 0;
30
31layout (constant_id = shape_constant_id_offset + 5) const int outdims = 0;
32layout (constant_id = shape_constant_id_offset + 6) const int outw = 0;
33layout (constant_id = shape_constant_id_offset + 7) const int outh = 0;
34layout (constant_id = shape_constant_id_offset + 8) const int outc = 0;
35layout (constant_id = shape_constant_id_offset + 9) const int outcstep = 0;
36
37#if NCNN_image_shader
38layout (binding = 0) uniform unfp sampler3D bottom_blob_3d;
39layout (binding = 1, imfmtc4) writeonly uniform unfp image3D top_blob;
40#else
41#if NCNN_fp16_packed
42layout (binding = 0) readonly buffer bottom_blob { sfpvec2 bottom_blob_data[]; };
43#else
44layout (binding = 0) readonly buffer bottom_blob { sfp bottom_blob_data[]; };
45#endif
46layout (binding = 1) writeonly buffer top_blob { sfpvec4 top_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
57 int outdims;
58 int outw;
59 int outh;
60 int outc;
61 int outcstep;
62} p;
63
64void main()
65{
66 int gx = int(gl_GlobalInvocationID.x);
67 int gy = int(gl_GlobalInvocationID.y);
68 int gz = int(gl_GlobalInvocationID.z);
69
70 if (gx >= psc(outw) || gy >= 1 || gz >= 1)
71 return;
72
73 ivec4 i4 = gx * 4 + ivec4(0, 1, 2, 3);
74
75#if NCNN_image_shader
76 afpvec4 v;
77
78 if (psc(dims) == 2)
79 {
80 ivec4 y4 = i4 / psc(w);
81 ivec4 x4 = i4 % psc(w);
82
83 afpvec4 v0 = image3d_ld4(bottom_blob_3d, ivec3(x4.r, y4.r / 4, 0));
84 afpvec4 v1 = image3d_ld4(bottom_blob_3d, ivec3(x4.g, y4.g / 4, 0));
85 afpvec4 v2 = image3d_ld4(bottom_blob_3d, ivec3(x4.b, y4.b / 4, 0));
86 afpvec4 v3 = image3d_ld4(bottom_blob_3d, ivec3(x4.a, y4.a / 4, 0));
87
88 v.r = v0[y4.r % 4];
89 v.g = v1[y4.g % 4];
90 v.b = v2[y4.b % 4];
91 v.a = v3[y4.a % 4];
92 }
93 else // if (psc(dims) == 3)
94 {
95 int size = psc(w) * psc(h);
96
97 ivec4 z4 = i4 / size;
98 ivec4 y4 = i4 % size / psc(w);
99 ivec4 x4 = i4 % size % psc(w);
100
101 afpvec4 v0 = image3d_ld4(bottom_blob_3d, ivec3(x4.r, y4.r, z4.r / 4));
102 afpvec4 v1 = image3d_ld4(bottom_blob_3d, ivec3(x4.g, y4.g, z4.g / 4));
103 afpvec4 v2 = image3d_ld4(bottom_blob_3d, ivec3(x4.b, y4.b, z4.b / 4));
104 afpvec4 v3 = image3d_ld4(bottom_blob_3d, ivec3(x4.a, y4.a, z4.a / 4));
105
106 v.r = v0[z4.r % 4];
107 v.g = v1[z4.g % 4];
108 v.b = v2[z4.b % 4];
109 v.a = v3[z4.a % 4];
110 }
111
112 image3d_st4(top_blob, ivec3(gx, 0, 0), v);
113#else
114#if NCNN_fp16_packed
115 ivec4 v_offset;
116 ivec4 lane2;
117
118 if (psc(dims) == 2)
119 {
120 ivec4 y4 = i4 / psc(w);
121 ivec4 x4 = i4 % psc(w);
122
123 v_offset = ((y4 / 4) * psc(w) + x4) * 2 + (y4 % 4) / 2;
124 lane2 = y4 % 2;
125 }
126 else // if (psc(dims) == 3)
127 {
128 int size = psc(w) * psc(h);
129
130 ivec4 z4 = i4 / size;
131 ivec4 y4 = i4 % size / psc(w);
132 ivec4 x4 = i4 % size % psc(w);
133
134 v_offset = ((z4 / 4) * psc(cstep) + y4 * psc(w) + x4) * 2 + (z4 % 4) / 2;
135 lane2 = z4 % 2;
136 }
137
138 afpvec2 vr = buffer_ld2(bottom_blob_data, v_offset.r);
139 afpvec2 vg = buffer_ld2(bottom_blob_data, v_offset.g);
140 afpvec2 vb = buffer_ld2(bottom_blob_data, v_offset.b);
141 afpvec2 va = buffer_ld2(bottom_blob_data, v_offset.a);
142
143 afpvec4 v = afpvec4(vr[lane2.r], vg[lane2.g], vb[lane2.b], va[lane2.a]);
144
145 buffer_st4(top_blob_data, gx, v);
146#else
147 ivec4 v_offset;
148
149 if (psc(dims) == 2)
150 {
151 ivec4 y4 = i4 / psc(w);
152 ivec4 x4 = i4 % psc(w);
153
154 v_offset = ((y4 / 4) * psc(w) + x4) * 4 + y4 % 4;
155 }
156 else // if (psc(dims) == 3)
157 {
158 int size = psc(w) * psc(h);
159
160 ivec4 z4 = i4 / size;
161 ivec4 y4 = i4 % size / psc(w);
162 ivec4 x4 = i4 % size % psc(w);
163
164 v_offset = ((z4 / 4) * psc(cstep) + y4 * psc(w) + x4) * 4 + z4 % 4;
165 }
166
167 buffer_cp1to4(top_blob_data, gx, bottom_blob_data, v_offset);
168#endif
169#endif
170}