2 * Copyright 2010 Jerome Glisse <glisse@freedesktop.org>
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:
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
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.
25 #include "si_compute.h"
26 #include "util/u_format.h"
27 #include "util/u_log.h"
28 #include "util/u_surface.h"
30 enum si_blitter_op
/* bitmask */
33 SI_SAVE_FRAMEBUFFER
= 2,
34 SI_SAVE_FRAGMENT_STATE
= 4,
35 SI_DISABLE_RENDER_COND
= 8,
37 SI_CLEAR
= SI_SAVE_FRAGMENT_STATE
,
39 SI_CLEAR_SURFACE
= SI_SAVE_FRAMEBUFFER
| SI_SAVE_FRAGMENT_STATE
,
41 SI_COPY
= SI_SAVE_FRAMEBUFFER
| SI_SAVE_TEXTURES
|
42 SI_SAVE_FRAGMENT_STATE
| SI_DISABLE_RENDER_COND
,
44 SI_BLIT
= SI_SAVE_FRAMEBUFFER
| SI_SAVE_TEXTURES
|
45 SI_SAVE_FRAGMENT_STATE
,
47 SI_DECOMPRESS
= SI_SAVE_FRAMEBUFFER
| SI_SAVE_FRAGMENT_STATE
|
48 SI_DISABLE_RENDER_COND
,
50 SI_COLOR_RESOLVE
= SI_SAVE_FRAMEBUFFER
| SI_SAVE_FRAGMENT_STATE
53 static void si_blitter_begin(struct pipe_context
*ctx
, enum si_blitter_op op
)
55 struct si_context
*sctx
= (struct si_context
*)ctx
;
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
);
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]);
77 if (op
& SI_SAVE_FRAMEBUFFER
)
78 util_blitter_save_framebuffer(sctx
->blitter
, &sctx
->framebuffer
.state
);
80 if (op
& SI_SAVE_TEXTURES
) {
81 util_blitter_save_fragment_sampler_states(
83 (void**)sctx
->samplers
[PIPE_SHADER_FRAGMENT
].sampler_states
);
85 util_blitter_save_fragment_sampler_views(sctx
->blitter
, 2,
86 sctx
->samplers
[PIPE_SHADER_FRAGMENT
].views
);
89 if (op
& SI_DISABLE_RENDER_COND
)
90 sctx
->b
.render_cond_force_off
= true;
93 static void si_blitter_end(struct pipe_context
*ctx
)
95 struct si_context
*sctx
= (struct si_context
*)ctx
;
97 sctx
->b
.render_cond_force_off
= false;
99 /* Restore shader pointers because the VS blit shader changed all
100 * non-global VS user SGPRs. */
101 sctx
->shader_pointers_dirty
|= SI_VS_SHADER_POINTER_MASK
;
102 sctx
->vertex_buffer_pointer_dirty
= true;
103 si_mark_atom_dirty(sctx
, &sctx
->shader_pointers
.atom
);
106 static unsigned u_max_sample(struct pipe_resource
*r
)
108 return r
->nr_samples
? r
->nr_samples
- 1 : 0;
112 si_blit_dbcb_copy(struct si_context
*sctx
,
113 struct r600_texture
*src
,
114 struct r600_texture
*dst
,
115 unsigned planes
, unsigned level_mask
,
116 unsigned first_layer
, unsigned last_layer
,
117 unsigned first_sample
, unsigned last_sample
)
119 struct pipe_surface surf_tmpl
= {{0}};
120 unsigned layer
, sample
, checked_last_layer
, max_layer
;
121 unsigned fully_copied_levels
= 0;
123 if (planes
& PIPE_MASK_Z
)
124 sctx
->dbcb_depth_copy_enabled
= true;
125 if (planes
& PIPE_MASK_S
)
126 sctx
->dbcb_stencil_copy_enabled
= true;
127 si_mark_atom_dirty(sctx
, &sctx
->db_render_state
);
129 assert(sctx
->dbcb_depth_copy_enabled
|| sctx
->dbcb_stencil_copy_enabled
);
131 sctx
->decompression_enabled
= true;
134 unsigned level
= u_bit_scan(&level_mask
);
136 /* The smaller the mipmap level, the less layers there are
137 * as far as 3D textures are concerned. */
138 max_layer
= util_max_layer(&src
->resource
.b
.b
, level
);
139 checked_last_layer
= MIN2(last_layer
, max_layer
);
141 surf_tmpl
.u
.tex
.level
= level
;
143 for (layer
= first_layer
; layer
<= checked_last_layer
; layer
++) {
144 struct pipe_surface
*zsurf
, *cbsurf
;
146 surf_tmpl
.format
= src
->resource
.b
.b
.format
;
147 surf_tmpl
.u
.tex
.first_layer
= layer
;
148 surf_tmpl
.u
.tex
.last_layer
= layer
;
150 zsurf
= sctx
->b
.b
.create_surface(&sctx
->b
.b
, &src
->resource
.b
.b
, &surf_tmpl
);
152 surf_tmpl
.format
= dst
->resource
.b
.b
.format
;
153 cbsurf
= sctx
->b
.b
.create_surface(&sctx
->b
.b
, &dst
->resource
.b
.b
, &surf_tmpl
);
155 for (sample
= first_sample
; sample
<= last_sample
; sample
++) {
156 if (sample
!= sctx
->dbcb_copy_sample
) {
157 sctx
->dbcb_copy_sample
= sample
;
158 si_mark_atom_dirty(sctx
, &sctx
->db_render_state
);
161 si_blitter_begin(&sctx
->b
.b
, SI_DECOMPRESS
);
162 util_blitter_custom_depth_stencil(sctx
->blitter
, zsurf
, cbsurf
, 1 << sample
,
163 sctx
->custom_dsa_flush
, 1.0f
);
164 si_blitter_end(&sctx
->b
.b
);
167 pipe_surface_reference(&zsurf
, NULL
);
168 pipe_surface_reference(&cbsurf
, NULL
);
171 if (first_layer
== 0 && last_layer
>= max_layer
&&
172 first_sample
== 0 && last_sample
>= u_max_sample(&src
->resource
.b
.b
))
173 fully_copied_levels
|= 1u << level
;
176 sctx
->decompression_enabled
= false;
177 sctx
->dbcb_depth_copy_enabled
= false;
178 sctx
->dbcb_stencil_copy_enabled
= false;
179 si_mark_atom_dirty(sctx
, &sctx
->db_render_state
);
181 return fully_copied_levels
;
184 static void si_blit_decompress_depth(struct pipe_context
*ctx
,
185 struct r600_texture
*texture
,
186 struct r600_texture
*staging
,
187 unsigned first_level
, unsigned last_level
,
188 unsigned first_layer
, unsigned last_layer
,
189 unsigned first_sample
, unsigned last_sample
)
191 const struct util_format_description
*desc
;
194 assert(staging
!= NULL
&& "use si_blit_decompress_zs_in_place instead");
196 desc
= util_format_description(staging
->resource
.b
.b
.format
);
198 if (util_format_has_depth(desc
))
199 planes
|= PIPE_MASK_Z
;
200 if (util_format_has_stencil(desc
))
201 planes
|= PIPE_MASK_S
;
204 (struct si_context
*)ctx
, texture
, staging
, planes
,
205 u_bit_consecutive(first_level
, last_level
- first_level
+ 1),
206 first_layer
, last_layer
, first_sample
, last_sample
);
209 /* Helper function for si_blit_decompress_zs_in_place.
212 si_blit_decompress_zs_planes_in_place(struct si_context
*sctx
,
213 struct r600_texture
*texture
,
214 unsigned planes
, unsigned level_mask
,
215 unsigned first_layer
, unsigned last_layer
)
217 struct pipe_surface
*zsurf
, surf_tmpl
= {{0}};
218 unsigned layer
, max_layer
, checked_last_layer
;
219 unsigned fully_decompressed_mask
= 0;
224 if (planes
& PIPE_MASK_S
)
225 sctx
->db_flush_stencil_inplace
= true;
226 if (planes
& PIPE_MASK_Z
)
227 sctx
->db_flush_depth_inplace
= true;
228 si_mark_atom_dirty(sctx
, &sctx
->db_render_state
);
230 surf_tmpl
.format
= texture
->resource
.b
.b
.format
;
232 sctx
->decompression_enabled
= true;
235 unsigned level
= u_bit_scan(&level_mask
);
237 surf_tmpl
.u
.tex
.level
= level
;
239 /* The smaller the mipmap level, the less layers there are
240 * as far as 3D textures are concerned. */
241 max_layer
= util_max_layer(&texture
->resource
.b
.b
, level
);
242 checked_last_layer
= MIN2(last_layer
, max_layer
);
244 for (layer
= first_layer
; layer
<= checked_last_layer
; layer
++) {
245 surf_tmpl
.u
.tex
.first_layer
= layer
;
246 surf_tmpl
.u
.tex
.last_layer
= layer
;
248 zsurf
= sctx
->b
.b
.create_surface(&sctx
->b
.b
, &texture
->resource
.b
.b
, &surf_tmpl
);
250 si_blitter_begin(&sctx
->b
.b
, SI_DECOMPRESS
);
251 util_blitter_custom_depth_stencil(sctx
->blitter
, zsurf
, NULL
, ~0,
252 sctx
->custom_dsa_flush
,
254 si_blitter_end(&sctx
->b
.b
);
256 pipe_surface_reference(&zsurf
, NULL
);
259 /* The texture will always be dirty if some layers aren't flushed.
260 * I don't think this case occurs often though. */
261 if (first_layer
== 0 && last_layer
>= max_layer
) {
262 fully_decompressed_mask
|= 1u << level
;
266 if (planes
& PIPE_MASK_Z
)
267 texture
->dirty_level_mask
&= ~fully_decompressed_mask
;
268 if (planes
& PIPE_MASK_S
)
269 texture
->stencil_dirty_level_mask
&= ~fully_decompressed_mask
;
271 sctx
->decompression_enabled
= false;
272 sctx
->db_flush_depth_inplace
= false;
273 sctx
->db_flush_stencil_inplace
= false;
274 si_mark_atom_dirty(sctx
, &sctx
->db_render_state
);
277 /* Helper function of si_flush_depth_texture: decompress the given levels
278 * of Z and/or S planes in place.
281 si_blit_decompress_zs_in_place(struct si_context
*sctx
,
282 struct r600_texture
*texture
,
283 unsigned levels_z
, unsigned levels_s
,
284 unsigned first_layer
, unsigned last_layer
)
286 unsigned both
= levels_z
& levels_s
;
288 /* First, do combined Z & S decompresses for levels that need it. */
290 si_blit_decompress_zs_planes_in_place(
291 sctx
, texture
, PIPE_MASK_Z
| PIPE_MASK_S
,
293 first_layer
, last_layer
);
298 /* Now do separate Z and S decompresses. */
300 si_blit_decompress_zs_planes_in_place(
301 sctx
, texture
, PIPE_MASK_Z
,
303 first_layer
, last_layer
);
307 si_blit_decompress_zs_planes_in_place(
308 sctx
, texture
, PIPE_MASK_S
,
310 first_layer
, last_layer
);
315 si_decompress_depth(struct si_context
*sctx
,
316 struct r600_texture
*tex
,
317 unsigned required_planes
,
318 unsigned first_level
, unsigned last_level
,
319 unsigned first_layer
, unsigned last_layer
)
321 unsigned inplace_planes
= 0;
322 unsigned copy_planes
= 0;
323 unsigned level_mask
= u_bit_consecutive(first_level
, last_level
- first_level
+ 1);
324 unsigned levels_z
= 0;
325 unsigned levels_s
= 0;
327 if (required_planes
& PIPE_MASK_Z
) {
328 levels_z
= level_mask
& tex
->dirty_level_mask
;
331 if (r600_can_sample_zs(tex
, false))
332 inplace_planes
|= PIPE_MASK_Z
;
334 copy_planes
|= PIPE_MASK_Z
;
337 if (required_planes
& PIPE_MASK_S
) {
338 levels_s
= level_mask
& tex
->stencil_dirty_level_mask
;
341 if (r600_can_sample_zs(tex
, true))
342 inplace_planes
|= PIPE_MASK_S
;
344 copy_planes
|= PIPE_MASK_S
;
348 if (unlikely(sctx
->b
.log
))
349 u_log_printf(sctx
->b
.log
,
350 "\n------------------------------------------------\n"
351 "Decompress Depth (levels %u - %u, levels Z: 0x%x S: 0x%x)\n\n",
352 first_level
, last_level
, levels_z
, levels_s
);
354 /* We may have to allocate the flushed texture here when called from
355 * si_decompress_subresource.
358 (tex
->flushed_depth_texture
||
359 si_init_flushed_depth_texture(&sctx
->b
.b
, &tex
->resource
.b
.b
, NULL
))) {
360 struct r600_texture
*dst
= tex
->flushed_depth_texture
;
361 unsigned fully_copied_levels
;
364 assert(tex
->flushed_depth_texture
);
366 if (util_format_is_depth_and_stencil(dst
->resource
.b
.b
.format
))
367 copy_planes
= PIPE_MASK_Z
| PIPE_MASK_S
;
369 if (copy_planes
& PIPE_MASK_Z
) {
373 if (copy_planes
& PIPE_MASK_S
) {
378 fully_copied_levels
= si_blit_dbcb_copy(
379 sctx
, tex
, dst
, copy_planes
, levels
,
380 first_layer
, last_layer
,
381 0, u_max_sample(&tex
->resource
.b
.b
));
383 if (copy_planes
& PIPE_MASK_Z
)
384 tex
->dirty_level_mask
&= ~fully_copied_levels
;
385 if (copy_planes
& PIPE_MASK_S
)
386 tex
->stencil_dirty_level_mask
&= ~fully_copied_levels
;
389 if (inplace_planes
) {
390 bool has_htile
= r600_htile_enabled(tex
, first_level
);
391 bool tc_compat_htile
= vi_tc_compat_htile_enabled(tex
, first_level
);
393 /* Don't decompress if there is no HTILE or when HTILE is
395 if (has_htile
&& !tc_compat_htile
) {
396 si_blit_decompress_zs_in_place(
399 first_layer
, last_layer
);
401 /* This is only a cache flush.
403 * Only clear the mask that we are flushing, because
404 * si_make_DB_shader_coherent() treats different levels
405 * and depth and stencil differently.
407 if (inplace_planes
& PIPE_MASK_Z
)
408 tex
->dirty_level_mask
&= ~levels_z
;
409 if (inplace_planes
& PIPE_MASK_S
)
410 tex
->stencil_dirty_level_mask
&= ~levels_s
;
413 /* Only in-place decompression needs to flush DB caches, or
414 * when we don't decompress but TC-compatible planes are dirty.
416 si_make_DB_shader_coherent(sctx
, tex
->resource
.b
.b
.nr_samples
,
417 inplace_planes
& PIPE_MASK_S
,
420 /* set_framebuffer_state takes care of coherency for single-sample.
421 * The DB->CB copy uses CB for the final writes.
423 if (copy_planes
&& tex
->resource
.b
.b
.nr_samples
> 1)
424 si_make_CB_shader_coherent(sctx
, tex
->resource
.b
.b
.nr_samples
,
429 si_decompress_sampler_depth_textures(struct si_context
*sctx
,
430 struct si_samplers
*textures
)
433 unsigned mask
= textures
->needs_depth_decompress_mask
;
436 struct pipe_sampler_view
*view
;
437 struct si_sampler_view
*sview
;
438 struct r600_texture
*tex
;
440 i
= u_bit_scan(&mask
);
442 view
= textures
->views
[i
];
444 sview
= (struct si_sampler_view
*)view
;
446 tex
= (struct r600_texture
*)view
->texture
;
447 assert(tex
->db_compatible
);
449 si_decompress_depth(sctx
, tex
,
450 sview
->is_stencil_sampler
? PIPE_MASK_S
: PIPE_MASK_Z
,
451 view
->u
.tex
.first_level
, view
->u
.tex
.last_level
,
452 0, util_max_layer(&tex
->resource
.b
.b
, view
->u
.tex
.first_level
));
456 static void si_blit_decompress_color(struct pipe_context
*ctx
,
457 struct r600_texture
*rtex
,
458 unsigned first_level
, unsigned last_level
,
459 unsigned first_layer
, unsigned last_layer
,
460 bool need_dcc_decompress
)
462 struct si_context
*sctx
= (struct si_context
*)ctx
;
464 unsigned layer
, checked_last_layer
, max_layer
;
465 unsigned level_mask
=
466 u_bit_consecutive(first_level
, last_level
- first_level
+ 1);
468 if (!need_dcc_decompress
)
469 level_mask
&= rtex
->dirty_level_mask
;
473 if (unlikely(sctx
->b
.log
))
474 u_log_printf(sctx
->b
.log
,
475 "\n------------------------------------------------\n"
476 "Decompress Color (levels %u - %u, mask 0x%x)\n\n",
477 first_level
, last_level
, level_mask
);
479 if (need_dcc_decompress
) {
480 custom_blend
= sctx
->custom_blend_dcc_decompress
;
482 assert(rtex
->dcc_offset
);
484 /* disable levels without DCC */
485 for (int i
= first_level
; i
<= last_level
; i
++) {
486 if (!vi_dcc_enabled(rtex
, i
))
487 level_mask
&= ~(1 << i
);
489 } else if (rtex
->fmask
.size
) {
490 custom_blend
= sctx
->custom_blend_fmask_decompress
;
492 custom_blend
= sctx
->custom_blend_eliminate_fastclear
;
495 sctx
->decompression_enabled
= true;
498 unsigned level
= u_bit_scan(&level_mask
);
500 /* The smaller the mipmap level, the less layers there are
501 * as far as 3D textures are concerned. */
502 max_layer
= util_max_layer(&rtex
->resource
.b
.b
, level
);
503 checked_last_layer
= MIN2(last_layer
, max_layer
);
505 for (layer
= first_layer
; layer
<= checked_last_layer
; layer
++) {
506 struct pipe_surface
*cbsurf
, surf_tmpl
;
508 surf_tmpl
.format
= rtex
->resource
.b
.b
.format
;
509 surf_tmpl
.u
.tex
.level
= level
;
510 surf_tmpl
.u
.tex
.first_layer
= layer
;
511 surf_tmpl
.u
.tex
.last_layer
= layer
;
512 cbsurf
= ctx
->create_surface(ctx
, &rtex
->resource
.b
.b
, &surf_tmpl
);
514 /* Required before and after FMASK and DCC_DECOMPRESS. */
515 if (custom_blend
== sctx
->custom_blend_fmask_decompress
||
516 custom_blend
== sctx
->custom_blend_dcc_decompress
)
517 sctx
->b
.flags
|= SI_CONTEXT_FLUSH_AND_INV_CB
;
519 si_blitter_begin(ctx
, SI_DECOMPRESS
);
520 util_blitter_custom_color(sctx
->blitter
, cbsurf
, custom_blend
);
523 if (custom_blend
== sctx
->custom_blend_fmask_decompress
||
524 custom_blend
== sctx
->custom_blend_dcc_decompress
)
525 sctx
->b
.flags
|= SI_CONTEXT_FLUSH_AND_INV_CB
;
527 pipe_surface_reference(&cbsurf
, NULL
);
530 /* The texture will always be dirty if some layers aren't flushed.
531 * I don't think this case occurs often though. */
532 if (first_layer
== 0 && last_layer
>= max_layer
) {
533 rtex
->dirty_level_mask
&= ~(1 << level
);
537 sctx
->decompression_enabled
= false;
538 si_make_CB_shader_coherent(sctx
, rtex
->resource
.b
.b
.nr_samples
,
539 vi_dcc_enabled(rtex
, first_level
));
543 si_decompress_color_texture(struct si_context
*sctx
, struct r600_texture
*tex
,
544 unsigned first_level
, unsigned last_level
)
546 /* CMASK or DCC can be discarded and we can still end up here. */
547 if (!tex
->cmask
.size
&& !tex
->fmask
.size
&& !tex
->dcc_offset
)
550 si_blit_decompress_color(&sctx
->b
.b
, tex
, first_level
, last_level
, 0,
551 util_max_layer(&tex
->resource
.b
.b
, first_level
),
556 si_decompress_sampler_color_textures(struct si_context
*sctx
,
557 struct si_samplers
*textures
)
560 unsigned mask
= textures
->needs_color_decompress_mask
;
563 struct pipe_sampler_view
*view
;
564 struct r600_texture
*tex
;
566 i
= u_bit_scan(&mask
);
568 view
= textures
->views
[i
];
571 tex
= (struct r600_texture
*)view
->texture
;
573 si_decompress_color_texture(sctx
, tex
, view
->u
.tex
.first_level
,
574 view
->u
.tex
.last_level
);
579 si_decompress_image_color_textures(struct si_context
*sctx
,
580 struct si_images
*images
)
583 unsigned mask
= images
->needs_color_decompress_mask
;
586 const struct pipe_image_view
*view
;
587 struct r600_texture
*tex
;
589 i
= u_bit_scan(&mask
);
591 view
= &images
->views
[i
];
592 assert(view
->resource
->target
!= PIPE_BUFFER
);
594 tex
= (struct r600_texture
*)view
->resource
;
596 si_decompress_color_texture(sctx
, tex
, view
->u
.tex
.level
,
601 static void si_check_render_feedback_texture(struct si_context
*sctx
,
602 struct r600_texture
*tex
,
603 unsigned first_level
,
605 unsigned first_layer
,
608 bool render_feedback
= false;
610 if (!tex
->dcc_offset
)
613 for (unsigned j
= 0; j
< sctx
->framebuffer
.state
.nr_cbufs
; ++j
) {
614 struct r600_surface
* surf
;
616 if (!sctx
->framebuffer
.state
.cbufs
[j
])
619 surf
= (struct r600_surface
*)sctx
->framebuffer
.state
.cbufs
[j
];
621 if (tex
== (struct r600_texture
*)surf
->base
.texture
&&
622 surf
->base
.u
.tex
.level
>= first_level
&&
623 surf
->base
.u
.tex
.level
<= last_level
&&
624 surf
->base
.u
.tex
.first_layer
<= last_layer
&&
625 surf
->base
.u
.tex
.last_layer
>= first_layer
) {
626 render_feedback
= true;
632 si_texture_disable_dcc(&sctx
->b
, tex
);
635 static void si_check_render_feedback_textures(struct si_context
*sctx
,
636 struct si_samplers
*textures
)
638 uint32_t mask
= textures
->enabled_mask
;
641 const struct pipe_sampler_view
*view
;
642 struct r600_texture
*tex
;
644 unsigned i
= u_bit_scan(&mask
);
646 view
= textures
->views
[i
];
647 if(view
->texture
->target
== PIPE_BUFFER
)
650 tex
= (struct r600_texture
*)view
->texture
;
652 si_check_render_feedback_texture(sctx
, tex
,
653 view
->u
.tex
.first_level
,
654 view
->u
.tex
.last_level
,
655 view
->u
.tex
.first_layer
,
656 view
->u
.tex
.last_layer
);
660 static void si_check_render_feedback_images(struct si_context
*sctx
,
661 struct si_images
*images
)
663 uint32_t mask
= images
->enabled_mask
;
666 const struct pipe_image_view
*view
;
667 struct r600_texture
*tex
;
669 unsigned i
= u_bit_scan(&mask
);
671 view
= &images
->views
[i
];
672 if (view
->resource
->target
== PIPE_BUFFER
)
675 tex
= (struct r600_texture
*)view
->resource
;
677 si_check_render_feedback_texture(sctx
, tex
,
680 view
->u
.tex
.first_layer
,
681 view
->u
.tex
.last_layer
);
685 static void si_check_render_feedback_resident_textures(struct si_context
*sctx
)
687 util_dynarray_foreach(&sctx
->resident_tex_handles
,
688 struct si_texture_handle
*, tex_handle
) {
689 struct pipe_sampler_view
*view
;
690 struct r600_texture
*tex
;
692 view
= (*tex_handle
)->view
;
693 if (view
->texture
->target
== PIPE_BUFFER
)
696 tex
= (struct r600_texture
*)view
->texture
;
698 si_check_render_feedback_texture(sctx
, tex
,
699 view
->u
.tex
.first_level
,
700 view
->u
.tex
.last_level
,
701 view
->u
.tex
.first_layer
,
702 view
->u
.tex
.last_layer
);
706 static void si_check_render_feedback_resident_images(struct si_context
*sctx
)
708 util_dynarray_foreach(&sctx
->resident_img_handles
,
709 struct si_image_handle
*, img_handle
) {
710 struct pipe_image_view
*view
;
711 struct r600_texture
*tex
;
713 view
= &(*img_handle
)->view
;
714 if (view
->resource
->target
== PIPE_BUFFER
)
717 tex
= (struct r600_texture
*)view
->resource
;
719 si_check_render_feedback_texture(sctx
, tex
,
722 view
->u
.tex
.first_layer
,
723 view
->u
.tex
.last_layer
);
727 static void si_check_render_feedback(struct si_context
*sctx
)
730 if (!sctx
->need_check_render_feedback
)
733 for (int i
= 0; i
< SI_NUM_SHADERS
; ++i
) {
734 si_check_render_feedback_images(sctx
, &sctx
->images
[i
]);
735 si_check_render_feedback_textures(sctx
, &sctx
->samplers
[i
]);
738 si_check_render_feedback_resident_images(sctx
);
739 si_check_render_feedback_resident_textures(sctx
);
741 sctx
->need_check_render_feedback
= false;
744 static void si_decompress_resident_textures(struct si_context
*sctx
)
746 util_dynarray_foreach(&sctx
->resident_tex_needs_color_decompress
,
747 struct si_texture_handle
*, tex_handle
) {
748 struct pipe_sampler_view
*view
= (*tex_handle
)->view
;
749 struct r600_texture
*tex
= (struct r600_texture
*)view
->texture
;
751 si_decompress_color_texture(sctx
, tex
, view
->u
.tex
.first_level
,
752 view
->u
.tex
.last_level
);
755 util_dynarray_foreach(&sctx
->resident_tex_needs_depth_decompress
,
756 struct si_texture_handle
*, tex_handle
) {
757 struct pipe_sampler_view
*view
= (*tex_handle
)->view
;
758 struct si_sampler_view
*sview
= (struct si_sampler_view
*)view
;
759 struct r600_texture
*tex
= (struct r600_texture
*)view
->texture
;
761 si_decompress_depth(sctx
, tex
,
762 sview
->is_stencil_sampler
? PIPE_MASK_S
: PIPE_MASK_Z
,
763 view
->u
.tex
.first_level
, view
->u
.tex
.last_level
,
764 0, util_max_layer(&tex
->resource
.b
.b
, view
->u
.tex
.first_level
));
768 static void si_decompress_resident_images(struct si_context
*sctx
)
770 util_dynarray_foreach(&sctx
->resident_img_needs_color_decompress
,
771 struct si_image_handle
*, img_handle
) {
772 struct pipe_image_view
*view
= &(*img_handle
)->view
;
773 struct r600_texture
*tex
= (struct r600_texture
*)view
->resource
;
775 si_decompress_color_texture(sctx
, tex
, view
->u
.tex
.level
,
780 void si_decompress_textures(struct si_context
*sctx
, unsigned shader_mask
)
782 unsigned compressed_colortex_counter
, mask
;
784 if (sctx
->blitter
->running
)
787 /* Update the compressed_colortex_mask if necessary. */
788 compressed_colortex_counter
= p_atomic_read(&sctx
->screen
->b
.compressed_colortex_counter
);
789 if (compressed_colortex_counter
!= sctx
->b
.last_compressed_colortex_counter
) {
790 sctx
->b
.last_compressed_colortex_counter
= compressed_colortex_counter
;
791 si_update_needs_color_decompress_masks(sctx
);
794 /* Decompress color & depth textures if needed. */
795 mask
= sctx
->shader_needs_decompress_mask
& shader_mask
;
797 unsigned i
= u_bit_scan(&mask
);
799 if (sctx
->samplers
[i
].needs_depth_decompress_mask
) {
800 si_decompress_sampler_depth_textures(sctx
, &sctx
->samplers
[i
]);
802 if (sctx
->samplers
[i
].needs_color_decompress_mask
) {
803 si_decompress_sampler_color_textures(sctx
, &sctx
->samplers
[i
]);
805 if (sctx
->images
[i
].needs_color_decompress_mask
) {
806 si_decompress_image_color_textures(sctx
, &sctx
->images
[i
]);
810 if (shader_mask
& u_bit_consecutive(0, SI_NUM_GRAPHICS_SHADERS
)) {
811 if (sctx
->uses_bindless_samplers
)
812 si_decompress_resident_textures(sctx
);
813 if (sctx
->uses_bindless_images
)
814 si_decompress_resident_images(sctx
);
815 } else if (shader_mask
& (1 << PIPE_SHADER_COMPUTE
)) {
816 if (sctx
->cs_shader_state
.program
->uses_bindless_samplers
)
817 si_decompress_resident_textures(sctx
);
818 if (sctx
->cs_shader_state
.program
->uses_bindless_images
)
819 si_decompress_resident_images(sctx
);
822 si_check_render_feedback(sctx
);
825 static void si_clear(struct pipe_context
*ctx
, unsigned buffers
,
826 const union pipe_color_union
*color
,
827 double depth
, unsigned stencil
)
829 struct si_context
*sctx
= (struct si_context
*)ctx
;
830 struct pipe_framebuffer_state
*fb
= &sctx
->framebuffer
.state
;
831 struct pipe_surface
*zsbuf
= fb
->zsbuf
;
832 struct r600_texture
*zstex
=
833 zsbuf
? (struct r600_texture
*)zsbuf
->texture
: NULL
;
835 if (buffers
& PIPE_CLEAR_COLOR
) {
836 si_do_fast_color_clear(&sctx
->b
, fb
,
837 &sctx
->framebuffer
.atom
, &buffers
,
838 &sctx
->framebuffer
.dirty_cbufs
,
841 return; /* all buffers have been fast cleared */
844 if (buffers
& PIPE_CLEAR_COLOR
) {
847 /* These buffers cannot use fast clear, make sure to disable expansion. */
848 for (i
= 0; i
< fb
->nr_cbufs
; i
++) {
849 struct r600_texture
*tex
;
851 /* If not clearing this buffer, skip. */
852 if (!(buffers
& (PIPE_CLEAR_COLOR0
<< i
)))
858 tex
= (struct r600_texture
*)fb
->cbufs
[i
]->texture
;
859 if (tex
->fmask
.size
== 0)
860 tex
->dirty_level_mask
&= ~(1 << fb
->cbufs
[i
]->u
.tex
.level
);
865 r600_htile_enabled(zstex
, zsbuf
->u
.tex
.level
) &&
866 zsbuf
->u
.tex
.first_layer
== 0 &&
867 zsbuf
->u
.tex
.last_layer
== util_max_layer(&zstex
->resource
.b
.b
, 0)) {
868 /* TC-compatible HTILE only supports depth clears to 0 or 1. */
869 if (buffers
& PIPE_CLEAR_DEPTH
&&
870 (!zstex
->tc_compatible_htile
||
871 depth
== 0 || depth
== 1)) {
872 /* Need to disable EXPCLEAR temporarily if clearing
874 if (!zstex
->depth_cleared
|| zstex
->depth_clear_value
!= depth
) {
875 sctx
->db_depth_disable_expclear
= true;
878 zstex
->depth_clear_value
= depth
;
879 sctx
->framebuffer
.dirty_zsbuf
= true;
880 si_mark_atom_dirty(sctx
, &sctx
->framebuffer
.atom
); /* updates DB_DEPTH_CLEAR */
881 sctx
->db_depth_clear
= true;
882 si_mark_atom_dirty(sctx
, &sctx
->db_render_state
);
885 /* TC-compatible HTILE only supports stencil clears to 0. */
886 if (buffers
& PIPE_CLEAR_STENCIL
&&
887 (!zstex
->tc_compatible_htile
|| stencil
== 0)) {
890 /* Need to disable EXPCLEAR temporarily if clearing
892 if (!zstex
->stencil_cleared
|| zstex
->stencil_clear_value
!= stencil
) {
893 sctx
->db_stencil_disable_expclear
= true;
896 zstex
->stencil_clear_value
= stencil
;
897 sctx
->framebuffer
.dirty_zsbuf
= true;
898 si_mark_atom_dirty(sctx
, &sctx
->framebuffer
.atom
); /* updates DB_STENCIL_CLEAR */
899 sctx
->db_stencil_clear
= true;
900 si_mark_atom_dirty(sctx
, &sctx
->db_render_state
);
903 /* TODO: Find out what's wrong here. Fast depth clear leads to
904 * corruption in ARK: Survival Evolved, but that may just be
905 * a coincidence and the root cause is elsewhere.
907 * The corruption can be fixed by putting the DB metadata flush
908 * before or after the depth clear. (suprisingly)
910 * https://bugs.freedesktop.org/show_bug.cgi?id=102955 (apitrace)
912 * This hack decreases back-to-back ClearDepth performance.
914 if (sctx
->screen
->clear_db_meta_before_clear
)
915 sctx
->b
.flags
|= SI_CONTEXT_FLUSH_AND_INV_DB_META
|
916 SI_CONTEXT_PS_PARTIAL_FLUSH
;
919 si_blitter_begin(ctx
, SI_CLEAR
);
920 util_blitter_clear(sctx
->blitter
, fb
->width
, fb
->height
,
921 util_framebuffer_get_num_layers(fb
),
922 buffers
, color
, depth
, stencil
);
925 if (sctx
->db_depth_clear
) {
926 sctx
->db_depth_clear
= false;
927 sctx
->db_depth_disable_expclear
= false;
928 zstex
->depth_cleared
= true;
929 si_mark_atom_dirty(sctx
, &sctx
->db_render_state
);
932 if (sctx
->db_stencil_clear
) {
933 sctx
->db_stencil_clear
= false;
934 sctx
->db_stencil_disable_expclear
= false;
935 zstex
->stencil_cleared
= true;
936 si_mark_atom_dirty(sctx
, &sctx
->db_render_state
);
940 static void si_clear_render_target(struct pipe_context
*ctx
,
941 struct pipe_surface
*dst
,
942 const union pipe_color_union
*color
,
943 unsigned dstx
, unsigned dsty
,
944 unsigned width
, unsigned height
,
945 bool render_condition_enabled
)
947 struct si_context
*sctx
= (struct si_context
*)ctx
;
949 si_blitter_begin(ctx
, SI_CLEAR_SURFACE
|
950 (render_condition_enabled
? 0 : SI_DISABLE_RENDER_COND
));
951 util_blitter_clear_render_target(sctx
->blitter
, dst
, color
,
952 dstx
, dsty
, width
, height
);
956 static void si_clear_depth_stencil(struct pipe_context
*ctx
,
957 struct pipe_surface
*dst
,
958 unsigned clear_flags
,
961 unsigned dstx
, unsigned dsty
,
962 unsigned width
, unsigned height
,
963 bool render_condition_enabled
)
965 struct si_context
*sctx
= (struct si_context
*)ctx
;
967 si_blitter_begin(ctx
, SI_CLEAR_SURFACE
|
968 (render_condition_enabled
? 0 : SI_DISABLE_RENDER_COND
));
969 util_blitter_clear_depth_stencil(sctx
->blitter
, dst
, clear_flags
, depth
, stencil
,
970 dstx
, dsty
, width
, height
);
974 /* Helper for decompressing a portion of a color or depth resource before
975 * blitting if any decompression is needed.
976 * The driver doesn't decompress resources automatically while u_blitter is
978 static void si_decompress_subresource(struct pipe_context
*ctx
,
979 struct pipe_resource
*tex
,
980 unsigned planes
, unsigned level
,
981 unsigned first_layer
, unsigned last_layer
)
983 struct si_context
*sctx
= (struct si_context
*)ctx
;
984 struct r600_texture
*rtex
= (struct r600_texture
*)tex
;
986 if (rtex
->db_compatible
) {
987 planes
&= PIPE_MASK_Z
| PIPE_MASK_S
;
989 if (!rtex
->surface
.has_stencil
)
990 planes
&= ~PIPE_MASK_S
;
992 /* If we've rendered into the framebuffer and it's a blitting
993 * source, make sure the decompression pass is invoked
994 * by dirtying the framebuffer.
996 if (sctx
->framebuffer
.state
.zsbuf
&&
997 sctx
->framebuffer
.state
.zsbuf
->u
.tex
.level
== level
&&
998 sctx
->framebuffer
.state
.zsbuf
->texture
== tex
)
999 si_update_fb_dirtiness_after_rendering(sctx
);
1001 si_decompress_depth(sctx
, rtex
, planes
,
1003 first_layer
, last_layer
);
1004 } else if (rtex
->fmask
.size
|| rtex
->cmask
.size
|| rtex
->dcc_offset
) {
1005 /* If we've rendered into the framebuffer and it's a blitting
1006 * source, make sure the decompression pass is invoked
1007 * by dirtying the framebuffer.
1009 for (unsigned i
= 0; i
< sctx
->framebuffer
.state
.nr_cbufs
; i
++) {
1010 if (sctx
->framebuffer
.state
.cbufs
[i
] &&
1011 sctx
->framebuffer
.state
.cbufs
[i
]->u
.tex
.level
== level
&&
1012 sctx
->framebuffer
.state
.cbufs
[i
]->texture
== tex
) {
1013 si_update_fb_dirtiness_after_rendering(sctx
);
1018 si_blit_decompress_color(ctx
, rtex
, level
, level
,
1019 first_layer
, last_layer
, false);
1023 struct texture_orig_info
{
1033 void si_resource_copy_region(struct pipe_context
*ctx
,
1034 struct pipe_resource
*dst
,
1036 unsigned dstx
, unsigned dsty
, unsigned dstz
,
1037 struct pipe_resource
*src
,
1039 const struct pipe_box
*src_box
)
1041 struct si_context
*sctx
= (struct si_context
*)ctx
;
1042 struct r600_texture
*rsrc
= (struct r600_texture
*)src
;
1043 struct pipe_surface
*dst_view
, dst_templ
;
1044 struct pipe_sampler_view src_templ
, *src_view
;
1045 unsigned dst_width
, dst_height
, src_width0
, src_height0
;
1046 unsigned dst_width0
, dst_height0
, src_force_level
= 0;
1047 struct pipe_box sbox
, dstbox
;
1049 /* Handle buffers first. */
1050 if (dst
->target
== PIPE_BUFFER
&& src
->target
== PIPE_BUFFER
) {
1051 si_copy_buffer(sctx
, dst
, src
, dstx
, src_box
->x
, src_box
->width
, 0);
1055 assert(u_max_sample(dst
) == u_max_sample(src
));
1057 /* The driver doesn't decompress resources automatically while
1058 * u_blitter is rendering. */
1059 si_decompress_subresource(ctx
, src
, PIPE_MASK_RGBAZS
, src_level
,
1060 src_box
->z
, src_box
->z
+ src_box
->depth
- 1);
1062 dst_width
= u_minify(dst
->width0
, dst_level
);
1063 dst_height
= u_minify(dst
->height0
, dst_level
);
1064 dst_width0
= dst
->width0
;
1065 dst_height0
= dst
->height0
;
1066 src_width0
= src
->width0
;
1067 src_height0
= src
->height0
;
1069 util_blitter_default_dst_texture(&dst_templ
, dst
, dst_level
, dstz
);
1070 util_blitter_default_src_texture(sctx
->blitter
, &src_templ
, src
, src_level
);
1072 if (util_format_is_compressed(src
->format
) ||
1073 util_format_is_compressed(dst
->format
)) {
1074 unsigned blocksize
= rsrc
->surface
.bpe
;
1077 src_templ
.format
= PIPE_FORMAT_R16G16B16A16_UINT
; /* 64-bit block */
1079 src_templ
.format
= PIPE_FORMAT_R32G32B32A32_UINT
; /* 128-bit block */
1080 dst_templ
.format
= src_templ
.format
;
1082 dst_width
= util_format_get_nblocksx(dst
->format
, dst_width
);
1083 dst_height
= util_format_get_nblocksy(dst
->format
, dst_height
);
1084 dst_width0
= util_format_get_nblocksx(dst
->format
, dst_width0
);
1085 dst_height0
= util_format_get_nblocksy(dst
->format
, dst_height0
);
1086 src_width0
= util_format_get_nblocksx(src
->format
, src_width0
);
1087 src_height0
= util_format_get_nblocksy(src
->format
, src_height0
);
1089 dstx
= util_format_get_nblocksx(dst
->format
, dstx
);
1090 dsty
= util_format_get_nblocksy(dst
->format
, dsty
);
1092 sbox
.x
= util_format_get_nblocksx(src
->format
, src_box
->x
);
1093 sbox
.y
= util_format_get_nblocksy(src
->format
, src_box
->y
);
1094 sbox
.z
= src_box
->z
;
1095 sbox
.width
= util_format_get_nblocksx(src
->format
, src_box
->width
);
1096 sbox
.height
= util_format_get_nblocksy(src
->format
, src_box
->height
);
1097 sbox
.depth
= src_box
->depth
;
1100 src_force_level
= src_level
;
1101 } else if (!util_blitter_is_copy_supported(sctx
->blitter
, dst
, src
)) {
1102 if (util_format_is_subsampled_422(src
->format
)) {
1103 src_templ
.format
= PIPE_FORMAT_R8G8B8A8_UINT
;
1104 dst_templ
.format
= PIPE_FORMAT_R8G8B8A8_UINT
;
1106 dst_width
= util_format_get_nblocksx(dst
->format
, dst_width
);
1107 dst_width0
= util_format_get_nblocksx(dst
->format
, dst_width0
);
1108 src_width0
= util_format_get_nblocksx(src
->format
, src_width0
);
1110 dstx
= util_format_get_nblocksx(dst
->format
, dstx
);
1113 sbox
.x
= util_format_get_nblocksx(src
->format
, src_box
->x
);
1114 sbox
.width
= util_format_get_nblocksx(src
->format
, src_box
->width
);
1117 unsigned blocksize
= rsrc
->surface
.bpe
;
1119 switch (blocksize
) {
1121 dst_templ
.format
= PIPE_FORMAT_R8_UNORM
;
1122 src_templ
.format
= PIPE_FORMAT_R8_UNORM
;
1125 dst_templ
.format
= PIPE_FORMAT_R8G8_UNORM
;
1126 src_templ
.format
= PIPE_FORMAT_R8G8_UNORM
;
1129 dst_templ
.format
= PIPE_FORMAT_R8G8B8A8_UNORM
;
1130 src_templ
.format
= PIPE_FORMAT_R8G8B8A8_UNORM
;
1133 dst_templ
.format
= PIPE_FORMAT_R16G16B16A16_UINT
;
1134 src_templ
.format
= PIPE_FORMAT_R16G16B16A16_UINT
;
1137 dst_templ
.format
= PIPE_FORMAT_R32G32B32A32_UINT
;
1138 src_templ
.format
= PIPE_FORMAT_R32G32B32A32_UINT
;
1141 fprintf(stderr
, "Unhandled format %s with blocksize %u\n",
1142 util_format_short_name(src
->format
), blocksize
);
1148 /* SNORM8 blitting has precision issues on some chips. Use the SINT
1149 * equivalent instead, which doesn't force DCC decompression.
1150 * Note that some chips avoid this issue by using SDMA.
1152 if (util_format_is_snorm8(dst_templ
.format
)) {
1153 switch (dst_templ
.format
) {
1154 case PIPE_FORMAT_R8_SNORM
:
1155 dst_templ
.format
= src_templ
.format
= PIPE_FORMAT_R8_SINT
;
1157 case PIPE_FORMAT_R8G8_SNORM
:
1158 dst_templ
.format
= src_templ
.format
= PIPE_FORMAT_R8G8_SINT
;
1160 case PIPE_FORMAT_R8G8B8X8_SNORM
:
1161 dst_templ
.format
= src_templ
.format
= PIPE_FORMAT_R8G8B8X8_SINT
;
1163 case PIPE_FORMAT_R8G8B8A8_SNORM
:
1164 /* There are no SINT variants for ABGR and XBGR, so we have to use RGBA. */
1165 case PIPE_FORMAT_A8B8G8R8_SNORM
:
1166 case PIPE_FORMAT_X8B8G8R8_SNORM
:
1167 dst_templ
.format
= src_templ
.format
= PIPE_FORMAT_R8G8B8A8_SINT
;
1169 case PIPE_FORMAT_A8_SNORM
:
1170 dst_templ
.format
= src_templ
.format
= PIPE_FORMAT_A8_SINT
;
1172 case PIPE_FORMAT_L8_SNORM
:
1173 dst_templ
.format
= src_templ
.format
= PIPE_FORMAT_L8_SINT
;
1175 case PIPE_FORMAT_L8A8_SNORM
:
1176 dst_templ
.format
= src_templ
.format
= PIPE_FORMAT_L8A8_SINT
;
1178 case PIPE_FORMAT_I8_SNORM
:
1179 dst_templ
.format
= src_templ
.format
= PIPE_FORMAT_I8_SINT
;
1181 default:; /* fall through */
1185 vi_disable_dcc_if_incompatible_format(&sctx
->b
, dst
, dst_level
,
1187 vi_disable_dcc_if_incompatible_format(&sctx
->b
, src
, src_level
,
1190 /* Initialize the surface. */
1191 dst_view
= si_create_surface_custom(ctx
, dst
, &dst_templ
,
1192 dst_width0
, dst_height0
,
1193 dst_width
, dst_height
);
1195 /* Initialize the sampler view. */
1196 src_view
= si_create_sampler_view_custom(ctx
, src
, &src_templ
,
1197 src_width0
, src_height0
,
1200 u_box_3d(dstx
, dsty
, dstz
, abs(src_box
->width
), abs(src_box
->height
),
1201 abs(src_box
->depth
), &dstbox
);
1204 si_blitter_begin(ctx
, SI_COPY
);
1205 util_blitter_blit_generic(sctx
->blitter
, dst_view
, &dstbox
,
1206 src_view
, src_box
, src_width0
, src_height0
,
1207 PIPE_MASK_RGBAZS
, PIPE_TEX_FILTER_NEAREST
, NULL
,
1209 si_blitter_end(ctx
);
1211 pipe_surface_reference(&dst_view
, NULL
);
1212 pipe_sampler_view_reference(&src_view
, NULL
);
1215 static void si_do_CB_resolve(struct si_context
*sctx
,
1216 const struct pipe_blit_info
*info
,
1217 struct pipe_resource
*dst
,
1218 unsigned dst_level
, unsigned dst_z
,
1219 enum pipe_format format
)
1221 /* Required before and after CB_RESOLVE. */
1222 sctx
->b
.flags
|= SI_CONTEXT_FLUSH_AND_INV_CB
;
1224 si_blitter_begin(&sctx
->b
.b
, SI_COLOR_RESOLVE
|
1225 (info
->render_condition_enable
? 0 : SI_DISABLE_RENDER_COND
));
1226 util_blitter_custom_resolve_color(sctx
->blitter
, dst
, dst_level
, dst_z
,
1227 info
->src
.resource
, info
->src
.box
.z
,
1228 ~0, sctx
->custom_blend_resolve
,
1230 si_blitter_end(&sctx
->b
.b
);
1232 /* Flush caches for possible texturing. */
1233 si_make_CB_shader_coherent(sctx
, 1, false);
1236 static bool do_hardware_msaa_resolve(struct pipe_context
*ctx
,
1237 const struct pipe_blit_info
*info
)
1239 struct si_context
*sctx
= (struct si_context
*)ctx
;
1240 struct r600_texture
*src
= (struct r600_texture
*)info
->src
.resource
;
1241 struct r600_texture
*dst
= (struct r600_texture
*)info
->dst
.resource
;
1242 MAYBE_UNUSED
struct r600_texture
*rtmp
;
1243 unsigned dst_width
= u_minify(info
->dst
.resource
->width0
, info
->dst
.level
);
1244 unsigned dst_height
= u_minify(info
->dst
.resource
->height0
, info
->dst
.level
);
1245 enum pipe_format format
= info
->src
.format
;
1246 struct pipe_resource
*tmp
, templ
;
1247 struct pipe_blit_info blit
;
1249 /* Check basic requirements for hw resolve. */
1250 if (!(info
->src
.resource
->nr_samples
> 1 &&
1251 info
->dst
.resource
->nr_samples
<= 1 &&
1252 !util_format_is_pure_integer(format
) &&
1253 !util_format_is_depth_or_stencil(format
) &&
1254 util_max_layer(info
->src
.resource
, 0) == 0))
1257 /* Hardware MSAA resolve doesn't work if SPI format = NORM16_ABGR and
1258 * the format is R16G16. Use R16A16, which does work.
1260 if (format
== PIPE_FORMAT_R16G16_UNORM
)
1261 format
= PIPE_FORMAT_R16A16_UNORM
;
1262 if (format
== PIPE_FORMAT_R16G16_SNORM
)
1263 format
= PIPE_FORMAT_R16A16_SNORM
;
1265 /* Check the remaining requirements for hw resolve. */
1266 if (util_max_layer(info
->dst
.resource
, info
->dst
.level
) == 0 &&
1267 !info
->scissor_enable
&&
1268 (info
->mask
& PIPE_MASK_RGBA
) == PIPE_MASK_RGBA
&&
1269 util_is_format_compatible(util_format_description(info
->src
.format
),
1270 util_format_description(info
->dst
.format
)) &&
1271 dst_width
== info
->src
.resource
->width0
&&
1272 dst_height
== info
->src
.resource
->height0
&&
1273 info
->dst
.box
.x
== 0 &&
1274 info
->dst
.box
.y
== 0 &&
1275 info
->dst
.box
.width
== dst_width
&&
1276 info
->dst
.box
.height
== dst_height
&&
1277 info
->dst
.box
.depth
== 1 &&
1278 info
->src
.box
.x
== 0 &&
1279 info
->src
.box
.y
== 0 &&
1280 info
->src
.box
.width
== dst_width
&&
1281 info
->src
.box
.height
== dst_height
&&
1282 info
->src
.box
.depth
== 1 &&
1283 !dst
->surface
.is_linear
&&
1284 (!dst
->cmask
.size
|| !dst
->dirty_level_mask
)) { /* dst cannot be fast-cleared */
1285 /* Check the last constraint. */
1286 if (src
->surface
.micro_tile_mode
!= dst
->surface
.micro_tile_mode
) {
1287 /* The next fast clear will switch to this mode to
1288 * get direct hw resolve next time if the mode is
1291 src
->last_msaa_resolve_target_micro_mode
=
1292 dst
->surface
.micro_tile_mode
;
1293 goto resolve_to_temp
;
1296 /* Resolving into a surface with DCC is unsupported. Since
1297 * it's being overwritten anyway, clear it to uncompressed.
1298 * This is still the fastest codepath even with this clear.
1300 if (vi_dcc_enabled(dst
, info
->dst
.level
)) {
1301 /* TODO: Implement per-level DCC clears for GFX9. */
1302 if (sctx
->b
.chip_class
>= GFX9
&&
1303 info
->dst
.resource
->last_level
!= 0)
1304 goto resolve_to_temp
;
1306 vi_dcc_clear_level(&sctx
->b
, dst
, info
->dst
.level
,
1308 dst
->dirty_level_mask
&= ~(1 << info
->dst
.level
);
1311 /* Resolve directly from src to dst. */
1312 si_do_CB_resolve(sctx
, info
, info
->dst
.resource
,
1313 info
->dst
.level
, info
->dst
.box
.z
, format
);
1318 /* Shader-based resolve is VERY SLOW. Instead, resolve into
1319 * a temporary texture and blit.
1321 memset(&templ
, 0, sizeof(templ
));
1322 templ
.target
= PIPE_TEXTURE_2D
;
1323 templ
.format
= info
->src
.resource
->format
;
1324 templ
.width0
= info
->src
.resource
->width0
;
1325 templ
.height0
= info
->src
.resource
->height0
;
1327 templ
.array_size
= 1;
1328 templ
.usage
= PIPE_USAGE_DEFAULT
;
1329 templ
.flags
= R600_RESOURCE_FLAG_FORCE_TILING
|
1330 R600_RESOURCE_FLAG_DISABLE_DCC
;
1332 /* The src and dst microtile modes must be the same. */
1333 if (src
->surface
.micro_tile_mode
== RADEON_MICRO_MODE_DISPLAY
)
1334 templ
.bind
= PIPE_BIND_SCANOUT
;
1338 tmp
= ctx
->screen
->resource_create(ctx
->screen
, &templ
);
1341 rtmp
= (struct r600_texture
*)tmp
;
1343 assert(!rtmp
->surface
.is_linear
);
1344 assert(src
->surface
.micro_tile_mode
== rtmp
->surface
.micro_tile_mode
);
1347 si_do_CB_resolve(sctx
, info
, tmp
, 0, 0, format
);
1351 blit
.src
.resource
= tmp
;
1354 si_blitter_begin(ctx
, SI_BLIT
|
1355 (info
->render_condition_enable
? 0 : SI_DISABLE_RENDER_COND
));
1356 util_blitter_blit(sctx
->blitter
, &blit
);
1357 si_blitter_end(ctx
);
1359 pipe_resource_reference(&tmp
, NULL
);
1363 static void si_blit(struct pipe_context
*ctx
,
1364 const struct pipe_blit_info
*info
)
1366 struct si_context
*sctx
= (struct si_context
*)ctx
;
1367 struct r600_texture
*rdst
= (struct r600_texture
*)info
->dst
.resource
;
1369 if (do_hardware_msaa_resolve(ctx
, info
)) {
1373 /* Using SDMA for copying to a linear texture in GTT is much faster.
1374 * This improves DRI PRIME performance.
1376 * resource_copy_region can't do this yet, because dma_copy calls it
1377 * on failure (recursion).
1379 if (rdst
->surface
.is_linear
&&
1381 util_can_blit_via_copy_region(info
, false)) {
1382 sctx
->b
.dma_copy(ctx
, info
->dst
.resource
, info
->dst
.level
,
1383 info
->dst
.box
.x
, info
->dst
.box
.y
,
1385 info
->src
.resource
, info
->src
.level
,
1390 assert(util_blitter_is_blit_supported(sctx
->blitter
, info
));
1392 /* The driver doesn't decompress resources automatically while
1393 * u_blitter is rendering. */
1394 vi_disable_dcc_if_incompatible_format(&sctx
->b
, info
->src
.resource
,
1397 vi_disable_dcc_if_incompatible_format(&sctx
->b
, info
->dst
.resource
,
1400 si_decompress_subresource(ctx
, info
->src
.resource
, info
->mask
,
1403 info
->src
.box
.z
+ info
->src
.box
.depth
- 1);
1405 if (sctx
->screen
->b
.debug_flags
& DBG_FORCE_DMA
&&
1406 util_try_blit_via_copy_region(ctx
, info
))
1409 si_blitter_begin(ctx
, SI_BLIT
|
1410 (info
->render_condition_enable
? 0 : SI_DISABLE_RENDER_COND
));
1411 util_blitter_blit(sctx
->blitter
, info
);
1412 si_blitter_end(ctx
);
1415 static boolean
si_generate_mipmap(struct pipe_context
*ctx
,
1416 struct pipe_resource
*tex
,
1417 enum pipe_format format
,
1418 unsigned base_level
, unsigned last_level
,
1419 unsigned first_layer
, unsigned last_layer
)
1421 struct si_context
*sctx
= (struct si_context
*)ctx
;
1422 struct r600_texture
*rtex
= (struct r600_texture
*)tex
;
1424 if (!util_blitter_is_copy_supported(sctx
->blitter
, tex
, tex
))
1427 /* The driver doesn't decompress resources automatically while
1428 * u_blitter is rendering. */
1429 vi_disable_dcc_if_incompatible_format(&sctx
->b
, tex
, base_level
,
1431 si_decompress_subresource(ctx
, tex
, PIPE_MASK_RGBAZS
,
1432 base_level
, first_layer
, last_layer
);
1434 /* Clear dirty_level_mask for the levels that will be overwritten. */
1435 assert(base_level
< last_level
);
1436 rtex
->dirty_level_mask
&= ~u_bit_consecutive(base_level
+ 1,
1437 last_level
- base_level
);
1439 sctx
->generate_mipmap_for_depth
= rtex
->is_depth
;
1441 si_blitter_begin(ctx
, SI_BLIT
| SI_DISABLE_RENDER_COND
);
1442 util_blitter_generate_mipmap(sctx
->blitter
, tex
, format
,
1443 base_level
, last_level
,
1444 first_layer
, last_layer
);
1445 si_blitter_end(ctx
);
1447 sctx
->generate_mipmap_for_depth
= false;
1451 static void si_flush_resource(struct pipe_context
*ctx
,
1452 struct pipe_resource
*res
)
1454 struct r600_texture
*rtex
= (struct r600_texture
*)res
;
1456 assert(res
->target
!= PIPE_BUFFER
);
1457 assert(!rtex
->dcc_separate_buffer
|| rtex
->dcc_gather_statistics
);
1459 /* st/dri calls flush twice per frame (not a bug), this prevents double
1461 if (rtex
->dcc_separate_buffer
&& !rtex
->separate_dcc_dirty
)
1464 if (!rtex
->is_depth
&& (rtex
->cmask
.size
|| rtex
->dcc_offset
)) {
1465 si_blit_decompress_color(ctx
, rtex
, 0, res
->last_level
,
1466 0, util_max_layer(res
, 0),
1467 rtex
->dcc_separate_buffer
!= NULL
);
1470 /* Always do the analysis even if DCC is disabled at the moment. */
1471 if (rtex
->dcc_gather_statistics
&& rtex
->separate_dcc_dirty
) {
1472 rtex
->separate_dcc_dirty
= false;
1473 vi_separate_dcc_process_and_reset_stats(ctx
, rtex
);
1477 static void si_decompress_dcc(struct pipe_context
*ctx
,
1478 struct r600_texture
*rtex
)
1480 if (!rtex
->dcc_offset
)
1483 si_blit_decompress_color(ctx
, rtex
, 0, rtex
->resource
.b
.b
.last_level
,
1484 0, util_max_layer(&rtex
->resource
.b
.b
, 0),
1488 static void si_pipe_clear_buffer(struct pipe_context
*ctx
,
1489 struct pipe_resource
*dst
,
1490 unsigned offset
, unsigned size
,
1491 const void *clear_value_ptr
,
1492 int clear_value_size
)
1494 struct si_context
*sctx
= (struct si_context
*)ctx
;
1495 uint32_t dword_value
;
1498 assert(offset
% clear_value_size
== 0);
1499 assert(size
% clear_value_size
== 0);
1501 if (clear_value_size
> 4) {
1502 const uint32_t *u32
= clear_value_ptr
;
1503 bool clear_dword_duplicated
= true;
1505 /* See if we can lower large fills to dword fills. */
1506 for (i
= 1; i
< clear_value_size
/ 4; i
++)
1507 if (u32
[0] != u32
[i
]) {
1508 clear_dword_duplicated
= false;
1512 if (!clear_dword_duplicated
) {
1513 /* Use transform feedback for 64-bit, 96-bit, and
1516 union pipe_color_union clear_value
;
1518 memcpy(&clear_value
, clear_value_ptr
, clear_value_size
);
1519 si_blitter_begin(ctx
, SI_DISABLE_RENDER_COND
);
1520 util_blitter_clear_buffer(sctx
->blitter
, dst
, offset
,
1521 size
, clear_value_size
/ 4,
1523 si_blitter_end(ctx
);
1528 /* Expand the clear value to a dword. */
1529 switch (clear_value_size
) {
1531 dword_value
= *(uint8_t*)clear_value_ptr
;
1532 dword_value
|= (dword_value
<< 8) |
1533 (dword_value
<< 16) |
1534 (dword_value
<< 24);
1537 dword_value
= *(uint16_t*)clear_value_ptr
;
1538 dword_value
|= dword_value
<< 16;
1541 dword_value
= *(uint32_t*)clear_value_ptr
;
1544 sctx
->b
.clear_buffer(ctx
, dst
, offset
, size
, dword_value
,
1545 R600_COHERENCY_SHADER
);
1548 void si_init_blit_functions(struct si_context
*sctx
)
1550 sctx
->b
.b
.clear
= si_clear
;
1551 sctx
->b
.b
.clear_buffer
= si_pipe_clear_buffer
;
1552 sctx
->b
.b
.clear_render_target
= si_clear_render_target
;
1553 sctx
->b
.b
.clear_depth_stencil
= si_clear_depth_stencil
;
1554 sctx
->b
.b
.resource_copy_region
= si_resource_copy_region
;
1555 sctx
->b
.b
.blit
= si_blit
;
1556 sctx
->b
.b
.flush_resource
= si_flush_resource
;
1557 sctx
->b
.b
.generate_mipmap
= si_generate_mipmap
;
1558 sctx
->b
.blit_decompress_depth
= si_blit_decompress_depth
;
1559 sctx
->b
.decompress_dcc
= si_decompress_dcc
;