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