r300g: add LATC support
[mesa.git] / src / gallium / drivers / r300 / r300_texture.c
1 /*
2 * Copyright 2008 Corbin Simpson <MostAwesomeDude@gmail.com>
3 * Copyright 2010 Marek Olšák <maraeo@gmail.com>
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE. */
23
24 /* Always include headers in the reverse order!! ~ M. */
25 #include "r300_texture.h"
26
27 #include "r300_context.h"
28 #include "r300_reg.h"
29 #include "r300_texture_desc.h"
30 #include "r300_transfer.h"
31 #include "r300_screen.h"
32 #include "r300_winsys.h"
33
34 #include "util/u_format.h"
35 #include "util/u_format_s3tc.h"
36 #include "util/u_math.h"
37 #include "util/u_memory.h"
38 #include "util/u_mm.h"
39
40 #include "pipe/p_screen.h"
41
42 unsigned r300_get_swizzle_combined(const unsigned char *swizzle_format,
43 const unsigned char *swizzle_view,
44 boolean dxtc_swizzle)
45 {
46 unsigned i;
47 unsigned char swizzle[4];
48 unsigned result = 0;
49 const uint32_t swizzle_shift[4] = {
50 R300_TX_FORMAT_R_SHIFT,
51 R300_TX_FORMAT_G_SHIFT,
52 R300_TX_FORMAT_B_SHIFT,
53 R300_TX_FORMAT_A_SHIFT
54 };
55 uint32_t swizzle_bit[4] = {
56 dxtc_swizzle ? R300_TX_FORMAT_Z : R300_TX_FORMAT_X,
57 R300_TX_FORMAT_Y,
58 dxtc_swizzle ? R300_TX_FORMAT_X : R300_TX_FORMAT_Z,
59 R300_TX_FORMAT_W
60 };
61
62 if (swizzle_view) {
63 /* Combine two sets of swizzles. */
64 for (i = 0; i < 4; i++) {
65 swizzle[i] = swizzle_view[i] <= UTIL_FORMAT_SWIZZLE_W ?
66 swizzle_format[swizzle_view[i]] : swizzle_view[i];
67 }
68 } else {
69 memcpy(swizzle, swizzle_format, 4);
70 }
71
72 /* Get swizzle. */
73 for (i = 0; i < 4; i++) {
74 switch (swizzle[i]) {
75 case UTIL_FORMAT_SWIZZLE_Y:
76 result |= swizzle_bit[1] << swizzle_shift[i];
77 break;
78 case UTIL_FORMAT_SWIZZLE_Z:
79 result |= swizzle_bit[2] << swizzle_shift[i];
80 break;
81 case UTIL_FORMAT_SWIZZLE_W:
82 result |= swizzle_bit[3] << swizzle_shift[i];
83 break;
84 case UTIL_FORMAT_SWIZZLE_0:
85 result |= R300_TX_FORMAT_ZERO << swizzle_shift[i];
86 break;
87 case UTIL_FORMAT_SWIZZLE_1:
88 result |= R300_TX_FORMAT_ONE << swizzle_shift[i];
89 break;
90 default: /* UTIL_FORMAT_SWIZZLE_X */
91 result |= swizzle_bit[0] << swizzle_shift[i];
92 }
93 }
94 return result;
95 }
96
97 /* Translate a pipe_format into a useful texture format for sampling.
98 *
99 * Some special formats are translated directly using R300_EASY_TX_FORMAT,
100 * but the majority of them is translated in a generic way, automatically
101 * supporting all the formats hw can support.
102 *
103 * R300_EASY_TX_FORMAT swizzles the texture.
104 * Note the signature of R300_EASY_TX_FORMAT:
105 * R300_EASY_TX_FORMAT(B, G, R, A, FORMAT);
106 *
107 * The FORMAT specifies how the texture sampler will treat the texture, and
108 * makes available X, Y, Z, W, ZERO, and ONE for swizzling. */
109 uint32_t r300_translate_texformat(enum pipe_format format,
110 const unsigned char *swizzle_view,
111 boolean is_r500,
112 boolean dxtc_swizzle)
113 {
114 uint32_t result = 0;
115 const struct util_format_description *desc;
116 unsigned i;
117 boolean uniform = TRUE;
118 const uint32_t sign_bit[4] = {
119 R300_TX_FORMAT_SIGNED_X,
120 R300_TX_FORMAT_SIGNED_Y,
121 R300_TX_FORMAT_SIGNED_Z,
122 R300_TX_FORMAT_SIGNED_W,
123 };
124
125 desc = util_format_description(format);
126
127 /* Colorspace (return non-RGB formats directly). */
128 switch (desc->colorspace) {
129 /* Depth stencil formats.
130 * Swizzles are added in r300_merge_textures_and_samplers. */
131 case UTIL_FORMAT_COLORSPACE_ZS:
132 switch (format) {
133 case PIPE_FORMAT_Z16_UNORM:
134 return R300_TX_FORMAT_X16;
135 case PIPE_FORMAT_X8Z24_UNORM:
136 case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
137 if (is_r500)
138 return R500_TX_FORMAT_Y8X24;
139 else
140 return R300_TX_FORMAT_Y16X16;
141 default:
142 return ~0; /* Unsupported. */
143 }
144
145 /* YUV formats. */
146 case UTIL_FORMAT_COLORSPACE_YUV:
147 result |= R300_TX_FORMAT_YUV_TO_RGB;
148
149 switch (format) {
150 case PIPE_FORMAT_UYVY:
151 return R300_EASY_TX_FORMAT(X, Y, Z, ONE, YVYU422) | result;
152 case PIPE_FORMAT_YUYV:
153 return R300_EASY_TX_FORMAT(X, Y, Z, ONE, VYUY422) | result;
154 default:
155 return ~0; /* Unsupported/unknown. */
156 }
157
158 /* Add gamma correction. */
159 case UTIL_FORMAT_COLORSPACE_SRGB:
160 result |= R300_TX_FORMAT_GAMMA;
161 break;
162
163 default:
164 switch (format) {
165 /* Same as YUV but without the YUR->RGB conversion. */
166 case PIPE_FORMAT_R8G8_B8G8_UNORM:
167 return R300_EASY_TX_FORMAT(X, Y, Z, ONE, YVYU422) | result;
168 case PIPE_FORMAT_G8R8_G8B8_UNORM:
169 return R300_EASY_TX_FORMAT(X, Y, Z, ONE, VYUY422) | result;
170 default:;
171 }
172 }
173
174 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 /* S3TC formats. */
188 if (desc->layout == UTIL_FORMAT_LAYOUT_S3TC) {
189 if (!util_format_s3tc_enabled) {
190 return ~0; /* Unsupported. */
191 }
192
193 switch (format) {
194 case PIPE_FORMAT_DXT1_RGB:
195 case PIPE_FORMAT_DXT1_RGBA:
196 case PIPE_FORMAT_DXT1_SRGB:
197 case PIPE_FORMAT_DXT1_SRGBA:
198 return R300_TX_FORMAT_DXT1 | result;
199 case PIPE_FORMAT_DXT3_RGBA:
200 case PIPE_FORMAT_DXT3_SRGBA:
201 return R300_TX_FORMAT_DXT3 | result;
202 case PIPE_FORMAT_DXT5_RGBA:
203 case PIPE_FORMAT_DXT5_SRGBA:
204 return R300_TX_FORMAT_DXT5 | result;
205 default:
206 return ~0; /* Unsupported/unknown. */
207 }
208 }
209
210 /* RGTC formats. */
211 if (desc->layout == UTIL_FORMAT_LAYOUT_RGTC) {
212 switch (format) {
213 case PIPE_FORMAT_RGTC1_SNORM:
214 case PIPE_FORMAT_LATC1_SNORM:
215 result |= sign_bit[1];
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[2] | sign_bit[3];
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 return R300_COLOR_FORMAT_UV88;
408
409 case PIPE_FORMAT_B5G6R5_UNORM:
410 return R300_COLOR_FORMAT_RGB565;
411
412 case PIPE_FORMAT_B5G5R5A1_UNORM:
413 case PIPE_FORMAT_B5G5R5X1_UNORM:
414 return R300_COLOR_FORMAT_ARGB1555;
415
416 case PIPE_FORMAT_B4G4R4A4_UNORM:
417 case PIPE_FORMAT_B4G4R4X4_UNORM:
418 return R300_COLOR_FORMAT_ARGB4444;
419
420 /* 32-bit buffers. */
421 case PIPE_FORMAT_B8G8R8A8_UNORM:
422 /*case PIPE_FORMAT_B8G8R8A8_SNORM:*/
423 case PIPE_FORMAT_B8G8R8X8_UNORM:
424 /*case PIPE_FORMAT_B8G8R8X8_SNORM:*/
425 case PIPE_FORMAT_A8R8G8B8_UNORM:
426 /*case PIPE_FORMAT_A8R8G8B8_SNORM:*/
427 case PIPE_FORMAT_X8R8G8B8_UNORM:
428 /*case PIPE_FORMAT_X8R8G8B8_SNORM:*/
429 case PIPE_FORMAT_A8B8G8R8_UNORM:
430 /*case PIPE_FORMAT_A8B8G8R8_SNORM:*/
431 case PIPE_FORMAT_R8G8B8A8_UNORM:
432 case PIPE_FORMAT_R8G8B8A8_SNORM:
433 case PIPE_FORMAT_X8B8G8R8_UNORM:
434 /*case PIPE_FORMAT_X8B8G8R8_SNORM:*/
435 case PIPE_FORMAT_R8G8B8X8_UNORM:
436 /*case PIPE_FORMAT_R8G8B8X8_SNORM:*/
437 case PIPE_FORMAT_R8SG8SB8UX8U_NORM:
438 return R300_COLOR_FORMAT_ARGB8888;
439
440 case PIPE_FORMAT_R10G10B10A2_UNORM:
441 case PIPE_FORMAT_R10G10B10X2_SNORM:
442 case PIPE_FORMAT_B10G10R10A2_UNORM:
443 case PIPE_FORMAT_R10SG10SB10SA2U_NORM:
444 return R500_COLOR_FORMAT_ARGB2101010; /* R5xx-only? */
445
446 /* 64-bit buffers. */
447 case PIPE_FORMAT_R16G16B16A16_UNORM:
448 case PIPE_FORMAT_R16G16B16A16_SNORM:
449 case PIPE_FORMAT_R16G16B16A16_FLOAT:
450 return R300_COLOR_FORMAT_ARGB16161616;
451
452 /* 128-bit buffers. */
453 case PIPE_FORMAT_R32G32B32A32_FLOAT:
454 return R300_COLOR_FORMAT_ARGB32323232;
455
456 /* YUV buffers. */
457 case PIPE_FORMAT_UYVY:
458 return R300_COLOR_FORMAT_YVYU;
459 case PIPE_FORMAT_YUYV:
460 return R300_COLOR_FORMAT_VYUY;
461 default:
462 return ~0; /* Unsupported. */
463 }
464 }
465
466 /* Depthbuffer and stencilbuffer. Thankfully, we only support two flavors. */
467 static uint32_t r300_translate_zsformat(enum pipe_format format)
468 {
469 switch (format) {
470 /* 16-bit depth, no stencil */
471 case PIPE_FORMAT_Z16_UNORM:
472 return R300_DEPTHFORMAT_16BIT_INT_Z;
473 /* 24-bit depth, ignored stencil */
474 case PIPE_FORMAT_X8Z24_UNORM:
475 /* 24-bit depth, 8-bit stencil */
476 case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
477 return R300_DEPTHFORMAT_24BIT_INT_Z_8BIT_STENCIL;
478 default:
479 return ~0; /* Unsupported. */
480 }
481 }
482
483 /* Shader output formats. This is essentially the swizzle from the shader
484 * to the RB3D block.
485 *
486 * Note that formats are stored from C3 to C0. */
487 static uint32_t r300_translate_out_fmt(enum pipe_format format)
488 {
489 uint32_t modifier = 0;
490 unsigned i;
491 const struct util_format_description *desc;
492 static const uint32_t sign_bit[4] = {
493 R300_OUT_SIGN(0x1),
494 R300_OUT_SIGN(0x2),
495 R300_OUT_SIGN(0x4),
496 R300_OUT_SIGN(0x8),
497 };
498
499 desc = util_format_description(format);
500
501 /* Find the first non-VOID channel. */
502 for (i = 0; i < 4; i++) {
503 if (desc->channel[i].type != UTIL_FORMAT_TYPE_VOID) {
504 break;
505 }
506 }
507
508 if (i == 4)
509 return ~0; /* Unsupported/unknown. */
510
511 /* Specifies how the shader output is written to the fog unit. */
512 if (desc->channel[i].type == UTIL_FORMAT_TYPE_FLOAT) {
513 if (desc->channel[i].size == 32) {
514 modifier |= R300_US_OUT_FMT_C4_32_FP;
515 } else {
516 modifier |= R300_US_OUT_FMT_C4_16_FP;
517 }
518 } else {
519 if (desc->channel[i].size == 16) {
520 modifier |= R300_US_OUT_FMT_C4_16;
521 } else if (desc->channel[i].size == 10) {
522 modifier |= R300_US_OUT_FMT_C4_10;
523 } else {
524 /* C4_8 seems to be used for the formats whose pixel size
525 * is <= 32 bits. */
526 modifier |= R300_US_OUT_FMT_C4_8;
527 }
528 }
529
530 /* Add sign. */
531 for (i = 0; i < 4; i++)
532 if (desc->channel[i].type == UTIL_FORMAT_TYPE_SIGNED) {
533 modifier |= sign_bit[i];
534 }
535
536 /* Add swizzles and return. */
537 switch (format) {
538 /* 8-bit outputs, one channel.
539 * COLORFORMAT_I8 stores the C2 component. */
540 case PIPE_FORMAT_A8_UNORM:
541 /*case PIPE_FORMAT_A8_SNORM:*/
542 return modifier | R300_C2_SEL_A;
543 case PIPE_FORMAT_I8_UNORM:
544 /*case PIPE_FORMAT_I8_SNORM:*/
545 case PIPE_FORMAT_L8_UNORM:
546 /*case PIPE_FORMAT_L8_SNORM:*/
547 case PIPE_FORMAT_R8_UNORM:
548 case PIPE_FORMAT_R8_SNORM:
549 return modifier | R300_C2_SEL_R;
550
551 /* 16-bit outputs, two channels.
552 * COLORFORMAT_UV88 stores C2 and C0. */
553 case PIPE_FORMAT_L8A8_UNORM:
554 /*case PIPE_FORMAT_L8A8_SNORM:*/
555 return modifier | R300_C0_SEL_A | R300_C2_SEL_R;
556 case PIPE_FORMAT_R8G8_UNORM:
557 case PIPE_FORMAT_R8G8_SNORM:
558 return modifier | R300_C0_SEL_G | R300_C2_SEL_R;
559
560 /* BGRA outputs. */
561 case PIPE_FORMAT_B5G6R5_UNORM:
562 case PIPE_FORMAT_B5G5R5A1_UNORM:
563 case PIPE_FORMAT_B5G5R5X1_UNORM:
564 case PIPE_FORMAT_B4G4R4A4_UNORM:
565 case PIPE_FORMAT_B4G4R4X4_UNORM:
566 case PIPE_FORMAT_B8G8R8A8_UNORM:
567 /*case PIPE_FORMAT_B8G8R8A8_SNORM:*/
568 case PIPE_FORMAT_B8G8R8X8_UNORM:
569 /*case PIPE_FORMAT_B8G8R8X8_SNORM:*/
570 case PIPE_FORMAT_B10G10R10A2_UNORM:
571 return modifier |
572 R300_C0_SEL_B | R300_C1_SEL_G |
573 R300_C2_SEL_R | R300_C3_SEL_A;
574
575 /* ARGB outputs. */
576 case PIPE_FORMAT_A8R8G8B8_UNORM:
577 /*case PIPE_FORMAT_A8R8G8B8_SNORM:*/
578 case PIPE_FORMAT_X8R8G8B8_UNORM:
579 /*case PIPE_FORMAT_X8R8G8B8_SNORM:*/
580 return modifier |
581 R300_C0_SEL_A | R300_C1_SEL_R |
582 R300_C2_SEL_G | R300_C3_SEL_B;
583
584 /* ABGR outputs. */
585 case PIPE_FORMAT_A8B8G8R8_UNORM:
586 /*case PIPE_FORMAT_A8B8G8R8_SNORM:*/
587 case PIPE_FORMAT_X8B8G8R8_UNORM:
588 /*case PIPE_FORMAT_X8B8G8R8_SNORM:*/
589 return modifier |
590 R300_C0_SEL_A | R300_C1_SEL_B |
591 R300_C2_SEL_G | R300_C3_SEL_R;
592
593 /* RGBA outputs. */
594 case PIPE_FORMAT_R8G8B8X8_UNORM:
595 /*case PIPE_FORMAT_R8G8B8X8_SNORM:*/
596 case PIPE_FORMAT_R8G8B8A8_UNORM:
597 case PIPE_FORMAT_R8G8B8A8_SNORM:
598 case PIPE_FORMAT_R8SG8SB8UX8U_NORM:
599 case PIPE_FORMAT_R10G10B10A2_UNORM:
600 case PIPE_FORMAT_R10G10B10X2_SNORM:
601 case PIPE_FORMAT_R10SG10SB10SA2U_NORM:
602 case PIPE_FORMAT_R16G16B16A16_UNORM:
603 case PIPE_FORMAT_R16G16B16A16_SNORM:
604 case PIPE_FORMAT_R16G16B16A16_FLOAT:
605 case PIPE_FORMAT_R32G32B32A32_FLOAT:
606 return modifier |
607 R300_C0_SEL_R | R300_C1_SEL_G |
608 R300_C2_SEL_B | R300_C3_SEL_A;
609
610 default:
611 return ~0; /* Unsupported. */
612 }
613 }
614
615 boolean r300_is_colorbuffer_format_supported(enum pipe_format format)
616 {
617 return r300_translate_colorformat(format) != ~0 &&
618 r300_translate_out_fmt(format) != ~0;
619 }
620
621 boolean r300_is_zs_format_supported(enum pipe_format format)
622 {
623 return r300_translate_zsformat(format) != ~0;
624 }
625
626 boolean r300_is_sampler_format_supported(enum pipe_format format)
627 {
628 return r300_translate_texformat(format, 0, TRUE, FALSE) != ~0;
629 }
630
631 void r300_texture_setup_format_state(struct r300_screen *screen,
632 struct r300_resource *tex,
633 unsigned level,
634 struct r300_texture_format_state *out)
635 {
636 struct pipe_resource *pt = &tex->b.b.b;
637 struct r300_texture_desc *desc = &tex->tex;
638 boolean is_r500 = screen->caps.is_r500;
639
640 /* Mask out all the fields we change. */
641 out->format0 = 0;
642 out->format1 &= ~R300_TX_FORMAT_TEX_COORD_TYPE_MASK;
643 out->format2 &= R500_TXFORMAT_MSB;
644 out->tile_config = 0;
645
646 /* Set sampler state. */
647 out->format0 =
648 R300_TX_WIDTH((u_minify(desc->width0, level) - 1) & 0x7ff) |
649 R300_TX_HEIGHT((u_minify(desc->height0, level) - 1) & 0x7ff) |
650 R300_TX_DEPTH(util_logbase2(u_minify(desc->depth0, level)) & 0xf);
651
652 if (desc->uses_stride_addressing) {
653 /* rectangles love this */
654 out->format0 |= R300_TX_PITCH_EN;
655 out->format2 = (desc->stride_in_pixels[level] - 1) & 0x1fff;
656 }
657
658 if (pt->target == PIPE_TEXTURE_CUBE) {
659 out->format1 |= R300_TX_FORMAT_CUBIC_MAP;
660 }
661 if (pt->target == PIPE_TEXTURE_3D) {
662 out->format1 |= R300_TX_FORMAT_3D;
663 }
664
665 /* large textures on r500 */
666 if (is_r500)
667 {
668 if (desc->width0 > 2048) {
669 out->format2 |= R500_TXWIDTH_BIT11;
670 }
671 if (desc->height0 > 2048) {
672 out->format2 |= R500_TXHEIGHT_BIT11;
673 }
674 }
675
676 out->tile_config = R300_TXO_MACRO_TILE(desc->macrotile[level]) |
677 R300_TXO_MICRO_TILE(desc->microtile);
678 }
679
680 static void r300_texture_setup_fb_state(struct r300_surface *surf)
681 {
682 struct r300_resource *tex = r300_resource(surf->base.texture);
683 unsigned level = surf->base.u.tex.level;
684
685 /* Set framebuffer state. */
686 if (util_format_is_depth_or_stencil(surf->base.format)) {
687 surf->pitch =
688 tex->tex.stride_in_pixels[level] |
689 R300_DEPTHMACROTILE(tex->tex.macrotile[level]) |
690 R300_DEPTHMICROTILE(tex->tex.microtile);
691 surf->format = r300_translate_zsformat(surf->base.format);
692 surf->pitch_zmask = tex->tex.zmask_stride_in_pixels[level];
693 surf->pitch_hiz = tex->tex.hiz_stride_in_pixels[level];
694 } else {
695 surf->pitch =
696 tex->tex.stride_in_pixels[level] |
697 r300_translate_colorformat(surf->base.format) |
698 R300_COLOR_TILE(tex->tex.macrotile[level]) |
699 R300_COLOR_MICROTILE(tex->tex.microtile);
700 surf->format = r300_translate_out_fmt(surf->base.format);
701 }
702 }
703
704 boolean r300_resource_set_properties(struct pipe_screen *screen,
705 struct pipe_resource *tex,
706 unsigned offset,
707 const struct pipe_resource *new_properties)
708 {
709 struct r300_screen *rscreen = r300_screen(screen);
710 struct r300_resource *res = r300_resource(tex);
711
712 SCREEN_DBG(rscreen, DBG_TEX,
713 "r300: texture_set_properties: %s -> %s\n",
714 util_format_short_name(tex->format),
715 util_format_short_name(new_properties->format));
716
717 if (!r300_texture_desc_init(rscreen, res, new_properties)) {
718 fprintf(stderr, "r300: ERROR: Cannot set texture properties.\n");
719 return FALSE;
720 }
721 res->tex_offset = offset;
722 r300_texture_setup_format_state(rscreen, res, 0, &res->tx_format);
723
724 return TRUE;
725 }
726
727 static void r300_texture_destroy(struct pipe_screen *screen,
728 struct pipe_resource* texture)
729 {
730 struct r300_resource* tex = (struct r300_resource*)texture;
731
732 r300_winsys_bo_reference(&tex->buf, NULL);
733 FREE(tex);
734 }
735
736 boolean r300_resource_get_handle(struct pipe_screen* screen,
737 struct pipe_resource *texture,
738 struct winsys_handle *whandle)
739 {
740 struct r300_winsys_screen *rws = (struct r300_winsys_screen *)screen->winsys;
741 struct r300_resource* tex = (struct r300_resource*)texture;
742
743 if (!tex) {
744 return FALSE;
745 }
746
747 return rws->buffer_get_handle(tex->buf,
748 tex->tex.stride_in_bytes[0], whandle);
749 }
750
751 static const struct u_resource_vtbl r300_texture_vtbl =
752 {
753 NULL, /* get_handle */
754 r300_texture_destroy, /* resource_destroy */
755 NULL, /* is_resource_referenced */
756 r300_texture_get_transfer, /* get_transfer */
757 r300_texture_transfer_destroy, /* transfer_destroy */
758 r300_texture_transfer_map, /* transfer_map */
759 NULL, /* transfer_flush_region */
760 r300_texture_transfer_unmap, /* transfer_unmap */
761 u_default_transfer_inline_write /* transfer_inline_write */
762 };
763
764 /* The common texture constructor. */
765 static struct r300_resource*
766 r300_texture_create_object(struct r300_screen *rscreen,
767 const struct pipe_resource *base,
768 enum r300_buffer_tiling microtile,
769 enum r300_buffer_tiling macrotile,
770 unsigned stride_in_bytes_override,
771 unsigned max_buffer_size,
772 struct r300_winsys_bo *buffer)
773 {
774 struct r300_winsys_screen *rws = rscreen->rws;
775 struct r300_resource *tex = CALLOC_STRUCT(r300_resource);
776 if (!tex) {
777 if (buffer)
778 r300_winsys_bo_reference(&buffer, NULL);
779 return NULL;
780 }
781
782 pipe_reference_init(&tex->b.b.b.reference, 1);
783 tex->b.b.b.screen = &rscreen->screen;
784 tex->b.b.b.usage = base->usage;
785 tex->b.b.b.bind = base->bind;
786 tex->b.b.b.flags = base->flags;
787 tex->b.b.vtbl = &r300_texture_vtbl;
788 tex->tex.microtile = microtile;
789 tex->tex.macrotile[0] = macrotile;
790 tex->tex.stride_in_bytes_override = stride_in_bytes_override;
791 tex->domain = base->flags & R300_RESOURCE_FLAG_TRANSFER ?
792 R300_DOMAIN_GTT :
793 R300_DOMAIN_VRAM | R300_DOMAIN_GTT;
794 tex->buf_size = max_buffer_size;
795
796 if (!r300_resource_set_properties(&rscreen->screen, &tex->b.b.b, 0, base)) {
797 if (buffer)
798 r300_winsys_bo_reference(&buffer, NULL);
799 FREE(tex);
800 return NULL;
801 }
802
803 /* Create the backing buffer if needed. */
804 if (!buffer) {
805 tex->buf_size = tex->tex.size_in_bytes;
806 tex->buf = rws->buffer_create(rws, tex->tex.size_in_bytes, 2048,
807 base->bind, base->usage, tex->domain);
808
809 if (!tex->buf) {
810 FREE(tex);
811 return NULL;
812 }
813 } else {
814 tex->buf = buffer;
815 }
816
817 tex->cs_buf = rws->buffer_get_cs_handle(tex->buf);
818
819 rws->buffer_set_tiling(tex->buf, NULL,
820 tex->tex.microtile, tex->tex.macrotile[0],
821 tex->tex.stride_in_bytes[0]);
822
823 return tex;
824 }
825
826 /* Create a new texture. */
827 struct pipe_resource *r300_texture_create(struct pipe_screen *screen,
828 const struct pipe_resource *base)
829 {
830 struct r300_screen *rscreen = r300_screen(screen);
831 enum r300_buffer_tiling microtile, macrotile;
832
833 if ((base->flags & R300_RESOURCE_FLAG_TRANSFER) ||
834 (base->bind & PIPE_BIND_SCANOUT)) {
835 microtile = R300_BUFFER_LINEAR;
836 macrotile = R300_BUFFER_LINEAR;
837 } else {
838 microtile = R300_BUFFER_SELECT_LAYOUT;
839 macrotile = R300_BUFFER_SELECT_LAYOUT;
840 }
841
842 return (struct pipe_resource*)
843 r300_texture_create_object(rscreen, base, microtile, macrotile,
844 0, 0, NULL);
845 }
846
847 struct pipe_resource *r300_texture_from_handle(struct pipe_screen *screen,
848 const struct pipe_resource *base,
849 struct winsys_handle *whandle)
850 {
851 struct r300_winsys_screen *rws = (struct r300_winsys_screen*)screen->winsys;
852 struct r300_screen *rscreen = r300_screen(screen);
853 struct r300_winsys_bo *buffer;
854 enum r300_buffer_tiling microtile, macrotile;
855 unsigned stride, size;
856
857 /* Support only 2D textures without mipmaps */
858 if ((base->target != PIPE_TEXTURE_2D &&
859 base->target != PIPE_TEXTURE_RECT) ||
860 base->depth0 != 1 ||
861 base->last_level != 0) {
862 return NULL;
863 }
864
865 buffer = rws->buffer_from_handle(rws, whandle, &stride, &size);
866 if (!buffer)
867 return NULL;
868
869 rws->buffer_get_tiling(buffer, &microtile, &macrotile);
870
871 /* Enforce a microtiled zbuffer. */
872 if (util_format_is_depth_or_stencil(base->format) &&
873 microtile == R300_BUFFER_LINEAR) {
874 switch (util_format_get_blocksize(base->format)) {
875 case 4:
876 microtile = R300_BUFFER_TILED;
877 break;
878
879 case 2:
880 microtile = R300_BUFFER_SQUARETILED;
881 break;
882 }
883 }
884
885 return (struct pipe_resource*)
886 r300_texture_create_object(rscreen, base, microtile, macrotile,
887 stride, size, buffer);
888 }
889
890 /* Not required to implement u_resource_vtbl, consider moving to another file:
891 */
892 struct pipe_surface* r300_create_surface(struct pipe_context * ctx,
893 struct pipe_resource* texture,
894 const struct pipe_surface *surf_tmpl)
895 {
896 struct r300_resource* tex = r300_resource(texture);
897 struct r300_surface* surface = CALLOC_STRUCT(r300_surface);
898 unsigned level = surf_tmpl->u.tex.level;
899
900 assert(surf_tmpl->u.tex.first_layer == surf_tmpl->u.tex.last_layer);
901
902 if (surface) {
903 uint32_t offset, tile_height;
904
905 pipe_reference_init(&surface->base.reference, 1);
906 pipe_resource_reference(&surface->base.texture, texture);
907 surface->base.context = ctx;
908 surface->base.format = surf_tmpl->format;
909 surface->base.width = u_minify(texture->width0, level);
910 surface->base.height = u_minify(texture->height0, level);
911 surface->base.usage = surf_tmpl->usage;
912 surface->base.u.tex.level = level;
913 surface->base.u.tex.first_layer = surf_tmpl->u.tex.first_layer;
914 surface->base.u.tex.last_layer = surf_tmpl->u.tex.last_layer;
915
916 surface->buf = tex->buf;
917 surface->cs_buf = tex->cs_buf;
918
919 /* Prefer VRAM if there are multiple domains to choose from. */
920 surface->domain = tex->domain;
921 if (surface->domain & R300_DOMAIN_VRAM)
922 surface->domain &= ~R300_DOMAIN_GTT;
923
924 surface->offset = r300_texture_get_offset(tex, level,
925 surf_tmpl->u.tex.first_layer);
926 r300_texture_setup_fb_state(surface);
927
928 /* Parameters for the CBZB clear. */
929 surface->cbzb_allowed = tex->tex.cbzb_allowed[level];
930 surface->cbzb_width = align(surface->base.width, 64);
931
932 /* Height must be aligned to the size of a tile. */
933 tile_height = r300_get_pixel_alignment(tex->b.b.b.format,
934 tex->b.b.b.nr_samples,
935 tex->tex.microtile,
936 tex->tex.macrotile[level],
937 DIM_HEIGHT, 0);
938
939 surface->cbzb_height = align((surface->base.height + 1) / 2,
940 tile_height);
941
942 /* Offset must be aligned to 2K and must point at the beginning
943 * of a scanline. */
944 offset = surface->offset +
945 tex->tex.stride_in_bytes[level] * surface->cbzb_height;
946 surface->cbzb_midpoint_offset = offset & ~2047;
947
948 surface->cbzb_pitch = surface->pitch & 0x1ffffc;
949
950 if (util_format_get_blocksizebits(surface->base.format) == 32)
951 surface->cbzb_format = R300_DEPTHFORMAT_24BIT_INT_Z_8BIT_STENCIL;
952 else
953 surface->cbzb_format = R300_DEPTHFORMAT_16BIT_INT_Z;
954
955 DBG(r300_context(ctx), DBG_CBZB,
956 "CBZB Allowed: %s, Dim: %ix%i, Misalignment: %i, Micro: %s, Macro: %s\n",
957 surface->cbzb_allowed ? "YES" : " NO",
958 surface->cbzb_width, surface->cbzb_height,
959 offset & 2047,
960 tex->tex.microtile ? "YES" : " NO",
961 tex->tex.macrotile[level] ? "YES" : " NO");
962 }
963
964 return &surface->base;
965 }
966
967 /* Not required to implement u_resource_vtbl, consider moving to another file:
968 */
969 void r300_surface_destroy(struct pipe_context *ctx, struct pipe_surface* s)
970 {
971 pipe_resource_reference(&s->texture, NULL);
972 FREE(s);
973 }