90796bce18764020bdd3072ab31137b803b32af0
[mesa.git] / src / mesa / drivers / dri / i915 / intel_blit.c
1 /**************************************************************************
2 *
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28
29 #include "main/mtypes.h"
30 #include "main/context.h"
31 #include "main/enums.h"
32 #include "main/colormac.h"
33 #include "main/fbobject.h"
34
35 #include "intel_blit.h"
36 #include "intel_buffers.h"
37 #include "intel_context.h"
38 #include "intel_fbo.h"
39 #include "intel_reg.h"
40 #include "intel_regions.h"
41 #include "intel_batchbuffer.h"
42 #include "intel_mipmap_tree.h"
43
44 #define FILE_DEBUG_FLAG DEBUG_BLIT
45
46 static void
47 intel_miptree_set_alpha_to_one(struct intel_context *intel,
48 struct intel_mipmap_tree *mt,
49 int x, int y, int width, int height);
50
51 static GLuint translate_raster_op(GLenum logicop)
52 {
53 switch(logicop) {
54 case GL_CLEAR: return 0x00;
55 case GL_AND: return 0x88;
56 case GL_AND_REVERSE: return 0x44;
57 case GL_COPY: return 0xCC;
58 case GL_AND_INVERTED: return 0x22;
59 case GL_NOOP: return 0xAA;
60 case GL_XOR: return 0x66;
61 case GL_OR: return 0xEE;
62 case GL_NOR: return 0x11;
63 case GL_EQUIV: return 0x99;
64 case GL_INVERT: return 0x55;
65 case GL_OR_REVERSE: return 0xDD;
66 case GL_COPY_INVERTED: return 0x33;
67 case GL_OR_INVERTED: return 0xBB;
68 case GL_NAND: return 0x77;
69 case GL_SET: return 0xFF;
70 default: return 0;
71 }
72 }
73
74 static uint32_t
75 br13_for_cpp(int cpp)
76 {
77 switch (cpp) {
78 case 4:
79 return BR13_8888;
80 break;
81 case 2:
82 return BR13_565;
83 break;
84 case 1:
85 return BR13_8;
86 break;
87 default:
88 assert(0);
89 return 0;
90 }
91 }
92
93 /**
94 * Emits the packet for switching the blitter from X to Y tiled or back.
95 *
96 * This has to be called in a single BEGIN_BATCH_BLT_TILED() /
97 * ADVANCE_BATCH_TILED(). This is because BCS_SWCTRL is saved and restored as
98 * part of the power context, not a render context, and if the batchbuffer was
99 * to get flushed between setting and blitting, or blitting and restoring, our
100 * tiling state would leak into other unsuspecting applications (like the X
101 * server).
102 */
103 static void
104 set_blitter_tiling(struct intel_context *intel,
105 bool dst_y_tiled, bool src_y_tiled)
106 {
107 assert(intel->gen >= 6);
108
109 /* Idle the blitter before we update how tiling is interpreted. */
110 OUT_BATCH(MI_FLUSH_DW);
111 OUT_BATCH(0);
112 OUT_BATCH(0);
113 OUT_BATCH(0);
114
115 OUT_BATCH(MI_LOAD_REGISTER_IMM | (3 - 2));
116 OUT_BATCH(BCS_SWCTRL);
117 OUT_BATCH((BCS_SWCTRL_DST_Y | BCS_SWCTRL_SRC_Y) << 16 |
118 (dst_y_tiled ? BCS_SWCTRL_DST_Y : 0) |
119 (src_y_tiled ? BCS_SWCTRL_SRC_Y : 0));
120 }
121
122 #define BEGIN_BATCH_BLT_TILED(n, dst_y_tiled, src_y_tiled) do { \
123 BEGIN_BATCH_BLT(n + ((dst_y_tiled || src_y_tiled) ? 14 : 0)); \
124 if (dst_y_tiled || src_y_tiled) \
125 set_blitter_tiling(intel, dst_y_tiled, src_y_tiled); \
126 } while (0)
127
128 #define ADVANCE_BATCH_TILED(dst_y_tiled, src_y_tiled) do { \
129 if (dst_y_tiled || src_y_tiled) \
130 set_blitter_tiling(intel, false, false); \
131 ADVANCE_BATCH(); \
132 } while (0)
133
134 /**
135 * Implements a rectangular block transfer (blit) of pixels between two
136 * miptrees.
137 *
138 * Our blitter can operate on 1, 2, or 4-byte-per-pixel data, with generous,
139 * but limited, pitches and sizes allowed.
140 *
141 * The src/dst coordinates are relative to the given level/slice of the
142 * miptree.
143 *
144 * If @src_flip or @dst_flip is set, then the rectangle within that miptree
145 * will be inverted (including scanline order) when copying. This is common
146 * in GL when copying between window system and user-created
147 * renderbuffers/textures.
148 */
149 bool
150 intel_miptree_blit(struct intel_context *intel,
151 struct intel_mipmap_tree *src_mt,
152 int src_level, int src_slice,
153 uint32_t src_x, uint32_t src_y, bool src_flip,
154 struct intel_mipmap_tree *dst_mt,
155 int dst_level, int dst_slice,
156 uint32_t dst_x, uint32_t dst_y, bool dst_flip,
157 uint32_t width, uint32_t height,
158 GLenum logicop)
159 {
160 /* No sRGB decode or encode is done by the hardware blitter, which is
161 * consistent with what we want in the callers (glCopyTexSubImage(),
162 * glBlitFramebuffer(), texture validation, etc.).
163 */
164 gl_format src_format = _mesa_get_srgb_format_linear(src_mt->format);
165 gl_format dst_format = _mesa_get_srgb_format_linear(dst_mt->format);
166
167 /* The blitter doesn't support doing any format conversions. We do also
168 * support blitting ARGB8888 to XRGB8888 (trivial, the values dropped into
169 * the X channel don't matter), and XRGB8888 to ARGB8888 by setting the A
170 * channel to 1.0 at the end.
171 */
172 if (src_format != dst_format &&
173 ((src_format != MESA_FORMAT_ARGB8888 &&
174 src_format != MESA_FORMAT_XRGB8888) ||
175 (dst_format != MESA_FORMAT_ARGB8888 &&
176 dst_format != MESA_FORMAT_XRGB8888))) {
177 perf_debug("%s: Can't use hardware blitter from %s to %s, "
178 "falling back.\n", __FUNCTION__,
179 _mesa_get_format_name(src_format),
180 _mesa_get_format_name(dst_format));
181 return false;
182 }
183
184 /* According to the Ivy Bridge PRM, Vol1 Part4, section 1.2.1.2 (Graphics
185 * Data Size Limitations):
186 *
187 * The BLT engine is capable of transferring very large quantities of
188 * graphics data. Any graphics data read from and written to the
189 * destination is permitted to represent a number of pixels that
190 * occupies up to 65,536 scan lines and up to 32,768 bytes per scan line
191 * at the destination. The maximum number of pixels that may be
192 * represented per scan line’s worth of graphics data depends on the
193 * color depth.
194 *
195 * Furthermore, intelEmitCopyBlit (which is called below) uses a signed
196 * 16-bit integer to represent buffer pitch, so it can only handle buffer
197 * pitches < 32k.
198 *
199 * As a result of these two limitations, we can only use the blitter to do
200 * this copy when the region's pitch is less than 32k.
201 */
202 if (src_mt->region->pitch > 32768 ||
203 dst_mt->region->pitch > 32768) {
204 perf_debug("Falling back due to >32k pitch\n");
205 return false;
206 }
207
208 /* The blitter has no idea about fast color clears, so we need to resolve
209 * the miptrees before we do anything.
210 */
211 intel_miptree_resolve_color(intel, src_mt);
212 intel_miptree_resolve_color(intel, dst_mt);
213
214 if (src_flip)
215 src_y = src_mt->level[src_level].height - src_y - height;
216
217 if (dst_flip)
218 dst_y = dst_mt->level[dst_level].height - dst_y - height;
219
220 int src_pitch = src_mt->region->pitch;
221 if (src_flip != dst_flip)
222 src_pitch = -src_pitch;
223
224 uint32_t src_image_x, src_image_y;
225 intel_miptree_get_image_offset(src_mt, src_level, src_slice,
226 &src_image_x, &src_image_y);
227 src_x += src_image_x;
228 src_y += src_image_y;
229
230 uint32_t dst_image_x, dst_image_y;
231 intel_miptree_get_image_offset(dst_mt, dst_level, dst_slice,
232 &dst_image_x, &dst_image_y);
233 dst_x += dst_image_x;
234 dst_y += dst_image_y;
235
236 if (!intelEmitCopyBlit(intel,
237 src_mt->cpp,
238 src_pitch,
239 src_mt->region->bo, src_mt->offset,
240 src_mt->region->tiling,
241 dst_mt->region->pitch,
242 dst_mt->region->bo, dst_mt->offset,
243 dst_mt->region->tiling,
244 src_x, src_y,
245 dst_x, dst_y,
246 width, height,
247 logicop)) {
248 return false;
249 }
250
251 if (src_mt->format == MESA_FORMAT_XRGB8888 &&
252 dst_mt->format == MESA_FORMAT_ARGB8888) {
253 intel_miptree_set_alpha_to_one(intel, dst_mt,
254 dst_x, dst_y,
255 width, height);
256 }
257
258 return true;
259 }
260
261 /* Copy BitBlt
262 */
263 bool
264 intelEmitCopyBlit(struct intel_context *intel,
265 GLuint cpp,
266 GLshort src_pitch,
267 drm_intel_bo *src_buffer,
268 GLuint src_offset,
269 uint32_t src_tiling,
270 GLshort dst_pitch,
271 drm_intel_bo *dst_buffer,
272 GLuint dst_offset,
273 uint32_t dst_tiling,
274 GLshort src_x, GLshort src_y,
275 GLshort dst_x, GLshort dst_y,
276 GLshort w, GLshort h,
277 GLenum logic_op)
278 {
279 GLuint CMD, BR13, pass = 0;
280 int dst_y2 = dst_y + h;
281 int dst_x2 = dst_x + w;
282 drm_intel_bo *aper_array[3];
283 bool dst_y_tiled = dst_tiling == I915_TILING_Y;
284 bool src_y_tiled = src_tiling == I915_TILING_Y;
285 BATCH_LOCALS;
286
287 if (dst_tiling != I915_TILING_NONE) {
288 if (dst_offset & 4095)
289 return false;
290 }
291 if (src_tiling != I915_TILING_NONE) {
292 if (src_offset & 4095)
293 return false;
294 }
295 if ((dst_y_tiled || src_y_tiled) && intel->gen < 6)
296 return false;
297
298 /* do space check before going any further */
299 do {
300 aper_array[0] = intel->batch.bo;
301 aper_array[1] = dst_buffer;
302 aper_array[2] = src_buffer;
303
304 if (dri_bufmgr_check_aperture_space(aper_array, 3) != 0) {
305 intel_batchbuffer_flush(intel);
306 pass++;
307 } else
308 break;
309 } while (pass < 2);
310
311 if (pass >= 2)
312 return false;
313
314 intel_batchbuffer_require_space(intel, 8 * 4, true);
315 DBG("%s src:buf(%p)/%d+%d %d,%d dst:buf(%p)/%d+%d %d,%d sz:%dx%d\n",
316 __FUNCTION__,
317 src_buffer, src_pitch, src_offset, src_x, src_y,
318 dst_buffer, dst_pitch, dst_offset, dst_x, dst_y, w, h);
319
320 /* Blit pitch must be dword-aligned. Otherwise, the hardware appears to drop
321 * the low bits.
322 */
323 if (src_pitch % 4 != 0 || dst_pitch % 4 != 0)
324 return false;
325
326 /* For big formats (such as floating point), do the copy using 16 or 32bpp
327 * and multiply the coordinates.
328 */
329 if (cpp > 4) {
330 if (cpp % 4 == 2) {
331 dst_x *= cpp / 2;
332 dst_x2 *= cpp / 2;
333 src_x *= cpp / 2;
334 cpp = 2;
335 } else {
336 assert(cpp % 4 == 0);
337 dst_x *= cpp / 4;
338 dst_x2 *= cpp / 4;
339 src_x *= cpp / 4;
340 cpp = 4;
341 }
342 }
343
344 BR13 = br13_for_cpp(cpp) | translate_raster_op(logic_op) << 16;
345
346 switch (cpp) {
347 case 1:
348 case 2:
349 CMD = XY_SRC_COPY_BLT_CMD;
350 break;
351 case 4:
352 CMD = XY_SRC_COPY_BLT_CMD | XY_BLT_WRITE_ALPHA | XY_BLT_WRITE_RGB;
353 break;
354 default:
355 return false;
356 }
357
358 #ifndef I915
359 if (dst_tiling != I915_TILING_NONE) {
360 CMD |= XY_DST_TILED;
361 dst_pitch /= 4;
362 }
363 if (src_tiling != I915_TILING_NONE) {
364 CMD |= XY_SRC_TILED;
365 src_pitch /= 4;
366 }
367 #endif
368
369 if (dst_y2 <= dst_y || dst_x2 <= dst_x) {
370 return true;
371 }
372
373 assert(dst_x < dst_x2);
374 assert(dst_y < dst_y2);
375
376 BEGIN_BATCH_BLT_TILED(8, dst_y_tiled, src_y_tiled);
377
378 OUT_BATCH(CMD | (8 - 2));
379 OUT_BATCH(BR13 | (uint16_t)dst_pitch);
380 OUT_BATCH((dst_y << 16) | dst_x);
381 OUT_BATCH((dst_y2 << 16) | dst_x2);
382 OUT_RELOC_FENCED(dst_buffer,
383 I915_GEM_DOMAIN_RENDER, I915_GEM_DOMAIN_RENDER,
384 dst_offset);
385 OUT_BATCH((src_y << 16) | src_x);
386 OUT_BATCH((uint16_t)src_pitch);
387 OUT_RELOC_FENCED(src_buffer,
388 I915_GEM_DOMAIN_RENDER, 0,
389 src_offset);
390
391 ADVANCE_BATCH_TILED(dst_y_tiled, src_y_tiled);
392
393 intel_batchbuffer_emit_mi_flush(intel);
394
395 return true;
396 }
397
398
399 /**
400 * Use blitting to clear the renderbuffers named by 'flags'.
401 * Note: we can't use the ctx->DrawBuffer->_ColorDrawBufferIndexes field
402 * since that might include software renderbuffers or renderbuffers
403 * which we're clearing with triangles.
404 * \param mask bitmask of BUFFER_BIT_* values indicating buffers to clear
405 */
406 GLbitfield
407 intelClearWithBlit(struct gl_context *ctx, GLbitfield mask)
408 {
409 struct intel_context *intel = intel_context(ctx);
410 struct gl_framebuffer *fb = ctx->DrawBuffer;
411 GLuint clear_depth_value, clear_depth_mask;
412 GLint cx, cy, cw, ch;
413 GLbitfield fail_mask = 0;
414 BATCH_LOCALS;
415
416 /* Note: we don't use this function on Gen7+ hardware, so we can safely
417 * ignore fast color clear issues.
418 */
419 assert(intel->gen < 7);
420
421 /*
422 * Compute values for clearing the buffers.
423 */
424 clear_depth_value = 0;
425 clear_depth_mask = 0;
426 if (mask & BUFFER_BIT_DEPTH) {
427 clear_depth_value = (GLuint) (fb->_DepthMax * ctx->Depth.Clear);
428 clear_depth_mask = XY_BLT_WRITE_RGB;
429 }
430 if (mask & BUFFER_BIT_STENCIL) {
431 clear_depth_value |= (ctx->Stencil.Clear & 0xff) << 24;
432 clear_depth_mask |= XY_BLT_WRITE_ALPHA;
433 }
434
435 cx = fb->_Xmin;
436 if (_mesa_is_winsys_fbo(fb))
437 cy = ctx->DrawBuffer->Height - fb->_Ymax;
438 else
439 cy = fb->_Ymin;
440 cw = fb->_Xmax - fb->_Xmin;
441 ch = fb->_Ymax - fb->_Ymin;
442
443 if (cw == 0 || ch == 0)
444 return 0;
445
446 /* Loop over all renderbuffers */
447 mask &= (1 << BUFFER_COUNT) - 1;
448 while (mask) {
449 GLuint buf = ffs(mask) - 1;
450 bool is_depth_stencil = buf == BUFFER_DEPTH || buf == BUFFER_STENCIL;
451 struct intel_renderbuffer *irb;
452 int x1, y1, x2, y2;
453 uint32_t clear_val;
454 uint32_t BR13, CMD;
455 struct intel_region *region;
456 int pitch, cpp;
457 drm_intel_bo *aper_array[2];
458
459 mask &= ~(1 << buf);
460
461 irb = intel_get_renderbuffer(fb, buf);
462 if (irb && irb->mt) {
463 region = irb->mt->region;
464 assert(region);
465 assert(region->bo);
466 } else {
467 fail_mask |= 1 << buf;
468 continue;
469 }
470
471 /* OK, clear this renderbuffer */
472 x1 = cx + irb->draw_x;
473 y1 = cy + irb->draw_y;
474 x2 = cx + cw + irb->draw_x;
475 y2 = cy + ch + irb->draw_y;
476
477 pitch = region->pitch;
478 cpp = region->cpp;
479
480 DBG("%s dst:buf(%p)/%d %d,%d sz:%dx%d\n",
481 __FUNCTION__,
482 region->bo, pitch,
483 x1, y1, x2 - x1, y2 - y1);
484
485 BR13 = 0xf0 << 16;
486 CMD = XY_COLOR_BLT_CMD;
487
488 /* Setup the blit command */
489 if (cpp == 4) {
490 if (is_depth_stencil) {
491 CMD |= clear_depth_mask;
492 } else {
493 /* clearing RGBA */
494 CMD |= XY_BLT_WRITE_ALPHA | XY_BLT_WRITE_RGB;
495 }
496 }
497
498 assert(region->tiling != I915_TILING_Y);
499
500 #ifndef I915
501 if (region->tiling != I915_TILING_NONE) {
502 CMD |= XY_DST_TILED;
503 pitch /= 4;
504 }
505 #endif
506 BR13 |= pitch;
507
508 if (is_depth_stencil) {
509 clear_val = clear_depth_value;
510 } else {
511 uint8_t clear[4];
512 GLfloat *color = ctx->Color.ClearColor.f;
513
514 _mesa_unclamped_float_rgba_to_ubyte(clear, color);
515
516 switch (intel_rb_format(irb)) {
517 case MESA_FORMAT_ARGB8888:
518 case MESA_FORMAT_XRGB8888:
519 clear_val = PACK_COLOR_8888(clear[3], clear[0],
520 clear[1], clear[2]);
521 break;
522 case MESA_FORMAT_RGB565:
523 clear_val = PACK_COLOR_565(clear[0], clear[1], clear[2]);
524 break;
525 case MESA_FORMAT_ARGB4444:
526 clear_val = PACK_COLOR_4444(clear[3], clear[0],
527 clear[1], clear[2]);
528 break;
529 case MESA_FORMAT_ARGB1555:
530 clear_val = PACK_COLOR_1555(clear[3], clear[0],
531 clear[1], clear[2]);
532 break;
533 case MESA_FORMAT_A8:
534 clear_val = PACK_COLOR_8888(clear[3], clear[3],
535 clear[3], clear[3]);
536 break;
537 default:
538 fail_mask |= 1 << buf;
539 continue;
540 }
541 }
542
543 BR13 |= br13_for_cpp(cpp);
544
545 assert(x1 < x2);
546 assert(y1 < y2);
547
548 /* do space check before going any further */
549 aper_array[0] = intel->batch.bo;
550 aper_array[1] = region->bo;
551
552 if (drm_intel_bufmgr_check_aperture_space(aper_array,
553 ARRAY_SIZE(aper_array)) != 0) {
554 intel_batchbuffer_flush(intel);
555 }
556
557 BEGIN_BATCH_BLT(6);
558 OUT_BATCH(CMD | (6 - 2));
559 OUT_BATCH(BR13);
560 OUT_BATCH((y1 << 16) | x1);
561 OUT_BATCH((y2 << 16) | x2);
562 OUT_RELOC_FENCED(region->bo,
563 I915_GEM_DOMAIN_RENDER, I915_GEM_DOMAIN_RENDER,
564 0);
565 OUT_BATCH(clear_val);
566 ADVANCE_BATCH();
567
568 if (intel->always_flush_cache)
569 intel_batchbuffer_emit_mi_flush(intel);
570
571 if (buf == BUFFER_DEPTH || buf == BUFFER_STENCIL)
572 mask &= ~(BUFFER_BIT_DEPTH | BUFFER_BIT_STENCIL);
573 }
574
575 return fail_mask;
576 }
577
578 bool
579 intelEmitImmediateColorExpandBlit(struct intel_context *intel,
580 GLuint cpp,
581 GLubyte *src_bits, GLuint src_size,
582 GLuint fg_color,
583 GLshort dst_pitch,
584 drm_intel_bo *dst_buffer,
585 GLuint dst_offset,
586 uint32_t dst_tiling,
587 GLshort x, GLshort y,
588 GLshort w, GLshort h,
589 GLenum logic_op)
590 {
591 int dwords = ALIGN(src_size, 8) / 4;
592 uint32_t opcode, br13, blit_cmd;
593
594 if (dst_tiling != I915_TILING_NONE) {
595 if (dst_offset & 4095)
596 return false;
597 if (dst_tiling == I915_TILING_Y)
598 return false;
599 }
600
601 assert( logic_op - GL_CLEAR >= 0 );
602 assert( logic_op - GL_CLEAR < 0x10 );
603 assert(dst_pitch > 0);
604
605 if (w < 0 || h < 0)
606 return true;
607
608 DBG("%s dst:buf(%p)/%d+%d %d,%d sz:%dx%d, %d bytes %d dwords\n",
609 __FUNCTION__,
610 dst_buffer, dst_pitch, dst_offset, x, y, w, h, src_size, dwords);
611
612 intel_batchbuffer_require_space(intel,
613 (8 * 4) +
614 (3 * 4) +
615 dwords * 4, true);
616
617 opcode = XY_SETUP_BLT_CMD;
618 if (cpp == 4)
619 opcode |= XY_BLT_WRITE_ALPHA | XY_BLT_WRITE_RGB;
620 #ifndef I915
621 if (dst_tiling != I915_TILING_NONE) {
622 opcode |= XY_DST_TILED;
623 dst_pitch /= 4;
624 }
625 #endif
626
627 br13 = dst_pitch | (translate_raster_op(logic_op) << 16) | (1 << 29);
628 br13 |= br13_for_cpp(cpp);
629
630 blit_cmd = XY_TEXT_IMMEDIATE_BLIT_CMD | XY_TEXT_BYTE_PACKED; /* packing? */
631 if (dst_tiling != I915_TILING_NONE)
632 blit_cmd |= XY_DST_TILED;
633
634 BEGIN_BATCH_BLT(8 + 3);
635 OUT_BATCH(opcode | (8 - 2));
636 OUT_BATCH(br13);
637 OUT_BATCH((0 << 16) | 0); /* clip x1, y1 */
638 OUT_BATCH((100 << 16) | 100); /* clip x2, y2 */
639 OUT_RELOC_FENCED(dst_buffer,
640 I915_GEM_DOMAIN_RENDER, I915_GEM_DOMAIN_RENDER,
641 dst_offset);
642 OUT_BATCH(0); /* bg */
643 OUT_BATCH(fg_color); /* fg */
644 OUT_BATCH(0); /* pattern base addr */
645
646 OUT_BATCH(blit_cmd | ((3 - 2) + dwords));
647 OUT_BATCH((y << 16) | x);
648 OUT_BATCH(((y + h) << 16) | (x + w));
649 ADVANCE_BATCH();
650
651 intel_batchbuffer_data(intel, src_bits, dwords * 4, true);
652
653 intel_batchbuffer_emit_mi_flush(intel);
654
655 return true;
656 }
657
658 /* We don't have a memmove-type blit like some other hardware, so we'll do a
659 * rectangular blit covering a large space, then emit 1-scanline blit at the
660 * end to cover the last if we need.
661 */
662 void
663 intel_emit_linear_blit(struct intel_context *intel,
664 drm_intel_bo *dst_bo,
665 unsigned int dst_offset,
666 drm_intel_bo *src_bo,
667 unsigned int src_offset,
668 unsigned int size)
669 {
670 struct gl_context *ctx = &intel->ctx;
671 GLuint pitch, height;
672 bool ok;
673
674 /* The pitch given to the GPU must be DWORD aligned, and
675 * we want width to match pitch. Max width is (1 << 15 - 1),
676 * rounding that down to the nearest DWORD is 1 << 15 - 4
677 */
678 pitch = ROUND_DOWN_TO(MIN2(size, (1 << 15) - 1), 4);
679 height = (pitch == 0) ? 1 : size / pitch;
680 ok = intelEmitCopyBlit(intel, 1,
681 pitch, src_bo, src_offset, I915_TILING_NONE,
682 pitch, dst_bo, dst_offset, I915_TILING_NONE,
683 0, 0, /* src x/y */
684 0, 0, /* dst x/y */
685 pitch, height, /* w, h */
686 GL_COPY);
687 if (!ok)
688 _mesa_problem(ctx, "Failed to linear blit %dx%d\n", pitch, height);
689
690 src_offset += pitch * height;
691 dst_offset += pitch * height;
692 size -= pitch * height;
693 assert (size < (1 << 15));
694 pitch = ALIGN(size, 4);
695 if (size != 0) {
696 ok = intelEmitCopyBlit(intel, 1,
697 pitch, src_bo, src_offset, I915_TILING_NONE,
698 pitch, dst_bo, dst_offset, I915_TILING_NONE,
699 0, 0, /* src x/y */
700 0, 0, /* dst x/y */
701 size, 1, /* w, h */
702 GL_COPY);
703 if (!ok)
704 _mesa_problem(ctx, "Failed to linear blit %dx%d\n", size, 1);
705 }
706 }
707
708 /**
709 * Used to initialize the alpha value of an ARGB8888 miptree after copying
710 * into it from an XRGB8888 source.
711 *
712 * This is very common with glCopyTexImage2D(). Note that the coordinates are
713 * relative to the start of the miptree, not relative to a slice within the
714 * miptree.
715 */
716 static void
717 intel_miptree_set_alpha_to_one(struct intel_context *intel,
718 struct intel_mipmap_tree *mt,
719 int x, int y, int width, int height)
720 {
721 struct intel_region *region = mt->region;
722 uint32_t BR13, CMD;
723 int pitch, cpp;
724 drm_intel_bo *aper_array[2];
725 BATCH_LOCALS;
726
727 pitch = region->pitch;
728 cpp = region->cpp;
729
730 DBG("%s dst:buf(%p)/%d %d,%d sz:%dx%d\n",
731 __FUNCTION__, region->bo, pitch, x, y, width, height);
732
733 BR13 = br13_for_cpp(cpp) | 0xf0 << 16;
734 CMD = XY_COLOR_BLT_CMD;
735 CMD |= XY_BLT_WRITE_ALPHA;
736
737 #ifndef I915
738 if (region->tiling != I915_TILING_NONE) {
739 CMD |= XY_DST_TILED;
740 pitch /= 4;
741 }
742 #endif
743 BR13 |= pitch;
744
745 /* do space check before going any further */
746 aper_array[0] = intel->batch.bo;
747 aper_array[1] = region->bo;
748
749 if (drm_intel_bufmgr_check_aperture_space(aper_array,
750 ARRAY_SIZE(aper_array)) != 0) {
751 intel_batchbuffer_flush(intel);
752 }
753
754 bool dst_y_tiled = region->tiling == I915_TILING_Y;
755
756 BEGIN_BATCH_BLT_TILED(6, dst_y_tiled, false);
757 OUT_BATCH(CMD | (6 - 2));
758 OUT_BATCH(BR13);
759 OUT_BATCH((y << 16) | x);
760 OUT_BATCH(((y + height) << 16) | (x + width));
761 OUT_RELOC_FENCED(region->bo,
762 I915_GEM_DOMAIN_RENDER, I915_GEM_DOMAIN_RENDER,
763 0);
764 OUT_BATCH(0xffffffff); /* white, but only alpha gets written */
765 ADVANCE_BATCH_TILED(dst_y_tiled, false);
766
767 intel_batchbuffer_emit_mi_flush(intel);
768 }