r600g: retrieve tiling info from kernel for shared buffers.
[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 <errno.h>
28 #include <pipe/p_screen.h>
29 #include <util/u_format.h>
30 #include <util/u_math.h>
31 #include <util/u_inlines.h>
32 #include <util/u_memory.h>
33 #include "state_tracker/drm_driver.h"
34 #include "r600_pipe.h"
35 #include "r600_resource.h"
36 #include "r600_state_inlines.h"
37 #include "r600d.h"
38 #include "r600_formats.h"
39
40 extern struct u_resource_vtbl r600_texture_vtbl;
41
42 /* Copy from a tiled texture to a detiled one. */
43 static void r600_copy_from_tiled_texture(struct pipe_context *ctx, struct r600_transfer *rtransfer)
44 {
45 struct pipe_transfer *transfer = (struct pipe_transfer*)rtransfer;
46 struct pipe_resource *texture = transfer->resource;
47 struct pipe_subresource subdst;
48
49 subdst.face = 0;
50 subdst.level = 0;
51 ctx->resource_copy_region(ctx, rtransfer->linear_texture,
52 subdst, 0, 0, 0, texture, transfer->sr,
53 transfer->box.x, transfer->box.y, transfer->box.z,
54 transfer->box.width, transfer->box.height);
55 }
56
57
58 /* Copy from a detiled texture to a tiled one. */
59 static void r600_copy_into_tiled_texture(struct pipe_context *ctx, struct r600_transfer *rtransfer)
60 {
61 struct pipe_transfer *transfer = (struct pipe_transfer*)rtransfer;
62 struct pipe_resource *texture = transfer->resource;
63 struct pipe_subresource subsrc;
64
65 subsrc.face = 0;
66 subsrc.level = 0;
67 ctx->resource_copy_region(ctx, texture, transfer->sr,
68 transfer->box.x, transfer->box.y, transfer->box.z,
69 rtransfer->linear_texture, subsrc,
70 0, 0, 0,
71 transfer->box.width, transfer->box.height);
72
73 ctx->flush(ctx, 0, NULL);
74 }
75
76 static unsigned r600_texture_get_offset(struct r600_resource_texture *rtex,
77 unsigned level, unsigned zslice,
78 unsigned face)
79 {
80 unsigned offset = rtex->offset[level];
81
82 switch (rtex->resource.base.b.target) {
83 case PIPE_TEXTURE_3D:
84 assert(face == 0);
85 return offset + zslice * rtex->layer_size[level];
86 case PIPE_TEXTURE_CUBE:
87 assert(zslice == 0);
88 return offset + face * rtex->layer_size[level];
89 default:
90 assert(zslice == 0 && face == 0);
91 return offset;
92 }
93 }
94
95 static unsigned r600_texture_get_stride(struct pipe_screen *screen,
96 struct r600_resource_texture *rtex,
97 unsigned level)
98 {
99 struct pipe_resource *ptex = &rtex->resource.base.b;
100 struct radeon *radeon = (struct radeon *)screen->winsys;
101 enum chip_class chipc = r600_get_family_class(radeon);
102 unsigned width, stride;
103
104 if (rtex->pitch_override)
105 return rtex->pitch_override;
106
107 width = u_minify(ptex->width0, level);
108
109 stride = util_format_get_stride(ptex->format, align(width, 64));
110 if (chipc == EVERGREEN)
111 stride = align(stride, 512);
112 else
113 stride = align(stride, 256);
114 return stride;
115 }
116
117 static unsigned r600_texture_get_nblocksy(struct pipe_screen *screen,
118 struct r600_resource_texture *rtex,
119 unsigned level)
120 {
121 struct pipe_resource *ptex = &rtex->resource.base.b;
122 unsigned height;
123
124 height = u_minify(ptex->height0, level);
125 height = util_next_power_of_two(height);
126 return util_format_get_nblocksy(ptex->format, height);
127 }
128
129 /* Get a width in pixels from a stride in bytes. */
130 static unsigned pitch_to_width(enum pipe_format format,
131 unsigned pitch_in_bytes)
132 {
133 return (pitch_in_bytes / util_format_get_blocksize(format)) *
134 util_format_get_blockwidth(format);
135 }
136
137 static void r600_setup_miptree(struct pipe_screen *screen,
138 struct r600_resource_texture *rtex)
139 {
140 struct pipe_resource *ptex = &rtex->resource.base.b;
141 struct radeon *radeon = (struct radeon *)screen->winsys;
142 enum chip_class chipc = r600_get_family_class(radeon);
143 unsigned pitch, size, layer_size, i, offset;
144 unsigned nblocksy;
145
146 for (i = 0, offset = 0; i <= ptex->last_level; i++) {
147 pitch = r600_texture_get_stride(screen, rtex, i);
148 nblocksy = r600_texture_get_nblocksy(screen, rtex, i);
149
150 layer_size = pitch * nblocksy;
151
152 if (ptex->target == PIPE_TEXTURE_CUBE) {
153 if (chipc >= R700)
154 size = layer_size * 8;
155 else
156 size = layer_size * 6;
157 }
158 else
159 size = layer_size * u_minify(ptex->depth0, i);
160 rtex->offset[i] = offset;
161 rtex->layer_size[i] = layer_size;
162 rtex->pitch_in_bytes[i] = pitch;
163 rtex->pitch_in_pixels[i] = pitch_to_width(ptex->format, pitch);
164 offset += size;
165 }
166 rtex->size = offset;
167 }
168
169 static struct r600_resource_texture *
170 r600_texture_create_object(struct pipe_screen *screen,
171 const struct pipe_resource *base,
172 unsigned array_mode,
173 unsigned pitch_in_bytes_override,
174 unsigned max_buffer_size,
175 struct r600_bo *bo)
176 {
177 struct r600_resource_texture *rtex;
178 struct r600_resource *resource;
179 struct radeon *radeon = (struct radeon *)screen->winsys;
180
181 rtex = CALLOC_STRUCT(r600_resource_texture);
182 if (rtex == NULL)
183 return NULL;
184
185 resource = &rtex->resource;
186 resource->base.b = *base;
187 resource->base.vtbl = &r600_texture_vtbl;
188 pipe_reference_init(&resource->base.b.reference, 1);
189 resource->base.b.screen = screen;
190 resource->bo = bo;
191 resource->domain = r600_domain_from_usage(resource->base.b.bind);
192 rtex->pitch_override = pitch_in_bytes_override;
193 rtex->array_mode = array_mode;
194
195 if (array_mode)
196 rtex->tiled = 1;
197 r600_setup_miptree(screen, rtex);
198
199 resource->size = rtex->size;
200
201 if (!resource->bo) {
202 resource->bo = r600_bo(radeon, rtex->size, 4096, 0);
203 if (!resource->bo) {
204 FREE(rtex);
205 return NULL;
206 }
207 }
208 return rtex;
209 }
210
211 struct pipe_resource *r600_texture_create(struct pipe_screen *screen,
212 const struct pipe_resource *templ)
213 {
214 unsigned array_mode = 0;
215
216 return (struct pipe_resource *)r600_texture_create_object(screen, templ, array_mode,
217 0, 0, NULL);
218
219 }
220
221 static void r600_texture_destroy(struct pipe_screen *screen,
222 struct pipe_resource *ptex)
223 {
224 struct r600_resource_texture *rtex = (struct r600_resource_texture*)ptex;
225 struct r600_resource *resource = &rtex->resource;
226 struct radeon *radeon = (struct radeon *)screen->winsys;
227
228 if (rtex->flushed_depth_texture)
229 pipe_resource_reference((struct pipe_resource **)&rtex->flushed_depth_texture, NULL);
230
231 if (resource->bo) {
232 r600_bo_reference(radeon, &resource->bo, NULL);
233 }
234 FREE(rtex);
235 }
236
237 static struct pipe_surface *r600_get_tex_surface(struct pipe_screen *screen,
238 struct pipe_resource *texture,
239 unsigned face, unsigned level,
240 unsigned zslice, unsigned flags)
241 {
242 struct r600_resource_texture *rtex = (struct r600_resource_texture*)texture;
243 struct pipe_surface *surface = CALLOC_STRUCT(pipe_surface);
244 unsigned offset;
245
246 if (surface == NULL)
247 return NULL;
248 offset = r600_texture_get_offset(rtex, level, zslice, face);
249 pipe_reference_init(&surface->reference, 1);
250 pipe_resource_reference(&surface->texture, texture);
251 surface->format = texture->format;
252 surface->width = u_minify(texture->width0, level);
253 surface->height = u_minify(texture->height0, level);
254 surface->offset = offset;
255 surface->usage = flags;
256 surface->zslice = zslice;
257 surface->texture = texture;
258 surface->face = face;
259 surface->level = level;
260 return surface;
261 }
262
263 static void r600_tex_surface_destroy(struct pipe_surface *surface)
264 {
265 pipe_resource_reference(&surface->texture, NULL);
266 FREE(surface);
267 }
268
269
270 struct pipe_resource *r600_texture_from_handle(struct pipe_screen *screen,
271 const struct pipe_resource *templ,
272 struct winsys_handle *whandle)
273 {
274 struct radeon *rw = (struct radeon*)screen->winsys;
275 struct r600_bo *bo = NULL;
276 unsigned array_mode = 0;
277
278 /* Support only 2D textures without mipmaps */
279 if ((templ->target != PIPE_TEXTURE_2D && templ->target != PIPE_TEXTURE_RECT) ||
280 templ->depth0 != 1 || templ->last_level != 0)
281 return NULL;
282
283 bo = r600_bo_handle(rw, whandle->handle, &array_mode);
284 if (bo == NULL) {
285 return NULL;
286 }
287
288 return (struct pipe_resource *)r600_texture_create_object(screen, templ, array_mode,
289 whandle->stride,
290 0,
291 bo);
292 }
293
294 static unsigned int r600_texture_is_referenced(struct pipe_context *context,
295 struct pipe_resource *texture,
296 unsigned face, unsigned level)
297 {
298 /* FIXME */
299 return PIPE_REFERENCED_FOR_READ | PIPE_REFERENCED_FOR_WRITE;
300 }
301
302 int (*r600_blit_uncompress_depth_ptr)(struct pipe_context *ctx, struct r600_resource_texture *texture);
303
304 int r600_texture_depth_flush(struct pipe_context *ctx,
305 struct pipe_resource *texture)
306 {
307 struct r600_resource_texture *rtex = (struct r600_resource_texture*)texture;
308 struct pipe_resource resource;
309
310 if (rtex->flushed_depth_texture)
311 goto out;
312
313 resource.target = PIPE_TEXTURE_2D;
314 resource.format = texture->format;
315 resource.width0 = texture->width0;
316 resource.height0 = texture->height0;
317 resource.depth0 = 1;
318 resource.last_level = 0;
319 resource.nr_samples = 0;
320 resource.usage = PIPE_USAGE_DYNAMIC;
321 resource.bind = 0;
322 resource.flags = 0;
323
324 resource.bind |= PIPE_BIND_DEPTH_STENCIL;
325
326 rtex->flushed_depth_texture = (struct r600_resource_texture *)ctx->screen->resource_create(ctx->screen, &resource);
327 if (rtex->flushed_depth_texture == NULL) {
328 R600_ERR("failed to create temporary texture to hold untiled copy\n");
329 return -ENOMEM;
330 }
331
332 out:
333 r600_blit_uncompress_depth_ptr(ctx, rtex);
334 return 0;
335 }
336
337 struct pipe_transfer* r600_texture_get_transfer(struct pipe_context *ctx,
338 struct pipe_resource *texture,
339 struct pipe_subresource sr,
340 unsigned usage,
341 const struct pipe_box *box)
342 {
343 struct r600_resource_texture *rtex = (struct r600_resource_texture*)texture;
344 struct pipe_resource resource;
345 struct r600_transfer *trans;
346 int r;
347
348 trans = CALLOC_STRUCT(r600_transfer);
349 if (trans == NULL)
350 return NULL;
351 pipe_resource_reference(&trans->transfer.resource, texture);
352 trans->transfer.sr = sr;
353 trans->transfer.usage = usage;
354 trans->transfer.box = *box;
355 if (rtex->depth) {
356 r = r600_texture_depth_flush(ctx, texture);
357 if (r < 0) {
358 R600_ERR("failed to create temporary texture to hold untiled copy\n");
359 pipe_resource_reference(&trans->transfer.resource, NULL);
360 FREE(trans);
361 return NULL;
362 }
363 } else if (rtex->tiled) {
364 resource.target = PIPE_TEXTURE_2D;
365 resource.format = texture->format;
366 resource.width0 = box->width;
367 resource.height0 = box->height;
368 resource.depth0 = 1;
369 resource.last_level = 0;
370 resource.nr_samples = 0;
371 resource.usage = PIPE_USAGE_DYNAMIC;
372 resource.bind = 0;
373 resource.flags = 0;
374 /* For texture reading, the temporary (detiled) texture is used as
375 * a render target when blitting from a tiled texture. */
376 if (usage & PIPE_TRANSFER_READ) {
377 resource.bind |= PIPE_BIND_RENDER_TARGET;
378 }
379 /* For texture writing, the temporary texture is used as a sampler
380 * when blitting into a tiled texture. */
381 if (usage & PIPE_TRANSFER_WRITE) {
382 resource.bind |= PIPE_BIND_SAMPLER_VIEW;
383 }
384 /* Create the temporary texture. */
385 trans->linear_texture = ctx->screen->resource_create(ctx->screen, &resource);
386 if (trans->linear_texture == NULL) {
387 R600_ERR("failed to create temporary texture to hold untiled copy\n");
388 pipe_resource_reference(&trans->transfer.resource, NULL);
389 FREE(trans);
390 return NULL;
391 }
392
393 trans->transfer.stride =
394 ((struct r600_resource_texture *)trans->linear_texture)->pitch_in_bytes[0];
395 if (usage & PIPE_TRANSFER_READ) {
396 /* We cannot map a tiled texture directly because the data is
397 * in a different order, therefore we do detiling using a blit. */
398 r600_copy_from_tiled_texture(ctx, trans);
399 /* Always referenced in the blit. */
400 ctx->flush(ctx, 0, NULL);
401 }
402 return &trans->transfer;
403 }
404 trans->transfer.stride = rtex->pitch_in_bytes[sr.level];
405 trans->offset = r600_texture_get_offset(rtex, sr.level, box->z, sr.face);
406 return &trans->transfer;
407 }
408
409 void r600_texture_transfer_destroy(struct pipe_context *ctx,
410 struct pipe_transfer *transfer)
411 {
412 struct r600_transfer *rtransfer = (struct r600_transfer*)transfer;
413 struct r600_resource_texture *rtex = (struct r600_resource_texture*)transfer->resource;
414
415 if (rtransfer->linear_texture) {
416 if (transfer->usage & PIPE_TRANSFER_WRITE) {
417 r600_copy_into_tiled_texture(ctx, rtransfer);
418 }
419 pipe_resource_reference(&rtransfer->linear_texture, NULL);
420 }
421 if (rtex->flushed_depth_texture) {
422 pipe_resource_reference((struct pipe_resource **)&rtex->flushed_depth_texture, NULL);
423 }
424 pipe_resource_reference(&transfer->resource, NULL);
425 FREE(transfer);
426 }
427
428 void* r600_texture_transfer_map(struct pipe_context *ctx,
429 struct pipe_transfer* transfer)
430 {
431 struct r600_transfer *rtransfer = (struct r600_transfer*)transfer;
432 struct r600_bo *bo;
433 enum pipe_format format = transfer->resource->format;
434 struct radeon *radeon = (struct radeon *)ctx->screen->winsys;
435 unsigned offset = 0;
436 char *map;
437
438 if (rtransfer->linear_texture) {
439 bo = ((struct r600_resource *)rtransfer->linear_texture)->bo;
440 } else {
441 struct r600_resource_texture *rtex = (struct r600_resource_texture*)transfer->resource;
442
443 if (rtex->flushed_depth_texture)
444 bo = ((struct r600_resource *)rtex->flushed_depth_texture)->bo;
445 else
446 bo = ((struct r600_resource *)transfer->resource)->bo;
447
448 offset = rtransfer->offset +
449 transfer->box.y / util_format_get_blockheight(format) * transfer->stride +
450 transfer->box.x / util_format_get_blockwidth(format) * util_format_get_blocksize(format);
451 }
452 map = r600_bo_map(radeon, bo, 0, ctx);
453 if (!map) {
454 return NULL;
455 }
456
457 return map + offset;
458 }
459
460 void r600_texture_transfer_unmap(struct pipe_context *ctx,
461 struct pipe_transfer* transfer)
462 {
463 struct r600_transfer *rtransfer = (struct r600_transfer*)transfer;
464 struct radeon *radeon = (struct radeon *)ctx->screen->winsys;
465 struct r600_bo *bo;
466
467 if (rtransfer->linear_texture) {
468 bo = ((struct r600_resource *)rtransfer->linear_texture)->bo;
469 } else {
470 struct r600_resource_texture *rtex = (struct r600_resource_texture*)transfer->resource;
471
472 if (rtex->flushed_depth_texture) {
473 bo = ((struct r600_resource *)rtex->flushed_depth_texture)->bo;
474 } else {
475 bo = ((struct r600_resource *)transfer->resource)->bo;
476 }
477 }
478 r600_bo_unmap(radeon, bo);
479 }
480
481 struct u_resource_vtbl r600_texture_vtbl =
482 {
483 u_default_resource_get_handle, /* get_handle */
484 r600_texture_destroy, /* resource_destroy */
485 r600_texture_is_referenced, /* is_resource_referenced */
486 r600_texture_get_transfer, /* get_transfer */
487 r600_texture_transfer_destroy, /* transfer_destroy */
488 r600_texture_transfer_map, /* transfer_map */
489 u_default_transfer_flush_region,/* transfer_flush_region */
490 r600_texture_transfer_unmap, /* transfer_unmap */
491 u_default_transfer_inline_write /* transfer_inline_write */
492 };
493
494 void r600_init_screen_texture_functions(struct pipe_screen *screen)
495 {
496 screen->get_tex_surface = r600_get_tex_surface;
497 screen->tex_surface_destroy = r600_tex_surface_destroy;
498 }
499
500 static unsigned r600_get_swizzle_combined(const unsigned char *swizzle_format,
501 const unsigned char *swizzle_view)
502 {
503 unsigned i;
504 unsigned char swizzle[4];
505 unsigned result = 0;
506 const uint32_t swizzle_shift[4] = {
507 16, 19, 22, 25,
508 };
509 const uint32_t swizzle_bit[4] = {
510 0, 1, 2, 3,
511 };
512
513 if (swizzle_view) {
514 /* Combine two sets of swizzles. */
515 for (i = 0; i < 4; i++) {
516 swizzle[i] = swizzle_view[i] <= UTIL_FORMAT_SWIZZLE_W ?
517 swizzle_format[swizzle_view[i]] : swizzle_view[i];
518 }
519 } else {
520 memcpy(swizzle, swizzle_format, 4);
521 }
522
523 /* Get swizzle. */
524 for (i = 0; i < 4; i++) {
525 switch (swizzle[i]) {
526 case UTIL_FORMAT_SWIZZLE_Y:
527 result |= swizzle_bit[1] << swizzle_shift[i];
528 break;
529 case UTIL_FORMAT_SWIZZLE_Z:
530 result |= swizzle_bit[2] << swizzle_shift[i];
531 break;
532 case UTIL_FORMAT_SWIZZLE_W:
533 result |= swizzle_bit[3] << swizzle_shift[i];
534 break;
535 case UTIL_FORMAT_SWIZZLE_0:
536 result |= V_038010_SQ_SEL_0 << swizzle_shift[i];
537 break;
538 case UTIL_FORMAT_SWIZZLE_1:
539 result |= V_038010_SQ_SEL_1 << swizzle_shift[i];
540 break;
541 default: /* UTIL_FORMAT_SWIZZLE_X */
542 result |= swizzle_bit[0] << swizzle_shift[i];
543 }
544 }
545 return result;
546 }
547
548 /* texture format translate */
549 uint32_t r600_translate_texformat(enum pipe_format format,
550 const unsigned char *swizzle_view,
551 uint32_t *word4_p, uint32_t *yuv_format_p)
552 {
553 uint32_t result = 0, word4 = 0, yuv_format = 0;
554 const struct util_format_description *desc;
555 boolean uniform = TRUE;
556 int i;
557 const uint32_t sign_bit[4] = {
558 S_038010_FORMAT_COMP_X(V_038010_SQ_FORMAT_COMP_SIGNED),
559 S_038010_FORMAT_COMP_Y(V_038010_SQ_FORMAT_COMP_SIGNED),
560 S_038010_FORMAT_COMP_Z(V_038010_SQ_FORMAT_COMP_SIGNED),
561 S_038010_FORMAT_COMP_W(V_038010_SQ_FORMAT_COMP_SIGNED)
562 };
563 desc = util_format_description(format);
564
565 word4 |= r600_get_swizzle_combined(desc->swizzle, swizzle_view);
566
567 /* Colorspace (return non-RGB formats directly). */
568 switch (desc->colorspace) {
569 /* Depth stencil formats */
570 case UTIL_FORMAT_COLORSPACE_ZS:
571 switch (format) {
572 case PIPE_FORMAT_Z16_UNORM:
573 result = FMT_16;
574 goto out_word4;
575 case PIPE_FORMAT_X24S8_USCALED:
576 word4 |= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT);
577 case PIPE_FORMAT_Z24X8_UNORM:
578 case PIPE_FORMAT_Z24_UNORM_S8_USCALED:
579 result = FMT_8_24;
580 goto out_word4;
581 case PIPE_FORMAT_S8X24_USCALED:
582 word4 |= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT);
583 case PIPE_FORMAT_X8Z24_UNORM:
584 case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
585 result = FMT_24_8;
586 goto out_word4;
587 case PIPE_FORMAT_S8_USCALED:
588 result = V_0280A0_COLOR_8;
589 word4 |= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT);
590 goto out_word4;
591 default:
592 goto out_unknown;
593 }
594
595 case UTIL_FORMAT_COLORSPACE_YUV:
596 yuv_format |= (1 << 30);
597 switch (format) {
598 case PIPE_FORMAT_UYVY:
599 case PIPE_FORMAT_YUYV:
600 default:
601 break;
602 }
603 goto out_unknown; /* TODO */
604
605 case UTIL_FORMAT_COLORSPACE_SRGB:
606 word4 |= S_038010_FORCE_DEGAMMA(1);
607 if (format == PIPE_FORMAT_L8A8_SRGB || format == PIPE_FORMAT_L8_SRGB)
608 goto out_unknown; /* fails for some reason - TODO */
609 break;
610
611 default:
612 break;
613 }
614
615 /* S3TC formats. TODO */
616 if (desc->layout == UTIL_FORMAT_LAYOUT_S3TC) {
617 goto out_unknown;
618 }
619
620
621 for (i = 0; i < desc->nr_channels; i++) {
622 if (desc->channel[i].type == UTIL_FORMAT_TYPE_SIGNED) {
623 word4 |= sign_bit[i];
624 }
625 }
626
627 /* R8G8Bx_SNORM - TODO CxV8U8 */
628
629 /* RGTC - TODO */
630
631 /* See whether the components are of the same size. */
632 for (i = 1; i < desc->nr_channels; i++) {
633 uniform = uniform && desc->channel[0].size == desc->channel[i].size;
634 }
635
636 /* Non-uniform formats. */
637 if (!uniform) {
638 switch(desc->nr_channels) {
639 case 3:
640 if (desc->channel[0].size == 5 &&
641 desc->channel[1].size == 6 &&
642 desc->channel[2].size == 5) {
643 result = FMT_5_6_5;
644 goto out_word4;
645 }
646 goto out_unknown;
647 case 4:
648 if (desc->channel[0].size == 5 &&
649 desc->channel[1].size == 5 &&
650 desc->channel[2].size == 5 &&
651 desc->channel[3].size == 1) {
652 result = FMT_1_5_5_5;
653 goto out_word4;
654 }
655 if (desc->channel[0].size == 10 &&
656 desc->channel[1].size == 10 &&
657 desc->channel[2].size == 10 &&
658 desc->channel[3].size == 2) {
659 result = FMT_10_10_10_2;
660 goto out_word4;
661 }
662 goto out_unknown;
663 }
664 goto out_unknown;
665 }
666
667 /* Find the first non-VOID channel. */
668 for (i = 0; i < 4; i++) {
669 if (desc->channel[i].type != UTIL_FORMAT_TYPE_VOID) {
670 break;
671 }
672 }
673
674 if (i == 4)
675 goto out_unknown;
676
677 /* uniform formats */
678 switch (desc->channel[i].type) {
679 case UTIL_FORMAT_TYPE_UNSIGNED:
680 case UTIL_FORMAT_TYPE_SIGNED:
681 if (!desc->channel[i].normalized &&
682 desc->colorspace != UTIL_FORMAT_COLORSPACE_SRGB) {
683 goto out_unknown;
684 }
685
686 switch (desc->channel[i].size) {
687 case 4:
688 switch (desc->nr_channels) {
689 case 2:
690 result = FMT_4_4;
691 goto out_word4;
692 case 4:
693 result = FMT_4_4_4_4;
694 goto out_word4;
695 }
696 goto out_unknown;
697 case 8:
698 switch (desc->nr_channels) {
699 case 1:
700 result = FMT_8;
701 goto out_word4;
702 case 2:
703 result = FMT_8_8;
704 goto out_word4;
705 case 4:
706 result = FMT_8_8_8_8;
707 goto out_word4;
708 }
709 goto out_unknown;
710 case 16:
711 switch (desc->nr_channels) {
712 case 1:
713 result = FMT_16;
714 goto out_word4;
715 case 2:
716 result = FMT_16_16;
717 goto out_word4;
718 case 4:
719 result = FMT_16_16_16_16;
720 goto out_word4;
721 }
722 }
723 goto out_unknown;
724
725 case UTIL_FORMAT_TYPE_FLOAT:
726 switch (desc->channel[i].size) {
727 case 16:
728 switch (desc->nr_channels) {
729 case 1:
730 result = FMT_16_FLOAT;
731 goto out_word4;
732 case 2:
733 result = FMT_16_16_FLOAT;
734 goto out_word4;
735 case 4:
736 result = FMT_16_16_16_16_FLOAT;
737 goto out_word4;
738 }
739 goto out_unknown;
740 case 32:
741 switch (desc->nr_channels) {
742 case 1:
743 result = FMT_32_FLOAT;
744 goto out_word4;
745 case 2:
746 result = FMT_32_32_FLOAT;
747 goto out_word4;
748 case 4:
749 result = FMT_32_32_32_32_FLOAT;
750 goto out_word4;
751 }
752 }
753
754 }
755 out_word4:
756 if (word4_p)
757 *word4_p = word4;
758 if (yuv_format_p)
759 *yuv_format_p = yuv_format;
760 return result;
761 out_unknown:
762 // R600_ERR("Unable to handle texformat %d %s\n", format, util_format_name(format));
763 return ~0;
764 }