Merge branch '7.8'
[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 #include "pipe/p_screen.h"
25
26 #include "util/u_format.h"
27 #include "util/u_math.h"
28 #include "util/u_memory.h"
29
30 #include "r300_context.h"
31 #include "r300_texture.h"
32 #include "r300_screen.h"
33 #include "r300_winsys.h"
34
35 /* XXX Enable float textures here. */
36 /*#define ENABLE_FLOAT_TEXTURES*/
37
38 #define TILE_WIDTH 0
39 #define TILE_HEIGHT 1
40
41 static const unsigned microblock_table[5][3][2] = {
42 /*linear tiled square-tiled */
43 {{32, 1}, {8, 4}, {0, 0}}, /* 8 bits per pixel */
44 {{16, 1}, {8, 2}, {4, 4}}, /* 16 bits per pixel */
45 {{ 8, 1}, {4, 2}, {0, 0}}, /* 32 bits per pixel */
46 {{ 4, 1}, {0, 0}, {2, 2}}, /* 64 bits per pixel */
47 {{ 2, 1}, {0, 0}, {0, 0}} /* 128 bits per pixel */
48 };
49
50 /* Return true for non-compressed and non-YUV formats. */
51 static boolean r300_format_is_plain(enum pipe_format format)
52 {
53 const struct util_format_description *desc = util_format_description(format);
54
55 if (!format) {
56 return FALSE;
57 }
58
59 return desc->layout == UTIL_FORMAT_LAYOUT_PLAIN;
60 }
61
62 /* Translate a pipe_format into a useful texture format for sampling.
63 *
64 * Some special formats are translated directly using R300_EASY_TX_FORMAT,
65 * but the majority of them is translated in a generic way, automatically
66 * supporting all the formats hw can support.
67 *
68 * R300_EASY_TX_FORMAT swizzles the texture.
69 * Note the signature of R300_EASY_TX_FORMAT:
70 * R300_EASY_TX_FORMAT(B, G, R, A, FORMAT);
71 *
72 * The FORMAT specifies how the texture sampler will treat the texture, and
73 * makes available X, Y, Z, W, ZERO, and ONE for swizzling. */
74 static uint32_t r300_translate_texformat(enum pipe_format format)
75 {
76 uint32_t result = 0;
77 const struct util_format_description *desc;
78 unsigned i;
79 boolean uniform = TRUE;
80 const uint32_t swizzle_shift[4] = {
81 R300_TX_FORMAT_R_SHIFT,
82 R300_TX_FORMAT_G_SHIFT,
83 R300_TX_FORMAT_B_SHIFT,
84 R300_TX_FORMAT_A_SHIFT
85 };
86 const uint32_t swizzle[4] = {
87 R300_TX_FORMAT_X,
88 R300_TX_FORMAT_Y,
89 R300_TX_FORMAT_Z,
90 R300_TX_FORMAT_W
91 };
92 const uint32_t sign_bit[4] = {
93 R300_TX_FORMAT_SIGNED_X,
94 R300_TX_FORMAT_SIGNED_Y,
95 R300_TX_FORMAT_SIGNED_Z,
96 R300_TX_FORMAT_SIGNED_W,
97 };
98
99 desc = util_format_description(format);
100
101 /* Colorspace (return non-RGB formats directly). */
102 switch (desc->colorspace) {
103 /* Depth stencil formats. */
104 case UTIL_FORMAT_COLORSPACE_ZS:
105 switch (format) {
106 case PIPE_FORMAT_Z16_UNORM:
107 return R300_EASY_TX_FORMAT(X, X, X, X, X16);
108 case PIPE_FORMAT_X8Z24_UNORM:
109 case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
110 return R300_EASY_TX_FORMAT(X, X, X, X, W24_FP);
111 default:
112 return ~0; /* Unsupported. */
113 }
114
115 /* YUV formats. */
116 case UTIL_FORMAT_COLORSPACE_YUV:
117 result |= R300_TX_FORMAT_YUV_TO_RGB;
118
119 switch (format) {
120 case PIPE_FORMAT_UYVY:
121 return R300_EASY_TX_FORMAT(X, Y, Z, ONE, YVYU422) | result;
122 case PIPE_FORMAT_YUYV:
123 return R300_EASY_TX_FORMAT(X, Y, Z, ONE, VYUY422) | result;
124 default:
125 return ~0; /* Unsupported/unknown. */
126 }
127
128 /* Add gamma correction. */
129 case UTIL_FORMAT_COLORSPACE_SRGB:
130 result |= R300_TX_FORMAT_GAMMA;
131 break;
132
133 default:;
134 }
135
136 /* Add swizzle. */
137 for (i = 0; i < 4; i++) {
138 switch (desc->swizzle[i]) {
139 case UTIL_FORMAT_SWIZZLE_X:
140 case UTIL_FORMAT_SWIZZLE_NONE:
141 result |= swizzle[0] << swizzle_shift[i];
142 break;
143 case UTIL_FORMAT_SWIZZLE_Y:
144 result |= swizzle[1] << swizzle_shift[i];
145 break;
146 case UTIL_FORMAT_SWIZZLE_Z:
147 result |= swizzle[2] << swizzle_shift[i];
148 break;
149 case UTIL_FORMAT_SWIZZLE_W:
150 result |= swizzle[3] << swizzle_shift[i];
151 break;
152 case UTIL_FORMAT_SWIZZLE_0:
153 result |= R300_TX_FORMAT_ZERO << swizzle_shift[i];
154 break;
155 case UTIL_FORMAT_SWIZZLE_1:
156 result |= R300_TX_FORMAT_ONE << swizzle_shift[i];
157 break;
158 default:
159 return ~0; /* Unsupported. */
160 }
161 }
162
163 /* S3TC formats. */
164 if (desc->layout == UTIL_FORMAT_LAYOUT_S3TC) {
165 switch (format) {
166 case PIPE_FORMAT_DXT1_RGB:
167 case PIPE_FORMAT_DXT1_RGBA:
168 case PIPE_FORMAT_DXT1_SRGB:
169 case PIPE_FORMAT_DXT1_SRGBA:
170 return R300_TX_FORMAT_DXT1 | result;
171 case PIPE_FORMAT_DXT3_RGBA:
172 case PIPE_FORMAT_DXT3_SRGBA:
173 return R300_TX_FORMAT_DXT3 | result;
174 case PIPE_FORMAT_DXT5_RGBA:
175 case PIPE_FORMAT_DXT5_SRGBA:
176 return R300_TX_FORMAT_DXT5 | result;
177 default:
178 return ~0; /* Unsupported/unknown. */
179 }
180 }
181
182 /* Add sign. */
183 for (i = 0; i < desc->nr_channels; i++) {
184 if (desc->channel[i].type == UTIL_FORMAT_TYPE_SIGNED) {
185 result |= sign_bit[i];
186 }
187 }
188
189 /* RGTC formats. */
190 if (desc->layout == UTIL_FORMAT_LAYOUT_RGTC) {
191 switch (format) {
192 case PIPE_FORMAT_RGTC1_UNORM:
193 case PIPE_FORMAT_RGTC1_SNORM:
194 return R500_TX_FORMAT_ATI1N | result;
195 case PIPE_FORMAT_RGTC2_UNORM:
196 case PIPE_FORMAT_RGTC2_SNORM:
197 return R400_TX_FORMAT_ATI2N | result;
198 default:
199 return ~0; /* Unsupported/unknown. */
200 }
201 }
202
203 /* See whether the components are of the same size. */
204 for (i = 1; i < desc->nr_channels; i++) {
205 uniform = uniform && desc->channel[0].size == desc->channel[i].size;
206 }
207
208 /* Non-uniform formats. */
209 if (!uniform) {
210 switch (desc->nr_channels) {
211 case 3:
212 if (desc->channel[0].size == 5 &&
213 desc->channel[1].size == 6 &&
214 desc->channel[2].size == 5) {
215 return R300_TX_FORMAT_Z5Y6X5 | result;
216 }
217 if (desc->channel[0].size == 5 &&
218 desc->channel[1].size == 5 &&
219 desc->channel[2].size == 6) {
220 return R300_TX_FORMAT_Z6Y5X5 | result;
221 }
222 return ~0; /* Unsupported/unknown. */
223
224 case 4:
225 if (desc->channel[0].size == 5 &&
226 desc->channel[1].size == 5 &&
227 desc->channel[2].size == 5 &&
228 desc->channel[3].size == 1) {
229 return R300_TX_FORMAT_W1Z5Y5X5 | result;
230 }
231 if (desc->channel[0].size == 10 &&
232 desc->channel[1].size == 10 &&
233 desc->channel[2].size == 10 &&
234 desc->channel[3].size == 2) {
235 return R300_TX_FORMAT_W2Z10Y10X10 | result;
236 }
237 }
238 return ~0; /* Unsupported/unknown. */
239 }
240
241 /* And finally, uniform formats. */
242 switch (desc->channel[0].type) {
243 case UTIL_FORMAT_TYPE_UNSIGNED:
244 case UTIL_FORMAT_TYPE_SIGNED:
245 if (!desc->channel[0].normalized &&
246 desc->colorspace != UTIL_FORMAT_COLORSPACE_SRGB) {
247 return ~0;
248 }
249
250 switch (desc->channel[0].size) {
251 case 4:
252 switch (desc->nr_channels) {
253 case 2:
254 return R300_TX_FORMAT_Y4X4 | result;
255 case 4:
256 return R300_TX_FORMAT_W4Z4Y4X4 | result;
257 }
258 return ~0;
259
260 case 8:
261 switch (desc->nr_channels) {
262 case 1:
263 return R300_TX_FORMAT_X8 | result;
264 case 2:
265 return R300_TX_FORMAT_Y8X8 | result;
266 case 4:
267 return R300_TX_FORMAT_W8Z8Y8X8 | result;
268 }
269 return ~0;
270
271 case 16:
272 switch (desc->nr_channels) {
273 case 1:
274 return R300_TX_FORMAT_X16 | result;
275 case 2:
276 return R300_TX_FORMAT_Y16X16 | result;
277 case 4:
278 return R300_TX_FORMAT_W16Z16Y16X16 | result;
279 }
280 }
281 return ~0;
282
283 #if defined(ENABLE_FLOAT_TEXTURES)
284 case UTIL_FORMAT_TYPE_FLOAT:
285 switch (desc->channel[0].size) {
286 case 16:
287 switch (desc->nr_channels) {
288 case 1:
289 return R300_TX_FORMAT_16F | result;
290 case 2:
291 return R300_TX_FORMAT_16F_16F | result;
292 case 4:
293 return R300_TX_FORMAT_16F_16F_16F_16F | result;
294 }
295 return ~0;
296
297 case 32:
298 switch (desc->nr_channels) {
299 case 1:
300 return R300_TX_FORMAT_32F | result;
301 case 2:
302 return R300_TX_FORMAT_32F_32F | result;
303 case 4:
304 return R300_TX_FORMAT_32F_32F_32F_32F | result;
305 }
306 }
307 #endif
308 }
309
310 return ~0; /* Unsupported/unknown. */
311 }
312
313 static uint32_t r500_tx_format_msb_bit(enum pipe_format format)
314 {
315 switch (format) {
316 case PIPE_FORMAT_RGTC1_UNORM:
317 case PIPE_FORMAT_RGTC1_SNORM:
318 return R500_TXFORMAT_MSB;
319 default:
320 return 0;
321 }
322 }
323
324 /* Buffer formats. */
325
326 /* Colorbuffer formats. This is the unswizzled format of the RB3D block's
327 * output. For the swizzling of the targets, check the shader's format. */
328 static uint32_t r300_translate_colorformat(enum pipe_format format)
329 {
330 switch (format) {
331 /* 8-bit buffers. */
332 case PIPE_FORMAT_A8_UNORM:
333 case PIPE_FORMAT_I8_UNORM:
334 case PIPE_FORMAT_L8_UNORM:
335 case PIPE_FORMAT_R8_UNORM:
336 case PIPE_FORMAT_R8_SNORM:
337 return R300_COLOR_FORMAT_I8;
338
339 /* 16-bit buffers. */
340 case PIPE_FORMAT_B5G6R5_UNORM:
341 return R300_COLOR_FORMAT_RGB565;
342 case PIPE_FORMAT_B5G5R5A1_UNORM:
343 case PIPE_FORMAT_B5G5R5X1_UNORM:
344 return R300_COLOR_FORMAT_ARGB1555;
345 case PIPE_FORMAT_B4G4R4A4_UNORM:
346 return R300_COLOR_FORMAT_ARGB4444;
347
348 /* 32-bit buffers. */
349 case PIPE_FORMAT_B8G8R8A8_UNORM:
350 case PIPE_FORMAT_B8G8R8X8_UNORM:
351 case PIPE_FORMAT_A8R8G8B8_UNORM:
352 case PIPE_FORMAT_X8R8G8B8_UNORM:
353 case PIPE_FORMAT_A8B8G8R8_UNORM:
354 case PIPE_FORMAT_R8G8B8A8_SNORM:
355 case PIPE_FORMAT_X8B8G8R8_UNORM:
356 case PIPE_FORMAT_R8SG8SB8UX8U_NORM:
357 return R300_COLOR_FORMAT_ARGB8888;
358 case PIPE_FORMAT_R10G10B10A2_UNORM:
359 return R500_COLOR_FORMAT_ARGB2101010; /* R5xx-only? */
360
361 /* 64-bit buffers. */
362 case PIPE_FORMAT_R16G16B16A16_UNORM:
363 case PIPE_FORMAT_R16G16B16A16_SNORM:
364 #if defined(ENABLE_FLOAT_TEXTURES)
365 case PIPE_FORMAT_R16G16B16A16_FLOAT:
366 #endif
367 return R300_COLOR_FORMAT_ARGB16161616;
368
369 /* 128-bit buffers. */
370 #if defined(ENABLE_FLOAT_TEXTURES)
371 case PIPE_FORMAT_R32G32B32A32_FLOAT:
372 return R300_COLOR_FORMAT_ARGB32323232;
373 #endif
374
375 /* YUV buffers. */
376 case PIPE_FORMAT_UYVY:
377 return R300_COLOR_FORMAT_YVYU;
378 case PIPE_FORMAT_YUYV:
379 return R300_COLOR_FORMAT_VYUY;
380 default:
381 return ~0; /* Unsupported. */
382 }
383 }
384
385 /* Depthbuffer and stencilbuffer. Thankfully, we only support two flavors. */
386 static uint32_t r300_translate_zsformat(enum pipe_format format)
387 {
388 switch (format) {
389 /* 16-bit depth, no stencil */
390 case PIPE_FORMAT_Z16_UNORM:
391 return R300_DEPTHFORMAT_16BIT_INT_Z;
392 /* 24-bit depth, ignored stencil */
393 case PIPE_FORMAT_X8Z24_UNORM:
394 /* 24-bit depth, 8-bit stencil */
395 case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
396 return R300_DEPTHFORMAT_24BIT_INT_Z_8BIT_STENCIL;
397 default:
398 return ~0; /* Unsupported. */
399 }
400 }
401
402 /* Shader output formats. This is essentially the swizzle from the shader
403 * to the RB3D block.
404 *
405 * Note that formats are stored from C3 to C0. */
406 static uint32_t r300_translate_out_fmt(enum pipe_format format)
407 {
408 uint32_t modifier = 0;
409 unsigned i;
410 const struct util_format_description *desc;
411 static const uint32_t sign_bit[4] = {
412 R300_OUT_SIGN(0x1),
413 R300_OUT_SIGN(0x2),
414 R300_OUT_SIGN(0x4),
415 R300_OUT_SIGN(0x8),
416 };
417
418 desc = util_format_description(format);
419
420 /* Specifies how the shader output is written to the fog unit. */
421 if (desc->channel[0].type == UTIL_FORMAT_TYPE_FLOAT) {
422 if (desc->channel[0].size == 32) {
423 modifier |= R300_US_OUT_FMT_C4_32_FP;
424 } else {
425 modifier |= R300_US_OUT_FMT_C4_16_FP;
426 }
427 } else {
428 if (desc->channel[0].size == 16) {
429 modifier |= R300_US_OUT_FMT_C4_16;
430 } else {
431 /* C4_8 seems to be used for the formats whose pixel size
432 * is <= 32 bits. */
433 modifier |= R300_US_OUT_FMT_C4_8;
434 }
435 }
436
437 /* Add sign. */
438 for (i = 0; i < 4; i++)
439 if (desc->channel[i].type == UTIL_FORMAT_TYPE_SIGNED) {
440 modifier |= sign_bit[i];
441 }
442
443 /* Add swizzles and return. */
444 switch (format) {
445 /* 8-bit outputs.
446 * COLORFORMAT_I8 stores the C2 component. */
447 case PIPE_FORMAT_A8_UNORM:
448 return modifier | R300_C2_SEL_A;
449 case PIPE_FORMAT_I8_UNORM:
450 case PIPE_FORMAT_L8_UNORM:
451 case PIPE_FORMAT_R8_UNORM:
452 case PIPE_FORMAT_R8_SNORM:
453 return modifier | R300_C2_SEL_R;
454
455 /* BGRA outputs. */
456 case PIPE_FORMAT_B5G6R5_UNORM:
457 case PIPE_FORMAT_B5G5R5A1_UNORM:
458 case PIPE_FORMAT_B5G5R5X1_UNORM:
459 case PIPE_FORMAT_B4G4R4A4_UNORM:
460 case PIPE_FORMAT_B8G8R8A8_UNORM:
461 case PIPE_FORMAT_B8G8R8X8_UNORM:
462 return modifier |
463 R300_C0_SEL_B | R300_C1_SEL_G |
464 R300_C2_SEL_R | R300_C3_SEL_A;
465
466 /* ARGB outputs. */
467 case PIPE_FORMAT_A8R8G8B8_UNORM:
468 case PIPE_FORMAT_X8R8G8B8_UNORM:
469 return modifier |
470 R300_C0_SEL_A | R300_C1_SEL_R |
471 R300_C2_SEL_G | R300_C3_SEL_B;
472
473 /* ABGR outputs. */
474 case PIPE_FORMAT_A8B8G8R8_UNORM:
475 case PIPE_FORMAT_X8B8G8R8_UNORM:
476 return modifier |
477 R300_C0_SEL_A | R300_C1_SEL_B |
478 R300_C2_SEL_G | R300_C3_SEL_R;
479
480 /* RGBA outputs. */
481 case PIPE_FORMAT_R8G8B8A8_SNORM:
482 case PIPE_FORMAT_R8SG8SB8UX8U_NORM:
483 case PIPE_FORMAT_R10G10B10A2_UNORM:
484 case PIPE_FORMAT_R16G16B16A16_UNORM:
485 case PIPE_FORMAT_R16G16B16A16_SNORM:
486 //case PIPE_FORMAT_R16G16B16A16_FLOAT: /* not in pipe_format */
487 case PIPE_FORMAT_R32G32B32A32_FLOAT:
488 return modifier |
489 R300_C0_SEL_R | R300_C1_SEL_G |
490 R300_C2_SEL_B | R300_C3_SEL_A;
491
492 default:
493 return ~0; /* Unsupported. */
494 }
495 }
496
497 boolean r300_is_colorbuffer_format_supported(enum pipe_format format)
498 {
499 return r300_translate_colorformat(format) != ~0 &&
500 r300_translate_out_fmt(format) != ~0;
501 }
502
503 boolean r300_is_zs_format_supported(enum pipe_format format)
504 {
505 return r300_translate_zsformat(format) != ~0;
506 }
507
508 boolean r300_is_sampler_format_supported(enum pipe_format format)
509 {
510 return r300_translate_texformat(format) != ~0;
511 }
512
513 static void r300_setup_texture_state(struct r300_screen* screen, struct r300_texture* tex)
514 {
515 struct r300_texture_format_state* state = &tex->state;
516 struct pipe_texture *pt = &tex->tex;
517 unsigned i;
518 boolean is_r500 = screen->caps->is_r500;
519
520 /* Set sampler state. */
521 state->format0 = R300_TX_WIDTH((pt->width0 - 1) & 0x7ff) |
522 R300_TX_HEIGHT((pt->height0 - 1) & 0x7ff);
523
524 if (tex->is_npot || tex->stride_override) {
525 /* rectangles love this */
526 state->format0 |= R300_TX_PITCH_EN;
527 state->format2 = (tex->pitch[0] - 1) & 0x1fff;
528 } else {
529 /* power of two textures (3D, mipmaps, and no pitch) */
530 state->format0 |= R300_TX_DEPTH(util_logbase2(pt->depth0) & 0xf);
531 }
532
533 state->format1 = r300_translate_texformat(pt->format);
534 if (pt->target == PIPE_TEXTURE_CUBE) {
535 state->format1 |= R300_TX_FORMAT_CUBIC_MAP;
536 }
537 if (pt->target == PIPE_TEXTURE_3D) {
538 state->format1 |= R300_TX_FORMAT_3D;
539 }
540
541 /* large textures on r500 */
542 if (is_r500)
543 {
544 if (pt->width0 > 2048) {
545 state->format2 |= R500_TXWIDTH_BIT11;
546 }
547 if (pt->height0 > 2048) {
548 state->format2 |= R500_TXHEIGHT_BIT11;
549 }
550 state->format2 |= r500_tx_format_msb_bit(pt->format);
551 }
552
553 SCREEN_DBG(screen, DBG_TEX, "r300: Set texture state (%dx%d, %d levels)\n",
554 pt->width0, pt->height0, pt->last_level);
555
556 /* Set framebuffer state. */
557 if (util_format_is_depth_or_stencil(tex->tex.format)) {
558 for (i = 0; i <= tex->tex.last_level; i++) {
559 tex->fb_state.depthpitch[i] =
560 tex->pitch[i] |
561 R300_DEPTHMACROTILE(tex->mip_macrotile[i]) |
562 R300_DEPTHMICROTILE(tex->microtile);
563 }
564 tex->fb_state.zb_format = r300_translate_zsformat(tex->tex.format);
565 } else {
566 for (i = 0; i <= tex->tex.last_level; i++) {
567 tex->fb_state.colorpitch[i] =
568 tex->pitch[i] |
569 r300_translate_colorformat(tex->tex.format) |
570 R300_COLOR_TILE(tex->mip_macrotile[i]) |
571 R300_COLOR_MICROTILE(tex->microtile);
572 }
573 tex->fb_state.us_out_fmt = r300_translate_out_fmt(tex->tex.format);
574 }
575 }
576
577 void r300_texture_reinterpret_format(struct pipe_screen *screen,
578 struct pipe_texture *tex,
579 enum pipe_format new_format)
580 {
581 struct r300_screen *r300screen = r300_screen(screen);
582
583 SCREEN_DBG(r300screen, DBG_TEX, "r300: Reinterpreting format: %s -> %s\n",
584 util_format_name(tex->format), util_format_name(new_format));
585
586 tex->format = new_format;
587
588 r300_setup_texture_state(r300_screen(screen), (struct r300_texture*)tex);
589 }
590
591 unsigned r300_texture_get_offset(struct r300_texture* tex, unsigned level,
592 unsigned zslice, unsigned face)
593 {
594 unsigned offset = tex->offset[level];
595
596 switch (tex->tex.target) {
597 case PIPE_TEXTURE_3D:
598 assert(face == 0);
599 return offset + zslice * tex->layer_size[level];
600
601 case PIPE_TEXTURE_CUBE:
602 assert(zslice == 0);
603 return offset + face * tex->layer_size[level];
604
605 default:
606 assert(zslice == 0 && face == 0);
607 return offset;
608 }
609 }
610
611 /**
612 * Return the width (dim==TILE_WIDTH) or height (dim==TILE_HEIGHT) of one tile
613 * of the given texture.
614 */
615 static unsigned r300_texture_get_tile_size(struct r300_texture* tex,
616 int dim, boolean macrotile)
617 {
618 unsigned pixsize, tile_size;
619
620 pixsize = util_format_get_blocksize(tex->tex.format);
621 tile_size = microblock_table[util_logbase2(pixsize)][tex->microtile][dim];
622
623 if (macrotile) {
624 tile_size *= 8;
625 }
626
627 assert(tile_size);
628 return tile_size;
629 }
630
631 /* Return true if macrotiling should be enabled on the miplevel. */
632 static boolean r300_texture_macro_switch(struct r300_texture *tex,
633 unsigned level,
634 boolean rv350_mode,
635 int dim)
636 {
637 unsigned tile, texdim;
638
639 tile = r300_texture_get_tile_size(tex, dim, TRUE);
640 if (dim == TILE_WIDTH) {
641 texdim = u_minify(tex->tex.width0, level);
642 } else {
643 texdim = u_minify(tex->tex.height0, level);
644 }
645
646 /* See TX_FILTER1_n.MACRO_SWITCH. */
647 if (rv350_mode) {
648 return texdim >= tile;
649 } else {
650 return texdim > tile;
651 }
652 }
653
654 /**
655 * Return the stride, in bytes, of the texture images of the given texture
656 * at the given level.
657 */
658 unsigned r300_texture_get_stride(struct r300_screen* screen,
659 struct r300_texture* tex, unsigned level)
660 {
661 unsigned tile_width, width;
662
663 if (tex->stride_override)
664 return tex->stride_override;
665
666 /* Check the level. */
667 if (level > tex->tex.last_level) {
668 SCREEN_DBG(screen, DBG_TEX, "%s: level (%u) > last_level (%u)\n",
669 __FUNCTION__, level, tex->tex.last_level);
670 return 0;
671 }
672
673 width = u_minify(tex->tex.width0, level);
674
675 if (r300_format_is_plain(tex->tex.format)) {
676 tile_width = r300_texture_get_tile_size(tex, TILE_WIDTH,
677 tex->mip_macrotile[level]);
678 width = align(width, tile_width);
679
680 return util_format_get_stride(tex->tex.format, width);
681 } else {
682 return align(util_format_get_stride(tex->tex.format, width), 32);
683 }
684 }
685
686 static unsigned r300_texture_get_nblocksy(struct r300_texture* tex,
687 unsigned level)
688 {
689 unsigned height, tile_height;
690
691 height = u_minify(tex->tex.height0, level);
692
693 if (r300_format_is_plain(tex->tex.format)) {
694 tile_height = r300_texture_get_tile_size(tex, TILE_HEIGHT,
695 tex->mip_macrotile[level]);
696 height = align(height, tile_height);
697 }
698
699 return util_format_get_nblocksy(tex->tex.format, height);
700 }
701
702 static void r300_setup_miptree(struct r300_screen* screen,
703 struct r300_texture* tex)
704 {
705 struct pipe_texture* base = &tex->tex;
706 unsigned stride, size, layer_size, nblocksy, i;
707 boolean rv350_mode = screen->caps->family >= CHIP_FAMILY_RV350;
708
709 SCREEN_DBG(screen, DBG_TEX, "r300: Making miptree for texture, format %s\n",
710 util_format_name(base->format));
711
712 for (i = 0; i <= base->last_level; i++) {
713 /* Let's see if this miplevel can be macrotiled. */
714 tex->mip_macrotile[i] =
715 (tex->macrotile == R300_BUFFER_TILED &&
716 r300_texture_macro_switch(tex, i, rv350_mode, TILE_WIDTH) &&
717 r300_texture_macro_switch(tex, i, rv350_mode, TILE_HEIGHT)) ?
718 R300_BUFFER_TILED : R300_BUFFER_LINEAR;
719
720 stride = r300_texture_get_stride(screen, tex, i);
721 nblocksy = r300_texture_get_nblocksy(tex, i);
722 layer_size = stride * nblocksy;
723
724 if (base->target == PIPE_TEXTURE_CUBE)
725 size = layer_size * 6;
726 else
727 size = layer_size * u_minify(base->depth0, i);
728
729 tex->offset[i] = tex->size;
730 tex->size = tex->offset[i] + size;
731 tex->layer_size[i] = layer_size;
732 tex->pitch[i] = stride / util_format_get_blocksize(base->format);
733
734 SCREEN_DBG(screen, DBG_TEX, "r300: Texture miptree: Level %d "
735 "(%dx%dx%d px, pitch %d bytes) %d bytes total, macrotiled %s\n",
736 i, u_minify(base->width0, i), u_minify(base->height0, i),
737 u_minify(base->depth0, i), stride, tex->size,
738 tex->mip_macrotile[i] ? "TRUE" : "FALSE");
739 }
740 }
741
742 static void r300_setup_flags(struct r300_texture* tex)
743 {
744 tex->is_npot = !util_is_power_of_two(tex->tex.width0) ||
745 !util_is_power_of_two(tex->tex.height0);
746 }
747
748 static void r300_setup_tiling(struct pipe_screen *screen,
749 struct r300_texture *tex)
750 {
751 struct r300_winsys_screen *rws = (struct r300_winsys_screen *)screen->winsys;
752 enum pipe_format format = tex->tex.format;
753 boolean rv350_mode = r300_screen(screen)->caps->family >= CHIP_FAMILY_RV350;
754
755 if (!r300_format_is_plain(format)) {
756 return;
757 }
758
759 if (tex->tex.width0 == 1 ||
760 tex->tex.height0 == 1) {
761 return;
762 }
763
764 /* Set microtiling. */
765 switch (util_format_get_blocksize(format)) {
766 case 1:
767 case 4:
768 tex->microtile = R300_BUFFER_TILED;
769 break;
770
771 case 2:
772 case 8:
773 if (rws->get_value(rws, R300_VID_SQUARE_TILING_SUPPORT)) {
774 tex->microtile = R300_BUFFER_SQUARETILED;
775 }
776 break;
777 }
778
779 /* Set macrotiling. */
780 if (r300_texture_macro_switch(tex, 0, rv350_mode, TILE_WIDTH) &&
781 r300_texture_macro_switch(tex, 0, rv350_mode, TILE_HEIGHT)) {
782 tex->macrotile = R300_BUFFER_TILED;
783 }
784 }
785
786 /* Create a new texture. */
787 static struct pipe_texture* r300_texture_create(struct pipe_screen* screen,
788 const struct pipe_texture* base)
789 {
790 struct r300_texture* tex = CALLOC_STRUCT(r300_texture);
791 struct r300_screen* rscreen = r300_screen(screen);
792 struct r300_winsys_screen *rws = (struct r300_winsys_screen *)screen->winsys;
793
794 if (!tex) {
795 return NULL;
796 }
797
798 tex->tex = *base;
799 pipe_reference_init(&tex->tex.reference, 1);
800 tex->tex.screen = screen;
801
802 r300_setup_flags(tex);
803 if (!(base->tex_usage & R300_TEXTURE_USAGE_TRANSFER)) {
804 r300_setup_tiling(screen, tex);
805 }
806 r300_setup_miptree(rscreen, tex);
807 r300_setup_texture_state(rscreen, tex);
808
809 tex->buffer = rws->buffer_create(rws, 2048,
810 PIPE_BUFFER_USAGE_PIXEL,
811 tex->size);
812 rws->buffer_set_tiling(rws, tex->buffer,
813 tex->pitch[0],
814 tex->microtile,
815 tex->macrotile);
816
817 if (!tex->buffer) {
818 FREE(tex);
819 return NULL;
820 }
821
822 return (struct pipe_texture*)tex;
823 }
824
825 static void r300_texture_destroy(struct pipe_texture* texture)
826 {
827 struct r300_texture* tex = (struct r300_texture*)texture;
828 struct r300_winsys_screen *rws = (struct r300_winsys_screen *)texture->screen->winsys;
829
830 rws->buffer_reference(rws, &tex->buffer, NULL);
831 FREE(tex);
832 }
833
834 static struct pipe_surface* r300_get_tex_surface(struct pipe_screen* screen,
835 struct pipe_texture* texture,
836 unsigned face,
837 unsigned level,
838 unsigned zslice,
839 unsigned flags)
840 {
841 struct r300_texture* tex = (struct r300_texture*)texture;
842 struct pipe_surface* surface = CALLOC_STRUCT(pipe_surface);
843 unsigned offset;
844
845 offset = r300_texture_get_offset(tex, level, zslice, face);
846
847 if (surface) {
848 pipe_reference_init(&surface->reference, 1);
849 pipe_texture_reference(&surface->texture, texture);
850 surface->format = texture->format;
851 surface->width = u_minify(texture->width0, level);
852 surface->height = u_minify(texture->height0, level);
853 surface->offset = offset;
854 surface->usage = flags;
855 surface->zslice = zslice;
856 surface->texture = texture;
857 surface->face = face;
858 surface->level = level;
859 }
860
861 return surface;
862 }
863
864 static void r300_tex_surface_destroy(struct pipe_surface* s)
865 {
866 pipe_texture_reference(&s->texture, NULL);
867 FREE(s);
868 }
869
870
871 static struct pipe_texture*
872 r300_texture_from_handle(struct pipe_screen* screen,
873 const struct pipe_texture* base,
874 struct winsys_handle *whandle)
875 {
876 struct r300_winsys_screen *rws = (struct r300_winsys_screen*)screen->winsys;
877 struct r300_screen* rscreen = r300_screen(screen);
878 struct r300_winsys_buffer *buffer;
879 struct r300_texture* tex;
880 unsigned stride;
881
882 /* Support only 2D textures without mipmaps */
883 if (base->target != PIPE_TEXTURE_2D ||
884 base->depth0 != 1 ||
885 base->last_level != 0) {
886 return NULL;
887 }
888
889 buffer = rws->buffer_from_handle(rws, screen, whandle, &stride);
890 if (!buffer) {
891 return NULL;
892 }
893
894 tex = CALLOC_STRUCT(r300_texture);
895 if (!tex) {
896 return NULL;
897 }
898
899 tex->tex = *base;
900 pipe_reference_init(&tex->tex.reference, 1);
901 tex->tex.screen = screen;
902
903 tex->stride_override = stride;
904
905 /* one ref already taken */
906 tex->buffer = buffer;
907
908 rws->buffer_get_tiling(rws, buffer, &tex->microtile, &tex->macrotile);
909 r300_setup_flags(tex);
910 r300_setup_miptree(rscreen, tex);
911 r300_setup_texture_state(rscreen, tex);
912 return (struct pipe_texture*)tex;
913 }
914
915 static boolean
916 r300_texture_get_handle(struct pipe_screen* screen,
917 struct pipe_texture *texture,
918 struct winsys_handle *whandle)
919 {
920 struct r300_winsys_screen *rws = (struct r300_winsys_screen *)screen->winsys;
921 struct r300_texture* tex = (struct r300_texture*)texture;
922 unsigned stride;
923
924 if (!tex) {
925 return FALSE;
926 }
927
928 stride = r300_texture_get_stride(r300_screen(screen), tex, 0);
929
930 rws->buffer_get_handle(rws, tex->buffer, stride, whandle);
931
932 return TRUE;
933 }
934
935 static struct pipe_video_surface *
936 r300_video_surface_create(struct pipe_screen *screen,
937 enum pipe_video_chroma_format chroma_format,
938 unsigned width, unsigned height)
939 {
940 struct r300_video_surface *r300_vsfc;
941 struct pipe_texture base;
942
943 assert(screen);
944 assert(width && height);
945
946 r300_vsfc = CALLOC_STRUCT(r300_video_surface);
947 if (!r300_vsfc)
948 return NULL;
949
950 pipe_reference_init(&r300_vsfc->base.reference, 1);
951 r300_vsfc->base.screen = screen;
952 r300_vsfc->base.chroma_format = chroma_format;
953 r300_vsfc->base.width = width;
954 r300_vsfc->base.height = height;
955
956 memset(&base, 0, sizeof(struct pipe_texture));
957 base.target = PIPE_TEXTURE_2D;
958 base.format = PIPE_FORMAT_B8G8R8X8_UNORM;
959 base.last_level = 0;
960 base.width0 = util_next_power_of_two(width);
961 base.height0 = util_next_power_of_two(height);
962 base.depth0 = 1;
963 base.tex_usage = PIPE_TEXTURE_USAGE_SAMPLER |
964 PIPE_TEXTURE_USAGE_RENDER_TARGET;
965
966 r300_vsfc->tex = screen->texture_create(screen, &base);
967 if (!r300_vsfc->tex)
968 {
969 FREE(r300_vsfc);
970 return NULL;
971 }
972
973 return &r300_vsfc->base;
974 }
975
976 static void r300_video_surface_destroy(struct pipe_video_surface *vsfc)
977 {
978 struct r300_video_surface *r300_vsfc = r300_video_surface(vsfc);
979 pipe_texture_reference(&r300_vsfc->tex, NULL);
980 FREE(r300_vsfc);
981 }
982
983 void r300_init_screen_texture_functions(struct pipe_screen* screen)
984 {
985 screen->texture_create = r300_texture_create;
986 screen->texture_from_handle = r300_texture_from_handle;
987 screen->texture_get_handle = r300_texture_get_handle;
988 screen->texture_destroy = r300_texture_destroy;
989 screen->get_tex_surface = r300_get_tex_surface;
990 screen->tex_surface_destroy = r300_tex_surface_destroy;
991
992 screen->video_surface_create = r300_video_surface_create;
993 screen->video_surface_destroy= r300_video_surface_destroy;
994 }
995
996 boolean r300_get_texture_buffer(struct pipe_screen* screen,
997 struct pipe_texture* texture,
998 struct r300_winsys_buffer** buffer,
999 unsigned* stride)
1000 {
1001 struct r300_texture* tex = (struct r300_texture*)texture;
1002 struct r300_winsys_screen *rws = (struct r300_winsys_screen *)screen->winsys;
1003 struct r300_winsys_buffer *buf;
1004
1005 if (!tex) {
1006 return FALSE;
1007 }
1008
1009 rws->buffer_reference(rws, &buf, tex->buffer);
1010
1011 if (stride) {
1012 *stride = r300_texture_get_stride(r300_screen(screen), tex, 0);
1013 }
1014
1015 *buffer = buf;
1016 return TRUE;
1017 }