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 **************************************************************************/
36 #include "intel_batchbuffer.h"
37 #include "intel_blit.h"
38 #include "intel_buffers.h"
39 #include "intel_context.h"
40 #include "intel_fbo.h"
41 #include "intel_reg.h"
42 #include "intel_regions.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
;
59 DBG("%s\n", __FUNCTION__
);
63 intel
= intelScreenContext(dPriv
->driScreenPriv
->private);
67 intelScreen
= intel
->intelScreen
;
69 /* The LOCK_HARDWARE is required for the cliprects. Buffer offsets
70 * should work regardless.
74 if (dPriv
&& dPriv
->numClipRects
) {
75 struct intel_framebuffer
*intel_fb
= dPriv
->driverPrivate
;
76 struct intel_region
*src
, *dst
;
77 int nbox
= dPriv
->numClipRects
;
78 drm_clip_rect_t
*pbox
= dPriv
->pClipRects
;
80 int src_pitch
, dst_pitch
;
81 unsigned short src_x
, src_y
;
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) | (1 << 24);
101 CMD
= XY_SRC_COPY_BLT_CMD
;
104 BR13
= (0xCC << 16) | (1 << 24) | (1 << 25);
105 CMD
= XY_SRC_COPY_BLT_CMD
| XY_BLT_WRITE_ALPHA
| XY_BLT_WRITE_RGB
;
120 ret
= dri_bufmgr_check_aperture_space(dst
->buffer
);
121 ret
|= dri_bufmgr_check_aperture_space(src
->buffer
);
124 intel_batchbuffer_flush(intel
->batch
);
128 for (i
= 0; i
< nbox
; i
++, pbox
++) {
129 drm_clip_rect_t box
= *pbox
;
132 if (!intel_intersect_cliprects(&box
, &box
, rect
))
136 if (box
.x1
>= box
.x2
||
140 assert(box
.x1
< box
.x2
);
141 assert(box
.y1
< box
.y2
);
142 src_x
= box
.x1
- dPriv
->x
+ dPriv
->backX
;
143 src_y
= box
.y1
- dPriv
->y
+ dPriv
->backY
;
145 BEGIN_BATCH(8, REFERENCES_CLIPRECTS
);
147 OUT_BATCH(BR13
| dst_pitch
);
148 OUT_BATCH((box
.y1
<< 16) | box
.x1
);
149 OUT_BATCH((box
.y2
<< 16) | box
.x2
);
151 OUT_RELOC(dst
->buffer
,
152 DRM_GEM_DOMAIN_I915_RENDER
, DRM_GEM_DOMAIN_I915_RENDER
,
154 OUT_BATCH((src_y
<< 16) | src_x
);
155 OUT_BATCH(src_pitch
);
156 OUT_RELOC(src
->buffer
,
157 DRM_GEM_DOMAIN_I915_RENDER
, 0,
162 /* Emit a flush so that, on systems where we don't have automatic flushing
163 * set (such as 965), the results all land on the screen in a timely
166 BEGIN_BATCH(1, IGNORE_CLIPRECTS
);
170 intel_batchbuffer_flush(intel
->batch
);
173 UNLOCK_HARDWARE(intel
);
180 intelEmitFillBlit(struct intel_context
*intel
,
186 GLshort x
, GLshort y
,
187 GLshort w
, GLshort h
,
199 BR13
= (0xF0 << 16) | (1 << 24);
200 CMD
= XY_COLOR_BLT_CMD
;
203 BR13
= (0xF0 << 16) | (1 << 24) | (1 << 25);
204 CMD
= XY_COLOR_BLT_CMD
| XY_BLT_WRITE_ALPHA
| XY_BLT_WRITE_RGB
;
216 DBG("%s dst:buf(%p)/%d+%d %d,%d sz:%dx%d\n",
217 __FUNCTION__
, dst_buffer
, dst_pitch
, dst_offset
, x
, y
, w
, h
);
222 BEGIN_BATCH(6, NO_LOOP_CLIPRECTS
);
224 OUT_BATCH(BR13
| dst_pitch
);
225 OUT_BATCH((y
<< 16) | x
);
226 OUT_BATCH(((y
+ h
) << 16) | (x
+ w
));
227 OUT_RELOC(dst_buffer
,
228 DRM_GEM_DOMAIN_I915_RENDER
, DRM_GEM_DOMAIN_I915_RENDER
,
234 static GLuint
translate_raster_op(GLenum logicop
)
237 case GL_CLEAR
: return 0x00;
238 case GL_AND
: return 0x88;
239 case GL_AND_REVERSE
: return 0x44;
240 case GL_COPY
: return 0xCC;
241 case GL_AND_INVERTED
: return 0x22;
242 case GL_NOOP
: return 0xAA;
243 case GL_XOR
: return 0x66;
244 case GL_OR
: return 0xEE;
245 case GL_NOR
: return 0x11;
246 case GL_EQUIV
: return 0x99;
247 case GL_INVERT
: return 0x55;
248 case GL_OR_REVERSE
: return 0xDD;
249 case GL_COPY_INVERTED
: return 0x33;
250 case GL_OR_INVERTED
: return 0xBB;
251 case GL_NAND
: return 0x77;
252 case GL_SET
: return 0xFF;
261 intelEmitCopyBlit(struct intel_context
*intel
,
271 GLshort src_x
, GLshort src_y
,
272 GLshort dst_x
, GLshort dst_y
,
273 GLshort w
, GLshort h
,
277 int dst_y2
= dst_y
+ h
;
278 int dst_x2
= dst_x
+ w
;
283 ret
= dri_bufmgr_check_aperture_space(dst_buffer
);
284 ret
|= dri_bufmgr_check_aperture_space(src_buffer
);
286 intel_batchbuffer_flush(intel
->batch
);
290 DBG("%s src:buf(%p)/%d+%d %d,%d dst:buf(%p)/%d+%d %d,%d sz:%dx%d\n",
292 src_buffer
, src_pitch
, src_offset
, src_x
, src_y
,
293 dst_buffer
, dst_pitch
, dst_offset
, dst_x
, dst_y
, w
, h
);
298 BR13
= translate_raster_op(logic_op
) << 16;
305 CMD
= XY_SRC_COPY_BLT_CMD
;
308 BR13
|= (1 << 24) | (1 << 25);
309 CMD
= XY_SRC_COPY_BLT_CMD
| XY_BLT_WRITE_ALPHA
| XY_BLT_WRITE_RGB
;
326 if (dst_y2
<= dst_y
|| dst_x2
<= dst_x
) {
333 /* Initial y values don't seem to work with negative pitches. If
334 * we adjust the offsets manually (below), it seems to work fine.
336 * On the other hand, if we always adjust, the hardware doesn't
337 * know which blit directions to use, so overlapping copypixels get
340 if (dst_pitch
> 0 && src_pitch
> 0) {
341 assert(dst_x
< dst_x2
);
342 assert(dst_y
< dst_y2
);
344 BEGIN_BATCH(8, NO_LOOP_CLIPRECTS
);
346 OUT_BATCH(BR13
| dst_pitch
);
347 OUT_BATCH((dst_y
<< 16) | dst_x
);
348 OUT_BATCH((dst_y2
<< 16) | dst_x2
);
349 OUT_RELOC(dst_buffer
,
350 DRM_GEM_DOMAIN_I915_RENDER
, DRM_GEM_DOMAIN_I915_RENDER
,
352 OUT_BATCH((src_y
<< 16) | src_x
);
353 OUT_BATCH(src_pitch
);
354 OUT_RELOC(src_buffer
,
355 DRM_GEM_DOMAIN_I915_RENDER
, 0,
360 assert(dst_x
< dst_x2
);
363 BEGIN_BATCH(8, NO_LOOP_CLIPRECTS
);
365 OUT_BATCH(BR13
| dst_pitch
);
366 OUT_BATCH((0 << 16) | dst_x
);
367 OUT_BATCH((h
<< 16) | dst_x2
);
368 OUT_RELOC(dst_buffer
,
369 DRM_GEM_DOMAIN_I915_RENDER
, DRM_GEM_DOMAIN_I915_RENDER
,
370 dst_offset
+ dst_y
* dst_pitch
);
371 OUT_BATCH((0 << 16) | src_x
);
372 OUT_BATCH(src_pitch
);
373 OUT_RELOC(src_buffer
,
374 DRM_GEM_DOMAIN_I915_RENDER
, 0,
375 src_offset
+ src_y
* src_pitch
);
378 BEGIN_BATCH(1, NO_LOOP_CLIPRECTS
);
381 intel_batchbuffer_flush(intel
->batch
);
386 * Use blitting to clear the renderbuffers named by 'flags'.
387 * Note: we can't use the ctx->DrawBuffer->_ColorDrawBufferIndexes field
388 * since that might include software renderbuffers or renderbuffers
389 * which we're clearing with triangles.
390 * \param mask bitmask of BUFFER_BIT_* values indicating buffers to clear
393 intelClearWithBlit(GLcontext
*ctx
, GLbitfield mask
)
395 struct intel_context
*intel
= intel_context(ctx
);
396 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
398 GLbitfield skipBuffers
= 0;
402 * Compute values for clearing the buffers.
405 if (mask
& BUFFER_BIT_DEPTH
) {
406 clear_depth
= (GLuint
) (fb
->_DepthMax
* ctx
->Depth
.Clear
);
408 if (mask
& BUFFER_BIT_STENCIL
) {
409 clear_depth
|= (ctx
->Stencil
.Clear
& 0xff) << 24;
412 /* If clearing both depth and stencil, skip BUFFER_BIT_STENCIL in
415 if ((mask
& BUFFER_BIT_DEPTH
) && (mask
& BUFFER_BIT_STENCIL
)) {
416 skipBuffers
= BUFFER_BIT_STENCIL
;
419 /* XXX Move this flush/lock into the following conditional? */
420 intelFlush(&intel
->ctx
);
421 LOCK_HARDWARE(intel
);
423 if (intel
->numClipRects
) {
424 GLint cx
, cy
, cw
, ch
;
425 drm_clip_rect_t clear
;
428 /* Get clear bounds after locking */
435 /* clearing a window */
437 /* flip top to bottom */
438 clear
.x1
= cx
+ intel
->drawX
;
439 clear
.y1
= intel
->driDrawable
->y
+ intel
->driDrawable
->h
- cy
- ch
;
440 clear
.x2
= clear
.x1
+ cw
;
441 clear
.y2
= clear
.y1
+ ch
;
445 assert(intel
->numClipRects
== 1);
446 assert(intel
->pClipRects
== &intel
->fboRect
);
449 clear
.x2
= clear
.x1
+ cw
;
450 clear
.y2
= clear
.y1
+ ch
;
451 /* no change to mask */
454 for (i
= 0; i
< intel
->numClipRects
; i
++) {
455 const drm_clip_rect_t
*box
= &intel
->pClipRects
[i
];
458 GLuint clearMask
= mask
; /* use copy, since we modify it below */
459 GLboolean all
= (cw
== fb
->Width
&& ch
== fb
->Height
);
462 intel_intersect_cliprects(&b
, &clear
, box
);
468 if (b
.x1
>= b
.x2
|| b
.y1
>= b
.y2
)
472 _mesa_printf("clear %d,%d..%d,%d, mask %x\n",
473 b
.x1
, b
.y1
, b
.x2
, b
.y2
, mask
);
475 /* Loop over all renderbuffers */
476 for (buf
= 0; buf
< BUFFER_COUNT
&& clearMask
; buf
++) {
477 const GLbitfield bufBit
= 1 << buf
;
478 if ((clearMask
& bufBit
) && !(bufBit
& skipBuffers
)) {
479 /* OK, clear this renderbuffer */
480 struct intel_region
*irb_region
=
481 intel_get_rb_region(fb
, buf
);
482 dri_bo
*write_buffer
=
483 intel_region_buffer(intel
, irb_region
,
484 all
? INTEL_WRITE_FULL
:
493 pitch
= irb_region
->pitch
;
494 cpp
= irb_region
->cpp
;
496 DBG("%s dst:buf(%p)/%d+%d %d,%d sz:%dx%d\n",
498 irb_region
->buffer
, (pitch
* cpp
),
499 irb_region
->draw_offset
,
500 b
.x1
, b
.y1
, b
.x2
- b
.x1
, b
.y2
- b
.y1
);
503 CMD
= XY_COLOR_BLT_CMD
;
505 /* Setup the blit command */
507 BR13
|= (1 << 24) | (1 << 25);
508 if (buf
== BUFFER_DEPTH
|| buf
== BUFFER_STENCIL
) {
509 if (clearMask
& BUFFER_BIT_DEPTH
)
510 CMD
|= XY_BLT_WRITE_RGB
;
511 if (clearMask
& BUFFER_BIT_STENCIL
)
512 CMD
|= XY_BLT_WRITE_ALPHA
;
516 CMD
|= XY_BLT_WRITE_ALPHA
| XY_BLT_WRITE_RGB
;
520 ASSERT(cpp
== 2 || cpp
== 0);
525 if (irb_region
->tiled
) {
530 BR13
|= (pitch
* cpp
);
532 if (buf
== BUFFER_DEPTH
|| buf
== BUFFER_STENCIL
) {
533 clearVal
= clear_depth
;
536 clearVal
= (cpp
== 4)
537 ? intel
->ClearColor8888
: intel
->ClearColor565
;
540 _mesa_debug(ctx, "hardware blit clear buf %d rb id %d\n",
541 buf, irb->Base.Name);
543 intel_wait_flips(intel
);
548 BEGIN_BATCH(6, REFERENCES_CLIPRECTS
);
551 OUT_BATCH((b
.y1
<< 16) | b
.x1
);
552 OUT_BATCH((b
.y2
<< 16) | b
.x2
);
553 OUT_RELOC(write_buffer
,
554 DRM_GEM_DOMAIN_I915_RENDER
, DRM_GEM_DOMAIN_I915_RENDER
,
555 irb_region
->draw_offset
);
558 clearMask
&= ~bufBit
; /* turn off bit, for faster loop exit */
562 intel_batchbuffer_flush(intel
->batch
);
565 UNLOCK_HARDWARE(intel
);
569 intelEmitImmediateColorExpandBlit(struct intel_context
*intel
,
571 GLubyte
*src_bits
, GLuint src_size
,
577 GLshort x
, GLshort y
,
578 GLshort w
, GLshort h
,
581 int dwords
= ALIGN(src_size
, 8) / 4;
582 uint32_t opcode
, br13
, blit_cmd
;
584 assert( logic_op
- GL_CLEAR
>= 0 );
585 assert( logic_op
- GL_CLEAR
< 0x10 );
595 DBG("%s dst:buf(%p)/%d+%d %d,%d sz:%dx%d, %d bytes %d dwords\n",
597 dst_buffer
, dst_pitch
, dst_offset
, x
, y
, w
, h
, src_size
, dwords
);
599 intel_batchbuffer_require_space( intel
->batch
,
605 opcode
= XY_SETUP_BLT_CMD
;
607 opcode
|= XY_BLT_WRITE_ALPHA
| XY_BLT_WRITE_RGB
;
609 opcode
|= XY_DST_TILED
;
611 br13
= dst_pitch
| (translate_raster_op(logic_op
) << 16) | (1 << 29);
617 blit_cmd
= XY_TEXT_IMMEDIATE_BLIT_CMD
| XY_TEXT_BYTE_PACKED
; /* packing? */
619 blit_cmd
|= XY_DST_TILED
;
621 BEGIN_BATCH(8 + 3, NO_LOOP_CLIPRECTS
);
624 OUT_BATCH((0 << 16) | 0); /* clip x1, y1 */
625 OUT_BATCH((100 << 16) | 100); /* clip x2, y2 */
626 OUT_RELOC(dst_buffer
,
627 DRM_GEM_DOMAIN_I915_RENDER
, DRM_GEM_DOMAIN_I915_RENDER
,
629 OUT_BATCH(0); /* bg */
630 OUT_BATCH(fg_color
); /* fg */
631 OUT_BATCH(0); /* pattern base addr */
633 OUT_BATCH(blit_cmd
| ((3 - 2) + dwords
));
634 OUT_BATCH((y
<< 16) | x
);
635 OUT_BATCH(((y
+ h
) << 16) | (x
+ w
));
638 intel_batchbuffer_data( intel
->batch
,