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