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 sign_bit
[4] = {
74 R300_TX_FORMAT_SIGNED_X
,
75 R300_TX_FORMAT_SIGNED_Y
,
76 R300_TX_FORMAT_SIGNED_Z
,
77 R300_TX_FORMAT_SIGNED_W
,
80 desc
= util_format_description(format
);
82 /* Colorspace (return non-RGB formats directly). */
83 switch (desc
->colorspace
) {
84 /* Depth stencil formats. */
85 case UTIL_FORMAT_COLORSPACE_ZS
:
87 case PIPE_FORMAT_Z16_UNORM
:
88 return R300_EASY_TX_FORMAT(X
, X
, X
, X
, X16
);
89 case PIPE_FORMAT_Z24X8_UNORM
:
90 case PIPE_FORMAT_Z24S8_UNORM
:
91 return R300_EASY_TX_FORMAT(X
, X
, X
, X
, W24_FP
);
93 return ~0; /* Unsupported. */
97 case UTIL_FORMAT_COLORSPACE_YUV
:
98 result
|= R300_TX_FORMAT_YUV_TO_RGB
;
101 case PIPE_FORMAT_YCBCR
:
102 return R300_EASY_TX_FORMAT(X
, Y
, Z
, ONE
, YVYU422
) | result
;
103 case PIPE_FORMAT_YCBCR_REV
:
104 return R300_EASY_TX_FORMAT(X
, Y
, Z
, ONE
, VYUY422
) | result
;
106 return ~0; /* Unsupported/unknown. */
109 /* Add gamma correction. */
110 case UTIL_FORMAT_COLORSPACE_SRGB
:
111 result
|= R300_TX_FORMAT_GAMMA
;
118 for (i
= 0; i
< 4; i
++) {
119 switch (desc
->swizzle
[i
]) {
120 case UTIL_FORMAT_SWIZZLE_X
:
121 case UTIL_FORMAT_SWIZZLE_NONE
:
122 result
|= R300_TX_FORMAT_X
<< swizzle_shift
[i
];
124 case UTIL_FORMAT_SWIZZLE_Y
:
125 result
|= R300_TX_FORMAT_Y
<< swizzle_shift
[i
];
127 case UTIL_FORMAT_SWIZZLE_Z
:
128 result
|= R300_TX_FORMAT_Z
<< swizzle_shift
[i
];
130 case UTIL_FORMAT_SWIZZLE_W
:
131 result
|= R300_TX_FORMAT_W
<< swizzle_shift
[i
];
133 case UTIL_FORMAT_SWIZZLE_0
:
134 result
|= R300_TX_FORMAT_ZERO
<< swizzle_shift
[i
];
136 case UTIL_FORMAT_SWIZZLE_1
:
137 result
|= R300_TX_FORMAT_ONE
<< swizzle_shift
[i
];
140 return ~0; /* Unsupported. */
144 /* Compressed formats. */
145 if (desc
->layout
== UTIL_FORMAT_LAYOUT_DXT
) {
147 case PIPE_FORMAT_DXT1_RGB
:
148 case PIPE_FORMAT_DXT1_RGBA
:
149 case PIPE_FORMAT_DXT1_SRGB
:
150 case PIPE_FORMAT_DXT1_SRGBA
:
151 return R300_TX_FORMAT_DXT1
| result
;
152 case PIPE_FORMAT_DXT3_RGBA
:
153 case PIPE_FORMAT_DXT3_SRGBA
:
154 return R300_TX_FORMAT_DXT3
| result
;
155 case PIPE_FORMAT_DXT5_RGBA
:
156 case PIPE_FORMAT_DXT5_SRGBA
:
157 return R300_TX_FORMAT_DXT5
| result
;
159 return ~0; /* Unsupported/unknown. */
163 /* Get the number of components. */
164 for (i
= 0; i
< 4; i
++) {
165 if (desc
->channel
[i
].type
!= UTIL_FORMAT_TYPE_VOID
) {
171 for (i
= 0; i
< components
; i
++) {
172 if (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_SIGNED
) {
173 result
|= sign_bit
[i
];
177 /* See whether the components are of the same size. */
178 for (i
= 1; i
< components
; i
++) {
179 uniform
= uniform
&& desc
->channel
[0].size
== desc
->channel
[i
].size
;
182 /* Non-uniform formats. */
184 switch (components
) {
186 if (desc
->channel
[0].size
== 5 &&
187 desc
->channel
[1].size
== 6 &&
188 desc
->channel
[2].size
== 5) {
189 return R300_TX_FORMAT_Z5Y6X5
| result
;
191 if (desc
->channel
[0].size
== 5 &&
192 desc
->channel
[1].size
== 5 &&
193 desc
->channel
[2].size
== 6) {
194 return R300_TX_FORMAT_Z6Y5X5
| result
;
196 return ~0; /* Unsupported/unknown. */
199 if (desc
->channel
[0].size
== 5 &&
200 desc
->channel
[1].size
== 5 &&
201 desc
->channel
[2].size
== 5 &&
202 desc
->channel
[3].size
== 1) {
203 return R300_TX_FORMAT_W1Z5Y5X5
| result
;
205 if (desc
->channel
[0].size
== 10 &&
206 desc
->channel
[1].size
== 10 &&
207 desc
->channel
[2].size
== 10 &&
208 desc
->channel
[3].size
== 2) {
209 return R300_TX_FORMAT_W2Z10Y10X10
| result
;
212 return ~0; /* Unsupported/unknown. */
215 /* And finally, uniform formats. */
216 switch (desc
->channel
[0].type
) {
217 case UTIL_FORMAT_TYPE_UNSIGNED
:
218 case UTIL_FORMAT_TYPE_SIGNED
:
219 if (!desc
->channel
[0].normalized
&&
220 desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_SRGB
) {
224 switch (desc
->channel
[0].size
) {
226 switch (components
) {
228 return R300_TX_FORMAT_Y4X4
| result
;
230 return R300_TX_FORMAT_W4Z4Y4X4
| result
;
235 switch (components
) {
237 return R300_TX_FORMAT_X8
| result
;
239 return R300_TX_FORMAT_Y8X8
| result
;
241 return R300_TX_FORMAT_W8Z8Y8X8
| result
;
246 switch (components
) {
248 return R300_TX_FORMAT_X16
| result
;
250 return R300_TX_FORMAT_Y16X16
| result
;
252 return R300_TX_FORMAT_W16Z16Y16X16
| result
;
257 /* XXX Enable float textures here. */
259 case UTIL_FORMAT_TYPE_FLOAT
:
260 switch (desc
->channel
[0].size
) {
262 switch (components
) {
264 return R300_TX_FORMAT_16F
| result
;
266 return R300_TX_FORMAT_16F_16F
| result
;
268 return R300_TX_FORMAT_16F_16F_16F_16F
| result
;
273 switch (components
) {
275 return R300_TX_FORMAT_32F
| result
;
277 return R300_TX_FORMAT_32F_32F
| result
;
279 return R300_TX_FORMAT_32F_32F_32F_32F
| result
;
285 return ~0; /* Unsupported/unknown. */
288 /* Buffer formats. */
290 /* Colorbuffer formats. This is the unswizzled format of the RB3D block's
291 * output. For the swizzling of the targets, check the shader's format. */
292 static uint32_t r300_translate_colorformat(enum pipe_format format
)
296 case PIPE_FORMAT_A8_UNORM
:
297 case PIPE_FORMAT_I8_UNORM
:
298 case PIPE_FORMAT_L8_UNORM
:
299 case PIPE_FORMAT_L8_SRGB
:
300 case PIPE_FORMAT_R8_UNORM
:
301 case PIPE_FORMAT_R8_SNORM
:
302 return R300_COLOR_FORMAT_I8
;
304 /* 16-bit buffers. */
305 case PIPE_FORMAT_R5G6B5_UNORM
:
306 return R300_COLOR_FORMAT_RGB565
;
307 case PIPE_FORMAT_A1R5G5B5_UNORM
:
308 return R300_COLOR_FORMAT_ARGB1555
;
309 case PIPE_FORMAT_A4R4G4B4_UNORM
:
310 return R300_COLOR_FORMAT_ARGB4444
;
312 /* 32-bit buffers. */
313 case PIPE_FORMAT_A8R8G8B8_UNORM
:
314 case PIPE_FORMAT_A8R8G8B8_SRGB
:
315 case PIPE_FORMAT_X8R8G8B8_UNORM
:
316 case PIPE_FORMAT_X8R8G8B8_SRGB
:
317 case PIPE_FORMAT_B8G8R8A8_UNORM
:
318 case PIPE_FORMAT_B8G8R8A8_SRGB
:
319 case PIPE_FORMAT_B8G8R8X8_UNORM
:
320 case PIPE_FORMAT_B8G8R8X8_SRGB
:
321 case PIPE_FORMAT_R8G8B8A8_UNORM
:
322 case PIPE_FORMAT_R8G8B8A8_SNORM
:
323 case PIPE_FORMAT_R8G8B8A8_SRGB
:
324 case PIPE_FORMAT_R8G8B8X8_UNORM
:
325 case PIPE_FORMAT_R8G8B8X8_SRGB
:
326 case PIPE_FORMAT_R8G8B8X8_SNORM
:
327 case PIPE_FORMAT_A8B8G8R8_SNORM
:
328 case PIPE_FORMAT_X8B8G8R8_SNORM
:
329 case PIPE_FORMAT_X8UB8UG8SR8S_NORM
:
330 return R300_COLOR_FORMAT_ARGB8888
;
331 case PIPE_FORMAT_A2B10G10R10_UNORM
:
332 return R500_COLOR_FORMAT_ARGB2101010
; /* R5xx-only? */
334 /* 64-bit buffers. */
335 case PIPE_FORMAT_R16G16B16A16_UNORM
:
336 case PIPE_FORMAT_R16G16B16A16_SNORM
:
337 //case PIPE_FORMAT_R16G16B16A16_FLOAT: /* not in pipe_format */
338 return R300_COLOR_FORMAT_ARGB16161616
;
340 /* XXX Enable float textures here. */
342 /* 128-bit buffers. */
343 case PIPE_FORMAT_R32G32B32A32_FLOAT
:
344 return R300_COLOR_FORMAT_ARGB32323232
;
348 case PIPE_FORMAT_YCBCR
:
349 return R300_COLOR_FORMAT_YVYU
;
350 case PIPE_FORMAT_YCBCR_REV
:
351 return R300_COLOR_FORMAT_VYUY
;
353 return ~0; /* Unsupported. */
357 /* Depthbuffer and stencilbuffer. Thankfully, we only support two flavors. */
358 static uint32_t r300_translate_zsformat(enum pipe_format format
)
361 /* 16-bit depth, no stencil */
362 case PIPE_FORMAT_Z16_UNORM
:
363 return R300_DEPTHFORMAT_16BIT_INT_Z
;
364 /* 24-bit depth, ignored stencil */
365 case PIPE_FORMAT_Z24X8_UNORM
:
366 /* 24-bit depth, 8-bit stencil */
367 case PIPE_FORMAT_Z24S8_UNORM
:
368 return R300_DEPTHFORMAT_24BIT_INT_Z_8BIT_STENCIL
;
370 return ~0; /* Unsupported. */
374 /* Shader output formats. This is essentially the swizzle from the shader
377 * Note that formats are stored from C3 to C0. */
378 static uint32_t r300_translate_out_fmt(enum pipe_format format
)
380 uint32_t modifier
= 0;
382 const struct util_format_description
*desc
;
383 static const uint32_t sign_bit
[4] = {
390 desc
= util_format_description(format
);
392 /* Specifies how the shader output is written to the fog unit. */
393 if (desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) {
394 /* The gamma correction causes precision loss so we need
395 * higher precision to maintain reasonable quality.
396 * It has nothing to do with the colorbuffer format. */
397 modifier
|= R300_US_OUT_FMT_C4_10_GAMMA
;
398 } else if (desc
->channel
[0].type
== UTIL_FORMAT_TYPE_FLOAT
) {
399 if (desc
->channel
[0].size
== 32) {
400 modifier
|= R300_US_OUT_FMT_C4_32_FP
;
402 modifier
|= R300_US_OUT_FMT_C4_16_FP
;
405 if (desc
->channel
[0].size
== 16) {
406 modifier
|= R300_US_OUT_FMT_C4_16
;
408 /* C4_8 seems to be used for the formats whose pixel size
410 modifier
|= R300_US_OUT_FMT_C4_8
;
415 for (i
= 0; i
< 4; i
++)
416 if (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_SIGNED
) {
417 modifier
|= sign_bit
[i
];
420 /* Add swizzles and return. */
423 * COLORFORMAT_I8 stores the C2 component. */
424 case PIPE_FORMAT_A8_UNORM
:
425 return modifier
| R300_C2_SEL_A
;
426 case PIPE_FORMAT_I8_UNORM
:
427 case PIPE_FORMAT_L8_UNORM
:
428 case PIPE_FORMAT_L8_SRGB
:
429 case PIPE_FORMAT_R8_UNORM
:
430 case PIPE_FORMAT_R8_SNORM
:
431 return modifier
| R300_C2_SEL_R
;
433 /* ARGB 32-bit outputs. */
434 case PIPE_FORMAT_R5G6B5_UNORM
:
435 case PIPE_FORMAT_A1R5G5B5_UNORM
:
436 case PIPE_FORMAT_A4R4G4B4_UNORM
:
437 case PIPE_FORMAT_A8R8G8B8_UNORM
:
438 case PIPE_FORMAT_A8R8G8B8_SRGB
:
439 case PIPE_FORMAT_X8R8G8B8_UNORM
:
440 case PIPE_FORMAT_X8R8G8B8_SRGB
:
442 R300_C0_SEL_B
| R300_C1_SEL_G
|
443 R300_C2_SEL_R
| R300_C3_SEL_A
;
445 /* BGRA 32-bit outputs. */
446 case PIPE_FORMAT_B8G8R8A8_UNORM
:
447 case PIPE_FORMAT_B8G8R8A8_SRGB
:
448 case PIPE_FORMAT_B8G8R8X8_UNORM
:
449 case PIPE_FORMAT_B8G8R8X8_SRGB
:
451 R300_C0_SEL_A
| R300_C1_SEL_R
|
452 R300_C2_SEL_G
| R300_C3_SEL_B
;
454 /* RGBA 32-bit outputs. */
455 case PIPE_FORMAT_R8G8B8A8_UNORM
:
456 case PIPE_FORMAT_R8G8B8A8_SNORM
:
457 case PIPE_FORMAT_R8G8B8A8_SRGB
:
458 case PIPE_FORMAT_R8G8B8X8_UNORM
:
459 case PIPE_FORMAT_R8G8B8X8_SRGB
:
460 case PIPE_FORMAT_R8G8B8X8_SNORM
:
462 R300_C0_SEL_A
| R300_C1_SEL_B
|
463 R300_C2_SEL_G
| R300_C3_SEL_R
;
465 /* ABGR 32-bit outputs. */
466 case PIPE_FORMAT_A8B8G8R8_SNORM
:
467 case PIPE_FORMAT_X8B8G8R8_SNORM
:
468 case PIPE_FORMAT_X8UB8UG8SR8S_NORM
:
469 case PIPE_FORMAT_A2B10G10R10_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_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
,
622 unsigned tile_width
, width
;
624 tile_width
= r300_texture_get_tile_size(tex
, TILE_WIDTH
, TRUE
);
625 width
= u_minify(tex
->tex
.width0
, level
);
627 /* See TX_FILTER1_n.MACRO_SWITCH. */
629 return width
>= tile_width
;
631 return width
> tile_width
;
636 * Return the stride, in bytes, of the texture images of the given texture
637 * at the given level.
639 unsigned r300_texture_get_stride(struct r300_screen
* screen
,
640 struct r300_texture
* tex
, unsigned level
)
642 unsigned tile_width
, width
;
644 if (tex
->stride_override
)
645 return tex
->stride_override
;
647 /* Check the level. */
648 if (level
> tex
->tex
.last_level
) {
649 SCREEN_DBG(screen
, DBG_TEX
, "%s: level (%u) > last_level (%u)\n",
650 __FUNCTION__
, level
, tex
->tex
.last_level
);
654 width
= u_minify(tex
->tex
.width0
, level
);
656 if (!util_format_is_compressed(tex
->tex
.format
)) {
657 tile_width
= r300_texture_get_tile_size(tex
, TILE_WIDTH
,
658 tex
->mip_macrotile
[level
]);
659 width
= align(width
, tile_width
);
661 return util_format_get_stride(tex
->tex
.format
, width
);
663 return align(util_format_get_stride(tex
->tex
.format
, width
), 32);
667 static unsigned r300_texture_get_nblocksy(struct r300_texture
* tex
,
670 unsigned height
, tile_height
;
672 height
= u_minify(tex
->tex
.height0
, level
);
674 if (!util_format_is_compressed(tex
->tex
.format
)) {
675 tile_height
= r300_texture_get_tile_size(tex
, TILE_HEIGHT
,
676 tex
->mip_macrotile
[level
]);
677 height
= align(height
, tile_height
);
680 return util_format_get_nblocksy(tex
->tex
.format
, height
);
683 static void r300_setup_miptree(struct r300_screen
* screen
,
684 struct r300_texture
* tex
)
686 struct pipe_texture
* base
= &tex
->tex
;
687 unsigned stride
, size
, layer_size
, nblocksy
, i
;
688 boolean rv350_mode
= screen
->caps
->family
>= CHIP_FAMILY_RV350
;
690 SCREEN_DBG(screen
, DBG_TEX
, "r300: Making miptree for texture, format %s\n",
691 util_format_name(base
->format
));
693 for (i
= 0; i
<= base
->last_level
; i
++) {
694 /* Let's see if this miplevel can be macrotiled. */
695 tex
->mip_macrotile
[i
] = (tex
->macrotile
== R300_BUFFER_TILED
&&
696 r300_texture_macro_switch(tex
, i
, rv350_mode
)) ?
697 R300_BUFFER_TILED
: R300_BUFFER_LINEAR
;
699 stride
= r300_texture_get_stride(screen
, tex
, i
);
700 nblocksy
= r300_texture_get_nblocksy(tex
, i
);
701 layer_size
= stride
* nblocksy
;
703 if (base
->target
== PIPE_TEXTURE_CUBE
)
704 size
= layer_size
* 6;
706 size
= layer_size
* u_minify(base
->depth0
, i
);
708 tex
->offset
[i
] = tex
->size
;
709 tex
->size
= tex
->offset
[i
] + size
;
710 tex
->layer_size
[i
] = layer_size
;
711 tex
->pitch
[i
] = stride
/ util_format_get_blocksize(base
->format
);
713 SCREEN_DBG(screen
, DBG_TEX
, "r300: Texture miptree: Level %d "
714 "(%dx%dx%d px, pitch %d bytes) %d bytes total, macrotiled %s\n",
715 i
, u_minify(base
->width0
, i
), u_minify(base
->height0
, i
),
716 u_minify(base
->depth0
, i
), stride
, tex
->size
,
717 tex
->mip_macrotile
[i
] ? "TRUE" : "FALSE");
721 static void r300_setup_flags(struct r300_texture
* tex
)
723 tex
->is_npot
= !util_is_power_of_two(tex
->tex
.width0
) ||
724 !util_is_power_of_two(tex
->tex
.height0
);
727 /* Create a new texture. */
728 static struct pipe_texture
*
729 r300_texture_create(struct pipe_screen
* screen
,
730 const struct pipe_texture
* template)
732 struct r300_texture
* tex
= CALLOC_STRUCT(r300_texture
);
733 struct r300_screen
* rscreen
= r300_screen(screen
);
734 struct r300_winsys_screen
*rws
= (struct r300_winsys_screen
*)screen
->winsys
;
740 tex
->tex
= *template;
741 pipe_reference_init(&tex
->tex
.reference
, 1);
742 tex
->tex
.screen
= screen
;
744 r300_setup_flags(tex
);
745 r300_setup_miptree(rscreen
, tex
);
746 r300_setup_texture_state(rscreen
, tex
);
748 tex
->buffer
= rws
->buffer_create(rws
, 2048,
749 PIPE_BUFFER_USAGE_PIXEL
,
751 rws
->buffer_set_tiling(rws
, tex
->buffer
,
753 tex
->microtile
!= R300_BUFFER_LINEAR
,
754 tex
->macrotile
!= R300_BUFFER_LINEAR
);
761 return (struct pipe_texture
*)tex
;
764 static void r300_texture_destroy(struct pipe_texture
* texture
)
766 struct r300_texture
* tex
= (struct r300_texture
*)texture
;
767 struct r300_winsys_screen
*rws
= (struct r300_winsys_screen
*)texture
->screen
->winsys
;
769 rws
->buffer_reference(rws
, &tex
->buffer
, NULL
);
773 static struct pipe_surface
* r300_get_tex_surface(struct pipe_screen
* screen
,
774 struct pipe_texture
* texture
,
780 struct r300_texture
* tex
= (struct r300_texture
*)texture
;
781 struct pipe_surface
* surface
= CALLOC_STRUCT(pipe_surface
);
784 offset
= r300_texture_get_offset(tex
, level
, zslice
, face
);
787 pipe_reference_init(&surface
->reference
, 1);
788 pipe_texture_reference(&surface
->texture
, texture
);
789 surface
->format
= texture
->format
;
790 surface
->width
= u_minify(texture
->width0
, level
);
791 surface
->height
= u_minify(texture
->height0
, level
);
792 surface
->offset
= offset
;
793 surface
->usage
= flags
;
794 surface
->zslice
= zslice
;
795 surface
->texture
= texture
;
796 surface
->face
= face
;
797 surface
->level
= level
;
803 static void r300_tex_surface_destroy(struct pipe_surface
* s
)
805 pipe_texture_reference(&s
->texture
, NULL
);
809 struct pipe_texture
*r300_texture_blanket_winsys_buffer(struct pipe_screen
*screen
,
810 const struct pipe_texture
*base
,
811 const unsigned *stride
,
812 struct r300_winsys_buffer
*buffer
)
814 struct r300_texture
* tex
;
815 struct r300_screen
* rscreen
= r300_screen(screen
);
816 struct r300_winsys_screen
*rws
= (struct r300_winsys_screen
*)screen
->winsys
;
818 /* Support only 2D textures without mipmaps */
819 if (base
->target
!= PIPE_TEXTURE_2D
||
821 base
->last_level
!= 0) {
825 tex
= CALLOC_STRUCT(r300_texture
);
831 pipe_reference_init(&tex
->tex
.reference
, 1);
832 tex
->tex
.screen
= screen
;
834 tex
->stride_override
= *stride
;
835 tex
->pitch
[0] = *stride
/ util_format_get_blocksize(base
->format
);
837 r300_setup_flags(tex
);
838 r300_setup_texture_state(rscreen
, tex
);
840 rws
->buffer_reference(rws
, &tex
->buffer
, buffer
);
842 return (struct pipe_texture
*)tex
;
845 static struct pipe_video_surface
*
846 r300_video_surface_create(struct pipe_screen
*screen
,
847 enum pipe_video_chroma_format chroma_format
,
848 unsigned width
, unsigned height
)
850 struct r300_video_surface
*r300_vsfc
;
851 struct pipe_texture
template;
854 assert(width
&& height
);
856 r300_vsfc
= CALLOC_STRUCT(r300_video_surface
);
860 pipe_reference_init(&r300_vsfc
->base
.reference
, 1);
861 r300_vsfc
->base
.screen
= screen
;
862 r300_vsfc
->base
.chroma_format
= chroma_format
;
863 r300_vsfc
->base
.width
= width
;
864 r300_vsfc
->base
.height
= height
;
866 memset(&template, 0, sizeof(struct pipe_texture
));
867 template.target
= PIPE_TEXTURE_2D
;
868 template.format
= PIPE_FORMAT_X8R8G8B8_UNORM
;
869 template.last_level
= 0;
870 template.width0
= util_next_power_of_two(width
);
871 template.height0
= util_next_power_of_two(height
);
873 template.tex_usage
= PIPE_TEXTURE_USAGE_SAMPLER
|
874 PIPE_TEXTURE_USAGE_RENDER_TARGET
;
876 r300_vsfc
->tex
= screen
->texture_create(screen
, &template);
883 return &r300_vsfc
->base
;
886 static void r300_video_surface_destroy(struct pipe_video_surface
*vsfc
)
888 struct r300_video_surface
*r300_vsfc
= r300_video_surface(vsfc
);
889 pipe_texture_reference(&r300_vsfc
->tex
, NULL
);
893 void r300_init_screen_texture_functions(struct pipe_screen
* screen
)
895 screen
->texture_create
= r300_texture_create
;
896 screen
->texture_destroy
= r300_texture_destroy
;
897 screen
->get_tex_surface
= r300_get_tex_surface
;
898 screen
->tex_surface_destroy
= r300_tex_surface_destroy
;
900 screen
->video_surface_create
= r300_video_surface_create
;
901 screen
->video_surface_destroy
= r300_video_surface_destroy
;
904 boolean
r300_get_texture_buffer(struct pipe_screen
* screen
,
905 struct pipe_texture
* texture
,
906 struct r300_winsys_buffer
** buffer
,
909 struct r300_texture
* tex
= (struct r300_texture
*)texture
;
910 struct r300_winsys_screen
*rws
= (struct r300_winsys_screen
*)screen
->winsys
;
911 struct r300_winsys_buffer
*buf
;
917 rws
->buffer_reference(rws
, &buf
, tex
->buffer
);
920 *stride
= r300_texture_get_stride(r300_screen(screen
), tex
, 0);