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 "r600_pipe.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_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
48 r600_context_queries_suspend(&rctx
->ctx
);
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
->vbuf_mgr
->nr_vertex_buffers
,
64 rctx
->vbuf_mgr
->vertex_buffer
);
65 util_blitter_save_so_targets(rctx
->blitter
, rctx
->ctx
.num_so_targets
,
66 (struct pipe_stream_output_target
**)rctx
->ctx
.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_pipe_context
*rctx
= (struct r600_pipe_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_context_queries_resume(&rctx
->ctx
);
101 static unsigned u_num_layers(struct pipe_resource
*r
, unsigned level
)
104 case PIPE_TEXTURE_CUBE
:
106 case PIPE_TEXTURE_3D
:
107 return u_minify(r
->depth0
, level
);
108 case PIPE_TEXTURE_1D_ARRAY
:
109 return r
->array_size
;
110 case PIPE_TEXTURE_2D_ARRAY
:
111 return r
->array_size
;
117 void r600_blit_uncompress_depth(struct pipe_context
*ctx
, struct r600_resource_texture
*texture
)
119 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
120 unsigned layer
, level
;
123 if (!texture
->dirty_db
)
126 if (rctx
->family
== CHIP_RV610
|| rctx
->family
== CHIP_RV630
||
127 rctx
->family
== CHIP_RV620
|| rctx
->family
== CHIP_RV635
)
130 for (level
= 0; level
<= texture
->resource
.b
.b
.b
.last_level
; level
++) {
131 unsigned num_layers
= u_num_layers(&texture
->resource
.b
.b
.b
, level
);
133 for (layer
= 0; layer
< num_layers
; layer
++) {
134 struct pipe_surface
*zsurf
, *cbsurf
, surf_tmpl
;
136 surf_tmpl
.format
= texture
->real_format
;
137 surf_tmpl
.u
.tex
.level
= level
;
138 surf_tmpl
.u
.tex
.first_layer
= layer
;
139 surf_tmpl
.u
.tex
.last_layer
= layer
;
140 surf_tmpl
.usage
= PIPE_BIND_DEPTH_STENCIL
;
142 zsurf
= ctx
->create_surface(ctx
, &texture
->resource
.b
.b
.b
, &surf_tmpl
);
144 surf_tmpl
.format
= texture
->flushed_depth_texture
->real_format
;
145 surf_tmpl
.usage
= PIPE_BIND_RENDER_TARGET
;
146 cbsurf
= ctx
->create_surface(ctx
,
147 (struct pipe_resource
*)texture
->flushed_depth_texture
, &surf_tmpl
);
149 r600_blitter_begin(ctx
, R600_DECOMPRESS
);
150 util_blitter_custom_depth_stencil(rctx
->blitter
, zsurf
, cbsurf
, rctx
->custom_dsa_flush
, depth
);
151 r600_blitter_end(ctx
);
153 pipe_surface_reference(&zsurf
, NULL
);
154 pipe_surface_reference(&cbsurf
, NULL
);
158 texture
->dirty_db
= FALSE
;
161 void r600_flush_depth_textures(struct r600_pipe_context
*rctx
)
165 /* FIXME: This handles fragment shader textures only. */
167 for (i
= 0; i
< rctx
->ps_samplers
.n_views
; ++i
) {
168 struct r600_pipe_sampler_view
*view
;
169 struct r600_resource_texture
*tex
;
171 view
= rctx
->ps_samplers
.views
[i
];
174 tex
= (struct r600_resource_texture
*)view
->base
.texture
;
178 if (tex
->is_flushing_texture
)
181 r600_blit_uncompress_depth(&rctx
->context
, tex
);
184 /* also check CB here */
185 for (i
= 0; i
< rctx
->framebuffer
.nr_cbufs
; i
++) {
186 struct r600_resource_texture
*tex
;
187 tex
= (struct r600_resource_texture
*)rctx
->framebuffer
.cbufs
[i
]->texture
;
192 if (tex
->is_flushing_texture
)
195 r600_blit_uncompress_depth(&rctx
->context
, tex
);
199 static void r600_clear(struct pipe_context
*ctx
, unsigned buffers
,
200 const union pipe_color_union
*color
,
201 double depth
, unsigned stencil
)
203 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
204 struct pipe_framebuffer_state
*fb
= &rctx
->framebuffer
;
206 r600_blitter_begin(ctx
, R600_CLEAR
);
207 util_blitter_clear(rctx
->blitter
, fb
->width
, fb
->height
,
208 fb
->nr_cbufs
, buffers
, fb
->nr_cbufs
? fb
->cbufs
[0]->format
: PIPE_FORMAT_NONE
,
209 color
, depth
, stencil
);
210 r600_blitter_end(ctx
);
213 static void r600_clear_render_target(struct pipe_context
*ctx
,
214 struct pipe_surface
*dst
,
215 const union pipe_color_union
*color
,
216 unsigned dstx
, unsigned dsty
,
217 unsigned width
, unsigned height
)
219 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
221 r600_blitter_begin(ctx
, R600_CLEAR_SURFACE
);
222 util_blitter_clear_render_target(rctx
->blitter
, dst
, color
,
223 dstx
, dsty
, width
, height
);
224 r600_blitter_end(ctx
);
227 static void r600_clear_depth_stencil(struct pipe_context
*ctx
,
228 struct pipe_surface
*dst
,
229 unsigned clear_flags
,
232 unsigned dstx
, unsigned dsty
,
233 unsigned width
, unsigned height
)
235 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
237 r600_blitter_begin(ctx
, R600_CLEAR_SURFACE
);
238 util_blitter_clear_depth_stencil(rctx
->blitter
, dst
, clear_flags
, depth
, stencil
,
239 dstx
, dsty
, width
, height
);
240 r600_blitter_end(ctx
);
245 /* Copy a block of pixels from one surface to another using HW. */
246 static void r600_hw_copy_region(struct pipe_context
*ctx
,
247 struct pipe_resource
*dst
,
249 unsigned dstx
, unsigned dsty
, unsigned dstz
,
250 struct pipe_resource
*src
,
252 const struct pipe_box
*src_box
)
254 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
256 r600_blitter_begin(ctx
, R600_COPY
);
257 util_blitter_copy_texture(rctx
->blitter
, dst
, dst_level
, dstx
, dsty
, dstz
,
258 src
, src_level
, src_box
, TRUE
);
259 r600_blitter_end(ctx
);
262 struct texture_orig_info
{
268 static void r600_compressed_to_blittable(struct pipe_resource
*tex
,
270 struct texture_orig_info
*orig
)
272 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)tex
;
273 unsigned pixsize
= util_format_get_blocksize(rtex
->real_format
);
275 int new_height
, new_width
;
277 orig
->format
= tex
->format
;
278 orig
->width0
= tex
->width0
;
279 orig
->height0
= tex
->height0
;
282 new_format
= PIPE_FORMAT_R16G16B16A16_UINT
; /* 64-bit block */
284 new_format
= PIPE_FORMAT_R32G32B32A32_UINT
; /* 128-bit block */
286 new_width
= util_format_get_nblocksx(tex
->format
, orig
->width0
);
287 new_height
= util_format_get_nblocksy(tex
->format
, orig
->height0
);
289 tex
->width0
= new_width
;
290 tex
->height0
= new_height
;
291 tex
->format
= new_format
;
294 static void r600_reset_blittable_to_compressed(struct pipe_resource
*tex
,
295 struct texture_orig_info
*orig
)
297 tex
->format
= orig
->format
;
298 tex
->width0
= orig
->width0
;
299 tex
->height0
= orig
->height0
;
302 static void r600_resource_copy_region(struct pipe_context
*ctx
,
303 struct pipe_resource
*dst
,
305 unsigned dstx
, unsigned dsty
, unsigned dstz
,
306 struct pipe_resource
*src
,
308 const struct pipe_box
*src_box
)
310 struct r600_resource_texture
*rsrc
= (struct r600_resource_texture
*)src
;
311 struct texture_orig_info orig_info
[2];
312 struct pipe_box sbox
;
313 const struct pipe_box
*psbox
;
314 boolean restore_orig
[2];
316 memset(orig_info
, 0, sizeof(orig_info
));
318 /* Fallback for buffers. */
319 if (dst
->target
== PIPE_BUFFER
&& src
->target
== PIPE_BUFFER
) {
320 util_resource_copy_region(ctx
, dst
, dst_level
, dstx
, dsty
, dstz
,
321 src
, src_level
, src_box
);
325 if (rsrc
->depth
&& !rsrc
->is_flushing_texture
)
326 r600_texture_depth_flush(ctx
, src
, FALSE
);
328 restore_orig
[0] = restore_orig
[1] = FALSE
;
330 if (util_format_is_compressed(src
->format
)) {
331 r600_compressed_to_blittable(src
, src_level
, &orig_info
[0]);
332 restore_orig
[0] = TRUE
;
333 sbox
.x
= util_format_get_nblocksx(orig_info
[0].format
, src_box
->x
);
334 sbox
.y
= util_format_get_nblocksy(orig_info
[0].format
, src_box
->y
);
336 sbox
.width
= util_format_get_nblocksx(orig_info
[0].format
, src_box
->width
);
337 sbox
.height
= util_format_get_nblocksy(orig_info
[0].format
, src_box
->height
);
338 sbox
.depth
= src_box
->depth
;
343 if (util_format_is_compressed(dst
->format
)) {
344 r600_compressed_to_blittable(dst
, dst_level
, &orig_info
[1]);
345 restore_orig
[1] = TRUE
;
346 /* translate the dst box as well */
347 dstx
= util_format_get_nblocksx(orig_info
[1].format
, dstx
);
348 dsty
= util_format_get_nblocksy(orig_info
[1].format
, dsty
);
351 r600_hw_copy_region(ctx
, dst
, dst_level
, dstx
, dsty
, dstz
,
352 src
, src_level
, psbox
);
355 r600_reset_blittable_to_compressed(src
, &orig_info
[0]);
358 r600_reset_blittable_to_compressed(dst
, &orig_info
[1]);
361 void r600_init_blit_functions(struct r600_pipe_context
*rctx
)
363 rctx
->context
.clear
= r600_clear
;
364 rctx
->context
.clear_render_target
= r600_clear_render_target
;
365 rctx
->context
.clear_depth_stencil
= r600_clear_depth_stencil
;
366 rctx
->context
.resource_copy_region
= r600_resource_copy_region
;
369 void r600_blit_push_depth(struct pipe_context
*ctx
, struct r600_resource_texture
*texture
)
371 struct pipe_box sbox
;
373 sbox
.x
= sbox
.y
= sbox
.z
= 0;
374 sbox
.width
= texture
->resource
.b
.b
.b
.width0
;
375 sbox
.height
= texture
->resource
.b
.b
.b
.height0
;
376 /* XXX that might be wrong */
379 r600_hw_copy_region(ctx
, (struct pipe_resource
*)texture
, 0,
381 (struct pipe_resource
*)texture
->flushed_depth_texture
, 0,