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