radeonsi: emit TA_BC_BASE_ADDR_HI for border color on CIK
[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_cond = rctx->current_render_cond_cond;
84 rctx->saved_render_cond_mode = rctx->current_render_cond_mode;
85 rctx->context.render_condition(&rctx->context, NULL, FALSE, 0);
86 }
87
88 }
89
90 static void r600_blitter_end(struct pipe_context *ctx)
91 {
92 struct r600_context *rctx = (struct r600_context *)ctx;
93 if (rctx->saved_render_cond) {
94 rctx->context.render_condition(&rctx->context,
95 rctx->saved_render_cond,
96 rctx->saved_render_cond_cond,
97 rctx->saved_render_cond_mode);
98 rctx->saved_render_cond = NULL;
99 }
100 r600_context_queries_resume(rctx);
101 }
102
103 void si_blit_uncompress_depth(struct pipe_context *ctx,
104 struct r600_resource_texture *texture,
105 struct r600_resource_texture *staging,
106 unsigned first_level, unsigned last_level,
107 unsigned first_layer, unsigned last_layer)
108 {
109 struct r600_context *rctx = (struct r600_context *)ctx;
110 unsigned layer, level, checked_last_layer, max_layer;
111 float depth = 1.0f;
112 const struct util_format_description *desc;
113 void *custom_dsa;
114 struct r600_resource_texture *flushed_depth_texture = staging ?
115 staging : texture->flushed_depth_texture;
116
117 if (!staging && !texture->dirty_db_mask)
118 return;
119
120 desc = util_format_description(flushed_depth_texture->resource.b.b.format);
121 switch (util_format_has_depth(desc) | util_format_has_stencil(desc) << 1) {
122 default:
123 assert(!"No depth or stencil to uncompress");
124 case 3:
125 custom_dsa = rctx->custom_dsa_flush_depth_stencil;
126 break;
127 case 2:
128 custom_dsa = rctx->custom_dsa_flush_stencil;
129 break;
130 case 1:
131 custom_dsa = rctx->custom_dsa_flush_depth;
132 break;
133 }
134
135 for (level = first_level; level <= last_level; level++) {
136 if (!staging && !(texture->dirty_db_mask & (1 << level)))
137 continue;
138
139 /* The smaller the mipmap level, the less layers there are
140 * as far as 3D textures are concerned. */
141 max_layer = util_max_layer(&texture->resource.b.b, level);
142 checked_last_layer = last_layer < max_layer ? last_layer : max_layer;
143
144 for (layer = first_layer; layer <= checked_last_layer; layer++) {
145 struct pipe_surface *zsurf, *cbsurf, surf_tmpl;
146
147 surf_tmpl.format = texture->real_format;
148 surf_tmpl.u.tex.level = level;
149 surf_tmpl.u.tex.first_layer = layer;
150 surf_tmpl.u.tex.last_layer = layer;
151
152 zsurf = ctx->create_surface(ctx, &texture->resource.b.b, &surf_tmpl);
153
154 surf_tmpl.format = flushed_depth_texture->real_format;
155 cbsurf = ctx->create_surface(ctx,
156 (struct pipe_resource*)flushed_depth_texture, &surf_tmpl);
157
158 r600_blitter_begin(ctx, R600_DECOMPRESS);
159 util_blitter_custom_depth_stencil(rctx->blitter, zsurf, cbsurf, ~0, custom_dsa, depth);
160 r600_blitter_end(ctx);
161
162 pipe_surface_reference(&zsurf, NULL);
163 pipe_surface_reference(&cbsurf, NULL);
164 }
165
166 /* The texture will always be dirty if some layers aren't flushed.
167 * I don't think this case can occur though. */
168 if (!staging && first_layer == 0 && last_layer == max_layer) {
169 texture->dirty_db_mask &= ~(1 << level);
170 }
171 }
172 }
173
174 static void si_blit_decompress_depth_in_place(struct r600_context *rctx,
175 struct r600_resource_texture *texture,
176 unsigned first_level, unsigned last_level,
177 unsigned first_layer, unsigned last_layer)
178 {
179 struct pipe_surface *zsurf, surf_tmpl = {{0}};
180 unsigned layer, max_layer, checked_last_layer, level;
181
182 surf_tmpl.format = texture->resource.b.b.format;
183
184 for (level = first_level; level <= last_level; level++) {
185 if (!(texture->dirty_db_mask & (1 << level)))
186 continue;
187
188 surf_tmpl.u.tex.level = level;
189
190 /* The smaller the mipmap level, the less layers there are
191 * as far as 3D textures are concerned. */
192 max_layer = util_max_layer(&texture->resource.b.b, level);
193 checked_last_layer = last_layer < max_layer ? last_layer : max_layer;
194
195 for (layer = first_layer; layer <= checked_last_layer; layer++) {
196 surf_tmpl.u.tex.first_layer = layer;
197 surf_tmpl.u.tex.last_layer = layer;
198
199 zsurf = rctx->context.create_surface(&rctx->context, &texture->resource.b.b, &surf_tmpl);
200
201 r600_blitter_begin(&rctx->context, R600_DECOMPRESS);
202 util_blitter_custom_depth_stencil(rctx->blitter, zsurf, NULL, ~0,
203 rctx->custom_dsa_flush_inplace,
204 1.0f);
205 r600_blitter_end(&rctx->context);
206
207 pipe_surface_reference(&zsurf, NULL);
208 }
209
210 /* The texture will always be dirty if some layers aren't flushed.
211 * I don't think this case occurs often though. */
212 if (first_layer == 0 && last_layer == max_layer) {
213 texture->dirty_db_mask &= ~(1 << level);
214 }
215 }
216 }
217
218 void si_flush_depth_textures(struct r600_context *rctx,
219 struct r600_textures_info *textures)
220 {
221 unsigned i;
222
223 for (i = 0; i < textures->n_views; ++i) {
224 struct pipe_sampler_view *view;
225 struct r600_resource_texture *tex;
226
227 view = &textures->views[i]->base;
228 if (!view) continue;
229
230 tex = (struct r600_resource_texture *)view->texture;
231 if (!tex->is_depth || tex->is_flushing_texture)
232 continue;
233
234 si_blit_decompress_depth_in_place(rctx, tex,
235 view->u.tex.first_level, view->u.tex.last_level,
236 0, util_max_layer(&tex->resource.b.b, view->u.tex.first_level));
237 }
238 }
239
240 static void r600_clear(struct pipe_context *ctx, unsigned buffers,
241 const union pipe_color_union *color,
242 double depth, unsigned stencil)
243 {
244 struct r600_context *rctx = (struct r600_context *)ctx;
245 struct pipe_framebuffer_state *fb = &rctx->framebuffer;
246
247 r600_blitter_begin(ctx, R600_CLEAR);
248 util_blitter_clear(rctx->blitter, fb->width, fb->height,
249 buffers, color, depth, stencil);
250 r600_blitter_end(ctx);
251 }
252
253 static void r600_clear_render_target(struct pipe_context *ctx,
254 struct pipe_surface *dst,
255 const union pipe_color_union *color,
256 unsigned dstx, unsigned dsty,
257 unsigned width, unsigned height)
258 {
259 struct r600_context *rctx = (struct r600_context *)ctx;
260
261 r600_blitter_begin(ctx, R600_CLEAR_SURFACE);
262 util_blitter_clear_render_target(rctx->blitter, dst, color,
263 dstx, dsty, width, height);
264 r600_blitter_end(ctx);
265 }
266
267 static void r600_clear_depth_stencil(struct pipe_context *ctx,
268 struct pipe_surface *dst,
269 unsigned clear_flags,
270 double depth,
271 unsigned stencil,
272 unsigned dstx, unsigned dsty,
273 unsigned width, unsigned height)
274 {
275 struct r600_context *rctx = (struct r600_context *)ctx;
276
277 r600_blitter_begin(ctx, R600_CLEAR_SURFACE);
278 util_blitter_clear_depth_stencil(rctx->blitter, dst, clear_flags, depth, stencil,
279 dstx, dsty, width, height);
280 r600_blitter_end(ctx);
281 }
282
283 struct texture_orig_info {
284 unsigned format;
285 unsigned width0;
286 unsigned height0;
287 unsigned npix_x;
288 unsigned npix_y;
289 unsigned npix0_x;
290 unsigned npix0_y;
291 };
292
293 static void r600_compressed_to_blittable(struct pipe_resource *tex,
294 unsigned level,
295 struct texture_orig_info *orig)
296 {
297 struct r600_resource_texture *rtex = (struct r600_resource_texture*)tex;
298 unsigned pixsize = util_format_get_blocksize(rtex->real_format);
299 int new_format;
300 int new_height, new_width;
301
302 orig->format = tex->format;
303 orig->width0 = tex->width0;
304 orig->height0 = tex->height0;
305 orig->npix0_x = rtex->surface.level[0].npix_x;
306 orig->npix0_y = rtex->surface.level[0].npix_y;
307 orig->npix_x = rtex->surface.level[level].npix_x;
308 orig->npix_y = rtex->surface.level[level].npix_y;
309
310 if (pixsize == 8)
311 new_format = PIPE_FORMAT_R16G16B16A16_UINT; /* 64-bit block */
312 else
313 new_format = PIPE_FORMAT_R32G32B32A32_UINT; /* 128-bit block */
314
315 new_width = util_format_get_nblocksx(tex->format, orig->width0);
316 new_height = util_format_get_nblocksy(tex->format, orig->height0);
317
318 tex->width0 = new_width;
319 tex->height0 = new_height;
320 tex->format = new_format;
321 rtex->surface.level[0].npix_x = util_format_get_nblocksx(orig->format, orig->npix0_x);
322 rtex->surface.level[0].npix_y = util_format_get_nblocksy(orig->format, orig->npix0_y);
323 rtex->surface.level[level].npix_x = util_format_get_nblocksx(orig->format, orig->npix_x);
324 rtex->surface.level[level].npix_y = util_format_get_nblocksy(orig->format, orig->npix_y);
325 }
326
327 static void r600_change_format(struct pipe_resource *tex,
328 unsigned level,
329 struct texture_orig_info *orig,
330 enum pipe_format format)
331 {
332 struct r600_resource_texture *rtex = (struct r600_resource_texture*)tex;
333
334 orig->format = tex->format;
335 orig->width0 = tex->width0;
336 orig->height0 = tex->height0;
337 orig->npix0_x = rtex->surface.level[0].npix_x;
338 orig->npix0_y = rtex->surface.level[0].npix_y;
339 orig->npix_x = rtex->surface.level[level].npix_x;
340 orig->npix_y = rtex->surface.level[level].npix_y;
341
342 tex->format = format;
343 }
344
345 static void r600_reset_blittable_to_orig(struct pipe_resource *tex,
346 unsigned level,
347 struct texture_orig_info *orig)
348 {
349 struct r600_resource_texture *rtex = (struct r600_resource_texture*)tex;
350
351 tex->format = orig->format;
352 tex->width0 = orig->width0;
353 tex->height0 = orig->height0;
354 rtex->surface.level[0].npix_x = orig->npix0_x;
355 rtex->surface.level[0].npix_y = orig->npix0_y;
356 rtex->surface.level[level].npix_x = orig->npix_x;
357 rtex->surface.level[level].npix_y = orig->npix_y;
358 }
359
360 static void r600_resource_copy_region(struct pipe_context *ctx,
361 struct pipe_resource *dst,
362 unsigned dst_level,
363 unsigned dstx, unsigned dsty, unsigned dstz,
364 struct pipe_resource *src,
365 unsigned src_level,
366 const struct pipe_box *src_box)
367 {
368 struct r600_context *rctx = (struct r600_context *)ctx;
369 struct r600_resource_texture *rsrc = (struct r600_resource_texture*)src;
370 struct texture_orig_info orig_info[2];
371 struct pipe_box sbox;
372 const struct pipe_box *psbox = src_box;
373 boolean restore_orig[2];
374
375 memset(orig_info, 0, sizeof(orig_info));
376
377 /* Fallback for buffers. */
378 if (dst->target == PIPE_BUFFER && src->target == PIPE_BUFFER) {
379 util_resource_copy_region(ctx, dst, dst_level, dstx, dsty, dstz,
380 src, src_level, src_box);
381 return;
382 }
383
384 /* This must be done before entering u_blitter to avoid recursion. */
385 if (rsrc->is_depth && !rsrc->is_flushing_texture) {
386 si_blit_decompress_depth_in_place(rctx, rsrc,
387 src_level, src_level,
388 src_box->z, src_box->z + src_box->depth - 1);
389 }
390
391 restore_orig[0] = restore_orig[1] = FALSE;
392
393 if (util_format_is_compressed(src->format) &&
394 util_format_is_compressed(dst->format)) {
395 r600_compressed_to_blittable(src, src_level, &orig_info[0]);
396 restore_orig[0] = TRUE;
397 sbox.x = util_format_get_nblocksx(orig_info[0].format, src_box->x);
398 sbox.y = util_format_get_nblocksy(orig_info[0].format, src_box->y);
399 sbox.z = src_box->z;
400 sbox.width = util_format_get_nblocksx(orig_info[0].format, src_box->width);
401 sbox.height = util_format_get_nblocksy(orig_info[0].format, src_box->height);
402 sbox.depth = src_box->depth;
403 psbox=&sbox;
404
405 r600_compressed_to_blittable(dst, dst_level, &orig_info[1]);
406 restore_orig[1] = TRUE;
407 /* translate the dst box as well */
408 dstx = util_format_get_nblocksx(orig_info[1].format, dstx);
409 dsty = util_format_get_nblocksy(orig_info[1].format, dsty);
410 } else if (!util_blitter_is_copy_supported(rctx->blitter, dst, src,
411 PIPE_MASK_RGBAZS)) {
412 unsigned blocksize = util_format_get_blocksize(src->format);
413
414 switch (blocksize) {
415 case 1:
416 r600_change_format(src, src_level, &orig_info[0],
417 PIPE_FORMAT_R8_UNORM);
418 r600_change_format(dst, dst_level, &orig_info[1],
419 PIPE_FORMAT_R8_UNORM);
420 break;
421 case 2:
422 r600_change_format(src, src_level, &orig_info[0],
423 PIPE_FORMAT_R8G8_UNORM);
424 r600_change_format(dst, dst_level, &orig_info[1],
425 PIPE_FORMAT_R8G8_UNORM);
426 break;
427 case 4:
428 r600_change_format(src, src_level, &orig_info[0],
429 PIPE_FORMAT_R8G8B8A8_UNORM);
430 r600_change_format(dst, dst_level, &orig_info[1],
431 PIPE_FORMAT_R8G8B8A8_UNORM);
432 break;
433 case 8:
434 r600_change_format(src, src_level, &orig_info[0],
435 PIPE_FORMAT_R16G16B16A16_UINT);
436 r600_change_format(dst, dst_level, &orig_info[1],
437 PIPE_FORMAT_R16G16B16A16_UINT);
438 break;
439 case 16:
440 r600_change_format(src, src_level, &orig_info[0],
441 PIPE_FORMAT_R32G32B32A32_UINT);
442 r600_change_format(dst, dst_level, &orig_info[1],
443 PIPE_FORMAT_R32G32B32A32_UINT);
444 break;
445 default:
446 fprintf(stderr, "Unhandled format %s with blocksize %u\n",
447 util_format_short_name(src->format), blocksize);
448 assert(0);
449 }
450 restore_orig[0] = TRUE;
451 restore_orig[1] = TRUE;
452 }
453
454 r600_blitter_begin(ctx, R600_COPY);
455 util_blitter_copy_texture(rctx->blitter, dst, dst_level, dstx, dsty, dstz,
456 src, src_level, psbox, PIPE_MASK_RGBAZS, TRUE);
457 r600_blitter_end(ctx);
458
459 if (restore_orig[0])
460 r600_reset_blittable_to_orig(src, src_level, &orig_info[0]);
461
462 if (restore_orig[1])
463 r600_reset_blittable_to_orig(dst, dst_level, &orig_info[1]);
464 }
465
466 static void si_blit(struct pipe_context *ctx,
467 const struct pipe_blit_info *info)
468 {
469 struct r600_context *rctx = (struct r600_context*)ctx;
470 struct r600_resource_texture *rsrc = (struct r600_resource_texture*)info->src.resource;
471
472 assert(util_blitter_is_blit_supported(rctx->blitter, info));
473
474 if (info->src.resource->nr_samples > 1 &&
475 info->dst.resource->nr_samples <= 1 &&
476 !util_format_is_depth_or_stencil(info->src.resource->format) &&
477 !util_format_is_pure_integer(info->src.resource->format)) {
478 debug_printf("radeonsi: color resolve is unimplemented\n");
479 return;
480 }
481
482 if (rsrc->is_depth && !rsrc->is_flushing_texture) {
483 si_blit_decompress_depth_in_place(rctx, rsrc,
484 info->src.level, info->src.level,
485 info->src.box.z,
486 info->src.box.z + info->src.box.depth - 1);
487 }
488
489 r600_blitter_begin(ctx, R600_BLIT);
490 util_blitter_blit(rctx->blitter, info);
491 r600_blitter_end(ctx);
492 }
493
494 void si_init_blit_functions(struct r600_context *rctx)
495 {
496 rctx->context.clear = r600_clear;
497 rctx->context.clear_render_target = r600_clear_render_target;
498 rctx->context.clear_depth_stencil = r600_clear_depth_stencil;
499 rctx->context.resource_copy_region = r600_resource_copy_region;
500 rctx->context.blit = si_blit;
501 }