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