e99a4630ee4294e40a818ad819c213c08e561a2e
[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 f->format0 |= R300_TX_DEPTH(util_logbase2(pt->depth0) & 0xf);
557 }
558
559 f->format1 = 0;
560 if (pt->target == PIPE_TEXTURE_CUBE) {
561 f->format1 |= R300_TX_FORMAT_CUBIC_MAP;
562 }
563 if (pt->target == PIPE_TEXTURE_3D) {
564 f->format1 |= R300_TX_FORMAT_3D;
565 }
566
567 /* large textures on r500 */
568 if (is_r500)
569 {
570 if (pt->width0 > 2048) {
571 f->format2 |= R500_TXWIDTH_BIT11;
572 }
573 if (pt->height0 > 2048) {
574 f->format2 |= R500_TXHEIGHT_BIT11;
575 }
576 }
577
578 f->tile_config = R300_TXO_MACRO_TILE(tex->desc.macrotile[0]) |
579 R300_TXO_MICRO_TILE(tex->desc.microtile);
580 }
581
582 static void r300_texture_setup_fb_state(struct r300_screen* screen,
583 struct r300_texture* tex)
584 {
585 unsigned i;
586
587 /* Set framebuffer state. */
588 if (util_format_is_depth_or_stencil(tex->desc.b.b.format)) {
589 for (i = 0; i <= tex->desc.b.b.last_level; i++) {
590 tex->fb_state.pitch[i] =
591 tex->desc.stride_in_pixels[i] |
592 R300_DEPTHMACROTILE(tex->desc.macrotile[i]) |
593 R300_DEPTHMICROTILE(tex->desc.microtile);
594 }
595 tex->fb_state.format = r300_translate_zsformat(tex->desc.b.b.format);
596 } else {
597 for (i = 0; i <= tex->desc.b.b.last_level; i++) {
598 tex->fb_state.pitch[i] =
599 tex->desc.stride_in_pixels[i] |
600 r300_translate_colorformat(tex->desc.b.b.format) |
601 R300_COLOR_TILE(tex->desc.macrotile[i]) |
602 R300_COLOR_MICROTILE(tex->desc.microtile);
603 }
604 tex->fb_state.format = r300_translate_out_fmt(tex->desc.b.b.format);
605 }
606 }
607
608 void r300_texture_reinterpret_format(struct pipe_screen *screen,
609 struct pipe_resource *tex,
610 enum pipe_format new_format)
611 {
612 struct r300_screen *r300screen = r300_screen(screen);
613
614 SCREEN_DBG(r300screen, DBG_TEX,
615 "r300: texture_reinterpret_format: %s -> %s\n",
616 util_format_short_name(tex->format),
617 util_format_short_name(new_format));
618
619 tex->format = new_format;
620
621 r300_texture_setup_fb_state(r300_screen(screen), r300_texture(tex));
622 }
623
624 static unsigned r300_texture_is_referenced(struct pipe_context *context,
625 struct pipe_resource *texture,
626 unsigned face, unsigned level)
627 {
628 struct r300_context *r300 = r300_context(context);
629 struct r300_texture *rtex = (struct r300_texture *)texture;
630
631 if (r300->rws->cs_is_buffer_referenced(r300->cs,
632 rtex->buffer, R300_REF_CS))
633 return PIPE_REFERENCED_FOR_READ | PIPE_REFERENCED_FOR_WRITE;
634
635 return PIPE_UNREFERENCED;
636 }
637
638 static void r300_texture_destroy(struct pipe_screen *screen,
639 struct pipe_resource* texture)
640 {
641 struct r300_texture* tex = (struct r300_texture*)texture;
642 struct r300_winsys_screen *rws = (struct r300_winsys_screen *)texture->screen->winsys;
643
644 rws->buffer_reference(rws, &tex->buffer, NULL);
645 FREE(tex);
646 }
647
648 static boolean r300_texture_get_handle(struct pipe_screen* screen,
649 struct pipe_resource *texture,
650 struct winsys_handle *whandle)
651 {
652 struct r300_winsys_screen *rws = (struct r300_winsys_screen *)screen->winsys;
653 struct r300_texture* tex = (struct r300_texture*)texture;
654
655 if (!tex) {
656 return FALSE;
657 }
658
659 return rws->buffer_get_handle(rws, tex->buffer,
660 tex->desc.stride_in_bytes[0], whandle);
661 }
662
663 struct u_resource_vtbl r300_texture_vtbl =
664 {
665 r300_texture_get_handle, /* get_handle */
666 r300_texture_destroy, /* resource_destroy */
667 r300_texture_is_referenced, /* is_resource_referenced */
668 r300_texture_get_transfer, /* get_transfer */
669 r300_texture_transfer_destroy, /* transfer_destroy */
670 r300_texture_transfer_map, /* transfer_map */
671 u_default_transfer_flush_region, /* transfer_flush_region */
672 r300_texture_transfer_unmap, /* transfer_unmap */
673 u_default_transfer_inline_write /* transfer_inline_write */
674 };
675
676 /* The common texture constructor. */
677 static struct r300_texture*
678 r300_texture_create_object(struct r300_screen *rscreen,
679 const struct pipe_resource *base,
680 enum r300_buffer_tiling microtile,
681 enum r300_buffer_tiling macrotile,
682 unsigned stride_in_bytes_override,
683 unsigned max_buffer_size,
684 struct r300_winsys_buffer *buffer)
685 {
686 struct r300_winsys_screen *rws = rscreen->rws;
687 struct r300_texture *tex = CALLOC_STRUCT(r300_texture);
688 if (!tex) {
689 if (buffer)
690 rws->buffer_reference(rws, &buffer, NULL);
691 return NULL;
692 }
693
694 /* Initialize the descriptor. */
695 if (!r300_texture_desc_init(rscreen, &tex->desc, base,
696 microtile, macrotile,
697 stride_in_bytes_override,
698 max_buffer_size)) {
699 if (buffer)
700 rws->buffer_reference(rws, &buffer, NULL);
701 FREE(tex);
702 return NULL;
703 }
704 /* Initialize the hardware state. */
705 r300_texture_setup_immutable_state(rscreen, tex);
706 r300_texture_setup_fb_state(rscreen, tex);
707
708 tex->desc.b.vtbl = &r300_texture_vtbl;
709 pipe_reference_init(&tex->desc.b.b.reference, 1);
710 tex->domain = base->flags & R300_RESOURCE_FLAG_TRANSFER ?
711 R300_DOMAIN_GTT :
712 R300_DOMAIN_VRAM | R300_DOMAIN_GTT;
713 tex->buffer = buffer;
714
715 /* Create the backing buffer if needed. */
716 if (!tex->buffer) {
717 tex->buffer = rws->buffer_create(rws, tex->desc.size_in_bytes, 2048,
718 base->bind, base->usage, tex->domain);
719
720 if (!tex->buffer) {
721 FREE(tex);
722 return NULL;
723 }
724 }
725
726 rws->buffer_set_tiling(rws, tex->buffer,
727 tex->desc.microtile, tex->desc.macrotile[0],
728 tex->desc.stride_in_bytes[0]);
729
730 return tex;
731 }
732
733 /* Create a new texture. */
734 struct pipe_resource *r300_texture_create(struct pipe_screen *screen,
735 const struct pipe_resource *base)
736 {
737 struct r300_screen *rscreen = r300_screen(screen);
738 enum r300_buffer_tiling microtile, macrotile;
739
740 /* Refuse to create a texture with size 0. */
741 if (!base->width0 ||
742 (!base->height0 && (base->target == PIPE_TEXTURE_2D ||
743 base->target == PIPE_TEXTURE_CUBE)) ||
744 (!base->depth0 && base->target == PIPE_TEXTURE_3D)) {
745 fprintf(stderr, "r300: texture_create: "
746 "Got invalid texture dimensions: %ix%ix%i\n",
747 base->width0, base->height0, base->depth0);
748 return NULL;
749 }
750
751 if ((base->flags & R300_RESOURCE_FLAG_TRANSFER) ||
752 (base->bind & PIPE_BIND_SCANOUT)) {
753 microtile = R300_BUFFER_LINEAR;
754 macrotile = R300_BUFFER_LINEAR;
755 } else {
756 microtile = R300_BUFFER_SELECT_LAYOUT;
757 macrotile = R300_BUFFER_SELECT_LAYOUT;
758 }
759
760 return (struct pipe_resource*)
761 r300_texture_create_object(rscreen, base, microtile, macrotile,
762 0, 0, NULL);
763 }
764
765 struct pipe_resource *r300_texture_from_handle(struct pipe_screen *screen,
766 const struct pipe_resource *base,
767 struct winsys_handle *whandle)
768 {
769 struct r300_winsys_screen *rws = (struct r300_winsys_screen*)screen->winsys;
770 struct r300_screen *rscreen = r300_screen(screen);
771 struct r300_winsys_buffer *buffer;
772 enum r300_buffer_tiling microtile, macrotile;
773 unsigned stride, size;
774
775 /* Support only 2D textures without mipmaps */
776 if (base->target != PIPE_TEXTURE_2D ||
777 base->depth0 != 1 ||
778 base->last_level != 0) {
779 return NULL;
780 }
781
782 buffer = rws->buffer_from_handle(rws, whandle, &stride, &size);
783 if (!buffer)
784 return NULL;
785
786 rws->buffer_get_tiling(rws, buffer, &microtile, &macrotile);
787
788 /* Enforce a microtiled zbuffer. */
789 if (util_format_is_depth_or_stencil(base->format) &&
790 microtile == R300_BUFFER_LINEAR) {
791 switch (util_format_get_blocksize(base->format)) {
792 case 4:
793 microtile = R300_BUFFER_TILED;
794 break;
795
796 case 2:
797 if (rws->get_value(rws, R300_VID_SQUARE_TILING_SUPPORT))
798 microtile = R300_BUFFER_SQUARETILED;
799 break;
800 }
801 }
802
803 return (struct pipe_resource*)
804 r300_texture_create_object(rscreen, base, microtile, macrotile,
805 stride, size, buffer);
806 }
807
808 /* Not required to implement u_resource_vtbl, consider moving to another file:
809 */
810 struct pipe_surface* r300_get_tex_surface(struct pipe_screen* screen,
811 struct pipe_resource* texture,
812 unsigned face,
813 unsigned level,
814 unsigned zslice,
815 unsigned flags)
816 {
817 struct r300_texture* tex = r300_texture(texture);
818 struct r300_surface* surface = CALLOC_STRUCT(r300_surface);
819
820 if (surface) {
821 uint32_t offset, tile_height;
822
823 pipe_reference_init(&surface->base.reference, 1);
824 pipe_resource_reference(&surface->base.texture, texture);
825 surface->base.format = texture->format;
826 surface->base.width = u_minify(texture->width0, level);
827 surface->base.height = u_minify(texture->height0, level);
828 surface->base.usage = flags;
829 surface->base.zslice = zslice;
830 surface->base.face = face;
831 surface->base.level = level;
832
833 surface->buffer = tex->buffer;
834
835 /* Prefer VRAM if there are multiple domains to choose from. */
836 surface->domain = tex->domain;
837 if (surface->domain & R300_DOMAIN_VRAM)
838 surface->domain &= ~R300_DOMAIN_GTT;
839
840 surface->offset = r300_texture_get_offset(&tex->desc,
841 level, zslice, face);
842 surface->pitch = tex->fb_state.pitch[level];
843 surface->format = tex->fb_state.format;
844
845 /* Parameters for the CBZB clear. */
846 surface->cbzb_allowed = tex->desc.cbzb_allowed[level];
847 surface->cbzb_width = align(surface->base.width, 64);
848
849 /* Height must be aligned to the size of a tile. */
850 tile_height = r300_get_pixel_alignment(tex->desc.b.b.format,
851 tex->desc.b.b.nr_samples,
852 tex->desc.microtile,
853 tex->desc.macrotile[level],
854 DIM_HEIGHT);
855
856 surface->cbzb_height = align((surface->base.height + 1) / 2,
857 tile_height);
858
859 /* Offset must be aligned to 2K and must point at the beginning
860 * of a scanline. */
861 offset = surface->offset +
862 tex->desc.stride_in_bytes[level] * surface->cbzb_height;
863 surface->cbzb_midpoint_offset = offset & ~2047;
864
865 surface->cbzb_pitch = surface->pitch & 0x1ffffc;
866
867 if (util_format_get_blocksizebits(surface->base.format) == 32)
868 surface->cbzb_format = R300_DEPTHFORMAT_24BIT_INT_Z_8BIT_STENCIL;
869 else
870 surface->cbzb_format = R300_DEPTHFORMAT_16BIT_INT_Z;
871
872 SCREEN_DBG(r300_screen(screen), DBG_CBZB,
873 "CBZB Dim: %ix%i, Misalignment: %i, Macro: %s\n",
874 surface->cbzb_width, surface->cbzb_height,
875 offset & 2047,
876 tex->desc.macrotile[level] ? "YES" : " NO");
877 }
878
879 return &surface->base;
880 }
881
882 /* Not required to implement u_resource_vtbl, consider moving to another file:
883 */
884 void r300_tex_surface_destroy(struct pipe_surface* s)
885 {
886 pipe_resource_reference(&s->texture, NULL);
887 FREE(s);
888 }