r600g: improve flushed depth texture handling v2
[mesa.git] / src / gallium / drivers / r600 / 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_formats.h"
28 #include "r600d.h"
29
30 #include <errno.h>
31 #include "util/u_format_s3tc.h"
32 #include "util/u_memory.h"
33
34 /* Copy from a full GPU texture to a transfer's staging one. */
35 static void r600_copy_to_staging_texture(struct pipe_context *ctx, struct r600_transfer *rtransfer)
36 {
37 struct pipe_transfer *transfer = (struct pipe_transfer*)rtransfer;
38 struct pipe_resource *texture = transfer->resource;
39
40 ctx->resource_copy_region(ctx, &rtransfer->staging->b.b,
41 0, 0, 0, 0, texture, transfer->level,
42 &transfer->box);
43 }
44
45
46 /* Copy from a transfer's staging texture to a full GPU one. */
47 static void r600_copy_from_staging_texture(struct pipe_context *ctx, struct r600_transfer *rtransfer)
48 {
49 struct pipe_transfer *transfer = (struct pipe_transfer*)rtransfer;
50 struct pipe_resource *texture = transfer->resource;
51 struct pipe_box sbox;
52
53 u_box_origin_2d(transfer->box.width, transfer->box.height, &sbox);
54
55 ctx->resource_copy_region(ctx, texture, transfer->level,
56 transfer->box.x, transfer->box.y, transfer->box.z,
57 &rtransfer->staging->b.b,
58 0, &sbox);
59 }
60
61 unsigned r600_texture_get_offset(struct r600_resource_texture *rtex,
62 unsigned level, unsigned layer)
63 {
64 unsigned offset = rtex->offset[level];
65
66 switch (rtex->resource.b.b.target) {
67 case PIPE_TEXTURE_3D:
68 case PIPE_TEXTURE_CUBE:
69 default:
70 return offset + layer * rtex->layer_size[level];
71 }
72 }
73
74 static unsigned r600_get_block_alignment(struct pipe_screen *screen,
75 enum pipe_format format,
76 unsigned array_mode)
77 {
78 struct r600_screen* rscreen = (struct r600_screen *)screen;
79 unsigned pixsize = util_format_get_blocksize(format);
80 int p_align;
81
82 switch(array_mode) {
83 case V_038000_ARRAY_1D_TILED_THIN1:
84 p_align = MAX2(8,
85 ((rscreen->tiling_info.group_bytes / 8 / pixsize)));
86 break;
87 case V_038000_ARRAY_2D_TILED_THIN1:
88 p_align = MAX2(rscreen->tiling_info.num_banks,
89 (((rscreen->tiling_info.group_bytes / 8 / pixsize)) *
90 rscreen->tiling_info.num_banks)) * 8;
91 break;
92 case V_038000_ARRAY_LINEAR_ALIGNED:
93 p_align = MAX2(64, rscreen->tiling_info.group_bytes / pixsize);
94 break;
95 case V_038000_ARRAY_LINEAR_GENERAL:
96 default:
97 p_align = rscreen->tiling_info.group_bytes / pixsize;
98 break;
99 }
100 return p_align;
101 }
102
103 static unsigned r600_get_height_alignment(struct pipe_screen *screen,
104 unsigned array_mode)
105 {
106 struct r600_screen* rscreen = (struct r600_screen *)screen;
107 int h_align;
108
109 switch (array_mode) {
110 case V_038000_ARRAY_2D_TILED_THIN1:
111 h_align = rscreen->tiling_info.num_channels * 8;
112 break;
113 case V_038000_ARRAY_1D_TILED_THIN1:
114 case V_038000_ARRAY_LINEAR_ALIGNED:
115 h_align = 8;
116 break;
117 case V_038000_ARRAY_LINEAR_GENERAL:
118 default:
119 h_align = 1;
120 break;
121 }
122 return h_align;
123 }
124
125 static unsigned r600_get_base_alignment(struct pipe_screen *screen,
126 enum pipe_format format,
127 unsigned array_mode)
128 {
129 struct r600_screen* rscreen = (struct r600_screen *)screen;
130 unsigned pixsize = util_format_get_blocksize(format);
131 int p_align = r600_get_block_alignment(screen, format, array_mode);
132 int h_align = r600_get_height_alignment(screen, array_mode);
133 int b_align;
134
135 switch (array_mode) {
136 case V_038000_ARRAY_2D_TILED_THIN1:
137 b_align = MAX2(rscreen->tiling_info.num_banks * rscreen->tiling_info.num_channels * 8 * 8 * pixsize,
138 p_align * pixsize * h_align);
139 break;
140 case V_038000_ARRAY_1D_TILED_THIN1:
141 case V_038000_ARRAY_LINEAR_ALIGNED:
142 case V_038000_ARRAY_LINEAR_GENERAL:
143 default:
144 b_align = rscreen->tiling_info.group_bytes;
145 break;
146 }
147 return b_align;
148 }
149
150 static unsigned mip_minify(unsigned size, unsigned level)
151 {
152 unsigned val;
153 val = u_minify(size, level);
154 if (level > 0)
155 val = util_next_power_of_two(val);
156 return val;
157 }
158
159 static unsigned r600_texture_get_nblocksx(struct pipe_screen *screen,
160 struct r600_resource_texture *rtex,
161 unsigned level)
162 {
163 struct pipe_resource *ptex = &rtex->resource.b.b;
164 unsigned nblocksx, block_align, width;
165 unsigned blocksize = util_format_get_blocksize(rtex->real_format);
166
167 if (rtex->pitch_override)
168 return rtex->pitch_override / blocksize;
169
170 width = mip_minify(ptex->width0, level);
171 nblocksx = util_format_get_nblocksx(rtex->real_format, width);
172
173 block_align = r600_get_block_alignment(screen, rtex->real_format,
174 rtex->array_mode[level]);
175 nblocksx = align(nblocksx, block_align);
176 return nblocksx;
177 }
178
179 static unsigned r600_texture_get_nblocksy(struct pipe_screen *screen,
180 struct r600_resource_texture *rtex,
181 unsigned level)
182 {
183 struct pipe_resource *ptex = &rtex->resource.b.b;
184 unsigned height, tile_height;
185
186 height = mip_minify(ptex->height0, level);
187 height = util_format_get_nblocksy(rtex->real_format, height);
188 tile_height = r600_get_height_alignment(screen,
189 rtex->array_mode[level]);
190
191 /* XXX Hack around an alignment issue. Less tests fail with this.
192 *
193 * The thing is depth-stencil buffers should be tiled, i.e.
194 * the alignment should be >=8. If I make them tiled, stencil starts
195 * working because it no longer overlaps with the depth buffer
196 * in memory, but texturing like drawpix-stencil breaks. */
197 if (util_format_is_depth_or_stencil(rtex->real_format) && tile_height < 8)
198 tile_height = 8;
199
200 height = align(height, tile_height);
201 return height;
202 }
203
204 static void r600_texture_set_array_mode(struct pipe_screen *screen,
205 struct r600_resource_texture *rtex,
206 unsigned level, unsigned array_mode)
207 {
208 struct pipe_resource *ptex = &rtex->resource.b.b;
209
210 switch (array_mode) {
211 case V_0280A0_ARRAY_LINEAR_GENERAL:
212 case V_0280A0_ARRAY_LINEAR_ALIGNED:
213 case V_0280A0_ARRAY_1D_TILED_THIN1:
214 default:
215 rtex->array_mode[level] = array_mode;
216 break;
217 case V_0280A0_ARRAY_2D_TILED_THIN1:
218 {
219 unsigned w, h, tile_height, tile_width;
220
221 tile_height = r600_get_height_alignment(screen, array_mode);
222 tile_width = r600_get_block_alignment(screen, rtex->real_format, array_mode);
223
224 w = mip_minify(ptex->width0, level);
225 h = mip_minify(ptex->height0, level);
226 if (w <= tile_width || h <= tile_height)
227 rtex->array_mode[level] = V_0280A0_ARRAY_1D_TILED_THIN1;
228 else
229 rtex->array_mode[level] = array_mode;
230 }
231 break;
232 }
233 }
234
235 static int r600_init_surface(struct radeon_surface *surface,
236 const struct pipe_resource *ptex,
237 unsigned array_mode, bool is_transfer)
238 {
239 surface->npix_x = ptex->width0;
240 surface->npix_y = ptex->height0;
241 surface->npix_z = ptex->depth0;
242 surface->blk_w = util_format_get_blockwidth(ptex->format);
243 surface->blk_h = util_format_get_blockheight(ptex->format);
244 surface->blk_d = 1;
245 surface->array_size = 1;
246 surface->last_level = ptex->last_level;
247 surface->bpe = util_format_get_blocksize(ptex->format);
248 /* align byte per element on dword */
249 if (surface->bpe == 3) {
250 surface->bpe = 4;
251 }
252 surface->nsamples = 1;
253 surface->flags = 0;
254 switch (array_mode) {
255 case V_038000_ARRAY_1D_TILED_THIN1:
256 surface->flags |= RADEON_SURF_SET(RADEON_SURF_MODE_1D, MODE);
257 break;
258 case V_038000_ARRAY_2D_TILED_THIN1:
259 surface->flags |= RADEON_SURF_SET(RADEON_SURF_MODE_2D, MODE);
260 break;
261 case V_038000_ARRAY_LINEAR_ALIGNED:
262 surface->flags |= RADEON_SURF_SET(RADEON_SURF_MODE_LINEAR_ALIGNED, MODE);
263 break;
264 case V_038000_ARRAY_LINEAR_GENERAL:
265 default:
266 surface->flags |= RADEON_SURF_SET(RADEON_SURF_MODE_LINEAR, MODE);
267 break;
268 }
269 switch (ptex->target) {
270 case PIPE_TEXTURE_1D:
271 surface->flags |= RADEON_SURF_SET(RADEON_SURF_TYPE_1D, TYPE);
272 break;
273 case PIPE_TEXTURE_RECT:
274 case PIPE_TEXTURE_2D:
275 surface->flags |= RADEON_SURF_SET(RADEON_SURF_TYPE_2D, TYPE);
276 break;
277 case PIPE_TEXTURE_3D:
278 surface->flags |= RADEON_SURF_SET(RADEON_SURF_TYPE_3D, TYPE);
279 break;
280 case PIPE_TEXTURE_1D_ARRAY:
281 surface->flags |= RADEON_SURF_SET(RADEON_SURF_TYPE_1D_ARRAY, TYPE);
282 surface->array_size = ptex->array_size;
283 break;
284 case PIPE_TEXTURE_2D_ARRAY:
285 surface->flags |= RADEON_SURF_SET(RADEON_SURF_TYPE_2D_ARRAY, TYPE);
286 surface->array_size = ptex->array_size;
287 break;
288 case PIPE_TEXTURE_CUBE:
289 surface->flags |= RADEON_SURF_SET(RADEON_SURF_TYPE_CUBEMAP, TYPE);
290 break;
291 case PIPE_BUFFER:
292 default:
293 return -EINVAL;
294 }
295 if (ptex->bind & PIPE_BIND_SCANOUT) {
296 surface->flags |= RADEON_SURF_SCANOUT;
297 }
298 if ((ptex->bind & PIPE_BIND_DEPTH_STENCIL) &&
299 util_format_is_depth_and_stencil(ptex->format) && !is_transfer) {
300 surface->flags |= RADEON_SURF_ZBUFFER;
301 surface->flags |= RADEON_SURF_SBUFFER;
302 }
303
304 return 0;
305 }
306
307 static int r600_setup_surface(struct pipe_screen *screen,
308 struct r600_resource_texture *rtex,
309 unsigned array_mode,
310 unsigned pitch_in_bytes_override)
311 {
312 struct pipe_resource *ptex = &rtex->resource.b.b;
313 struct r600_screen *rscreen = (struct r600_screen*)screen;
314 unsigned i;
315 int r;
316
317 r = rscreen->ws->surface_init(rscreen->ws, &rtex->surface);
318 if (r) {
319 return r;
320 }
321 rtex->size = rtex->surface.bo_size;
322 if (pitch_in_bytes_override && pitch_in_bytes_override != rtex->surface.level[0].pitch_bytes) {
323 /* old ddx on evergreen over estimate alignment for 1d, only 1 level
324 * for those
325 */
326 rtex->surface.level[0].nblk_x = pitch_in_bytes_override / rtex->surface.bpe;
327 rtex->surface.level[0].pitch_bytes = pitch_in_bytes_override;
328 rtex->surface.level[0].slice_size = pitch_in_bytes_override * rtex->surface.level[0].nblk_y;
329 if (rtex->surface.flags & RADEON_SURF_SBUFFER) {
330 rtex->surface.stencil_offset = rtex->surface.level[0].slice_size;
331 }
332 }
333 for (i = 0; i <= ptex->last_level; i++) {
334 rtex->offset[i] = rtex->surface.level[i].offset;
335 rtex->layer_size[i] = rtex->surface.level[i].slice_size;
336 rtex->pitch_in_bytes[i] = rtex->surface.level[i].pitch_bytes;
337 switch (rtex->surface.level[i].mode) {
338 case RADEON_SURF_MODE_LINEAR_ALIGNED:
339 rtex->array_mode[i] = V_038000_ARRAY_LINEAR_ALIGNED;
340 break;
341 case RADEON_SURF_MODE_1D:
342 rtex->array_mode[i] = V_038000_ARRAY_1D_TILED_THIN1;
343 break;
344 case RADEON_SURF_MODE_2D:
345 rtex->array_mode[i] = V_038000_ARRAY_2D_TILED_THIN1;
346 break;
347 default:
348 case RADEON_SURF_MODE_LINEAR:
349 rtex->array_mode[i] = 0;
350 break;
351 }
352 }
353 return 0;
354 }
355
356 static void r600_setup_miptree(struct pipe_screen *screen,
357 struct r600_resource_texture *rtex,
358 unsigned array_mode)
359 {
360 struct pipe_resource *ptex = &rtex->resource.b.b;
361 enum chip_class chipc = ((struct r600_screen*)screen)->chip_class;
362 unsigned size, layer_size, i, offset;
363 unsigned nblocksx, nblocksy;
364
365 for (i = 0, offset = 0; i <= ptex->last_level; i++) {
366 unsigned blocksize = util_format_get_blocksize(rtex->real_format);
367 unsigned base_align = r600_get_base_alignment(screen, rtex->real_format, array_mode);
368
369 r600_texture_set_array_mode(screen, rtex, i, array_mode);
370
371 nblocksx = r600_texture_get_nblocksx(screen, rtex, i);
372 nblocksy = r600_texture_get_nblocksy(screen, rtex, i);
373
374 if (chipc >= EVERGREEN && array_mode == V_038000_ARRAY_LINEAR_GENERAL)
375 layer_size = align(nblocksx, 64) * nblocksy * blocksize;
376 else
377 layer_size = nblocksx * nblocksy * blocksize;
378
379 if (ptex->target == PIPE_TEXTURE_CUBE) {
380 if (chipc >= R700)
381 size = layer_size * 8;
382 else
383 size = layer_size * 6;
384 }
385 else if (ptex->target == PIPE_TEXTURE_3D)
386 size = layer_size * u_minify(ptex->depth0, i);
387 else
388 size = layer_size * ptex->array_size;
389
390 /* align base image and start of miptree */
391 if ((i == 0) || (i == 1))
392 offset = align(offset, base_align);
393 rtex->offset[i] = offset;
394 rtex->layer_size[i] = layer_size;
395 rtex->pitch_in_blocks[i] = nblocksx; /* CB talks in elements */
396 rtex->pitch_in_bytes[i] = nblocksx * blocksize;
397
398 offset += size;
399 }
400 rtex->size = offset;
401 }
402
403 /* Figure out whether u_blitter will fallback to a transfer operation.
404 * If so, don't use a staging resource.
405 */
406 static boolean permit_hardware_blit(struct pipe_screen *screen,
407 const struct pipe_resource *res)
408 {
409 unsigned bind;
410
411 if (util_format_is_depth_or_stencil(res->format))
412 bind = PIPE_BIND_DEPTH_STENCIL;
413 else
414 bind = PIPE_BIND_RENDER_TARGET;
415
416 /* hackaround for S3TC */
417 if (util_format_is_compressed(res->format))
418 return TRUE;
419
420 if (!screen->is_format_supported(screen,
421 res->format,
422 res->target,
423 res->nr_samples,
424 bind))
425 return FALSE;
426
427 if (!screen->is_format_supported(screen,
428 res->format,
429 res->target,
430 res->nr_samples,
431 PIPE_BIND_SAMPLER_VIEW))
432 return FALSE;
433
434 return TRUE;
435 }
436
437 static boolean r600_texture_get_handle(struct pipe_screen* screen,
438 struct pipe_resource *ptex,
439 struct winsys_handle *whandle)
440 {
441 struct r600_resource_texture *rtex = (struct r600_resource_texture*)ptex;
442 struct r600_resource *resource = &rtex->resource;
443 struct radeon_surface *surface = &rtex->surface;
444 struct r600_screen *rscreen = (struct r600_screen*)screen;
445
446 rscreen->ws->buffer_set_tiling(resource->buf,
447 NULL,
448 surface->level[0].mode >= RADEON_SURF_MODE_1D ?
449 RADEON_LAYOUT_TILED : RADEON_LAYOUT_LINEAR,
450 surface->level[0].mode >= RADEON_SURF_MODE_2D ?
451 RADEON_LAYOUT_TILED : RADEON_LAYOUT_LINEAR,
452 surface->bankw, surface->bankh,
453 surface->tile_split,
454 surface->stencil_tile_split,
455 surface->mtilea,
456 rtex->pitch_in_bytes[0]);
457
458 return rscreen->ws->buffer_get_handle(resource->buf,
459 rtex->pitch_in_bytes[0], whandle);
460 }
461
462 static void r600_texture_destroy(struct pipe_screen *screen,
463 struct pipe_resource *ptex)
464 {
465 struct r600_resource_texture *rtex = (struct r600_resource_texture*)ptex;
466 struct r600_resource *resource = &rtex->resource;
467
468 if (rtex->flushed_depth_texture)
469 pipe_resource_reference((struct pipe_resource **)&rtex->flushed_depth_texture, NULL);
470
471 if (rtex->stencil)
472 pipe_resource_reference((struct pipe_resource **)&rtex->stencil, NULL);
473
474 pb_reference(&resource->buf, NULL);
475 FREE(rtex);
476 }
477
478 static const struct u_resource_vtbl r600_texture_vtbl =
479 {
480 r600_texture_get_handle, /* get_handle */
481 r600_texture_destroy, /* resource_destroy */
482 r600_texture_get_transfer, /* get_transfer */
483 r600_texture_transfer_destroy, /* transfer_destroy */
484 r600_texture_transfer_map, /* transfer_map */
485 NULL, /* transfer_flush_region */
486 r600_texture_transfer_unmap, /* transfer_unmap */
487 NULL /* transfer_inline_write */
488 };
489
490 static struct r600_resource_texture *
491 r600_texture_create_object(struct pipe_screen *screen,
492 const struct pipe_resource *base,
493 unsigned array_mode,
494 unsigned pitch_in_bytes_override,
495 unsigned max_buffer_size,
496 struct pb_buffer *buf,
497 boolean alloc_bo,
498 struct radeon_surface *surface)
499 {
500 struct r600_resource_texture *rtex;
501 struct r600_resource *resource;
502 struct r600_screen *rscreen = (struct r600_screen*)screen;
503 int r;
504
505 rtex = CALLOC_STRUCT(r600_resource_texture);
506 if (rtex == NULL)
507 return NULL;
508
509 resource = &rtex->resource;
510 resource->b.b = *base;
511 resource->b.vtbl = &r600_texture_vtbl;
512 pipe_reference_init(&resource->b.b.reference, 1);
513 resource->b.b.screen = screen;
514 rtex->pitch_override = pitch_in_bytes_override;
515 rtex->real_format = base->format;
516
517 /* We must split depth and stencil into two separate buffers on Evergreen. */
518 if ((base->bind & PIPE_BIND_DEPTH_STENCIL) &&
519 ((struct r600_screen*)screen)->chip_class >= EVERGREEN &&
520 util_format_is_depth_and_stencil(base->format) &&
521 !rscreen->use_surface_alloc) {
522 struct pipe_resource stencil;
523 unsigned stencil_pitch_override = 0;
524
525 switch (base->format) {
526 case PIPE_FORMAT_Z24_UNORM_S8_UINT:
527 rtex->real_format = PIPE_FORMAT_Z24X8_UNORM;
528 break;
529 case PIPE_FORMAT_S8_UINT_Z24_UNORM:
530 rtex->real_format = PIPE_FORMAT_X8Z24_UNORM;
531 break;
532 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
533 rtex->real_format = PIPE_FORMAT_Z32_FLOAT;
534 break;
535 default:
536 assert(0);
537 FREE(rtex);
538 return NULL;
539 }
540
541 /* Divide the pitch in bytes by 4 for stencil, because it has a smaller pixel size. */
542 if (pitch_in_bytes_override) {
543 assert(base->format == PIPE_FORMAT_Z24_UNORM_S8_UINT ||
544 base->format == PIPE_FORMAT_S8_UINT_Z24_UNORM);
545 stencil_pitch_override = pitch_in_bytes_override / 4;
546 }
547
548 /* Allocate the stencil buffer. */
549 stencil = *base;
550 stencil.format = PIPE_FORMAT_S8_UINT;
551 rtex->stencil = r600_texture_create_object(screen, &stencil, array_mode,
552 stencil_pitch_override,
553 max_buffer_size, NULL, FALSE, surface);
554 if (!rtex->stencil) {
555 FREE(rtex);
556 return NULL;
557 }
558 /* Proceed in creating the depth buffer. */
559 }
560
561 /* only mark depth textures the HW can hit as depth textures */
562 if (util_format_is_depth_or_stencil(rtex->real_format) &&
563 permit_hardware_blit(screen, base))
564 rtex->is_depth = true;
565
566 r600_setup_miptree(screen, rtex, array_mode);
567 if (rscreen->use_surface_alloc) {
568 rtex->surface = *surface;
569 r = r600_setup_surface(screen, rtex, array_mode,
570 pitch_in_bytes_override);
571 if (r) {
572 FREE(rtex);
573 return NULL;
574 }
575 }
576
577 /* If we initialized separate stencil for Evergreen. place it after depth. */
578 if (rtex->stencil) {
579 unsigned stencil_align, stencil_offset;
580
581 stencil_align = r600_get_base_alignment(screen, rtex->stencil->real_format, array_mode);
582 stencil_offset = align(rtex->size, stencil_align);
583
584 for (unsigned i = 0; i <= rtex->stencil->resource.b.b.last_level; i++)
585 rtex->stencil->offset[i] += stencil_offset;
586
587 rtex->size = stencil_offset + rtex->stencil->size;
588 }
589
590 /* Now create the backing buffer. */
591 if (!buf && alloc_bo) {
592 struct pipe_resource *ptex = &rtex->resource.b.b;
593 unsigned base_align = r600_get_base_alignment(screen, ptex->format, array_mode);
594
595 if (rscreen->use_surface_alloc) {
596 base_align = rtex->surface.bo_alignment;
597 } else if (util_format_is_depth_or_stencil(rtex->real_format)) {
598 /* ugly work around depth buffer need stencil room at end of bo */
599 rtex->size += ptex->width0 * ptex->height0;
600 }
601 if (!r600_init_resource(rscreen, resource, rtex->size, base_align, base->bind, base->usage)) {
602 pipe_resource_reference((struct pipe_resource**)&rtex->stencil, NULL);
603 FREE(rtex);
604 return NULL;
605 }
606 } else if (buf) {
607 resource->buf = buf;
608 resource->cs_buf = rscreen->ws->buffer_get_cs_handle(buf);
609 resource->domains = RADEON_DOMAIN_GTT | RADEON_DOMAIN_VRAM;
610 }
611
612 if (rtex->stencil) {
613 pb_reference(&rtex->stencil->resource.buf, rtex->resource.buf);
614 rtex->stencil->resource.cs_buf = rtex->resource.cs_buf;
615 rtex->stencil->resource.domains = rtex->resource.domains;
616 }
617 return rtex;
618 }
619
620 struct pipe_resource *r600_texture_create(struct pipe_screen *screen,
621 const struct pipe_resource *templ)
622 {
623 struct r600_screen *rscreen = (struct r600_screen*)screen;
624 struct radeon_surface surface;
625 unsigned array_mode = 0;
626 int r;
627
628 if (!(templ->flags & R600_RESOURCE_FLAG_TRANSFER)) {
629 if (rscreen->use_surface_alloc &&
630 !(templ->bind & PIPE_BIND_SCANOUT) &&
631 templ->usage != PIPE_USAGE_STAGING &&
632 templ->usage != PIPE_USAGE_STREAM &&
633 permit_hardware_blit(screen, templ)) {
634 array_mode = V_038000_ARRAY_2D_TILED_THIN1;
635 } else if (util_format_is_compressed(templ->format)) {
636 array_mode = V_038000_ARRAY_1D_TILED_THIN1;
637 }
638 }
639
640 r = r600_init_surface(&surface, templ, array_mode,
641 templ->flags & R600_RESOURCE_FLAG_TRANSFER);
642 if (r) {
643 return NULL;
644 }
645 r = rscreen->ws->surface_best(rscreen->ws, &surface);
646 if (r) {
647 return NULL;
648 }
649 return (struct pipe_resource *)r600_texture_create_object(screen, templ, array_mode,
650 0, 0, NULL, TRUE, &surface);
651 }
652
653 static struct pipe_surface *r600_create_surface(struct pipe_context *pipe,
654 struct pipe_resource *texture,
655 const struct pipe_surface *surf_tmpl)
656 {
657 struct r600_resource_texture *rtex = (struct r600_resource_texture*)texture;
658 struct r600_surface *surface = CALLOC_STRUCT(r600_surface);
659 unsigned level = surf_tmpl->u.tex.level;
660
661 assert(surf_tmpl->u.tex.first_layer == surf_tmpl->u.tex.last_layer);
662 if (surface == NULL)
663 return NULL;
664 pipe_reference_init(&surface->base.reference, 1);
665 pipe_resource_reference(&surface->base.texture, texture);
666 surface->base.context = pipe;
667 surface->base.format = surf_tmpl->format;
668 surface->base.width = mip_minify(texture->width0, level);
669 surface->base.height = mip_minify(texture->height0, level);
670 surface->base.usage = surf_tmpl->usage;
671 surface->base.texture = texture;
672 surface->base.u.tex.first_layer = surf_tmpl->u.tex.first_layer;
673 surface->base.u.tex.last_layer = surf_tmpl->u.tex.last_layer;
674 surface->base.u.tex.level = level;
675
676 surface->aligned_height = r600_texture_get_nblocksy(pipe->screen,
677 rtex, level);
678 return &surface->base;
679 }
680
681 static void r600_surface_destroy(struct pipe_context *pipe,
682 struct pipe_surface *surface)
683 {
684 pipe_resource_reference(&surface->texture, NULL);
685 FREE(surface);
686 }
687
688 struct pipe_resource *r600_texture_from_handle(struct pipe_screen *screen,
689 const struct pipe_resource *templ,
690 struct winsys_handle *whandle)
691 {
692 struct r600_screen *rscreen = (struct r600_screen*)screen;
693 struct pb_buffer *buf = NULL;
694 unsigned stride = 0;
695 unsigned array_mode = 0;
696 enum radeon_bo_layout micro, macro;
697 struct radeon_surface surface;
698 int r;
699
700 /* Support only 2D textures without mipmaps */
701 if ((templ->target != PIPE_TEXTURE_2D && templ->target != PIPE_TEXTURE_RECT) ||
702 templ->depth0 != 1 || templ->last_level != 0)
703 return NULL;
704
705 buf = rscreen->ws->buffer_from_handle(rscreen->ws, whandle, &stride);
706 if (!buf)
707 return NULL;
708
709 rscreen->ws->buffer_get_tiling(buf, &micro, &macro,
710 &surface.bankw, &surface.bankh,
711 &surface.tile_split,
712 &surface.stencil_tile_split,
713 &surface.mtilea);
714
715 if (macro == RADEON_LAYOUT_TILED)
716 array_mode = V_0280A0_ARRAY_2D_TILED_THIN1;
717 else if (micro == RADEON_LAYOUT_TILED)
718 array_mode = V_0280A0_ARRAY_1D_TILED_THIN1;
719 else
720 array_mode = 0;
721
722 r = r600_init_surface(&surface, templ, array_mode, 0);
723 if (r) {
724 return NULL;
725 }
726 return (struct pipe_resource *)r600_texture_create_object(screen, templ, array_mode,
727 stride, 0, buf, FALSE, &surface);
728 }
729
730 void r600_init_flushed_depth_texture(struct pipe_context *ctx,
731 struct pipe_resource *texture,
732 struct r600_resource_texture **staging)
733 {
734 struct r600_resource_texture *rtex = (struct r600_resource_texture*)texture;
735 struct pipe_resource resource;
736 struct r600_resource_texture **flushed_depth_texture = staging ?
737 staging : &rtex->flushed_depth_texture;
738
739 if (!staging && rtex->flushed_depth_texture)
740 return; /* it's ready */
741
742 resource.target = texture->target;
743 resource.format = texture->format;
744 resource.width0 = texture->width0;
745 resource.height0 = texture->height0;
746 resource.depth0 = texture->depth0;
747 resource.array_size = texture->array_size;
748 resource.last_level = texture->last_level;
749 resource.nr_samples = texture->nr_samples;
750 resource.usage = staging ? PIPE_USAGE_DYNAMIC : PIPE_USAGE_DEFAULT;
751 resource.bind = texture->bind & ~PIPE_BIND_DEPTH_STENCIL;
752 resource.flags = texture->flags;
753
754 if (staging)
755 resource.flags |= R600_RESOURCE_FLAG_TRANSFER;
756 else
757 rtex->dirty_db = TRUE;
758
759 *flushed_depth_texture = (struct r600_resource_texture *)ctx->screen->resource_create(ctx->screen, &resource);
760 if (*flushed_depth_texture == NULL) {
761 R600_ERR("failed to create temporary texture to hold flushed depth\n");
762 return;
763 }
764
765 (*flushed_depth_texture)->is_flushing_texture = TRUE;
766
767 }
768
769 void r600_texture_depth_flush(struct pipe_context *ctx,
770 struct pipe_resource *texture,
771 struct r600_resource_texture **staging)
772 {
773 struct r600_resource_texture *rtex = (struct r600_resource_texture*)texture;
774
775 r600_init_flushed_depth_texture(ctx, texture, staging);
776
777 if (staging) {
778 if (!*staging)
779 return; /* error */
780
781 r600_blit_uncompress_depth(ctx, rtex, *staging);
782 } else {
783 if (!rtex->flushed_depth_texture)
784 return; /* error */
785
786 r600_blit_uncompress_depth(ctx, rtex, NULL);
787 }
788 }
789
790 /* Needs adjustment for pixelformat:
791 */
792 static INLINE unsigned u_box_volume( const struct pipe_box *box )
793 {
794 return box->width * box->depth * box->height;
795 }
796
797 struct pipe_transfer* r600_texture_get_transfer(struct pipe_context *ctx,
798 struct pipe_resource *texture,
799 unsigned level,
800 unsigned usage,
801 const struct pipe_box *box)
802 {
803 struct r600_context *rctx = (struct r600_context*)ctx;
804 struct r600_resource_texture *rtex = (struct r600_resource_texture*)texture;
805 struct pipe_resource resource;
806 struct r600_transfer *trans;
807 boolean use_staging_texture = FALSE;
808
809 /* We cannot map a tiled texture directly because the data is
810 * in a different order, therefore we do detiling using a blit.
811 *
812 * Also, use a temporary in GTT memory for read transfers, as
813 * the CPU is much happier reading out of cached system memory
814 * than uncached VRAM.
815 */
816 if (R600_TEX_IS_TILED(rtex, level)) {
817 use_staging_texture = TRUE;
818 }
819
820 if ((usage & PIPE_TRANSFER_READ) && u_box_volume(box) > 1024)
821 use_staging_texture = TRUE;
822
823 /* Use a staging texture for uploads if the underlying BO is busy. */
824 if (!(usage & PIPE_TRANSFER_READ) &&
825 (rctx->ws->cs_is_buffer_referenced(rctx->cs, rtex->resource.cs_buf, RADEON_USAGE_READWRITE) ||
826 rctx->ws->buffer_is_busy(rtex->resource.buf, RADEON_USAGE_READWRITE))) {
827 use_staging_texture = TRUE;
828 }
829
830 if (!permit_hardware_blit(ctx->screen, texture) ||
831 (texture->flags & R600_RESOURCE_FLAG_TRANSFER)) {
832 use_staging_texture = FALSE;
833 }
834
835 if (use_staging_texture && (usage & PIPE_TRANSFER_MAP_DIRECTLY)) {
836 return NULL;
837 }
838
839 trans = CALLOC_STRUCT(r600_transfer);
840 if (trans == NULL)
841 return NULL;
842 pipe_resource_reference(&trans->transfer.resource, texture);
843 trans->transfer.level = level;
844 trans->transfer.usage = usage;
845 trans->transfer.box = *box;
846 if (rtex->is_depth) {
847 /* XXX: only readback the rectangle which is being mapped?
848 */
849 /* XXX: when discard is true, no need to read back from depth texture
850 */
851 struct r600_resource_texture *staging_depth;
852
853 r600_texture_depth_flush(ctx, texture, &staging_depth);
854 if (!staging_depth) {
855 R600_ERR("failed to create temporary texture to hold untiled copy\n");
856 pipe_resource_reference(&trans->transfer.resource, NULL);
857 FREE(trans);
858 return NULL;
859 }
860 trans->transfer.stride = staging_depth->pitch_in_bytes[level];
861 trans->offset = r600_texture_get_offset(staging_depth, level, box->z);
862 trans->staging = (struct r600_resource*)staging_depth;
863 return &trans->transfer;
864 } else if (use_staging_texture) {
865 resource.target = PIPE_TEXTURE_2D;
866 resource.format = texture->format;
867 resource.width0 = box->width;
868 resource.height0 = box->height;
869 resource.depth0 = 1;
870 resource.array_size = 1;
871 resource.last_level = 0;
872 resource.nr_samples = 0;
873 resource.usage = PIPE_USAGE_STAGING;
874 resource.bind = 0;
875 resource.flags = R600_RESOURCE_FLAG_TRANSFER;
876 /* For texture reading, the temporary (detiled) texture is used as
877 * a render target when blitting from a tiled texture. */
878 if (usage & PIPE_TRANSFER_READ) {
879 resource.bind |= PIPE_BIND_RENDER_TARGET;
880 }
881 /* For texture writing, the temporary texture is used as a sampler
882 * when blitting into a tiled texture. */
883 if (usage & PIPE_TRANSFER_WRITE) {
884 resource.bind |= PIPE_BIND_SAMPLER_VIEW;
885 }
886 /* Create the temporary texture. */
887 trans->staging = (struct r600_resource*)ctx->screen->resource_create(ctx->screen, &resource);
888 if (trans->staging == NULL) {
889 R600_ERR("failed to create temporary texture to hold untiled copy\n");
890 pipe_resource_reference(&trans->transfer.resource, NULL);
891 FREE(trans);
892 return NULL;
893 }
894
895 trans->transfer.stride =
896 ((struct r600_resource_texture *)trans->staging)->pitch_in_bytes[0];
897 if (usage & PIPE_TRANSFER_READ) {
898 r600_copy_to_staging_texture(ctx, trans);
899 /* Always referenced in the blit. */
900 r600_flush(ctx, NULL, 0);
901 }
902 return &trans->transfer;
903 }
904 trans->transfer.stride = rtex->pitch_in_bytes[level];
905 trans->transfer.layer_stride = rtex->layer_size[level];
906 trans->offset = r600_texture_get_offset(rtex, level, box->z);
907 return &trans->transfer;
908 }
909
910 void r600_texture_transfer_destroy(struct pipe_context *ctx,
911 struct pipe_transfer *transfer)
912 {
913 struct r600_transfer *rtransfer = (struct r600_transfer*)transfer;
914 struct pipe_resource *texture = transfer->resource;
915 struct r600_resource_texture *rtex = (struct r600_resource_texture*)texture;
916
917 if (rtex->is_depth) {
918 if ((transfer->usage & PIPE_TRANSFER_WRITE) && rtransfer->staging) {
919 struct pipe_box sbox;
920
921 u_box_origin_2d(texture->width0, texture->height0, &sbox);
922
923 ctx->resource_copy_region(ctx, texture, 0, 0, 0, 0,
924 &rtransfer->staging->b.b, 0,
925 &sbox);
926 }
927 } else if (rtransfer->staging) {
928 if (transfer->usage & PIPE_TRANSFER_WRITE) {
929 r600_copy_from_staging_texture(ctx, rtransfer);
930 }
931 }
932
933 if (rtransfer->staging)
934 pipe_resource_reference((struct pipe_resource**)&rtransfer->staging, NULL);
935
936 pipe_resource_reference(&transfer->resource, NULL);
937 FREE(transfer);
938 }
939
940 void* r600_texture_transfer_map(struct pipe_context *ctx,
941 struct pipe_transfer* transfer)
942 {
943 struct r600_context *rctx = (struct r600_context *)ctx;
944 struct r600_transfer *rtransfer = (struct r600_transfer*)transfer;
945 struct radeon_winsys_cs_handle *buf;
946 struct r600_resource_texture *rtex =
947 (struct r600_resource_texture*)transfer->resource;
948 enum pipe_format format = transfer->resource->format;
949 unsigned offset = 0;
950 char *map;
951
952 if ((transfer->resource->bind & PIPE_BIND_GLOBAL) && transfer->resource->target == PIPE_BUFFER) {
953 return r600_compute_global_transfer_map(ctx, transfer);
954 }
955
956 if (rtransfer->staging) {
957 buf = ((struct r600_resource *)rtransfer->staging)->cs_buf;
958 } else {
959 buf = ((struct r600_resource *)transfer->resource)->cs_buf;
960 }
961
962 if (rtex->is_depth || !rtransfer->staging)
963 offset = rtransfer->offset +
964 transfer->box.y / util_format_get_blockheight(format) * transfer->stride +
965 transfer->box.x / util_format_get_blockwidth(format) * util_format_get_blocksize(format);
966
967 if (!(map = rctx->ws->buffer_map(buf, rctx->cs, transfer->usage))) {
968 return NULL;
969 }
970
971 return map + offset;
972 }
973
974 void r600_texture_transfer_unmap(struct pipe_context *ctx,
975 struct pipe_transfer* transfer)
976 {
977 struct r600_transfer *rtransfer = (struct r600_transfer*)transfer;
978 struct r600_context *rctx = (struct r600_context*)ctx;
979 struct radeon_winsys_cs_handle *buf;
980
981 if ((transfer->resource->bind & PIPE_BIND_GLOBAL) && transfer->resource->target == PIPE_BUFFER) {
982 return r600_compute_global_transfer_unmap(ctx, transfer);
983 }
984
985 if (rtransfer->staging) {
986 buf = ((struct r600_resource *)rtransfer->staging)->cs_buf;
987 } else {
988 buf = ((struct r600_resource *)transfer->resource)->cs_buf;
989 }
990 rctx->ws->buffer_unmap(buf);
991 }
992
993 void r600_init_surface_functions(struct r600_context *r600)
994 {
995 r600->context.create_surface = r600_create_surface;
996 r600->context.surface_destroy = r600_surface_destroy;
997 }
998
999 static unsigned r600_get_swizzle_combined(const unsigned char *swizzle_format,
1000 const unsigned char *swizzle_view)
1001 {
1002 unsigned i;
1003 unsigned char swizzle[4];
1004 unsigned result = 0;
1005 const uint32_t swizzle_shift[4] = {
1006 16, 19, 22, 25,
1007 };
1008 const uint32_t swizzle_bit[4] = {
1009 0, 1, 2, 3,
1010 };
1011
1012 if (swizzle_view) {
1013 util_format_compose_swizzles(swizzle_format, swizzle_view, swizzle);
1014 } else {
1015 memcpy(swizzle, swizzle_format, 4);
1016 }
1017
1018 /* Get swizzle. */
1019 for (i = 0; i < 4; i++) {
1020 switch (swizzle[i]) {
1021 case UTIL_FORMAT_SWIZZLE_Y:
1022 result |= swizzle_bit[1] << swizzle_shift[i];
1023 break;
1024 case UTIL_FORMAT_SWIZZLE_Z:
1025 result |= swizzle_bit[2] << swizzle_shift[i];
1026 break;
1027 case UTIL_FORMAT_SWIZZLE_W:
1028 result |= swizzle_bit[3] << swizzle_shift[i];
1029 break;
1030 case UTIL_FORMAT_SWIZZLE_0:
1031 result |= V_038010_SQ_SEL_0 << swizzle_shift[i];
1032 break;
1033 case UTIL_FORMAT_SWIZZLE_1:
1034 result |= V_038010_SQ_SEL_1 << swizzle_shift[i];
1035 break;
1036 default: /* UTIL_FORMAT_SWIZZLE_X */
1037 result |= swizzle_bit[0] << swizzle_shift[i];
1038 }
1039 }
1040 return result;
1041 }
1042
1043 /* texture format translate */
1044 uint32_t r600_translate_texformat(struct pipe_screen *screen,
1045 enum pipe_format format,
1046 const unsigned char *swizzle_view,
1047 uint32_t *word4_p, uint32_t *yuv_format_p)
1048 {
1049 uint32_t result = 0, word4 = 0, yuv_format = 0;
1050 const struct util_format_description *desc;
1051 boolean uniform = TRUE;
1052 static int r600_enable_s3tc = -1;
1053 bool is_srgb_valid = FALSE;
1054
1055 int i;
1056 const uint32_t sign_bit[4] = {
1057 S_038010_FORMAT_COMP_X(V_038010_SQ_FORMAT_COMP_SIGNED),
1058 S_038010_FORMAT_COMP_Y(V_038010_SQ_FORMAT_COMP_SIGNED),
1059 S_038010_FORMAT_COMP_Z(V_038010_SQ_FORMAT_COMP_SIGNED),
1060 S_038010_FORMAT_COMP_W(V_038010_SQ_FORMAT_COMP_SIGNED)
1061 };
1062 desc = util_format_description(format);
1063
1064 word4 |= r600_get_swizzle_combined(desc->swizzle, swizzle_view);
1065
1066 /* Colorspace (return non-RGB formats directly). */
1067 switch (desc->colorspace) {
1068 /* Depth stencil formats */
1069 case UTIL_FORMAT_COLORSPACE_ZS:
1070 switch (format) {
1071 case PIPE_FORMAT_Z16_UNORM:
1072 result = FMT_16;
1073 goto out_word4;
1074 case PIPE_FORMAT_X24S8_UINT:
1075 word4 |= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT);
1076 case PIPE_FORMAT_Z24X8_UNORM:
1077 case PIPE_FORMAT_Z24_UNORM_S8_UINT:
1078 result = FMT_8_24;
1079 goto out_word4;
1080 case PIPE_FORMAT_S8X24_UINT:
1081 word4 |= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT);
1082 case PIPE_FORMAT_X8Z24_UNORM:
1083 case PIPE_FORMAT_S8_UINT_Z24_UNORM:
1084 result = FMT_24_8;
1085 goto out_word4;
1086 case PIPE_FORMAT_S8_UINT:
1087 result = FMT_8;
1088 word4 |= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT);
1089 goto out_word4;
1090 case PIPE_FORMAT_Z32_FLOAT:
1091 result = FMT_32_FLOAT;
1092 goto out_word4;
1093 case PIPE_FORMAT_X32_S8X24_UINT:
1094 word4 |= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT);
1095 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
1096 result = FMT_X24_8_32_FLOAT;
1097 goto out_word4;
1098 default:
1099 goto out_unknown;
1100 }
1101
1102 case UTIL_FORMAT_COLORSPACE_YUV:
1103 yuv_format |= (1 << 30);
1104 switch (format) {
1105 case PIPE_FORMAT_UYVY:
1106 case PIPE_FORMAT_YUYV:
1107 default:
1108 break;
1109 }
1110 goto out_unknown; /* XXX */
1111
1112 case UTIL_FORMAT_COLORSPACE_SRGB:
1113 word4 |= S_038010_FORCE_DEGAMMA(1);
1114 break;
1115
1116 default:
1117 break;
1118 }
1119
1120 if (r600_enable_s3tc == -1) {
1121 struct r600_screen *rscreen = (struct r600_screen *)screen;
1122 if (rscreen->info.drm_minor >= 9)
1123 r600_enable_s3tc = 1;
1124 else
1125 r600_enable_s3tc = debug_get_bool_option("R600_ENABLE_S3TC", FALSE);
1126 }
1127
1128 if (desc->layout == UTIL_FORMAT_LAYOUT_RGTC) {
1129 if (!r600_enable_s3tc)
1130 goto out_unknown;
1131
1132 switch (format) {
1133 case PIPE_FORMAT_RGTC1_SNORM:
1134 case PIPE_FORMAT_LATC1_SNORM:
1135 word4 |= sign_bit[0];
1136 case PIPE_FORMAT_RGTC1_UNORM:
1137 case PIPE_FORMAT_LATC1_UNORM:
1138 result = FMT_BC4;
1139 goto out_word4;
1140 case PIPE_FORMAT_RGTC2_SNORM:
1141 case PIPE_FORMAT_LATC2_SNORM:
1142 word4 |= sign_bit[0] | sign_bit[1];
1143 case PIPE_FORMAT_RGTC2_UNORM:
1144 case PIPE_FORMAT_LATC2_UNORM:
1145 result = FMT_BC5;
1146 goto out_word4;
1147 default:
1148 goto out_unknown;
1149 }
1150 }
1151
1152 if (desc->layout == UTIL_FORMAT_LAYOUT_S3TC) {
1153
1154 if (!r600_enable_s3tc)
1155 goto out_unknown;
1156
1157 if (!util_format_s3tc_enabled) {
1158 goto out_unknown;
1159 }
1160
1161 switch (format) {
1162 case PIPE_FORMAT_DXT1_RGB:
1163 case PIPE_FORMAT_DXT1_RGBA:
1164 case PIPE_FORMAT_DXT1_SRGB:
1165 case PIPE_FORMAT_DXT1_SRGBA:
1166 result = FMT_BC1;
1167 is_srgb_valid = TRUE;
1168 goto out_word4;
1169 case PIPE_FORMAT_DXT3_RGBA:
1170 case PIPE_FORMAT_DXT3_SRGBA:
1171 result = FMT_BC2;
1172 is_srgb_valid = TRUE;
1173 goto out_word4;
1174 case PIPE_FORMAT_DXT5_RGBA:
1175 case PIPE_FORMAT_DXT5_SRGBA:
1176 result = FMT_BC3;
1177 is_srgb_valid = TRUE;
1178 goto out_word4;
1179 default:
1180 goto out_unknown;
1181 }
1182 }
1183
1184 if (desc->layout == UTIL_FORMAT_LAYOUT_SUBSAMPLED) {
1185 switch (format) {
1186 case PIPE_FORMAT_R8G8_B8G8_UNORM:
1187 case PIPE_FORMAT_G8R8_B8R8_UNORM:
1188 result = FMT_GB_GR;
1189 goto out_word4;
1190 case PIPE_FORMAT_G8R8_G8B8_UNORM:
1191 case PIPE_FORMAT_R8G8_R8B8_UNORM:
1192 result = FMT_BG_RG;
1193 goto out_word4;
1194 default:
1195 goto out_unknown;
1196 }
1197 }
1198
1199 if (format == PIPE_FORMAT_R9G9B9E5_FLOAT) {
1200 result = FMT_5_9_9_9_SHAREDEXP;
1201 goto out_word4;
1202 } else if (format == PIPE_FORMAT_R11G11B10_FLOAT) {
1203 result = FMT_10_11_11_FLOAT;
1204 goto out_word4;
1205 }
1206
1207
1208 for (i = 0; i < desc->nr_channels; i++) {
1209 if (desc->channel[i].type == UTIL_FORMAT_TYPE_SIGNED) {
1210 word4 |= sign_bit[i];
1211 }
1212 }
1213
1214 /* R8G8Bx_SNORM - XXX CxV8U8 */
1215
1216 /* See whether the components are of the same size. */
1217 for (i = 1; i < desc->nr_channels; i++) {
1218 uniform = uniform && desc->channel[0].size == desc->channel[i].size;
1219 }
1220
1221 /* Non-uniform formats. */
1222 if (!uniform) {
1223 if (desc->colorspace != UTIL_FORMAT_COLORSPACE_SRGB &&
1224 desc->channel[0].pure_integer)
1225 word4 |= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT);
1226 switch(desc->nr_channels) {
1227 case 3:
1228 if (desc->channel[0].size == 5 &&
1229 desc->channel[1].size == 6 &&
1230 desc->channel[2].size == 5) {
1231 result = FMT_5_6_5;
1232 goto out_word4;
1233 }
1234 goto out_unknown;
1235 case 4:
1236 if (desc->channel[0].size == 5 &&
1237 desc->channel[1].size == 5 &&
1238 desc->channel[2].size == 5 &&
1239 desc->channel[3].size == 1) {
1240 result = FMT_1_5_5_5;
1241 goto out_word4;
1242 }
1243 if (desc->channel[0].size == 10 &&
1244 desc->channel[1].size == 10 &&
1245 desc->channel[2].size == 10 &&
1246 desc->channel[3].size == 2) {
1247 result = FMT_2_10_10_10;
1248 goto out_word4;
1249 }
1250 goto out_unknown;
1251 }
1252 goto out_unknown;
1253 }
1254
1255 /* Find the first non-VOID channel. */
1256 for (i = 0; i < 4; i++) {
1257 if (desc->channel[i].type != UTIL_FORMAT_TYPE_VOID) {
1258 break;
1259 }
1260 }
1261
1262 if (i == 4)
1263 goto out_unknown;
1264
1265 /* uniform formats */
1266 switch (desc->channel[i].type) {
1267 case UTIL_FORMAT_TYPE_UNSIGNED:
1268 case UTIL_FORMAT_TYPE_SIGNED:
1269 #if 0
1270 if (!desc->channel[i].normalized &&
1271 desc->colorspace != UTIL_FORMAT_COLORSPACE_SRGB) {
1272 goto out_unknown;
1273 }
1274 #endif
1275 if (desc->colorspace != UTIL_FORMAT_COLORSPACE_SRGB &&
1276 desc->channel[i].pure_integer)
1277 word4 |= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT);
1278
1279 switch (desc->channel[i].size) {
1280 case 4:
1281 switch (desc->nr_channels) {
1282 case 2:
1283 result = FMT_4_4;
1284 goto out_word4;
1285 case 4:
1286 result = FMT_4_4_4_4;
1287 goto out_word4;
1288 }
1289 goto out_unknown;
1290 case 8:
1291 switch (desc->nr_channels) {
1292 case 1:
1293 result = FMT_8;
1294 goto out_word4;
1295 case 2:
1296 result = FMT_8_8;
1297 goto out_word4;
1298 case 4:
1299 result = FMT_8_8_8_8;
1300 is_srgb_valid = TRUE;
1301 goto out_word4;
1302 }
1303 goto out_unknown;
1304 case 16:
1305 switch (desc->nr_channels) {
1306 case 1:
1307 result = FMT_16;
1308 goto out_word4;
1309 case 2:
1310 result = FMT_16_16;
1311 goto out_word4;
1312 case 4:
1313 result = FMT_16_16_16_16;
1314 goto out_word4;
1315 }
1316 goto out_unknown;
1317 case 32:
1318 switch (desc->nr_channels) {
1319 case 1:
1320 result = FMT_32;
1321 goto out_word4;
1322 case 2:
1323 result = FMT_32_32;
1324 goto out_word4;
1325 case 4:
1326 result = FMT_32_32_32_32;
1327 goto out_word4;
1328 }
1329 }
1330 goto out_unknown;
1331
1332 case UTIL_FORMAT_TYPE_FLOAT:
1333 switch (desc->channel[i].size) {
1334 case 16:
1335 switch (desc->nr_channels) {
1336 case 1:
1337 result = FMT_16_FLOAT;
1338 goto out_word4;
1339 case 2:
1340 result = FMT_16_16_FLOAT;
1341 goto out_word4;
1342 case 4:
1343 result = FMT_16_16_16_16_FLOAT;
1344 goto out_word4;
1345 }
1346 goto out_unknown;
1347 case 32:
1348 switch (desc->nr_channels) {
1349 case 1:
1350 result = FMT_32_FLOAT;
1351 goto out_word4;
1352 case 2:
1353 result = FMT_32_32_FLOAT;
1354 goto out_word4;
1355 case 4:
1356 result = FMT_32_32_32_32_FLOAT;
1357 goto out_word4;
1358 }
1359 }
1360 goto out_unknown;
1361 }
1362
1363 out_word4:
1364
1365 if (desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB && !is_srgb_valid)
1366 return ~0;
1367 if (word4_p)
1368 *word4_p = word4;
1369 if (yuv_format_p)
1370 *yuv_format_p = yuv_format;
1371 return result;
1372 out_unknown:
1373 /* R600_ERR("Unable to handle texformat %d %s\n", format, util_format_name(format)); */
1374 return ~0;
1375 }