intel_mipmap_tree.c revision c932e21fa848562325f666dce5db3b09bc61bffa
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_chipset.h" 32#ifndef I915 33#include "brw_state.h" 34#endif 35#include "main/enums.h" 36 37#define FILE_DEBUG_FLAG DEBUG_MIPTREE 38 39static GLenum 40target_to_target(GLenum target) 41{ 42 switch (target) { 43 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB: 44 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB: 45 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB: 46 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB: 47 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB: 48 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB: 49 return GL_TEXTURE_CUBE_MAP_ARB; 50 default: 51 return target; 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 mt->pitch = 0; 85 86#ifdef I915 87 if (IS_945(intel->intelScreen->deviceID)) 88 ok = i945_miptree_layout(intel, mt, tiling); 89 else 90 ok = i915_miptree_layout(intel, mt, tiling); 91#else 92 ok = brw_miptree_layout(intel, mt, tiling); 93#endif 94 95 if (!ok) { 96 free(mt); 97 DBG("%s not okay - returning NULL\n", __FUNCTION__); 98 return NULL; 99 } 100 101 return mt; 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; 118 119 if (intel->use_texture_tiling && compress_byte == 0 && 120 intel->intelScreen->kernel_exec_fencing) { 121 if (IS_965(intel->intelScreen->deviceID) && 122 (base_format == GL_DEPTH_COMPONENT || 123 base_format == GL_DEPTH_STENCIL_EXT)) 124 tiling = I915_TILING_Y; 125 else 126 tiling = I915_TILING_X; 127 } else 128 tiling = I915_TILING_NONE; 129 130 mt = intel_miptree_create_internal(intel, target, internal_format, 131 first_level, last_level, width0, 132 height0, depth0, cpp, compress_byte, 133 tiling); 134 /* 135 * pitch == 0 || height == 0 indicates the null texture 136 */ 137 if (!mt || !mt->pitch || !mt->total_height) 138 return NULL; 139 140 mt->region = intel_region_alloc(intel, 141 tiling, 142 mt->cpp, 143 mt->pitch, 144 mt->total_height, 145 mt->pitch, 146 expect_accelerated_upload); 147 148 if (!mt->region) { 149 free(mt); 150 return NULL; 151 } 152 153 return mt; 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#if 0 176 if (mt->pitch != region->pitch) { 177 fprintf(stderr, 178 "region pitch (%d) doesn't match mipmap tree pitch (%d)\n", 179 region->pitch, mt->pitch); 180 free(mt); 181 return NULL; 182 } 183#else 184 /* The mipmap tree pitch is aligned to 64 bytes to make sure render 185 * to texture works, but we don't need that for texturing from a 186 * pixmap. Just override it here. */ 187 mt->pitch = region->pitch; 188#endif 189 190 intel_region_reference(&mt->region, region); 191 192 return mt; 193 } 194 195/** 196 * intel_miptree_pitch_align: 197 * 198 * @intel: intel context pointer 199 * 200 * @mt: the miptree to compute pitch alignment for 201 * 202 * @pitch: the natural pitch value 203 * 204 * Given @pitch, compute a larger value which accounts for 205 * any necessary alignment required by the device 206 */ 207 208int intel_miptree_pitch_align (struct intel_context *intel, 209 struct intel_mipmap_tree *mt, 210 uint32_t tiling, 211 int pitch) 212{ 213#ifdef I915 214 GLcontext *ctx = &intel->ctx; 215#endif 216 217 if (!mt->compressed) { 218 int pitch_align; 219 220 if (intel->ttm) { 221 /* XXX: Align pitch to multiple of 64 bytes for now to allow 222 * render-to-texture to work in all cases. This should probably be 223 * replaced at some point by some scheme to only do this when really 224 * necessary. 225 */ 226 pitch_align = 64; 227 } else { 228 pitch_align = 4; 229 } 230 231 if (tiling == I915_TILING_X) 232 pitch_align = 512; 233 else if (tiling == I915_TILING_Y) 234 pitch_align = 128; 235 236 pitch = ALIGN(pitch * mt->cpp, pitch_align); 237 238#ifdef I915 239 /* XXX: At least the i915 seems very upset when the pitch is a multiple 240 * of 1024 and sometimes 512 bytes - performance can drop by several 241 * times. Go to the next multiple of the required alignment for now. 242 */ 243 if (!(pitch & 511) && 244 (pitch + pitch_align) < (1 << ctx->Const.MaxTextureLevels)) 245 pitch += pitch_align; 246#endif 247 248 pitch /= mt->cpp; 249 } 250 return pitch; 251} 252 253void 254intel_miptree_reference(struct intel_mipmap_tree **dst, 255 struct intel_mipmap_tree *src) 256{ 257 src->refcount++; 258 *dst = src; 259 DBG("%s %p refcount now %d\n", __FUNCTION__, src, src->refcount); 260} 261 262void 263intel_miptree_release(struct intel_context *intel, 264 struct intel_mipmap_tree **mt) 265{ 266 if (!*mt) 267 return; 268 269 DBG("%s %p refcount will be %d\n", __FUNCTION__, *mt, (*mt)->refcount - 1); 270 if (--(*mt)->refcount <= 0) { 271 GLuint i; 272 273 DBG("%s deleting %p\n", __FUNCTION__, *mt); 274 275#ifndef I915 276 /* Free up cached binding tables holding a reference on our buffer, to 277 * avoid excessive memory consumption. 278 * 279 * This isn't as aggressive as we could be, as we'd like to do 280 * it from any time we free the last ref on a region. But intel_region.c 281 * is context-agnostic. Perhaps our constant state cache should be, as 282 * well. 283 */ 284 brw_state_cache_bo_delete(&brw_context(&intel->ctx)->surface_cache, 285 (*mt)->region->buffer); 286#endif 287 288 intel_region_release(&((*mt)->region)); 289 290 for (i = 0; i < MAX_TEXTURE_LEVELS; i++) 291 if ((*mt)->level[i].image_offset) 292 free((*mt)->level[i].image_offset); 293 294 free(*mt); 295 } 296 *mt = NULL; 297} 298 299 300 301 302/* Can the image be pulled into a unified mipmap tree. This mirrors 303 * the completeness test in a lot of ways. 304 * 305 * Not sure whether I want to pass gl_texture_image here. 306 */ 307GLboolean 308intel_miptree_match_image(struct intel_mipmap_tree *mt, 309 struct gl_texture_image *image, 310 GLuint face, GLuint level) 311{ 312 /* Images with borders are never pulled into mipmap trees. 313 */ 314 if (image->Border || 315 ((image->_BaseFormat == GL_DEPTH_COMPONENT) && 316 ((image->TexObject->WrapS == GL_CLAMP_TO_BORDER) || 317 (image->TexObject->WrapT == GL_CLAMP_TO_BORDER)))) 318 return GL_FALSE; 319 320 if (image->InternalFormat != mt->internal_format || 321 image->IsCompressed != mt->compressed) 322 return GL_FALSE; 323 324 if (!image->IsCompressed && 325 !mt->compressed && 326 image->TexFormat->TexelBytes != mt->cpp) 327 return GL_FALSE; 328 329 /* Test image dimensions against the base level image adjusted for 330 * minification. This will also catch images not present in the 331 * tree, changed targets, etc. 332 */ 333 if (image->Width != mt->level[level].width || 334 image->Height != mt->level[level].height || 335 image->Depth != mt->level[level].depth) 336 return GL_FALSE; 337 338 return GL_TRUE; 339} 340 341 342void 343intel_miptree_set_level_info(struct intel_mipmap_tree *mt, 344 GLuint level, 345 GLuint nr_images, 346 GLuint x, GLuint y, 347 GLuint w, GLuint h, GLuint d) 348{ 349 mt->level[level].width = w; 350 mt->level[level].height = h; 351 mt->level[level].depth = d; 352 mt->level[level].level_offset = (x + y * mt->pitch) * mt->cpp; 353 mt->level[level].nr_images = nr_images; 354 355 DBG("%s level %d size: %d,%d,%d offset %d,%d (0x%x)\n", __FUNCTION__, 356 level, w, h, d, x, y, mt->level[level].level_offset); 357 358 /* Not sure when this would happen, but anyway: 359 */ 360 if (mt->level[level].image_offset) { 361 free(mt->level[level].image_offset); 362 mt->level[level].image_offset = NULL; 363 } 364 365 assert(nr_images); 366 367 mt->level[level].image_offset = malloc(nr_images * sizeof(GLuint)); 368 mt->level[level].image_offset[0] = 0; 369} 370 371 372void 373intel_miptree_set_image_offset_ex(struct intel_mipmap_tree *mt, 374 GLuint level, GLuint img, 375 GLuint x, GLuint y, 376 GLuint offset) 377{ 378 if (img == 0 && level == 0) 379 assert(x == 0 && y == 0); 380 381 assert(img < mt->level[level].nr_images); 382 383 mt->level[level].image_offset[img] = (x + y * mt->pitch) * mt->cpp + offset; 384 385 DBG("%s level %d img %d pos %d,%d image_offset %x\n", 386 __FUNCTION__, level, img, x, y, mt->level[level].image_offset[img]); 387} 388 389void 390intel_miptree_set_image_offset(struct intel_mipmap_tree *mt, 391 GLuint level, GLuint img, 392 GLuint x, GLuint y) 393{ 394 intel_miptree_set_image_offset_ex(mt, level, img, x, y, 0); 395} 396 397 398/** 399 * Return offset to the start of a 2D slice of a texture (a mipmap level, 400 * cube face, 3D Z slice). 401 * \param mt the texture object/miptree 402 * \param face cube map face in [0,5] or zero for non-cube textures 403 * \param level mipmap level 404 * \param zslice Z slice of a 3D texture, or zero for non-3D textures 405 */ 406GLuint 407intel_miptree_image_offset(const struct intel_mipmap_tree *mt, 408 GLuint face, GLuint level, GLuint zslice) 409{ 410 GLuint offset = mt->level[level].level_offset; 411 412 if (mt->target == GL_TEXTURE_CUBE_MAP_ARB) 413 offset += mt->level[level].image_offset[face]; 414 else if (mt->target == GL_TEXTURE_3D) 415 offset += mt->level[level].image_offset[zslice]; 416 417 return offset; 418} 419 420 421 422/** 423 * Map a teximage in a mipmap tree. 424 * \param row_stride returns row stride in bytes 425 * \param image_stride returns image stride in bytes (for 3D textures). 426 * \param image_offsets pointer to array of pixel offsets from the returned 427 * pointer to each depth image 428 * \return address of mapping 429 */ 430GLubyte * 431intel_miptree_image_map(struct intel_context * intel, 432 struct intel_mipmap_tree * mt, 433 GLuint face, 434 GLuint level, 435 GLuint * row_stride, GLuint * image_offsets) 436{ 437 DBG("%s \n", __FUNCTION__); 438 439 if (row_stride) 440 *row_stride = mt->pitch * mt->cpp; 441 442 if (mt->target == GL_TEXTURE_3D) { 443 int i; 444 445 for (i = 0; i < mt->level[level].depth; i++) 446 image_offsets[i] = mt->level[level].image_offset[i] / mt->cpp; 447 } else { 448 assert(mt->level[level].depth == 1); 449 assert(mt->target == GL_TEXTURE_CUBE_MAP || 450 mt->level[level].image_offset[0] == 0); 451 image_offsets[0] = 0; 452 } 453 454 return (intel_region_map(intel, mt->region) + 455 intel_miptree_image_offset(mt, face, level, 0)); 456} 457 458void 459intel_miptree_image_unmap(struct intel_context *intel, 460 struct intel_mipmap_tree *mt) 461{ 462 DBG("%s\n", __FUNCTION__); 463 intel_region_unmap(intel, mt->region); 464} 465 466 467 468/* Upload data for a particular image. 469 */ 470void 471intel_miptree_image_data(struct intel_context *intel, 472 struct intel_mipmap_tree *dst, 473 GLuint face, 474 GLuint level, 475 void *src, 476 GLuint src_row_pitch, 477 GLuint src_image_pitch) 478{ 479 const GLuint depth = dst->level[level].depth; 480 GLuint i; 481 482 DBG("%s: %d/%d\n", __FUNCTION__, face, level); 483 for (i = 0; i < depth; i++) { 484 GLuint dst_offset = intel_miptree_image_offset(dst, face, level, i); 485 GLuint height = dst->level[level].height; 486 487 if (dst->compressed) 488 height = (height + 3) / 4; 489 490 intel_region_data(intel, 491 dst->region, 492 dst_offset, 493 0, 0, /* dstx, dsty */ 494 src, 495 src_row_pitch, 496 0, 0, /* source x, y */ 497 dst->level[level].width, height); /* width, height */ 498 499 src = (char *)src + src_image_pitch * dst->cpp; 500 } 501} 502 503extern void intel_get_texture_alignment_unit(GLenum, GLuint *, GLuint *); 504/* Copy mipmap image between trees 505 */ 506void 507intel_miptree_image_copy(struct intel_context *intel, 508 struct intel_mipmap_tree *dst, 509 GLuint face, GLuint level, 510 struct intel_mipmap_tree *src) 511{ 512 GLuint width = src->level[level].width; 513 GLuint height = src->level[level].height; 514 GLuint depth = src->level[level].depth; 515 GLuint i; 516 GLboolean success; 517 518 if (dst->compressed) { 519 GLuint align_w, align_h; 520 521 intel_get_texture_alignment_unit(dst->internal_format, &align_w, &align_h); 522 height = (height + 3) / 4; 523 width = ALIGN(width, align_w); 524 } 525 526 for (i = 0; i < depth; i++) { 527 GLuint dst_offset = intel_miptree_image_offset(dst, face, level, i); 528 GLuint src_offset = intel_miptree_image_offset(src, face, level, i); 529 530 success = intel_region_copy(intel, 531 dst->region, dst_offset, 532 0, 0, 533 src->region, src_offset, 534 0, 0, width, height, GL_COPY); 535 if (!success) { 536 GLubyte *src_ptr, *dst_ptr; 537 538 src_ptr = intel_region_map(intel, src->region); 539 dst_ptr = intel_region_map(intel, dst->region); 540 541 _mesa_copy_rect(dst_ptr + dst_offset, 542 dst->cpp, 543 dst->pitch, 544 0, 0, width, height, 545 src_ptr + src_offset, 546 src->pitch, 547 0, 0); 548 intel_region_unmap(intel, src->region); 549 intel_region_unmap(intel, dst->region); 550 } 551 } 552} 553