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.
23 #include "r600_pipe.h"
24 #include "compute_memory_pool.h"
25 #include "evergreen_compute.h"
26 #include "util/u_surface.h"
27 #include "util/format/u_format.h"
28 #include "evergreend.h"
30 enum r600_blitter_op
/* bitmask */
32 R600_SAVE_FRAGMENT_STATE
= 1,
33 R600_SAVE_TEXTURES
= 2,
34 R600_SAVE_FRAMEBUFFER
= 4,
35 R600_DISABLE_RENDER_COND
= 8,
37 R600_CLEAR
= R600_SAVE_FRAGMENT_STATE
,
39 R600_CLEAR_SURFACE
= R600_SAVE_FRAGMENT_STATE
| R600_SAVE_FRAMEBUFFER
,
41 R600_COPY_BUFFER
= R600_DISABLE_RENDER_COND
,
43 R600_COPY_TEXTURE
= R600_SAVE_FRAGMENT_STATE
| R600_SAVE_FRAMEBUFFER
| R600_SAVE_TEXTURES
|
44 R600_DISABLE_RENDER_COND
,
46 R600_BLIT
= R600_SAVE_FRAGMENT_STATE
| R600_SAVE_FRAMEBUFFER
| R600_SAVE_TEXTURES
,
48 R600_DECOMPRESS
= R600_SAVE_FRAGMENT_STATE
| R600_SAVE_FRAMEBUFFER
| R600_DISABLE_RENDER_COND
,
50 R600_COLOR_RESOLVE
= R600_SAVE_FRAGMENT_STATE
| R600_SAVE_FRAMEBUFFER
53 static void r600_blitter_begin(struct pipe_context
*ctx
, enum r600_blitter_op op
)
55 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
57 if (rctx
->cmd_buf_is_compute
) {
58 rctx
->b
.gfx
.flush(rctx
, PIPE_FLUSH_ASYNC
, NULL
);
59 rctx
->cmd_buf_is_compute
= false;
62 util_blitter_save_vertex_buffer_slot(rctx
->blitter
, rctx
->vertex_buffer_state
.vb
);
63 util_blitter_save_vertex_elements(rctx
->blitter
, rctx
->vertex_fetch_shader
.cso
);
64 util_blitter_save_vertex_shader(rctx
->blitter
, rctx
->vs_shader
);
65 util_blitter_save_geometry_shader(rctx
->blitter
, rctx
->gs_shader
);
66 util_blitter_save_tessctrl_shader(rctx
->blitter
, rctx
->tcs_shader
);
67 util_blitter_save_tesseval_shader(rctx
->blitter
, rctx
->tes_shader
);
68 util_blitter_save_so_targets(rctx
->blitter
, rctx
->b
.streamout
.num_targets
,
69 (struct pipe_stream_output_target
**)rctx
->b
.streamout
.targets
);
70 util_blitter_save_rasterizer(rctx
->blitter
, rctx
->rasterizer_state
.cso
);
72 if (op
& R600_SAVE_FRAGMENT_STATE
) {
73 util_blitter_save_viewport(rctx
->blitter
, &rctx
->b
.viewports
.states
[0]);
74 util_blitter_save_scissor(rctx
->blitter
, &rctx
->b
.scissors
.states
[0]);
75 util_blitter_save_fragment_shader(rctx
->blitter
, rctx
->ps_shader
);
76 util_blitter_save_blend(rctx
->blitter
, rctx
->blend_state
.cso
);
77 util_blitter_save_depth_stencil_alpha(rctx
->blitter
, rctx
->dsa_state
.cso
);
78 util_blitter_save_stencil_ref(rctx
->blitter
, &rctx
->stencil_ref
.pipe_state
);
79 util_blitter_save_sample_mask(rctx
->blitter
, rctx
->sample_mask
.sample_mask
);
82 if (op
& R600_SAVE_FRAMEBUFFER
)
83 util_blitter_save_framebuffer(rctx
->blitter
, &rctx
->framebuffer
.state
);
85 if (op
& R600_SAVE_TEXTURES
) {
86 util_blitter_save_fragment_sampler_states(
87 rctx
->blitter
, util_last_bit(rctx
->samplers
[PIPE_SHADER_FRAGMENT
].states
.enabled_mask
),
88 (void**)rctx
->samplers
[PIPE_SHADER_FRAGMENT
].states
.states
);
90 util_blitter_save_fragment_sampler_views(
91 rctx
->blitter
, util_last_bit(rctx
->samplers
[PIPE_SHADER_FRAGMENT
].views
.enabled_mask
),
92 (struct pipe_sampler_view
**)rctx
->samplers
[PIPE_SHADER_FRAGMENT
].views
.views
);
95 if (op
& R600_DISABLE_RENDER_COND
)
96 rctx
->b
.render_cond_force_off
= true;
99 static void r600_blitter_end(struct pipe_context
*ctx
)
101 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
103 rctx
->b
.render_cond_force_off
= false;
106 static unsigned u_max_sample(struct pipe_resource
*r
)
108 return r
->nr_samples
? r
->nr_samples
- 1 : 0;
111 static void r600_blit_decompress_depth(struct pipe_context
*ctx
,
112 struct r600_texture
*texture
,
113 struct r600_texture
*staging
,
114 unsigned first_level
, unsigned last_level
,
115 unsigned first_layer
, unsigned last_layer
,
116 unsigned first_sample
, unsigned last_sample
)
118 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
119 unsigned layer
, level
, sample
, checked_last_layer
, max_layer
, max_sample
;
120 struct r600_texture
*flushed_depth_texture
= staging
?
121 staging
: texture
->flushed_depth_texture
;
122 const struct util_format_description
*desc
=
123 util_format_description(texture
->resource
.b
.b
.format
);
126 if (!staging
&& !texture
->dirty_level_mask
)
129 max_sample
= u_max_sample(&texture
->resource
.b
.b
);
131 /* XXX Decompressing MSAA depth textures is broken on R6xx.
132 * There is also a hardlock if CMASK and FMASK are not present.
133 * Just skip this until we find out how to fix it. */
134 if (rctx
->b
.chip_class
== R600
&& max_sample
> 0) {
135 texture
->dirty_level_mask
= 0;
139 if (rctx
->b
.family
== CHIP_RV610
|| rctx
->b
.family
== CHIP_RV630
||
140 rctx
->b
.family
== CHIP_RV620
|| rctx
->b
.family
== CHIP_RV635
)
145 /* Enable decompression in DB_RENDER_CONTROL */
146 rctx
->db_misc_state
.flush_depthstencil_through_cb
= true;
147 rctx
->db_misc_state
.copy_depth
= util_format_has_depth(desc
);
148 rctx
->db_misc_state
.copy_stencil
= util_format_has_stencil(desc
);
149 rctx
->db_misc_state
.copy_sample
= first_sample
;
150 r600_mark_atom_dirty(rctx
, &rctx
->db_misc_state
.atom
);
152 for (level
= first_level
; level
<= last_level
; level
++) {
153 if (!staging
&& !(texture
->dirty_level_mask
& (1 << level
)))
156 /* The smaller the mipmap level, the less layers there are
157 * as far as 3D textures are concerned. */
158 max_layer
= util_max_layer(&texture
->resource
.b
.b
, level
);
159 checked_last_layer
= last_layer
< max_layer
? last_layer
: max_layer
;
161 for (layer
= first_layer
; layer
<= checked_last_layer
; layer
++) {
162 for (sample
= first_sample
; sample
<= last_sample
; sample
++) {
163 struct pipe_surface
*zsurf
, *cbsurf
, surf_tmpl
;
165 if (sample
!= rctx
->db_misc_state
.copy_sample
) {
166 rctx
->db_misc_state
.copy_sample
= sample
;
167 r600_mark_atom_dirty(rctx
, &rctx
->db_misc_state
.atom
);
170 surf_tmpl
.format
= texture
->resource
.b
.b
.format
;
171 surf_tmpl
.u
.tex
.level
= level
;
172 surf_tmpl
.u
.tex
.first_layer
= layer
;
173 surf_tmpl
.u
.tex
.last_layer
= layer
;
175 zsurf
= ctx
->create_surface(ctx
, &texture
->resource
.b
.b
, &surf_tmpl
);
177 surf_tmpl
.format
= flushed_depth_texture
->resource
.b
.b
.format
;
178 cbsurf
= ctx
->create_surface(ctx
,
179 &flushed_depth_texture
->resource
.b
.b
, &surf_tmpl
);
181 r600_blitter_begin(ctx
, R600_DECOMPRESS
);
182 util_blitter_custom_depth_stencil(rctx
->blitter
, zsurf
, cbsurf
, 1 << sample
,
183 rctx
->custom_dsa_flush
, depth
);
184 r600_blitter_end(ctx
);
186 pipe_surface_reference(&zsurf
, NULL
);
187 pipe_surface_reference(&cbsurf
, NULL
);
191 /* The texture will always be dirty if some layers or samples aren't flushed.
192 * I don't think this case occurs often though. */
194 first_layer
== 0 && last_layer
== max_layer
&&
195 first_sample
== 0 && last_sample
== max_sample
) {
196 texture
->dirty_level_mask
&= ~(1 << level
);
200 /* reenable compression in DB_RENDER_CONTROL */
201 rctx
->db_misc_state
.flush_depthstencil_through_cb
= false;
202 r600_mark_atom_dirty(rctx
, &rctx
->db_misc_state
.atom
);
205 static void r600_blit_decompress_depth_in_place(struct r600_context
*rctx
,
206 struct r600_texture
*texture
,
207 bool is_stencil_sampler
,
208 unsigned first_level
, unsigned last_level
,
209 unsigned first_layer
, unsigned last_layer
)
211 struct pipe_surface
*zsurf
, surf_tmpl
= {{0}};
212 unsigned layer
, max_layer
, checked_last_layer
, level
;
213 unsigned *dirty_level_mask
;
215 /* Enable decompression in DB_RENDER_CONTROL */
216 if (is_stencil_sampler
) {
217 rctx
->db_misc_state
.flush_stencil_inplace
= true;
218 dirty_level_mask
= &texture
->stencil_dirty_level_mask
;
220 rctx
->db_misc_state
.flush_depth_inplace
= true;
221 dirty_level_mask
= &texture
->dirty_level_mask
;
223 r600_mark_atom_dirty(rctx
, &rctx
->db_misc_state
.atom
);
225 surf_tmpl
.format
= texture
->resource
.b
.b
.format
;
227 for (level
= first_level
; level
<= last_level
; level
++) {
228 if (!(*dirty_level_mask
& (1 << level
)))
231 surf_tmpl
.u
.tex
.level
= level
;
233 /* The smaller the mipmap level, the less layers there are
234 * as far as 3D textures are concerned. */
235 max_layer
= util_max_layer(&texture
->resource
.b
.b
, level
);
236 checked_last_layer
= last_layer
< max_layer
? last_layer
: max_layer
;
238 for (layer
= first_layer
; layer
<= checked_last_layer
; layer
++) {
239 surf_tmpl
.u
.tex
.first_layer
= layer
;
240 surf_tmpl
.u
.tex
.last_layer
= layer
;
242 zsurf
= rctx
->b
.b
.create_surface(&rctx
->b
.b
, &texture
->resource
.b
.b
, &surf_tmpl
);
244 r600_blitter_begin(&rctx
->b
.b
, R600_DECOMPRESS
);
245 util_blitter_custom_depth_stencil(rctx
->blitter
, zsurf
, NULL
, ~0,
246 rctx
->custom_dsa_flush
, 1.0f
);
247 r600_blitter_end(&rctx
->b
.b
);
249 pipe_surface_reference(&zsurf
, NULL
);
252 /* The texture will always be dirty if some layers or samples aren't flushed.
253 * I don't think this case occurs often though. */
254 if (first_layer
== 0 && last_layer
== max_layer
) {
255 *dirty_level_mask
&= ~(1 << level
);
259 /* Disable decompression in DB_RENDER_CONTROL */
260 rctx
->db_misc_state
.flush_depth_inplace
= false;
261 rctx
->db_misc_state
.flush_stencil_inplace
= false;
262 r600_mark_atom_dirty(rctx
, &rctx
->db_misc_state
.atom
);
265 void r600_decompress_depth_textures(struct r600_context
*rctx
,
266 struct r600_samplerview_state
*textures
)
269 unsigned depth_texture_mask
= textures
->compressed_depthtex_mask
;
271 while (depth_texture_mask
) {
272 struct pipe_sampler_view
*view
;
273 struct r600_pipe_sampler_view
*rview
;
274 struct r600_texture
*tex
;
276 i
= u_bit_scan(&depth_texture_mask
);
278 view
= &textures
->views
[i
]->base
;
280 rview
= (struct r600_pipe_sampler_view
*)view
;
282 tex
= (struct r600_texture
*)view
->texture
;
283 assert(tex
->db_compatible
);
285 if (r600_can_sample_zs(tex
, rview
->is_stencil_sampler
)) {
286 r600_blit_decompress_depth_in_place(rctx
, tex
,
287 rview
->is_stencil_sampler
,
288 view
->u
.tex
.first_level
, view
->u
.tex
.last_level
,
289 0, util_max_layer(&tex
->resource
.b
.b
, view
->u
.tex
.first_level
));
291 r600_blit_decompress_depth(&rctx
->b
.b
, tex
, NULL
,
292 view
->u
.tex
.first_level
, view
->u
.tex
.last_level
,
293 0, util_max_layer(&tex
->resource
.b
.b
, view
->u
.tex
.first_level
),
294 0, u_max_sample(&tex
->resource
.b
.b
));
299 void r600_decompress_depth_images(struct r600_context
*rctx
,
300 struct r600_image_state
*images
)
303 unsigned depth_texture_mask
= images
->compressed_depthtex_mask
;
305 while (depth_texture_mask
) {
306 struct r600_image_view
*view
;
307 struct r600_texture
*tex
;
309 i
= u_bit_scan(&depth_texture_mask
);
311 view
= &images
->views
[i
];
314 tex
= (struct r600_texture
*)view
->base
.resource
;
315 assert(tex
->db_compatible
);
317 if (r600_can_sample_zs(tex
, false)) {
318 r600_blit_decompress_depth_in_place(rctx
, tex
,
320 view
->base
.u
.tex
.level
,
321 view
->base
.u
.tex
.level
,
322 0, util_max_layer(&tex
->resource
.b
.b
, view
->base
.u
.tex
.level
));
324 r600_blit_decompress_depth(&rctx
->b
.b
, tex
, NULL
,
325 view
->base
.u
.tex
.level
,
326 view
->base
.u
.tex
.level
,
327 0, util_max_layer(&tex
->resource
.b
.b
, view
->base
.u
.tex
.level
),
328 0, u_max_sample(&tex
->resource
.b
.b
));
333 static void r600_blit_decompress_color(struct pipe_context
*ctx
,
334 struct r600_texture
*rtex
,
335 unsigned first_level
, unsigned last_level
,
336 unsigned first_layer
, unsigned last_layer
)
338 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
339 unsigned layer
, level
, checked_last_layer
, max_layer
;
341 if (!rtex
->dirty_level_mask
)
344 for (level
= first_level
; level
<= last_level
; level
++) {
345 if (!(rtex
->dirty_level_mask
& (1 << level
)))
348 /* The smaller the mipmap level, the less layers there are
349 * as far as 3D textures are concerned. */
350 max_layer
= util_max_layer(&rtex
->resource
.b
.b
, level
);
351 checked_last_layer
= last_layer
< max_layer
? last_layer
: max_layer
;
353 for (layer
= first_layer
; layer
<= checked_last_layer
; layer
++) {
354 struct pipe_surface
*cbsurf
, surf_tmpl
;
356 surf_tmpl
.format
= rtex
->resource
.b
.b
.format
;
357 surf_tmpl
.u
.tex
.level
= level
;
358 surf_tmpl
.u
.tex
.first_layer
= layer
;
359 surf_tmpl
.u
.tex
.last_layer
= layer
;
360 cbsurf
= ctx
->create_surface(ctx
, &rtex
->resource
.b
.b
, &surf_tmpl
);
362 r600_blitter_begin(ctx
, R600_DECOMPRESS
);
363 util_blitter_custom_color(rctx
->blitter
, cbsurf
,
364 rtex
->fmask
.size
? rctx
->custom_blend_decompress
: rctx
->custom_blend_fastclear
);
365 r600_blitter_end(ctx
);
367 pipe_surface_reference(&cbsurf
, NULL
);
370 /* The texture will always be dirty if some layers aren't flushed.
371 * I don't think this case occurs often though. */
372 if (first_layer
== 0 && last_layer
== max_layer
) {
373 rtex
->dirty_level_mask
&= ~(1 << level
);
378 void r600_decompress_color_textures(struct r600_context
*rctx
,
379 struct r600_samplerview_state
*textures
)
382 unsigned mask
= textures
->compressed_colortex_mask
;
385 struct pipe_sampler_view
*view
;
386 struct r600_texture
*tex
;
388 i
= u_bit_scan(&mask
);
390 view
= &textures
->views
[i
]->base
;
393 tex
= (struct r600_texture
*)view
->texture
;
394 assert(tex
->cmask
.size
);
396 r600_blit_decompress_color(&rctx
->b
.b
, tex
,
397 view
->u
.tex
.first_level
, view
->u
.tex
.last_level
,
398 0, util_max_layer(&tex
->resource
.b
.b
, view
->u
.tex
.first_level
));
402 void r600_decompress_color_images(struct r600_context
*rctx
,
403 struct r600_image_state
*images
)
406 unsigned mask
= images
->compressed_colortex_mask
;
409 struct r600_image_view
*view
;
410 struct r600_texture
*tex
;
412 i
= u_bit_scan(&mask
);
414 view
= &images
->views
[i
];
417 tex
= (struct r600_texture
*)view
->base
.resource
;
418 assert(tex
->cmask
.size
);
420 r600_blit_decompress_color(&rctx
->b
.b
, tex
,
421 view
->base
.u
.tex
.level
, view
->base
.u
.tex
.level
,
422 view
->base
.u
.tex
.first_layer
,
423 view
->base
.u
.tex
.last_layer
);
427 /* Helper for decompressing a portion of a color or depth resource before
428 * blitting if any decompression is needed.
429 * The driver doesn't decompress resources automatically while u_blitter is
431 static bool r600_decompress_subresource(struct pipe_context
*ctx
,
432 struct pipe_resource
*tex
,
434 unsigned first_layer
, unsigned last_layer
)
436 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
437 struct r600_texture
*rtex
= (struct r600_texture
*)tex
;
439 if (rtex
->db_compatible
) {
440 if (r600_can_sample_zs(rtex
, false)) {
441 r600_blit_decompress_depth_in_place(rctx
, rtex
, false,
443 first_layer
, last_layer
);
444 if (rtex
->surface
.has_stencil
) {
445 r600_blit_decompress_depth_in_place(rctx
, rtex
, true,
447 first_layer
, last_layer
);
450 if (!r600_init_flushed_depth_texture(ctx
, tex
, NULL
))
451 return false; /* error */
453 r600_blit_decompress_depth(ctx
, rtex
, NULL
,
455 first_layer
, last_layer
,
456 0, u_max_sample(tex
));
458 } else if (rtex
->cmask
.size
) {
459 r600_blit_decompress_color(ctx
, rtex
, level
, level
,
460 first_layer
, last_layer
);
465 static void r600_clear(struct pipe_context
*ctx
, unsigned buffers
,
466 const struct pipe_scissor_state
*scissor_state
,
467 const union pipe_color_union
*color
,
468 double depth
, unsigned stencil
)
470 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
471 struct pipe_framebuffer_state
*fb
= &rctx
->framebuffer
.state
;
473 if (buffers
& PIPE_CLEAR_COLOR
&& rctx
->b
.chip_class
>= EVERGREEN
) {
474 evergreen_do_fast_color_clear(&rctx
->b
, fb
, &rctx
->framebuffer
.atom
,
475 &buffers
, NULL
, color
);
477 return; /* all buffers have been fast cleared */
480 if (buffers
& PIPE_CLEAR_COLOR
) {
483 /* These buffers cannot use fast clear, make sure to disable expansion. */
484 for (i
= 0; i
< fb
->nr_cbufs
; i
++) {
485 struct r600_texture
*tex
;
487 /* If not clearing this buffer, skip. */
488 if (!(buffers
& (PIPE_CLEAR_COLOR0
<< i
)))
494 tex
= (struct r600_texture
*)fb
->cbufs
[i
]->texture
;
495 if (tex
->fmask
.size
== 0)
496 tex
->dirty_level_mask
&= ~(1 << fb
->cbufs
[i
]->u
.tex
.level
);
500 /* if hyperz enabled just clear hyperz */
501 if (fb
->zsbuf
&& (buffers
& PIPE_CLEAR_DEPTH
)) {
502 struct r600_texture
*rtex
;
503 unsigned level
= fb
->zsbuf
->u
.tex
.level
;
505 rtex
= (struct r600_texture
*)fb
->zsbuf
->texture
;
507 /* We can't use hyperz fast clear if each slice of a texture
508 * array are clear to different value. To simplify code just
509 * disable fast clear for texture array.
511 if (r600_htile_enabled(rtex
, level
) &&
512 fb
->zsbuf
->u
.tex
.first_layer
== 0 &&
513 fb
->zsbuf
->u
.tex
.last_layer
== util_max_layer(&rtex
->resource
.b
.b
, level
)) {
514 if (rtex
->depth_clear_value
!= depth
) {
515 rtex
->depth_clear_value
= depth
;
516 r600_mark_atom_dirty(rctx
, &rctx
->db_state
.atom
);
518 rctx
->db_misc_state
.htile_clear
= true;
519 r600_mark_atom_dirty(rctx
, &rctx
->db_misc_state
.atom
);
523 r600_blitter_begin(ctx
, R600_CLEAR
);
524 util_blitter_clear(rctx
->blitter
, fb
->width
, fb
->height
,
525 util_framebuffer_get_num_layers(fb
),
526 buffers
, color
, depth
, stencil
,
527 util_framebuffer_get_num_samples(fb
) > 1);
528 r600_blitter_end(ctx
);
530 /* disable fast clear */
531 if (rctx
->db_misc_state
.htile_clear
) {
532 rctx
->db_misc_state
.htile_clear
= false;
533 r600_mark_atom_dirty(rctx
, &rctx
->db_misc_state
.atom
);
537 static void r600_clear_render_target(struct pipe_context
*ctx
,
538 struct pipe_surface
*dst
,
539 const union pipe_color_union
*color
,
540 unsigned dstx
, unsigned dsty
,
541 unsigned width
, unsigned height
,
542 bool render_condition_enabled
)
544 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
546 r600_blitter_begin(ctx
, R600_CLEAR_SURFACE
|
547 (render_condition_enabled
? 0 : R600_DISABLE_RENDER_COND
));
548 util_blitter_clear_render_target(rctx
->blitter
, dst
, color
,
549 dstx
, dsty
, width
, height
);
550 r600_blitter_end(ctx
);
553 static void r600_clear_depth_stencil(struct pipe_context
*ctx
,
554 struct pipe_surface
*dst
,
555 unsigned clear_flags
,
558 unsigned dstx
, unsigned dsty
,
559 unsigned width
, unsigned height
,
560 bool render_condition_enabled
)
562 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
564 r600_blitter_begin(ctx
, R600_CLEAR_SURFACE
|
565 (render_condition_enabled
? 0 : R600_DISABLE_RENDER_COND
));
566 util_blitter_clear_depth_stencil(rctx
->blitter
, dst
, clear_flags
, depth
, stencil
,
567 dstx
, dsty
, width
, height
);
568 r600_blitter_end(ctx
);
571 static void r600_copy_buffer(struct pipe_context
*ctx
, struct pipe_resource
*dst
, unsigned dstx
,
572 struct pipe_resource
*src
, const struct pipe_box
*src_box
)
574 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
576 if (rctx
->screen
->b
.has_cp_dma
) {
577 r600_cp_dma_copy_buffer(rctx
, dst
, dstx
, src
, src_box
->x
, src_box
->width
);
579 else if (rctx
->screen
->b
.has_streamout
&&
580 /* Require 4-byte alignment. */
581 dstx
% 4 == 0 && src_box
->x
% 4 == 0 && src_box
->width
% 4 == 0) {
583 r600_blitter_begin(ctx
, R600_COPY_BUFFER
);
584 util_blitter_copy_buffer(rctx
->blitter
, dst
, dstx
, src
, src_box
->x
, src_box
->width
);
585 r600_blitter_end(ctx
);
587 util_resource_copy_region(ctx
, dst
, 0, dstx
, 0, 0, src
, 0, src_box
);
592 * Global buffers are not really resources, they are are actually offsets
593 * into a single global resource (r600_screen::global_pool). The means
594 * they don't have their own buf handle, so they cannot be passed
595 * to r600_copy_buffer() and must be handled separately.
597 static void r600_copy_global_buffer(struct pipe_context
*ctx
,
598 struct pipe_resource
*dst
, unsigned
599 dstx
, struct pipe_resource
*src
,
600 const struct pipe_box
*src_box
)
602 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
603 struct compute_memory_pool
*pool
= rctx
->screen
->global_pool
;
604 struct pipe_box new_src_box
= *src_box
;
606 if (src
->bind
& PIPE_BIND_GLOBAL
) {
607 struct r600_resource_global
*rsrc
=
608 (struct r600_resource_global
*)src
;
609 struct compute_memory_item
*item
= rsrc
->chunk
;
611 if (is_item_in_pool(item
)) {
612 new_src_box
.x
+= 4 * item
->start_in_dw
;
613 src
= (struct pipe_resource
*)pool
->bo
;
615 if (item
->real_buffer
== NULL
) {
617 r600_compute_buffer_alloc_vram(pool
->screen
,
618 item
->size_in_dw
* 4);
620 src
= (struct pipe_resource
*)item
->real_buffer
;
623 if (dst
->bind
& PIPE_BIND_GLOBAL
) {
624 struct r600_resource_global
*rdst
=
625 (struct r600_resource_global
*)dst
;
626 struct compute_memory_item
*item
= rdst
->chunk
;
628 if (is_item_in_pool(item
)) {
629 dstx
+= 4 * item
->start_in_dw
;
630 dst
= (struct pipe_resource
*)pool
->bo
;
632 if (item
->real_buffer
== NULL
) {
634 r600_compute_buffer_alloc_vram(pool
->screen
,
635 item
->size_in_dw
* 4);
637 dst
= (struct pipe_resource
*)item
->real_buffer
;
641 r600_copy_buffer(ctx
, dst
, dstx
, src
, &new_src_box
);
644 static void r600_clear_buffer(struct pipe_context
*ctx
, struct pipe_resource
*dst
,
645 uint64_t offset
, uint64_t size
, unsigned value
,
646 enum r600_coherency coher
)
648 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
650 if (rctx
->screen
->b
.has_cp_dma
&&
651 rctx
->b
.chip_class
>= EVERGREEN
&&
652 offset
% 4 == 0 && size
% 4 == 0) {
653 evergreen_cp_dma_clear_buffer(rctx
, dst
, offset
, size
, value
, coher
);
654 } else if (rctx
->screen
->b
.has_streamout
&& offset
% 4 == 0 && size
% 4 == 0) {
655 union pipe_color_union clear_value
;
656 clear_value
.ui
[0] = value
;
658 r600_blitter_begin(ctx
, R600_DISABLE_RENDER_COND
);
659 util_blitter_clear_buffer(rctx
->blitter
, dst
, offset
, size
,
661 r600_blitter_end(ctx
);
663 uint32_t *map
= r600_buffer_map_sync_with_rings(&rctx
->b
, r600_resource(dst
),
664 PIPE_TRANSFER_WRITE
);
667 for (unsigned i
= 0; i
< size
; i
++)
672 void r600_resource_copy_region(struct pipe_context
*ctx
,
673 struct pipe_resource
*dst
,
675 unsigned dstx
, unsigned dsty
, unsigned dstz
,
676 struct pipe_resource
*src
,
678 const struct pipe_box
*src_box
)
680 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
681 struct pipe_surface
*dst_view
, dst_templ
;
682 struct pipe_sampler_view src_templ
, *src_view
;
683 unsigned dst_width
, dst_height
, src_width0
, src_height0
, src_widthFL
, src_heightFL
;
684 unsigned src_force_level
= 0;
685 struct pipe_box sbox
, dstbox
;
687 /* Handle buffers first. */
688 if (dst
->target
== PIPE_BUFFER
&& src
->target
== PIPE_BUFFER
) {
689 if ((src
->bind
& PIPE_BIND_GLOBAL
) ||
690 (dst
->bind
& PIPE_BIND_GLOBAL
)) {
691 r600_copy_global_buffer(ctx
, dst
, dstx
, src
, src_box
);
693 r600_copy_buffer(ctx
, dst
, dstx
, src
, src_box
);
698 assert(u_max_sample(dst
) == u_max_sample(src
));
700 /* The driver doesn't decompress resources automatically while
701 * u_blitter is rendering. */
702 if (!r600_decompress_subresource(ctx
, src
, src_level
,
703 src_box
->z
, src_box
->z
+ src_box
->depth
- 1)) {
707 dst_width
= u_minify(dst
->width0
, dst_level
);
708 dst_height
= u_minify(dst
->height0
, dst_level
);
709 src_width0
= src
->width0
;
710 src_height0
= src
->height0
;
711 src_widthFL
= u_minify(src
->width0
, src_level
);
712 src_heightFL
= u_minify(src
->height0
, src_level
);
714 util_blitter_default_dst_texture(&dst_templ
, dst
, dst_level
, dstz
);
715 util_blitter_default_src_texture(rctx
->blitter
, &src_templ
, src
, src_level
);
717 if (util_format_is_compressed(src
->format
) ||
718 util_format_is_compressed(dst
->format
)) {
719 unsigned blocksize
= util_format_get_blocksize(src
->format
);
722 src_templ
.format
= PIPE_FORMAT_R16G16B16A16_UINT
; /* 64-bit block */
724 src_templ
.format
= PIPE_FORMAT_R32G32B32A32_UINT
; /* 128-bit block */
725 dst_templ
.format
= src_templ
.format
;
727 dst_width
= util_format_get_nblocksx(dst
->format
, dst_width
);
728 dst_height
= util_format_get_nblocksy(dst
->format
, dst_height
);
729 src_width0
= util_format_get_nblocksx(src
->format
, src_width0
);
730 src_height0
= util_format_get_nblocksy(src
->format
, src_height0
);
731 src_widthFL
= util_format_get_nblocksx(src
->format
, src_widthFL
);
732 src_heightFL
= util_format_get_nblocksy(src
->format
, src_heightFL
);
734 dstx
= util_format_get_nblocksx(dst
->format
, dstx
);
735 dsty
= util_format_get_nblocksy(dst
->format
, dsty
);
737 sbox
.x
= util_format_get_nblocksx(src
->format
, src_box
->x
);
738 sbox
.y
= util_format_get_nblocksy(src
->format
, src_box
->y
);
740 sbox
.width
= util_format_get_nblocksx(src
->format
, src_box
->width
);
741 sbox
.height
= util_format_get_nblocksy(src
->format
, src_box
->height
);
742 sbox
.depth
= src_box
->depth
;
745 src_force_level
= src_level
;
746 } else if (!util_blitter_is_copy_supported(rctx
->blitter
, dst
, src
)) {
747 if (util_format_is_subsampled_422(src
->format
)) {
749 src_templ
.format
= PIPE_FORMAT_R8G8B8A8_UINT
;
750 dst_templ
.format
= PIPE_FORMAT_R8G8B8A8_UINT
;
752 dst_width
= util_format_get_nblocksx(dst
->format
, dst_width
);
753 src_width0
= util_format_get_nblocksx(src
->format
, src_width0
);
754 src_widthFL
= util_format_get_nblocksx(src
->format
, src_widthFL
);
756 dstx
= util_format_get_nblocksx(dst
->format
, dstx
);
759 sbox
.x
= util_format_get_nblocksx(src
->format
, src_box
->x
);
760 sbox
.width
= util_format_get_nblocksx(src
->format
, src_box
->width
);
763 unsigned blocksize
= util_format_get_blocksize(src
->format
);
767 dst_templ
.format
= PIPE_FORMAT_R8_UNORM
;
768 src_templ
.format
= PIPE_FORMAT_R8_UNORM
;
771 dst_templ
.format
= PIPE_FORMAT_R8G8_UNORM
;
772 src_templ
.format
= PIPE_FORMAT_R8G8_UNORM
;
775 dst_templ
.format
= PIPE_FORMAT_R8G8B8A8_UNORM
;
776 src_templ
.format
= PIPE_FORMAT_R8G8B8A8_UNORM
;
779 dst_templ
.format
= PIPE_FORMAT_R16G16B16A16_UINT
;
780 src_templ
.format
= PIPE_FORMAT_R16G16B16A16_UINT
;
783 dst_templ
.format
= PIPE_FORMAT_R32G32B32A32_UINT
;
784 src_templ
.format
= PIPE_FORMAT_R32G32B32A32_UINT
;
787 fprintf(stderr
, "Unhandled format %s with blocksize %u\n",
788 util_format_short_name(src
->format
), blocksize
);
794 dst_view
= r600_create_surface_custom(ctx
, dst
, &dst_templ
,
795 /* we don't care about these two for r600g */
796 dst
->width0
, dst
->height0
,
797 dst_width
, dst_height
);
799 if (rctx
->b
.chip_class
>= EVERGREEN
) {
800 src_view
= evergreen_create_sampler_view_custom(ctx
, src
, &src_templ
,
801 src_width0
, src_height0
,
804 src_view
= r600_create_sampler_view_custom(ctx
, src
, &src_templ
,
805 src_widthFL
, src_heightFL
);
808 u_box_3d(dstx
, dsty
, dstz
, abs(src_box
->width
), abs(src_box
->height
),
809 abs(src_box
->depth
), &dstbox
);
812 r600_blitter_begin(ctx
, R600_COPY_TEXTURE
);
813 util_blitter_blit_generic(rctx
->blitter
, dst_view
, &dstbox
,
814 src_view
, src_box
, src_width0
, src_height0
,
815 PIPE_MASK_RGBAZS
, PIPE_TEX_FILTER_NEAREST
, NULL
,
817 r600_blitter_end(ctx
);
819 pipe_surface_reference(&dst_view
, NULL
);
820 pipe_sampler_view_reference(&src_view
, NULL
);
823 static bool do_hardware_msaa_resolve(struct pipe_context
*ctx
,
824 const struct pipe_blit_info
*info
)
826 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
827 struct r600_texture
*dst
= (struct r600_texture
*)info
->dst
.resource
;
828 unsigned dst_width
= u_minify(info
->dst
.resource
->width0
, info
->dst
.level
);
829 unsigned dst_height
= u_minify(info
->dst
.resource
->height0
, info
->dst
.level
);
830 enum pipe_format format
= info
->src
.format
;
831 unsigned sample_mask
=
832 rctx
->b
.chip_class
== CAYMAN
? ~0 :
833 ((1ull << MAX2(1, info
->src
.resource
->nr_samples
)) - 1);
834 struct pipe_resource
*tmp
, templ
;
835 struct pipe_blit_info blit
;
837 /* Check basic requirements for hw resolve. */
838 if (!(info
->src
.resource
->nr_samples
> 1 &&
839 info
->dst
.resource
->nr_samples
<= 1 &&
840 !util_format_is_pure_integer(format
) &&
841 !util_format_is_depth_or_stencil(format
) &&
842 util_max_layer(info
->src
.resource
, 0) == 0))
845 /* Check the remaining requirements for hw resolve. */
846 if (util_max_layer(info
->dst
.resource
, info
->dst
.level
) == 0 &&
847 util_is_format_compatible(util_format_description(info
->src
.format
),
848 util_format_description(info
->dst
.format
)) &&
849 !info
->scissor_enable
&&
850 (info
->mask
& PIPE_MASK_RGBA
) == PIPE_MASK_RGBA
&&
851 dst_width
== info
->src
.resource
->width0
&&
852 dst_height
== info
->src
.resource
->height0
&&
853 info
->dst
.box
.x
== 0 &&
854 info
->dst
.box
.y
== 0 &&
855 info
->dst
.box
.width
== dst_width
&&
856 info
->dst
.box
.height
== dst_height
&&
857 info
->dst
.box
.depth
== 1 &&
858 info
->src
.box
.x
== 0 &&
859 info
->src
.box
.y
== 0 &&
860 info
->src
.box
.width
== dst_width
&&
861 info
->src
.box
.height
== dst_height
&&
862 info
->src
.box
.depth
== 1 &&
863 dst
->surface
.u
.legacy
.level
[info
->dst
.level
].mode
>= RADEON_SURF_MODE_1D
&&
864 (!dst
->cmask
.size
|| !dst
->dirty_level_mask
) /* dst cannot be fast-cleared */) {
865 r600_blitter_begin(ctx
, R600_COLOR_RESOLVE
|
866 (info
->render_condition_enable
? 0 : R600_DISABLE_RENDER_COND
));
867 util_blitter_custom_resolve_color(rctx
->blitter
,
868 info
->dst
.resource
, info
->dst
.level
,
870 info
->src
.resource
, info
->src
.box
.z
,
871 sample_mask
, rctx
->custom_blend_resolve
,
873 r600_blitter_end(ctx
);
877 /* Shader-based resolve is VERY SLOW. Instead, resolve into
878 * a temporary texture and blit.
880 memset(&templ
, 0, sizeof(templ
));
881 templ
.target
= PIPE_TEXTURE_2D
;
882 templ
.format
= info
->src
.resource
->format
;
883 templ
.width0
= info
->src
.resource
->width0
;
884 templ
.height0
= info
->src
.resource
->height0
;
886 templ
.array_size
= 1;
887 templ
.usage
= PIPE_USAGE_DEFAULT
;
888 templ
.flags
= R600_RESOURCE_FLAG_FORCE_TILING
;
890 tmp
= ctx
->screen
->resource_create(ctx
->screen
, &templ
);
895 r600_blitter_begin(ctx
, R600_COLOR_RESOLVE
|
896 (info
->render_condition_enable
? 0 : R600_DISABLE_RENDER_COND
));
897 util_blitter_custom_resolve_color(rctx
->blitter
, tmp
, 0, 0,
898 info
->src
.resource
, info
->src
.box
.z
,
899 sample_mask
, rctx
->custom_blend_resolve
,
901 r600_blitter_end(ctx
);
905 blit
.src
.resource
= tmp
;
908 r600_blitter_begin(ctx
, R600_BLIT
|
909 (info
->render_condition_enable
? 0 : R600_DISABLE_RENDER_COND
));
910 util_blitter_blit(rctx
->blitter
, &blit
);
911 r600_blitter_end(ctx
);
913 pipe_resource_reference(&tmp
, NULL
);
917 static void r600_blit(struct pipe_context
*ctx
,
918 const struct pipe_blit_info
*info
)
920 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
921 struct r600_texture
*rdst
= (struct r600_texture
*)info
->dst
.resource
;
923 if (do_hardware_msaa_resolve(ctx
, info
)) {
927 /* Using SDMA for copying to a linear texture in GTT is much faster.
928 * This improves DRI PRIME performance.
930 * resource_copy_region can't do this yet, because dma_copy calls it
931 * on failure (recursion).
933 if (rdst
->surface
.u
.legacy
.level
[info
->dst
.level
].mode
==
934 RADEON_SURF_MODE_LINEAR_ALIGNED
&&
936 util_can_blit_via_copy_region(info
, false)) {
937 rctx
->b
.dma_copy(ctx
, info
->dst
.resource
, info
->dst
.level
,
938 info
->dst
.box
.x
, info
->dst
.box
.y
,
940 info
->src
.resource
, info
->src
.level
,
945 assert(util_blitter_is_blit_supported(rctx
->blitter
, info
));
947 /* The driver doesn't decompress resources automatically while
948 * u_blitter is rendering. */
949 if (!r600_decompress_subresource(ctx
, info
->src
.resource
, info
->src
.level
,
951 info
->src
.box
.z
+ info
->src
.box
.depth
- 1)) {
955 if (rctx
->screen
->b
.debug_flags
& DBG_FORCE_DMA
&&
956 util_try_blit_via_copy_region(ctx
, info
))
959 r600_blitter_begin(ctx
, R600_BLIT
|
960 (info
->render_condition_enable
? 0 : R600_DISABLE_RENDER_COND
));
961 util_blitter_blit(rctx
->blitter
, info
);
962 r600_blitter_end(ctx
);
965 static void r600_flush_resource(struct pipe_context
*ctx
,
966 struct pipe_resource
*res
)
968 struct r600_texture
*rtex
= (struct r600_texture
*)res
;
970 assert(res
->target
!= PIPE_BUFFER
);
972 if (!rtex
->is_depth
&& rtex
->cmask
.size
) {
973 r600_blit_decompress_color(ctx
, rtex
, 0, res
->last_level
,
974 0, util_max_layer(res
, 0));
978 void r600_init_blit_functions(struct r600_context
*rctx
)
980 rctx
->b
.b
.clear
= r600_clear
;
981 rctx
->b
.b
.clear_render_target
= r600_clear_render_target
;
982 rctx
->b
.b
.clear_depth_stencil
= r600_clear_depth_stencil
;
983 rctx
->b
.b
.resource_copy_region
= r600_resource_copy_region
;
984 rctx
->b
.b
.blit
= r600_blit
;
985 rctx
->b
.b
.flush_resource
= r600_flush_resource
;
986 rctx
->b
.clear_buffer
= r600_clear_buffer
;
987 rctx
->b
.blit_decompress_depth
= r600_blit_decompress_depth
;