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