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_TEXTURES
= 1,
31 R600_SAVE_FRAMEBUFFER
= 2,
32 R600_DISABLE_RENDER_COND
= 4,
36 R600_CLEAR_SURFACE
= R600_SAVE_FRAMEBUFFER
,
38 R600_COPY
= R600_SAVE_FRAMEBUFFER
| R600_SAVE_TEXTURES
|
39 R600_DISABLE_RENDER_COND
,
41 R600_DECOMPRESS
= R600_SAVE_FRAMEBUFFER
| R600_DISABLE_RENDER_COND
,
44 static void r600_blitter_begin(struct pipe_context
*ctx
, enum r600_blitter_op op
)
46 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
48 r600_suspend_nontimer_queries(rctx
);
50 util_blitter_save_blend(rctx
->blitter
, rctx
->states
[R600_PIPE_STATE_BLEND
]);
51 util_blitter_save_depth_stencil_alpha(rctx
->blitter
, rctx
->states
[R600_PIPE_STATE_DSA
]);
52 if (rctx
->states
[R600_PIPE_STATE_STENCIL_REF
]) {
53 util_blitter_save_stencil_ref(rctx
->blitter
, &rctx
->stencil_ref
);
55 util_blitter_save_rasterizer(rctx
->blitter
, rctx
->states
[R600_PIPE_STATE_RASTERIZER
]);
56 util_blitter_save_fragment_shader(rctx
->blitter
, rctx
->ps_shader
);
57 util_blitter_save_vertex_shader(rctx
->blitter
, rctx
->vs_shader
);
58 util_blitter_save_vertex_elements(rctx
->blitter
, rctx
->vertex_elements
);
59 if (rctx
->states
[R600_PIPE_STATE_VIEWPORT
]) {
60 util_blitter_save_viewport(rctx
->blitter
, &rctx
->viewport
);
62 util_blitter_save_vertex_buffers(rctx
->blitter
,
63 rctx
->nr_vertex_buffers
,
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_mode
= rctx
->current_render_cond_mode
;
84 rctx
->context
.render_condition(&rctx
->context
, NULL
, 0);
89 static void r600_blitter_end(struct pipe_context
*ctx
)
91 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
92 if (rctx
->saved_render_cond
) {
93 rctx
->context
.render_condition(&rctx
->context
,
94 rctx
->saved_render_cond
,
95 rctx
->saved_render_cond_mode
);
96 rctx
->saved_render_cond
= NULL
;
98 r600_resume_nontimer_queries(rctx
);
101 static unsigned u_max_layer(struct pipe_resource
*r
, unsigned level
)
104 case PIPE_TEXTURE_CUBE
:
106 case PIPE_TEXTURE_3D
:
107 return u_minify(r
->depth0
, level
) - 1;
108 case PIPE_TEXTURE_1D_ARRAY
:
109 case PIPE_TEXTURE_2D_ARRAY
:
110 return r
->array_size
- 1;
116 void r600_blit_uncompress_depth(struct pipe_context
*ctx
,
117 struct r600_resource_texture
*texture
,
118 struct r600_resource_texture
*staging
,
119 unsigned first_level
, unsigned last_level
,
120 unsigned first_layer
, unsigned last_layer
)
122 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
123 unsigned layer
, level
, checked_last_layer
, max_layer
;
125 struct r600_resource_texture
*flushed_depth_texture
= staging
?
126 staging
: texture
->flushed_depth_texture
;
128 if (!staging
&& !texture
->dirty_db_mask
)
131 if (rctx
->family
== CHIP_RV610
|| rctx
->family
== CHIP_RV630
||
132 rctx
->family
== CHIP_RV620
|| rctx
->family
== CHIP_RV635
)
135 if (rctx
->chip_class
<= R700
&&
136 !rctx
->db_misc_state
.flush_depthstencil_through_cb
) {
137 /* Enable decompression in DB_RENDER_CONTROL */
138 rctx
->db_misc_state
.flush_depthstencil_through_cb
= true;
139 r600_atom_dirty(rctx
, &rctx
->db_misc_state
.atom
);
142 for (level
= first_level
; level
<= last_level
; level
++) {
143 if (!staging
&& !(texture
->dirty_db_mask
& (1 << level
)))
146 /* The smaller the mipmap level, the less layers there are
147 * as far as 3D textures are concerned. */
148 max_layer
= u_max_layer(&texture
->resource
.b
.b
, level
);
149 checked_last_layer
= last_layer
< max_layer
? last_layer
: max_layer
;
151 for (layer
= first_layer
; layer
<= checked_last_layer
; layer
++) {
152 struct pipe_surface
*zsurf
, *cbsurf
, surf_tmpl
;
154 surf_tmpl
.format
= texture
->real_format
;
155 surf_tmpl
.u
.tex
.level
= level
;
156 surf_tmpl
.u
.tex
.first_layer
= layer
;
157 surf_tmpl
.u
.tex
.last_layer
= layer
;
158 surf_tmpl
.usage
= PIPE_BIND_DEPTH_STENCIL
;
160 zsurf
= ctx
->create_surface(ctx
, &texture
->resource
.b
.b
, &surf_tmpl
);
162 surf_tmpl
.format
= flushed_depth_texture
->real_format
;
163 surf_tmpl
.usage
= PIPE_BIND_RENDER_TARGET
;
164 cbsurf
= ctx
->create_surface(ctx
,
165 (struct pipe_resource
*)flushed_depth_texture
, &surf_tmpl
);
167 r600_blitter_begin(ctx
, R600_DECOMPRESS
);
168 util_blitter_custom_depth_stencil(rctx
->blitter
, zsurf
, cbsurf
, rctx
->custom_dsa_flush
, depth
);
169 r600_blitter_end(ctx
);
171 pipe_surface_reference(&zsurf
, NULL
);
172 pipe_surface_reference(&cbsurf
, NULL
);
175 /* The texture will always be dirty if some layers aren't flushed.
176 * I don't think this case can occur though. */
177 if (!staging
&& first_layer
== 0 && last_layer
== max_layer
) {
178 texture
->dirty_db_mask
&= ~(1 << level
);
182 if (rctx
->chip_class
<= R700
) {
183 /* Disable decompression in DB_RENDER_CONTROL */
184 rctx
->db_misc_state
.flush_depthstencil_through_cb
= false;
185 r600_atom_dirty(rctx
, &rctx
->db_misc_state
.atom
);
189 void r600_flush_depth_textures(struct r600_context
*rctx
)
193 /* XXX: This handles fragment shader textures only. */
195 for (i
= 0; i
< rctx
->ps_samplers
.n_views
; ++i
) {
196 struct pipe_sampler_view
*view
;
197 struct r600_resource_texture
*tex
;
199 view
= &rctx
->ps_samplers
.views
[i
]->base
;
202 tex
= (struct r600_resource_texture
*)view
->texture
;
206 if (tex
->is_flushing_texture
)
209 r600_blit_uncompress_depth(&rctx
->context
, tex
, NULL
,
210 view
->u
.tex
.first_level
,
211 view
->u
.tex
.last_level
,
213 u_max_layer(&tex
->resource
.b
.b
, view
->u
.tex
.first_level
));
216 /* also check CB here */
217 for (i
= 0; i
< rctx
->framebuffer
.nr_cbufs
; i
++) {
218 struct r600_resource_texture
*tex
;
219 struct pipe_surface
*surf
= rctx
->framebuffer
.cbufs
[i
];
220 tex
= (struct r600_resource_texture
*)surf
->texture
;
225 if (tex
->is_flushing_texture
)
228 r600_blit_uncompress_depth(&rctx
->context
, tex
, NULL
,
231 surf
->u
.tex
.first_layer
,
232 surf
->u
.tex
.last_layer
);
236 static void r600_clear(struct pipe_context
*ctx
, unsigned buffers
,
237 const union pipe_color_union
*color
,
238 double depth
, unsigned stencil
)
240 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
241 struct pipe_framebuffer_state
*fb
= &rctx
->framebuffer
;
243 r600_blitter_begin(ctx
, R600_CLEAR
);
244 util_blitter_clear(rctx
->blitter
, fb
->width
, fb
->height
,
245 fb
->nr_cbufs
, buffers
, fb
->nr_cbufs
? fb
->cbufs
[0]->format
: PIPE_FORMAT_NONE
,
246 color
, depth
, stencil
);
247 r600_blitter_end(ctx
);
250 static void r600_clear_render_target(struct pipe_context
*ctx
,
251 struct pipe_surface
*dst
,
252 const union pipe_color_union
*color
,
253 unsigned dstx
, unsigned dsty
,
254 unsigned width
, unsigned height
)
256 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
258 r600_blitter_begin(ctx
, R600_CLEAR_SURFACE
);
259 util_blitter_clear_render_target(rctx
->blitter
, dst
, color
,
260 dstx
, dsty
, width
, height
);
261 r600_blitter_end(ctx
);
264 static void r600_clear_depth_stencil(struct pipe_context
*ctx
,
265 struct pipe_surface
*dst
,
266 unsigned clear_flags
,
269 unsigned dstx
, unsigned dsty
,
270 unsigned width
, unsigned height
)
272 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
274 r600_blitter_begin(ctx
, R600_CLEAR_SURFACE
);
275 util_blitter_clear_depth_stencil(rctx
->blitter
, dst
, clear_flags
, depth
, stencil
,
276 dstx
, dsty
, width
, height
);
277 r600_blitter_end(ctx
);
280 struct texture_orig_info
{
290 static void r600_compressed_to_blittable(struct pipe_resource
*tex
,
292 struct texture_orig_info
*orig
)
294 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)tex
;
295 unsigned pixsize
= util_format_get_blocksize(rtex
->real_format
);
297 int new_height
, new_width
;
299 orig
->format
= tex
->format
;
300 orig
->width0
= tex
->width0
;
301 orig
->height0
= tex
->height0
;
302 orig
->npix0_x
= rtex
->surface
.level
[0].npix_x
;
303 orig
->npix0_y
= rtex
->surface
.level
[0].npix_y
;
304 orig
->npix_x
= rtex
->surface
.level
[level
].npix_x
;
305 orig
->npix_y
= rtex
->surface
.level
[level
].npix_y
;
308 new_format
= PIPE_FORMAT_R16G16B16A16_UINT
; /* 64-bit block */
310 new_format
= PIPE_FORMAT_R32G32B32A32_UINT
; /* 128-bit block */
312 new_width
= util_format_get_nblocksx(tex
->format
, orig
->width0
);
313 new_height
= util_format_get_nblocksy(tex
->format
, orig
->height0
);
315 tex
->width0
= new_width
;
316 tex
->height0
= new_height
;
317 tex
->format
= new_format
;
318 rtex
->surface
.level
[0].npix_x
= util_format_get_nblocksx(orig
->format
, orig
->npix0_x
);
319 rtex
->surface
.level
[0].npix_y
= util_format_get_nblocksy(orig
->format
, orig
->npix0_y
);
320 rtex
->surface
.level
[level
].npix_x
= util_format_get_nblocksx(orig
->format
, orig
->npix_x
);
321 rtex
->surface
.level
[level
].npix_y
= util_format_get_nblocksy(orig
->format
, orig
->npix_y
);
324 static void r600_reset_blittable_to_compressed(struct pipe_resource
*tex
,
326 struct texture_orig_info
*orig
)
328 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)tex
;
330 tex
->format
= orig
->format
;
331 tex
->width0
= orig
->width0
;
332 tex
->height0
= orig
->height0
;
333 rtex
->surface
.level
[0].npix_x
= orig
->npix0_x
;
334 rtex
->surface
.level
[0].npix_y
= orig
->npix0_y
;
335 rtex
->surface
.level
[level
].npix_x
= orig
->npix_x
;
336 rtex
->surface
.level
[level
].npix_y
= orig
->npix_y
;
339 static void r600_resource_copy_region(struct pipe_context
*ctx
,
340 struct pipe_resource
*dst
,
342 unsigned dstx
, unsigned dsty
, unsigned dstz
,
343 struct pipe_resource
*src
,
345 const struct pipe_box
*src_box
)
347 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
348 struct r600_resource_texture
*rsrc
= (struct r600_resource_texture
*)src
;
349 struct texture_orig_info orig_info
[2];
350 struct pipe_box sbox
;
351 const struct pipe_box
*psbox
;
352 boolean restore_orig
[2];
354 memset(orig_info
, 0, sizeof(orig_info
));
356 /* Fallback for buffers. */
357 if (dst
->target
== PIPE_BUFFER
&& src
->target
== PIPE_BUFFER
) {
358 util_resource_copy_region(ctx
, dst
, dst_level
, dstx
, dsty
, dstz
,
359 src
, src_level
, src_box
);
363 if (rsrc
->is_depth
&& !rsrc
->is_flushing_texture
)
364 r600_texture_depth_flush(ctx
, src
, NULL
,
365 src_level
, src_level
,
366 src_box
->z
, src_box
->z
+ src_box
->depth
- 1);
368 restore_orig
[0] = restore_orig
[1] = FALSE
;
370 if (util_format_is_compressed(src
->format
)) {
371 r600_compressed_to_blittable(src
, src_level
, &orig_info
[0]);
372 restore_orig
[0] = TRUE
;
373 sbox
.x
= util_format_get_nblocksx(orig_info
[0].format
, src_box
->x
);
374 sbox
.y
= util_format_get_nblocksy(orig_info
[0].format
, src_box
->y
);
376 sbox
.width
= util_format_get_nblocksx(orig_info
[0].format
, src_box
->width
);
377 sbox
.height
= util_format_get_nblocksy(orig_info
[0].format
, src_box
->height
);
378 sbox
.depth
= src_box
->depth
;
383 if (util_format_is_compressed(dst
->format
)) {
384 r600_compressed_to_blittable(dst
, dst_level
, &orig_info
[1]);
385 restore_orig
[1] = TRUE
;
386 /* translate the dst box as well */
387 dstx
= util_format_get_nblocksx(orig_info
[1].format
, dstx
);
388 dsty
= util_format_get_nblocksy(orig_info
[1].format
, dsty
);
391 r600_blitter_begin(ctx
, R600_COPY
);
392 util_blitter_copy_texture(rctx
->blitter
, dst
, dst_level
, dstx
, dsty
, dstz
,
393 src
, src_level
, psbox
, TRUE
);
394 r600_blitter_end(ctx
);
397 r600_reset_blittable_to_compressed(src
, src_level
, &orig_info
[0]);
400 r600_reset_blittable_to_compressed(dst
, dst_level
, &orig_info
[1]);
403 void r600_init_blit_functions(struct r600_context
*rctx
)
405 rctx
->context
.clear
= r600_clear
;
406 rctx
->context
.clear_render_target
= r600_clear_render_target
;
407 rctx
->context
.clear_depth_stencil
= r600_clear_depth_stencil
;
408 rctx
->context
.resource_copy_region
= r600_resource_copy_region
;