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