2 /**************************************************************************
4 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
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 TUNGSTEN GRAPHICS 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/macros.h"
31 #include "main/mtypes.h"
33 #include "main/texobj.h"
34 #include "main/texstore.h"
35 #include "main/texcompress.h"
36 #include "main/enums.h"
38 #include "brw_context.h"
39 #include "intel_batchbuffer.h"
40 #include "intel_tex.h"
41 #include "intel_mipmap_tree.h"
42 #include "intel_blit.h"
44 #define FILE_DEBUG_FLAG DEBUG_TEXTURE
46 #define ALIGN_DOWN(a, b) ROUND_DOWN_TO(a, b)
47 #define ALIGN_UP(a, b) ALIGN(a, b)
50 * Width and span are in bytes, height is in pixels (i.e. unitless).
51 * A "span" is the most number of bytes we can copy from linear to tiled
52 * without needing to calculate a new destination address.
54 static const uint32_t xtile_width
= 512;
55 static const uint32_t xtile_height
= 8;
56 static const uint32_t xtile_span
= 64;
57 static const uint32_t ytile_width
= 128;
58 static const uint32_t ytile_height
= 32;
59 static const uint32_t ytile_span
= 16;
61 typedef void *(*mem_copy_fn
)(void *dest
, const void *src
, size_t n
);
64 * Each row from y0 to y1 is copied in three parts: [x0,x1), [x1,x2), [x2,x3).
65 * These ranges are in bytes, i.e. pixels * bytes-per-pixel.
66 * The first and last ranges must be shorter than a "span" (the longest linear
67 * stretch within a tile) and the middle must equal a whole number of spans.
68 * Ranges may be empty. The region copied must land entirely within one tile.
69 * 'dst' is the start of the tile and 'src' is the corresponding
70 * address to copy from, though copying begins at (x0, y0).
71 * To enable swizzling 'swizzle_bit' must be 1<<6, otherwise zero.
72 * Swizzling flips bit 6 in the copy destination offset, when certain other
75 typedef void (*tile_copy_fn
)(uint32_t x0
, uint32_t x1
, uint32_t x2
, uint32_t x3
,
76 uint32_t y0
, uint32_t y1
,
77 char *dst
, const char *src
,
80 mem_copy_fn mem_copy
);
84 intel_blit_texsubimage(struct gl_context
* ctx
,
85 struct gl_texture_image
*texImage
,
86 GLint xoffset
, GLint yoffset
,
87 GLint width
, GLint height
,
88 GLenum format
, GLenum type
, const void *pixels
,
89 const struct gl_pixelstore_attrib
*packing
)
91 struct brw_context
*brw
= brw_context(ctx
);
92 struct intel_texture_image
*intelImage
= intel_texture_image(texImage
);
94 /* Try to do a blit upload of the subimage if the texture is
100 /* The blitter can't handle Y tiling */
101 if (intelImage
->mt
->region
->tiling
== I915_TILING_Y
)
104 if (texImage
->TexObject
->Target
!= GL_TEXTURE_2D
)
107 /* On gen6, it's probably not worth swapping to the blit ring to do
108 * this because of all the overhead involved.
113 if (!drm_intel_bo_busy(intelImage
->mt
->region
->bo
))
116 DBG("BLT subimage %s target %s level %d offset %d,%d %dx%d\n",
118 _mesa_lookup_enum_by_nr(texImage
->TexObject
->Target
),
119 texImage
->Level
, xoffset
, yoffset
, width
, height
);
121 pixels
= _mesa_validate_pbo_teximage(ctx
, 2, width
, height
, 1,
122 format
, type
, pixels
, packing
,
127 struct intel_mipmap_tree
*temp_mt
=
128 intel_miptree_create(brw
, GL_TEXTURE_2D
, texImage
->TexFormat
,
131 false, 0, INTEL_MIPTREE_TILING_NONE
);
135 GLubyte
*dst
= intel_miptree_map_raw(brw
, temp_mt
);
139 if (!_mesa_texstore(ctx
, 2, texImage
->_BaseFormat
,
141 temp_mt
->region
->pitch
,
144 format
, type
, pixels
, packing
)) {
145 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "intelTexSubImage");
148 intel_miptree_unmap_raw(brw
, temp_mt
);
152 ret
= intel_miptree_blit(brw
,
155 intelImage
->mt
, texImage
->Level
, texImage
->Face
,
156 xoffset
, yoffset
, false,
157 width
, height
, GL_COPY
);
160 intel_miptree_release(&temp_mt
);
161 _mesa_unmap_teximage_pbo(ctx
, packing
);
166 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "intelTexSubImage");
167 intel_miptree_release(&temp_mt
);
168 _mesa_unmap_teximage_pbo(ctx
, packing
);
173 static const uint8_t rgba8_permutation
[16] =
174 { 2,1,0,3, 6,5,4,7, 10,9,8,11, 14,13,12,15 };
176 typedef char v16
__attribute__((vector_size(16)));
178 /* NOTE: dst must be 16 byte aligned */
179 #define rgba8_copy_16(dst, src) \
180 *(v16*)(dst) = __builtin_ia32_pshufb128( \
181 (v16) __builtin_ia32_loadups((float*)(src)), \
182 *(v16*) rgba8_permutation \
187 * Copy RGBA to BGRA - swap R and B.
190 rgba8_copy(void *dst
, const void *src
, size_t bytes
)
193 uint8_t const *s
= src
;
196 /* Fast copying for tile spans.
198 * As long as the destination texture is 16 aligned,
199 * any 16 or 64 spans we get here should also be 16 aligned.
203 assert(!(((uintptr_t)dst
) & 0xf));
204 rgba8_copy_16(d
+ 0, s
+ 0);
209 assert(!(((uintptr_t)dst
) & 0xf));
210 rgba8_copy_16(d
+ 0, s
+ 0);
211 rgba8_copy_16(d
+16, s
+16);
212 rgba8_copy_16(d
+32, s
+32);
213 rgba8_copy_16(d
+48, s
+48);
231 * Copy texture data from linear to X tile layout.
233 * \copydoc tile_copy_fn
236 xtile_copy(uint32_t x0
, uint32_t x1
, uint32_t x2
, uint32_t x3
,
237 uint32_t y0
, uint32_t y1
,
238 char *dst
, const char *src
,
240 uint32_t swizzle_bit
,
241 mem_copy_fn mem_copy
)
243 /* The copy destination offset for each range copied is the sum of
244 * an X offset 'x0' or 'xo' and a Y offset 'yo.'
248 src
+= y0
* src_pitch
;
250 for (yo
= y0
* xtile_width
; yo
< y1
* xtile_width
; yo
+= xtile_width
) {
251 /* Bits 9 and 10 of the copy destination offset control swizzling.
252 * Only 'yo' contributes to those bits in the total offset,
253 * so calculate 'swizzle' just once per row.
254 * Move bits 9 and 10 three and four places respectively down
255 * to bit 6 and xor them.
257 uint32_t swizzle
= ((yo
>> 3) ^ (yo
>> 4)) & swizzle_bit
;
259 mem_copy(dst
+ ((x0
+ yo
) ^ swizzle
), src
+ x0
, x1
- x0
);
261 for (xo
= x1
; xo
< x2
; xo
+= xtile_span
) {
262 mem_copy(dst
+ ((xo
+ yo
) ^ swizzle
), src
+ xo
, xtile_span
);
265 mem_copy(dst
+ ((xo
+ yo
) ^ swizzle
), src
+ x2
, x3
- x2
);
272 * Copy texture data from linear to Y tile layout.
274 * \copydoc tile_copy_fn
278 uint32_t x0
, uint32_t x1
, uint32_t x2
, uint32_t x3
,
279 uint32_t y0
, uint32_t y1
,
280 char *dst
, const char *src
,
282 uint32_t swizzle_bit
,
283 mem_copy_fn mem_copy
)
285 /* Y tiles consist of columns that are 'ytile_span' wide (and the same height
286 * as the tile). Thus the destination offset for (x,y) is the sum of:
287 * (x % column_width) // position within column
288 * (x / column_width) * bytes_per_column // column number * bytes per column
291 * The copy destination offset for each range copied is the sum of
292 * an X offset 'xo0' or 'xo' and a Y offset 'yo.'
294 const uint32_t column_width
= ytile_span
;
295 const uint32_t bytes_per_column
= column_width
* ytile_height
;
297 uint32_t xo0
= (x0
% ytile_span
) + (x0
/ ytile_span
) * bytes_per_column
;
298 uint32_t xo1
= (x1
% ytile_span
) + (x1
/ ytile_span
) * bytes_per_column
;
300 /* Bit 9 of the destination offset control swizzling.
301 * Only the X offset contributes to bit 9 of the total offset,
302 * so swizzle can be calculated in advance for these X positions.
303 * Move bit 9 three places down to bit 6.
305 uint32_t swizzle0
= (xo0
>> 3) & swizzle_bit
;
306 uint32_t swizzle1
= (xo1
>> 3) & swizzle_bit
;
310 src
+= y0
* src_pitch
;
312 for (yo
= y0
* column_width
; yo
< y1
* column_width
; yo
+= column_width
) {
314 uint32_t swizzle
= swizzle1
;
316 mem_copy(dst
+ ((xo0
+ yo
) ^ swizzle0
), src
+ x0
, x1
- x0
);
318 /* Step by spans/columns. As it happens, the swizzle bit flips
319 * at each step so we don't need to calculate it explicitly.
321 for (x
= x1
; x
< x2
; x
+= ytile_span
) {
322 mem_copy(dst
+ ((xo
+ yo
) ^ swizzle
), src
+ x
, ytile_span
);
323 xo
+= bytes_per_column
;
324 swizzle
^= swizzle_bit
;
327 mem_copy(dst
+ ((xo
+ yo
) ^ swizzle
), src
+ x2
, x3
- x2
);
334 * Copy texture data from linear to X tile layout, faster.
336 * Same as \ref xtile_copy but faster, because it passes constant parameters
337 * for common cases, allowing the compiler to inline code optimized for those
340 * \copydoc tile_copy_fn
343 xtile_copy_faster(uint32_t x0
, uint32_t x1
, uint32_t x2
, uint32_t x3
,
344 uint32_t y0
, uint32_t y1
,
345 char *dst
, const char *src
,
347 uint32_t swizzle_bit
,
348 mem_copy_fn mem_copy
)
350 if (x0
== 0 && x3
== xtile_width
&& y0
== 0 && y1
== xtile_height
) {
351 if (mem_copy
== memcpy
)
352 return xtile_copy(0, 0, xtile_width
, xtile_width
, 0, xtile_height
,
353 dst
, src
, src_pitch
, swizzle_bit
, memcpy
);
354 else if (mem_copy
== rgba8_copy
)
355 return xtile_copy(0, 0, xtile_width
, xtile_width
, 0, xtile_height
,
356 dst
, src
, src_pitch
, swizzle_bit
, rgba8_copy
);
358 if (mem_copy
== memcpy
)
359 return xtile_copy(x0
, x1
, x2
, x3
, y0
, y1
,
360 dst
, src
, src_pitch
, swizzle_bit
, memcpy
);
361 else if (mem_copy
== rgba8_copy
)
362 return xtile_copy(x0
, x1
, x2
, x3
, y0
, y1
,
363 dst
, src
, src_pitch
, swizzle_bit
, rgba8_copy
);
365 xtile_copy(x0
, x1
, x2
, x3
, y0
, y1
,
366 dst
, src
, src_pitch
, swizzle_bit
, mem_copy
);
370 * Copy texture data from linear to Y tile layout, faster.
372 * Same as \ref ytile_copy but faster, because it passes constant parameters
373 * for common cases, allowing the compiler to inline code optimized for those
376 * \copydoc tile_copy_fn
379 ytile_copy_faster(uint32_t x0
, uint32_t x1
, uint32_t x2
, uint32_t x3
,
380 uint32_t y0
, uint32_t y1
,
381 char *dst
, const char *src
,
383 uint32_t swizzle_bit
,
384 mem_copy_fn mem_copy
)
386 if (x0
== 0 && x3
== ytile_width
&& y0
== 0 && y1
== ytile_height
) {
387 if (mem_copy
== memcpy
)
388 return ytile_copy(0, 0, ytile_width
, ytile_width
, 0, ytile_height
,
389 dst
, src
, src_pitch
, swizzle_bit
, memcpy
);
390 else if (mem_copy
== rgba8_copy
)
391 return ytile_copy(0, 0, ytile_width
, ytile_width
, 0, ytile_height
,
392 dst
, src
, src_pitch
, swizzle_bit
, rgba8_copy
);
394 if (mem_copy
== memcpy
)
395 return ytile_copy(x0
, x1
, x2
, x3
, y0
, y1
,
396 dst
, src
, src_pitch
, swizzle_bit
, memcpy
);
397 else if (mem_copy
== rgba8_copy
)
398 return ytile_copy(x0
, x1
, x2
, x3
, y0
, y1
,
399 dst
, src
, src_pitch
, swizzle_bit
, rgba8_copy
);
401 ytile_copy(x0
, x1
, x2
, x3
, y0
, y1
,
402 dst
, src
, src_pitch
, swizzle_bit
, mem_copy
);
406 * Copy from linear to tiled texture.
408 * Divide the region given by X range [xt1, xt2) and Y range [yt1, yt2) into
409 * pieces that do not cross tile boundaries and copy each piece with a tile
410 * copy function (\ref tile_copy_fn).
411 * The X range is in bytes, i.e. pixels * bytes-per-pixel.
412 * The Y range is in pixels (i.e. unitless).
413 * 'dst' is the start of the texture and 'src' is the corresponding
414 * address to copy from, though copying begins at (xt1, yt1).
417 linear_to_tiled(uint32_t xt1
, uint32_t xt2
,
418 uint32_t yt1
, uint32_t yt2
,
419 char *dst
, const char *src
,
420 uint32_t dst_pitch
, uint32_t src_pitch
,
423 mem_copy_fn mem_copy
)
425 tile_copy_fn tile_copy
;
429 uint32_t tw
, th
, span
;
430 uint32_t swizzle_bit
= has_swizzling
? 1<<6 : 0;
432 if (tiling
== I915_TILING_X
) {
436 tile_copy
= xtile_copy_faster
;
437 } else if (tiling
== I915_TILING_Y
) {
441 tile_copy
= ytile_copy_faster
;
443 assert(!"unsupported tiling");
447 /* Round out to tile boundaries. */
448 xt0
= ALIGN_DOWN(xt1
, tw
);
449 xt3
= ALIGN_UP (xt2
, tw
);
450 yt0
= ALIGN_DOWN(yt1
, th
);
451 yt3
= ALIGN_UP (yt2
, th
);
453 /* Loop over all tiles to which we have something to copy.
454 * 'xt' and 'yt' are the origin of the destination tile, whether copying
455 * copying a full or partial tile.
456 * tile_copy() copies one tile or partial tile.
457 * Looping x inside y is the faster memory access pattern.
459 for (yt
= yt0
; yt
< yt3
; yt
+= th
) {
460 for (xt
= xt0
; xt
< xt3
; xt
+= tw
) {
461 /* The area to update is [x0,x3) x [y0,y1).
462 * May not want the whole tile, hence the min and max.
464 uint32_t x0
= MAX2(xt1
, xt
);
465 uint32_t y0
= MAX2(yt1
, yt
);
466 uint32_t x3
= MIN2(xt2
, xt
+ tw
);
467 uint32_t y1
= MIN2(yt2
, yt
+ th
);
469 /* [x0,x3) is split into [x0,x1), [x1,x2), [x2,x3) such that
470 * the middle interval is the longest span-aligned part.
471 * The sub-ranges could be empty.
474 x1
= ALIGN_UP(x0
, span
);
478 x2
= ALIGN_DOWN(x3
, span
);
480 assert(x0
<= x1
&& x1
<= x2
&& x2
<= x3
);
481 assert(x1
- x0
< span
&& x3
- x2
< span
);
482 assert(x3
- x0
<= tw
);
483 assert((x2
- x1
) % span
== 0);
485 /* Translate by (xt,yt) for single-tile copier. */
486 tile_copy(x0
-xt
, x1
-xt
, x2
-xt
, x3
-xt
,
488 dst
+ xt
* th
+ yt
* dst_pitch
,
489 src
+ xt
+ yt
* src_pitch
,
498 * \brief A fast path for glTexImage and glTexSubImage.
500 * \param for_glTexImage Was this called from glTexImage or glTexSubImage?
502 * This fast path is taken when the texture format is BGRA, RGBA,
503 * A or L and when the texture memory is X- or Y-tiled. It uploads
504 * the texture data by mapping the texture memory without a GTT fence, thus
505 * acquiring a tiled view of the memory, and then copying sucessive
506 * spans within each tile.
508 * This is a performance win over the conventional texture upload path because
509 * it avoids the performance penalty of writing through the write-combine
510 * buffer. In the conventional texture upload path,
511 * texstore.c:store_texsubimage(), the texture memory is mapped through a GTT
512 * fence, thus acquiring a linear view of the memory, then each row in the
513 * image is memcpy'd. In this fast path, we replace each row's copy with
514 * a sequence of copies over each linear span in tile.
516 * One use case is Google Chrome's paint rectangles. Chrome (as
517 * of version 21) renders each page as a tiling of 256x256 GL_BGRA textures.
518 * Each page's content is initially uploaded with glTexImage2D and damaged
519 * regions are updated with glTexSubImage2D. On some workloads, the
520 * performance gain of this fastpath on Sandybridge is over 5x.
523 intel_texsubimage_tiled_memcpy(struct gl_context
* ctx
,
525 struct gl_texture_image
*texImage
,
526 GLint xoffset
, GLint yoffset
, GLint zoffset
,
527 GLsizei width
, GLsizei height
, GLsizei depth
,
528 GLenum format
, GLenum type
,
529 const GLvoid
*pixels
,
530 const struct gl_pixelstore_attrib
*packing
,
533 struct brw_context
*brw
= brw_context(ctx
);
534 struct intel_texture_image
*image
= intel_texture_image(texImage
);
536 /* The miptree's buffer. */
542 mem_copy_fn mem_copy
= NULL
;
544 /* This fastpath is restricted to specific texture types: level 0 of
545 * a 2D BGRA, RGBA, L8 or A8 texture. It could be generalized to support
549 type
!= GL_UNSIGNED_BYTE
||
550 texImage
->TexObject
->Target
!= GL_TEXTURE_2D
||
551 texImage
->Level
!= 0 ||
553 _mesa_is_bufferobj(packing
->BufferObj
) ||
554 packing
->Alignment
> 4 ||
555 packing
->SkipPixels
> 0 ||
556 packing
->SkipRows
> 0 ||
557 (packing
->RowLength
!= 0 && packing
->RowLength
!= width
) ||
558 packing
->SwapBytes
||
563 if ((texImage
->TexFormat
== MESA_FORMAT_L8
&& format
== GL_LUMINANCE
) ||
564 (texImage
->TexFormat
== MESA_FORMAT_A8
&& format
== GL_ALPHA
)) {
567 } else if (texImage
->TexFormat
== MESA_FORMAT_ARGB8888
) {
569 if (format
== GL_BGRA
) {
571 } else if (format
== GL_RGBA
) {
572 mem_copy
= rgba8_copy
;
579 ctx
->Driver
.AllocTextureImageBuffer(ctx
, texImage
);
582 (image
->mt
->region
->tiling
!= I915_TILING_X
&&
583 image
->mt
->region
->tiling
!= I915_TILING_Y
)) {
584 /* The algorithm is written only for X- or Y-tiled memory. */
588 /* Since we are going to write raw data to the miptree, we need to resolve
589 * any pending fast color clears before we start.
591 intel_miptree_resolve_color(brw
, image
->mt
);
593 bo
= image
->mt
->region
->bo
;
595 if (drm_intel_bo_references(brw
->batch
.bo
, bo
)) {
596 perf_debug("Flushing before mapping a referenced bo.\n");
597 intel_batchbuffer_flush(brw
);
600 if (unlikely(brw
->perf_debug
)) {
601 if (drm_intel_bo_busy(bo
)) {
602 perf_debug("Mapping a busy BO, causing a stall on the GPU.\n");
606 error
= drm_intel_bo_map(bo
, true /*write_enable*/);
607 if (error
|| bo
->virtual == NULL
) {
608 DBG("%s: failed to map bo\n", __FUNCTION__
);
612 /* We postponed printing this message until having committed to executing
615 DBG("%s: level=%d offset=(%d,%d) (w,h)=(%d,%d)\n",
616 __FUNCTION__
, texImage
->Level
, xoffset
, yoffset
, width
, height
);
619 xoffset
* cpp
, (xoffset
+ width
) * cpp
,
620 yoffset
, yoffset
+ height
,
621 bo
->virtual, pixels
- (xoffset
+ yoffset
* width
) * cpp
,
622 image
->mt
->region
->pitch
, width
* cpp
,
624 image
->mt
->region
->tiling
,
628 drm_intel_bo_unmap(bo
);
633 intelTexSubImage(struct gl_context
* ctx
,
635 struct gl_texture_image
*texImage
,
636 GLint xoffset
, GLint yoffset
, GLint zoffset
,
637 GLsizei width
, GLsizei height
, GLsizei depth
,
638 GLenum format
, GLenum type
,
639 const GLvoid
* pixels
,
640 const struct gl_pixelstore_attrib
*packing
)
644 ok
= intel_texsubimage_tiled_memcpy(ctx
, dims
, texImage
,
645 xoffset
, yoffset
, zoffset
,
646 width
, height
, depth
,
647 format
, type
, pixels
, packing
,
648 false /*for_glTexImage*/);
652 /* The intel_blit_texsubimage() function only handles 2D images */
653 if (dims
!= 2 || !intel_blit_texsubimage(ctx
, texImage
,
656 format
, type
, pixels
, packing
)) {
657 _mesa_store_texsubimage(ctx
, dims
, texImage
,
658 xoffset
, yoffset
, zoffset
,
659 width
, height
, depth
,
660 format
, type
, pixels
, packing
);
665 intelInitTextureSubImageFuncs(struct dd_function_table
*functions
)
667 functions
->TexSubImage
= intelTexSubImage
;