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_transfer.h"
33 #include "r300_screen.h"
34 #include "r300_winsys.h"
36 /* XXX Enable float textures here. */
37 /*#define ENABLE_FLOAT_TEXTURES*/
42 static const unsigned microblock_table[5][3][2] = {
43 /*linear tiled square-tiled */
44 {{32, 1}, {8, 4}, {0, 0}}, /* 8 bits per pixel */
45 {{16, 1}, {8, 2}, {4, 4}}, /* 16 bits per pixel */
46 {{ 8, 1}, {4, 2}, {0, 0}}, /* 32 bits per pixel */
47 {{ 4, 1}, {0, 0}, {2, 2}}, /* 64 bits per pixel */
48 {{ 2, 1}, {0, 0}, {0, 0}} /* 128 bits per pixel */
51 /* Return true for non-compressed and non-YUV formats. */
52 static boolean r300_format_is_plain(enum pipe_format format)
54 const struct util_format_description *desc = util_format_description(format);
60 return desc->layout == UTIL_FORMAT_LAYOUT_PLAIN;
63 /* Translate a pipe_format into a useful texture format for sampling.
65 * Some special formats are translated directly using R300_EASY_TX_FORMAT,
66 * but the majority of them is translated in a generic way, automatically
67 * supporting all the formats hw can support.
69 * R300_EASY_TX_FORMAT swizzles the texture.
70 * Note the signature of R300_EASY_TX_FORMAT:
71 * R300_EASY_TX_FORMAT(B, G, R, A, FORMAT);
73 * The FORMAT specifies how the texture sampler will treat the texture, and
74 * makes available X, Y, Z, W, ZERO, and ONE for swizzling. */
75 static uint32_t r300_translate_texformat(enum pipe_format format)
78 const struct util_format_description *desc;
80 boolean uniform = TRUE;
81 const uint32_t swizzle_shift[4] = {
82 R300_TX_FORMAT_R_SHIFT,
83 R300_TX_FORMAT_G_SHIFT,
84 R300_TX_FORMAT_B_SHIFT,
85 R300_TX_FORMAT_A_SHIFT
87 const uint32_t swizzle[4] = {
93 const uint32_t sign_bit[4] = {
94 R300_TX_FORMAT_SIGNED_X,
95 R300_TX_FORMAT_SIGNED_Y,
96 R300_TX_FORMAT_SIGNED_Z,
97 R300_TX_FORMAT_SIGNED_W,
100 desc = util_format_description(format);
102 /* Colorspace (return non-RGB formats directly). */
103 switch (desc->colorspace) {
104 /* Depth stencil formats. */
105 case UTIL_FORMAT_COLORSPACE_ZS:
107 case PIPE_FORMAT_Z16_UNORM:
108 return R300_EASY_TX_FORMAT(X, X, X, X, X16);
109 case PIPE_FORMAT_X8Z24_UNORM:
110 case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
111 return R300_EASY_TX_FORMAT(X, X, X, X, W24_FP);
113 return ~0; /* Unsupported. */
117 case UTIL_FORMAT_COLORSPACE_YUV:
118 result |= R300_TX_FORMAT_YUV_TO_RGB;
121 case PIPE_FORMAT_UYVY:
122 return R300_EASY_TX_FORMAT(X, Y, Z, ONE, YVYU422) | result;
123 case PIPE_FORMAT_YUYV:
124 return R300_EASY_TX_FORMAT(X, Y, Z, ONE, VYUY422) | result;
126 return ~0; /* Unsupported/unknown. */
129 /* Add gamma correction. */
130 case UTIL_FORMAT_COLORSPACE_SRGB:
131 result |= R300_TX_FORMAT_GAMMA;
138 for (i = 0; i < 4; i++) {
139 switch (desc->swizzle[i]) {
140 case UTIL_FORMAT_SWIZZLE_X:
141 case UTIL_FORMAT_SWIZZLE_NONE:
142 result |= swizzle[0] << swizzle_shift[i];
144 case UTIL_FORMAT_SWIZZLE_Y:
145 result |= swizzle[1] << swizzle_shift[i];
147 case UTIL_FORMAT_SWIZZLE_Z:
148 result |= swizzle[2] << swizzle_shift[i];
150 case UTIL_FORMAT_SWIZZLE_W:
151 result |= swizzle[3] << swizzle_shift[i];
153 case UTIL_FORMAT_SWIZZLE_0:
154 result |= R300_TX_FORMAT_ZERO << swizzle_shift[i];
156 case UTIL_FORMAT_SWIZZLE_1:
157 result |= R300_TX_FORMAT_ONE << swizzle_shift[i];
160 return ~0; /* Unsupported. */
165 if (desc->layout == UTIL_FORMAT_LAYOUT_S3TC) {
167 case PIPE_FORMAT_DXT1_RGB:
168 case PIPE_FORMAT_DXT1_RGBA:
169 case PIPE_FORMAT_DXT1_SRGB:
170 case PIPE_FORMAT_DXT1_SRGBA:
171 return R300_TX_FORMAT_DXT1 | result;
172 case PIPE_FORMAT_DXT3_RGBA:
173 case PIPE_FORMAT_DXT3_SRGBA:
174 return R300_TX_FORMAT_DXT3 | result;
175 case PIPE_FORMAT_DXT5_RGBA:
176 case PIPE_FORMAT_DXT5_SRGBA:
177 return R300_TX_FORMAT_DXT5 | result;
179 return ~0; /* Unsupported/unknown. */
184 for (i = 0; i < desc->nr_channels; i++) {
185 if (desc->channel[i].type == UTIL_FORMAT_TYPE_SIGNED) {
186 result |= sign_bit[i];
191 if (desc->layout == UTIL_FORMAT_LAYOUT_RGTC) {
193 case PIPE_FORMAT_RGTC1_UNORM:
194 case PIPE_FORMAT_RGTC1_SNORM:
195 return R500_TX_FORMAT_ATI1N | result;
196 case PIPE_FORMAT_RGTC2_UNORM:
197 case PIPE_FORMAT_RGTC2_SNORM:
198 return R400_TX_FORMAT_ATI2N | result;
200 return ~0; /* Unsupported/unknown. */
204 /* See whether the components are of the same size. */
205 for (i = 1; i < desc->nr_channels; i++) {
206 uniform = uniform && desc->channel[0].size == desc->channel[i].size;
209 /* Non-uniform formats. */
211 switch (desc->nr_channels) {
213 if (desc->channel[0].size == 5 &&
214 desc->channel[1].size == 6 &&
215 desc->channel[2].size == 5) {
216 return R300_TX_FORMAT_Z5Y6X5 | result;
218 if (desc->channel[0].size == 5 &&
219 desc->channel[1].size == 5 &&
220 desc->channel[2].size == 6) {
221 return R300_TX_FORMAT_Z6Y5X5 | result;
223 return ~0; /* Unsupported/unknown. */
226 if (desc->channel[0].size == 5 &&
227 desc->channel[1].size == 5 &&
228 desc->channel[2].size == 5 &&
229 desc->channel[3].size == 1) {
230 return R300_TX_FORMAT_W1Z5Y5X5 | result;
232 if (desc->channel[0].size == 10 &&
233 desc->channel[1].size == 10 &&
234 desc->channel[2].size == 10 &&
235 desc->channel[3].size == 2) {
236 return R300_TX_FORMAT_W2Z10Y10X10 | result;
239 return ~0; /* Unsupported/unknown. */
242 /* And finally, uniform formats. */
243 switch (desc->channel[0].type) {
244 case UTIL_FORMAT_TYPE_UNSIGNED:
245 case UTIL_FORMAT_TYPE_SIGNED:
246 if (!desc->channel[0].normalized &&
247 desc->colorspace != UTIL_FORMAT_COLORSPACE_SRGB) {
251 switch (desc->channel[0].size) {
253 switch (desc->nr_channels) {
255 return R300_TX_FORMAT_Y4X4 | result;
257 return R300_TX_FORMAT_W4Z4Y4X4 | result;
262 switch (desc->nr_channels) {
264 return R300_TX_FORMAT_X8 | result;
266 return R300_TX_FORMAT_Y8X8 | result;
268 return R300_TX_FORMAT_W8Z8Y8X8 | result;
273 switch (desc->nr_channels) {
275 return R300_TX_FORMAT_X16 | result;
277 return R300_TX_FORMAT_Y16X16 | result;
279 return R300_TX_FORMAT_W16Z16Y16X16 | result;
284 #if defined(ENABLE_FLOAT_TEXTURES)
285 case UTIL_FORMAT_TYPE_FLOAT:
286 switch (desc->channel[0].size) {
288 switch (desc->nr_channels) {
290 return R300_TX_FORMAT_16F | result;
292 return R300_TX_FORMAT_16F_16F | result;
294 return R300_TX_FORMAT_16F_16F_16F_16F | result;
299 switch (desc->nr_channels) {
301 return R300_TX_FORMAT_32F | result;
303 return R300_TX_FORMAT_32F_32F | result;
305 return R300_TX_FORMAT_32F_32F_32F_32F | result;
311 return ~0; /* Unsupported/unknown. */
314 static uint32_t r500_tx_format_msb_bit(enum pipe_format format)
317 case PIPE_FORMAT_RGTC1_UNORM:
318 case PIPE_FORMAT_RGTC1_SNORM:
319 return R500_TXFORMAT_MSB;
325 /* Buffer formats. */
327 /* Colorbuffer formats. This is the unswizzled format of the RB3D block's
328 * output. For the swizzling of the targets, check the shader's format. */
329 static uint32_t r300_translate_colorformat(enum pipe_format format)
333 case PIPE_FORMAT_A8_UNORM:
334 case PIPE_FORMAT_I8_UNORM:
335 case PIPE_FORMAT_L8_UNORM:
336 case PIPE_FORMAT_R8_UNORM:
337 case PIPE_FORMAT_R8_SNORM:
338 return R300_COLOR_FORMAT_I8;
340 /* 16-bit buffers. */
341 case PIPE_FORMAT_B5G6R5_UNORM:
342 return R300_COLOR_FORMAT_RGB565;
343 case PIPE_FORMAT_B5G5R5A1_UNORM:
344 case PIPE_FORMAT_B5G5R5X1_UNORM:
345 return R300_COLOR_FORMAT_ARGB1555;
346 case PIPE_FORMAT_B4G4R4A4_UNORM:
347 return R300_COLOR_FORMAT_ARGB4444;
349 /* 32-bit buffers. */
350 case PIPE_FORMAT_B8G8R8A8_UNORM:
351 case PIPE_FORMAT_B8G8R8X8_UNORM:
352 case PIPE_FORMAT_A8R8G8B8_UNORM:
353 case PIPE_FORMAT_X8R8G8B8_UNORM:
354 case PIPE_FORMAT_A8B8G8R8_UNORM:
355 case PIPE_FORMAT_R8G8B8A8_SNORM:
356 case PIPE_FORMAT_X8B8G8R8_UNORM:
357 case PIPE_FORMAT_R8SG8SB8UX8U_NORM:
358 return R300_COLOR_FORMAT_ARGB8888;
359 case PIPE_FORMAT_R10G10B10A2_UNORM:
360 return R500_COLOR_FORMAT_ARGB2101010; /* R5xx-only? */
362 /* 64-bit buffers. */
363 case PIPE_FORMAT_R16G16B16A16_UNORM:
364 case PIPE_FORMAT_R16G16B16A16_SNORM:
365 #if defined(ENABLE_FLOAT_TEXTURES)
366 case PIPE_FORMAT_R16G16B16A16_FLOAT:
368 return R300_COLOR_FORMAT_ARGB16161616;
370 /* 128-bit buffers. */
371 #if defined(ENABLE_FLOAT_TEXTURES)
372 case PIPE_FORMAT_R32G32B32A32_FLOAT:
373 return R300_COLOR_FORMAT_ARGB32323232;
377 case PIPE_FORMAT_UYVY:
378 return R300_COLOR_FORMAT_YVYU;
379 case PIPE_FORMAT_YUYV:
380 return R300_COLOR_FORMAT_VYUY;
382 return ~0; /* Unsupported. */
386 /* Depthbuffer and stencilbuffer. Thankfully, we only support two flavors. */
387 static uint32_t r300_translate_zsformat(enum pipe_format format)
390 /* 16-bit depth, no stencil */
391 case PIPE_FORMAT_Z16_UNORM:
392 return R300_DEPTHFORMAT_16BIT_INT_Z;
393 /* 24-bit depth, ignored stencil */
394 case PIPE_FORMAT_X8Z24_UNORM:
395 /* 24-bit depth, 8-bit stencil */
396 case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
397 return R300_DEPTHFORMAT_24BIT_INT_Z_8BIT_STENCIL;
399 return ~0; /* Unsupported. */
403 /* Shader output formats. This is essentially the swizzle from the shader
406 * Note that formats are stored from C3 to C0. */
407 static uint32_t r300_translate_out_fmt(enum pipe_format format)
409 uint32_t modifier = 0;
411 const struct util_format_description *desc;
412 static const uint32_t sign_bit[4] = {
419 desc = util_format_description(format);
421 /* Specifies how the shader output is written to the fog unit. */
422 if (desc->channel[0].type == UTIL_FORMAT_TYPE_FLOAT) {
423 if (desc->channel[0].size == 32) {
424 modifier |= R300_US_OUT_FMT_C4_32_FP;
426 modifier |= R300_US_OUT_FMT_C4_16_FP;
429 if (desc->channel[0].size == 16) {
430 modifier |= R300_US_OUT_FMT_C4_16;
432 /* C4_8 seems to be used for the formats whose pixel size
434 modifier |= R300_US_OUT_FMT_C4_8;
439 for (i = 0; i < 4; i++)
440 if (desc->channel[i].type == UTIL_FORMAT_TYPE_SIGNED) {
441 modifier |= sign_bit[i];
444 /* Add swizzles and return. */
447 * COLORFORMAT_I8 stores the C2 component. */
448 case PIPE_FORMAT_A8_UNORM:
449 return modifier | R300_C2_SEL_A;
450 case PIPE_FORMAT_I8_UNORM:
451 case PIPE_FORMAT_L8_UNORM:
452 case PIPE_FORMAT_R8_UNORM:
453 case PIPE_FORMAT_R8_SNORM:
454 return modifier | R300_C2_SEL_R;
457 case PIPE_FORMAT_B5G6R5_UNORM:
458 case PIPE_FORMAT_B5G5R5A1_UNORM:
459 case PIPE_FORMAT_B5G5R5X1_UNORM:
460 case PIPE_FORMAT_B4G4R4A4_UNORM:
461 case PIPE_FORMAT_B8G8R8A8_UNORM:
462 case PIPE_FORMAT_B8G8R8X8_UNORM:
464 R300_C0_SEL_B | R300_C1_SEL_G |
465 R300_C2_SEL_R | R300_C3_SEL_A;
468 case PIPE_FORMAT_A8R8G8B8_UNORM:
469 case PIPE_FORMAT_X8R8G8B8_UNORM:
471 R300_C0_SEL_A | R300_C1_SEL_R |
472 R300_C2_SEL_G | R300_C3_SEL_B;
475 case PIPE_FORMAT_A8B8G8R8_UNORM:
476 case PIPE_FORMAT_X8B8G8R8_UNORM:
478 R300_C0_SEL_A | R300_C1_SEL_B |
479 R300_C2_SEL_G | R300_C3_SEL_R;
482 case PIPE_FORMAT_R8G8B8A8_SNORM:
483 case PIPE_FORMAT_R8SG8SB8UX8U_NORM:
484 case PIPE_FORMAT_R10G10B10A2_UNORM:
485 case PIPE_FORMAT_R16G16B16A16_UNORM:
486 case PIPE_FORMAT_R16G16B16A16_SNORM:
487 //case PIPE_FORMAT_R16G16B16A16_FLOAT: /* not in pipe_format */
488 case PIPE_FORMAT_R32G32B32A32_FLOAT:
490 R300_C0_SEL_R | R300_C1_SEL_G |
491 R300_C2_SEL_B | R300_C3_SEL_A;
494 return ~0; /* Unsupported. */
498 boolean r300_is_colorbuffer_format_supported(enum pipe_format format)
500 return r300_translate_colorformat(format) != ~0 &&
501 r300_translate_out_fmt(format) != ~0;
504 boolean r300_is_zs_format_supported(enum pipe_format format)
506 return r300_translate_zsformat(format) != ~0;
509 boolean r300_is_sampler_format_supported(enum pipe_format format)
511 return r300_translate_texformat(format) != ~0;
514 static void r300_setup_texture_state(struct r300_screen* screen, struct r300_texture* tex)
516 struct r300_texture_format_state* state = &tex->state;
517 struct pipe_resource *pt = &tex->b.b;
519 boolean is_r500 = screen->caps.is_r500;
521 /* Set sampler state. */
522 state->format0 = R300_TX_WIDTH((pt->width0 - 1) & 0x7ff) |
523 R300_TX_HEIGHT((pt->height0 - 1) & 0x7ff);
525 if (tex->uses_pitch) {
526 /* rectangles love this */
527 state->format0 |= R300_TX_PITCH_EN;
528 state->format2 = (tex->pitch[0] - 1) & 0x1fff;
530 /* power of two textures (3D, mipmaps, and no pitch) */
531 state->format0 |= R300_TX_DEPTH(util_logbase2(pt->depth0) & 0xf);
534 state->format1 = r300_translate_texformat(pt->format);
535 if (pt->target == PIPE_TEXTURE_CUBE) {
536 state->format1 |= R300_TX_FORMAT_CUBIC_MAP;
538 if (pt->target == PIPE_TEXTURE_3D) {
539 state->format1 |= R300_TX_FORMAT_3D;
542 /* large textures on r500 */
545 if (pt->width0 > 2048) {
546 state->format2 |= R500_TXWIDTH_BIT11;
548 if (pt->height0 > 2048) {
549 state->format2 |= R500_TXHEIGHT_BIT11;
551 state->format2 |= r500_tx_format_msb_bit(pt->format);
554 SCREEN_DBG(screen, DBG_TEX, "r300: Set texture state (%dx%d, %d levels)\n",
555 pt->width0, pt->height0, pt->last_level);
557 /* Set framebuffer state. */
558 if (util_format_is_depth_or_stencil(tex->b.b.format)) {
559 for (i = 0; i <= tex->b.b.last_level; i++) {
560 tex->fb_state.depthpitch[i] =
562 R300_DEPTHMACROTILE(tex->mip_macrotile[i]) |
563 R300_DEPTHMICROTILE(tex->microtile);
565 tex->fb_state.zb_format = r300_translate_zsformat(tex->b.b.format);
567 for (i = 0; i <= tex->b.b.last_level; i++) {
568 tex->fb_state.colorpitch[i] =
570 r300_translate_colorformat(tex->b.b.format) |
571 R300_COLOR_TILE(tex->mip_macrotile[i]) |
572 R300_COLOR_MICROTILE(tex->microtile);
574 tex->fb_state.us_out_fmt = r300_translate_out_fmt(tex->b.b.format);
578 void r300_texture_reinterpret_format(struct pipe_screen *screen,
579 struct pipe_resource *tex,
580 enum pipe_format new_format)
582 struct r300_screen *r300screen = r300_screen(screen);
584 SCREEN_DBG(r300screen, DBG_TEX, "r300: Reinterpreting format: %s -> %s\n",
585 util_format_name(tex->format), util_format_name(new_format));
587 tex->format = new_format;
589 r300_setup_texture_state(r300_screen(screen), r300_texture(tex));
592 unsigned r300_texture_get_offset(struct r300_texture* tex, unsigned level,
593 unsigned zslice, unsigned face)
595 unsigned offset = tex->offset[level];
597 switch (tex->b.b.target) {
598 case PIPE_TEXTURE_3D:
600 return offset + zslice * tex->layer_size[level];
602 case PIPE_TEXTURE_CUBE:
604 return offset + face * tex->layer_size[level];
607 assert(zslice == 0 && face == 0);
613 * Return the width (dim==TILE_WIDTH) or height (dim==TILE_HEIGHT) of one tile
614 * of the given texture.
616 static unsigned r300_texture_get_tile_size(struct r300_texture* tex,
617 int dim, boolean macrotile)
619 unsigned pixsize, tile_size;
621 pixsize = util_format_get_blocksize(tex->b.b.format);
622 tile_size = microblock_table[util_logbase2(pixsize)][tex->microtile][dim];
632 /* Return true if macrotiling should be enabled on the miplevel. */
633 static boolean r300_texture_macro_switch(struct r300_texture *tex,
638 unsigned tile, texdim;
640 tile = r300_texture_get_tile_size(tex, dim, TRUE);
641 if (dim == TILE_WIDTH) {
642 texdim = u_minify(tex->b.b.width0, level);
644 texdim = u_minify(tex->b.b.height0, level);
647 /* See TX_FILTER1_n.MACRO_SWITCH. */
649 return texdim >= tile;
651 return texdim > tile;
656 * Return the stride, in bytes, of the texture images of the given texture
657 * at the given level.
659 unsigned r300_texture_get_stride(struct r300_screen* screen,
660 struct r300_texture* tex, unsigned level)
662 unsigned tile_width, width;
664 if (tex->stride_override)
665 return tex->stride_override;
667 /* Check the level. */
668 if (level > tex->b.b.last_level) {
669 SCREEN_DBG(screen, DBG_TEX, "%s: level (%u) > last_level (%u)\n",
670 __FUNCTION__, level, tex->b.b.last_level);
674 width = u_minify(tex->b.b.width0, level);
676 if (r300_format_is_plain(tex->b.b.format)) {
677 tile_width = r300_texture_get_tile_size(tex, TILE_WIDTH,
678 tex->mip_macrotile[level]);
679 width = align(width, tile_width);
681 return util_format_get_stride(tex->b.b.format, width);
683 return align(util_format_get_stride(tex->b.b.format, width), 32);
687 static unsigned r300_texture_get_nblocksy(struct r300_texture* tex,
690 unsigned height, tile_height;
692 height = u_minify(tex->b.b.height0, level);
694 if (r300_format_is_plain(tex->b.b.format)) {
695 tile_height = r300_texture_get_tile_size(tex, TILE_HEIGHT,
696 tex->mip_macrotile[level]);
697 height = align(height, tile_height);
699 /* This is needed for the kernel checker, unfortunately. */
700 height = util_next_power_of_two(height);
703 return util_format_get_nblocksy(tex->b.b.format, height);
706 static void r300_setup_miptree(struct r300_screen* screen,
707 struct r300_texture* tex)
709 struct pipe_resource* base = &tex->b.b;
710 unsigned stride, size, layer_size, nblocksy, i;
711 boolean rv350_mode = screen->caps.family >= CHIP_FAMILY_RV350;
713 SCREEN_DBG(screen, DBG_TEX, "r300: Making miptree for texture, format %s\n",
714 util_format_name(base->format));
716 for (i = 0; i <= base->last_level; i++) {
717 /* Let's see if this miplevel can be macrotiled. */
718 tex->mip_macrotile[i] =
719 (tex->macrotile == R300_BUFFER_TILED &&
720 r300_texture_macro_switch(tex, i, rv350_mode, TILE_WIDTH) &&
721 r300_texture_macro_switch(tex, i, rv350_mode, TILE_HEIGHT)) ?
722 R300_BUFFER_TILED : R300_BUFFER_LINEAR;
724 stride = r300_texture_get_stride(screen, tex, i);
725 nblocksy = r300_texture_get_nblocksy(tex, i);
726 layer_size = stride * nblocksy;
728 if (base->target == PIPE_TEXTURE_CUBE)
729 size = layer_size * 6;
731 size = layer_size * u_minify(base->depth0, i);
733 tex->offset[i] = tex->size;
734 tex->size = tex->offset[i] + size;
735 tex->layer_size[i] = layer_size;
736 tex->pitch[i] = stride / util_format_get_blocksize(base->format);
738 SCREEN_DBG(screen, DBG_TEX, "r300: Texture miptree: Level %d "
739 "(%dx%dx%d px, pitch %d bytes) %d bytes total, macrotiled %s\n",
740 i, u_minify(base->width0, i), u_minify(base->height0, i),
741 u_minify(base->depth0, i), stride, tex->size,
742 tex->mip_macrotile[i] ? "TRUE" : "FALSE");
746 static void r300_setup_flags(struct r300_texture* tex)
748 tex->uses_pitch = !util_is_power_of_two(tex->b.b.width0) ||
749 !util_is_power_of_two(tex->b.b.height0) ||
750 tex->stride_override;
753 static void r300_setup_tiling(struct pipe_screen *screen,
754 struct r300_texture *tex)
756 struct r300_winsys_screen *rws = (struct r300_winsys_screen *)screen->winsys;
757 enum pipe_format format = tex->b.b.format;
758 boolean rv350_mode = r300_screen(screen)->caps.family >= CHIP_FAMILY_RV350;
760 if (!r300_format_is_plain(format)) {
764 if (tex->b.b.width0 == 1 ||
765 tex->b.b.height0 == 1) {
769 /* Set microtiling. */
770 switch (util_format_get_blocksize(format)) {
773 tex->microtile = R300_BUFFER_TILED;
778 if (rws->get_value(rws, R300_VID_SQUARE_TILING_SUPPORT)) {
779 tex->microtile = R300_BUFFER_SQUARETILED;
784 /* Set macrotiling. */
785 if (r300_texture_macro_switch(tex, 0, rv350_mode, TILE_WIDTH) &&
786 r300_texture_macro_switch(tex, 0, rv350_mode, TILE_HEIGHT)) {
787 tex->macrotile = R300_BUFFER_TILED;
792 static unsigned r300_texture_is_referenced(struct pipe_context *context,
793 struct pipe_resource *texture,
794 unsigned face, unsigned level)
796 struct r300_context *r300 = r300_context(context);
797 struct r300_texture *rtex = (struct r300_texture *)texture;
799 if (r300->rws->is_buffer_referenced(r300->rws, rtex->buffer))
800 return PIPE_REFERENCED_FOR_READ | PIPE_REFERENCED_FOR_WRITE;
802 return PIPE_UNREFERENCED;
805 static void r300_texture_destroy(struct pipe_screen *screen,
806 struct pipe_resource* texture)
808 struct r300_texture* tex = (struct r300_texture*)texture;
809 struct r300_winsys_screen *rws = (struct r300_winsys_screen *)texture->screen->winsys;
811 rws->buffer_reference(rws, &tex->buffer, NULL);
819 r300_texture_get_handle(struct pipe_screen* screen,
820 struct pipe_resource *texture,
821 struct winsys_handle *whandle)
823 struct r300_winsys_screen *rws = (struct r300_winsys_screen *)screen->winsys;
824 struct r300_texture* tex = (struct r300_texture*)texture;
831 stride = r300_texture_get_stride(r300_screen(screen), tex, 0);
833 rws->buffer_get_handle(rws, tex->buffer, stride, whandle);
840 struct u_resource_vtbl r300_texture_vtbl =
842 r300_texture_get_handle, /* get_handle */
843 r300_texture_destroy, /* resource_destroy */
844 r300_texture_is_referenced, /* is_resource_referenced */
845 r300_texture_get_transfer, /* get_transfer */
846 r300_texture_transfer_destroy, /* transfer_destroy */
847 r300_texture_transfer_map, /* transfer_map */
848 u_default_transfer_flush_region, /* transfer_flush_region */
849 r300_texture_transfer_unmap, /* transfer_unmap */
850 u_default_transfer_inline_write /* transfer_inline_write */
855 /* Create a new texture. */
856 struct pipe_resource* r300_texture_create(struct pipe_screen* screen,
857 const struct pipe_resource* base)
859 struct r300_texture* tex = CALLOC_STRUCT(r300_texture);
860 struct r300_screen* rscreen = r300_screen(screen);
861 struct r300_winsys_screen *rws = (struct r300_winsys_screen *)screen->winsys;
868 tex->b.vtbl = &r300_texture_vtbl;
869 pipe_reference_init(&tex->b.b.reference, 1);
870 tex->b.b.screen = screen;
872 r300_setup_flags(tex);
873 if (!(base->flags & R300_RESOURCE_FLAG_TRANSFER) &&
874 !(base->bind & PIPE_BIND_SCANOUT)) {
875 r300_setup_tiling(screen, tex);
877 r300_setup_miptree(rscreen, tex);
878 r300_setup_texture_state(rscreen, tex);
880 tex->buffer = rws->buffer_create(rws, 2048,
881 PIPE_BIND_SAMPLER_VIEW, /* XXX */
883 rws->buffer_set_tiling(rws, tex->buffer,
893 return (struct pipe_resource*)tex;
898 /* Not required to implement u_resource_vtbl, consider moving to another file:
900 struct pipe_surface* r300_get_tex_surface(struct pipe_screen* screen,
901 struct pipe_resource* texture,
907 struct r300_texture* tex = r300_texture(texture);
908 struct pipe_surface* surface = CALLOC_STRUCT(pipe_surface);
911 offset = r300_texture_get_offset(tex, level, zslice, face);
914 pipe_reference_init(&surface->reference, 1);
915 pipe_resource_reference(&surface->texture, texture);
916 surface->format = texture->format;
917 surface->width = u_minify(texture->width0, level);
918 surface->height = u_minify(texture->height0, level);
919 surface->offset = offset;
920 surface->usage = flags;
921 surface->zslice = zslice;
922 surface->texture = texture;
923 surface->face = face;
924 surface->level = level;
930 /* Not required to implement u_resource_vtbl, consider moving to another file:
932 void r300_tex_surface_destroy(struct pipe_surface* s)
934 pipe_resource_reference(&s->texture, NULL);
938 struct pipe_resource*
939 r300_texture_from_handle(struct pipe_screen* screen,
940 const struct pipe_resource* base,
941 struct winsys_handle *whandle)
943 struct r300_winsys_screen *rws = (struct r300_winsys_screen*)screen->winsys;
944 struct r300_screen* rscreen = r300_screen(screen);
945 struct r300_winsys_buffer *buffer;
946 struct r300_texture* tex;
948 boolean override_zb_flags;
950 /* Support only 2D textures without mipmaps */
951 if (base->target != PIPE_TEXTURE_2D ||
953 base->last_level != 0) {
957 buffer = rws->buffer_from_handle(rws, screen, whandle, &stride);
962 tex = CALLOC_STRUCT(r300_texture);
968 tex->b.vtbl = &r300_texture_vtbl;
969 pipe_reference_init(&tex->b.b.reference, 1);
970 tex->b.b.screen = screen;
972 tex->stride_override = stride;
974 /* one ref already taken */
975 tex->buffer = buffer;
977 rws->buffer_get_tiling(rws, buffer, &tex->microtile, &tex->macrotile);
978 r300_setup_flags(tex);
980 /* Enforce microtiled zbuffer. */
981 override_zb_flags = util_format_is_depth_or_stencil(base->format) &&
982 tex->microtile == R300_BUFFER_LINEAR;
984 if (override_zb_flags) {
985 switch (util_format_get_blocksize(base->format)) {
987 tex->microtile = R300_BUFFER_TILED;
991 if (rws->get_value(rws, R300_VID_SQUARE_TILING_SUPPORT)) {
992 tex->microtile = R300_BUFFER_SQUARETILED;
998 override_zb_flags = FALSE;
1002 r300_setup_miptree(rscreen, tex);
1003 r300_setup_texture_state(rscreen, tex);
1005 if (override_zb_flags) {
1006 rws->buffer_set_tiling(rws, tex->buffer,
1011 return (struct pipe_resource*)tex;