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
,
47 static void r600_blitter_begin(struct pipe_context
*ctx
, enum r600_blitter_op op
)
49 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
51 r600_suspend_nontimer_queries(rctx
);
53 util_blitter_save_vertex_buffers(rctx
->blitter
,
54 util_last_bit(rctx
->vertex_buffer_state
.enabled_mask
),
55 rctx
->vertex_buffer_state
.vb
);
56 util_blitter_save_vertex_elements(rctx
->blitter
, rctx
->vertex_elements
);
57 util_blitter_save_vertex_shader(rctx
->blitter
, rctx
->vs_shader
);
58 util_blitter_save_so_targets(rctx
->blitter
, rctx
->num_so_targets
,
59 (struct pipe_stream_output_target
**)rctx
->so_targets
);
60 util_blitter_save_rasterizer(rctx
->blitter
, rctx
->states
[R600_PIPE_STATE_RASTERIZER
]);
62 if (op
& R600_SAVE_FRAGMENT_STATE
) {
63 if (rctx
->states
[R600_PIPE_STATE_VIEWPORT
]) {
64 util_blitter_save_viewport(rctx
->blitter
, &rctx
->viewport
);
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 if (rctx
->states
[R600_PIPE_STATE_STENCIL_REF
]) {
70 util_blitter_save_stencil_ref(rctx
->blitter
, &rctx
->stencil_ref
);
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
);
78 if (op
& R600_SAVE_TEXTURES
) {
79 util_blitter_save_fragment_sampler_states(
80 rctx
->blitter
, rctx
->ps_samplers
.n_samplers
,
81 (void**)rctx
->ps_samplers
.samplers
);
83 util_blitter_save_fragment_sampler_views(
84 rctx
->blitter
, util_last_bit(rctx
->ps_samplers
.views
.enabled_mask
),
85 (struct pipe_sampler_view
**)rctx
->ps_samplers
.views
.views
);
88 if ((op
& R600_DISABLE_RENDER_COND
) && rctx
->current_render_cond
) {
89 rctx
->saved_render_cond
= rctx
->current_render_cond
;
90 rctx
->saved_render_cond_mode
= rctx
->current_render_cond_mode
;
91 rctx
->context
.render_condition(&rctx
->context
, NULL
, 0);
96 static void r600_blitter_end(struct pipe_context
*ctx
)
98 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
99 if (rctx
->saved_render_cond
) {
100 rctx
->context
.render_condition(&rctx
->context
,
101 rctx
->saved_render_cond
,
102 rctx
->saved_render_cond_mode
);
103 rctx
->saved_render_cond
= NULL
;
105 r600_resume_nontimer_queries(rctx
);
108 static unsigned u_max_layer(struct pipe_resource
*r
, unsigned level
)
111 case PIPE_TEXTURE_CUBE
:
113 case PIPE_TEXTURE_3D
:
114 return u_minify(r
->depth0
, level
) - 1;
115 case PIPE_TEXTURE_1D_ARRAY
:
116 case PIPE_TEXTURE_2D_ARRAY
:
117 return r
->array_size
- 1;
123 void r600_blit_uncompress_depth(struct pipe_context
*ctx
,
124 struct r600_resource_texture
*texture
,
125 struct r600_resource_texture
*staging
,
126 unsigned first_level
, unsigned last_level
,
127 unsigned first_layer
, unsigned last_layer
)
129 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
130 unsigned layer
, level
, checked_last_layer
, max_layer
;
132 struct r600_resource_texture
*flushed_depth_texture
= staging
?
133 staging
: texture
->flushed_depth_texture
;
134 const struct util_format_description
*desc
=
135 util_format_description(texture
->resource
.b
.b
.format
);
137 if (!staging
&& !texture
->dirty_db_mask
)
140 if (rctx
->family
== CHIP_RV610
|| rctx
->family
== CHIP_RV630
||
141 rctx
->family
== CHIP_RV620
|| rctx
->family
== CHIP_RV635
)
144 /* Enable decompression in DB_RENDER_CONTROL */
145 rctx
->db_misc_state
.flush_depthstencil_through_cb
= true;
146 rctx
->db_misc_state
.copy_depth
= util_format_has_depth(desc
);
147 rctx
->db_misc_state
.copy_stencil
= util_format_has_stencil(desc
);
148 r600_atom_dirty(rctx
, &rctx
->db_misc_state
.atom
);
150 for (level
= first_level
; level
<= last_level
; level
++) {
151 if (!staging
&& !(texture
->dirty_db_mask
& (1 << level
)))
154 /* The smaller the mipmap level, the less layers there are
155 * as far as 3D textures are concerned. */
156 max_layer
= u_max_layer(&texture
->resource
.b
.b
, level
);
157 checked_last_layer
= last_layer
< max_layer
? last_layer
: max_layer
;
159 for (layer
= first_layer
; layer
<= checked_last_layer
; layer
++) {
160 struct pipe_surface
*zsurf
, *cbsurf
, surf_tmpl
;
162 surf_tmpl
.format
= texture
->real_format
;
163 surf_tmpl
.u
.tex
.level
= level
;
164 surf_tmpl
.u
.tex
.first_layer
= layer
;
165 surf_tmpl
.u
.tex
.last_layer
= layer
;
166 surf_tmpl
.usage
= PIPE_BIND_DEPTH_STENCIL
;
168 zsurf
= ctx
->create_surface(ctx
, &texture
->resource
.b
.b
, &surf_tmpl
);
170 surf_tmpl
.format
= flushed_depth_texture
->real_format
;
171 surf_tmpl
.usage
= PIPE_BIND_RENDER_TARGET
;
172 cbsurf
= ctx
->create_surface(ctx
,
173 (struct pipe_resource
*)flushed_depth_texture
, &surf_tmpl
);
175 r600_blitter_begin(ctx
, R600_DECOMPRESS
);
176 util_blitter_custom_depth_stencil(rctx
->blitter
, zsurf
, cbsurf
, rctx
->custom_dsa_flush
, depth
);
177 r600_blitter_end(ctx
);
179 pipe_surface_reference(&zsurf
, NULL
);
180 pipe_surface_reference(&cbsurf
, NULL
);
183 /* The texture will always be dirty if some layers aren't flushed.
184 * I don't think this case can occur though. */
185 if (!staging
&& first_layer
== 0 && last_layer
== max_layer
) {
186 texture
->dirty_db_mask
&= ~(1 << level
);
190 /* reenable compression in DB_RENDER_CONTROL */
191 rctx
->db_misc_state
.flush_depthstencil_through_cb
= false;
192 r600_atom_dirty(rctx
, &rctx
->db_misc_state
.atom
);
195 void r600_flush_depth_textures(struct r600_context
*rctx
,
196 struct r600_samplerview_state
*textures
)
199 unsigned depth_texture_mask
= textures
->depth_texture_mask
;
201 while (depth_texture_mask
) {
202 struct pipe_sampler_view
*view
;
203 struct r600_resource_texture
*tex
;
205 i
= u_bit_scan(&depth_texture_mask
);
207 view
= &textures
->views
[i
]->base
;
210 tex
= (struct r600_resource_texture
*)view
->texture
;
211 assert(tex
->is_depth
&& !tex
->is_flushing_texture
);
213 r600_blit_uncompress_depth(&rctx
->context
, tex
, NULL
,
214 view
->u
.tex
.first_level
,
215 view
->u
.tex
.last_level
,
217 u_max_layer(&tex
->resource
.b
.b
, view
->u
.tex
.first_level
));
221 static void r600_clear(struct pipe_context
*ctx
, unsigned buffers
,
222 const union pipe_color_union
*color
,
223 double depth
, unsigned stencil
)
225 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
226 struct pipe_framebuffer_state
*fb
= &rctx
->framebuffer
;
228 r600_blitter_begin(ctx
, R600_CLEAR
);
229 util_blitter_clear(rctx
->blitter
, fb
->width
, fb
->height
,
230 fb
->nr_cbufs
, buffers
, fb
->nr_cbufs
? fb
->cbufs
[0]->format
: PIPE_FORMAT_NONE
,
231 color
, depth
, stencil
);
232 r600_blitter_end(ctx
);
235 static void r600_clear_render_target(struct pipe_context
*ctx
,
236 struct pipe_surface
*dst
,
237 const union pipe_color_union
*color
,
238 unsigned dstx
, unsigned dsty
,
239 unsigned width
, unsigned height
)
241 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
243 r600_blitter_begin(ctx
, R600_CLEAR_SURFACE
);
244 util_blitter_clear_render_target(rctx
->blitter
, dst
, color
,
245 dstx
, dsty
, width
, height
);
246 r600_blitter_end(ctx
);
249 static void r600_clear_depth_stencil(struct pipe_context
*ctx
,
250 struct pipe_surface
*dst
,
251 unsigned clear_flags
,
254 unsigned dstx
, unsigned dsty
,
255 unsigned width
, unsigned height
)
257 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
259 r600_blitter_begin(ctx
, R600_CLEAR_SURFACE
);
260 util_blitter_clear_depth_stencil(rctx
->blitter
, dst
, clear_flags
, depth
, stencil
,
261 dstx
, dsty
, width
, height
);
262 r600_blitter_end(ctx
);
265 void r600_copy_buffer(struct pipe_context
*ctx
, struct
266 pipe_resource
*dst
, unsigned dstx
,
267 struct pipe_resource
*src
, const struct pipe_box
*src_box
)
269 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
271 if (rctx
->screen
->has_streamout
&&
272 /* Require dword alignment. */
273 dstx
% 4 == 0 && src_box
->x
% 4 == 0 && src_box
->width
% 4 == 0) {
274 r600_blitter_begin(ctx
, R600_COPY_BUFFER
);
275 util_blitter_copy_buffer(rctx
->blitter
, dst
, dstx
, src
, src_box
->x
, src_box
->width
);
276 r600_blitter_end(ctx
);
278 util_resource_copy_region(ctx
, dst
, 0, dstx
, 0, 0, src
, 0, src_box
);
282 struct texture_orig_info
{
292 static void r600_compressed_to_blittable(struct pipe_resource
*tex
,
294 struct texture_orig_info
*orig
)
296 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)tex
;
297 unsigned pixsize
= util_format_get_blocksize(rtex
->real_format
);
299 int new_height
, new_width
;
301 orig
->format
= tex
->format
;
302 orig
->width0
= tex
->width0
;
303 orig
->height0
= tex
->height0
;
304 orig
->npix0_x
= rtex
->surface
.level
[0].npix_x
;
305 orig
->npix0_y
= rtex
->surface
.level
[0].npix_y
;
306 orig
->npix_x
= rtex
->surface
.level
[level
].npix_x
;
307 orig
->npix_y
= rtex
->surface
.level
[level
].npix_y
;
310 new_format
= PIPE_FORMAT_R16G16B16A16_UINT
; /* 64-bit block */
312 new_format
= PIPE_FORMAT_R32G32B32A32_UINT
; /* 128-bit block */
314 new_width
= util_format_get_nblocksx(tex
->format
, orig
->width0
);
315 new_height
= util_format_get_nblocksy(tex
->format
, orig
->height0
);
317 tex
->width0
= new_width
;
318 tex
->height0
= new_height
;
319 tex
->format
= new_format
;
320 rtex
->surface
.level
[0].npix_x
= util_format_get_nblocksx(orig
->format
, orig
->npix0_x
);
321 rtex
->surface
.level
[0].npix_y
= util_format_get_nblocksy(orig
->format
, orig
->npix0_y
);
322 rtex
->surface
.level
[level
].npix_x
= util_format_get_nblocksx(orig
->format
, orig
->npix_x
);
323 rtex
->surface
.level
[level
].npix_y
= util_format_get_nblocksy(orig
->format
, orig
->npix_y
);
326 static void r600_change_format(struct pipe_resource
*tex
,
328 struct texture_orig_info
*orig
,
329 enum pipe_format format
)
331 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)tex
;
333 orig
->format
= tex
->format
;
334 orig
->width0
= tex
->width0
;
335 orig
->height0
= tex
->height0
;
336 orig
->npix0_x
= rtex
->surface
.level
[0].npix_x
;
337 orig
->npix0_y
= rtex
->surface
.level
[0].npix_y
;
338 orig
->npix_x
= rtex
->surface
.level
[level
].npix_x
;
339 orig
->npix_y
= rtex
->surface
.level
[level
].npix_y
;
341 tex
->format
= format
;
344 static void r600_reset_blittable_to_orig(struct pipe_resource
*tex
,
346 struct texture_orig_info
*orig
)
348 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)tex
;
350 tex
->format
= orig
->format
;
351 tex
->width0
= orig
->width0
;
352 tex
->height0
= orig
->height0
;
353 rtex
->surface
.level
[0].npix_x
= orig
->npix0_x
;
354 rtex
->surface
.level
[0].npix_y
= orig
->npix0_y
;
355 rtex
->surface
.level
[level
].npix_x
= orig
->npix_x
;
356 rtex
->surface
.level
[level
].npix_y
= orig
->npix_y
;
359 static void r600_resource_copy_region(struct pipe_context
*ctx
,
360 struct pipe_resource
*dst
,
362 unsigned dstx
, unsigned dsty
, unsigned dstz
,
363 struct pipe_resource
*src
,
365 const struct pipe_box
*src_box
)
367 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
368 struct r600_resource_texture
*rsrc
= (struct r600_resource_texture
*)src
;
369 struct texture_orig_info orig_info
[2];
370 struct pipe_box sbox
;
371 const struct pipe_box
*psbox
= src_box
;
372 boolean restore_orig
[2];
374 memset(orig_info
, 0, sizeof(orig_info
));
376 /* Handle buffers first. */
377 if (dst
->target
== PIPE_BUFFER
&& src
->target
== PIPE_BUFFER
) {
378 r600_copy_buffer(ctx
, dst
, dstx
, src
, src_box
);
382 /* This must be done before entering u_blitter to avoid recursion. */
383 if (rsrc
->is_depth
&& !rsrc
->is_flushing_texture
) {
384 if (!r600_init_flushed_depth_texture(ctx
, src
, NULL
))
387 r600_blit_uncompress_depth(ctx
, rsrc
, NULL
,
388 src_level
, src_level
,
389 src_box
->z
, src_box
->z
+ src_box
->depth
- 1);
392 restore_orig
[0] = restore_orig
[1] = FALSE
;
394 if (util_format_is_compressed(src
->format
) &&
395 util_format_is_compressed(dst
->format
)) {
396 r600_compressed_to_blittable(src
, src_level
, &orig_info
[0]);
397 restore_orig
[0] = TRUE
;
398 sbox
.x
= util_format_get_nblocksx(orig_info
[0].format
, src_box
->x
);
399 sbox
.y
= util_format_get_nblocksy(orig_info
[0].format
, src_box
->y
);
401 sbox
.width
= util_format_get_nblocksx(orig_info
[0].format
, src_box
->width
);
402 sbox
.height
= util_format_get_nblocksy(orig_info
[0].format
, src_box
->height
);
403 sbox
.depth
= src_box
->depth
;
406 r600_compressed_to_blittable(dst
, dst_level
, &orig_info
[1]);
407 restore_orig
[1] = TRUE
;
408 /* translate the dst box as well */
409 dstx
= util_format_get_nblocksx(orig_info
[1].format
, dstx
);
410 dsty
= util_format_get_nblocksy(orig_info
[1].format
, dsty
);
411 } else if (!util_blitter_is_copy_supported(rctx
->blitter
, dst
, src
,
413 unsigned blocksize
= util_format_get_blocksize(src
->format
);
417 r600_change_format(src
, src_level
, &orig_info
[0],
418 PIPE_FORMAT_R8_UNORM
);
419 r600_change_format(dst
, dst_level
, &orig_info
[1],
420 PIPE_FORMAT_R8_UNORM
);
423 r600_change_format(src
, src_level
, &orig_info
[0],
424 PIPE_FORMAT_R8G8B8A8_UNORM
);
425 r600_change_format(dst
, dst_level
, &orig_info
[1],
426 PIPE_FORMAT_R8G8B8A8_UNORM
);
429 fprintf(stderr
, "Unhandled format %s with blocksize %u\n",
430 util_format_short_name(src
->format
), blocksize
);
433 restore_orig
[0] = TRUE
;
434 restore_orig
[1] = TRUE
;
437 r600_blitter_begin(ctx
, R600_COPY_TEXTURE
);
438 util_blitter_copy_texture(rctx
->blitter
, dst
, dst_level
, ~0, dstx
, dsty
, dstz
,
439 src
, src_level
, 0, psbox
);
440 r600_blitter_end(ctx
);
443 r600_reset_blittable_to_orig(src
, src_level
, &orig_info
[0]);
446 r600_reset_blittable_to_orig(dst
, dst_level
, &orig_info
[1]);
449 void r600_init_blit_functions(struct r600_context
*rctx
)
451 rctx
->context
.clear
= r600_clear
;
452 rctx
->context
.clear_render_target
= r600_clear_render_target
;
453 rctx
->context
.clear_depth_stencil
= r600_clear_depth_stencil
;
454 rctx
->context
.resource_copy_region
= r600_resource_copy_region
;