radeon_mipmap_tree.c revision 15f5f839b1a52a49bb60e73625b8c6b2f73a75e8
1/* 2 * Copyright (C) 2008 Nicolai Haehnle. 3 * 4 * All Rights Reserved. 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining 7 * a copy of this software and associated documentation files (the 8 * "Software"), to deal in the Software without restriction, including 9 * without limitation the rights to use, copy, modify, merge, publish, 10 * distribute, sublicense, and/or sell copies of the Software, and to 11 * permit persons to whom the Software is furnished to do so, subject to 12 * the following conditions: 13 * 14 * The above copyright notice and this permission notice (including the 15 * next paragraph) shall be included in all copies or substantial 16 * portions of the Software. 17 * 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 21 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE 22 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 24 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 * 26 */ 27 28#include "radeon_mipmap_tree.h" 29 30#include <errno.h> 31#include <unistd.h> 32 33#include "main/simple_list.h" 34#include "main/texcompress.h" 35#include "main/texformat.h" 36 37static GLuint radeon_compressed_texture_size(GLcontext *ctx, 38 GLsizei width, GLsizei height, GLsizei depth, 39 GLuint mesaFormat) 40{ 41 GLuint size = _mesa_compressed_texture_size(ctx, width, height, depth, mesaFormat); 42 43 if (mesaFormat == MESA_FORMAT_RGB_DXT1 || 44 mesaFormat == MESA_FORMAT_RGBA_DXT1) { 45 if (width + 3 < 8) /* width one block */ 46 size = size * 4; 47 else if (width + 3 < 16) 48 size = size * 2; 49 } else { 50 /* DXT3/5, 16 bytes per block */ 51 // WARN_ONCE("DXT 3/5 suffers from multitexturing problems!\n"); 52 if (width + 3 < 8) 53 size = size * 2; 54 } 55 56 return size; 57} 58 59 60static int radeon_compressed_num_bytes(GLuint mesaFormat) 61{ 62 int bytes = 0; 63 switch(mesaFormat) { 64 65 case MESA_FORMAT_RGB_FXT1: 66 case MESA_FORMAT_RGBA_FXT1: 67 case MESA_FORMAT_RGB_DXT1: 68 case MESA_FORMAT_RGBA_DXT1: 69 bytes = 2; 70 break; 71 72 case MESA_FORMAT_RGBA_DXT3: 73 case MESA_FORMAT_RGBA_DXT5: 74 bytes = 4; 75 default: 76 break; 77 } 78 79 return bytes; 80} 81 82/** 83 * Compute sizes and fill in offset and blit information for the given 84 * image (determined by \p face and \p level). 85 * 86 * \param curOffset points to the offset at which the image is to be stored 87 * and is updated by this function according to the size of the image. 88 */ 89static void compute_tex_image_offset(radeonContextPtr rmesa, radeon_mipmap_tree *mt, 90 GLuint face, GLuint level, GLuint* curOffset) 91{ 92 radeon_mipmap_level *lvl = &mt->levels[level]; 93 uint32_t row_align; 94 95 /* Find image size in bytes */ 96 if (mt->compressed) { 97 /* TODO: Is this correct? Need test cases for compressed textures! */ 98 row_align = rmesa->texture_compressed_row_align - 1; 99 lvl->rowstride = (lvl->width * mt->bpp + row_align) & ~row_align; 100 lvl->size = radeon_compressed_texture_size(mt->radeon->glCtx, 101 lvl->width, lvl->height, lvl->depth, mt->compressed); 102 } else if (mt->target == GL_TEXTURE_RECTANGLE_NV) { 103 row_align = rmesa->texture_rect_row_align - 1; 104 lvl->rowstride = (lvl->width * mt->bpp + row_align) & ~row_align; 105 lvl->size = lvl->rowstride * lvl->height; 106 } else if (mt->tilebits & RADEON_TXO_MICRO_TILE) { 107 /* tile pattern is 16 bytes x2. mipmaps stay 32 byte aligned, 108 * though the actual offset may be different (if texture is less than 109 * 32 bytes width) to the untiled case */ 110 lvl->rowstride = (lvl->width * mt->bpp * 2 + 31) & ~31; 111 lvl->size = lvl->rowstride * ((lvl->height + 1) / 2) * lvl->depth; 112 } else { 113 row_align = rmesa->texture_row_align - 1; 114 lvl->rowstride = (lvl->width * mt->bpp + row_align) & ~row_align; 115 lvl->size = lvl->rowstride * lvl->height * lvl->depth; 116 } 117 assert(lvl->size > 0); 118 119 /* All images are aligned to a 32-byte offset */ 120 *curOffset = (*curOffset + 0x1f) & ~0x1f; 121 lvl->faces[face].offset = *curOffset; 122 *curOffset += lvl->size; 123 124 if (RADEON_DEBUG & DEBUG_TEXTURE) 125 fprintf(stderr, 126 "level %d, face %d: rs:%d %dx%d at %d\n", 127 level, face, lvl->rowstride, lvl->width, lvl->height, lvl->faces[face].offset); 128} 129 130static GLuint minify(GLuint size, GLuint levels) 131{ 132 size = size >> levels; 133 if (size < 1) 134 size = 1; 135 return size; 136} 137 138 139static void calculate_miptree_layout_r100(radeonContextPtr rmesa, radeon_mipmap_tree *mt) 140{ 141 GLuint curOffset; 142 GLuint numLevels; 143 GLuint i; 144 GLuint face; 145 146 numLevels = mt->lastLevel - mt->firstLevel + 1; 147 assert(numLevels <= rmesa->glCtx->Const.MaxTextureLevels); 148 149 curOffset = 0; 150 for(face = 0; face < mt->faces; face++) { 151 152 for(i = 0; i < numLevels; i++) { 153 mt->levels[i].width = minify(mt->width0, i); 154 mt->levels[i].height = minify(mt->height0, i); 155 mt->levels[i].depth = minify(mt->depth0, i); 156 compute_tex_image_offset(rmesa, mt, face, i, &curOffset); 157 } 158 } 159 160 /* Note the required size in memory */ 161 mt->totalsize = (curOffset + RADEON_OFFSET_MASK) & ~RADEON_OFFSET_MASK; 162} 163 164static void calculate_miptree_layout_r300(radeonContextPtr rmesa, radeon_mipmap_tree *mt) 165{ 166 GLuint curOffset; 167 GLuint numLevels; 168 GLuint i; 169 170 numLevels = mt->lastLevel - mt->firstLevel + 1; 171 assert(numLevels <= rmesa->glCtx->Const.MaxTextureLevels); 172 173 curOffset = 0; 174 for(i = 0; i < numLevels; i++) { 175 GLuint face; 176 177 mt->levels[i].width = minify(mt->width0, i); 178 mt->levels[i].height = minify(mt->height0, i); 179 mt->levels[i].depth = minify(mt->depth0, i); 180 181 for(face = 0; face < mt->faces; face++) 182 compute_tex_image_offset(rmesa, mt, face, i, &curOffset); 183 } 184 185 /* Note the required size in memory */ 186 mt->totalsize = (curOffset + RADEON_OFFSET_MASK) & ~RADEON_OFFSET_MASK; 187} 188 189/** 190 * Create a new mipmap tree, calculate its layout and allocate memory. 191 */ 192radeon_mipmap_tree* radeon_miptree_create(radeonContextPtr rmesa, radeonTexObj *t, 193 GLenum target, GLuint firstLevel, GLuint lastLevel, 194 GLuint width0, GLuint height0, GLuint depth0, 195 GLuint bpp, GLuint tilebits, GLuint compressed) 196{ 197 radeon_mipmap_tree *mt = CALLOC_STRUCT(_radeon_mipmap_tree); 198 199 mt->radeon = rmesa; 200 mt->refcount = 1; 201 mt->t = t; 202 mt->target = target; 203 mt->faces = (target == GL_TEXTURE_CUBE_MAP) ? 6 : 1; 204 mt->firstLevel = firstLevel; 205 mt->lastLevel = lastLevel; 206 mt->width0 = width0; 207 mt->height0 = height0; 208 mt->depth0 = depth0; 209 mt->bpp = compressed ? radeon_compressed_num_bytes(compressed) : bpp; 210 mt->tilebits = tilebits; 211 mt->compressed = compressed; 212 213 if (rmesa->radeonScreen->chip_family >= CHIP_FAMILY_R300) 214 calculate_miptree_layout_r300(rmesa, mt); 215 else 216 calculate_miptree_layout_r100(rmesa, mt); 217 218#ifdef RADEON_DEBUG_BO 219 mt->bo = radeon_bo_open(rmesa->radeonScreen->bom, 220 0, mt->totalsize, 1024, 221 RADEON_GEM_DOMAIN_VRAM, 222 0, 223 "MIPMAP TREE"); 224#else 225 mt->bo = radeon_bo_open(rmesa->radeonScreen->bom, 226 0, mt->totalsize, 1024, 227 RADEON_GEM_DOMAIN_VRAM, 228 0); 229#endif /* RADEON_DEBUG_BO */ 230 231 return mt; 232} 233 234void radeon_miptree_reference(radeon_mipmap_tree *mt) 235{ 236 mt->refcount++; 237 assert(mt->refcount > 0); 238} 239 240void radeon_miptree_unreference(radeon_mipmap_tree *mt) 241{ 242 if (!mt) 243 return; 244 245 assert(mt->refcount > 0); 246 mt->refcount--; 247 if (!mt->refcount) { 248 radeon_bo_unref(mt->bo); 249 free(mt); 250 } 251} 252 253 254/** 255 * Calculate first and last mip levels for the given texture object, 256 * where the dimensions are taken from the given texture image at 257 * the given level. 258 * 259 * Note: level is the OpenGL level number, which is not necessarily the same 260 * as the first level that is actually present. 261 * 262 * The base level image of the given texture face must be non-null, 263 * or this will fail. 264 */ 265static void calculate_first_last_level(struct gl_texture_object *tObj, 266 GLuint *pfirstLevel, GLuint *plastLevel, 267 GLuint face, GLuint level) 268{ 269 const struct gl_texture_image * const baseImage = 270 tObj->Image[face][level]; 271 272 assert(baseImage); 273 274 /* These must be signed values. MinLod and MaxLod can be negative numbers, 275 * and having firstLevel and lastLevel as signed prevents the need for 276 * extra sign checks. 277 */ 278 int firstLevel; 279 int lastLevel; 280 281 /* Yes, this looks overly complicated, but it's all needed. 282 */ 283 switch (tObj->Target) { 284 case GL_TEXTURE_1D: 285 case GL_TEXTURE_2D: 286 case GL_TEXTURE_3D: 287 case GL_TEXTURE_CUBE_MAP: 288 if (tObj->MinFilter == GL_NEAREST || tObj->MinFilter == GL_LINEAR) { 289 /* GL_NEAREST and GL_LINEAR only care about GL_TEXTURE_BASE_LEVEL. 290 */ 291 firstLevel = lastLevel = tObj->BaseLevel; 292 } else { 293 firstLevel = tObj->BaseLevel + (GLint)(tObj->MinLod + 0.5); 294 firstLevel = MAX2(firstLevel, tObj->BaseLevel); 295 firstLevel = MIN2(firstLevel, level + baseImage->MaxLog2); 296 lastLevel = tObj->BaseLevel + (GLint)(tObj->MaxLod + 0.5); 297 lastLevel = MAX2(lastLevel, tObj->BaseLevel); 298 lastLevel = MIN2(lastLevel, level + baseImage->MaxLog2); 299 lastLevel = MIN2(lastLevel, tObj->MaxLevel); 300 lastLevel = MAX2(firstLevel, lastLevel); /* need at least one level */ 301 } 302 break; 303 case GL_TEXTURE_RECTANGLE_NV: 304 case GL_TEXTURE_4D_SGIS: 305 firstLevel = lastLevel = 0; 306 break; 307 default: 308 return; 309 } 310 311 /* save these values */ 312 *pfirstLevel = firstLevel; 313 *plastLevel = lastLevel; 314} 315 316 317/** 318 * Checks whether the given miptree can hold the given texture image at the 319 * given face and level. 320 */ 321GLboolean radeon_miptree_matches_image(radeon_mipmap_tree *mt, 322 struct gl_texture_image *texImage, GLuint face, GLuint level) 323{ 324 radeon_mipmap_level *lvl; 325 326 if (face >= mt->faces || level < mt->firstLevel || level > mt->lastLevel) 327 return GL_FALSE; 328 329 if ((!texImage->IsCompressed && mt->compressed) || 330 (texImage->IsCompressed && !mt->compressed)) 331 return GL_FALSE; 332 333 if (!texImage->IsCompressed && 334 !mt->compressed && 335 texImage->TexFormat->TexelBytes != mt->bpp) 336 return GL_FALSE; 337 338 lvl = &mt->levels[level - mt->firstLevel]; 339 if (lvl->width != texImage->Width || 340 lvl->height != texImage->Height || 341 lvl->depth != texImage->Depth) 342 return GL_FALSE; 343 344 return GL_TRUE; 345} 346 347 348/** 349 * Checks whether the given miptree has the right format to store the given texture object. 350 */ 351GLboolean radeon_miptree_matches_texture(radeon_mipmap_tree *mt, struct gl_texture_object *texObj) 352{ 353 struct gl_texture_image *firstImage; 354 GLuint compressed; 355 GLuint numfaces = 1; 356 GLuint firstLevel, lastLevel; 357 358 calculate_first_last_level(texObj, &firstLevel, &lastLevel, 0, texObj->BaseLevel); 359 if (texObj->Target == GL_TEXTURE_CUBE_MAP) 360 numfaces = 6; 361 362 firstImage = texObj->Image[0][firstLevel]; 363 compressed = firstImage->IsCompressed ? firstImage->TexFormat->MesaFormat : 0; 364 365 return (mt->firstLevel == firstLevel && 366 mt->lastLevel == lastLevel && 367 mt->width0 == firstImage->Width && 368 mt->height0 == firstImage->Height && 369 mt->depth0 == firstImage->Depth && 370 mt->compressed == compressed && 371 (!mt->compressed ? (mt->bpp == firstImage->TexFormat->TexelBytes) : 1)); 372} 373 374 375/** 376 * Try to allocate a mipmap tree for the given texture that will fit the 377 * given image in the given position. 378 */ 379void radeon_try_alloc_miptree(radeonContextPtr rmesa, radeonTexObj *t, 380 struct gl_texture_image *texImage, GLuint face, GLuint level) 381{ 382 GLuint compressed = texImage->IsCompressed ? texImage->TexFormat->MesaFormat : 0; 383 GLuint numfaces = 1; 384 GLuint firstLevel, lastLevel; 385 386 assert(!t->mt); 387 388 calculate_first_last_level(&t->base, &firstLevel, &lastLevel, face, level); 389 if (t->base.Target == GL_TEXTURE_CUBE_MAP) 390 numfaces = 6; 391 392 if (level != firstLevel || face >= numfaces) 393 return; 394 395 t->mt = radeon_miptree_create(rmesa, t, t->base.Target, 396 firstLevel, lastLevel, 397 texImage->Width, texImage->Height, texImage->Depth, 398 texImage->TexFormat->TexelBytes, t->tile_bits, compressed); 399} 400 401/* Although we use the image_offset[] array to store relative offsets 402 * to cube faces, Mesa doesn't know anything about this and expects 403 * each cube face to be treated as a separate image. 404 * 405 * These functions present that view to mesa: 406 */ 407void 408radeon_miptree_depth_offsets(radeon_mipmap_tree *mt, GLuint level, GLuint *offsets) 409{ 410 if (mt->target != GL_TEXTURE_3D || mt->faces == 1) 411 offsets[0] = 0; 412 else { 413 int i; 414 for (i = 0; i < 6; i++) 415 offsets[i] = mt->levels[level].faces[i].offset; 416 } 417} 418 419GLuint 420radeon_miptree_image_offset(radeon_mipmap_tree *mt, 421 GLuint face, GLuint level) 422{ 423 if (mt->target == GL_TEXTURE_CUBE_MAP_ARB) 424 return (mt->levels[level].faces[face].offset); 425 else 426 return mt->levels[level].faces[0].offset; 427} 428