gallium: add pipe_screen::check_resource_capability
[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_log.h"
32 #include "util/u_memory.h"
33 #include "util/u_pack_color.h"
34 #include "util/u_surface.h"
35 #include "os/os_time.h"
36 #include <errno.h>
37 #include <inttypes.h>
38 #include "state_tracker/drm_driver.h"
39 #include "amd/common/sid.h"
40
41 static void r600_texture_discard_cmask(struct r600_common_screen *rscreen,
42 struct r600_texture *rtex);
43 static enum radeon_surf_mode
44 r600_choose_tiling(struct r600_common_screen *rscreen,
45 const struct pipe_resource *templ);
46
47
48 bool si_prepare_for_dma_blit(struct r600_common_context *rctx,
49 struct r600_texture *rdst,
50 unsigned dst_level, unsigned dstx,
51 unsigned dsty, unsigned dstz,
52 struct r600_texture *rsrc,
53 unsigned src_level,
54 const struct pipe_box *src_box)
55 {
56 if (!rctx->dma.cs)
57 return false;
58
59 if (rdst->surface.bpe != rsrc->surface.bpe)
60 return false;
61
62 /* MSAA: Blits don't exist in the real world. */
63 if (rsrc->resource.b.b.nr_samples > 1 ||
64 rdst->resource.b.b.nr_samples > 1)
65 return false;
66
67 /* Depth-stencil surfaces:
68 * When dst is linear, the DB->CB copy preserves HTILE.
69 * When dst is tiled, the 3D path must be used to update HTILE.
70 */
71 if (rsrc->is_depth || rdst->is_depth)
72 return false;
73
74 /* DCC as:
75 * src: Use the 3D path. DCC decompression is expensive.
76 * dst: Use the 3D path to compress the pixels with DCC.
77 */
78 if (vi_dcc_enabled(rsrc, src_level) ||
79 vi_dcc_enabled(rdst, dst_level))
80 return false;
81
82 /* CMASK as:
83 * src: Both texture and SDMA paths need decompression. Use SDMA.
84 * dst: If overwriting the whole texture, discard CMASK and use
85 * SDMA. Otherwise, use the 3D path.
86 */
87 if (rdst->cmask.size && rdst->dirty_level_mask & (1 << dst_level)) {
88 /* The CMASK clear is only enabled for the first level. */
89 assert(dst_level == 0);
90 if (!util_texrange_covers_whole_level(&rdst->resource.b.b, dst_level,
91 dstx, dsty, dstz, src_box->width,
92 src_box->height, src_box->depth))
93 return false;
94
95 r600_texture_discard_cmask(rctx->screen, rdst);
96 }
97
98 /* All requirements are met. Prepare textures for SDMA. */
99 if (rsrc->cmask.size && rsrc->dirty_level_mask & (1 << src_level))
100 rctx->b.flush_resource(&rctx->b, &rsrc->resource.b.b);
101
102 assert(!(rsrc->dirty_level_mask & (1 << src_level)));
103 assert(!(rdst->dirty_level_mask & (1 << dst_level)));
104
105 return true;
106 }
107
108 /* Same as resource_copy_region, except that both upsampling and downsampling are allowed. */
109 static void r600_copy_region_with_blit(struct pipe_context *pipe,
110 struct pipe_resource *dst,
111 unsigned dst_level,
112 unsigned dstx, unsigned dsty, unsigned dstz,
113 struct pipe_resource *src,
114 unsigned src_level,
115 const struct pipe_box *src_box)
116 {
117 struct pipe_blit_info blit;
118
119 memset(&blit, 0, sizeof(blit));
120 blit.src.resource = src;
121 blit.src.format = src->format;
122 blit.src.level = src_level;
123 blit.src.box = *src_box;
124 blit.dst.resource = dst;
125 blit.dst.format = dst->format;
126 blit.dst.level = dst_level;
127 blit.dst.box.x = dstx;
128 blit.dst.box.y = dsty;
129 blit.dst.box.z = dstz;
130 blit.dst.box.width = src_box->width;
131 blit.dst.box.height = src_box->height;
132 blit.dst.box.depth = src_box->depth;
133 blit.mask = util_format_get_mask(src->format) &
134 util_format_get_mask(dst->format);
135 blit.filter = PIPE_TEX_FILTER_NEAREST;
136
137 if (blit.mask) {
138 pipe->blit(pipe, &blit);
139 }
140 }
141
142 /* Copy from a full GPU texture to a transfer's staging one. */
143 static void r600_copy_to_staging_texture(struct pipe_context *ctx, struct r600_transfer *rtransfer)
144 {
145 struct r600_common_context *rctx = (struct r600_common_context*)ctx;
146 struct pipe_transfer *transfer = (struct pipe_transfer*)rtransfer;
147 struct pipe_resource *dst = &rtransfer->staging->b.b;
148 struct pipe_resource *src = transfer->resource;
149
150 if (src->nr_samples > 1) {
151 r600_copy_region_with_blit(ctx, dst, 0, 0, 0, 0,
152 src, transfer->level, &transfer->box);
153 return;
154 }
155
156 rctx->dma_copy(ctx, dst, 0, 0, 0, 0, src, transfer->level,
157 &transfer->box);
158 }
159
160 /* Copy from a transfer's staging texture to a full GPU one. */
161 static void r600_copy_from_staging_texture(struct pipe_context *ctx, struct r600_transfer *rtransfer)
162 {
163 struct r600_common_context *rctx = (struct r600_common_context*)ctx;
164 struct pipe_transfer *transfer = (struct pipe_transfer*)rtransfer;
165 struct pipe_resource *dst = transfer->resource;
166 struct pipe_resource *src = &rtransfer->staging->b.b;
167 struct pipe_box sbox;
168
169 u_box_3d(0, 0, 0, transfer->box.width, transfer->box.height, transfer->box.depth, &sbox);
170
171 if (dst->nr_samples > 1) {
172 r600_copy_region_with_blit(ctx, dst, transfer->level,
173 transfer->box.x, transfer->box.y, transfer->box.z,
174 src, 0, &sbox);
175 return;
176 }
177
178 rctx->dma_copy(ctx, dst, transfer->level,
179 transfer->box.x, transfer->box.y, transfer->box.z,
180 src, 0, &sbox);
181 }
182
183 static unsigned r600_texture_get_offset(struct r600_common_screen *rscreen,
184 struct r600_texture *rtex, unsigned level,
185 const struct pipe_box *box,
186 unsigned *stride,
187 unsigned *layer_stride)
188 {
189 if (rscreen->chip_class >= GFX9) {
190 *stride = rtex->surface.u.gfx9.surf_pitch * rtex->surface.bpe;
191 *layer_stride = rtex->surface.u.gfx9.surf_slice_size;
192
193 if (!box)
194 return 0;
195
196 /* Each texture is an array of slices. Each slice is an array
197 * of mipmap levels. */
198 return box->z * rtex->surface.u.gfx9.surf_slice_size +
199 rtex->surface.u.gfx9.offset[level] +
200 (box->y / rtex->surface.blk_h *
201 rtex->surface.u.gfx9.surf_pitch +
202 box->x / rtex->surface.blk_w) * rtex->surface.bpe;
203 } else {
204 *stride = rtex->surface.u.legacy.level[level].nblk_x *
205 rtex->surface.bpe;
206 *layer_stride = rtex->surface.u.legacy.level[level].slice_size;
207
208 if (!box)
209 return rtex->surface.u.legacy.level[level].offset;
210
211 /* Each texture is an array of mipmap levels. Each level is
212 * an array of slices. */
213 return rtex->surface.u.legacy.level[level].offset +
214 box->z * rtex->surface.u.legacy.level[level].slice_size +
215 (box->y / rtex->surface.blk_h *
216 rtex->surface.u.legacy.level[level].nblk_x +
217 box->x / rtex->surface.blk_w) * rtex->surface.bpe;
218 }
219 }
220
221 static int r600_init_surface(struct r600_common_screen *rscreen,
222 struct radeon_surf *surface,
223 const struct pipe_resource *ptex,
224 enum radeon_surf_mode array_mode,
225 unsigned pitch_in_bytes_override,
226 unsigned offset,
227 bool is_imported,
228 bool is_scanout,
229 bool is_flushed_depth,
230 bool tc_compatible_htile)
231 {
232 const struct util_format_description *desc =
233 util_format_description(ptex->format);
234 bool is_depth, is_stencil;
235 int r;
236 unsigned i, bpe, flags = 0;
237
238 is_depth = util_format_has_depth(desc);
239 is_stencil = util_format_has_stencil(desc);
240
241 if (!is_flushed_depth &&
242 ptex->format == PIPE_FORMAT_Z32_FLOAT_S8X24_UINT) {
243 bpe = 4; /* stencil is allocated separately on evergreen */
244 } else {
245 bpe = util_format_get_blocksize(ptex->format);
246 assert(util_is_power_of_two(bpe));
247 }
248
249 if (!is_flushed_depth && is_depth) {
250 flags |= RADEON_SURF_ZBUFFER;
251
252 if (tc_compatible_htile &&
253 (rscreen->chip_class >= GFX9 ||
254 array_mode == RADEON_SURF_MODE_2D)) {
255 /* TC-compatible HTILE only supports Z32_FLOAT.
256 * GFX9 also supports Z16_UNORM.
257 * On VI, promote Z16 to Z32. DB->CB copies will convert
258 * the format for transfers.
259 */
260 if (rscreen->chip_class == VI)
261 bpe = 4;
262
263 flags |= RADEON_SURF_TC_COMPATIBLE_HTILE;
264 }
265
266 if (is_stencil)
267 flags |= RADEON_SURF_SBUFFER;
268 }
269
270 if (rscreen->chip_class >= VI &&
271 (ptex->flags & R600_RESOURCE_FLAG_DISABLE_DCC ||
272 ptex->format == PIPE_FORMAT_R9G9B9E5_FLOAT))
273 flags |= RADEON_SURF_DISABLE_DCC;
274
275 if (ptex->bind & PIPE_BIND_SCANOUT || is_scanout) {
276 /* This should catch bugs in gallium users setting incorrect flags. */
277 assert(ptex->nr_samples <= 1 &&
278 ptex->array_size == 1 &&
279 ptex->depth0 == 1 &&
280 ptex->last_level == 0 &&
281 !(flags & RADEON_SURF_Z_OR_SBUFFER));
282
283 flags |= RADEON_SURF_SCANOUT;
284 }
285
286 if (ptex->bind & PIPE_BIND_SHARED)
287 flags |= RADEON_SURF_SHAREABLE;
288 if (is_imported)
289 flags |= RADEON_SURF_IMPORTED | RADEON_SURF_SHAREABLE;
290 if (!(ptex->flags & R600_RESOURCE_FLAG_FORCE_TILING))
291 flags |= RADEON_SURF_OPTIMIZE_FOR_SPACE;
292
293 r = rscreen->ws->surface_init(rscreen->ws, ptex, flags, bpe,
294 array_mode, surface);
295 if (r) {
296 return r;
297 }
298
299 if (rscreen->chip_class >= GFX9) {
300 assert(!pitch_in_bytes_override ||
301 pitch_in_bytes_override == surface->u.gfx9.surf_pitch * bpe);
302 surface->u.gfx9.surf_offset = offset;
303 } else {
304 if (pitch_in_bytes_override &&
305 pitch_in_bytes_override != surface->u.legacy.level[0].nblk_x * bpe) {
306 /* old ddx on evergreen over estimate alignment for 1d, only 1 level
307 * for those
308 */
309 surface->u.legacy.level[0].nblk_x = pitch_in_bytes_override / bpe;
310 surface->u.legacy.level[0].slice_size = pitch_in_bytes_override *
311 surface->u.legacy.level[0].nblk_y;
312 }
313
314 if (offset) {
315 for (i = 0; i < ARRAY_SIZE(surface->u.legacy.level); ++i)
316 surface->u.legacy.level[i].offset += offset;
317 }
318 }
319 return 0;
320 }
321
322 static void r600_texture_init_metadata(struct r600_common_screen *rscreen,
323 struct r600_texture *rtex,
324 struct radeon_bo_metadata *metadata)
325 {
326 struct radeon_surf *surface = &rtex->surface;
327
328 memset(metadata, 0, sizeof(*metadata));
329
330 if (rscreen->chip_class >= GFX9) {
331 metadata->u.gfx9.swizzle_mode = surface->u.gfx9.surf.swizzle_mode;
332 } else {
333 metadata->u.legacy.microtile = surface->u.legacy.level[0].mode >= RADEON_SURF_MODE_1D ?
334 RADEON_LAYOUT_TILED : RADEON_LAYOUT_LINEAR;
335 metadata->u.legacy.macrotile = surface->u.legacy.level[0].mode >= RADEON_SURF_MODE_2D ?
336 RADEON_LAYOUT_TILED : RADEON_LAYOUT_LINEAR;
337 metadata->u.legacy.pipe_config = surface->u.legacy.pipe_config;
338 metadata->u.legacy.bankw = surface->u.legacy.bankw;
339 metadata->u.legacy.bankh = surface->u.legacy.bankh;
340 metadata->u.legacy.tile_split = surface->u.legacy.tile_split;
341 metadata->u.legacy.mtilea = surface->u.legacy.mtilea;
342 metadata->u.legacy.num_banks = surface->u.legacy.num_banks;
343 metadata->u.legacy.stride = surface->u.legacy.level[0].nblk_x * surface->bpe;
344 metadata->u.legacy.scanout = (surface->flags & RADEON_SURF_SCANOUT) != 0;
345 }
346 }
347
348 static void r600_surface_import_metadata(struct r600_common_screen *rscreen,
349 struct radeon_surf *surf,
350 struct radeon_bo_metadata *metadata,
351 enum radeon_surf_mode *array_mode,
352 bool *is_scanout)
353 {
354 if (rscreen->chip_class >= GFX9) {
355 if (metadata->u.gfx9.swizzle_mode > 0)
356 *array_mode = RADEON_SURF_MODE_2D;
357 else
358 *array_mode = RADEON_SURF_MODE_LINEAR_ALIGNED;
359
360 *is_scanout = metadata->u.gfx9.swizzle_mode == 0 ||
361 metadata->u.gfx9.swizzle_mode % 4 == 2;
362
363 surf->u.gfx9.surf.swizzle_mode = metadata->u.gfx9.swizzle_mode;
364 } else {
365 surf->u.legacy.pipe_config = metadata->u.legacy.pipe_config;
366 surf->u.legacy.bankw = metadata->u.legacy.bankw;
367 surf->u.legacy.bankh = metadata->u.legacy.bankh;
368 surf->u.legacy.tile_split = metadata->u.legacy.tile_split;
369 surf->u.legacy.mtilea = metadata->u.legacy.mtilea;
370 surf->u.legacy.num_banks = metadata->u.legacy.num_banks;
371
372 if (metadata->u.legacy.macrotile == RADEON_LAYOUT_TILED)
373 *array_mode = RADEON_SURF_MODE_2D;
374 else if (metadata->u.legacy.microtile == RADEON_LAYOUT_TILED)
375 *array_mode = RADEON_SURF_MODE_1D;
376 else
377 *array_mode = RADEON_SURF_MODE_LINEAR_ALIGNED;
378
379 *is_scanout = metadata->u.legacy.scanout;
380 }
381 }
382
383 static void r600_eliminate_fast_color_clear(struct r600_common_context *rctx,
384 struct r600_texture *rtex)
385 {
386 struct r600_common_screen *rscreen = rctx->screen;
387 struct pipe_context *ctx = &rctx->b;
388
389 if (ctx == rscreen->aux_context)
390 mtx_lock(&rscreen->aux_context_lock);
391
392 ctx->flush_resource(ctx, &rtex->resource.b.b);
393 ctx->flush(ctx, NULL, 0);
394
395 if (ctx == rscreen->aux_context)
396 mtx_unlock(&rscreen->aux_context_lock);
397 }
398
399 static void r600_texture_discard_cmask(struct r600_common_screen *rscreen,
400 struct r600_texture *rtex)
401 {
402 if (!rtex->cmask.size)
403 return;
404
405 assert(rtex->resource.b.b.nr_samples <= 1);
406
407 /* Disable CMASK. */
408 memset(&rtex->cmask, 0, sizeof(rtex->cmask));
409 rtex->cmask.base_address_reg = rtex->resource.gpu_address >> 8;
410 rtex->dirty_level_mask = 0;
411
412 rtex->cb_color_info &= ~S_028C70_FAST_CLEAR(1);
413
414 if (rtex->cmask_buffer != &rtex->resource)
415 r600_resource_reference(&rtex->cmask_buffer, NULL);
416
417 /* Notify all contexts about the change. */
418 p_atomic_inc(&rscreen->dirty_tex_counter);
419 p_atomic_inc(&rscreen->compressed_colortex_counter);
420 }
421
422 static bool r600_can_disable_dcc(struct r600_texture *rtex)
423 {
424 /* We can't disable DCC if it can be written by another process. */
425 return rtex->dcc_offset &&
426 (!rtex->resource.b.is_shared ||
427 !(rtex->resource.external_usage & PIPE_HANDLE_USAGE_WRITE));
428 }
429
430 static bool r600_texture_discard_dcc(struct r600_common_screen *rscreen,
431 struct r600_texture *rtex)
432 {
433 if (!r600_can_disable_dcc(rtex))
434 return false;
435
436 assert(rtex->dcc_separate_buffer == NULL);
437
438 /* Disable DCC. */
439 rtex->dcc_offset = 0;
440
441 /* Notify all contexts about the change. */
442 p_atomic_inc(&rscreen->dirty_tex_counter);
443 return true;
444 }
445
446 /**
447 * Disable DCC for the texture. (first decompress, then discard metadata).
448 *
449 * There is unresolved multi-context synchronization issue between
450 * screen::aux_context and the current context. If applications do this with
451 * multiple contexts, it's already undefined behavior for them and we don't
452 * have to worry about that. The scenario is:
453 *
454 * If context 1 disables DCC and context 2 has queued commands that write
455 * to the texture via CB with DCC enabled, and the order of operations is
456 * as follows:
457 * context 2 queues draw calls rendering to the texture, but doesn't flush
458 * context 1 disables DCC and flushes
459 * context 1 & 2 reset descriptors and FB state
460 * context 2 flushes (new compressed tiles written by the draw calls)
461 * context 1 & 2 read garbage, because DCC is disabled, yet there are
462 * compressed tiled
463 *
464 * \param rctx the current context if you have one, or rscreen->aux_context
465 * if you don't.
466 */
467 bool si_texture_disable_dcc(struct r600_common_context *rctx,
468 struct r600_texture *rtex)
469 {
470 struct r600_common_screen *rscreen = rctx->screen;
471
472 if (!r600_can_disable_dcc(rtex))
473 return false;
474
475 if (&rctx->b == rscreen->aux_context)
476 mtx_lock(&rscreen->aux_context_lock);
477
478 /* Decompress DCC. */
479 rctx->decompress_dcc(&rctx->b, rtex);
480 rctx->b.flush(&rctx->b, NULL, 0);
481
482 if (&rctx->b == rscreen->aux_context)
483 mtx_unlock(&rscreen->aux_context_lock);
484
485 return r600_texture_discard_dcc(rscreen, rtex);
486 }
487
488 static void r600_reallocate_texture_inplace(struct r600_common_context *rctx,
489 struct r600_texture *rtex,
490 unsigned new_bind_flag,
491 bool invalidate_storage)
492 {
493 struct pipe_screen *screen = rctx->b.screen;
494 struct r600_texture *new_tex;
495 struct pipe_resource templ = rtex->resource.b.b;
496 unsigned i;
497
498 templ.bind |= new_bind_flag;
499
500 if (rtex->resource.b.is_shared)
501 return;
502
503 if (new_bind_flag == PIPE_BIND_LINEAR) {
504 if (rtex->surface.is_linear)
505 return;
506
507 /* This fails with MSAA, depth, and compressed textures. */
508 if (r600_choose_tiling(rctx->screen, &templ) !=
509 RADEON_SURF_MODE_LINEAR_ALIGNED)
510 return;
511 }
512
513 new_tex = (struct r600_texture*)screen->resource_create(screen, &templ);
514 if (!new_tex)
515 return;
516
517 /* Copy the pixels to the new texture. */
518 if (!invalidate_storage) {
519 for (i = 0; i <= templ.last_level; i++) {
520 struct pipe_box box;
521
522 u_box_3d(0, 0, 0,
523 u_minify(templ.width0, i), u_minify(templ.height0, i),
524 util_max_layer(&templ, i) + 1, &box);
525
526 rctx->dma_copy(&rctx->b, &new_tex->resource.b.b, i, 0, 0, 0,
527 &rtex->resource.b.b, i, &box);
528 }
529 }
530
531 if (new_bind_flag == PIPE_BIND_LINEAR) {
532 r600_texture_discard_cmask(rctx->screen, rtex);
533 r600_texture_discard_dcc(rctx->screen, rtex);
534 }
535
536 /* Replace the structure fields of rtex. */
537 rtex->resource.b.b.bind = templ.bind;
538 pb_reference(&rtex->resource.buf, new_tex->resource.buf);
539 rtex->resource.gpu_address = new_tex->resource.gpu_address;
540 rtex->resource.vram_usage = new_tex->resource.vram_usage;
541 rtex->resource.gart_usage = new_tex->resource.gart_usage;
542 rtex->resource.bo_size = new_tex->resource.bo_size;
543 rtex->resource.bo_alignment = new_tex->resource.bo_alignment;
544 rtex->resource.domains = new_tex->resource.domains;
545 rtex->resource.flags = new_tex->resource.flags;
546 rtex->size = new_tex->size;
547 rtex->db_render_format = new_tex->db_render_format;
548 rtex->db_compatible = new_tex->db_compatible;
549 rtex->can_sample_z = new_tex->can_sample_z;
550 rtex->can_sample_s = new_tex->can_sample_s;
551 rtex->surface = new_tex->surface;
552 rtex->fmask = new_tex->fmask;
553 rtex->cmask = new_tex->cmask;
554 rtex->cb_color_info = new_tex->cb_color_info;
555 rtex->last_msaa_resolve_target_micro_mode = new_tex->last_msaa_resolve_target_micro_mode;
556 rtex->htile_offset = new_tex->htile_offset;
557 rtex->tc_compatible_htile = new_tex->tc_compatible_htile;
558 rtex->depth_cleared = new_tex->depth_cleared;
559 rtex->stencil_cleared = new_tex->stencil_cleared;
560 rtex->non_disp_tiling = new_tex->non_disp_tiling;
561 rtex->dcc_gather_statistics = new_tex->dcc_gather_statistics;
562 rtex->framebuffers_bound = new_tex->framebuffers_bound;
563
564 if (new_bind_flag == PIPE_BIND_LINEAR) {
565 assert(!rtex->htile_offset);
566 assert(!rtex->cmask.size);
567 assert(!rtex->fmask.size);
568 assert(!rtex->dcc_offset);
569 assert(!rtex->is_depth);
570 }
571
572 r600_texture_reference(&new_tex, NULL);
573
574 p_atomic_inc(&rctx->screen->dirty_tex_counter);
575 }
576
577 static boolean r600_texture_get_handle(struct pipe_screen* screen,
578 struct pipe_context *ctx,
579 struct pipe_resource *resource,
580 struct winsys_handle *whandle,
581 unsigned usage)
582 {
583 struct r600_common_screen *rscreen = (struct r600_common_screen*)screen;
584 struct r600_common_context *rctx;
585 struct r600_resource *res = (struct r600_resource*)resource;
586 struct r600_texture *rtex = (struct r600_texture*)resource;
587 struct radeon_bo_metadata metadata;
588 bool update_metadata = false;
589 unsigned stride, offset, slice_size;
590
591 ctx = threaded_context_unwrap_sync(ctx);
592 rctx = (struct r600_common_context*)(ctx ? ctx : rscreen->aux_context);
593
594 if (resource->target != PIPE_BUFFER) {
595 /* This is not supported now, but it might be required for OpenCL
596 * interop in the future.
597 */
598 if (resource->nr_samples > 1 || rtex->is_depth)
599 return false;
600
601 /* Move a suballocated texture into a non-suballocated allocation. */
602 if (rscreen->ws->buffer_is_suballocated(res->buf) ||
603 rtex->surface.tile_swizzle ||
604 (rtex->resource.flags & RADEON_FLAG_NO_INTERPROCESS_SHARING &&
605 whandle->type != DRM_API_HANDLE_TYPE_KMS)) {
606 assert(!res->b.is_shared);
607 r600_reallocate_texture_inplace(rctx, rtex,
608 PIPE_BIND_SHARED, false);
609 rctx->b.flush(&rctx->b, NULL, 0);
610 assert(res->b.b.bind & PIPE_BIND_SHARED);
611 assert(res->flags & RADEON_FLAG_NO_SUBALLOC);
612 assert(!(res->flags & RADEON_FLAG_NO_INTERPROCESS_SHARING));
613 assert(rtex->surface.tile_swizzle == 0);
614 }
615
616 /* Since shader image stores don't support DCC on VI,
617 * disable it for external clients that want write
618 * access.
619 */
620 if (usage & PIPE_HANDLE_USAGE_WRITE && rtex->dcc_offset) {
621 if (si_texture_disable_dcc(rctx, rtex))
622 update_metadata = true;
623 }
624
625 if (!(usage & PIPE_HANDLE_USAGE_EXPLICIT_FLUSH) &&
626 (rtex->cmask.size || rtex->dcc_offset)) {
627 /* Eliminate fast clear (both CMASK and DCC) */
628 r600_eliminate_fast_color_clear(rctx, rtex);
629
630 /* Disable CMASK if flush_resource isn't going
631 * to be called.
632 */
633 if (rtex->cmask.size)
634 r600_texture_discard_cmask(rscreen, rtex);
635 }
636
637 /* Set metadata. */
638 if (!res->b.is_shared || update_metadata) {
639 r600_texture_init_metadata(rscreen, rtex, &metadata);
640 if (rscreen->query_opaque_metadata)
641 rscreen->query_opaque_metadata(rscreen, rtex,
642 &metadata);
643
644 rscreen->ws->buffer_set_metadata(res->buf, &metadata);
645 }
646
647 if (rscreen->chip_class >= GFX9) {
648 offset = rtex->surface.u.gfx9.surf_offset;
649 stride = rtex->surface.u.gfx9.surf_pitch *
650 rtex->surface.bpe;
651 slice_size = rtex->surface.u.gfx9.surf_slice_size;
652 } else {
653 offset = rtex->surface.u.legacy.level[0].offset;
654 stride = rtex->surface.u.legacy.level[0].nblk_x *
655 rtex->surface.bpe;
656 slice_size = rtex->surface.u.legacy.level[0].slice_size;
657 }
658 } else {
659 /* Move a suballocated buffer into a non-suballocated allocation. */
660 if (rscreen->ws->buffer_is_suballocated(res->buf)) {
661 assert(!res->b.is_shared);
662
663 /* Allocate a new buffer with PIPE_BIND_SHARED. */
664 struct pipe_resource templ = res->b.b;
665 templ.bind |= PIPE_BIND_SHARED;
666
667 struct pipe_resource *newb =
668 screen->resource_create(screen, &templ);
669 if (!newb)
670 return false;
671
672 /* Copy the old buffer contents to the new one. */
673 struct pipe_box box;
674 u_box_1d(0, newb->width0, &box);
675 rctx->b.resource_copy_region(&rctx->b, newb, 0, 0, 0, 0,
676 &res->b.b, 0, &box);
677 /* Move the new buffer storage to the old pipe_resource. */
678 si_replace_buffer_storage(&rctx->b, &res->b.b, newb);
679 pipe_resource_reference(&newb, NULL);
680
681 assert(res->b.b.bind & PIPE_BIND_SHARED);
682 assert(res->flags & RADEON_FLAG_NO_SUBALLOC);
683 }
684
685 /* Buffers */
686 offset = 0;
687 stride = 0;
688 slice_size = 0;
689 }
690
691 if (res->b.is_shared) {
692 /* USAGE_EXPLICIT_FLUSH must be cleared if at least one user
693 * doesn't set it.
694 */
695 res->external_usage |= usage & ~PIPE_HANDLE_USAGE_EXPLICIT_FLUSH;
696 if (!(usage & PIPE_HANDLE_USAGE_EXPLICIT_FLUSH))
697 res->external_usage &= ~PIPE_HANDLE_USAGE_EXPLICIT_FLUSH;
698 } else {
699 res->b.is_shared = true;
700 res->external_usage = usage;
701 }
702
703 return rscreen->ws->buffer_get_handle(res->buf, stride, offset,
704 slice_size, whandle);
705 }
706
707 static void r600_texture_destroy(struct pipe_screen *screen,
708 struct pipe_resource *ptex)
709 {
710 struct r600_texture *rtex = (struct r600_texture*)ptex;
711 struct r600_resource *resource = &rtex->resource;
712
713 r600_texture_reference(&rtex->flushed_depth_texture, NULL);
714
715 if (rtex->cmask_buffer != &rtex->resource) {
716 r600_resource_reference(&rtex->cmask_buffer, NULL);
717 }
718 pb_reference(&resource->buf, NULL);
719 r600_resource_reference(&rtex->dcc_separate_buffer, NULL);
720 r600_resource_reference(&rtex->last_dcc_separate_buffer, NULL);
721 FREE(rtex);
722 }
723
724 static const struct u_resource_vtbl r600_texture_vtbl;
725
726 /* The number of samples can be specified independently of the texture. */
727 void si_texture_get_fmask_info(struct r600_common_screen *rscreen,
728 struct r600_texture *rtex,
729 unsigned nr_samples,
730 struct r600_fmask_info *out)
731 {
732 /* FMASK is allocated like an ordinary texture. */
733 struct pipe_resource templ = rtex->resource.b.b;
734 struct radeon_surf fmask = {};
735 unsigned flags, bpe;
736
737 memset(out, 0, sizeof(*out));
738
739 if (rscreen->chip_class >= GFX9) {
740 out->alignment = rtex->surface.u.gfx9.fmask_alignment;
741 out->size = rtex->surface.u.gfx9.fmask_size;
742 return;
743 }
744
745 templ.nr_samples = 1;
746 flags = rtex->surface.flags | RADEON_SURF_FMASK;
747
748 switch (nr_samples) {
749 case 2:
750 case 4:
751 bpe = 1;
752 break;
753 case 8:
754 bpe = 4;
755 break;
756 default:
757 R600_ERR("Invalid sample count for FMASK allocation.\n");
758 return;
759 }
760
761 if (rscreen->ws->surface_init(rscreen->ws, &templ, flags, bpe,
762 RADEON_SURF_MODE_2D, &fmask)) {
763 R600_ERR("Got error in surface_init while allocating FMASK.\n");
764 return;
765 }
766
767 assert(fmask.u.legacy.level[0].mode == RADEON_SURF_MODE_2D);
768
769 out->slice_tile_max = (fmask.u.legacy.level[0].nblk_x * fmask.u.legacy.level[0].nblk_y) / 64;
770 if (out->slice_tile_max)
771 out->slice_tile_max -= 1;
772
773 out->tile_mode_index = fmask.u.legacy.tiling_index[0];
774 out->pitch_in_pixels = fmask.u.legacy.level[0].nblk_x;
775 out->bank_height = fmask.u.legacy.bankh;
776 out->tile_swizzle = fmask.tile_swizzle;
777 out->alignment = MAX2(256, fmask.surf_alignment);
778 out->size = fmask.surf_size;
779 }
780
781 static void r600_texture_allocate_fmask(struct r600_common_screen *rscreen,
782 struct r600_texture *rtex)
783 {
784 si_texture_get_fmask_info(rscreen, rtex,
785 rtex->resource.b.b.nr_samples, &rtex->fmask);
786
787 rtex->fmask.offset = align64(rtex->size, rtex->fmask.alignment);
788 rtex->size = rtex->fmask.offset + rtex->fmask.size;
789 }
790
791 static void si_texture_get_cmask_info(struct r600_common_screen *rscreen,
792 struct r600_texture *rtex,
793 struct r600_cmask_info *out)
794 {
795 unsigned pipe_interleave_bytes = rscreen->info.pipe_interleave_bytes;
796 unsigned num_pipes = rscreen->info.num_tile_pipes;
797 unsigned cl_width, cl_height;
798
799 if (rscreen->chip_class >= GFX9) {
800 out->alignment = rtex->surface.u.gfx9.cmask_alignment;
801 out->size = rtex->surface.u.gfx9.cmask_size;
802 return;
803 }
804
805 switch (num_pipes) {
806 case 2:
807 cl_width = 32;
808 cl_height = 16;
809 break;
810 case 4:
811 cl_width = 32;
812 cl_height = 32;
813 break;
814 case 8:
815 cl_width = 64;
816 cl_height = 32;
817 break;
818 case 16: /* Hawaii */
819 cl_width = 64;
820 cl_height = 64;
821 break;
822 default:
823 assert(0);
824 return;
825 }
826
827 unsigned base_align = num_pipes * pipe_interleave_bytes;
828
829 unsigned width = align(rtex->resource.b.b.width0, cl_width*8);
830 unsigned height = align(rtex->resource.b.b.height0, cl_height*8);
831 unsigned slice_elements = (width * height) / (8*8);
832
833 /* Each element of CMASK is a nibble. */
834 unsigned slice_bytes = slice_elements / 2;
835
836 out->slice_tile_max = (width * height) / (128*128);
837 if (out->slice_tile_max)
838 out->slice_tile_max -= 1;
839
840 out->alignment = MAX2(256, base_align);
841 out->size = (util_max_layer(&rtex->resource.b.b, 0) + 1) *
842 align(slice_bytes, base_align);
843 }
844
845 static void r600_texture_allocate_cmask(struct r600_common_screen *rscreen,
846 struct r600_texture *rtex)
847 {
848 si_texture_get_cmask_info(rscreen, rtex, &rtex->cmask);
849
850 rtex->cmask.offset = align64(rtex->size, rtex->cmask.alignment);
851 rtex->size = rtex->cmask.offset + rtex->cmask.size;
852
853 rtex->cb_color_info |= S_028C70_FAST_CLEAR(1);
854 }
855
856 static void r600_texture_alloc_cmask_separate(struct r600_common_screen *rscreen,
857 struct r600_texture *rtex)
858 {
859 if (rtex->cmask_buffer)
860 return;
861
862 assert(rtex->cmask.size == 0);
863
864 si_texture_get_cmask_info(rscreen, rtex, &rtex->cmask);
865
866 rtex->cmask_buffer = (struct r600_resource *)
867 si_aligned_buffer_create(&rscreen->b,
868 R600_RESOURCE_FLAG_UNMAPPABLE,
869 PIPE_USAGE_DEFAULT,
870 rtex->cmask.size,
871 rtex->cmask.alignment);
872 if (rtex->cmask_buffer == NULL) {
873 rtex->cmask.size = 0;
874 return;
875 }
876
877 /* update colorbuffer state bits */
878 rtex->cmask.base_address_reg = rtex->cmask_buffer->gpu_address >> 8;
879
880 rtex->cb_color_info |= S_028C70_FAST_CLEAR(1);
881
882 p_atomic_inc(&rscreen->compressed_colortex_counter);
883 }
884
885 static void r600_texture_get_htile_size(struct r600_common_screen *rscreen,
886 struct r600_texture *rtex)
887 {
888 unsigned cl_width, cl_height, width, height;
889 unsigned slice_elements, slice_bytes, pipe_interleave_bytes, base_align;
890 unsigned num_pipes = rscreen->info.num_tile_pipes;
891
892 assert(rscreen->chip_class <= VI);
893
894 rtex->surface.htile_size = 0;
895
896 /* HTILE is broken with 1D tiling on old kernels and CIK. */
897 if (rscreen->chip_class >= CIK &&
898 rtex->surface.u.legacy.level[0].mode == RADEON_SURF_MODE_1D &&
899 rscreen->info.drm_major == 2 && rscreen->info.drm_minor < 38)
900 return;
901
902 /* Overalign HTILE on P2 configs to work around GPU hangs in
903 * piglit/depthstencil-render-miplevels 585.
904 *
905 * This has been confirmed to help Kabini & Stoney, where the hangs
906 * are always reproducible. I think I have seen the test hang
907 * on Carrizo too, though it was very rare there.
908 */
909 if (rscreen->chip_class >= CIK && num_pipes < 4)
910 num_pipes = 4;
911
912 switch (num_pipes) {
913 case 1:
914 cl_width = 32;
915 cl_height = 16;
916 break;
917 case 2:
918 cl_width = 32;
919 cl_height = 32;
920 break;
921 case 4:
922 cl_width = 64;
923 cl_height = 32;
924 break;
925 case 8:
926 cl_width = 64;
927 cl_height = 64;
928 break;
929 case 16:
930 cl_width = 128;
931 cl_height = 64;
932 break;
933 default:
934 assert(0);
935 return;
936 }
937
938 width = align(rtex->resource.b.b.width0, cl_width * 8);
939 height = align(rtex->resource.b.b.height0, cl_height * 8);
940
941 slice_elements = (width * height) / (8 * 8);
942 slice_bytes = slice_elements * 4;
943
944 pipe_interleave_bytes = rscreen->info.pipe_interleave_bytes;
945 base_align = num_pipes * pipe_interleave_bytes;
946
947 rtex->surface.htile_alignment = base_align;
948 rtex->surface.htile_size =
949 (util_max_layer(&rtex->resource.b.b, 0) + 1) *
950 align(slice_bytes, base_align);
951 }
952
953 static void r600_texture_allocate_htile(struct r600_common_screen *rscreen,
954 struct r600_texture *rtex)
955 {
956 if (rscreen->chip_class <= VI && !rtex->tc_compatible_htile)
957 r600_texture_get_htile_size(rscreen, rtex);
958
959 if (!rtex->surface.htile_size)
960 return;
961
962 rtex->htile_offset = align(rtex->size, rtex->surface.htile_alignment);
963 rtex->size = rtex->htile_offset + rtex->surface.htile_size;
964 }
965
966 void si_print_texture_info(struct r600_common_screen *rscreen,
967 struct r600_texture *rtex, struct u_log_context *log)
968 {
969 int i;
970
971 /* Common parameters. */
972 u_log_printf(log, " Info: npix_x=%u, npix_y=%u, npix_z=%u, blk_w=%u, "
973 "blk_h=%u, array_size=%u, last_level=%u, "
974 "bpe=%u, nsamples=%u, flags=0x%x, %s\n",
975 rtex->resource.b.b.width0, rtex->resource.b.b.height0,
976 rtex->resource.b.b.depth0, rtex->surface.blk_w,
977 rtex->surface.blk_h,
978 rtex->resource.b.b.array_size, rtex->resource.b.b.last_level,
979 rtex->surface.bpe, rtex->resource.b.b.nr_samples,
980 rtex->surface.flags, util_format_short_name(rtex->resource.b.b.format));
981
982 if (rscreen->chip_class >= GFX9) {
983 u_log_printf(log, " Surf: size=%"PRIu64", slice_size=%"PRIu64", "
984 "alignment=%u, swmode=%u, epitch=%u, pitch=%u\n",
985 rtex->surface.surf_size,
986 rtex->surface.u.gfx9.surf_slice_size,
987 rtex->surface.surf_alignment,
988 rtex->surface.u.gfx9.surf.swizzle_mode,
989 rtex->surface.u.gfx9.surf.epitch,
990 rtex->surface.u.gfx9.surf_pitch);
991
992 if (rtex->fmask.size) {
993 u_log_printf(log, " FMASK: offset=%"PRIu64", size=%"PRIu64", "
994 "alignment=%u, swmode=%u, epitch=%u\n",
995 rtex->fmask.offset,
996 rtex->surface.u.gfx9.fmask_size,
997 rtex->surface.u.gfx9.fmask_alignment,
998 rtex->surface.u.gfx9.fmask.swizzle_mode,
999 rtex->surface.u.gfx9.fmask.epitch);
1000 }
1001
1002 if (rtex->cmask.size) {
1003 u_log_printf(log, " CMask: offset=%"PRIu64", size=%"PRIu64", "
1004 "alignment=%u, rb_aligned=%u, pipe_aligned=%u\n",
1005 rtex->cmask.offset,
1006 rtex->surface.u.gfx9.cmask_size,
1007 rtex->surface.u.gfx9.cmask_alignment,
1008 rtex->surface.u.gfx9.cmask.rb_aligned,
1009 rtex->surface.u.gfx9.cmask.pipe_aligned);
1010 }
1011
1012 if (rtex->htile_offset) {
1013 u_log_printf(log, " HTile: offset=%"PRIu64", size=%"PRIu64", alignment=%u, "
1014 "rb_aligned=%u, pipe_aligned=%u\n",
1015 rtex->htile_offset,
1016 rtex->surface.htile_size,
1017 rtex->surface.htile_alignment,
1018 rtex->surface.u.gfx9.htile.rb_aligned,
1019 rtex->surface.u.gfx9.htile.pipe_aligned);
1020 }
1021
1022 if (rtex->dcc_offset) {
1023 u_log_printf(log, " DCC: offset=%"PRIu64", size=%"PRIu64", "
1024 "alignment=%u, pitch_max=%u, num_dcc_levels=%u\n",
1025 rtex->dcc_offset, rtex->surface.dcc_size,
1026 rtex->surface.dcc_alignment,
1027 rtex->surface.u.gfx9.dcc_pitch_max,
1028 rtex->surface.num_dcc_levels);
1029 }
1030
1031 if (rtex->surface.u.gfx9.stencil_offset) {
1032 u_log_printf(log, " Stencil: offset=%"PRIu64", swmode=%u, epitch=%u\n",
1033 rtex->surface.u.gfx9.stencil_offset,
1034 rtex->surface.u.gfx9.stencil.swizzle_mode,
1035 rtex->surface.u.gfx9.stencil.epitch);
1036 }
1037 return;
1038 }
1039
1040 u_log_printf(log, " Layout: size=%"PRIu64", alignment=%u, bankw=%u, "
1041 "bankh=%u, nbanks=%u, mtilea=%u, tilesplit=%u, pipeconfig=%u, scanout=%u\n",
1042 rtex->surface.surf_size, rtex->surface.surf_alignment, rtex->surface.u.legacy.bankw,
1043 rtex->surface.u.legacy.bankh, rtex->surface.u.legacy.num_banks, rtex->surface.u.legacy.mtilea,
1044 rtex->surface.u.legacy.tile_split, rtex->surface.u.legacy.pipe_config,
1045 (rtex->surface.flags & RADEON_SURF_SCANOUT) != 0);
1046
1047 if (rtex->fmask.size)
1048 u_log_printf(log, " FMask: offset=%"PRIu64", size=%"PRIu64", alignment=%u, pitch_in_pixels=%u, "
1049 "bankh=%u, slice_tile_max=%u, tile_mode_index=%u\n",
1050 rtex->fmask.offset, rtex->fmask.size, rtex->fmask.alignment,
1051 rtex->fmask.pitch_in_pixels, rtex->fmask.bank_height,
1052 rtex->fmask.slice_tile_max, rtex->fmask.tile_mode_index);
1053
1054 if (rtex->cmask.size)
1055 u_log_printf(log, " CMask: offset=%"PRIu64", size=%"PRIu64", alignment=%u, "
1056 "slice_tile_max=%u\n",
1057 rtex->cmask.offset, rtex->cmask.size, rtex->cmask.alignment,
1058 rtex->cmask.slice_tile_max);
1059
1060 if (rtex->htile_offset)
1061 u_log_printf(log, " HTile: offset=%"PRIu64", size=%"PRIu64", "
1062 "alignment=%u, TC_compatible = %u\n",
1063 rtex->htile_offset, rtex->surface.htile_size,
1064 rtex->surface.htile_alignment,
1065 rtex->tc_compatible_htile);
1066
1067 if (rtex->dcc_offset) {
1068 u_log_printf(log, " DCC: offset=%"PRIu64", size=%"PRIu64", alignment=%u\n",
1069 rtex->dcc_offset, rtex->surface.dcc_size,
1070 rtex->surface.dcc_alignment);
1071 for (i = 0; i <= rtex->resource.b.b.last_level; i++)
1072 u_log_printf(log, " DCCLevel[%i]: enabled=%u, offset=%"PRIu64", "
1073 "fast_clear_size=%"PRIu64"\n",
1074 i, i < rtex->surface.num_dcc_levels,
1075 rtex->surface.u.legacy.level[i].dcc_offset,
1076 rtex->surface.u.legacy.level[i].dcc_fast_clear_size);
1077 }
1078
1079 for (i = 0; i <= rtex->resource.b.b.last_level; i++)
1080 u_log_printf(log, " Level[%i]: offset=%"PRIu64", slice_size=%"PRIu64", "
1081 "npix_x=%u, npix_y=%u, npix_z=%u, nblk_x=%u, nblk_y=%u, "
1082 "mode=%u, tiling_index = %u\n",
1083 i, rtex->surface.u.legacy.level[i].offset,
1084 rtex->surface.u.legacy.level[i].slice_size,
1085 u_minify(rtex->resource.b.b.width0, i),
1086 u_minify(rtex->resource.b.b.height0, i),
1087 u_minify(rtex->resource.b.b.depth0, i),
1088 rtex->surface.u.legacy.level[i].nblk_x,
1089 rtex->surface.u.legacy.level[i].nblk_y,
1090 rtex->surface.u.legacy.level[i].mode,
1091 rtex->surface.u.legacy.tiling_index[i]);
1092
1093 if (rtex->surface.has_stencil) {
1094 u_log_printf(log, " StencilLayout: tilesplit=%u\n",
1095 rtex->surface.u.legacy.stencil_tile_split);
1096 for (i = 0; i <= rtex->resource.b.b.last_level; i++) {
1097 u_log_printf(log, " StencilLevel[%i]: offset=%"PRIu64", "
1098 "slice_size=%"PRIu64", npix_x=%u, "
1099 "npix_y=%u, npix_z=%u, nblk_x=%u, nblk_y=%u, "
1100 "mode=%u, tiling_index = %u\n",
1101 i, rtex->surface.u.legacy.stencil_level[i].offset,
1102 rtex->surface.u.legacy.stencil_level[i].slice_size,
1103 u_minify(rtex->resource.b.b.width0, i),
1104 u_minify(rtex->resource.b.b.height0, i),
1105 u_minify(rtex->resource.b.b.depth0, i),
1106 rtex->surface.u.legacy.stencil_level[i].nblk_x,
1107 rtex->surface.u.legacy.stencil_level[i].nblk_y,
1108 rtex->surface.u.legacy.stencil_level[i].mode,
1109 rtex->surface.u.legacy.stencil_tiling_index[i]);
1110 }
1111 }
1112 }
1113
1114 /* Common processing for r600_texture_create and r600_texture_from_handle */
1115 static struct r600_texture *
1116 r600_texture_create_object(struct pipe_screen *screen,
1117 const struct pipe_resource *base,
1118 struct pb_buffer *buf,
1119 struct radeon_surf *surface)
1120 {
1121 struct r600_texture *rtex;
1122 struct r600_resource *resource;
1123 struct r600_common_screen *rscreen = (struct r600_common_screen*)screen;
1124
1125 rtex = CALLOC_STRUCT(r600_texture);
1126 if (!rtex)
1127 return NULL;
1128
1129 resource = &rtex->resource;
1130 resource->b.b = *base;
1131 resource->b.b.next = NULL;
1132 resource->b.vtbl = &r600_texture_vtbl;
1133 pipe_reference_init(&resource->b.b.reference, 1);
1134 resource->b.b.screen = screen;
1135
1136 /* don't include stencil-only formats which we don't support for rendering */
1137 rtex->is_depth = util_format_has_depth(util_format_description(rtex->resource.b.b.format));
1138
1139 rtex->surface = *surface;
1140 rtex->size = rtex->surface.surf_size;
1141
1142 rtex->tc_compatible_htile = rtex->surface.htile_size != 0 &&
1143 (rtex->surface.flags &
1144 RADEON_SURF_TC_COMPATIBLE_HTILE);
1145
1146 /* TC-compatible HTILE:
1147 * - VI only supports Z32_FLOAT.
1148 * - GFX9 only supports Z32_FLOAT and Z16_UNORM. */
1149 if (rtex->tc_compatible_htile) {
1150 if (rscreen->chip_class >= GFX9 &&
1151 base->format == PIPE_FORMAT_Z16_UNORM)
1152 rtex->db_render_format = base->format;
1153 else {
1154 rtex->db_render_format = PIPE_FORMAT_Z32_FLOAT;
1155 rtex->upgraded_depth = base->format != PIPE_FORMAT_Z32_FLOAT &&
1156 base->format != PIPE_FORMAT_Z32_FLOAT_S8X24_UINT;
1157 }
1158 } else {
1159 rtex->db_render_format = base->format;
1160 }
1161
1162 /* Tiled depth textures utilize the non-displayable tile order.
1163 * This must be done after r600_setup_surface.
1164 * Applies to R600-Cayman. */
1165 rtex->non_disp_tiling = rtex->is_depth && rtex->surface.u.legacy.level[0].mode >= RADEON_SURF_MODE_1D;
1166 /* Applies to GCN. */
1167 rtex->last_msaa_resolve_target_micro_mode = rtex->surface.micro_tile_mode;
1168
1169 /* Disable separate DCC at the beginning. DRI2 doesn't reuse buffers
1170 * between frames, so the only thing that can enable separate DCC
1171 * with DRI2 is multiple slow clears within a frame.
1172 */
1173 rtex->ps_draw_ratio = 0;
1174
1175 if (rtex->is_depth) {
1176 if (rscreen->chip_class >= GFX9) {
1177 rtex->can_sample_z = true;
1178 rtex->can_sample_s = true;
1179 } else {
1180 rtex->can_sample_z = !rtex->surface.u.legacy.depth_adjusted;
1181 rtex->can_sample_s = !rtex->surface.u.legacy.stencil_adjusted;
1182 }
1183
1184 if (!(base->flags & (R600_RESOURCE_FLAG_TRANSFER |
1185 R600_RESOURCE_FLAG_FLUSHED_DEPTH))) {
1186 rtex->db_compatible = true;
1187
1188 if (!(rscreen->debug_flags & DBG(NO_HYPERZ)))
1189 r600_texture_allocate_htile(rscreen, rtex);
1190 }
1191 } else {
1192 if (base->nr_samples > 1) {
1193 if (!buf) {
1194 r600_texture_allocate_fmask(rscreen, rtex);
1195 r600_texture_allocate_cmask(rscreen, rtex);
1196 rtex->cmask_buffer = &rtex->resource;
1197 }
1198 if (!rtex->fmask.size || !rtex->cmask.size) {
1199 FREE(rtex);
1200 return NULL;
1201 }
1202 }
1203
1204 /* Shared textures must always set up DCC here.
1205 * If it's not present, it will be disabled by
1206 * apply_opaque_metadata later.
1207 */
1208 if (rtex->surface.dcc_size &&
1209 (buf || !(rscreen->debug_flags & DBG(NO_DCC))) &&
1210 !(rtex->surface.flags & RADEON_SURF_SCANOUT)) {
1211 /* Reserve space for the DCC buffer. */
1212 rtex->dcc_offset = align64(rtex->size, rtex->surface.dcc_alignment);
1213 rtex->size = rtex->dcc_offset + rtex->surface.dcc_size;
1214 }
1215 }
1216
1217 /* Now create the backing buffer. */
1218 if (!buf) {
1219 si_init_resource_fields(rscreen, resource, rtex->size,
1220 rtex->surface.surf_alignment);
1221
1222 if (!si_alloc_resource(rscreen, resource)) {
1223 FREE(rtex);
1224 return NULL;
1225 }
1226 } else {
1227 resource->buf = buf;
1228 resource->gpu_address = rscreen->ws->buffer_get_virtual_address(resource->buf);
1229 resource->bo_size = buf->size;
1230 resource->bo_alignment = buf->alignment;
1231 resource->domains = rscreen->ws->buffer_get_initial_domain(resource->buf);
1232 if (resource->domains & RADEON_DOMAIN_VRAM)
1233 resource->vram_usage = buf->size;
1234 else if (resource->domains & RADEON_DOMAIN_GTT)
1235 resource->gart_usage = buf->size;
1236 }
1237
1238 if (rtex->cmask.size) {
1239 /* Initialize the cmask to 0xCC (= compressed state). */
1240 si_screen_clear_buffer(rscreen, &rtex->cmask_buffer->b.b,
1241 rtex->cmask.offset, rtex->cmask.size,
1242 0xCCCCCCCC);
1243 }
1244 if (rtex->htile_offset) {
1245 uint32_t clear_value = 0;
1246
1247 if (rscreen->chip_class >= GFX9 || rtex->tc_compatible_htile)
1248 clear_value = 0x0000030F;
1249
1250 si_screen_clear_buffer(rscreen, &rtex->resource.b.b,
1251 rtex->htile_offset,
1252 rtex->surface.htile_size,
1253 clear_value);
1254 }
1255
1256 /* Initialize DCC only if the texture is not being imported. */
1257 if (!buf && rtex->dcc_offset) {
1258 si_screen_clear_buffer(rscreen, &rtex->resource.b.b,
1259 rtex->dcc_offset,
1260 rtex->surface.dcc_size,
1261 0xFFFFFFFF);
1262 }
1263
1264 /* Initialize the CMASK base register value. */
1265 rtex->cmask.base_address_reg =
1266 (rtex->resource.gpu_address + rtex->cmask.offset) >> 8;
1267
1268 if (rscreen->debug_flags & DBG(VM)) {
1269 fprintf(stderr, "VM start=0x%"PRIX64" end=0x%"PRIX64" | Texture %ix%ix%i, %i levels, %i samples, %s\n",
1270 rtex->resource.gpu_address,
1271 rtex->resource.gpu_address + rtex->resource.buf->size,
1272 base->width0, base->height0, util_max_layer(base, 0)+1, base->last_level+1,
1273 base->nr_samples ? base->nr_samples : 1, util_format_short_name(base->format));
1274 }
1275
1276 if (rscreen->debug_flags & DBG(TEX)) {
1277 puts("Texture:");
1278 struct u_log_context log;
1279 u_log_context_init(&log);
1280 si_print_texture_info(rscreen, rtex, &log);
1281 u_log_new_page_print(&log, stdout);
1282 fflush(stdout);
1283 u_log_context_destroy(&log);
1284 }
1285
1286 return rtex;
1287 }
1288
1289 static enum radeon_surf_mode
1290 r600_choose_tiling(struct r600_common_screen *rscreen,
1291 const struct pipe_resource *templ)
1292 {
1293 const struct util_format_description *desc = util_format_description(templ->format);
1294 bool force_tiling = templ->flags & R600_RESOURCE_FLAG_FORCE_TILING;
1295 bool is_depth_stencil = util_format_is_depth_or_stencil(templ->format) &&
1296 !(templ->flags & R600_RESOURCE_FLAG_FLUSHED_DEPTH);
1297
1298 /* MSAA resources must be 2D tiled. */
1299 if (templ->nr_samples > 1)
1300 return RADEON_SURF_MODE_2D;
1301
1302 /* Transfer resources should be linear. */
1303 if (templ->flags & R600_RESOURCE_FLAG_TRANSFER)
1304 return RADEON_SURF_MODE_LINEAR_ALIGNED;
1305
1306 /* Avoid Z/S decompress blits by forcing TC-compatible HTILE on VI,
1307 * which requires 2D tiling.
1308 */
1309 if (rscreen->chip_class == VI &&
1310 is_depth_stencil &&
1311 (templ->flags & PIPE_RESOURCE_FLAG_TEXTURING_MORE_LIKELY))
1312 return RADEON_SURF_MODE_2D;
1313
1314 /* Handle common candidates for the linear mode.
1315 * Compressed textures and DB surfaces must always be tiled.
1316 */
1317 if (!force_tiling &&
1318 !is_depth_stencil &&
1319 !util_format_is_compressed(templ->format)) {
1320 if (rscreen->debug_flags & DBG(NO_TILING))
1321 return RADEON_SURF_MODE_LINEAR_ALIGNED;
1322
1323 /* Tiling doesn't work with the 422 (SUBSAMPLED) formats on R600+. */
1324 if (desc->layout == UTIL_FORMAT_LAYOUT_SUBSAMPLED)
1325 return RADEON_SURF_MODE_LINEAR_ALIGNED;
1326
1327 /* Cursors are linear on SI.
1328 * (XXX double-check, maybe also use RADEON_SURF_SCANOUT) */
1329 if (templ->bind & PIPE_BIND_CURSOR)
1330 return RADEON_SURF_MODE_LINEAR_ALIGNED;
1331
1332 if (templ->bind & PIPE_BIND_LINEAR)
1333 return RADEON_SURF_MODE_LINEAR_ALIGNED;
1334
1335 /* Textures with a very small height are recommended to be linear. */
1336 if (templ->target == PIPE_TEXTURE_1D ||
1337 templ->target == PIPE_TEXTURE_1D_ARRAY ||
1338 /* Only very thin and long 2D textures should benefit from
1339 * linear_aligned. */
1340 (templ->width0 > 8 && templ->height0 <= 2))
1341 return RADEON_SURF_MODE_LINEAR_ALIGNED;
1342
1343 /* Textures likely to be mapped often. */
1344 if (templ->usage == PIPE_USAGE_STAGING ||
1345 templ->usage == PIPE_USAGE_STREAM)
1346 return RADEON_SURF_MODE_LINEAR_ALIGNED;
1347 }
1348
1349 /* Make small textures 1D tiled. */
1350 if (templ->width0 <= 16 || templ->height0 <= 16 ||
1351 (rscreen->debug_flags & DBG(NO_2D_TILING)))
1352 return RADEON_SURF_MODE_1D;
1353
1354 /* The allocator will switch to 1D if needed. */
1355 return RADEON_SURF_MODE_2D;
1356 }
1357
1358 struct pipe_resource *si_texture_create(struct pipe_screen *screen,
1359 const struct pipe_resource *templ)
1360 {
1361 struct r600_common_screen *rscreen = (struct r600_common_screen*)screen;
1362 struct radeon_surf surface = {0};
1363 bool is_flushed_depth = templ->flags & R600_RESOURCE_FLAG_FLUSHED_DEPTH;
1364 bool tc_compatible_htile =
1365 rscreen->chip_class >= VI &&
1366 (templ->flags & PIPE_RESOURCE_FLAG_TEXTURING_MORE_LIKELY) &&
1367 !(rscreen->debug_flags & DBG(NO_HYPERZ)) &&
1368 !is_flushed_depth &&
1369 templ->nr_samples <= 1 && /* TC-compat HTILE is less efficient with MSAA */
1370 util_format_is_depth_or_stencil(templ->format);
1371
1372 int r;
1373
1374 r = r600_init_surface(rscreen, &surface, templ,
1375 r600_choose_tiling(rscreen, templ), 0, 0,
1376 false, false, is_flushed_depth,
1377 tc_compatible_htile);
1378 if (r) {
1379 return NULL;
1380 }
1381
1382 return (struct pipe_resource *)
1383 r600_texture_create_object(screen, templ, NULL, &surface);
1384 }
1385
1386 static struct pipe_resource *r600_texture_from_handle(struct pipe_screen *screen,
1387 const struct pipe_resource *templ,
1388 struct winsys_handle *whandle,
1389 unsigned usage)
1390 {
1391 struct r600_common_screen *rscreen = (struct r600_common_screen*)screen;
1392 struct pb_buffer *buf = NULL;
1393 unsigned stride = 0, offset = 0;
1394 enum radeon_surf_mode array_mode;
1395 struct radeon_surf surface = {};
1396 int r;
1397 struct radeon_bo_metadata metadata = {};
1398 struct r600_texture *rtex;
1399 bool is_scanout;
1400
1401 /* Support only 2D textures without mipmaps */
1402 if ((templ->target != PIPE_TEXTURE_2D && templ->target != PIPE_TEXTURE_RECT) ||
1403 templ->depth0 != 1 || templ->last_level != 0)
1404 return NULL;
1405
1406 buf = rscreen->ws->buffer_from_handle(rscreen->ws, whandle, &stride, &offset);
1407 if (!buf)
1408 return NULL;
1409
1410 rscreen->ws->buffer_get_metadata(buf, &metadata);
1411 r600_surface_import_metadata(rscreen, &surface, &metadata,
1412 &array_mode, &is_scanout);
1413
1414 r = r600_init_surface(rscreen, &surface, templ, array_mode, stride,
1415 offset, true, is_scanout, false, false);
1416 if (r) {
1417 return NULL;
1418 }
1419
1420 rtex = r600_texture_create_object(screen, templ, buf, &surface);
1421 if (!rtex)
1422 return NULL;
1423
1424 rtex->resource.b.is_shared = true;
1425 rtex->resource.external_usage = usage;
1426
1427 if (rscreen->apply_opaque_metadata)
1428 rscreen->apply_opaque_metadata(rscreen, rtex, &metadata);
1429
1430 assert(rtex->surface.tile_swizzle == 0);
1431 return &rtex->resource.b.b;
1432 }
1433
1434 bool si_init_flushed_depth_texture(struct pipe_context *ctx,
1435 struct pipe_resource *texture,
1436 struct r600_texture **staging)
1437 {
1438 struct r600_texture *rtex = (struct r600_texture*)texture;
1439 struct pipe_resource resource;
1440 struct r600_texture **flushed_depth_texture = staging ?
1441 staging : &rtex->flushed_depth_texture;
1442 enum pipe_format pipe_format = texture->format;
1443
1444 if (!staging) {
1445 if (rtex->flushed_depth_texture)
1446 return true; /* it's ready */
1447
1448 if (!rtex->can_sample_z && rtex->can_sample_s) {
1449 switch (pipe_format) {
1450 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
1451 /* Save memory by not allocating the S plane. */
1452 pipe_format = PIPE_FORMAT_Z32_FLOAT;
1453 break;
1454 case PIPE_FORMAT_Z24_UNORM_S8_UINT:
1455 case PIPE_FORMAT_S8_UINT_Z24_UNORM:
1456 /* Save memory bandwidth by not copying the
1457 * stencil part during flush.
1458 *
1459 * This potentially increases memory bandwidth
1460 * if an application uses both Z and S texturing
1461 * simultaneously (a flushed Z24S8 texture
1462 * would be stored compactly), but how often
1463 * does that really happen?
1464 */
1465 pipe_format = PIPE_FORMAT_Z24X8_UNORM;
1466 break;
1467 default:;
1468 }
1469 } else if (!rtex->can_sample_s && rtex->can_sample_z) {
1470 assert(util_format_has_stencil(util_format_description(pipe_format)));
1471
1472 /* DB->CB copies to an 8bpp surface don't work. */
1473 pipe_format = PIPE_FORMAT_X24S8_UINT;
1474 }
1475 }
1476
1477 memset(&resource, 0, sizeof(resource));
1478 resource.target = texture->target;
1479 resource.format = pipe_format;
1480 resource.width0 = texture->width0;
1481 resource.height0 = texture->height0;
1482 resource.depth0 = texture->depth0;
1483 resource.array_size = texture->array_size;
1484 resource.last_level = texture->last_level;
1485 resource.nr_samples = texture->nr_samples;
1486 resource.usage = staging ? PIPE_USAGE_STAGING : PIPE_USAGE_DEFAULT;
1487 resource.bind = texture->bind & ~PIPE_BIND_DEPTH_STENCIL;
1488 resource.flags = texture->flags | R600_RESOURCE_FLAG_FLUSHED_DEPTH;
1489
1490 if (staging)
1491 resource.flags |= R600_RESOURCE_FLAG_TRANSFER;
1492
1493 *flushed_depth_texture = (struct r600_texture *)ctx->screen->resource_create(ctx->screen, &resource);
1494 if (*flushed_depth_texture == NULL) {
1495 R600_ERR("failed to create temporary texture to hold flushed depth\n");
1496 return false;
1497 }
1498
1499 (*flushed_depth_texture)->non_disp_tiling = false;
1500 return true;
1501 }
1502
1503 /**
1504 * Initialize the pipe_resource descriptor to be of the same size as the box,
1505 * which is supposed to hold a subregion of the texture "orig" at the given
1506 * mipmap level.
1507 */
1508 static void r600_init_temp_resource_from_box(struct pipe_resource *res,
1509 struct pipe_resource *orig,
1510 const struct pipe_box *box,
1511 unsigned level, unsigned flags)
1512 {
1513 memset(res, 0, sizeof(*res));
1514 res->format = orig->format;
1515 res->width0 = box->width;
1516 res->height0 = box->height;
1517 res->depth0 = 1;
1518 res->array_size = 1;
1519 res->usage = flags & R600_RESOURCE_FLAG_TRANSFER ? PIPE_USAGE_STAGING : PIPE_USAGE_DEFAULT;
1520 res->flags = flags;
1521
1522 /* We must set the correct texture target and dimensions for a 3D box. */
1523 if (box->depth > 1 && util_max_layer(orig, level) > 0) {
1524 res->target = PIPE_TEXTURE_2D_ARRAY;
1525 res->array_size = box->depth;
1526 } else {
1527 res->target = PIPE_TEXTURE_2D;
1528 }
1529 }
1530
1531 static bool r600_can_invalidate_texture(struct r600_common_screen *rscreen,
1532 struct r600_texture *rtex,
1533 unsigned transfer_usage,
1534 const struct pipe_box *box)
1535 {
1536 return !rtex->resource.b.is_shared &&
1537 !(transfer_usage & PIPE_TRANSFER_READ) &&
1538 rtex->resource.b.b.last_level == 0 &&
1539 util_texrange_covers_whole_level(&rtex->resource.b.b, 0,
1540 box->x, box->y, box->z,
1541 box->width, box->height,
1542 box->depth);
1543 }
1544
1545 static void r600_texture_invalidate_storage(struct r600_common_context *rctx,
1546 struct r600_texture *rtex)
1547 {
1548 struct r600_common_screen *rscreen = rctx->screen;
1549
1550 /* There is no point in discarding depth and tiled buffers. */
1551 assert(!rtex->is_depth);
1552 assert(rtex->surface.is_linear);
1553
1554 /* Reallocate the buffer in the same pipe_resource. */
1555 si_alloc_resource(rscreen, &rtex->resource);
1556
1557 /* Initialize the CMASK base address (needed even without CMASK). */
1558 rtex->cmask.base_address_reg =
1559 (rtex->resource.gpu_address + rtex->cmask.offset) >> 8;
1560
1561 p_atomic_inc(&rscreen->dirty_tex_counter);
1562
1563 rctx->num_alloc_tex_transfer_bytes += rtex->size;
1564 }
1565
1566 static void *r600_texture_transfer_map(struct pipe_context *ctx,
1567 struct pipe_resource *texture,
1568 unsigned level,
1569 unsigned usage,
1570 const struct pipe_box *box,
1571 struct pipe_transfer **ptransfer)
1572 {
1573 struct r600_common_context *rctx = (struct r600_common_context*)ctx;
1574 struct r600_texture *rtex = (struct r600_texture*)texture;
1575 struct r600_transfer *trans;
1576 struct r600_resource *buf;
1577 unsigned offset = 0;
1578 char *map;
1579 bool use_staging_texture = false;
1580
1581 assert(!(texture->flags & R600_RESOURCE_FLAG_TRANSFER));
1582 assert(box->width && box->height && box->depth);
1583
1584 /* Depth textures use staging unconditionally. */
1585 if (!rtex->is_depth) {
1586 /* Degrade the tile mode if we get too many transfers on APUs.
1587 * On dGPUs, the staging texture is always faster.
1588 * Only count uploads that are at least 4x4 pixels large.
1589 */
1590 if (!rctx->screen->info.has_dedicated_vram &&
1591 level == 0 &&
1592 box->width >= 4 && box->height >= 4 &&
1593 p_atomic_inc_return(&rtex->num_level0_transfers) == 10) {
1594 bool can_invalidate =
1595 r600_can_invalidate_texture(rctx->screen, rtex,
1596 usage, box);
1597
1598 r600_reallocate_texture_inplace(rctx, rtex,
1599 PIPE_BIND_LINEAR,
1600 can_invalidate);
1601 }
1602
1603 /* Tiled textures need to be converted into a linear texture for CPU
1604 * access. The staging texture is always linear and is placed in GART.
1605 *
1606 * Reading from VRAM or GTT WC is slow, always use the staging
1607 * texture in this case.
1608 *
1609 * Use the staging texture for uploads if the underlying BO
1610 * is busy.
1611 */
1612 if (!rtex->surface.is_linear)
1613 use_staging_texture = true;
1614 else if (usage & PIPE_TRANSFER_READ)
1615 use_staging_texture =
1616 rtex->resource.domains & RADEON_DOMAIN_VRAM ||
1617 rtex->resource.flags & RADEON_FLAG_GTT_WC;
1618 /* Write & linear only: */
1619 else if (si_rings_is_buffer_referenced(rctx, rtex->resource.buf,
1620 RADEON_USAGE_READWRITE) ||
1621 !rctx->ws->buffer_wait(rtex->resource.buf, 0,
1622 RADEON_USAGE_READWRITE)) {
1623 /* It's busy. */
1624 if (r600_can_invalidate_texture(rctx->screen, rtex,
1625 usage, box))
1626 r600_texture_invalidate_storage(rctx, rtex);
1627 else
1628 use_staging_texture = true;
1629 }
1630 }
1631
1632 trans = CALLOC_STRUCT(r600_transfer);
1633 if (!trans)
1634 return NULL;
1635 pipe_resource_reference(&trans->b.b.resource, texture);
1636 trans->b.b.level = level;
1637 trans->b.b.usage = usage;
1638 trans->b.b.box = *box;
1639
1640 if (rtex->is_depth) {
1641 struct r600_texture *staging_depth;
1642
1643 if (rtex->resource.b.b.nr_samples > 1) {
1644 /* MSAA depth buffers need to be converted to single sample buffers.
1645 *
1646 * Mapping MSAA depth buffers can occur if ReadPixels is called
1647 * with a multisample GLX visual.
1648 *
1649 * First downsample the depth buffer to a temporary texture,
1650 * then decompress the temporary one to staging.
1651 *
1652 * Only the region being mapped is transfered.
1653 */
1654 struct pipe_resource resource;
1655
1656 r600_init_temp_resource_from_box(&resource, texture, box, level, 0);
1657
1658 if (!si_init_flushed_depth_texture(ctx, &resource, &staging_depth)) {
1659 R600_ERR("failed to create temporary texture to hold untiled copy\n");
1660 FREE(trans);
1661 return NULL;
1662 }
1663
1664 if (usage & PIPE_TRANSFER_READ) {
1665 struct pipe_resource *temp = ctx->screen->resource_create(ctx->screen, &resource);
1666 if (!temp) {
1667 R600_ERR("failed to create a temporary depth texture\n");
1668 FREE(trans);
1669 return NULL;
1670 }
1671
1672 r600_copy_region_with_blit(ctx, temp, 0, 0, 0, 0, texture, level, box);
1673 rctx->blit_decompress_depth(ctx, (struct r600_texture*)temp, staging_depth,
1674 0, 0, 0, box->depth, 0, 0);
1675 pipe_resource_reference(&temp, NULL);
1676 }
1677
1678 /* Just get the strides. */
1679 r600_texture_get_offset(rctx->screen, staging_depth, level, NULL,
1680 &trans->b.b.stride,
1681 &trans->b.b.layer_stride);
1682 } else {
1683 /* XXX: only readback the rectangle which is being mapped? */
1684 /* XXX: when discard is true, no need to read back from depth texture */
1685 if (!si_init_flushed_depth_texture(ctx, texture, &staging_depth)) {
1686 R600_ERR("failed to create temporary texture to hold untiled copy\n");
1687 FREE(trans);
1688 return NULL;
1689 }
1690
1691 rctx->blit_decompress_depth(ctx, rtex, staging_depth,
1692 level, level,
1693 box->z, box->z + box->depth - 1,
1694 0, 0);
1695
1696 offset = r600_texture_get_offset(rctx->screen, staging_depth,
1697 level, box,
1698 &trans->b.b.stride,
1699 &trans->b.b.layer_stride);
1700 }
1701
1702 trans->staging = (struct r600_resource*)staging_depth;
1703 buf = trans->staging;
1704 } else if (use_staging_texture) {
1705 struct pipe_resource resource;
1706 struct r600_texture *staging;
1707
1708 r600_init_temp_resource_from_box(&resource, texture, box, level,
1709 R600_RESOURCE_FLAG_TRANSFER);
1710 resource.usage = (usage & PIPE_TRANSFER_READ) ?
1711 PIPE_USAGE_STAGING : PIPE_USAGE_STREAM;
1712
1713 /* Create the temporary texture. */
1714 staging = (struct r600_texture*)ctx->screen->resource_create(ctx->screen, &resource);
1715 if (!staging) {
1716 R600_ERR("failed to create temporary texture to hold untiled copy\n");
1717 FREE(trans);
1718 return NULL;
1719 }
1720 trans->staging = &staging->resource;
1721
1722 /* Just get the strides. */
1723 r600_texture_get_offset(rctx->screen, staging, 0, NULL,
1724 &trans->b.b.stride,
1725 &trans->b.b.layer_stride);
1726
1727 if (usage & PIPE_TRANSFER_READ)
1728 r600_copy_to_staging_texture(ctx, trans);
1729 else
1730 usage |= PIPE_TRANSFER_UNSYNCHRONIZED;
1731
1732 buf = trans->staging;
1733 } else {
1734 /* the resource is mapped directly */
1735 offset = r600_texture_get_offset(rctx->screen, rtex, level, box,
1736 &trans->b.b.stride,
1737 &trans->b.b.layer_stride);
1738 buf = &rtex->resource;
1739 }
1740
1741 if (!(map = si_buffer_map_sync_with_rings(rctx, buf, usage))) {
1742 r600_resource_reference(&trans->staging, NULL);
1743 FREE(trans);
1744 return NULL;
1745 }
1746
1747 *ptransfer = &trans->b.b;
1748 return map + offset;
1749 }
1750
1751 static void r600_texture_transfer_unmap(struct pipe_context *ctx,
1752 struct pipe_transfer* transfer)
1753 {
1754 struct r600_common_context *rctx = (struct r600_common_context*)ctx;
1755 struct r600_transfer *rtransfer = (struct r600_transfer*)transfer;
1756 struct pipe_resource *texture = transfer->resource;
1757 struct r600_texture *rtex = (struct r600_texture*)texture;
1758
1759 if ((transfer->usage & PIPE_TRANSFER_WRITE) && rtransfer->staging) {
1760 if (rtex->is_depth && rtex->resource.b.b.nr_samples <= 1) {
1761 ctx->resource_copy_region(ctx, texture, transfer->level,
1762 transfer->box.x, transfer->box.y, transfer->box.z,
1763 &rtransfer->staging->b.b, transfer->level,
1764 &transfer->box);
1765 } else {
1766 r600_copy_from_staging_texture(ctx, rtransfer);
1767 }
1768 }
1769
1770 if (rtransfer->staging) {
1771 rctx->num_alloc_tex_transfer_bytes += rtransfer->staging->buf->size;
1772 r600_resource_reference(&rtransfer->staging, NULL);
1773 }
1774
1775 /* Heuristic for {upload, draw, upload, draw, ..}:
1776 *
1777 * Flush the gfx IB if we've allocated too much texture storage.
1778 *
1779 * The idea is that we don't want to build IBs that use too much
1780 * memory and put pressure on the kernel memory manager and we also
1781 * want to make temporary and invalidated buffers go idle ASAP to
1782 * decrease the total memory usage or make them reusable. The memory
1783 * usage will be slightly higher than given here because of the buffer
1784 * cache in the winsys.
1785 *
1786 * The result is that the kernel memory manager is never a bottleneck.
1787 */
1788 if (rctx->num_alloc_tex_transfer_bytes > rctx->screen->info.gart_size / 4) {
1789 rctx->gfx.flush(rctx, RADEON_FLUSH_ASYNC, NULL);
1790 rctx->num_alloc_tex_transfer_bytes = 0;
1791 }
1792
1793 pipe_resource_reference(&transfer->resource, NULL);
1794 FREE(transfer);
1795 }
1796
1797 static const struct u_resource_vtbl r600_texture_vtbl =
1798 {
1799 NULL, /* get_handle */
1800 r600_texture_destroy, /* resource_destroy */
1801 r600_texture_transfer_map, /* transfer_map */
1802 u_default_transfer_flush_region, /* transfer_flush_region */
1803 r600_texture_transfer_unmap, /* transfer_unmap */
1804 };
1805
1806 /* DCC channel type categories within which formats can be reinterpreted
1807 * while keeping the same DCC encoding. The swizzle must also match. */
1808 enum dcc_channel_type {
1809 dcc_channel_float32,
1810 dcc_channel_uint32,
1811 dcc_channel_sint32,
1812 dcc_channel_float16,
1813 dcc_channel_uint16,
1814 dcc_channel_sint16,
1815 dcc_channel_uint_10_10_10_2,
1816 dcc_channel_uint8,
1817 dcc_channel_sint8,
1818 dcc_channel_incompatible,
1819 };
1820
1821 /* Return the type of DCC encoding. */
1822 static enum dcc_channel_type
1823 vi_get_dcc_channel_type(const struct util_format_description *desc)
1824 {
1825 int i;
1826
1827 /* Find the first non-void channel. */
1828 for (i = 0; i < desc->nr_channels; i++)
1829 if (desc->channel[i].type != UTIL_FORMAT_TYPE_VOID)
1830 break;
1831 if (i == desc->nr_channels)
1832 return dcc_channel_incompatible;
1833
1834 switch (desc->channel[i].size) {
1835 case 32:
1836 if (desc->channel[i].type == UTIL_FORMAT_TYPE_FLOAT)
1837 return dcc_channel_float32;
1838 if (desc->channel[i].type == UTIL_FORMAT_TYPE_UNSIGNED)
1839 return dcc_channel_uint32;
1840 return dcc_channel_sint32;
1841 case 16:
1842 if (desc->channel[i].type == UTIL_FORMAT_TYPE_FLOAT)
1843 return dcc_channel_float16;
1844 if (desc->channel[i].type == UTIL_FORMAT_TYPE_UNSIGNED)
1845 return dcc_channel_uint16;
1846 return dcc_channel_sint16;
1847 case 10:
1848 return dcc_channel_uint_10_10_10_2;
1849 case 8:
1850 if (desc->channel[i].type == UTIL_FORMAT_TYPE_UNSIGNED)
1851 return dcc_channel_uint8;
1852 return dcc_channel_sint8;
1853 default:
1854 return dcc_channel_incompatible;
1855 }
1856 }
1857
1858 /* Return if it's allowed to reinterpret one format as another with DCC enabled. */
1859 bool vi_dcc_formats_compatible(enum pipe_format format1,
1860 enum pipe_format format2)
1861 {
1862 const struct util_format_description *desc1, *desc2;
1863 enum dcc_channel_type type1, type2;
1864 int i;
1865
1866 if (format1 == format2)
1867 return true;
1868
1869 desc1 = util_format_description(format1);
1870 desc2 = util_format_description(format2);
1871
1872 if (desc1->nr_channels != desc2->nr_channels)
1873 return false;
1874
1875 /* Swizzles must be the same. */
1876 for (i = 0; i < desc1->nr_channels; i++)
1877 if (desc1->swizzle[i] <= PIPE_SWIZZLE_W &&
1878 desc2->swizzle[i] <= PIPE_SWIZZLE_W &&
1879 desc1->swizzle[i] != desc2->swizzle[i])
1880 return false;
1881
1882 type1 = vi_get_dcc_channel_type(desc1);
1883 type2 = vi_get_dcc_channel_type(desc2);
1884
1885 return type1 != dcc_channel_incompatible &&
1886 type2 != dcc_channel_incompatible &&
1887 type1 == type2;
1888 }
1889
1890 bool vi_dcc_formats_are_incompatible(struct pipe_resource *tex,
1891 unsigned level,
1892 enum pipe_format view_format)
1893 {
1894 struct r600_texture *rtex = (struct r600_texture *)tex;
1895
1896 return vi_dcc_enabled(rtex, level) &&
1897 !vi_dcc_formats_compatible(tex->format, view_format);
1898 }
1899
1900 /* This can't be merged with the above function, because
1901 * vi_dcc_formats_compatible should be called only when DCC is enabled. */
1902 void vi_disable_dcc_if_incompatible_format(struct r600_common_context *rctx,
1903 struct pipe_resource *tex,
1904 unsigned level,
1905 enum pipe_format view_format)
1906 {
1907 struct r600_texture *rtex = (struct r600_texture *)tex;
1908
1909 if (vi_dcc_formats_are_incompatible(tex, level, view_format))
1910 if (!si_texture_disable_dcc(rctx, (struct r600_texture*)tex))
1911 rctx->decompress_dcc(&rctx->b, rtex);
1912 }
1913
1914 struct pipe_surface *si_create_surface_custom(struct pipe_context *pipe,
1915 struct pipe_resource *texture,
1916 const struct pipe_surface *templ,
1917 unsigned width0, unsigned height0,
1918 unsigned width, unsigned height)
1919 {
1920 struct r600_surface *surface = CALLOC_STRUCT(r600_surface);
1921
1922 if (!surface)
1923 return NULL;
1924
1925 assert(templ->u.tex.first_layer <= util_max_layer(texture, templ->u.tex.level));
1926 assert(templ->u.tex.last_layer <= util_max_layer(texture, templ->u.tex.level));
1927
1928 pipe_reference_init(&surface->base.reference, 1);
1929 pipe_resource_reference(&surface->base.texture, texture);
1930 surface->base.context = pipe;
1931 surface->base.format = templ->format;
1932 surface->base.width = width;
1933 surface->base.height = height;
1934 surface->base.u = templ->u;
1935
1936 surface->width0 = width0;
1937 surface->height0 = height0;
1938
1939 surface->dcc_incompatible =
1940 texture->target != PIPE_BUFFER &&
1941 vi_dcc_formats_are_incompatible(texture, templ->u.tex.level,
1942 templ->format);
1943 return &surface->base;
1944 }
1945
1946 static struct pipe_surface *r600_create_surface(struct pipe_context *pipe,
1947 struct pipe_resource *tex,
1948 const struct pipe_surface *templ)
1949 {
1950 unsigned level = templ->u.tex.level;
1951 unsigned width = u_minify(tex->width0, level);
1952 unsigned height = u_minify(tex->height0, level);
1953 unsigned width0 = tex->width0;
1954 unsigned height0 = tex->height0;
1955
1956 if (tex->target != PIPE_BUFFER && templ->format != tex->format) {
1957 const struct util_format_description *tex_desc
1958 = util_format_description(tex->format);
1959 const struct util_format_description *templ_desc
1960 = util_format_description(templ->format);
1961
1962 assert(tex_desc->block.bits == templ_desc->block.bits);
1963
1964 /* Adjust size of surface if and only if the block width or
1965 * height is changed. */
1966 if (tex_desc->block.width != templ_desc->block.width ||
1967 tex_desc->block.height != templ_desc->block.height) {
1968 unsigned nblks_x = util_format_get_nblocksx(tex->format, width);
1969 unsigned nblks_y = util_format_get_nblocksy(tex->format, height);
1970
1971 width = nblks_x * templ_desc->block.width;
1972 height = nblks_y * templ_desc->block.height;
1973
1974 width0 = util_format_get_nblocksx(tex->format, width0);
1975 height0 = util_format_get_nblocksy(tex->format, height0);
1976 }
1977 }
1978
1979 return si_create_surface_custom(pipe, tex, templ,
1980 width0, height0,
1981 width, height);
1982 }
1983
1984 static void r600_surface_destroy(struct pipe_context *pipe,
1985 struct pipe_surface *surface)
1986 {
1987 struct r600_surface *surf = (struct r600_surface*)surface;
1988 r600_resource_reference(&surf->cb_buffer_fmask, NULL);
1989 r600_resource_reference(&surf->cb_buffer_cmask, NULL);
1990 pipe_resource_reference(&surface->texture, NULL);
1991 FREE(surface);
1992 }
1993
1994 static void r600_clear_texture(struct pipe_context *pipe,
1995 struct pipe_resource *tex,
1996 unsigned level,
1997 const struct pipe_box *box,
1998 const void *data)
1999 {
2000 struct pipe_screen *screen = pipe->screen;
2001 struct r600_texture *rtex = (struct r600_texture*)tex;
2002 struct pipe_surface tmpl = {{0}};
2003 struct pipe_surface *sf;
2004 const struct util_format_description *desc =
2005 util_format_description(tex->format);
2006
2007 tmpl.format = tex->format;
2008 tmpl.u.tex.first_layer = box->z;
2009 tmpl.u.tex.last_layer = box->z + box->depth - 1;
2010 tmpl.u.tex.level = level;
2011 sf = pipe->create_surface(pipe, tex, &tmpl);
2012 if (!sf)
2013 return;
2014
2015 if (rtex->is_depth) {
2016 unsigned clear;
2017 float depth;
2018 uint8_t stencil = 0;
2019
2020 /* Depth is always present. */
2021 clear = PIPE_CLEAR_DEPTH;
2022 desc->unpack_z_float(&depth, 0, data, 0, 1, 1);
2023
2024 if (rtex->surface.has_stencil) {
2025 clear |= PIPE_CLEAR_STENCIL;
2026 desc->unpack_s_8uint(&stencil, 0, data, 0, 1, 1);
2027 }
2028
2029 pipe->clear_depth_stencil(pipe, sf, clear, depth, stencil,
2030 box->x, box->y,
2031 box->width, box->height, false);
2032 } else {
2033 union pipe_color_union color;
2034
2035 /* pipe_color_union requires the full vec4 representation. */
2036 if (util_format_is_pure_uint(tex->format))
2037 desc->unpack_rgba_uint(color.ui, 0, data, 0, 1, 1);
2038 else if (util_format_is_pure_sint(tex->format))
2039 desc->unpack_rgba_sint(color.i, 0, data, 0, 1, 1);
2040 else
2041 desc->unpack_rgba_float(color.f, 0, data, 0, 1, 1);
2042
2043 if (screen->is_format_supported(screen, tex->format,
2044 tex->target, 0,
2045 PIPE_BIND_RENDER_TARGET)) {
2046 pipe->clear_render_target(pipe, sf, &color,
2047 box->x, box->y,
2048 box->width, box->height, false);
2049 } else {
2050 /* Software fallback - just for R9G9B9E5_FLOAT */
2051 util_clear_render_target(pipe, sf, &color,
2052 box->x, box->y,
2053 box->width, box->height);
2054 }
2055 }
2056 pipe_surface_reference(&sf, NULL);
2057 }
2058
2059 unsigned si_translate_colorswap(enum pipe_format format, bool do_endian_swap)
2060 {
2061 const struct util_format_description *desc = util_format_description(format);
2062
2063 #define HAS_SWIZZLE(chan,swz) (desc->swizzle[chan] == PIPE_SWIZZLE_##swz)
2064
2065 if (format == PIPE_FORMAT_R11G11B10_FLOAT) /* isn't plain */
2066 return V_028C70_SWAP_STD;
2067
2068 if (desc->layout != UTIL_FORMAT_LAYOUT_PLAIN)
2069 return ~0U;
2070
2071 switch (desc->nr_channels) {
2072 case 1:
2073 if (HAS_SWIZZLE(0,X))
2074 return V_028C70_SWAP_STD; /* X___ */
2075 else if (HAS_SWIZZLE(3,X))
2076 return V_028C70_SWAP_ALT_REV; /* ___X */
2077 break;
2078 case 2:
2079 if ((HAS_SWIZZLE(0,X) && HAS_SWIZZLE(1,Y)) ||
2080 (HAS_SWIZZLE(0,X) && HAS_SWIZZLE(1,NONE)) ||
2081 (HAS_SWIZZLE(0,NONE) && HAS_SWIZZLE(1,Y)))
2082 return V_028C70_SWAP_STD; /* XY__ */
2083 else if ((HAS_SWIZZLE(0,Y) && HAS_SWIZZLE(1,X)) ||
2084 (HAS_SWIZZLE(0,Y) && HAS_SWIZZLE(1,NONE)) ||
2085 (HAS_SWIZZLE(0,NONE) && HAS_SWIZZLE(1,X)))
2086 /* YX__ */
2087 return (do_endian_swap ? V_028C70_SWAP_STD : V_028C70_SWAP_STD_REV);
2088 else if (HAS_SWIZZLE(0,X) && HAS_SWIZZLE(3,Y))
2089 return V_028C70_SWAP_ALT; /* X__Y */
2090 else if (HAS_SWIZZLE(0,Y) && HAS_SWIZZLE(3,X))
2091 return V_028C70_SWAP_ALT_REV; /* Y__X */
2092 break;
2093 case 3:
2094 if (HAS_SWIZZLE(0,X))
2095 return (do_endian_swap ? V_028C70_SWAP_STD_REV : V_028C70_SWAP_STD);
2096 else if (HAS_SWIZZLE(0,Z))
2097 return V_028C70_SWAP_STD_REV; /* ZYX */
2098 break;
2099 case 4:
2100 /* check the middle channels, the 1st and 4th channel can be NONE */
2101 if (HAS_SWIZZLE(1,Y) && HAS_SWIZZLE(2,Z)) {
2102 return V_028C70_SWAP_STD; /* XYZW */
2103 } else if (HAS_SWIZZLE(1,Z) && HAS_SWIZZLE(2,Y)) {
2104 return V_028C70_SWAP_STD_REV; /* WZYX */
2105 } else if (HAS_SWIZZLE(1,Y) && HAS_SWIZZLE(2,X)) {
2106 return V_028C70_SWAP_ALT; /* ZYXW */
2107 } else if (HAS_SWIZZLE(1,Z) && HAS_SWIZZLE(2,W)) {
2108 /* YZWX */
2109 if (desc->is_array)
2110 return V_028C70_SWAP_ALT_REV;
2111 else
2112 return (do_endian_swap ? V_028C70_SWAP_ALT : V_028C70_SWAP_ALT_REV);
2113 }
2114 break;
2115 }
2116 return ~0U;
2117 }
2118
2119 /* PIPELINE_STAT-BASED DCC ENABLEMENT FOR DISPLAYABLE SURFACES */
2120
2121 static void vi_dcc_clean_up_context_slot(struct r600_common_context *rctx,
2122 int slot)
2123 {
2124 int i;
2125
2126 if (rctx->dcc_stats[slot].query_active)
2127 vi_separate_dcc_stop_query(&rctx->b,
2128 rctx->dcc_stats[slot].tex);
2129
2130 for (i = 0; i < ARRAY_SIZE(rctx->dcc_stats[slot].ps_stats); i++)
2131 if (rctx->dcc_stats[slot].ps_stats[i]) {
2132 rctx->b.destroy_query(&rctx->b,
2133 rctx->dcc_stats[slot].ps_stats[i]);
2134 rctx->dcc_stats[slot].ps_stats[i] = NULL;
2135 }
2136
2137 r600_texture_reference(&rctx->dcc_stats[slot].tex, NULL);
2138 }
2139
2140 /**
2141 * Return the per-context slot where DCC statistics queries for the texture live.
2142 */
2143 static unsigned vi_get_context_dcc_stats_index(struct r600_common_context *rctx,
2144 struct r600_texture *tex)
2145 {
2146 int i, empty_slot = -1;
2147
2148 /* Remove zombie textures (textures kept alive by this array only). */
2149 for (i = 0; i < ARRAY_SIZE(rctx->dcc_stats); i++)
2150 if (rctx->dcc_stats[i].tex &&
2151 rctx->dcc_stats[i].tex->resource.b.b.reference.count == 1)
2152 vi_dcc_clean_up_context_slot(rctx, i);
2153
2154 /* Find the texture. */
2155 for (i = 0; i < ARRAY_SIZE(rctx->dcc_stats); i++) {
2156 /* Return if found. */
2157 if (rctx->dcc_stats[i].tex == tex) {
2158 rctx->dcc_stats[i].last_use_timestamp = os_time_get();
2159 return i;
2160 }
2161
2162 /* Record the first seen empty slot. */
2163 if (empty_slot == -1 && !rctx->dcc_stats[i].tex)
2164 empty_slot = i;
2165 }
2166
2167 /* Not found. Remove the oldest member to make space in the array. */
2168 if (empty_slot == -1) {
2169 int oldest_slot = 0;
2170
2171 /* Find the oldest slot. */
2172 for (i = 1; i < ARRAY_SIZE(rctx->dcc_stats); i++)
2173 if (rctx->dcc_stats[oldest_slot].last_use_timestamp >
2174 rctx->dcc_stats[i].last_use_timestamp)
2175 oldest_slot = i;
2176
2177 /* Clean up the oldest slot. */
2178 vi_dcc_clean_up_context_slot(rctx, oldest_slot);
2179 empty_slot = oldest_slot;
2180 }
2181
2182 /* Add the texture to the new slot. */
2183 r600_texture_reference(&rctx->dcc_stats[empty_slot].tex, tex);
2184 rctx->dcc_stats[empty_slot].last_use_timestamp = os_time_get();
2185 return empty_slot;
2186 }
2187
2188 static struct pipe_query *
2189 vi_create_resuming_pipestats_query(struct pipe_context *ctx)
2190 {
2191 struct r600_query_hw *query = (struct r600_query_hw*)
2192 ctx->create_query(ctx, PIPE_QUERY_PIPELINE_STATISTICS, 0);
2193
2194 query->flags |= R600_QUERY_HW_FLAG_BEGIN_RESUMES;
2195 return (struct pipe_query*)query;
2196 }
2197
2198 /**
2199 * Called when binding a color buffer.
2200 */
2201 void vi_separate_dcc_start_query(struct pipe_context *ctx,
2202 struct r600_texture *tex)
2203 {
2204 struct r600_common_context *rctx = (struct r600_common_context*)ctx;
2205 unsigned i = vi_get_context_dcc_stats_index(rctx, tex);
2206
2207 assert(!rctx->dcc_stats[i].query_active);
2208
2209 if (!rctx->dcc_stats[i].ps_stats[0])
2210 rctx->dcc_stats[i].ps_stats[0] = vi_create_resuming_pipestats_query(ctx);
2211
2212 /* begin or resume the query */
2213 ctx->begin_query(ctx, rctx->dcc_stats[i].ps_stats[0]);
2214 rctx->dcc_stats[i].query_active = true;
2215 }
2216
2217 /**
2218 * Called when unbinding a color buffer.
2219 */
2220 void vi_separate_dcc_stop_query(struct pipe_context *ctx,
2221 struct r600_texture *tex)
2222 {
2223 struct r600_common_context *rctx = (struct r600_common_context*)ctx;
2224 unsigned i = vi_get_context_dcc_stats_index(rctx, tex);
2225
2226 assert(rctx->dcc_stats[i].query_active);
2227 assert(rctx->dcc_stats[i].ps_stats[0]);
2228
2229 /* pause or end the query */
2230 ctx->end_query(ctx, rctx->dcc_stats[i].ps_stats[0]);
2231 rctx->dcc_stats[i].query_active = false;
2232 }
2233
2234 static bool vi_should_enable_separate_dcc(struct r600_texture *tex)
2235 {
2236 /* The minimum number of fullscreen draws per frame that is required
2237 * to enable DCC. */
2238 return tex->ps_draw_ratio + tex->num_slow_clears >= 5;
2239 }
2240
2241 /* Called by fast clear. */
2242 static void vi_separate_dcc_try_enable(struct r600_common_context *rctx,
2243 struct r600_texture *tex)
2244 {
2245 /* The intent is to use this with shared displayable back buffers,
2246 * but it's not strictly limited only to them.
2247 */
2248 if (!tex->resource.b.is_shared ||
2249 !(tex->resource.external_usage & PIPE_HANDLE_USAGE_EXPLICIT_FLUSH) ||
2250 tex->resource.b.b.target != PIPE_TEXTURE_2D ||
2251 tex->resource.b.b.last_level > 0 ||
2252 !tex->surface.dcc_size)
2253 return;
2254
2255 if (tex->dcc_offset)
2256 return; /* already enabled */
2257
2258 /* Enable the DCC stat gathering. */
2259 if (!tex->dcc_gather_statistics) {
2260 tex->dcc_gather_statistics = true;
2261 vi_separate_dcc_start_query(&rctx->b, tex);
2262 }
2263
2264 if (!vi_should_enable_separate_dcc(tex))
2265 return; /* stats show that DCC decompression is too expensive */
2266
2267 assert(tex->surface.num_dcc_levels);
2268 assert(!tex->dcc_separate_buffer);
2269
2270 r600_texture_discard_cmask(rctx->screen, tex);
2271
2272 /* Get a DCC buffer. */
2273 if (tex->last_dcc_separate_buffer) {
2274 assert(tex->dcc_gather_statistics);
2275 assert(!tex->dcc_separate_buffer);
2276 tex->dcc_separate_buffer = tex->last_dcc_separate_buffer;
2277 tex->last_dcc_separate_buffer = NULL;
2278 } else {
2279 tex->dcc_separate_buffer = (struct r600_resource*)
2280 si_aligned_buffer_create(rctx->b.screen,
2281 R600_RESOURCE_FLAG_UNMAPPABLE,
2282 PIPE_USAGE_DEFAULT,
2283 tex->surface.dcc_size,
2284 tex->surface.dcc_alignment);
2285 if (!tex->dcc_separate_buffer)
2286 return;
2287 }
2288
2289 /* dcc_offset is the absolute GPUVM address. */
2290 tex->dcc_offset = tex->dcc_separate_buffer->gpu_address;
2291
2292 /* no need to flag anything since this is called by fast clear that
2293 * flags framebuffer state
2294 */
2295 }
2296
2297 /**
2298 * Called by pipe_context::flush_resource, the place where DCC decompression
2299 * takes place.
2300 */
2301 void vi_separate_dcc_process_and_reset_stats(struct pipe_context *ctx,
2302 struct r600_texture *tex)
2303 {
2304 struct r600_common_context *rctx = (struct r600_common_context*)ctx;
2305 struct pipe_query *tmp;
2306 unsigned i = vi_get_context_dcc_stats_index(rctx, tex);
2307 bool query_active = rctx->dcc_stats[i].query_active;
2308 bool disable = false;
2309
2310 if (rctx->dcc_stats[i].ps_stats[2]) {
2311 union pipe_query_result result;
2312
2313 /* Read the results. */
2314 ctx->get_query_result(ctx, rctx->dcc_stats[i].ps_stats[2],
2315 true, &result);
2316 si_query_hw_reset_buffers(rctx,
2317 (struct r600_query_hw*)
2318 rctx->dcc_stats[i].ps_stats[2]);
2319
2320 /* Compute the approximate number of fullscreen draws. */
2321 tex->ps_draw_ratio =
2322 result.pipeline_statistics.ps_invocations /
2323 (tex->resource.b.b.width0 * tex->resource.b.b.height0);
2324 rctx->last_tex_ps_draw_ratio = tex->ps_draw_ratio;
2325
2326 disable = tex->dcc_separate_buffer &&
2327 !vi_should_enable_separate_dcc(tex);
2328 }
2329
2330 tex->num_slow_clears = 0;
2331
2332 /* stop the statistics query for ps_stats[0] */
2333 if (query_active)
2334 vi_separate_dcc_stop_query(ctx, tex);
2335
2336 /* Move the queries in the queue by one. */
2337 tmp = rctx->dcc_stats[i].ps_stats[2];
2338 rctx->dcc_stats[i].ps_stats[2] = rctx->dcc_stats[i].ps_stats[1];
2339 rctx->dcc_stats[i].ps_stats[1] = rctx->dcc_stats[i].ps_stats[0];
2340 rctx->dcc_stats[i].ps_stats[0] = tmp;
2341
2342 /* create and start a new query as ps_stats[0] */
2343 if (query_active)
2344 vi_separate_dcc_start_query(ctx, tex);
2345
2346 if (disable) {
2347 assert(!tex->last_dcc_separate_buffer);
2348 tex->last_dcc_separate_buffer = tex->dcc_separate_buffer;
2349 tex->dcc_separate_buffer = NULL;
2350 tex->dcc_offset = 0;
2351 /* no need to flag anything since this is called after
2352 * decompression that re-sets framebuffer state
2353 */
2354 }
2355 }
2356
2357 /* FAST COLOR CLEAR */
2358
2359 static void evergreen_set_clear_color(struct r600_texture *rtex,
2360 enum pipe_format surface_format,
2361 const union pipe_color_union *color)
2362 {
2363 union util_color uc;
2364
2365 memset(&uc, 0, sizeof(uc));
2366
2367 if (rtex->surface.bpe == 16) {
2368 /* DCC fast clear only:
2369 * CLEAR_WORD0 = R = G = B
2370 * CLEAR_WORD1 = A
2371 */
2372 assert(color->ui[0] == color->ui[1] &&
2373 color->ui[0] == color->ui[2]);
2374 uc.ui[0] = color->ui[0];
2375 uc.ui[1] = color->ui[3];
2376 } else if (util_format_is_pure_uint(surface_format)) {
2377 util_format_write_4ui(surface_format, color->ui, 0, &uc, 0, 0, 0, 1, 1);
2378 } else if (util_format_is_pure_sint(surface_format)) {
2379 util_format_write_4i(surface_format, color->i, 0, &uc, 0, 0, 0, 1, 1);
2380 } else {
2381 util_pack_color(color->f, surface_format, &uc);
2382 }
2383
2384 memcpy(rtex->color_clear_value, &uc, 2 * sizeof(uint32_t));
2385 }
2386
2387 static bool vi_get_fast_clear_parameters(enum pipe_format surface_format,
2388 const union pipe_color_union *color,
2389 uint32_t* reset_value,
2390 bool* clear_words_needed)
2391 {
2392 bool values[4] = {};
2393 int i;
2394 bool main_value = false;
2395 bool extra_value = false;
2396 int extra_channel;
2397
2398 /* This is needed to get the correct DCC clear value for luminance formats.
2399 * 1) Get the linear format (because the next step can't handle L8_SRGB).
2400 * 2) Convert luminance to red. (the real hw format for luminance)
2401 */
2402 surface_format = util_format_linear(surface_format);
2403 surface_format = util_format_luminance_to_red(surface_format);
2404
2405 const struct util_format_description *desc = util_format_description(surface_format);
2406
2407 if (desc->block.bits == 128 &&
2408 (color->ui[0] != color->ui[1] ||
2409 color->ui[0] != color->ui[2]))
2410 return false;
2411
2412 *clear_words_needed = true;
2413 *reset_value = 0x20202020U;
2414
2415 /* If we want to clear without needing a fast clear eliminate step, we
2416 * can set each channel to 0 or 1 (or 0/max for integer formats). We
2417 * have two sets of flags, one for the last or first channel(extra) and
2418 * one for the other channels(main).
2419 */
2420
2421 if (surface_format == PIPE_FORMAT_R11G11B10_FLOAT ||
2422 surface_format == PIPE_FORMAT_B5G6R5_UNORM ||
2423 surface_format == PIPE_FORMAT_B5G6R5_SRGB ||
2424 util_format_is_alpha(surface_format)) {
2425 extra_channel = -1;
2426 } else if (desc->layout == UTIL_FORMAT_LAYOUT_PLAIN) {
2427 if(si_translate_colorswap(surface_format, false) <= 1)
2428 extra_channel = desc->nr_channels - 1;
2429 else
2430 extra_channel = 0;
2431 } else
2432 return true;
2433
2434 for (i = 0; i < 4; ++i) {
2435 int index = desc->swizzle[i] - PIPE_SWIZZLE_X;
2436
2437 if (desc->swizzle[i] < PIPE_SWIZZLE_X ||
2438 desc->swizzle[i] > PIPE_SWIZZLE_W)
2439 continue;
2440
2441 if (desc->channel[i].pure_integer &&
2442 desc->channel[i].type == UTIL_FORMAT_TYPE_SIGNED) {
2443 /* Use the maximum value for clamping the clear color. */
2444 int max = u_bit_consecutive(0, desc->channel[i].size - 1);
2445
2446 values[i] = color->i[i] != 0;
2447 if (color->i[i] != 0 && MIN2(color->i[i], max) != max)
2448 return true;
2449 } else if (desc->channel[i].pure_integer &&
2450 desc->channel[i].type == UTIL_FORMAT_TYPE_UNSIGNED) {
2451 /* Use the maximum value for clamping the clear color. */
2452 unsigned max = u_bit_consecutive(0, desc->channel[i].size);
2453
2454 values[i] = color->ui[i] != 0U;
2455 if (color->ui[i] != 0U && MIN2(color->ui[i], max) != max)
2456 return true;
2457 } else {
2458 values[i] = color->f[i] != 0.0F;
2459 if (color->f[i] != 0.0F && color->f[i] != 1.0F)
2460 return true;
2461 }
2462
2463 if (index == extra_channel)
2464 extra_value = values[i];
2465 else
2466 main_value = values[i];
2467 }
2468
2469 for (int i = 0; i < 4; ++i)
2470 if (values[i] != main_value &&
2471 desc->swizzle[i] - PIPE_SWIZZLE_X != extra_channel &&
2472 desc->swizzle[i] >= PIPE_SWIZZLE_X &&
2473 desc->swizzle[i] <= PIPE_SWIZZLE_W)
2474 return true;
2475
2476 *clear_words_needed = false;
2477 if (main_value)
2478 *reset_value |= 0x80808080U;
2479
2480 if (extra_value)
2481 *reset_value |= 0x40404040U;
2482 return true;
2483 }
2484
2485 void vi_dcc_clear_level(struct r600_common_context *rctx,
2486 struct r600_texture *rtex,
2487 unsigned level, unsigned clear_value)
2488 {
2489 struct pipe_resource *dcc_buffer;
2490 uint64_t dcc_offset, clear_size;
2491
2492 assert(vi_dcc_enabled(rtex, level));
2493
2494 if (rtex->dcc_separate_buffer) {
2495 dcc_buffer = &rtex->dcc_separate_buffer->b.b;
2496 dcc_offset = 0;
2497 } else {
2498 dcc_buffer = &rtex->resource.b.b;
2499 dcc_offset = rtex->dcc_offset;
2500 }
2501
2502 if (rctx->chip_class >= GFX9) {
2503 /* Mipmap level clears aren't implemented. */
2504 assert(rtex->resource.b.b.last_level == 0);
2505 /* MSAA needs a different clear size. */
2506 assert(rtex->resource.b.b.nr_samples <= 1);
2507 clear_size = rtex->surface.dcc_size;
2508 } else {
2509 dcc_offset += rtex->surface.u.legacy.level[level].dcc_offset;
2510 clear_size = rtex->surface.u.legacy.level[level].dcc_fast_clear_size;
2511 }
2512
2513 rctx->clear_buffer(&rctx->b, dcc_buffer, dcc_offset, clear_size,
2514 clear_value, R600_COHERENCY_CB_META);
2515 }
2516
2517 /* Set the same micro tile mode as the destination of the last MSAA resolve.
2518 * This allows hitting the MSAA resolve fast path, which requires that both
2519 * src and dst micro tile modes match.
2520 */
2521 static void si_set_optimal_micro_tile_mode(struct r600_common_screen *rscreen,
2522 struct r600_texture *rtex)
2523 {
2524 if (rtex->resource.b.is_shared ||
2525 rtex->resource.b.b.nr_samples <= 1 ||
2526 rtex->surface.micro_tile_mode == rtex->last_msaa_resolve_target_micro_mode)
2527 return;
2528
2529 assert(rscreen->chip_class >= GFX9 ||
2530 rtex->surface.u.legacy.level[0].mode == RADEON_SURF_MODE_2D);
2531 assert(rtex->resource.b.b.last_level == 0);
2532
2533 if (rscreen->chip_class >= GFX9) {
2534 /* 4K or larger tiles only. 0 is linear. 1-3 are 256B tiles. */
2535 assert(rtex->surface.u.gfx9.surf.swizzle_mode >= 4);
2536
2537 /* If you do swizzle_mode % 4, you'll get:
2538 * 0 = Depth
2539 * 1 = Standard,
2540 * 2 = Displayable
2541 * 3 = Rotated
2542 *
2543 * Depth-sample order isn't allowed:
2544 */
2545 assert(rtex->surface.u.gfx9.surf.swizzle_mode % 4 != 0);
2546
2547 switch (rtex->last_msaa_resolve_target_micro_mode) {
2548 case RADEON_MICRO_MODE_DISPLAY:
2549 rtex->surface.u.gfx9.surf.swizzle_mode &= ~0x3;
2550 rtex->surface.u.gfx9.surf.swizzle_mode += 2; /* D */
2551 break;
2552 case RADEON_MICRO_MODE_THIN:
2553 rtex->surface.u.gfx9.surf.swizzle_mode &= ~0x3;
2554 rtex->surface.u.gfx9.surf.swizzle_mode += 1; /* S */
2555 break;
2556 case RADEON_MICRO_MODE_ROTATED:
2557 rtex->surface.u.gfx9.surf.swizzle_mode &= ~0x3;
2558 rtex->surface.u.gfx9.surf.swizzle_mode += 3; /* R */
2559 break;
2560 default: /* depth */
2561 assert(!"unexpected micro mode");
2562 return;
2563 }
2564 } else if (rscreen->chip_class >= CIK) {
2565 /* These magic numbers were copied from addrlib. It doesn't use
2566 * any definitions for them either. They are all 2D_TILED_THIN1
2567 * modes with different bpp and micro tile mode.
2568 */
2569 switch (rtex->last_msaa_resolve_target_micro_mode) {
2570 case RADEON_MICRO_MODE_DISPLAY:
2571 rtex->surface.u.legacy.tiling_index[0] = 10;
2572 break;
2573 case RADEON_MICRO_MODE_THIN:
2574 rtex->surface.u.legacy.tiling_index[0] = 14;
2575 break;
2576 case RADEON_MICRO_MODE_ROTATED:
2577 rtex->surface.u.legacy.tiling_index[0] = 28;
2578 break;
2579 default: /* depth, thick */
2580 assert(!"unexpected micro mode");
2581 return;
2582 }
2583 } else { /* SI */
2584 switch (rtex->last_msaa_resolve_target_micro_mode) {
2585 case RADEON_MICRO_MODE_DISPLAY:
2586 switch (rtex->surface.bpe) {
2587 case 1:
2588 rtex->surface.u.legacy.tiling_index[0] = 10;
2589 break;
2590 case 2:
2591 rtex->surface.u.legacy.tiling_index[0] = 11;
2592 break;
2593 default: /* 4, 8 */
2594 rtex->surface.u.legacy.tiling_index[0] = 12;
2595 break;
2596 }
2597 break;
2598 case RADEON_MICRO_MODE_THIN:
2599 switch (rtex->surface.bpe) {
2600 case 1:
2601 rtex->surface.u.legacy.tiling_index[0] = 14;
2602 break;
2603 case 2:
2604 rtex->surface.u.legacy.tiling_index[0] = 15;
2605 break;
2606 case 4:
2607 rtex->surface.u.legacy.tiling_index[0] = 16;
2608 break;
2609 default: /* 8, 16 */
2610 rtex->surface.u.legacy.tiling_index[0] = 17;
2611 break;
2612 }
2613 break;
2614 default: /* depth, thick */
2615 assert(!"unexpected micro mode");
2616 return;
2617 }
2618 }
2619
2620 rtex->surface.micro_tile_mode = rtex->last_msaa_resolve_target_micro_mode;
2621
2622 p_atomic_inc(&rscreen->dirty_tex_counter);
2623 }
2624
2625 void si_do_fast_color_clear(struct r600_common_context *rctx,
2626 struct pipe_framebuffer_state *fb,
2627 struct r600_atom *fb_state,
2628 unsigned *buffers, ubyte *dirty_cbufs,
2629 const union pipe_color_union *color)
2630 {
2631 int i;
2632
2633 /* This function is broken in BE, so just disable this path for now */
2634 #ifdef PIPE_ARCH_BIG_ENDIAN
2635 return;
2636 #endif
2637
2638 if (rctx->render_cond)
2639 return;
2640
2641 for (i = 0; i < fb->nr_cbufs; i++) {
2642 struct r600_texture *tex;
2643 unsigned clear_bit = PIPE_CLEAR_COLOR0 << i;
2644
2645 if (!fb->cbufs[i])
2646 continue;
2647
2648 /* if this colorbuffer is not being cleared */
2649 if (!(*buffers & clear_bit))
2650 continue;
2651
2652 tex = (struct r600_texture *)fb->cbufs[i]->texture;
2653
2654 /* the clear is allowed if all layers are bound */
2655 if (fb->cbufs[i]->u.tex.first_layer != 0 ||
2656 fb->cbufs[i]->u.tex.last_layer != util_max_layer(&tex->resource.b.b, 0)) {
2657 continue;
2658 }
2659
2660 /* cannot clear mipmapped textures */
2661 if (fb->cbufs[i]->texture->last_level != 0) {
2662 continue;
2663 }
2664
2665 /* only supported on tiled surfaces */
2666 if (tex->surface.is_linear) {
2667 continue;
2668 }
2669
2670 /* shared textures can't use fast clear without an explicit flush,
2671 * because there is no way to communicate the clear color among
2672 * all clients
2673 */
2674 if (tex->resource.b.is_shared &&
2675 !(tex->resource.external_usage & PIPE_HANDLE_USAGE_EXPLICIT_FLUSH))
2676 continue;
2677
2678 /* fast color clear with 1D tiling doesn't work on old kernels and CIK */
2679 if (rctx->chip_class == CIK &&
2680 tex->surface.u.legacy.level[0].mode == RADEON_SURF_MODE_1D &&
2681 rctx->screen->info.drm_major == 2 &&
2682 rctx->screen->info.drm_minor < 38) {
2683 continue;
2684 }
2685
2686 /* Fast clear is the most appropriate place to enable DCC for
2687 * displayable surfaces.
2688 */
2689 if (rctx->chip_class >= VI &&
2690 !(rctx->screen->debug_flags & DBG(NO_DCC_FB))) {
2691 vi_separate_dcc_try_enable(rctx, tex);
2692
2693 /* RB+ isn't supported with a CMASK clear only on Stoney,
2694 * so all clears are considered to be hypothetically slow
2695 * clears, which is weighed when determining whether to
2696 * enable separate DCC.
2697 */
2698 if (tex->dcc_gather_statistics &&
2699 rctx->family == CHIP_STONEY)
2700 tex->num_slow_clears++;
2701 }
2702
2703 /* Try to clear DCC first, otherwise try CMASK. */
2704 if (vi_dcc_enabled(tex, 0)) {
2705 uint32_t reset_value;
2706 bool clear_words_needed;
2707
2708 if (rctx->screen->debug_flags & DBG(NO_DCC_CLEAR))
2709 continue;
2710
2711 if (!vi_get_fast_clear_parameters(fb->cbufs[i]->format,
2712 color, &reset_value,
2713 &clear_words_needed))
2714 continue;
2715
2716 vi_dcc_clear_level(rctx, tex, 0, reset_value);
2717
2718 unsigned level_bit = 1 << fb->cbufs[i]->u.tex.level;
2719 if (clear_words_needed) {
2720 bool need_compressed_update = !tex->dirty_level_mask;
2721
2722 tex->dirty_level_mask |= level_bit;
2723
2724 if (need_compressed_update)
2725 p_atomic_inc(&rctx->screen->compressed_colortex_counter);
2726 }
2727 tex->separate_dcc_dirty = true;
2728 } else {
2729 /* 128-bit formats are unusupported */
2730 if (tex->surface.bpe > 8) {
2731 continue;
2732 }
2733
2734 /* RB+ doesn't work with CMASK fast clear on Stoney. */
2735 if (rctx->family == CHIP_STONEY)
2736 continue;
2737
2738 /* ensure CMASK is enabled */
2739 r600_texture_alloc_cmask_separate(rctx->screen, tex);
2740 if (tex->cmask.size == 0) {
2741 continue;
2742 }
2743
2744 /* Do the fast clear. */
2745 rctx->clear_buffer(&rctx->b, &tex->cmask_buffer->b.b,
2746 tex->cmask.offset, tex->cmask.size, 0,
2747 R600_COHERENCY_CB_META);
2748
2749 bool need_compressed_update = !tex->dirty_level_mask;
2750
2751 tex->dirty_level_mask |= 1 << fb->cbufs[i]->u.tex.level;
2752
2753 if (need_compressed_update)
2754 p_atomic_inc(&rctx->screen->compressed_colortex_counter);
2755 }
2756
2757 /* We can change the micro tile mode before a full clear. */
2758 si_set_optimal_micro_tile_mode(rctx->screen, tex);
2759
2760 evergreen_set_clear_color(tex, fb->cbufs[i]->format, color);
2761
2762 if (dirty_cbufs)
2763 *dirty_cbufs |= 1 << i;
2764 rctx->set_atom_dirty(rctx, fb_state, true);
2765 *buffers &= ~clear_bit;
2766 }
2767 }
2768
2769 static struct pipe_memory_object *
2770 r600_memobj_from_handle(struct pipe_screen *screen,
2771 struct winsys_handle *whandle,
2772 bool dedicated)
2773 {
2774 struct r600_common_screen *rscreen = (struct r600_common_screen*)screen;
2775 struct r600_memory_object *memobj = CALLOC_STRUCT(r600_memory_object);
2776 struct pb_buffer *buf = NULL;
2777 uint32_t stride, offset;
2778
2779 if (!memobj)
2780 return NULL;
2781
2782 buf = rscreen->ws->buffer_from_handle(rscreen->ws, whandle,
2783 &stride, &offset);
2784 if (!buf) {
2785 free(memobj);
2786 return NULL;
2787 }
2788
2789 memobj->b.dedicated = dedicated;
2790 memobj->buf = buf;
2791 memobj->stride = stride;
2792 memobj->offset = offset;
2793
2794 return (struct pipe_memory_object *)memobj;
2795
2796 }
2797
2798 static void
2799 r600_memobj_destroy(struct pipe_screen *screen,
2800 struct pipe_memory_object *_memobj)
2801 {
2802 struct r600_memory_object *memobj = (struct r600_memory_object *)_memobj;
2803
2804 pb_reference(&memobj->buf, NULL);
2805 free(memobj);
2806 }
2807
2808 static struct pipe_resource *
2809 r600_texture_from_memobj(struct pipe_screen *screen,
2810 const struct pipe_resource *templ,
2811 struct pipe_memory_object *_memobj,
2812 uint64_t offset)
2813 {
2814 int r;
2815 struct r600_common_screen *rscreen = (struct r600_common_screen*)screen;
2816 struct r600_memory_object *memobj = (struct r600_memory_object *)_memobj;
2817 struct r600_texture *rtex;
2818 struct radeon_surf surface = {};
2819 struct radeon_bo_metadata metadata = {};
2820 enum radeon_surf_mode array_mode;
2821 bool is_scanout;
2822 struct pb_buffer *buf = NULL;
2823
2824 if (memobj->b.dedicated) {
2825 rscreen->ws->buffer_get_metadata(memobj->buf, &metadata);
2826 r600_surface_import_metadata(rscreen, &surface, &metadata,
2827 &array_mode, &is_scanout);
2828 } else {
2829 /**
2830 * The bo metadata is unset for un-dedicated images. So we fall
2831 * back to linear. See answer to question 5 of the
2832 * VK_KHX_external_memory spec for some details.
2833 *
2834 * It is possible that this case isn't going to work if the
2835 * surface pitch isn't correctly aligned by default.
2836 *
2837 * In order to support it correctly we require multi-image
2838 * metadata to be syncrhonized between radv and radeonsi. The
2839 * semantics of associating multiple image metadata to a memory
2840 * object on the vulkan export side are not concretely defined
2841 * either.
2842 *
2843 * All the use cases we are aware of at the moment for memory
2844 * objects use dedicated allocations. So lets keep the initial
2845 * implementation simple.
2846 *
2847 * A possible alternative is to attempt to reconstruct the
2848 * tiling information when the TexParameter TEXTURE_TILING_EXT
2849 * is set.
2850 */
2851 array_mode = RADEON_SURF_MODE_LINEAR_ALIGNED;
2852 is_scanout = false;
2853
2854 }
2855
2856 r = r600_init_surface(rscreen, &surface, templ,
2857 array_mode, memobj->stride,
2858 offset, true, is_scanout,
2859 false, false);
2860 if (r)
2861 return NULL;
2862
2863 rtex = r600_texture_create_object(screen, templ, memobj->buf, &surface);
2864 if (!rtex)
2865 return NULL;
2866
2867 /* r600_texture_create_object doesn't increment refcount of
2868 * memobj->buf, so increment it here.
2869 */
2870 pb_reference(&buf, memobj->buf);
2871
2872 rtex->resource.b.is_shared = true;
2873 rtex->resource.external_usage = PIPE_HANDLE_USAGE_READ_WRITE;
2874
2875 if (rscreen->apply_opaque_metadata)
2876 rscreen->apply_opaque_metadata(rscreen, rtex, &metadata);
2877
2878 return &rtex->resource.b.b;
2879 }
2880
2881 static bool si_check_resource_capability(struct pipe_screen *screen,
2882 struct pipe_resource *resource,
2883 unsigned bind)
2884 {
2885 struct r600_texture *tex = (struct r600_texture*)resource;
2886
2887 /* Buffers only support the linear flag. */
2888 if (resource->target == PIPE_BUFFER)
2889 return (bind & ~PIPE_BIND_LINEAR) == 0;
2890
2891 if (bind & PIPE_BIND_LINEAR && !tex->surface.is_linear)
2892 return false;
2893
2894 if (bind & PIPE_BIND_SCANOUT && !tex->surface.is_displayable)
2895 return false;
2896
2897 /* TODO: PIPE_BIND_CURSOR - do we care? */
2898 return true;
2899 }
2900
2901 void si_init_screen_texture_functions(struct r600_common_screen *rscreen)
2902 {
2903 rscreen->b.resource_from_handle = r600_texture_from_handle;
2904 rscreen->b.resource_get_handle = r600_texture_get_handle;
2905 rscreen->b.resource_from_memobj = r600_texture_from_memobj;
2906 rscreen->b.memobj_create_from_handle = r600_memobj_from_handle;
2907 rscreen->b.memobj_destroy = r600_memobj_destroy;
2908 rscreen->b.check_resource_capability = si_check_resource_capability;
2909 }
2910
2911 void si_init_context_texture_functions(struct r600_common_context *rctx)
2912 {
2913 rctx->b.create_surface = r600_create_surface;
2914 rctx->b.surface_destroy = r600_surface_destroy;
2915 rctx->b.clear_texture = r600_clear_texture;
2916 }