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