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
45 static GLuint
translate_raster_op(GLenum logicop
)
48 case GL_CLEAR
: return 0x00;
49 case GL_AND
: return 0x88;
50 case GL_AND_REVERSE
: return 0x44;
51 case GL_COPY
: return 0xCC;
52 case GL_AND_INVERTED
: return 0x22;
53 case GL_NOOP
: return 0xAA;
54 case GL_XOR
: return 0x66;
55 case GL_OR
: return 0xEE;
56 case GL_NOR
: return 0x11;
57 case GL_EQUIV
: return 0x99;
58 case GL_INVERT
: return 0x55;
59 case GL_OR_REVERSE
: return 0xDD;
60 case GL_COPY_INVERTED
: return 0x33;
61 case GL_OR_INVERTED
: return 0xBB;
62 case GL_NAND
: return 0x77;
63 case GL_SET
: return 0xFF;
72 intelEmitCopyBlit(struct intel_context
*intel
,
82 GLshort src_x
, GLshort src_y
,
83 GLshort dst_x
, GLshort dst_y
,
87 GLuint CMD
, BR13
, pass
= 0;
88 int dst_y2
= dst_y
+ h
;
89 int dst_x2
= dst_x
+ w
;
90 dri_bo
*aper_array
[3];
93 if (dst_tiling
!= I915_TILING_NONE
) {
94 if (dst_offset
& 4095)
96 if (dst_tiling
== I915_TILING_Y
)
99 if (src_tiling
!= I915_TILING_NONE
) {
100 if (src_offset
& 4095)
102 if (src_tiling
== I915_TILING_Y
)
106 /* do space/cliprects check before going any further */
108 aper_array
[0] = intel
->batch
->buf
;
109 aper_array
[1] = dst_buffer
;
110 aper_array
[2] = src_buffer
;
112 if (dri_bufmgr_check_aperture_space(aper_array
, 3) != 0) {
113 intel_batchbuffer_flush(intel
->batch
);
120 dri_bo_map(dst_buffer
, GL_TRUE
);
121 dri_bo_map(src_buffer
, GL_FALSE
);
122 _mesa_copy_rect((GLubyte
*)dst_buffer
->virtual + dst_offset
,
127 (GLubyte
*)src_buffer
->virtual + src_offset
,
131 dri_bo_unmap(src_buffer
);
132 dri_bo_unmap(dst_buffer
);
137 intel_batchbuffer_require_space(intel
->batch
, 8 * 4);
138 DBG("%s src:buf(%p)/%d+%d %d,%d dst:buf(%p)/%d+%d %d,%d sz:%dx%d\n",
140 src_buffer
, src_pitch
, src_offset
, src_x
, src_y
,
141 dst_buffer
, dst_pitch
, dst_offset
, dst_x
, dst_y
, w
, h
);
146 BR13
= translate_raster_op(logic_op
) << 16;
150 CMD
= XY_SRC_COPY_BLT_CMD
;
154 CMD
= XY_SRC_COPY_BLT_CMD
;
158 CMD
= XY_SRC_COPY_BLT_CMD
| XY_BLT_WRITE_ALPHA
| XY_BLT_WRITE_RGB
;
165 if (dst_tiling
!= I915_TILING_NONE
) {
169 if (src_tiling
!= I915_TILING_NONE
) {
175 if (dst_y2
<= dst_y
|| dst_x2
<= dst_x
) {
179 assert(dst_x
< dst_x2
);
180 assert(dst_y
< dst_y2
);
184 OUT_BATCH(BR13
| (uint16_t)dst_pitch
);
185 OUT_BATCH((dst_y
<< 16) | dst_x
);
186 OUT_BATCH((dst_y2
<< 16) | dst_x2
);
187 OUT_RELOC(dst_buffer
,
188 I915_GEM_DOMAIN_RENDER
, I915_GEM_DOMAIN_RENDER
,
190 OUT_BATCH((src_y
<< 16) | src_x
);
191 OUT_BATCH((uint16_t)src_pitch
);
192 OUT_RELOC(src_buffer
,
193 I915_GEM_DOMAIN_RENDER
, 0,
197 intel_batchbuffer_emit_mi_flush(intel
->batch
);
204 * Use blitting to clear the renderbuffers named by 'flags'.
205 * Note: we can't use the ctx->DrawBuffer->_ColorDrawBufferIndexes field
206 * since that might include software renderbuffers or renderbuffers
207 * which we're clearing with triangles.
208 * \param mask bitmask of BUFFER_BIT_* values indicating buffers to clear
211 intelClearWithBlit(GLcontext
*ctx
, GLbitfield mask
)
213 struct intel_context
*intel
= intel_context(ctx
);
214 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
216 GLbitfield skipBuffers
= 0;
217 unsigned int num_cliprects
;
218 struct drm_clip_rect
*cliprects
;
223 * Compute values for clearing the buffers.
226 if (mask
& BUFFER_BIT_DEPTH
) {
227 clear_depth
= (GLuint
) (fb
->_DepthMax
* ctx
->Depth
.Clear
);
229 if (mask
& BUFFER_BIT_STENCIL
) {
230 clear_depth
|= (ctx
->Stencil
.Clear
& 0xff) << 24;
233 /* If clearing both depth and stencil, skip BUFFER_BIT_STENCIL in
236 if ((mask
& BUFFER_BIT_DEPTH
) && (mask
& BUFFER_BIT_STENCIL
)) {
237 skipBuffers
= BUFFER_BIT_STENCIL
;
240 intel_get_cliprects(intel
, &cliprects
, &num_cliprects
, &x_off
, &y_off
);
242 GLint cx
, cy
, cw
, ch
;
243 drm_clip_rect_t clear
;
246 /* Get clear bounds after locking */
253 /* clearing a window */
255 /* flip top to bottom */
256 clear
.x1
= cx
+ x_off
;
257 clear
.y1
= intel
->driDrawable
->y
+ intel
->driDrawable
->h
- cy
- ch
;
258 clear
.x2
= clear
.x1
+ cw
;
259 clear
.y2
= clear
.y1
+ ch
;
263 assert(num_cliprects
== 1);
264 assert(cliprects
== &intel
->fboRect
);
267 clear
.x2
= clear
.x1
+ cw
;
268 clear
.y2
= clear
.y1
+ ch
;
269 /* no change to mask */
272 for (i
= 0; i
< num_cliprects
; i
++) {
273 const drm_clip_rect_t
*box
= &cliprects
[i
];
276 GLuint clearMask
= mask
; /* use copy, since we modify it below */
277 GLboolean all
= (cw
== fb
->Width
&& ch
== fb
->Height
);
280 intel_intersect_cliprects(&b
, &clear
, box
);
286 if (b
.x1
>= b
.x2
|| b
.y1
>= b
.y2
)
290 _mesa_printf("clear %d,%d..%d,%d, mask %x\n",
291 b
.x1
, b
.y1
, b
.x2
, b
.y2
, mask
);
293 /* Loop over all renderbuffers */
294 for (buf
= 0; buf
< BUFFER_COUNT
&& clearMask
; buf
++) {
295 const GLbitfield bufBit
= 1 << buf
;
296 if ((clearMask
& bufBit
) && !(bufBit
& skipBuffers
)) {
297 /* OK, clear this renderbuffer */
298 struct intel_renderbuffer
*irb
= intel_get_renderbuffer(fb
, buf
);
299 dri_bo
*write_buffer
=
300 intel_region_buffer(intel
, irb
->region
,
301 all
? INTEL_WRITE_FULL
:
303 int x1
= b
.x1
+ irb
->region
->draw_x
;
304 int y1
= b
.y1
+ irb
->region
->draw_y
;
305 int x2
= b
.x2
+ irb
->region
->draw_x
;
306 int y2
= b
.y2
+ irb
->region
->draw_y
;
312 pitch
= irb
->region
->pitch
;
313 cpp
= irb
->region
->cpp
;
315 DBG("%s dst:buf(%p)/%d %d,%d sz:%dx%d\n",
317 irb
->region
->buffer
, (pitch
* cpp
),
318 x1
, y1
, x2
- x1
, y2
- y1
);
321 CMD
= XY_COLOR_BLT_CMD
;
323 /* Setup the blit command */
326 if (buf
== BUFFER_DEPTH
|| buf
== BUFFER_STENCIL
) {
327 if (clearMask
& BUFFER_BIT_DEPTH
)
328 CMD
|= XY_BLT_WRITE_RGB
;
329 if (clearMask
& BUFFER_BIT_STENCIL
)
330 CMD
|= XY_BLT_WRITE_ALPHA
;
334 CMD
|= XY_BLT_WRITE_ALPHA
| XY_BLT_WRITE_RGB
;
342 assert(irb
->region
->tiling
!= I915_TILING_Y
);
345 if (irb
->region
->tiling
!= I915_TILING_NONE
) {
350 BR13
|= (pitch
* cpp
);
352 if (buf
== BUFFER_DEPTH
|| buf
== BUFFER_STENCIL
) {
353 clearVal
= clear_depth
;
357 GLclampf
*color
= ctx
->Color
.ClearColor
;
359 CLAMPED_FLOAT_TO_UBYTE(clear
[0], color
[0]);
360 CLAMPED_FLOAT_TO_UBYTE(clear
[1], color
[1]);
361 CLAMPED_FLOAT_TO_UBYTE(clear
[2], color
[2]);
362 CLAMPED_FLOAT_TO_UBYTE(clear
[3], color
[3]);
364 switch (irb
->Base
.Format
) {
365 case MESA_FORMAT_ARGB8888
:
366 case MESA_FORMAT_XRGB8888
:
367 clearVal
= PACK_COLOR_8888(clear
[3], clear
[0],
370 case MESA_FORMAT_RGB565
:
371 clearVal
= PACK_COLOR_565(clear
[0], clear
[1], clear
[2]);
373 case MESA_FORMAT_ARGB4444
:
374 clearVal
= PACK_COLOR_4444(clear
[3], clear
[0],
377 case MESA_FORMAT_ARGB1555
:
378 clearVal
= PACK_COLOR_1555(clear
[3], clear
[0],
382 _mesa_problem(ctx
, "Unexpected renderbuffer format: %d\n",
389 _mesa_debug(ctx, "hardware blit clear buf %d rb id %d\n",
390 buf, irb->Base.Name);
399 OUT_BATCH((y1
<< 16) | x1
);
400 OUT_BATCH((y2
<< 16) | x2
);
401 OUT_RELOC(write_buffer
,
402 I915_GEM_DOMAIN_RENDER
, I915_GEM_DOMAIN_RENDER
,
406 clearMask
&= ~bufBit
; /* turn off bit, for faster loop exit */
414 intelEmitImmediateColorExpandBlit(struct intel_context
*intel
,
416 GLubyte
*src_bits
, GLuint src_size
,
422 GLshort x
, GLshort y
,
423 GLshort w
, GLshort h
,
426 int dwords
= ALIGN(src_size
, 8) / 4;
427 uint32_t opcode
, br13
, blit_cmd
;
429 if (dst_tiling
!= I915_TILING_NONE
) {
430 if (dst_offset
& 4095)
432 if (dst_tiling
== I915_TILING_Y
)
436 assert( logic_op
- GL_CLEAR
>= 0 );
437 assert( logic_op
- GL_CLEAR
< 0x10 );
438 assert(dst_pitch
> 0);
445 DBG("%s dst:buf(%p)/%d+%d %d,%d sz:%dx%d, %d bytes %d dwords\n",
447 dst_buffer
, dst_pitch
, dst_offset
, x
, y
, w
, h
, src_size
, dwords
);
449 intel_batchbuffer_require_space( intel
->batch
,
454 opcode
= XY_SETUP_BLT_CMD
;
456 opcode
|= XY_BLT_WRITE_ALPHA
| XY_BLT_WRITE_RGB
;
458 if (dst_tiling
!= I915_TILING_NONE
) {
459 opcode
|= XY_DST_TILED
;
464 br13
= dst_pitch
| (translate_raster_op(logic_op
) << 16) | (1 << 29);
470 blit_cmd
= XY_TEXT_IMMEDIATE_BLIT_CMD
| XY_TEXT_BYTE_PACKED
; /* packing? */
471 if (dst_tiling
!= I915_TILING_NONE
)
472 blit_cmd
|= XY_DST_TILED
;
477 OUT_BATCH((0 << 16) | 0); /* clip x1, y1 */
478 OUT_BATCH((100 << 16) | 100); /* clip x2, y2 */
479 OUT_RELOC(dst_buffer
,
480 I915_GEM_DOMAIN_RENDER
, I915_GEM_DOMAIN_RENDER
,
482 OUT_BATCH(0); /* bg */
483 OUT_BATCH(fg_color
); /* fg */
484 OUT_BATCH(0); /* pattern base addr */
486 OUT_BATCH(blit_cmd
| ((3 - 2) + dwords
));
487 OUT_BATCH((y
<< 16) | x
);
488 OUT_BATCH(((y
+ h
) << 16) | (x
+ w
));
491 intel_batchbuffer_data( intel
->batch
,
495 intel_batchbuffer_emit_mi_flush(intel
->batch
);
500 /* We don't have a memmove-type blit like some other hardware, so we'll do a
501 * rectangular blit covering a large space, then emit 1-scanline blit at the
502 * end to cover the last if we need.
505 intel_emit_linear_blit(struct intel_context
*intel
,
506 drm_intel_bo
*dst_bo
,
507 unsigned int dst_offset
,
508 drm_intel_bo
*src_bo
,
509 unsigned int src_offset
,
512 GLuint pitch
, height
;
514 /* The pitch is a signed value. */
515 pitch
= MIN2(size
, (1 << 15) - 1);
516 height
= size
/ pitch
;
517 intelEmitCopyBlit(intel
, 1,
518 pitch
, src_bo
, src_offset
, I915_TILING_NONE
,
519 pitch
, dst_bo
, dst_offset
, I915_TILING_NONE
,
522 pitch
, height
, /* w, h */
525 src_offset
+= pitch
* height
;
526 dst_offset
+= pitch
* height
;
527 size
-= pitch
* height
;
528 assert (size
< (1 << 15));
530 intelEmitCopyBlit(intel
, 1,
531 size
, src_bo
, src_offset
, I915_TILING_NONE
,
532 size
, dst_bo
, dst_offset
, I915_TILING_NONE
,