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 /* Always include headers in the reverse order!! ~ M. */
25 #include "r300_texture.h"
27 #include "r300_context.h"
29 #include "r300_texture_desc.h"
30 #include "r300_transfer.h"
31 #include "r300_screen.h"
32 #include "r300_winsys.h"
34 #include "util/u_format.h"
35 #include "util/u_format_s3tc.h"
36 #include "util/u_math.h"
37 #include "util/u_memory.h"
39 #include "pipe/p_screen.h"
41 unsigned r300_get_swizzle_combined(const unsigned char *swizzle_format
,
42 const unsigned char *swizzle_view
)
45 unsigned char swizzle
[4];
47 const uint32_t swizzle_shift
[4] = {
48 R300_TX_FORMAT_R_SHIFT
,
49 R300_TX_FORMAT_G_SHIFT
,
50 R300_TX_FORMAT_B_SHIFT
,
51 R300_TX_FORMAT_A_SHIFT
53 const uint32_t swizzle_bit
[4] = {
61 /* Combine two sets of swizzles. */
62 for (i
= 0; i
< 4; i
++) {
63 swizzle
[i
] = swizzle_view
[i
] <= UTIL_FORMAT_SWIZZLE_W
?
64 swizzle_format
[swizzle_view
[i
]] : swizzle_view
[i
];
67 memcpy(swizzle
, swizzle_format
, 4);
71 for (i
= 0; i
< 4; i
++) {
73 case UTIL_FORMAT_SWIZZLE_Y
:
74 result
|= swizzle_bit
[1] << swizzle_shift
[i
];
76 case UTIL_FORMAT_SWIZZLE_Z
:
77 result
|= swizzle_bit
[2] << swizzle_shift
[i
];
79 case UTIL_FORMAT_SWIZZLE_W
:
80 result
|= swizzle_bit
[3] << swizzle_shift
[i
];
82 case UTIL_FORMAT_SWIZZLE_0
:
83 result
|= R300_TX_FORMAT_ZERO
<< swizzle_shift
[i
];
85 case UTIL_FORMAT_SWIZZLE_1
:
86 result
|= R300_TX_FORMAT_ONE
<< swizzle_shift
[i
];
88 default: /* UTIL_FORMAT_SWIZZLE_X */
89 result
|= swizzle_bit
[0] << swizzle_shift
[i
];
95 /* Translate a pipe_format into a useful texture format for sampling.
97 * Some special formats are translated directly using R300_EASY_TX_FORMAT,
98 * but the majority of them is translated in a generic way, automatically
99 * supporting all the formats hw can support.
101 * R300_EASY_TX_FORMAT swizzles the texture.
102 * Note the signature of R300_EASY_TX_FORMAT:
103 * R300_EASY_TX_FORMAT(B, G, R, A, FORMAT);
105 * The FORMAT specifies how the texture sampler will treat the texture, and
106 * makes available X, Y, Z, W, ZERO, and ONE for swizzling. */
107 uint32_t r300_translate_texformat(enum pipe_format format
,
108 const unsigned char *swizzle_view
)
111 const struct util_format_description
*desc
;
113 boolean uniform
= TRUE
;
114 const uint32_t sign_bit
[4] = {
115 R300_TX_FORMAT_SIGNED_X
,
116 R300_TX_FORMAT_SIGNED_Y
,
117 R300_TX_FORMAT_SIGNED_Z
,
118 R300_TX_FORMAT_SIGNED_W
,
121 desc
= util_format_description(format
);
123 /* Colorspace (return non-RGB formats directly). */
124 switch (desc
->colorspace
) {
125 /* Depth stencil formats.
126 * Swizzles are added in r300_merge_textures_and_samplers. */
127 case UTIL_FORMAT_COLORSPACE_ZS
:
129 case PIPE_FORMAT_Z16_UNORM
:
130 return R300_TX_FORMAT_X16
;
131 case PIPE_FORMAT_X8Z24_UNORM
:
132 case PIPE_FORMAT_S8_USCALED_Z24_UNORM
:
133 return R500_TX_FORMAT_Y8X24
;
135 return ~0; /* Unsupported. */
139 case UTIL_FORMAT_COLORSPACE_YUV
:
140 result
|= R300_TX_FORMAT_YUV_TO_RGB
;
143 case PIPE_FORMAT_UYVY
:
144 return R300_EASY_TX_FORMAT(X
, Y
, Z
, ONE
, YVYU422
) | result
;
145 case PIPE_FORMAT_YUYV
:
146 return R300_EASY_TX_FORMAT(X
, Y
, Z
, ONE
, VYUY422
) | result
;
148 return ~0; /* Unsupported/unknown. */
151 /* Add gamma correction. */
152 case UTIL_FORMAT_COLORSPACE_SRGB
:
153 result
|= R300_TX_FORMAT_GAMMA
;
158 /* Same as YUV but without the YUR->RGB conversion. */
159 case PIPE_FORMAT_R8G8_B8G8_UNORM
:
160 return R300_EASY_TX_FORMAT(X
, Y
, Z
, ONE
, YVYU422
) | result
;
161 case PIPE_FORMAT_G8R8_G8B8_UNORM
:
162 return R300_EASY_TX_FORMAT(X
, Y
, Z
, ONE
, VYUY422
) | result
;
167 result
|= r300_get_swizzle_combined(desc
->swizzle
, swizzle_view
);
170 if (desc
->layout
== UTIL_FORMAT_LAYOUT_S3TC
) {
171 if (!util_format_s3tc_enabled
) {
172 return ~0; /* Unsupported. */
176 case PIPE_FORMAT_DXT1_RGB
:
177 case PIPE_FORMAT_DXT1_RGBA
:
178 case PIPE_FORMAT_DXT1_SRGB
:
179 case PIPE_FORMAT_DXT1_SRGBA
:
180 return R300_TX_FORMAT_DXT1
| result
;
181 case PIPE_FORMAT_DXT3_RGBA
:
182 case PIPE_FORMAT_DXT3_SRGBA
:
183 return R300_TX_FORMAT_DXT3
| result
;
184 case PIPE_FORMAT_DXT5_RGBA
:
185 case PIPE_FORMAT_DXT5_SRGBA
:
186 return R300_TX_FORMAT_DXT5
| result
;
188 return ~0; /* Unsupported/unknown. */
193 for (i
= 0; i
< desc
->nr_channels
; i
++) {
194 if (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_SIGNED
) {
195 result
|= sign_bit
[i
];
199 /* This is truly a special format.
200 * It stores R8G8 and B is computed using sqrt(1 - R^2 - G^2)
201 * in the sampler unit. Also known as D3DFMT_CxV8U8. */
202 if (format
== PIPE_FORMAT_R8G8Bx_SNORM
) {
203 return R300_TX_FORMAT_CxV8U8
| result
;
207 if (desc
->layout
== UTIL_FORMAT_LAYOUT_RGTC
) {
209 case PIPE_FORMAT_RGTC1_UNORM
:
210 case PIPE_FORMAT_RGTC1_SNORM
:
211 return R500_TX_FORMAT_ATI1N
| result
;
212 case PIPE_FORMAT_RGTC2_UNORM
:
213 case PIPE_FORMAT_RGTC2_SNORM
:
214 return R400_TX_FORMAT_ATI2N
| result
;
216 return ~0; /* Unsupported/unknown. */
220 /* See whether the components are of the same size. */
221 for (i
= 1; i
< desc
->nr_channels
; i
++) {
222 uniform
= uniform
&& desc
->channel
[0].size
== desc
->channel
[i
].size
;
225 /* Non-uniform formats. */
227 switch (desc
->nr_channels
) {
229 if (desc
->channel
[0].size
== 5 &&
230 desc
->channel
[1].size
== 6 &&
231 desc
->channel
[2].size
== 5) {
232 return R300_TX_FORMAT_Z5Y6X5
| result
;
234 if (desc
->channel
[0].size
== 5 &&
235 desc
->channel
[1].size
== 5 &&
236 desc
->channel
[2].size
== 6) {
237 return R300_TX_FORMAT_Z6Y5X5
| result
;
239 return ~0; /* Unsupported/unknown. */
242 if (desc
->channel
[0].size
== 5 &&
243 desc
->channel
[1].size
== 5 &&
244 desc
->channel
[2].size
== 5 &&
245 desc
->channel
[3].size
== 1) {
246 return R300_TX_FORMAT_W1Z5Y5X5
| result
;
248 if (desc
->channel
[0].size
== 10 &&
249 desc
->channel
[1].size
== 10 &&
250 desc
->channel
[2].size
== 10 &&
251 desc
->channel
[3].size
== 2) {
252 return R300_TX_FORMAT_W2Z10Y10X10
| result
;
255 return ~0; /* Unsupported/unknown. */
258 /* And finally, uniform formats. */
259 switch (desc
->channel
[0].type
) {
260 case UTIL_FORMAT_TYPE_UNSIGNED
:
261 case UTIL_FORMAT_TYPE_SIGNED
:
262 if (!desc
->channel
[0].normalized
&&
263 desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_SRGB
) {
267 switch (desc
->channel
[0].size
) {
269 switch (desc
->nr_channels
) {
271 return R300_TX_FORMAT_Y4X4
| result
;
273 return R300_TX_FORMAT_W4Z4Y4X4
| result
;
278 switch (desc
->nr_channels
) {
280 return R300_TX_FORMAT_X8
| result
;
282 return R300_TX_FORMAT_Y8X8
| result
;
284 return R300_TX_FORMAT_W8Z8Y8X8
| result
;
289 switch (desc
->nr_channels
) {
291 return R300_TX_FORMAT_X16
| result
;
293 return R300_TX_FORMAT_Y16X16
| result
;
295 return R300_TX_FORMAT_W16Z16Y16X16
| result
;
300 case UTIL_FORMAT_TYPE_FLOAT
:
301 switch (desc
->channel
[0].size
) {
303 switch (desc
->nr_channels
) {
305 return R300_TX_FORMAT_16F
| result
;
307 return R300_TX_FORMAT_16F_16F
| result
;
309 return R300_TX_FORMAT_16F_16F_16F_16F
| result
;
314 switch (desc
->nr_channels
) {
316 return R300_TX_FORMAT_32F
| result
;
318 return R300_TX_FORMAT_32F_32F
| result
;
320 return R300_TX_FORMAT_32F_32F_32F_32F
| result
;
325 return ~0; /* Unsupported/unknown. */
328 uint32_t r500_tx_format_msb_bit(enum pipe_format format
)
331 case PIPE_FORMAT_RGTC1_UNORM
:
332 case PIPE_FORMAT_RGTC1_SNORM
:
333 case PIPE_FORMAT_X8Z24_UNORM
:
334 case PIPE_FORMAT_S8_USCALED_Z24_UNORM
:
335 return R500_TXFORMAT_MSB
;
341 /* Buffer formats. */
343 /* Colorbuffer formats. This is the unswizzled format of the RB3D block's
344 * output. For the swizzling of the targets, check the shader's format. */
345 static uint32_t r300_translate_colorformat(enum pipe_format format
)
349 case PIPE_FORMAT_A8_UNORM
:
350 case PIPE_FORMAT_I8_UNORM
:
351 case PIPE_FORMAT_L8_UNORM
:
352 case PIPE_FORMAT_R8_UNORM
:
353 case PIPE_FORMAT_R8_SNORM
:
354 return R300_COLOR_FORMAT_I8
;
356 /* 16-bit buffers. */
357 case PIPE_FORMAT_B5G6R5_UNORM
:
358 return R300_COLOR_FORMAT_RGB565
;
360 case PIPE_FORMAT_B5G5R5A1_UNORM
:
361 case PIPE_FORMAT_B5G5R5X1_UNORM
:
362 return R300_COLOR_FORMAT_ARGB1555
;
364 case PIPE_FORMAT_B4G4R4A4_UNORM
:
365 case PIPE_FORMAT_B4G4R4X4_UNORM
:
366 return R300_COLOR_FORMAT_ARGB4444
;
368 /* 32-bit buffers. */
369 case PIPE_FORMAT_B8G8R8A8_UNORM
:
370 case PIPE_FORMAT_B8G8R8X8_UNORM
:
371 case PIPE_FORMAT_A8R8G8B8_UNORM
:
372 case PIPE_FORMAT_X8R8G8B8_UNORM
:
373 case PIPE_FORMAT_A8B8G8R8_UNORM
:
374 case PIPE_FORMAT_R8G8B8A8_SNORM
:
375 case PIPE_FORMAT_X8B8G8R8_UNORM
:
376 case PIPE_FORMAT_R8G8B8X8_UNORM
:
377 case PIPE_FORMAT_R8SG8SB8UX8U_NORM
:
378 return R300_COLOR_FORMAT_ARGB8888
;
380 case PIPE_FORMAT_R10G10B10A2_UNORM
:
381 case PIPE_FORMAT_R10G10B10X2_SNORM
:
382 case PIPE_FORMAT_B10G10R10A2_UNORM
:
383 case PIPE_FORMAT_R10SG10SB10SA2U_NORM
:
384 return R500_COLOR_FORMAT_ARGB2101010
; /* R5xx-only? */
386 /* 64-bit buffers. */
387 case PIPE_FORMAT_R16G16B16A16_UNORM
:
388 case PIPE_FORMAT_R16G16B16A16_SNORM
:
389 case PIPE_FORMAT_R16G16B16A16_FLOAT
:
390 return R300_COLOR_FORMAT_ARGB16161616
;
392 /* 128-bit buffers. */
393 case PIPE_FORMAT_R32G32B32A32_FLOAT
:
394 return R300_COLOR_FORMAT_ARGB32323232
;
397 case PIPE_FORMAT_UYVY
:
398 return R300_COLOR_FORMAT_YVYU
;
399 case PIPE_FORMAT_YUYV
:
400 return R300_COLOR_FORMAT_VYUY
;
402 return ~0; /* Unsupported. */
406 /* Depthbuffer and stencilbuffer. Thankfully, we only support two flavors. */
407 static uint32_t r300_translate_zsformat(enum pipe_format format
)
410 /* 16-bit depth, no stencil */
411 case PIPE_FORMAT_Z16_UNORM
:
412 return R300_DEPTHFORMAT_16BIT_INT_Z
;
413 /* 24-bit depth, ignored stencil */
414 case PIPE_FORMAT_X8Z24_UNORM
:
415 /* 24-bit depth, 8-bit stencil */
416 case PIPE_FORMAT_S8_USCALED_Z24_UNORM
:
417 return R300_DEPTHFORMAT_24BIT_INT_Z_8BIT_STENCIL
;
419 return ~0; /* Unsupported. */
423 /* Shader output formats. This is essentially the swizzle from the shader
426 * Note that formats are stored from C3 to C0. */
427 static uint32_t r300_translate_out_fmt(enum pipe_format format
)
429 uint32_t modifier
= 0;
431 const struct util_format_description
*desc
;
432 static const uint32_t sign_bit
[4] = {
439 desc
= util_format_description(format
);
441 /* Specifies how the shader output is written to the fog unit. */
442 if (desc
->channel
[0].type
== UTIL_FORMAT_TYPE_FLOAT
) {
443 if (desc
->channel
[0].size
== 32) {
444 modifier
|= R300_US_OUT_FMT_C4_32_FP
;
446 modifier
|= R300_US_OUT_FMT_C4_16_FP
;
449 if (desc
->channel
[0].size
== 16) {
450 modifier
|= R300_US_OUT_FMT_C4_16
;
452 /* C4_8 seems to be used for the formats whose pixel size
454 modifier
|= R300_US_OUT_FMT_C4_8
;
459 for (i
= 0; i
< 4; i
++)
460 if (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_SIGNED
) {
461 modifier
|= sign_bit
[i
];
464 /* Add swizzles and return. */
467 * COLORFORMAT_I8 stores the C2 component. */
468 case PIPE_FORMAT_A8_UNORM
:
469 return modifier
| R300_C2_SEL_A
;
470 case PIPE_FORMAT_I8_UNORM
:
471 case PIPE_FORMAT_L8_UNORM
:
472 case PIPE_FORMAT_R8_UNORM
:
473 case PIPE_FORMAT_R8_SNORM
:
474 return modifier
| R300_C2_SEL_R
;
477 case PIPE_FORMAT_B5G6R5_UNORM
:
478 case PIPE_FORMAT_B5G5R5A1_UNORM
:
479 case PIPE_FORMAT_B5G5R5X1_UNORM
:
480 case PIPE_FORMAT_B4G4R4A4_UNORM
:
481 case PIPE_FORMAT_B4G4R4X4_UNORM
:
482 case PIPE_FORMAT_B8G8R8A8_UNORM
:
483 case PIPE_FORMAT_B8G8R8X8_UNORM
:
484 case PIPE_FORMAT_B10G10R10A2_UNORM
:
486 R300_C0_SEL_B
| R300_C1_SEL_G
|
487 R300_C2_SEL_R
| R300_C3_SEL_A
;
490 case PIPE_FORMAT_A8R8G8B8_UNORM
:
491 case PIPE_FORMAT_X8R8G8B8_UNORM
:
493 R300_C0_SEL_A
| R300_C1_SEL_R
|
494 R300_C2_SEL_G
| R300_C3_SEL_B
;
497 case PIPE_FORMAT_A8B8G8R8_UNORM
:
498 case PIPE_FORMAT_X8B8G8R8_UNORM
:
500 R300_C0_SEL_A
| R300_C1_SEL_B
|
501 R300_C2_SEL_G
| R300_C3_SEL_R
;
504 case PIPE_FORMAT_R8G8B8X8_UNORM
:
505 case PIPE_FORMAT_R8G8B8A8_SNORM
:
506 case PIPE_FORMAT_R8SG8SB8UX8U_NORM
:
507 case PIPE_FORMAT_R10G10B10A2_UNORM
:
508 case PIPE_FORMAT_R10G10B10X2_SNORM
:
509 case PIPE_FORMAT_R10SG10SB10SA2U_NORM
:
510 case PIPE_FORMAT_R16G16B16A16_UNORM
:
511 case PIPE_FORMAT_R16G16B16A16_SNORM
:
512 case PIPE_FORMAT_R16G16B16A16_FLOAT
:
513 case PIPE_FORMAT_R32G32B32A32_FLOAT
:
515 R300_C0_SEL_R
| R300_C1_SEL_G
|
516 R300_C2_SEL_B
| R300_C3_SEL_A
;
519 return ~0; /* Unsupported. */
523 boolean
r300_is_colorbuffer_format_supported(enum pipe_format format
)
525 return r300_translate_colorformat(format
) != ~0 &&
526 r300_translate_out_fmt(format
) != ~0;
529 boolean
r300_is_zs_format_supported(enum pipe_format format
)
531 return r300_translate_zsformat(format
) != ~0;
534 boolean
r300_is_sampler_format_supported(enum pipe_format format
)
536 return r300_translate_texformat(format
, 0) != ~0;
539 static void r300_texture_setup_immutable_state(struct r300_screen
* screen
,
540 struct r300_texture
* tex
)
542 struct r300_texture_format_state
* f
= &tex
->tx_format
;
543 struct pipe_resource
*pt
= &tex
->desc
.b
.b
;
544 boolean is_r500
= screen
->caps
.is_r500
;
546 /* Set sampler state. */
547 f
->format0
= R300_TX_WIDTH((pt
->width0
- 1) & 0x7ff) |
548 R300_TX_HEIGHT((pt
->height0
- 1) & 0x7ff);
550 if (tex
->desc
.uses_stride_addressing
) {
551 /* rectangles love this */
552 f
->format0
|= R300_TX_PITCH_EN
;
553 f
->format2
= (tex
->desc
.stride_in_pixels
[0] - 1) & 0x1fff;
555 /* Power of two textures (3D, mipmaps, and no pitch),
556 * also NPOT textures with a width being POT. */
557 f
->format0
|= R300_TX_DEPTH(util_logbase2(pt
->depth0
) & 0xf);
561 if (pt
->target
== PIPE_TEXTURE_CUBE
) {
562 f
->format1
|= R300_TX_FORMAT_CUBIC_MAP
;
564 if (pt
->target
== PIPE_TEXTURE_3D
) {
565 f
->format1
|= R300_TX_FORMAT_3D
;
568 /* large textures on r500 */
571 if (pt
->width0
> 2048) {
572 f
->format2
|= R500_TXWIDTH_BIT11
;
574 if (pt
->height0
> 2048) {
575 f
->format2
|= R500_TXHEIGHT_BIT11
;
579 f
->tile_config
= R300_TXO_MACRO_TILE(tex
->desc
.macrotile
[0]) |
580 R300_TXO_MICRO_TILE(tex
->desc
.microtile
);
583 static void r300_texture_setup_fb_state(struct r300_screen
* screen
,
584 struct r300_texture
* tex
)
588 /* Set framebuffer state. */
589 if (util_format_is_depth_or_stencil(tex
->desc
.b
.b
.format
)) {
590 for (i
= 0; i
<= tex
->desc
.b
.b
.last_level
; i
++) {
591 tex
->fb_state
.pitch
[i
] =
592 tex
->desc
.stride_in_pixels
[i
] |
593 R300_DEPTHMACROTILE(tex
->desc
.macrotile
[i
]) |
594 R300_DEPTHMICROTILE(tex
->desc
.microtile
);
596 tex
->fb_state
.format
= r300_translate_zsformat(tex
->desc
.b
.b
.format
);
598 for (i
= 0; i
<= tex
->desc
.b
.b
.last_level
; i
++) {
599 tex
->fb_state
.pitch
[i
] =
600 tex
->desc
.stride_in_pixels
[i
] |
601 r300_translate_colorformat(tex
->desc
.b
.b
.format
) |
602 R300_COLOR_TILE(tex
->desc
.macrotile
[i
]) |
603 R300_COLOR_MICROTILE(tex
->desc
.microtile
);
605 tex
->fb_state
.format
= r300_translate_out_fmt(tex
->desc
.b
.b
.format
);
609 void r300_texture_reinterpret_format(struct pipe_screen
*screen
,
610 struct pipe_resource
*tex
,
611 enum pipe_format new_format
)
613 struct r300_screen
*r300screen
= r300_screen(screen
);
615 SCREEN_DBG(r300screen
, DBG_TEX
,
616 "r300: texture_reinterpret_format: %s -> %s\n",
617 util_format_short_name(tex
->format
),
618 util_format_short_name(new_format
));
620 tex
->format
= new_format
;
622 r300_texture_setup_fb_state(r300_screen(screen
), r300_texture(tex
));
625 static unsigned r300_texture_is_referenced(struct pipe_context
*context
,
626 struct pipe_resource
*texture
,
627 unsigned face
, unsigned level
)
629 struct r300_context
*r300
= r300_context(context
);
630 struct r300_texture
*rtex
= (struct r300_texture
*)texture
;
632 if (r300
->rws
->cs_is_buffer_referenced(r300
->cs
,
633 rtex
->buffer
, R300_REF_CS
))
634 return PIPE_REFERENCED_FOR_READ
| PIPE_REFERENCED_FOR_WRITE
;
636 return PIPE_UNREFERENCED
;
639 static void r300_texture_destroy(struct pipe_screen
*screen
,
640 struct pipe_resource
* texture
)
642 struct r300_texture
* tex
= (struct r300_texture
*)texture
;
643 struct r300_winsys_screen
*rws
= (struct r300_winsys_screen
*)texture
->screen
->winsys
;
645 rws
->buffer_reference(rws
, &tex
->buffer
, NULL
);
649 static boolean
r300_texture_get_handle(struct pipe_screen
* screen
,
650 struct pipe_resource
*texture
,
651 struct winsys_handle
*whandle
)
653 struct r300_winsys_screen
*rws
= (struct r300_winsys_screen
*)screen
->winsys
;
654 struct r300_texture
* tex
= (struct r300_texture
*)texture
;
660 return rws
->buffer_get_handle(rws
, tex
->buffer
,
661 tex
->desc
.stride_in_bytes
[0], whandle
);
664 struct u_resource_vtbl r300_texture_vtbl
=
666 r300_texture_get_handle
, /* get_handle */
667 r300_texture_destroy
, /* resource_destroy */
668 r300_texture_is_referenced
, /* is_resource_referenced */
669 r300_texture_get_transfer
, /* get_transfer */
670 r300_texture_transfer_destroy
, /* transfer_destroy */
671 r300_texture_transfer_map
, /* transfer_map */
672 u_default_transfer_flush_region
, /* transfer_flush_region */
673 r300_texture_transfer_unmap
, /* transfer_unmap */
674 u_default_transfer_inline_write
/* transfer_inline_write */
677 /* The common texture constructor. */
678 static struct r300_texture
*
679 r300_texture_create_object(struct r300_screen
*rscreen
,
680 const struct pipe_resource
*base
,
681 enum r300_buffer_tiling microtile
,
682 enum r300_buffer_tiling macrotile
,
683 unsigned stride_in_bytes_override
,
684 unsigned max_buffer_size
,
685 struct r300_winsys_buffer
*buffer
)
687 struct r300_winsys_screen
*rws
= rscreen
->rws
;
688 struct r300_texture
*tex
= CALLOC_STRUCT(r300_texture
);
691 rws
->buffer_reference(rws
, &buffer
, NULL
);
695 /* Initialize the descriptor. */
696 if (!r300_texture_desc_init(rscreen
, &tex
->desc
, base
,
697 microtile
, macrotile
,
698 stride_in_bytes_override
,
701 rws
->buffer_reference(rws
, &buffer
, NULL
);
705 /* Initialize the hardware state. */
706 r300_texture_setup_immutable_state(rscreen
, tex
);
707 r300_texture_setup_fb_state(rscreen
, tex
);
709 tex
->desc
.b
.vtbl
= &r300_texture_vtbl
;
710 pipe_reference_init(&tex
->desc
.b
.b
.reference
, 1);
711 tex
->domain
= base
->flags
& R300_RESOURCE_FLAG_TRANSFER
?
713 R300_DOMAIN_VRAM
| R300_DOMAIN_GTT
;
714 tex
->buffer
= buffer
;
716 /* Create the backing buffer if needed. */
718 tex
->buffer
= rws
->buffer_create(rws
, tex
->desc
.size_in_bytes
, 2048,
719 base
->bind
, base
->usage
, tex
->domain
);
727 rws
->buffer_set_tiling(rws
, tex
->buffer
,
728 tex
->desc
.microtile
, tex
->desc
.macrotile
[0],
729 tex
->desc
.stride_in_bytes
[0]);
734 /* Create a new texture. */
735 struct pipe_resource
*r300_texture_create(struct pipe_screen
*screen
,
736 const struct pipe_resource
*base
)
738 struct r300_screen
*rscreen
= r300_screen(screen
);
739 enum r300_buffer_tiling microtile
, macrotile
;
741 /* Refuse to create a texture with size 0. */
743 (!base
->height0
&& (base
->target
== PIPE_TEXTURE_2D
||
744 base
->target
== PIPE_TEXTURE_CUBE
)) ||
745 (!base
->depth0
&& base
->target
== PIPE_TEXTURE_3D
)) {
746 fprintf(stderr
, "r300: texture_create: "
747 "Got invalid texture dimensions: %ix%ix%i\n",
748 base
->width0
, base
->height0
, base
->depth0
);
752 if ((base
->flags
& R300_RESOURCE_FLAG_TRANSFER
) ||
753 (base
->bind
& PIPE_BIND_SCANOUT
)) {
754 microtile
= R300_BUFFER_LINEAR
;
755 macrotile
= R300_BUFFER_LINEAR
;
757 microtile
= R300_BUFFER_SELECT_LAYOUT
;
758 macrotile
= R300_BUFFER_SELECT_LAYOUT
;
761 return (struct pipe_resource
*)
762 r300_texture_create_object(rscreen
, base
, microtile
, macrotile
,
766 struct pipe_resource
*r300_texture_from_handle(struct pipe_screen
*screen
,
767 const struct pipe_resource
*base
,
768 struct winsys_handle
*whandle
)
770 struct r300_winsys_screen
*rws
= (struct r300_winsys_screen
*)screen
->winsys
;
771 struct r300_screen
*rscreen
= r300_screen(screen
);
772 struct r300_winsys_buffer
*buffer
;
773 enum r300_buffer_tiling microtile
, macrotile
;
774 unsigned stride
, size
;
776 /* Support only 2D textures without mipmaps */
777 if (base
->target
!= PIPE_TEXTURE_2D
||
779 base
->last_level
!= 0) {
783 buffer
= rws
->buffer_from_handle(rws
, whandle
, &stride
, &size
);
787 rws
->buffer_get_tiling(rws
, buffer
, µtile
, ¯otile
);
789 /* Enforce a microtiled zbuffer. */
790 if (util_format_is_depth_or_stencil(base
->format
) &&
791 microtile
== R300_BUFFER_LINEAR
) {
792 switch (util_format_get_blocksize(base
->format
)) {
794 microtile
= R300_BUFFER_TILED
;
798 if (rws
->get_value(rws
, R300_VID_SQUARE_TILING_SUPPORT
))
799 microtile
= R300_BUFFER_SQUARETILED
;
804 return (struct pipe_resource
*)
805 r300_texture_create_object(rscreen
, base
, microtile
, macrotile
,
806 stride
, size
, buffer
);
809 /* Not required to implement u_resource_vtbl, consider moving to another file:
811 struct pipe_surface
* r300_get_tex_surface(struct pipe_screen
* screen
,
812 struct pipe_resource
* texture
,
818 struct r300_texture
* tex
= r300_texture(texture
);
819 struct r300_surface
* surface
= CALLOC_STRUCT(r300_surface
);
822 uint32_t offset
, tile_height
;
824 pipe_reference_init(&surface
->base
.reference
, 1);
825 pipe_resource_reference(&surface
->base
.texture
, texture
);
826 surface
->base
.format
= texture
->format
;
827 surface
->base
.width
= u_minify(texture
->width0
, level
);
828 surface
->base
.height
= u_minify(texture
->height0
, level
);
829 surface
->base
.usage
= flags
;
830 surface
->base
.zslice
= zslice
;
831 surface
->base
.face
= face
;
832 surface
->base
.level
= level
;
834 surface
->buffer
= tex
->buffer
;
836 /* Prefer VRAM if there are multiple domains to choose from. */
837 surface
->domain
= tex
->domain
;
838 if (surface
->domain
& R300_DOMAIN_VRAM
)
839 surface
->domain
&= ~R300_DOMAIN_GTT
;
841 surface
->offset
= r300_texture_get_offset(&tex
->desc
,
842 level
, zslice
, face
);
843 surface
->pitch
= tex
->fb_state
.pitch
[level
];
844 surface
->format
= tex
->fb_state
.format
;
846 /* Parameters for the CBZB clear. */
847 surface
->cbzb_allowed
= tex
->desc
.cbzb_allowed
[level
];
848 surface
->cbzb_width
= align(surface
->base
.width
, 64);
850 /* Height must be aligned to the size of a tile. */
851 tile_height
= r300_get_pixel_alignment(tex
->desc
.b
.b
.format
,
852 tex
->desc
.b
.b
.nr_samples
,
854 tex
->desc
.macrotile
[level
],
857 surface
->cbzb_height
= align((surface
->base
.height
+ 1) / 2,
860 /* Offset must be aligned to 2K and must point at the beginning
862 offset
= surface
->offset
+
863 tex
->desc
.stride_in_bytes
[level
] * surface
->cbzb_height
;
864 surface
->cbzb_midpoint_offset
= offset
& ~2047;
866 surface
->cbzb_pitch
= surface
->pitch
& 0x1ffffc;
868 if (util_format_get_blocksizebits(surface
->base
.format
) == 32)
869 surface
->cbzb_format
= R300_DEPTHFORMAT_24BIT_INT_Z_8BIT_STENCIL
;
871 surface
->cbzb_format
= R300_DEPTHFORMAT_16BIT_INT_Z
;
873 SCREEN_DBG(r300_screen(screen
), DBG_CBZB
,
874 "CBZB Dim: %ix%i, Misalignment: %i, Macro: %s\n",
875 surface
->cbzb_width
, surface
->cbzb_height
,
877 tex
->desc
.macrotile
[level
] ? "YES" : " NO");
880 return &surface
->base
;
883 /* Not required to implement u_resource_vtbl, consider moving to another file:
885 void r300_tex_surface_destroy(struct pipe_surface
* s
)
887 pipe_resource_reference(&s
->texture
, NULL
);