1 /**************************************************************************
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
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 TUNGSTEN GRAPHICS 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/texformat.h"
33 #include "main/colormac.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_chipset.h"
44 #define FILE_DEBUG_FLAG DEBUG_BLIT
47 * Copy the back color buffer to the front color buffer.
48 * Used for SwapBuffers().
51 intelCopyBuffer(const __DRIdrawablePrivate
* dPriv
,
52 const drm_clip_rect_t
* rect
)
55 struct intel_context
*intel
;
56 const intelScreenPrivate
*intelScreen
;
58 DBG("%s\n", __FUNCTION__
);
62 intel
= intelScreenContext(dPriv
->driScreenPriv
->private);
66 intelScreen
= intel
->intelScreen
;
68 /* The LOCK_HARDWARE is required for the cliprects. Buffer offsets
69 * should work regardless.
73 if (dPriv
&& dPriv
->numClipRects
) {
74 struct intel_framebuffer
*intel_fb
= dPriv
->driverPrivate
;
75 struct intel_region
*src
, *dst
;
76 int nbox
= dPriv
->numClipRects
;
77 drm_clip_rect_t
*pbox
= dPriv
->pClipRects
;
79 int src_pitch
, dst_pitch
;
80 unsigned short src_x
, src_y
;
83 dri_bo
*aper_array
[3];
85 src
= intel_get_rb_region(&intel_fb
->Base
, BUFFER_BACK_LEFT
);
86 dst
= intel_get_rb_region(&intel_fb
->Base
, BUFFER_FRONT_LEFT
);
88 src_pitch
= src
->pitch
* src
->cpp
;
89 dst_pitch
= dst
->pitch
* dst
->cpp
;
94 ASSERT(intel_fb
->Base
.Name
== 0); /* Not a user-created FBO */
97 ASSERT(src
->cpp
== dst
->cpp
);
100 BR13
= (0xCC << 16) | BR13_565
;
101 CMD
= XY_SRC_COPY_BLT_CMD
;
104 BR13
= (0xCC << 16) | BR13_8888
;
105 CMD
= XY_SRC_COPY_BLT_CMD
| XY_BLT_WRITE_ALPHA
| XY_BLT_WRITE_RGB
;
108 assert(src
->tiling
!= I915_TILING_Y
);
109 assert(dst
->tiling
!= I915_TILING_Y
);
111 if (src
->tiling
!= I915_TILING_NONE
) {
115 if (dst
->tiling
!= I915_TILING_NONE
) {
120 /* do space/cliprects check before going any further */
121 intel_batchbuffer_require_space(intel
->batch
, 8 * 4,
122 REFERENCES_CLIPRECTS
);
124 aper_array
[0] = intel
->batch
->buf
;
125 aper_array
[1] = dst
->buffer
;
126 aper_array
[2] = src
->buffer
;
128 if (dri_bufmgr_check_aperture_space(aper_array
, 3) != 0) {
129 intel_batchbuffer_flush(intel
->batch
);
133 for (i
= 0; i
< nbox
; i
++, pbox
++) {
134 drm_clip_rect_t box
= *pbox
;
137 if (!intel_intersect_cliprects(&box
, &box
, rect
))
141 if (box
.x1
>= box
.x2
||
145 assert(box
.x1
< box
.x2
);
146 assert(box
.y1
< box
.y2
);
147 src_x
= box
.x1
- dPriv
->x
+ dPriv
->backX
;
148 src_y
= box
.y1
- dPriv
->y
+ dPriv
->backY
;
150 BEGIN_BATCH(8, REFERENCES_CLIPRECTS
);
152 OUT_BATCH(BR13
| dst_pitch
);
153 OUT_BATCH((box
.y1
<< 16) | box
.x1
);
154 OUT_BATCH((box
.y2
<< 16) | box
.x2
);
156 OUT_RELOC(dst
->buffer
,
157 I915_GEM_DOMAIN_RENDER
, I915_GEM_DOMAIN_RENDER
,
159 OUT_BATCH((src_y
<< 16) | src_x
);
160 OUT_BATCH(src_pitch
);
161 OUT_RELOC(src
->buffer
,
162 I915_GEM_DOMAIN_RENDER
, 0,
167 /* Flush the rendering and the batch so that the results all land on the
168 * screen in a timely fashion.
170 intel_batchbuffer_emit_mi_flush(intel
->batch
);
171 intel_batchbuffer_flush(intel
->batch
);
174 UNLOCK_HARDWARE(intel
);
177 static GLuint
translate_raster_op(GLenum logicop
)
180 case GL_CLEAR
: return 0x00;
181 case GL_AND
: return 0x88;
182 case GL_AND_REVERSE
: return 0x44;
183 case GL_COPY
: return 0xCC;
184 case GL_AND_INVERTED
: return 0x22;
185 case GL_NOOP
: return 0xAA;
186 case GL_XOR
: return 0x66;
187 case GL_OR
: return 0xEE;
188 case GL_NOR
: return 0x11;
189 case GL_EQUIV
: return 0x99;
190 case GL_INVERT
: return 0x55;
191 case GL_OR_REVERSE
: return 0xDD;
192 case GL_COPY_INVERTED
: return 0x33;
193 case GL_OR_INVERTED
: return 0xBB;
194 case GL_NAND
: return 0x77;
195 case GL_SET
: return 0xFF;
204 intelEmitCopyBlit(struct intel_context
*intel
,
214 GLshort src_x
, GLshort src_y
,
215 GLshort dst_x
, GLshort dst_y
,
216 GLshort w
, GLshort h
,
219 GLuint CMD
, BR13
, pass
= 0;
220 int dst_y2
= dst_y
+ h
;
221 int dst_x2
= dst_x
+ w
;
222 dri_bo
*aper_array
[3];
225 if (dst_tiling
!= I915_TILING_NONE
) {
226 if (dst_offset
& 4095)
228 if (dst_tiling
== I915_TILING_Y
)
231 if (src_tiling
!= I915_TILING_NONE
) {
232 if (src_offset
& 4095)
234 if (src_tiling
== I915_TILING_Y
)
238 /* do space/cliprects check before going any further */
240 aper_array
[0] = intel
->batch
->buf
;
241 aper_array
[1] = dst_buffer
;
242 aper_array
[2] = src_buffer
;
244 if (dri_bufmgr_check_aperture_space(aper_array
, 3) != 0) {
245 intel_batchbuffer_flush(intel
->batch
);
252 LOCK_HARDWARE(intel
);
253 dri_bo_map(dst_buffer
, GL_TRUE
);
254 dri_bo_map(src_buffer
, GL_FALSE
);
255 _mesa_copy_rect((GLubyte
*)dst_buffer
->virtual + dst_offset
,
260 (GLubyte
*)src_buffer
->virtual + src_offset
,
264 dri_bo_unmap(src_buffer
);
265 dri_bo_unmap(dst_buffer
);
266 UNLOCK_HARDWARE(intel
);
271 intel_batchbuffer_require_space(intel
->batch
, 8 * 4, NO_LOOP_CLIPRECTS
);
272 DBG("%s src:buf(%p)/%d+%d %d,%d dst:buf(%p)/%d+%d %d,%d sz:%dx%d\n",
274 src_buffer
, src_pitch
, src_offset
, src_x
, src_y
,
275 dst_buffer
, dst_pitch
, dst_offset
, dst_x
, dst_y
, w
, h
);
280 BR13
= translate_raster_op(logic_op
) << 16;
284 CMD
= XY_SRC_COPY_BLT_CMD
;
288 CMD
= XY_SRC_COPY_BLT_CMD
;
292 CMD
= XY_SRC_COPY_BLT_CMD
| XY_BLT_WRITE_ALPHA
| XY_BLT_WRITE_RGB
;
299 if (dst_tiling
!= I915_TILING_NONE
) {
303 if (src_tiling
!= I915_TILING_NONE
) {
309 if (dst_y2
<= dst_y
|| dst_x2
<= dst_x
) {
313 assert(dst_x
< dst_x2
);
314 assert(dst_y
< dst_y2
);
316 BEGIN_BATCH(8, NO_LOOP_CLIPRECTS
);
318 OUT_BATCH(BR13
| (uint16_t)dst_pitch
);
319 OUT_BATCH((dst_y
<< 16) | dst_x
);
320 OUT_BATCH((dst_y2
<< 16) | dst_x2
);
321 OUT_RELOC(dst_buffer
,
322 I915_GEM_DOMAIN_RENDER
, I915_GEM_DOMAIN_RENDER
,
324 OUT_BATCH((src_y
<< 16) | src_x
);
325 OUT_BATCH((uint16_t)src_pitch
);
326 OUT_RELOC(src_buffer
,
327 I915_GEM_DOMAIN_RENDER
, 0,
331 intel_batchbuffer_emit_mi_flush(intel
->batch
);
338 * Use blitting to clear the renderbuffers named by 'flags'.
339 * Note: we can't use the ctx->DrawBuffer->_ColorDrawBufferIndexes field
340 * since that might include software renderbuffers or renderbuffers
341 * which we're clearing with triangles.
342 * \param mask bitmask of BUFFER_BIT_* values indicating buffers to clear
345 intelClearWithBlit(GLcontext
*ctx
, GLbitfield mask
)
347 struct intel_context
*intel
= intel_context(ctx
);
348 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
350 GLbitfield skipBuffers
= 0;
351 unsigned int num_cliprects
;
352 struct drm_clip_rect
*cliprects
;
357 * Compute values for clearing the buffers.
360 if (mask
& BUFFER_BIT_DEPTH
) {
361 clear_depth
= (GLuint
) (fb
->_DepthMax
* ctx
->Depth
.Clear
);
363 if (mask
& BUFFER_BIT_STENCIL
) {
364 clear_depth
|= (ctx
->Stencil
.Clear
& 0xff) << 24;
367 /* If clearing both depth and stencil, skip BUFFER_BIT_STENCIL in
370 if ((mask
& BUFFER_BIT_DEPTH
) && (mask
& BUFFER_BIT_STENCIL
)) {
371 skipBuffers
= BUFFER_BIT_STENCIL
;
374 LOCK_HARDWARE(intel
);
376 intel_get_cliprects(intel
, &cliprects
, &num_cliprects
, &x_off
, &y_off
);
378 GLint cx
, cy
, cw
, ch
;
379 drm_clip_rect_t clear
;
382 /* Get clear bounds after locking */
389 /* clearing a window */
391 /* flip top to bottom */
392 clear
.x1
= cx
+ x_off
;
393 clear
.y1
= intel
->driDrawable
->y
+ intel
->driDrawable
->h
- cy
- ch
;
394 clear
.x2
= clear
.x1
+ cw
;
395 clear
.y2
= clear
.y1
+ ch
;
399 assert(num_cliprects
== 1);
400 assert(cliprects
== &intel
->fboRect
);
403 clear
.x2
= clear
.x1
+ cw
;
404 clear
.y2
= clear
.y1
+ ch
;
405 /* no change to mask */
408 for (i
= 0; i
< num_cliprects
; i
++) {
409 const drm_clip_rect_t
*box
= &cliprects
[i
];
412 GLuint clearMask
= mask
; /* use copy, since we modify it below */
413 GLboolean all
= (cw
== fb
->Width
&& ch
== fb
->Height
);
416 intel_intersect_cliprects(&b
, &clear
, box
);
422 if (b
.x1
>= b
.x2
|| b
.y1
>= b
.y2
)
426 _mesa_printf("clear %d,%d..%d,%d, mask %x\n",
427 b
.x1
, b
.y1
, b
.x2
, b
.y2
, mask
);
429 /* Loop over all renderbuffers */
430 for (buf
= 0; buf
< BUFFER_COUNT
&& clearMask
; buf
++) {
431 const GLbitfield bufBit
= 1 << buf
;
432 if ((clearMask
& bufBit
) && !(bufBit
& skipBuffers
)) {
433 /* OK, clear this renderbuffer */
434 struct intel_renderbuffer
*irb
= intel_get_renderbuffer(fb
, buf
);
435 dri_bo
*write_buffer
=
436 intel_region_buffer(intel
, irb
->region
,
437 all
? INTEL_WRITE_FULL
:
444 pitch
= irb
->region
->pitch
;
445 cpp
= irb
->region
->cpp
;
447 DBG("%s dst:buf(%p)/%d+%d %d,%d sz:%dx%d\n",
449 irb
->region
->buffer
, (pitch
* cpp
),
450 irb
->region
->draw_offset
,
451 b
.x1
, b
.y1
, b
.x2
- b
.x1
, b
.y2
- b
.y1
);
454 CMD
= XY_COLOR_BLT_CMD
;
456 /* Setup the blit command */
459 if (buf
== BUFFER_DEPTH
|| buf
== BUFFER_STENCIL
) {
460 if (clearMask
& BUFFER_BIT_DEPTH
)
461 CMD
|= XY_BLT_WRITE_RGB
;
462 if (clearMask
& BUFFER_BIT_STENCIL
)
463 CMD
|= XY_BLT_WRITE_ALPHA
;
467 CMD
|= XY_BLT_WRITE_ALPHA
| XY_BLT_WRITE_RGB
;
475 assert(irb
->region
->tiling
!= I915_TILING_Y
);
478 if (irb
->region
->tiling
!= I915_TILING_NONE
) {
483 BR13
|= (pitch
* cpp
);
485 if (buf
== BUFFER_DEPTH
|| buf
== BUFFER_STENCIL
) {
486 clearVal
= clear_depth
;
490 GLclampf
*color
= ctx
->Color
.ClearColor
;
492 CLAMPED_FLOAT_TO_UBYTE(clear
[0], color
[0]);
493 CLAMPED_FLOAT_TO_UBYTE(clear
[1], color
[1]);
494 CLAMPED_FLOAT_TO_UBYTE(clear
[2], color
[2]);
495 CLAMPED_FLOAT_TO_UBYTE(clear
[3], color
[3]);
497 switch (irb
->texformat
->MesaFormat
) {
498 case MESA_FORMAT_ARGB8888
:
499 clearVal
= intel
->ClearColor8888
;
501 case MESA_FORMAT_RGB565
:
502 clearVal
= intel
->ClearColor565
;
504 case MESA_FORMAT_ARGB4444
:
505 clearVal
= PACK_COLOR_4444(clear
[3], clear
[0],
508 case MESA_FORMAT_ARGB1555
:
509 clearVal
= PACK_COLOR_1555(clear
[3], clear
[0],
513 _mesa_problem(ctx
, "Unexpected renderbuffer format: %d\n",
514 irb
->texformat
->MesaFormat
);
520 _mesa_debug(ctx, "hardware blit clear buf %d rb id %d\n",
521 buf, irb->Base.Name);
527 BEGIN_BATCH(6, REFERENCES_CLIPRECTS
);
530 OUT_BATCH((b
.y1
<< 16) | b
.x1
);
531 OUT_BATCH((b
.y2
<< 16) | b
.x2
);
532 OUT_RELOC(write_buffer
,
533 I915_GEM_DOMAIN_RENDER
, I915_GEM_DOMAIN_RENDER
,
534 irb
->region
->draw_offset
);
537 clearMask
&= ~bufBit
; /* turn off bit, for faster loop exit */
543 UNLOCK_HARDWARE(intel
);
547 intelEmitImmediateColorExpandBlit(struct intel_context
*intel
,
549 GLubyte
*src_bits
, GLuint src_size
,
555 GLshort x
, GLshort y
,
556 GLshort w
, GLshort h
,
559 int dwords
= ALIGN(src_size
, 8) / 4;
560 uint32_t opcode
, br13
, blit_cmd
;
562 if (dst_tiling
!= I915_TILING_NONE
) {
563 if (dst_offset
& 4095)
565 if (dst_tiling
== I915_TILING_Y
)
569 assert( logic_op
- GL_CLEAR
>= 0 );
570 assert( logic_op
- GL_CLEAR
< 0x10 );
571 assert(dst_pitch
> 0);
578 DBG("%s dst:buf(%p)/%d+%d %d,%d sz:%dx%d, %d bytes %d dwords\n",
580 dst_buffer
, dst_pitch
, dst_offset
, x
, y
, w
, h
, src_size
, dwords
);
582 intel_batchbuffer_require_space( intel
->batch
,
586 REFERENCES_CLIPRECTS
);
588 opcode
= XY_SETUP_BLT_CMD
;
590 opcode
|= XY_BLT_WRITE_ALPHA
| XY_BLT_WRITE_RGB
;
592 if (dst_tiling
!= I915_TILING_NONE
) {
593 opcode
|= XY_DST_TILED
;
598 br13
= dst_pitch
| (translate_raster_op(logic_op
) << 16) | (1 << 29);
604 blit_cmd
= XY_TEXT_IMMEDIATE_BLIT_CMD
| XY_TEXT_BYTE_PACKED
; /* packing? */
605 if (dst_tiling
!= I915_TILING_NONE
)
606 blit_cmd
|= XY_DST_TILED
;
608 BEGIN_BATCH(8 + 3, REFERENCES_CLIPRECTS
);
611 OUT_BATCH((0 << 16) | 0); /* clip x1, y1 */
612 OUT_BATCH((100 << 16) | 100); /* clip x2, y2 */
613 OUT_RELOC(dst_buffer
,
614 I915_GEM_DOMAIN_RENDER
, I915_GEM_DOMAIN_RENDER
,
616 OUT_BATCH(0); /* bg */
617 OUT_BATCH(fg_color
); /* fg */
618 OUT_BATCH(0); /* pattern base addr */
620 OUT_BATCH(blit_cmd
| ((3 - 2) + dwords
));
621 OUT_BATCH((y
<< 16) | x
);
622 OUT_BATCH(((y
+ h
) << 16) | (x
+ w
));
625 intel_batchbuffer_data( intel
->batch
,
628 REFERENCES_CLIPRECTS
);
630 intel_batchbuffer_emit_mi_flush(intel
->batch
);
635 /* We don't have a memmove-type blit like some other hardware, so we'll do a
636 * rectangular blit covering a large space, then emit 1-scanline blit at the
637 * end to cover the last if we need.
640 intel_emit_linear_blit(struct intel_context
*intel
,
641 drm_intel_bo
*dst_bo
,
642 unsigned int dst_offset
,
643 drm_intel_bo
*src_bo
,
644 unsigned int src_offset
,
647 GLuint pitch
, height
;
649 /* The pitch is a signed value. */
650 pitch
= MIN2(size
, (1 << 15) - 1);
651 height
= size
/ pitch
;
652 intelEmitCopyBlit(intel
, 1,
653 pitch
, src_bo
, src_offset
, I915_TILING_NONE
,
654 pitch
, dst_bo
, dst_offset
, I915_TILING_NONE
,
657 pitch
, height
, /* w, h */
660 src_offset
+= pitch
* height
;
661 dst_offset
+= pitch
* height
;
662 size
-= pitch
* height
;
663 assert (size
< (1 << 15));
665 intelEmitCopyBlit(intel
, 1,
666 size
, src_bo
, src_offset
, I915_TILING_NONE
,
667 size
, dst_bo
, dst_offset
, I915_TILING_NONE
,