r600g,radeonsi: consolidate create_surface and surface_destroy
[mesa.git] / src / gallium / drivers / radeonsi / si_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
24 #include "si_pipe.h"
25 #include "util/u_blitter.h"
26 #include "util/u_format.h"
27
28 enum si_blitter_op /* bitmask */
29 {
30 SI_SAVE_TEXTURES = 1,
31 SI_SAVE_FRAMEBUFFER = 2,
32 SI_DISABLE_RENDER_COND = 4,
33
34 SI_CLEAR = 0,
35
36 SI_CLEAR_SURFACE = SI_SAVE_FRAMEBUFFER,
37
38 SI_COPY = SI_SAVE_FRAMEBUFFER | SI_SAVE_TEXTURES |
39 SI_DISABLE_RENDER_COND,
40
41 SI_BLIT = SI_SAVE_FRAMEBUFFER | SI_SAVE_TEXTURES |
42 SI_DISABLE_RENDER_COND,
43
44 SI_DECOMPRESS = SI_SAVE_FRAMEBUFFER | SI_DISABLE_RENDER_COND,
45
46 SI_COLOR_RESOLVE = SI_SAVE_FRAMEBUFFER | SI_DISABLE_RENDER_COND
47 };
48
49 static void si_blitter_begin(struct pipe_context *ctx, enum si_blitter_op op)
50 {
51 struct si_context *sctx = (struct si_context *)ctx;
52
53 r600_suspend_nontimer_queries(&sctx->b);
54
55 util_blitter_save_blend(sctx->blitter, sctx->queued.named.blend);
56 util_blitter_save_depth_stencil_alpha(sctx->blitter, sctx->queued.named.dsa);
57 util_blitter_save_stencil_ref(sctx->blitter, &sctx->stencil_ref);
58 util_blitter_save_rasterizer(sctx->blitter, sctx->queued.named.rasterizer);
59 util_blitter_save_fragment_shader(sctx->blitter, sctx->ps_shader);
60 util_blitter_save_geometry_shader(sctx->blitter, sctx->gs_shader);
61 util_blitter_save_vertex_shader(sctx->blitter, sctx->vs_shader);
62 util_blitter_save_vertex_elements(sctx->blitter, sctx->vertex_elements);
63 if (sctx->queued.named.viewport) {
64 util_blitter_save_viewport(sctx->blitter, &sctx->queued.named.viewport->viewport);
65 }
66 util_blitter_save_vertex_buffer_slot(sctx->blitter, sctx->vertex_buffer);
67 util_blitter_save_so_targets(sctx->blitter, sctx->b.streamout.num_targets,
68 (struct pipe_stream_output_target**)sctx->b.streamout.targets);
69
70 if (op & SI_SAVE_FRAMEBUFFER)
71 util_blitter_save_framebuffer(sctx->blitter, &sctx->framebuffer);
72
73 if (op & SI_SAVE_TEXTURES) {
74 util_blitter_save_fragment_sampler_states(
75 sctx->blitter, sctx->samplers[PIPE_SHADER_FRAGMENT].n_samplers,
76 (void**)sctx->samplers[PIPE_SHADER_FRAGMENT].samplers);
77
78 util_blitter_save_fragment_sampler_views(sctx->blitter,
79 util_last_bit(sctx->samplers[PIPE_SHADER_FRAGMENT].views.desc.enabled_mask &
80 ((1 << NUM_TEX_UNITS) - 1)),
81 sctx->samplers[PIPE_SHADER_FRAGMENT].views.views);
82 }
83
84 if ((op & SI_DISABLE_RENDER_COND) && sctx->b.current_render_cond) {
85 util_blitter_save_render_condition(sctx->blitter,
86 sctx->b.current_render_cond,
87 sctx->b.current_render_cond_cond,
88 sctx->b.current_render_cond_mode);
89 }
90 }
91
92 static void si_blitter_end(struct pipe_context *ctx)
93 {
94 struct si_context *sctx = (struct si_context *)ctx;
95 r600_resume_nontimer_queries(&sctx->b);
96 }
97
98 static unsigned u_max_sample(struct pipe_resource *r)
99 {
100 return r->nr_samples ? r->nr_samples - 1 : 0;
101 }
102
103 static void si_blit_decompress_depth(struct pipe_context *ctx,
104 struct r600_texture *texture,
105 struct r600_texture *staging,
106 unsigned first_level, unsigned last_level,
107 unsigned first_layer, unsigned last_layer,
108 unsigned first_sample, unsigned last_sample)
109 {
110 struct si_context *sctx = (struct si_context *)ctx;
111 unsigned layer, level, sample, checked_last_layer, max_layer, max_sample;
112 float depth = 1.0f;
113 const struct util_format_description *desc;
114 void **custom_dsa;
115 struct r600_texture *flushed_depth_texture = staging ?
116 staging : texture->flushed_depth_texture;
117
118 if (!staging && !texture->dirty_level_mask)
119 return;
120
121 max_sample = u_max_sample(&texture->resource.b.b);
122
123 desc = util_format_description(flushed_depth_texture->resource.b.b.format);
124 switch (util_format_has_depth(desc) | util_format_has_stencil(desc) << 1) {
125 default:
126 assert(!"No depth or stencil to uncompress");
127 return;
128 case 3:
129 custom_dsa = sctx->custom_dsa_flush_depth_stencil;
130 break;
131 case 2:
132 custom_dsa = sctx->custom_dsa_flush_stencil;
133 break;
134 case 1:
135 custom_dsa = sctx->custom_dsa_flush_depth;
136 break;
137 }
138
139 for (level = first_level; level <= last_level; level++) {
140 if (!staging && !(texture->dirty_level_mask & (1 << level)))
141 continue;
142
143 /* The smaller the mipmap level, the less layers there are
144 * as far as 3D textures are concerned. */
145 max_layer = util_max_layer(&texture->resource.b.b, level);
146 checked_last_layer = last_layer < max_layer ? last_layer : max_layer;
147
148 for (layer = first_layer; layer <= checked_last_layer; layer++) {
149 for (sample = first_sample; sample <= last_sample; sample++) {
150 struct pipe_surface *zsurf, *cbsurf, surf_tmpl;
151
152 surf_tmpl.format = texture->resource.b.b.format;
153 surf_tmpl.u.tex.level = level;
154 surf_tmpl.u.tex.first_layer = layer;
155 surf_tmpl.u.tex.last_layer = layer;
156
157 zsurf = ctx->create_surface(ctx, &texture->resource.b.b, &surf_tmpl);
158
159 surf_tmpl.format = flushed_depth_texture->resource.b.b.format;
160 cbsurf = ctx->create_surface(ctx,
161 (struct pipe_resource*)flushed_depth_texture, &surf_tmpl);
162
163 si_blitter_begin(ctx, SI_DECOMPRESS);
164 util_blitter_custom_depth_stencil(sctx->blitter, zsurf, cbsurf, 1 << sample,
165 custom_dsa[sample], depth);
166 si_blitter_end(ctx);
167
168 pipe_surface_reference(&zsurf, NULL);
169 pipe_surface_reference(&cbsurf, NULL);
170 }
171 }
172
173 /* The texture will always be dirty if some layers aren't flushed.
174 * I don't think this case can occur though. */
175 if (!staging &&
176 first_layer == 0 && last_layer == max_layer &&
177 first_sample == 0 && last_sample == max_sample) {
178 texture->dirty_level_mask &= ~(1 << level);
179 }
180 }
181 }
182
183 static void si_blit_decompress_depth_in_place(struct si_context *sctx,
184 struct r600_texture *texture,
185 unsigned first_level, unsigned last_level,
186 unsigned first_layer, unsigned last_layer)
187 {
188 struct pipe_surface *zsurf, surf_tmpl = {{0}};
189 unsigned layer, max_layer, checked_last_layer, level;
190
191 surf_tmpl.format = texture->resource.b.b.format;
192
193 for (level = first_level; level <= last_level; level++) {
194 if (!(texture->dirty_level_mask & (1 << level)))
195 continue;
196
197 surf_tmpl.u.tex.level = level;
198
199 /* The smaller the mipmap level, the less layers there are
200 * as far as 3D textures are concerned. */
201 max_layer = util_max_layer(&texture->resource.b.b, level);
202 checked_last_layer = last_layer < max_layer ? last_layer : max_layer;
203
204 for (layer = first_layer; layer <= checked_last_layer; layer++) {
205 surf_tmpl.u.tex.first_layer = layer;
206 surf_tmpl.u.tex.last_layer = layer;
207
208 zsurf = sctx->b.b.create_surface(&sctx->b.b, &texture->resource.b.b, &surf_tmpl);
209
210 si_blitter_begin(&sctx->b.b, SI_DECOMPRESS);
211 util_blitter_custom_depth_stencil(sctx->blitter, zsurf, NULL, ~0,
212 sctx->custom_dsa_flush_inplace,
213 1.0f);
214 si_blitter_end(&sctx->b.b);
215
216 pipe_surface_reference(&zsurf, NULL);
217 }
218
219 /* The texture will always be dirty if some layers aren't flushed.
220 * I don't think this case occurs often though. */
221 if (first_layer == 0 && last_layer == max_layer) {
222 texture->dirty_level_mask &= ~(1 << level);
223 }
224 }
225 }
226
227 void si_flush_depth_textures(struct si_context *sctx,
228 struct si_textures_info *textures)
229 {
230 unsigned i;
231
232 for (i = 0; i < textures->n_views; ++i) {
233 struct pipe_sampler_view *view;
234 struct r600_texture *tex;
235
236 view = textures->views.views[i];
237 if (!view) continue;
238
239 tex = (struct r600_texture *)view->texture;
240 if (!tex->is_depth || tex->is_flushing_texture)
241 continue;
242
243 si_blit_decompress_depth_in_place(sctx, tex,
244 view->u.tex.first_level, view->u.tex.last_level,
245 0, util_max_layer(&tex->resource.b.b, view->u.tex.first_level));
246 }
247 }
248
249 static void si_blit_decompress_color(struct pipe_context *ctx,
250 struct r600_texture *rtex,
251 unsigned first_level, unsigned last_level,
252 unsigned first_layer, unsigned last_layer)
253 {
254 struct si_context *sctx = (struct si_context *)ctx;
255 unsigned layer, level, checked_last_layer, max_layer;
256
257 if (!rtex->dirty_level_mask)
258 return;
259
260 for (level = first_level; level <= last_level; level++) {
261 if (!(rtex->dirty_level_mask & (1 << level)))
262 continue;
263
264 /* The smaller the mipmap level, the less layers there are
265 * as far as 3D textures are concerned. */
266 max_layer = util_max_layer(&rtex->resource.b.b, level);
267 checked_last_layer = last_layer < max_layer ? last_layer : max_layer;
268
269 for (layer = first_layer; layer <= checked_last_layer; layer++) {
270 struct pipe_surface *cbsurf, surf_tmpl;
271
272 surf_tmpl.format = rtex->resource.b.b.format;
273 surf_tmpl.u.tex.level = level;
274 surf_tmpl.u.tex.first_layer = layer;
275 surf_tmpl.u.tex.last_layer = layer;
276 cbsurf = ctx->create_surface(ctx, &rtex->resource.b.b, &surf_tmpl);
277
278 si_blitter_begin(ctx, SI_DECOMPRESS);
279 util_blitter_custom_color(sctx->blitter, cbsurf,
280 sctx->custom_blend_decompress);
281 si_blitter_end(ctx);
282
283 pipe_surface_reference(&cbsurf, NULL);
284 }
285
286 /* The texture will always be dirty if some layers aren't flushed.
287 * I don't think this case occurs often though. */
288 if (first_layer == 0 && last_layer == max_layer) {
289 rtex->dirty_level_mask &= ~(1 << level);
290 }
291 }
292 }
293
294 void si_decompress_color_textures(struct si_context *sctx,
295 struct si_textures_info *textures)
296 {
297 unsigned i;
298 unsigned mask = textures->compressed_colortex_mask;
299
300 while (mask) {
301 struct pipe_sampler_view *view;
302 struct r600_texture *tex;
303
304 i = u_bit_scan(&mask);
305
306 view = textures->views.views[i];
307 assert(view);
308
309 tex = (struct r600_texture *)view->texture;
310 assert(tex->cmask.size || tex->fmask.size);
311
312 si_blit_decompress_color(&sctx->b.b, tex,
313 view->u.tex.first_level, view->u.tex.last_level,
314 0, util_max_layer(&tex->resource.b.b, view->u.tex.first_level));
315 }
316 }
317
318 static void si_clear(struct pipe_context *ctx, unsigned buffers,
319 const union pipe_color_union *color,
320 double depth, unsigned stencil)
321 {
322 struct si_context *sctx = (struct si_context *)ctx;
323 struct pipe_framebuffer_state *fb = &sctx->framebuffer;
324
325 si_blitter_begin(ctx, SI_CLEAR);
326 util_blitter_clear(sctx->blitter, fb->width, fb->height,
327 util_framebuffer_get_num_layers(fb),
328 buffers, color, depth, stencil);
329 si_blitter_end(ctx);
330 }
331
332 static void si_clear_render_target(struct pipe_context *ctx,
333 struct pipe_surface *dst,
334 const union pipe_color_union *color,
335 unsigned dstx, unsigned dsty,
336 unsigned width, unsigned height)
337 {
338 struct si_context *sctx = (struct si_context *)ctx;
339
340 si_blitter_begin(ctx, SI_CLEAR_SURFACE);
341 util_blitter_clear_render_target(sctx->blitter, dst, color,
342 dstx, dsty, width, height);
343 si_blitter_end(ctx);
344 }
345
346 static void si_clear_depth_stencil(struct pipe_context *ctx,
347 struct pipe_surface *dst,
348 unsigned clear_flags,
349 double depth,
350 unsigned stencil,
351 unsigned dstx, unsigned dsty,
352 unsigned width, unsigned height)
353 {
354 struct si_context *sctx = (struct si_context *)ctx;
355
356 si_blitter_begin(ctx, SI_CLEAR_SURFACE);
357 util_blitter_clear_depth_stencil(sctx->blitter, dst, clear_flags, depth, stencil,
358 dstx, dsty, width, height);
359 si_blitter_end(ctx);
360 }
361
362 /* Helper for decompressing a portion of a color or depth resource before
363 * blitting if any decompression is needed.
364 * The driver doesn't decompress resources automatically while u_blitter is
365 * rendering. */
366 static void si_decompress_subresource(struct pipe_context *ctx,
367 struct pipe_resource *tex,
368 unsigned level,
369 unsigned first_layer, unsigned last_layer)
370 {
371 struct si_context *sctx = (struct si_context *)ctx;
372 struct r600_texture *rtex = (struct r600_texture*)tex;
373
374 if (rtex->is_depth && !rtex->is_flushing_texture) {
375 si_blit_decompress_depth_in_place(sctx, rtex,
376 level, level,
377 first_layer, last_layer);
378 } else if (rtex->fmask.size || rtex->cmask.size) {
379 si_blit_decompress_color(ctx, rtex, level, level,
380 first_layer, last_layer);
381 }
382 }
383
384 struct texture_orig_info {
385 unsigned format;
386 unsigned width0;
387 unsigned height0;
388 unsigned npix_x;
389 unsigned npix_y;
390 unsigned npix0_x;
391 unsigned npix0_y;
392 };
393
394 static void si_compressed_to_blittable(struct pipe_resource *tex,
395 unsigned level,
396 struct texture_orig_info *orig)
397 {
398 struct r600_texture *rtex = (struct r600_texture*)tex;
399 unsigned pixsize = util_format_get_blocksize(rtex->resource.b.b.format);
400 int new_format;
401 int new_height, new_width;
402
403 orig->format = tex->format;
404 orig->width0 = tex->width0;
405 orig->height0 = tex->height0;
406 orig->npix0_x = rtex->surface.level[0].npix_x;
407 orig->npix0_y = rtex->surface.level[0].npix_y;
408 orig->npix_x = rtex->surface.level[level].npix_x;
409 orig->npix_y = rtex->surface.level[level].npix_y;
410
411 if (pixsize == 8)
412 new_format = PIPE_FORMAT_R16G16B16A16_UINT; /* 64-bit block */
413 else
414 new_format = PIPE_FORMAT_R32G32B32A32_UINT; /* 128-bit block */
415
416 new_width = util_format_get_nblocksx(tex->format, orig->width0);
417 new_height = util_format_get_nblocksy(tex->format, orig->height0);
418
419 tex->width0 = new_width;
420 tex->height0 = new_height;
421 tex->format = new_format;
422 rtex->surface.level[0].npix_x = util_format_get_nblocksx(orig->format, orig->npix0_x);
423 rtex->surface.level[0].npix_y = util_format_get_nblocksy(orig->format, orig->npix0_y);
424 rtex->surface.level[level].npix_x = util_format_get_nblocksx(orig->format, orig->npix_x);
425 rtex->surface.level[level].npix_y = util_format_get_nblocksy(orig->format, orig->npix_y);
426
427 /* By dividing the dimensions by 4, we effectively decrement
428 * last_level by 2, therefore the last 2 mipmap levels disappear and
429 * aren't blittable. Note that the last 3 mipmap levels (4x4, 2x2,
430 * 1x1) have equal slice sizes, which is an important assumption
431 * for this to work.
432 *
433 * In order to make the last 2 mipmap levels blittable, we have to
434 * add the slice size of the last mipmap level to the texture
435 * address, so that even though the hw thinks it reads last_level-2,
436 * it will actually read last_level-1, and if we add the slice size*2,
437 * it will read last_level. That's how this workaround works.
438 */
439 if (level > rtex->resource.b.b.last_level-2)
440 rtex->mipmap_shift = level - (rtex->resource.b.b.last_level-2);
441 }
442
443 static void si_change_format(struct pipe_resource *tex,
444 unsigned level,
445 struct texture_orig_info *orig,
446 enum pipe_format format)
447 {
448 struct r600_texture *rtex = (struct r600_texture*)tex;
449
450 orig->format = tex->format;
451 orig->width0 = tex->width0;
452 orig->height0 = tex->height0;
453 orig->npix0_x = rtex->surface.level[0].npix_x;
454 orig->npix0_y = rtex->surface.level[0].npix_y;
455 orig->npix_x = rtex->surface.level[level].npix_x;
456 orig->npix_y = rtex->surface.level[level].npix_y;
457
458 tex->format = format;
459 }
460
461 static void si_reset_blittable_to_orig(struct pipe_resource *tex,
462 unsigned level,
463 struct texture_orig_info *orig)
464 {
465 struct r600_texture *rtex = (struct r600_texture*)tex;
466
467 tex->format = orig->format;
468 tex->width0 = orig->width0;
469 tex->height0 = orig->height0;
470 rtex->surface.level[0].npix_x = orig->npix0_x;
471 rtex->surface.level[0].npix_y = orig->npix0_y;
472 rtex->surface.level[level].npix_x = orig->npix_x;
473 rtex->surface.level[level].npix_y = orig->npix_y;
474 rtex->mipmap_shift = 0;
475 }
476
477 static void si_resource_copy_region(struct pipe_context *ctx,
478 struct pipe_resource *dst,
479 unsigned dst_level,
480 unsigned dstx, unsigned dsty, unsigned dstz,
481 struct pipe_resource *src,
482 unsigned src_level,
483 const struct pipe_box *src_box)
484 {
485 struct si_context *sctx = (struct si_context *)ctx;
486 struct r600_texture *rdst = (struct r600_texture*)dst;
487 struct pipe_surface *dst_view, dst_templ;
488 struct pipe_sampler_view src_templ, *src_view;
489 struct texture_orig_info orig_info[2];
490 struct pipe_box sbox, dstbox;
491 boolean restore_orig[2];
492
493 /* Fallback for buffers. */
494 if (dst->target == PIPE_BUFFER && src->target == PIPE_BUFFER) {
495 si_copy_buffer(sctx, dst, src, dstx, src_box->x, src_box->width);
496 return;
497 }
498
499 memset(orig_info, 0, sizeof(orig_info));
500
501 /* The driver doesn't decompress resources automatically while
502 * u_blitter is rendering. */
503 si_decompress_subresource(ctx, src, src_level,
504 src_box->z, src_box->z + src_box->depth - 1);
505
506 restore_orig[0] = restore_orig[1] = FALSE;
507
508 if (util_format_is_compressed(src->format) &&
509 util_format_is_compressed(dst->format)) {
510 si_compressed_to_blittable(src, src_level, &orig_info[0]);
511 restore_orig[0] = TRUE;
512 sbox.x = util_format_get_nblocksx(orig_info[0].format, src_box->x);
513 sbox.y = util_format_get_nblocksy(orig_info[0].format, src_box->y);
514 sbox.z = src_box->z;
515 sbox.width = util_format_get_nblocksx(orig_info[0].format, src_box->width);
516 sbox.height = util_format_get_nblocksy(orig_info[0].format, src_box->height);
517 sbox.depth = src_box->depth;
518 src_box = &sbox;
519
520 si_compressed_to_blittable(dst, dst_level, &orig_info[1]);
521 restore_orig[1] = TRUE;
522 /* translate the dst box as well */
523 dstx = util_format_get_nblocksx(orig_info[1].format, dstx);
524 dsty = util_format_get_nblocksy(orig_info[1].format, dsty);
525 } else if (!util_blitter_is_copy_supported(sctx->blitter, dst, src)) {
526 unsigned blocksize = util_format_get_blocksize(src->format);
527
528 switch (blocksize) {
529 case 1:
530 si_change_format(src, src_level, &orig_info[0],
531 PIPE_FORMAT_R8_UNORM);
532 si_change_format(dst, dst_level, &orig_info[1],
533 PIPE_FORMAT_R8_UNORM);
534 break;
535 case 2:
536 si_change_format(src, src_level, &orig_info[0],
537 PIPE_FORMAT_R8G8_UNORM);
538 si_change_format(dst, dst_level, &orig_info[1],
539 PIPE_FORMAT_R8G8_UNORM);
540 break;
541 case 4:
542 si_change_format(src, src_level, &orig_info[0],
543 PIPE_FORMAT_R8G8B8A8_UNORM);
544 si_change_format(dst, dst_level, &orig_info[1],
545 PIPE_FORMAT_R8G8B8A8_UNORM);
546 break;
547 case 8:
548 si_change_format(src, src_level, &orig_info[0],
549 PIPE_FORMAT_R16G16B16A16_UINT);
550 si_change_format(dst, dst_level, &orig_info[1],
551 PIPE_FORMAT_R16G16B16A16_UINT);
552 break;
553 case 16:
554 si_change_format(src, src_level, &orig_info[0],
555 PIPE_FORMAT_R32G32B32A32_UINT);
556 si_change_format(dst, dst_level, &orig_info[1],
557 PIPE_FORMAT_R32G32B32A32_UINT);
558 break;
559 default:
560 fprintf(stderr, "Unhandled format %s with blocksize %u\n",
561 util_format_short_name(src->format), blocksize);
562 assert(0);
563 }
564 restore_orig[0] = TRUE;
565 restore_orig[1] = TRUE;
566 }
567
568 /* Initialize the surface. */
569 util_blitter_default_dst_texture(&dst_templ, dst, dst_level, dstz);
570 dst_view = r600_create_surface_custom(ctx, dst, &dst_templ,
571 rdst->surface.level[dst_level].npix_x,
572 rdst->surface.level[dst_level].npix_y);
573
574 /* Initialize the sampler view. */
575 util_blitter_default_src_texture(&src_templ, src, src_level);
576 src_view = ctx->create_sampler_view(ctx, src, &src_templ);
577
578 u_box_3d(dstx, dsty, dstz, abs(src_box->width), abs(src_box->height),
579 abs(src_box->depth), &dstbox);
580
581 /* Copy. */
582 si_blitter_begin(ctx, SI_COPY);
583 util_blitter_blit_generic(sctx->blitter, dst_view, &dstbox,
584 src_view, src_box, src->width0, src->height0,
585 PIPE_MASK_RGBAZS, PIPE_TEX_FILTER_NEAREST, NULL);
586 si_blitter_end(ctx);
587
588 pipe_surface_reference(&dst_view, NULL);
589 pipe_sampler_view_reference(&src_view, NULL);
590
591 if (restore_orig[0])
592 si_reset_blittable_to_orig(src, src_level, &orig_info[0]);
593
594 if (restore_orig[1])
595 si_reset_blittable_to_orig(dst, dst_level, &orig_info[1]);
596 }
597
598 /* For MSAA integer resolving to work, we change the format to NORM using this function. */
599 static enum pipe_format int_to_norm_format(enum pipe_format format)
600 {
601 switch (format) {
602 #define REPLACE_FORMAT_SIGN(format,sign) \
603 case PIPE_FORMAT_##format##_##sign##INT: \
604 return PIPE_FORMAT_##format##_##sign##NORM
605 #define REPLACE_FORMAT(format) \
606 REPLACE_FORMAT_SIGN(format, U); \
607 REPLACE_FORMAT_SIGN(format, S)
608
609 REPLACE_FORMAT_SIGN(B10G10R10A2, U);
610 REPLACE_FORMAT(R8);
611 REPLACE_FORMAT(R8G8);
612 REPLACE_FORMAT(R8G8B8X8);
613 REPLACE_FORMAT(R8G8B8A8);
614 REPLACE_FORMAT(A8);
615 REPLACE_FORMAT(I8);
616 REPLACE_FORMAT(L8);
617 REPLACE_FORMAT(L8A8);
618 REPLACE_FORMAT(R16);
619 REPLACE_FORMAT(R16G16);
620 REPLACE_FORMAT(R16G16B16X16);
621 REPLACE_FORMAT(R16G16B16A16);
622 REPLACE_FORMAT(A16);
623 REPLACE_FORMAT(I16);
624 REPLACE_FORMAT(L16);
625 REPLACE_FORMAT(L16A16);
626
627 #undef REPLACE_FORMAT
628 #undef REPLACE_FORMAT_SIGN
629 default:
630 return format;
631 }
632 }
633
634 static bool do_hardware_msaa_resolve(struct pipe_context *ctx,
635 const struct pipe_blit_info *info)
636 {
637 struct si_context *sctx = (struct si_context*)ctx;
638 struct r600_texture *dst = (struct r600_texture*)info->dst.resource;
639 unsigned dst_width = u_minify(info->dst.resource->width0, info->dst.level);
640 unsigned dst_height = u_minify(info->dst.resource->height0, info->dst.level);
641 enum pipe_format format = int_to_norm_format(info->dst.format);
642 unsigned sample_mask = ~0;
643
644 if (info->src.resource->nr_samples > 1 &&
645 info->dst.resource->nr_samples <= 1 &&
646 util_max_layer(info->src.resource, 0) == 0 &&
647 util_max_layer(info->dst.resource, info->dst.level) == 0 &&
648 info->dst.format == info->src.format &&
649 !util_format_is_pure_integer(format) &&
650 !util_format_is_depth_or_stencil(format) &&
651 !info->scissor_enable &&
652 (info->mask & PIPE_MASK_RGBA) == PIPE_MASK_RGBA &&
653 dst_width == info->src.resource->width0 &&
654 dst_height == info->src.resource->height0 &&
655 info->dst.box.x == 0 &&
656 info->dst.box.y == 0 &&
657 info->dst.box.width == dst_width &&
658 info->dst.box.height == dst_height &&
659 info->dst.box.depth == 1 &&
660 info->src.box.x == 0 &&
661 info->src.box.y == 0 &&
662 info->src.box.width == dst_width &&
663 info->src.box.height == dst_height &&
664 info->src.box.depth == 1 &&
665 dst->surface.level[info->dst.level].mode >= RADEON_SURF_MODE_1D &&
666 !(dst->surface.flags & RADEON_SURF_SCANOUT)) {
667 si_blitter_begin(ctx, SI_COLOR_RESOLVE);
668 util_blitter_custom_resolve_color(sctx->blitter,
669 info->dst.resource, info->dst.level,
670 info->dst.box.z,
671 info->src.resource, info->src.box.z,
672 sample_mask, sctx->custom_blend_resolve,
673 format);
674 si_blitter_end(ctx);
675 return true;
676 }
677 return false;
678 }
679
680 static void si_blit(struct pipe_context *ctx,
681 const struct pipe_blit_info *info)
682 {
683 struct si_context *sctx = (struct si_context*)ctx;
684
685 if (do_hardware_msaa_resolve(ctx, info)) {
686 return;
687 }
688
689 assert(util_blitter_is_blit_supported(sctx->blitter, info));
690
691 /* The driver doesn't decompress resources automatically while
692 * u_blitter is rendering. */
693 si_decompress_subresource(ctx, info->src.resource, info->src.level,
694 info->src.box.z,
695 info->src.box.z + info->src.box.depth - 1);
696
697 si_blitter_begin(ctx, SI_BLIT);
698 util_blitter_blit(sctx->blitter, info);
699 si_blitter_end(ctx);
700 }
701
702 static void si_flush_resource(struct pipe_context *ctx,
703 struct pipe_resource *resource)
704 {
705 }
706
707 void si_init_blit_functions(struct si_context *sctx)
708 {
709 sctx->b.b.clear = si_clear;
710 sctx->b.b.clear_render_target = si_clear_render_target;
711 sctx->b.b.clear_depth_stencil = si_clear_depth_stencil;
712 sctx->b.b.resource_copy_region = si_resource_copy_region;
713 sctx->b.b.blit = si_blit;
714 sctx->b.b.flush_resource = si_flush_resource;
715 sctx->b.blit_decompress_depth = si_blit_decompress_depth;
716 }