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"
35 #include "radeon_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 /* Translate a pipe_format into a useful texture format for sampling.
51 * Some special formats are translated directly using R300_EASY_TX_FORMAT,
52 * but the majority of them is translated in a generic way, automatically
53 * supporting all the formats hw can support.
55 * R300_EASY_TX_FORMAT swizzles the texture.
56 * Note the signature of R300_EASY_TX_FORMAT:
57 * R300_EASY_TX_FORMAT(B, G, R, A, FORMAT);
59 * The FORMAT specifies how the texture sampler will treat the texture, and
60 * makes available X, Y, Z, W, ZERO, and ONE for swizzling. */
61 static uint32_t r300_translate_texformat(enum pipe_format format
)
64 const struct util_format_description
*desc
;
65 unsigned components
= 0, i
;
66 boolean uniform
= TRUE
;
67 const uint32_t swizzle_shift
[4] = {
68 R300_TX_FORMAT_R_SHIFT
,
69 R300_TX_FORMAT_G_SHIFT
,
70 R300_TX_FORMAT_B_SHIFT
,
71 R300_TX_FORMAT_A_SHIFT
73 const uint32_t swizzle
[4] = {
79 const uint32_t sign_bit
[4] = {
80 R300_TX_FORMAT_SIGNED_X
,
81 R300_TX_FORMAT_SIGNED_Y
,
82 R300_TX_FORMAT_SIGNED_Z
,
83 R300_TX_FORMAT_SIGNED_W
,
86 desc
= util_format_description(format
);
88 /* Colorspace (return non-RGB formats directly). */
89 switch (desc
->colorspace
) {
90 /* Depth stencil formats. */
91 case UTIL_FORMAT_COLORSPACE_ZS
:
93 case PIPE_FORMAT_Z16_UNORM
:
94 return R300_EASY_TX_FORMAT(X
, X
, X
, X
, X16
);
95 case PIPE_FORMAT_X8Z24_UNORM
:
96 case PIPE_FORMAT_S8Z24_UNORM
:
97 return R300_EASY_TX_FORMAT(X
, X
, X
, X
, W24_FP
);
99 return ~0; /* Unsupported. */
103 case UTIL_FORMAT_COLORSPACE_YUV
:
104 result
|= R300_TX_FORMAT_YUV_TO_RGB
;
107 case PIPE_FORMAT_UYVY
:
108 return R300_EASY_TX_FORMAT(X
, Y
, Z
, ONE
, YVYU422
) | result
;
109 case PIPE_FORMAT_YUYV
:
110 return R300_EASY_TX_FORMAT(X
, Y
, Z
, ONE
, VYUY422
) | result
;
112 return ~0; /* Unsupported/unknown. */
115 /* Add gamma correction. */
116 case UTIL_FORMAT_COLORSPACE_SRGB
:
117 result
|= R300_TX_FORMAT_GAMMA
;
124 for (i
= 0; i
< 4; i
++) {
125 switch (desc
->swizzle
[i
]) {
126 case UTIL_FORMAT_SWIZZLE_X
:
127 case UTIL_FORMAT_SWIZZLE_NONE
:
128 result
|= swizzle
[0] << swizzle_shift
[i
];
130 case UTIL_FORMAT_SWIZZLE_Y
:
131 result
|= swizzle
[1] << swizzle_shift
[i
];
133 case UTIL_FORMAT_SWIZZLE_Z
:
134 result
|= swizzle
[2] << swizzle_shift
[i
];
136 case UTIL_FORMAT_SWIZZLE_W
:
137 result
|= swizzle
[3] << swizzle_shift
[i
];
139 case UTIL_FORMAT_SWIZZLE_0
:
140 result
|= R300_TX_FORMAT_ZERO
<< swizzle_shift
[i
];
142 case UTIL_FORMAT_SWIZZLE_1
:
143 result
|= R300_TX_FORMAT_ONE
<< swizzle_shift
[i
];
146 return ~0; /* Unsupported. */
150 /* Compressed formats. */
151 if (desc
->layout
== UTIL_FORMAT_LAYOUT_COMPRESSED
) {
153 case PIPE_FORMAT_DXT1_RGB
:
154 case PIPE_FORMAT_DXT1_RGBA
:
155 case PIPE_FORMAT_DXT1_SRGB
:
156 case PIPE_FORMAT_DXT1_SRGBA
:
157 return R300_TX_FORMAT_DXT1
| result
;
158 case PIPE_FORMAT_DXT3_RGBA
:
159 case PIPE_FORMAT_DXT3_SRGBA
:
160 return R300_TX_FORMAT_DXT3
| result
;
161 case PIPE_FORMAT_DXT5_RGBA
:
162 case PIPE_FORMAT_DXT5_SRGBA
:
163 return R300_TX_FORMAT_DXT5
| result
;
165 return ~0; /* Unsupported/unknown. */
169 /* Get the number of components. */
170 for (i
= 0; i
< 4; i
++) {
171 if (desc
->channel
[i
].type
!= UTIL_FORMAT_TYPE_VOID
) {
177 for (i
= 0; i
< components
; i
++) {
178 if (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_SIGNED
) {
179 result
|= sign_bit
[i
];
183 /* See whether the components are of the same size. */
184 for (i
= 1; i
< components
; i
++) {
185 uniform
= uniform
&& desc
->channel
[0].size
== desc
->channel
[i
].size
;
188 /* Non-uniform formats. */
190 switch (components
) {
192 if (desc
->channel
[0].size
== 5 &&
193 desc
->channel
[1].size
== 6 &&
194 desc
->channel
[2].size
== 5) {
195 return R300_TX_FORMAT_Z5Y6X5
| result
;
197 if (desc
->channel
[0].size
== 5 &&
198 desc
->channel
[1].size
== 5 &&
199 desc
->channel
[2].size
== 6) {
200 return R300_TX_FORMAT_Z6Y5X5
| result
;
202 return ~0; /* Unsupported/unknown. */
205 if (desc
->channel
[0].size
== 5 &&
206 desc
->channel
[1].size
== 5 &&
207 desc
->channel
[2].size
== 5 &&
208 desc
->channel
[3].size
== 1) {
209 return R300_TX_FORMAT_W1Z5Y5X5
| result
;
211 if (desc
->channel
[0].size
== 10 &&
212 desc
->channel
[1].size
== 10 &&
213 desc
->channel
[2].size
== 10 &&
214 desc
->channel
[3].size
== 2) {
215 return R300_TX_FORMAT_W2Z10Y10X10
| result
;
218 return ~0; /* Unsupported/unknown. */
221 /* And finally, uniform formats. */
222 switch (desc
->channel
[0].type
) {
223 case UTIL_FORMAT_TYPE_UNSIGNED
:
224 case UTIL_FORMAT_TYPE_SIGNED
:
225 if (!desc
->channel
[0].normalized
&&
226 desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_SRGB
) {
230 switch (desc
->channel
[0].size
) {
232 switch (components
) {
234 return R300_TX_FORMAT_Y4X4
| result
;
236 return R300_TX_FORMAT_W4Z4Y4X4
| result
;
241 switch (components
) {
243 return R300_TX_FORMAT_X8
| result
;
245 return R300_TX_FORMAT_Y8X8
| result
;
247 return R300_TX_FORMAT_W8Z8Y8X8
| result
;
252 switch (components
) {
254 return R300_TX_FORMAT_X16
| result
;
256 return R300_TX_FORMAT_Y16X16
| result
;
258 return R300_TX_FORMAT_W16Z16Y16X16
| result
;
263 /* XXX Enable float textures here. */
265 case UTIL_FORMAT_TYPE_FLOAT
:
266 switch (desc
->channel
[0].size
) {
268 switch (components
) {
270 return R300_TX_FORMAT_16F
| result
;
272 return R300_TX_FORMAT_16F_16F
| result
;
274 return R300_TX_FORMAT_16F_16F_16F_16F
| result
;
279 switch (components
) {
281 return R300_TX_FORMAT_32F
| result
;
283 return R300_TX_FORMAT_32F_32F
| result
;
285 return R300_TX_FORMAT_32F_32F_32F_32F
| result
;
291 return ~0; /* Unsupported/unknown. */
294 /* Buffer formats. */
296 /* Colorbuffer formats. This is the unswizzled format of the RB3D block's
297 * output. For the swizzling of the targets, check the shader's format. */
298 static uint32_t r300_translate_colorformat(enum pipe_format format
)
302 case PIPE_FORMAT_A8_UNORM
:
303 case PIPE_FORMAT_I8_UNORM
:
304 case PIPE_FORMAT_L8_UNORM
:
305 case PIPE_FORMAT_L8_SRGB
:
306 case PIPE_FORMAT_R8_UNORM
:
307 case PIPE_FORMAT_R8_SNORM
:
308 return R300_COLOR_FORMAT_I8
;
310 /* 16-bit buffers. */
311 case PIPE_FORMAT_B5G6R5_UNORM
:
312 return R300_COLOR_FORMAT_RGB565
;
313 case PIPE_FORMAT_B5G5R5A1_UNORM
:
314 return R300_COLOR_FORMAT_ARGB1555
;
315 case PIPE_FORMAT_B4G4R4A4_UNORM
:
316 return R300_COLOR_FORMAT_ARGB4444
;
318 /* 32-bit buffers. */
319 case PIPE_FORMAT_B8G8R8A8_UNORM
:
320 case PIPE_FORMAT_B8G8R8A8_SRGB
:
321 case PIPE_FORMAT_B8G8R8X8_UNORM
:
322 case PIPE_FORMAT_B8G8R8X8_SRGB
:
323 case PIPE_FORMAT_A8R8G8B8_UNORM
:
324 case PIPE_FORMAT_A8R8G8B8_SRGB
:
325 case PIPE_FORMAT_X8R8G8B8_UNORM
:
326 case PIPE_FORMAT_X8R8G8B8_SRGB
:
327 case PIPE_FORMAT_A8B8G8R8_UNORM
:
328 case PIPE_FORMAT_R8G8B8A8_SNORM
:
329 case PIPE_FORMAT_A8B8G8R8_SRGB
:
330 case PIPE_FORMAT_X8B8G8R8_UNORM
:
331 case PIPE_FORMAT_X8B8G8R8_SRGB
:
332 case PIPE_FORMAT_R8SG8SB8UX8U_NORM
:
333 return R300_COLOR_FORMAT_ARGB8888
;
334 case PIPE_FORMAT_R10G10B10A2_UNORM
:
335 return R500_COLOR_FORMAT_ARGB2101010
; /* R5xx-only? */
337 /* 64-bit buffers. */
338 case PIPE_FORMAT_R16G16B16A16_UNORM
:
339 case PIPE_FORMAT_R16G16B16A16_SNORM
:
340 //case PIPE_FORMAT_R16G16B16A16_FLOAT: /* not in pipe_format */
341 return R300_COLOR_FORMAT_ARGB16161616
;
343 /* XXX Enable float textures here. */
345 /* 128-bit buffers. */
346 case PIPE_FORMAT_R32G32B32A32_FLOAT
:
347 return R300_COLOR_FORMAT_ARGB32323232
;
351 case PIPE_FORMAT_UYVY
:
352 return R300_COLOR_FORMAT_YVYU
;
353 case PIPE_FORMAT_YUYV
:
354 return R300_COLOR_FORMAT_VYUY
;
356 return ~0; /* Unsupported. */
360 /* Depthbuffer and stencilbuffer. Thankfully, we only support two flavors. */
361 static uint32_t r300_translate_zsformat(enum pipe_format format
)
364 /* 16-bit depth, no stencil */
365 case PIPE_FORMAT_Z16_UNORM
:
366 return R300_DEPTHFORMAT_16BIT_INT_Z
;
367 /* 24-bit depth, ignored stencil */
368 case PIPE_FORMAT_X8Z24_UNORM
:
369 /* 24-bit depth, 8-bit stencil */
370 case PIPE_FORMAT_S8Z24_UNORM
:
371 return R300_DEPTHFORMAT_24BIT_INT_Z_8BIT_STENCIL
;
373 return ~0; /* Unsupported. */
377 /* Shader output formats. This is essentially the swizzle from the shader
380 * Note that formats are stored from C3 to C0. */
381 static uint32_t r300_translate_out_fmt(enum pipe_format format
)
383 uint32_t modifier
= 0;
385 const struct util_format_description
*desc
;
386 static const uint32_t sign_bit
[4] = {
393 desc
= util_format_description(format
);
395 /* Specifies how the shader output is written to the fog unit. */
396 if (desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) {
397 /* The gamma correction causes precision loss so we need
398 * higher precision to maintain reasonable quality.
399 * It has nothing to do with the colorbuffer format. */
400 modifier
|= R300_US_OUT_FMT_C4_10_GAMMA
;
401 } else if (desc
->channel
[0].type
== UTIL_FORMAT_TYPE_FLOAT
) {
402 if (desc
->channel
[0].size
== 32) {
403 modifier
|= R300_US_OUT_FMT_C4_32_FP
;
405 modifier
|= R300_US_OUT_FMT_C4_16_FP
;
408 if (desc
->channel
[0].size
== 16) {
409 modifier
|= R300_US_OUT_FMT_C4_16
;
411 /* C4_8 seems to be used for the formats whose pixel size
413 modifier
|= R300_US_OUT_FMT_C4_8
;
418 for (i
= 0; i
< 4; i
++)
419 if (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_SIGNED
) {
420 modifier
|= sign_bit
[i
];
423 /* Add swizzles and return. */
426 * COLORFORMAT_I8 stores the C2 component. */
427 case PIPE_FORMAT_A8_UNORM
:
428 return modifier
| R300_C2_SEL_A
;
429 case PIPE_FORMAT_I8_UNORM
:
430 case PIPE_FORMAT_L8_UNORM
:
431 case PIPE_FORMAT_L8_SRGB
:
432 case PIPE_FORMAT_R8_UNORM
:
433 case PIPE_FORMAT_R8_SNORM
:
434 return modifier
| R300_C2_SEL_R
;
436 /* ARGB 32-bit outputs. */
437 case PIPE_FORMAT_B5G6R5_UNORM
:
438 case PIPE_FORMAT_B5G5R5A1_UNORM
:
439 case PIPE_FORMAT_B4G4R4A4_UNORM
:
440 case PIPE_FORMAT_B8G8R8A8_UNORM
:
441 case PIPE_FORMAT_B8G8R8A8_SRGB
:
442 case PIPE_FORMAT_B8G8R8X8_UNORM
:
443 case PIPE_FORMAT_B8G8R8X8_SRGB
:
445 R300_C0_SEL_B
| R300_C1_SEL_G
|
446 R300_C2_SEL_R
| R300_C3_SEL_A
;
448 /* BGRA 32-bit outputs. */
449 case PIPE_FORMAT_A8R8G8B8_UNORM
:
450 case PIPE_FORMAT_A8R8G8B8_SRGB
:
451 case PIPE_FORMAT_X8R8G8B8_UNORM
:
452 case PIPE_FORMAT_X8R8G8B8_SRGB
:
454 R300_C0_SEL_A
| R300_C1_SEL_R
|
455 R300_C2_SEL_G
| R300_C3_SEL_B
;
457 /* RGBA 32-bit outputs. */
458 case PIPE_FORMAT_A8B8G8R8_UNORM
:
459 case PIPE_FORMAT_R8G8B8A8_SNORM
:
460 case PIPE_FORMAT_A8B8G8R8_SRGB
:
461 case PIPE_FORMAT_X8B8G8R8_UNORM
:
462 case PIPE_FORMAT_X8B8G8R8_SRGB
:
464 R300_C0_SEL_A
| R300_C1_SEL_B
|
465 R300_C2_SEL_G
| R300_C3_SEL_R
;
467 /* ABGR 32-bit outputs. */
468 case PIPE_FORMAT_R8SG8SB8UX8U_NORM
:
469 case PIPE_FORMAT_R10G10B10A2_UNORM
:
470 /* RGBA high precision outputs (same swizzles as ABGR low precision) */
471 case PIPE_FORMAT_R16G16B16A16_UNORM
:
472 case PIPE_FORMAT_R16G16B16A16_SNORM
:
473 //case PIPE_FORMAT_R16G16B16A16_FLOAT: /* not in pipe_format */
474 case PIPE_FORMAT_R32G32B32A32_FLOAT
:
476 R300_C0_SEL_R
| R300_C1_SEL_G
|
477 R300_C2_SEL_B
| R300_C3_SEL_A
;
480 return ~0; /* Unsupported. */
484 boolean
r300_is_colorbuffer_format_supported(enum pipe_format format
)
486 return r300_translate_colorformat(format
) != ~0 &&
487 r300_translate_out_fmt(format
) != ~0;
490 boolean
r300_is_zs_format_supported(enum pipe_format format
)
492 return r300_translate_zsformat(format
) != ~0;
495 boolean
r300_is_sampler_format_supported(enum pipe_format format
)
497 return r300_translate_texformat(format
) != ~0;
500 static void r300_setup_texture_state(struct r300_screen
* screen
, struct r300_texture
* tex
)
502 struct r300_texture_format_state
* state
= &tex
->state
;
503 struct pipe_texture
*pt
= &tex
->tex
;
505 boolean is_r500
= screen
->caps
->is_r500
;
507 /* Set sampler state. */
508 state
->format0
= R300_TX_WIDTH((pt
->width0
- 1) & 0x7ff) |
509 R300_TX_HEIGHT((pt
->height0
- 1) & 0x7ff);
512 /* rectangles love this */
513 state
->format0
|= R300_TX_PITCH_EN
;
514 state
->format2
= (tex
->pitch
[0] - 1) & 0x1fff;
516 /* power of two textures (3D, mipmaps, and no pitch) */
517 state
->format0
|= R300_TX_DEPTH(util_logbase2(pt
->depth0
) & 0xf);
520 state
->format1
= r300_translate_texformat(pt
->format
);
521 if (pt
->target
== PIPE_TEXTURE_CUBE
) {
522 state
->format1
|= R300_TX_FORMAT_CUBIC_MAP
;
524 if (pt
->target
== PIPE_TEXTURE_3D
) {
525 state
->format1
|= R300_TX_FORMAT_3D
;
528 /* large textures on r500 */
531 if (pt
->width0
> 2048) {
532 state
->format2
|= R500_TXWIDTH_BIT11
;
534 if (pt
->height0
> 2048) {
535 state
->format2
|= R500_TXHEIGHT_BIT11
;
539 SCREEN_DBG(screen
, DBG_TEX
, "r300: Set texture state (%dx%d, %d levels)\n",
540 pt
->width0
, pt
->height0
, pt
->last_level
);
542 /* Set framebuffer state. */
543 if (util_format_is_depth_or_stencil(tex
->tex
.format
)) {
544 for (i
= 0; i
<= tex
->tex
.last_level
; i
++) {
545 tex
->fb_state
.depthpitch
[i
] =
547 R300_DEPTHMACROTILE(tex
->mip_macrotile
[i
]) |
548 R300_DEPTHMICROTILE(tex
->microtile
);
550 tex
->fb_state
.zb_format
= r300_translate_zsformat(tex
->tex
.format
);
552 for (i
= 0; i
<= tex
->tex
.last_level
; i
++) {
553 tex
->fb_state
.colorpitch
[i
] =
555 r300_translate_colorformat(tex
->tex
.format
) |
556 R300_COLOR_TILE(tex
->mip_macrotile
[i
]) |
557 R300_COLOR_MICROTILE(tex
->microtile
);
559 tex
->fb_state
.us_out_fmt
= r300_translate_out_fmt(tex
->tex
.format
);
563 void r300_texture_reinterpret_format(struct pipe_screen
*screen
,
564 struct pipe_texture
*tex
,
565 enum pipe_format new_format
)
567 struct r300_screen
*r300screen
= r300_screen(screen
);
569 SCREEN_DBG(r300screen
, DBG_TEX
, "r300: Reinterpreting format: %s -> %s\n",
570 util_format_name(tex
->format
), util_format_name(new_format
));
572 tex
->format
= new_format
;
574 r300_setup_texture_state(r300_screen(screen
), (struct r300_texture
*)tex
);
577 unsigned r300_texture_get_offset(struct r300_texture
* tex
, unsigned level
,
578 unsigned zslice
, unsigned face
)
580 unsigned offset
= tex
->offset
[level
];
582 switch (tex
->tex
.target
) {
583 case PIPE_TEXTURE_3D
:
585 return offset
+ zslice
* tex
->layer_size
[level
];
587 case PIPE_TEXTURE_CUBE
:
589 return offset
+ face
* tex
->layer_size
[level
];
592 assert(zslice
== 0 && face
== 0);
598 * Return the width (dim==TILE_WIDTH) or height (dim==TILE_HEIGHT) of one tile
599 * of the given texture.
601 static unsigned r300_texture_get_tile_size(struct r300_texture
* tex
,
602 int dim
, boolean macrotile
)
604 unsigned pixsize
, tile_size
;
606 pixsize
= util_format_get_blocksize(tex
->tex
.format
);
607 tile_size
= microblock_table
[util_logbase2(pixsize
)][tex
->microtile
][dim
];
617 /* Return true if macrotiling should be enabled on the miplevel. */
618 static boolean
r300_texture_macro_switch(struct r300_texture
*tex
,
623 unsigned tile
, texdim
;
625 tile
= r300_texture_get_tile_size(tex
, dim
, TRUE
);
626 if (dim
== TILE_WIDTH
) {
627 texdim
= u_minify(tex
->tex
.width0
, level
);
629 texdim
= u_minify(tex
->tex
.height0
, level
);
632 /* See TX_FILTER1_n.MACRO_SWITCH. */
634 return texdim
>= tile
;
636 return texdim
> tile
;
641 * Return the stride, in bytes, of the texture images of the given texture
642 * at the given level.
644 unsigned r300_texture_get_stride(struct r300_screen
* screen
,
645 struct r300_texture
* tex
, unsigned level
)
647 unsigned tile_width
, width
;
649 if (tex
->stride_override
)
650 return tex
->stride_override
;
652 /* Check the level. */
653 if (level
> tex
->tex
.last_level
) {
654 SCREEN_DBG(screen
, DBG_TEX
, "%s: level (%u) > last_level (%u)\n",
655 __FUNCTION__
, level
, tex
->tex
.last_level
);
659 width
= u_minify(tex
->tex
.width0
, level
);
661 if (!util_format_is_compressed(tex
->tex
.format
)) {
662 tile_width
= r300_texture_get_tile_size(tex
, TILE_WIDTH
,
663 tex
->mip_macrotile
[level
]);
664 width
= align(width
, tile_width
);
666 return util_format_get_stride(tex
->tex
.format
, width
);
668 return align(util_format_get_stride(tex
->tex
.format
, width
), 32);
672 static unsigned r300_texture_get_nblocksy(struct r300_texture
* tex
,
675 unsigned height
, tile_height
;
677 height
= u_minify(tex
->tex
.height0
, level
);
679 if (!util_format_is_compressed(tex
->tex
.format
)) {
680 tile_height
= r300_texture_get_tile_size(tex
, TILE_HEIGHT
,
681 tex
->mip_macrotile
[level
]);
682 height
= align(height
, tile_height
);
685 return util_format_get_nblocksy(tex
->tex
.format
, height
);
688 static void r300_setup_miptree(struct r300_screen
* screen
,
689 struct r300_texture
* tex
)
691 struct pipe_texture
* base
= &tex
->tex
;
692 unsigned stride
, size
, layer_size
, nblocksy
, i
;
693 boolean rv350_mode
= screen
->caps
->family
>= CHIP_FAMILY_RV350
;
695 SCREEN_DBG(screen
, DBG_TEX
, "r300: Making miptree for texture, format %s\n",
696 util_format_name(base
->format
));
698 for (i
= 0; i
<= base
->last_level
; i
++) {
699 /* Let's see if this miplevel can be macrotiled. */
700 tex
->mip_macrotile
[i
] =
701 (tex
->macrotile
== R300_BUFFER_TILED
&&
702 r300_texture_macro_switch(tex
, i
, rv350_mode
, TILE_WIDTH
)) ?
703 R300_BUFFER_TILED
: R300_BUFFER_LINEAR
;
705 stride
= r300_texture_get_stride(screen
, tex
, i
);
706 nblocksy
= r300_texture_get_nblocksy(tex
, i
);
707 layer_size
= stride
* nblocksy
;
709 if (base
->target
== PIPE_TEXTURE_CUBE
)
710 size
= layer_size
* 6;
712 size
= layer_size
* u_minify(base
->depth0
, i
);
714 tex
->offset
[i
] = tex
->size
;
715 tex
->size
= tex
->offset
[i
] + size
;
716 tex
->layer_size
[i
] = layer_size
;
717 tex
->pitch
[i
] = stride
/ util_format_get_blocksize(base
->format
);
719 SCREEN_DBG(screen
, DBG_TEX
, "r300: Texture miptree: Level %d "
720 "(%dx%dx%d px, pitch %d bytes) %d bytes total, macrotiled %s\n",
721 i
, u_minify(base
->width0
, i
), u_minify(base
->height0
, i
),
722 u_minify(base
->depth0
, i
), stride
, tex
->size
,
723 tex
->mip_macrotile
[i
] ? "TRUE" : "FALSE");
727 static void r300_setup_flags(struct r300_texture
* tex
)
729 tex
->is_npot
= !util_is_power_of_two(tex
->tex
.width0
) ||
730 !util_is_power_of_two(tex
->tex
.height0
);
733 static void r300_setup_tiling(struct pipe_screen
*screen
,
734 struct r300_texture
*tex
)
736 enum pipe_format format
= tex
->tex
.format
;
737 boolean rv350_mode
= r300_screen(screen
)->caps
->family
>= CHIP_FAMILY_RV350
;
739 if (util_format_is_compressed(format
)) {
743 if (tex
->tex
.width0
== 1 ||
744 tex
->tex
.height0
== 1) {
748 /* Set microtiling. */
749 switch (util_format_get_blocksize(format
)) {
752 tex
->microtile
= R300_BUFFER_TILED
;
755 /* XXX Square-tiling doesn't work with kernel older than 2.6.34,
756 * XXX need to check the DRM version */
759 tex->microtile = R300_BUFFER_SQUARETILED;
763 /* Set macrotiling. */
764 if (r300_texture_macro_switch(tex
, 0, rv350_mode
, TILE_WIDTH
) &&
765 r300_texture_macro_switch(tex
, 0, rv350_mode
, TILE_HEIGHT
)) {
766 tex
->macrotile
= R300_BUFFER_TILED
;
770 /* Create a new texture. */
771 static struct pipe_texture
* r300_texture_create(struct pipe_screen
* screen
,
772 const struct pipe_texture
* template)
774 struct r300_texture
* tex
= CALLOC_STRUCT(r300_texture
);
775 struct r300_screen
* rscreen
= r300_screen(screen
);
776 struct r300_winsys_screen
*rws
= (struct r300_winsys_screen
*)screen
->winsys
;
782 tex
->tex
= *template;
783 pipe_reference_init(&tex
->tex
.reference
, 1);
784 tex
->tex
.screen
= screen
;
786 r300_setup_flags(tex
);
787 if (!(template->tex_usage
& R300_TEXTURE_USAGE_TRANSFER
)) {
788 r300_setup_tiling(screen
, tex
);
790 r300_setup_miptree(rscreen
, tex
);
791 r300_setup_texture_state(rscreen
, tex
);
793 tex
->buffer
= rws
->buffer_create(rws
, 2048,
794 PIPE_BUFFER_USAGE_PIXEL
,
796 rws
->buffer_set_tiling(rws
, tex
->buffer
,
798 tex
->microtile
!= R300_BUFFER_LINEAR
,
799 tex
->macrotile
!= R300_BUFFER_LINEAR
);
806 return (struct pipe_texture
*)tex
;
809 static void r300_texture_destroy(struct pipe_texture
* texture
)
811 struct r300_texture
* tex
= (struct r300_texture
*)texture
;
812 struct r300_winsys_screen
*rws
= (struct r300_winsys_screen
*)texture
->screen
->winsys
;
814 rws
->buffer_reference(rws
, &tex
->buffer
, NULL
);
818 static struct pipe_surface
* r300_get_tex_surface(struct pipe_screen
* screen
,
819 struct pipe_texture
* texture
,
825 struct r300_texture
* tex
= (struct r300_texture
*)texture
;
826 struct pipe_surface
* surface
= CALLOC_STRUCT(pipe_surface
);
829 offset
= r300_texture_get_offset(tex
, level
, zslice
, face
);
832 pipe_reference_init(&surface
->reference
, 1);
833 pipe_texture_reference(&surface
->texture
, texture
);
834 surface
->format
= texture
->format
;
835 surface
->width
= u_minify(texture
->width0
, level
);
836 surface
->height
= u_minify(texture
->height0
, level
);
837 surface
->offset
= offset
;
838 surface
->usage
= flags
;
839 surface
->zslice
= zslice
;
840 surface
->texture
= texture
;
841 surface
->face
= face
;
842 surface
->level
= level
;
848 static void r300_tex_surface_destroy(struct pipe_surface
* s
)
850 pipe_texture_reference(&s
->texture
, NULL
);
855 static struct pipe_texture
*
856 r300_texture_from_handle(struct pipe_screen
* screen
,
857 const struct pipe_texture
* base
,
858 struct winsys_handle
*whandle
)
860 struct r300_winsys_screen
*rws
= (struct r300_winsys_screen
*)screen
->winsys
;
861 struct r300_screen
* rscreen
= r300_screen(screen
);
862 struct r300_winsys_buffer
*buffer
;
863 struct r300_texture
* tex
;
866 /* Support only 2D textures without mipmaps */
867 if (base
->target
!= PIPE_TEXTURE_2D
||
869 base
->last_level
!= 0) {
873 buffer
= rws
->buffer_from_handle(rws
, screen
, whandle
, &stride
);
878 tex
= CALLOC_STRUCT(r300_texture
);
884 pipe_reference_init(&tex
->tex
.reference
, 1);
885 tex
->tex
.screen
= screen
;
887 tex
->stride_override
= stride
;
888 tex
->pitch
[0] = stride
/ util_format_get_blocksize(base
->format
);
890 r300_setup_flags(tex
);
891 r300_setup_texture_state(rscreen
, tex
);
893 /* one ref already taken */
894 tex
->buffer
= buffer
;
896 return (struct pipe_texture
*)tex
;
900 r300_texture_get_handle(struct pipe_screen
* screen
,
901 struct pipe_texture
*texture
,
902 struct winsys_handle
*whandle
)
904 struct r300_winsys_screen
*rws
= (struct r300_winsys_screen
*)screen
->winsys
;
905 struct r300_texture
* tex
= (struct r300_texture
*)texture
;
912 stride
= r300_texture_get_stride(r300_screen(screen
), tex
, 0);
914 rws
->buffer_get_handle(rws
, tex
->buffer
, stride
, whandle
);
919 static struct pipe_video_surface
*
920 r300_video_surface_create(struct pipe_screen
*screen
,
921 enum pipe_video_chroma_format chroma_format
,
922 unsigned width
, unsigned height
)
924 struct r300_video_surface
*r300_vsfc
;
925 struct pipe_texture
template;
928 assert(width
&& height
);
930 r300_vsfc
= CALLOC_STRUCT(r300_video_surface
);
934 pipe_reference_init(&r300_vsfc
->base
.reference
, 1);
935 r300_vsfc
->base
.screen
= screen
;
936 r300_vsfc
->base
.chroma_format
= chroma_format
;
937 r300_vsfc
->base
.width
= width
;
938 r300_vsfc
->base
.height
= height
;
940 memset(&template, 0, sizeof(struct pipe_texture
));
941 template.target
= PIPE_TEXTURE_2D
;
942 template.format
= PIPE_FORMAT_B8G8R8X8_UNORM
;
943 template.last_level
= 0;
944 template.width0
= util_next_power_of_two(width
);
945 template.height0
= util_next_power_of_two(height
);
947 template.tex_usage
= PIPE_TEXTURE_USAGE_SAMPLER
|
948 PIPE_TEXTURE_USAGE_RENDER_TARGET
;
950 r300_vsfc
->tex
= screen
->texture_create(screen
, &template);
957 return &r300_vsfc
->base
;
960 static void r300_video_surface_destroy(struct pipe_video_surface
*vsfc
)
962 struct r300_video_surface
*r300_vsfc
= r300_video_surface(vsfc
);
963 pipe_texture_reference(&r300_vsfc
->tex
, NULL
);
967 void r300_init_screen_texture_functions(struct pipe_screen
* screen
)
969 screen
->texture_create
= r300_texture_create
;
970 screen
->texture_from_handle
= r300_texture_from_handle
;
971 screen
->texture_get_handle
= r300_texture_get_handle
;
972 screen
->texture_destroy
= r300_texture_destroy
;
973 screen
->get_tex_surface
= r300_get_tex_surface
;
974 screen
->tex_surface_destroy
= r300_tex_surface_destroy
;
976 screen
->video_surface_create
= r300_video_surface_create
;
977 screen
->video_surface_destroy
= r300_video_surface_destroy
;
980 boolean
r300_get_texture_buffer(struct pipe_screen
* screen
,
981 struct pipe_texture
* texture
,
982 struct r300_winsys_buffer
** buffer
,
985 struct r300_texture
* tex
= (struct r300_texture
*)texture
;
986 struct r300_winsys_screen
*rws
= (struct r300_winsys_screen
*)screen
->winsys
;
987 struct r300_winsys_buffer
*buf
;
993 rws
->buffer_reference(rws
, &buf
, tex
->buffer
);
996 *stride
= r300_texture_get_stride(r300_screen(screen
), tex
, 0);