64f06e8bd4e3ff53da509f4ecd9217b693f16317
[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 "compute_memory_pool.h"
25 #include "evergreen_compute.h"
26 #include "util/u_surface.h"
27 #include "util/u_format.h"
28 #include "evergreend.h"
29
30 enum r600_blitter_op /* bitmask */
31 {
32 R600_SAVE_FRAGMENT_STATE = 1,
33 R600_SAVE_TEXTURES = 2,
34 R600_SAVE_FRAMEBUFFER = 4,
35 R600_DISABLE_RENDER_COND = 8,
36
37 R600_CLEAR = R600_SAVE_FRAGMENT_STATE,
38
39 R600_CLEAR_SURFACE = R600_SAVE_FRAGMENT_STATE | R600_SAVE_FRAMEBUFFER,
40
41 R600_COPY_BUFFER = R600_DISABLE_RENDER_COND,
42
43 R600_COPY_TEXTURE = R600_SAVE_FRAGMENT_STATE | R600_SAVE_FRAMEBUFFER | R600_SAVE_TEXTURES |
44 R600_DISABLE_RENDER_COND,
45
46 R600_BLIT = R600_SAVE_FRAGMENT_STATE | R600_SAVE_FRAMEBUFFER | R600_SAVE_TEXTURES,
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
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 util_blitter_save_vertex_buffer_slot(rctx->blitter, rctx->vertex_buffer_state.vb);
58 util_blitter_save_vertex_elements(rctx->blitter, rctx->vertex_fetch_shader.cso);
59 util_blitter_save_vertex_shader(rctx->blitter, rctx->vs_shader);
60 util_blitter_save_geometry_shader(rctx->blitter, rctx->gs_shader);
61 util_blitter_save_tessctrl_shader(rctx->blitter, rctx->tcs_shader);
62 util_blitter_save_tesseval_shader(rctx->blitter, rctx->tes_shader);
63 util_blitter_save_so_targets(rctx->blitter, rctx->b.streamout.num_targets,
64 (struct pipe_stream_output_target**)rctx->b.streamout.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->b.viewports.states[0]);
69 util_blitter_save_scissor(rctx->blitter, &rctx->b.scissors.states[0]);
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)
91 rctx->b.render_cond_force_off = true;
92 }
93
94 static void r600_blitter_end(struct pipe_context *ctx)
95 {
96 struct r600_context *rctx = (struct r600_context *)ctx;
97
98 rctx->b.render_cond_force_off = false;
99 }
100
101 static unsigned u_max_sample(struct pipe_resource *r)
102 {
103 return r->nr_samples ? r->nr_samples - 1 : 0;
104 }
105
106 static void r600_blit_decompress_depth(struct pipe_context *ctx,
107 struct r600_texture *texture,
108 struct r600_texture *staging,
109 unsigned first_level, unsigned last_level,
110 unsigned first_layer, unsigned last_layer,
111 unsigned first_sample, unsigned last_sample)
112 {
113 struct r600_context *rctx = (struct r600_context *)ctx;
114 unsigned layer, level, sample, checked_last_layer, max_layer, max_sample;
115 struct r600_texture *flushed_depth_texture = staging ?
116 staging : texture->flushed_depth_texture;
117 const struct util_format_description *desc =
118 util_format_description(texture->resource.b.b.format);
119 float depth;
120
121 if (!staging && !texture->dirty_level_mask)
122 return;
123
124 max_sample = u_max_sample(&texture->resource.b.b);
125
126 /* XXX Decompressing MSAA depth textures is broken on R6xx.
127 * There is also a hardlock if CMASK and FMASK are not present.
128 * Just skip this until we find out how to fix it. */
129 if (rctx->b.chip_class == R600 && max_sample > 0) {
130 texture->dirty_level_mask = 0;
131 return;
132 }
133
134 if (rctx->b.family == CHIP_RV610 || rctx->b.family == CHIP_RV630 ||
135 rctx->b.family == CHIP_RV620 || rctx->b.family == CHIP_RV635)
136 depth = 0.0f;
137 else
138 depth = 1.0f;
139
140 /* Enable decompression in DB_RENDER_CONTROL */
141 rctx->db_misc_state.flush_depthstencil_through_cb = true;
142 rctx->db_misc_state.copy_depth = util_format_has_depth(desc);
143 rctx->db_misc_state.copy_stencil = util_format_has_stencil(desc);
144 rctx->db_misc_state.copy_sample = first_sample;
145 r600_mark_atom_dirty(rctx, &rctx->db_misc_state.atom);
146
147 for (level = first_level; level <= last_level; level++) {
148 if (!staging && !(texture->dirty_level_mask & (1 << level)))
149 continue;
150
151 /* The smaller the mipmap level, the less layers there are
152 * as far as 3D textures are concerned. */
153 max_layer = util_max_layer(&texture->resource.b.b, level);
154 checked_last_layer = last_layer < max_layer ? last_layer : max_layer;
155
156 for (layer = first_layer; layer <= checked_last_layer; layer++) {
157 for (sample = first_sample; sample <= last_sample; sample++) {
158 struct pipe_surface *zsurf, *cbsurf, surf_tmpl;
159
160 if (sample != rctx->db_misc_state.copy_sample) {
161 rctx->db_misc_state.copy_sample = sample;
162 r600_mark_atom_dirty(rctx, &rctx->db_misc_state.atom);
163 }
164
165 surf_tmpl.format = texture->resource.b.b.format;
166 surf_tmpl.u.tex.level = level;
167 surf_tmpl.u.tex.first_layer = layer;
168 surf_tmpl.u.tex.last_layer = layer;
169
170 zsurf = ctx->create_surface(ctx, &texture->resource.b.b, &surf_tmpl);
171
172 surf_tmpl.format = flushed_depth_texture->resource.b.b.format;
173 cbsurf = ctx->create_surface(ctx,
174 &flushed_depth_texture->resource.b.b, &surf_tmpl);
175
176 r600_blitter_begin(ctx, R600_DECOMPRESS);
177 util_blitter_custom_depth_stencil(rctx->blitter, zsurf, cbsurf, 1 << sample,
178 rctx->custom_dsa_flush, depth);
179 r600_blitter_end(ctx);
180
181 pipe_surface_reference(&zsurf, NULL);
182 pipe_surface_reference(&cbsurf, NULL);
183 }
184 }
185
186 /* The texture will always be dirty if some layers or samples aren't flushed.
187 * I don't think this case occurs often though. */
188 if (!staging &&
189 first_layer == 0 && last_layer == max_layer &&
190 first_sample == 0 && last_sample == max_sample) {
191 texture->dirty_level_mask &= ~(1 << level);
192 }
193 }
194
195 /* reenable compression in DB_RENDER_CONTROL */
196 rctx->db_misc_state.flush_depthstencil_through_cb = false;
197 r600_mark_atom_dirty(rctx, &rctx->db_misc_state.atom);
198 }
199
200 static void r600_blit_decompress_depth_in_place(struct r600_context *rctx,
201 struct r600_texture *texture,
202 bool is_stencil_sampler,
203 unsigned first_level, unsigned last_level,
204 unsigned first_layer, unsigned last_layer)
205 {
206 struct pipe_surface *zsurf, surf_tmpl = {{0}};
207 unsigned layer, max_layer, checked_last_layer, level;
208 unsigned *dirty_level_mask;
209
210 /* Enable decompression in DB_RENDER_CONTROL */
211 if (is_stencil_sampler) {
212 rctx->db_misc_state.flush_stencil_inplace = true;
213 dirty_level_mask = &texture->stencil_dirty_level_mask;
214 } else {
215 rctx->db_misc_state.flush_depth_inplace = true;
216 dirty_level_mask = &texture->dirty_level_mask;
217 }
218 r600_mark_atom_dirty(rctx, &rctx->db_misc_state.atom);
219
220 surf_tmpl.format = texture->resource.b.b.format;
221
222 for (level = first_level; level <= last_level; level++) {
223 if (!(*dirty_level_mask & (1 << level)))
224 continue;
225
226 surf_tmpl.u.tex.level = level;
227
228 /* The smaller the mipmap level, the less layers there are
229 * as far as 3D textures are concerned. */
230 max_layer = util_max_layer(&texture->resource.b.b, level);
231 checked_last_layer = last_layer < max_layer ? last_layer : max_layer;
232
233 for (layer = first_layer; layer <= checked_last_layer; layer++) {
234 surf_tmpl.u.tex.first_layer = layer;
235 surf_tmpl.u.tex.last_layer = layer;
236
237 zsurf = rctx->b.b.create_surface(&rctx->b.b, &texture->resource.b.b, &surf_tmpl);
238
239 r600_blitter_begin(&rctx->b.b, R600_DECOMPRESS);
240 util_blitter_custom_depth_stencil(rctx->blitter, zsurf, NULL, ~0,
241 rctx->custom_dsa_flush, 1.0f);
242 r600_blitter_end(&rctx->b.b);
243
244 pipe_surface_reference(&zsurf, NULL);
245 }
246
247 /* The texture will always be dirty if some layers or samples aren't flushed.
248 * I don't think this case occurs often though. */
249 if (first_layer == 0 && last_layer == max_layer) {
250 *dirty_level_mask &= ~(1 << level);
251 }
252 }
253
254 /* Disable decompression in DB_RENDER_CONTROL */
255 rctx->db_misc_state.flush_depth_inplace = false;
256 rctx->db_misc_state.flush_stencil_inplace = false;
257 r600_mark_atom_dirty(rctx, &rctx->db_misc_state.atom);
258 }
259
260 void r600_decompress_depth_textures(struct r600_context *rctx,
261 struct r600_samplerview_state *textures)
262 {
263 unsigned i;
264 unsigned depth_texture_mask = textures->compressed_depthtex_mask;
265
266 while (depth_texture_mask) {
267 struct pipe_sampler_view *view;
268 struct r600_pipe_sampler_view *rview;
269 struct r600_texture *tex;
270
271 i = u_bit_scan(&depth_texture_mask);
272
273 view = &textures->views[i]->base;
274 assert(view);
275 rview = (struct r600_pipe_sampler_view*)view;
276
277 tex = (struct r600_texture *)view->texture;
278 assert(tex->db_compatible);
279
280 if (r600_can_sample_zs(tex, rview->is_stencil_sampler)) {
281 r600_blit_decompress_depth_in_place(rctx, tex,
282 rview->is_stencil_sampler,
283 view->u.tex.first_level, view->u.tex.last_level,
284 0, util_max_layer(&tex->resource.b.b, view->u.tex.first_level));
285 } else {
286 r600_blit_decompress_depth(&rctx->b.b, tex, NULL,
287 view->u.tex.first_level, view->u.tex.last_level,
288 0, util_max_layer(&tex->resource.b.b, view->u.tex.first_level),
289 0, u_max_sample(&tex->resource.b.b));
290 }
291 }
292 }
293
294 static void r600_blit_decompress_color(struct pipe_context *ctx,
295 struct r600_texture *rtex,
296 unsigned first_level, unsigned last_level,
297 unsigned first_layer, unsigned last_layer)
298 {
299 struct r600_context *rctx = (struct r600_context *)ctx;
300 unsigned layer, level, checked_last_layer, max_layer;
301
302 if (!rtex->dirty_level_mask)
303 return;
304
305 for (level = first_level; level <= last_level; level++) {
306 if (!(rtex->dirty_level_mask & (1 << level)))
307 continue;
308
309 /* The smaller the mipmap level, the less layers there are
310 * as far as 3D textures are concerned. */
311 max_layer = util_max_layer(&rtex->resource.b.b, level);
312 checked_last_layer = last_layer < max_layer ? last_layer : max_layer;
313
314 for (layer = first_layer; layer <= checked_last_layer; layer++) {
315 struct pipe_surface *cbsurf, surf_tmpl;
316
317 surf_tmpl.format = rtex->resource.b.b.format;
318 surf_tmpl.u.tex.level = level;
319 surf_tmpl.u.tex.first_layer = layer;
320 surf_tmpl.u.tex.last_layer = layer;
321 cbsurf = ctx->create_surface(ctx, &rtex->resource.b.b, &surf_tmpl);
322
323 r600_blitter_begin(ctx, R600_DECOMPRESS);
324 util_blitter_custom_color(rctx->blitter, cbsurf,
325 rtex->fmask.size ? rctx->custom_blend_decompress : rctx->custom_blend_fastclear);
326 r600_blitter_end(ctx);
327
328 pipe_surface_reference(&cbsurf, NULL);
329 }
330
331 /* The texture will always be dirty if some layers aren't flushed.
332 * I don't think this case occurs often though. */
333 if (first_layer == 0 && last_layer == max_layer) {
334 rtex->dirty_level_mask &= ~(1 << level);
335 }
336 }
337 }
338
339 void r600_decompress_color_textures(struct r600_context *rctx,
340 struct r600_samplerview_state *textures)
341 {
342 unsigned i;
343 unsigned mask = textures->compressed_colortex_mask;
344
345 while (mask) {
346 struct pipe_sampler_view *view;
347 struct r600_texture *tex;
348
349 i = u_bit_scan(&mask);
350
351 view = &textures->views[i]->base;
352 assert(view);
353
354 tex = (struct r600_texture *)view->texture;
355 assert(tex->cmask.size);
356
357 r600_blit_decompress_color(&rctx->b.b, tex,
358 view->u.tex.first_level, view->u.tex.last_level,
359 0, util_max_layer(&tex->resource.b.b, view->u.tex.first_level));
360 }
361 }
362
363 /* Helper for decompressing a portion of a color or depth resource before
364 * blitting if any decompression is needed.
365 * The driver doesn't decompress resources automatically while u_blitter is
366 * rendering. */
367 static bool r600_decompress_subresource(struct pipe_context *ctx,
368 struct pipe_resource *tex,
369 unsigned level,
370 unsigned first_layer, unsigned last_layer)
371 {
372 struct r600_context *rctx = (struct r600_context *)ctx;
373 struct r600_texture *rtex = (struct r600_texture*)tex;
374
375 if (rtex->db_compatible) {
376 if (r600_can_sample_zs(rtex, false)) {
377 r600_blit_decompress_depth_in_place(rctx, rtex, false,
378 level, level,
379 first_layer, last_layer);
380 if (rtex->surface.has_stencil) {
381 r600_blit_decompress_depth_in_place(rctx, rtex, true,
382 level, level,
383 first_layer, last_layer);
384 }
385 } else {
386 if (!r600_init_flushed_depth_texture(ctx, tex, NULL))
387 return false; /* error */
388
389 r600_blit_decompress_depth(ctx, rtex, NULL,
390 level, level,
391 first_layer, last_layer,
392 0, u_max_sample(tex));
393 }
394 } else if (rtex->cmask.size) {
395 r600_blit_decompress_color(ctx, rtex, level, level,
396 first_layer, last_layer);
397 }
398 return true;
399 }
400
401 static void r600_clear(struct pipe_context *ctx, unsigned buffers,
402 const union pipe_color_union *color,
403 double depth, unsigned stencil)
404 {
405 struct r600_context *rctx = (struct r600_context *)ctx;
406 struct pipe_framebuffer_state *fb = &rctx->framebuffer.state;
407
408 if (buffers & PIPE_CLEAR_COLOR && rctx->b.chip_class >= EVERGREEN) {
409 evergreen_do_fast_color_clear(&rctx->b, fb, &rctx->framebuffer.atom,
410 &buffers, NULL, color);
411 if (!buffers)
412 return; /* all buffers have been fast cleared */
413 }
414
415 if (buffers & PIPE_CLEAR_COLOR) {
416 int i;
417
418 /* These buffers cannot use fast clear, make sure to disable expansion. */
419 for (i = 0; i < fb->nr_cbufs; i++) {
420 struct r600_texture *tex;
421
422 /* If not clearing this buffer, skip. */
423 if (!(buffers & (PIPE_CLEAR_COLOR0 << i)))
424 continue;
425
426 if (!fb->cbufs[i])
427 continue;
428
429 tex = (struct r600_texture *)fb->cbufs[i]->texture;
430 if (tex->fmask.size == 0)
431 tex->dirty_level_mask &= ~(1 << fb->cbufs[i]->u.tex.level);
432 }
433 }
434
435 /* if hyperz enabled just clear hyperz */
436 if (fb->zsbuf && (buffers & PIPE_CLEAR_DEPTH)) {
437 struct r600_texture *rtex;
438 unsigned level = fb->zsbuf->u.tex.level;
439
440 rtex = (struct r600_texture*)fb->zsbuf->texture;
441
442 /* We can't use hyperz fast clear if each slice of a texture
443 * array are clear to different value. To simplify code just
444 * disable fast clear for texture array.
445 */
446 if (r600_htile_enabled(rtex, level) &&
447 fb->zsbuf->u.tex.first_layer == 0 &&
448 fb->zsbuf->u.tex.last_layer == util_max_layer(&rtex->resource.b.b, level)) {
449 if (rtex->depth_clear_value != depth) {
450 rtex->depth_clear_value = depth;
451 r600_mark_atom_dirty(rctx, &rctx->db_state.atom);
452 }
453 rctx->db_misc_state.htile_clear = true;
454 r600_mark_atom_dirty(rctx, &rctx->db_misc_state.atom);
455 }
456 }
457
458 r600_blitter_begin(ctx, R600_CLEAR);
459 util_blitter_clear(rctx->blitter, fb->width, fb->height,
460 util_framebuffer_get_num_layers(fb),
461 buffers, color, depth, stencil);
462 r600_blitter_end(ctx);
463
464 /* disable fast clear */
465 if (rctx->db_misc_state.htile_clear) {
466 rctx->db_misc_state.htile_clear = false;
467 r600_mark_atom_dirty(rctx, &rctx->db_misc_state.atom);
468 }
469 }
470
471 static void r600_clear_render_target(struct pipe_context *ctx,
472 struct pipe_surface *dst,
473 const union pipe_color_union *color,
474 unsigned dstx, unsigned dsty,
475 unsigned width, unsigned height,
476 bool render_condition_enabled)
477 {
478 struct r600_context *rctx = (struct r600_context *)ctx;
479
480 r600_blitter_begin(ctx, R600_CLEAR_SURFACE |
481 (render_condition_enabled ? 0 : R600_DISABLE_RENDER_COND));
482 util_blitter_clear_render_target(rctx->blitter, dst, color,
483 dstx, dsty, width, height);
484 r600_blitter_end(ctx);
485 }
486
487 static void r600_clear_depth_stencil(struct pipe_context *ctx,
488 struct pipe_surface *dst,
489 unsigned clear_flags,
490 double depth,
491 unsigned stencil,
492 unsigned dstx, unsigned dsty,
493 unsigned width, unsigned height,
494 bool render_condition_enabled)
495 {
496 struct r600_context *rctx = (struct r600_context *)ctx;
497
498 r600_blitter_begin(ctx, R600_CLEAR_SURFACE |
499 (render_condition_enabled ? 0 : R600_DISABLE_RENDER_COND));
500 util_blitter_clear_depth_stencil(rctx->blitter, dst, clear_flags, depth, stencil,
501 dstx, dsty, width, height);
502 r600_blitter_end(ctx);
503 }
504
505 static void r600_copy_buffer(struct pipe_context *ctx, struct pipe_resource *dst, unsigned dstx,
506 struct pipe_resource *src, const struct pipe_box *src_box)
507 {
508 struct r600_context *rctx = (struct r600_context*)ctx;
509
510 if (rctx->screen->b.has_cp_dma) {
511 r600_cp_dma_copy_buffer(rctx, dst, dstx, src, src_box->x, src_box->width);
512 }
513 else if (rctx->screen->b.has_streamout &&
514 /* Require 4-byte alignment. */
515 dstx % 4 == 0 && src_box->x % 4 == 0 && src_box->width % 4 == 0) {
516
517 r600_blitter_begin(ctx, R600_COPY_BUFFER);
518 util_blitter_copy_buffer(rctx->blitter, dst, dstx, src, src_box->x, src_box->width);
519 r600_blitter_end(ctx);
520 } else {
521 util_resource_copy_region(ctx, dst, 0, dstx, 0, 0, src, 0, src_box);
522 }
523 }
524
525 /**
526 * Global buffers are not really resources, they are are actually offsets
527 * into a single global resource (r600_screen::global_pool). The means
528 * they don't have their own buf handle, so they cannot be passed
529 * to r600_copy_buffer() and must be handled separately.
530 */
531 static void r600_copy_global_buffer(struct pipe_context *ctx,
532 struct pipe_resource *dst, unsigned
533 dstx, struct pipe_resource *src,
534 const struct pipe_box *src_box)
535 {
536 struct r600_context *rctx = (struct r600_context*)ctx;
537 struct compute_memory_pool *pool = rctx->screen->global_pool;
538 struct pipe_box new_src_box = *src_box;
539
540 if (src->bind & PIPE_BIND_GLOBAL) {
541 struct r600_resource_global *rsrc =
542 (struct r600_resource_global *)src;
543 struct compute_memory_item *item = rsrc->chunk;
544
545 if (is_item_in_pool(item)) {
546 new_src_box.x += 4 * item->start_in_dw;
547 src = (struct pipe_resource *)pool->bo;
548 } else {
549 if (item->real_buffer == NULL) {
550 item->real_buffer =
551 r600_compute_buffer_alloc_vram(pool->screen,
552 item->size_in_dw * 4);
553 }
554 src = (struct pipe_resource*)item->real_buffer;
555 }
556 }
557 if (dst->bind & PIPE_BIND_GLOBAL) {
558 struct r600_resource_global *rdst =
559 (struct r600_resource_global *)dst;
560 struct compute_memory_item *item = rdst->chunk;
561
562 if (is_item_in_pool(item)) {
563 dstx += 4 * item->start_in_dw;
564 dst = (struct pipe_resource *)pool->bo;
565 } else {
566 if (item->real_buffer == NULL) {
567 item->real_buffer =
568 r600_compute_buffer_alloc_vram(pool->screen,
569 item->size_in_dw * 4);
570 }
571 dst = (struct pipe_resource*)item->real_buffer;
572 }
573 }
574
575 r600_copy_buffer(ctx, dst, dstx, src, &new_src_box);
576 }
577
578 static void r600_clear_buffer(struct pipe_context *ctx, struct pipe_resource *dst,
579 uint64_t offset, uint64_t size, unsigned value,
580 enum r600_coherency coher)
581 {
582 struct r600_context *rctx = (struct r600_context*)ctx;
583
584 if (rctx->screen->b.has_cp_dma &&
585 rctx->b.chip_class >= EVERGREEN &&
586 offset % 4 == 0 && size % 4 == 0) {
587 evergreen_cp_dma_clear_buffer(rctx, dst, offset, size, value, coher);
588 } else if (rctx->screen->b.has_streamout && offset % 4 == 0 && size % 4 == 0) {
589 union pipe_color_union clear_value;
590 clear_value.ui[0] = value;
591
592 r600_blitter_begin(ctx, R600_DISABLE_RENDER_COND);
593 util_blitter_clear_buffer(rctx->blitter, dst, offset, size,
594 1, &clear_value);
595 r600_blitter_end(ctx);
596 } else {
597 uint32_t *map = r600_buffer_map_sync_with_rings(&rctx->b, r600_resource(dst),
598 PIPE_TRANSFER_WRITE);
599 map += offset / 4;
600 size /= 4;
601 for (unsigned i = 0; i < size; i++)
602 *map++ = value;
603 }
604 }
605
606 void r600_resource_copy_region(struct pipe_context *ctx,
607 struct pipe_resource *dst,
608 unsigned dst_level,
609 unsigned dstx, unsigned dsty, unsigned dstz,
610 struct pipe_resource *src,
611 unsigned src_level,
612 const struct pipe_box *src_box)
613 {
614 struct r600_context *rctx = (struct r600_context *)ctx;
615 struct pipe_surface *dst_view, dst_templ;
616 struct pipe_sampler_view src_templ, *src_view;
617 unsigned dst_width, dst_height, src_width0, src_height0, src_widthFL, src_heightFL;
618 unsigned src_force_level = 0;
619 struct pipe_box sbox, dstbox;
620
621 /* Handle buffers first. */
622 if (dst->target == PIPE_BUFFER && src->target == PIPE_BUFFER) {
623 if ((src->bind & PIPE_BIND_GLOBAL) ||
624 (dst->bind & PIPE_BIND_GLOBAL)) {
625 r600_copy_global_buffer(ctx, dst, dstx, src, src_box);
626 } else {
627 r600_copy_buffer(ctx, dst, dstx, src, src_box);
628 }
629 return;
630 }
631
632 assert(u_max_sample(dst) == u_max_sample(src));
633
634 /* The driver doesn't decompress resources automatically while
635 * u_blitter is rendering. */
636 if (!r600_decompress_subresource(ctx, src, src_level,
637 src_box->z, src_box->z + src_box->depth - 1)) {
638 return; /* error */
639 }
640
641 dst_width = u_minify(dst->width0, dst_level);
642 dst_height = u_minify(dst->height0, dst_level);
643 src_width0 = src->width0;
644 src_height0 = src->height0;
645 src_widthFL = u_minify(src->width0, src_level);
646 src_heightFL = u_minify(src->height0, src_level);
647
648 util_blitter_default_dst_texture(&dst_templ, dst, dst_level, dstz);
649 util_blitter_default_src_texture(rctx->blitter, &src_templ, src, src_level);
650
651 if (util_format_is_compressed(src->format) ||
652 util_format_is_compressed(dst->format)) {
653 unsigned blocksize = util_format_get_blocksize(src->format);
654
655 if (blocksize == 8)
656 src_templ.format = PIPE_FORMAT_R16G16B16A16_UINT; /* 64-bit block */
657 else
658 src_templ.format = PIPE_FORMAT_R32G32B32A32_UINT; /* 128-bit block */
659 dst_templ.format = src_templ.format;
660
661 dst_width = util_format_get_nblocksx(dst->format, dst_width);
662 dst_height = util_format_get_nblocksy(dst->format, dst_height);
663 src_width0 = util_format_get_nblocksx(src->format, src_width0);
664 src_height0 = util_format_get_nblocksy(src->format, src_height0);
665 src_widthFL = util_format_get_nblocksx(src->format, src_widthFL);
666 src_heightFL = util_format_get_nblocksy(src->format, src_heightFL);
667
668 dstx = util_format_get_nblocksx(dst->format, dstx);
669 dsty = util_format_get_nblocksy(dst->format, dsty);
670
671 sbox.x = util_format_get_nblocksx(src->format, src_box->x);
672 sbox.y = util_format_get_nblocksy(src->format, src_box->y);
673 sbox.z = src_box->z;
674 sbox.width = util_format_get_nblocksx(src->format, src_box->width);
675 sbox.height = util_format_get_nblocksy(src->format, src_box->height);
676 sbox.depth = src_box->depth;
677 src_box = &sbox;
678
679 src_force_level = src_level;
680 } else if (!util_blitter_is_copy_supported(rctx->blitter, dst, src)) {
681 if (util_format_is_subsampled_422(src->format)) {
682
683 src_templ.format = PIPE_FORMAT_R8G8B8A8_UINT;
684 dst_templ.format = PIPE_FORMAT_R8G8B8A8_UINT;
685
686 dst_width = util_format_get_nblocksx(dst->format, dst_width);
687 src_width0 = util_format_get_nblocksx(src->format, src_width0);
688 src_widthFL = util_format_get_nblocksx(src->format, src_widthFL);
689
690 dstx = util_format_get_nblocksx(dst->format, dstx);
691
692 sbox = *src_box;
693 sbox.x = util_format_get_nblocksx(src->format, src_box->x);
694 sbox.width = util_format_get_nblocksx(src->format, src_box->width);
695 src_box = &sbox;
696 } else {
697 unsigned blocksize = util_format_get_blocksize(src->format);
698
699 switch (blocksize) {
700 case 1:
701 dst_templ.format = PIPE_FORMAT_R8_UNORM;
702 src_templ.format = PIPE_FORMAT_R8_UNORM;
703 break;
704 case 2:
705 dst_templ.format = PIPE_FORMAT_R8G8_UNORM;
706 src_templ.format = PIPE_FORMAT_R8G8_UNORM;
707 break;
708 case 4:
709 dst_templ.format = PIPE_FORMAT_R8G8B8A8_UNORM;
710 src_templ.format = PIPE_FORMAT_R8G8B8A8_UNORM;
711 break;
712 case 8:
713 dst_templ.format = PIPE_FORMAT_R16G16B16A16_UINT;
714 src_templ.format = PIPE_FORMAT_R16G16B16A16_UINT;
715 break;
716 case 16:
717 dst_templ.format = PIPE_FORMAT_R32G32B32A32_UINT;
718 src_templ.format = PIPE_FORMAT_R32G32B32A32_UINT;
719 break;
720 default:
721 fprintf(stderr, "Unhandled format %s with blocksize %u\n",
722 util_format_short_name(src->format), blocksize);
723 assert(0);
724 }
725 }
726 }
727
728 dst_view = r600_create_surface_custom(ctx, dst, &dst_templ,
729 /* we don't care about these two for r600g */
730 dst->width0, dst->height0,
731 dst_width, dst_height);
732
733 if (rctx->b.chip_class >= EVERGREEN) {
734 src_view = evergreen_create_sampler_view_custom(ctx, src, &src_templ,
735 src_width0, src_height0,
736 src_force_level);
737 } else {
738 src_view = r600_create_sampler_view_custom(ctx, src, &src_templ,
739 src_widthFL, src_heightFL);
740 }
741
742 u_box_3d(dstx, dsty, dstz, abs(src_box->width), abs(src_box->height),
743 abs(src_box->depth), &dstbox);
744
745 /* Copy. */
746 r600_blitter_begin(ctx, R600_COPY_TEXTURE);
747 util_blitter_blit_generic(rctx->blitter, dst_view, &dstbox,
748 src_view, src_box, src_width0, src_height0,
749 PIPE_MASK_RGBAZS, PIPE_TEX_FILTER_NEAREST, NULL,
750 FALSE);
751 r600_blitter_end(ctx);
752
753 pipe_surface_reference(&dst_view, NULL);
754 pipe_sampler_view_reference(&src_view, NULL);
755 }
756
757 static bool do_hardware_msaa_resolve(struct pipe_context *ctx,
758 const struct pipe_blit_info *info)
759 {
760 struct r600_context *rctx = (struct r600_context*)ctx;
761 struct r600_texture *dst = (struct r600_texture*)info->dst.resource;
762 unsigned dst_width = u_minify(info->dst.resource->width0, info->dst.level);
763 unsigned dst_height = u_minify(info->dst.resource->height0, info->dst.level);
764 enum pipe_format format = info->src.format;
765 unsigned sample_mask =
766 rctx->b.chip_class == CAYMAN ? ~0 :
767 ((1ull << MAX2(1, info->src.resource->nr_samples)) - 1);
768 struct pipe_resource *tmp, templ;
769 struct pipe_blit_info blit;
770
771 /* Check basic requirements for hw resolve. */
772 if (!(info->src.resource->nr_samples > 1 &&
773 info->dst.resource->nr_samples <= 1 &&
774 !util_format_is_pure_integer(format) &&
775 !util_format_is_depth_or_stencil(format) &&
776 util_max_layer(info->src.resource, 0) == 0))
777 return false;
778
779 /* Check the remaining requirements for hw resolve. */
780 if (util_max_layer(info->dst.resource, info->dst.level) == 0 &&
781 util_is_format_compatible(util_format_description(info->src.format),
782 util_format_description(info->dst.format)) &&
783 !info->scissor_enable &&
784 (info->mask & PIPE_MASK_RGBA) == PIPE_MASK_RGBA &&
785 dst_width == info->src.resource->width0 &&
786 dst_height == info->src.resource->height0 &&
787 info->dst.box.x == 0 &&
788 info->dst.box.y == 0 &&
789 info->dst.box.width == dst_width &&
790 info->dst.box.height == dst_height &&
791 info->dst.box.depth == 1 &&
792 info->src.box.x == 0 &&
793 info->src.box.y == 0 &&
794 info->src.box.width == dst_width &&
795 info->src.box.height == dst_height &&
796 info->src.box.depth == 1 &&
797 dst->surface.u.legacy.level[info->dst.level].mode >= RADEON_SURF_MODE_1D &&
798 (!dst->cmask.size || !dst->dirty_level_mask) /* dst cannot be fast-cleared */) {
799 r600_blitter_begin(ctx, R600_COLOR_RESOLVE |
800 (info->render_condition_enable ? 0 : R600_DISABLE_RENDER_COND));
801 util_blitter_custom_resolve_color(rctx->blitter,
802 info->dst.resource, info->dst.level,
803 info->dst.box.z,
804 info->src.resource, info->src.box.z,
805 sample_mask, rctx->custom_blend_resolve,
806 format);
807 r600_blitter_end(ctx);
808 return true;
809 }
810
811 /* Shader-based resolve is VERY SLOW. Instead, resolve into
812 * a temporary texture and blit.
813 */
814 memset(&templ, 0, sizeof(templ));
815 templ.target = PIPE_TEXTURE_2D;
816 templ.format = info->src.resource->format;
817 templ.width0 = info->src.resource->width0;
818 templ.height0 = info->src.resource->height0;
819 templ.depth0 = 1;
820 templ.array_size = 1;
821 templ.usage = PIPE_USAGE_DEFAULT;
822 templ.flags = R600_RESOURCE_FLAG_FORCE_TILING;
823
824 tmp = ctx->screen->resource_create(ctx->screen, &templ);
825 if (!tmp)
826 return false;
827
828 /* resolve */
829 r600_blitter_begin(ctx, R600_COLOR_RESOLVE |
830 (info->render_condition_enable ? 0 : R600_DISABLE_RENDER_COND));
831 util_blitter_custom_resolve_color(rctx->blitter, tmp, 0, 0,
832 info->src.resource, info->src.box.z,
833 sample_mask, rctx->custom_blend_resolve,
834 format);
835 r600_blitter_end(ctx);
836
837 /* blit */
838 blit = *info;
839 blit.src.resource = tmp;
840 blit.src.box.z = 0;
841
842 r600_blitter_begin(ctx, R600_BLIT |
843 (info->render_condition_enable ? 0 : R600_DISABLE_RENDER_COND));
844 util_blitter_blit(rctx->blitter, &blit);
845 r600_blitter_end(ctx);
846
847 pipe_resource_reference(&tmp, NULL);
848 return true;
849 }
850
851 static void r600_blit(struct pipe_context *ctx,
852 const struct pipe_blit_info *info)
853 {
854 struct r600_context *rctx = (struct r600_context*)ctx;
855 struct r600_texture *rdst = (struct r600_texture *)info->dst.resource;
856
857 if (do_hardware_msaa_resolve(ctx, info)) {
858 return;
859 }
860
861 /* Using SDMA for copying to a linear texture in GTT is much faster.
862 * This improves DRI PRIME performance.
863 *
864 * resource_copy_region can't do this yet, because dma_copy calls it
865 * on failure (recursion).
866 */
867 if (rdst->surface.u.legacy.level[info->dst.level].mode ==
868 RADEON_SURF_MODE_LINEAR_ALIGNED &&
869 rctx->b.dma_copy &&
870 util_can_blit_via_copy_region(info, false)) {
871 rctx->b.dma_copy(ctx, info->dst.resource, info->dst.level,
872 info->dst.box.x, info->dst.box.y,
873 info->dst.box.z,
874 info->src.resource, info->src.level,
875 &info->src.box);
876 return;
877 }
878
879 assert(util_blitter_is_blit_supported(rctx->blitter, info));
880
881 /* The driver doesn't decompress resources automatically while
882 * u_blitter is rendering. */
883 if (!r600_decompress_subresource(ctx, info->src.resource, info->src.level,
884 info->src.box.z,
885 info->src.box.z + info->src.box.depth - 1)) {
886 return; /* error */
887 }
888
889 if (rctx->screen->b.debug_flags & DBG_FORCE_DMA &&
890 util_try_blit_via_copy_region(ctx, info))
891 return;
892
893 r600_blitter_begin(ctx, R600_BLIT |
894 (info->render_condition_enable ? 0 : R600_DISABLE_RENDER_COND));
895 util_blitter_blit(rctx->blitter, info);
896 r600_blitter_end(ctx);
897 }
898
899 static void r600_flush_resource(struct pipe_context *ctx,
900 struct pipe_resource *res)
901 {
902 struct r600_texture *rtex = (struct r600_texture*)res;
903
904 assert(res->target != PIPE_BUFFER);
905
906 if (!rtex->is_depth && rtex->cmask.size) {
907 r600_blit_decompress_color(ctx, rtex, 0, res->last_level,
908 0, util_max_layer(res, 0));
909 }
910 }
911
912 void r600_init_blit_functions(struct r600_context *rctx)
913 {
914 rctx->b.b.clear = r600_clear;
915 rctx->b.b.clear_render_target = r600_clear_render_target;
916 rctx->b.b.clear_depth_stencil = r600_clear_depth_stencil;
917 rctx->b.b.resource_copy_region = r600_resource_copy_region;
918 rctx->b.b.blit = r600_blit;
919 rctx->b.b.flush_resource = r600_flush_resource;
920 rctx->b.clear_buffer = r600_clear_buffer;
921 rctx->b.blit_decompress_depth = r600_blit_decompress_depth;
922 }