dfc43feb5d9e7a0021b873e36979f5e96521008b
[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_SAVE_TEXTURES = 1,
31 R600_SAVE_FRAMEBUFFER = 2,
32 R600_DISABLE_RENDER_COND = 4,
33
34 R600_CLEAR = 0,
35
36 R600_CLEAR_SURFACE = R600_SAVE_FRAMEBUFFER,
37
38 R600_COPY = R600_SAVE_FRAMEBUFFER | R600_SAVE_TEXTURES |
39 R600_DISABLE_RENDER_COND,
40
41 R600_DECOMPRESS = R600_SAVE_FRAMEBUFFER | R600_DISABLE_RENDER_COND,
42 };
43
44 static void r600_blitter_begin(struct pipe_context *ctx, enum r600_blitter_op op)
45 {
46 struct r600_pipe_context *rctx = (struct r600_pipe_context *)ctx;
47
48 rctx->blit = true;
49 r600_context_queries_suspend(&rctx->ctx);
50
51 util_blitter_save_blend(rctx->blitter, rctx->states[R600_PIPE_STATE_BLEND]);
52 util_blitter_save_depth_stencil_alpha(rctx->blitter, rctx->states[R600_PIPE_STATE_DSA]);
53 if (rctx->states[R600_PIPE_STATE_STENCIL_REF]) {
54 util_blitter_save_stencil_ref(rctx->blitter, &rctx->stencil_ref);
55 }
56 util_blitter_save_rasterizer(rctx->blitter, rctx->states[R600_PIPE_STATE_RASTERIZER]);
57 util_blitter_save_fragment_shader(rctx->blitter, rctx->ps_shader);
58 util_blitter_save_vertex_shader(rctx->blitter, rctx->vs_shader);
59 util_blitter_save_vertex_elements(rctx->blitter, rctx->vertex_elements);
60 if (rctx->states[R600_PIPE_STATE_VIEWPORT]) {
61 util_blitter_save_viewport(rctx->blitter, &rctx->viewport);
62 }
63 if (rctx->states[R600_PIPE_STATE_CLIP]) {
64 util_blitter_save_clip(rctx->blitter, &rctx->clip);
65 }
66 util_blitter_save_vertex_buffers(rctx->blitter,
67 rctx->vbuf_mgr->nr_vertex_buffers,
68 rctx->vbuf_mgr->vertex_buffer);
69
70 if (op & R600_SAVE_FRAMEBUFFER)
71 util_blitter_save_framebuffer(rctx->blitter, &rctx->framebuffer);
72
73 if (op & R600_SAVE_TEXTURES) {
74 util_blitter_save_fragment_sampler_states(
75 rctx->blitter, rctx->ps_samplers.n_samplers,
76 (void**)rctx->ps_samplers.samplers);
77
78 util_blitter_save_fragment_sampler_views(
79 rctx->blitter, rctx->ps_samplers.n_views,
80 (struct pipe_sampler_view**)rctx->ps_samplers.views);
81 }
82
83 if ((op & R600_DISABLE_RENDER_COND) && rctx->current_render_cond) {
84 rctx->saved_render_cond = rctx->current_render_cond;
85 rctx->saved_render_cond_mode = rctx->current_render_cond_mode;
86 rctx->context.render_condition(&rctx->context, NULL, 0);
87 }
88
89 }
90
91 static void r600_blitter_end(struct pipe_context *ctx)
92 {
93 struct r600_pipe_context *rctx = (struct r600_pipe_context *)ctx;
94 if (rctx->saved_render_cond) {
95 rctx->context.render_condition(&rctx->context,
96 rctx->saved_render_cond,
97 rctx->saved_render_cond_mode);
98 rctx->saved_render_cond = NULL;
99 }
100 r600_context_queries_resume(&rctx->ctx, FALSE);
101 rctx->blit = false;
102 }
103
104 static unsigned u_num_layers(struct pipe_resource *r, unsigned level)
105 {
106 switch (r->target) {
107 case PIPE_TEXTURE_CUBE:
108 return 6;
109 case PIPE_TEXTURE_3D:
110 return u_minify(r->depth0, level);
111 case PIPE_TEXTURE_1D_ARRAY:
112 return r->array_size;
113 case PIPE_TEXTURE_2D_ARRAY:
114 return r->array_size;
115 default:
116 return 1;
117 }
118 }
119
120 void r600_blit_uncompress_depth(struct pipe_context *ctx, struct r600_resource_texture *texture)
121 {
122 struct r600_pipe_context *rctx = (struct r600_pipe_context *)ctx;
123 unsigned layer, level;
124 float depth = 1.0f;
125
126 if (!texture->dirty_db)
127 return;
128
129 if (rctx->family == CHIP_RV610 || rctx->family == CHIP_RV630 ||
130 rctx->family == CHIP_RV620 || rctx->family == CHIP_RV635)
131 depth = 0.0f;
132
133 for (level = 0; level <= texture->resource.b.b.b.last_level; level++) {
134 unsigned num_layers = u_num_layers(&texture->resource.b.b.b, level);
135
136 for (layer = 0; layer < num_layers; layer++) {
137 struct pipe_surface *zsurf, *cbsurf, surf_tmpl;
138
139 surf_tmpl.format = texture->real_format;
140 surf_tmpl.u.tex.level = level;
141 surf_tmpl.u.tex.first_layer = layer;
142 surf_tmpl.u.tex.last_layer = layer;
143 surf_tmpl.usage = PIPE_BIND_DEPTH_STENCIL;
144
145 zsurf = ctx->create_surface(ctx, &texture->resource.b.b.b, &surf_tmpl);
146
147 surf_tmpl.format = texture->flushed_depth_texture->real_format;
148 surf_tmpl.usage = PIPE_BIND_RENDER_TARGET;
149 cbsurf = ctx->create_surface(ctx,
150 (struct pipe_resource*)texture->flushed_depth_texture, &surf_tmpl);
151
152 r600_blitter_begin(ctx, R600_DECOMPRESS);
153 util_blitter_custom_depth_stencil(rctx->blitter, zsurf, cbsurf, rctx->custom_dsa_flush, depth);
154 r600_blitter_end(ctx);
155
156 pipe_surface_reference(&zsurf, NULL);
157 pipe_surface_reference(&cbsurf, NULL);
158 }
159 }
160
161 texture->dirty_db = FALSE;
162 }
163
164 void r600_flush_depth_textures(struct r600_pipe_context *rctx)
165 {
166 unsigned int i;
167
168 /* FIXME: This handles fragment shader textures only. */
169
170 for (i = 0; i < rctx->ps_samplers.n_views; ++i) {
171 struct r600_pipe_sampler_view *view;
172 struct r600_resource_texture *tex;
173
174 view = rctx->ps_samplers.views[i];
175 if (!view) continue;
176
177 tex = (struct r600_resource_texture *)view->base.texture;
178 if (!tex->depth)
179 continue;
180
181 if (tex->is_flushing_texture)
182 continue;
183
184 r600_blit_uncompress_depth(&rctx->context, tex);
185 }
186
187 /* also check CB here */
188 for (i = 0; i < rctx->framebuffer.nr_cbufs; i++) {
189 struct r600_resource_texture *tex;
190 tex = (struct r600_resource_texture *)rctx->framebuffer.cbufs[i]->texture;
191
192 if (!tex->depth)
193 continue;
194
195 if (tex->is_flushing_texture)
196 continue;
197
198 r600_blit_uncompress_depth(&rctx->context, tex);
199 }
200 }
201
202 static void r600_clear(struct pipe_context *ctx, unsigned buffers,
203 const union pipe_color_union *color,
204 double depth, unsigned stencil)
205 {
206 struct r600_pipe_context *rctx = (struct r600_pipe_context *)ctx;
207 struct pipe_framebuffer_state *fb = &rctx->framebuffer;
208
209 r600_blitter_begin(ctx, R600_CLEAR);
210 util_blitter_clear(rctx->blitter, fb->width, fb->height,
211 fb->nr_cbufs, buffers, color, depth,
212 stencil);
213 r600_blitter_end(ctx);
214 }
215
216 static void r600_clear_render_target(struct pipe_context *ctx,
217 struct pipe_surface *dst,
218 const union pipe_color_union *color,
219 unsigned dstx, unsigned dsty,
220 unsigned width, unsigned height)
221 {
222 struct r600_pipe_context *rctx = (struct r600_pipe_context *)ctx;
223
224 r600_blitter_begin(ctx, R600_CLEAR_SURFACE);
225 util_blitter_clear_render_target(rctx->blitter, dst, color,
226 dstx, dsty, width, height);
227 r600_blitter_end(ctx);
228 }
229
230 static void r600_clear_depth_stencil(struct pipe_context *ctx,
231 struct pipe_surface *dst,
232 unsigned clear_flags,
233 double depth,
234 unsigned stencil,
235 unsigned dstx, unsigned dsty,
236 unsigned width, unsigned height)
237 {
238 struct r600_pipe_context *rctx = (struct r600_pipe_context *)ctx;
239
240 r600_blitter_begin(ctx, R600_CLEAR_SURFACE);
241 util_blitter_clear_depth_stencil(rctx->blitter, dst, clear_flags, depth, stencil,
242 dstx, dsty, width, height);
243 r600_blitter_end(ctx);
244 }
245
246
247
248 /* Copy a block of pixels from one surface to another using HW. */
249 static void r600_hw_copy_region(struct pipe_context *ctx,
250 struct pipe_resource *dst,
251 unsigned dst_level,
252 unsigned dstx, unsigned dsty, unsigned dstz,
253 struct pipe_resource *src,
254 unsigned src_level,
255 const struct pipe_box *src_box)
256 {
257 struct r600_pipe_context *rctx = (struct r600_pipe_context *)ctx;
258
259 r600_blitter_begin(ctx, R600_COPY);
260 util_blitter_copy_texture(rctx->blitter, dst, dst_level, dstx, dsty, dstz,
261 src, src_level, src_box, TRUE);
262 r600_blitter_end(ctx);
263 }
264
265 struct texture_orig_info {
266 unsigned format;
267 unsigned width0;
268 unsigned height0;
269 };
270
271 static void r600_compressed_to_blittable(struct pipe_resource *tex,
272 unsigned level,
273 struct texture_orig_info *orig)
274 {
275 struct r600_resource_texture *rtex = (struct r600_resource_texture*)tex;
276 unsigned pixsize = util_format_get_blocksize(rtex->real_format);
277 int new_format;
278 int new_height, new_width;
279
280 orig->format = tex->format;
281 orig->width0 = tex->width0;
282 orig->height0 = tex->height0;
283
284 if (pixsize == 8)
285 new_format = PIPE_FORMAT_R16G16B16A16_UNORM; /* 64-bit block */
286 else
287 new_format = PIPE_FORMAT_R32G32B32A32_UNORM; /* 128-bit block */
288
289 new_width = util_format_get_nblocksx(tex->format, orig->width0);
290 new_height = util_format_get_nblocksy(tex->format, orig->height0);
291
292 rtex->force_int_type = true;
293 tex->width0 = new_width;
294 tex->height0 = new_height;
295 tex->format = new_format;
296 }
297
298 static void r600_reset_blittable_to_compressed(struct pipe_resource *tex,
299 unsigned level,
300 struct texture_orig_info *orig)
301 {
302 struct r600_resource_texture *rtex = (struct r600_resource_texture*)tex;
303 rtex->force_int_type = false;
304
305 tex->format = orig->format;
306 tex->width0 = orig->width0;
307 tex->height0 = orig->height0;
308 }
309
310 static void r600_resource_copy_region(struct pipe_context *ctx,
311 struct pipe_resource *dst,
312 unsigned dst_level,
313 unsigned dstx, unsigned dsty, unsigned dstz,
314 struct pipe_resource *src,
315 unsigned src_level,
316 const struct pipe_box *src_box)
317 {
318 struct r600_resource_texture *rsrc = (struct r600_resource_texture*)src;
319 struct texture_orig_info orig_info[2];
320 struct pipe_box sbox;
321 const struct pipe_box *psbox;
322 boolean restore_orig[2];
323
324 /* Fallback for buffers. */
325 if (dst->target == PIPE_BUFFER && src->target == PIPE_BUFFER) {
326 util_resource_copy_region(ctx, dst, dst_level, dstx, dsty, dstz,
327 src, src_level, src_box);
328 return;
329 }
330
331 if (rsrc->depth && !rsrc->is_flushing_texture)
332 r600_texture_depth_flush(ctx, src, FALSE);
333
334 restore_orig[0] = restore_orig[1] = FALSE;
335
336 if (util_format_is_compressed(src->format)) {
337 r600_compressed_to_blittable(src, src_level, &orig_info[0]);
338 restore_orig[0] = TRUE;
339 sbox.x = util_format_get_nblocksx(orig_info[0].format, src_box->x);
340 sbox.y = util_format_get_nblocksy(orig_info[0].format, src_box->y);
341 sbox.z = src_box->z;
342 sbox.width = util_format_get_nblocksx(orig_info[0].format, src_box->width);
343 sbox.height = util_format_get_nblocksy(orig_info[0].format, src_box->height);
344 sbox.depth = src_box->depth;
345 psbox=&sbox;
346 } else
347 psbox=src_box;
348
349 if (util_format_is_compressed(dst->format)) {
350 r600_compressed_to_blittable(dst, dst_level, &orig_info[1]);
351 restore_orig[1] = TRUE;
352 /* translate the dst box as well */
353 dstx = util_format_get_nblocksx(orig_info[1].format, dstx);
354 dsty = util_format_get_nblocksy(orig_info[1].format, dsty);
355 }
356
357 r600_hw_copy_region(ctx, dst, dst_level, dstx, dsty, dstz,
358 src, src_level, psbox);
359
360 if (restore_orig[0])
361 r600_reset_blittable_to_compressed(src, src_level, &orig_info[0]);
362
363 if (restore_orig[1])
364 r600_reset_blittable_to_compressed(dst, dst_level, &orig_info[1]);
365 }
366
367 void r600_init_blit_functions(struct r600_pipe_context *rctx)
368 {
369 rctx->context.clear = r600_clear;
370 rctx->context.clear_render_target = r600_clear_render_target;
371 rctx->context.clear_depth_stencil = r600_clear_depth_stencil;
372 rctx->context.resource_copy_region = r600_resource_copy_region;
373 }
374
375 void r600_blit_push_depth(struct pipe_context *ctx, struct r600_resource_texture *texture)
376 {
377 struct pipe_box sbox;
378
379 sbox.x = sbox.y = sbox.z = 0;
380 sbox.width = texture->resource.b.b.b.width0;
381 sbox.height = texture->resource.b.b.b.height0;
382 /* XXX that might be wrong */
383 sbox.depth = 1;
384
385 r600_hw_copy_region(ctx, (struct pipe_resource *)texture, 0,
386 0, 0, 0,
387 (struct pipe_resource *)texture->flushed_depth_texture, 0,
388 &sbox);
389 }