Merge commit 'origin/master' into gallium-msaa
[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 #include "pipe/p_screen.h"
25
26 #include "util/u_format.h"
27 #include "util/u_math.h"
28 #include "util/u_memory.h"
29
30 #include "r300_context.h"
31 #include "r300_reg.h"
32 #include "r300_texture.h"
33 #include "r300_transfer.h"
34 #include "r300_screen.h"
35 #include "r300_winsys.h"
36
37 #define TILE_WIDTH 0
38 #define TILE_HEIGHT 1
39
40 static const unsigned microblock_table[5][3][2] = {
41     /*linear  tiled   square-tiled */
42     {{32, 1}, {8, 4}, {0, 0}}, /*   8 bits per pixel */
43     {{16, 1}, {8, 2}, {4, 4}}, /*  16 bits per pixel */
44     {{ 8, 1}, {4, 2}, {0, 0}}, /*  32 bits per pixel */
45     {{ 4, 1}, {0, 0}, {2, 2}}, /*  64 bits per pixel */
46     {{ 2, 1}, {0, 0}, {0, 0}}  /* 128 bits per pixel */
47 };
48
49 /* Return true for non-compressed and non-YUV formats. */
50 static boolean r300_format_is_plain(enum pipe_format format)
51 {
52     const struct util_format_description *desc = util_format_description(format);
53
54     if (!format) {
55         return FALSE;
56     }
57
58     return desc->layout == UTIL_FORMAT_LAYOUT_PLAIN;
59 }
60
61 /* Translate a pipe_format into a useful texture format for sampling.
62  *
63  * Some special formats are translated directly using R300_EASY_TX_FORMAT,
64  * but the majority of them is translated in a generic way, automatically
65  * supporting all the formats hw can support.
66  *
67  * R300_EASY_TX_FORMAT swizzles the texture.
68  * Note the signature of R300_EASY_TX_FORMAT:
69  *   R300_EASY_TX_FORMAT(B, G, R, A, FORMAT);
70  *
71  * The FORMAT specifies how the texture sampler will treat the texture, and
72  * makes available X, Y, Z, W, ZERO, and ONE for swizzling. */
73 uint32_t r300_translate_texformat(enum pipe_format format,
74                                   const unsigned char *swizzle_view)
75 {
76     uint32_t result = 0;
77     const struct util_format_description *desc;
78     unsigned i;
79     boolean uniform = TRUE;
80     const uint32_t swizzle_shift[4] = {
81         R300_TX_FORMAT_R_SHIFT,
82         R300_TX_FORMAT_G_SHIFT,
83         R300_TX_FORMAT_B_SHIFT,
84         R300_TX_FORMAT_A_SHIFT
85     };
86     const uint32_t swizzle_bit[4] = {
87         R300_TX_FORMAT_X,
88         R300_TX_FORMAT_Y,
89         R300_TX_FORMAT_Z,
90         R300_TX_FORMAT_W
91     };
92     const uint32_t sign_bit[4] = {
93         R300_TX_FORMAT_SIGNED_X,
94         R300_TX_FORMAT_SIGNED_Y,
95         R300_TX_FORMAT_SIGNED_Z,
96         R300_TX_FORMAT_SIGNED_W,
97     };
98     unsigned char swizzle[4];
99
100     desc = util_format_description(format);
101
102     /* Colorspace (return non-RGB formats directly). */
103     switch (desc->colorspace) {
104         /* Depth stencil formats. */
105         case UTIL_FORMAT_COLORSPACE_ZS:
106             switch (format) {
107                 case PIPE_FORMAT_Z16_UNORM:
108                     return R300_EASY_TX_FORMAT(X, X, X, X, X16);
109                 case PIPE_FORMAT_X8Z24_UNORM:
110                 case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
111                     return R300_EASY_TX_FORMAT(X, X, X, X, W24_FP);
112                 default:
113                     return ~0; /* Unsupported. */
114             }
115
116         /* YUV formats. */
117         case UTIL_FORMAT_COLORSPACE_YUV:
118             result |= R300_TX_FORMAT_YUV_TO_RGB;
119
120             switch (format) {
121                 case PIPE_FORMAT_UYVY:
122                     return R300_EASY_TX_FORMAT(X, Y, Z, ONE, YVYU422) | result;
123                 case PIPE_FORMAT_YUYV:
124                     return R300_EASY_TX_FORMAT(X, Y, Z, ONE, VYUY422) | result;
125                 default:
126                     return ~0; /* Unsupported/unknown. */
127             }
128
129         /* Add gamma correction. */
130         case UTIL_FORMAT_COLORSPACE_SRGB:
131             result |= R300_TX_FORMAT_GAMMA;
132             break;
133
134         default:
135             switch (format) {
136                 /* Same as YUV but without the YUR->RGB conversion. */
137                 case PIPE_FORMAT_R8G8_B8G8_UNORM:
138                     return R300_EASY_TX_FORMAT(X, Y, Z, ONE, YVYU422) | result;
139                 case PIPE_FORMAT_G8R8_G8B8_UNORM:
140                     return R300_EASY_TX_FORMAT(X, Y, Z, ONE, VYUY422) | result;
141                 default:;
142             }
143     }
144
145     /* Get swizzle. */
146     if (swizzle_view) {
147         /* Compose two sets of swizzles. */
148         for (i = 0; i < 4; i++) {
149             swizzle[i] = swizzle_view[i] <= UTIL_FORMAT_SWIZZLE_W ?
150                          desc->swizzle[swizzle_view[i]] : swizzle_view[i];
151         }
152     } else {
153         memcpy(swizzle, desc->swizzle, sizeof(swizzle));
154     }
155
156     /* Add swizzle. */
157     for (i = 0; i < 4; i++) {
158         switch (swizzle[i]) {
159             case UTIL_FORMAT_SWIZZLE_X:
160             case UTIL_FORMAT_SWIZZLE_NONE:
161                 result |= swizzle_bit[0] << swizzle_shift[i];
162                 break;
163             case UTIL_FORMAT_SWIZZLE_Y:
164                 result |= swizzle_bit[1] << swizzle_shift[i];
165                 break;
166             case UTIL_FORMAT_SWIZZLE_Z:
167                 result |= swizzle_bit[2] << swizzle_shift[i];
168                 break;
169             case UTIL_FORMAT_SWIZZLE_W:
170                 result |= swizzle_bit[3] << swizzle_shift[i];
171                 break;
172             case UTIL_FORMAT_SWIZZLE_0:
173                 result |= R300_TX_FORMAT_ZERO << swizzle_shift[i];
174                 break;
175             case UTIL_FORMAT_SWIZZLE_1:
176                 result |= R300_TX_FORMAT_ONE << swizzle_shift[i];
177                 break;
178             default:
179                 return ~0; /* Unsupported. */
180         }
181     }
182
183     /* S3TC formats. */
184     if (desc->layout == UTIL_FORMAT_LAYOUT_S3TC) {
185         switch (format) {
186             case PIPE_FORMAT_DXT1_RGB:
187             case PIPE_FORMAT_DXT1_RGBA:
188             case PIPE_FORMAT_DXT1_SRGB:
189             case PIPE_FORMAT_DXT1_SRGBA:
190                 return R300_TX_FORMAT_DXT1 | result;
191             case PIPE_FORMAT_DXT3_RGBA:
192             case PIPE_FORMAT_DXT3_SRGBA:
193                 return R300_TX_FORMAT_DXT3 | result;
194             case PIPE_FORMAT_DXT5_RGBA:
195             case PIPE_FORMAT_DXT5_SRGBA:
196                 return R300_TX_FORMAT_DXT5 | result;
197             default:
198                 return ~0; /* Unsupported/unknown. */
199         }
200     }
201
202     /* Add sign. */
203     for (i = 0; i < desc->nr_channels; i++) {
204         if (desc->channel[i].type == UTIL_FORMAT_TYPE_SIGNED) {
205             result |= sign_bit[i];
206         }
207     }
208
209     /* This is truly a special format.
210      * It stores R8G8 and B is computed using sqrt(1 - R^2 - G^2)
211      * in the sampler unit. Also known as D3DFMT_CxV8U8. */
212     if (format == PIPE_FORMAT_R8G8Bx_SNORM) {
213         return R300_TX_FORMAT_CxV8U8 | result;
214     }
215
216     /* RGTC formats. */
217     if (desc->layout == UTIL_FORMAT_LAYOUT_RGTC) {
218         switch (format) {
219             case PIPE_FORMAT_RGTC1_UNORM:
220             case PIPE_FORMAT_RGTC1_SNORM:
221                 return R500_TX_FORMAT_ATI1N | result;
222             case PIPE_FORMAT_RGTC2_UNORM:
223             case PIPE_FORMAT_RGTC2_SNORM:
224                 return R400_TX_FORMAT_ATI2N | result;
225             default:
226                 return ~0; /* Unsupported/unknown. */
227         }
228     }
229
230     /* See whether the components are of the same size. */
231     for (i = 1; i < desc->nr_channels; i++) {
232         uniform = uniform && desc->channel[0].size == desc->channel[i].size;
233     }
234
235     /* Non-uniform formats. */
236     if (!uniform) {
237         switch (desc->nr_channels) {
238             case 3:
239                 if (desc->channel[0].size == 5 &&
240                     desc->channel[1].size == 6 &&
241                     desc->channel[2].size == 5) {
242                     return R300_TX_FORMAT_Z5Y6X5 | result;
243                 }
244                 if (desc->channel[0].size == 5 &&
245                     desc->channel[1].size == 5 &&
246                     desc->channel[2].size == 6) {
247                     return R300_TX_FORMAT_Z6Y5X5 | result;
248                 }
249                 return ~0; /* Unsupported/unknown. */
250
251             case 4:
252                 if (desc->channel[0].size == 5 &&
253                     desc->channel[1].size == 5 &&
254                     desc->channel[2].size == 5 &&
255                     desc->channel[3].size == 1) {
256                     return R300_TX_FORMAT_W1Z5Y5X5 | result;
257                 }
258                 if (desc->channel[0].size == 10 &&
259                     desc->channel[1].size == 10 &&
260                     desc->channel[2].size == 10 &&
261                     desc->channel[3].size == 2) {
262                     return R300_TX_FORMAT_W2Z10Y10X10 | result;
263                 }
264         }
265         return ~0; /* Unsupported/unknown. */
266     }
267
268     /* And finally, uniform formats. */
269     switch (desc->channel[0].type) {
270         case UTIL_FORMAT_TYPE_UNSIGNED:
271         case UTIL_FORMAT_TYPE_SIGNED:
272             if (!desc->channel[0].normalized &&
273                 desc->colorspace != UTIL_FORMAT_COLORSPACE_SRGB) {
274                 return ~0;
275             }
276
277             switch (desc->channel[0].size) {
278                 case 4:
279                     switch (desc->nr_channels) {
280                         case 2:
281                             return R300_TX_FORMAT_Y4X4 | result;
282                         case 4:
283                             return R300_TX_FORMAT_W4Z4Y4X4 | result;
284                     }
285                     return ~0;
286
287                 case 8:
288                     switch (desc->nr_channels) {
289                         case 1:
290                             return R300_TX_FORMAT_X8 | result;
291                         case 2:
292                             return R300_TX_FORMAT_Y8X8 | result;
293                         case 4:
294                             return R300_TX_FORMAT_W8Z8Y8X8 | result;
295                     }
296                     return ~0;
297
298                 case 16:
299                     switch (desc->nr_channels) {
300                         case 1:
301                             return R300_TX_FORMAT_X16 | result;
302                         case 2:
303                             return R300_TX_FORMAT_Y16X16 | result;
304                         case 4:
305                             return R300_TX_FORMAT_W16Z16Y16X16 | result;
306                     }
307             }
308             return ~0;
309
310         case UTIL_FORMAT_TYPE_FLOAT:
311             switch (desc->channel[0].size) {
312                 case 16:
313                     switch (desc->nr_channels) {
314                         case 1:
315                             return R300_TX_FORMAT_16F | result;
316                         case 2:
317                             return R300_TX_FORMAT_16F_16F | result;
318                         case 4:
319                             return R300_TX_FORMAT_16F_16F_16F_16F | result;
320                     }
321                     return ~0;
322
323                 case 32:
324                     switch (desc->nr_channels) {
325                         case 1:
326                             return R300_TX_FORMAT_32F | result;
327                         case 2:
328                             return R300_TX_FORMAT_32F_32F | result;
329                         case 4:
330                             return R300_TX_FORMAT_32F_32F_32F_32F | result;
331                     }
332             }
333     }
334
335     return ~0; /* Unsupported/unknown. */
336 }
337
338 uint32_t r500_tx_format_msb_bit(enum pipe_format format)
339 {
340     switch (format) {
341         case PIPE_FORMAT_RGTC1_UNORM:
342         case PIPE_FORMAT_RGTC1_SNORM:
343             return R500_TXFORMAT_MSB;
344         default:
345             return 0;
346     }
347 }
348
349 /* Buffer formats. */
350
351 /* Colorbuffer formats. This is the unswizzled format of the RB3D block's
352  * output. For the swizzling of the targets, check the shader's format. */
353 static uint32_t r300_translate_colorformat(enum pipe_format format)
354 {
355     switch (format) {
356         /* 8-bit buffers. */
357         case PIPE_FORMAT_A8_UNORM:
358         case PIPE_FORMAT_I8_UNORM:
359         case PIPE_FORMAT_L8_UNORM:
360         case PIPE_FORMAT_R8_UNORM:
361         case PIPE_FORMAT_R8_SNORM:
362             return R300_COLOR_FORMAT_I8;
363
364         /* 16-bit buffers. */
365         case PIPE_FORMAT_B5G6R5_UNORM:
366             return R300_COLOR_FORMAT_RGB565;
367
368         case PIPE_FORMAT_B5G5R5A1_UNORM:
369         case PIPE_FORMAT_B5G5R5X1_UNORM:
370             return R300_COLOR_FORMAT_ARGB1555;
371
372         case PIPE_FORMAT_B4G4R4A4_UNORM:
373         case PIPE_FORMAT_B4G4R4X4_UNORM:
374             return R300_COLOR_FORMAT_ARGB4444;
375
376         /* 32-bit buffers. */
377         case PIPE_FORMAT_B8G8R8A8_UNORM:
378         case PIPE_FORMAT_B8G8R8X8_UNORM:
379         case PIPE_FORMAT_A8R8G8B8_UNORM:
380         case PIPE_FORMAT_X8R8G8B8_UNORM:
381         case PIPE_FORMAT_A8B8G8R8_UNORM:
382         case PIPE_FORMAT_R8G8B8A8_SNORM:
383         case PIPE_FORMAT_X8B8G8R8_UNORM:
384         case PIPE_FORMAT_R8G8B8X8_UNORM:
385         case PIPE_FORMAT_R8SG8SB8UX8U_NORM:
386             return R300_COLOR_FORMAT_ARGB8888;
387
388         case PIPE_FORMAT_R10G10B10A2_UNORM:
389         case PIPE_FORMAT_R10G10B10X2_SNORM:
390         case PIPE_FORMAT_B10G10R10A2_UNORM:
391         case PIPE_FORMAT_R10SG10SB10SA2U_NORM:
392             return R500_COLOR_FORMAT_ARGB2101010;  /* R5xx-only? */
393
394         /* 64-bit buffers. */
395         case PIPE_FORMAT_R16G16B16A16_UNORM:
396         case PIPE_FORMAT_R16G16B16A16_SNORM:
397         case PIPE_FORMAT_R16G16B16A16_FLOAT:
398             return R300_COLOR_FORMAT_ARGB16161616;
399
400         /* 128-bit buffers. */
401         case PIPE_FORMAT_R32G32B32A32_FLOAT:
402             return R300_COLOR_FORMAT_ARGB32323232;
403
404         /* YUV buffers. */
405         case PIPE_FORMAT_UYVY:
406             return R300_COLOR_FORMAT_YVYU;
407         case PIPE_FORMAT_YUYV:
408             return R300_COLOR_FORMAT_VYUY;
409         default:
410             return ~0; /* Unsupported. */
411     }
412 }
413
414 /* Depthbuffer and stencilbuffer. Thankfully, we only support two flavors. */
415 static uint32_t r300_translate_zsformat(enum pipe_format format)
416 {
417     switch (format) {
418         /* 16-bit depth, no stencil */
419         case PIPE_FORMAT_Z16_UNORM:
420             return R300_DEPTHFORMAT_16BIT_INT_Z;
421         /* 24-bit depth, ignored stencil */
422         case PIPE_FORMAT_X8Z24_UNORM:
423         /* 24-bit depth, 8-bit stencil */
424         case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
425             return R300_DEPTHFORMAT_24BIT_INT_Z_8BIT_STENCIL;
426         default:
427             return ~0; /* Unsupported. */
428     }
429 }
430
431 /* Shader output formats. This is essentially the swizzle from the shader
432  * to the RB3D block.
433  *
434  * Note that formats are stored from C3 to C0. */
435 static uint32_t r300_translate_out_fmt(enum pipe_format format)
436 {
437     uint32_t modifier = 0;
438     unsigned i;
439     const struct util_format_description *desc;
440     static const uint32_t sign_bit[4] = {
441         R300_OUT_SIGN(0x1),
442         R300_OUT_SIGN(0x2),
443         R300_OUT_SIGN(0x4),
444         R300_OUT_SIGN(0x8),
445     };
446
447     desc = util_format_description(format);
448
449     /* Specifies how the shader output is written to the fog unit. */
450     if (desc->channel[0].type == UTIL_FORMAT_TYPE_FLOAT) {
451         if (desc->channel[0].size == 32) {
452             modifier |= R300_US_OUT_FMT_C4_32_FP;
453         } else {
454             modifier |= R300_US_OUT_FMT_C4_16_FP;
455         }
456     } else {
457         if (desc->channel[0].size == 16) {
458             modifier |= R300_US_OUT_FMT_C4_16;
459         } else {
460             /* C4_8 seems to be used for the formats whose pixel size
461              * is <= 32 bits. */
462             modifier |= R300_US_OUT_FMT_C4_8;
463         }
464     }
465
466     /* Add sign. */
467     for (i = 0; i < 4; i++)
468         if (desc->channel[i].type == UTIL_FORMAT_TYPE_SIGNED) {
469             modifier |= sign_bit[i];
470         }
471
472     /* Add swizzles and return. */
473     switch (format) {
474         /* 8-bit outputs.
475          * COLORFORMAT_I8 stores the C2 component. */
476         case PIPE_FORMAT_A8_UNORM:
477             return modifier | R300_C2_SEL_A;
478         case PIPE_FORMAT_I8_UNORM:
479         case PIPE_FORMAT_L8_UNORM:
480         case PIPE_FORMAT_R8_UNORM:
481         case PIPE_FORMAT_R8_SNORM:
482             return modifier | R300_C2_SEL_R;
483
484         /* BGRA outputs. */
485         case PIPE_FORMAT_B5G6R5_UNORM:
486         case PIPE_FORMAT_B5G5R5A1_UNORM:
487         case PIPE_FORMAT_B5G5R5X1_UNORM:
488         case PIPE_FORMAT_B4G4R4A4_UNORM:
489         case PIPE_FORMAT_B4G4R4X4_UNORM:
490         case PIPE_FORMAT_B8G8R8A8_UNORM:
491         case PIPE_FORMAT_B8G8R8X8_UNORM:
492         case PIPE_FORMAT_B10G10R10A2_UNORM:
493             return modifier |
494                 R300_C0_SEL_B | R300_C1_SEL_G |
495                 R300_C2_SEL_R | R300_C3_SEL_A;
496
497         /* ARGB outputs. */
498         case PIPE_FORMAT_A8R8G8B8_UNORM:
499         case PIPE_FORMAT_X8R8G8B8_UNORM:
500             return modifier |
501                 R300_C0_SEL_A | R300_C1_SEL_R |
502                 R300_C2_SEL_G | R300_C3_SEL_B;
503
504         /* ABGR outputs. */
505         case PIPE_FORMAT_A8B8G8R8_UNORM:
506         case PIPE_FORMAT_X8B8G8R8_UNORM:
507             return modifier |
508                 R300_C0_SEL_A | R300_C1_SEL_B |
509                 R300_C2_SEL_G | R300_C3_SEL_R;
510
511         /* RGBA outputs. */
512         case PIPE_FORMAT_R8G8B8X8_UNORM:
513         case PIPE_FORMAT_R8G8B8A8_SNORM:
514         case PIPE_FORMAT_R8SG8SB8UX8U_NORM:
515         case PIPE_FORMAT_R10G10B10A2_UNORM:
516         case PIPE_FORMAT_R10G10B10X2_SNORM:
517         case PIPE_FORMAT_R10SG10SB10SA2U_NORM:
518         case PIPE_FORMAT_R16G16B16A16_UNORM:
519         case PIPE_FORMAT_R16G16B16A16_SNORM:
520         case PIPE_FORMAT_R16G16B16A16_FLOAT:
521         case PIPE_FORMAT_R32G32B32A32_FLOAT:
522             return modifier |
523                 R300_C0_SEL_R | R300_C1_SEL_G |
524                 R300_C2_SEL_B | R300_C3_SEL_A;
525
526         default:
527             return ~0; /* Unsupported. */
528     }
529 }
530
531 boolean r300_is_colorbuffer_format_supported(enum pipe_format format)
532 {
533     return r300_translate_colorformat(format) != ~0 &&
534            r300_translate_out_fmt(format) != ~0;
535 }
536
537 boolean r300_is_zs_format_supported(enum pipe_format format)
538 {
539     return r300_translate_zsformat(format) != ~0;
540 }
541
542 boolean r300_is_sampler_format_supported(enum pipe_format format)
543 {
544     return r300_translate_texformat(format, 0) != ~0;
545 }
546
547 static void r300_texture_setup_immutable_state(struct r300_screen* screen,
548                                                struct r300_texture* tex)
549 {
550     struct r300_texture_format_state* f = &tex->tx_format;
551     struct pipe_resource *pt = &tex->b.b;
552     boolean is_r500 = screen->caps.is_r500;
553
554     /* Set sampler state. */
555     f->format0 = R300_TX_WIDTH((pt->width0 - 1) & 0x7ff) |
556                  R300_TX_HEIGHT((pt->height0 - 1) & 0x7ff);
557
558     if (tex->uses_pitch) {
559         /* rectangles love this */
560         f->format0 |= R300_TX_PITCH_EN;
561         f->format2 = (tex->hwpitch[0] - 1) & 0x1fff;
562     } else {
563         /* power of two textures (3D, mipmaps, and no pitch) */
564         f->format0 |= R300_TX_DEPTH(util_logbase2(pt->depth0) & 0xf);
565     }
566
567     f->format1 = 0;
568     if (pt->target == PIPE_TEXTURE_CUBE) {
569         f->format1 |= R300_TX_FORMAT_CUBIC_MAP;
570     }
571     if (pt->target == PIPE_TEXTURE_3D) {
572         f->format1 |= R300_TX_FORMAT_3D;
573     }
574
575     /* large textures on r500 */
576     if (is_r500)
577     {
578         if (pt->width0 > 2048) {
579             f->format2 |= R500_TXWIDTH_BIT11;
580         }
581         if (pt->height0 > 2048) {
582             f->format2 |= R500_TXHEIGHT_BIT11;
583         }
584     }
585
586     f->tile_config = R300_TXO_MACRO_TILE(tex->macrotile) |
587                      R300_TXO_MICRO_TILE(tex->microtile);
588
589     SCREEN_DBG(screen, DBG_TEX, "r300: Set texture state (%dx%d, %d levels)\n",
590                pt->width0, pt->height0, pt->last_level);
591 }
592
593 static void r300_texture_setup_fb_state(struct r300_screen* screen,
594                                         struct r300_texture* tex)
595 {
596     unsigned i;
597
598     /* Set framebuffer state. */
599     if (util_format_is_depth_or_stencil(tex->b.b.format)) {
600         for (i = 0; i <= tex->b.b.last_level; i++) {
601             tex->fb_state.depthpitch[i] =
602                 tex->hwpitch[i] |
603                 R300_DEPTHMACROTILE(tex->mip_macrotile[i]) |
604                 R300_DEPTHMICROTILE(tex->microtile);
605         }
606         tex->fb_state.zb_format = r300_translate_zsformat(tex->b.b.format);
607     } else {
608         for (i = 0; i <= tex->b.b.last_level; i++) {
609             tex->fb_state.colorpitch[i] =
610                 tex->hwpitch[i] |
611                 r300_translate_colorformat(tex->b.b.format) |
612                 R300_COLOR_TILE(tex->mip_macrotile[i]) |
613                 R300_COLOR_MICROTILE(tex->microtile);
614         }
615         tex->fb_state.us_out_fmt = r300_translate_out_fmt(tex->b.b.format);
616     }
617 }
618
619 void r300_texture_reinterpret_format(struct pipe_screen *screen,
620                                      struct pipe_resource *tex,
621                                      enum pipe_format new_format)
622 {
623     struct r300_screen *r300screen = r300_screen(screen);
624
625     SCREEN_DBG(r300screen, DBG_TEX, "r300: Reinterpreting format: %s -> %s\n",
626                util_format_name(tex->format), util_format_name(new_format));
627
628     tex->format = new_format;
629
630     r300_texture_setup_fb_state(r300_screen(screen), r300_texture(tex));
631 }
632
633 unsigned r300_texture_get_offset(struct r300_texture* tex, unsigned level,
634                                  unsigned zslice, unsigned face)
635 {
636     unsigned offset = tex->offset[level];
637
638     switch (tex->b.b.target) {
639         case PIPE_TEXTURE_3D:
640             assert(face == 0);
641             return offset + zslice * tex->layer_size[level];
642
643         case PIPE_TEXTURE_CUBE:
644             assert(zslice == 0);
645             return offset + face * tex->layer_size[level];
646
647         default:
648             assert(zslice == 0 && face == 0);
649             return offset;
650     }
651 }
652
653 /**
654  * Return the width (dim==TILE_WIDTH) or height (dim==TILE_HEIGHT) of one tile
655  * of the given texture.
656  */
657 static unsigned r300_texture_get_tile_size(struct r300_texture* tex,
658                                            int dim, boolean macrotile)
659 {
660     unsigned pixsize, tile_size;
661
662     pixsize = util_format_get_blocksize(tex->b.b.format);
663     tile_size = microblock_table[util_logbase2(pixsize)][tex->microtile][dim];
664
665     if (macrotile) {
666         tile_size *= 8;
667     }
668
669     assert(tile_size);
670     return tile_size;
671 }
672
673 /* Return true if macrotiling should be enabled on the miplevel. */
674 static boolean r300_texture_macro_switch(struct r300_texture *tex,
675                                          unsigned level,
676                                          boolean rv350_mode,
677                                          int dim)
678 {
679     unsigned tile, texdim;
680
681     tile = r300_texture_get_tile_size(tex, dim, TRUE);
682     if (dim == TILE_WIDTH) {
683         texdim = u_minify(tex->b.b.width0, level);
684     } else {
685         texdim = u_minify(tex->b.b.height0, level);
686     }
687
688     /* See TX_FILTER1_n.MACRO_SWITCH. */
689     if (rv350_mode) {
690         return texdim >= tile;
691     } else {
692         return texdim > tile;
693     }
694 }
695
696 /**
697  * Return the stride, in bytes, of the texture images of the given texture
698  * at the given level.
699  */
700 unsigned r300_texture_get_stride(struct r300_screen* screen,
701                                  struct r300_texture* tex, unsigned level)
702 {
703     unsigned tile_width, width;
704
705     if (tex->stride_override)
706         return tex->stride_override;
707
708     /* Check the level. */
709     if (level > tex->b.b.last_level) {
710         SCREEN_DBG(screen, DBG_TEX, "%s: level (%u) > last_level (%u)\n",
711                    __FUNCTION__, level, tex->b.b.last_level);
712         return 0;
713     }
714
715     width = u_minify(tex->b.b.width0, level);
716
717     if (r300_format_is_plain(tex->b.b.format)) {
718         tile_width = r300_texture_get_tile_size(tex, TILE_WIDTH,
719                                                 tex->mip_macrotile[level]);
720         width = align(width, tile_width);
721
722         return util_format_get_stride(tex->b.b.format, width);
723     } else {
724         return align(util_format_get_stride(tex->b.b.format, width), 32);
725     }
726 }
727
728 static unsigned r300_texture_get_nblocksy(struct r300_texture* tex,
729                                           unsigned level)
730 {
731     unsigned height, tile_height;
732
733     height = u_minify(tex->b.b.height0, level);
734
735     if (r300_format_is_plain(tex->b.b.format)) {
736         tile_height = r300_texture_get_tile_size(tex, TILE_HEIGHT,
737                                                  tex->mip_macrotile[level]);
738         height = align(height, tile_height);
739
740         /* This is needed for the kernel checker, unfortunately. */
741         height = util_next_power_of_two(height);
742     }
743
744     return util_format_get_nblocksy(tex->b.b.format, height);
745 }
746
747 static void r300_texture_3d_fix_mipmapping(struct r300_screen *screen,
748                                            struct r300_texture *tex)
749 {
750     /* The kernels <= 2.6.34-rc4 compute the size of mipmapped 3D textures
751      * incorrectly. This is a workaround to prevent CS from being rejected. */
752
753     unsigned i, size;
754
755     if (!screen->rws->get_value(screen->rws, R300_VID_DRM_2_3_0) &&
756         tex->b.b.target == PIPE_TEXTURE_3D &&
757         tex->b.b.last_level > 0) {
758         size = 0;
759
760         for (i = 0; i <= tex->b.b.last_level; i++) {
761             size += r300_texture_get_stride(screen, tex, i) *
762                     r300_texture_get_nblocksy(tex, i);
763         }
764
765         size *= tex->b.b.depth0;
766         tex->size = size;
767     }
768 }
769
770 static void r300_setup_miptree(struct r300_screen* screen,
771                                struct r300_texture* tex)
772 {
773     struct pipe_resource* base = &tex->b.b;
774     unsigned stride, size, layer_size, nblocksy, i;
775     boolean rv350_mode = screen->caps.is_rv350;
776
777     SCREEN_DBG(screen, DBG_TEX, "r300: Making miptree for texture, format %s\n",
778                util_format_name(base->format));
779
780     for (i = 0; i <= base->last_level; i++) {
781         /* Let's see if this miplevel can be macrotiled. */
782         tex->mip_macrotile[i] =
783             (tex->macrotile == R300_BUFFER_TILED &&
784              r300_texture_macro_switch(tex, i, rv350_mode, TILE_WIDTH) &&
785              r300_texture_macro_switch(tex, i, rv350_mode, TILE_HEIGHT)) ?
786              R300_BUFFER_TILED : R300_BUFFER_LINEAR;
787
788         stride = r300_texture_get_stride(screen, tex, i);
789         nblocksy = r300_texture_get_nblocksy(tex, i);
790         layer_size = stride * nblocksy;
791
792         if (base->target == PIPE_TEXTURE_CUBE)
793             size = layer_size * 6;
794         else
795             size = layer_size * u_minify(base->depth0, i);
796
797         tex->offset[i] = tex->size;
798         tex->size = tex->offset[i] + size;
799         tex->layer_size[i] = layer_size;
800         tex->pitch[i] = stride / util_format_get_blocksize(base->format);
801         tex->hwpitch[i] =
802                 tex->pitch[i] * util_format_get_blockwidth(base->format);
803
804         SCREEN_DBG(screen, DBG_TEX, "r300: Texture miptree: Level %d "
805                 "(%dx%dx%d px, pitch %d bytes) %d bytes total, macrotiled %s\n",
806                 i, u_minify(base->width0, i), u_minify(base->height0, i),
807                 u_minify(base->depth0, i), stride, tex->size,
808                 tex->mip_macrotile[i] ? "TRUE" : "FALSE");
809     }
810 }
811
812 static void r300_setup_flags(struct r300_texture* tex)
813 {
814     tex->uses_pitch = !util_is_power_of_two(tex->b.b.width0) ||
815                       !util_is_power_of_two(tex->b.b.height0) ||
816                       tex->stride_override;
817 }
818
819 static void r300_setup_tiling(struct pipe_screen *screen,
820                               struct r300_texture *tex)
821 {
822     struct r300_winsys_screen *rws = (struct r300_winsys_screen *)screen->winsys;
823     enum pipe_format format = tex->b.b.format;
824     boolean rv350_mode = r300_screen(screen)->caps.is_rv350;
825     boolean is_zb = util_format_is_depth_or_stencil(format);
826     boolean dbg_no_tiling = SCREEN_DBG_ON(r300_screen(screen), DBG_NO_TILING);
827
828     if (!r300_format_is_plain(format)) {
829         return;
830     }
831
832     /* If height == 1, disable microtiling except for zbuffer. */
833     if (!is_zb && (tex->b.b.height0 == 1 || dbg_no_tiling)) {
834         return;
835     }
836
837     /* Set microtiling. */
838     switch (util_format_get_blocksize(format)) {
839         case 1:
840         case 4:
841             tex->microtile = R300_BUFFER_TILED;
842             break;
843
844         case 2:
845         case 8:
846             if (rws->get_value(rws, R300_VID_SQUARE_TILING_SUPPORT)) {
847                 tex->microtile = R300_BUFFER_SQUARETILED;
848             }
849             break;
850     }
851
852     if (dbg_no_tiling) {
853         return;
854     }
855
856     /* Set macrotiling. */
857     if (r300_texture_macro_switch(tex, 0, rv350_mode, TILE_WIDTH) &&
858         r300_texture_macro_switch(tex, 0, rv350_mode, TILE_HEIGHT)) {
859         tex->macrotile = R300_BUFFER_TILED;
860     }
861 }
862
863 static unsigned r300_texture_is_referenced(struct pipe_context *context,
864                                          struct pipe_resource *texture,
865                                          unsigned face, unsigned level)
866 {
867     struct r300_context *r300 = r300_context(context);
868     struct r300_texture *rtex = (struct r300_texture *)texture;
869
870     if (r300->rws->is_buffer_referenced(r300->rws, rtex->buffer, R300_REF_CS))
871         return PIPE_REFERENCED_FOR_READ | PIPE_REFERENCED_FOR_WRITE;
872
873     return PIPE_UNREFERENCED;
874 }
875
876 static void r300_texture_destroy(struct pipe_screen *screen,
877                                  struct pipe_resource* texture)
878 {
879     struct r300_texture* tex = (struct r300_texture*)texture;
880     struct r300_winsys_screen *rws = (struct r300_winsys_screen *)texture->screen->winsys;
881
882     rws->buffer_reference(rws, &tex->buffer, NULL);
883     FREE(tex);
884 }
885
886 static boolean r300_texture_get_handle(struct pipe_screen* screen,
887                                        struct pipe_resource *texture,
888                                        struct winsys_handle *whandle)
889 {
890     struct r300_winsys_screen *rws = (struct r300_winsys_screen *)screen->winsys;
891     struct r300_texture* tex = (struct r300_texture*)texture;
892     unsigned stride;
893
894     if (!tex) {
895         return FALSE;
896     }
897
898     stride = r300_texture_get_stride(r300_screen(screen), tex, 0);
899
900     rws->buffer_get_handle(rws, tex->buffer, stride, whandle);
901
902     return TRUE;
903 }
904
905 struct u_resource_vtbl r300_texture_vtbl = 
906 {
907    r300_texture_get_handle,           /* get_handle */
908    r300_texture_destroy,              /* resource_destroy */
909    r300_texture_is_referenced,        /* is_resource_referenced */
910    r300_texture_get_transfer,         /* get_transfer */
911    r300_texture_transfer_destroy,     /* transfer_destroy */
912    r300_texture_transfer_map,         /* transfer_map */
913    u_default_transfer_flush_region,   /* transfer_flush_region */
914    r300_texture_transfer_unmap,       /* transfer_unmap */
915    u_default_transfer_inline_write    /* transfer_inline_write */
916 };
917
918 /* Create a new texture. */
919 struct pipe_resource* r300_texture_create(struct pipe_screen* screen,
920                                           const struct pipe_resource* base)
921 {
922     struct r300_texture* tex = CALLOC_STRUCT(r300_texture);
923     struct r300_screen* rscreen = r300_screen(screen);
924     struct r300_winsys_screen *rws = (struct r300_winsys_screen *)screen->winsys;
925
926     if (!tex) {
927         return NULL;
928     }
929
930     tex->b.b = *base;
931     tex->b.vtbl = &r300_texture_vtbl;
932     pipe_reference_init(&tex->b.b.reference, 1);
933     tex->b.b.screen = screen;
934
935     r300_setup_flags(tex);
936     if (!(base->flags & R300_RESOURCE_FLAG_TRANSFER) &&
937         !(base->bind & PIPE_BIND_SCANOUT)) {
938         r300_setup_tiling(screen, tex);
939     }
940     r300_setup_miptree(rscreen, tex);
941     r300_texture_3d_fix_mipmapping(rscreen, tex);
942     r300_texture_setup_immutable_state(rscreen, tex);
943     r300_texture_setup_fb_state(rscreen, tex);
944
945     tex->buffer = rws->buffer_create(rws, 2048,
946                                      PIPE_BIND_SAMPLER_VIEW, /* XXX */
947                                      tex->size);
948     rws->buffer_set_tiling(rws, tex->buffer,
949                            tex->pitch[0],
950                            tex->microtile,
951                            tex->macrotile);
952
953     if (!tex->buffer) {
954         FREE(tex);
955         return NULL;
956     }
957
958     return (struct pipe_resource*)tex;
959 }
960
961 /* Not required to implement u_resource_vtbl, consider moving to another file:
962  */
963 struct pipe_surface* r300_get_tex_surface(struct pipe_screen* screen,
964                                           struct pipe_resource* texture,
965                                           unsigned face,
966                                           unsigned level,
967                                           unsigned zslice,
968                                           unsigned flags)
969 {
970     struct r300_texture* tex = r300_texture(texture);
971     struct pipe_surface* surface = CALLOC_STRUCT(pipe_surface);
972     unsigned offset;
973
974     offset = r300_texture_get_offset(tex, level, zslice, face);
975
976     if (surface) {
977         pipe_reference_init(&surface->reference, 1);
978         pipe_resource_reference(&surface->texture, texture);
979         surface->format = texture->format;
980         surface->width = u_minify(texture->width0, level);
981         surface->height = u_minify(texture->height0, level);
982         surface->offset = offset;
983         surface->usage = flags;
984         surface->zslice = zslice;
985         surface->texture = texture;
986         surface->face = face;
987         surface->level = level;
988     }
989
990     return surface;
991 }
992
993 /* Not required to implement u_resource_vtbl, consider moving to another file:
994  */
995 void r300_tex_surface_destroy(struct pipe_surface* s)
996 {
997     pipe_resource_reference(&s->texture, NULL);
998     FREE(s);
999 }
1000
1001 struct pipe_resource*
1002 r300_texture_from_handle(struct pipe_screen* screen,
1003                           const struct pipe_resource* base,
1004                           struct winsys_handle *whandle)
1005 {
1006     struct r300_winsys_screen *rws = (struct r300_winsys_screen*)screen->winsys;
1007     struct r300_screen* rscreen = r300_screen(screen);
1008     struct r300_winsys_buffer *buffer;
1009     struct r300_texture* tex;
1010     unsigned stride;
1011     boolean override_zb_flags;
1012
1013     /* Support only 2D textures without mipmaps */
1014     if (base->target != PIPE_TEXTURE_2D ||
1015         base->depth0 != 1 ||
1016         base->last_level != 0) {
1017         return NULL;
1018     }
1019
1020     buffer = rws->buffer_from_handle(rws, screen, whandle, &stride);
1021     if (!buffer) {
1022         return NULL;
1023     }
1024
1025     tex = CALLOC_STRUCT(r300_texture);
1026     if (!tex) {
1027         return NULL;
1028     }
1029
1030     tex->b.b = *base;
1031     tex->b.vtbl = &r300_texture_vtbl;
1032     pipe_reference_init(&tex->b.b.reference, 1);
1033     tex->b.b.screen = screen;
1034
1035     tex->stride_override = stride;
1036
1037     /* one ref already taken */
1038     tex->buffer = buffer;
1039
1040     rws->buffer_get_tiling(rws, buffer, &tex->microtile, &tex->macrotile);
1041     r300_setup_flags(tex);
1042
1043     /* Enforce microtiled zbuffer. */
1044     override_zb_flags = util_format_is_depth_or_stencil(base->format) &&
1045                         tex->microtile == R300_BUFFER_LINEAR;
1046
1047     if (override_zb_flags) {
1048         switch (util_format_get_blocksize(base->format)) {
1049             case 4:
1050                 tex->microtile = R300_BUFFER_TILED;
1051                 break;
1052
1053             case 2:
1054                 if (rws->get_value(rws, R300_VID_SQUARE_TILING_SUPPORT)) {
1055                     tex->microtile = R300_BUFFER_SQUARETILED;
1056                     break;
1057                 }
1058                 /* Pass through. */
1059
1060             default:
1061                 override_zb_flags = FALSE;
1062         }
1063     }
1064
1065     r300_setup_miptree(rscreen, tex);
1066     r300_texture_setup_immutable_state(rscreen, tex);
1067     r300_texture_setup_fb_state(rscreen, tex);
1068
1069     if (override_zb_flags) {
1070         rws->buffer_set_tiling(rws, tex->buffer,
1071                                tex->pitch[0],
1072                                tex->microtile,
1073                                tex->macrotile);
1074     }
1075     return (struct pipe_resource*)tex;
1076 }