Merge branch 'gallium-drm-driver-drescriptor'
[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 /* XXX NO! just no! */
41 #include "state_tracker/drm_driver.h"
42
43 enum r300_dim {
44 DIM_WIDTH = 0,
45 DIM_HEIGHT = 1
46 };
47
48 unsigned r300_get_swizzle_combined(const unsigned char *swizzle_format,
49 const unsigned char *swizzle_view)
50 {
51 unsigned i;
52 unsigned char swizzle[4];
53 unsigned result = 0;
54 const uint32_t swizzle_shift[4] = {
55 R300_TX_FORMAT_R_SHIFT,
56 R300_TX_FORMAT_G_SHIFT,
57 R300_TX_FORMAT_B_SHIFT,
58 R300_TX_FORMAT_A_SHIFT
59 };
60 const uint32_t swizzle_bit[4] = {
61 R300_TX_FORMAT_X,
62 R300_TX_FORMAT_Y,
63 R300_TX_FORMAT_Z,
64 R300_TX_FORMAT_W
65 };
66
67 if (swizzle_view) {
68 /* Combine two sets of swizzles. */
69 for (i = 0; i < 4; i++) {
70 swizzle[i] = swizzle_view[i] <= UTIL_FORMAT_SWIZZLE_W ?
71 swizzle_format[swizzle_view[i]] : swizzle_view[i];
72 }
73 } else {
74 memcpy(swizzle, swizzle_format, 4);
75 }
76
77 /* Get swizzle. */
78 for (i = 0; i < 4; i++) {
79 switch (swizzle[i]) {
80 case UTIL_FORMAT_SWIZZLE_Y:
81 result |= swizzle_bit[1] << swizzle_shift[i];
82 break;
83 case UTIL_FORMAT_SWIZZLE_Z:
84 result |= swizzle_bit[2] << swizzle_shift[i];
85 break;
86 case UTIL_FORMAT_SWIZZLE_W:
87 result |= swizzle_bit[3] << swizzle_shift[i];
88 break;
89 case UTIL_FORMAT_SWIZZLE_0:
90 result |= R300_TX_FORMAT_ZERO << swizzle_shift[i];
91 break;
92 case UTIL_FORMAT_SWIZZLE_1:
93 result |= R300_TX_FORMAT_ONE << swizzle_shift[i];
94 break;
95 default: /* UTIL_FORMAT_SWIZZLE_X */
96 result |= swizzle_bit[0] << swizzle_shift[i];
97 }
98 }
99 return result;
100 }
101
102 /* Translate a pipe_format into a useful texture format for sampling.
103 *
104 * Some special formats are translated directly using R300_EASY_TX_FORMAT,
105 * but the majority of them is translated in a generic way, automatically
106 * supporting all the formats hw can support.
107 *
108 * R300_EASY_TX_FORMAT swizzles the texture.
109 * Note the signature of R300_EASY_TX_FORMAT:
110 * R300_EASY_TX_FORMAT(B, G, R, A, FORMAT);
111 *
112 * The FORMAT specifies how the texture sampler will treat the texture, and
113 * makes available X, Y, Z, W, ZERO, and ONE for swizzling. */
114 uint32_t r300_translate_texformat(enum pipe_format format,
115 const unsigned char *swizzle_view)
116 {
117 uint32_t result = 0;
118 const struct util_format_description *desc;
119 unsigned i;
120 boolean uniform = TRUE;
121 const uint32_t sign_bit[4] = {
122 R300_TX_FORMAT_SIGNED_X,
123 R300_TX_FORMAT_SIGNED_Y,
124 R300_TX_FORMAT_SIGNED_Z,
125 R300_TX_FORMAT_SIGNED_W,
126 };
127
128 desc = util_format_description(format);
129
130 /* Colorspace (return non-RGB formats directly). */
131 switch (desc->colorspace) {
132 /* Depth stencil formats.
133 * Swizzles are added in r300_merge_textures_and_samplers. */
134 case UTIL_FORMAT_COLORSPACE_ZS:
135 switch (format) {
136 case PIPE_FORMAT_Z16_UNORM:
137 return R300_TX_FORMAT_X16;
138 case PIPE_FORMAT_X8Z24_UNORM:
139 case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
140 return R500_TX_FORMAT_Y8X24;
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
176 /* S3TC formats. */
177 if (desc->layout == UTIL_FORMAT_LAYOUT_S3TC) {
178 if (!util_format_s3tc_enabled) {
179 return ~0; /* Unsupported. */
180 }
181
182 switch (format) {
183 case PIPE_FORMAT_DXT1_RGB:
184 case PIPE_FORMAT_DXT1_RGBA:
185 case PIPE_FORMAT_DXT1_SRGB:
186 case PIPE_FORMAT_DXT1_SRGBA:
187 return R300_TX_FORMAT_DXT1 | result;
188 case PIPE_FORMAT_DXT3_RGBA:
189 case PIPE_FORMAT_DXT3_SRGBA:
190 return R300_TX_FORMAT_DXT3 | result;
191 case PIPE_FORMAT_DXT5_RGBA:
192 case PIPE_FORMAT_DXT5_SRGBA:
193 return R300_TX_FORMAT_DXT5 | result;
194 default:
195 return ~0; /* Unsupported/unknown. */
196 }
197 }
198
199 /* Add sign. */
200 for (i = 0; i < desc->nr_channels; i++) {
201 if (desc->channel[i].type == UTIL_FORMAT_TYPE_SIGNED) {
202 result |= sign_bit[i];
203 }
204 }
205
206 /* This is truly a special format.
207 * It stores R8G8 and B is computed using sqrt(1 - R^2 - G^2)
208 * in the sampler unit. Also known as D3DFMT_CxV8U8. */
209 if (format == PIPE_FORMAT_R8G8Bx_SNORM) {
210 return R300_TX_FORMAT_CxV8U8 | result;
211 }
212
213 /* RGTC formats. */
214 if (desc->layout == UTIL_FORMAT_LAYOUT_RGTC) {
215 switch (format) {
216 case PIPE_FORMAT_RGTC1_UNORM:
217 case PIPE_FORMAT_RGTC1_SNORM:
218 return R500_TX_FORMAT_ATI1N | result;
219 case PIPE_FORMAT_RGTC2_UNORM:
220 case PIPE_FORMAT_RGTC2_SNORM:
221 return R400_TX_FORMAT_ATI2N | result;
222 default:
223 return ~0; /* Unsupported/unknown. */
224 }
225 }
226
227 /* See whether the components are of the same size. */
228 for (i = 1; i < desc->nr_channels; i++) {
229 uniform = uniform && desc->channel[0].size == desc->channel[i].size;
230 }
231
232 /* Non-uniform formats. */
233 if (!uniform) {
234 switch (desc->nr_channels) {
235 case 3:
236 if (desc->channel[0].size == 5 &&
237 desc->channel[1].size == 6 &&
238 desc->channel[2].size == 5) {
239 return R300_TX_FORMAT_Z5Y6X5 | result;
240 }
241 if (desc->channel[0].size == 5 &&
242 desc->channel[1].size == 5 &&
243 desc->channel[2].size == 6) {
244 return R300_TX_FORMAT_Z6Y5X5 | result;
245 }
246 return ~0; /* Unsupported/unknown. */
247
248 case 4:
249 if (desc->channel[0].size == 5 &&
250 desc->channel[1].size == 5 &&
251 desc->channel[2].size == 5 &&
252 desc->channel[3].size == 1) {
253 return R300_TX_FORMAT_W1Z5Y5X5 | result;
254 }
255 if (desc->channel[0].size == 10 &&
256 desc->channel[1].size == 10 &&
257 desc->channel[2].size == 10 &&
258 desc->channel[3].size == 2) {
259 return R300_TX_FORMAT_W2Z10Y10X10 | result;
260 }
261 }
262 return ~0; /* Unsupported/unknown. */
263 }
264
265 /* And finally, uniform formats. */
266 switch (desc->channel[0].type) {
267 case UTIL_FORMAT_TYPE_UNSIGNED:
268 case UTIL_FORMAT_TYPE_SIGNED:
269 if (!desc->channel[0].normalized &&
270 desc->colorspace != UTIL_FORMAT_COLORSPACE_SRGB) {
271 return ~0;
272 }
273
274 switch (desc->channel[0].size) {
275 case 4:
276 switch (desc->nr_channels) {
277 case 2:
278 return R300_TX_FORMAT_Y4X4 | result;
279 case 4:
280 return R300_TX_FORMAT_W4Z4Y4X4 | result;
281 }
282 return ~0;
283
284 case 8:
285 switch (desc->nr_channels) {
286 case 1:
287 return R300_TX_FORMAT_X8 | result;
288 case 2:
289 return R300_TX_FORMAT_Y8X8 | result;
290 case 4:
291 return R300_TX_FORMAT_W8Z8Y8X8 | result;
292 }
293 return ~0;
294
295 case 16:
296 switch (desc->nr_channels) {
297 case 1:
298 return R300_TX_FORMAT_X16 | result;
299 case 2:
300 return R300_TX_FORMAT_Y16X16 | result;
301 case 4:
302 return R300_TX_FORMAT_W16Z16Y16X16 | result;
303 }
304 }
305 return ~0;
306
307 case UTIL_FORMAT_TYPE_FLOAT:
308 switch (desc->channel[0].size) {
309 case 16:
310 switch (desc->nr_channels) {
311 case 1:
312 return R300_TX_FORMAT_16F | result;
313 case 2:
314 return R300_TX_FORMAT_16F_16F | result;
315 case 4:
316 return R300_TX_FORMAT_16F_16F_16F_16F | result;
317 }
318 return ~0;
319
320 case 32:
321 switch (desc->nr_channels) {
322 case 1:
323 return R300_TX_FORMAT_32F | result;
324 case 2:
325 return R300_TX_FORMAT_32F_32F | result;
326 case 4:
327 return R300_TX_FORMAT_32F_32F_32F_32F | result;
328 }
329 }
330 }
331
332 return ~0; /* Unsupported/unknown. */
333 }
334
335 uint32_t r500_tx_format_msb_bit(enum pipe_format format)
336 {
337 switch (format) {
338 case PIPE_FORMAT_RGTC1_UNORM:
339 case PIPE_FORMAT_RGTC1_SNORM:
340 case PIPE_FORMAT_X8Z24_UNORM:
341 case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
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.pitch[i] =
598 tex->hwpitch[i] |
599 R300_DEPTHMACROTILE(tex->mip_macrotile[i]) |
600 R300_DEPTHMICROTILE(tex->microtile);
601 }
602 tex->fb_state.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.pitch[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.format = 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,
622 "r300: texture_reinterpret_format: %s -> %s\n",
623 util_format_short_name(tex->format),
624 util_format_short_name(new_format));
625
626 tex->format = new_format;
627
628 r300_texture_setup_fb_state(r300_screen(screen), r300_texture(tex));
629 }
630
631 unsigned r300_texture_get_offset(struct r300_texture* tex, unsigned level,
632 unsigned zslice, unsigned face)
633 {
634 unsigned offset = tex->offset[level];
635
636 switch (tex->b.b.target) {
637 case PIPE_TEXTURE_3D:
638 assert(face == 0);
639 return offset + zslice * tex->layer_size[level];
640
641 case PIPE_TEXTURE_CUBE:
642 assert(zslice == 0);
643 return offset + face * tex->layer_size[level];
644
645 default:
646 assert(zslice == 0 && face == 0);
647 return offset;
648 }
649 }
650
651 /* Returns the number of pixels that the texture should be aligned to
652 * in the given dimension. */
653 static unsigned r300_get_pixel_alignment(struct r300_texture *tex,
654 enum r300_buffer_tiling macrotile,
655 enum r300_dim dim)
656 {
657 static const unsigned table[2][5][3][2] =
658 {
659 {
660 /* Macro: linear linear linear
661 Micro: linear tiled square-tiled */
662 {{ 32, 1}, { 8, 4}, { 0, 0}}, /* 8 bits per pixel */
663 {{ 16, 1}, { 8, 2}, { 4, 4}}, /* 16 bits per pixel */
664 {{ 8, 1}, { 4, 2}, { 0, 0}}, /* 32 bits per pixel */
665 {{ 4, 1}, { 0, 0}, { 2, 2}}, /* 64 bits per pixel */
666 {{ 2, 1}, { 0, 0}, { 0, 0}} /* 128 bits per pixel */
667 },
668 {
669 /* Macro: tiled tiled tiled
670 Micro: linear tiled square-tiled */
671 {{256, 8}, {64, 32}, { 0, 0}}, /* 8 bits per pixel */
672 {{128, 8}, {64, 16}, {32, 32}}, /* 16 bits per pixel */
673 {{ 64, 8}, {32, 16}, { 0, 0}}, /* 32 bits per pixel */
674 {{ 32, 8}, { 0, 0}, {16, 16}}, /* 64 bits per pixel */
675 {{ 16, 8}, { 0, 0}, { 0, 0}} /* 128 bits per pixel */
676 }
677 };
678 static const unsigned aa_block[2] = {4, 8};
679 unsigned res = 0;
680 unsigned pixsize = util_format_get_blocksize(tex->b.b.format);
681
682 assert(macrotile <= R300_BUFFER_TILED);
683 assert(tex->microtile <= R300_BUFFER_SQUARETILED);
684 assert(pixsize <= 16);
685 assert(dim <= DIM_HEIGHT);
686
687 if (tex->b.b.nr_samples > 1) {
688 /* Multisampled textures have their own alignment scheme. */
689 if (pixsize == 4)
690 res = aa_block[dim];
691 } else {
692 /* Standard alignment. */
693 res = table[macrotile][util_logbase2(pixsize)][tex->microtile][dim];
694 }
695
696 assert(res);
697 return res;
698 }
699
700 /* Return true if macrotiling should be enabled on the miplevel. */
701 static boolean r300_texture_macro_switch(struct r300_texture *tex,
702 unsigned level,
703 boolean rv350_mode,
704 enum r300_dim dim)
705 {
706 unsigned tile, texdim;
707
708 tile = r300_get_pixel_alignment(tex, R300_BUFFER_TILED, dim);
709 if (dim == DIM_WIDTH) {
710 texdim = u_minify(tex->b.b.width0, level);
711 } else {
712 texdim = u_minify(tex->b.b.height0, level);
713 }
714
715 /* See TX_FILTER1_n.MACRO_SWITCH. */
716 if (rv350_mode) {
717 return texdim >= tile;
718 } else {
719 return texdim > tile;
720 }
721 }
722
723 /**
724 * Return the stride, in bytes, of the texture images of the given texture
725 * at the given level.
726 */
727 unsigned r300_texture_get_stride(struct r300_screen* screen,
728 struct r300_texture* tex, unsigned level)
729 {
730 unsigned tile_width, width, stride;
731
732 if (tex->stride_override)
733 return tex->stride_override;
734
735 /* Check the level. */
736 if (level > tex->b.b.last_level) {
737 SCREEN_DBG(screen, DBG_TEX, "%s: level (%u) > last_level (%u)\n",
738 __FUNCTION__, level, tex->b.b.last_level);
739 return 0;
740 }
741
742 width = u_minify(tex->b.b.width0, level);
743
744 if (util_format_is_plain(tex->b.b.format)) {
745 tile_width = r300_get_pixel_alignment(tex, tex->mip_macrotile[level],
746 DIM_WIDTH);
747 width = align(width, tile_width);
748
749 stride = util_format_get_stride(tex->b.b.format, width);
750
751 /* Some IGPs need a minimum stride of 64 bytes, hmm...
752 * This doesn't seem to apply to tiled textures, according to r300c. */
753 if (!tex->microtile && !tex->mip_macrotile[level] &&
754 (screen->caps.family == CHIP_FAMILY_RS600 ||
755 screen->caps.family == CHIP_FAMILY_RS690 ||
756 screen->caps.family == CHIP_FAMILY_RS740)) {
757 return stride < 64 ? 64 : stride;
758 }
759
760 /* The alignment to 32 bytes is sort of implied by the layout... */
761 return stride;
762 } else {
763 return align(util_format_get_stride(tex->b.b.format, width), 32);
764 }
765 }
766
767 static unsigned r300_texture_get_nblocksy(struct r300_texture* tex,
768 unsigned level)
769 {
770 unsigned height, tile_height;
771
772 height = u_minify(tex->b.b.height0, level);
773
774 if (util_format_is_plain(tex->b.b.format)) {
775 tile_height = r300_get_pixel_alignment(tex, tex->mip_macrotile[level],
776 DIM_HEIGHT);
777 height = align(height, tile_height);
778
779 /* This is needed for the kernel checker, unfortunately. */
780 height = util_next_power_of_two(height);
781 }
782
783 return util_format_get_nblocksy(tex->b.b.format, height);
784 }
785
786 static void r300_texture_3d_fix_mipmapping(struct r300_screen *screen,
787 struct r300_texture *tex)
788 {
789 /* The kernels <= 2.6.34-rc4 compute the size of mipmapped 3D textures
790 * incorrectly. This is a workaround to prevent CS from being rejected. */
791
792 unsigned i, size;
793
794 if (!screen->rws->get_value(screen->rws, R300_VID_DRM_2_3_0) &&
795 tex->b.b.target == PIPE_TEXTURE_3D &&
796 tex->b.b.last_level > 0) {
797 size = 0;
798
799 for (i = 0; i <= tex->b.b.last_level; i++) {
800 size += r300_texture_get_stride(screen, tex, i) *
801 r300_texture_get_nblocksy(tex, i);
802 }
803
804 size *= tex->b.b.depth0;
805 tex->size = size;
806 }
807 }
808
809 static void r300_setup_miptree(struct r300_screen* screen,
810 struct r300_texture* tex)
811 {
812 struct pipe_resource* base = &tex->b.b;
813 unsigned stride, size, layer_size, nblocksy, i;
814 boolean rv350_mode = screen->caps.is_rv350;
815
816 SCREEN_DBG(screen, DBG_TEXALLOC,
817 "r300: Making miptree for texture, format %s\n",
818 util_format_short_name(base->format));
819
820 for (i = 0; i <= base->last_level; i++) {
821 /* Let's see if this miplevel can be macrotiled. */
822 tex->mip_macrotile[i] =
823 (tex->macrotile == R300_BUFFER_TILED &&
824 r300_texture_macro_switch(tex, i, rv350_mode, DIM_WIDTH) &&
825 r300_texture_macro_switch(tex, i, rv350_mode, DIM_HEIGHT)) ?
826 R300_BUFFER_TILED : R300_BUFFER_LINEAR;
827
828 stride = r300_texture_get_stride(screen, tex, i);
829 nblocksy = r300_texture_get_nblocksy(tex, i);
830 layer_size = stride * nblocksy;
831
832 if (base->nr_samples) {
833 layer_size *= base->nr_samples;
834 }
835
836 if (base->target == PIPE_TEXTURE_CUBE)
837 size = layer_size * 6;
838 else
839 size = layer_size * u_minify(base->depth0, i);
840
841 tex->offset[i] = tex->size;
842 tex->size = tex->offset[i] + size;
843 tex->layer_size[i] = layer_size;
844 tex->pitch[i] = stride / util_format_get_blocksize(base->format);
845 tex->hwpitch[i] =
846 tex->pitch[i] * util_format_get_blockwidth(base->format);
847
848 SCREEN_DBG(screen, DBG_TEXALLOC, "r300: Texture miptree: Level %d "
849 "(%dx%dx%d px, pitch %d bytes) %d bytes total, macrotiled %s\n",
850 i, u_minify(base->width0, i), u_minify(base->height0, i),
851 u_minify(base->depth0, i), stride, tex->size,
852 tex->mip_macrotile[i] ? "TRUE" : "FALSE");
853 }
854 }
855
856 static void r300_setup_flags(struct r300_texture* tex)
857 {
858 tex->uses_pitch = !util_is_power_of_two(tex->b.b.width0) ||
859 !util_is_power_of_two(tex->b.b.height0) ||
860 tex->stride_override;
861 }
862
863 static void r300_setup_tiling(struct pipe_screen *screen,
864 struct r300_texture *tex)
865 {
866 struct r300_winsys_screen *rws = (struct r300_winsys_screen *)screen->winsys;
867 enum pipe_format format = tex->b.b.format;
868 boolean rv350_mode = r300_screen(screen)->caps.is_rv350;
869 boolean is_zb = util_format_is_depth_or_stencil(format);
870 boolean dbg_no_tiling = SCREEN_DBG_ON(r300_screen(screen), DBG_NO_TILING);
871
872 if (!util_format_is_plain(format)) {
873 return;
874 }
875
876 /* If height == 1, disable microtiling except for zbuffer. */
877 if (!is_zb && (tex->b.b.height0 == 1 || dbg_no_tiling)) {
878 return;
879 }
880
881 /* Set microtiling. */
882 switch (util_format_get_blocksize(format)) {
883 case 1:
884 case 4:
885 tex->microtile = R300_BUFFER_TILED;
886 break;
887
888 case 2:
889 case 8:
890 if (rws->get_value(rws, R300_VID_SQUARE_TILING_SUPPORT)) {
891 tex->microtile = R300_BUFFER_SQUARETILED;
892 }
893 break;
894 }
895
896 if (dbg_no_tiling) {
897 return;
898 }
899
900 /* Set macrotiling. */
901 if (r300_texture_macro_switch(tex, 0, rv350_mode, DIM_WIDTH) &&
902 r300_texture_macro_switch(tex, 0, rv350_mode, DIM_HEIGHT)) {
903 tex->macrotile = R300_BUFFER_TILED;
904 }
905 }
906
907 static unsigned r300_texture_is_referenced(struct pipe_context *context,
908 struct pipe_resource *texture,
909 unsigned face, unsigned level)
910 {
911 struct r300_context *r300 = r300_context(context);
912 struct r300_texture *rtex = (struct r300_texture *)texture;
913
914 if (r300->rws->is_buffer_referenced(r300->rws, rtex->buffer, R300_REF_CS))
915 return PIPE_REFERENCED_FOR_READ | PIPE_REFERENCED_FOR_WRITE;
916
917 return PIPE_UNREFERENCED;
918 }
919
920 static void r300_texture_destroy(struct pipe_screen *screen,
921 struct pipe_resource* texture)
922 {
923 struct r300_texture* tex = (struct r300_texture*)texture;
924 struct r300_winsys_screen *rws = (struct r300_winsys_screen *)texture->screen->winsys;
925
926 rws->buffer_reference(rws, &tex->buffer, NULL);
927 FREE(tex);
928 }
929
930 static boolean r300_texture_get_handle(struct pipe_screen* screen,
931 struct pipe_resource *texture,
932 struct winsys_handle *whandle)
933 {
934 struct r300_winsys_screen *rws = (struct r300_winsys_screen *)screen->winsys;
935 struct r300_texture* tex = (struct r300_texture*)texture;
936
937 if (!tex) {
938 return FALSE;
939 }
940
941 whandle->stride = r300_texture_get_stride(r300_screen(screen), tex, 0);
942
943 return rws->buffer_get_handle(rws, tex->buffer, whandle);
944 }
945
946 struct u_resource_vtbl r300_texture_vtbl =
947 {
948 r300_texture_get_handle, /* get_handle */
949 r300_texture_destroy, /* resource_destroy */
950 r300_texture_is_referenced, /* is_resource_referenced */
951 r300_texture_get_transfer, /* get_transfer */
952 r300_texture_transfer_destroy, /* transfer_destroy */
953 r300_texture_transfer_map, /* transfer_map */
954 u_default_transfer_flush_region, /* transfer_flush_region */
955 r300_texture_transfer_unmap, /* transfer_unmap */
956 u_default_transfer_inline_write /* transfer_inline_write */
957 };
958
959 /* Create a new texture. */
960 struct pipe_resource* r300_texture_create(struct pipe_screen* screen,
961 const struct pipe_resource* base)
962 {
963 struct r300_texture* tex = CALLOC_STRUCT(r300_texture);
964 struct r300_screen* rscreen = r300_screen(screen);
965 struct r300_winsys_screen *rws = (struct r300_winsys_screen *)screen->winsys;
966
967 if (!tex) {
968 return NULL;
969 }
970
971 /* Refuse to create a texture with size 0. */
972 if (!base->width0 ||
973 (!base->height0 && (base->target == PIPE_TEXTURE_2D ||
974 base->target == PIPE_TEXTURE_CUBE)) ||
975 (!base->depth0 && base->target == PIPE_TEXTURE_3D)) {
976 fprintf(stderr, "r300: texture_create: "
977 "Got invalid texture dimensions: %ix%ix%i\n",
978 base->width0, base->height0, base->depth0);
979 FREE(tex);
980 return NULL;
981 }
982
983 tex->b.b = *base;
984 tex->b.vtbl = &r300_texture_vtbl;
985 pipe_reference_init(&tex->b.b.reference, 1);
986 tex->b.b.screen = screen;
987
988 r300_setup_flags(tex);
989 if (!(base->flags & R300_RESOURCE_FLAG_TRANSFER) &&
990 !(base->bind & PIPE_BIND_SCANOUT)) {
991 r300_setup_tiling(screen, tex);
992 }
993 r300_setup_miptree(rscreen, tex);
994 r300_texture_3d_fix_mipmapping(rscreen, tex);
995 r300_texture_setup_immutable_state(rscreen, tex);
996 r300_texture_setup_fb_state(rscreen, tex);
997
998 SCREEN_DBG(rscreen, DBG_TEX,
999 "r300: texture_create: Macro: %s, Micro: %s, Pitch: %i, "
1000 "Dim: %ix%ix%i, LastLevel: %i, Size: %i, Format: %s\n",
1001 tex->macrotile ? "YES" : " NO",
1002 tex->microtile ? "YES" : " NO",
1003 tex->hwpitch[0],
1004 base->width0, base->height0, base->depth0, base->last_level,
1005 tex->size,
1006 util_format_short_name(base->format));
1007
1008 tex->domain = base->flags & R300_RESOURCE_FLAG_TRANSFER ? R300_DOMAIN_GTT :
1009 R300_DOMAIN_VRAM;
1010
1011 tex->buffer = rws->buffer_create(rws, 2048, base->bind, tex->domain,
1012 tex->size);
1013
1014 if (!tex->buffer) {
1015 FREE(tex);
1016 return NULL;
1017 }
1018
1019 rws->buffer_set_tiling(rws, tex->buffer,
1020 tex->pitch[0] * util_format_get_blocksize(tex->b.b.format),
1021 tex->microtile,
1022 tex->macrotile);
1023
1024 return (struct pipe_resource*)tex;
1025 }
1026
1027 /* Not required to implement u_resource_vtbl, consider moving to another file:
1028 */
1029 struct pipe_surface* r300_get_tex_surface(struct pipe_screen* screen,
1030 struct pipe_resource* texture,
1031 unsigned face,
1032 unsigned level,
1033 unsigned zslice,
1034 unsigned flags)
1035 {
1036 struct r300_texture* tex = r300_texture(texture);
1037 struct r300_surface* surface = CALLOC_STRUCT(r300_surface);
1038
1039 if (surface) {
1040 pipe_reference_init(&surface->base.reference, 1);
1041 pipe_resource_reference(&surface->base.texture, texture);
1042 surface->base.format = texture->format;
1043 surface->base.width = u_minify(texture->width0, level);
1044 surface->base.height = u_minify(texture->height0, level);
1045 surface->base.usage = flags;
1046 surface->base.zslice = zslice;
1047 surface->base.face = face;
1048 surface->base.level = level;
1049
1050 surface->buffer = tex->buffer;
1051 surface->domain = tex->domain;
1052 surface->offset = r300_texture_get_offset(tex, level, zslice, face);
1053 surface->pitch = tex->fb_state.pitch[level];
1054 surface->format = tex->fb_state.format;
1055 }
1056
1057 return &surface->base;
1058 }
1059
1060 /* Not required to implement u_resource_vtbl, consider moving to another file:
1061 */
1062 void r300_tex_surface_destroy(struct pipe_surface* s)
1063 {
1064 pipe_resource_reference(&s->texture, NULL);
1065 FREE(s);
1066 }
1067
1068 struct pipe_resource*
1069 r300_texture_from_handle(struct pipe_screen* screen,
1070 const struct pipe_resource* base,
1071 struct winsys_handle *whandle)
1072 {
1073 struct r300_winsys_screen *rws = (struct r300_winsys_screen*)screen->winsys;
1074 struct r300_screen* rscreen = r300_screen(screen);
1075 struct r300_winsys_buffer *buffer;
1076 struct r300_texture* tex;
1077 boolean override_zb_flags;
1078
1079 /* Support only 2D textures without mipmaps */
1080 if (base->target != PIPE_TEXTURE_2D ||
1081 base->depth0 != 1 ||
1082 base->last_level != 0) {
1083 return NULL;
1084 }
1085
1086 /* XXX make the winsys return the stride_override, see i915_resource_texture.c:830 */
1087 buffer = rws->buffer_from_handle(rws, whandle->handle);
1088 if (!buffer) {
1089 return NULL;
1090 }
1091
1092 tex = CALLOC_STRUCT(r300_texture);
1093 if (!tex) {
1094 return NULL;
1095 }
1096
1097 tex->b.b = *base;
1098 tex->b.vtbl = &r300_texture_vtbl;
1099 pipe_reference_init(&tex->b.b.reference, 1);
1100 tex->b.b.screen = screen;
1101 tex->domain = R300_DOMAIN_VRAM;
1102
1103 tex->stride_override = whandle->stride;
1104
1105 /* one ref already taken */
1106 tex->buffer = buffer;
1107
1108 rws->buffer_get_tiling(rws, buffer, &tex->microtile, &tex->macrotile);
1109 r300_setup_flags(tex);
1110 SCREEN_DBG(rscreen, DBG_TEX,
1111 "r300: texture_from_handle: Macro: %s, Micro: %s, "
1112 "Pitch: % 4i, Dim: %ix%i, Format: %s\n",
1113 tex->macrotile ? "YES" : " NO",
1114 tex->microtile ? "YES" : " NO",
1115 whandle->stride / util_format_get_blocksize(base->format),
1116 base->width0, base->height0,
1117 util_format_short_name(base->format));
1118
1119 /* Enforce microtiled zbuffer. */
1120 override_zb_flags = util_format_is_depth_or_stencil(base->format) &&
1121 tex->microtile == R300_BUFFER_LINEAR;
1122
1123 if (override_zb_flags) {
1124 switch (util_format_get_blocksize(base->format)) {
1125 case 4:
1126 tex->microtile = R300_BUFFER_TILED;
1127 break;
1128
1129 case 2:
1130 if (rws->get_value(rws, R300_VID_SQUARE_TILING_SUPPORT)) {
1131 tex->microtile = R300_BUFFER_SQUARETILED;
1132 break;
1133 }
1134 /* Pass through. */
1135
1136 default:
1137 override_zb_flags = FALSE;
1138 }
1139 }
1140
1141 r300_setup_miptree(rscreen, tex);
1142 r300_texture_setup_immutable_state(rscreen, tex);
1143 r300_texture_setup_fb_state(rscreen, tex);
1144
1145 if (override_zb_flags) {
1146 rws->buffer_set_tiling(rws, tex->buffer,
1147 tex->pitch[0] * util_format_get_blocksize(tex->b.b.format),
1148 tex->microtile,
1149 tex->macrotile);
1150 }
1151 return (struct pipe_resource*)tex;
1152 }