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