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