Elffuss BraidHub

kikocis/ncnn public

master · 4.9 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 w = 0;
25layout (constant_id = 1) const int h = 0;
26layout (constant_id = 2) const int outw = 0;
27layout (constant_id = 3) const int outh = 0;
28layout (constant_id = 4) const int type_to = 0;
29layout (constant_id = 5) const int rotate_from = 0;
30layout (constant_id = 6) const int need_resize = 0;
31
32layout (binding = 0) uniform sampler2D android_hardware_buffer_image;
33#if NCNN_image_shader
34layout (binding = 1, imfmtc1) writeonly uniform unfp image3D vkmat_blob;
35layout (binding = 2, imfmtc4) writeonly uniform unfp image3D vkmat_pack4_blob;
36#else
37layout (binding = 1) writeonly buffer vkmat_blob { sfp vkmat_blob_data[]; };
38layout (binding = 2) writeonly buffer vkmat_pack4_blob { sfpvec4 vkmat_pack4_blob_data[]; };
39#endif
40
41void main()
42{
43 int gx = int(gl_GlobalInvocationID.x);
44 int gy = int(gl_GlobalInvocationID.y);
45 int gz = int(gl_GlobalInvocationID.z);
46
47 if (gx >= outw || gy >= outh || gz >= 1)
48 return;
49
50 vec2 pos;
51
52 if (rotate_from == 1)
53 {
54 pos = vec2(gx, gy);
55 }
56
57 if (rotate_from == 2)
58 {
59 pos = vec2(outw - 1 - gx, gy);
60 }
61
62 if (rotate_from == 3)
63 {
64 pos = vec2(outw - 1 - gx, outh - 1 - gy);
65 }
66
67 if (rotate_from == 4)
68 {
69 pos = vec2(gx, outh - 1 - gy);
70 }
71
72 if (rotate_from == 5)
73 {
74 pos = vec2(gy, gx);
75 }
76
77 if (rotate_from == 6)
78 {
79 pos = vec2(gy, outw - 1 - gx);
80 }
81
82 if (rotate_from == 7)
83 {
84 pos = vec2(outh - 1 - gy, outw - 1 - gx);
85 }
86
87 if (rotate_from == 8)
88 {
89 pos = vec2(outh - 1 - gy, gx);
90 }
91
92 if (need_resize == 1)
93 {
94 if (rotate_from < 5) // 1 2 3 4
95 {
96 pos.x = pos.x * (float(w) / outw);
97 pos.y = pos.y * (float(h) / outh);
98 }
99 else // 5 6 7 8
100 {
101 pos.x = pos.x * (float(w) / outh);
102 pos.y = pos.y * (float(h) / outw);
103 }
104 }
105
106 vec3 rgb = texture(android_hardware_buffer_image, pos).rgb * 255.f;
107
108 const int outcstep = outw * outh / 4 * 4;
109
110 if (type_to == 1) // PIXEL_RGB
111 {
112#if NCNN_image_shader
113 image3d_st1(vkmat_blob, ivec3(gx, gy, 0), rgb.r);
114 image3d_st1(vkmat_blob, ivec3(gx, gy, 1), rgb.g);
115 image3d_st1(vkmat_blob, ivec3(gx, gy, 2), rgb.b);
116#else
117 ivec3 v_offset = (gy * outw + gx) + ivec3(0, 1, 2) * outcstep;
118
119 buffer_st1(vkmat_blob_data, v_offset.r, afp(rgb.r));
120 buffer_st1(vkmat_blob_data, v_offset.g, afp(rgb.g));
121 buffer_st1(vkmat_blob_data, v_offset.b, afp(rgb.b));
122#endif
123 }
124
125 if (type_to == 2) // PIXEL_BGR
126 {
127#if NCNN_image_shader
128 image3d_st1(vkmat_blob, ivec3(gx, gy, 0), rgb.b);
129 image3d_st1(vkmat_blob, ivec3(gx, gy, 1), rgb.g);
130 image3d_st1(vkmat_blob, ivec3(gx, gy, 2), rgb.r);
131#else
132 ivec3 v_offset = (gy * outw + gx) + ivec3(0, 1, 2) * outcstep;
133
134 buffer_st1(vkmat_blob_data, v_offset.r, afp(rgb.b));
135 buffer_st1(vkmat_blob_data, v_offset.g, afp(rgb.g));
136 buffer_st1(vkmat_blob_data, v_offset.b, afp(rgb.r));
137#endif
138 }
139
140 if (type_to == 3) // PIXEL_GRAY
141 {
142 // coeffs for r g b = 0.299f, 0.587f, 0.114f
143 float v = clamp(rgb.r * 0.299f + rgb.g * 0.587f + rgb.b * 0.114f, 0.f, 255.f);
144
145#if NCNN_image_shader
146 image3d_st1(vkmat_blob, ivec3(gx, gy, 0), v);
147#else
148 int v_offset = gy * outw + gx;
149
150 buffer_st1(vkmat_blob_data, v_offset, afp(v));
151#endif
152 }
153
154 if (type_to == 4) // PIXEL_RGBA
155 {
156 vec4 rgba;
157 rgba.rgb = rgb;
158 rgba.a = 255.f;
159
160#if NCNN_image_shader
161 image3d_st4(vkmat_pack4_blob, ivec3(gx, gy, 0), rgba);
162#else
163 int v_offset = gy * outw + gx;
164
165 buffer_st4(vkmat_pack4_blob_data, v_offset, afpvec4(rgba));
166#endif
167 }
168
169 if (type_to == 5) // PIXEL_BGRA
170 {
171 vec4 rgba;
172 rgba.bgr = rgb;
173 rgba.a = 255.f;
174
175#if NCNN_image_shader
176 image3d_st4(vkmat_pack4_blob, ivec3(gx, gy, 0), rgba);
177#else
178 int v_offset = gy * outw + gx;
179
180 buffer_st4(vkmat_pack4_blob_data, v_offset, afpvec4(rgba));
181#endif
182 }
183}