radeon_mipmap_tree.c revision dc8a707c672918b88dd4135930bef60ed148d8ce
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/** 60 * Compute sizes and fill in offset and blit information for the given 61 * image (determined by \p face and \p level). 62 * 63 * \param curOffset points to the offset at which the image is to be stored 64 * and is updated by this function according to the size of the image. 65 */ 66static void compute_tex_image_offset(radeon_mipmap_tree *mt, 67 GLuint face, GLuint level, GLuint* curOffset) 68{ 69 radeon_mipmap_level *lvl = &mt->levels[level]; 70 71 /* Find image size in bytes */ 72 if (mt->compressed) { 73 /* TODO: Is this correct? Need test cases for compressed textures! */ 74 GLuint align; 75 76 if (mt->target == GL_TEXTURE_RECTANGLE_NV) 77 align = 64 / mt->bpp; 78 else 79 align = 32 / mt->bpp; 80 lvl->rowstride = (lvl->width + align - 1) & ~(align - 1); 81 lvl->size = radeon_compressed_texture_size(mt->radeon->glCtx, 82 lvl->width, lvl->height, lvl->depth, mt->compressed); 83 } else if (mt->target == GL_TEXTURE_RECTANGLE_NV) { 84 lvl->rowstride = (lvl->width * mt->bpp + 63) & ~63; 85 lvl->size = lvl->rowstride * lvl->height; 86 } else if (mt->tilebits & RADEON_TXO_MICRO_TILE) { 87 /* tile pattern is 16 bytes x2. mipmaps stay 32 byte aligned, 88 * though the actual offset may be different (if texture is less than 89 * 32 bytes width) to the untiled case */ 90 lvl->rowstride = (lvl->width * mt->bpp * 2 + 31) & ~31; 91 lvl->size = lvl->rowstride * ((lvl->height + 1) / 2) * lvl->depth; 92 } else { 93 lvl->rowstride = (lvl->width * mt->bpp + 31) & ~31; 94 lvl->size = lvl->rowstride * lvl->height * lvl->depth; 95 } 96 assert(lvl->size > 0); 97 98 /* All images are aligned to a 32-byte offset */ 99 *curOffset = (*curOffset + 0x1f) & ~0x1f; 100 lvl->faces[face].offset = *curOffset; 101 *curOffset += lvl->size; 102 103 if (RADEON_DEBUG & DEBUG_TEXTURE) 104 fprintf(stderr, 105 "level %d, face %d: rs:%d %dx%d at %d\n", 106 level, face, lvl->rowstride, lvl->width, lvl->height, lvl->faces[face].offset); 107} 108 109static GLuint minify(GLuint size, GLuint levels) 110{ 111 size = size >> levels; 112 if (size < 1) 113 size = 1; 114 return size; 115} 116 117static void calculate_miptree_layout(radeon_mipmap_tree *mt) 118{ 119 GLuint curOffset; 120 GLuint numLevels; 121 GLuint i; 122 123 numLevels = mt->lastLevel - mt->firstLevel + 1; 124 assert(numLevels <= RADEON_MAX_TEXTURE_LEVELS); 125 126 curOffset = 0; 127 for(i = 0; i < numLevels; i++) { 128 GLuint face; 129 130 mt->levels[i].width = minify(mt->width0, i); 131 mt->levels[i].height = minify(mt->height0, i); 132 mt->levels[i].depth = minify(mt->depth0, i); 133 134 for(face = 0; face < mt->faces; face++) 135 compute_tex_image_offset(mt, face, i, &curOffset); 136 } 137 138 /* Note the required size in memory */ 139 mt->totalsize = (curOffset + RADEON_OFFSET_MASK) & ~RADEON_OFFSET_MASK; 140} 141 142 143/** 144 * Create a new mipmap tree, calculate its layout and allocate memory. 145 */ 146radeon_mipmap_tree* radeon_miptree_create(radeonContextPtr rmesa, radeonTexObj *t, 147 GLenum target, GLuint firstLevel, GLuint lastLevel, 148 GLuint width0, GLuint height0, GLuint depth0, 149 GLuint bpp, GLuint tilebits, GLuint compressed) 150{ 151 radeon_mipmap_tree *mt = CALLOC_STRUCT(_radeon_mipmap_tree); 152 153 mt->radeon = rmesa; 154 mt->refcount = 1; 155 mt->t = t; 156 mt->target = target; 157 mt->faces = (target == GL_TEXTURE_CUBE_MAP) ? 6 : 1; 158 mt->firstLevel = firstLevel; 159 mt->lastLevel = lastLevel; 160 mt->width0 = width0; 161 mt->height0 = height0; 162 mt->depth0 = depth0; 163 mt->bpp = bpp; 164 mt->tilebits = tilebits; 165 mt->compressed = compressed; 166 167 calculate_miptree_layout(mt); 168 169 mt->bo = radeon_bo_open(rmesa->radeonScreen->bom, 170 0, mt->totalsize, 1024, 171 RADEON_GEM_DOMAIN_VRAM, 172 0); 173 174 return mt; 175} 176 177void radeon_miptree_reference(radeon_mipmap_tree *mt) 178{ 179 mt->refcount++; 180 assert(mt->refcount > 0); 181} 182 183void radeon_miptree_unreference(radeon_mipmap_tree *mt) 184{ 185 if (!mt) 186 return; 187 188 assert(mt->refcount > 0); 189 mt->refcount--; 190 if (!mt->refcount) { 191 radeon_bo_unref(mt->bo); 192 free(mt); 193 } 194} 195 196 197static void calculate_first_last_level(struct gl_texture_object *tObj, 198 GLuint *pfirstLevel, GLuint *plastLevel) 199{ 200 const struct gl_texture_image * const baseImage = 201 tObj->Image[0][tObj->BaseLevel]; 202 203 /* These must be signed values. MinLod and MaxLod can be negative numbers, 204 * and having firstLevel and lastLevel as signed prevents the need for 205 * extra sign checks. 206 */ 207 int firstLevel; 208 int lastLevel; 209 210 /* Yes, this looks overly complicated, but it's all needed. 211 */ 212 switch (tObj->Target) { 213 case GL_TEXTURE_1D: 214 case GL_TEXTURE_2D: 215 case GL_TEXTURE_3D: 216 case GL_TEXTURE_CUBE_MAP: 217 if (tObj->MinFilter == GL_NEAREST || tObj->MinFilter == GL_LINEAR) { 218 /* GL_NEAREST and GL_LINEAR only care about GL_TEXTURE_BASE_LEVEL. 219 */ 220 firstLevel = lastLevel = tObj->BaseLevel; 221 } else { 222 firstLevel = tObj->BaseLevel + (GLint)(tObj->MinLod + 0.5); 223 firstLevel = MAX2(firstLevel, tObj->BaseLevel); 224 firstLevel = MIN2(firstLevel, tObj->BaseLevel + baseImage->MaxLog2); 225 lastLevel = tObj->BaseLevel + (GLint)(tObj->MaxLod + 0.5); 226 lastLevel = MAX2(lastLevel, tObj->BaseLevel); 227 lastLevel = MIN2(lastLevel, tObj->BaseLevel + baseImage->MaxLog2); 228 lastLevel = MIN2(lastLevel, tObj->MaxLevel); 229 lastLevel = MAX2(firstLevel, lastLevel); /* need at least one level */ 230 } 231 break; 232 case GL_TEXTURE_RECTANGLE_NV: 233 case GL_TEXTURE_4D_SGIS: 234 firstLevel = lastLevel = 0; 235 break; 236 default: 237 return; 238 } 239 240 /* save these values */ 241 *pfirstLevel = firstLevel; 242 *plastLevel = lastLevel; 243} 244 245 246/** 247 * Checks whether the given miptree can hold the given texture image at the 248 * given face and level. 249 */ 250GLboolean radeon_miptree_matches_image(radeon_mipmap_tree *mt, 251 struct gl_texture_image *texImage, GLuint face, GLuint level) 252{ 253 radeon_mipmap_level *lvl; 254 255 if (face >= mt->faces || level < mt->firstLevel || level > mt->lastLevel) 256 return GL_FALSE; 257 258 if (texImage->TexFormat->TexelBytes != mt->bpp) 259 return GL_FALSE; 260 261 lvl = &mt->levels[level - mt->firstLevel]; 262 if (lvl->width != texImage->Width || 263 lvl->height != texImage->Height || 264 lvl->depth != texImage->Depth) 265 return GL_FALSE; 266 267 return GL_TRUE; 268} 269 270 271/** 272 * Checks whether the given miptree has the right format to store the given texture object. 273 */ 274GLboolean radeon_miptree_matches_texture(radeon_mipmap_tree *mt, struct gl_texture_object *texObj) 275{ 276 struct gl_texture_image *firstImage; 277 GLuint compressed; 278 GLuint numfaces = 1; 279 GLuint firstLevel, lastLevel; 280 281 calculate_first_last_level(texObj, &firstLevel, &lastLevel); 282 if (texObj->Target == GL_TEXTURE_CUBE_MAP) 283 numfaces = 6; 284 285 firstImage = texObj->Image[0][firstLevel]; 286 compressed = firstImage->IsCompressed ? firstImage->TexFormat->MesaFormat : 0; 287 288 return (mt->firstLevel == firstLevel && 289 mt->lastLevel == lastLevel && 290 mt->width0 == firstImage->Width && 291 mt->height0 == firstImage->Height && 292 mt->depth0 == firstImage->Depth && 293 mt->bpp == firstImage->TexFormat->TexelBytes && 294 mt->compressed == compressed); 295} 296 297 298/** 299 * Try to allocate a mipmap tree for the given texture that will fit the 300 * given image in the given position. 301 */ 302void radeon_try_alloc_miptree(radeonContextPtr rmesa, radeonTexObj *t, 303 struct gl_texture_image *texImage, GLuint face, GLuint level) 304{ 305 GLuint compressed = texImage->IsCompressed ? texImage->TexFormat->MesaFormat : 0; 306 GLuint numfaces = 1; 307 GLuint firstLevel, lastLevel; 308 309 assert(!t->mt); 310 311 calculate_first_last_level(&t->base, &firstLevel, &lastLevel); 312 if (t->base.Target == GL_TEXTURE_CUBE_MAP) 313 numfaces = 6; 314 315 if (level != firstLevel || face >= numfaces) 316 return; 317 318 t->mt = radeon_miptree_create(rmesa, t, t->base.Target, 319 firstLevel, lastLevel, 320 texImage->Width, texImage->Height, texImage->Depth, 321 texImage->TexFormat->TexelBytes, t->tile_bits, compressed); 322} 323