util: Move gallium's PIPE_FORMAT utils to /util/format/
[mesa.git] / src / gallium / drivers / r300 / r300_texture.c
1 /*
2 * Copyright 2008 Corbin Simpson <MostAwesomeDude@gmail.com>
3 * Copyright 2010 Marek Olšák <maraeo@gmail.com>
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE. */
23
24 /* Always include headers in the reverse order!! ~ M. */
25 #include "r300_texture.h"
26
27 #include "r300_context.h"
28 #include "r300_reg.h"
29 #include "r300_texture_desc.h"
30 #include "r300_transfer.h"
31 #include "r300_screen.h"
32
33 #include "util/format/u_format.h"
34 #include "util/format/u_format_s3tc.h"
35 #include "util/u_math.h"
36 #include "util/u_memory.h"
37
38 #include "pipe/p_screen.h"
39 #include "state_tracker/winsys_handle.h"
40
41 /* These formats are supported by swapping their bytes.
42 * The swizzles must be set exactly like their non-swapped counterparts,
43 * because byte-swapping is what reverses the component order, not swizzling.
44 *
45 * This function returns the format that must be used to program CB and TX
46 * swizzles.
47 */
48 static enum pipe_format r300_unbyteswap_array_format(enum pipe_format format)
49 {
50 /* FIXME: Disabled on little endian because of a reported regression:
51 * https://bugs.freedesktop.org/show_bug.cgi?id=98869 */
52 if (PIPE_ENDIAN_NATIVE != PIPE_ENDIAN_BIG)
53 return format;
54
55 /* Only BGRA 8888 array formats are supported for simplicity of
56 * the implementation. */
57 switch (format) {
58 case PIPE_FORMAT_A8R8G8B8_UNORM:
59 return PIPE_FORMAT_B8G8R8A8_UNORM;
60 case PIPE_FORMAT_A8R8G8B8_SRGB:
61 return PIPE_FORMAT_B8G8R8A8_SRGB;
62 case PIPE_FORMAT_X8R8G8B8_UNORM:
63 return PIPE_FORMAT_B8G8R8X8_UNORM;
64 case PIPE_FORMAT_X8R8G8B8_SRGB:
65 return PIPE_FORMAT_B8G8R8X8_SRGB;
66 default:
67 return format;
68 }
69 }
70
71 static unsigned r300_get_endian_swap(enum pipe_format format)
72 {
73 const struct util_format_description *desc;
74 unsigned swap_size;
75
76 if (r300_unbyteswap_array_format(format) != format)
77 return R300_SURF_DWORD_SWAP;
78
79 if (PIPE_ENDIAN_NATIVE != PIPE_ENDIAN_BIG)
80 return R300_SURF_NO_SWAP;
81
82 desc = util_format_description(format);
83 if (!desc)
84 return R300_SURF_NO_SWAP;
85
86 /* Compressed formats should be in the little endian format. */
87 if (desc->block.width != 1 || desc->block.height != 1)
88 return R300_SURF_NO_SWAP;
89
90 swap_size = desc->is_array ? desc->channel[0].size : desc->block.bits;
91
92 switch (swap_size) {
93 default: /* shouldn't happen? */
94 case 8:
95 return R300_SURF_NO_SWAP;
96 case 16:
97 return R300_SURF_WORD_SWAP;
98 case 32:
99 return R300_SURF_DWORD_SWAP;
100 }
101 }
102
103 unsigned r300_get_swizzle_combined(const unsigned char *swizzle_format,
104 const unsigned char *swizzle_view,
105 boolean dxtc_swizzle)
106 {
107 unsigned i;
108 unsigned char swizzle[4];
109 unsigned result = 0;
110 const uint32_t swizzle_shift[4] = {
111 R300_TX_FORMAT_R_SHIFT,
112 R300_TX_FORMAT_G_SHIFT,
113 R300_TX_FORMAT_B_SHIFT,
114 R300_TX_FORMAT_A_SHIFT
115 };
116 uint32_t swizzle_bit[4] = {
117 dxtc_swizzle ? R300_TX_FORMAT_Z : R300_TX_FORMAT_X,
118 R300_TX_FORMAT_Y,
119 dxtc_swizzle ? R300_TX_FORMAT_X : R300_TX_FORMAT_Z,
120 R300_TX_FORMAT_W
121 };
122
123 if (swizzle_view) {
124 /* Combine two sets of swizzles. */
125 util_format_compose_swizzles(swizzle_format, swizzle_view, swizzle);
126 } else {
127 memcpy(swizzle, swizzle_format, 4);
128 }
129
130 /* Get swizzle. */
131 for (i = 0; i < 4; i++) {
132 switch (swizzle[i]) {
133 case PIPE_SWIZZLE_Y:
134 result |= swizzle_bit[1] << swizzle_shift[i];
135 break;
136 case PIPE_SWIZZLE_Z:
137 result |= swizzle_bit[2] << swizzle_shift[i];
138 break;
139 case PIPE_SWIZZLE_W:
140 result |= swizzle_bit[3] << swizzle_shift[i];
141 break;
142 case PIPE_SWIZZLE_0:
143 result |= R300_TX_FORMAT_ZERO << swizzle_shift[i];
144 break;
145 case PIPE_SWIZZLE_1:
146 result |= R300_TX_FORMAT_ONE << swizzle_shift[i];
147 break;
148 default: /* PIPE_SWIZZLE_X */
149 result |= swizzle_bit[0] << swizzle_shift[i];
150 }
151 }
152 return result;
153 }
154
155 /* Translate a pipe_format into a useful texture format for sampling.
156 *
157 * Some special formats are translated directly using R300_EASY_TX_FORMAT,
158 * but the majority of them is translated in a generic way, automatically
159 * supporting all the formats hw can support.
160 *
161 * R300_EASY_TX_FORMAT swizzles the texture.
162 * Note the signature of R300_EASY_TX_FORMAT:
163 * R300_EASY_TX_FORMAT(B, G, R, A, FORMAT);
164 *
165 * The FORMAT specifies how the texture sampler will treat the texture, and
166 * makes available X, Y, Z, W, ZERO, and ONE for swizzling. */
167 uint32_t r300_translate_texformat(enum pipe_format format,
168 const unsigned char *swizzle_view,
169 boolean is_r500,
170 boolean dxtc_swizzle)
171 {
172 uint32_t result = 0;
173 const struct util_format_description *desc;
174 unsigned i;
175 boolean uniform = TRUE;
176 const uint32_t sign_bit[4] = {
177 R300_TX_FORMAT_SIGNED_W,
178 R300_TX_FORMAT_SIGNED_Z,
179 R300_TX_FORMAT_SIGNED_Y,
180 R300_TX_FORMAT_SIGNED_X,
181 };
182
183 format = r300_unbyteswap_array_format(format);
184 desc = util_format_description(format);
185
186 /* Colorspace (return non-RGB formats directly). */
187 switch (desc->colorspace) {
188 /* Depth stencil formats.
189 * Swizzles are added in r300_merge_textures_and_samplers. */
190 case UTIL_FORMAT_COLORSPACE_ZS:
191 switch (format) {
192 case PIPE_FORMAT_Z16_UNORM:
193 return R300_TX_FORMAT_X16;
194 case PIPE_FORMAT_X8Z24_UNORM:
195 case PIPE_FORMAT_S8_UINT_Z24_UNORM:
196 if (is_r500)
197 return R500_TX_FORMAT_Y8X24;
198 else
199 return R300_TX_FORMAT_Y16X16;
200 default:
201 return ~0; /* Unsupported. */
202 }
203
204 /* YUV formats. */
205 case UTIL_FORMAT_COLORSPACE_YUV:
206 result |= R300_TX_FORMAT_YUV_TO_RGB;
207
208 switch (format) {
209 case PIPE_FORMAT_UYVY:
210 return R300_EASY_TX_FORMAT(X, Y, Z, ONE, YVYU422) | result;
211 case PIPE_FORMAT_YUYV:
212 return R300_EASY_TX_FORMAT(X, Y, Z, ONE, VYUY422) | result;
213 default:
214 return ~0; /* Unsupported/unknown. */
215 }
216
217 /* Add gamma correction. */
218 case UTIL_FORMAT_COLORSPACE_SRGB:
219 result |= R300_TX_FORMAT_GAMMA;
220 break;
221
222 default:
223 switch (format) {
224 /* Same as YUV but without the YUR->RGB conversion. */
225 case PIPE_FORMAT_R8G8_B8G8_UNORM:
226 return R300_EASY_TX_FORMAT(X, Y, Z, ONE, YVYU422) | result;
227 case PIPE_FORMAT_G8R8_G8B8_UNORM:
228 return R300_EASY_TX_FORMAT(X, Y, Z, ONE, VYUY422) | result;
229 default:;
230 }
231 }
232
233 /* Add swizzling. */
234 /* The RGTC1_SNORM and LATC1_SNORM swizzle is done in the shader. */
235 if (util_format_is_compressed(format) &&
236 dxtc_swizzle &&
237 format != PIPE_FORMAT_RGTC2_UNORM &&
238 format != PIPE_FORMAT_RGTC2_SNORM &&
239 format != PIPE_FORMAT_LATC2_UNORM &&
240 format != PIPE_FORMAT_LATC2_SNORM &&
241 format != PIPE_FORMAT_RGTC1_UNORM &&
242 format != PIPE_FORMAT_RGTC1_SNORM &&
243 format != PIPE_FORMAT_LATC1_UNORM &&
244 format != PIPE_FORMAT_LATC1_SNORM) {
245 result |= r300_get_swizzle_combined(desc->swizzle, swizzle_view,
246 TRUE);
247 } else {
248 result |= r300_get_swizzle_combined(desc->swizzle, swizzle_view,
249 FALSE);
250 }
251
252 /* S3TC formats. */
253 if (desc->layout == UTIL_FORMAT_LAYOUT_S3TC) {
254 switch (format) {
255 case PIPE_FORMAT_DXT1_RGB:
256 case PIPE_FORMAT_DXT1_RGBA:
257 case PIPE_FORMAT_DXT1_SRGB:
258 case PIPE_FORMAT_DXT1_SRGBA:
259 return R300_TX_FORMAT_DXT1 | result;
260 case PIPE_FORMAT_DXT3_RGBA:
261 case PIPE_FORMAT_DXT3_SRGBA:
262 return R300_TX_FORMAT_DXT3 | result;
263 case PIPE_FORMAT_DXT5_RGBA:
264 case PIPE_FORMAT_DXT5_SRGBA:
265 return R300_TX_FORMAT_DXT5 | result;
266 default:
267 return ~0; /* Unsupported/unknown. */
268 }
269 }
270
271 /* RGTC formats. */
272 if (desc->layout == UTIL_FORMAT_LAYOUT_RGTC) {
273 switch (format) {
274 case PIPE_FORMAT_RGTC1_SNORM:
275 case PIPE_FORMAT_LATC1_SNORM:
276 result |= sign_bit[0];
277 case PIPE_FORMAT_LATC1_UNORM:
278 case PIPE_FORMAT_RGTC1_UNORM:
279 return R500_TX_FORMAT_ATI1N | result;
280
281 case PIPE_FORMAT_RGTC2_SNORM:
282 case PIPE_FORMAT_LATC2_SNORM:
283 result |= sign_bit[1] | sign_bit[0];
284 case PIPE_FORMAT_RGTC2_UNORM:
285 case PIPE_FORMAT_LATC2_UNORM:
286 return R400_TX_FORMAT_ATI2N | result;
287
288 default:
289 return ~0; /* Unsupported/unknown. */
290 }
291 }
292
293 /* This is truly a special format.
294 * It stores R8G8 and B is computed using sqrt(1 - R^2 - G^2)
295 * in the sampler unit. Also known as D3DFMT_CxV8U8. */
296 if (format == PIPE_FORMAT_R8G8Bx_SNORM) {
297 return R300_TX_FORMAT_CxV8U8 | result;
298 }
299
300 /* Integer and fixed-point 16.16 textures are not supported. */
301 for (i = 0; i < 4; i++) {
302 if (desc->channel[i].type == UTIL_FORMAT_TYPE_FIXED ||
303 ((desc->channel[i].type == UTIL_FORMAT_TYPE_SIGNED ||
304 desc->channel[i].type == UTIL_FORMAT_TYPE_UNSIGNED) &&
305 (!desc->channel[i].normalized ||
306 desc->channel[i].pure_integer))) {
307 return ~0; /* Unsupported/unknown. */
308 }
309 }
310
311 /* Add sign. */
312 for (i = 0; i < desc->nr_channels; i++) {
313 if (desc->channel[i].type == UTIL_FORMAT_TYPE_SIGNED) {
314 result |= sign_bit[i];
315 }
316 }
317
318 /* See whether the components are of the same size. */
319 for (i = 1; i < desc->nr_channels; i++) {
320 uniform = uniform && desc->channel[0].size == desc->channel[i].size;
321 }
322
323 /* Non-uniform formats. */
324 if (!uniform) {
325 switch (desc->nr_channels) {
326 case 3:
327 if (desc->channel[0].size == 5 &&
328 desc->channel[1].size == 6 &&
329 desc->channel[2].size == 5) {
330 return R300_TX_FORMAT_Z5Y6X5 | result;
331 }
332 if (desc->channel[0].size == 5 &&
333 desc->channel[1].size == 5 &&
334 desc->channel[2].size == 6) {
335 return R300_TX_FORMAT_Z6Y5X5 | result;
336 }
337 if (desc->channel[0].size == 2 &&
338 desc->channel[1].size == 3 &&
339 desc->channel[2].size == 3) {
340 return R300_TX_FORMAT_Z3Y3X2 | result;
341 }
342 return ~0; /* Unsupported/unknown. */
343
344 case 4:
345 if (desc->channel[0].size == 5 &&
346 desc->channel[1].size == 5 &&
347 desc->channel[2].size == 5 &&
348 desc->channel[3].size == 1) {
349 return R300_TX_FORMAT_W1Z5Y5X5 | result;
350 }
351 if (desc->channel[0].size == 10 &&
352 desc->channel[1].size == 10 &&
353 desc->channel[2].size == 10 &&
354 desc->channel[3].size == 2) {
355 return R300_TX_FORMAT_W2Z10Y10X10 | result;
356 }
357 }
358 return ~0; /* Unsupported/unknown. */
359 }
360
361 /* Find the first non-VOID channel. */
362 for (i = 0; i < 4; i++) {
363 if (desc->channel[i].type != UTIL_FORMAT_TYPE_VOID) {
364 break;
365 }
366 }
367
368 if (i == 4)
369 return ~0; /* Unsupported/unknown. */
370
371 /* And finally, uniform formats. */
372 switch (desc->channel[i].type) {
373 case UTIL_FORMAT_TYPE_UNSIGNED:
374 case UTIL_FORMAT_TYPE_SIGNED:
375 if (!desc->channel[i].normalized &&
376 desc->colorspace != UTIL_FORMAT_COLORSPACE_SRGB) {
377 return ~0;
378 }
379
380 switch (desc->channel[i].size) {
381 case 4:
382 switch (desc->nr_channels) {
383 case 2:
384 return R300_TX_FORMAT_Y4X4 | result;
385 case 4:
386 return R300_TX_FORMAT_W4Z4Y4X4 | result;
387 }
388 return ~0;
389
390 case 8:
391 switch (desc->nr_channels) {
392 case 1:
393 return R300_TX_FORMAT_X8 | result;
394 case 2:
395 return R300_TX_FORMAT_Y8X8 | result;
396 case 4:
397 return R300_TX_FORMAT_W8Z8Y8X8 | result;
398 }
399 return ~0;
400
401 case 16:
402 switch (desc->nr_channels) {
403 case 1:
404 return R300_TX_FORMAT_X16 | result;
405 case 2:
406 return R300_TX_FORMAT_Y16X16 | result;
407 case 4:
408 return R300_TX_FORMAT_W16Z16Y16X16 | result;
409 }
410 }
411 return ~0;
412
413 case UTIL_FORMAT_TYPE_FLOAT:
414 switch (desc->channel[i].size) {
415 case 16:
416 switch (desc->nr_channels) {
417 case 1:
418 return R300_TX_FORMAT_16F | result;
419 case 2:
420 return R300_TX_FORMAT_16F_16F | result;
421 case 4:
422 return R300_TX_FORMAT_16F_16F_16F_16F | result;
423 }
424 return ~0;
425
426 case 32:
427 switch (desc->nr_channels) {
428 case 1:
429 return R300_TX_FORMAT_32F | result;
430 case 2:
431 return R300_TX_FORMAT_32F_32F | result;
432 case 4:
433 return R300_TX_FORMAT_32F_32F_32F_32F | result;
434 }
435 }
436 }
437
438 return ~0; /* Unsupported/unknown. */
439 }
440
441 uint32_t r500_tx_format_msb_bit(enum pipe_format format)
442 {
443 switch (format) {
444 case PIPE_FORMAT_RGTC1_UNORM:
445 case PIPE_FORMAT_RGTC1_SNORM:
446 case PIPE_FORMAT_LATC1_UNORM:
447 case PIPE_FORMAT_LATC1_SNORM:
448 case PIPE_FORMAT_X8Z24_UNORM:
449 case PIPE_FORMAT_S8_UINT_Z24_UNORM:
450 return R500_TXFORMAT_MSB;
451 default:
452 return 0;
453 }
454 }
455
456 /* Buffer formats. */
457
458 /* Colorbuffer formats. This is the unswizzled format of the RB3D block's
459 * output. For the swizzling of the targets, check the shader's format. */
460 static uint32_t r300_translate_colorformat(enum pipe_format format)
461 {
462 format = r300_unbyteswap_array_format(format);
463
464 switch (format) {
465 /* 8-bit buffers. */
466 case PIPE_FORMAT_A8_UNORM:
467 case PIPE_FORMAT_A8_SNORM:
468 case PIPE_FORMAT_I8_UNORM:
469 case PIPE_FORMAT_I8_SNORM:
470 case PIPE_FORMAT_L8_UNORM:
471 case PIPE_FORMAT_L8_SNORM:
472 case PIPE_FORMAT_R8_UNORM:
473 case PIPE_FORMAT_R8_SNORM:
474 return R300_COLOR_FORMAT_I8;
475
476 /* 16-bit buffers. */
477 case PIPE_FORMAT_L8A8_UNORM:
478 case PIPE_FORMAT_L8A8_SNORM:
479 case PIPE_FORMAT_R8G8_UNORM:
480 case PIPE_FORMAT_R8G8_SNORM:
481 case PIPE_FORMAT_R8A8_UNORM:
482 case PIPE_FORMAT_R8A8_SNORM:
483 /* These formats work fine with UV88 if US_OUT_FMT is set correctly. */
484 case PIPE_FORMAT_A16_UNORM:
485 case PIPE_FORMAT_A16_SNORM:
486 case PIPE_FORMAT_A16_FLOAT:
487 case PIPE_FORMAT_L16_UNORM:
488 case PIPE_FORMAT_L16_SNORM:
489 case PIPE_FORMAT_L16_FLOAT:
490 case PIPE_FORMAT_I16_UNORM:
491 case PIPE_FORMAT_I16_SNORM:
492 case PIPE_FORMAT_I16_FLOAT:
493 case PIPE_FORMAT_R16_UNORM:
494 case PIPE_FORMAT_R16_SNORM:
495 case PIPE_FORMAT_R16_FLOAT:
496 return R300_COLOR_FORMAT_UV88;
497
498 case PIPE_FORMAT_B5G6R5_UNORM:
499 return R300_COLOR_FORMAT_RGB565;
500
501 case PIPE_FORMAT_B5G5R5A1_UNORM:
502 case PIPE_FORMAT_B5G5R5X1_UNORM:
503 return R300_COLOR_FORMAT_ARGB1555;
504
505 case PIPE_FORMAT_B4G4R4A4_UNORM:
506 case PIPE_FORMAT_B4G4R4X4_UNORM:
507 return R300_COLOR_FORMAT_ARGB4444;
508
509 /* 32-bit buffers. */
510 case PIPE_FORMAT_B8G8R8A8_UNORM:
511 /*case PIPE_FORMAT_B8G8R8A8_SNORM:*/
512 case PIPE_FORMAT_B8G8R8X8_UNORM:
513 /*case PIPE_FORMAT_B8G8R8X8_SNORM:*/
514 case PIPE_FORMAT_R8G8B8A8_UNORM:
515 case PIPE_FORMAT_R8G8B8A8_SNORM:
516 case PIPE_FORMAT_R8G8B8X8_UNORM:
517 case PIPE_FORMAT_R8G8B8X8_SNORM:
518 /* These formats work fine with ARGB8888 if US_OUT_FMT is set
519 * correctly. */
520 case PIPE_FORMAT_R16G16_UNORM:
521 case PIPE_FORMAT_R16G16_SNORM:
522 case PIPE_FORMAT_R16G16_FLOAT:
523 case PIPE_FORMAT_L16A16_UNORM:
524 case PIPE_FORMAT_L16A16_SNORM:
525 case PIPE_FORMAT_L16A16_FLOAT:
526 case PIPE_FORMAT_R16A16_UNORM:
527 case PIPE_FORMAT_R16A16_SNORM:
528 case PIPE_FORMAT_R16A16_FLOAT:
529 case PIPE_FORMAT_A32_FLOAT:
530 case PIPE_FORMAT_L32_FLOAT:
531 case PIPE_FORMAT_I32_FLOAT:
532 case PIPE_FORMAT_R32_FLOAT:
533 return R300_COLOR_FORMAT_ARGB8888;
534
535 case PIPE_FORMAT_R10G10B10A2_UNORM:
536 case PIPE_FORMAT_R10G10B10X2_SNORM:
537 case PIPE_FORMAT_B10G10R10A2_UNORM:
538 case PIPE_FORMAT_B10G10R10X2_UNORM:
539 return R500_COLOR_FORMAT_ARGB2101010; /* R5xx-only? */
540
541 /* 64-bit buffers. */
542 case PIPE_FORMAT_R16G16B16A16_UNORM:
543 case PIPE_FORMAT_R16G16B16A16_SNORM:
544 case PIPE_FORMAT_R16G16B16A16_FLOAT:
545 case PIPE_FORMAT_R16G16B16X16_UNORM:
546 case PIPE_FORMAT_R16G16B16X16_SNORM:
547 case PIPE_FORMAT_R16G16B16X16_FLOAT:
548 /* These formats work fine with ARGB16161616 if US_OUT_FMT is set
549 * correctly. */
550 case PIPE_FORMAT_R32G32_FLOAT:
551 case PIPE_FORMAT_L32A32_FLOAT:
552 case PIPE_FORMAT_R32A32_FLOAT:
553 return R300_COLOR_FORMAT_ARGB16161616;
554
555 /* 128-bit buffers. */
556 case PIPE_FORMAT_R32G32B32A32_FLOAT:
557 case PIPE_FORMAT_R32G32B32X32_FLOAT:
558 return R300_COLOR_FORMAT_ARGB32323232;
559
560 /* YUV buffers. */
561 case PIPE_FORMAT_UYVY:
562 return R300_COLOR_FORMAT_YVYU;
563 case PIPE_FORMAT_YUYV:
564 return R300_COLOR_FORMAT_VYUY;
565 default:
566 return ~0; /* Unsupported. */
567 }
568 }
569
570 /* Depthbuffer and stencilbuffer. Thankfully, we only support two flavors. */
571 static uint32_t r300_translate_zsformat(enum pipe_format format)
572 {
573 switch (format) {
574 /* 16-bit depth, no stencil */
575 case PIPE_FORMAT_Z16_UNORM:
576 return R300_DEPTHFORMAT_16BIT_INT_Z;
577 /* 24-bit depth, ignored stencil */
578 case PIPE_FORMAT_X8Z24_UNORM:
579 /* 24-bit depth, 8-bit stencil */
580 case PIPE_FORMAT_S8_UINT_Z24_UNORM:
581 return R300_DEPTHFORMAT_24BIT_INT_Z_8BIT_STENCIL;
582 default:
583 return ~0; /* Unsupported. */
584 }
585 }
586
587 /* Shader output formats. This is essentially the swizzle from the shader
588 * to the RB3D block.
589 *
590 * Note that formats are stored from C3 to C0. */
591 static uint32_t r300_translate_out_fmt(enum pipe_format format)
592 {
593 uint32_t modifier = 0;
594 unsigned i;
595 const struct util_format_description *desc;
596 boolean uniform_sign;
597
598 format = r300_unbyteswap_array_format(format);
599 desc = util_format_description(format);
600
601 /* Find the first non-VOID channel. */
602 for (i = 0; i < 4; i++) {
603 if (desc->channel[i].type != UTIL_FORMAT_TYPE_VOID) {
604 break;
605 }
606 }
607
608 if (i == 4)
609 return ~0; /* Unsupported/unknown. */
610
611 /* Specifies how the shader output is written to the fog unit. */
612 switch (desc->channel[i].type) {
613 case UTIL_FORMAT_TYPE_FLOAT:
614 switch (desc->channel[i].size) {
615 case 32:
616 switch (desc->nr_channels) {
617 case 1:
618 modifier |= R300_US_OUT_FMT_C_32_FP;
619 break;
620 case 2:
621 modifier |= R300_US_OUT_FMT_C2_32_FP;
622 break;
623 case 4:
624 modifier |= R300_US_OUT_FMT_C4_32_FP;
625 break;
626 }
627 break;
628
629 case 16:
630 switch (desc->nr_channels) {
631 case 1:
632 modifier |= R300_US_OUT_FMT_C_16_FP;
633 break;
634 case 2:
635 modifier |= R300_US_OUT_FMT_C2_16_FP;
636 break;
637 case 4:
638 modifier |= R300_US_OUT_FMT_C4_16_FP;
639 break;
640 }
641 break;
642 }
643 break;
644
645 default:
646 switch (desc->channel[i].size) {
647 case 16:
648 switch (desc->nr_channels) {
649 case 1:
650 modifier |= R300_US_OUT_FMT_C_16;
651 break;
652 case 2:
653 modifier |= R300_US_OUT_FMT_C2_16;
654 break;
655 case 4:
656 modifier |= R300_US_OUT_FMT_C4_16;
657 break;
658 }
659 break;
660
661 case 10:
662 modifier |= R300_US_OUT_FMT_C4_10;
663 break;
664
665 default:
666 /* C4_8 seems to be used for the formats whose pixel size
667 * is <= 32 bits. */
668 modifier |= R300_US_OUT_FMT_C4_8;
669 break;
670 }
671 }
672
673 /* Add sign. */
674 uniform_sign = TRUE;
675 for (i = 0; i < desc->nr_channels; i++)
676 if (desc->channel[i].type != UTIL_FORMAT_TYPE_SIGNED)
677 uniform_sign = FALSE;
678
679 if (uniform_sign)
680 modifier |= R300_OUT_SIGN(0xf);
681
682 /* Add swizzles and return. */
683 switch (format) {
684 /*** Special cases (non-standard channel mapping) ***/
685
686 /* X8
687 * COLORFORMAT_I8 stores the Z component (C2). */
688 case PIPE_FORMAT_A8_UNORM:
689 case PIPE_FORMAT_A8_SNORM:
690 return modifier | R300_C2_SEL_A;
691 case PIPE_FORMAT_I8_UNORM:
692 case PIPE_FORMAT_I8_SNORM:
693 case PIPE_FORMAT_L8_UNORM:
694 case PIPE_FORMAT_L8_SNORM:
695 case PIPE_FORMAT_R8_UNORM:
696 case PIPE_FORMAT_R8_SNORM:
697 return modifier | R300_C2_SEL_R;
698
699 /* X8Y8
700 * COLORFORMAT_UV88 stores ZX (C2 and C0). */
701 case PIPE_FORMAT_L8A8_SNORM:
702 case PIPE_FORMAT_L8A8_UNORM:
703 case PIPE_FORMAT_R8A8_SNORM:
704 case PIPE_FORMAT_R8A8_UNORM:
705 return modifier | R300_C0_SEL_A | R300_C2_SEL_R;
706 case PIPE_FORMAT_R8G8_SNORM:
707 case PIPE_FORMAT_R8G8_UNORM:
708 return modifier | R300_C0_SEL_G | R300_C2_SEL_R;
709
710 /* X32Y32
711 * ARGB16161616 stores XZ for RG32F */
712 case PIPE_FORMAT_R32G32_FLOAT:
713 return modifier | R300_C0_SEL_R | R300_C2_SEL_G;
714
715 /*** Generic cases (standard channel mapping) ***/
716
717 /* BGRA outputs. */
718 case PIPE_FORMAT_B5G6R5_UNORM:
719 case PIPE_FORMAT_B5G5R5A1_UNORM:
720 case PIPE_FORMAT_B5G5R5X1_UNORM:
721 case PIPE_FORMAT_B4G4R4A4_UNORM:
722 case PIPE_FORMAT_B4G4R4X4_UNORM:
723 case PIPE_FORMAT_B8G8R8A8_UNORM:
724 /*case PIPE_FORMAT_B8G8R8A8_SNORM:*/
725 case PIPE_FORMAT_B8G8R8X8_UNORM:
726 /*case PIPE_FORMAT_B8G8R8X8_SNORM:*/
727 case PIPE_FORMAT_B10G10R10A2_UNORM:
728 case PIPE_FORMAT_B10G10R10X2_UNORM:
729 return modifier |
730 R300_C0_SEL_B | R300_C1_SEL_G |
731 R300_C2_SEL_R | R300_C3_SEL_A;
732
733 /* ARGB outputs. */
734 case PIPE_FORMAT_A16_UNORM:
735 case PIPE_FORMAT_A16_SNORM:
736 case PIPE_FORMAT_A16_FLOAT:
737 case PIPE_FORMAT_A32_FLOAT:
738 return modifier |
739 R300_C0_SEL_A | R300_C1_SEL_R |
740 R300_C2_SEL_G | R300_C3_SEL_B;
741
742 /* RGBA outputs. */
743 case PIPE_FORMAT_R8G8B8X8_UNORM:
744 case PIPE_FORMAT_R8G8B8X8_SNORM:
745 case PIPE_FORMAT_R8G8B8A8_UNORM:
746 case PIPE_FORMAT_R8G8B8A8_SNORM:
747 case PIPE_FORMAT_R10G10B10A2_UNORM:
748 case PIPE_FORMAT_R10G10B10X2_SNORM:
749 case PIPE_FORMAT_R16_UNORM:
750 case PIPE_FORMAT_R16G16_UNORM:
751 case PIPE_FORMAT_R16G16B16A16_UNORM:
752 case PIPE_FORMAT_R16_SNORM:
753 case PIPE_FORMAT_R16G16_SNORM:
754 case PIPE_FORMAT_R16G16B16A16_SNORM:
755 case PIPE_FORMAT_R16_FLOAT:
756 case PIPE_FORMAT_R16G16_FLOAT:
757 case PIPE_FORMAT_R16G16B16A16_FLOAT:
758 case PIPE_FORMAT_R32_FLOAT:
759 case PIPE_FORMAT_R32G32B32A32_FLOAT:
760 case PIPE_FORMAT_R32G32B32X32_FLOAT:
761 case PIPE_FORMAT_L16_UNORM:
762 case PIPE_FORMAT_L16_SNORM:
763 case PIPE_FORMAT_L16_FLOAT:
764 case PIPE_FORMAT_L32_FLOAT:
765 case PIPE_FORMAT_I16_UNORM:
766 case PIPE_FORMAT_I16_SNORM:
767 case PIPE_FORMAT_I16_FLOAT:
768 case PIPE_FORMAT_I32_FLOAT:
769 case PIPE_FORMAT_R16G16B16X16_UNORM:
770 case PIPE_FORMAT_R16G16B16X16_SNORM:
771 case PIPE_FORMAT_R16G16B16X16_FLOAT:
772 return modifier |
773 R300_C0_SEL_R | R300_C1_SEL_G |
774 R300_C2_SEL_B | R300_C3_SEL_A;
775
776 /* LA outputs. */
777 case PIPE_FORMAT_L16A16_UNORM:
778 case PIPE_FORMAT_L16A16_SNORM:
779 case PIPE_FORMAT_L16A16_FLOAT:
780 case PIPE_FORMAT_R16A16_UNORM:
781 case PIPE_FORMAT_R16A16_SNORM:
782 case PIPE_FORMAT_R16A16_FLOAT:
783 case PIPE_FORMAT_L32A32_FLOAT:
784 case PIPE_FORMAT_R32A32_FLOAT:
785 return modifier |
786 R300_C0_SEL_R | R300_C1_SEL_A;
787
788 default:
789 return ~0; /* Unsupported. */
790 }
791 }
792
793 static uint32_t r300_translate_colormask_swizzle(enum pipe_format format)
794 {
795 format = r300_unbyteswap_array_format(format);
796
797 switch (format) {
798 case PIPE_FORMAT_A8_UNORM:
799 case PIPE_FORMAT_A8_SNORM:
800 case PIPE_FORMAT_A16_UNORM:
801 case PIPE_FORMAT_A16_SNORM:
802 case PIPE_FORMAT_A16_FLOAT:
803 case PIPE_FORMAT_A32_FLOAT:
804 return COLORMASK_AAAA;
805
806 case PIPE_FORMAT_I8_UNORM:
807 case PIPE_FORMAT_I8_SNORM:
808 case PIPE_FORMAT_L8_UNORM:
809 case PIPE_FORMAT_L8_SNORM:
810 case PIPE_FORMAT_R8_UNORM:
811 case PIPE_FORMAT_R8_SNORM:
812 case PIPE_FORMAT_R32_FLOAT:
813 case PIPE_FORMAT_L32_FLOAT:
814 case PIPE_FORMAT_I32_FLOAT:
815 return COLORMASK_RRRR;
816
817 case PIPE_FORMAT_L8A8_SNORM:
818 case PIPE_FORMAT_L8A8_UNORM:
819 case PIPE_FORMAT_R8A8_UNORM:
820 case PIPE_FORMAT_R8A8_SNORM:
821 case PIPE_FORMAT_L16A16_UNORM:
822 case PIPE_FORMAT_L16A16_SNORM:
823 case PIPE_FORMAT_L16A16_FLOAT:
824 case PIPE_FORMAT_R16A16_UNORM:
825 case PIPE_FORMAT_R16A16_SNORM:
826 case PIPE_FORMAT_R16A16_FLOAT:
827 case PIPE_FORMAT_L32A32_FLOAT:
828 case PIPE_FORMAT_R32A32_FLOAT:
829 return COLORMASK_ARRA;
830
831 case PIPE_FORMAT_R8G8_SNORM:
832 case PIPE_FORMAT_R8G8_UNORM:
833 case PIPE_FORMAT_R16G16_UNORM:
834 case PIPE_FORMAT_R16G16_SNORM:
835 case PIPE_FORMAT_R16G16_FLOAT:
836 case PIPE_FORMAT_R32G32_FLOAT:
837 return COLORMASK_GRRG;
838
839 case PIPE_FORMAT_B5G5R5X1_UNORM:
840 case PIPE_FORMAT_B4G4R4X4_UNORM:
841 case PIPE_FORMAT_B8G8R8X8_UNORM:
842 /*case PIPE_FORMAT_B8G8R8X8_SNORM:*/
843 case PIPE_FORMAT_B10G10R10X2_UNORM:
844 return COLORMASK_BGRX;
845
846 case PIPE_FORMAT_B5G6R5_UNORM:
847 case PIPE_FORMAT_B5G5R5A1_UNORM:
848 case PIPE_FORMAT_B4G4R4A4_UNORM:
849 case PIPE_FORMAT_B8G8R8A8_UNORM:
850 /*case PIPE_FORMAT_B8G8R8A8_SNORM:*/
851 case PIPE_FORMAT_B10G10R10A2_UNORM:
852 return COLORMASK_BGRA;
853
854 case PIPE_FORMAT_R8G8B8X8_UNORM:
855 /* RGBX_SNORM formats are broken for an unknown reason */
856 /*case PIPE_FORMAT_R8G8B8X8_SNORM:*/
857 /*case PIPE_FORMAT_R10G10B10X2_SNORM:*/
858 case PIPE_FORMAT_R16G16B16X16_UNORM:
859 /*case PIPE_FORMAT_R16G16B16X16_SNORM:*/
860 case PIPE_FORMAT_R16G16B16X16_FLOAT:
861 case PIPE_FORMAT_R32G32B32X32_FLOAT:
862 return COLORMASK_RGBX;
863
864 case PIPE_FORMAT_R8G8B8A8_UNORM:
865 case PIPE_FORMAT_R8G8B8A8_SNORM:
866 case PIPE_FORMAT_R10G10B10A2_UNORM:
867 case PIPE_FORMAT_R16_UNORM:
868 case PIPE_FORMAT_R16G16B16A16_UNORM:
869 case PIPE_FORMAT_R16_SNORM:
870 case PIPE_FORMAT_R16G16B16A16_SNORM:
871 case PIPE_FORMAT_R16_FLOAT:
872 case PIPE_FORMAT_R16G16B16A16_FLOAT:
873 case PIPE_FORMAT_R32G32B32A32_FLOAT:
874 case PIPE_FORMAT_L16_UNORM:
875 case PIPE_FORMAT_L16_SNORM:
876 case PIPE_FORMAT_L16_FLOAT:
877 case PIPE_FORMAT_I16_UNORM:
878 case PIPE_FORMAT_I16_SNORM:
879 case PIPE_FORMAT_I16_FLOAT:
880 return COLORMASK_RGBA;
881
882 default:
883 return ~0; /* Unsupported. */
884 }
885 }
886
887 boolean r300_is_colorbuffer_format_supported(enum pipe_format format)
888 {
889 return r300_translate_colorformat(format) != ~0 &&
890 r300_translate_out_fmt(format) != ~0 &&
891 r300_translate_colormask_swizzle(format) != ~0;
892 }
893
894 boolean r300_is_zs_format_supported(enum pipe_format format)
895 {
896 return r300_translate_zsformat(format) != ~0;
897 }
898
899 boolean r300_is_sampler_format_supported(enum pipe_format format)
900 {
901 return r300_translate_texformat(format, 0, TRUE, FALSE) != ~0;
902 }
903
904 void r300_texture_setup_format_state(struct r300_screen *screen,
905 struct r300_resource *tex,
906 enum pipe_format format,
907 unsigned level,
908 unsigned width0_override,
909 unsigned height0_override,
910 struct r300_texture_format_state *out)
911 {
912 struct pipe_resource *pt = &tex->b.b;
913 struct r300_texture_desc *desc = &tex->tex;
914 boolean is_r500 = screen->caps.is_r500;
915 unsigned width, height, depth;
916 unsigned txwidth, txheight, txdepth;
917
918 width = u_minify(width0_override, level);
919 height = u_minify(height0_override, level);
920 depth = u_minify(desc->depth0, level);
921
922 txwidth = (width - 1) & 0x7ff;
923 txheight = (height - 1) & 0x7ff;
924 txdepth = util_logbase2(depth) & 0xf;
925
926 /* Mask out all the fields we change. */
927 out->format0 = 0;
928 out->format1 &= ~R300_TX_FORMAT_TEX_COORD_TYPE_MASK;
929 out->format2 &= R500_TXFORMAT_MSB;
930 out->tile_config = 0;
931
932 /* Set sampler state. */
933 out->format0 =
934 R300_TX_WIDTH(txwidth) |
935 R300_TX_HEIGHT(txheight) |
936 R300_TX_DEPTH(txdepth);
937
938 if (desc->uses_stride_addressing) {
939 unsigned stride =
940 r300_stride_to_width(format, desc->stride_in_bytes[level]);
941 /* rectangles love this */
942 out->format0 |= R300_TX_PITCH_EN;
943 out->format2 = (stride - 1) & 0x1fff;
944 }
945
946 if (pt->target == PIPE_TEXTURE_CUBE) {
947 out->format1 |= R300_TX_FORMAT_CUBIC_MAP;
948 }
949 if (pt->target == PIPE_TEXTURE_3D) {
950 out->format1 |= R300_TX_FORMAT_3D;
951 }
952
953 /* large textures on r500 */
954 if (is_r500)
955 {
956 unsigned us_width = txwidth;
957 unsigned us_height = txheight;
958 unsigned us_depth = txdepth;
959
960 if (width > 2048) {
961 out->format2 |= R500_TXWIDTH_BIT11;
962 }
963 if (height > 2048) {
964 out->format2 |= R500_TXHEIGHT_BIT11;
965 }
966
967 /* The US_FORMAT register fixes an R500 TX addressing bug.
968 * Don't ask why it must be set like this. I don't know it either. */
969 if (width > 2048) {
970 us_width = (0x000007FF + us_width) >> 1;
971 us_depth |= 0x0000000D;
972 }
973 if (height > 2048) {
974 us_height = (0x000007FF + us_height) >> 1;
975 us_depth |= 0x0000000E;
976 }
977
978 out->us_format0 =
979 R300_TX_WIDTH(us_width) |
980 R300_TX_HEIGHT(us_height) |
981 R300_TX_DEPTH(us_depth);
982 }
983
984 out->tile_config = R300_TXO_MACRO_TILE(desc->macrotile[level]) |
985 R300_TXO_MICRO_TILE(desc->microtile) |
986 R300_TXO_ENDIAN(r300_get_endian_swap(format));
987 }
988
989 static void r300_texture_setup_fb_state(struct r300_surface *surf)
990 {
991 struct r300_resource *tex = r300_resource(surf->base.texture);
992 unsigned level = surf->base.u.tex.level;
993 unsigned stride =
994 r300_stride_to_width(surf->base.format, tex->tex.stride_in_bytes[level]);
995
996 /* Set framebuffer state. */
997 if (util_format_is_depth_or_stencil(surf->base.format)) {
998 surf->pitch =
999 stride |
1000 R300_DEPTHMACROTILE(tex->tex.macrotile[level]) |
1001 R300_DEPTHMICROTILE(tex->tex.microtile) |
1002 R300_DEPTHENDIAN(r300_get_endian_swap(surf->base.format));
1003 surf->format = r300_translate_zsformat(surf->base.format);
1004 surf->pitch_zmask = tex->tex.zmask_stride_in_pixels[level];
1005 surf->pitch_hiz = tex->tex.hiz_stride_in_pixels[level];
1006 } else {
1007 enum pipe_format format = util_format_linear(surf->base.format);
1008
1009 surf->pitch =
1010 stride |
1011 r300_translate_colorformat(format) |
1012 R300_COLOR_TILE(tex->tex.macrotile[level]) |
1013 R300_COLOR_MICROTILE(tex->tex.microtile) |
1014 R300_COLOR_ENDIAN(r300_get_endian_swap(format));
1015 surf->format = r300_translate_out_fmt(format);
1016 surf->colormask_swizzle =
1017 r300_translate_colormask_swizzle(format);
1018 surf->pitch_cmask = tex->tex.cmask_stride_in_pixels;
1019 }
1020 }
1021
1022 static void r300_texture_destroy(struct pipe_screen *screen,
1023 struct pipe_resource* texture)
1024 {
1025 struct r300_screen *rscreen = r300_screen(screen);
1026 struct r300_resource* tex = (struct r300_resource*)texture;
1027
1028 if (tex->tex.cmask_dwords) {
1029 mtx_lock(&rscreen->cmask_mutex);
1030 if (texture == rscreen->cmask_resource) {
1031 rscreen->cmask_resource = NULL;
1032 }
1033 mtx_unlock(&rscreen->cmask_mutex);
1034 }
1035 pb_reference(&tex->buf, NULL);
1036 FREE(tex);
1037 }
1038
1039 bool r300_resource_get_handle(struct pipe_screen* screen,
1040 struct pipe_context *ctx,
1041 struct pipe_resource *texture,
1042 struct winsys_handle *whandle,
1043 unsigned usage)
1044 {
1045 struct radeon_winsys *rws = r300_screen(screen)->rws;
1046 struct r300_resource* tex = (struct r300_resource*)texture;
1047
1048 if (!tex) {
1049 return false;
1050 }
1051
1052 whandle->stride = tex->tex.stride_in_bytes[0];
1053 whandle->offset = 0;
1054
1055 return rws->buffer_get_handle(rws, tex->buf, whandle);
1056 }
1057
1058 static const struct u_resource_vtbl r300_texture_vtbl =
1059 {
1060 NULL, /* get_handle */
1061 r300_texture_destroy, /* resource_destroy */
1062 r300_texture_transfer_map, /* transfer_map */
1063 NULL, /* transfer_flush_region */
1064 r300_texture_transfer_unmap, /* transfer_unmap */
1065 };
1066
1067 /* The common texture constructor. */
1068 static struct r300_resource*
1069 r300_texture_create_object(struct r300_screen *rscreen,
1070 const struct pipe_resource *base,
1071 enum radeon_bo_layout microtile,
1072 enum radeon_bo_layout macrotile,
1073 unsigned stride_in_bytes_override,
1074 struct pb_buffer *buffer)
1075 {
1076 struct radeon_winsys *rws = rscreen->rws;
1077 struct r300_resource *tex = NULL;
1078 struct radeon_bo_metadata tiling = {};
1079
1080 tex = CALLOC_STRUCT(r300_resource);
1081 if (!tex) {
1082 goto fail;
1083 }
1084
1085 pipe_reference_init(&tex->b.b.reference, 1);
1086 tex->b.b.screen = &rscreen->screen;
1087 tex->b.b.usage = base->usage;
1088 tex->b.b.bind = base->bind;
1089 tex->b.b.flags = base->flags;
1090 tex->b.vtbl = &r300_texture_vtbl;
1091 tex->tex.microtile = microtile;
1092 tex->tex.macrotile[0] = macrotile;
1093 tex->tex.stride_in_bytes_override = stride_in_bytes_override;
1094 tex->domain = (base->flags & R300_RESOURCE_FLAG_TRANSFER ||
1095 base->usage == PIPE_USAGE_STAGING) ? RADEON_DOMAIN_GTT :
1096 base->nr_samples > 1 ? RADEON_DOMAIN_VRAM :
1097 RADEON_DOMAIN_VRAM | RADEON_DOMAIN_GTT;
1098 tex->buf = buffer;
1099
1100 r300_texture_desc_init(rscreen, tex, base);
1101
1102 /* Figure out the ideal placement for the texture.. */
1103 if (tex->domain & RADEON_DOMAIN_VRAM &&
1104 tex->tex.size_in_bytes >= rscreen->info.vram_size) {
1105 tex->domain &= ~RADEON_DOMAIN_VRAM;
1106 tex->domain |= RADEON_DOMAIN_GTT;
1107 }
1108 if (tex->domain & RADEON_DOMAIN_GTT &&
1109 tex->tex.size_in_bytes >= rscreen->info.gart_size) {
1110 tex->domain &= ~RADEON_DOMAIN_GTT;
1111 }
1112 /* Just fail if the texture is too large. */
1113 if (!tex->domain) {
1114 goto fail;
1115 }
1116
1117 /* Create the backing buffer if needed. */
1118 if (!tex->buf) {
1119 /* Only use the first domain for allocation. Multiple domains are not allowed. */
1120 unsigned alloc_domain =
1121 tex->domain & RADEON_DOMAIN_VRAM ? RADEON_DOMAIN_VRAM :
1122 RADEON_DOMAIN_GTT;
1123
1124 tex->buf = rws->buffer_create(rws, tex->tex.size_in_bytes, 2048,
1125 alloc_domain,
1126 RADEON_FLAG_NO_SUBALLOC |
1127 /* Use the reusable pool: */
1128 RADEON_FLAG_NO_INTERPROCESS_SHARING);
1129
1130 if (!tex->buf) {
1131 goto fail;
1132 }
1133 }
1134
1135 if (SCREEN_DBG_ON(rscreen, DBG_MSAA) && base->nr_samples > 1) {
1136 fprintf(stderr, "r300: %ix MSAA %s buffer created\n",
1137 base->nr_samples,
1138 util_format_is_depth_or_stencil(base->format) ? "depth" : "color");
1139 }
1140
1141 tiling.u.legacy.microtile = tex->tex.microtile;
1142 tiling.u.legacy.macrotile = tex->tex.macrotile[0];
1143 tiling.u.legacy.stride = tex->tex.stride_in_bytes[0];
1144 rws->buffer_set_metadata(tex->buf, &tiling);
1145
1146 return tex;
1147
1148 fail:
1149 FREE(tex);
1150 if (buffer)
1151 pb_reference(&buffer, NULL);
1152 return NULL;
1153 }
1154
1155 /* Create a new texture. */
1156 struct pipe_resource *r300_texture_create(struct pipe_screen *screen,
1157 const struct pipe_resource *base)
1158 {
1159 struct r300_screen *rscreen = r300_screen(screen);
1160 enum radeon_bo_layout microtile, macrotile;
1161
1162 if ((base->flags & R300_RESOURCE_FLAG_TRANSFER) ||
1163 (base->bind & (PIPE_BIND_SCANOUT | PIPE_BIND_LINEAR))) {
1164 microtile = RADEON_LAYOUT_LINEAR;
1165 macrotile = RADEON_LAYOUT_LINEAR;
1166 } else {
1167 /* This will make the texture_create_function select the layout. */
1168 microtile = RADEON_LAYOUT_UNKNOWN;
1169 macrotile = RADEON_LAYOUT_UNKNOWN;
1170 }
1171
1172 return (struct pipe_resource*)
1173 r300_texture_create_object(rscreen, base, microtile, macrotile,
1174 0, NULL);
1175 }
1176
1177 struct pipe_resource *r300_texture_from_handle(struct pipe_screen *screen,
1178 const struct pipe_resource *base,
1179 struct winsys_handle *whandle,
1180 unsigned usage)
1181 {
1182 struct r300_screen *rscreen = r300_screen(screen);
1183 struct radeon_winsys *rws = rscreen->rws;
1184 struct pb_buffer *buffer;
1185 struct radeon_bo_metadata tiling = {};
1186
1187 /* Support only 2D textures without mipmaps */
1188 if ((base->target != PIPE_TEXTURE_2D &&
1189 base->target != PIPE_TEXTURE_RECT) ||
1190 base->depth0 != 1 ||
1191 base->last_level != 0) {
1192 return NULL;
1193 }
1194
1195 buffer = rws->buffer_from_handle(rws, whandle, 0);
1196 if (!buffer)
1197 return NULL;
1198
1199 rws->buffer_get_metadata(buffer, &tiling);
1200
1201 /* Enforce a microtiled zbuffer. */
1202 if (util_format_is_depth_or_stencil(base->format) &&
1203 tiling.u.legacy.microtile == RADEON_LAYOUT_LINEAR) {
1204 switch (util_format_get_blocksize(base->format)) {
1205 case 4:
1206 tiling.u.legacy.microtile = RADEON_LAYOUT_TILED;
1207 break;
1208
1209 case 2:
1210 tiling.u.legacy.microtile = RADEON_LAYOUT_SQUARETILED;
1211 break;
1212 }
1213 }
1214
1215 return (struct pipe_resource*)
1216 r300_texture_create_object(rscreen, base, tiling.u.legacy.microtile, tiling.u.legacy.macrotile,
1217 whandle->stride, buffer);
1218 }
1219
1220 /* Not required to implement u_resource_vtbl, consider moving to another file:
1221 */
1222 struct pipe_surface* r300_create_surface_custom(struct pipe_context * ctx,
1223 struct pipe_resource* texture,
1224 const struct pipe_surface *surf_tmpl,
1225 unsigned width0_override,
1226 unsigned height0_override)
1227 {
1228 struct r300_resource* tex = r300_resource(texture);
1229 struct r300_surface* surface = CALLOC_STRUCT(r300_surface);
1230 unsigned level = surf_tmpl->u.tex.level;
1231
1232 assert(surf_tmpl->u.tex.first_layer == surf_tmpl->u.tex.last_layer);
1233
1234 if (surface) {
1235 uint32_t offset, tile_height;
1236
1237 pipe_reference_init(&surface->base.reference, 1);
1238 pipe_resource_reference(&surface->base.texture, texture);
1239 surface->base.context = ctx;
1240 surface->base.format = surf_tmpl->format;
1241 surface->base.width = u_minify(width0_override, level);
1242 surface->base.height = u_minify(height0_override, level);
1243 surface->base.u.tex.level = level;
1244 surface->base.u.tex.first_layer = surf_tmpl->u.tex.first_layer;
1245 surface->base.u.tex.last_layer = surf_tmpl->u.tex.last_layer;
1246
1247 surface->buf = tex->buf;
1248
1249 /* Prefer VRAM if there are multiple domains to choose from. */
1250 surface->domain = tex->domain;
1251 if (surface->domain & RADEON_DOMAIN_VRAM)
1252 surface->domain &= ~RADEON_DOMAIN_GTT;
1253
1254 surface->offset = r300_texture_get_offset(tex, level,
1255 surf_tmpl->u.tex.first_layer);
1256 r300_texture_setup_fb_state(surface);
1257
1258 /* Parameters for the CBZB clear. */
1259 surface->cbzb_allowed = tex->tex.cbzb_allowed[level];
1260 surface->cbzb_width = align(surface->base.width, 64);
1261
1262 /* Height must be aligned to the size of a tile. */
1263 tile_height = r300_get_pixel_alignment(surface->base.format,
1264 tex->b.b.nr_samples,
1265 tex->tex.microtile,
1266 tex->tex.macrotile[level],
1267 DIM_HEIGHT, 0);
1268
1269 surface->cbzb_height = align((surface->base.height + 1) / 2,
1270 tile_height);
1271
1272 /* Offset must be aligned to 2K and must point at the beginning
1273 * of a scanline. */
1274 offset = surface->offset +
1275 tex->tex.stride_in_bytes[level] * surface->cbzb_height;
1276 surface->cbzb_midpoint_offset = offset & ~2047;
1277
1278 surface->cbzb_pitch = surface->pitch & 0x1ffffc;
1279
1280 if (util_format_get_blocksizebits(surface->base.format) == 32)
1281 surface->cbzb_format = R300_DEPTHFORMAT_24BIT_INT_Z_8BIT_STENCIL;
1282 else
1283 surface->cbzb_format = R300_DEPTHFORMAT_16BIT_INT_Z;
1284
1285 DBG(r300_context(ctx), DBG_CBZB,
1286 "CBZB Allowed: %s, Dim: %ix%i, Misalignment: %i, Micro: %s, Macro: %s\n",
1287 surface->cbzb_allowed ? "YES" : " NO",
1288 surface->cbzb_width, surface->cbzb_height,
1289 offset & 2047,
1290 tex->tex.microtile ? "YES" : " NO",
1291 tex->tex.macrotile[level] ? "YES" : " NO");
1292 }
1293
1294 return &surface->base;
1295 }
1296
1297 struct pipe_surface* r300_create_surface(struct pipe_context * ctx,
1298 struct pipe_resource* texture,
1299 const struct pipe_surface *surf_tmpl)
1300 {
1301 return r300_create_surface_custom(ctx, texture, surf_tmpl,
1302 texture->width0,
1303 texture->height0);
1304 }
1305
1306 /* Not required to implement u_resource_vtbl, consider moving to another file:
1307 */
1308 void r300_surface_destroy(struct pipe_context *ctx, struct pipe_surface* s)
1309 {
1310 pipe_resource_reference(&s->texture, NULL);
1311 FREE(s);
1312 }