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 */
31 R600_CLEAR_SURFACE
= 2,
35 static void r600_blitter_begin(struct pipe_context
*ctx
, enum r600_blitter_op op
)
37 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
40 r600_context_queries_suspend(&rctx
->ctx
);
42 util_blitter_save_blend(rctx
->blitter
, rctx
->states
[R600_PIPE_STATE_BLEND
]);
43 util_blitter_save_depth_stencil_alpha(rctx
->blitter
, rctx
->states
[R600_PIPE_STATE_DSA
]);
44 if (rctx
->states
[R600_PIPE_STATE_STENCIL_REF
]) {
45 util_blitter_save_stencil_ref(rctx
->blitter
, &rctx
->stencil_ref
);
47 util_blitter_save_rasterizer(rctx
->blitter
, rctx
->states
[R600_PIPE_STATE_RASTERIZER
]);
48 util_blitter_save_fragment_shader(rctx
->blitter
, rctx
->ps_shader
);
49 util_blitter_save_vertex_shader(rctx
->blitter
, rctx
->vs_shader
);
50 util_blitter_save_vertex_elements(rctx
->blitter
, rctx
->vertex_elements
);
51 if (rctx
->states
[R600_PIPE_STATE_VIEWPORT
]) {
52 util_blitter_save_viewport(rctx
->blitter
, &rctx
->viewport
);
54 if (rctx
->states
[R600_PIPE_STATE_CLIP
]) {
55 util_blitter_save_clip(rctx
->blitter
, &rctx
->clip
);
57 util_blitter_save_vertex_buffers(rctx
->blitter
,
58 rctx
->vbuf_mgr
->nr_vertex_buffers
,
59 rctx
->vbuf_mgr
->vertex_buffer
);
61 if (op
& (R600_CLEAR_SURFACE
| R600_COPY
))
62 util_blitter_save_framebuffer(rctx
->blitter
, &rctx
->framebuffer
);
65 util_blitter_save_fragment_sampler_states(
66 rctx
->blitter
, rctx
->ps_samplers
.n_samplers
,
67 (void**)rctx
->ps_samplers
.samplers
);
69 util_blitter_save_fragment_sampler_views(
70 rctx
->blitter
, rctx
->ps_samplers
.n_views
,
71 (struct pipe_sampler_view
**)rctx
->ps_samplers
.views
);
76 static void r600_blitter_end(struct pipe_context
*ctx
)
78 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
79 r600_context_queries_resume(&rctx
->ctx
);
83 void r600_blit_uncompress_depth(struct pipe_context
*ctx
, struct r600_resource_texture
*texture
)
85 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
86 struct pipe_surface
*zsurf
, *cbsurf
, surf_tmpl
;
90 if (!texture
->dirty_db
)
93 surf_tmpl
.format
= texture
->resource
.b
.b
.b
.format
;
94 surf_tmpl
.u
.tex
.level
= level
;
95 surf_tmpl
.u
.tex
.first_layer
= 0;
96 surf_tmpl
.u
.tex
.last_layer
= 0;
97 surf_tmpl
.usage
= PIPE_BIND_DEPTH_STENCIL
;
99 zsurf
= ctx
->create_surface(ctx
, &texture
->resource
.b
.b
.b
, &surf_tmpl
);
101 surf_tmpl
.format
= ((struct pipe_resource
*)texture
->flushed_depth_texture
)->format
;
102 surf_tmpl
.usage
= PIPE_BIND_RENDER_TARGET
;
103 cbsurf
= ctx
->create_surface(ctx
,
104 (struct pipe_resource
*)texture
->flushed_depth_texture
, &surf_tmpl
);
106 if (rctx
->family
== CHIP_RV610
|| rctx
->family
== CHIP_RV630
||
107 rctx
->family
== CHIP_RV620
|| rctx
->family
== CHIP_RV635
)
110 r600_blitter_begin(ctx
, R600_CLEAR_SURFACE
);
111 util_blitter_custom_depth_stencil(rctx
->blitter
, zsurf
, cbsurf
, rctx
->custom_dsa_flush
, depth
);
112 r600_blitter_end(ctx
);
114 pipe_surface_reference(&zsurf
, NULL
);
115 pipe_surface_reference(&cbsurf
, NULL
);
117 texture
->dirty_db
= FALSE
;
120 void r600_flush_depth_textures(struct r600_pipe_context
*rctx
)
124 if (rctx
->blit
) return;
126 /* FIXME: This handles fragment shader textures only. */
128 for (i
= 0; i
< rctx
->ps_samplers
.n_views
; ++i
) {
129 struct r600_pipe_sampler_view
*view
;
130 struct r600_resource_texture
*tex
;
132 view
= rctx
->ps_samplers
.views
[i
];
135 tex
= (struct r600_resource_texture
*)view
->base
.texture
;
139 if (tex
->is_flushing_texture
)
142 r600_blit_uncompress_depth(&rctx
->context
, tex
);
145 /* also check CB here */
146 for (i
= 0; i
< rctx
->framebuffer
.nr_cbufs
; i
++) {
147 struct r600_resource_texture
*tex
;
148 tex
= (struct r600_resource_texture
*)rctx
->framebuffer
.cbufs
[i
]->texture
;
153 if (tex
->is_flushing_texture
)
156 r600_blit_uncompress_depth(&rctx
->context
, tex
);
160 static void r600_clear(struct pipe_context
*ctx
, unsigned buffers
,
161 const float *rgba
, double depth
, unsigned stencil
)
163 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
164 struct pipe_framebuffer_state
*fb
= &rctx
->framebuffer
;
166 r600_blitter_begin(ctx
, R600_CLEAR
);
167 util_blitter_clear(rctx
->blitter
, fb
->width
, fb
->height
,
168 fb
->nr_cbufs
, buffers
, rgba
, depth
,
170 r600_blitter_end(ctx
);
173 static void r600_clear_render_target(struct pipe_context
*ctx
,
174 struct pipe_surface
*dst
,
176 unsigned dstx
, unsigned dsty
,
177 unsigned width
, unsigned height
)
179 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
181 r600_blitter_begin(ctx
, R600_CLEAR_SURFACE
);
182 util_blitter_clear_render_target(rctx
->blitter
, dst
, rgba
,
183 dstx
, dsty
, width
, height
);
184 r600_blitter_end(ctx
);
187 static void r600_clear_depth_stencil(struct pipe_context
*ctx
,
188 struct pipe_surface
*dst
,
189 unsigned clear_flags
,
192 unsigned dstx
, unsigned dsty
,
193 unsigned width
, unsigned height
)
195 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
197 r600_blitter_begin(ctx
, R600_CLEAR_SURFACE
);
198 util_blitter_clear_depth_stencil(rctx
->blitter
, dst
, clear_flags
, depth
, stencil
,
199 dstx
, dsty
, width
, height
);
200 r600_blitter_end(ctx
);
205 /* Copy a block of pixels from one surface to another using HW. */
206 static void r600_hw_copy_region(struct pipe_context
*ctx
,
207 struct pipe_resource
*dst
,
209 unsigned dstx
, unsigned dsty
, unsigned dstz
,
210 struct pipe_resource
*src
,
212 const struct pipe_box
*src_box
)
214 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
216 r600_blitter_begin(ctx
, R600_COPY
);
217 util_blitter_copy_region(rctx
->blitter
, dst
, dst_level
, dstx
, dsty
, dstz
,
218 src
, src_level
, src_box
, TRUE
);
219 r600_blitter_end(ctx
);
222 static void r600_resource_copy_region(struct pipe_context
*ctx
,
223 struct pipe_resource
*dst
,
225 unsigned dstx
, unsigned dsty
, unsigned dstz
,
226 struct pipe_resource
*src
,
228 const struct pipe_box
*src_box
)
230 struct r600_resource_texture
*rsrc
= (struct r600_resource_texture
*)src
;
232 if (rsrc
->depth
&& !rsrc
->is_flushing_texture
)
233 r600_texture_depth_flush(ctx
, src
, FALSE
);
235 r600_hw_copy_region(ctx
, dst
, dst_level
, dstx
, dsty
, dstz
,
236 src
, src_level
, src_box
);
240 void r600_init_blit_functions(struct r600_pipe_context
*rctx
)
242 rctx
->context
.clear
= r600_clear
;
243 rctx
->context
.clear_render_target
= r600_clear_render_target
;
244 rctx
->context
.clear_depth_stencil
= r600_clear_depth_stencil
;
245 rctx
->context
.resource_copy_region
= r600_resource_copy_region
;
248 void r600_blit_push_depth(struct pipe_context
*ctx
, struct r600_resource_texture
*texture
)
250 struct pipe_box sbox
;
252 sbox
.x
= sbox
.y
= sbox
.z
= 0;
253 sbox
.width
= texture
->resource
.b
.b
.b
.width0
;
254 sbox
.height
= texture
->resource
.b
.b
.b
.height0
;
255 /* XXX that might be wrong */
258 r600_hw_copy_region(ctx
, (struct pipe_resource
*)texture
, 0,
260 (struct pipe_resource
*)texture
->flushed_depth_texture
, 0,