Merge remote branch 'origin/nv50-compiler'
[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 #include "r300_winsys.h"
33
34 #include "util/u_format.h"
35 #include "util/u_format_s3tc.h"
36 #include "util/u_math.h"
37 #include "util/u_memory.h"
38 #include "util/u_mm.h"
39
40 #include "pipe/p_screen.h"
41
42 unsigned r300_get_swizzle_combined(const unsigned char *swizzle_format,
43 const unsigned char *swizzle_view)
44 {
45 unsigned i;
46 unsigned char swizzle[4];
47 unsigned result = 0;
48 const uint32_t swizzle_shift[4] = {
49 R300_TX_FORMAT_R_SHIFT,
50 R300_TX_FORMAT_G_SHIFT,
51 R300_TX_FORMAT_B_SHIFT,
52 R300_TX_FORMAT_A_SHIFT
53 };
54 const uint32_t swizzle_bit[4] = {
55 R300_TX_FORMAT_X,
56 R300_TX_FORMAT_Y,
57 R300_TX_FORMAT_Z,
58 R300_TX_FORMAT_W
59 };
60
61 if (swizzle_view) {
62 /* Combine two sets of swizzles. */
63 for (i = 0; i < 4; i++) {
64 swizzle[i] = swizzle_view[i] <= UTIL_FORMAT_SWIZZLE_W ?
65 swizzle_format[swizzle_view[i]] : swizzle_view[i];
66 }
67 } else {
68 memcpy(swizzle, swizzle_format, 4);
69 }
70
71 /* Get swizzle. */
72 for (i = 0; i < 4; i++) {
73 switch (swizzle[i]) {
74 case UTIL_FORMAT_SWIZZLE_Y:
75 result |= swizzle_bit[1] << swizzle_shift[i];
76 break;
77 case UTIL_FORMAT_SWIZZLE_Z:
78 result |= swizzle_bit[2] << swizzle_shift[i];
79 break;
80 case UTIL_FORMAT_SWIZZLE_W:
81 result |= swizzle_bit[3] << swizzle_shift[i];
82 break;
83 case UTIL_FORMAT_SWIZZLE_0:
84 result |= R300_TX_FORMAT_ZERO << swizzle_shift[i];
85 break;
86 case UTIL_FORMAT_SWIZZLE_1:
87 result |= R300_TX_FORMAT_ONE << swizzle_shift[i];
88 break;
89 default: /* UTIL_FORMAT_SWIZZLE_X */
90 result |= swizzle_bit[0] << swizzle_shift[i];
91 }
92 }
93 return result;
94 }
95
96 /* Translate a pipe_format into a useful texture format for sampling.
97 *
98 * Some special formats are translated directly using R300_EASY_TX_FORMAT,
99 * but the majority of them is translated in a generic way, automatically
100 * supporting all the formats hw can support.
101 *
102 * R300_EASY_TX_FORMAT swizzles the texture.
103 * Note the signature of R300_EASY_TX_FORMAT:
104 * R300_EASY_TX_FORMAT(B, G, R, A, FORMAT);
105 *
106 * The FORMAT specifies how the texture sampler will treat the texture, and
107 * makes available X, Y, Z, W, ZERO, and ONE for swizzling. */
108 uint32_t r300_translate_texformat(enum pipe_format format,
109 const unsigned char *swizzle_view,
110 boolean is_r500)
111 {
112 uint32_t result = 0;
113 const struct util_format_description *desc;
114 unsigned i;
115 boolean uniform = TRUE;
116 const uint32_t sign_bit[4] = {
117 R300_TX_FORMAT_SIGNED_X,
118 R300_TX_FORMAT_SIGNED_Y,
119 R300_TX_FORMAT_SIGNED_Z,
120 R300_TX_FORMAT_SIGNED_W,
121 };
122
123 desc = util_format_description(format);
124
125 /* Colorspace (return non-RGB formats directly). */
126 switch (desc->colorspace) {
127 /* Depth stencil formats.
128 * Swizzles are added in r300_merge_textures_and_samplers. */
129 case UTIL_FORMAT_COLORSPACE_ZS:
130 switch (format) {
131 case PIPE_FORMAT_Z16_UNORM:
132 return R300_TX_FORMAT_X16;
133 case PIPE_FORMAT_X8Z24_UNORM:
134 case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
135 if (is_r500)
136 return R500_TX_FORMAT_Y8X24;
137 else
138 return R300_TX_FORMAT_Y16X16;
139 default:
140 return ~0; /* Unsupported. */
141 }
142
143 /* YUV formats. */
144 case UTIL_FORMAT_COLORSPACE_YUV:
145 result |= R300_TX_FORMAT_YUV_TO_RGB;
146
147 switch (format) {
148 case PIPE_FORMAT_UYVY:
149 return R300_EASY_TX_FORMAT(X, Y, Z, ONE, YVYU422) | result;
150 case PIPE_FORMAT_YUYV:
151 return R300_EASY_TX_FORMAT(X, Y, Z, ONE, VYUY422) | result;
152 default:
153 return ~0; /* Unsupported/unknown. */
154 }
155
156 /* Add gamma correction. */
157 case UTIL_FORMAT_COLORSPACE_SRGB:
158 result |= R300_TX_FORMAT_GAMMA;
159 break;
160
161 default:
162 switch (format) {
163 /* Same as YUV but without the YUR->RGB conversion. */
164 case PIPE_FORMAT_R8G8_B8G8_UNORM:
165 return R300_EASY_TX_FORMAT(X, Y, Z, ONE, YVYU422) | result;
166 case PIPE_FORMAT_G8R8_G8B8_UNORM:
167 return R300_EASY_TX_FORMAT(X, Y, Z, ONE, VYUY422) | result;
168 default:;
169 }
170 }
171
172 result |= r300_get_swizzle_combined(desc->swizzle, swizzle_view);
173
174 /* S3TC formats. */
175 if (desc->layout == UTIL_FORMAT_LAYOUT_S3TC) {
176 if (!util_format_s3tc_enabled) {
177 return ~0; /* Unsupported. */
178 }
179
180 switch (format) {
181 case PIPE_FORMAT_DXT1_RGB:
182 case PIPE_FORMAT_DXT1_RGBA:
183 case PIPE_FORMAT_DXT1_SRGB:
184 case PIPE_FORMAT_DXT1_SRGBA:
185 return R300_TX_FORMAT_DXT1 | result;
186 case PIPE_FORMAT_DXT3_RGBA:
187 case PIPE_FORMAT_DXT3_SRGBA:
188 return R300_TX_FORMAT_DXT3 | result;
189 case PIPE_FORMAT_DXT5_RGBA:
190 case PIPE_FORMAT_DXT5_SRGBA:
191 return R300_TX_FORMAT_DXT5 | result;
192 default:
193 return ~0; /* Unsupported/unknown. */
194 }
195 }
196
197 /* Add sign. */
198 for (i = 0; i < desc->nr_channels; i++) {
199 if (desc->channel[i].type == UTIL_FORMAT_TYPE_SIGNED) {
200 result |= sign_bit[i];
201 }
202 }
203
204 /* This is truly a special format.
205 * It stores R8G8 and B is computed using sqrt(1 - R^2 - G^2)
206 * in the sampler unit. Also known as D3DFMT_CxV8U8. */
207 if (format == PIPE_FORMAT_R8G8Bx_SNORM) {
208 return R300_TX_FORMAT_CxV8U8 | result;
209 }
210
211 /* RGTC formats. */
212 if (desc->layout == UTIL_FORMAT_LAYOUT_RGTC) {
213 switch (format) {
214 case PIPE_FORMAT_RGTC1_UNORM:
215 case PIPE_FORMAT_RGTC1_SNORM:
216 return R500_TX_FORMAT_ATI1N | result;
217 case PIPE_FORMAT_RGTC2_UNORM:
218 case PIPE_FORMAT_RGTC2_SNORM:
219 return R400_TX_FORMAT_ATI2N | result;
220 default:
221 return ~0; /* Unsupported/unknown. */
222 }
223 }
224
225 /* See whether the components are of the same size. */
226 for (i = 1; i < desc->nr_channels; i++) {
227 uniform = uniform && desc->channel[0].size == desc->channel[i].size;
228 }
229
230 /* Non-uniform formats. */
231 if (!uniform) {
232 switch (desc->nr_channels) {
233 case 3:
234 if (desc->channel[0].size == 5 &&
235 desc->channel[1].size == 6 &&
236 desc->channel[2].size == 5) {
237 return R300_TX_FORMAT_Z5Y6X5 | result;
238 }
239 if (desc->channel[0].size == 5 &&
240 desc->channel[1].size == 5 &&
241 desc->channel[2].size == 6) {
242 return R300_TX_FORMAT_Z6Y5X5 | result;
243 }
244 return ~0; /* Unsupported/unknown. */
245
246 case 4:
247 if (desc->channel[0].size == 5 &&
248 desc->channel[1].size == 5 &&
249 desc->channel[2].size == 5 &&
250 desc->channel[3].size == 1) {
251 return R300_TX_FORMAT_W1Z5Y5X5 | result;
252 }
253 if (desc->channel[0].size == 10 &&
254 desc->channel[1].size == 10 &&
255 desc->channel[2].size == 10 &&
256 desc->channel[3].size == 2) {
257 return R300_TX_FORMAT_W2Z10Y10X10 | result;
258 }
259 }
260 return ~0; /* Unsupported/unknown. */
261 }
262
263 /* And finally, uniform formats. */
264 switch (desc->channel[0].type) {
265 case UTIL_FORMAT_TYPE_UNSIGNED:
266 case UTIL_FORMAT_TYPE_SIGNED:
267 if (!desc->channel[0].normalized &&
268 desc->colorspace != UTIL_FORMAT_COLORSPACE_SRGB) {
269 return ~0;
270 }
271
272 switch (desc->channel[0].size) {
273 case 4:
274 switch (desc->nr_channels) {
275 case 2:
276 return R300_TX_FORMAT_Y4X4 | result;
277 case 4:
278 return R300_TX_FORMAT_W4Z4Y4X4 | result;
279 }
280 return ~0;
281
282 case 8:
283 switch (desc->nr_channels) {
284 case 1:
285 return R300_TX_FORMAT_X8 | result;
286 case 2:
287 return R300_TX_FORMAT_Y8X8 | result;
288 case 4:
289 return R300_TX_FORMAT_W8Z8Y8X8 | result;
290 }
291 return ~0;
292
293 case 16:
294 switch (desc->nr_channels) {
295 case 1:
296 return R300_TX_FORMAT_X16 | result;
297 case 2:
298 return R300_TX_FORMAT_Y16X16 | result;
299 case 4:
300 return R300_TX_FORMAT_W16Z16Y16X16 | result;
301 }
302 }
303 return ~0;
304
305 case UTIL_FORMAT_TYPE_FLOAT:
306 switch (desc->channel[0].size) {
307 case 16:
308 switch (desc->nr_channels) {
309 case 1:
310 return R300_TX_FORMAT_16F | result;
311 case 2:
312 return R300_TX_FORMAT_16F_16F | result;
313 case 4:
314 return R300_TX_FORMAT_16F_16F_16F_16F | result;
315 }
316 return ~0;
317
318 case 32:
319 switch (desc->nr_channels) {
320 case 1:
321 return R300_TX_FORMAT_32F | result;
322 case 2:
323 return R300_TX_FORMAT_32F_32F | result;
324 case 4:
325 return R300_TX_FORMAT_32F_32F_32F_32F | result;
326 }
327 }
328 }
329
330 return ~0; /* Unsupported/unknown. */
331 }
332
333 uint32_t r500_tx_format_msb_bit(enum pipe_format format)
334 {
335 switch (format) {
336 case PIPE_FORMAT_RGTC1_UNORM:
337 case PIPE_FORMAT_RGTC1_SNORM:
338 case PIPE_FORMAT_X8Z24_UNORM:
339 case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
340 return R500_TXFORMAT_MSB;
341 default:
342 return 0;
343 }
344 }
345
346 /* Buffer formats. */
347
348 /* Colorbuffer formats. This is the unswizzled format of the RB3D block's
349 * output. For the swizzling of the targets, check the shader's format. */
350 static uint32_t r300_translate_colorformat(enum pipe_format format)
351 {
352 switch (format) {
353 /* 8-bit buffers. */
354 case PIPE_FORMAT_A8_UNORM:
355 case PIPE_FORMAT_I8_UNORM:
356 case PIPE_FORMAT_L8_UNORM:
357 case PIPE_FORMAT_R8_UNORM:
358 case PIPE_FORMAT_R8_SNORM:
359 return R300_COLOR_FORMAT_I8;
360
361 /* 16-bit buffers. */
362 case PIPE_FORMAT_B5G6R5_UNORM:
363 return R300_COLOR_FORMAT_RGB565;
364
365 case PIPE_FORMAT_B5G5R5A1_UNORM:
366 case PIPE_FORMAT_B5G5R5X1_UNORM:
367 return R300_COLOR_FORMAT_ARGB1555;
368
369 case PIPE_FORMAT_B4G4R4A4_UNORM:
370 case PIPE_FORMAT_B4G4R4X4_UNORM:
371 return R300_COLOR_FORMAT_ARGB4444;
372
373 /* 32-bit buffers. */
374 case PIPE_FORMAT_B8G8R8A8_UNORM:
375 case PIPE_FORMAT_B8G8R8X8_UNORM:
376 case PIPE_FORMAT_A8R8G8B8_UNORM:
377 case PIPE_FORMAT_X8R8G8B8_UNORM:
378 case PIPE_FORMAT_A8B8G8R8_UNORM:
379 case PIPE_FORMAT_R8G8B8A8_SNORM:
380 case PIPE_FORMAT_X8B8G8R8_UNORM:
381 case PIPE_FORMAT_R8G8B8X8_UNORM:
382 case PIPE_FORMAT_R8SG8SB8UX8U_NORM:
383 return R300_COLOR_FORMAT_ARGB8888;
384
385 case PIPE_FORMAT_R10G10B10A2_UNORM:
386 case PIPE_FORMAT_R10G10B10X2_SNORM:
387 case PIPE_FORMAT_B10G10R10A2_UNORM:
388 case PIPE_FORMAT_R10SG10SB10SA2U_NORM:
389 return R500_COLOR_FORMAT_ARGB2101010; /* R5xx-only? */
390
391 /* 64-bit buffers. */
392 case PIPE_FORMAT_R16G16B16A16_UNORM:
393 case PIPE_FORMAT_R16G16B16A16_SNORM:
394 case PIPE_FORMAT_R16G16B16A16_FLOAT:
395 return R300_COLOR_FORMAT_ARGB16161616;
396
397 /* 128-bit buffers. */
398 case PIPE_FORMAT_R32G32B32A32_FLOAT:
399 return R300_COLOR_FORMAT_ARGB32323232;
400
401 /* YUV buffers. */
402 case PIPE_FORMAT_UYVY:
403 return R300_COLOR_FORMAT_YVYU;
404 case PIPE_FORMAT_YUYV:
405 return R300_COLOR_FORMAT_VYUY;
406 default:
407 return ~0; /* Unsupported. */
408 }
409 }
410
411 /* Depthbuffer and stencilbuffer. Thankfully, we only support two flavors. */
412 static uint32_t r300_translate_zsformat(enum pipe_format format)
413 {
414 switch (format) {
415 /* 16-bit depth, no stencil */
416 case PIPE_FORMAT_Z16_UNORM:
417 return R300_DEPTHFORMAT_16BIT_INT_Z;
418 /* 24-bit depth, ignored stencil */
419 case PIPE_FORMAT_X8Z24_UNORM:
420 /* 24-bit depth, 8-bit stencil */
421 case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
422 return R300_DEPTHFORMAT_24BIT_INT_Z_8BIT_STENCIL;
423 default:
424 return ~0; /* Unsupported. */
425 }
426 }
427
428 /* Shader output formats. This is essentially the swizzle from the shader
429 * to the RB3D block.
430 *
431 * Note that formats are stored from C3 to C0. */
432 static uint32_t r300_translate_out_fmt(enum pipe_format format)
433 {
434 uint32_t modifier = 0;
435 unsigned i;
436 const struct util_format_description *desc;
437 static const uint32_t sign_bit[4] = {
438 R300_OUT_SIGN(0x1),
439 R300_OUT_SIGN(0x2),
440 R300_OUT_SIGN(0x4),
441 R300_OUT_SIGN(0x8),
442 };
443
444 desc = util_format_description(format);
445
446 /* Specifies how the shader output is written to the fog unit. */
447 if (desc->channel[0].type == UTIL_FORMAT_TYPE_FLOAT) {
448 if (desc->channel[0].size == 32) {
449 modifier |= R300_US_OUT_FMT_C4_32_FP;
450 } else {
451 modifier |= R300_US_OUT_FMT_C4_16_FP;
452 }
453 } else {
454 if (desc->channel[0].size == 16) {
455 modifier |= R300_US_OUT_FMT_C4_16;
456 } else {
457 /* C4_8 seems to be used for the formats whose pixel size
458 * is <= 32 bits. */
459 modifier |= R300_US_OUT_FMT_C4_8;
460 }
461 }
462
463 /* Add sign. */
464 for (i = 0; i < 4; i++)
465 if (desc->channel[i].type == UTIL_FORMAT_TYPE_SIGNED) {
466 modifier |= sign_bit[i];
467 }
468
469 /* Add swizzles and return. */
470 switch (format) {
471 /* 8-bit outputs.
472 * COLORFORMAT_I8 stores the C2 component. */
473 case PIPE_FORMAT_A8_UNORM:
474 return modifier | R300_C2_SEL_A;
475 case PIPE_FORMAT_I8_UNORM:
476 case PIPE_FORMAT_L8_UNORM:
477 case PIPE_FORMAT_R8_UNORM:
478 case PIPE_FORMAT_R8_SNORM:
479 return modifier | R300_C2_SEL_R;
480
481 /* BGRA outputs. */
482 case PIPE_FORMAT_B5G6R5_UNORM:
483 case PIPE_FORMAT_B5G5R5A1_UNORM:
484 case PIPE_FORMAT_B5G5R5X1_UNORM:
485 case PIPE_FORMAT_B4G4R4A4_UNORM:
486 case PIPE_FORMAT_B4G4R4X4_UNORM:
487 case PIPE_FORMAT_B8G8R8A8_UNORM:
488 case PIPE_FORMAT_B8G8R8X8_UNORM:
489 case PIPE_FORMAT_B10G10R10A2_UNORM:
490 return modifier |
491 R300_C0_SEL_B | R300_C1_SEL_G |
492 R300_C2_SEL_R | R300_C3_SEL_A;
493
494 /* ARGB outputs. */
495 case PIPE_FORMAT_A8R8G8B8_UNORM:
496 case PIPE_FORMAT_X8R8G8B8_UNORM:
497 return modifier |
498 R300_C0_SEL_A | R300_C1_SEL_R |
499 R300_C2_SEL_G | R300_C3_SEL_B;
500
501 /* ABGR outputs. */
502 case PIPE_FORMAT_A8B8G8R8_UNORM:
503 case PIPE_FORMAT_X8B8G8R8_UNORM:
504 return modifier |
505 R300_C0_SEL_A | R300_C1_SEL_B |
506 R300_C2_SEL_G | R300_C3_SEL_R;
507
508 /* RGBA outputs. */
509 case PIPE_FORMAT_R8G8B8X8_UNORM:
510 case PIPE_FORMAT_R8G8B8A8_SNORM:
511 case PIPE_FORMAT_R8SG8SB8UX8U_NORM:
512 case PIPE_FORMAT_R10G10B10A2_UNORM:
513 case PIPE_FORMAT_R10G10B10X2_SNORM:
514 case PIPE_FORMAT_R10SG10SB10SA2U_NORM:
515 case PIPE_FORMAT_R16G16B16A16_UNORM:
516 case PIPE_FORMAT_R16G16B16A16_SNORM:
517 case PIPE_FORMAT_R16G16B16A16_FLOAT:
518 case PIPE_FORMAT_R32G32B32A32_FLOAT:
519 return modifier |
520 R300_C0_SEL_R | R300_C1_SEL_G |
521 R300_C2_SEL_B | R300_C3_SEL_A;
522
523 default:
524 return ~0; /* Unsupported. */
525 }
526 }
527
528 boolean r300_is_colorbuffer_format_supported(enum pipe_format format)
529 {
530 return r300_translate_colorformat(format) != ~0 &&
531 r300_translate_out_fmt(format) != ~0;
532 }
533
534 boolean r300_is_zs_format_supported(enum pipe_format format)
535 {
536 return r300_translate_zsformat(format) != ~0;
537 }
538
539 boolean r300_is_sampler_format_supported(enum pipe_format format)
540 {
541 return r300_translate_texformat(format, 0, TRUE) != ~0;
542 }
543
544 void r300_texture_setup_format_state(struct r300_screen *screen,
545 struct r300_texture_desc *desc,
546 unsigned level,
547 struct r300_texture_format_state *out)
548 {
549 struct pipe_resource *pt = &desc->b.b;
550 boolean is_r500 = screen->caps.is_r500;
551
552 /* Mask out all the fields we change. */
553 out->format0 = 0;
554 out->format1 &= ~R300_TX_FORMAT_TEX_COORD_TYPE_MASK;
555 out->format2 &= R500_TXFORMAT_MSB;
556 out->tile_config = 0;
557
558 /* Set sampler state. */
559 out->format0 = R300_TX_WIDTH((u_minify(pt->width0, level) - 1) & 0x7ff) |
560 R300_TX_HEIGHT((u_minify(pt->height0, level) - 1) & 0x7ff);
561
562 if (desc->uses_stride_addressing) {
563 /* rectangles love this */
564 out->format0 |= R300_TX_PITCH_EN;
565 out->format2 = (desc->stride_in_pixels[level] - 1) & 0x1fff;
566 } else {
567 /* Power of two textures (3D, mipmaps, and no pitch),
568 * also NPOT textures with a width being POT. */
569 out->format0 |=
570 R300_TX_DEPTH(util_logbase2(u_minify(pt->depth0, level)) & 0xf);
571 }
572
573 if (pt->target == PIPE_TEXTURE_CUBE) {
574 out->format1 |= R300_TX_FORMAT_CUBIC_MAP;
575 }
576 if (pt->target == PIPE_TEXTURE_3D) {
577 out->format1 |= R300_TX_FORMAT_3D;
578 }
579
580 /* large textures on r500 */
581 if (is_r500)
582 {
583 if (pt->width0 > 2048) {
584 out->format2 |= R500_TXWIDTH_BIT11;
585 }
586 if (pt->height0 > 2048) {
587 out->format2 |= R500_TXHEIGHT_BIT11;
588 }
589 }
590
591 out->tile_config = R300_TXO_MACRO_TILE(desc->macrotile[level]) |
592 R300_TXO_MICRO_TILE(desc->microtile);
593 }
594
595 static void r300_texture_setup_fb_state(struct r300_screen* screen,
596 struct r300_texture* tex)
597 {
598 unsigned i;
599
600 /* Set framebuffer state. */
601 if (util_format_is_depth_or_stencil(tex->desc.b.b.format)) {
602 for (i = 0; i <= tex->desc.b.b.last_level; i++) {
603 tex->fb_state.pitch[i] =
604 tex->desc.stride_in_pixels[i] |
605 R300_DEPTHMACROTILE(tex->desc.macrotile[i]) |
606 R300_DEPTHMICROTILE(tex->desc.microtile);
607 }
608 tex->fb_state.format = r300_translate_zsformat(tex->desc.b.b.format);
609 } else {
610 for (i = 0; i <= tex->desc.b.b.last_level; i++) {
611 tex->fb_state.pitch[i] =
612 tex->desc.stride_in_pixels[i] |
613 r300_translate_colorformat(tex->desc.b.b.format) |
614 R300_COLOR_TILE(tex->desc.macrotile[i]) |
615 R300_COLOR_MICROTILE(tex->desc.microtile);
616 }
617 tex->fb_state.format = r300_translate_out_fmt(tex->desc.b.b.format);
618 }
619 }
620
621 void r300_texture_reinterpret_format(struct pipe_screen *screen,
622 struct pipe_resource *tex,
623 enum pipe_format new_format)
624 {
625 struct r300_screen *r300screen = r300_screen(screen);
626
627 SCREEN_DBG(r300screen, DBG_TEX,
628 "r300: texture_reinterpret_format: %s -> %s\n",
629 util_format_short_name(tex->format),
630 util_format_short_name(new_format));
631
632 tex->format = new_format;
633
634 r300_texture_setup_fb_state(r300_screen(screen), r300_texture(tex));
635 }
636
637 static unsigned r300_texture_is_referenced(struct pipe_context *context,
638 struct pipe_resource *texture,
639 unsigned face, unsigned level)
640 {
641 struct r300_context *r300 = r300_context(context);
642 struct r300_texture *rtex = (struct r300_texture *)texture;
643
644 if (r300->rws->cs_is_buffer_referenced(r300->cs,
645 rtex->buffer, R300_REF_CS))
646 return PIPE_REFERENCED_FOR_READ | PIPE_REFERENCED_FOR_WRITE;
647
648 return PIPE_UNREFERENCED;
649 }
650
651 static void r300_texture_destroy(struct pipe_screen *screen,
652 struct pipe_resource* texture)
653 {
654 struct r300_texture* tex = (struct r300_texture*)texture;
655 struct r300_winsys_screen *rws = (struct r300_winsys_screen *)texture->screen->winsys;
656 int i;
657
658 rws->buffer_reference(rws, &tex->buffer, NULL);
659 for (i = 0; i < R300_MAX_TEXTURE_LEVELS; i++) {
660 if (tex->hiz_mem[i])
661 u_mmFreeMem(tex->hiz_mem[i]);
662 if (tex->zmask_mem[i])
663 u_mmFreeMem(tex->zmask_mem[i]);
664 }
665
666 FREE(tex);
667 }
668
669 static boolean r300_texture_get_handle(struct pipe_screen* screen,
670 struct pipe_resource *texture,
671 struct winsys_handle *whandle)
672 {
673 struct r300_winsys_screen *rws = (struct r300_winsys_screen *)screen->winsys;
674 struct r300_texture* tex = (struct r300_texture*)texture;
675
676 if (!tex) {
677 return FALSE;
678 }
679
680 return rws->buffer_get_handle(rws, tex->buffer,
681 tex->desc.stride_in_bytes[0], whandle);
682 }
683
684 struct u_resource_vtbl r300_texture_vtbl =
685 {
686 r300_texture_get_handle, /* get_handle */
687 r300_texture_destroy, /* resource_destroy */
688 r300_texture_is_referenced, /* is_resource_referenced */
689 r300_texture_get_transfer, /* get_transfer */
690 r300_texture_transfer_destroy, /* transfer_destroy */
691 r300_texture_transfer_map, /* transfer_map */
692 u_default_transfer_flush_region, /* transfer_flush_region */
693 r300_texture_transfer_unmap, /* transfer_unmap */
694 u_default_transfer_inline_write /* transfer_inline_write */
695 };
696
697 /* The common texture constructor. */
698 static struct r300_texture*
699 r300_texture_create_object(struct r300_screen *rscreen,
700 const struct pipe_resource *base,
701 enum r300_buffer_tiling microtile,
702 enum r300_buffer_tiling macrotile,
703 unsigned stride_in_bytes_override,
704 unsigned max_buffer_size,
705 struct r300_winsys_buffer *buffer)
706 {
707 struct r300_winsys_screen *rws = rscreen->rws;
708 struct r300_texture *tex = CALLOC_STRUCT(r300_texture);
709 if (!tex) {
710 if (buffer)
711 rws->buffer_reference(rws, &buffer, NULL);
712 return NULL;
713 }
714
715 /* Initialize the descriptor. */
716 if (!r300_texture_desc_init(rscreen, &tex->desc, base,
717 microtile, macrotile,
718 stride_in_bytes_override,
719 max_buffer_size)) {
720 if (buffer)
721 rws->buffer_reference(rws, &buffer, NULL);
722 FREE(tex);
723 return NULL;
724 }
725 /* Initialize the hardware state. */
726 r300_texture_setup_format_state(rscreen, &tex->desc, 0, &tex->tx_format);
727 r300_texture_setup_fb_state(rscreen, tex);
728
729 tex->desc.b.vtbl = &r300_texture_vtbl;
730 pipe_reference_init(&tex->desc.b.b.reference, 1);
731 tex->domain = base->flags & R300_RESOURCE_FLAG_TRANSFER ?
732 R300_DOMAIN_GTT :
733 R300_DOMAIN_VRAM | R300_DOMAIN_GTT;
734 tex->buffer = buffer;
735
736 /* Create the backing buffer if needed. */
737 if (!tex->buffer) {
738 tex->buffer = rws->buffer_create(rws, tex->desc.size_in_bytes, 2048,
739 base->bind, base->usage, tex->domain);
740
741 if (!tex->buffer) {
742 FREE(tex);
743 return NULL;
744 }
745 }
746
747 rws->buffer_set_tiling(rws, tex->buffer,
748 tex->desc.microtile, tex->desc.macrotile[0],
749 tex->desc.stride_in_bytes[0]);
750
751 return tex;
752 }
753
754 /* Create a new texture. */
755 struct pipe_resource *r300_texture_create(struct pipe_screen *screen,
756 const struct pipe_resource *base)
757 {
758 struct r300_screen *rscreen = r300_screen(screen);
759 enum r300_buffer_tiling microtile, macrotile;
760
761 if ((base->flags & R300_RESOURCE_FLAG_TRANSFER) ||
762 (base->bind & PIPE_BIND_SCANOUT)) {
763 microtile = R300_BUFFER_LINEAR;
764 macrotile = R300_BUFFER_LINEAR;
765 } else {
766 microtile = R300_BUFFER_SELECT_LAYOUT;
767 macrotile = R300_BUFFER_SELECT_LAYOUT;
768 }
769
770 return (struct pipe_resource*)
771 r300_texture_create_object(rscreen, base, microtile, macrotile,
772 0, 0, NULL);
773 }
774
775 struct pipe_resource *r300_texture_from_handle(struct pipe_screen *screen,
776 const struct pipe_resource *base,
777 struct winsys_handle *whandle)
778 {
779 struct r300_winsys_screen *rws = (struct r300_winsys_screen*)screen->winsys;
780 struct r300_screen *rscreen = r300_screen(screen);
781 struct r300_winsys_buffer *buffer;
782 enum r300_buffer_tiling microtile, macrotile;
783 unsigned stride, size;
784
785 /* Support only 2D textures without mipmaps */
786 if ((base->target != PIPE_TEXTURE_2D &&
787 base->target != PIPE_TEXTURE_RECT) ||
788 base->depth0 != 1 ||
789 base->last_level != 0) {
790 return NULL;
791 }
792
793 buffer = rws->buffer_from_handle(rws, whandle, &stride, &size);
794 if (!buffer)
795 return NULL;
796
797 rws->buffer_get_tiling(rws, buffer, &microtile, &macrotile);
798
799 /* Enforce a microtiled zbuffer. */
800 if (util_format_is_depth_or_stencil(base->format) &&
801 microtile == R300_BUFFER_LINEAR) {
802 switch (util_format_get_blocksize(base->format)) {
803 case 4:
804 microtile = R300_BUFFER_TILED;
805 break;
806
807 case 2:
808 if (rws->get_value(rws, R300_VID_SQUARE_TILING_SUPPORT))
809 microtile = R300_BUFFER_SQUARETILED;
810 break;
811 }
812 }
813
814 return (struct pipe_resource*)
815 r300_texture_create_object(rscreen, base, microtile, macrotile,
816 stride, size, buffer);
817 }
818
819 /* Not required to implement u_resource_vtbl, consider moving to another file:
820 */
821 struct pipe_surface* r300_get_tex_surface(struct pipe_screen* screen,
822 struct pipe_resource* texture,
823 unsigned face,
824 unsigned level,
825 unsigned zslice,
826 unsigned flags)
827 {
828 struct r300_texture* tex = r300_texture(texture);
829 struct r300_surface* surface = CALLOC_STRUCT(r300_surface);
830
831 if (surface) {
832 uint32_t offset, tile_height;
833
834 pipe_reference_init(&surface->base.reference, 1);
835 pipe_resource_reference(&surface->base.texture, texture);
836 surface->base.format = texture->format;
837 surface->base.width = u_minify(texture->width0, level);
838 surface->base.height = u_minify(texture->height0, level);
839 surface->base.usage = flags;
840 surface->base.zslice = zslice;
841 surface->base.face = face;
842 surface->base.level = level;
843
844 surface->buffer = tex->buffer;
845
846 /* Prefer VRAM if there are multiple domains to choose from. */
847 surface->domain = tex->domain;
848 if (surface->domain & R300_DOMAIN_VRAM)
849 surface->domain &= ~R300_DOMAIN_GTT;
850
851 surface->offset = r300_texture_get_offset(&tex->desc,
852 level, zslice, face);
853 surface->pitch = tex->fb_state.pitch[level];
854 surface->format = tex->fb_state.format;
855
856 /* Parameters for the CBZB clear. */
857 surface->cbzb_allowed = tex->desc.cbzb_allowed[level];
858 surface->cbzb_width = align(surface->base.width, 64);
859
860 /* Height must be aligned to the size of a tile. */
861 tile_height = r300_get_pixel_alignment(tex->desc.b.b.format,
862 tex->desc.b.b.nr_samples,
863 tex->desc.microtile,
864 tex->desc.macrotile[level],
865 DIM_HEIGHT);
866
867 surface->cbzb_height = align((surface->base.height + 1) / 2,
868 tile_height);
869
870 /* Offset must be aligned to 2K and must point at the beginning
871 * of a scanline. */
872 offset = surface->offset +
873 tex->desc.stride_in_bytes[level] * surface->cbzb_height;
874 surface->cbzb_midpoint_offset = offset & ~2047;
875
876 surface->cbzb_pitch = surface->pitch & 0x1ffffc;
877
878 if (util_format_get_blocksizebits(surface->base.format) == 32)
879 surface->cbzb_format = R300_DEPTHFORMAT_24BIT_INT_Z_8BIT_STENCIL;
880 else
881 surface->cbzb_format = R300_DEPTHFORMAT_16BIT_INT_Z;
882
883 SCREEN_DBG(r300_screen(screen), DBG_CBZB,
884 "CBZB Allowed: %s, Dim: %ix%i, Misalignment: %i, Micro: %s, Macro: %s\n",
885 surface->cbzb_allowed ? "YES" : " NO",
886 surface->cbzb_width, surface->cbzb_height,
887 offset & 2047,
888 tex->desc.microtile ? "YES" : " NO",
889 tex->desc.macrotile[level] ? "YES" : " NO");
890 }
891
892 return &surface->base;
893 }
894
895 /* Not required to implement u_resource_vtbl, consider moving to another file:
896 */
897 void r300_tex_surface_destroy(struct pipe_surface* s)
898 {
899 pipe_resource_reference(&s->texture, NULL);
900 FREE(s);
901 }