r600g: fix transfer function for tiling.
[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 if (rtex->depth) {
353 r = r600_texture_depth_flush(ctx, texture);
354 if (r < 0) {
355 R600_ERR("failed to create temporary texture to hold untiled copy\n");
356 pipe_resource_reference(&trans->transfer.resource, NULL);
357 FREE(trans);
358 return NULL;
359 }
360 } else if (rtex->tiled) {
361 resource.target = PIPE_TEXTURE_2D;
362 resource.format = texture->format;
363 resource.width0 = box->width;
364 resource.height0 = box->height;
365 resource.depth0 = 1;
366 resource.last_level = 0;
367 resource.nr_samples = 0;
368 resource.usage = PIPE_USAGE_DYNAMIC;
369 resource.bind = 0;
370 resource.flags = 0;
371 /* For texture reading, the temporary (detiled) texture is used as
372 * a render target when blitting from a tiled texture. */
373 if (usage & PIPE_TRANSFER_READ) {
374 resource.bind |= PIPE_BIND_RENDER_TARGET;
375 }
376 /* For texture writing, the temporary texture is used as a sampler
377 * when blitting into a tiled texture. */
378 if (usage & PIPE_TRANSFER_WRITE) {
379 resource.bind |= PIPE_BIND_SAMPLER_VIEW;
380 }
381 /* Create the temporary texture. */
382 trans->linear_texture = ctx->screen->resource_create(ctx->screen, &resource);
383 if (trans->linear_texture == NULL) {
384 R600_ERR("failed to create temporary texture to hold untiled copy\n");
385 pipe_resource_reference(&trans->transfer.resource, NULL);
386 FREE(trans);
387 return NULL;
388 }
389
390 trans->transfer.stride =
391 ((struct r600_resource_texture *)trans->linear_texture)->pitch_in_bytes[0];
392 if (usage & PIPE_TRANSFER_READ) {
393 /* We cannot map a tiled texture directly because the data is
394 * in a different order, therefore we do detiling using a blit. */
395 r600_copy_from_tiled_texture(ctx, trans);
396 /* Always referenced in the blit. */
397 ctx->flush(ctx, 0, NULL);
398 }
399 return &trans->transfer;
400 }
401 trans->transfer.stride = rtex->pitch_in_bytes[sr.level];
402 trans->offset = r600_texture_get_offset(rtex, sr.level, box->z, sr.face);
403 return &trans->transfer;
404 }
405
406 void r600_texture_transfer_destroy(struct pipe_context *ctx,
407 struct pipe_transfer *transfer)
408 {
409 struct r600_transfer *rtransfer = (struct r600_transfer*)transfer;
410 struct r600_resource_texture *rtex = (struct r600_resource_texture*)transfer->resource;
411
412 if (rtransfer->linear_texture) {
413 if (transfer->usage & PIPE_TRANSFER_WRITE) {
414 r600_copy_into_tiled_texture(ctx, rtransfer);
415 }
416 pipe_resource_reference(&rtransfer->linear_texture, NULL);
417 }
418 if (rtex->flushed_depth_texture) {
419 pipe_resource_reference((struct pipe_resource **)&rtex->flushed_depth_texture, NULL);
420 }
421 pipe_resource_reference(&transfer->resource, NULL);
422 FREE(transfer);
423 }
424
425 void* r600_texture_transfer_map(struct pipe_context *ctx,
426 struct pipe_transfer* transfer)
427 {
428 struct r600_transfer *rtransfer = (struct r600_transfer*)transfer;
429 struct r600_bo *bo;
430 enum pipe_format format = transfer->resource->format;
431 struct radeon *radeon = (struct radeon *)ctx->screen->winsys;
432 unsigned offset = 0;
433 char *map;
434
435 if (rtransfer->linear_texture) {
436 bo = ((struct r600_resource *)rtransfer->linear_texture)->bo;
437 } else {
438 struct r600_resource_texture *rtex = (struct r600_resource_texture*)transfer->resource;
439
440 if (rtex->flushed_depth_texture)
441 bo = ((struct r600_resource *)rtex->flushed_depth_texture)->bo;
442 else
443 bo = ((struct r600_resource *)transfer->resource)->bo;
444
445 offset = rtransfer->offset +
446 transfer->box.y / util_format_get_blockheight(format) * transfer->stride +
447 transfer->box.x / util_format_get_blockwidth(format) * util_format_get_blocksize(format);
448 }
449 map = r600_bo_map(radeon, bo, 0, ctx);
450 if (!map) {
451 return NULL;
452 }
453
454 return map + offset;
455 }
456
457 void r600_texture_transfer_unmap(struct pipe_context *ctx,
458 struct pipe_transfer* transfer)
459 {
460 struct r600_transfer *rtransfer = (struct r600_transfer*)transfer;
461 struct radeon *radeon = (struct radeon *)ctx->screen->winsys;
462 struct r600_bo *bo;
463
464 if (rtransfer->linear_texture) {
465 bo = ((struct r600_resource *)rtransfer->linear_texture)->bo;
466 } else {
467 struct r600_resource_texture *rtex = (struct r600_resource_texture*)transfer->resource;
468
469 if (rtex->flushed_depth_texture) {
470 bo = ((struct r600_resource *)rtex->flushed_depth_texture)->bo;
471 } else {
472 bo = ((struct r600_resource *)transfer->resource)->bo;
473 }
474 }
475 r600_bo_unmap(radeon, bo);
476 }
477
478 struct u_resource_vtbl r600_texture_vtbl =
479 {
480 u_default_resource_get_handle, /* get_handle */
481 r600_texture_destroy, /* resource_destroy */
482 r600_texture_is_referenced, /* is_resource_referenced */
483 r600_texture_get_transfer, /* get_transfer */
484 r600_texture_transfer_destroy, /* transfer_destroy */
485 r600_texture_transfer_map, /* transfer_map */
486 u_default_transfer_flush_region,/* transfer_flush_region */
487 r600_texture_transfer_unmap, /* transfer_unmap */
488 u_default_transfer_inline_write /* transfer_inline_write */
489 };
490
491 void r600_init_screen_texture_functions(struct pipe_screen *screen)
492 {
493 screen->get_tex_surface = r600_get_tex_surface;
494 screen->tex_surface_destroy = r600_tex_surface_destroy;
495 }
496
497 static unsigned r600_get_swizzle_combined(const unsigned char *swizzle_format,
498 const unsigned char *swizzle_view)
499 {
500 unsigned i;
501 unsigned char swizzle[4];
502 unsigned result = 0;
503 const uint32_t swizzle_shift[4] = {
504 16, 19, 22, 25,
505 };
506 const uint32_t swizzle_bit[4] = {
507 0, 1, 2, 3,
508 };
509
510 if (swizzle_view) {
511 /* Combine two sets of swizzles. */
512 for (i = 0; i < 4; i++) {
513 swizzle[i] = swizzle_view[i] <= UTIL_FORMAT_SWIZZLE_W ?
514 swizzle_format[swizzle_view[i]] : swizzle_view[i];
515 }
516 } else {
517 memcpy(swizzle, swizzle_format, 4);
518 }
519
520 /* Get swizzle. */
521 for (i = 0; i < 4; i++) {
522 switch (swizzle[i]) {
523 case UTIL_FORMAT_SWIZZLE_Y:
524 result |= swizzle_bit[1] << swizzle_shift[i];
525 break;
526 case UTIL_FORMAT_SWIZZLE_Z:
527 result |= swizzle_bit[2] << swizzle_shift[i];
528 break;
529 case UTIL_FORMAT_SWIZZLE_W:
530 result |= swizzle_bit[3] << swizzle_shift[i];
531 break;
532 case UTIL_FORMAT_SWIZZLE_0:
533 result |= V_038010_SQ_SEL_0 << swizzle_shift[i];
534 break;
535 case UTIL_FORMAT_SWIZZLE_1:
536 result |= V_038010_SQ_SEL_1 << swizzle_shift[i];
537 break;
538 default: /* UTIL_FORMAT_SWIZZLE_X */
539 result |= swizzle_bit[0] << swizzle_shift[i];
540 }
541 }
542 return result;
543 }
544
545 /* texture format translate */
546 uint32_t r600_translate_texformat(enum pipe_format format,
547 const unsigned char *swizzle_view,
548 uint32_t *word4_p, uint32_t *yuv_format_p)
549 {
550 uint32_t result = 0, word4 = 0, yuv_format = 0;
551 const struct util_format_description *desc;
552 boolean uniform = TRUE;
553 int i;
554 const uint32_t sign_bit[4] = {
555 S_038010_FORMAT_COMP_X(V_038010_SQ_FORMAT_COMP_SIGNED),
556 S_038010_FORMAT_COMP_Y(V_038010_SQ_FORMAT_COMP_SIGNED),
557 S_038010_FORMAT_COMP_Z(V_038010_SQ_FORMAT_COMP_SIGNED),
558 S_038010_FORMAT_COMP_W(V_038010_SQ_FORMAT_COMP_SIGNED)
559 };
560 desc = util_format_description(format);
561
562 word4 |= r600_get_swizzle_combined(desc->swizzle, swizzle_view);
563
564 /* Colorspace (return non-RGB formats directly). */
565 switch (desc->colorspace) {
566 /* Depth stencil formats */
567 case UTIL_FORMAT_COLORSPACE_ZS:
568 switch (format) {
569 case PIPE_FORMAT_Z16_UNORM:
570 result = FMT_16;
571 goto out_word4;
572 case PIPE_FORMAT_X24S8_USCALED:
573 word4 |= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT);
574 case PIPE_FORMAT_Z24X8_UNORM:
575 case PIPE_FORMAT_Z24_UNORM_S8_USCALED:
576 result = FMT_8_24;
577 goto out_word4;
578 case PIPE_FORMAT_S8X24_USCALED:
579 word4 |= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT);
580 case PIPE_FORMAT_X8Z24_UNORM:
581 case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
582 result = FMT_24_8;
583 goto out_word4;
584 case PIPE_FORMAT_S8_USCALED:
585 result = V_0280A0_COLOR_8;
586 word4 |= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT);
587 goto out_word4;
588 default:
589 goto out_unknown;
590 }
591
592 case UTIL_FORMAT_COLORSPACE_YUV:
593 yuv_format |= (1 << 30);
594 switch (format) {
595 case PIPE_FORMAT_UYVY:
596 case PIPE_FORMAT_YUYV:
597 default:
598 break;
599 }
600 goto out_unknown; /* TODO */
601
602 case UTIL_FORMAT_COLORSPACE_SRGB:
603 word4 |= S_038010_FORCE_DEGAMMA(1);
604 if (format == PIPE_FORMAT_L8A8_SRGB || format == PIPE_FORMAT_L8_SRGB)
605 goto out_unknown; /* fails for some reason - TODO */
606 break;
607
608 default:
609 break;
610 }
611
612 /* S3TC formats. TODO */
613 if (desc->layout == UTIL_FORMAT_LAYOUT_S3TC) {
614 goto out_unknown;
615 }
616
617
618 for (i = 0; i < desc->nr_channels; i++) {
619 if (desc->channel[i].type == UTIL_FORMAT_TYPE_SIGNED) {
620 word4 |= sign_bit[i];
621 }
622 }
623
624 /* R8G8Bx_SNORM - TODO CxV8U8 */
625
626 /* RGTC - TODO */
627
628 /* See whether the components are of the same size. */
629 for (i = 1; i < desc->nr_channels; i++) {
630 uniform = uniform && desc->channel[0].size == desc->channel[i].size;
631 }
632
633 /* Non-uniform formats. */
634 if (!uniform) {
635 switch(desc->nr_channels) {
636 case 3:
637 if (desc->channel[0].size == 5 &&
638 desc->channel[1].size == 6 &&
639 desc->channel[2].size == 5) {
640 result = FMT_5_6_5;
641 goto out_word4;
642 }
643 goto out_unknown;
644 case 4:
645 if (desc->channel[0].size == 5 &&
646 desc->channel[1].size == 5 &&
647 desc->channel[2].size == 5 &&
648 desc->channel[3].size == 1) {
649 result = FMT_1_5_5_5;
650 goto out_word4;
651 }
652 if (desc->channel[0].size == 10 &&
653 desc->channel[1].size == 10 &&
654 desc->channel[2].size == 10 &&
655 desc->channel[3].size == 2) {
656 result = FMT_10_10_10_2;
657 goto out_word4;
658 }
659 goto out_unknown;
660 }
661 goto out_unknown;
662 }
663
664 /* Find the first non-VOID channel. */
665 for (i = 0; i < 4; i++) {
666 if (desc->channel[i].type != UTIL_FORMAT_TYPE_VOID) {
667 break;
668 }
669 }
670
671 if (i == 4)
672 goto out_unknown;
673
674 /* uniform formats */
675 switch (desc->channel[i].type) {
676 case UTIL_FORMAT_TYPE_UNSIGNED:
677 case UTIL_FORMAT_TYPE_SIGNED:
678 if (!desc->channel[i].normalized &&
679 desc->colorspace != UTIL_FORMAT_COLORSPACE_SRGB) {
680 goto out_unknown;
681 }
682
683 switch (desc->channel[i].size) {
684 case 4:
685 switch (desc->nr_channels) {
686 case 2:
687 result = FMT_4_4;
688 goto out_word4;
689 case 4:
690 result = FMT_4_4_4_4;
691 goto out_word4;
692 }
693 goto out_unknown;
694 case 8:
695 switch (desc->nr_channels) {
696 case 1:
697 result = FMT_8;
698 goto out_word4;
699 case 2:
700 result = FMT_8_8;
701 goto out_word4;
702 case 4:
703 result = FMT_8_8_8_8;
704 goto out_word4;
705 }
706 goto out_unknown;
707 case 16:
708 switch (desc->nr_channels) {
709 case 1:
710 result = FMT_16;
711 goto out_word4;
712 case 2:
713 result = FMT_16_16;
714 goto out_word4;
715 case 4:
716 result = FMT_16_16_16_16;
717 goto out_word4;
718 }
719 }
720 goto out_unknown;
721
722 case UTIL_FORMAT_TYPE_FLOAT:
723 switch (desc->channel[i].size) {
724 case 16:
725 switch (desc->nr_channels) {
726 case 1:
727 result = FMT_16_FLOAT;
728 goto out_word4;
729 case 2:
730 result = FMT_16_16_FLOAT;
731 goto out_word4;
732 case 4:
733 result = FMT_16_16_16_16_FLOAT;
734 goto out_word4;
735 }
736 goto out_unknown;
737 case 32:
738 switch (desc->nr_channels) {
739 case 1:
740 result = FMT_32_FLOAT;
741 goto out_word4;
742 case 2:
743 result = FMT_32_32_FLOAT;
744 goto out_word4;
745 case 4:
746 result = FMT_32_32_32_32_FLOAT;
747 goto out_word4;
748 }
749 }
750
751 }
752 out_word4:
753 if (word4_p)
754 *word4_p = word4;
755 if (yuv_format_p)
756 *yuv_format_p = yuv_format;
757 return result;
758 out_unknown:
759 // R600_ERR("Unable to handle texformat %d %s\n", format, util_format_name(format));
760 return ~0;
761 }