Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * Copyright 2008 Corbin Simpson <MostAwesomeDude@gmail.com>
  3.  * Copyright 2010 Marek Olšák <maraeo@gmail.com>
  4.  *
  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:
  11.  *
  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
  14.  * Software.
  15.  *
  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. */
  23.  
  24. /* Always include headers in the reverse order!! ~ M. */
  25. #include "r300_texture.h"
  26.  
  27. #include "r300_context.h"
  28. #include "r300_reg.h"
  29. #include "r300_texture_desc.h"
  30. #include "r300_transfer.h"
  31. #include "r300_screen.h"
  32.  
  33. #include "util/u_format.h"
  34. #include "util/u_format_s3tc.h"
  35. #include "util/u_math.h"
  36. #include "util/u_memory.h"
  37. #include "util/u_mm.h"
  38.  
  39. #include "pipe/p_screen.h"
  40.  
  41. unsigned r300_get_swizzle_combined(const unsigned char *swizzle_format,
  42.                                    const unsigned char *swizzle_view,
  43.                                    boolean dxtc_swizzle)
  44. {
  45.     unsigned i;
  46.     unsigned char swizzle[4];
  47.     unsigned result = 0;
  48.     const uint32_t swizzle_shift[4] = {
  49.         R300_TX_FORMAT_R_SHIFT,
  50.         R300_TX_FORMAT_G_SHIFT,
  51.         R300_TX_FORMAT_B_SHIFT,
  52.         R300_TX_FORMAT_A_SHIFT
  53.     };
  54.     uint32_t swizzle_bit[4] = {
  55.         dxtc_swizzle ? R300_TX_FORMAT_Z : R300_TX_FORMAT_X,
  56.         R300_TX_FORMAT_Y,
  57.         dxtc_swizzle ? R300_TX_FORMAT_X : R300_TX_FORMAT_Z,
  58.         R300_TX_FORMAT_W
  59.     };
  60.  
  61.     if (swizzle_view) {
  62.         /* Combine two sets of swizzles. */
  63.         util_format_compose_swizzles(swizzle_format, swizzle_view, swizzle);
  64.     } else {
  65.         memcpy(swizzle, swizzle_format, 4);
  66.     }
  67.  
  68.     /* Get swizzle. */
  69.     for (i = 0; i < 4; i++) {
  70.         switch (swizzle[i]) {
  71.             case UTIL_FORMAT_SWIZZLE_Y:
  72.                 result |= swizzle_bit[1] << swizzle_shift[i];
  73.                 break;
  74.             case UTIL_FORMAT_SWIZZLE_Z:
  75.                 result |= swizzle_bit[2] << swizzle_shift[i];
  76.                 break;
  77.             case UTIL_FORMAT_SWIZZLE_W:
  78.                 result |= swizzle_bit[3] << swizzle_shift[i];
  79.                 break;
  80.             case UTIL_FORMAT_SWIZZLE_0:
  81.                 result |= R300_TX_FORMAT_ZERO << swizzle_shift[i];
  82.                 break;
  83.             case UTIL_FORMAT_SWIZZLE_1:
  84.                 result |= R300_TX_FORMAT_ONE << swizzle_shift[i];
  85.                 break;
  86.             default: /* UTIL_FORMAT_SWIZZLE_X */
  87.                 result |= swizzle_bit[0] << swizzle_shift[i];
  88.         }
  89.     }
  90.     return result;
  91. }
  92.  
  93. /* Translate a pipe_format into a useful texture format for sampling.
  94.  *
  95.  * Some special formats are translated directly using R300_EASY_TX_FORMAT,
  96.  * but the majority of them is translated in a generic way, automatically
  97.  * supporting all the formats hw can support.
  98.  *
  99.  * R300_EASY_TX_FORMAT swizzles the texture.
  100.  * Note the signature of R300_EASY_TX_FORMAT:
  101.  *   R300_EASY_TX_FORMAT(B, G, R, A, FORMAT);
  102.  *
  103.  * The FORMAT specifies how the texture sampler will treat the texture, and
  104.  * makes available X, Y, Z, W, ZERO, and ONE for swizzling. */
  105. uint32_t r300_translate_texformat(enum pipe_format format,
  106.                                   const unsigned char *swizzle_view,
  107.                                   boolean is_r500,
  108.                                   boolean dxtc_swizzle)
  109. {
  110.     uint32_t result = 0;
  111.     const struct util_format_description *desc;
  112.     unsigned i;
  113.     boolean uniform = TRUE;
  114.     const uint32_t sign_bit[4] = {
  115.         R300_TX_FORMAT_SIGNED_W,
  116.         R300_TX_FORMAT_SIGNED_Z,
  117.         R300_TX_FORMAT_SIGNED_Y,
  118.         R300_TX_FORMAT_SIGNED_X,
  119.     };
  120.  
  121.     desc = util_format_description(format);
  122.  
  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:
  128.             switch (format) {
  129.                 case PIPE_FORMAT_Z16_UNORM:
  130.                     return R300_TX_FORMAT_X16;
  131.                 case PIPE_FORMAT_X8Z24_UNORM:
  132.                 case PIPE_FORMAT_S8_UINT_Z24_UNORM:
  133.                     if (is_r500)
  134.                         return R500_TX_FORMAT_Y8X24;
  135.                     else
  136.                         return R300_TX_FORMAT_Y16X16;
  137.                 default:
  138.                     return ~0; /* Unsupported. */
  139.             }
  140.  
  141.         /* YUV formats. */
  142.         case UTIL_FORMAT_COLORSPACE_YUV:
  143.             result |= R300_TX_FORMAT_YUV_TO_RGB;
  144.  
  145.             switch (format) {
  146.                 case PIPE_FORMAT_UYVY:
  147.                     return R300_EASY_TX_FORMAT(X, Y, Z, ONE, YVYU422) | result;
  148.                 case PIPE_FORMAT_YUYV:
  149.                     return R300_EASY_TX_FORMAT(X, Y, Z, ONE, VYUY422) | result;
  150.                 default:
  151.                     return ~0; /* Unsupported/unknown. */
  152.             }
  153.  
  154.         /* Add gamma correction. */
  155.         case UTIL_FORMAT_COLORSPACE_SRGB:
  156.             result |= R300_TX_FORMAT_GAMMA;
  157.             break;
  158.  
  159.         default:
  160.             switch (format) {
  161.                 /* Same as YUV but without the YUR->RGB conversion. */
  162.                 case PIPE_FORMAT_R8G8_B8G8_UNORM:
  163.                     return R300_EASY_TX_FORMAT(X, Y, Z, ONE, YVYU422) | result;
  164.                 case PIPE_FORMAT_G8R8_G8B8_UNORM:
  165.                     return R300_EASY_TX_FORMAT(X, Y, Z, ONE, VYUY422) | result;
  166.                 default:;
  167.             }
  168.     }
  169.  
  170.     /* Add swizzling. */
  171.     /* The RGTC1_SNORM and LATC1_SNORM swizzle is done in the shader. */
  172.     if (format != PIPE_FORMAT_RGTC1_SNORM &&
  173.         format != PIPE_FORMAT_LATC1_SNORM) {
  174.         if (util_format_is_compressed(format) &&
  175.             dxtc_swizzle &&
  176.             format != PIPE_FORMAT_RGTC2_UNORM &&
  177.             format != PIPE_FORMAT_RGTC2_SNORM &&
  178.             format != PIPE_FORMAT_LATC2_UNORM &&
  179.             format != PIPE_FORMAT_LATC2_SNORM) {
  180.             result |= r300_get_swizzle_combined(desc->swizzle, swizzle_view,
  181.                                                 TRUE);
  182.         } else {
  183.             result |= r300_get_swizzle_combined(desc->swizzle, swizzle_view,
  184.                                                 FALSE);
  185.         }
  186.     }
  187.  
  188.     /* S3TC formats. */
  189.     if (desc->layout == UTIL_FORMAT_LAYOUT_S3TC) {
  190.         if (!util_format_s3tc_enabled) {
  191.             return ~0; /* Unsupported. */
  192.         }
  193.  
  194.         switch (format) {
  195.             case PIPE_FORMAT_DXT1_RGB:
  196.             case PIPE_FORMAT_DXT1_RGBA:
  197.             case PIPE_FORMAT_DXT1_SRGB:
  198.             case PIPE_FORMAT_DXT1_SRGBA:
  199.                 return R300_TX_FORMAT_DXT1 | result;
  200.             case PIPE_FORMAT_DXT3_RGBA:
  201.             case PIPE_FORMAT_DXT3_SRGBA:
  202.                 return R300_TX_FORMAT_DXT3 | result;
  203.             case PIPE_FORMAT_DXT5_RGBA:
  204.             case PIPE_FORMAT_DXT5_SRGBA:
  205.                 return R300_TX_FORMAT_DXT5 | result;
  206.             default:
  207.                 return ~0; /* Unsupported/unknown. */
  208.         }
  209.     }
  210.  
  211.     /* RGTC formats. */
  212.     if (desc->layout == UTIL_FORMAT_LAYOUT_RGTC) {
  213.         switch (format) {
  214.             case PIPE_FORMAT_RGTC1_SNORM:
  215.             case PIPE_FORMAT_LATC1_SNORM:
  216.             case PIPE_FORMAT_LATC1_UNORM:
  217.             case PIPE_FORMAT_RGTC1_UNORM:
  218.                 return R500_TX_FORMAT_ATI1N | result;
  219.  
  220.             case PIPE_FORMAT_RGTC2_SNORM:
  221.             case PIPE_FORMAT_LATC2_SNORM:
  222.                 result |= sign_bit[1] | sign_bit[0];
  223.             case PIPE_FORMAT_RGTC2_UNORM:
  224.             case PIPE_FORMAT_LATC2_UNORM:
  225.                 return R400_TX_FORMAT_ATI2N | result;
  226.  
  227.             default:
  228.                 return ~0; /* Unsupported/unknown. */
  229.         }
  230.     }
  231.  
  232.     /* This is truly a special format.
  233.      * It stores R8G8 and B is computed using sqrt(1 - R^2 - G^2)
  234.      * in the sampler unit. Also known as D3DFMT_CxV8U8. */
  235.     if (format == PIPE_FORMAT_R8G8Bx_SNORM) {
  236.         return R300_TX_FORMAT_CxV8U8 | result;
  237.     }
  238.  
  239.     /* Integer and fixed-point 16.16 textures are not supported. */
  240.     for (i = 0; i < 4; i++) {
  241.         if (desc->channel[i].type == UTIL_FORMAT_TYPE_FIXED ||
  242.             ((desc->channel[i].type == UTIL_FORMAT_TYPE_SIGNED ||
  243.               desc->channel[i].type == UTIL_FORMAT_TYPE_UNSIGNED) &&
  244.              (!desc->channel[i].normalized ||
  245.               desc->channel[i].pure_integer))) {
  246.             return ~0; /* Unsupported/unknown. */
  247.         }
  248.     }
  249.  
  250.     /* Add sign. */
  251.     for (i = 0; i < desc->nr_channels; i++) {
  252.         if (desc->channel[i].type == UTIL_FORMAT_TYPE_SIGNED) {
  253.             result |= sign_bit[i];
  254.         }
  255.     }
  256.  
  257.     /* See whether the components are of the same size. */
  258.     for (i = 1; i < desc->nr_channels; i++) {
  259.         uniform = uniform && desc->channel[0].size == desc->channel[i].size;
  260.     }
  261.  
  262.     /* Non-uniform formats. */
  263.     if (!uniform) {
  264.         switch (desc->nr_channels) {
  265.             case 3:
  266.                 if (desc->channel[0].size == 5 &&
  267.                     desc->channel[1].size == 6 &&
  268.                     desc->channel[2].size == 5) {
  269.                     return R300_TX_FORMAT_Z5Y6X5 | result;
  270.                 }
  271.                 if (desc->channel[0].size == 5 &&
  272.                     desc->channel[1].size == 5 &&
  273.                     desc->channel[2].size == 6) {
  274.                     return R300_TX_FORMAT_Z6Y5X5 | result;
  275.                 }
  276.                 if (desc->channel[0].size == 2 &&
  277.                     desc->channel[1].size == 3 &&
  278.                     desc->channel[2].size == 3) {
  279.                     return R300_TX_FORMAT_Z3Y3X2 | result;
  280.                 }
  281.                 return ~0; /* Unsupported/unknown. */
  282.  
  283.             case 4:
  284.                 if (desc->channel[0].size == 5 &&
  285.                     desc->channel[1].size == 5 &&
  286.                     desc->channel[2].size == 5 &&
  287.                     desc->channel[3].size == 1) {
  288.                     return R300_TX_FORMAT_W1Z5Y5X5 | result;
  289.                 }
  290.                 if (desc->channel[0].size == 10 &&
  291.                     desc->channel[1].size == 10 &&
  292.                     desc->channel[2].size == 10 &&
  293.                     desc->channel[3].size == 2) {
  294.                     return R300_TX_FORMAT_W2Z10Y10X10 | result;
  295.                 }
  296.         }
  297.         return ~0; /* Unsupported/unknown. */
  298.     }
  299.  
  300.     /* Find the first non-VOID channel. */
  301.     for (i = 0; i < 4; i++) {
  302.         if (desc->channel[i].type != UTIL_FORMAT_TYPE_VOID) {
  303.             break;
  304.         }
  305.     }
  306.  
  307.     if (i == 4)
  308.         return ~0; /* Unsupported/unknown. */
  309.  
  310.     /* And finally, uniform formats. */
  311.     switch (desc->channel[i].type) {
  312.         case UTIL_FORMAT_TYPE_UNSIGNED:
  313.         case UTIL_FORMAT_TYPE_SIGNED:
  314.             if (!desc->channel[i].normalized &&
  315.                 desc->colorspace != UTIL_FORMAT_COLORSPACE_SRGB) {
  316.                 return ~0;
  317.             }
  318.  
  319.             switch (desc->channel[i].size) {
  320.                 case 4:
  321.                     switch (desc->nr_channels) {
  322.                         case 2:
  323.                             return R300_TX_FORMAT_Y4X4 | result;
  324.                         case 4:
  325.                             return R300_TX_FORMAT_W4Z4Y4X4 | result;
  326.                     }
  327.                     return ~0;
  328.  
  329.                 case 8:
  330.                     switch (desc->nr_channels) {
  331.                         case 1:
  332.                             return R300_TX_FORMAT_X8 | result;
  333.                         case 2:
  334.                             return R300_TX_FORMAT_Y8X8 | result;
  335.                         case 4:
  336.                             return R300_TX_FORMAT_W8Z8Y8X8 | result;
  337.                     }
  338.                     return ~0;
  339.  
  340.                 case 16:
  341.                     switch (desc->nr_channels) {
  342.                         case 1:
  343.                             return R300_TX_FORMAT_X16 | result;
  344.                         case 2:
  345.                             return R300_TX_FORMAT_Y16X16 | result;
  346.                         case 4:
  347.                             return R300_TX_FORMAT_W16Z16Y16X16 | result;
  348.                     }
  349.             }
  350.             return ~0;
  351.  
  352.         case UTIL_FORMAT_TYPE_FLOAT:
  353.             switch (desc->channel[i].size) {
  354.                 case 16:
  355.                     switch (desc->nr_channels) {
  356.                         case 1:
  357.                             return R300_TX_FORMAT_16F | result;
  358.                         case 2:
  359.                             return R300_TX_FORMAT_16F_16F | result;
  360.                         case 4:
  361.                             return R300_TX_FORMAT_16F_16F_16F_16F | result;
  362.                     }
  363.                     return ~0;
  364.  
  365.                 case 32:
  366.                     switch (desc->nr_channels) {
  367.                         case 1:
  368.                             return R300_TX_FORMAT_32F | result;
  369.                         case 2:
  370.                             return R300_TX_FORMAT_32F_32F | result;
  371.                         case 4:
  372.                             return R300_TX_FORMAT_32F_32F_32F_32F | result;
  373.                     }
  374.             }
  375.     }
  376.  
  377.     return ~0; /* Unsupported/unknown. */
  378. }
  379.  
  380. uint32_t r500_tx_format_msb_bit(enum pipe_format format)
  381. {
  382.     switch (format) {
  383.         case PIPE_FORMAT_RGTC1_UNORM:
  384.         case PIPE_FORMAT_RGTC1_SNORM:
  385.         case PIPE_FORMAT_LATC1_UNORM:
  386.         case PIPE_FORMAT_LATC1_SNORM:
  387.         case PIPE_FORMAT_X8Z24_UNORM:
  388.         case PIPE_FORMAT_S8_UINT_Z24_UNORM:
  389.             return R500_TXFORMAT_MSB;
  390.         default:
  391.             return 0;
  392.     }
  393. }
  394.  
  395. /* Buffer formats. */
  396.  
  397. /* Colorbuffer formats. This is the unswizzled format of the RB3D block's
  398.  * output. For the swizzling of the targets, check the shader's format. */
  399. static uint32_t r300_translate_colorformat(enum pipe_format format)
  400. {
  401.     switch (format) {
  402.         /* 8-bit buffers. */
  403.         case PIPE_FORMAT_A8_UNORM:
  404.         case PIPE_FORMAT_A8_SNORM:
  405.         case PIPE_FORMAT_I8_UNORM:
  406.         case PIPE_FORMAT_I8_SNORM:
  407.         case PIPE_FORMAT_L8_UNORM:
  408.         case PIPE_FORMAT_L8_SNORM:
  409.         case PIPE_FORMAT_R8_UNORM:
  410.         case PIPE_FORMAT_R8_SNORM:
  411.             return R300_COLOR_FORMAT_I8;
  412.  
  413.         /* 16-bit buffers. */
  414.         case PIPE_FORMAT_L8A8_UNORM:
  415.         case PIPE_FORMAT_L8A8_SNORM:
  416.         case PIPE_FORMAT_R8G8_UNORM:
  417.         case PIPE_FORMAT_R8G8_SNORM:
  418.         case PIPE_FORMAT_R8A8_UNORM:
  419.         case PIPE_FORMAT_R8A8_SNORM:
  420.         /* These formats work fine with UV88 if US_OUT_FMT is set correctly. */
  421.         case PIPE_FORMAT_A16_UNORM:
  422.         case PIPE_FORMAT_A16_SNORM:
  423.         case PIPE_FORMAT_A16_FLOAT:
  424.         case PIPE_FORMAT_L16_UNORM:
  425.         case PIPE_FORMAT_L16_SNORM:
  426.         case PIPE_FORMAT_L16_FLOAT:
  427.         case PIPE_FORMAT_I16_UNORM:
  428.         case PIPE_FORMAT_I16_SNORM:
  429.         case PIPE_FORMAT_I16_FLOAT:
  430.         case PIPE_FORMAT_R16_UNORM:
  431.         case PIPE_FORMAT_R16_SNORM:
  432.         case PIPE_FORMAT_R16_FLOAT:
  433.             return R300_COLOR_FORMAT_UV88;
  434.  
  435.         case PIPE_FORMAT_B5G6R5_UNORM:
  436.             return R300_COLOR_FORMAT_RGB565;
  437.  
  438.         case PIPE_FORMAT_B5G5R5A1_UNORM:
  439.         case PIPE_FORMAT_B5G5R5X1_UNORM:
  440.             return R300_COLOR_FORMAT_ARGB1555;
  441.  
  442.         case PIPE_FORMAT_B4G4R4A4_UNORM:
  443.         case PIPE_FORMAT_B4G4R4X4_UNORM:
  444.             return R300_COLOR_FORMAT_ARGB4444;
  445.  
  446.         /* 32-bit buffers. */
  447.         case PIPE_FORMAT_B8G8R8A8_UNORM:
  448.         /*case PIPE_FORMAT_B8G8R8A8_SNORM:*/
  449.         case PIPE_FORMAT_B8G8R8X8_UNORM:
  450.         /*case PIPE_FORMAT_B8G8R8X8_SNORM:*/
  451.         case PIPE_FORMAT_R8G8B8A8_UNORM:
  452.         case PIPE_FORMAT_R8G8B8A8_SNORM:
  453.         case PIPE_FORMAT_R8G8B8X8_UNORM:
  454.         case PIPE_FORMAT_R8G8B8X8_SNORM:
  455.         /* These formats work fine with ARGB8888 if US_OUT_FMT is set
  456.          * correctly. */
  457.         case PIPE_FORMAT_R16G16_UNORM:
  458.         case PIPE_FORMAT_R16G16_SNORM:
  459.         case PIPE_FORMAT_R16G16_FLOAT:
  460.         case PIPE_FORMAT_L16A16_UNORM:
  461.         case PIPE_FORMAT_L16A16_SNORM:
  462.         case PIPE_FORMAT_L16A16_FLOAT:
  463.         case PIPE_FORMAT_R16A16_UNORM:
  464.         case PIPE_FORMAT_R16A16_SNORM:
  465.         case PIPE_FORMAT_R16A16_FLOAT:
  466.         case PIPE_FORMAT_A32_FLOAT:
  467.         case PIPE_FORMAT_L32_FLOAT:
  468.         case PIPE_FORMAT_I32_FLOAT:
  469.         case PIPE_FORMAT_R32_FLOAT:
  470.             return R300_COLOR_FORMAT_ARGB8888;
  471.  
  472.         case PIPE_FORMAT_R10G10B10A2_UNORM:
  473.         case PIPE_FORMAT_R10G10B10X2_SNORM:
  474.         case PIPE_FORMAT_B10G10R10A2_UNORM:
  475.         case PIPE_FORMAT_B10G10R10X2_UNORM:
  476.             return R500_COLOR_FORMAT_ARGB2101010;  /* R5xx-only? */
  477.  
  478.         /* 64-bit buffers. */
  479.         case PIPE_FORMAT_R16G16B16A16_UNORM:
  480.         case PIPE_FORMAT_R16G16B16A16_SNORM:
  481.         case PIPE_FORMAT_R16G16B16A16_FLOAT:
  482.         case PIPE_FORMAT_R16G16B16X16_UNORM:
  483.         case PIPE_FORMAT_R16G16B16X16_SNORM:
  484.         case PIPE_FORMAT_R16G16B16X16_FLOAT:
  485.         /* These formats work fine with ARGB16161616 if US_OUT_FMT is set
  486.          * correctly. */
  487.         case PIPE_FORMAT_R32G32_FLOAT:
  488.         case PIPE_FORMAT_L32A32_FLOAT:
  489.         case PIPE_FORMAT_R32A32_FLOAT:
  490.             return R300_COLOR_FORMAT_ARGB16161616;
  491.  
  492.         /* 128-bit buffers. */
  493.         case PIPE_FORMAT_R32G32B32A32_FLOAT:
  494.         case PIPE_FORMAT_R32G32B32X32_FLOAT:
  495.             return R300_COLOR_FORMAT_ARGB32323232;
  496.  
  497.         /* YUV buffers. */
  498.         case PIPE_FORMAT_UYVY:
  499.             return R300_COLOR_FORMAT_YVYU;
  500.         case PIPE_FORMAT_YUYV:
  501.             return R300_COLOR_FORMAT_VYUY;
  502.         default:
  503.             return ~0; /* Unsupported. */
  504.     }
  505. }
  506.  
  507. /* Depthbuffer and stencilbuffer. Thankfully, we only support two flavors. */
  508. static uint32_t r300_translate_zsformat(enum pipe_format format)
  509. {
  510.     switch (format) {
  511.         /* 16-bit depth, no stencil */
  512.         case PIPE_FORMAT_Z16_UNORM:
  513.             return R300_DEPTHFORMAT_16BIT_INT_Z;
  514.         /* 24-bit depth, ignored stencil */
  515.         case PIPE_FORMAT_X8Z24_UNORM:
  516.         /* 24-bit depth, 8-bit stencil */
  517.         case PIPE_FORMAT_S8_UINT_Z24_UNORM:
  518.             return R300_DEPTHFORMAT_24BIT_INT_Z_8BIT_STENCIL;
  519.         default:
  520.             return ~0; /* Unsupported. */
  521.     }
  522. }
  523.  
  524. /* Shader output formats. This is essentially the swizzle from the shader
  525.  * to the RB3D block.
  526.  *
  527.  * Note that formats are stored from C3 to C0. */
  528. static uint32_t r300_translate_out_fmt(enum pipe_format format)
  529. {
  530.     uint32_t modifier = 0;
  531.     unsigned i;
  532.     const struct util_format_description *desc;
  533.     boolean uniform_sign;
  534.  
  535.     desc = util_format_description(format);
  536.  
  537.     /* Find the first non-VOID channel. */
  538.     for (i = 0; i < 4; i++) {
  539.         if (desc->channel[i].type != UTIL_FORMAT_TYPE_VOID) {
  540.             break;
  541.         }
  542.     }
  543.  
  544.     if (i == 4)
  545.         return ~0; /* Unsupported/unknown. */
  546.  
  547.     /* Specifies how the shader output is written to the fog unit. */
  548.     switch (desc->channel[i].type) {
  549.     case UTIL_FORMAT_TYPE_FLOAT:
  550.         switch (desc->channel[i].size) {
  551.         case 32:
  552.             switch (desc->nr_channels) {
  553.             case 1:
  554.                 modifier |= R300_US_OUT_FMT_C_32_FP;
  555.                 break;
  556.             case 2:
  557.                 modifier |= R300_US_OUT_FMT_C2_32_FP;
  558.                 break;
  559.             case 4:
  560.                 modifier |= R300_US_OUT_FMT_C4_32_FP;
  561.                 break;
  562.             }
  563.             break;
  564.  
  565.         case 16:
  566.             switch (desc->nr_channels) {
  567.             case 1:
  568.                 modifier |= R300_US_OUT_FMT_C_16_FP;
  569.                 break;
  570.             case 2:
  571.                 modifier |= R300_US_OUT_FMT_C2_16_FP;
  572.                 break;
  573.             case 4:
  574.                 modifier |= R300_US_OUT_FMT_C4_16_FP;
  575.                 break;
  576.             }
  577.             break;
  578.         }
  579.         break;
  580.  
  581.     default:
  582.         switch (desc->channel[i].size) {
  583.         case 16:
  584.             switch (desc->nr_channels) {
  585.             case 1:
  586.                 modifier |= R300_US_OUT_FMT_C_16;
  587.                 break;
  588.             case 2:
  589.                 modifier |= R300_US_OUT_FMT_C2_16;
  590.                 break;
  591.             case 4:
  592.                 modifier |= R300_US_OUT_FMT_C4_16;
  593.                 break;
  594.             }
  595.             break;
  596.  
  597.         case 10:
  598.             modifier |= R300_US_OUT_FMT_C4_10;
  599.             break;
  600.  
  601.         default:
  602.             /* C4_8 seems to be used for the formats whose pixel size
  603.              * is <= 32 bits. */
  604.             modifier |= R300_US_OUT_FMT_C4_8;
  605.             break;
  606.         }
  607.     }
  608.  
  609.     /* Add sign. */
  610.     uniform_sign = TRUE;
  611.     for (i = 0; i < desc->nr_channels; i++)
  612.         if (desc->channel[i].type != UTIL_FORMAT_TYPE_SIGNED)
  613.             uniform_sign = FALSE;
  614.  
  615.     if (uniform_sign)
  616.         modifier |= R300_OUT_SIGN(0xf);
  617.  
  618.     /* Add swizzles and return. */
  619.     switch (format) {
  620.         /*** Special cases (non-standard channel mapping) ***/
  621.  
  622.         /* X8
  623.          * COLORFORMAT_I8 stores the Z component (C2). */
  624.         case PIPE_FORMAT_A8_UNORM:
  625.         case PIPE_FORMAT_A8_SNORM:
  626.             return modifier | R300_C2_SEL_A;
  627.         case PIPE_FORMAT_I8_UNORM:
  628.         case PIPE_FORMAT_I8_SNORM:
  629.         case PIPE_FORMAT_L8_UNORM:
  630.         case PIPE_FORMAT_L8_SNORM:
  631.         case PIPE_FORMAT_R8_UNORM:
  632.         case PIPE_FORMAT_R8_SNORM:
  633.             return modifier | R300_C2_SEL_R;
  634.  
  635.         /* X8Y8
  636.          * COLORFORMAT_UV88 stores ZX (C2 and C0). */
  637.         case PIPE_FORMAT_L8A8_SNORM:
  638.         case PIPE_FORMAT_L8A8_UNORM:
  639.         case PIPE_FORMAT_R8A8_SNORM:
  640.         case PIPE_FORMAT_R8A8_UNORM:
  641.             return modifier | R300_C0_SEL_A | R300_C2_SEL_R;
  642.         case PIPE_FORMAT_R8G8_SNORM:
  643.         case PIPE_FORMAT_R8G8_UNORM:
  644.             return modifier | R300_C0_SEL_G | R300_C2_SEL_R;
  645.  
  646.         /* X32Y32
  647.          * ARGB16161616 stores XZ for RG32F */
  648.         case PIPE_FORMAT_R32G32_FLOAT:
  649.             return modifier | R300_C0_SEL_R | R300_C2_SEL_G;
  650.  
  651.         /*** Generic cases (standard channel mapping) ***/
  652.  
  653.         /* BGRA outputs. */
  654.         case PIPE_FORMAT_B5G6R5_UNORM:
  655.         case PIPE_FORMAT_B5G5R5A1_UNORM:
  656.         case PIPE_FORMAT_B5G5R5X1_UNORM:
  657.         case PIPE_FORMAT_B4G4R4A4_UNORM:
  658.         case PIPE_FORMAT_B4G4R4X4_UNORM:
  659.         case PIPE_FORMAT_B8G8R8A8_UNORM:
  660.         /*case PIPE_FORMAT_B8G8R8A8_SNORM:*/
  661.         case PIPE_FORMAT_B8G8R8X8_UNORM:
  662.         /*case PIPE_FORMAT_B8G8R8X8_SNORM:*/
  663.         case PIPE_FORMAT_B10G10R10A2_UNORM:
  664.         case PIPE_FORMAT_B10G10R10X2_UNORM:
  665.             return modifier |
  666.                 R300_C0_SEL_B | R300_C1_SEL_G |
  667.                 R300_C2_SEL_R | R300_C3_SEL_A;
  668.  
  669.         /* ARGB outputs. */
  670.         case PIPE_FORMAT_A16_UNORM:
  671.         case PIPE_FORMAT_A16_SNORM:
  672.         case PIPE_FORMAT_A16_FLOAT:
  673.         case PIPE_FORMAT_A32_FLOAT:
  674.             return modifier |
  675.                 R300_C0_SEL_A | R300_C1_SEL_R |
  676.                 R300_C2_SEL_G | R300_C3_SEL_B;
  677.  
  678.         /* RGBA outputs. */
  679.         case PIPE_FORMAT_R8G8B8X8_UNORM:
  680.         case PIPE_FORMAT_R8G8B8X8_SNORM:
  681.         case PIPE_FORMAT_R8G8B8A8_UNORM:
  682.         case PIPE_FORMAT_R8G8B8A8_SNORM:
  683.         case PIPE_FORMAT_R10G10B10A2_UNORM:
  684.         case PIPE_FORMAT_R10G10B10X2_SNORM:
  685.         case PIPE_FORMAT_R16_UNORM:
  686.         case PIPE_FORMAT_R16G16_UNORM:
  687.         case PIPE_FORMAT_R16G16B16A16_UNORM:
  688.         case PIPE_FORMAT_R16_SNORM:
  689.         case PIPE_FORMAT_R16G16_SNORM:
  690.         case PIPE_FORMAT_R16G16B16A16_SNORM:
  691.         case PIPE_FORMAT_R16_FLOAT:
  692.         case PIPE_FORMAT_R16G16_FLOAT:
  693.         case PIPE_FORMAT_R16G16B16A16_FLOAT:
  694.         case PIPE_FORMAT_R32_FLOAT:
  695.         case PIPE_FORMAT_R32G32B32A32_FLOAT:
  696.         case PIPE_FORMAT_R32G32B32X32_FLOAT:
  697.         case PIPE_FORMAT_L16_UNORM:
  698.         case PIPE_FORMAT_L16_SNORM:
  699.         case PIPE_FORMAT_L16_FLOAT:
  700.         case PIPE_FORMAT_L32_FLOAT:
  701.         case PIPE_FORMAT_I16_UNORM:
  702.         case PIPE_FORMAT_I16_SNORM:
  703.         case PIPE_FORMAT_I16_FLOAT:
  704.         case PIPE_FORMAT_I32_FLOAT:
  705.         case PIPE_FORMAT_R16G16B16X16_UNORM:
  706.         case PIPE_FORMAT_R16G16B16X16_SNORM:
  707.         case PIPE_FORMAT_R16G16B16X16_FLOAT:
  708.             return modifier |
  709.                 R300_C0_SEL_R | R300_C1_SEL_G |
  710.                 R300_C2_SEL_B | R300_C3_SEL_A;
  711.  
  712.         /* LA outputs. */
  713.         case PIPE_FORMAT_L16A16_UNORM:
  714.         case PIPE_FORMAT_L16A16_SNORM:
  715.         case PIPE_FORMAT_L16A16_FLOAT:
  716.         case PIPE_FORMAT_R16A16_UNORM:
  717.         case PIPE_FORMAT_R16A16_SNORM:
  718.         case PIPE_FORMAT_R16A16_FLOAT:
  719.         case PIPE_FORMAT_L32A32_FLOAT:
  720.         case PIPE_FORMAT_R32A32_FLOAT:
  721.             return modifier |
  722.                 R300_C0_SEL_R | R300_C1_SEL_A;
  723.  
  724.         default:
  725.             return ~0; /* Unsupported. */
  726.     }
  727. }
  728.  
  729. static uint32_t r300_translate_colormask_swizzle(enum pipe_format format)
  730. {
  731.     switch (format) {
  732.     case PIPE_FORMAT_A8_UNORM:
  733.     case PIPE_FORMAT_A8_SNORM:
  734.     case PIPE_FORMAT_A16_UNORM:
  735.     case PIPE_FORMAT_A16_SNORM:
  736.     case PIPE_FORMAT_A16_FLOAT:
  737.     case PIPE_FORMAT_A32_FLOAT:
  738.         return COLORMASK_AAAA;
  739.  
  740.     case PIPE_FORMAT_I8_UNORM:
  741.     case PIPE_FORMAT_I8_SNORM:
  742.     case PIPE_FORMAT_L8_UNORM:
  743.     case PIPE_FORMAT_L8_SNORM:
  744.     case PIPE_FORMAT_R8_UNORM:
  745.     case PIPE_FORMAT_R8_SNORM:
  746.     case PIPE_FORMAT_R32_FLOAT:
  747.     case PIPE_FORMAT_L32_FLOAT:
  748.     case PIPE_FORMAT_I32_FLOAT:
  749.         return COLORMASK_RRRR;
  750.  
  751.     case PIPE_FORMAT_L8A8_SNORM:
  752.     case PIPE_FORMAT_L8A8_UNORM:
  753.     case PIPE_FORMAT_R8A8_UNORM:
  754.     case PIPE_FORMAT_R8A8_SNORM:
  755.     case PIPE_FORMAT_L16A16_UNORM:
  756.     case PIPE_FORMAT_L16A16_SNORM:
  757.     case PIPE_FORMAT_L16A16_FLOAT:
  758.     case PIPE_FORMAT_R16A16_UNORM:
  759.     case PIPE_FORMAT_R16A16_SNORM:
  760.     case PIPE_FORMAT_R16A16_FLOAT:
  761.     case PIPE_FORMAT_L32A32_FLOAT:
  762.     case PIPE_FORMAT_R32A32_FLOAT:
  763.         return COLORMASK_ARRA;
  764.  
  765.     case PIPE_FORMAT_R8G8_SNORM:
  766.     case PIPE_FORMAT_R8G8_UNORM:
  767.     case PIPE_FORMAT_R16G16_UNORM:
  768.     case PIPE_FORMAT_R16G16_SNORM:
  769.     case PIPE_FORMAT_R16G16_FLOAT:
  770.     case PIPE_FORMAT_R32G32_FLOAT:
  771.         return COLORMASK_GRRG;
  772.  
  773.     case PIPE_FORMAT_B5G5R5X1_UNORM:
  774.     case PIPE_FORMAT_B4G4R4X4_UNORM:
  775.     case PIPE_FORMAT_B8G8R8X8_UNORM:
  776.     /*case PIPE_FORMAT_B8G8R8X8_SNORM:*/
  777.     case PIPE_FORMAT_B10G10R10X2_UNORM:
  778.         return COLORMASK_BGRX;
  779.  
  780.     case PIPE_FORMAT_B5G6R5_UNORM:
  781.     case PIPE_FORMAT_B5G5R5A1_UNORM:
  782.     case PIPE_FORMAT_B4G4R4A4_UNORM:
  783.     case PIPE_FORMAT_B8G8R8A8_UNORM:
  784.     /*case PIPE_FORMAT_B8G8R8A8_SNORM:*/
  785.     case PIPE_FORMAT_B10G10R10A2_UNORM:
  786.         return COLORMASK_BGRA;
  787.  
  788.     case PIPE_FORMAT_R8G8B8X8_UNORM:
  789.     /* RGBX_SNORM formats are broken for an unknown reason */
  790.     /*case PIPE_FORMAT_R8G8B8X8_SNORM:*/
  791.     /*case PIPE_FORMAT_R10G10B10X2_SNORM:*/
  792.     case PIPE_FORMAT_R16G16B16X16_UNORM:
  793.     /*case PIPE_FORMAT_R16G16B16X16_SNORM:*/
  794.     case PIPE_FORMAT_R16G16B16X16_FLOAT:
  795.     case PIPE_FORMAT_R32G32B32X32_FLOAT:
  796.         return COLORMASK_RGBX;
  797.  
  798.     case PIPE_FORMAT_R8G8B8A8_UNORM:
  799.     case PIPE_FORMAT_R8G8B8A8_SNORM:
  800.     case PIPE_FORMAT_R10G10B10A2_UNORM:
  801.     case PIPE_FORMAT_R16_UNORM:
  802.     case PIPE_FORMAT_R16G16B16A16_UNORM:
  803.     case PIPE_FORMAT_R16_SNORM:
  804.     case PIPE_FORMAT_R16G16B16A16_SNORM:
  805.     case PIPE_FORMAT_R16_FLOAT:
  806.     case PIPE_FORMAT_R16G16B16A16_FLOAT:
  807.     case PIPE_FORMAT_R32G32B32A32_FLOAT:
  808.     case PIPE_FORMAT_L16_UNORM:
  809.     case PIPE_FORMAT_L16_SNORM:
  810.     case PIPE_FORMAT_L16_FLOAT:
  811.     case PIPE_FORMAT_I16_UNORM:
  812.     case PIPE_FORMAT_I16_SNORM:
  813.     case PIPE_FORMAT_I16_FLOAT:
  814.         return COLORMASK_RGBA;
  815.  
  816.     default:
  817.         return ~0; /* Unsupported. */
  818.     }
  819. }
  820.  
  821. boolean r300_is_colorbuffer_format_supported(enum pipe_format format)
  822. {
  823.     return r300_translate_colorformat(format) != ~0 &&
  824.            r300_translate_out_fmt(format) != ~0 &&
  825.            r300_translate_colormask_swizzle(format) != ~0;
  826. }
  827.  
  828. boolean r300_is_zs_format_supported(enum pipe_format format)
  829. {
  830.     return r300_translate_zsformat(format) != ~0;
  831. }
  832.  
  833. boolean r300_is_sampler_format_supported(enum pipe_format format)
  834. {
  835.     return r300_translate_texformat(format, 0, TRUE, FALSE) != ~0;
  836. }
  837.  
  838. void r300_texture_setup_format_state(struct r300_screen *screen,
  839.                                      struct r300_resource *tex,
  840.                                      enum pipe_format format,
  841.                                      unsigned level,
  842.                                      unsigned width0_override,
  843.                                      unsigned height0_override,
  844.                                      struct r300_texture_format_state *out)
  845. {
  846.     struct pipe_resource *pt = &tex->b.b;
  847.     struct r300_texture_desc *desc = &tex->tex;
  848.     boolean is_r500 = screen->caps.is_r500;
  849.     unsigned width, height, depth;
  850.     unsigned txwidth, txheight, txdepth;
  851.  
  852.     width = u_minify(width0_override, level);
  853.     height = u_minify(height0_override, level);
  854.     depth = u_minify(desc->depth0, level);
  855.  
  856.     txwidth = (width - 1) & 0x7ff;
  857.     txheight = (height - 1) & 0x7ff;
  858.     txdepth = util_logbase2(depth) & 0xf;
  859.  
  860.     /* Mask out all the fields we change. */
  861.     out->format0 = 0;
  862.     out->format1 &= ~R300_TX_FORMAT_TEX_COORD_TYPE_MASK;
  863.     out->format2 &= R500_TXFORMAT_MSB;
  864.     out->tile_config = 0;
  865.  
  866.     /* Set sampler state. */
  867.     out->format0 =
  868.         R300_TX_WIDTH(txwidth) |
  869.         R300_TX_HEIGHT(txheight) |
  870.         R300_TX_DEPTH(txdepth);
  871.  
  872.     if (desc->uses_stride_addressing) {
  873.         unsigned stride =
  874.             r300_stride_to_width(format, desc->stride_in_bytes[level]);
  875.         /* rectangles love this */
  876.         out->format0 |= R300_TX_PITCH_EN;
  877.         out->format2 = (stride - 1) & 0x1fff;
  878.     }
  879.  
  880.     if (pt->target == PIPE_TEXTURE_CUBE) {
  881.         out->format1 |= R300_TX_FORMAT_CUBIC_MAP;
  882.     }
  883.     if (pt->target == PIPE_TEXTURE_3D) {
  884.         out->format1 |= R300_TX_FORMAT_3D;
  885.     }
  886.  
  887.     /* large textures on r500 */
  888.     if (is_r500)
  889.     {
  890.         unsigned us_width = txwidth;
  891.         unsigned us_height = txheight;
  892.         unsigned us_depth = txdepth;
  893.  
  894.         if (width > 2048) {
  895.             out->format2 |= R500_TXWIDTH_BIT11;
  896.         }
  897.         if (height > 2048) {
  898.             out->format2 |= R500_TXHEIGHT_BIT11;
  899.         }
  900.  
  901.         /* The US_FORMAT register fixes an R500 TX addressing bug.
  902.          * Don't ask why it must be set like this. I don't know it either. */
  903.         if (width > 2048) {
  904.             us_width = (0x000007FF + us_width) >> 1;
  905.             us_depth |= 0x0000000D;
  906.         }
  907.         if (height > 2048) {
  908.             us_height = (0x000007FF + us_height) >> 1;
  909.             us_depth |= 0x0000000E;
  910.         }
  911.  
  912.         out->us_format0 =
  913.             R300_TX_WIDTH(us_width) |
  914.             R300_TX_HEIGHT(us_height) |
  915.             R300_TX_DEPTH(us_depth);
  916.     }
  917.  
  918.     out->tile_config = R300_TXO_MACRO_TILE(desc->macrotile[level]) |
  919.                        R300_TXO_MICRO_TILE(desc->microtile);
  920. }
  921.  
  922. static void r300_texture_setup_fb_state(struct r300_surface *surf)
  923. {
  924.     struct r300_resource *tex = r300_resource(surf->base.texture);
  925.     unsigned level = surf->base.u.tex.level;
  926.     unsigned stride =
  927.       r300_stride_to_width(surf->base.format, tex->tex.stride_in_bytes[level]);
  928.  
  929.     /* Set framebuffer state. */
  930.     if (util_format_is_depth_or_stencil(surf->base.format)) {
  931.         surf->pitch =
  932.                 stride |
  933.                 R300_DEPTHMACROTILE(tex->tex.macrotile[level]) |
  934.                 R300_DEPTHMICROTILE(tex->tex.microtile);
  935.         surf->format = r300_translate_zsformat(surf->base.format);
  936.         surf->pitch_zmask = tex->tex.zmask_stride_in_pixels[level];
  937.         surf->pitch_hiz = tex->tex.hiz_stride_in_pixels[level];
  938.     } else {
  939.         surf->pitch =
  940.                 stride |
  941.                 r300_translate_colorformat(surf->base.format) |
  942.                 R300_COLOR_TILE(tex->tex.macrotile[level]) |
  943.                 R300_COLOR_MICROTILE(tex->tex.microtile);
  944.         surf->format = r300_translate_out_fmt(surf->base.format);
  945.         surf->colormask_swizzle =
  946.             r300_translate_colormask_swizzle(surf->base.format);
  947.         surf->pitch_cmask = tex->tex.cmask_stride_in_pixels;
  948.     }
  949. }
  950.  
  951. static void r300_texture_destroy(struct pipe_screen *screen,
  952.                                  struct pipe_resource* texture)
  953. {
  954.     struct r300_screen *rscreen = r300_screen(screen);
  955.     struct r300_resource* tex = (struct r300_resource*)texture;
  956.  
  957.     if (tex->tex.cmask_dwords) {
  958.         pipe_mutex_lock(rscreen->cmask_mutex);
  959.         if (texture == rscreen->cmask_resource) {
  960.             rscreen->cmask_resource = NULL;
  961.         }
  962.         pipe_mutex_unlock(rscreen->cmask_mutex);
  963.     }
  964.     pb_reference(&tex->buf, NULL);
  965.     FREE(tex);
  966. }
  967.  
  968. boolean r300_resource_get_handle(struct pipe_screen* screen,
  969.                                  struct pipe_resource *texture,
  970.                                  struct winsys_handle *whandle)
  971. {
  972.     struct radeon_winsys *rws = r300_screen(screen)->rws;
  973.     struct r300_resource* tex = (struct r300_resource*)texture;
  974.  
  975.     if (!tex) {
  976.         return FALSE;
  977.     }
  978.  
  979.     return rws->buffer_get_handle(tex->buf,
  980.                                   tex->tex.stride_in_bytes[0], whandle);
  981. }
  982.  
  983. static const struct u_resource_vtbl r300_texture_vtbl =
  984. {
  985.     NULL,                           /* get_handle */
  986.     r300_texture_destroy,           /* resource_destroy */
  987.     r300_texture_transfer_map,      /* transfer_map */
  988.     NULL,                           /* transfer_flush_region */
  989.     r300_texture_transfer_unmap,    /* transfer_unmap */
  990.     NULL /* transfer_inline_write */
  991. };
  992.  
  993. /* The common texture constructor. */
  994. static struct r300_resource*
  995. r300_texture_create_object(struct r300_screen *rscreen,
  996.                            const struct pipe_resource *base,
  997.                            enum radeon_bo_layout microtile,
  998.                            enum radeon_bo_layout macrotile,
  999.                            unsigned stride_in_bytes_override,
  1000.                            struct pb_buffer *buffer)
  1001. {
  1002.     struct radeon_winsys *rws = rscreen->rws;
  1003.     struct r300_resource *tex = NULL;
  1004.  
  1005.     tex = CALLOC_STRUCT(r300_resource);
  1006.     if (!tex) {
  1007.         goto fail;
  1008.     }
  1009.  
  1010.     pipe_reference_init(&tex->b.b.reference, 1);
  1011.     tex->b.b.screen = &rscreen->screen;
  1012.     tex->b.b.usage = base->usage;
  1013.     tex->b.b.bind = base->bind;
  1014.     tex->b.b.flags = base->flags;
  1015.     tex->b.vtbl = &r300_texture_vtbl;
  1016.     tex->tex.microtile = microtile;
  1017.     tex->tex.macrotile[0] = macrotile;
  1018.     tex->tex.stride_in_bytes_override = stride_in_bytes_override;
  1019.     tex->domain = (base->flags & R300_RESOURCE_FLAG_TRANSFER ||
  1020.                    base->usage == PIPE_USAGE_STAGING) ? RADEON_DOMAIN_GTT :
  1021.                   base->nr_samples > 1 ? RADEON_DOMAIN_VRAM :
  1022.                                          RADEON_DOMAIN_VRAM | RADEON_DOMAIN_GTT;
  1023.     tex->buf = buffer;
  1024.  
  1025.     r300_texture_desc_init(rscreen, tex, base);
  1026.  
  1027.     /* Figure out the ideal placement for the texture.. */
  1028.     if (tex->domain & RADEON_DOMAIN_VRAM &&
  1029.         tex->tex.size_in_bytes >= rscreen->info.vram_size) {
  1030.         tex->domain &= ~RADEON_DOMAIN_VRAM;
  1031.         tex->domain |= RADEON_DOMAIN_GTT;
  1032.     }
  1033.     if (tex->domain & RADEON_DOMAIN_GTT &&
  1034.         tex->tex.size_in_bytes >= rscreen->info.gart_size) {
  1035.         tex->domain &= ~RADEON_DOMAIN_GTT;
  1036.     }
  1037.     /* Just fail if the texture is too large. */
  1038.     if (!tex->domain) {
  1039.         goto fail;
  1040.     }
  1041.  
  1042.     /* Create the backing buffer if needed. */
  1043.     if (!tex->buf) {
  1044.         tex->buf = rws->buffer_create(rws, tex->tex.size_in_bytes, 2048, TRUE,
  1045.                                       tex->domain);
  1046.  
  1047.         if (!tex->buf) {
  1048.             goto fail;
  1049.         }
  1050.     }
  1051.  
  1052.     if (SCREEN_DBG_ON(rscreen, DBG_MSAA) && base->nr_samples > 1) {
  1053.         fprintf(stderr, "r300: %ix MSAA %s buffer created\n",
  1054.                 base->nr_samples,
  1055.                 util_format_is_depth_or_stencil(base->format) ? "depth" : "color");
  1056.     }
  1057.  
  1058.     tex->cs_buf = rws->buffer_get_cs_handle(tex->buf);
  1059.  
  1060.     rws->buffer_set_tiling(tex->buf, NULL,
  1061.             tex->tex.microtile, tex->tex.macrotile[0],
  1062.             0, 0, 0, 0, 0,
  1063.             tex->tex.stride_in_bytes[0]);
  1064.  
  1065.     return tex;
  1066.  
  1067. fail:
  1068.     FREE(tex);
  1069.     if (buffer)
  1070.         pb_reference(&buffer, NULL);
  1071.     return NULL;
  1072. }
  1073.  
  1074. /* Create a new texture. */
  1075. struct pipe_resource *r300_texture_create(struct pipe_screen *screen,
  1076.                                           const struct pipe_resource *base)
  1077. {
  1078.     struct r300_screen *rscreen = r300_screen(screen);
  1079.     enum radeon_bo_layout microtile, macrotile;
  1080.  
  1081.     if ((base->flags & R300_RESOURCE_FLAG_TRANSFER) ||
  1082.         (base->bind & PIPE_BIND_SCANOUT)) {
  1083.         microtile = RADEON_LAYOUT_LINEAR;
  1084.         macrotile = RADEON_LAYOUT_LINEAR;
  1085.     } else {
  1086.         /* This will make the texture_create_function select the layout. */
  1087.         microtile = RADEON_LAYOUT_UNKNOWN;
  1088.         macrotile = RADEON_LAYOUT_UNKNOWN;
  1089.     }
  1090.  
  1091.     return (struct pipe_resource*)
  1092.            r300_texture_create_object(rscreen, base, microtile, macrotile,
  1093.                                       0, NULL);
  1094. }
  1095.  
  1096. struct pipe_resource *r300_texture_from_handle(struct pipe_screen *screen,
  1097.                                                const struct pipe_resource *base,
  1098.                                                struct winsys_handle *whandle)
  1099. {
  1100.     struct r300_screen *rscreen = r300_screen(screen);
  1101.     struct radeon_winsys *rws = rscreen->rws;
  1102.     struct pb_buffer *buffer;
  1103.     enum radeon_bo_layout microtile, macrotile;
  1104.     unsigned stride;
  1105.  
  1106.     /* Support only 2D textures without mipmaps */
  1107.     if ((base->target != PIPE_TEXTURE_2D &&
  1108.           base->target != PIPE_TEXTURE_RECT) ||
  1109.         base->depth0 != 1 ||
  1110.         base->last_level != 0) {
  1111.         return NULL;
  1112.     }
  1113.  
  1114.     buffer = rws->buffer_from_handle(rws, whandle, &stride);
  1115.     if (!buffer)
  1116.         return NULL;
  1117.  
  1118.     rws->buffer_get_tiling(buffer, &microtile, &macrotile, NULL, NULL, NULL, NULL, NULL);
  1119.  
  1120.     /* Enforce a microtiled zbuffer. */
  1121.     if (util_format_is_depth_or_stencil(base->format) &&
  1122.         microtile == RADEON_LAYOUT_LINEAR) {
  1123.         switch (util_format_get_blocksize(base->format)) {
  1124.             case 4:
  1125.                 microtile = RADEON_LAYOUT_TILED;
  1126.                 break;
  1127.  
  1128.             case 2:
  1129.                 microtile = RADEON_LAYOUT_SQUARETILED;
  1130.                 break;
  1131.         }
  1132.     }
  1133.  
  1134.     return (struct pipe_resource*)
  1135.            r300_texture_create_object(rscreen, base, microtile, macrotile,
  1136.                                       stride, buffer);
  1137. }
  1138.  
  1139. /* Not required to implement u_resource_vtbl, consider moving to another file:
  1140.  */
  1141. struct pipe_surface* r300_create_surface_custom(struct pipe_context * ctx,
  1142.                                          struct pipe_resource* texture,
  1143.                                          const struct pipe_surface *surf_tmpl,
  1144.                                          unsigned width0_override,
  1145.                                          unsigned height0_override)
  1146. {
  1147.     struct r300_resource* tex = r300_resource(texture);
  1148.     struct r300_surface* surface = CALLOC_STRUCT(r300_surface);
  1149.     unsigned level = surf_tmpl->u.tex.level;
  1150.  
  1151.     assert(surf_tmpl->u.tex.first_layer == surf_tmpl->u.tex.last_layer);
  1152.  
  1153.     if (surface) {
  1154.         uint32_t offset, tile_height;
  1155.  
  1156.         pipe_reference_init(&surface->base.reference, 1);
  1157.         pipe_resource_reference(&surface->base.texture, texture);
  1158.         surface->base.context = ctx;
  1159.         surface->base.format = surf_tmpl->format;
  1160.         surface->base.width = u_minify(width0_override, level);
  1161.         surface->base.height = u_minify(height0_override, level);
  1162.         surface->base.u.tex.level = level;
  1163.         surface->base.u.tex.first_layer = surf_tmpl->u.tex.first_layer;
  1164.         surface->base.u.tex.last_layer = surf_tmpl->u.tex.last_layer;
  1165.  
  1166.         surface->buf = tex->buf;
  1167.         surface->cs_buf = tex->cs_buf;
  1168.  
  1169.         /* Prefer VRAM if there are multiple domains to choose from. */
  1170.         surface->domain = tex->domain;
  1171.         if (surface->domain & RADEON_DOMAIN_VRAM)
  1172.             surface->domain &= ~RADEON_DOMAIN_GTT;
  1173.  
  1174.         surface->offset = r300_texture_get_offset(tex, level,
  1175.                                                   surf_tmpl->u.tex.first_layer);
  1176.         r300_texture_setup_fb_state(surface);
  1177.  
  1178.         /* Parameters for the CBZB clear. */
  1179.         surface->cbzb_allowed = tex->tex.cbzb_allowed[level];
  1180.         surface->cbzb_width = align(surface->base.width, 64);
  1181.  
  1182.         /* Height must be aligned to the size of a tile. */
  1183.         tile_height = r300_get_pixel_alignment(surface->base.format,
  1184.                                                tex->b.b.nr_samples,
  1185.                                                tex->tex.microtile,
  1186.                                                tex->tex.macrotile[level],
  1187.                                                DIM_HEIGHT, 0);
  1188.  
  1189.         surface->cbzb_height = align((surface->base.height + 1) / 2,
  1190.                                      tile_height);
  1191.  
  1192.         /* Offset must be aligned to 2K and must point at the beginning
  1193.          * of a scanline. */
  1194.         offset = surface->offset +
  1195.                  tex->tex.stride_in_bytes[level] * surface->cbzb_height;
  1196.         surface->cbzb_midpoint_offset = offset & ~2047;
  1197.  
  1198.         surface->cbzb_pitch = surface->pitch & 0x1ffffc;
  1199.  
  1200.         if (util_format_get_blocksizebits(surface->base.format) == 32)
  1201.             surface->cbzb_format = R300_DEPTHFORMAT_24BIT_INT_Z_8BIT_STENCIL;
  1202.         else
  1203.             surface->cbzb_format = R300_DEPTHFORMAT_16BIT_INT_Z;
  1204.  
  1205.         DBG(r300_context(ctx), DBG_CBZB,
  1206.             "CBZB Allowed: %s, Dim: %ix%i, Misalignment: %i, Micro: %s, Macro: %s\n",
  1207.             surface->cbzb_allowed ? "YES" : " NO",
  1208.             surface->cbzb_width, surface->cbzb_height,
  1209.             offset & 2047,
  1210.             tex->tex.microtile ? "YES" : " NO",
  1211.             tex->tex.macrotile[level] ? "YES" : " NO");
  1212.     }
  1213.  
  1214.     return &surface->base;
  1215. }
  1216.  
  1217. struct pipe_surface* r300_create_surface(struct pipe_context * ctx,
  1218.                                          struct pipe_resource* texture,
  1219.                                          const struct pipe_surface *surf_tmpl)
  1220. {
  1221.     return r300_create_surface_custom(ctx, texture, surf_tmpl,
  1222.                                       texture->width0,
  1223.                                       texture->height0);
  1224. }
  1225.  
  1226. /* Not required to implement u_resource_vtbl, consider moving to another file:
  1227.  */
  1228. void r300_surface_destroy(struct pipe_context *ctx, struct pipe_surface* s)
  1229. {
  1230.     pipe_resource_reference(&s->texture, NULL);
  1231.     FREE(s);
  1232. }
  1233.