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 "util/u_surface.h"
25 #include "util/u_blitter.h"
26 #include "util/u_format.h"
27 #include "evergreend.h"
29 enum r600_blitter_op
/* bitmask */
31 R600_SAVE_FRAGMENT_STATE
= 1,
32 R600_SAVE_TEXTURES
= 2,
33 R600_SAVE_FRAMEBUFFER
= 4,
34 R600_DISABLE_RENDER_COND
= 8,
36 R600_CLEAR
= R600_SAVE_FRAGMENT_STATE
,
38 R600_CLEAR_SURFACE
= R600_SAVE_FRAGMENT_STATE
| R600_SAVE_FRAMEBUFFER
,
40 R600_COPY_BUFFER
= R600_DISABLE_RENDER_COND
,
42 R600_COPY_TEXTURE
= R600_SAVE_FRAGMENT_STATE
| R600_SAVE_FRAMEBUFFER
| R600_SAVE_TEXTURES
|
43 R600_DISABLE_RENDER_COND
,
45 R600_BLIT
= R600_SAVE_FRAGMENT_STATE
| R600_SAVE_FRAMEBUFFER
| R600_SAVE_TEXTURES
|
46 R600_DISABLE_RENDER_COND
,
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
| R600_DISABLE_RENDER_COND
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 r600_suspend_nontimer_queries(&rctx
->b
);
59 util_blitter_save_vertex_buffer_slot(rctx
->blitter
, rctx
->vertex_buffer_state
.vb
);
60 util_blitter_save_vertex_elements(rctx
->blitter
, rctx
->vertex_fetch_shader
.cso
);
61 util_blitter_save_vertex_shader(rctx
->blitter
, rctx
->vs_shader
);
62 util_blitter_save_geometry_shader(rctx
->blitter
, rctx
->gs_shader
);
63 util_blitter_save_so_targets(rctx
->blitter
, rctx
->b
.streamout
.num_targets
,
64 (struct pipe_stream_output_target
**)rctx
->b
.streamout
.targets
);
65 util_blitter_save_rasterizer(rctx
->blitter
, rctx
->rasterizer_state
.cso
);
67 if (op
& R600_SAVE_FRAGMENT_STATE
) {
68 util_blitter_save_viewport(rctx
->blitter
, &rctx
->viewport
[0].state
);
69 util_blitter_save_scissor(rctx
->blitter
, &rctx
->scissor
[0].scissor
);
70 util_blitter_save_fragment_shader(rctx
->blitter
, rctx
->ps_shader
);
71 util_blitter_save_blend(rctx
->blitter
, rctx
->blend_state
.cso
);
72 util_blitter_save_depth_stencil_alpha(rctx
->blitter
, rctx
->dsa_state
.cso
);
73 util_blitter_save_stencil_ref(rctx
->blitter
, &rctx
->stencil_ref
.pipe_state
);
74 util_blitter_save_sample_mask(rctx
->blitter
, rctx
->sample_mask
.sample_mask
);
77 if (op
& R600_SAVE_FRAMEBUFFER
)
78 util_blitter_save_framebuffer(rctx
->blitter
, &rctx
->framebuffer
.state
);
80 if (op
& R600_SAVE_TEXTURES
) {
81 util_blitter_save_fragment_sampler_states(
82 rctx
->blitter
, util_last_bit(rctx
->samplers
[PIPE_SHADER_FRAGMENT
].states
.enabled_mask
),
83 (void**)rctx
->samplers
[PIPE_SHADER_FRAGMENT
].states
.states
);
85 util_blitter_save_fragment_sampler_views(
86 rctx
->blitter
, util_last_bit(rctx
->samplers
[PIPE_SHADER_FRAGMENT
].views
.enabled_mask
),
87 (struct pipe_sampler_view
**)rctx
->samplers
[PIPE_SHADER_FRAGMENT
].views
.views
);
90 if ((op
& R600_DISABLE_RENDER_COND
) && rctx
->b
.current_render_cond
) {
91 util_blitter_save_render_condition(rctx
->blitter
,
92 rctx
->b
.current_render_cond
,
93 rctx
->b
.current_render_cond_cond
,
94 rctx
->b
.current_render_cond_mode
);
98 static void r600_blitter_end(struct pipe_context
*ctx
)
100 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
101 r600_resume_nontimer_queries(&rctx
->b
);
104 static unsigned u_max_sample(struct pipe_resource
*r
)
106 return r
->nr_samples
? r
->nr_samples
- 1 : 0;
109 static void r600_blit_decompress_depth(struct pipe_context
*ctx
,
110 struct r600_texture
*texture
,
111 struct r600_texture
*staging
,
112 unsigned first_level
, unsigned last_level
,
113 unsigned first_layer
, unsigned last_layer
,
114 unsigned first_sample
, unsigned last_sample
)
116 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
117 unsigned layer
, level
, sample
, checked_last_layer
, max_layer
, max_sample
;
118 struct r600_texture
*flushed_depth_texture
= staging
?
119 staging
: texture
->flushed_depth_texture
;
120 const struct util_format_description
*desc
=
121 util_format_description(texture
->resource
.b
.b
.format
);
124 if (!staging
&& !texture
->dirty_level_mask
)
127 max_sample
= u_max_sample(&texture
->resource
.b
.b
);
129 /* XXX Decompressing MSAA depth textures is broken on R6xx.
130 * There is also a hardlock if CMASK and FMASK are not present.
131 * Just skip this until we find out how to fix it. */
132 if (rctx
->b
.chip_class
== R600
&& max_sample
> 0) {
133 texture
->dirty_level_mask
= 0;
137 if (rctx
->b
.family
== CHIP_RV610
|| rctx
->b
.family
== CHIP_RV630
||
138 rctx
->b
.family
== CHIP_RV620
|| rctx
->b
.family
== CHIP_RV635
)
143 /* Enable decompression in DB_RENDER_CONTROL */
144 rctx
->db_misc_state
.flush_depthstencil_through_cb
= true;
145 rctx
->db_misc_state
.copy_depth
= util_format_has_depth(desc
);
146 rctx
->db_misc_state
.copy_stencil
= util_format_has_stencil(desc
);
147 rctx
->db_misc_state
.copy_sample
= first_sample
;
148 rctx
->db_misc_state
.atom
.dirty
= true;
150 for (level
= first_level
; level
<= last_level
; level
++) {
151 if (!staging
&& !(texture
->dirty_level_mask
& (1 << level
)))
154 /* The smaller the mipmap level, the less layers there are
155 * as far as 3D textures are concerned. */
156 max_layer
= util_max_layer(&texture
->resource
.b
.b
, level
);
157 checked_last_layer
= last_layer
< max_layer
? last_layer
: max_layer
;
159 for (layer
= first_layer
; layer
<= checked_last_layer
; layer
++) {
160 for (sample
= first_sample
; sample
<= last_sample
; sample
++) {
161 struct pipe_surface
*zsurf
, *cbsurf
, surf_tmpl
;
163 if (sample
!= rctx
->db_misc_state
.copy_sample
) {
164 rctx
->db_misc_state
.copy_sample
= sample
;
165 rctx
->db_misc_state
.atom
.dirty
= true;
168 surf_tmpl
.format
= texture
->resource
.b
.b
.format
;
169 surf_tmpl
.u
.tex
.level
= level
;
170 surf_tmpl
.u
.tex
.first_layer
= layer
;
171 surf_tmpl
.u
.tex
.last_layer
= layer
;
173 zsurf
= ctx
->create_surface(ctx
, &texture
->resource
.b
.b
, &surf_tmpl
);
175 surf_tmpl
.format
= flushed_depth_texture
->resource
.b
.b
.format
;
176 cbsurf
= ctx
->create_surface(ctx
,
177 &flushed_depth_texture
->resource
.b
.b
, &surf_tmpl
);
179 r600_blitter_begin(ctx
, R600_DECOMPRESS
);
180 util_blitter_custom_depth_stencil(rctx
->blitter
, zsurf
, cbsurf
, 1 << sample
,
181 rctx
->custom_dsa_flush
, depth
);
182 r600_blitter_end(ctx
);
184 pipe_surface_reference(&zsurf
, NULL
);
185 pipe_surface_reference(&cbsurf
, NULL
);
189 /* The texture will always be dirty if some layers or samples aren't flushed.
190 * I don't think this case occurs often though. */
192 first_layer
== 0 && last_layer
== max_layer
&&
193 first_sample
== 0 && last_sample
== max_sample
) {
194 texture
->dirty_level_mask
&= ~(1 << level
);
198 /* reenable compression in DB_RENDER_CONTROL */
199 rctx
->db_misc_state
.flush_depthstencil_through_cb
= false;
200 rctx
->db_misc_state
.atom
.dirty
= true;
203 static void r600_blit_decompress_depth_in_place(struct r600_context
*rctx
,
204 struct r600_texture
*texture
,
205 unsigned first_level
, unsigned last_level
,
206 unsigned first_layer
, unsigned last_layer
)
208 struct pipe_surface
*zsurf
, surf_tmpl
= {{0}};
209 unsigned layer
, max_layer
, checked_last_layer
, level
;
211 /* Enable decompression in DB_RENDER_CONTROL */
212 rctx
->db_misc_state
.flush_depthstencil_in_place
= true;
213 rctx
->db_misc_state
.atom
.dirty
= true;
215 surf_tmpl
.format
= texture
->resource
.b
.b
.format
;
217 for (level
= first_level
; level
<= last_level
; level
++) {
218 if (!(texture
->dirty_level_mask
& (1 << level
)))
221 surf_tmpl
.u
.tex
.level
= level
;
223 /* The smaller the mipmap level, the less layers there are
224 * as far as 3D textures are concerned. */
225 max_layer
= util_max_layer(&texture
->resource
.b
.b
, level
);
226 checked_last_layer
= last_layer
< max_layer
? last_layer
: max_layer
;
228 for (layer
= first_layer
; layer
<= checked_last_layer
; layer
++) {
229 surf_tmpl
.u
.tex
.first_layer
= layer
;
230 surf_tmpl
.u
.tex
.last_layer
= layer
;
232 zsurf
= rctx
->b
.b
.create_surface(&rctx
->b
.b
, &texture
->resource
.b
.b
, &surf_tmpl
);
234 r600_blitter_begin(&rctx
->b
.b
, R600_DECOMPRESS
);
235 util_blitter_custom_depth_stencil(rctx
->blitter
, zsurf
, NULL
, ~0,
236 rctx
->custom_dsa_flush
, 1.0f
);
237 r600_blitter_end(&rctx
->b
.b
);
239 pipe_surface_reference(&zsurf
, NULL
);
242 /* The texture will always be dirty if some layers or samples aren't flushed.
243 * I don't think this case occurs often though. */
244 if (first_layer
== 0 && last_layer
== max_layer
) {
245 texture
->dirty_level_mask
&= ~(1 << level
);
249 /* Disable decompression in DB_RENDER_CONTROL */
250 rctx
->db_misc_state
.flush_depthstencil_in_place
= false;
251 rctx
->db_misc_state
.atom
.dirty
= true;
254 void r600_decompress_depth_textures(struct r600_context
*rctx
,
255 struct r600_samplerview_state
*textures
)
258 unsigned depth_texture_mask
= textures
->compressed_depthtex_mask
;
260 while (depth_texture_mask
) {
261 struct pipe_sampler_view
*view
;
262 struct r600_texture
*tex
;
264 i
= u_bit_scan(&depth_texture_mask
);
266 view
= &textures
->views
[i
]->base
;
269 tex
= (struct r600_texture
*)view
->texture
;
270 assert(tex
->is_depth
&& !tex
->is_flushing_texture
);
272 if (rctx
->b
.chip_class
>= EVERGREEN
||
273 r600_can_read_depth(tex
)) {
274 r600_blit_decompress_depth_in_place(rctx
, tex
,
275 view
->u
.tex
.first_level
, view
->u
.tex
.last_level
,
276 0, util_max_layer(&tex
->resource
.b
.b
, view
->u
.tex
.first_level
));
278 r600_blit_decompress_depth(&rctx
->b
.b
, tex
, NULL
,
279 view
->u
.tex
.first_level
, view
->u
.tex
.last_level
,
280 0, util_max_layer(&tex
->resource
.b
.b
, view
->u
.tex
.first_level
),
281 0, u_max_sample(&tex
->resource
.b
.b
));
286 static void r600_blit_decompress_color(struct pipe_context
*ctx
,
287 struct r600_texture
*rtex
,
288 unsigned first_level
, unsigned last_level
,
289 unsigned first_layer
, unsigned last_layer
)
291 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
292 unsigned layer
, level
, checked_last_layer
, max_layer
;
294 if (!rtex
->dirty_level_mask
)
297 for (level
= first_level
; level
<= last_level
; level
++) {
298 if (!(rtex
->dirty_level_mask
& (1 << level
)))
301 /* The smaller the mipmap level, the less layers there are
302 * as far as 3D textures are concerned. */
303 max_layer
= util_max_layer(&rtex
->resource
.b
.b
, level
);
304 checked_last_layer
= last_layer
< max_layer
? last_layer
: max_layer
;
306 for (layer
= first_layer
; layer
<= checked_last_layer
; layer
++) {
307 struct pipe_surface
*cbsurf
, surf_tmpl
;
309 surf_tmpl
.format
= rtex
->resource
.b
.b
.format
;
310 surf_tmpl
.u
.tex
.level
= level
;
311 surf_tmpl
.u
.tex
.first_layer
= layer
;
312 surf_tmpl
.u
.tex
.last_layer
= layer
;
313 cbsurf
= ctx
->create_surface(ctx
, &rtex
->resource
.b
.b
, &surf_tmpl
);
315 r600_blitter_begin(ctx
, R600_DECOMPRESS
);
316 util_blitter_custom_color(rctx
->blitter
, cbsurf
,
317 rtex
->fmask
.size
? rctx
->custom_blend_decompress
: rctx
->custom_blend_fastclear
);
318 r600_blitter_end(ctx
);
320 pipe_surface_reference(&cbsurf
, NULL
);
323 /* The texture will always be dirty if some layers aren't flushed.
324 * I don't think this case occurs often though. */
325 if (first_layer
== 0 && last_layer
== max_layer
) {
326 rtex
->dirty_level_mask
&= ~(1 << level
);
331 void r600_decompress_color_textures(struct r600_context
*rctx
,
332 struct r600_samplerview_state
*textures
)
335 unsigned mask
= textures
->compressed_colortex_mask
;
338 struct pipe_sampler_view
*view
;
339 struct r600_texture
*tex
;
341 i
= u_bit_scan(&mask
);
343 view
= &textures
->views
[i
]->base
;
346 tex
= (struct r600_texture
*)view
->texture
;
347 assert(tex
->cmask
.size
);
349 r600_blit_decompress_color(&rctx
->b
.b
, tex
,
350 view
->u
.tex
.first_level
, view
->u
.tex
.last_level
,
351 0, util_max_layer(&tex
->resource
.b
.b
, view
->u
.tex
.first_level
));
355 /* Helper for decompressing a portion of a color or depth resource before
356 * blitting if any decompression is needed.
357 * The driver doesn't decompress resources automatically while u_blitter is
359 static bool r600_decompress_subresource(struct pipe_context
*ctx
,
360 struct pipe_resource
*tex
,
362 unsigned first_layer
, unsigned last_layer
)
364 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
365 struct r600_texture
*rtex
= (struct r600_texture
*)tex
;
367 if (rtex
->is_depth
&& !rtex
->is_flushing_texture
) {
368 if (rctx
->b
.chip_class
>= EVERGREEN
||
369 r600_can_read_depth(rtex
)) {
370 r600_blit_decompress_depth_in_place(rctx
, rtex
,
372 first_layer
, last_layer
);
374 if (!r600_init_flushed_depth_texture(ctx
, tex
, NULL
))
375 return false; /* error */
377 r600_blit_decompress_depth(ctx
, rtex
, NULL
,
379 first_layer
, last_layer
,
380 0, u_max_sample(tex
));
382 } else if (rtex
->cmask
.size
) {
383 r600_blit_decompress_color(ctx
, rtex
, level
, level
,
384 first_layer
, last_layer
);
389 static void r600_clear_buffer(struct pipe_context
*ctx
, struct pipe_resource
*dst
,
390 unsigned offset
, unsigned size
, unsigned value
);
392 static void evergreen_set_clear_color(struct pipe_surface
*cbuf
,
393 const union pipe_color_union
*color
)
395 unsigned *clear_value
= ((struct r600_texture
*)cbuf
->texture
)->color_clear_value
;
398 memset(&uc
, 0, sizeof(uc
));
400 if (util_format_is_pure_uint(cbuf
->format
)) {
401 util_format_write_4ui(cbuf
->format
, color
->ui
, 0, &uc
, 0, 0, 0, 1, 1);
402 } else if (util_format_is_pure_sint(cbuf
->format
)) {
403 util_format_write_4i(cbuf
->format
, color
->i
, 0, &uc
, 0, 0, 0, 1, 1);
405 util_pack_color(color
->f
, cbuf
->format
, &uc
);
408 memcpy(clear_value
, &uc
, 2 * sizeof(uint32_t));
411 static void r600_try_fast_color_clear(struct r600_context
*rctx
, unsigned *buffers
,
412 const union pipe_color_union
*color
)
414 struct pipe_framebuffer_state
*fb
= &rctx
->framebuffer
.state
;
417 for (i
= 0; i
< fb
->nr_cbufs
; i
++) {
418 struct r600_texture
*tex
;
419 unsigned clear_bit
= PIPE_CLEAR_COLOR0
<< i
;
424 /* if this colorbuffer is not being cleared */
425 if (!(*buffers
& clear_bit
))
428 tex
= (struct r600_texture
*)fb
->cbufs
[i
]->texture
;
430 /* 128-bit formats are unusupported */
431 if (util_format_get_blocksizebits(fb
->cbufs
[i
]->format
) > 64) {
435 /* the clear is allowed if all layers are bound */
436 if (fb
->cbufs
[i
]->u
.tex
.first_layer
!= 0 ||
437 fb
->cbufs
[i
]->u
.tex
.last_layer
!= util_max_layer(&tex
->resource
.b
.b
, 0)) {
441 /* cannot clear mipmapped textures */
442 if (fb
->cbufs
[i
]->texture
->last_level
!= 0) {
446 /* only supported on tiled surfaces */
447 if (tex
->surface
.level
[0].mode
< RADEON_SURF_MODE_1D
) {
451 /* ensure CMASK is enabled */
452 r600_texture_alloc_cmask_separate(&rctx
->screen
->b
, tex
);
453 if (tex
->cmask
.size
== 0) {
457 /* Do the fast clear. */
458 evergreen_set_clear_color(fb
->cbufs
[i
], color
);
459 r600_clear_buffer(&rctx
->b
.b
, &tex
->cmask_buffer
->b
.b
,
460 tex
->cmask
.offset
, tex
->cmask
.size
, 0);
462 tex
->dirty_level_mask
|= 1 << fb
->cbufs
[i
]->u
.tex
.level
;
463 rctx
->framebuffer
.atom
.dirty
= true;
464 *buffers
&= ~clear_bit
;
468 static void r600_clear(struct pipe_context
*ctx
, unsigned buffers
,
469 const union pipe_color_union
*color
,
470 double depth
, unsigned stencil
)
472 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
473 struct pipe_framebuffer_state
*fb
= &rctx
->framebuffer
.state
;
475 if (buffers
& PIPE_CLEAR_COLOR
&& rctx
->b
.chip_class
>= EVERGREEN
) {
476 r600_try_fast_color_clear(rctx
, &buffers
, color
);
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 /* Only use htile for first level */
511 if (rtex
->htile_buffer
&& !level
&& rtex
->surface
.array_size
== 1) {
512 if (rtex
->depth_clear_value
!= depth
) {
513 rtex
->depth_clear_value
= depth
;
514 rctx
->db_state
.atom
.dirty
= true;
516 rctx
->db_misc_state
.htile_clear
= true;
517 rctx
->db_misc_state
.atom
.dirty
= true;
521 r600_blitter_begin(ctx
, R600_CLEAR
);
522 util_blitter_clear(rctx
->blitter
, fb
->width
, fb
->height
, 1,
523 buffers
, color
, depth
, stencil
);
524 r600_blitter_end(ctx
);
526 /* disable fast clear */
527 if (rctx
->db_misc_state
.htile_clear
) {
528 rctx
->db_misc_state
.htile_clear
= false;
529 rctx
->db_misc_state
.atom
.dirty
= true;
533 static void r600_clear_render_target(struct pipe_context
*ctx
,
534 struct pipe_surface
*dst
,
535 const union pipe_color_union
*color
,
536 unsigned dstx
, unsigned dsty
,
537 unsigned width
, unsigned height
)
539 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
541 r600_blitter_begin(ctx
, R600_CLEAR_SURFACE
);
542 util_blitter_clear_render_target(rctx
->blitter
, dst
, color
,
543 dstx
, dsty
, width
, height
);
544 r600_blitter_end(ctx
);
547 static void r600_clear_depth_stencil(struct pipe_context
*ctx
,
548 struct pipe_surface
*dst
,
549 unsigned clear_flags
,
552 unsigned dstx
, unsigned dsty
,
553 unsigned width
, unsigned height
)
555 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
557 r600_blitter_begin(ctx
, R600_CLEAR_SURFACE
);
558 util_blitter_clear_depth_stencil(rctx
->blitter
, dst
, clear_flags
, depth
, stencil
,
559 dstx
, dsty
, width
, height
);
560 r600_blitter_end(ctx
);
563 static void r600_copy_buffer(struct pipe_context
*ctx
, struct pipe_resource
*dst
, unsigned dstx
,
564 struct pipe_resource
*src
, const struct pipe_box
*src_box
)
566 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
568 if (rctx
->screen
->b
.has_cp_dma
) {
569 r600_cp_dma_copy_buffer(rctx
, dst
, dstx
, src
, src_box
->x
, src_box
->width
);
571 else if (rctx
->screen
->b
.has_streamout
&&
572 /* Require 4-byte alignment. */
573 dstx
% 4 == 0 && src_box
->x
% 4 == 0 && src_box
->width
% 4 == 0) {
575 r600_blitter_begin(ctx
, R600_COPY_BUFFER
);
576 util_blitter_copy_buffer(rctx
->blitter
, dst
, dstx
, src
, src_box
->x
, src_box
->width
);
577 r600_blitter_end(ctx
);
579 util_resource_copy_region(ctx
, dst
, 0, dstx
, 0, 0, src
, 0, src_box
);
584 * Global buffers are not really resources, they are are actually offsets
585 * into a single global resource (r600_screen::global_pool). The means
586 * they don't have their own cs_buf handle, so they cannot be passed
587 * to r600_copy_buffer() and must be handled separately.
589 * XXX: It should be possible to implement this function using
590 * r600_copy_buffer() by passing the memory_pool resource as both src
591 * and dst and updating dstx and src_box to point to the correct offsets.
592 * This would likely perform better than the current implementation.
594 static void r600_copy_global_buffer(struct pipe_context
*ctx
,
595 struct pipe_resource
*dst
, unsigned
596 dstx
, struct pipe_resource
*src
,
597 const struct pipe_box
*src_box
)
599 struct pipe_box dst_box
; struct pipe_transfer
*src_pxfer
,
602 u_box_1d(dstx
, src_box
->width
, &dst_box
);
603 void *src_ptr
= ctx
->transfer_map(ctx
, src
, 0, PIPE_TRANSFER_READ
,
604 src_box
, &src_pxfer
);
605 void *dst_ptr
= ctx
->transfer_map(ctx
, dst
, 0, PIPE_TRANSFER_WRITE
,
606 &dst_box
, &dst_pxfer
);
607 memcpy(dst_ptr
, src_ptr
, src_box
->width
);
609 ctx
->transfer_unmap(ctx
, src_pxfer
);
610 ctx
->transfer_unmap(ctx
, dst_pxfer
);
613 static void r600_clear_buffer(struct pipe_context
*ctx
, struct pipe_resource
*dst
,
614 unsigned offset
, unsigned size
, unsigned value
)
616 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
618 if (rctx
->screen
->b
.has_cp_dma
&&
619 rctx
->b
.chip_class
>= EVERGREEN
&&
620 offset
% 4 == 0 && size
% 4 == 0) {
621 evergreen_cp_dma_clear_buffer(rctx
, dst
, offset
, size
, value
);
622 } else if (rctx
->screen
->b
.has_streamout
&& offset
% 4 == 0 && size
% 4 == 0) {
623 union pipe_color_union clear_value
;
624 clear_value
.ui
[0] = value
;
626 r600_blitter_begin(ctx
, R600_DISABLE_RENDER_COND
);
627 util_blitter_clear_buffer(rctx
->blitter
, dst
, offset
, size
,
629 r600_blitter_end(ctx
);
631 uint32_t *map
= r600_buffer_map_sync_with_rings(&rctx
->b
, r600_resource(dst
),
632 PIPE_TRANSFER_WRITE
);
634 for (unsigned i
= 0; i
< size
; i
++)
639 static bool util_format_is_subsampled_2x1_32bpp(enum pipe_format format
)
641 const struct util_format_description
*desc
= util_format_description(format
);
643 return desc
->layout
== UTIL_FORMAT_LAYOUT_SUBSAMPLED
&&
644 desc
->block
.width
== 2 &&
645 desc
->block
.height
== 1 &&
646 desc
->block
.bits
== 32;
649 static void r600_resource_copy_region(struct pipe_context
*ctx
,
650 struct pipe_resource
*dst
,
652 unsigned dstx
, unsigned dsty
, unsigned dstz
,
653 struct pipe_resource
*src
,
655 const struct pipe_box
*src_box
)
657 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
658 struct pipe_surface
*dst_view
, dst_templ
;
659 struct pipe_sampler_view src_templ
, *src_view
;
660 unsigned dst_width
, dst_height
, src_width0
, src_height0
, src_widthFL
, src_heightFL
;
661 unsigned src_force_level
= 0;
662 struct pipe_box sbox
, dstbox
;
664 /* Handle buffers first. */
665 if (dst
->target
== PIPE_BUFFER
&& src
->target
== PIPE_BUFFER
) {
666 if ((src
->bind
& PIPE_BIND_GLOBAL
) ||
667 (dst
->bind
& PIPE_BIND_GLOBAL
)) {
668 r600_copy_global_buffer(ctx
, dst
, dstx
, src
, src_box
);
670 r600_copy_buffer(ctx
, dst
, dstx
, src
, src_box
);
675 assert(u_max_sample(dst
) == u_max_sample(src
));
677 /* The driver doesn't decompress resources automatically while
678 * u_blitter is rendering. */
679 if (!r600_decompress_subresource(ctx
, src
, src_level
,
680 src_box
->z
, src_box
->z
+ src_box
->depth
- 1)) {
684 dst_width
= u_minify(dst
->width0
, dst_level
);
685 dst_height
= u_minify(dst
->height0
, dst_level
);
686 src_width0
= src
->width0
;
687 src_height0
= src
->height0
;
688 src_widthFL
= u_minify(src
->width0
, src_level
);
689 src_heightFL
= u_minify(src
->height0
, src_level
);
691 util_blitter_default_dst_texture(&dst_templ
, dst
, dst_level
, dstz
);
692 util_blitter_default_src_texture(&src_templ
, src
, src_level
);
694 if (util_format_is_compressed(src
->format
)) {
695 unsigned blocksize
= util_format_get_blocksize(src
->format
);
698 src_templ
.format
= PIPE_FORMAT_R16G16B16A16_UINT
; /* 64-bit block */
700 src_templ
.format
= PIPE_FORMAT_R32G32B32A32_UINT
; /* 128-bit block */
701 dst_templ
.format
= src_templ
.format
;
703 dst_width
= util_format_get_nblocksx(dst
->format
, dst_width
);
704 dst_height
= util_format_get_nblocksy(dst
->format
, dst_height
);
705 src_width0
= util_format_get_nblocksx(src
->format
, src_width0
);
706 src_height0
= util_format_get_nblocksy(src
->format
, src_height0
);
707 src_widthFL
= util_format_get_nblocksx(src
->format
, src_widthFL
);
708 src_heightFL
= util_format_get_nblocksy(src
->format
, src_heightFL
);
710 dstx
= util_format_get_nblocksx(dst
->format
, dstx
);
711 dsty
= util_format_get_nblocksy(dst
->format
, dsty
);
713 sbox
.x
= util_format_get_nblocksx(src
->format
, src_box
->x
);
714 sbox
.y
= util_format_get_nblocksy(src
->format
, src_box
->y
);
716 sbox
.width
= util_format_get_nblocksx(src
->format
, src_box
->width
);
717 sbox
.height
= util_format_get_nblocksy(src
->format
, src_box
->height
);
718 sbox
.depth
= src_box
->depth
;
721 src_force_level
= src_level
;
722 } else if (!util_blitter_is_copy_supported(rctx
->blitter
, dst
, src
)) {
723 if (util_format_is_subsampled_2x1_32bpp(src
->format
)) {
725 src_templ
.format
= PIPE_FORMAT_R8G8B8A8_UINT
;
726 dst_templ
.format
= PIPE_FORMAT_R8G8B8A8_UINT
;
728 dst_width
= util_format_get_nblocksx(dst
->format
, dst_width
);
729 src_width0
= util_format_get_nblocksx(src
->format
, src_width0
);
730 src_widthFL
= util_format_get_nblocksx(src
->format
, src_widthFL
);
732 dstx
= util_format_get_nblocksx(dst
->format
, dstx
);
735 sbox
.x
= util_format_get_nblocksx(src
->format
, src_box
->x
);
736 sbox
.width
= util_format_get_nblocksx(src
->format
, src_box
->width
);
739 unsigned blocksize
= util_format_get_blocksize(src
->format
);
743 dst_templ
.format
= PIPE_FORMAT_R8_UNORM
;
744 src_templ
.format
= PIPE_FORMAT_R8_UNORM
;
747 dst_templ
.format
= PIPE_FORMAT_R8G8_UNORM
;
748 src_templ
.format
= PIPE_FORMAT_R8G8_UNORM
;
751 dst_templ
.format
= PIPE_FORMAT_R8G8B8A8_UNORM
;
752 src_templ
.format
= PIPE_FORMAT_R8G8B8A8_UNORM
;
755 dst_templ
.format
= PIPE_FORMAT_R16G16B16A16_UINT
;
756 src_templ
.format
= PIPE_FORMAT_R16G16B16A16_UINT
;
759 dst_templ
.format
= PIPE_FORMAT_R32G32B32A32_UINT
;
760 src_templ
.format
= PIPE_FORMAT_R32G32B32A32_UINT
;
763 fprintf(stderr
, "Unhandled format %s with blocksize %u\n",
764 util_format_short_name(src
->format
), blocksize
);
770 dst_view
= r600_create_surface_custom(ctx
, dst
, &dst_templ
, dst_width
, dst_height
);
772 if (rctx
->b
.chip_class
>= EVERGREEN
) {
773 src_view
= evergreen_create_sampler_view_custom(ctx
, src
, &src_templ
,
774 src_width0
, src_height0
,
777 src_view
= r600_create_sampler_view_custom(ctx
, src
, &src_templ
,
778 src_widthFL
, src_heightFL
);
781 u_box_3d(dstx
, dsty
, dstz
, abs(src_box
->width
), abs(src_box
->height
),
782 abs(src_box
->depth
), &dstbox
);
785 r600_blitter_begin(ctx
, R600_COPY_TEXTURE
);
786 util_blitter_blit_generic(rctx
->blitter
, dst_view
, &dstbox
,
787 src_view
, src_box
, src_width0
, src_height0
,
788 PIPE_MASK_RGBAZS
, PIPE_TEX_FILTER_NEAREST
, NULL
);
789 r600_blitter_end(ctx
);
791 pipe_surface_reference(&dst_view
, NULL
);
792 pipe_sampler_view_reference(&src_view
, NULL
);
795 /* For MSAA integer resolving to work, we change the format to NORM using this function. */
796 static enum pipe_format
int_to_norm_format(enum pipe_format format
)
799 #define REPLACE_FORMAT_SIGN(format,sign) \
800 case PIPE_FORMAT_##format##_##sign##INT: \
801 return PIPE_FORMAT_##format##_##sign##NORM
802 #define REPLACE_FORMAT(format) \
803 REPLACE_FORMAT_SIGN(format, U); \
804 REPLACE_FORMAT_SIGN(format, S)
806 REPLACE_FORMAT_SIGN(B10G10R10A2
, U
);
808 REPLACE_FORMAT(R8G8
);
809 REPLACE_FORMAT(R8G8B8X8
);
810 REPLACE_FORMAT(R8G8B8A8
);
814 REPLACE_FORMAT(L8A8
);
816 REPLACE_FORMAT(R16G16
);
817 REPLACE_FORMAT(R16G16B16X16
);
818 REPLACE_FORMAT(R16G16B16A16
);
822 REPLACE_FORMAT(L16A16
);
824 #undef REPLACE_FORMAT
825 #undef REPLACE_FORMAT_SIGN
831 static bool do_hardware_msaa_resolve(struct pipe_context
*ctx
,
832 const struct pipe_blit_info
*info
)
834 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
835 struct r600_texture
*dst
= (struct r600_texture
*)info
->dst
.resource
;
836 unsigned dst_width
= u_minify(info
->dst
.resource
->width0
, info
->dst
.level
);
837 unsigned dst_height
= u_minify(info
->dst
.resource
->height0
, info
->dst
.level
);
838 enum pipe_format format
= int_to_norm_format(info
->dst
.format
);
839 unsigned sample_mask
=
840 rctx
->b
.chip_class
== CAYMAN
? ~0 :
841 ((1ull << MAX2(1, info
->src
.resource
->nr_samples
)) - 1);
843 if (info
->src
.resource
->nr_samples
> 1 &&
844 info
->dst
.resource
->nr_samples
<= 1 &&
845 util_max_layer(info
->src
.resource
, 0) == 0 &&
846 util_max_layer(info
->dst
.resource
, info
->dst
.level
) == 0 &&
847 info
->dst
.format
== info
->src
.format
&&
848 !util_format_is_pure_integer(format
) &&
849 !util_format_is_depth_or_stencil(format
) &&
850 !info
->scissor_enable
&&
851 (info
->mask
& PIPE_MASK_RGBA
) == PIPE_MASK_RGBA
&&
852 dst_width
== info
->src
.resource
->width0
&&
853 dst_height
== info
->src
.resource
->height0
&&
854 info
->dst
.box
.x
== 0 &&
855 info
->dst
.box
.y
== 0 &&
856 info
->dst
.box
.width
== dst_width
&&
857 info
->dst
.box
.height
== dst_height
&&
858 info
->dst
.box
.depth
== 1 &&
859 info
->src
.box
.x
== 0 &&
860 info
->src
.box
.y
== 0 &&
861 info
->src
.box
.width
== dst_width
&&
862 info
->src
.box
.height
== dst_height
&&
863 info
->src
.box
.depth
== 1 &&
864 dst
->surface
.level
[info
->dst
.level
].mode
>= RADEON_SURF_MODE_1D
) {
865 r600_blitter_begin(ctx
, R600_COLOR_RESOLVE
);
866 util_blitter_custom_resolve_color(rctx
->blitter
,
867 info
->dst
.resource
, info
->dst
.level
,
869 info
->src
.resource
, info
->src
.box
.z
,
870 sample_mask
, rctx
->custom_blend_resolve
,
872 r600_blitter_end(ctx
);
878 static void r600_blit(struct pipe_context
*ctx
,
879 const struct pipe_blit_info
*info
)
881 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
883 if (do_hardware_msaa_resolve(ctx
, info
)) {
887 assert(util_blitter_is_blit_supported(rctx
->blitter
, info
));
889 /* The driver doesn't decompress resources automatically while
890 * u_blitter is rendering. */
891 if (!r600_decompress_subresource(ctx
, info
->src
.resource
, info
->src
.level
,
893 info
->src
.box
.z
+ info
->src
.box
.depth
- 1)) {
897 r600_blitter_begin(ctx
, R600_BLIT
);
898 util_blitter_blit(rctx
->blitter
, info
);
899 r600_blitter_end(ctx
);
902 static void r600_flush_resource(struct pipe_context
*ctx
,
903 struct pipe_resource
*res
)
905 struct r600_texture
*rtex
= (struct r600_texture
*)res
;
907 assert(res
->target
!= PIPE_BUFFER
);
909 if (!rtex
->is_depth
&& rtex
->cmask
.size
) {
910 r600_blit_decompress_color(ctx
, rtex
, 0, res
->last_level
,
911 0, res
->array_size
- 1);
915 void r600_init_blit_functions(struct r600_context
*rctx
)
917 rctx
->b
.b
.clear
= r600_clear
;
918 rctx
->b
.b
.clear_render_target
= r600_clear_render_target
;
919 rctx
->b
.b
.clear_depth_stencil
= r600_clear_depth_stencil
;
920 rctx
->b
.b
.resource_copy_region
= r600_resource_copy_region
;
921 rctx
->b
.b
.blit
= r600_blit
;
922 rctx
->b
.b
.flush_resource
= r600_flush_resource
;
923 rctx
->b
.clear_buffer
= r600_clear_buffer
;
924 rctx
->b
.blit_decompress_depth
= r600_blit_decompress_depth
;