gallium: s/PIPE_CAP_TIMER_QUERY/PIPE_CAP_QUERY_TIME_ELAPSED/
[mesa.git] / src / gallium / drivers / radeonsi / 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 "radeonsi_pipe.h"
27 #include "si_state.h"
28
29 enum r600_blitter_op /* bitmask */
30 {
31 R600_SAVE_TEXTURES = 1,
32 R600_SAVE_FRAMEBUFFER = 2,
33 R600_DISABLE_RENDER_COND = 4,
34
35 R600_CLEAR = 0,
36
37 R600_CLEAR_SURFACE = R600_SAVE_FRAMEBUFFER,
38
39 R600_COPY = R600_SAVE_FRAMEBUFFER | R600_SAVE_TEXTURES |
40 R600_DISABLE_RENDER_COND,
41
42 R600_BLIT = R600_SAVE_FRAMEBUFFER | R600_SAVE_TEXTURES |
43 R600_DISABLE_RENDER_COND,
44
45 R600_DECOMPRESS = R600_SAVE_FRAMEBUFFER | R600_DISABLE_RENDER_COND,
46 };
47
48 static void r600_blitter_begin(struct pipe_context *ctx, enum r600_blitter_op op)
49 {
50 struct r600_context *rctx = (struct r600_context *)ctx;
51
52 r600_context_queries_suspend(rctx);
53
54 util_blitter_save_blend(rctx->blitter, rctx->queued.named.blend);
55 util_blitter_save_depth_stencil_alpha(rctx->blitter, rctx->queued.named.dsa);
56 util_blitter_save_stencil_ref(rctx->blitter, &rctx->stencil_ref);
57 util_blitter_save_rasterizer(rctx->blitter, rctx->queued.named.rasterizer);
58 util_blitter_save_fragment_shader(rctx->blitter, rctx->ps_shader);
59 util_blitter_save_vertex_shader(rctx->blitter, rctx->vs_shader);
60 util_blitter_save_vertex_elements(rctx->blitter, rctx->vertex_elements);
61 if (rctx->queued.named.viewport) {
62 util_blitter_save_viewport(rctx->blitter, &rctx->queued.named.viewport->viewport);
63 }
64 util_blitter_save_vertex_buffer_slot(rctx->blitter, rctx->vertex_buffer);
65 util_blitter_save_so_targets(rctx->blitter, rctx->num_so_targets,
66 (struct pipe_stream_output_target**)rctx->so_targets);
67
68 if (op & R600_SAVE_FRAMEBUFFER)
69 util_blitter_save_framebuffer(rctx->blitter, &rctx->framebuffer);
70
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);
75
76 util_blitter_save_fragment_sampler_views(
77 rctx->blitter, rctx->ps_samplers.n_views,
78 (struct pipe_sampler_view**)rctx->ps_samplers.views);
79 }
80
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);
85 }
86
87 }
88
89 static void r600_blitter_end(struct pipe_context *ctx)
90 {
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;
97 }
98 r600_context_queries_resume(rctx);
99 }
100
101 static unsigned u_num_layers(struct pipe_resource *r, unsigned level)
102 {
103 switch (r->target) {
104 case PIPE_TEXTURE_CUBE:
105 return 6;
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;
112 default:
113 return 1;
114 }
115 }
116
117 void si_blit_uncompress_depth(struct pipe_context *ctx, struct r600_resource_texture *texture)
118 {
119 struct r600_context *rctx = (struct r600_context *)ctx;
120 unsigned layer, level;
121 float depth = 1.0f;
122
123 if (!texture->dirty_db)
124 return;
125
126 for (level = 0; level <= texture->resource.b.b.last_level; level++) {
127 unsigned num_layers = u_num_layers(&texture->resource.b.b, level);
128
129 for (layer = 0; layer < num_layers; layer++) {
130 struct pipe_surface *zsurf, *cbsurf, surf_tmpl;
131
132 surf_tmpl.format = texture->real_format;
133 surf_tmpl.u.tex.level = level;
134 surf_tmpl.u.tex.first_layer = layer;
135 surf_tmpl.u.tex.last_layer = layer;
136
137 zsurf = ctx->create_surface(ctx, &texture->resource.b.b, &surf_tmpl);
138
139 surf_tmpl.format = texture->flushed_depth_texture->real_format;
140 cbsurf = ctx->create_surface(ctx,
141 (struct pipe_resource*)texture->flushed_depth_texture, &surf_tmpl);
142
143 r600_blitter_begin(ctx, R600_DECOMPRESS);
144 util_blitter_custom_depth_stencil(rctx->blitter, zsurf, cbsurf, ~0, rctx->custom_dsa_flush, depth);
145 r600_blitter_end(ctx);
146
147 pipe_surface_reference(&zsurf, NULL);
148 pipe_surface_reference(&cbsurf, NULL);
149 }
150 }
151
152 texture->dirty_db = FALSE;
153 }
154
155 void si_flush_depth_textures(struct r600_context *rctx)
156 {
157 unsigned int i;
158
159 /* FIXME: This handles fragment shader textures only. */
160
161 for (i = 0; i < rctx->ps_samplers.n_views; ++i) {
162 struct si_pipe_sampler_view *view;
163 struct r600_resource_texture *tex;
164
165 view = rctx->ps_samplers.views[i];
166 if (!view) continue;
167
168 tex = (struct r600_resource_texture *)view->base.texture;
169 if (!tex->depth)
170 continue;
171
172 if (tex->is_flushing_texture)
173 continue;
174
175 si_blit_uncompress_depth(&rctx->context, tex);
176 }
177
178 /* also check CB here */
179 for (i = 0; i < rctx->framebuffer.nr_cbufs; i++) {
180 struct r600_resource_texture *tex;
181 tex = (struct r600_resource_texture *)rctx->framebuffer.cbufs[i]->texture;
182
183 if (!tex->depth)
184 continue;
185
186 if (tex->is_flushing_texture)
187 continue;
188
189 si_blit_uncompress_depth(&rctx->context, tex);
190 }
191 }
192
193 static void r600_clear(struct pipe_context *ctx, unsigned buffers,
194 const union pipe_color_union *color,
195 double depth, unsigned stencil)
196 {
197 struct r600_context *rctx = (struct r600_context *)ctx;
198 struct pipe_framebuffer_state *fb = &rctx->framebuffer;
199
200 r600_blitter_begin(ctx, R600_CLEAR);
201 util_blitter_clear(rctx->blitter, fb->width, fb->height,
202 fb->nr_cbufs, buffers, fb->nr_cbufs ? fb->cbufs[0]->format : PIPE_FORMAT_NONE,
203 color, depth, stencil);
204 r600_blitter_end(ctx);
205 }
206
207 static void r600_clear_render_target(struct pipe_context *ctx,
208 struct pipe_surface *dst,
209 const union pipe_color_union *color,
210 unsigned dstx, unsigned dsty,
211 unsigned width, unsigned height)
212 {
213 struct r600_context *rctx = (struct r600_context *)ctx;
214
215 r600_blitter_begin(ctx, R600_CLEAR_SURFACE);
216 util_blitter_clear_render_target(rctx->blitter, dst, color,
217 dstx, dsty, width, height);
218 r600_blitter_end(ctx);
219 }
220
221 static void r600_clear_depth_stencil(struct pipe_context *ctx,
222 struct pipe_surface *dst,
223 unsigned clear_flags,
224 double depth,
225 unsigned stencil,
226 unsigned dstx, unsigned dsty,
227 unsigned width, unsigned height)
228 {
229 struct r600_context *rctx = (struct r600_context *)ctx;
230
231 r600_blitter_begin(ctx, R600_CLEAR_SURFACE);
232 util_blitter_clear_depth_stencil(rctx->blitter, dst, clear_flags, depth, stencil,
233 dstx, dsty, width, height);
234 r600_blitter_end(ctx);
235 }
236
237
238
239 /* Copy a block of pixels from one surface to another using HW. */
240 static void r600_hw_copy_region(struct pipe_context *ctx,
241 struct pipe_resource *dst,
242 unsigned dst_level,
243 unsigned dstx, unsigned dsty, unsigned dstz,
244 struct pipe_resource *src,
245 unsigned src_level,
246 const struct pipe_box *src_box)
247 {
248 struct r600_context *rctx = (struct r600_context *)ctx;
249
250 r600_blitter_begin(ctx, R600_COPY);
251 util_blitter_copy_texture(rctx->blitter, dst, dst_level, dstx, dsty, dstz,
252 src, src_level, src_box, PIPE_MASK_RGBAZS, TRUE);
253 r600_blitter_end(ctx);
254 }
255
256 struct texture_orig_info {
257 unsigned format;
258 unsigned width0;
259 unsigned height0;
260 unsigned npix_x;
261 unsigned npix_y;
262 unsigned npix0_x;
263 unsigned npix0_y;
264 };
265
266 static void r600_compressed_to_blittable(struct pipe_resource *tex,
267 unsigned level,
268 struct texture_orig_info *orig)
269 {
270 struct r600_resource_texture *rtex = (struct r600_resource_texture*)tex;
271 unsigned pixsize = util_format_get_blocksize(rtex->real_format);
272 int new_format;
273 int new_height, new_width;
274
275 orig->format = tex->format;
276 orig->width0 = tex->width0;
277 orig->height0 = tex->height0;
278 orig->npix0_x = rtex->surface.level[0].npix_x;
279 orig->npix0_y = rtex->surface.level[0].npix_y;
280 orig->npix_x = rtex->surface.level[level].npix_x;
281 orig->npix_y = rtex->surface.level[level].npix_y;
282
283 if (pixsize == 8)
284 new_format = PIPE_FORMAT_R16G16B16A16_UINT; /* 64-bit block */
285 else
286 new_format = PIPE_FORMAT_R32G32B32A32_UINT; /* 128-bit block */
287
288 new_width = util_format_get_nblocksx(tex->format, orig->width0);
289 new_height = util_format_get_nblocksy(tex->format, orig->height0);
290
291 tex->width0 = new_width;
292 tex->height0 = new_height;
293 tex->format = new_format;
294 rtex->surface.level[0].npix_x = util_format_get_nblocksx(orig->format, orig->npix0_x);
295 rtex->surface.level[0].npix_y = util_format_get_nblocksy(orig->format, orig->npix0_y);
296 rtex->surface.level[level].npix_x = util_format_get_nblocksx(orig->format, orig->npix_x);
297 rtex->surface.level[level].npix_y = util_format_get_nblocksy(orig->format, orig->npix_y);
298 }
299
300 static void r600_reset_blittable_to_compressed(struct pipe_resource *tex,
301 unsigned level,
302 struct texture_orig_info *orig)
303 {
304 struct r600_resource_texture *rtex = (struct r600_resource_texture*)tex;
305
306 tex->format = orig->format;
307 tex->width0 = orig->width0;
308 tex->height0 = orig->height0;
309 rtex->surface.level[0].npix_x = orig->npix0_x;
310 rtex->surface.level[0].npix_y = orig->npix0_y;
311 rtex->surface.level[level].npix_x = orig->npix_x;
312 rtex->surface.level[level].npix_y = orig->npix_y;
313 }
314
315 static void r600_resource_copy_region(struct pipe_context *ctx,
316 struct pipe_resource *dst,
317 unsigned dst_level,
318 unsigned dstx, unsigned dsty, unsigned dstz,
319 struct pipe_resource *src,
320 unsigned src_level,
321 const struct pipe_box *src_box)
322 {
323 struct r600_resource_texture *rsrc = (struct r600_resource_texture*)src;
324 struct texture_orig_info orig_info[2];
325 struct pipe_box sbox;
326 const struct pipe_box *psbox;
327 boolean restore_orig[2];
328
329 memset(orig_info, 0, sizeof(orig_info));
330
331 /* Fallback for buffers. */
332 if (dst->target == PIPE_BUFFER && src->target == PIPE_BUFFER) {
333 util_resource_copy_region(ctx, dst, dst_level, dstx, dsty, dstz,
334 src, src_level, src_box);
335 return;
336 }
337
338 if (rsrc->depth && !rsrc->is_flushing_texture)
339 r600_texture_depth_flush(ctx, src, FALSE);
340
341 restore_orig[0] = restore_orig[1] = FALSE;
342
343 if (util_format_is_compressed(src->format)) {
344 r600_compressed_to_blittable(src, src_level, &orig_info[0]);
345 restore_orig[0] = TRUE;
346 sbox.x = util_format_get_nblocksx(orig_info[0].format, src_box->x);
347 sbox.y = util_format_get_nblocksy(orig_info[0].format, src_box->y);
348 sbox.z = src_box->z;
349 sbox.width = util_format_get_nblocksx(orig_info[0].format, src_box->width);
350 sbox.height = util_format_get_nblocksy(orig_info[0].format, src_box->height);
351 sbox.depth = src_box->depth;
352 psbox=&sbox;
353 } else
354 psbox=src_box;
355
356 if (util_format_is_compressed(dst->format)) {
357 r600_compressed_to_blittable(dst, dst_level, &orig_info[1]);
358 restore_orig[1] = TRUE;
359 /* translate the dst box as well */
360 dstx = util_format_get_nblocksx(orig_info[1].format, dstx);
361 dsty = util_format_get_nblocksy(orig_info[1].format, dsty);
362 }
363
364 r600_hw_copy_region(ctx, dst, dst_level, dstx, dsty, dstz,
365 src, src_level, psbox);
366
367 if (restore_orig[0])
368 r600_reset_blittable_to_compressed(src, src_level, &orig_info[0]);
369
370 if (restore_orig[1])
371 r600_reset_blittable_to_compressed(dst, dst_level, &orig_info[1]);
372 }
373
374 static void si_blit(struct pipe_context *ctx,
375 const struct pipe_blit_info *info)
376 {
377 struct r600_context *rctx = (struct r600_context*)ctx;
378 struct r600_resource_texture *rsrc = (struct r600_resource_texture*)info->src.resource;
379
380 assert(util_blitter_is_blit_supported(rctx->blitter, info));
381
382 if (info->src.resource->nr_samples > 1 &&
383 info->dst.resource->nr_samples <= 1 &&
384 !util_format_is_depth_or_stencil(info->src.resource->format) &&
385 !util_format_is_pure_integer(info->src.resource->format)) {
386 debug_printf("radeonsi: color resolve is unimplemented\n");
387 return;
388 }
389
390 if (rsrc->depth && !rsrc->is_flushing_texture)
391 r600_texture_depth_flush(ctx, info->src.resource, FALSE);
392
393 r600_blitter_begin(ctx, R600_BLIT);
394 util_blitter_blit(rctx->blitter, info);
395 r600_blitter_end(ctx);
396 }
397
398 void si_init_blit_functions(struct r600_context *rctx)
399 {
400 rctx->context.clear = r600_clear;
401 rctx->context.clear_render_target = r600_clear_render_target;
402 rctx->context.clear_depth_stencil = r600_clear_depth_stencil;
403 rctx->context.resource_copy_region = r600_resource_copy_region;
404 rctx->context.blit = si_blit;
405 }
406
407 void r600_blit_push_depth(struct pipe_context *ctx, struct r600_resource_texture *texture)
408 {
409 struct pipe_box sbox;
410
411 sbox.x = sbox.y = sbox.z = 0;
412 sbox.width = texture->resource.b.b.width0;
413 sbox.height = texture->resource.b.b.height0;
414 /* XXX that might be wrong */
415 sbox.depth = 1;
416
417 r600_hw_copy_region(ctx, (struct pipe_resource *)texture, 0,
418 0, 0, 0,
419 (struct pipe_resource *)texture->flushed_depth_texture, 0,
420 &sbox);
421 }