blob: 6d020fe70d8696a46c51080711b91637a63a66a7 [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
112BUFFER_DECLARATION(src, 1, uvec2, readonly);
113BUFFER_DECLARATION(dst, 2, uvec2, writeonly);
114
115layout(std140) uniform shader_params
116{
117 IMAGE_PARAM_DECLARATION(src);
118 IMAGE_PARAM_DECLARATION(dst);
119};
120
121/** This OpenGL ES kernel computes the matrix transposition of input matrix
122 *
123 * @param[in] src_ptr Pointer to the source matrix. Supported data types: F16
124 * @param[in] src_stride_x Stride of the source matrix in X dimension (in bytes)
125 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
126 * @param[in] src_stride_y Stride of the source matrix in Y dimension (in bytes)
127 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
128 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source matrix
129 * @param[out] dst_ptr Pointer to the destination matrix Supported data type: same as src_ptr
130 * @param[in] dst_stride_x Stride of the destination matrix in X dimension (in bytes)
131 * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes)
132 * @param[in] dst_stride_y Stride of the destination matrix in Y dimension (in bytes)
133 * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes)
134 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination matrix
135 */
136void main(void)
137{
138 // Compute source address
139 Image src = GC_CONVERT_TO_IMAGE_STRUCT(src);
140 Image dst = GC_CONVERT_TO_IMAGE_STRUCT(dst);
141
142 // Load the NxN block at (x, y)
143 vec4 u0;
144 vec4 u1;
145 vec4 u2;
146 vec4 u3;
147 uvec2 packed_s[4];
148 GC_LOAD1_2D_OFFSET(packed_s[0], src, 0, 0);
149 GC_LOAD1_2D_OFFSET(packed_s[1], src, 0, 1);
150 GC_LOAD1_2D_OFFSET(packed_s[2], src, 0, 2);
151 GC_LOAD1_2D_OFFSET(packed_s[3], src, 0, 3);
152 u0 = vec4(unpackHalf2x16(packed_s[0].x), unpackHalf2x16(packed_s[0].y));
153 u1 = vec4(unpackHalf2x16(packed_s[1].x), unpackHalf2x16(packed_s[1].y));
154 u2 = vec4(unpackHalf2x16(packed_s[2].x), unpackHalf2x16(packed_s[2].y));
155 u3 = vec4(unpackHalf2x16(packed_s[3].x), unpackHalf2x16(packed_s[3].y));
156
157 // Transpose the block
158 vec4 tmp;
159 tmp.xyz = u0.yzw;
160 u0.y = u1.x;
161 u0.z = u2.x;
162 u0.w = u3.x;
163 u1.x = tmp.x;
164 u2.x = tmp.y;
165 u3.x = tmp.z;
166 tmp.xy = u1.zw;
167 u1.z = u2.y;
168 u1.w = u3.y;
169 u2.y = tmp.x;
170 u3.y = tmp.y;
171 tmp.x = u2.w;
172 u2.w = u3.z;
173 u3.z = tmp.x;
174
175 // Store the block at (y, x)
176 uint dst_offset_in_bytes = uint(8) * uint(gl_GlobalInvocationID.y) + uint(gl_GlobalInvocationID.x) * (dst_step_y) + (dst.offset_first_element_in_bytes);
177
178 packed_s[0] = uvec2(packHalf2x16(u0.xy), packHalf2x16(u0.zw));
179 packed_s[1] = uvec2(packHalf2x16(u1.xy), packHalf2x16(u1.zw));
180 packed_s[2] = uvec2(packHalf2x16(u2.xy), packHalf2x16(u2.zw));
181 packed_s[3] = uvec2(packHalf2x16(u3.xy), packHalf2x16(u3.zw));
182 GC_STORE1(packed_s[0], dst, uint((dst_offset_in_bytes + uint(0) * dst_stride_y) >> 3));
183 GC_STORE1(packed_s[1], dst, uint((dst_offset_in_bytes + uint(1) * dst_stride_y) >> 3));
184 GC_STORE1(packed_s[2], dst, uint((dst_offset_in_bytes + uint(2) * dst_stride_y) >> 3));
185 GC_STORE1(packed_s[3], dst, uint((dst_offset_in_bytes + uint(3) * dst_stride_y) >> 3));
186}
187#endif /*ARM_COMPUTE_ENABLE_FP16*/