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"
38 #include "util/u_mm.h"
40 #include "pipe/p_screen.h"
42 unsigned r300_get_swizzle_combined(const unsigned char *swizzle_format,
43 const unsigned char *swizzle_view,
47 unsigned char swizzle[4];
49 const uint32_t swizzle_shift[4] = {
50 R300_TX_FORMAT_R_SHIFT,
51 R300_TX_FORMAT_G_SHIFT,
52 R300_TX_FORMAT_B_SHIFT,
53 R300_TX_FORMAT_A_SHIFT
55 uint32_t swizzle_bit[4] = {
56 dxtc_swizzle ? R300_TX_FORMAT_Z : R300_TX_FORMAT_X,
58 dxtc_swizzle ? R300_TX_FORMAT_X : R300_TX_FORMAT_Z,
63 /* Combine two sets of swizzles. */
64 for (i = 0; i < 4; i++) {
65 swizzle[i] = swizzle_view[i] <= UTIL_FORMAT_SWIZZLE_W ?
66 swizzle_format[swizzle_view[i]] : swizzle_view[i];
69 memcpy(swizzle, swizzle_format, 4);
73 for (i = 0; i < 4; i++) {
75 case UTIL_FORMAT_SWIZZLE_Y:
76 result |= swizzle_bit[1] << swizzle_shift[i];
78 case UTIL_FORMAT_SWIZZLE_Z:
79 result |= swizzle_bit[2] << swizzle_shift[i];
81 case UTIL_FORMAT_SWIZZLE_W:
82 result |= swizzle_bit[3] << swizzle_shift[i];
84 case UTIL_FORMAT_SWIZZLE_0:
85 result |= R300_TX_FORMAT_ZERO << swizzle_shift[i];
87 case UTIL_FORMAT_SWIZZLE_1:
88 result |= R300_TX_FORMAT_ONE << swizzle_shift[i];
90 default: /* UTIL_FORMAT_SWIZZLE_X */
91 result |= swizzle_bit[0] << swizzle_shift[i];
97 /* Translate a pipe_format into a useful texture format for sampling.
99 * Some special formats are translated directly using R300_EASY_TX_FORMAT,
100 * but the majority of them is translated in a generic way, automatically
101 * supporting all the formats hw can support.
103 * R300_EASY_TX_FORMAT swizzles the texture.
104 * Note the signature of R300_EASY_TX_FORMAT:
105 * R300_EASY_TX_FORMAT(B, G, R, A, FORMAT);
107 * The FORMAT specifies how the texture sampler will treat the texture, and
108 * makes available X, Y, Z, W, ZERO, and ONE for swizzling. */
109 uint32_t r300_translate_texformat(enum pipe_format format,
110 const unsigned char *swizzle_view,
112 boolean dxtc_swizzle)
115 const struct util_format_description *desc;
117 boolean uniform = TRUE;
118 const uint32_t sign_bit[4] = {
119 R300_TX_FORMAT_SIGNED_X,
120 R300_TX_FORMAT_SIGNED_Y,
121 R300_TX_FORMAT_SIGNED_Z,
122 R300_TX_FORMAT_SIGNED_W,
125 desc = util_format_description(format);
127 /* Colorspace (return non-RGB formats directly). */
128 switch (desc->colorspace) {
129 /* Depth stencil formats.
130 * Swizzles are added in r300_merge_textures_and_samplers. */
131 case UTIL_FORMAT_COLORSPACE_ZS:
133 case PIPE_FORMAT_Z16_UNORM:
134 return R300_TX_FORMAT_X16;
135 case PIPE_FORMAT_X8Z24_UNORM:
136 case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
138 return R500_TX_FORMAT_Y8X24;
140 return R300_TX_FORMAT_Y16X16;
142 return ~0; /* Unsupported. */
146 case UTIL_FORMAT_COLORSPACE_YUV:
147 result |= R300_TX_FORMAT_YUV_TO_RGB;
150 case PIPE_FORMAT_UYVY:
151 return R300_EASY_TX_FORMAT(X, Y, Z, ONE, YVYU422) | result;
152 case PIPE_FORMAT_YUYV:
153 return R300_EASY_TX_FORMAT(X, Y, Z, ONE, VYUY422) | result;
155 return ~0; /* Unsupported/unknown. */
158 /* Add gamma correction. */
159 case UTIL_FORMAT_COLORSPACE_SRGB:
160 result |= R300_TX_FORMAT_GAMMA;
165 /* Same as YUV but without the YUR->RGB conversion. */
166 case PIPE_FORMAT_R8G8_B8G8_UNORM:
167 return R300_EASY_TX_FORMAT(X, Y, Z, ONE, YVYU422) | result;
168 case PIPE_FORMAT_G8R8_G8B8_UNORM:
169 return R300_EASY_TX_FORMAT(X, Y, Z, ONE, VYUY422) | result;
174 result |= r300_get_swizzle_combined(desc->swizzle, swizzle_view,
175 util_format_is_compressed(format) && dxtc_swizzle);
178 if (desc->layout == UTIL_FORMAT_LAYOUT_S3TC) {
179 if (!util_format_s3tc_enabled) {
180 return ~0; /* Unsupported. */
184 case PIPE_FORMAT_DXT1_RGB:
185 case PIPE_FORMAT_DXT1_RGBA:
186 case PIPE_FORMAT_DXT1_SRGB:
187 case PIPE_FORMAT_DXT1_SRGBA:
188 return R300_TX_FORMAT_DXT1 | result;
189 case PIPE_FORMAT_DXT3_RGBA:
190 case PIPE_FORMAT_DXT3_SRGBA:
191 return R300_TX_FORMAT_DXT3 | result;
192 case PIPE_FORMAT_DXT5_RGBA:
193 case PIPE_FORMAT_DXT5_SRGBA:
194 return R300_TX_FORMAT_DXT5 | result;
196 return ~0; /* Unsupported/unknown. */
201 for (i = 0; i < desc->nr_channels; i++) {
202 if (desc->channel[i].type == UTIL_FORMAT_TYPE_SIGNED) {
203 result |= sign_bit[i];
207 /* This is truly a special format.
208 * It stores R8G8 and B is computed using sqrt(1 - R^2 - G^2)
209 * in the sampler unit. Also known as D3DFMT_CxV8U8. */
210 if (format == PIPE_FORMAT_R8G8Bx_SNORM) {
211 return R300_TX_FORMAT_CxV8U8 | result;
215 if (desc->layout == UTIL_FORMAT_LAYOUT_RGTC) {
217 case PIPE_FORMAT_RGTC1_UNORM:
218 case PIPE_FORMAT_RGTC1_SNORM:
219 return R500_TX_FORMAT_ATI1N | result;
220 case PIPE_FORMAT_RGTC2_UNORM:
221 case PIPE_FORMAT_RGTC2_SNORM:
222 return R400_TX_FORMAT_ATI2N | result;
224 return ~0; /* Unsupported/unknown. */
228 /* See whether the components are of the same size. */
229 for (i = 1; i < desc->nr_channels; i++) {
230 uniform = uniform && desc->channel[0].size == desc->channel[i].size;
233 /* Non-uniform formats. */
235 switch (desc->nr_channels) {
237 if (desc->channel[0].size == 5 &&
238 desc->channel[1].size == 6 &&
239 desc->channel[2].size == 5) {
240 return R300_TX_FORMAT_Z5Y6X5 | result;
242 if (desc->channel[0].size == 5 &&
243 desc->channel[1].size == 5 &&
244 desc->channel[2].size == 6) {
245 return R300_TX_FORMAT_Z6Y5X5 | result;
247 if (desc->channel[0].size == 2 &&
248 desc->channel[1].size == 3 &&
249 desc->channel[2].size == 3) {
250 return R300_TX_FORMAT_Z3Y3X2 | result;
252 return ~0; /* Unsupported/unknown. */
255 if (desc->channel[0].size == 5 &&
256 desc->channel[1].size == 5 &&
257 desc->channel[2].size == 5 &&
258 desc->channel[3].size == 1) {
259 return R300_TX_FORMAT_W1Z5Y5X5 | result;
261 if (desc->channel[0].size == 10 &&
262 desc->channel[1].size == 10 &&
263 desc->channel[2].size == 10 &&
264 desc->channel[3].size == 2) {
265 return R300_TX_FORMAT_W2Z10Y10X10 | result;
268 return ~0; /* Unsupported/unknown. */
271 /* Find the first non-VOID channel. */
272 for (i = 0; i < 4; i++) {
273 if (desc->channel[i].type != UTIL_FORMAT_TYPE_VOID) {
279 return ~0; /* Unsupported/unknown. */
281 /* And finally, uniform formats. */
282 switch (desc->channel[i].type) {
283 case UTIL_FORMAT_TYPE_UNSIGNED:
284 case UTIL_FORMAT_TYPE_SIGNED:
285 if (!desc->channel[i].normalized &&
286 desc->colorspace != UTIL_FORMAT_COLORSPACE_SRGB) {
290 switch (desc->channel[i].size) {
292 switch (desc->nr_channels) {
294 return R300_TX_FORMAT_Y4X4 | result;
296 return R300_TX_FORMAT_W4Z4Y4X4 | result;
301 switch (desc->nr_channels) {
303 return R300_TX_FORMAT_X8 | result;
305 return R300_TX_FORMAT_Y8X8 | result;
307 return R300_TX_FORMAT_W8Z8Y8X8 | result;
312 switch (desc->nr_channels) {
314 return R300_TX_FORMAT_X16 | result;
316 return R300_TX_FORMAT_Y16X16 | result;
318 return R300_TX_FORMAT_W16Z16Y16X16 | result;
323 case UTIL_FORMAT_TYPE_FLOAT:
324 switch (desc->channel[i].size) {
326 switch (desc->nr_channels) {
328 return R300_TX_FORMAT_16F | result;
330 return R300_TX_FORMAT_16F_16F | result;
332 return R300_TX_FORMAT_16F_16F_16F_16F | result;
337 switch (desc->nr_channels) {
339 return R300_TX_FORMAT_32F | result;
341 return R300_TX_FORMAT_32F_32F | result;
343 return R300_TX_FORMAT_32F_32F_32F_32F | result;
348 return ~0; /* Unsupported/unknown. */
351 uint32_t r500_tx_format_msb_bit(enum pipe_format format)
354 case PIPE_FORMAT_RGTC1_UNORM:
355 case PIPE_FORMAT_RGTC1_SNORM:
356 case PIPE_FORMAT_X8Z24_UNORM:
357 case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
358 return R500_TXFORMAT_MSB;
364 /* Buffer formats. */
366 /* Colorbuffer formats. This is the unswizzled format of the RB3D block's
367 * output. For the swizzling of the targets, check the shader's format. */
368 static uint32_t r300_translate_colorformat(enum pipe_format format)
372 case PIPE_FORMAT_A8_UNORM:
373 case PIPE_FORMAT_I8_UNORM:
374 case PIPE_FORMAT_L8_UNORM:
375 case PIPE_FORMAT_R8_UNORM:
376 case PIPE_FORMAT_R8_SNORM:
377 return R300_COLOR_FORMAT_I8;
379 /* 16-bit buffers. */
380 case PIPE_FORMAT_L8A8_UNORM:
381 case PIPE_FORMAT_R8G8_UNORM:
382 case PIPE_FORMAT_R8G8_SNORM:
383 return R300_COLOR_FORMAT_UV88;
385 case PIPE_FORMAT_B5G6R5_UNORM:
386 return R300_COLOR_FORMAT_RGB565;
388 case PIPE_FORMAT_B5G5R5A1_UNORM:
389 case PIPE_FORMAT_B5G5R5X1_UNORM:
390 return R300_COLOR_FORMAT_ARGB1555;
392 case PIPE_FORMAT_B4G4R4A4_UNORM:
393 case PIPE_FORMAT_B4G4R4X4_UNORM:
394 return R300_COLOR_FORMAT_ARGB4444;
396 /* 32-bit buffers. */
397 case PIPE_FORMAT_B8G8R8A8_UNORM:
398 case PIPE_FORMAT_B8G8R8X8_UNORM:
399 case PIPE_FORMAT_A8R8G8B8_UNORM:
400 case PIPE_FORMAT_X8R8G8B8_UNORM:
401 case PIPE_FORMAT_A8B8G8R8_UNORM:
402 case PIPE_FORMAT_R8G8B8A8_SNORM:
403 case PIPE_FORMAT_X8B8G8R8_UNORM:
404 case PIPE_FORMAT_R8G8B8X8_UNORM:
405 case PIPE_FORMAT_R8SG8SB8UX8U_NORM:
406 return R300_COLOR_FORMAT_ARGB8888;
408 case PIPE_FORMAT_R10G10B10A2_UNORM:
409 case PIPE_FORMAT_R10G10B10X2_SNORM:
410 case PIPE_FORMAT_B10G10R10A2_UNORM:
411 case PIPE_FORMAT_R10SG10SB10SA2U_NORM:
412 return R500_COLOR_FORMAT_ARGB2101010; /* R5xx-only? */
414 /* 64-bit buffers. */
415 case PIPE_FORMAT_R16G16B16A16_UNORM:
416 case PIPE_FORMAT_R16G16B16A16_SNORM:
417 case PIPE_FORMAT_R16G16B16A16_FLOAT:
418 return R300_COLOR_FORMAT_ARGB16161616;
420 /* 128-bit buffers. */
421 case PIPE_FORMAT_R32G32B32A32_FLOAT:
422 return R300_COLOR_FORMAT_ARGB32323232;
425 case PIPE_FORMAT_UYVY:
426 return R300_COLOR_FORMAT_YVYU;
427 case PIPE_FORMAT_YUYV:
428 return R300_COLOR_FORMAT_VYUY;
430 return ~0; /* Unsupported. */
434 /* Depthbuffer and stencilbuffer. Thankfully, we only support two flavors. */
435 static uint32_t r300_translate_zsformat(enum pipe_format format)
438 /* 16-bit depth, no stencil */
439 case PIPE_FORMAT_Z16_UNORM:
440 return R300_DEPTHFORMAT_16BIT_INT_Z;
441 /* 24-bit depth, ignored stencil */
442 case PIPE_FORMAT_X8Z24_UNORM:
443 /* 24-bit depth, 8-bit stencil */
444 case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
445 return R300_DEPTHFORMAT_24BIT_INT_Z_8BIT_STENCIL;
447 return ~0; /* Unsupported. */
451 /* Shader output formats. This is essentially the swizzle from the shader
454 * Note that formats are stored from C3 to C0. */
455 static uint32_t r300_translate_out_fmt(enum pipe_format format)
457 uint32_t modifier = 0;
459 const struct util_format_description *desc;
460 static const uint32_t sign_bit[4] = {
467 desc = util_format_description(format);
469 /* Find the first non-VOID channel. */
470 for (i = 0; i < 4; i++) {
471 if (desc->channel[i].type != UTIL_FORMAT_TYPE_VOID) {
477 return ~0; /* Unsupported/unknown. */
479 /* Specifies how the shader output is written to the fog unit. */
480 if (desc->channel[i].type == UTIL_FORMAT_TYPE_FLOAT) {
481 if (desc->channel[i].size == 32) {
482 modifier |= R300_US_OUT_FMT_C4_32_FP;
484 modifier |= R300_US_OUT_FMT_C4_16_FP;
487 if (desc->channel[i].size == 16) {
488 modifier |= R300_US_OUT_FMT_C4_16;
489 } else if (desc->channel[i].size == 10) {
490 modifier |= R300_US_OUT_FMT_C4_10;
492 /* C4_8 seems to be used for the formats whose pixel size
494 modifier |= R300_US_OUT_FMT_C4_8;
499 for (i = 0; i < 4; i++)
500 if (desc->channel[i].type == UTIL_FORMAT_TYPE_SIGNED) {
501 modifier |= sign_bit[i];
504 /* Add swizzles and return. */
506 /* 8-bit outputs, one channel.
507 * COLORFORMAT_I8 stores the C2 component. */
508 case PIPE_FORMAT_A8_UNORM:
509 return modifier | R300_C2_SEL_A;
510 case PIPE_FORMAT_I8_UNORM:
511 case PIPE_FORMAT_L8_UNORM:
512 case PIPE_FORMAT_R8_UNORM:
513 case PIPE_FORMAT_R8_SNORM:
514 return modifier | R300_C2_SEL_R;
516 /* 16-bit outputs, two channels.
517 * COLORFORMAT_UV88 stores C2 and C0. */
518 case PIPE_FORMAT_L8A8_UNORM:
519 return modifier | R300_C0_SEL_A | R300_C2_SEL_R;
520 case PIPE_FORMAT_R8G8_UNORM:
521 case PIPE_FORMAT_R8G8_SNORM:
522 return modifier | R300_C0_SEL_G | R300_C2_SEL_R;
525 case PIPE_FORMAT_B5G6R5_UNORM:
526 case PIPE_FORMAT_B5G5R5A1_UNORM:
527 case PIPE_FORMAT_B5G5R5X1_UNORM:
528 case PIPE_FORMAT_B4G4R4A4_UNORM:
529 case PIPE_FORMAT_B4G4R4X4_UNORM:
530 case PIPE_FORMAT_B8G8R8A8_UNORM:
531 case PIPE_FORMAT_B8G8R8X8_UNORM:
532 case PIPE_FORMAT_B10G10R10A2_UNORM:
534 R300_C0_SEL_B | R300_C1_SEL_G |
535 R300_C2_SEL_R | R300_C3_SEL_A;
538 case PIPE_FORMAT_A8R8G8B8_UNORM:
539 case PIPE_FORMAT_X8R8G8B8_UNORM:
541 R300_C0_SEL_A | R300_C1_SEL_R |
542 R300_C2_SEL_G | R300_C3_SEL_B;
545 case PIPE_FORMAT_A8B8G8R8_UNORM:
546 case PIPE_FORMAT_X8B8G8R8_UNORM:
548 R300_C0_SEL_A | R300_C1_SEL_B |
549 R300_C2_SEL_G | R300_C3_SEL_R;
552 case PIPE_FORMAT_R8G8B8X8_UNORM:
553 case PIPE_FORMAT_R8G8B8A8_SNORM:
554 case PIPE_FORMAT_R8SG8SB8UX8U_NORM:
555 case PIPE_FORMAT_R10G10B10A2_UNORM:
556 case PIPE_FORMAT_R10G10B10X2_SNORM:
557 case PIPE_FORMAT_R10SG10SB10SA2U_NORM:
558 case PIPE_FORMAT_R16G16B16A16_UNORM:
559 case PIPE_FORMAT_R16G16B16A16_SNORM:
560 case PIPE_FORMAT_R16G16B16A16_FLOAT:
561 case PIPE_FORMAT_R32G32B32A32_FLOAT:
563 R300_C0_SEL_R | R300_C1_SEL_G |
564 R300_C2_SEL_B | R300_C3_SEL_A;
567 return ~0; /* Unsupported. */
571 boolean r300_is_colorbuffer_format_supported(enum pipe_format format)
573 return r300_translate_colorformat(format) != ~0 &&
574 r300_translate_out_fmt(format) != ~0;
577 boolean r300_is_zs_format_supported(enum pipe_format format)
579 return r300_translate_zsformat(format) != ~0;
582 boolean r300_is_sampler_format_supported(enum pipe_format format)
584 return r300_translate_texformat(format, 0, TRUE, FALSE) != ~0;
587 void r300_texture_setup_format_state(struct r300_screen *screen,
588 struct r300_texture_desc *desc,
590 struct r300_texture_format_state *out)
592 struct pipe_resource *pt = &desc->b.b;
593 boolean is_r500 = screen->caps.is_r500;
595 /* Mask out all the fields we change. */
597 out->format1 &= ~R300_TX_FORMAT_TEX_COORD_TYPE_MASK;
598 out->format2 &= R500_TXFORMAT_MSB;
599 out->tile_config = 0;
601 /* Set sampler state. */
603 R300_TX_WIDTH((u_minify(desc->width0, level) - 1) & 0x7ff) |
604 R300_TX_HEIGHT((u_minify(desc->height0, level) - 1) & 0x7ff) |
605 R300_TX_DEPTH(util_logbase2(u_minify(desc->depth0, level)) & 0xf);
607 if (desc->uses_stride_addressing) {
608 /* rectangles love this */
609 out->format0 |= R300_TX_PITCH_EN;
610 out->format2 = (desc->stride_in_pixels[level] - 1) & 0x1fff;
613 if (pt->target == PIPE_TEXTURE_CUBE) {
614 out->format1 |= R300_TX_FORMAT_CUBIC_MAP;
616 if (pt->target == PIPE_TEXTURE_3D) {
617 out->format1 |= R300_TX_FORMAT_3D;
620 /* large textures on r500 */
623 if (desc->width0 > 2048) {
624 out->format2 |= R500_TXWIDTH_BIT11;
626 if (desc->height0 > 2048) {
627 out->format2 |= R500_TXHEIGHT_BIT11;
631 out->tile_config = R300_TXO_MACRO_TILE(desc->macrotile[level]) |
632 R300_TXO_MICRO_TILE(desc->microtile);
635 static void r300_texture_setup_fb_state(struct r300_screen* screen,
636 struct r300_texture* tex)
640 /* Set framebuffer state. */
641 if (util_format_is_depth_or_stencil(tex->desc.b.b.format)) {
642 for (i = 0; i <= tex->desc.b.b.last_level; i++) {
643 tex->fb_state.pitch[i] =
644 tex->desc.stride_in_pixels[i] |
645 R300_DEPTHMACROTILE(tex->desc.macrotile[i]) |
646 R300_DEPTHMICROTILE(tex->desc.microtile);
648 tex->fb_state.format = r300_translate_zsformat(tex->desc.b.b.format);
650 for (i = 0; i <= tex->desc.b.b.last_level; i++) {
651 tex->fb_state.pitch[i] =
652 tex->desc.stride_in_pixels[i] |
653 r300_translate_colorformat(tex->desc.b.b.format) |
654 R300_COLOR_TILE(tex->desc.macrotile[i]) |
655 R300_COLOR_MICROTILE(tex->desc.microtile);
657 tex->fb_state.format = r300_translate_out_fmt(tex->desc.b.b.format);
661 void r300_texture_reinterpret_format(struct pipe_screen *screen,
662 struct pipe_resource *tex,
663 enum pipe_format new_format)
665 struct r300_screen *r300screen = r300_screen(screen);
667 SCREEN_DBG(r300screen, DBG_TEX,
668 "r300: texture_reinterpret_format: %s -> %s\n",
669 util_format_short_name(tex->format),
670 util_format_short_name(new_format));
672 tex->format = new_format;
674 r300_texture_setup_fb_state(r300_screen(screen), r300_texture(tex));
677 static unsigned r300_texture_is_referenced(struct pipe_context *context,
678 struct pipe_resource *texture,
679 unsigned level, int layer)
681 struct r300_context *r300 = r300_context(context);
682 struct r300_texture *rtex = (struct r300_texture *)texture;
684 if (r300->rws->cs_is_buffer_referenced(r300->cs,
685 rtex->cs_buffer, R300_REF_CS))
686 return PIPE_REFERENCED_FOR_READ | PIPE_REFERENCED_FOR_WRITE;
688 return PIPE_UNREFERENCED;
691 static void r300_texture_destroy(struct pipe_screen *screen,
692 struct pipe_resource* texture)
694 struct r300_texture* tex = (struct r300_texture*)texture;
695 struct r300_winsys_screen *rws = (struct r300_winsys_screen *)texture->screen->winsys;
698 rws->buffer_reference(rws, &tex->buffer, NULL);
699 for (i = 0; i < R300_MAX_TEXTURE_LEVELS; i++) {
701 u_mmFreeMem(tex->hiz_mem[i]);
702 if (tex->zmask_mem[i])
703 u_mmFreeMem(tex->zmask_mem[i]);
709 static boolean r300_texture_get_handle(struct pipe_screen* screen,
710 struct pipe_resource *texture,
711 struct winsys_handle *whandle)
713 struct r300_winsys_screen *rws = (struct r300_winsys_screen *)screen->winsys;
714 struct r300_texture* tex = (struct r300_texture*)texture;
720 return rws->buffer_get_handle(rws, tex->buffer,
721 tex->desc.stride_in_bytes[0], whandle);
724 struct u_resource_vtbl r300_texture_vtbl =
726 r300_texture_get_handle, /* get_handle */
727 r300_texture_destroy, /* resource_destroy */
728 r300_texture_is_referenced, /* is_resource_referenced */
729 r300_texture_get_transfer, /* get_transfer */
730 r300_texture_transfer_destroy, /* transfer_destroy */
731 r300_texture_transfer_map, /* transfer_map */
732 u_default_transfer_flush_region, /* transfer_flush_region */
733 r300_texture_transfer_unmap, /* transfer_unmap */
734 u_default_transfer_inline_write /* transfer_inline_write */
737 /* The common texture constructor. */
738 static struct r300_texture*
739 r300_texture_create_object(struct r300_screen *rscreen,
740 const struct pipe_resource *base,
741 enum r300_buffer_tiling microtile,
742 enum r300_buffer_tiling macrotile,
743 unsigned stride_in_bytes_override,
744 unsigned max_buffer_size,
745 struct r300_winsys_buffer *buffer)
747 struct r300_winsys_screen *rws = rscreen->rws;
748 struct r300_texture *tex = CALLOC_STRUCT(r300_texture);
751 rws->buffer_reference(rws, &buffer, NULL);
755 /* Initialize the descriptor. */
756 if (!r300_texture_desc_init(rscreen, &tex->desc, base,
757 microtile, macrotile,
758 stride_in_bytes_override,
761 rws->buffer_reference(rws, &buffer, NULL);
765 /* Initialize the hardware state. */
766 r300_texture_setup_format_state(rscreen, &tex->desc, 0, &tex->tx_format);
767 r300_texture_setup_fb_state(rscreen, tex);
769 tex->desc.b.vtbl = &r300_texture_vtbl;
770 pipe_reference_init(&tex->desc.b.b.reference, 1);
771 tex->domain = base->flags & R300_RESOURCE_FLAG_TRANSFER ?
773 R300_DOMAIN_VRAM | R300_DOMAIN_GTT;
774 tex->buffer = buffer;
776 /* Create the backing buffer if needed. */
778 tex->buffer = rws->buffer_create(rws, tex->desc.size_in_bytes, 2048,
779 base->bind, base->usage, tex->domain);
787 tex->cs_buffer = rws->buffer_get_cs_handle(rws, tex->buffer);
789 rws->buffer_set_tiling(rws, tex->buffer,
790 tex->desc.microtile, tex->desc.macrotile[0],
791 tex->desc.stride_in_bytes[0]);
796 /* Create a new texture. */
797 struct pipe_resource *r300_texture_create(struct pipe_screen *screen,
798 const struct pipe_resource *base)
800 struct r300_screen *rscreen = r300_screen(screen);
801 enum r300_buffer_tiling microtile, macrotile;
803 if ((base->flags & R300_RESOURCE_FLAG_TRANSFER) ||
804 (base->bind & PIPE_BIND_SCANOUT)) {
805 microtile = R300_BUFFER_LINEAR;
806 macrotile = R300_BUFFER_LINEAR;
808 microtile = R300_BUFFER_SELECT_LAYOUT;
809 macrotile = R300_BUFFER_SELECT_LAYOUT;
812 return (struct pipe_resource*)
813 r300_texture_create_object(rscreen, base, microtile, macrotile,
817 struct pipe_resource *r300_texture_from_handle(struct pipe_screen *screen,
818 const struct pipe_resource *base,
819 struct winsys_handle *whandle)
821 struct r300_winsys_screen *rws = (struct r300_winsys_screen*)screen->winsys;
822 struct r300_screen *rscreen = r300_screen(screen);
823 struct r300_winsys_buffer *buffer;
824 enum r300_buffer_tiling microtile, macrotile;
825 unsigned stride, size;
827 /* Support only 2D textures without mipmaps */
828 if ((base->target != PIPE_TEXTURE_2D &&
829 base->target != PIPE_TEXTURE_RECT) ||
831 base->last_level != 0) {
835 buffer = rws->buffer_from_handle(rws, whandle, &stride, &size);
839 rws->buffer_get_tiling(rws, buffer, µtile, ¯otile);
841 /* Enforce a microtiled zbuffer. */
842 if (util_format_is_depth_or_stencil(base->format) &&
843 microtile == R300_BUFFER_LINEAR) {
844 switch (util_format_get_blocksize(base->format)) {
846 microtile = R300_BUFFER_TILED;
850 if (rws->get_value(rws, R300_VID_SQUARE_TILING_SUPPORT))
851 microtile = R300_BUFFER_SQUARETILED;
856 return (struct pipe_resource*)
857 r300_texture_create_object(rscreen, base, microtile, macrotile,
858 stride, size, buffer);
861 /* Not required to implement u_resource_vtbl, consider moving to another file:
863 struct pipe_surface* r300_create_surface(struct pipe_context * ctx,
864 struct pipe_resource* texture,
865 const struct pipe_surface *surf_tmpl)
867 struct r300_texture* tex = r300_texture(texture);
868 struct r300_surface* surface = CALLOC_STRUCT(r300_surface);
869 unsigned level = surf_tmpl->u.tex.level;
871 assert(surf_tmpl->u.tex.first_layer == surf_tmpl->u.tex.last_layer);
874 uint32_t offset, tile_height;
876 pipe_reference_init(&surface->base.reference, 1);
877 pipe_resource_reference(&surface->base.texture, texture);
878 surface->base.context = ctx;
879 surface->base.format = surf_tmpl->format;
880 surface->base.width = u_minify(texture->width0, level);
881 surface->base.height = u_minify(texture->height0, level);
882 surface->base.usage = surf_tmpl->usage;
883 surface->base.u.tex.level = level;
884 surface->base.u.tex.first_layer = surf_tmpl->u.tex.first_layer;
885 surface->base.u.tex.last_layer = surf_tmpl->u.tex.last_layer;
887 surface->buffer = tex->buffer;
888 surface->cs_buffer = tex->cs_buffer;
890 /* Prefer VRAM if there are multiple domains to choose from. */
891 surface->domain = tex->domain;
892 if (surface->domain & R300_DOMAIN_VRAM)
893 surface->domain &= ~R300_DOMAIN_GTT;
895 surface->offset = r300_texture_get_offset(&tex->desc, level,
896 surf_tmpl->u.tex.first_layer);
897 surface->pitch = tex->fb_state.pitch[level];
898 surface->format = tex->fb_state.format;
900 /* Parameters for the CBZB clear. */
901 surface->cbzb_allowed = tex->desc.cbzb_allowed[level];
902 surface->cbzb_width = align(surface->base.width, 64);
904 /* Height must be aligned to the size of a tile. */
905 tile_height = r300_get_pixel_alignment(tex->desc.b.b.format,
906 tex->desc.b.b.nr_samples,
908 tex->desc.macrotile[level],
911 surface->cbzb_height = align((surface->base.height + 1) / 2,
914 /* Offset must be aligned to 2K and must point at the beginning
916 offset = surface->offset +
917 tex->desc.stride_in_bytes[level] * surface->cbzb_height;
918 surface->cbzb_midpoint_offset = offset & ~2047;
920 surface->cbzb_pitch = surface->pitch & 0x1ffffc;
922 if (util_format_get_blocksizebits(surface->base.format) == 32)
923 surface->cbzb_format = R300_DEPTHFORMAT_24BIT_INT_Z_8BIT_STENCIL;
925 surface->cbzb_format = R300_DEPTHFORMAT_16BIT_INT_Z;
927 DBG(r300_context(ctx), DBG_CBZB,
928 "CBZB Allowed: %s, Dim: %ix%i, Misalignment: %i, Micro: %s, Macro: %s\n",
929 surface->cbzb_allowed ? "YES" : " NO",
930 surface->cbzb_width, surface->cbzb_height,
932 tex->desc.microtile ? "YES" : " NO",
933 tex->desc.macrotile[level] ? "YES" : " NO");
936 return &surface->base;
939 /* Not required to implement u_resource_vtbl, consider moving to another file:
941 void r300_surface_destroy(struct pipe_context *ctx, struct pipe_surface* s)
943 pipe_resource_reference(&s->texture, NULL);