r600g: Update the flushed depth texture after drawing to the corresponding texture.
[mesa.git] / src / gallium / drivers / r600 / r600_blit.c
1 /*
2 * Copyright 2010 Jerome Glisse <glisse@freedesktop.org>
3 *
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:
10 *
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
13 * Software.
14 *
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.
22 */
23 #include <util/u_surface.h>
24 #include <util/u_blitter.h>
25 #include <util/u_format.h>
26 #include "r600_pipe.h"
27
28 enum r600_blitter_op /* bitmask */
29 {
30 R600_CLEAR = 1,
31 R600_CLEAR_SURFACE = 2,
32 R600_COPY = 4
33 };
34
35 static void r600_blitter_begin(struct pipe_context *ctx, enum r600_blitter_op op)
36 {
37 struct r600_pipe_context *rctx = (struct r600_pipe_context *)ctx;
38
39 rctx->blit = true;
40 r600_context_queries_suspend(&rctx->ctx);
41
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);
46 }
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);
53 }
54 if (rctx->states[R600_PIPE_STATE_CLIP]) {
55 util_blitter_save_clip(rctx->blitter, &rctx->clip);
56 }
57 util_blitter_save_vertex_buffers(rctx->blitter, rctx->nvertex_buffers, rctx->vertex_buffer);
58
59 if (op & (R600_CLEAR_SURFACE | R600_COPY))
60 util_blitter_save_framebuffer(rctx->blitter, &rctx->framebuffer);
61
62 if (op & R600_COPY) {
63 util_blitter_save_fragment_sampler_states(
64 rctx->blitter, rctx->ps_samplers.n_samplers,
65 (void**)rctx->ps_samplers.samplers);
66
67 util_blitter_save_fragment_sampler_views(
68 rctx->blitter, rctx->ps_samplers.n_views,
69 (struct pipe_sampler_view**)rctx->ps_samplers.views);
70 }
71
72 }
73
74 static void r600_blitter_end(struct pipe_context *ctx)
75 {
76 struct r600_pipe_context *rctx = (struct r600_pipe_context *)ctx;
77 r600_context_queries_resume(&rctx->ctx);
78 rctx->blit = false;
79 }
80
81 void r600_blit_uncompress_depth(struct pipe_context *ctx, struct r600_resource_texture *texture)
82 {
83 struct r600_pipe_context *rctx = (struct r600_pipe_context *)ctx;
84 struct pipe_surface *zsurf, *cbsurf, surf_tmpl;
85 int level = 0;
86 float depth = 1.0f;
87
88 if (texture->flushed) return;
89
90 surf_tmpl.format = texture->resource.base.b.format;
91 surf_tmpl.u.tex.level = level;
92 surf_tmpl.u.tex.first_layer = 0;
93 surf_tmpl.u.tex.last_layer = 0;
94 surf_tmpl.usage = PIPE_BIND_DEPTH_STENCIL;
95
96 zsurf = ctx->create_surface(ctx, &texture->resource.base.b, &surf_tmpl);
97
98 surf_tmpl.format = ((struct pipe_resource*)texture->flushed_depth_texture)->format;
99 surf_tmpl.usage = PIPE_BIND_RENDER_TARGET;
100 cbsurf = ctx->create_surface(ctx,
101 (struct pipe_resource*)texture->flushed_depth_texture, &surf_tmpl);
102
103 if (rctx->family == CHIP_RV610 || rctx->family == CHIP_RV630 ||
104 rctx->family == CHIP_RV620 || rctx->family == CHIP_RV635)
105 depth = 0.0f;
106
107 r600_blitter_begin(ctx, R600_CLEAR_SURFACE);
108 util_blitter_custom_depth_stencil(rctx->blitter, zsurf, cbsurf, rctx->custom_dsa_flush, depth);
109 r600_blitter_end(ctx);
110 texture->flushed = true;
111
112 pipe_surface_reference(&zsurf, NULL);
113 pipe_surface_reference(&cbsurf, NULL);
114 }
115
116 void r600_flush_depth_textures(struct r600_pipe_context *rctx)
117 {
118 unsigned int i;
119
120 if (rctx->blit) return;
121
122 /* FIXME: This handles fragment shader textures only. */
123
124 for (i = 0; i < rctx->ps_samplers.n_views; ++i) {
125 struct r600_pipe_sampler_view *view;
126 struct r600_resource_texture *tex;
127
128 view = rctx->ps_samplers.views[i];
129 if (!view) continue;
130
131 tex = (struct r600_resource_texture *)view->base.texture;
132 if (!tex->depth) continue;
133
134 r600_blit_uncompress_depth(&rctx->context, tex);
135 }
136 }
137
138 static void r600_clear(struct pipe_context *ctx, unsigned buffers,
139 const float *rgba, double depth, unsigned stencil)
140 {
141 struct r600_pipe_context *rctx = (struct r600_pipe_context *)ctx;
142 struct pipe_framebuffer_state *fb = &rctx->framebuffer;
143
144 r600_blitter_begin(ctx, R600_CLEAR);
145 util_blitter_clear(rctx->blitter, fb->width, fb->height,
146 fb->nr_cbufs, buffers, rgba, depth,
147 stencil);
148 r600_blitter_end(ctx);
149 }
150
151 static void r600_clear_render_target(struct pipe_context *ctx,
152 struct pipe_surface *dst,
153 const float *rgba,
154 unsigned dstx, unsigned dsty,
155 unsigned width, unsigned height)
156 {
157 struct r600_pipe_context *rctx = (struct r600_pipe_context *)ctx;
158
159 r600_blitter_begin(ctx, R600_CLEAR_SURFACE);
160 util_blitter_clear_render_target(rctx->blitter, dst, rgba,
161 dstx, dsty, width, height);
162 r600_blitter_end(ctx);
163 }
164
165 static void r600_clear_depth_stencil(struct pipe_context *ctx,
166 struct pipe_surface *dst,
167 unsigned clear_flags,
168 double depth,
169 unsigned stencil,
170 unsigned dstx, unsigned dsty,
171 unsigned width, unsigned height)
172 {
173 struct r600_pipe_context *rctx = (struct r600_pipe_context *)ctx;
174
175 r600_blitter_begin(ctx, R600_CLEAR_SURFACE);
176 util_blitter_clear_depth_stencil(rctx->blitter, dst, clear_flags, depth, stencil,
177 dstx, dsty, width, height);
178 r600_blitter_end(ctx);
179 }
180
181
182
183 /* Copy a block of pixels from one surface to another using HW. */
184 static void r600_hw_copy_region(struct pipe_context *ctx,
185 struct pipe_resource *dst,
186 unsigned dst_level,
187 unsigned dstx, unsigned dsty, unsigned dstz,
188 struct pipe_resource *src,
189 unsigned src_level,
190 const struct pipe_box *src_box)
191 {
192 struct r600_pipe_context *rctx = (struct r600_pipe_context *)ctx;
193
194 r600_blitter_begin(ctx, R600_COPY);
195 util_blitter_copy_region(rctx->blitter, dst, dst_level, dstx, dsty, dstz,
196 src, src_level, src_box, TRUE);
197 r600_blitter_end(ctx);
198 }
199
200 static void r600_resource_copy_region(struct pipe_context *ctx,
201 struct pipe_resource *dst,
202 unsigned dst_level,
203 unsigned dstx, unsigned dsty, unsigned dstz,
204 struct pipe_resource *src,
205 unsigned src_level,
206 const struct pipe_box *src_box)
207 {
208 boolean is_depth;
209 /* there is something wrong with depth resource copies at the moment so avoid them for now */
210 is_depth = util_format_get_component_bits(src->format, UTIL_FORMAT_COLORSPACE_ZS, 0) != 0;
211 if (is_depth)
212 util_resource_copy_region(ctx, dst, dst_level, dstx, dsty, dstz,
213 src, src_level, src_box);
214 else
215 r600_hw_copy_region(ctx, dst, dst_level, dstx, dsty, dstz,
216 src, src_level, src_box);
217 }
218
219 void r600_init_blit_functions(struct r600_pipe_context *rctx)
220 {
221 rctx->context.clear = r600_clear;
222 rctx->context.clear_render_target = r600_clear_render_target;
223 rctx->context.clear_depth_stencil = r600_clear_depth_stencil;
224 rctx->context.resource_copy_region = r600_resource_copy_region;
225 }