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
;
76 unsigned components
= 0, i
;
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. */
161 /* Compressed formats. */
162 if (desc
->layout
== UTIL_FORMAT_LAYOUT_COMPRESSED
) {
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. */
180 /* Get the number of components. */
181 for (i
= 0; i
< 4; i
++) {
182 if (desc
->channel
[i
].type
!= UTIL_FORMAT_TYPE_VOID
) {
188 for (i
= 0; i
< components
; i
++) {
189 if (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_SIGNED
) {
190 result
|= sign_bit
[i
];
194 /* See whether the components are of the same size. */
195 for (i
= 1; i
< components
; i
++) {
196 uniform
= uniform
&& desc
->channel
[0].size
== desc
->channel
[i
].size
;
199 /* Non-uniform formats. */
201 switch (components
) {
203 if (desc
->channel
[0].size
== 5 &&
204 desc
->channel
[1].size
== 6 &&
205 desc
->channel
[2].size
== 5) {
206 return R300_TX_FORMAT_Z5Y6X5
| result
;
208 if (desc
->channel
[0].size
== 5 &&
209 desc
->channel
[1].size
== 5 &&
210 desc
->channel
[2].size
== 6) {
211 return R300_TX_FORMAT_Z6Y5X5
| result
;
213 return ~0; /* Unsupported/unknown. */
216 if (desc
->channel
[0].size
== 5 &&
217 desc
->channel
[1].size
== 5 &&
218 desc
->channel
[2].size
== 5 &&
219 desc
->channel
[3].size
== 1) {
220 return R300_TX_FORMAT_W1Z5Y5X5
| result
;
222 if (desc
->channel
[0].size
== 10 &&
223 desc
->channel
[1].size
== 10 &&
224 desc
->channel
[2].size
== 10 &&
225 desc
->channel
[3].size
== 2) {
226 return R300_TX_FORMAT_W2Z10Y10X10
| result
;
229 return ~0; /* Unsupported/unknown. */
232 /* And finally, uniform formats. */
233 switch (desc
->channel
[0].type
) {
234 case UTIL_FORMAT_TYPE_UNSIGNED
:
235 case UTIL_FORMAT_TYPE_SIGNED
:
236 if (!desc
->channel
[0].normalized
&&
237 desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_SRGB
) {
241 switch (desc
->channel
[0].size
) {
243 switch (components
) {
245 return R300_TX_FORMAT_Y4X4
| result
;
247 return R300_TX_FORMAT_W4Z4Y4X4
| result
;
252 switch (components
) {
254 return R300_TX_FORMAT_X8
| result
;
256 return R300_TX_FORMAT_Y8X8
| result
;
258 return R300_TX_FORMAT_W8Z8Y8X8
| result
;
263 switch (components
) {
265 return R300_TX_FORMAT_X16
| result
;
267 return R300_TX_FORMAT_Y16X16
| result
;
269 return R300_TX_FORMAT_W16Z16Y16X16
| result
;
274 /* XXX Enable float textures here. */
276 case UTIL_FORMAT_TYPE_FLOAT
:
277 switch (desc
->channel
[0].size
) {
279 switch (components
) {
281 return R300_TX_FORMAT_16F
| result
;
283 return R300_TX_FORMAT_16F_16F
| result
;
285 return R300_TX_FORMAT_16F_16F_16F_16F
| result
;
290 switch (components
) {
292 return R300_TX_FORMAT_32F
| result
;
294 return R300_TX_FORMAT_32F_32F
| result
;
296 return R300_TX_FORMAT_32F_32F_32F_32F
| result
;
302 return ~0; /* Unsupported/unknown. */
305 /* Buffer formats. */
307 /* Colorbuffer formats. This is the unswizzled format of the RB3D block's
308 * output. For the swizzling of the targets, check the shader's format. */
309 static uint32_t r300_translate_colorformat(enum pipe_format format
)
313 case PIPE_FORMAT_A8_UNORM
:
314 case PIPE_FORMAT_I8_UNORM
:
315 case PIPE_FORMAT_L8_UNORM
:
316 case PIPE_FORMAT_R8_UNORM
:
317 case PIPE_FORMAT_R8_SNORM
:
318 return R300_COLOR_FORMAT_I8
;
320 /* 16-bit buffers. */
321 case PIPE_FORMAT_B5G6R5_UNORM
:
322 return R300_COLOR_FORMAT_RGB565
;
323 case PIPE_FORMAT_B5G5R5A1_UNORM
:
324 case PIPE_FORMAT_B5G5R5X1_UNORM
:
325 return R300_COLOR_FORMAT_ARGB1555
;
326 case PIPE_FORMAT_B4G4R4A4_UNORM
:
327 return R300_COLOR_FORMAT_ARGB4444
;
329 /* 32-bit buffers. */
330 case PIPE_FORMAT_B8G8R8A8_UNORM
:
331 case PIPE_FORMAT_B8G8R8X8_UNORM
:
332 case PIPE_FORMAT_A8R8G8B8_UNORM
:
333 case PIPE_FORMAT_X8R8G8B8_UNORM
:
334 case PIPE_FORMAT_A8B8G8R8_UNORM
:
335 case PIPE_FORMAT_R8G8B8A8_SNORM
:
336 case PIPE_FORMAT_X8B8G8R8_UNORM
:
337 case PIPE_FORMAT_R8SG8SB8UX8U_NORM
:
338 return R300_COLOR_FORMAT_ARGB8888
;
339 case PIPE_FORMAT_R10G10B10A2_UNORM
:
340 return R500_COLOR_FORMAT_ARGB2101010
; /* R5xx-only? */
342 /* 64-bit buffers. */
343 case PIPE_FORMAT_R16G16B16A16_UNORM
:
344 case PIPE_FORMAT_R16G16B16A16_SNORM
:
345 //case PIPE_FORMAT_R16G16B16A16_FLOAT: /* not in pipe_format */
346 return R300_COLOR_FORMAT_ARGB16161616
;
348 /* XXX Enable float textures here. */
350 /* 128-bit buffers. */
351 case PIPE_FORMAT_R32G32B32A32_FLOAT
:
352 return R300_COLOR_FORMAT_ARGB32323232
;
356 case PIPE_FORMAT_UYVY
:
357 return R300_COLOR_FORMAT_YVYU
;
358 case PIPE_FORMAT_YUYV
:
359 return R300_COLOR_FORMAT_VYUY
;
361 return ~0; /* Unsupported. */
365 /* Depthbuffer and stencilbuffer. Thankfully, we only support two flavors. */
366 static uint32_t r300_translate_zsformat(enum pipe_format format
)
369 /* 16-bit depth, no stencil */
370 case PIPE_FORMAT_Z16_UNORM
:
371 return R300_DEPTHFORMAT_16BIT_INT_Z
;
372 /* 24-bit depth, ignored stencil */
373 case PIPE_FORMAT_X8Z24_UNORM
:
374 /* 24-bit depth, 8-bit stencil */
375 case PIPE_FORMAT_S8_USCALED_Z24_UNORM
:
376 return R300_DEPTHFORMAT_24BIT_INT_Z_8BIT_STENCIL
;
378 return ~0; /* Unsupported. */
382 /* Shader output formats. This is essentially the swizzle from the shader
385 * Note that formats are stored from C3 to C0. */
386 static uint32_t r300_translate_out_fmt(enum pipe_format format
)
388 uint32_t modifier
= 0;
390 const struct util_format_description
*desc
;
391 static const uint32_t sign_bit
[4] = {
398 desc
= util_format_description(format
);
400 /* Specifies how the shader output is written to the fog unit. */
401 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_R8_UNORM
:
432 case PIPE_FORMAT_R8_SNORM
:
433 return modifier
| R300_C2_SEL_R
;
436 case PIPE_FORMAT_B5G6R5_UNORM
:
437 case PIPE_FORMAT_B5G5R5A1_UNORM
:
438 case PIPE_FORMAT_B5G5R5X1_UNORM
:
439 case PIPE_FORMAT_B4G4R4A4_UNORM
:
440 case PIPE_FORMAT_B8G8R8A8_UNORM
:
441 case PIPE_FORMAT_B8G8R8X8_UNORM
:
443 R300_C0_SEL_B
| R300_C1_SEL_G
|
444 R300_C2_SEL_R
| R300_C3_SEL_A
;
447 case PIPE_FORMAT_A8R8G8B8_UNORM
:
448 case PIPE_FORMAT_X8R8G8B8_UNORM
:
450 R300_C0_SEL_A
| R300_C1_SEL_R
|
451 R300_C2_SEL_G
| R300_C3_SEL_B
;
454 case PIPE_FORMAT_A8B8G8R8_UNORM
:
455 case PIPE_FORMAT_X8B8G8R8_UNORM
:
457 R300_C0_SEL_A
| R300_C1_SEL_B
|
458 R300_C2_SEL_G
| R300_C3_SEL_R
;
461 case PIPE_FORMAT_R8G8B8A8_SNORM
:
462 case PIPE_FORMAT_R8SG8SB8UX8U_NORM
:
463 case PIPE_FORMAT_R10G10B10A2_UNORM
:
464 case PIPE_FORMAT_R16G16B16A16_UNORM
:
465 case PIPE_FORMAT_R16G16B16A16_SNORM
:
466 //case PIPE_FORMAT_R16G16B16A16_FLOAT: /* not in pipe_format */
467 case PIPE_FORMAT_R32G32B32A32_FLOAT
:
469 R300_C0_SEL_R
| R300_C1_SEL_G
|
470 R300_C2_SEL_B
| R300_C3_SEL_A
;
473 return ~0; /* Unsupported. */
477 boolean
r300_is_colorbuffer_format_supported(enum pipe_format format
)
479 return r300_translate_colorformat(format
) != ~0 &&
480 r300_translate_out_fmt(format
) != ~0;
483 boolean
r300_is_zs_format_supported(enum pipe_format format
)
485 return r300_translate_zsformat(format
) != ~0;
488 boolean
r300_is_sampler_format_supported(enum pipe_format format
)
490 return r300_translate_texformat(format
) != ~0;
493 static void r300_setup_texture_state(struct r300_screen
* screen
, struct r300_texture
* tex
)
495 struct r300_texture_format_state
* state
= &tex
->state
;
496 struct pipe_texture
*pt
= &tex
->tex
;
498 boolean is_r500
= screen
->caps
->is_r500
;
500 /* Set sampler state. */
501 state
->format0
= R300_TX_WIDTH((pt
->width0
- 1) & 0x7ff) |
502 R300_TX_HEIGHT((pt
->height0
- 1) & 0x7ff);
505 /* rectangles love this */
506 state
->format0
|= R300_TX_PITCH_EN
;
507 state
->format2
= (tex
->pitch
[0] - 1) & 0x1fff;
509 /* power of two textures (3D, mipmaps, and no pitch) */
510 state
->format0
|= R300_TX_DEPTH(util_logbase2(pt
->depth0
) & 0xf);
513 state
->format1
= r300_translate_texformat(pt
->format
);
514 if (pt
->target
== PIPE_TEXTURE_CUBE
) {
515 state
->format1
|= R300_TX_FORMAT_CUBIC_MAP
;
517 if (pt
->target
== PIPE_TEXTURE_3D
) {
518 state
->format1
|= R300_TX_FORMAT_3D
;
521 /* large textures on r500 */
524 if (pt
->width0
> 2048) {
525 state
->format2
|= R500_TXWIDTH_BIT11
;
527 if (pt
->height0
> 2048) {
528 state
->format2
|= R500_TXHEIGHT_BIT11
;
532 SCREEN_DBG(screen
, DBG_TEX
, "r300: Set texture state (%dx%d, %d levels)\n",
533 pt
->width0
, pt
->height0
, pt
->last_level
);
535 /* Set framebuffer state. */
536 if (util_format_is_depth_or_stencil(tex
->tex
.format
)) {
537 for (i
= 0; i
<= tex
->tex
.last_level
; i
++) {
538 tex
->fb_state
.depthpitch
[i
] =
540 R300_DEPTHMACROTILE(tex
->mip_macrotile
[i
]) |
541 R300_DEPTHMICROTILE(tex
->microtile
);
543 tex
->fb_state
.zb_format
= r300_translate_zsformat(tex
->tex
.format
);
545 for (i
= 0; i
<= tex
->tex
.last_level
; i
++) {
546 tex
->fb_state
.colorpitch
[i
] =
548 r300_translate_colorformat(tex
->tex
.format
) |
549 R300_COLOR_TILE(tex
->mip_macrotile
[i
]) |
550 R300_COLOR_MICROTILE(tex
->microtile
);
552 tex
->fb_state
.us_out_fmt
= r300_translate_out_fmt(tex
->tex
.format
);
556 void r300_texture_reinterpret_format(struct pipe_screen
*screen
,
557 struct pipe_texture
*tex
,
558 enum pipe_format new_format
)
560 struct r300_screen
*r300screen
= r300_screen(screen
);
562 SCREEN_DBG(r300screen
, DBG_TEX
, "r300: Reinterpreting format: %s -> %s\n",
563 util_format_name(tex
->format
), util_format_name(new_format
));
565 tex
->format
= new_format
;
567 r300_setup_texture_state(r300_screen(screen
), (struct r300_texture
*)tex
);
570 unsigned r300_texture_get_offset(struct r300_texture
* tex
, unsigned level
,
571 unsigned zslice
, unsigned face
)
573 unsigned offset
= tex
->offset
[level
];
575 switch (tex
->tex
.target
) {
576 case PIPE_TEXTURE_3D
:
578 return offset
+ zslice
* tex
->layer_size
[level
];
580 case PIPE_TEXTURE_CUBE
:
582 return offset
+ face
* tex
->layer_size
[level
];
585 assert(zslice
== 0 && face
== 0);
591 * Return the width (dim==TILE_WIDTH) or height (dim==TILE_HEIGHT) of one tile
592 * of the given texture.
594 static unsigned r300_texture_get_tile_size(struct r300_texture
* tex
,
595 int dim
, boolean macrotile
)
597 unsigned pixsize
, tile_size
;
599 pixsize
= util_format_get_blocksize(tex
->tex
.format
);
600 tile_size
= microblock_table
[util_logbase2(pixsize
)][tex
->microtile
][dim
];
610 /* Return true if macrotiling should be enabled on the miplevel. */
611 static boolean
r300_texture_macro_switch(struct r300_texture
*tex
,
616 unsigned tile
, texdim
;
618 tile
= r300_texture_get_tile_size(tex
, dim
, TRUE
);
619 if (dim
== TILE_WIDTH
) {
620 texdim
= u_minify(tex
->tex
.width0
, level
);
622 texdim
= u_minify(tex
->tex
.height0
, level
);
625 /* See TX_FILTER1_n.MACRO_SWITCH. */
627 return texdim
>= tile
;
629 return texdim
> tile
;
634 * Return the stride, in bytes, of the texture images of the given texture
635 * at the given level.
637 unsigned r300_texture_get_stride(struct r300_screen
* screen
,
638 struct r300_texture
* tex
, unsigned level
)
640 unsigned tile_width
, width
;
642 if (tex
->stride_override
)
643 return tex
->stride_override
;
645 /* Check the level. */
646 if (level
> tex
->tex
.last_level
) {
647 SCREEN_DBG(screen
, DBG_TEX
, "%s: level (%u) > last_level (%u)\n",
648 __FUNCTION__
, level
, tex
->tex
.last_level
);
652 width
= u_minify(tex
->tex
.width0
, level
);
654 if (r300_format_is_plain(tex
->tex
.format
)) {
655 tile_width
= r300_texture_get_tile_size(tex
, TILE_WIDTH
,
656 tex
->mip_macrotile
[level
]);
657 width
= align(width
, tile_width
);
659 return util_format_get_stride(tex
->tex
.format
, width
);
661 return align(util_format_get_stride(tex
->tex
.format
, width
), 32);
665 static unsigned r300_texture_get_nblocksy(struct r300_texture
* tex
,
668 unsigned height
, tile_height
;
670 height
= u_minify(tex
->tex
.height0
, level
);
672 if (r300_format_is_plain(tex
->tex
.format
)) {
673 tile_height
= r300_texture_get_tile_size(tex
, TILE_HEIGHT
,
674 tex
->mip_macrotile
[level
]);
675 height
= align(height
, tile_height
);
678 return util_format_get_nblocksy(tex
->tex
.format
, height
);
681 static void r300_setup_miptree(struct r300_screen
* screen
,
682 struct r300_texture
* tex
)
684 struct pipe_texture
* base
= &tex
->tex
;
685 unsigned stride
, size
, layer_size
, nblocksy
, i
;
686 boolean rv350_mode
= screen
->caps
->family
>= CHIP_FAMILY_RV350
;
688 SCREEN_DBG(screen
, DBG_TEX
, "r300: Making miptree for texture, format %s\n",
689 util_format_name(base
->format
));
691 for (i
= 0; i
<= base
->last_level
; i
++) {
692 /* Let's see if this miplevel can be macrotiled. */
693 tex
->mip_macrotile
[i
] =
694 (tex
->macrotile
== R300_BUFFER_TILED
&&
695 r300_texture_macro_switch(tex
, i
, rv350_mode
, TILE_WIDTH
) &&
696 r300_texture_macro_switch(tex
, i
, rv350_mode
, TILE_HEIGHT
)) ?
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 static void r300_setup_tiling(struct pipe_screen
*screen
,
728 struct r300_texture
*tex
)
730 struct r300_winsys_screen
*rws
= (struct r300_winsys_screen
*)screen
->winsys
;
731 enum pipe_format format
= tex
->tex
.format
;
732 boolean rv350_mode
= r300_screen(screen
)->caps
->family
>= CHIP_FAMILY_RV350
;
734 if (!r300_format_is_plain(format
)) {
738 if (tex
->tex
.width0
== 1 ||
739 tex
->tex
.height0
== 1) {
743 /* Set microtiling. */
744 switch (util_format_get_blocksize(format
)) {
747 tex
->microtile
= R300_BUFFER_TILED
;
752 if (rws
->get_value(rws
, R300_VID_SQUARE_TILING_SUPPORT
)) {
753 tex
->microtile
= R300_BUFFER_SQUARETILED
;
758 /* Set macrotiling. */
759 if (r300_texture_macro_switch(tex
, 0, rv350_mode
, TILE_WIDTH
) &&
760 r300_texture_macro_switch(tex
, 0, rv350_mode
, TILE_HEIGHT
)) {
761 tex
->macrotile
= R300_BUFFER_TILED
;
765 /* Create a new texture. */
766 static struct pipe_texture
* r300_texture_create(struct pipe_screen
* screen
,
767 const struct pipe_texture
* template)
769 struct r300_texture
* tex
= CALLOC_STRUCT(r300_texture
);
770 struct r300_screen
* rscreen
= r300_screen(screen
);
771 struct r300_winsys_screen
*rws
= (struct r300_winsys_screen
*)screen
->winsys
;
777 tex
->tex
= *template;
778 pipe_reference_init(&tex
->tex
.reference
, 1);
779 tex
->tex
.screen
= screen
;
781 r300_setup_flags(tex
);
782 if (!(template->tex_usage
& R300_TEXTURE_USAGE_TRANSFER
)) {
783 r300_setup_tiling(screen
, tex
);
785 r300_setup_miptree(rscreen
, tex
);
786 r300_setup_texture_state(rscreen
, tex
);
788 tex
->buffer
= rws
->buffer_create(rws
, 2048,
789 PIPE_BUFFER_USAGE_PIXEL
,
791 rws
->buffer_set_tiling(rws
, tex
->buffer
,
801 return (struct pipe_texture
*)tex
;
804 static void r300_texture_destroy(struct pipe_texture
* texture
)
806 struct r300_texture
* tex
= (struct r300_texture
*)texture
;
807 struct r300_winsys_screen
*rws
= (struct r300_winsys_screen
*)texture
->screen
->winsys
;
809 rws
->buffer_reference(rws
, &tex
->buffer
, NULL
);
813 static struct pipe_surface
* r300_get_tex_surface(struct pipe_screen
* screen
,
814 struct pipe_texture
* texture
,
820 struct r300_texture
* tex
= (struct r300_texture
*)texture
;
821 struct pipe_surface
* surface
= CALLOC_STRUCT(pipe_surface
);
824 offset
= r300_texture_get_offset(tex
, level
, zslice
, face
);
827 pipe_reference_init(&surface
->reference
, 1);
828 pipe_texture_reference(&surface
->texture
, texture
);
829 surface
->format
= texture
->format
;
830 surface
->width
= u_minify(texture
->width0
, level
);
831 surface
->height
= u_minify(texture
->height0
, level
);
832 surface
->offset
= offset
;
833 surface
->usage
= flags
;
834 surface
->zslice
= zslice
;
835 surface
->texture
= texture
;
836 surface
->face
= face
;
837 surface
->level
= level
;
843 static void r300_tex_surface_destroy(struct pipe_surface
* s
)
845 pipe_texture_reference(&s
->texture
, NULL
);
850 static struct pipe_texture
*
851 r300_texture_from_handle(struct pipe_screen
* screen
,
852 const struct pipe_texture
* base
,
853 struct winsys_handle
*whandle
)
855 struct r300_winsys_screen
*rws
= (struct r300_winsys_screen
*)screen
->winsys
;
856 struct r300_screen
* rscreen
= r300_screen(screen
);
857 struct r300_winsys_buffer
*buffer
;
858 struct r300_texture
* tex
;
861 /* Support only 2D textures without mipmaps */
862 if (base
->target
!= PIPE_TEXTURE_2D
||
864 base
->last_level
!= 0) {
868 buffer
= rws
->buffer_from_handle(rws
, screen
, whandle
, &stride
);
873 tex
= CALLOC_STRUCT(r300_texture
);
879 pipe_reference_init(&tex
->tex
.reference
, 1);
880 tex
->tex
.screen
= screen
;
882 tex
->stride_override
= stride
;
883 tex
->pitch
[0] = stride
/ util_format_get_blocksize(base
->format
);
885 r300_setup_flags(tex
);
886 r300_setup_texture_state(rscreen
, tex
);
888 /* one ref already taken */
889 tex
->buffer
= buffer
;
891 return (struct pipe_texture
*)tex
;
895 r300_texture_get_handle(struct pipe_screen
* screen
,
896 struct pipe_texture
*texture
,
897 struct winsys_handle
*whandle
)
899 struct r300_winsys_screen
*rws
= (struct r300_winsys_screen
*)screen
->winsys
;
900 struct r300_texture
* tex
= (struct r300_texture
*)texture
;
907 stride
= r300_texture_get_stride(r300_screen(screen
), tex
, 0);
909 rws
->buffer_get_handle(rws
, tex
->buffer
, stride
, whandle
);
914 static struct pipe_video_surface
*
915 r300_video_surface_create(struct pipe_screen
*screen
,
916 enum pipe_video_chroma_format chroma_format
,
917 unsigned width
, unsigned height
)
919 struct r300_video_surface
*r300_vsfc
;
920 struct pipe_texture
template;
923 assert(width
&& height
);
925 r300_vsfc
= CALLOC_STRUCT(r300_video_surface
);
929 pipe_reference_init(&r300_vsfc
->base
.reference
, 1);
930 r300_vsfc
->base
.screen
= screen
;
931 r300_vsfc
->base
.chroma_format
= chroma_format
;
932 r300_vsfc
->base
.width
= width
;
933 r300_vsfc
->base
.height
= height
;
935 memset(&template, 0, sizeof(struct pipe_texture
));
936 template.target
= PIPE_TEXTURE_2D
;
937 template.format
= PIPE_FORMAT_B8G8R8X8_UNORM
;
938 template.last_level
= 0;
939 template.width0
= util_next_power_of_two(width
);
940 template.height0
= util_next_power_of_two(height
);
942 template.tex_usage
= PIPE_TEXTURE_USAGE_SAMPLER
|
943 PIPE_TEXTURE_USAGE_RENDER_TARGET
;
945 r300_vsfc
->tex
= screen
->texture_create(screen
, &template);
952 return &r300_vsfc
->base
;
955 static void r300_video_surface_destroy(struct pipe_video_surface
*vsfc
)
957 struct r300_video_surface
*r300_vsfc
= r300_video_surface(vsfc
);
958 pipe_texture_reference(&r300_vsfc
->tex
, NULL
);
962 void r300_init_screen_texture_functions(struct pipe_screen
* screen
)
964 screen
->texture_create
= r300_texture_create
;
965 screen
->texture_from_handle
= r300_texture_from_handle
;
966 screen
->texture_get_handle
= r300_texture_get_handle
;
967 screen
->texture_destroy
= r300_texture_destroy
;
968 screen
->get_tex_surface
= r300_get_tex_surface
;
969 screen
->tex_surface_destroy
= r300_tex_surface_destroy
;
971 screen
->video_surface_create
= r300_video_surface_create
;
972 screen
->video_surface_destroy
= r300_video_surface_destroy
;
975 boolean
r300_get_texture_buffer(struct pipe_screen
* screen
,
976 struct pipe_texture
* texture
,
977 struct r300_winsys_buffer
** buffer
,
980 struct r300_texture
* tex
= (struct r300_texture
*)texture
;
981 struct r300_winsys_screen
*rws
= (struct r300_winsys_screen
*)screen
->winsys
;
982 struct r300_winsys_buffer
*buf
;
988 rws
->buffer_reference(rws
, &buf
, tex
->buffer
);
991 *stride
= r300_texture_get_stride(r300_screen(screen
), tex
, 0);