r300: Update for UTIL_FORMAT_LAYOUT_xxx changes.
[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_Z24X8_UNORM:
96 case PIPE_FORMAT_Z24S8_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_YCBCR:
108 return R300_EASY_TX_FORMAT(X, Y, Z, ONE, YVYU422) | result;
109 case PIPE_FORMAT_YCBCR_REV:
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_R5G6B5_UNORM:
312 return R300_COLOR_FORMAT_RGB565;
313 case PIPE_FORMAT_A1R5G5B5_UNORM:
314 return R300_COLOR_FORMAT_ARGB1555;
315 case PIPE_FORMAT_A4R4G4B4_UNORM:
316 return R300_COLOR_FORMAT_ARGB4444;
317
318 /* 32-bit buffers. */
319 case PIPE_FORMAT_A8R8G8B8_UNORM:
320 case PIPE_FORMAT_A8R8G8B8_SRGB:
321 case PIPE_FORMAT_X8R8G8B8_UNORM:
322 case PIPE_FORMAT_X8R8G8B8_SRGB:
323 case PIPE_FORMAT_B8G8R8A8_UNORM:
324 case PIPE_FORMAT_B8G8R8A8_SRGB:
325 case PIPE_FORMAT_B8G8R8X8_UNORM:
326 case PIPE_FORMAT_B8G8R8X8_SRGB:
327 case PIPE_FORMAT_R8G8B8A8_UNORM:
328 case PIPE_FORMAT_R8G8B8A8_SNORM:
329 case PIPE_FORMAT_R8G8B8A8_SRGB:
330 case PIPE_FORMAT_R8G8B8X8_UNORM:
331 case PIPE_FORMAT_R8G8B8X8_SRGB:
332 case PIPE_FORMAT_R8G8B8X8_SNORM:
333 case PIPE_FORMAT_A8B8G8R8_SNORM:
334 case PIPE_FORMAT_X8B8G8R8_SNORM:
335 case PIPE_FORMAT_X8UB8UG8SR8S_NORM:
336 return R300_COLOR_FORMAT_ARGB8888;
337 case PIPE_FORMAT_A2B10G10R10_UNORM:
338 return R500_COLOR_FORMAT_ARGB2101010; /* R5xx-only? */
339
340 /* 64-bit buffers. */
341 case PIPE_FORMAT_R16G16B16A16_UNORM:
342 case PIPE_FORMAT_R16G16B16A16_SNORM:
343 //case PIPE_FORMAT_R16G16B16A16_FLOAT: /* not in pipe_format */
344 return R300_COLOR_FORMAT_ARGB16161616;
345
346 /* XXX Enable float textures here. */
347 #if 0
348 /* 128-bit buffers. */
349 case PIPE_FORMAT_R32G32B32A32_FLOAT:
350 return R300_COLOR_FORMAT_ARGB32323232;
351 #endif
352
353 /* YUV buffers. */
354 case PIPE_FORMAT_YCBCR:
355 return R300_COLOR_FORMAT_YVYU;
356 case PIPE_FORMAT_YCBCR_REV:
357 return R300_COLOR_FORMAT_VYUY;
358 default:
359 return ~0; /* Unsupported. */
360 }
361 }
362
363 /* Depthbuffer and stencilbuffer. Thankfully, we only support two flavors. */
364 static uint32_t r300_translate_zsformat(enum pipe_format format)
365 {
366 switch (format) {
367 /* 16-bit depth, no stencil */
368 case PIPE_FORMAT_Z16_UNORM:
369 return R300_DEPTHFORMAT_16BIT_INT_Z;
370 /* 24-bit depth, ignored stencil */
371 case PIPE_FORMAT_Z24X8_UNORM:
372 /* 24-bit depth, 8-bit stencil */
373 case PIPE_FORMAT_Z24S8_UNORM:
374 return R300_DEPTHFORMAT_24BIT_INT_Z_8BIT_STENCIL;
375 default:
376 return ~0; /* Unsupported. */
377 }
378 }
379
380 /* Shader output formats. This is essentially the swizzle from the shader
381 * to the RB3D block.
382 *
383 * Note that formats are stored from C3 to C0. */
384 static uint32_t r300_translate_out_fmt(enum pipe_format format)
385 {
386 uint32_t modifier = 0;
387 unsigned i;
388 const struct util_format_description *desc;
389 static const uint32_t sign_bit[4] = {
390 R300_OUT_SIGN(0x1),
391 R300_OUT_SIGN(0x2),
392 R300_OUT_SIGN(0x4),
393 R300_OUT_SIGN(0x8),
394 };
395
396 desc = util_format_description(format);
397
398 /* Specifies how the shader output is written to the fog unit. */
399 if (desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB) {
400 /* The gamma correction causes precision loss so we need
401 * higher precision to maintain reasonable quality.
402 * It has nothing to do with the colorbuffer format. */
403 modifier |= R300_US_OUT_FMT_C4_10_GAMMA;
404 } else if (desc->channel[0].type == UTIL_FORMAT_TYPE_FLOAT) {
405 if (desc->channel[0].size == 32) {
406 modifier |= R300_US_OUT_FMT_C4_32_FP;
407 } else {
408 modifier |= R300_US_OUT_FMT_C4_16_FP;
409 }
410 } else {
411 if (desc->channel[0].size == 16) {
412 modifier |= R300_US_OUT_FMT_C4_16;
413 } else {
414 /* C4_8 seems to be used for the formats whose pixel size
415 * is <= 32 bits. */
416 modifier |= R300_US_OUT_FMT_C4_8;
417 }
418 }
419
420 /* Add sign. */
421 for (i = 0; i < 4; i++)
422 if (desc->channel[i].type == UTIL_FORMAT_TYPE_SIGNED) {
423 modifier |= sign_bit[i];
424 }
425
426 /* Add swizzles and return. */
427 switch (format) {
428 /* 8-bit outputs.
429 * COLORFORMAT_I8 stores the C2 component. */
430 case PIPE_FORMAT_A8_UNORM:
431 return modifier | R300_C2_SEL_A;
432 case PIPE_FORMAT_I8_UNORM:
433 case PIPE_FORMAT_L8_UNORM:
434 case PIPE_FORMAT_L8_SRGB:
435 case PIPE_FORMAT_R8_UNORM:
436 case PIPE_FORMAT_R8_SNORM:
437 return modifier | R300_C2_SEL_R;
438
439 /* ARGB 32-bit outputs. */
440 case PIPE_FORMAT_R5G6B5_UNORM:
441 case PIPE_FORMAT_A1R5G5B5_UNORM:
442 case PIPE_FORMAT_A4R4G4B4_UNORM:
443 case PIPE_FORMAT_A8R8G8B8_UNORM:
444 case PIPE_FORMAT_A8R8G8B8_SRGB:
445 case PIPE_FORMAT_X8R8G8B8_UNORM:
446 case PIPE_FORMAT_X8R8G8B8_SRGB:
447 return modifier |
448 R300_C0_SEL_B | R300_C1_SEL_G |
449 R300_C2_SEL_R | R300_C3_SEL_A;
450
451 /* BGRA 32-bit outputs. */
452 case PIPE_FORMAT_B8G8R8A8_UNORM:
453 case PIPE_FORMAT_B8G8R8A8_SRGB:
454 case PIPE_FORMAT_B8G8R8X8_UNORM:
455 case PIPE_FORMAT_B8G8R8X8_SRGB:
456 return modifier |
457 R300_C0_SEL_A | R300_C1_SEL_R |
458 R300_C2_SEL_G | R300_C3_SEL_B;
459
460 /* RGBA 32-bit outputs. */
461 case PIPE_FORMAT_R8G8B8A8_UNORM:
462 case PIPE_FORMAT_R8G8B8A8_SNORM:
463 case PIPE_FORMAT_R8G8B8A8_SRGB:
464 case PIPE_FORMAT_R8G8B8X8_UNORM:
465 case PIPE_FORMAT_R8G8B8X8_SRGB:
466 case PIPE_FORMAT_R8G8B8X8_SNORM:
467 return modifier |
468 R300_C0_SEL_A | R300_C1_SEL_B |
469 R300_C2_SEL_G | R300_C3_SEL_R;
470
471 /* ABGR 32-bit outputs. */
472 case PIPE_FORMAT_A8B8G8R8_SNORM:
473 case PIPE_FORMAT_X8B8G8R8_SNORM:
474 case PIPE_FORMAT_X8UB8UG8SR8S_NORM:
475 case PIPE_FORMAT_A2B10G10R10_UNORM:
476 /* RGBA high precision outputs (same swizzles as ABGR low precision) */
477 case PIPE_FORMAT_R16G16B16A16_UNORM:
478 case PIPE_FORMAT_R16G16B16A16_SNORM:
479 //case PIPE_FORMAT_R16G16B16A16_FLOAT: /* not in pipe_format */
480 case PIPE_FORMAT_R32G32B32A32_FLOAT:
481 return modifier |
482 R300_C0_SEL_R | R300_C1_SEL_G |
483 R300_C2_SEL_B | R300_C3_SEL_A;
484
485 default:
486 return ~0; /* Unsupported. */
487 }
488 }
489
490 boolean r300_is_colorbuffer_format_supported(enum pipe_format format)
491 {
492 return r300_translate_colorformat(format) != ~0 &&
493 r300_translate_out_fmt(format) != ~0;
494 }
495
496 boolean r300_is_zs_format_supported(enum pipe_format format)
497 {
498 return r300_translate_zsformat(format) != ~0;
499 }
500
501 boolean r300_is_sampler_format_supported(enum pipe_format format)
502 {
503 return r300_translate_texformat(format) != ~0;
504 }
505
506 static void r300_setup_texture_state(struct r300_screen* screen, struct r300_texture* tex)
507 {
508 struct r300_texture_state* state = &tex->state;
509 struct pipe_texture *pt = &tex->tex;
510 unsigned i;
511 boolean is_r500 = screen->caps->is_r500;
512
513 /* Set sampler state. */
514 state->format0 = R300_TX_WIDTH((pt->width0 - 1) & 0x7ff) |
515 R300_TX_HEIGHT((pt->height0 - 1) & 0x7ff);
516
517 if (tex->is_npot) {
518 /* rectangles love this */
519 state->format0 |= R300_TX_PITCH_EN;
520 state->format2 = (tex->pitch[0] - 1) & 0x1fff;
521 } else {
522 /* power of two textures (3D, mipmaps, and no pitch) */
523 state->format0 |= R300_TX_DEPTH(util_logbase2(pt->depth0) & 0xf);
524 }
525
526 state->format1 = r300_translate_texformat(pt->format);
527 if (pt->target == PIPE_TEXTURE_CUBE) {
528 state->format1 |= R300_TX_FORMAT_CUBIC_MAP;
529 }
530 if (pt->target == PIPE_TEXTURE_3D) {
531 state->format1 |= R300_TX_FORMAT_3D;
532 }
533
534 /* large textures on r500 */
535 if (is_r500)
536 {
537 if (pt->width0 > 2048) {
538 state->format2 |= R500_TXWIDTH_BIT11;
539 }
540 if (pt->height0 > 2048) {
541 state->format2 |= R500_TXHEIGHT_BIT11;
542 }
543 }
544
545 SCREEN_DBG(screen, DBG_TEX, "r300: Set texture state (%dx%d, %d levels)\n",
546 pt->width0, pt->height0, pt->last_level);
547
548 /* Set framebuffer state. */
549 if (util_format_is_depth_or_stencil(tex->tex.format)) {
550 for (i = 0; i <= tex->tex.last_level; i++) {
551 tex->fb_state.depthpitch[i] =
552 tex->pitch[i] |
553 R300_DEPTHMACROTILE(tex->mip_macrotile[i]) |
554 R300_DEPTHMICROTILE(tex->microtile);
555 }
556 tex->fb_state.zb_format = r300_translate_zsformat(tex->tex.format);
557 } else {
558 for (i = 0; i <= tex->tex.last_level; i++) {
559 tex->fb_state.colorpitch[i] =
560 tex->pitch[i] |
561 r300_translate_colorformat(tex->tex.format) |
562 R300_COLOR_TILE(tex->mip_macrotile[i]) |
563 R300_COLOR_MICROTILE(tex->microtile);
564 }
565 tex->fb_state.us_out_fmt = r300_translate_out_fmt(tex->tex.format);
566 }
567 }
568
569 void r300_texture_reinterpret_format(struct pipe_screen *screen,
570 struct pipe_texture *tex,
571 enum pipe_format new_format)
572 {
573 struct r300_screen *r300screen = r300_screen(screen);
574
575 SCREEN_DBG(r300screen, DBG_TEX, "r300: Reinterpreting format: %s -> %s\n",
576 util_format_name(tex->format), util_format_name(new_format));
577
578 tex->format = new_format;
579
580 r300_setup_texture_state(r300_screen(screen), (struct r300_texture*)tex);
581 }
582
583 unsigned r300_texture_get_offset(struct r300_texture* tex, unsigned level,
584 unsigned zslice, unsigned face)
585 {
586 unsigned offset = tex->offset[level];
587
588 switch (tex->tex.target) {
589 case PIPE_TEXTURE_3D:
590 assert(face == 0);
591 return offset + zslice * tex->layer_size[level];
592
593 case PIPE_TEXTURE_CUBE:
594 assert(zslice == 0);
595 return offset + face * tex->layer_size[level];
596
597 default:
598 assert(zslice == 0 && face == 0);
599 return offset;
600 }
601 }
602
603 /**
604 * Return the width (dim==TILE_WIDTH) or height (dim==TILE_HEIGHT) of one tile
605 * of the given texture.
606 */
607 static unsigned r300_texture_get_tile_size(struct r300_texture* tex,
608 int dim, boolean macrotile)
609 {
610 unsigned pixsize, tile_size;
611
612 pixsize = util_format_get_blocksize(tex->tex.format);
613 tile_size = microblock_table[util_logbase2(pixsize)][tex->microtile][dim];
614
615 if (macrotile) {
616 tile_size *= 8;
617 }
618
619 assert(tile_size);
620 return tile_size;
621 }
622
623 /* Return true if macrotiling should be enabled on the miplevel. */
624 static boolean r300_texture_macro_switch(struct r300_texture *tex,
625 unsigned level,
626 boolean rv350_mode)
627 {
628 unsigned tile_width, width;
629
630 tile_width = r300_texture_get_tile_size(tex, TILE_WIDTH, TRUE);
631 width = u_minify(tex->tex.width0, level);
632
633 /* See TX_FILTER1_n.MACRO_SWITCH. */
634 if (rv350_mode) {
635 return width >= tile_width;
636 } else {
637 return width > tile_width;
638 }
639 }
640
641 /**
642 * Return the stride, in bytes, of the texture images of the given texture
643 * at the given level.
644 */
645 unsigned r300_texture_get_stride(struct r300_screen* screen,
646 struct r300_texture* tex, unsigned level)
647 {
648 unsigned tile_width, width;
649
650 if (tex->stride_override)
651 return tex->stride_override;
652
653 /* Check the level. */
654 if (level > tex->tex.last_level) {
655 SCREEN_DBG(screen, DBG_TEX, "%s: level (%u) > last_level (%u)\n",
656 __FUNCTION__, level, tex->tex.last_level);
657 return 0;
658 }
659
660 width = u_minify(tex->tex.width0, level);
661
662 if (!util_format_is_compressed(tex->tex.format)) {
663 tile_width = r300_texture_get_tile_size(tex, TILE_WIDTH,
664 tex->mip_macrotile[level]);
665 width = align(width, tile_width);
666
667 return util_format_get_stride(tex->tex.format, width);
668 } else {
669 return align(util_format_get_stride(tex->tex.format, width), 32);
670 }
671 }
672
673 static unsigned r300_texture_get_nblocksy(struct r300_texture* tex,
674 unsigned level)
675 {
676 unsigned height, tile_height;
677
678 height = u_minify(tex->tex.height0, level);
679
680 if (!util_format_is_compressed(tex->tex.format)) {
681 tile_height = r300_texture_get_tile_size(tex, TILE_HEIGHT,
682 tex->mip_macrotile[level]);
683 height = align(height, tile_height);
684 }
685
686 return util_format_get_nblocksy(tex->tex.format, height);
687 }
688
689 static void r300_setup_miptree(struct r300_screen* screen,
690 struct r300_texture* tex)
691 {
692 struct pipe_texture* base = &tex->tex;
693 unsigned stride, size, layer_size, nblocksy, i;
694 boolean rv350_mode = screen->caps->family >= CHIP_FAMILY_RV350;
695
696 SCREEN_DBG(screen, DBG_TEX, "r300: Making miptree for texture, format %s\n",
697 util_format_name(base->format));
698
699 for (i = 0; i <= base->last_level; i++) {
700 /* Let's see if this miplevel can be macrotiled. */
701 tex->mip_macrotile[i] = (tex->macrotile == R300_BUFFER_TILED &&
702 r300_texture_macro_switch(tex, i, rv350_mode)) ?
703 R300_BUFFER_TILED : R300_BUFFER_LINEAR;
704
705 stride = r300_texture_get_stride(screen, tex, i);
706 nblocksy = r300_texture_get_nblocksy(tex, i);
707 layer_size = stride * nblocksy;
708
709 if (base->target == PIPE_TEXTURE_CUBE)
710 size = layer_size * 6;
711 else
712 size = layer_size * u_minify(base->depth0, i);
713
714 tex->offset[i] = tex->size;
715 tex->size = tex->offset[i] + size;
716 tex->layer_size[i] = layer_size;
717 tex->pitch[i] = stride / util_format_get_blocksize(base->format);
718
719 SCREEN_DBG(screen, DBG_TEX, "r300: Texture miptree: Level %d "
720 "(%dx%dx%d px, pitch %d bytes) %d bytes total, macrotiled %s\n",
721 i, u_minify(base->width0, i), u_minify(base->height0, i),
722 u_minify(base->depth0, i), stride, tex->size,
723 tex->mip_macrotile[i] ? "TRUE" : "FALSE");
724 }
725 }
726
727 static void r300_setup_flags(struct r300_texture* tex)
728 {
729 tex->is_npot = !util_is_power_of_two(tex->tex.width0) ||
730 !util_is_power_of_two(tex->tex.height0);
731 }
732
733 /* Create a new texture. */
734 static struct pipe_texture*
735 r300_texture_create(struct pipe_screen* screen,
736 const struct pipe_texture* template)
737 {
738 struct r300_texture* tex = CALLOC_STRUCT(r300_texture);
739 struct r300_screen* rscreen = r300_screen(screen);
740 struct radeon_winsys* winsys = (struct radeon_winsys*)screen->winsys;
741
742 if (!tex) {
743 return NULL;
744 }
745
746 tex->tex = *template;
747 pipe_reference_init(&tex->tex.reference, 1);
748 tex->tex.screen = screen;
749
750 r300_setup_flags(tex);
751 r300_setup_miptree(rscreen, tex);
752 r300_setup_texture_state(rscreen, tex);
753
754 tex->buffer = screen->buffer_create(screen, 2048,
755 PIPE_BUFFER_USAGE_PIXEL,
756 tex->size);
757 winsys->buffer_set_tiling(winsys, tex->buffer,
758 tex->pitch[0],
759 tex->microtile != R300_BUFFER_LINEAR,
760 tex->macrotile != R300_BUFFER_LINEAR);
761
762 if (!tex->buffer) {
763 FREE(tex);
764 return NULL;
765 }
766
767 return (struct pipe_texture*)tex;
768 }
769
770 static void r300_texture_destroy(struct pipe_texture* texture)
771 {
772 struct r300_texture* tex = (struct r300_texture*)texture;
773
774 pipe_buffer_reference(&tex->buffer, NULL);
775
776 FREE(tex);
777 }
778
779 static struct pipe_surface* r300_get_tex_surface(struct pipe_screen* screen,
780 struct pipe_texture* texture,
781 unsigned face,
782 unsigned level,
783 unsigned zslice,
784 unsigned flags)
785 {
786 struct r300_texture* tex = (struct r300_texture*)texture;
787 struct pipe_surface* surface = CALLOC_STRUCT(pipe_surface);
788 unsigned offset;
789
790 offset = r300_texture_get_offset(tex, level, zslice, face);
791
792 if (surface) {
793 pipe_reference_init(&surface->reference, 1);
794 pipe_texture_reference(&surface->texture, texture);
795 surface->format = texture->format;
796 surface->width = u_minify(texture->width0, level);
797 surface->height = u_minify(texture->height0, level);
798 surface->offset = offset;
799 surface->usage = flags;
800 surface->zslice = zslice;
801 surface->texture = texture;
802 surface->face = face;
803 surface->level = level;
804 }
805
806 return surface;
807 }
808
809 static void r300_tex_surface_destroy(struct pipe_surface* s)
810 {
811 pipe_texture_reference(&s->texture, NULL);
812 FREE(s);
813 }
814
815 static struct pipe_texture*
816 r300_texture_blanket(struct pipe_screen* screen,
817 const struct pipe_texture* base,
818 const unsigned* stride,
819 struct pipe_buffer* buffer)
820 {
821 struct r300_texture* tex;
822 struct r300_screen* rscreen = r300_screen(screen);
823
824 /* Support only 2D textures without mipmaps */
825 if (base->target != PIPE_TEXTURE_2D ||
826 base->depth0 != 1 ||
827 base->last_level != 0) {
828 return NULL;
829 }
830
831 tex = CALLOC_STRUCT(r300_texture);
832 if (!tex) {
833 return NULL;
834 }
835
836 tex->tex = *base;
837 pipe_reference_init(&tex->tex.reference, 1);
838 tex->tex.screen = screen;
839
840 tex->stride_override = *stride;
841 tex->pitch[0] = *stride / util_format_get_blocksize(base->format);
842
843 r300_setup_flags(tex);
844 r300_setup_texture_state(rscreen, tex);
845
846 pipe_buffer_reference(&tex->buffer, buffer);
847
848 return (struct pipe_texture*)tex;
849 }
850
851 static struct pipe_video_surface *
852 r300_video_surface_create(struct pipe_screen *screen,
853 enum pipe_video_chroma_format chroma_format,
854 unsigned width, unsigned height)
855 {
856 struct r300_video_surface *r300_vsfc;
857 struct pipe_texture template;
858
859 assert(screen);
860 assert(width && height);
861
862 r300_vsfc = CALLOC_STRUCT(r300_video_surface);
863 if (!r300_vsfc)
864 return NULL;
865
866 pipe_reference_init(&r300_vsfc->base.reference, 1);
867 r300_vsfc->base.screen = screen;
868 r300_vsfc->base.chroma_format = chroma_format;
869 r300_vsfc->base.width = width;
870 r300_vsfc->base.height = height;
871
872 memset(&template, 0, sizeof(struct pipe_texture));
873 template.target = PIPE_TEXTURE_2D;
874 template.format = PIPE_FORMAT_X8R8G8B8_UNORM;
875 template.last_level = 0;
876 template.width0 = util_next_power_of_two(width);
877 template.height0 = util_next_power_of_two(height);
878 template.depth0 = 1;
879 template.tex_usage = PIPE_TEXTURE_USAGE_SAMPLER |
880 PIPE_TEXTURE_USAGE_RENDER_TARGET;
881
882 r300_vsfc->tex = screen->texture_create(screen, &template);
883 if (!r300_vsfc->tex)
884 {
885 FREE(r300_vsfc);
886 return NULL;
887 }
888
889 return &r300_vsfc->base;
890 }
891
892 static void r300_video_surface_destroy(struct pipe_video_surface *vsfc)
893 {
894 struct r300_video_surface *r300_vsfc = r300_video_surface(vsfc);
895 pipe_texture_reference(&r300_vsfc->tex, NULL);
896 FREE(r300_vsfc);
897 }
898
899 void r300_init_screen_texture_functions(struct pipe_screen* screen)
900 {
901 screen->texture_create = r300_texture_create;
902 screen->texture_destroy = r300_texture_destroy;
903 screen->get_tex_surface = r300_get_tex_surface;
904 screen->tex_surface_destroy = r300_tex_surface_destroy;
905 screen->texture_blanket = r300_texture_blanket;
906
907 screen->video_surface_create = r300_video_surface_create;
908 screen->video_surface_destroy= r300_video_surface_destroy;
909 }
910
911 boolean r300_get_texture_buffer(struct pipe_screen* screen,
912 struct pipe_texture* texture,
913 struct pipe_buffer** buffer,
914 unsigned* stride)
915 {
916 struct r300_texture* tex = (struct r300_texture*)texture;
917 if (!tex) {
918 return FALSE;
919 }
920
921 pipe_buffer_reference(buffer, tex->buffer);
922
923 if (stride) {
924 *stride = r300_texture_get_stride(r300_screen(screen), tex, 0);
925 }
926
927 return TRUE;
928 }