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 #include "pipe/p_screen.h"
26 #include "util/u_format.h"
27 #include "util/u_math.h"
28 #include "util/u_memory.h"
30 #include "r300_context.h"
32 #include "r300_texture.h"
33 #include "r300_transfer.h"
34 #include "r300_screen.h"
35 #include "r300_winsys.h"
37 /* XXX Enable float textures here. */
38 /*#define ENABLE_FLOAT_TEXTURES*/
43 static const unsigned microblock_table
[5][3][2] = {
44 /*linear tiled square-tiled */
45 {{32, 1}, {8, 4}, {0, 0}}, /* 8 bits per pixel */
46 {{16, 1}, {8, 2}, {4, 4}}, /* 16 bits per pixel */
47 {{ 8, 1}, {4, 2}, {0, 0}}, /* 32 bits per pixel */
48 {{ 4, 1}, {0, 0}, {2, 2}}, /* 64 bits per pixel */
49 {{ 2, 1}, {0, 0}, {0, 0}} /* 128 bits per pixel */
52 /* Return true for non-compressed and non-YUV formats. */
53 static boolean
r300_format_is_plain(enum pipe_format format
)
55 const struct util_format_description
*desc
= util_format_description(format
);
61 return desc
->layout
== UTIL_FORMAT_LAYOUT_PLAIN
;
64 /* Translate a pipe_format into a useful texture format for sampling.
66 * Some special formats are translated directly using R300_EASY_TX_FORMAT,
67 * but the majority of them is translated in a generic way, automatically
68 * supporting all the formats hw can support.
70 * R300_EASY_TX_FORMAT swizzles the texture.
71 * Note the signature of R300_EASY_TX_FORMAT:
72 * R300_EASY_TX_FORMAT(B, G, R, A, FORMAT);
74 * The FORMAT specifies how the texture sampler will treat the texture, and
75 * makes available X, Y, Z, W, ZERO, and ONE for swizzling. */
76 uint32_t r300_translate_texformat(enum pipe_format format
,
77 const unsigned char *swizzle
)
80 const struct util_format_description
*desc
;
82 boolean uniform
= TRUE
;
83 const uint32_t swizzle_shift
[4] = {
84 R300_TX_FORMAT_R_SHIFT
,
85 R300_TX_FORMAT_G_SHIFT
,
86 R300_TX_FORMAT_B_SHIFT
,
87 R300_TX_FORMAT_A_SHIFT
89 const uint32_t swizzle_bit
[4] = {
95 const uint32_t sign_bit
[4] = {
96 R300_TX_FORMAT_SIGNED_X
,
97 R300_TX_FORMAT_SIGNED_Y
,
98 R300_TX_FORMAT_SIGNED_Z
,
99 R300_TX_FORMAT_SIGNED_W
,
102 desc
= util_format_description(format
);
104 /* Colorspace (return non-RGB formats directly). */
105 switch (desc
->colorspace
) {
106 /* Depth stencil formats. */
107 case UTIL_FORMAT_COLORSPACE_ZS
:
109 case PIPE_FORMAT_Z16_UNORM
:
110 return R300_EASY_TX_FORMAT(X
, X
, X
, X
, X16
);
111 case PIPE_FORMAT_X8Z24_UNORM
:
112 case PIPE_FORMAT_S8_USCALED_Z24_UNORM
:
113 return R300_EASY_TX_FORMAT(X
, X
, X
, X
, W24_FP
);
115 return ~0; /* Unsupported. */
119 case UTIL_FORMAT_COLORSPACE_YUV
:
120 result
|= R300_TX_FORMAT_YUV_TO_RGB
;
123 case PIPE_FORMAT_UYVY
:
124 return R300_EASY_TX_FORMAT(X
, Y
, Z
, ONE
, YVYU422
) | result
;
125 case PIPE_FORMAT_YUYV
:
126 return R300_EASY_TX_FORMAT(X
, Y
, Z
, ONE
, VYUY422
) | result
;
128 return ~0; /* Unsupported/unknown. */
131 /* Add gamma correction. */
132 case UTIL_FORMAT_COLORSPACE_SRGB
:
133 result
|= R300_TX_FORMAT_GAMMA
;
138 /* Same as YUV but without the YUR->RGB conversion. */
139 case PIPE_FORMAT_R8G8_B8G8_UNORM
:
140 return R300_EASY_TX_FORMAT(X
, Y
, Z
, ONE
, YVYU422
) | result
;
141 case PIPE_FORMAT_G8R8_G8B8_UNORM
:
142 return R300_EASY_TX_FORMAT(X
, Y
, Z
, ONE
, VYUY422
) | result
;
149 swizzle
= desc
->swizzle
;
151 if (swizzle[0] != desc->swizzle[0] ||
152 swizzle[1] != desc->swizzle[1] ||
153 swizzle[2] != desc->swizzle[2] ||
154 swizzle[3] != desc->swizzle[3])
156 const char n[6] = "RGBA01";
157 fprintf(stderr, "Got different swizzling! Format: %c%c%c%c, "
159 n[desc->swizzle[0]], n[desc->swizzle[1]],
160 n[desc->swizzle[2]], n[desc->swizzle[3]],
161 n[swizzle[0]], n[swizzle[1]], n[swizzle[2]],
166 for (i
= 0; i
< 4; i
++) {
167 switch (swizzle
[i
]) {
168 case UTIL_FORMAT_SWIZZLE_X
:
169 case UTIL_FORMAT_SWIZZLE_NONE
:
170 result
|= swizzle_bit
[0] << swizzle_shift
[i
];
172 case UTIL_FORMAT_SWIZZLE_Y
:
173 result
|= swizzle_bit
[1] << swizzle_shift
[i
];
175 case UTIL_FORMAT_SWIZZLE_Z
:
176 result
|= swizzle_bit
[2] << swizzle_shift
[i
];
178 case UTIL_FORMAT_SWIZZLE_W
:
179 result
|= swizzle_bit
[3] << swizzle_shift
[i
];
181 case UTIL_FORMAT_SWIZZLE_0
:
182 result
|= R300_TX_FORMAT_ZERO
<< swizzle_shift
[i
];
184 case UTIL_FORMAT_SWIZZLE_1
:
185 result
|= R300_TX_FORMAT_ONE
<< swizzle_shift
[i
];
188 return ~0; /* Unsupported. */
193 if (desc
->layout
== UTIL_FORMAT_LAYOUT_S3TC
) {
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 for (i
= 0; i
< desc
->nr_channels
; i
++) {
213 if (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_SIGNED
) {
214 result
|= sign_bit
[i
];
218 /* This is truly a special format.
219 * It stores R8G8 and B is computed using sqrt(1 - R^2 - G^2)
220 * in the sampler unit. Also known as D3DFMT_CxV8U8. */
221 if (format
== PIPE_FORMAT_R8G8Bx_SNORM
) {
222 return R300_TX_FORMAT_CxV8U8
| result
;
226 if (desc
->layout
== UTIL_FORMAT_LAYOUT_RGTC
) {
228 case PIPE_FORMAT_RGTC1_UNORM
:
229 case PIPE_FORMAT_RGTC1_SNORM
:
230 return R500_TX_FORMAT_ATI1N
| result
;
231 case PIPE_FORMAT_RGTC2_UNORM
:
232 case PIPE_FORMAT_RGTC2_SNORM
:
233 return R400_TX_FORMAT_ATI2N
| result
;
235 return ~0; /* Unsupported/unknown. */
239 /* See whether the components are of the same size. */
240 for (i
= 1; i
< desc
->nr_channels
; i
++) {
241 uniform
= uniform
&& desc
->channel
[0].size
== desc
->channel
[i
].size
;
244 /* Non-uniform formats. */
246 switch (desc
->nr_channels
) {
248 if (desc
->channel
[0].size
== 5 &&
249 desc
->channel
[1].size
== 6 &&
250 desc
->channel
[2].size
== 5) {
251 return R300_TX_FORMAT_Z5Y6X5
| result
;
253 if (desc
->channel
[0].size
== 5 &&
254 desc
->channel
[1].size
== 5 &&
255 desc
->channel
[2].size
== 6) {
256 return R300_TX_FORMAT_Z6Y5X5
| result
;
258 return ~0; /* Unsupported/unknown. */
261 if (desc
->channel
[0].size
== 5 &&
262 desc
->channel
[1].size
== 5 &&
263 desc
->channel
[2].size
== 5 &&
264 desc
->channel
[3].size
== 1) {
265 return R300_TX_FORMAT_W1Z5Y5X5
| result
;
267 if (desc
->channel
[0].size
== 10 &&
268 desc
->channel
[1].size
== 10 &&
269 desc
->channel
[2].size
== 10 &&
270 desc
->channel
[3].size
== 2) {
271 return R300_TX_FORMAT_W2Z10Y10X10
| result
;
274 return ~0; /* Unsupported/unknown. */
277 /* And finally, uniform formats. */
278 switch (desc
->channel
[0].type
) {
279 case UTIL_FORMAT_TYPE_UNSIGNED
:
280 case UTIL_FORMAT_TYPE_SIGNED
:
281 if (!desc
->channel
[0].normalized
&&
282 desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_SRGB
) {
286 switch (desc
->channel
[0].size
) {
288 switch (desc
->nr_channels
) {
290 return R300_TX_FORMAT_Y4X4
| result
;
292 return R300_TX_FORMAT_W4Z4Y4X4
| result
;
297 switch (desc
->nr_channels
) {
299 return R300_TX_FORMAT_X8
| result
;
301 return R300_TX_FORMAT_Y8X8
| result
;
303 return R300_TX_FORMAT_W8Z8Y8X8
| result
;
308 switch (desc
->nr_channels
) {
310 return R300_TX_FORMAT_X16
| result
;
312 return R300_TX_FORMAT_Y16X16
| result
;
314 return R300_TX_FORMAT_W16Z16Y16X16
| result
;
319 #if defined(ENABLE_FLOAT_TEXTURES)
320 case UTIL_FORMAT_TYPE_FLOAT
:
321 switch (desc
->channel
[0].size
) {
323 switch (desc
->nr_channels
) {
325 return R300_TX_FORMAT_16F
| result
;
327 return R300_TX_FORMAT_16F_16F
| result
;
329 return R300_TX_FORMAT_16F_16F_16F_16F
| result
;
334 switch (desc
->nr_channels
) {
336 return R300_TX_FORMAT_32F
| result
;
338 return R300_TX_FORMAT_32F_32F
| result
;
340 return R300_TX_FORMAT_32F_32F_32F_32F
| result
;
346 return ~0; /* Unsupported/unknown. */
349 uint32_t r500_tx_format_msb_bit(enum pipe_format format
)
352 case PIPE_FORMAT_RGTC1_UNORM
:
353 case PIPE_FORMAT_RGTC1_SNORM
:
354 return R500_TXFORMAT_MSB
;
360 /* Buffer formats. */
362 /* Colorbuffer formats. This is the unswizzled format of the RB3D block's
363 * output. For the swizzling of the targets, check the shader's format. */
364 static uint32_t r300_translate_colorformat(enum pipe_format format
)
368 case PIPE_FORMAT_A8_UNORM
:
369 case PIPE_FORMAT_I8_UNORM
:
370 case PIPE_FORMAT_L8_UNORM
:
371 case PIPE_FORMAT_R8_UNORM
:
372 case PIPE_FORMAT_R8_SNORM
:
373 return R300_COLOR_FORMAT_I8
;
375 /* 16-bit buffers. */
376 case PIPE_FORMAT_B5G6R5_UNORM
:
377 return R300_COLOR_FORMAT_RGB565
;
379 case PIPE_FORMAT_B5G5R5A1_UNORM
:
380 case PIPE_FORMAT_B5G5R5X1_UNORM
:
381 return R300_COLOR_FORMAT_ARGB1555
;
383 case PIPE_FORMAT_B4G4R4A4_UNORM
:
384 case PIPE_FORMAT_B4G4R4X4_UNORM
:
385 return R300_COLOR_FORMAT_ARGB4444
;
387 /* 32-bit buffers. */
388 case PIPE_FORMAT_B8G8R8A8_UNORM
:
389 case PIPE_FORMAT_B8G8R8X8_UNORM
:
390 case PIPE_FORMAT_A8R8G8B8_UNORM
:
391 case PIPE_FORMAT_X8R8G8B8_UNORM
:
392 case PIPE_FORMAT_A8B8G8R8_UNORM
:
393 case PIPE_FORMAT_R8G8B8A8_SNORM
:
394 case PIPE_FORMAT_X8B8G8R8_UNORM
:
395 case PIPE_FORMAT_R8G8B8X8_UNORM
:
396 case PIPE_FORMAT_R8SG8SB8UX8U_NORM
:
397 return R300_COLOR_FORMAT_ARGB8888
;
399 case PIPE_FORMAT_R10G10B10A2_UNORM
:
400 case PIPE_FORMAT_R10G10B10X2_SNORM
:
401 case PIPE_FORMAT_B10G10R10A2_UNORM
:
402 case PIPE_FORMAT_R10SG10SB10SA2U_NORM
:
403 return R500_COLOR_FORMAT_ARGB2101010
; /* R5xx-only? */
405 /* 64-bit buffers. */
406 case PIPE_FORMAT_R16G16B16A16_UNORM
:
407 case PIPE_FORMAT_R16G16B16A16_SNORM
:
408 #if defined(ENABLE_FLOAT_TEXTURES)
409 case PIPE_FORMAT_R16G16B16A16_FLOAT
:
411 return R300_COLOR_FORMAT_ARGB16161616
;
413 /* 128-bit buffers. */
414 #if defined(ENABLE_FLOAT_TEXTURES)
415 case PIPE_FORMAT_R32G32B32A32_FLOAT
:
416 return R300_COLOR_FORMAT_ARGB32323232
;
420 case PIPE_FORMAT_UYVY
:
421 return R300_COLOR_FORMAT_YVYU
;
422 case PIPE_FORMAT_YUYV
:
423 return R300_COLOR_FORMAT_VYUY
;
425 return ~0; /* Unsupported. */
429 /* Depthbuffer and stencilbuffer. Thankfully, we only support two flavors. */
430 static uint32_t r300_translate_zsformat(enum pipe_format format
)
433 /* 16-bit depth, no stencil */
434 case PIPE_FORMAT_Z16_UNORM
:
435 return R300_DEPTHFORMAT_16BIT_INT_Z
;
436 /* 24-bit depth, ignored stencil */
437 case PIPE_FORMAT_X8Z24_UNORM
:
438 /* 24-bit depth, 8-bit stencil */
439 case PIPE_FORMAT_S8_USCALED_Z24_UNORM
:
440 return R300_DEPTHFORMAT_24BIT_INT_Z_8BIT_STENCIL
;
442 return ~0; /* Unsupported. */
446 /* Shader output formats. This is essentially the swizzle from the shader
449 * Note that formats are stored from C3 to C0. */
450 static uint32_t r300_translate_out_fmt(enum pipe_format format
)
452 uint32_t modifier
= 0;
454 const struct util_format_description
*desc
;
455 static const uint32_t sign_bit
[4] = {
462 desc
= util_format_description(format
);
464 /* Specifies how the shader output is written to the fog unit. */
465 if (desc
->channel
[0].type
== UTIL_FORMAT_TYPE_FLOAT
) {
466 if (desc
->channel
[0].size
== 32) {
467 modifier
|= R300_US_OUT_FMT_C4_32_FP
;
469 modifier
|= R300_US_OUT_FMT_C4_16_FP
;
472 if (desc
->channel
[0].size
== 16) {
473 modifier
|= R300_US_OUT_FMT_C4_16
;
475 /* C4_8 seems to be used for the formats whose pixel size
477 modifier
|= R300_US_OUT_FMT_C4_8
;
482 for (i
= 0; i
< 4; i
++)
483 if (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_SIGNED
) {
484 modifier
|= sign_bit
[i
];
487 /* Add swizzles and return. */
490 * COLORFORMAT_I8 stores the C2 component. */
491 case PIPE_FORMAT_A8_UNORM
:
492 return modifier
| R300_C2_SEL_A
;
493 case PIPE_FORMAT_I8_UNORM
:
494 case PIPE_FORMAT_L8_UNORM
:
495 case PIPE_FORMAT_R8_UNORM
:
496 case PIPE_FORMAT_R8_SNORM
:
497 return modifier
| R300_C2_SEL_R
;
500 case PIPE_FORMAT_B5G6R5_UNORM
:
501 case PIPE_FORMAT_B5G5R5A1_UNORM
:
502 case PIPE_FORMAT_B5G5R5X1_UNORM
:
503 case PIPE_FORMAT_B4G4R4A4_UNORM
:
504 case PIPE_FORMAT_B4G4R4X4_UNORM
:
505 case PIPE_FORMAT_B8G8R8A8_UNORM
:
506 case PIPE_FORMAT_B8G8R8X8_UNORM
:
507 case PIPE_FORMAT_B10G10R10A2_UNORM
:
509 R300_C0_SEL_B
| R300_C1_SEL_G
|
510 R300_C2_SEL_R
| R300_C3_SEL_A
;
513 case PIPE_FORMAT_A8R8G8B8_UNORM
:
514 case PIPE_FORMAT_X8R8G8B8_UNORM
:
516 R300_C0_SEL_A
| R300_C1_SEL_R
|
517 R300_C2_SEL_G
| R300_C3_SEL_B
;
520 case PIPE_FORMAT_A8B8G8R8_UNORM
:
521 case PIPE_FORMAT_X8B8G8R8_UNORM
:
523 R300_C0_SEL_A
| R300_C1_SEL_B
|
524 R300_C2_SEL_G
| R300_C3_SEL_R
;
527 case PIPE_FORMAT_R8G8B8X8_UNORM
:
528 case PIPE_FORMAT_R8G8B8A8_SNORM
:
529 case PIPE_FORMAT_R8SG8SB8UX8U_NORM
:
530 case PIPE_FORMAT_R10G10B10A2_UNORM
:
531 case PIPE_FORMAT_R10G10B10X2_SNORM
:
532 case PIPE_FORMAT_R10SG10SB10SA2U_NORM
:
533 case PIPE_FORMAT_R16G16B16A16_UNORM
:
534 case PIPE_FORMAT_R16G16B16A16_SNORM
:
535 //case PIPE_FORMAT_R16G16B16A16_FLOAT: /* not in pipe_format */
536 case PIPE_FORMAT_R32G32B32A32_FLOAT
:
538 R300_C0_SEL_R
| R300_C1_SEL_G
|
539 R300_C2_SEL_B
| R300_C3_SEL_A
;
542 return ~0; /* Unsupported. */
546 boolean
r300_is_colorbuffer_format_supported(enum pipe_format format
)
548 return r300_translate_colorformat(format
) != ~0 &&
549 r300_translate_out_fmt(format
) != ~0;
552 boolean
r300_is_zs_format_supported(enum pipe_format format
)
554 return r300_translate_zsformat(format
) != ~0;
557 boolean
r300_is_sampler_format_supported(enum pipe_format format
)
559 return r300_translate_texformat(format
, 0) != ~0;
562 static void r300_texture_setup_immutable_state(struct r300_screen
* screen
,
563 struct r300_texture
* tex
)
565 struct r300_texture_format_state
* f
= &tex
->tx_format
;
566 struct pipe_resource
*pt
= &tex
->b
.b
;
567 boolean is_r500
= screen
->caps
.is_r500
;
569 /* Set sampler state. */
570 f
->format0
= R300_TX_WIDTH((pt
->width0
- 1) & 0x7ff) |
571 R300_TX_HEIGHT((pt
->height0
- 1) & 0x7ff);
573 if (tex
->uses_pitch
) {
574 /* rectangles love this */
575 f
->format0
|= R300_TX_PITCH_EN
;
576 f
->format2
= (tex
->pitch
[0] - 1) & 0x1fff;
578 /* power of two textures (3D, mipmaps, and no pitch) */
579 f
->format0
|= R300_TX_DEPTH(util_logbase2(pt
->depth0
) & 0xf);
583 if (pt
->target
== PIPE_TEXTURE_CUBE
) {
584 f
->format1
|= R300_TX_FORMAT_CUBIC_MAP
;
586 if (pt
->target
== PIPE_TEXTURE_3D
) {
587 f
->format1
|= R300_TX_FORMAT_3D
;
590 /* large textures on r500 */
593 if (pt
->width0
> 2048) {
594 f
->format2
|= R500_TXWIDTH_BIT11
;
596 if (pt
->height0
> 2048) {
597 f
->format2
|= R500_TXHEIGHT_BIT11
;
601 f
->tile_config
= R300_TXO_MACRO_TILE(tex
->macrotile
) |
602 R300_TXO_MICRO_TILE(tex
->microtile
);
604 SCREEN_DBG(screen
, DBG_TEX
, "r300: Set texture state (%dx%d, %d levels)\n",
605 pt
->width0
, pt
->height0
, pt
->last_level
);
608 static void r300_texture_setup_fb_state(struct r300_screen
* screen
,
609 struct r300_texture
* tex
)
613 /* Set framebuffer state. */
614 if (util_format_is_depth_or_stencil(tex
->b
.b
.format
)) {
615 for (i
= 0; i
<= tex
->b
.b
.last_level
; i
++) {
616 tex
->fb_state
.depthpitch
[i
] =
618 R300_DEPTHMACROTILE(tex
->mip_macrotile
[i
]) |
619 R300_DEPTHMICROTILE(tex
->microtile
);
621 tex
->fb_state
.zb_format
= r300_translate_zsformat(tex
->b
.b
.format
);
623 for (i
= 0; i
<= tex
->b
.b
.last_level
; i
++) {
624 tex
->fb_state
.colorpitch
[i
] =
626 r300_translate_colorformat(tex
->b
.b
.format
) |
627 R300_COLOR_TILE(tex
->mip_macrotile
[i
]) |
628 R300_COLOR_MICROTILE(tex
->microtile
);
630 tex
->fb_state
.us_out_fmt
= r300_translate_out_fmt(tex
->b
.b
.format
);
634 void r300_texture_reinterpret_format(struct pipe_screen
*screen
,
635 struct pipe_resource
*tex
,
636 enum pipe_format new_format
)
638 struct r300_screen
*r300screen
= r300_screen(screen
);
640 SCREEN_DBG(r300screen
, DBG_TEX
, "r300: Reinterpreting format: %s -> %s\n",
641 util_format_name(tex
->format
), util_format_name(new_format
));
643 tex
->format
= new_format
;
645 r300_texture_setup_fb_state(r300_screen(screen
), r300_texture(tex
));
648 unsigned r300_texture_get_offset(struct r300_texture
* tex
, unsigned level
,
649 unsigned zslice
, unsigned face
)
651 unsigned offset
= tex
->offset
[level
];
653 switch (tex
->b
.b
.target
) {
654 case PIPE_TEXTURE_3D
:
656 return offset
+ zslice
* tex
->layer_size
[level
];
658 case PIPE_TEXTURE_CUBE
:
660 return offset
+ face
* tex
->layer_size
[level
];
663 assert(zslice
== 0 && face
== 0);
669 * Return the width (dim==TILE_WIDTH) or height (dim==TILE_HEIGHT) of one tile
670 * of the given texture.
672 static unsigned r300_texture_get_tile_size(struct r300_texture
* tex
,
673 int dim
, boolean macrotile
)
675 unsigned pixsize
, tile_size
;
677 pixsize
= util_format_get_blocksize(tex
->b
.b
.format
);
678 tile_size
= microblock_table
[util_logbase2(pixsize
)][tex
->microtile
][dim
];
688 /* Return true if macrotiling should be enabled on the miplevel. */
689 static boolean
r300_texture_macro_switch(struct r300_texture
*tex
,
694 unsigned tile
, texdim
;
696 tile
= r300_texture_get_tile_size(tex
, dim
, TRUE
);
697 if (dim
== TILE_WIDTH
) {
698 texdim
= u_minify(tex
->b
.b
.width0
, level
);
700 texdim
= u_minify(tex
->b
.b
.height0
, level
);
703 /* See TX_FILTER1_n.MACRO_SWITCH. */
705 return texdim
>= tile
;
707 return texdim
> tile
;
712 * Return the stride, in bytes, of the texture images of the given texture
713 * at the given level.
715 unsigned r300_texture_get_stride(struct r300_screen
* screen
,
716 struct r300_texture
* tex
, unsigned level
)
718 unsigned tile_width
, width
;
720 if (tex
->stride_override
)
721 return tex
->stride_override
;
723 /* Check the level. */
724 if (level
> tex
->b
.b
.last_level
) {
725 SCREEN_DBG(screen
, DBG_TEX
, "%s: level (%u) > last_level (%u)\n",
726 __FUNCTION__
, level
, tex
->b
.b
.last_level
);
730 width
= u_minify(tex
->b
.b
.width0
, level
);
732 if (r300_format_is_plain(tex
->b
.b
.format
)) {
733 tile_width
= r300_texture_get_tile_size(tex
, TILE_WIDTH
,
734 tex
->mip_macrotile
[level
]);
735 width
= align(width
, tile_width
);
737 return util_format_get_stride(tex
->b
.b
.format
, width
);
739 return align(util_format_get_stride(tex
->b
.b
.format
, width
), 32);
743 static unsigned r300_texture_get_nblocksy(struct r300_texture
* tex
,
746 unsigned height
, tile_height
;
748 height
= u_minify(tex
->b
.b
.height0
, level
);
750 if (r300_format_is_plain(tex
->b
.b
.format
)) {
751 tile_height
= r300_texture_get_tile_size(tex
, TILE_HEIGHT
,
752 tex
->mip_macrotile
[level
]);
753 height
= align(height
, tile_height
);
755 /* This is needed for the kernel checker, unfortunately. */
756 height
= util_next_power_of_two(height
);
759 return util_format_get_nblocksy(tex
->b
.b
.format
, height
);
762 static void r300_texture_3d_fix_mipmapping(struct r300_screen
*screen
,
763 struct r300_texture
*tex
)
765 /* The kernels <= 2.6.34-rc3 compute the size of mipmapped 3D textures
766 * incorrectly. This is a workaround to prevent CS from being rejected. */
770 if (screen
->rws
->get_value(screen
->rws
, R300_VID_TEX3D_MIP_BUG
) &&
771 tex
->b
.b
.target
== PIPE_TEXTURE_3D
&&
772 tex
->b
.b
.last_level
> 0) {
775 for (i
= 0; i
<= tex
->b
.b
.last_level
; i
++) {
776 size
+= r300_texture_get_stride(screen
, tex
, i
) *
777 r300_texture_get_nblocksy(tex
, i
);
780 size
*= tex
->b
.b
.depth0
;
785 static void r300_setup_miptree(struct r300_screen
* screen
,
786 struct r300_texture
* tex
)
788 struct pipe_resource
* base
= &tex
->b
.b
;
789 unsigned stride
, size
, layer_size
, nblocksy
, i
;
790 boolean rv350_mode
= screen
->caps
.is_rv350
;
792 SCREEN_DBG(screen
, DBG_TEX
, "r300: Making miptree for texture, format %s\n",
793 util_format_name(base
->format
));
795 for (i
= 0; i
<= base
->last_level
; i
++) {
796 /* Let's see if this miplevel can be macrotiled. */
797 tex
->mip_macrotile
[i
] =
798 (tex
->macrotile
== R300_BUFFER_TILED
&&
799 r300_texture_macro_switch(tex
, i
, rv350_mode
, TILE_WIDTH
) &&
800 r300_texture_macro_switch(tex
, i
, rv350_mode
, TILE_HEIGHT
)) ?
801 R300_BUFFER_TILED
: R300_BUFFER_LINEAR
;
803 stride
= r300_texture_get_stride(screen
, tex
, i
);
804 nblocksy
= r300_texture_get_nblocksy(tex
, i
);
805 layer_size
= stride
* nblocksy
;
807 if (base
->target
== PIPE_TEXTURE_CUBE
)
808 size
= layer_size
* 6;
810 size
= layer_size
* u_minify(base
->depth0
, i
);
812 tex
->offset
[i
] = tex
->size
;
813 tex
->size
= tex
->offset
[i
] + size
;
814 tex
->layer_size
[i
] = layer_size
;
815 tex
->pitch
[i
] = stride
/ util_format_get_blocksize(base
->format
);
817 SCREEN_DBG(screen
, DBG_TEX
, "r300: Texture miptree: Level %d "
818 "(%dx%dx%d px, pitch %d bytes) %d bytes total, macrotiled %s\n",
819 i
, u_minify(base
->width0
, i
), u_minify(base
->height0
, i
),
820 u_minify(base
->depth0
, i
), stride
, tex
->size
,
821 tex
->mip_macrotile
[i
] ? "TRUE" : "FALSE");
825 static void r300_setup_flags(struct r300_texture
* tex
)
827 tex
->uses_pitch
= !util_is_power_of_two(tex
->b
.b
.width0
) ||
828 !util_is_power_of_two(tex
->b
.b
.height0
) ||
829 tex
->stride_override
;
832 static void r300_setup_tiling(struct pipe_screen
*screen
,
833 struct r300_texture
*tex
)
835 struct r300_winsys_screen
*rws
= (struct r300_winsys_screen
*)screen
->winsys
;
836 enum pipe_format format
= tex
->b
.b
.format
;
837 boolean rv350_mode
= r300_screen(screen
)->caps
.is_rv350
;
838 boolean is_zb
= util_format_is_depth_or_stencil(format
);
839 boolean dbg_no_tiling
= SCREEN_DBG_ON(r300_screen(screen
), DBG_NO_TILING
);
841 if (!r300_format_is_plain(format
)) {
845 /* If height == 1, disable microtiling except for zbuffer. */
846 if (!is_zb
&& (tex
->b
.b
.height0
== 1 || dbg_no_tiling
)) {
850 /* Set microtiling. */
851 switch (util_format_get_blocksize(format
)) {
854 tex
->microtile
= R300_BUFFER_TILED
;
859 if (rws
->get_value(rws
, R300_VID_SQUARE_TILING_SUPPORT
)) {
860 tex
->microtile
= R300_BUFFER_SQUARETILED
;
869 /* Set macrotiling. */
870 if (r300_texture_macro_switch(tex
, 0, rv350_mode
, TILE_WIDTH
) &&
871 r300_texture_macro_switch(tex
, 0, rv350_mode
, TILE_HEIGHT
)) {
872 tex
->macrotile
= R300_BUFFER_TILED
;
876 static unsigned r300_texture_is_referenced(struct pipe_context
*context
,
877 struct pipe_resource
*texture
,
878 unsigned face
, unsigned level
)
880 struct r300_context
*r300
= r300_context(context
);
881 struct r300_texture
*rtex
= (struct r300_texture
*)texture
;
883 if (r300
->rws
->is_buffer_referenced(r300
->rws
, rtex
->buffer
, R300_REF_CS
))
884 return PIPE_REFERENCED_FOR_READ
| PIPE_REFERENCED_FOR_WRITE
;
886 return PIPE_UNREFERENCED
;
889 static void r300_texture_destroy(struct pipe_screen
*screen
,
890 struct pipe_resource
* texture
)
892 struct r300_texture
* tex
= (struct r300_texture
*)texture
;
893 struct r300_winsys_screen
*rws
= (struct r300_winsys_screen
*)texture
->screen
->winsys
;
895 rws
->buffer_reference(rws
, &tex
->buffer
, NULL
);
899 static boolean
r300_texture_get_handle(struct pipe_screen
* screen
,
900 struct pipe_resource
*texture
,
901 struct winsys_handle
*whandle
)
903 struct r300_winsys_screen
*rws
= (struct r300_winsys_screen
*)screen
->winsys
;
904 struct r300_texture
* tex
= (struct r300_texture
*)texture
;
911 stride
= r300_texture_get_stride(r300_screen(screen
), tex
, 0);
913 rws
->buffer_get_handle(rws
, tex
->buffer
, stride
, whandle
);
918 struct u_resource_vtbl r300_texture_vtbl
=
920 r300_texture_get_handle
, /* get_handle */
921 r300_texture_destroy
, /* resource_destroy */
922 r300_texture_is_referenced
, /* is_resource_referenced */
923 r300_texture_get_transfer
, /* get_transfer */
924 r300_texture_transfer_destroy
, /* transfer_destroy */
925 r300_texture_transfer_map
, /* transfer_map */
926 u_default_transfer_flush_region
, /* transfer_flush_region */
927 r300_texture_transfer_unmap
, /* transfer_unmap */
928 u_default_transfer_inline_write
/* transfer_inline_write */
931 /* Create a new texture. */
932 struct pipe_resource
* r300_texture_create(struct pipe_screen
* screen
,
933 const struct pipe_resource
* base
)
935 struct r300_texture
* tex
= CALLOC_STRUCT(r300_texture
);
936 struct r300_screen
* rscreen
= r300_screen(screen
);
937 struct r300_winsys_screen
*rws
= (struct r300_winsys_screen
*)screen
->winsys
;
944 tex
->b
.vtbl
= &r300_texture_vtbl
;
945 pipe_reference_init(&tex
->b
.b
.reference
, 1);
946 tex
->b
.b
.screen
= screen
;
948 r300_setup_flags(tex
);
949 if (!(base
->flags
& R300_RESOURCE_FLAG_TRANSFER
) &&
950 !(base
->bind
& PIPE_BIND_SCANOUT
)) {
951 r300_setup_tiling(screen
, tex
);
953 r300_setup_miptree(rscreen
, tex
);
954 r300_texture_3d_fix_mipmapping(rscreen
, tex
);
955 r300_texture_setup_immutable_state(rscreen
, tex
);
956 r300_texture_setup_fb_state(rscreen
, tex
);
958 tex
->buffer
= rws
->buffer_create(rws
, 2048,
959 PIPE_BIND_SAMPLER_VIEW
, /* XXX */
961 rws
->buffer_set_tiling(rws
, tex
->buffer
,
971 return (struct pipe_resource
*)tex
;
974 /* Not required to implement u_resource_vtbl, consider moving to another file:
976 struct pipe_surface
* r300_get_tex_surface(struct pipe_screen
* screen
,
977 struct pipe_resource
* texture
,
983 struct r300_texture
* tex
= r300_texture(texture
);
984 struct pipe_surface
* surface
= CALLOC_STRUCT(pipe_surface
);
987 offset
= r300_texture_get_offset(tex
, level
, zslice
, face
);
990 pipe_reference_init(&surface
->reference
, 1);
991 pipe_resource_reference(&surface
->texture
, texture
);
992 surface
->format
= texture
->format
;
993 surface
->width
= u_minify(texture
->width0
, level
);
994 surface
->height
= u_minify(texture
->height0
, level
);
995 surface
->offset
= offset
;
996 surface
->usage
= flags
;
997 surface
->zslice
= zslice
;
998 surface
->texture
= texture
;
999 surface
->face
= face
;
1000 surface
->level
= level
;
1006 /* Not required to implement u_resource_vtbl, consider moving to another file:
1008 void r300_tex_surface_destroy(struct pipe_surface
* s
)
1010 pipe_resource_reference(&s
->texture
, NULL
);
1014 struct pipe_resource
*
1015 r300_texture_from_handle(struct pipe_screen
* screen
,
1016 const struct pipe_resource
* base
,
1017 struct winsys_handle
*whandle
)
1019 struct r300_winsys_screen
*rws
= (struct r300_winsys_screen
*)screen
->winsys
;
1020 struct r300_screen
* rscreen
= r300_screen(screen
);
1021 struct r300_winsys_buffer
*buffer
;
1022 struct r300_texture
* tex
;
1024 boolean override_zb_flags
;
1026 /* Support only 2D textures without mipmaps */
1027 if (base
->target
!= PIPE_TEXTURE_2D
||
1028 base
->depth0
!= 1 ||
1029 base
->last_level
!= 0) {
1033 buffer
= rws
->buffer_from_handle(rws
, screen
, whandle
, &stride
);
1038 tex
= CALLOC_STRUCT(r300_texture
);
1044 tex
->b
.vtbl
= &r300_texture_vtbl
;
1045 pipe_reference_init(&tex
->b
.b
.reference
, 1);
1046 tex
->b
.b
.screen
= screen
;
1048 tex
->stride_override
= stride
;
1050 /* one ref already taken */
1051 tex
->buffer
= buffer
;
1053 rws
->buffer_get_tiling(rws
, buffer
, &tex
->microtile
, &tex
->macrotile
);
1054 r300_setup_flags(tex
);
1056 /* Enforce microtiled zbuffer. */
1057 override_zb_flags
= util_format_is_depth_or_stencil(base
->format
) &&
1058 tex
->microtile
== R300_BUFFER_LINEAR
;
1060 if (override_zb_flags
) {
1061 switch (util_format_get_blocksize(base
->format
)) {
1063 tex
->microtile
= R300_BUFFER_TILED
;
1067 if (rws
->get_value(rws
, R300_VID_SQUARE_TILING_SUPPORT
)) {
1068 tex
->microtile
= R300_BUFFER_SQUARETILED
;
1074 override_zb_flags
= FALSE
;
1078 r300_setup_miptree(rscreen
, tex
);
1079 r300_texture_setup_immutable_state(rscreen
, tex
);
1080 r300_texture_setup_fb_state(rscreen
, tex
);
1082 if (override_zb_flags
) {
1083 rws
->buffer_set_tiling(rws
, tex
->buffer
,
1088 return (struct pipe_resource
*)tex
;