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 if (intel
->last_swap_fence
) {
70 dri_fence_wait(intel
->last_swap_fence
);
71 dri_fence_unreference(intel
->last_swap_fence
);
72 intel
->last_swap_fence
= NULL
;
74 intel
->last_swap_fence
= intel
->first_swap_fence
;
75 intel
->first_swap_fence
= NULL
;
77 /* The LOCK_HARDWARE is required for the cliprects. Buffer offsets
78 * should work regardless.
82 if (dPriv
&& dPriv
->numClipRects
) {
83 struct intel_framebuffer
*intel_fb
= dPriv
->driverPrivate
;
84 struct intel_region
*src
, *dst
;
85 int nbox
= dPriv
->numClipRects
;
86 drm_clip_rect_t
*pbox
= dPriv
->pClipRects
;
88 int src_pitch
, dst_pitch
;
89 unsigned short src_x
, src_y
;
93 src
= intel_get_rb_region(&intel_fb
->Base
, BUFFER_BACK_LEFT
);
94 dst
= intel_get_rb_region(&intel_fb
->Base
, BUFFER_FRONT_LEFT
);
96 src_pitch
= src
->pitch
* src
->cpp
;
97 dst_pitch
= dst
->pitch
* dst
->cpp
;
102 ASSERT(intel_fb
->Base
.Name
== 0); /* Not a user-created FBO */
105 ASSERT(src
->cpp
== dst
->cpp
);
108 BR13
= (0xCC << 16) | (1 << 24);
109 CMD
= XY_SRC_COPY_BLT_CMD
;
112 BR13
= (0xCC << 16) | (1 << 24) | (1 << 25);
113 CMD
= XY_SRC_COPY_BLT_CMD
| XY_BLT_WRITE_ALPHA
| XY_BLT_WRITE_RGB
;
128 ret
= dri_bufmgr_check_aperture_space(dst
->buffer
);
129 ret
|= dri_bufmgr_check_aperture_space(src
->buffer
);
132 intel_batchbuffer_flush(intel
->batch
);
136 for (i
= 0; i
< nbox
; i
++, pbox
++) {
137 drm_clip_rect_t box
= *pbox
;
140 if (!intel_intersect_cliprects(&box
, &box
, rect
))
144 if (box
.x1
>= box
.x2
||
148 assert(box
.x1
< box
.x2
);
149 assert(box
.y1
< box
.y2
);
150 src_x
= box
.x1
- dPriv
->x
+ dPriv
->backX
;
151 src_y
= box
.y1
- dPriv
->y
+ dPriv
->backY
;
153 BEGIN_BATCH(8, REFERENCES_CLIPRECTS
);
155 OUT_BATCH(BR13
| dst_pitch
);
156 OUT_BATCH((box
.y1
<< 16) | box
.x1
);
157 OUT_BATCH((box
.y2
<< 16) | box
.x2
);
159 OUT_RELOC(dst
->buffer
, DRM_BO_FLAG_MEM_TT
| DRM_BO_FLAG_WRITE
, 0);
160 OUT_BATCH((src_y
<< 16) | src_x
);
161 OUT_BATCH(src_pitch
);
162 OUT_RELOC(src
->buffer
, DRM_BO_FLAG_MEM_TT
| DRM_BO_FLAG_READ
, 0);
166 if (intel
->first_swap_fence
)
167 dri_fence_unreference(intel
->first_swap_fence
);
168 intel_batchbuffer_flush(intel
->batch
);
169 intel
->first_swap_fence
= intel
->batch
->last_fence
;
170 if (intel
->first_swap_fence
)
171 dri_fence_reference(intel
->first_swap_fence
);
174 UNLOCK_HARDWARE(intel
);
181 intelEmitFillBlit(struct intel_context
*intel
,
187 GLshort x
, GLshort y
,
188 GLshort w
, GLshort h
,
200 BR13
= (0xF0 << 16) | (1 << 24);
201 CMD
= XY_COLOR_BLT_CMD
;
204 BR13
= (0xF0 << 16) | (1 << 24) | (1 << 25);
205 CMD
= XY_COLOR_BLT_CMD
| XY_BLT_WRITE_ALPHA
| XY_BLT_WRITE_RGB
;
217 DBG("%s dst:buf(%p)/%d+%d %d,%d sz:%dx%d\n",
218 __FUNCTION__
, dst_buffer
, dst_pitch
, dst_offset
, x
, y
, w
, h
);
223 BEGIN_BATCH(6, NO_LOOP_CLIPRECTS
);
225 OUT_BATCH(BR13
| dst_pitch
);
226 OUT_BATCH((y
<< 16) | x
);
227 OUT_BATCH(((y
+ h
) << 16) | (x
+ w
));
228 OUT_RELOC(dst_buffer
, DRM_BO_FLAG_MEM_TT
| DRM_BO_FLAG_WRITE
, dst_offset
);
233 static GLuint
translate_raster_op(GLenum logicop
)
236 case GL_CLEAR
: return 0x00;
237 case GL_AND
: return 0x88;
238 case GL_AND_REVERSE
: return 0x44;
239 case GL_COPY
: return 0xCC;
240 case GL_AND_INVERTED
: return 0x22;
241 case GL_NOOP
: return 0xAA;
242 case GL_XOR
: return 0x66;
243 case GL_OR
: return 0xEE;
244 case GL_NOR
: return 0x11;
245 case GL_EQUIV
: return 0x99;
246 case GL_INVERT
: return 0x55;
247 case GL_OR_REVERSE
: return 0xDD;
248 case GL_COPY_INVERTED
: return 0x33;
249 case GL_OR_INVERTED
: return 0xBB;
250 case GL_NAND
: return 0x77;
251 case GL_SET
: return 0xFF;
260 intelEmitCopyBlit(struct intel_context
*intel
,
270 GLshort src_x
, GLshort src_y
,
271 GLshort dst_x
, GLshort dst_y
,
272 GLshort w
, GLshort h
,
276 int dst_y2
= dst_y
+ h
;
277 int dst_x2
= dst_x
+ w
;
282 ret
= dri_bufmgr_check_aperture_space(dst_buffer
);
283 ret
|= dri_bufmgr_check_aperture_space(src_buffer
);
285 intel_batchbuffer_flush(intel
->batch
);
289 DBG("%s src:buf(%p)/%d+%d %d,%d dst:buf(%p)/%d+%d %d,%d sz:%dx%d\n",
291 src_buffer
, src_pitch
, src_offset
, src_x
, src_y
,
292 dst_buffer
, dst_pitch
, dst_offset
, dst_x
, dst_y
, w
, h
);
297 BR13
= translate_raster_op(logic_op
) << 16;
304 CMD
= XY_SRC_COPY_BLT_CMD
;
307 BR13
|= (1 << 24) | (1 << 25);
308 CMD
= XY_SRC_COPY_BLT_CMD
| XY_BLT_WRITE_ALPHA
| XY_BLT_WRITE_RGB
;
325 if (dst_y2
<= dst_y
|| dst_x2
<= dst_x
) {
332 /* Initial y values don't seem to work with negative pitches. If
333 * we adjust the offsets manually (below), it seems to work fine.
335 * On the other hand, if we always adjust, the hardware doesn't
336 * know which blit directions to use, so overlapping copypixels get
339 if (dst_pitch
> 0 && src_pitch
> 0) {
340 assert(dst_x
< dst_x2
);
341 assert(dst_y
< dst_y2
);
343 BEGIN_BATCH(8, NO_LOOP_CLIPRECTS
);
345 OUT_BATCH(BR13
| dst_pitch
);
346 OUT_BATCH((dst_y
<< 16) | dst_x
);
347 OUT_BATCH((dst_y2
<< 16) | dst_x2
);
348 OUT_RELOC(dst_buffer
, DRM_BO_FLAG_MEM_TT
| DRM_BO_FLAG_WRITE
,
350 OUT_BATCH((src_y
<< 16) | src_x
);
351 OUT_BATCH(src_pitch
);
352 OUT_RELOC(src_buffer
, DRM_BO_FLAG_MEM_TT
| DRM_BO_FLAG_READ
,
357 assert(dst_x
< dst_x2
);
360 BEGIN_BATCH(8, NO_LOOP_CLIPRECTS
);
362 OUT_BATCH(BR13
| dst_pitch
);
363 OUT_BATCH((0 << 16) | dst_x
);
364 OUT_BATCH((h
<< 16) | dst_x2
);
365 OUT_RELOC(dst_buffer
, DRM_BO_FLAG_MEM_TT
| DRM_BO_FLAG_WRITE
,
366 dst_offset
+ dst_y
* dst_pitch
);
367 OUT_BATCH((0 << 16) | src_x
);
368 OUT_BATCH(src_pitch
);
369 OUT_RELOC(src_buffer
, DRM_BO_FLAG_MEM_TT
| DRM_BO_FLAG_READ
,
370 src_offset
+ src_y
* src_pitch
);
377 * Use blitting to clear the renderbuffers named by 'flags'.
378 * Note: we can't use the ctx->DrawBuffer->_ColorDrawBufferIndexes field
379 * since that might include software renderbuffers or renderbuffers
380 * which we're clearing with triangles.
381 * \param mask bitmask of BUFFER_BIT_* values indicating buffers to clear
384 intelClearWithBlit(GLcontext
*ctx
, GLbitfield mask
)
386 struct intel_context
*intel
= intel_context(ctx
);
387 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
389 GLbitfield skipBuffers
= 0;
393 * Compute values for clearing the buffers.
396 if (mask
& BUFFER_BIT_DEPTH
) {
397 clear_depth
= (GLuint
) (fb
->_DepthMax
* ctx
->Depth
.Clear
);
399 if (mask
& BUFFER_BIT_STENCIL
) {
400 clear_depth
|= (ctx
->Stencil
.Clear
& 0xff) << 24;
403 /* If clearing both depth and stencil, skip BUFFER_BIT_STENCIL in
406 if ((mask
& BUFFER_BIT_DEPTH
) && (mask
& BUFFER_BIT_STENCIL
)) {
407 skipBuffers
= BUFFER_BIT_STENCIL
;
410 /* XXX Move this flush/lock into the following conditional? */
411 intelFlush(&intel
->ctx
);
412 LOCK_HARDWARE(intel
);
414 if (intel
->numClipRects
) {
415 GLint cx
, cy
, cw
, ch
;
416 drm_clip_rect_t clear
;
419 /* Get clear bounds after locking */
426 /* clearing a window */
428 /* flip top to bottom */
429 clear
.x1
= cx
+ intel
->drawX
;
430 clear
.y1
= intel
->driDrawable
->y
+ intel
->driDrawable
->h
- cy
- ch
;
431 clear
.x2
= clear
.x1
+ cw
;
432 clear
.y2
= clear
.y1
+ ch
;
436 assert(intel
->numClipRects
== 1);
437 assert(intel
->pClipRects
== &intel
->fboRect
);
440 clear
.x2
= clear
.x1
+ cw
;
441 clear
.y2
= clear
.y1
+ ch
;
442 /* no change to mask */
445 for (i
= 0; i
< intel
->numClipRects
; i
++) {
446 const drm_clip_rect_t
*box
= &intel
->pClipRects
[i
];
449 GLuint clearMask
= mask
; /* use copy, since we modify it below */
450 GLboolean all
= (cw
== fb
->Width
&& ch
== fb
->Height
);
453 intel_intersect_cliprects(&b
, &clear
, box
);
459 if (b
.x1
>= b
.x2
|| b
.y1
>= b
.y2
)
463 _mesa_printf("clear %d,%d..%d,%d, mask %x\n",
464 b
.x1
, b
.y1
, b
.x2
, b
.y2
, mask
);
466 /* Loop over all renderbuffers */
467 for (buf
= 0; buf
< BUFFER_COUNT
&& clearMask
; buf
++) {
468 const GLbitfield bufBit
= 1 << buf
;
469 if ((clearMask
& bufBit
) && !(bufBit
& skipBuffers
)) {
470 /* OK, clear this renderbuffer */
471 struct intel_region
*irb_region
=
472 intel_get_rb_region(fb
, buf
);
473 dri_bo
*write_buffer
=
474 intel_region_buffer(intel
, irb_region
,
475 all
? INTEL_WRITE_FULL
:
484 pitch
= irb_region
->pitch
;
485 cpp
= irb_region
->cpp
;
487 DBG("%s dst:buf(%p)/%d+%d %d,%d sz:%dx%d\n",
489 irb_region
->buffer
, (pitch
* cpp
),
490 irb_region
->draw_offset
,
491 b
.x1
, b
.y1
, b
.x2
- b
.x1
, b
.y2
- b
.y1
);
494 CMD
= XY_COLOR_BLT_CMD
;
496 /* Setup the blit command */
498 BR13
|= (1 << 24) | (1 << 25);
499 if (buf
== BUFFER_DEPTH
|| buf
== BUFFER_STENCIL
) {
500 if (clearMask
& BUFFER_BIT_DEPTH
)
501 CMD
|= XY_BLT_WRITE_RGB
;
502 if (clearMask
& BUFFER_BIT_STENCIL
)
503 CMD
|= XY_BLT_WRITE_ALPHA
;
507 CMD
|= XY_BLT_WRITE_ALPHA
| XY_BLT_WRITE_RGB
;
511 ASSERT(cpp
== 2 || cpp
== 0);
516 if (irb_region
->tiled
) {
521 BR13
|= (pitch
* cpp
);
523 if (buf
== BUFFER_DEPTH
|| buf
== BUFFER_STENCIL
) {
524 clearVal
= clear_depth
;
527 clearVal
= (cpp
== 4)
528 ? intel
->ClearColor8888
: intel
->ClearColor565
;
531 _mesa_debug(ctx, "hardware blit clear buf %d rb id %d\n",
532 buf, irb->Base.Name);
534 intel_wait_flips(intel
);
539 BEGIN_BATCH(6, REFERENCES_CLIPRECTS
);
542 OUT_BATCH((b
.y1
<< 16) | b
.x1
);
543 OUT_BATCH((b
.y2
<< 16) | b
.x2
);
544 OUT_RELOC(write_buffer
, DRM_BO_FLAG_MEM_TT
| DRM_BO_FLAG_WRITE
,
545 irb_region
->draw_offset
);
548 clearMask
&= ~bufBit
; /* turn off bit, for faster loop exit */
552 intel_batchbuffer_flush(intel
->batch
);
555 UNLOCK_HARDWARE(intel
);
559 intelEmitImmediateColorExpandBlit(struct intel_context
*intel
,
561 GLubyte
*src_bits
, GLuint src_size
,
567 GLshort x
, GLshort y
,
568 GLshort w
, GLshort h
,
571 int dwords
= ALIGN(src_size
, 8) / 4;
572 uint32_t opcode
, br13
, blit_cmd
;
574 assert( logic_op
- GL_CLEAR
>= 0 );
575 assert( logic_op
- GL_CLEAR
< 0x10 );
585 DBG("%s dst:buf(%p)/%d+%d %d,%d sz:%dx%d, %d bytes %d dwords\n",
587 dst_buffer
, dst_pitch
, dst_offset
, x
, y
, w
, h
, src_size
, dwords
);
589 intel_batchbuffer_require_space( intel
->batch
,
595 opcode
= XY_SETUP_BLT_CMD
;
597 opcode
|= XY_BLT_WRITE_ALPHA
| XY_BLT_WRITE_RGB
;
599 opcode
|= XY_DST_TILED
;
601 br13
= dst_pitch
| (translate_raster_op(logic_op
) << 16) | (1 << 29);
607 blit_cmd
= XY_TEXT_IMMEDIATE_BLIT_CMD
| XY_TEXT_BYTE_PACKED
; /* packing? */
609 blit_cmd
|= XY_DST_TILED
;
611 BEGIN_BATCH(8 + 3, NO_LOOP_CLIPRECTS
);
614 OUT_BATCH((0 << 16) | 0); /* clip x1, y1 */
615 OUT_BATCH((100 << 16) | 100); /* clip x2, y2 */
616 OUT_RELOC(dst_buffer
, DRM_BO_FLAG_MEM_TT
| DRM_BO_FLAG_WRITE
, dst_offset
);
617 OUT_BATCH(0); /* bg */
618 OUT_BATCH(fg_color
); /* fg */
619 OUT_BATCH(0); /* pattern base addr */
621 OUT_BATCH(blit_cmd
| ((3 - 2) + dwords
));
622 OUT_BATCH((y
<< 16) | x
);
623 OUT_BATCH(((y
+ h
) << 16) | (x
+ w
));
626 intel_batchbuffer_data( intel
->batch
,