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_format_s3tc.h"
28 #include "util/u_math.h"
29 #include "util/u_memory.h"
31 #include "r300_context.h"
33 #include "r300_texture.h"
34 #include "r300_transfer.h"
35 #include "r300_screen.h"
36 #include "r300_winsys.h"
41 static const unsigned microblock_table
[5][3][2] = {
42 /*linear tiled square-tiled */
43 {{32, 1}, {8, 4}, {0, 0}}, /* 8 bits per pixel */
44 {{16, 1}, {8, 2}, {4, 4}}, /* 16 bits per pixel */
45 {{ 8, 1}, {4, 2}, {0, 0}}, /* 32 bits per pixel */
46 {{ 4, 1}, {0, 0}, {2, 2}}, /* 64 bits per pixel */
47 {{ 2, 1}, {0, 0}, {0, 0}} /* 128 bits per pixel */
50 unsigned r300_get_swizzle_combined(const unsigned char *swizzle_format
,
51 const unsigned char *swizzle_view
)
54 unsigned char swizzle
[4];
56 const uint32_t swizzle_shift
[4] = {
57 R300_TX_FORMAT_R_SHIFT
,
58 R300_TX_FORMAT_G_SHIFT
,
59 R300_TX_FORMAT_B_SHIFT
,
60 R300_TX_FORMAT_A_SHIFT
62 const uint32_t swizzle_bit
[4] = {
70 /* Combine two sets of swizzles. */
71 for (i
= 0; i
< 4; i
++) {
72 swizzle
[i
] = swizzle_view
[i
] <= UTIL_FORMAT_SWIZZLE_W
?
73 swizzle_format
[swizzle_view
[i
]] : swizzle_view
[i
];
76 memcpy(swizzle
, swizzle_format
, 4);
80 for (i
= 0; i
< 4; i
++) {
82 case UTIL_FORMAT_SWIZZLE_Y
:
83 result
|= swizzle_bit
[1] << swizzle_shift
[i
];
85 case UTIL_FORMAT_SWIZZLE_Z
:
86 result
|= swizzle_bit
[2] << swizzle_shift
[i
];
88 case UTIL_FORMAT_SWIZZLE_W
:
89 result
|= swizzle_bit
[3] << swizzle_shift
[i
];
91 case UTIL_FORMAT_SWIZZLE_0
:
92 result
|= R300_TX_FORMAT_ZERO
<< swizzle_shift
[i
];
94 case UTIL_FORMAT_SWIZZLE_1
:
95 result
|= R300_TX_FORMAT_ONE
<< swizzle_shift
[i
];
97 default: /* UTIL_FORMAT_SWIZZLE_X */
98 result
|= swizzle_bit
[0] << swizzle_shift
[i
];
104 /* Translate a pipe_format into a useful texture format for sampling.
106 * Some special formats are translated directly using R300_EASY_TX_FORMAT,
107 * but the majority of them is translated in a generic way, automatically
108 * supporting all the formats hw can support.
110 * R300_EASY_TX_FORMAT swizzles the texture.
111 * Note the signature of R300_EASY_TX_FORMAT:
112 * R300_EASY_TX_FORMAT(B, G, R, A, FORMAT);
114 * The FORMAT specifies how the texture sampler will treat the texture, and
115 * makes available X, Y, Z, W, ZERO, and ONE for swizzling. */
116 uint32_t r300_translate_texformat(enum pipe_format format
,
117 const unsigned char *swizzle_view
)
120 const struct util_format_description
*desc
;
122 boolean uniform
= TRUE
;
123 const uint32_t sign_bit
[4] = {
124 R300_TX_FORMAT_SIGNED_X
,
125 R300_TX_FORMAT_SIGNED_Y
,
126 R300_TX_FORMAT_SIGNED_Z
,
127 R300_TX_FORMAT_SIGNED_W
,
130 desc
= util_format_description(format
);
132 /* Colorspace (return non-RGB formats directly). */
133 switch (desc
->colorspace
) {
134 /* Depth stencil formats.
135 * Swizzles are added in r300_merge_textures_and_samplers. */
136 case UTIL_FORMAT_COLORSPACE_ZS
:
138 case PIPE_FORMAT_Z16_UNORM
:
139 return R300_TX_FORMAT_X16
;
140 case PIPE_FORMAT_X8Z24_UNORM
:
141 case PIPE_FORMAT_S8_USCALED_Z24_UNORM
:
142 return R500_TX_FORMAT_Y8X24
;
144 return ~0; /* Unsupported. */
148 case UTIL_FORMAT_COLORSPACE_YUV
:
149 result
|= R300_TX_FORMAT_YUV_TO_RGB
;
152 case PIPE_FORMAT_UYVY
:
153 return R300_EASY_TX_FORMAT(X
, Y
, Z
, ONE
, YVYU422
) | result
;
154 case PIPE_FORMAT_YUYV
:
155 return R300_EASY_TX_FORMAT(X
, Y
, Z
, ONE
, VYUY422
) | result
;
157 return ~0; /* Unsupported/unknown. */
160 /* Add gamma correction. */
161 case UTIL_FORMAT_COLORSPACE_SRGB
:
162 result
|= R300_TX_FORMAT_GAMMA
;
167 /* Same as YUV but without the YUR->RGB conversion. */
168 case PIPE_FORMAT_R8G8_B8G8_UNORM
:
169 return R300_EASY_TX_FORMAT(X
, Y
, Z
, ONE
, YVYU422
) | result
;
170 case PIPE_FORMAT_G8R8_G8B8_UNORM
:
171 return R300_EASY_TX_FORMAT(X
, Y
, Z
, ONE
, VYUY422
) | result
;
176 result
|= r300_get_swizzle_combined(desc
->swizzle
, swizzle_view
);
179 if (desc
->layout
== UTIL_FORMAT_LAYOUT_S3TC
) {
180 if (!util_format_s3tc_enabled
) {
181 return ~0; /* Unsupported. */
185 case PIPE_FORMAT_DXT1_RGB
:
186 case PIPE_FORMAT_DXT1_RGBA
:
187 case PIPE_FORMAT_DXT1_SRGB
:
188 case PIPE_FORMAT_DXT1_SRGBA
:
189 return R300_TX_FORMAT_DXT1
| result
;
190 case PIPE_FORMAT_DXT3_RGBA
:
191 case PIPE_FORMAT_DXT3_SRGBA
:
192 return R300_TX_FORMAT_DXT3
| result
;
193 case PIPE_FORMAT_DXT5_RGBA
:
194 case PIPE_FORMAT_DXT5_SRGBA
:
195 return R300_TX_FORMAT_DXT5
| result
;
197 return ~0; /* Unsupported/unknown. */
202 for (i
= 0; i
< desc
->nr_channels
; i
++) {
203 if (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_SIGNED
) {
204 result
|= sign_bit
[i
];
208 /* This is truly a special format.
209 * It stores R8G8 and B is computed using sqrt(1 - R^2 - G^2)
210 * in the sampler unit. Also known as D3DFMT_CxV8U8. */
211 if (format
== PIPE_FORMAT_R8G8Bx_SNORM
) {
212 return R300_TX_FORMAT_CxV8U8
| result
;
216 if (desc
->layout
== UTIL_FORMAT_LAYOUT_RGTC
) {
218 case PIPE_FORMAT_RGTC1_UNORM
:
219 case PIPE_FORMAT_RGTC1_SNORM
:
220 return R500_TX_FORMAT_ATI1N
| result
;
221 case PIPE_FORMAT_RGTC2_UNORM
:
222 case PIPE_FORMAT_RGTC2_SNORM
:
223 return R400_TX_FORMAT_ATI2N
| result
;
225 return ~0; /* Unsupported/unknown. */
229 /* See whether the components are of the same size. */
230 for (i
= 1; i
< desc
->nr_channels
; i
++) {
231 uniform
= uniform
&& desc
->channel
[0].size
== desc
->channel
[i
].size
;
234 /* Non-uniform formats. */
236 switch (desc
->nr_channels
) {
238 if (desc
->channel
[0].size
== 5 &&
239 desc
->channel
[1].size
== 6 &&
240 desc
->channel
[2].size
== 5) {
241 return R300_TX_FORMAT_Z5Y6X5
| result
;
243 if (desc
->channel
[0].size
== 5 &&
244 desc
->channel
[1].size
== 5 &&
245 desc
->channel
[2].size
== 6) {
246 return R300_TX_FORMAT_Z6Y5X5
| result
;
248 return ~0; /* Unsupported/unknown. */
251 if (desc
->channel
[0].size
== 5 &&
252 desc
->channel
[1].size
== 5 &&
253 desc
->channel
[2].size
== 5 &&
254 desc
->channel
[3].size
== 1) {
255 return R300_TX_FORMAT_W1Z5Y5X5
| result
;
257 if (desc
->channel
[0].size
== 10 &&
258 desc
->channel
[1].size
== 10 &&
259 desc
->channel
[2].size
== 10 &&
260 desc
->channel
[3].size
== 2) {
261 return R300_TX_FORMAT_W2Z10Y10X10
| result
;
264 return ~0; /* Unsupported/unknown. */
267 /* And finally, uniform formats. */
268 switch (desc
->channel
[0].type
) {
269 case UTIL_FORMAT_TYPE_UNSIGNED
:
270 case UTIL_FORMAT_TYPE_SIGNED
:
271 if (!desc
->channel
[0].normalized
&&
272 desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_SRGB
) {
276 switch (desc
->channel
[0].size
) {
278 switch (desc
->nr_channels
) {
280 return R300_TX_FORMAT_Y4X4
| result
;
282 return R300_TX_FORMAT_W4Z4Y4X4
| result
;
287 switch (desc
->nr_channels
) {
289 return R300_TX_FORMAT_X8
| result
;
291 return R300_TX_FORMAT_Y8X8
| result
;
293 return R300_TX_FORMAT_W8Z8Y8X8
| result
;
298 switch (desc
->nr_channels
) {
300 return R300_TX_FORMAT_X16
| result
;
302 return R300_TX_FORMAT_Y16X16
| result
;
304 return R300_TX_FORMAT_W16Z16Y16X16
| result
;
309 case UTIL_FORMAT_TYPE_FLOAT
:
310 switch (desc
->channel
[0].size
) {
312 switch (desc
->nr_channels
) {
314 return R300_TX_FORMAT_16F
| result
;
316 return R300_TX_FORMAT_16F_16F
| result
;
318 return R300_TX_FORMAT_16F_16F_16F_16F
| result
;
323 switch (desc
->nr_channels
) {
325 return R300_TX_FORMAT_32F
| result
;
327 return R300_TX_FORMAT_32F_32F
| result
;
329 return R300_TX_FORMAT_32F_32F_32F_32F
| result
;
334 return ~0; /* Unsupported/unknown. */
337 uint32_t r500_tx_format_msb_bit(enum pipe_format format
)
340 case PIPE_FORMAT_RGTC1_UNORM
:
341 case PIPE_FORMAT_RGTC1_SNORM
:
342 case PIPE_FORMAT_X8Z24_UNORM
:
343 case PIPE_FORMAT_S8_USCALED_Z24_UNORM
:
344 return R500_TXFORMAT_MSB
;
350 /* Buffer formats. */
352 /* Colorbuffer formats. This is the unswizzled format of the RB3D block's
353 * output. For the swizzling of the targets, check the shader's format. */
354 static uint32_t r300_translate_colorformat(enum pipe_format format
)
358 case PIPE_FORMAT_A8_UNORM
:
359 case PIPE_FORMAT_I8_UNORM
:
360 case PIPE_FORMAT_L8_UNORM
:
361 case PIPE_FORMAT_R8_UNORM
:
362 case PIPE_FORMAT_R8_SNORM
:
363 return R300_COLOR_FORMAT_I8
;
365 /* 16-bit buffers. */
366 case PIPE_FORMAT_B5G6R5_UNORM
:
367 return R300_COLOR_FORMAT_RGB565
;
369 case PIPE_FORMAT_B5G5R5A1_UNORM
:
370 case PIPE_FORMAT_B5G5R5X1_UNORM
:
371 return R300_COLOR_FORMAT_ARGB1555
;
373 case PIPE_FORMAT_B4G4R4A4_UNORM
:
374 case PIPE_FORMAT_B4G4R4X4_UNORM
:
375 return R300_COLOR_FORMAT_ARGB4444
;
377 /* 32-bit buffers. */
378 case PIPE_FORMAT_B8G8R8A8_UNORM
:
379 case PIPE_FORMAT_B8G8R8X8_UNORM
:
380 case PIPE_FORMAT_A8R8G8B8_UNORM
:
381 case PIPE_FORMAT_X8R8G8B8_UNORM
:
382 case PIPE_FORMAT_A8B8G8R8_UNORM
:
383 case PIPE_FORMAT_R8G8B8A8_SNORM
:
384 case PIPE_FORMAT_X8B8G8R8_UNORM
:
385 case PIPE_FORMAT_R8G8B8X8_UNORM
:
386 case PIPE_FORMAT_R8SG8SB8UX8U_NORM
:
387 return R300_COLOR_FORMAT_ARGB8888
;
389 case PIPE_FORMAT_R10G10B10A2_UNORM
:
390 case PIPE_FORMAT_R10G10B10X2_SNORM
:
391 case PIPE_FORMAT_B10G10R10A2_UNORM
:
392 case PIPE_FORMAT_R10SG10SB10SA2U_NORM
:
393 return R500_COLOR_FORMAT_ARGB2101010
; /* R5xx-only? */
395 /* 64-bit buffers. */
396 case PIPE_FORMAT_R16G16B16A16_UNORM
:
397 case PIPE_FORMAT_R16G16B16A16_SNORM
:
398 case PIPE_FORMAT_R16G16B16A16_FLOAT
:
399 return R300_COLOR_FORMAT_ARGB16161616
;
401 /* 128-bit buffers. */
402 case PIPE_FORMAT_R32G32B32A32_FLOAT
:
403 return R300_COLOR_FORMAT_ARGB32323232
;
406 case PIPE_FORMAT_UYVY
:
407 return R300_COLOR_FORMAT_YVYU
;
408 case PIPE_FORMAT_YUYV
:
409 return R300_COLOR_FORMAT_VYUY
;
411 return ~0; /* Unsupported. */
415 /* Depthbuffer and stencilbuffer. Thankfully, we only support two flavors. */
416 static uint32_t r300_translate_zsformat(enum pipe_format format
)
419 /* 16-bit depth, no stencil */
420 case PIPE_FORMAT_Z16_UNORM
:
421 return R300_DEPTHFORMAT_16BIT_INT_Z
;
422 /* 24-bit depth, ignored stencil */
423 case PIPE_FORMAT_X8Z24_UNORM
:
424 /* 24-bit depth, 8-bit stencil */
425 case PIPE_FORMAT_S8_USCALED_Z24_UNORM
:
426 return R300_DEPTHFORMAT_24BIT_INT_Z_8BIT_STENCIL
;
428 return ~0; /* Unsupported. */
432 /* Shader output formats. This is essentially the swizzle from the shader
435 * Note that formats are stored from C3 to C0. */
436 static uint32_t r300_translate_out_fmt(enum pipe_format format
)
438 uint32_t modifier
= 0;
440 const struct util_format_description
*desc
;
441 static const uint32_t sign_bit
[4] = {
448 desc
= util_format_description(format
);
450 /* Specifies how the shader output is written to the fog unit. */
451 if (desc
->channel
[0].type
== UTIL_FORMAT_TYPE_FLOAT
) {
452 if (desc
->channel
[0].size
== 32) {
453 modifier
|= R300_US_OUT_FMT_C4_32_FP
;
455 modifier
|= R300_US_OUT_FMT_C4_16_FP
;
458 if (desc
->channel
[0].size
== 16) {
459 modifier
|= R300_US_OUT_FMT_C4_16
;
461 /* C4_8 seems to be used for the formats whose pixel size
463 modifier
|= R300_US_OUT_FMT_C4_8
;
468 for (i
= 0; i
< 4; i
++)
469 if (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_SIGNED
) {
470 modifier
|= sign_bit
[i
];
473 /* Add swizzles and return. */
476 * COLORFORMAT_I8 stores the C2 component. */
477 case PIPE_FORMAT_A8_UNORM
:
478 return modifier
| R300_C2_SEL_A
;
479 case PIPE_FORMAT_I8_UNORM
:
480 case PIPE_FORMAT_L8_UNORM
:
481 case PIPE_FORMAT_R8_UNORM
:
482 case PIPE_FORMAT_R8_SNORM
:
483 return modifier
| R300_C2_SEL_R
;
486 case PIPE_FORMAT_B5G6R5_UNORM
:
487 case PIPE_FORMAT_B5G5R5A1_UNORM
:
488 case PIPE_FORMAT_B5G5R5X1_UNORM
:
489 case PIPE_FORMAT_B4G4R4A4_UNORM
:
490 case PIPE_FORMAT_B4G4R4X4_UNORM
:
491 case PIPE_FORMAT_B8G8R8A8_UNORM
:
492 case PIPE_FORMAT_B8G8R8X8_UNORM
:
493 case PIPE_FORMAT_B10G10R10A2_UNORM
:
495 R300_C0_SEL_B
| R300_C1_SEL_G
|
496 R300_C2_SEL_R
| R300_C3_SEL_A
;
499 case PIPE_FORMAT_A8R8G8B8_UNORM
:
500 case PIPE_FORMAT_X8R8G8B8_UNORM
:
502 R300_C0_SEL_A
| R300_C1_SEL_R
|
503 R300_C2_SEL_G
| R300_C3_SEL_B
;
506 case PIPE_FORMAT_A8B8G8R8_UNORM
:
507 case PIPE_FORMAT_X8B8G8R8_UNORM
:
509 R300_C0_SEL_A
| R300_C1_SEL_B
|
510 R300_C2_SEL_G
| R300_C3_SEL_R
;
513 case PIPE_FORMAT_R8G8B8X8_UNORM
:
514 case PIPE_FORMAT_R8G8B8A8_SNORM
:
515 case PIPE_FORMAT_R8SG8SB8UX8U_NORM
:
516 case PIPE_FORMAT_R10G10B10A2_UNORM
:
517 case PIPE_FORMAT_R10G10B10X2_SNORM
:
518 case PIPE_FORMAT_R10SG10SB10SA2U_NORM
:
519 case PIPE_FORMAT_R16G16B16A16_UNORM
:
520 case PIPE_FORMAT_R16G16B16A16_SNORM
:
521 case PIPE_FORMAT_R16G16B16A16_FLOAT
:
522 case PIPE_FORMAT_R32G32B32A32_FLOAT
:
524 R300_C0_SEL_R
| R300_C1_SEL_G
|
525 R300_C2_SEL_B
| R300_C3_SEL_A
;
528 return ~0; /* Unsupported. */
532 boolean
r300_is_colorbuffer_format_supported(enum pipe_format format
)
534 return r300_translate_colorformat(format
) != ~0 &&
535 r300_translate_out_fmt(format
) != ~0;
538 boolean
r300_is_zs_format_supported(enum pipe_format format
)
540 return r300_translate_zsformat(format
) != ~0;
543 boolean
r300_is_sampler_format_supported(enum pipe_format format
)
545 return r300_translate_texformat(format
, 0) != ~0;
548 static void r300_texture_setup_immutable_state(struct r300_screen
* screen
,
549 struct r300_texture
* tex
)
551 struct r300_texture_format_state
* f
= &tex
->tx_format
;
552 struct pipe_resource
*pt
= &tex
->b
.b
;
553 boolean is_r500
= screen
->caps
.is_r500
;
555 /* Set sampler state. */
556 f
->format0
= R300_TX_WIDTH((pt
->width0
- 1) & 0x7ff) |
557 R300_TX_HEIGHT((pt
->height0
- 1) & 0x7ff);
559 if (tex
->uses_pitch
) {
560 /* rectangles love this */
561 f
->format0
|= R300_TX_PITCH_EN
;
562 f
->format2
= (tex
->hwpitch
[0] - 1) & 0x1fff;
564 /* power of two textures (3D, mipmaps, and no pitch) */
565 f
->format0
|= R300_TX_DEPTH(util_logbase2(pt
->depth0
) & 0xf);
569 if (pt
->target
== PIPE_TEXTURE_CUBE
) {
570 f
->format1
|= R300_TX_FORMAT_CUBIC_MAP
;
572 if (pt
->target
== PIPE_TEXTURE_3D
) {
573 f
->format1
|= R300_TX_FORMAT_3D
;
576 /* large textures on r500 */
579 if (pt
->width0
> 2048) {
580 f
->format2
|= R500_TXWIDTH_BIT11
;
582 if (pt
->height0
> 2048) {
583 f
->format2
|= R500_TXHEIGHT_BIT11
;
587 f
->tile_config
= R300_TXO_MACRO_TILE(tex
->macrotile
) |
588 R300_TXO_MICRO_TILE(tex
->microtile
);
591 static void r300_texture_setup_fb_state(struct r300_screen
* screen
,
592 struct r300_texture
* tex
)
596 /* Set framebuffer state. */
597 if (util_format_is_depth_or_stencil(tex
->b
.b
.format
)) {
598 for (i
= 0; i
<= tex
->b
.b
.last_level
; i
++) {
599 tex
->fb_state
.pitch
[i
] =
601 R300_DEPTHMACROTILE(tex
->mip_macrotile
[i
]) |
602 R300_DEPTHMICROTILE(tex
->microtile
);
604 tex
->fb_state
.format
= r300_translate_zsformat(tex
->b
.b
.format
);
606 for (i
= 0; i
<= tex
->b
.b
.last_level
; i
++) {
607 tex
->fb_state
.pitch
[i
] =
609 r300_translate_colorformat(tex
->b
.b
.format
) |
610 R300_COLOR_TILE(tex
->mip_macrotile
[i
]) |
611 R300_COLOR_MICROTILE(tex
->microtile
);
613 tex
->fb_state
.format
= r300_translate_out_fmt(tex
->b
.b
.format
);
617 void r300_texture_reinterpret_format(struct pipe_screen
*screen
,
618 struct pipe_resource
*tex
,
619 enum pipe_format new_format
)
621 struct r300_screen
*r300screen
= r300_screen(screen
);
623 SCREEN_DBG(r300screen
, DBG_TEX
,
624 "r300: texture_reinterpret_format: %s -> %s\n",
625 util_format_short_name(tex
->format
),
626 util_format_short_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
, stride
;
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 (util_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 stride
= util_format_get_stride(tex
->b
.b
.format
, width
);
724 /* Some IGPs need a minimum stride of 64 bytes, hmm...
725 * This doesn't seem to apply to tiled textures, according to r300c. */
726 if (!tex
->microtile
&& !tex
->mip_macrotile
[level
] &&
727 (screen
->caps
.family
== CHIP_FAMILY_RS600
||
728 screen
->caps
.family
== CHIP_FAMILY_RS690
||
729 screen
->caps
.family
== CHIP_FAMILY_RS740
)) {
730 return stride
< 64 ? 64 : stride
;
733 /* The alignment to 32 bytes is sort of implied by the layout... */
736 return align(util_format_get_stride(tex
->b
.b
.format
, width
), 32);
740 static unsigned r300_texture_get_nblocksy(struct r300_texture
* tex
,
743 unsigned height
, tile_height
;
745 height
= u_minify(tex
->b
.b
.height0
, level
);
747 if (util_format_is_plain(tex
->b
.b
.format
)) {
748 tile_height
= r300_texture_get_tile_size(tex
, TILE_HEIGHT
,
749 tex
->mip_macrotile
[level
]);
750 height
= align(height
, tile_height
);
752 /* This is needed for the kernel checker, unfortunately. */
753 height
= util_next_power_of_two(height
);
756 return util_format_get_nblocksy(tex
->b
.b
.format
, height
);
759 static void r300_texture_3d_fix_mipmapping(struct r300_screen
*screen
,
760 struct r300_texture
*tex
)
762 /* The kernels <= 2.6.34-rc4 compute the size of mipmapped 3D textures
763 * incorrectly. This is a workaround to prevent CS from being rejected. */
767 if (!screen
->rws
->get_value(screen
->rws
, R300_VID_DRM_2_3_0
) &&
768 tex
->b
.b
.target
== PIPE_TEXTURE_3D
&&
769 tex
->b
.b
.last_level
> 0) {
772 for (i
= 0; i
<= tex
->b
.b
.last_level
; i
++) {
773 size
+= r300_texture_get_stride(screen
, tex
, i
) *
774 r300_texture_get_nblocksy(tex
, i
);
777 size
*= tex
->b
.b
.depth0
;
782 static void r300_setup_miptree(struct r300_screen
* screen
,
783 struct r300_texture
* tex
)
785 struct pipe_resource
* base
= &tex
->b
.b
;
786 unsigned stride
, size
, layer_size
, nblocksy
, i
;
787 boolean rv350_mode
= screen
->caps
.is_rv350
;
789 SCREEN_DBG(screen
, DBG_TEXALLOC
,
790 "r300: Making miptree for texture, format %s\n",
791 util_format_short_name(base
->format
));
793 for (i
= 0; i
<= base
->last_level
; i
++) {
794 /* Let's see if this miplevel can be macrotiled. */
795 tex
->mip_macrotile
[i
] =
796 (tex
->macrotile
== R300_BUFFER_TILED
&&
797 r300_texture_macro_switch(tex
, i
, rv350_mode
, TILE_WIDTH
) &&
798 r300_texture_macro_switch(tex
, i
, rv350_mode
, TILE_HEIGHT
)) ?
799 R300_BUFFER_TILED
: R300_BUFFER_LINEAR
;
801 stride
= r300_texture_get_stride(screen
, tex
, i
);
802 nblocksy
= r300_texture_get_nblocksy(tex
, i
);
803 layer_size
= stride
* nblocksy
;
805 if (base
->target
== PIPE_TEXTURE_CUBE
)
806 size
= layer_size
* 6;
808 size
= layer_size
* u_minify(base
->depth0
, i
);
810 tex
->offset
[i
] = tex
->size
;
811 tex
->size
= tex
->offset
[i
] + size
;
812 tex
->layer_size
[i
] = layer_size
;
813 tex
->pitch
[i
] = stride
/ util_format_get_blocksize(base
->format
);
815 tex
->pitch
[i
] * util_format_get_blockwidth(base
->format
);
817 SCREEN_DBG(screen
, DBG_TEXALLOC
, "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 (!util_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
;
943 /* Refuse to create a texture with size 0. */
945 (!base
->height0
&& (base
->target
== PIPE_TEXTURE_2D
||
946 base
->target
== PIPE_TEXTURE_CUBE
)) ||
947 (!base
->depth0
&& base
->target
== PIPE_TEXTURE_3D
)) {
948 fprintf(stderr
, "r300: texture_create: "
949 "Got invalid texture dimensions: %ix%ix%i\n",
950 base
->width0
, base
->height0
, base
->depth0
);
956 tex
->b
.vtbl
= &r300_texture_vtbl
;
957 pipe_reference_init(&tex
->b
.b
.reference
, 1);
958 tex
->b
.b
.screen
= screen
;
960 r300_setup_flags(tex
);
961 if (!(base
->flags
& R300_RESOURCE_FLAG_TRANSFER
) &&
962 !(base
->bind
& PIPE_BIND_SCANOUT
)) {
963 r300_setup_tiling(screen
, tex
);
965 r300_setup_miptree(rscreen
, tex
);
966 r300_texture_3d_fix_mipmapping(rscreen
, tex
);
967 r300_texture_setup_immutable_state(rscreen
, tex
);
968 r300_texture_setup_fb_state(rscreen
, tex
);
970 SCREEN_DBG(rscreen
, DBG_TEX
,
971 "r300: texture_create: Macro: %s, Micro: %s, Pitch: %i, "
972 "Dim: %ix%ix%i, LastLevel: %i, Size: %i, Format: %s\n",
973 tex
->macrotile
? "YES" : " NO",
974 tex
->microtile
? "YES" : " NO",
976 base
->width0
, base
->height0
, base
->depth0
, base
->last_level
,
978 util_format_short_name(base
->format
));
980 tex
->domain
= base
->flags
& R300_RESOURCE_FLAG_TRANSFER
? R300_DOMAIN_GTT
:
983 tex
->buffer
= rws
->buffer_create(rws
, 2048, base
->bind
, tex
->domain
,
991 rws
->buffer_set_tiling(rws
, tex
->buffer
,
992 tex
->pitch
[0] * util_format_get_blocksize(tex
->b
.b
.format
),
996 return (struct pipe_resource
*)tex
;
999 /* Not required to implement u_resource_vtbl, consider moving to another file:
1001 struct pipe_surface
* r300_get_tex_surface(struct pipe_screen
* screen
,
1002 struct pipe_resource
* texture
,
1008 struct r300_texture
* tex
= r300_texture(texture
);
1009 struct r300_surface
* surface
= CALLOC_STRUCT(r300_surface
);
1012 pipe_reference_init(&surface
->base
.reference
, 1);
1013 pipe_resource_reference(&surface
->base
.texture
, texture
);
1014 surface
->base
.format
= texture
->format
;
1015 surface
->base
.width
= u_minify(texture
->width0
, level
);
1016 surface
->base
.height
= u_minify(texture
->height0
, level
);
1017 surface
->base
.usage
= flags
;
1018 surface
->base
.zslice
= zslice
;
1019 surface
->base
.face
= face
;
1020 surface
->base
.level
= level
;
1022 surface
->buffer
= tex
->buffer
;
1023 surface
->domain
= tex
->domain
;
1024 surface
->offset
= r300_texture_get_offset(tex
, level
, zslice
, face
);
1025 surface
->pitch
= tex
->fb_state
.pitch
[level
];
1026 surface
->format
= tex
->fb_state
.format
;
1029 return &surface
->base
;
1032 /* Not required to implement u_resource_vtbl, consider moving to another file:
1034 void r300_tex_surface_destroy(struct pipe_surface
* s
)
1036 pipe_resource_reference(&s
->texture
, NULL
);
1040 struct pipe_resource
*
1041 r300_texture_from_handle(struct pipe_screen
* screen
,
1042 const struct pipe_resource
* base
,
1043 struct winsys_handle
*whandle
)
1045 struct r300_winsys_screen
*rws
= (struct r300_winsys_screen
*)screen
->winsys
;
1046 struct r300_screen
* rscreen
= r300_screen(screen
);
1047 struct r300_winsys_buffer
*buffer
;
1048 struct r300_texture
* tex
;
1050 boolean override_zb_flags
;
1052 /* Support only 2D textures without mipmaps */
1053 if (base
->target
!= PIPE_TEXTURE_2D
||
1054 base
->depth0
!= 1 ||
1055 base
->last_level
!= 0) {
1059 buffer
= rws
->buffer_from_handle(rws
, screen
, whandle
, &stride
);
1064 tex
= CALLOC_STRUCT(r300_texture
);
1070 tex
->b
.vtbl
= &r300_texture_vtbl
;
1071 pipe_reference_init(&tex
->b
.b
.reference
, 1);
1072 tex
->b
.b
.screen
= screen
;
1073 tex
->domain
= R300_DOMAIN_VRAM
;
1075 tex
->stride_override
= stride
;
1077 /* one ref already taken */
1078 tex
->buffer
= buffer
;
1080 rws
->buffer_get_tiling(rws
, buffer
, &tex
->microtile
, &tex
->macrotile
);
1081 r300_setup_flags(tex
);
1082 SCREEN_DBG(rscreen
, DBG_TEX
,
1083 "r300: texture_from_handle: Macro: %s, Micro: %s, "
1084 "Pitch: % 4i, Dim: %ix%i, Format: %s\n",
1085 tex
->macrotile
? "YES" : " NO",
1086 tex
->microtile
? "YES" : " NO",
1087 stride
/ util_format_get_blocksize(base
->format
),
1088 base
->width0
, base
->height0
,
1089 util_format_short_name(base
->format
));
1091 /* Enforce microtiled zbuffer. */
1092 override_zb_flags
= util_format_is_depth_or_stencil(base
->format
) &&
1093 tex
->microtile
== R300_BUFFER_LINEAR
;
1095 if (override_zb_flags
) {
1096 switch (util_format_get_blocksize(base
->format
)) {
1098 tex
->microtile
= R300_BUFFER_TILED
;
1102 if (rws
->get_value(rws
, R300_VID_SQUARE_TILING_SUPPORT
)) {
1103 tex
->microtile
= R300_BUFFER_SQUARETILED
;
1109 override_zb_flags
= FALSE
;
1113 r300_setup_miptree(rscreen
, tex
);
1114 r300_texture_setup_immutable_state(rscreen
, tex
);
1115 r300_texture_setup_fb_state(rscreen
, tex
);
1117 if (override_zb_flags
) {
1118 rws
->buffer_set_tiling(rws
, tex
->buffer
,
1119 tex
->pitch
[0] * util_format_get_blocksize(tex
->b
.b
.format
),
1123 return (struct pipe_resource
*)tex
;