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/u_format.h"
34 #include "util/u_format_s3tc.h"
35 #include "util/u_math.h"
36 #include "util/u_memory.h"
37 #include "util/u_mm.h"
39 #include "pipe/p_screen.h"
41 unsigned r300_get_swizzle_combined(const unsigned char *swizzle_format
,
42 const unsigned char *swizzle_view
,
46 unsigned char swizzle
[4];
48 const uint32_t swizzle_shift
[4] = {
49 R300_TX_FORMAT_R_SHIFT
,
50 R300_TX_FORMAT_G_SHIFT
,
51 R300_TX_FORMAT_B_SHIFT
,
52 R300_TX_FORMAT_A_SHIFT
54 uint32_t swizzle_bit
[4] = {
55 dxtc_swizzle
? R300_TX_FORMAT_Z
: R300_TX_FORMAT_X
,
57 dxtc_swizzle
? R300_TX_FORMAT_X
: R300_TX_FORMAT_Z
,
62 /* Combine two sets of swizzles. */
63 util_format_compose_swizzles(swizzle_format
, swizzle_view
, swizzle
);
65 memcpy(swizzle
, swizzle_format
, 4);
69 for (i
= 0; i
< 4; i
++) {
71 case UTIL_FORMAT_SWIZZLE_Y
:
72 result
|= swizzle_bit
[1] << swizzle_shift
[i
];
74 case UTIL_FORMAT_SWIZZLE_Z
:
75 result
|= swizzle_bit
[2] << swizzle_shift
[i
];
77 case UTIL_FORMAT_SWIZZLE_W
:
78 result
|= swizzle_bit
[3] << swizzle_shift
[i
];
80 case UTIL_FORMAT_SWIZZLE_0
:
81 result
|= R300_TX_FORMAT_ZERO
<< swizzle_shift
[i
];
83 case UTIL_FORMAT_SWIZZLE_1
:
84 result
|= R300_TX_FORMAT_ONE
<< swizzle_shift
[i
];
86 default: /* UTIL_FORMAT_SWIZZLE_X */
87 result
|= swizzle_bit
[0] << swizzle_shift
[i
];
93 /* Translate a pipe_format into a useful texture format for sampling.
95 * Some special formats are translated directly using R300_EASY_TX_FORMAT,
96 * but the majority of them is translated in a generic way, automatically
97 * supporting all the formats hw can support.
99 * R300_EASY_TX_FORMAT swizzles the texture.
100 * Note the signature of R300_EASY_TX_FORMAT:
101 * R300_EASY_TX_FORMAT(B, G, R, A, FORMAT);
103 * The FORMAT specifies how the texture sampler will treat the texture, and
104 * makes available X, Y, Z, W, ZERO, and ONE for swizzling. */
105 uint32_t r300_translate_texformat(enum pipe_format format
,
106 const unsigned char *swizzle_view
,
108 boolean dxtc_swizzle
)
111 const struct util_format_description
*desc
;
113 boolean uniform
= TRUE
;
114 const uint32_t sign_bit
[4] = {
115 R300_TX_FORMAT_SIGNED_W
,
116 R300_TX_FORMAT_SIGNED_Z
,
117 R300_TX_FORMAT_SIGNED_Y
,
118 R300_TX_FORMAT_SIGNED_X
,
121 desc
= util_format_description(format
);
123 /* Colorspace (return non-RGB formats directly). */
124 switch (desc
->colorspace
) {
125 /* Depth stencil formats.
126 * Swizzles are added in r300_merge_textures_and_samplers. */
127 case UTIL_FORMAT_COLORSPACE_ZS
:
129 case PIPE_FORMAT_Z16_UNORM
:
130 return R300_TX_FORMAT_X16
;
131 case PIPE_FORMAT_X8Z24_UNORM
:
132 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
134 return R500_TX_FORMAT_Y8X24
;
136 return R300_TX_FORMAT_Y16X16
;
138 return ~0; /* Unsupported. */
142 case UTIL_FORMAT_COLORSPACE_YUV
:
143 result
|= R300_TX_FORMAT_YUV_TO_RGB
;
146 case PIPE_FORMAT_UYVY
:
147 return R300_EASY_TX_FORMAT(X
, Y
, Z
, ONE
, YVYU422
) | result
;
148 case PIPE_FORMAT_YUYV
:
149 return R300_EASY_TX_FORMAT(X
, Y
, Z
, ONE
, VYUY422
) | result
;
151 return ~0; /* Unsupported/unknown. */
154 /* Add gamma correction. */
155 case UTIL_FORMAT_COLORSPACE_SRGB
:
156 result
|= R300_TX_FORMAT_GAMMA
;
161 /* Same as YUV but without the YUR->RGB conversion. */
162 case PIPE_FORMAT_R8G8_B8G8_UNORM
:
163 return R300_EASY_TX_FORMAT(X
, Y
, Z
, ONE
, YVYU422
) | result
;
164 case PIPE_FORMAT_G8R8_G8B8_UNORM
:
165 return R300_EASY_TX_FORMAT(X
, Y
, Z
, ONE
, VYUY422
) | result
;
171 /* The RGTC1_SNORM and LATC1_SNORM swizzle is done in the shader. */
172 if (format
!= PIPE_FORMAT_RGTC1_SNORM
&&
173 format
!= PIPE_FORMAT_LATC1_SNORM
) {
174 if (util_format_is_compressed(format
) &&
176 format
!= PIPE_FORMAT_RGTC2_UNORM
&&
177 format
!= PIPE_FORMAT_RGTC2_SNORM
&&
178 format
!= PIPE_FORMAT_LATC2_UNORM
&&
179 format
!= PIPE_FORMAT_LATC2_SNORM
) {
180 result
|= r300_get_swizzle_combined(desc
->swizzle
, swizzle_view
,
183 result
|= r300_get_swizzle_combined(desc
->swizzle
, swizzle_view
,
189 if (desc
->layout
== UTIL_FORMAT_LAYOUT_S3TC
) {
190 if (!util_format_s3tc_enabled
) {
191 return ~0; /* Unsupported. */
195 case PIPE_FORMAT_DXT1_RGB
:
196 case PIPE_FORMAT_DXT1_RGBA
:
197 case PIPE_FORMAT_DXT1_SRGB
:
198 case PIPE_FORMAT_DXT1_SRGBA
:
199 return R300_TX_FORMAT_DXT1
| result
;
200 case PIPE_FORMAT_DXT3_RGBA
:
201 case PIPE_FORMAT_DXT3_SRGBA
:
202 return R300_TX_FORMAT_DXT3
| result
;
203 case PIPE_FORMAT_DXT5_RGBA
:
204 case PIPE_FORMAT_DXT5_SRGBA
:
205 return R300_TX_FORMAT_DXT5
| result
;
207 return ~0; /* Unsupported/unknown. */
212 if (desc
->layout
== UTIL_FORMAT_LAYOUT_RGTC
) {
214 case PIPE_FORMAT_RGTC1_SNORM
:
215 case PIPE_FORMAT_LATC1_SNORM
:
216 case PIPE_FORMAT_LATC1_UNORM
:
217 case PIPE_FORMAT_RGTC1_UNORM
:
218 return R500_TX_FORMAT_ATI1N
| result
;
220 case PIPE_FORMAT_RGTC2_SNORM
:
221 case PIPE_FORMAT_LATC2_SNORM
:
222 result
|= sign_bit
[1] | sign_bit
[0];
223 case PIPE_FORMAT_RGTC2_UNORM
:
224 case PIPE_FORMAT_LATC2_UNORM
:
225 return R400_TX_FORMAT_ATI2N
| result
;
228 return ~0; /* Unsupported/unknown. */
232 /* This is truly a special format.
233 * It stores R8G8 and B is computed using sqrt(1 - R^2 - G^2)
234 * in the sampler unit. Also known as D3DFMT_CxV8U8. */
235 if (format
== PIPE_FORMAT_R8G8Bx_SNORM
) {
236 return R300_TX_FORMAT_CxV8U8
| result
;
239 /* Integer and fixed-point 16.16 textures are not supported. */
240 for (i
= 0; i
< 4; i
++) {
241 if (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_FIXED
||
242 ((desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_SIGNED
||
243 desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_UNSIGNED
) &&
244 (!desc
->channel
[i
].normalized
||
245 desc
->channel
[i
].pure_integer
))) {
246 return ~0; /* Unsupported/unknown. */
251 for (i
= 0; i
< desc
->nr_channels
; i
++) {
252 if (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_SIGNED
) {
253 result
|= sign_bit
[i
];
257 /* See whether the components are of the same size. */
258 for (i
= 1; i
< desc
->nr_channels
; i
++) {
259 uniform
= uniform
&& desc
->channel
[0].size
== desc
->channel
[i
].size
;
262 /* Non-uniform formats. */
264 switch (desc
->nr_channels
) {
266 if (desc
->channel
[0].size
== 5 &&
267 desc
->channel
[1].size
== 6 &&
268 desc
->channel
[2].size
== 5) {
269 return R300_TX_FORMAT_Z5Y6X5
| result
;
271 if (desc
->channel
[0].size
== 5 &&
272 desc
->channel
[1].size
== 5 &&
273 desc
->channel
[2].size
== 6) {
274 return R300_TX_FORMAT_Z6Y5X5
| result
;
276 if (desc
->channel
[0].size
== 2 &&
277 desc
->channel
[1].size
== 3 &&
278 desc
->channel
[2].size
== 3) {
279 return R300_TX_FORMAT_Z3Y3X2
| result
;
281 return ~0; /* Unsupported/unknown. */
284 if (desc
->channel
[0].size
== 5 &&
285 desc
->channel
[1].size
== 5 &&
286 desc
->channel
[2].size
== 5 &&
287 desc
->channel
[3].size
== 1) {
288 return R300_TX_FORMAT_W1Z5Y5X5
| result
;
290 if (desc
->channel
[0].size
== 10 &&
291 desc
->channel
[1].size
== 10 &&
292 desc
->channel
[2].size
== 10 &&
293 desc
->channel
[3].size
== 2) {
294 return R300_TX_FORMAT_W2Z10Y10X10
| result
;
297 return ~0; /* Unsupported/unknown. */
300 /* Find the first non-VOID channel. */
301 for (i
= 0; i
< 4; i
++) {
302 if (desc
->channel
[i
].type
!= UTIL_FORMAT_TYPE_VOID
) {
308 return ~0; /* Unsupported/unknown. */
310 /* And finally, uniform formats. */
311 switch (desc
->channel
[i
].type
) {
312 case UTIL_FORMAT_TYPE_UNSIGNED
:
313 case UTIL_FORMAT_TYPE_SIGNED
:
314 if (!desc
->channel
[i
].normalized
&&
315 desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_SRGB
) {
319 switch (desc
->channel
[i
].size
) {
321 switch (desc
->nr_channels
) {
323 return R300_TX_FORMAT_Y4X4
| result
;
325 return R300_TX_FORMAT_W4Z4Y4X4
| result
;
330 switch (desc
->nr_channels
) {
332 return R300_TX_FORMAT_X8
| result
;
334 return R300_TX_FORMAT_Y8X8
| result
;
336 return R300_TX_FORMAT_W8Z8Y8X8
| result
;
341 switch (desc
->nr_channels
) {
343 return R300_TX_FORMAT_X16
| result
;
345 return R300_TX_FORMAT_Y16X16
| result
;
347 return R300_TX_FORMAT_W16Z16Y16X16
| result
;
352 case UTIL_FORMAT_TYPE_FLOAT
:
353 switch (desc
->channel
[i
].size
) {
355 switch (desc
->nr_channels
) {
357 return R300_TX_FORMAT_16F
| result
;
359 return R300_TX_FORMAT_16F_16F
| result
;
361 return R300_TX_FORMAT_16F_16F_16F_16F
| result
;
366 switch (desc
->nr_channels
) {
368 return R300_TX_FORMAT_32F
| result
;
370 return R300_TX_FORMAT_32F_32F
| result
;
372 return R300_TX_FORMAT_32F_32F_32F_32F
| result
;
377 return ~0; /* Unsupported/unknown. */
380 uint32_t r500_tx_format_msb_bit(enum pipe_format format
)
383 case PIPE_FORMAT_RGTC1_UNORM
:
384 case PIPE_FORMAT_RGTC1_SNORM
:
385 case PIPE_FORMAT_LATC1_UNORM
:
386 case PIPE_FORMAT_LATC1_SNORM
:
387 case PIPE_FORMAT_X8Z24_UNORM
:
388 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
389 return R500_TXFORMAT_MSB
;
395 /* Buffer formats. */
397 /* Colorbuffer formats. This is the unswizzled format of the RB3D block's
398 * output. For the swizzling of the targets, check the shader's format. */
399 static uint32_t r300_translate_colorformat(enum pipe_format format
)
403 case PIPE_FORMAT_A8_UNORM
:
404 case PIPE_FORMAT_A8_SNORM
:
405 case PIPE_FORMAT_I8_UNORM
:
406 case PIPE_FORMAT_I8_SNORM
:
407 case PIPE_FORMAT_L8_UNORM
:
408 case PIPE_FORMAT_L8_SNORM
:
409 case PIPE_FORMAT_R8_UNORM
:
410 case PIPE_FORMAT_R8_SNORM
:
411 return R300_COLOR_FORMAT_I8
;
413 /* 16-bit buffers. */
414 case PIPE_FORMAT_L8A8_UNORM
:
415 case PIPE_FORMAT_L8A8_SNORM
:
416 case PIPE_FORMAT_R8G8_UNORM
:
417 case PIPE_FORMAT_R8G8_SNORM
:
418 /* These formats work fine with UV88 if US_OUT_FMT is set correctly. */
419 case PIPE_FORMAT_A16_UNORM
:
420 case PIPE_FORMAT_A16_SNORM
:
421 case PIPE_FORMAT_A16_FLOAT
:
422 case PIPE_FORMAT_L16_UNORM
:
423 case PIPE_FORMAT_L16_SNORM
:
424 case PIPE_FORMAT_L16_FLOAT
:
425 case PIPE_FORMAT_I16_UNORM
:
426 case PIPE_FORMAT_I16_SNORM
:
427 case PIPE_FORMAT_I16_FLOAT
:
428 case PIPE_FORMAT_R16_UNORM
:
429 case PIPE_FORMAT_R16_SNORM
:
430 case PIPE_FORMAT_R16_FLOAT
:
431 return R300_COLOR_FORMAT_UV88
;
433 case PIPE_FORMAT_B5G6R5_UNORM
:
434 return R300_COLOR_FORMAT_RGB565
;
436 case PIPE_FORMAT_B5G5R5A1_UNORM
:
437 case PIPE_FORMAT_B5G5R5X1_UNORM
:
438 return R300_COLOR_FORMAT_ARGB1555
;
440 case PIPE_FORMAT_B4G4R4A4_UNORM
:
441 case PIPE_FORMAT_B4G4R4X4_UNORM
:
442 return R300_COLOR_FORMAT_ARGB4444
;
444 /* 32-bit buffers. */
445 case PIPE_FORMAT_B8G8R8A8_UNORM
:
446 /*case PIPE_FORMAT_B8G8R8A8_SNORM:*/
447 case PIPE_FORMAT_B8G8R8X8_UNORM
:
448 /*case PIPE_FORMAT_B8G8R8X8_SNORM:*/
449 case PIPE_FORMAT_R8G8B8A8_UNORM
:
450 case PIPE_FORMAT_R8G8B8A8_SNORM
:
451 case PIPE_FORMAT_R8G8B8X8_UNORM
:
452 case PIPE_FORMAT_R8G8B8X8_SNORM
:
453 /* These formats work fine with ARGB8888 if US_OUT_FMT is set
455 case PIPE_FORMAT_R16G16_UNORM
:
456 case PIPE_FORMAT_R16G16_SNORM
:
457 case PIPE_FORMAT_R16G16_FLOAT
:
458 case PIPE_FORMAT_L16A16_UNORM
:
459 case PIPE_FORMAT_L16A16_SNORM
:
460 case PIPE_FORMAT_L16A16_FLOAT
:
461 case PIPE_FORMAT_A32_FLOAT
:
462 case PIPE_FORMAT_L32_FLOAT
:
463 case PIPE_FORMAT_I32_FLOAT
:
464 case PIPE_FORMAT_R32_FLOAT
:
465 return R300_COLOR_FORMAT_ARGB8888
;
467 case PIPE_FORMAT_R10G10B10A2_UNORM
:
468 case PIPE_FORMAT_R10G10B10X2_SNORM
:
469 case PIPE_FORMAT_B10G10R10A2_UNORM
:
470 case PIPE_FORMAT_B10G10R10X2_UNORM
:
471 return R500_COLOR_FORMAT_ARGB2101010
; /* R5xx-only? */
473 /* 64-bit buffers. */
474 case PIPE_FORMAT_R16G16B16A16_UNORM
:
475 case PIPE_FORMAT_R16G16B16A16_SNORM
:
476 case PIPE_FORMAT_R16G16B16A16_FLOAT
:
477 case PIPE_FORMAT_R16G16B16X16_UNORM
:
478 case PIPE_FORMAT_R16G16B16X16_SNORM
:
479 case PIPE_FORMAT_R16G16B16X16_FLOAT
:
480 /* These formats work fine with ARGB16161616 if US_OUT_FMT is set
482 case PIPE_FORMAT_R32G32_FLOAT
:
483 case PIPE_FORMAT_L32A32_FLOAT
:
484 return R300_COLOR_FORMAT_ARGB16161616
;
486 /* 128-bit buffers. */
487 case PIPE_FORMAT_R32G32B32A32_FLOAT
:
488 case PIPE_FORMAT_R32G32B32X32_FLOAT
:
489 return R300_COLOR_FORMAT_ARGB32323232
;
492 case PIPE_FORMAT_UYVY
:
493 return R300_COLOR_FORMAT_YVYU
;
494 case PIPE_FORMAT_YUYV
:
495 return R300_COLOR_FORMAT_VYUY
;
497 return ~0; /* Unsupported. */
501 /* Depthbuffer and stencilbuffer. Thankfully, we only support two flavors. */
502 static uint32_t r300_translate_zsformat(enum pipe_format format
)
505 /* 16-bit depth, no stencil */
506 case PIPE_FORMAT_Z16_UNORM
:
507 return R300_DEPTHFORMAT_16BIT_INT_Z
;
508 /* 24-bit depth, ignored stencil */
509 case PIPE_FORMAT_X8Z24_UNORM
:
510 /* 24-bit depth, 8-bit stencil */
511 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
512 return R300_DEPTHFORMAT_24BIT_INT_Z_8BIT_STENCIL
;
514 return ~0; /* Unsupported. */
518 /* Shader output formats. This is essentially the swizzle from the shader
521 * Note that formats are stored from C3 to C0. */
522 static uint32_t r300_translate_out_fmt(enum pipe_format format
)
524 uint32_t modifier
= 0;
526 const struct util_format_description
*desc
;
527 boolean uniform_sign
;
529 desc
= util_format_description(format
);
531 /* Find the first non-VOID channel. */
532 for (i
= 0; i
< 4; i
++) {
533 if (desc
->channel
[i
].type
!= UTIL_FORMAT_TYPE_VOID
) {
539 return ~0; /* Unsupported/unknown. */
541 /* Specifies how the shader output is written to the fog unit. */
542 switch (desc
->channel
[i
].type
) {
543 case UTIL_FORMAT_TYPE_FLOAT
:
544 switch (desc
->channel
[i
].size
) {
546 switch (desc
->nr_channels
) {
548 modifier
|= R300_US_OUT_FMT_C_32_FP
;
551 modifier
|= R300_US_OUT_FMT_C2_32_FP
;
554 modifier
|= R300_US_OUT_FMT_C4_32_FP
;
560 switch (desc
->nr_channels
) {
562 modifier
|= R300_US_OUT_FMT_C_16_FP
;
565 modifier
|= R300_US_OUT_FMT_C2_16_FP
;
568 modifier
|= R300_US_OUT_FMT_C4_16_FP
;
576 switch (desc
->channel
[i
].size
) {
578 switch (desc
->nr_channels
) {
580 modifier
|= R300_US_OUT_FMT_C_16
;
583 modifier
|= R300_US_OUT_FMT_C2_16
;
586 modifier
|= R300_US_OUT_FMT_C4_16
;
592 modifier
|= R300_US_OUT_FMT_C4_10
;
596 /* C4_8 seems to be used for the formats whose pixel size
598 modifier
|= R300_US_OUT_FMT_C4_8
;
605 for (i
= 0; i
< desc
->nr_channels
; i
++)
606 if (desc
->channel
[i
].type
!= UTIL_FORMAT_TYPE_SIGNED
)
607 uniform_sign
= FALSE
;
610 modifier
|= R300_OUT_SIGN(0xf);
612 /* Add swizzles and return. */
614 /*** Special cases (non-standard channel mapping) ***/
617 * COLORFORMAT_I8 stores the Z component (C2). */
618 case PIPE_FORMAT_A8_UNORM
:
619 case PIPE_FORMAT_A8_SNORM
:
620 return modifier
| R300_C2_SEL_A
;
621 case PIPE_FORMAT_I8_UNORM
:
622 case PIPE_FORMAT_I8_SNORM
:
623 case PIPE_FORMAT_L8_UNORM
:
624 case PIPE_FORMAT_L8_SNORM
:
625 case PIPE_FORMAT_R8_UNORM
:
626 case PIPE_FORMAT_R8_SNORM
:
627 return modifier
| R300_C2_SEL_R
;
630 * COLORFORMAT_UV88 stores ZX (C2 and C0). */
631 case PIPE_FORMAT_L8A8_SNORM
:
632 case PIPE_FORMAT_L8A8_UNORM
:
633 return modifier
| R300_C0_SEL_A
| R300_C2_SEL_R
;
634 case PIPE_FORMAT_R8G8_SNORM
:
635 case PIPE_FORMAT_R8G8_UNORM
:
636 return modifier
| R300_C0_SEL_G
| R300_C2_SEL_R
;
639 * ARGB16161616 stores XZ for RG32F */
640 case PIPE_FORMAT_R32G32_FLOAT
:
641 return modifier
| R300_C0_SEL_R
| R300_C2_SEL_G
;
643 /*** Generic cases (standard channel mapping) ***/
646 case PIPE_FORMAT_B5G6R5_UNORM
:
647 case PIPE_FORMAT_B5G5R5A1_UNORM
:
648 case PIPE_FORMAT_B5G5R5X1_UNORM
:
649 case PIPE_FORMAT_B4G4R4A4_UNORM
:
650 case PIPE_FORMAT_B4G4R4X4_UNORM
:
651 case PIPE_FORMAT_B8G8R8A8_UNORM
:
652 /*case PIPE_FORMAT_B8G8R8A8_SNORM:*/
653 case PIPE_FORMAT_B8G8R8X8_UNORM
:
654 /*case PIPE_FORMAT_B8G8R8X8_SNORM:*/
655 case PIPE_FORMAT_B10G10R10A2_UNORM
:
656 case PIPE_FORMAT_B10G10R10X2_UNORM
:
658 R300_C0_SEL_B
| R300_C1_SEL_G
|
659 R300_C2_SEL_R
| R300_C3_SEL_A
;
662 case PIPE_FORMAT_A16_UNORM
:
663 case PIPE_FORMAT_A16_SNORM
:
664 case PIPE_FORMAT_A16_FLOAT
:
665 case PIPE_FORMAT_A32_FLOAT
:
667 R300_C0_SEL_A
| R300_C1_SEL_R
|
668 R300_C2_SEL_G
| R300_C3_SEL_B
;
671 case PIPE_FORMAT_R8G8B8X8_UNORM
:
672 case PIPE_FORMAT_R8G8B8X8_SNORM
:
673 case PIPE_FORMAT_R8G8B8A8_UNORM
:
674 case PIPE_FORMAT_R8G8B8A8_SNORM
:
675 case PIPE_FORMAT_R10G10B10A2_UNORM
:
676 case PIPE_FORMAT_R10G10B10X2_SNORM
:
677 case PIPE_FORMAT_R16_UNORM
:
678 case PIPE_FORMAT_R16G16_UNORM
:
679 case PIPE_FORMAT_R16G16B16A16_UNORM
:
680 case PIPE_FORMAT_R16_SNORM
:
681 case PIPE_FORMAT_R16G16_SNORM
:
682 case PIPE_FORMAT_R16G16B16A16_SNORM
:
683 case PIPE_FORMAT_R16_FLOAT
:
684 case PIPE_FORMAT_R16G16_FLOAT
:
685 case PIPE_FORMAT_R16G16B16A16_FLOAT
:
686 case PIPE_FORMAT_R32_FLOAT
:
687 case PIPE_FORMAT_R32G32B32A32_FLOAT
:
688 case PIPE_FORMAT_R32G32B32X32_FLOAT
:
689 case PIPE_FORMAT_L16_UNORM
:
690 case PIPE_FORMAT_L16_SNORM
:
691 case PIPE_FORMAT_L16_FLOAT
:
692 case PIPE_FORMAT_L32_FLOAT
:
693 case PIPE_FORMAT_I16_UNORM
:
694 case PIPE_FORMAT_I16_SNORM
:
695 case PIPE_FORMAT_I16_FLOAT
:
696 case PIPE_FORMAT_I32_FLOAT
:
697 case PIPE_FORMAT_R16G16B16X16_UNORM
:
698 case PIPE_FORMAT_R16G16B16X16_SNORM
:
699 case PIPE_FORMAT_R16G16B16X16_FLOAT
:
701 R300_C0_SEL_R
| R300_C1_SEL_G
|
702 R300_C2_SEL_B
| R300_C3_SEL_A
;
705 case PIPE_FORMAT_L16A16_UNORM
:
706 case PIPE_FORMAT_L16A16_SNORM
:
707 case PIPE_FORMAT_L16A16_FLOAT
:
708 case PIPE_FORMAT_L32A32_FLOAT
:
710 R300_C0_SEL_R
| R300_C1_SEL_A
;
713 return ~0; /* Unsupported. */
717 static uint32_t r300_translate_colormask_swizzle(enum pipe_format format
)
720 case PIPE_FORMAT_A8_UNORM
:
721 case PIPE_FORMAT_A8_SNORM
:
722 case PIPE_FORMAT_A16_UNORM
:
723 case PIPE_FORMAT_A16_SNORM
:
724 case PIPE_FORMAT_A16_FLOAT
:
725 case PIPE_FORMAT_A32_FLOAT
:
726 return COLORMASK_AAAA
;
728 case PIPE_FORMAT_I8_UNORM
:
729 case PIPE_FORMAT_I8_SNORM
:
730 case PIPE_FORMAT_L8_UNORM
:
731 case PIPE_FORMAT_L8_SNORM
:
732 case PIPE_FORMAT_R8_UNORM
:
733 case PIPE_FORMAT_R8_SNORM
:
734 case PIPE_FORMAT_R32_FLOAT
:
735 case PIPE_FORMAT_L32_FLOAT
:
736 case PIPE_FORMAT_I32_FLOAT
:
737 return COLORMASK_RRRR
;
739 case PIPE_FORMAT_L8A8_SNORM
:
740 case PIPE_FORMAT_L8A8_UNORM
:
741 case PIPE_FORMAT_L16A16_UNORM
:
742 case PIPE_FORMAT_L16A16_SNORM
:
743 case PIPE_FORMAT_L16A16_FLOAT
:
744 case PIPE_FORMAT_L32A32_FLOAT
:
745 return COLORMASK_ARRA
;
747 case PIPE_FORMAT_R8G8_SNORM
:
748 case PIPE_FORMAT_R8G8_UNORM
:
749 case PIPE_FORMAT_R16G16_UNORM
:
750 case PIPE_FORMAT_R16G16_SNORM
:
751 case PIPE_FORMAT_R16G16_FLOAT
:
752 case PIPE_FORMAT_R32G32_FLOAT
:
753 return COLORMASK_GRRG
;
755 case PIPE_FORMAT_B5G5R5X1_UNORM
:
756 case PIPE_FORMAT_B4G4R4X4_UNORM
:
757 case PIPE_FORMAT_B8G8R8X8_UNORM
:
758 /*case PIPE_FORMAT_B8G8R8X8_SNORM:*/
759 case PIPE_FORMAT_B10G10R10X2_UNORM
:
760 return COLORMASK_BGRX
;
762 case PIPE_FORMAT_B5G6R5_UNORM
:
763 case PIPE_FORMAT_B5G5R5A1_UNORM
:
764 case PIPE_FORMAT_B4G4R4A4_UNORM
:
765 case PIPE_FORMAT_B8G8R8A8_UNORM
:
766 /*case PIPE_FORMAT_B8G8R8A8_SNORM:*/
767 case PIPE_FORMAT_B10G10R10A2_UNORM
:
768 return COLORMASK_BGRA
;
770 case PIPE_FORMAT_R8G8B8X8_UNORM
:
771 /* RGBX_SNORM formats are broken for an unknown reason */
772 /*case PIPE_FORMAT_R8G8B8X8_SNORM:*/
773 /*case PIPE_FORMAT_R10G10B10X2_SNORM:*/
774 case PIPE_FORMAT_R16G16B16X16_UNORM
:
775 /*case PIPE_FORMAT_R16G16B16X16_SNORM:*/
776 case PIPE_FORMAT_R16G16B16X16_FLOAT
:
777 case PIPE_FORMAT_R32G32B32X32_FLOAT
:
778 return COLORMASK_RGBX
;
780 case PIPE_FORMAT_R8G8B8A8_UNORM
:
781 case PIPE_FORMAT_R8G8B8A8_SNORM
:
782 case PIPE_FORMAT_R10G10B10A2_UNORM
:
783 case PIPE_FORMAT_R16_UNORM
:
784 case PIPE_FORMAT_R16G16B16A16_UNORM
:
785 case PIPE_FORMAT_R16_SNORM
:
786 case PIPE_FORMAT_R16G16B16A16_SNORM
:
787 case PIPE_FORMAT_R16_FLOAT
:
788 case PIPE_FORMAT_R16G16B16A16_FLOAT
:
789 case PIPE_FORMAT_R32G32B32A32_FLOAT
:
790 case PIPE_FORMAT_L16_UNORM
:
791 case PIPE_FORMAT_L16_SNORM
:
792 case PIPE_FORMAT_L16_FLOAT
:
793 case PIPE_FORMAT_I16_UNORM
:
794 case PIPE_FORMAT_I16_SNORM
:
795 case PIPE_FORMAT_I16_FLOAT
:
796 return COLORMASK_RGBA
;
799 return ~0; /* Unsupported. */
803 boolean
r300_is_colorbuffer_format_supported(enum pipe_format format
)
805 return r300_translate_colorformat(format
) != ~0 &&
806 r300_translate_out_fmt(format
) != ~0 &&
807 r300_translate_colormask_swizzle(format
) != ~0;
810 boolean
r300_is_zs_format_supported(enum pipe_format format
)
812 return r300_translate_zsformat(format
) != ~0;
815 boolean
r300_is_sampler_format_supported(enum pipe_format format
)
817 return r300_translate_texformat(format
, 0, TRUE
, FALSE
) != ~0;
820 void r300_texture_setup_format_state(struct r300_screen
*screen
,
821 struct r300_resource
*tex
,
822 enum pipe_format format
,
824 unsigned width0_override
,
825 unsigned height0_override
,
826 struct r300_texture_format_state
*out
)
828 struct pipe_resource
*pt
= &tex
->b
.b
;
829 struct r300_texture_desc
*desc
= &tex
->tex
;
830 boolean is_r500
= screen
->caps
.is_r500
;
831 unsigned width
, height
, depth
;
832 unsigned txwidth
, txheight
, txdepth
;
834 width
= u_minify(width0_override
, level
);
835 height
= u_minify(height0_override
, level
);
836 depth
= u_minify(desc
->depth0
, level
);
838 txwidth
= (width
- 1) & 0x7ff;
839 txheight
= (height
- 1) & 0x7ff;
840 txdepth
= util_logbase2(depth
) & 0xf;
842 /* Mask out all the fields we change. */
844 out
->format1
&= ~R300_TX_FORMAT_TEX_COORD_TYPE_MASK
;
845 out
->format2
&= R500_TXFORMAT_MSB
;
846 out
->tile_config
= 0;
848 /* Set sampler state. */
850 R300_TX_WIDTH(txwidth
) |
851 R300_TX_HEIGHT(txheight
) |
852 R300_TX_DEPTH(txdepth
);
854 if (desc
->uses_stride_addressing
) {
856 r300_stride_to_width(format
, desc
->stride_in_bytes
[level
]);
857 /* rectangles love this */
858 out
->format0
|= R300_TX_PITCH_EN
;
859 out
->format2
= (stride
- 1) & 0x1fff;
862 if (pt
->target
== PIPE_TEXTURE_CUBE
) {
863 out
->format1
|= R300_TX_FORMAT_CUBIC_MAP
;
865 if (pt
->target
== PIPE_TEXTURE_3D
) {
866 out
->format1
|= R300_TX_FORMAT_3D
;
869 /* large textures on r500 */
872 unsigned us_width
= txwidth
;
873 unsigned us_height
= txheight
;
874 unsigned us_depth
= txdepth
;
877 out
->format2
|= R500_TXWIDTH_BIT11
;
880 out
->format2
|= R500_TXHEIGHT_BIT11
;
883 /* The US_FORMAT register fixes an R500 TX addressing bug.
884 * Don't ask why it must be set like this. I don't know it either. */
886 us_width
= (0x000007FF + us_width
) >> 1;
887 us_depth
|= 0x0000000D;
890 us_height
= (0x000007FF + us_height
) >> 1;
891 us_depth
|= 0x0000000E;
895 R300_TX_WIDTH(us_width
) |
896 R300_TX_HEIGHT(us_height
) |
897 R300_TX_DEPTH(us_depth
);
900 out
->tile_config
= R300_TXO_MACRO_TILE(desc
->macrotile
[level
]) |
901 R300_TXO_MICRO_TILE(desc
->microtile
);
904 static void r300_texture_setup_fb_state(struct r300_surface
*surf
)
906 struct r300_resource
*tex
= r300_resource(surf
->base
.texture
);
907 unsigned level
= surf
->base
.u
.tex
.level
;
909 r300_stride_to_width(surf
->base
.format
, tex
->tex
.stride_in_bytes
[level
]);
911 /* Set framebuffer state. */
912 if (util_format_is_depth_or_stencil(surf
->base
.format
)) {
915 R300_DEPTHMACROTILE(tex
->tex
.macrotile
[level
]) |
916 R300_DEPTHMICROTILE(tex
->tex
.microtile
);
917 surf
->format
= r300_translate_zsformat(surf
->base
.format
);
918 surf
->pitch_zmask
= tex
->tex
.zmask_stride_in_pixels
[level
];
919 surf
->pitch_hiz
= tex
->tex
.hiz_stride_in_pixels
[level
];
923 r300_translate_colorformat(surf
->base
.format
) |
924 R300_COLOR_TILE(tex
->tex
.macrotile
[level
]) |
925 R300_COLOR_MICROTILE(tex
->tex
.microtile
);
926 surf
->format
= r300_translate_out_fmt(surf
->base
.format
);
927 surf
->colormask_swizzle
=
928 r300_translate_colormask_swizzle(surf
->base
.format
);
929 surf
->pitch_cmask
= tex
->tex
.cmask_stride_in_pixels
;
933 static void r300_texture_destroy(struct pipe_screen
*screen
,
934 struct pipe_resource
* texture
)
936 struct r300_screen
*rscreen
= r300_screen(screen
);
937 struct r300_resource
* tex
= (struct r300_resource
*)texture
;
939 if (tex
->tex
.cmask_dwords
) {
940 pipe_mutex_lock(rscreen
->cmask_mutex
);
941 if (texture
== rscreen
->cmask_resource
) {
942 rscreen
->cmask_resource
= NULL
;
944 pipe_mutex_unlock(rscreen
->cmask_mutex
);
946 pb_reference(&tex
->buf
, NULL
);
950 boolean
r300_resource_get_handle(struct pipe_screen
* screen
,
951 struct pipe_resource
*texture
,
952 struct winsys_handle
*whandle
)
954 struct radeon_winsys
*rws
= r300_screen(screen
)->rws
;
955 struct r300_resource
* tex
= (struct r300_resource
*)texture
;
961 return rws
->buffer_get_handle(tex
->buf
,
962 tex
->tex
.stride_in_bytes
[0], whandle
);
965 static const struct u_resource_vtbl r300_texture_vtbl
=
967 NULL
, /* get_handle */
968 r300_texture_destroy
, /* resource_destroy */
969 r300_texture_transfer_map
, /* transfer_map */
970 NULL
, /* transfer_flush_region */
971 r300_texture_transfer_unmap
, /* transfer_unmap */
972 NULL
/* transfer_inline_write */
975 /* The common texture constructor. */
976 static struct r300_resource
*
977 r300_texture_create_object(struct r300_screen
*rscreen
,
978 const struct pipe_resource
*base
,
979 enum radeon_bo_layout microtile
,
980 enum radeon_bo_layout macrotile
,
981 unsigned stride_in_bytes_override
,
982 struct pb_buffer
*buffer
)
984 struct radeon_winsys
*rws
= rscreen
->rws
;
985 struct r300_resource
*tex
= NULL
;
987 tex
= CALLOC_STRUCT(r300_resource
);
992 pipe_reference_init(&tex
->b
.b
.reference
, 1);
993 tex
->b
.b
.screen
= &rscreen
->screen
;
994 tex
->b
.b
.usage
= base
->usage
;
995 tex
->b
.b
.bind
= base
->bind
;
996 tex
->b
.b
.flags
= base
->flags
;
997 tex
->b
.vtbl
= &r300_texture_vtbl
;
998 tex
->tex
.microtile
= microtile
;
999 tex
->tex
.macrotile
[0] = macrotile
;
1000 tex
->tex
.stride_in_bytes_override
= stride_in_bytes_override
;
1001 tex
->domain
= base
->flags
& R300_RESOURCE_FLAG_TRANSFER
? RADEON_DOMAIN_GTT
:
1002 base
->nr_samples
> 1 ? RADEON_DOMAIN_VRAM
:
1003 RADEON_DOMAIN_VRAM
| RADEON_DOMAIN_GTT
;
1006 r300_texture_desc_init(rscreen
, tex
, base
);
1008 /* Figure out the ideal placement for the texture.. */
1009 if (tex
->domain
& RADEON_DOMAIN_VRAM
&&
1010 tex
->tex
.size_in_bytes
>= rscreen
->info
.vram_size
) {
1011 tex
->domain
&= ~RADEON_DOMAIN_VRAM
;
1012 tex
->domain
|= RADEON_DOMAIN_GTT
;
1014 if (tex
->domain
& RADEON_DOMAIN_GTT
&&
1015 tex
->tex
.size_in_bytes
>= rscreen
->info
.gart_size
) {
1016 tex
->domain
&= ~RADEON_DOMAIN_GTT
;
1018 /* Just fail if the texture is too large. */
1023 /* Create the backing buffer if needed. */
1025 tex
->buf
= rws
->buffer_create(rws
, tex
->tex
.size_in_bytes
, 2048, TRUE
,
1033 if (SCREEN_DBG_ON(rscreen
, DBG_MSAA
) && base
->nr_samples
> 1) {
1034 fprintf(stderr
, "r300: %ix MSAA %s buffer created\n",
1036 util_format_is_depth_or_stencil(base
->format
) ? "depth" : "color");
1039 tex
->cs_buf
= rws
->buffer_get_cs_handle(tex
->buf
);
1041 rws
->buffer_set_tiling(tex
->buf
, NULL
,
1042 tex
->tex
.microtile
, tex
->tex
.macrotile
[0],
1044 tex
->tex
.stride_in_bytes
[0]);
1051 pb_reference(&buffer
, NULL
);
1055 /* Create a new texture. */
1056 struct pipe_resource
*r300_texture_create(struct pipe_screen
*screen
,
1057 const struct pipe_resource
*base
)
1059 struct r300_screen
*rscreen
= r300_screen(screen
);
1060 enum radeon_bo_layout microtile
, macrotile
;
1062 if ((base
->flags
& R300_RESOURCE_FLAG_TRANSFER
) ||
1063 (base
->bind
& PIPE_BIND_SCANOUT
)) {
1064 microtile
= RADEON_LAYOUT_LINEAR
;
1065 macrotile
= RADEON_LAYOUT_LINEAR
;
1067 /* This will make the texture_create_function select the layout. */
1068 microtile
= RADEON_LAYOUT_UNKNOWN
;
1069 macrotile
= RADEON_LAYOUT_UNKNOWN
;
1072 return (struct pipe_resource
*)
1073 r300_texture_create_object(rscreen
, base
, microtile
, macrotile
,
1077 struct pipe_resource
*r300_texture_from_handle(struct pipe_screen
*screen
,
1078 const struct pipe_resource
*base
,
1079 struct winsys_handle
*whandle
)
1081 struct r300_screen
*rscreen
= r300_screen(screen
);
1082 struct radeon_winsys
*rws
= rscreen
->rws
;
1083 struct pb_buffer
*buffer
;
1084 enum radeon_bo_layout microtile
, macrotile
;
1087 /* Support only 2D textures without mipmaps */
1088 if ((base
->target
!= PIPE_TEXTURE_2D
&&
1089 base
->target
!= PIPE_TEXTURE_RECT
) ||
1090 base
->depth0
!= 1 ||
1091 base
->last_level
!= 0) {
1095 buffer
= rws
->buffer_from_handle(rws
, whandle
, &stride
);
1099 rws
->buffer_get_tiling(buffer
, µtile
, ¯otile
, NULL
, NULL
, NULL
, NULL
, NULL
);
1101 /* Enforce a microtiled zbuffer. */
1102 if (util_format_is_depth_or_stencil(base
->format
) &&
1103 microtile
== RADEON_LAYOUT_LINEAR
) {
1104 switch (util_format_get_blocksize(base
->format
)) {
1106 microtile
= RADEON_LAYOUT_TILED
;
1110 microtile
= RADEON_LAYOUT_SQUARETILED
;
1115 return (struct pipe_resource
*)
1116 r300_texture_create_object(rscreen
, base
, microtile
, macrotile
,
1120 /* Not required to implement u_resource_vtbl, consider moving to another file:
1122 struct pipe_surface
* r300_create_surface_custom(struct pipe_context
* ctx
,
1123 struct pipe_resource
* texture
,
1124 const struct pipe_surface
*surf_tmpl
,
1125 unsigned width0_override
,
1126 unsigned height0_override
)
1128 struct r300_resource
* tex
= r300_resource(texture
);
1129 struct r300_surface
* surface
= CALLOC_STRUCT(r300_surface
);
1130 unsigned level
= surf_tmpl
->u
.tex
.level
;
1132 assert(surf_tmpl
->u
.tex
.first_layer
== surf_tmpl
->u
.tex
.last_layer
);
1135 uint32_t offset
, tile_height
;
1137 pipe_reference_init(&surface
->base
.reference
, 1);
1138 pipe_resource_reference(&surface
->base
.texture
, texture
);
1139 surface
->base
.context
= ctx
;
1140 surface
->base
.format
= surf_tmpl
->format
;
1141 surface
->base
.width
= u_minify(width0_override
, level
);
1142 surface
->base
.height
= u_minify(height0_override
, level
);
1143 surface
->base
.u
.tex
.level
= level
;
1144 surface
->base
.u
.tex
.first_layer
= surf_tmpl
->u
.tex
.first_layer
;
1145 surface
->base
.u
.tex
.last_layer
= surf_tmpl
->u
.tex
.last_layer
;
1147 surface
->buf
= tex
->buf
;
1148 surface
->cs_buf
= tex
->cs_buf
;
1150 /* Prefer VRAM if there are multiple domains to choose from. */
1151 surface
->domain
= tex
->domain
;
1152 if (surface
->domain
& RADEON_DOMAIN_VRAM
)
1153 surface
->domain
&= ~RADEON_DOMAIN_GTT
;
1155 surface
->offset
= r300_texture_get_offset(tex
, level
,
1156 surf_tmpl
->u
.tex
.first_layer
);
1157 r300_texture_setup_fb_state(surface
);
1159 /* Parameters for the CBZB clear. */
1160 surface
->cbzb_allowed
= tex
->tex
.cbzb_allowed
[level
];
1161 surface
->cbzb_width
= align(surface
->base
.width
, 64);
1163 /* Height must be aligned to the size of a tile. */
1164 tile_height
= r300_get_pixel_alignment(surface
->base
.format
,
1165 tex
->b
.b
.nr_samples
,
1167 tex
->tex
.macrotile
[level
],
1170 surface
->cbzb_height
= align((surface
->base
.height
+ 1) / 2,
1173 /* Offset must be aligned to 2K and must point at the beginning
1175 offset
= surface
->offset
+
1176 tex
->tex
.stride_in_bytes
[level
] * surface
->cbzb_height
;
1177 surface
->cbzb_midpoint_offset
= offset
& ~2047;
1179 surface
->cbzb_pitch
= surface
->pitch
& 0x1ffffc;
1181 if (util_format_get_blocksizebits(surface
->base
.format
) == 32)
1182 surface
->cbzb_format
= R300_DEPTHFORMAT_24BIT_INT_Z_8BIT_STENCIL
;
1184 surface
->cbzb_format
= R300_DEPTHFORMAT_16BIT_INT_Z
;
1186 DBG(r300_context(ctx
), DBG_CBZB
,
1187 "CBZB Allowed: %s, Dim: %ix%i, Misalignment: %i, Micro: %s, Macro: %s\n",
1188 surface
->cbzb_allowed
? "YES" : " NO",
1189 surface
->cbzb_width
, surface
->cbzb_height
,
1191 tex
->tex
.microtile
? "YES" : " NO",
1192 tex
->tex
.macrotile
[level
] ? "YES" : " NO");
1195 return &surface
->base
;
1198 struct pipe_surface
* r300_create_surface(struct pipe_context
* ctx
,
1199 struct pipe_resource
* texture
,
1200 const struct pipe_surface
*surf_tmpl
)
1202 return r300_create_surface_custom(ctx
, texture
, surf_tmpl
,
1207 /* Not required to implement u_resource_vtbl, consider moving to another file:
1209 void r300_surface_destroy(struct pipe_context
*ctx
, struct pipe_surface
* s
)
1211 pipe_resource_reference(&s
->texture
, NULL
);