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