2 * Copyright 2008 Corbin Simpson <MostAwesomeDude@gmail.com>
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE. */
23 #include "pipe/p_screen.h"
25 #include "util/u_format.h"
26 #include "util/u_math.h"
27 #include "util/u_memory.h"
29 #include "r300_context.h"
30 #include "r300_texture.h"
31 #include "r300_screen.h"
33 #include "radeon_winsys.h"
38 static const unsigned microblock_table
[5][3][2] = {
39 /*linear tiled square-tiled */
40 {{32, 1}, {8, 4}, {0, 0}}, /* 8 bits per pixel */
41 {{16, 1}, {8, 2}, {4, 4}}, /* 16 bits per pixel */
42 {{ 8, 1}, {4, 2}, {0, 0}}, /* 32 bits per pixel */
43 {{ 4, 1}, {0, 0}, {2, 2}}, /* 64 bits per pixel */
44 {{ 2, 1}, {0, 0}, {0, 0}} /* 128 bits per pixel */
47 /* Translate a pipe_format into a useful texture format for sampling.
49 * Some special formats are translated directly using R300_EASY_TX_FORMAT,
50 * but the majority of them is translated in a generic way, automatically
51 * supporting all the formats hw can support.
53 * R300_EASY_TX_FORMAT swizzles the texture.
54 * Note the signature of R300_EASY_TX_FORMAT:
55 * R300_EASY_TX_FORMAT(B, G, R, A, FORMAT);
57 * The FORMAT specifies how the texture sampler will treat the texture, and
58 * makes available X, Y, Z, W, ZERO, and ONE for swizzling. */
59 static uint32_t r300_translate_texformat(enum pipe_format format
)
62 const struct util_format_description
*desc
;
63 unsigned components
= 0, i
;
64 boolean uniform
= TRUE
;
65 const uint32_t swizzle_shift
[4] = {
66 R300_TX_FORMAT_R_SHIFT
,
67 R300_TX_FORMAT_G_SHIFT
,
68 R300_TX_FORMAT_B_SHIFT
,
69 R300_TX_FORMAT_A_SHIFT
71 const uint32_t sign_bit
[4] = {
72 R300_TX_FORMAT_SIGNED_X
,
73 R300_TX_FORMAT_SIGNED_Y
,
74 R300_TX_FORMAT_SIGNED_Z
,
75 R300_TX_FORMAT_SIGNED_W
,
78 desc
= util_format_description(format
);
80 /* Colorspace (return non-RGB formats directly). */
81 switch (desc
->colorspace
) {
82 /* Depth stencil formats. */
83 case UTIL_FORMAT_COLORSPACE_ZS
:
85 case PIPE_FORMAT_Z16_UNORM
:
86 return R300_EASY_TX_FORMAT(X
, X
, X
, X
, X16
);
87 case PIPE_FORMAT_Z24X8_UNORM
:
88 case PIPE_FORMAT_Z24S8_UNORM
:
89 return R300_EASY_TX_FORMAT(X
, X
, X
, X
, W24_FP
);
91 return ~0; /* Unsupported. */
95 case UTIL_FORMAT_COLORSPACE_YUV
:
96 result
|= R300_TX_FORMAT_YUV_TO_RGB
;
99 case PIPE_FORMAT_YCBCR
:
100 return R300_EASY_TX_FORMAT(X
, Y
, Z
, ONE
, YVYU422
) | result
;
101 case PIPE_FORMAT_YCBCR_REV
:
102 return R300_EASY_TX_FORMAT(X
, Y
, Z
, ONE
, VYUY422
) | result
;
104 return ~0; /* Unsupported/unknown. */
107 /* Add gamma correction. */
108 case UTIL_FORMAT_COLORSPACE_SRGB
:
109 result
|= R300_TX_FORMAT_GAMMA
;
116 for (i
= 0; i
< 4; i
++) {
117 switch (desc
->swizzle
[i
]) {
118 case UTIL_FORMAT_SWIZZLE_X
:
119 case UTIL_FORMAT_SWIZZLE_NONE
:
120 result
|= R300_TX_FORMAT_X
<< swizzle_shift
[i
];
122 case UTIL_FORMAT_SWIZZLE_Y
:
123 result
|= R300_TX_FORMAT_Y
<< swizzle_shift
[i
];
125 case UTIL_FORMAT_SWIZZLE_Z
:
126 result
|= R300_TX_FORMAT_Z
<< swizzle_shift
[i
];
128 case UTIL_FORMAT_SWIZZLE_W
:
129 result
|= R300_TX_FORMAT_W
<< swizzle_shift
[i
];
131 case UTIL_FORMAT_SWIZZLE_0
:
132 result
|= R300_TX_FORMAT_ZERO
<< swizzle_shift
[i
];
134 case UTIL_FORMAT_SWIZZLE_1
:
135 result
|= R300_TX_FORMAT_ONE
<< swizzle_shift
[i
];
138 return ~0; /* Unsupported. */
142 /* Compressed formats. */
143 if (desc
->layout
== UTIL_FORMAT_LAYOUT_DXT
) {
145 case PIPE_FORMAT_DXT1_RGB
:
146 case PIPE_FORMAT_DXT1_RGBA
:
147 case PIPE_FORMAT_DXT1_SRGB
:
148 case PIPE_FORMAT_DXT1_SRGBA
:
149 return R300_TX_FORMAT_DXT1
| result
;
150 case PIPE_FORMAT_DXT3_RGBA
:
151 case PIPE_FORMAT_DXT3_SRGBA
:
152 return R300_TX_FORMAT_DXT3
| result
;
153 case PIPE_FORMAT_DXT5_RGBA
:
154 case PIPE_FORMAT_DXT5_SRGBA
:
155 return R300_TX_FORMAT_DXT5
| result
;
157 return ~0; /* Unsupported/unknown. */
161 /* Get the number of components. */
162 for (i
= 0; i
< 4; i
++) {
163 if (desc
->channel
[i
].type
!= UTIL_FORMAT_TYPE_VOID
) {
169 for (i
= 0; i
< components
; i
++) {
170 if (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_SIGNED
) {
171 result
|= sign_bit
[i
];
175 /* See whether the components are of the same size. */
176 for (i
= 1; i
< components
; i
++) {
177 uniform
= uniform
&& desc
->channel
[0].size
== desc
->channel
[i
].size
;
180 /* Non-uniform formats. */
182 switch (components
) {
184 if (desc
->channel
[0].size
== 5 &&
185 desc
->channel
[1].size
== 6 &&
186 desc
->channel
[2].size
== 5) {
187 return R300_TX_FORMAT_Z5Y6X5
| result
;
189 if (desc
->channel
[0].size
== 5 &&
190 desc
->channel
[1].size
== 5 &&
191 desc
->channel
[2].size
== 6) {
192 return R300_TX_FORMAT_Z6Y5X5
| result
;
194 return ~0; /* Unsupported/unknown. */
197 if (desc
->channel
[0].size
== 5 &&
198 desc
->channel
[1].size
== 5 &&
199 desc
->channel
[2].size
== 5 &&
200 desc
->channel
[3].size
== 1) {
201 return R300_TX_FORMAT_W1Z5Y5X5
| result
;
203 if (desc
->channel
[0].size
== 10 &&
204 desc
->channel
[1].size
== 10 &&
205 desc
->channel
[2].size
== 10 &&
206 desc
->channel
[3].size
== 2) {
207 return R300_TX_FORMAT_W2Z10Y10X10
| result
;
210 return ~0; /* Unsupported/unknown. */
213 /* And finally, uniform formats. */
214 switch (desc
->channel
[0].type
) {
215 case UTIL_FORMAT_TYPE_UNSIGNED
:
216 case UTIL_FORMAT_TYPE_SIGNED
:
217 if (!desc
->channel
[0].normalized
&&
218 desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_SRGB
) {
222 switch (desc
->channel
[0].size
) {
224 switch (components
) {
226 return R300_TX_FORMAT_Y4X4
| result
;
228 return R300_TX_FORMAT_W4Z4Y4X4
| result
;
233 switch (components
) {
235 return R300_TX_FORMAT_X8
| result
;
237 return R300_TX_FORMAT_Y8X8
| result
;
239 return R300_TX_FORMAT_W8Z8Y8X8
| result
;
244 switch (components
) {
246 return R300_TX_FORMAT_X16
| result
;
248 return R300_TX_FORMAT_Y16X16
| result
;
250 return R300_TX_FORMAT_W16Z16Y16X16
| result
;
255 /* XXX Enable float textures here. */
257 case UTIL_FORMAT_TYPE_FLOAT
:
258 switch (desc
->channel
[0].size
) {
260 switch (components
) {
262 return R300_TX_FORMAT_16F
| result
;
264 return R300_TX_FORMAT_16F_16F
| result
;
266 return R300_TX_FORMAT_16F_16F_16F_16F
| result
;
271 switch (components
) {
273 return R300_TX_FORMAT_32F
| result
;
275 return R300_TX_FORMAT_32F_32F
| result
;
277 return R300_TX_FORMAT_32F_32F_32F_32F
| result
;
283 return ~0; /* Unsupported/unknown. */
286 boolean
r300_is_sampler_format_supported(enum pipe_format format
)
288 return r300_translate_texformat(format
) != ~0;
291 static void r300_setup_texture_state(struct r300_screen
* screen
, struct r300_texture
* tex
)
293 struct r300_texture_state
* state
= &tex
->state
;
294 struct pipe_texture
*pt
= &tex
->tex
;
295 boolean is_r500
= screen
->caps
->is_r500
;
297 state
->format0
= R300_TX_WIDTH((pt
->width0
- 1) & 0x7ff) |
298 R300_TX_HEIGHT((pt
->height0
- 1) & 0x7ff);
301 /* rectangles love this */
302 state
->format0
|= R300_TX_PITCH_EN
;
303 state
->format2
= (tex
->pitch
[0] - 1) & 0x1fff;
305 /* power of two textures (3D, mipmaps, and no pitch) */
306 state
->format0
|= R300_TX_DEPTH(util_logbase2(pt
->depth0
) & 0xf);
309 state
->format1
= r300_translate_texformat(pt
->format
);
310 if (pt
->target
== PIPE_TEXTURE_CUBE
) {
311 state
->format1
|= R300_TX_FORMAT_CUBIC_MAP
;
313 if (pt
->target
== PIPE_TEXTURE_3D
) {
314 state
->format1
|= R300_TX_FORMAT_3D
;
317 /* large textures on r500 */
320 if (pt
->width0
> 2048) {
321 state
->format2
|= R500_TXWIDTH_BIT11
;
323 if (pt
->height0
> 2048) {
324 state
->format2
|= R500_TXHEIGHT_BIT11
;
328 SCREEN_DBG(screen
, DBG_TEX
, "r300: Set texture state (%dx%d, %d levels)\n",
329 pt
->width0
, pt
->height0
, pt
->last_level
);
332 void r300_texture_reinterpret_format(struct pipe_screen
*screen
,
333 struct pipe_texture
*tex
,
334 enum pipe_format new_format
)
336 struct r300_screen
*r300screen
= r300_screen(screen
);
338 SCREEN_DBG(r300screen
, DBG_TEX
, "r300: Reinterpreting format: %s -> %s\n",
339 util_format_name(tex
->format
), util_format_name(new_format
));
341 tex
->format
= new_format
;
343 r300_setup_texture_state(r300_screen(screen
), (struct r300_texture
*)tex
);
346 unsigned r300_texture_get_offset(struct r300_texture
* tex
, unsigned level
,
347 unsigned zslice
, unsigned face
)
349 unsigned offset
= tex
->offset
[level
];
351 switch (tex
->tex
.target
) {
352 case PIPE_TEXTURE_3D
:
354 return offset
+ zslice
* tex
->layer_size
[level
];
356 case PIPE_TEXTURE_CUBE
:
358 return offset
+ face
* tex
->layer_size
[level
];
361 assert(zslice
== 0 && face
== 0);
367 * Return the width (dim==TILE_WIDTH) or height (dim==TILE_HEIGHT) of one tile
368 * of the given texture.
370 static unsigned r300_texture_get_tile_size(struct r300_texture
* tex
,
371 int dim
, boolean macrotile
)
373 unsigned pixsize
, tile_size
;
375 pixsize
= util_format_get_blocksize(tex
->tex
.format
);
376 tile_size
= microblock_table
[util_logbase2(pixsize
)][tex
->microtile
][dim
];
386 /* Return true if macrotiling should be enabled on the miplevel. */
387 static boolean
r300_texture_macro_switch(struct r300_texture
*tex
,
391 unsigned tile_width
, width
;
393 tile_width
= r300_texture_get_tile_size(tex
, TILE_WIDTH
, TRUE
);
394 width
= u_minify(tex
->tex
.width0
, level
);
396 /* See TX_FILTER1_n.MACRO_SWITCH. */
398 return width
>= tile_width
;
400 return width
> tile_width
;
405 * Return the stride, in bytes, of the texture images of the given texture
406 * at the given level.
408 unsigned r300_texture_get_stride(struct r300_screen
* screen
,
409 struct r300_texture
* tex
, unsigned level
)
411 unsigned tile_width
, width
;
413 if (tex
->stride_override
)
414 return tex
->stride_override
;
416 /* Check the level. */
417 if (level
> tex
->tex
.last_level
) {
418 SCREEN_DBG(screen
, DBG_TEX
, "%s: level (%u) > last_level (%u)\n",
419 __FUNCTION__
, level
, tex
->tex
.last_level
);
423 width
= u_minify(tex
->tex
.width0
, level
);
425 if (!util_format_is_compressed(tex
->tex
.format
)) {
426 tile_width
= r300_texture_get_tile_size(tex
, TILE_WIDTH
,
427 tex
->mip_macrotile
[level
]);
428 width
= align(width
, tile_width
);
430 return util_format_get_stride(tex
->tex
.format
, width
);
432 return align(util_format_get_stride(tex
->tex
.format
, width
), 32);
436 static unsigned r300_texture_get_nblocksy(struct r300_texture
* tex
,
439 unsigned height
, tile_height
;
441 height
= u_minify(tex
->tex
.height0
, level
);
443 if (!util_format_is_compressed(tex
->tex
.format
)) {
444 tile_height
= r300_texture_get_tile_size(tex
, TILE_HEIGHT
,
445 tex
->mip_macrotile
[level
]);
446 height
= align(height
, tile_height
);
449 return util_format_get_nblocksy(tex
->tex
.format
, height
);
452 static void r300_setup_miptree(struct r300_screen
* screen
,
453 struct r300_texture
* tex
)
455 struct pipe_texture
* base
= &tex
->tex
;
456 unsigned stride
, size
, layer_size
, nblocksy
, i
;
457 boolean rv350_mode
= screen
->caps
->family
>= CHIP_FAMILY_RV350
;
459 SCREEN_DBG(screen
, DBG_TEX
, "r300: Making miptree for texture, format %s\n",
460 util_format_name(base
->format
));
462 for (i
= 0; i
<= base
->last_level
; i
++) {
463 /* Let's see if this miplevel can be macrotiled. */
464 tex
->mip_macrotile
[i
] = (tex
->macrotile
== R300_BUFFER_TILED
&&
465 r300_texture_macro_switch(tex
, i
, rv350_mode
)) ?
466 R300_BUFFER_TILED
: R300_BUFFER_LINEAR
;
468 stride
= r300_texture_get_stride(screen
, tex
, i
);
469 nblocksy
= r300_texture_get_nblocksy(tex
, i
);
470 layer_size
= stride
* nblocksy
;
472 if (base
->target
== PIPE_TEXTURE_CUBE
)
473 size
= layer_size
* 6;
475 size
= layer_size
* u_minify(base
->depth0
, i
);
477 tex
->offset
[i
] = tex
->size
;
478 tex
->size
= tex
->offset
[i
] + size
;
479 tex
->layer_size
[i
] = layer_size
;
480 tex
->pitch
[i
] = stride
/ util_format_get_blocksize(base
->format
);
482 SCREEN_DBG(screen
, DBG_TEX
, "r300: Texture miptree: Level %d "
483 "(%dx%dx%d px, pitch %d bytes) %d bytes total, macrotiled %s\n",
484 i
, u_minify(base
->width0
, i
), u_minify(base
->height0
, i
),
485 u_minify(base
->depth0
, i
), stride
, tex
->size
,
486 tex
->mip_macrotile
[i
] ? "TRUE" : "FALSE");
490 static void r300_setup_flags(struct r300_texture
* tex
)
492 tex
->is_npot
= !util_is_power_of_two(tex
->tex
.width0
) ||
493 !util_is_power_of_two(tex
->tex
.height0
);
496 /* Create a new texture. */
497 static struct pipe_texture
*
498 r300_texture_create(struct pipe_screen
* screen
,
499 const struct pipe_texture
* template)
501 struct r300_texture
* tex
= CALLOC_STRUCT(r300_texture
);
502 struct r300_screen
* rscreen
= r300_screen(screen
);
503 struct radeon_winsys
* winsys
= (struct radeon_winsys
*)screen
->winsys
;
509 tex
->tex
= *template;
510 pipe_reference_init(&tex
->tex
.reference
, 1);
511 tex
->tex
.screen
= screen
;
513 r300_setup_flags(tex
);
514 r300_setup_miptree(rscreen
, tex
);
515 r300_setup_texture_state(rscreen
, tex
);
517 tex
->buffer
= screen
->buffer_create(screen
, 2048,
518 PIPE_BUFFER_USAGE_PIXEL
,
520 winsys
->buffer_set_tiling(winsys
, tex
->buffer
,
522 tex
->microtile
!= R300_BUFFER_LINEAR
,
523 tex
->macrotile
!= R300_BUFFER_LINEAR
);
530 return (struct pipe_texture
*)tex
;
533 static void r300_texture_destroy(struct pipe_texture
* texture
)
535 struct r300_texture
* tex
= (struct r300_texture
*)texture
;
537 pipe_buffer_reference(&tex
->buffer
, NULL
);
542 static struct pipe_surface
* r300_get_tex_surface(struct pipe_screen
* screen
,
543 struct pipe_texture
* texture
,
549 struct r300_texture
* tex
= (struct r300_texture
*)texture
;
550 struct pipe_surface
* surface
= CALLOC_STRUCT(pipe_surface
);
553 offset
= r300_texture_get_offset(tex
, level
, zslice
, face
);
556 pipe_reference_init(&surface
->reference
, 1);
557 pipe_texture_reference(&surface
->texture
, texture
);
558 surface
->format
= texture
->format
;
559 surface
->width
= u_minify(texture
->width0
, level
);
560 surface
->height
= u_minify(texture
->height0
, level
);
561 surface
->offset
= offset
;
562 surface
->usage
= flags
;
563 surface
->zslice
= zslice
;
564 surface
->texture
= texture
;
565 surface
->face
= face
;
566 surface
->level
= level
;
572 static void r300_tex_surface_destroy(struct pipe_surface
* s
)
574 pipe_texture_reference(&s
->texture
, NULL
);
578 static struct pipe_texture
*
579 r300_texture_blanket(struct pipe_screen
* screen
,
580 const struct pipe_texture
* base
,
581 const unsigned* stride
,
582 struct pipe_buffer
* buffer
)
584 struct r300_texture
* tex
;
585 struct r300_screen
* rscreen
= r300_screen(screen
);
587 /* Support only 2D textures without mipmaps */
588 if (base
->target
!= PIPE_TEXTURE_2D
||
590 base
->last_level
!= 0) {
594 tex
= CALLOC_STRUCT(r300_texture
);
600 pipe_reference_init(&tex
->tex
.reference
, 1);
601 tex
->tex
.screen
= screen
;
603 tex
->stride_override
= *stride
;
604 tex
->pitch
[0] = *stride
/ util_format_get_blocksize(base
->format
);
606 r300_setup_flags(tex
);
607 r300_setup_texture_state(rscreen
, tex
);
609 pipe_buffer_reference(&tex
->buffer
, buffer
);
611 return (struct pipe_texture
*)tex
;
614 static struct pipe_video_surface
*
615 r300_video_surface_create(struct pipe_screen
*screen
,
616 enum pipe_video_chroma_format chroma_format
,
617 unsigned width
, unsigned height
)
619 struct r300_video_surface
*r300_vsfc
;
620 struct pipe_texture
template;
623 assert(width
&& height
);
625 r300_vsfc
= CALLOC_STRUCT(r300_video_surface
);
629 pipe_reference_init(&r300_vsfc
->base
.reference
, 1);
630 r300_vsfc
->base
.screen
= screen
;
631 r300_vsfc
->base
.chroma_format
= chroma_format
;
632 r300_vsfc
->base
.width
= width
;
633 r300_vsfc
->base
.height
= height
;
635 memset(&template, 0, sizeof(struct pipe_texture
));
636 template.target
= PIPE_TEXTURE_2D
;
637 template.format
= PIPE_FORMAT_X8R8G8B8_UNORM
;
638 template.last_level
= 0;
639 template.width0
= util_next_power_of_two(width
);
640 template.height0
= util_next_power_of_two(height
);
642 template.tex_usage
= PIPE_TEXTURE_USAGE_SAMPLER
|
643 PIPE_TEXTURE_USAGE_RENDER_TARGET
;
645 r300_vsfc
->tex
= screen
->texture_create(screen
, &template);
652 return &r300_vsfc
->base
;
655 static void r300_video_surface_destroy(struct pipe_video_surface
*vsfc
)
657 struct r300_video_surface
*r300_vsfc
= r300_video_surface(vsfc
);
658 pipe_texture_reference(&r300_vsfc
->tex
, NULL
);
662 void r300_init_screen_texture_functions(struct pipe_screen
* screen
)
664 screen
->texture_create
= r300_texture_create
;
665 screen
->texture_destroy
= r300_texture_destroy
;
666 screen
->get_tex_surface
= r300_get_tex_surface
;
667 screen
->tex_surface_destroy
= r300_tex_surface_destroy
;
668 screen
->texture_blanket
= r300_texture_blanket
;
670 screen
->video_surface_create
= r300_video_surface_create
;
671 screen
->video_surface_destroy
= r300_video_surface_destroy
;
674 boolean
r300_get_texture_buffer(struct pipe_screen
* screen
,
675 struct pipe_texture
* texture
,
676 struct pipe_buffer
** buffer
,
679 struct r300_texture
* tex
= (struct r300_texture
*)texture
;
684 pipe_buffer_reference(buffer
, tex
->buffer
);
687 *stride
= r300_texture_get_stride(r300_screen(screen
), tex
, 0);