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 "util/u_surface.h"
24 #include "util/u_blitter.h"
25 #include "util/u_format.h"
26 #include "radeonsi_pipe.h"
29 enum r600_blitter_op
/* bitmask */
31 R600_SAVE_TEXTURES
= 1,
32 R600_SAVE_FRAMEBUFFER
= 2,
33 R600_DISABLE_RENDER_COND
= 4,
37 R600_CLEAR_SURFACE
= R600_SAVE_FRAMEBUFFER
,
39 R600_COPY
= R600_SAVE_FRAMEBUFFER
| R600_SAVE_TEXTURES
|
40 R600_DISABLE_RENDER_COND
,
42 R600_BLIT
= R600_SAVE_FRAMEBUFFER
| R600_SAVE_TEXTURES
|
43 R600_DISABLE_RENDER_COND
,
45 R600_DECOMPRESS
= R600_SAVE_FRAMEBUFFER
| R600_DISABLE_RENDER_COND
,
48 static void r600_blitter_begin(struct pipe_context
*ctx
, enum r600_blitter_op op
)
50 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
52 r600_context_queries_suspend(rctx
);
54 util_blitter_save_blend(rctx
->blitter
, rctx
->queued
.named
.blend
);
55 util_blitter_save_depth_stencil_alpha(rctx
->blitter
, rctx
->queued
.named
.dsa
);
56 util_blitter_save_stencil_ref(rctx
->blitter
, &rctx
->stencil_ref
);
57 util_blitter_save_rasterizer(rctx
->blitter
, rctx
->queued
.named
.rasterizer
);
58 util_blitter_save_fragment_shader(rctx
->blitter
, rctx
->ps_shader
);
59 util_blitter_save_vertex_shader(rctx
->blitter
, rctx
->vs_shader
);
60 util_blitter_save_vertex_elements(rctx
->blitter
, rctx
->vertex_elements
);
61 if (rctx
->queued
.named
.viewport
) {
62 util_blitter_save_viewport(rctx
->blitter
, &rctx
->queued
.named
.viewport
->viewport
);
64 util_blitter_save_vertex_buffer_slot(rctx
->blitter
, rctx
->vertex_buffer
);
65 util_blitter_save_so_targets(rctx
->blitter
, rctx
->num_so_targets
,
66 (struct pipe_stream_output_target
**)rctx
->so_targets
);
68 if (op
& R600_SAVE_FRAMEBUFFER
)
69 util_blitter_save_framebuffer(rctx
->blitter
, &rctx
->framebuffer
);
71 if (op
& R600_SAVE_TEXTURES
) {
72 util_blitter_save_fragment_sampler_states(
73 rctx
->blitter
, rctx
->ps_samplers
.n_samplers
,
74 (void**)rctx
->ps_samplers
.samplers
);
76 util_blitter_save_fragment_sampler_views(
77 rctx
->blitter
, rctx
->ps_samplers
.n_views
,
78 (struct pipe_sampler_view
**)rctx
->ps_samplers
.views
);
81 if ((op
& R600_DISABLE_RENDER_COND
) && rctx
->current_render_cond
) {
82 rctx
->saved_render_cond
= rctx
->current_render_cond
;
83 rctx
->saved_render_cond_cond
= rctx
->current_render_cond_cond
;
84 rctx
->saved_render_cond_mode
= rctx
->current_render_cond_mode
;
85 rctx
->context
.render_condition(&rctx
->context
, NULL
, FALSE
, 0);
90 static void r600_blitter_end(struct pipe_context
*ctx
)
92 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
93 if (rctx
->saved_render_cond
) {
94 rctx
->context
.render_condition(&rctx
->context
,
95 rctx
->saved_render_cond
,
96 rctx
->saved_render_cond_cond
,
97 rctx
->saved_render_cond_mode
);
98 rctx
->saved_render_cond
= NULL
;
100 r600_context_queries_resume(rctx
);
103 void si_blit_uncompress_depth(struct pipe_context
*ctx
,
104 struct r600_resource_texture
*texture
,
105 struct r600_resource_texture
*staging
,
106 unsigned first_level
, unsigned last_level
,
107 unsigned first_layer
, unsigned last_layer
)
109 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
110 unsigned layer
, level
, checked_last_layer
, max_layer
;
112 const struct util_format_description
*desc
;
114 struct r600_resource_texture
*flushed_depth_texture
= staging
?
115 staging
: texture
->flushed_depth_texture
;
117 if (!staging
&& !texture
->dirty_db_mask
)
120 desc
= util_format_description(flushed_depth_texture
->resource
.b
.b
.format
);
121 switch (util_format_has_depth(desc
) | util_format_has_stencil(desc
) << 1) {
123 assert(!"No depth or stencil to uncompress");
125 custom_dsa
= rctx
->custom_dsa_flush_depth_stencil
;
128 custom_dsa
= rctx
->custom_dsa_flush_stencil
;
131 custom_dsa
= rctx
->custom_dsa_flush_depth
;
135 for (level
= first_level
; level
<= last_level
; level
++) {
136 if (!staging
&& !(texture
->dirty_db_mask
& (1 << level
)))
139 /* The smaller the mipmap level, the less layers there are
140 * as far as 3D textures are concerned. */
141 max_layer
= util_max_layer(&texture
->resource
.b
.b
, level
);
142 checked_last_layer
= last_layer
< max_layer
? last_layer
: max_layer
;
144 for (layer
= first_layer
; layer
<= checked_last_layer
; layer
++) {
145 struct pipe_surface
*zsurf
, *cbsurf
, surf_tmpl
;
147 surf_tmpl
.format
= texture
->real_format
;
148 surf_tmpl
.u
.tex
.level
= level
;
149 surf_tmpl
.u
.tex
.first_layer
= layer
;
150 surf_tmpl
.u
.tex
.last_layer
= layer
;
152 zsurf
= ctx
->create_surface(ctx
, &texture
->resource
.b
.b
, &surf_tmpl
);
154 surf_tmpl
.format
= flushed_depth_texture
->real_format
;
155 cbsurf
= ctx
->create_surface(ctx
,
156 (struct pipe_resource
*)flushed_depth_texture
, &surf_tmpl
);
158 r600_blitter_begin(ctx
, R600_DECOMPRESS
);
159 util_blitter_custom_depth_stencil(rctx
->blitter
, zsurf
, cbsurf
, ~0, custom_dsa
, depth
);
160 r600_blitter_end(ctx
);
162 pipe_surface_reference(&zsurf
, NULL
);
163 pipe_surface_reference(&cbsurf
, NULL
);
166 /* The texture will always be dirty if some layers aren't flushed.
167 * I don't think this case can occur though. */
168 if (!staging
&& first_layer
== 0 && last_layer
== max_layer
) {
169 texture
->dirty_db_mask
&= ~(1 << level
);
174 static void si_blit_decompress_depth_in_place(struct r600_context
*rctx
,
175 struct r600_resource_texture
*texture
,
176 unsigned first_level
, unsigned last_level
,
177 unsigned first_layer
, unsigned last_layer
)
179 struct pipe_surface
*zsurf
, surf_tmpl
= {{0}};
180 unsigned layer
, max_layer
, checked_last_layer
, level
;
182 surf_tmpl
.format
= texture
->resource
.b
.b
.format
;
184 for (level
= first_level
; level
<= last_level
; level
++) {
185 if (!(texture
->dirty_db_mask
& (1 << level
)))
188 surf_tmpl
.u
.tex
.level
= level
;
190 /* The smaller the mipmap level, the less layers there are
191 * as far as 3D textures are concerned. */
192 max_layer
= util_max_layer(&texture
->resource
.b
.b
, level
);
193 checked_last_layer
= last_layer
< max_layer
? last_layer
: max_layer
;
195 for (layer
= first_layer
; layer
<= checked_last_layer
; layer
++) {
196 surf_tmpl
.u
.tex
.first_layer
= layer
;
197 surf_tmpl
.u
.tex
.last_layer
= layer
;
199 zsurf
= rctx
->context
.create_surface(&rctx
->context
, &texture
->resource
.b
.b
, &surf_tmpl
);
201 r600_blitter_begin(&rctx
->context
, R600_DECOMPRESS
);
202 util_blitter_custom_depth_stencil(rctx
->blitter
, zsurf
, NULL
, ~0,
203 rctx
->custom_dsa_flush_inplace
,
205 r600_blitter_end(&rctx
->context
);
207 pipe_surface_reference(&zsurf
, NULL
);
210 /* The texture will always be dirty if some layers aren't flushed.
211 * I don't think this case occurs often though. */
212 if (first_layer
== 0 && last_layer
== max_layer
) {
213 texture
->dirty_db_mask
&= ~(1 << level
);
218 void si_flush_depth_textures(struct r600_context
*rctx
,
219 struct r600_textures_info
*textures
)
223 for (i
= 0; i
< textures
->n_views
; ++i
) {
224 struct pipe_sampler_view
*view
;
225 struct r600_resource_texture
*tex
;
227 view
= &textures
->views
[i
]->base
;
230 tex
= (struct r600_resource_texture
*)view
->texture
;
231 if (!tex
->is_depth
|| tex
->is_flushing_texture
)
234 si_blit_decompress_depth_in_place(rctx
, tex
,
235 view
->u
.tex
.first_level
, view
->u
.tex
.last_level
,
236 0, util_max_layer(&tex
->resource
.b
.b
, view
->u
.tex
.first_level
));
240 static void r600_clear(struct pipe_context
*ctx
, unsigned buffers
,
241 const union pipe_color_union
*color
,
242 double depth
, unsigned stencil
)
244 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
245 struct pipe_framebuffer_state
*fb
= &rctx
->framebuffer
;
247 r600_blitter_begin(ctx
, R600_CLEAR
);
248 util_blitter_clear(rctx
->blitter
, fb
->width
, fb
->height
,
249 buffers
, color
, depth
, stencil
);
250 r600_blitter_end(ctx
);
253 static void r600_clear_render_target(struct pipe_context
*ctx
,
254 struct pipe_surface
*dst
,
255 const union pipe_color_union
*color
,
256 unsigned dstx
, unsigned dsty
,
257 unsigned width
, unsigned height
)
259 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
261 r600_blitter_begin(ctx
, R600_CLEAR_SURFACE
);
262 util_blitter_clear_render_target(rctx
->blitter
, dst
, color
,
263 dstx
, dsty
, width
, height
);
264 r600_blitter_end(ctx
);
267 static void r600_clear_depth_stencil(struct pipe_context
*ctx
,
268 struct pipe_surface
*dst
,
269 unsigned clear_flags
,
272 unsigned dstx
, unsigned dsty
,
273 unsigned width
, unsigned height
)
275 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
277 r600_blitter_begin(ctx
, R600_CLEAR_SURFACE
);
278 util_blitter_clear_depth_stencil(rctx
->blitter
, dst
, clear_flags
, depth
, stencil
,
279 dstx
, dsty
, width
, height
);
280 r600_blitter_end(ctx
);
283 struct texture_orig_info
{
293 static void r600_compressed_to_blittable(struct pipe_resource
*tex
,
295 struct texture_orig_info
*orig
)
297 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)tex
;
298 unsigned pixsize
= util_format_get_blocksize(rtex
->real_format
);
300 int new_height
, new_width
;
302 orig
->format
= tex
->format
;
303 orig
->width0
= tex
->width0
;
304 orig
->height0
= tex
->height0
;
305 orig
->npix0_x
= rtex
->surface
.level
[0].npix_x
;
306 orig
->npix0_y
= rtex
->surface
.level
[0].npix_y
;
307 orig
->npix_x
= rtex
->surface
.level
[level
].npix_x
;
308 orig
->npix_y
= rtex
->surface
.level
[level
].npix_y
;
311 new_format
= PIPE_FORMAT_R16G16B16A16_UINT
; /* 64-bit block */
313 new_format
= PIPE_FORMAT_R32G32B32A32_UINT
; /* 128-bit block */
315 new_width
= util_format_get_nblocksx(tex
->format
, orig
->width0
);
316 new_height
= util_format_get_nblocksy(tex
->format
, orig
->height0
);
318 tex
->width0
= new_width
;
319 tex
->height0
= new_height
;
320 tex
->format
= new_format
;
321 rtex
->surface
.level
[0].npix_x
= util_format_get_nblocksx(orig
->format
, orig
->npix0_x
);
322 rtex
->surface
.level
[0].npix_y
= util_format_get_nblocksy(orig
->format
, orig
->npix0_y
);
323 rtex
->surface
.level
[level
].npix_x
= util_format_get_nblocksx(orig
->format
, orig
->npix_x
);
324 rtex
->surface
.level
[level
].npix_y
= util_format_get_nblocksy(orig
->format
, orig
->npix_y
);
327 static void r600_change_format(struct pipe_resource
*tex
,
329 struct texture_orig_info
*orig
,
330 enum pipe_format format
)
332 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)tex
;
334 orig
->format
= tex
->format
;
335 orig
->width0
= tex
->width0
;
336 orig
->height0
= tex
->height0
;
337 orig
->npix0_x
= rtex
->surface
.level
[0].npix_x
;
338 orig
->npix0_y
= rtex
->surface
.level
[0].npix_y
;
339 orig
->npix_x
= rtex
->surface
.level
[level
].npix_x
;
340 orig
->npix_y
= rtex
->surface
.level
[level
].npix_y
;
342 tex
->format
= format
;
345 static void r600_reset_blittable_to_orig(struct pipe_resource
*tex
,
347 struct texture_orig_info
*orig
)
349 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)tex
;
351 tex
->format
= orig
->format
;
352 tex
->width0
= orig
->width0
;
353 tex
->height0
= orig
->height0
;
354 rtex
->surface
.level
[0].npix_x
= orig
->npix0_x
;
355 rtex
->surface
.level
[0].npix_y
= orig
->npix0_y
;
356 rtex
->surface
.level
[level
].npix_x
= orig
->npix_x
;
357 rtex
->surface
.level
[level
].npix_y
= orig
->npix_y
;
360 static void r600_resource_copy_region(struct pipe_context
*ctx
,
361 struct pipe_resource
*dst
,
363 unsigned dstx
, unsigned dsty
, unsigned dstz
,
364 struct pipe_resource
*src
,
366 const struct pipe_box
*src_box
)
368 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
369 struct r600_resource_texture
*rsrc
= (struct r600_resource_texture
*)src
;
370 struct texture_orig_info orig_info
[2];
371 struct pipe_box sbox
;
372 const struct pipe_box
*psbox
= src_box
;
373 boolean restore_orig
[2];
375 memset(orig_info
, 0, sizeof(orig_info
));
377 /* Fallback for buffers. */
378 if (dst
->target
== PIPE_BUFFER
&& src
->target
== PIPE_BUFFER
) {
379 util_resource_copy_region(ctx
, dst
, dst_level
, dstx
, dsty
, dstz
,
380 src
, src_level
, src_box
);
384 /* This must be done before entering u_blitter to avoid recursion. */
385 if (rsrc
->is_depth
&& !rsrc
->is_flushing_texture
) {
386 si_blit_decompress_depth_in_place(rctx
, rsrc
,
387 src_level
, src_level
,
388 src_box
->z
, src_box
->z
+ src_box
->depth
- 1);
391 restore_orig
[0] = restore_orig
[1] = FALSE
;
393 if (util_format_is_compressed(src
->format
) &&
394 util_format_is_compressed(dst
->format
)) {
395 r600_compressed_to_blittable(src
, src_level
, &orig_info
[0]);
396 restore_orig
[0] = TRUE
;
397 sbox
.x
= util_format_get_nblocksx(orig_info
[0].format
, src_box
->x
);
398 sbox
.y
= util_format_get_nblocksy(orig_info
[0].format
, src_box
->y
);
400 sbox
.width
= util_format_get_nblocksx(orig_info
[0].format
, src_box
->width
);
401 sbox
.height
= util_format_get_nblocksy(orig_info
[0].format
, src_box
->height
);
402 sbox
.depth
= src_box
->depth
;
405 r600_compressed_to_blittable(dst
, dst_level
, &orig_info
[1]);
406 restore_orig
[1] = TRUE
;
407 /* translate the dst box as well */
408 dstx
= util_format_get_nblocksx(orig_info
[1].format
, dstx
);
409 dsty
= util_format_get_nblocksy(orig_info
[1].format
, dsty
);
410 } else if (!util_blitter_is_copy_supported(rctx
->blitter
, dst
, src
,
412 unsigned blocksize
= util_format_get_blocksize(src
->format
);
416 r600_change_format(src
, src_level
, &orig_info
[0],
417 PIPE_FORMAT_R8_UNORM
);
418 r600_change_format(dst
, dst_level
, &orig_info
[1],
419 PIPE_FORMAT_R8_UNORM
);
422 r600_change_format(src
, src_level
, &orig_info
[0],
423 PIPE_FORMAT_R8G8_UNORM
);
424 r600_change_format(dst
, dst_level
, &orig_info
[1],
425 PIPE_FORMAT_R8G8_UNORM
);
428 r600_change_format(src
, src_level
, &orig_info
[0],
429 PIPE_FORMAT_R8G8B8A8_UNORM
);
430 r600_change_format(dst
, dst_level
, &orig_info
[1],
431 PIPE_FORMAT_R8G8B8A8_UNORM
);
434 r600_change_format(src
, src_level
, &orig_info
[0],
435 PIPE_FORMAT_R16G16B16A16_UINT
);
436 r600_change_format(dst
, dst_level
, &orig_info
[1],
437 PIPE_FORMAT_R16G16B16A16_UINT
);
440 r600_change_format(src
, src_level
, &orig_info
[0],
441 PIPE_FORMAT_R32G32B32A32_UINT
);
442 r600_change_format(dst
, dst_level
, &orig_info
[1],
443 PIPE_FORMAT_R32G32B32A32_UINT
);
446 fprintf(stderr
, "Unhandled format %s with blocksize %u\n",
447 util_format_short_name(src
->format
), blocksize
);
450 restore_orig
[0] = TRUE
;
451 restore_orig
[1] = TRUE
;
454 r600_blitter_begin(ctx
, R600_COPY
);
455 util_blitter_copy_texture(rctx
->blitter
, dst
, dst_level
, dstx
, dsty
, dstz
,
456 src
, src_level
, psbox
, PIPE_MASK_RGBAZS
, TRUE
);
457 r600_blitter_end(ctx
);
460 r600_reset_blittable_to_orig(src
, src_level
, &orig_info
[0]);
463 r600_reset_blittable_to_orig(dst
, dst_level
, &orig_info
[1]);
466 static void si_blit(struct pipe_context
*ctx
,
467 const struct pipe_blit_info
*info
)
469 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
470 struct r600_resource_texture
*rsrc
= (struct r600_resource_texture
*)info
->src
.resource
;
472 assert(util_blitter_is_blit_supported(rctx
->blitter
, info
));
474 if (info
->src
.resource
->nr_samples
> 1 &&
475 info
->dst
.resource
->nr_samples
<= 1 &&
476 !util_format_is_depth_or_stencil(info
->src
.resource
->format
) &&
477 !util_format_is_pure_integer(info
->src
.resource
->format
)) {
478 debug_printf("radeonsi: color resolve is unimplemented\n");
482 if (rsrc
->is_depth
&& !rsrc
->is_flushing_texture
) {
483 si_blit_decompress_depth_in_place(rctx
, rsrc
,
484 info
->src
.level
, info
->src
.level
,
486 info
->src
.box
.z
+ info
->src
.box
.depth
- 1);
489 r600_blitter_begin(ctx
, R600_BLIT
);
490 util_blitter_blit(rctx
->blitter
, info
);
491 r600_blitter_end(ctx
);
494 void si_init_blit_functions(struct r600_context
*rctx
)
496 rctx
->context
.clear
= r600_clear
;
497 rctx
->context
.clear_render_target
= r600_clear_render_target
;
498 rctx
->context
.clear_depth_stencil
= r600_clear_depth_stencil
;
499 rctx
->context
.resource_copy_region
= r600_resource_copy_region
;
500 rctx
->context
.blit
= si_blit
;