r300g: fix MSAA resolve to an untiled texture
[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 unsigned clear_buffers)
140 {
141 struct pipe_framebuffer_state *fb =
142 (struct pipe_framebuffer_state*)r300->fb_state.state;
143
144 return r300_resource(fb->zsbuf->texture)->tex.zmask_dwords[fb->zsbuf->u.tex.level] != 0;
145 }
146
147 static boolean r300_hiz_clear_allowed(struct r300_context *r300)
148 {
149 struct pipe_framebuffer_state *fb =
150 (struct pipe_framebuffer_state*)r300->fb_state.state;
151
152 return r300_resource(fb->zsbuf->texture)->tex.hiz_dwords[fb->zsbuf->u.tex.level] != 0;
153 }
154
155 static uint32_t r300_depth_clear_value(enum pipe_format format,
156 double depth, unsigned stencil)
157 {
158 switch (format) {
159 case PIPE_FORMAT_Z16_UNORM:
160 case PIPE_FORMAT_X8Z24_UNORM:
161 return util_pack_z(format, depth);
162
163 case PIPE_FORMAT_S8_UINT_Z24_UNORM:
164 return util_pack_z_stencil(format, depth, stencil);
165
166 default:
167 assert(0);
168 return 0;
169 }
170 }
171
172 static uint32_t r300_hiz_clear_value(double depth)
173 {
174 uint32_t r = (uint32_t)(CLAMP(depth, 0, 1) * 255.5);
175 assert(r <= 255);
176 return r | (r << 8) | (r << 16) | (r << 24);
177 }
178
179 DEBUG_GET_ONCE_BOOL_OPTION(hyperz, "RADEON_HYPERZ", FALSE)
180
181 /* Clear currently bound buffers. */
182 static void r300_clear(struct pipe_context* pipe,
183 unsigned buffers,
184 const union pipe_color_union *color,
185 double depth,
186 unsigned stencil)
187 {
188 /* My notes about Zbuffer compression:
189 *
190 * 1) The zbuffer must be micro-tiled and whole microtiles must be
191 * written if compression is enabled. If microtiling is disabled,
192 * it locks up.
193 *
194 * 2) There is ZMASK RAM which contains a compressed zbuffer.
195 * Each dword of the Z Mask contains compression information
196 * for 16 4x4 pixel tiles, that is 2 bits for each tile.
197 * On chips with 2 Z pipes, every other dword maps to a different
198 * pipe. On newer chipsets, there is a new compression mode
199 * with 8x8 pixel tiles per 2 bits.
200 *
201 * 3) The FASTFILL bit has nothing to do with filling. It only tells hw
202 * it should look in the ZMASK RAM first before fetching from a real
203 * zbuffer.
204 *
205 * 4) If a pixel is in a cleared state, ZB_DEPTHCLEARVALUE is returned
206 * during zbuffer reads instead of the value that is actually stored
207 * in the zbuffer memory. A pixel is in a cleared state when its ZMASK
208 * is equal to 0. Therefore, if you clear ZMASK with zeros, you may
209 * leave the zbuffer memory uninitialized, but then you must enable
210 * compression, so that the ZMASK RAM is actually used.
211 *
212 * 5) Each 4x4 (or 8x8) tile is automatically decompressed and recompressed
213 * during zbuffer updates. A special decompressing operation should be
214 * used to fully decompress a zbuffer, which basically just stores all
215 * compressed tiles in ZMASK to the zbuffer memory.
216 *
217 * 6) For a 16-bit zbuffer, compression causes a hung with one or
218 * two samples and should not be used.
219 *
220 * 7) FORCE_COMPRESSED_STENCIL_VALUE should be enabled for stencil clears
221 * to avoid needless decompression.
222 *
223 * 8) Fastfill must not be used if reading of compressed Z data is disabled
224 * and writing of compressed Z data is enabled (RD/WR_COMP_ENABLE),
225 * i.e. it cannot be used to compress the zbuffer.
226 *
227 * 9) ZB_CB_CLEAR does not interact with zbuffer compression in any way.
228 *
229 * - Marek
230 */
231
232 struct r300_context* r300 = r300_context(pipe);
233 struct pipe_framebuffer_state *fb =
234 (struct pipe_framebuffer_state*)r300->fb_state.state;
235 struct r300_hyperz_state *hyperz =
236 (struct r300_hyperz_state*)r300->hyperz_state.state;
237 uint32_t width = fb->width;
238 uint32_t height = fb->height;
239 uint32_t hyperz_dcv = hyperz->zb_depthclearvalue;
240
241 /* Use fast Z clear.
242 * The zbuffer must be in micro-tiled mode, otherwise it locks up. */
243 if (buffers & PIPE_CLEAR_DEPTHSTENCIL) {
244 boolean zmask_clear, hiz_clear;
245
246 /* If both depth and stencil are present, they must be cleared together. */
247 if (fb->zsbuf->texture->format == PIPE_FORMAT_S8_UINT_Z24_UNORM &&
248 (buffers & PIPE_CLEAR_DEPTHSTENCIL) != PIPE_CLEAR_DEPTHSTENCIL) {
249 zmask_clear = FALSE;
250 hiz_clear = FALSE;
251 } else {
252 zmask_clear = r300_fast_zclear_allowed(r300, buffers);
253 hiz_clear = r300_hiz_clear_allowed(r300);
254 }
255
256 /* If we need Hyper-Z. */
257 if (zmask_clear || hiz_clear) {
258 /* Try to obtain the access to Hyper-Z buffers if we don't have one. */
259 if (!r300->hyperz_enabled &&
260 (r300->screen->caps.is_r500 || debug_get_option_hyperz())) {
261 r300->hyperz_enabled =
262 r300->rws->cs_request_feature(r300->cs,
263 RADEON_FID_R300_HYPERZ_ACCESS,
264 TRUE);
265 if (r300->hyperz_enabled) {
266 /* Need to emit HyperZ buffer regs for the first time. */
267 r300_mark_fb_state_dirty(r300, R300_CHANGED_HYPERZ_FLAG);
268 }
269 }
270
271 /* Setup Hyper-Z clears. */
272 if (r300->hyperz_enabled) {
273 if (zmask_clear) {
274 hyperz_dcv = hyperz->zb_depthclearvalue =
275 r300_depth_clear_value(fb->zsbuf->format, depth, stencil);
276
277 r300_mark_atom_dirty(r300, &r300->zmask_clear);
278 buffers &= ~PIPE_CLEAR_DEPTHSTENCIL;
279 }
280
281 if (hiz_clear) {
282 r300->hiz_clear_value = r300_hiz_clear_value(depth);
283 r300_mark_atom_dirty(r300, &r300->hiz_clear);
284 }
285 r300->num_z_clears++;
286 }
287 }
288 }
289
290 /* Enable CBZB clear. */
291 if (r300_cbzb_clear_allowed(r300, buffers)) {
292 struct r300_surface *surf = r300_surface(fb->cbufs[0]);
293
294 hyperz->zb_depthclearvalue =
295 r300_depth_clear_cb_value(surf->base.format, color->f);
296
297 width = surf->cbzb_width;
298 height = surf->cbzb_height;
299
300 r300->cbzb_clear = TRUE;
301 r300_mark_fb_state_dirty(r300, R300_CHANGED_HYPERZ_FLAG);
302 }
303
304 /* Clear. */
305 if (buffers) {
306 enum pipe_format cformat = fb->nr_cbufs ? fb->cbufs[0]->format : PIPE_FORMAT_NONE;
307 /* Clear using the blitter. */
308 r300_blitter_begin(r300, R300_CLEAR);
309 util_blitter_clear(r300->blitter,
310 width,
311 height,
312 fb->nr_cbufs,
313 buffers, cformat, color, depth, stencil);
314 r300_blitter_end(r300);
315 } else if (r300->zmask_clear.dirty || r300->hiz_clear.dirty) {
316 /* Just clear zmask and hiz now, this does not use the standard draw
317 * procedure. */
318 /* Calculate zmask_clear and hiz_clear atom sizes. */
319 unsigned dwords =
320 (r300->zmask_clear.dirty ? r300->zmask_clear.size : 0) +
321 (r300->hiz_clear.dirty ? r300->hiz_clear.size : 0) +
322 r300_get_num_cs_end_dwords(r300);
323
324 /* Reserve CS space. */
325 if (dwords > (RADEON_MAX_CMDBUF_DWORDS - r300->cs->cdw)) {
326 r300_flush(&r300->context, RADEON_FLUSH_ASYNC, NULL);
327 }
328
329 /* Emit clear packets. */
330 if (r300->zmask_clear.dirty) {
331 r300_emit_zmask_clear(r300, r300->zmask_clear.size,
332 r300->zmask_clear.state);
333 r300->zmask_clear.dirty = FALSE;
334 }
335 if (r300->hiz_clear.dirty) {
336 r300_emit_hiz_clear(r300, r300->hiz_clear.size,
337 r300->hiz_clear.state);
338 r300->hiz_clear.dirty = FALSE;
339 }
340 } else {
341 assert(0);
342 }
343
344 /* Disable CBZB clear. */
345 if (r300->cbzb_clear) {
346 r300->cbzb_clear = FALSE;
347 hyperz->zb_depthclearvalue = hyperz_dcv;
348 r300_mark_fb_state_dirty(r300, R300_CHANGED_HYPERZ_FLAG);
349 }
350
351 /* Enable fastfill and/or hiz.
352 *
353 * If we cleared zmask/hiz, it's in use now. The Hyper-Z state update
354 * looks if zmask/hiz is in use and programs hardware accordingly. */
355 if (r300->zmask_in_use || r300->hiz_in_use) {
356 r300_mark_atom_dirty(r300, &r300->hyperz_state);
357 }
358 }
359
360 /* Clear a region of a color surface to a constant value. */
361 static void r300_clear_render_target(struct pipe_context *pipe,
362 struct pipe_surface *dst,
363 const union pipe_color_union *color,
364 unsigned dstx, unsigned dsty,
365 unsigned width, unsigned height)
366 {
367 struct r300_context *r300 = r300_context(pipe);
368
369 r300_blitter_begin(r300, R300_CLEAR_SURFACE);
370 util_blitter_clear_render_target(r300->blitter, dst, color,
371 dstx, dsty, width, height);
372 r300_blitter_end(r300);
373 }
374
375 /* Clear a region of a depth stencil surface. */
376 static void r300_clear_depth_stencil(struct pipe_context *pipe,
377 struct pipe_surface *dst,
378 unsigned clear_flags,
379 double depth,
380 unsigned stencil,
381 unsigned dstx, unsigned dsty,
382 unsigned width, unsigned height)
383 {
384 struct r300_context *r300 = r300_context(pipe);
385 struct pipe_framebuffer_state *fb =
386 (struct pipe_framebuffer_state*)r300->fb_state.state;
387
388 if (r300->zmask_in_use && !r300->locked_zbuffer) {
389 if (fb->zsbuf->texture == dst->texture) {
390 r300_decompress_zmask(r300);
391 }
392 }
393
394 /* XXX Do not decompress ZMask of the currently-set zbuffer. */
395 r300_blitter_begin(r300, R300_CLEAR_SURFACE);
396 util_blitter_clear_depth_stencil(r300->blitter, dst, clear_flags, depth, stencil,
397 dstx, dsty, width, height);
398 r300_blitter_end(r300);
399 }
400
401 void r300_decompress_zmask(struct r300_context *r300)
402 {
403 struct pipe_framebuffer_state *fb =
404 (struct pipe_framebuffer_state*)r300->fb_state.state;
405
406 if (!r300->zmask_in_use || r300->locked_zbuffer)
407 return;
408
409 r300->zmask_decompress = TRUE;
410 r300_mark_atom_dirty(r300, &r300->hyperz_state);
411
412 r300_blitter_begin(r300, R300_DECOMPRESS);
413 util_blitter_custom_clear_depth(r300->blitter, fb->width, fb->height, 0,
414 r300->dsa_decompress_zmask);
415 r300_blitter_end(r300);
416
417 r300->zmask_decompress = FALSE;
418 r300->zmask_in_use = FALSE;
419 r300_mark_atom_dirty(r300, &r300->hyperz_state);
420 }
421
422 void r300_decompress_zmask_locked_unsafe(struct r300_context *r300)
423 {
424 struct pipe_framebuffer_state fb;
425
426 memset(&fb, 0, sizeof(fb));
427 fb.width = r300->locked_zbuffer->width;
428 fb.height = r300->locked_zbuffer->height;
429 fb.zsbuf = r300->locked_zbuffer;
430
431 r300->context.set_framebuffer_state(&r300->context, &fb);
432 r300_decompress_zmask(r300);
433 }
434
435 void r300_decompress_zmask_locked(struct r300_context *r300)
436 {
437 struct pipe_framebuffer_state saved_fb;
438
439 memset(&saved_fb, 0, sizeof(saved_fb));
440 util_copy_framebuffer_state(&saved_fb, r300->fb_state.state);
441 r300_decompress_zmask_locked_unsafe(r300);
442 r300->context.set_framebuffer_state(&r300->context, &saved_fb);
443 util_unreference_framebuffer_state(&saved_fb);
444
445 pipe_surface_reference(&r300->locked_zbuffer, NULL);
446 }
447
448 bool r300_is_blit_supported(enum pipe_format format)
449 {
450 const struct util_format_description *desc =
451 util_format_description(format);
452
453 return desc->layout == UTIL_FORMAT_LAYOUT_PLAIN ||
454 desc->layout == UTIL_FORMAT_LAYOUT_S3TC ||
455 desc->layout == UTIL_FORMAT_LAYOUT_RGTC;
456 }
457
458 /* Copy a block of pixels from one surface to another. */
459 static void r300_resource_copy_region(struct pipe_context *pipe,
460 struct pipe_resource *dst,
461 unsigned dst_level,
462 unsigned dstx, unsigned dsty, unsigned dstz,
463 struct pipe_resource *src,
464 unsigned src_level,
465 const struct pipe_box *src_box)
466 {
467 struct pipe_screen *screen = pipe->screen;
468 struct r300_context *r300 = r300_context(pipe);
469 struct pipe_framebuffer_state *fb =
470 (struct pipe_framebuffer_state*)r300->fb_state.state;
471 unsigned src_width0 = r300_resource(src)->tex.width0;
472 unsigned src_height0 = r300_resource(src)->tex.height0;
473 unsigned dst_width0 = r300_resource(dst)->tex.width0;
474 unsigned dst_height0 = r300_resource(dst)->tex.height0;
475 unsigned layout;
476 struct pipe_box box, dstbox;
477 struct pipe_sampler_view src_templ, *src_view;
478 struct pipe_surface dst_templ, *dst_view;
479
480 /* Fallback for buffers. */
481 if ((dst->target == PIPE_BUFFER && src->target == PIPE_BUFFER) ||
482 !r300_is_blit_supported(dst->format)) {
483 util_resource_copy_region(pipe, dst, dst_level, dstx, dsty, dstz,
484 src, src_level, src_box);
485 return;
486 }
487
488 /* Can't read MSAA textures. */
489 if (src->nr_samples > 1 || dst->nr_samples > 1) {
490 return;
491 }
492
493 /* The code below changes the texture format so that the copy can be done
494 * on hardware. E.g. depth-stencil surfaces are copied as RGBA
495 * colorbuffers. */
496
497 util_blitter_default_dst_texture(&dst_templ, dst, dst_level, dstz);
498 util_blitter_default_src_texture(&src_templ, src, src_level);
499
500 layout = util_format_description(dst_templ.format)->layout;
501
502 /* Handle non-renderable plain formats. */
503 if (layout == UTIL_FORMAT_LAYOUT_PLAIN &&
504 (!screen->is_format_supported(screen, src_templ.format, src->target,
505 src->nr_samples,
506 PIPE_BIND_SAMPLER_VIEW) ||
507 !screen->is_format_supported(screen, dst_templ.format, dst->target,
508 dst->nr_samples,
509 PIPE_BIND_RENDER_TARGET))) {
510 switch (util_format_get_blocksize(dst_templ.format)) {
511 case 1:
512 dst_templ.format = PIPE_FORMAT_I8_UNORM;
513 break;
514 case 2:
515 dst_templ.format = PIPE_FORMAT_B4G4R4A4_UNORM;
516 break;
517 case 4:
518 dst_templ.format = PIPE_FORMAT_B8G8R8A8_UNORM;
519 break;
520 case 8:
521 dst_templ.format = PIPE_FORMAT_R16G16B16A16_UNORM;
522 break;
523 default:
524 debug_printf("r300: copy_region: Unhandled format: %s. Falling back to software.\n"
525 "r300: copy_region: Software fallback doesn't work for tiled textures.\n",
526 util_format_short_name(dst_templ.format));
527 }
528 src_templ.format = dst_templ.format;
529 }
530
531 /* Handle compressed formats. */
532 if (layout == UTIL_FORMAT_LAYOUT_S3TC ||
533 layout == UTIL_FORMAT_LAYOUT_RGTC) {
534 assert(src_templ.format == dst_templ.format);
535
536 box = *src_box;
537 src_box = &box;
538
539 dst_width0 = align(dst_width0, 4);
540 dst_height0 = align(dst_height0, 4);
541 src_width0 = align(src_width0, 4);
542 src_height0 = align(src_height0, 4);
543 box.width = align(box.width, 4);
544 box.height = align(box.height, 4);
545
546 switch (util_format_get_blocksize(dst_templ.format)) {
547 case 8:
548 /* one 4x4 pixel block has 8 bytes.
549 * we set 1 pixel = 4 bytes ===> 1 block corrensponds to 2 pixels. */
550 dst_templ.format = PIPE_FORMAT_R8G8B8A8_UNORM;
551 dst_width0 = dst_width0 / 2;
552 src_width0 = src_width0 / 2;
553 dstx /= 2;
554 box.x /= 2;
555 box.width /= 2;
556 break;
557 case 16:
558 /* one 4x4 pixel block has 16 bytes.
559 * we set 1 pixel = 4 bytes ===> 1 block corresponds to 4 pixels. */
560 dst_templ.format = PIPE_FORMAT_R8G8B8A8_UNORM;
561 break;
562 }
563 src_templ.format = dst_templ.format;
564
565 dst_height0 = dst_height0 / 4;
566 src_height0 = src_height0 / 4;
567 dsty /= 4;
568 box.y /= 4;
569 box.height /= 4;
570 }
571
572 /* Fallback for textures. */
573 if (!screen->is_format_supported(screen, dst_templ.format,
574 dst->target, dst->nr_samples,
575 PIPE_BIND_RENDER_TARGET) ||
576 !screen->is_format_supported(screen, src_templ.format,
577 src->target, src->nr_samples,
578 PIPE_BIND_SAMPLER_VIEW)) {
579 assert(0 && "this shouldn't happen, update r300_is_blit_supported");
580 util_resource_copy_region(pipe, dst, dst_level, dstx, dsty, dstz,
581 src, src_level, src_box);
582 return;
583 }
584
585 /* Decompress ZMASK. */
586 if (r300->zmask_in_use && !r300->locked_zbuffer) {
587 if (fb->zsbuf->texture == src ||
588 fb->zsbuf->texture == dst) {
589 r300_decompress_zmask(r300);
590 }
591 }
592
593 dst_view = r300_create_surface_custom(pipe, dst, &dst_templ, dst_width0, dst_height0);
594 src_view = r300_create_sampler_view_custom(pipe, src, &src_templ, src_width0, src_height0);
595
596 u_box_3d(dstx, dsty, dstz, abs(src_box->width), abs(src_box->height),
597 abs(src_box->depth), &dstbox);
598
599 r300_blitter_begin(r300, R300_COPY);
600 util_blitter_blit_generic(r300->blitter, dst_view, &dstbox,
601 src_view, src_box, src_width0, src_height0,
602 PIPE_MASK_RGBAZS, PIPE_TEX_FILTER_NEAREST, NULL,
603 FALSE);
604 r300_blitter_end(r300);
605
606 pipe_surface_reference(&dst_view, NULL);
607 pipe_sampler_view_reference(&src_view, NULL);
608 }
609
610 static boolean r300_is_simple_msaa_resolve(const struct pipe_blit_info *info)
611 {
612 unsigned dst_width = u_minify(info->dst.resource->width0, info->dst.level);
613 unsigned dst_height = u_minify(info->dst.resource->height0, info->dst.level);
614
615 return info->dst.resource->format == info->src.resource->format &&
616 info->dst.resource->format == info->dst.format &&
617 info->src.resource->format == info->src.format &&
618 !info->scissor_enable &&
619 info->mask == PIPE_MASK_RGBA &&
620 dst_width == info->src.resource->width0 &&
621 dst_height == info->src.resource->height0 &&
622 info->dst.box.x == 0 &&
623 info->dst.box.y == 0 &&
624 info->dst.box.width == dst_width &&
625 info->dst.box.height == dst_height &&
626 info->src.box.x == 0 &&
627 info->src.box.y == 0 &&
628 info->src.box.width == dst_width &&
629 info->src.box.height == dst_height &&
630 (r300_resource(info->dst.resource)->tex.microtile != RADEON_LAYOUT_LINEAR ||
631 r300_resource(info->dst.resource)->tex.macrotile[info->dst.level] != RADEON_LAYOUT_LINEAR);
632 }
633
634 static void r300_simple_msaa_resolve(struct pipe_context *pipe,
635 struct pipe_resource *dst,
636 unsigned dst_level,
637 unsigned dst_layer,
638 struct pipe_resource *src,
639 enum pipe_format format)
640 {
641 struct r300_context *r300 = r300_context(pipe);
642 struct r300_surface *srcsurf, *dstsurf;
643 struct pipe_surface surf_tmpl;
644 struct r300_aa_state *aa = (struct r300_aa_state*)r300->aa_state.state;
645
646 memset(&surf_tmpl, 0, sizeof(surf_tmpl));
647 surf_tmpl.format = format;
648 srcsurf = r300_surface(pipe->create_surface(pipe, src, &surf_tmpl));
649
650 surf_tmpl.format = format;
651 surf_tmpl.u.tex.level = dst_level;
652 surf_tmpl.u.tex.first_layer =
653 surf_tmpl.u.tex.last_layer = dst_layer;
654 dstsurf = r300_surface(pipe->create_surface(pipe, dst, &surf_tmpl));
655
656 /* COLORPITCH should contain the tiling info of the resolve buffer.
657 * The tiling of the AA buffer isn't programmable anyway. */
658 srcsurf->pitch &= ~(R300_COLOR_TILE(1) | R300_COLOR_MICROTILE(3));
659 srcsurf->pitch |= dstsurf->pitch & (R300_COLOR_TILE(1) | R300_COLOR_MICROTILE(3));
660
661 /* Enable AA resolve. */
662 aa->dest = dstsurf;
663 r300->aa_state.size = 8;
664 r300_mark_atom_dirty(r300, &r300->aa_state);
665
666 /* Resolve the surface. */
667 r300_blitter_begin(r300, R300_CLEAR_SURFACE);
668 util_blitter_custom_color(r300->blitter, &srcsurf->base, NULL);
669 r300_blitter_end(r300);
670
671 /* Disable AA resolve. */
672 aa->dest = NULL;
673 r300->aa_state.size = 4;
674 r300_mark_atom_dirty(r300, &r300->aa_state);
675
676 pipe_surface_reference((struct pipe_surface**)&srcsurf, NULL);
677 pipe_surface_reference((struct pipe_surface**)&dstsurf, NULL);
678 }
679
680 static void r300_msaa_resolve(struct pipe_context *pipe,
681 const struct pipe_blit_info *info)
682 {
683 struct r300_context *r300 = r300_context(pipe);
684 struct pipe_screen *screen = pipe->screen;
685 struct pipe_resource *tmp, templ;
686 struct pipe_blit_info blit;
687
688 assert(info->src.level == 0);
689 assert(info->src.box.z == 0);
690 assert(info->src.box.depth == 1);
691 assert(info->dst.box.depth == 1);
692
693 if (r300_is_simple_msaa_resolve(info)) {
694 r300_simple_msaa_resolve(pipe, info->dst.resource, info->dst.level,
695 info->dst.box.z, info->src.resource,
696 info->src.format);
697 return;
698 }
699
700 /* resolve into a temporary texture, then blit */
701 memset(&templ, 0, sizeof(templ));
702 templ.target = PIPE_TEXTURE_2D;
703 templ.format = info->src.resource->format;
704 templ.width0 = info->src.resource->width0;
705 templ.height0 = info->src.resource->height0;
706 templ.depth0 = 1;
707 templ.array_size = 1;
708 templ.usage = PIPE_USAGE_STATIC;
709 templ.flags = R300_RESOURCE_FORCE_MICROTILING;
710
711 tmp = screen->resource_create(screen, &templ);
712
713 /* resolve */
714 r300_simple_msaa_resolve(pipe, tmp, 0, 0, info->src.resource,
715 info->src.format);
716
717 /* blit */
718 blit = *info;
719 blit.src.resource = tmp;
720 blit.src.box.z = 0;
721
722 r300_blitter_begin(r300, R300_BLIT);
723 util_blitter_blit(r300->blitter, &blit);
724 r300_blitter_end(r300);
725
726 pipe_resource_reference(&tmp, NULL);
727 }
728
729 static void r300_blit(struct pipe_context *pipe,
730 const struct pipe_blit_info *blit)
731 {
732 struct r300_context *r300 = r300_context(pipe);
733 struct pipe_framebuffer_state *fb =
734 (struct pipe_framebuffer_state*)r300->fb_state.state;
735 struct pipe_blit_info info = *blit;
736
737 /* MSAA resolve. */
738 if (info.src.resource->nr_samples > 1 &&
739 info.dst.resource->nr_samples <= 1 &&
740 !util_format_is_depth_or_stencil(info.src.resource->format)) {
741 r300_msaa_resolve(pipe, &info);
742 return;
743 }
744
745 /* Can't read MSAA textures. */
746 if (info.src.resource->nr_samples > 1) {
747 return;
748 }
749
750 /* Blit a combined depth-stencil resource as color.
751 * S8Z24 is the only supported stencil format. */
752 if ((info.mask & PIPE_MASK_S) &&
753 info.src.format == PIPE_FORMAT_S8_UINT_Z24_UNORM &&
754 info.dst.format == PIPE_FORMAT_S8_UINT_Z24_UNORM) {
755 if (info.dst.resource->nr_samples > 1) {
756 /* Cannot do that with MSAA buffers. */
757 info.mask &= ~PIPE_MASK_S;
758 if (!(info.mask & PIPE_MASK_Z)) {
759 return;
760 }
761 } else {
762 /* Single-sample buffer. */
763 info.src.format = PIPE_FORMAT_B8G8R8A8_UNORM;
764 info.dst.format = PIPE_FORMAT_B8G8R8A8_UNORM;
765 if (info.mask & PIPE_MASK_Z) {
766 info.mask = PIPE_MASK_RGBA; /* depth+stencil */
767 } else {
768 info.mask = PIPE_MASK_B; /* stencil only */
769 }
770 }
771 }
772
773 /* Decompress ZMASK. */
774 if (r300->zmask_in_use && !r300->locked_zbuffer) {
775 if (fb->zsbuf->texture == info.src.resource ||
776 fb->zsbuf->texture == info.dst.resource) {
777 r300_decompress_zmask(r300);
778 }
779 }
780
781 r300_blitter_begin(r300, R300_BLIT);
782 util_blitter_blit(r300->blitter, &info);
783 r300_blitter_end(r300);
784 }
785
786 void r300_init_blit_functions(struct r300_context *r300)
787 {
788 r300->context.clear = r300_clear;
789 r300->context.clear_render_target = r300_clear_render_target;
790 r300->context.clear_depth_stencil = r300_clear_depth_stencil;
791 r300->context.resource_copy_region = r300_resource_copy_region;
792 r300->context.blit = r300_blit;
793 }