2 * Copyright 2008 Corbin Simpson <MostAwesomeDude@gmail.com>
3 * Copyright 2010 Marek Olšák <maraeo@gmail.com>
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:
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
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. */
24 #include "pipe/p_screen.h"
26 #include "util/u_format.h"
27 #include "util/u_math.h"
28 #include "util/u_memory.h"
30 #include "r300_context.h"
31 #include "r300_texture.h"
32 #include "r300_screen.h"
33 #include "r300_winsys.h"
35 /* XXX Enable float textures here. */
36 /*#define ENABLE_FLOAT_TEXTURES*/
41 static const unsigned microblock_table[5][3][2] = {
42 /*linear tiled square-tiled */
43 {{32, 1}, {8, 4}, {0, 0}}, /* 8 bits per pixel */
44 {{16, 1}, {8, 2}, {4, 4}}, /* 16 bits per pixel */
45 {{ 8, 1}, {4, 2}, {0, 0}}, /* 32 bits per pixel */
46 {{ 4, 1}, {0, 0}, {2, 2}}, /* 64 bits per pixel */
47 {{ 2, 1}, {0, 0}, {0, 0}} /* 128 bits per pixel */
50 /* Return true for non-compressed and non-YUV formats. */
51 static boolean r300_format_is_plain(enum pipe_format format)
53 const struct util_format_description *desc = util_format_description(format);
59 return desc->layout == UTIL_FORMAT_LAYOUT_PLAIN;
62 /* Translate a pipe_format into a useful texture format for sampling.
64 * Some special formats are translated directly using R300_EASY_TX_FORMAT,
65 * but the majority of them is translated in a generic way, automatically
66 * supporting all the formats hw can support.
68 * R300_EASY_TX_FORMAT swizzles the texture.
69 * Note the signature of R300_EASY_TX_FORMAT:
70 * R300_EASY_TX_FORMAT(B, G, R, A, FORMAT);
72 * The FORMAT specifies how the texture sampler will treat the texture, and
73 * makes available X, Y, Z, W, ZERO, and ONE for swizzling. */
74 static uint32_t r300_translate_texformat(enum pipe_format format)
77 const struct util_format_description *desc;
79 boolean uniform = TRUE;
80 const uint32_t swizzle_shift[4] = {
81 R300_TX_FORMAT_R_SHIFT,
82 R300_TX_FORMAT_G_SHIFT,
83 R300_TX_FORMAT_B_SHIFT,
84 R300_TX_FORMAT_A_SHIFT
86 const uint32_t swizzle[4] = {
92 const uint32_t sign_bit[4] = {
93 R300_TX_FORMAT_SIGNED_X,
94 R300_TX_FORMAT_SIGNED_Y,
95 R300_TX_FORMAT_SIGNED_Z,
96 R300_TX_FORMAT_SIGNED_W,
99 desc = util_format_description(format);
101 /* Colorspace (return non-RGB formats directly). */
102 switch (desc->colorspace) {
103 /* Depth stencil formats. */
104 case UTIL_FORMAT_COLORSPACE_ZS:
106 case PIPE_FORMAT_Z16_UNORM:
107 return R300_EASY_TX_FORMAT(X, X, X, X, X16);
108 case PIPE_FORMAT_X8Z24_UNORM:
109 case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
110 return R300_EASY_TX_FORMAT(X, X, X, X, W24_FP);
112 return ~0; /* Unsupported. */
116 case UTIL_FORMAT_COLORSPACE_YUV:
117 result |= R300_TX_FORMAT_YUV_TO_RGB;
120 case PIPE_FORMAT_UYVY:
121 return R300_EASY_TX_FORMAT(X, Y, Z, ONE, YVYU422) | result;
122 case PIPE_FORMAT_YUYV:
123 return R300_EASY_TX_FORMAT(X, Y, Z, ONE, VYUY422) | result;
125 return ~0; /* Unsupported/unknown. */
128 /* Add gamma correction. */
129 case UTIL_FORMAT_COLORSPACE_SRGB:
130 result |= R300_TX_FORMAT_GAMMA;
137 for (i = 0; i < 4; i++) {
138 switch (desc->swizzle[i]) {
139 case UTIL_FORMAT_SWIZZLE_X:
140 case UTIL_FORMAT_SWIZZLE_NONE:
141 result |= swizzle[0] << swizzle_shift[i];
143 case UTIL_FORMAT_SWIZZLE_Y:
144 result |= swizzle[1] << swizzle_shift[i];
146 case UTIL_FORMAT_SWIZZLE_Z:
147 result |= swizzle[2] << swizzle_shift[i];
149 case UTIL_FORMAT_SWIZZLE_W:
150 result |= swizzle[3] << swizzle_shift[i];
152 case UTIL_FORMAT_SWIZZLE_0:
153 result |= R300_TX_FORMAT_ZERO << swizzle_shift[i];
155 case UTIL_FORMAT_SWIZZLE_1:
156 result |= R300_TX_FORMAT_ONE << swizzle_shift[i];
159 return ~0; /* Unsupported. */
164 if (desc->layout == UTIL_FORMAT_LAYOUT_S3TC) {
166 case PIPE_FORMAT_DXT1_RGB:
167 case PIPE_FORMAT_DXT1_RGBA:
168 case PIPE_FORMAT_DXT1_SRGB:
169 case PIPE_FORMAT_DXT1_SRGBA:
170 return R300_TX_FORMAT_DXT1 | result;
171 case PIPE_FORMAT_DXT3_RGBA:
172 case PIPE_FORMAT_DXT3_SRGBA:
173 return R300_TX_FORMAT_DXT3 | result;
174 case PIPE_FORMAT_DXT5_RGBA:
175 case PIPE_FORMAT_DXT5_SRGBA:
176 return R300_TX_FORMAT_DXT5 | result;
178 return ~0; /* Unsupported/unknown. */
183 for (i = 0; i < desc->nr_channels; i++) {
184 if (desc->channel[i].type == UTIL_FORMAT_TYPE_SIGNED) {
185 result |= sign_bit[i];
190 if (desc->layout == UTIL_FORMAT_LAYOUT_RGTC) {
192 case PIPE_FORMAT_RGTC1_UNORM:
193 case PIPE_FORMAT_RGTC1_SNORM:
194 return R500_TX_FORMAT_ATI1N | result;
195 case PIPE_FORMAT_RGTC2_UNORM:
196 case PIPE_FORMAT_RGTC2_SNORM:
197 return R400_TX_FORMAT_ATI2N | result;
199 return ~0; /* Unsupported/unknown. */
203 /* See whether the components are of the same size. */
204 for (i = 1; i < desc->nr_channels; i++) {
205 uniform = uniform && desc->channel[0].size == desc->channel[i].size;
208 /* Non-uniform formats. */
210 switch (desc->nr_channels) {
212 if (desc->channel[0].size == 5 &&
213 desc->channel[1].size == 6 &&
214 desc->channel[2].size == 5) {
215 return R300_TX_FORMAT_Z5Y6X5 | result;
217 if (desc->channel[0].size == 5 &&
218 desc->channel[1].size == 5 &&
219 desc->channel[2].size == 6) {
220 return R300_TX_FORMAT_Z6Y5X5 | result;
222 return ~0; /* Unsupported/unknown. */
225 if (desc->channel[0].size == 5 &&
226 desc->channel[1].size == 5 &&
227 desc->channel[2].size == 5 &&
228 desc->channel[3].size == 1) {
229 return R300_TX_FORMAT_W1Z5Y5X5 | result;
231 if (desc->channel[0].size == 10 &&
232 desc->channel[1].size == 10 &&
233 desc->channel[2].size == 10 &&
234 desc->channel[3].size == 2) {
235 return R300_TX_FORMAT_W2Z10Y10X10 | result;
238 return ~0; /* Unsupported/unknown. */
241 /* And finally, uniform formats. */
242 switch (desc->channel[0].type) {
243 case UTIL_FORMAT_TYPE_UNSIGNED:
244 case UTIL_FORMAT_TYPE_SIGNED:
245 if (!desc->channel[0].normalized &&
246 desc->colorspace != UTIL_FORMAT_COLORSPACE_SRGB) {
250 switch (desc->channel[0].size) {
252 switch (desc->nr_channels) {
254 return R300_TX_FORMAT_Y4X4 | result;
256 return R300_TX_FORMAT_W4Z4Y4X4 | result;
261 switch (desc->nr_channels) {
263 return R300_TX_FORMAT_X8 | result;
265 return R300_TX_FORMAT_Y8X8 | result;
267 return R300_TX_FORMAT_W8Z8Y8X8 | result;
272 switch (desc->nr_channels) {
274 return R300_TX_FORMAT_X16 | result;
276 return R300_TX_FORMAT_Y16X16 | result;
278 return R300_TX_FORMAT_W16Z16Y16X16 | result;
283 #if defined(ENABLE_FLOAT_TEXTURES)
284 case UTIL_FORMAT_TYPE_FLOAT:
285 switch (desc->channel[0].size) {
287 switch (desc->nr_channels) {
289 return R300_TX_FORMAT_16F | result;
291 return R300_TX_FORMAT_16F_16F | result;
293 return R300_TX_FORMAT_16F_16F_16F_16F | result;
298 switch (desc->nr_channels) {
300 return R300_TX_FORMAT_32F | result;
302 return R300_TX_FORMAT_32F_32F | result;
304 return R300_TX_FORMAT_32F_32F_32F_32F | result;
310 return ~0; /* Unsupported/unknown. */
313 static uint32_t r500_tx_format_msb_bit(enum pipe_format format)
316 case PIPE_FORMAT_RGTC1_UNORM:
317 case PIPE_FORMAT_RGTC1_SNORM:
318 return R500_TXFORMAT_MSB;
324 /* Buffer formats. */
326 /* Colorbuffer formats. This is the unswizzled format of the RB3D block's
327 * output. For the swizzling of the targets, check the shader's format. */
328 static uint32_t r300_translate_colorformat(enum pipe_format format)
332 case PIPE_FORMAT_A8_UNORM:
333 case PIPE_FORMAT_I8_UNORM:
334 case PIPE_FORMAT_L8_UNORM:
335 case PIPE_FORMAT_R8_UNORM:
336 case PIPE_FORMAT_R8_SNORM:
337 return R300_COLOR_FORMAT_I8;
339 /* 16-bit buffers. */
340 case PIPE_FORMAT_B5G6R5_UNORM:
341 return R300_COLOR_FORMAT_RGB565;
342 case PIPE_FORMAT_B5G5R5A1_UNORM:
343 case PIPE_FORMAT_B5G5R5X1_UNORM:
344 return R300_COLOR_FORMAT_ARGB1555;
345 case PIPE_FORMAT_B4G4R4A4_UNORM:
346 return R300_COLOR_FORMAT_ARGB4444;
348 /* 32-bit buffers. */
349 case PIPE_FORMAT_B8G8R8A8_UNORM:
350 case PIPE_FORMAT_B8G8R8X8_UNORM:
351 case PIPE_FORMAT_A8R8G8B8_UNORM:
352 case PIPE_FORMAT_X8R8G8B8_UNORM:
353 case PIPE_FORMAT_A8B8G8R8_UNORM:
354 case PIPE_FORMAT_R8G8B8A8_SNORM:
355 case PIPE_FORMAT_X8B8G8R8_UNORM:
356 case PIPE_FORMAT_R8SG8SB8UX8U_NORM:
357 return R300_COLOR_FORMAT_ARGB8888;
358 case PIPE_FORMAT_R10G10B10A2_UNORM:
359 return R500_COLOR_FORMAT_ARGB2101010; /* R5xx-only? */
361 /* 64-bit buffers. */
362 case PIPE_FORMAT_R16G16B16A16_UNORM:
363 case PIPE_FORMAT_R16G16B16A16_SNORM:
364 #if defined(ENABLE_FLOAT_TEXTURES)
365 case PIPE_FORMAT_R16G16B16A16_FLOAT:
367 return R300_COLOR_FORMAT_ARGB16161616;
369 /* 128-bit buffers. */
370 #if defined(ENABLE_FLOAT_TEXTURES)
371 case PIPE_FORMAT_R32G32B32A32_FLOAT:
372 return R300_COLOR_FORMAT_ARGB32323232;
376 case PIPE_FORMAT_UYVY:
377 return R300_COLOR_FORMAT_YVYU;
378 case PIPE_FORMAT_YUYV:
379 return R300_COLOR_FORMAT_VYUY;
381 return ~0; /* Unsupported. */
385 /* Depthbuffer and stencilbuffer. Thankfully, we only support two flavors. */
386 static uint32_t r300_translate_zsformat(enum pipe_format format)
389 /* 16-bit depth, no stencil */
390 case PIPE_FORMAT_Z16_UNORM:
391 return R300_DEPTHFORMAT_16BIT_INT_Z;
392 /* 24-bit depth, ignored stencil */
393 case PIPE_FORMAT_X8Z24_UNORM:
394 /* 24-bit depth, 8-bit stencil */
395 case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
396 return R300_DEPTHFORMAT_24BIT_INT_Z_8BIT_STENCIL;
398 return ~0; /* Unsupported. */
402 /* Shader output formats. This is essentially the swizzle from the shader
405 * Note that formats are stored from C3 to C0. */
406 static uint32_t r300_translate_out_fmt(enum pipe_format format)
408 uint32_t modifier = 0;
410 const struct util_format_description *desc;
411 static const uint32_t sign_bit[4] = {
418 desc = util_format_description(format);
420 /* Specifies how the shader output is written to the fog unit. */
421 if (desc->channel[0].type == UTIL_FORMAT_TYPE_FLOAT) {
422 if (desc->channel[0].size == 32) {
423 modifier |= R300_US_OUT_FMT_C4_32_FP;
425 modifier |= R300_US_OUT_FMT_C4_16_FP;
428 if (desc->channel[0].size == 16) {
429 modifier |= R300_US_OUT_FMT_C4_16;
431 /* C4_8 seems to be used for the formats whose pixel size
433 modifier |= R300_US_OUT_FMT_C4_8;
438 for (i = 0; i < 4; i++)
439 if (desc->channel[i].type == UTIL_FORMAT_TYPE_SIGNED) {
440 modifier |= sign_bit[i];
443 /* Add swizzles and return. */
446 * COLORFORMAT_I8 stores the C2 component. */
447 case PIPE_FORMAT_A8_UNORM:
448 return modifier | R300_C2_SEL_A;
449 case PIPE_FORMAT_I8_UNORM:
450 case PIPE_FORMAT_L8_UNORM:
451 case PIPE_FORMAT_R8_UNORM:
452 case PIPE_FORMAT_R8_SNORM:
453 return modifier | R300_C2_SEL_R;
456 case PIPE_FORMAT_B5G6R5_UNORM:
457 case PIPE_FORMAT_B5G5R5A1_UNORM:
458 case PIPE_FORMAT_B5G5R5X1_UNORM:
459 case PIPE_FORMAT_B4G4R4A4_UNORM:
460 case PIPE_FORMAT_B8G8R8A8_UNORM:
461 case PIPE_FORMAT_B8G8R8X8_UNORM:
463 R300_C0_SEL_B | R300_C1_SEL_G |
464 R300_C2_SEL_R | R300_C3_SEL_A;
467 case PIPE_FORMAT_A8R8G8B8_UNORM:
468 case PIPE_FORMAT_X8R8G8B8_UNORM:
470 R300_C0_SEL_A | R300_C1_SEL_R |
471 R300_C2_SEL_G | R300_C3_SEL_B;
474 case PIPE_FORMAT_A8B8G8R8_UNORM:
475 case PIPE_FORMAT_X8B8G8R8_UNORM:
477 R300_C0_SEL_A | R300_C1_SEL_B |
478 R300_C2_SEL_G | R300_C3_SEL_R;
481 case PIPE_FORMAT_R8G8B8A8_SNORM:
482 case PIPE_FORMAT_R8SG8SB8UX8U_NORM:
483 case PIPE_FORMAT_R10G10B10A2_UNORM:
484 case PIPE_FORMAT_R16G16B16A16_UNORM:
485 case PIPE_FORMAT_R16G16B16A16_SNORM:
486 //case PIPE_FORMAT_R16G16B16A16_FLOAT: /* not in pipe_format */
487 case PIPE_FORMAT_R32G32B32A32_FLOAT:
489 R300_C0_SEL_R | R300_C1_SEL_G |
490 R300_C2_SEL_B | R300_C3_SEL_A;
493 return ~0; /* Unsupported. */
497 boolean r300_is_colorbuffer_format_supported(enum pipe_format format)
499 return r300_translate_colorformat(format) != ~0 &&
500 r300_translate_out_fmt(format) != ~0;
503 boolean r300_is_zs_format_supported(enum pipe_format format)
505 return r300_translate_zsformat(format) != ~0;
508 boolean r300_is_sampler_format_supported(enum pipe_format format)
510 return r300_translate_texformat(format) != ~0;
513 static void r300_setup_texture_state(struct r300_screen* screen, struct r300_texture* tex)
515 struct r300_texture_format_state* state = &tex->state;
516 struct pipe_texture *pt = &tex->tex;
518 boolean is_r500 = screen->caps.is_r500;
520 /* Set sampler state. */
521 state->format0 = R300_TX_WIDTH((pt->width0 - 1) & 0x7ff) |
522 R300_TX_HEIGHT((pt->height0 - 1) & 0x7ff);
524 if (tex->uses_pitch) {
525 /* rectangles love this */
526 state->format0 |= R300_TX_PITCH_EN;
527 state->format2 = (tex->pitch[0] - 1) & 0x1fff;
529 /* power of two textures (3D, mipmaps, and no pitch) */
530 state->format0 |= R300_TX_DEPTH(util_logbase2(pt->depth0) & 0xf);
533 state->format1 = r300_translate_texformat(pt->format);
534 if (pt->target == PIPE_TEXTURE_CUBE) {
535 state->format1 |= R300_TX_FORMAT_CUBIC_MAP;
537 if (pt->target == PIPE_TEXTURE_3D) {
538 state->format1 |= R300_TX_FORMAT_3D;
541 /* large textures on r500 */
544 if (pt->width0 > 2048) {
545 state->format2 |= R500_TXWIDTH_BIT11;
547 if (pt->height0 > 2048) {
548 state->format2 |= R500_TXHEIGHT_BIT11;
550 state->format2 |= r500_tx_format_msb_bit(pt->format);
553 SCREEN_DBG(screen, DBG_TEX, "r300: Set texture state (%dx%d, %d levels)\n",
554 pt->width0, pt->height0, pt->last_level);
556 /* Set framebuffer state. */
557 if (util_format_is_depth_or_stencil(tex->tex.format)) {
558 for (i = 0; i <= tex->tex.last_level; i++) {
559 tex->fb_state.depthpitch[i] =
561 R300_DEPTHMACROTILE(tex->mip_macrotile[i]) |
562 R300_DEPTHMICROTILE(tex->microtile);
564 tex->fb_state.zb_format = r300_translate_zsformat(tex->tex.format);
566 for (i = 0; i <= tex->tex.last_level; i++) {
567 tex->fb_state.colorpitch[i] =
569 r300_translate_colorformat(tex->tex.format) |
570 R300_COLOR_TILE(tex->mip_macrotile[i]) |
571 R300_COLOR_MICROTILE(tex->microtile);
573 tex->fb_state.us_out_fmt = r300_translate_out_fmt(tex->tex.format);
577 void r300_texture_reinterpret_format(struct pipe_screen *screen,
578 struct pipe_texture *tex,
579 enum pipe_format new_format)
581 struct r300_screen *r300screen = r300_screen(screen);
583 SCREEN_DBG(r300screen, DBG_TEX, "r300: Reinterpreting format: %s -> %s\n",
584 util_format_name(tex->format), util_format_name(new_format));
586 tex->format = new_format;
588 r300_setup_texture_state(r300_screen(screen), r300_texture(tex));
591 unsigned r300_texture_get_offset(struct r300_texture* tex, unsigned level,
592 unsigned zslice, unsigned face)
594 unsigned offset = tex->offset[level];
596 switch (tex->tex.target) {
597 case PIPE_TEXTURE_3D:
599 return offset + zslice * tex->layer_size[level];
601 case PIPE_TEXTURE_CUBE:
603 return offset + face * tex->layer_size[level];
606 assert(zslice == 0 && face == 0);
612 * Return the width (dim==TILE_WIDTH) or height (dim==TILE_HEIGHT) of one tile
613 * of the given texture.
615 static unsigned r300_texture_get_tile_size(struct r300_texture* tex,
616 int dim, boolean macrotile)
618 unsigned pixsize, tile_size;
620 pixsize = util_format_get_blocksize(tex->tex.format);
621 tile_size = microblock_table[util_logbase2(pixsize)][tex->microtile][dim];
631 /* Return true if macrotiling should be enabled on the miplevel. */
632 static boolean r300_texture_macro_switch(struct r300_texture *tex,
637 unsigned tile, texdim;
639 tile = r300_texture_get_tile_size(tex, dim, TRUE);
640 if (dim == TILE_WIDTH) {
641 texdim = u_minify(tex->tex.width0, level);
643 texdim = u_minify(tex->tex.height0, level);
646 /* See TX_FILTER1_n.MACRO_SWITCH. */
648 return texdim >= tile;
650 return texdim > tile;
655 * Return the stride, in bytes, of the texture images of the given texture
656 * at the given level.
658 unsigned r300_texture_get_stride(struct r300_screen* screen,
659 struct r300_texture* tex, unsigned level)
661 unsigned tile_width, width;
663 if (tex->stride_override)
664 return tex->stride_override;
666 /* Check the level. */
667 if (level > tex->tex.last_level) {
668 SCREEN_DBG(screen, DBG_TEX, "%s: level (%u) > last_level (%u)\n",
669 __FUNCTION__, level, tex->tex.last_level);
673 width = u_minify(tex->tex.width0, level);
675 if (r300_format_is_plain(tex->tex.format)) {
676 tile_width = r300_texture_get_tile_size(tex, TILE_WIDTH,
677 tex->mip_macrotile[level]);
678 width = align(width, tile_width);
680 return util_format_get_stride(tex->tex.format, width);
682 return align(util_format_get_stride(tex->tex.format, width), 32);
686 static unsigned r300_texture_get_nblocksy(struct r300_texture* tex,
689 unsigned height, tile_height;
691 height = u_minify(tex->tex.height0, level);
693 if (r300_format_is_plain(tex->tex.format)) {
694 tile_height = r300_texture_get_tile_size(tex, TILE_HEIGHT,
695 tex->mip_macrotile[level]);
696 height = align(height, tile_height);
698 /* This is needed for the kernel checker, unfortunately. */
699 height = util_next_power_of_two(height);
702 return util_format_get_nblocksy(tex->tex.format, height);
705 static void r300_setup_miptree(struct r300_screen* screen,
706 struct r300_texture* tex)
708 struct pipe_texture* base = &tex->tex;
709 unsigned stride, size, layer_size, nblocksy, i;
710 boolean rv350_mode = screen->caps.family >= CHIP_FAMILY_RV350;
712 SCREEN_DBG(screen, DBG_TEX, "r300: Making miptree for texture, format %s\n",
713 util_format_name(base->format));
715 for (i = 0; i <= base->last_level; i++) {
716 /* Let's see if this miplevel can be macrotiled. */
717 tex->mip_macrotile[i] =
718 (tex->macrotile == R300_BUFFER_TILED &&
719 r300_texture_macro_switch(tex, i, rv350_mode, TILE_WIDTH) &&
720 r300_texture_macro_switch(tex, i, rv350_mode, TILE_HEIGHT)) ?
721 R300_BUFFER_TILED : R300_BUFFER_LINEAR;
723 stride = r300_texture_get_stride(screen, tex, i);
724 nblocksy = r300_texture_get_nblocksy(tex, i);
725 layer_size = stride * nblocksy;
727 if (base->target == PIPE_TEXTURE_CUBE)
728 size = layer_size * 6;
730 size = layer_size * u_minify(base->depth0, i);
732 tex->offset[i] = tex->size;
733 tex->size = tex->offset[i] + size;
734 tex->layer_size[i] = layer_size;
735 tex->pitch[i] = stride / util_format_get_blocksize(base->format);
737 SCREEN_DBG(screen, DBG_TEX, "r300: Texture miptree: Level %d "
738 "(%dx%dx%d px, pitch %d bytes) %d bytes total, macrotiled %s\n",
739 i, u_minify(base->width0, i), u_minify(base->height0, i),
740 u_minify(base->depth0, i), stride, tex->size,
741 tex->mip_macrotile[i] ? "TRUE" : "FALSE");
745 static void r300_setup_flags(struct r300_texture* tex)
747 tex->uses_pitch = !util_is_power_of_two(tex->tex.width0) ||
748 !util_is_power_of_two(tex->tex.height0) ||
749 tex->stride_override;
752 static void r300_setup_tiling(struct pipe_screen *screen,
753 struct r300_texture *tex)
755 struct r300_winsys_screen *rws = (struct r300_winsys_screen *)screen->winsys;
756 enum pipe_format format = tex->tex.format;
757 boolean rv350_mode = r300_screen(screen)->caps.family >= CHIP_FAMILY_RV350;
759 if (!r300_format_is_plain(format)) {
763 if (tex->tex.width0 == 1 ||
764 tex->tex.height0 == 1) {
768 /* Set microtiling. */
769 switch (util_format_get_blocksize(format)) {
772 tex->microtile = R300_BUFFER_TILED;
777 if (rws->get_value(rws, R300_VID_SQUARE_TILING_SUPPORT)) {
778 tex->microtile = R300_BUFFER_SQUARETILED;
783 /* Set macrotiling. */
784 if (r300_texture_macro_switch(tex, 0, rv350_mode, TILE_WIDTH) &&
785 r300_texture_macro_switch(tex, 0, rv350_mode, TILE_HEIGHT)) {
786 tex->macrotile = R300_BUFFER_TILED;
790 /* Create a new texture. */
791 static struct pipe_texture* r300_texture_create(struct pipe_screen* screen,
792 const struct pipe_texture* base)
794 struct r300_texture* tex = CALLOC_STRUCT(r300_texture);
795 struct r300_screen* rscreen = r300_screen(screen);
796 struct r300_winsys_screen *rws = (struct r300_winsys_screen *)screen->winsys;
803 pipe_reference_init(&tex->tex.reference, 1);
804 tex->tex.screen = screen;
806 r300_setup_flags(tex);
807 if (!(base->tex_usage & R300_TEXTURE_USAGE_TRANSFER) &&
808 !(base->tex_usage & PIPE_TEXTURE_USAGE_SCANOUT)) {
809 r300_setup_tiling(screen, tex);
811 r300_setup_miptree(rscreen, tex);
812 r300_setup_texture_state(rscreen, tex);
814 tex->buffer = rws->buffer_create(rws, 2048,
815 PIPE_BUFFER_USAGE_PIXEL,
817 rws->buffer_set_tiling(rws, tex->buffer,
827 return (struct pipe_texture*)tex;
830 static void r300_texture_destroy(struct pipe_texture* texture)
832 struct r300_texture* tex = r300_texture(texture);
833 struct r300_winsys_screen *rws = (struct r300_winsys_screen *)texture->screen->winsys;
835 rws->buffer_reference(rws, &tex->buffer, NULL);
839 static struct pipe_surface* r300_get_tex_surface(struct pipe_screen* screen,
840 struct pipe_texture* texture,
846 struct r300_texture* tex = r300_texture(texture);
847 struct pipe_surface* surface = CALLOC_STRUCT(pipe_surface);
850 offset = r300_texture_get_offset(tex, level, zslice, face);
853 pipe_reference_init(&surface->reference, 1);
854 pipe_texture_reference(&surface->texture, texture);
855 surface->format = texture->format;
856 surface->width = u_minify(texture->width0, level);
857 surface->height = u_minify(texture->height0, level);
858 surface->offset = offset;
859 surface->usage = flags;
860 surface->zslice = zslice;
861 surface->texture = texture;
862 surface->face = face;
863 surface->level = level;
869 static void r300_tex_surface_destroy(struct pipe_surface* s)
871 pipe_texture_reference(&s->texture, NULL);
876 static struct pipe_texture*
877 r300_texture_from_handle(struct pipe_screen* screen,
878 const struct pipe_texture* base,
879 struct winsys_handle *whandle)
881 struct r300_winsys_screen *rws = (struct r300_winsys_screen*)screen->winsys;
882 struct r300_screen* rscreen = r300_screen(screen);
883 struct r300_winsys_buffer *buffer;
884 struct r300_texture* tex;
886 boolean override_zb_flags;
888 /* Support only 2D textures without mipmaps */
889 if (base->target != PIPE_TEXTURE_2D ||
891 base->last_level != 0) {
895 buffer = rws->buffer_from_handle(rws, screen, whandle, &stride);
900 tex = CALLOC_STRUCT(r300_texture);
906 pipe_reference_init(&tex->tex.reference, 1);
907 tex->tex.screen = screen;
909 tex->stride_override = stride;
911 /* one ref already taken */
912 tex->buffer = buffer;
914 rws->buffer_get_tiling(rws, buffer, &tex->microtile, &tex->macrotile);
915 r300_setup_flags(tex);
917 /* Enforce microtiled zbuffer. */
918 override_zb_flags = util_format_is_depth_or_stencil(base->format) &&
919 tex->microtile == R300_BUFFER_LINEAR;
921 if (override_zb_flags) {
922 switch (util_format_get_blocksize(base->format)) {
924 tex->microtile = R300_BUFFER_TILED;
928 if (rws->get_value(rws, R300_VID_SQUARE_TILING_SUPPORT)) {
929 tex->microtile = R300_BUFFER_SQUARETILED;
935 override_zb_flags = FALSE;
939 r300_setup_miptree(rscreen, tex);
940 r300_setup_texture_state(rscreen, tex);
942 if (override_zb_flags) {
943 rws->buffer_set_tiling(rws, tex->buffer,
948 return (struct pipe_texture*)tex;
952 r300_texture_get_handle(struct pipe_screen* screen,
953 struct pipe_texture *texture,
954 struct winsys_handle *whandle)
956 struct r300_winsys_screen *rws = (struct r300_winsys_screen *)screen->winsys;
957 struct r300_texture* tex = r300_texture(texture);
964 stride = r300_texture_get_stride(r300_screen(screen), tex, 0);
966 rws->buffer_get_handle(rws, tex->buffer, stride, whandle);
971 static struct pipe_video_surface *
972 r300_video_surface_create(struct pipe_screen *screen,
973 enum pipe_video_chroma_format chroma_format,
974 unsigned width, unsigned height)
976 struct r300_video_surface *r300_vsfc;
977 struct pipe_texture base;
980 assert(width && height);
982 r300_vsfc = CALLOC_STRUCT(r300_video_surface);
986 pipe_reference_init(&r300_vsfc->base.reference, 1);
987 r300_vsfc->base.screen = screen;
988 r300_vsfc->base.chroma_format = chroma_format;
989 r300_vsfc->base.width = width;
990 r300_vsfc->base.height = height;
992 memset(&base, 0, sizeof(struct pipe_texture));
993 base.target = PIPE_TEXTURE_2D;
994 base.format = PIPE_FORMAT_B8G8R8X8_UNORM;
996 base.width0 = util_next_power_of_two(width);
997 base.height0 = util_next_power_of_two(height);
999 base.tex_usage = PIPE_TEXTURE_USAGE_SAMPLER |
1000 PIPE_TEXTURE_USAGE_RENDER_TARGET;
1002 r300_vsfc->tex = screen->texture_create(screen, &base);
1003 if (!r300_vsfc->tex)
1009 return &r300_vsfc->base;
1012 static void r300_video_surface_destroy(struct pipe_video_surface *vsfc)
1014 struct r300_video_surface *r300_vsfc = r300_video_surface(vsfc);
1015 pipe_texture_reference(&r300_vsfc->tex, NULL);
1019 void r300_init_screen_texture_functions(struct pipe_screen* screen)
1021 screen->texture_create = r300_texture_create;
1022 screen->texture_from_handle = r300_texture_from_handle;
1023 screen->texture_get_handle = r300_texture_get_handle;
1024 screen->texture_destroy = r300_texture_destroy;
1025 screen->get_tex_surface = r300_get_tex_surface;
1026 screen->tex_surface_destroy = r300_tex_surface_destroy;
1028 screen->video_surface_create = r300_video_surface_create;
1029 screen->video_surface_destroy= r300_video_surface_destroy;
1032 boolean r300_get_texture_buffer(struct pipe_screen* screen,
1033 struct pipe_texture* texture,
1034 struct r300_winsys_buffer** buffer,
1037 struct r300_texture* tex = r300_texture(texture);
1038 struct r300_winsys_screen *rws = (struct r300_winsys_screen *)screen->winsys;
1039 struct r300_winsys_buffer *buf;
1045 rws->buffer_reference(rws, &buf, tex->buffer);
1048 *stride = r300_texture_get_stride(r300_screen(screen), tex, 0);