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