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