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