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