blob: f8ad30312c13ac87785b4c4052553cee25dc4430 [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
steli017d473dd2017-12-06 18:53:32 +080027#define SWAP_ROW_func(u0, l0) \
28 { \
29 tmp_swap = u0; \
30 u0 = l0; \
31 l0 = tmp_swap; \
32 }
Anthony Barbier7068f992017-10-26 15:23:08 +010033
steli017d473dd2017-12-06 18:53:32 +080034#define SWAP_4x4_func(u0, u1, u2, u3, l0, l1, l2, l3) \
35 { \
36 vec4 tmp_swap; \
37 SWAP_ROW_func(u0, l0); \
38 SWAP_ROW_func(u1, l1); \
39 SWAP_ROW_func(u2, l2); \
40 SWAP_ROW_func(u3, l3); \
41 }
Anthony Barbier7068f992017-10-26 15:23:08 +010042
steli017d473dd2017-12-06 18:53:32 +080043#define TRANSPOSE_4x4_func(u0, u1, u2, u3) \
44 { \
45 mat4x4 matin, matout; \
46 matin[0] = u0; \
47 matin[1] = u1; \
48 matin[2] = u2; \
49 matin[3] = u3; \
50 matout = transpose(matin); \
51 u0 = matout[0]; \
52 u1 = matout[1]; \
53 u2 = matout[2]; \
54 u3 = matout[3]; \
55 }
Anthony Barbier7068f992017-10-26 15:23:08 +010056
57/** This OpenGL ES kernel computes the matrix transposition of input matrix
58 *
steli017d473dd2017-12-06 18:53:32 +080059 * @note The data type must be passed at compile time using "#define DATA_TYPE_NAME". e.g. "#define DATA_TYPE_FP32"
60 * @note Optimization name must be passed using "#define OPTIMIZATION_NAME" for F16. e.g. "#define TRANSPOSE_8X8"
61 *
62 * @param[in] src_ptr Pointer to the source matrix. Supported data types: F32/F16
Anthony Barbier7068f992017-10-26 15:23:08 +010063 * @param[in] src_stride_x Stride of the source matrix in X dimension (in bytes)
64 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
65 * @param[in] src_stride_y Stride of the source matrix in Y dimension (in bytes)
66 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
67 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source matrix
68 * @param[out] dst_ptr Pointer to the destination matrix Supported data type: same as src_ptr
69 * @param[in] dst_stride_x Stride of the destination matrix in X dimension (in bytes)
70 * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes)
71 * @param[in] dst_stride_y Stride of the destination matrix in Y dimension (in bytes)
72 * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes)
73 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination matrix
74 */
steli017d473dd2017-12-06 18:53:32 +080075
76layout(std140) uniform shader_params
77{
78 IMAGE_PARAM_DECLARATION(src);
79 IMAGE_PARAM_DECLARATION(dst);
80};
81
82#ifdef DATA_TYPE_FP32
83precision highp float;
84
85BUFFER_DECLARATION(src, 1, float, readonly);
86BUFFER_DECLARATION(dst, 2, float, writeonly);
87
88#define LOAD16(r, name, offset) \
89 { \
90 r.x = LOAD4(name, offset); \
91 r.y = LOAD4(name, offset + uint(1)); \
92 r.z = LOAD4(name, offset + uint(2)); \
93 r.w = LOAD4(name, offset + uint(3)); \
94 }
95
96#define STORE16(name, offset, r) \
97 { \
98 STORE4(name, offset, r.x); \
99 STORE4(name, offset + uint(1), r.y); \
100 STORE4(name, offset + uint(2), r.z); \
101 STORE4(name, offset + uint(3), r.w); \
102 }
103
Anthony Barbier7068f992017-10-26 15:23:08 +0100104void main(void)
105{
steli017d473dd2017-12-06 18:53:32 +0800106 // compute source address
Anthony Barbier7068f992017-10-26 15:23:08 +0100107 Image src = CONVERT_TO_IMAGE_STRUCT(src);
108 Image dst = CONVERT_TO_IMAGE_STRUCT(dst);
109
steli017d473dd2017-12-06 18:53:32 +0800110 // load the NxN block at (x, y)
Anthony Barbier7068f992017-10-26 15:23:08 +0100111 vec4 u0;
112 vec4 u1;
113 vec4 u2;
114 vec4 u3;
115 LOAD16(u0, src, offset(src, 0, 0));
116 LOAD16(u1, src, offset(src, 0, 1));
117 LOAD16(u2, src, offset(src, 0, 2));
118 LOAD16(u3, src, offset(src, 0, 3));
119
steli017d473dd2017-12-06 18:53:32 +0800120 // transpose the block
121 TRANSPOSE_4x4_func(u0, u1, u2, u3);
Anthony Barbier7068f992017-10-26 15:23:08 +0100122
steli017d473dd2017-12-06 18:53:32 +0800123 // store the block at (y, x)
Anthony Barbier7068f992017-10-26 15:23:08 +0100124 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);
125
126 STORE16(dst, uint((dst_offset_in_bytes + uint(0) * dst.stride_y) >> 2), u0);
127 STORE16(dst, uint((dst_offset_in_bytes + uint(1) * dst.stride_y) >> 2), u1);
128 STORE16(dst, uint((dst_offset_in_bytes + uint(2) * dst.stride_y) >> 2), u2);
129 STORE16(dst, uint((dst_offset_in_bytes + uint(3) * dst.stride_y) >> 2), u3);
130}
131
steli017d473dd2017-12-06 18:53:32 +0800132#elif defined(DATA_TYPE_FP16) /* DATA_TYPE_FP32 */
Anthony Barbier7068f992017-10-26 15:23:08 +0100133precision mediump float;
134
Frank Leib9d38ee2017-12-05 10:43:33 +0800135#if defined(TRANSPOSE_4X4)
steli017d473dd2017-12-06 18:53:32 +0800136
Frank Leib9d38ee2017-12-05 10:43:33 +0800137BUFFER_DECLARATION(src, 1, uvec2, readonly);
138BUFFER_DECLARATION(dst, 2, uvec2, writeonly);
139
Anthony Barbier7068f992017-10-26 15:23:08 +0100140void main(void)
141{
steli017d473dd2017-12-06 18:53:32 +0800142 // compute source address
Anthony Barbier7068f992017-10-26 15:23:08 +0100143 Image src = GC_CONVERT_TO_IMAGE_STRUCT(src);
144 Image dst = GC_CONVERT_TO_IMAGE_STRUCT(dst);
145
steli017d473dd2017-12-06 18:53:32 +0800146 // load the NxN block at (x, y)
Anthony Barbier7068f992017-10-26 15:23:08 +0100147 vec4 u0;
148 vec4 u1;
149 vec4 u2;
150 vec4 u3;
151 uvec2 packed_s[4];
steli017d473dd2017-12-06 18:53:32 +0800152
Anthony Barbier7068f992017-10-26 15:23:08 +0100153 GC_LOAD1_2D_OFFSET(packed_s[0], src, 0, 0);
154 GC_LOAD1_2D_OFFSET(packed_s[1], src, 0, 1);
155 GC_LOAD1_2D_OFFSET(packed_s[2], src, 0, 2);
156 GC_LOAD1_2D_OFFSET(packed_s[3], src, 0, 3);
steli017d473dd2017-12-06 18:53:32 +0800157
Anthony Barbier7068f992017-10-26 15:23:08 +0100158 u0 = vec4(unpackHalf2x16(packed_s[0].x), unpackHalf2x16(packed_s[0].y));
159 u1 = vec4(unpackHalf2x16(packed_s[1].x), unpackHalf2x16(packed_s[1].y));
160 u2 = vec4(unpackHalf2x16(packed_s[2].x), unpackHalf2x16(packed_s[2].y));
161 u3 = vec4(unpackHalf2x16(packed_s[3].x), unpackHalf2x16(packed_s[3].y));
162
steli017d473dd2017-12-06 18:53:32 +0800163 // transpose the block
164 TRANSPOSE_4x4_func(u0, u1, u2, u3);
Anthony Barbier7068f992017-10-26 15:23:08 +0100165
steli017d473dd2017-12-06 18:53:32 +0800166 // store the block at (y, x)
Anthony Barbier7068f992017-10-26 15:23:08 +0100167 uint dst_offset_in_bytes = uint(8) * uint(gl_GlobalInvocationID.y) + uint(gl_GlobalInvocationID.x) * (dst_step_y) + (dst.offset_first_element_in_bytes);
steli017d473dd2017-12-06 18:53:32 +0800168 dst.current_offset = dst_offset_in_bytes;
Anthony Barbier7068f992017-10-26 15:23:08 +0100169
170 packed_s[0] = uvec2(packHalf2x16(u0.xy), packHalf2x16(u0.zw));
171 packed_s[1] = uvec2(packHalf2x16(u1.xy), packHalf2x16(u1.zw));
172 packed_s[2] = uvec2(packHalf2x16(u2.xy), packHalf2x16(u2.zw));
173 packed_s[3] = uvec2(packHalf2x16(u3.xy), packHalf2x16(u3.zw));
steli017d473dd2017-12-06 18:53:32 +0800174
175 GC_STORE1_2D_OFFSET(packed_s[0], dst, 0, 0);
176 GC_STORE1_2D_OFFSET(packed_s[1], dst, 0, 1);
177 GC_STORE1_2D_OFFSET(packed_s[2], dst, 0, 2);
178 GC_STORE1_2D_OFFSET(packed_s[3], dst, 0, 3);
Anthony Barbier7068f992017-10-26 15:23:08 +0100179}
steli017d473dd2017-12-06 18:53:32 +0800180
Frank Leib9d38ee2017-12-05 10:43:33 +0800181#elif defined(TRANSPOSE_8X8) /* TRANSPOSE_4X4 */
steli017d473dd2017-12-06 18:53:32 +0800182
Frank Leib9d38ee2017-12-05 10:43:33 +0800183BUFFER_DECLARATION(src, 1, uvec4, readonly);
184BUFFER_DECLARATION(dst, 2, uvec4, writeonly);
185
Frank Leib9d38ee2017-12-05 10:43:33 +0800186void main(void)
187{
steli017d473dd2017-12-06 18:53:32 +0800188 // compute source address
Frank Leib9d38ee2017-12-05 10:43:33 +0800189 Image src = GC_CONVERT_TO_IMAGE_STRUCT(src);
190 Image dst = GC_CONVERT_TO_IMAGE_STRUCT(dst);
191
steli017d473dd2017-12-06 18:53:32 +0800192 vec4 u[8][2];
Frank Leib9d38ee2017-12-05 10:43:33 +0800193 uvec4 packed_s[8];
194
195 for(int i = 0; i < 8; i++)
196 {
197 GC_LOAD1_2D_OFFSET(packed_s[i], src, 0, i);
198 u[i][0] = vec4(unpackHalf2x16(packed_s[i].x), unpackHalf2x16(packed_s[i].y));
199 u[i][1] = vec4(unpackHalf2x16(packed_s[i].z), unpackHalf2x16(packed_s[i].w));
200 }
201
steli017d473dd2017-12-06 18:53:32 +0800202 // transpose the block
203 TRANSPOSE_4x4_func(u[0][0], u[1][0], u[2][0], u[3][0]);
204 TRANSPOSE_4x4_func(u[0][1], u[1][1], u[2][1], u[3][1]);
205 TRANSPOSE_4x4_func(u[4][0], u[5][0], u[6][0], u[7][0]);
206 TRANSPOSE_4x4_func(u[4][1], u[5][1], u[6][1], u[7][1]);
207 SWAP_4x4_func(u[0][1], u[1][1], u[2][1], u[3][1], u[4][0], u[5][0], u[6][0], u[7][0]);
Frank Leib9d38ee2017-12-05 10:43:33 +0800208
steli017d473dd2017-12-06 18:53:32 +0800209 // store the block at (y, x)
Frank Leib9d38ee2017-12-05 10:43:33 +0800210 uint dst_offset_in_bytes = uint(16) * uint(gl_GlobalInvocationID.y) + uint(gl_GlobalInvocationID.x) * (dst_step_y) + (dst.offset_first_element_in_bytes);
steli017d473dd2017-12-06 18:53:32 +0800211 dst.current_offset = dst_offset_in_bytes;
Frank Leib9d38ee2017-12-05 10:43:33 +0800212
213 for(int i = 0; i < 8; i++)
214 {
215 packed_s[i] = uvec4(packHalf2x16(u[i][0].xy), packHalf2x16(u[i][0].zw), packHalf2x16(u[i][1].xy), packHalf2x16(u[i][1].zw));
steli017d473dd2017-12-06 18:53:32 +0800216 GC_STORE1_2D_OFFSET(packed_s[i], dst, 0, i);
Frank Leib9d38ee2017-12-05 10:43:33 +0800217 }
218}
steli017d473dd2017-12-06 18:53:32 +0800219
220#elif defined(TRANSPOSE_8X8_SQUARE) /* TRANSPOSE_4X4 */
221
222BUFFER_DECLARATION(src, 1, uvec4, readonly);
223BUFFER_DECLARATION(dst, 2, uvec4, writeonly);
224
225void main(void)
226{
227 Image src = GC_CONVERT_TO_IMAGE_STRUCT(src);
228 Image dst = GC_CONVERT_TO_IMAGE_STRUCT(dst);
229
230 if(gl_GlobalInvocationID.x <= gl_GlobalInvocationID.y)
231 {
232 uint blk1_offset_in_bytes = src.current_offset;
233 uint blk2_offset_in_bytes = uint(16) * uint(gl_GlobalInvocationID.y) + uint(gl_GlobalInvocationID.x) * (dst_step_y) + (dst.offset_first_element_in_bytes);
234
235 // load block1
236 vec4 u1[8][2];
237 uvec4 packed_s[8];
238
239 src.current_offset = blk1_offset_in_bytes;
240 for(int i = 0; i < 8; i++)
241 {
242 GC_LOAD1_2D_OFFSET(packed_s[i], src, 0, i);
243 u1[i][0] = vec4(unpackHalf2x16(packed_s[i].x), unpackHalf2x16(packed_s[i].y));
244 u1[i][1] = vec4(unpackHalf2x16(packed_s[i].z), unpackHalf2x16(packed_s[i].w));
245 }
246
247 // transpose block1
248 TRANSPOSE_4x4_func(u1[0][0], u1[1][0], u1[2][0], u1[3][0]);
249 TRANSPOSE_4x4_func(u1[0][1], u1[1][1], u1[2][1], u1[3][1]);
250 TRANSPOSE_4x4_func(u1[4][0], u1[5][0], u1[6][0], u1[7][0]);
251 TRANSPOSE_4x4_func(u1[4][1], u1[5][1], u1[6][1], u1[7][1]);
252 SWAP_4x4_func(u1[0][1], u1[1][1], u1[2][1], u1[3][1], u1[4][0], u1[5][0], u1[6][0], u1[7][0]);
253
254 // write to block2
255 dst.current_offset = blk2_offset_in_bytes;
256 for(int i = 0; i < 8; i++)
257 {
258 packed_s[i] = uvec4(packHalf2x16(u1[i][0].xy), packHalf2x16(u1[i][0].zw), packHalf2x16(u1[i][1].xy), packHalf2x16(u1[i][1].zw));
259 GC_STORE1_2D_OFFSET(packed_s[i], dst, 0, i);
260 }
261
262 // load block2
263 vec4 u2[8][2];
264
265 src.current_offset = blk2_offset_in_bytes;
266 for(int i = 0; i < 8; i++)
267 {
268 GC_LOAD1_2D_OFFSET(packed_s[i], src, 0, i);
269 u2[i][0] = vec4(unpackHalf2x16(packed_s[i].x), unpackHalf2x16(packed_s[i].y));
270 u2[i][1] = vec4(unpackHalf2x16(packed_s[i].z), unpackHalf2x16(packed_s[i].w));
271 }
272
273 // transpose block2
274 TRANSPOSE_4x4_func(u2[0][0], u2[1][0], u2[2][0], u2[3][0]);
275 TRANSPOSE_4x4_func(u2[0][1], u2[1][1], u2[2][1], u2[3][1]);
276 TRANSPOSE_4x4_func(u2[4][0], u2[5][0], u2[6][0], u2[7][0]);
277 TRANSPOSE_4x4_func(u2[4][1], u2[5][1], u2[6][1], u2[7][1]);
278 SWAP_4x4_func(u2[0][1], u2[1][1], u2[2][1], u2[3][1], u2[4][0], u2[5][0], u2[6][0], u2[7][0]);
279
280 // write to block1
281 dst.current_offset = blk1_offset_in_bytes;
282 for(int i = 0; i < 8; i++)
283 {
284 packed_s[i] = uvec4(packHalf2x16(u2[i][0].xy), packHalf2x16(u2[i][0].zw), packHalf2x16(u2[i][1].xy), packHalf2x16(u2[i][1].zw));
285 GC_STORE1_2D_OFFSET(packed_s[i], dst, 0, i);
286 }
287 }
288}
289
Frank Leib9d38ee2017-12-05 10:43:33 +0800290#endif /* TRANSPOSE_4X4 */
steli017d473dd2017-12-06 18:53:32 +0800291
292#endif /* DATA_TYPE_FP32 */