r600g,radeonsi: don't use hardware MSAA resolve if dst is fast-cleared
[mesa.git] / src / gallium / drivers / r600 / r600_blit.c
1 /*
2 * Copyright 2010 Jerome Glisse <glisse@freedesktop.org>
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 "r600_pipe.h"
24 #include "util/u_surface.h"
25 #include "util/u_blitter.h"
26 #include "util/u_format.h"
27 #include "evergreend.h"
28
29 enum r600_blitter_op /* bitmask */
30 {
31 R600_SAVE_FRAGMENT_STATE = 1,
32 R600_SAVE_TEXTURES = 2,
33 R600_SAVE_FRAMEBUFFER = 4,
34 R600_DISABLE_RENDER_COND = 8,
35
36 R600_CLEAR = R600_SAVE_FRAGMENT_STATE,
37
38 R600_CLEAR_SURFACE = R600_SAVE_FRAGMENT_STATE | R600_SAVE_FRAMEBUFFER,
39
40 R600_COPY_BUFFER = R600_DISABLE_RENDER_COND,
41
42 R600_COPY_TEXTURE = R600_SAVE_FRAGMENT_STATE | R600_SAVE_FRAMEBUFFER | R600_SAVE_TEXTURES |
43 R600_DISABLE_RENDER_COND,
44
45 R600_BLIT = R600_SAVE_FRAGMENT_STATE | R600_SAVE_FRAMEBUFFER | R600_SAVE_TEXTURES,
46
47 R600_DECOMPRESS = R600_SAVE_FRAGMENT_STATE | R600_SAVE_FRAMEBUFFER | R600_DISABLE_RENDER_COND,
48
49 R600_COLOR_RESOLVE = R600_SAVE_FRAGMENT_STATE | R600_SAVE_FRAMEBUFFER
50 };
51
52 static void r600_blitter_begin(struct pipe_context *ctx, enum r600_blitter_op op)
53 {
54 struct r600_context *rctx = (struct r600_context *)ctx;
55
56 r600_suspend_nontimer_queries(&rctx->b);
57
58 util_blitter_save_vertex_buffer_slot(rctx->blitter, rctx->vertex_buffer_state.vb);
59 util_blitter_save_vertex_elements(rctx->blitter, rctx->vertex_fetch_shader.cso);
60 util_blitter_save_vertex_shader(rctx->blitter, rctx->vs_shader);
61 util_blitter_save_geometry_shader(rctx->blitter, rctx->gs_shader);
62 util_blitter_save_so_targets(rctx->blitter, rctx->b.streamout.num_targets,
63 (struct pipe_stream_output_target**)rctx->b.streamout.targets);
64 util_blitter_save_rasterizer(rctx->blitter, rctx->rasterizer_state.cso);
65
66 if (op & R600_SAVE_FRAGMENT_STATE) {
67 util_blitter_save_viewport(rctx->blitter, &rctx->viewport[0].state);
68 util_blitter_save_scissor(rctx->blitter, &rctx->scissor[0].scissor);
69 util_blitter_save_fragment_shader(rctx->blitter, rctx->ps_shader);
70 util_blitter_save_blend(rctx->blitter, rctx->blend_state.cso);
71 util_blitter_save_depth_stencil_alpha(rctx->blitter, rctx->dsa_state.cso);
72 util_blitter_save_stencil_ref(rctx->blitter, &rctx->stencil_ref.pipe_state);
73 util_blitter_save_sample_mask(rctx->blitter, rctx->sample_mask.sample_mask);
74 }
75
76 if (op & R600_SAVE_FRAMEBUFFER)
77 util_blitter_save_framebuffer(rctx->blitter, &rctx->framebuffer.state);
78
79 if (op & R600_SAVE_TEXTURES) {
80 util_blitter_save_fragment_sampler_states(
81 rctx->blitter, util_last_bit(rctx->samplers[PIPE_SHADER_FRAGMENT].states.enabled_mask),
82 (void**)rctx->samplers[PIPE_SHADER_FRAGMENT].states.states);
83
84 util_blitter_save_fragment_sampler_views(
85 rctx->blitter, util_last_bit(rctx->samplers[PIPE_SHADER_FRAGMENT].views.enabled_mask),
86 (struct pipe_sampler_view**)rctx->samplers[PIPE_SHADER_FRAGMENT].views.views);
87 }
88
89 if ((op & R600_DISABLE_RENDER_COND) && rctx->b.current_render_cond) {
90 util_blitter_save_render_condition(rctx->blitter,
91 rctx->b.current_render_cond,
92 rctx->b.current_render_cond_cond,
93 rctx->b.current_render_cond_mode);
94 }
95 }
96
97 static void r600_blitter_end(struct pipe_context *ctx)
98 {
99 struct r600_context *rctx = (struct r600_context *)ctx;
100 r600_resume_nontimer_queries(&rctx->b);
101 }
102
103 static unsigned u_max_sample(struct pipe_resource *r)
104 {
105 return r->nr_samples ? r->nr_samples - 1 : 0;
106 }
107
108 static void r600_blit_decompress_depth(struct pipe_context *ctx,
109 struct r600_texture *texture,
110 struct r600_texture *staging,
111 unsigned first_level, unsigned last_level,
112 unsigned first_layer, unsigned last_layer,
113 unsigned first_sample, unsigned last_sample)
114 {
115 struct r600_context *rctx = (struct r600_context *)ctx;
116 unsigned layer, level, sample, checked_last_layer, max_layer, max_sample;
117 struct r600_texture *flushed_depth_texture = staging ?
118 staging : texture->flushed_depth_texture;
119 const struct util_format_description *desc =
120 util_format_description(texture->resource.b.b.format);
121 float depth;
122
123 if (!staging && !texture->dirty_level_mask)
124 return;
125
126 max_sample = u_max_sample(&texture->resource.b.b);
127
128 /* XXX Decompressing MSAA depth textures is broken on R6xx.
129 * There is also a hardlock if CMASK and FMASK are not present.
130 * Just skip this until we find out how to fix it. */
131 if (rctx->b.chip_class == R600 && max_sample > 0) {
132 texture->dirty_level_mask = 0;
133 return;
134 }
135
136 if (rctx->b.family == CHIP_RV610 || rctx->b.family == CHIP_RV630 ||
137 rctx->b.family == CHIP_RV620 || rctx->b.family == CHIP_RV635)
138 depth = 0.0f;
139 else
140 depth = 1.0f;
141
142 /* Enable decompression in DB_RENDER_CONTROL */
143 rctx->db_misc_state.flush_depthstencil_through_cb = true;
144 rctx->db_misc_state.copy_depth = util_format_has_depth(desc);
145 rctx->db_misc_state.copy_stencil = util_format_has_stencil(desc);
146 rctx->db_misc_state.copy_sample = first_sample;
147 rctx->db_misc_state.atom.dirty = true;
148
149 for (level = first_level; level <= last_level; level++) {
150 if (!staging && !(texture->dirty_level_mask & (1 << level)))
151 continue;
152
153 /* The smaller the mipmap level, the less layers there are
154 * as far as 3D textures are concerned. */
155 max_layer = util_max_layer(&texture->resource.b.b, level);
156 checked_last_layer = last_layer < max_layer ? last_layer : max_layer;
157
158 for (layer = first_layer; layer <= checked_last_layer; layer++) {
159 for (sample = first_sample; sample <= last_sample; sample++) {
160 struct pipe_surface *zsurf, *cbsurf, surf_tmpl;
161
162 if (sample != rctx->db_misc_state.copy_sample) {
163 rctx->db_misc_state.copy_sample = sample;
164 rctx->db_misc_state.atom.dirty = true;
165 }
166
167 surf_tmpl.format = texture->resource.b.b.format;
168 surf_tmpl.u.tex.level = level;
169 surf_tmpl.u.tex.first_layer = layer;
170 surf_tmpl.u.tex.last_layer = layer;
171
172 zsurf = ctx->create_surface(ctx, &texture->resource.b.b, &surf_tmpl);
173
174 surf_tmpl.format = flushed_depth_texture->resource.b.b.format;
175 cbsurf = ctx->create_surface(ctx,
176 &flushed_depth_texture->resource.b.b, &surf_tmpl);
177
178 r600_blitter_begin(ctx, R600_DECOMPRESS);
179 util_blitter_custom_depth_stencil(rctx->blitter, zsurf, cbsurf, 1 << sample,
180 rctx->custom_dsa_flush, depth);
181 r600_blitter_end(ctx);
182
183 pipe_surface_reference(&zsurf, NULL);
184 pipe_surface_reference(&cbsurf, NULL);
185 }
186 }
187
188 /* The texture will always be dirty if some layers or samples aren't flushed.
189 * I don't think this case occurs often though. */
190 if (!staging &&
191 first_layer == 0 && last_layer == max_layer &&
192 first_sample == 0 && last_sample == max_sample) {
193 texture->dirty_level_mask &= ~(1 << level);
194 }
195 }
196
197 /* reenable compression in DB_RENDER_CONTROL */
198 rctx->db_misc_state.flush_depthstencil_through_cb = false;
199 rctx->db_misc_state.atom.dirty = true;
200 }
201
202 static void r600_blit_decompress_depth_in_place(struct r600_context *rctx,
203 struct r600_texture *texture,
204 unsigned first_level, unsigned last_level,
205 unsigned first_layer, unsigned last_layer)
206 {
207 struct pipe_surface *zsurf, surf_tmpl = {{0}};
208 unsigned layer, max_layer, checked_last_layer, level;
209
210 /* Enable decompression in DB_RENDER_CONTROL */
211 rctx->db_misc_state.flush_depthstencil_in_place = true;
212 rctx->db_misc_state.atom.dirty = true;
213
214 surf_tmpl.format = texture->resource.b.b.format;
215
216 for (level = first_level; level <= last_level; level++) {
217 if (!(texture->dirty_level_mask & (1 << level)))
218 continue;
219
220 surf_tmpl.u.tex.level = level;
221
222 /* The smaller the mipmap level, the less layers there are
223 * as far as 3D textures are concerned. */
224 max_layer = util_max_layer(&texture->resource.b.b, level);
225 checked_last_layer = last_layer < max_layer ? last_layer : max_layer;
226
227 for (layer = first_layer; layer <= checked_last_layer; layer++) {
228 surf_tmpl.u.tex.first_layer = layer;
229 surf_tmpl.u.tex.last_layer = layer;
230
231 zsurf = rctx->b.b.create_surface(&rctx->b.b, &texture->resource.b.b, &surf_tmpl);
232
233 r600_blitter_begin(&rctx->b.b, R600_DECOMPRESS);
234 util_blitter_custom_depth_stencil(rctx->blitter, zsurf, NULL, ~0,
235 rctx->custom_dsa_flush, 1.0f);
236 r600_blitter_end(&rctx->b.b);
237
238 pipe_surface_reference(&zsurf, NULL);
239 }
240
241 /* The texture will always be dirty if some layers or samples aren't flushed.
242 * I don't think this case occurs often though. */
243 if (first_layer == 0 && last_layer == max_layer) {
244 texture->dirty_level_mask &= ~(1 << level);
245 }
246 }
247
248 /* Disable decompression in DB_RENDER_CONTROL */
249 rctx->db_misc_state.flush_depthstencil_in_place = false;
250 rctx->db_misc_state.atom.dirty = true;
251 }
252
253 void r600_decompress_depth_textures(struct r600_context *rctx,
254 struct r600_samplerview_state *textures)
255 {
256 unsigned i;
257 unsigned depth_texture_mask = textures->compressed_depthtex_mask;
258
259 while (depth_texture_mask) {
260 struct pipe_sampler_view *view;
261 struct r600_texture *tex;
262
263 i = u_bit_scan(&depth_texture_mask);
264
265 view = &textures->views[i]->base;
266 assert(view);
267
268 tex = (struct r600_texture *)view->texture;
269 assert(tex->is_depth && !tex->is_flushing_texture);
270
271 if (rctx->b.chip_class >= EVERGREEN ||
272 r600_can_read_depth(tex)) {
273 r600_blit_decompress_depth_in_place(rctx, tex,
274 view->u.tex.first_level, view->u.tex.last_level,
275 0, util_max_layer(&tex->resource.b.b, view->u.tex.first_level));
276 } else {
277 r600_blit_decompress_depth(&rctx->b.b, tex, NULL,
278 view->u.tex.first_level, view->u.tex.last_level,
279 0, util_max_layer(&tex->resource.b.b, view->u.tex.first_level),
280 0, u_max_sample(&tex->resource.b.b));
281 }
282 }
283 }
284
285 static void r600_blit_decompress_color(struct pipe_context *ctx,
286 struct r600_texture *rtex,
287 unsigned first_level, unsigned last_level,
288 unsigned first_layer, unsigned last_layer)
289 {
290 struct r600_context *rctx = (struct r600_context *)ctx;
291 unsigned layer, level, checked_last_layer, max_layer;
292
293 if (!rtex->dirty_level_mask)
294 return;
295
296 for (level = first_level; level <= last_level; level++) {
297 if (!(rtex->dirty_level_mask & (1 << level)))
298 continue;
299
300 /* The smaller the mipmap level, the less layers there are
301 * as far as 3D textures are concerned. */
302 max_layer = util_max_layer(&rtex->resource.b.b, level);
303 checked_last_layer = last_layer < max_layer ? last_layer : max_layer;
304
305 for (layer = first_layer; layer <= checked_last_layer; layer++) {
306 struct pipe_surface *cbsurf, surf_tmpl;
307
308 surf_tmpl.format = rtex->resource.b.b.format;
309 surf_tmpl.u.tex.level = level;
310 surf_tmpl.u.tex.first_layer = layer;
311 surf_tmpl.u.tex.last_layer = layer;
312 cbsurf = ctx->create_surface(ctx, &rtex->resource.b.b, &surf_tmpl);
313
314 r600_blitter_begin(ctx, R600_DECOMPRESS);
315 util_blitter_custom_color(rctx->blitter, cbsurf,
316 rtex->fmask.size ? rctx->custom_blend_decompress : rctx->custom_blend_fastclear);
317 r600_blitter_end(ctx);
318
319 pipe_surface_reference(&cbsurf, NULL);
320 }
321
322 /* The texture will always be dirty if some layers aren't flushed.
323 * I don't think this case occurs often though. */
324 if (first_layer == 0 && last_layer == max_layer) {
325 rtex->dirty_level_mask &= ~(1 << level);
326 }
327 }
328 }
329
330 void r600_decompress_color_textures(struct r600_context *rctx,
331 struct r600_samplerview_state *textures)
332 {
333 unsigned i;
334 unsigned mask = textures->compressed_colortex_mask;
335
336 while (mask) {
337 struct pipe_sampler_view *view;
338 struct r600_texture *tex;
339
340 i = u_bit_scan(&mask);
341
342 view = &textures->views[i]->base;
343 assert(view);
344
345 tex = (struct r600_texture *)view->texture;
346 assert(tex->cmask.size);
347
348 r600_blit_decompress_color(&rctx->b.b, tex,
349 view->u.tex.first_level, view->u.tex.last_level,
350 0, util_max_layer(&tex->resource.b.b, view->u.tex.first_level));
351 }
352 }
353
354 /* Helper for decompressing a portion of a color or depth resource before
355 * blitting if any decompression is needed.
356 * The driver doesn't decompress resources automatically while u_blitter is
357 * rendering. */
358 static bool r600_decompress_subresource(struct pipe_context *ctx,
359 struct pipe_resource *tex,
360 unsigned level,
361 unsigned first_layer, unsigned last_layer)
362 {
363 struct r600_context *rctx = (struct r600_context *)ctx;
364 struct r600_texture *rtex = (struct r600_texture*)tex;
365
366 if (rtex->is_depth && !rtex->is_flushing_texture) {
367 if (rctx->b.chip_class >= EVERGREEN ||
368 r600_can_read_depth(rtex)) {
369 r600_blit_decompress_depth_in_place(rctx, rtex,
370 level, level,
371 first_layer, last_layer);
372 } else {
373 if (!r600_init_flushed_depth_texture(ctx, tex, NULL))
374 return false; /* error */
375
376 r600_blit_decompress_depth(ctx, rtex, NULL,
377 level, level,
378 first_layer, last_layer,
379 0, u_max_sample(tex));
380 }
381 } else if (rtex->cmask.size) {
382 r600_blit_decompress_color(ctx, rtex, level, level,
383 first_layer, last_layer);
384 }
385 return true;
386 }
387
388 static void r600_clear(struct pipe_context *ctx, unsigned buffers,
389 const union pipe_color_union *color,
390 double depth, unsigned stencil)
391 {
392 struct r600_context *rctx = (struct r600_context *)ctx;
393 struct pipe_framebuffer_state *fb = &rctx->framebuffer.state;
394
395 if (buffers & PIPE_CLEAR_COLOR && rctx->b.chip_class >= EVERGREEN) {
396 evergreen_do_fast_color_clear(&rctx->b, fb, &rctx->framebuffer.atom,
397 &buffers, color);
398 }
399
400 if (buffers & PIPE_CLEAR_COLOR) {
401 int i;
402
403 /* These buffers cannot use fast clear, make sure to disable expansion. */
404 for (i = 0; i < fb->nr_cbufs; i++) {
405 struct r600_texture *tex;
406
407 /* If not clearing this buffer, skip. */
408 if (!(buffers & (PIPE_CLEAR_COLOR0 << i)))
409 continue;
410
411 if (!fb->cbufs[i])
412 continue;
413
414 tex = (struct r600_texture *)fb->cbufs[i]->texture;
415 if (tex->fmask.size == 0)
416 tex->dirty_level_mask &= ~(1 << fb->cbufs[i]->u.tex.level);
417 }
418 }
419
420 /* if hyperz enabled just clear hyperz */
421 if (fb->zsbuf && (buffers & PIPE_CLEAR_DEPTH)) {
422 struct r600_texture *rtex;
423 unsigned level = fb->zsbuf->u.tex.level;
424
425 rtex = (struct r600_texture*)fb->zsbuf->texture;
426
427 /* We can't use hyperz fast clear if each slice of a texture
428 * array are clear to different value. To simplify code just
429 * disable fast clear for texture array.
430 */
431 /* Only use htile for first level */
432 if (rtex->htile_buffer && !level && rtex->surface.array_size == 1) {
433 if (rtex->depth_clear_value != depth) {
434 rtex->depth_clear_value = depth;
435 rctx->db_state.atom.dirty = true;
436 }
437 rctx->db_misc_state.htile_clear = true;
438 rctx->db_misc_state.atom.dirty = true;
439 }
440 }
441
442 r600_blitter_begin(ctx, R600_CLEAR);
443 util_blitter_clear(rctx->blitter, fb->width, fb->height, 1,
444 buffers, color, depth, stencil);
445 r600_blitter_end(ctx);
446
447 /* disable fast clear */
448 if (rctx->db_misc_state.htile_clear) {
449 rctx->db_misc_state.htile_clear = false;
450 rctx->db_misc_state.atom.dirty = true;
451 }
452 }
453
454 static void r600_clear_render_target(struct pipe_context *ctx,
455 struct pipe_surface *dst,
456 const union pipe_color_union *color,
457 unsigned dstx, unsigned dsty,
458 unsigned width, unsigned height)
459 {
460 struct r600_context *rctx = (struct r600_context *)ctx;
461
462 r600_blitter_begin(ctx, R600_CLEAR_SURFACE);
463 util_blitter_clear_render_target(rctx->blitter, dst, color,
464 dstx, dsty, width, height);
465 r600_blitter_end(ctx);
466 }
467
468 static void r600_clear_depth_stencil(struct pipe_context *ctx,
469 struct pipe_surface *dst,
470 unsigned clear_flags,
471 double depth,
472 unsigned stencil,
473 unsigned dstx, unsigned dsty,
474 unsigned width, unsigned height)
475 {
476 struct r600_context *rctx = (struct r600_context *)ctx;
477
478 r600_blitter_begin(ctx, R600_CLEAR_SURFACE);
479 util_blitter_clear_depth_stencil(rctx->blitter, dst, clear_flags, depth, stencil,
480 dstx, dsty, width, height);
481 r600_blitter_end(ctx);
482 }
483
484 static void r600_copy_buffer(struct pipe_context *ctx, struct pipe_resource *dst, unsigned dstx,
485 struct pipe_resource *src, const struct pipe_box *src_box)
486 {
487 struct r600_context *rctx = (struct r600_context*)ctx;
488
489 if (rctx->screen->b.has_cp_dma) {
490 r600_cp_dma_copy_buffer(rctx, dst, dstx, src, src_box->x, src_box->width);
491 }
492 else if (rctx->screen->b.has_streamout &&
493 /* Require 4-byte alignment. */
494 dstx % 4 == 0 && src_box->x % 4 == 0 && src_box->width % 4 == 0) {
495
496 r600_blitter_begin(ctx, R600_COPY_BUFFER);
497 util_blitter_copy_buffer(rctx->blitter, dst, dstx, src, src_box->x, src_box->width);
498 r600_blitter_end(ctx);
499 } else {
500 util_resource_copy_region(ctx, dst, 0, dstx, 0, 0, src, 0, src_box);
501 }
502
503 /* The index buffer (VGT) doesn't seem to see the result of the copying.
504 * Can we somehow flush the index buffer cache? Starting a new IB seems
505 * to do the trick. */
506 if (rctx->b.chip_class <= R700)
507 rctx->b.rings.gfx.flush(ctx, RADEON_FLUSH_ASYNC, NULL);
508 }
509
510 /**
511 * Global buffers are not really resources, they are are actually offsets
512 * into a single global resource (r600_screen::global_pool). The means
513 * they don't have their own cs_buf handle, so they cannot be passed
514 * to r600_copy_buffer() and must be handled separately.
515 *
516 * XXX: It should be possible to implement this function using
517 * r600_copy_buffer() by passing the memory_pool resource as both src
518 * and dst and updating dstx and src_box to point to the correct offsets.
519 * This would likely perform better than the current implementation.
520 */
521 static void r600_copy_global_buffer(struct pipe_context *ctx,
522 struct pipe_resource *dst, unsigned
523 dstx, struct pipe_resource *src,
524 const struct pipe_box *src_box)
525 {
526 struct pipe_box dst_box; struct pipe_transfer *src_pxfer,
527 *dst_pxfer;
528
529 u_box_1d(dstx, src_box->width, &dst_box);
530 void *src_ptr = ctx->transfer_map(ctx, src, 0, PIPE_TRANSFER_READ,
531 src_box, &src_pxfer);
532 void *dst_ptr = ctx->transfer_map(ctx, dst, 0, PIPE_TRANSFER_WRITE,
533 &dst_box, &dst_pxfer);
534 memcpy(dst_ptr, src_ptr, src_box->width);
535
536 ctx->transfer_unmap(ctx, src_pxfer);
537 ctx->transfer_unmap(ctx, dst_pxfer);
538 }
539
540 static void r600_clear_buffer(struct pipe_context *ctx, struct pipe_resource *dst,
541 unsigned offset, unsigned size, unsigned value)
542 {
543 struct r600_context *rctx = (struct r600_context*)ctx;
544
545 if (rctx->screen->b.has_cp_dma &&
546 rctx->b.chip_class >= EVERGREEN &&
547 offset % 4 == 0 && size % 4 == 0) {
548 evergreen_cp_dma_clear_buffer(rctx, dst, offset, size, value);
549 } else if (rctx->screen->b.has_streamout && offset % 4 == 0 && size % 4 == 0) {
550 union pipe_color_union clear_value;
551 clear_value.ui[0] = value;
552
553 r600_blitter_begin(ctx, R600_DISABLE_RENDER_COND);
554 util_blitter_clear_buffer(rctx->blitter, dst, offset, size,
555 1, &clear_value);
556 r600_blitter_end(ctx);
557 } else {
558 uint32_t *map = r600_buffer_map_sync_with_rings(&rctx->b, r600_resource(dst),
559 PIPE_TRANSFER_WRITE);
560 size /= 4;
561 for (unsigned i = 0; i < size; i++)
562 *map++ = value;
563 }
564 }
565
566 static bool util_format_is_subsampled_2x1_32bpp(enum pipe_format format)
567 {
568 const struct util_format_description *desc = util_format_description(format);
569
570 return desc->layout == UTIL_FORMAT_LAYOUT_SUBSAMPLED &&
571 desc->block.width == 2 &&
572 desc->block.height == 1 &&
573 desc->block.bits == 32;
574 }
575
576 static void r600_resource_copy_region(struct pipe_context *ctx,
577 struct pipe_resource *dst,
578 unsigned dst_level,
579 unsigned dstx, unsigned dsty, unsigned dstz,
580 struct pipe_resource *src,
581 unsigned src_level,
582 const struct pipe_box *src_box)
583 {
584 struct r600_context *rctx = (struct r600_context *)ctx;
585 struct pipe_surface *dst_view, dst_templ;
586 struct pipe_sampler_view src_templ, *src_view;
587 unsigned dst_width, dst_height, src_width0, src_height0, src_widthFL, src_heightFL;
588 unsigned src_force_level = 0;
589 struct pipe_box sbox, dstbox;
590
591 /* Handle buffers first. */
592 if (dst->target == PIPE_BUFFER && src->target == PIPE_BUFFER) {
593 if ((src->bind & PIPE_BIND_GLOBAL) ||
594 (dst->bind & PIPE_BIND_GLOBAL)) {
595 r600_copy_global_buffer(ctx, dst, dstx, src, src_box);
596 } else {
597 r600_copy_buffer(ctx, dst, dstx, src, src_box);
598 }
599 return;
600 }
601
602 assert(u_max_sample(dst) == u_max_sample(src));
603
604 /* The driver doesn't decompress resources automatically while
605 * u_blitter is rendering. */
606 if (!r600_decompress_subresource(ctx, src, src_level,
607 src_box->z, src_box->z + src_box->depth - 1)) {
608 return; /* error */
609 }
610
611 dst_width = u_minify(dst->width0, dst_level);
612 dst_height = u_minify(dst->height0, dst_level);
613 src_width0 = src->width0;
614 src_height0 = src->height0;
615 src_widthFL = u_minify(src->width0, src_level);
616 src_heightFL = u_minify(src->height0, src_level);
617
618 util_blitter_default_dst_texture(&dst_templ, dst, dst_level, dstz);
619 util_blitter_default_src_texture(&src_templ, src, src_level);
620
621 if (util_format_is_compressed(src->format)) {
622 unsigned blocksize = util_format_get_blocksize(src->format);
623
624 if (blocksize == 8)
625 src_templ.format = PIPE_FORMAT_R16G16B16A16_UINT; /* 64-bit block */
626 else
627 src_templ.format = PIPE_FORMAT_R32G32B32A32_UINT; /* 128-bit block */
628 dst_templ.format = src_templ.format;
629
630 dst_width = util_format_get_nblocksx(dst->format, dst_width);
631 dst_height = util_format_get_nblocksy(dst->format, dst_height);
632 src_width0 = util_format_get_nblocksx(src->format, src_width0);
633 src_height0 = util_format_get_nblocksy(src->format, src_height0);
634 src_widthFL = util_format_get_nblocksx(src->format, src_widthFL);
635 src_heightFL = util_format_get_nblocksy(src->format, src_heightFL);
636
637 dstx = util_format_get_nblocksx(dst->format, dstx);
638 dsty = util_format_get_nblocksy(dst->format, dsty);
639
640 sbox.x = util_format_get_nblocksx(src->format, src_box->x);
641 sbox.y = util_format_get_nblocksy(src->format, src_box->y);
642 sbox.z = src_box->z;
643 sbox.width = util_format_get_nblocksx(src->format, src_box->width);
644 sbox.height = util_format_get_nblocksy(src->format, src_box->height);
645 sbox.depth = src_box->depth;
646 src_box = &sbox;
647
648 src_force_level = src_level;
649 } else if (!util_blitter_is_copy_supported(rctx->blitter, dst, src)) {
650 if (util_format_is_subsampled_2x1_32bpp(src->format)) {
651
652 src_templ.format = PIPE_FORMAT_R8G8B8A8_UINT;
653 dst_templ.format = PIPE_FORMAT_R8G8B8A8_UINT;
654
655 dst_width = util_format_get_nblocksx(dst->format, dst_width);
656 src_width0 = util_format_get_nblocksx(src->format, src_width0);
657 src_widthFL = util_format_get_nblocksx(src->format, src_widthFL);
658
659 dstx = util_format_get_nblocksx(dst->format, dstx);
660
661 sbox = *src_box;
662 sbox.x = util_format_get_nblocksx(src->format, src_box->x);
663 sbox.width = util_format_get_nblocksx(src->format, src_box->width);
664 src_box = &sbox;
665 } else {
666 unsigned blocksize = util_format_get_blocksize(src->format);
667
668 switch (blocksize) {
669 case 1:
670 dst_templ.format = PIPE_FORMAT_R8_UNORM;
671 src_templ.format = PIPE_FORMAT_R8_UNORM;
672 break;
673 case 2:
674 dst_templ.format = PIPE_FORMAT_R8G8_UNORM;
675 src_templ.format = PIPE_FORMAT_R8G8_UNORM;
676 break;
677 case 4:
678 dst_templ.format = PIPE_FORMAT_R8G8B8A8_UNORM;
679 src_templ.format = PIPE_FORMAT_R8G8B8A8_UNORM;
680 break;
681 case 8:
682 dst_templ.format = PIPE_FORMAT_R16G16B16A16_UINT;
683 src_templ.format = PIPE_FORMAT_R16G16B16A16_UINT;
684 break;
685 case 16:
686 dst_templ.format = PIPE_FORMAT_R32G32B32A32_UINT;
687 src_templ.format = PIPE_FORMAT_R32G32B32A32_UINT;
688 break;
689 default:
690 fprintf(stderr, "Unhandled format %s with blocksize %u\n",
691 util_format_short_name(src->format), blocksize);
692 assert(0);
693 }
694 }
695 }
696
697 dst_view = r600_create_surface_custom(ctx, dst, &dst_templ, dst_width, dst_height);
698
699 if (rctx->b.chip_class >= EVERGREEN) {
700 src_view = evergreen_create_sampler_view_custom(ctx, src, &src_templ,
701 src_width0, src_height0,
702 src_force_level);
703 } else {
704 src_view = r600_create_sampler_view_custom(ctx, src, &src_templ,
705 src_widthFL, src_heightFL);
706 }
707
708 u_box_3d(dstx, dsty, dstz, abs(src_box->width), abs(src_box->height),
709 abs(src_box->depth), &dstbox);
710
711 /* Copy. */
712 r600_blitter_begin(ctx, R600_COPY_TEXTURE);
713 util_blitter_blit_generic(rctx->blitter, dst_view, &dstbox,
714 src_view, src_box, src_width0, src_height0,
715 PIPE_MASK_RGBAZS, PIPE_TEX_FILTER_NEAREST, NULL);
716 r600_blitter_end(ctx);
717
718 pipe_surface_reference(&dst_view, NULL);
719 pipe_sampler_view_reference(&src_view, NULL);
720 }
721
722 /* For MSAA integer resolving to work, we change the format to NORM using this function. */
723 static enum pipe_format int_to_norm_format(enum pipe_format format)
724 {
725 switch (format) {
726 #define REPLACE_FORMAT_SIGN(format,sign) \
727 case PIPE_FORMAT_##format##_##sign##INT: \
728 return PIPE_FORMAT_##format##_##sign##NORM
729 #define REPLACE_FORMAT(format) \
730 REPLACE_FORMAT_SIGN(format, U); \
731 REPLACE_FORMAT_SIGN(format, S)
732
733 REPLACE_FORMAT_SIGN(B10G10R10A2, U);
734 REPLACE_FORMAT(R8);
735 REPLACE_FORMAT(R8G8);
736 REPLACE_FORMAT(R8G8B8X8);
737 REPLACE_FORMAT(R8G8B8A8);
738 REPLACE_FORMAT(A8);
739 REPLACE_FORMAT(I8);
740 REPLACE_FORMAT(L8);
741 REPLACE_FORMAT(L8A8);
742 REPLACE_FORMAT(R16);
743 REPLACE_FORMAT(R16G16);
744 REPLACE_FORMAT(R16G16B16X16);
745 REPLACE_FORMAT(R16G16B16A16);
746 REPLACE_FORMAT(A16);
747 REPLACE_FORMAT(I16);
748 REPLACE_FORMAT(L16);
749 REPLACE_FORMAT(L16A16);
750
751 #undef REPLACE_FORMAT
752 #undef REPLACE_FORMAT_SIGN
753 default:
754 return format;
755 }
756 }
757
758 static bool do_hardware_msaa_resolve(struct pipe_context *ctx,
759 const struct pipe_blit_info *info)
760 {
761 struct r600_context *rctx = (struct r600_context*)ctx;
762 struct r600_texture *dst = (struct r600_texture*)info->dst.resource;
763 unsigned dst_width = u_minify(info->dst.resource->width0, info->dst.level);
764 unsigned dst_height = u_minify(info->dst.resource->height0, info->dst.level);
765 enum pipe_format format = int_to_norm_format(info->dst.format);
766 unsigned sample_mask =
767 rctx->b.chip_class == CAYMAN ? ~0 :
768 ((1ull << MAX2(1, info->src.resource->nr_samples)) - 1);
769
770 if (info->src.resource->nr_samples > 1 &&
771 info->dst.resource->nr_samples <= 1 &&
772 util_max_layer(info->src.resource, 0) == 0 &&
773 util_max_layer(info->dst.resource, info->dst.level) == 0 &&
774 info->dst.format == info->src.format &&
775 !util_format_is_pure_integer(format) &&
776 !util_format_is_depth_or_stencil(format) &&
777 !info->scissor_enable &&
778 (info->mask & PIPE_MASK_RGBA) == PIPE_MASK_RGBA &&
779 dst_width == info->src.resource->width0 &&
780 dst_height == info->src.resource->height0 &&
781 info->dst.box.x == 0 &&
782 info->dst.box.y == 0 &&
783 info->dst.box.width == dst_width &&
784 info->dst.box.height == dst_height &&
785 info->dst.box.depth == 1 &&
786 info->src.box.x == 0 &&
787 info->src.box.y == 0 &&
788 info->src.box.width == dst_width &&
789 info->src.box.height == dst_height &&
790 info->src.box.depth == 1 &&
791 dst->surface.level[info->dst.level].mode >= RADEON_SURF_MODE_1D &&
792 (!dst->cmask.size || !dst->dirty_level_mask) /* dst cannot be fast-cleared */) {
793 r600_blitter_begin(ctx, R600_COLOR_RESOLVE |
794 (info->render_condition_enable ? 0 : R600_DISABLE_RENDER_COND));
795 util_blitter_custom_resolve_color(rctx->blitter,
796 info->dst.resource, info->dst.level,
797 info->dst.box.z,
798 info->src.resource, info->src.box.z,
799 sample_mask, rctx->custom_blend_resolve,
800 format);
801 r600_blitter_end(ctx);
802 return true;
803 }
804 return false;
805 }
806
807 static void r600_blit(struct pipe_context *ctx,
808 const struct pipe_blit_info *info)
809 {
810 struct r600_context *rctx = (struct r600_context*)ctx;
811
812 if (do_hardware_msaa_resolve(ctx, info)) {
813 return;
814 }
815
816 assert(util_blitter_is_blit_supported(rctx->blitter, info));
817
818 /* The driver doesn't decompress resources automatically while
819 * u_blitter is rendering. */
820 if (!r600_decompress_subresource(ctx, info->src.resource, info->src.level,
821 info->src.box.z,
822 info->src.box.z + info->src.box.depth - 1)) {
823 return; /* error */
824 }
825
826 r600_blitter_begin(ctx, R600_BLIT |
827 (info->render_condition_enable ? 0 : R600_DISABLE_RENDER_COND));
828 util_blitter_blit(rctx->blitter, info);
829 r600_blitter_end(ctx);
830 }
831
832 static void r600_flush_resource(struct pipe_context *ctx,
833 struct pipe_resource *res)
834 {
835 struct r600_texture *rtex = (struct r600_texture*)res;
836
837 assert(res->target != PIPE_BUFFER);
838
839 if (!rtex->is_depth && rtex->cmask.size) {
840 r600_blit_decompress_color(ctx, rtex, 0, res->last_level,
841 0, res->array_size - 1);
842 }
843 }
844
845 void r600_init_blit_functions(struct r600_context *rctx)
846 {
847 rctx->b.b.clear = r600_clear;
848 rctx->b.b.clear_render_target = r600_clear_render_target;
849 rctx->b.b.clear_depth_stencil = r600_clear_depth_stencil;
850 rctx->b.b.resource_copy_region = r600_resource_copy_region;
851 rctx->b.b.blit = r600_blit;
852 rctx->b.b.flush_resource = r600_flush_resource;
853 rctx->b.clear_buffer = r600_clear_buffer;
854 rctx->b.blit_decompress_depth = r600_blit_decompress_depth;
855 }