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/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 union pipe_color_union
*color
,
467 double depth
, unsigned stencil
)
469 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
470 struct pipe_framebuffer_state
*fb
= &rctx
->framebuffer
.state
;
472 if (buffers
& PIPE_CLEAR_COLOR
&& rctx
->b
.chip_class
>= EVERGREEN
) {
473 evergreen_do_fast_color_clear(&rctx
->b
, fb
, &rctx
->framebuffer
.atom
,
474 &buffers
, NULL
, color
);
476 return; /* all buffers have been fast cleared */
479 if (buffers
& PIPE_CLEAR_COLOR
) {
482 /* These buffers cannot use fast clear, make sure to disable expansion. */
483 for (i
= 0; i
< fb
->nr_cbufs
; i
++) {
484 struct r600_texture
*tex
;
486 /* If not clearing this buffer, skip. */
487 if (!(buffers
& (PIPE_CLEAR_COLOR0
<< i
)))
493 tex
= (struct r600_texture
*)fb
->cbufs
[i
]->texture
;
494 if (tex
->fmask
.size
== 0)
495 tex
->dirty_level_mask
&= ~(1 << fb
->cbufs
[i
]->u
.tex
.level
);
499 /* if hyperz enabled just clear hyperz */
500 if (fb
->zsbuf
&& (buffers
& PIPE_CLEAR_DEPTH
)) {
501 struct r600_texture
*rtex
;
502 unsigned level
= fb
->zsbuf
->u
.tex
.level
;
504 rtex
= (struct r600_texture
*)fb
->zsbuf
->texture
;
506 /* We can't use hyperz fast clear if each slice of a texture
507 * array are clear to different value. To simplify code just
508 * disable fast clear for texture array.
510 if (r600_htile_enabled(rtex
, level
) &&
511 fb
->zsbuf
->u
.tex
.first_layer
== 0 &&
512 fb
->zsbuf
->u
.tex
.last_layer
== util_max_layer(&rtex
->resource
.b
.b
, level
)) {
513 if (rtex
->depth_clear_value
!= depth
) {
514 rtex
->depth_clear_value
= depth
;
515 r600_mark_atom_dirty(rctx
, &rctx
->db_state
.atom
);
517 rctx
->db_misc_state
.htile_clear
= true;
518 r600_mark_atom_dirty(rctx
, &rctx
->db_misc_state
.atom
);
522 r600_blitter_begin(ctx
, R600_CLEAR
);
523 util_blitter_clear(rctx
->blitter
, fb
->width
, fb
->height
,
524 util_framebuffer_get_num_layers(fb
),
525 buffers
, color
, depth
, stencil
);
526 r600_blitter_end(ctx
);
528 /* disable fast clear */
529 if (rctx
->db_misc_state
.htile_clear
) {
530 rctx
->db_misc_state
.htile_clear
= false;
531 r600_mark_atom_dirty(rctx
, &rctx
->db_misc_state
.atom
);
535 static void r600_clear_render_target(struct pipe_context
*ctx
,
536 struct pipe_surface
*dst
,
537 const union pipe_color_union
*color
,
538 unsigned dstx
, unsigned dsty
,
539 unsigned width
, unsigned height
,
540 bool render_condition_enabled
)
542 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
544 r600_blitter_begin(ctx
, R600_CLEAR_SURFACE
|
545 (render_condition_enabled
? 0 : R600_DISABLE_RENDER_COND
));
546 util_blitter_clear_render_target(rctx
->blitter
, dst
, color
,
547 dstx
, dsty
, width
, height
);
548 r600_blitter_end(ctx
);
551 static void r600_clear_depth_stencil(struct pipe_context
*ctx
,
552 struct pipe_surface
*dst
,
553 unsigned clear_flags
,
556 unsigned dstx
, unsigned dsty
,
557 unsigned width
, unsigned height
,
558 bool render_condition_enabled
)
560 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
562 r600_blitter_begin(ctx
, R600_CLEAR_SURFACE
|
563 (render_condition_enabled
? 0 : R600_DISABLE_RENDER_COND
));
564 util_blitter_clear_depth_stencil(rctx
->blitter
, dst
, clear_flags
, depth
, stencil
,
565 dstx
, dsty
, width
, height
);
566 r600_blitter_end(ctx
);
569 static void r600_copy_buffer(struct pipe_context
*ctx
, struct pipe_resource
*dst
, unsigned dstx
,
570 struct pipe_resource
*src
, const struct pipe_box
*src_box
)
572 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
574 if (rctx
->screen
->b
.has_cp_dma
) {
575 r600_cp_dma_copy_buffer(rctx
, dst
, dstx
, src
, src_box
->x
, src_box
->width
);
577 else if (rctx
->screen
->b
.has_streamout
&&
578 /* Require 4-byte alignment. */
579 dstx
% 4 == 0 && src_box
->x
% 4 == 0 && src_box
->width
% 4 == 0) {
581 r600_blitter_begin(ctx
, R600_COPY_BUFFER
);
582 util_blitter_copy_buffer(rctx
->blitter
, dst
, dstx
, src
, src_box
->x
, src_box
->width
);
583 r600_blitter_end(ctx
);
585 util_resource_copy_region(ctx
, dst
, 0, dstx
, 0, 0, src
, 0, src_box
);
590 * Global buffers are not really resources, they are are actually offsets
591 * into a single global resource (r600_screen::global_pool). The means
592 * they don't have their own buf handle, so they cannot be passed
593 * to r600_copy_buffer() and must be handled separately.
595 static void r600_copy_global_buffer(struct pipe_context
*ctx
,
596 struct pipe_resource
*dst
, unsigned
597 dstx
, struct pipe_resource
*src
,
598 const struct pipe_box
*src_box
)
600 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
601 struct compute_memory_pool
*pool
= rctx
->screen
->global_pool
;
602 struct pipe_box new_src_box
= *src_box
;
604 if (src
->bind
& PIPE_BIND_GLOBAL
) {
605 struct r600_resource_global
*rsrc
=
606 (struct r600_resource_global
*)src
;
607 struct compute_memory_item
*item
= rsrc
->chunk
;
609 if (is_item_in_pool(item
)) {
610 new_src_box
.x
+= 4 * item
->start_in_dw
;
611 src
= (struct pipe_resource
*)pool
->bo
;
613 if (item
->real_buffer
== NULL
) {
615 r600_compute_buffer_alloc_vram(pool
->screen
,
616 item
->size_in_dw
* 4);
618 src
= (struct pipe_resource
*)item
->real_buffer
;
621 if (dst
->bind
& PIPE_BIND_GLOBAL
) {
622 struct r600_resource_global
*rdst
=
623 (struct r600_resource_global
*)dst
;
624 struct compute_memory_item
*item
= rdst
->chunk
;
626 if (is_item_in_pool(item
)) {
627 dstx
+= 4 * item
->start_in_dw
;
628 dst
= (struct pipe_resource
*)pool
->bo
;
630 if (item
->real_buffer
== NULL
) {
632 r600_compute_buffer_alloc_vram(pool
->screen
,
633 item
->size_in_dw
* 4);
635 dst
= (struct pipe_resource
*)item
->real_buffer
;
639 r600_copy_buffer(ctx
, dst
, dstx
, src
, &new_src_box
);
642 static void r600_clear_buffer(struct pipe_context
*ctx
, struct pipe_resource
*dst
,
643 uint64_t offset
, uint64_t size
, unsigned value
,
644 enum r600_coherency coher
)
646 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
648 if (rctx
->screen
->b
.has_cp_dma
&&
649 rctx
->b
.chip_class
>= EVERGREEN
&&
650 offset
% 4 == 0 && size
% 4 == 0) {
651 evergreen_cp_dma_clear_buffer(rctx
, dst
, offset
, size
, value
, coher
);
652 } else if (rctx
->screen
->b
.has_streamout
&& offset
% 4 == 0 && size
% 4 == 0) {
653 union pipe_color_union clear_value
;
654 clear_value
.ui
[0] = value
;
656 r600_blitter_begin(ctx
, R600_DISABLE_RENDER_COND
);
657 util_blitter_clear_buffer(rctx
->blitter
, dst
, offset
, size
,
659 r600_blitter_end(ctx
);
661 uint32_t *map
= r600_buffer_map_sync_with_rings(&rctx
->b
, r600_resource(dst
),
662 PIPE_TRANSFER_WRITE
);
665 for (unsigned i
= 0; i
< size
; i
++)
670 void r600_resource_copy_region(struct pipe_context
*ctx
,
671 struct pipe_resource
*dst
,
673 unsigned dstx
, unsigned dsty
, unsigned dstz
,
674 struct pipe_resource
*src
,
676 const struct pipe_box
*src_box
)
678 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
679 struct pipe_surface
*dst_view
, dst_templ
;
680 struct pipe_sampler_view src_templ
, *src_view
;
681 unsigned dst_width
, dst_height
, src_width0
, src_height0
, src_widthFL
, src_heightFL
;
682 unsigned src_force_level
= 0;
683 struct pipe_box sbox
, dstbox
;
685 /* Handle buffers first. */
686 if (dst
->target
== PIPE_BUFFER
&& src
->target
== PIPE_BUFFER
) {
687 if ((src
->bind
& PIPE_BIND_GLOBAL
) ||
688 (dst
->bind
& PIPE_BIND_GLOBAL
)) {
689 r600_copy_global_buffer(ctx
, dst
, dstx
, src
, src_box
);
691 r600_copy_buffer(ctx
, dst
, dstx
, src
, src_box
);
696 assert(u_max_sample(dst
) == u_max_sample(src
));
698 /* The driver doesn't decompress resources automatically while
699 * u_blitter is rendering. */
700 if (!r600_decompress_subresource(ctx
, src
, src_level
,
701 src_box
->z
, src_box
->z
+ src_box
->depth
- 1)) {
705 dst_width
= u_minify(dst
->width0
, dst_level
);
706 dst_height
= u_minify(dst
->height0
, dst_level
);
707 src_width0
= src
->width0
;
708 src_height0
= src
->height0
;
709 src_widthFL
= u_minify(src
->width0
, src_level
);
710 src_heightFL
= u_minify(src
->height0
, src_level
);
712 util_blitter_default_dst_texture(&dst_templ
, dst
, dst_level
, dstz
);
713 util_blitter_default_src_texture(rctx
->blitter
, &src_templ
, src
, src_level
);
715 if (util_format_is_compressed(src
->format
) ||
716 util_format_is_compressed(dst
->format
)) {
717 unsigned blocksize
= util_format_get_blocksize(src
->format
);
720 src_templ
.format
= PIPE_FORMAT_R16G16B16A16_UINT
; /* 64-bit block */
722 src_templ
.format
= PIPE_FORMAT_R32G32B32A32_UINT
; /* 128-bit block */
723 dst_templ
.format
= src_templ
.format
;
725 dst_width
= util_format_get_nblocksx(dst
->format
, dst_width
);
726 dst_height
= util_format_get_nblocksy(dst
->format
, dst_height
);
727 src_width0
= util_format_get_nblocksx(src
->format
, src_width0
);
728 src_height0
= util_format_get_nblocksy(src
->format
, src_height0
);
729 src_widthFL
= util_format_get_nblocksx(src
->format
, src_widthFL
);
730 src_heightFL
= util_format_get_nblocksy(src
->format
, src_heightFL
);
732 dstx
= util_format_get_nblocksx(dst
->format
, dstx
);
733 dsty
= util_format_get_nblocksy(dst
->format
, dsty
);
735 sbox
.x
= util_format_get_nblocksx(src
->format
, src_box
->x
);
736 sbox
.y
= util_format_get_nblocksy(src
->format
, src_box
->y
);
738 sbox
.width
= util_format_get_nblocksx(src
->format
, src_box
->width
);
739 sbox
.height
= util_format_get_nblocksy(src
->format
, src_box
->height
);
740 sbox
.depth
= src_box
->depth
;
743 src_force_level
= src_level
;
744 } else if (!util_blitter_is_copy_supported(rctx
->blitter
, dst
, src
)) {
745 if (util_format_is_subsampled_422(src
->format
)) {
747 src_templ
.format
= PIPE_FORMAT_R8G8B8A8_UINT
;
748 dst_templ
.format
= PIPE_FORMAT_R8G8B8A8_UINT
;
750 dst_width
= util_format_get_nblocksx(dst
->format
, dst_width
);
751 src_width0
= util_format_get_nblocksx(src
->format
, src_width0
);
752 src_widthFL
= util_format_get_nblocksx(src
->format
, src_widthFL
);
754 dstx
= util_format_get_nblocksx(dst
->format
, dstx
);
757 sbox
.x
= util_format_get_nblocksx(src
->format
, src_box
->x
);
758 sbox
.width
= util_format_get_nblocksx(src
->format
, src_box
->width
);
761 unsigned blocksize
= util_format_get_blocksize(src
->format
);
765 dst_templ
.format
= PIPE_FORMAT_R8_UNORM
;
766 src_templ
.format
= PIPE_FORMAT_R8_UNORM
;
769 dst_templ
.format
= PIPE_FORMAT_R8G8_UNORM
;
770 src_templ
.format
= PIPE_FORMAT_R8G8_UNORM
;
773 dst_templ
.format
= PIPE_FORMAT_R8G8B8A8_UNORM
;
774 src_templ
.format
= PIPE_FORMAT_R8G8B8A8_UNORM
;
777 dst_templ
.format
= PIPE_FORMAT_R16G16B16A16_UINT
;
778 src_templ
.format
= PIPE_FORMAT_R16G16B16A16_UINT
;
781 dst_templ
.format
= PIPE_FORMAT_R32G32B32A32_UINT
;
782 src_templ
.format
= PIPE_FORMAT_R32G32B32A32_UINT
;
785 fprintf(stderr
, "Unhandled format %s with blocksize %u\n",
786 util_format_short_name(src
->format
), blocksize
);
792 dst_view
= r600_create_surface_custom(ctx
, dst
, &dst_templ
,
793 /* we don't care about these two for r600g */
794 dst
->width0
, dst
->height0
,
795 dst_width
, dst_height
);
797 if (rctx
->b
.chip_class
>= EVERGREEN
) {
798 src_view
= evergreen_create_sampler_view_custom(ctx
, src
, &src_templ
,
799 src_width0
, src_height0
,
802 src_view
= r600_create_sampler_view_custom(ctx
, src
, &src_templ
,
803 src_widthFL
, src_heightFL
);
806 u_box_3d(dstx
, dsty
, dstz
, abs(src_box
->width
), abs(src_box
->height
),
807 abs(src_box
->depth
), &dstbox
);
810 r600_blitter_begin(ctx
, R600_COPY_TEXTURE
);
811 util_blitter_blit_generic(rctx
->blitter
, dst_view
, &dstbox
,
812 src_view
, src_box
, src_width0
, src_height0
,
813 PIPE_MASK_RGBAZS
, PIPE_TEX_FILTER_NEAREST
, NULL
,
815 r600_blitter_end(ctx
);
817 pipe_surface_reference(&dst_view
, NULL
);
818 pipe_sampler_view_reference(&src_view
, NULL
);
821 static bool do_hardware_msaa_resolve(struct pipe_context
*ctx
,
822 const struct pipe_blit_info
*info
)
824 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
825 struct r600_texture
*dst
= (struct r600_texture
*)info
->dst
.resource
;
826 unsigned dst_width
= u_minify(info
->dst
.resource
->width0
, info
->dst
.level
);
827 unsigned dst_height
= u_minify(info
->dst
.resource
->height0
, info
->dst
.level
);
828 enum pipe_format format
= info
->src
.format
;
829 unsigned sample_mask
=
830 rctx
->b
.chip_class
== CAYMAN
? ~0 :
831 ((1ull << MAX2(1, info
->src
.resource
->nr_samples
)) - 1);
832 struct pipe_resource
*tmp
, templ
;
833 struct pipe_blit_info blit
;
835 /* Check basic requirements for hw resolve. */
836 if (!(info
->src
.resource
->nr_samples
> 1 &&
837 info
->dst
.resource
->nr_samples
<= 1 &&
838 !util_format_is_pure_integer(format
) &&
839 !util_format_is_depth_or_stencil(format
) &&
840 util_max_layer(info
->src
.resource
, 0) == 0))
843 /* Check the remaining requirements for hw resolve. */
844 if (util_max_layer(info
->dst
.resource
, info
->dst
.level
) == 0 &&
845 util_is_format_compatible(util_format_description(info
->src
.format
),
846 util_format_description(info
->dst
.format
)) &&
847 !info
->scissor_enable
&&
848 (info
->mask
& PIPE_MASK_RGBA
) == PIPE_MASK_RGBA
&&
849 dst_width
== info
->src
.resource
->width0
&&
850 dst_height
== info
->src
.resource
->height0
&&
851 info
->dst
.box
.x
== 0 &&
852 info
->dst
.box
.y
== 0 &&
853 info
->dst
.box
.width
== dst_width
&&
854 info
->dst
.box
.height
== dst_height
&&
855 info
->dst
.box
.depth
== 1 &&
856 info
->src
.box
.x
== 0 &&
857 info
->src
.box
.y
== 0 &&
858 info
->src
.box
.width
== dst_width
&&
859 info
->src
.box
.height
== dst_height
&&
860 info
->src
.box
.depth
== 1 &&
861 dst
->surface
.u
.legacy
.level
[info
->dst
.level
].mode
>= RADEON_SURF_MODE_1D
&&
862 (!dst
->cmask
.size
|| !dst
->dirty_level_mask
) /* dst cannot be fast-cleared */) {
863 r600_blitter_begin(ctx
, R600_COLOR_RESOLVE
|
864 (info
->render_condition_enable
? 0 : R600_DISABLE_RENDER_COND
));
865 util_blitter_custom_resolve_color(rctx
->blitter
,
866 info
->dst
.resource
, info
->dst
.level
,
868 info
->src
.resource
, info
->src
.box
.z
,
869 sample_mask
, rctx
->custom_blend_resolve
,
871 r600_blitter_end(ctx
);
875 /* Shader-based resolve is VERY SLOW. Instead, resolve into
876 * a temporary texture and blit.
878 memset(&templ
, 0, sizeof(templ
));
879 templ
.target
= PIPE_TEXTURE_2D
;
880 templ
.format
= info
->src
.resource
->format
;
881 templ
.width0
= info
->src
.resource
->width0
;
882 templ
.height0
= info
->src
.resource
->height0
;
884 templ
.array_size
= 1;
885 templ
.usage
= PIPE_USAGE_DEFAULT
;
886 templ
.flags
= R600_RESOURCE_FLAG_FORCE_TILING
;
888 tmp
= ctx
->screen
->resource_create(ctx
->screen
, &templ
);
893 r600_blitter_begin(ctx
, R600_COLOR_RESOLVE
|
894 (info
->render_condition_enable
? 0 : R600_DISABLE_RENDER_COND
));
895 util_blitter_custom_resolve_color(rctx
->blitter
, tmp
, 0, 0,
896 info
->src
.resource
, info
->src
.box
.z
,
897 sample_mask
, rctx
->custom_blend_resolve
,
899 r600_blitter_end(ctx
);
903 blit
.src
.resource
= tmp
;
906 r600_blitter_begin(ctx
, R600_BLIT
|
907 (info
->render_condition_enable
? 0 : R600_DISABLE_RENDER_COND
));
908 util_blitter_blit(rctx
->blitter
, &blit
);
909 r600_blitter_end(ctx
);
911 pipe_resource_reference(&tmp
, NULL
);
915 static void r600_blit(struct pipe_context
*ctx
,
916 const struct pipe_blit_info
*info
)
918 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
919 struct r600_texture
*rdst
= (struct r600_texture
*)info
->dst
.resource
;
921 if (do_hardware_msaa_resolve(ctx
, info
)) {
925 /* Using SDMA for copying to a linear texture in GTT is much faster.
926 * This improves DRI PRIME performance.
928 * resource_copy_region can't do this yet, because dma_copy calls it
929 * on failure (recursion).
931 if (rdst
->surface
.u
.legacy
.level
[info
->dst
.level
].mode
==
932 RADEON_SURF_MODE_LINEAR_ALIGNED
&&
934 util_can_blit_via_copy_region(info
, false)) {
935 rctx
->b
.dma_copy(ctx
, info
->dst
.resource
, info
->dst
.level
,
936 info
->dst
.box
.x
, info
->dst
.box
.y
,
938 info
->src
.resource
, info
->src
.level
,
943 assert(util_blitter_is_blit_supported(rctx
->blitter
, info
));
945 /* The driver doesn't decompress resources automatically while
946 * u_blitter is rendering. */
947 if (!r600_decompress_subresource(ctx
, info
->src
.resource
, info
->src
.level
,
949 info
->src
.box
.z
+ info
->src
.box
.depth
- 1)) {
953 if (rctx
->screen
->b
.debug_flags
& DBG_FORCE_DMA
&&
954 util_try_blit_via_copy_region(ctx
, info
))
957 r600_blitter_begin(ctx
, R600_BLIT
|
958 (info
->render_condition_enable
? 0 : R600_DISABLE_RENDER_COND
));
959 util_blitter_blit(rctx
->blitter
, info
);
960 r600_blitter_end(ctx
);
963 static void r600_flush_resource(struct pipe_context
*ctx
,
964 struct pipe_resource
*res
)
966 struct r600_texture
*rtex
= (struct r600_texture
*)res
;
968 assert(res
->target
!= PIPE_BUFFER
);
970 if (!rtex
->is_depth
&& rtex
->cmask
.size
) {
971 r600_blit_decompress_color(ctx
, rtex
, 0, res
->last_level
,
972 0, util_max_layer(res
, 0));
976 void r600_init_blit_functions(struct r600_context
*rctx
)
978 rctx
->b
.b
.clear
= r600_clear
;
979 rctx
->b
.b
.clear_render_target
= r600_clear_render_target
;
980 rctx
->b
.b
.clear_depth_stencil
= r600_clear_depth_stencil
;
981 rctx
->b
.b
.resource_copy_region
= r600_resource_copy_region
;
982 rctx
->b
.b
.blit
= r600_blit
;
983 rctx
->b
.b
.flush_resource
= r600_flush_resource
;
984 rctx
->b
.clear_buffer
= r600_clear_buffer
;
985 rctx
->b
.blit_decompress_depth
= r600_blit_decompress_depth
;