Elffuss BraidHub

kikocis/ncnn public

master · 5.2 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
24layout (constant_id = 0) const int flip = 0;
25layout (constant_id = 1) const int clip = 0;
26layout (constant_id = 2) const float offset = 0;
27layout (constant_id = 3) const float variances_0 = 0;
28layout (constant_id = 4) const float variances_1 = 0;
29layout (constant_id = 5) const float variances_2 = 0;
30layout (constant_id = 6) const float variances_3 = 0;
31layout (constant_id = 7) const int num_min_size = 0;
32layout (constant_id = 8) const int num_max_size = 0;
33layout (constant_id = 9) const int num_aspect_ratio = 0;
34layout (constant_id = 10) const int num_prior = 0;
35
36#define shape_constant_id_offset 11
37layout (constant_id = shape_constant_id_offset + 0) const int w = 0;
38layout (constant_id = shape_constant_id_offset + 1) const int h = 0;
39
40#if NCNN_fp16_packed
41layout (binding = 0) writeonly buffer top_blob { vec4 top_blob_data[]; };
42#else
43layout (binding = 0) writeonly buffer top_blob { sfpvec4 top_blob_data[]; };
44#endif
45layout (binding = 1) readonly buffer min_sizes { sfp min_sizes_data[]; };
46layout (binding = 2) readonly buffer max_sizes { sfp max_sizes_data[]; };
47layout (binding = 3) readonly buffer aspect_ratios { sfp aspect_ratios_data[]; };
48
49layout (push_constant) uniform parameter
50{
51 int w;
52 int h;
53
54 float image_w;
55 float image_h;
56 float step_w;
57 float step_h;
58} p;
59
60void main()
61{
62 int gx = int(gl_GlobalInvocationID.x);
63 int gy = int(gl_GlobalInvocationID.y);
64 int gz = int(gl_GlobalInvocationID.z);
65
66 if (gx >= num_min_size || gy >= psc(w) || gz >= psc(h))
67 return;
68
69 // anchor and variance
70 int v_offset = (gz * psc(w) + gy) * num_prior + gx;
71 int var_offset = psc(w) * psc(h) * num_prior + v_offset;
72
73 afp center_x = (afp(gy) + afp(offset)) * afp(p.step_w);
74 afp center_y = (afp(gz) + afp(offset)) * afp(p.step_h);
75 afpvec4 center = afpvec4(center_x, center_y, center_x, center_y);
76
77 afpvec4 image_norm = afp(1.f) / afpvec4(p.image_w, p.image_h, p.image_w, p.image_h);
78
79 afpvec4 box;
80
81 afp box_w;
82 afp box_h;
83
84 afp min_size = buffer_ld1(min_sizes_data, gx);
85
86 afpvec4 variances = afpvec4(afp(variances_0), afp(variances_1), afp(variances_2), afp(variances_3));
87
88 // min size box
89 box_w = box_h = min_size;
90
91 box = (center + afpvec4(-box_w, -box_h, box_w, box_h) * afp(0.5f)) * image_norm;
92 box = clip == 1 ? clamp(box, afp(0.f), afp(1.f)) : box;
93
94#if NCNN_fp16_packed
95 top_blob_data[v_offset] = vec4(box);
96 top_blob_data[var_offset] = vec4(variances);
97#else
98 buffer_st4(top_blob_data, v_offset, box);
99 buffer_st4(top_blob_data, var_offset, variances);
100#endif
101
102 v_offset += 1;
103 var_offset += 1;
104
105 if (num_max_size > 0)
106 {
107 afp max_size = buffer_ld1(max_sizes_data, gx);
108
109 // max size box
110 box_w = box_h = sqrt(min_size * max_size);
111
112 box = (center + afpvec4(-box_w, -box_h, box_w, box_h) * afp(0.5f)) * image_norm;
113 box = clip == 1 ? clamp(box, afp(0.f), afp(1.f)) : box;
114
115#if NCNN_fp16_packed
116 top_blob_data[v_offset] = vec4(box);
117 top_blob_data[var_offset] = vec4(variances);
118#else
119 buffer_st4(top_blob_data, v_offset, box);
120 buffer_st4(top_blob_data, var_offset, variances);
121#endif
122
123 v_offset += 1;
124 var_offset += 1;
125 }
126
127 // all aspect_ratios
128 for (int pi = 0; pi < num_aspect_ratio; pi++)
129 {
130 afp ar = buffer_ld1(aspect_ratios_data, pi);
131
132 box_w = min_size * sqrt(ar);
133 box_h = min_size / sqrt(ar);
134
135 box = (center + afpvec4(-box_w, -box_h, box_w, box_h) * afp(0.5f)) * image_norm;
136 box = clip == 1 ? clamp(box, afp(0.f), afp(1.f)) : box;
137
138#if NCNN_fp16_packed
139 top_blob_data[v_offset] = vec4(box);
140 top_blob_data[var_offset] = vec4(variances);
141#else
142 buffer_st4(top_blob_data, v_offset, box);
143 buffer_st4(top_blob_data, var_offset, variances);
144#endif
145
146 v_offset += 1;
147 var_offset += 1;
148
149 if (flip == 1)
150 {
151 box = (center + afpvec4(-box_h, -box_w, box_h, box_w) * afp(0.5f)) * image_norm;
152 box = clip == 1 ? clamp(box, afp(0.f), afp(1.f)) : box;
153
154#if NCNN_fp16_packed
155 top_blob_data[v_offset] = vec4(box);
156 top_blob_data[var_offset] = vec4(variances);
157#else
158 buffer_st4(top_blob_data, v_offset, box);
159 buffer_st4(top_blob_data, var_offset, variances);
160#endif
161
162 v_offset += 1;
163 var_offset += 1;
164 }
165 }
166}