gallium/radeon: use PRIX64 instead of PRIu64
[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_surface *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_surface *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->bankw, surface->bankh,
247 surface->tile_split,
248 surface->stencil_tile_split,
249 surface->mtilea,
250 surface->level[0].pitch_bytes,
251 (surface->flags & RADEON_SURF_SCANOUT) != 0);
252
253 return rscreen->ws->buffer_get_handle(resource->buf,
254 surface->level[0].pitch_bytes, whandle);
255 }
256
257 static void r600_texture_destroy(struct pipe_screen *screen,
258 struct pipe_resource *ptex)
259 {
260 struct r600_texture *rtex = (struct r600_texture*)ptex;
261 struct r600_resource *resource = &rtex->resource;
262
263 if (rtex->flushed_depth_texture)
264 pipe_resource_reference((struct pipe_resource **)&rtex->flushed_depth_texture, NULL);
265
266 pipe_resource_reference((struct pipe_resource**)&rtex->htile_buffer, NULL);
267 if (rtex->cmask_buffer != &rtex->resource) {
268 pipe_resource_reference((struct pipe_resource**)&rtex->cmask_buffer, NULL);
269 }
270 pb_reference(&resource->buf, NULL);
271 FREE(rtex);
272 }
273
274 static const struct u_resource_vtbl r600_texture_vtbl;
275
276 /* The number of samples can be specified independently of the texture. */
277 void r600_texture_get_fmask_info(struct r600_common_screen *rscreen,
278 struct r600_texture *rtex,
279 unsigned nr_samples,
280 struct r600_fmask_info *out)
281 {
282 /* FMASK is allocated like an ordinary texture. */
283 struct radeon_surface fmask = rtex->surface;
284
285 memset(out, 0, sizeof(*out));
286
287 fmask.bo_alignment = 0;
288 fmask.bo_size = 0;
289 fmask.nsamples = 1;
290 fmask.flags |= RADEON_SURF_FMASK;
291
292 /* Force 2D tiling if it wasn't set. This may occur when creating
293 * FMASK for MSAA resolve on R6xx. On R6xx, the single-sample
294 * destination buffer must have an FMASK too. */
295 fmask.flags = RADEON_SURF_CLR(fmask.flags, MODE);
296 fmask.flags |= RADEON_SURF_SET(RADEON_SURF_MODE_2D, MODE);
297
298 if (rscreen->chip_class >= SI) {
299 fmask.flags |= RADEON_SURF_HAS_TILE_MODE_INDEX;
300 }
301
302 switch (nr_samples) {
303 case 2:
304 case 4:
305 fmask.bpe = 1;
306 if (rscreen->chip_class <= CAYMAN) {
307 fmask.bankh = 4;
308 }
309 break;
310 case 8:
311 fmask.bpe = 4;
312 break;
313 default:
314 R600_ERR("Invalid sample count for FMASK allocation.\n");
315 return;
316 }
317
318 /* Overallocate FMASK on R600-R700 to fix colorbuffer corruption.
319 * This can be fixed by writing a separate FMASK allocator specifically
320 * for R600-R700 asics. */
321 if (rscreen->chip_class <= R700) {
322 fmask.bpe *= 2;
323 }
324
325 if (rscreen->ws->surface_init(rscreen->ws, &fmask)) {
326 R600_ERR("Got error in surface_init while allocating FMASK.\n");
327 return;
328 }
329
330 assert(fmask.level[0].mode == RADEON_SURF_MODE_2D);
331
332 out->slice_tile_max = (fmask.level[0].nblk_x * fmask.level[0].nblk_y) / 64;
333 if (out->slice_tile_max)
334 out->slice_tile_max -= 1;
335
336 out->tile_mode_index = fmask.tiling_index[0];
337 out->pitch = fmask.level[0].nblk_x;
338 out->bank_height = fmask.bankh;
339 out->alignment = MAX2(256, fmask.bo_alignment);
340 out->size = fmask.bo_size;
341 }
342
343 static void r600_texture_allocate_fmask(struct r600_common_screen *rscreen,
344 struct r600_texture *rtex)
345 {
346 r600_texture_get_fmask_info(rscreen, rtex,
347 rtex->resource.b.b.nr_samples, &rtex->fmask);
348
349 rtex->fmask.offset = align(rtex->size, rtex->fmask.alignment);
350 rtex->size = rtex->fmask.offset + rtex->fmask.size;
351 }
352
353 void r600_texture_get_cmask_info(struct r600_common_screen *rscreen,
354 struct r600_texture *rtex,
355 struct r600_cmask_info *out)
356 {
357 unsigned cmask_tile_width = 8;
358 unsigned cmask_tile_height = 8;
359 unsigned cmask_tile_elements = cmask_tile_width * cmask_tile_height;
360 unsigned element_bits = 4;
361 unsigned cmask_cache_bits = 1024;
362 unsigned num_pipes = rscreen->tiling_info.num_channels;
363 unsigned pipe_interleave_bytes = rscreen->tiling_info.group_bytes;
364
365 unsigned elements_per_macro_tile = (cmask_cache_bits / element_bits) * num_pipes;
366 unsigned pixels_per_macro_tile = elements_per_macro_tile * cmask_tile_elements;
367 unsigned sqrt_pixels_per_macro_tile = sqrt(pixels_per_macro_tile);
368 unsigned macro_tile_width = util_next_power_of_two(sqrt_pixels_per_macro_tile);
369 unsigned macro_tile_height = pixels_per_macro_tile / macro_tile_width;
370
371 unsigned pitch_elements = align(rtex->surface.npix_x, macro_tile_width);
372 unsigned height = align(rtex->surface.npix_y, macro_tile_height);
373
374 unsigned base_align = num_pipes * pipe_interleave_bytes;
375 unsigned slice_bytes =
376 ((pitch_elements * height * element_bits + 7) / 8) / cmask_tile_elements;
377
378 assert(macro_tile_width % 128 == 0);
379 assert(macro_tile_height % 128 == 0);
380
381 out->slice_tile_max = ((pitch_elements * height) / (128*128)) - 1;
382 out->alignment = MAX2(256, base_align);
383 out->size = rtex->surface.array_size * align(slice_bytes, base_align);
384 }
385
386 static void si_texture_get_cmask_info(struct r600_common_screen *rscreen,
387 struct r600_texture *rtex,
388 struct r600_cmask_info *out)
389 {
390 unsigned pipe_interleave_bytes = rscreen->tiling_info.group_bytes;
391 unsigned num_pipes = rscreen->tiling_info.num_channels;
392 unsigned cl_width, cl_height;
393
394 switch (num_pipes) {
395 case 2:
396 cl_width = 32;
397 cl_height = 16;
398 break;
399 case 4:
400 cl_width = 32;
401 cl_height = 32;
402 break;
403 case 8:
404 cl_width = 64;
405 cl_height = 32;
406 break;
407 case 16: /* Hawaii */
408 cl_width = 64;
409 cl_height = 64;
410 break;
411 default:
412 assert(0);
413 return;
414 }
415
416 unsigned base_align = num_pipes * pipe_interleave_bytes;
417
418 unsigned width = align(rtex->surface.npix_x, cl_width*8);
419 unsigned height = align(rtex->surface.npix_y, cl_height*8);
420 unsigned slice_elements = (width * height) / (8*8);
421
422 /* Each element of CMASK is a nibble. */
423 unsigned slice_bytes = slice_elements / 2;
424
425 out->slice_tile_max = (width * height) / (128*128);
426 if (out->slice_tile_max)
427 out->slice_tile_max -= 1;
428
429 out->alignment = MAX2(256, base_align);
430 out->size = rtex->surface.array_size * align(slice_bytes, base_align);
431 }
432
433 static void r600_texture_allocate_cmask(struct r600_common_screen *rscreen,
434 struct r600_texture *rtex)
435 {
436 if (rscreen->chip_class >= SI) {
437 si_texture_get_cmask_info(rscreen, rtex, &rtex->cmask);
438 } else {
439 r600_texture_get_cmask_info(rscreen, rtex, &rtex->cmask);
440 }
441
442 rtex->cmask.offset = align(rtex->size, rtex->cmask.alignment);
443 rtex->size = rtex->cmask.offset + rtex->cmask.size;
444
445 if (rscreen->chip_class >= SI)
446 rtex->cb_color_info |= SI_S_028C70_FAST_CLEAR(1);
447 else
448 rtex->cb_color_info |= EG_S_028C70_FAST_CLEAR(1);
449 }
450
451 static void r600_texture_alloc_cmask_separate(struct r600_common_screen *rscreen,
452 struct r600_texture *rtex)
453 {
454 if (rtex->cmask_buffer)
455 return;
456
457 assert(rtex->cmask.size == 0);
458
459 if (rscreen->chip_class >= SI) {
460 si_texture_get_cmask_info(rscreen, rtex, &rtex->cmask);
461 } else {
462 r600_texture_get_cmask_info(rscreen, rtex, &rtex->cmask);
463 }
464
465 rtex->cmask_buffer = (struct r600_resource *)
466 pipe_buffer_create(&rscreen->b, PIPE_BIND_CUSTOM,
467 PIPE_USAGE_DEFAULT, rtex->cmask.size);
468 if (rtex->cmask_buffer == NULL) {
469 rtex->cmask.size = 0;
470 return;
471 }
472
473 /* update colorbuffer state bits */
474 rtex->cmask.base_address_reg =
475 r600_resource_va(&rscreen->b, &rtex->cmask_buffer->b.b) >> 8;
476
477 if (rscreen->chip_class >= SI)
478 rtex->cb_color_info |= SI_S_028C70_FAST_CLEAR(1);
479 else
480 rtex->cb_color_info |= EG_S_028C70_FAST_CLEAR(1);
481 }
482
483 static unsigned si_texture_htile_alloc_size(struct r600_common_screen *rscreen,
484 struct r600_texture *rtex)
485 {
486 unsigned cl_width, cl_height, width, height;
487 unsigned slice_elements, slice_bytes, pipe_interleave_bytes, base_align;
488 unsigned num_pipes = rscreen->tiling_info.num_channels;
489
490 /* HTILE is broken with 1D tiling on old kernels and CIK. */
491 if (rtex->surface.level[0].mode == RADEON_SURF_MODE_1D &&
492 rscreen->chip_class >= CIK && rscreen->info.drm_minor < 38)
493 return 0;
494
495 switch (num_pipes) {
496 case 2:
497 cl_width = 32;
498 cl_height = 32;
499 break;
500 case 4:
501 cl_width = 64;
502 cl_height = 32;
503 break;
504 case 8:
505 cl_width = 64;
506 cl_height = 64;
507 break;
508 case 16:
509 cl_width = 128;
510 cl_height = 64;
511 break;
512 default:
513 assert(0);
514 return 0;
515 }
516
517 width = align(rtex->surface.npix_x, cl_width * 8);
518 height = align(rtex->surface.npix_y, cl_height * 8);
519
520 slice_elements = (width * height) / (8 * 8);
521 slice_bytes = slice_elements * 4;
522
523 pipe_interleave_bytes = rscreen->tiling_info.group_bytes;
524 base_align = num_pipes * pipe_interleave_bytes;
525
526 return rtex->surface.array_size * align(slice_bytes, base_align);
527 }
528
529 static unsigned r600_texture_htile_alloc_size(struct r600_common_screen *rscreen,
530 struct r600_texture *rtex)
531 {
532 unsigned sw = rtex->surface.level[0].nblk_x * rtex->surface.blk_w;
533 unsigned sh = rtex->surface.level[0].nblk_y * rtex->surface.blk_h;
534 unsigned npipes = rscreen->info.r600_num_tile_pipes;
535 unsigned htile_size;
536
537 /* XXX also use it for other texture targets */
538 if (rscreen->info.drm_minor < 26 ||
539 rtex->resource.b.b.target != PIPE_TEXTURE_2D ||
540 rtex->surface.level[0].nblk_x < 32 ||
541 rtex->surface.level[0].nblk_y < 32) {
542 return 0;
543 }
544
545 /* HW bug on R6xx. */
546 if (rscreen->chip_class == R600 &&
547 (rtex->surface.level[0].npix_x > 7680 ||
548 rtex->surface.level[0].npix_y > 7680))
549 return 0;
550
551 /* this alignment and htile size only apply to linear htile buffer */
552 sw = align(sw, 16 << 3);
553 sh = align(sh, npipes << 3);
554 htile_size = (sw >> 3) * (sh >> 3) * 4;
555 /* must be aligned with 2K * npipes */
556 htile_size = align(htile_size, (2 << 10) * npipes);
557 return htile_size;
558 }
559
560 static void r600_texture_allocate_htile(struct r600_common_screen *rscreen,
561 struct r600_texture *rtex)
562 {
563 unsigned htile_size;
564 if (rscreen->chip_class >= SI) {
565 htile_size = si_texture_htile_alloc_size(rscreen, rtex);
566 } else {
567 htile_size = r600_texture_htile_alloc_size(rscreen, rtex);
568 }
569
570 if (!htile_size)
571 return;
572
573 /* XXX don't allocate it separately */
574 rtex->htile_buffer = (struct r600_resource*)
575 pipe_buffer_create(&rscreen->b, PIPE_BIND_CUSTOM,
576 PIPE_USAGE_DEFAULT, htile_size);
577 if (rtex->htile_buffer == NULL) {
578 /* this is not a fatal error as we can still keep rendering
579 * without htile buffer */
580 R600_ERR("Failed to create buffer object for htile buffer.\n");
581 } else {
582 r600_screen_clear_buffer(rscreen, &rtex->htile_buffer->b.b, 0, htile_size, 0);
583 }
584 }
585
586 /* Common processing for r600_texture_create and r600_texture_from_handle */
587 static struct r600_texture *
588 r600_texture_create_object(struct pipe_screen *screen,
589 const struct pipe_resource *base,
590 unsigned pitch_in_bytes_override,
591 struct pb_buffer *buf,
592 struct radeon_surface *surface)
593 {
594 struct r600_texture *rtex;
595 struct r600_resource *resource;
596 struct r600_common_screen *rscreen = (struct r600_common_screen*)screen;
597 uint64_t va;
598
599 rtex = CALLOC_STRUCT(r600_texture);
600 if (rtex == NULL)
601 return NULL;
602
603 resource = &rtex->resource;
604 resource->b.b = *base;
605 resource->b.vtbl = &r600_texture_vtbl;
606 pipe_reference_init(&resource->b.b.reference, 1);
607 resource->b.b.screen = screen;
608 rtex->pitch_override = pitch_in_bytes_override;
609
610 /* don't include stencil-only formats which we don't support for rendering */
611 rtex->is_depth = util_format_has_depth(util_format_description(rtex->resource.b.b.format));
612
613 rtex->surface = *surface;
614 if (r600_setup_surface(screen, rtex, pitch_in_bytes_override)) {
615 FREE(rtex);
616 return NULL;
617 }
618
619 /* Tiled depth textures utilize the non-displayable tile order.
620 * This must be done after r600_setup_surface.
621 * Applies to R600-Cayman. */
622 rtex->non_disp_tiling = rtex->is_depth && rtex->surface.level[0].mode >= RADEON_SURF_MODE_1D;
623
624 if (rtex->is_depth) {
625 if (!(base->flags & (R600_RESOURCE_FLAG_TRANSFER |
626 R600_RESOURCE_FLAG_FLUSHED_DEPTH)) &&
627 (rscreen->debug_flags & DBG_HYPERZ)) {
628
629 r600_texture_allocate_htile(rscreen, rtex);
630 }
631 } else {
632 if (base->nr_samples > 1) {
633 if (!buf) {
634 r600_texture_allocate_fmask(rscreen, rtex);
635 r600_texture_allocate_cmask(rscreen, rtex);
636 rtex->cmask_buffer = &rtex->resource;
637 }
638 if (!rtex->fmask.size || !rtex->cmask.size) {
639 FREE(rtex);
640 return NULL;
641 }
642 }
643 }
644
645 /* Now create the backing buffer. */
646 if (!buf) {
647 if (!r600_init_resource(rscreen, resource, rtex->size,
648 rtex->surface.bo_alignment, TRUE)) {
649 FREE(rtex);
650 return NULL;
651 }
652 } else {
653 resource->buf = buf;
654 resource->cs_buf = rscreen->ws->buffer_get_cs_handle(buf);
655 resource->domains = rscreen->ws->buffer_get_initial_domain(resource->cs_buf);
656 }
657
658 if (rtex->cmask.size) {
659 /* Initialize the cmask to 0xCC (= compressed state). */
660 r600_screen_clear_buffer(rscreen, &rtex->cmask_buffer->b.b,
661 rtex->cmask.offset, rtex->cmask.size, 0xCCCCCCCC);
662 }
663
664 /* Initialize the CMASK base register value. */
665 va = r600_resource_va(&rscreen->b, &rtex->resource.b.b);
666 rtex->cmask.base_address_reg = (va + rtex->cmask.offset) >> 8;
667
668 if (rscreen->debug_flags & DBG_VM) {
669 fprintf(stderr, "VM start=0x%"PRIX64" end=0x%"PRIX64" | Texture %ix%ix%i, %i levels, %i samples, %s\n",
670 r600_resource_va(screen, &rtex->resource.b.b),
671 r600_resource_va(screen, &rtex->resource.b.b) + rtex->resource.buf->size,
672 base->width0, base->height0, util_max_layer(base, 0)+1, base->last_level+1,
673 base->nr_samples ? base->nr_samples : 1, util_format_short_name(base->format));
674 }
675
676 if (rscreen->debug_flags & DBG_TEX ||
677 (rtex->resource.b.b.last_level > 0 && rscreen->debug_flags & DBG_TEXMIP)) {
678 printf("Texture: npix_x=%u, npix_y=%u, npix_z=%u, blk_w=%u, "
679 "blk_h=%u, blk_d=%u, array_size=%u, last_level=%u, "
680 "bpe=%u, nsamples=%u, flags=0x%x, %s\n",
681 rtex->surface.npix_x, rtex->surface.npix_y,
682 rtex->surface.npix_z, rtex->surface.blk_w,
683 rtex->surface.blk_h, rtex->surface.blk_d,
684 rtex->surface.array_size, rtex->surface.last_level,
685 rtex->surface.bpe, rtex->surface.nsamples,
686 rtex->surface.flags, util_format_short_name(base->format));
687 for (int i = 0; i <= rtex->surface.last_level; i++) {
688 printf(" L %i: offset=%"PRIu64", slice_size=%"PRIu64", npix_x=%u, "
689 "npix_y=%u, npix_z=%u, nblk_x=%u, nblk_y=%u, "
690 "nblk_z=%u, pitch_bytes=%u, mode=%u\n",
691 i, rtex->surface.level[i].offset,
692 rtex->surface.level[i].slice_size,
693 u_minify(rtex->resource.b.b.width0, i),
694 u_minify(rtex->resource.b.b.height0, i),
695 u_minify(rtex->resource.b.b.depth0, i),
696 rtex->surface.level[i].nblk_x,
697 rtex->surface.level[i].nblk_y,
698 rtex->surface.level[i].nblk_z,
699 rtex->surface.level[i].pitch_bytes,
700 rtex->surface.level[i].mode);
701 }
702 if (rtex->surface.flags & RADEON_SURF_SBUFFER) {
703 for (int i = 0; i <= rtex->surface.last_level; i++) {
704 printf(" S %i: offset=%"PRIu64", slice_size=%"PRIu64", npix_x=%u, "
705 "npix_y=%u, npix_z=%u, nblk_x=%u, nblk_y=%u, "
706 "nblk_z=%u, pitch_bytes=%u, mode=%u\n",
707 i, rtex->surface.stencil_level[i].offset,
708 rtex->surface.stencil_level[i].slice_size,
709 u_minify(rtex->resource.b.b.width0, i),
710 u_minify(rtex->resource.b.b.height0, i),
711 u_minify(rtex->resource.b.b.depth0, i),
712 rtex->surface.stencil_level[i].nblk_x,
713 rtex->surface.stencil_level[i].nblk_y,
714 rtex->surface.stencil_level[i].nblk_z,
715 rtex->surface.stencil_level[i].pitch_bytes,
716 rtex->surface.stencil_level[i].mode);
717 }
718 }
719 }
720 return rtex;
721 }
722
723 static unsigned r600_choose_tiling(struct r600_common_screen *rscreen,
724 const struct pipe_resource *templ)
725 {
726 const struct util_format_description *desc = util_format_description(templ->format);
727
728 /* MSAA resources must be 2D tiled. */
729 if (templ->nr_samples > 1)
730 return RADEON_SURF_MODE_2D;
731
732 /* Transfer resources should be linear. */
733 if (templ->flags & R600_RESOURCE_FLAG_TRANSFER)
734 return RADEON_SURF_MODE_LINEAR_ALIGNED;
735
736 /* Handle common candidates for the linear mode.
737 * Compressed textures must always be tiled. */
738 if (!(templ->flags & R600_RESOURCE_FLAG_FORCE_TILING) &&
739 !util_format_is_compressed(templ->format)) {
740 /* Tiling doesn't work with the 422 (SUBSAMPLED) formats on R600+. */
741 if (desc->layout == UTIL_FORMAT_LAYOUT_SUBSAMPLED)
742 return RADEON_SURF_MODE_LINEAR_ALIGNED;
743
744 /* Cursors are linear on SI.
745 * (XXX double-check, maybe also use RADEON_SURF_SCANOUT) */
746 if (rscreen->chip_class >= SI &&
747 (templ->bind & PIPE_BIND_CURSOR))
748 return RADEON_SURF_MODE_LINEAR_ALIGNED;
749
750 if (templ->bind & PIPE_BIND_LINEAR)
751 return RADEON_SURF_MODE_LINEAR_ALIGNED;
752
753 /* Textures with a very small height are recommended to be linear. */
754 if (templ->target == PIPE_TEXTURE_1D ||
755 templ->target == PIPE_TEXTURE_1D_ARRAY ||
756 templ->height0 <= 4)
757 return RADEON_SURF_MODE_LINEAR_ALIGNED;
758
759 /* Textures likely to be mapped often. */
760 if (templ->usage == PIPE_USAGE_STAGING ||
761 templ->usage == PIPE_USAGE_STREAM)
762 return RADEON_SURF_MODE_LINEAR_ALIGNED;
763 }
764
765 /* Make small textures 1D tiled. */
766 if (templ->width0 <= 16 || templ->height0 <= 16)
767 return RADEON_SURF_MODE_1D;
768
769 /* The allocator will switch to 1D if needed. */
770 return RADEON_SURF_MODE_2D;
771 }
772
773 struct pipe_resource *r600_texture_create(struct pipe_screen *screen,
774 const struct pipe_resource *templ)
775 {
776 struct r600_common_screen *rscreen = (struct r600_common_screen*)screen;
777 struct radeon_surface surface = {0};
778 int r;
779
780 r = r600_init_surface(rscreen, &surface, templ,
781 r600_choose_tiling(rscreen, templ),
782 templ->flags & R600_RESOURCE_FLAG_FLUSHED_DEPTH);
783 if (r) {
784 return NULL;
785 }
786 r = rscreen->ws->surface_best(rscreen->ws, &surface);
787 if (r) {
788 return NULL;
789 }
790 return (struct pipe_resource *)r600_texture_create_object(screen, templ,
791 0, NULL, &surface);
792 }
793
794 static struct pipe_resource *r600_texture_from_handle(struct pipe_screen *screen,
795 const struct pipe_resource *templ,
796 struct winsys_handle *whandle)
797 {
798 struct r600_common_screen *rscreen = (struct r600_common_screen*)screen;
799 struct pb_buffer *buf = NULL;
800 unsigned stride = 0;
801 unsigned array_mode;
802 enum radeon_bo_layout micro, macro;
803 struct radeon_surface surface;
804 bool scanout;
805 int r;
806
807 /* Support only 2D textures without mipmaps */
808 if ((templ->target != PIPE_TEXTURE_2D && templ->target != PIPE_TEXTURE_RECT) ||
809 templ->depth0 != 1 || templ->last_level != 0)
810 return NULL;
811
812 buf = rscreen->ws->buffer_from_handle(rscreen->ws, whandle, &stride);
813 if (!buf)
814 return NULL;
815
816 rscreen->ws->buffer_get_tiling(buf, &micro, &macro,
817 &surface.bankw, &surface.bankh,
818 &surface.tile_split,
819 &surface.stencil_tile_split,
820 &surface.mtilea, &scanout);
821
822 if (macro == RADEON_LAYOUT_TILED)
823 array_mode = RADEON_SURF_MODE_2D;
824 else if (micro == RADEON_LAYOUT_TILED)
825 array_mode = RADEON_SURF_MODE_1D;
826 else
827 array_mode = RADEON_SURF_MODE_LINEAR_ALIGNED;
828
829 r = r600_init_surface(rscreen, &surface, templ, array_mode, false);
830 if (r) {
831 return NULL;
832 }
833
834 if (scanout)
835 surface.flags |= RADEON_SURF_SCANOUT;
836
837 return (struct pipe_resource *)r600_texture_create_object(screen, templ,
838 stride, buf, &surface);
839 }
840
841 bool r600_init_flushed_depth_texture(struct pipe_context *ctx,
842 struct pipe_resource *texture,
843 struct r600_texture **staging)
844 {
845 struct r600_texture *rtex = (struct r600_texture*)texture;
846 struct pipe_resource resource;
847 struct r600_texture **flushed_depth_texture = staging ?
848 staging : &rtex->flushed_depth_texture;
849
850 if (!staging && rtex->flushed_depth_texture)
851 return true; /* it's ready */
852
853 resource.target = texture->target;
854 resource.format = texture->format;
855 resource.width0 = texture->width0;
856 resource.height0 = texture->height0;
857 resource.depth0 = texture->depth0;
858 resource.array_size = texture->array_size;
859 resource.last_level = texture->last_level;
860 resource.nr_samples = texture->nr_samples;
861 resource.usage = staging ? PIPE_USAGE_STAGING : PIPE_USAGE_DEFAULT;
862 resource.bind = texture->bind & ~PIPE_BIND_DEPTH_STENCIL;
863 resource.flags = texture->flags | R600_RESOURCE_FLAG_FLUSHED_DEPTH;
864
865 if (staging)
866 resource.flags |= R600_RESOURCE_FLAG_TRANSFER;
867
868 *flushed_depth_texture = (struct r600_texture *)ctx->screen->resource_create(ctx->screen, &resource);
869 if (*flushed_depth_texture == NULL) {
870 R600_ERR("failed to create temporary texture to hold flushed depth\n");
871 return false;
872 }
873
874 (*flushed_depth_texture)->is_flushing_texture = TRUE;
875 (*flushed_depth_texture)->non_disp_tiling = false;
876 return true;
877 }
878
879 /**
880 * Initialize the pipe_resource descriptor to be of the same size as the box,
881 * which is supposed to hold a subregion of the texture "orig" at the given
882 * mipmap level.
883 */
884 static void r600_init_temp_resource_from_box(struct pipe_resource *res,
885 struct pipe_resource *orig,
886 const struct pipe_box *box,
887 unsigned level, unsigned flags)
888 {
889 memset(res, 0, sizeof(*res));
890 res->format = orig->format;
891 res->width0 = box->width;
892 res->height0 = box->height;
893 res->depth0 = 1;
894 res->array_size = 1;
895 res->usage = flags & R600_RESOURCE_FLAG_TRANSFER ? PIPE_USAGE_STAGING : PIPE_USAGE_DEFAULT;
896 res->flags = flags;
897
898 /* We must set the correct texture target and dimensions for a 3D box. */
899 if (box->depth > 1 && util_max_layer(orig, level) > 0)
900 res->target = orig->target;
901 else
902 res->target = PIPE_TEXTURE_2D;
903
904 switch (res->target) {
905 case PIPE_TEXTURE_1D_ARRAY:
906 case PIPE_TEXTURE_2D_ARRAY:
907 case PIPE_TEXTURE_CUBE_ARRAY:
908 res->array_size = box->depth;
909 break;
910 case PIPE_TEXTURE_3D:
911 res->depth0 = box->depth;
912 break;
913 default:;
914 }
915 }
916
917 static void *r600_texture_transfer_map(struct pipe_context *ctx,
918 struct pipe_resource *texture,
919 unsigned level,
920 unsigned usage,
921 const struct pipe_box *box,
922 struct pipe_transfer **ptransfer)
923 {
924 struct r600_common_context *rctx = (struct r600_common_context*)ctx;
925 struct r600_texture *rtex = (struct r600_texture*)texture;
926 struct r600_transfer *trans;
927 boolean use_staging_texture = FALSE;
928 struct r600_resource *buf;
929 unsigned offset = 0;
930 char *map;
931
932 /* We cannot map a tiled texture directly because the data is
933 * in a different order, therefore we do detiling using a blit.
934 *
935 * Also, use a temporary in GTT memory for read transfers, as
936 * the CPU is much happier reading out of cached system memory
937 * than uncached VRAM.
938 */
939 if (rtex->surface.level[level].mode >= RADEON_SURF_MODE_1D)
940 use_staging_texture = TRUE;
941
942 /* Untiled buffers in VRAM, which is slow for CPU reads */
943 if ((usage & PIPE_TRANSFER_READ) && !(usage & PIPE_TRANSFER_MAP_DIRECTLY) &&
944 (rtex->resource.domains == RADEON_DOMAIN_VRAM)) {
945 use_staging_texture = TRUE;
946 }
947
948 /* Use a staging texture for uploads if the underlying BO is busy. */
949 if (!(usage & PIPE_TRANSFER_READ) &&
950 (r600_rings_is_buffer_referenced(rctx, rtex->resource.cs_buf, RADEON_USAGE_READWRITE) ||
951 rctx->ws->buffer_is_busy(rtex->resource.buf, RADEON_USAGE_READWRITE))) {
952 use_staging_texture = TRUE;
953 }
954
955 if (texture->flags & R600_RESOURCE_FLAG_TRANSFER) {
956 use_staging_texture = FALSE;
957 }
958
959 if (use_staging_texture && (usage & PIPE_TRANSFER_MAP_DIRECTLY)) {
960 return NULL;
961 }
962
963 trans = CALLOC_STRUCT(r600_transfer);
964 if (trans == NULL)
965 return NULL;
966 trans->transfer.resource = texture;
967 trans->transfer.level = level;
968 trans->transfer.usage = usage;
969 trans->transfer.box = *box;
970
971 if (rtex->is_depth) {
972 struct r600_texture *staging_depth;
973
974 if (rtex->resource.b.b.nr_samples > 1) {
975 /* MSAA depth buffers need to be converted to single sample buffers.
976 *
977 * Mapping MSAA depth buffers can occur if ReadPixels is called
978 * with a multisample GLX visual.
979 *
980 * First downsample the depth buffer to a temporary texture,
981 * then decompress the temporary one to staging.
982 *
983 * Only the region being mapped is transfered.
984 */
985 struct pipe_resource resource;
986
987 r600_init_temp_resource_from_box(&resource, texture, box, level, 0);
988
989 if (!r600_init_flushed_depth_texture(ctx, &resource, &staging_depth)) {
990 R600_ERR("failed to create temporary texture to hold untiled copy\n");
991 FREE(trans);
992 return NULL;
993 }
994
995 if (usage & PIPE_TRANSFER_READ) {
996 struct pipe_resource *temp = ctx->screen->resource_create(ctx->screen, &resource);
997
998 r600_copy_region_with_blit(ctx, temp, 0, 0, 0, 0, texture, level, box);
999 rctx->blit_decompress_depth(ctx, (struct r600_texture*)temp, staging_depth,
1000 0, 0, 0, box->depth, 0, 0);
1001 pipe_resource_reference((struct pipe_resource**)&temp, NULL);
1002 }
1003 }
1004 else {
1005 /* XXX: only readback the rectangle which is being mapped? */
1006 /* XXX: when discard is true, no need to read back from depth texture */
1007 if (!r600_init_flushed_depth_texture(ctx, texture, &staging_depth)) {
1008 R600_ERR("failed to create temporary texture to hold untiled copy\n");
1009 FREE(trans);
1010 return NULL;
1011 }
1012
1013 rctx->blit_decompress_depth(ctx, rtex, staging_depth,
1014 level, level,
1015 box->z, box->z + box->depth - 1,
1016 0, 0);
1017
1018 offset = r600_texture_get_offset(staging_depth, level, box);
1019 }
1020
1021 trans->transfer.stride = staging_depth->surface.level[level].pitch_bytes;
1022 trans->transfer.layer_stride = staging_depth->surface.level[level].slice_size;
1023 trans->staging = (struct r600_resource*)staging_depth;
1024 } else if (use_staging_texture) {
1025 struct pipe_resource resource;
1026 struct r600_texture *staging;
1027
1028 r600_init_temp_resource_from_box(&resource, texture, box, level,
1029 R600_RESOURCE_FLAG_TRANSFER);
1030
1031 /* Create the temporary texture. */
1032 staging = (struct r600_texture*)ctx->screen->resource_create(ctx->screen, &resource);
1033 if (staging == NULL) {
1034 R600_ERR("failed to create temporary texture to hold untiled copy\n");
1035 FREE(trans);
1036 return NULL;
1037 }
1038 trans->staging = &staging->resource;
1039 trans->transfer.stride = staging->surface.level[0].pitch_bytes;
1040 trans->transfer.layer_stride = staging->surface.level[0].slice_size;
1041 if (usage & PIPE_TRANSFER_READ) {
1042 r600_copy_to_staging_texture(ctx, trans);
1043 }
1044 } else {
1045 /* the resource is mapped directly */
1046 trans->transfer.stride = rtex->surface.level[level].pitch_bytes;
1047 trans->transfer.layer_stride = rtex->surface.level[level].slice_size;
1048 offset = r600_texture_get_offset(rtex, level, box);
1049 }
1050
1051 if (trans->staging) {
1052 buf = trans->staging;
1053 if (!rtex->is_depth && !(usage & PIPE_TRANSFER_READ))
1054 usage |= PIPE_TRANSFER_UNSYNCHRONIZED;
1055 } else {
1056 buf = &rtex->resource;
1057 }
1058
1059 if (!(map = r600_buffer_map_sync_with_rings(rctx, buf, usage))) {
1060 pipe_resource_reference((struct pipe_resource**)&trans->staging, NULL);
1061 FREE(trans);
1062 return NULL;
1063 }
1064
1065 *ptransfer = &trans->transfer;
1066 return map + offset;
1067 }
1068
1069 static void r600_texture_transfer_unmap(struct pipe_context *ctx,
1070 struct pipe_transfer* transfer)
1071 {
1072 struct r600_transfer *rtransfer = (struct r600_transfer*)transfer;
1073 struct r600_common_context *rctx = (struct r600_common_context*)ctx;
1074 struct radeon_winsys_cs_handle *buf;
1075 struct pipe_resource *texture = transfer->resource;
1076 struct r600_texture *rtex = (struct r600_texture*)texture;
1077
1078 if (rtransfer->staging) {
1079 buf = rtransfer->staging->cs_buf;
1080 } else {
1081 buf = r600_resource(transfer->resource)->cs_buf;
1082 }
1083 rctx->ws->buffer_unmap(buf);
1084
1085 if ((transfer->usage & PIPE_TRANSFER_WRITE) && rtransfer->staging) {
1086 if (rtex->is_depth && rtex->resource.b.b.nr_samples <= 1) {
1087 ctx->resource_copy_region(ctx, texture, transfer->level,
1088 transfer->box.x, transfer->box.y, transfer->box.z,
1089 &rtransfer->staging->b.b, transfer->level,
1090 &transfer->box);
1091 } else {
1092 r600_copy_from_staging_texture(ctx, rtransfer);
1093 }
1094 }
1095
1096 if (rtransfer->staging)
1097 pipe_resource_reference((struct pipe_resource**)&rtransfer->staging, NULL);
1098
1099 FREE(transfer);
1100 }
1101
1102 static const struct u_resource_vtbl r600_texture_vtbl =
1103 {
1104 NULL, /* get_handle */
1105 r600_texture_destroy, /* resource_destroy */
1106 r600_texture_transfer_map, /* transfer_map */
1107 NULL, /* transfer_flush_region */
1108 r600_texture_transfer_unmap, /* transfer_unmap */
1109 NULL /* transfer_inline_write */
1110 };
1111
1112 struct pipe_surface *r600_create_surface_custom(struct pipe_context *pipe,
1113 struct pipe_resource *texture,
1114 const struct pipe_surface *templ,
1115 unsigned width, unsigned height)
1116 {
1117 struct r600_surface *surface = CALLOC_STRUCT(r600_surface);
1118
1119 if (surface == NULL)
1120 return NULL;
1121
1122 assert(templ->u.tex.first_layer <= util_max_layer(texture, templ->u.tex.level));
1123 assert(templ->u.tex.last_layer <= util_max_layer(texture, templ->u.tex.level));
1124
1125 pipe_reference_init(&surface->base.reference, 1);
1126 pipe_resource_reference(&surface->base.texture, texture);
1127 surface->base.context = pipe;
1128 surface->base.format = templ->format;
1129 surface->base.width = width;
1130 surface->base.height = height;
1131 surface->base.u = templ->u;
1132 return &surface->base;
1133 }
1134
1135 static struct pipe_surface *r600_create_surface(struct pipe_context *pipe,
1136 struct pipe_resource *tex,
1137 const struct pipe_surface *templ)
1138 {
1139 unsigned level = templ->u.tex.level;
1140
1141 return r600_create_surface_custom(pipe, tex, templ,
1142 u_minify(tex->width0, level),
1143 u_minify(tex->height0, level));
1144 }
1145
1146 static void r600_surface_destroy(struct pipe_context *pipe,
1147 struct pipe_surface *surface)
1148 {
1149 struct r600_surface *surf = (struct r600_surface*)surface;
1150 pipe_resource_reference((struct pipe_resource**)&surf->cb_buffer_fmask, NULL);
1151 pipe_resource_reference((struct pipe_resource**)&surf->cb_buffer_cmask, NULL);
1152 pipe_resource_reference(&surface->texture, NULL);
1153 FREE(surface);
1154 }
1155
1156 unsigned r600_translate_colorswap(enum pipe_format format)
1157 {
1158 const struct util_format_description *desc = util_format_description(format);
1159
1160 #define HAS_SWIZZLE(chan,swz) (desc->swizzle[chan] == UTIL_FORMAT_SWIZZLE_##swz)
1161
1162 if (format == PIPE_FORMAT_R11G11B10_FLOAT) /* isn't plain */
1163 return V_0280A0_SWAP_STD;
1164
1165 if (desc->layout != UTIL_FORMAT_LAYOUT_PLAIN)
1166 return ~0U;
1167
1168 switch (desc->nr_channels) {
1169 case 1:
1170 if (HAS_SWIZZLE(0,X))
1171 return V_0280A0_SWAP_STD; /* X___ */
1172 else if (HAS_SWIZZLE(3,X))
1173 return V_0280A0_SWAP_ALT_REV; /* ___X */
1174 break;
1175 case 2:
1176 if ((HAS_SWIZZLE(0,X) && HAS_SWIZZLE(1,Y)) ||
1177 (HAS_SWIZZLE(0,X) && HAS_SWIZZLE(1,NONE)) ||
1178 (HAS_SWIZZLE(0,NONE) && HAS_SWIZZLE(1,Y)))
1179 return V_0280A0_SWAP_STD; /* XY__ */
1180 else if ((HAS_SWIZZLE(0,Y) && HAS_SWIZZLE(1,X)) ||
1181 (HAS_SWIZZLE(0,Y) && HAS_SWIZZLE(1,NONE)) ||
1182 (HAS_SWIZZLE(0,NONE) && HAS_SWIZZLE(1,X)))
1183 return V_0280A0_SWAP_STD_REV; /* YX__ */
1184 else if (HAS_SWIZZLE(0,X) && HAS_SWIZZLE(3,Y))
1185 return V_0280A0_SWAP_ALT; /* X__Y */
1186 else if (HAS_SWIZZLE(0,Y) && HAS_SWIZZLE(3,X))
1187 return V_0280A0_SWAP_ALT_REV; /* Y__X */
1188 break;
1189 case 3:
1190 if (HAS_SWIZZLE(0,X))
1191 return V_0280A0_SWAP_STD; /* XYZ */
1192 else if (HAS_SWIZZLE(0,Z))
1193 return V_0280A0_SWAP_STD_REV; /* ZYX */
1194 break;
1195 case 4:
1196 /* check the middle channels, the 1st and 4th channel can be NONE */
1197 if (HAS_SWIZZLE(1,Y) && HAS_SWIZZLE(2,Z))
1198 return V_0280A0_SWAP_STD; /* XYZW */
1199 else if (HAS_SWIZZLE(1,Z) && HAS_SWIZZLE(2,Y))
1200 return V_0280A0_SWAP_STD_REV; /* WZYX */
1201 else if (HAS_SWIZZLE(1,Y) && HAS_SWIZZLE(2,X))
1202 return V_0280A0_SWAP_ALT; /* ZYXW */
1203 else if (HAS_SWIZZLE(1,X) && HAS_SWIZZLE(2,Y))
1204 return V_0280A0_SWAP_ALT_REV; /* WXYZ */
1205 break;
1206 }
1207 return ~0U;
1208 }
1209
1210 static void evergreen_set_clear_color(struct r600_texture *rtex,
1211 enum pipe_format surface_format,
1212 const union pipe_color_union *color)
1213 {
1214 union util_color uc;
1215
1216 memset(&uc, 0, sizeof(uc));
1217
1218 if (util_format_is_pure_uint(surface_format)) {
1219 util_format_write_4ui(surface_format, color->ui, 0, &uc, 0, 0, 0, 1, 1);
1220 } else if (util_format_is_pure_sint(surface_format)) {
1221 util_format_write_4i(surface_format, color->i, 0, &uc, 0, 0, 0, 1, 1);
1222 } else {
1223 util_pack_color(color->f, surface_format, &uc);
1224 }
1225
1226 memcpy(rtex->color_clear_value, &uc, 2 * sizeof(uint32_t));
1227 }
1228
1229 void evergreen_do_fast_color_clear(struct r600_common_context *rctx,
1230 struct pipe_framebuffer_state *fb,
1231 struct r600_atom *fb_state,
1232 unsigned *buffers,
1233 const union pipe_color_union *color)
1234 {
1235 int i;
1236
1237 if (rctx->current_render_cond)
1238 return;
1239
1240 for (i = 0; i < fb->nr_cbufs; i++) {
1241 struct r600_texture *tex;
1242 unsigned clear_bit = PIPE_CLEAR_COLOR0 << i;
1243
1244 if (!fb->cbufs[i])
1245 continue;
1246
1247 /* if this colorbuffer is not being cleared */
1248 if (!(*buffers & clear_bit))
1249 continue;
1250
1251 tex = (struct r600_texture *)fb->cbufs[i]->texture;
1252
1253 /* 128-bit formats are unusupported */
1254 if (util_format_get_blocksizebits(fb->cbufs[i]->format) > 64) {
1255 continue;
1256 }
1257
1258 /* the clear is allowed if all layers are bound */
1259 if (fb->cbufs[i]->u.tex.first_layer != 0 ||
1260 fb->cbufs[i]->u.tex.last_layer != util_max_layer(&tex->resource.b.b, 0)) {
1261 continue;
1262 }
1263
1264 /* cannot clear mipmapped textures */
1265 if (fb->cbufs[i]->texture->last_level != 0) {
1266 continue;
1267 }
1268
1269 /* only supported on tiled surfaces */
1270 if (tex->surface.level[0].mode < RADEON_SURF_MODE_1D) {
1271 continue;
1272 }
1273
1274 /* fast color clear with 1D tiling doesn't work on old kernels and CIK */
1275 if (tex->surface.level[0].mode == RADEON_SURF_MODE_1D &&
1276 rctx->chip_class >= CIK && rctx->screen->info.drm_minor < 38) {
1277 continue;
1278 }
1279
1280 /* ensure CMASK is enabled */
1281 r600_texture_alloc_cmask_separate(rctx->screen, tex);
1282 if (tex->cmask.size == 0) {
1283 continue;
1284 }
1285
1286 /* Do the fast clear. */
1287 evergreen_set_clear_color(tex, fb->cbufs[i]->format, color);
1288 rctx->clear_buffer(&rctx->b, &tex->cmask_buffer->b.b,
1289 tex->cmask.offset, tex->cmask.size, 0);
1290
1291 tex->dirty_level_mask |= 1 << fb->cbufs[i]->u.tex.level;
1292 fb_state->dirty = true;
1293 *buffers &= ~clear_bit;
1294 }
1295 }
1296
1297 void r600_init_screen_texture_functions(struct r600_common_screen *rscreen)
1298 {
1299 rscreen->b.resource_from_handle = r600_texture_from_handle;
1300 rscreen->b.resource_get_handle = r600_texture_get_handle;
1301 }
1302
1303 void r600_init_context_texture_functions(struct r600_common_context *rctx)
1304 {
1305 rctx->b.create_surface = r600_create_surface;
1306 rctx->b.surface_destroy = r600_surface_destroy;
1307 }