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