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