Merge remote branch 'origin/master' into pipe-video
[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 result |= r300_get_swizzle_combined(desc->swizzle, swizzle_view,
175 util_format_is_compressed(format) && dxtc_swizzle);
176
177 /* S3TC formats. */
178 if (desc->layout == UTIL_FORMAT_LAYOUT_S3TC) {
179 if (!util_format_s3tc_enabled) {
180 return ~0; /* Unsupported. */
181 }
182
183 switch (format) {
184 case PIPE_FORMAT_DXT1_RGB:
185 case PIPE_FORMAT_DXT1_RGBA:
186 case PIPE_FORMAT_DXT1_SRGB:
187 case PIPE_FORMAT_DXT1_SRGBA:
188 return R300_TX_FORMAT_DXT1 | result;
189 case PIPE_FORMAT_DXT3_RGBA:
190 case PIPE_FORMAT_DXT3_SRGBA:
191 return R300_TX_FORMAT_DXT3 | result;
192 case PIPE_FORMAT_DXT5_RGBA:
193 case PIPE_FORMAT_DXT5_SRGBA:
194 return R300_TX_FORMAT_DXT5 | result;
195 default:
196 return ~0; /* Unsupported/unknown. */
197 }
198 }
199
200 /* Add sign. */
201 for (i = 0; i < desc->nr_channels; i++) {
202 if (desc->channel[i].type == UTIL_FORMAT_TYPE_SIGNED) {
203 result |= sign_bit[i];
204 }
205 }
206
207 /* This is truly a special format.
208 * It stores R8G8 and B is computed using sqrt(1 - R^2 - G^2)
209 * in the sampler unit. Also known as D3DFMT_CxV8U8. */
210 if (format == PIPE_FORMAT_R8G8Bx_SNORM) {
211 return R300_TX_FORMAT_CxV8U8 | result;
212 }
213
214 /* RGTC formats. */
215 if (desc->layout == UTIL_FORMAT_LAYOUT_RGTC) {
216 switch (format) {
217 case PIPE_FORMAT_RGTC1_UNORM:
218 case PIPE_FORMAT_RGTC1_SNORM:
219 return R500_TX_FORMAT_ATI1N | result;
220 case PIPE_FORMAT_RGTC2_UNORM:
221 case PIPE_FORMAT_RGTC2_SNORM:
222 return R400_TX_FORMAT_ATI2N | result;
223 default:
224 return ~0; /* Unsupported/unknown. */
225 }
226 }
227
228 /* See whether the components are of the same size. */
229 for (i = 1; i < desc->nr_channels; i++) {
230 uniform = uniform && desc->channel[0].size == desc->channel[i].size;
231 }
232
233 /* Non-uniform formats. */
234 if (!uniform) {
235 switch (desc->nr_channels) {
236 case 3:
237 if (desc->channel[0].size == 5 &&
238 desc->channel[1].size == 6 &&
239 desc->channel[2].size == 5) {
240 return R300_TX_FORMAT_Z5Y6X5 | result;
241 }
242 if (desc->channel[0].size == 5 &&
243 desc->channel[1].size == 5 &&
244 desc->channel[2].size == 6) {
245 return R300_TX_FORMAT_Z6Y5X5 | result;
246 }
247 if (desc->channel[0].size == 2 &&
248 desc->channel[1].size == 3 &&
249 desc->channel[2].size == 3) {
250 return R300_TX_FORMAT_Z3Y3X2 | result;
251 }
252 return ~0; /* Unsupported/unknown. */
253
254 case 4:
255 if (desc->channel[0].size == 5 &&
256 desc->channel[1].size == 5 &&
257 desc->channel[2].size == 5 &&
258 desc->channel[3].size == 1) {
259 return R300_TX_FORMAT_W1Z5Y5X5 | result;
260 }
261 if (desc->channel[0].size == 10 &&
262 desc->channel[1].size == 10 &&
263 desc->channel[2].size == 10 &&
264 desc->channel[3].size == 2) {
265 return R300_TX_FORMAT_W2Z10Y10X10 | result;
266 }
267 }
268 return ~0; /* Unsupported/unknown. */
269 }
270
271 /* Find the first non-VOID channel. */
272 for (i = 0; i < 4; i++) {
273 if (desc->channel[i].type != UTIL_FORMAT_TYPE_VOID) {
274 break;
275 }
276 }
277
278 if (i == 4)
279 return ~0; /* Unsupported/unknown. */
280
281 /* And finally, uniform formats. */
282 switch (desc->channel[i].type) {
283 case UTIL_FORMAT_TYPE_UNSIGNED:
284 case UTIL_FORMAT_TYPE_SIGNED:
285 if (!desc->channel[i].normalized &&
286 desc->colorspace != UTIL_FORMAT_COLORSPACE_SRGB) {
287 return ~0;
288 }
289
290 switch (desc->channel[i].size) {
291 case 4:
292 switch (desc->nr_channels) {
293 case 2:
294 return R300_TX_FORMAT_Y4X4 | result;
295 case 4:
296 return R300_TX_FORMAT_W4Z4Y4X4 | result;
297 }
298 return ~0;
299
300 case 8:
301 switch (desc->nr_channels) {
302 case 1:
303 return R300_TX_FORMAT_X8 | result;
304 case 2:
305 return R300_TX_FORMAT_Y8X8 | result;
306 case 4:
307 return R300_TX_FORMAT_W8Z8Y8X8 | result;
308 }
309 return ~0;
310
311 case 16:
312 switch (desc->nr_channels) {
313 case 1:
314 return R300_TX_FORMAT_X16 | result;
315 case 2:
316 return R300_TX_FORMAT_Y16X16 | result;
317 case 4:
318 return R300_TX_FORMAT_W16Z16Y16X16 | result;
319 }
320 }
321 return ~0;
322
323 case UTIL_FORMAT_TYPE_FLOAT:
324 switch (desc->channel[i].size) {
325 case 16:
326 switch (desc->nr_channels) {
327 case 1:
328 return R300_TX_FORMAT_16F | result;
329 case 2:
330 return R300_TX_FORMAT_16F_16F | result;
331 case 4:
332 return R300_TX_FORMAT_16F_16F_16F_16F | result;
333 }
334 return ~0;
335
336 case 32:
337 switch (desc->nr_channels) {
338 case 1:
339 return R300_TX_FORMAT_32F | result;
340 case 2:
341 return R300_TX_FORMAT_32F_32F | result;
342 case 4:
343 return R300_TX_FORMAT_32F_32F_32F_32F | result;
344 }
345 }
346 }
347
348 return ~0; /* Unsupported/unknown. */
349 }
350
351 uint32_t r500_tx_format_msb_bit(enum pipe_format format)
352 {
353 switch (format) {
354 case PIPE_FORMAT_RGTC1_UNORM:
355 case PIPE_FORMAT_RGTC1_SNORM:
356 case PIPE_FORMAT_X8Z24_UNORM:
357 case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
358 return R500_TXFORMAT_MSB;
359 default:
360 return 0;
361 }
362 }
363
364 /* Buffer formats. */
365
366 /* Colorbuffer formats. This is the unswizzled format of the RB3D block's
367 * output. For the swizzling of the targets, check the shader's format. */
368 static uint32_t r300_translate_colorformat(enum pipe_format format)
369 {
370 switch (format) {
371 /* 8-bit buffers. */
372 case PIPE_FORMAT_A8_UNORM:
373 /*case PIPE_FORMAT_A8_SNORM:*/
374 case PIPE_FORMAT_I8_UNORM:
375 /*case PIPE_FORMAT_I8_SNORM:*/
376 case PIPE_FORMAT_L8_UNORM:
377 /*case PIPE_FORMAT_L8_SNORM:*/
378 case PIPE_FORMAT_R8_UNORM:
379 case PIPE_FORMAT_R8_SNORM:
380 return R300_COLOR_FORMAT_I8;
381
382 /* 16-bit buffers. */
383 case PIPE_FORMAT_L8A8_UNORM:
384 /*case PIPE_FORMAT_L8A8_SNORM:*/
385 case PIPE_FORMAT_R8G8_UNORM:
386 case PIPE_FORMAT_R8G8_SNORM:
387 return R300_COLOR_FORMAT_UV88;
388
389 case PIPE_FORMAT_B5G6R5_UNORM:
390 return R300_COLOR_FORMAT_RGB565;
391
392 case PIPE_FORMAT_B5G5R5A1_UNORM:
393 case PIPE_FORMAT_B5G5R5X1_UNORM:
394 return R300_COLOR_FORMAT_ARGB1555;
395
396 case PIPE_FORMAT_B4G4R4A4_UNORM:
397 case PIPE_FORMAT_B4G4R4X4_UNORM:
398 return R300_COLOR_FORMAT_ARGB4444;
399
400 /* 32-bit buffers. */
401 case PIPE_FORMAT_B8G8R8A8_UNORM:
402 /*case PIPE_FORMAT_B8G8R8A8_SNORM:*/
403 case PIPE_FORMAT_B8G8R8X8_UNORM:
404 /*case PIPE_FORMAT_B8G8R8X8_SNORM:*/
405 case PIPE_FORMAT_A8R8G8B8_UNORM:
406 /*case PIPE_FORMAT_A8R8G8B8_SNORM:*/
407 case PIPE_FORMAT_X8R8G8B8_UNORM:
408 /*case PIPE_FORMAT_X8R8G8B8_SNORM:*/
409 case PIPE_FORMAT_A8B8G8R8_UNORM:
410 /*case PIPE_FORMAT_A8B8G8R8_SNORM:*/
411 case PIPE_FORMAT_R8G8B8A8_UNORM:
412 case PIPE_FORMAT_R8G8B8A8_SNORM:
413 case PIPE_FORMAT_X8B8G8R8_UNORM:
414 /*case PIPE_FORMAT_X8B8G8R8_SNORM:*/
415 case PIPE_FORMAT_R8G8B8X8_UNORM:
416 /*case PIPE_FORMAT_R8G8B8X8_SNORM:*/
417 case PIPE_FORMAT_R8SG8SB8UX8U_NORM:
418 return R300_COLOR_FORMAT_ARGB8888;
419
420 case PIPE_FORMAT_R10G10B10A2_UNORM:
421 case PIPE_FORMAT_R10G10B10X2_SNORM:
422 case PIPE_FORMAT_B10G10R10A2_UNORM:
423 case PIPE_FORMAT_R10SG10SB10SA2U_NORM:
424 return R500_COLOR_FORMAT_ARGB2101010; /* R5xx-only? */
425
426 /* 64-bit buffers. */
427 case PIPE_FORMAT_R16G16B16A16_UNORM:
428 case PIPE_FORMAT_R16G16B16A16_SNORM:
429 case PIPE_FORMAT_R16G16B16A16_FLOAT:
430 return R300_COLOR_FORMAT_ARGB16161616;
431
432 /* 128-bit buffers. */
433 case PIPE_FORMAT_R32G32B32A32_FLOAT:
434 return R300_COLOR_FORMAT_ARGB32323232;
435
436 /* YUV buffers. */
437 case PIPE_FORMAT_UYVY:
438 return R300_COLOR_FORMAT_YVYU;
439 case PIPE_FORMAT_YUYV:
440 return R300_COLOR_FORMAT_VYUY;
441 default:
442 return ~0; /* Unsupported. */
443 }
444 }
445
446 /* Depthbuffer and stencilbuffer. Thankfully, we only support two flavors. */
447 static uint32_t r300_translate_zsformat(enum pipe_format format)
448 {
449 switch (format) {
450 /* 16-bit depth, no stencil */
451 case PIPE_FORMAT_Z16_UNORM:
452 return R300_DEPTHFORMAT_16BIT_INT_Z;
453 /* 24-bit depth, ignored stencil */
454 case PIPE_FORMAT_X8Z24_UNORM:
455 /* 24-bit depth, 8-bit stencil */
456 case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
457 return R300_DEPTHFORMAT_24BIT_INT_Z_8BIT_STENCIL;
458 default:
459 return ~0; /* Unsupported. */
460 }
461 }
462
463 /* Shader output formats. This is essentially the swizzle from the shader
464 * to the RB3D block.
465 *
466 * Note that formats are stored from C3 to C0. */
467 static uint32_t r300_translate_out_fmt(enum pipe_format format)
468 {
469 uint32_t modifier = 0;
470 unsigned i;
471 const struct util_format_description *desc;
472 static const uint32_t sign_bit[4] = {
473 R300_OUT_SIGN(0x1),
474 R300_OUT_SIGN(0x2),
475 R300_OUT_SIGN(0x4),
476 R300_OUT_SIGN(0x8),
477 };
478
479 desc = util_format_description(format);
480
481 /* Find the first non-VOID channel. */
482 for (i = 0; i < 4; i++) {
483 if (desc->channel[i].type != UTIL_FORMAT_TYPE_VOID) {
484 break;
485 }
486 }
487
488 if (i == 4)
489 return ~0; /* Unsupported/unknown. */
490
491 /* Specifies how the shader output is written to the fog unit. */
492 if (desc->channel[i].type == UTIL_FORMAT_TYPE_FLOAT) {
493 if (desc->channel[i].size == 32) {
494 modifier |= R300_US_OUT_FMT_C4_32_FP;
495 } else {
496 modifier |= R300_US_OUT_FMT_C4_16_FP;
497 }
498 } else {
499 if (desc->channel[i].size == 16) {
500 modifier |= R300_US_OUT_FMT_C4_16;
501 } else if (desc->channel[i].size == 10) {
502 modifier |= R300_US_OUT_FMT_C4_10;
503 } else {
504 /* C4_8 seems to be used for the formats whose pixel size
505 * is <= 32 bits. */
506 modifier |= R300_US_OUT_FMT_C4_8;
507 }
508 }
509
510 /* Add sign. */
511 for (i = 0; i < 4; i++)
512 if (desc->channel[i].type == UTIL_FORMAT_TYPE_SIGNED) {
513 modifier |= sign_bit[i];
514 }
515
516 /* Add swizzles and return. */
517 switch (format) {
518 /* 8-bit outputs, one channel.
519 * COLORFORMAT_I8 stores the C2 component. */
520 case PIPE_FORMAT_A8_UNORM:
521 /*case PIPE_FORMAT_A8_SNORM:*/
522 return modifier | R300_C2_SEL_A;
523 case PIPE_FORMAT_I8_UNORM:
524 /*case PIPE_FORMAT_I8_SNORM:*/
525 case PIPE_FORMAT_L8_UNORM:
526 /*case PIPE_FORMAT_L8_SNORM:*/
527 case PIPE_FORMAT_R8_UNORM:
528 case PIPE_FORMAT_R8_SNORM:
529 return modifier | R300_C2_SEL_R;
530
531 /* 16-bit outputs, two channels.
532 * COLORFORMAT_UV88 stores C2 and C0. */
533 case PIPE_FORMAT_L8A8_UNORM:
534 /*case PIPE_FORMAT_L8A8_SNORM:*/
535 return modifier | R300_C0_SEL_A | R300_C2_SEL_R;
536 case PIPE_FORMAT_R8G8_UNORM:
537 case PIPE_FORMAT_R8G8_SNORM:
538 return modifier | R300_C0_SEL_G | R300_C2_SEL_R;
539
540 /* BGRA outputs. */
541 case PIPE_FORMAT_B5G6R5_UNORM:
542 case PIPE_FORMAT_B5G5R5A1_UNORM:
543 case PIPE_FORMAT_B5G5R5X1_UNORM:
544 case PIPE_FORMAT_B4G4R4A4_UNORM:
545 case PIPE_FORMAT_B4G4R4X4_UNORM:
546 case PIPE_FORMAT_B8G8R8A8_UNORM:
547 /*case PIPE_FORMAT_B8G8R8A8_SNORM:*/
548 case PIPE_FORMAT_B8G8R8X8_UNORM:
549 /*case PIPE_FORMAT_B8G8R8X8_SNORM:*/
550 case PIPE_FORMAT_B10G10R10A2_UNORM:
551 return modifier |
552 R300_C0_SEL_B | R300_C1_SEL_G |
553 R300_C2_SEL_R | R300_C3_SEL_A;
554
555 /* ARGB outputs. */
556 case PIPE_FORMAT_A8R8G8B8_UNORM:
557 /*case PIPE_FORMAT_A8R8G8B8_SNORM:*/
558 case PIPE_FORMAT_X8R8G8B8_UNORM:
559 /*case PIPE_FORMAT_X8R8G8B8_SNORM:*/
560 return modifier |
561 R300_C0_SEL_A | R300_C1_SEL_R |
562 R300_C2_SEL_G | R300_C3_SEL_B;
563
564 /* ABGR outputs. */
565 case PIPE_FORMAT_A8B8G8R8_UNORM:
566 /*case PIPE_FORMAT_A8B8G8R8_SNORM:*/
567 case PIPE_FORMAT_X8B8G8R8_UNORM:
568 /*case PIPE_FORMAT_X8B8G8R8_SNORM:*/
569 return modifier |
570 R300_C0_SEL_A | R300_C1_SEL_B |
571 R300_C2_SEL_G | R300_C3_SEL_R;
572
573 /* RGBA outputs. */
574 case PIPE_FORMAT_R8G8B8X8_UNORM:
575 /*case PIPE_FORMAT_R8G8B8X8_SNORM:*/
576 case PIPE_FORMAT_R8G8B8A8_UNORM:
577 case PIPE_FORMAT_R8G8B8A8_SNORM:
578 case PIPE_FORMAT_R8SG8SB8UX8U_NORM:
579 case PIPE_FORMAT_R10G10B10A2_UNORM:
580 case PIPE_FORMAT_R10G10B10X2_SNORM:
581 case PIPE_FORMAT_R10SG10SB10SA2U_NORM:
582 case PIPE_FORMAT_R16G16B16A16_UNORM:
583 case PIPE_FORMAT_R16G16B16A16_SNORM:
584 case PIPE_FORMAT_R16G16B16A16_FLOAT:
585 case PIPE_FORMAT_R32G32B32A32_FLOAT:
586 return modifier |
587 R300_C0_SEL_R | R300_C1_SEL_G |
588 R300_C2_SEL_B | R300_C3_SEL_A;
589
590 default:
591 return ~0; /* Unsupported. */
592 }
593 }
594
595 boolean r300_is_colorbuffer_format_supported(enum pipe_format format)
596 {
597 return r300_translate_colorformat(format) != ~0 &&
598 r300_translate_out_fmt(format) != ~0;
599 }
600
601 boolean r300_is_zs_format_supported(enum pipe_format format)
602 {
603 return r300_translate_zsformat(format) != ~0;
604 }
605
606 boolean r300_is_sampler_format_supported(enum pipe_format format)
607 {
608 return r300_translate_texformat(format, 0, TRUE, FALSE) != ~0;
609 }
610
611 void r300_texture_setup_format_state(struct r300_screen *screen,
612 struct r300_resource *tex,
613 unsigned level,
614 struct r300_texture_format_state *out)
615 {
616 struct pipe_resource *pt = &tex->b.b.b;
617 struct r300_texture_desc *desc = &tex->tex;
618 boolean is_r500 = screen->caps.is_r500;
619
620 /* Mask out all the fields we change. */
621 out->format0 = 0;
622 out->format1 &= ~R300_TX_FORMAT_TEX_COORD_TYPE_MASK;
623 out->format2 &= R500_TXFORMAT_MSB;
624 out->tile_config = 0;
625
626 /* Set sampler state. */
627 out->format0 =
628 R300_TX_WIDTH((u_minify(desc->width0, level) - 1) & 0x7ff) |
629 R300_TX_HEIGHT((u_minify(desc->height0, level) - 1) & 0x7ff) |
630 R300_TX_DEPTH(util_logbase2(u_minify(desc->depth0, level)) & 0xf);
631
632 if (desc->uses_stride_addressing) {
633 /* rectangles love this */
634 out->format0 |= R300_TX_PITCH_EN;
635 out->format2 = (desc->stride_in_pixels[level] - 1) & 0x1fff;
636 }
637
638 if (pt->target == PIPE_TEXTURE_CUBE) {
639 out->format1 |= R300_TX_FORMAT_CUBIC_MAP;
640 }
641 if (pt->target == PIPE_TEXTURE_3D) {
642 out->format1 |= R300_TX_FORMAT_3D;
643 }
644
645 /* large textures on r500 */
646 if (is_r500)
647 {
648 if (desc->width0 > 2048) {
649 out->format2 |= R500_TXWIDTH_BIT11;
650 }
651 if (desc->height0 > 2048) {
652 out->format2 |= R500_TXHEIGHT_BIT11;
653 }
654 }
655
656 out->tile_config = R300_TXO_MACRO_TILE(desc->macrotile[level]) |
657 R300_TXO_MICRO_TILE(desc->microtile);
658 }
659
660 static void r300_texture_setup_fb_state(struct r300_surface *surf)
661 {
662 struct r300_resource *tex = r300_resource(surf->base.texture);
663 unsigned level = surf->base.u.tex.level;
664
665 /* Set framebuffer state. */
666 if (util_format_is_depth_or_stencil(surf->base.format)) {
667 surf->pitch =
668 tex->tex.stride_in_pixels[level] |
669 R300_DEPTHMACROTILE(tex->tex.macrotile[level]) |
670 R300_DEPTHMICROTILE(tex->tex.microtile);
671 surf->format = r300_translate_zsformat(surf->base.format);
672 } else {
673 surf->pitch =
674 tex->tex.stride_in_pixels[level] |
675 r300_translate_colorformat(surf->base.format) |
676 R300_COLOR_TILE(tex->tex.macrotile[level]) |
677 R300_COLOR_MICROTILE(tex->tex.microtile);
678 surf->format = r300_translate_out_fmt(surf->base.format);
679 }
680 }
681
682 boolean r300_resource_set_properties(struct pipe_screen *screen,
683 struct pipe_resource *tex,
684 unsigned offset,
685 const struct pipe_resource *new_properties)
686 {
687 struct r300_screen *rscreen = r300_screen(screen);
688 struct r300_resource *res = r300_resource(tex);
689
690 SCREEN_DBG(rscreen, DBG_TEX,
691 "r300: texture_set_properties: %s -> %s\n",
692 util_format_short_name(tex->format),
693 util_format_short_name(new_properties->format));
694
695 if (!r300_texture_desc_init(rscreen, res, new_properties)) {
696 fprintf(stderr, "r300: ERROR: Cannot set texture properties.\n");
697 return FALSE;
698 }
699 res->tex_offset = offset;
700 r300_texture_setup_format_state(rscreen, res, 0, &res->tx_format);
701
702 return TRUE;
703 }
704
705 static void r300_texture_destroy(struct pipe_screen *screen,
706 struct pipe_resource* texture)
707 {
708 struct r300_resource* tex = (struct r300_resource*)texture;
709 int i;
710
711 r300_winsys_bo_reference(&tex->buf, NULL);
712 for (i = 0; i < R300_MAX_TEXTURE_LEVELS; i++) {
713 if (tex->hiz_mem[i])
714 u_mmFreeMem(tex->hiz_mem[i]);
715 }
716
717 FREE(tex);
718 }
719
720 boolean r300_resource_get_handle(struct pipe_screen* screen,
721 struct pipe_resource *texture,
722 struct winsys_handle *whandle)
723 {
724 struct r300_winsys_screen *rws = (struct r300_winsys_screen *)screen->winsys;
725 struct r300_resource* tex = (struct r300_resource*)texture;
726
727 if (!tex) {
728 return FALSE;
729 }
730
731 return rws->buffer_get_handle(tex->buf,
732 tex->tex.stride_in_bytes[0], whandle);
733 }
734
735 static const struct u_resource_vtbl r300_texture_vtbl =
736 {
737 NULL, /* get_handle */
738 r300_texture_destroy, /* resource_destroy */
739 NULL, /* is_resource_referenced */
740 r300_texture_get_transfer, /* get_transfer */
741 r300_texture_transfer_destroy, /* transfer_destroy */
742 r300_texture_transfer_map, /* transfer_map */
743 NULL, /* transfer_flush_region */
744 r300_texture_transfer_unmap, /* transfer_unmap */
745 u_default_transfer_inline_write /* transfer_inline_write */
746 };
747
748 /* The common texture constructor. */
749 static struct r300_resource*
750 r300_texture_create_object(struct r300_screen *rscreen,
751 const struct pipe_resource *base,
752 enum r300_buffer_tiling microtile,
753 enum r300_buffer_tiling macrotile,
754 unsigned stride_in_bytes_override,
755 unsigned max_buffer_size,
756 struct r300_winsys_bo *buffer)
757 {
758 struct r300_winsys_screen *rws = rscreen->rws;
759 struct r300_resource *tex = CALLOC_STRUCT(r300_resource);
760 if (!tex) {
761 if (buffer)
762 r300_winsys_bo_reference(&buffer, NULL);
763 return NULL;
764 }
765
766 pipe_reference_init(&tex->b.b.b.reference, 1);
767 tex->b.b.b.screen = &rscreen->screen;
768 tex->b.b.b.usage = base->usage;
769 tex->b.b.b.bind = base->bind;
770 tex->b.b.b.flags = base->flags;
771 tex->b.b.vtbl = &r300_texture_vtbl;
772 tex->tex.microtile = microtile;
773 tex->tex.macrotile[0] = macrotile;
774 tex->tex.stride_in_bytes_override = stride_in_bytes_override;
775 tex->domain = base->flags & R300_RESOURCE_FLAG_TRANSFER ?
776 R300_DOMAIN_GTT :
777 R300_DOMAIN_VRAM | R300_DOMAIN_GTT;
778 tex->buf_size = max_buffer_size;
779
780 if (!r300_resource_set_properties(&rscreen->screen, &tex->b.b.b, 0, base)) {
781 if (buffer)
782 r300_winsys_bo_reference(&buffer, NULL);
783 FREE(tex);
784 return NULL;
785 }
786
787 /* Create the backing buffer if needed. */
788 if (!buffer) {
789 tex->buf_size = tex->tex.size_in_bytes;
790 tex->buf = rws->buffer_create(rws, tex->tex.size_in_bytes, 2048,
791 base->bind, base->usage, tex->domain);
792
793 if (!tex->buf) {
794 FREE(tex);
795 return NULL;
796 }
797 } else {
798 tex->buf = buffer;
799 }
800
801 tex->cs_buf = rws->buffer_get_cs_handle(tex->buf);
802
803 rws->buffer_set_tiling(tex->buf, NULL,
804 tex->tex.microtile, tex->tex.macrotile[0],
805 tex->tex.stride_in_bytes[0]);
806
807 return tex;
808 }
809
810 /* Create a new texture. */
811 struct pipe_resource *r300_texture_create(struct pipe_screen *screen,
812 const struct pipe_resource *base)
813 {
814 struct r300_screen *rscreen = r300_screen(screen);
815 enum r300_buffer_tiling microtile, macrotile;
816
817 if ((base->flags & R300_RESOURCE_FLAG_TRANSFER) ||
818 (base->bind & PIPE_BIND_SCANOUT)) {
819 microtile = R300_BUFFER_LINEAR;
820 macrotile = R300_BUFFER_LINEAR;
821 } else {
822 microtile = R300_BUFFER_SELECT_LAYOUT;
823 macrotile = R300_BUFFER_SELECT_LAYOUT;
824 }
825
826 return (struct pipe_resource*)
827 r300_texture_create_object(rscreen, base, microtile, macrotile,
828 0, 0, NULL);
829 }
830
831 struct pipe_resource *r300_texture_from_handle(struct pipe_screen *screen,
832 const struct pipe_resource *base,
833 struct winsys_handle *whandle)
834 {
835 struct r300_winsys_screen *rws = (struct r300_winsys_screen*)screen->winsys;
836 struct r300_screen *rscreen = r300_screen(screen);
837 struct r300_winsys_bo *buffer;
838 enum r300_buffer_tiling microtile, macrotile;
839 unsigned stride, size;
840
841 /* Support only 2D textures without mipmaps */
842 if ((base->target != PIPE_TEXTURE_2D &&
843 base->target != PIPE_TEXTURE_RECT) ||
844 base->depth0 != 1 ||
845 base->last_level != 0) {
846 return NULL;
847 }
848
849 buffer = rws->buffer_from_handle(rws, whandle, &stride, &size);
850 if (!buffer)
851 return NULL;
852
853 rws->buffer_get_tiling(buffer, &microtile, &macrotile);
854
855 /* Enforce a microtiled zbuffer. */
856 if (util_format_is_depth_or_stencil(base->format) &&
857 microtile == R300_BUFFER_LINEAR) {
858 switch (util_format_get_blocksize(base->format)) {
859 case 4:
860 microtile = R300_BUFFER_TILED;
861 break;
862
863 case 2:
864 if (rws->get_value(rws, R300_VID_DRM_2_1_0))
865 microtile = R300_BUFFER_SQUARETILED;
866 break;
867 }
868 }
869
870 return (struct pipe_resource*)
871 r300_texture_create_object(rscreen, base, microtile, macrotile,
872 stride, size, buffer);
873 }
874
875 /* Not required to implement u_resource_vtbl, consider moving to another file:
876 */
877 struct pipe_surface* r300_create_surface(struct pipe_context * ctx,
878 struct pipe_resource* texture,
879 const struct pipe_surface *surf_tmpl)
880 {
881 struct r300_resource* tex = r300_resource(texture);
882 struct r300_surface* surface = CALLOC_STRUCT(r300_surface);
883 unsigned level = surf_tmpl->u.tex.level;
884
885 assert(surf_tmpl->u.tex.first_layer == surf_tmpl->u.tex.last_layer);
886
887 if (surface) {
888 uint32_t offset, tile_height;
889
890 pipe_reference_init(&surface->base.reference, 1);
891 pipe_resource_reference(&surface->base.texture, texture);
892 surface->base.context = ctx;
893 surface->base.format = surf_tmpl->format;
894 surface->base.width = u_minify(texture->width0, level);
895 surface->base.height = u_minify(texture->height0, level);
896 surface->base.usage = surf_tmpl->usage;
897 surface->base.u.tex.level = level;
898 surface->base.u.tex.first_layer = surf_tmpl->u.tex.first_layer;
899 surface->base.u.tex.last_layer = surf_tmpl->u.tex.last_layer;
900
901 surface->buf = tex->buf;
902 surface->cs_buf = tex->cs_buf;
903
904 /* Prefer VRAM if there are multiple domains to choose from. */
905 surface->domain = tex->domain;
906 if (surface->domain & R300_DOMAIN_VRAM)
907 surface->domain &= ~R300_DOMAIN_GTT;
908
909 surface->offset = r300_texture_get_offset(tex, level,
910 surf_tmpl->u.tex.first_layer);
911 r300_texture_setup_fb_state(surface);
912
913 /* Parameters for the CBZB clear. */
914 surface->cbzb_allowed = tex->tex.cbzb_allowed[level];
915 surface->cbzb_width = align(surface->base.width, 64);
916
917 /* Height must be aligned to the size of a tile. */
918 tile_height = r300_get_pixel_alignment(tex->b.b.b.format,
919 tex->b.b.b.nr_samples,
920 tex->tex.microtile,
921 tex->tex.macrotile[level],
922 DIM_HEIGHT, 0);
923
924 surface->cbzb_height = align((surface->base.height + 1) / 2,
925 tile_height);
926
927 /* Offset must be aligned to 2K and must point at the beginning
928 * of a scanline. */
929 offset = surface->offset +
930 tex->tex.stride_in_bytes[level] * surface->cbzb_height;
931 surface->cbzb_midpoint_offset = offset & ~2047;
932
933 surface->cbzb_pitch = surface->pitch & 0x1ffffc;
934
935 if (util_format_get_blocksizebits(surface->base.format) == 32)
936 surface->cbzb_format = R300_DEPTHFORMAT_24BIT_INT_Z_8BIT_STENCIL;
937 else
938 surface->cbzb_format = R300_DEPTHFORMAT_16BIT_INT_Z;
939
940 DBG(r300_context(ctx), DBG_CBZB,
941 "CBZB Allowed: %s, Dim: %ix%i, Misalignment: %i, Micro: %s, Macro: %s\n",
942 surface->cbzb_allowed ? "YES" : " NO",
943 surface->cbzb_width, surface->cbzb_height,
944 offset & 2047,
945 tex->tex.microtile ? "YES" : " NO",
946 tex->tex.macrotile[level] ? "YES" : " NO");
947 }
948
949 return &surface->base;
950 }
951
952 /* Not required to implement u_resource_vtbl, consider moving to another file:
953 */
954 void r300_surface_destroy(struct pipe_context *ctx, struct pipe_surface* s)
955 {
956 pipe_resource_reference(&s->texture, NULL);
957 FREE(s);
958 }