r600g: advertise 32 fragment shaders inputs, not 34
[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 "r600_pipe.h"
24 #include "util/u_surface.h"
25 #include "util/u_blitter.h"
26 #include "util/u_format.h"
27
28 enum r600_blitter_op /* bitmask */
29 {
30 R600_SAVE_FRAGMENT_STATE = 1,
31 R600_SAVE_TEXTURES = 2,
32 R600_SAVE_FRAMEBUFFER = 4,
33 R600_DISABLE_RENDER_COND = 8,
34
35 R600_CLEAR = R600_SAVE_FRAGMENT_STATE,
36
37 R600_CLEAR_SURFACE = R600_SAVE_FRAGMENT_STATE | R600_SAVE_FRAMEBUFFER,
38
39 R600_COPY_BUFFER = R600_DISABLE_RENDER_COND,
40
41 R600_COPY_TEXTURE = R600_SAVE_FRAGMENT_STATE | R600_SAVE_FRAMEBUFFER | R600_SAVE_TEXTURES |
42 R600_DISABLE_RENDER_COND,
43
44 R600_BLIT = R600_SAVE_FRAGMENT_STATE | R600_SAVE_FRAMEBUFFER | R600_SAVE_TEXTURES |
45 R600_DISABLE_RENDER_COND,
46
47 R600_DECOMPRESS = R600_SAVE_FRAGMENT_STATE | R600_SAVE_FRAMEBUFFER | R600_DISABLE_RENDER_COND,
48
49 R600_COLOR_RESOLVE = R600_SAVE_FRAGMENT_STATE | R600_SAVE_FRAMEBUFFER | R600_DISABLE_RENDER_COND
50 };
51
52 static void r600_blitter_begin(struct pipe_context *ctx, enum r600_blitter_op op)
53 {
54 struct r600_context *rctx = (struct r600_context *)ctx;
55
56 r600_suspend_nontimer_queries(rctx);
57
58 util_blitter_save_vertex_buffers(rctx->blitter,
59 util_last_bit(rctx->vertex_buffer_state.enabled_mask),
60 rctx->vertex_buffer_state.vb);
61 util_blitter_save_vertex_elements(rctx->blitter, rctx->vertex_fetch_shader.cso);
62 util_blitter_save_vertex_shader(rctx->blitter, rctx->vs_shader);
63 util_blitter_save_so_targets(rctx->blitter, rctx->num_so_targets,
64 (struct pipe_stream_output_target**)rctx->so_targets);
65 util_blitter_save_rasterizer(rctx->blitter, rctx->rasterizer_state.cso);
66
67 if (op & R600_SAVE_FRAGMENT_STATE) {
68 util_blitter_save_viewport(rctx->blitter, &rctx->viewport.state);
69 util_blitter_save_scissor(rctx->blitter, &rctx->scissor.scissor);
70 util_blitter_save_fragment_shader(rctx->blitter, rctx->ps_shader);
71 util_blitter_save_blend(rctx->blitter, rctx->blend_state.cso);
72 util_blitter_save_depth_stencil_alpha(rctx->blitter, rctx->dsa_state.cso);
73 util_blitter_save_stencil_ref(rctx->blitter, &rctx->stencil_ref.pipe_state);
74 util_blitter_save_sample_mask(rctx->blitter, rctx->sample_mask.sample_mask);
75 }
76
77 if (op & R600_SAVE_FRAMEBUFFER)
78 util_blitter_save_framebuffer(rctx->blitter, &rctx->framebuffer.state);
79
80 if (op & R600_SAVE_TEXTURES) {
81 util_blitter_save_fragment_sampler_states(
82 rctx->blitter, util_last_bit(rctx->samplers[PIPE_SHADER_FRAGMENT].states.enabled_mask),
83 (void**)rctx->samplers[PIPE_SHADER_FRAGMENT].states.states);
84
85 util_blitter_save_fragment_sampler_views(
86 rctx->blitter, util_last_bit(rctx->samplers[PIPE_SHADER_FRAGMENT].views.enabled_mask),
87 (struct pipe_sampler_view**)rctx->samplers[PIPE_SHADER_FRAGMENT].views.views);
88 }
89
90 if ((op & R600_DISABLE_RENDER_COND) && rctx->current_render_cond) {
91 util_blitter_save_render_condition(rctx->blitter,
92 rctx->current_render_cond,
93 rctx->current_render_cond_mode);
94 }
95 }
96
97 static void r600_blitter_end(struct pipe_context *ctx)
98 {
99 struct r600_context *rctx = (struct r600_context *)ctx;
100 r600_resume_nontimer_queries(rctx);
101 }
102
103 static unsigned u_max_layer(struct pipe_resource *r, unsigned level)
104 {
105 switch (r->target) {
106 case PIPE_TEXTURE_CUBE:
107 return 6 - 1;
108 case PIPE_TEXTURE_3D:
109 return u_minify(r->depth0, level) - 1;
110 case PIPE_TEXTURE_1D_ARRAY:
111 case PIPE_TEXTURE_2D_ARRAY:
112 return r->array_size - 1;
113 default:
114 return 0;
115 }
116 }
117
118 static unsigned u_max_sample(struct pipe_resource *r)
119 {
120 return r->nr_samples ? r->nr_samples - 1 : 0;
121 }
122
123 void r600_blit_decompress_depth(struct pipe_context *ctx,
124 struct r600_texture *texture,
125 struct r600_texture *staging,
126 unsigned first_level, unsigned last_level,
127 unsigned first_layer, unsigned last_layer,
128 unsigned first_sample, unsigned last_sample)
129 {
130 struct r600_context *rctx = (struct r600_context *)ctx;
131 unsigned layer, level, sample, checked_last_layer, max_layer, max_sample;
132 struct r600_texture *flushed_depth_texture = staging ?
133 staging : texture->flushed_depth_texture;
134 const struct util_format_description *desc =
135 util_format_description(texture->resource.b.b.format);
136 float depth;
137
138 if (!staging && !texture->dirty_level_mask)
139 return;
140
141 max_sample = u_max_sample(&texture->resource.b.b);
142
143 /* XXX Decompressing MSAA depth textures is broken on R6xx.
144 * There is also a hardlock if CMASK and FMASK are not present.
145 * Just skip this until we find out how to fix it. */
146 if (rctx->chip_class == R600 && max_sample > 0) {
147 texture->dirty_level_mask = 0;
148 return;
149 }
150
151 if (rctx->family == CHIP_RV610 || rctx->family == CHIP_RV630 ||
152 rctx->family == CHIP_RV620 || rctx->family == CHIP_RV635)
153 depth = 0.0f;
154 else
155 depth = 1.0f;
156
157 /* Enable decompression in DB_RENDER_CONTROL */
158 rctx->db_misc_state.flush_depthstencil_through_cb = true;
159 rctx->db_misc_state.copy_depth = util_format_has_depth(desc);
160 rctx->db_misc_state.copy_stencil = util_format_has_stencil(desc);
161 rctx->db_misc_state.copy_sample = first_sample;
162 rctx->db_misc_state.atom.dirty = true;
163
164
165 for (level = first_level; level <= last_level; level++) {
166 if (!staging && !(texture->dirty_level_mask & (1 << level)))
167 continue;
168
169 /* The smaller the mipmap level, the less layers there are
170 * as far as 3D textures are concerned. */
171 max_layer = u_max_layer(&texture->resource.b.b, level);
172 checked_last_layer = last_layer < max_layer ? last_layer : max_layer;
173
174 for (layer = first_layer; layer <= checked_last_layer; layer++) {
175 for (sample = first_sample; sample <= last_sample; sample++) {
176 struct pipe_surface *zsurf, *cbsurf, surf_tmpl;
177
178 if (sample != rctx->db_misc_state.copy_sample) {
179 rctx->db_misc_state.copy_sample = sample;
180 rctx->db_misc_state.atom.dirty = true;
181 }
182
183 surf_tmpl.format = texture->resource.b.b.format;
184 surf_tmpl.u.tex.level = level;
185 surf_tmpl.u.tex.first_layer = layer;
186 surf_tmpl.u.tex.last_layer = layer;
187 surf_tmpl.usage = PIPE_BIND_DEPTH_STENCIL;
188
189 zsurf = ctx->create_surface(ctx, &texture->resource.b.b, &surf_tmpl);
190
191 surf_tmpl.format = flushed_depth_texture->resource.b.b.format;
192 surf_tmpl.u.tex.level = level;
193 surf_tmpl.u.tex.first_layer = layer;
194 surf_tmpl.u.tex.last_layer = layer;
195 surf_tmpl.usage = PIPE_BIND_RENDER_TARGET;
196 cbsurf = ctx->create_surface(ctx,
197 &flushed_depth_texture->resource.b.b, &surf_tmpl);
198
199 r600_blitter_begin(ctx, R600_DECOMPRESS);
200 util_blitter_custom_depth_stencil(rctx->blitter, zsurf, cbsurf, 1 << sample,
201 rctx->custom_dsa_flush, depth);
202 r600_blitter_end(ctx);
203
204 pipe_surface_reference(&zsurf, NULL);
205 pipe_surface_reference(&cbsurf, NULL);
206 }
207 }
208
209 /* The texture will always be dirty if some layers or samples aren't flushed.
210 * I don't think this case occurs often though. */
211 if (!staging &&
212 first_layer == 0 && last_layer == max_layer &&
213 first_sample == 0 && last_sample == max_sample) {
214 texture->dirty_level_mask &= ~(1 << level);
215 }
216 }
217
218 /* reenable compression in DB_RENDER_CONTROL */
219 rctx->db_misc_state.flush_depthstencil_through_cb = false;
220 rctx->db_misc_state.atom.dirty = true;
221 }
222
223 void r600_decompress_depth_textures(struct r600_context *rctx,
224 struct r600_samplerview_state *textures)
225 {
226 unsigned i;
227 unsigned depth_texture_mask = textures->compressed_depthtex_mask;
228
229 while (depth_texture_mask) {
230 struct pipe_sampler_view *view;
231 struct r600_texture *tex;
232
233 i = u_bit_scan(&depth_texture_mask);
234
235 view = &textures->views[i]->base;
236 assert(view);
237
238 tex = (struct r600_texture *)view->texture;
239 assert(tex->is_depth && !tex->is_flushing_texture);
240
241 r600_blit_decompress_depth(&rctx->context, tex, NULL,
242 view->u.tex.first_level, view->u.tex.last_level,
243 0, u_max_layer(&tex->resource.b.b, view->u.tex.first_level),
244 0, u_max_sample(&tex->resource.b.b));
245 }
246 }
247
248 static void r600_blit_decompress_color(struct pipe_context *ctx,
249 struct r600_texture *rtex,
250 unsigned first_level, unsigned last_level,
251 unsigned first_layer, unsigned last_layer)
252 {
253 struct r600_context *rctx = (struct r600_context *)ctx;
254 unsigned layer, level, checked_last_layer, max_layer;
255
256 assert(rctx->chip_class != CAYMAN);
257
258 if (!rtex->dirty_level_mask)
259 return;
260
261 for (level = first_level; level <= last_level; level++) {
262 if (!(rtex->dirty_level_mask & (1 << level)))
263 continue;
264
265 /* The smaller the mipmap level, the less layers there are
266 * as far as 3D textures are concerned. */
267 max_layer = u_max_layer(&rtex->resource.b.b, level);
268 checked_last_layer = last_layer < max_layer ? last_layer : max_layer;
269
270 for (layer = first_layer; layer <= checked_last_layer; layer++) {
271 struct pipe_surface *cbsurf, surf_tmpl;
272
273 surf_tmpl.format = rtex->resource.b.b.format;
274 surf_tmpl.u.tex.level = level;
275 surf_tmpl.u.tex.first_layer = layer;
276 surf_tmpl.u.tex.last_layer = layer;
277 surf_tmpl.usage = PIPE_BIND_RENDER_TARGET;
278 cbsurf = ctx->create_surface(ctx, &rtex->resource.b.b, &surf_tmpl);
279
280 r600_blitter_begin(ctx, R600_DECOMPRESS);
281 util_blitter_custom_color(rctx->blitter, cbsurf,
282 rctx->custom_blend_decompress);
283 r600_blitter_end(ctx);
284
285 pipe_surface_reference(&cbsurf, NULL);
286 }
287
288 /* The texture will always be dirty if some layers or samples aren't flushed.
289 * I don't think this case occurs often though. */
290 if (first_layer == 0 && last_layer == max_layer) {
291 rtex->dirty_level_mask &= ~(1 << level);
292 }
293 }
294 }
295
296 void r600_decompress_color_textures(struct r600_context *rctx,
297 struct r600_samplerview_state *textures)
298 {
299 unsigned i;
300 unsigned mask = textures->compressed_colortex_mask;
301
302 /* Cayman cannot decompress an MSAA colorbuffer,
303 * but it can read it compressed, so skip this. */
304 assert(rctx->chip_class != CAYMAN);
305 if (rctx->chip_class == CAYMAN) {
306 return;
307 }
308
309 while (mask) {
310 struct pipe_sampler_view *view;
311 struct r600_texture *tex;
312
313 i = u_bit_scan(&mask);
314
315 view = &textures->views[i]->base;
316 assert(view);
317
318 tex = (struct r600_texture *)view->texture;
319 assert(tex->cmask_size && tex->fmask_size);
320
321 r600_blit_decompress_color(&rctx->context, tex,
322 view->u.tex.first_level, view->u.tex.last_level,
323 0, u_max_layer(&tex->resource.b.b, view->u.tex.first_level));
324 }
325 }
326
327 /* Helper for decompressing a portion of a color or depth resource before
328 * blitting if any decompression is needed.
329 * The driver doesn't decompress resources automatically while u_blitter is
330 * rendering. */
331 static bool r600_decompress_subresource(struct pipe_context *ctx,
332 struct pipe_resource *tex,
333 unsigned level,
334 unsigned first_layer, unsigned last_layer)
335 {
336 struct r600_context *rctx = (struct r600_context *)ctx;
337 struct r600_texture *rtex = (struct r600_texture*)tex;
338
339 if (rtex->is_depth && !rtex->is_flushing_texture) {
340 if (!r600_init_flushed_depth_texture(ctx, tex, NULL))
341 return false; /* error */
342
343 r600_blit_decompress_depth(ctx, rtex, NULL,
344 level, level,
345 first_layer, last_layer,
346 0, u_max_sample(tex));
347 } else if (rctx->chip_class != CAYMAN && rtex->fmask_size && rtex->cmask_size) {
348 r600_blit_decompress_color(ctx, rtex, level, level,
349 first_layer, last_layer);
350 }
351 return true;
352 }
353
354 static boolean is_simple_msaa_resolve(const struct pipe_blit_info *info)
355 {
356 unsigned dst_width = u_minify(info->dst.resource->width0, info->dst.level);
357 unsigned dst_height = u_minify(info->dst.resource->height0, info->dst.level);
358
359 return info->dst.resource->format == info->src.resource->format &&
360 info->dst.resource->format == info->dst.format &&
361 info->src.resource->format == info->src.format &&
362 !info->scissor_enable &&
363 info->mask == PIPE_MASK_RGBA &&
364 dst_width == info->src.resource->width0 &&
365 dst_height == info->src.resource->height0 &&
366 info->dst.box.x == 0 &&
367 info->dst.box.y == 0 &&
368 info->dst.box.width == dst_width &&
369 info->dst.box.height == dst_height &&
370 info->src.box.x == 0 &&
371 info->src.box.y == 0 &&
372 info->src.box.width == dst_width &&
373 info->src.box.height == dst_height;
374 }
375
376 static void r600_clear(struct pipe_context *ctx, unsigned buffers,
377 const union pipe_color_union *color,
378 double depth, unsigned stencil)
379 {
380 struct r600_context *rctx = (struct r600_context *)ctx;
381 struct pipe_framebuffer_state *fb = &rctx->framebuffer.state;
382
383 r600_blitter_begin(ctx, R600_CLEAR);
384 util_blitter_clear(rctx->blitter, fb->width, fb->height,
385 fb->nr_cbufs, buffers, fb->nr_cbufs ? fb->cbufs[0]->format : PIPE_FORMAT_NONE,
386 color, depth, stencil);
387 r600_blitter_end(ctx);
388 }
389
390 static void r600_clear_render_target(struct pipe_context *ctx,
391 struct pipe_surface *dst,
392 const union pipe_color_union *color,
393 unsigned dstx, unsigned dsty,
394 unsigned width, unsigned height)
395 {
396 struct r600_context *rctx = (struct r600_context *)ctx;
397
398 r600_blitter_begin(ctx, R600_CLEAR_SURFACE);
399 util_blitter_clear_render_target(rctx->blitter, dst, color,
400 dstx, dsty, width, height);
401 r600_blitter_end(ctx);
402 }
403
404 static void r600_clear_depth_stencil(struct pipe_context *ctx,
405 struct pipe_surface *dst,
406 unsigned clear_flags,
407 double depth,
408 unsigned stencil,
409 unsigned dstx, unsigned dsty,
410 unsigned width, unsigned height)
411 {
412 struct r600_context *rctx = (struct r600_context *)ctx;
413
414 r600_blitter_begin(ctx, R600_CLEAR_SURFACE);
415 util_blitter_clear_depth_stencil(rctx->blitter, dst, clear_flags, depth, stencil,
416 dstx, dsty, width, height);
417 r600_blitter_end(ctx);
418 }
419
420 void r600_copy_buffer(struct pipe_context *ctx, struct
421 pipe_resource *dst, unsigned dstx,
422 struct pipe_resource *src, const struct pipe_box *src_box)
423 {
424 struct r600_context *rctx = (struct r600_context*)ctx;
425
426 if (rctx->screen->has_streamout &&
427 /* Require dword alignment. */
428 dstx % 4 == 0 && src_box->x % 4 == 0 && src_box->width % 4 == 0) {
429 r600_blitter_begin(ctx, R600_COPY_BUFFER);
430 util_blitter_copy_buffer(rctx->blitter, dst, dstx, src, src_box->x, src_box->width);
431 r600_blitter_end(ctx);
432 } else {
433 util_resource_copy_region(ctx, dst, 0, dstx, 0, 0, src, 0, src_box);
434 }
435 }
436
437 static bool util_format_is_subsampled_2x1_32bpp(enum pipe_format format)
438 {
439 const struct util_format_description *desc = util_format_description(format);
440
441 return desc->layout == UTIL_FORMAT_LAYOUT_SUBSAMPLED &&
442 desc->block.width == 2 &&
443 desc->block.height == 1 &&
444 desc->block.bits == 32;
445 }
446
447 static void r600_resource_copy_region(struct pipe_context *ctx,
448 struct pipe_resource *dst,
449 unsigned dst_level,
450 unsigned dstx, unsigned dsty, unsigned dstz,
451 struct pipe_resource *src,
452 unsigned src_level,
453 const struct pipe_box *src_box)
454 {
455 struct r600_context *rctx = (struct r600_context *)ctx;
456 struct r600_texture *rsrc = (struct r600_texture*)src;
457 struct r600_texture *rdst = (struct r600_texture*)dst;
458 struct pipe_surface *dst_view, dst_templ;
459 struct pipe_sampler_view src_templ, *src_view;
460 unsigned dst_width, dst_height, src_width0, src_height0, src_widthFL, src_heightFL;
461 struct pipe_box sbox;
462
463 /* Handle buffers first. */
464 if (dst->target == PIPE_BUFFER && src->target == PIPE_BUFFER) {
465 r600_copy_buffer(ctx, dst, dstx, src, src_box);
466 return;
467 }
468
469 assert(u_max_sample(dst) == u_max_sample(src));
470
471 /* The driver doesn't decompress resources automatically while
472 * u_blitter is rendering. */
473 if (!r600_decompress_subresource(ctx, src, src_level,
474 src_box->z, src_box->z + src_box->depth - 1)) {
475 return; /* error */
476 }
477
478 dst_width = rdst->surface.level[dst_level].npix_x;
479 dst_height = rdst->surface.level[dst_level].npix_y;
480 src_width0 = src->width0;
481 src_height0 = src->height0;
482 src_widthFL = rsrc->surface.level[src_level].npix_x;
483 src_heightFL = rsrc->surface.level[src_level].npix_y;
484
485 util_blitter_default_dst_texture(&dst_templ, dst, dst_level, dstz, src_box);
486 util_blitter_default_src_texture(&src_templ, src, src_level);
487
488 if (util_format_is_compressed(src->format)) {
489 unsigned blocksize = util_format_get_blocksize(src->format);
490
491 if (blocksize == 8)
492 src_templ.format = PIPE_FORMAT_R16G16B16A16_UINT; /* 64-bit block */
493 else
494 src_templ.format = PIPE_FORMAT_R32G32B32A32_UINT; /* 128-bit block */
495 dst_templ.format = src_templ.format;
496
497 dst_width = util_format_get_nblocksx(dst->format, rdst->surface.level[dst_level].npix_x);
498 dst_height = util_format_get_nblocksy(dst->format, rdst->surface.level[dst_level].npix_y);
499 src_width0 = util_format_get_nblocksx(src->format, src->width0);
500 src_height0 = util_format_get_nblocksy(src->format, src->height0);
501 src_widthFL = util_format_get_nblocksx(src->format, rsrc->surface.level[src_level].npix_x);
502 src_heightFL = util_format_get_nblocksy(src->format, rsrc->surface.level[src_level].npix_y);
503
504 dstx = util_format_get_nblocksx(dst->format, dstx);
505 dsty = util_format_get_nblocksy(dst->format, dsty);
506
507 sbox.x = util_format_get_nblocksx(src->format, src_box->x);
508 sbox.y = util_format_get_nblocksy(src->format, src_box->y);
509 sbox.z = src_box->z;
510 sbox.width = util_format_get_nblocksx(src->format, src_box->width);
511 sbox.height = util_format_get_nblocksy(src->format, src_box->height);
512 sbox.depth = src_box->depth;
513 src_box = &sbox;
514 } else if (!util_blitter_is_copy_supported(rctx->blitter, dst, src,
515 PIPE_MASK_RGBAZS)) {
516 if (util_format_is_subsampled_2x1_32bpp(src->format)) {
517
518 src_templ.format = PIPE_FORMAT_R8G8B8A8_UINT;
519 dst_templ.format = PIPE_FORMAT_R8G8B8A8_UINT;
520
521 dst_width = util_format_get_nblocksx(dst->format, rdst->surface.level[dst_level].npix_x);
522 src_width0 = util_format_get_nblocksx(src->format, src->width0);
523 src_widthFL = util_format_get_nblocksx(src->format, rsrc->surface.level[src_level].npix_x);
524
525 dstx = util_format_get_nblocksx(dst->format, dstx);
526
527 sbox = *src_box;
528 sbox.x = util_format_get_nblocksx(src->format, src_box->x);
529 sbox.width = util_format_get_nblocksx(src->format, src_box->width);
530 src_box = &sbox;
531 } else {
532 unsigned blocksize = util_format_get_blocksize(src->format);
533
534 switch (blocksize) {
535 case 1:
536 dst_templ.format = PIPE_FORMAT_R8_UNORM;
537 src_templ.format = PIPE_FORMAT_R8_UNORM;
538 break;
539 case 4:
540 dst_templ.format = PIPE_FORMAT_R8G8B8A8_UNORM;
541 src_templ.format = PIPE_FORMAT_R8G8B8A8_UNORM;
542 break;
543 default:
544 fprintf(stderr, "Unhandled format %s with blocksize %u\n",
545 util_format_short_name(src->format), blocksize);
546 assert(0);
547 }
548 }
549 }
550
551 dst_view = r600_create_surface_custom(ctx, dst, &dst_templ, dst_width, dst_height);
552
553 if (rctx->chip_class >= EVERGREEN) {
554 src_view = evergreen_create_sampler_view_custom(ctx, src, &src_templ,
555 src_width0, src_height0);
556 } else {
557 src_view = r600_create_sampler_view_custom(ctx, src, &src_templ,
558 src_widthFL, src_heightFL);
559 }
560
561 /* Copy. */
562 /* XXX Multisample texturing is unimplemented on Cayman. In the meantime,
563 * copy only the first sample (which is the only one that is uncompressed
564 * and therefore doesn't return garbage). */
565 r600_blitter_begin(ctx, R600_COPY_TEXTURE);
566 util_blitter_blit_generic(rctx->blitter, dst_view, dstx, dsty,
567 abs(src_box->width), abs(src_box->height),
568 src_view, src_box, src_width0, src_height0,
569 PIPE_MASK_RGBAZS, PIPE_TEX_FILTER_NEAREST, NULL,
570 rctx->chip_class != CAYMAN);
571 r600_blitter_end(ctx);
572
573 pipe_surface_reference(&dst_view, NULL);
574 pipe_sampler_view_reference(&src_view, NULL);
575 }
576
577 /* For MSAA integer resolving to work, we change the format to NORM using this function. */
578 static enum pipe_format int_to_norm_format(enum pipe_format format)
579 {
580 switch (format) {
581 #define REPLACE_FORMAT_SIGN(format,sign) \
582 case PIPE_FORMAT_##format##_##sign##INT: \
583 return PIPE_FORMAT_##format##_##sign##NORM
584 #define REPLACE_FORMAT(format) \
585 REPLACE_FORMAT_SIGN(format, U); \
586 REPLACE_FORMAT_SIGN(format, S)
587
588 REPLACE_FORMAT_SIGN(B10G10R10A2, U);
589 REPLACE_FORMAT(R8);
590 REPLACE_FORMAT(R8G8);
591 REPLACE_FORMAT(R8G8B8);
592 REPLACE_FORMAT(R8G8B8A8);
593 REPLACE_FORMAT(A8);
594 REPLACE_FORMAT(I8);
595 REPLACE_FORMAT(L8);
596 REPLACE_FORMAT(L8A8);
597 REPLACE_FORMAT(R16);
598 REPLACE_FORMAT(R16G16);
599 REPLACE_FORMAT(R16G16B16);
600 REPLACE_FORMAT(R16G16B16A16);
601 REPLACE_FORMAT(A16);
602 REPLACE_FORMAT(I16);
603 REPLACE_FORMAT(L16);
604 REPLACE_FORMAT(L16A16);
605
606 #undef REPLACE_FORMAT
607 #undef REPLACE_FORMAT_SIGN
608 default:
609 return format;
610 }
611 }
612
613 static void r600_msaa_color_resolve(struct pipe_context *ctx,
614 const struct pipe_blit_info *info)
615 {
616 struct r600_context *rctx = (struct r600_context *)ctx;
617 struct pipe_screen *screen = ctx->screen;
618 struct pipe_resource *tmp, templ;
619 struct pipe_blit_info blit;
620 unsigned sample_mask =
621 rctx->chip_class == CAYMAN ? ~0 :
622 ((1ull << MAX2(1, info->src.resource->nr_samples)) - 1);
623
624 assert(info->src.level == 0);
625 assert(info->src.box.depth == 1);
626 assert(info->dst.box.depth == 1);
627
628 if (is_simple_msaa_resolve(info)) {
629 r600_blitter_begin(ctx, R600_COLOR_RESOLVE);
630 util_blitter_custom_resolve_color(rctx->blitter,
631 info->dst.resource, info->dst.level,
632 info->dst.box.z,
633 info->src.resource, info->src.box.z,
634 sample_mask, rctx->custom_blend_resolve,
635 int_to_norm_format(info->dst.format));
636 r600_blitter_end(ctx);
637 return;
638 }
639
640 /* resolve into a temporary texture, then blit */
641 templ.target = PIPE_TEXTURE_2D;
642 templ.format = info->src.resource->format;
643 templ.width0 = info->src.resource->width0;
644 templ.height0 = info->src.resource->height0;
645 templ.depth0 = 1;
646 templ.array_size = 1;
647 templ.last_level = 0;
648 templ.nr_samples = 0;
649 templ.usage = PIPE_USAGE_STATIC;
650 templ.bind = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
651 templ.flags = 0;
652
653 tmp = screen->resource_create(screen, &templ);
654
655 /* resolve */
656 r600_blitter_begin(ctx, R600_COLOR_RESOLVE);
657 util_blitter_custom_resolve_color(rctx->blitter,
658 tmp, 0, 0,
659 info->src.resource, info->src.box.z,
660 sample_mask, rctx->custom_blend_resolve,
661 int_to_norm_format(tmp->format));
662 r600_blitter_end(ctx);
663
664 /* blit */
665 blit = *info;
666 blit.src.resource = tmp;
667 blit.src.box.z = 0;
668
669 r600_blitter_begin(ctx, R600_BLIT);
670 util_blitter_blit(rctx->blitter, &blit);
671 r600_blitter_end(ctx);
672
673 pipe_resource_reference(&tmp, NULL);
674 }
675
676 static void r600_blit(struct pipe_context *ctx,
677 const struct pipe_blit_info *info)
678 {
679 struct r600_context *rctx = (struct r600_context*)ctx;
680
681 assert(util_blitter_is_blit_supported(rctx->blitter, info));
682
683 if (info->src.resource->nr_samples > 1 &&
684 info->dst.resource->nr_samples <= 1 &&
685 !util_format_is_depth_or_stencil(info->src.resource->format) &&
686 !util_format_is_pure_integer(int_to_norm_format(info->src.resource->format))) {
687 r600_msaa_color_resolve(ctx, info);
688 return;
689 }
690
691 /* The driver doesn't decompress resources automatically while
692 * u_blitter is rendering. */
693 if (!r600_decompress_subresource(ctx, info->src.resource, info->src.level,
694 info->src.box.z,
695 info->src.box.z + info->src.box.depth - 1)) {
696 return; /* error */
697 }
698
699 r600_blitter_begin(ctx, R600_BLIT);
700 util_blitter_blit(rctx->blitter, info);
701 r600_blitter_end(ctx);
702 }
703
704 void r600_init_blit_functions(struct r600_context *rctx)
705 {
706 rctx->context.clear = r600_clear;
707 rctx->context.clear_render_target = r600_clear_render_target;
708 rctx->context.clear_depth_stencil = r600_clear_depth_stencil;
709 rctx->context.resource_copy_region = r600_resource_copy_region;
710 rctx->context.blit = r600_blit;
711 }