gallium/radeon: remove DBG_TEXMIP
[mesa.git] / src / gallium / drivers / radeon / r600_texture.c
1 /*
2 * Copyright 2010 Jerome Glisse <glisse@freedesktop.org>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Jerome Glisse
25 * Corbin Simpson
26 */
27 #include "r600_pipe_common.h"
28 #include "r600_cs.h"
29 #include "util/u_format.h"
30 #include "util/u_memory.h"
31 #include "util/u_pack_color.h"
32 #include <errno.h>
33 #include <inttypes.h>
34
35 /* Same as resource_copy_region, except that both upsampling and downsampling are allowed. */
36 static void r600_copy_region_with_blit(struct pipe_context *pipe,
37 struct pipe_resource *dst,
38 unsigned dst_level,
39 unsigned dstx, unsigned dsty, unsigned dstz,
40 struct pipe_resource *src,
41 unsigned src_level,
42 const struct pipe_box *src_box)
43 {
44 struct pipe_blit_info blit;
45
46 memset(&blit, 0, sizeof(blit));
47 blit.src.resource = src;
48 blit.src.format = src->format;
49 blit.src.level = src_level;
50 blit.src.box = *src_box;
51 blit.dst.resource = dst;
52 blit.dst.format = dst->format;
53 blit.dst.level = dst_level;
54 blit.dst.box.x = dstx;
55 blit.dst.box.y = dsty;
56 blit.dst.box.z = dstz;
57 blit.dst.box.width = src_box->width;
58 blit.dst.box.height = src_box->height;
59 blit.dst.box.depth = src_box->depth;
60 blit.mask = util_format_get_mask(src->format) &
61 util_format_get_mask(dst->format);
62 blit.filter = PIPE_TEX_FILTER_NEAREST;
63
64 if (blit.mask) {
65 pipe->blit(pipe, &blit);
66 }
67 }
68
69 /* Copy from a full GPU texture to a transfer's staging one. */
70 static void r600_copy_to_staging_texture(struct pipe_context *ctx, struct r600_transfer *rtransfer)
71 {
72 struct r600_common_context *rctx = (struct r600_common_context*)ctx;
73 struct pipe_transfer *transfer = (struct pipe_transfer*)rtransfer;
74 struct pipe_resource *dst = &rtransfer->staging->b.b;
75 struct pipe_resource *src = transfer->resource;
76
77 if (src->nr_samples > 1) {
78 r600_copy_region_with_blit(ctx, dst, 0, 0, 0, 0,
79 src, transfer->level, &transfer->box);
80 return;
81 }
82
83 rctx->dma_copy(ctx, dst, 0, 0, 0, 0, src, transfer->level,
84 &transfer->box);
85 }
86
87 /* Copy from a transfer's staging texture to a full GPU one. */
88 static void r600_copy_from_staging_texture(struct pipe_context *ctx, struct r600_transfer *rtransfer)
89 {
90 struct r600_common_context *rctx = (struct r600_common_context*)ctx;
91 struct pipe_transfer *transfer = (struct pipe_transfer*)rtransfer;
92 struct pipe_resource *dst = transfer->resource;
93 struct pipe_resource *src = &rtransfer->staging->b.b;
94 struct pipe_box sbox;
95
96 u_box_3d(0, 0, 0, transfer->box.width, transfer->box.height, transfer->box.depth, &sbox);
97
98 if (dst->nr_samples > 1) {
99 r600_copy_region_with_blit(ctx, dst, transfer->level,
100 transfer->box.x, transfer->box.y, transfer->box.z,
101 src, 0, &sbox);
102 return;
103 }
104
105 rctx->dma_copy(ctx, dst, transfer->level,
106 transfer->box.x, transfer->box.y, transfer->box.z,
107 src, 0, &sbox);
108 }
109
110 static unsigned r600_texture_get_offset(struct r600_texture *rtex, unsigned level,
111 const struct pipe_box *box)
112 {
113 enum pipe_format format = rtex->resource.b.b.format;
114
115 return rtex->surface.level[level].offset +
116 box->z * rtex->surface.level[level].slice_size +
117 box->y / util_format_get_blockheight(format) * rtex->surface.level[level].pitch_bytes +
118 box->x / util_format_get_blockwidth(format) * util_format_get_blocksize(format);
119 }
120
121 static int r600_init_surface(struct r600_common_screen *rscreen,
122 struct radeon_surf *surface,
123 const struct pipe_resource *ptex,
124 unsigned array_mode,
125 bool is_flushed_depth)
126 {
127 const struct util_format_description *desc =
128 util_format_description(ptex->format);
129 bool is_depth, is_stencil;
130
131 is_depth = util_format_has_depth(desc);
132 is_stencil = util_format_has_stencil(desc);
133
134 surface->npix_x = ptex->width0;
135 surface->npix_y = ptex->height0;
136 surface->npix_z = ptex->depth0;
137 surface->blk_w = util_format_get_blockwidth(ptex->format);
138 surface->blk_h = util_format_get_blockheight(ptex->format);
139 surface->blk_d = 1;
140 surface->array_size = 1;
141 surface->last_level = ptex->last_level;
142
143 if (rscreen->chip_class >= EVERGREEN && !is_flushed_depth &&
144 ptex->format == PIPE_FORMAT_Z32_FLOAT_S8X24_UINT) {
145 surface->bpe = 4; /* stencil is allocated separately on evergreen */
146 } else {
147 surface->bpe = util_format_get_blocksize(ptex->format);
148 /* align byte per element on dword */
149 if (surface->bpe == 3) {
150 surface->bpe = 4;
151 }
152 }
153
154 surface->nsamples = ptex->nr_samples ? ptex->nr_samples : 1;
155 surface->flags = RADEON_SURF_SET(array_mode, MODE);
156
157 switch (ptex->target) {
158 case PIPE_TEXTURE_1D:
159 surface->flags |= RADEON_SURF_SET(RADEON_SURF_TYPE_1D, TYPE);
160 break;
161 case PIPE_TEXTURE_RECT:
162 case PIPE_TEXTURE_2D:
163 surface->flags |= RADEON_SURF_SET(RADEON_SURF_TYPE_2D, TYPE);
164 break;
165 case PIPE_TEXTURE_3D:
166 surface->flags |= RADEON_SURF_SET(RADEON_SURF_TYPE_3D, TYPE);
167 break;
168 case PIPE_TEXTURE_1D_ARRAY:
169 surface->flags |= RADEON_SURF_SET(RADEON_SURF_TYPE_1D_ARRAY, TYPE);
170 surface->array_size = ptex->array_size;
171 break;
172 case PIPE_TEXTURE_2D_ARRAY:
173 case PIPE_TEXTURE_CUBE_ARRAY: /* cube array layout like 2d array */
174 surface->flags |= RADEON_SURF_SET(RADEON_SURF_TYPE_2D_ARRAY, TYPE);
175 surface->array_size = ptex->array_size;
176 break;
177 case PIPE_TEXTURE_CUBE:
178 surface->flags |= RADEON_SURF_SET(RADEON_SURF_TYPE_CUBEMAP, TYPE);
179 break;
180 case PIPE_BUFFER:
181 default:
182 return -EINVAL;
183 }
184 if (ptex->bind & PIPE_BIND_SCANOUT) {
185 surface->flags |= RADEON_SURF_SCANOUT;
186 }
187
188 if (!is_flushed_depth && is_depth) {
189 surface->flags |= RADEON_SURF_ZBUFFER;
190
191 if (is_stencil) {
192 surface->flags |= RADEON_SURF_SBUFFER |
193 RADEON_SURF_HAS_SBUFFER_MIPTREE;
194 }
195 }
196 if (rscreen->chip_class >= SI) {
197 surface->flags |= RADEON_SURF_HAS_TILE_MODE_INDEX;
198 }
199 return 0;
200 }
201
202 static int r600_setup_surface(struct pipe_screen *screen,
203 struct r600_texture *rtex,
204 unsigned pitch_in_bytes_override)
205 {
206 struct r600_common_screen *rscreen = (struct r600_common_screen*)screen;
207 int r;
208
209 r = rscreen->ws->surface_init(rscreen->ws, &rtex->surface);
210 if (r) {
211 return r;
212 }
213
214 rtex->size = rtex->surface.bo_size;
215
216 if (pitch_in_bytes_override && pitch_in_bytes_override != rtex->surface.level[0].pitch_bytes) {
217 /* old ddx on evergreen over estimate alignment for 1d, only 1 level
218 * for those
219 */
220 rtex->surface.level[0].nblk_x = pitch_in_bytes_override / rtex->surface.bpe;
221 rtex->surface.level[0].pitch_bytes = pitch_in_bytes_override;
222 rtex->surface.level[0].slice_size = pitch_in_bytes_override * rtex->surface.level[0].nblk_y;
223 if (rtex->surface.flags & RADEON_SURF_SBUFFER) {
224 rtex->surface.stencil_offset =
225 rtex->surface.stencil_level[0].offset = rtex->surface.level[0].slice_size;
226 }
227 }
228 return 0;
229 }
230
231 static boolean r600_texture_get_handle(struct pipe_screen* screen,
232 struct pipe_resource *ptex,
233 struct winsys_handle *whandle)
234 {
235 struct r600_texture *rtex = (struct r600_texture*)ptex;
236 struct r600_resource *resource = &rtex->resource;
237 struct radeon_surf *surface = &rtex->surface;
238 struct r600_common_screen *rscreen = (struct r600_common_screen*)screen;
239
240 rscreen->ws->buffer_set_tiling(resource->buf,
241 NULL,
242 surface->level[0].mode >= RADEON_SURF_MODE_1D ?
243 RADEON_LAYOUT_TILED : RADEON_LAYOUT_LINEAR,
244 surface->level[0].mode >= RADEON_SURF_MODE_2D ?
245 RADEON_LAYOUT_TILED : RADEON_LAYOUT_LINEAR,
246 surface->pipe_config,
247 surface->bankw, surface->bankh,
248 surface->tile_split,
249 surface->stencil_tile_split,
250 surface->mtilea, surface->num_banks,
251 surface->level[0].pitch_bytes,
252 (surface->flags & RADEON_SURF_SCANOUT) != 0);
253
254 return rscreen->ws->buffer_get_handle(resource->buf,
255 surface->level[0].pitch_bytes, whandle);
256 }
257
258 static void r600_texture_destroy(struct pipe_screen *screen,
259 struct pipe_resource *ptex)
260 {
261 struct r600_texture *rtex = (struct r600_texture*)ptex;
262 struct r600_resource *resource = &rtex->resource;
263
264 if (rtex->flushed_depth_texture)
265 pipe_resource_reference((struct pipe_resource **)&rtex->flushed_depth_texture, NULL);
266
267 pipe_resource_reference((struct pipe_resource**)&rtex->htile_buffer, NULL);
268 if (rtex->cmask_buffer != &rtex->resource) {
269 pipe_resource_reference((struct pipe_resource**)&rtex->cmask_buffer, NULL);
270 }
271 pipe_resource_reference((struct pipe_resource**)&rtex->dcc_buffer, NULL);
272 pb_reference(&resource->buf, NULL);
273 FREE(rtex);
274 }
275
276 static const struct u_resource_vtbl r600_texture_vtbl;
277
278 /* The number of samples can be specified independently of the texture. */
279 void r600_texture_get_fmask_info(struct r600_common_screen *rscreen,
280 struct r600_texture *rtex,
281 unsigned nr_samples,
282 struct r600_fmask_info *out)
283 {
284 /* FMASK is allocated like an ordinary texture. */
285 struct radeon_surf fmask = rtex->surface;
286
287 memset(out, 0, sizeof(*out));
288
289 fmask.bo_alignment = 0;
290 fmask.bo_size = 0;
291 fmask.nsamples = 1;
292 fmask.flags |= RADEON_SURF_FMASK;
293
294 /* Force 2D tiling if it wasn't set. This may occur when creating
295 * FMASK for MSAA resolve on R6xx. On R6xx, the single-sample
296 * destination buffer must have an FMASK too. */
297 fmask.flags = RADEON_SURF_CLR(fmask.flags, MODE);
298 fmask.flags |= RADEON_SURF_SET(RADEON_SURF_MODE_2D, MODE);
299
300 if (rscreen->chip_class >= SI) {
301 fmask.flags |= RADEON_SURF_HAS_TILE_MODE_INDEX;
302 }
303
304 switch (nr_samples) {
305 case 2:
306 case 4:
307 fmask.bpe = 1;
308 if (rscreen->chip_class <= CAYMAN) {
309 fmask.bankh = 4;
310 }
311 break;
312 case 8:
313 fmask.bpe = 4;
314 break;
315 default:
316 R600_ERR("Invalid sample count for FMASK allocation.\n");
317 return;
318 }
319
320 /* Overallocate FMASK on R600-R700 to fix colorbuffer corruption.
321 * This can be fixed by writing a separate FMASK allocator specifically
322 * for R600-R700 asics. */
323 if (rscreen->chip_class <= R700) {
324 fmask.bpe *= 2;
325 }
326
327 if (rscreen->ws->surface_init(rscreen->ws, &fmask)) {
328 R600_ERR("Got error in surface_init while allocating FMASK.\n");
329 return;
330 }
331
332 assert(fmask.level[0].mode == RADEON_SURF_MODE_2D);
333
334 out->slice_tile_max = (fmask.level[0].nblk_x * fmask.level[0].nblk_y) / 64;
335 if (out->slice_tile_max)
336 out->slice_tile_max -= 1;
337
338 out->tile_mode_index = fmask.tiling_index[0];
339 out->pitch = fmask.level[0].nblk_x;
340 out->bank_height = fmask.bankh;
341 out->alignment = MAX2(256, fmask.bo_alignment);
342 out->size = fmask.bo_size;
343 }
344
345 static void r600_texture_allocate_fmask(struct r600_common_screen *rscreen,
346 struct r600_texture *rtex)
347 {
348 r600_texture_get_fmask_info(rscreen, rtex,
349 rtex->resource.b.b.nr_samples, &rtex->fmask);
350
351 rtex->fmask.offset = align(rtex->size, rtex->fmask.alignment);
352 rtex->size = rtex->fmask.offset + rtex->fmask.size;
353 }
354
355 void r600_texture_get_cmask_info(struct r600_common_screen *rscreen,
356 struct r600_texture *rtex,
357 struct r600_cmask_info *out)
358 {
359 unsigned cmask_tile_width = 8;
360 unsigned cmask_tile_height = 8;
361 unsigned cmask_tile_elements = cmask_tile_width * cmask_tile_height;
362 unsigned element_bits = 4;
363 unsigned cmask_cache_bits = 1024;
364 unsigned num_pipes = rscreen->tiling_info.num_channels;
365 unsigned pipe_interleave_bytes = rscreen->tiling_info.group_bytes;
366
367 unsigned elements_per_macro_tile = (cmask_cache_bits / element_bits) * num_pipes;
368 unsigned pixels_per_macro_tile = elements_per_macro_tile * cmask_tile_elements;
369 unsigned sqrt_pixels_per_macro_tile = sqrt(pixels_per_macro_tile);
370 unsigned macro_tile_width = util_next_power_of_two(sqrt_pixels_per_macro_tile);
371 unsigned macro_tile_height = pixels_per_macro_tile / macro_tile_width;
372
373 unsigned pitch_elements = align(rtex->surface.npix_x, macro_tile_width);
374 unsigned height = align(rtex->surface.npix_y, macro_tile_height);
375
376 unsigned base_align = num_pipes * pipe_interleave_bytes;
377 unsigned slice_bytes =
378 ((pitch_elements * height * element_bits + 7) / 8) / cmask_tile_elements;
379
380 assert(macro_tile_width % 128 == 0);
381 assert(macro_tile_height % 128 == 0);
382
383 out->slice_tile_max = ((pitch_elements * height) / (128*128)) - 1;
384 out->alignment = MAX2(256, base_align);
385 out->size = (util_max_layer(&rtex->resource.b.b, 0) + 1) *
386 align(slice_bytes, base_align);
387 }
388
389 static void si_texture_get_cmask_info(struct r600_common_screen *rscreen,
390 struct r600_texture *rtex,
391 struct r600_cmask_info *out)
392 {
393 unsigned pipe_interleave_bytes = rscreen->tiling_info.group_bytes;
394 unsigned num_pipes = rscreen->tiling_info.num_channels;
395 unsigned cl_width, cl_height;
396
397 switch (num_pipes) {
398 case 2:
399 cl_width = 32;
400 cl_height = 16;
401 break;
402 case 4:
403 cl_width = 32;
404 cl_height = 32;
405 break;
406 case 8:
407 cl_width = 64;
408 cl_height = 32;
409 break;
410 case 16: /* Hawaii */
411 cl_width = 64;
412 cl_height = 64;
413 break;
414 default:
415 assert(0);
416 return;
417 }
418
419 unsigned base_align = num_pipes * pipe_interleave_bytes;
420
421 unsigned width = align(rtex->surface.npix_x, cl_width*8);
422 unsigned height = align(rtex->surface.npix_y, cl_height*8);
423 unsigned slice_elements = (width * height) / (8*8);
424
425 /* Each element of CMASK is a nibble. */
426 unsigned slice_bytes = slice_elements / 2;
427
428 out->slice_tile_max = (width * height) / (128*128);
429 if (out->slice_tile_max)
430 out->slice_tile_max -= 1;
431
432 out->alignment = MAX2(256, base_align);
433 out->size = (util_max_layer(&rtex->resource.b.b, 0) + 1) *
434 align(slice_bytes, base_align);
435 }
436
437 static void r600_texture_allocate_cmask(struct r600_common_screen *rscreen,
438 struct r600_texture *rtex)
439 {
440 if (rscreen->chip_class >= SI) {
441 si_texture_get_cmask_info(rscreen, rtex, &rtex->cmask);
442 } else {
443 r600_texture_get_cmask_info(rscreen, rtex, &rtex->cmask);
444 }
445
446 rtex->cmask.offset = align(rtex->size, rtex->cmask.alignment);
447 rtex->size = rtex->cmask.offset + rtex->cmask.size;
448
449 if (rscreen->chip_class >= SI)
450 rtex->cb_color_info |= SI_S_028C70_FAST_CLEAR(1);
451 else
452 rtex->cb_color_info |= EG_S_028C70_FAST_CLEAR(1);
453 }
454
455 static void r600_texture_alloc_cmask_separate(struct r600_common_screen *rscreen,
456 struct r600_texture *rtex)
457 {
458 if (rtex->cmask_buffer)
459 return;
460
461 assert(rtex->cmask.size == 0);
462
463 if (rscreen->chip_class >= SI) {
464 si_texture_get_cmask_info(rscreen, rtex, &rtex->cmask);
465 } else {
466 r600_texture_get_cmask_info(rscreen, rtex, &rtex->cmask);
467 }
468
469 rtex->cmask_buffer = (struct r600_resource *)
470 pipe_buffer_create(&rscreen->b, PIPE_BIND_CUSTOM,
471 PIPE_USAGE_DEFAULT, rtex->cmask.size);
472 if (rtex->cmask_buffer == NULL) {
473 rtex->cmask.size = 0;
474 return;
475 }
476
477 /* update colorbuffer state bits */
478 rtex->cmask.base_address_reg = rtex->cmask_buffer->gpu_address >> 8;
479
480 if (rscreen->chip_class >= SI)
481 rtex->cb_color_info |= SI_S_028C70_FAST_CLEAR(1);
482 else
483 rtex->cb_color_info |= EG_S_028C70_FAST_CLEAR(1);
484 }
485
486 static void vi_texture_alloc_dcc_separate(struct r600_common_screen *rscreen,
487 struct r600_texture *rtex)
488 {
489 if (rscreen->debug_flags & DBG_NO_DCC)
490 return;
491
492 rtex->dcc_buffer = (struct r600_resource *)
493 r600_aligned_buffer_create(&rscreen->b, PIPE_BIND_CUSTOM,
494 PIPE_USAGE_DEFAULT, rtex->surface.dcc_size, rtex->surface.dcc_alignment);
495 if (rtex->dcc_buffer == NULL) {
496 return;
497 }
498
499 r600_screen_clear_buffer(rscreen, &rtex->dcc_buffer->b.b, 0, rtex->surface.dcc_size,
500 0xFFFFFFFF, true);
501
502 rtex->cb_color_info |= VI_S_028C70_DCC_ENABLE(1);
503 }
504
505 static unsigned r600_texture_get_htile_size(struct r600_common_screen *rscreen,
506 struct r600_texture *rtex)
507 {
508 unsigned cl_width, cl_height, width, height;
509 unsigned slice_elements, slice_bytes, pipe_interleave_bytes, base_align;
510 unsigned num_pipes = rscreen->tiling_info.num_channels;
511
512 if (rscreen->chip_class <= EVERGREEN &&
513 rscreen->info.drm_major == 2 && rscreen->info.drm_minor < 26)
514 return 0;
515
516 /* HW bug on R6xx. */
517 if (rscreen->chip_class == R600 &&
518 (rtex->surface.level[0].npix_x > 7680 ||
519 rtex->surface.level[0].npix_y > 7680))
520 return 0;
521
522 /* HTILE is broken with 1D tiling on old kernels and CIK. */
523 if (rscreen->chip_class >= CIK &&
524 rtex->surface.level[0].mode == RADEON_SURF_MODE_1D &&
525 rscreen->info.drm_major == 2 && rscreen->info.drm_minor < 38)
526 return 0;
527
528 switch (num_pipes) {
529 case 1:
530 cl_width = 32;
531 cl_height = 16;
532 break;
533 case 2:
534 cl_width = 32;
535 cl_height = 32;
536 break;
537 case 4:
538 cl_width = 64;
539 cl_height = 32;
540 break;
541 case 8:
542 cl_width = 64;
543 cl_height = 64;
544 break;
545 case 16:
546 cl_width = 128;
547 cl_height = 64;
548 break;
549 default:
550 assert(0);
551 return 0;
552 }
553
554 width = align(rtex->surface.npix_x, cl_width * 8);
555 height = align(rtex->surface.npix_y, cl_height * 8);
556
557 slice_elements = (width * height) / (8 * 8);
558 slice_bytes = slice_elements * 4;
559
560 pipe_interleave_bytes = rscreen->tiling_info.group_bytes;
561 base_align = num_pipes * pipe_interleave_bytes;
562
563 return (util_max_layer(&rtex->resource.b.b, 0) + 1) *
564 align(slice_bytes, base_align);
565 }
566
567 static void r600_texture_allocate_htile(struct r600_common_screen *rscreen,
568 struct r600_texture *rtex)
569 {
570 unsigned htile_size = r600_texture_get_htile_size(rscreen, rtex);
571
572 if (!htile_size)
573 return;
574
575 rtex->htile_buffer = (struct r600_resource*)
576 pipe_buffer_create(&rscreen->b, PIPE_BIND_CUSTOM,
577 PIPE_USAGE_DEFAULT, htile_size);
578 if (rtex->htile_buffer == NULL) {
579 /* this is not a fatal error as we can still keep rendering
580 * without htile buffer */
581 R600_ERR("Failed to create buffer object for htile buffer.\n");
582 } else {
583 r600_screen_clear_buffer(rscreen, &rtex->htile_buffer->b.b, 0,
584 htile_size, 0, true);
585 }
586 }
587
588 /* Common processing for r600_texture_create and r600_texture_from_handle */
589 static struct r600_texture *
590 r600_texture_create_object(struct pipe_screen *screen,
591 const struct pipe_resource *base,
592 unsigned pitch_in_bytes_override,
593 struct pb_buffer *buf,
594 struct radeon_surf *surface)
595 {
596 struct r600_texture *rtex;
597 struct r600_resource *resource;
598 struct r600_common_screen *rscreen = (struct r600_common_screen*)screen;
599
600 rtex = CALLOC_STRUCT(r600_texture);
601 if (rtex == NULL)
602 return NULL;
603
604 resource = &rtex->resource;
605 resource->b.b = *base;
606 resource->b.vtbl = &r600_texture_vtbl;
607 pipe_reference_init(&resource->b.b.reference, 1);
608 resource->b.b.screen = screen;
609 rtex->pitch_override = pitch_in_bytes_override;
610
611 /* don't include stencil-only formats which we don't support for rendering */
612 rtex->is_depth = util_format_has_depth(util_format_description(rtex->resource.b.b.format));
613
614 rtex->surface = *surface;
615 if (r600_setup_surface(screen, rtex, pitch_in_bytes_override)) {
616 FREE(rtex);
617 return NULL;
618 }
619
620 /* Tiled depth textures utilize the non-displayable tile order.
621 * This must be done after r600_setup_surface.
622 * Applies to R600-Cayman. */
623 rtex->non_disp_tiling = rtex->is_depth && rtex->surface.level[0].mode >= RADEON_SURF_MODE_1D;
624
625 if (rtex->is_depth) {
626 if (!(base->flags & (R600_RESOURCE_FLAG_TRANSFER |
627 R600_RESOURCE_FLAG_FLUSHED_DEPTH)) &&
628 !(rscreen->debug_flags & DBG_NO_HYPERZ)) {
629
630 r600_texture_allocate_htile(rscreen, rtex);
631 }
632 } else {
633 if (base->nr_samples > 1) {
634 if (!buf) {
635 r600_texture_allocate_fmask(rscreen, rtex);
636 r600_texture_allocate_cmask(rscreen, rtex);
637 rtex->cmask_buffer = &rtex->resource;
638 }
639 if (!rtex->fmask.size || !rtex->cmask.size) {
640 FREE(rtex);
641 return NULL;
642 }
643 }
644 if (rtex->surface.dcc_size)
645 vi_texture_alloc_dcc_separate(rscreen, rtex);
646 }
647
648 /* Now create the backing buffer. */
649 if (!buf) {
650 if (!r600_init_resource(rscreen, resource, rtex->size,
651 rtex->surface.bo_alignment, TRUE)) {
652 FREE(rtex);
653 return NULL;
654 }
655 } else {
656 resource->buf = buf;
657 resource->cs_buf = rscreen->ws->buffer_get_cs_handle(buf);
658 resource->gpu_address = rscreen->ws->buffer_get_virtual_address(resource->cs_buf);
659 resource->domains = rscreen->ws->buffer_get_initial_domain(resource->cs_buf);
660 }
661
662 if (rtex->cmask.size) {
663 /* Initialize the cmask to 0xCC (= compressed state). */
664 r600_screen_clear_buffer(rscreen, &rtex->cmask_buffer->b.b,
665 rtex->cmask.offset, rtex->cmask.size,
666 0xCCCCCCCC, true);
667 }
668
669 /* Initialize the CMASK base register value. */
670 rtex->cmask.base_address_reg =
671 (rtex->resource.gpu_address + rtex->cmask.offset) >> 8;
672
673 if (rscreen->debug_flags & DBG_VM) {
674 fprintf(stderr, "VM start=0x%"PRIX64" end=0x%"PRIX64" | Texture %ix%ix%i, %i levels, %i samples, %s\n",
675 rtex->resource.gpu_address,
676 rtex->resource.gpu_address + rtex->resource.buf->size,
677 base->width0, base->height0, util_max_layer(base, 0)+1, base->last_level+1,
678 base->nr_samples ? base->nr_samples : 1, util_format_short_name(base->format));
679 }
680
681 if (rscreen->debug_flags & DBG_TEX) {
682 printf("Texture: npix_x=%u, npix_y=%u, npix_z=%u, blk_w=%u, "
683 "blk_h=%u, blk_d=%u, array_size=%u, last_level=%u, "
684 "bpe=%u, nsamples=%u, flags=0x%x, %s\n",
685 rtex->surface.npix_x, rtex->surface.npix_y,
686 rtex->surface.npix_z, rtex->surface.blk_w,
687 rtex->surface.blk_h, rtex->surface.blk_d,
688 rtex->surface.array_size, rtex->surface.last_level,
689 rtex->surface.bpe, rtex->surface.nsamples,
690 rtex->surface.flags, util_format_short_name(base->format));
691 for (int i = 0; i <= rtex->surface.last_level; i++) {
692 printf(" L %i: offset=%"PRIu64", slice_size=%"PRIu64", npix_x=%u, "
693 "npix_y=%u, npix_z=%u, nblk_x=%u, nblk_y=%u, "
694 "nblk_z=%u, pitch_bytes=%u, mode=%u\n",
695 i, rtex->surface.level[i].offset,
696 rtex->surface.level[i].slice_size,
697 u_minify(rtex->resource.b.b.width0, i),
698 u_minify(rtex->resource.b.b.height0, i),
699 u_minify(rtex->resource.b.b.depth0, i),
700 rtex->surface.level[i].nblk_x,
701 rtex->surface.level[i].nblk_y,
702 rtex->surface.level[i].nblk_z,
703 rtex->surface.level[i].pitch_bytes,
704 rtex->surface.level[i].mode);
705 }
706 if (rtex->surface.flags & RADEON_SURF_SBUFFER) {
707 for (int i = 0; i <= rtex->surface.last_level; i++) {
708 printf(" S %i: offset=%"PRIu64", slice_size=%"PRIu64", npix_x=%u, "
709 "npix_y=%u, npix_z=%u, nblk_x=%u, nblk_y=%u, "
710 "nblk_z=%u, pitch_bytes=%u, mode=%u\n",
711 i, rtex->surface.stencil_level[i].offset,
712 rtex->surface.stencil_level[i].slice_size,
713 u_minify(rtex->resource.b.b.width0, i),
714 u_minify(rtex->resource.b.b.height0, i),
715 u_minify(rtex->resource.b.b.depth0, i),
716 rtex->surface.stencil_level[i].nblk_x,
717 rtex->surface.stencil_level[i].nblk_y,
718 rtex->surface.stencil_level[i].nblk_z,
719 rtex->surface.stencil_level[i].pitch_bytes,
720 rtex->surface.stencil_level[i].mode);
721 }
722 }
723 }
724 return rtex;
725 }
726
727 static unsigned r600_choose_tiling(struct r600_common_screen *rscreen,
728 const struct pipe_resource *templ)
729 {
730 const struct util_format_description *desc = util_format_description(templ->format);
731 bool force_tiling = templ->flags & R600_RESOURCE_FLAG_FORCE_TILING;
732
733 /* MSAA resources must be 2D tiled. */
734 if (templ->nr_samples > 1)
735 return RADEON_SURF_MODE_2D;
736
737 /* Transfer resources should be linear. */
738 if (templ->flags & R600_RESOURCE_FLAG_TRANSFER)
739 return RADEON_SURF_MODE_LINEAR_ALIGNED;
740
741 /* r600g: force tiling on TEXTURE_2D and TEXTURE_3D compute resources. */
742 if (rscreen->chip_class >= R600 && rscreen->chip_class <= CAYMAN &&
743 (templ->bind & PIPE_BIND_COMPUTE_RESOURCE) &&
744 (templ->target == PIPE_TEXTURE_2D ||
745 templ->target == PIPE_TEXTURE_3D))
746 force_tiling = true;
747
748 /* Handle common candidates for the linear mode.
749 * Compressed textures must always be tiled. */
750 if (!force_tiling && !util_format_is_compressed(templ->format)) {
751 /* Not everything can be linear, so we cannot enforce it
752 * for all textures. */
753 if ((rscreen->debug_flags & DBG_NO_TILING) &&
754 (!util_format_is_depth_or_stencil(templ->format) ||
755 !(templ->flags & R600_RESOURCE_FLAG_FLUSHED_DEPTH)))
756 return RADEON_SURF_MODE_LINEAR_ALIGNED;
757
758 /* Tiling doesn't work with the 422 (SUBSAMPLED) formats on R600+. */
759 if (desc->layout == UTIL_FORMAT_LAYOUT_SUBSAMPLED)
760 return RADEON_SURF_MODE_LINEAR_ALIGNED;
761
762 /* Cursors are linear on SI.
763 * (XXX double-check, maybe also use RADEON_SURF_SCANOUT) */
764 if (rscreen->chip_class >= SI &&
765 (templ->bind & PIPE_BIND_CURSOR))
766 return RADEON_SURF_MODE_LINEAR_ALIGNED;
767
768 if (templ->bind & PIPE_BIND_LINEAR)
769 return RADEON_SURF_MODE_LINEAR_ALIGNED;
770
771 /* Textures with a very small height are recommended to be linear. */
772 if (templ->target == PIPE_TEXTURE_1D ||
773 templ->target == PIPE_TEXTURE_1D_ARRAY ||
774 templ->height0 <= 4)
775 return RADEON_SURF_MODE_LINEAR_ALIGNED;
776
777 /* Textures likely to be mapped often. */
778 if (templ->usage == PIPE_USAGE_STAGING ||
779 templ->usage == PIPE_USAGE_STREAM)
780 return RADEON_SURF_MODE_LINEAR_ALIGNED;
781 }
782
783 /* Make small textures 1D tiled. */
784 if (templ->width0 <= 16 || templ->height0 <= 16 ||
785 (rscreen->debug_flags & DBG_NO_2D_TILING))
786 return RADEON_SURF_MODE_1D;
787
788 /* The allocator will switch to 1D if needed. */
789 return RADEON_SURF_MODE_2D;
790 }
791
792 struct pipe_resource *r600_texture_create(struct pipe_screen *screen,
793 const struct pipe_resource *templ)
794 {
795 struct r600_common_screen *rscreen = (struct r600_common_screen*)screen;
796 struct radeon_surf surface = {0};
797 int r;
798
799 r = r600_init_surface(rscreen, &surface, templ,
800 r600_choose_tiling(rscreen, templ),
801 templ->flags & R600_RESOURCE_FLAG_FLUSHED_DEPTH);
802 if (r) {
803 return NULL;
804 }
805 r = rscreen->ws->surface_best(rscreen->ws, &surface);
806 if (r) {
807 return NULL;
808 }
809 return (struct pipe_resource *)r600_texture_create_object(screen, templ,
810 0, NULL, &surface);
811 }
812
813 static struct pipe_resource *r600_texture_from_handle(struct pipe_screen *screen,
814 const struct pipe_resource *templ,
815 struct winsys_handle *whandle)
816 {
817 struct r600_common_screen *rscreen = (struct r600_common_screen*)screen;
818 struct pb_buffer *buf = NULL;
819 unsigned stride = 0;
820 unsigned array_mode;
821 enum radeon_bo_layout micro, macro;
822 struct radeon_surf surface;
823 bool scanout;
824 int r;
825
826 /* Support only 2D textures without mipmaps */
827 if ((templ->target != PIPE_TEXTURE_2D && templ->target != PIPE_TEXTURE_RECT) ||
828 templ->depth0 != 1 || templ->last_level != 0)
829 return NULL;
830
831 buf = rscreen->ws->buffer_from_handle(rscreen->ws, whandle, &stride);
832 if (!buf)
833 return NULL;
834
835 rscreen->ws->buffer_get_tiling(buf, &micro, &macro,
836 &surface.bankw, &surface.bankh,
837 &surface.tile_split,
838 &surface.stencil_tile_split,
839 &surface.mtilea, &scanout);
840
841 if (macro == RADEON_LAYOUT_TILED)
842 array_mode = RADEON_SURF_MODE_2D;
843 else if (micro == RADEON_LAYOUT_TILED)
844 array_mode = RADEON_SURF_MODE_1D;
845 else
846 array_mode = RADEON_SURF_MODE_LINEAR_ALIGNED;
847
848 r = r600_init_surface(rscreen, &surface, templ, array_mode, false);
849 if (r) {
850 return NULL;
851 }
852
853 if (scanout)
854 surface.flags |= RADEON_SURF_SCANOUT;
855
856 return (struct pipe_resource *)r600_texture_create_object(screen, templ,
857 stride, buf, &surface);
858 }
859
860 bool r600_init_flushed_depth_texture(struct pipe_context *ctx,
861 struct pipe_resource *texture,
862 struct r600_texture **staging)
863 {
864 struct r600_texture *rtex = (struct r600_texture*)texture;
865 struct pipe_resource resource;
866 struct r600_texture **flushed_depth_texture = staging ?
867 staging : &rtex->flushed_depth_texture;
868
869 if (!staging && rtex->flushed_depth_texture)
870 return true; /* it's ready */
871
872 resource.target = texture->target;
873 resource.format = texture->format;
874 resource.width0 = texture->width0;
875 resource.height0 = texture->height0;
876 resource.depth0 = texture->depth0;
877 resource.array_size = texture->array_size;
878 resource.last_level = texture->last_level;
879 resource.nr_samples = texture->nr_samples;
880 resource.usage = staging ? PIPE_USAGE_STAGING : PIPE_USAGE_DEFAULT;
881 resource.bind = texture->bind & ~PIPE_BIND_DEPTH_STENCIL;
882 resource.flags = texture->flags | R600_RESOURCE_FLAG_FLUSHED_DEPTH;
883
884 if (staging)
885 resource.flags |= R600_RESOURCE_FLAG_TRANSFER;
886
887 *flushed_depth_texture = (struct r600_texture *)ctx->screen->resource_create(ctx->screen, &resource);
888 if (*flushed_depth_texture == NULL) {
889 R600_ERR("failed to create temporary texture to hold flushed depth\n");
890 return false;
891 }
892
893 (*flushed_depth_texture)->is_flushing_texture = TRUE;
894 (*flushed_depth_texture)->non_disp_tiling = false;
895 return true;
896 }
897
898 /**
899 * Initialize the pipe_resource descriptor to be of the same size as the box,
900 * which is supposed to hold a subregion of the texture "orig" at the given
901 * mipmap level.
902 */
903 static void r600_init_temp_resource_from_box(struct pipe_resource *res,
904 struct pipe_resource *orig,
905 const struct pipe_box *box,
906 unsigned level, unsigned flags)
907 {
908 memset(res, 0, sizeof(*res));
909 res->format = orig->format;
910 res->width0 = box->width;
911 res->height0 = box->height;
912 res->depth0 = 1;
913 res->array_size = 1;
914 res->usage = flags & R600_RESOURCE_FLAG_TRANSFER ? PIPE_USAGE_STAGING : PIPE_USAGE_DEFAULT;
915 res->flags = flags;
916
917 /* We must set the correct texture target and dimensions for a 3D box. */
918 if (box->depth > 1 && util_max_layer(orig, level) > 0)
919 res->target = orig->target;
920 else
921 res->target = PIPE_TEXTURE_2D;
922
923 switch (res->target) {
924 case PIPE_TEXTURE_1D_ARRAY:
925 case PIPE_TEXTURE_2D_ARRAY:
926 case PIPE_TEXTURE_CUBE_ARRAY:
927 res->array_size = box->depth;
928 break;
929 case PIPE_TEXTURE_3D:
930 res->depth0 = box->depth;
931 break;
932 default:;
933 }
934 }
935
936 static void *r600_texture_transfer_map(struct pipe_context *ctx,
937 struct pipe_resource *texture,
938 unsigned level,
939 unsigned usage,
940 const struct pipe_box *box,
941 struct pipe_transfer **ptransfer)
942 {
943 struct r600_common_context *rctx = (struct r600_common_context*)ctx;
944 struct r600_texture *rtex = (struct r600_texture*)texture;
945 struct r600_transfer *trans;
946 boolean use_staging_texture = FALSE;
947 struct r600_resource *buf;
948 unsigned offset = 0;
949 char *map;
950
951 /* We cannot map a tiled texture directly because the data is
952 * in a different order, therefore we do detiling using a blit.
953 *
954 * Also, use a temporary in GTT memory for read transfers, as
955 * the CPU is much happier reading out of cached system memory
956 * than uncached VRAM.
957 */
958 if (rtex->surface.level[0].mode >= RADEON_SURF_MODE_1D) {
959 use_staging_texture = TRUE;
960 } else if ((usage & PIPE_TRANSFER_READ) && !(usage & PIPE_TRANSFER_MAP_DIRECTLY) &&
961 (rtex->resource.domains == RADEON_DOMAIN_VRAM)) {
962 /* Untiled buffers in VRAM, which is slow for CPU reads */
963 use_staging_texture = TRUE;
964 } else if (!(usage & PIPE_TRANSFER_READ) &&
965 (r600_rings_is_buffer_referenced(rctx, rtex->resource.cs_buf, RADEON_USAGE_READWRITE) ||
966 !rctx->ws->buffer_wait(rtex->resource.buf, 0, RADEON_USAGE_READWRITE))) {
967 /* Use a staging texture for uploads if the underlying BO is busy. */
968 use_staging_texture = TRUE;
969 }
970
971 if (texture->flags & R600_RESOURCE_FLAG_TRANSFER) {
972 use_staging_texture = FALSE;
973 }
974
975 if (use_staging_texture && (usage & PIPE_TRANSFER_MAP_DIRECTLY)) {
976 return NULL;
977 }
978
979 trans = CALLOC_STRUCT(r600_transfer);
980 if (trans == NULL)
981 return NULL;
982 trans->transfer.resource = texture;
983 trans->transfer.level = level;
984 trans->transfer.usage = usage;
985 trans->transfer.box = *box;
986
987 if (rtex->is_depth) {
988 struct r600_texture *staging_depth;
989
990 if (rtex->resource.b.b.nr_samples > 1) {
991 /* MSAA depth buffers need to be converted to single sample buffers.
992 *
993 * Mapping MSAA depth buffers can occur if ReadPixels is called
994 * with a multisample GLX visual.
995 *
996 * First downsample the depth buffer to a temporary texture,
997 * then decompress the temporary one to staging.
998 *
999 * Only the region being mapped is transfered.
1000 */
1001 struct pipe_resource resource;
1002
1003 r600_init_temp_resource_from_box(&resource, texture, box, level, 0);
1004
1005 if (!r600_init_flushed_depth_texture(ctx, &resource, &staging_depth)) {
1006 R600_ERR("failed to create temporary texture to hold untiled copy\n");
1007 FREE(trans);
1008 return NULL;
1009 }
1010
1011 if (usage & PIPE_TRANSFER_READ) {
1012 struct pipe_resource *temp = ctx->screen->resource_create(ctx->screen, &resource);
1013 if (!temp) {
1014 R600_ERR("failed to create a temporary depth texture\n");
1015 FREE(trans);
1016 return NULL;
1017 }
1018
1019 r600_copy_region_with_blit(ctx, temp, 0, 0, 0, 0, texture, level, box);
1020 rctx->blit_decompress_depth(ctx, (struct r600_texture*)temp, staging_depth,
1021 0, 0, 0, box->depth, 0, 0);
1022 pipe_resource_reference((struct pipe_resource**)&temp, NULL);
1023 }
1024 }
1025 else {
1026 /* XXX: only readback the rectangle which is being mapped? */
1027 /* XXX: when discard is true, no need to read back from depth texture */
1028 if (!r600_init_flushed_depth_texture(ctx, texture, &staging_depth)) {
1029 R600_ERR("failed to create temporary texture to hold untiled copy\n");
1030 FREE(trans);
1031 return NULL;
1032 }
1033
1034 rctx->blit_decompress_depth(ctx, rtex, staging_depth,
1035 level, level,
1036 box->z, box->z + box->depth - 1,
1037 0, 0);
1038
1039 offset = r600_texture_get_offset(staging_depth, level, box);
1040 }
1041
1042 trans->transfer.stride = staging_depth->surface.level[level].pitch_bytes;
1043 trans->transfer.layer_stride = staging_depth->surface.level[level].slice_size;
1044 trans->staging = (struct r600_resource*)staging_depth;
1045 } else if (use_staging_texture) {
1046 struct pipe_resource resource;
1047 struct r600_texture *staging;
1048
1049 r600_init_temp_resource_from_box(&resource, texture, box, level,
1050 R600_RESOURCE_FLAG_TRANSFER);
1051 resource.usage = (usage & PIPE_TRANSFER_READ) ?
1052 PIPE_USAGE_STAGING : PIPE_USAGE_STREAM;
1053
1054 /* Create the temporary texture. */
1055 staging = (struct r600_texture*)ctx->screen->resource_create(ctx->screen, &resource);
1056 if (staging == NULL) {
1057 R600_ERR("failed to create temporary texture to hold untiled copy\n");
1058 FREE(trans);
1059 return NULL;
1060 }
1061 trans->staging = &staging->resource;
1062 trans->transfer.stride = staging->surface.level[0].pitch_bytes;
1063 trans->transfer.layer_stride = staging->surface.level[0].slice_size;
1064 if (usage & PIPE_TRANSFER_READ) {
1065 r600_copy_to_staging_texture(ctx, trans);
1066 }
1067 } else {
1068 /* the resource is mapped directly */
1069 trans->transfer.stride = rtex->surface.level[level].pitch_bytes;
1070 trans->transfer.layer_stride = rtex->surface.level[level].slice_size;
1071 offset = r600_texture_get_offset(rtex, level, box);
1072 }
1073
1074 if (trans->staging) {
1075 buf = trans->staging;
1076 if (!rtex->is_depth && !(usage & PIPE_TRANSFER_READ))
1077 usage |= PIPE_TRANSFER_UNSYNCHRONIZED;
1078 } else {
1079 buf = &rtex->resource;
1080 }
1081
1082 if (!(map = r600_buffer_map_sync_with_rings(rctx, buf, usage))) {
1083 pipe_resource_reference((struct pipe_resource**)&trans->staging, NULL);
1084 FREE(trans);
1085 return NULL;
1086 }
1087
1088 *ptransfer = &trans->transfer;
1089 return map + offset;
1090 }
1091
1092 static void r600_texture_transfer_unmap(struct pipe_context *ctx,
1093 struct pipe_transfer* transfer)
1094 {
1095 struct r600_transfer *rtransfer = (struct r600_transfer*)transfer;
1096 struct pipe_resource *texture = transfer->resource;
1097 struct r600_texture *rtex = (struct r600_texture*)texture;
1098
1099 if ((transfer->usage & PIPE_TRANSFER_WRITE) && rtransfer->staging) {
1100 if (rtex->is_depth && rtex->resource.b.b.nr_samples <= 1) {
1101 ctx->resource_copy_region(ctx, texture, transfer->level,
1102 transfer->box.x, transfer->box.y, transfer->box.z,
1103 &rtransfer->staging->b.b, transfer->level,
1104 &transfer->box);
1105 } else {
1106 r600_copy_from_staging_texture(ctx, rtransfer);
1107 }
1108 }
1109
1110 if (rtransfer->staging)
1111 pipe_resource_reference((struct pipe_resource**)&rtransfer->staging, NULL);
1112
1113 FREE(transfer);
1114 }
1115
1116 static const struct u_resource_vtbl r600_texture_vtbl =
1117 {
1118 NULL, /* get_handle */
1119 r600_texture_destroy, /* resource_destroy */
1120 r600_texture_transfer_map, /* transfer_map */
1121 u_default_transfer_flush_region, /* transfer_flush_region */
1122 r600_texture_transfer_unmap, /* transfer_unmap */
1123 NULL /* transfer_inline_write */
1124 };
1125
1126 struct pipe_surface *r600_create_surface_custom(struct pipe_context *pipe,
1127 struct pipe_resource *texture,
1128 const struct pipe_surface *templ,
1129 unsigned width, unsigned height)
1130 {
1131 struct r600_surface *surface = CALLOC_STRUCT(r600_surface);
1132
1133 if (surface == NULL)
1134 return NULL;
1135
1136 assert(templ->u.tex.first_layer <= util_max_layer(texture, templ->u.tex.level));
1137 assert(templ->u.tex.last_layer <= util_max_layer(texture, templ->u.tex.level));
1138
1139 pipe_reference_init(&surface->base.reference, 1);
1140 pipe_resource_reference(&surface->base.texture, texture);
1141 surface->base.context = pipe;
1142 surface->base.format = templ->format;
1143 surface->base.width = width;
1144 surface->base.height = height;
1145 surface->base.u = templ->u;
1146 return &surface->base;
1147 }
1148
1149 static struct pipe_surface *r600_create_surface(struct pipe_context *pipe,
1150 struct pipe_resource *tex,
1151 const struct pipe_surface *templ)
1152 {
1153 unsigned level = templ->u.tex.level;
1154
1155 return r600_create_surface_custom(pipe, tex, templ,
1156 u_minify(tex->width0, level),
1157 u_minify(tex->height0, level));
1158 }
1159
1160 static void r600_surface_destroy(struct pipe_context *pipe,
1161 struct pipe_surface *surface)
1162 {
1163 struct r600_surface *surf = (struct r600_surface*)surface;
1164 pipe_resource_reference((struct pipe_resource**)&surf->cb_buffer_fmask, NULL);
1165 pipe_resource_reference((struct pipe_resource**)&surf->cb_buffer_cmask, NULL);
1166 pipe_resource_reference(&surface->texture, NULL);
1167 FREE(surface);
1168 }
1169
1170 unsigned r600_translate_colorswap(enum pipe_format format)
1171 {
1172 const struct util_format_description *desc = util_format_description(format);
1173
1174 #define HAS_SWIZZLE(chan,swz) (desc->swizzle[chan] == UTIL_FORMAT_SWIZZLE_##swz)
1175
1176 if (format == PIPE_FORMAT_R11G11B10_FLOAT) /* isn't plain */
1177 return V_0280A0_SWAP_STD;
1178
1179 if (desc->layout != UTIL_FORMAT_LAYOUT_PLAIN)
1180 return ~0U;
1181
1182 switch (desc->nr_channels) {
1183 case 1:
1184 if (HAS_SWIZZLE(0,X))
1185 return V_0280A0_SWAP_STD; /* X___ */
1186 else if (HAS_SWIZZLE(3,X))
1187 return V_0280A0_SWAP_ALT_REV; /* ___X */
1188 break;
1189 case 2:
1190 if ((HAS_SWIZZLE(0,X) && HAS_SWIZZLE(1,Y)) ||
1191 (HAS_SWIZZLE(0,X) && HAS_SWIZZLE(1,NONE)) ||
1192 (HAS_SWIZZLE(0,NONE) && HAS_SWIZZLE(1,Y)))
1193 return V_0280A0_SWAP_STD; /* XY__ */
1194 else if ((HAS_SWIZZLE(0,Y) && HAS_SWIZZLE(1,X)) ||
1195 (HAS_SWIZZLE(0,Y) && HAS_SWIZZLE(1,NONE)) ||
1196 (HAS_SWIZZLE(0,NONE) && HAS_SWIZZLE(1,X)))
1197 return V_0280A0_SWAP_STD_REV; /* YX__ */
1198 else if (HAS_SWIZZLE(0,X) && HAS_SWIZZLE(3,Y))
1199 return V_0280A0_SWAP_ALT; /* X__Y */
1200 else if (HAS_SWIZZLE(0,Y) && HAS_SWIZZLE(3,X))
1201 return V_0280A0_SWAP_ALT_REV; /* Y__X */
1202 break;
1203 case 3:
1204 if (HAS_SWIZZLE(0,X))
1205 return V_0280A0_SWAP_STD; /* XYZ */
1206 else if (HAS_SWIZZLE(0,Z))
1207 return V_0280A0_SWAP_STD_REV; /* ZYX */
1208 break;
1209 case 4:
1210 /* check the middle channels, the 1st and 4th channel can be NONE */
1211 if (HAS_SWIZZLE(1,Y) && HAS_SWIZZLE(2,Z))
1212 return V_0280A0_SWAP_STD; /* XYZW */
1213 else if (HAS_SWIZZLE(1,Z) && HAS_SWIZZLE(2,Y))
1214 return V_0280A0_SWAP_STD_REV; /* WZYX */
1215 else if (HAS_SWIZZLE(1,Y) && HAS_SWIZZLE(2,X))
1216 return V_0280A0_SWAP_ALT; /* ZYXW */
1217 else if (HAS_SWIZZLE(1,X) && HAS_SWIZZLE(2,Y))
1218 return V_0280A0_SWAP_ALT_REV; /* WXYZ */
1219 break;
1220 }
1221 return ~0U;
1222 }
1223
1224 static void evergreen_set_clear_color(struct r600_texture *rtex,
1225 enum pipe_format surface_format,
1226 const union pipe_color_union *color)
1227 {
1228 union util_color uc;
1229
1230 memset(&uc, 0, sizeof(uc));
1231
1232 if (util_format_is_pure_uint(surface_format)) {
1233 util_format_write_4ui(surface_format, color->ui, 0, &uc, 0, 0, 0, 1, 1);
1234 } else if (util_format_is_pure_sint(surface_format)) {
1235 util_format_write_4i(surface_format, color->i, 0, &uc, 0, 0, 0, 1, 1);
1236 } else {
1237 util_pack_color(color->f, surface_format, &uc);
1238 }
1239
1240 memcpy(rtex->color_clear_value, &uc, 2 * sizeof(uint32_t));
1241 }
1242
1243 static void vi_get_fast_clear_parameters(enum pipe_format surface_format,
1244 const union pipe_color_union *color,
1245 uint32_t* reset_value,
1246 bool* clear_words_needed)
1247 {
1248 bool values[4] = {};
1249 int i;
1250 bool main_value = false;
1251 bool extra_value = false;
1252 int extra_channel;
1253 const struct util_format_description *desc = util_format_description(surface_format);
1254
1255 *clear_words_needed = true;
1256 *reset_value = 0x20202020U;
1257
1258 /* If we want to clear without needing a fast clear eliminate step, we
1259 * can set each channel to 0 or 1 (or 0/max for integer formats). We
1260 * have two sets of flags, one for the last or first channel(extra) and
1261 * one for the other channels(main).
1262 */
1263
1264 if (surface_format == PIPE_FORMAT_R11G11B10_FLOAT ||
1265 surface_format == PIPE_FORMAT_B5G6R5_UNORM ||
1266 surface_format == PIPE_FORMAT_B5G6R5_SRGB) {
1267 extra_channel = -1;
1268 } else if (desc->layout == UTIL_FORMAT_LAYOUT_PLAIN) {
1269 if(r600_translate_colorswap(surface_format) <= 1)
1270 extra_channel = desc->nr_channels - 1;
1271 else
1272 extra_channel = 0;
1273 } else
1274 return;
1275
1276 for (i = 0; i < 4; ++i) {
1277 int index = desc->swizzle[i] - UTIL_FORMAT_SWIZZLE_X;
1278
1279 if (desc->swizzle[i] < UTIL_FORMAT_SWIZZLE_X ||
1280 desc->swizzle[i] > UTIL_FORMAT_SWIZZLE_W)
1281 continue;
1282
1283 if (util_format_is_pure_sint(surface_format)) {
1284 values[i] = color->i[i] != 0;
1285 if (color->i[i] != 0 && color->i[i] != INT32_MAX)
1286 return;
1287 } else if (util_format_is_pure_uint(surface_format)) {
1288 values[i] = color->ui[i] != 0U;
1289 if (color->ui[i] != 0U && color->ui[i] != UINT32_MAX)
1290 return;
1291 } else {
1292 values[i] = color->f[i] != 0.0F;
1293 if (color->f[i] != 0.0F && color->f[i] != 1.0F)
1294 return;
1295 }
1296
1297 if (index == extra_channel)
1298 extra_value = values[i];
1299 else
1300 main_value = values[i];
1301 }
1302
1303 for (int i = 0; i < 4; ++i)
1304 if (values[i] != main_value &&
1305 desc->swizzle[i] - UTIL_FORMAT_SWIZZLE_X != extra_channel &&
1306 desc->swizzle[i] >= UTIL_FORMAT_SWIZZLE_X &&
1307 desc->swizzle[i] <= UTIL_FORMAT_SWIZZLE_W)
1308 return;
1309
1310 *clear_words_needed = false;
1311 if (main_value)
1312 *reset_value |= 0x80808080U;
1313
1314 if (extra_value)
1315 *reset_value |= 0x40404040U;
1316 }
1317
1318 void evergreen_do_fast_color_clear(struct r600_common_context *rctx,
1319 struct pipe_framebuffer_state *fb,
1320 struct r600_atom *fb_state,
1321 unsigned *buffers, unsigned *dirty_cbufs,
1322 const union pipe_color_union *color)
1323 {
1324 int i;
1325
1326 if (rctx->render_cond)
1327 return;
1328
1329 for (i = 0; i < fb->nr_cbufs; i++) {
1330 struct r600_texture *tex;
1331 unsigned clear_bit = PIPE_CLEAR_COLOR0 << i;
1332
1333 if (!fb->cbufs[i])
1334 continue;
1335
1336 /* if this colorbuffer is not being cleared */
1337 if (!(*buffers & clear_bit))
1338 continue;
1339
1340 tex = (struct r600_texture *)fb->cbufs[i]->texture;
1341
1342 /* 128-bit formats are unusupported */
1343 if (util_format_get_blocksizebits(fb->cbufs[i]->format) > 64) {
1344 continue;
1345 }
1346
1347 /* the clear is allowed if all layers are bound */
1348 if (fb->cbufs[i]->u.tex.first_layer != 0 ||
1349 fb->cbufs[i]->u.tex.last_layer != util_max_layer(&tex->resource.b.b, 0)) {
1350 continue;
1351 }
1352
1353 /* cannot clear mipmapped textures */
1354 if (fb->cbufs[i]->texture->last_level != 0) {
1355 continue;
1356 }
1357
1358 /* only supported on tiled surfaces */
1359 if (tex->surface.level[0].mode < RADEON_SURF_MODE_1D) {
1360 continue;
1361 }
1362
1363 /* fast color clear with 1D tiling doesn't work on old kernels and CIK */
1364 if (tex->surface.level[0].mode == RADEON_SURF_MODE_1D &&
1365 rctx->chip_class >= CIK &&
1366 rctx->screen->info.drm_major == 2 &&
1367 rctx->screen->info.drm_minor < 38) {
1368 continue;
1369 }
1370
1371 if (tex->dcc_buffer) {
1372 uint32_t reset_value;
1373 bool clear_words_needed;
1374
1375 if (rctx->screen->debug_flags & DBG_NO_DCC_CLEAR)
1376 continue;
1377
1378 vi_get_fast_clear_parameters(fb->cbufs[i]->format, color, &reset_value, &clear_words_needed);
1379
1380 rctx->clear_buffer(&rctx->b, &tex->dcc_buffer->b.b,
1381 0, tex->surface.dcc_size, reset_value, true);
1382
1383 if (clear_words_needed)
1384 tex->dirty_level_mask |= 1 << fb->cbufs[i]->u.tex.level;
1385 } else {
1386 /* ensure CMASK is enabled */
1387 r600_texture_alloc_cmask_separate(rctx->screen, tex);
1388 if (tex->cmask.size == 0) {
1389 continue;
1390 }
1391
1392 /* Do the fast clear. */
1393 rctx->clear_buffer(&rctx->b, &tex->cmask_buffer->b.b,
1394 tex->cmask.offset, tex->cmask.size, 0, true);
1395
1396 tex->dirty_level_mask |= 1 << fb->cbufs[i]->u.tex.level;
1397 }
1398
1399 evergreen_set_clear_color(tex, fb->cbufs[i]->format, color);
1400
1401 if (dirty_cbufs)
1402 *dirty_cbufs |= 1 << i;
1403 rctx->set_atom_dirty(rctx, fb_state, true);
1404 *buffers &= ~clear_bit;
1405 }
1406 }
1407
1408 void r600_init_screen_texture_functions(struct r600_common_screen *rscreen)
1409 {
1410 rscreen->b.resource_from_handle = r600_texture_from_handle;
1411 rscreen->b.resource_get_handle = r600_texture_get_handle;
1412 }
1413
1414 void r600_init_context_texture_functions(struct r600_common_context *rctx)
1415 {
1416 rctx->b.create_surface = r600_create_surface;
1417 rctx->b.surface_destroy = r600_surface_destroy;
1418 }