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