r600g: handle NULL colorbuffers correctly on Evergreen
[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 #include "evergreend.h"
28
29 enum r600_blitter_op /* bitmask */
30 {
31 R600_SAVE_FRAGMENT_STATE = 1,
32 R600_SAVE_TEXTURES = 2,
33 R600_SAVE_FRAMEBUFFER = 4,
34 R600_DISABLE_RENDER_COND = 8,
35
36 R600_CLEAR = R600_SAVE_FRAGMENT_STATE,
37
38 R600_CLEAR_SURFACE = R600_SAVE_FRAGMENT_STATE | R600_SAVE_FRAMEBUFFER,
39
40 R600_COPY_BUFFER = R600_DISABLE_RENDER_COND,
41
42 R600_COPY_TEXTURE = R600_SAVE_FRAGMENT_STATE | R600_SAVE_FRAMEBUFFER | R600_SAVE_TEXTURES |
43 R600_DISABLE_RENDER_COND,
44
45 R600_BLIT = R600_SAVE_FRAGMENT_STATE | R600_SAVE_FRAMEBUFFER | R600_SAVE_TEXTURES |
46 R600_DISABLE_RENDER_COND,
47
48 R600_DECOMPRESS = R600_SAVE_FRAGMENT_STATE | R600_SAVE_FRAMEBUFFER | R600_DISABLE_RENDER_COND,
49
50 R600_COLOR_RESOLVE = R600_SAVE_FRAGMENT_STATE | R600_SAVE_FRAMEBUFFER | R600_DISABLE_RENDER_COND
51 };
52
53 static void r600_blitter_begin(struct pipe_context *ctx, enum r600_blitter_op op)
54 {
55 struct r600_context *rctx = (struct r600_context *)ctx;
56
57 r600_suspend_nontimer_queries(rctx);
58
59 util_blitter_save_vertex_buffer_slot(rctx->blitter, rctx->vertex_buffer_state.vb);
60 util_blitter_save_vertex_elements(rctx->blitter, rctx->vertex_fetch_shader.cso);
61 util_blitter_save_vertex_shader(rctx->blitter, rctx->vs_shader);
62 util_blitter_save_so_targets(rctx->blitter, rctx->b.streamout.num_targets,
63 (struct pipe_stream_output_target**)rctx->b.streamout.targets);
64 util_blitter_save_rasterizer(rctx->blitter, rctx->rasterizer_state.cso);
65
66 if (op & R600_SAVE_FRAGMENT_STATE) {
67 util_blitter_save_viewport(rctx->blitter, &rctx->viewport.state);
68 util_blitter_save_scissor(rctx->blitter, &rctx->scissor.scissor);
69 util_blitter_save_fragment_shader(rctx->blitter, rctx->ps_shader);
70 util_blitter_save_blend(rctx->blitter, rctx->blend_state.cso);
71 util_blitter_save_depth_stencil_alpha(rctx->blitter, rctx->dsa_state.cso);
72 util_blitter_save_stencil_ref(rctx->blitter, &rctx->stencil_ref.pipe_state);
73 util_blitter_save_sample_mask(rctx->blitter, rctx->sample_mask.sample_mask);
74 }
75
76 if (op & R600_SAVE_FRAMEBUFFER)
77 util_blitter_save_framebuffer(rctx->blitter, &rctx->framebuffer.state);
78
79 if (op & R600_SAVE_TEXTURES) {
80 util_blitter_save_fragment_sampler_states(
81 rctx->blitter, util_last_bit(rctx->samplers[PIPE_SHADER_FRAGMENT].states.enabled_mask),
82 (void**)rctx->samplers[PIPE_SHADER_FRAGMENT].states.states);
83
84 util_blitter_save_fragment_sampler_views(
85 rctx->blitter, util_last_bit(rctx->samplers[PIPE_SHADER_FRAGMENT].views.enabled_mask),
86 (struct pipe_sampler_view**)rctx->samplers[PIPE_SHADER_FRAGMENT].views.views);
87 }
88
89 if ((op & R600_DISABLE_RENDER_COND) && rctx->current_render_cond) {
90 util_blitter_save_render_condition(rctx->blitter,
91 rctx->current_render_cond,
92 rctx->current_render_cond_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_sample(struct pipe_resource *r)
104 {
105 return r->nr_samples ? r->nr_samples - 1 : 0;
106 }
107
108 static void r600_blit_decompress_depth(struct pipe_context *ctx,
109 struct r600_texture *texture,
110 struct r600_texture *staging,
111 unsigned first_level, unsigned last_level,
112 unsigned first_layer, unsigned last_layer,
113 unsigned first_sample, unsigned last_sample)
114 {
115 struct r600_context *rctx = (struct r600_context *)ctx;
116 unsigned layer, level, sample, checked_last_layer, max_layer, max_sample;
117 struct r600_texture *flushed_depth_texture = staging ?
118 staging : texture->flushed_depth_texture;
119 const struct util_format_description *desc =
120 util_format_description(texture->resource.b.b.format);
121 float depth;
122
123 if (!staging && !texture->dirty_level_mask)
124 return;
125
126 max_sample = u_max_sample(&texture->resource.b.b);
127
128 /* XXX Decompressing MSAA depth textures is broken on R6xx.
129 * There is also a hardlock if CMASK and FMASK are not present.
130 * Just skip this until we find out how to fix it. */
131 if (rctx->b.chip_class == R600 && max_sample > 0) {
132 texture->dirty_level_mask = 0;
133 return;
134 }
135
136 if (rctx->b.family == CHIP_RV610 || rctx->b.family == CHIP_RV630 ||
137 rctx->b.family == CHIP_RV620 || rctx->b.family == CHIP_RV635)
138 depth = 0.0f;
139 else
140 depth = 1.0f;
141
142 /* Enable decompression in DB_RENDER_CONTROL */
143 rctx->db_misc_state.flush_depthstencil_through_cb = true;
144 rctx->db_misc_state.copy_depth = util_format_has_depth(desc);
145 rctx->db_misc_state.copy_stencil = util_format_has_stencil(desc);
146 rctx->db_misc_state.copy_sample = first_sample;
147 rctx->db_misc_state.atom.dirty = true;
148
149 for (level = first_level; level <= last_level; level++) {
150 if (!staging && !(texture->dirty_level_mask & (1 << level)))
151 continue;
152
153 /* The smaller the mipmap level, the less layers there are
154 * as far as 3D textures are concerned. */
155 max_layer = util_max_layer(&texture->resource.b.b, level);
156 checked_last_layer = last_layer < max_layer ? last_layer : max_layer;
157
158 for (layer = first_layer; layer <= checked_last_layer; layer++) {
159 for (sample = first_sample; sample <= last_sample; sample++) {
160 struct pipe_surface *zsurf, *cbsurf, surf_tmpl;
161
162 if (sample != rctx->db_misc_state.copy_sample) {
163 rctx->db_misc_state.copy_sample = sample;
164 rctx->db_misc_state.atom.dirty = true;
165 }
166
167 surf_tmpl.format = texture->resource.b.b.format;
168 surf_tmpl.u.tex.level = level;
169 surf_tmpl.u.tex.first_layer = layer;
170 surf_tmpl.u.tex.last_layer = layer;
171
172 zsurf = ctx->create_surface(ctx, &texture->resource.b.b, &surf_tmpl);
173
174 surf_tmpl.format = flushed_depth_texture->resource.b.b.format;
175 cbsurf = ctx->create_surface(ctx,
176 &flushed_depth_texture->resource.b.b, &surf_tmpl);
177
178 r600_blitter_begin(ctx, R600_DECOMPRESS);
179 util_blitter_custom_depth_stencil(rctx->blitter, zsurf, cbsurf, 1 << sample,
180 rctx->custom_dsa_flush, depth);
181 r600_blitter_end(ctx);
182
183 pipe_surface_reference(&zsurf, NULL);
184 pipe_surface_reference(&cbsurf, NULL);
185 }
186 }
187
188 /* The texture will always be dirty if some layers or samples aren't flushed.
189 * I don't think this case occurs often though. */
190 if (!staging &&
191 first_layer == 0 && last_layer == max_layer &&
192 first_sample == 0 && last_sample == max_sample) {
193 texture->dirty_level_mask &= ~(1 << level);
194 }
195 }
196
197 /* reenable compression in DB_RENDER_CONTROL */
198 rctx->db_misc_state.flush_depthstencil_through_cb = false;
199 rctx->db_misc_state.atom.dirty = true;
200 }
201
202 static void r600_blit_decompress_depth_in_place(struct r600_context *rctx,
203 struct r600_texture *texture,
204 unsigned first_level, unsigned last_level,
205 unsigned first_layer, unsigned last_layer)
206 {
207 struct pipe_surface *zsurf, surf_tmpl = {{0}};
208 unsigned layer, max_layer, checked_last_layer, level;
209
210 /* Enable decompression in DB_RENDER_CONTROL */
211 rctx->db_misc_state.flush_depthstencil_in_place = true;
212 rctx->db_misc_state.atom.dirty = true;
213
214 surf_tmpl.format = texture->resource.b.b.format;
215
216 for (level = first_level; level <= last_level; level++) {
217 if (!(texture->dirty_level_mask & (1 << level)))
218 continue;
219
220 surf_tmpl.u.tex.level = level;
221
222 /* The smaller the mipmap level, the less layers there are
223 * as far as 3D textures are concerned. */
224 max_layer = util_max_layer(&texture->resource.b.b, level);
225 checked_last_layer = last_layer < max_layer ? last_layer : max_layer;
226
227 for (layer = first_layer; layer <= checked_last_layer; layer++) {
228 surf_tmpl.u.tex.first_layer = layer;
229 surf_tmpl.u.tex.last_layer = layer;
230
231 zsurf = rctx->b.b.create_surface(&rctx->b.b, &texture->resource.b.b, &surf_tmpl);
232
233 r600_blitter_begin(&rctx->b.b, R600_DECOMPRESS);
234 util_blitter_custom_depth_stencil(rctx->blitter, zsurf, NULL, ~0,
235 rctx->custom_dsa_flush, 1.0f);
236 r600_blitter_end(&rctx->b.b);
237
238 pipe_surface_reference(&zsurf, NULL);
239 }
240
241 /* The texture will always be dirty if some layers or samples aren't flushed.
242 * I don't think this case occurs often though. */
243 if (first_layer == 0 && last_layer == max_layer) {
244 texture->dirty_level_mask &= ~(1 << level);
245 }
246 }
247
248 /* Disable decompression in DB_RENDER_CONTROL */
249 rctx->db_misc_state.flush_depthstencil_in_place = false;
250 rctx->db_misc_state.atom.dirty = true;
251 }
252
253 void r600_decompress_depth_textures(struct r600_context *rctx,
254 struct r600_samplerview_state *textures)
255 {
256 unsigned i;
257 unsigned depth_texture_mask = textures->compressed_depthtex_mask;
258
259 while (depth_texture_mask) {
260 struct pipe_sampler_view *view;
261 struct r600_texture *tex;
262
263 i = u_bit_scan(&depth_texture_mask);
264
265 view = &textures->views[i]->base;
266 assert(view);
267
268 tex = (struct r600_texture *)view->texture;
269 assert(tex->is_depth && !tex->is_flushing_texture);
270
271 if (rctx->b.chip_class >= EVERGREEN ||
272 r600_can_read_depth(tex)) {
273 r600_blit_decompress_depth_in_place(rctx, tex,
274 view->u.tex.first_level, view->u.tex.last_level,
275 0, util_max_layer(&tex->resource.b.b, view->u.tex.first_level));
276 } else {
277 r600_blit_decompress_depth(&rctx->b.b, tex, NULL,
278 view->u.tex.first_level, view->u.tex.last_level,
279 0, util_max_layer(&tex->resource.b.b, view->u.tex.first_level),
280 0, u_max_sample(&tex->resource.b.b));
281 }
282 }
283 }
284
285 static void r600_blit_decompress_color(struct pipe_context *ctx,
286 struct r600_texture *rtex,
287 unsigned first_level, unsigned last_level,
288 unsigned first_layer, unsigned last_layer)
289 {
290 struct r600_context *rctx = (struct r600_context *)ctx;
291 unsigned layer, level, checked_last_layer, max_layer;
292
293 if (!rtex->dirty_level_mask)
294 return;
295
296 for (level = first_level; level <= last_level; level++) {
297 if (!(rtex->dirty_level_mask & (1 << level)))
298 continue;
299
300 /* The smaller the mipmap level, the less layers there are
301 * as far as 3D textures are concerned. */
302 max_layer = util_max_layer(&rtex->resource.b.b, level);
303 checked_last_layer = last_layer < max_layer ? last_layer : max_layer;
304
305 for (layer = first_layer; layer <= checked_last_layer; layer++) {
306 struct pipe_surface *cbsurf, surf_tmpl;
307
308 surf_tmpl.format = rtex->resource.b.b.format;
309 surf_tmpl.u.tex.level = level;
310 surf_tmpl.u.tex.first_layer = layer;
311 surf_tmpl.u.tex.last_layer = layer;
312 cbsurf = ctx->create_surface(ctx, &rtex->resource.b.b, &surf_tmpl);
313
314 r600_blitter_begin(ctx, R600_DECOMPRESS);
315 util_blitter_custom_color(rctx->blitter, cbsurf,
316 rtex->fmask.size ? rctx->custom_blend_decompress : rctx->custom_blend_fastclear);
317 r600_blitter_end(ctx);
318
319 pipe_surface_reference(&cbsurf, NULL);
320 }
321
322 /* The texture will always be dirty if some layers aren't flushed.
323 * I don't think this case occurs often though. */
324 if (first_layer == 0 && last_layer == max_layer) {
325 rtex->dirty_level_mask &= ~(1 << level);
326 }
327 }
328 }
329
330 void r600_decompress_color_textures(struct r600_context *rctx,
331 struct r600_samplerview_state *textures)
332 {
333 unsigned i;
334 unsigned mask = textures->compressed_colortex_mask;
335
336 while (mask) {
337 struct pipe_sampler_view *view;
338 struct r600_texture *tex;
339
340 i = u_bit_scan(&mask);
341
342 view = &textures->views[i]->base;
343 assert(view);
344
345 tex = (struct r600_texture *)view->texture;
346 assert(tex->cmask.size);
347
348 r600_blit_decompress_color(&rctx->b.b, tex,
349 view->u.tex.first_level, view->u.tex.last_level,
350 0, util_max_layer(&tex->resource.b.b, view->u.tex.first_level));
351 }
352 }
353
354 /* Helper for decompressing a portion of a color or depth resource before
355 * blitting if any decompression is needed.
356 * The driver doesn't decompress resources automatically while u_blitter is
357 * rendering. */
358 static bool r600_decompress_subresource(struct pipe_context *ctx,
359 struct pipe_resource *tex,
360 unsigned level,
361 unsigned first_layer, unsigned last_layer)
362 {
363 struct r600_context *rctx = (struct r600_context *)ctx;
364 struct r600_texture *rtex = (struct r600_texture*)tex;
365
366 if (rtex->is_depth && !rtex->is_flushing_texture) {
367 if (rctx->b.chip_class >= EVERGREEN ||
368 r600_can_read_depth(rtex)) {
369 r600_blit_decompress_depth_in_place(rctx, rtex,
370 level, level,
371 first_layer, last_layer);
372 } else {
373 if (!r600_init_flushed_depth_texture(ctx, tex, NULL))
374 return false; /* error */
375
376 r600_blit_decompress_depth(ctx, rtex, NULL,
377 level, level,
378 first_layer, last_layer,
379 0, u_max_sample(tex));
380 }
381 } else if (rtex->cmask.size) {
382 r600_blit_decompress_color(ctx, rtex, level, level,
383 first_layer, last_layer);
384 }
385 return true;
386 }
387
388 static void r600_clear_buffer(struct pipe_context *ctx, struct pipe_resource *dst,
389 unsigned offset, unsigned size, unsigned value);
390
391 static void evergreen_set_clear_color(struct pipe_surface *cbuf,
392 const union pipe_color_union *color)
393 {
394 unsigned *clear_value = ((struct r600_texture *)cbuf->texture)->color_clear_value;
395 union util_color uc;
396
397 memset(&uc, 0, sizeof(uc));
398
399 if (util_format_is_pure_uint(cbuf->format)) {
400 util_format_write_4ui(cbuf->format, color->ui, 0, &uc, 0, 0, 0, 1, 1);
401 } else if (util_format_is_pure_sint(cbuf->format)) {
402 util_format_write_4i(cbuf->format, color->i, 0, &uc, 0, 0, 0, 1, 1);
403 } else {
404 util_pack_color(color->f, cbuf->format, &uc);
405 }
406
407 memcpy(clear_value, &uc, 2 * sizeof(uint32_t));
408 }
409
410 static void evergreen_check_alloc_cmask(struct pipe_context *ctx,
411 struct pipe_surface *cbuf)
412 {
413 struct r600_context *rctx = (struct r600_context *)ctx;
414 struct r600_texture *tex = (struct r600_texture *)cbuf->texture;
415 struct r600_surface *surf = (struct r600_surface *)cbuf;
416
417 if (tex->cmask_buffer)
418 return;
419
420 r600_texture_init_cmask(&rctx->screen->b, tex);
421
422 /* update colorbuffer state bits */
423 if (tex->cmask_buffer != NULL) {
424 uint64_t va = r600_resource_va(rctx->b.b.screen, &tex->cmask_buffer->b.b);
425 surf->cb_color_cmask = va >> 8;
426 surf->cb_color_cmask_slice = S_028C80_TILE_MAX(tex->cmask.slice_tile_max);
427 surf->cb_color_info |= S_028C70_FAST_CLEAR(1);
428 }
429 }
430
431 static bool can_fast_clear_color(struct pipe_context *ctx)
432 {
433 struct r600_context *rctx = (struct r600_context *)ctx;
434 struct pipe_framebuffer_state *fb = &rctx->framebuffer.state;
435 int i;
436
437 if (rctx->b.chip_class < EVERGREEN) {
438 return false;
439 }
440
441 for (i = 0; i < fb->nr_cbufs; i++) {
442 struct r600_texture *tex;
443
444 if (!fb->cbufs[i])
445 continue;
446
447 tex = (struct r600_texture *)fb->cbufs[i]->texture;
448
449 /* 128-bit formats are unusupported */
450 if (util_format_get_blocksizebits(fb->cbufs[i]->format) > 64) {
451 return false;
452 }
453
454 /* the clear is allowed if all layers are bound */
455 if (fb->cbufs[i]->u.tex.first_layer != 0 ||
456 fb->cbufs[i]->u.tex.last_layer != util_max_layer(&tex->resource.b.b, 0)) {
457 return false;
458 }
459
460 /* cannot clear mipmapped textures */
461 if (fb->cbufs[i]->texture->last_level != 0) {
462 return false;
463 }
464
465 /* only supported on tiled surfaces */
466 if (tex->surface.level[0].mode < RADEON_SURF_MODE_1D) {
467 return false;
468 }
469
470 /* ensure CMASK is enabled */
471 evergreen_check_alloc_cmask(ctx, fb->cbufs[i]);
472 if (tex->cmask.size == 0) {
473 return false;
474 }
475 }
476
477 return true;
478 }
479
480 static void r600_clear(struct pipe_context *ctx, unsigned buffers,
481 const union pipe_color_union *color,
482 double depth, unsigned stencil)
483 {
484 struct r600_context *rctx = (struct r600_context *)ctx;
485 struct pipe_framebuffer_state *fb = &rctx->framebuffer.state;
486
487 /* fast clear on colorbuffers (EG+) */
488 if ((buffers & PIPE_CLEAR_COLOR) && can_fast_clear_color(ctx)) {
489 int i;
490
491 for (i = 0; i < fb->nr_cbufs; i++) {
492 struct r600_texture *tex;
493
494 if (!fb->cbufs[i])
495 continue;
496
497 tex = (struct r600_texture *)fb->cbufs[i]->texture;
498
499 evergreen_set_clear_color(fb->cbufs[i], color);
500 r600_clear_buffer(ctx, &tex->cmask_buffer->b.b,
501 tex->cmask.offset, tex->cmask.size, 0);
502 tex->dirty_level_mask |= 1 << fb->cbufs[i]->u.tex.level;
503 }
504
505 rctx->framebuffer.atom.dirty = true;
506
507 buffers &= ~PIPE_CLEAR_COLOR;
508 if (!buffers)
509 return;
510 } else if (buffers & PIPE_CLEAR_COLOR) {
511 int i;
512
513 /* cannot use fast clear, make sure to disable expansion */
514 for (i = 0; i < fb->nr_cbufs; i++) {
515 struct r600_texture *tex;
516
517 if (!fb->cbufs[i])
518 continue;
519
520 tex = (struct r600_texture *)fb->cbufs[i]->texture;
521 if (tex->fmask.size == 0)
522 tex->dirty_level_mask &= ~(1 << fb->cbufs[i]->u.tex.level);
523 }
524 }
525
526 /* if hyperz enabled just clear hyperz */
527 if (fb->zsbuf && (buffers & PIPE_CLEAR_DEPTH)) {
528 struct r600_texture *rtex;
529 unsigned level = fb->zsbuf->u.tex.level;
530
531 rtex = (struct r600_texture*)fb->zsbuf->texture;
532
533 /* We can't use hyperz fast clear if each slice of a texture
534 * array are clear to different value. To simplify code just
535 * disable fast clear for texture array.
536 */
537 /* Only use htile for first level */
538 if (rtex->htile_buffer && !level && rtex->surface.array_size == 1) {
539 if (rtex->depth_clear_value != depth) {
540 rtex->depth_clear_value = depth;
541 rctx->db_state.atom.dirty = true;
542 }
543 rctx->db_misc_state.htile_clear = true;
544 rctx->db_misc_state.atom.dirty = true;
545 }
546 }
547
548 r600_blitter_begin(ctx, R600_CLEAR);
549 util_blitter_clear(rctx->blitter, fb->width, fb->height, 1,
550 buffers, color, depth, stencil);
551 r600_blitter_end(ctx);
552
553 /* disable fast clear */
554 if (rctx->db_misc_state.htile_clear) {
555 rctx->db_misc_state.htile_clear = false;
556 rctx->db_misc_state.atom.dirty = true;
557 }
558 }
559
560 static void r600_clear_render_target(struct pipe_context *ctx,
561 struct pipe_surface *dst,
562 const union pipe_color_union *color,
563 unsigned dstx, unsigned dsty,
564 unsigned width, unsigned height)
565 {
566 struct r600_context *rctx = (struct r600_context *)ctx;
567
568 r600_blitter_begin(ctx, R600_CLEAR_SURFACE);
569 util_blitter_clear_render_target(rctx->blitter, dst, color,
570 dstx, dsty, width, height);
571 r600_blitter_end(ctx);
572 }
573
574 static void r600_clear_depth_stencil(struct pipe_context *ctx,
575 struct pipe_surface *dst,
576 unsigned clear_flags,
577 double depth,
578 unsigned stencil,
579 unsigned dstx, unsigned dsty,
580 unsigned width, unsigned height)
581 {
582 struct r600_context *rctx = (struct r600_context *)ctx;
583
584 r600_blitter_begin(ctx, R600_CLEAR_SURFACE);
585 util_blitter_clear_depth_stencil(rctx->blitter, dst, clear_flags, depth, stencil,
586 dstx, dsty, width, height);
587 r600_blitter_end(ctx);
588 }
589
590 static void r600_copy_buffer(struct pipe_context *ctx, struct pipe_resource *dst, unsigned dstx,
591 struct pipe_resource *src, const struct pipe_box *src_box)
592 {
593 struct r600_context *rctx = (struct r600_context*)ctx;
594
595 if (rctx->screen->b.has_cp_dma) {
596 r600_cp_dma_copy_buffer(rctx, dst, dstx, src, src_box->x, src_box->width);
597 }
598 else if (rctx->screen->b.has_streamout &&
599 /* Require 4-byte alignment. */
600 dstx % 4 == 0 && src_box->x % 4 == 0 && src_box->width % 4 == 0) {
601
602 r600_blitter_begin(ctx, R600_COPY_BUFFER);
603 util_blitter_copy_buffer(rctx->blitter, dst, dstx, src, src_box->x, src_box->width);
604 r600_blitter_end(ctx);
605 } else {
606 util_resource_copy_region(ctx, dst, 0, dstx, 0, 0, src, 0, src_box);
607 }
608 }
609
610 /**
611 * Global buffers are not really resources, they are are actually offsets
612 * into a single global resource (r600_screen::global_pool). The means
613 * they don't have their own cs_buf handle, so they cannot be passed
614 * to r600_copy_buffer() and must be handled separately.
615 *
616 * XXX: It should be possible to implement this function using
617 * r600_copy_buffer() by passing the memory_pool resource as both src
618 * and dst and updating dstx and src_box to point to the correct offsets.
619 * This would likely perform better than the current implementation.
620 */
621 static void r600_copy_global_buffer(struct pipe_context *ctx,
622 struct pipe_resource *dst, unsigned
623 dstx, struct pipe_resource *src,
624 const struct pipe_box *src_box)
625 {
626 struct pipe_box dst_box; struct pipe_transfer *src_pxfer,
627 *dst_pxfer;
628
629 u_box_1d(dstx, src_box->width, &dst_box);
630 void *src_ptr = ctx->transfer_map(ctx, src, 0, PIPE_TRANSFER_READ,
631 src_box, &src_pxfer);
632 void *dst_ptr = ctx->transfer_map(ctx, dst, 0, PIPE_TRANSFER_WRITE,
633 &dst_box, &dst_pxfer);
634 memcpy(dst_ptr, src_ptr, src_box->width);
635
636 ctx->transfer_unmap(ctx, src_pxfer);
637 ctx->transfer_unmap(ctx, dst_pxfer);
638 }
639
640 static void r600_clear_buffer(struct pipe_context *ctx, struct pipe_resource *dst,
641 unsigned offset, unsigned size, unsigned value)
642 {
643 struct r600_context *rctx = (struct r600_context*)ctx;
644
645 if (rctx->screen->b.has_cp_dma &&
646 rctx->b.chip_class >= EVERGREEN &&
647 offset % 4 == 0 && size % 4 == 0) {
648 evergreen_cp_dma_clear_buffer(rctx, dst, offset, size, value);
649 } else if (rctx->screen->b.has_streamout && offset % 4 == 0 && size % 4 == 0) {
650 union pipe_color_union clear_value;
651 clear_value.ui[0] = value;
652
653 r600_blitter_begin(ctx, R600_DISABLE_RENDER_COND);
654 util_blitter_clear_buffer(rctx->blitter, dst, offset, size,
655 1, &clear_value);
656 r600_blitter_end(ctx);
657 } else {
658 uint32_t *map = r600_buffer_map_sync_with_rings(&rctx->b, r600_resource(dst),
659 PIPE_TRANSFER_WRITE);
660 size /= 4;
661 for (unsigned i = 0; i < size; i++)
662 *map++ = value;
663 }
664 }
665
666 static bool util_format_is_subsampled_2x1_32bpp(enum pipe_format format)
667 {
668 const struct util_format_description *desc = util_format_description(format);
669
670 return desc->layout == UTIL_FORMAT_LAYOUT_SUBSAMPLED &&
671 desc->block.width == 2 &&
672 desc->block.height == 1 &&
673 desc->block.bits == 32;
674 }
675
676 static void r600_resource_copy_region(struct pipe_context *ctx,
677 struct pipe_resource *dst,
678 unsigned dst_level,
679 unsigned dstx, unsigned dsty, unsigned dstz,
680 struct pipe_resource *src,
681 unsigned src_level,
682 const struct pipe_box *src_box)
683 {
684 struct r600_context *rctx = (struct r600_context *)ctx;
685 struct pipe_surface *dst_view, dst_templ;
686 struct pipe_sampler_view src_templ, *src_view;
687 unsigned dst_width, dst_height, src_width0, src_height0, src_widthFL, src_heightFL;
688 struct pipe_box sbox, dstbox;
689
690 /* Handle buffers first. */
691 if (dst->target == PIPE_BUFFER && src->target == PIPE_BUFFER) {
692 if ((src->bind & PIPE_BIND_GLOBAL) ||
693 (dst->bind & PIPE_BIND_GLOBAL)) {
694 r600_copy_global_buffer(ctx, dst, dstx, src, src_box);
695 } else {
696 r600_copy_buffer(ctx, dst, dstx, src, src_box);
697 }
698 return;
699 }
700
701 assert(u_max_sample(dst) == u_max_sample(src));
702
703 /* The driver doesn't decompress resources automatically while
704 * u_blitter is rendering. */
705 if (!r600_decompress_subresource(ctx, src, src_level,
706 src_box->z, src_box->z + src_box->depth - 1)) {
707 return; /* error */
708 }
709
710 dst_width = u_minify(dst->width0, dst_level);
711 dst_height = u_minify(dst->height0, dst_level);
712 src_width0 = src->width0;
713 src_height0 = src->height0;
714 src_widthFL = u_minify(src->width0, src_level);
715 src_heightFL = u_minify(src->height0, src_level);
716
717 util_blitter_default_dst_texture(&dst_templ, dst, dst_level, dstz);
718 util_blitter_default_src_texture(&src_templ, src, src_level);
719
720 if (util_format_is_compressed(src->format)) {
721 unsigned blocksize = util_format_get_blocksize(src->format);
722
723 if (blocksize == 8)
724 src_templ.format = PIPE_FORMAT_R16G16B16A16_UINT; /* 64-bit block */
725 else
726 src_templ.format = PIPE_FORMAT_R32G32B32A32_UINT; /* 128-bit block */
727 dst_templ.format = src_templ.format;
728
729 dst_width = util_format_get_nblocksx(dst->format, dst_width);
730 dst_height = util_format_get_nblocksy(dst->format, dst_height);
731 src_width0 = util_format_get_nblocksx(src->format, src_width0);
732 src_height0 = util_format_get_nblocksy(src->format, src_height0);
733 src_widthFL = util_format_get_nblocksx(src->format, src_widthFL);
734 src_heightFL = util_format_get_nblocksy(src->format, src_heightFL);
735
736 dstx = util_format_get_nblocksx(dst->format, dstx);
737 dsty = util_format_get_nblocksy(dst->format, dsty);
738
739 sbox.x = util_format_get_nblocksx(src->format, src_box->x);
740 sbox.y = util_format_get_nblocksy(src->format, src_box->y);
741 sbox.z = src_box->z;
742 sbox.width = util_format_get_nblocksx(src->format, src_box->width);
743 sbox.height = util_format_get_nblocksy(src->format, src_box->height);
744 sbox.depth = src_box->depth;
745 src_box = &sbox;
746 } else if (!util_blitter_is_copy_supported(rctx->blitter, dst, src)) {
747 if (util_format_is_subsampled_2x1_32bpp(src->format)) {
748
749 src_templ.format = PIPE_FORMAT_R8G8B8A8_UINT;
750 dst_templ.format = PIPE_FORMAT_R8G8B8A8_UINT;
751
752 dst_width = util_format_get_nblocksx(dst->format, dst_width);
753 src_width0 = util_format_get_nblocksx(src->format, src_width0);
754 src_widthFL = util_format_get_nblocksx(src->format, src_widthFL);
755
756 dstx = util_format_get_nblocksx(dst->format, dstx);
757
758 sbox = *src_box;
759 sbox.x = util_format_get_nblocksx(src->format, src_box->x);
760 sbox.width = util_format_get_nblocksx(src->format, src_box->width);
761 src_box = &sbox;
762 } else {
763 unsigned blocksize = util_format_get_blocksize(src->format);
764
765 switch (blocksize) {
766 case 1:
767 dst_templ.format = PIPE_FORMAT_R8_UNORM;
768 src_templ.format = PIPE_FORMAT_R8_UNORM;
769 break;
770 case 2:
771 dst_templ.format = PIPE_FORMAT_R8G8_UNORM;
772 src_templ.format = PIPE_FORMAT_R8G8_UNORM;
773 break;
774 case 4:
775 dst_templ.format = PIPE_FORMAT_R8G8B8A8_UNORM;
776 src_templ.format = PIPE_FORMAT_R8G8B8A8_UNORM;
777 break;
778 case 8:
779 dst_templ.format = PIPE_FORMAT_R16G16B16A16_UINT;
780 src_templ.format = PIPE_FORMAT_R16G16B16A16_UINT;
781 break;
782 case 16:
783 dst_templ.format = PIPE_FORMAT_R32G32B32A32_UINT;
784 src_templ.format = PIPE_FORMAT_R32G32B32A32_UINT;
785 break;
786 default:
787 fprintf(stderr, "Unhandled format %s with blocksize %u\n",
788 util_format_short_name(src->format), blocksize);
789 assert(0);
790 }
791 }
792 }
793
794 dst_view = r600_create_surface_custom(ctx, dst, &dst_templ, dst_width, dst_height);
795
796 if (rctx->b.chip_class >= EVERGREEN) {
797 src_view = evergreen_create_sampler_view_custom(ctx, src, &src_templ,
798 src_width0, src_height0);
799 } else {
800 src_view = r600_create_sampler_view_custom(ctx, src, &src_templ,
801 src_widthFL, src_heightFL);
802 }
803
804 u_box_3d(dstx, dsty, dstz, abs(src_box->width), abs(src_box->height),
805 abs(src_box->depth), &dstbox);
806
807 /* Copy. */
808 r600_blitter_begin(ctx, R600_COPY_TEXTURE);
809 util_blitter_blit_generic(rctx->blitter, dst_view, &dstbox,
810 src_view, src_box, src_width0, src_height0,
811 PIPE_MASK_RGBAZS, PIPE_TEX_FILTER_NEAREST, NULL);
812 r600_blitter_end(ctx);
813
814 pipe_surface_reference(&dst_view, NULL);
815 pipe_sampler_view_reference(&src_view, NULL);
816 }
817
818 /* For MSAA integer resolving to work, we change the format to NORM using this function. */
819 static enum pipe_format int_to_norm_format(enum pipe_format format)
820 {
821 switch (format) {
822 #define REPLACE_FORMAT_SIGN(format,sign) \
823 case PIPE_FORMAT_##format##_##sign##INT: \
824 return PIPE_FORMAT_##format##_##sign##NORM
825 #define REPLACE_FORMAT(format) \
826 REPLACE_FORMAT_SIGN(format, U); \
827 REPLACE_FORMAT_SIGN(format, S)
828
829 REPLACE_FORMAT_SIGN(B10G10R10A2, U);
830 REPLACE_FORMAT(R8);
831 REPLACE_FORMAT(R8G8);
832 REPLACE_FORMAT(R8G8B8X8);
833 REPLACE_FORMAT(R8G8B8A8);
834 REPLACE_FORMAT(A8);
835 REPLACE_FORMAT(I8);
836 REPLACE_FORMAT(L8);
837 REPLACE_FORMAT(L8A8);
838 REPLACE_FORMAT(R16);
839 REPLACE_FORMAT(R16G16);
840 REPLACE_FORMAT(R16G16B16X16);
841 REPLACE_FORMAT(R16G16B16A16);
842 REPLACE_FORMAT(A16);
843 REPLACE_FORMAT(I16);
844 REPLACE_FORMAT(L16);
845 REPLACE_FORMAT(L16A16);
846
847 #undef REPLACE_FORMAT
848 #undef REPLACE_FORMAT_SIGN
849 default:
850 return format;
851 }
852 }
853
854 static bool do_hardware_msaa_resolve(struct pipe_context *ctx,
855 const struct pipe_blit_info *info)
856 {
857 struct r600_context *rctx = (struct r600_context*)ctx;
858 struct r600_texture *dst = (struct r600_texture*)info->dst.resource;
859 unsigned dst_width = u_minify(info->dst.resource->width0, info->dst.level);
860 unsigned dst_height = u_minify(info->dst.resource->height0, info->dst.level);
861 enum pipe_format format = int_to_norm_format(info->dst.format);
862 unsigned sample_mask =
863 rctx->b.chip_class == CAYMAN ? ~0 :
864 ((1ull << MAX2(1, info->src.resource->nr_samples)) - 1);
865
866 if (info->src.resource->nr_samples > 1 &&
867 info->dst.resource->nr_samples <= 1 &&
868 util_max_layer(info->src.resource, 0) == 0 &&
869 util_max_layer(info->dst.resource, info->dst.level) == 0 &&
870 info->dst.format == info->src.format &&
871 !util_format_is_pure_integer(format) &&
872 !util_format_is_depth_or_stencil(format) &&
873 !info->scissor_enable &&
874 (info->mask & PIPE_MASK_RGBA) == PIPE_MASK_RGBA &&
875 dst_width == info->src.resource->width0 &&
876 dst_height == info->src.resource->height0 &&
877 info->dst.box.x == 0 &&
878 info->dst.box.y == 0 &&
879 info->dst.box.width == dst_width &&
880 info->dst.box.height == dst_height &&
881 info->dst.box.depth == 1 &&
882 info->src.box.x == 0 &&
883 info->src.box.y == 0 &&
884 info->src.box.width == dst_width &&
885 info->src.box.height == dst_height &&
886 info->src.box.depth == 1 &&
887 dst->surface.level[info->dst.level].mode >= RADEON_SURF_MODE_1D) {
888 r600_blitter_begin(ctx, R600_COLOR_RESOLVE);
889 util_blitter_custom_resolve_color(rctx->blitter,
890 info->dst.resource, info->dst.level,
891 info->dst.box.z,
892 info->src.resource, info->src.box.z,
893 sample_mask, rctx->custom_blend_resolve,
894 format);
895 r600_blitter_end(ctx);
896 return true;
897 }
898 return false;
899 }
900
901 static void r600_blit(struct pipe_context *ctx,
902 const struct pipe_blit_info *info)
903 {
904 struct r600_context *rctx = (struct r600_context*)ctx;
905
906 if (do_hardware_msaa_resolve(ctx, info)) {
907 return;
908 }
909
910 assert(util_blitter_is_blit_supported(rctx->blitter, info));
911
912 /* The driver doesn't decompress resources automatically while
913 * u_blitter is rendering. */
914 if (!r600_decompress_subresource(ctx, info->src.resource, info->src.level,
915 info->src.box.z,
916 info->src.box.z + info->src.box.depth - 1)) {
917 return; /* error */
918 }
919
920 r600_blitter_begin(ctx, R600_BLIT);
921 util_blitter_blit(rctx->blitter, info);
922 r600_blitter_end(ctx);
923 }
924
925 static void r600_flush_resource(struct pipe_context *ctx,
926 struct pipe_resource *res)
927 {
928 struct r600_texture *rtex = (struct r600_texture*)res;
929
930 assert(res->target != PIPE_BUFFER);
931
932 if (!rtex->is_depth && rtex->cmask.size) {
933 r600_blit_decompress_color(ctx, rtex, 0, res->last_level,
934 0, res->array_size - 1);
935 }
936 }
937
938 void r600_init_blit_functions(struct r600_context *rctx)
939 {
940 rctx->b.b.clear = r600_clear;
941 rctx->b.b.clear_render_target = r600_clear_render_target;
942 rctx->b.b.clear_depth_stencil = r600_clear_depth_stencil;
943 rctx->b.b.resource_copy_region = r600_resource_copy_region;
944 rctx->b.b.blit = r600_blit;
945 rctx->b.b.flush_resource = r600_flush_resource;
946 rctx->b.clear_buffer = r600_clear_buffer;
947 rctx->b.blit_decompress_depth = r600_blit_decompress_depth;
948 }