2 * Copyright (C) 2010 Maciej Cencora <m.cencora@gmail.com>
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:
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.
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.
28 #include "radeon_tile.h"
33 #include <main/macros.h>
35 #define MICRO_TILE_SIZE 32
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
)
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
;
48 for (row
= 0; row
< height
; row
+= tile_height
)
50 for (col
= 0; col
< width
; col
+= tile_width
, ++k
)
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);
57 for (j
= 0; j
< MIN2(tile_height
, height
- row
); ++j
)
59 unsigned columns
= MIN2(tile_width
, width
- col
);
60 memcpy(dst2
, src2
, columns
* sizeof(uint8_t));
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
)
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
;
79 for (row
= 0; row
< height
; row
+= tile_height
)
81 for (col
= 0; col
< width
; col
+= tile_width
, ++k
)
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);
88 for (j
= 0; j
< MIN2(tile_height
, height
- row
); ++j
)
90 unsigned columns
= MIN2(tile_width
, width
- col
);
91 memcpy(dst2
, src2
, columns
* sizeof(uint16_t));
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
)
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
;
110 for (row
= 0; row
< height
; row
+= tile_height
)
112 for (col
= 0; col
< width
; col
+= tile_width
, ++k
)
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);
119 for (j
= 0; j
< MIN2(tile_height
, height
- row
); ++j
)
121 unsigned columns
= MIN2(tile_width
, width
- col
);
122 memcpy(dst2
, src2
, columns
* sizeof(uint16_t));
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
)
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
;
141 for (row
= 0; row
< height
; row
+= tile_height
)
143 for (col
= 0; col
< width
; col
+= tile_width
, ++k
)
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);
150 for (j
= 0; j
< MIN2(tile_height
, height
- row
); ++j
)
152 unsigned columns
= MIN2(tile_width
, width
- col
);
153 memcpy(dst2
, src2
, columns
* sizeof(uint32_t));
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
)
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
;
172 for (row
= 0; row
< height
; row
+= tile_height
)
174 for (col
= 0; col
< width
; col
+= tile_width
, ++k
)
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);
181 for (j
= 0; j
< MIN2(tile_height
, height
- row
); ++j
)
183 unsigned columns
= MIN2(tile_width
, width
- col
);
184 memcpy(dst2
, src2
, columns
* sizeof(uint64_t));
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
)
197 const unsigned elem_size
= 16; /* sizeof(uint128_t) */
199 for (j
= 0; j
< height
; ++j
)
201 for (i
= 0; i
< width
; ++i
)
203 memcpy(dst
, src
, width
* elem_size
);
204 dst
+= dst_pitch
* elem_size
;
205 src
+= src_pitch
* elem_size
;
210 void tile_image(const void * src
, unsigned src_pitch
,
211 void *dst
, unsigned dst_pitch
,
212 gl_format format
, unsigned width
, unsigned height
)
214 assert(src_pitch
>= width
);
215 assert(dst_pitch
>= width
);
216 assert(dst_pitch
* _mesa_get_format_bytes(format
) % MICRO_TILE_SIZE
== 0);
218 switch (_mesa_get_format_bytes(format
))
221 micro_tile_1_x_1_128bit(src
, src_pitch
, dst
, dst_pitch
, width
, height
);
224 micro_tile_2_x_2_64bit(src
, src_pitch
, dst
, dst_pitch
, width
, height
);
227 micro_tile_4_x_2_32bit(src
, src_pitch
, dst
, dst_pitch
, width
, height
);
230 if (_mesa_get_format_bits(format
, GL_DEPTH_BITS
))
232 micro_tile_4_x_4_16bit(src
, src_pitch
, dst
, dst_pitch
, width
, height
);
236 micro_tile_8_x_2_16bit(src
, src_pitch
, dst
, dst_pitch
, width
, height
);
240 micro_tile_8_x_4_8bit(src
, src_pitch
, dst
, dst_pitch
, width
, height
);
248 void get_tile_size(gl_format format
, unsigned *block_width
, unsigned *block_height
)
250 switch (_mesa_get_format_bytes(format
))
265 if (_mesa_get_format_bits(format
, GL_DEPTH_BITS
))