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