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
->buf
;
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
->batch
);
140 intel_batchbuffer_require_space(intel
->batch
, 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 BR13
= br13_for_cpp(cpp
) | translate_raster_op(logic_op
) << 16;
154 CMD
= XY_SRC_COPY_BLT_CMD
;
157 CMD
= XY_SRC_COPY_BLT_CMD
| XY_BLT_WRITE_ALPHA
| XY_BLT_WRITE_RGB
;
164 if (dst_tiling
!= I915_TILING_NONE
) {
168 if (src_tiling
!= I915_TILING_NONE
) {
174 if (dst_y2
<= dst_y
|| dst_x2
<= dst_x
) {
178 assert(dst_x
< dst_x2
);
179 assert(dst_y
< dst_y2
);
183 OUT_BATCH(BR13
| (uint16_t)dst_pitch
);
184 OUT_BATCH((dst_y
<< 16) | dst_x
);
185 OUT_BATCH((dst_y2
<< 16) | dst_x2
);
186 OUT_RELOC_FENCED(dst_buffer
,
187 I915_GEM_DOMAIN_RENDER
, I915_GEM_DOMAIN_RENDER
,
189 OUT_BATCH((src_y
<< 16) | src_x
);
190 OUT_BATCH((uint16_t)src_pitch
);
191 OUT_RELOC_FENCED(src_buffer
,
192 I915_GEM_DOMAIN_RENDER
, 0,
196 intel_batchbuffer_emit_mi_flush(intel
->batch
);
203 * Use blitting to clear the renderbuffers named by 'flags'.
204 * Note: we can't use the ctx->DrawBuffer->_ColorDrawBufferIndexes field
205 * since that might include software renderbuffers or renderbuffers
206 * which we're clearing with triangles.
207 * \param mask bitmask of BUFFER_BIT_* values indicating buffers to clear
210 intelClearWithBlit(struct gl_context
*ctx
, GLbitfield mask
)
212 struct intel_context
*intel
= intel_context(ctx
);
213 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
216 GLint cx
, cy
, cw
, ch
;
220 * Compute values for clearing the buffers.
223 if (mask
& BUFFER_BIT_DEPTH
) {
224 clear_depth
= (GLuint
) (fb
->_DepthMax
* ctx
->Depth
.Clear
);
226 if (mask
& BUFFER_BIT_STENCIL
) {
227 clear_depth
|= (ctx
->Stencil
.Clear
& 0xff) << 24;
232 cy
= ctx
->DrawBuffer
->Height
- fb
->_Ymax
;
235 cw
= fb
->_Xmax
- fb
->_Xmin
;
236 ch
= fb
->_Ymax
- fb
->_Ymin
;
238 if (cw
== 0 || ch
== 0)
242 all
= (cw
== fb
->Width
&& ch
== fb
->Height
);
244 /* Loop over all renderbuffers */
245 for (buf
= 0; buf
< BUFFER_COUNT
&& mask
; buf
++) {
246 const GLbitfield bufBit
= 1 << buf
;
247 struct intel_renderbuffer
*irb
;
248 drm_intel_bo
*write_buffer
;
253 drm_intel_bo
*aper_array
[2];
255 if (!(mask
& bufBit
))
258 /* OK, clear this renderbuffer */
259 irb
= intel_get_renderbuffer(fb
, buf
);
260 write_buffer
= intel_region_buffer(intel
, irb
->region
,
261 all
? INTEL_WRITE_FULL
:
263 x1
= cx
+ irb
->region
->draw_x
;
264 y1
= cy
+ irb
->region
->draw_y
;
265 x2
= cx
+ cw
+ irb
->region
->draw_x
;
266 y2
= cy
+ ch
+ irb
->region
->draw_y
;
268 pitch
= irb
->region
->pitch
;
269 cpp
= irb
->region
->cpp
;
271 DBG("%s dst:buf(%p)/%d %d,%d sz:%dx%d\n",
273 irb
->region
->buffer
, (pitch
* cpp
),
274 x1
, y1
, x2
- x1
, y2
- y1
);
276 BR13
= br13_for_cpp(cpp
) | 0xf0 << 16;
277 CMD
= XY_COLOR_BLT_CMD
;
279 /* Setup the blit command */
281 if (buf
== BUFFER_DEPTH
|| buf
== BUFFER_STENCIL
) {
282 if (mask
& BUFFER_BIT_DEPTH
)
283 CMD
|= XY_BLT_WRITE_RGB
;
284 if (mask
& BUFFER_BIT_STENCIL
)
285 CMD
|= XY_BLT_WRITE_ALPHA
;
288 CMD
|= XY_BLT_WRITE_ALPHA
| XY_BLT_WRITE_RGB
;
292 assert(irb
->region
->tiling
!= I915_TILING_Y
);
295 if (irb
->region
->tiling
!= I915_TILING_NONE
) {
300 BR13
|= (pitch
* cpp
);
302 if (buf
== BUFFER_DEPTH
|| buf
== BUFFER_STENCIL
) {
303 clear_val
= clear_depth
;
306 GLclampf
*color
= ctx
->Color
.ClearColor
;
308 CLAMPED_FLOAT_TO_UBYTE(clear
[0], color
[0]);
309 CLAMPED_FLOAT_TO_UBYTE(clear
[1], color
[1]);
310 CLAMPED_FLOAT_TO_UBYTE(clear
[2], color
[2]);
311 CLAMPED_FLOAT_TO_UBYTE(clear
[3], color
[3]);
313 switch (irb
->Base
.Format
) {
314 case MESA_FORMAT_ARGB8888
:
315 case MESA_FORMAT_XRGB8888
:
316 clear_val
= PACK_COLOR_8888(clear
[3], clear
[0],
319 case MESA_FORMAT_RGB565
:
320 clear_val
= PACK_COLOR_565(clear
[0], clear
[1], clear
[2]);
322 case MESA_FORMAT_ARGB4444
:
323 clear_val
= PACK_COLOR_4444(clear
[3], clear
[0],
326 case MESA_FORMAT_ARGB1555
:
327 clear_val
= PACK_COLOR_1555(clear
[3], clear
[0],
331 clear_val
= PACK_COLOR_8888(clear
[3], clear
[3],
335 _mesa_problem(ctx
, "Unexpected renderbuffer format: %d\n",
344 /* do space check before going any further */
345 aper_array
[0] = intel
->batch
->buf
;
346 aper_array
[1] = write_buffer
;
348 if (drm_intel_bufmgr_check_aperture_space(aper_array
,
349 ARRAY_SIZE(aper_array
)) != 0) {
350 intel_batchbuffer_flush(intel
->batch
);
356 OUT_BATCH((y1
<< 16) | x1
);
357 OUT_BATCH((y2
<< 16) | x2
);
358 OUT_RELOC_FENCED(write_buffer
,
359 I915_GEM_DOMAIN_RENDER
, I915_GEM_DOMAIN_RENDER
,
361 OUT_BATCH(clear_val
);
364 if (intel
->always_flush_cache
)
365 intel_batchbuffer_emit_mi_flush(intel
->batch
);
367 if (buf
== BUFFER_DEPTH
|| buf
== BUFFER_STENCIL
)
368 mask
&= ~(BUFFER_BIT_DEPTH
| BUFFER_BIT_STENCIL
);
370 mask
&= ~bufBit
; /* turn off bit, for faster loop exit */
375 intelEmitImmediateColorExpandBlit(struct intel_context
*intel
,
377 GLubyte
*src_bits
, GLuint src_size
,
380 drm_intel_bo
*dst_buffer
,
383 GLshort x
, GLshort y
,
384 GLshort w
, GLshort h
,
387 int dwords
= ALIGN(src_size
, 8) / 4;
388 uint32_t opcode
, br13
, blit_cmd
;
390 if (dst_tiling
!= I915_TILING_NONE
) {
391 if (dst_offset
& 4095)
393 if (dst_tiling
== I915_TILING_Y
)
397 assert( logic_op
- GL_CLEAR
>= 0 );
398 assert( logic_op
- GL_CLEAR
< 0x10 );
399 assert(dst_pitch
> 0);
406 DBG("%s dst:buf(%p)/%d+%d %d,%d sz:%dx%d, %d bytes %d dwords\n",
408 dst_buffer
, dst_pitch
, dst_offset
, x
, y
, w
, h
, src_size
, dwords
);
410 intel_batchbuffer_require_space( intel
->batch
,
415 opcode
= XY_SETUP_BLT_CMD
;
417 opcode
|= XY_BLT_WRITE_ALPHA
| XY_BLT_WRITE_RGB
;
419 if (dst_tiling
!= I915_TILING_NONE
) {
420 opcode
|= XY_DST_TILED
;
425 br13
= dst_pitch
| (translate_raster_op(logic_op
) << 16) | (1 << 29);
426 br13
|= br13_for_cpp(cpp
);
428 blit_cmd
= XY_TEXT_IMMEDIATE_BLIT_CMD
| XY_TEXT_BYTE_PACKED
; /* packing? */
429 if (dst_tiling
!= I915_TILING_NONE
)
430 blit_cmd
|= XY_DST_TILED
;
432 BEGIN_BATCH_BLT(8 + 3);
435 OUT_BATCH((0 << 16) | 0); /* clip x1, y1 */
436 OUT_BATCH((100 << 16) | 100); /* clip x2, y2 */
437 OUT_RELOC_FENCED(dst_buffer
,
438 I915_GEM_DOMAIN_RENDER
, I915_GEM_DOMAIN_RENDER
,
440 OUT_BATCH(0); /* bg */
441 OUT_BATCH(fg_color
); /* fg */
442 OUT_BATCH(0); /* pattern base addr */
444 OUT_BATCH(blit_cmd
| ((3 - 2) + dwords
));
445 OUT_BATCH((y
<< 16) | x
);
446 OUT_BATCH(((y
+ h
) << 16) | (x
+ w
));
449 intel_batchbuffer_data(intel
->batch
,
453 intel_batchbuffer_emit_mi_flush(intel
->batch
);
458 /* We don't have a memmove-type blit like some other hardware, so we'll do a
459 * rectangular blit covering a large space, then emit 1-scanline blit at the
460 * end to cover the last if we need.
463 intel_emit_linear_blit(struct intel_context
*intel
,
464 drm_intel_bo
*dst_bo
,
465 unsigned int dst_offset
,
466 drm_intel_bo
*src_bo
,
467 unsigned int src_offset
,
470 GLuint pitch
, height
;
473 /* The pitch given to the GPU must be DWORD aligned, and
474 * we want width to match pitch. Max width is (1 << 15 - 1),
475 * rounding that down to the nearest DWORD is 1 << 15 - 4
477 pitch
= MIN2(size
, (1 << 15) - 4);
478 height
= size
/ pitch
;
479 ok
= intelEmitCopyBlit(intel
, 1,
480 pitch
, src_bo
, src_offset
, I915_TILING_NONE
,
481 pitch
, dst_bo
, dst_offset
, I915_TILING_NONE
,
484 pitch
, height
, /* w, h */
488 src_offset
+= pitch
* height
;
489 dst_offset
+= pitch
* height
;
490 size
-= pitch
* height
;
491 assert (size
< (1 << 15));
492 assert ((size
& 3) == 0); /* Pitch must be DWORD aligned */
494 ok
= intelEmitCopyBlit(intel
, 1,
495 size
, src_bo
, src_offset
, I915_TILING_NONE
,
496 size
, dst_bo
, dst_offset
, I915_TILING_NONE
,
506 * Used to initialize the alpha value of an ARGB8888 teximage after
507 * loading it from an XRGB8888 source.
509 * This is very common with glCopyTexImage2D().
512 intel_set_teximage_alpha_to_one(struct gl_context
*ctx
,
513 struct intel_texture_image
*intel_image
)
515 struct intel_context
*intel
= intel_context(ctx
);
516 unsigned int image_x
, image_y
;
517 uint32_t x1
, y1
, x2
, y2
;
520 drm_intel_bo
*aper_array
[2];
521 struct intel_region
*region
= intel_image
->mt
->region
;
524 assert(intel_image
->base
.TexFormat
== MESA_FORMAT_ARGB8888
);
526 /* get dest x/y in destination texture */
527 intel_miptree_get_image_offset(intel_image
->mt
,
535 x2
= image_x
+ intel_image
->base
.Width
;
536 y2
= image_y
+ intel_image
->base
.Height
;
538 pitch
= region
->pitch
;
541 DBG("%s dst:buf(%p)/%d %d,%d sz:%dx%d\n",
543 intel_image
->mt
->region
->buffer
, (pitch
* region
->cpp
),
544 x1
, y1
, x2
- x1
, y2
- y1
);
546 BR13
= br13_for_cpp(region
->cpp
) | 0xf0 << 16;
547 CMD
= XY_COLOR_BLT_CMD
;
548 CMD
|= XY_BLT_WRITE_ALPHA
;
550 assert(region
->tiling
!= I915_TILING_Y
);
553 if (region
->tiling
!= I915_TILING_NONE
) {
558 BR13
|= (pitch
* region
->cpp
);
560 /* do space check before going any further */
561 aper_array
[0] = intel
->batch
->buf
;
562 aper_array
[1] = region
->buffer
;
564 if (drm_intel_bufmgr_check_aperture_space(aper_array
,
565 ARRAY_SIZE(aper_array
)) != 0) {
566 intel_batchbuffer_flush(intel
->batch
);
572 OUT_BATCH((y1
<< 16) | x1
);
573 OUT_BATCH((y2
<< 16) | x2
);
574 OUT_RELOC_FENCED(region
->buffer
,
575 I915_GEM_DOMAIN_RENDER
, I915_GEM_DOMAIN_RENDER
,
577 OUT_BATCH(0xffffffff); /* white, but only alpha gets written */
580 intel_batchbuffer_emit_mi_flush(intel
->batch
);