blob: c251d9529270c1227976e4b3144bb12c42d028ee [file] [log] [blame]
Anthony Barbier7068f992017-10-26 15:23:08 +01001/*
2 * Copyright (c) 2017 ARM Limited.
3 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24layout(local_size_x = LOCAL_SIZE_X, local_size_y = LOCAL_SIZE_Y, local_size_z = LOCAL_SIZE_Z) in;
25#include "helpers.h"
26
27#ifdef DATA_TYPE_FP32
28precision highp float;
29
30BUFFER_DECLARATION(src, 1, float, readonly);
31BUFFER_DECLARATION(dst, 2, float, writeonly);
32
33layout(std140) uniform shader_params
34{
35 IMAGE_PARAM_DECLARATION(src);
36 IMAGE_PARAM_DECLARATION(dst);
37};
38
39#define LOAD16(r, name, offset) \
40 r.x = LOAD4(name, offset); \
41 r.y = LOAD4(name, offset + uint(1)); \
42 r.z = LOAD4(name, offset + uint(2)); \
43 r.w = LOAD4(name, offset + uint(3))
44
45#define STORE16(name, offset, r) \
46 STORE4(name, offset, r.x); \
47 STORE4(name, offset + uint(1), r.y); \
48 STORE4(name, offset + uint(2), r.z); \
49 STORE4(name, offset + uint(3), r.w)
50
51/** This OpenGL ES kernel computes the matrix transposition of input matrix
52 *
53 * @param[in] src_ptr Pointer to the source matrix. Supported data types: F32
54 * @param[in] src_stride_x Stride of the source matrix in X dimension (in bytes)
55 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
56 * @param[in] src_stride_y Stride of the source matrix in Y dimension (in bytes)
57 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
58 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source matrix
59 * @param[out] dst_ptr Pointer to the destination matrix Supported data type: same as src_ptr
60 * @param[in] dst_stride_x Stride of the destination matrix in X dimension (in bytes)
61 * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes)
62 * @param[in] dst_stride_y Stride of the destination matrix in Y dimension (in bytes)
63 * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes)
64 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination matrix
65 */
66void main(void)
67{
68 // Compute source address
69 Image src = CONVERT_TO_IMAGE_STRUCT(src);
70 Image dst = CONVERT_TO_IMAGE_STRUCT(dst);
71
72 // Load the NxN block at (x, y)
73 vec4 u0;
74 vec4 u1;
75 vec4 u2;
76 vec4 u3;
77 LOAD16(u0, src, offset(src, 0, 0));
78 LOAD16(u1, src, offset(src, 0, 1));
79 LOAD16(u2, src, offset(src, 0, 2));
80 LOAD16(u3, src, offset(src, 0, 3));
81
82 // Transpose the block
83 vec4 tmp;
84 tmp.xyz = u0.yzw;
85 u0.y = u1.x;
86 u0.z = u2.x;
87 u0.w = u3.x;
88 u1.x = tmp.x;
89 u2.x = tmp.y;
90 u3.x = tmp.z;
91 tmp.xy = u1.zw;
92 u1.z = u2.y;
93 u1.w = u3.y;
94 u2.y = tmp.x;
95 u3.y = tmp.y;
96 tmp.x = u2.w;
97 u2.w = u3.z;
98 u3.z = tmp.x;
99
100 // Store the block at (y, x)
101 uint dst_offset_in_bytes = uint(16) * uint(gl_GlobalInvocationID.y) + uint(4) * uint(gl_GlobalInvocationID.x) * (dst.stride_y) + (dst.offset_first_element_in_bytes);
102
103 STORE16(dst, uint((dst_offset_in_bytes + uint(0) * dst.stride_y) >> 2), u0);
104 STORE16(dst, uint((dst_offset_in_bytes + uint(1) * dst.stride_y) >> 2), u1);
105 STORE16(dst, uint((dst_offset_in_bytes + uint(2) * dst.stride_y) >> 2), u2);
106 STORE16(dst, uint((dst_offset_in_bytes + uint(3) * dst.stride_y) >> 2), u3);
107}
108
109#elif defined(DATA_TYPE_FP16)
110precision mediump float;
111
Anthony Barbier7068f992017-10-26 15:23:08 +0100112layout(std140) uniform shader_params
113{
114 IMAGE_PARAM_DECLARATION(src);
115 IMAGE_PARAM_DECLARATION(dst);
116};
117
Frank Leib9d38ee2017-12-05 10:43:33 +0800118#if defined(TRANSPOSE_4X4)
119BUFFER_DECLARATION(src, 1, uvec2, readonly);
120BUFFER_DECLARATION(dst, 2, uvec2, writeonly);
121
Anthony Barbier7068f992017-10-26 15:23:08 +0100122/** This OpenGL ES kernel computes the matrix transposition of input matrix
123 *
124 * @param[in] src_ptr Pointer to the source matrix. Supported data types: F16
125 * @param[in] src_stride_x Stride of the source matrix in X dimension (in bytes)
126 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
127 * @param[in] src_stride_y Stride of the source matrix in Y dimension (in bytes)
128 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
129 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source matrix
130 * @param[out] dst_ptr Pointer to the destination matrix Supported data type: same as src_ptr
131 * @param[in] dst_stride_x Stride of the destination matrix in X dimension (in bytes)
132 * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes)
133 * @param[in] dst_stride_y Stride of the destination matrix in Y dimension (in bytes)
134 * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes)
135 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination matrix
136 */
137void main(void)
138{
139 // Compute source address
140 Image src = GC_CONVERT_TO_IMAGE_STRUCT(src);
141 Image dst = GC_CONVERT_TO_IMAGE_STRUCT(dst);
142
143 // Load the NxN block at (x, y)
144 vec4 u0;
145 vec4 u1;
146 vec4 u2;
147 vec4 u3;
148 uvec2 packed_s[4];
149 GC_LOAD1_2D_OFFSET(packed_s[0], src, 0, 0);
150 GC_LOAD1_2D_OFFSET(packed_s[1], src, 0, 1);
151 GC_LOAD1_2D_OFFSET(packed_s[2], src, 0, 2);
152 GC_LOAD1_2D_OFFSET(packed_s[3], src, 0, 3);
153 u0 = vec4(unpackHalf2x16(packed_s[0].x), unpackHalf2x16(packed_s[0].y));
154 u1 = vec4(unpackHalf2x16(packed_s[1].x), unpackHalf2x16(packed_s[1].y));
155 u2 = vec4(unpackHalf2x16(packed_s[2].x), unpackHalf2x16(packed_s[2].y));
156 u3 = vec4(unpackHalf2x16(packed_s[3].x), unpackHalf2x16(packed_s[3].y));
157
158 // Transpose the block
159 vec4 tmp;
160 tmp.xyz = u0.yzw;
161 u0.y = u1.x;
162 u0.z = u2.x;
163 u0.w = u3.x;
164 u1.x = tmp.x;
165 u2.x = tmp.y;
166 u3.x = tmp.z;
167 tmp.xy = u1.zw;
168 u1.z = u2.y;
169 u1.w = u3.y;
170 u2.y = tmp.x;
171 u3.y = tmp.y;
172 tmp.x = u2.w;
173 u2.w = u3.z;
174 u3.z = tmp.x;
175
176 // Store the block at (y, x)
177 uint dst_offset_in_bytes = uint(8) * uint(gl_GlobalInvocationID.y) + uint(gl_GlobalInvocationID.x) * (dst_step_y) + (dst.offset_first_element_in_bytes);
178
179 packed_s[0] = uvec2(packHalf2x16(u0.xy), packHalf2x16(u0.zw));
180 packed_s[1] = uvec2(packHalf2x16(u1.xy), packHalf2x16(u1.zw));
181 packed_s[2] = uvec2(packHalf2x16(u2.xy), packHalf2x16(u2.zw));
182 packed_s[3] = uvec2(packHalf2x16(u3.xy), packHalf2x16(u3.zw));
183 GC_STORE1(packed_s[0], dst, uint((dst_offset_in_bytes + uint(0) * dst_stride_y) >> 3));
184 GC_STORE1(packed_s[1], dst, uint((dst_offset_in_bytes + uint(1) * dst_stride_y) >> 3));
185 GC_STORE1(packed_s[2], dst, uint((dst_offset_in_bytes + uint(2) * dst_stride_y) >> 3));
186 GC_STORE1(packed_s[3], dst, uint((dst_offset_in_bytes + uint(3) * dst_stride_y) >> 3));
187}
Frank Leib9d38ee2017-12-05 10:43:33 +0800188#elif defined(TRANSPOSE_8X8) /* TRANSPOSE_4X4 */
189BUFFER_DECLARATION(src, 1, uvec4, readonly);
190BUFFER_DECLARATION(dst, 2, uvec4, writeonly);
191
192#define SWAP_ROW(u0, l0) \
193 { \
194 tmp_swap = u0; \
195 u0 = l0; \
196 l0 = tmp_swap; \
197 }
198
199#define SWAP_4x4(u0, u1, u2, u3, l0, l1, l2, l3) \
200 { \
201 vec4 tmp_swap; \
202 SWAP_ROW(u0, l0); \
203 SWAP_ROW(u1, l1); \
204 SWAP_ROW(u2, l2); \
205 SWAP_ROW(u3, l3); \
206 }
207
208#define TRANSPOSE_4x4(u0, u1, u2, u3) \
209 { \
210 vec4 tmp; \
211 tmp.xyz = u0.yzw; \
212 u0.y = u1.x; \
213 u0.z = u2.x; \
214 u0.w = u3.x; \
215 u1.x = tmp.x; \
216 u2.x = tmp.y; \
217 u3.x = tmp.z; \
218 tmp.xy = u1.zw; \
219 u1.z = u2.y; \
220 u1.w = u3.y; \
221 u2.y = tmp.x; \
222 u3.y = tmp.y; \
223 tmp.x = u2.w; \
224 u2.w = u3.z; \
225 u3.z = tmp.x; \
226 }
227
228/** This OpenGL ES kernel computes the matrix transposition of input matrix
229 *
230 * @param[in] src_ptr Pointer to the source matrix. Supported data types:F16
231 * @param[in] src_stride_x Stride of the source matrix in X dimension (in bytes)
232 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
233 * @param[in] src_stride_y Stride of the source matrix in Y dimension (in bytes)
234 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
235 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source matrix
236 * @param[out] dst_ptr Pointer to the destination matrix Supported data type: same as src_ptr
237 * @param[in] dst_stride_x Stride of the destination matrix in X dimension (in bytes)
238 * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes)
239 * @param[in] dst_stride_y Stride of the destination matrix in Y dimension (in bytes)
240 * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes)
241 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination matrix
242 */
243void main(void)
244{
245 // Compute source address
246 Image src = GC_CONVERT_TO_IMAGE_STRUCT(src);
247 Image dst = GC_CONVERT_TO_IMAGE_STRUCT(dst);
248
249 vec4 u[8][2];
250
251 uvec4 packed_s[8];
252
253 for(int i = 0; i < 8; i++)
254 {
255 GC_LOAD1_2D_OFFSET(packed_s[i], src, 0, i);
256 u[i][0] = vec4(unpackHalf2x16(packed_s[i].x), unpackHalf2x16(packed_s[i].y));
257 u[i][1] = vec4(unpackHalf2x16(packed_s[i].z), unpackHalf2x16(packed_s[i].w));
258 }
259
260 // Transpose the block
261 TRANSPOSE_4x4(u[0][0], u[1][0], u[2][0], u[3][0]);
262 TRANSPOSE_4x4(u[0][1], u[1][1], u[2][1], u[3][1]);
263 TRANSPOSE_4x4(u[4][0], u[5][0], u[6][0], u[7][0]);
264 TRANSPOSE_4x4(u[4][1], u[5][1], u[6][1], u[7][1]);
265 SWAP_4x4(u[0][1], u[1][1], u[2][1], u[3][1], u[4][0], u[5][0], u[6][0], u[7][0]);
266
267 // Store the block at (y, x)
268 uint dst_offset_in_bytes = uint(16) * uint(gl_GlobalInvocationID.y) + uint(gl_GlobalInvocationID.x) * (dst_step_y) + (dst.offset_first_element_in_bytes);
269
270 for(int i = 0; i < 8; i++)
271 {
272 packed_s[i] = uvec4(packHalf2x16(u[i][0].xy), packHalf2x16(u[i][0].zw), packHalf2x16(u[i][1].xy), packHalf2x16(u[i][1].zw));
273 GC_STORE1(packed_s[i], dst, uint((dst_offset_in_bytes + uint(i) * dst_stride_y) >> 4));
274 }
275}
276#endif /* TRANSPOSE_4X4 */
Anthony Barbier7068f992017-10-26 15:23:08 +0100277#endif /*ARM_COMPUTE_ENABLE_FP16*/