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"
31 #include "r300_texture.h"
32 #include "r300_screen.h"
33 #include "r300_state_inlines.h"
34 #include "r300_winsys.h"
39 static const unsigned microblock_table
[5][3][2] = {
40 /*linear tiled square-tiled */
41 {{32, 1}, {8, 4}, {0, 0}}, /* 8 bits per pixel */
42 {{16, 1}, {8, 2}, {4, 4}}, /* 16 bits per pixel */
43 {{ 8, 1}, {4, 2}, {0, 0}}, /* 32 bits per pixel */
44 {{ 4, 1}, {0, 0}, {2, 2}}, /* 64 bits per pixel */
45 {{ 2, 1}, {0, 0}, {0, 0}} /* 128 bits per pixel */
48 /* Return true for non-compressed and non-YUV formats. */
49 static boolean
r300_format_is_plain(enum pipe_format format
)
51 const struct util_format_description
*desc
= util_format_description(format
);
57 return desc
->layout
== UTIL_FORMAT_LAYOUT_PLAIN
;
60 /* Translate a pipe_format into a useful texture format for sampling.
62 * Some special formats are translated directly using R300_EASY_TX_FORMAT,
63 * but the majority of them is translated in a generic way, automatically
64 * supporting all the formats hw can support.
66 * R300_EASY_TX_FORMAT swizzles the texture.
67 * Note the signature of R300_EASY_TX_FORMAT:
68 * R300_EASY_TX_FORMAT(B, G, R, A, FORMAT);
70 * The FORMAT specifies how the texture sampler will treat the texture, and
71 * makes available X, Y, Z, W, ZERO, and ONE for swizzling. */
72 static uint32_t r300_translate_texformat(enum pipe_format format
)
75 const struct util_format_description
*desc
;
77 boolean uniform
= TRUE
;
78 const uint32_t swizzle_shift
[4] = {
79 R300_TX_FORMAT_R_SHIFT
,
80 R300_TX_FORMAT_G_SHIFT
,
81 R300_TX_FORMAT_B_SHIFT
,
82 R300_TX_FORMAT_A_SHIFT
84 const uint32_t swizzle
[4] = {
90 const uint32_t sign_bit
[4] = {
91 R300_TX_FORMAT_SIGNED_X
,
92 R300_TX_FORMAT_SIGNED_Y
,
93 R300_TX_FORMAT_SIGNED_Z
,
94 R300_TX_FORMAT_SIGNED_W
,
97 desc
= util_format_description(format
);
99 /* Colorspace (return non-RGB formats directly). */
100 switch (desc
->colorspace
) {
101 /* Depth stencil formats. */
102 case UTIL_FORMAT_COLORSPACE_ZS
:
104 case PIPE_FORMAT_Z16_UNORM
:
105 return R300_EASY_TX_FORMAT(X
, X
, X
, X
, X16
);
106 case PIPE_FORMAT_X8Z24_UNORM
:
107 case PIPE_FORMAT_S8_USCALED_Z24_UNORM
:
108 return R300_EASY_TX_FORMAT(X
, X
, X
, X
, W24_FP
);
110 return ~0; /* Unsupported. */
114 case UTIL_FORMAT_COLORSPACE_YUV
:
115 result
|= R300_TX_FORMAT_YUV_TO_RGB
;
118 case PIPE_FORMAT_UYVY
:
119 return R300_EASY_TX_FORMAT(X
, Y
, Z
, ONE
, YVYU422
) | result
;
120 case PIPE_FORMAT_YUYV
:
121 return R300_EASY_TX_FORMAT(X
, Y
, Z
, ONE
, VYUY422
) | result
;
123 return ~0; /* Unsupported/unknown. */
126 /* Add gamma correction. */
127 case UTIL_FORMAT_COLORSPACE_SRGB
:
128 result
|= R300_TX_FORMAT_GAMMA
;
135 for (i
= 0; i
< 4; i
++) {
136 switch (desc
->swizzle
[i
]) {
137 case UTIL_FORMAT_SWIZZLE_X
:
138 case UTIL_FORMAT_SWIZZLE_NONE
:
139 result
|= swizzle
[0] << swizzle_shift
[i
];
141 case UTIL_FORMAT_SWIZZLE_Y
:
142 result
|= swizzle
[1] << swizzle_shift
[i
];
144 case UTIL_FORMAT_SWIZZLE_Z
:
145 result
|= swizzle
[2] << swizzle_shift
[i
];
147 case UTIL_FORMAT_SWIZZLE_W
:
148 result
|= swizzle
[3] << swizzle_shift
[i
];
150 case UTIL_FORMAT_SWIZZLE_0
:
151 result
|= R300_TX_FORMAT_ZERO
<< swizzle_shift
[i
];
153 case UTIL_FORMAT_SWIZZLE_1
:
154 result
|= R300_TX_FORMAT_ONE
<< swizzle_shift
[i
];
157 return ~0; /* Unsupported. */
162 if (desc
->layout
== UTIL_FORMAT_LAYOUT_S3TC
) {
164 case PIPE_FORMAT_DXT1_RGB
:
165 case PIPE_FORMAT_DXT1_RGBA
:
166 case PIPE_FORMAT_DXT1_SRGB
:
167 case PIPE_FORMAT_DXT1_SRGBA
:
168 return R300_TX_FORMAT_DXT1
| result
;
169 case PIPE_FORMAT_DXT3_RGBA
:
170 case PIPE_FORMAT_DXT3_SRGBA
:
171 return R300_TX_FORMAT_DXT3
| result
;
172 case PIPE_FORMAT_DXT5_RGBA
:
173 case PIPE_FORMAT_DXT5_SRGBA
:
174 return R300_TX_FORMAT_DXT5
| result
;
176 return ~0; /* Unsupported/unknown. */
181 for (i
= 0; i
< desc
->nr_channels
; i
++) {
182 if (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_SIGNED
) {
183 result
|= sign_bit
[i
];
188 if (desc
->layout
== UTIL_FORMAT_LAYOUT_RGTC
) {
190 case PIPE_FORMAT_RGTC1_UNORM
:
191 case PIPE_FORMAT_RGTC1_SNORM
:
192 return R500_TX_FORMAT_ATI1N
| result
;
193 case PIPE_FORMAT_RGTC2_UNORM
:
194 case PIPE_FORMAT_RGTC2_SNORM
:
195 return R400_TX_FORMAT_ATI2N
| result
;
197 return ~0; /* Unsupported/unknown. */
201 /* See whether the components are of the same size. */
202 for (i
= 1; i
< desc
->nr_channels
; i
++) {
203 uniform
= uniform
&& desc
->channel
[0].size
== desc
->channel
[i
].size
;
206 /* Non-uniform formats. */
208 switch (desc
->nr_channels
) {
210 if (desc
->channel
[0].size
== 5 &&
211 desc
->channel
[1].size
== 6 &&
212 desc
->channel
[2].size
== 5) {
213 return R300_TX_FORMAT_Z5Y6X5
| result
;
215 if (desc
->channel
[0].size
== 5 &&
216 desc
->channel
[1].size
== 5 &&
217 desc
->channel
[2].size
== 6) {
218 return R300_TX_FORMAT_Z6Y5X5
| result
;
220 return ~0; /* Unsupported/unknown. */
223 if (desc
->channel
[0].size
== 5 &&
224 desc
->channel
[1].size
== 5 &&
225 desc
->channel
[2].size
== 5 &&
226 desc
->channel
[3].size
== 1) {
227 return R300_TX_FORMAT_W1Z5Y5X5
| result
;
229 if (desc
->channel
[0].size
== 10 &&
230 desc
->channel
[1].size
== 10 &&
231 desc
->channel
[2].size
== 10 &&
232 desc
->channel
[3].size
== 2) {
233 return R300_TX_FORMAT_W2Z10Y10X10
| result
;
236 return ~0; /* Unsupported/unknown. */
239 /* And finally, uniform formats. */
240 switch (desc
->channel
[0].type
) {
241 case UTIL_FORMAT_TYPE_UNSIGNED
:
242 case UTIL_FORMAT_TYPE_SIGNED
:
243 if (!desc
->channel
[0].normalized
&&
244 desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_SRGB
) {
248 switch (desc
->channel
[0].size
) {
250 switch (desc
->nr_channels
) {
252 return R300_TX_FORMAT_Y4X4
| result
;
254 return R300_TX_FORMAT_W4Z4Y4X4
| result
;
259 switch (desc
->nr_channels
) {
261 return R300_TX_FORMAT_X8
| result
;
263 return R300_TX_FORMAT_Y8X8
| result
;
265 return R300_TX_FORMAT_W8Z8Y8X8
| result
;
270 switch (desc
->nr_channels
) {
272 return R300_TX_FORMAT_X16
| result
;
274 return R300_TX_FORMAT_Y16X16
| result
;
276 return R300_TX_FORMAT_W16Z16Y16X16
| result
;
281 /* XXX Enable float textures here. */
283 case UTIL_FORMAT_TYPE_FLOAT
:
284 switch (desc
->channel
[0].size
) {
286 switch (desc
->nr_channels
) {
288 return R300_TX_FORMAT_16F
| result
;
290 return R300_TX_FORMAT_16F_16F
| result
;
292 return R300_TX_FORMAT_16F_16F_16F_16F
| result
;
297 switch (desc
->nr_channels
) {
299 return R300_TX_FORMAT_32F
| result
;
301 return R300_TX_FORMAT_32F_32F
| result
;
303 return R300_TX_FORMAT_32F_32F_32F_32F
| result
;
309 return ~0; /* Unsupported/unknown. */
312 static uint32_t r500_tx_format_msb_bit(enum pipe_format format
)
315 case PIPE_FORMAT_RGTC1_UNORM
:
316 case PIPE_FORMAT_RGTC1_SNORM
:
317 return R500_TXFORMAT_MSB
;
323 /* Buffer formats. */
325 /* Colorbuffer formats. This is the unswizzled format of the RB3D block's
326 * output. For the swizzling of the targets, check the shader's format. */
327 static uint32_t r300_translate_colorformat(enum pipe_format format
)
331 case PIPE_FORMAT_A8_UNORM
:
332 case PIPE_FORMAT_I8_UNORM
:
333 case PIPE_FORMAT_L8_UNORM
:
334 case PIPE_FORMAT_R8_UNORM
:
335 case PIPE_FORMAT_R8_SNORM
:
336 return R300_COLOR_FORMAT_I8
;
338 /* 16-bit buffers. */
339 case PIPE_FORMAT_B5G6R5_UNORM
:
340 return R300_COLOR_FORMAT_RGB565
;
341 case PIPE_FORMAT_B5G5R5A1_UNORM
:
342 case PIPE_FORMAT_B5G5R5X1_UNORM
:
343 return R300_COLOR_FORMAT_ARGB1555
;
344 case PIPE_FORMAT_B4G4R4A4_UNORM
:
345 return R300_COLOR_FORMAT_ARGB4444
;
347 /* 32-bit buffers. */
348 case PIPE_FORMAT_B8G8R8A8_UNORM
:
349 case PIPE_FORMAT_B8G8R8X8_UNORM
:
350 case PIPE_FORMAT_A8R8G8B8_UNORM
:
351 case PIPE_FORMAT_X8R8G8B8_UNORM
:
352 case PIPE_FORMAT_A8B8G8R8_UNORM
:
353 case PIPE_FORMAT_R8G8B8A8_SNORM
:
354 case PIPE_FORMAT_X8B8G8R8_UNORM
:
355 case PIPE_FORMAT_R8SG8SB8UX8U_NORM
:
356 return R300_COLOR_FORMAT_ARGB8888
;
357 case PIPE_FORMAT_R10G10B10A2_UNORM
:
358 return R500_COLOR_FORMAT_ARGB2101010
; /* R5xx-only? */
360 /* 64-bit buffers. */
361 case PIPE_FORMAT_R16G16B16A16_UNORM
:
362 case PIPE_FORMAT_R16G16B16A16_SNORM
:
363 //case PIPE_FORMAT_R16G16B16A16_FLOAT: /* not in pipe_format */
364 return R300_COLOR_FORMAT_ARGB16161616
;
366 /* XXX Enable float textures here. */
368 /* 128-bit buffers. */
369 case PIPE_FORMAT_R32G32B32A32_FLOAT
:
370 return R300_COLOR_FORMAT_ARGB32323232
;
374 case PIPE_FORMAT_UYVY
:
375 return R300_COLOR_FORMAT_YVYU
;
376 case PIPE_FORMAT_YUYV
:
377 return R300_COLOR_FORMAT_VYUY
;
379 return ~0; /* Unsupported. */
383 /* Depthbuffer and stencilbuffer. Thankfully, we only support two flavors. */
384 static uint32_t r300_translate_zsformat(enum pipe_format format
)
387 /* 16-bit depth, no stencil */
388 case PIPE_FORMAT_Z16_UNORM
:
389 return R300_DEPTHFORMAT_16BIT_INT_Z
;
390 /* 24-bit depth, ignored stencil */
391 case PIPE_FORMAT_X8Z24_UNORM
:
392 /* 24-bit depth, 8-bit stencil */
393 case PIPE_FORMAT_S8_USCALED_Z24_UNORM
:
394 return R300_DEPTHFORMAT_24BIT_INT_Z_8BIT_STENCIL
;
396 return ~0; /* Unsupported. */
400 /* Shader output formats. This is essentially the swizzle from the shader
403 * Note that formats are stored from C3 to C0. */
404 static uint32_t r300_translate_out_fmt(enum pipe_format format
)
406 uint32_t modifier
= 0;
408 const struct util_format_description
*desc
;
409 static const uint32_t sign_bit
[4] = {
416 desc
= util_format_description(format
);
418 /* Specifies how the shader output is written to the fog unit. */
419 if (desc
->channel
[0].type
== UTIL_FORMAT_TYPE_FLOAT
) {
420 if (desc
->channel
[0].size
== 32) {
421 modifier
|= R300_US_OUT_FMT_C4_32_FP
;
423 modifier
|= R300_US_OUT_FMT_C4_16_FP
;
426 if (desc
->channel
[0].size
== 16) {
427 modifier
|= R300_US_OUT_FMT_C4_16
;
429 /* C4_8 seems to be used for the formats whose pixel size
431 modifier
|= R300_US_OUT_FMT_C4_8
;
436 for (i
= 0; i
< 4; i
++)
437 if (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_SIGNED
) {
438 modifier
|= sign_bit
[i
];
441 /* Add swizzles and return. */
444 * COLORFORMAT_I8 stores the C2 component. */
445 case PIPE_FORMAT_A8_UNORM
:
446 return modifier
| R300_C2_SEL_A
;
447 case PIPE_FORMAT_I8_UNORM
:
448 case PIPE_FORMAT_L8_UNORM
:
449 case PIPE_FORMAT_R8_UNORM
:
450 case PIPE_FORMAT_R8_SNORM
:
451 return modifier
| R300_C2_SEL_R
;
454 case PIPE_FORMAT_B5G6R5_UNORM
:
455 case PIPE_FORMAT_B5G5R5A1_UNORM
:
456 case PIPE_FORMAT_B5G5R5X1_UNORM
:
457 case PIPE_FORMAT_B4G4R4A4_UNORM
:
458 case PIPE_FORMAT_B8G8R8A8_UNORM
:
459 case PIPE_FORMAT_B8G8R8X8_UNORM
:
461 R300_C0_SEL_B
| R300_C1_SEL_G
|
462 R300_C2_SEL_R
| R300_C3_SEL_A
;
465 case PIPE_FORMAT_A8R8G8B8_UNORM
:
466 case PIPE_FORMAT_X8R8G8B8_UNORM
:
468 R300_C0_SEL_A
| R300_C1_SEL_R
|
469 R300_C2_SEL_G
| R300_C3_SEL_B
;
472 case PIPE_FORMAT_A8B8G8R8_UNORM
:
473 case PIPE_FORMAT_X8B8G8R8_UNORM
:
475 R300_C0_SEL_A
| R300_C1_SEL_B
|
476 R300_C2_SEL_G
| R300_C3_SEL_R
;
479 case PIPE_FORMAT_R8G8B8A8_SNORM
:
480 case PIPE_FORMAT_R8SG8SB8UX8U_NORM
:
481 case PIPE_FORMAT_R10G10B10A2_UNORM
:
482 case PIPE_FORMAT_R16G16B16A16_UNORM
:
483 case PIPE_FORMAT_R16G16B16A16_SNORM
:
484 //case PIPE_FORMAT_R16G16B16A16_FLOAT: /* not in pipe_format */
485 case PIPE_FORMAT_R32G32B32A32_FLOAT
:
487 R300_C0_SEL_R
| R300_C1_SEL_G
|
488 R300_C2_SEL_B
| R300_C3_SEL_A
;
491 return ~0; /* Unsupported. */
495 boolean
r300_is_colorbuffer_format_supported(enum pipe_format format
)
497 return r300_translate_colorformat(format
) != ~0 &&
498 r300_translate_out_fmt(format
) != ~0;
501 boolean
r300_is_zs_format_supported(enum pipe_format format
)
503 return r300_translate_zsformat(format
) != ~0;
506 boolean
r300_is_sampler_format_supported(enum pipe_format format
)
508 return r300_translate_texformat(format
) != ~0;
511 static void r300_setup_texture_state(struct r300_screen
* screen
, struct r300_texture
* tex
)
513 struct r300_texture_format_state
* state
= &tex
->state
;
514 struct pipe_texture
*pt
= &tex
->tex
;
516 boolean is_r500
= screen
->caps
->is_r500
;
518 /* Set sampler state. */
519 state
->format0
= R300_TX_WIDTH((pt
->width0
- 1) & 0x7ff) |
520 R300_TX_HEIGHT((pt
->height0
- 1) & 0x7ff);
523 /* rectangles love this */
524 state
->format0
|= R300_TX_PITCH_EN
;
525 state
->format2
= (tex
->pitch
[0] - 1) & 0x1fff;
527 /* power of two textures (3D, mipmaps, and no pitch) */
528 state
->format0
|= R300_TX_DEPTH(util_logbase2(pt
->depth0
) & 0xf);
531 state
->format1
= r300_translate_texformat(pt
->format
);
532 if (pt
->target
== PIPE_TEXTURE_CUBE
) {
533 state
->format1
|= R300_TX_FORMAT_CUBIC_MAP
;
535 if (pt
->target
== PIPE_TEXTURE_3D
) {
536 state
->format1
|= R300_TX_FORMAT_3D
;
539 /* large textures on r500 */
542 if (pt
->width0
> 2048) {
543 state
->format2
|= R500_TXWIDTH_BIT11
;
545 if (pt
->height0
> 2048) {
546 state
->format2
|= R500_TXHEIGHT_BIT11
;
548 state
->format2
|= r500_tx_format_msb_bit(pt
->format
);
551 SCREEN_DBG(screen
, DBG_TEX
, "r300: Set texture state (%dx%d, %d levels)\n",
552 pt
->width0
, pt
->height0
, pt
->last_level
);
554 /* Set framebuffer state. */
555 if (util_format_is_depth_or_stencil(tex
->tex
.format
)) {
556 for (i
= 0; i
<= tex
->tex
.last_level
; i
++) {
557 tex
->fb_state
.depthpitch
[i
] =
559 R300_DEPTHMACROTILE(tex
->mip_macrotile
[i
]) |
560 R300_DEPTHMICROTILE(tex
->microtile
);
562 tex
->fb_state
.zb_format
= r300_translate_zsformat(tex
->tex
.format
);
564 for (i
= 0; i
<= tex
->tex
.last_level
; i
++) {
565 tex
->fb_state
.colorpitch
[i
] =
567 r300_translate_colorformat(tex
->tex
.format
) |
568 R300_COLOR_TILE(tex
->mip_macrotile
[i
]) |
569 R300_COLOR_MICROTILE(tex
->microtile
);
571 tex
->fb_state
.us_out_fmt
= r300_translate_out_fmt(tex
->tex
.format
);
575 void r300_texture_reinterpret_format(struct pipe_screen
*screen
,
576 struct pipe_texture
*tex
,
577 enum pipe_format new_format
)
579 struct r300_screen
*r300screen
= r300_screen(screen
);
581 SCREEN_DBG(r300screen
, DBG_TEX
, "r300: Reinterpreting format: %s -> %s\n",
582 util_format_name(tex
->format
), util_format_name(new_format
));
584 tex
->format
= new_format
;
586 r300_setup_texture_state(r300_screen(screen
), (struct r300_texture
*)tex
);
589 unsigned r300_texture_get_offset(struct r300_texture
* tex
, unsigned level
,
590 unsigned zslice
, unsigned face
)
592 unsigned offset
= tex
->offset
[level
];
594 switch (tex
->tex
.target
) {
595 case PIPE_TEXTURE_3D
:
597 return offset
+ zslice
* tex
->layer_size
[level
];
599 case PIPE_TEXTURE_CUBE
:
601 return offset
+ face
* tex
->layer_size
[level
];
604 assert(zslice
== 0 && face
== 0);
610 * Return the width (dim==TILE_WIDTH) or height (dim==TILE_HEIGHT) of one tile
611 * of the given texture.
613 static unsigned r300_texture_get_tile_size(struct r300_texture
* tex
,
614 int dim
, boolean macrotile
)
616 unsigned pixsize
, tile_size
;
618 pixsize
= util_format_get_blocksize(tex
->tex
.format
);
619 tile_size
= microblock_table
[util_logbase2(pixsize
)][tex
->microtile
][dim
];
629 /* Return true if macrotiling should be enabled on the miplevel. */
630 static boolean
r300_texture_macro_switch(struct r300_texture
*tex
,
635 unsigned tile
, texdim
;
637 tile
= r300_texture_get_tile_size(tex
, dim
, TRUE
);
638 if (dim
== TILE_WIDTH
) {
639 texdim
= u_minify(tex
->tex
.width0
, level
);
641 texdim
= u_minify(tex
->tex
.height0
, level
);
644 /* See TX_FILTER1_n.MACRO_SWITCH. */
646 return texdim
>= tile
;
648 return texdim
> tile
;
653 * Return the stride, in bytes, of the texture images of the given texture
654 * at the given level.
656 unsigned r300_texture_get_stride(struct r300_screen
* screen
,
657 struct r300_texture
* tex
, unsigned level
)
659 unsigned tile_width
, width
;
661 if (tex
->stride_override
)
662 return tex
->stride_override
;
664 /* Check the level. */
665 if (level
> tex
->tex
.last_level
) {
666 SCREEN_DBG(screen
, DBG_TEX
, "%s: level (%u) > last_level (%u)\n",
667 __FUNCTION__
, level
, tex
->tex
.last_level
);
671 width
= u_minify(tex
->tex
.width0
, level
);
673 if (r300_format_is_plain(tex
->tex
.format
)) {
674 tile_width
= r300_texture_get_tile_size(tex
, TILE_WIDTH
,
675 tex
->mip_macrotile
[level
]);
676 width
= align(width
, tile_width
);
678 return util_format_get_stride(tex
->tex
.format
, width
);
680 return align(util_format_get_stride(tex
->tex
.format
, width
), 32);
684 static unsigned r300_texture_get_nblocksy(struct r300_texture
* tex
,
687 unsigned height
, tile_height
;
689 height
= u_minify(tex
->tex
.height0
, level
);
691 if (r300_format_is_plain(tex
->tex
.format
)) {
692 tile_height
= r300_texture_get_tile_size(tex
, TILE_HEIGHT
,
693 tex
->mip_macrotile
[level
]);
694 height
= align(height
, tile_height
);
697 return util_format_get_nblocksy(tex
->tex
.format
, height
);
700 static void r300_setup_miptree(struct r300_screen
* screen
,
701 struct r300_texture
* tex
)
703 struct pipe_texture
* base
= &tex
->tex
;
704 unsigned stride
, size
, layer_size
, nblocksy
, i
;
705 boolean rv350_mode
= screen
->caps
->family
>= CHIP_FAMILY_RV350
;
707 SCREEN_DBG(screen
, DBG_TEX
, "r300: Making miptree for texture, format %s\n",
708 util_format_name(base
->format
));
710 for (i
= 0; i
<= base
->last_level
; i
++) {
711 /* Let's see if this miplevel can be macrotiled. */
712 tex
->mip_macrotile
[i
] =
713 (tex
->macrotile
== R300_BUFFER_TILED
&&
714 r300_texture_macro_switch(tex
, i
, rv350_mode
, TILE_WIDTH
) &&
715 r300_texture_macro_switch(tex
, i
, rv350_mode
, TILE_HEIGHT
)) ?
716 R300_BUFFER_TILED
: R300_BUFFER_LINEAR
;
718 stride
= r300_texture_get_stride(screen
, tex
, i
);
719 nblocksy
= r300_texture_get_nblocksy(tex
, i
);
720 layer_size
= stride
* nblocksy
;
722 if (base
->target
== PIPE_TEXTURE_CUBE
)
723 size
= layer_size
* 6;
725 size
= layer_size
* u_minify(base
->depth0
, i
);
727 tex
->offset
[i
] = tex
->size
;
728 tex
->size
= tex
->offset
[i
] + size
;
729 tex
->layer_size
[i
] = layer_size
;
730 tex
->pitch
[i
] = stride
/ util_format_get_blocksize(base
->format
);
732 SCREEN_DBG(screen
, DBG_TEX
, "r300: Texture miptree: Level %d "
733 "(%dx%dx%d px, pitch %d bytes) %d bytes total, macrotiled %s\n",
734 i
, u_minify(base
->width0
, i
), u_minify(base
->height0
, i
),
735 u_minify(base
->depth0
, i
), stride
, tex
->size
,
736 tex
->mip_macrotile
[i
] ? "TRUE" : "FALSE");
740 static void r300_setup_flags(struct r300_texture
* tex
)
742 tex
->is_npot
= !util_is_power_of_two(tex
->tex
.width0
) ||
743 !util_is_power_of_two(tex
->tex
.height0
);
746 static void r300_setup_tiling(struct pipe_screen
*screen
,
747 struct r300_texture
*tex
)
749 struct r300_winsys_screen
*rws
= (struct r300_winsys_screen
*)screen
->winsys
;
750 enum pipe_format format
= tex
->tex
.format
;
751 boolean rv350_mode
= r300_screen(screen
)->caps
->family
>= CHIP_FAMILY_RV350
;
753 if (!r300_format_is_plain(format
)) {
757 if (tex
->tex
.width0
== 1 ||
758 tex
->tex
.height0
== 1) {
762 /* Set microtiling. */
763 switch (util_format_get_blocksize(format
)) {
766 tex
->microtile
= R300_BUFFER_TILED
;
771 if (rws
->get_value(rws
, R300_VID_SQUARE_TILING_SUPPORT
)) {
772 tex
->microtile
= R300_BUFFER_SQUARETILED
;
777 /* Set macrotiling. */
778 if (r300_texture_macro_switch(tex
, 0, rv350_mode
, TILE_WIDTH
) &&
779 r300_texture_macro_switch(tex
, 0, rv350_mode
, TILE_HEIGHT
)) {
780 tex
->macrotile
= R300_BUFFER_TILED
;
784 /* Create a new texture. */
785 static struct pipe_texture
* r300_texture_create(struct pipe_screen
* screen
,
786 const struct pipe_texture
* template)
788 struct r300_texture
* tex
= CALLOC_STRUCT(r300_texture
);
789 struct r300_screen
* rscreen
= r300_screen(screen
);
790 struct r300_winsys_screen
*rws
= (struct r300_winsys_screen
*)screen
->winsys
;
796 tex
->tex
= *template;
797 pipe_reference_init(&tex
->tex
.reference
, 1);
798 tex
->tex
.screen
= screen
;
800 r300_setup_flags(tex
);
801 if (!(template->tex_usage
& R300_TEXTURE_USAGE_TRANSFER
)) {
802 r300_setup_tiling(screen
, tex
);
804 r300_setup_miptree(rscreen
, tex
);
805 r300_setup_texture_state(rscreen
, tex
);
807 tex
->buffer
= rws
->buffer_create(rws
, 2048,
808 PIPE_BUFFER_USAGE_PIXEL
,
810 rws
->buffer_set_tiling(rws
, tex
->buffer
,
820 return (struct pipe_texture
*)tex
;
823 static void r300_texture_destroy(struct pipe_texture
* texture
)
825 struct r300_texture
* tex
= (struct r300_texture
*)texture
;
826 struct r300_winsys_screen
*rws
= (struct r300_winsys_screen
*)texture
->screen
->winsys
;
828 rws
->buffer_reference(rws
, &tex
->buffer
, NULL
);
832 static struct pipe_surface
* r300_get_tex_surface(struct pipe_screen
* screen
,
833 struct pipe_texture
* texture
,
839 struct r300_texture
* tex
= (struct r300_texture
*)texture
;
840 struct pipe_surface
* surface
= CALLOC_STRUCT(pipe_surface
);
843 offset
= r300_texture_get_offset(tex
, level
, zslice
, face
);
846 pipe_reference_init(&surface
->reference
, 1);
847 pipe_texture_reference(&surface
->texture
, texture
);
848 surface
->format
= texture
->format
;
849 surface
->width
= u_minify(texture
->width0
, level
);
850 surface
->height
= u_minify(texture
->height0
, level
);
851 surface
->offset
= offset
;
852 surface
->usage
= flags
;
853 surface
->zslice
= zslice
;
854 surface
->texture
= texture
;
855 surface
->face
= face
;
856 surface
->level
= level
;
862 static void r300_tex_surface_destroy(struct pipe_surface
* s
)
864 pipe_texture_reference(&s
->texture
, NULL
);
869 static struct pipe_texture
*
870 r300_texture_from_handle(struct pipe_screen
* screen
,
871 const struct pipe_texture
* base
,
872 struct winsys_handle
*whandle
)
874 struct r300_winsys_screen
*rws
= (struct r300_winsys_screen
*)screen
->winsys
;
875 struct r300_screen
* rscreen
= r300_screen(screen
);
876 struct r300_winsys_buffer
*buffer
;
877 struct r300_texture
* tex
;
880 /* Support only 2D textures without mipmaps */
881 if (base
->target
!= PIPE_TEXTURE_2D
||
883 base
->last_level
!= 0) {
887 buffer
= rws
->buffer_from_handle(rws
, screen
, whandle
, &stride
);
892 tex
= CALLOC_STRUCT(r300_texture
);
898 pipe_reference_init(&tex
->tex
.reference
, 1);
899 tex
->tex
.screen
= screen
;
901 tex
->stride_override
= stride
;
902 tex
->pitch
[0] = stride
/ util_format_get_blocksize(base
->format
);
904 r300_setup_flags(tex
);
905 r300_setup_texture_state(rscreen
, tex
);
907 /* one ref already taken */
908 tex
->buffer
= buffer
;
910 return (struct pipe_texture
*)tex
;
914 r300_texture_get_handle(struct pipe_screen
* screen
,
915 struct pipe_texture
*texture
,
916 struct winsys_handle
*whandle
)
918 struct r300_winsys_screen
*rws
= (struct r300_winsys_screen
*)screen
->winsys
;
919 struct r300_texture
* tex
= (struct r300_texture
*)texture
;
926 stride
= r300_texture_get_stride(r300_screen(screen
), tex
, 0);
928 rws
->buffer_get_handle(rws
, tex
->buffer
, stride
, whandle
);
933 static struct pipe_video_surface
*
934 r300_video_surface_create(struct pipe_screen
*screen
,
935 enum pipe_video_chroma_format chroma_format
,
936 unsigned width
, unsigned height
)
938 struct r300_video_surface
*r300_vsfc
;
939 struct pipe_texture
template;
942 assert(width
&& height
);
944 r300_vsfc
= CALLOC_STRUCT(r300_video_surface
);
948 pipe_reference_init(&r300_vsfc
->base
.reference
, 1);
949 r300_vsfc
->base
.screen
= screen
;
950 r300_vsfc
->base
.chroma_format
= chroma_format
;
951 r300_vsfc
->base
.width
= width
;
952 r300_vsfc
->base
.height
= height
;
954 memset(&template, 0, sizeof(struct pipe_texture
));
955 template.target
= PIPE_TEXTURE_2D
;
956 template.format
= PIPE_FORMAT_B8G8R8X8_UNORM
;
957 template.last_level
= 0;
958 template.width0
= util_next_power_of_two(width
);
959 template.height0
= util_next_power_of_two(height
);
961 template.tex_usage
= PIPE_TEXTURE_USAGE_SAMPLER
|
962 PIPE_TEXTURE_USAGE_RENDER_TARGET
;
964 r300_vsfc
->tex
= screen
->texture_create(screen
, &template);
971 return &r300_vsfc
->base
;
974 static void r300_video_surface_destroy(struct pipe_video_surface
*vsfc
)
976 struct r300_video_surface
*r300_vsfc
= r300_video_surface(vsfc
);
977 pipe_texture_reference(&r300_vsfc
->tex
, NULL
);
981 void r300_init_screen_texture_functions(struct pipe_screen
* screen
)
983 screen
->texture_create
= r300_texture_create
;
984 screen
->texture_from_handle
= r300_texture_from_handle
;
985 screen
->texture_get_handle
= r300_texture_get_handle
;
986 screen
->texture_destroy
= r300_texture_destroy
;
987 screen
->get_tex_surface
= r300_get_tex_surface
;
988 screen
->tex_surface_destroy
= r300_tex_surface_destroy
;
990 screen
->video_surface_create
= r300_video_surface_create
;
991 screen
->video_surface_destroy
= r300_video_surface_destroy
;
994 boolean
r300_get_texture_buffer(struct pipe_screen
* screen
,
995 struct pipe_texture
* texture
,
996 struct r300_winsys_buffer
** buffer
,
999 struct r300_texture
* tex
= (struct r300_texture
*)texture
;
1000 struct r300_winsys_screen
*rws
= (struct r300_winsys_screen
*)screen
->winsys
;
1001 struct r300_winsys_buffer
*buf
;
1007 rws
->buffer_reference(rws
, &buf
, tex
->buffer
);
1010 *stride
= r300_texture_get_stride(r300_screen(screen
), tex
, 0);