2 /**************************************************************************
4 * Copyright 2003 VMware, Inc.
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, sub license, 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 NON-INFRINGEMENT.
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.
27 **************************************************************************/
29 #include "main/bufferobj.h"
30 #include "main/image.h"
31 #include "main/macros.h"
32 #include "main/mtypes.h"
34 #include "main/texobj.h"
35 #include "main/texstore.h"
36 #include "main/texcompress.h"
37 #include "main/enums.h"
39 #include "brw_context.h"
40 #include "intel_batchbuffer.h"
41 #include "intel_tex.h"
42 #include "intel_mipmap_tree.h"
43 #include "intel_blit.h"
45 #define FILE_DEBUG_FLAG DEBUG_TEXTURE
47 #define ALIGN_DOWN(a, b) ROUND_DOWN_TO(a, b)
48 #define ALIGN_UP(a, b) ALIGN(a, b)
51 * Width and span are in bytes, height is in pixels (i.e. unitless).
52 * A "span" is the most number of bytes we can copy from linear to tiled
53 * 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 typedef void *(*mem_copy_fn
)(void *dest
, const void *src
, size_t n
);
65 * Each row from y0 to y1 is copied in three parts: [x0,x1), [x1,x2), [x2,x3).
66 * These ranges are in bytes, i.e. pixels * bytes-per-pixel.
67 * The first and last ranges must be shorter than a "span" (the longest linear
68 * stretch within a tile) and the middle must equal a whole number of spans.
69 * Ranges may be empty. The region copied must land entirely within one tile.
70 * 'dst' is the start of the tile and 'src' is the corresponding
71 * address to copy from, though copying begins at (x0, y0).
72 * To enable swizzling 'swizzle_bit' must be 1<<6, otherwise zero.
73 * Swizzling flips bit 6 in the copy destination offset, when certain other
76 typedef void (*tile_copy_fn
)(uint32_t x0
, uint32_t x1
, uint32_t x2
, uint32_t x3
,
77 uint32_t y0
, uint32_t y1
,
78 char *dst
, const char *src
,
81 mem_copy_fn mem_copy
);
85 intel_blit_texsubimage(struct gl_context
* ctx
,
86 struct gl_texture_image
*texImage
,
87 GLint xoffset
, GLint yoffset
,
88 GLint width
, GLint height
,
89 GLenum format
, GLenum type
, const void *pixels
,
90 const struct gl_pixelstore_attrib
*packing
)
92 struct brw_context
*brw
= brw_context(ctx
);
93 struct intel_texture_image
*intelImage
= intel_texture_image(texImage
);
95 /* Try to do a blit upload of the subimage if the texture is
101 /* The blitter can't handle Y tiling */
102 if (intelImage
->mt
->region
->tiling
== I915_TILING_Y
)
105 if (texImage
->TexObject
->Target
!= GL_TEXTURE_2D
)
108 /* On gen6, it's probably not worth swapping to the blit ring to do
109 * this because of all the overhead involved.
114 if (!drm_intel_bo_busy(intelImage
->mt
->region
->bo
))
117 DBG("BLT subimage %s target %s level %d offset %d,%d %dx%d\n",
119 _mesa_lookup_enum_by_nr(texImage
->TexObject
->Target
),
120 texImage
->Level
, xoffset
, yoffset
, width
, height
);
122 pixels
= _mesa_validate_pbo_teximage(ctx
, 2, width
, height
, 1,
123 format
, type
, pixels
, packing
,
128 struct intel_mipmap_tree
*temp_mt
=
129 intel_miptree_create(brw
, GL_TEXTURE_2D
, texImage
->TexFormat
,
132 false, 0, INTEL_MIPTREE_TILING_NONE
);
136 GLubyte
*dst
= intel_miptree_map_raw(brw
, temp_mt
);
140 if (!_mesa_texstore(ctx
, 2, texImage
->_BaseFormat
,
142 temp_mt
->region
->pitch
,
145 format
, type
, pixels
, packing
)) {
146 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "intelTexSubImage");
149 intel_miptree_unmap_raw(brw
, temp_mt
);
153 ret
= intel_miptree_blit(brw
,
156 intelImage
->mt
, texImage
->Level
, texImage
->Face
,
157 xoffset
, yoffset
, false,
158 width
, height
, GL_COPY
);
161 intel_miptree_release(&temp_mt
);
162 _mesa_unmap_teximage_pbo(ctx
, packing
);
167 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "intelTexSubImage");
168 intel_miptree_release(&temp_mt
);
169 _mesa_unmap_teximage_pbo(ctx
, packing
);
174 static const uint8_t rgba8_permutation
[16] =
175 { 2,1,0,3, 6,5,4,7, 10,9,8,11, 14,13,12,15 };
177 typedef char v16
__attribute__((vector_size(16)));
179 /* NOTE: dst must be 16 byte aligned */
180 #define rgba8_copy_16(dst, src) \
181 *(v16*)(dst) = __builtin_ia32_pshufb128( \
182 (v16) __builtin_ia32_loadups((float*)(src)), \
183 *(v16*) rgba8_permutation \
188 * Copy RGBA to BGRA - swap R and B.
191 rgba8_copy(void *dst
, const void *src
, size_t bytes
)
194 uint8_t const *s
= src
;
197 /* Fast copying for tile spans.
199 * As long as the destination texture is 16 aligned,
200 * any 16 or 64 spans we get here should also be 16 aligned.
204 assert(!(((uintptr_t)dst
) & 0xf));
205 rgba8_copy_16(d
+ 0, s
+ 0);
210 assert(!(((uintptr_t)dst
) & 0xf));
211 rgba8_copy_16(d
+ 0, s
+ 0);
212 rgba8_copy_16(d
+16, s
+16);
213 rgba8_copy_16(d
+32, s
+32);
214 rgba8_copy_16(d
+48, s
+48);
232 * Copy texture data from linear to X tile layout.
234 * \copydoc tile_copy_fn
237 xtile_copy(uint32_t x0
, uint32_t x1
, uint32_t x2
, uint32_t x3
,
238 uint32_t y0
, uint32_t y1
,
239 char *dst
, const char *src
,
241 uint32_t swizzle_bit
,
242 mem_copy_fn mem_copy
)
244 /* The copy destination offset for each range copied is the sum of
245 * an X offset 'x0' or 'xo' and a Y offset 'yo.'
249 src
+= y0
* src_pitch
;
251 for (yo
= y0
* xtile_width
; yo
< y1
* xtile_width
; yo
+= xtile_width
) {
252 /* Bits 9 and 10 of the copy destination offset control swizzling.
253 * Only 'yo' contributes to those bits in the total offset,
254 * so calculate 'swizzle' just once per row.
255 * Move bits 9 and 10 three and four places respectively down
256 * to bit 6 and xor them.
258 uint32_t swizzle
= ((yo
>> 3) ^ (yo
>> 4)) & swizzle_bit
;
260 mem_copy(dst
+ ((x0
+ yo
) ^ swizzle
), src
+ x0
, x1
- x0
);
262 for (xo
= x1
; xo
< x2
; xo
+= xtile_span
) {
263 mem_copy(dst
+ ((xo
+ yo
) ^ swizzle
), src
+ xo
, xtile_span
);
266 mem_copy(dst
+ ((xo
+ yo
) ^ swizzle
), src
+ x2
, x3
- x2
);
273 * Copy texture data from linear to Y tile layout.
275 * \copydoc tile_copy_fn
279 uint32_t x0
, uint32_t x1
, uint32_t x2
, uint32_t x3
,
280 uint32_t y0
, uint32_t y1
,
281 char *dst
, const char *src
,
283 uint32_t swizzle_bit
,
284 mem_copy_fn mem_copy
)
286 /* Y tiles consist of columns that are 'ytile_span' wide (and the same height
287 * as the tile). Thus the destination offset for (x,y) is the sum of:
288 * (x % column_width) // position within column
289 * (x / column_width) * bytes_per_column // column number * bytes per column
292 * The copy destination offset for each range copied is the sum of
293 * an X offset 'xo0' or 'xo' and a Y offset 'yo.'
295 const uint32_t column_width
= ytile_span
;
296 const uint32_t bytes_per_column
= column_width
* ytile_height
;
298 uint32_t xo0
= (x0
% ytile_span
) + (x0
/ ytile_span
) * bytes_per_column
;
299 uint32_t xo1
= (x1
% ytile_span
) + (x1
/ ytile_span
) * bytes_per_column
;
301 /* Bit 9 of the destination offset control swizzling.
302 * Only the X offset contributes to bit 9 of the total offset,
303 * so swizzle can be calculated in advance for these X positions.
304 * Move bit 9 three places down to bit 6.
306 uint32_t swizzle0
= (xo0
>> 3) & swizzle_bit
;
307 uint32_t swizzle1
= (xo1
>> 3) & swizzle_bit
;
311 src
+= y0
* src_pitch
;
313 for (yo
= y0
* column_width
; yo
< y1
* column_width
; yo
+= column_width
) {
315 uint32_t swizzle
= swizzle1
;
317 mem_copy(dst
+ ((xo0
+ yo
) ^ swizzle0
), src
+ x0
, x1
- x0
);
319 /* Step by spans/columns. As it happens, the swizzle bit flips
320 * at each step so we don't need to calculate it explicitly.
322 for (x
= x1
; x
< x2
; x
+= ytile_span
) {
323 mem_copy(dst
+ ((xo
+ yo
) ^ swizzle
), src
+ x
, ytile_span
);
324 xo
+= bytes_per_column
;
325 swizzle
^= swizzle_bit
;
328 mem_copy(dst
+ ((xo
+ yo
) ^ swizzle
), src
+ x2
, x3
- x2
);
335 #define FLATTEN __attribute__((flatten))
341 * Copy texture data from linear to X tile layout, faster.
343 * Same as \ref xtile_copy but faster, because it passes constant parameters
344 * for common cases, allowing the compiler to inline code optimized for those
347 * \copydoc tile_copy_fn
350 xtile_copy_faster(uint32_t x0
, uint32_t x1
, uint32_t x2
, uint32_t x3
,
351 uint32_t y0
, uint32_t y1
,
352 char *dst
, const char *src
,
354 uint32_t swizzle_bit
,
355 mem_copy_fn mem_copy
)
357 if (x0
== 0 && x3
== xtile_width
&& y0
== 0 && y1
== xtile_height
) {
358 if (mem_copy
== memcpy
)
359 return xtile_copy(0, 0, xtile_width
, xtile_width
, 0, xtile_height
,
360 dst
, src
, src_pitch
, swizzle_bit
, memcpy
);
361 else if (mem_copy
== rgba8_copy
)
362 return xtile_copy(0, 0, xtile_width
, xtile_width
, 0, xtile_height
,
363 dst
, src
, src_pitch
, swizzle_bit
, rgba8_copy
);
365 if (mem_copy
== memcpy
)
366 return xtile_copy(x0
, x1
, x2
, x3
, y0
, y1
,
367 dst
, src
, src_pitch
, swizzle_bit
, memcpy
);
368 else if (mem_copy
== rgba8_copy
)
369 return xtile_copy(x0
, x1
, x2
, x3
, y0
, y1
,
370 dst
, src
, src_pitch
, swizzle_bit
, rgba8_copy
);
372 xtile_copy(x0
, x1
, x2
, x3
, y0
, y1
,
373 dst
, src
, src_pitch
, swizzle_bit
, mem_copy
);
377 * Copy texture data from linear to Y tile layout, faster.
379 * Same as \ref ytile_copy but faster, because it passes constant parameters
380 * for common cases, allowing the compiler to inline code optimized for those
383 * \copydoc tile_copy_fn
386 ytile_copy_faster(uint32_t x0
, uint32_t x1
, uint32_t x2
, uint32_t x3
,
387 uint32_t y0
, uint32_t y1
,
388 char *dst
, const char *src
,
390 uint32_t swizzle_bit
,
391 mem_copy_fn mem_copy
)
393 if (x0
== 0 && x3
== ytile_width
&& y0
== 0 && y1
== ytile_height
) {
394 if (mem_copy
== memcpy
)
395 return ytile_copy(0, 0, ytile_width
, ytile_width
, 0, ytile_height
,
396 dst
, src
, src_pitch
, swizzle_bit
, memcpy
);
397 else if (mem_copy
== rgba8_copy
)
398 return ytile_copy(0, 0, ytile_width
, ytile_width
, 0, ytile_height
,
399 dst
, src
, src_pitch
, swizzle_bit
, rgba8_copy
);
401 if (mem_copy
== memcpy
)
402 return ytile_copy(x0
, x1
, x2
, x3
, y0
, y1
,
403 dst
, src
, src_pitch
, swizzle_bit
, memcpy
);
404 else if (mem_copy
== rgba8_copy
)
405 return ytile_copy(x0
, x1
, x2
, x3
, y0
, y1
,
406 dst
, src
, src_pitch
, swizzle_bit
, rgba8_copy
);
408 ytile_copy(x0
, x1
, x2
, x3
, y0
, y1
,
409 dst
, src
, src_pitch
, swizzle_bit
, mem_copy
);
413 * Copy from linear to tiled texture.
415 * Divide the region given by X range [xt1, xt2) and Y range [yt1, yt2) into
416 * pieces that do not cross tile boundaries and copy each piece with a tile
417 * copy function (\ref tile_copy_fn).
418 * The X range is in bytes, i.e. pixels * bytes-per-pixel.
419 * The Y range is in pixels (i.e. unitless).
420 * 'dst' is the start of the texture and 'src' is the corresponding
421 * address to copy from, though copying begins at (xt1, yt1).
424 linear_to_tiled(uint32_t xt1
, uint32_t xt2
,
425 uint32_t yt1
, uint32_t yt2
,
426 char *dst
, const char *src
,
427 uint32_t dst_pitch
, uint32_t src_pitch
,
430 mem_copy_fn mem_copy
)
432 tile_copy_fn tile_copy
;
436 uint32_t tw
, th
, span
;
437 uint32_t swizzle_bit
= has_swizzling
? 1<<6 : 0;
439 if (tiling
== I915_TILING_X
) {
443 tile_copy
= xtile_copy_faster
;
444 } else if (tiling
== I915_TILING_Y
) {
448 tile_copy
= ytile_copy_faster
;
450 assert(!"unsupported tiling");
454 /* Round out to tile boundaries. */
455 xt0
= ALIGN_DOWN(xt1
, tw
);
456 xt3
= ALIGN_UP (xt2
, tw
);
457 yt0
= ALIGN_DOWN(yt1
, th
);
458 yt3
= ALIGN_UP (yt2
, th
);
460 /* Loop over all tiles to which we have something to copy.
461 * 'xt' and 'yt' are the origin of the destination tile, whether copying
462 * copying a full or partial tile.
463 * tile_copy() copies one tile or partial tile.
464 * Looping x inside y is the faster memory access pattern.
466 for (yt
= yt0
; yt
< yt3
; yt
+= th
) {
467 for (xt
= xt0
; xt
< xt3
; xt
+= tw
) {
468 /* The area to update is [x0,x3) x [y0,y1).
469 * May not want the whole tile, hence the min and max.
471 uint32_t x0
= MAX2(xt1
, xt
);
472 uint32_t y0
= MAX2(yt1
, yt
);
473 uint32_t x3
= MIN2(xt2
, xt
+ tw
);
474 uint32_t y1
= MIN2(yt2
, yt
+ th
);
476 /* [x0,x3) is split into [x0,x1), [x1,x2), [x2,x3) such that
477 * the middle interval is the longest span-aligned part.
478 * The sub-ranges could be empty.
481 x1
= ALIGN_UP(x0
, span
);
485 x2
= ALIGN_DOWN(x3
, span
);
487 assert(x0
<= x1
&& x1
<= x2
&& x2
<= x3
);
488 assert(x1
- x0
< span
&& x3
- x2
< span
);
489 assert(x3
- x0
<= tw
);
490 assert((x2
- x1
) % span
== 0);
492 /* Translate by (xt,yt) for single-tile copier. */
493 tile_copy(x0
-xt
, x1
-xt
, x2
-xt
, x3
-xt
,
495 dst
+ xt
* th
+ yt
* dst_pitch
,
496 src
+ xt
+ yt
* src_pitch
,
505 * \brief A fast path for glTexImage and glTexSubImage.
507 * \param for_glTexImage Was this called from glTexImage or glTexSubImage?
509 * This fast path is taken when the texture format is BGRA, RGBA,
510 * A or L and when the texture memory is X- or Y-tiled. It uploads
511 * the texture data by mapping the texture memory without a GTT fence, thus
512 * acquiring a tiled view of the memory, and then copying sucessive
513 * spans within each tile.
515 * This is a performance win over the conventional texture upload path because
516 * it avoids the performance penalty of writing through the write-combine
517 * buffer. In the conventional texture upload path,
518 * texstore.c:store_texsubimage(), the texture memory is mapped through a GTT
519 * fence, thus acquiring a linear view of the memory, then each row in the
520 * image is memcpy'd. In this fast path, we replace each row's copy with
521 * a sequence of copies over each linear span in tile.
523 * One use case is Google Chrome's paint rectangles. Chrome (as
524 * of version 21) renders each page as a tiling of 256x256 GL_BGRA textures.
525 * Each page's content is initially uploaded with glTexImage2D and damaged
526 * regions are updated with glTexSubImage2D. On some workloads, the
527 * performance gain of this fastpath on Sandybridge is over 5x.
530 intel_texsubimage_tiled_memcpy(struct gl_context
* ctx
,
532 struct gl_texture_image
*texImage
,
533 GLint xoffset
, GLint yoffset
, GLint zoffset
,
534 GLsizei width
, GLsizei height
, GLsizei depth
,
535 GLenum format
, GLenum type
,
536 const GLvoid
*pixels
,
537 const struct gl_pixelstore_attrib
*packing
,
540 struct brw_context
*brw
= brw_context(ctx
);
541 struct intel_texture_image
*image
= intel_texture_image(texImage
);
544 /* The miptree's buffer. */
550 mem_copy_fn mem_copy
= NULL
;
552 /* This fastpath is restricted to specific texture types:
553 * a 2D BGRA, RGBA, L8 or A8 texture. It could be generalized to support
556 * FINISHME: The restrictions below on packing alignment and packing row
557 * length are likely unneeded now because we calculate the source stride
558 * with _mesa_image_row_stride. However, before removing the restrictions
562 type
!= GL_UNSIGNED_BYTE
||
563 texImage
->TexObject
->Target
!= GL_TEXTURE_2D
||
565 _mesa_is_bufferobj(packing
->BufferObj
) ||
566 packing
->Alignment
> 4 ||
567 packing
->SkipPixels
> 0 ||
568 packing
->SkipRows
> 0 ||
569 (packing
->RowLength
!= 0 && packing
->RowLength
!= width
) ||
570 packing
->SwapBytes
||
575 if ((texImage
->TexFormat
== MESA_FORMAT_L8
&& format
== GL_LUMINANCE
) ||
576 (texImage
->TexFormat
== MESA_FORMAT_A8
&& format
== GL_ALPHA
)) {
579 } else if ((texImage
->TexFormat
== MESA_FORMAT_ARGB8888
) ||
580 (texImage
->TexFormat
== MESA_FORMAT_XRGB8888
)) {
582 if (format
== GL_BGRA
) {
584 } else if (format
== GL_RGBA
) {
585 mem_copy
= rgba8_copy
;
592 ctx
->Driver
.AllocTextureImageBuffer(ctx
, texImage
);
595 (image
->mt
->region
->tiling
!= I915_TILING_X
&&
596 image
->mt
->region
->tiling
!= I915_TILING_Y
)) {
597 /* The algorithm is written only for X- or Y-tiled memory. */
601 /* Since we are going to write raw data to the miptree, we need to resolve
602 * any pending fast color clears before we start.
604 intel_miptree_resolve_color(brw
, image
->mt
);
606 bo
= image
->mt
->region
->bo
;
608 if (drm_intel_bo_references(brw
->batch
.bo
, bo
)) {
609 perf_debug("Flushing before mapping a referenced bo.\n");
610 intel_batchbuffer_flush(brw
);
613 if (unlikely(brw
->perf_debug
)) {
614 if (drm_intel_bo_busy(bo
)) {
615 perf_debug("Mapping a busy BO, causing a stall on the GPU.\n");
619 error
= drm_intel_bo_map(bo
, true /*write_enable*/);
620 if (error
|| bo
->virtual == NULL
) {
621 DBG("%s: failed to map bo\n", __FUNCTION__
);
625 src_pitch
= _mesa_image_row_stride(packing
, width
, format
, type
);
627 /* We postponed printing this message until having committed to executing
630 DBG("%s: level=%d offset=(%d,%d) (w,h)=(%d,%d) format=0x%x type=0x%x "
631 "mesa_format=0x%x tiling=%d "
632 "packing=(alignment=%d row_length=%d skip_pixels=%d skip_rows=%d) "
633 "for_glTexImage=%d\n",
634 __FUNCTION__
, texImage
->Level
, xoffset
, yoffset
, width
, height
,
635 format
, type
, texImage
->TexFormat
, image
->mt
->region
->tiling
,
636 packing
->Alignment
, packing
->RowLength
, packing
->SkipPixels
,
637 packing
->SkipRows
, for_glTexImage
);
639 /* Adjust x and y offset based on miplevel */
640 xoffset
+= image
->mt
->level
[texImage
->Level
].level_x
;
641 yoffset
+= image
->mt
->level
[texImage
->Level
].level_y
;
644 xoffset
* cpp
, (xoffset
+ width
) * cpp
,
645 yoffset
, yoffset
+ height
,
646 bo
->virtual, pixels
- yoffset
* src_pitch
- xoffset
* cpp
,
647 image
->mt
->region
->pitch
, src_pitch
,
649 image
->mt
->region
->tiling
,
653 drm_intel_bo_unmap(bo
);
658 intelTexSubImage(struct gl_context
* ctx
,
660 struct gl_texture_image
*texImage
,
661 GLint xoffset
, GLint yoffset
, GLint zoffset
,
662 GLsizei width
, GLsizei height
, GLsizei depth
,
663 GLenum format
, GLenum type
,
664 const GLvoid
* pixels
,
665 const struct gl_pixelstore_attrib
*packing
)
669 ok
= intel_texsubimage_tiled_memcpy(ctx
, dims
, texImage
,
670 xoffset
, yoffset
, zoffset
,
671 width
, height
, depth
,
672 format
, type
, pixels
, packing
,
673 false /*for_glTexImage*/);
677 /* The intel_blit_texsubimage() function only handles 2D images */
678 if (dims
!= 2 || !intel_blit_texsubimage(ctx
, texImage
,
681 format
, type
, pixels
, packing
)) {
682 _mesa_store_texsubimage(ctx
, dims
, texImage
,
683 xoffset
, yoffset
, zoffset
,
684 width
, height
, depth
,
685 format
, type
, pixels
, packing
);
690 intelInitTextureSubImageFuncs(struct dd_function_table
*functions
)
692 functions
->TexSubImage
= intelTexSubImage
;