radeon: add tile size getter
[mesa.git] / src / mesa / drivers / dri / radeon / radeon_tile.c
1 /*
2 * Copyright (C) 2010 Maciej Cencora <m.cencora@gmail.com>
3 *
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial
16 * portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
22 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 */
27
28 #include "radeon_tile.h"
29
30 #include <stdint.h>
31 #include <string.h>
32
33 #include <main/macros.h>
34
35 #define MICRO_TILE_SIZE 32
36
37 static void micro_tile_8_x_4_8bit(const void * const src, unsigned src_pitch,
38 void * const dst, unsigned dst_pitch,
39 unsigned width, unsigned height)
40 {
41 unsigned row; /* current source row */
42 unsigned col; /* current source column */
43 unsigned k; /* number of processed tiles */
44 const unsigned tile_width = 8, tile_height = 4;
45 const unsigned tiles_in_row = (width + (tile_width - 1)) / tile_width;
46
47 k = 0;
48 for (row = 0; row < height; row += tile_height)
49 {
50 for (col = 0; col < width; col += tile_width, ++k)
51 {
52 uint8_t *src2 = (uint8_t *)src + src_pitch * row + col;
53 uint8_t *dst2 = (uint8_t *)dst + row * dst_pitch +
54 (k % tiles_in_row) * MICRO_TILE_SIZE / sizeof(uint8_t);
55 unsigned j;
56
57 for (j = 0; j < MIN2(tile_height, height - row); ++j)
58 {
59 unsigned columns = MIN2(tile_width, width - col);
60 memcpy(dst2, src2, columns * sizeof(uint8_t));
61 dst2 += tile_width;
62 src2 += src_pitch;
63 }
64 }
65 }
66 }
67
68 static void micro_tile_4_x_4_16bit(const void * const src, unsigned src_pitch,
69 void * const dst, unsigned dst_pitch,
70 unsigned width, unsigned height)
71 {
72 unsigned row; /* current source row */
73 unsigned col; /* current source column */
74 unsigned k; /* number of processed tiles */
75 const unsigned tile_width = 4, tile_height = 4;
76 const unsigned tiles_in_row = (width + (tile_width - 1)) / tile_width;
77
78 k = 0;
79 for (row = 0; row < height; row += tile_height)
80 {
81 for (col = 0; col < width; col += tile_width, ++k)
82 {
83 uint16_t *src2 = (uint16_t *)src + src_pitch * row + col;
84 uint16_t *dst2 = (uint16_t *)dst + row * dst_pitch +
85 (k % tiles_in_row) * MICRO_TILE_SIZE / sizeof(uint16_t);
86 unsigned j;
87
88 for (j = 0; j < MIN2(tile_height, height - row); ++j)
89 {
90 unsigned columns = MIN2(tile_width, width - col);
91 memcpy(dst2, src2, columns * sizeof(uint16_t));
92 dst2 += tile_width;
93 src2 += src_pitch;
94 }
95 }
96 }
97 }
98
99 static void micro_tile_8_x_2_16bit(const void * const src, unsigned src_pitch,
100 void * const dst, unsigned dst_pitch,
101 unsigned width, unsigned height)
102 {
103 unsigned row; /* current source row */
104 unsigned col; /* current source column */
105 unsigned k; /* number of processed tiles */
106 const unsigned tile_width = 8, tile_height = 2;
107 const unsigned tiles_in_row = (width + (tile_width - 1)) / tile_width;
108
109 k = 0;
110 for (row = 0; row < height; row += tile_height)
111 {
112 for (col = 0; col < width; col += tile_width, ++k)
113 {
114 uint16_t *src2 = (uint16_t *)src + src_pitch * row + col;
115 uint16_t *dst2 = (uint16_t *)dst + row * dst_pitch +
116 (k % tiles_in_row) * MICRO_TILE_SIZE / sizeof(uint16_t);
117 unsigned j;
118
119 for (j = 0; j < MIN2(tile_height, height - row); ++j)
120 {
121 unsigned columns = MIN2(tile_width, width - col);
122 memcpy(dst2, src2, columns * sizeof(uint16_t));
123 dst2 += tile_width;
124 src2 += src_pitch;
125 }
126 }
127 }
128 }
129
130 static void micro_tile_4_x_2_32bit(const void * const src, unsigned src_pitch,
131 void * const dst, unsigned dst_pitch,
132 unsigned width, unsigned height)
133 {
134 unsigned row; /* current source row */
135 unsigned col; /* current source column */
136 unsigned k; /* number of processed tiles */
137 const unsigned tile_width = 4, tile_height = 2;
138 const unsigned tiles_in_row = (width + (tile_width - 1)) / tile_width;
139
140 k = 0;
141 for (row = 0; row < height; row += tile_height)
142 {
143 for (col = 0; col < width; col += tile_width, ++k)
144 {
145 uint32_t *src2 = (uint32_t *)src + src_pitch * row + col;
146 uint32_t *dst2 = (uint32_t *)dst + row * dst_pitch +
147 (k % tiles_in_row) * MICRO_TILE_SIZE / sizeof(uint32_t);
148 unsigned j;
149
150 for (j = 0; j < MIN2(tile_height, height - row); ++j)
151 {
152 unsigned columns = MIN2(tile_width, width - col);
153 memcpy(dst2, src2, columns * sizeof(uint32_t));
154 dst2 += tile_width;
155 src2 += src_pitch;
156 }
157 }
158 }
159 }
160
161 static void micro_tile_2_x_2_64bit(const void * const src, unsigned src_pitch,
162 void * const dst, unsigned dst_pitch,
163 unsigned width, unsigned height)
164 {
165 unsigned row; /* current source row */
166 unsigned col; /* current source column */
167 unsigned k; /* number of processed tiles */
168 const unsigned tile_width = 2, tile_height = 2;
169 const unsigned tiles_in_row = (width + (tile_width - 1)) / tile_width;
170
171 k = 0;
172 for (row = 0; row < height; row += tile_height)
173 {
174 for (col = 0; col < width; col += tile_width, ++k)
175 {
176 uint64_t *src2 = (uint64_t *)src + src_pitch * row + col;
177 uint64_t *dst2 = (uint64_t *)dst + row * dst_pitch +
178 (k % tiles_in_row) * MICRO_TILE_SIZE / sizeof(uint64_t);
179 unsigned j;
180
181 for (j = 0; j < MIN2(tile_height, height - row); ++j)
182 {
183 unsigned columns = MIN2(tile_width, width - col);
184 memcpy(dst2, src2, columns * sizeof(uint64_t));
185 dst2 += tile_width;
186 src2 += src_pitch;
187 }
188 }
189 }
190 }
191
192 static void micro_tile_1_x_1_128bit(const void * src, unsigned src_pitch,
193 void * dst, unsigned dst_pitch,
194 unsigned width, unsigned height)
195 {
196 unsigned i, j;
197 const unsigned elem_size = 16; /* sizeof(uint128_t) */
198
199 for (j = 0; j < height; ++j)
200 {
201 for (i = 0; i < width; ++i)
202 {
203 memcpy(dst, src, width * elem_size);
204 dst += dst_pitch * elem_size;
205 src += src_pitch * elem_size;
206 }
207 }
208 }
209
210 void tile_image(const void * src, unsigned src_pitch,
211 void *dst, unsigned dst_pitch,
212 gl_format format, unsigned width, unsigned height)
213 {
214 assert(src_pitch >= width);
215 assert(dst_pitch >= width);
216 assert(dst_pitch * _mesa_get_format_bytes(format) % MICRO_TILE_SIZE == 0);
217
218 switch (_mesa_get_format_bytes(format))
219 {
220 case 16:
221 micro_tile_1_x_1_128bit(src, src_pitch, dst, dst_pitch, width, height);
222 break;
223 case 8:
224 micro_tile_2_x_2_64bit(src, src_pitch, dst, dst_pitch, width, height);
225 break;
226 case 4:
227 micro_tile_4_x_2_32bit(src, src_pitch, dst, dst_pitch, width, height);
228 break;
229 case 2:
230 if (_mesa_get_format_bits(format, GL_DEPTH_BITS))
231 {
232 micro_tile_4_x_4_16bit(src, src_pitch, dst, dst_pitch, width, height);
233 }
234 else
235 {
236 micro_tile_8_x_2_16bit(src, src_pitch, dst, dst_pitch, width, height);
237 }
238 break;
239 case 1:
240 micro_tile_8_x_4_8bit(src, src_pitch, dst, dst_pitch, width, height);
241 break;
242 default:
243 assert(0);
244 break;
245 }
246 }
247
248 void get_tile_size(gl_format format, unsigned *block_width, unsigned *block_height)
249 {
250 switch (_mesa_get_format_bytes(format))
251 {
252 case 16:
253 *block_width = 1;
254 *block_height = 1;
255 break;
256 case 8:
257 *block_width = 2;
258 *block_height = 2;
259 break;
260 case 4:
261 *block_width = 4;
262 *block_height = 2;
263 break;
264 case 2:
265 if (_mesa_get_format_bits(format, GL_DEPTH_BITS))
266 {
267 *block_width = 4;
268 *block_height = 4;
269 }
270 else
271 {
272 *block_width = 8;
273 *block_height = 2;
274 }
275 break;
276 case 1:
277 *block_width = 8;
278 *block_height = 4;
279 break;
280 default:
281 assert(0);
282 break;
283 }
284 }