i915g: Change state code in vbuf code
[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_transfer.h"
30 #include "r300_screen.h"
31 #include "r300_winsys.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 #include "state_tracker/drm_api.h"
40
41 enum r300_dim {
42 DIM_WIDTH = 0,
43 DIM_HEIGHT = 1
44 };
45
46 unsigned r300_get_swizzle_combined(const unsigned char *swizzle_format,
47 const unsigned char *swizzle_view)
48 {
49 unsigned i;
50 unsigned char swizzle[4];
51 unsigned result = 0;
52 const uint32_t swizzle_shift[4] = {
53 R300_TX_FORMAT_R_SHIFT,
54 R300_TX_FORMAT_G_SHIFT,
55 R300_TX_FORMAT_B_SHIFT,
56 R300_TX_FORMAT_A_SHIFT
57 };
58 const uint32_t swizzle_bit[4] = {
59 R300_TX_FORMAT_X,
60 R300_TX_FORMAT_Y,
61 R300_TX_FORMAT_Z,
62 R300_TX_FORMAT_W
63 };
64
65 if (swizzle_view) {
66 /* Combine two sets of swizzles. */
67 for (i = 0; i < 4; i++) {
68 swizzle[i] = swizzle_view[i] <= UTIL_FORMAT_SWIZZLE_W ?
69 swizzle_format[swizzle_view[i]] : swizzle_view[i];
70 }
71 } else {
72 memcpy(swizzle, swizzle_format, 4);
73 }
74
75 /* Get swizzle. */
76 for (i = 0; i < 4; i++) {
77 switch (swizzle[i]) {
78 case UTIL_FORMAT_SWIZZLE_Y:
79 result |= swizzle_bit[1] << swizzle_shift[i];
80 break;
81 case UTIL_FORMAT_SWIZZLE_Z:
82 result |= swizzle_bit[2] << swizzle_shift[i];
83 break;
84 case UTIL_FORMAT_SWIZZLE_W:
85 result |= swizzle_bit[3] << swizzle_shift[i];
86 break;
87 case UTIL_FORMAT_SWIZZLE_0:
88 result |= R300_TX_FORMAT_ZERO << swizzle_shift[i];
89 break;
90 case UTIL_FORMAT_SWIZZLE_1:
91 result |= R300_TX_FORMAT_ONE << swizzle_shift[i];
92 break;
93 default: /* UTIL_FORMAT_SWIZZLE_X */
94 result |= swizzle_bit[0] << swizzle_shift[i];
95 }
96 }
97 return result;
98 }
99
100 /* Translate a pipe_format into a useful texture format for sampling.
101 *
102 * Some special formats are translated directly using R300_EASY_TX_FORMAT,
103 * but the majority of them is translated in a generic way, automatically
104 * supporting all the formats hw can support.
105 *
106 * R300_EASY_TX_FORMAT swizzles the texture.
107 * Note the signature of R300_EASY_TX_FORMAT:
108 * R300_EASY_TX_FORMAT(B, G, R, A, FORMAT);
109 *
110 * The FORMAT specifies how the texture sampler will treat the texture, and
111 * makes available X, Y, Z, W, ZERO, and ONE for swizzling. */
112 uint32_t r300_translate_texformat(enum pipe_format format,
113 const unsigned char *swizzle_view)
114 {
115 uint32_t result = 0;
116 const struct util_format_description *desc;
117 unsigned i;
118 boolean uniform = TRUE;
119 const uint32_t sign_bit[4] = {
120 R300_TX_FORMAT_SIGNED_X,
121 R300_TX_FORMAT_SIGNED_Y,
122 R300_TX_FORMAT_SIGNED_Z,
123 R300_TX_FORMAT_SIGNED_W,
124 };
125
126 desc = util_format_description(format);
127
128 /* Colorspace (return non-RGB formats directly). */
129 switch (desc->colorspace) {
130 /* Depth stencil formats.
131 * Swizzles are added in r300_merge_textures_and_samplers. */
132 case UTIL_FORMAT_COLORSPACE_ZS:
133 switch (format) {
134 case PIPE_FORMAT_Z16_UNORM:
135 return R300_TX_FORMAT_X16;
136 case PIPE_FORMAT_X8Z24_UNORM:
137 case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
138 return R500_TX_FORMAT_Y8X24;
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) != ~0;
542 }
543
544 static void r300_texture_setup_immutable_state(struct r300_screen* screen,
545 struct r300_texture* tex)
546 {
547 struct r300_texture_format_state* f = &tex->tx_format;
548 struct pipe_resource *pt = &tex->b.b;
549 boolean is_r500 = screen->caps.is_r500;
550
551 /* Set sampler state. */
552 f->format0 = R300_TX_WIDTH((pt->width0 - 1) & 0x7ff) |
553 R300_TX_HEIGHT((pt->height0 - 1) & 0x7ff);
554
555 if (tex->uses_pitch) {
556 /* rectangles love this */
557 f->format0 |= R300_TX_PITCH_EN;
558 f->format2 = (tex->hwpitch[0] - 1) & 0x1fff;
559 } else {
560 /* power of two textures (3D, mipmaps, and no pitch) */
561 f->format0 |= R300_TX_DEPTH(util_logbase2(pt->depth0) & 0xf);
562 }
563
564 f->format1 = 0;
565 if (pt->target == PIPE_TEXTURE_CUBE) {
566 f->format1 |= R300_TX_FORMAT_CUBIC_MAP;
567 }
568 if (pt->target == PIPE_TEXTURE_3D) {
569 f->format1 |= R300_TX_FORMAT_3D;
570 }
571
572 /* large textures on r500 */
573 if (is_r500)
574 {
575 if (pt->width0 > 2048) {
576 f->format2 |= R500_TXWIDTH_BIT11;
577 }
578 if (pt->height0 > 2048) {
579 f->format2 |= R500_TXHEIGHT_BIT11;
580 }
581 }
582
583 f->tile_config = R300_TXO_MACRO_TILE(tex->macrotile) |
584 R300_TXO_MICRO_TILE(tex->microtile);
585 }
586
587 static void r300_texture_setup_fb_state(struct r300_screen* screen,
588 struct r300_texture* tex)
589 {
590 unsigned i;
591
592 /* Set framebuffer state. */
593 if (util_format_is_depth_or_stencil(tex->b.b.format)) {
594 for (i = 0; i <= tex->b.b.last_level; i++) {
595 tex->fb_state.pitch[i] =
596 tex->hwpitch[i] |
597 R300_DEPTHMACROTILE(tex->mip_macrotile[i]) |
598 R300_DEPTHMICROTILE(tex->microtile);
599 }
600 tex->fb_state.format = r300_translate_zsformat(tex->b.b.format);
601 } else {
602 for (i = 0; i <= tex->b.b.last_level; i++) {
603 tex->fb_state.pitch[i] =
604 tex->hwpitch[i] |
605 r300_translate_colorformat(tex->b.b.format) |
606 R300_COLOR_TILE(tex->mip_macrotile[i]) |
607 R300_COLOR_MICROTILE(tex->microtile);
608 }
609 tex->fb_state.format = r300_translate_out_fmt(tex->b.b.format);
610 }
611 }
612
613 void r300_texture_reinterpret_format(struct pipe_screen *screen,
614 struct pipe_resource *tex,
615 enum pipe_format new_format)
616 {
617 struct r300_screen *r300screen = r300_screen(screen);
618
619 SCREEN_DBG(r300screen, DBG_TEX,
620 "r300: texture_reinterpret_format: %s -> %s\n",
621 util_format_short_name(tex->format),
622 util_format_short_name(new_format));
623
624 tex->format = new_format;
625
626 r300_texture_setup_fb_state(r300_screen(screen), r300_texture(tex));
627 }
628
629 unsigned r300_texture_get_offset(struct r300_texture* tex, unsigned level,
630 unsigned zslice, unsigned face)
631 {
632 unsigned offset = tex->offset[level];
633
634 switch (tex->b.b.target) {
635 case PIPE_TEXTURE_3D:
636 assert(face == 0);
637 return offset + zslice * tex->layer_size[level];
638
639 case PIPE_TEXTURE_CUBE:
640 assert(zslice == 0);
641 return offset + face * tex->layer_size[level];
642
643 default:
644 assert(zslice == 0 && face == 0);
645 return offset;
646 }
647 }
648
649 /* Returns the number of pixels that the texture should be aligned to
650 * in the given dimension. */
651 static unsigned r300_get_pixel_alignment(struct r300_texture *tex,
652 enum r300_buffer_tiling macrotile,
653 enum r300_dim dim)
654 {
655 static const unsigned table[2][5][3][2] =
656 {
657 {
658 /* Macro: linear linear linear
659 Micro: linear tiled square-tiled */
660 {{ 32, 1}, { 8, 4}, { 0, 0}}, /* 8 bits per pixel */
661 {{ 16, 1}, { 8, 2}, { 4, 4}}, /* 16 bits per pixel */
662 {{ 8, 1}, { 4, 2}, { 0, 0}}, /* 32 bits per pixel */
663 {{ 4, 1}, { 0, 0}, { 2, 2}}, /* 64 bits per pixel */
664 {{ 2, 1}, { 0, 0}, { 0, 0}} /* 128 bits per pixel */
665 },
666 {
667 /* Macro: tiled tiled tiled
668 Micro: linear tiled square-tiled */
669 {{256, 8}, {64, 32}, { 0, 0}}, /* 8 bits per pixel */
670 {{128, 8}, {64, 16}, {32, 32}}, /* 16 bits per pixel */
671 {{ 64, 8}, {32, 16}, { 0, 0}}, /* 32 bits per pixel */
672 {{ 32, 8}, { 0, 0}, {16, 16}}, /* 64 bits per pixel */
673 {{ 16, 8}, { 0, 0}, { 0, 0}} /* 128 bits per pixel */
674 }
675 };
676 static const unsigned aa_block[2] = {4, 8};
677 unsigned res = 0;
678 unsigned pixsize = util_format_get_blocksize(tex->b.b.format);
679
680 assert(macrotile <= R300_BUFFER_TILED);
681 assert(tex->microtile <= R300_BUFFER_SQUARETILED);
682 assert(pixsize <= 16);
683 assert(dim <= DIM_HEIGHT);
684
685 if (tex->b.b.nr_samples > 1) {
686 /* Multisampled textures have their own alignment scheme. */
687 if (pixsize == 4)
688 res = aa_block[dim];
689 } else {
690 /* Standard alignment. */
691 res = table[macrotile][util_logbase2(pixsize)][tex->microtile][dim];
692 }
693
694 assert(res);
695 return res;
696 }
697
698 /* Return true if macrotiling should be enabled on the miplevel. */
699 static boolean r300_texture_macro_switch(struct r300_texture *tex,
700 unsigned level,
701 boolean rv350_mode,
702 enum r300_dim dim)
703 {
704 unsigned tile, texdim;
705
706 tile = r300_get_pixel_alignment(tex, R300_BUFFER_TILED, dim);
707 if (dim == DIM_WIDTH) {
708 texdim = u_minify(tex->b.b.width0, level);
709 } else {
710 texdim = u_minify(tex->b.b.height0, level);
711 }
712
713 /* See TX_FILTER1_n.MACRO_SWITCH. */
714 if (rv350_mode) {
715 return texdim >= tile;
716 } else {
717 return texdim > tile;
718 }
719 }
720
721 /**
722 * Return the stride, in bytes, of the texture images of the given texture
723 * at the given level.
724 */
725 unsigned r300_texture_get_stride(struct r300_screen* screen,
726 struct r300_texture* tex, unsigned level)
727 {
728 unsigned tile_width, width, stride;
729
730 if (tex->stride_override)
731 return tex->stride_override;
732
733 /* Check the level. */
734 if (level > tex->b.b.last_level) {
735 SCREEN_DBG(screen, DBG_TEX, "%s: level (%u) > last_level (%u)\n",
736 __FUNCTION__, level, tex->b.b.last_level);
737 return 0;
738 }
739
740 width = u_minify(tex->b.b.width0, level);
741
742 if (util_format_is_plain(tex->b.b.format)) {
743 tile_width = r300_get_pixel_alignment(tex, tex->mip_macrotile[level],
744 DIM_WIDTH);
745 width = align(width, tile_width);
746
747 stride = util_format_get_stride(tex->b.b.format, width);
748
749 /* Some IGPs need a minimum stride of 64 bytes, hmm...
750 * This doesn't seem to apply to tiled textures, according to r300c. */
751 if (!tex->microtile && !tex->mip_macrotile[level] &&
752 (screen->caps.family == CHIP_FAMILY_RS600 ||
753 screen->caps.family == CHIP_FAMILY_RS690 ||
754 screen->caps.family == CHIP_FAMILY_RS740)) {
755 return stride < 64 ? 64 : stride;
756 }
757
758 /* The alignment to 32 bytes is sort of implied by the layout... */
759 return stride;
760 } else {
761 return align(util_format_get_stride(tex->b.b.format, width), 32);
762 }
763 }
764
765 static unsigned r300_texture_get_nblocksy(struct r300_texture* tex,
766 unsigned level)
767 {
768 unsigned height, tile_height;
769
770 height = u_minify(tex->b.b.height0, level);
771
772 if (util_format_is_plain(tex->b.b.format)) {
773 tile_height = r300_get_pixel_alignment(tex, tex->mip_macrotile[level],
774 DIM_HEIGHT);
775 height = align(height, tile_height);
776
777 /* This is needed for the kernel checker, unfortunately. */
778 height = util_next_power_of_two(height);
779 }
780
781 return util_format_get_nblocksy(tex->b.b.format, height);
782 }
783
784 static void r300_texture_3d_fix_mipmapping(struct r300_screen *screen,
785 struct r300_texture *tex)
786 {
787 /* The kernels <= 2.6.34-rc4 compute the size of mipmapped 3D textures
788 * incorrectly. This is a workaround to prevent CS from being rejected. */
789
790 unsigned i, size;
791
792 if (!screen->rws->get_value(screen->rws, R300_VID_DRM_2_3_0) &&
793 tex->b.b.target == PIPE_TEXTURE_3D &&
794 tex->b.b.last_level > 0) {
795 size = 0;
796
797 for (i = 0; i <= tex->b.b.last_level; i++) {
798 size += r300_texture_get_stride(screen, tex, i) *
799 r300_texture_get_nblocksy(tex, i);
800 }
801
802 size *= tex->b.b.depth0;
803 tex->size = size;
804 }
805 }
806
807 static void r300_setup_miptree(struct r300_screen* screen,
808 struct r300_texture* tex)
809 {
810 struct pipe_resource* base = &tex->b.b;
811 unsigned stride, size, layer_size, nblocksy, i;
812 boolean rv350_mode = screen->caps.is_rv350;
813
814 SCREEN_DBG(screen, DBG_TEXALLOC,
815 "r300: Making miptree for texture, format %s\n",
816 util_format_short_name(base->format));
817
818 for (i = 0; i <= base->last_level; i++) {
819 /* Let's see if this miplevel can be macrotiled. */
820 tex->mip_macrotile[i] =
821 (tex->macrotile == R300_BUFFER_TILED &&
822 r300_texture_macro_switch(tex, i, rv350_mode, DIM_WIDTH) &&
823 r300_texture_macro_switch(tex, i, rv350_mode, DIM_HEIGHT)) ?
824 R300_BUFFER_TILED : R300_BUFFER_LINEAR;
825
826 stride = r300_texture_get_stride(screen, tex, i);
827 nblocksy = r300_texture_get_nblocksy(tex, i);
828 layer_size = stride * nblocksy;
829
830 if (base->nr_samples) {
831 layer_size *= base->nr_samples;
832 }
833
834 if (base->target == PIPE_TEXTURE_CUBE)
835 size = layer_size * 6;
836 else
837 size = layer_size * u_minify(base->depth0, i);
838
839 tex->offset[i] = tex->size;
840 tex->size = tex->offset[i] + size;
841 tex->layer_size[i] = layer_size;
842 tex->pitch[i] = stride / util_format_get_blocksize(base->format);
843 tex->hwpitch[i] =
844 tex->pitch[i] * util_format_get_blockwidth(base->format);
845
846 SCREEN_DBG(screen, DBG_TEXALLOC, "r300: Texture miptree: Level %d "
847 "(%dx%dx%d px, pitch %d bytes) %d bytes total, macrotiled %s\n",
848 i, u_minify(base->width0, i), u_minify(base->height0, i),
849 u_minify(base->depth0, i), stride, tex->size,
850 tex->mip_macrotile[i] ? "TRUE" : "FALSE");
851 }
852 }
853
854 static void r300_setup_flags(struct r300_texture* tex)
855 {
856 tex->uses_pitch = !util_is_power_of_two(tex->b.b.width0) ||
857 !util_is_power_of_two(tex->b.b.height0) ||
858 tex->stride_override;
859 }
860
861 static void r300_setup_tiling(struct pipe_screen *screen,
862 struct r300_texture *tex)
863 {
864 struct r300_winsys_screen *rws = (struct r300_winsys_screen *)screen->winsys;
865 enum pipe_format format = tex->b.b.format;
866 boolean rv350_mode = r300_screen(screen)->caps.is_rv350;
867 boolean is_zb = util_format_is_depth_or_stencil(format);
868 boolean dbg_no_tiling = SCREEN_DBG_ON(r300_screen(screen), DBG_NO_TILING);
869
870 if (!util_format_is_plain(format)) {
871 return;
872 }
873
874 /* If height == 1, disable microtiling except for zbuffer. */
875 if (!is_zb && (tex->b.b.height0 == 1 || dbg_no_tiling)) {
876 return;
877 }
878
879 /* Set microtiling. */
880 switch (util_format_get_blocksize(format)) {
881 case 1:
882 case 4:
883 tex->microtile = R300_BUFFER_TILED;
884 break;
885
886 case 2:
887 case 8:
888 if (rws->get_value(rws, R300_VID_SQUARE_TILING_SUPPORT)) {
889 tex->microtile = R300_BUFFER_SQUARETILED;
890 }
891 break;
892 }
893
894 if (dbg_no_tiling) {
895 return;
896 }
897
898 /* Set macrotiling. */
899 if (r300_texture_macro_switch(tex, 0, rv350_mode, DIM_WIDTH) &&
900 r300_texture_macro_switch(tex, 0, rv350_mode, DIM_HEIGHT)) {
901 tex->macrotile = R300_BUFFER_TILED;
902 }
903 }
904
905 static unsigned r300_texture_is_referenced(struct pipe_context *context,
906 struct pipe_resource *texture,
907 unsigned face, unsigned level)
908 {
909 struct r300_context *r300 = r300_context(context);
910 struct r300_texture *rtex = (struct r300_texture *)texture;
911
912 if (r300->rws->is_buffer_referenced(r300->rws, rtex->buffer, R300_REF_CS))
913 return PIPE_REFERENCED_FOR_READ | PIPE_REFERENCED_FOR_WRITE;
914
915 return PIPE_UNREFERENCED;
916 }
917
918 static void r300_texture_destroy(struct pipe_screen *screen,
919 struct pipe_resource* texture)
920 {
921 struct r300_texture* tex = (struct r300_texture*)texture;
922 struct r300_winsys_screen *rws = (struct r300_winsys_screen *)texture->screen->winsys;
923
924 rws->buffer_reference(rws, &tex->buffer, NULL);
925 FREE(tex);
926 }
927
928 static boolean r300_texture_get_handle(struct pipe_screen* screen,
929 struct pipe_resource *texture,
930 struct winsys_handle *whandle)
931 {
932 struct r300_winsys_screen *rws = (struct r300_winsys_screen *)screen->winsys;
933 struct r300_texture* tex = (struct r300_texture*)texture;
934
935 if (!tex) {
936 return FALSE;
937 }
938
939 whandle->stride = r300_texture_get_stride(r300_screen(screen), tex, 0);
940
941 return rws->buffer_get_handle(rws, tex->buffer, whandle);
942 }
943
944 struct u_resource_vtbl r300_texture_vtbl =
945 {
946 r300_texture_get_handle, /* get_handle */
947 r300_texture_destroy, /* resource_destroy */
948 r300_texture_is_referenced, /* is_resource_referenced */
949 r300_texture_get_transfer, /* get_transfer */
950 r300_texture_transfer_destroy, /* transfer_destroy */
951 r300_texture_transfer_map, /* transfer_map */
952 u_default_transfer_flush_region, /* transfer_flush_region */
953 r300_texture_transfer_unmap, /* transfer_unmap */
954 u_default_transfer_inline_write /* transfer_inline_write */
955 };
956
957 /* Create a new texture. */
958 struct pipe_resource* r300_texture_create(struct pipe_screen* screen,
959 const struct pipe_resource* base)
960 {
961 struct r300_texture* tex = CALLOC_STRUCT(r300_texture);
962 struct r300_screen* rscreen = r300_screen(screen);
963 struct r300_winsys_screen *rws = (struct r300_winsys_screen *)screen->winsys;
964
965 if (!tex) {
966 return NULL;
967 }
968
969 /* Refuse to create a texture with size 0. */
970 if (!base->width0 ||
971 (!base->height0 && (base->target == PIPE_TEXTURE_2D ||
972 base->target == PIPE_TEXTURE_CUBE)) ||
973 (!base->depth0 && base->target == PIPE_TEXTURE_3D)) {
974 fprintf(stderr, "r300: texture_create: "
975 "Got invalid texture dimensions: %ix%ix%i\n",
976 base->width0, base->height0, base->depth0);
977 FREE(tex);
978 return NULL;
979 }
980
981 tex->b.b = *base;
982 tex->b.vtbl = &r300_texture_vtbl;
983 pipe_reference_init(&tex->b.b.reference, 1);
984 tex->b.b.screen = screen;
985
986 r300_setup_flags(tex);
987 if (!(base->flags & R300_RESOURCE_FLAG_TRANSFER) &&
988 !(base->bind & PIPE_BIND_SCANOUT)) {
989 r300_setup_tiling(screen, tex);
990 }
991 r300_setup_miptree(rscreen, tex);
992 r300_texture_3d_fix_mipmapping(rscreen, tex);
993 r300_texture_setup_immutable_state(rscreen, tex);
994 r300_texture_setup_fb_state(rscreen, tex);
995
996 SCREEN_DBG(rscreen, DBG_TEX,
997 "r300: texture_create: Macro: %s, Micro: %s, Pitch: %i, "
998 "Dim: %ix%ix%i, LastLevel: %i, Size: %i, Format: %s\n",
999 tex->macrotile ? "YES" : " NO",
1000 tex->microtile ? "YES" : " NO",
1001 tex->hwpitch[0],
1002 base->width0, base->height0, base->depth0, base->last_level,
1003 tex->size,
1004 util_format_short_name(base->format));
1005
1006 tex->domain = base->flags & R300_RESOURCE_FLAG_TRANSFER ? R300_DOMAIN_GTT :
1007 R300_DOMAIN_VRAM;
1008
1009 tex->buffer = rws->buffer_create(rws, 2048, base->bind, tex->domain,
1010 tex->size);
1011
1012 if (!tex->buffer) {
1013 FREE(tex);
1014 return NULL;
1015 }
1016
1017 rws->buffer_set_tiling(rws, tex->buffer,
1018 tex->pitch[0] * util_format_get_blocksize(tex->b.b.format),
1019 tex->microtile,
1020 tex->macrotile);
1021
1022 return (struct pipe_resource*)tex;
1023 }
1024
1025 /* Not required to implement u_resource_vtbl, consider moving to another file:
1026 */
1027 struct pipe_surface* r300_get_tex_surface(struct pipe_screen* screen,
1028 struct pipe_resource* texture,
1029 unsigned face,
1030 unsigned level,
1031 unsigned zslice,
1032 unsigned flags)
1033 {
1034 struct r300_texture* tex = r300_texture(texture);
1035 struct r300_surface* surface = CALLOC_STRUCT(r300_surface);
1036
1037 if (surface) {
1038 pipe_reference_init(&surface->base.reference, 1);
1039 pipe_resource_reference(&surface->base.texture, texture);
1040 surface->base.format = texture->format;
1041 surface->base.width = u_minify(texture->width0, level);
1042 surface->base.height = u_minify(texture->height0, level);
1043 surface->base.usage = flags;
1044 surface->base.zslice = zslice;
1045 surface->base.face = face;
1046 surface->base.level = level;
1047
1048 surface->buffer = tex->buffer;
1049 surface->domain = tex->domain;
1050 surface->offset = r300_texture_get_offset(tex, level, zslice, face);
1051 surface->pitch = tex->fb_state.pitch[level];
1052 surface->format = tex->fb_state.format;
1053 }
1054
1055 return &surface->base;
1056 }
1057
1058 /* Not required to implement u_resource_vtbl, consider moving to another file:
1059 */
1060 void r300_tex_surface_destroy(struct pipe_surface* s)
1061 {
1062 pipe_resource_reference(&s->texture, NULL);
1063 FREE(s);
1064 }
1065
1066 struct pipe_resource*
1067 r300_texture_from_handle(struct pipe_screen* screen,
1068 const struct pipe_resource* base,
1069 struct winsys_handle *whandle)
1070 {
1071 struct r300_winsys_screen *rws = (struct r300_winsys_screen*)screen->winsys;
1072 struct r300_screen* rscreen = r300_screen(screen);
1073 struct r300_winsys_buffer *buffer;
1074 struct r300_texture* tex;
1075 boolean override_zb_flags;
1076
1077 /* Support only 2D textures without mipmaps */
1078 if (base->target != PIPE_TEXTURE_2D ||
1079 base->depth0 != 1 ||
1080 base->last_level != 0) {
1081 return NULL;
1082 }
1083
1084 buffer = rws->buffer_from_handle(rws, whandle->handle);
1085 if (!buffer) {
1086 return NULL;
1087 }
1088
1089 tex = CALLOC_STRUCT(r300_texture);
1090 if (!tex) {
1091 return NULL;
1092 }
1093
1094 tex->b.b = *base;
1095 tex->b.vtbl = &r300_texture_vtbl;
1096 pipe_reference_init(&tex->b.b.reference, 1);
1097 tex->b.b.screen = screen;
1098 tex->domain = R300_DOMAIN_VRAM;
1099
1100 tex->stride_override = whandle->stride;
1101
1102 /* one ref already taken */
1103 tex->buffer = buffer;
1104
1105 rws->buffer_get_tiling(rws, buffer, &tex->microtile, &tex->macrotile);
1106 r300_setup_flags(tex);
1107 SCREEN_DBG(rscreen, DBG_TEX,
1108 "r300: texture_from_handle: Macro: %s, Micro: %s, "
1109 "Pitch: % 4i, Dim: %ix%i, Format: %s\n",
1110 tex->macrotile ? "YES" : " NO",
1111 tex->microtile ? "YES" : " NO",
1112 whandle->stride / util_format_get_blocksize(base->format),
1113 base->width0, base->height0,
1114 util_format_short_name(base->format));
1115
1116 /* Enforce microtiled zbuffer. */
1117 override_zb_flags = util_format_is_depth_or_stencil(base->format) &&
1118 tex->microtile == R300_BUFFER_LINEAR;
1119
1120 if (override_zb_flags) {
1121 switch (util_format_get_blocksize(base->format)) {
1122 case 4:
1123 tex->microtile = R300_BUFFER_TILED;
1124 break;
1125
1126 case 2:
1127 if (rws->get_value(rws, R300_VID_SQUARE_TILING_SUPPORT)) {
1128 tex->microtile = R300_BUFFER_SQUARETILED;
1129 break;
1130 }
1131 /* Pass through. */
1132
1133 default:
1134 override_zb_flags = FALSE;
1135 }
1136 }
1137
1138 r300_setup_miptree(rscreen, tex);
1139 r300_texture_setup_immutable_state(rscreen, tex);
1140 r300_texture_setup_fb_state(rscreen, tex);
1141
1142 if (override_zb_flags) {
1143 rws->buffer_set_tiling(rws, tex->buffer,
1144 tex->pitch[0] * util_format_get_blocksize(tex->b.b.format),
1145 tex->microtile,
1146 tex->macrotile);
1147 }
1148 return (struct pipe_resource*)tex;
1149 }