2 * Mesa 3-D graphics library
4 * Copyright 2012 Intel Corporation
5 * Copyright 2013 Google
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sublicense, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 * Chad Versace <chad.versace@linux.intel.com>
29 * Frank Henigman <fjhenigman@google.com>
34 #include "util/macros.h"
36 #include "brw_context.h"
37 #include "intel_tiled_memcpy.h"
39 #if defined(__SSSE3__)
40 #include <tmmintrin.h>
41 #elif defined(__SSE2__)
42 #include <emmintrin.h>
46 #define FILE_DEBUG_FLAG DEBUG_TEXTURE
48 #define ALIGN_DOWN(a, b) ROUND_DOWN_TO(a, b)
49 #define ALIGN_UP(a, b) ALIGN(a, b)
51 /* Tile dimensions. Width and span are in bytes, height is in pixels (i.e.
52 * unitless). A "span" is the most number of bytes we can copy from linear
53 * to tiled without needing to calculate a new destination address.
55 static const uint32_t xtile_width
= 512;
56 static const uint32_t xtile_height
= 8;
57 static const uint32_t xtile_span
= 64;
58 static const uint32_t ytile_width
= 128;
59 static const uint32_t ytile_height
= 32;
60 static const uint32_t ytile_span
= 16;
62 #if defined(__SSSE3__)
63 static const uint8_t rgba8_permutation
[16] =
64 { 2,1,0,3, 6,5,4,7, 10,9,8,11, 14,13,12,15 };
66 /* NOTE: dst must be 16-byte aligned. src may be unaligned. */
68 rgba8_copy_16_aligned_dst(void *dst
, const void *src
)
70 _mm_store_si128((__m128i
*)(dst
),
71 _mm_shuffle_epi8(_mm_loadu_si128((__m128i
*)(src
)),
72 *(__m128i
*)rgba8_permutation
));
75 /* NOTE: src must be 16-byte aligned. dst may be unaligned. */
77 rgba8_copy_16_aligned_src(void *dst
, const void *src
)
79 _mm_storeu_si128((__m128i
*)(dst
),
80 _mm_shuffle_epi8(_mm_load_si128((__m128i
*)(src
)),
81 *(__m128i
*)rgba8_permutation
));
84 #elif defined(__SSE2__)
86 rgba8_copy_16_aligned_dst(void *dst
, const void *src
)
88 __m128i srcreg
, dstreg
, agmask
, ag
, rb
, br
;
90 agmask
= _mm_set1_epi32(0xFF00FF00);
91 srcreg
= _mm_loadu_si128((__m128i
*)src
);
93 rb
= _mm_andnot_si128(agmask
, srcreg
);
94 ag
= _mm_and_si128(agmask
, srcreg
);
95 br
= _mm_shufflehi_epi16(_mm_shufflelo_epi16(rb
, _MM_SHUFFLE(2, 3, 0, 1)),
96 _MM_SHUFFLE(2, 3, 0, 1));
97 dstreg
= _mm_or_si128(ag
, br
);
99 _mm_store_si128((__m128i
*)dst
, dstreg
);
103 rgba8_copy_16_aligned_src(void *dst
, const void *src
)
105 __m128i srcreg
, dstreg
, agmask
, ag
, rb
, br
;
107 agmask
= _mm_set1_epi32(0xFF00FF00);
108 srcreg
= _mm_load_si128((__m128i
*)src
);
110 rb
= _mm_andnot_si128(agmask
, srcreg
);
111 ag
= _mm_and_si128(agmask
, srcreg
);
112 br
= _mm_shufflehi_epi16(_mm_shufflelo_epi16(rb
, _MM_SHUFFLE(2, 3, 0, 1)),
113 _MM_SHUFFLE(2, 3, 0, 1));
114 dstreg
= _mm_or_si128(ag
, br
);
116 _mm_storeu_si128((__m128i
*)dst
, dstreg
);
122 * Copy RGBA to BGRA - swap R and B, with the destination 16-byte aligned.
125 rgba8_copy_aligned_dst(void *dst
, const void *src
, size_t bytes
)
128 uint8_t const *s
= src
;
130 #if defined(__SSSE3__) || defined(__SSE2__)
132 assert(!(((uintptr_t)dst
) & 0xf));
133 rgba8_copy_16_aligned_dst(d
+ 0, s
+ 0);
138 assert(!(((uintptr_t)dst
) & 0xf));
139 rgba8_copy_16_aligned_dst(d
+ 0, s
+ 0);
140 rgba8_copy_16_aligned_dst(d
+16, s
+16);
141 rgba8_copy_16_aligned_dst(d
+32, s
+32);
142 rgba8_copy_16_aligned_dst(d
+48, s
+48);
160 * Copy RGBA to BGRA - swap R and B, with the source 16-byte aligned.
163 rgba8_copy_aligned_src(void *dst
, const void *src
, size_t bytes
)
166 uint8_t const *s
= src
;
168 #if defined(__SSSE3__) || defined(__SSE2__)
170 assert(!(((uintptr_t)src
) & 0xf));
171 rgba8_copy_16_aligned_src(d
+ 0, s
+ 0);
176 assert(!(((uintptr_t)src
) & 0xf));
177 rgba8_copy_16_aligned_src(d
+ 0, s
+ 0);
178 rgba8_copy_16_aligned_src(d
+16, s
+16);
179 rgba8_copy_16_aligned_src(d
+32, s
+32);
180 rgba8_copy_16_aligned_src(d
+48, s
+48);
198 * Each row from y0 to y1 is copied in three parts: [x0,x1), [x1,x2), [x2,x3).
199 * These ranges are in bytes, i.e. pixels * bytes-per-pixel.
200 * The first and last ranges must be shorter than a "span" (the longest linear
201 * stretch within a tile) and the middle must equal a whole number of spans.
202 * Ranges may be empty. The region copied must land entirely within one tile.
203 * 'dst' is the start of the tile and 'src' is the corresponding
204 * address to copy from, though copying begins at (x0, y0).
205 * To enable swizzling 'swizzle_bit' must be 1<<6, otherwise zero.
206 * Swizzling flips bit 6 in the copy destination offset, when certain other
207 * bits are set in it.
209 typedef void (*tile_copy_fn
)(uint32_t x0
, uint32_t x1
, uint32_t x2
, uint32_t x3
,
210 uint32_t y0
, uint32_t y1
,
211 char *dst
, const char *src
,
212 int32_t linear_pitch
,
213 uint32_t swizzle_bit
,
214 mem_copy_fn mem_copy
);
217 * Copy texture data from linear to X tile layout.
219 * \copydoc tile_copy_fn
222 linear_to_xtiled(uint32_t x0
, uint32_t x1
, uint32_t x2
, uint32_t x3
,
223 uint32_t y0
, uint32_t y1
,
224 char *dst
, const char *src
,
226 uint32_t swizzle_bit
,
227 mem_copy_fn mem_copy
)
229 /* The copy destination offset for each range copied is the sum of
230 * an X offset 'x0' or 'xo' and a Y offset 'yo.'
234 src
+= (ptrdiff_t)y0
* src_pitch
;
236 for (yo
= y0
* xtile_width
; yo
< y1
* xtile_width
; yo
+= xtile_width
) {
237 /* Bits 9 and 10 of the copy destination offset control swizzling.
238 * Only 'yo' contributes to those bits in the total offset,
239 * so calculate 'swizzle' just once per row.
240 * Move bits 9 and 10 three and four places respectively down
241 * to bit 6 and xor them.
243 uint32_t swizzle
= ((yo
>> 3) ^ (yo
>> 4)) & swizzle_bit
;
245 mem_copy(dst
+ ((x0
+ yo
) ^ swizzle
), src
+ x0
, x1
- x0
);
247 for (xo
= x1
; xo
< x2
; xo
+= xtile_span
) {
248 mem_copy(dst
+ ((xo
+ yo
) ^ swizzle
), src
+ xo
, xtile_span
);
251 mem_copy(dst
+ ((xo
+ yo
) ^ swizzle
), src
+ x2
, x3
- x2
);
258 * Copy texture data from linear to Y tile layout.
260 * \copydoc tile_copy_fn
263 linear_to_ytiled(uint32_t x0
, uint32_t x1
, uint32_t x2
, uint32_t x3
,
264 uint32_t y0
, uint32_t y1
,
265 char *dst
, const char *src
,
267 uint32_t swizzle_bit
,
268 mem_copy_fn mem_copy
)
270 /* Y tiles consist of columns that are 'ytile_span' wide (and the same height
271 * as the tile). Thus the destination offset for (x,y) is the sum of:
272 * (x % column_width) // position within column
273 * (x / column_width) * bytes_per_column // column number * bytes per column
276 * The copy destination offset for each range copied is the sum of
277 * an X offset 'xo0' or 'xo' and a Y offset 'yo.'
279 const uint32_t column_width
= ytile_span
;
280 const uint32_t bytes_per_column
= column_width
* ytile_height
;
282 uint32_t xo0
= (x0
% ytile_span
) + (x0
/ ytile_span
) * bytes_per_column
;
283 uint32_t xo1
= (x1
% ytile_span
) + (x1
/ ytile_span
) * bytes_per_column
;
285 /* Bit 9 of the destination offset control swizzling.
286 * Only the X offset contributes to bit 9 of the total offset,
287 * so swizzle can be calculated in advance for these X positions.
288 * Move bit 9 three places down to bit 6.
290 uint32_t swizzle0
= (xo0
>> 3) & swizzle_bit
;
291 uint32_t swizzle1
= (xo1
>> 3) & swizzle_bit
;
295 src
+= (ptrdiff_t)y0
* src_pitch
;
297 for (yo
= y0
* column_width
; yo
< y1
* column_width
; yo
+= column_width
) {
299 uint32_t swizzle
= swizzle1
;
301 mem_copy(dst
+ ((xo0
+ yo
) ^ swizzle0
), src
+ x0
, x1
- x0
);
303 /* Step by spans/columns. As it happens, the swizzle bit flips
304 * at each step so we don't need to calculate it explicitly.
306 for (x
= x1
; x
< x2
; x
+= ytile_span
) {
307 mem_copy(dst
+ ((xo
+ yo
) ^ swizzle
), src
+ x
, ytile_span
);
308 xo
+= bytes_per_column
;
309 swizzle
^= swizzle_bit
;
312 mem_copy(dst
+ ((xo
+ yo
) ^ swizzle
), src
+ x2
, x3
- x2
);
319 * Copy texture data from X tile layout to linear.
321 * \copydoc tile_copy_fn
324 xtiled_to_linear(uint32_t x0
, uint32_t x1
, uint32_t x2
, uint32_t x3
,
325 uint32_t y0
, uint32_t y1
,
326 char *dst
, const char *src
,
328 uint32_t swizzle_bit
,
329 mem_copy_fn mem_copy
)
331 /* The copy destination offset for each range copied is the sum of
332 * an X offset 'x0' or 'xo' and a Y offset 'yo.'
336 dst
+= (ptrdiff_t)y0
* dst_pitch
;
338 for (yo
= y0
* xtile_width
; yo
< y1
* xtile_width
; yo
+= xtile_width
) {
339 /* Bits 9 and 10 of the copy destination offset control swizzling.
340 * Only 'yo' contributes to those bits in the total offset,
341 * so calculate 'swizzle' just once per row.
342 * Move bits 9 and 10 three and four places respectively down
343 * to bit 6 and xor them.
345 uint32_t swizzle
= ((yo
>> 3) ^ (yo
>> 4)) & swizzle_bit
;
347 mem_copy(dst
+ x0
, src
+ ((x0
+ yo
) ^ swizzle
), x1
- x0
);
349 for (xo
= x1
; xo
< x2
; xo
+= xtile_span
) {
350 mem_copy(dst
+ xo
, src
+ ((xo
+ yo
) ^ swizzle
), xtile_span
);
353 mem_copy(dst
+ x2
, src
+ ((xo
+ yo
) ^ swizzle
), x3
- x2
);
360 * Copy texture data from Y tile layout to linear.
362 * \copydoc tile_copy_fn
365 ytiled_to_linear(uint32_t x0
, uint32_t x1
, uint32_t x2
, uint32_t x3
,
366 uint32_t y0
, uint32_t y1
,
367 char *dst
, const char *src
,
369 uint32_t swizzle_bit
,
370 mem_copy_fn mem_copy
)
372 /* Y tiles consist of columns that are 'ytile_span' wide (and the same height
373 * as the tile). Thus the destination offset for (x,y) is the sum of:
374 * (x % column_width) // position within column
375 * (x / column_width) * bytes_per_column // column number * bytes per column
378 * The copy destination offset for each range copied is the sum of
379 * an X offset 'xo0' or 'xo' and a Y offset 'yo.'
381 const uint32_t column_width
= ytile_span
;
382 const uint32_t bytes_per_column
= column_width
* ytile_height
;
384 uint32_t xo0
= (x0
% ytile_span
) + (x0
/ ytile_span
) * bytes_per_column
;
385 uint32_t xo1
= (x1
% ytile_span
) + (x1
/ ytile_span
) * bytes_per_column
;
387 /* Bit 9 of the destination offset control swizzling.
388 * Only the X offset contributes to bit 9 of the total offset,
389 * so swizzle can be calculated in advance for these X positions.
390 * Move bit 9 three places down to bit 6.
392 uint32_t swizzle0
= (xo0
>> 3) & swizzle_bit
;
393 uint32_t swizzle1
= (xo1
>> 3) & swizzle_bit
;
397 dst
+= (ptrdiff_t)y0
* dst_pitch
;
399 for (yo
= y0
* column_width
; yo
< y1
* column_width
; yo
+= column_width
) {
401 uint32_t swizzle
= swizzle1
;
403 mem_copy(dst
+ x0
, src
+ ((xo0
+ yo
) ^ swizzle0
), x1
- x0
);
405 /* Step by spans/columns. As it happens, the swizzle bit flips
406 * at each step so we don't need to calculate it explicitly.
408 for (x
= x1
; x
< x2
; x
+= ytile_span
) {
409 mem_copy(dst
+ x
, src
+ ((xo
+ yo
) ^ swizzle
), ytile_span
);
410 xo
+= bytes_per_column
;
411 swizzle
^= swizzle_bit
;
414 mem_copy(dst
+ x2
, src
+ ((xo
+ yo
) ^ swizzle
), x3
- x2
);
422 * Copy texture data from linear to X tile layout, faster.
424 * Same as \ref linear_to_xtiled but faster, because it passes constant
425 * parameters for common cases, allowing the compiler to inline code
426 * optimized for those cases.
428 * \copydoc tile_copy_fn
431 linear_to_xtiled_faster(uint32_t x0
, uint32_t x1
, uint32_t x2
, uint32_t x3
,
432 uint32_t y0
, uint32_t y1
,
433 char *dst
, const char *src
,
435 uint32_t swizzle_bit
,
436 mem_copy_fn mem_copy
)
438 if (x0
== 0 && x3
== xtile_width
&& y0
== 0 && y1
== xtile_height
) {
439 if (mem_copy
== memcpy
)
440 return linear_to_xtiled(0, 0, xtile_width
, xtile_width
, 0, xtile_height
,
441 dst
, src
, src_pitch
, swizzle_bit
, memcpy
);
442 else if (mem_copy
== rgba8_copy_aligned_dst
)
443 return linear_to_xtiled(0, 0, xtile_width
, xtile_width
, 0, xtile_height
,
444 dst
, src
, src_pitch
, swizzle_bit
,
445 rgba8_copy_aligned_dst
);
447 unreachable("not reached");
449 if (mem_copy
== memcpy
)
450 return linear_to_xtiled(x0
, x1
, x2
, x3
, y0
, y1
,
451 dst
, src
, src_pitch
, swizzle_bit
, memcpy
);
452 else if (mem_copy
== rgba8_copy_aligned_dst
)
453 return linear_to_xtiled(x0
, x1
, x2
, x3
, y0
, y1
,
454 dst
, src
, src_pitch
, swizzle_bit
,
455 rgba8_copy_aligned_dst
);
457 unreachable("not reached");
459 linear_to_xtiled(x0
, x1
, x2
, x3
, y0
, y1
,
460 dst
, src
, src_pitch
, swizzle_bit
, mem_copy
);
464 * Copy texture data from linear to Y tile layout, faster.
466 * Same as \ref linear_to_ytiled but faster, because it passes constant
467 * parameters for common cases, allowing the compiler to inline code
468 * optimized for those cases.
470 * \copydoc tile_copy_fn
473 linear_to_ytiled_faster(uint32_t x0
, uint32_t x1
, uint32_t x2
, uint32_t x3
,
474 uint32_t y0
, uint32_t y1
,
475 char *dst
, const char *src
,
477 uint32_t swizzle_bit
,
478 mem_copy_fn mem_copy
)
480 if (x0
== 0 && x3
== ytile_width
&& y0
== 0 && y1
== ytile_height
) {
481 if (mem_copy
== memcpy
)
482 return linear_to_ytiled(0, 0, ytile_width
, ytile_width
, 0, ytile_height
,
483 dst
, src
, src_pitch
, swizzle_bit
, memcpy
);
484 else if (mem_copy
== rgba8_copy_aligned_dst
)
485 return linear_to_ytiled(0, 0, ytile_width
, ytile_width
, 0, ytile_height
,
486 dst
, src
, src_pitch
, swizzle_bit
,
487 rgba8_copy_aligned_dst
);
489 unreachable("not reached");
491 if (mem_copy
== memcpy
)
492 return linear_to_ytiled(x0
, x1
, x2
, x3
, y0
, y1
,
493 dst
, src
, src_pitch
, swizzle_bit
, memcpy
);
494 else if (mem_copy
== rgba8_copy_aligned_dst
)
495 return linear_to_ytiled(x0
, x1
, x2
, x3
, y0
, y1
,
496 dst
, src
, src_pitch
, swizzle_bit
,
497 rgba8_copy_aligned_dst
);
499 unreachable("not reached");
501 linear_to_ytiled(x0
, x1
, x2
, x3
, y0
, y1
,
502 dst
, src
, src_pitch
, swizzle_bit
, mem_copy
);
506 * Copy texture data from X tile layout to linear, faster.
508 * Same as \ref xtile_to_linear but faster, because it passes constant
509 * parameters for common cases, allowing the compiler to inline code
510 * optimized for those cases.
512 * \copydoc tile_copy_fn
515 xtiled_to_linear_faster(uint32_t x0
, uint32_t x1
, uint32_t x2
, uint32_t x3
,
516 uint32_t y0
, uint32_t y1
,
517 char *dst
, const char *src
,
519 uint32_t swizzle_bit
,
520 mem_copy_fn mem_copy
)
522 if (x0
== 0 && x3
== xtile_width
&& y0
== 0 && y1
== xtile_height
) {
523 if (mem_copy
== memcpy
)
524 return xtiled_to_linear(0, 0, xtile_width
, xtile_width
, 0, xtile_height
,
525 dst
, src
, dst_pitch
, swizzle_bit
, memcpy
);
526 else if (mem_copy
== rgba8_copy_aligned_src
)
527 return xtiled_to_linear(0, 0, xtile_width
, xtile_width
, 0, xtile_height
,
528 dst
, src
, dst_pitch
, swizzle_bit
,
529 rgba8_copy_aligned_src
);
531 unreachable("not reached");
533 if (mem_copy
== memcpy
)
534 return xtiled_to_linear(x0
, x1
, x2
, x3
, y0
, y1
,
535 dst
, src
, dst_pitch
, swizzle_bit
, memcpy
);
536 else if (mem_copy
== rgba8_copy_aligned_src
)
537 return xtiled_to_linear(x0
, x1
, x2
, x3
, y0
, y1
,
538 dst
, src
, dst_pitch
, swizzle_bit
,
539 rgba8_copy_aligned_src
);
541 unreachable("not reached");
543 xtiled_to_linear(x0
, x1
, x2
, x3
, y0
, y1
,
544 dst
, src
, dst_pitch
, swizzle_bit
, mem_copy
);
548 * Copy texture data from Y tile layout to linear, faster.
550 * Same as \ref ytile_to_linear but faster, because it passes constant
551 * parameters for common cases, allowing the compiler to inline code
552 * optimized for those cases.
554 * \copydoc tile_copy_fn
557 ytiled_to_linear_faster(uint32_t x0
, uint32_t x1
, uint32_t x2
, uint32_t x3
,
558 uint32_t y0
, uint32_t y1
,
559 char *dst
, const char *src
,
561 uint32_t swizzle_bit
,
562 mem_copy_fn mem_copy
)
564 if (x0
== 0 && x3
== ytile_width
&& y0
== 0 && y1
== ytile_height
) {
565 if (mem_copy
== memcpy
)
566 return ytiled_to_linear(0, 0, ytile_width
, ytile_width
, 0, ytile_height
,
567 dst
, src
, dst_pitch
, swizzle_bit
, memcpy
);
568 else if (mem_copy
== rgba8_copy_aligned_src
)
569 return ytiled_to_linear(0, 0, ytile_width
, ytile_width
, 0, ytile_height
,
570 dst
, src
, dst_pitch
, swizzle_bit
,
571 rgba8_copy_aligned_src
);
573 unreachable("not reached");
575 if (mem_copy
== memcpy
)
576 return ytiled_to_linear(x0
, x1
, x2
, x3
, y0
, y1
,
577 dst
, src
, dst_pitch
, swizzle_bit
, memcpy
);
578 else if (mem_copy
== rgba8_copy_aligned_src
)
579 return ytiled_to_linear(x0
, x1
, x2
, x3
, y0
, y1
,
580 dst
, src
, dst_pitch
, swizzle_bit
,
581 rgba8_copy_aligned_src
);
583 unreachable("not reached");
585 ytiled_to_linear(x0
, x1
, x2
, x3
, y0
, y1
,
586 dst
, src
, dst_pitch
, swizzle_bit
, mem_copy
);
590 * Copy from linear to tiled texture.
592 * Divide the region given by X range [xt1, xt2) and Y range [yt1, yt2) into
593 * pieces that do not cross tile boundaries and copy each piece with a tile
594 * copy function (\ref tile_copy_fn).
595 * The X range is in bytes, i.e. pixels * bytes-per-pixel.
596 * The Y range is in pixels (i.e. unitless).
597 * 'dst' is the start of the texture and 'src' is the corresponding
598 * address to copy from, though copying begins at (xt1, yt1).
601 linear_to_tiled(uint32_t xt1
, uint32_t xt2
,
602 uint32_t yt1
, uint32_t yt2
,
603 char *dst
, const char *src
,
604 uint32_t dst_pitch
, int32_t src_pitch
,
607 mem_copy_fn mem_copy
)
609 tile_copy_fn tile_copy
;
613 uint32_t tw
, th
, span
;
614 uint32_t swizzle_bit
= has_swizzling
? 1<<6 : 0;
616 if (tiling
== I915_TILING_X
) {
620 tile_copy
= linear_to_xtiled_faster
;
621 } else if (tiling
== I915_TILING_Y
) {
625 tile_copy
= linear_to_ytiled_faster
;
627 unreachable("unsupported tiling");
630 /* Round out to tile boundaries. */
631 xt0
= ALIGN_DOWN(xt1
, tw
);
632 xt3
= ALIGN_UP (xt2
, tw
);
633 yt0
= ALIGN_DOWN(yt1
, th
);
634 yt3
= ALIGN_UP (yt2
, th
);
636 /* Loop over all tiles to which we have something to copy.
637 * 'xt' and 'yt' are the origin of the destination tile, whether copying
638 * copying a full or partial tile.
639 * tile_copy() copies one tile or partial tile.
640 * Looping x inside y is the faster memory access pattern.
642 for (yt
= yt0
; yt
< yt3
; yt
+= th
) {
643 for (xt
= xt0
; xt
< xt3
; xt
+= tw
) {
644 /* The area to update is [x0,x3) x [y0,y1).
645 * May not want the whole tile, hence the min and max.
647 uint32_t x0
= MAX2(xt1
, xt
);
648 uint32_t y0
= MAX2(yt1
, yt
);
649 uint32_t x3
= MIN2(xt2
, xt
+ tw
);
650 uint32_t y1
= MIN2(yt2
, yt
+ th
);
652 /* [x0,x3) is split into [x0,x1), [x1,x2), [x2,x3) such that
653 * the middle interval is the longest span-aligned part.
654 * The sub-ranges could be empty.
657 x1
= ALIGN_UP(x0
, span
);
661 x2
= ALIGN_DOWN(x3
, span
);
663 assert(x0
<= x1
&& x1
<= x2
&& x2
<= x3
);
664 assert(x1
- x0
< span
&& x3
- x2
< span
);
665 assert(x3
- x0
<= tw
);
666 assert((x2
- x1
) % span
== 0);
668 /* Translate by (xt,yt) for single-tile copier. */
669 tile_copy(x0
-xt
, x1
-xt
, x2
-xt
, x3
-xt
,
671 dst
+ (ptrdiff_t) xt
* th
+ (ptrdiff_t) yt
* dst_pitch
,
672 src
+ (ptrdiff_t) xt
+ (ptrdiff_t) yt
* src_pitch
,
681 * Copy from tiled to linear texture.
683 * Divide the region given by X range [xt1, xt2) and Y range [yt1, yt2) into
684 * pieces that do not cross tile boundaries and copy each piece with a tile
685 * copy function (\ref tile_copy_fn).
686 * The X range is in bytes, i.e. pixels * bytes-per-pixel.
687 * The Y range is in pixels (i.e. unitless).
688 * 'dst' is the start of the texture and 'src' is the corresponding
689 * address to copy from, though copying begins at (xt1, yt1).
692 tiled_to_linear(uint32_t xt1
, uint32_t xt2
,
693 uint32_t yt1
, uint32_t yt2
,
694 char *dst
, const char *src
,
695 int32_t dst_pitch
, uint32_t src_pitch
,
698 mem_copy_fn mem_copy
)
700 tile_copy_fn tile_copy
;
704 uint32_t tw
, th
, span
;
705 uint32_t swizzle_bit
= has_swizzling
? 1<<6 : 0;
707 if (tiling
== I915_TILING_X
) {
711 tile_copy
= xtiled_to_linear_faster
;
712 } else if (tiling
== I915_TILING_Y
) {
716 tile_copy
= ytiled_to_linear_faster
;
718 unreachable("unsupported tiling");
721 /* Round out to tile boundaries. */
722 xt0
= ALIGN_DOWN(xt1
, tw
);
723 xt3
= ALIGN_UP (xt2
, tw
);
724 yt0
= ALIGN_DOWN(yt1
, th
);
725 yt3
= ALIGN_UP (yt2
, th
);
727 /* Loop over all tiles to which we have something to copy.
728 * 'xt' and 'yt' are the origin of the destination tile, whether copying
729 * copying a full or partial tile.
730 * tile_copy() copies one tile or partial tile.
731 * Looping x inside y is the faster memory access pattern.
733 for (yt
= yt0
; yt
< yt3
; yt
+= th
) {
734 for (xt
= xt0
; xt
< xt3
; xt
+= tw
) {
735 /* The area to update is [x0,x3) x [y0,y1).
736 * May not want the whole tile, hence the min and max.
738 uint32_t x0
= MAX2(xt1
, xt
);
739 uint32_t y0
= MAX2(yt1
, yt
);
740 uint32_t x3
= MIN2(xt2
, xt
+ tw
);
741 uint32_t y1
= MIN2(yt2
, yt
+ th
);
743 /* [x0,x3) is split into [x0,x1), [x1,x2), [x2,x3) such that
744 * the middle interval is the longest span-aligned part.
745 * The sub-ranges could be empty.
748 x1
= ALIGN_UP(x0
, span
);
752 x2
= ALIGN_DOWN(x3
, span
);
754 assert(x0
<= x1
&& x1
<= x2
&& x2
<= x3
);
755 assert(x1
- x0
< span
&& x3
- x2
< span
);
756 assert(x3
- x0
<= tw
);
757 assert((x2
- x1
) % span
== 0);
759 /* Translate by (xt,yt) for single-tile copier. */
760 tile_copy(x0
-xt
, x1
-xt
, x2
-xt
, x3
-xt
,
762 dst
+ (ptrdiff_t) xt
+ (ptrdiff_t) yt
* dst_pitch
,
763 src
+ (ptrdiff_t) xt
* th
+ (ptrdiff_t) yt
* src_pitch
,
773 * Determine which copy function to use for the given format combination
775 * The only two possible copy functions which are ever returned are a
776 * direct memcpy and a RGBA <-> BGRA copy function. Since RGBA -> BGRA and
777 * BGRA -> RGBA are exactly the same operation (and memcpy is obviously
778 * symmetric), it doesn't matter whether the copy is from the tiled image
779 * to the untiled or vice versa. The copy function required is the same in
780 * either case so this function can be used.
782 * \param[in] tiledFormat The format of the tiled image
783 * \param[in] format The GL format of the client data
784 * \param[in] type The GL type of the client data
785 * \param[out] mem_copy Will be set to one of either the standard
786 * library's memcpy or a different copy function
787 * that performs an RGBA to BGRA conversion
788 * \param[out] cpp Number of bytes per channel
790 * \return true if the format and type combination are valid
792 bool intel_get_memcpy(mesa_format tiledFormat
, GLenum format
,
793 GLenum type
, mem_copy_fn
*mem_copy
, uint32_t *cpp
,
794 enum intel_memcpy_direction direction
)
796 if (type
== GL_UNSIGNED_INT_8_8_8_8_REV
&&
797 !(format
== GL_RGBA
|| format
== GL_BGRA
))
798 return false; /* Invalid type/format combination */
800 if ((tiledFormat
== MESA_FORMAT_L_UNORM8
&& format
== GL_LUMINANCE
) ||
801 (tiledFormat
== MESA_FORMAT_A_UNORM8
&& format
== GL_ALPHA
)) {
804 } else if ((tiledFormat
== MESA_FORMAT_B8G8R8A8_UNORM
) ||
805 (tiledFormat
== MESA_FORMAT_B8G8R8X8_UNORM
)) {
807 if (format
== GL_BGRA
) {
809 } else if (format
== GL_RGBA
) {
810 *mem_copy
= direction
== INTEL_UPLOAD
? rgba8_copy_aligned_dst
811 : rgba8_copy_aligned_src
;
813 } else if ((tiledFormat
== MESA_FORMAT_R8G8B8A8_UNORM
) ||
814 (tiledFormat
== MESA_FORMAT_R8G8B8X8_UNORM
)) {
816 if (format
== GL_BGRA
) {
817 /* Copying from RGBA to BGRA is the same as BGRA to RGBA so we can
818 * use the same function.
820 *mem_copy
= direction
== INTEL_UPLOAD
? rgba8_copy_aligned_dst
821 : rgba8_copy_aligned_src
;
822 } else if (format
== GL_RGBA
) {