gallium: split transfer_inline_write into buffer and texture callbacks
[mesa.git] / src / gallium / drivers / radeon / r600_texture.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 * Authors:
24 * Jerome Glisse
25 * Corbin Simpson
26 */
27 #include "r600_pipe_common.h"
28 #include "r600_cs.h"
29 #include "r600_query.h"
30 #include "util/u_format.h"
31 #include "util/u_memory.h"
32 #include "util/u_pack_color.h"
33 #include "os/os_time.h"
34 #include <errno.h>
35 #include <inttypes.h>
36
37 static void r600_texture_discard_cmask(struct r600_common_screen *rscreen,
38 struct r600_texture *rtex);
39 static unsigned r600_choose_tiling(struct r600_common_screen *rscreen,
40 const struct pipe_resource *templ);
41
42
43 bool r600_prepare_for_dma_blit(struct r600_common_context *rctx,
44 struct r600_texture *rdst,
45 unsigned dst_level, unsigned dstx,
46 unsigned dsty, unsigned dstz,
47 struct r600_texture *rsrc,
48 unsigned src_level,
49 const struct pipe_box *src_box)
50 {
51 if (!rctx->dma.cs)
52 return false;
53
54 if (util_format_get_blocksizebits(rdst->resource.b.b.format) !=
55 util_format_get_blocksizebits(rsrc->resource.b.b.format))
56 return false;
57
58 /* MSAA: Blits don't exist in the real world. */
59 if (rsrc->resource.b.b.nr_samples > 1 ||
60 rdst->resource.b.b.nr_samples > 1)
61 return false;
62
63 /* Depth-stencil surfaces:
64 * When dst is linear, the DB->CB copy preserves HTILE.
65 * When dst is tiled, the 3D path must be used to update HTILE.
66 */
67 if (rsrc->is_depth || rdst->is_depth)
68 return false;
69
70 /* DCC as:
71 * src: Use the 3D path. DCC decompression is expensive.
72 * dst: Use the 3D path to compress the pixels with DCC.
73 */
74 if ((rsrc->dcc_offset && rsrc->surface.level[src_level].dcc_enabled) ||
75 (rdst->dcc_offset && rdst->surface.level[dst_level].dcc_enabled))
76 return false;
77
78 /* CMASK as:
79 * src: Both texture and SDMA paths need decompression. Use SDMA.
80 * dst: If overwriting the whole texture, discard CMASK and use
81 * SDMA. Otherwise, use the 3D path.
82 */
83 if (rdst->cmask.size && rdst->dirty_level_mask & (1 << dst_level)) {
84 /* The CMASK clear is only enabled for the first level. */
85 assert(dst_level == 0);
86 if (!util_texrange_covers_whole_level(&rdst->resource.b.b, dst_level,
87 dstx, dsty, dstz, src_box->width,
88 src_box->height, src_box->depth))
89 return false;
90
91 r600_texture_discard_cmask(rctx->screen, rdst);
92 }
93
94 /* All requirements are met. Prepare textures for SDMA. */
95 if (rsrc->cmask.size && rsrc->dirty_level_mask & (1 << src_level))
96 rctx->b.flush_resource(&rctx->b, &rsrc->resource.b.b);
97
98 assert(!(rsrc->dirty_level_mask & (1 << src_level)));
99 assert(!(rdst->dirty_level_mask & (1 << dst_level)));
100
101 return true;
102 }
103
104 /* Same as resource_copy_region, except that both upsampling and downsampling are allowed. */
105 static void r600_copy_region_with_blit(struct pipe_context *pipe,
106 struct pipe_resource *dst,
107 unsigned dst_level,
108 unsigned dstx, unsigned dsty, unsigned dstz,
109 struct pipe_resource *src,
110 unsigned src_level,
111 const struct pipe_box *src_box)
112 {
113 struct pipe_blit_info blit;
114
115 memset(&blit, 0, sizeof(blit));
116 blit.src.resource = src;
117 blit.src.format = src->format;
118 blit.src.level = src_level;
119 blit.src.box = *src_box;
120 blit.dst.resource = dst;
121 blit.dst.format = dst->format;
122 blit.dst.level = dst_level;
123 blit.dst.box.x = dstx;
124 blit.dst.box.y = dsty;
125 blit.dst.box.z = dstz;
126 blit.dst.box.width = src_box->width;
127 blit.dst.box.height = src_box->height;
128 blit.dst.box.depth = src_box->depth;
129 blit.mask = util_format_get_mask(src->format) &
130 util_format_get_mask(dst->format);
131 blit.filter = PIPE_TEX_FILTER_NEAREST;
132
133 if (blit.mask) {
134 pipe->blit(pipe, &blit);
135 }
136 }
137
138 /* Copy from a full GPU texture to a transfer's staging one. */
139 static void r600_copy_to_staging_texture(struct pipe_context *ctx, struct r600_transfer *rtransfer)
140 {
141 struct r600_common_context *rctx = (struct r600_common_context*)ctx;
142 struct pipe_transfer *transfer = (struct pipe_transfer*)rtransfer;
143 struct pipe_resource *dst = &rtransfer->staging->b.b;
144 struct pipe_resource *src = transfer->resource;
145
146 if (src->nr_samples > 1) {
147 r600_copy_region_with_blit(ctx, dst, 0, 0, 0, 0,
148 src, transfer->level, &transfer->box);
149 return;
150 }
151
152 rctx->dma_copy(ctx, dst, 0, 0, 0, 0, src, transfer->level,
153 &transfer->box);
154 }
155
156 /* Copy from a transfer's staging texture to a full GPU one. */
157 static void r600_copy_from_staging_texture(struct pipe_context *ctx, struct r600_transfer *rtransfer)
158 {
159 struct r600_common_context *rctx = (struct r600_common_context*)ctx;
160 struct pipe_transfer *transfer = (struct pipe_transfer*)rtransfer;
161 struct pipe_resource *dst = transfer->resource;
162 struct pipe_resource *src = &rtransfer->staging->b.b;
163 struct pipe_box sbox;
164
165 u_box_3d(0, 0, 0, transfer->box.width, transfer->box.height, transfer->box.depth, &sbox);
166
167 if (dst->nr_samples > 1) {
168 r600_copy_region_with_blit(ctx, dst, transfer->level,
169 transfer->box.x, transfer->box.y, transfer->box.z,
170 src, 0, &sbox);
171 return;
172 }
173
174 rctx->dma_copy(ctx, dst, transfer->level,
175 transfer->box.x, transfer->box.y, transfer->box.z,
176 src, 0, &sbox);
177 }
178
179 static unsigned r600_texture_get_offset(struct r600_texture *rtex, unsigned level,
180 const struct pipe_box *box)
181 {
182 enum pipe_format format = rtex->resource.b.b.format;
183
184 return rtex->surface.level[level].offset +
185 box->z * rtex->surface.level[level].slice_size +
186 box->y / util_format_get_blockheight(format) * rtex->surface.level[level].pitch_bytes +
187 box->x / util_format_get_blockwidth(format) * util_format_get_blocksize(format);
188 }
189
190 static int r600_init_surface(struct r600_common_screen *rscreen,
191 struct radeon_surf *surface,
192 const struct pipe_resource *ptex,
193 unsigned array_mode,
194 bool is_flushed_depth)
195 {
196 const struct util_format_description *desc =
197 util_format_description(ptex->format);
198 bool is_depth, is_stencil;
199
200 is_depth = util_format_has_depth(desc);
201 is_stencil = util_format_has_stencil(desc);
202
203 surface->npix_x = ptex->width0;
204 surface->npix_y = ptex->height0;
205 surface->npix_z = ptex->depth0;
206 surface->blk_w = util_format_get_blockwidth(ptex->format);
207 surface->blk_h = util_format_get_blockheight(ptex->format);
208 surface->blk_d = 1;
209 surface->array_size = 1;
210 surface->last_level = ptex->last_level;
211
212 if (rscreen->chip_class >= EVERGREEN && !is_flushed_depth &&
213 ptex->format == PIPE_FORMAT_Z32_FLOAT_S8X24_UINT) {
214 surface->bpe = 4; /* stencil is allocated separately on evergreen */
215 } else {
216 surface->bpe = util_format_get_blocksize(ptex->format);
217 /* align byte per element on dword */
218 if (surface->bpe == 3) {
219 surface->bpe = 4;
220 }
221 }
222
223 surface->nsamples = ptex->nr_samples ? ptex->nr_samples : 1;
224 surface->flags = RADEON_SURF_SET(array_mode, MODE);
225
226 switch (ptex->target) {
227 case PIPE_TEXTURE_1D:
228 surface->flags |= RADEON_SURF_SET(RADEON_SURF_TYPE_1D, TYPE);
229 break;
230 case PIPE_TEXTURE_RECT:
231 case PIPE_TEXTURE_2D:
232 surface->flags |= RADEON_SURF_SET(RADEON_SURF_TYPE_2D, TYPE);
233 break;
234 case PIPE_TEXTURE_3D:
235 surface->flags |= RADEON_SURF_SET(RADEON_SURF_TYPE_3D, TYPE);
236 break;
237 case PIPE_TEXTURE_1D_ARRAY:
238 surface->flags |= RADEON_SURF_SET(RADEON_SURF_TYPE_1D_ARRAY, TYPE);
239 surface->array_size = ptex->array_size;
240 break;
241 case PIPE_TEXTURE_CUBE_ARRAY: /* cube array layout like 2d array */
242 assert(ptex->array_size % 6 == 0);
243 case PIPE_TEXTURE_2D_ARRAY:
244 surface->flags |= RADEON_SURF_SET(RADEON_SURF_TYPE_2D_ARRAY, TYPE);
245 surface->array_size = ptex->array_size;
246 break;
247 case PIPE_TEXTURE_CUBE:
248 surface->flags |= RADEON_SURF_SET(RADEON_SURF_TYPE_CUBEMAP, TYPE);
249 break;
250 case PIPE_BUFFER:
251 default:
252 return -EINVAL;
253 }
254
255 if (!is_flushed_depth && is_depth) {
256 surface->flags |= RADEON_SURF_ZBUFFER;
257
258 if (is_stencil) {
259 surface->flags |= RADEON_SURF_SBUFFER |
260 RADEON_SURF_HAS_SBUFFER_MIPTREE;
261 }
262 }
263 if (rscreen->chip_class >= SI) {
264 surface->flags |= RADEON_SURF_HAS_TILE_MODE_INDEX;
265 }
266
267 if (rscreen->chip_class >= VI &&
268 (ptex->flags & R600_RESOURCE_FLAG_DISABLE_DCC ||
269 ptex->format == PIPE_FORMAT_R9G9B9E5_FLOAT))
270 surface->flags |= RADEON_SURF_DISABLE_DCC;
271
272 if (ptex->bind & PIPE_BIND_SCANOUT) {
273 /* This should catch bugs in gallium users setting incorrect flags. */
274 assert(surface->nsamples == 1 &&
275 surface->array_size == 1 &&
276 surface->npix_z == 1 &&
277 surface->last_level == 0 &&
278 !(surface->flags & RADEON_SURF_Z_OR_SBUFFER));
279
280 surface->flags |= RADEON_SURF_SCANOUT;
281 }
282 return 0;
283 }
284
285 static int r600_setup_surface(struct pipe_screen *screen,
286 struct r600_texture *rtex,
287 unsigned pitch_in_bytes_override,
288 unsigned offset)
289 {
290 struct r600_common_screen *rscreen = (struct r600_common_screen*)screen;
291 unsigned i;
292 int r;
293
294 r = rscreen->ws->surface_init(rscreen->ws, &rtex->surface);
295 if (r) {
296 return r;
297 }
298
299 rtex->size = rtex->surface.bo_size;
300
301 if (pitch_in_bytes_override && pitch_in_bytes_override != rtex->surface.level[0].pitch_bytes) {
302 /* old ddx on evergreen over estimate alignment for 1d, only 1 level
303 * for those
304 */
305 rtex->surface.level[0].nblk_x = pitch_in_bytes_override / rtex->surface.bpe;
306 rtex->surface.level[0].pitch_bytes = pitch_in_bytes_override;
307 rtex->surface.level[0].slice_size = pitch_in_bytes_override * rtex->surface.level[0].nblk_y;
308 }
309
310 if (offset) {
311 for (i = 0; i < ARRAY_SIZE(rtex->surface.level); ++i)
312 rtex->surface.level[i].offset += offset;
313 }
314 return 0;
315 }
316
317 static void r600_texture_init_metadata(struct r600_texture *rtex,
318 struct radeon_bo_metadata *metadata)
319 {
320 struct radeon_surf *surface = &rtex->surface;
321
322 memset(metadata, 0, sizeof(*metadata));
323 metadata->microtile = surface->level[0].mode >= RADEON_SURF_MODE_1D ?
324 RADEON_LAYOUT_TILED : RADEON_LAYOUT_LINEAR;
325 metadata->macrotile = surface->level[0].mode >= RADEON_SURF_MODE_2D ?
326 RADEON_LAYOUT_TILED : RADEON_LAYOUT_LINEAR;
327 metadata->pipe_config = surface->pipe_config;
328 metadata->bankw = surface->bankw;
329 metadata->bankh = surface->bankh;
330 metadata->tile_split = surface->tile_split;
331 metadata->mtilea = surface->mtilea;
332 metadata->num_banks = surface->num_banks;
333 metadata->stride = surface->level[0].pitch_bytes;
334 metadata->scanout = (surface->flags & RADEON_SURF_SCANOUT) != 0;
335 }
336
337 static void r600_dirty_all_framebuffer_states(struct r600_common_screen *rscreen)
338 {
339 p_atomic_inc(&rscreen->dirty_fb_counter);
340 }
341
342 static void r600_eliminate_fast_color_clear(struct r600_common_screen *rscreen,
343 struct r600_texture *rtex)
344 {
345 struct pipe_context *ctx = rscreen->aux_context;
346
347 pipe_mutex_lock(rscreen->aux_context_lock);
348 ctx->flush_resource(ctx, &rtex->resource.b.b);
349 ctx->flush(ctx, NULL, 0);
350 pipe_mutex_unlock(rscreen->aux_context_lock);
351 }
352
353 static void r600_texture_discard_cmask(struct r600_common_screen *rscreen,
354 struct r600_texture *rtex)
355 {
356 if (!rtex->cmask.size)
357 return;
358
359 assert(rtex->resource.b.b.nr_samples <= 1);
360
361 /* Disable CMASK. */
362 memset(&rtex->cmask, 0, sizeof(rtex->cmask));
363 rtex->cmask.base_address_reg = rtex->resource.gpu_address >> 8;
364
365 if (rscreen->chip_class >= SI)
366 rtex->cb_color_info &= ~SI_S_028C70_FAST_CLEAR(1);
367 else
368 rtex->cb_color_info &= ~EG_S_028C70_FAST_CLEAR(1);
369
370 if (rtex->cmask_buffer != &rtex->resource)
371 r600_resource_reference(&rtex->cmask_buffer, NULL);
372
373 /* Notify all contexts about the change. */
374 r600_dirty_all_framebuffer_states(rscreen);
375 p_atomic_inc(&rscreen->compressed_colortex_counter);
376 }
377
378 static bool r600_can_disable_dcc(struct r600_texture *rtex)
379 {
380 /* We can't disable DCC if it can be written by another process. */
381 return rtex->dcc_offset &&
382 (!rtex->resource.is_shared ||
383 !(rtex->resource.external_usage & PIPE_HANDLE_USAGE_WRITE));
384 }
385
386 static bool r600_texture_discard_dcc(struct r600_common_screen *rscreen,
387 struct r600_texture *rtex)
388 {
389 if (!r600_can_disable_dcc(rtex))
390 return false;
391
392 assert(rtex->dcc_separate_buffer == NULL);
393
394 /* Disable DCC. */
395 rtex->dcc_offset = 0;
396
397 /* Notify all contexts about the change. */
398 r600_dirty_all_framebuffer_states(rscreen);
399 return true;
400 }
401
402 bool r600_texture_disable_dcc(struct r600_common_screen *rscreen,
403 struct r600_texture *rtex)
404 {
405 struct r600_common_context *rctx =
406 (struct r600_common_context *)rscreen->aux_context;
407
408 if (!r600_can_disable_dcc(rtex))
409 return false;
410
411 /* Decompress DCC. */
412 pipe_mutex_lock(rscreen->aux_context_lock);
413 rctx->decompress_dcc(&rctx->b, rtex);
414 rctx->b.flush(&rctx->b, NULL, 0);
415 pipe_mutex_unlock(rscreen->aux_context_lock);
416
417 return r600_texture_discard_dcc(rscreen, rtex);
418 }
419
420 static void r600_degrade_tile_mode_to_linear(struct r600_common_context *rctx,
421 struct r600_texture *rtex,
422 bool invalidate_storage)
423 {
424 struct pipe_screen *screen = rctx->b.screen;
425 struct r600_texture *new_tex;
426 struct pipe_resource templ = rtex->resource.b.b;
427 unsigned i;
428
429 templ.bind |= PIPE_BIND_LINEAR;
430
431 /* r600g doesn't react to dirty_tex_descriptor_counter */
432 if (rctx->chip_class < SI)
433 return;
434
435 if (rtex->resource.is_shared ||
436 rtex->surface.level[0].mode == RADEON_SURF_MODE_LINEAR_ALIGNED)
437 return;
438
439 /* This fails with MSAA, depth, and compressed textures. */
440 if (r600_choose_tiling(rctx->screen, &templ) !=
441 RADEON_SURF_MODE_LINEAR_ALIGNED)
442 return;
443
444 new_tex = (struct r600_texture*)screen->resource_create(screen, &templ);
445 if (!new_tex)
446 return;
447
448 /* Copy the pixels to the new texture. */
449 if (!invalidate_storage) {
450 for (i = 0; i <= templ.last_level; i++) {
451 struct pipe_box box;
452
453 u_box_3d(0, 0, 0,
454 u_minify(templ.width0, i), u_minify(templ.height0, i),
455 util_max_layer(&templ, i) + 1, &box);
456
457 rctx->dma_copy(&rctx->b, &new_tex->resource.b.b, i, 0, 0, 0,
458 &rtex->resource.b.b, i, &box);
459 }
460 }
461
462 r600_texture_discard_cmask(rctx->screen, rtex);
463 r600_texture_discard_dcc(rctx->screen, rtex);
464
465 /* Replace the structure fields of rtex. */
466 rtex->resource.b.b.bind = templ.bind;
467 pb_reference(&rtex->resource.buf, new_tex->resource.buf);
468 rtex->resource.gpu_address = new_tex->resource.gpu_address;
469 rtex->resource.domains = new_tex->resource.domains;
470 rtex->size = new_tex->size;
471 rtex->surface = new_tex->surface;
472 rtex->non_disp_tiling = new_tex->non_disp_tiling;
473 rtex->cb_color_info = new_tex->cb_color_info;
474 rtex->cmask = new_tex->cmask; /* needed even without CMASK */
475
476 assert(!rtex->htile_buffer);
477 assert(!rtex->cmask.size);
478 assert(!rtex->fmask.size);
479 assert(!rtex->dcc_offset);
480 assert(!rtex->is_depth);
481
482 r600_texture_reference(&new_tex, NULL);
483
484 r600_dirty_all_framebuffer_states(rctx->screen);
485 p_atomic_inc(&rctx->screen->dirty_tex_descriptor_counter);
486 }
487
488 static boolean r600_texture_get_handle(struct pipe_screen* screen,
489 struct pipe_resource *resource,
490 struct winsys_handle *whandle,
491 unsigned usage)
492 {
493 struct r600_common_screen *rscreen = (struct r600_common_screen*)screen;
494 struct r600_resource *res = (struct r600_resource*)resource;
495 struct r600_texture *rtex = (struct r600_texture*)resource;
496 struct radeon_bo_metadata metadata;
497 bool update_metadata = false;
498
499 /* This is not supported now, but it might be required for OpenCL
500 * interop in the future.
501 */
502 if (resource->target != PIPE_BUFFER &&
503 (resource->nr_samples > 1 || rtex->is_depth))
504 return false;
505
506 if (resource->target != PIPE_BUFFER) {
507 /* Since shader image stores don't support DCC on VI,
508 * disable it for external clients that want write
509 * access.
510 */
511 if (usage & PIPE_HANDLE_USAGE_WRITE && rtex->dcc_offset) {
512 if (r600_texture_disable_dcc(rscreen, rtex))
513 update_metadata = true;
514 }
515
516 if (!(usage & PIPE_HANDLE_USAGE_EXPLICIT_FLUSH) &&
517 rtex->cmask.size) {
518 /* Eliminate fast clear (both CMASK and DCC) */
519 r600_eliminate_fast_color_clear(rscreen, rtex);
520
521 /* Disable CMASK if flush_resource isn't going
522 * to be called.
523 */
524 r600_texture_discard_cmask(rscreen, rtex);
525 }
526
527 /* Set metadata. */
528 if (!res->is_shared || update_metadata) {
529 r600_texture_init_metadata(rtex, &metadata);
530 if (rscreen->query_opaque_metadata)
531 rscreen->query_opaque_metadata(rscreen, rtex,
532 &metadata);
533
534 rscreen->ws->buffer_set_metadata(res->buf, &metadata);
535 }
536 }
537
538 if (res->is_shared) {
539 /* USAGE_EXPLICIT_FLUSH must be cleared if at least one user
540 * doesn't set it.
541 */
542 res->external_usage |= usage & ~PIPE_HANDLE_USAGE_EXPLICIT_FLUSH;
543 if (!(usage & PIPE_HANDLE_USAGE_EXPLICIT_FLUSH))
544 res->external_usage &= ~PIPE_HANDLE_USAGE_EXPLICIT_FLUSH;
545 } else {
546 res->is_shared = true;
547 res->external_usage = usage;
548 }
549
550 return rscreen->ws->buffer_get_handle(res->buf,
551 rtex->surface.level[0].pitch_bytes,
552 rtex->surface.level[0].offset,
553 rtex->surface.level[0].slice_size,
554 whandle);
555 }
556
557 static void r600_texture_destroy(struct pipe_screen *screen,
558 struct pipe_resource *ptex)
559 {
560 struct r600_texture *rtex = (struct r600_texture*)ptex;
561 struct r600_resource *resource = &rtex->resource;
562
563 r600_texture_reference(&rtex->flushed_depth_texture, NULL);
564
565 r600_resource_reference(&rtex->htile_buffer, NULL);
566 if (rtex->cmask_buffer != &rtex->resource) {
567 r600_resource_reference(&rtex->cmask_buffer, NULL);
568 }
569 pb_reference(&resource->buf, NULL);
570 r600_resource_reference(&rtex->dcc_separate_buffer, NULL);
571 r600_resource_reference(&rtex->last_dcc_separate_buffer, NULL);
572 FREE(rtex);
573 }
574
575 static const struct u_resource_vtbl r600_texture_vtbl;
576
577 /* The number of samples can be specified independently of the texture. */
578 void r600_texture_get_fmask_info(struct r600_common_screen *rscreen,
579 struct r600_texture *rtex,
580 unsigned nr_samples,
581 struct r600_fmask_info *out)
582 {
583 /* FMASK is allocated like an ordinary texture. */
584 struct radeon_surf fmask = rtex->surface;
585
586 memset(out, 0, sizeof(*out));
587
588 fmask.bo_alignment = 0;
589 fmask.bo_size = 0;
590 fmask.nsamples = 1;
591 fmask.flags |= RADEON_SURF_FMASK;
592
593 /* Force 2D tiling if it wasn't set. This may occur when creating
594 * FMASK for MSAA resolve on R6xx. On R6xx, the single-sample
595 * destination buffer must have an FMASK too. */
596 fmask.flags = RADEON_SURF_CLR(fmask.flags, MODE);
597 fmask.flags |= RADEON_SURF_SET(RADEON_SURF_MODE_2D, MODE);
598
599 if (rscreen->chip_class >= SI) {
600 fmask.flags |= RADEON_SURF_HAS_TILE_MODE_INDEX;
601 }
602
603 switch (nr_samples) {
604 case 2:
605 case 4:
606 fmask.bpe = 1;
607 if (rscreen->chip_class <= CAYMAN) {
608 fmask.bankh = 4;
609 }
610 break;
611 case 8:
612 fmask.bpe = 4;
613 break;
614 default:
615 R600_ERR("Invalid sample count for FMASK allocation.\n");
616 return;
617 }
618
619 /* Overallocate FMASK on R600-R700 to fix colorbuffer corruption.
620 * This can be fixed by writing a separate FMASK allocator specifically
621 * for R600-R700 asics. */
622 if (rscreen->chip_class <= R700) {
623 fmask.bpe *= 2;
624 }
625
626 if (rscreen->ws->surface_init(rscreen->ws, &fmask)) {
627 R600_ERR("Got error in surface_init while allocating FMASK.\n");
628 return;
629 }
630
631 assert(fmask.level[0].mode == RADEON_SURF_MODE_2D);
632
633 out->slice_tile_max = (fmask.level[0].nblk_x * fmask.level[0].nblk_y) / 64;
634 if (out->slice_tile_max)
635 out->slice_tile_max -= 1;
636
637 out->tile_mode_index = fmask.tiling_index[0];
638 out->pitch_in_pixels = fmask.level[0].nblk_x;
639 out->bank_height = fmask.bankh;
640 out->alignment = MAX2(256, fmask.bo_alignment);
641 out->size = fmask.bo_size;
642 }
643
644 static void r600_texture_allocate_fmask(struct r600_common_screen *rscreen,
645 struct r600_texture *rtex)
646 {
647 r600_texture_get_fmask_info(rscreen, rtex,
648 rtex->resource.b.b.nr_samples, &rtex->fmask);
649
650 rtex->fmask.offset = align64(rtex->size, rtex->fmask.alignment);
651 rtex->size = rtex->fmask.offset + rtex->fmask.size;
652 }
653
654 void r600_texture_get_cmask_info(struct r600_common_screen *rscreen,
655 struct r600_texture *rtex,
656 struct r600_cmask_info *out)
657 {
658 unsigned cmask_tile_width = 8;
659 unsigned cmask_tile_height = 8;
660 unsigned cmask_tile_elements = cmask_tile_width * cmask_tile_height;
661 unsigned element_bits = 4;
662 unsigned cmask_cache_bits = 1024;
663 unsigned num_pipes = rscreen->info.num_tile_pipes;
664 unsigned pipe_interleave_bytes = rscreen->info.pipe_interleave_bytes;
665
666 unsigned elements_per_macro_tile = (cmask_cache_bits / element_bits) * num_pipes;
667 unsigned pixels_per_macro_tile = elements_per_macro_tile * cmask_tile_elements;
668 unsigned sqrt_pixels_per_macro_tile = sqrt(pixels_per_macro_tile);
669 unsigned macro_tile_width = util_next_power_of_two(sqrt_pixels_per_macro_tile);
670 unsigned macro_tile_height = pixels_per_macro_tile / macro_tile_width;
671
672 unsigned pitch_elements = align(rtex->surface.npix_x, macro_tile_width);
673 unsigned height = align(rtex->surface.npix_y, macro_tile_height);
674
675 unsigned base_align = num_pipes * pipe_interleave_bytes;
676 unsigned slice_bytes =
677 ((pitch_elements * height * element_bits + 7) / 8) / cmask_tile_elements;
678
679 assert(macro_tile_width % 128 == 0);
680 assert(macro_tile_height % 128 == 0);
681
682 out->pitch = pitch_elements;
683 out->height = height;
684 out->xalign = macro_tile_width;
685 out->yalign = macro_tile_height;
686 out->slice_tile_max = ((pitch_elements * height) / (128*128)) - 1;
687 out->alignment = MAX2(256, base_align);
688 out->size = (util_max_layer(&rtex->resource.b.b, 0) + 1) *
689 align(slice_bytes, base_align);
690 }
691
692 static void si_texture_get_cmask_info(struct r600_common_screen *rscreen,
693 struct r600_texture *rtex,
694 struct r600_cmask_info *out)
695 {
696 unsigned pipe_interleave_bytes = rscreen->info.pipe_interleave_bytes;
697 unsigned num_pipes = rscreen->info.num_tile_pipes;
698 unsigned cl_width, cl_height;
699
700 switch (num_pipes) {
701 case 2:
702 cl_width = 32;
703 cl_height = 16;
704 break;
705 case 4:
706 cl_width = 32;
707 cl_height = 32;
708 break;
709 case 8:
710 cl_width = 64;
711 cl_height = 32;
712 break;
713 case 16: /* Hawaii */
714 cl_width = 64;
715 cl_height = 64;
716 break;
717 default:
718 assert(0);
719 return;
720 }
721
722 unsigned base_align = num_pipes * pipe_interleave_bytes;
723
724 unsigned width = align(rtex->surface.npix_x, cl_width*8);
725 unsigned height = align(rtex->surface.npix_y, cl_height*8);
726 unsigned slice_elements = (width * height) / (8*8);
727
728 /* Each element of CMASK is a nibble. */
729 unsigned slice_bytes = slice_elements / 2;
730
731 out->pitch = width;
732 out->height = height;
733 out->xalign = cl_width * 8;
734 out->yalign = cl_height * 8;
735 out->slice_tile_max = (width * height) / (128*128);
736 if (out->slice_tile_max)
737 out->slice_tile_max -= 1;
738
739 out->alignment = MAX2(256, base_align);
740 out->size = (util_max_layer(&rtex->resource.b.b, 0) + 1) *
741 align(slice_bytes, base_align);
742 }
743
744 static void r600_texture_allocate_cmask(struct r600_common_screen *rscreen,
745 struct r600_texture *rtex)
746 {
747 if (rscreen->chip_class >= SI) {
748 si_texture_get_cmask_info(rscreen, rtex, &rtex->cmask);
749 } else {
750 r600_texture_get_cmask_info(rscreen, rtex, &rtex->cmask);
751 }
752
753 rtex->cmask.offset = align64(rtex->size, rtex->cmask.alignment);
754 rtex->size = rtex->cmask.offset + rtex->cmask.size;
755
756 if (rscreen->chip_class >= SI)
757 rtex->cb_color_info |= SI_S_028C70_FAST_CLEAR(1);
758 else
759 rtex->cb_color_info |= EG_S_028C70_FAST_CLEAR(1);
760 }
761
762 static void r600_texture_alloc_cmask_separate(struct r600_common_screen *rscreen,
763 struct r600_texture *rtex)
764 {
765 if (rtex->cmask_buffer)
766 return;
767
768 assert(rtex->cmask.size == 0);
769
770 if (rscreen->chip_class >= SI) {
771 si_texture_get_cmask_info(rscreen, rtex, &rtex->cmask);
772 } else {
773 r600_texture_get_cmask_info(rscreen, rtex, &rtex->cmask);
774 }
775
776 rtex->cmask_buffer = (struct r600_resource *)
777 pipe_buffer_create(&rscreen->b, PIPE_BIND_CUSTOM,
778 PIPE_USAGE_DEFAULT, rtex->cmask.size);
779 if (rtex->cmask_buffer == NULL) {
780 rtex->cmask.size = 0;
781 return;
782 }
783
784 /* update colorbuffer state bits */
785 rtex->cmask.base_address_reg = rtex->cmask_buffer->gpu_address >> 8;
786
787 if (rscreen->chip_class >= SI)
788 rtex->cb_color_info |= SI_S_028C70_FAST_CLEAR(1);
789 else
790 rtex->cb_color_info |= EG_S_028C70_FAST_CLEAR(1);
791
792 p_atomic_inc(&rscreen->compressed_colortex_counter);
793 }
794
795 static unsigned r600_texture_get_htile_size(struct r600_common_screen *rscreen,
796 struct r600_texture *rtex)
797 {
798 unsigned cl_width, cl_height, width, height;
799 unsigned slice_elements, slice_bytes, pipe_interleave_bytes, base_align;
800 unsigned num_pipes = rscreen->info.num_tile_pipes;
801
802 if (rscreen->chip_class <= EVERGREEN &&
803 rscreen->info.drm_major == 2 && rscreen->info.drm_minor < 26)
804 return 0;
805
806 /* HW bug on R6xx. */
807 if (rscreen->chip_class == R600 &&
808 (rtex->surface.level[0].npix_x > 7680 ||
809 rtex->surface.level[0].npix_y > 7680))
810 return 0;
811
812 /* HTILE is broken with 1D tiling on old kernels and CIK. */
813 if (rscreen->chip_class >= CIK &&
814 rtex->surface.level[0].mode == RADEON_SURF_MODE_1D &&
815 rscreen->info.drm_major == 2 && rscreen->info.drm_minor < 38)
816 return 0;
817
818 /* Overalign HTILE on P2 configs to work around GPU hangs in
819 * piglit/depthstencil-render-miplevels 585.
820 *
821 * This has been confirmed to help Kabini & Stoney, where the hangs
822 * are always reproducible. I think I have seen the test hang
823 * on Carrizo too, though it was very rare there.
824 */
825 if (rscreen->chip_class >= CIK && num_pipes < 4)
826 num_pipes = 4;
827
828 switch (num_pipes) {
829 case 1:
830 cl_width = 32;
831 cl_height = 16;
832 break;
833 case 2:
834 cl_width = 32;
835 cl_height = 32;
836 break;
837 case 4:
838 cl_width = 64;
839 cl_height = 32;
840 break;
841 case 8:
842 cl_width = 64;
843 cl_height = 64;
844 break;
845 case 16:
846 cl_width = 128;
847 cl_height = 64;
848 break;
849 default:
850 assert(0);
851 return 0;
852 }
853
854 width = align(rtex->surface.npix_x, cl_width * 8);
855 height = align(rtex->surface.npix_y, cl_height * 8);
856
857 slice_elements = (width * height) / (8 * 8);
858 slice_bytes = slice_elements * 4;
859
860 pipe_interleave_bytes = rscreen->info.pipe_interleave_bytes;
861 base_align = num_pipes * pipe_interleave_bytes;
862
863 rtex->htile.pitch = width;
864 rtex->htile.height = height;
865 rtex->htile.xalign = cl_width * 8;
866 rtex->htile.yalign = cl_height * 8;
867
868 return (util_max_layer(&rtex->resource.b.b, 0) + 1) *
869 align(slice_bytes, base_align);
870 }
871
872 static void r600_texture_allocate_htile(struct r600_common_screen *rscreen,
873 struct r600_texture *rtex)
874 {
875 unsigned htile_size = r600_texture_get_htile_size(rscreen, rtex);
876
877 if (!htile_size)
878 return;
879
880 rtex->htile_buffer = (struct r600_resource*)
881 pipe_buffer_create(&rscreen->b, PIPE_BIND_CUSTOM,
882 PIPE_USAGE_DEFAULT, htile_size);
883 if (rtex->htile_buffer == NULL) {
884 /* this is not a fatal error as we can still keep rendering
885 * without htile buffer */
886 R600_ERR("Failed to create buffer object for htile buffer.\n");
887 } else {
888 r600_screen_clear_buffer(rscreen, &rtex->htile_buffer->b.b, 0,
889 htile_size, 0, R600_COHERENCY_NONE);
890 }
891 }
892
893 void r600_print_texture_info(struct r600_texture *rtex, FILE *f)
894 {
895 int i;
896
897 fprintf(f, " Info: npix_x=%u, npix_y=%u, npix_z=%u, blk_w=%u, "
898 "blk_h=%u, blk_d=%u, array_size=%u, last_level=%u, "
899 "bpe=%u, nsamples=%u, flags=0x%x, %s\n",
900 rtex->surface.npix_x, rtex->surface.npix_y,
901 rtex->surface.npix_z, rtex->surface.blk_w,
902 rtex->surface.blk_h, rtex->surface.blk_d,
903 rtex->surface.array_size, rtex->surface.last_level,
904 rtex->surface.bpe, rtex->surface.nsamples,
905 rtex->surface.flags, util_format_short_name(rtex->resource.b.b.format));
906
907 fprintf(f, " Layout: size=%"PRIu64", alignment=%"PRIu64", bankw=%u, "
908 "bankh=%u, nbanks=%u, mtilea=%u, tilesplit=%u, pipeconfig=%u, scanout=%u\n",
909 rtex->surface.bo_size, rtex->surface.bo_alignment, rtex->surface.bankw,
910 rtex->surface.bankh, rtex->surface.num_banks, rtex->surface.mtilea,
911 rtex->surface.tile_split, rtex->surface.pipe_config,
912 (rtex->surface.flags & RADEON_SURF_SCANOUT) != 0);
913
914 if (rtex->fmask.size)
915 fprintf(f, " FMask: offset=%"PRIu64", size=%"PRIu64", alignment=%u, pitch_in_pixels=%u, "
916 "bankh=%u, slice_tile_max=%u, tile_mode_index=%u\n",
917 rtex->fmask.offset, rtex->fmask.size, rtex->fmask.alignment,
918 rtex->fmask.pitch_in_pixels, rtex->fmask.bank_height,
919 rtex->fmask.slice_tile_max, rtex->fmask.tile_mode_index);
920
921 if (rtex->cmask.size)
922 fprintf(f, " CMask: offset=%"PRIu64", size=%"PRIu64", alignment=%u, pitch=%u, "
923 "height=%u, xalign=%u, yalign=%u, slice_tile_max=%u\n",
924 rtex->cmask.offset, rtex->cmask.size, rtex->cmask.alignment,
925 rtex->cmask.pitch, rtex->cmask.height, rtex->cmask.xalign,
926 rtex->cmask.yalign, rtex->cmask.slice_tile_max);
927
928 if (rtex->htile_buffer)
929 fprintf(f, " HTile: size=%u, alignment=%u, pitch=%u, height=%u, "
930 "xalign=%u, yalign=%u\n",
931 rtex->htile_buffer->b.b.width0,
932 rtex->htile_buffer->buf->alignment, rtex->htile.pitch,
933 rtex->htile.height, rtex->htile.xalign, rtex->htile.yalign);
934
935 if (rtex->dcc_offset) {
936 fprintf(f, " DCC: offset=%"PRIu64", size=%"PRIu64", alignment=%"PRIu64"\n",
937 rtex->dcc_offset, rtex->surface.dcc_size,
938 rtex->surface.dcc_alignment);
939 for (i = 0; i <= rtex->surface.last_level; i++)
940 fprintf(f, " DCCLevel[%i]: enabled=%u, offset=%"PRIu64", "
941 "fast_clear_size=%"PRIu64"\n",
942 i, rtex->surface.level[i].dcc_enabled,
943 rtex->surface.level[i].dcc_offset,
944 rtex->surface.level[i].dcc_fast_clear_size);
945 }
946
947 for (i = 0; i <= rtex->surface.last_level; i++)
948 fprintf(f, " Level[%i]: offset=%"PRIu64", slice_size=%"PRIu64", "
949 "npix_x=%u, npix_y=%u, npix_z=%u, nblk_x=%u, nblk_y=%u, "
950 "nblk_z=%u, pitch_bytes=%u, mode=%u\n",
951 i, rtex->surface.level[i].offset,
952 rtex->surface.level[i].slice_size,
953 u_minify(rtex->resource.b.b.width0, i),
954 u_minify(rtex->resource.b.b.height0, i),
955 u_minify(rtex->resource.b.b.depth0, i),
956 rtex->surface.level[i].nblk_x,
957 rtex->surface.level[i].nblk_y,
958 rtex->surface.level[i].nblk_z,
959 rtex->surface.level[i].pitch_bytes,
960 rtex->surface.level[i].mode);
961
962 if (rtex->surface.flags & RADEON_SURF_SBUFFER) {
963 fprintf(f, " StencilLayout: tilesplit=%u\n",
964 rtex->surface.stencil_tile_split);
965 for (i = 0; i <= rtex->surface.last_level; i++) {
966 fprintf(f, " StencilLevel[%i]: offset=%"PRIu64", "
967 "slice_size=%"PRIu64", npix_x=%u, "
968 "npix_y=%u, npix_z=%u, nblk_x=%u, nblk_y=%u, "
969 "nblk_z=%u, pitch_bytes=%u, mode=%u\n",
970 i, rtex->surface.stencil_level[i].offset,
971 rtex->surface.stencil_level[i].slice_size,
972 u_minify(rtex->resource.b.b.width0, i),
973 u_minify(rtex->resource.b.b.height0, i),
974 u_minify(rtex->resource.b.b.depth0, i),
975 rtex->surface.stencil_level[i].nblk_x,
976 rtex->surface.stencil_level[i].nblk_y,
977 rtex->surface.stencil_level[i].nblk_z,
978 rtex->surface.stencil_level[i].pitch_bytes,
979 rtex->surface.stencil_level[i].mode);
980 }
981 }
982 }
983
984 /* Common processing for r600_texture_create and r600_texture_from_handle */
985 static struct r600_texture *
986 r600_texture_create_object(struct pipe_screen *screen,
987 const struct pipe_resource *base,
988 unsigned pitch_in_bytes_override,
989 unsigned offset,
990 struct pb_buffer *buf,
991 struct radeon_surf *surface)
992 {
993 struct r600_texture *rtex;
994 struct r600_resource *resource;
995 struct r600_common_screen *rscreen = (struct r600_common_screen*)screen;
996
997 rtex = CALLOC_STRUCT(r600_texture);
998 if (!rtex)
999 return NULL;
1000
1001 resource = &rtex->resource;
1002 resource->b.b = *base;
1003 resource->b.vtbl = &r600_texture_vtbl;
1004 pipe_reference_init(&resource->b.b.reference, 1);
1005 resource->b.b.screen = screen;
1006
1007 /* don't include stencil-only formats which we don't support for rendering */
1008 rtex->is_depth = util_format_has_depth(util_format_description(rtex->resource.b.b.format));
1009
1010 rtex->surface = *surface;
1011 if (r600_setup_surface(screen, rtex, pitch_in_bytes_override, offset)) {
1012 FREE(rtex);
1013 return NULL;
1014 }
1015
1016 /* Tiled depth textures utilize the non-displayable tile order.
1017 * This must be done after r600_setup_surface.
1018 * Applies to R600-Cayman. */
1019 rtex->non_disp_tiling = rtex->is_depth && rtex->surface.level[0].mode >= RADEON_SURF_MODE_1D;
1020 /* Applies to GCN. */
1021 rtex->last_msaa_resolve_target_micro_mode = rtex->surface.micro_tile_mode;
1022
1023 /* Disable separate DCC at the beginning. DRI2 doesn't reuse buffers
1024 * between frames, so the only thing that can enable separate DCC
1025 * with DRI2 is multiple slow clears within a frame.
1026 */
1027 rtex->ps_draw_ratio = 0;
1028
1029 if (rtex->is_depth) {
1030 if (base->flags & (R600_RESOURCE_FLAG_TRANSFER |
1031 R600_RESOURCE_FLAG_FLUSHED_DEPTH) ||
1032 rscreen->chip_class >= EVERGREEN) {
1033 rtex->can_sample_z = !rtex->surface.depth_adjusted;
1034 rtex->can_sample_s = !rtex->surface.stencil_adjusted;
1035 } else {
1036 if (rtex->resource.b.b.nr_samples <= 1 &&
1037 (rtex->resource.b.b.format == PIPE_FORMAT_Z16_UNORM ||
1038 rtex->resource.b.b.format == PIPE_FORMAT_Z32_FLOAT))
1039 rtex->can_sample_z = true;
1040 }
1041
1042 if (!(base->flags & (R600_RESOURCE_FLAG_TRANSFER |
1043 R600_RESOURCE_FLAG_FLUSHED_DEPTH))) {
1044 rtex->db_compatible = true;
1045
1046 if (!(rscreen->debug_flags & DBG_NO_HYPERZ))
1047 r600_texture_allocate_htile(rscreen, rtex);
1048 }
1049 } else {
1050 if (base->nr_samples > 1) {
1051 if (!buf) {
1052 r600_texture_allocate_fmask(rscreen, rtex);
1053 r600_texture_allocate_cmask(rscreen, rtex);
1054 rtex->cmask_buffer = &rtex->resource;
1055 }
1056 if (!rtex->fmask.size || !rtex->cmask.size) {
1057 FREE(rtex);
1058 return NULL;
1059 }
1060 }
1061
1062 /* Shared textures must always set up DCC here.
1063 * If it's not present, it will be disabled by
1064 * apply_opaque_metadata later.
1065 */
1066 if (rtex->surface.dcc_size &&
1067 (buf || !(rscreen->debug_flags & DBG_NO_DCC)) &&
1068 !(rtex->surface.flags & RADEON_SURF_SCANOUT)) {
1069 /* Reserve space for the DCC buffer. */
1070 rtex->dcc_offset = align64(rtex->size, rtex->surface.dcc_alignment);
1071 rtex->size = rtex->dcc_offset + rtex->surface.dcc_size;
1072 }
1073 }
1074
1075 /* Now create the backing buffer. */
1076 if (!buf) {
1077 if (!r600_init_resource(rscreen, resource, rtex->size,
1078 rtex->surface.bo_alignment)) {
1079 FREE(rtex);
1080 return NULL;
1081 }
1082 } else {
1083 resource->buf = buf;
1084 resource->gpu_address = rscreen->ws->buffer_get_virtual_address(resource->buf);
1085 resource->domains = rscreen->ws->buffer_get_initial_domain(resource->buf);
1086 }
1087
1088 if (rtex->cmask.size) {
1089 /* Initialize the cmask to 0xCC (= compressed state). */
1090 r600_screen_clear_buffer(rscreen, &rtex->cmask_buffer->b.b,
1091 rtex->cmask.offset, rtex->cmask.size,
1092 0xCCCCCCCC, R600_COHERENCY_NONE);
1093 }
1094
1095 /* Initialize DCC only if the texture is not being imported. */
1096 if (!buf && rtex->dcc_offset) {
1097 r600_screen_clear_buffer(rscreen, &rtex->resource.b.b,
1098 rtex->dcc_offset,
1099 rtex->surface.dcc_size,
1100 0xFFFFFFFF, R600_COHERENCY_NONE);
1101 }
1102
1103 /* Initialize the CMASK base register value. */
1104 rtex->cmask.base_address_reg =
1105 (rtex->resource.gpu_address + rtex->cmask.offset) >> 8;
1106
1107 if (rscreen->debug_flags & DBG_VM) {
1108 fprintf(stderr, "VM start=0x%"PRIX64" end=0x%"PRIX64" | Texture %ix%ix%i, %i levels, %i samples, %s\n",
1109 rtex->resource.gpu_address,
1110 rtex->resource.gpu_address + rtex->resource.buf->size,
1111 base->width0, base->height0, util_max_layer(base, 0)+1, base->last_level+1,
1112 base->nr_samples ? base->nr_samples : 1, util_format_short_name(base->format));
1113 }
1114
1115 if (rscreen->debug_flags & DBG_TEX) {
1116 puts("Texture:");
1117 r600_print_texture_info(rtex, stdout);
1118 fflush(stdout);
1119 }
1120
1121 return rtex;
1122 }
1123
1124 static unsigned r600_choose_tiling(struct r600_common_screen *rscreen,
1125 const struct pipe_resource *templ)
1126 {
1127 const struct util_format_description *desc = util_format_description(templ->format);
1128 bool force_tiling = templ->flags & R600_RESOURCE_FLAG_FORCE_TILING;
1129
1130 /* MSAA resources must be 2D tiled. */
1131 if (templ->nr_samples > 1)
1132 return RADEON_SURF_MODE_2D;
1133
1134 /* Transfer resources should be linear. */
1135 if (templ->flags & R600_RESOURCE_FLAG_TRANSFER)
1136 return RADEON_SURF_MODE_LINEAR_ALIGNED;
1137
1138 /* r600g: force tiling on TEXTURE_2D and TEXTURE_3D compute resources. */
1139 if (rscreen->chip_class >= R600 && rscreen->chip_class <= CAYMAN &&
1140 (templ->bind & PIPE_BIND_COMPUTE_RESOURCE) &&
1141 (templ->target == PIPE_TEXTURE_2D ||
1142 templ->target == PIPE_TEXTURE_3D))
1143 force_tiling = true;
1144
1145 /* Handle common candidates for the linear mode.
1146 * Compressed textures and DB surfaces must always be tiled.
1147 */
1148 if (!force_tiling && !util_format_is_compressed(templ->format) &&
1149 (!util_format_is_depth_or_stencil(templ->format) ||
1150 templ->flags & R600_RESOURCE_FLAG_FLUSHED_DEPTH)) {
1151 if (rscreen->debug_flags & DBG_NO_TILING)
1152 return RADEON_SURF_MODE_LINEAR_ALIGNED;
1153
1154 /* Tiling doesn't work with the 422 (SUBSAMPLED) formats on R600+. */
1155 if (desc->layout == UTIL_FORMAT_LAYOUT_SUBSAMPLED)
1156 return RADEON_SURF_MODE_LINEAR_ALIGNED;
1157
1158 /* Cursors are linear on SI.
1159 * (XXX double-check, maybe also use RADEON_SURF_SCANOUT) */
1160 if (rscreen->chip_class >= SI &&
1161 (templ->bind & PIPE_BIND_CURSOR))
1162 return RADEON_SURF_MODE_LINEAR_ALIGNED;
1163
1164 if (templ->bind & PIPE_BIND_LINEAR)
1165 return RADEON_SURF_MODE_LINEAR_ALIGNED;
1166
1167 /* Textures with a very small height are recommended to be linear. */
1168 if (templ->target == PIPE_TEXTURE_1D ||
1169 templ->target == PIPE_TEXTURE_1D_ARRAY ||
1170 templ->height0 <= 4)
1171 return RADEON_SURF_MODE_LINEAR_ALIGNED;
1172
1173 /* Textures likely to be mapped often. */
1174 if (templ->usage == PIPE_USAGE_STAGING ||
1175 templ->usage == PIPE_USAGE_STREAM)
1176 return RADEON_SURF_MODE_LINEAR_ALIGNED;
1177 }
1178
1179 /* Make small textures 1D tiled. */
1180 if (templ->width0 <= 16 || templ->height0 <= 16 ||
1181 (rscreen->debug_flags & DBG_NO_2D_TILING))
1182 return RADEON_SURF_MODE_1D;
1183
1184 /* The allocator will switch to 1D if needed. */
1185 return RADEON_SURF_MODE_2D;
1186 }
1187
1188 struct pipe_resource *r600_texture_create(struct pipe_screen *screen,
1189 const struct pipe_resource *templ)
1190 {
1191 struct r600_common_screen *rscreen = (struct r600_common_screen*)screen;
1192 struct radeon_surf surface = {0};
1193 int r;
1194
1195 r = r600_init_surface(rscreen, &surface, templ,
1196 r600_choose_tiling(rscreen, templ),
1197 templ->flags & R600_RESOURCE_FLAG_FLUSHED_DEPTH);
1198 if (r) {
1199 return NULL;
1200 }
1201 r = rscreen->ws->surface_best(rscreen->ws, &surface);
1202 if (r) {
1203 return NULL;
1204 }
1205 return (struct pipe_resource *)r600_texture_create_object(screen, templ, 0,
1206 0, NULL, &surface);
1207 }
1208
1209 static struct pipe_resource *r600_texture_from_handle(struct pipe_screen *screen,
1210 const struct pipe_resource *templ,
1211 struct winsys_handle *whandle,
1212 unsigned usage)
1213 {
1214 struct r600_common_screen *rscreen = (struct r600_common_screen*)screen;
1215 struct pb_buffer *buf = NULL;
1216 unsigned stride = 0, offset = 0;
1217 unsigned array_mode;
1218 struct radeon_surf surface;
1219 int r;
1220 struct radeon_bo_metadata metadata = {};
1221 struct r600_texture *rtex;
1222
1223 /* Support only 2D textures without mipmaps */
1224 if ((templ->target != PIPE_TEXTURE_2D && templ->target != PIPE_TEXTURE_RECT) ||
1225 templ->depth0 != 1 || templ->last_level != 0)
1226 return NULL;
1227
1228 buf = rscreen->ws->buffer_from_handle(rscreen->ws, whandle, &stride, &offset);
1229 if (!buf)
1230 return NULL;
1231
1232 rscreen->ws->buffer_get_metadata(buf, &metadata);
1233
1234 surface.pipe_config = metadata.pipe_config;
1235 surface.bankw = metadata.bankw;
1236 surface.bankh = metadata.bankh;
1237 surface.tile_split = metadata.tile_split;
1238 surface.mtilea = metadata.mtilea;
1239 surface.num_banks = metadata.num_banks;
1240
1241 if (metadata.macrotile == RADEON_LAYOUT_TILED)
1242 array_mode = RADEON_SURF_MODE_2D;
1243 else if (metadata.microtile == RADEON_LAYOUT_TILED)
1244 array_mode = RADEON_SURF_MODE_1D;
1245 else
1246 array_mode = RADEON_SURF_MODE_LINEAR_ALIGNED;
1247
1248 r = r600_init_surface(rscreen, &surface, templ, array_mode, false);
1249 if (r) {
1250 return NULL;
1251 }
1252
1253 if (metadata.scanout)
1254 surface.flags |= RADEON_SURF_SCANOUT;
1255
1256 rtex = r600_texture_create_object(screen, templ, stride,
1257 offset, buf, &surface);
1258 if (!rtex)
1259 return NULL;
1260
1261 rtex->resource.is_shared = true;
1262 rtex->resource.external_usage = usage;
1263
1264 if (rscreen->apply_opaque_metadata)
1265 rscreen->apply_opaque_metadata(rscreen, rtex, &metadata);
1266
1267 return &rtex->resource.b.b;
1268 }
1269
1270 bool r600_init_flushed_depth_texture(struct pipe_context *ctx,
1271 struct pipe_resource *texture,
1272 struct r600_texture **staging)
1273 {
1274 struct r600_texture *rtex = (struct r600_texture*)texture;
1275 struct pipe_resource resource;
1276 struct r600_texture **flushed_depth_texture = staging ?
1277 staging : &rtex->flushed_depth_texture;
1278 enum pipe_format pipe_format = texture->format;
1279
1280 if (!staging) {
1281 if (rtex->flushed_depth_texture)
1282 return true; /* it's ready */
1283
1284 if (!rtex->can_sample_z && rtex->can_sample_s) {
1285 switch (pipe_format) {
1286 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
1287 /* Save memory by not allocating the S plane. */
1288 pipe_format = PIPE_FORMAT_Z32_FLOAT;
1289 break;
1290 case PIPE_FORMAT_Z24_UNORM_S8_UINT:
1291 case PIPE_FORMAT_S8_UINT_Z24_UNORM:
1292 /* Save memory bandwidth by not copying the
1293 * stencil part during flush.
1294 *
1295 * This potentially increases memory bandwidth
1296 * if an application uses both Z and S texturing
1297 * simultaneously (a flushed Z24S8 texture
1298 * would be stored compactly), but how often
1299 * does that really happen?
1300 */
1301 pipe_format = PIPE_FORMAT_Z24X8_UNORM;
1302 break;
1303 default:;
1304 }
1305 } else if (!rtex->can_sample_s && rtex->can_sample_z) {
1306 assert(util_format_has_stencil(util_format_description(pipe_format)));
1307
1308 /* DB->CB copies to an 8bpp surface don't work. */
1309 pipe_format = PIPE_FORMAT_X24S8_UINT;
1310 }
1311 }
1312
1313 resource.target = texture->target;
1314 resource.format = pipe_format;
1315 resource.width0 = texture->width0;
1316 resource.height0 = texture->height0;
1317 resource.depth0 = texture->depth0;
1318 resource.array_size = texture->array_size;
1319 resource.last_level = texture->last_level;
1320 resource.nr_samples = texture->nr_samples;
1321 resource.usage = staging ? PIPE_USAGE_STAGING : PIPE_USAGE_DEFAULT;
1322 resource.bind = texture->bind & ~PIPE_BIND_DEPTH_STENCIL;
1323 resource.flags = texture->flags | R600_RESOURCE_FLAG_FLUSHED_DEPTH;
1324
1325 if (staging)
1326 resource.flags |= R600_RESOURCE_FLAG_TRANSFER;
1327
1328 *flushed_depth_texture = (struct r600_texture *)ctx->screen->resource_create(ctx->screen, &resource);
1329 if (*flushed_depth_texture == NULL) {
1330 R600_ERR("failed to create temporary texture to hold flushed depth\n");
1331 return false;
1332 }
1333
1334 (*flushed_depth_texture)->non_disp_tiling = false;
1335 return true;
1336 }
1337
1338 /**
1339 * Initialize the pipe_resource descriptor to be of the same size as the box,
1340 * which is supposed to hold a subregion of the texture "orig" at the given
1341 * mipmap level.
1342 */
1343 static void r600_init_temp_resource_from_box(struct pipe_resource *res,
1344 struct pipe_resource *orig,
1345 const struct pipe_box *box,
1346 unsigned level, unsigned flags)
1347 {
1348 memset(res, 0, sizeof(*res));
1349 res->format = orig->format;
1350 res->width0 = box->width;
1351 res->height0 = box->height;
1352 res->depth0 = 1;
1353 res->array_size = 1;
1354 res->usage = flags & R600_RESOURCE_FLAG_TRANSFER ? PIPE_USAGE_STAGING : PIPE_USAGE_DEFAULT;
1355 res->flags = flags;
1356
1357 /* We must set the correct texture target and dimensions for a 3D box. */
1358 if (box->depth > 1 && util_max_layer(orig, level) > 0) {
1359 res->target = PIPE_TEXTURE_2D_ARRAY;
1360 res->array_size = box->depth;
1361 } else {
1362 res->target = PIPE_TEXTURE_2D;
1363 }
1364 }
1365
1366 static bool r600_can_invalidate_texture(struct r600_common_screen *rscreen,
1367 struct r600_texture *rtex,
1368 unsigned transfer_usage,
1369 const struct pipe_box *box)
1370 {
1371 /* r600g doesn't react to dirty_tex_descriptor_counter */
1372 return rscreen->chip_class >= SI &&
1373 !rtex->resource.is_shared &&
1374 !(transfer_usage & PIPE_TRANSFER_READ) &&
1375 rtex->resource.b.b.last_level == 0 &&
1376 util_texrange_covers_whole_level(&rtex->resource.b.b, 0,
1377 box->x, box->y, box->z,
1378 box->width, box->height,
1379 box->depth);
1380 }
1381
1382 static void r600_texture_invalidate_storage(struct r600_common_context *rctx,
1383 struct r600_texture *rtex)
1384 {
1385 struct r600_common_screen *rscreen = rctx->screen;
1386
1387 /* There is no point in discarding depth and tiled buffers. */
1388 assert(!rtex->is_depth);
1389 assert(rtex->surface.level[0].mode == RADEON_SURF_MODE_LINEAR_ALIGNED);
1390
1391 /* Reallocate the buffer in the same pipe_resource. */
1392 r600_init_resource(rscreen, &rtex->resource, rtex->size,
1393 rtex->surface.bo_alignment);
1394
1395 /* Initialize the CMASK base address (needed even without CMASK). */
1396 rtex->cmask.base_address_reg =
1397 (rtex->resource.gpu_address + rtex->cmask.offset) >> 8;
1398
1399 r600_dirty_all_framebuffer_states(rscreen);
1400 p_atomic_inc(&rscreen->dirty_tex_descriptor_counter);
1401
1402 rctx->num_alloc_tex_transfer_bytes += rtex->size;
1403 }
1404
1405 static void *r600_texture_transfer_map(struct pipe_context *ctx,
1406 struct pipe_resource *texture,
1407 unsigned level,
1408 unsigned usage,
1409 const struct pipe_box *box,
1410 struct pipe_transfer **ptransfer)
1411 {
1412 struct r600_common_context *rctx = (struct r600_common_context*)ctx;
1413 struct r600_texture *rtex = (struct r600_texture*)texture;
1414 struct r600_transfer *trans;
1415 struct r600_resource *buf;
1416 unsigned offset = 0;
1417 char *map;
1418 bool use_staging_texture = false;
1419
1420 assert(!(texture->flags & R600_RESOURCE_FLAG_TRANSFER));
1421
1422 /* Depth textures use staging unconditionally. */
1423 if (!rtex->is_depth) {
1424 /* Degrade the tile mode if we get too many transfers on APUs.
1425 * On dGPUs, the staging texture is always faster.
1426 * Only count uploads that are at least 4x4 pixels large.
1427 */
1428 if (!rctx->screen->info.has_dedicated_vram &&
1429 level == 0 &&
1430 box->width >= 4 && box->height >= 4 &&
1431 p_atomic_inc_return(&rtex->num_level0_transfers) == 10) {
1432 bool can_invalidate =
1433 r600_can_invalidate_texture(rctx->screen, rtex,
1434 usage, box);
1435
1436 r600_degrade_tile_mode_to_linear(rctx, rtex,
1437 can_invalidate);
1438 }
1439
1440 /* Tiled textures need to be converted into a linear texture for CPU
1441 * access. The staging texture is always linear and is placed in GART.
1442 *
1443 * Reading from VRAM is slow, always use the staging texture in
1444 * this case.
1445 *
1446 * Use the staging texture for uploads if the underlying BO
1447 * is busy.
1448 */
1449 if (rtex->surface.level[0].mode >= RADEON_SURF_MODE_1D)
1450 use_staging_texture = true;
1451 else if (usage & PIPE_TRANSFER_READ)
1452 use_staging_texture = (rtex->resource.domains &
1453 RADEON_DOMAIN_VRAM) != 0;
1454 /* Write & linear only: */
1455 else if (r600_rings_is_buffer_referenced(rctx, rtex->resource.buf,
1456 RADEON_USAGE_READWRITE) ||
1457 !rctx->ws->buffer_wait(rtex->resource.buf, 0,
1458 RADEON_USAGE_READWRITE)) {
1459 /* It's busy. */
1460 if (r600_can_invalidate_texture(rctx->screen, rtex,
1461 usage, box))
1462 r600_texture_invalidate_storage(rctx, rtex);
1463 else
1464 use_staging_texture = true;
1465 }
1466 }
1467
1468 trans = CALLOC_STRUCT(r600_transfer);
1469 if (!trans)
1470 return NULL;
1471 trans->transfer.resource = texture;
1472 trans->transfer.level = level;
1473 trans->transfer.usage = usage;
1474 trans->transfer.box = *box;
1475
1476 if (rtex->is_depth) {
1477 struct r600_texture *staging_depth;
1478
1479 if (rtex->resource.b.b.nr_samples > 1) {
1480 /* MSAA depth buffers need to be converted to single sample buffers.
1481 *
1482 * Mapping MSAA depth buffers can occur if ReadPixels is called
1483 * with a multisample GLX visual.
1484 *
1485 * First downsample the depth buffer to a temporary texture,
1486 * then decompress the temporary one to staging.
1487 *
1488 * Only the region being mapped is transfered.
1489 */
1490 struct pipe_resource resource;
1491
1492 r600_init_temp_resource_from_box(&resource, texture, box, level, 0);
1493
1494 if (!r600_init_flushed_depth_texture(ctx, &resource, &staging_depth)) {
1495 R600_ERR("failed to create temporary texture to hold untiled copy\n");
1496 FREE(trans);
1497 return NULL;
1498 }
1499
1500 if (usage & PIPE_TRANSFER_READ) {
1501 struct pipe_resource *temp = ctx->screen->resource_create(ctx->screen, &resource);
1502 if (!temp) {
1503 R600_ERR("failed to create a temporary depth texture\n");
1504 FREE(trans);
1505 return NULL;
1506 }
1507
1508 r600_copy_region_with_blit(ctx, temp, 0, 0, 0, 0, texture, level, box);
1509 rctx->blit_decompress_depth(ctx, (struct r600_texture*)temp, staging_depth,
1510 0, 0, 0, box->depth, 0, 0);
1511 pipe_resource_reference(&temp, NULL);
1512 }
1513 }
1514 else {
1515 /* XXX: only readback the rectangle which is being mapped? */
1516 /* XXX: when discard is true, no need to read back from depth texture */
1517 if (!r600_init_flushed_depth_texture(ctx, texture, &staging_depth)) {
1518 R600_ERR("failed to create temporary texture to hold untiled copy\n");
1519 FREE(trans);
1520 return NULL;
1521 }
1522
1523 rctx->blit_decompress_depth(ctx, rtex, staging_depth,
1524 level, level,
1525 box->z, box->z + box->depth - 1,
1526 0, 0);
1527
1528 offset = r600_texture_get_offset(staging_depth, level, box);
1529 }
1530
1531 trans->transfer.stride = staging_depth->surface.level[level].pitch_bytes;
1532 trans->transfer.layer_stride = staging_depth->surface.level[level].slice_size;
1533 trans->staging = (struct r600_resource*)staging_depth;
1534 buf = trans->staging;
1535 } else if (use_staging_texture) {
1536 struct pipe_resource resource;
1537 struct r600_texture *staging;
1538
1539 r600_init_temp_resource_from_box(&resource, texture, box, level,
1540 R600_RESOURCE_FLAG_TRANSFER);
1541 resource.usage = (usage & PIPE_TRANSFER_READ) ?
1542 PIPE_USAGE_STAGING : PIPE_USAGE_STREAM;
1543
1544 /* Create the temporary texture. */
1545 staging = (struct r600_texture*)ctx->screen->resource_create(ctx->screen, &resource);
1546 if (!staging) {
1547 R600_ERR("failed to create temporary texture to hold untiled copy\n");
1548 FREE(trans);
1549 return NULL;
1550 }
1551 trans->staging = &staging->resource;
1552 trans->transfer.stride = staging->surface.level[0].pitch_bytes;
1553 trans->transfer.layer_stride = staging->surface.level[0].slice_size;
1554
1555 if (usage & PIPE_TRANSFER_READ)
1556 r600_copy_to_staging_texture(ctx, trans);
1557 else
1558 usage |= PIPE_TRANSFER_UNSYNCHRONIZED;
1559
1560 buf = trans->staging;
1561 } else {
1562 /* the resource is mapped directly */
1563 trans->transfer.stride = rtex->surface.level[level].pitch_bytes;
1564 trans->transfer.layer_stride = rtex->surface.level[level].slice_size;
1565 offset = r600_texture_get_offset(rtex, level, box);
1566 buf = &rtex->resource;
1567 }
1568
1569 if (!(map = r600_buffer_map_sync_with_rings(rctx, buf, usage))) {
1570 r600_resource_reference(&trans->staging, NULL);
1571 FREE(trans);
1572 return NULL;
1573 }
1574
1575 *ptransfer = &trans->transfer;
1576 return map + offset;
1577 }
1578
1579 static void r600_texture_transfer_unmap(struct pipe_context *ctx,
1580 struct pipe_transfer* transfer)
1581 {
1582 struct r600_common_context *rctx = (struct r600_common_context*)ctx;
1583 struct r600_transfer *rtransfer = (struct r600_transfer*)transfer;
1584 struct pipe_resource *texture = transfer->resource;
1585 struct r600_texture *rtex = (struct r600_texture*)texture;
1586
1587 if ((transfer->usage & PIPE_TRANSFER_WRITE) && rtransfer->staging) {
1588 if (rtex->is_depth && rtex->resource.b.b.nr_samples <= 1) {
1589 ctx->resource_copy_region(ctx, texture, transfer->level,
1590 transfer->box.x, transfer->box.y, transfer->box.z,
1591 &rtransfer->staging->b.b, transfer->level,
1592 &transfer->box);
1593 } else {
1594 r600_copy_from_staging_texture(ctx, rtransfer);
1595 }
1596 }
1597
1598 if (rtransfer->staging) {
1599 rctx->num_alloc_tex_transfer_bytes += rtransfer->staging->buf->size;
1600 r600_resource_reference(&rtransfer->staging, NULL);
1601 }
1602
1603 /* Heuristic for {upload, draw, upload, draw, ..}:
1604 *
1605 * Flush the gfx IB if we've allocated too much texture storage.
1606 *
1607 * The idea is that we don't want to build IBs that use too much
1608 * memory and put pressure on the kernel memory manager and we also
1609 * want to make temporary and invalidated buffers go idle ASAP to
1610 * decrease the total memory usage or make them reusable. The memory
1611 * usage will be slightly higher than given here because of the buffer
1612 * cache in the winsys.
1613 *
1614 * The result is that the kernel memory manager is never a bottleneck.
1615 */
1616 if (rctx->num_alloc_tex_transfer_bytes > rctx->screen->info.gart_size / 4) {
1617 rctx->gfx.flush(rctx, RADEON_FLUSH_ASYNC, NULL);
1618 rctx->num_alloc_tex_transfer_bytes = 0;
1619 }
1620
1621 FREE(transfer);
1622 }
1623
1624 static const struct u_resource_vtbl r600_texture_vtbl =
1625 {
1626 NULL, /* get_handle */
1627 r600_texture_destroy, /* resource_destroy */
1628 r600_texture_transfer_map, /* transfer_map */
1629 u_default_transfer_flush_region, /* transfer_flush_region */
1630 r600_texture_transfer_unmap, /* transfer_unmap */
1631 };
1632
1633 struct pipe_surface *r600_create_surface_custom(struct pipe_context *pipe,
1634 struct pipe_resource *texture,
1635 const struct pipe_surface *templ,
1636 unsigned width, unsigned height)
1637 {
1638 struct r600_texture *rtex = (struct r600_texture*)texture;
1639 struct r600_surface *surface = CALLOC_STRUCT(r600_surface);
1640
1641 if (!surface)
1642 return NULL;
1643
1644 assert(templ->u.tex.first_layer <= util_max_layer(texture, templ->u.tex.level));
1645 assert(templ->u.tex.last_layer <= util_max_layer(texture, templ->u.tex.level));
1646
1647 pipe_reference_init(&surface->base.reference, 1);
1648 pipe_resource_reference(&surface->base.texture, texture);
1649 surface->base.context = pipe;
1650 surface->base.format = templ->format;
1651 surface->base.width = width;
1652 surface->base.height = height;
1653 surface->base.u = templ->u;
1654 surface->level_info = &rtex->surface.level[templ->u.tex.level];
1655 return &surface->base;
1656 }
1657
1658 static struct pipe_surface *r600_create_surface(struct pipe_context *pipe,
1659 struct pipe_resource *tex,
1660 const struct pipe_surface *templ)
1661 {
1662 unsigned level = templ->u.tex.level;
1663 unsigned width = u_minify(tex->width0, level);
1664 unsigned height = u_minify(tex->height0, level);
1665
1666 if (tex->target != PIPE_BUFFER && templ->format != tex->format) {
1667 const struct util_format_description *tex_desc
1668 = util_format_description(tex->format);
1669 const struct util_format_description *templ_desc
1670 = util_format_description(templ->format);
1671
1672 assert(tex_desc->block.bits == templ_desc->block.bits);
1673
1674 /* Adjust size of surface if and only if the block width or
1675 * height is changed. */
1676 if (tex_desc->block.width != templ_desc->block.width ||
1677 tex_desc->block.height != templ_desc->block.height) {
1678 unsigned nblks_x = util_format_get_nblocksx(tex->format, width);
1679 unsigned nblks_y = util_format_get_nblocksy(tex->format, height);
1680
1681 width = nblks_x * templ_desc->block.width;
1682 height = nblks_y * templ_desc->block.height;
1683 }
1684 }
1685
1686 return r600_create_surface_custom(pipe, tex, templ, width, height);
1687 }
1688
1689 static void r600_surface_destroy(struct pipe_context *pipe,
1690 struct pipe_surface *surface)
1691 {
1692 struct r600_surface *surf = (struct r600_surface*)surface;
1693 r600_resource_reference(&surf->cb_buffer_fmask, NULL);
1694 r600_resource_reference(&surf->cb_buffer_cmask, NULL);
1695 pipe_resource_reference(&surface->texture, NULL);
1696 FREE(surface);
1697 }
1698
1699 unsigned r600_translate_colorswap(enum pipe_format format, bool do_endian_swap)
1700 {
1701 const struct util_format_description *desc = util_format_description(format);
1702
1703 #define HAS_SWIZZLE(chan,swz) (desc->swizzle[chan] == PIPE_SWIZZLE_##swz)
1704
1705 if (format == PIPE_FORMAT_R11G11B10_FLOAT) /* isn't plain */
1706 return V_0280A0_SWAP_STD;
1707
1708 if (desc->layout != UTIL_FORMAT_LAYOUT_PLAIN)
1709 return ~0U;
1710
1711 switch (desc->nr_channels) {
1712 case 1:
1713 if (HAS_SWIZZLE(0,X))
1714 return V_0280A0_SWAP_STD; /* X___ */
1715 else if (HAS_SWIZZLE(3,X))
1716 return V_0280A0_SWAP_ALT_REV; /* ___X */
1717 break;
1718 case 2:
1719 if ((HAS_SWIZZLE(0,X) && HAS_SWIZZLE(1,Y)) ||
1720 (HAS_SWIZZLE(0,X) && HAS_SWIZZLE(1,NONE)) ||
1721 (HAS_SWIZZLE(0,NONE) && HAS_SWIZZLE(1,Y)))
1722 return V_0280A0_SWAP_STD; /* XY__ */
1723 else if ((HAS_SWIZZLE(0,Y) && HAS_SWIZZLE(1,X)) ||
1724 (HAS_SWIZZLE(0,Y) && HAS_SWIZZLE(1,NONE)) ||
1725 (HAS_SWIZZLE(0,NONE) && HAS_SWIZZLE(1,X)))
1726 /* YX__ */
1727 return (do_endian_swap ? V_0280A0_SWAP_STD : V_0280A0_SWAP_STD_REV);
1728 else if (HAS_SWIZZLE(0,X) && HAS_SWIZZLE(3,Y))
1729 return V_0280A0_SWAP_ALT; /* X__Y */
1730 else if (HAS_SWIZZLE(0,Y) && HAS_SWIZZLE(3,X))
1731 return V_0280A0_SWAP_ALT_REV; /* Y__X */
1732 break;
1733 case 3:
1734 if (HAS_SWIZZLE(0,X))
1735 return (do_endian_swap ? V_0280A0_SWAP_STD_REV : V_0280A0_SWAP_STD);
1736 else if (HAS_SWIZZLE(0,Z))
1737 return V_0280A0_SWAP_STD_REV; /* ZYX */
1738 break;
1739 case 4:
1740 /* check the middle channels, the 1st and 4th channel can be NONE */
1741 if (HAS_SWIZZLE(1,Y) && HAS_SWIZZLE(2,Z)) {
1742 return V_0280A0_SWAP_STD; /* XYZW */
1743 } else if (HAS_SWIZZLE(1,Z) && HAS_SWIZZLE(2,Y)) {
1744 return V_0280A0_SWAP_STD_REV; /* WZYX */
1745 } else if (HAS_SWIZZLE(1,Y) && HAS_SWIZZLE(2,X)) {
1746 return V_0280A0_SWAP_ALT; /* ZYXW */
1747 } else if (HAS_SWIZZLE(1,Z) && HAS_SWIZZLE(2,W)) {
1748 /* YZWX */
1749 if (desc->is_array)
1750 return V_0280A0_SWAP_ALT_REV;
1751 else
1752 return (do_endian_swap ? V_0280A0_SWAP_ALT : V_0280A0_SWAP_ALT_REV);
1753 }
1754 break;
1755 }
1756 return ~0U;
1757 }
1758
1759 /* PIPELINE_STAT-BASED DCC ENABLEMENT FOR DISPLAYABLE SURFACES */
1760
1761 static void vi_dcc_clean_up_context_slot(struct r600_common_context *rctx,
1762 int slot)
1763 {
1764 int i;
1765
1766 if (rctx->dcc_stats[slot].query_active)
1767 vi_separate_dcc_stop_query(&rctx->b,
1768 rctx->dcc_stats[slot].tex);
1769
1770 for (i = 0; i < ARRAY_SIZE(rctx->dcc_stats[slot].ps_stats); i++)
1771 if (rctx->dcc_stats[slot].ps_stats[i]) {
1772 rctx->b.destroy_query(&rctx->b,
1773 rctx->dcc_stats[slot].ps_stats[i]);
1774 rctx->dcc_stats[slot].ps_stats[i] = NULL;
1775 }
1776
1777 r600_texture_reference(&rctx->dcc_stats[slot].tex, NULL);
1778 }
1779
1780 /**
1781 * Return the per-context slot where DCC statistics queries for the texture live.
1782 */
1783 static unsigned vi_get_context_dcc_stats_index(struct r600_common_context *rctx,
1784 struct r600_texture *tex)
1785 {
1786 int i, empty_slot = -1;
1787
1788 /* Remove zombie textures (textures kept alive by this array only). */
1789 for (i = 0; i < ARRAY_SIZE(rctx->dcc_stats); i++)
1790 if (rctx->dcc_stats[i].tex &&
1791 rctx->dcc_stats[i].tex->resource.b.b.reference.count == 1)
1792 vi_dcc_clean_up_context_slot(rctx, i);
1793
1794 /* Find the texture. */
1795 for (i = 0; i < ARRAY_SIZE(rctx->dcc_stats); i++) {
1796 /* Return if found. */
1797 if (rctx->dcc_stats[i].tex == tex) {
1798 rctx->dcc_stats[i].last_use_timestamp = os_time_get();
1799 return i;
1800 }
1801
1802 /* Record the first seen empty slot. */
1803 if (empty_slot == -1 && !rctx->dcc_stats[i].tex)
1804 empty_slot = i;
1805 }
1806
1807 /* Not found. Remove the oldest member to make space in the array. */
1808 if (empty_slot == -1) {
1809 int oldest_slot = 0;
1810
1811 /* Find the oldest slot. */
1812 for (i = 1; i < ARRAY_SIZE(rctx->dcc_stats); i++)
1813 if (rctx->dcc_stats[oldest_slot].last_use_timestamp >
1814 rctx->dcc_stats[i].last_use_timestamp)
1815 oldest_slot = i;
1816
1817 /* Clean up the oldest slot. */
1818 vi_dcc_clean_up_context_slot(rctx, oldest_slot);
1819 empty_slot = oldest_slot;
1820 }
1821
1822 /* Add the texture to the new slot. */
1823 r600_texture_reference(&rctx->dcc_stats[empty_slot].tex, tex);
1824 rctx->dcc_stats[empty_slot].last_use_timestamp = os_time_get();
1825 return empty_slot;
1826 }
1827
1828 static struct pipe_query *
1829 vi_create_resuming_pipestats_query(struct pipe_context *ctx)
1830 {
1831 struct r600_query_hw *query = (struct r600_query_hw*)
1832 ctx->create_query(ctx, PIPE_QUERY_PIPELINE_STATISTICS, 0);
1833
1834 query->flags |= R600_QUERY_HW_FLAG_BEGIN_RESUMES;
1835 return (struct pipe_query*)query;
1836 }
1837
1838 /**
1839 * Called when binding a color buffer.
1840 */
1841 void vi_separate_dcc_start_query(struct pipe_context *ctx,
1842 struct r600_texture *tex)
1843 {
1844 struct r600_common_context *rctx = (struct r600_common_context*)ctx;
1845 unsigned i = vi_get_context_dcc_stats_index(rctx, tex);
1846
1847 assert(!rctx->dcc_stats[i].query_active);
1848
1849 if (!rctx->dcc_stats[i].ps_stats[0])
1850 rctx->dcc_stats[i].ps_stats[0] = vi_create_resuming_pipestats_query(ctx);
1851
1852 /* begin or resume the query */
1853 ctx->begin_query(ctx, rctx->dcc_stats[i].ps_stats[0]);
1854 rctx->dcc_stats[i].query_active = true;
1855 }
1856
1857 /**
1858 * Called when unbinding a color buffer.
1859 */
1860 void vi_separate_dcc_stop_query(struct pipe_context *ctx,
1861 struct r600_texture *tex)
1862 {
1863 struct r600_common_context *rctx = (struct r600_common_context*)ctx;
1864 unsigned i = vi_get_context_dcc_stats_index(rctx, tex);
1865
1866 assert(rctx->dcc_stats[i].query_active);
1867 assert(rctx->dcc_stats[i].ps_stats[0]);
1868
1869 /* pause or end the query */
1870 ctx->end_query(ctx, rctx->dcc_stats[i].ps_stats[0]);
1871 rctx->dcc_stats[i].query_active = false;
1872 }
1873
1874 static bool vi_should_enable_separate_dcc(struct r600_texture *tex)
1875 {
1876 /* The minimum number of fullscreen draws per frame that is required
1877 * to enable DCC. */
1878 return tex->ps_draw_ratio + tex->num_slow_clears >= 5;
1879 }
1880
1881 /* Called by fast clear. */
1882 static void vi_separate_dcc_try_enable(struct r600_common_context *rctx,
1883 struct r600_texture *tex)
1884 {
1885 /* The intent is to use this with shared displayable back buffers,
1886 * but it's not strictly limited only to them.
1887 */
1888 if (!tex->resource.is_shared ||
1889 !(tex->resource.external_usage & PIPE_HANDLE_USAGE_EXPLICIT_FLUSH) ||
1890 tex->resource.b.b.target != PIPE_TEXTURE_2D ||
1891 tex->surface.last_level > 0 ||
1892 !tex->surface.dcc_size)
1893 return;
1894
1895 if (tex->dcc_offset)
1896 return; /* already enabled */
1897
1898 /* Enable the DCC stat gathering. */
1899 if (!tex->dcc_gather_statistics) {
1900 tex->dcc_gather_statistics = true;
1901 vi_separate_dcc_start_query(&rctx->b, tex);
1902 }
1903
1904 if (!vi_should_enable_separate_dcc(tex))
1905 return; /* stats show that DCC decompression is too expensive */
1906
1907 assert(tex->surface.level[0].dcc_enabled);
1908 assert(!tex->dcc_separate_buffer);
1909
1910 r600_texture_discard_cmask(rctx->screen, tex);
1911
1912 /* Get a DCC buffer. */
1913 if (tex->last_dcc_separate_buffer) {
1914 assert(tex->dcc_gather_statistics);
1915 assert(!tex->dcc_separate_buffer);
1916 tex->dcc_separate_buffer = tex->last_dcc_separate_buffer;
1917 tex->last_dcc_separate_buffer = NULL;
1918 } else {
1919 tex->dcc_separate_buffer = (struct r600_resource*)
1920 r600_aligned_buffer_create(rctx->b.screen, 0,
1921 PIPE_USAGE_DEFAULT,
1922 tex->surface.dcc_size,
1923 tex->surface.dcc_alignment);
1924 if (!tex->dcc_separate_buffer)
1925 return;
1926 }
1927
1928 /* dcc_offset is the absolute GPUVM address. */
1929 tex->dcc_offset = tex->dcc_separate_buffer->gpu_address;
1930
1931 /* no need to flag anything since this is called by fast clear that
1932 * flags framebuffer state
1933 */
1934 }
1935
1936 /**
1937 * Called by pipe_context::flush_resource, the place where DCC decompression
1938 * takes place.
1939 */
1940 void vi_separate_dcc_process_and_reset_stats(struct pipe_context *ctx,
1941 struct r600_texture *tex)
1942 {
1943 struct r600_common_context *rctx = (struct r600_common_context*)ctx;
1944 struct pipe_query *tmp;
1945 unsigned i = vi_get_context_dcc_stats_index(rctx, tex);
1946 bool query_active = rctx->dcc_stats[i].query_active;
1947 bool disable = false;
1948
1949 if (rctx->dcc_stats[i].ps_stats[2]) {
1950 union pipe_query_result result;
1951
1952 /* Read the results. */
1953 ctx->get_query_result(ctx, rctx->dcc_stats[i].ps_stats[2],
1954 true, &result);
1955 r600_query_hw_reset_buffers(rctx,
1956 (struct r600_query_hw*)
1957 rctx->dcc_stats[i].ps_stats[2]);
1958
1959 /* Compute the approximate number of fullscreen draws. */
1960 tex->ps_draw_ratio =
1961 result.pipeline_statistics.ps_invocations /
1962 (tex->resource.b.b.width0 * tex->resource.b.b.height0);
1963 rctx->last_tex_ps_draw_ratio = tex->ps_draw_ratio;
1964
1965 disable = tex->dcc_separate_buffer &&
1966 !vi_should_enable_separate_dcc(tex);
1967 }
1968
1969 tex->num_slow_clears = 0;
1970
1971 /* stop the statistics query for ps_stats[0] */
1972 if (query_active)
1973 vi_separate_dcc_stop_query(ctx, tex);
1974
1975 /* Move the queries in the queue by one. */
1976 tmp = rctx->dcc_stats[i].ps_stats[2];
1977 rctx->dcc_stats[i].ps_stats[2] = rctx->dcc_stats[i].ps_stats[1];
1978 rctx->dcc_stats[i].ps_stats[1] = rctx->dcc_stats[i].ps_stats[0];
1979 rctx->dcc_stats[i].ps_stats[0] = tmp;
1980
1981 /* create and start a new query as ps_stats[0] */
1982 if (query_active)
1983 vi_separate_dcc_start_query(ctx, tex);
1984
1985 if (disable) {
1986 assert(!tex->last_dcc_separate_buffer);
1987 tex->last_dcc_separate_buffer = tex->dcc_separate_buffer;
1988 tex->dcc_separate_buffer = NULL;
1989 tex->dcc_offset = 0;
1990 /* no need to flag anything since this is called after
1991 * decompression that re-sets framebuffer state
1992 */
1993 }
1994 }
1995
1996 /* FAST COLOR CLEAR */
1997
1998 static void evergreen_set_clear_color(struct r600_texture *rtex,
1999 enum pipe_format surface_format,
2000 const union pipe_color_union *color)
2001 {
2002 union util_color uc;
2003
2004 memset(&uc, 0, sizeof(uc));
2005
2006 if (util_format_is_pure_uint(surface_format)) {
2007 util_format_write_4ui(surface_format, color->ui, 0, &uc, 0, 0, 0, 1, 1);
2008 } else if (util_format_is_pure_sint(surface_format)) {
2009 util_format_write_4i(surface_format, color->i, 0, &uc, 0, 0, 0, 1, 1);
2010 } else {
2011 util_pack_color(color->f, surface_format, &uc);
2012 }
2013
2014 memcpy(rtex->color_clear_value, &uc, 2 * sizeof(uint32_t));
2015 }
2016
2017 static void vi_get_fast_clear_parameters(enum pipe_format surface_format,
2018 const union pipe_color_union *color,
2019 uint32_t* reset_value,
2020 bool* clear_words_needed)
2021 {
2022 bool values[4] = {};
2023 int i;
2024 bool main_value = false;
2025 bool extra_value = false;
2026 int extra_channel;
2027 const struct util_format_description *desc = util_format_description(surface_format);
2028
2029 *clear_words_needed = true;
2030 *reset_value = 0x20202020U;
2031
2032 /* If we want to clear without needing a fast clear eliminate step, we
2033 * can set each channel to 0 or 1 (or 0/max for integer formats). We
2034 * have two sets of flags, one for the last or first channel(extra) and
2035 * one for the other channels(main).
2036 */
2037
2038 if (surface_format == PIPE_FORMAT_R11G11B10_FLOAT ||
2039 surface_format == PIPE_FORMAT_B5G6R5_UNORM ||
2040 surface_format == PIPE_FORMAT_B5G6R5_SRGB) {
2041 extra_channel = -1;
2042 } else if (desc->layout == UTIL_FORMAT_LAYOUT_PLAIN) {
2043 if(r600_translate_colorswap(surface_format, false) <= 1)
2044 extra_channel = desc->nr_channels - 1;
2045 else
2046 extra_channel = 0;
2047 } else
2048 return;
2049
2050 for (i = 0; i < 4; ++i) {
2051 int index = desc->swizzle[i] - PIPE_SWIZZLE_X;
2052
2053 if (desc->swizzle[i] < PIPE_SWIZZLE_X ||
2054 desc->swizzle[i] > PIPE_SWIZZLE_W)
2055 continue;
2056
2057 if (util_format_is_pure_sint(surface_format)) {
2058 values[i] = color->i[i] != 0;
2059 if (color->i[i] != 0 && color->i[i] != INT32_MAX)
2060 return;
2061 } else if (util_format_is_pure_uint(surface_format)) {
2062 values[i] = color->ui[i] != 0U;
2063 if (color->ui[i] != 0U && color->ui[i] != UINT32_MAX)
2064 return;
2065 } else {
2066 values[i] = color->f[i] != 0.0F;
2067 if (color->f[i] != 0.0F && color->f[i] != 1.0F)
2068 return;
2069 }
2070
2071 if (index == extra_channel)
2072 extra_value = values[i];
2073 else
2074 main_value = values[i];
2075 }
2076
2077 for (int i = 0; i < 4; ++i)
2078 if (values[i] != main_value &&
2079 desc->swizzle[i] - PIPE_SWIZZLE_X != extra_channel &&
2080 desc->swizzle[i] >= PIPE_SWIZZLE_X &&
2081 desc->swizzle[i] <= PIPE_SWIZZLE_W)
2082 return;
2083
2084 *clear_words_needed = false;
2085 if (main_value)
2086 *reset_value |= 0x80808080U;
2087
2088 if (extra_value)
2089 *reset_value |= 0x40404040U;
2090 }
2091
2092 void vi_dcc_clear_level(struct r600_common_context *rctx,
2093 struct r600_texture *rtex,
2094 unsigned level, unsigned clear_value)
2095 {
2096 struct pipe_resource *dcc_buffer;
2097 uint64_t dcc_offset;
2098
2099 assert(rtex->dcc_offset && rtex->surface.level[level].dcc_enabled);
2100
2101 if (rtex->dcc_separate_buffer) {
2102 dcc_buffer = &rtex->dcc_separate_buffer->b.b;
2103 dcc_offset = 0;
2104 } else {
2105 dcc_buffer = &rtex->resource.b.b;
2106 dcc_offset = rtex->dcc_offset;
2107 }
2108
2109 dcc_offset += rtex->surface.level[level].dcc_offset;
2110
2111 rctx->clear_buffer(&rctx->b, dcc_buffer, dcc_offset,
2112 rtex->surface.level[level].dcc_fast_clear_size,
2113 clear_value, R600_COHERENCY_CB_META);
2114 }
2115
2116 /* Set the same micro tile mode as the destination of the last MSAA resolve.
2117 * This allows hitting the MSAA resolve fast path, which requires that both
2118 * src and dst micro tile modes match.
2119 */
2120 static void si_set_optimal_micro_tile_mode(struct r600_common_screen *rscreen,
2121 struct r600_texture *rtex)
2122 {
2123 if (rtex->resource.is_shared ||
2124 rtex->surface.nsamples <= 1 ||
2125 rtex->surface.micro_tile_mode == rtex->last_msaa_resolve_target_micro_mode)
2126 return;
2127
2128 assert(rtex->surface.level[0].mode == RADEON_SURF_MODE_2D);
2129 assert(rtex->surface.last_level == 0);
2130
2131 /* These magic numbers were copied from addrlib. It doesn't use any
2132 * definitions for them either. They are all 2D_TILED_THIN1 modes with
2133 * different bpp and micro tile mode.
2134 */
2135 if (rscreen->chip_class >= CIK) {
2136 switch (rtex->last_msaa_resolve_target_micro_mode) {
2137 case 0: /* displayable */
2138 rtex->surface.tiling_index[0] = 10;
2139 break;
2140 case 1: /* thin */
2141 rtex->surface.tiling_index[0] = 14;
2142 break;
2143 case 3: /* rotated */
2144 rtex->surface.tiling_index[0] = 28;
2145 break;
2146 default: /* depth, thick */
2147 assert(!"unexpected micro mode");
2148 return;
2149 }
2150 } else { /* SI */
2151 switch (rtex->last_msaa_resolve_target_micro_mode) {
2152 case 0: /* displayable */
2153 switch (rtex->surface.bpe) {
2154 case 8:
2155 rtex->surface.tiling_index[0] = 10;
2156 break;
2157 case 16:
2158 rtex->surface.tiling_index[0] = 11;
2159 break;
2160 default: /* 32, 64 */
2161 rtex->surface.tiling_index[0] = 12;
2162 break;
2163 }
2164 break;
2165 case 1: /* thin */
2166 switch (rtex->surface.bpe) {
2167 case 8:
2168 rtex->surface.tiling_index[0] = 14;
2169 break;
2170 case 16:
2171 rtex->surface.tiling_index[0] = 15;
2172 break;
2173 case 32:
2174 rtex->surface.tiling_index[0] = 16;
2175 break;
2176 default: /* 64, 128 */
2177 rtex->surface.tiling_index[0] = 17;
2178 break;
2179 }
2180 break;
2181 default: /* depth, thick */
2182 assert(!"unexpected micro mode");
2183 return;
2184 }
2185 }
2186
2187 rtex->surface.micro_tile_mode = rtex->last_msaa_resolve_target_micro_mode;
2188
2189 p_atomic_inc(&rscreen->dirty_fb_counter);
2190 p_atomic_inc(&rscreen->dirty_tex_descriptor_counter);
2191 }
2192
2193 void evergreen_do_fast_color_clear(struct r600_common_context *rctx,
2194 struct pipe_framebuffer_state *fb,
2195 struct r600_atom *fb_state,
2196 unsigned *buffers, unsigned *dirty_cbufs,
2197 const union pipe_color_union *color)
2198 {
2199 int i;
2200
2201 /* This function is broken in BE, so just disable this path for now */
2202 #ifdef PIPE_ARCH_BIG_ENDIAN
2203 return;
2204 #endif
2205
2206 if (rctx->render_cond)
2207 return;
2208
2209 for (i = 0; i < fb->nr_cbufs; i++) {
2210 struct r600_texture *tex;
2211 unsigned clear_bit = PIPE_CLEAR_COLOR0 << i;
2212
2213 if (!fb->cbufs[i])
2214 continue;
2215
2216 /* if this colorbuffer is not being cleared */
2217 if (!(*buffers & clear_bit))
2218 continue;
2219
2220 tex = (struct r600_texture *)fb->cbufs[i]->texture;
2221
2222 /* 128-bit formats are unusupported */
2223 if (util_format_get_blocksizebits(fb->cbufs[i]->format) > 64) {
2224 continue;
2225 }
2226
2227 /* the clear is allowed if all layers are bound */
2228 if (fb->cbufs[i]->u.tex.first_layer != 0 ||
2229 fb->cbufs[i]->u.tex.last_layer != util_max_layer(&tex->resource.b.b, 0)) {
2230 continue;
2231 }
2232
2233 /* cannot clear mipmapped textures */
2234 if (fb->cbufs[i]->texture->last_level != 0) {
2235 continue;
2236 }
2237
2238 /* only supported on tiled surfaces */
2239 if (tex->surface.level[0].mode < RADEON_SURF_MODE_1D) {
2240 continue;
2241 }
2242
2243 /* shared textures can't use fast clear without an explicit flush,
2244 * because there is no way to communicate the clear color among
2245 * all clients
2246 */
2247 if (tex->resource.is_shared &&
2248 !(tex->resource.external_usage & PIPE_HANDLE_USAGE_EXPLICIT_FLUSH))
2249 continue;
2250
2251 /* fast color clear with 1D tiling doesn't work on old kernels and CIK */
2252 if (tex->surface.level[0].mode == RADEON_SURF_MODE_1D &&
2253 rctx->chip_class >= CIK &&
2254 rctx->screen->info.drm_major == 2 &&
2255 rctx->screen->info.drm_minor < 38) {
2256 continue;
2257 }
2258
2259 /* Fast clear is the most appropriate place to enable DCC for
2260 * displayable surfaces.
2261 */
2262 if (rctx->chip_class >= VI &&
2263 !(rctx->screen->debug_flags & DBG_NO_DCC_FB)) {
2264 vi_separate_dcc_try_enable(rctx, tex);
2265
2266 /* Stoney can't do a CMASK-based clear, so all clears are
2267 * considered to be hypothetically slow clears, which
2268 * is weighed when determining to enable separate DCC.
2269 */
2270 if (tex->dcc_gather_statistics &&
2271 rctx->family == CHIP_STONEY)
2272 tex->num_slow_clears++;
2273 }
2274
2275 /* Try to clear DCC first, otherwise try CMASK. */
2276 if (tex->dcc_offset && tex->surface.level[0].dcc_enabled) {
2277 uint32_t reset_value;
2278 bool clear_words_needed;
2279
2280 if (rctx->screen->debug_flags & DBG_NO_DCC_CLEAR)
2281 continue;
2282
2283 /* We can change the micro tile mode before a full clear. */
2284 if (rctx->screen->chip_class >= SI)
2285 si_set_optimal_micro_tile_mode(rctx->screen, tex);
2286
2287 vi_get_fast_clear_parameters(fb->cbufs[i]->format, color, &reset_value, &clear_words_needed);
2288 vi_dcc_clear_level(rctx, tex, 0, reset_value);
2289
2290 if (clear_words_needed)
2291 tex->dirty_level_mask |= 1 << fb->cbufs[i]->u.tex.level;
2292 tex->separate_dcc_dirty = true;
2293 } else {
2294 /* Stoney/RB+ doesn't work with CMASK fast clear. */
2295 if (rctx->family == CHIP_STONEY)
2296 continue;
2297
2298 /* ensure CMASK is enabled */
2299 r600_texture_alloc_cmask_separate(rctx->screen, tex);
2300 if (tex->cmask.size == 0) {
2301 continue;
2302 }
2303
2304 /* We can change the micro tile mode before a full clear. */
2305 if (rctx->screen->chip_class >= SI)
2306 si_set_optimal_micro_tile_mode(rctx->screen, tex);
2307
2308 /* Do the fast clear. */
2309 rctx->clear_buffer(&rctx->b, &tex->cmask_buffer->b.b,
2310 tex->cmask.offset, tex->cmask.size, 0,
2311 R600_COHERENCY_CB_META);
2312
2313 tex->dirty_level_mask |= 1 << fb->cbufs[i]->u.tex.level;
2314 }
2315
2316 evergreen_set_clear_color(tex, fb->cbufs[i]->format, color);
2317
2318 if (dirty_cbufs)
2319 *dirty_cbufs |= 1 << i;
2320 rctx->set_atom_dirty(rctx, fb_state, true);
2321 *buffers &= ~clear_bit;
2322 }
2323 }
2324
2325 void r600_init_screen_texture_functions(struct r600_common_screen *rscreen)
2326 {
2327 rscreen->b.resource_from_handle = r600_texture_from_handle;
2328 rscreen->b.resource_get_handle = r600_texture_get_handle;
2329 }
2330
2331 void r600_init_context_texture_functions(struct r600_common_context *rctx)
2332 {
2333 rctx->b.create_surface = r600_create_surface;
2334 rctx->b.surface_destroy = r600_surface_destroy;
2335 }