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