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"
40 static const unsigned microblock_table
[5][3][2] = {
41 /*linear tiled square-tiled */
42 {{32, 1}, {8, 4}, {0, 0}}, /* 8 bits per pixel */
43 {{16, 1}, {8, 2}, {4, 4}}, /* 16 bits per pixel */
44 {{ 8, 1}, {4, 2}, {0, 0}}, /* 32 bits per pixel */
45 {{ 4, 1}, {0, 0}, {2, 2}}, /* 64 bits per pixel */
46 {{ 2, 1}, {0, 0}, {0, 0}} /* 128 bits per pixel */
49 /* Return true for non-compressed and non-YUV formats. */
50 static boolean
r300_format_is_plain(enum pipe_format format
)
52 const struct util_format_description
*desc
= util_format_description(format
);
58 return desc
->layout
== UTIL_FORMAT_LAYOUT_PLAIN
;
61 /* Translate a pipe_format into a useful texture format for sampling.
63 * Some special formats are translated directly using R300_EASY_TX_FORMAT,
64 * but the majority of them is translated in a generic way, automatically
65 * supporting all the formats hw can support.
67 * R300_EASY_TX_FORMAT swizzles the texture.
68 * Note the signature of R300_EASY_TX_FORMAT:
69 * R300_EASY_TX_FORMAT(B, G, R, A, FORMAT);
71 * The FORMAT specifies how the texture sampler will treat the texture, and
72 * makes available X, Y, Z, W, ZERO, and ONE for swizzling. */
73 uint32_t r300_translate_texformat(enum pipe_format format
,
74 const unsigned char *swizzle_view
)
77 const struct util_format_description
*desc
;
79 boolean uniform
= TRUE
;
80 const uint32_t swizzle_shift
[4] = {
81 R300_TX_FORMAT_R_SHIFT
,
82 R300_TX_FORMAT_G_SHIFT
,
83 R300_TX_FORMAT_B_SHIFT
,
84 R300_TX_FORMAT_A_SHIFT
86 const uint32_t swizzle_bit
[4] = {
92 const uint32_t sign_bit
[4] = {
93 R300_TX_FORMAT_SIGNED_X
,
94 R300_TX_FORMAT_SIGNED_Y
,
95 R300_TX_FORMAT_SIGNED_Z
,
96 R300_TX_FORMAT_SIGNED_W
,
98 unsigned char swizzle
[4];
100 desc
= util_format_description(format
);
102 /* Colorspace (return non-RGB formats directly). */
103 switch (desc
->colorspace
) {
104 /* Depth stencil formats. */
105 case UTIL_FORMAT_COLORSPACE_ZS
:
107 case PIPE_FORMAT_Z16_UNORM
:
108 return R300_EASY_TX_FORMAT(X
, X
, X
, X
, X16
);
109 case PIPE_FORMAT_X8Z24_UNORM
:
110 case PIPE_FORMAT_S8_USCALED_Z24_UNORM
:
111 return R300_EASY_TX_FORMAT(X
, X
, X
, X
, W24_FP
);
113 return ~0; /* Unsupported. */
117 case UTIL_FORMAT_COLORSPACE_YUV
:
118 result
|= R300_TX_FORMAT_YUV_TO_RGB
;
121 case PIPE_FORMAT_UYVY
:
122 return R300_EASY_TX_FORMAT(X
, Y
, Z
, ONE
, YVYU422
) | result
;
123 case PIPE_FORMAT_YUYV
:
124 return R300_EASY_TX_FORMAT(X
, Y
, Z
, ONE
, VYUY422
) | result
;
126 return ~0; /* Unsupported/unknown. */
129 /* Add gamma correction. */
130 case UTIL_FORMAT_COLORSPACE_SRGB
:
131 result
|= R300_TX_FORMAT_GAMMA
;
136 /* Same as YUV but without the YUR->RGB conversion. */
137 case PIPE_FORMAT_R8G8_B8G8_UNORM
:
138 return R300_EASY_TX_FORMAT(X
, Y
, Z
, ONE
, YVYU422
) | result
;
139 case PIPE_FORMAT_G8R8_G8B8_UNORM
:
140 return R300_EASY_TX_FORMAT(X
, Y
, Z
, ONE
, VYUY422
) | result
;
147 /* Compose two sets of swizzles. */
148 for (i
= 0; i
< 4; i
++) {
149 swizzle
[i
] = swizzle_view
[i
] <= UTIL_FORMAT_SWIZZLE_W
?
150 desc
->swizzle
[swizzle_view
[i
]] : swizzle_view
[i
];
153 memcpy(swizzle
, desc
->swizzle
, sizeof(swizzle
));
157 for (i
= 0; i
< 4; i
++) {
158 switch (swizzle
[i
]) {
159 case UTIL_FORMAT_SWIZZLE_X
:
160 case UTIL_FORMAT_SWIZZLE_NONE
:
161 result
|= swizzle_bit
[0] << swizzle_shift
[i
];
163 case UTIL_FORMAT_SWIZZLE_Y
:
164 result
|= swizzle_bit
[1] << swizzle_shift
[i
];
166 case UTIL_FORMAT_SWIZZLE_Z
:
167 result
|= swizzle_bit
[2] << swizzle_shift
[i
];
169 case UTIL_FORMAT_SWIZZLE_W
:
170 result
|= swizzle_bit
[3] << swizzle_shift
[i
];
172 case UTIL_FORMAT_SWIZZLE_0
:
173 result
|= R300_TX_FORMAT_ZERO
<< swizzle_shift
[i
];
175 case UTIL_FORMAT_SWIZZLE_1
:
176 result
|= R300_TX_FORMAT_ONE
<< swizzle_shift
[i
];
179 return ~0; /* Unsupported. */
184 if (desc
->layout
== UTIL_FORMAT_LAYOUT_S3TC
) {
186 case PIPE_FORMAT_DXT1_RGB
:
187 case PIPE_FORMAT_DXT1_RGBA
:
188 case PIPE_FORMAT_DXT1_SRGB
:
189 case PIPE_FORMAT_DXT1_SRGBA
:
190 return R300_TX_FORMAT_DXT1
| result
;
191 case PIPE_FORMAT_DXT3_RGBA
:
192 case PIPE_FORMAT_DXT3_SRGBA
:
193 return R300_TX_FORMAT_DXT3
| result
;
194 case PIPE_FORMAT_DXT5_RGBA
:
195 case PIPE_FORMAT_DXT5_SRGBA
:
196 return R300_TX_FORMAT_DXT5
| result
;
198 return ~0; /* Unsupported/unknown. */
203 for (i
= 0; i
< desc
->nr_channels
; i
++) {
204 if (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_SIGNED
) {
205 result
|= sign_bit
[i
];
209 /* This is truly a special format.
210 * It stores R8G8 and B is computed using sqrt(1 - R^2 - G^2)
211 * in the sampler unit. Also known as D3DFMT_CxV8U8. */
212 if (format
== PIPE_FORMAT_R8G8Bx_SNORM
) {
213 return R300_TX_FORMAT_CxV8U8
| result
;
217 if (desc
->layout
== UTIL_FORMAT_LAYOUT_RGTC
) {
219 case PIPE_FORMAT_RGTC1_UNORM
:
220 case PIPE_FORMAT_RGTC1_SNORM
:
221 return R500_TX_FORMAT_ATI1N
| result
;
222 case PIPE_FORMAT_RGTC2_UNORM
:
223 case PIPE_FORMAT_RGTC2_SNORM
:
224 return R400_TX_FORMAT_ATI2N
| result
;
226 return ~0; /* Unsupported/unknown. */
230 /* See whether the components are of the same size. */
231 for (i
= 1; i
< desc
->nr_channels
; i
++) {
232 uniform
= uniform
&& desc
->channel
[0].size
== desc
->channel
[i
].size
;
235 /* Non-uniform formats. */
237 switch (desc
->nr_channels
) {
239 if (desc
->channel
[0].size
== 5 &&
240 desc
->channel
[1].size
== 6 &&
241 desc
->channel
[2].size
== 5) {
242 return R300_TX_FORMAT_Z5Y6X5
| result
;
244 if (desc
->channel
[0].size
== 5 &&
245 desc
->channel
[1].size
== 5 &&
246 desc
->channel
[2].size
== 6) {
247 return R300_TX_FORMAT_Z6Y5X5
| result
;
249 return ~0; /* Unsupported/unknown. */
252 if (desc
->channel
[0].size
== 5 &&
253 desc
->channel
[1].size
== 5 &&
254 desc
->channel
[2].size
== 5 &&
255 desc
->channel
[3].size
== 1) {
256 return R300_TX_FORMAT_W1Z5Y5X5
| result
;
258 if (desc
->channel
[0].size
== 10 &&
259 desc
->channel
[1].size
== 10 &&
260 desc
->channel
[2].size
== 10 &&
261 desc
->channel
[3].size
== 2) {
262 return R300_TX_FORMAT_W2Z10Y10X10
| result
;
265 return ~0; /* Unsupported/unknown. */
268 /* And finally, uniform formats. */
269 switch (desc
->channel
[0].type
) {
270 case UTIL_FORMAT_TYPE_UNSIGNED
:
271 case UTIL_FORMAT_TYPE_SIGNED
:
272 if (!desc
->channel
[0].normalized
&&
273 desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_SRGB
) {
277 switch (desc
->channel
[0].size
) {
279 switch (desc
->nr_channels
) {
281 return R300_TX_FORMAT_Y4X4
| result
;
283 return R300_TX_FORMAT_W4Z4Y4X4
| result
;
288 switch (desc
->nr_channels
) {
290 return R300_TX_FORMAT_X8
| result
;
292 return R300_TX_FORMAT_Y8X8
| result
;
294 return R300_TX_FORMAT_W8Z8Y8X8
| result
;
299 switch (desc
->nr_channels
) {
301 return R300_TX_FORMAT_X16
| result
;
303 return R300_TX_FORMAT_Y16X16
| result
;
305 return R300_TX_FORMAT_W16Z16Y16X16
| result
;
310 case UTIL_FORMAT_TYPE_FLOAT
:
311 switch (desc
->channel
[0].size
) {
313 switch (desc
->nr_channels
) {
315 return R300_TX_FORMAT_16F
| result
;
317 return R300_TX_FORMAT_16F_16F
| result
;
319 return R300_TX_FORMAT_16F_16F_16F_16F
| result
;
324 switch (desc
->nr_channels
) {
326 return R300_TX_FORMAT_32F
| result
;
328 return R300_TX_FORMAT_32F_32F
| result
;
330 return R300_TX_FORMAT_32F_32F_32F_32F
| result
;
335 return ~0; /* Unsupported/unknown. */
338 uint32_t r500_tx_format_msb_bit(enum pipe_format format
)
341 case PIPE_FORMAT_RGTC1_UNORM
:
342 case PIPE_FORMAT_RGTC1_SNORM
:
343 return R500_TXFORMAT_MSB
;
349 /* Buffer formats. */
351 /* Colorbuffer formats. This is the unswizzled format of the RB3D block's
352 * output. For the swizzling of the targets, check the shader's format. */
353 static uint32_t r300_translate_colorformat(enum pipe_format format
)
357 case PIPE_FORMAT_A8_UNORM
:
358 case PIPE_FORMAT_I8_UNORM
:
359 case PIPE_FORMAT_L8_UNORM
:
360 case PIPE_FORMAT_R8_UNORM
:
361 case PIPE_FORMAT_R8_SNORM
:
362 return R300_COLOR_FORMAT_I8
;
364 /* 16-bit buffers. */
365 case PIPE_FORMAT_B5G6R5_UNORM
:
366 return R300_COLOR_FORMAT_RGB565
;
368 case PIPE_FORMAT_B5G5R5A1_UNORM
:
369 case PIPE_FORMAT_B5G5R5X1_UNORM
:
370 return R300_COLOR_FORMAT_ARGB1555
;
372 case PIPE_FORMAT_B4G4R4A4_UNORM
:
373 case PIPE_FORMAT_B4G4R4X4_UNORM
:
374 return R300_COLOR_FORMAT_ARGB4444
;
376 /* 32-bit buffers. */
377 case PIPE_FORMAT_B8G8R8A8_UNORM
:
378 case PIPE_FORMAT_B8G8R8X8_UNORM
:
379 case PIPE_FORMAT_A8R8G8B8_UNORM
:
380 case PIPE_FORMAT_X8R8G8B8_UNORM
:
381 case PIPE_FORMAT_A8B8G8R8_UNORM
:
382 case PIPE_FORMAT_R8G8B8A8_SNORM
:
383 case PIPE_FORMAT_X8B8G8R8_UNORM
:
384 case PIPE_FORMAT_R8G8B8X8_UNORM
:
385 case PIPE_FORMAT_R8SG8SB8UX8U_NORM
:
386 return R300_COLOR_FORMAT_ARGB8888
;
388 case PIPE_FORMAT_R10G10B10A2_UNORM
:
389 case PIPE_FORMAT_R10G10B10X2_SNORM
:
390 case PIPE_FORMAT_B10G10R10A2_UNORM
:
391 case PIPE_FORMAT_R10SG10SB10SA2U_NORM
:
392 return R500_COLOR_FORMAT_ARGB2101010
; /* R5xx-only? */
394 /* 64-bit buffers. */
395 case PIPE_FORMAT_R16G16B16A16_UNORM
:
396 case PIPE_FORMAT_R16G16B16A16_SNORM
:
397 case PIPE_FORMAT_R16G16B16A16_FLOAT
:
398 return R300_COLOR_FORMAT_ARGB16161616
;
400 /* 128-bit buffers. */
401 case PIPE_FORMAT_R32G32B32A32_FLOAT
:
402 return R300_COLOR_FORMAT_ARGB32323232
;
405 case PIPE_FORMAT_UYVY
:
406 return R300_COLOR_FORMAT_YVYU
;
407 case PIPE_FORMAT_YUYV
:
408 return R300_COLOR_FORMAT_VYUY
;
410 return ~0; /* Unsupported. */
414 /* Depthbuffer and stencilbuffer. Thankfully, we only support two flavors. */
415 static uint32_t r300_translate_zsformat(enum pipe_format format
)
418 /* 16-bit depth, no stencil */
419 case PIPE_FORMAT_Z16_UNORM
:
420 return R300_DEPTHFORMAT_16BIT_INT_Z
;
421 /* 24-bit depth, ignored stencil */
422 case PIPE_FORMAT_X8Z24_UNORM
:
423 /* 24-bit depth, 8-bit stencil */
424 case PIPE_FORMAT_S8_USCALED_Z24_UNORM
:
425 return R300_DEPTHFORMAT_24BIT_INT_Z_8BIT_STENCIL
;
427 return ~0; /* Unsupported. */
431 /* Shader output formats. This is essentially the swizzle from the shader
434 * Note that formats are stored from C3 to C0. */
435 static uint32_t r300_translate_out_fmt(enum pipe_format format
)
437 uint32_t modifier
= 0;
439 const struct util_format_description
*desc
;
440 static const uint32_t sign_bit
[4] = {
447 desc
= util_format_description(format
);
449 /* Specifies how the shader output is written to the fog unit. */
450 if (desc
->channel
[0].type
== UTIL_FORMAT_TYPE_FLOAT
) {
451 if (desc
->channel
[0].size
== 32) {
452 modifier
|= R300_US_OUT_FMT_C4_32_FP
;
454 modifier
|= R300_US_OUT_FMT_C4_16_FP
;
457 if (desc
->channel
[0].size
== 16) {
458 modifier
|= R300_US_OUT_FMT_C4_16
;
460 /* C4_8 seems to be used for the formats whose pixel size
462 modifier
|= R300_US_OUT_FMT_C4_8
;
467 for (i
= 0; i
< 4; i
++)
468 if (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_SIGNED
) {
469 modifier
|= sign_bit
[i
];
472 /* Add swizzles and return. */
475 * COLORFORMAT_I8 stores the C2 component. */
476 case PIPE_FORMAT_A8_UNORM
:
477 return modifier
| R300_C2_SEL_A
;
478 case PIPE_FORMAT_I8_UNORM
:
479 case PIPE_FORMAT_L8_UNORM
:
480 case PIPE_FORMAT_R8_UNORM
:
481 case PIPE_FORMAT_R8_SNORM
:
482 return modifier
| R300_C2_SEL_R
;
485 case PIPE_FORMAT_B5G6R5_UNORM
:
486 case PIPE_FORMAT_B5G5R5A1_UNORM
:
487 case PIPE_FORMAT_B5G5R5X1_UNORM
:
488 case PIPE_FORMAT_B4G4R4A4_UNORM
:
489 case PIPE_FORMAT_B4G4R4X4_UNORM
:
490 case PIPE_FORMAT_B8G8R8A8_UNORM
:
491 case PIPE_FORMAT_B8G8R8X8_UNORM
:
492 case PIPE_FORMAT_B10G10R10A2_UNORM
:
494 R300_C0_SEL_B
| R300_C1_SEL_G
|
495 R300_C2_SEL_R
| R300_C3_SEL_A
;
498 case PIPE_FORMAT_A8R8G8B8_UNORM
:
499 case PIPE_FORMAT_X8R8G8B8_UNORM
:
501 R300_C0_SEL_A
| R300_C1_SEL_R
|
502 R300_C2_SEL_G
| R300_C3_SEL_B
;
505 case PIPE_FORMAT_A8B8G8R8_UNORM
:
506 case PIPE_FORMAT_X8B8G8R8_UNORM
:
508 R300_C0_SEL_A
| R300_C1_SEL_B
|
509 R300_C2_SEL_G
| R300_C3_SEL_R
;
512 case PIPE_FORMAT_R8G8B8X8_UNORM
:
513 case PIPE_FORMAT_R8G8B8A8_SNORM
:
514 case PIPE_FORMAT_R8SG8SB8UX8U_NORM
:
515 case PIPE_FORMAT_R10G10B10A2_UNORM
:
516 case PIPE_FORMAT_R10G10B10X2_SNORM
:
517 case PIPE_FORMAT_R10SG10SB10SA2U_NORM
:
518 case PIPE_FORMAT_R16G16B16A16_UNORM
:
519 case PIPE_FORMAT_R16G16B16A16_SNORM
:
520 case PIPE_FORMAT_R16G16B16A16_FLOAT
:
521 case PIPE_FORMAT_R32G32B32A32_FLOAT
:
523 R300_C0_SEL_R
| R300_C1_SEL_G
|
524 R300_C2_SEL_B
| R300_C3_SEL_A
;
527 return ~0; /* Unsupported. */
531 boolean
r300_is_colorbuffer_format_supported(enum pipe_format format
)
533 return r300_translate_colorformat(format
) != ~0 &&
534 r300_translate_out_fmt(format
) != ~0;
537 boolean
r300_is_zs_format_supported(enum pipe_format format
)
539 return r300_translate_zsformat(format
) != ~0;
542 boolean
r300_is_sampler_format_supported(enum pipe_format format
)
544 return r300_translate_texformat(format
, 0) != ~0;
547 static void r300_texture_setup_immutable_state(struct r300_screen
* screen
,
548 struct r300_texture
* tex
)
550 struct r300_texture_format_state
* f
= &tex
->tx_format
;
551 struct pipe_resource
*pt
= &tex
->b
.b
;
552 boolean is_r500
= screen
->caps
.is_r500
;
554 /* Set sampler state. */
555 f
->format0
= R300_TX_WIDTH((pt
->width0
- 1) & 0x7ff) |
556 R300_TX_HEIGHT((pt
->height0
- 1) & 0x7ff);
558 if (tex
->uses_pitch
) {
559 /* rectangles love this */
560 f
->format0
|= R300_TX_PITCH_EN
;
561 f
->format2
= (tex
->hwpitch
[0] - 1) & 0x1fff;
563 /* power of two textures (3D, mipmaps, and no pitch) */
564 f
->format0
|= R300_TX_DEPTH(util_logbase2(pt
->depth0
) & 0xf);
568 if (pt
->target
== PIPE_TEXTURE_CUBE
) {
569 f
->format1
|= R300_TX_FORMAT_CUBIC_MAP
;
571 if (pt
->target
== PIPE_TEXTURE_3D
) {
572 f
->format1
|= R300_TX_FORMAT_3D
;
575 /* large textures on r500 */
578 if (pt
->width0
> 2048) {
579 f
->format2
|= R500_TXWIDTH_BIT11
;
581 if (pt
->height0
> 2048) {
582 f
->format2
|= R500_TXHEIGHT_BIT11
;
586 f
->tile_config
= R300_TXO_MACRO_TILE(tex
->macrotile
) |
587 R300_TXO_MICRO_TILE(tex
->microtile
);
589 SCREEN_DBG(screen
, DBG_TEX
, "r300: Set texture state (%dx%d, %d levels)\n",
590 pt
->width0
, pt
->height0
, pt
->last_level
);
593 static void r300_texture_setup_fb_state(struct r300_screen
* screen
,
594 struct r300_texture
* tex
)
598 /* Set framebuffer state. */
599 if (util_format_is_depth_or_stencil(tex
->b
.b
.format
)) {
600 for (i
= 0; i
<= tex
->b
.b
.last_level
; i
++) {
601 tex
->fb_state
.depthpitch
[i
] =
603 R300_DEPTHMACROTILE(tex
->mip_macrotile
[i
]) |
604 R300_DEPTHMICROTILE(tex
->microtile
);
606 tex
->fb_state
.zb_format
= r300_translate_zsformat(tex
->b
.b
.format
);
608 for (i
= 0; i
<= tex
->b
.b
.last_level
; i
++) {
609 tex
->fb_state
.colorpitch
[i
] =
611 r300_translate_colorformat(tex
->b
.b
.format
) |
612 R300_COLOR_TILE(tex
->mip_macrotile
[i
]) |
613 R300_COLOR_MICROTILE(tex
->microtile
);
615 tex
->fb_state
.us_out_fmt
= r300_translate_out_fmt(tex
->b
.b
.format
);
619 void r300_texture_reinterpret_format(struct pipe_screen
*screen
,
620 struct pipe_resource
*tex
,
621 enum pipe_format new_format
)
623 struct r300_screen
*r300screen
= r300_screen(screen
);
625 SCREEN_DBG(r300screen
, DBG_TEX
, "r300: Reinterpreting format: %s -> %s\n",
626 util_format_name(tex
->format
), util_format_name(new_format
));
628 tex
->format
= new_format
;
630 r300_texture_setup_fb_state(r300_screen(screen
), r300_texture(tex
));
633 unsigned r300_texture_get_offset(struct r300_texture
* tex
, unsigned level
,
634 unsigned zslice
, unsigned face
)
636 unsigned offset
= tex
->offset
[level
];
638 switch (tex
->b
.b
.target
) {
639 case PIPE_TEXTURE_3D
:
641 return offset
+ zslice
* tex
->layer_size
[level
];
643 case PIPE_TEXTURE_CUBE
:
645 return offset
+ face
* tex
->layer_size
[level
];
648 assert(zslice
== 0 && face
== 0);
654 * Return the width (dim==TILE_WIDTH) or height (dim==TILE_HEIGHT) of one tile
655 * of the given texture.
657 static unsigned r300_texture_get_tile_size(struct r300_texture
* tex
,
658 int dim
, boolean macrotile
)
660 unsigned pixsize
, tile_size
;
662 pixsize
= util_format_get_blocksize(tex
->b
.b
.format
);
663 tile_size
= microblock_table
[util_logbase2(pixsize
)][tex
->microtile
][dim
];
673 /* Return true if macrotiling should be enabled on the miplevel. */
674 static boolean
r300_texture_macro_switch(struct r300_texture
*tex
,
679 unsigned tile
, texdim
;
681 tile
= r300_texture_get_tile_size(tex
, dim
, TRUE
);
682 if (dim
== TILE_WIDTH
) {
683 texdim
= u_minify(tex
->b
.b
.width0
, level
);
685 texdim
= u_minify(tex
->b
.b
.height0
, level
);
688 /* See TX_FILTER1_n.MACRO_SWITCH. */
690 return texdim
>= tile
;
692 return texdim
> tile
;
697 * Return the stride, in bytes, of the texture images of the given texture
698 * at the given level.
700 unsigned r300_texture_get_stride(struct r300_screen
* screen
,
701 struct r300_texture
* tex
, unsigned level
)
703 unsigned tile_width
, width
;
705 if (tex
->stride_override
)
706 return tex
->stride_override
;
708 /* Check the level. */
709 if (level
> tex
->b
.b
.last_level
) {
710 SCREEN_DBG(screen
, DBG_TEX
, "%s: level (%u) > last_level (%u)\n",
711 __FUNCTION__
, level
, tex
->b
.b
.last_level
);
715 width
= u_minify(tex
->b
.b
.width0
, level
);
717 if (r300_format_is_plain(tex
->b
.b
.format
)) {
718 tile_width
= r300_texture_get_tile_size(tex
, TILE_WIDTH
,
719 tex
->mip_macrotile
[level
]);
720 width
= align(width
, tile_width
);
722 return util_format_get_stride(tex
->b
.b
.format
, width
);
724 return align(util_format_get_stride(tex
->b
.b
.format
, width
), 32);
728 static unsigned r300_texture_get_nblocksy(struct r300_texture
* tex
,
731 unsigned height
, tile_height
;
733 height
= u_minify(tex
->b
.b
.height0
, level
);
735 if (r300_format_is_plain(tex
->b
.b
.format
)) {
736 tile_height
= r300_texture_get_tile_size(tex
, TILE_HEIGHT
,
737 tex
->mip_macrotile
[level
]);
738 height
= align(height
, tile_height
);
740 /* This is needed for the kernel checker, unfortunately. */
741 height
= util_next_power_of_two(height
);
744 return util_format_get_nblocksy(tex
->b
.b
.format
, height
);
747 static void r300_texture_3d_fix_mipmapping(struct r300_screen
*screen
,
748 struct r300_texture
*tex
)
750 /* The kernels <= 2.6.34-rc4 compute the size of mipmapped 3D textures
751 * incorrectly. This is a workaround to prevent CS from being rejected. */
755 if (!screen
->rws
->get_value(screen
->rws
, R300_VID_DRM_2_3_0
) &&
756 tex
->b
.b
.target
== PIPE_TEXTURE_3D
&&
757 tex
->b
.b
.last_level
> 0) {
760 for (i
= 0; i
<= tex
->b
.b
.last_level
; i
++) {
761 size
+= r300_texture_get_stride(screen
, tex
, i
) *
762 r300_texture_get_nblocksy(tex
, i
);
765 size
*= tex
->b
.b
.depth0
;
770 static void r300_setup_miptree(struct r300_screen
* screen
,
771 struct r300_texture
* tex
)
773 struct pipe_resource
* base
= &tex
->b
.b
;
774 unsigned stride
, size
, layer_size
, nblocksy
, i
;
775 boolean rv350_mode
= screen
->caps
.is_rv350
;
777 SCREEN_DBG(screen
, DBG_TEX
, "r300: Making miptree for texture, format %s\n",
778 util_format_name(base
->format
));
780 for (i
= 0; i
<= base
->last_level
; i
++) {
781 /* Let's see if this miplevel can be macrotiled. */
782 tex
->mip_macrotile
[i
] =
783 (tex
->macrotile
== R300_BUFFER_TILED
&&
784 r300_texture_macro_switch(tex
, i
, rv350_mode
, TILE_WIDTH
) &&
785 r300_texture_macro_switch(tex
, i
, rv350_mode
, TILE_HEIGHT
)) ?
786 R300_BUFFER_TILED
: R300_BUFFER_LINEAR
;
788 stride
= r300_texture_get_stride(screen
, tex
, i
);
789 nblocksy
= r300_texture_get_nblocksy(tex
, i
);
790 layer_size
= stride
* nblocksy
;
792 if (base
->target
== PIPE_TEXTURE_CUBE
)
793 size
= layer_size
* 6;
795 size
= layer_size
* u_minify(base
->depth0
, i
);
797 tex
->offset
[i
] = tex
->size
;
798 tex
->size
= tex
->offset
[i
] + size
;
799 tex
->layer_size
[i
] = layer_size
;
800 tex
->pitch
[i
] = stride
/ util_format_get_blocksize(base
->format
);
802 tex
->pitch
[i
] * util_format_get_blockwidth(base
->format
);
804 SCREEN_DBG(screen
, DBG_TEX
, "r300: Texture miptree: Level %d "
805 "(%dx%dx%d px, pitch %d bytes) %d bytes total, macrotiled %s\n",
806 i
, u_minify(base
->width0
, i
), u_minify(base
->height0
, i
),
807 u_minify(base
->depth0
, i
), stride
, tex
->size
,
808 tex
->mip_macrotile
[i
] ? "TRUE" : "FALSE");
812 static void r300_setup_flags(struct r300_texture
* tex
)
814 tex
->uses_pitch
= !util_is_power_of_two(tex
->b
.b
.width0
) ||
815 !util_is_power_of_two(tex
->b
.b
.height0
) ||
816 tex
->stride_override
;
819 static void r300_setup_tiling(struct pipe_screen
*screen
,
820 struct r300_texture
*tex
)
822 struct r300_winsys_screen
*rws
= (struct r300_winsys_screen
*)screen
->winsys
;
823 enum pipe_format format
= tex
->b
.b
.format
;
824 boolean rv350_mode
= r300_screen(screen
)->caps
.is_rv350
;
825 boolean is_zb
= util_format_is_depth_or_stencil(format
);
826 boolean dbg_no_tiling
= SCREEN_DBG_ON(r300_screen(screen
), DBG_NO_TILING
);
828 if (!r300_format_is_plain(format
)) {
832 /* If height == 1, disable microtiling except for zbuffer. */
833 if (!is_zb
&& (tex
->b
.b
.height0
== 1 || dbg_no_tiling
)) {
837 /* Set microtiling. */
838 switch (util_format_get_blocksize(format
)) {
841 tex
->microtile
= R300_BUFFER_TILED
;
846 if (rws
->get_value(rws
, R300_VID_SQUARE_TILING_SUPPORT
)) {
847 tex
->microtile
= R300_BUFFER_SQUARETILED
;
856 /* Set macrotiling. */
857 if (r300_texture_macro_switch(tex
, 0, rv350_mode
, TILE_WIDTH
) &&
858 r300_texture_macro_switch(tex
, 0, rv350_mode
, TILE_HEIGHT
)) {
859 tex
->macrotile
= R300_BUFFER_TILED
;
863 static unsigned r300_texture_is_referenced(struct pipe_context
*context
,
864 struct pipe_resource
*texture
,
865 unsigned face
, unsigned level
)
867 struct r300_context
*r300
= r300_context(context
);
868 struct r300_texture
*rtex
= (struct r300_texture
*)texture
;
870 if (r300
->rws
->is_buffer_referenced(r300
->rws
, rtex
->buffer
, R300_REF_CS
))
871 return PIPE_REFERENCED_FOR_READ
| PIPE_REFERENCED_FOR_WRITE
;
873 return PIPE_UNREFERENCED
;
876 static void r300_texture_destroy(struct pipe_screen
*screen
,
877 struct pipe_resource
* texture
)
879 struct r300_texture
* tex
= (struct r300_texture
*)texture
;
880 struct r300_winsys_screen
*rws
= (struct r300_winsys_screen
*)texture
->screen
->winsys
;
882 rws
->buffer_reference(rws
, &tex
->buffer
, NULL
);
886 static boolean
r300_texture_get_handle(struct pipe_screen
* screen
,
887 struct pipe_resource
*texture
,
888 struct winsys_handle
*whandle
)
890 struct r300_winsys_screen
*rws
= (struct r300_winsys_screen
*)screen
->winsys
;
891 struct r300_texture
* tex
= (struct r300_texture
*)texture
;
898 stride
= r300_texture_get_stride(r300_screen(screen
), tex
, 0);
900 rws
->buffer_get_handle(rws
, tex
->buffer
, stride
, whandle
);
905 struct u_resource_vtbl r300_texture_vtbl
=
907 r300_texture_get_handle
, /* get_handle */
908 r300_texture_destroy
, /* resource_destroy */
909 r300_texture_is_referenced
, /* is_resource_referenced */
910 r300_texture_get_transfer
, /* get_transfer */
911 r300_texture_transfer_destroy
, /* transfer_destroy */
912 r300_texture_transfer_map
, /* transfer_map */
913 u_default_transfer_flush_region
, /* transfer_flush_region */
914 r300_texture_transfer_unmap
, /* transfer_unmap */
915 u_default_transfer_inline_write
/* transfer_inline_write */
918 /* Create a new texture. */
919 struct pipe_resource
* r300_texture_create(struct pipe_screen
* screen
,
920 const struct pipe_resource
* base
)
922 struct r300_texture
* tex
= CALLOC_STRUCT(r300_texture
);
923 struct r300_screen
* rscreen
= r300_screen(screen
);
924 struct r300_winsys_screen
*rws
= (struct r300_winsys_screen
*)screen
->winsys
;
931 tex
->b
.vtbl
= &r300_texture_vtbl
;
932 pipe_reference_init(&tex
->b
.b
.reference
, 1);
933 tex
->b
.b
.screen
= screen
;
935 r300_setup_flags(tex
);
936 if (!(base
->flags
& R300_RESOURCE_FLAG_TRANSFER
) &&
937 !(base
->bind
& PIPE_BIND_SCANOUT
)) {
938 r300_setup_tiling(screen
, tex
);
940 r300_setup_miptree(rscreen
, tex
);
941 r300_texture_3d_fix_mipmapping(rscreen
, tex
);
942 r300_texture_setup_immutable_state(rscreen
, tex
);
943 r300_texture_setup_fb_state(rscreen
, tex
);
945 tex
->buffer
= rws
->buffer_create(rws
, 2048,
946 PIPE_BIND_SAMPLER_VIEW
, /* XXX */
948 rws
->buffer_set_tiling(rws
, tex
->buffer
,
958 return (struct pipe_resource
*)tex
;
961 /* Not required to implement u_resource_vtbl, consider moving to another file:
963 struct pipe_surface
* r300_get_tex_surface(struct pipe_screen
* screen
,
964 struct pipe_resource
* texture
,
970 struct r300_texture
* tex
= r300_texture(texture
);
971 struct pipe_surface
* surface
= CALLOC_STRUCT(pipe_surface
);
974 offset
= r300_texture_get_offset(tex
, level
, zslice
, face
);
977 pipe_reference_init(&surface
->reference
, 1);
978 pipe_resource_reference(&surface
->texture
, texture
);
979 surface
->format
= texture
->format
;
980 surface
->width
= u_minify(texture
->width0
, level
);
981 surface
->height
= u_minify(texture
->height0
, level
);
982 surface
->offset
= offset
;
983 surface
->usage
= flags
;
984 surface
->zslice
= zslice
;
985 surface
->texture
= texture
;
986 surface
->face
= face
;
987 surface
->level
= level
;
993 /* Not required to implement u_resource_vtbl, consider moving to another file:
995 void r300_tex_surface_destroy(struct pipe_surface
* s
)
997 pipe_resource_reference(&s
->texture
, NULL
);
1001 struct pipe_resource
*
1002 r300_texture_from_handle(struct pipe_screen
* screen
,
1003 const struct pipe_resource
* base
,
1004 struct winsys_handle
*whandle
)
1006 struct r300_winsys_screen
*rws
= (struct r300_winsys_screen
*)screen
->winsys
;
1007 struct r300_screen
* rscreen
= r300_screen(screen
);
1008 struct r300_winsys_buffer
*buffer
;
1009 struct r300_texture
* tex
;
1011 boolean override_zb_flags
;
1013 /* Support only 2D textures without mipmaps */
1014 if (base
->target
!= PIPE_TEXTURE_2D
||
1015 base
->depth0
!= 1 ||
1016 base
->last_level
!= 0) {
1020 buffer
= rws
->buffer_from_handle(rws
, screen
, whandle
, &stride
);
1025 tex
= CALLOC_STRUCT(r300_texture
);
1031 tex
->b
.vtbl
= &r300_texture_vtbl
;
1032 pipe_reference_init(&tex
->b
.b
.reference
, 1);
1033 tex
->b
.b
.screen
= screen
;
1035 tex
->stride_override
= stride
;
1037 /* one ref already taken */
1038 tex
->buffer
= buffer
;
1040 rws
->buffer_get_tiling(rws
, buffer
, &tex
->microtile
, &tex
->macrotile
);
1041 r300_setup_flags(tex
);
1043 /* Enforce microtiled zbuffer. */
1044 override_zb_flags
= util_format_is_depth_or_stencil(base
->format
) &&
1045 tex
->microtile
== R300_BUFFER_LINEAR
;
1047 if (override_zb_flags
) {
1048 switch (util_format_get_blocksize(base
->format
)) {
1050 tex
->microtile
= R300_BUFFER_TILED
;
1054 if (rws
->get_value(rws
, R300_VID_SQUARE_TILING_SUPPORT
)) {
1055 tex
->microtile
= R300_BUFFER_SQUARETILED
;
1061 override_zb_flags
= FALSE
;
1065 r300_setup_miptree(rscreen
, tex
);
1066 r300_texture_setup_immutable_state(rscreen
, tex
);
1067 r300_texture_setup_fb_state(rscreen
, tex
);
1069 if (override_zb_flags
) {
1070 rws
->buffer_set_tiling(rws
, tex
->buffer
,
1075 return (struct pipe_resource
*)tex
;