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"
28 enum r600_blitter_op
/* bitmask */
30 R600_SAVE_FRAGMENT_STATE
= 1,
31 R600_SAVE_TEXTURES
= 2,
32 R600_SAVE_FRAMEBUFFER
= 4,
33 R600_DISABLE_RENDER_COND
= 8,
35 R600_CLEAR
= R600_SAVE_FRAGMENT_STATE
,
37 R600_CLEAR_SURFACE
= R600_SAVE_FRAGMENT_STATE
| R600_SAVE_FRAMEBUFFER
,
39 R600_COPY_BUFFER
= R600_DISABLE_RENDER_COND
,
41 R600_COPY_TEXTURE
= R600_SAVE_FRAGMENT_STATE
| R600_SAVE_FRAMEBUFFER
| R600_SAVE_TEXTURES
|
42 R600_DISABLE_RENDER_COND
,
44 R600_BLIT
= R600_SAVE_FRAGMENT_STATE
| R600_SAVE_FRAMEBUFFER
| R600_SAVE_TEXTURES
|
45 R600_DISABLE_RENDER_COND
,
47 R600_DECOMPRESS
= R600_SAVE_FRAGMENT_STATE
| R600_SAVE_FRAMEBUFFER
| R600_DISABLE_RENDER_COND
,
49 R600_COLOR_RESOLVE
= R600_SAVE_FRAGMENT_STATE
| R600_SAVE_FRAMEBUFFER
| R600_DISABLE_RENDER_COND
52 static void r600_blitter_begin(struct pipe_context
*ctx
, enum r600_blitter_op op
)
54 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
56 r600_suspend_nontimer_queries(rctx
);
58 util_blitter_save_vertex_buffer_slot(rctx
->blitter
, rctx
->vertex_buffer_state
.vb
);
59 util_blitter_save_vertex_elements(rctx
->blitter
, rctx
->vertex_fetch_shader
.cso
);
60 util_blitter_save_vertex_shader(rctx
->blitter
, rctx
->vs_shader
);
61 util_blitter_save_so_targets(rctx
->blitter
, rctx
->streamout
.num_targets
,
62 (struct pipe_stream_output_target
**)rctx
->streamout
.targets
);
63 util_blitter_save_rasterizer(rctx
->blitter
, rctx
->rasterizer_state
.cso
);
65 if (op
& R600_SAVE_FRAGMENT_STATE
) {
66 util_blitter_save_viewport(rctx
->blitter
, &rctx
->viewport
.state
);
67 util_blitter_save_scissor(rctx
->blitter
, &rctx
->scissor
.scissor
);
68 util_blitter_save_fragment_shader(rctx
->blitter
, rctx
->ps_shader
);
69 util_blitter_save_blend(rctx
->blitter
, rctx
->blend_state
.cso
);
70 util_blitter_save_depth_stencil_alpha(rctx
->blitter
, rctx
->dsa_state
.cso
);
71 util_blitter_save_stencil_ref(rctx
->blitter
, &rctx
->stencil_ref
.pipe_state
);
72 util_blitter_save_sample_mask(rctx
->blitter
, rctx
->sample_mask
.sample_mask
);
75 if (op
& R600_SAVE_FRAMEBUFFER
)
76 util_blitter_save_framebuffer(rctx
->blitter
, &rctx
->framebuffer
.state
);
78 if (op
& R600_SAVE_TEXTURES
) {
79 util_blitter_save_fragment_sampler_states(
80 rctx
->blitter
, util_last_bit(rctx
->samplers
[PIPE_SHADER_FRAGMENT
].states
.enabled_mask
),
81 (void**)rctx
->samplers
[PIPE_SHADER_FRAGMENT
].states
.states
);
83 util_blitter_save_fragment_sampler_views(
84 rctx
->blitter
, util_last_bit(rctx
->samplers
[PIPE_SHADER_FRAGMENT
].views
.enabled_mask
),
85 (struct pipe_sampler_view
**)rctx
->samplers
[PIPE_SHADER_FRAGMENT
].views
.views
);
88 if ((op
& R600_DISABLE_RENDER_COND
) && rctx
->current_render_cond
) {
89 util_blitter_save_render_condition(rctx
->blitter
,
90 rctx
->current_render_cond
,
91 rctx
->current_render_cond_cond
,
92 rctx
->current_render_cond_mode
);
96 static void r600_blitter_end(struct pipe_context
*ctx
)
98 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
99 r600_resume_nontimer_queries(rctx
);
102 static unsigned u_max_sample(struct pipe_resource
*r
)
104 return r
->nr_samples
? r
->nr_samples
- 1 : 0;
107 void r600_blit_decompress_depth(struct pipe_context
*ctx
,
108 struct r600_texture
*texture
,
109 struct r600_texture
*staging
,
110 unsigned first_level
, unsigned last_level
,
111 unsigned first_layer
, unsigned last_layer
,
112 unsigned first_sample
, unsigned last_sample
)
114 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
115 unsigned layer
, level
, sample
, checked_last_layer
, max_layer
, max_sample
;
116 struct r600_texture
*flushed_depth_texture
= staging
?
117 staging
: texture
->flushed_depth_texture
;
118 const struct util_format_description
*desc
=
119 util_format_description(texture
->resource
.b
.b
.format
);
122 if (!staging
&& !texture
->dirty_level_mask
)
125 max_sample
= u_max_sample(&texture
->resource
.b
.b
);
127 /* XXX Decompressing MSAA depth textures is broken on R6xx.
128 * There is also a hardlock if CMASK and FMASK are not present.
129 * Just skip this until we find out how to fix it. */
130 if (rctx
->chip_class
== R600
&& max_sample
> 0) {
131 texture
->dirty_level_mask
= 0;
135 if (rctx
->family
== CHIP_RV610
|| rctx
->family
== CHIP_RV630
||
136 rctx
->family
== CHIP_RV620
|| rctx
->family
== CHIP_RV635
)
141 /* Enable decompression in DB_RENDER_CONTROL */
142 rctx
->db_misc_state
.flush_depthstencil_through_cb
= true;
143 rctx
->db_misc_state
.copy_depth
= util_format_has_depth(desc
);
144 rctx
->db_misc_state
.copy_stencil
= util_format_has_stencil(desc
);
145 rctx
->db_misc_state
.copy_sample
= first_sample
;
146 rctx
->db_misc_state
.atom
.dirty
= true;
148 for (level
= first_level
; level
<= last_level
; level
++) {
149 if (!staging
&& !(texture
->dirty_level_mask
& (1 << level
)))
152 /* The smaller the mipmap level, the less layers there are
153 * as far as 3D textures are concerned. */
154 max_layer
= util_max_layer(&texture
->resource
.b
.b
, level
);
155 checked_last_layer
= last_layer
< max_layer
? last_layer
: max_layer
;
157 for (layer
= first_layer
; layer
<= checked_last_layer
; layer
++) {
158 for (sample
= first_sample
; sample
<= last_sample
; sample
++) {
159 struct pipe_surface
*zsurf
, *cbsurf
, surf_tmpl
;
161 if (sample
!= rctx
->db_misc_state
.copy_sample
) {
162 rctx
->db_misc_state
.copy_sample
= sample
;
163 rctx
->db_misc_state
.atom
.dirty
= true;
166 surf_tmpl
.format
= texture
->resource
.b
.b
.format
;
167 surf_tmpl
.u
.tex
.level
= level
;
168 surf_tmpl
.u
.tex
.first_layer
= layer
;
169 surf_tmpl
.u
.tex
.last_layer
= layer
;
171 zsurf
= ctx
->create_surface(ctx
, &texture
->resource
.b
.b
, &surf_tmpl
);
173 surf_tmpl
.format
= flushed_depth_texture
->resource
.b
.b
.format
;
174 surf_tmpl
.u
.tex
.level
= level
;
175 surf_tmpl
.u
.tex
.first_layer
= layer
;
176 surf_tmpl
.u
.tex
.last_layer
= layer
;
177 cbsurf
= ctx
->create_surface(ctx
,
178 &flushed_depth_texture
->resource
.b
.b
, &surf_tmpl
);
180 r600_blitter_begin(ctx
, R600_DECOMPRESS
);
181 util_blitter_custom_depth_stencil(rctx
->blitter
, zsurf
, cbsurf
, 1 << sample
,
182 rctx
->custom_dsa_flush
, depth
);
183 r600_blitter_end(ctx
);
185 pipe_surface_reference(&zsurf
, NULL
);
186 pipe_surface_reference(&cbsurf
, NULL
);
190 /* The texture will always be dirty if some layers or samples aren't flushed.
191 * I don't think this case occurs often though. */
193 first_layer
== 0 && last_layer
== max_layer
&&
194 first_sample
== 0 && last_sample
== max_sample
) {
195 texture
->dirty_level_mask
&= ~(1 << level
);
199 /* reenable compression in DB_RENDER_CONTROL */
200 rctx
->db_misc_state
.flush_depthstencil_through_cb
= false;
201 rctx
->db_misc_state
.atom
.dirty
= true;
204 static void r600_blit_decompress_depth_in_place(struct r600_context
*rctx
,
205 struct r600_texture
*texture
,
206 unsigned first_level
, unsigned last_level
,
207 unsigned first_layer
, unsigned last_layer
)
209 struct pipe_surface
*zsurf
, surf_tmpl
= {{0}};
210 unsigned layer
, max_layer
, checked_last_layer
, level
;
212 /* Enable decompression in DB_RENDER_CONTROL */
213 rctx
->db_misc_state
.flush_depthstencil_in_place
= true;
214 rctx
->db_misc_state
.atom
.dirty
= true;
216 surf_tmpl
.format
= texture
->resource
.b
.b
.format
;
218 for (level
= first_level
; level
<= last_level
; level
++) {
219 if (!(texture
->dirty_level_mask
& (1 << level
)))
222 surf_tmpl
.u
.tex
.level
= level
;
224 /* The smaller the mipmap level, the less layers there are
225 * as far as 3D textures are concerned. */
226 max_layer
= util_max_layer(&texture
->resource
.b
.b
, level
);
227 checked_last_layer
= last_layer
< max_layer
? last_layer
: max_layer
;
229 for (layer
= first_layer
; layer
<= checked_last_layer
; layer
++) {
230 surf_tmpl
.u
.tex
.first_layer
= layer
;
231 surf_tmpl
.u
.tex
.last_layer
= layer
;
233 zsurf
= rctx
->context
.create_surface(&rctx
->context
, &texture
->resource
.b
.b
, &surf_tmpl
);
235 r600_blitter_begin(&rctx
->context
, R600_DECOMPRESS
);
236 util_blitter_custom_depth_stencil(rctx
->blitter
, zsurf
, NULL
, ~0,
237 rctx
->custom_dsa_flush
, 1.0f
);
238 r600_blitter_end(&rctx
->context
);
240 pipe_surface_reference(&zsurf
, NULL
);
243 /* The texture will always be dirty if some layers or samples aren't flushed.
244 * I don't think this case occurs often though. */
245 if (first_layer
== 0 && last_layer
== max_layer
) {
246 texture
->dirty_level_mask
&= ~(1 << level
);
250 /* Disable decompression in DB_RENDER_CONTROL */
251 rctx
->db_misc_state
.flush_depthstencil_in_place
= false;
252 rctx
->db_misc_state
.atom
.dirty
= true;
255 void r600_decompress_depth_textures(struct r600_context
*rctx
,
256 struct r600_samplerview_state
*textures
)
259 unsigned depth_texture_mask
= textures
->compressed_depthtex_mask
;
261 while (depth_texture_mask
) {
262 struct pipe_sampler_view
*view
;
263 struct r600_texture
*tex
;
265 i
= u_bit_scan(&depth_texture_mask
);
267 view
= &textures
->views
[i
]->base
;
270 tex
= (struct r600_texture
*)view
->texture
;
271 assert(tex
->is_depth
&& !tex
->is_flushing_texture
);
273 if (rctx
->chip_class
>= EVERGREEN
||
274 r600_can_read_depth(tex
)) {
275 r600_blit_decompress_depth_in_place(rctx
, tex
,
276 view
->u
.tex
.first_level
, view
->u
.tex
.last_level
,
277 0, util_max_layer(&tex
->resource
.b
.b
, view
->u
.tex
.first_level
));
279 r600_blit_decompress_depth(&rctx
->context
, tex
, NULL
,
280 view
->u
.tex
.first_level
, view
->u
.tex
.last_level
,
281 0, util_max_layer(&tex
->resource
.b
.b
, view
->u
.tex
.first_level
),
282 0, u_max_sample(&tex
->resource
.b
.b
));
287 static void r600_blit_decompress_color(struct pipe_context
*ctx
,
288 struct r600_texture
*rtex
,
289 unsigned first_level
, unsigned last_level
,
290 unsigned first_layer
, unsigned last_layer
)
292 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
293 unsigned layer
, level
, checked_last_layer
, max_layer
;
295 if (!rtex
->dirty_level_mask
)
298 for (level
= first_level
; level
<= last_level
; level
++) {
299 if (!(rtex
->dirty_level_mask
& (1 << level
)))
302 /* The smaller the mipmap level, the less layers there are
303 * as far as 3D textures are concerned. */
304 max_layer
= util_max_layer(&rtex
->resource
.b
.b
, level
);
305 checked_last_layer
= last_layer
< max_layer
? last_layer
: max_layer
;
307 for (layer
= first_layer
; layer
<= checked_last_layer
; layer
++) {
308 struct pipe_surface
*cbsurf
, surf_tmpl
;
310 surf_tmpl
.format
= rtex
->resource
.b
.b
.format
;
311 surf_tmpl
.u
.tex
.level
= level
;
312 surf_tmpl
.u
.tex
.first_layer
= layer
;
313 surf_tmpl
.u
.tex
.last_layer
= layer
;
314 cbsurf
= ctx
->create_surface(ctx
, &rtex
->resource
.b
.b
, &surf_tmpl
);
316 r600_blitter_begin(ctx
, R600_DECOMPRESS
);
317 util_blitter_custom_color(rctx
->blitter
, cbsurf
, rctx
->custom_blend_decompress
);
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
&& tex
->fmask_size
);
349 r600_blit_decompress_color(&rctx
->context
, 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
->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
->fmask_size
&& rtex
->cmask_size
) {
383 r600_blit_decompress_color(ctx
, rtex
, level
, level
,
384 first_layer
, last_layer
);
389 static boolean
is_simple_msaa_resolve(const struct pipe_blit_info
*info
)
391 unsigned dst_width
= u_minify(info
->dst
.resource
->width0
, info
->dst
.level
);
392 unsigned dst_height
= u_minify(info
->dst
.resource
->height0
, info
->dst
.level
);
393 struct r600_texture
*dst
= (struct r600_texture
*)info
->dst
.resource
;
394 unsigned dst_tile_mode
= dst
->surface
.level
[info
->dst
.level
].mode
;
396 return info
->dst
.resource
->format
== info
->src
.resource
->format
&&
397 info
->dst
.resource
->format
== info
->dst
.format
&&
398 info
->src
.resource
->format
== info
->src
.format
&&
399 !info
->scissor_enable
&&
400 info
->mask
== PIPE_MASK_RGBA
&&
401 dst_width
== info
->src
.resource
->width0
&&
402 dst_height
== info
->src
.resource
->height0
&&
403 info
->dst
.box
.x
== 0 &&
404 info
->dst
.box
.y
== 0 &&
405 info
->dst
.box
.width
== dst_width
&&
406 info
->dst
.box
.height
== dst_height
&&
407 info
->src
.box
.x
== 0 &&
408 info
->src
.box
.y
== 0 &&
409 info
->src
.box
.width
== dst_width
&&
410 info
->src
.box
.height
== dst_height
&&
411 /* Dst must be tiled. If it's not, we have to use a temporary
412 * resource which is tiled. */
413 dst_tile_mode
>= RADEON_SURF_MODE_1D
;
416 static void r600_clear_buffer(struct pipe_context
*ctx
, struct pipe_resource
*dst
,
417 unsigned offset
, unsigned size
, unsigned char value
);
419 static void evergreen_set_clear_color(struct pipe_surface
*cbuf
,
420 const union pipe_color_union
*color
)
422 unsigned *clear_value
= ((struct r600_texture
*)cbuf
->texture
)->color_clear_value
;
425 memset(&uc
, 0, sizeof(uc
));
427 if (util_format_is_pure_uint(cbuf
->format
)) {
428 util_format_write_4ui(cbuf
->format
, color
->ui
, 0, &uc
, 0, 0, 0, 1, 1);
429 } else if (util_format_is_pure_sint(cbuf
->format
)) {
430 util_format_write_4i(cbuf
->format
, color
->i
, 0, &uc
, 0, 0, 0, 1, 1);
432 util_pack_color(color
->f
, cbuf
->format
, &uc
);
435 memcpy(clear_value
, &uc
, 2 * sizeof(uint32_t));
438 static bool can_fast_clear_color(struct pipe_context
*ctx
)
440 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
441 struct pipe_framebuffer_state
*fb
= &rctx
->framebuffer
.state
;
444 if (rctx
->chip_class
< EVERGREEN
) {
448 for (i
= 0; i
< fb
->nr_cbufs
; i
++) {
449 struct r600_texture
*tex
= (struct r600_texture
*)fb
->cbufs
[i
]->texture
;
451 if (tex
->cmask_size
== 0) {
455 /* 128-bit formats are unuspported */
456 if (util_format_get_blocksizebits(fb
->cbufs
[i
]->format
) > 64) {
460 /* the clear is allowed if all layers are bound */
461 if (fb
->cbufs
[i
]->u
.tex
.first_layer
!= 0 ||
462 fb
->cbufs
[i
]->u
.tex
.last_layer
!= util_max_layer(&tex
->resource
.b
.b
, 0)) {
470 static void r600_clear(struct pipe_context
*ctx
, unsigned buffers
,
471 const union pipe_color_union
*color
,
472 double depth
, unsigned stencil
)
474 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
475 struct pipe_framebuffer_state
*fb
= &rctx
->framebuffer
.state
;
477 /* fast color clear on AA framebuffers (EG+) */
478 if ((buffers
& PIPE_CLEAR_COLOR
) && can_fast_clear_color(ctx
)) {
481 for (i
= 0; i
< fb
->nr_cbufs
; i
++) {
482 struct r600_texture
*tex
= (struct r600_texture
*)fb
->cbufs
[i
]->texture
;
484 evergreen_set_clear_color(fb
->cbufs
[i
], color
);
485 r600_clear_buffer(ctx
, fb
->cbufs
[i
]->texture
,
486 tex
->cmask_offset
, tex
->cmask_size
, 0);
487 tex
->dirty_level_mask
|= 1 << fb
->cbufs
[i
]->u
.tex
.level
;
490 rctx
->framebuffer
.atom
.dirty
= true;
492 buffers
&= ~PIPE_CLEAR_COLOR
;
497 /* if hyperz enabled just clear hyperz */
498 if (fb
->zsbuf
&& (buffers
& PIPE_CLEAR_DEPTH
)) {
499 struct r600_texture
*rtex
;
500 unsigned level
= fb
->zsbuf
->u
.tex
.level
;
502 rtex
= (struct r600_texture
*)fb
->zsbuf
->texture
;
504 /* We can't use hyperz fast clear if each slice of a texture
505 * array are clear to different value. To simplify code just
506 * disable fast clear for texture array.
508 /* Only use htile for first level */
509 if (rtex
->htile
&& !level
&& rtex
->surface
.array_size
== 1) {
510 if (rtex
->depth_clear
!= depth
) {
511 rtex
->depth_clear
= depth
;
512 rctx
->db_state
.atom
.dirty
= true;
514 rctx
->db_misc_state
.htile_clear
= true;
515 rctx
->db_misc_state
.atom
.dirty
= true;
519 r600_blitter_begin(ctx
, R600_CLEAR
);
520 util_blitter_clear(rctx
->blitter
, fb
->width
, fb
->height
,
521 buffers
, color
, depth
, stencil
);
522 r600_blitter_end(ctx
);
524 /* disable fast clear */
525 if (rctx
->db_misc_state
.htile_clear
) {
526 rctx
->db_misc_state
.htile_clear
= false;
527 rctx
->db_misc_state
.atom
.dirty
= true;
531 static void r600_clear_render_target(struct pipe_context
*ctx
,
532 struct pipe_surface
*dst
,
533 const union pipe_color_union
*color
,
534 unsigned dstx
, unsigned dsty
,
535 unsigned width
, unsigned height
)
537 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
539 r600_blitter_begin(ctx
, R600_CLEAR_SURFACE
);
540 util_blitter_clear_render_target(rctx
->blitter
, dst
, color
,
541 dstx
, dsty
, width
, height
);
542 r600_blitter_end(ctx
);
545 static void r600_clear_depth_stencil(struct pipe_context
*ctx
,
546 struct pipe_surface
*dst
,
547 unsigned clear_flags
,
550 unsigned dstx
, unsigned dsty
,
551 unsigned width
, unsigned height
)
553 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
555 r600_blitter_begin(ctx
, R600_CLEAR_SURFACE
);
556 util_blitter_clear_depth_stencil(rctx
->blitter
, dst
, clear_flags
, depth
, stencil
,
557 dstx
, dsty
, width
, height
);
558 r600_blitter_end(ctx
);
561 void r600_copy_buffer(struct pipe_context
*ctx
, struct pipe_resource
*dst
, unsigned dstx
,
562 struct pipe_resource
*src
, const struct pipe_box
*src_box
)
564 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
566 if (rctx
->screen
->has_cp_dma
) {
567 r600_cp_dma_copy_buffer(rctx
, dst
, dstx
, src
, src_box
->x
, src_box
->width
);
569 else if (rctx
->screen
->has_streamout
&&
570 /* Require 4-byte alignment. */
571 dstx
% 4 == 0 && src_box
->x
% 4 == 0 && src_box
->width
% 4 == 0) {
572 r600_blitter_begin(ctx
, R600_COPY_BUFFER
);
573 util_blitter_copy_buffer(rctx
->blitter
, dst
, dstx
, src
, src_box
->x
, src_box
->width
);
574 r600_blitter_end(ctx
);
576 util_resource_copy_region(ctx
, dst
, 0, dstx
, 0, 0, src
, 0, src_box
);
580 static void r600_clear_buffer(struct pipe_context
*ctx
, struct pipe_resource
*dst
,
581 unsigned offset
, unsigned size
, unsigned char value
)
583 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
586 if (rctx
->screen
->has_cp_dma
&&
587 rctx
->chip_class
>= EVERGREEN
&&
588 offset
% 4 == 0 && size
% 4 == 0) {
589 uint32_t clear_value
= v
| (v
<< 8) | (v
<< 16) | (v
<< 24);
591 evergreen_cp_dma_clear_buffer(rctx
, dst
, offset
, size
, clear_value
);
592 } else if (rctx
->screen
->has_streamout
&& offset
% 4 == 0 && size
% 4 == 0) {
593 union pipe_color_union clear_value
;
595 clear_value
.ui
[0] = v
| (v
<< 8) | (v
<< 16) | (v
<< 24);
597 r600_blitter_begin(ctx
, R600_DISABLE_RENDER_COND
);
598 util_blitter_clear_buffer(rctx
->blitter
, dst
, offset
, size
,
600 r600_blitter_end(ctx
);
602 char *map
= r600_buffer_mmap_sync_with_rings(rctx
, r600_resource(dst
),
603 PIPE_TRANSFER_WRITE
);
604 memset(map
+ offset
, value
, size
);
608 void r600_screen_clear_buffer(struct r600_screen
*rscreen
, struct pipe_resource
*dst
,
609 unsigned offset
, unsigned size
, unsigned char value
)
611 pipe_mutex_lock(rscreen
->aux_context_lock
);
612 r600_clear_buffer(rscreen
->aux_context
, dst
, offset
, size
, value
);
613 rscreen
->aux_context
->flush(rscreen
->aux_context
, NULL
, 0);
614 pipe_mutex_unlock(rscreen
->aux_context_lock
);
617 static bool util_format_is_subsampled_2x1_32bpp(enum pipe_format format
)
619 const struct util_format_description
*desc
= util_format_description(format
);
621 return desc
->layout
== UTIL_FORMAT_LAYOUT_SUBSAMPLED
&&
622 desc
->block
.width
== 2 &&
623 desc
->block
.height
== 1 &&
624 desc
->block
.bits
== 32;
627 static void r600_resource_copy_region(struct pipe_context
*ctx
,
628 struct pipe_resource
*dst
,
630 unsigned dstx
, unsigned dsty
, unsigned dstz
,
631 struct pipe_resource
*src
,
633 const struct pipe_box
*src_box
)
635 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
636 struct pipe_surface
*dst_view
, dst_templ
;
637 struct pipe_sampler_view src_templ
, *src_view
;
638 unsigned dst_width
, dst_height
, src_width0
, src_height0
, src_widthFL
, src_heightFL
;
639 struct pipe_box sbox
, dstbox
;
641 /* Handle buffers first. */
642 if (dst
->target
== PIPE_BUFFER
&& src
->target
== PIPE_BUFFER
) {
643 r600_copy_buffer(ctx
, dst
, dstx
, src
, src_box
);
647 assert(u_max_sample(dst
) == u_max_sample(src
));
649 /* The driver doesn't decompress resources automatically while
650 * u_blitter is rendering. */
651 if (!r600_decompress_subresource(ctx
, src
, src_level
,
652 src_box
->z
, src_box
->z
+ src_box
->depth
- 1)) {
656 dst_width
= u_minify(dst
->width0
, dst_level
);
657 dst_height
= u_minify(dst
->height0
, dst_level
);
658 src_width0
= src
->width0
;
659 src_height0
= src
->height0
;
660 src_widthFL
= u_minify(src
->width0
, src_level
);
661 src_heightFL
= u_minify(src
->height0
, src_level
);
663 util_blitter_default_dst_texture(&dst_templ
, dst
, dst_level
, dstz
);
664 util_blitter_default_src_texture(&src_templ
, src
, src_level
);
666 if (util_format_is_compressed(src
->format
)) {
667 unsigned blocksize
= util_format_get_blocksize(src
->format
);
670 src_templ
.format
= PIPE_FORMAT_R16G16B16A16_UINT
; /* 64-bit block */
672 src_templ
.format
= PIPE_FORMAT_R32G32B32A32_UINT
; /* 128-bit block */
673 dst_templ
.format
= src_templ
.format
;
675 dst_width
= util_format_get_nblocksx(dst
->format
, dst_width
);
676 dst_height
= util_format_get_nblocksy(dst
->format
, dst_height
);
677 src_width0
= util_format_get_nblocksx(src
->format
, src_width0
);
678 src_height0
= util_format_get_nblocksy(src
->format
, src_height0
);
679 src_widthFL
= util_format_get_nblocksx(src
->format
, src_widthFL
);
680 src_heightFL
= util_format_get_nblocksy(src
->format
, src_heightFL
);
682 dstx
= util_format_get_nblocksx(dst
->format
, dstx
);
683 dsty
= util_format_get_nblocksy(dst
->format
, dsty
);
685 sbox
.x
= util_format_get_nblocksx(src
->format
, src_box
->x
);
686 sbox
.y
= util_format_get_nblocksy(src
->format
, src_box
->y
);
688 sbox
.width
= util_format_get_nblocksx(src
->format
, src_box
->width
);
689 sbox
.height
= util_format_get_nblocksy(src
->format
, src_box
->height
);
690 sbox
.depth
= src_box
->depth
;
692 } else if (!util_blitter_is_copy_supported(rctx
->blitter
, dst
, src
,
694 if (util_format_is_subsampled_2x1_32bpp(src
->format
)) {
696 src_templ
.format
= PIPE_FORMAT_R8G8B8A8_UINT
;
697 dst_templ
.format
= PIPE_FORMAT_R8G8B8A8_UINT
;
699 dst_width
= util_format_get_nblocksx(dst
->format
, dst_width
);
700 src_width0
= util_format_get_nblocksx(src
->format
, src_width0
);
701 src_widthFL
= util_format_get_nblocksx(src
->format
, src_widthFL
);
703 dstx
= util_format_get_nblocksx(dst
->format
, dstx
);
706 sbox
.x
= util_format_get_nblocksx(src
->format
, src_box
->x
);
707 sbox
.width
= util_format_get_nblocksx(src
->format
, src_box
->width
);
710 unsigned blocksize
= util_format_get_blocksize(src
->format
);
714 dst_templ
.format
= PIPE_FORMAT_R8_UNORM
;
715 src_templ
.format
= PIPE_FORMAT_R8_UNORM
;
718 dst_templ
.format
= PIPE_FORMAT_R8G8_UNORM
;
719 src_templ
.format
= PIPE_FORMAT_R8G8_UNORM
;
722 dst_templ
.format
= PIPE_FORMAT_R8G8B8A8_UNORM
;
723 src_templ
.format
= PIPE_FORMAT_R8G8B8A8_UNORM
;
726 dst_templ
.format
= PIPE_FORMAT_R16G16B16A16_UINT
;
727 src_templ
.format
= PIPE_FORMAT_R16G16B16A16_UINT
;
730 dst_templ
.format
= PIPE_FORMAT_R32G32B32A32_UINT
;
731 src_templ
.format
= PIPE_FORMAT_R32G32B32A32_UINT
;
734 fprintf(stderr
, "Unhandled format %s with blocksize %u\n",
735 util_format_short_name(src
->format
), blocksize
);
741 dst_view
= r600_create_surface_custom(ctx
, dst
, &dst_templ
, dst_width
, dst_height
);
743 if (rctx
->chip_class
>= EVERGREEN
) {
744 src_view
= evergreen_create_sampler_view_custom(ctx
, src
, &src_templ
,
745 src_width0
, src_height0
);
747 src_view
= r600_create_sampler_view_custom(ctx
, src
, &src_templ
,
748 src_widthFL
, src_heightFL
);
751 u_box_3d(dstx
, dsty
, dstz
, abs(src_box
->width
), abs(src_box
->height
),
752 abs(src_box
->depth
), &dstbox
);
755 r600_blitter_begin(ctx
, R600_COPY_TEXTURE
);
756 util_blitter_blit_generic(rctx
->blitter
, dst_view
, &dstbox
,
757 src_view
, src_box
, src_width0
, src_height0
,
758 PIPE_MASK_RGBAZS
, PIPE_TEX_FILTER_NEAREST
, NULL
,
760 r600_blitter_end(ctx
);
762 pipe_surface_reference(&dst_view
, NULL
);
763 pipe_sampler_view_reference(&src_view
, NULL
);
766 /* For MSAA integer resolving to work, we change the format to NORM using this function. */
767 static enum pipe_format
int_to_norm_format(enum pipe_format format
)
770 #define REPLACE_FORMAT_SIGN(format,sign) \
771 case PIPE_FORMAT_##format##_##sign##INT: \
772 return PIPE_FORMAT_##format##_##sign##NORM
773 #define REPLACE_FORMAT(format) \
774 REPLACE_FORMAT_SIGN(format, U); \
775 REPLACE_FORMAT_SIGN(format, S)
777 REPLACE_FORMAT_SIGN(B10G10R10A2
, U
);
779 REPLACE_FORMAT(R8G8
);
780 REPLACE_FORMAT(R8G8B8
);
781 REPLACE_FORMAT(R8G8B8A8
);
785 REPLACE_FORMAT(L8A8
);
787 REPLACE_FORMAT(R16G16
);
788 REPLACE_FORMAT(R16G16B16
);
789 REPLACE_FORMAT(R16G16B16A16
);
793 REPLACE_FORMAT(L16A16
);
795 #undef REPLACE_FORMAT
796 #undef REPLACE_FORMAT_SIGN
802 static void r600_msaa_color_resolve(struct pipe_context
*ctx
,
803 const struct pipe_blit_info
*info
)
805 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
806 struct pipe_screen
*screen
= ctx
->screen
;
807 struct pipe_resource
*tmp
, templ
;
808 struct pipe_blit_info blit
;
809 unsigned sample_mask
=
810 rctx
->chip_class
== CAYMAN
? ~0 :
811 ((1ull << MAX2(1, info
->src
.resource
->nr_samples
)) - 1);
813 assert(info
->src
.level
== 0);
814 assert(info
->src
.box
.depth
== 1);
815 assert(info
->dst
.box
.depth
== 1);
817 if (is_simple_msaa_resolve(info
)) {
818 r600_blitter_begin(ctx
, R600_COLOR_RESOLVE
);
819 util_blitter_custom_resolve_color(rctx
->blitter
,
820 info
->dst
.resource
, info
->dst
.level
,
822 info
->src
.resource
, info
->src
.box
.z
,
823 sample_mask
, rctx
->custom_blend_resolve
,
824 int_to_norm_format(info
->dst
.format
));
825 r600_blitter_end(ctx
);
829 /* resolve into a temporary texture, then blit */
830 templ
.target
= PIPE_TEXTURE_2D
;
831 templ
.format
= info
->src
.resource
->format
;
832 templ
.width0
= info
->src
.resource
->width0
;
833 templ
.height0
= info
->src
.resource
->height0
;
835 templ
.array_size
= 1;
836 templ
.last_level
= 0;
837 templ
.nr_samples
= 0;
838 templ
.usage
= PIPE_USAGE_STATIC
;
839 templ
.bind
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
840 templ
.flags
= R600_RESOURCE_FLAG_FORCE_TILING
; /* dst must not have a linear layout */
842 tmp
= screen
->resource_create(screen
, &templ
);
845 r600_blitter_begin(ctx
, R600_COLOR_RESOLVE
);
846 util_blitter_custom_resolve_color(rctx
->blitter
,
848 info
->src
.resource
, info
->src
.box
.z
,
849 sample_mask
, rctx
->custom_blend_resolve
,
850 int_to_norm_format(tmp
->format
));
851 r600_blitter_end(ctx
);
855 blit
.src
.resource
= tmp
;
858 r600_blitter_begin(ctx
, R600_BLIT
);
859 util_blitter_blit(rctx
->blitter
, &blit
);
860 r600_blitter_end(ctx
);
862 pipe_resource_reference(&tmp
, NULL
);
865 static void r600_blit(struct pipe_context
*ctx
,
866 const struct pipe_blit_info
*info
)
868 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
870 assert(util_blitter_is_blit_supported(rctx
->blitter
, info
));
872 if (info
->src
.resource
->nr_samples
> 1 &&
873 info
->dst
.resource
->nr_samples
<= 1 &&
874 !util_format_is_depth_or_stencil(info
->src
.resource
->format
) &&
875 !util_format_is_pure_integer(int_to_norm_format(info
->src
.resource
->format
))) {
876 r600_msaa_color_resolve(ctx
, info
);
880 /* The driver doesn't decompress resources automatically while
881 * u_blitter is rendering. */
882 if (!r600_decompress_subresource(ctx
, info
->src
.resource
, info
->src
.level
,
884 info
->src
.box
.z
+ info
->src
.box
.depth
- 1)) {
888 r600_blitter_begin(ctx
, R600_BLIT
);
889 util_blitter_blit(rctx
->blitter
, info
);
890 r600_blitter_end(ctx
);
893 void r600_init_blit_functions(struct r600_context
*rctx
)
895 rctx
->context
.clear
= r600_clear
;
896 rctx
->context
.clear_render_target
= r600_clear_render_target
;
897 rctx
->context
.clear_depth_stencil
= r600_clear_depth_stencil
;
898 rctx
->context
.resource_copy_region
= r600_resource_copy_region
;
899 rctx
->context
.blit
= r600_blit
;