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