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