gallium: adjust the query interface to support custom types
[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 R500_TX_FORMAT_Y8X24;
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 case PIPE_FORMAT_X8Z24_UNORM:
343 case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
344 return R500_TXFORMAT_MSB;
345 default:
346 return 0;
347 }
348 }
349
350 /* Buffer formats. */
351
352 /* Colorbuffer formats. This is the unswizzled format of the RB3D block's
353 * output. For the swizzling of the targets, check the shader's format. */
354 static uint32_t r300_translate_colorformat(enum pipe_format format)
355 {
356 switch (format) {
357 /* 8-bit buffers. */
358 case PIPE_FORMAT_A8_UNORM:
359 case PIPE_FORMAT_I8_UNORM:
360 case PIPE_FORMAT_L8_UNORM:
361 case PIPE_FORMAT_R8_UNORM:
362 case PIPE_FORMAT_R8_SNORM:
363 return R300_COLOR_FORMAT_I8;
364
365 /* 16-bit buffers. */
366 case PIPE_FORMAT_B5G6R5_UNORM:
367 return R300_COLOR_FORMAT_RGB565;
368
369 case PIPE_FORMAT_B5G5R5A1_UNORM:
370 case PIPE_FORMAT_B5G5R5X1_UNORM:
371 return R300_COLOR_FORMAT_ARGB1555;
372
373 case PIPE_FORMAT_B4G4R4A4_UNORM:
374 case PIPE_FORMAT_B4G4R4X4_UNORM:
375 return R300_COLOR_FORMAT_ARGB4444;
376
377 /* 32-bit buffers. */
378 case PIPE_FORMAT_B8G8R8A8_UNORM:
379 case PIPE_FORMAT_B8G8R8X8_UNORM:
380 case PIPE_FORMAT_A8R8G8B8_UNORM:
381 case PIPE_FORMAT_X8R8G8B8_UNORM:
382 case PIPE_FORMAT_A8B8G8R8_UNORM:
383 case PIPE_FORMAT_R8G8B8A8_SNORM:
384 case PIPE_FORMAT_X8B8G8R8_UNORM:
385 case PIPE_FORMAT_R8G8B8X8_UNORM:
386 case PIPE_FORMAT_R8SG8SB8UX8U_NORM:
387 return R300_COLOR_FORMAT_ARGB8888;
388
389 case PIPE_FORMAT_R10G10B10A2_UNORM:
390 case PIPE_FORMAT_R10G10B10X2_SNORM:
391 case PIPE_FORMAT_B10G10R10A2_UNORM:
392 case PIPE_FORMAT_R10SG10SB10SA2U_NORM:
393 return R500_COLOR_FORMAT_ARGB2101010; /* R5xx-only? */
394
395 /* 64-bit buffers. */
396 case PIPE_FORMAT_R16G16B16A16_UNORM:
397 case PIPE_FORMAT_R16G16B16A16_SNORM:
398 case PIPE_FORMAT_R16G16B16A16_FLOAT:
399 return R300_COLOR_FORMAT_ARGB16161616;
400
401 /* 128-bit buffers. */
402 case PIPE_FORMAT_R32G32B32A32_FLOAT:
403 return R300_COLOR_FORMAT_ARGB32323232;
404
405 /* YUV buffers. */
406 case PIPE_FORMAT_UYVY:
407 return R300_COLOR_FORMAT_YVYU;
408 case PIPE_FORMAT_YUYV:
409 return R300_COLOR_FORMAT_VYUY;
410 default:
411 return ~0; /* Unsupported. */
412 }
413 }
414
415 /* Depthbuffer and stencilbuffer. Thankfully, we only support two flavors. */
416 static uint32_t r300_translate_zsformat(enum pipe_format format)
417 {
418 switch (format) {
419 /* 16-bit depth, no stencil */
420 case PIPE_FORMAT_Z16_UNORM:
421 return R300_DEPTHFORMAT_16BIT_INT_Z;
422 /* 24-bit depth, ignored stencil */
423 case PIPE_FORMAT_X8Z24_UNORM:
424 /* 24-bit depth, 8-bit stencil */
425 case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
426 return R300_DEPTHFORMAT_24BIT_INT_Z_8BIT_STENCIL;
427 default:
428 return ~0; /* Unsupported. */
429 }
430 }
431
432 /* Shader output formats. This is essentially the swizzle from the shader
433 * to the RB3D block.
434 *
435 * Note that formats are stored from C3 to C0. */
436 static uint32_t r300_translate_out_fmt(enum pipe_format format)
437 {
438 uint32_t modifier = 0;
439 unsigned i;
440 const struct util_format_description *desc;
441 static const uint32_t sign_bit[4] = {
442 R300_OUT_SIGN(0x1),
443 R300_OUT_SIGN(0x2),
444 R300_OUT_SIGN(0x4),
445 R300_OUT_SIGN(0x8),
446 };
447
448 desc = util_format_description(format);
449
450 /* Specifies how the shader output is written to the fog unit. */
451 if (desc->channel[0].type == UTIL_FORMAT_TYPE_FLOAT) {
452 if (desc->channel[0].size == 32) {
453 modifier |= R300_US_OUT_FMT_C4_32_FP;
454 } else {
455 modifier |= R300_US_OUT_FMT_C4_16_FP;
456 }
457 } else {
458 if (desc->channel[0].size == 16) {
459 modifier |= R300_US_OUT_FMT_C4_16;
460 } else {
461 /* C4_8 seems to be used for the formats whose pixel size
462 * is <= 32 bits. */
463 modifier |= R300_US_OUT_FMT_C4_8;
464 }
465 }
466
467 /* Add sign. */
468 for (i = 0; i < 4; i++)
469 if (desc->channel[i].type == UTIL_FORMAT_TYPE_SIGNED) {
470 modifier |= sign_bit[i];
471 }
472
473 /* Add swizzles and return. */
474 switch (format) {
475 /* 8-bit outputs.
476 * COLORFORMAT_I8 stores the C2 component. */
477 case PIPE_FORMAT_A8_UNORM:
478 return modifier | R300_C2_SEL_A;
479 case PIPE_FORMAT_I8_UNORM:
480 case PIPE_FORMAT_L8_UNORM:
481 case PIPE_FORMAT_R8_UNORM:
482 case PIPE_FORMAT_R8_SNORM:
483 return modifier | R300_C2_SEL_R;
484
485 /* BGRA outputs. */
486 case PIPE_FORMAT_B5G6R5_UNORM:
487 case PIPE_FORMAT_B5G5R5A1_UNORM:
488 case PIPE_FORMAT_B5G5R5X1_UNORM:
489 case PIPE_FORMAT_B4G4R4A4_UNORM:
490 case PIPE_FORMAT_B4G4R4X4_UNORM:
491 case PIPE_FORMAT_B8G8R8A8_UNORM:
492 case PIPE_FORMAT_B8G8R8X8_UNORM:
493 case PIPE_FORMAT_B10G10R10A2_UNORM:
494 return modifier |
495 R300_C0_SEL_B | R300_C1_SEL_G |
496 R300_C2_SEL_R | R300_C3_SEL_A;
497
498 /* ARGB outputs. */
499 case PIPE_FORMAT_A8R8G8B8_UNORM:
500 case PIPE_FORMAT_X8R8G8B8_UNORM:
501 return modifier |
502 R300_C0_SEL_A | R300_C1_SEL_R |
503 R300_C2_SEL_G | R300_C3_SEL_B;
504
505 /* ABGR outputs. */
506 case PIPE_FORMAT_A8B8G8R8_UNORM:
507 case PIPE_FORMAT_X8B8G8R8_UNORM:
508 return modifier |
509 R300_C0_SEL_A | R300_C1_SEL_B |
510 R300_C2_SEL_G | R300_C3_SEL_R;
511
512 /* RGBA outputs. */
513 case PIPE_FORMAT_R8G8B8X8_UNORM:
514 case PIPE_FORMAT_R8G8B8A8_SNORM:
515 case PIPE_FORMAT_R8SG8SB8UX8U_NORM:
516 case PIPE_FORMAT_R10G10B10A2_UNORM:
517 case PIPE_FORMAT_R10G10B10X2_SNORM:
518 case PIPE_FORMAT_R10SG10SB10SA2U_NORM:
519 case PIPE_FORMAT_R16G16B16A16_UNORM:
520 case PIPE_FORMAT_R16G16B16A16_SNORM:
521 case PIPE_FORMAT_R16G16B16A16_FLOAT:
522 case PIPE_FORMAT_R32G32B32A32_FLOAT:
523 return modifier |
524 R300_C0_SEL_R | R300_C1_SEL_G |
525 R300_C2_SEL_B | R300_C3_SEL_A;
526
527 default:
528 return ~0; /* Unsupported. */
529 }
530 }
531
532 boolean r300_is_colorbuffer_format_supported(enum pipe_format format)
533 {
534 return r300_translate_colorformat(format) != ~0 &&
535 r300_translate_out_fmt(format) != ~0;
536 }
537
538 boolean r300_is_zs_format_supported(enum pipe_format format)
539 {
540 return r300_translate_zsformat(format) != ~0;
541 }
542
543 boolean r300_is_sampler_format_supported(enum pipe_format format)
544 {
545 return r300_translate_texformat(format, 0) != ~0;
546 }
547
548 static void r300_texture_setup_immutable_state(struct r300_screen* screen,
549 struct r300_texture* tex)
550 {
551 struct r300_texture_format_state* f = &tex->tx_format;
552 struct pipe_resource *pt = &tex->b.b;
553 boolean is_r500 = screen->caps.is_r500;
554
555 /* Set sampler state. */
556 f->format0 = R300_TX_WIDTH((pt->width0 - 1) & 0x7ff) |
557 R300_TX_HEIGHT((pt->height0 - 1) & 0x7ff);
558
559 if (tex->uses_pitch) {
560 /* rectangles love this */
561 f->format0 |= R300_TX_PITCH_EN;
562 f->format2 = (tex->hwpitch[0] - 1) & 0x1fff;
563 } else {
564 /* power of two textures (3D, mipmaps, and no pitch) */
565 f->format0 |= R300_TX_DEPTH(util_logbase2(pt->depth0) & 0xf);
566 }
567
568 f->format1 = 0;
569 if (pt->target == PIPE_TEXTURE_CUBE) {
570 f->format1 |= R300_TX_FORMAT_CUBIC_MAP;
571 }
572 if (pt->target == PIPE_TEXTURE_3D) {
573 f->format1 |= R300_TX_FORMAT_3D;
574 }
575
576 /* large textures on r500 */
577 if (is_r500)
578 {
579 if (pt->width0 > 2048) {
580 f->format2 |= R500_TXWIDTH_BIT11;
581 }
582 if (pt->height0 > 2048) {
583 f->format2 |= R500_TXHEIGHT_BIT11;
584 }
585 }
586
587 f->tile_config = R300_TXO_MACRO_TILE(tex->macrotile) |
588 R300_TXO_MICRO_TILE(tex->microtile);
589 }
590
591 static void r300_texture_setup_fb_state(struct r300_screen* screen,
592 struct r300_texture* tex)
593 {
594 unsigned i;
595
596 /* Set framebuffer state. */
597 if (util_format_is_depth_or_stencil(tex->b.b.format)) {
598 for (i = 0; i <= tex->b.b.last_level; i++) {
599 tex->fb_state.depthpitch[i] =
600 tex->hwpitch[i] |
601 R300_DEPTHMACROTILE(tex->mip_macrotile[i]) |
602 R300_DEPTHMICROTILE(tex->microtile);
603 }
604 tex->fb_state.zb_format = r300_translate_zsformat(tex->b.b.format);
605 } else {
606 for (i = 0; i <= tex->b.b.last_level; i++) {
607 tex->fb_state.colorpitch[i] =
608 tex->hwpitch[i] |
609 r300_translate_colorformat(tex->b.b.format) |
610 R300_COLOR_TILE(tex->mip_macrotile[i]) |
611 R300_COLOR_MICROTILE(tex->microtile);
612 }
613 tex->fb_state.us_out_fmt = r300_translate_out_fmt(tex->b.b.format);
614 }
615 }
616
617 void r300_texture_reinterpret_format(struct pipe_screen *screen,
618 struct pipe_resource *tex,
619 enum pipe_format new_format)
620 {
621 struct r300_screen *r300screen = r300_screen(screen);
622
623 SCREEN_DBG(r300screen, DBG_TEX, "r300: texture_reinterpret_format: %s -> %s\n",
624 util_format_short_name(tex->format), 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 /**
652 * Return the width (dim==TILE_WIDTH) or height (dim==TILE_HEIGHT) of one tile
653 * of the given texture.
654 */
655 static unsigned r300_texture_get_tile_size(struct r300_texture* tex,
656 int dim, boolean macrotile)
657 {
658 unsigned pixsize, tile_size;
659
660 pixsize = util_format_get_blocksize(tex->b.b.format);
661 tile_size = microblock_table[util_logbase2(pixsize)][tex->microtile][dim];
662
663 if (macrotile) {
664 tile_size *= 8;
665 }
666
667 assert(tile_size);
668 return tile_size;
669 }
670
671 /* Return true if macrotiling should be enabled on the miplevel. */
672 static boolean r300_texture_macro_switch(struct r300_texture *tex,
673 unsigned level,
674 boolean rv350_mode,
675 int dim)
676 {
677 unsigned tile, texdim;
678
679 tile = r300_texture_get_tile_size(tex, dim, TRUE);
680 if (dim == TILE_WIDTH) {
681 texdim = u_minify(tex->b.b.width0, level);
682 } else {
683 texdim = u_minify(tex->b.b.height0, level);
684 }
685
686 /* See TX_FILTER1_n.MACRO_SWITCH. */
687 if (rv350_mode) {
688 return texdim >= tile;
689 } else {
690 return texdim > tile;
691 }
692 }
693
694 /**
695 * Return the stride, in bytes, of the texture images of the given texture
696 * at the given level.
697 */
698 unsigned r300_texture_get_stride(struct r300_screen* screen,
699 struct r300_texture* tex, unsigned level)
700 {
701 unsigned tile_width, width, stride;
702
703 if (tex->stride_override)
704 return tex->stride_override;
705
706 /* Check the level. */
707 if (level > tex->b.b.last_level) {
708 SCREEN_DBG(screen, DBG_TEX, "%s: level (%u) > last_level (%u)\n",
709 __FUNCTION__, level, tex->b.b.last_level);
710 return 0;
711 }
712
713 width = u_minify(tex->b.b.width0, level);
714
715 if (util_format_is_plain(tex->b.b.format)) {
716 tile_width = r300_texture_get_tile_size(tex, TILE_WIDTH,
717 tex->mip_macrotile[level]);
718 width = align(width, tile_width);
719
720 stride = util_format_get_stride(tex->b.b.format, width);
721
722 /* Some IGPs need a minimum stride of 64 bytes, hmm...
723 * This doesn't seem to apply to tiled textures, according to r300c. */
724 if (!tex->microtile && !tex->mip_macrotile[level] &&
725 (screen->caps.family == CHIP_FAMILY_RS600 ||
726 screen->caps.family == CHIP_FAMILY_RS690 ||
727 screen->caps.family == CHIP_FAMILY_RS740)) {
728 return stride < 64 ? 64 : stride;
729 }
730
731 /* The alignment to 32 bytes is sort of implied by the layout... */
732 return stride;
733 } else {
734 return align(util_format_get_stride(tex->b.b.format, width), 32);
735 }
736 }
737
738 static unsigned r300_texture_get_nblocksy(struct r300_texture* tex,
739 unsigned level)
740 {
741 unsigned height, tile_height;
742
743 height = u_minify(tex->b.b.height0, level);
744
745 if (util_format_is_plain(tex->b.b.format)) {
746 tile_height = r300_texture_get_tile_size(tex, TILE_HEIGHT,
747 tex->mip_macrotile[level]);
748 height = align(height, tile_height);
749
750 /* This is needed for the kernel checker, unfortunately. */
751 height = util_next_power_of_two(height);
752 }
753
754 return util_format_get_nblocksy(tex->b.b.format, height);
755 }
756
757 static void r300_texture_3d_fix_mipmapping(struct r300_screen *screen,
758 struct r300_texture *tex)
759 {
760 /* The kernels <= 2.6.34-rc4 compute the size of mipmapped 3D textures
761 * incorrectly. This is a workaround to prevent CS from being rejected. */
762
763 unsigned i, size;
764
765 if (!screen->rws->get_value(screen->rws, R300_VID_DRM_2_3_0) &&
766 tex->b.b.target == PIPE_TEXTURE_3D &&
767 tex->b.b.last_level > 0) {
768 size = 0;
769
770 for (i = 0; i <= tex->b.b.last_level; i++) {
771 size += r300_texture_get_stride(screen, tex, i) *
772 r300_texture_get_nblocksy(tex, i);
773 }
774
775 size *= tex->b.b.depth0;
776 tex->size = size;
777 }
778 }
779
780 static void r300_setup_miptree(struct r300_screen* screen,
781 struct r300_texture* tex)
782 {
783 struct pipe_resource* base = &tex->b.b;
784 unsigned stride, size, layer_size, nblocksy, i;
785 boolean rv350_mode = screen->caps.is_rv350;
786
787 SCREEN_DBG(screen, DBG_TEXALLOC, "r300: Making miptree for texture, format %s\n",
788 util_format_short_name(base->format));
789
790 for (i = 0; i <= base->last_level; i++) {
791 /* Let's see if this miplevel can be macrotiled. */
792 tex->mip_macrotile[i] =
793 (tex->macrotile == R300_BUFFER_TILED &&
794 r300_texture_macro_switch(tex, i, rv350_mode, TILE_WIDTH) &&
795 r300_texture_macro_switch(tex, i, rv350_mode, TILE_HEIGHT)) ?
796 R300_BUFFER_TILED : R300_BUFFER_LINEAR;
797
798 stride = r300_texture_get_stride(screen, tex, i);
799 nblocksy = r300_texture_get_nblocksy(tex, i);
800 layer_size = stride * nblocksy;
801
802 if (base->target == PIPE_TEXTURE_CUBE)
803 size = layer_size * 6;
804 else
805 size = layer_size * u_minify(base->depth0, i);
806
807 tex->offset[i] = tex->size;
808 tex->size = tex->offset[i] + size;
809 tex->layer_size[i] = layer_size;
810 tex->pitch[i] = stride / util_format_get_blocksize(base->format);
811 tex->hwpitch[i] =
812 tex->pitch[i] * util_format_get_blockwidth(base->format);
813
814 SCREEN_DBG(screen, DBG_TEXALLOC, "r300: Texture miptree: Level %d "
815 "(%dx%dx%d px, pitch %d bytes) %d bytes total, macrotiled %s\n",
816 i, u_minify(base->width0, i), u_minify(base->height0, i),
817 u_minify(base->depth0, i), stride, tex->size,
818 tex->mip_macrotile[i] ? "TRUE" : "FALSE");
819 }
820 }
821
822 static void r300_setup_flags(struct r300_texture* tex)
823 {
824 tex->uses_pitch = !util_is_power_of_two(tex->b.b.width0) ||
825 !util_is_power_of_two(tex->b.b.height0) ||
826 tex->stride_override;
827 }
828
829 static void r300_setup_tiling(struct pipe_screen *screen,
830 struct r300_texture *tex)
831 {
832 struct r300_winsys_screen *rws = (struct r300_winsys_screen *)screen->winsys;
833 enum pipe_format format = tex->b.b.format;
834 boolean rv350_mode = r300_screen(screen)->caps.is_rv350;
835 boolean is_zb = util_format_is_depth_or_stencil(format);
836 boolean dbg_no_tiling = SCREEN_DBG_ON(r300_screen(screen), DBG_NO_TILING);
837
838 if (!util_format_is_plain(format)) {
839 return;
840 }
841
842 /* If height == 1, disable microtiling except for zbuffer. */
843 if (!is_zb && (tex->b.b.height0 == 1 || dbg_no_tiling)) {
844 return;
845 }
846
847 /* Set microtiling. */
848 switch (util_format_get_blocksize(format)) {
849 case 1:
850 case 4:
851 tex->microtile = R300_BUFFER_TILED;
852 break;
853
854 case 2:
855 case 8:
856 if (rws->get_value(rws, R300_VID_SQUARE_TILING_SUPPORT)) {
857 tex->microtile = R300_BUFFER_SQUARETILED;
858 }
859 break;
860 }
861
862 if (dbg_no_tiling) {
863 return;
864 }
865
866 /* Set macrotiling. */
867 if (r300_texture_macro_switch(tex, 0, rv350_mode, TILE_WIDTH) &&
868 r300_texture_macro_switch(tex, 0, rv350_mode, TILE_HEIGHT)) {
869 tex->macrotile = R300_BUFFER_TILED;
870 }
871 }
872
873 static unsigned r300_texture_is_referenced(struct pipe_context *context,
874 struct pipe_resource *texture,
875 unsigned face, unsigned level)
876 {
877 struct r300_context *r300 = r300_context(context);
878 struct r300_texture *rtex = (struct r300_texture *)texture;
879
880 if (r300->rws->is_buffer_referenced(r300->rws, rtex->buffer, R300_REF_CS))
881 return PIPE_REFERENCED_FOR_READ | PIPE_REFERENCED_FOR_WRITE;
882
883 return PIPE_UNREFERENCED;
884 }
885
886 static void r300_texture_destroy(struct pipe_screen *screen,
887 struct pipe_resource* texture)
888 {
889 struct r300_texture* tex = (struct r300_texture*)texture;
890 struct r300_winsys_screen *rws = (struct r300_winsys_screen *)texture->screen->winsys;
891
892 rws->buffer_reference(rws, &tex->buffer, NULL);
893 FREE(tex);
894 }
895
896 static boolean r300_texture_get_handle(struct pipe_screen* screen,
897 struct pipe_resource *texture,
898 struct winsys_handle *whandle)
899 {
900 struct r300_winsys_screen *rws = (struct r300_winsys_screen *)screen->winsys;
901 struct r300_texture* tex = (struct r300_texture*)texture;
902 unsigned stride;
903
904 if (!tex) {
905 return FALSE;
906 }
907
908 stride = r300_texture_get_stride(r300_screen(screen), tex, 0);
909
910 rws->buffer_get_handle(rws, tex->buffer, stride, whandle);
911
912 return TRUE;
913 }
914
915 struct u_resource_vtbl r300_texture_vtbl =
916 {
917 r300_texture_get_handle, /* get_handle */
918 r300_texture_destroy, /* resource_destroy */
919 r300_texture_is_referenced, /* is_resource_referenced */
920 r300_texture_get_transfer, /* get_transfer */
921 r300_texture_transfer_destroy, /* transfer_destroy */
922 r300_texture_transfer_map, /* transfer_map */
923 u_default_transfer_flush_region, /* transfer_flush_region */
924 r300_texture_transfer_unmap, /* transfer_unmap */
925 u_default_transfer_inline_write /* transfer_inline_write */
926 };
927
928 /* Create a new texture. */
929 struct pipe_resource* r300_texture_create(struct pipe_screen* screen,
930 const struct pipe_resource* base)
931 {
932 struct r300_texture* tex = CALLOC_STRUCT(r300_texture);
933 struct r300_screen* rscreen = r300_screen(screen);
934 struct r300_winsys_screen *rws = (struct r300_winsys_screen *)screen->winsys;
935
936 if (!tex) {
937 return NULL;
938 }
939
940 /* Refuse to create a texture with size 0. */
941 if (!base->width0 ||
942 (!base->height0 && (base->target == PIPE_TEXTURE_2D ||
943 base->target == PIPE_TEXTURE_CUBE)) ||
944 (!base->depth0 && base->target == PIPE_TEXTURE_3D)) {
945 fprintf(stderr, "r300: texture_create: "
946 "Got invalid texture dimensions: %ix%ix%i\n",
947 base->width0, base->height0, base->depth0);
948 FREE(tex);
949 return NULL;
950 }
951
952 tex->b.b = *base;
953 tex->b.vtbl = &r300_texture_vtbl;
954 pipe_reference_init(&tex->b.b.reference, 1);
955 tex->b.b.screen = screen;
956
957 r300_setup_flags(tex);
958 if (!(base->flags & R300_RESOURCE_FLAG_TRANSFER) &&
959 !(base->bind & PIPE_BIND_SCANOUT)) {
960 r300_setup_tiling(screen, tex);
961 }
962 r300_setup_miptree(rscreen, tex);
963 r300_texture_3d_fix_mipmapping(rscreen, tex);
964 r300_texture_setup_immutable_state(rscreen, tex);
965 r300_texture_setup_fb_state(rscreen, tex);
966
967 SCREEN_DBG(rscreen, DBG_TEX,
968 "r300: texture_create: Macro: %s, Micro: %s, Pitch: %i, "
969 "Dim: %ix%ix%i, LastLevel: %i, Format: %s\n",
970 tex->macrotile ? "YES" : " NO",
971 tex->microtile ? "YES" : " NO",
972 tex->hwpitch[0],
973 base->width0, base->height0, base->depth0, base->last_level,
974 util_format_short_name(base->format));
975
976 tex->domain = base->flags & R300_RESOURCE_FLAG_TRANSFER ? R300_DOMAIN_GTT :
977 R300_DOMAIN_VRAM;
978
979 tex->buffer = rws->buffer_create(rws, 2048, base->bind, tex->domain,
980 tex->size);
981
982 rws->buffer_set_tiling(rws, tex->buffer,
983 tex->pitch[0] * util_format_get_blocksize(tex->b.b.format),
984 tex->microtile,
985 tex->macrotile);
986
987 if (!tex->buffer) {
988 FREE(tex);
989 return NULL;
990 }
991
992 return (struct pipe_resource*)tex;
993 }
994
995 /* Not required to implement u_resource_vtbl, consider moving to another file:
996 */
997 struct pipe_surface* r300_get_tex_surface(struct pipe_screen* screen,
998 struct pipe_resource* texture,
999 unsigned face,
1000 unsigned level,
1001 unsigned zslice,
1002 unsigned flags)
1003 {
1004 struct r300_texture* tex = r300_texture(texture);
1005 struct pipe_surface* surface = CALLOC_STRUCT(pipe_surface);
1006 unsigned offset;
1007
1008 offset = r300_texture_get_offset(tex, level, zslice, face);
1009
1010 if (surface) {
1011 pipe_reference_init(&surface->reference, 1);
1012 pipe_resource_reference(&surface->texture, texture);
1013 surface->format = texture->format;
1014 surface->width = u_minify(texture->width0, level);
1015 surface->height = u_minify(texture->height0, level);
1016 surface->offset = offset;
1017 surface->usage = flags;
1018 surface->zslice = zslice;
1019 surface->texture = texture;
1020 surface->face = face;
1021 surface->level = level;
1022 }
1023
1024 return surface;
1025 }
1026
1027 /* Not required to implement u_resource_vtbl, consider moving to another file:
1028 */
1029 void r300_tex_surface_destroy(struct pipe_surface* s)
1030 {
1031 pipe_resource_reference(&s->texture, NULL);
1032 FREE(s);
1033 }
1034
1035 struct pipe_resource*
1036 r300_texture_from_handle(struct pipe_screen* screen,
1037 const struct pipe_resource* base,
1038 struct winsys_handle *whandle)
1039 {
1040 struct r300_winsys_screen *rws = (struct r300_winsys_screen*)screen->winsys;
1041 struct r300_screen* rscreen = r300_screen(screen);
1042 struct r300_winsys_buffer *buffer;
1043 struct r300_texture* tex;
1044 unsigned stride;
1045 boolean override_zb_flags;
1046
1047 /* Support only 2D textures without mipmaps */
1048 if (base->target != PIPE_TEXTURE_2D ||
1049 base->depth0 != 1 ||
1050 base->last_level != 0) {
1051 return NULL;
1052 }
1053
1054 buffer = rws->buffer_from_handle(rws, screen, whandle, &stride);
1055 if (!buffer) {
1056 return NULL;
1057 }
1058
1059 tex = CALLOC_STRUCT(r300_texture);
1060 if (!tex) {
1061 return NULL;
1062 }
1063
1064 tex->b.b = *base;
1065 tex->b.vtbl = &r300_texture_vtbl;
1066 pipe_reference_init(&tex->b.b.reference, 1);
1067 tex->b.b.screen = screen;
1068 tex->domain = R300_DOMAIN_VRAM;
1069
1070 tex->stride_override = stride;
1071
1072 /* one ref already taken */
1073 tex->buffer = buffer;
1074
1075 rws->buffer_get_tiling(rws, buffer, &tex->microtile, &tex->macrotile);
1076 r300_setup_flags(tex);
1077 SCREEN_DBG(rscreen, DBG_TEX,
1078 "r300: texture_from_handle: Macro: %s, Micro: %s, "
1079 "Pitch: % 4i, Dim: %ix%i, Format: %s\n",
1080 tex->macrotile ? "YES" : " NO",
1081 tex->microtile ? "YES" : " NO",
1082 stride / util_format_get_blocksize(base->format),
1083 base->width0, base->height0,
1084 util_format_short_name(base->format));
1085
1086 /* Enforce microtiled zbuffer. */
1087 override_zb_flags = util_format_is_depth_or_stencil(base->format) &&
1088 tex->microtile == R300_BUFFER_LINEAR;
1089
1090 if (override_zb_flags) {
1091 switch (util_format_get_blocksize(base->format)) {
1092 case 4:
1093 tex->microtile = R300_BUFFER_TILED;
1094 break;
1095
1096 case 2:
1097 if (rws->get_value(rws, R300_VID_SQUARE_TILING_SUPPORT)) {
1098 tex->microtile = R300_BUFFER_SQUARETILED;
1099 break;
1100 }
1101 /* Pass through. */
1102
1103 default:
1104 override_zb_flags = FALSE;
1105 }
1106 }
1107
1108 r300_setup_miptree(rscreen, tex);
1109 r300_texture_setup_immutable_state(rscreen, tex);
1110 r300_texture_setup_fb_state(rscreen, tex);
1111
1112 if (override_zb_flags) {
1113 rws->buffer_set_tiling(rws, tex->buffer,
1114 tex->pitch[0] * util_format_get_blocksize(tex->b.b.format),
1115 tex->microtile,
1116 tex->macrotile);
1117 }
1118 return (struct pipe_resource*)tex;
1119 }