Subversion Repositories Kolibri OS

Rev

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