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_DECOMPRESS
= R600_SAVE_FRAGMENT_STATE
| R600_SAVE_FRAMEBUFFER
| R600_DISABLE_RENDER_COND
,
46 R600_COLOR_RESOLVE
= R600_SAVE_FRAGMENT_STATE
| R600_SAVE_FRAMEBUFFER
| R600_DISABLE_RENDER_COND
49 static void r600_blitter_begin(struct pipe_context
*ctx
, enum r600_blitter_op op
)
51 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
53 r600_suspend_nontimer_queries(rctx
);
55 util_blitter_save_vertex_buffers(rctx
->blitter
,
56 util_last_bit(rctx
->vertex_buffer_state
.enabled_mask
),
57 rctx
->vertex_buffer_state
.vb
);
58 util_blitter_save_vertex_elements(rctx
->blitter
, rctx
->vertex_elements
);
59 util_blitter_save_vertex_shader(rctx
->blitter
, rctx
->vs_shader
);
60 util_blitter_save_so_targets(rctx
->blitter
, rctx
->num_so_targets
,
61 (struct pipe_stream_output_target
**)rctx
->so_targets
);
62 util_blitter_save_rasterizer(rctx
->blitter
, rctx
->states
[R600_PIPE_STATE_RASTERIZER
]);
64 if (op
& R600_SAVE_FRAGMENT_STATE
) {
65 util_blitter_save_viewport(rctx
->blitter
, &rctx
->viewport
.state
);
66 util_blitter_save_fragment_shader(rctx
->blitter
, rctx
->ps_shader
);
67 util_blitter_save_blend(rctx
->blitter
, rctx
->states
[R600_PIPE_STATE_BLEND
]);
68 util_blitter_save_depth_stencil_alpha(rctx
->blitter
, rctx
->states
[R600_PIPE_STATE_DSA
]);
69 util_blitter_save_stencil_ref(rctx
->blitter
, &rctx
->stencil_ref
.pipe_state
);
70 util_blitter_save_sample_mask(rctx
->blitter
, rctx
->sample_mask
.sample_mask
);
73 if (op
& R600_SAVE_FRAMEBUFFER
)
74 util_blitter_save_framebuffer(rctx
->blitter
, &rctx
->framebuffer
);
76 if (op
& R600_SAVE_TEXTURES
) {
77 util_blitter_save_fragment_sampler_states(
78 rctx
->blitter
, util_last_bit(rctx
->samplers
[PIPE_SHADER_FRAGMENT
].states
.enabled_mask
),
79 (void**)rctx
->samplers
[PIPE_SHADER_FRAGMENT
].states
.states
);
81 util_blitter_save_fragment_sampler_views(
82 rctx
->blitter
, util_last_bit(rctx
->samplers
[PIPE_SHADER_FRAGMENT
].views
.enabled_mask
),
83 (struct pipe_sampler_view
**)rctx
->samplers
[PIPE_SHADER_FRAGMENT
].views
.views
);
86 if ((op
& R600_DISABLE_RENDER_COND
) && rctx
->current_render_cond
) {
87 rctx
->saved_render_cond
= rctx
->current_render_cond
;
88 rctx
->saved_render_cond_mode
= rctx
->current_render_cond_mode
;
89 rctx
->context
.render_condition(&rctx
->context
, NULL
, 0);
94 static void r600_blitter_end(struct pipe_context
*ctx
)
96 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
97 if (rctx
->saved_render_cond
) {
98 rctx
->context
.render_condition(&rctx
->context
,
99 rctx
->saved_render_cond
,
100 rctx
->saved_render_cond_mode
);
101 rctx
->saved_render_cond
= NULL
;
103 r600_resume_nontimer_queries(rctx
);
106 static unsigned u_max_layer(struct pipe_resource
*r
, unsigned level
)
109 case PIPE_TEXTURE_CUBE
:
111 case PIPE_TEXTURE_3D
:
112 return u_minify(r
->depth0
, level
) - 1;
113 case PIPE_TEXTURE_1D_ARRAY
:
114 case PIPE_TEXTURE_2D_ARRAY
:
115 return r
->array_size
- 1;
121 static unsigned u_max_sample(struct pipe_resource
*r
)
123 return r
->nr_samples
? r
->nr_samples
- 1 : 0;
126 void r600_blit_decompress_depth(struct pipe_context
*ctx
,
127 struct r600_texture
*texture
,
128 struct r600_texture
*staging
,
129 unsigned first_level
, unsigned last_level
,
130 unsigned first_layer
, unsigned last_layer
,
131 unsigned first_sample
, unsigned last_sample
)
133 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
134 unsigned layer
, level
, sample
, checked_last_layer
, max_layer
, max_sample
;
135 struct r600_texture
*flushed_depth_texture
= staging
?
136 staging
: texture
->flushed_depth_texture
;
137 const struct util_format_description
*desc
=
138 util_format_description(texture
->resource
.b
.b
.format
);
141 if (!staging
&& !texture
->dirty_level_mask
)
144 max_sample
= u_max_sample(&texture
->resource
.b
.b
);
146 /* XXX Decompressing MSAA depth textures is broken on R6xx.
147 * There is also a hardlock if CMASK and FMASK are not present.
148 * Just skip this until we find out how to fix it. */
149 if (rctx
->chip_class
== R600
&& max_sample
> 0) {
150 texture
->dirty_level_mask
= 0;
154 if (rctx
->family
== CHIP_RV610
|| rctx
->family
== CHIP_RV630
||
155 rctx
->family
== CHIP_RV620
|| rctx
->family
== CHIP_RV635
)
160 /* Enable decompression in DB_RENDER_CONTROL */
161 rctx
->db_misc_state
.flush_depthstencil_through_cb
= true;
162 rctx
->db_misc_state
.copy_depth
= util_format_has_depth(desc
);
163 rctx
->db_misc_state
.copy_stencil
= util_format_has_stencil(desc
);
164 rctx
->db_misc_state
.copy_sample
= first_sample
;
165 r600_atom_dirty(rctx
, &rctx
->db_misc_state
.atom
);
168 for (level
= first_level
; level
<= last_level
; level
++) {
169 if (!staging
&& !(texture
->dirty_level_mask
& (1 << level
)))
172 /* The smaller the mipmap level, the less layers there are
173 * as far as 3D textures are concerned. */
174 max_layer
= u_max_layer(&texture
->resource
.b
.b
, level
);
175 checked_last_layer
= last_layer
< max_layer
? last_layer
: max_layer
;
177 for (layer
= first_layer
; layer
<= checked_last_layer
; layer
++) {
178 for (sample
= first_sample
; sample
<= last_sample
; sample
++) {
179 struct pipe_surface
*zsurf
, *cbsurf
, surf_tmpl
;
181 if (sample
!= rctx
->db_misc_state
.copy_sample
) {
182 rctx
->db_misc_state
.copy_sample
= sample
;
183 r600_atom_dirty(rctx
, &rctx
->db_misc_state
.atom
);
186 surf_tmpl
.format
= texture
->resource
.b
.b
.format
;
187 surf_tmpl
.u
.tex
.level
= level
;
188 surf_tmpl
.u
.tex
.first_layer
= layer
;
189 surf_tmpl
.u
.tex
.last_layer
= layer
;
190 surf_tmpl
.usage
= PIPE_BIND_DEPTH_STENCIL
;
192 zsurf
= ctx
->create_surface(ctx
, &texture
->resource
.b
.b
, &surf_tmpl
);
194 surf_tmpl
.format
= flushed_depth_texture
->resource
.b
.b
.format
;
195 surf_tmpl
.u
.tex
.level
= level
;
196 surf_tmpl
.u
.tex
.first_layer
= layer
;
197 surf_tmpl
.u
.tex
.last_layer
= layer
;
198 surf_tmpl
.usage
= PIPE_BIND_RENDER_TARGET
;
199 cbsurf
= ctx
->create_surface(ctx
,
200 &flushed_depth_texture
->resource
.b
.b
, &surf_tmpl
);
202 r600_blitter_begin(ctx
, R600_DECOMPRESS
);
203 util_blitter_custom_depth_stencil(rctx
->blitter
, zsurf
, cbsurf
, 1 << sample
,
204 rctx
->custom_dsa_flush
, depth
);
205 r600_blitter_end(ctx
);
207 pipe_surface_reference(&zsurf
, NULL
);
208 pipe_surface_reference(&cbsurf
, NULL
);
212 /* The texture will always be dirty if some layers or samples aren't flushed.
213 * I don't think this case occurs often though. */
215 first_layer
== 0 && last_layer
== max_layer
&&
216 first_sample
== 0 && last_sample
== max_sample
) {
217 texture
->dirty_level_mask
&= ~(1 << level
);
221 /* reenable compression in DB_RENDER_CONTROL */
222 rctx
->db_misc_state
.flush_depthstencil_through_cb
= false;
223 r600_atom_dirty(rctx
, &rctx
->db_misc_state
.atom
);
226 void r600_decompress_depth_textures(struct r600_context
*rctx
,
227 struct r600_samplerview_state
*textures
)
230 unsigned depth_texture_mask
= textures
->compressed_depthtex_mask
;
232 while (depth_texture_mask
) {
233 struct pipe_sampler_view
*view
;
234 struct r600_texture
*tex
;
236 i
= u_bit_scan(&depth_texture_mask
);
238 view
= &textures
->views
[i
]->base
;
241 tex
= (struct r600_texture
*)view
->texture
;
242 assert(tex
->is_depth
&& !tex
->is_flushing_texture
);
244 r600_blit_decompress_depth(&rctx
->context
, tex
, NULL
,
245 view
->u
.tex
.first_level
, view
->u
.tex
.last_level
,
246 0, u_max_layer(&tex
->resource
.b
.b
, view
->u
.tex
.first_level
),
247 0, u_max_sample(&tex
->resource
.b
.b
));
251 static void r600_blit_decompress_color(struct pipe_context
*ctx
,
252 struct r600_texture
*rtex
,
253 unsigned first_level
, unsigned last_level
,
254 unsigned first_layer
, unsigned last_layer
)
256 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
257 unsigned layer
, level
, checked_last_layer
, max_layer
;
259 assert(rctx
->chip_class
!= CAYMAN
);
261 if (!rtex
->dirty_level_mask
)
264 for (level
= first_level
; level
<= last_level
; level
++) {
265 if (!(rtex
->dirty_level_mask
& (1 << level
)))
268 /* The smaller the mipmap level, the less layers there are
269 * as far as 3D textures are concerned. */
270 max_layer
= u_max_layer(&rtex
->resource
.b
.b
, level
);
271 checked_last_layer
= last_layer
< max_layer
? last_layer
: max_layer
;
273 for (layer
= first_layer
; layer
<= checked_last_layer
; layer
++) {
274 struct pipe_surface
*cbsurf
, surf_tmpl
;
276 surf_tmpl
.format
= rtex
->resource
.b
.b
.format
;
277 surf_tmpl
.u
.tex
.level
= level
;
278 surf_tmpl
.u
.tex
.first_layer
= layer
;
279 surf_tmpl
.u
.tex
.last_layer
= layer
;
280 surf_tmpl
.usage
= PIPE_BIND_RENDER_TARGET
;
281 cbsurf
= ctx
->create_surface(ctx
, &rtex
->resource
.b
.b
, &surf_tmpl
);
283 r600_blitter_begin(ctx
, R600_DECOMPRESS
);
284 util_blitter_custom_color(rctx
->blitter
, cbsurf
,
285 rctx
->custom_blend_decompress
);
286 r600_blitter_end(ctx
);
288 pipe_surface_reference(&cbsurf
, NULL
);
291 /* The texture will always be dirty if some layers or samples aren't flushed.
292 * I don't think this case occurs often though. */
293 if (first_layer
== 0 && last_layer
== max_layer
) {
294 rtex
->dirty_level_mask
&= ~(1 << level
);
299 void r600_decompress_color_textures(struct r600_context
*rctx
,
300 struct r600_samplerview_state
*textures
)
303 unsigned mask
= textures
->compressed_colortex_mask
;
305 /* Cayman cannot decompress an MSAA colorbuffer,
306 * but it can read it compressed, so skip this. */
307 assert(rctx
->chip_class
!= CAYMAN
);
308 if (rctx
->chip_class
== CAYMAN
) {
313 struct pipe_sampler_view
*view
;
314 struct r600_texture
*tex
;
316 i
= u_bit_scan(&mask
);
318 view
= &textures
->views
[i
]->base
;
321 tex
= (struct r600_texture
*)view
->texture
;
322 assert(tex
->cmask_size
&& tex
->fmask_size
);
324 r600_blit_decompress_color(&rctx
->context
, tex
,
325 view
->u
.tex
.first_level
, view
->u
.tex
.last_level
,
326 0, u_max_layer(&tex
->resource
.b
.b
, view
->u
.tex
.first_level
));
330 static void r600_copy_first_sample(struct pipe_context
*ctx
,
331 const struct pipe_resolve_info
*info
)
333 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
334 struct r600_texture
*rsrc
= (struct r600_texture
*)info
->src
.res
;
335 struct pipe_surface
*dst_view
, dst_templ
;
336 struct pipe_sampler_view src_templ
, *src_view
;
339 if (rsrc
->is_depth
&& !rsrc
->is_flushing_texture
) {
340 if (!r600_init_flushed_depth_texture(ctx
, info
->src
.res
, NULL
))
343 /* Decompress the first sample only. */
344 r600_blit_decompress_depth(ctx
, rsrc
, NULL
,
346 info
->src
.layer
, info
->src
.layer
,
349 if (rctx
->chip_class
!= CAYMAN
&& rsrc
->fmask_size
&& rsrc
->cmask_size
) {
350 r600_blit_decompress_color(ctx
, rsrc
,
352 info
->src
.layer
, info
->src
.layer
);
355 /* this is correct for upside-down blits too */
356 u_box_2d(info
->src
.x0
,
358 info
->src
.x1
- info
->src
.x0
,
359 info
->src
.y1
- info
->src
.y0
, &box
);
361 /* Initialize the surface. */
362 util_blitter_default_dst_texture(&dst_templ
, info
->dst
.res
,
363 info
->dst
.level
, info
->dst
.layer
, &box
);
364 dst_view
= ctx
->create_surface(ctx
, info
->dst
.res
, &dst_templ
);
366 /* Initialize the sampler view. */
367 util_blitter_default_src_texture(&src_templ
, info
->src
.res
, 0);
368 src_view
= ctx
->create_sampler_view(ctx
, info
->src
.res
, &src_templ
);
370 /* Copy the first sample into dst. */
371 r600_blitter_begin(ctx
, R600_COPY_TEXTURE
);
372 util_blitter_copy_texture_view(rctx
->blitter
, dst_view
, ~0, info
->dst
.x0
,
373 info
->dst
.y0
, src_view
, 0, &box
,
374 info
->src
.res
->width0
, info
->src
.res
->height0
,
376 r600_blitter_end(ctx
);
378 pipe_surface_reference(&dst_view
, NULL
);
379 pipe_sampler_view_reference(&src_view
, NULL
);
382 static boolean
is_simple_resolve(const struct pipe_resolve_info
*info
)
384 unsigned dst_width
= u_minify(info
->dst
.res
->width0
, info
->dst
.level
);
385 unsigned dst_height
= u_minify(info
->dst
.res
->height0
, info
->dst
.level
);
387 return info
->dst
.res
->format
== info
->src
.res
->format
&&
388 dst_width
== info
->src
.res
->width0
&&
389 dst_height
== info
->src
.res
->height0
&&
392 info
->dst
.x1
== dst_width
&&
393 info
->dst
.y1
== dst_height
&&
396 info
->src
.x1
== dst_width
&&
397 info
->src
.y1
== dst_height
;
400 static void r600_color_resolve(struct pipe_context
*ctx
,
401 const struct pipe_resolve_info
*info
)
403 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
404 struct pipe_screen
*screen
= ctx
->screen
;
405 struct pipe_resource
*tmp
, templ
;
407 unsigned sample_mask
=
408 rctx
->chip_class
== CAYMAN
? ~0 : ((1ull << MAX2(1, info
->src
.res
->nr_samples
)) - 1);
410 assert((info
->mask
& PIPE_MASK_RGBA
) == PIPE_MASK_RGBA
);
412 if (is_simple_resolve(info
)) {
413 r600_blitter_begin(ctx
, R600_COLOR_RESOLVE
);
414 util_blitter_custom_resolve_color(rctx
->blitter
,
415 info
->dst
.res
, info
->dst
.level
, info
->dst
.layer
,
416 info
->src
.res
, info
->src
.layer
,
417 sample_mask
, rctx
->custom_blend_resolve
);
418 r600_blitter_end(ctx
);
422 /* resolve into a temporary texture, then blit */
423 templ
.target
= PIPE_TEXTURE_2D
;
424 templ
.format
= info
->src
.res
->format
;
425 templ
.width0
= info
->src
.res
->width0
;
426 templ
.height0
= info
->src
.res
->height0
;
428 templ
.array_size
= 1;
429 templ
.last_level
= 0;
430 templ
.nr_samples
= 0;
431 templ
.usage
= PIPE_USAGE_STATIC
;
432 templ
.bind
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
435 tmp
= screen
->resource_create(screen
, &templ
);
437 /* XXX use scissor, so that only the needed part of the resource is resolved */
438 r600_blitter_begin(ctx
, R600_COLOR_RESOLVE
);
439 util_blitter_custom_resolve_color(rctx
->blitter
,
441 info
->src
.res
, info
->src
.layer
,
442 sample_mask
, rctx
->custom_blend_resolve
);
443 r600_blitter_end(ctx
);
445 /* this is correct for upside-down blits too */
446 u_box_2d(info
->src
.x0
,
448 info
->src
.x1
- info
->src
.x0
,
449 info
->src
.y1
- info
->src
.y0
, &box
);
451 r600_blitter_begin(ctx
, R600_COPY_TEXTURE
);
452 util_blitter_copy_texture(rctx
->blitter
, info
->dst
.res
, info
->dst
.level
,
453 ~0, info
->dst
.x0
, info
->dst
.y0
, info
->dst
.layer
,
455 r600_blitter_end(ctx
);
457 pipe_resource_reference(&tmp
, NULL
);
460 static void r600_resource_resolve(struct pipe_context
*ctx
,
461 const struct pipe_resolve_info
*info
)
463 /* make sure we're doing a resolve operation */
464 assert(info
->src
.res
->nr_samples
> 1);
465 assert(info
->dst
.res
->nr_samples
<= 1);
467 /* limitations of multisample resources */
468 assert(info
->src
.res
->last_level
== 0);
469 assert(info
->src
.res
->target
== PIPE_TEXTURE_2D
||
470 info
->src
.res
->target
== PIPE_TEXTURE_2D_ARRAY
);
472 /* check if the resolve box is valid */
473 assert(info
->dst
.x0
< info
->dst
.x1
);
474 assert(info
->dst
.y0
< info
->dst
.y1
);
476 /* scaled resolve isn't allowed */
477 assert(abs(info
->dst
.x0
- info
->dst
.x1
) ==
478 abs(info
->src
.x0
- info
->src
.x1
));
479 assert(abs(info
->dst
.y0
- info
->dst
.y1
) ==
480 abs(info
->src
.y0
- info
->src
.y1
));
482 if ((info
->mask
& PIPE_MASK_ZS
) ||
483 util_format_is_pure_integer(info
->src
.res
->format
)) {
484 r600_copy_first_sample(ctx
, info
);
486 r600_color_resolve(ctx
, info
);
490 static void r600_clear(struct pipe_context
*ctx
, unsigned buffers
,
491 const union pipe_color_union
*color
,
492 double depth
, unsigned stencil
)
494 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
495 struct pipe_framebuffer_state
*fb
= &rctx
->framebuffer
;
497 r600_blitter_begin(ctx
, R600_CLEAR
);
498 util_blitter_clear(rctx
->blitter
, fb
->width
, fb
->height
,
499 fb
->nr_cbufs
, buffers
, fb
->nr_cbufs
? fb
->cbufs
[0]->format
: PIPE_FORMAT_NONE
,
500 color
, depth
, stencil
);
501 r600_blitter_end(ctx
);
504 static void r600_clear_render_target(struct pipe_context
*ctx
,
505 struct pipe_surface
*dst
,
506 const union pipe_color_union
*color
,
507 unsigned dstx
, unsigned dsty
,
508 unsigned width
, unsigned height
)
510 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
512 r600_blitter_begin(ctx
, R600_CLEAR_SURFACE
);
513 util_blitter_clear_render_target(rctx
->blitter
, dst
, color
,
514 dstx
, dsty
, width
, height
);
515 r600_blitter_end(ctx
);
518 static void r600_clear_depth_stencil(struct pipe_context
*ctx
,
519 struct pipe_surface
*dst
,
520 unsigned clear_flags
,
523 unsigned dstx
, unsigned dsty
,
524 unsigned width
, unsigned height
)
526 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
528 r600_blitter_begin(ctx
, R600_CLEAR_SURFACE
);
529 util_blitter_clear_depth_stencil(rctx
->blitter
, dst
, clear_flags
, depth
, stencil
,
530 dstx
, dsty
, width
, height
);
531 r600_blitter_end(ctx
);
534 void r600_copy_buffer(struct pipe_context
*ctx
, struct
535 pipe_resource
*dst
, unsigned dstx
,
536 struct pipe_resource
*src
, const struct pipe_box
*src_box
)
538 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
540 if (rctx
->screen
->has_streamout
&&
541 /* Require dword alignment. */
542 dstx
% 4 == 0 && src_box
->x
% 4 == 0 && src_box
->width
% 4 == 0) {
543 r600_blitter_begin(ctx
, R600_COPY_BUFFER
);
544 util_blitter_copy_buffer(rctx
->blitter
, dst
, dstx
, src
, src_box
->x
, src_box
->width
);
545 r600_blitter_end(ctx
);
547 util_resource_copy_region(ctx
, dst
, 0, dstx
, 0, 0, src
, 0, src_box
);
551 struct texture_orig_info
{
561 static void r600_compressed_to_blittable(struct pipe_resource
*tex
,
563 struct texture_orig_info
*orig
)
565 struct r600_texture
*rtex
= (struct r600_texture
*)tex
;
566 unsigned pixsize
= util_format_get_blocksize(rtex
->resource
.b
.b
.format
);
568 int new_height
, new_width
;
570 orig
->format
= tex
->format
;
571 orig
->width0
= tex
->width0
;
572 orig
->height0
= tex
->height0
;
573 orig
->npix0_x
= rtex
->surface
.level
[0].npix_x
;
574 orig
->npix0_y
= rtex
->surface
.level
[0].npix_y
;
575 orig
->npix_x
= rtex
->surface
.level
[level
].npix_x
;
576 orig
->npix_y
= rtex
->surface
.level
[level
].npix_y
;
579 new_format
= PIPE_FORMAT_R16G16B16A16_UINT
; /* 64-bit block */
581 new_format
= PIPE_FORMAT_R32G32B32A32_UINT
; /* 128-bit block */
583 new_width
= util_format_get_nblocksx(tex
->format
, orig
->width0
);
584 new_height
= util_format_get_nblocksy(tex
->format
, orig
->height0
);
586 tex
->width0
= new_width
;
587 tex
->height0
= new_height
;
588 tex
->format
= new_format
;
589 rtex
->surface
.level
[0].npix_x
= util_format_get_nblocksx(orig
->format
, orig
->npix0_x
);
590 rtex
->surface
.level
[0].npix_y
= util_format_get_nblocksy(orig
->format
, orig
->npix0_y
);
591 rtex
->surface
.level
[level
].npix_x
= util_format_get_nblocksx(orig
->format
, orig
->npix_x
);
592 rtex
->surface
.level
[level
].npix_y
= util_format_get_nblocksy(orig
->format
, orig
->npix_y
);
595 static void r600_subsampled_2x1_32bpp_to_blittable(struct pipe_resource
*tex
,
597 struct texture_orig_info
*orig
)
599 struct r600_texture
*rtex
= (struct r600_texture
*)tex
;
601 orig
->format
= tex
->format
;
602 orig
->width0
= tex
->width0
;
603 orig
->height0
= tex
->height0
;
604 orig
->npix0_x
= rtex
->surface
.level
[0].npix_x
;
605 orig
->npix0_y
= rtex
->surface
.level
[0].npix_y
;
606 orig
->npix_x
= rtex
->surface
.level
[level
].npix_x
;
607 orig
->npix_y
= rtex
->surface
.level
[level
].npix_y
;
609 tex
->width0
= util_format_get_nblocksx(orig
->format
, orig
->width0
);
610 tex
->format
= PIPE_FORMAT_R8G8B8A8_UINT
;
611 rtex
->surface
.level
[0].npix_x
= util_format_get_nblocksx(orig
->format
, orig
->npix0_x
);
612 rtex
->surface
.level
[level
].npix_x
= util_format_get_nblocksx(orig
->format
, orig
->npix_x
);
615 static void r600_change_format(struct pipe_resource
*tex
,
617 struct texture_orig_info
*orig
,
618 enum pipe_format format
)
620 struct r600_texture
*rtex
= (struct r600_texture
*)tex
;
622 orig
->format
= tex
->format
;
623 orig
->width0
= tex
->width0
;
624 orig
->height0
= tex
->height0
;
625 orig
->npix0_x
= rtex
->surface
.level
[0].npix_x
;
626 orig
->npix0_y
= rtex
->surface
.level
[0].npix_y
;
627 orig
->npix_x
= rtex
->surface
.level
[level
].npix_x
;
628 orig
->npix_y
= rtex
->surface
.level
[level
].npix_y
;
630 tex
->format
= format
;
633 static void r600_reset_blittable_to_orig(struct pipe_resource
*tex
,
635 struct texture_orig_info
*orig
)
637 struct r600_texture
*rtex
= (struct r600_texture
*)tex
;
639 tex
->format
= orig
->format
;
640 tex
->width0
= orig
->width0
;
641 tex
->height0
= orig
->height0
;
642 rtex
->surface
.level
[0].npix_x
= orig
->npix0_x
;
643 rtex
->surface
.level
[0].npix_y
= orig
->npix0_y
;
644 rtex
->surface
.level
[level
].npix_x
= orig
->npix_x
;
645 rtex
->surface
.level
[level
].npix_y
= orig
->npix_y
;
648 static bool util_format_is_subsampled_2x1_32bpp(enum pipe_format format
)
650 const struct util_format_description
*desc
= util_format_description(format
);
652 return desc
->layout
== UTIL_FORMAT_LAYOUT_SUBSAMPLED
&&
653 desc
->block
.width
== 2 &&
654 desc
->block
.height
== 1 &&
655 desc
->block
.bits
== 32;
658 static void r600_resource_copy_region(struct pipe_context
*ctx
,
659 struct pipe_resource
*dst
,
661 unsigned dstx
, unsigned dsty
, unsigned dstz
,
662 struct pipe_resource
*src
,
664 const struct pipe_box
*src_box
)
666 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
667 struct r600_texture
*rsrc
= (struct r600_texture
*)src
;
668 struct texture_orig_info orig_info
[2];
669 struct pipe_box sbox
;
670 const struct pipe_box
*psbox
= src_box
;
671 boolean restore_orig
[2];
672 unsigned last_sample
, i
;
674 memset(orig_info
, 0, sizeof(orig_info
));
676 /* Handle buffers first. */
677 if (dst
->target
== PIPE_BUFFER
&& src
->target
== PIPE_BUFFER
) {
678 r600_copy_buffer(ctx
, dst
, dstx
, src
, src_box
);
682 assert(u_max_sample(dst
) == u_max_sample(src
));
683 last_sample
= u_max_sample(dst
);
685 /* This must be done before entering u_blitter to avoid recursion. */
686 if (rsrc
->is_depth
&& !rsrc
->is_flushing_texture
) {
687 if (!r600_init_flushed_depth_texture(ctx
, src
, NULL
))
690 r600_blit_decompress_depth(ctx
, rsrc
, NULL
,
691 src_level
, src_level
,
692 src_box
->z
, src_box
->z
+ src_box
->depth
- 1,
693 0, u_max_sample(src
));
695 if (rctx
->chip_class
!= CAYMAN
&& rsrc
->fmask_size
&& rsrc
->cmask_size
) {
696 r600_blit_decompress_color(ctx
, rsrc
, src_level
, src_level
,
697 src_box
->z
, src_box
->z
+ src_box
->depth
- 1);
700 restore_orig
[0] = restore_orig
[1] = FALSE
;
702 if (util_format_is_compressed(src
->format
) &&
703 util_format_is_compressed(dst
->format
)) {
704 r600_compressed_to_blittable(src
, src_level
, &orig_info
[0]);
705 restore_orig
[0] = TRUE
;
706 sbox
.x
= util_format_get_nblocksx(orig_info
[0].format
, src_box
->x
);
707 sbox
.y
= util_format_get_nblocksy(orig_info
[0].format
, src_box
->y
);
709 sbox
.width
= util_format_get_nblocksx(orig_info
[0].format
, src_box
->width
);
710 sbox
.height
= util_format_get_nblocksy(orig_info
[0].format
, src_box
->height
);
711 sbox
.depth
= src_box
->depth
;
714 r600_compressed_to_blittable(dst
, dst_level
, &orig_info
[1]);
715 restore_orig
[1] = TRUE
;
716 /* translate the dst box as well */
717 dstx
= util_format_get_nblocksx(orig_info
[1].format
, dstx
);
718 dsty
= util_format_get_nblocksy(orig_info
[1].format
, dsty
);
719 } else if (!util_blitter_is_copy_supported(rctx
->blitter
, dst
, src
,
721 if (util_format_is_subsampled_2x1_32bpp(src
->format
) &&
722 util_format_is_subsampled_2x1_32bpp(dst
->format
)) {
723 r600_subsampled_2x1_32bpp_to_blittable(src
, src_level
, &orig_info
[0]);
724 r600_subsampled_2x1_32bpp_to_blittable(dst
, dst_level
, &orig_info
[1]);
727 sbox
.x
= util_format_get_nblocksx(orig_info
[0].format
, src_box
->x
);
728 sbox
.width
= util_format_get_nblocksx(orig_info
[0].format
, src_box
->width
);
731 dstx
= util_format_get_nblocksx(orig_info
[1].format
, dstx
);
733 unsigned blocksize
= util_format_get_blocksize(src
->format
);
737 r600_change_format(src
, src_level
, &orig_info
[0],
738 PIPE_FORMAT_R8_UNORM
);
739 r600_change_format(dst
, dst_level
, &orig_info
[1],
740 PIPE_FORMAT_R8_UNORM
);
743 r600_change_format(src
, src_level
, &orig_info
[0],
744 PIPE_FORMAT_R8G8B8A8_UNORM
);
745 r600_change_format(dst
, dst_level
, &orig_info
[1],
746 PIPE_FORMAT_R8G8B8A8_UNORM
);
749 fprintf(stderr
, "Unhandled format %s with blocksize %u\n",
750 util_format_short_name(src
->format
), blocksize
);
754 restore_orig
[0] = TRUE
;
755 restore_orig
[1] = TRUE
;
758 /* XXX Properly implement multisample textures on Cayman. In the meantime,
759 * copy only the first sample (which is the only one that doesn't return garbage). */
760 if (rctx
->chip_class
== CAYMAN
) {
761 r600_blitter_begin(ctx
, R600_COPY_TEXTURE
);
762 util_blitter_copy_texture(rctx
->blitter
, dst
, dst_level
, ~0, dstx
, dsty
, dstz
,
763 src
, src_level
, 0, psbox
);
764 r600_blitter_end(ctx
);
766 for (i
= 0; i
<= last_sample
; i
++) {
767 r600_blitter_begin(ctx
, R600_COPY_TEXTURE
);
768 util_blitter_copy_texture(rctx
->blitter
, dst
, dst_level
, 1 << i
, dstx
, dsty
, dstz
,
769 src
, src_level
, i
, psbox
);
770 r600_blitter_end(ctx
);
775 r600_reset_blittable_to_orig(src
, src_level
, &orig_info
[0]);
778 r600_reset_blittable_to_orig(dst
, dst_level
, &orig_info
[1]);
781 void r600_init_blit_functions(struct r600_context
*rctx
)
783 rctx
->context
.clear
= r600_clear
;
784 rctx
->context
.clear_render_target
= r600_clear_render_target
;
785 rctx
->context
.clear_depth_stencil
= r600_clear_depth_stencil
;
786 rctx
->context
.resource_copy_region
= r600_resource_copy_region
;
787 rctx
->context
.resource_resolve
= r600_resource_resolve
;