r300g: support sRGB colorbuffers
[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_L8_SRGB:
379 case PIPE_FORMAT_R8_UNORM:
380 case PIPE_FORMAT_R8_SNORM:
381 return R300_COLOR_FORMAT_I8;
382
383 /* 16-bit buffers. */
384 case PIPE_FORMAT_L8A8_UNORM:
385 /*case PIPE_FORMAT_L8A8_SNORM:*/
386 case PIPE_FORMAT_L8A8_SRGB:
387 case PIPE_FORMAT_R8G8_UNORM:
388 case PIPE_FORMAT_R8G8_SNORM:
389 return R300_COLOR_FORMAT_UV88;
390
391 case PIPE_FORMAT_B5G6R5_UNORM:
392 return R300_COLOR_FORMAT_RGB565;
393
394 case PIPE_FORMAT_B5G5R5A1_UNORM:
395 case PIPE_FORMAT_B5G5R5X1_UNORM:
396 return R300_COLOR_FORMAT_ARGB1555;
397
398 case PIPE_FORMAT_B4G4R4A4_UNORM:
399 case PIPE_FORMAT_B4G4R4X4_UNORM:
400 return R300_COLOR_FORMAT_ARGB4444;
401
402 /* 32-bit buffers. */
403 case PIPE_FORMAT_B8G8R8A8_UNORM:
404 /*case PIPE_FORMAT_B8G8R8A8_SNORM:*/
405 case PIPE_FORMAT_B8G8R8A8_SRGB:
406 case PIPE_FORMAT_B8G8R8X8_UNORM:
407 /*case PIPE_FORMAT_B8G8R8X8_SNORM:*/
408 case PIPE_FORMAT_B8G8R8X8_SRGB:
409 case PIPE_FORMAT_A8R8G8B8_UNORM:
410 /*case PIPE_FORMAT_A8R8G8B8_SNORM:*/
411 case PIPE_FORMAT_A8R8G8B8_SRGB:
412 case PIPE_FORMAT_X8R8G8B8_UNORM:
413 /*case PIPE_FORMAT_X8R8G8B8_SNORM:*/
414 case PIPE_FORMAT_X8R8G8B8_SRGB:
415 case PIPE_FORMAT_A8B8G8R8_UNORM:
416 /*case PIPE_FORMAT_A8B8G8R8_SNORM:*/
417 case PIPE_FORMAT_A8B8G8R8_SRGB:
418 case PIPE_FORMAT_R8G8B8A8_UNORM:
419 case PIPE_FORMAT_R8G8B8A8_SNORM:
420 case PIPE_FORMAT_R8G8B8A8_SRGB:
421 case PIPE_FORMAT_X8B8G8R8_UNORM:
422 /*case PIPE_FORMAT_X8B8G8R8_SNORM:*/
423 case PIPE_FORMAT_X8B8G8R8_SRGB:
424 case PIPE_FORMAT_R8G8B8X8_UNORM:
425 /*case PIPE_FORMAT_R8G8B8X8_SNORM:*/
426 /*case PIPE_FORMAT_R8G8B8X8_SRGB:*/
427 case PIPE_FORMAT_R8SG8SB8UX8U_NORM:
428 return R300_COLOR_FORMAT_ARGB8888;
429
430 case PIPE_FORMAT_R10G10B10A2_UNORM:
431 case PIPE_FORMAT_R10G10B10X2_SNORM:
432 case PIPE_FORMAT_B10G10R10A2_UNORM:
433 case PIPE_FORMAT_R10SG10SB10SA2U_NORM:
434 return R500_COLOR_FORMAT_ARGB2101010; /* R5xx-only? */
435
436 /* 64-bit buffers. */
437 case PIPE_FORMAT_R16G16B16A16_UNORM:
438 case PIPE_FORMAT_R16G16B16A16_SNORM:
439 case PIPE_FORMAT_R16G16B16A16_FLOAT:
440 return R300_COLOR_FORMAT_ARGB16161616;
441
442 /* 128-bit buffers. */
443 case PIPE_FORMAT_R32G32B32A32_FLOAT:
444 return R300_COLOR_FORMAT_ARGB32323232;
445
446 /* YUV buffers. */
447 case PIPE_FORMAT_UYVY:
448 return R300_COLOR_FORMAT_YVYU;
449 case PIPE_FORMAT_YUYV:
450 return R300_COLOR_FORMAT_VYUY;
451 default:
452 return ~0; /* Unsupported. */
453 }
454 }
455
456 /* Depthbuffer and stencilbuffer. Thankfully, we only support two flavors. */
457 static uint32_t r300_translate_zsformat(enum pipe_format format)
458 {
459 switch (format) {
460 /* 16-bit depth, no stencil */
461 case PIPE_FORMAT_Z16_UNORM:
462 return R300_DEPTHFORMAT_16BIT_INT_Z;
463 /* 24-bit depth, ignored stencil */
464 case PIPE_FORMAT_X8Z24_UNORM:
465 /* 24-bit depth, 8-bit stencil */
466 case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
467 return R300_DEPTHFORMAT_24BIT_INT_Z_8BIT_STENCIL;
468 default:
469 return ~0; /* Unsupported. */
470 }
471 }
472
473 /* Shader output formats. This is essentially the swizzle from the shader
474 * to the RB3D block.
475 *
476 * Note that formats are stored from C3 to C0. */
477 static uint32_t r300_translate_out_fmt(enum pipe_format format)
478 {
479 uint32_t modifier = 0;
480 unsigned i;
481 const struct util_format_description *desc;
482 static const uint32_t sign_bit[4] = {
483 R300_OUT_SIGN(0x1),
484 R300_OUT_SIGN(0x2),
485 R300_OUT_SIGN(0x4),
486 R300_OUT_SIGN(0x8),
487 };
488
489 desc = util_format_description(format);
490
491 /* Find the first non-VOID channel. */
492 for (i = 0; i < 4; i++) {
493 if (desc->channel[i].type != UTIL_FORMAT_TYPE_VOID) {
494 break;
495 }
496 }
497
498 if (i == 4)
499 return ~0; /* Unsupported/unknown. */
500
501 /* Specifies how the shader output is written to the fog unit. */
502 if (desc->channel[i].type == UTIL_FORMAT_TYPE_FLOAT) {
503 if (desc->channel[i].size == 32) {
504 modifier |= R300_US_OUT_FMT_C4_32_FP;
505 } else {
506 modifier |= R300_US_OUT_FMT_C4_16_FP;
507 }
508 } else {
509 if (desc->channel[i].size == 16) {
510 modifier |= R300_US_OUT_FMT_C4_16;
511 } else if (desc->channel[i].size == 10) {
512 modifier |= R300_US_OUT_FMT_C4_10;
513 } else {
514 /* C4_8 seems to be used for the formats whose pixel size
515 * is <= 32 bits. */
516 modifier |= R300_US_OUT_FMT_C4_8;
517 }
518 }
519
520 /* Add sign. */
521 for (i = 0; i < 4; i++)
522 if (desc->channel[i].type == UTIL_FORMAT_TYPE_SIGNED) {
523 modifier |= sign_bit[i];
524 }
525
526 /* Add swizzles and return. */
527 switch (format) {
528 /* 8-bit outputs, one channel.
529 * COLORFORMAT_I8 stores the C2 component. */
530 case PIPE_FORMAT_A8_UNORM:
531 /*case PIPE_FORMAT_A8_SNORM:*/
532 return modifier | R300_C2_SEL_A;
533 case PIPE_FORMAT_I8_UNORM:
534 /*case PIPE_FORMAT_I8_SNORM:*/
535 case PIPE_FORMAT_L8_UNORM:
536 /*case PIPE_FORMAT_L8_SNORM:*/
537 case PIPE_FORMAT_L8_SRGB:
538 case PIPE_FORMAT_R8_UNORM:
539 case PIPE_FORMAT_R8_SNORM:
540 return modifier | R300_C2_SEL_R;
541
542 /* 16-bit outputs, two channels.
543 * COLORFORMAT_UV88 stores C2 and C0. */
544 case PIPE_FORMAT_L8A8_UNORM:
545 /*case PIPE_FORMAT_L8A8_SNORM:*/
546 case PIPE_FORMAT_L8A8_SRGB:
547 return modifier | R300_C0_SEL_A | R300_C2_SEL_R;
548 case PIPE_FORMAT_R8G8_UNORM:
549 case PIPE_FORMAT_R8G8_SNORM:
550 return modifier | R300_C0_SEL_G | R300_C2_SEL_R;
551
552 /* BGRA outputs. */
553 case PIPE_FORMAT_B5G6R5_UNORM:
554 case PIPE_FORMAT_B5G5R5A1_UNORM:
555 case PIPE_FORMAT_B5G5R5X1_UNORM:
556 case PIPE_FORMAT_B4G4R4A4_UNORM:
557 case PIPE_FORMAT_B4G4R4X4_UNORM:
558 case PIPE_FORMAT_B8G8R8A8_UNORM:
559 /*case PIPE_FORMAT_B8G8R8A8_SNORM:*/
560 case PIPE_FORMAT_B8G8R8A8_SRGB:
561 case PIPE_FORMAT_B8G8R8X8_UNORM:
562 /*case PIPE_FORMAT_B8G8R8X8_SNORM:*/
563 case PIPE_FORMAT_B8G8R8X8_SRGB:
564 case PIPE_FORMAT_B10G10R10A2_UNORM:
565 return modifier |
566 R300_C0_SEL_B | R300_C1_SEL_G |
567 R300_C2_SEL_R | R300_C3_SEL_A;
568
569 /* ARGB outputs. */
570 case PIPE_FORMAT_A8R8G8B8_UNORM:
571 /*case PIPE_FORMAT_A8R8G8B8_SNORM:*/
572 case PIPE_FORMAT_A8R8G8B8_SRGB:
573 case PIPE_FORMAT_X8R8G8B8_UNORM:
574 /*case PIPE_FORMAT_X8R8G8B8_SNORM:*/
575 case PIPE_FORMAT_X8R8G8B8_SRGB:
576 return modifier |
577 R300_C0_SEL_A | R300_C1_SEL_R |
578 R300_C2_SEL_G | R300_C3_SEL_B;
579
580 /* ABGR outputs. */
581 case PIPE_FORMAT_A8B8G8R8_UNORM:
582 /*case PIPE_FORMAT_A8B8G8R8_SNORM:*/
583 case PIPE_FORMAT_A8B8G8R8_SRGB:
584 case PIPE_FORMAT_X8B8G8R8_UNORM:
585 /*case PIPE_FORMAT_X8B8G8R8_SNORM:*/
586 case PIPE_FORMAT_X8B8G8R8_SRGB:
587 return modifier |
588 R300_C0_SEL_A | R300_C1_SEL_B |
589 R300_C2_SEL_G | R300_C3_SEL_R;
590
591 /* RGBA outputs. */
592 case PIPE_FORMAT_R8G8B8X8_UNORM:
593 /*case PIPE_FORMAT_R8G8B8X8_SNORM:*/
594 /*case PIPE_FORMAT_R8G8B8X8_SRGB:*/
595 case PIPE_FORMAT_R8G8B8A8_UNORM:
596 case PIPE_FORMAT_R8G8B8A8_SNORM:
597 case PIPE_FORMAT_R8G8B8A8_SRGB:
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_texture_desc *desc,
633 unsigned level,
634 struct r300_texture_format_state *out)
635 {
636 struct pipe_resource *pt = &desc->b.b;
637 boolean is_r500 = screen->caps.is_r500;
638
639 /* Mask out all the fields we change. */
640 out->format0 = 0;
641 out->format1 &= ~R300_TX_FORMAT_TEX_COORD_TYPE_MASK;
642 out->format2 &= R500_TXFORMAT_MSB;
643 out->tile_config = 0;
644
645 /* Set sampler state. */
646 out->format0 =
647 R300_TX_WIDTH((u_minify(desc->width0, level) - 1) & 0x7ff) |
648 R300_TX_HEIGHT((u_minify(desc->height0, level) - 1) & 0x7ff) |
649 R300_TX_DEPTH(util_logbase2(u_minify(desc->depth0, level)) & 0xf);
650
651 if (desc->uses_stride_addressing) {
652 /* rectangles love this */
653 out->format0 |= R300_TX_PITCH_EN;
654 out->format2 = (desc->stride_in_pixels[level] - 1) & 0x1fff;
655 }
656
657 if (pt->target == PIPE_TEXTURE_CUBE) {
658 out->format1 |= R300_TX_FORMAT_CUBIC_MAP;
659 }
660 if (pt->target == PIPE_TEXTURE_3D) {
661 out->format1 |= R300_TX_FORMAT_3D;
662 }
663
664 /* large textures on r500 */
665 if (is_r500)
666 {
667 if (desc->width0 > 2048) {
668 out->format2 |= R500_TXWIDTH_BIT11;
669 }
670 if (desc->height0 > 2048) {
671 out->format2 |= R500_TXHEIGHT_BIT11;
672 }
673 }
674
675 out->tile_config = R300_TXO_MACRO_TILE(desc->macrotile[level]) |
676 R300_TXO_MICRO_TILE(desc->microtile);
677 }
678
679 static void r300_texture_setup_fb_state(struct r300_screen* screen,
680 struct r300_texture* tex)
681 {
682 unsigned i;
683
684 /* Set framebuffer state. */
685 if (util_format_is_depth_or_stencil(tex->desc.b.b.format)) {
686 for (i = 0; i <= tex->desc.b.b.last_level; i++) {
687 tex->fb_state.pitch[i] =
688 tex->desc.stride_in_pixels[i] |
689 R300_DEPTHMACROTILE(tex->desc.macrotile[i]) |
690 R300_DEPTHMICROTILE(tex->desc.microtile);
691 }
692 tex->fb_state.format = r300_translate_zsformat(tex->desc.b.b.format);
693 } else {
694 for (i = 0; i <= tex->desc.b.b.last_level; i++) {
695 tex->fb_state.pitch[i] =
696 tex->desc.stride_in_pixels[i] |
697 r300_translate_colorformat(tex->desc.b.b.format) |
698 R300_COLOR_TILE(tex->desc.macrotile[i]) |
699 R300_COLOR_MICROTILE(tex->desc.microtile);
700 }
701 tex->fb_state.format = r300_translate_out_fmt(tex->desc.b.b.format);
702 }
703 }
704
705 void r300_texture_reinterpret_format(struct pipe_screen *screen,
706 struct pipe_resource *tex,
707 enum pipe_format new_format)
708 {
709 struct r300_screen *r300screen = r300_screen(screen);
710
711 SCREEN_DBG(r300screen, DBG_TEX,
712 "r300: texture_reinterpret_format: %s -> %s\n",
713 util_format_short_name(tex->format),
714 util_format_short_name(new_format));
715
716 tex->format = new_format;
717
718 r300_texture_setup_fb_state(r300_screen(screen), r300_texture(tex));
719 }
720
721 static unsigned r300_texture_is_referenced(struct pipe_context *context,
722 struct pipe_resource *texture,
723 unsigned level, int layer)
724 {
725 struct r300_context *r300 = r300_context(context);
726 struct r300_texture *rtex = (struct r300_texture *)texture;
727
728 if (r300->rws->cs_is_buffer_referenced(r300->cs,
729 rtex->cs_buffer, R300_REF_CS))
730 return PIPE_REFERENCED_FOR_READ | PIPE_REFERENCED_FOR_WRITE;
731
732 return PIPE_UNREFERENCED;
733 }
734
735 static void r300_texture_destroy(struct pipe_screen *screen,
736 struct pipe_resource* texture)
737 {
738 struct r300_texture* tex = (struct r300_texture*)texture;
739 struct r300_winsys_screen *rws = (struct r300_winsys_screen *)texture->screen->winsys;
740 int i;
741
742 rws->buffer_reference(rws, &tex->buffer, NULL);
743 for (i = 0; i < R300_MAX_TEXTURE_LEVELS; i++) {
744 if (tex->hiz_mem[i])
745 u_mmFreeMem(tex->hiz_mem[i]);
746 if (tex->zmask_mem[i])
747 u_mmFreeMem(tex->zmask_mem[i]);
748 }
749
750 FREE(tex);
751 }
752
753 static boolean r300_texture_get_handle(struct pipe_screen* screen,
754 struct pipe_resource *texture,
755 struct winsys_handle *whandle)
756 {
757 struct r300_winsys_screen *rws = (struct r300_winsys_screen *)screen->winsys;
758 struct r300_texture* tex = (struct r300_texture*)texture;
759
760 if (!tex) {
761 return FALSE;
762 }
763
764 return rws->buffer_get_handle(rws, tex->buffer,
765 tex->desc.stride_in_bytes[0], whandle);
766 }
767
768 struct u_resource_vtbl r300_texture_vtbl =
769 {
770 r300_texture_get_handle, /* get_handle */
771 r300_texture_destroy, /* resource_destroy */
772 r300_texture_is_referenced, /* is_resource_referenced */
773 r300_texture_get_transfer, /* get_transfer */
774 r300_texture_transfer_destroy, /* transfer_destroy */
775 r300_texture_transfer_map, /* transfer_map */
776 u_default_transfer_flush_region, /* transfer_flush_region */
777 r300_texture_transfer_unmap, /* transfer_unmap */
778 u_default_transfer_inline_write /* transfer_inline_write */
779 };
780
781 /* The common texture constructor. */
782 static struct r300_texture*
783 r300_texture_create_object(struct r300_screen *rscreen,
784 const struct pipe_resource *base,
785 enum r300_buffer_tiling microtile,
786 enum r300_buffer_tiling macrotile,
787 unsigned stride_in_bytes_override,
788 unsigned max_buffer_size,
789 struct r300_winsys_buffer *buffer)
790 {
791 struct r300_winsys_screen *rws = rscreen->rws;
792 struct r300_texture *tex = CALLOC_STRUCT(r300_texture);
793 if (!tex) {
794 if (buffer)
795 rws->buffer_reference(rws, &buffer, NULL);
796 return NULL;
797 }
798
799 /* Initialize the descriptor. */
800 if (!r300_texture_desc_init(rscreen, &tex->desc, base,
801 microtile, macrotile,
802 stride_in_bytes_override,
803 max_buffer_size)) {
804 if (buffer)
805 rws->buffer_reference(rws, &buffer, NULL);
806 FREE(tex);
807 return NULL;
808 }
809 /* Initialize the hardware state. */
810 r300_texture_setup_format_state(rscreen, &tex->desc, 0, &tex->tx_format);
811 r300_texture_setup_fb_state(rscreen, tex);
812
813 tex->desc.b.vtbl = &r300_texture_vtbl;
814 pipe_reference_init(&tex->desc.b.b.reference, 1);
815 tex->domain = base->flags & R300_RESOURCE_FLAG_TRANSFER ?
816 R300_DOMAIN_GTT :
817 R300_DOMAIN_VRAM | R300_DOMAIN_GTT;
818 tex->buffer = buffer;
819
820 /* Create the backing buffer if needed. */
821 if (!tex->buffer) {
822 tex->buffer = rws->buffer_create(rws, tex->desc.size_in_bytes, 2048,
823 base->bind, base->usage, tex->domain);
824
825 if (!tex->buffer) {
826 FREE(tex);
827 return NULL;
828 }
829 }
830
831 tex->cs_buffer = rws->buffer_get_cs_handle(rws, tex->buffer);
832
833 rws->buffer_set_tiling(rws, tex->buffer,
834 tex->desc.microtile, tex->desc.macrotile[0],
835 tex->desc.stride_in_bytes[0]);
836
837 return tex;
838 }
839
840 /* Create a new texture. */
841 struct pipe_resource *r300_texture_create(struct pipe_screen *screen,
842 const struct pipe_resource *base)
843 {
844 struct r300_screen *rscreen = r300_screen(screen);
845 enum r300_buffer_tiling microtile, macrotile;
846
847 if ((base->flags & R300_RESOURCE_FLAG_TRANSFER) ||
848 (base->bind & PIPE_BIND_SCANOUT)) {
849 microtile = R300_BUFFER_LINEAR;
850 macrotile = R300_BUFFER_LINEAR;
851 } else {
852 microtile = R300_BUFFER_SELECT_LAYOUT;
853 macrotile = R300_BUFFER_SELECT_LAYOUT;
854 }
855
856 return (struct pipe_resource*)
857 r300_texture_create_object(rscreen, base, microtile, macrotile,
858 0, 0, NULL);
859 }
860
861 struct pipe_resource *r300_texture_from_handle(struct pipe_screen *screen,
862 const struct pipe_resource *base,
863 struct winsys_handle *whandle)
864 {
865 struct r300_winsys_screen *rws = (struct r300_winsys_screen*)screen->winsys;
866 struct r300_screen *rscreen = r300_screen(screen);
867 struct r300_winsys_buffer *buffer;
868 enum r300_buffer_tiling microtile, macrotile;
869 unsigned stride, size;
870
871 /* Support only 2D textures without mipmaps */
872 if ((base->target != PIPE_TEXTURE_2D &&
873 base->target != PIPE_TEXTURE_RECT) ||
874 base->depth0 != 1 ||
875 base->last_level != 0) {
876 return NULL;
877 }
878
879 buffer = rws->buffer_from_handle(rws, whandle, &stride, &size);
880 if (!buffer)
881 return NULL;
882
883 rws->buffer_get_tiling(rws, buffer, &microtile, &macrotile);
884
885 /* Enforce a microtiled zbuffer. */
886 if (util_format_is_depth_or_stencil(base->format) &&
887 microtile == R300_BUFFER_LINEAR) {
888 switch (util_format_get_blocksize(base->format)) {
889 case 4:
890 microtile = R300_BUFFER_TILED;
891 break;
892
893 case 2:
894 if (rws->get_value(rws, R300_VID_SQUARE_TILING_SUPPORT))
895 microtile = R300_BUFFER_SQUARETILED;
896 break;
897 }
898 }
899
900 return (struct pipe_resource*)
901 r300_texture_create_object(rscreen, base, microtile, macrotile,
902 stride, size, buffer);
903 }
904
905 /* Not required to implement u_resource_vtbl, consider moving to another file:
906 */
907 struct pipe_surface* r300_create_surface(struct pipe_context * ctx,
908 struct pipe_resource* texture,
909 const struct pipe_surface *surf_tmpl)
910 {
911 struct r300_texture* tex = r300_texture(texture);
912 struct r300_surface* surface = CALLOC_STRUCT(r300_surface);
913 unsigned level = surf_tmpl->u.tex.level;
914
915 assert(surf_tmpl->u.tex.first_layer == surf_tmpl->u.tex.last_layer);
916
917 if (surface) {
918 uint32_t offset, tile_height;
919
920 pipe_reference_init(&surface->base.reference, 1);
921 pipe_resource_reference(&surface->base.texture, texture);
922 surface->base.context = ctx;
923 surface->base.format = surf_tmpl->format;
924 surface->base.width = u_minify(texture->width0, level);
925 surface->base.height = u_minify(texture->height0, level);
926 surface->base.usage = surf_tmpl->usage;
927 surface->base.u.tex.level = level;
928 surface->base.u.tex.first_layer = surf_tmpl->u.tex.first_layer;
929 surface->base.u.tex.last_layer = surf_tmpl->u.tex.last_layer;
930
931 surface->buffer = tex->buffer;
932 surface->cs_buffer = tex->cs_buffer;
933
934 /* Prefer VRAM if there are multiple domains to choose from. */
935 surface->domain = tex->domain;
936 if (surface->domain & R300_DOMAIN_VRAM)
937 surface->domain &= ~R300_DOMAIN_GTT;
938
939 surface->offset = r300_texture_get_offset(&tex->desc, level,
940 surf_tmpl->u.tex.first_layer);
941 surface->pitch = tex->fb_state.pitch[level];
942 surface->format = tex->fb_state.format;
943
944 /* Parameters for the CBZB clear. */
945 surface->cbzb_allowed = tex->desc.cbzb_allowed[level];
946 surface->cbzb_width = align(surface->base.width, 64);
947
948 /* Height must be aligned to the size of a tile. */
949 tile_height = r300_get_pixel_alignment(tex->desc.b.b.format,
950 tex->desc.b.b.nr_samples,
951 tex->desc.microtile,
952 tex->desc.macrotile[level],
953 DIM_HEIGHT, 0);
954
955 surface->cbzb_height = align((surface->base.height + 1) / 2,
956 tile_height);
957
958 /* Offset must be aligned to 2K and must point at the beginning
959 * of a scanline. */
960 offset = surface->offset +
961 tex->desc.stride_in_bytes[level] * surface->cbzb_height;
962 surface->cbzb_midpoint_offset = offset & ~2047;
963
964 surface->cbzb_pitch = surface->pitch & 0x1ffffc;
965
966 if (util_format_get_blocksizebits(surface->base.format) == 32)
967 surface->cbzb_format = R300_DEPTHFORMAT_24BIT_INT_Z_8BIT_STENCIL;
968 else
969 surface->cbzb_format = R300_DEPTHFORMAT_16BIT_INT_Z;
970
971 DBG(r300_context(ctx), DBG_CBZB,
972 "CBZB Allowed: %s, Dim: %ix%i, Misalignment: %i, Micro: %s, Macro: %s\n",
973 surface->cbzb_allowed ? "YES" : " NO",
974 surface->cbzb_width, surface->cbzb_height,
975 offset & 2047,
976 tex->desc.microtile ? "YES" : " NO",
977 tex->desc.macrotile[level] ? "YES" : " NO");
978 }
979
980 return &surface->base;
981 }
982
983 /* Not required to implement u_resource_vtbl, consider moving to another file:
984 */
985 void r300_surface_destroy(struct pipe_context *ctx, struct pipe_surface* s)
986 {
987 pipe_resource_reference(&s->texture, NULL);
988 FREE(s);
989 }