1 /**************************************************************************
3 * Copyright 2003 VMware, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * 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, sub license, 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 portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
29 #include "main/mtypes.h"
30 #include "main/context.h"
31 #include "main/enums.h"
32 #include "main/colormac.h"
33 #include "main/fbobject.h"
35 #include "intel_blit.h"
36 #include "intel_buffers.h"
37 #include "intel_context.h"
38 #include "intel_fbo.h"
39 #include "intel_reg.h"
40 #include "intel_regions.h"
41 #include "intel_batchbuffer.h"
42 #include "intel_mipmap_tree.h"
44 #define FILE_DEBUG_FLAG DEBUG_BLIT
47 intel_miptree_set_alpha_to_one(struct intel_context
*intel
,
48 struct intel_mipmap_tree
*mt
,
49 int x
, int y
, int width
, int height
);
51 static GLuint
translate_raster_op(enum gl_logicop_mode logicop
)
53 return logicop
| (logicop
<< 4);
76 * Implements a rectangular block transfer (blit) of pixels between two
79 * Our blitter can operate on 1, 2, or 4-byte-per-pixel data, with generous,
80 * but limited, pitches and sizes allowed.
82 * The src/dst coordinates are relative to the given level/slice of the
85 * If @src_flip or @dst_flip is set, then the rectangle within that miptree
86 * will be inverted (including scanline order) when copying. This is common
87 * in GL when copying between window system and user-created
88 * renderbuffers/textures.
91 intel_miptree_blit(struct intel_context
*intel
,
92 struct intel_mipmap_tree
*src_mt
,
93 int src_level
, int src_slice
,
94 uint32_t src_x
, uint32_t src_y
, bool src_flip
,
95 struct intel_mipmap_tree
*dst_mt
,
96 int dst_level
, int dst_slice
,
97 uint32_t dst_x
, uint32_t dst_y
, bool dst_flip
,
98 uint32_t width
, uint32_t height
,
99 enum gl_logicop_mode logicop
)
101 /* No sRGB decode or encode is done by the hardware blitter, which is
102 * consistent with what we want in the callers (glCopyTexSubImage(),
103 * glBlitFramebuffer(), texture validation, etc.).
105 mesa_format src_format
= _mesa_get_srgb_format_linear(src_mt
->format
);
106 mesa_format dst_format
= _mesa_get_srgb_format_linear(dst_mt
->format
);
108 /* The blitter doesn't support doing any format conversions. We do also
109 * support blitting ARGB8888 to XRGB8888 (trivial, the values dropped into
110 * the X channel don't matter), and XRGB8888 to ARGB8888 by setting the A
111 * channel to 1.0 at the end.
113 if (src_format
!= dst_format
&&
114 ((src_format
!= MESA_FORMAT_B8G8R8A8_UNORM
&&
115 src_format
!= MESA_FORMAT_B8G8R8X8_UNORM
) ||
116 (dst_format
!= MESA_FORMAT_B8G8R8A8_UNORM
&&
117 dst_format
!= MESA_FORMAT_B8G8R8X8_UNORM
))) {
118 perf_debug("%s: Can't use hardware blitter from %s to %s, "
119 "falling back.\n", __func__
,
120 _mesa_get_format_name(src_format
),
121 _mesa_get_format_name(dst_format
));
125 /* According to the Ivy Bridge PRM, Vol1 Part4, section 1.2.1.2 (Graphics
126 * Data Size Limitations):
128 * The BLT engine is capable of transferring very large quantities of
129 * graphics data. Any graphics data read from and written to the
130 * destination is permitted to represent a number of pixels that
131 * occupies up to 65,536 scan lines and up to 32,768 bytes per scan line
132 * at the destination. The maximum number of pixels that may be
133 * represented per scan line’s worth of graphics data depends on the
136 * Furthermore, intelEmitCopyBlit (which is called below) uses a signed
137 * 16-bit integer to represent buffer pitch, so it can only handle buffer
140 * As a result of these two limitations, we can only use the blitter to do
141 * this copy when the region's pitch is less than 32k.
143 if (src_mt
->region
->pitch
> 32768 ||
144 dst_mt
->region
->pitch
> 32768) {
145 perf_debug("Falling back due to >32k pitch\n");
150 src_y
= src_mt
->level
[src_level
].height
- src_y
- height
;
153 dst_y
= dst_mt
->level
[dst_level
].height
- dst_y
- height
;
155 int src_pitch
= src_mt
->region
->pitch
;
156 if (src_flip
!= dst_flip
)
157 src_pitch
= -src_pitch
;
159 uint32_t src_image_x
, src_image_y
;
160 intel_miptree_get_image_offset(src_mt
, src_level
, src_slice
,
161 &src_image_x
, &src_image_y
);
162 src_x
+= src_image_x
;
163 src_y
+= src_image_y
;
165 uint32_t dst_image_x
, dst_image_y
;
166 intel_miptree_get_image_offset(dst_mt
, dst_level
, dst_slice
,
167 &dst_image_x
, &dst_image_y
);
168 dst_x
+= dst_image_x
;
169 dst_y
+= dst_image_y
;
171 if (!intelEmitCopyBlit(intel
,
174 src_mt
->region
->bo
, src_mt
->offset
,
175 src_mt
->region
->tiling
,
176 dst_mt
->region
->pitch
,
177 dst_mt
->region
->bo
, dst_mt
->offset
,
178 dst_mt
->region
->tiling
,
186 if (src_mt
->format
== MESA_FORMAT_B8G8R8X8_UNORM
&&
187 dst_mt
->format
== MESA_FORMAT_B8G8R8A8_UNORM
) {
188 intel_miptree_set_alpha_to_one(intel
, dst_mt
,
199 intelEmitCopyBlit(struct intel_context
*intel
,
202 drm_intel_bo
*src_buffer
,
206 drm_intel_bo
*dst_buffer
,
209 GLshort src_x
, GLshort src_y
,
210 GLshort dst_x
, GLshort dst_y
,
211 GLshort w
, GLshort h
,
212 enum gl_logicop_mode logic_op
)
214 GLuint CMD
, BR13
, pass
= 0;
215 int dst_y2
= dst_y
+ h
;
216 int dst_x2
= dst_x
+ w
;
217 drm_intel_bo
*aper_array
[3];
218 bool dst_y_tiled
= dst_tiling
== I915_TILING_Y
;
219 bool src_y_tiled
= src_tiling
== I915_TILING_Y
;
222 if (dst_tiling
!= I915_TILING_NONE
) {
223 if (dst_offset
& 4095)
226 if (src_tiling
!= I915_TILING_NONE
) {
227 if (src_offset
& 4095)
230 if (dst_y_tiled
|| src_y_tiled
)
233 /* do space check before going any further */
235 aper_array
[0] = intel
->batch
.bo
;
236 aper_array
[1] = dst_buffer
;
237 aper_array
[2] = src_buffer
;
239 if (dri_bufmgr_check_aperture_space(aper_array
, 3) != 0) {
240 intel_batchbuffer_flush(intel
);
249 intel_batchbuffer_require_space(intel
, 8 * 4);
250 DBG("%s src:buf(%p)/%d+%d %d,%d dst:buf(%p)/%d+%d %d,%d sz:%dx%d\n",
252 src_buffer
, src_pitch
, src_offset
, src_x
, src_y
,
253 dst_buffer
, dst_pitch
, dst_offset
, dst_x
, dst_y
, w
, h
);
255 /* Blit pitch must be dword-aligned. Otherwise, the hardware appears to drop
256 * the low bits. Offsets must be naturally aligned.
258 if (src_pitch
% 4 != 0 || src_offset
% cpp
!= 0 ||
259 dst_pitch
% 4 != 0 || dst_offset
% cpp
!= 0)
262 /* For big formats (such as floating point), do the copy using 16 or 32bpp
263 * and multiply the coordinates.
272 assert(cpp
% 4 == 0);
280 BR13
= br13_for_cpp(cpp
) | translate_raster_op(logic_op
) << 16;
285 CMD
= XY_SRC_COPY_BLT_CMD
;
288 CMD
= XY_SRC_COPY_BLT_CMD
| XY_BLT_WRITE_ALPHA
| XY_BLT_WRITE_RGB
;
294 if (dst_y2
<= dst_y
|| dst_x2
<= dst_x
) {
298 assert(dst_x
< dst_x2
);
299 assert(dst_y
< dst_y2
);
303 OUT_BATCH(CMD
| (8 - 2));
304 OUT_BATCH(BR13
| (uint16_t)dst_pitch
);
305 OUT_BATCH((dst_y
<< 16) | dst_x
);
306 OUT_BATCH((dst_y2
<< 16) | dst_x2
);
307 OUT_RELOC_FENCED(dst_buffer
,
308 I915_GEM_DOMAIN_RENDER
, I915_GEM_DOMAIN_RENDER
,
310 OUT_BATCH((src_y
<< 16) | src_x
);
311 OUT_BATCH((uint16_t)src_pitch
);
312 OUT_RELOC_FENCED(src_buffer
,
313 I915_GEM_DOMAIN_RENDER
, 0,
318 intel_batchbuffer_emit_mi_flush(intel
);
325 * Use blitting to clear the renderbuffers named by 'flags'.
326 * Note: we can't use the ctx->DrawBuffer->_ColorDrawBufferIndexes field
327 * since that might include software renderbuffers or renderbuffers
328 * which we're clearing with triangles.
329 * \param mask bitmask of BUFFER_BIT_* values indicating buffers to clear
332 intelClearWithBlit(struct gl_context
*ctx
, GLbitfield mask
)
334 struct intel_context
*intel
= intel_context(ctx
);
335 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
336 GLuint clear_depth_value
, clear_depth_mask
;
337 GLint cx
, cy
, cw
, ch
;
338 GLbitfield fail_mask
= 0;
341 /* Note: we don't use this function on Gen7+ hardware, so we can safely
342 * ignore fast color clear issues.
344 assert(intel
->gen
< 7);
347 * Compute values for clearing the buffers.
349 clear_depth_value
= 0;
350 clear_depth_mask
= 0;
351 if (mask
& BUFFER_BIT_DEPTH
) {
352 clear_depth_value
= (GLuint
) (fb
->_DepthMax
* ctx
->Depth
.Clear
);
353 clear_depth_mask
= XY_BLT_WRITE_RGB
;
355 if (mask
& BUFFER_BIT_STENCIL
) {
356 clear_depth_value
|= (ctx
->Stencil
.Clear
& 0xff) << 24;
357 clear_depth_mask
|= XY_BLT_WRITE_ALPHA
;
361 if (_mesa_is_winsys_fbo(fb
))
362 cy
= ctx
->DrawBuffer
->Height
- fb
->_Ymax
;
365 cw
= fb
->_Xmax
- fb
->_Xmin
;
366 ch
= fb
->_Ymax
- fb
->_Ymin
;
368 if (cw
== 0 || ch
== 0)
371 /* Loop over all renderbuffers */
372 mask
&= (1 << BUFFER_COUNT
) - 1;
374 GLuint buf
= ffs(mask
) - 1;
375 bool is_depth_stencil
= buf
== BUFFER_DEPTH
|| buf
== BUFFER_STENCIL
;
376 struct intel_renderbuffer
*irb
;
380 struct intel_region
*region
;
382 drm_intel_bo
*aper_array
[2];
386 irb
= intel_get_renderbuffer(fb
, buf
);
387 if (irb
&& irb
->mt
) {
388 region
= irb
->mt
->region
;
392 fail_mask
|= 1 << buf
;
396 /* OK, clear this renderbuffer */
397 x1
= cx
+ irb
->draw_x
;
398 y1
= cy
+ irb
->draw_y
;
399 x2
= cx
+ cw
+ irb
->draw_x
;
400 y2
= cy
+ ch
+ irb
->draw_y
;
402 pitch
= region
->pitch
;
405 DBG("%s dst:buf(%p)/%d %d,%d sz:%dx%d\n",
408 x1
, y1
, x2
- x1
, y2
- y1
);
411 CMD
= XY_COLOR_BLT_CMD
;
413 /* Setup the blit command */
415 if (is_depth_stencil
) {
416 CMD
|= clear_depth_mask
;
419 CMD
|= XY_BLT_WRITE_ALPHA
| XY_BLT_WRITE_RGB
;
423 assert(region
->tiling
!= I915_TILING_Y
);
427 if (is_depth_stencil
) {
428 clear_val
= clear_depth_value
;
431 GLfloat
*color
= ctx
->Color
.ClearColor
.f
;
433 _mesa_unclamped_float_rgba_to_ubyte(clear
, color
);
435 switch (intel_rb_format(irb
)) {
436 case MESA_FORMAT_B8G8R8A8_UNORM
:
437 case MESA_FORMAT_B8G8R8X8_UNORM
:
438 clear_val
= PACK_COLOR_8888(clear
[3], clear
[0],
441 case MESA_FORMAT_B5G6R5_UNORM
:
442 clear_val
= PACK_COLOR_565(clear
[0], clear
[1], clear
[2]);
444 case MESA_FORMAT_B4G4R4A4_UNORM
:
445 clear_val
= PACK_COLOR_4444(clear
[3], clear
[0],
448 case MESA_FORMAT_B5G5R5A1_UNORM
:
449 clear_val
= PACK_COLOR_1555(clear
[3], clear
[0],
452 case MESA_FORMAT_A_UNORM8
:
453 clear_val
= PACK_COLOR_8888(clear
[3], clear
[3],
457 fail_mask
|= 1 << buf
;
462 BR13
|= br13_for_cpp(cpp
);
467 /* do space check before going any further */
468 aper_array
[0] = intel
->batch
.bo
;
469 aper_array
[1] = region
->bo
;
471 if (drm_intel_bufmgr_check_aperture_space(aper_array
,
472 ARRAY_SIZE(aper_array
)) != 0) {
473 intel_batchbuffer_flush(intel
);
477 OUT_BATCH(CMD
| (6 - 2));
479 OUT_BATCH((y1
<< 16) | x1
);
480 OUT_BATCH((y2
<< 16) | x2
);
481 OUT_RELOC_FENCED(region
->bo
,
482 I915_GEM_DOMAIN_RENDER
, I915_GEM_DOMAIN_RENDER
,
484 OUT_BATCH(clear_val
);
487 if (intel
->always_flush_cache
)
488 intel_batchbuffer_emit_mi_flush(intel
);
490 if (buf
== BUFFER_DEPTH
|| buf
== BUFFER_STENCIL
)
491 mask
&= ~(BUFFER_BIT_DEPTH
| BUFFER_BIT_STENCIL
);
498 intelEmitImmediateColorExpandBlit(struct intel_context
*intel
,
500 GLubyte
*src_bits
, GLuint src_size
,
503 drm_intel_bo
*dst_buffer
,
506 GLshort x
, GLshort y
,
507 GLshort w
, GLshort h
,
508 enum gl_logicop_mode logic_op
)
510 int dwords
= ALIGN(src_size
, 8) / 4;
511 uint32_t opcode
, br13
, blit_cmd
;
513 if (dst_tiling
!= I915_TILING_NONE
) {
514 if (dst_offset
& 4095)
516 if (dst_tiling
== I915_TILING_Y
)
520 assert((unsigned)logic_op
<= 0x0f);
521 assert(dst_pitch
> 0);
526 DBG("%s dst:buf(%p)/%d+%d %d,%d sz:%dx%d, %d bytes %d dwords\n",
528 dst_buffer
, dst_pitch
, dst_offset
, x
, y
, w
, h
, src_size
, dwords
);
530 intel_batchbuffer_require_space(intel
,
535 opcode
= XY_SETUP_BLT_CMD
;
537 opcode
|= XY_BLT_WRITE_ALPHA
| XY_BLT_WRITE_RGB
;
539 br13
= dst_pitch
| (translate_raster_op(logic_op
) << 16) | (1 << 29);
540 br13
|= br13_for_cpp(cpp
);
542 blit_cmd
= XY_TEXT_IMMEDIATE_BLIT_CMD
| XY_TEXT_BYTE_PACKED
; /* packing? */
543 if (dst_tiling
!= I915_TILING_NONE
)
544 blit_cmd
|= XY_DST_TILED
;
547 OUT_BATCH(opcode
| (8 - 2));
549 OUT_BATCH((0 << 16) | 0); /* clip x1, y1 */
550 OUT_BATCH((100 << 16) | 100); /* clip x2, y2 */
551 OUT_RELOC_FENCED(dst_buffer
,
552 I915_GEM_DOMAIN_RENDER
, I915_GEM_DOMAIN_RENDER
,
554 OUT_BATCH(0); /* bg */
555 OUT_BATCH(fg_color
); /* fg */
556 OUT_BATCH(0); /* pattern base addr */
558 OUT_BATCH(blit_cmd
| ((3 - 2) + dwords
));
559 OUT_BATCH((y
<< 16) | x
);
560 OUT_BATCH(((y
+ h
) << 16) | (x
+ w
));
563 intel_batchbuffer_data(intel
, src_bits
, dwords
* 4);
565 intel_batchbuffer_emit_mi_flush(intel
);
570 /* We don't have a memmove-type blit like some other hardware, so we'll do a
571 * rectangular blit covering a large space, then emit 1-scanline blit at the
572 * end to cover the last if we need.
575 intel_emit_linear_blit(struct intel_context
*intel
,
576 drm_intel_bo
*dst_bo
,
577 unsigned int dst_offset
,
578 drm_intel_bo
*src_bo
,
579 unsigned int src_offset
,
582 struct gl_context
*ctx
= &intel
->ctx
;
583 GLuint pitch
, height
;
586 /* The pitch given to the GPU must be DWORD aligned, and
587 * we want width to match pitch. Max width is (1 << 15 - 1),
588 * rounding that down to the nearest DWORD is 1 << 15 - 4
590 pitch
= ROUND_DOWN_TO(MIN2(size
, (1 << 15) - 1), 4);
591 height
= (pitch
== 0) ? 1 : size
/ pitch
;
592 ok
= intelEmitCopyBlit(intel
, 1,
593 pitch
, src_bo
, src_offset
, I915_TILING_NONE
,
594 pitch
, dst_bo
, dst_offset
, I915_TILING_NONE
,
597 pitch
, height
, /* w, h */
600 _mesa_problem(ctx
, "Failed to linear blit %dx%d\n", pitch
, height
);
602 src_offset
+= pitch
* height
;
603 dst_offset
+= pitch
* height
;
604 size
-= pitch
* height
;
605 assert (size
< (1 << 15));
606 pitch
= ALIGN(size
, 4);
608 ok
= intelEmitCopyBlit(intel
, 1,
609 pitch
, src_bo
, src_offset
, I915_TILING_NONE
,
610 pitch
, dst_bo
, dst_offset
, I915_TILING_NONE
,
616 _mesa_problem(ctx
, "Failed to linear blit %dx%d\n", size
, 1);
621 * Used to initialize the alpha value of an ARGB8888 miptree after copying
622 * into it from an XRGB8888 source.
624 * This is very common with glCopyTexImage2D(). Note that the coordinates are
625 * relative to the start of the miptree, not relative to a slice within the
629 intel_miptree_set_alpha_to_one(struct intel_context
*intel
,
630 struct intel_mipmap_tree
*mt
,
631 int x
, int y
, int width
, int height
)
633 struct intel_region
*region
= mt
->region
;
636 drm_intel_bo
*aper_array
[2];
639 pitch
= region
->pitch
;
642 DBG("%s dst:buf(%p)/%d %d,%d sz:%dx%d\n",
643 __func__
, region
->bo
, pitch
, x
, y
, width
, height
);
645 BR13
= br13_for_cpp(cpp
) | 0xf0 << 16;
646 CMD
= XY_COLOR_BLT_CMD
;
647 CMD
|= XY_BLT_WRITE_ALPHA
;
651 /* do space check before going any further */
652 aper_array
[0] = intel
->batch
.bo
;
653 aper_array
[1] = region
->bo
;
655 if (drm_intel_bufmgr_check_aperture_space(aper_array
,
656 ARRAY_SIZE(aper_array
)) != 0) {
657 intel_batchbuffer_flush(intel
);
661 OUT_BATCH(CMD
| (6 - 2));
663 OUT_BATCH((y
<< 16) | x
);
664 OUT_BATCH(((y
+ height
) << 16) | (x
+ width
));
665 OUT_RELOC_FENCED(region
->bo
,
666 I915_GEM_DOMAIN_RENDER
, I915_GEM_DOMAIN_RENDER
,
668 OUT_BATCH(0xffffffff); /* white, but only alpha gets written */
671 intel_batchbuffer_emit_mi_flush(intel
);