r300g: fix psychedelic colors with SWTCL
[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 #include "pipe/p_screen.h"
25
26 #include "util/u_format.h"
27 #include "util/u_format_s3tc.h"
28 #include "util/u_math.h"
29 #include "util/u_memory.h"
30
31 #include "r300_context.h"
32 #include "r300_reg.h"
33 #include "r300_texture.h"
34 #include "r300_transfer.h"
35 #include "r300_screen.h"
36 #include "r300_winsys.h"
37
38 #define TILE_WIDTH 0
39 #define TILE_HEIGHT 1
40
41 static const unsigned microblock_table[5][3][2] = {
42 /*linear tiled square-tiled */
43 {{32, 1}, {8, 4}, {0, 0}}, /* 8 bits per pixel */
44 {{16, 1}, {8, 2}, {4, 4}}, /* 16 bits per pixel */
45 {{ 8, 1}, {4, 2}, {0, 0}}, /* 32 bits per pixel */
46 {{ 4, 1}, {0, 0}, {2, 2}}, /* 64 bits per pixel */
47 {{ 2, 1}, {0, 0}, {0, 0}} /* 128 bits per pixel */
48 };
49
50 unsigned r300_get_swizzle_combined(const unsigned char *swizzle_format,
51 const unsigned char *swizzle_view)
52 {
53 unsigned i;
54 unsigned char swizzle[4];
55 unsigned result = 0;
56 const uint32_t swizzle_shift[4] = {
57 R300_TX_FORMAT_R_SHIFT,
58 R300_TX_FORMAT_G_SHIFT,
59 R300_TX_FORMAT_B_SHIFT,
60 R300_TX_FORMAT_A_SHIFT
61 };
62 const uint32_t swizzle_bit[4] = {
63 R300_TX_FORMAT_X,
64 R300_TX_FORMAT_Y,
65 R300_TX_FORMAT_Z,
66 R300_TX_FORMAT_W
67 };
68
69 if (swizzle_view) {
70 /* Combine two sets of swizzles. */
71 for (i = 0; i < 4; i++) {
72 swizzle[i] = swizzle_view[i] <= UTIL_FORMAT_SWIZZLE_W ?
73 swizzle_format[swizzle_view[i]] : swizzle_view[i];
74 }
75 } else {
76 memcpy(swizzle, swizzle_format, 4);
77 }
78
79 /* Get swizzle. */
80 for (i = 0; i < 4; i++) {
81 switch (swizzle[i]) {
82 case UTIL_FORMAT_SWIZZLE_Y:
83 result |= swizzle_bit[1] << swizzle_shift[i];
84 break;
85 case UTIL_FORMAT_SWIZZLE_Z:
86 result |= swizzle_bit[2] << swizzle_shift[i];
87 break;
88 case UTIL_FORMAT_SWIZZLE_W:
89 result |= swizzle_bit[3] << swizzle_shift[i];
90 break;
91 case UTIL_FORMAT_SWIZZLE_0:
92 result |= R300_TX_FORMAT_ZERO << swizzle_shift[i];
93 break;
94 case UTIL_FORMAT_SWIZZLE_1:
95 result |= R300_TX_FORMAT_ONE << swizzle_shift[i];
96 break;
97 default: /* UTIL_FORMAT_SWIZZLE_X */
98 result |= swizzle_bit[0] << swizzle_shift[i];
99 }
100 }
101 return result;
102 }
103
104 /* Translate a pipe_format into a useful texture format for sampling.
105 *
106 * Some special formats are translated directly using R300_EASY_TX_FORMAT,
107 * but the majority of them is translated in a generic way, automatically
108 * supporting all the formats hw can support.
109 *
110 * R300_EASY_TX_FORMAT swizzles the texture.
111 * Note the signature of R300_EASY_TX_FORMAT:
112 * R300_EASY_TX_FORMAT(B, G, R, A, FORMAT);
113 *
114 * The FORMAT specifies how the texture sampler will treat the texture, and
115 * makes available X, Y, Z, W, ZERO, and ONE for swizzling. */
116 uint32_t r300_translate_texformat(enum pipe_format format,
117 const unsigned char *swizzle_view)
118 {
119 uint32_t result = 0;
120 const struct util_format_description *desc;
121 unsigned i;
122 boolean uniform = TRUE;
123 const uint32_t sign_bit[4] = {
124 R300_TX_FORMAT_SIGNED_X,
125 R300_TX_FORMAT_SIGNED_Y,
126 R300_TX_FORMAT_SIGNED_Z,
127 R300_TX_FORMAT_SIGNED_W,
128 };
129
130 desc = util_format_description(format);
131
132 /* Colorspace (return non-RGB formats directly). */
133 switch (desc->colorspace) {
134 /* Depth stencil formats.
135 * Swizzles are added in r300_merge_textures_and_samplers. */
136 case UTIL_FORMAT_COLORSPACE_ZS:
137 switch (format) {
138 case PIPE_FORMAT_Z16_UNORM:
139 return R300_TX_FORMAT_X16;
140 case PIPE_FORMAT_X8Z24_UNORM:
141 case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
142 return R300_TX_FORMAT_W24_FP;
143 default:
144 return ~0; /* Unsupported. */
145 }
146
147 /* YUV formats. */
148 case UTIL_FORMAT_COLORSPACE_YUV:
149 result |= R300_TX_FORMAT_YUV_TO_RGB;
150
151 switch (format) {
152 case PIPE_FORMAT_UYVY:
153 return R300_EASY_TX_FORMAT(X, Y, Z, ONE, YVYU422) | result;
154 case PIPE_FORMAT_YUYV:
155 return R300_EASY_TX_FORMAT(X, Y, Z, ONE, VYUY422) | result;
156 default:
157 return ~0; /* Unsupported/unknown. */
158 }
159
160 /* Add gamma correction. */
161 case UTIL_FORMAT_COLORSPACE_SRGB:
162 result |= R300_TX_FORMAT_GAMMA;
163 break;
164
165 default:
166 switch (format) {
167 /* Same as YUV but without the YUR->RGB conversion. */
168 case PIPE_FORMAT_R8G8_B8G8_UNORM:
169 return R300_EASY_TX_FORMAT(X, Y, Z, ONE, YVYU422) | result;
170 case PIPE_FORMAT_G8R8_G8B8_UNORM:
171 return R300_EASY_TX_FORMAT(X, Y, Z, ONE, VYUY422) | result;
172 default:;
173 }
174 }
175
176 result |= r300_get_swizzle_combined(desc->swizzle, swizzle_view);
177
178 /* S3TC formats. */
179 if (desc->layout == UTIL_FORMAT_LAYOUT_S3TC) {
180 if (!util_format_s3tc_enabled) {
181 return ~0; /* Unsupported. */
182 }
183
184 switch (format) {
185 case PIPE_FORMAT_DXT1_RGB:
186 case PIPE_FORMAT_DXT1_RGBA:
187 case PIPE_FORMAT_DXT1_SRGB:
188 case PIPE_FORMAT_DXT1_SRGBA:
189 return R300_TX_FORMAT_DXT1 | result;
190 case PIPE_FORMAT_DXT3_RGBA:
191 case PIPE_FORMAT_DXT3_SRGBA:
192 return R300_TX_FORMAT_DXT3 | result;
193 case PIPE_FORMAT_DXT5_RGBA:
194 case PIPE_FORMAT_DXT5_SRGBA:
195 return R300_TX_FORMAT_DXT5 | result;
196 default:
197 return ~0; /* Unsupported/unknown. */
198 }
199 }
200
201 /* Add sign. */
202 for (i = 0; i < desc->nr_channels; i++) {
203 if (desc->channel[i].type == UTIL_FORMAT_TYPE_SIGNED) {
204 result |= sign_bit[i];
205 }
206 }
207
208 /* This is truly a special format.
209 * It stores R8G8 and B is computed using sqrt(1 - R^2 - G^2)
210 * in the sampler unit. Also known as D3DFMT_CxV8U8. */
211 if (format == PIPE_FORMAT_R8G8Bx_SNORM) {
212 return R300_TX_FORMAT_CxV8U8 | result;
213 }
214
215 /* RGTC formats. */
216 if (desc->layout == UTIL_FORMAT_LAYOUT_RGTC) {
217 switch (format) {
218 case PIPE_FORMAT_RGTC1_UNORM:
219 case PIPE_FORMAT_RGTC1_SNORM:
220 return R500_TX_FORMAT_ATI1N | result;
221 case PIPE_FORMAT_RGTC2_UNORM:
222 case PIPE_FORMAT_RGTC2_SNORM:
223 return R400_TX_FORMAT_ATI2N | result;
224 default:
225 return ~0; /* Unsupported/unknown. */
226 }
227 }
228
229 /* See whether the components are of the same size. */
230 for (i = 1; i < desc->nr_channels; i++) {
231 uniform = uniform && desc->channel[0].size == desc->channel[i].size;
232 }
233
234 /* Non-uniform formats. */
235 if (!uniform) {
236 switch (desc->nr_channels) {
237 case 3:
238 if (desc->channel[0].size == 5 &&
239 desc->channel[1].size == 6 &&
240 desc->channel[2].size == 5) {
241 return R300_TX_FORMAT_Z5Y6X5 | result;
242 }
243 if (desc->channel[0].size == 5 &&
244 desc->channel[1].size == 5 &&
245 desc->channel[2].size == 6) {
246 return R300_TX_FORMAT_Z6Y5X5 | result;
247 }
248 return ~0; /* Unsupported/unknown. */
249
250 case 4:
251 if (desc->channel[0].size == 5 &&
252 desc->channel[1].size == 5 &&
253 desc->channel[2].size == 5 &&
254 desc->channel[3].size == 1) {
255 return R300_TX_FORMAT_W1Z5Y5X5 | result;
256 }
257 if (desc->channel[0].size == 10 &&
258 desc->channel[1].size == 10 &&
259 desc->channel[2].size == 10 &&
260 desc->channel[3].size == 2) {
261 return R300_TX_FORMAT_W2Z10Y10X10 | result;
262 }
263 }
264 return ~0; /* Unsupported/unknown. */
265 }
266
267 /* And finally, uniform formats. */
268 switch (desc->channel[0].type) {
269 case UTIL_FORMAT_TYPE_UNSIGNED:
270 case UTIL_FORMAT_TYPE_SIGNED:
271 if (!desc->channel[0].normalized &&
272 desc->colorspace != UTIL_FORMAT_COLORSPACE_SRGB) {
273 return ~0;
274 }
275
276 switch (desc->channel[0].size) {
277 case 4:
278 switch (desc->nr_channels) {
279 case 2:
280 return R300_TX_FORMAT_Y4X4 | result;
281 case 4:
282 return R300_TX_FORMAT_W4Z4Y4X4 | result;
283 }
284 return ~0;
285
286 case 8:
287 switch (desc->nr_channels) {
288 case 1:
289 return R300_TX_FORMAT_X8 | result;
290 case 2:
291 return R300_TX_FORMAT_Y8X8 | result;
292 case 4:
293 return R300_TX_FORMAT_W8Z8Y8X8 | result;
294 }
295 return ~0;
296
297 case 16:
298 switch (desc->nr_channels) {
299 case 1:
300 return R300_TX_FORMAT_X16 | result;
301 case 2:
302 return R300_TX_FORMAT_Y16X16 | result;
303 case 4:
304 return R300_TX_FORMAT_W16Z16Y16X16 | result;
305 }
306 }
307 return ~0;
308
309 case UTIL_FORMAT_TYPE_FLOAT:
310 switch (desc->channel[0].size) {
311 case 16:
312 switch (desc->nr_channels) {
313 case 1:
314 return R300_TX_FORMAT_16F | result;
315 case 2:
316 return R300_TX_FORMAT_16F_16F | result;
317 case 4:
318 return R300_TX_FORMAT_16F_16F_16F_16F | result;
319 }
320 return ~0;
321
322 case 32:
323 switch (desc->nr_channels) {
324 case 1:
325 return R300_TX_FORMAT_32F | result;
326 case 2:
327 return R300_TX_FORMAT_32F_32F | result;
328 case 4:
329 return R300_TX_FORMAT_32F_32F_32F_32F | result;
330 }
331 }
332 }
333
334 return ~0; /* Unsupported/unknown. */
335 }
336
337 uint32_t r500_tx_format_msb_bit(enum pipe_format format)
338 {
339 switch (format) {
340 case PIPE_FORMAT_RGTC1_UNORM:
341 case PIPE_FORMAT_RGTC1_SNORM:
342 return R500_TXFORMAT_MSB;
343 default:
344 return 0;
345 }
346 }
347
348 /* Buffer formats. */
349
350 /* Colorbuffer formats. This is the unswizzled format of the RB3D block's
351 * output. For the swizzling of the targets, check the shader's format. */
352 static uint32_t r300_translate_colorformat(enum pipe_format format)
353 {
354 switch (format) {
355 /* 8-bit buffers. */
356 case PIPE_FORMAT_A8_UNORM:
357 case PIPE_FORMAT_I8_UNORM:
358 case PIPE_FORMAT_L8_UNORM:
359 case PIPE_FORMAT_R8_UNORM:
360 case PIPE_FORMAT_R8_SNORM:
361 return R300_COLOR_FORMAT_I8;
362
363 /* 16-bit buffers. */
364 case PIPE_FORMAT_B5G6R5_UNORM:
365 return R300_COLOR_FORMAT_RGB565;
366
367 case PIPE_FORMAT_B5G5R5A1_UNORM:
368 case PIPE_FORMAT_B5G5R5X1_UNORM:
369 return R300_COLOR_FORMAT_ARGB1555;
370
371 case PIPE_FORMAT_B4G4R4A4_UNORM:
372 case PIPE_FORMAT_B4G4R4X4_UNORM:
373 return R300_COLOR_FORMAT_ARGB4444;
374
375 /* 32-bit buffers. */
376 case PIPE_FORMAT_B8G8R8A8_UNORM:
377 case PIPE_FORMAT_B8G8R8X8_UNORM:
378 case PIPE_FORMAT_A8R8G8B8_UNORM:
379 case PIPE_FORMAT_X8R8G8B8_UNORM:
380 case PIPE_FORMAT_A8B8G8R8_UNORM:
381 case PIPE_FORMAT_R8G8B8A8_SNORM:
382 case PIPE_FORMAT_X8B8G8R8_UNORM:
383 case PIPE_FORMAT_R8G8B8X8_UNORM:
384 case PIPE_FORMAT_R8SG8SB8UX8U_NORM:
385 return R300_COLOR_FORMAT_ARGB8888;
386
387 case PIPE_FORMAT_R10G10B10A2_UNORM:
388 case PIPE_FORMAT_R10G10B10X2_SNORM:
389 case PIPE_FORMAT_B10G10R10A2_UNORM:
390 case PIPE_FORMAT_R10SG10SB10SA2U_NORM:
391 return R500_COLOR_FORMAT_ARGB2101010; /* R5xx-only? */
392
393 /* 64-bit buffers. */
394 case PIPE_FORMAT_R16G16B16A16_UNORM:
395 case PIPE_FORMAT_R16G16B16A16_SNORM:
396 case PIPE_FORMAT_R16G16B16A16_FLOAT:
397 return R300_COLOR_FORMAT_ARGB16161616;
398
399 /* 128-bit buffers. */
400 case PIPE_FORMAT_R32G32B32A32_FLOAT:
401 return R300_COLOR_FORMAT_ARGB32323232;
402
403 /* YUV buffers. */
404 case PIPE_FORMAT_UYVY:
405 return R300_COLOR_FORMAT_YVYU;
406 case PIPE_FORMAT_YUYV:
407 return R300_COLOR_FORMAT_VYUY;
408 default:
409 return ~0; /* Unsupported. */
410 }
411 }
412
413 /* Depthbuffer and stencilbuffer. Thankfully, we only support two flavors. */
414 static uint32_t r300_translate_zsformat(enum pipe_format format)
415 {
416 switch (format) {
417 /* 16-bit depth, no stencil */
418 case PIPE_FORMAT_Z16_UNORM:
419 return R300_DEPTHFORMAT_16BIT_INT_Z;
420 /* 24-bit depth, ignored stencil */
421 case PIPE_FORMAT_X8Z24_UNORM:
422 /* 24-bit depth, 8-bit stencil */
423 case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
424 return R300_DEPTHFORMAT_24BIT_INT_Z_8BIT_STENCIL;
425 default:
426 return ~0; /* Unsupported. */
427 }
428 }
429
430 /* Shader output formats. This is essentially the swizzle from the shader
431 * to the RB3D block.
432 *
433 * Note that formats are stored from C3 to C0. */
434 static uint32_t r300_translate_out_fmt(enum pipe_format format)
435 {
436 uint32_t modifier = 0;
437 unsigned i;
438 const struct util_format_description *desc;
439 static const uint32_t sign_bit[4] = {
440 R300_OUT_SIGN(0x1),
441 R300_OUT_SIGN(0x2),
442 R300_OUT_SIGN(0x4),
443 R300_OUT_SIGN(0x8),
444 };
445
446 desc = util_format_description(format);
447
448 /* Specifies how the shader output is written to the fog unit. */
449 if (desc->channel[0].type == UTIL_FORMAT_TYPE_FLOAT) {
450 if (desc->channel[0].size == 32) {
451 modifier |= R300_US_OUT_FMT_C4_32_FP;
452 } else {
453 modifier |= R300_US_OUT_FMT_C4_16_FP;
454 }
455 } else {
456 if (desc->channel[0].size == 16) {
457 modifier |= R300_US_OUT_FMT_C4_16;
458 } else {
459 /* C4_8 seems to be used for the formats whose pixel size
460 * is <= 32 bits. */
461 modifier |= R300_US_OUT_FMT_C4_8;
462 }
463 }
464
465 /* Add sign. */
466 for (i = 0; i < 4; i++)
467 if (desc->channel[i].type == UTIL_FORMAT_TYPE_SIGNED) {
468 modifier |= sign_bit[i];
469 }
470
471 /* Add swizzles and return. */
472 switch (format) {
473 /* 8-bit outputs.
474 * COLORFORMAT_I8 stores the C2 component. */
475 case PIPE_FORMAT_A8_UNORM:
476 return modifier | R300_C2_SEL_A;
477 case PIPE_FORMAT_I8_UNORM:
478 case PIPE_FORMAT_L8_UNORM:
479 case PIPE_FORMAT_R8_UNORM:
480 case PIPE_FORMAT_R8_SNORM:
481 return modifier | R300_C2_SEL_R;
482
483 /* BGRA outputs. */
484 case PIPE_FORMAT_B5G6R5_UNORM:
485 case PIPE_FORMAT_B5G5R5A1_UNORM:
486 case PIPE_FORMAT_B5G5R5X1_UNORM:
487 case PIPE_FORMAT_B4G4R4A4_UNORM:
488 case PIPE_FORMAT_B4G4R4X4_UNORM:
489 case PIPE_FORMAT_B8G8R8A8_UNORM:
490 case PIPE_FORMAT_B8G8R8X8_UNORM:
491 case PIPE_FORMAT_B10G10R10A2_UNORM:
492 return modifier |
493 R300_C0_SEL_B | R300_C1_SEL_G |
494 R300_C2_SEL_R | R300_C3_SEL_A;
495
496 /* ARGB outputs. */
497 case PIPE_FORMAT_A8R8G8B8_UNORM:
498 case PIPE_FORMAT_X8R8G8B8_UNORM:
499 return modifier |
500 R300_C0_SEL_A | R300_C1_SEL_R |
501 R300_C2_SEL_G | R300_C3_SEL_B;
502
503 /* ABGR outputs. */
504 case PIPE_FORMAT_A8B8G8R8_UNORM:
505 case PIPE_FORMAT_X8B8G8R8_UNORM:
506 return modifier |
507 R300_C0_SEL_A | R300_C1_SEL_B |
508 R300_C2_SEL_G | R300_C3_SEL_R;
509
510 /* RGBA outputs. */
511 case PIPE_FORMAT_R8G8B8X8_UNORM:
512 case PIPE_FORMAT_R8G8B8A8_SNORM:
513 case PIPE_FORMAT_R8SG8SB8UX8U_NORM:
514 case PIPE_FORMAT_R10G10B10A2_UNORM:
515 case PIPE_FORMAT_R10G10B10X2_SNORM:
516 case PIPE_FORMAT_R10SG10SB10SA2U_NORM:
517 case PIPE_FORMAT_R16G16B16A16_UNORM:
518 case PIPE_FORMAT_R16G16B16A16_SNORM:
519 case PIPE_FORMAT_R16G16B16A16_FLOAT:
520 case PIPE_FORMAT_R32G32B32A32_FLOAT:
521 return modifier |
522 R300_C0_SEL_R | R300_C1_SEL_G |
523 R300_C2_SEL_B | R300_C3_SEL_A;
524
525 default:
526 return ~0; /* Unsupported. */
527 }
528 }
529
530 boolean r300_is_colorbuffer_format_supported(enum pipe_format format)
531 {
532 return r300_translate_colorformat(format) != ~0 &&
533 r300_translate_out_fmt(format) != ~0;
534 }
535
536 boolean r300_is_zs_format_supported(enum pipe_format format)
537 {
538 return r300_translate_zsformat(format) != ~0;
539 }
540
541 boolean r300_is_sampler_format_supported(enum pipe_format format)
542 {
543 return r300_translate_texformat(format, 0) != ~0;
544 }
545
546 static void r300_texture_setup_immutable_state(struct r300_screen* screen,
547 struct r300_texture* tex)
548 {
549 struct r300_texture_format_state* f = &tex->tx_format;
550 struct pipe_resource *pt = &tex->b.b;
551 boolean is_r500 = screen->caps.is_r500;
552
553 /* Set sampler state. */
554 f->format0 = R300_TX_WIDTH((pt->width0 - 1) & 0x7ff) |
555 R300_TX_HEIGHT((pt->height0 - 1) & 0x7ff);
556
557 if (tex->uses_pitch) {
558 /* rectangles love this */
559 f->format0 |= R300_TX_PITCH_EN;
560 f->format2 = (tex->hwpitch[0] - 1) & 0x1fff;
561 } else {
562 /* power of two textures (3D, mipmaps, and no pitch) */
563 f->format0 |= R300_TX_DEPTH(util_logbase2(pt->depth0) & 0xf);
564 }
565
566 f->format1 = 0;
567 if (pt->target == PIPE_TEXTURE_CUBE) {
568 f->format1 |= R300_TX_FORMAT_CUBIC_MAP;
569 }
570 if (pt->target == PIPE_TEXTURE_3D) {
571 f->format1 |= R300_TX_FORMAT_3D;
572 }
573
574 /* large textures on r500 */
575 if (is_r500)
576 {
577 if (pt->width0 > 2048) {
578 f->format2 |= R500_TXWIDTH_BIT11;
579 }
580 if (pt->height0 > 2048) {
581 f->format2 |= R500_TXHEIGHT_BIT11;
582 }
583 }
584
585 f->tile_config = R300_TXO_MACRO_TILE(tex->macrotile) |
586 R300_TXO_MICRO_TILE(tex->microtile);
587 }
588
589 static void r300_texture_setup_fb_state(struct r300_screen* screen,
590 struct r300_texture* tex)
591 {
592 unsigned i;
593
594 /* Set framebuffer state. */
595 if (util_format_is_depth_or_stencil(tex->b.b.format)) {
596 for (i = 0; i <= tex->b.b.last_level; i++) {
597 tex->fb_state.depthpitch[i] =
598 tex->hwpitch[i] |
599 R300_DEPTHMACROTILE(tex->mip_macrotile[i]) |
600 R300_DEPTHMICROTILE(tex->microtile);
601 }
602 tex->fb_state.zb_format = r300_translate_zsformat(tex->b.b.format);
603 } else {
604 for (i = 0; i <= tex->b.b.last_level; i++) {
605 tex->fb_state.colorpitch[i] =
606 tex->hwpitch[i] |
607 r300_translate_colorformat(tex->b.b.format) |
608 R300_COLOR_TILE(tex->mip_macrotile[i]) |
609 R300_COLOR_MICROTILE(tex->microtile);
610 }
611 tex->fb_state.us_out_fmt = r300_translate_out_fmt(tex->b.b.format);
612 }
613 }
614
615 void r300_texture_reinterpret_format(struct pipe_screen *screen,
616 struct pipe_resource *tex,
617 enum pipe_format new_format)
618 {
619 struct r300_screen *r300screen = r300_screen(screen);
620
621 SCREEN_DBG(r300screen, DBG_TEX, "r300: texture_reinterpret_format: %s -> %s\n",
622 util_format_short_name(tex->format), util_format_short_name(new_format));
623
624 tex->format = new_format;
625
626 r300_texture_setup_fb_state(r300_screen(screen), r300_texture(tex));
627 }
628
629 unsigned r300_texture_get_offset(struct r300_texture* tex, unsigned level,
630 unsigned zslice, unsigned face)
631 {
632 unsigned offset = tex->offset[level];
633
634 switch (tex->b.b.target) {
635 case PIPE_TEXTURE_3D:
636 assert(face == 0);
637 return offset + zslice * tex->layer_size[level];
638
639 case PIPE_TEXTURE_CUBE:
640 assert(zslice == 0);
641 return offset + face * tex->layer_size[level];
642
643 default:
644 assert(zslice == 0 && face == 0);
645 return offset;
646 }
647 }
648
649 /**
650 * Return the width (dim==TILE_WIDTH) or height (dim==TILE_HEIGHT) of one tile
651 * of the given texture.
652 */
653 static unsigned r300_texture_get_tile_size(struct r300_texture* tex,
654 int dim, boolean macrotile)
655 {
656 unsigned pixsize, tile_size;
657
658 pixsize = util_format_get_blocksize(tex->b.b.format);
659 tile_size = microblock_table[util_logbase2(pixsize)][tex->microtile][dim];
660
661 if (macrotile) {
662 tile_size *= 8;
663 }
664
665 assert(tile_size);
666 return tile_size;
667 }
668
669 /* Return true if macrotiling should be enabled on the miplevel. */
670 static boolean r300_texture_macro_switch(struct r300_texture *tex,
671 unsigned level,
672 boolean rv350_mode,
673 int dim)
674 {
675 unsigned tile, texdim;
676
677 tile = r300_texture_get_tile_size(tex, dim, TRUE);
678 if (dim == TILE_WIDTH) {
679 texdim = u_minify(tex->b.b.width0, level);
680 } else {
681 texdim = u_minify(tex->b.b.height0, level);
682 }
683
684 /* See TX_FILTER1_n.MACRO_SWITCH. */
685 if (rv350_mode) {
686 return texdim >= tile;
687 } else {
688 return texdim > tile;
689 }
690 }
691
692 /**
693 * Return the stride, in bytes, of the texture images of the given texture
694 * at the given level.
695 */
696 unsigned r300_texture_get_stride(struct r300_screen* screen,
697 struct r300_texture* tex, unsigned level)
698 {
699 unsigned tile_width, width;
700
701 if (tex->stride_override)
702 return tex->stride_override;
703
704 /* Check the level. */
705 if (level > tex->b.b.last_level) {
706 SCREEN_DBG(screen, DBG_TEX, "%s: level (%u) > last_level (%u)\n",
707 __FUNCTION__, level, tex->b.b.last_level);
708 return 0;
709 }
710
711 width = u_minify(tex->b.b.width0, level);
712
713 if (util_format_is_plain(tex->b.b.format)) {
714 tile_width = r300_texture_get_tile_size(tex, TILE_WIDTH,
715 tex->mip_macrotile[level]);
716 width = align(width, tile_width);
717
718 return util_format_get_stride(tex->b.b.format, width);
719 } else {
720 return align(util_format_get_stride(tex->b.b.format, width), 32);
721 }
722 }
723
724 static unsigned r300_texture_get_nblocksy(struct r300_texture* tex,
725 unsigned level)
726 {
727 unsigned height, tile_height;
728
729 height = u_minify(tex->b.b.height0, level);
730
731 if (util_format_is_plain(tex->b.b.format)) {
732 tile_height = r300_texture_get_tile_size(tex, TILE_HEIGHT,
733 tex->mip_macrotile[level]);
734 height = align(height, tile_height);
735
736 /* This is needed for the kernel checker, unfortunately. */
737 height = util_next_power_of_two(height);
738 }
739
740 return util_format_get_nblocksy(tex->b.b.format, height);
741 }
742
743 static void r300_texture_3d_fix_mipmapping(struct r300_screen *screen,
744 struct r300_texture *tex)
745 {
746 /* The kernels <= 2.6.34-rc4 compute the size of mipmapped 3D textures
747 * incorrectly. This is a workaround to prevent CS from being rejected. */
748
749 unsigned i, size;
750
751 if (!screen->rws->get_value(screen->rws, R300_VID_DRM_2_3_0) &&
752 tex->b.b.target == PIPE_TEXTURE_3D &&
753 tex->b.b.last_level > 0) {
754 size = 0;
755
756 for (i = 0; i <= tex->b.b.last_level; i++) {
757 size += r300_texture_get_stride(screen, tex, i) *
758 r300_texture_get_nblocksy(tex, i);
759 }
760
761 size *= tex->b.b.depth0;
762 tex->size = size;
763 }
764 }
765
766 static void r300_setup_miptree(struct r300_screen* screen,
767 struct r300_texture* tex)
768 {
769 struct pipe_resource* base = &tex->b.b;
770 unsigned stride, size, layer_size, nblocksy, i;
771 boolean rv350_mode = screen->caps.is_rv350;
772
773 SCREEN_DBG(screen, DBG_TEXALLOC, "r300: Making miptree for texture, format %s\n",
774 util_format_short_name(base->format));
775
776 for (i = 0; i <= base->last_level; i++) {
777 /* Let's see if this miplevel can be macrotiled. */
778 tex->mip_macrotile[i] =
779 (tex->macrotile == R300_BUFFER_TILED &&
780 r300_texture_macro_switch(tex, i, rv350_mode, TILE_WIDTH) &&
781 r300_texture_macro_switch(tex, i, rv350_mode, TILE_HEIGHT)) ?
782 R300_BUFFER_TILED : R300_BUFFER_LINEAR;
783
784 stride = r300_texture_get_stride(screen, tex, i);
785 nblocksy = r300_texture_get_nblocksy(tex, i);
786 layer_size = stride * nblocksy;
787
788 if (base->target == PIPE_TEXTURE_CUBE)
789 size = layer_size * 6;
790 else
791 size = layer_size * u_minify(base->depth0, i);
792
793 tex->offset[i] = tex->size;
794 tex->size = tex->offset[i] + size;
795 tex->layer_size[i] = layer_size;
796 tex->pitch[i] = stride / util_format_get_blocksize(base->format);
797 tex->hwpitch[i] =
798 tex->pitch[i] * util_format_get_blockwidth(base->format);
799
800 SCREEN_DBG(screen, DBG_TEXALLOC, "r300: Texture miptree: Level %d "
801 "(%dx%dx%d px, pitch %d bytes) %d bytes total, macrotiled %s\n",
802 i, u_minify(base->width0, i), u_minify(base->height0, i),
803 u_minify(base->depth0, i), stride, tex->size,
804 tex->mip_macrotile[i] ? "TRUE" : "FALSE");
805 }
806 }
807
808 static void r300_setup_flags(struct r300_texture* tex)
809 {
810 tex->uses_pitch = !util_is_power_of_two(tex->b.b.width0) ||
811 !util_is_power_of_two(tex->b.b.height0) ||
812 tex->stride_override;
813 }
814
815 static void r300_setup_tiling(struct pipe_screen *screen,
816 struct r300_texture *tex)
817 {
818 struct r300_winsys_screen *rws = (struct r300_winsys_screen *)screen->winsys;
819 enum pipe_format format = tex->b.b.format;
820 boolean rv350_mode = r300_screen(screen)->caps.is_rv350;
821 boolean is_zb = util_format_is_depth_or_stencil(format);
822 boolean dbg_no_tiling = SCREEN_DBG_ON(r300_screen(screen), DBG_NO_TILING);
823
824 if (!util_format_is_plain(format)) {
825 return;
826 }
827
828 /* If height == 1, disable microtiling except for zbuffer. */
829 if (!is_zb && (tex->b.b.height0 == 1 || dbg_no_tiling)) {
830 return;
831 }
832
833 /* Set microtiling. */
834 switch (util_format_get_blocksize(format)) {
835 case 1:
836 case 4:
837 tex->microtile = R300_BUFFER_TILED;
838 break;
839
840 case 2:
841 case 8:
842 if (rws->get_value(rws, R300_VID_SQUARE_TILING_SUPPORT)) {
843 tex->microtile = R300_BUFFER_SQUARETILED;
844 }
845 break;
846 }
847
848 if (dbg_no_tiling) {
849 return;
850 }
851
852 /* Set macrotiling. */
853 if (r300_texture_macro_switch(tex, 0, rv350_mode, TILE_WIDTH) &&
854 r300_texture_macro_switch(tex, 0, rv350_mode, TILE_HEIGHT)) {
855 tex->macrotile = R300_BUFFER_TILED;
856 }
857 }
858
859 static unsigned r300_texture_is_referenced(struct pipe_context *context,
860 struct pipe_resource *texture,
861 unsigned face, unsigned level)
862 {
863 struct r300_context *r300 = r300_context(context);
864 struct r300_texture *rtex = (struct r300_texture *)texture;
865
866 if (r300->rws->is_buffer_referenced(r300->rws, rtex->buffer, R300_REF_CS))
867 return PIPE_REFERENCED_FOR_READ | PIPE_REFERENCED_FOR_WRITE;
868
869 return PIPE_UNREFERENCED;
870 }
871
872 static void r300_texture_destroy(struct pipe_screen *screen,
873 struct pipe_resource* texture)
874 {
875 struct r300_texture* tex = (struct r300_texture*)texture;
876 struct r300_winsys_screen *rws = (struct r300_winsys_screen *)texture->screen->winsys;
877
878 rws->buffer_reference(rws, &tex->buffer, NULL);
879 FREE(tex);
880 }
881
882 static boolean r300_texture_get_handle(struct pipe_screen* screen,
883 struct pipe_resource *texture,
884 struct winsys_handle *whandle)
885 {
886 struct r300_winsys_screen *rws = (struct r300_winsys_screen *)screen->winsys;
887 struct r300_texture* tex = (struct r300_texture*)texture;
888 unsigned stride;
889
890 if (!tex) {
891 return FALSE;
892 }
893
894 stride = r300_texture_get_stride(r300_screen(screen), tex, 0);
895
896 rws->buffer_get_handle(rws, tex->buffer, stride, whandle);
897
898 return TRUE;
899 }
900
901 struct u_resource_vtbl r300_texture_vtbl =
902 {
903 r300_texture_get_handle, /* get_handle */
904 r300_texture_destroy, /* resource_destroy */
905 r300_texture_is_referenced, /* is_resource_referenced */
906 r300_texture_get_transfer, /* get_transfer */
907 r300_texture_transfer_destroy, /* transfer_destroy */
908 r300_texture_transfer_map, /* transfer_map */
909 u_default_transfer_flush_region, /* transfer_flush_region */
910 r300_texture_transfer_unmap, /* transfer_unmap */
911 u_default_transfer_inline_write /* transfer_inline_write */
912 };
913
914 /* Create a new texture. */
915 struct pipe_resource* r300_texture_create(struct pipe_screen* screen,
916 const struct pipe_resource* base)
917 {
918 struct r300_texture* tex = CALLOC_STRUCT(r300_texture);
919 struct r300_screen* rscreen = r300_screen(screen);
920 struct r300_winsys_screen *rws = (struct r300_winsys_screen *)screen->winsys;
921
922 if (!tex) {
923 return NULL;
924 }
925
926 /* Refuse to create a texture with size 0. */
927 if (!base->width0 ||
928 (!base->height0 && (base->target == PIPE_TEXTURE_2D ||
929 base->target == PIPE_TEXTURE_CUBE)) ||
930 (!base->depth0 && base->target == PIPE_TEXTURE_3D)) {
931 fprintf(stderr, "r300: texture_create: "
932 "Got invalid texture dimensions: %ix%ix%i\n",
933 base->width0, base->height0, base->depth0);
934 FREE(tex);
935 return NULL;
936 }
937
938 tex->b.b = *base;
939 tex->b.vtbl = &r300_texture_vtbl;
940 pipe_reference_init(&tex->b.b.reference, 1);
941 tex->b.b.screen = screen;
942
943 r300_setup_flags(tex);
944 if (!(base->flags & R300_RESOURCE_FLAG_TRANSFER) &&
945 !(base->bind & PIPE_BIND_SCANOUT)) {
946 r300_setup_tiling(screen, tex);
947 }
948 r300_setup_miptree(rscreen, tex);
949 r300_texture_3d_fix_mipmapping(rscreen, tex);
950 r300_texture_setup_immutable_state(rscreen, tex);
951 r300_texture_setup_fb_state(rscreen, tex);
952
953 SCREEN_DBG(rscreen, DBG_TEX,
954 "r300: texture_create: Macro: %s, Micro: %s, Pitch: %i, "
955 "Dim: %ix%ix%i, LastLevel: %i, Format: %s\n",
956 tex->macrotile ? "YES" : " NO",
957 tex->microtile ? "YES" : " NO",
958 tex->hwpitch[0],
959 base->width0, base->height0, base->depth0, base->last_level,
960 util_format_short_name(base->format));
961
962 tex->buffer = rws->buffer_create(rws, 2048,
963 base->bind,
964 tex->size);
965 rws->buffer_set_tiling(rws, tex->buffer,
966 tex->pitch[0],
967 tex->microtile,
968 tex->macrotile);
969
970 if (!tex->buffer) {
971 FREE(tex);
972 return NULL;
973 }
974
975 return (struct pipe_resource*)tex;
976 }
977
978 /* Not required to implement u_resource_vtbl, consider moving to another file:
979 */
980 struct pipe_surface* r300_get_tex_surface(struct pipe_screen* screen,
981 struct pipe_resource* texture,
982 unsigned face,
983 unsigned level,
984 unsigned zslice,
985 unsigned flags)
986 {
987 struct r300_texture* tex = r300_texture(texture);
988 struct pipe_surface* surface = CALLOC_STRUCT(pipe_surface);
989 unsigned offset;
990
991 offset = r300_texture_get_offset(tex, level, zslice, face);
992
993 if (surface) {
994 pipe_reference_init(&surface->reference, 1);
995 pipe_resource_reference(&surface->texture, texture);
996 surface->format = texture->format;
997 surface->width = u_minify(texture->width0, level);
998 surface->height = u_minify(texture->height0, level);
999 surface->offset = offset;
1000 surface->usage = flags;
1001 surface->zslice = zslice;
1002 surface->texture = texture;
1003 surface->face = face;
1004 surface->level = level;
1005 }
1006
1007 return surface;
1008 }
1009
1010 /* Not required to implement u_resource_vtbl, consider moving to another file:
1011 */
1012 void r300_tex_surface_destroy(struct pipe_surface* s)
1013 {
1014 pipe_resource_reference(&s->texture, NULL);
1015 FREE(s);
1016 }
1017
1018 struct pipe_resource*
1019 r300_texture_from_handle(struct pipe_screen* screen,
1020 const struct pipe_resource* base,
1021 struct winsys_handle *whandle)
1022 {
1023 struct r300_winsys_screen *rws = (struct r300_winsys_screen*)screen->winsys;
1024 struct r300_screen* rscreen = r300_screen(screen);
1025 struct r300_winsys_buffer *buffer;
1026 struct r300_texture* tex;
1027 unsigned stride;
1028 boolean override_zb_flags;
1029
1030 /* Support only 2D textures without mipmaps */
1031 if (base->target != PIPE_TEXTURE_2D ||
1032 base->depth0 != 1 ||
1033 base->last_level != 0) {
1034 return NULL;
1035 }
1036
1037 buffer = rws->buffer_from_handle(rws, screen, whandle, &stride);
1038 if (!buffer) {
1039 return NULL;
1040 }
1041
1042 tex = CALLOC_STRUCT(r300_texture);
1043 if (!tex) {
1044 return NULL;
1045 }
1046
1047 tex->b.b = *base;
1048 tex->b.vtbl = &r300_texture_vtbl;
1049 pipe_reference_init(&tex->b.b.reference, 1);
1050 tex->b.b.screen = screen;
1051
1052 tex->stride_override = stride;
1053
1054 /* one ref already taken */
1055 tex->buffer = buffer;
1056
1057 rws->buffer_get_tiling(rws, buffer, &tex->microtile, &tex->macrotile);
1058 r300_setup_flags(tex);
1059 SCREEN_DBG(rscreen, DBG_TEX,
1060 "r300: texture_from_handle: Macro: %s, Micro: %s, "
1061 "Pitch: % 4i, Dim: %ix%i, Format: %s\n",
1062 tex->macrotile ? "YES" : " NO",
1063 tex->microtile ? "YES" : " NO",
1064 stride / util_format_get_blocksize(base->format),
1065 base->width0, base->height0,
1066 util_format_short_name(base->format));
1067
1068 /* Enforce microtiled zbuffer. */
1069 override_zb_flags = util_format_is_depth_or_stencil(base->format) &&
1070 tex->microtile == R300_BUFFER_LINEAR;
1071
1072 if (override_zb_flags) {
1073 switch (util_format_get_blocksize(base->format)) {
1074 case 4:
1075 tex->microtile = R300_BUFFER_TILED;
1076 break;
1077
1078 case 2:
1079 if (rws->get_value(rws, R300_VID_SQUARE_TILING_SUPPORT)) {
1080 tex->microtile = R300_BUFFER_SQUARETILED;
1081 break;
1082 }
1083 /* Pass through. */
1084
1085 default:
1086 override_zb_flags = FALSE;
1087 }
1088 }
1089
1090 r300_setup_miptree(rscreen, tex);
1091 r300_texture_setup_immutable_state(rscreen, tex);
1092 r300_texture_setup_fb_state(rscreen, tex);
1093
1094 if (override_zb_flags) {
1095 rws->buffer_set_tiling(rws, tex->buffer,
1096 tex->pitch[0],
1097 tex->microtile,
1098 tex->macrotile);
1099 }
1100 return (struct pipe_resource*)tex;
1101 }