Merge remote-tracking branch 'mesa-public/master' into vulkan
[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_format.h"
26 #include "util/u_surface.h"
27
28 enum si_blitter_op /* bitmask */
29 {
30 SI_SAVE_TEXTURES = 1,
31 SI_SAVE_FRAMEBUFFER = 2,
32 SI_SAVE_FRAGMENT_STATE = 4,
33 SI_DISABLE_RENDER_COND = 8,
34
35 SI_CLEAR = SI_SAVE_FRAGMENT_STATE,
36
37 SI_CLEAR_SURFACE = SI_SAVE_FRAMEBUFFER | SI_SAVE_FRAGMENT_STATE,
38
39 SI_COPY = SI_SAVE_FRAMEBUFFER | SI_SAVE_TEXTURES |
40 SI_SAVE_FRAGMENT_STATE | SI_DISABLE_RENDER_COND,
41
42 SI_BLIT = SI_SAVE_FRAMEBUFFER | SI_SAVE_TEXTURES |
43 SI_SAVE_FRAGMENT_STATE,
44
45 SI_DECOMPRESS = SI_SAVE_FRAMEBUFFER | SI_SAVE_FRAGMENT_STATE |
46 SI_DISABLE_RENDER_COND,
47
48 SI_COLOR_RESOLVE = SI_SAVE_FRAMEBUFFER | SI_SAVE_FRAGMENT_STATE
49 };
50
51 static void si_blitter_begin(struct pipe_context *ctx, enum si_blitter_op op)
52 {
53 struct si_context *sctx = (struct si_context *)ctx;
54
55 r600_suspend_nontimer_queries(&sctx->b);
56
57 util_blitter_save_vertex_buffer_slot(sctx->blitter, sctx->vertex_buffer);
58 util_blitter_save_vertex_elements(sctx->blitter, sctx->vertex_elements);
59 util_blitter_save_vertex_shader(sctx->blitter, sctx->vs_shader.cso);
60 util_blitter_save_tessctrl_shader(sctx->blitter, sctx->tcs_shader.cso);
61 util_blitter_save_tesseval_shader(sctx->blitter, sctx->tes_shader.cso);
62 util_blitter_save_geometry_shader(sctx->blitter, sctx->gs_shader.cso);
63 util_blitter_save_so_targets(sctx->blitter, sctx->b.streamout.num_targets,
64 (struct pipe_stream_output_target**)sctx->b.streamout.targets);
65 util_blitter_save_rasterizer(sctx->blitter, sctx->queued.named.rasterizer);
66
67 if (op & SI_SAVE_FRAGMENT_STATE) {
68 util_blitter_save_blend(sctx->blitter, sctx->queued.named.blend);
69 util_blitter_save_depth_stencil_alpha(sctx->blitter, sctx->queued.named.dsa);
70 util_blitter_save_stencil_ref(sctx->blitter, &sctx->stencil_ref.state);
71 util_blitter_save_fragment_shader(sctx->blitter, sctx->ps_shader.cso);
72 util_blitter_save_sample_mask(sctx->blitter, sctx->sample_mask.sample_mask);
73 util_blitter_save_viewport(sctx->blitter, &sctx->viewports.states[0]);
74 util_blitter_save_scissor(sctx->blitter, &sctx->scissors.states[0]);
75 }
76
77 if (op & SI_SAVE_FRAMEBUFFER)
78 util_blitter_save_framebuffer(sctx->blitter, &sctx->framebuffer.state);
79
80 if (op & SI_SAVE_TEXTURES) {
81 util_blitter_save_fragment_sampler_states(
82 sctx->blitter, 2,
83 sctx->samplers[PIPE_SHADER_FRAGMENT].states.saved_states);
84
85 util_blitter_save_fragment_sampler_views(sctx->blitter, 2,
86 sctx->samplers[PIPE_SHADER_FRAGMENT].views.views);
87 }
88
89 if (op & SI_DISABLE_RENDER_COND)
90 sctx->b.render_cond_force_off = true;
91 }
92
93 static void si_blitter_end(struct pipe_context *ctx)
94 {
95 struct si_context *sctx = (struct si_context *)ctx;
96
97 sctx->b.render_cond_force_off = false;
98 r600_resume_nontimer_queries(&sctx->b);
99 }
100
101 static unsigned u_max_sample(struct pipe_resource *r)
102 {
103 return r->nr_samples ? r->nr_samples - 1 : 0;
104 }
105
106 static void si_blit_decompress_depth(struct pipe_context *ctx,
107 struct r600_texture *texture,
108 struct r600_texture *staging,
109 unsigned first_level, unsigned last_level,
110 unsigned first_layer, unsigned last_layer,
111 unsigned first_sample, unsigned last_sample)
112 {
113 struct si_context *sctx = (struct si_context *)ctx;
114 unsigned layer, level, sample, checked_last_layer, max_layer, max_sample;
115 float depth = 1.0f;
116 const struct util_format_description *desc;
117 struct r600_texture *flushed_depth_texture = staging ?
118 staging : texture->flushed_depth_texture;
119
120 if (!staging && !texture->dirty_level_mask)
121 return;
122
123 max_sample = u_max_sample(&texture->resource.b.b);
124
125 desc = util_format_description(flushed_depth_texture->resource.b.b.format);
126
127 if (util_format_has_depth(desc))
128 sctx->dbcb_depth_copy_enabled = true;
129 if (util_format_has_stencil(desc))
130 sctx->dbcb_stencil_copy_enabled = true;
131
132 assert(sctx->dbcb_depth_copy_enabled || sctx->dbcb_stencil_copy_enabled);
133
134 for (level = first_level; level <= last_level; level++) {
135 if (!staging && !(texture->dirty_level_mask & (1 << level)))
136 continue;
137
138 /* The smaller the mipmap level, the less layers there are
139 * as far as 3D textures are concerned. */
140 max_layer = util_max_layer(&texture->resource.b.b, level);
141 checked_last_layer = last_layer < max_layer ? last_layer : max_layer;
142
143 for (layer = first_layer; layer <= checked_last_layer; layer++) {
144 for (sample = first_sample; sample <= last_sample; sample++) {
145 struct pipe_surface *zsurf, *cbsurf, surf_tmpl;
146
147 sctx->dbcb_copy_sample = sample;
148 si_mark_atom_dirty(sctx, &sctx->db_render_state);
149
150 surf_tmpl.format = texture->resource.b.b.format;
151 surf_tmpl.u.tex.level = level;
152 surf_tmpl.u.tex.first_layer = layer;
153 surf_tmpl.u.tex.last_layer = layer;
154
155 zsurf = ctx->create_surface(ctx, &texture->resource.b.b, &surf_tmpl);
156
157 surf_tmpl.format = flushed_depth_texture->resource.b.b.format;
158 cbsurf = ctx->create_surface(ctx,
159 (struct pipe_resource*)flushed_depth_texture, &surf_tmpl);
160
161 si_blitter_begin(ctx, SI_DECOMPRESS);
162 util_blitter_custom_depth_stencil(sctx->blitter, zsurf, cbsurf, 1 << sample,
163 sctx->custom_dsa_flush, depth);
164 si_blitter_end(ctx);
165
166 pipe_surface_reference(&zsurf, NULL);
167 pipe_surface_reference(&cbsurf, NULL);
168 }
169 }
170
171 /* The texture will always be dirty if some layers aren't flushed.
172 * I don't think this case can occur though. */
173 if (!staging &&
174 first_layer == 0 && last_layer == max_layer &&
175 first_sample == 0 && last_sample == max_sample) {
176 texture->dirty_level_mask &= ~(1 << level);
177 }
178 }
179
180 sctx->dbcb_depth_copy_enabled = false;
181 sctx->dbcb_stencil_copy_enabled = false;
182 si_mark_atom_dirty(sctx, &sctx->db_render_state);
183 }
184
185 static void si_blit_decompress_depth_in_place(struct si_context *sctx,
186 struct r600_texture *texture,
187 bool is_stencil_sampler,
188 unsigned first_level, unsigned last_level,
189 unsigned first_layer, unsigned last_layer)
190 {
191 struct pipe_surface *zsurf, surf_tmpl = {{0}};
192 unsigned layer, max_layer, checked_last_layer, level;
193 unsigned *dirty_level_mask;
194
195 if (is_stencil_sampler) {
196 sctx->db_flush_stencil_inplace = true;
197 dirty_level_mask = &texture->stencil_dirty_level_mask;
198 } else {
199 sctx->db_flush_depth_inplace = true;
200 dirty_level_mask = &texture->dirty_level_mask;
201 }
202 si_mark_atom_dirty(sctx, &sctx->db_render_state);
203
204 surf_tmpl.format = texture->resource.b.b.format;
205
206 for (level = first_level; level <= last_level; level++) {
207 if (!(*dirty_level_mask & (1 << level)))
208 continue;
209
210 surf_tmpl.u.tex.level = level;
211
212 /* The smaller the mipmap level, the less layers there are
213 * as far as 3D textures are concerned. */
214 max_layer = util_max_layer(&texture->resource.b.b, level);
215 checked_last_layer = last_layer < max_layer ? last_layer : max_layer;
216
217 for (layer = first_layer; layer <= checked_last_layer; layer++) {
218 surf_tmpl.u.tex.first_layer = layer;
219 surf_tmpl.u.tex.last_layer = layer;
220
221 zsurf = sctx->b.b.create_surface(&sctx->b.b, &texture->resource.b.b, &surf_tmpl);
222
223 si_blitter_begin(&sctx->b.b, SI_DECOMPRESS);
224 util_blitter_custom_depth_stencil(sctx->blitter, zsurf, NULL, ~0,
225 sctx->custom_dsa_flush,
226 1.0f);
227 si_blitter_end(&sctx->b.b);
228
229 pipe_surface_reference(&zsurf, NULL);
230 }
231
232 /* The texture will always be dirty if some layers aren't flushed.
233 * I don't think this case occurs often though. */
234 if (first_layer == 0 && last_layer == max_layer) {
235 *dirty_level_mask &= ~(1 << level);
236 }
237 }
238
239 sctx->db_flush_depth_inplace = false;
240 sctx->db_flush_stencil_inplace = false;
241 si_mark_atom_dirty(sctx, &sctx->db_render_state);
242 }
243
244 void si_flush_depth_textures(struct si_context *sctx,
245 struct si_textures_info *textures)
246 {
247 unsigned i;
248 unsigned mask = textures->depth_texture_mask;
249
250 while (mask) {
251 struct pipe_sampler_view *view;
252 struct si_sampler_view *sview;
253 struct r600_texture *tex;
254
255 i = u_bit_scan(&mask);
256
257 view = textures->views.views[i];
258 assert(view);
259 sview = (struct si_sampler_view*)view;
260
261 tex = (struct r600_texture *)view->texture;
262 assert(tex->is_depth && !tex->is_flushing_texture);
263
264 si_blit_decompress_depth_in_place(sctx, tex,
265 sview->is_stencil_sampler,
266 view->u.tex.first_level, view->u.tex.last_level,
267 0, util_max_layer(&tex->resource.b.b, view->u.tex.first_level));
268 }
269 }
270
271 static void si_blit_decompress_color(struct pipe_context *ctx,
272 struct r600_texture *rtex,
273 unsigned first_level, unsigned last_level,
274 unsigned first_layer, unsigned last_layer)
275 {
276 struct si_context *sctx = (struct si_context *)ctx;
277 unsigned layer, level, checked_last_layer, max_layer;
278
279 if (!rtex->dirty_level_mask)
280 return;
281
282 for (level = first_level; level <= last_level; level++) {
283 if (!(rtex->dirty_level_mask & (1 << level)))
284 continue;
285
286 /* The smaller the mipmap level, the less layers there are
287 * as far as 3D textures are concerned. */
288 max_layer = util_max_layer(&rtex->resource.b.b, level);
289 checked_last_layer = last_layer < max_layer ? last_layer : max_layer;
290
291 for (layer = first_layer; layer <= checked_last_layer; layer++) {
292 struct pipe_surface *cbsurf, surf_tmpl;
293
294 surf_tmpl.format = rtex->resource.b.b.format;
295 surf_tmpl.u.tex.level = level;
296 surf_tmpl.u.tex.first_layer = layer;
297 surf_tmpl.u.tex.last_layer = layer;
298 cbsurf = ctx->create_surface(ctx, &rtex->resource.b.b, &surf_tmpl);
299
300 si_blitter_begin(ctx, SI_DECOMPRESS);
301 util_blitter_custom_color(sctx->blitter, cbsurf,
302 rtex->fmask.size ? sctx->custom_blend_decompress :
303 sctx->custom_blend_fastclear);
304 si_blitter_end(ctx);
305
306 pipe_surface_reference(&cbsurf, NULL);
307 }
308
309 /* The texture will always be dirty if some layers aren't flushed.
310 * I don't think this case occurs often though. */
311 if (first_layer == 0 && last_layer == max_layer) {
312 rtex->dirty_level_mask &= ~(1 << level);
313 }
314 }
315 }
316
317 void si_decompress_color_textures(struct si_context *sctx,
318 struct si_textures_info *textures)
319 {
320 unsigned i;
321 unsigned mask = textures->compressed_colortex_mask;
322
323 while (mask) {
324 struct pipe_sampler_view *view;
325 struct r600_texture *tex;
326
327 i = u_bit_scan(&mask);
328
329 view = textures->views.views[i];
330 assert(view);
331
332 tex = (struct r600_texture *)view->texture;
333 assert(tex->cmask.size || tex->fmask.size || tex->dcc_buffer);
334
335 si_blit_decompress_color(&sctx->b.b, tex,
336 view->u.tex.first_level, view->u.tex.last_level,
337 0, util_max_layer(&tex->resource.b.b, view->u.tex.first_level));
338 }
339 }
340
341 static void si_clear(struct pipe_context *ctx, unsigned buffers,
342 const union pipe_color_union *color,
343 double depth, unsigned stencil)
344 {
345 struct si_context *sctx = (struct si_context *)ctx;
346 struct pipe_framebuffer_state *fb = &sctx->framebuffer.state;
347 struct pipe_surface *zsbuf = fb->zsbuf;
348 struct r600_texture *zstex =
349 zsbuf ? (struct r600_texture*)zsbuf->texture : NULL;
350
351 if (buffers & PIPE_CLEAR_COLOR) {
352 evergreen_do_fast_color_clear(&sctx->b, fb,
353 &sctx->framebuffer.atom, &buffers,
354 &sctx->framebuffer.dirty_cbufs,
355 color);
356 if (!buffers)
357 return; /* all buffers have been fast cleared */
358 }
359
360 if (buffers & PIPE_CLEAR_COLOR) {
361 int i;
362
363 /* These buffers cannot use fast clear, make sure to disable expansion. */
364 for (i = 0; i < fb->nr_cbufs; i++) {
365 struct r600_texture *tex;
366
367 /* If not clearing this buffer, skip. */
368 if (!(buffers & (PIPE_CLEAR_COLOR0 << i)))
369 continue;
370
371 if (!fb->cbufs[i])
372 continue;
373
374 tex = (struct r600_texture *)fb->cbufs[i]->texture;
375 if (tex->fmask.size == 0)
376 tex->dirty_level_mask &= ~(1 << fb->cbufs[i]->u.tex.level);
377 }
378 }
379
380 if (buffers & PIPE_CLEAR_DEPTH &&
381 zstex && zstex->htile_buffer &&
382 zsbuf->u.tex.level == 0 &&
383 zsbuf->u.tex.first_layer == 0 &&
384 zsbuf->u.tex.last_layer == util_max_layer(&zstex->resource.b.b, 0)) {
385 /* Need to disable EXPCLEAR temporarily if clearing
386 * to a new value. */
387 if (zstex->depth_cleared && zstex->depth_clear_value != depth) {
388 sctx->db_depth_disable_expclear = true;
389 }
390
391 zstex->depth_clear_value = depth;
392 sctx->framebuffer.dirty_zsbuf = true;
393 si_mark_atom_dirty(sctx, &sctx->framebuffer.atom); /* updates DB_DEPTH_CLEAR */
394 sctx->db_depth_clear = true;
395 si_mark_atom_dirty(sctx, &sctx->db_render_state);
396 }
397
398 si_blitter_begin(ctx, SI_CLEAR);
399 util_blitter_clear(sctx->blitter, fb->width, fb->height,
400 util_framebuffer_get_num_layers(fb),
401 buffers, color, depth, stencil);
402 si_blitter_end(ctx);
403
404 if (sctx->db_depth_clear) {
405 sctx->db_depth_clear = false;
406 sctx->db_depth_disable_expclear = false;
407 zstex->depth_cleared = true;
408 si_mark_atom_dirty(sctx, &sctx->db_render_state);
409 }
410 }
411
412 static void si_clear_render_target(struct pipe_context *ctx,
413 struct pipe_surface *dst,
414 const union pipe_color_union *color,
415 unsigned dstx, unsigned dsty,
416 unsigned width, unsigned height)
417 {
418 struct si_context *sctx = (struct si_context *)ctx;
419
420 si_blitter_begin(ctx, SI_CLEAR_SURFACE);
421 util_blitter_clear_render_target(sctx->blitter, dst, color,
422 dstx, dsty, width, height);
423 si_blitter_end(ctx);
424 }
425
426 static void si_clear_depth_stencil(struct pipe_context *ctx,
427 struct pipe_surface *dst,
428 unsigned clear_flags,
429 double depth,
430 unsigned stencil,
431 unsigned dstx, unsigned dsty,
432 unsigned width, unsigned height)
433 {
434 struct si_context *sctx = (struct si_context *)ctx;
435
436 si_blitter_begin(ctx, SI_CLEAR_SURFACE);
437 util_blitter_clear_depth_stencil(sctx->blitter, dst, clear_flags, depth, stencil,
438 dstx, dsty, width, height);
439 si_blitter_end(ctx);
440 }
441
442 /* Helper for decompressing a portion of a color or depth resource before
443 * blitting if any decompression is needed.
444 * The driver doesn't decompress resources automatically while u_blitter is
445 * rendering. */
446 static void si_decompress_subresource(struct pipe_context *ctx,
447 struct pipe_resource *tex,
448 unsigned level,
449 unsigned first_layer, unsigned last_layer)
450 {
451 struct si_context *sctx = (struct si_context *)ctx;
452 struct r600_texture *rtex = (struct r600_texture*)tex;
453
454 if (rtex->is_depth && !rtex->is_flushing_texture) {
455 si_blit_decompress_depth_in_place(sctx, rtex, false,
456 level, level,
457 first_layer, last_layer);
458 if (rtex->surface.flags & RADEON_SURF_SBUFFER)
459 si_blit_decompress_depth_in_place(sctx, rtex, true,
460 level, level,
461 first_layer, last_layer);
462 } else if (rtex->fmask.size || rtex->cmask.size || rtex->dcc_buffer) {
463 si_blit_decompress_color(ctx, rtex, level, level,
464 first_layer, last_layer);
465 }
466 }
467
468 struct texture_orig_info {
469 unsigned format;
470 unsigned width0;
471 unsigned height0;
472 unsigned npix_x;
473 unsigned npix_y;
474 unsigned npix0_x;
475 unsigned npix0_y;
476 };
477
478 void si_resource_copy_region(struct pipe_context *ctx,
479 struct pipe_resource *dst,
480 unsigned dst_level,
481 unsigned dstx, unsigned dsty, unsigned dstz,
482 struct pipe_resource *src,
483 unsigned src_level,
484 const struct pipe_box *src_box)
485 {
486 struct si_context *sctx = (struct si_context *)ctx;
487 struct pipe_surface *dst_view, dst_templ;
488 struct pipe_sampler_view src_templ, *src_view;
489 unsigned dst_width, dst_height, src_width0, src_height0;
490 unsigned src_force_level = 0;
491 struct pipe_box sbox, dstbox;
492
493 /* Handle buffers first. */
494 if (dst->target == PIPE_BUFFER && src->target == PIPE_BUFFER) {
495 si_copy_buffer(sctx, dst, src, dstx, src_box->x, src_box->width, false);
496 return;
497 }
498
499 assert(u_max_sample(dst) == u_max_sample(src));
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 dst_width = u_minify(dst->width0, dst_level);
507 dst_height = u_minify(dst->height0, dst_level);
508 src_width0 = src->width0;
509 src_height0 = src->height0;
510
511 util_blitter_default_dst_texture(&dst_templ, dst, dst_level, dstz);
512 util_blitter_default_src_texture(&src_templ, src, src_level);
513
514 if (util_format_is_compressed(src->format) ||
515 util_format_is_compressed(dst->format)) {
516 unsigned blocksize = util_format_get_blocksize(src->format);
517
518 if (blocksize == 8)
519 src_templ.format = PIPE_FORMAT_R16G16B16A16_UINT; /* 64-bit block */
520 else
521 src_templ.format = PIPE_FORMAT_R32G32B32A32_UINT; /* 128-bit block */
522 dst_templ.format = src_templ.format;
523
524 dst_width = util_format_get_nblocksx(dst->format, dst_width);
525 dst_height = util_format_get_nblocksy(dst->format, dst_height);
526 src_width0 = util_format_get_nblocksx(src->format, src_width0);
527 src_height0 = util_format_get_nblocksy(src->format, src_height0);
528
529 dstx = util_format_get_nblocksx(dst->format, dstx);
530 dsty = util_format_get_nblocksy(dst->format, dsty);
531
532 sbox.x = util_format_get_nblocksx(src->format, src_box->x);
533 sbox.y = util_format_get_nblocksy(src->format, src_box->y);
534 sbox.z = src_box->z;
535 sbox.width = util_format_get_nblocksx(src->format, src_box->width);
536 sbox.height = util_format_get_nblocksy(src->format, src_box->height);
537 sbox.depth = src_box->depth;
538 src_box = &sbox;
539
540 src_force_level = src_level;
541 } else if (!util_blitter_is_copy_supported(sctx->blitter, dst, src) ||
542 /* also *8_SNORM has precision issues, use UNORM instead */
543 util_format_is_snorm8(src->format)) {
544 if (util_format_is_subsampled_422(src->format)) {
545 src_templ.format = PIPE_FORMAT_R8G8B8A8_UINT;
546 dst_templ.format = PIPE_FORMAT_R8G8B8A8_UINT;
547
548 dst_width = util_format_get_nblocksx(dst->format, dst_width);
549 src_width0 = util_format_get_nblocksx(src->format, src_width0);
550
551 dstx = util_format_get_nblocksx(dst->format, dstx);
552
553 sbox = *src_box;
554 sbox.x = util_format_get_nblocksx(src->format, src_box->x);
555 sbox.width = util_format_get_nblocksx(src->format, src_box->width);
556 src_box = &sbox;
557 } else {
558 unsigned blocksize = util_format_get_blocksize(src->format);
559
560 switch (blocksize) {
561 case 1:
562 dst_templ.format = PIPE_FORMAT_R8_UNORM;
563 src_templ.format = PIPE_FORMAT_R8_UNORM;
564 break;
565 case 2:
566 dst_templ.format = PIPE_FORMAT_R8G8_UNORM;
567 src_templ.format = PIPE_FORMAT_R8G8_UNORM;
568 break;
569 case 4:
570 dst_templ.format = PIPE_FORMAT_R8G8B8A8_UNORM;
571 src_templ.format = PIPE_FORMAT_R8G8B8A8_UNORM;
572 break;
573 case 8:
574 dst_templ.format = PIPE_FORMAT_R16G16B16A16_UINT;
575 src_templ.format = PIPE_FORMAT_R16G16B16A16_UINT;
576 break;
577 case 16:
578 dst_templ.format = PIPE_FORMAT_R32G32B32A32_UINT;
579 src_templ.format = PIPE_FORMAT_R32G32B32A32_UINT;
580 break;
581 default:
582 fprintf(stderr, "Unhandled format %s with blocksize %u\n",
583 util_format_short_name(src->format), blocksize);
584 assert(0);
585 }
586 }
587 }
588
589 /* Initialize the surface. */
590 dst_view = r600_create_surface_custom(ctx, dst, &dst_templ,
591 dst_width, dst_height);
592
593 /* Initialize the sampler view. */
594 src_view = si_create_sampler_view_custom(ctx, src, &src_templ,
595 src_width0, src_height0,
596 src_force_level);
597
598 u_box_3d(dstx, dsty, dstz, abs(src_box->width), abs(src_box->height),
599 abs(src_box->depth), &dstbox);
600
601 /* Copy. */
602 si_blitter_begin(ctx, SI_COPY);
603 util_blitter_blit_generic(sctx->blitter, dst_view, &dstbox,
604 src_view, src_box, src_width0, src_height0,
605 PIPE_MASK_RGBAZS, PIPE_TEX_FILTER_NEAREST, NULL,
606 FALSE);
607 si_blitter_end(ctx);
608
609 pipe_surface_reference(&dst_view, NULL);
610 pipe_sampler_view_reference(&src_view, NULL);
611 }
612
613 /* For MSAA integer resolving to work, we change the format to NORM using this function. */
614 static enum pipe_format int_to_norm_format(enum pipe_format format)
615 {
616 switch (format) {
617 #define REPLACE_FORMAT_SIGN(format,sign) \
618 case PIPE_FORMAT_##format##_##sign##INT: \
619 return PIPE_FORMAT_##format##_##sign##NORM
620 #define REPLACE_FORMAT(format) \
621 REPLACE_FORMAT_SIGN(format, U); \
622 REPLACE_FORMAT_SIGN(format, S)
623
624 REPLACE_FORMAT_SIGN(B10G10R10A2, U);
625 REPLACE_FORMAT(R8);
626 REPLACE_FORMAT(R8G8);
627 REPLACE_FORMAT(R8G8B8X8);
628 REPLACE_FORMAT(R8G8B8A8);
629 REPLACE_FORMAT(A8);
630 REPLACE_FORMAT(I8);
631 REPLACE_FORMAT(L8);
632 REPLACE_FORMAT(L8A8);
633 REPLACE_FORMAT(R16);
634 REPLACE_FORMAT(R16G16);
635 REPLACE_FORMAT(R16G16B16X16);
636 REPLACE_FORMAT(R16G16B16A16);
637 REPLACE_FORMAT(A16);
638 REPLACE_FORMAT(I16);
639 REPLACE_FORMAT(L16);
640 REPLACE_FORMAT(L16A16);
641
642 #undef REPLACE_FORMAT
643 #undef REPLACE_FORMAT_SIGN
644 default:
645 return format;
646 }
647 }
648
649 static bool do_hardware_msaa_resolve(struct pipe_context *ctx,
650 const struct pipe_blit_info *info)
651 {
652 struct si_context *sctx = (struct si_context*)ctx;
653 struct r600_texture *dst = (struct r600_texture*)info->dst.resource;
654 unsigned dst_width = u_minify(info->dst.resource->width0, info->dst.level);
655 unsigned dst_height = u_minify(info->dst.resource->height0, info->dst.level);
656 enum pipe_format format = int_to_norm_format(info->dst.format);
657 unsigned sample_mask = ~0;
658
659 if (info->src.resource->nr_samples > 1 &&
660 info->dst.resource->nr_samples <= 1 &&
661 util_max_layer(info->src.resource, 0) == 0 &&
662 util_max_layer(info->dst.resource, info->dst.level) == 0 &&
663 info->dst.format == info->src.format &&
664 !util_format_is_pure_integer(format) &&
665 !util_format_is_depth_or_stencil(format) &&
666 !info->scissor_enable &&
667 (info->mask & PIPE_MASK_RGBA) == PIPE_MASK_RGBA &&
668 dst_width == info->src.resource->width0 &&
669 dst_height == info->src.resource->height0 &&
670 info->dst.box.x == 0 &&
671 info->dst.box.y == 0 &&
672 info->dst.box.width == dst_width &&
673 info->dst.box.height == dst_height &&
674 info->dst.box.depth == 1 &&
675 info->src.box.x == 0 &&
676 info->src.box.y == 0 &&
677 info->src.box.width == dst_width &&
678 info->src.box.height == dst_height &&
679 info->src.box.depth == 1 &&
680 dst->surface.level[info->dst.level].mode >= RADEON_SURF_MODE_1D &&
681 !(dst->surface.flags & RADEON_SURF_SCANOUT) &&
682 (!dst->cmask.size || !dst->dirty_level_mask) && /* dst cannot be fast-cleared */
683 !dst->dcc_buffer) {
684 si_blitter_begin(ctx, SI_COLOR_RESOLVE |
685 (info->render_condition_enable ? 0 : SI_DISABLE_RENDER_COND));
686 util_blitter_custom_resolve_color(sctx->blitter,
687 info->dst.resource, info->dst.level,
688 info->dst.box.z,
689 info->src.resource, info->src.box.z,
690 sample_mask, sctx->custom_blend_resolve,
691 format);
692 si_blitter_end(ctx);
693 return true;
694 }
695 return false;
696 }
697
698 static void si_blit(struct pipe_context *ctx,
699 const struct pipe_blit_info *info)
700 {
701 struct si_context *sctx = (struct si_context*)ctx;
702
703 if (do_hardware_msaa_resolve(ctx, info)) {
704 return;
705 }
706
707 assert(util_blitter_is_blit_supported(sctx->blitter, info));
708
709 /* The driver doesn't decompress resources automatically while
710 * u_blitter is rendering. */
711 si_decompress_subresource(ctx, info->src.resource, info->src.level,
712 info->src.box.z,
713 info->src.box.z + info->src.box.depth - 1);
714
715 if (sctx->screen->b.debug_flags & DBG_FORCE_DMA &&
716 util_try_blit_via_copy_region(ctx, info))
717 return;
718
719 si_blitter_begin(ctx, SI_BLIT |
720 (info->render_condition_enable ? 0 : SI_DISABLE_RENDER_COND));
721 util_blitter_blit(sctx->blitter, info);
722 si_blitter_end(ctx);
723 }
724
725 static void si_flush_resource(struct pipe_context *ctx,
726 struct pipe_resource *res)
727 {
728 struct r600_texture *rtex = (struct r600_texture*)res;
729
730 assert(res->target != PIPE_BUFFER);
731
732 if (!rtex->is_depth && rtex->cmask.size) {
733 si_blit_decompress_color(ctx, rtex, 0, res->last_level,
734 0, util_max_layer(res, 0));
735 }
736 }
737
738 static void si_pipe_clear_buffer(struct pipe_context *ctx,
739 struct pipe_resource *dst,
740 unsigned offset, unsigned size,
741 const void *clear_value_ptr,
742 int clear_value_size)
743 {
744 struct si_context *sctx = (struct si_context*)ctx;
745 uint32_t dword_value;
746 unsigned i;
747
748 assert(offset % clear_value_size == 0);
749 assert(size % clear_value_size == 0);
750
751 if (clear_value_size > 4) {
752 const uint32_t *u32 = clear_value_ptr;
753 bool clear_dword_duplicated = true;
754
755 /* See if we can lower large fills to dword fills. */
756 for (i = 1; i < clear_value_size / 4; i++)
757 if (u32[0] != u32[i]) {
758 clear_dword_duplicated = false;
759 break;
760 }
761
762 if (!clear_dword_duplicated) {
763 /* Use transform feedback for 64-bit, 96-bit, and
764 * 128-bit fills.
765 */
766 union pipe_color_union clear_value;
767
768 memcpy(&clear_value, clear_value_ptr, clear_value_size);
769 si_blitter_begin(ctx, SI_DISABLE_RENDER_COND);
770 util_blitter_clear_buffer(sctx->blitter, dst, offset,
771 size, clear_value_size / 4,
772 &clear_value);
773 si_blitter_end(ctx);
774 return;
775 }
776 }
777
778 /* Expand the clear value to a dword. */
779 switch (clear_value_size) {
780 case 1:
781 dword_value = *(uint8_t*)clear_value_ptr;
782 dword_value |= (dword_value << 8) |
783 (dword_value << 16) |
784 (dword_value << 24);
785 break;
786 case 2:
787 dword_value = *(uint16_t*)clear_value_ptr;
788 dword_value |= dword_value << 16;
789 break;
790 default:
791 dword_value = *(uint32_t*)clear_value_ptr;
792 }
793
794 sctx->b.clear_buffer(ctx, dst, offset, size, dword_value, false);
795 }
796
797 void si_init_blit_functions(struct si_context *sctx)
798 {
799 sctx->b.b.clear = si_clear;
800 sctx->b.b.clear_buffer = si_pipe_clear_buffer;
801 sctx->b.b.clear_render_target = si_clear_render_target;
802 sctx->b.b.clear_depth_stencil = si_clear_depth_stencil;
803 sctx->b.b.resource_copy_region = si_resource_copy_region;
804 sctx->b.b.blit = si_blit;
805 sctx->b.b.flush_resource = si_flush_resource;
806 sctx->b.blit_decompress_depth = si_blit_decompress_depth;
807 }