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