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
.state
);
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 util_blitter_save_render_condition(rctx
->blitter
,
88 rctx
->current_render_cond
,
89 rctx
->current_render_cond_mode
);
93 static void r600_blitter_end(struct pipe_context
*ctx
)
95 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
96 r600_resume_nontimer_queries(rctx
);
99 static unsigned u_max_layer(struct pipe_resource
*r
, unsigned level
)
102 case PIPE_TEXTURE_CUBE
:
104 case PIPE_TEXTURE_3D
:
105 return u_minify(r
->depth0
, level
) - 1;
106 case PIPE_TEXTURE_1D_ARRAY
:
107 case PIPE_TEXTURE_2D_ARRAY
:
108 return r
->array_size
- 1;
114 static unsigned u_max_sample(struct pipe_resource
*r
)
116 return r
->nr_samples
? r
->nr_samples
- 1 : 0;
119 void r600_blit_decompress_depth(struct pipe_context
*ctx
,
120 struct r600_texture
*texture
,
121 struct r600_texture
*staging
,
122 unsigned first_level
, unsigned last_level
,
123 unsigned first_layer
, unsigned last_layer
,
124 unsigned first_sample
, unsigned last_sample
)
126 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
127 unsigned layer
, level
, sample
, checked_last_layer
, max_layer
, max_sample
;
128 struct r600_texture
*flushed_depth_texture
= staging
?
129 staging
: texture
->flushed_depth_texture
;
130 const struct util_format_description
*desc
=
131 util_format_description(texture
->resource
.b
.b
.format
);
134 if (!staging
&& !texture
->dirty_level_mask
)
137 max_sample
= u_max_sample(&texture
->resource
.b
.b
);
139 /* XXX Decompressing MSAA depth textures is broken on R6xx.
140 * There is also a hardlock if CMASK and FMASK are not present.
141 * Just skip this until we find out how to fix it. */
142 if (rctx
->chip_class
== R600
&& max_sample
> 0) {
143 texture
->dirty_level_mask
= 0;
147 if (rctx
->family
== CHIP_RV610
|| rctx
->family
== CHIP_RV630
||
148 rctx
->family
== CHIP_RV620
|| rctx
->family
== CHIP_RV635
)
153 /* Enable decompression in DB_RENDER_CONTROL */
154 rctx
->db_misc_state
.flush_depthstencil_through_cb
= true;
155 rctx
->db_misc_state
.copy_depth
= util_format_has_depth(desc
);
156 rctx
->db_misc_state
.copy_stencil
= util_format_has_stencil(desc
);
157 rctx
->db_misc_state
.copy_sample
= first_sample
;
158 r600_atom_dirty(rctx
, &rctx
->db_misc_state
.atom
);
161 for (level
= first_level
; level
<= last_level
; level
++) {
162 if (!staging
&& !(texture
->dirty_level_mask
& (1 << level
)))
165 /* The smaller the mipmap level, the less layers there are
166 * as far as 3D textures are concerned. */
167 max_layer
= u_max_layer(&texture
->resource
.b
.b
, level
);
168 checked_last_layer
= last_layer
< max_layer
? last_layer
: max_layer
;
170 for (layer
= first_layer
; layer
<= checked_last_layer
; layer
++) {
171 for (sample
= first_sample
; sample
<= last_sample
; sample
++) {
172 struct pipe_surface
*zsurf
, *cbsurf
, surf_tmpl
;
174 if (sample
!= rctx
->db_misc_state
.copy_sample
) {
175 rctx
->db_misc_state
.copy_sample
= sample
;
176 r600_atom_dirty(rctx
, &rctx
->db_misc_state
.atom
);
179 surf_tmpl
.format
= texture
->resource
.b
.b
.format
;
180 surf_tmpl
.u
.tex
.level
= level
;
181 surf_tmpl
.u
.tex
.first_layer
= layer
;
182 surf_tmpl
.u
.tex
.last_layer
= layer
;
183 surf_tmpl
.usage
= PIPE_BIND_DEPTH_STENCIL
;
185 zsurf
= ctx
->create_surface(ctx
, &texture
->resource
.b
.b
, &surf_tmpl
);
187 surf_tmpl
.format
= flushed_depth_texture
->resource
.b
.b
.format
;
188 surf_tmpl
.u
.tex
.level
= level
;
189 surf_tmpl
.u
.tex
.first_layer
= layer
;
190 surf_tmpl
.u
.tex
.last_layer
= layer
;
191 surf_tmpl
.usage
= PIPE_BIND_RENDER_TARGET
;
192 cbsurf
= ctx
->create_surface(ctx
,
193 &flushed_depth_texture
->resource
.b
.b
, &surf_tmpl
);
195 r600_blitter_begin(ctx
, R600_DECOMPRESS
);
196 util_blitter_custom_depth_stencil(rctx
->blitter
, zsurf
, cbsurf
, 1 << sample
,
197 rctx
->custom_dsa_flush
, depth
);
198 r600_blitter_end(ctx
);
200 pipe_surface_reference(&zsurf
, NULL
);
201 pipe_surface_reference(&cbsurf
, NULL
);
205 /* The texture will always be dirty if some layers or samples aren't flushed.
206 * I don't think this case occurs often though. */
208 first_layer
== 0 && last_layer
== max_layer
&&
209 first_sample
== 0 && last_sample
== max_sample
) {
210 texture
->dirty_level_mask
&= ~(1 << level
);
214 /* reenable compression in DB_RENDER_CONTROL */
215 rctx
->db_misc_state
.flush_depthstencil_through_cb
= false;
216 r600_atom_dirty(rctx
, &rctx
->db_misc_state
.atom
);
219 void r600_decompress_depth_textures(struct r600_context
*rctx
,
220 struct r600_samplerview_state
*textures
)
223 unsigned depth_texture_mask
= textures
->compressed_depthtex_mask
;
225 while (depth_texture_mask
) {
226 struct pipe_sampler_view
*view
;
227 struct r600_texture
*tex
;
229 i
= u_bit_scan(&depth_texture_mask
);
231 view
= &textures
->views
[i
]->base
;
234 tex
= (struct r600_texture
*)view
->texture
;
235 assert(tex
->is_depth
&& !tex
->is_flushing_texture
);
237 r600_blit_decompress_depth(&rctx
->context
, tex
, NULL
,
238 view
->u
.tex
.first_level
, view
->u
.tex
.last_level
,
239 0, u_max_layer(&tex
->resource
.b
.b
, view
->u
.tex
.first_level
),
240 0, u_max_sample(&tex
->resource
.b
.b
));
244 static void r600_blit_decompress_color(struct pipe_context
*ctx
,
245 struct r600_texture
*rtex
,
246 unsigned first_level
, unsigned last_level
,
247 unsigned first_layer
, unsigned last_layer
)
249 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
250 unsigned layer
, level
, checked_last_layer
, max_layer
;
252 assert(rctx
->chip_class
!= CAYMAN
);
254 if (!rtex
->dirty_level_mask
)
257 for (level
= first_level
; level
<= last_level
; level
++) {
258 if (!(rtex
->dirty_level_mask
& (1 << level
)))
261 /* The smaller the mipmap level, the less layers there are
262 * as far as 3D textures are concerned. */
263 max_layer
= u_max_layer(&rtex
->resource
.b
.b
, level
);
264 checked_last_layer
= last_layer
< max_layer
? last_layer
: max_layer
;
266 for (layer
= first_layer
; layer
<= checked_last_layer
; layer
++) {
267 struct pipe_surface
*cbsurf
, surf_tmpl
;
269 surf_tmpl
.format
= rtex
->resource
.b
.b
.format
;
270 surf_tmpl
.u
.tex
.level
= level
;
271 surf_tmpl
.u
.tex
.first_layer
= layer
;
272 surf_tmpl
.u
.tex
.last_layer
= layer
;
273 surf_tmpl
.usage
= PIPE_BIND_RENDER_TARGET
;
274 cbsurf
= ctx
->create_surface(ctx
, &rtex
->resource
.b
.b
, &surf_tmpl
);
276 r600_blitter_begin(ctx
, R600_DECOMPRESS
);
277 util_blitter_custom_color(rctx
->blitter
, cbsurf
,
278 rctx
->custom_blend_decompress
);
279 r600_blitter_end(ctx
);
281 pipe_surface_reference(&cbsurf
, NULL
);
284 /* The texture will always be dirty if some layers or samples aren't flushed.
285 * I don't think this case occurs often though. */
286 if (first_layer
== 0 && last_layer
== max_layer
) {
287 rtex
->dirty_level_mask
&= ~(1 << level
);
292 void r600_decompress_color_textures(struct r600_context
*rctx
,
293 struct r600_samplerview_state
*textures
)
296 unsigned mask
= textures
->compressed_colortex_mask
;
298 /* Cayman cannot decompress an MSAA colorbuffer,
299 * but it can read it compressed, so skip this. */
300 assert(rctx
->chip_class
!= CAYMAN
);
301 if (rctx
->chip_class
== CAYMAN
) {
306 struct pipe_sampler_view
*view
;
307 struct r600_texture
*tex
;
309 i
= u_bit_scan(&mask
);
311 view
= &textures
->views
[i
]->base
;
314 tex
= (struct r600_texture
*)view
->texture
;
315 assert(tex
->cmask_size
&& tex
->fmask_size
);
317 r600_blit_decompress_color(&rctx
->context
, tex
,
318 view
->u
.tex
.first_level
, view
->u
.tex
.last_level
,
319 0, u_max_layer(&tex
->resource
.b
.b
, view
->u
.tex
.first_level
));
323 /* Helper for decompressing a portion of a color or depth resource before
324 * blitting if any decompression is needed.
325 * The driver doesn't decompress resources automatically while u_blitter is
327 static bool r600_decompress_subresource(struct pipe_context
*ctx
,
328 struct pipe_resource
*tex
,
330 unsigned first_layer
, unsigned last_layer
)
332 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
333 struct r600_texture
*rtex
= (struct r600_texture
*)tex
;
335 if (rtex
->is_depth
&& !rtex
->is_flushing_texture
) {
336 if (!r600_init_flushed_depth_texture(ctx
, tex
, NULL
))
337 return false; /* error */
339 r600_blit_decompress_depth(ctx
, rtex
, NULL
,
341 first_layer
, last_layer
,
342 0, u_max_sample(tex
));
343 } else if (rctx
->chip_class
!= CAYMAN
&& rtex
->fmask_size
&& rtex
->cmask_size
) {
344 r600_blit_decompress_color(ctx
, rtex
, level
, level
,
345 first_layer
, last_layer
);
350 static void r600_copy_first_sample(struct pipe_context
*ctx
,
351 const struct pipe_resolve_info
*info
)
353 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
354 struct pipe_surface
*dst_view
, dst_templ
;
355 struct pipe_sampler_view src_templ
, *src_view
;
358 /* The driver doesn't decompress resources automatically while
359 * u_blitter is rendering. */
360 if (!r600_decompress_subresource(ctx
, info
->src
.res
, 0,
361 info
->src
.layer
, info
->src
.layer
)) {
365 /* this is correct for upside-down blits too */
366 u_box_2d(info
->src
.x0
,
368 info
->src
.x1
- info
->src
.x0
,
369 info
->src
.y1
- info
->src
.y0
, &box
);
371 /* Initialize the surface. */
372 util_blitter_default_dst_texture(&dst_templ
, info
->dst
.res
,
373 info
->dst
.level
, info
->dst
.layer
, &box
);
374 dst_view
= ctx
->create_surface(ctx
, info
->dst
.res
, &dst_templ
);
376 /* Initialize the sampler view. */
377 util_blitter_default_src_texture(&src_templ
, info
->src
.res
, 0);
378 src_view
= ctx
->create_sampler_view(ctx
, info
->src
.res
, &src_templ
);
380 /* Copy the first sample into dst. */
381 r600_blitter_begin(ctx
, R600_COPY_TEXTURE
);
382 util_blitter_copy_texture_view(rctx
->blitter
, dst_view
, ~0, info
->dst
.x0
,
383 info
->dst
.y0
, src_view
, 0, &box
,
384 info
->src
.res
->width0
, info
->src
.res
->height0
,
386 r600_blitter_end(ctx
);
388 pipe_surface_reference(&dst_view
, NULL
);
389 pipe_sampler_view_reference(&src_view
, NULL
);
392 static boolean
is_simple_resolve(const struct pipe_resolve_info
*info
)
394 unsigned dst_width
= u_minify(info
->dst
.res
->width0
, info
->dst
.level
);
395 unsigned dst_height
= u_minify(info
->dst
.res
->height0
, info
->dst
.level
);
397 return info
->dst
.res
->format
== info
->src
.res
->format
&&
398 dst_width
== info
->src
.res
->width0
&&
399 dst_height
== info
->src
.res
->height0
&&
402 info
->dst
.x1
== dst_width
&&
403 info
->dst
.y1
== dst_height
&&
406 info
->src
.x1
== dst_width
&&
407 info
->src
.y1
== dst_height
;
410 static void r600_color_resolve(struct pipe_context
*ctx
,
411 const struct pipe_resolve_info
*info
)
413 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
414 struct pipe_screen
*screen
= ctx
->screen
;
415 struct pipe_resource
*tmp
, templ
;
417 unsigned sample_mask
=
418 rctx
->chip_class
== CAYMAN
? ~0 : ((1ull << MAX2(1, info
->src
.res
->nr_samples
)) - 1);
420 assert((info
->mask
& PIPE_MASK_RGBA
) == PIPE_MASK_RGBA
);
422 if (is_simple_resolve(info
)) {
423 r600_blitter_begin(ctx
, R600_COLOR_RESOLVE
);
424 util_blitter_custom_resolve_color(rctx
->blitter
,
425 info
->dst
.res
, info
->dst
.level
, info
->dst
.layer
,
426 info
->src
.res
, info
->src
.layer
,
427 sample_mask
, rctx
->custom_blend_resolve
);
428 r600_blitter_end(ctx
);
432 /* resolve into a temporary texture, then blit */
433 templ
.target
= PIPE_TEXTURE_2D
;
434 templ
.format
= info
->src
.res
->format
;
435 templ
.width0
= info
->src
.res
->width0
;
436 templ
.height0
= info
->src
.res
->height0
;
438 templ
.array_size
= 1;
439 templ
.last_level
= 0;
440 templ
.nr_samples
= 0;
441 templ
.usage
= PIPE_USAGE_STATIC
;
442 templ
.bind
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
445 tmp
= screen
->resource_create(screen
, &templ
);
447 /* XXX use scissor, so that only the needed part of the resource is resolved */
448 r600_blitter_begin(ctx
, R600_COLOR_RESOLVE
);
449 util_blitter_custom_resolve_color(rctx
->blitter
,
451 info
->src
.res
, info
->src
.layer
,
452 sample_mask
, rctx
->custom_blend_resolve
);
453 r600_blitter_end(ctx
);
455 /* this is correct for upside-down blits too */
456 u_box_2d(info
->src
.x0
,
458 info
->src
.x1
- info
->src
.x0
,
459 info
->src
.y1
- info
->src
.y0
, &box
);
461 r600_blitter_begin(ctx
, R600_COPY_TEXTURE
);
462 util_blitter_copy_texture(rctx
->blitter
, info
->dst
.res
, info
->dst
.level
,
463 ~0, info
->dst
.x0
, info
->dst
.y0
, info
->dst
.layer
,
465 r600_blitter_end(ctx
);
467 pipe_resource_reference(&tmp
, NULL
);
470 static void r600_resource_resolve(struct pipe_context
*ctx
,
471 const struct pipe_resolve_info
*info
)
473 /* make sure we're doing a resolve operation */
474 assert(info
->src
.res
->nr_samples
> 1);
475 assert(info
->dst
.res
->nr_samples
<= 1);
477 /* limitations of multisample resources */
478 assert(info
->src
.res
->last_level
== 0);
479 assert(info
->src
.res
->target
== PIPE_TEXTURE_2D
||
480 info
->src
.res
->target
== PIPE_TEXTURE_2D_ARRAY
);
482 /* check if the resolve box is valid */
483 assert(info
->dst
.x0
< info
->dst
.x1
);
484 assert(info
->dst
.y0
< info
->dst
.y1
);
486 /* scaled resolve isn't allowed */
487 assert(abs(info
->dst
.x0
- info
->dst
.x1
) ==
488 abs(info
->src
.x0
- info
->src
.x1
));
489 assert(abs(info
->dst
.y0
- info
->dst
.y1
) ==
490 abs(info
->src
.y0
- info
->src
.y1
));
492 if ((info
->mask
& PIPE_MASK_ZS
) ||
493 util_format_is_pure_integer(info
->src
.res
->format
)) {
494 r600_copy_first_sample(ctx
, info
);
496 r600_color_resolve(ctx
, info
);
500 static void r600_clear(struct pipe_context
*ctx
, unsigned buffers
,
501 const union pipe_color_union
*color
,
502 double depth
, unsigned stencil
)
504 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
505 struct pipe_framebuffer_state
*fb
= &rctx
->framebuffer
.state
;
507 r600_blitter_begin(ctx
, R600_CLEAR
);
508 util_blitter_clear(rctx
->blitter
, fb
->width
, fb
->height
,
509 fb
->nr_cbufs
, buffers
, fb
->nr_cbufs
? fb
->cbufs
[0]->format
: PIPE_FORMAT_NONE
,
510 color
, depth
, stencil
);
511 r600_blitter_end(ctx
);
514 static void r600_clear_render_target(struct pipe_context
*ctx
,
515 struct pipe_surface
*dst
,
516 const union pipe_color_union
*color
,
517 unsigned dstx
, unsigned dsty
,
518 unsigned width
, unsigned height
)
520 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
522 r600_blitter_begin(ctx
, R600_CLEAR_SURFACE
);
523 util_blitter_clear_render_target(rctx
->blitter
, dst
, color
,
524 dstx
, dsty
, width
, height
);
525 r600_blitter_end(ctx
);
528 static void r600_clear_depth_stencil(struct pipe_context
*ctx
,
529 struct pipe_surface
*dst
,
530 unsigned clear_flags
,
533 unsigned dstx
, unsigned dsty
,
534 unsigned width
, unsigned height
)
536 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
538 r600_blitter_begin(ctx
, R600_CLEAR_SURFACE
);
539 util_blitter_clear_depth_stencil(rctx
->blitter
, dst
, clear_flags
, depth
, stencil
,
540 dstx
, dsty
, width
, height
);
541 r600_blitter_end(ctx
);
544 void r600_copy_buffer(struct pipe_context
*ctx
, struct
545 pipe_resource
*dst
, unsigned dstx
,
546 struct pipe_resource
*src
, const struct pipe_box
*src_box
)
548 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
550 if (rctx
->screen
->has_streamout
&&
551 /* Require dword alignment. */
552 dstx
% 4 == 0 && src_box
->x
% 4 == 0 && src_box
->width
% 4 == 0) {
553 r600_blitter_begin(ctx
, R600_COPY_BUFFER
);
554 util_blitter_copy_buffer(rctx
->blitter
, dst
, dstx
, src
, src_box
->x
, src_box
->width
);
555 r600_blitter_end(ctx
);
557 util_resource_copy_region(ctx
, dst
, 0, dstx
, 0, 0, src
, 0, src_box
);
561 struct texture_orig_info
{
571 static void r600_compressed_to_blittable(struct pipe_resource
*tex
,
573 struct texture_orig_info
*orig
)
575 struct r600_texture
*rtex
= (struct r600_texture
*)tex
;
576 unsigned pixsize
= util_format_get_blocksize(rtex
->resource
.b
.b
.format
);
578 int new_height
, new_width
;
580 orig
->format
= tex
->format
;
581 orig
->width0
= tex
->width0
;
582 orig
->height0
= tex
->height0
;
583 orig
->npix0_x
= rtex
->surface
.level
[0].npix_x
;
584 orig
->npix0_y
= rtex
->surface
.level
[0].npix_y
;
585 orig
->npix_x
= rtex
->surface
.level
[level
].npix_x
;
586 orig
->npix_y
= rtex
->surface
.level
[level
].npix_y
;
589 new_format
= PIPE_FORMAT_R16G16B16A16_UINT
; /* 64-bit block */
591 new_format
= PIPE_FORMAT_R32G32B32A32_UINT
; /* 128-bit block */
593 new_width
= util_format_get_nblocksx(tex
->format
, orig
->width0
);
594 new_height
= util_format_get_nblocksy(tex
->format
, orig
->height0
);
596 tex
->width0
= new_width
;
597 tex
->height0
= new_height
;
598 tex
->format
= new_format
;
599 rtex
->surface
.level
[0].npix_x
= util_format_get_nblocksx(orig
->format
, orig
->npix0_x
);
600 rtex
->surface
.level
[0].npix_y
= util_format_get_nblocksy(orig
->format
, orig
->npix0_y
);
601 rtex
->surface
.level
[level
].npix_x
= util_format_get_nblocksx(orig
->format
, orig
->npix_x
);
602 rtex
->surface
.level
[level
].npix_y
= util_format_get_nblocksy(orig
->format
, orig
->npix_y
);
605 static void r600_subsampled_2x1_32bpp_to_blittable(struct pipe_resource
*tex
,
607 struct texture_orig_info
*orig
)
609 struct r600_texture
*rtex
= (struct r600_texture
*)tex
;
611 orig
->format
= tex
->format
;
612 orig
->width0
= tex
->width0
;
613 orig
->height0
= tex
->height0
;
614 orig
->npix0_x
= rtex
->surface
.level
[0].npix_x
;
615 orig
->npix0_y
= rtex
->surface
.level
[0].npix_y
;
616 orig
->npix_x
= rtex
->surface
.level
[level
].npix_x
;
617 orig
->npix_y
= rtex
->surface
.level
[level
].npix_y
;
619 tex
->width0
= util_format_get_nblocksx(orig
->format
, orig
->width0
);
620 tex
->format
= PIPE_FORMAT_R8G8B8A8_UINT
;
621 rtex
->surface
.level
[0].npix_x
= util_format_get_nblocksx(orig
->format
, orig
->npix0_x
);
622 rtex
->surface
.level
[level
].npix_x
= util_format_get_nblocksx(orig
->format
, orig
->npix_x
);
625 static void r600_change_format(struct pipe_resource
*tex
,
627 struct texture_orig_info
*orig
,
628 enum pipe_format format
)
630 struct r600_texture
*rtex
= (struct r600_texture
*)tex
;
632 orig
->format
= tex
->format
;
633 orig
->width0
= tex
->width0
;
634 orig
->height0
= tex
->height0
;
635 orig
->npix0_x
= rtex
->surface
.level
[0].npix_x
;
636 orig
->npix0_y
= rtex
->surface
.level
[0].npix_y
;
637 orig
->npix_x
= rtex
->surface
.level
[level
].npix_x
;
638 orig
->npix_y
= rtex
->surface
.level
[level
].npix_y
;
640 tex
->format
= format
;
643 static void r600_reset_blittable_to_orig(struct pipe_resource
*tex
,
645 struct texture_orig_info
*orig
)
647 struct r600_texture
*rtex
= (struct r600_texture
*)tex
;
649 tex
->format
= orig
->format
;
650 tex
->width0
= orig
->width0
;
651 tex
->height0
= orig
->height0
;
652 rtex
->surface
.level
[0].npix_x
= orig
->npix0_x
;
653 rtex
->surface
.level
[0].npix_y
= orig
->npix0_y
;
654 rtex
->surface
.level
[level
].npix_x
= orig
->npix_x
;
655 rtex
->surface
.level
[level
].npix_y
= orig
->npix_y
;
658 static bool util_format_is_subsampled_2x1_32bpp(enum pipe_format format
)
660 const struct util_format_description
*desc
= util_format_description(format
);
662 return desc
->layout
== UTIL_FORMAT_LAYOUT_SUBSAMPLED
&&
663 desc
->block
.width
== 2 &&
664 desc
->block
.height
== 1 &&
665 desc
->block
.bits
== 32;
668 static void r600_resource_copy_region(struct pipe_context
*ctx
,
669 struct pipe_resource
*dst
,
671 unsigned dstx
, unsigned dsty
, unsigned dstz
,
672 struct pipe_resource
*src
,
674 const struct pipe_box
*src_box
)
676 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
677 struct texture_orig_info orig_info
[2];
678 struct pipe_box sbox
;
679 const struct pipe_box
*psbox
= src_box
;
680 boolean restore_orig
[2];
681 unsigned last_sample
, i
;
683 memset(orig_info
, 0, sizeof(orig_info
));
685 /* Handle buffers first. */
686 if (dst
->target
== PIPE_BUFFER
&& src
->target
== PIPE_BUFFER
) {
687 r600_copy_buffer(ctx
, dst
, dstx
, src
, src_box
);
691 assert(u_max_sample(dst
) == u_max_sample(src
));
692 last_sample
= u_max_sample(dst
);
694 /* The driver doesn't decompress resources automatically while
695 * u_blitter is rendering. */
696 if (!r600_decompress_subresource(ctx
, src
, src_level
,
697 src_box
->z
, src_box
->z
+ src_box
->depth
- 1)) {
701 restore_orig
[0] = restore_orig
[1] = FALSE
;
703 if (util_format_is_compressed(src
->format
) &&
704 util_format_is_compressed(dst
->format
)) {
705 r600_compressed_to_blittable(src
, src_level
, &orig_info
[0]);
706 restore_orig
[0] = TRUE
;
707 sbox
.x
= util_format_get_nblocksx(orig_info
[0].format
, src_box
->x
);
708 sbox
.y
= util_format_get_nblocksy(orig_info
[0].format
, src_box
->y
);
710 sbox
.width
= util_format_get_nblocksx(orig_info
[0].format
, src_box
->width
);
711 sbox
.height
= util_format_get_nblocksy(orig_info
[0].format
, src_box
->height
);
712 sbox
.depth
= src_box
->depth
;
715 r600_compressed_to_blittable(dst
, dst_level
, &orig_info
[1]);
716 restore_orig
[1] = TRUE
;
717 /* translate the dst box as well */
718 dstx
= util_format_get_nblocksx(orig_info
[1].format
, dstx
);
719 dsty
= util_format_get_nblocksy(orig_info
[1].format
, dsty
);
720 } else if (!util_blitter_is_copy_supported(rctx
->blitter
, dst
, src
,
722 if (util_format_is_subsampled_2x1_32bpp(src
->format
) &&
723 util_format_is_subsampled_2x1_32bpp(dst
->format
)) {
724 r600_subsampled_2x1_32bpp_to_blittable(src
, src_level
, &orig_info
[0]);
725 r600_subsampled_2x1_32bpp_to_blittable(dst
, dst_level
, &orig_info
[1]);
728 sbox
.x
= util_format_get_nblocksx(orig_info
[0].format
, src_box
->x
);
729 sbox
.width
= util_format_get_nblocksx(orig_info
[0].format
, src_box
->width
);
732 dstx
= util_format_get_nblocksx(orig_info
[1].format
, dstx
);
734 unsigned blocksize
= util_format_get_blocksize(src
->format
);
738 r600_change_format(src
, src_level
, &orig_info
[0],
739 PIPE_FORMAT_R8_UNORM
);
740 r600_change_format(dst
, dst_level
, &orig_info
[1],
741 PIPE_FORMAT_R8_UNORM
);
744 r600_change_format(src
, src_level
, &orig_info
[0],
745 PIPE_FORMAT_R8G8B8A8_UNORM
);
746 r600_change_format(dst
, dst_level
, &orig_info
[1],
747 PIPE_FORMAT_R8G8B8A8_UNORM
);
750 fprintf(stderr
, "Unhandled format %s with blocksize %u\n",
751 util_format_short_name(src
->format
), blocksize
);
755 restore_orig
[0] = TRUE
;
756 restore_orig
[1] = TRUE
;
759 /* XXX Properly implement multisample textures on Cayman. In the meantime,
760 * copy only the first sample (which is the only one that doesn't return garbage). */
761 if (rctx
->chip_class
== CAYMAN
) {
762 r600_blitter_begin(ctx
, R600_COPY_TEXTURE
);
763 util_blitter_copy_texture(rctx
->blitter
, dst
, dst_level
, ~0, dstx
, dsty
, dstz
,
764 src
, src_level
, 0, psbox
);
765 r600_blitter_end(ctx
);
767 for (i
= 0; i
<= last_sample
; i
++) {
768 r600_blitter_begin(ctx
, R600_COPY_TEXTURE
);
769 util_blitter_copy_texture(rctx
->blitter
, dst
, dst_level
, 1 << i
, dstx
, dsty
, dstz
,
770 src
, src_level
, i
, psbox
);
771 r600_blitter_end(ctx
);
776 r600_reset_blittable_to_orig(src
, src_level
, &orig_info
[0]);
779 r600_reset_blittable_to_orig(dst
, dst_level
, &orig_info
[1]);
782 void r600_init_blit_functions(struct r600_context
*rctx
)
784 rctx
->context
.clear
= r600_clear
;
785 rctx
->context
.clear_render_target
= r600_clear_render_target
;
786 rctx
->context
.clear_depth_stencil
= r600_clear_depth_stencil
;
787 rctx
->context
.resource_copy_region
= r600_resource_copy_region
;
788 rctx
->context
.resource_resolve
= r600_resource_resolve
;