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 unsigned r300_get_swizzle_combined(const unsigned char *swizzle_format
,
43 const unsigned char *swizzle_view
,
47 unsigned char swizzle
[4];
49 const uint32_t swizzle_shift
[4] = {
50 R300_TX_FORMAT_R_SHIFT
,
51 R300_TX_FORMAT_G_SHIFT
,
52 R300_TX_FORMAT_B_SHIFT
,
53 R300_TX_FORMAT_A_SHIFT
55 uint32_t swizzle_bit
[4] = {
56 dxtc_swizzle
? R300_TX_FORMAT_Z
: R300_TX_FORMAT_X
,
58 dxtc_swizzle
? R300_TX_FORMAT_X
: R300_TX_FORMAT_Z
,
63 /* Combine two sets of swizzles. */
64 for (i
= 0; i
< 4; i
++) {
65 swizzle
[i
] = swizzle_view
[i
] <= UTIL_FORMAT_SWIZZLE_W
?
66 swizzle_format
[swizzle_view
[i
]] : swizzle_view
[i
];
69 memcpy(swizzle
, swizzle_format
, 4);
73 for (i
= 0; i
< 4; i
++) {
75 case UTIL_FORMAT_SWIZZLE_Y
:
76 result
|= swizzle_bit
[1] << swizzle_shift
[i
];
78 case UTIL_FORMAT_SWIZZLE_Z
:
79 result
|= swizzle_bit
[2] << swizzle_shift
[i
];
81 case UTIL_FORMAT_SWIZZLE_W
:
82 result
|= swizzle_bit
[3] << swizzle_shift
[i
];
84 case UTIL_FORMAT_SWIZZLE_0
:
85 result
|= R300_TX_FORMAT_ZERO
<< swizzle_shift
[i
];
87 case UTIL_FORMAT_SWIZZLE_1
:
88 result
|= R300_TX_FORMAT_ONE
<< swizzle_shift
[i
];
90 default: /* UTIL_FORMAT_SWIZZLE_X */
91 result
|= swizzle_bit
[0] << swizzle_shift
[i
];
97 /* Translate a pipe_format into a useful texture format for sampling.
99 * Some special formats are translated directly using R300_EASY_TX_FORMAT,
100 * but the majority of them is translated in a generic way, automatically
101 * supporting all the formats hw can support.
103 * R300_EASY_TX_FORMAT swizzles the texture.
104 * Note the signature of R300_EASY_TX_FORMAT:
105 * R300_EASY_TX_FORMAT(B, G, R, A, FORMAT);
107 * The FORMAT specifies how the texture sampler will treat the texture, and
108 * makes available X, Y, Z, W, ZERO, and ONE for swizzling. */
109 uint32_t r300_translate_texformat(enum pipe_format format
,
110 const unsigned char *swizzle_view
,
112 boolean dxtc_swizzle
)
115 const struct util_format_description
*desc
;
117 boolean uniform
= TRUE
;
118 const uint32_t sign_bit
[4] = {
119 R300_TX_FORMAT_SIGNED_X
,
120 R300_TX_FORMAT_SIGNED_Y
,
121 R300_TX_FORMAT_SIGNED_Z
,
122 R300_TX_FORMAT_SIGNED_W
,
125 desc
= util_format_description(format
);
127 /* Colorspace (return non-RGB formats directly). */
128 switch (desc
->colorspace
) {
129 /* Depth stencil formats.
130 * Swizzles are added in r300_merge_textures_and_samplers. */
131 case UTIL_FORMAT_COLORSPACE_ZS
:
133 case PIPE_FORMAT_Z16_UNORM
:
134 return R300_TX_FORMAT_X16
;
135 case PIPE_FORMAT_X8Z24_UNORM
:
136 case PIPE_FORMAT_S8_USCALED_Z24_UNORM
:
138 return R500_TX_FORMAT_Y8X24
;
140 return R300_TX_FORMAT_Y16X16
;
142 return ~0; /* Unsupported. */
146 case UTIL_FORMAT_COLORSPACE_YUV
:
147 result
|= R300_TX_FORMAT_YUV_TO_RGB
;
150 case PIPE_FORMAT_UYVY
:
151 return R300_EASY_TX_FORMAT(X
, Y
, Z
, ONE
, YVYU422
) | result
;
152 case PIPE_FORMAT_YUYV
:
153 return R300_EASY_TX_FORMAT(X
, Y
, Z
, ONE
, VYUY422
) | result
;
155 return ~0; /* Unsupported/unknown. */
158 /* Add gamma correction. */
159 case UTIL_FORMAT_COLORSPACE_SRGB
:
160 result
|= R300_TX_FORMAT_GAMMA
;
165 /* Same as YUV but without the YUR->RGB conversion. */
166 case PIPE_FORMAT_R8G8_B8G8_UNORM
:
167 return R300_EASY_TX_FORMAT(X
, Y
, Z
, ONE
, YVYU422
) | result
;
168 case PIPE_FORMAT_G8R8_G8B8_UNORM
:
169 return R300_EASY_TX_FORMAT(X
, Y
, Z
, ONE
, VYUY422
) | result
;
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
,
188 if (desc
->layout
== UTIL_FORMAT_LAYOUT_S3TC
) {
189 if (!util_format_s3tc_enabled
) {
190 return ~0; /* Unsupported. */
194 case PIPE_FORMAT_DXT1_RGB
:
195 case PIPE_FORMAT_DXT1_RGBA
:
196 case PIPE_FORMAT_DXT1_SRGB
:
197 case PIPE_FORMAT_DXT1_SRGBA
:
198 return R300_TX_FORMAT_DXT1
| result
;
199 case PIPE_FORMAT_DXT3_RGBA
:
200 case PIPE_FORMAT_DXT3_SRGBA
:
201 return R300_TX_FORMAT_DXT3
| result
;
202 case PIPE_FORMAT_DXT5_RGBA
:
203 case PIPE_FORMAT_DXT5_SRGBA
:
204 return R300_TX_FORMAT_DXT5
| result
;
206 return ~0; /* Unsupported/unknown. */
211 if (desc
->layout
== UTIL_FORMAT_LAYOUT_RGTC
) {
213 case PIPE_FORMAT_RGTC1_SNORM
:
214 case PIPE_FORMAT_LATC1_SNORM
:
215 result
|= sign_bit
[1];
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
[2] | sign_bit
[3];
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
;
240 for (i
= 0; i
< desc
->nr_channels
; i
++) {
241 if (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_SIGNED
) {
242 result
|= sign_bit
[i
];
246 /* See whether the components are of the same size. */
247 for (i
= 1; i
< desc
->nr_channels
; i
++) {
248 uniform
= uniform
&& desc
->channel
[0].size
== desc
->channel
[i
].size
;
251 /* Non-uniform formats. */
253 switch (desc
->nr_channels
) {
255 if (desc
->channel
[0].size
== 5 &&
256 desc
->channel
[1].size
== 6 &&
257 desc
->channel
[2].size
== 5) {
258 return R300_TX_FORMAT_Z5Y6X5
| result
;
260 if (desc
->channel
[0].size
== 5 &&
261 desc
->channel
[1].size
== 5 &&
262 desc
->channel
[2].size
== 6) {
263 return R300_TX_FORMAT_Z6Y5X5
| result
;
265 if (desc
->channel
[0].size
== 2 &&
266 desc
->channel
[1].size
== 3 &&
267 desc
->channel
[2].size
== 3) {
268 return R300_TX_FORMAT_Z3Y3X2
| result
;
270 return ~0; /* Unsupported/unknown. */
273 if (desc
->channel
[0].size
== 5 &&
274 desc
->channel
[1].size
== 5 &&
275 desc
->channel
[2].size
== 5 &&
276 desc
->channel
[3].size
== 1) {
277 return R300_TX_FORMAT_W1Z5Y5X5
| result
;
279 if (desc
->channel
[0].size
== 10 &&
280 desc
->channel
[1].size
== 10 &&
281 desc
->channel
[2].size
== 10 &&
282 desc
->channel
[3].size
== 2) {
283 return R300_TX_FORMAT_W2Z10Y10X10
| result
;
286 return ~0; /* Unsupported/unknown. */
289 /* Find the first non-VOID channel. */
290 for (i
= 0; i
< 4; i
++) {
291 if (desc
->channel
[i
].type
!= UTIL_FORMAT_TYPE_VOID
) {
297 return ~0; /* Unsupported/unknown. */
299 /* And finally, uniform formats. */
300 switch (desc
->channel
[i
].type
) {
301 case UTIL_FORMAT_TYPE_UNSIGNED
:
302 case UTIL_FORMAT_TYPE_SIGNED
:
303 if (!desc
->channel
[i
].normalized
&&
304 desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_SRGB
) {
308 switch (desc
->channel
[i
].size
) {
310 switch (desc
->nr_channels
) {
312 return R300_TX_FORMAT_Y4X4
| result
;
314 return R300_TX_FORMAT_W4Z4Y4X4
| result
;
319 switch (desc
->nr_channels
) {
321 return R300_TX_FORMAT_X8
| result
;
323 return R300_TX_FORMAT_Y8X8
| result
;
325 return R300_TX_FORMAT_W8Z8Y8X8
| result
;
330 switch (desc
->nr_channels
) {
332 return R300_TX_FORMAT_X16
| result
;
334 return R300_TX_FORMAT_Y16X16
| result
;
336 return R300_TX_FORMAT_W16Z16Y16X16
| result
;
341 case UTIL_FORMAT_TYPE_FLOAT
:
342 switch (desc
->channel
[i
].size
) {
344 switch (desc
->nr_channels
) {
346 return R300_TX_FORMAT_16F
| result
;
348 return R300_TX_FORMAT_16F_16F
| result
;
350 return R300_TX_FORMAT_16F_16F_16F_16F
| result
;
355 switch (desc
->nr_channels
) {
357 return R300_TX_FORMAT_32F
| result
;
359 return R300_TX_FORMAT_32F_32F
| result
;
361 return R300_TX_FORMAT_32F_32F_32F_32F
| result
;
366 return ~0; /* Unsupported/unknown. */
369 uint32_t r500_tx_format_msb_bit(enum pipe_format format
)
372 case PIPE_FORMAT_RGTC1_UNORM
:
373 case PIPE_FORMAT_RGTC1_SNORM
:
374 case PIPE_FORMAT_LATC1_UNORM
:
375 case PIPE_FORMAT_LATC1_SNORM
:
376 case PIPE_FORMAT_X8Z24_UNORM
:
377 case PIPE_FORMAT_S8_USCALED_Z24_UNORM
:
378 return R500_TXFORMAT_MSB
;
384 /* Buffer formats. */
386 /* Colorbuffer formats. This is the unswizzled format of the RB3D block's
387 * output. For the swizzling of the targets, check the shader's format. */
388 static uint32_t r300_translate_colorformat(enum pipe_format format
)
392 case PIPE_FORMAT_A8_UNORM
:
393 /*case PIPE_FORMAT_A8_SNORM:*/
394 case PIPE_FORMAT_I8_UNORM
:
395 /*case PIPE_FORMAT_I8_SNORM:*/
396 case PIPE_FORMAT_L8_UNORM
:
397 /*case PIPE_FORMAT_L8_SNORM:*/
398 case PIPE_FORMAT_R8_UNORM
:
399 case PIPE_FORMAT_R8_SNORM
:
400 return R300_COLOR_FORMAT_I8
;
402 /* 16-bit buffers. */
403 case PIPE_FORMAT_L8A8_UNORM
:
404 /*case PIPE_FORMAT_L8A8_SNORM:*/
405 case PIPE_FORMAT_R8G8_UNORM
:
406 case PIPE_FORMAT_R8G8_SNORM
:
407 return R300_COLOR_FORMAT_UV88
;
409 case PIPE_FORMAT_B5G6R5_UNORM
:
410 return R300_COLOR_FORMAT_RGB565
;
412 case PIPE_FORMAT_B5G5R5A1_UNORM
:
413 case PIPE_FORMAT_B5G5R5X1_UNORM
:
414 return R300_COLOR_FORMAT_ARGB1555
;
416 case PIPE_FORMAT_B4G4R4A4_UNORM
:
417 case PIPE_FORMAT_B4G4R4X4_UNORM
:
418 return R300_COLOR_FORMAT_ARGB4444
;
420 /* 32-bit buffers. */
421 case PIPE_FORMAT_B8G8R8A8_UNORM
:
422 /*case PIPE_FORMAT_B8G8R8A8_SNORM:*/
423 case PIPE_FORMAT_B8G8R8X8_UNORM
:
424 /*case PIPE_FORMAT_B8G8R8X8_SNORM:*/
425 case PIPE_FORMAT_A8R8G8B8_UNORM
:
426 /*case PIPE_FORMAT_A8R8G8B8_SNORM:*/
427 case PIPE_FORMAT_X8R8G8B8_UNORM
:
428 /*case PIPE_FORMAT_X8R8G8B8_SNORM:*/
429 case PIPE_FORMAT_A8B8G8R8_UNORM
:
430 /*case PIPE_FORMAT_A8B8G8R8_SNORM:*/
431 case PIPE_FORMAT_R8G8B8A8_UNORM
:
432 case PIPE_FORMAT_R8G8B8A8_SNORM
:
433 case PIPE_FORMAT_X8B8G8R8_UNORM
:
434 /*case PIPE_FORMAT_X8B8G8R8_SNORM:*/
435 case PIPE_FORMAT_R8G8B8X8_UNORM
:
436 /*case PIPE_FORMAT_R8G8B8X8_SNORM:*/
437 case PIPE_FORMAT_R8SG8SB8UX8U_NORM
:
438 return R300_COLOR_FORMAT_ARGB8888
;
440 case PIPE_FORMAT_R10G10B10A2_UNORM
:
441 case PIPE_FORMAT_R10G10B10X2_SNORM
:
442 case PIPE_FORMAT_B10G10R10A2_UNORM
:
443 case PIPE_FORMAT_R10SG10SB10SA2U_NORM
:
444 return R500_COLOR_FORMAT_ARGB2101010
; /* R5xx-only? */
446 /* 64-bit buffers. */
447 case PIPE_FORMAT_R16G16B16A16_UNORM
:
448 case PIPE_FORMAT_R16G16B16A16_SNORM
:
449 case PIPE_FORMAT_R16G16B16A16_FLOAT
:
450 return R300_COLOR_FORMAT_ARGB16161616
;
452 /* 128-bit buffers. */
453 case PIPE_FORMAT_R32G32B32A32_FLOAT
:
454 return R300_COLOR_FORMAT_ARGB32323232
;
457 case PIPE_FORMAT_UYVY
:
458 return R300_COLOR_FORMAT_YVYU
;
459 case PIPE_FORMAT_YUYV
:
460 return R300_COLOR_FORMAT_VYUY
;
462 return ~0; /* Unsupported. */
466 /* Depthbuffer and stencilbuffer. Thankfully, we only support two flavors. */
467 static uint32_t r300_translate_zsformat(enum pipe_format format
)
470 /* 16-bit depth, no stencil */
471 case PIPE_FORMAT_Z16_UNORM
:
472 return R300_DEPTHFORMAT_16BIT_INT_Z
;
473 /* 24-bit depth, ignored stencil */
474 case PIPE_FORMAT_X8Z24_UNORM
:
475 /* 24-bit depth, 8-bit stencil */
476 case PIPE_FORMAT_S8_USCALED_Z24_UNORM
:
477 return R300_DEPTHFORMAT_24BIT_INT_Z_8BIT_STENCIL
;
479 return ~0; /* Unsupported. */
483 /* Shader output formats. This is essentially the swizzle from the shader
486 * Note that formats are stored from C3 to C0. */
487 static uint32_t r300_translate_out_fmt(enum pipe_format format
)
489 uint32_t modifier
= 0;
491 const struct util_format_description
*desc
;
492 static const uint32_t sign_bit
[4] = {
499 desc
= util_format_description(format
);
501 /* Find the first non-VOID channel. */
502 for (i
= 0; i
< 4; i
++) {
503 if (desc
->channel
[i
].type
!= UTIL_FORMAT_TYPE_VOID
) {
509 return ~0; /* Unsupported/unknown. */
511 /* Specifies how the shader output is written to the fog unit. */
512 if (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_FLOAT
) {
513 if (desc
->channel
[i
].size
== 32) {
514 modifier
|= R300_US_OUT_FMT_C4_32_FP
;
516 modifier
|= R300_US_OUT_FMT_C4_16_FP
;
519 if (desc
->channel
[i
].size
== 16) {
520 modifier
|= R300_US_OUT_FMT_C4_16
;
521 } else if (desc
->channel
[i
].size
== 10) {
522 modifier
|= R300_US_OUT_FMT_C4_10
;
524 /* C4_8 seems to be used for the formats whose pixel size
526 modifier
|= R300_US_OUT_FMT_C4_8
;
531 for (i
= 0; i
< 4; i
++)
532 if (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_SIGNED
) {
533 modifier
|= sign_bit
[i
];
536 /* Add swizzles and return. */
538 /* 8-bit outputs, one channel.
539 * COLORFORMAT_I8 stores the C2 component. */
540 case PIPE_FORMAT_A8_UNORM
:
541 /*case PIPE_FORMAT_A8_SNORM:*/
542 return modifier
| R300_C2_SEL_A
;
543 case PIPE_FORMAT_I8_UNORM
:
544 /*case PIPE_FORMAT_I8_SNORM:*/
545 case PIPE_FORMAT_L8_UNORM
:
546 /*case PIPE_FORMAT_L8_SNORM:*/
547 case PIPE_FORMAT_R8_UNORM
:
548 case PIPE_FORMAT_R8_SNORM
:
549 return modifier
| R300_C2_SEL_R
;
551 /* 16-bit outputs, two channels.
552 * COLORFORMAT_UV88 stores C2 and C0. */
553 case PIPE_FORMAT_L8A8_UNORM
:
554 /*case PIPE_FORMAT_L8A8_SNORM:*/
555 return modifier
| R300_C0_SEL_A
| R300_C2_SEL_R
;
556 case PIPE_FORMAT_R8G8_UNORM
:
557 case PIPE_FORMAT_R8G8_SNORM
:
558 return modifier
| R300_C0_SEL_G
| R300_C2_SEL_R
;
561 case PIPE_FORMAT_B5G6R5_UNORM
:
562 case PIPE_FORMAT_B5G5R5A1_UNORM
:
563 case PIPE_FORMAT_B5G5R5X1_UNORM
:
564 case PIPE_FORMAT_B4G4R4A4_UNORM
:
565 case PIPE_FORMAT_B4G4R4X4_UNORM
:
566 case PIPE_FORMAT_B8G8R8A8_UNORM
:
567 /*case PIPE_FORMAT_B8G8R8A8_SNORM:*/
568 case PIPE_FORMAT_B8G8R8X8_UNORM
:
569 /*case PIPE_FORMAT_B8G8R8X8_SNORM:*/
570 case PIPE_FORMAT_B10G10R10A2_UNORM
:
572 R300_C0_SEL_B
| R300_C1_SEL_G
|
573 R300_C2_SEL_R
| R300_C3_SEL_A
;
576 case PIPE_FORMAT_A8R8G8B8_UNORM
:
577 /*case PIPE_FORMAT_A8R8G8B8_SNORM:*/
578 case PIPE_FORMAT_X8R8G8B8_UNORM
:
579 /*case PIPE_FORMAT_X8R8G8B8_SNORM:*/
581 R300_C0_SEL_A
| R300_C1_SEL_R
|
582 R300_C2_SEL_G
| R300_C3_SEL_B
;
585 case PIPE_FORMAT_A8B8G8R8_UNORM
:
586 /*case PIPE_FORMAT_A8B8G8R8_SNORM:*/
587 case PIPE_FORMAT_X8B8G8R8_UNORM
:
588 /*case PIPE_FORMAT_X8B8G8R8_SNORM:*/
590 R300_C0_SEL_A
| R300_C1_SEL_B
|
591 R300_C2_SEL_G
| R300_C3_SEL_R
;
594 case PIPE_FORMAT_R8G8B8X8_UNORM
:
595 /*case PIPE_FORMAT_R8G8B8X8_SNORM:*/
596 case PIPE_FORMAT_R8G8B8A8_UNORM
:
597 case PIPE_FORMAT_R8G8B8A8_SNORM
:
598 case PIPE_FORMAT_R8SG8SB8UX8U_NORM
:
599 case PIPE_FORMAT_R10G10B10A2_UNORM
:
600 case PIPE_FORMAT_R10G10B10X2_SNORM
:
601 case PIPE_FORMAT_R10SG10SB10SA2U_NORM
:
602 case PIPE_FORMAT_R16G16B16A16_UNORM
:
603 case PIPE_FORMAT_R16G16B16A16_SNORM
:
604 case PIPE_FORMAT_R16G16B16A16_FLOAT
:
605 case PIPE_FORMAT_R32G32B32A32_FLOAT
:
607 R300_C0_SEL_R
| R300_C1_SEL_G
|
608 R300_C2_SEL_B
| R300_C3_SEL_A
;
611 return ~0; /* Unsupported. */
615 boolean
r300_is_colorbuffer_format_supported(enum pipe_format format
)
617 return r300_translate_colorformat(format
) != ~0 &&
618 r300_translate_out_fmt(format
) != ~0;
621 boolean
r300_is_zs_format_supported(enum pipe_format format
)
623 return r300_translate_zsformat(format
) != ~0;
626 boolean
r300_is_sampler_format_supported(enum pipe_format format
)
628 return r300_translate_texformat(format
, 0, TRUE
, FALSE
) != ~0;
631 void r300_texture_setup_format_state(struct r300_screen
*screen
,
632 struct r300_resource
*tex
,
634 struct r300_texture_format_state
*out
)
636 struct pipe_resource
*pt
= &tex
->b
.b
.b
;
637 struct r300_texture_desc
*desc
= &tex
->tex
;
638 boolean is_r500
= screen
->caps
.is_r500
;
640 /* Mask out all the fields we change. */
642 out
->format1
&= ~R300_TX_FORMAT_TEX_COORD_TYPE_MASK
;
643 out
->format2
&= R500_TXFORMAT_MSB
;
644 out
->tile_config
= 0;
646 /* Set sampler state. */
648 R300_TX_WIDTH((u_minify(desc
->width0
, level
) - 1) & 0x7ff) |
649 R300_TX_HEIGHT((u_minify(desc
->height0
, level
) - 1) & 0x7ff) |
650 R300_TX_DEPTH(util_logbase2(u_minify(desc
->depth0
, level
)) & 0xf);
652 if (desc
->uses_stride_addressing
) {
653 /* rectangles love this */
654 out
->format0
|= R300_TX_PITCH_EN
;
655 out
->format2
= (desc
->stride_in_pixels
[level
] - 1) & 0x1fff;
658 if (pt
->target
== PIPE_TEXTURE_CUBE
) {
659 out
->format1
|= R300_TX_FORMAT_CUBIC_MAP
;
661 if (pt
->target
== PIPE_TEXTURE_3D
) {
662 out
->format1
|= R300_TX_FORMAT_3D
;
665 /* large textures on r500 */
668 if (desc
->width0
> 2048) {
669 out
->format2
|= R500_TXWIDTH_BIT11
;
671 if (desc
->height0
> 2048) {
672 out
->format2
|= R500_TXHEIGHT_BIT11
;
676 out
->tile_config
= R300_TXO_MACRO_TILE(desc
->macrotile
[level
]) |
677 R300_TXO_MICRO_TILE(desc
->microtile
);
680 static void r300_texture_setup_fb_state(struct r300_surface
*surf
)
682 struct r300_resource
*tex
= r300_resource(surf
->base
.texture
);
683 unsigned level
= surf
->base
.u
.tex
.level
;
685 /* Set framebuffer state. */
686 if (util_format_is_depth_or_stencil(surf
->base
.format
)) {
688 tex
->tex
.stride_in_pixels
[level
] |
689 R300_DEPTHMACROTILE(tex
->tex
.macrotile
[level
]) |
690 R300_DEPTHMICROTILE(tex
->tex
.microtile
);
691 surf
->format
= r300_translate_zsformat(surf
->base
.format
);
692 surf
->pitch_zmask
= tex
->tex
.zmask_stride_in_pixels
[level
];
693 surf
->pitch_hiz
= tex
->tex
.hiz_stride_in_pixels
[level
];
696 tex
->tex
.stride_in_pixels
[level
] |
697 r300_translate_colorformat(surf
->base
.format
) |
698 R300_COLOR_TILE(tex
->tex
.macrotile
[level
]) |
699 R300_COLOR_MICROTILE(tex
->tex
.microtile
);
700 surf
->format
= r300_translate_out_fmt(surf
->base
.format
);
704 boolean
r300_resource_set_properties(struct pipe_screen
*screen
,
705 struct pipe_resource
*tex
,
707 const struct pipe_resource
*new_properties
)
709 struct r300_screen
*rscreen
= r300_screen(screen
);
710 struct r300_resource
*res
= r300_resource(tex
);
712 SCREEN_DBG(rscreen
, DBG_TEX
,
713 "r300: texture_set_properties: %s -> %s\n",
714 util_format_short_name(tex
->format
),
715 util_format_short_name(new_properties
->format
));
717 if (!r300_texture_desc_init(rscreen
, res
, new_properties
)) {
718 fprintf(stderr
, "r300: ERROR: Cannot set texture properties.\n");
721 res
->tex_offset
= offset
;
722 r300_texture_setup_format_state(rscreen
, res
, 0, &res
->tx_format
);
727 static void r300_texture_destroy(struct pipe_screen
*screen
,
728 struct pipe_resource
* texture
)
730 struct r300_resource
* tex
= (struct r300_resource
*)texture
;
732 r300_winsys_bo_reference(&tex
->buf
, NULL
);
736 boolean
r300_resource_get_handle(struct pipe_screen
* screen
,
737 struct pipe_resource
*texture
,
738 struct winsys_handle
*whandle
)
740 struct r300_winsys_screen
*rws
= (struct r300_winsys_screen
*)screen
->winsys
;
741 struct r300_resource
* tex
= (struct r300_resource
*)texture
;
747 return rws
->buffer_get_handle(tex
->buf
,
748 tex
->tex
.stride_in_bytes
[0], whandle
);
751 static const struct u_resource_vtbl r300_texture_vtbl
=
753 NULL
, /* get_handle */
754 r300_texture_destroy
, /* resource_destroy */
755 NULL
, /* is_resource_referenced */
756 r300_texture_get_transfer
, /* get_transfer */
757 r300_texture_transfer_destroy
, /* transfer_destroy */
758 r300_texture_transfer_map
, /* transfer_map */
759 NULL
, /* transfer_flush_region */
760 r300_texture_transfer_unmap
, /* transfer_unmap */
761 u_default_transfer_inline_write
/* transfer_inline_write */
764 /* The common texture constructor. */
765 static struct r300_resource
*
766 r300_texture_create_object(struct r300_screen
*rscreen
,
767 const struct pipe_resource
*base
,
768 enum r300_buffer_tiling microtile
,
769 enum r300_buffer_tiling macrotile
,
770 unsigned stride_in_bytes_override
,
771 unsigned max_buffer_size
,
772 struct r300_winsys_bo
*buffer
)
774 struct r300_winsys_screen
*rws
= rscreen
->rws
;
775 struct r300_resource
*tex
= CALLOC_STRUCT(r300_resource
);
778 r300_winsys_bo_reference(&buffer
, NULL
);
782 pipe_reference_init(&tex
->b
.b
.b
.reference
, 1);
783 tex
->b
.b
.b
.screen
= &rscreen
->screen
;
784 tex
->b
.b
.b
.usage
= base
->usage
;
785 tex
->b
.b
.b
.bind
= base
->bind
;
786 tex
->b
.b
.b
.flags
= base
->flags
;
787 tex
->b
.b
.vtbl
= &r300_texture_vtbl
;
788 tex
->tex
.microtile
= microtile
;
789 tex
->tex
.macrotile
[0] = macrotile
;
790 tex
->tex
.stride_in_bytes_override
= stride_in_bytes_override
;
791 tex
->domain
= base
->flags
& R300_RESOURCE_FLAG_TRANSFER
?
793 R300_DOMAIN_VRAM
| R300_DOMAIN_GTT
;
794 tex
->buf_size
= max_buffer_size
;
796 if (!r300_resource_set_properties(&rscreen
->screen
, &tex
->b
.b
.b
, 0, base
)) {
798 r300_winsys_bo_reference(&buffer
, NULL
);
803 /* Create the backing buffer if needed. */
805 tex
->buf_size
= tex
->tex
.size_in_bytes
;
806 tex
->buf
= rws
->buffer_create(rws
, tex
->tex
.size_in_bytes
, 2048,
807 base
->bind
, base
->usage
, tex
->domain
);
817 tex
->cs_buf
= rws
->buffer_get_cs_handle(tex
->buf
);
819 rws
->buffer_set_tiling(tex
->buf
, NULL
,
820 tex
->tex
.microtile
, tex
->tex
.macrotile
[0],
821 tex
->tex
.stride_in_bytes
[0]);
826 /* Create a new texture. */
827 struct pipe_resource
*r300_texture_create(struct pipe_screen
*screen
,
828 const struct pipe_resource
*base
)
830 struct r300_screen
*rscreen
= r300_screen(screen
);
831 enum r300_buffer_tiling microtile
, macrotile
;
833 if ((base
->flags
& R300_RESOURCE_FLAG_TRANSFER
) ||
834 (base
->bind
& PIPE_BIND_SCANOUT
)) {
835 microtile
= R300_BUFFER_LINEAR
;
836 macrotile
= R300_BUFFER_LINEAR
;
838 microtile
= R300_BUFFER_SELECT_LAYOUT
;
839 macrotile
= R300_BUFFER_SELECT_LAYOUT
;
842 return (struct pipe_resource
*)
843 r300_texture_create_object(rscreen
, base
, microtile
, macrotile
,
847 struct pipe_resource
*r300_texture_from_handle(struct pipe_screen
*screen
,
848 const struct pipe_resource
*base
,
849 struct winsys_handle
*whandle
)
851 struct r300_winsys_screen
*rws
= (struct r300_winsys_screen
*)screen
->winsys
;
852 struct r300_screen
*rscreen
= r300_screen(screen
);
853 struct r300_winsys_bo
*buffer
;
854 enum r300_buffer_tiling microtile
, macrotile
;
855 unsigned stride
, size
;
857 /* Support only 2D textures without mipmaps */
858 if ((base
->target
!= PIPE_TEXTURE_2D
&&
859 base
->target
!= PIPE_TEXTURE_RECT
) ||
861 base
->last_level
!= 0) {
865 buffer
= rws
->buffer_from_handle(rws
, whandle
, &stride
, &size
);
869 rws
->buffer_get_tiling(buffer
, µtile
, ¯otile
);
871 /* Enforce a microtiled zbuffer. */
872 if (util_format_is_depth_or_stencil(base
->format
) &&
873 microtile
== R300_BUFFER_LINEAR
) {
874 switch (util_format_get_blocksize(base
->format
)) {
876 microtile
= R300_BUFFER_TILED
;
880 microtile
= R300_BUFFER_SQUARETILED
;
885 return (struct pipe_resource
*)
886 r300_texture_create_object(rscreen
, base
, microtile
, macrotile
,
887 stride
, size
, buffer
);
890 /* Not required to implement u_resource_vtbl, consider moving to another file:
892 struct pipe_surface
* r300_create_surface(struct pipe_context
* ctx
,
893 struct pipe_resource
* texture
,
894 const struct pipe_surface
*surf_tmpl
)
896 struct r300_resource
* tex
= r300_resource(texture
);
897 struct r300_surface
* surface
= CALLOC_STRUCT(r300_surface
);
898 unsigned level
= surf_tmpl
->u
.tex
.level
;
900 assert(surf_tmpl
->u
.tex
.first_layer
== surf_tmpl
->u
.tex
.last_layer
);
903 uint32_t offset
, tile_height
;
905 pipe_reference_init(&surface
->base
.reference
, 1);
906 pipe_resource_reference(&surface
->base
.texture
, texture
);
907 surface
->base
.context
= ctx
;
908 surface
->base
.format
= surf_tmpl
->format
;
909 surface
->base
.width
= u_minify(texture
->width0
, level
);
910 surface
->base
.height
= u_minify(texture
->height0
, level
);
911 surface
->base
.usage
= surf_tmpl
->usage
;
912 surface
->base
.u
.tex
.level
= level
;
913 surface
->base
.u
.tex
.first_layer
= surf_tmpl
->u
.tex
.first_layer
;
914 surface
->base
.u
.tex
.last_layer
= surf_tmpl
->u
.tex
.last_layer
;
916 surface
->buf
= tex
->buf
;
917 surface
->cs_buf
= tex
->cs_buf
;
919 /* Prefer VRAM if there are multiple domains to choose from. */
920 surface
->domain
= tex
->domain
;
921 if (surface
->domain
& R300_DOMAIN_VRAM
)
922 surface
->domain
&= ~R300_DOMAIN_GTT
;
924 surface
->offset
= r300_texture_get_offset(tex
, level
,
925 surf_tmpl
->u
.tex
.first_layer
);
926 r300_texture_setup_fb_state(surface
);
928 /* Parameters for the CBZB clear. */
929 surface
->cbzb_allowed
= tex
->tex
.cbzb_allowed
[level
];
930 surface
->cbzb_width
= align(surface
->base
.width
, 64);
932 /* Height must be aligned to the size of a tile. */
933 tile_height
= r300_get_pixel_alignment(tex
->b
.b
.b
.format
,
934 tex
->b
.b
.b
.nr_samples
,
936 tex
->tex
.macrotile
[level
],
939 surface
->cbzb_height
= align((surface
->base
.height
+ 1) / 2,
942 /* Offset must be aligned to 2K and must point at the beginning
944 offset
= surface
->offset
+
945 tex
->tex
.stride_in_bytes
[level
] * surface
->cbzb_height
;
946 surface
->cbzb_midpoint_offset
= offset
& ~2047;
948 surface
->cbzb_pitch
= surface
->pitch
& 0x1ffffc;
950 if (util_format_get_blocksizebits(surface
->base
.format
) == 32)
951 surface
->cbzb_format
= R300_DEPTHFORMAT_24BIT_INT_Z_8BIT_STENCIL
;
953 surface
->cbzb_format
= R300_DEPTHFORMAT_16BIT_INT_Z
;
955 DBG(r300_context(ctx
), DBG_CBZB
,
956 "CBZB Allowed: %s, Dim: %ix%i, Misalignment: %i, Micro: %s, Macro: %s\n",
957 surface
->cbzb_allowed
? "YES" : " NO",
958 surface
->cbzb_width
, surface
->cbzb_height
,
960 tex
->tex
.microtile
? "YES" : " NO",
961 tex
->tex
.macrotile
[level
] ? "YES" : " NO");
964 return &surface
->base
;
967 /* Not required to implement u_resource_vtbl, consider moving to another file:
969 void r300_surface_destroy(struct pipe_context
*ctx
, struct pipe_surface
* s
)
971 pipe_resource_reference(&s
->texture
, NULL
);