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/colormac.h"
34 #include "intel_blit.h"
35 #include "intel_buffers.h"
36 #include "intel_context.h"
37 #include "intel_fbo.h"
38 #include "intel_reg.h"
39 #include "intel_regions.h"
40 #include "intel_batchbuffer.h"
41 #include "intel_chipset.h"
43 #define FILE_DEBUG_FLAG DEBUG_BLIT
46 * Copy the back color buffer to the front color buffer.
47 * Used for SwapBuffers().
50 intelCopyBuffer(const __DRIdrawablePrivate
* dPriv
,
51 const drm_clip_rect_t
* rect
)
54 struct intel_context
*intel
;
56 DBG("%s\n", __FUNCTION__
);
60 intel
= intelScreenContext(dPriv
->driScreenPriv
->private);
64 /* The LOCK_HARDWARE is required for the cliprects. Buffer offsets
65 * should work regardless.
69 if (dPriv
&& dPriv
->numClipRects
) {
70 struct intel_framebuffer
*intel_fb
= dPriv
->driverPrivate
;
71 struct intel_region
*src
, *dst
;
72 int nbox
= dPriv
->numClipRects
;
73 drm_clip_rect_t
*pbox
= dPriv
->pClipRects
;
75 int src_pitch
, dst_pitch
;
76 unsigned short src_x
, src_y
;
79 dri_bo
*aper_array
[3];
81 src
= intel_get_rb_region(&intel_fb
->Base
, BUFFER_BACK_LEFT
);
82 dst
= intel_get_rb_region(&intel_fb
->Base
, BUFFER_FRONT_LEFT
);
84 src_pitch
= src
->pitch
* src
->cpp
;
85 dst_pitch
= dst
->pitch
* dst
->cpp
;
90 ASSERT(intel_fb
->Base
.Name
== 0); /* Not a user-created FBO */
93 ASSERT(src
->cpp
== dst
->cpp
);
96 BR13
= (0xCC << 16) | BR13_565
;
97 CMD
= XY_SRC_COPY_BLT_CMD
;
100 BR13
= (0xCC << 16) | BR13_8888
;
101 CMD
= XY_SRC_COPY_BLT_CMD
| XY_BLT_WRITE_ALPHA
| XY_BLT_WRITE_RGB
;
104 assert(src
->tiling
!= I915_TILING_Y
);
105 assert(dst
->tiling
!= I915_TILING_Y
);
107 if (src
->tiling
!= I915_TILING_NONE
) {
111 if (dst
->tiling
!= I915_TILING_NONE
) {
116 /* do space/cliprects check before going any further */
117 intel_batchbuffer_require_space(intel
->batch
, 8 * 4,
118 REFERENCES_CLIPRECTS
);
120 aper_array
[0] = intel
->batch
->buf
;
121 aper_array
[1] = dst
->buffer
;
122 aper_array
[2] = src
->buffer
;
124 if (dri_bufmgr_check_aperture_space(aper_array
, 3) != 0) {
125 intel_batchbuffer_flush(intel
->batch
);
129 for (i
= 0; i
< nbox
; i
++, pbox
++) {
130 drm_clip_rect_t box
= *pbox
;
133 if (!intel_intersect_cliprects(&box
, &box
, rect
))
137 if (box
.x1
>= box
.x2
||
141 assert(box
.x1
< box
.x2
);
142 assert(box
.y1
< box
.y2
);
143 src_x
= box
.x1
- dPriv
->x
+ dPriv
->backX
;
144 src_y
= box
.y1
- dPriv
->y
+ dPriv
->backY
;
146 BEGIN_BATCH(8, REFERENCES_CLIPRECTS
);
148 OUT_BATCH(BR13
| dst_pitch
);
149 OUT_BATCH((box
.y1
<< 16) | box
.x1
);
150 OUT_BATCH((box
.y2
<< 16) | box
.x2
);
152 OUT_RELOC(dst
->buffer
,
153 I915_GEM_DOMAIN_RENDER
, I915_GEM_DOMAIN_RENDER
,
155 OUT_BATCH((src_y
<< 16) | src_x
);
156 OUT_BATCH(src_pitch
);
157 OUT_RELOC(src
->buffer
,
158 I915_GEM_DOMAIN_RENDER
, 0,
163 /* Flush the rendering and the batch so that the results all land on the
164 * screen in a timely fashion.
166 intel_batchbuffer_emit_mi_flush(intel
->batch
);
167 intel_batchbuffer_flush(intel
->batch
);
170 UNLOCK_HARDWARE(intel
);
173 static GLuint
translate_raster_op(GLenum logicop
)
176 case GL_CLEAR
: return 0x00;
177 case GL_AND
: return 0x88;
178 case GL_AND_REVERSE
: return 0x44;
179 case GL_COPY
: return 0xCC;
180 case GL_AND_INVERTED
: return 0x22;
181 case GL_NOOP
: return 0xAA;
182 case GL_XOR
: return 0x66;
183 case GL_OR
: return 0xEE;
184 case GL_NOR
: return 0x11;
185 case GL_EQUIV
: return 0x99;
186 case GL_INVERT
: return 0x55;
187 case GL_OR_REVERSE
: return 0xDD;
188 case GL_COPY_INVERTED
: return 0x33;
189 case GL_OR_INVERTED
: return 0xBB;
190 case GL_NAND
: return 0x77;
191 case GL_SET
: return 0xFF;
200 intelEmitCopyBlit(struct intel_context
*intel
,
210 GLshort src_x
, GLshort src_y
,
211 GLshort dst_x
, GLshort dst_y
,
212 GLshort w
, GLshort h
,
215 GLuint CMD
, BR13
, pass
= 0;
216 int dst_y2
= dst_y
+ h
;
217 int dst_x2
= dst_x
+ w
;
218 dri_bo
*aper_array
[3];
221 if (dst_tiling
!= I915_TILING_NONE
) {
222 if (dst_offset
& 4095)
224 if (dst_tiling
== I915_TILING_Y
)
227 if (src_tiling
!= I915_TILING_NONE
) {
228 if (src_offset
& 4095)
230 if (src_tiling
== I915_TILING_Y
)
234 /* do space/cliprects check before going any further */
236 aper_array
[0] = intel
->batch
->buf
;
237 aper_array
[1] = dst_buffer
;
238 aper_array
[2] = src_buffer
;
240 if (dri_bufmgr_check_aperture_space(aper_array
, 3) != 0) {
241 intel_batchbuffer_flush(intel
->batch
);
248 LOCK_HARDWARE(intel
);
249 dri_bo_map(dst_buffer
, GL_TRUE
);
250 dri_bo_map(src_buffer
, GL_FALSE
);
251 _mesa_copy_rect((GLubyte
*)dst_buffer
->virtual + dst_offset
,
256 (GLubyte
*)src_buffer
->virtual + src_offset
,
260 dri_bo_unmap(src_buffer
);
261 dri_bo_unmap(dst_buffer
);
262 UNLOCK_HARDWARE(intel
);
267 intel_batchbuffer_require_space(intel
->batch
, 8 * 4, NO_LOOP_CLIPRECTS
);
268 DBG("%s src:buf(%p)/%d+%d %d,%d dst:buf(%p)/%d+%d %d,%d sz:%dx%d\n",
270 src_buffer
, src_pitch
, src_offset
, src_x
, src_y
,
271 dst_buffer
, dst_pitch
, dst_offset
, dst_x
, dst_y
, w
, h
);
276 BR13
= translate_raster_op(logic_op
) << 16;
280 CMD
= XY_SRC_COPY_BLT_CMD
;
284 CMD
= XY_SRC_COPY_BLT_CMD
;
288 CMD
= XY_SRC_COPY_BLT_CMD
| XY_BLT_WRITE_ALPHA
| XY_BLT_WRITE_RGB
;
295 if (dst_tiling
!= I915_TILING_NONE
) {
299 if (src_tiling
!= I915_TILING_NONE
) {
305 if (dst_y2
<= dst_y
|| dst_x2
<= dst_x
) {
309 assert(dst_x
< dst_x2
);
310 assert(dst_y
< dst_y2
);
312 BEGIN_BATCH(8, NO_LOOP_CLIPRECTS
);
314 OUT_BATCH(BR13
| (uint16_t)dst_pitch
);
315 OUT_BATCH((dst_y
<< 16) | dst_x
);
316 OUT_BATCH((dst_y2
<< 16) | dst_x2
);
317 OUT_RELOC(dst_buffer
,
318 I915_GEM_DOMAIN_RENDER
, I915_GEM_DOMAIN_RENDER
,
320 OUT_BATCH((src_y
<< 16) | src_x
);
321 OUT_BATCH((uint16_t)src_pitch
);
322 OUT_RELOC(src_buffer
,
323 I915_GEM_DOMAIN_RENDER
, 0,
327 intel_batchbuffer_emit_mi_flush(intel
->batch
);
334 * Use blitting to clear the renderbuffers named by 'flags'.
335 * Note: we can't use the ctx->DrawBuffer->_ColorDrawBufferIndexes field
336 * since that might include software renderbuffers or renderbuffers
337 * which we're clearing with triangles.
338 * \param mask bitmask of BUFFER_BIT_* values indicating buffers to clear
341 intelClearWithBlit(GLcontext
*ctx
, GLbitfield mask
)
343 struct intel_context
*intel
= intel_context(ctx
);
344 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
346 GLbitfield skipBuffers
= 0;
347 unsigned int num_cliprects
;
348 struct drm_clip_rect
*cliprects
;
353 * Compute values for clearing the buffers.
356 if (mask
& BUFFER_BIT_DEPTH
) {
357 clear_depth
= (GLuint
) (fb
->_DepthMax
* ctx
->Depth
.Clear
);
359 if (mask
& BUFFER_BIT_STENCIL
) {
360 clear_depth
|= (ctx
->Stencil
.Clear
& 0xff) << 24;
363 /* If clearing both depth and stencil, skip BUFFER_BIT_STENCIL in
366 if ((mask
& BUFFER_BIT_DEPTH
) && (mask
& BUFFER_BIT_STENCIL
)) {
367 skipBuffers
= BUFFER_BIT_STENCIL
;
370 LOCK_HARDWARE(intel
);
372 intel_get_cliprects(intel
, &cliprects
, &num_cliprects
, &x_off
, &y_off
);
374 GLint cx
, cy
, cw
, ch
;
375 drm_clip_rect_t clear
;
378 /* Get clear bounds after locking */
385 /* clearing a window */
387 /* flip top to bottom */
388 clear
.x1
= cx
+ x_off
;
389 clear
.y1
= intel
->driDrawable
->y
+ intel
->driDrawable
->h
- cy
- ch
;
390 clear
.x2
= clear
.x1
+ cw
;
391 clear
.y2
= clear
.y1
+ ch
;
395 assert(num_cliprects
== 1);
396 assert(cliprects
== &intel
->fboRect
);
399 clear
.x2
= clear
.x1
+ cw
;
400 clear
.y2
= clear
.y1
+ ch
;
401 /* no change to mask */
404 for (i
= 0; i
< num_cliprects
; i
++) {
405 const drm_clip_rect_t
*box
= &cliprects
[i
];
408 GLuint clearMask
= mask
; /* use copy, since we modify it below */
409 GLboolean all
= (cw
== fb
->Width
&& ch
== fb
->Height
);
412 intel_intersect_cliprects(&b
, &clear
, box
);
418 if (b
.x1
>= b
.x2
|| b
.y1
>= b
.y2
)
422 _mesa_printf("clear %d,%d..%d,%d, mask %x\n",
423 b
.x1
, b
.y1
, b
.x2
, b
.y2
, mask
);
425 /* Loop over all renderbuffers */
426 for (buf
= 0; buf
< BUFFER_COUNT
&& clearMask
; buf
++) {
427 const GLbitfield bufBit
= 1 << buf
;
428 if ((clearMask
& bufBit
) && !(bufBit
& skipBuffers
)) {
429 /* OK, clear this renderbuffer */
430 struct intel_renderbuffer
*irb
= intel_get_renderbuffer(fb
, buf
);
431 dri_bo
*write_buffer
=
432 intel_region_buffer(intel
, irb
->region
,
433 all
? INTEL_WRITE_FULL
:
435 int x1
= b
.x1
+ irb
->region
->draw_x
;
436 int y1
= b
.y1
+ irb
->region
->draw_y
;
437 int x2
= b
.x2
+ irb
->region
->draw_x
;
438 int y2
= b
.y2
+ irb
->region
->draw_y
;
444 pitch
= irb
->region
->pitch
;
445 cpp
= irb
->region
->cpp
;
447 DBG("%s dst:buf(%p)/%d %d,%d sz:%dx%d\n",
449 irb
->region
->buffer
, (pitch
* cpp
),
450 x1
, y1
, x2
- x1
, y2
- y1
);
453 CMD
= XY_COLOR_BLT_CMD
;
455 /* Setup the blit command */
458 if (buf
== BUFFER_DEPTH
|| buf
== BUFFER_STENCIL
) {
459 if (clearMask
& BUFFER_BIT_DEPTH
)
460 CMD
|= XY_BLT_WRITE_RGB
;
461 if (clearMask
& BUFFER_BIT_STENCIL
)
462 CMD
|= XY_BLT_WRITE_ALPHA
;
466 CMD
|= XY_BLT_WRITE_ALPHA
| XY_BLT_WRITE_RGB
;
474 assert(irb
->region
->tiling
!= I915_TILING_Y
);
477 if (irb
->region
->tiling
!= I915_TILING_NONE
) {
482 BR13
|= (pitch
* cpp
);
484 if (buf
== BUFFER_DEPTH
|| buf
== BUFFER_STENCIL
) {
485 clearVal
= clear_depth
;
489 GLclampf
*color
= ctx
->Color
.ClearColor
;
491 CLAMPED_FLOAT_TO_UBYTE(clear
[0], color
[0]);
492 CLAMPED_FLOAT_TO_UBYTE(clear
[1], color
[1]);
493 CLAMPED_FLOAT_TO_UBYTE(clear
[2], color
[2]);
494 CLAMPED_FLOAT_TO_UBYTE(clear
[3], color
[3]);
496 switch (irb
->Base
.Format
) {
497 case MESA_FORMAT_ARGB8888
:
498 case MESA_FORMAT_XRGB8888
:
499 clearVal
= PACK_COLOR_8888(clear
[3], clear
[0],
502 case MESA_FORMAT_RGB565
:
503 clearVal
= PACK_COLOR_565(clear
[0], clear
[1], clear
[2]);
505 case MESA_FORMAT_ARGB4444
:
506 clearVal
= PACK_COLOR_4444(clear
[3], clear
[0],
509 case MESA_FORMAT_ARGB1555
:
510 clearVal
= PACK_COLOR_1555(clear
[3], clear
[0],
514 _mesa_problem(ctx
, "Unexpected renderbuffer format: %d\n",
521 _mesa_debug(ctx, "hardware blit clear buf %d rb id %d\n",
522 buf, irb->Base.Name);
528 BEGIN_BATCH(6, REFERENCES_CLIPRECTS
);
531 OUT_BATCH((y1
<< 16) | x1
);
532 OUT_BATCH((y2
<< 16) | x2
);
533 OUT_RELOC(write_buffer
,
534 I915_GEM_DOMAIN_RENDER
, I915_GEM_DOMAIN_RENDER
,
538 clearMask
&= ~bufBit
; /* turn off bit, for faster loop exit */
544 UNLOCK_HARDWARE(intel
);
548 intelEmitImmediateColorExpandBlit(struct intel_context
*intel
,
550 GLubyte
*src_bits
, GLuint src_size
,
556 GLshort x
, GLshort y
,
557 GLshort w
, GLshort h
,
560 int dwords
= ALIGN(src_size
, 8) / 4;
561 uint32_t opcode
, br13
, blit_cmd
;
563 if (dst_tiling
!= I915_TILING_NONE
) {
564 if (dst_offset
& 4095)
566 if (dst_tiling
== I915_TILING_Y
)
570 assert( logic_op
- GL_CLEAR
>= 0 );
571 assert( logic_op
- GL_CLEAR
< 0x10 );
572 assert(dst_pitch
> 0);
579 DBG("%s dst:buf(%p)/%d+%d %d,%d sz:%dx%d, %d bytes %d dwords\n",
581 dst_buffer
, dst_pitch
, dst_offset
, x
, y
, w
, h
, src_size
, dwords
);
583 intel_batchbuffer_require_space( intel
->batch
,
587 REFERENCES_CLIPRECTS
);
589 opcode
= XY_SETUP_BLT_CMD
;
591 opcode
|= XY_BLT_WRITE_ALPHA
| XY_BLT_WRITE_RGB
;
593 if (dst_tiling
!= I915_TILING_NONE
) {
594 opcode
|= XY_DST_TILED
;
599 br13
= dst_pitch
| (translate_raster_op(logic_op
) << 16) | (1 << 29);
605 blit_cmd
= XY_TEXT_IMMEDIATE_BLIT_CMD
| XY_TEXT_BYTE_PACKED
; /* packing? */
606 if (dst_tiling
!= I915_TILING_NONE
)
607 blit_cmd
|= XY_DST_TILED
;
609 BEGIN_BATCH(8 + 3, REFERENCES_CLIPRECTS
);
612 OUT_BATCH((0 << 16) | 0); /* clip x1, y1 */
613 OUT_BATCH((100 << 16) | 100); /* clip x2, y2 */
614 OUT_RELOC(dst_buffer
,
615 I915_GEM_DOMAIN_RENDER
, I915_GEM_DOMAIN_RENDER
,
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
,
629 REFERENCES_CLIPRECTS
);
631 intel_batchbuffer_emit_mi_flush(intel
->batch
);
636 /* We don't have a memmove-type blit like some other hardware, so we'll do a
637 * rectangular blit covering a large space, then emit 1-scanline blit at the
638 * end to cover the last if we need.
641 intel_emit_linear_blit(struct intel_context
*intel
,
642 drm_intel_bo
*dst_bo
,
643 unsigned int dst_offset
,
644 drm_intel_bo
*src_bo
,
645 unsigned int src_offset
,
648 GLuint pitch
, height
;
650 /* The pitch is a signed value. */
651 pitch
= MIN2(size
, (1 << 15) - 1);
652 height
= size
/ pitch
;
653 intelEmitCopyBlit(intel
, 1,
654 pitch
, src_bo
, src_offset
, I915_TILING_NONE
,
655 pitch
, dst_bo
, dst_offset
, I915_TILING_NONE
,
658 pitch
, height
, /* w, h */
661 src_offset
+= pitch
* height
;
662 dst_offset
+= pitch
* height
;
663 size
-= pitch
* height
;
664 assert (size
< (1 << 15));
666 intelEmitCopyBlit(intel
, 1,
667 size
, src_bo
, src_offset
, I915_TILING_NONE
,
668 size
, dst_bo
, dst_offset
, I915_TILING_NONE
,