2 * Copyright 2008 Corbin Simpson <MostAwesomeDude@gmail.com>
3 * Copyright 2010 Marek Olšák <maraeo@gmail.com>
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE. */
24 /* Always include headers in the reverse order!! ~ M. */
25 #include "r300_texture.h"
27 #include "r300_context.h"
29 #include "r300_texture_desc.h"
30 #include "r300_transfer.h"
31 #include "r300_screen.h"
33 #include "util/format/u_format.h"
34 #include "util/format/u_format_s3tc.h"
35 #include "util/u_math.h"
36 #include "util/u_memory.h"
38 #include "pipe/p_screen.h"
39 #include "frontend/winsys_handle.h"
41 /* These formats are supported by swapping their bytes.
42 * The swizzles must be set exactly like their non-swapped counterparts,
43 * because byte-swapping is what reverses the component order, not swizzling.
45 * This function returns the format that must be used to program CB and TX
48 static enum pipe_format r300_unbyteswap_array_format(enum pipe_format format)
50 /* FIXME: Disabled on little endian because of a reported regression:
51 * https://bugs.freedesktop.org/show_bug.cgi?id=98869 */
52 if (PIPE_ENDIAN_NATIVE != PIPE_ENDIAN_BIG)
55 /* Only BGRA 8888 array formats are supported for simplicity of
56 * the implementation. */
58 case PIPE_FORMAT_A8R8G8B8_UNORM:
59 return PIPE_FORMAT_B8G8R8A8_UNORM;
60 case PIPE_FORMAT_A8R8G8B8_SRGB:
61 return PIPE_FORMAT_B8G8R8A8_SRGB;
62 case PIPE_FORMAT_X8R8G8B8_UNORM:
63 return PIPE_FORMAT_B8G8R8X8_UNORM;
64 case PIPE_FORMAT_X8R8G8B8_SRGB:
65 return PIPE_FORMAT_B8G8R8X8_SRGB;
71 static unsigned r300_get_endian_swap(enum pipe_format format)
73 const struct util_format_description *desc;
76 if (r300_unbyteswap_array_format(format) != format)
77 return R300_SURF_DWORD_SWAP;
79 if (PIPE_ENDIAN_NATIVE != PIPE_ENDIAN_BIG)
80 return R300_SURF_NO_SWAP;
82 desc = util_format_description(format);
84 /* Compressed formats should be in the little endian format. */
85 if (desc->block.width != 1 || desc->block.height != 1)
86 return R300_SURF_NO_SWAP;
88 swap_size = desc->is_array ? desc->channel[0].size : desc->block.bits;
91 default: /* shouldn't happen? */
93 return R300_SURF_NO_SWAP;
95 return R300_SURF_WORD_SWAP;
97 return R300_SURF_DWORD_SWAP;
101 unsigned r300_get_swizzle_combined(const unsigned char *swizzle_format,
102 const unsigned char *swizzle_view,
106 unsigned char swizzle[4];
108 const uint32_t swizzle_shift[4] = {
109 R300_TX_FORMAT_R_SHIFT,
110 R300_TX_FORMAT_G_SHIFT,
111 R300_TX_FORMAT_B_SHIFT,
112 R300_TX_FORMAT_A_SHIFT
114 uint32_t swizzle_bit[4] = {
115 dxtc_swizzle ? R300_TX_FORMAT_Z : R300_TX_FORMAT_X,
117 dxtc_swizzle ? R300_TX_FORMAT_X : R300_TX_FORMAT_Z,
122 /* Combine two sets of swizzles. */
123 util_format_compose_swizzles(swizzle_format, swizzle_view, swizzle);
125 memcpy(swizzle, swizzle_format, 4);
129 for (i = 0; i < 4; i++) {
130 switch (swizzle[i]) {
132 result |= swizzle_bit[1] << swizzle_shift[i];
135 result |= swizzle_bit[2] << swizzle_shift[i];
138 result |= swizzle_bit[3] << swizzle_shift[i];
141 result |= R300_TX_FORMAT_ZERO << swizzle_shift[i];
144 result |= R300_TX_FORMAT_ONE << swizzle_shift[i];
146 default: /* PIPE_SWIZZLE_X */
147 result |= swizzle_bit[0] << swizzle_shift[i];
153 /* Translate a pipe_format into a useful texture format for sampling.
155 * Some special formats are translated directly using R300_EASY_TX_FORMAT,
156 * but the majority of them is translated in a generic way, automatically
157 * supporting all the formats hw can support.
159 * R300_EASY_TX_FORMAT swizzles the texture.
160 * Note the signature of R300_EASY_TX_FORMAT:
161 * R300_EASY_TX_FORMAT(B, G, R, A, FORMAT);
163 * The FORMAT specifies how the texture sampler will treat the texture, and
164 * makes available X, Y, Z, W, ZERO, and ONE for swizzling. */
165 uint32_t r300_translate_texformat(enum pipe_format format,
166 const unsigned char *swizzle_view,
171 const struct util_format_description *desc;
174 const uint32_t sign_bit[4] = {
175 R300_TX_FORMAT_SIGNED_W,
176 R300_TX_FORMAT_SIGNED_Z,
177 R300_TX_FORMAT_SIGNED_Y,
178 R300_TX_FORMAT_SIGNED_X,
181 format = r300_unbyteswap_array_format(format);
182 desc = util_format_description(format);
184 /* Colorspace (return non-RGB formats directly). */
185 switch (desc->colorspace) {
186 /* Depth stencil formats.
187 * Swizzles are added in r300_merge_textures_and_samplers. */
188 case UTIL_FORMAT_COLORSPACE_ZS:
190 case PIPE_FORMAT_Z16_UNORM:
191 return R300_TX_FORMAT_X16;
192 case PIPE_FORMAT_X8Z24_UNORM:
193 case PIPE_FORMAT_S8_UINT_Z24_UNORM:
195 return R500_TX_FORMAT_Y8X24;
197 return R300_TX_FORMAT_Y16X16;
199 return ~0; /* Unsupported. */
203 case UTIL_FORMAT_COLORSPACE_YUV:
204 result |= R300_TX_FORMAT_YUV_TO_RGB;
207 case PIPE_FORMAT_UYVY:
208 return R300_EASY_TX_FORMAT(X, Y, Z, ONE, YVYU422) | result;
209 case PIPE_FORMAT_YUYV:
210 return R300_EASY_TX_FORMAT(X, Y, Z, ONE, VYUY422) | result;
212 return ~0; /* Unsupported/unknown. */
215 /* Add gamma correction. */
216 case UTIL_FORMAT_COLORSPACE_SRGB:
217 result |= R300_TX_FORMAT_GAMMA;
222 /* Same as YUV but without the YUR->RGB conversion. */
223 case PIPE_FORMAT_R8G8_B8G8_UNORM:
224 return R300_EASY_TX_FORMAT(X, Y, Z, ONE, YVYU422) | result;
225 case PIPE_FORMAT_G8R8_G8B8_UNORM:
226 return R300_EASY_TX_FORMAT(X, Y, Z, ONE, VYUY422) | result;
232 /* The RGTC1_SNORM and LATC1_SNORM swizzle is done in the shader. */
233 if (util_format_is_compressed(format) &&
235 format != PIPE_FORMAT_RGTC2_UNORM &&
236 format != PIPE_FORMAT_RGTC2_SNORM &&
237 format != PIPE_FORMAT_LATC2_UNORM &&
238 format != PIPE_FORMAT_LATC2_SNORM &&
239 format != PIPE_FORMAT_RGTC1_UNORM &&
240 format != PIPE_FORMAT_RGTC1_SNORM &&
241 format != PIPE_FORMAT_LATC1_UNORM &&
242 format != PIPE_FORMAT_LATC1_SNORM) {
243 result |= r300_get_swizzle_combined(desc->swizzle, swizzle_view,
246 result |= r300_get_swizzle_combined(desc->swizzle, swizzle_view,
251 if (desc->layout == UTIL_FORMAT_LAYOUT_S3TC) {
253 case PIPE_FORMAT_DXT1_RGB:
254 case PIPE_FORMAT_DXT1_RGBA:
255 case PIPE_FORMAT_DXT1_SRGB:
256 case PIPE_FORMAT_DXT1_SRGBA:
257 return R300_TX_FORMAT_DXT1 | result;
258 case PIPE_FORMAT_DXT3_RGBA:
259 case PIPE_FORMAT_DXT3_SRGBA:
260 return R300_TX_FORMAT_DXT3 | result;
261 case PIPE_FORMAT_DXT5_RGBA:
262 case PIPE_FORMAT_DXT5_SRGBA:
263 return R300_TX_FORMAT_DXT5 | result;
265 return ~0; /* Unsupported/unknown. */
270 if (desc->layout == UTIL_FORMAT_LAYOUT_RGTC) {
272 case PIPE_FORMAT_RGTC1_SNORM:
273 case PIPE_FORMAT_LATC1_SNORM:
274 result |= sign_bit[0];
276 case PIPE_FORMAT_LATC1_UNORM:
277 case PIPE_FORMAT_RGTC1_UNORM:
278 return R500_TX_FORMAT_ATI1N | result;
280 case PIPE_FORMAT_RGTC2_SNORM:
281 case PIPE_FORMAT_LATC2_SNORM:
282 result |= sign_bit[1] | sign_bit[0];
284 case PIPE_FORMAT_RGTC2_UNORM:
285 case PIPE_FORMAT_LATC2_UNORM:
286 return R400_TX_FORMAT_ATI2N | result;
289 return ~0; /* Unsupported/unknown. */
293 /* This is truly a special format.
294 * It stores R8G8 and B is computed using sqrt(1 - R^2 - G^2)
295 * in the sampler unit. Also known as D3DFMT_CxV8U8. */
296 if (format == PIPE_FORMAT_R8G8Bx_SNORM) {
297 return R300_TX_FORMAT_CxV8U8 | result;
300 /* Integer and fixed-point 16.16 textures are not supported. */
301 for (i = 0; i < 4; i++) {
302 if (desc->channel[i].type == UTIL_FORMAT_TYPE_FIXED ||
303 ((desc->channel[i].type == UTIL_FORMAT_TYPE_SIGNED ||
304 desc->channel[i].type == UTIL_FORMAT_TYPE_UNSIGNED) &&
305 (!desc->channel[i].normalized ||
306 desc->channel[i].pure_integer))) {
307 return ~0; /* Unsupported/unknown. */
312 for (i = 0; i < desc->nr_channels; i++) {
313 if (desc->channel[i].type == UTIL_FORMAT_TYPE_SIGNED) {
314 result |= sign_bit[i];
318 /* See whether the components are of the same size. */
319 for (i = 1; i < desc->nr_channels; i++) {
320 uniform = uniform && desc->channel[0].size == desc->channel[i].size;
323 /* Non-uniform formats. */
325 switch (desc->nr_channels) {
327 if (desc->channel[0].size == 5 &&
328 desc->channel[1].size == 6 &&
329 desc->channel[2].size == 5) {
330 return R300_TX_FORMAT_Z5Y6X5 | result;
332 if (desc->channel[0].size == 5 &&
333 desc->channel[1].size == 5 &&
334 desc->channel[2].size == 6) {
335 return R300_TX_FORMAT_Z6Y5X5 | result;
337 if (desc->channel[0].size == 2 &&
338 desc->channel[1].size == 3 &&
339 desc->channel[2].size == 3) {
340 return R300_TX_FORMAT_Z3Y3X2 | result;
342 return ~0; /* Unsupported/unknown. */
345 if (desc->channel[0].size == 5 &&
346 desc->channel[1].size == 5 &&
347 desc->channel[2].size == 5 &&
348 desc->channel[3].size == 1) {
349 return R300_TX_FORMAT_W1Z5Y5X5 | result;
351 if (desc->channel[0].size == 10 &&
352 desc->channel[1].size == 10 &&
353 desc->channel[2].size == 10 &&
354 desc->channel[3].size == 2) {
355 return R300_TX_FORMAT_W2Z10Y10X10 | result;
358 return ~0; /* Unsupported/unknown. */
361 i = util_format_get_first_non_void_channel(format);
363 return ~0; /* Unsupported/unknown. */
365 /* And finally, uniform formats. */
366 switch (desc->channel[i].type) {
367 case UTIL_FORMAT_TYPE_UNSIGNED:
368 case UTIL_FORMAT_TYPE_SIGNED:
369 if (!desc->channel[i].normalized &&
370 desc->colorspace != UTIL_FORMAT_COLORSPACE_SRGB) {
374 switch (desc->channel[i].size) {
376 switch (desc->nr_channels) {
378 return R300_TX_FORMAT_Y4X4 | result;
380 return R300_TX_FORMAT_W4Z4Y4X4 | result;
385 switch (desc->nr_channels) {
387 return R300_TX_FORMAT_X8 | result;
389 return R300_TX_FORMAT_Y8X8 | result;
391 return R300_TX_FORMAT_W8Z8Y8X8 | result;
396 switch (desc->nr_channels) {
398 return R300_TX_FORMAT_X16 | result;
400 return R300_TX_FORMAT_Y16X16 | result;
402 return R300_TX_FORMAT_W16Z16Y16X16 | result;
407 case UTIL_FORMAT_TYPE_FLOAT:
408 switch (desc->channel[i].size) {
410 switch (desc->nr_channels) {
412 return R300_TX_FORMAT_16F | result;
414 return R300_TX_FORMAT_16F_16F | result;
416 return R300_TX_FORMAT_16F_16F_16F_16F | result;
421 switch (desc->nr_channels) {
423 return R300_TX_FORMAT_32F | result;
425 return R300_TX_FORMAT_32F_32F | result;
427 return R300_TX_FORMAT_32F_32F_32F_32F | result;
432 return ~0; /* Unsupported/unknown. */
435 uint32_t r500_tx_format_msb_bit(enum pipe_format format)
438 case PIPE_FORMAT_RGTC1_UNORM:
439 case PIPE_FORMAT_RGTC1_SNORM:
440 case PIPE_FORMAT_LATC1_UNORM:
441 case PIPE_FORMAT_LATC1_SNORM:
442 case PIPE_FORMAT_X8Z24_UNORM:
443 case PIPE_FORMAT_S8_UINT_Z24_UNORM:
444 return R500_TXFORMAT_MSB;
450 /* Buffer formats. */
452 /* Colorbuffer formats. This is the unswizzled format of the RB3D block's
453 * output. For the swizzling of the targets, check the shader's format. */
454 static uint32_t r300_translate_colorformat(enum pipe_format format)
456 format = r300_unbyteswap_array_format(format);
460 case PIPE_FORMAT_A8_UNORM:
461 case PIPE_FORMAT_A8_SNORM:
462 case PIPE_FORMAT_I8_UNORM:
463 case PIPE_FORMAT_I8_SNORM:
464 case PIPE_FORMAT_L8_UNORM:
465 case PIPE_FORMAT_L8_SNORM:
466 case PIPE_FORMAT_R8_UNORM:
467 case PIPE_FORMAT_R8_SNORM:
468 return R300_COLOR_FORMAT_I8;
470 /* 16-bit buffers. */
471 case PIPE_FORMAT_L8A8_UNORM:
472 case PIPE_FORMAT_L8A8_SNORM:
473 case PIPE_FORMAT_R8G8_UNORM:
474 case PIPE_FORMAT_R8G8_SNORM:
475 case PIPE_FORMAT_R8A8_UNORM:
476 case PIPE_FORMAT_R8A8_SNORM:
477 /* These formats work fine with UV88 if US_OUT_FMT is set correctly. */
478 case PIPE_FORMAT_A16_UNORM:
479 case PIPE_FORMAT_A16_SNORM:
480 case PIPE_FORMAT_A16_FLOAT:
481 case PIPE_FORMAT_L16_UNORM:
482 case PIPE_FORMAT_L16_SNORM:
483 case PIPE_FORMAT_L16_FLOAT:
484 case PIPE_FORMAT_I16_UNORM:
485 case PIPE_FORMAT_I16_SNORM:
486 case PIPE_FORMAT_I16_FLOAT:
487 case PIPE_FORMAT_R16_UNORM:
488 case PIPE_FORMAT_R16_SNORM:
489 case PIPE_FORMAT_R16_FLOAT:
490 return R300_COLOR_FORMAT_UV88;
492 case PIPE_FORMAT_B5G6R5_UNORM:
493 return R300_COLOR_FORMAT_RGB565;
495 case PIPE_FORMAT_B5G5R5A1_UNORM:
496 case PIPE_FORMAT_B5G5R5X1_UNORM:
497 return R300_COLOR_FORMAT_ARGB1555;
499 case PIPE_FORMAT_B4G4R4A4_UNORM:
500 case PIPE_FORMAT_B4G4R4X4_UNORM:
501 return R300_COLOR_FORMAT_ARGB4444;
503 /* 32-bit buffers. */
504 case PIPE_FORMAT_B8G8R8A8_UNORM:
505 /*case PIPE_FORMAT_B8G8R8A8_SNORM:*/
506 case PIPE_FORMAT_B8G8R8X8_UNORM:
507 /*case PIPE_FORMAT_B8G8R8X8_SNORM:*/
508 case PIPE_FORMAT_R8G8B8A8_UNORM:
509 case PIPE_FORMAT_R8G8B8A8_SNORM:
510 case PIPE_FORMAT_R8G8B8X8_UNORM:
511 case PIPE_FORMAT_R8G8B8X8_SNORM:
512 /* These formats work fine with ARGB8888 if US_OUT_FMT is set
514 case PIPE_FORMAT_R16G16_UNORM:
515 case PIPE_FORMAT_R16G16_SNORM:
516 case PIPE_FORMAT_R16G16_FLOAT:
517 case PIPE_FORMAT_L16A16_UNORM:
518 case PIPE_FORMAT_L16A16_SNORM:
519 case PIPE_FORMAT_L16A16_FLOAT:
520 case PIPE_FORMAT_R16A16_UNORM:
521 case PIPE_FORMAT_R16A16_SNORM:
522 case PIPE_FORMAT_R16A16_FLOAT:
523 case PIPE_FORMAT_A32_FLOAT:
524 case PIPE_FORMAT_L32_FLOAT:
525 case PIPE_FORMAT_I32_FLOAT:
526 case PIPE_FORMAT_R32_FLOAT:
527 return R300_COLOR_FORMAT_ARGB8888;
529 case PIPE_FORMAT_R10G10B10A2_UNORM:
530 case PIPE_FORMAT_R10G10B10X2_SNORM:
531 case PIPE_FORMAT_B10G10R10A2_UNORM:
532 case PIPE_FORMAT_B10G10R10X2_UNORM:
533 return R500_COLOR_FORMAT_ARGB2101010; /* R5xx-only? */
535 /* 64-bit buffers. */
536 case PIPE_FORMAT_R16G16B16A16_UNORM:
537 case PIPE_FORMAT_R16G16B16A16_SNORM:
538 case PIPE_FORMAT_R16G16B16A16_FLOAT:
539 case PIPE_FORMAT_R16G16B16X16_UNORM:
540 case PIPE_FORMAT_R16G16B16X16_SNORM:
541 case PIPE_FORMAT_R16G16B16X16_FLOAT:
542 /* These formats work fine with ARGB16161616 if US_OUT_FMT is set
544 case PIPE_FORMAT_R32G32_FLOAT:
545 case PIPE_FORMAT_L32A32_FLOAT:
546 case PIPE_FORMAT_R32A32_FLOAT:
547 return R300_COLOR_FORMAT_ARGB16161616;
549 /* 128-bit buffers. */
550 case PIPE_FORMAT_R32G32B32A32_FLOAT:
551 case PIPE_FORMAT_R32G32B32X32_FLOAT:
552 return R300_COLOR_FORMAT_ARGB32323232;
555 case PIPE_FORMAT_UYVY:
556 return R300_COLOR_FORMAT_YVYU;
557 case PIPE_FORMAT_YUYV:
558 return R300_COLOR_FORMAT_VYUY;
560 return ~0; /* Unsupported. */
564 /* Depthbuffer and stencilbuffer. Thankfully, we only support two flavors. */
565 static uint32_t r300_translate_zsformat(enum pipe_format format)
568 /* 16-bit depth, no stencil */
569 case PIPE_FORMAT_Z16_UNORM:
570 return R300_DEPTHFORMAT_16BIT_INT_Z;
571 /* 24-bit depth, ignored stencil */
572 case PIPE_FORMAT_X8Z24_UNORM:
573 /* 24-bit depth, 8-bit stencil */
574 case PIPE_FORMAT_S8_UINT_Z24_UNORM:
575 return R300_DEPTHFORMAT_24BIT_INT_Z_8BIT_STENCIL;
577 return ~0; /* Unsupported. */
581 /* Shader output formats. This is essentially the swizzle from the shader
584 * Note that formats are stored from C3 to C0. */
585 static uint32_t r300_translate_out_fmt(enum pipe_format format)
587 uint32_t modifier = 0;
589 const struct util_format_description *desc;
592 format = r300_unbyteswap_array_format(format);
593 desc = util_format_description(format);
595 i = util_format_get_first_non_void_channel(format);
597 return ~0; /* Unsupported/unknown. */
599 /* Specifies how the shader output is written to the fog unit. */
600 switch (desc->channel[i].type) {
601 case UTIL_FORMAT_TYPE_FLOAT:
602 switch (desc->channel[i].size) {
604 switch (desc->nr_channels) {
606 modifier |= R300_US_OUT_FMT_C_32_FP;
609 modifier |= R300_US_OUT_FMT_C2_32_FP;
612 modifier |= R300_US_OUT_FMT_C4_32_FP;
618 switch (desc->nr_channels) {
620 modifier |= R300_US_OUT_FMT_C_16_FP;
623 modifier |= R300_US_OUT_FMT_C2_16_FP;
626 modifier |= R300_US_OUT_FMT_C4_16_FP;
634 switch (desc->channel[i].size) {
636 switch (desc->nr_channels) {
638 modifier |= R300_US_OUT_FMT_C_16;
641 modifier |= R300_US_OUT_FMT_C2_16;
644 modifier |= R300_US_OUT_FMT_C4_16;
650 modifier |= R300_US_OUT_FMT_C4_10;
654 /* C4_8 seems to be used for the formats whose pixel size
656 modifier |= R300_US_OUT_FMT_C4_8;
663 for (i = 0; i < desc->nr_channels; i++)
664 if (desc->channel[i].type != UTIL_FORMAT_TYPE_SIGNED)
665 uniform_sign = false;
668 modifier |= R300_OUT_SIGN(0xf);
670 /* Add swizzles and return. */
672 /*** Special cases (non-standard channel mapping) ***/
675 * COLORFORMAT_I8 stores the Z component (C2). */
676 case PIPE_FORMAT_A8_UNORM:
677 case PIPE_FORMAT_A8_SNORM:
678 return modifier | R300_C2_SEL_A;
679 case PIPE_FORMAT_I8_UNORM:
680 case PIPE_FORMAT_I8_SNORM:
681 case PIPE_FORMAT_L8_UNORM:
682 case PIPE_FORMAT_L8_SNORM:
683 case PIPE_FORMAT_R8_UNORM:
684 case PIPE_FORMAT_R8_SNORM:
685 return modifier | R300_C2_SEL_R;
688 * COLORFORMAT_UV88 stores ZX (C2 and C0). */
689 case PIPE_FORMAT_L8A8_SNORM:
690 case PIPE_FORMAT_L8A8_UNORM:
691 case PIPE_FORMAT_R8A8_SNORM:
692 case PIPE_FORMAT_R8A8_UNORM:
693 return modifier | R300_C0_SEL_A | R300_C2_SEL_R;
694 case PIPE_FORMAT_R8G8_SNORM:
695 case PIPE_FORMAT_R8G8_UNORM:
696 return modifier | R300_C0_SEL_G | R300_C2_SEL_R;
699 * ARGB16161616 stores XZ for RG32F */
700 case PIPE_FORMAT_R32G32_FLOAT:
701 return modifier | R300_C0_SEL_R | R300_C2_SEL_G;
703 /*** Generic cases (standard channel mapping) ***/
706 case PIPE_FORMAT_B5G6R5_UNORM:
707 case PIPE_FORMAT_B5G5R5A1_UNORM:
708 case PIPE_FORMAT_B5G5R5X1_UNORM:
709 case PIPE_FORMAT_B4G4R4A4_UNORM:
710 case PIPE_FORMAT_B4G4R4X4_UNORM:
711 case PIPE_FORMAT_B8G8R8A8_UNORM:
712 /*case PIPE_FORMAT_B8G8R8A8_SNORM:*/
713 case PIPE_FORMAT_B8G8R8X8_UNORM:
714 /*case PIPE_FORMAT_B8G8R8X8_SNORM:*/
715 case PIPE_FORMAT_B10G10R10A2_UNORM:
716 case PIPE_FORMAT_B10G10R10X2_UNORM:
718 R300_C0_SEL_B | R300_C1_SEL_G |
719 R300_C2_SEL_R | R300_C3_SEL_A;
722 case PIPE_FORMAT_A16_UNORM:
723 case PIPE_FORMAT_A16_SNORM:
724 case PIPE_FORMAT_A16_FLOAT:
725 case PIPE_FORMAT_A32_FLOAT:
727 R300_C0_SEL_A | R300_C1_SEL_R |
728 R300_C2_SEL_G | R300_C3_SEL_B;
731 case PIPE_FORMAT_R8G8B8X8_UNORM:
732 case PIPE_FORMAT_R8G8B8X8_SNORM:
733 case PIPE_FORMAT_R8G8B8A8_UNORM:
734 case PIPE_FORMAT_R8G8B8A8_SNORM:
735 case PIPE_FORMAT_R10G10B10A2_UNORM:
736 case PIPE_FORMAT_R10G10B10X2_SNORM:
737 case PIPE_FORMAT_R16_UNORM:
738 case PIPE_FORMAT_R16G16_UNORM:
739 case PIPE_FORMAT_R16G16B16A16_UNORM:
740 case PIPE_FORMAT_R16_SNORM:
741 case PIPE_FORMAT_R16G16_SNORM:
742 case PIPE_FORMAT_R16G16B16A16_SNORM:
743 case PIPE_FORMAT_R16_FLOAT:
744 case PIPE_FORMAT_R16G16_FLOAT:
745 case PIPE_FORMAT_R16G16B16A16_FLOAT:
746 case PIPE_FORMAT_R32_FLOAT:
747 case PIPE_FORMAT_R32G32B32A32_FLOAT:
748 case PIPE_FORMAT_R32G32B32X32_FLOAT:
749 case PIPE_FORMAT_L16_UNORM:
750 case PIPE_FORMAT_L16_SNORM:
751 case PIPE_FORMAT_L16_FLOAT:
752 case PIPE_FORMAT_L32_FLOAT:
753 case PIPE_FORMAT_I16_UNORM:
754 case PIPE_FORMAT_I16_SNORM:
755 case PIPE_FORMAT_I16_FLOAT:
756 case PIPE_FORMAT_I32_FLOAT:
757 case PIPE_FORMAT_R16G16B16X16_UNORM:
758 case PIPE_FORMAT_R16G16B16X16_SNORM:
759 case PIPE_FORMAT_R16G16B16X16_FLOAT:
761 R300_C0_SEL_R | R300_C1_SEL_G |
762 R300_C2_SEL_B | R300_C3_SEL_A;
765 case PIPE_FORMAT_L16A16_UNORM:
766 case PIPE_FORMAT_L16A16_SNORM:
767 case PIPE_FORMAT_L16A16_FLOAT:
768 case PIPE_FORMAT_R16A16_UNORM:
769 case PIPE_FORMAT_R16A16_SNORM:
770 case PIPE_FORMAT_R16A16_FLOAT:
771 case PIPE_FORMAT_L32A32_FLOAT:
772 case PIPE_FORMAT_R32A32_FLOAT:
774 R300_C0_SEL_R | R300_C1_SEL_A;
777 return ~0; /* Unsupported. */
781 static uint32_t r300_translate_colormask_swizzle(enum pipe_format format)
783 format = r300_unbyteswap_array_format(format);
786 case PIPE_FORMAT_A8_UNORM:
787 case PIPE_FORMAT_A8_SNORM:
788 case PIPE_FORMAT_A16_UNORM:
789 case PIPE_FORMAT_A16_SNORM:
790 case PIPE_FORMAT_A16_FLOAT:
791 case PIPE_FORMAT_A32_FLOAT:
792 return COLORMASK_AAAA;
794 case PIPE_FORMAT_I8_UNORM:
795 case PIPE_FORMAT_I8_SNORM:
796 case PIPE_FORMAT_L8_UNORM:
797 case PIPE_FORMAT_L8_SNORM:
798 case PIPE_FORMAT_R8_UNORM:
799 case PIPE_FORMAT_R8_SNORM:
800 case PIPE_FORMAT_R32_FLOAT:
801 case PIPE_FORMAT_L32_FLOAT:
802 case PIPE_FORMAT_I32_FLOAT:
803 return COLORMASK_RRRR;
805 case PIPE_FORMAT_L8A8_SNORM:
806 case PIPE_FORMAT_L8A8_UNORM:
807 case PIPE_FORMAT_R8A8_UNORM:
808 case PIPE_FORMAT_R8A8_SNORM:
809 case PIPE_FORMAT_L16A16_UNORM:
810 case PIPE_FORMAT_L16A16_SNORM:
811 case PIPE_FORMAT_L16A16_FLOAT:
812 case PIPE_FORMAT_R16A16_UNORM:
813 case PIPE_FORMAT_R16A16_SNORM:
814 case PIPE_FORMAT_R16A16_FLOAT:
815 case PIPE_FORMAT_L32A32_FLOAT:
816 case PIPE_FORMAT_R32A32_FLOAT:
817 return COLORMASK_ARRA;
819 case PIPE_FORMAT_R8G8_SNORM:
820 case PIPE_FORMAT_R8G8_UNORM:
821 case PIPE_FORMAT_R16G16_UNORM:
822 case PIPE_FORMAT_R16G16_SNORM:
823 case PIPE_FORMAT_R16G16_FLOAT:
824 case PIPE_FORMAT_R32G32_FLOAT:
825 return COLORMASK_GRRG;
827 case PIPE_FORMAT_B5G5R5X1_UNORM:
828 case PIPE_FORMAT_B4G4R4X4_UNORM:
829 case PIPE_FORMAT_B8G8R8X8_UNORM:
830 /*case PIPE_FORMAT_B8G8R8X8_SNORM:*/
831 case PIPE_FORMAT_B10G10R10X2_UNORM:
832 return COLORMASK_BGRX;
834 case PIPE_FORMAT_B5G6R5_UNORM:
835 case PIPE_FORMAT_B5G5R5A1_UNORM:
836 case PIPE_FORMAT_B4G4R4A4_UNORM:
837 case PIPE_FORMAT_B8G8R8A8_UNORM:
838 /*case PIPE_FORMAT_B8G8R8A8_SNORM:*/
839 case PIPE_FORMAT_B10G10R10A2_UNORM:
840 return COLORMASK_BGRA;
842 case PIPE_FORMAT_R8G8B8X8_UNORM:
843 /* RGBX_SNORM formats are broken for an unknown reason */
844 /*case PIPE_FORMAT_R8G8B8X8_SNORM:*/
845 /*case PIPE_FORMAT_R10G10B10X2_SNORM:*/
846 case PIPE_FORMAT_R16G16B16X16_UNORM:
847 /*case PIPE_FORMAT_R16G16B16X16_SNORM:*/
848 case PIPE_FORMAT_R16G16B16X16_FLOAT:
849 case PIPE_FORMAT_R32G32B32X32_FLOAT:
850 return COLORMASK_RGBX;
852 case PIPE_FORMAT_R8G8B8A8_UNORM:
853 case PIPE_FORMAT_R8G8B8A8_SNORM:
854 case PIPE_FORMAT_R10G10B10A2_UNORM:
855 case PIPE_FORMAT_R16_UNORM:
856 case PIPE_FORMAT_R16G16B16A16_UNORM:
857 case PIPE_FORMAT_R16_SNORM:
858 case PIPE_FORMAT_R16G16B16A16_SNORM:
859 case PIPE_FORMAT_R16_FLOAT:
860 case PIPE_FORMAT_R16G16B16A16_FLOAT:
861 case PIPE_FORMAT_R32G32B32A32_FLOAT:
862 case PIPE_FORMAT_L16_UNORM:
863 case PIPE_FORMAT_L16_SNORM:
864 case PIPE_FORMAT_L16_FLOAT:
865 case PIPE_FORMAT_I16_UNORM:
866 case PIPE_FORMAT_I16_SNORM:
867 case PIPE_FORMAT_I16_FLOAT:
868 return COLORMASK_RGBA;
871 return ~0; /* Unsupported. */
875 bool r300_is_colorbuffer_format_supported(enum pipe_format format)
877 return r300_translate_colorformat(format) != ~0 &&
878 r300_translate_out_fmt(format) != ~0 &&
879 r300_translate_colormask_swizzle(format) != ~0;
882 bool r300_is_zs_format_supported(enum pipe_format format)
884 return r300_translate_zsformat(format) != ~0;
887 bool r300_is_sampler_format_supported(enum pipe_format format)
889 return r300_translate_texformat(format, NULL, true, false) != ~0;
892 void r300_texture_setup_format_state(struct r300_screen *screen,
893 struct r300_resource *tex,
894 enum pipe_format format,
896 unsigned width0_override,
897 unsigned height0_override,
898 struct r300_texture_format_state *out)
900 struct pipe_resource *pt = &tex->b;
901 struct r300_texture_desc *desc = &tex->tex;
902 bool is_r500 = screen->caps.is_r500;
903 unsigned width, height, depth;
904 unsigned txwidth, txheight, txdepth;
906 width = u_minify(width0_override, level);
907 height = u_minify(height0_override, level);
908 depth = u_minify(desc->depth0, level);
910 txwidth = (width - 1) & 0x7ff;
911 txheight = (height - 1) & 0x7ff;
912 txdepth = util_logbase2(depth) & 0xf;
914 /* Mask out all the fields we change. */
916 out->format1 &= ~R300_TX_FORMAT_TEX_COORD_TYPE_MASK;
917 out->format2 &= R500_TXFORMAT_MSB;
918 out->tile_config = 0;
920 /* Set sampler state. */
922 R300_TX_WIDTH(txwidth) |
923 R300_TX_HEIGHT(txheight) |
924 R300_TX_DEPTH(txdepth);
926 if (desc->uses_stride_addressing) {
928 r300_stride_to_width(format, desc->stride_in_bytes[level]);
929 /* rectangles love this */
930 out->format0 |= R300_TX_PITCH_EN;
931 out->format2 = (stride - 1) & 0x1fff;
934 if (pt->target == PIPE_TEXTURE_CUBE) {
935 out->format1 |= R300_TX_FORMAT_CUBIC_MAP;
937 if (pt->target == PIPE_TEXTURE_3D) {
938 out->format1 |= R300_TX_FORMAT_3D;
941 /* large textures on r500 */
944 unsigned us_width = txwidth;
945 unsigned us_height = txheight;
946 unsigned us_depth = txdepth;
949 out->format2 |= R500_TXWIDTH_BIT11;
952 out->format2 |= R500_TXHEIGHT_BIT11;
955 /* The US_FORMAT register fixes an R500 TX addressing bug.
956 * Don't ask why it must be set like this. I don't know it either. */
958 us_width = (0x000007FF + us_width) >> 1;
959 us_depth |= 0x0000000D;
962 us_height = (0x000007FF + us_height) >> 1;
963 us_depth |= 0x0000000E;
967 R300_TX_WIDTH(us_width) |
968 R300_TX_HEIGHT(us_height) |
969 R300_TX_DEPTH(us_depth);
972 out->tile_config = R300_TXO_MACRO_TILE(desc->macrotile[level]) |
973 R300_TXO_MICRO_TILE(desc->microtile) |
974 R300_TXO_ENDIAN(r300_get_endian_swap(format));
977 static void r300_texture_setup_fb_state(struct r300_surface *surf)
979 struct r300_resource *tex = r300_resource(surf->base.texture);
980 unsigned level = surf->base.u.tex.level;
982 r300_stride_to_width(surf->base.format, tex->tex.stride_in_bytes[level]);
984 /* Set framebuffer state. */
985 if (util_format_is_depth_or_stencil(surf->base.format)) {
988 R300_DEPTHMACROTILE(tex->tex.macrotile[level]) |
989 R300_DEPTHMICROTILE(tex->tex.microtile) |
990 R300_DEPTHENDIAN(r300_get_endian_swap(surf->base.format));
991 surf->format = r300_translate_zsformat(surf->base.format);
992 surf->pitch_zmask = tex->tex.zmask_stride_in_pixels[level];
993 surf->pitch_hiz = tex->tex.hiz_stride_in_pixels[level];
995 enum pipe_format format = util_format_linear(surf->base.format);
999 r300_translate_colorformat(format) |
1000 R300_COLOR_TILE(tex->tex.macrotile[level]) |
1001 R300_COLOR_MICROTILE(tex->tex.microtile) |
1002 R300_COLOR_ENDIAN(r300_get_endian_swap(format));
1003 surf->format = r300_translate_out_fmt(format);
1004 surf->colormask_swizzle =
1005 r300_translate_colormask_swizzle(format);
1006 surf->pitch_cmask = tex->tex.cmask_stride_in_pixels;
1010 bool r300_resource_get_handle(struct pipe_screen* screen,
1011 struct pipe_context *ctx,
1012 struct pipe_resource *texture,
1013 struct winsys_handle *whandle,
1016 struct radeon_winsys *rws = r300_screen(screen)->rws;
1017 struct r300_resource* tex = (struct r300_resource*)texture;
1023 whandle->stride = tex->tex.stride_in_bytes[0];
1024 whandle->offset = 0;
1026 return rws->buffer_get_handle(rws, tex->buf, whandle);
1029 /* The common texture constructor. */
1030 static struct r300_resource*
1031 r300_texture_create_object(struct r300_screen *rscreen,
1032 const struct pipe_resource *base,
1033 enum radeon_bo_layout microtile,
1034 enum radeon_bo_layout macrotile,
1035 unsigned stride_in_bytes_override,
1036 struct pb_buffer *buffer)
1038 struct radeon_winsys *rws = rscreen->rws;
1039 struct r300_resource *tex = NULL;
1040 struct radeon_bo_metadata tiling = {};
1042 tex = CALLOC_STRUCT(r300_resource);
1047 pipe_reference_init(&tex->b.reference, 1);
1048 tex->b.screen = &rscreen->screen;
1049 tex->b.usage = base->usage;
1050 tex->b.bind = base->bind;
1051 tex->b.flags = base->flags;
1052 tex->tex.microtile = microtile;
1053 tex->tex.macrotile[0] = macrotile;
1054 tex->tex.stride_in_bytes_override = stride_in_bytes_override;
1055 tex->domain = (base->flags & R300_RESOURCE_FLAG_TRANSFER ||
1056 base->usage == PIPE_USAGE_STAGING) ? RADEON_DOMAIN_GTT :
1057 base->nr_samples > 1 ? RADEON_DOMAIN_VRAM :
1058 RADEON_DOMAIN_VRAM | RADEON_DOMAIN_GTT;
1061 r300_texture_desc_init(rscreen, tex, base);
1063 /* Figure out the ideal placement for the texture.. */
1064 if (tex->domain & RADEON_DOMAIN_VRAM &&
1065 tex->tex.size_in_bytes >= (uint64_t)rscreen->info.vram_size_kb * 1024) {
1066 tex->domain &= ~RADEON_DOMAIN_VRAM;
1067 tex->domain |= RADEON_DOMAIN_GTT;
1069 if (tex->domain & RADEON_DOMAIN_GTT &&
1070 tex->tex.size_in_bytes >= (uint64_t)rscreen->info.gart_size_kb * 1024) {
1071 tex->domain &= ~RADEON_DOMAIN_GTT;
1073 /* Just fail if the texture is too large. */
1078 /* Create the backing buffer if needed. */
1080 /* Only use the first domain for allocation. Multiple domains are not allowed. */
1081 unsigned alloc_domain =
1082 tex->domain & RADEON_DOMAIN_VRAM ? RADEON_DOMAIN_VRAM :
1085 tex->buf = rws->buffer_create(rws, tex->tex.size_in_bytes, 2048,
1087 RADEON_FLAG_NO_SUBALLOC |
1088 /* Use the reusable pool: */
1089 RADEON_FLAG_NO_INTERPROCESS_SHARING);
1096 if (SCREEN_DBG_ON(rscreen, DBG_MSAA) && base->nr_samples > 1) {
1097 fprintf(stderr, "r300: %ix MSAA %s buffer created\n",
1099 util_format_is_depth_or_stencil(base->format) ? "depth" : "color");
1102 tiling.u.legacy.microtile = tex->tex.microtile;
1103 tiling.u.legacy.macrotile = tex->tex.macrotile[0];
1104 tiling.u.legacy.stride = tex->tex.stride_in_bytes[0];
1105 rws->buffer_set_metadata(rws, tex->buf, &tiling, NULL);
1112 pb_reference(&buffer, NULL);
1116 /* Create a new texture. */
1117 struct pipe_resource *r300_texture_create(struct pipe_screen *screen,
1118 const struct pipe_resource *base)
1120 struct r300_screen *rscreen = r300_screen(screen);
1121 enum radeon_bo_layout microtile, macrotile;
1123 if ((base->flags & R300_RESOURCE_FLAG_TRANSFER) ||
1124 (base->bind & (PIPE_BIND_SCANOUT | PIPE_BIND_LINEAR))) {
1125 microtile = RADEON_LAYOUT_LINEAR;
1126 macrotile = RADEON_LAYOUT_LINEAR;
1128 /* This will make the texture_create_function select the layout. */
1129 microtile = RADEON_LAYOUT_UNKNOWN;
1130 macrotile = RADEON_LAYOUT_UNKNOWN;
1133 return (struct pipe_resource*)
1134 r300_texture_create_object(rscreen, base, microtile, macrotile,
1138 struct pipe_resource *r300_texture_from_handle(struct pipe_screen *screen,
1139 const struct pipe_resource *base,
1140 struct winsys_handle *whandle,
1143 struct r300_screen *rscreen = r300_screen(screen);
1144 struct radeon_winsys *rws = rscreen->rws;
1145 struct pb_buffer *buffer;
1146 struct radeon_bo_metadata tiling = {};
1148 /* Support only 2D textures without mipmaps */
1149 if ((base->target != PIPE_TEXTURE_2D &&
1150 base->target != PIPE_TEXTURE_RECT) ||
1151 base->depth0 != 1 ||
1152 base->last_level != 0) {
1156 buffer = rws->buffer_from_handle(rws, whandle, 0, false);
1160 rws->buffer_get_metadata(rws, buffer, &tiling, NULL);
1162 /* Enforce a microtiled zbuffer. */
1163 if (util_format_is_depth_or_stencil(base->format) &&
1164 tiling.u.legacy.microtile == RADEON_LAYOUT_LINEAR) {
1165 switch (util_format_get_blocksize(base->format)) {
1167 tiling.u.legacy.microtile = RADEON_LAYOUT_TILED;
1171 tiling.u.legacy.microtile = RADEON_LAYOUT_SQUARETILED;
1176 return (struct pipe_resource*)
1177 r300_texture_create_object(rscreen, base, tiling.u.legacy.microtile, tiling.u.legacy.macrotile,
1178 whandle->stride, buffer);
1181 struct pipe_surface* r300_create_surface_custom(struct pipe_context * ctx,
1182 struct pipe_resource* texture,
1183 const struct pipe_surface *surf_tmpl,
1184 unsigned width0_override,
1185 unsigned height0_override)
1187 struct r300_resource* tex = r300_resource(texture);
1188 struct r300_surface* surface = CALLOC_STRUCT(r300_surface);
1189 unsigned level = surf_tmpl->u.tex.level;
1191 assert(surf_tmpl->u.tex.first_layer == surf_tmpl->u.tex.last_layer);
1194 uint32_t offset, tile_height;
1196 pipe_reference_init(&surface->base.reference, 1);
1197 pipe_resource_reference(&surface->base.texture, texture);
1198 surface->base.context = ctx;
1199 surface->base.format = surf_tmpl->format;
1200 surface->base.width = u_minify(width0_override, level);
1201 surface->base.height = u_minify(height0_override, level);
1202 surface->base.u.tex.level = level;
1203 surface->base.u.tex.first_layer = surf_tmpl->u.tex.first_layer;
1204 surface->base.u.tex.last_layer = surf_tmpl->u.tex.last_layer;
1206 surface->buf = tex->buf;
1208 /* Prefer VRAM if there are multiple domains to choose from. */
1209 surface->domain = tex->domain;
1210 if (surface->domain & RADEON_DOMAIN_VRAM)
1211 surface->domain &= ~RADEON_DOMAIN_GTT;
1213 surface->offset = r300_texture_get_offset(tex, level,
1214 surf_tmpl->u.tex.first_layer);
1215 r300_texture_setup_fb_state(surface);
1217 /* Parameters for the CBZB clear. */
1218 surface->cbzb_allowed = tex->tex.cbzb_allowed[level];
1219 surface->cbzb_width = align(surface->base.width, 64);
1221 /* Height must be aligned to the size of a tile. */
1222 tile_height = r300_get_pixel_alignment(surface->base.format,
1225 tex->tex.macrotile[level],
1228 surface->cbzb_height = align((surface->base.height + 1) / 2,
1231 /* Offset must be aligned to 2K and must point at the beginning
1233 offset = surface->offset +
1234 tex->tex.stride_in_bytes[level] * surface->cbzb_height;
1235 surface->cbzb_midpoint_offset = offset & ~2047;
1237 surface->cbzb_pitch = surface->pitch & 0x1ffffc;
1239 if (util_format_get_blocksizebits(surface->base.format) == 32)
1240 surface->cbzb_format = R300_DEPTHFORMAT_24BIT_INT_Z_8BIT_STENCIL;
1242 surface->cbzb_format = R300_DEPTHFORMAT_16BIT_INT_Z;
1244 DBG(r300_context(ctx), DBG_CBZB,
1245 "CBZB Allowed: %s, Dim: %ix%i, Misalignment: %i, Micro: %s, Macro: %s\n",
1246 surface->cbzb_allowed ? "YES" : " NO",
1247 surface->cbzb_width, surface->cbzb_height,
1249 tex->tex.microtile ? "YES" : " NO",
1250 tex->tex.macrotile[level] ? "YES" : " NO");
1253 return &surface->base;
1256 struct pipe_surface* r300_create_surface(struct pipe_context * ctx,
1257 struct pipe_resource* texture,
1258 const struct pipe_surface *surf_tmpl)
1260 return r300_create_surface_custom(ctx, texture, surf_tmpl,
1265 void r300_surface_destroy(struct pipe_context *ctx, struct pipe_surface* s)
1267 pipe_resource_reference(&s->texture, NULL);