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_mipmap_tree.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;
90 intelEmitCopyBlit(struct intel_context
*intel
,
93 drm_intel_bo
*src_buffer
,
97 drm_intel_bo
*dst_buffer
,
100 GLshort src_x
, GLshort src_y
,
101 GLshort dst_x
, GLshort dst_y
,
102 GLshort w
, GLshort h
,
105 GLuint CMD
, BR13
, pass
= 0;
106 int dst_y2
= dst_y
+ h
;
107 int dst_x2
= dst_x
+ w
;
108 drm_intel_bo
*aper_array
[3];
111 if (dst_tiling
!= I915_TILING_NONE
) {
112 if (dst_offset
& 4095)
114 if (dst_tiling
== I915_TILING_Y
)
117 if (src_tiling
!= I915_TILING_NONE
) {
118 if (src_offset
& 4095)
120 if (src_tiling
== I915_TILING_Y
)
124 /* do space check before going any further */
126 aper_array
[0] = intel
->batch
.bo
;
127 aper_array
[1] = dst_buffer
;
128 aper_array
[2] = src_buffer
;
130 if (dri_bufmgr_check_aperture_space(aper_array
, 3) != 0) {
131 intel_batchbuffer_flush(intel
);
140 intel_batchbuffer_require_space(intel
, 8 * 4, true);
141 DBG("%s src:buf(%p)/%d+%d %d,%d dst:buf(%p)/%d+%d %d,%d sz:%dx%d\n",
143 src_buffer
, src_pitch
, src_offset
, src_x
, src_y
,
144 dst_buffer
, dst_pitch
, dst_offset
, dst_x
, dst_y
, w
, h
);
149 /* Blit pitch must be dword-aligned. Otherwise, the hardware appears to drop
152 assert(src_pitch
% 4 == 0);
153 assert(dst_pitch
% 4 == 0);
155 /* For big formats (such as floating point), do the copy using 32bpp and
156 * multiply the coordinates.
159 assert(cpp
% 4 == 0);
166 BR13
= br13_for_cpp(cpp
) | translate_raster_op(logic_op
) << 16;
171 CMD
= XY_SRC_COPY_BLT_CMD
;
174 CMD
= XY_SRC_COPY_BLT_CMD
| XY_BLT_WRITE_ALPHA
| XY_BLT_WRITE_RGB
;
181 if (dst_tiling
!= I915_TILING_NONE
) {
185 if (src_tiling
!= I915_TILING_NONE
) {
191 if (dst_y2
<= dst_y
|| dst_x2
<= dst_x
) {
195 assert(dst_x
< dst_x2
);
196 assert(dst_y
< dst_y2
);
200 OUT_BATCH(BR13
| (uint16_t)dst_pitch
);
201 OUT_BATCH((dst_y
<< 16) | dst_x
);
202 OUT_BATCH((dst_y2
<< 16) | dst_x2
);
203 OUT_RELOC_FENCED(dst_buffer
,
204 I915_GEM_DOMAIN_RENDER
, I915_GEM_DOMAIN_RENDER
,
206 OUT_BATCH((src_y
<< 16) | src_x
);
207 OUT_BATCH((uint16_t)src_pitch
);
208 OUT_RELOC_FENCED(src_buffer
,
209 I915_GEM_DOMAIN_RENDER
, 0,
213 intel_batchbuffer_emit_mi_flush(intel
);
220 * Use blitting to clear the renderbuffers named by 'flags'.
221 * Note: we can't use the ctx->DrawBuffer->_ColorDrawBufferIndexes field
222 * since that might include software renderbuffers or renderbuffers
223 * which we're clearing with triangles.
224 * \param mask bitmask of BUFFER_BIT_* values indicating buffers to clear
227 intelClearWithBlit(struct gl_context
*ctx
, GLbitfield mask
)
229 struct intel_context
*intel
= intel_context(ctx
);
230 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
231 GLuint clear_depth_value
, clear_depth_mask
;
232 GLint cx
, cy
, cw
, ch
;
233 GLbitfield fail_mask
= 0;
237 * Compute values for clearing the buffers.
239 clear_depth_value
= 0;
240 clear_depth_mask
= 0;
241 if (mask
& BUFFER_BIT_DEPTH
) {
242 clear_depth_value
= (GLuint
) (fb
->_DepthMax
* ctx
->Depth
.Clear
);
243 clear_depth_mask
= XY_BLT_WRITE_RGB
;
245 if (mask
& BUFFER_BIT_STENCIL
) {
246 clear_depth_value
|= (ctx
->Stencil
.Clear
& 0xff) << 24;
247 clear_depth_mask
|= XY_BLT_WRITE_ALPHA
;
252 cy
= ctx
->DrawBuffer
->Height
- fb
->_Ymax
;
255 cw
= fb
->_Xmax
- fb
->_Xmin
;
256 ch
= fb
->_Ymax
- fb
->_Ymin
;
258 if (cw
== 0 || ch
== 0)
261 /* Loop over all renderbuffers */
262 mask
&= (1 << BUFFER_COUNT
) - 1;
264 GLuint buf
= _mesa_ffs(mask
) - 1;
265 bool is_depth_stencil
= buf
== BUFFER_DEPTH
|| buf
== BUFFER_STENCIL
;
266 struct intel_renderbuffer
*irb
;
271 drm_intel_bo
*aper_array
[2];
275 irb
= intel_get_renderbuffer(fb
, buf
);
276 if (irb
== NULL
|| irb
->region
== NULL
|| irb
->region
->bo
== NULL
) {
277 fail_mask
|= 1 << buf
;
281 /* OK, clear this renderbuffer */
282 x1
= cx
+ irb
->draw_x
;
283 y1
= cy
+ irb
->draw_y
;
284 x2
= cx
+ cw
+ irb
->draw_x
;
285 y2
= cy
+ ch
+ irb
->draw_y
;
287 pitch
= irb
->region
->pitch
;
288 cpp
= irb
->region
->cpp
;
290 DBG("%s dst:buf(%p)/%d %d,%d sz:%dx%d\n",
292 irb
->region
->bo
, (pitch
* cpp
),
293 x1
, y1
, x2
- x1
, y2
- y1
);
296 CMD
= XY_COLOR_BLT_CMD
;
298 /* Setup the blit command */
300 if (is_depth_stencil
) {
301 CMD
|= clear_depth_mask
;
304 CMD
|= XY_BLT_WRITE_ALPHA
| XY_BLT_WRITE_RGB
;
308 assert(irb
->region
->tiling
!= I915_TILING_Y
);
311 if (irb
->region
->tiling
!= I915_TILING_NONE
) {
316 BR13
|= (pitch
* cpp
);
318 if (is_depth_stencil
) {
319 clear_val
= clear_depth_value
;
322 GLfloat
*color
= ctx
->Color
.ClearColor
.f
;
324 _mesa_unclamped_float_rgba_to_ubyte(clear
, color
);
326 switch (irb
->Base
.Format
) {
327 case MESA_FORMAT_ARGB8888
:
328 case MESA_FORMAT_XRGB8888
:
329 clear_val
= PACK_COLOR_8888(clear
[3], clear
[0],
332 case MESA_FORMAT_RGB565
:
333 clear_val
= PACK_COLOR_565(clear
[0], clear
[1], clear
[2]);
335 case MESA_FORMAT_ARGB4444
:
336 clear_val
= PACK_COLOR_4444(clear
[3], clear
[0],
339 case MESA_FORMAT_ARGB1555
:
340 clear_val
= PACK_COLOR_1555(clear
[3], clear
[0],
344 clear_val
= PACK_COLOR_8888(clear
[3], clear
[3],
348 fail_mask
|= 1 << buf
;
353 BR13
|= br13_for_cpp(cpp
);
358 /* do space check before going any further */
359 aper_array
[0] = intel
->batch
.bo
;
360 aper_array
[1] = irb
->region
->bo
;
362 if (drm_intel_bufmgr_check_aperture_space(aper_array
,
363 ARRAY_SIZE(aper_array
)) != 0) {
364 intel_batchbuffer_flush(intel
);
370 OUT_BATCH((y1
<< 16) | x1
);
371 OUT_BATCH((y2
<< 16) | x2
);
372 OUT_RELOC_FENCED(irb
->region
->bo
,
373 I915_GEM_DOMAIN_RENDER
, I915_GEM_DOMAIN_RENDER
,
375 OUT_BATCH(clear_val
);
378 if (intel
->always_flush_cache
)
379 intel_batchbuffer_emit_mi_flush(intel
);
381 if (buf
== BUFFER_DEPTH
|| buf
== BUFFER_STENCIL
)
382 mask
&= ~(BUFFER_BIT_DEPTH
| BUFFER_BIT_STENCIL
);
389 intelEmitImmediateColorExpandBlit(struct intel_context
*intel
,
391 GLubyte
*src_bits
, GLuint src_size
,
394 drm_intel_bo
*dst_buffer
,
397 GLshort x
, GLshort y
,
398 GLshort w
, GLshort h
,
401 int dwords
= ALIGN(src_size
, 8) / 4;
402 uint32_t opcode
, br13
, blit_cmd
;
404 if (dst_tiling
!= I915_TILING_NONE
) {
405 if (dst_offset
& 4095)
407 if (dst_tiling
== I915_TILING_Y
)
411 assert( logic_op
- GL_CLEAR
>= 0 );
412 assert( logic_op
- GL_CLEAR
< 0x10 );
413 assert(dst_pitch
> 0);
420 DBG("%s dst:buf(%p)/%d+%d %d,%d sz:%dx%d, %d bytes %d dwords\n",
422 dst_buffer
, dst_pitch
, dst_offset
, x
, y
, w
, h
, src_size
, dwords
);
424 intel_batchbuffer_require_space(intel
,
429 opcode
= XY_SETUP_BLT_CMD
;
431 opcode
|= XY_BLT_WRITE_ALPHA
| XY_BLT_WRITE_RGB
;
433 if (dst_tiling
!= I915_TILING_NONE
) {
434 opcode
|= XY_DST_TILED
;
439 br13
= dst_pitch
| (translate_raster_op(logic_op
) << 16) | (1 << 29);
440 br13
|= br13_for_cpp(cpp
);
442 blit_cmd
= XY_TEXT_IMMEDIATE_BLIT_CMD
| XY_TEXT_BYTE_PACKED
; /* packing? */
443 if (dst_tiling
!= I915_TILING_NONE
)
444 blit_cmd
|= XY_DST_TILED
;
446 BEGIN_BATCH_BLT(8 + 3);
449 OUT_BATCH((0 << 16) | 0); /* clip x1, y1 */
450 OUT_BATCH((100 << 16) | 100); /* clip x2, y2 */
451 OUT_RELOC_FENCED(dst_buffer
,
452 I915_GEM_DOMAIN_RENDER
, I915_GEM_DOMAIN_RENDER
,
454 OUT_BATCH(0); /* bg */
455 OUT_BATCH(fg_color
); /* fg */
456 OUT_BATCH(0); /* pattern base addr */
458 OUT_BATCH(blit_cmd
| ((3 - 2) + dwords
));
459 OUT_BATCH((y
<< 16) | x
);
460 OUT_BATCH(((y
+ h
) << 16) | (x
+ w
));
463 intel_batchbuffer_data(intel
, src_bits
, dwords
* 4, true);
465 intel_batchbuffer_emit_mi_flush(intel
);
470 /* We don't have a memmove-type blit like some other hardware, so we'll do a
471 * rectangular blit covering a large space, then emit 1-scanline blit at the
472 * end to cover the last if we need.
475 intel_emit_linear_blit(struct intel_context
*intel
,
476 drm_intel_bo
*dst_bo
,
477 unsigned int dst_offset
,
478 drm_intel_bo
*src_bo
,
479 unsigned int src_offset
,
482 GLuint pitch
, height
;
485 /* The pitch given to the GPU must be DWORD aligned, and
486 * we want width to match pitch. Max width is (1 << 15 - 1),
487 * rounding that down to the nearest DWORD is 1 << 15 - 4
489 pitch
= MIN2(size
, (1 << 15) - 4);
490 height
= size
/ pitch
;
491 ok
= intelEmitCopyBlit(intel
, 1,
492 pitch
, src_bo
, src_offset
, I915_TILING_NONE
,
493 pitch
, dst_bo
, dst_offset
, I915_TILING_NONE
,
496 pitch
, height
, /* w, h */
500 src_offset
+= pitch
* height
;
501 dst_offset
+= pitch
* height
;
502 size
-= pitch
* height
;
503 assert (size
< (1 << 15));
504 assert ((size
& 3) == 0); /* Pitch must be DWORD aligned */
506 ok
= intelEmitCopyBlit(intel
, 1,
507 size
, src_bo
, src_offset
, I915_TILING_NONE
,
508 size
, dst_bo
, dst_offset
, I915_TILING_NONE
,
518 * Used to initialize the alpha value of an ARGB8888 teximage after
519 * loading it from an XRGB8888 source.
521 * This is very common with glCopyTexImage2D().
524 intel_set_teximage_alpha_to_one(struct gl_context
*ctx
,
525 struct intel_texture_image
*intel_image
)
527 struct intel_context
*intel
= intel_context(ctx
);
528 unsigned int image_x
, image_y
;
529 uint32_t x1
, y1
, x2
, y2
;
532 drm_intel_bo
*aper_array
[2];
533 struct intel_region
*region
= intel_image
->mt
->region
;
534 int width
, height
, depth
;
537 intel_miptree_get_dimensions_for_image(&intel_image
->base
.Base
,
538 &width
, &height
, &depth
);
541 assert(intel_image
->base
.Base
.TexFormat
== MESA_FORMAT_ARGB8888
);
543 /* get dest x/y in destination texture */
544 intel_miptree_get_image_offset(intel_image
->mt
,
545 intel_image
->base
.Base
.Level
,
546 intel_image
->base
.Base
.Face
,
552 x2
= image_x
+ width
;
553 y2
= image_y
+ height
;
555 pitch
= region
->pitch
;
558 DBG("%s dst:buf(%p)/%d %d,%d sz:%dx%d\n",
560 intel_image
->mt
->region
->bo
, (pitch
* cpp
),
561 x1
, y1
, x2
- x1
, y2
- y1
);
563 BR13
= br13_for_cpp(cpp
) | 0xf0 << 16;
564 CMD
= XY_COLOR_BLT_CMD
;
565 CMD
|= XY_BLT_WRITE_ALPHA
;
567 assert(region
->tiling
!= I915_TILING_Y
);
570 if (region
->tiling
!= I915_TILING_NONE
) {
575 BR13
|= (pitch
* cpp
);
577 /* do space check before going any further */
578 aper_array
[0] = intel
->batch
.bo
;
579 aper_array
[1] = region
->bo
;
581 if (drm_intel_bufmgr_check_aperture_space(aper_array
,
582 ARRAY_SIZE(aper_array
)) != 0) {
583 intel_batchbuffer_flush(intel
);
589 OUT_BATCH((y1
<< 16) | x1
);
590 OUT_BATCH((y2
<< 16) | x2
);
591 OUT_RELOC_FENCED(region
->bo
,
592 I915_GEM_DOMAIN_RENDER
, I915_GEM_DOMAIN_RENDER
,
594 OUT_BATCH(0xffffffff); /* white, but only alpha gets written */
597 intel_batchbuffer_emit_mi_flush(intel
);