r600g: depth needs to bound to ds
[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 r600_setup_miptree(screen, rtex);
196
197 resource->size = rtex->size;
198
199 if (!resource->bo) {
200 resource->bo = r600_bo(radeon, rtex->size, 4096, 0);
201 if (!resource->bo) {
202 FREE(rtex);
203 return NULL;
204 }
205 }
206 return rtex;
207 }
208
209 struct pipe_resource *r600_texture_create(struct pipe_screen *screen,
210 const struct pipe_resource *templ)
211 {
212 unsigned array_mode = 0;
213
214 return (struct pipe_resource *)r600_texture_create_object(screen, templ, array_mode,
215 0, 0, NULL);
216
217 }
218
219 static void r600_texture_destroy(struct pipe_screen *screen,
220 struct pipe_resource *ptex)
221 {
222 struct r600_resource_texture *rtex = (struct r600_resource_texture*)ptex;
223 struct r600_resource *resource = &rtex->resource;
224 struct radeon *radeon = (struct radeon *)screen->winsys;
225
226 if (rtex->flushed_depth_texture)
227 pipe_resource_reference((struct pipe_resource **)&rtex->flushed_depth_texture, NULL);
228
229 if (resource->bo) {
230 r600_bo_reference(radeon, &resource->bo, NULL);
231 }
232 FREE(rtex);
233 }
234
235 static struct pipe_surface *r600_get_tex_surface(struct pipe_screen *screen,
236 struct pipe_resource *texture,
237 unsigned face, unsigned level,
238 unsigned zslice, unsigned flags)
239 {
240 struct r600_resource_texture *rtex = (struct r600_resource_texture*)texture;
241 struct pipe_surface *surface = CALLOC_STRUCT(pipe_surface);
242 unsigned offset;
243
244 if (surface == NULL)
245 return NULL;
246 offset = r600_texture_get_offset(rtex, level, zslice, face);
247 pipe_reference_init(&surface->reference, 1);
248 pipe_resource_reference(&surface->texture, texture);
249 surface->format = texture->format;
250 surface->width = u_minify(texture->width0, level);
251 surface->height = u_minify(texture->height0, level);
252 surface->offset = offset;
253 surface->usage = flags;
254 surface->zslice = zslice;
255 surface->texture = texture;
256 surface->face = face;
257 surface->level = level;
258 return surface;
259 }
260
261 static void r600_tex_surface_destroy(struct pipe_surface *surface)
262 {
263 pipe_resource_reference(&surface->texture, NULL);
264 FREE(surface);
265 }
266
267
268 struct pipe_resource *r600_texture_from_handle(struct pipe_screen *screen,
269 const struct pipe_resource *templ,
270 struct winsys_handle *whandle)
271 {
272 struct radeon *rw = (struct radeon*)screen->winsys;
273 struct r600_bo *bo = NULL;
274
275 /* Support only 2D textures without mipmaps */
276 if ((templ->target != PIPE_TEXTURE_2D && templ->target != PIPE_TEXTURE_RECT) ||
277 templ->depth0 != 1 || templ->last_level != 0)
278 return NULL;
279
280 bo = r600_bo_handle(rw, whandle->handle);
281 if (bo == NULL) {
282 return NULL;
283 }
284
285 return (struct pipe_resource *)r600_texture_create_object(screen, templ, 0,
286 whandle->stride,
287 0,
288 bo);
289 }
290
291 static unsigned int r600_texture_is_referenced(struct pipe_context *context,
292 struct pipe_resource *texture,
293 unsigned face, unsigned level)
294 {
295 /* FIXME */
296 return PIPE_REFERENCED_FOR_READ | PIPE_REFERENCED_FOR_WRITE;
297 }
298
299 int (*r600_blit_uncompress_depth_ptr)(struct pipe_context *ctx, struct r600_resource_texture *texture);
300
301 int r600_texture_depth_flush(struct pipe_context *ctx,
302 struct pipe_resource *texture)
303 {
304 struct r600_resource_texture *rtex = (struct r600_resource_texture*)texture;
305 struct pipe_resource resource;
306
307 if (rtex->flushed_depth_texture)
308 goto out;
309
310 resource.target = PIPE_TEXTURE_2D;
311 resource.format = texture->format;
312 resource.width0 = texture->width0;
313 resource.height0 = texture->height0;
314 resource.depth0 = 1;
315 resource.last_level = 0;
316 resource.nr_samples = 0;
317 resource.usage = PIPE_USAGE_DYNAMIC;
318 resource.bind = 0;
319 resource.flags = 0;
320
321 resource.bind |= PIPE_BIND_DEPTH_STENCIL;
322
323 rtex->flushed_depth_texture = (struct r600_resource_texture *)ctx->screen->resource_create(ctx->screen, &resource);
324 if (rtex->flushed_depth_texture == NULL) {
325 R600_ERR("failed to create temporary texture to hold untiled copy\n");
326 return -ENOMEM;
327 }
328
329 out:
330 r600_blit_uncompress_depth_ptr(ctx, rtex);
331 return 0;
332 }
333
334 struct pipe_transfer* r600_texture_get_transfer(struct pipe_context *ctx,
335 struct pipe_resource *texture,
336 struct pipe_subresource sr,
337 unsigned usage,
338 const struct pipe_box *box)
339 {
340 struct r600_resource_texture *rtex = (struct r600_resource_texture*)texture;
341 struct pipe_resource resource;
342 struct r600_transfer *trans;
343 int r;
344
345 trans = CALLOC_STRUCT(r600_transfer);
346 if (trans == NULL)
347 return NULL;
348 pipe_resource_reference(&trans->transfer.resource, texture);
349 trans->transfer.sr = sr;
350 trans->transfer.usage = usage;
351 trans->transfer.box = *box;
352 trans->transfer.stride = rtex->pitch_in_bytes[sr.level];
353 trans->offset = r600_texture_get_offset(rtex, sr.level, box->z, sr.face);
354 if (rtex->depth) {
355 r = r600_texture_depth_flush(ctx, texture);
356 if (r < 0) {
357 R600_ERR("failed to create temporary texture to hold untiled copy\n");
358 pipe_resource_reference(&trans->transfer.resource, NULL);
359 FREE(trans);
360 return NULL;
361 }
362 } else if (rtex->tiled) {
363 resource.target = PIPE_TEXTURE_2D;
364 resource.format = texture->format;
365 resource.width0 = box->width;
366 resource.height0 = box->height;
367 resource.depth0 = 1;
368 resource.last_level = 0;
369 resource.nr_samples = 0;
370 resource.usage = PIPE_USAGE_DYNAMIC;
371 resource.bind = 0;
372 resource.flags = 0;
373 /* For texture reading, the temporary (detiled) texture is used as
374 * a render target when blitting from a tiled texture. */
375 if (usage & PIPE_TRANSFER_READ) {
376 resource.bind |= PIPE_BIND_RENDER_TARGET;
377 }
378 /* For texture writing, the temporary texture is used as a sampler
379 * when blitting into a tiled texture. */
380 if (usage & PIPE_TRANSFER_WRITE) {
381 resource.bind |= PIPE_BIND_SAMPLER_VIEW;
382 }
383 /* Create the temporary texture. */
384 trans->linear_texture = ctx->screen->resource_create(ctx->screen, &resource);
385 if (trans->linear_texture == NULL) {
386 R600_ERR("failed to create temporary texture to hold untiled copy\n");
387 pipe_resource_reference(&trans->transfer.resource, NULL);
388 FREE(trans);
389 return NULL;
390 }
391
392 trans->transfer.stride =
393 ((struct r600_resource_texture *)trans->linear_texture)->pitch_in_bytes[0];
394 if (usage & PIPE_TRANSFER_READ) {
395 /* We cannot map a tiled texture directly because the data is
396 * in a different order, therefore we do detiling using a blit. */
397 r600_copy_from_tiled_texture(ctx, trans);
398 /* Always referenced in the blit. */
399 ctx->flush(ctx, 0, NULL);
400 }
401 }
402 return &trans->transfer;
403 }
404
405 void r600_texture_transfer_destroy(struct pipe_context *ctx,
406 struct pipe_transfer *transfer)
407 {
408 struct r600_transfer *rtransfer = (struct r600_transfer*)transfer;
409 struct r600_resource_texture *rtex = (struct r600_resource_texture*)transfer->resource;
410
411 if (rtransfer->linear_texture) {
412 if (transfer->usage & PIPE_TRANSFER_WRITE) {
413 r600_copy_into_tiled_texture(ctx, rtransfer);
414 }
415 pipe_resource_reference(&rtransfer->linear_texture, NULL);
416 }
417 if (rtex->flushed_depth_texture) {
418 pipe_resource_reference((struct pipe_resource **)&rtex->flushed_depth_texture, NULL);
419 }
420 pipe_resource_reference(&transfer->resource, NULL);
421 FREE(transfer);
422 }
423
424 void* r600_texture_transfer_map(struct pipe_context *ctx,
425 struct pipe_transfer* transfer)
426 {
427 struct r600_transfer *rtransfer = (struct r600_transfer*)transfer;
428 struct r600_bo *bo;
429 enum pipe_format format = transfer->resource->format;
430 struct radeon *radeon = (struct radeon *)ctx->screen->winsys;
431 unsigned offset = 0;
432 char *map;
433
434 if (rtransfer->linear_texture) {
435 bo = ((struct r600_resource *)rtransfer->linear_texture)->bo;
436 } else {
437 struct r600_resource_texture *rtex = (struct r600_resource_texture*)transfer->resource;
438
439 if (rtex->flushed_depth_texture)
440 bo = ((struct r600_resource *)rtex->flushed_depth_texture)->bo;
441 else
442 bo = ((struct r600_resource *)transfer->resource)->bo;
443
444 offset = rtransfer->offset +
445 transfer->box.y / util_format_get_blockheight(format) * transfer->stride +
446 transfer->box.x / util_format_get_blockwidth(format) * util_format_get_blocksize(format);
447 }
448 map = r600_bo_map(radeon, bo, 0, ctx);
449 if (!map) {
450 return NULL;
451 }
452
453 return map + offset;
454 }
455
456 void r600_texture_transfer_unmap(struct pipe_context *ctx,
457 struct pipe_transfer* transfer)
458 {
459 struct r600_transfer *rtransfer = (struct r600_transfer*)transfer;
460 struct radeon *radeon = (struct radeon *)ctx->screen->winsys;
461 struct r600_bo *bo;
462
463 if (rtransfer->linear_texture) {
464 bo = ((struct r600_resource *)rtransfer->linear_texture)->bo;
465 } else {
466 struct r600_resource_texture *rtex = (struct r600_resource_texture*)transfer->resource;
467
468 if (rtex->flushed_depth_texture) {
469 bo = ((struct r600_resource *)rtex->flushed_depth_texture)->bo;
470 } else {
471 bo = ((struct r600_resource *)transfer->resource)->bo;
472 }
473 }
474 r600_bo_unmap(radeon, bo);
475 }
476
477 struct u_resource_vtbl r600_texture_vtbl =
478 {
479 u_default_resource_get_handle, /* get_handle */
480 r600_texture_destroy, /* resource_destroy */
481 r600_texture_is_referenced, /* is_resource_referenced */
482 r600_texture_get_transfer, /* get_transfer */
483 r600_texture_transfer_destroy, /* transfer_destroy */
484 r600_texture_transfer_map, /* transfer_map */
485 u_default_transfer_flush_region,/* transfer_flush_region */
486 r600_texture_transfer_unmap, /* transfer_unmap */
487 u_default_transfer_inline_write /* transfer_inline_write */
488 };
489
490 void r600_init_screen_texture_functions(struct pipe_screen *screen)
491 {
492 screen->get_tex_surface = r600_get_tex_surface;
493 screen->tex_surface_destroy = r600_tex_surface_destroy;
494 }
495
496 static unsigned r600_get_swizzle_combined(const unsigned char *swizzle_format,
497 const unsigned char *swizzle_view)
498 {
499 unsigned i;
500 unsigned char swizzle[4];
501 unsigned result = 0;
502 const uint32_t swizzle_shift[4] = {
503 16, 19, 22, 25,
504 };
505 const uint32_t swizzle_bit[4] = {
506 0, 1, 2, 3,
507 };
508
509 if (swizzle_view) {
510 /* Combine two sets of swizzles. */
511 for (i = 0; i < 4; i++) {
512 swizzle[i] = swizzle_view[i] <= UTIL_FORMAT_SWIZZLE_W ?
513 swizzle_format[swizzle_view[i]] : swizzle_view[i];
514 }
515 } else {
516 memcpy(swizzle, swizzle_format, 4);
517 }
518
519 /* Get swizzle. */
520 for (i = 0; i < 4; i++) {
521 switch (swizzle[i]) {
522 case UTIL_FORMAT_SWIZZLE_Y:
523 result |= swizzle_bit[1] << swizzle_shift[i];
524 break;
525 case UTIL_FORMAT_SWIZZLE_Z:
526 result |= swizzle_bit[2] << swizzle_shift[i];
527 break;
528 case UTIL_FORMAT_SWIZZLE_W:
529 result |= swizzle_bit[3] << swizzle_shift[i];
530 break;
531 case UTIL_FORMAT_SWIZZLE_0:
532 result |= V_038010_SQ_SEL_0 << swizzle_shift[i];
533 break;
534 case UTIL_FORMAT_SWIZZLE_1:
535 result |= V_038010_SQ_SEL_1 << swizzle_shift[i];
536 break;
537 default: /* UTIL_FORMAT_SWIZZLE_X */
538 result |= swizzle_bit[0] << swizzle_shift[i];
539 }
540 }
541 return result;
542 }
543
544 /* texture format translate */
545 uint32_t r600_translate_texformat(enum pipe_format format,
546 const unsigned char *swizzle_view,
547 uint32_t *word4_p, uint32_t *yuv_format_p)
548 {
549 uint32_t result = 0, word4 = 0, yuv_format = 0;
550 const struct util_format_description *desc;
551 boolean uniform = TRUE;
552 int i;
553 const uint32_t sign_bit[4] = {
554 S_038010_FORMAT_COMP_X(V_038010_SQ_FORMAT_COMP_SIGNED),
555 S_038010_FORMAT_COMP_Y(V_038010_SQ_FORMAT_COMP_SIGNED),
556 S_038010_FORMAT_COMP_Z(V_038010_SQ_FORMAT_COMP_SIGNED),
557 S_038010_FORMAT_COMP_W(V_038010_SQ_FORMAT_COMP_SIGNED)
558 };
559 desc = util_format_description(format);
560
561 word4 |= r600_get_swizzle_combined(desc->swizzle, swizzle_view);
562
563 /* Colorspace (return non-RGB formats directly). */
564 switch (desc->colorspace) {
565 /* Depth stencil formats */
566 case UTIL_FORMAT_COLORSPACE_ZS:
567 switch (format) {
568 case PIPE_FORMAT_Z16_UNORM:
569 result = FMT_16;
570 goto out_word4;
571 case PIPE_FORMAT_X24S8_USCALED:
572 word4 |= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT);
573 case PIPE_FORMAT_Z24X8_UNORM:
574 case PIPE_FORMAT_Z24_UNORM_S8_USCALED:
575 result = FMT_8_24;
576 goto out_word4;
577 case PIPE_FORMAT_S8X24_USCALED:
578 word4 |= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT);
579 case PIPE_FORMAT_X8Z24_UNORM:
580 case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
581 result = FMT_24_8;
582 goto out_word4;
583 case PIPE_FORMAT_S8_USCALED:
584 result = V_0280A0_COLOR_8;
585 word4 |= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT);
586 goto out_word4;
587 default:
588 goto out_unknown;
589 }
590
591 case UTIL_FORMAT_COLORSPACE_YUV:
592 yuv_format |= (1 << 30);
593 switch (format) {
594 case PIPE_FORMAT_UYVY:
595 case PIPE_FORMAT_YUYV:
596 default:
597 break;
598 }
599 goto out_unknown; /* TODO */
600
601 case UTIL_FORMAT_COLORSPACE_SRGB:
602 word4 |= S_038010_FORCE_DEGAMMA(1);
603 if (format == PIPE_FORMAT_L8A8_SRGB || format == PIPE_FORMAT_L8_SRGB)
604 goto out_unknown; /* fails for some reason - TODO */
605 break;
606
607 default:
608 break;
609 }
610
611 /* S3TC formats. TODO */
612 if (desc->layout == UTIL_FORMAT_LAYOUT_S3TC) {
613 goto out_unknown;
614 }
615
616
617 for (i = 0; i < desc->nr_channels; i++) {
618 if (desc->channel[i].type == UTIL_FORMAT_TYPE_SIGNED) {
619 word4 |= sign_bit[i];
620 }
621 }
622
623 /* R8G8Bx_SNORM - TODO CxV8U8 */
624
625 /* RGTC - TODO */
626
627 /* See whether the components are of the same size. */
628 for (i = 1; i < desc->nr_channels; i++) {
629 uniform = uniform && desc->channel[0].size == desc->channel[i].size;
630 }
631
632 /* Non-uniform formats. */
633 if (!uniform) {
634 switch(desc->nr_channels) {
635 case 3:
636 if (desc->channel[0].size == 5 &&
637 desc->channel[1].size == 6 &&
638 desc->channel[2].size == 5) {
639 result = FMT_5_6_5;
640 goto out_word4;
641 }
642 goto out_unknown;
643 case 4:
644 if (desc->channel[0].size == 5 &&
645 desc->channel[1].size == 5 &&
646 desc->channel[2].size == 5 &&
647 desc->channel[3].size == 1) {
648 result = FMT_1_5_5_5;
649 goto out_word4;
650 }
651 if (desc->channel[0].size == 10 &&
652 desc->channel[1].size == 10 &&
653 desc->channel[2].size == 10 &&
654 desc->channel[3].size == 2) {
655 result = FMT_10_10_10_2;
656 goto out_word4;
657 }
658 goto out_unknown;
659 }
660 goto out_unknown;
661 }
662
663 /* Find the first non-VOID channel. */
664 for (i = 0; i < 4; i++) {
665 if (desc->channel[i].type != UTIL_FORMAT_TYPE_VOID) {
666 break;
667 }
668 }
669
670 if (i == 4)
671 goto out_unknown;
672
673 /* uniform formats */
674 switch (desc->channel[i].type) {
675 case UTIL_FORMAT_TYPE_UNSIGNED:
676 case UTIL_FORMAT_TYPE_SIGNED:
677 if (!desc->channel[i].normalized &&
678 desc->colorspace != UTIL_FORMAT_COLORSPACE_SRGB) {
679 goto out_unknown;
680 }
681
682 switch (desc->channel[i].size) {
683 case 4:
684 switch (desc->nr_channels) {
685 case 2:
686 result = FMT_4_4;
687 goto out_word4;
688 case 4:
689 result = FMT_4_4_4_4;
690 goto out_word4;
691 }
692 goto out_unknown;
693 case 8:
694 switch (desc->nr_channels) {
695 case 1:
696 result = FMT_8;
697 goto out_word4;
698 case 2:
699 result = FMT_8_8;
700 goto out_word4;
701 case 4:
702 result = FMT_8_8_8_8;
703 goto out_word4;
704 }
705 goto out_unknown;
706 case 16:
707 switch (desc->nr_channels) {
708 case 1:
709 result = FMT_16;
710 goto out_word4;
711 case 2:
712 result = FMT_16_16;
713 goto out_word4;
714 case 4:
715 result = FMT_16_16_16_16;
716 goto out_word4;
717 }
718 }
719 goto out_unknown;
720
721 case UTIL_FORMAT_TYPE_FLOAT:
722 switch (desc->channel[i].size) {
723 case 16:
724 switch (desc->nr_channels) {
725 case 1:
726 result = FMT_16_FLOAT;
727 goto out_word4;
728 case 2:
729 result = FMT_16_16_FLOAT;
730 goto out_word4;
731 case 4:
732 result = FMT_16_16_16_16_FLOAT;
733 goto out_word4;
734 }
735 goto out_unknown;
736 case 32:
737 switch (desc->nr_channels) {
738 case 1:
739 result = FMT_32_FLOAT;
740 goto out_word4;
741 case 2:
742 result = FMT_32_32_FLOAT;
743 goto out_word4;
744 case 4:
745 result = FMT_32_32_32_32_FLOAT;
746 goto out_word4;
747 }
748 }
749
750 }
751 out_word4:
752 if (word4_p)
753 *word4_p = word4;
754 if (yuv_format_p)
755 *yuv_format_p = yuv_format;
756 return result;
757 out_unknown:
758 // R600_ERR("Unable to handle texformat %d %s\n", format, util_format_name(format));
759 return ~0;
760 }