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) 2021 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 type = 1;
26layout (constant_id = 1) const float value = 0;
27layout (constant_id = 2) const int per_channel_pad = 0;
28
29#define shape_constant_id_offset 3
30layout (constant_id = shape_constant_id_offset + 0) const int dims = 0;
31layout (constant_id = shape_constant_id_offset + 1) const int w = 0;
32layout (constant_id = shape_constant_id_offset + 2) const int h = 0;
33layout (constant_id = shape_constant_id_offset + 3) const int d = 0;
34layout (constant_id = shape_constant_id_offset + 4) const int c = 0;
35layout (constant_id = shape_constant_id_offset + 5) const int cstep = 0;
36
37layout (constant_id = shape_constant_id_offset + 6) const int outdims = 0;
38layout (constant_id = shape_constant_id_offset + 7) const int outw = 0;
39layout (constant_id = shape_constant_id_offset + 8) const int outh = 0;
40layout (constant_id = shape_constant_id_offset + 9) const int outd = 0;
41layout (constant_id = shape_constant_id_offset + 10) const int outc = 0;
42layout (constant_id = shape_constant_id_offset + 11) const int outcstep = 0;
43
44#if NCNN_image_shader
45layout (binding = 0) uniform unfp sampler3D bottom_blob;
46layout (binding = 1, imfmtc4) writeonly uniform unfp image3D top_blob;
47layout (binding = 2) uniform unfp sampler3D per_channel_pad_blob;
48#else
49layout (binding = 0) readonly buffer bottom_blob { sfpvec8 bottom_blob_data[]; };
50layout (binding = 1) writeonly buffer top_blob { sfpvec8 top_blob_data[]; };
51layout (binding = 2) readonly buffer per_channel_pad_blob { sfpvec8 per_channel_pad_blob_data[]; };
52#endif
53
54layout (push_constant) uniform parameter
55{
56 int dims;
57 int w;
58 int h;
59 int d;
60 int c;
61 int cstep;
62
63 int outdims;
64 int outw;
65 int outh;
66 int outd;
67 int outc;
68 int outcstep;
69
70 int left;
71 int top;
72 int front;
73} p;
74
75void main()
76{
77 int gx = int(gl_GlobalInvocationID.x);
78 int gy = int(gl_GlobalInvocationID.y);
79 int gz = int(gl_GlobalInvocationID.z);
80
81 if (gx >= psc(outw) || gy >= psc(outh) * psc(outd) || gz >= psc(outc))
82 return;
83
84 // if (psc(dims) == 4)
85 {
86 const int gi = gz * psc(outcstep) + gy * psc(outw) + gx;
87
88 int x = gx - p.left;
89 int yh = gy % psc(outh) - p.top;
90 int yd = gy / psc(outh) - p.front;
91 int y = yd * psc(h) + yh;
92 int z = gz;
93
94 if (type == 0)
95 {
96 if (x >= 0 && x < psc(w) && yh >= 0 && yh < psc(h) && yd >= 0 && yd < psc(d) && z >= 0 && z < psc(c))
97 {
98#if NCNN_image_shader
99 image3d_cp8(top_blob, ivec3(gx, gy, gz), bottom_blob, ivec3(x, y, z));
100#else
101 int v_offset = z * psc(cstep) + y * psc(w) + x;
102 buffer_cp8(top_blob_data, gi, bottom_blob_data, v_offset);
103#endif
104 }
105 else if (per_channel_pad == 1)
106 {
107#if NCNN_image_shader
108 image3d_cp8(top_blob, ivec3(gx, gy, gz), per_channel_pad_blob, ivec3(gz, 0, 0));
109#else
110 buffer_cp8(top_blob_data, gi, per_channel_pad_blob_data, gz);
111#endif
112 }
113 else
114 {
115 afpvec8 v = afpvec8(afpvec4(value), afpvec4(value));
116#if NCNN_image_shader
117 image3d_st8(top_blob, ivec3(gx, gy, gz), v);
118#else
119 buffer_st8(top_blob_data, gi, v);
120#endif
121 }
122 }
123 // TODO implement type 1 and 2
124 }
125}