radeonsi: rework RB+ for Stoney
[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_in_pixels = 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->pitch = pitch_elements;
384 out->height = height;
385 out->xalign = macro_tile_width;
386 out->yalign = macro_tile_height;
387 out->slice_tile_max = ((pitch_elements * height) / (128*128)) - 1;
388 out->alignment = MAX2(256, base_align);
389 out->size = (util_max_layer(&rtex->resource.b.b, 0) + 1) *
390 align(slice_bytes, base_align);
391 }
392
393 static void si_texture_get_cmask_info(struct r600_common_screen *rscreen,
394 struct r600_texture *rtex,
395 struct r600_cmask_info *out)
396 {
397 unsigned pipe_interleave_bytes = rscreen->tiling_info.group_bytes;
398 unsigned num_pipes = rscreen->tiling_info.num_channels;
399 unsigned cl_width, cl_height;
400
401 switch (num_pipes) {
402 case 2:
403 cl_width = 32;
404 cl_height = 16;
405 break;
406 case 4:
407 cl_width = 32;
408 cl_height = 32;
409 break;
410 case 8:
411 cl_width = 64;
412 cl_height = 32;
413 break;
414 case 16: /* Hawaii */
415 cl_width = 64;
416 cl_height = 64;
417 break;
418 default:
419 assert(0);
420 return;
421 }
422
423 unsigned base_align = num_pipes * pipe_interleave_bytes;
424
425 unsigned width = align(rtex->surface.npix_x, cl_width*8);
426 unsigned height = align(rtex->surface.npix_y, cl_height*8);
427 unsigned slice_elements = (width * height) / (8*8);
428
429 /* Each element of CMASK is a nibble. */
430 unsigned slice_bytes = slice_elements / 2;
431
432 out->pitch = width;
433 out->height = height;
434 out->xalign = cl_width * 8;
435 out->yalign = cl_height * 8;
436 out->slice_tile_max = (width * height) / (128*128);
437 if (out->slice_tile_max)
438 out->slice_tile_max -= 1;
439
440 out->alignment = MAX2(256, base_align);
441 out->size = (util_max_layer(&rtex->resource.b.b, 0) + 1) *
442 align(slice_bytes, base_align);
443 }
444
445 static void r600_texture_allocate_cmask(struct r600_common_screen *rscreen,
446 struct r600_texture *rtex)
447 {
448 if (rscreen->chip_class >= SI) {
449 si_texture_get_cmask_info(rscreen, rtex, &rtex->cmask);
450 } else {
451 r600_texture_get_cmask_info(rscreen, rtex, &rtex->cmask);
452 }
453
454 rtex->cmask.offset = align(rtex->size, rtex->cmask.alignment);
455 rtex->size = rtex->cmask.offset + rtex->cmask.size;
456
457 if (rscreen->chip_class >= SI)
458 rtex->cb_color_info |= SI_S_028C70_FAST_CLEAR(1);
459 else
460 rtex->cb_color_info |= EG_S_028C70_FAST_CLEAR(1);
461 }
462
463 static void r600_texture_alloc_cmask_separate(struct r600_common_screen *rscreen,
464 struct r600_texture *rtex)
465 {
466 if (rtex->cmask_buffer)
467 return;
468
469 assert(rtex->cmask.size == 0);
470
471 if (rscreen->chip_class >= SI) {
472 si_texture_get_cmask_info(rscreen, rtex, &rtex->cmask);
473 } else {
474 r600_texture_get_cmask_info(rscreen, rtex, &rtex->cmask);
475 }
476
477 rtex->cmask_buffer = (struct r600_resource *)
478 pipe_buffer_create(&rscreen->b, PIPE_BIND_CUSTOM,
479 PIPE_USAGE_DEFAULT, rtex->cmask.size);
480 if (rtex->cmask_buffer == NULL) {
481 rtex->cmask.size = 0;
482 return;
483 }
484
485 /* update colorbuffer state bits */
486 rtex->cmask.base_address_reg = rtex->cmask_buffer->gpu_address >> 8;
487
488 if (rscreen->chip_class >= SI)
489 rtex->cb_color_info |= SI_S_028C70_FAST_CLEAR(1);
490 else
491 rtex->cb_color_info |= EG_S_028C70_FAST_CLEAR(1);
492 }
493
494 static void vi_texture_alloc_dcc_separate(struct r600_common_screen *rscreen,
495 struct r600_texture *rtex)
496 {
497 if (rscreen->debug_flags & DBG_NO_DCC)
498 return;
499
500 rtex->dcc_buffer = (struct r600_resource *)
501 r600_aligned_buffer_create(&rscreen->b, PIPE_BIND_CUSTOM,
502 PIPE_USAGE_DEFAULT, rtex->surface.dcc_size, rtex->surface.dcc_alignment);
503 if (rtex->dcc_buffer == NULL) {
504 return;
505 }
506
507 r600_screen_clear_buffer(rscreen, &rtex->dcc_buffer->b.b, 0, rtex->surface.dcc_size,
508 0xFFFFFFFF, true);
509
510 rtex->cb_color_info |= VI_S_028C70_DCC_ENABLE(1);
511 }
512
513 static unsigned r600_texture_get_htile_size(struct r600_common_screen *rscreen,
514 struct r600_texture *rtex)
515 {
516 unsigned cl_width, cl_height, width, height;
517 unsigned slice_elements, slice_bytes, pipe_interleave_bytes, base_align;
518 unsigned num_pipes = rscreen->tiling_info.num_channels;
519
520 if (rscreen->chip_class <= EVERGREEN &&
521 rscreen->info.drm_major == 2 && rscreen->info.drm_minor < 26)
522 return 0;
523
524 /* HW bug on R6xx. */
525 if (rscreen->chip_class == R600 &&
526 (rtex->surface.level[0].npix_x > 7680 ||
527 rtex->surface.level[0].npix_y > 7680))
528 return 0;
529
530 /* HTILE is broken with 1D tiling on old kernels and CIK. */
531 if (rscreen->chip_class >= CIK &&
532 rtex->surface.level[0].mode == RADEON_SURF_MODE_1D &&
533 rscreen->info.drm_major == 2 && rscreen->info.drm_minor < 38)
534 return 0;
535
536 switch (num_pipes) {
537 case 1:
538 cl_width = 32;
539 cl_height = 16;
540 break;
541 case 2:
542 cl_width = 32;
543 cl_height = 32;
544 break;
545 case 4:
546 cl_width = 64;
547 cl_height = 32;
548 break;
549 case 8:
550 cl_width = 64;
551 cl_height = 64;
552 break;
553 case 16:
554 cl_width = 128;
555 cl_height = 64;
556 break;
557 default:
558 assert(0);
559 return 0;
560 }
561
562 width = align(rtex->surface.npix_x, cl_width * 8);
563 height = align(rtex->surface.npix_y, cl_height * 8);
564
565 slice_elements = (width * height) / (8 * 8);
566 slice_bytes = slice_elements * 4;
567
568 pipe_interleave_bytes = rscreen->tiling_info.group_bytes;
569 base_align = num_pipes * pipe_interleave_bytes;
570
571 rtex->htile.pitch = width;
572 rtex->htile.height = height;
573 rtex->htile.xalign = cl_width * 8;
574 rtex->htile.yalign = cl_height * 8;
575
576 return (util_max_layer(&rtex->resource.b.b, 0) + 1) *
577 align(slice_bytes, base_align);
578 }
579
580 static void r600_texture_allocate_htile(struct r600_common_screen *rscreen,
581 struct r600_texture *rtex)
582 {
583 unsigned htile_size = r600_texture_get_htile_size(rscreen, rtex);
584
585 if (!htile_size)
586 return;
587
588 rtex->htile_buffer = (struct r600_resource*)
589 pipe_buffer_create(&rscreen->b, PIPE_BIND_CUSTOM,
590 PIPE_USAGE_DEFAULT, htile_size);
591 if (rtex->htile_buffer == NULL) {
592 /* this is not a fatal error as we can still keep rendering
593 * without htile buffer */
594 R600_ERR("Failed to create buffer object for htile buffer.\n");
595 } else {
596 r600_screen_clear_buffer(rscreen, &rtex->htile_buffer->b.b, 0,
597 htile_size, 0, true);
598 }
599 }
600
601 void r600_print_texture_info(struct r600_texture *rtex, FILE *f)
602 {
603 int i;
604
605 fprintf(f, " Info: npix_x=%u, npix_y=%u, npix_z=%u, blk_w=%u, "
606 "blk_h=%u, blk_d=%u, array_size=%u, last_level=%u, "
607 "bpe=%u, nsamples=%u, flags=0x%x, %s\n",
608 rtex->surface.npix_x, rtex->surface.npix_y,
609 rtex->surface.npix_z, rtex->surface.blk_w,
610 rtex->surface.blk_h, rtex->surface.blk_d,
611 rtex->surface.array_size, rtex->surface.last_level,
612 rtex->surface.bpe, rtex->surface.nsamples,
613 rtex->surface.flags, util_format_short_name(rtex->resource.b.b.format));
614
615 fprintf(f, " Layout: size=%"PRIu64", alignment=%"PRIu64", bankw=%u, "
616 "bankh=%u, nbanks=%u, mtilea=%u, tilesplit=%u, pipeconfig=%u, scanout=%u\n",
617 rtex->surface.bo_size, rtex->surface.bo_alignment, rtex->surface.bankw,
618 rtex->surface.bankh, rtex->surface.num_banks, rtex->surface.mtilea,
619 rtex->surface.tile_split, rtex->surface.pipe_config,
620 (rtex->surface.flags & RADEON_SURF_SCANOUT) != 0);
621
622 if (rtex->fmask.size)
623 fprintf(f, " FMask: offset=%u, size=%u, alignment=%u, pitch_in_pixels=%u, "
624 "bankh=%u, slice_tile_max=%u, tile_mode_index=%u\n",
625 rtex->fmask.offset, rtex->fmask.size, rtex->fmask.alignment,
626 rtex->fmask.pitch_in_pixels, rtex->fmask.bank_height,
627 rtex->fmask.slice_tile_max, rtex->fmask.tile_mode_index);
628
629 if (rtex->cmask.size)
630 fprintf(f, " CMask: offset=%u, size=%u, alignment=%u, pitch=%u, "
631 "height=%u, xalign=%u, yalign=%u, slice_tile_max=%u\n",
632 rtex->cmask.offset, rtex->cmask.size, rtex->cmask.alignment,
633 rtex->cmask.pitch, rtex->cmask.height, rtex->cmask.xalign,
634 rtex->cmask.yalign, rtex->cmask.slice_tile_max);
635
636 if (rtex->htile_buffer)
637 fprintf(f, " HTile: size=%u, alignment=%u, pitch=%u, height=%u, "
638 "xalign=%u, yalign=%u\n",
639 rtex->htile_buffer->b.b.width0,
640 rtex->htile_buffer->buf->alignment, rtex->htile.pitch,
641 rtex->htile.height, rtex->htile.xalign, rtex->htile.yalign);
642
643 if (rtex->dcc_buffer) {
644 fprintf(f, " DCC: size=%u, alignment=%u\n",
645 rtex->dcc_buffer->b.b.width0,
646 rtex->dcc_buffer->buf->alignment);
647 for (i = 0; i <= rtex->surface.last_level; i++)
648 fprintf(f, " DCCLevel[%i]: offset=%"PRIu64"\n",
649 i, rtex->surface.level[i].dcc_offset);
650 }
651
652 for (i = 0; i <= rtex->surface.last_level; i++)
653 fprintf(f, " Level[%i]: offset=%"PRIu64", slice_size=%"PRIu64", "
654 "npix_x=%u, npix_y=%u, npix_z=%u, nblk_x=%u, nblk_y=%u, "
655 "nblk_z=%u, pitch_bytes=%u, mode=%u\n",
656 i, rtex->surface.level[i].offset,
657 rtex->surface.level[i].slice_size,
658 u_minify(rtex->resource.b.b.width0, i),
659 u_minify(rtex->resource.b.b.height0, i),
660 u_minify(rtex->resource.b.b.depth0, i),
661 rtex->surface.level[i].nblk_x,
662 rtex->surface.level[i].nblk_y,
663 rtex->surface.level[i].nblk_z,
664 rtex->surface.level[i].pitch_bytes,
665 rtex->surface.level[i].mode);
666
667 if (rtex->surface.flags & RADEON_SURF_SBUFFER) {
668 for (i = 0; i <= rtex->surface.last_level; i++) {
669 fprintf(f, " StencilLayout: tilesplit=%u\n",
670 rtex->surface.stencil_tile_split);
671 fprintf(f, " StencilLevel[%i]: offset=%"PRIu64", "
672 "slice_size=%"PRIu64", npix_x=%u, "
673 "npix_y=%u, npix_z=%u, nblk_x=%u, nblk_y=%u, "
674 "nblk_z=%u, pitch_bytes=%u, mode=%u\n",
675 i, rtex->surface.stencil_level[i].offset,
676 rtex->surface.stencil_level[i].slice_size,
677 u_minify(rtex->resource.b.b.width0, i),
678 u_minify(rtex->resource.b.b.height0, i),
679 u_minify(rtex->resource.b.b.depth0, i),
680 rtex->surface.stencil_level[i].nblk_x,
681 rtex->surface.stencil_level[i].nblk_y,
682 rtex->surface.stencil_level[i].nblk_z,
683 rtex->surface.stencil_level[i].pitch_bytes,
684 rtex->surface.stencil_level[i].mode);
685 }
686 }
687 }
688
689 /* Common processing for r600_texture_create and r600_texture_from_handle */
690 static struct r600_texture *
691 r600_texture_create_object(struct pipe_screen *screen,
692 const struct pipe_resource *base,
693 unsigned pitch_in_bytes_override,
694 struct pb_buffer *buf,
695 struct radeon_surf *surface)
696 {
697 struct r600_texture *rtex;
698 struct r600_resource *resource;
699 struct r600_common_screen *rscreen = (struct r600_common_screen*)screen;
700
701 rtex = CALLOC_STRUCT(r600_texture);
702 if (!rtex)
703 return NULL;
704
705 resource = &rtex->resource;
706 resource->b.b = *base;
707 resource->b.vtbl = &r600_texture_vtbl;
708 pipe_reference_init(&resource->b.b.reference, 1);
709 resource->b.b.screen = screen;
710
711 /* don't include stencil-only formats which we don't support for rendering */
712 rtex->is_depth = util_format_has_depth(util_format_description(rtex->resource.b.b.format));
713
714 rtex->surface = *surface;
715 if (r600_setup_surface(screen, rtex, pitch_in_bytes_override)) {
716 FREE(rtex);
717 return NULL;
718 }
719
720 /* Tiled depth textures utilize the non-displayable tile order.
721 * This must be done after r600_setup_surface.
722 * Applies to R600-Cayman. */
723 rtex->non_disp_tiling = rtex->is_depth && rtex->surface.level[0].mode >= RADEON_SURF_MODE_1D;
724
725 if (rtex->is_depth) {
726 if (!(base->flags & (R600_RESOURCE_FLAG_TRANSFER |
727 R600_RESOURCE_FLAG_FLUSHED_DEPTH)) &&
728 !(rscreen->debug_flags & DBG_NO_HYPERZ)) {
729
730 r600_texture_allocate_htile(rscreen, rtex);
731 }
732 } else {
733 if (base->nr_samples > 1) {
734 if (!buf) {
735 r600_texture_allocate_fmask(rscreen, rtex);
736 r600_texture_allocate_cmask(rscreen, rtex);
737 rtex->cmask_buffer = &rtex->resource;
738 }
739 if (!rtex->fmask.size || !rtex->cmask.size) {
740 FREE(rtex);
741 return NULL;
742 }
743 }
744 if (rtex->surface.dcc_size)
745 vi_texture_alloc_dcc_separate(rscreen, rtex);
746 }
747
748 /* Now create the backing buffer. */
749 if (!buf) {
750 if (!r600_init_resource(rscreen, resource, rtex->size,
751 rtex->surface.bo_alignment, TRUE)) {
752 FREE(rtex);
753 return NULL;
754 }
755 } else {
756 resource->buf = buf;
757 resource->gpu_address = rscreen->ws->buffer_get_virtual_address(resource->buf);
758 resource->domains = rscreen->ws->buffer_get_initial_domain(resource->buf);
759 }
760
761 if (rtex->cmask.size) {
762 /* Initialize the cmask to 0xCC (= compressed state). */
763 r600_screen_clear_buffer(rscreen, &rtex->cmask_buffer->b.b,
764 rtex->cmask.offset, rtex->cmask.size,
765 0xCCCCCCCC, true);
766 }
767
768 /* Initialize the CMASK base register value. */
769 rtex->cmask.base_address_reg =
770 (rtex->resource.gpu_address + rtex->cmask.offset) >> 8;
771
772 if (rscreen->debug_flags & DBG_VM) {
773 fprintf(stderr, "VM start=0x%"PRIX64" end=0x%"PRIX64" | Texture %ix%ix%i, %i levels, %i samples, %s\n",
774 rtex->resource.gpu_address,
775 rtex->resource.gpu_address + rtex->resource.buf->size,
776 base->width0, base->height0, util_max_layer(base, 0)+1, base->last_level+1,
777 base->nr_samples ? base->nr_samples : 1, util_format_short_name(base->format));
778 }
779
780 if (rscreen->debug_flags & DBG_TEX) {
781 puts("Texture:");
782 r600_print_texture_info(rtex, stdout);
783 }
784
785 return rtex;
786 }
787
788 static unsigned r600_choose_tiling(struct r600_common_screen *rscreen,
789 const struct pipe_resource *templ)
790 {
791 const struct util_format_description *desc = util_format_description(templ->format);
792 bool force_tiling = templ->flags & R600_RESOURCE_FLAG_FORCE_TILING;
793
794 /* MSAA resources must be 2D tiled. */
795 if (templ->nr_samples > 1)
796 return RADEON_SURF_MODE_2D;
797
798 /* Transfer resources should be linear. */
799 if (templ->flags & R600_RESOURCE_FLAG_TRANSFER)
800 return RADEON_SURF_MODE_LINEAR_ALIGNED;
801
802 /* r600g: force tiling on TEXTURE_2D and TEXTURE_3D compute resources. */
803 if (rscreen->chip_class >= R600 && rscreen->chip_class <= CAYMAN &&
804 (templ->bind & PIPE_BIND_COMPUTE_RESOURCE) &&
805 (templ->target == PIPE_TEXTURE_2D ||
806 templ->target == PIPE_TEXTURE_3D))
807 force_tiling = true;
808
809 /* Handle common candidates for the linear mode.
810 * Compressed textures must always be tiled. */
811 if (!force_tiling && !util_format_is_compressed(templ->format)) {
812 /* Not everything can be linear, so we cannot enforce it
813 * for all textures. */
814 if ((rscreen->debug_flags & DBG_NO_TILING) &&
815 (!util_format_is_depth_or_stencil(templ->format) ||
816 !(templ->flags & R600_RESOURCE_FLAG_FLUSHED_DEPTH)))
817 return RADEON_SURF_MODE_LINEAR_ALIGNED;
818
819 /* Tiling doesn't work with the 422 (SUBSAMPLED) formats on R600+. */
820 if (desc->layout == UTIL_FORMAT_LAYOUT_SUBSAMPLED)
821 return RADEON_SURF_MODE_LINEAR_ALIGNED;
822
823 /* Cursors are linear on SI.
824 * (XXX double-check, maybe also use RADEON_SURF_SCANOUT) */
825 if (rscreen->chip_class >= SI &&
826 (templ->bind & PIPE_BIND_CURSOR))
827 return RADEON_SURF_MODE_LINEAR_ALIGNED;
828
829 if (templ->bind & PIPE_BIND_LINEAR)
830 return RADEON_SURF_MODE_LINEAR_ALIGNED;
831
832 /* Textures with a very small height are recommended to be linear. */
833 if (templ->target == PIPE_TEXTURE_1D ||
834 templ->target == PIPE_TEXTURE_1D_ARRAY ||
835 templ->height0 <= 4)
836 return RADEON_SURF_MODE_LINEAR_ALIGNED;
837
838 /* Textures likely to be mapped often. */
839 if (templ->usage == PIPE_USAGE_STAGING ||
840 templ->usage == PIPE_USAGE_STREAM)
841 return RADEON_SURF_MODE_LINEAR_ALIGNED;
842 }
843
844 /* Make small textures 1D tiled. */
845 if (templ->width0 <= 16 || templ->height0 <= 16 ||
846 (rscreen->debug_flags & DBG_NO_2D_TILING))
847 return RADEON_SURF_MODE_1D;
848
849 /* The allocator will switch to 1D if needed. */
850 return RADEON_SURF_MODE_2D;
851 }
852
853 struct pipe_resource *r600_texture_create(struct pipe_screen *screen,
854 const struct pipe_resource *templ)
855 {
856 struct r600_common_screen *rscreen = (struct r600_common_screen*)screen;
857 struct radeon_surf surface = {0};
858 int r;
859
860 r = r600_init_surface(rscreen, &surface, templ,
861 r600_choose_tiling(rscreen, templ),
862 templ->flags & R600_RESOURCE_FLAG_FLUSHED_DEPTH);
863 if (r) {
864 return NULL;
865 }
866 r = rscreen->ws->surface_best(rscreen->ws, &surface);
867 if (r) {
868 return NULL;
869 }
870 return (struct pipe_resource *)r600_texture_create_object(screen, templ,
871 0, NULL, &surface);
872 }
873
874 static struct pipe_resource *r600_texture_from_handle(struct pipe_screen *screen,
875 const struct pipe_resource *templ,
876 struct winsys_handle *whandle)
877 {
878 struct r600_common_screen *rscreen = (struct r600_common_screen*)screen;
879 struct pb_buffer *buf = NULL;
880 unsigned stride = 0;
881 unsigned array_mode;
882 enum radeon_bo_layout micro, macro;
883 struct radeon_surf surface;
884 bool scanout;
885 int r;
886
887 /* Support only 2D textures without mipmaps */
888 if ((templ->target != PIPE_TEXTURE_2D && templ->target != PIPE_TEXTURE_RECT) ||
889 templ->depth0 != 1 || templ->last_level != 0)
890 return NULL;
891
892 buf = rscreen->ws->buffer_from_handle(rscreen->ws, whandle, &stride);
893 if (!buf)
894 return NULL;
895
896 rscreen->ws->buffer_get_tiling(buf, &micro, &macro,
897 &surface.bankw, &surface.bankh,
898 &surface.tile_split,
899 &surface.stencil_tile_split,
900 &surface.mtilea, &scanout);
901
902 if (macro == RADEON_LAYOUT_TILED)
903 array_mode = RADEON_SURF_MODE_2D;
904 else if (micro == RADEON_LAYOUT_TILED)
905 array_mode = RADEON_SURF_MODE_1D;
906 else
907 array_mode = RADEON_SURF_MODE_LINEAR_ALIGNED;
908
909 r = r600_init_surface(rscreen, &surface, templ, array_mode, false);
910 if (r) {
911 return NULL;
912 }
913
914 if (scanout)
915 surface.flags |= RADEON_SURF_SCANOUT;
916
917 return (struct pipe_resource *)r600_texture_create_object(screen, templ,
918 stride, buf, &surface);
919 }
920
921 bool r600_init_flushed_depth_texture(struct pipe_context *ctx,
922 struct pipe_resource *texture,
923 struct r600_texture **staging)
924 {
925 struct r600_texture *rtex = (struct r600_texture*)texture;
926 struct pipe_resource resource;
927 struct r600_texture **flushed_depth_texture = staging ?
928 staging : &rtex->flushed_depth_texture;
929
930 if (!staging && rtex->flushed_depth_texture)
931 return true; /* it's ready */
932
933 resource.target = texture->target;
934 resource.format = texture->format;
935 resource.width0 = texture->width0;
936 resource.height0 = texture->height0;
937 resource.depth0 = texture->depth0;
938 resource.array_size = texture->array_size;
939 resource.last_level = texture->last_level;
940 resource.nr_samples = texture->nr_samples;
941 resource.usage = staging ? PIPE_USAGE_STAGING : PIPE_USAGE_DEFAULT;
942 resource.bind = texture->bind & ~PIPE_BIND_DEPTH_STENCIL;
943 resource.flags = texture->flags | R600_RESOURCE_FLAG_FLUSHED_DEPTH;
944
945 if (staging)
946 resource.flags |= R600_RESOURCE_FLAG_TRANSFER;
947
948 *flushed_depth_texture = (struct r600_texture *)ctx->screen->resource_create(ctx->screen, &resource);
949 if (*flushed_depth_texture == NULL) {
950 R600_ERR("failed to create temporary texture to hold flushed depth\n");
951 return false;
952 }
953
954 (*flushed_depth_texture)->is_flushing_texture = TRUE;
955 (*flushed_depth_texture)->non_disp_tiling = false;
956 return true;
957 }
958
959 /**
960 * Initialize the pipe_resource descriptor to be of the same size as the box,
961 * which is supposed to hold a subregion of the texture "orig" at the given
962 * mipmap level.
963 */
964 static void r600_init_temp_resource_from_box(struct pipe_resource *res,
965 struct pipe_resource *orig,
966 const struct pipe_box *box,
967 unsigned level, unsigned flags)
968 {
969 memset(res, 0, sizeof(*res));
970 res->format = orig->format;
971 res->width0 = box->width;
972 res->height0 = box->height;
973 res->depth0 = 1;
974 res->array_size = 1;
975 res->usage = flags & R600_RESOURCE_FLAG_TRANSFER ? PIPE_USAGE_STAGING : PIPE_USAGE_DEFAULT;
976 res->flags = flags;
977
978 /* We must set the correct texture target and dimensions for a 3D box. */
979 if (box->depth > 1 && util_max_layer(orig, level) > 0)
980 res->target = orig->target;
981 else
982 res->target = PIPE_TEXTURE_2D;
983
984 switch (res->target) {
985 case PIPE_TEXTURE_1D_ARRAY:
986 case PIPE_TEXTURE_2D_ARRAY:
987 case PIPE_TEXTURE_CUBE_ARRAY:
988 res->array_size = box->depth;
989 break;
990 case PIPE_TEXTURE_3D:
991 res->depth0 = box->depth;
992 break;
993 default:;
994 }
995 }
996
997 static void *r600_texture_transfer_map(struct pipe_context *ctx,
998 struct pipe_resource *texture,
999 unsigned level,
1000 unsigned usage,
1001 const struct pipe_box *box,
1002 struct pipe_transfer **ptransfer)
1003 {
1004 struct r600_common_context *rctx = (struct r600_common_context*)ctx;
1005 struct r600_texture *rtex = (struct r600_texture*)texture;
1006 struct r600_transfer *trans;
1007 boolean use_staging_texture = FALSE;
1008 struct r600_resource *buf;
1009 unsigned offset = 0;
1010 char *map;
1011
1012 /* We cannot map a tiled texture directly because the data is
1013 * in a different order, therefore we do detiling using a blit.
1014 *
1015 * Also, use a temporary in GTT memory for read transfers, as
1016 * the CPU is much happier reading out of cached system memory
1017 * than uncached VRAM.
1018 */
1019 if (rtex->surface.level[0].mode >= RADEON_SURF_MODE_1D) {
1020 use_staging_texture = TRUE;
1021 } else if ((usage & PIPE_TRANSFER_READ) && !(usage & PIPE_TRANSFER_MAP_DIRECTLY) &&
1022 (rtex->resource.domains == RADEON_DOMAIN_VRAM)) {
1023 /* Untiled buffers in VRAM, which is slow for CPU reads */
1024 use_staging_texture = TRUE;
1025 } else if (!(usage & PIPE_TRANSFER_READ) &&
1026 (r600_rings_is_buffer_referenced(rctx, rtex->resource.buf, RADEON_USAGE_READWRITE) ||
1027 !rctx->ws->buffer_wait(rtex->resource.buf, 0, RADEON_USAGE_READWRITE))) {
1028 /* Use a staging texture for uploads if the underlying BO is busy. */
1029 use_staging_texture = TRUE;
1030 }
1031
1032 if (texture->flags & R600_RESOURCE_FLAG_TRANSFER) {
1033 use_staging_texture = FALSE;
1034 }
1035
1036 if (use_staging_texture && (usage & PIPE_TRANSFER_MAP_DIRECTLY)) {
1037 return NULL;
1038 }
1039
1040 trans = CALLOC_STRUCT(r600_transfer);
1041 if (!trans)
1042 return NULL;
1043 trans->transfer.resource = texture;
1044 trans->transfer.level = level;
1045 trans->transfer.usage = usage;
1046 trans->transfer.box = *box;
1047
1048 if (rtex->is_depth) {
1049 struct r600_texture *staging_depth;
1050
1051 if (rtex->resource.b.b.nr_samples > 1) {
1052 /* MSAA depth buffers need to be converted to single sample buffers.
1053 *
1054 * Mapping MSAA depth buffers can occur if ReadPixels is called
1055 * with a multisample GLX visual.
1056 *
1057 * First downsample the depth buffer to a temporary texture,
1058 * then decompress the temporary one to staging.
1059 *
1060 * Only the region being mapped is transfered.
1061 */
1062 struct pipe_resource resource;
1063
1064 r600_init_temp_resource_from_box(&resource, texture, box, level, 0);
1065
1066 if (!r600_init_flushed_depth_texture(ctx, &resource, &staging_depth)) {
1067 R600_ERR("failed to create temporary texture to hold untiled copy\n");
1068 FREE(trans);
1069 return NULL;
1070 }
1071
1072 if (usage & PIPE_TRANSFER_READ) {
1073 struct pipe_resource *temp = ctx->screen->resource_create(ctx->screen, &resource);
1074 if (!temp) {
1075 R600_ERR("failed to create a temporary depth texture\n");
1076 FREE(trans);
1077 return NULL;
1078 }
1079
1080 r600_copy_region_with_blit(ctx, temp, 0, 0, 0, 0, texture, level, box);
1081 rctx->blit_decompress_depth(ctx, (struct r600_texture*)temp, staging_depth,
1082 0, 0, 0, box->depth, 0, 0);
1083 pipe_resource_reference(&temp, NULL);
1084 }
1085 }
1086 else {
1087 /* XXX: only readback the rectangle which is being mapped? */
1088 /* XXX: when discard is true, no need to read back from depth texture */
1089 if (!r600_init_flushed_depth_texture(ctx, texture, &staging_depth)) {
1090 R600_ERR("failed to create temporary texture to hold untiled copy\n");
1091 FREE(trans);
1092 return NULL;
1093 }
1094
1095 rctx->blit_decompress_depth(ctx, rtex, staging_depth,
1096 level, level,
1097 box->z, box->z + box->depth - 1,
1098 0, 0);
1099
1100 offset = r600_texture_get_offset(staging_depth, level, box);
1101 }
1102
1103 trans->transfer.stride = staging_depth->surface.level[level].pitch_bytes;
1104 trans->transfer.layer_stride = staging_depth->surface.level[level].slice_size;
1105 trans->staging = (struct r600_resource*)staging_depth;
1106 } else if (use_staging_texture) {
1107 struct pipe_resource resource;
1108 struct r600_texture *staging;
1109
1110 r600_init_temp_resource_from_box(&resource, texture, box, level,
1111 R600_RESOURCE_FLAG_TRANSFER);
1112 resource.usage = (usage & PIPE_TRANSFER_READ) ?
1113 PIPE_USAGE_STAGING : PIPE_USAGE_STREAM;
1114
1115 /* Create the temporary texture. */
1116 staging = (struct r600_texture*)ctx->screen->resource_create(ctx->screen, &resource);
1117 if (!staging) {
1118 R600_ERR("failed to create temporary texture to hold untiled copy\n");
1119 FREE(trans);
1120 return NULL;
1121 }
1122 trans->staging = &staging->resource;
1123 trans->transfer.stride = staging->surface.level[0].pitch_bytes;
1124 trans->transfer.layer_stride = staging->surface.level[0].slice_size;
1125 if (usage & PIPE_TRANSFER_READ) {
1126 r600_copy_to_staging_texture(ctx, trans);
1127 }
1128 } else {
1129 /* the resource is mapped directly */
1130 trans->transfer.stride = rtex->surface.level[level].pitch_bytes;
1131 trans->transfer.layer_stride = rtex->surface.level[level].slice_size;
1132 offset = r600_texture_get_offset(rtex, level, box);
1133 }
1134
1135 if (trans->staging) {
1136 buf = trans->staging;
1137 if (!rtex->is_depth && !(usage & PIPE_TRANSFER_READ))
1138 usage |= PIPE_TRANSFER_UNSYNCHRONIZED;
1139 } else {
1140 buf = &rtex->resource;
1141 }
1142
1143 if (!(map = r600_buffer_map_sync_with_rings(rctx, buf, usage))) {
1144 pipe_resource_reference((struct pipe_resource**)&trans->staging, NULL);
1145 FREE(trans);
1146 return NULL;
1147 }
1148
1149 *ptransfer = &trans->transfer;
1150 return map + offset;
1151 }
1152
1153 static void r600_texture_transfer_unmap(struct pipe_context *ctx,
1154 struct pipe_transfer* transfer)
1155 {
1156 struct r600_transfer *rtransfer = (struct r600_transfer*)transfer;
1157 struct pipe_resource *texture = transfer->resource;
1158 struct r600_texture *rtex = (struct r600_texture*)texture;
1159
1160 if ((transfer->usage & PIPE_TRANSFER_WRITE) && rtransfer->staging) {
1161 if (rtex->is_depth && rtex->resource.b.b.nr_samples <= 1) {
1162 ctx->resource_copy_region(ctx, texture, transfer->level,
1163 transfer->box.x, transfer->box.y, transfer->box.z,
1164 &rtransfer->staging->b.b, transfer->level,
1165 &transfer->box);
1166 } else {
1167 r600_copy_from_staging_texture(ctx, rtransfer);
1168 }
1169 }
1170
1171 if (rtransfer->staging)
1172 pipe_resource_reference((struct pipe_resource**)&rtransfer->staging, NULL);
1173
1174 FREE(transfer);
1175 }
1176
1177 static const struct u_resource_vtbl r600_texture_vtbl =
1178 {
1179 NULL, /* get_handle */
1180 r600_texture_destroy, /* resource_destroy */
1181 r600_texture_transfer_map, /* transfer_map */
1182 u_default_transfer_flush_region, /* transfer_flush_region */
1183 r600_texture_transfer_unmap, /* transfer_unmap */
1184 NULL /* transfer_inline_write */
1185 };
1186
1187 struct pipe_surface *r600_create_surface_custom(struct pipe_context *pipe,
1188 struct pipe_resource *texture,
1189 const struct pipe_surface *templ,
1190 unsigned width, unsigned height)
1191 {
1192 struct r600_surface *surface = CALLOC_STRUCT(r600_surface);
1193
1194 if (!surface)
1195 return NULL;
1196
1197 assert(templ->u.tex.first_layer <= util_max_layer(texture, templ->u.tex.level));
1198 assert(templ->u.tex.last_layer <= util_max_layer(texture, templ->u.tex.level));
1199
1200 pipe_reference_init(&surface->base.reference, 1);
1201 pipe_resource_reference(&surface->base.texture, texture);
1202 surface->base.context = pipe;
1203 surface->base.format = templ->format;
1204 surface->base.width = width;
1205 surface->base.height = height;
1206 surface->base.u = templ->u;
1207 return &surface->base;
1208 }
1209
1210 static struct pipe_surface *r600_create_surface(struct pipe_context *pipe,
1211 struct pipe_resource *tex,
1212 const struct pipe_surface *templ)
1213 {
1214 unsigned level = templ->u.tex.level;
1215
1216 return r600_create_surface_custom(pipe, tex, templ,
1217 u_minify(tex->width0, level),
1218 u_minify(tex->height0, level));
1219 }
1220
1221 static void r600_surface_destroy(struct pipe_context *pipe,
1222 struct pipe_surface *surface)
1223 {
1224 struct r600_surface *surf = (struct r600_surface*)surface;
1225 pipe_resource_reference((struct pipe_resource**)&surf->cb_buffer_fmask, NULL);
1226 pipe_resource_reference((struct pipe_resource**)&surf->cb_buffer_cmask, NULL);
1227 pipe_resource_reference(&surface->texture, NULL);
1228 FREE(surface);
1229 }
1230
1231 unsigned r600_translate_colorswap(enum pipe_format format)
1232 {
1233 const struct util_format_description *desc = util_format_description(format);
1234
1235 #define HAS_SWIZZLE(chan,swz) (desc->swizzle[chan] == UTIL_FORMAT_SWIZZLE_##swz)
1236
1237 if (format == PIPE_FORMAT_R11G11B10_FLOAT) /* isn't plain */
1238 return V_0280A0_SWAP_STD;
1239
1240 if (desc->layout != UTIL_FORMAT_LAYOUT_PLAIN)
1241 return ~0U;
1242
1243 switch (desc->nr_channels) {
1244 case 1:
1245 if (HAS_SWIZZLE(0,X))
1246 return V_0280A0_SWAP_STD; /* X___ */
1247 else if (HAS_SWIZZLE(3,X))
1248 return V_0280A0_SWAP_ALT_REV; /* ___X */
1249 break;
1250 case 2:
1251 if ((HAS_SWIZZLE(0,X) && HAS_SWIZZLE(1,Y)) ||
1252 (HAS_SWIZZLE(0,X) && HAS_SWIZZLE(1,NONE)) ||
1253 (HAS_SWIZZLE(0,NONE) && HAS_SWIZZLE(1,Y)))
1254 return V_0280A0_SWAP_STD; /* XY__ */
1255 else if ((HAS_SWIZZLE(0,Y) && HAS_SWIZZLE(1,X)) ||
1256 (HAS_SWIZZLE(0,Y) && HAS_SWIZZLE(1,NONE)) ||
1257 (HAS_SWIZZLE(0,NONE) && HAS_SWIZZLE(1,X)))
1258 return V_0280A0_SWAP_STD_REV; /* YX__ */
1259 else if (HAS_SWIZZLE(0,X) && HAS_SWIZZLE(3,Y))
1260 return V_0280A0_SWAP_ALT; /* X__Y */
1261 else if (HAS_SWIZZLE(0,Y) && HAS_SWIZZLE(3,X))
1262 return V_0280A0_SWAP_ALT_REV; /* Y__X */
1263 break;
1264 case 3:
1265 if (HAS_SWIZZLE(0,X))
1266 return V_0280A0_SWAP_STD; /* XYZ */
1267 else if (HAS_SWIZZLE(0,Z))
1268 return V_0280A0_SWAP_STD_REV; /* ZYX */
1269 break;
1270 case 4:
1271 /* check the middle channels, the 1st and 4th channel can be NONE */
1272 if (HAS_SWIZZLE(1,Y) && HAS_SWIZZLE(2,Z))
1273 return V_0280A0_SWAP_STD; /* XYZW */
1274 else if (HAS_SWIZZLE(1,Z) && HAS_SWIZZLE(2,Y))
1275 return V_0280A0_SWAP_STD_REV; /* WZYX */
1276 else if (HAS_SWIZZLE(1,Y) && HAS_SWIZZLE(2,X))
1277 return V_0280A0_SWAP_ALT; /* ZYXW */
1278 else if (HAS_SWIZZLE(1,X) && HAS_SWIZZLE(2,Y))
1279 return V_0280A0_SWAP_ALT_REV; /* WXYZ */
1280 break;
1281 }
1282 return ~0U;
1283 }
1284
1285 static void evergreen_set_clear_color(struct r600_texture *rtex,
1286 enum pipe_format surface_format,
1287 const union pipe_color_union *color)
1288 {
1289 union util_color uc;
1290
1291 memset(&uc, 0, sizeof(uc));
1292
1293 if (util_format_is_pure_uint(surface_format)) {
1294 util_format_write_4ui(surface_format, color->ui, 0, &uc, 0, 0, 0, 1, 1);
1295 } else if (util_format_is_pure_sint(surface_format)) {
1296 util_format_write_4i(surface_format, color->i, 0, &uc, 0, 0, 0, 1, 1);
1297 } else {
1298 util_pack_color(color->f, surface_format, &uc);
1299 }
1300
1301 memcpy(rtex->color_clear_value, &uc, 2 * sizeof(uint32_t));
1302 }
1303
1304 static void vi_get_fast_clear_parameters(enum pipe_format surface_format,
1305 const union pipe_color_union *color,
1306 uint32_t* reset_value,
1307 bool* clear_words_needed)
1308 {
1309 bool values[4] = {};
1310 int i;
1311 bool main_value = false;
1312 bool extra_value = false;
1313 int extra_channel;
1314 const struct util_format_description *desc = util_format_description(surface_format);
1315
1316 *clear_words_needed = true;
1317 *reset_value = 0x20202020U;
1318
1319 /* If we want to clear without needing a fast clear eliminate step, we
1320 * can set each channel to 0 or 1 (or 0/max for integer formats). We
1321 * have two sets of flags, one for the last or first channel(extra) and
1322 * one for the other channels(main).
1323 */
1324
1325 if (surface_format == PIPE_FORMAT_R11G11B10_FLOAT ||
1326 surface_format == PIPE_FORMAT_B5G6R5_UNORM ||
1327 surface_format == PIPE_FORMAT_B5G6R5_SRGB) {
1328 extra_channel = -1;
1329 } else if (desc->layout == UTIL_FORMAT_LAYOUT_PLAIN) {
1330 if(r600_translate_colorswap(surface_format) <= 1)
1331 extra_channel = desc->nr_channels - 1;
1332 else
1333 extra_channel = 0;
1334 } else
1335 return;
1336
1337 for (i = 0; i < 4; ++i) {
1338 int index = desc->swizzle[i] - UTIL_FORMAT_SWIZZLE_X;
1339
1340 if (desc->swizzle[i] < UTIL_FORMAT_SWIZZLE_X ||
1341 desc->swizzle[i] > UTIL_FORMAT_SWIZZLE_W)
1342 continue;
1343
1344 if (util_format_is_pure_sint(surface_format)) {
1345 values[i] = color->i[i] != 0;
1346 if (color->i[i] != 0 && color->i[i] != INT32_MAX)
1347 return;
1348 } else if (util_format_is_pure_uint(surface_format)) {
1349 values[i] = color->ui[i] != 0U;
1350 if (color->ui[i] != 0U && color->ui[i] != UINT32_MAX)
1351 return;
1352 } else {
1353 values[i] = color->f[i] != 0.0F;
1354 if (color->f[i] != 0.0F && color->f[i] != 1.0F)
1355 return;
1356 }
1357
1358 if (index == extra_channel)
1359 extra_value = values[i];
1360 else
1361 main_value = values[i];
1362 }
1363
1364 for (int i = 0; i < 4; ++i)
1365 if (values[i] != main_value &&
1366 desc->swizzle[i] - UTIL_FORMAT_SWIZZLE_X != extra_channel &&
1367 desc->swizzle[i] >= UTIL_FORMAT_SWIZZLE_X &&
1368 desc->swizzle[i] <= UTIL_FORMAT_SWIZZLE_W)
1369 return;
1370
1371 *clear_words_needed = false;
1372 if (main_value)
1373 *reset_value |= 0x80808080U;
1374
1375 if (extra_value)
1376 *reset_value |= 0x40404040U;
1377 }
1378
1379 void evergreen_do_fast_color_clear(struct r600_common_context *rctx,
1380 struct pipe_framebuffer_state *fb,
1381 struct r600_atom *fb_state,
1382 unsigned *buffers, unsigned *dirty_cbufs,
1383 const union pipe_color_union *color)
1384 {
1385 int i;
1386
1387 if (rctx->render_cond)
1388 return;
1389
1390 for (i = 0; i < fb->nr_cbufs; i++) {
1391 struct r600_texture *tex;
1392 unsigned clear_bit = PIPE_CLEAR_COLOR0 << i;
1393
1394 if (!fb->cbufs[i])
1395 continue;
1396
1397 /* if this colorbuffer is not being cleared */
1398 if (!(*buffers & clear_bit))
1399 continue;
1400
1401 tex = (struct r600_texture *)fb->cbufs[i]->texture;
1402
1403 /* 128-bit formats are unusupported */
1404 if (util_format_get_blocksizebits(fb->cbufs[i]->format) > 64) {
1405 continue;
1406 }
1407
1408 /* the clear is allowed if all layers are bound */
1409 if (fb->cbufs[i]->u.tex.first_layer != 0 ||
1410 fb->cbufs[i]->u.tex.last_layer != util_max_layer(&tex->resource.b.b, 0)) {
1411 continue;
1412 }
1413
1414 /* cannot clear mipmapped textures */
1415 if (fb->cbufs[i]->texture->last_level != 0) {
1416 continue;
1417 }
1418
1419 /* only supported on tiled surfaces */
1420 if (tex->surface.level[0].mode < RADEON_SURF_MODE_1D) {
1421 continue;
1422 }
1423
1424 /* fast color clear with 1D tiling doesn't work on old kernels and CIK */
1425 if (tex->surface.level[0].mode == RADEON_SURF_MODE_1D &&
1426 rctx->chip_class >= CIK &&
1427 rctx->screen->info.drm_major == 2 &&
1428 rctx->screen->info.drm_minor < 38) {
1429 continue;
1430 }
1431
1432 if (tex->dcc_buffer) {
1433 uint32_t reset_value;
1434 bool clear_words_needed;
1435
1436 if (rctx->screen->debug_flags & DBG_NO_DCC_CLEAR)
1437 continue;
1438
1439 vi_get_fast_clear_parameters(fb->cbufs[i]->format, color, &reset_value, &clear_words_needed);
1440
1441 rctx->clear_buffer(&rctx->b, &tex->dcc_buffer->b.b,
1442 0, tex->surface.dcc_size, reset_value, true);
1443
1444 if (clear_words_needed)
1445 tex->dirty_level_mask |= 1 << fb->cbufs[i]->u.tex.level;
1446 } else {
1447 /* Stoney/RB+ doesn't work with CMASK fast clear. */
1448 if (rctx->family == CHIP_STONEY)
1449 continue;
1450
1451 /* ensure CMASK is enabled */
1452 r600_texture_alloc_cmask_separate(rctx->screen, tex);
1453 if (tex->cmask.size == 0) {
1454 continue;
1455 }
1456
1457 /* Do the fast clear. */
1458 rctx->clear_buffer(&rctx->b, &tex->cmask_buffer->b.b,
1459 tex->cmask.offset, tex->cmask.size, 0, true);
1460
1461 tex->dirty_level_mask |= 1 << fb->cbufs[i]->u.tex.level;
1462 }
1463
1464 evergreen_set_clear_color(tex, fb->cbufs[i]->format, color);
1465
1466 if (dirty_cbufs)
1467 *dirty_cbufs |= 1 << i;
1468 rctx->set_atom_dirty(rctx, fb_state, true);
1469 *buffers &= ~clear_bit;
1470 }
1471 }
1472
1473 void r600_init_screen_texture_functions(struct r600_common_screen *rscreen)
1474 {
1475 rscreen->b.resource_from_handle = r600_texture_from_handle;
1476 rscreen->b.resource_get_handle = r600_texture_get_handle;
1477 }
1478
1479 void r600_init_context_texture_functions(struct r600_common_context *rctx)
1480 {
1481 rctx->b.create_surface = r600_create_surface;
1482 rctx->b.surface_destroy = r600_surface_destroy;
1483 }