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