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"
32 #include "r300_winsys.h"
34 #include "util/u_format.h"
35 #include "util/u_format_s3tc.h"
36 #include "util/u_math.h"
37 #include "util/u_memory.h"
38 #include "util/u_mm.h"
40 #include "pipe/p_screen.h"
42 void util_format_combine_swizzles(unsigned char *dst
,
43 const unsigned char *swz1
,
44 const unsigned char *swz2
)
48 for (i
= 0; i
< 4; i
++) {
49 dst
[i
] = swz2
[i
] <= UTIL_FORMAT_SWIZZLE_W
?
50 swz1
[swz2
[i
]] : swz2
[i
];
54 unsigned r300_get_swizzle_combined(const unsigned char *swizzle_format
,
55 const unsigned char *swizzle_view
,
59 unsigned char swizzle
[4];
61 const uint32_t swizzle_shift
[4] = {
62 R300_TX_FORMAT_R_SHIFT
,
63 R300_TX_FORMAT_G_SHIFT
,
64 R300_TX_FORMAT_B_SHIFT
,
65 R300_TX_FORMAT_A_SHIFT
67 uint32_t swizzle_bit
[4] = {
68 dxtc_swizzle
? R300_TX_FORMAT_Z
: R300_TX_FORMAT_X
,
70 dxtc_swizzle
? R300_TX_FORMAT_X
: R300_TX_FORMAT_Z
,
75 /* Combine two sets of swizzles. */
76 util_format_combine_swizzles(swizzle
, swizzle_format
, swizzle_view
);
78 memcpy(swizzle
, swizzle_format
, 4);
82 for (i
= 0; i
< 4; i
++) {
84 case UTIL_FORMAT_SWIZZLE_Y
:
85 result
|= swizzle_bit
[1] << swizzle_shift
[i
];
87 case UTIL_FORMAT_SWIZZLE_Z
:
88 result
|= swizzle_bit
[2] << swizzle_shift
[i
];
90 case UTIL_FORMAT_SWIZZLE_W
:
91 result
|= swizzle_bit
[3] << swizzle_shift
[i
];
93 case UTIL_FORMAT_SWIZZLE_0
:
94 result
|= R300_TX_FORMAT_ZERO
<< swizzle_shift
[i
];
96 case UTIL_FORMAT_SWIZZLE_1
:
97 result
|= R300_TX_FORMAT_ONE
<< swizzle_shift
[i
];
99 default: /* UTIL_FORMAT_SWIZZLE_X */
100 result
|= swizzle_bit
[0] << swizzle_shift
[i
];
106 /* Translate a pipe_format into a useful texture format for sampling.
108 * Some special formats are translated directly using R300_EASY_TX_FORMAT,
109 * but the majority of them is translated in a generic way, automatically
110 * supporting all the formats hw can support.
112 * R300_EASY_TX_FORMAT swizzles the texture.
113 * Note the signature of R300_EASY_TX_FORMAT:
114 * R300_EASY_TX_FORMAT(B, G, R, A, FORMAT);
116 * The FORMAT specifies how the texture sampler will treat the texture, and
117 * makes available X, Y, Z, W, ZERO, and ONE for swizzling. */
118 uint32_t r300_translate_texformat(enum pipe_format format
,
119 const unsigned char *swizzle_view
,
121 boolean dxtc_swizzle
)
124 const struct util_format_description
*desc
;
126 boolean uniform
= TRUE
;
127 const uint32_t sign_bit
[4] = {
128 R300_TX_FORMAT_SIGNED_W
,
129 R300_TX_FORMAT_SIGNED_Z
,
130 R300_TX_FORMAT_SIGNED_Y
,
131 R300_TX_FORMAT_SIGNED_X
,
134 desc
= util_format_description(format
);
136 /* Colorspace (return non-RGB formats directly). */
137 switch (desc
->colorspace
) {
138 /* Depth stencil formats.
139 * Swizzles are added in r300_merge_textures_and_samplers. */
140 case UTIL_FORMAT_COLORSPACE_ZS
:
142 case PIPE_FORMAT_Z16_UNORM
:
143 return R300_TX_FORMAT_X16
;
144 case PIPE_FORMAT_X8Z24_UNORM
:
145 case PIPE_FORMAT_S8_USCALED_Z24_UNORM
:
147 return R500_TX_FORMAT_Y8X24
;
149 return R300_TX_FORMAT_Y16X16
;
151 return ~0; /* Unsupported. */
155 case UTIL_FORMAT_COLORSPACE_YUV
:
156 result
|= R300_TX_FORMAT_YUV_TO_RGB
;
159 case PIPE_FORMAT_UYVY
:
160 return R300_EASY_TX_FORMAT(X
, Y
, Z
, ONE
, YVYU422
) | result
;
161 case PIPE_FORMAT_YUYV
:
162 return R300_EASY_TX_FORMAT(X
, Y
, Z
, ONE
, VYUY422
) | result
;
164 return ~0; /* Unsupported/unknown. */
167 /* Add gamma correction. */
168 case UTIL_FORMAT_COLORSPACE_SRGB
:
169 result
|= R300_TX_FORMAT_GAMMA
;
174 /* Same as YUV but without the YUR->RGB conversion. */
175 case PIPE_FORMAT_R8G8_B8G8_UNORM
:
176 return R300_EASY_TX_FORMAT(X
, Y
, Z
, ONE
, YVYU422
) | result
;
177 case PIPE_FORMAT_G8R8_G8B8_UNORM
:
178 return R300_EASY_TX_FORMAT(X
, Y
, Z
, ONE
, VYUY422
) | result
;
184 /* The RGTC1_SNORM and LATC1_SNORM swizzle is done in the shader. */
185 if (format
!= PIPE_FORMAT_RGTC1_SNORM
&&
186 format
!= PIPE_FORMAT_LATC1_SNORM
) {
187 if (util_format_is_compressed(format
) &&
189 format
!= PIPE_FORMAT_RGTC2_UNORM
&&
190 format
!= PIPE_FORMAT_RGTC2_SNORM
&&
191 format
!= PIPE_FORMAT_LATC2_UNORM
&&
192 format
!= PIPE_FORMAT_LATC2_SNORM
) {
193 result
|= r300_get_swizzle_combined(desc
->swizzle
, swizzle_view
,
196 result
|= r300_get_swizzle_combined(desc
->swizzle
, swizzle_view
,
202 if (desc
->layout
== UTIL_FORMAT_LAYOUT_S3TC
) {
203 if (!util_format_s3tc_enabled
) {
204 return ~0; /* Unsupported. */
208 case PIPE_FORMAT_DXT1_RGB
:
209 case PIPE_FORMAT_DXT1_RGBA
:
210 case PIPE_FORMAT_DXT1_SRGB
:
211 case PIPE_FORMAT_DXT1_SRGBA
:
212 return R300_TX_FORMAT_DXT1
| result
;
213 case PIPE_FORMAT_DXT3_RGBA
:
214 case PIPE_FORMAT_DXT3_SRGBA
:
215 return R300_TX_FORMAT_DXT3
| result
;
216 case PIPE_FORMAT_DXT5_RGBA
:
217 case PIPE_FORMAT_DXT5_SRGBA
:
218 return R300_TX_FORMAT_DXT5
| result
;
220 return ~0; /* Unsupported/unknown. */
225 if (desc
->layout
== UTIL_FORMAT_LAYOUT_RGTC
) {
227 case PIPE_FORMAT_RGTC1_SNORM
:
228 case PIPE_FORMAT_LATC1_SNORM
:
229 case PIPE_FORMAT_LATC1_UNORM
:
230 case PIPE_FORMAT_RGTC1_UNORM
:
231 return R500_TX_FORMAT_ATI1N
| result
;
233 case PIPE_FORMAT_RGTC2_SNORM
:
234 case PIPE_FORMAT_LATC2_SNORM
:
235 result
|= sign_bit
[1] | sign_bit
[0];
236 case PIPE_FORMAT_RGTC2_UNORM
:
237 case PIPE_FORMAT_LATC2_UNORM
:
238 return R400_TX_FORMAT_ATI2N
| result
;
241 return ~0; /* Unsupported/unknown. */
245 /* This is truly a special format.
246 * It stores R8G8 and B is computed using sqrt(1 - R^2 - G^2)
247 * in the sampler unit. Also known as D3DFMT_CxV8U8. */
248 if (format
== PIPE_FORMAT_R8G8Bx_SNORM
) {
249 return R300_TX_FORMAT_CxV8U8
| result
;
253 for (i
= 0; i
< desc
->nr_channels
; i
++) {
254 if (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_SIGNED
) {
255 result
|= sign_bit
[i
];
259 /* See whether the components are of the same size. */
260 for (i
= 1; i
< desc
->nr_channels
; i
++) {
261 uniform
= uniform
&& desc
->channel
[0].size
== desc
->channel
[i
].size
;
264 /* Non-uniform formats. */
266 switch (desc
->nr_channels
) {
268 if (desc
->channel
[0].size
== 5 &&
269 desc
->channel
[1].size
== 6 &&
270 desc
->channel
[2].size
== 5) {
271 return R300_TX_FORMAT_Z5Y6X5
| result
;
273 if (desc
->channel
[0].size
== 5 &&
274 desc
->channel
[1].size
== 5 &&
275 desc
->channel
[2].size
== 6) {
276 return R300_TX_FORMAT_Z6Y5X5
| result
;
278 if (desc
->channel
[0].size
== 2 &&
279 desc
->channel
[1].size
== 3 &&
280 desc
->channel
[2].size
== 3) {
281 return R300_TX_FORMAT_Z3Y3X2
| result
;
283 return ~0; /* Unsupported/unknown. */
286 if (desc
->channel
[0].size
== 5 &&
287 desc
->channel
[1].size
== 5 &&
288 desc
->channel
[2].size
== 5 &&
289 desc
->channel
[3].size
== 1) {
290 return R300_TX_FORMAT_W1Z5Y5X5
| result
;
292 if (desc
->channel
[0].size
== 10 &&
293 desc
->channel
[1].size
== 10 &&
294 desc
->channel
[2].size
== 10 &&
295 desc
->channel
[3].size
== 2) {
296 return R300_TX_FORMAT_W2Z10Y10X10
| result
;
299 return ~0; /* Unsupported/unknown. */
302 /* Find the first non-VOID channel. */
303 for (i
= 0; i
< 4; i
++) {
304 if (desc
->channel
[i
].type
!= UTIL_FORMAT_TYPE_VOID
) {
310 return ~0; /* Unsupported/unknown. */
312 /* And finally, uniform formats. */
313 switch (desc
->channel
[i
].type
) {
314 case UTIL_FORMAT_TYPE_UNSIGNED
:
315 case UTIL_FORMAT_TYPE_SIGNED
:
316 if (!desc
->channel
[i
].normalized
&&
317 desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_SRGB
) {
321 switch (desc
->channel
[i
].size
) {
323 switch (desc
->nr_channels
) {
325 return R300_TX_FORMAT_Y4X4
| result
;
327 return R300_TX_FORMAT_W4Z4Y4X4
| result
;
332 switch (desc
->nr_channels
) {
334 return R300_TX_FORMAT_X8
| result
;
336 return R300_TX_FORMAT_Y8X8
| result
;
338 return R300_TX_FORMAT_W8Z8Y8X8
| result
;
343 switch (desc
->nr_channels
) {
345 return R300_TX_FORMAT_X16
| result
;
347 return R300_TX_FORMAT_Y16X16
| result
;
349 return R300_TX_FORMAT_W16Z16Y16X16
| result
;
354 case UTIL_FORMAT_TYPE_FLOAT
:
355 switch (desc
->channel
[i
].size
) {
357 switch (desc
->nr_channels
) {
359 return R300_TX_FORMAT_16F
| result
;
361 return R300_TX_FORMAT_16F_16F
| result
;
363 return R300_TX_FORMAT_16F_16F_16F_16F
| result
;
368 switch (desc
->nr_channels
) {
370 return R300_TX_FORMAT_32F
| result
;
372 return R300_TX_FORMAT_32F_32F
| result
;
374 return R300_TX_FORMAT_32F_32F_32F_32F
| result
;
379 return ~0; /* Unsupported/unknown. */
382 uint32_t r500_tx_format_msb_bit(enum pipe_format format
)
385 case PIPE_FORMAT_RGTC1_UNORM
:
386 case PIPE_FORMAT_RGTC1_SNORM
:
387 case PIPE_FORMAT_LATC1_UNORM
:
388 case PIPE_FORMAT_LATC1_SNORM
:
389 case PIPE_FORMAT_X8Z24_UNORM
:
390 case PIPE_FORMAT_S8_USCALED_Z24_UNORM
:
391 return R500_TXFORMAT_MSB
;
397 /* Buffer formats. */
399 /* Colorbuffer formats. This is the unswizzled format of the RB3D block's
400 * output. For the swizzling of the targets, check the shader's format. */
401 static uint32_t r300_translate_colorformat(enum pipe_format format
)
405 case PIPE_FORMAT_A8_UNORM
:
406 case PIPE_FORMAT_A8_SNORM
:
407 case PIPE_FORMAT_I8_UNORM
:
408 case PIPE_FORMAT_I8_SNORM
:
409 case PIPE_FORMAT_L8_UNORM
:
410 case PIPE_FORMAT_L8_SNORM
:
411 case PIPE_FORMAT_R8_UNORM
:
412 case PIPE_FORMAT_R8_SNORM
:
413 return R300_COLOR_FORMAT_I8
;
415 /* 16-bit buffers. */
416 case PIPE_FORMAT_L8A8_UNORM
:
417 case PIPE_FORMAT_L8A8_SNORM
:
418 case PIPE_FORMAT_R8G8_UNORM
:
419 case PIPE_FORMAT_R8G8_SNORM
:
420 /* These formats work fine with UV88 if US_OUT_FMT is set correctly. */
421 case PIPE_FORMAT_A16_UNORM
:
422 case PIPE_FORMAT_A16_SNORM
:
423 /*case PIPE_FORMAT_A16_FLOAT:*/
424 case PIPE_FORMAT_L16_UNORM
:
425 case PIPE_FORMAT_L16_SNORM
:
426 /*case PIPE_FORMAT_L16_FLOAT:*/
427 case PIPE_FORMAT_I16_UNORM
:
428 case PIPE_FORMAT_I16_SNORM
:
429 /*case PIPE_FORMAT_I16_FLOAT:*/
430 case PIPE_FORMAT_R16_UNORM
:
431 case PIPE_FORMAT_R16_SNORM
:
432 case PIPE_FORMAT_R16_FLOAT
:
433 return R300_COLOR_FORMAT_UV88
;
435 case PIPE_FORMAT_B5G6R5_UNORM
:
436 return R300_COLOR_FORMAT_RGB565
;
438 case PIPE_FORMAT_B5G5R5A1_UNORM
:
439 case PIPE_FORMAT_B5G5R5X1_UNORM
:
440 return R300_COLOR_FORMAT_ARGB1555
;
442 case PIPE_FORMAT_B4G4R4A4_UNORM
:
443 case PIPE_FORMAT_B4G4R4X4_UNORM
:
444 return R300_COLOR_FORMAT_ARGB4444
;
446 /* 32-bit buffers. */
447 case PIPE_FORMAT_B8G8R8A8_UNORM
:
448 /*case PIPE_FORMAT_B8G8R8A8_SNORM:*/
449 case PIPE_FORMAT_B8G8R8X8_UNORM
:
450 /*case PIPE_FORMAT_B8G8R8X8_SNORM:*/
451 case PIPE_FORMAT_A8R8G8B8_UNORM
:
452 /*case PIPE_FORMAT_A8R8G8B8_SNORM:*/
453 case PIPE_FORMAT_X8R8G8B8_UNORM
:
454 /*case PIPE_FORMAT_X8R8G8B8_SNORM:*/
455 case PIPE_FORMAT_A8B8G8R8_UNORM
:
456 /*case PIPE_FORMAT_A8B8G8R8_SNORM:*/
457 case PIPE_FORMAT_R8G8B8A8_UNORM
:
458 case PIPE_FORMAT_R8G8B8A8_SNORM
:
459 case PIPE_FORMAT_X8B8G8R8_UNORM
:
460 /*case PIPE_FORMAT_X8B8G8R8_SNORM:*/
461 case PIPE_FORMAT_R8G8B8X8_UNORM
:
462 /*case PIPE_FORMAT_R8G8B8X8_SNORM:*/
463 /* These formats work fine with ARGB8888 if US_OUT_FMT is set
465 case PIPE_FORMAT_R16G16_UNORM
:
466 case PIPE_FORMAT_R16G16_SNORM
:
467 case PIPE_FORMAT_R16G16_FLOAT
:
468 case PIPE_FORMAT_L16A16_UNORM
:
469 case PIPE_FORMAT_L16A16_SNORM
:
470 /*case PIPE_FORMAT_L16A16_FLOAT:
471 case PIPE_FORMAT_A32_FLOAT:
472 case PIPE_FORMAT_L32_FLOAT:
473 case PIPE_FORMAT_I32_FLOAT:*/
474 case PIPE_FORMAT_R32_FLOAT
:
475 return R300_COLOR_FORMAT_ARGB8888
;
477 case PIPE_FORMAT_R10G10B10A2_UNORM
:
478 case PIPE_FORMAT_R10G10B10X2_SNORM
:
479 case PIPE_FORMAT_B10G10R10A2_UNORM
:
480 return R500_COLOR_FORMAT_ARGB2101010
; /* R5xx-only? */
482 /* 64-bit buffers. */
483 case PIPE_FORMAT_R16G16B16A16_UNORM
:
484 case PIPE_FORMAT_R16G16B16A16_SNORM
:
485 case PIPE_FORMAT_R16G16B16A16_FLOAT
:
486 /* These formats work fine with ARGB16161616 if US_OUT_FMT is set
488 case PIPE_FORMAT_R32G32_FLOAT
:
489 /*case PIPE_FORMAT_L32A32_FLOAT:*/
490 return R300_COLOR_FORMAT_ARGB16161616
;
492 /* 128-bit buffers. */
493 case PIPE_FORMAT_R32G32B32A32_FLOAT
:
494 return R300_COLOR_FORMAT_ARGB32323232
;
497 case PIPE_FORMAT_UYVY
:
498 return R300_COLOR_FORMAT_YVYU
;
499 case PIPE_FORMAT_YUYV
:
500 return R300_COLOR_FORMAT_VYUY
;
502 return ~0; /* Unsupported. */
506 /* Depthbuffer and stencilbuffer. Thankfully, we only support two flavors. */
507 static uint32_t r300_translate_zsformat(enum pipe_format format
)
510 /* 16-bit depth, no stencil */
511 case PIPE_FORMAT_Z16_UNORM
:
512 return R300_DEPTHFORMAT_16BIT_INT_Z
;
513 /* 24-bit depth, ignored stencil */
514 case PIPE_FORMAT_X8Z24_UNORM
:
515 /* 24-bit depth, 8-bit stencil */
516 case PIPE_FORMAT_S8_USCALED_Z24_UNORM
:
517 return R300_DEPTHFORMAT_24BIT_INT_Z_8BIT_STENCIL
;
519 return ~0; /* Unsupported. */
523 /* Shader output formats. This is essentially the swizzle from the shader
526 * Note that formats are stored from C3 to C0. */
527 static uint32_t r300_translate_out_fmt(enum pipe_format format
)
529 uint32_t modifier
= 0;
531 const struct util_format_description
*desc
;
532 boolean uniform_sign
;
534 desc
= util_format_description(format
);
536 /* Find the first non-VOID channel. */
537 for (i
= 0; i
< 4; i
++) {
538 if (desc
->channel
[i
].type
!= UTIL_FORMAT_TYPE_VOID
) {
544 return ~0; /* Unsupported/unknown. */
546 /* Specifies how the shader output is written to the fog unit. */
547 switch (desc
->channel
[i
].type
) {
548 case UTIL_FORMAT_TYPE_FLOAT
:
549 switch (desc
->channel
[i
].size
) {
551 switch (desc
->nr_channels
) {
553 modifier
|= R300_US_OUT_FMT_C_32_FP
;
556 modifier
|= R300_US_OUT_FMT_C2_32_FP
;
559 modifier
|= R300_US_OUT_FMT_C4_32_FP
;
565 switch (desc
->nr_channels
) {
567 modifier
|= R300_US_OUT_FMT_C_16_FP
;
570 modifier
|= R300_US_OUT_FMT_C2_16_FP
;
573 modifier
|= R300_US_OUT_FMT_C4_16_FP
;
581 switch (desc
->channel
[i
].size
) {
583 switch (desc
->nr_channels
) {
585 modifier
|= R300_US_OUT_FMT_C_16
;
588 modifier
|= R300_US_OUT_FMT_C2_16
;
591 modifier
|= R300_US_OUT_FMT_C4_16
;
597 modifier
|= R300_US_OUT_FMT_C4_10
;
601 /* C4_8 seems to be used for the formats whose pixel size
603 modifier
|= R300_US_OUT_FMT_C4_8
;
610 for (i
= 0; i
< desc
->nr_channels
; i
++)
611 if (desc
->channel
[i
].type
!= UTIL_FORMAT_TYPE_SIGNED
)
612 uniform_sign
= FALSE
;
615 modifier
|= R300_OUT_SIGN(0xf);
617 /* Add swizzles and return. */
619 /*** Special cases (non-standard channel mapping) ***/
622 * COLORFORMAT_I8 stores the Z component (C2). */
623 case PIPE_FORMAT_A8_UNORM
:
624 case PIPE_FORMAT_A8_SNORM
:
625 return modifier
| R300_C2_SEL_A
;
626 case PIPE_FORMAT_I8_UNORM
:
627 case PIPE_FORMAT_I8_SNORM
:
628 case PIPE_FORMAT_L8_UNORM
:
629 case PIPE_FORMAT_L8_SNORM
:
630 case PIPE_FORMAT_R8_UNORM
:
631 case PIPE_FORMAT_R8_SNORM
:
632 return modifier
| R300_C2_SEL_R
;
635 * COLORFORMAT_UV88 stores ZX (C2 and C0). */
636 case PIPE_FORMAT_L8A8_SNORM
:
637 case PIPE_FORMAT_L8A8_UNORM
:
638 return modifier
| R300_C0_SEL_A
| R300_C2_SEL_R
;
639 case PIPE_FORMAT_R8G8_SNORM
:
640 case PIPE_FORMAT_R8G8_UNORM
:
641 return modifier
| R300_C0_SEL_G
| R300_C2_SEL_R
;
644 * ARGB16161616 stores XZ for RG32F */
645 case PIPE_FORMAT_R32G32_FLOAT
:
646 return modifier
| R300_C0_SEL_R
| R300_C2_SEL_G
;
648 /*** Generic cases (standard channel mapping) ***/
651 case PIPE_FORMAT_B5G6R5_UNORM
:
652 case PIPE_FORMAT_B5G5R5A1_UNORM
:
653 case PIPE_FORMAT_B5G5R5X1_UNORM
:
654 case PIPE_FORMAT_B4G4R4A4_UNORM
:
655 case PIPE_FORMAT_B4G4R4X4_UNORM
:
656 case PIPE_FORMAT_B8G8R8A8_UNORM
:
657 /*case PIPE_FORMAT_B8G8R8A8_SNORM:*/
658 case PIPE_FORMAT_B8G8R8X8_UNORM
:
659 /*case PIPE_FORMAT_B8G8R8X8_SNORM:*/
660 case PIPE_FORMAT_B10G10R10A2_UNORM
:
662 R300_C0_SEL_B
| R300_C1_SEL_G
|
663 R300_C2_SEL_R
| R300_C3_SEL_A
;
666 case PIPE_FORMAT_A8R8G8B8_UNORM
:
667 /*case PIPE_FORMAT_A8R8G8B8_SNORM:*/
668 case PIPE_FORMAT_X8R8G8B8_UNORM
:
669 /*case PIPE_FORMAT_X8R8G8B8_SNORM:*/
670 case PIPE_FORMAT_A16_UNORM
:
671 case PIPE_FORMAT_A16_SNORM
:
672 /*case PIPE_FORMAT_A16_FLOAT:
673 case PIPE_FORMAT_A32_FLOAT:*/
675 R300_C0_SEL_A
| R300_C1_SEL_R
|
676 R300_C2_SEL_G
| R300_C3_SEL_B
;
679 case PIPE_FORMAT_A8B8G8R8_UNORM
:
680 /*case PIPE_FORMAT_A8B8G8R8_SNORM:*/
681 case PIPE_FORMAT_X8B8G8R8_UNORM
:
682 /*case PIPE_FORMAT_X8B8G8R8_SNORM:*/
684 R300_C0_SEL_A
| R300_C1_SEL_B
|
685 R300_C2_SEL_G
| R300_C3_SEL_R
;
688 case PIPE_FORMAT_R8G8B8X8_UNORM
:
689 /*case PIPE_FORMAT_R8G8B8X8_SNORM:*/
690 case PIPE_FORMAT_R8G8B8A8_UNORM
:
691 case PIPE_FORMAT_R8G8B8A8_SNORM
:
692 case PIPE_FORMAT_R10G10B10A2_UNORM
:
693 case PIPE_FORMAT_R10G10B10X2_SNORM
:
694 case PIPE_FORMAT_R16_UNORM
:
695 case PIPE_FORMAT_R16G16_UNORM
:
696 case PIPE_FORMAT_R16G16B16A16_UNORM
:
697 case PIPE_FORMAT_R16_SNORM
:
698 case PIPE_FORMAT_R16G16_SNORM
:
699 case PIPE_FORMAT_R16G16B16A16_SNORM
:
700 case PIPE_FORMAT_R16_FLOAT
:
701 case PIPE_FORMAT_R16G16_FLOAT
:
702 case PIPE_FORMAT_R16G16B16A16_FLOAT
:
703 case PIPE_FORMAT_R32_FLOAT
:
704 case PIPE_FORMAT_R32G32B32A32_FLOAT
:
705 case PIPE_FORMAT_L16_UNORM
:
706 case PIPE_FORMAT_L16_SNORM
:
707 /*case PIPE_FORMAT_L16_FLOAT:
708 case PIPE_FORMAT_L32_FLOAT:*/
709 case PIPE_FORMAT_I16_UNORM
:
710 case PIPE_FORMAT_I16_SNORM
:
711 /*case PIPE_FORMAT_I16_FLOAT:
712 case PIPE_FORMAT_I32_FLOAT:*/
714 R300_C0_SEL_R
| R300_C1_SEL_G
|
715 R300_C2_SEL_B
| R300_C3_SEL_A
;
718 case PIPE_FORMAT_L16A16_UNORM
:
719 case PIPE_FORMAT_L16A16_SNORM
:
720 /*case PIPE_FORMAT_L16A16_FLOAT:
721 case PIPE_FORMAT_L32A32_FLOAT:*/
723 R300_C0_SEL_R
| R300_C1_SEL_A
;
726 return ~0; /* Unsupported. */
730 boolean
r300_is_colorbuffer_format_supported(enum pipe_format format
)
732 return r300_translate_colorformat(format
) != ~0 &&
733 r300_translate_out_fmt(format
) != ~0;
736 boolean
r300_is_zs_format_supported(enum pipe_format format
)
738 return r300_translate_zsformat(format
) != ~0;
741 boolean
r300_is_sampler_format_supported(enum pipe_format format
)
743 return r300_translate_texformat(format
, 0, TRUE
, FALSE
) != ~0;
746 void r300_texture_setup_format_state(struct r300_screen
*screen
,
747 struct r300_resource
*tex
,
749 struct r300_texture_format_state
*out
)
751 struct pipe_resource
*pt
= &tex
->b
.b
.b
;
752 struct r300_texture_desc
*desc
= &tex
->tex
;
753 boolean is_r500
= screen
->caps
.is_r500
;
755 /* Mask out all the fields we change. */
757 out
->format1
&= ~R300_TX_FORMAT_TEX_COORD_TYPE_MASK
;
758 out
->format2
&= R500_TXFORMAT_MSB
;
759 out
->tile_config
= 0;
761 /* Set sampler state. */
763 R300_TX_WIDTH((u_minify(desc
->width0
, level
) - 1) & 0x7ff) |
764 R300_TX_HEIGHT((u_minify(desc
->height0
, level
) - 1) & 0x7ff) |
765 R300_TX_DEPTH(util_logbase2(u_minify(desc
->depth0
, level
)) & 0xf);
767 if (desc
->uses_stride_addressing
) {
768 /* rectangles love this */
769 out
->format0
|= R300_TX_PITCH_EN
;
770 out
->format2
= (desc
->stride_in_pixels
[level
] - 1) & 0x1fff;
773 if (pt
->target
== PIPE_TEXTURE_CUBE
) {
774 out
->format1
|= R300_TX_FORMAT_CUBIC_MAP
;
776 if (pt
->target
== PIPE_TEXTURE_3D
) {
777 out
->format1
|= R300_TX_FORMAT_3D
;
780 /* large textures on r500 */
783 if (desc
->width0
> 2048) {
784 out
->format2
|= R500_TXWIDTH_BIT11
;
786 if (desc
->height0
> 2048) {
787 out
->format2
|= R500_TXHEIGHT_BIT11
;
791 out
->tile_config
= R300_TXO_MACRO_TILE(desc
->macrotile
[level
]) |
792 R300_TXO_MICRO_TILE(desc
->microtile
);
795 static void r300_texture_setup_fb_state(struct r300_surface
*surf
)
797 struct r300_resource
*tex
= r300_resource(surf
->base
.texture
);
798 unsigned level
= surf
->base
.u
.tex
.level
;
800 /* Set framebuffer state. */
801 if (util_format_is_depth_or_stencil(surf
->base
.format
)) {
803 tex
->tex
.stride_in_pixels
[level
] |
804 R300_DEPTHMACROTILE(tex
->tex
.macrotile
[level
]) |
805 R300_DEPTHMICROTILE(tex
->tex
.microtile
);
806 surf
->format
= r300_translate_zsformat(surf
->base
.format
);
807 surf
->pitch_zmask
= tex
->tex
.zmask_stride_in_pixels
[level
];
808 surf
->pitch_hiz
= tex
->tex
.hiz_stride_in_pixels
[level
];
811 tex
->tex
.stride_in_pixels
[level
] |
812 r300_translate_colorformat(surf
->base
.format
) |
813 R300_COLOR_TILE(tex
->tex
.macrotile
[level
]) |
814 R300_COLOR_MICROTILE(tex
->tex
.microtile
);
815 surf
->format
= r300_translate_out_fmt(surf
->base
.format
);
819 boolean
r300_resource_set_properties(struct pipe_screen
*screen
,
820 struct pipe_resource
*tex
,
822 const struct pipe_resource
*new_properties
)
824 struct r300_screen
*rscreen
= r300_screen(screen
);
825 struct r300_resource
*res
= r300_resource(tex
);
827 SCREEN_DBG(rscreen
, DBG_TEX
,
828 "r300: texture_set_properties: %s -> %s\n",
829 util_format_short_name(tex
->format
),
830 util_format_short_name(new_properties
->format
));
832 if (!r300_texture_desc_init(rscreen
, res
, new_properties
)) {
833 fprintf(stderr
, "r300: ERROR: Cannot set texture properties.\n");
836 res
->tex_offset
= offset
;
837 r300_texture_setup_format_state(rscreen
, res
, 0, &res
->tx_format
);
842 static void r300_texture_destroy(struct pipe_screen
*screen
,
843 struct pipe_resource
* texture
)
845 struct r300_resource
* tex
= (struct r300_resource
*)texture
;
847 r300_winsys_bo_reference(&tex
->buf
, NULL
);
851 boolean
r300_resource_get_handle(struct pipe_screen
* screen
,
852 struct pipe_resource
*texture
,
853 struct winsys_handle
*whandle
)
855 struct r300_winsys_screen
*rws
= (struct r300_winsys_screen
*)screen
->winsys
;
856 struct r300_resource
* tex
= (struct r300_resource
*)texture
;
862 return rws
->buffer_get_handle(tex
->buf
,
863 tex
->tex
.stride_in_bytes
[0], whandle
);
866 static const struct u_resource_vtbl r300_texture_vtbl
=
868 NULL
, /* get_handle */
869 r300_texture_destroy
, /* resource_destroy */
870 r300_texture_get_transfer
, /* get_transfer */
871 r300_texture_transfer_destroy
, /* transfer_destroy */
872 r300_texture_transfer_map
, /* transfer_map */
873 NULL
, /* transfer_flush_region */
874 r300_texture_transfer_unmap
, /* transfer_unmap */
875 u_default_transfer_inline_write
/* transfer_inline_write */
878 /* The common texture constructor. */
879 static struct r300_resource
*
880 r300_texture_create_object(struct r300_screen
*rscreen
,
881 const struct pipe_resource
*base
,
882 enum r300_buffer_tiling microtile
,
883 enum r300_buffer_tiling macrotile
,
884 unsigned stride_in_bytes_override
,
885 unsigned max_buffer_size
,
886 struct r300_winsys_bo
*buffer
)
888 struct r300_winsys_screen
*rws
= rscreen
->rws
;
889 struct r300_resource
*tex
= CALLOC_STRUCT(r300_resource
);
892 r300_winsys_bo_reference(&buffer
, NULL
);
896 pipe_reference_init(&tex
->b
.b
.b
.reference
, 1);
897 tex
->b
.b
.b
.screen
= &rscreen
->screen
;
898 tex
->b
.b
.b
.usage
= base
->usage
;
899 tex
->b
.b
.b
.bind
= base
->bind
;
900 tex
->b
.b
.b
.flags
= base
->flags
;
901 tex
->b
.b
.vtbl
= &r300_texture_vtbl
;
902 tex
->tex
.microtile
= microtile
;
903 tex
->tex
.macrotile
[0] = macrotile
;
904 tex
->tex
.stride_in_bytes_override
= stride_in_bytes_override
;
905 tex
->domain
= base
->flags
& R300_RESOURCE_FLAG_TRANSFER
?
907 R300_DOMAIN_VRAM
| R300_DOMAIN_GTT
;
908 tex
->buf_size
= max_buffer_size
;
910 if (!r300_resource_set_properties(&rscreen
->screen
, &tex
->b
.b
.b
, 0, base
)) {
912 r300_winsys_bo_reference(&buffer
, NULL
);
917 /* Create the backing buffer if needed. */
919 tex
->buf_size
= tex
->tex
.size_in_bytes
;
920 tex
->buf
= rws
->buffer_create(rws
, tex
->tex
.size_in_bytes
, 2048,
921 base
->bind
, base
->usage
, tex
->domain
);
931 tex
->cs_buf
= rws
->buffer_get_cs_handle(tex
->buf
);
933 rws
->buffer_set_tiling(tex
->buf
, NULL
,
934 tex
->tex
.microtile
, tex
->tex
.macrotile
[0],
935 tex
->tex
.stride_in_bytes
[0]);
940 /* Create a new texture. */
941 struct pipe_resource
*r300_texture_create(struct pipe_screen
*screen
,
942 const struct pipe_resource
*base
)
944 struct r300_screen
*rscreen
= r300_screen(screen
);
945 enum r300_buffer_tiling microtile
, macrotile
;
947 if ((base
->flags
& R300_RESOURCE_FLAG_TRANSFER
) ||
948 (base
->bind
& PIPE_BIND_SCANOUT
)) {
949 microtile
= R300_BUFFER_LINEAR
;
950 macrotile
= R300_BUFFER_LINEAR
;
952 microtile
= R300_BUFFER_SELECT_LAYOUT
;
953 macrotile
= R300_BUFFER_SELECT_LAYOUT
;
956 return (struct pipe_resource
*)
957 r300_texture_create_object(rscreen
, base
, microtile
, macrotile
,
961 struct pipe_resource
*r300_texture_from_handle(struct pipe_screen
*screen
,
962 const struct pipe_resource
*base
,
963 struct winsys_handle
*whandle
)
965 struct r300_winsys_screen
*rws
= (struct r300_winsys_screen
*)screen
->winsys
;
966 struct r300_screen
*rscreen
= r300_screen(screen
);
967 struct r300_winsys_bo
*buffer
;
968 enum r300_buffer_tiling microtile
, macrotile
;
969 unsigned stride
, size
;
971 /* Support only 2D textures without mipmaps */
972 if ((base
->target
!= PIPE_TEXTURE_2D
&&
973 base
->target
!= PIPE_TEXTURE_RECT
) ||
975 base
->last_level
!= 0) {
979 buffer
= rws
->buffer_from_handle(rws
, whandle
, &stride
, &size
);
983 rws
->buffer_get_tiling(buffer
, µtile
, ¯otile
);
985 /* Enforce a microtiled zbuffer. */
986 if (util_format_is_depth_or_stencil(base
->format
) &&
987 microtile
== R300_BUFFER_LINEAR
) {
988 switch (util_format_get_blocksize(base
->format
)) {
990 microtile
= R300_BUFFER_TILED
;
994 microtile
= R300_BUFFER_SQUARETILED
;
999 return (struct pipe_resource
*)
1000 r300_texture_create_object(rscreen
, base
, microtile
, macrotile
,
1001 stride
, size
, buffer
);
1004 /* Not required to implement u_resource_vtbl, consider moving to another file:
1006 struct pipe_surface
* r300_create_surface(struct pipe_context
* ctx
,
1007 struct pipe_resource
* texture
,
1008 const struct pipe_surface
*surf_tmpl
)
1010 struct r300_resource
* tex
= r300_resource(texture
);
1011 struct r300_surface
* surface
= CALLOC_STRUCT(r300_surface
);
1012 unsigned level
= surf_tmpl
->u
.tex
.level
;
1014 assert(surf_tmpl
->u
.tex
.first_layer
== surf_tmpl
->u
.tex
.last_layer
);
1017 uint32_t offset
, tile_height
;
1019 pipe_reference_init(&surface
->base
.reference
, 1);
1020 pipe_resource_reference(&surface
->base
.texture
, texture
);
1021 surface
->base
.context
= ctx
;
1022 surface
->base
.format
= surf_tmpl
->format
;
1023 surface
->base
.width
= u_minify(texture
->width0
, level
);
1024 surface
->base
.height
= u_minify(texture
->height0
, level
);
1025 surface
->base
.usage
= surf_tmpl
->usage
;
1026 surface
->base
.u
.tex
.level
= level
;
1027 surface
->base
.u
.tex
.first_layer
= surf_tmpl
->u
.tex
.first_layer
;
1028 surface
->base
.u
.tex
.last_layer
= surf_tmpl
->u
.tex
.last_layer
;
1030 surface
->buf
= tex
->buf
;
1031 surface
->cs_buf
= tex
->cs_buf
;
1033 /* Prefer VRAM if there are multiple domains to choose from. */
1034 surface
->domain
= tex
->domain
;
1035 if (surface
->domain
& R300_DOMAIN_VRAM
)
1036 surface
->domain
&= ~R300_DOMAIN_GTT
;
1038 surface
->offset
= r300_texture_get_offset(tex
, level
,
1039 surf_tmpl
->u
.tex
.first_layer
);
1040 r300_texture_setup_fb_state(surface
);
1042 /* Parameters for the CBZB clear. */
1043 surface
->cbzb_allowed
= tex
->tex
.cbzb_allowed
[level
];
1044 surface
->cbzb_width
= align(surface
->base
.width
, 64);
1046 /* Height must be aligned to the size of a tile. */
1047 tile_height
= r300_get_pixel_alignment(tex
->b
.b
.b
.format
,
1048 tex
->b
.b
.b
.nr_samples
,
1050 tex
->tex
.macrotile
[level
],
1053 surface
->cbzb_height
= align((surface
->base
.height
+ 1) / 2,
1056 /* Offset must be aligned to 2K and must point at the beginning
1058 offset
= surface
->offset
+
1059 tex
->tex
.stride_in_bytes
[level
] * surface
->cbzb_height
;
1060 surface
->cbzb_midpoint_offset
= offset
& ~2047;
1062 surface
->cbzb_pitch
= surface
->pitch
& 0x1ffffc;
1064 if (util_format_get_blocksizebits(surface
->base
.format
) == 32)
1065 surface
->cbzb_format
= R300_DEPTHFORMAT_24BIT_INT_Z_8BIT_STENCIL
;
1067 surface
->cbzb_format
= R300_DEPTHFORMAT_16BIT_INT_Z
;
1069 DBG(r300_context(ctx
), DBG_CBZB
,
1070 "CBZB Allowed: %s, Dim: %ix%i, Misalignment: %i, Micro: %s, Macro: %s\n",
1071 surface
->cbzb_allowed
? "YES" : " NO",
1072 surface
->cbzb_width
, surface
->cbzb_height
,
1074 tex
->tex
.microtile
? "YES" : " NO",
1075 tex
->tex
.macrotile
[level
] ? "YES" : " NO");
1078 return &surface
->base
;
1081 /* Not required to implement u_resource_vtbl, consider moving to another file:
1083 void r300_surface_destroy(struct pipe_context
*ctx
, struct pipe_surface
* s
)
1085 pipe_resource_reference(&s
->texture
, NULL
);