46578318af2c51bd1f677a69b9069a4db17e41f0
[mesa.git] / src / gallium / drivers / r300 / r300_blit.c
1 /*
2 * Copyright 2009 Marek Olšák <maraeo@gmail.com>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE. */
22
23 #include "r300_context.h"
24 #include "r300_emit.h"
25 #include "r300_texture.h"
26 #include "r300_reg.h"
27
28 #include "util/u_format.h"
29 #include "util/u_pack_color.h"
30 #include "util/u_surface.h"
31
32 enum r300_blitter_op /* bitmask */
33 {
34 R300_STOP_QUERY = 1,
35 R300_SAVE_TEXTURES = 2,
36 R300_SAVE_FRAMEBUFFER = 4,
37 R300_IGNORE_RENDER_COND = 8,
38
39 R300_CLEAR = R300_STOP_QUERY,
40
41 R300_CLEAR_SURFACE = R300_STOP_QUERY | R300_SAVE_FRAMEBUFFER,
42
43 R300_COPY = R300_STOP_QUERY | R300_SAVE_FRAMEBUFFER |
44 R300_SAVE_TEXTURES | R300_IGNORE_RENDER_COND,
45
46 R300_BLIT = R300_STOP_QUERY | R300_SAVE_FRAMEBUFFER |
47 R300_SAVE_TEXTURES | R300_IGNORE_RENDER_COND,
48
49 R300_DECOMPRESS = R300_STOP_QUERY | R300_IGNORE_RENDER_COND,
50 };
51
52 static void r300_blitter_begin(struct r300_context* r300, enum r300_blitter_op op)
53 {
54 if ((op & R300_STOP_QUERY) && r300->query_current) {
55 r300->blitter_saved_query = r300->query_current;
56 r300_stop_query(r300);
57 }
58
59 /* Yeah we have to save all those states to ensure the blitter operation
60 * is really transparent. The states will be restored by the blitter once
61 * copying is done. */
62 util_blitter_save_blend(r300->blitter, r300->blend_state.state);
63 util_blitter_save_depth_stencil_alpha(r300->blitter, r300->dsa_state.state);
64 util_blitter_save_stencil_ref(r300->blitter, &(r300->stencil_ref));
65 util_blitter_save_rasterizer(r300->blitter, r300->rs_state.state);
66 util_blitter_save_fragment_shader(r300->blitter, r300->fs.state);
67 util_blitter_save_vertex_shader(r300->blitter, r300->vs_state.state);
68 util_blitter_save_viewport(r300->blitter, &r300->viewport);
69 util_blitter_save_scissor(r300->blitter, r300->scissor_state.state);
70 util_blitter_save_sample_mask(r300->blitter, *(unsigned*)r300->sample_mask.state);
71 util_blitter_save_vertex_buffer_slot(r300->blitter, r300->vertex_buffer);
72 util_blitter_save_vertex_elements(r300->blitter, r300->velems);
73
74 if (op & R300_SAVE_FRAMEBUFFER) {
75 util_blitter_save_framebuffer(r300->blitter, r300->fb_state.state);
76 }
77
78 if (op & R300_SAVE_TEXTURES) {
79 struct r300_textures_state* state =
80 (struct r300_textures_state*)r300->textures_state.state;
81
82 util_blitter_save_fragment_sampler_states(
83 r300->blitter, state->sampler_state_count,
84 (void**)state->sampler_states);
85
86 util_blitter_save_fragment_sampler_views(
87 r300->blitter, state->sampler_view_count,
88 (struct pipe_sampler_view**)state->sampler_views);
89 }
90
91 if (op & R300_IGNORE_RENDER_COND) {
92 /* Save the flag. */
93 r300->blitter_saved_skip_rendering = r300->skip_rendering+1;
94 r300->skip_rendering = FALSE;
95 } else {
96 r300->blitter_saved_skip_rendering = 0;
97 }
98 }
99
100 static void r300_blitter_end(struct r300_context *r300)
101 {
102 if (r300->blitter_saved_query) {
103 r300_resume_query(r300, r300->blitter_saved_query);
104 r300->blitter_saved_query = NULL;
105 }
106
107 if (r300->blitter_saved_skip_rendering) {
108 /* Restore the flag. */
109 r300->skip_rendering = r300->blitter_saved_skip_rendering-1;
110 }
111 }
112
113 static uint32_t r300_depth_clear_cb_value(enum pipe_format format,
114 const float* rgba)
115 {
116 union util_color uc;
117 util_pack_color(rgba, format, &uc);
118
119 if (util_format_get_blocksizebits(format) == 32)
120 return uc.ui;
121 else
122 return uc.us | (uc.us << 16);
123 }
124
125 static boolean r300_cbzb_clear_allowed(struct r300_context *r300,
126 unsigned clear_buffers)
127 {
128 struct pipe_framebuffer_state *fb =
129 (struct pipe_framebuffer_state*)r300->fb_state.state;
130
131 /* Only color clear allowed, and only one colorbuffer. */
132 if (clear_buffers != PIPE_CLEAR_COLOR || fb->nr_cbufs != 1)
133 return FALSE;
134
135 return r300_surface(fb->cbufs[0])->cbzb_allowed;
136 }
137
138 static boolean r300_fast_zclear_allowed(struct r300_context *r300)
139 {
140 struct pipe_framebuffer_state *fb =
141 (struct pipe_framebuffer_state*)r300->fb_state.state;
142
143 return r300_resource(fb->zsbuf->texture)->tex.zmask_dwords[fb->zsbuf->u.tex.level] != 0;
144 }
145
146 static boolean r300_hiz_clear_allowed(struct r300_context *r300)
147 {
148 struct pipe_framebuffer_state *fb =
149 (struct pipe_framebuffer_state*)r300->fb_state.state;
150
151 return r300_resource(fb->zsbuf->texture)->tex.hiz_dwords[fb->zsbuf->u.tex.level] != 0;
152 }
153
154 static uint32_t r300_depth_clear_value(enum pipe_format format,
155 double depth, unsigned stencil)
156 {
157 switch (format) {
158 case PIPE_FORMAT_Z16_UNORM:
159 case PIPE_FORMAT_X8Z24_UNORM:
160 return util_pack_z(format, depth);
161
162 case PIPE_FORMAT_S8_UINT_Z24_UNORM:
163 return util_pack_z_stencil(format, depth, stencil);
164
165 default:
166 assert(0);
167 return 0;
168 }
169 }
170
171 static uint32_t r300_hiz_clear_value(double depth)
172 {
173 uint32_t r = (uint32_t)(CLAMP(depth, 0, 1) * 255.5);
174 assert(r <= 255);
175 return r | (r << 8) | (r << 16) | (r << 24);
176 }
177
178 DEBUG_GET_ONCE_BOOL_OPTION(hyperz, "RADEON_HYPERZ", FALSE)
179
180 /* Clear currently bound buffers. */
181 static void r300_clear(struct pipe_context* pipe,
182 unsigned buffers,
183 const union pipe_color_union *color,
184 double depth,
185 unsigned stencil)
186 {
187 /* My notes about Zbuffer compression:
188 *
189 * 1) The zbuffer must be micro-tiled and whole microtiles must be
190 * written if compression is enabled. If microtiling is disabled,
191 * it locks up.
192 *
193 * 2) There is ZMASK RAM which contains a compressed zbuffer.
194 * Each dword of the Z Mask contains compression information
195 * for 16 4x4 pixel tiles, that is 2 bits for each tile.
196 * On chips with 2 Z pipes, every other dword maps to a different
197 * pipe. On newer chipsets, there is a new compression mode
198 * with 8x8 pixel tiles per 2 bits.
199 *
200 * 3) The FASTFILL bit has nothing to do with filling. It only tells hw
201 * it should look in the ZMASK RAM first before fetching from a real
202 * zbuffer.
203 *
204 * 4) If a pixel is in a cleared state, ZB_DEPTHCLEARVALUE is returned
205 * during zbuffer reads instead of the value that is actually stored
206 * in the zbuffer memory. A pixel is in a cleared state when its ZMASK
207 * is equal to 0. Therefore, if you clear ZMASK with zeros, you may
208 * leave the zbuffer memory uninitialized, but then you must enable
209 * compression, so that the ZMASK RAM is actually used.
210 *
211 * 5) Each 4x4 (or 8x8) tile is automatically decompressed and recompressed
212 * during zbuffer updates. A special decompressing operation should be
213 * used to fully decompress a zbuffer, which basically just stores all
214 * compressed tiles in ZMASK to the zbuffer memory.
215 *
216 * 6) For a 16-bit zbuffer, compression causes a hung with one or
217 * two samples and should not be used.
218 *
219 * 7) FORCE_COMPRESSED_STENCIL_VALUE should be enabled for stencil clears
220 * to avoid needless decompression.
221 *
222 * 8) Fastfill must not be used if reading of compressed Z data is disabled
223 * and writing of compressed Z data is enabled (RD/WR_COMP_ENABLE),
224 * i.e. it cannot be used to compress the zbuffer.
225 *
226 * 9) ZB_CB_CLEAR does not interact with zbuffer compression in any way.
227 *
228 * - Marek
229 */
230
231 struct r300_context* r300 = r300_context(pipe);
232 struct pipe_framebuffer_state *fb =
233 (struct pipe_framebuffer_state*)r300->fb_state.state;
234 struct r300_hyperz_state *hyperz =
235 (struct r300_hyperz_state*)r300->hyperz_state.state;
236 uint32_t width = fb->width;
237 uint32_t height = fb->height;
238 uint32_t hyperz_dcv = hyperz->zb_depthclearvalue;
239
240 /* Enable fast Z clear.
241 * The zbuffer must be in micro-tiled mode, otherwise it locks up. */
242 if (buffers & PIPE_CLEAR_DEPTHSTENCIL) {
243 boolean zmask_clear, hiz_clear;
244
245 zmask_clear = r300_fast_zclear_allowed(r300);
246 hiz_clear = r300_hiz_clear_allowed(r300);
247
248 /* If we need Hyper-Z. */
249 if (zmask_clear || hiz_clear) {
250 /* Try to obtain the access to Hyper-Z buffers if we don't have one. */
251 if (!r300->hyperz_enabled &&
252 (r300->screen->caps.is_r500 || debug_get_option_hyperz())) {
253 r300->hyperz_enabled =
254 r300->rws->cs_request_feature(r300->cs,
255 RADEON_FID_R300_HYPERZ_ACCESS,
256 TRUE);
257 if (r300->hyperz_enabled) {
258 /* Need to emit HyperZ buffer regs for the first time. */
259 r300_mark_fb_state_dirty(r300, R300_CHANGED_HYPERZ_FLAG);
260 }
261 }
262
263 /* Setup Hyper-Z clears. */
264 if (r300->hyperz_enabled) {
265 DBG(r300, DBG_HYPERZ, "r300: Clear memory: %s%s\n",
266 zmask_clear ? "ZMASK " : "", hiz_clear ? "HIZ" : "");
267
268 if (zmask_clear) {
269 hyperz_dcv = hyperz->zb_depthclearvalue =
270 r300_depth_clear_value(fb->zsbuf->format, depth, stencil);
271
272 r300_mark_atom_dirty(r300, &r300->zmask_clear);
273 buffers &= ~PIPE_CLEAR_DEPTHSTENCIL;
274 }
275
276 if (hiz_clear) {
277 r300->hiz_clear_value = r300_hiz_clear_value(depth);
278 r300_mark_atom_dirty(r300, &r300->hiz_clear);
279 }
280 r300->num_z_clears++;
281 }
282 }
283 }
284
285 /* Enable CBZB clear. */
286 if (r300_cbzb_clear_allowed(r300, buffers)) {
287 struct r300_surface *surf = r300_surface(fb->cbufs[0]);
288
289 hyperz->zb_depthclearvalue =
290 r300_depth_clear_cb_value(surf->base.format, color->f);
291
292 width = surf->cbzb_width;
293 height = surf->cbzb_height;
294
295 r300->cbzb_clear = TRUE;
296 r300_mark_fb_state_dirty(r300, R300_CHANGED_HYPERZ_FLAG);
297 }
298
299 /* Clear. */
300 if (buffers) {
301 enum pipe_format cformat = fb->nr_cbufs ? fb->cbufs[0]->format : PIPE_FORMAT_NONE;
302 /* Clear using the blitter. */
303 r300_blitter_begin(r300, R300_CLEAR);
304 util_blitter_clear(r300->blitter,
305 width,
306 height,
307 fb->nr_cbufs,
308 buffers, cformat, color, depth, stencil);
309 r300_blitter_end(r300);
310 } else if (r300->zmask_clear.dirty || r300->hiz_clear.dirty) {
311 /* Just clear zmask and hiz now, this does not use the standard draw
312 * procedure. */
313 /* Calculate zmask_clear and hiz_clear atom sizes. */
314 unsigned dwords =
315 (r300->zmask_clear.dirty ? r300->zmask_clear.size : 0) +
316 (r300->hiz_clear.dirty ? r300->hiz_clear.size : 0) +
317 r300_get_num_cs_end_dwords(r300);
318
319 /* Reserve CS space. */
320 if (dwords > (RADEON_MAX_CMDBUF_DWORDS - r300->cs->cdw)) {
321 r300_flush(&r300->context, RADEON_FLUSH_ASYNC, NULL);
322 }
323
324 /* Emit clear packets. */
325 if (r300->zmask_clear.dirty) {
326 r300_emit_zmask_clear(r300, r300->zmask_clear.size,
327 r300->zmask_clear.state);
328 r300->zmask_clear.dirty = FALSE;
329 }
330 if (r300->hiz_clear.dirty) {
331 r300_emit_hiz_clear(r300, r300->hiz_clear.size,
332 r300->hiz_clear.state);
333 r300->hiz_clear.dirty = FALSE;
334 }
335 } else {
336 assert(0);
337 }
338
339 /* Disable CBZB clear. */
340 if (r300->cbzb_clear) {
341 r300->cbzb_clear = FALSE;
342 hyperz->zb_depthclearvalue = hyperz_dcv;
343 r300_mark_fb_state_dirty(r300, R300_CHANGED_HYPERZ_FLAG);
344 }
345
346 /* Enable fastfill and/or hiz.
347 *
348 * If we cleared zmask/hiz, it's in use now. The Hyper-Z state update
349 * looks if zmask/hiz is in use and programs hardware accordingly. */
350 if (r300->zmask_in_use || r300->hiz_in_use) {
351 r300_mark_atom_dirty(r300, &r300->hyperz_state);
352 }
353 }
354
355 /* Clear a region of a color surface to a constant value. */
356 static void r300_clear_render_target(struct pipe_context *pipe,
357 struct pipe_surface *dst,
358 const union pipe_color_union *color,
359 unsigned dstx, unsigned dsty,
360 unsigned width, unsigned height)
361 {
362 struct r300_context *r300 = r300_context(pipe);
363
364 r300_blitter_begin(r300, R300_CLEAR_SURFACE);
365 util_blitter_clear_render_target(r300->blitter, dst, color,
366 dstx, dsty, width, height);
367 r300_blitter_end(r300);
368 }
369
370 /* Clear a region of a depth stencil surface. */
371 static void r300_clear_depth_stencil(struct pipe_context *pipe,
372 struct pipe_surface *dst,
373 unsigned clear_flags,
374 double depth,
375 unsigned stencil,
376 unsigned dstx, unsigned dsty,
377 unsigned width, unsigned height)
378 {
379 struct r300_context *r300 = r300_context(pipe);
380 struct pipe_framebuffer_state *fb =
381 (struct pipe_framebuffer_state*)r300->fb_state.state;
382
383 if (r300->zmask_in_use && !r300->locked_zbuffer) {
384 if (fb->zsbuf->texture == dst->texture) {
385 r300_decompress_zmask(r300);
386 }
387 }
388
389 /* XXX Do not decompress ZMask of the currently-set zbuffer. */
390 r300_blitter_begin(r300, R300_CLEAR_SURFACE);
391 util_blitter_clear_depth_stencil(r300->blitter, dst, clear_flags, depth, stencil,
392 dstx, dsty, width, height);
393 r300_blitter_end(r300);
394 }
395
396 void r300_decompress_zmask(struct r300_context *r300)
397 {
398 struct pipe_framebuffer_state *fb =
399 (struct pipe_framebuffer_state*)r300->fb_state.state;
400
401 if (!r300->zmask_in_use || r300->locked_zbuffer)
402 return;
403
404 r300->zmask_decompress = TRUE;
405 r300_mark_atom_dirty(r300, &r300->hyperz_state);
406
407 r300_blitter_begin(r300, R300_DECOMPRESS);
408 util_blitter_custom_clear_depth(r300->blitter, fb->width, fb->height, 0,
409 r300->dsa_decompress_zmask);
410 r300_blitter_end(r300);
411
412 r300->zmask_decompress = FALSE;
413 r300->zmask_in_use = FALSE;
414 r300_mark_atom_dirty(r300, &r300->hyperz_state);
415 }
416
417 void r300_decompress_zmask_locked_unsafe(struct r300_context *r300)
418 {
419 struct pipe_framebuffer_state fb;
420
421 memset(&fb, 0, sizeof(fb));
422 fb.width = r300->locked_zbuffer->width;
423 fb.height = r300->locked_zbuffer->height;
424 fb.zsbuf = r300->locked_zbuffer;
425
426 r300->context.set_framebuffer_state(&r300->context, &fb);
427 r300_decompress_zmask(r300);
428 }
429
430 void r300_decompress_zmask_locked(struct r300_context *r300)
431 {
432 struct pipe_framebuffer_state saved_fb;
433
434 memset(&saved_fb, 0, sizeof(saved_fb));
435 util_copy_framebuffer_state(&saved_fb, r300->fb_state.state);
436 r300_decompress_zmask_locked_unsafe(r300);
437 r300->context.set_framebuffer_state(&r300->context, &saved_fb);
438 util_unreference_framebuffer_state(&saved_fb);
439
440 pipe_surface_reference(&r300->locked_zbuffer, NULL);
441 }
442
443 bool r300_is_blit_supported(enum pipe_format format)
444 {
445 const struct util_format_description *desc =
446 util_format_description(format);
447
448 return desc->layout == UTIL_FORMAT_LAYOUT_PLAIN ||
449 desc->layout == UTIL_FORMAT_LAYOUT_S3TC ||
450 desc->layout == UTIL_FORMAT_LAYOUT_RGTC;
451 }
452
453 /* Copy a block of pixels from one surface to another. */
454 static void r300_resource_copy_region(struct pipe_context *pipe,
455 struct pipe_resource *dst,
456 unsigned dst_level,
457 unsigned dstx, unsigned dsty, unsigned dstz,
458 struct pipe_resource *src,
459 unsigned src_level,
460 const struct pipe_box *src_box)
461 {
462 struct pipe_screen *screen = pipe->screen;
463 struct r300_context *r300 = r300_context(pipe);
464 struct pipe_framebuffer_state *fb =
465 (struct pipe_framebuffer_state*)r300->fb_state.state;
466 unsigned src_width0 = r300_resource(src)->tex.width0;
467 unsigned src_height0 = r300_resource(src)->tex.height0;
468 unsigned dst_width0 = r300_resource(dst)->tex.width0;
469 unsigned dst_height0 = r300_resource(dst)->tex.height0;
470 unsigned layout;
471 struct pipe_box box, dstbox;
472 struct pipe_sampler_view src_templ, *src_view;
473 struct pipe_surface dst_templ, *dst_view;
474
475 /* Fallback for buffers. */
476 if ((dst->target == PIPE_BUFFER && src->target == PIPE_BUFFER) ||
477 !r300_is_blit_supported(dst->format)) {
478 util_resource_copy_region(pipe, dst, dst_level, dstx, dsty, dstz,
479 src, src_level, src_box);
480 return;
481 }
482
483 /* Can't read MSAA textures. */
484 if (src->nr_samples > 1 || dst->nr_samples > 1) {
485 return;
486 }
487
488 /* The code below changes the texture format so that the copy can be done
489 * on hardware. E.g. depth-stencil surfaces are copied as RGBA
490 * colorbuffers. */
491
492 util_blitter_default_dst_texture(&dst_templ, dst, dst_level, dstz);
493 util_blitter_default_src_texture(&src_templ, src, src_level);
494
495 layout = util_format_description(dst_templ.format)->layout;
496
497 /* Handle non-renderable plain formats. */
498 if (layout == UTIL_FORMAT_LAYOUT_PLAIN &&
499 (!screen->is_format_supported(screen, src_templ.format, src->target,
500 src->nr_samples,
501 PIPE_BIND_SAMPLER_VIEW) ||
502 !screen->is_format_supported(screen, dst_templ.format, dst->target,
503 dst->nr_samples,
504 PIPE_BIND_RENDER_TARGET))) {
505 switch (util_format_get_blocksize(dst_templ.format)) {
506 case 1:
507 dst_templ.format = PIPE_FORMAT_I8_UNORM;
508 break;
509 case 2:
510 dst_templ.format = PIPE_FORMAT_B4G4R4A4_UNORM;
511 break;
512 case 4:
513 dst_templ.format = PIPE_FORMAT_B8G8R8A8_UNORM;
514 break;
515 case 8:
516 dst_templ.format = PIPE_FORMAT_R16G16B16A16_UNORM;
517 break;
518 default:
519 debug_printf("r300: copy_region: Unhandled format: %s. Falling back to software.\n"
520 "r300: copy_region: Software fallback doesn't work for tiled textures.\n",
521 util_format_short_name(dst_templ.format));
522 }
523 src_templ.format = dst_templ.format;
524 }
525
526 /* Handle compressed formats. */
527 if (layout == UTIL_FORMAT_LAYOUT_S3TC ||
528 layout == UTIL_FORMAT_LAYOUT_RGTC) {
529 assert(src_templ.format == dst_templ.format);
530
531 box = *src_box;
532 src_box = &box;
533
534 dst_width0 = align(dst_width0, 4);
535 dst_height0 = align(dst_height0, 4);
536 src_width0 = align(src_width0, 4);
537 src_height0 = align(src_height0, 4);
538 box.width = align(box.width, 4);
539 box.height = align(box.height, 4);
540
541 switch (util_format_get_blocksize(dst_templ.format)) {
542 case 8:
543 /* one 4x4 pixel block has 8 bytes.
544 * we set 1 pixel = 4 bytes ===> 1 block corrensponds to 2 pixels. */
545 dst_templ.format = PIPE_FORMAT_R8G8B8A8_UNORM;
546 dst_width0 = dst_width0 / 2;
547 src_width0 = src_width0 / 2;
548 dstx /= 2;
549 box.x /= 2;
550 box.width /= 2;
551 break;
552 case 16:
553 /* one 4x4 pixel block has 16 bytes.
554 * we set 1 pixel = 4 bytes ===> 1 block corresponds to 4 pixels. */
555 dst_templ.format = PIPE_FORMAT_R8G8B8A8_UNORM;
556 break;
557 }
558 src_templ.format = dst_templ.format;
559
560 dst_height0 = dst_height0 / 4;
561 src_height0 = src_height0 / 4;
562 dsty /= 4;
563 box.y /= 4;
564 box.height /= 4;
565 }
566
567 /* Fallback for textures. */
568 if (!screen->is_format_supported(screen, dst_templ.format,
569 dst->target, dst->nr_samples,
570 PIPE_BIND_RENDER_TARGET) ||
571 !screen->is_format_supported(screen, src_templ.format,
572 src->target, src->nr_samples,
573 PIPE_BIND_SAMPLER_VIEW)) {
574 assert(0 && "this shouldn't happen, update r300_is_blit_supported");
575 util_resource_copy_region(pipe, dst, dst_level, dstx, dsty, dstz,
576 src, src_level, src_box);
577 return;
578 }
579
580 /* Decompress ZMASK. */
581 if (r300->zmask_in_use && !r300->locked_zbuffer) {
582 if (fb->zsbuf->texture == src ||
583 fb->zsbuf->texture == dst) {
584 r300_decompress_zmask(r300);
585 }
586 }
587
588 dst_view = r300_create_surface_custom(pipe, dst, &dst_templ, dst_width0, dst_height0);
589 src_view = r300_create_sampler_view_custom(pipe, src, &src_templ, src_width0, src_height0);
590
591 u_box_3d(dstx, dsty, dstz, abs(src_box->width), abs(src_box->height),
592 abs(src_box->depth), &dstbox);
593
594 r300_blitter_begin(r300, R300_COPY);
595 util_blitter_blit_generic(r300->blitter, dst_view, &dstbox,
596 src_view, src_box, src_width0, src_height0,
597 PIPE_MASK_RGBAZS, PIPE_TEX_FILTER_NEAREST, NULL,
598 FALSE);
599 r300_blitter_end(r300);
600
601 pipe_surface_reference(&dst_view, NULL);
602 pipe_sampler_view_reference(&src_view, NULL);
603 }
604
605 static boolean r300_is_simple_msaa_resolve(const struct pipe_blit_info *info)
606 {
607 unsigned dst_width = u_minify(info->dst.resource->width0, info->dst.level);
608 unsigned dst_height = u_minify(info->dst.resource->height0, info->dst.level);
609
610 return info->dst.resource->format == info->src.resource->format &&
611 info->dst.resource->format == info->dst.format &&
612 info->src.resource->format == info->src.format &&
613 !info->scissor_enable &&
614 info->mask == PIPE_MASK_RGBA &&
615 dst_width == info->src.resource->width0 &&
616 dst_height == info->src.resource->height0 &&
617 info->dst.box.x == 0 &&
618 info->dst.box.y == 0 &&
619 info->dst.box.width == dst_width &&
620 info->dst.box.height == dst_height &&
621 info->src.box.x == 0 &&
622 info->src.box.y == 0 &&
623 info->src.box.width == dst_width &&
624 info->src.box.height == dst_height;
625 }
626
627 static void r300_simple_msaa_resolve(struct pipe_context *pipe,
628 struct pipe_resource *dst,
629 unsigned dst_level,
630 unsigned dst_layer,
631 struct pipe_resource *src,
632 enum pipe_format format)
633 {
634 struct r300_context *r300 = r300_context(pipe);
635 struct r300_surface *srcsurf, *dstsurf;
636 struct pipe_surface surf_tmpl;
637 struct r300_aa_state *aa = (struct r300_aa_state*)r300->aa_state.state;
638
639 memset(&surf_tmpl, 0, sizeof(surf_tmpl));
640 surf_tmpl.format = format;
641 srcsurf = r300_surface(pipe->create_surface(pipe, src, &surf_tmpl));
642
643 surf_tmpl.format = format;
644 surf_tmpl.u.tex.level = dst_level;
645 surf_tmpl.u.tex.first_layer =
646 surf_tmpl.u.tex.last_layer = dst_layer;
647 dstsurf = r300_surface(pipe->create_surface(pipe, dst, &surf_tmpl));
648
649 /* COLORPITCH should contain the tiling info of the resolve buffer.
650 * The tiling of the AA buffer isn't programmable anyway. */
651 srcsurf->pitch &= ~(R300_COLOR_TILE(1) | R300_COLOR_MICROTILE(3));
652 srcsurf->pitch |= dstsurf->pitch & (R300_COLOR_TILE(1) | R300_COLOR_MICROTILE(3));
653
654 /* Enable AA resolve. */
655 aa->dest = dstsurf;
656 r300->aa_state.size = 8;
657 r300_mark_atom_dirty(r300, &r300->aa_state);
658
659 /* Resolve the surface. */
660 r300_blitter_begin(r300, R300_CLEAR_SURFACE);
661 util_blitter_custom_color(r300->blitter, &srcsurf->base, NULL);
662 r300_blitter_end(r300);
663
664 /* Disable AA resolve. */
665 aa->dest = NULL;
666 r300->aa_state.size = 4;
667 r300_mark_atom_dirty(r300, &r300->aa_state);
668
669 pipe_surface_reference((struct pipe_surface**)&srcsurf, NULL);
670 pipe_surface_reference((struct pipe_surface**)&dstsurf, NULL);
671 }
672
673 static void r300_msaa_resolve(struct pipe_context *pipe,
674 const struct pipe_blit_info *info)
675 {
676 struct r300_context *r300 = r300_context(pipe);
677 struct pipe_screen *screen = pipe->screen;
678 struct pipe_resource *tmp, templ;
679 struct pipe_blit_info blit;
680
681 assert(info->src.level == 0);
682 assert(info->src.box.z == 0);
683 assert(info->src.box.depth == 1);
684 assert(info->dst.box.depth == 1);
685
686 if (r300_is_simple_msaa_resolve(info)) {
687 r300_simple_msaa_resolve(pipe, info->dst.resource, info->dst.level,
688 info->dst.box.z, info->src.resource,
689 info->src.format);
690 return;
691 }
692
693 /* resolve into a temporary texture, then blit */
694 memset(&templ, 0, sizeof(templ));
695 templ.target = PIPE_TEXTURE_2D;
696 templ.format = info->src.resource->format;
697 templ.width0 = info->src.resource->width0;
698 templ.height0 = info->src.resource->height0;
699 templ.depth0 = 1;
700 templ.array_size = 1;
701 templ.usage = PIPE_USAGE_STATIC;
702
703 tmp = screen->resource_create(screen, &templ);
704
705 /* resolve */
706 r300_simple_msaa_resolve(pipe, tmp, 0, 0, info->src.resource,
707 info->src.format);
708
709 /* blit */
710 blit = *info;
711 blit.src.resource = tmp;
712 blit.src.box.z = 0;
713
714 r300_blitter_begin(r300, R300_BLIT);
715 util_blitter_blit(r300->blitter, &blit);
716 r300_blitter_end(r300);
717
718 pipe_resource_reference(&tmp, NULL);
719 }
720
721 static void r300_blit(struct pipe_context *pipe,
722 const struct pipe_blit_info *blit)
723 {
724 struct r300_context *r300 = r300_context(pipe);
725 struct pipe_framebuffer_state *fb =
726 (struct pipe_framebuffer_state*)r300->fb_state.state;
727 struct pipe_blit_info info = *blit;
728
729 /* MSAA resolve. */
730 if (info.src.resource->nr_samples > 1 &&
731 info.dst.resource->nr_samples <= 1 &&
732 !util_format_is_depth_or_stencil(info.src.resource->format)) {
733 r300_msaa_resolve(pipe, &info);
734 return;
735 }
736
737 /* Can't read MSAA textures. */
738 if (info.src.resource->nr_samples > 1) {
739 return;
740 }
741
742 /* Blit a combined depth-stencil resource as color.
743 * S8Z24 is the only supported stencil format. */
744 if ((info.mask & PIPE_MASK_S) &&
745 info.src.format == PIPE_FORMAT_S8_UINT_Z24_UNORM &&
746 info.dst.format == PIPE_FORMAT_S8_UINT_Z24_UNORM) {
747 if (info.dst.resource->nr_samples > 1) {
748 /* Cannot do that with MSAA buffers. */
749 info.mask &= ~PIPE_MASK_S;
750 if (!(info.mask & PIPE_MASK_Z)) {
751 return;
752 }
753 } else {
754 /* Single-sample buffer. */
755 info.src.format = PIPE_FORMAT_B8G8R8A8_UNORM;
756 info.dst.format = PIPE_FORMAT_B8G8R8A8_UNORM;
757 if (info.mask & PIPE_MASK_Z) {
758 info.mask = PIPE_MASK_RGBA; /* depth+stencil */
759 } else {
760 info.mask = PIPE_MASK_B; /* stencil only */
761 }
762 }
763 }
764
765 /* Decompress ZMASK. */
766 if (r300->zmask_in_use && !r300->locked_zbuffer) {
767 if (fb->zsbuf->texture == info.src.resource ||
768 fb->zsbuf->texture == info.dst.resource) {
769 r300_decompress_zmask(r300);
770 }
771 }
772
773 r300_blitter_begin(r300, R300_BLIT);
774 util_blitter_blit(r300->blitter, &info);
775 r300_blitter_end(r300);
776 }
777
778 void r300_init_blit_functions(struct r300_context *r300)
779 {
780 r300->context.clear = r300_clear;
781 r300->context.clear_render_target = r300_clear_render_target;
782 r300->context.clear_depth_stencil = r300_clear_depth_stencil;
783 r300->context.resource_copy_region = r300_resource_copy_region;
784 r300->context.blit = r300_blit;
785 }