i965: use _mesa_readpixels() instead of _swrast_ReadPixels()
[mesa.git] / src / mesa / drivers / dri / intel / 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
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"
42
43 #define FILE_DEBUG_FLAG DEBUG_BLIT
44
45 static GLuint translate_raster_op(GLenum logicop)
46 {
47 switch(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;
64 default: return 0;
65 }
66 }
67
68 static uint32_t
69 br13_for_cpp(int cpp)
70 {
71 switch (cpp) {
72 case 4:
73 return BR13_8888;
74 break;
75 case 2:
76 return BR13_565;
77 break;
78 case 1:
79 return BR13_8;
80 break;
81 default:
82 assert(0);
83 return 0;
84 }
85 }
86
87 /* Copy BitBlt
88 */
89 bool
90 intelEmitCopyBlit(struct intel_context *intel,
91 GLuint cpp,
92 GLshort src_pitch,
93 drm_intel_bo *src_buffer,
94 GLuint src_offset,
95 uint32_t src_tiling,
96 GLshort dst_pitch,
97 drm_intel_bo *dst_buffer,
98 GLuint dst_offset,
99 uint32_t dst_tiling,
100 GLshort src_x, GLshort src_y,
101 GLshort dst_x, GLshort dst_y,
102 GLshort w, GLshort h,
103 GLenum logic_op)
104 {
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];
109 BATCH_LOCALS;
110
111 if (dst_tiling != I915_TILING_NONE) {
112 if (dst_offset & 4095)
113 return false;
114 if (dst_tiling == I915_TILING_Y)
115 return false;
116 }
117 if (src_tiling != I915_TILING_NONE) {
118 if (src_offset & 4095)
119 return false;
120 if (src_tiling == I915_TILING_Y)
121 return false;
122 }
123
124 /* do space check before going any further */
125 do {
126 aper_array[0] = intel->batch.bo;
127 aper_array[1] = dst_buffer;
128 aper_array[2] = src_buffer;
129
130 if (dri_bufmgr_check_aperture_space(aper_array, 3) != 0) {
131 intel_batchbuffer_flush(intel);
132 pass++;
133 } else
134 break;
135 } while (pass < 2);
136
137 if (pass >= 2)
138 return false;
139
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",
142 __FUNCTION__,
143 src_buffer, src_pitch, src_offset, src_x, src_y,
144 dst_buffer, dst_pitch, dst_offset, dst_x, dst_y, w, h);
145
146 src_pitch *= cpp;
147 dst_pitch *= cpp;
148
149 /* Blit pitch must be dword-aligned. Otherwise, the hardware appears to drop
150 * the low bits.
151 */
152 assert(src_pitch % 4 == 0);
153 assert(dst_pitch % 4 == 0);
154
155 /* For big formats (such as floating point), do the copy using 32bpp and
156 * multiply the coordinates.
157 */
158 if (cpp > 4) {
159 assert(cpp % 4 == 0);
160 dst_x *= cpp / 4;
161 dst_x2 *= cpp / 4;
162 src_x *= cpp / 4;
163 cpp = 4;
164 }
165
166 BR13 = br13_for_cpp(cpp) | translate_raster_op(logic_op) << 16;
167
168 switch (cpp) {
169 case 1:
170 case 2:
171 CMD = XY_SRC_COPY_BLT_CMD;
172 break;
173 case 4:
174 CMD = XY_SRC_COPY_BLT_CMD | XY_BLT_WRITE_ALPHA | XY_BLT_WRITE_RGB;
175 break;
176 default:
177 return false;
178 }
179
180 #ifndef I915
181 if (dst_tiling != I915_TILING_NONE) {
182 CMD |= XY_DST_TILED;
183 dst_pitch /= 4;
184 }
185 if (src_tiling != I915_TILING_NONE) {
186 CMD |= XY_SRC_TILED;
187 src_pitch /= 4;
188 }
189 #endif
190
191 if (dst_y2 <= dst_y || dst_x2 <= dst_x) {
192 return true;
193 }
194
195 assert(dst_x < dst_x2);
196 assert(dst_y < dst_y2);
197
198 BEGIN_BATCH_BLT(8);
199 OUT_BATCH(CMD);
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,
205 dst_offset);
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,
210 src_offset);
211 ADVANCE_BATCH();
212
213 intel_batchbuffer_emit_mi_flush(intel);
214
215 return true;
216 }
217
218
219 /**
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
225 */
226 GLbitfield
227 intelClearWithBlit(struct gl_context *ctx, GLbitfield mask)
228 {
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;
234 BATCH_LOCALS;
235
236 /*
237 * Compute values for clearing the buffers.
238 */
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;
244 }
245 if (mask & BUFFER_BIT_STENCIL) {
246 clear_depth_value |= (ctx->Stencil.Clear & 0xff) << 24;
247 clear_depth_mask |= XY_BLT_WRITE_ALPHA;
248 }
249
250 cx = fb->_Xmin;
251 if (fb->Name == 0)
252 cy = ctx->DrawBuffer->Height - fb->_Ymax;
253 else
254 cy = fb->_Ymin;
255 cw = fb->_Xmax - fb->_Xmin;
256 ch = fb->_Ymax - fb->_Ymin;
257
258 if (cw == 0 || ch == 0)
259 return 0;
260
261 /* Loop over all renderbuffers */
262 mask &= (1 << BUFFER_COUNT) - 1;
263 while (mask) {
264 GLuint buf = _mesa_ffs(mask) - 1;
265 bool is_depth_stencil = buf == BUFFER_DEPTH || buf == BUFFER_STENCIL;
266 struct intel_renderbuffer *irb;
267 int x1, y1, x2, y2;
268 uint32_t clear_val;
269 uint32_t BR13, CMD;
270 int pitch, cpp;
271 drm_intel_bo *aper_array[2];
272
273 mask &= ~(1 << buf);
274
275 irb = intel_get_renderbuffer(fb, buf);
276 if (irb == NULL || irb->region == NULL || irb->region->bo == NULL) {
277 fail_mask |= 1 << buf;
278 continue;
279 }
280
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;
286
287 pitch = irb->region->pitch;
288 cpp = irb->region->cpp;
289
290 DBG("%s dst:buf(%p)/%d %d,%d sz:%dx%d\n",
291 __FUNCTION__,
292 irb->region->bo, (pitch * cpp),
293 x1, y1, x2 - x1, y2 - y1);
294
295 BR13 = 0xf0 << 16;
296 CMD = XY_COLOR_BLT_CMD;
297
298 /* Setup the blit command */
299 if (cpp == 4) {
300 if (is_depth_stencil) {
301 CMD |= clear_depth_mask;
302 } else {
303 /* clearing RGBA */
304 CMD |= XY_BLT_WRITE_ALPHA | XY_BLT_WRITE_RGB;
305 }
306 }
307
308 assert(irb->region->tiling != I915_TILING_Y);
309
310 #ifndef I915
311 if (irb->region->tiling != I915_TILING_NONE) {
312 CMD |= XY_DST_TILED;
313 pitch /= 4;
314 }
315 #endif
316 BR13 |= (pitch * cpp);
317
318 if (is_depth_stencil) {
319 clear_val = clear_depth_value;
320 } else {
321 uint8_t clear[4];
322 GLfloat *color = ctx->Color.ClearColor.f;
323
324 _mesa_unclamped_float_rgba_to_ubyte(clear, color);
325
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],
330 clear[1], clear[2]);
331 break;
332 case MESA_FORMAT_RGB565:
333 clear_val = PACK_COLOR_565(clear[0], clear[1], clear[2]);
334 break;
335 case MESA_FORMAT_ARGB4444:
336 clear_val = PACK_COLOR_4444(clear[3], clear[0],
337 clear[1], clear[2]);
338 break;
339 case MESA_FORMAT_ARGB1555:
340 clear_val = PACK_COLOR_1555(clear[3], clear[0],
341 clear[1], clear[2]);
342 break;
343 case MESA_FORMAT_A8:
344 clear_val = PACK_COLOR_8888(clear[3], clear[3],
345 clear[3], clear[3]);
346 break;
347 default:
348 fail_mask |= 1 << buf;
349 continue;
350 }
351 }
352
353 BR13 |= br13_for_cpp(cpp);
354
355 assert(x1 < x2);
356 assert(y1 < y2);
357
358 /* do space check before going any further */
359 aper_array[0] = intel->batch.bo;
360 aper_array[1] = irb->region->bo;
361
362 if (drm_intel_bufmgr_check_aperture_space(aper_array,
363 ARRAY_SIZE(aper_array)) != 0) {
364 intel_batchbuffer_flush(intel);
365 }
366
367 BEGIN_BATCH_BLT(6);
368 OUT_BATCH(CMD);
369 OUT_BATCH(BR13);
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,
374 0);
375 OUT_BATCH(clear_val);
376 ADVANCE_BATCH();
377
378 if (intel->always_flush_cache)
379 intel_batchbuffer_emit_mi_flush(intel);
380
381 if (buf == BUFFER_DEPTH || buf == BUFFER_STENCIL)
382 mask &= ~(BUFFER_BIT_DEPTH | BUFFER_BIT_STENCIL);
383 }
384
385 return fail_mask;
386 }
387
388 bool
389 intelEmitImmediateColorExpandBlit(struct intel_context *intel,
390 GLuint cpp,
391 GLubyte *src_bits, GLuint src_size,
392 GLuint fg_color,
393 GLshort dst_pitch,
394 drm_intel_bo *dst_buffer,
395 GLuint dst_offset,
396 uint32_t dst_tiling,
397 GLshort x, GLshort y,
398 GLshort w, GLshort h,
399 GLenum logic_op)
400 {
401 int dwords = ALIGN(src_size, 8) / 4;
402 uint32_t opcode, br13, blit_cmd;
403
404 if (dst_tiling != I915_TILING_NONE) {
405 if (dst_offset & 4095)
406 return false;
407 if (dst_tiling == I915_TILING_Y)
408 return false;
409 }
410
411 assert( logic_op - GL_CLEAR >= 0 );
412 assert( logic_op - GL_CLEAR < 0x10 );
413 assert(dst_pitch > 0);
414
415 if (w < 0 || h < 0)
416 return true;
417
418 dst_pitch *= cpp;
419
420 DBG("%s dst:buf(%p)/%d+%d %d,%d sz:%dx%d, %d bytes %d dwords\n",
421 __FUNCTION__,
422 dst_buffer, dst_pitch, dst_offset, x, y, w, h, src_size, dwords);
423
424 intel_batchbuffer_require_space(intel,
425 (8 * 4) +
426 (3 * 4) +
427 dwords * 4, true);
428
429 opcode = XY_SETUP_BLT_CMD;
430 if (cpp == 4)
431 opcode |= XY_BLT_WRITE_ALPHA | XY_BLT_WRITE_RGB;
432 #ifndef I915
433 if (dst_tiling != I915_TILING_NONE) {
434 opcode |= XY_DST_TILED;
435 dst_pitch /= 4;
436 }
437 #endif
438
439 br13 = dst_pitch | (translate_raster_op(logic_op) << 16) | (1 << 29);
440 br13 |= br13_for_cpp(cpp);
441
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;
445
446 BEGIN_BATCH_BLT(8 + 3);
447 OUT_BATCH(opcode);
448 OUT_BATCH(br13);
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,
453 dst_offset);
454 OUT_BATCH(0); /* bg */
455 OUT_BATCH(fg_color); /* fg */
456 OUT_BATCH(0); /* pattern base addr */
457
458 OUT_BATCH(blit_cmd | ((3 - 2) + dwords));
459 OUT_BATCH((y << 16) | x);
460 OUT_BATCH(((y + h) << 16) | (x + w));
461 ADVANCE_BATCH();
462
463 intel_batchbuffer_data(intel, src_bits, dwords * 4, true);
464
465 intel_batchbuffer_emit_mi_flush(intel);
466
467 return true;
468 }
469
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.
473 */
474 void
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,
480 unsigned int size)
481 {
482 GLuint pitch, height;
483 bool ok;
484
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
488 */
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,
494 0, 0, /* src x/y */
495 0, 0, /* dst x/y */
496 pitch, height, /* w, h */
497 GL_COPY);
498 assert(ok);
499
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 */
505 if (size != 0) {
506 ok = intelEmitCopyBlit(intel, 1,
507 size, src_bo, src_offset, I915_TILING_NONE,
508 size, dst_bo, dst_offset, I915_TILING_NONE,
509 0, 0, /* src x/y */
510 0, 0, /* dst x/y */
511 size, 1, /* w, h */
512 GL_COPY);
513 assert(ok);
514 }
515 }
516
517 /**
518 * Used to initialize the alpha value of an ARGB8888 teximage after
519 * loading it from an XRGB8888 source.
520 *
521 * This is very common with glCopyTexImage2D().
522 */
523 void
524 intel_set_teximage_alpha_to_one(struct gl_context *ctx,
525 struct intel_texture_image *intel_image)
526 {
527 struct intel_context *intel = intel_context(ctx);
528 unsigned int image_x, image_y;
529 uint32_t x1, y1, x2, y2;
530 uint32_t BR13, CMD;
531 int pitch, cpp;
532 drm_intel_bo *aper_array[2];
533 struct intel_region *region = intel_image->mt->region;
534 int width, height, depth;
535 BATCH_LOCALS;
536
537 intel_miptree_get_dimensions_for_image(&intel_image->base.Base,
538 &width, &height, &depth);
539 assert(depth == 1);
540
541 assert(intel_image->base.Base.TexFormat == MESA_FORMAT_ARGB8888);
542
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,
547 0,
548 &image_x, &image_y);
549
550 x1 = image_x;
551 y1 = image_y;
552 x2 = image_x + width;
553 y2 = image_y + height;
554
555 pitch = region->pitch;
556 cpp = region->cpp;
557
558 DBG("%s dst:buf(%p)/%d %d,%d sz:%dx%d\n",
559 __FUNCTION__,
560 intel_image->mt->region->bo, (pitch * cpp),
561 x1, y1, x2 - x1, y2 - y1);
562
563 BR13 = br13_for_cpp(cpp) | 0xf0 << 16;
564 CMD = XY_COLOR_BLT_CMD;
565 CMD |= XY_BLT_WRITE_ALPHA;
566
567 assert(region->tiling != I915_TILING_Y);
568
569 #ifndef I915
570 if (region->tiling != I915_TILING_NONE) {
571 CMD |= XY_DST_TILED;
572 pitch /= 4;
573 }
574 #endif
575 BR13 |= (pitch * cpp);
576
577 /* do space check before going any further */
578 aper_array[0] = intel->batch.bo;
579 aper_array[1] = region->bo;
580
581 if (drm_intel_bufmgr_check_aperture_space(aper_array,
582 ARRAY_SIZE(aper_array)) != 0) {
583 intel_batchbuffer_flush(intel);
584 }
585
586 BEGIN_BATCH_BLT(6);
587 OUT_BATCH(CMD);
588 OUT_BATCH(BR13);
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,
593 0);
594 OUT_BATCH(0xffffffff); /* white, but only alpha gets written */
595 ADVANCE_BATCH();
596
597 intel_batchbuffer_emit_mi_flush(intel);
598 }