intel_mipmap_tree.c revision 9087ba128089ed0dc00e6eb38f37126fb7557d3b
1/************************************************************************** 2 * 3 * Copyright 2006 Tungsten Graphics, Inc., Cedar Park, Texas. 4 * All Rights Reserved. 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * 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, sub license, 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 portions 16 * of the Software. 17 * 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. 21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR 22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 * 26 **************************************************************************/ 27 28#include "intel_context.h" 29#include "intel_mipmap_tree.h" 30#include "intel_regions.h" 31#include "intel_tex_layout.h" 32#include "main/enums.h" 33#include "main/formats.h" 34 35#define FILE_DEBUG_FLAG DEBUG_MIPTREE 36 37 38static GLenum 39target_to_target(GLenum target) 40{ 41 switch (target) { 42 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB: 43 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB: 44 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB: 45 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB: 46 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB: 47 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB: 48 return GL_TEXTURE_CUBE_MAP_ARB; 49 default: 50 return target; 51 } 52} 53 54 55static struct intel_mipmap_tree * 56intel_miptree_create_internal(struct intel_context *intel, 57 GLenum target, 58 GLenum internal_format, 59 GLuint first_level, 60 GLuint last_level, 61 GLuint width0, 62 GLuint height0, 63 GLuint depth0, GLuint cpp, GLuint compress_byte, 64 uint32_t tiling) 65{ 66 GLboolean ok; 67 struct intel_mipmap_tree *mt = calloc(sizeof(*mt), 1); 68 69 DBG("%s target %s format %s level %d..%d <-- %p\n", __FUNCTION__, 70 _mesa_lookup_enum_by_nr(target), 71 _mesa_lookup_enum_by_nr(internal_format), 72 first_level, last_level, mt); 73 74 mt->target = target_to_target(target); 75 mt->internal_format = internal_format; 76 mt->first_level = first_level; 77 mt->last_level = last_level; 78 mt->width0 = width0; 79 mt->height0 = height0; 80 mt->depth0 = depth0; 81 mt->cpp = compress_byte ? compress_byte : cpp; 82 mt->compressed = compress_byte ? 1 : 0; 83 mt->refcount = 1; 84 85#ifdef I915 86 if (intel->is_945) 87 ok = i945_miptree_layout(intel, mt, tiling); 88 else 89 ok = i915_miptree_layout(intel, mt, tiling); 90#else 91 ok = brw_miptree_layout(intel, mt, tiling); 92#endif 93 94 if (!ok) { 95 free(mt); 96 DBG("%s not okay - returning NULL\n", __FUNCTION__); 97 return NULL; 98 } 99 100 return mt; 101} 102 103 104struct intel_mipmap_tree * 105intel_miptree_create(struct intel_context *intel, 106 GLenum target, 107 GLenum base_format, 108 GLenum internal_format, 109 GLuint first_level, 110 GLuint last_level, 111 GLuint width0, 112 GLuint height0, 113 GLuint depth0, GLuint cpp, GLuint compress_byte, 114 GLboolean expect_accelerated_upload) 115{ 116 struct intel_mipmap_tree *mt; 117 uint32_t tiling = I915_TILING_NONE; 118 119 if (intel->use_texture_tiling && compress_byte == 0) { 120 if (intel->gen >= 4 && 121 (base_format == GL_DEPTH_COMPONENT || 122 base_format == GL_DEPTH_STENCIL_EXT)) 123 tiling = I915_TILING_Y; 124 else if (width0 >= 64) 125 tiling = I915_TILING_X; 126 } 127 128 mt = intel_miptree_create_internal(intel, target, internal_format, 129 first_level, last_level, width0, 130 height0, depth0, cpp, compress_byte, 131 tiling); 132 /* 133 * pitch == 0 || height == 0 indicates the null texture 134 */ 135 if (!mt || !mt->total_height) { 136 free(mt); 137 return NULL; 138 } 139 140 mt->region = intel_region_alloc(intel->intelScreen, 141 tiling, 142 mt->cpp, 143 mt->total_width, 144 mt->total_height, 145 expect_accelerated_upload); 146 147 if (!mt->region) { 148 free(mt); 149 return NULL; 150 } 151 152 return mt; 153} 154 155 156struct intel_mipmap_tree * 157intel_miptree_create_for_region(struct intel_context *intel, 158 GLenum target, 159 GLenum internal_format, 160 GLuint first_level, 161 GLuint last_level, 162 struct intel_region *region, 163 GLuint depth0, 164 GLuint compress_byte) 165{ 166 struct intel_mipmap_tree *mt; 167 168 mt = intel_miptree_create_internal(intel, target, internal_format, 169 first_level, last_level, 170 region->width, region->height, 1, 171 region->cpp, compress_byte, 172 I915_TILING_NONE); 173 if (!mt) 174 return mt; 175 176 intel_region_reference(&mt->region, region); 177 178 return mt; 179} 180 181void 182intel_miptree_reference(struct intel_mipmap_tree **dst, 183 struct intel_mipmap_tree *src) 184{ 185 src->refcount++; 186 *dst = src; 187 DBG("%s %p refcount now %d\n", __FUNCTION__, src, src->refcount); 188} 189 190 191void 192intel_miptree_release(struct intel_context *intel, 193 struct intel_mipmap_tree **mt) 194{ 195 if (!*mt) 196 return; 197 198 DBG("%s %p refcount will be %d\n", __FUNCTION__, *mt, (*mt)->refcount - 1); 199 if (--(*mt)->refcount <= 0) { 200 GLuint i; 201 202 DBG("%s deleting %p\n", __FUNCTION__, *mt); 203 204 intel_region_release(&((*mt)->region)); 205 206 for (i = 0; i < MAX_TEXTURE_LEVELS; i++) { 207 free((*mt)->level[i].x_offset); 208 free((*mt)->level[i].y_offset); 209 } 210 211 free(*mt); 212 } 213 *mt = NULL; 214} 215 216 217/** 218 * Can the image be pulled into a unified mipmap tree? This mirrors 219 * the completeness test in a lot of ways. 220 * 221 * Not sure whether I want to pass gl_texture_image here. 222 */ 223GLboolean 224intel_miptree_match_image(struct intel_mipmap_tree *mt, 225 struct gl_texture_image *image) 226{ 227 GLboolean isCompressed = _mesa_is_format_compressed(image->TexFormat); 228 struct intel_texture_image *intelImage = intel_texture_image(image); 229 GLuint level = intelImage->level; 230 231 /* Images with borders are never pulled into mipmap trees. */ 232 if (image->Border) 233 return GL_FALSE; 234 235 if (image->InternalFormat != mt->internal_format || 236 isCompressed != mt->compressed) 237 return GL_FALSE; 238 239 if (!isCompressed && 240 !mt->compressed && 241 _mesa_get_format_bytes(image->TexFormat) != mt->cpp) 242 return GL_FALSE; 243 244 /* Test image dimensions against the base level image adjusted for 245 * minification. This will also catch images not present in the 246 * tree, changed targets, etc. 247 */ 248 if (image->Width != mt->level[level].width || 249 image->Height != mt->level[level].height || 250 image->Depth != mt->level[level].depth) 251 return GL_FALSE; 252 253 return GL_TRUE; 254} 255 256 257void 258intel_miptree_set_level_info(struct intel_mipmap_tree *mt, 259 GLuint level, 260 GLuint nr_images, 261 GLuint x, GLuint y, 262 GLuint w, GLuint h, GLuint d) 263{ 264 mt->level[level].width = w; 265 mt->level[level].height = h; 266 mt->level[level].depth = d; 267 mt->level[level].level_x = x; 268 mt->level[level].level_y = y; 269 mt->level[level].nr_images = nr_images; 270 271 DBG("%s level %d size: %d,%d,%d offset %d,%d\n", __FUNCTION__, 272 level, w, h, d, x, y); 273 274 assert(nr_images); 275 assert(!mt->level[level].x_offset); 276 277 mt->level[level].x_offset = malloc(nr_images * sizeof(GLuint)); 278 mt->level[level].x_offset[0] = mt->level[level].level_x; 279 mt->level[level].y_offset = malloc(nr_images * sizeof(GLuint)); 280 mt->level[level].y_offset[0] = mt->level[level].level_y; 281} 282 283 284void 285intel_miptree_set_image_offset(struct intel_mipmap_tree *mt, 286 GLuint level, GLuint img, 287 GLuint x, GLuint y) 288{ 289 if (img == 0 && level == 0) 290 assert(x == 0 && y == 0); 291 292 assert(img < mt->level[level].nr_images); 293 294 mt->level[level].x_offset[img] = mt->level[level].level_x + x; 295 mt->level[level].y_offset[img] = mt->level[level].level_y + y; 296 297 DBG("%s level %d img %d pos %d,%d\n", 298 __FUNCTION__, level, img, 299 mt->level[level].x_offset[img], mt->level[level].y_offset[img]); 300} 301 302 303void 304intel_miptree_get_image_offset(struct intel_mipmap_tree *mt, 305 GLuint level, GLuint face, GLuint depth, 306 GLuint *x, GLuint *y) 307{ 308 if (mt->target == GL_TEXTURE_CUBE_MAP_ARB) { 309 *x = mt->level[level].x_offset[face]; 310 *y = mt->level[level].y_offset[face]; 311 } else if (mt->target == GL_TEXTURE_3D) { 312 *x = mt->level[level].x_offset[depth]; 313 *y = mt->level[level].y_offset[depth]; 314 } else { 315 *x = mt->level[level].x_offset[0]; 316 *y = mt->level[level].y_offset[0]; 317 } 318} 319 320/** 321 * Map a teximage in a mipmap tree. 322 * \param row_stride returns row stride in bytes 323 * \param image_stride returns image stride in bytes (for 3D textures). 324 * \param image_offsets pointer to array of pixel offsets from the returned 325 * pointer to each depth image 326 * \return address of mapping 327 */ 328GLubyte * 329intel_miptree_image_map(struct intel_context * intel, 330 struct intel_mipmap_tree * mt, 331 GLuint face, 332 GLuint level, 333 GLuint * row_stride, GLuint * image_offsets) 334{ 335 GLuint x, y; 336 DBG("%s \n", __FUNCTION__); 337 338 if (row_stride) 339 *row_stride = mt->region->pitch * mt->cpp; 340 341 if (mt->target == GL_TEXTURE_3D) { 342 int i; 343 344 for (i = 0; i < mt->level[level].depth; i++) { 345 346 intel_miptree_get_image_offset(mt, level, face, i, 347 &x, &y); 348 image_offsets[i] = x + y * mt->region->pitch; 349 } 350 351 return intel_region_map(intel, mt->region); 352 } else { 353 assert(mt->level[level].depth == 1); 354 intel_miptree_get_image_offset(mt, level, face, 0, 355 &x, &y); 356 image_offsets[0] = 0; 357 358 return intel_region_map(intel, mt->region) + 359 (x + y * mt->region->pitch) * mt->cpp; 360 } 361} 362 363 364void 365intel_miptree_image_unmap(struct intel_context *intel, 366 struct intel_mipmap_tree *mt) 367{ 368 DBG("%s\n", __FUNCTION__); 369 intel_region_unmap(intel, mt->region); 370} 371 372 373/** 374 * Upload data for a particular image. 375 */ 376void 377intel_miptree_image_data(struct intel_context *intel, 378 struct intel_mipmap_tree *dst, 379 GLuint face, 380 GLuint level, 381 void *src, 382 GLuint src_row_pitch, 383 GLuint src_image_pitch) 384{ 385 const GLuint depth = dst->level[level].depth; 386 GLuint i; 387 388 DBG("%s: %d/%d\n", __FUNCTION__, face, level); 389 for (i = 0; i < depth; i++) { 390 GLuint dst_x, dst_y, height; 391 392 intel_miptree_get_image_offset(dst, level, face, i, &dst_x, &dst_y); 393 394 height = dst->level[level].height; 395 if(dst->compressed) 396 height = (height + 3) / 4; 397 398 intel_region_data(intel, 399 dst->region, 0, dst_x, dst_y, 400 src, 401 src_row_pitch, 402 0, 0, /* source x, y */ 403 dst->level[level].width, height); /* width, height */ 404 405 src = (char *)src + src_image_pitch * dst->cpp; 406 } 407} 408 409 410/** 411 * Copy mipmap image between trees 412 */ 413void 414intel_miptree_image_copy(struct intel_context *intel, 415 struct intel_mipmap_tree *dst, 416 GLuint face, GLuint level, 417 struct intel_mipmap_tree *src) 418{ 419 GLuint width = src->level[level].width; 420 GLuint height = src->level[level].height; 421 GLuint depth = src->level[level].depth; 422 GLuint src_x, src_y, dst_x, dst_y; 423 GLuint i; 424 GLboolean success; 425 426 if (dst->compressed) { 427 GLuint align_w, align_h; 428 429 intel_get_texture_alignment_unit(dst->internal_format, 430 &align_w, &align_h); 431 height = (height + 3) / 4; 432 width = ALIGN(width, align_w); 433 } 434 435 intel_prepare_render(intel); 436 437 for (i = 0; i < depth; i++) { 438 intel_miptree_get_image_offset(src, level, face, i, &src_x, &src_y); 439 intel_miptree_get_image_offset(dst, level, face, i, &dst_x, &dst_y); 440 success = intel_region_copy(intel, 441 dst->region, 0, dst_x, dst_y, 442 src->region, 0, src_x, src_y, 443 width, height, GL_FALSE, 444 GL_COPY); 445 if (!success) { 446 GLubyte *src_ptr, *dst_ptr; 447 448 src_ptr = intel_region_map(intel, src->region); 449 dst_ptr = intel_region_map(intel, dst->region); 450 451 _mesa_copy_rect(dst_ptr, 452 dst->cpp, 453 dst->region->pitch, 454 dst_x, dst_y, width, height, 455 src_ptr, 456 src->region->pitch, 457 src_x, src_y); 458 intel_region_unmap(intel, src->region); 459 intel_region_unmap(intel, dst->region); 460 } 461 } 462} 463