r300g: fix HiZ memory size computation and deciding when to use HiZ
[platform/upstream/mesa.git] / src / gallium / drivers / r300 / r300_texture.c
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 #include "r300_winsys.h"
33
34 #include "util/u_format.h"
35 #include "util/u_format_s3tc.h"
36 #include "util/u_math.h"
37 #include "util/u_memory.h"
38 #include "util/u_mm.h"
39
40 #include "pipe/p_screen.h"
41
42 unsigned r300_get_swizzle_combined(const unsigned char *swizzle_format,
43                                    const unsigned char *swizzle_view,
44                                    boolean dxtc_swizzle)
45 {
46     unsigned i;
47     unsigned char swizzle[4];
48     unsigned result = 0;
49     const uint32_t swizzle_shift[4] = {
50         R300_TX_FORMAT_R_SHIFT,
51         R300_TX_FORMAT_G_SHIFT,
52         R300_TX_FORMAT_B_SHIFT,
53         R300_TX_FORMAT_A_SHIFT
54     };
55     uint32_t swizzle_bit[4] = {
56         dxtc_swizzle ? R300_TX_FORMAT_Z : R300_TX_FORMAT_X,
57         R300_TX_FORMAT_Y,
58         dxtc_swizzle ? R300_TX_FORMAT_X : R300_TX_FORMAT_Z,
59         R300_TX_FORMAT_W
60     };
61
62     if (swizzle_view) {
63         /* Combine two sets of swizzles. */
64         for (i = 0; i < 4; i++) {
65             swizzle[i] = swizzle_view[i] <= UTIL_FORMAT_SWIZZLE_W ?
66                          swizzle_format[swizzle_view[i]] : swizzle_view[i];
67         }
68     } else {
69         memcpy(swizzle, swizzle_format, 4);
70     }
71
72     /* Get swizzle. */
73     for (i = 0; i < 4; i++) {
74         switch (swizzle[i]) {
75             case UTIL_FORMAT_SWIZZLE_Y:
76                 result |= swizzle_bit[1] << swizzle_shift[i];
77                 break;
78             case UTIL_FORMAT_SWIZZLE_Z:
79                 result |= swizzle_bit[2] << swizzle_shift[i];
80                 break;
81             case UTIL_FORMAT_SWIZZLE_W:
82                 result |= swizzle_bit[3] << swizzle_shift[i];
83                 break;
84             case UTIL_FORMAT_SWIZZLE_0:
85                 result |= R300_TX_FORMAT_ZERO << swizzle_shift[i];
86                 break;
87             case UTIL_FORMAT_SWIZZLE_1:
88                 result |= R300_TX_FORMAT_ONE << swizzle_shift[i];
89                 break;
90             default: /* UTIL_FORMAT_SWIZZLE_X */
91                 result |= swizzle_bit[0] << swizzle_shift[i];
92         }
93     }
94     return result;
95 }
96
97 /* Translate a pipe_format into a useful texture format for sampling.
98  *
99  * Some special formats are translated directly using R300_EASY_TX_FORMAT,
100  * but the majority of them is translated in a generic way, automatically
101  * supporting all the formats hw can support.
102  *
103  * R300_EASY_TX_FORMAT swizzles the texture.
104  * Note the signature of R300_EASY_TX_FORMAT:
105  *   R300_EASY_TX_FORMAT(B, G, R, A, FORMAT);
106  *
107  * The FORMAT specifies how the texture sampler will treat the texture, and
108  * makes available X, Y, Z, W, ZERO, and ONE for swizzling. */
109 uint32_t r300_translate_texformat(enum pipe_format format,
110                                   const unsigned char *swizzle_view,
111                                   boolean is_r500,
112                                   boolean dxtc_swizzle)
113 {
114     uint32_t result = 0;
115     const struct util_format_description *desc;
116     unsigned i;
117     boolean uniform = TRUE;
118     const uint32_t sign_bit[4] = {
119         R300_TX_FORMAT_SIGNED_X,
120         R300_TX_FORMAT_SIGNED_Y,
121         R300_TX_FORMAT_SIGNED_Z,
122         R300_TX_FORMAT_SIGNED_W,
123     };
124
125     desc = util_format_description(format);
126
127     /* Colorspace (return non-RGB formats directly). */
128     switch (desc->colorspace) {
129         /* Depth stencil formats.
130          * Swizzles are added in r300_merge_textures_and_samplers. */
131         case UTIL_FORMAT_COLORSPACE_ZS:
132             switch (format) {
133                 case PIPE_FORMAT_Z16_UNORM:
134                     return R300_TX_FORMAT_X16;
135                 case PIPE_FORMAT_X8Z24_UNORM:
136                 case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
137                     if (is_r500)
138                         return R500_TX_FORMAT_Y8X24;
139                     else
140                         return R300_TX_FORMAT_Y16X16;
141                 default:
142                     return ~0; /* Unsupported. */
143             }
144
145         /* YUV formats. */
146         case UTIL_FORMAT_COLORSPACE_YUV:
147             result |= R300_TX_FORMAT_YUV_TO_RGB;
148
149             switch (format) {
150                 case PIPE_FORMAT_UYVY:
151                     return R300_EASY_TX_FORMAT(X, Y, Z, ONE, YVYU422) | result;
152                 case PIPE_FORMAT_YUYV:
153                     return R300_EASY_TX_FORMAT(X, Y, Z, ONE, VYUY422) | result;
154                 default:
155                     return ~0; /* Unsupported/unknown. */
156             }
157
158         /* Add gamma correction. */
159         case UTIL_FORMAT_COLORSPACE_SRGB:
160             result |= R300_TX_FORMAT_GAMMA;
161             break;
162
163         default:
164             switch (format) {
165                 /* Same as YUV but without the YUR->RGB conversion. */
166                 case PIPE_FORMAT_R8G8_B8G8_UNORM:
167                     return R300_EASY_TX_FORMAT(X, Y, Z, ONE, YVYU422) | result;
168                 case PIPE_FORMAT_G8R8_G8B8_UNORM:
169                     return R300_EASY_TX_FORMAT(X, Y, Z, ONE, VYUY422) | result;
170                 default:;
171             }
172     }
173
174     result |= r300_get_swizzle_combined(desc->swizzle, swizzle_view,
175                     util_format_is_compressed(format) && dxtc_swizzle);
176
177     /* S3TC formats. */
178     if (desc->layout == UTIL_FORMAT_LAYOUT_S3TC) {
179         if (!util_format_s3tc_enabled) {
180             return ~0; /* Unsupported. */
181         }
182
183         switch (format) {
184             case PIPE_FORMAT_DXT1_RGB:
185             case PIPE_FORMAT_DXT1_RGBA:
186             case PIPE_FORMAT_DXT1_SRGB:
187             case PIPE_FORMAT_DXT1_SRGBA:
188                 return R300_TX_FORMAT_DXT1 | result;
189             case PIPE_FORMAT_DXT3_RGBA:
190             case PIPE_FORMAT_DXT3_SRGBA:
191                 return R300_TX_FORMAT_DXT3 | result;
192             case PIPE_FORMAT_DXT5_RGBA:
193             case PIPE_FORMAT_DXT5_SRGBA:
194                 return R300_TX_FORMAT_DXT5 | result;
195             default:
196                 return ~0; /* Unsupported/unknown. */
197         }
198     }
199
200     /* Add sign. */
201     for (i = 0; i < desc->nr_channels; i++) {
202         if (desc->channel[i].type == UTIL_FORMAT_TYPE_SIGNED) {
203             result |= sign_bit[i];
204         }
205     }
206
207     /* This is truly a special format.
208      * It stores R8G8 and B is computed using sqrt(1 - R^2 - G^2)
209      * in the sampler unit. Also known as D3DFMT_CxV8U8. */
210     if (format == PIPE_FORMAT_R8G8Bx_SNORM) {
211         return R300_TX_FORMAT_CxV8U8 | result;
212     }
213
214     /* RGTC formats. */
215     if (desc->layout == UTIL_FORMAT_LAYOUT_RGTC) {
216         switch (format) {
217             case PIPE_FORMAT_RGTC1_SNORM:
218                 result |= sign_bit[0];
219             case PIPE_FORMAT_RGTC1_UNORM:
220                 result &= ~(0xfff << 9); /* mask off swizzle */
221                 result |= R300_TX_FORMAT_Y << R300_TX_FORMAT_R_SHIFT;
222                 return R500_TX_FORMAT_ATI1N | result;
223             case PIPE_FORMAT_RGTC2_SNORM:
224                 result |= sign_bit[0] | sign_bit[1];
225             case PIPE_FORMAT_RGTC2_UNORM:
226                 result &= ~(0xfff << 9); /* mask off swizzle */
227                 result |= R300_TX_FORMAT_Y << R300_TX_FORMAT_R_SHIFT |
228                           R300_TX_FORMAT_X << R300_TX_FORMAT_G_SHIFT;
229                 return R400_TX_FORMAT_ATI2N | result;
230             default:
231                 return ~0; /* Unsupported/unknown. */
232         }
233     }
234
235     /* See whether the components are of the same size. */
236     for (i = 1; i < desc->nr_channels; i++) {
237         uniform = uniform && desc->channel[0].size == desc->channel[i].size;
238     }
239
240     /* Non-uniform formats. */
241     if (!uniform) {
242         switch (desc->nr_channels) {
243             case 3:
244                 if (desc->channel[0].size == 5 &&
245                     desc->channel[1].size == 6 &&
246                     desc->channel[2].size == 5) {
247                     return R300_TX_FORMAT_Z5Y6X5 | result;
248                 }
249                 if (desc->channel[0].size == 5 &&
250                     desc->channel[1].size == 5 &&
251                     desc->channel[2].size == 6) {
252                     return R300_TX_FORMAT_Z6Y5X5 | result;
253                 }
254                 if (desc->channel[0].size == 2 &&
255                     desc->channel[1].size == 3 &&
256                     desc->channel[2].size == 3) {
257                     return R300_TX_FORMAT_Z3Y3X2 | result;
258                 }
259                 return ~0; /* Unsupported/unknown. */
260
261             case 4:
262                 if (desc->channel[0].size == 5 &&
263                     desc->channel[1].size == 5 &&
264                     desc->channel[2].size == 5 &&
265                     desc->channel[3].size == 1) {
266                     return R300_TX_FORMAT_W1Z5Y5X5 | result;
267                 }
268                 if (desc->channel[0].size == 10 &&
269                     desc->channel[1].size == 10 &&
270                     desc->channel[2].size == 10 &&
271                     desc->channel[3].size == 2) {
272                     return R300_TX_FORMAT_W2Z10Y10X10 | result;
273                 }
274         }
275         return ~0; /* Unsupported/unknown. */
276     }
277
278     /* Find the first non-VOID channel. */
279     for (i = 0; i < 4; i++) {
280         if (desc->channel[i].type != UTIL_FORMAT_TYPE_VOID) {
281             break;
282         }
283     }
284
285     if (i == 4)
286         return ~0; /* Unsupported/unknown. */
287
288     /* And finally, uniform formats. */
289     switch (desc->channel[i].type) {
290         case UTIL_FORMAT_TYPE_UNSIGNED:
291         case UTIL_FORMAT_TYPE_SIGNED:
292             if (!desc->channel[i].normalized &&
293                 desc->colorspace != UTIL_FORMAT_COLORSPACE_SRGB) {
294                 return ~0;
295             }
296
297             switch (desc->channel[i].size) {
298                 case 4:
299                     switch (desc->nr_channels) {
300                         case 2:
301                             return R300_TX_FORMAT_Y4X4 | result;
302                         case 4:
303                             return R300_TX_FORMAT_W4Z4Y4X4 | result;
304                     }
305                     return ~0;
306
307                 case 8:
308                     switch (desc->nr_channels) {
309                         case 1:
310                             return R300_TX_FORMAT_X8 | result;
311                         case 2:
312                             return R300_TX_FORMAT_Y8X8 | result;
313                         case 4:
314                             return R300_TX_FORMAT_W8Z8Y8X8 | result;
315                     }
316                     return ~0;
317
318                 case 16:
319                     switch (desc->nr_channels) {
320                         case 1:
321                             return R300_TX_FORMAT_X16 | result;
322                         case 2:
323                             return R300_TX_FORMAT_Y16X16 | result;
324                         case 4:
325                             return R300_TX_FORMAT_W16Z16Y16X16 | result;
326                     }
327             }
328             return ~0;
329
330         case UTIL_FORMAT_TYPE_FLOAT:
331             switch (desc->channel[i].size) {
332                 case 16:
333                     switch (desc->nr_channels) {
334                         case 1:
335                             return R300_TX_FORMAT_16F | result;
336                         case 2:
337                             return R300_TX_FORMAT_16F_16F | result;
338                         case 4:
339                             return R300_TX_FORMAT_16F_16F_16F_16F | result;
340                     }
341                     return ~0;
342
343                 case 32:
344                     switch (desc->nr_channels) {
345                         case 1:
346                             return R300_TX_FORMAT_32F | result;
347                         case 2:
348                             return R300_TX_FORMAT_32F_32F | result;
349                         case 4:
350                             return R300_TX_FORMAT_32F_32F_32F_32F | result;
351                     }
352             }
353     }
354
355     return ~0; /* Unsupported/unknown. */
356 }
357
358 uint32_t r500_tx_format_msb_bit(enum pipe_format format)
359 {
360     switch (format) {
361         case PIPE_FORMAT_RGTC1_UNORM:
362         case PIPE_FORMAT_RGTC1_SNORM:
363         case PIPE_FORMAT_X8Z24_UNORM:
364         case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
365             return R500_TXFORMAT_MSB;
366         default:
367             return 0;
368     }
369 }
370
371 /* Buffer formats. */
372
373 /* Colorbuffer formats. This is the unswizzled format of the RB3D block's
374  * output. For the swizzling of the targets, check the shader's format. */
375 static uint32_t r300_translate_colorformat(enum pipe_format format)
376 {
377     switch (format) {
378         /* 8-bit buffers. */
379         case PIPE_FORMAT_A8_UNORM:
380         /*case PIPE_FORMAT_A8_SNORM:*/
381         case PIPE_FORMAT_I8_UNORM:
382         /*case PIPE_FORMAT_I8_SNORM:*/
383         case PIPE_FORMAT_L8_UNORM:
384         /*case PIPE_FORMAT_L8_SNORM:*/
385         case PIPE_FORMAT_R8_UNORM:
386         case PIPE_FORMAT_R8_SNORM:
387             return R300_COLOR_FORMAT_I8;
388
389         /* 16-bit buffers. */
390         case PIPE_FORMAT_L8A8_UNORM:
391         /*case PIPE_FORMAT_L8A8_SNORM:*/
392         case PIPE_FORMAT_R8G8_UNORM:
393         case PIPE_FORMAT_R8G8_SNORM:
394             return R300_COLOR_FORMAT_UV88;
395
396         case PIPE_FORMAT_B5G6R5_UNORM:
397             return R300_COLOR_FORMAT_RGB565;
398
399         case PIPE_FORMAT_B5G5R5A1_UNORM:
400         case PIPE_FORMAT_B5G5R5X1_UNORM:
401             return R300_COLOR_FORMAT_ARGB1555;
402
403         case PIPE_FORMAT_B4G4R4A4_UNORM:
404         case PIPE_FORMAT_B4G4R4X4_UNORM:
405             return R300_COLOR_FORMAT_ARGB4444;
406
407         /* 32-bit buffers. */
408         case PIPE_FORMAT_B8G8R8A8_UNORM:
409         /*case PIPE_FORMAT_B8G8R8A8_SNORM:*/
410         case PIPE_FORMAT_B8G8R8X8_UNORM:
411         /*case PIPE_FORMAT_B8G8R8X8_SNORM:*/
412         case PIPE_FORMAT_A8R8G8B8_UNORM:
413         /*case PIPE_FORMAT_A8R8G8B8_SNORM:*/
414         case PIPE_FORMAT_X8R8G8B8_UNORM:
415         /*case PIPE_FORMAT_X8R8G8B8_SNORM:*/
416         case PIPE_FORMAT_A8B8G8R8_UNORM:
417         /*case PIPE_FORMAT_A8B8G8R8_SNORM:*/
418         case PIPE_FORMAT_R8G8B8A8_UNORM:
419         case PIPE_FORMAT_R8G8B8A8_SNORM:
420         case PIPE_FORMAT_X8B8G8R8_UNORM:
421         /*case PIPE_FORMAT_X8B8G8R8_SNORM:*/
422         case PIPE_FORMAT_R8G8B8X8_UNORM:
423         /*case PIPE_FORMAT_R8G8B8X8_SNORM:*/
424         case PIPE_FORMAT_R8SG8SB8UX8U_NORM:
425             return R300_COLOR_FORMAT_ARGB8888;
426
427         case PIPE_FORMAT_R10G10B10A2_UNORM:
428         case PIPE_FORMAT_R10G10B10X2_SNORM:
429         case PIPE_FORMAT_B10G10R10A2_UNORM:
430         case PIPE_FORMAT_R10SG10SB10SA2U_NORM:
431             return R500_COLOR_FORMAT_ARGB2101010;  /* R5xx-only? */
432
433         /* 64-bit buffers. */
434         case PIPE_FORMAT_R16G16B16A16_UNORM:
435         case PIPE_FORMAT_R16G16B16A16_SNORM:
436         case PIPE_FORMAT_R16G16B16A16_FLOAT:
437             return R300_COLOR_FORMAT_ARGB16161616;
438
439         /* 128-bit buffers. */
440         case PIPE_FORMAT_R32G32B32A32_FLOAT:
441             return R300_COLOR_FORMAT_ARGB32323232;
442
443         /* YUV buffers. */
444         case PIPE_FORMAT_UYVY:
445             return R300_COLOR_FORMAT_YVYU;
446         case PIPE_FORMAT_YUYV:
447             return R300_COLOR_FORMAT_VYUY;
448         default:
449             return ~0; /* Unsupported. */
450     }
451 }
452
453 /* Depthbuffer and stencilbuffer. Thankfully, we only support two flavors. */
454 static uint32_t r300_translate_zsformat(enum pipe_format format)
455 {
456     switch (format) {
457         /* 16-bit depth, no stencil */
458         case PIPE_FORMAT_Z16_UNORM:
459             return R300_DEPTHFORMAT_16BIT_INT_Z;
460         /* 24-bit depth, ignored stencil */
461         case PIPE_FORMAT_X8Z24_UNORM:
462         /* 24-bit depth, 8-bit stencil */
463         case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
464             return R300_DEPTHFORMAT_24BIT_INT_Z_8BIT_STENCIL;
465         default:
466             return ~0; /* Unsupported. */
467     }
468 }
469
470 /* Shader output formats. This is essentially the swizzle from the shader
471  * to the RB3D block.
472  *
473  * Note that formats are stored from C3 to C0. */
474 static uint32_t r300_translate_out_fmt(enum pipe_format format)
475 {
476     uint32_t modifier = 0;
477     unsigned i;
478     const struct util_format_description *desc;
479     static const uint32_t sign_bit[4] = {
480         R300_OUT_SIGN(0x1),
481         R300_OUT_SIGN(0x2),
482         R300_OUT_SIGN(0x4),
483         R300_OUT_SIGN(0x8),
484     };
485
486     desc = util_format_description(format);
487
488     /* Find the first non-VOID channel. */
489     for (i = 0; i < 4; i++) {
490         if (desc->channel[i].type != UTIL_FORMAT_TYPE_VOID) {
491             break;
492         }
493     }
494
495     if (i == 4)
496         return ~0; /* Unsupported/unknown. */
497
498     /* Specifies how the shader output is written to the fog unit. */
499     if (desc->channel[i].type == UTIL_FORMAT_TYPE_FLOAT) {
500         if (desc->channel[i].size == 32) {
501             modifier |= R300_US_OUT_FMT_C4_32_FP;
502         } else {
503             modifier |= R300_US_OUT_FMT_C4_16_FP;
504         }
505     } else {
506         if (desc->channel[i].size == 16) {
507             modifier |= R300_US_OUT_FMT_C4_16;
508         } else if (desc->channel[i].size == 10) {
509             modifier |= R300_US_OUT_FMT_C4_10;
510         } else {
511             /* C4_8 seems to be used for the formats whose pixel size
512              * is <= 32 bits. */
513             modifier |= R300_US_OUT_FMT_C4_8;
514         }
515     }
516
517     /* Add sign. */
518     for (i = 0; i < 4; i++)
519         if (desc->channel[i].type == UTIL_FORMAT_TYPE_SIGNED) {
520             modifier |= sign_bit[i];
521         }
522
523     /* Add swizzles and return. */
524     switch (format) {
525         /* 8-bit outputs, one channel.
526          * COLORFORMAT_I8 stores the C2 component. */
527         case PIPE_FORMAT_A8_UNORM:
528         /*case PIPE_FORMAT_A8_SNORM:*/
529             return modifier | R300_C2_SEL_A;
530         case PIPE_FORMAT_I8_UNORM:
531         /*case PIPE_FORMAT_I8_SNORM:*/
532         case PIPE_FORMAT_L8_UNORM:
533         /*case PIPE_FORMAT_L8_SNORM:*/
534         case PIPE_FORMAT_R8_UNORM:
535         case PIPE_FORMAT_R8_SNORM:
536             return modifier | R300_C2_SEL_R;
537
538         /* 16-bit outputs, two channels.
539          * COLORFORMAT_UV88 stores C2 and C0. */
540         case PIPE_FORMAT_L8A8_UNORM:
541         /*case PIPE_FORMAT_L8A8_SNORM:*/
542             return modifier | R300_C0_SEL_A | R300_C2_SEL_R;
543         case PIPE_FORMAT_R8G8_UNORM:
544         case PIPE_FORMAT_R8G8_SNORM:
545             return modifier | R300_C0_SEL_G | R300_C2_SEL_R;
546
547         /* BGRA outputs. */
548         case PIPE_FORMAT_B5G6R5_UNORM:
549         case PIPE_FORMAT_B5G5R5A1_UNORM:
550         case PIPE_FORMAT_B5G5R5X1_UNORM:
551         case PIPE_FORMAT_B4G4R4A4_UNORM:
552         case PIPE_FORMAT_B4G4R4X4_UNORM:
553         case PIPE_FORMAT_B8G8R8A8_UNORM:
554         /*case PIPE_FORMAT_B8G8R8A8_SNORM:*/
555         case PIPE_FORMAT_B8G8R8X8_UNORM:
556         /*case PIPE_FORMAT_B8G8R8X8_SNORM:*/
557         case PIPE_FORMAT_B10G10R10A2_UNORM:
558             return modifier |
559                 R300_C0_SEL_B | R300_C1_SEL_G |
560                 R300_C2_SEL_R | R300_C3_SEL_A;
561
562         /* ARGB outputs. */
563         case PIPE_FORMAT_A8R8G8B8_UNORM:
564         /*case PIPE_FORMAT_A8R8G8B8_SNORM:*/
565         case PIPE_FORMAT_X8R8G8B8_UNORM:
566         /*case PIPE_FORMAT_X8R8G8B8_SNORM:*/
567             return modifier |
568                 R300_C0_SEL_A | R300_C1_SEL_R |
569                 R300_C2_SEL_G | R300_C3_SEL_B;
570
571         /* ABGR outputs. */
572         case PIPE_FORMAT_A8B8G8R8_UNORM:
573         /*case PIPE_FORMAT_A8B8G8R8_SNORM:*/
574         case PIPE_FORMAT_X8B8G8R8_UNORM:
575         /*case PIPE_FORMAT_X8B8G8R8_SNORM:*/
576             return modifier |
577                 R300_C0_SEL_A | R300_C1_SEL_B |
578                 R300_C2_SEL_G | R300_C3_SEL_R;
579
580         /* RGBA outputs. */
581         case PIPE_FORMAT_R8G8B8X8_UNORM:
582         /*case PIPE_FORMAT_R8G8B8X8_SNORM:*/
583         case PIPE_FORMAT_R8G8B8A8_UNORM:
584         case PIPE_FORMAT_R8G8B8A8_SNORM:
585         case PIPE_FORMAT_R8SG8SB8UX8U_NORM:
586         case PIPE_FORMAT_R10G10B10A2_UNORM:
587         case PIPE_FORMAT_R10G10B10X2_SNORM:
588         case PIPE_FORMAT_R10SG10SB10SA2U_NORM:
589         case PIPE_FORMAT_R16G16B16A16_UNORM:
590         case PIPE_FORMAT_R16G16B16A16_SNORM:
591         case PIPE_FORMAT_R16G16B16A16_FLOAT:
592         case PIPE_FORMAT_R32G32B32A32_FLOAT:
593             return modifier |
594                 R300_C0_SEL_R | R300_C1_SEL_G |
595                 R300_C2_SEL_B | R300_C3_SEL_A;
596
597         default:
598             return ~0; /* Unsupported. */
599     }
600 }
601
602 boolean r300_is_colorbuffer_format_supported(enum pipe_format format)
603 {
604     return r300_translate_colorformat(format) != ~0 &&
605            r300_translate_out_fmt(format) != ~0;
606 }
607
608 boolean r300_is_zs_format_supported(enum pipe_format format)
609 {
610     return r300_translate_zsformat(format) != ~0;
611 }
612
613 boolean r300_is_sampler_format_supported(enum pipe_format format)
614 {
615     return r300_translate_texformat(format, 0, TRUE, FALSE) != ~0;
616 }
617
618 void r300_texture_setup_format_state(struct r300_screen *screen,
619                                      struct r300_resource *tex,
620                                      unsigned level,
621                                      struct r300_texture_format_state *out)
622 {
623     struct pipe_resource *pt = &tex->b.b.b;
624     struct r300_texture_desc *desc = &tex->tex;
625     boolean is_r500 = screen->caps.is_r500;
626
627     /* Mask out all the fields we change. */
628     out->format0 = 0;
629     out->format1 &= ~R300_TX_FORMAT_TEX_COORD_TYPE_MASK;
630     out->format2 &= R500_TXFORMAT_MSB;
631     out->tile_config = 0;
632
633     /* Set sampler state. */
634     out->format0 =
635         R300_TX_WIDTH((u_minify(desc->width0, level) - 1) & 0x7ff) |
636         R300_TX_HEIGHT((u_minify(desc->height0, level) - 1) & 0x7ff) |
637         R300_TX_DEPTH(util_logbase2(u_minify(desc->depth0, level)) & 0xf);
638
639     if (desc->uses_stride_addressing) {
640         /* rectangles love this */
641         out->format0 |= R300_TX_PITCH_EN;
642         out->format2 = (desc->stride_in_pixels[level] - 1) & 0x1fff;
643     }
644
645     if (pt->target == PIPE_TEXTURE_CUBE) {
646         out->format1 |= R300_TX_FORMAT_CUBIC_MAP;
647     }
648     if (pt->target == PIPE_TEXTURE_3D) {
649         out->format1 |= R300_TX_FORMAT_3D;
650     }
651
652     /* large textures on r500 */
653     if (is_r500)
654     {
655         if (desc->width0 > 2048) {
656             out->format2 |= R500_TXWIDTH_BIT11;
657         }
658         if (desc->height0 > 2048) {
659             out->format2 |= R500_TXHEIGHT_BIT11;
660         }
661     }
662
663     out->tile_config = R300_TXO_MACRO_TILE(desc->macrotile[level]) |
664                        R300_TXO_MICRO_TILE(desc->microtile);
665 }
666
667 static void r300_texture_setup_fb_state(struct r300_surface *surf)
668 {
669     struct r300_resource *tex = r300_resource(surf->base.texture);
670     unsigned level = surf->base.u.tex.level;
671
672     /* Set framebuffer state. */
673     if (util_format_is_depth_or_stencil(surf->base.format)) {
674         surf->pitch =
675                 tex->tex.stride_in_pixels[level] |
676                 R300_DEPTHMACROTILE(tex->tex.macrotile[level]) |
677                 R300_DEPTHMICROTILE(tex->tex.microtile);
678         surf->format = r300_translate_zsformat(surf->base.format);
679         surf->pitch_zmask = tex->tex.zmask_stride_in_pixels[level];
680         surf->pitch_hiz = tex->tex.hiz_stride_in_pixels[level];
681     } else {
682         surf->pitch =
683                 tex->tex.stride_in_pixels[level] |
684                 r300_translate_colorformat(surf->base.format) |
685                 R300_COLOR_TILE(tex->tex.macrotile[level]) |
686                 R300_COLOR_MICROTILE(tex->tex.microtile);
687         surf->format = r300_translate_out_fmt(surf->base.format);
688     }
689 }
690
691 boolean r300_resource_set_properties(struct pipe_screen *screen,
692                                      struct pipe_resource *tex,
693                                      unsigned offset,
694                                      const struct pipe_resource *new_properties)
695 {
696     struct r300_screen *rscreen = r300_screen(screen);
697     struct r300_resource *res = r300_resource(tex);
698
699     SCREEN_DBG(rscreen, DBG_TEX,
700         "r300: texture_set_properties: %s -> %s\n",
701         util_format_short_name(tex->format),
702         util_format_short_name(new_properties->format));
703
704     if (!r300_texture_desc_init(rscreen, res, new_properties)) {
705         fprintf(stderr, "r300: ERROR: Cannot set texture properties.\n");
706         return FALSE;
707     }
708     res->tex_offset = offset;
709     r300_texture_setup_format_state(rscreen, res, 0, &res->tx_format);
710
711     return TRUE;
712 }
713
714 static void r300_texture_destroy(struct pipe_screen *screen,
715                                  struct pipe_resource* texture)
716 {
717     struct r300_resource* tex = (struct r300_resource*)texture;
718
719     r300_winsys_bo_reference(&tex->buf, NULL);
720     FREE(tex);
721 }
722
723 boolean r300_resource_get_handle(struct pipe_screen* screen,
724                                  struct pipe_resource *texture,
725                                  struct winsys_handle *whandle)
726 {
727     struct r300_winsys_screen *rws = (struct r300_winsys_screen *)screen->winsys;
728     struct r300_resource* tex = (struct r300_resource*)texture;
729
730     if (!tex) {
731         return FALSE;
732     }
733
734     return rws->buffer_get_handle(tex->buf,
735                                   tex->tex.stride_in_bytes[0], whandle);
736 }
737
738 static const struct u_resource_vtbl r300_texture_vtbl =
739 {
740     NULL,                           /* get_handle */
741     r300_texture_destroy,           /* resource_destroy */
742     NULL,                           /* is_resource_referenced */
743     r300_texture_get_transfer,      /* get_transfer */
744     r300_texture_transfer_destroy,  /* transfer_destroy */
745     r300_texture_transfer_map,      /* transfer_map */
746     NULL,                           /* transfer_flush_region */
747     r300_texture_transfer_unmap,    /* transfer_unmap */
748     u_default_transfer_inline_write /* transfer_inline_write */
749 };
750
751 /* The common texture constructor. */
752 static struct r300_resource*
753 r300_texture_create_object(struct r300_screen *rscreen,
754                            const struct pipe_resource *base,
755                            enum r300_buffer_tiling microtile,
756                            enum r300_buffer_tiling macrotile,
757                            unsigned stride_in_bytes_override,
758                            unsigned max_buffer_size,
759                            struct r300_winsys_bo *buffer)
760 {
761     struct r300_winsys_screen *rws = rscreen->rws;
762     struct r300_resource *tex = CALLOC_STRUCT(r300_resource);
763     if (!tex) {
764         if (buffer)
765             r300_winsys_bo_reference(&buffer, NULL);
766         return NULL;
767     }
768
769     pipe_reference_init(&tex->b.b.b.reference, 1);
770     tex->b.b.b.screen = &rscreen->screen;
771     tex->b.b.b.usage = base->usage;
772     tex->b.b.b.bind = base->bind;
773     tex->b.b.b.flags = base->flags;
774     tex->b.b.vtbl = &r300_texture_vtbl;
775     tex->tex.microtile = microtile;
776     tex->tex.macrotile[0] = macrotile;
777     tex->tex.stride_in_bytes_override = stride_in_bytes_override;
778     tex->domain = base->flags & R300_RESOURCE_FLAG_TRANSFER ?
779                   R300_DOMAIN_GTT :
780                   R300_DOMAIN_VRAM | R300_DOMAIN_GTT;
781     tex->buf_size = max_buffer_size;
782
783     if (!r300_resource_set_properties(&rscreen->screen, &tex->b.b.b, 0, base)) {
784         if (buffer)
785             r300_winsys_bo_reference(&buffer, NULL);
786         FREE(tex);
787         return NULL;
788     }
789
790     /* Create the backing buffer if needed. */
791     if (!buffer) {
792         tex->buf_size = tex->tex.size_in_bytes;
793         tex->buf = rws->buffer_create(rws, tex->tex.size_in_bytes, 2048,
794                                          base->bind, base->usage, tex->domain);
795
796         if (!tex->buf) {
797             FREE(tex);
798             return NULL;
799         }
800     } else {
801         tex->buf = buffer;
802     }
803
804     tex->cs_buf = rws->buffer_get_cs_handle(tex->buf);
805
806     rws->buffer_set_tiling(tex->buf, NULL,
807             tex->tex.microtile, tex->tex.macrotile[0],
808             tex->tex.stride_in_bytes[0]);
809
810     return tex;
811 }
812
813 /* Create a new texture. */
814 struct pipe_resource *r300_texture_create(struct pipe_screen *screen,
815                                           const struct pipe_resource *base)
816 {
817     struct r300_screen *rscreen = r300_screen(screen);
818     enum r300_buffer_tiling microtile, macrotile;
819
820     if ((base->flags & R300_RESOURCE_FLAG_TRANSFER) ||
821         (base->bind & PIPE_BIND_SCANOUT)) {
822         microtile = R300_BUFFER_LINEAR;
823         macrotile = R300_BUFFER_LINEAR;
824     } else {
825         microtile = R300_BUFFER_SELECT_LAYOUT;
826         macrotile = R300_BUFFER_SELECT_LAYOUT;
827     }
828
829     return (struct pipe_resource*)
830            r300_texture_create_object(rscreen, base, microtile, macrotile,
831                                       0, 0, NULL);
832 }
833
834 struct pipe_resource *r300_texture_from_handle(struct pipe_screen *screen,
835                                                const struct pipe_resource *base,
836                                                struct winsys_handle *whandle)
837 {
838     struct r300_winsys_screen *rws = (struct r300_winsys_screen*)screen->winsys;
839     struct r300_screen *rscreen = r300_screen(screen);
840     struct r300_winsys_bo *buffer;
841     enum r300_buffer_tiling microtile, macrotile;
842     unsigned stride, size;
843
844     /* Support only 2D textures without mipmaps */
845     if ((base->target != PIPE_TEXTURE_2D &&
846           base->target != PIPE_TEXTURE_RECT) ||
847         base->depth0 != 1 ||
848         base->last_level != 0) {
849         return NULL;
850     }
851
852     buffer = rws->buffer_from_handle(rws, whandle, &stride, &size);
853     if (!buffer)
854         return NULL;
855
856     rws->buffer_get_tiling(buffer, &microtile, &macrotile);
857
858     /* Enforce a microtiled zbuffer. */
859     if (util_format_is_depth_or_stencil(base->format) &&
860         microtile == R300_BUFFER_LINEAR) {
861         switch (util_format_get_blocksize(base->format)) {
862             case 4:
863                 microtile = R300_BUFFER_TILED;
864                 break;
865
866             case 2:
867                 if (rws->get_value(rws, R300_VID_DRM_2_1_0))
868                     microtile = R300_BUFFER_SQUARETILED;
869                 break;
870         }
871     }
872
873     return (struct pipe_resource*)
874            r300_texture_create_object(rscreen, base, microtile, macrotile,
875                                       stride, size, buffer);
876 }
877
878 /* Not required to implement u_resource_vtbl, consider moving to another file:
879  */
880 struct pipe_surface* r300_create_surface(struct pipe_context * ctx,
881                                          struct pipe_resource* texture,
882                                          const struct pipe_surface *surf_tmpl)
883 {
884     struct r300_resource* tex = r300_resource(texture);
885     struct r300_surface* surface = CALLOC_STRUCT(r300_surface);
886     unsigned level = surf_tmpl->u.tex.level;
887
888     assert(surf_tmpl->u.tex.first_layer == surf_tmpl->u.tex.last_layer);
889
890     if (surface) {
891         uint32_t offset, tile_height;
892
893         pipe_reference_init(&surface->base.reference, 1);
894         pipe_resource_reference(&surface->base.texture, texture);
895         surface->base.context = ctx;
896         surface->base.format = surf_tmpl->format;
897         surface->base.width = u_minify(texture->width0, level);
898         surface->base.height = u_minify(texture->height0, level);
899         surface->base.usage = surf_tmpl->usage;
900         surface->base.u.tex.level = level;
901         surface->base.u.tex.first_layer = surf_tmpl->u.tex.first_layer;
902         surface->base.u.tex.last_layer = surf_tmpl->u.tex.last_layer;
903
904         surface->buf = tex->buf;
905         surface->cs_buf = tex->cs_buf;
906
907         /* Prefer VRAM if there are multiple domains to choose from. */
908         surface->domain = tex->domain;
909         if (surface->domain & R300_DOMAIN_VRAM)
910             surface->domain &= ~R300_DOMAIN_GTT;
911
912         surface->offset = r300_texture_get_offset(tex, level,
913                                                   surf_tmpl->u.tex.first_layer);
914         r300_texture_setup_fb_state(surface);
915
916         /* Parameters for the CBZB clear. */
917         surface->cbzb_allowed = tex->tex.cbzb_allowed[level];
918         surface->cbzb_width = align(surface->base.width, 64);
919
920         /* Height must be aligned to the size of a tile. */
921         tile_height = r300_get_pixel_alignment(tex->b.b.b.format,
922                                                tex->b.b.b.nr_samples,
923                                                tex->tex.microtile,
924                                                tex->tex.macrotile[level],
925                                                DIM_HEIGHT, 0);
926
927         surface->cbzb_height = align((surface->base.height + 1) / 2,
928                                      tile_height);
929
930         /* Offset must be aligned to 2K and must point at the beginning
931          * of a scanline. */
932         offset = surface->offset +
933                  tex->tex.stride_in_bytes[level] * surface->cbzb_height;
934         surface->cbzb_midpoint_offset = offset & ~2047;
935
936         surface->cbzb_pitch = surface->pitch & 0x1ffffc;
937
938         if (util_format_get_blocksizebits(surface->base.format) == 32)
939             surface->cbzb_format = R300_DEPTHFORMAT_24BIT_INT_Z_8BIT_STENCIL;
940         else
941             surface->cbzb_format = R300_DEPTHFORMAT_16BIT_INT_Z;
942
943         DBG(r300_context(ctx), DBG_CBZB,
944             "CBZB Allowed: %s, Dim: %ix%i, Misalignment: %i, Micro: %s, Macro: %s\n",
945             surface->cbzb_allowed ? "YES" : " NO",
946             surface->cbzb_width, surface->cbzb_height,
947             offset & 2047,
948             tex->tex.microtile ? "YES" : " NO",
949             tex->tex.macrotile[level] ? "YES" : " NO");
950     }
951
952     return &surface->base;
953 }
954
955 /* Not required to implement u_resource_vtbl, consider moving to another file:
956  */
957 void r300_surface_destroy(struct pipe_context *ctx, struct pipe_surface* s)
958 {
959     pipe_resource_reference(&s->texture, NULL);
960     FREE(s);
961 }