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.
26 #include "util/u_format.h"
27 #include "util/u_surface.h"
29 enum si_blitter_op
/* bitmask */
32 SI_SAVE_FRAMEBUFFER
= 2,
33 SI_SAVE_FRAGMENT_STATE
= 4,
34 SI_DISABLE_RENDER_COND
= 8,
36 SI_CLEAR
= SI_SAVE_FRAGMENT_STATE
,
38 SI_CLEAR_SURFACE
= SI_SAVE_FRAMEBUFFER
| SI_SAVE_FRAGMENT_STATE
,
40 SI_COPY
= SI_SAVE_FRAMEBUFFER
| SI_SAVE_TEXTURES
|
41 SI_SAVE_FRAGMENT_STATE
| SI_DISABLE_RENDER_COND
,
43 SI_BLIT
= SI_SAVE_FRAMEBUFFER
| SI_SAVE_TEXTURES
|
44 SI_SAVE_FRAGMENT_STATE
,
46 SI_DECOMPRESS
= SI_SAVE_FRAMEBUFFER
| SI_SAVE_FRAGMENT_STATE
|
47 SI_DISABLE_RENDER_COND
,
49 SI_COLOR_RESOLVE
= SI_SAVE_FRAMEBUFFER
| SI_SAVE_FRAGMENT_STATE
52 static void si_blitter_begin(struct pipe_context
*ctx
, enum si_blitter_op op
)
54 struct si_context
*sctx
= (struct si_context
*)ctx
;
56 util_blitter_save_vertex_buffer_slot(sctx
->blitter
, sctx
->vertex_buffer
);
57 util_blitter_save_vertex_elements(sctx
->blitter
, sctx
->vertex_elements
);
58 util_blitter_save_vertex_shader(sctx
->blitter
, sctx
->vs_shader
.cso
);
59 util_blitter_save_tessctrl_shader(sctx
->blitter
, sctx
->tcs_shader
.cso
);
60 util_blitter_save_tesseval_shader(sctx
->blitter
, sctx
->tes_shader
.cso
);
61 util_blitter_save_geometry_shader(sctx
->blitter
, sctx
->gs_shader
.cso
);
62 util_blitter_save_so_targets(sctx
->blitter
, sctx
->b
.streamout
.num_targets
,
63 (struct pipe_stream_output_target
**)sctx
->b
.streamout
.targets
);
64 util_blitter_save_rasterizer(sctx
->blitter
, sctx
->queued
.named
.rasterizer
);
66 if (op
& SI_SAVE_FRAGMENT_STATE
) {
67 util_blitter_save_blend(sctx
->blitter
, sctx
->queued
.named
.blend
);
68 util_blitter_save_depth_stencil_alpha(sctx
->blitter
, sctx
->queued
.named
.dsa
);
69 util_blitter_save_stencil_ref(sctx
->blitter
, &sctx
->stencil_ref
.state
);
70 util_blitter_save_fragment_shader(sctx
->blitter
, sctx
->ps_shader
.cso
);
71 util_blitter_save_sample_mask(sctx
->blitter
, sctx
->sample_mask
.sample_mask
);
72 util_blitter_save_viewport(sctx
->blitter
, &sctx
->b
.viewports
.states
[0]);
73 util_blitter_save_scissor(sctx
->blitter
, &sctx
->b
.scissors
.states
[0]);
76 if (op
& SI_SAVE_FRAMEBUFFER
)
77 util_blitter_save_framebuffer(sctx
->blitter
, &sctx
->framebuffer
.state
);
79 if (op
& SI_SAVE_TEXTURES
) {
80 util_blitter_save_fragment_sampler_states(
82 sctx
->samplers
[PIPE_SHADER_FRAGMENT
].views
.sampler_states
);
84 util_blitter_save_fragment_sampler_views(sctx
->blitter
, 2,
85 sctx
->samplers
[PIPE_SHADER_FRAGMENT
].views
.views
);
88 if (op
& SI_DISABLE_RENDER_COND
)
89 sctx
->b
.render_cond_force_off
= true;
92 static void si_blitter_end(struct pipe_context
*ctx
)
94 struct si_context
*sctx
= (struct si_context
*)ctx
;
96 sctx
->b
.render_cond_force_off
= false;
99 static unsigned u_max_sample(struct pipe_resource
*r
)
101 return r
->nr_samples
? r
->nr_samples
- 1 : 0;
105 si_blit_dbcb_copy(struct si_context
*sctx
,
106 struct r600_texture
*src
,
107 struct r600_texture
*dst
,
108 unsigned planes
, unsigned level_mask
,
109 unsigned first_layer
, unsigned last_layer
,
110 unsigned first_sample
, unsigned last_sample
)
112 struct pipe_surface surf_tmpl
= {{0}};
113 unsigned layer
, sample
, checked_last_layer
, max_layer
;
115 if (planes
& PIPE_MASK_Z
)
116 sctx
->dbcb_depth_copy_enabled
= true;
117 if (planes
& PIPE_MASK_S
)
118 sctx
->dbcb_stencil_copy_enabled
= true;
119 si_mark_atom_dirty(sctx
, &sctx
->db_render_state
);
121 assert(sctx
->dbcb_depth_copy_enabled
|| sctx
->dbcb_stencil_copy_enabled
);
124 unsigned level
= u_bit_scan(&level_mask
);
126 /* The smaller the mipmap level, the less layers there are
127 * as far as 3D textures are concerned. */
128 max_layer
= util_max_layer(&src
->resource
.b
.b
, level
);
129 checked_last_layer
= MIN2(last_layer
, max_layer
);
131 surf_tmpl
.u
.tex
.level
= level
;
133 for (layer
= first_layer
; layer
<= checked_last_layer
; layer
++) {
134 struct pipe_surface
*zsurf
, *cbsurf
;
136 surf_tmpl
.format
= src
->resource
.b
.b
.format
;
137 surf_tmpl
.u
.tex
.first_layer
= layer
;
138 surf_tmpl
.u
.tex
.last_layer
= layer
;
140 zsurf
= sctx
->b
.b
.create_surface(&sctx
->b
.b
, &src
->resource
.b
.b
, &surf_tmpl
);
142 surf_tmpl
.format
= dst
->resource
.b
.b
.format
;
143 cbsurf
= sctx
->b
.b
.create_surface(&sctx
->b
.b
, &dst
->resource
.b
.b
, &surf_tmpl
);
145 for (sample
= first_sample
; sample
<= last_sample
; sample
++) {
146 if (sample
!= sctx
->dbcb_copy_sample
) {
147 sctx
->dbcb_copy_sample
= sample
;
148 si_mark_atom_dirty(sctx
, &sctx
->db_render_state
);
151 si_blitter_begin(&sctx
->b
.b
, SI_DECOMPRESS
);
152 util_blitter_custom_depth_stencil(sctx
->blitter
, zsurf
, cbsurf
, 1 << sample
,
153 sctx
->custom_dsa_flush
, 1.0f
);
154 si_blitter_end(&sctx
->b
.b
);
157 pipe_surface_reference(&zsurf
, NULL
);
158 pipe_surface_reference(&cbsurf
, NULL
);
162 sctx
->dbcb_depth_copy_enabled
= false;
163 sctx
->dbcb_stencil_copy_enabled
= false;
164 si_mark_atom_dirty(sctx
, &sctx
->db_render_state
);
167 static void si_blit_decompress_depth(struct pipe_context
*ctx
,
168 struct r600_texture
*texture
,
169 struct r600_texture
*staging
,
170 unsigned first_level
, unsigned last_level
,
171 unsigned first_layer
, unsigned last_layer
,
172 unsigned first_sample
, unsigned last_sample
)
174 const struct util_format_description
*desc
;
177 assert(staging
!= NULL
&& "use si_blit_decompress_zs_in_place instead");
179 desc
= util_format_description(staging
->resource
.b
.b
.format
);
181 if (util_format_has_depth(desc
))
182 planes
|= PIPE_MASK_Z
;
183 if (util_format_has_stencil(desc
))
184 planes
|= PIPE_MASK_S
;
187 (struct si_context
*)ctx
, texture
, staging
, planes
,
188 u_bit_consecutive(first_level
, last_level
- first_level
+ 1),
189 first_layer
, last_layer
, first_sample
, last_sample
);
192 /* Helper function for si_blit_decompress_zs_in_place.
195 si_blit_decompress_zs_planes_in_place(struct si_context
*sctx
,
196 struct r600_texture
*texture
,
197 unsigned planes
, unsigned level_mask
,
198 unsigned first_layer
, unsigned last_layer
)
200 struct pipe_surface
*zsurf
, surf_tmpl
= {{0}};
201 unsigned layer
, max_layer
, checked_last_layer
;
202 unsigned fully_decompressed_mask
= 0;
207 if (planes
& PIPE_MASK_S
)
208 sctx
->db_flush_stencil_inplace
= true;
209 if (planes
& PIPE_MASK_Z
)
210 sctx
->db_flush_depth_inplace
= true;
211 si_mark_atom_dirty(sctx
, &sctx
->db_render_state
);
213 surf_tmpl
.format
= texture
->resource
.b
.b
.format
;
216 unsigned level
= u_bit_scan(&level_mask
);
218 surf_tmpl
.u
.tex
.level
= level
;
220 /* The smaller the mipmap level, the less layers there are
221 * as far as 3D textures are concerned. */
222 max_layer
= util_max_layer(&texture
->resource
.b
.b
, level
);
223 checked_last_layer
= MIN2(last_layer
, max_layer
);
225 for (layer
= first_layer
; layer
<= checked_last_layer
; layer
++) {
226 surf_tmpl
.u
.tex
.first_layer
= layer
;
227 surf_tmpl
.u
.tex
.last_layer
= layer
;
229 zsurf
= sctx
->b
.b
.create_surface(&sctx
->b
.b
, &texture
->resource
.b
.b
, &surf_tmpl
);
231 si_blitter_begin(&sctx
->b
.b
, SI_DECOMPRESS
);
232 util_blitter_custom_depth_stencil(sctx
->blitter
, zsurf
, NULL
, ~0,
233 sctx
->custom_dsa_flush
,
235 si_blitter_end(&sctx
->b
.b
);
237 pipe_surface_reference(&zsurf
, NULL
);
240 /* The texture will always be dirty if some layers aren't flushed.
241 * I don't think this case occurs often though. */
242 if (first_layer
== 0 && last_layer
>= max_layer
) {
243 fully_decompressed_mask
|= 1u << level
;
247 if (planes
& PIPE_MASK_Z
)
248 texture
->dirty_level_mask
&= ~fully_decompressed_mask
;
249 if (planes
& PIPE_MASK_S
)
250 texture
->stencil_dirty_level_mask
&= ~fully_decompressed_mask
;
252 sctx
->db_flush_depth_inplace
= false;
253 sctx
->db_flush_stencil_inplace
= false;
254 si_mark_atom_dirty(sctx
, &sctx
->db_render_state
);
257 /* Decompress Z and/or S planes in place, depending on mask.
260 si_blit_decompress_zs_in_place(struct si_context
*sctx
,
261 struct r600_texture
*texture
,
263 unsigned first_level
, unsigned last_level
,
264 unsigned first_layer
, unsigned last_layer
)
266 unsigned level_mask
=
267 u_bit_consecutive(first_level
, last_level
- first_level
+ 1);
268 unsigned cur_level_mask
;
270 /* First, do combined Z & S decompresses for levels that need it. */
271 if (planes
== (PIPE_MASK_Z
| PIPE_MASK_S
)) {
274 texture
->dirty_level_mask
&
275 texture
->stencil_dirty_level_mask
;
276 si_blit_decompress_zs_planes_in_place(
277 sctx
, texture
, PIPE_MASK_Z
| PIPE_MASK_S
,
279 first_layer
, last_layer
);
280 level_mask
&= ~cur_level_mask
;
283 /* Now do separate Z and S decompresses. */
284 if (planes
& PIPE_MASK_Z
) {
285 cur_level_mask
= level_mask
& texture
->dirty_level_mask
;
286 si_blit_decompress_zs_planes_in_place(
287 sctx
, texture
, PIPE_MASK_Z
,
289 first_layer
, last_layer
);
290 level_mask
&= ~cur_level_mask
;
293 if (planes
& PIPE_MASK_S
) {
294 cur_level_mask
= level_mask
& texture
->stencil_dirty_level_mask
;
295 si_blit_decompress_zs_planes_in_place(
296 sctx
, texture
, PIPE_MASK_S
,
298 first_layer
, last_layer
);
303 si_flush_depth_textures(struct si_context
*sctx
,
304 struct si_textures_info
*textures
)
307 unsigned mask
= textures
->depth_texture_mask
;
310 struct pipe_sampler_view
*view
;
311 struct si_sampler_view
*sview
;
312 struct r600_texture
*tex
;
314 i
= u_bit_scan(&mask
);
316 view
= textures
->views
.views
[i
];
318 sview
= (struct si_sampler_view
*)view
;
320 tex
= (struct r600_texture
*)view
->texture
;
321 assert(tex
->db_compatible
);
323 si_blit_decompress_zs_in_place(sctx
, tex
,
324 sview
->is_stencil_sampler
? PIPE_MASK_S
326 view
->u
.tex
.first_level
, view
->u
.tex
.last_level
,
327 0, util_max_layer(&tex
->resource
.b
.b
, view
->u
.tex
.first_level
));
331 static void si_blit_decompress_color(struct pipe_context
*ctx
,
332 struct r600_texture
*rtex
,
333 unsigned first_level
, unsigned last_level
,
334 unsigned first_layer
, unsigned last_layer
,
335 bool need_dcc_decompress
)
337 struct si_context
*sctx
= (struct si_context
*)ctx
;
339 unsigned layer
, checked_last_layer
, max_layer
;
340 unsigned level_mask
=
341 u_bit_consecutive(first_level
, last_level
- first_level
+ 1);
343 if (!need_dcc_decompress
)
344 level_mask
&= rtex
->dirty_level_mask
;
348 if (rtex
->dcc_offset
&& need_dcc_decompress
) {
349 custom_blend
= sctx
->custom_blend_dcc_decompress
;
351 /* disable levels without DCC */
352 for (int i
= first_level
; i
<= last_level
; i
++) {
353 if (!rtex
->dcc_offset
||
354 !rtex
->surface
.level
[i
].dcc_enabled
)
355 level_mask
&= ~(1 << i
);
357 } else if (rtex
->fmask
.size
) {
358 custom_blend
= sctx
->custom_blend_decompress
;
360 custom_blend
= sctx
->custom_blend_fastclear
;
364 unsigned level
= u_bit_scan(&level_mask
);
366 /* The smaller the mipmap level, the less layers there are
367 * as far as 3D textures are concerned. */
368 max_layer
= util_max_layer(&rtex
->resource
.b
.b
, level
);
369 checked_last_layer
= MIN2(last_layer
, max_layer
);
371 for (layer
= first_layer
; layer
<= checked_last_layer
; layer
++) {
372 struct pipe_surface
*cbsurf
, surf_tmpl
;
374 surf_tmpl
.format
= rtex
->resource
.b
.b
.format
;
375 surf_tmpl
.u
.tex
.level
= level
;
376 surf_tmpl
.u
.tex
.first_layer
= layer
;
377 surf_tmpl
.u
.tex
.last_layer
= layer
;
378 cbsurf
= ctx
->create_surface(ctx
, &rtex
->resource
.b
.b
, &surf_tmpl
);
380 si_blitter_begin(ctx
, SI_DECOMPRESS
);
381 util_blitter_custom_color(sctx
->blitter
, cbsurf
, custom_blend
);
384 pipe_surface_reference(&cbsurf
, NULL
);
387 /* The texture will always be dirty if some layers aren't flushed.
388 * I don't think this case occurs often though. */
389 if (first_layer
== 0 && last_layer
>= max_layer
) {
390 rtex
->dirty_level_mask
&= ~(1 << level
);
396 si_decompress_sampler_color_textures(struct si_context
*sctx
,
397 struct si_textures_info
*textures
)
400 unsigned mask
= textures
->compressed_colortex_mask
;
403 struct pipe_sampler_view
*view
;
404 struct r600_texture
*tex
;
406 i
= u_bit_scan(&mask
);
408 view
= textures
->views
.views
[i
];
411 tex
= (struct r600_texture
*)view
->texture
;
412 assert(tex
->cmask
.size
|| tex
->fmask
.size
|| tex
->dcc_offset
);
414 si_blit_decompress_color(&sctx
->b
.b
, tex
,
415 view
->u
.tex
.first_level
, view
->u
.tex
.last_level
,
416 0, util_max_layer(&tex
->resource
.b
.b
, view
->u
.tex
.first_level
),
422 si_decompress_image_color_textures(struct si_context
*sctx
,
423 struct si_images_info
*images
)
426 unsigned mask
= images
->compressed_colortex_mask
;
429 const struct pipe_image_view
*view
;
430 struct r600_texture
*tex
;
432 i
= u_bit_scan(&mask
);
434 view
= &images
->views
[i
];
435 assert(view
->resource
->target
!= PIPE_BUFFER
);
437 tex
= (struct r600_texture
*)view
->resource
;
438 if (!tex
->cmask
.size
&& !tex
->fmask
.size
&& !tex
->dcc_offset
)
441 si_blit_decompress_color(&sctx
->b
.b
, tex
,
442 view
->u
.tex
.level
, view
->u
.tex
.level
,
443 0, util_max_layer(&tex
->resource
.b
.b
, view
->u
.tex
.level
),
448 static void si_check_render_feedback_textures(struct si_context
*sctx
,
449 struct si_textures_info
*textures
)
451 uint32_t mask
= textures
->views
.enabled_mask
;
454 const struct pipe_sampler_view
*view
;
455 struct r600_texture
*tex
;
456 bool render_feedback
= false;
458 unsigned i
= u_bit_scan(&mask
);
460 view
= textures
->views
.views
[i
];
461 if(view
->texture
->target
== PIPE_BUFFER
)
464 tex
= (struct r600_texture
*)view
->texture
;
465 if (!tex
->dcc_offset
)
468 for (unsigned j
= 0; j
< sctx
->framebuffer
.state
.nr_cbufs
; ++j
) {
469 struct r600_surface
* surf
;
471 if (!sctx
->framebuffer
.state
.cbufs
[j
])
474 surf
= (struct r600_surface
*)sctx
->framebuffer
.state
.cbufs
[j
];
476 if (tex
== (struct r600_texture
*)surf
->base
.texture
&&
477 surf
->base
.u
.tex
.level
>= view
->u
.tex
.first_level
&&
478 surf
->base
.u
.tex
.level
<= view
->u
.tex
.last_level
&&
479 surf
->base
.u
.tex
.first_layer
<= view
->u
.tex
.last_layer
&&
480 surf
->base
.u
.tex
.last_layer
>= view
->u
.tex
.first_layer
)
481 render_feedback
= true;
484 if (render_feedback
) {
485 struct si_screen
*screen
= sctx
->screen
;
486 r600_texture_disable_dcc(&screen
->b
, tex
);
491 static void si_check_render_feedback_images(struct si_context
*sctx
,
492 struct si_images_info
*images
)
494 uint32_t mask
= images
->enabled_mask
;
497 const struct pipe_image_view
*view
;
498 struct r600_texture
*tex
;
499 bool render_feedback
= false;
501 unsigned i
= u_bit_scan(&mask
);
503 view
= &images
->views
[i
];
504 if (view
->resource
->target
== PIPE_BUFFER
)
507 tex
= (struct r600_texture
*)view
->resource
;
508 if (!tex
->dcc_offset
)
511 for (unsigned j
= 0; j
< sctx
->framebuffer
.state
.nr_cbufs
; ++j
) {
512 struct r600_surface
* surf
;
514 if (!sctx
->framebuffer
.state
.cbufs
[j
])
517 surf
= (struct r600_surface
*)sctx
->framebuffer
.state
.cbufs
[j
];
519 if (tex
== (struct r600_texture
*)surf
->base
.texture
&&
520 surf
->base
.u
.tex
.level
== view
->u
.tex
.level
&&
521 surf
->base
.u
.tex
.first_layer
<= view
->u
.tex
.last_layer
&&
522 surf
->base
.u
.tex
.last_layer
>= view
->u
.tex
.first_layer
)
523 render_feedback
= true;
526 if (render_feedback
) {
527 struct si_screen
*screen
= sctx
->screen
;
528 r600_texture_disable_dcc(&screen
->b
, tex
);
533 static void si_check_render_feedback(struct si_context
*sctx
)
536 if (!sctx
->need_check_render_feedback
)
539 for (int i
= 0; i
< SI_NUM_SHADERS
; ++i
) {
540 si_check_render_feedback_images(sctx
, &sctx
->images
[i
]);
541 si_check_render_feedback_textures(sctx
, &sctx
->samplers
[i
]);
543 sctx
->need_check_render_feedback
= false;
546 static void si_decompress_textures(struct si_context
*sctx
, int shader_start
,
549 unsigned compressed_colortex_counter
;
551 if (sctx
->blitter
->running
)
554 /* Update the compressed_colortex_mask if necessary. */
555 compressed_colortex_counter
= p_atomic_read(&sctx
->screen
->b
.compressed_colortex_counter
);
556 if (compressed_colortex_counter
!= sctx
->b
.last_compressed_colortex_counter
) {
557 sctx
->b
.last_compressed_colortex_counter
= compressed_colortex_counter
;
558 si_update_compressed_colortex_masks(sctx
);
561 /* Flush depth textures which need to be flushed. */
562 for (int i
= shader_start
; i
< shader_end
; i
++) {
563 if (sctx
->samplers
[i
].depth_texture_mask
) {
564 si_flush_depth_textures(sctx
, &sctx
->samplers
[i
]);
566 if (sctx
->samplers
[i
].compressed_colortex_mask
) {
567 si_decompress_sampler_color_textures(sctx
, &sctx
->samplers
[i
]);
569 if (sctx
->images
[i
].compressed_colortex_mask
) {
570 si_decompress_image_color_textures(sctx
, &sctx
->images
[i
]);
574 si_check_render_feedback(sctx
);
577 void si_decompress_graphics_textures(struct si_context
*sctx
)
579 si_decompress_textures(sctx
, 0, SI_NUM_GRAPHICS_SHADERS
);
582 void si_decompress_compute_textures(struct si_context
*sctx
)
584 si_decompress_textures(sctx
, SI_NUM_GRAPHICS_SHADERS
, SI_NUM_SHADERS
);
587 static void si_clear(struct pipe_context
*ctx
, unsigned buffers
,
588 const union pipe_color_union
*color
,
589 double depth
, unsigned stencil
)
591 struct si_context
*sctx
= (struct si_context
*)ctx
;
592 struct pipe_framebuffer_state
*fb
= &sctx
->framebuffer
.state
;
593 struct pipe_surface
*zsbuf
= fb
->zsbuf
;
594 struct r600_texture
*zstex
=
595 zsbuf
? (struct r600_texture
*)zsbuf
->texture
: NULL
;
597 if (buffers
& PIPE_CLEAR_COLOR
) {
598 evergreen_do_fast_color_clear(&sctx
->b
, fb
,
599 &sctx
->framebuffer
.atom
, &buffers
,
600 &sctx
->framebuffer
.dirty_cbufs
,
603 return; /* all buffers have been fast cleared */
606 if (buffers
& PIPE_CLEAR_COLOR
) {
609 /* These buffers cannot use fast clear, make sure to disable expansion. */
610 for (i
= 0; i
< fb
->nr_cbufs
; i
++) {
611 struct r600_texture
*tex
;
613 /* If not clearing this buffer, skip. */
614 if (!(buffers
& (PIPE_CLEAR_COLOR0
<< i
)))
620 tex
= (struct r600_texture
*)fb
->cbufs
[i
]->texture
;
621 if (tex
->fmask
.size
== 0)
622 tex
->dirty_level_mask
&= ~(1 << fb
->cbufs
[i
]->u
.tex
.level
);
626 if (zstex
&& zstex
->htile_buffer
&&
627 zsbuf
->u
.tex
.level
== 0 &&
628 zsbuf
->u
.tex
.first_layer
== 0 &&
629 zsbuf
->u
.tex
.last_layer
== util_max_layer(&zstex
->resource
.b
.b
, 0)) {
630 if (buffers
& PIPE_CLEAR_DEPTH
) {
631 /* Need to disable EXPCLEAR temporarily if clearing
633 if (!zstex
->depth_cleared
|| zstex
->depth_clear_value
!= depth
) {
634 sctx
->db_depth_disable_expclear
= true;
637 zstex
->depth_clear_value
= depth
;
638 sctx
->framebuffer
.dirty_zsbuf
= true;
639 si_mark_atom_dirty(sctx
, &sctx
->framebuffer
.atom
); /* updates DB_DEPTH_CLEAR */
640 sctx
->db_depth_clear
= true;
641 si_mark_atom_dirty(sctx
, &sctx
->db_render_state
);
644 if (buffers
& PIPE_CLEAR_STENCIL
) {
647 /* Need to disable EXPCLEAR temporarily if clearing
649 if (!zstex
->stencil_cleared
|| zstex
->stencil_clear_value
!= stencil
) {
650 sctx
->db_stencil_disable_expclear
= true;
653 zstex
->stencil_clear_value
= stencil
;
654 sctx
->framebuffer
.dirty_zsbuf
= true;
655 si_mark_atom_dirty(sctx
, &sctx
->framebuffer
.atom
); /* updates DB_STENCIL_CLEAR */
656 sctx
->db_stencil_clear
= true;
657 si_mark_atom_dirty(sctx
, &sctx
->db_render_state
);
661 si_blitter_begin(ctx
, SI_CLEAR
);
662 util_blitter_clear(sctx
->blitter
, fb
->width
, fb
->height
,
663 util_framebuffer_get_num_layers(fb
),
664 buffers
, color
, depth
, stencil
);
667 if (sctx
->db_depth_clear
) {
668 sctx
->db_depth_clear
= false;
669 sctx
->db_depth_disable_expclear
= false;
670 zstex
->depth_cleared
= true;
671 si_mark_atom_dirty(sctx
, &sctx
->db_render_state
);
674 if (sctx
->db_stencil_clear
) {
675 sctx
->db_stencil_clear
= false;
676 sctx
->db_stencil_disable_expclear
= false;
677 zstex
->stencil_cleared
= true;
678 si_mark_atom_dirty(sctx
, &sctx
->db_render_state
);
682 static void si_clear_render_target(struct pipe_context
*ctx
,
683 struct pipe_surface
*dst
,
684 const union pipe_color_union
*color
,
685 unsigned dstx
, unsigned dsty
,
686 unsigned width
, unsigned height
)
688 struct si_context
*sctx
= (struct si_context
*)ctx
;
690 si_blitter_begin(ctx
, SI_CLEAR_SURFACE
);
691 util_blitter_clear_render_target(sctx
->blitter
, dst
, color
,
692 dstx
, dsty
, width
, height
);
696 static void si_clear_depth_stencil(struct pipe_context
*ctx
,
697 struct pipe_surface
*dst
,
698 unsigned clear_flags
,
701 unsigned dstx
, unsigned dsty
,
702 unsigned width
, unsigned height
)
704 struct si_context
*sctx
= (struct si_context
*)ctx
;
706 si_blitter_begin(ctx
, SI_CLEAR_SURFACE
);
707 util_blitter_clear_depth_stencil(sctx
->blitter
, dst
, clear_flags
, depth
, stencil
,
708 dstx
, dsty
, width
, height
);
712 /* Helper for decompressing a portion of a color or depth resource before
713 * blitting if any decompression is needed.
714 * The driver doesn't decompress resources automatically while u_blitter is
716 static void si_decompress_subresource(struct pipe_context
*ctx
,
717 struct pipe_resource
*tex
,
718 unsigned planes
, unsigned level
,
719 unsigned first_layer
, unsigned last_layer
)
721 struct si_context
*sctx
= (struct si_context
*)ctx
;
722 struct r600_texture
*rtex
= (struct r600_texture
*)tex
;
724 if (rtex
->db_compatible
) {
725 planes
&= PIPE_MASK_Z
| PIPE_MASK_S
;
727 if (!(rtex
->surface
.flags
& RADEON_SURF_SBUFFER
))
728 planes
&= ~PIPE_MASK_S
;
730 si_blit_decompress_zs_in_place(sctx
, rtex
, planes
,
732 first_layer
, last_layer
);
733 } else if (rtex
->fmask
.size
|| rtex
->cmask
.size
|| rtex
->dcc_offset
) {
734 si_blit_decompress_color(ctx
, rtex
, level
, level
,
735 first_layer
, last_layer
, false);
739 struct texture_orig_info
{
749 void si_resource_copy_region(struct pipe_context
*ctx
,
750 struct pipe_resource
*dst
,
752 unsigned dstx
, unsigned dsty
, unsigned dstz
,
753 struct pipe_resource
*src
,
755 const struct pipe_box
*src_box
)
757 struct si_context
*sctx
= (struct si_context
*)ctx
;
758 struct pipe_surface
*dst_view
, dst_templ
;
759 struct pipe_sampler_view src_templ
, *src_view
;
760 unsigned dst_width
, dst_height
, src_width0
, src_height0
;
761 unsigned src_force_level
= 0;
762 struct pipe_box sbox
, dstbox
;
764 /* Handle buffers first. */
765 if (dst
->target
== PIPE_BUFFER
&& src
->target
== PIPE_BUFFER
) {
766 si_copy_buffer(sctx
, dst
, src
, dstx
, src_box
->x
, src_box
->width
);
770 assert(u_max_sample(dst
) == u_max_sample(src
));
772 /* The driver doesn't decompress resources automatically while
773 * u_blitter is rendering. */
774 si_decompress_subresource(ctx
, src
, PIPE_MASK_RGBAZS
, src_level
,
775 src_box
->z
, src_box
->z
+ src_box
->depth
- 1);
777 dst_width
= u_minify(dst
->width0
, dst_level
);
778 dst_height
= u_minify(dst
->height0
, dst_level
);
779 src_width0
= src
->width0
;
780 src_height0
= src
->height0
;
782 util_blitter_default_dst_texture(&dst_templ
, dst
, dst_level
, dstz
);
783 util_blitter_default_src_texture(&src_templ
, src
, src_level
);
785 if (util_format_is_compressed(src
->format
) ||
786 util_format_is_compressed(dst
->format
)) {
787 unsigned blocksize
= util_format_get_blocksize(src
->format
);
790 src_templ
.format
= PIPE_FORMAT_R16G16B16A16_UINT
; /* 64-bit block */
792 src_templ
.format
= PIPE_FORMAT_R32G32B32A32_UINT
; /* 128-bit block */
793 dst_templ
.format
= src_templ
.format
;
795 dst_width
= util_format_get_nblocksx(dst
->format
, dst_width
);
796 dst_height
= util_format_get_nblocksy(dst
->format
, dst_height
);
797 src_width0
= util_format_get_nblocksx(src
->format
, src_width0
);
798 src_height0
= util_format_get_nblocksy(src
->format
, src_height0
);
800 dstx
= util_format_get_nblocksx(dst
->format
, dstx
);
801 dsty
= util_format_get_nblocksy(dst
->format
, dsty
);
803 sbox
.x
= util_format_get_nblocksx(src
->format
, src_box
->x
);
804 sbox
.y
= util_format_get_nblocksy(src
->format
, src_box
->y
);
806 sbox
.width
= util_format_get_nblocksx(src
->format
, src_box
->width
);
807 sbox
.height
= util_format_get_nblocksy(src
->format
, src_box
->height
);
808 sbox
.depth
= src_box
->depth
;
811 src_force_level
= src_level
;
812 } else if (!util_blitter_is_copy_supported(sctx
->blitter
, dst
, src
) ||
813 /* also *8_SNORM has precision issues, use UNORM instead */
814 util_format_is_snorm8(src
->format
)) {
815 if (util_format_is_subsampled_422(src
->format
)) {
816 src_templ
.format
= PIPE_FORMAT_R8G8B8A8_UINT
;
817 dst_templ
.format
= PIPE_FORMAT_R8G8B8A8_UINT
;
819 dst_width
= util_format_get_nblocksx(dst
->format
, dst_width
);
820 src_width0
= util_format_get_nblocksx(src
->format
, src_width0
);
822 dstx
= util_format_get_nblocksx(dst
->format
, dstx
);
825 sbox
.x
= util_format_get_nblocksx(src
->format
, src_box
->x
);
826 sbox
.width
= util_format_get_nblocksx(src
->format
, src_box
->width
);
829 unsigned blocksize
= util_format_get_blocksize(src
->format
);
833 dst_templ
.format
= PIPE_FORMAT_R8_UNORM
;
834 src_templ
.format
= PIPE_FORMAT_R8_UNORM
;
837 dst_templ
.format
= PIPE_FORMAT_R8G8_UNORM
;
838 src_templ
.format
= PIPE_FORMAT_R8G8_UNORM
;
841 dst_templ
.format
= PIPE_FORMAT_R8G8B8A8_UNORM
;
842 src_templ
.format
= PIPE_FORMAT_R8G8B8A8_UNORM
;
845 dst_templ
.format
= PIPE_FORMAT_R16G16B16A16_UINT
;
846 src_templ
.format
= PIPE_FORMAT_R16G16B16A16_UINT
;
849 dst_templ
.format
= PIPE_FORMAT_R32G32B32A32_UINT
;
850 src_templ
.format
= PIPE_FORMAT_R32G32B32A32_UINT
;
853 fprintf(stderr
, "Unhandled format %s with blocksize %u\n",
854 util_format_short_name(src
->format
), blocksize
);
860 /* Initialize the surface. */
861 dst_view
= r600_create_surface_custom(ctx
, dst
, &dst_templ
,
862 dst_width
, dst_height
);
864 /* Initialize the sampler view. */
865 src_view
= si_create_sampler_view_custom(ctx
, src
, &src_templ
,
866 src_width0
, src_height0
,
869 u_box_3d(dstx
, dsty
, dstz
, abs(src_box
->width
), abs(src_box
->height
),
870 abs(src_box
->depth
), &dstbox
);
873 si_blitter_begin(ctx
, SI_COPY
);
874 util_blitter_blit_generic(sctx
->blitter
, dst_view
, &dstbox
,
875 src_view
, src_box
, src_width0
, src_height0
,
876 PIPE_MASK_RGBAZS
, PIPE_TEX_FILTER_NEAREST
, NULL
,
880 pipe_surface_reference(&dst_view
, NULL
);
881 pipe_sampler_view_reference(&src_view
, NULL
);
884 static bool do_hardware_msaa_resolve(struct pipe_context
*ctx
,
885 const struct pipe_blit_info
*info
)
887 struct si_context
*sctx
= (struct si_context
*)ctx
;
888 struct r600_texture
*src
= (struct r600_texture
*)info
->src
.resource
;
889 struct r600_texture
*dst
= (struct r600_texture
*)info
->dst
.resource
;
890 unsigned dst_width
= u_minify(info
->dst
.resource
->width0
, info
->dst
.level
);
891 unsigned dst_height
= u_minify(info
->dst
.resource
->height0
, info
->dst
.level
);
892 enum pipe_format format
= info
->src
.format
;
893 unsigned sample_mask
= ~0;
894 struct pipe_resource
*tmp
, templ
;
895 struct pipe_blit_info blit
;
897 /* Check basic requirements for hw resolve. */
898 if (!(info
->src
.resource
->nr_samples
> 1 &&
899 info
->dst
.resource
->nr_samples
<= 1 &&
900 !util_format_is_pure_integer(format
) &&
901 !util_format_is_depth_or_stencil(format
) &&
902 util_max_layer(info
->src
.resource
, 0) == 0))
905 /* Hardware MSAA resolve doesn't work if SPI format = NORM16_ABGR and
906 * the format is R16G16. Use R16A16, which does work.
908 if (format
== PIPE_FORMAT_R16G16_UNORM
)
909 format
= PIPE_FORMAT_R16A16_UNORM
;
910 if (format
== PIPE_FORMAT_R16G16_SNORM
)
911 format
= PIPE_FORMAT_R16A16_SNORM
;
913 /* Check the remaining requirements for hw resolve. */
914 if (util_max_layer(info
->dst
.resource
, info
->dst
.level
) == 0 &&
915 !info
->scissor_enable
&&
916 (info
->mask
& PIPE_MASK_RGBA
) == PIPE_MASK_RGBA
&&
917 util_is_format_compatible(util_format_description(info
->src
.format
),
918 util_format_description(info
->dst
.format
)) &&
919 dst_width
== info
->src
.resource
->width0
&&
920 dst_height
== info
->src
.resource
->height0
&&
921 info
->dst
.box
.x
== 0 &&
922 info
->dst
.box
.y
== 0 &&
923 info
->dst
.box
.width
== dst_width
&&
924 info
->dst
.box
.height
== dst_height
&&
925 info
->dst
.box
.depth
== 1 &&
926 info
->src
.box
.x
== 0 &&
927 info
->src
.box
.y
== 0 &&
928 info
->src
.box
.width
== dst_width
&&
929 info
->src
.box
.height
== dst_height
&&
930 info
->src
.box
.depth
== 1 &&
931 dst
->surface
.level
[info
->dst
.level
].mode
>= RADEON_SURF_MODE_1D
&&
932 (!dst
->cmask
.size
|| !dst
->dirty_level_mask
)) { /* dst cannot be fast-cleared */
933 /* Check the last constraint. */
934 if (src
->surface
.micro_tile_mode
!= dst
->surface
.micro_tile_mode
) {
935 /* The next fast clear will switch to this mode to
936 * get direct hw resolve next time if the mode is
939 src
->last_msaa_resolve_target_micro_mode
=
940 dst
->surface
.micro_tile_mode
;
941 goto resolve_to_temp
;
944 /* Resolving into a surface with DCC is unsupported. Since
945 * it's being overwritten anyway, clear it to uncompressed.
946 * This is still the fastest codepath even with this clear.
948 if (dst
->dcc_offset
&&
949 dst
->surface
.level
[info
->dst
.level
].dcc_enabled
) {
950 vi_dcc_clear_level(&sctx
->b
, dst
, info
->dst
.level
,
952 dst
->dirty_level_mask
&= ~(1 << info
->dst
.level
);
955 /* Resolve directly from src to dst. */
956 si_blitter_begin(ctx
, SI_COLOR_RESOLVE
|
957 (info
->render_condition_enable
? 0 : SI_DISABLE_RENDER_COND
));
958 util_blitter_custom_resolve_color(sctx
->blitter
,
959 info
->dst
.resource
, info
->dst
.level
,
961 info
->src
.resource
, info
->src
.box
.z
,
962 sample_mask
, sctx
->custom_blend_resolve
,
969 /* Shader-based resolve is VERY SLOW. Instead, resolve into
970 * a temporary texture and blit.
972 memset(&templ
, 0, sizeof(templ
));
973 templ
.target
= PIPE_TEXTURE_2D
;
974 templ
.format
= info
->src
.resource
->format
;
975 templ
.width0
= info
->src
.resource
->width0
;
976 templ
.height0
= info
->src
.resource
->height0
;
978 templ
.array_size
= 1;
979 templ
.usage
= PIPE_USAGE_DEFAULT
;
980 templ
.flags
= R600_RESOURCE_FLAG_FORCE_TILING
|
981 R600_RESOURCE_FLAG_DISABLE_DCC
;
983 /* The src and dst microtile modes must be the same. */
984 if (src
->surface
.micro_tile_mode
== V_009910_ADDR_SURF_DISPLAY_MICRO_TILING
)
985 templ
.bind
= PIPE_BIND_SCANOUT
;
989 tmp
= ctx
->screen
->resource_create(ctx
->screen
, &templ
);
993 assert(src
->surface
.micro_tile_mode
==
994 ((struct r600_texture
*)tmp
)->surface
.micro_tile_mode
);
997 si_blitter_begin(ctx
, SI_COLOR_RESOLVE
|
998 (info
->render_condition_enable
? 0 : SI_DISABLE_RENDER_COND
));
999 util_blitter_custom_resolve_color(sctx
->blitter
, tmp
, 0, 0,
1000 info
->src
.resource
, info
->src
.box
.z
,
1001 sample_mask
, sctx
->custom_blend_resolve
,
1003 si_blitter_end(ctx
);
1007 blit
.src
.resource
= tmp
;
1010 si_blitter_begin(ctx
, SI_BLIT
|
1011 (info
->render_condition_enable
? 0 : SI_DISABLE_RENDER_COND
));
1012 util_blitter_blit(sctx
->blitter
, &blit
);
1013 si_blitter_end(ctx
);
1015 pipe_resource_reference(&tmp
, NULL
);
1019 static void si_blit(struct pipe_context
*ctx
,
1020 const struct pipe_blit_info
*info
)
1022 struct si_context
*sctx
= (struct si_context
*)ctx
;
1024 if (do_hardware_msaa_resolve(ctx
, info
)) {
1028 assert(util_blitter_is_blit_supported(sctx
->blitter
, info
));
1030 /* The driver doesn't decompress resources automatically while
1031 * u_blitter is rendering. */
1032 si_decompress_subresource(ctx
, info
->src
.resource
, info
->mask
,
1035 info
->src
.box
.z
+ info
->src
.box
.depth
- 1);
1037 if (sctx
->screen
->b
.debug_flags
& DBG_FORCE_DMA
&&
1038 util_try_blit_via_copy_region(ctx
, info
))
1041 si_blitter_begin(ctx
, SI_BLIT
|
1042 (info
->render_condition_enable
? 0 : SI_DISABLE_RENDER_COND
));
1043 util_blitter_blit(sctx
->blitter
, info
);
1044 si_blitter_end(ctx
);
1047 static boolean
si_generate_mipmap(struct pipe_context
*ctx
,
1048 struct pipe_resource
*tex
,
1049 enum pipe_format format
,
1050 unsigned base_level
, unsigned last_level
,
1051 unsigned first_layer
, unsigned last_layer
)
1053 struct si_context
*sctx
= (struct si_context
*)ctx
;
1054 struct r600_texture
*rtex
= (struct r600_texture
*)tex
;
1056 if (!util_blitter_is_copy_supported(sctx
->blitter
, tex
, tex
))
1059 /* The driver doesn't decompress resources automatically while
1060 * u_blitter is rendering. */
1061 si_decompress_subresource(ctx
, tex
, PIPE_MASK_RGBAZS
,
1062 base_level
, first_layer
, last_layer
);
1064 /* Clear dirty_level_mask for the levels that will be overwritten. */
1065 assert(base_level
< last_level
);
1066 rtex
->dirty_level_mask
&= ~u_bit_consecutive(base_level
+ 1,
1067 last_level
- base_level
);
1069 si_blitter_begin(ctx
, SI_BLIT
| SI_DISABLE_RENDER_COND
);
1070 util_blitter_generate_mipmap(sctx
->blitter
, tex
, format
,
1071 base_level
, last_level
,
1072 first_layer
, last_layer
);
1073 si_blitter_end(ctx
);
1077 static void si_flush_resource(struct pipe_context
*ctx
,
1078 struct pipe_resource
*res
)
1080 struct r600_texture
*rtex
= (struct r600_texture
*)res
;
1082 assert(res
->target
!= PIPE_BUFFER
);
1083 assert(!rtex
->dcc_separate_buffer
|| rtex
->dcc_gather_statistics
);
1085 /* st/dri calls flush twice per frame (not a bug), this prevents double
1087 if (rtex
->dcc_separate_buffer
&& !rtex
->separate_dcc_dirty
)
1090 if (!rtex
->is_depth
&& (rtex
->cmask
.size
|| rtex
->dcc_offset
)) {
1091 si_blit_decompress_color(ctx
, rtex
, 0, res
->last_level
,
1092 0, util_max_layer(res
, 0),
1093 rtex
->dcc_separate_buffer
!= NULL
);
1096 /* Always do the analysis even if DCC is disabled at the moment. */
1097 if (rtex
->dcc_gather_statistics
&& rtex
->separate_dcc_dirty
) {
1098 rtex
->separate_dcc_dirty
= false;
1099 vi_separate_dcc_process_and_reset_stats(ctx
, rtex
);
1103 static void si_decompress_dcc(struct pipe_context
*ctx
,
1104 struct r600_texture
*rtex
)
1106 if (!rtex
->dcc_offset
)
1109 si_blit_decompress_color(ctx
, rtex
, 0, rtex
->resource
.b
.b
.last_level
,
1110 0, util_max_layer(&rtex
->resource
.b
.b
, 0),
1114 static void si_pipe_clear_buffer(struct pipe_context
*ctx
,
1115 struct pipe_resource
*dst
,
1116 unsigned offset
, unsigned size
,
1117 const void *clear_value_ptr
,
1118 int clear_value_size
)
1120 struct si_context
*sctx
= (struct si_context
*)ctx
;
1121 uint32_t dword_value
;
1124 assert(offset
% clear_value_size
== 0);
1125 assert(size
% clear_value_size
== 0);
1127 if (clear_value_size
> 4) {
1128 const uint32_t *u32
= clear_value_ptr
;
1129 bool clear_dword_duplicated
= true;
1131 /* See if we can lower large fills to dword fills. */
1132 for (i
= 1; i
< clear_value_size
/ 4; i
++)
1133 if (u32
[0] != u32
[i
]) {
1134 clear_dword_duplicated
= false;
1138 if (!clear_dword_duplicated
) {
1139 /* Use transform feedback for 64-bit, 96-bit, and
1142 union pipe_color_union clear_value
;
1144 memcpy(&clear_value
, clear_value_ptr
, clear_value_size
);
1145 si_blitter_begin(ctx
, SI_DISABLE_RENDER_COND
);
1146 util_blitter_clear_buffer(sctx
->blitter
, dst
, offset
,
1147 size
, clear_value_size
/ 4,
1149 si_blitter_end(ctx
);
1154 /* Expand the clear value to a dword. */
1155 switch (clear_value_size
) {
1157 dword_value
= *(uint8_t*)clear_value_ptr
;
1158 dword_value
|= (dword_value
<< 8) |
1159 (dword_value
<< 16) |
1160 (dword_value
<< 24);
1163 dword_value
= *(uint16_t*)clear_value_ptr
;
1164 dword_value
|= dword_value
<< 16;
1167 dword_value
= *(uint32_t*)clear_value_ptr
;
1170 sctx
->b
.clear_buffer(ctx
, dst
, offset
, size
, dword_value
,
1171 R600_COHERENCY_SHADER
);
1174 void si_init_blit_functions(struct si_context
*sctx
)
1176 sctx
->b
.b
.clear
= si_clear
;
1177 sctx
->b
.b
.clear_buffer
= si_pipe_clear_buffer
;
1178 sctx
->b
.b
.clear_render_target
= si_clear_render_target
;
1179 sctx
->b
.b
.clear_depth_stencil
= si_clear_depth_stencil
;
1180 sctx
->b
.b
.resource_copy_region
= si_resource_copy_region
;
1181 sctx
->b
.b
.blit
= si_blit
;
1182 sctx
->b
.b
.flush_resource
= si_flush_resource
;
1183 sctx
->b
.b
.generate_mipmap
= si_generate_mipmap
;
1184 sctx
->b
.blit_decompress_depth
= si_blit_decompress_depth
;
1185 sctx
->b
.decompress_dcc
= si_decompress_dcc
;