1/** 2 * \file texobj.c 3 * Texture object management. 4 */ 5 6/* 7 * Mesa 3-D graphics library 8 * 9 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved. 10 * 11 * Permission is hereby granted, free of charge, to any person obtaining a 12 * copy of this software and associated documentation files (the "Software"), 13 * to deal in the Software without restriction, including without limitation 14 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 15 * and/or sell copies of the Software, and to permit persons to whom the 16 * Software is furnished to do so, subject to the following conditions: 17 * 18 * The above copyright notice and this permission notice shall be included 19 * in all copies or substantial portions of the Software. 20 * 21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 22 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 24 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 25 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 26 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 27 * OTHER DEALINGS IN THE SOFTWARE. 28 */ 29 30 31#include <stdio.h> 32#include "bufferobj.h" 33#include "context.h" 34#include "enums.h" 35#include "fbobject.h" 36#include "formats.h" 37#include "hash.h" 38#include "imports.h" 39#include "macros.h" 40#include "shaderimage.h" 41#include "teximage.h" 42#include "texobj.h" 43#include "texstate.h" 44#include "mtypes.h" 45#include "program/prog_instruction.h" 46#include "texturebindless.h" 47 48 49 50/**********************************************************************/ 51/** \name Internal functions */ 52/*@{*/ 53 54/** 55 * This function checks for all valid combinations of Min and Mag filters for 56 * Float types, when extensions like OES_texture_float and 57 * OES_texture_float_linear are supported. OES_texture_float mentions support 58 * for NEAREST, NEAREST_MIPMAP_NEAREST magnification and minification filters. 59 * Mag filters like LINEAR and min filters like NEAREST_MIPMAP_LINEAR, 60 * LINEAR_MIPMAP_NEAREST and LINEAR_MIPMAP_LINEAR are only valid in case 61 * OES_texture_float_linear is supported. 62 * 63 * Returns true in case the filter is valid for given Float type else false. 64 */ 65static bool 66valid_filter_for_float(const struct gl_context *ctx, 67 const struct gl_texture_object *obj) 68{ 69 switch (obj->Sampler.MagFilter) { 70 case GL_LINEAR: 71 if (obj->_IsHalfFloat && !ctx->Extensions.OES_texture_half_float_linear) { 72 return false; 73 } else if (obj->_IsFloat && !ctx->Extensions.OES_texture_float_linear) { 74 return false; 75 } 76 case GL_NEAREST: 77 case GL_NEAREST_MIPMAP_NEAREST: 78 break; 79 default: 80 unreachable("Invalid mag filter"); 81 } 82 83 switch (obj->Sampler.MinFilter) { 84 case GL_LINEAR: 85 case GL_NEAREST_MIPMAP_LINEAR: 86 case GL_LINEAR_MIPMAP_NEAREST: 87 case GL_LINEAR_MIPMAP_LINEAR: 88 if (obj->_IsHalfFloat && !ctx->Extensions.OES_texture_half_float_linear) { 89 return false; 90 } else if (obj->_IsFloat && !ctx->Extensions.OES_texture_float_linear) { 91 return false; 92 } 93 case GL_NEAREST: 94 case GL_NEAREST_MIPMAP_NEAREST: 95 break; 96 default: 97 unreachable("Invalid min filter"); 98 } 99 100 return true; 101} 102 103/** 104 * Return the gl_texture_object for a given ID. 105 */ 106struct gl_texture_object * 107_mesa_lookup_texture(struct gl_context *ctx, GLuint id) 108{ 109 return (struct gl_texture_object *) 110 _mesa_HashLookup(ctx->Shared->TexObjects, id); 111} 112 113/** 114 * Wrapper around _mesa_lookup_texture that throws GL_INVALID_OPERATION if id 115 * is not in the hash table. After calling _mesa_error, it returns NULL. 116 */ 117struct gl_texture_object * 118_mesa_lookup_texture_err(struct gl_context *ctx, GLuint id, const char* func) 119{ 120 struct gl_texture_object *texObj = NULL; 121 122 if (id > 0) 123 texObj = _mesa_lookup_texture(ctx, id); /* Returns NULL if not found. */ 124 125 if (!texObj) 126 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(texture)", func); 127 128 return texObj; 129} 130 131 132struct gl_texture_object * 133_mesa_lookup_texture_locked(struct gl_context *ctx, GLuint id) 134{ 135 return (struct gl_texture_object *) 136 _mesa_HashLookupLocked(ctx->Shared->TexObjects, id); 137} 138 139/** 140 * Return a pointer to the current texture object for the given target 141 * on the current texture unit. 142 * Note: all <target> error checking should have been done by this point. 143 */ 144struct gl_texture_object * 145_mesa_get_current_tex_object(struct gl_context *ctx, GLenum target) 146{ 147 struct gl_texture_unit *texUnit = _mesa_get_current_tex_unit(ctx); 148 const GLboolean arrayTex = ctx->Extensions.EXT_texture_array; 149 150 switch (target) { 151 case GL_TEXTURE_1D: 152 return texUnit->CurrentTex[TEXTURE_1D_INDEX]; 153 case GL_PROXY_TEXTURE_1D: 154 return ctx->Texture.ProxyTex[TEXTURE_1D_INDEX]; 155 case GL_TEXTURE_2D: 156 return texUnit->CurrentTex[TEXTURE_2D_INDEX]; 157 case GL_PROXY_TEXTURE_2D: 158 return ctx->Texture.ProxyTex[TEXTURE_2D_INDEX]; 159 case GL_TEXTURE_3D: 160 return texUnit->CurrentTex[TEXTURE_3D_INDEX]; 161 case GL_PROXY_TEXTURE_3D: 162 return ctx->Texture.ProxyTex[TEXTURE_3D_INDEX]; 163 case GL_TEXTURE_CUBE_MAP_POSITIVE_X: 164 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X: 165 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y: 166 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y: 167 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z: 168 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z: 169 case GL_TEXTURE_CUBE_MAP: 170 return ctx->Extensions.ARB_texture_cube_map 171 ? texUnit->CurrentTex[TEXTURE_CUBE_INDEX] : NULL; 172 case GL_PROXY_TEXTURE_CUBE_MAP: 173 return ctx->Extensions.ARB_texture_cube_map 174 ? ctx->Texture.ProxyTex[TEXTURE_CUBE_INDEX] : NULL; 175 case GL_TEXTURE_CUBE_MAP_ARRAY: 176 return _mesa_has_texture_cube_map_array(ctx) 177 ? texUnit->CurrentTex[TEXTURE_CUBE_ARRAY_INDEX] : NULL; 178 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY: 179 return _mesa_has_texture_cube_map_array(ctx) 180 ? ctx->Texture.ProxyTex[TEXTURE_CUBE_ARRAY_INDEX] : NULL; 181 case GL_TEXTURE_RECTANGLE_NV: 182 return ctx->Extensions.NV_texture_rectangle 183 ? texUnit->CurrentTex[TEXTURE_RECT_INDEX] : NULL; 184 case GL_PROXY_TEXTURE_RECTANGLE_NV: 185 return ctx->Extensions.NV_texture_rectangle 186 ? ctx->Texture.ProxyTex[TEXTURE_RECT_INDEX] : NULL; 187 case GL_TEXTURE_1D_ARRAY_EXT: 188 return arrayTex ? texUnit->CurrentTex[TEXTURE_1D_ARRAY_INDEX] : NULL; 189 case GL_PROXY_TEXTURE_1D_ARRAY_EXT: 190 return arrayTex ? ctx->Texture.ProxyTex[TEXTURE_1D_ARRAY_INDEX] : NULL; 191 case GL_TEXTURE_2D_ARRAY_EXT: 192 return arrayTex ? texUnit->CurrentTex[TEXTURE_2D_ARRAY_INDEX] : NULL; 193 case GL_PROXY_TEXTURE_2D_ARRAY_EXT: 194 return arrayTex ? ctx->Texture.ProxyTex[TEXTURE_2D_ARRAY_INDEX] : NULL; 195 case GL_TEXTURE_BUFFER: 196 return (_mesa_has_ARB_texture_buffer_object(ctx) || 197 _mesa_has_OES_texture_buffer(ctx)) ? 198 texUnit->CurrentTex[TEXTURE_BUFFER_INDEX] : NULL; 199 case GL_TEXTURE_EXTERNAL_OES: 200 return _mesa_is_gles(ctx) && ctx->Extensions.OES_EGL_image_external 201 ? texUnit->CurrentTex[TEXTURE_EXTERNAL_INDEX] : NULL; 202 case GL_TEXTURE_2D_MULTISAMPLE: 203 return ctx->Extensions.ARB_texture_multisample 204 ? texUnit->CurrentTex[TEXTURE_2D_MULTISAMPLE_INDEX] : NULL; 205 case GL_PROXY_TEXTURE_2D_MULTISAMPLE: 206 return ctx->Extensions.ARB_texture_multisample 207 ? ctx->Texture.ProxyTex[TEXTURE_2D_MULTISAMPLE_INDEX] : NULL; 208 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY: 209 return ctx->Extensions.ARB_texture_multisample 210 ? texUnit->CurrentTex[TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX] : NULL; 211 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY: 212 return ctx->Extensions.ARB_texture_multisample 213 ? ctx->Texture.ProxyTex[TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX] : NULL; 214 default: 215 _mesa_problem(NULL, "bad target in _mesa_get_current_tex_object()"); 216 return NULL; 217 } 218} 219 220 221/** 222 * Allocate and initialize a new texture object. But don't put it into the 223 * texture object hash table. 224 * 225 * Called via ctx->Driver.NewTextureObject, unless overridden by a device 226 * driver. 227 * 228 * \param shared the shared GL state structure to contain the texture object 229 * \param name integer name for the texture object 230 * \param target either GL_TEXTURE_1D, GL_TEXTURE_2D, GL_TEXTURE_3D, 231 * GL_TEXTURE_CUBE_MAP or GL_TEXTURE_RECTANGLE_NV. zero is ok for the sake 232 * of GenTextures() 233 * 234 * \return pointer to new texture object. 235 */ 236struct gl_texture_object * 237_mesa_new_texture_object(struct gl_context *ctx, GLuint name, GLenum target) 238{ 239 struct gl_texture_object *obj; 240 241 obj = MALLOC_STRUCT(gl_texture_object); 242 if (!obj) 243 return NULL; 244 245 _mesa_initialize_texture_object(ctx, obj, name, target); 246 return obj; 247} 248 249 250/** 251 * Initialize a new texture object to default values. 252 * \param obj the texture object 253 * \param name the texture name 254 * \param target the texture target 255 */ 256void 257_mesa_initialize_texture_object( struct gl_context *ctx, 258 struct gl_texture_object *obj, 259 GLuint name, GLenum target ) 260{ 261 assert(target == 0 || 262 target == GL_TEXTURE_1D || 263 target == GL_TEXTURE_2D || 264 target == GL_TEXTURE_3D || 265 target == GL_TEXTURE_CUBE_MAP || 266 target == GL_TEXTURE_RECTANGLE_NV || 267 target == GL_TEXTURE_1D_ARRAY_EXT || 268 target == GL_TEXTURE_2D_ARRAY_EXT || 269 target == GL_TEXTURE_EXTERNAL_OES || 270 target == GL_TEXTURE_CUBE_MAP_ARRAY || 271 target == GL_TEXTURE_BUFFER || 272 target == GL_TEXTURE_2D_MULTISAMPLE || 273 target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY); 274 275 GLenum filter = GL_LINEAR; 276 277 memset(obj, 0, sizeof(*obj)); 278 /* init the non-zero fields */ 279 simple_mtx_init(&obj->Mutex, mtx_plain); 280 obj->RefCount = 1; 281 obj->Name = name; 282 obj->Target = target; 283 if (target != 0) { 284 obj->TargetIndex = _mesa_tex_target_to_index(ctx, target); 285 } 286 else { 287 obj->TargetIndex = NUM_TEXTURE_TARGETS; /* invalid/error value */ 288 } 289 obj->Priority = 1.0F; 290 obj->BaseLevel = 0; 291 obj->MaxLevel = 1000; 292 293 /* must be one; no support for (YUV) planes in separate buffers */ 294 obj->RequiredTextureImageUnits = 1; 295 296 /* sampler state */ 297 switch (target) { 298 case GL_TEXTURE_2D_MULTISAMPLE: 299 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY: 300 filter = GL_NEAREST; 301 /* fallthrough */ 302 303 case GL_TEXTURE_RECTANGLE_NV: 304 case GL_TEXTURE_EXTERNAL_OES: 305 obj->Sampler.WrapS = GL_CLAMP_TO_EDGE; 306 obj->Sampler.WrapT = GL_CLAMP_TO_EDGE; 307 obj->Sampler.WrapR = GL_CLAMP_TO_EDGE; 308 obj->Sampler.MinFilter = filter; 309 obj->Sampler.MagFilter = filter; 310 break; 311 312 default: 313 obj->Sampler.WrapS = GL_REPEAT; 314 obj->Sampler.WrapT = GL_REPEAT; 315 obj->Sampler.WrapR = GL_REPEAT; 316 obj->Sampler.MinFilter = GL_NEAREST_MIPMAP_LINEAR; 317 obj->Sampler.MagFilter = GL_LINEAR; 318 break; 319 } 320 321 obj->Sampler.MinLod = -1000.0; 322 obj->Sampler.MaxLod = 1000.0; 323 obj->Sampler.LodBias = 0.0; 324 obj->Sampler.MaxAnisotropy = 1.0; 325 obj->Sampler.CompareMode = GL_NONE; /* ARB_shadow */ 326 obj->Sampler.CompareFunc = GL_LEQUAL; /* ARB_shadow */ 327 obj->DepthMode = ctx->API == API_OPENGL_CORE ? GL_RED : GL_LUMINANCE; 328 obj->StencilSampling = false; 329 obj->Sampler.CubeMapSeamless = GL_FALSE; 330 obj->Sampler.HandleAllocated = GL_FALSE; 331 obj->Swizzle[0] = GL_RED; 332 obj->Swizzle[1] = GL_GREEN; 333 obj->Swizzle[2] = GL_BLUE; 334 obj->Swizzle[3] = GL_ALPHA; 335 obj->_Swizzle = SWIZZLE_NOOP; 336 obj->Sampler.sRGBDecode = GL_DECODE_EXT; 337 obj->BufferObjectFormat = GL_R8; 338 obj->_BufferObjectFormat = MESA_FORMAT_R_UNORM8; 339 obj->ImageFormatCompatibilityType = GL_IMAGE_FORMAT_COMPATIBILITY_BY_SIZE; 340 341 /* GL_ARB_bindless_texture */ 342 _mesa_init_texture_handles(obj); 343} 344 345 346/** 347 * Some texture initialization can't be finished until we know which 348 * target it's getting bound to (GL_TEXTURE_1D/2D/etc). 349 */ 350static void 351finish_texture_init(struct gl_context *ctx, GLenum target, 352 struct gl_texture_object *obj, int targetIndex) 353{ 354 GLenum filter = GL_LINEAR; 355 assert(obj->Target == 0); 356 357 obj->Target = target; 358 obj->TargetIndex = targetIndex; 359 assert(obj->TargetIndex < NUM_TEXTURE_TARGETS); 360 361 switch (target) { 362 case GL_TEXTURE_2D_MULTISAMPLE: 363 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY: 364 filter = GL_NEAREST; 365 /* fallthrough */ 366 367 case GL_TEXTURE_RECTANGLE_NV: 368 case GL_TEXTURE_EXTERNAL_OES: 369 /* have to init wrap and filter state here - kind of klunky */ 370 obj->Sampler.WrapS = GL_CLAMP_TO_EDGE; 371 obj->Sampler.WrapT = GL_CLAMP_TO_EDGE; 372 obj->Sampler.WrapR = GL_CLAMP_TO_EDGE; 373 obj->Sampler.MinFilter = filter; 374 obj->Sampler.MagFilter = filter; 375 if (ctx->Driver.TexParameter) { 376 /* XXX we probably don't need to make all these calls */ 377 ctx->Driver.TexParameter(ctx, obj, GL_TEXTURE_WRAP_S); 378 ctx->Driver.TexParameter(ctx, obj, GL_TEXTURE_WRAP_T); 379 ctx->Driver.TexParameter(ctx, obj, GL_TEXTURE_WRAP_R); 380 ctx->Driver.TexParameter(ctx, obj, GL_TEXTURE_MIN_FILTER); 381 ctx->Driver.TexParameter(ctx, obj, GL_TEXTURE_MAG_FILTER); 382 } 383 break; 384 385 default: 386 /* nothing needs done */ 387 break; 388 } 389} 390 391 392/** 393 * Deallocate a texture object struct. It should have already been 394 * removed from the texture object pool. 395 * Called via ctx->Driver.DeleteTexture() if not overriden by a driver. 396 * 397 * \param shared the shared GL state to which the object belongs. 398 * \param texObj the texture object to delete. 399 */ 400void 401_mesa_delete_texture_object(struct gl_context *ctx, 402 struct gl_texture_object *texObj) 403{ 404 GLuint i, face; 405 406 /* Set Target to an invalid value. With some assertions elsewhere 407 * we can try to detect possible use of deleted textures. 408 */ 409 texObj->Target = 0x99; 410 411 /* free the texture images */ 412 for (face = 0; face < 6; face++) { 413 for (i = 0; i < MAX_TEXTURE_LEVELS; i++) { 414 if (texObj->Image[face][i]) { 415 ctx->Driver.DeleteTextureImage(ctx, texObj->Image[face][i]); 416 } 417 } 418 } 419 420 /* Delete all texture/image handles. */ 421 _mesa_delete_texture_handles(ctx, texObj); 422 423 _mesa_reference_buffer_object(ctx, &texObj->BufferObject, NULL); 424 425 /* destroy the mutex -- it may have allocated memory (eg on bsd) */ 426 simple_mtx_destroy(&texObj->Mutex); 427 428 free(texObj->Label); 429 430 /* free this object */ 431 free(texObj); 432} 433 434 435/** 436 * Copy texture object state from one texture object to another. 437 * Use for glPush/PopAttrib. 438 * 439 * \param dest destination texture object. 440 * \param src source texture object. 441 */ 442void 443_mesa_copy_texture_object( struct gl_texture_object *dest, 444 const struct gl_texture_object *src ) 445{ 446 dest->Target = src->Target; 447 dest->TargetIndex = src->TargetIndex; 448 dest->Name = src->Name; 449 dest->Priority = src->Priority; 450 dest->Sampler.BorderColor.f[0] = src->Sampler.BorderColor.f[0]; 451 dest->Sampler.BorderColor.f[1] = src->Sampler.BorderColor.f[1]; 452 dest->Sampler.BorderColor.f[2] = src->Sampler.BorderColor.f[2]; 453 dest->Sampler.BorderColor.f[3] = src->Sampler.BorderColor.f[3]; 454 dest->Sampler.WrapS = src->Sampler.WrapS; 455 dest->Sampler.WrapT = src->Sampler.WrapT; 456 dest->Sampler.WrapR = src->Sampler.WrapR; 457 dest->Sampler.MinFilter = src->Sampler.MinFilter; 458 dest->Sampler.MagFilter = src->Sampler.MagFilter; 459 dest->Sampler.MinLod = src->Sampler.MinLod; 460 dest->Sampler.MaxLod = src->Sampler.MaxLod; 461 dest->Sampler.LodBias = src->Sampler.LodBias; 462 dest->BaseLevel = src->BaseLevel; 463 dest->MaxLevel = src->MaxLevel; 464 dest->Sampler.MaxAnisotropy = src->Sampler.MaxAnisotropy; 465 dest->Sampler.CompareMode = src->Sampler.CompareMode; 466 dest->Sampler.CompareFunc = src->Sampler.CompareFunc; 467 dest->Sampler.CubeMapSeamless = src->Sampler.CubeMapSeamless; 468 dest->DepthMode = src->DepthMode; 469 dest->StencilSampling = src->StencilSampling; 470 dest->Sampler.sRGBDecode = src->Sampler.sRGBDecode; 471 dest->_MaxLevel = src->_MaxLevel; 472 dest->_MaxLambda = src->_MaxLambda; 473 dest->GenerateMipmap = src->GenerateMipmap; 474 dest->_BaseComplete = src->_BaseComplete; 475 dest->_MipmapComplete = src->_MipmapComplete; 476 COPY_4V(dest->Swizzle, src->Swizzle); 477 dest->_Swizzle = src->_Swizzle; 478 dest->_IsHalfFloat = src->_IsHalfFloat; 479 dest->_IsFloat = src->_IsFloat; 480 481 dest->RequiredTextureImageUnits = src->RequiredTextureImageUnits; 482} 483 484 485/** 486 * Free all texture images of the given texture objectm, except for 487 * \p retainTexImage. 488 * 489 * \param ctx GL context. 490 * \param texObj texture object. 491 * \param retainTexImage a texture image that will \em not be freed. 492 * 493 * \sa _mesa_clear_texture_image(). 494 */ 495void 496_mesa_clear_texture_object(struct gl_context *ctx, 497 struct gl_texture_object *texObj, 498 struct gl_texture_image *retainTexImage) 499{ 500 GLuint i, j; 501 502 if (texObj->Target == 0) 503 return; 504 505 for (i = 0; i < MAX_FACES; i++) { 506 for (j = 0; j < MAX_TEXTURE_LEVELS; j++) { 507 struct gl_texture_image *texImage = texObj->Image[i][j]; 508 if (texImage && texImage != retainTexImage) 509 _mesa_clear_texture_image(ctx, texImage); 510 } 511 } 512} 513 514 515/** 516 * Check if the given texture object is valid by examining its Target field. 517 * For debugging only. 518 */ 519static GLboolean 520valid_texture_object(const struct gl_texture_object *tex) 521{ 522 switch (tex->Target) { 523 case 0: 524 case GL_TEXTURE_1D: 525 case GL_TEXTURE_2D: 526 case GL_TEXTURE_3D: 527 case GL_TEXTURE_CUBE_MAP: 528 case GL_TEXTURE_RECTANGLE_NV: 529 case GL_TEXTURE_1D_ARRAY_EXT: 530 case GL_TEXTURE_2D_ARRAY_EXT: 531 case GL_TEXTURE_BUFFER: 532 case GL_TEXTURE_EXTERNAL_OES: 533 case GL_TEXTURE_CUBE_MAP_ARRAY: 534 case GL_TEXTURE_2D_MULTISAMPLE: 535 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY: 536 return GL_TRUE; 537 case 0x99: 538 _mesa_problem(NULL, "invalid reference to a deleted texture object"); 539 return GL_FALSE; 540 default: 541 _mesa_problem(NULL, "invalid texture object Target 0x%x, Id = %u", 542 tex->Target, tex->Name); 543 return GL_FALSE; 544 } 545} 546 547 548/** 549 * Reference (or unreference) a texture object. 550 * If '*ptr', decrement *ptr's refcount (and delete if it becomes zero). 551 * If 'tex' is non-null, increment its refcount. 552 * This is normally only called from the _mesa_reference_texobj() macro 553 * when there's a real pointer change. 554 */ 555void 556_mesa_reference_texobj_(struct gl_texture_object **ptr, 557 struct gl_texture_object *tex) 558{ 559 assert(ptr); 560 561 if (*ptr) { 562 /* Unreference the old texture */ 563 GLboolean deleteFlag = GL_FALSE; 564 struct gl_texture_object *oldTex = *ptr; 565 566 assert(valid_texture_object(oldTex)); 567 (void) valid_texture_object; /* silence warning in release builds */ 568 569 simple_mtx_lock(&oldTex->Mutex); 570 assert(oldTex->RefCount > 0); 571 oldTex->RefCount--; 572 573 deleteFlag = (oldTex->RefCount == 0); 574 simple_mtx_unlock(&oldTex->Mutex); 575 576 if (deleteFlag) { 577 /* Passing in the context drastically changes the driver code for 578 * framebuffer deletion. 579 */ 580 GET_CURRENT_CONTEXT(ctx); 581 if (ctx) 582 ctx->Driver.DeleteTexture(ctx, oldTex); 583 else 584 _mesa_problem(NULL, "Unable to delete texture, no context"); 585 } 586 587 *ptr = NULL; 588 } 589 assert(!*ptr); 590 591 if (tex) { 592 /* reference new texture */ 593 assert(valid_texture_object(tex)); 594 simple_mtx_lock(&tex->Mutex); 595 assert(tex->RefCount > 0); 596 597 tex->RefCount++; 598 *ptr = tex; 599 simple_mtx_unlock(&tex->Mutex); 600 } 601} 602 603 604enum base_mipmap { BASE, MIPMAP }; 605 606 607/** 608 * Mark a texture object as incomplete. There are actually three kinds of 609 * (in)completeness: 610 * 1. "base incomplete": the base level of the texture is invalid so no 611 * texturing is possible. 612 * 2. "mipmap incomplete": a non-base level of the texture is invalid so 613 * mipmap filtering isn't possible, but non-mipmap filtering is. 614 * 3. "texture incompleteness": some combination of texture state and 615 * sampler state renders the texture incomplete. 616 * 617 * \param t texture object 618 * \param bm either BASE or MIPMAP to indicate what's incomplete 619 * \param fmt... string describing why it's incomplete (for debugging). 620 */ 621static void 622incomplete(struct gl_texture_object *t, enum base_mipmap bm, 623 const char *fmt, ...) 624{ 625 if (MESA_DEBUG_FLAGS & DEBUG_INCOMPLETE_TEXTURE) { 626 va_list args; 627 char s[100]; 628 629 va_start(args, fmt); 630 vsnprintf(s, sizeof(s), fmt, args); 631 va_end(args); 632 633 _mesa_debug(NULL, "Texture Obj %d incomplete because: %s\n", t->Name, s); 634 } 635 636 if (bm == BASE) 637 t->_BaseComplete = GL_FALSE; 638 t->_MipmapComplete = GL_FALSE; 639} 640 641 642/** 643 * Examine a texture object to determine if it is complete. 644 * 645 * The gl_texture_object::Complete flag will be set to GL_TRUE or GL_FALSE 646 * accordingly. 647 * 648 * \param ctx GL context. 649 * \param t texture object. 650 * 651 * According to the texture target, verifies that each of the mipmaps is 652 * present and has the expected size. 653 */ 654void 655_mesa_test_texobj_completeness( const struct gl_context *ctx, 656 struct gl_texture_object *t ) 657{ 658 const GLint baseLevel = t->BaseLevel; 659 const struct gl_texture_image *baseImage; 660 GLint maxLevels = 0; 661 662 /* We'll set these to FALSE if tests fail below */ 663 t->_BaseComplete = GL_TRUE; 664 t->_MipmapComplete = GL_TRUE; 665 666 if (t->Target == GL_TEXTURE_BUFFER) { 667 /* Buffer textures are always considered complete. The obvious case where 668 * they would be incomplete (no BO attached) is actually specced to be 669 * undefined rendering results. 670 */ 671 return; 672 } 673 674 /* Detect cases where the application set the base level to an invalid 675 * value. 676 */ 677 if ((baseLevel < 0) || (baseLevel >= MAX_TEXTURE_LEVELS)) { 678 incomplete(t, BASE, "base level = %d is invalid", baseLevel); 679 return; 680 } 681 682 if (t->MaxLevel < baseLevel) { 683 incomplete(t, MIPMAP, "MAX_LEVEL (%d) < BASE_LEVEL (%d)", 684 t->MaxLevel, baseLevel); 685 return; 686 } 687 688 baseImage = t->Image[0][baseLevel]; 689 690 /* Always need the base level image */ 691 if (!baseImage) { 692 incomplete(t, BASE, "Image[baseLevel=%d] == NULL", baseLevel); 693 return; 694 } 695 696 /* Check width/height/depth for zero */ 697 if (baseImage->Width == 0 || 698 baseImage->Height == 0 || 699 baseImage->Depth == 0) { 700 incomplete(t, BASE, "texture width or height or depth = 0"); 701 return; 702 } 703 704 /* Check if the texture values are integer */ 705 { 706 GLenum datatype = _mesa_get_format_datatype(baseImage->TexFormat); 707 t->_IsIntegerFormat = datatype == GL_INT || datatype == GL_UNSIGNED_INT; 708 } 709 710 /* Check if the texture type is Float or HalfFloatOES and ensure Min and Mag 711 * filters are supported in this case. 712 */ 713 if (_mesa_is_gles(ctx) && !valid_filter_for_float(ctx, t)) { 714 incomplete(t, BASE, "Filter is not supported with Float types."); 715 return; 716 } 717 718 /* Compute _MaxLevel (the maximum mipmap level we'll sample from given the 719 * mipmap image sizes and GL_TEXTURE_MAX_LEVEL state). 720 */ 721 switch (t->Target) { 722 case GL_TEXTURE_1D: 723 case GL_TEXTURE_1D_ARRAY_EXT: 724 maxLevels = ctx->Const.MaxTextureLevels; 725 break; 726 case GL_TEXTURE_2D: 727 case GL_TEXTURE_2D_ARRAY_EXT: 728 maxLevels = ctx->Const.MaxTextureLevels; 729 break; 730 case GL_TEXTURE_3D: 731 maxLevels = ctx->Const.Max3DTextureLevels; 732 break; 733 case GL_TEXTURE_CUBE_MAP: 734 case GL_TEXTURE_CUBE_MAP_ARRAY: 735 maxLevels = ctx->Const.MaxCubeTextureLevels; 736 break; 737 case GL_TEXTURE_RECTANGLE_NV: 738 case GL_TEXTURE_BUFFER: 739 case GL_TEXTURE_EXTERNAL_OES: 740 case GL_TEXTURE_2D_MULTISAMPLE: 741 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY: 742 maxLevels = 1; /* no mipmapping */ 743 break; 744 default: 745 _mesa_problem(ctx, "Bad t->Target in _mesa_test_texobj_completeness"); 746 return; 747 } 748 749 assert(maxLevels > 0); 750 751 t->_MaxLevel = MIN3(t->MaxLevel, 752 /* 'p' in the GL spec */ 753 (int) (baseLevel + baseImage->MaxNumLevels - 1), 754 /* 'q' in the GL spec */ 755 maxLevels - 1); 756 757 if (t->Immutable) { 758 /* Adjust max level for views: the data store may have more levels than 759 * the view exposes. 760 */ 761 t->_MaxLevel = MIN2(t->_MaxLevel, t->NumLevels - 1); 762 } 763 764 /* Compute _MaxLambda = q - p in the spec used during mipmapping */ 765 t->_MaxLambda = (GLfloat) (t->_MaxLevel - baseLevel); 766 767 if (t->Immutable) { 768 /* This texture object was created with glTexStorage1/2/3D() so we 769 * know that all the mipmap levels are the right size and all cube 770 * map faces are the same size. 771 * We don't need to do any of the additional checks below. 772 */ 773 return; 774 } 775 776 if (t->Target == GL_TEXTURE_CUBE_MAP) { 777 /* Make sure that all six cube map level 0 images are the same size and 778 * format. 779 * Note: we know that the image's width==height (we enforce that 780 * at glTexImage time) so we only need to test the width here. 781 */ 782 GLuint face; 783 assert(baseImage->Width2 == baseImage->Height); 784 for (face = 1; face < 6; face++) { 785 assert(t->Image[face][baseLevel] == NULL || 786 t->Image[face][baseLevel]->Width2 == 787 t->Image[face][baseLevel]->Height2); 788 if (t->Image[face][baseLevel] == NULL || 789 t->Image[face][baseLevel]->Width2 != baseImage->Width2) { 790 incomplete(t, BASE, "Cube face missing or mismatched size"); 791 return; 792 } 793 if (t->Image[face][baseLevel]->InternalFormat != 794 baseImage->InternalFormat) { 795 incomplete(t, BASE, "Cube face format mismatch"); 796 return; 797 } 798 if (t->Image[face][baseLevel]->Border != baseImage->Border) { 799 incomplete(t, BASE, "Cube face border size mismatch"); 800 return; 801 } 802 } 803 } 804 805 /* 806 * Do mipmap consistency checking. 807 * Note: we don't care about the current texture sampler state here. 808 * To determine texture completeness we'll either look at _BaseComplete 809 * or _MipmapComplete depending on the current minification filter mode. 810 */ 811 { 812 GLint i; 813 const GLint minLevel = baseLevel; 814 const GLint maxLevel = t->_MaxLevel; 815 const GLuint numFaces = _mesa_num_tex_faces(t->Target); 816 GLuint width, height, depth, face; 817 818 if (minLevel > maxLevel) { 819 incomplete(t, MIPMAP, "minLevel > maxLevel"); 820 return; 821 } 822 823 /* Get the base image's dimensions */ 824 width = baseImage->Width2; 825 height = baseImage->Height2; 826 depth = baseImage->Depth2; 827 828 /* Note: this loop will be a no-op for RECT, BUFFER, EXTERNAL, 829 * MULTISAMPLE and MULTISAMPLE_ARRAY textures 830 */ 831 for (i = baseLevel + 1; i < maxLevels; i++) { 832 /* Compute the expected size of image at level[i] */ 833 if (width > 1) { 834 width /= 2; 835 } 836 if (height > 1 && t->Target != GL_TEXTURE_1D_ARRAY) { 837 height /= 2; 838 } 839 if (depth > 1 && t->Target != GL_TEXTURE_2D_ARRAY 840 && t->Target != GL_TEXTURE_CUBE_MAP_ARRAY) { 841 depth /= 2; 842 } 843 844 /* loop over cube faces (or single face otherwise) */ 845 for (face = 0; face < numFaces; face++) { 846 if (i >= minLevel && i <= maxLevel) { 847 const struct gl_texture_image *img = t->Image[face][i]; 848 849 if (!img) { 850 incomplete(t, MIPMAP, "TexImage[%d] is missing", i); 851 return; 852 } 853 if (img->InternalFormat != baseImage->InternalFormat) { 854 incomplete(t, MIPMAP, "Format[i] != Format[baseLevel]"); 855 return; 856 } 857 if (img->Border != baseImage->Border) { 858 incomplete(t, MIPMAP, "Border[i] != Border[baseLevel]"); 859 return; 860 } 861 if (img->Width2 != width) { 862 incomplete(t, MIPMAP, "TexImage[%d] bad width %u", i, 863 img->Width2); 864 return; 865 } 866 if (img->Height2 != height) { 867 incomplete(t, MIPMAP, "TexImage[%d] bad height %u", i, 868 img->Height2); 869 return; 870 } 871 if (img->Depth2 != depth) { 872 incomplete(t, MIPMAP, "TexImage[%d] bad depth %u", i, 873 img->Depth2); 874 return; 875 } 876 } 877 } 878 879 if (width == 1 && height == 1 && depth == 1) { 880 return; /* found smallest needed mipmap, all done! */ 881 } 882 } 883 } 884} 885 886 887GLboolean 888_mesa_cube_level_complete(const struct gl_texture_object *texObj, 889 const GLint level) 890{ 891 const struct gl_texture_image *img0, *img; 892 GLuint face; 893 894 if (texObj->Target != GL_TEXTURE_CUBE_MAP) 895 return GL_FALSE; 896 897 if ((level < 0) || (level >= MAX_TEXTURE_LEVELS)) 898 return GL_FALSE; 899 900 /* check first face */ 901 img0 = texObj->Image[0][level]; 902 if (!img0 || 903 img0->Width < 1 || 904 img0->Width != img0->Height) 905 return GL_FALSE; 906 907 /* check remaining faces vs. first face */ 908 for (face = 1; face < 6; face++) { 909 img = texObj->Image[face][level]; 910 if (!img || 911 img->Width != img0->Width || 912 img->Height != img0->Height || 913 img->TexFormat != img0->TexFormat) 914 return GL_FALSE; 915 } 916 917 return GL_TRUE; 918} 919 920/** 921 * Check if the given cube map texture is "cube complete" as defined in 922 * the OpenGL specification. 923 */ 924GLboolean 925_mesa_cube_complete(const struct gl_texture_object *texObj) 926{ 927 return _mesa_cube_level_complete(texObj, texObj->BaseLevel); 928} 929 930/** 931 * Mark a texture object dirty. It forces the object to be incomplete 932 * and forces the context to re-validate its state. 933 * 934 * \param ctx GL context. 935 * \param texObj texture object. 936 */ 937void 938_mesa_dirty_texobj(struct gl_context *ctx, struct gl_texture_object *texObj) 939{ 940 texObj->_BaseComplete = GL_FALSE; 941 texObj->_MipmapComplete = GL_FALSE; 942 ctx->NewState |= _NEW_TEXTURE_OBJECT; 943} 944 945 946/** 947 * Return pointer to a default/fallback texture of the given type/target. 948 * The texture is an RGBA texture with all texels = (0,0,0,1). 949 * That's the value a GLSL sampler should get when sampling from an 950 * incomplete texture. 951 */ 952struct gl_texture_object * 953_mesa_get_fallback_texture(struct gl_context *ctx, gl_texture_index tex) 954{ 955 if (!ctx->Shared->FallbackTex[tex]) { 956 /* create fallback texture now */ 957 const GLsizei width = 1, height = 1; 958 GLsizei depth = 1; 959 GLubyte texel[24]; 960 struct gl_texture_object *texObj; 961 struct gl_texture_image *texImage; 962 mesa_format texFormat; 963 GLuint dims, face, numFaces = 1; 964 GLenum target; 965 966 for (face = 0; face < 6; face++) { 967 texel[4*face + 0] = 968 texel[4*face + 1] = 969 texel[4*face + 2] = 0x0; 970 texel[4*face + 3] = 0xff; 971 } 972 973 switch (tex) { 974 case TEXTURE_2D_ARRAY_INDEX: 975 dims = 3; 976 target = GL_TEXTURE_2D_ARRAY; 977 break; 978 case TEXTURE_1D_ARRAY_INDEX: 979 dims = 2; 980 target = GL_TEXTURE_1D_ARRAY; 981 break; 982 case TEXTURE_CUBE_INDEX: 983 dims = 2; 984 target = GL_TEXTURE_CUBE_MAP; 985 numFaces = 6; 986 break; 987 case TEXTURE_3D_INDEX: 988 dims = 3; 989 target = GL_TEXTURE_3D; 990 break; 991 case TEXTURE_RECT_INDEX: 992 dims = 2; 993 target = GL_TEXTURE_RECTANGLE; 994 break; 995 case TEXTURE_2D_INDEX: 996 dims = 2; 997 target = GL_TEXTURE_2D; 998 break; 999 case TEXTURE_1D_INDEX: 1000 dims = 1; 1001 target = GL_TEXTURE_1D; 1002 break; 1003 case TEXTURE_BUFFER_INDEX: 1004 dims = 0; 1005 target = GL_TEXTURE_BUFFER; 1006 break; 1007 case TEXTURE_CUBE_ARRAY_INDEX: 1008 dims = 3; 1009 target = GL_TEXTURE_CUBE_MAP_ARRAY; 1010 depth = 6; 1011 break; 1012 case TEXTURE_EXTERNAL_INDEX: 1013 dims = 2; 1014 target = GL_TEXTURE_EXTERNAL_OES; 1015 break; 1016 case TEXTURE_2D_MULTISAMPLE_INDEX: 1017 dims = 2; 1018 target = GL_TEXTURE_2D_MULTISAMPLE; 1019 break; 1020 case TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX: 1021 dims = 3; 1022 target = GL_TEXTURE_2D_MULTISAMPLE_ARRAY; 1023 break; 1024 default: 1025 /* no-op */ 1026 return NULL; 1027 } 1028 1029 /* create texture object */ 1030 texObj = ctx->Driver.NewTextureObject(ctx, 0, target); 1031 if (!texObj) 1032 return NULL; 1033 1034 assert(texObj->RefCount == 1); 1035 texObj->Sampler.MinFilter = GL_NEAREST; 1036 texObj->Sampler.MagFilter = GL_NEAREST; 1037 1038 texFormat = ctx->Driver.ChooseTextureFormat(ctx, target, 1039 GL_RGBA, GL_RGBA, 1040 GL_UNSIGNED_BYTE); 1041 1042 /* need a loop here just for cube maps */ 1043 for (face = 0; face < numFaces; face++) { 1044 const GLenum faceTarget = _mesa_cube_face_target(target, face); 1045 1046 /* initialize level[0] texture image */ 1047 texImage = _mesa_get_tex_image(ctx, texObj, faceTarget, 0); 1048 1049 _mesa_init_teximage_fields(ctx, texImage, 1050 width, 1051 (dims > 1) ? height : 1, 1052 (dims > 2) ? depth : 1, 1053 0, /* border */ 1054 GL_RGBA, texFormat); 1055 1056 ctx->Driver.TexImage(ctx, dims, texImage, 1057 GL_RGBA, GL_UNSIGNED_BYTE, texel, 1058 &ctx->DefaultPacking); 1059 } 1060 1061 _mesa_test_texobj_completeness(ctx, texObj); 1062 assert(texObj->_BaseComplete); 1063 assert(texObj->_MipmapComplete); 1064 1065 ctx->Shared->FallbackTex[tex] = texObj; 1066 1067 /* Complete the driver's operation in case another context will also 1068 * use the same fallback texture. */ 1069 if (ctx->Driver.Finish) 1070 ctx->Driver.Finish(ctx); 1071 } 1072 return ctx->Shared->FallbackTex[tex]; 1073} 1074 1075 1076/** 1077 * Compute the size of the given texture object, in bytes. 1078 */ 1079static GLuint 1080texture_size(const struct gl_texture_object *texObj) 1081{ 1082 const GLuint numFaces = _mesa_num_tex_faces(texObj->Target); 1083 GLuint face, level, size = 0; 1084 1085 for (face = 0; face < numFaces; face++) { 1086 for (level = 0; level < MAX_TEXTURE_LEVELS; level++) { 1087 const struct gl_texture_image *img = texObj->Image[face][level]; 1088 if (img) { 1089 GLuint sz = _mesa_format_image_size(img->TexFormat, img->Width, 1090 img->Height, img->Depth); 1091 size += sz; 1092 } 1093 } 1094 } 1095 1096 return size; 1097} 1098 1099 1100/** 1101 * Callback called from _mesa_HashWalk() 1102 */ 1103static void 1104count_tex_size(GLuint key, void *data, void *userData) 1105{ 1106 const struct gl_texture_object *texObj = 1107 (const struct gl_texture_object *) data; 1108 GLuint *total = (GLuint *) userData; 1109 1110 (void) key; 1111 1112 *total = *total + texture_size(texObj); 1113} 1114 1115 1116/** 1117 * Compute total size (in bytes) of all textures for the given context. 1118 * For debugging purposes. 1119 */ 1120GLuint 1121_mesa_total_texture_memory(struct gl_context *ctx) 1122{ 1123 GLuint tgt, total = 0; 1124 1125 _mesa_HashWalk(ctx->Shared->TexObjects, count_tex_size, &total); 1126 1127 /* plus, the default texture objects */ 1128 for (tgt = 0; tgt < NUM_TEXTURE_TARGETS; tgt++) { 1129 total += texture_size(ctx->Shared->DefaultTex[tgt]); 1130 } 1131 1132 return total; 1133} 1134 1135 1136/** 1137 * Return the base format for the given texture object by looking 1138 * at the base texture image. 1139 * \return base format (such as GL_RGBA) or GL_NONE if it can't be determined 1140 */ 1141GLenum 1142_mesa_texture_base_format(const struct gl_texture_object *texObj) 1143{ 1144 const struct gl_texture_image *texImage = _mesa_base_tex_image(texObj); 1145 1146 return texImage ? texImage->_BaseFormat : GL_NONE; 1147} 1148 1149 1150static struct gl_texture_object * 1151invalidate_tex_image_error_check(struct gl_context *ctx, GLuint texture, 1152 GLint level, const char *name) 1153{ 1154 /* The GL_ARB_invalidate_subdata spec says: 1155 * 1156 * "If <texture> is zero or is not the name of a texture, the error 1157 * INVALID_VALUE is generated." 1158 * 1159 * This performs the error check in a different order than listed in the 1160 * spec. We have to get the texture object before we can validate the 1161 * other parameters against values in the texture object. 1162 */ 1163 struct gl_texture_object *const t = _mesa_lookup_texture(ctx, texture); 1164 if (texture == 0 || t == NULL) { 1165 _mesa_error(ctx, GL_INVALID_VALUE, "%s(texture)", name); 1166 return NULL; 1167 } 1168 1169 /* The GL_ARB_invalidate_subdata spec says: 1170 * 1171 * "If <level> is less than zero or greater than the base 2 logarithm 1172 * of the maximum texture width, height, or depth, the error 1173 * INVALID_VALUE is generated." 1174 */ 1175 if (level < 0 || level > t->MaxLevel) { 1176 _mesa_error(ctx, GL_INVALID_VALUE, "%s(level)", name); 1177 return NULL; 1178 } 1179 1180 /* The GL_ARB_invalidate_subdata spec says: 1181 * 1182 * "If the target of <texture> is TEXTURE_RECTANGLE, TEXTURE_BUFFER, 1183 * TEXTURE_2D_MULTISAMPLE, or TEXTURE_2D_MULTISAMPLE_ARRAY, and <level> 1184 * is not zero, the error INVALID_VALUE is generated." 1185 */ 1186 if (level != 0) { 1187 switch (t->Target) { 1188 case GL_TEXTURE_RECTANGLE: 1189 case GL_TEXTURE_BUFFER: 1190 case GL_TEXTURE_2D_MULTISAMPLE: 1191 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY: 1192 _mesa_error(ctx, GL_INVALID_VALUE, "%s(level)", name); 1193 return NULL; 1194 1195 default: 1196 break; 1197 } 1198 } 1199 1200 return t; 1201} 1202 1203 1204/** 1205 * Helper function for glCreateTextures and glGenTextures. Need this because 1206 * glCreateTextures should throw errors if target = 0. This is not exposed to 1207 * the rest of Mesa to encourage Mesa internals to use nameless textures, 1208 * which do not require expensive hash lookups. 1209 * \param target either 0 or a valid / error-checked texture target enum 1210 */ 1211static void 1212create_textures(struct gl_context *ctx, GLenum target, 1213 GLsizei n, GLuint *textures, const char *caller) 1214{ 1215 GLuint first; 1216 GLint i; 1217 1218 if (!textures) 1219 return; 1220 1221 /* 1222 * This must be atomic (generation and allocation of texture IDs) 1223 */ 1224 _mesa_HashLockMutex(ctx->Shared->TexObjects); 1225 1226 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->TexObjects, n); 1227 1228 /* Allocate new, empty texture objects */ 1229 for (i = 0; i < n; i++) { 1230 struct gl_texture_object *texObj; 1231 GLuint name = first + i; 1232 texObj = ctx->Driver.NewTextureObject(ctx, name, target); 1233 if (!texObj) { 1234 _mesa_HashUnlockMutex(ctx->Shared->TexObjects); 1235 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", caller); 1236 return; 1237 } 1238 1239 /* insert into hash table */ 1240 _mesa_HashInsertLocked(ctx->Shared->TexObjects, texObj->Name, texObj); 1241 1242 textures[i] = name; 1243 } 1244 1245 _mesa_HashUnlockMutex(ctx->Shared->TexObjects); 1246} 1247 1248 1249static void 1250create_textures_err(struct gl_context *ctx, GLenum target, 1251 GLsizei n, GLuint *textures, const char *caller) 1252{ 1253 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE)) 1254 _mesa_debug(ctx, "%s %d\n", caller, n); 1255 1256 if (n < 0) { 1257 _mesa_error(ctx, GL_INVALID_VALUE, "%s(n < 0)", caller); 1258 return; 1259 } 1260 1261 create_textures(ctx, target, n, textures, caller); 1262} 1263 1264/*@}*/ 1265 1266 1267/***********************************************************************/ 1268/** \name API functions */ 1269/*@{*/ 1270 1271 1272/** 1273 * Generate texture names. 1274 * 1275 * \param n number of texture names to be generated. 1276 * \param textures an array in which will hold the generated texture names. 1277 * 1278 * \sa glGenTextures(), glCreateTextures(). 1279 * 1280 * Calls _mesa_HashFindFreeKeyBlock() to find a block of free texture 1281 * IDs which are stored in \p textures. Corresponding empty texture 1282 * objects are also generated. 1283 */ 1284void GLAPIENTRY 1285_mesa_GenTextures_no_error(GLsizei n, GLuint *textures) 1286{ 1287 GET_CURRENT_CONTEXT(ctx); 1288 create_textures(ctx, 0, n, textures, "glGenTextures"); 1289} 1290 1291 1292void GLAPIENTRY 1293_mesa_GenTextures(GLsizei n, GLuint *textures) 1294{ 1295 GET_CURRENT_CONTEXT(ctx); 1296 create_textures_err(ctx, 0, n, textures, "glGenTextures"); 1297} 1298 1299/** 1300 * Create texture objects. 1301 * 1302 * \param target the texture target for each name to be generated. 1303 * \param n number of texture names to be generated. 1304 * \param textures an array in which will hold the generated texture names. 1305 * 1306 * \sa glCreateTextures(), glGenTextures(). 1307 * 1308 * Calls _mesa_HashFindFreeKeyBlock() to find a block of free texture 1309 * IDs which are stored in \p textures. Corresponding empty texture 1310 * objects are also generated. 1311 */ 1312void GLAPIENTRY 1313_mesa_CreateTextures_no_error(GLenum target, GLsizei n, GLuint *textures) 1314{ 1315 GET_CURRENT_CONTEXT(ctx); 1316 create_textures(ctx, target, n, textures, "glCreateTextures"); 1317} 1318 1319 1320void GLAPIENTRY 1321_mesa_CreateTextures(GLenum target, GLsizei n, GLuint *textures) 1322{ 1323 GLint targetIndex; 1324 GET_CURRENT_CONTEXT(ctx); 1325 1326 /* 1327 * The 4.5 core profile spec (30.10.2014) doesn't specify what 1328 * glCreateTextures should do with invalid targets, which was probably an 1329 * oversight. This conforms to the spec for glBindTexture. 1330 */ 1331 targetIndex = _mesa_tex_target_to_index(ctx, target); 1332 if (targetIndex < 0) { 1333 _mesa_error(ctx, GL_INVALID_ENUM, "glCreateTextures(target)"); 1334 return; 1335 } 1336 1337 create_textures_err(ctx, target, n, textures, "glCreateTextures"); 1338} 1339 1340/** 1341 * Check if the given texture object is bound to the current draw or 1342 * read framebuffer. If so, Unbind it. 1343 */ 1344static void 1345unbind_texobj_from_fbo(struct gl_context *ctx, 1346 struct gl_texture_object *texObj) 1347{ 1348 bool progress = false; 1349 1350 /* Section 4.4.2 (Attaching Images to Framebuffer Objects), subsection 1351 * "Attaching Texture Images to a Framebuffer," of the OpenGL 3.1 spec 1352 * says: 1353 * 1354 * "If a texture object is deleted while its image is attached to one 1355 * or more attachment points in the currently bound framebuffer, then 1356 * it is as if FramebufferTexture* had been called, with a texture of 1357 * zero, for each attachment point to which this image was attached in 1358 * the currently bound framebuffer. In other words, this texture image 1359 * is first detached from all attachment points in the currently bound 1360 * framebuffer. Note that the texture image is specifically not 1361 * detached from any other framebuffer objects. Detaching the texture 1362 * image from any other framebuffer objects is the responsibility of 1363 * the application." 1364 */ 1365 if (_mesa_is_user_fbo(ctx->DrawBuffer)) { 1366 progress = _mesa_detach_renderbuffer(ctx, ctx->DrawBuffer, texObj); 1367 } 1368 if (_mesa_is_user_fbo(ctx->ReadBuffer) 1369 && ctx->ReadBuffer != ctx->DrawBuffer) { 1370 progress = _mesa_detach_renderbuffer(ctx, ctx->ReadBuffer, texObj) 1371 || progress; 1372 } 1373 1374 if (progress) 1375 /* Vertices are already flushed by _mesa_DeleteTextures */ 1376 ctx->NewState |= _NEW_BUFFERS; 1377} 1378 1379 1380/** 1381 * Check if the given texture object is bound to any texture image units and 1382 * unbind it if so (revert to default textures). 1383 */ 1384static void 1385unbind_texobj_from_texunits(struct gl_context *ctx, 1386 struct gl_texture_object *texObj) 1387{ 1388 const gl_texture_index index = texObj->TargetIndex; 1389 GLuint u; 1390 1391 if (texObj->Target == 0) { 1392 /* texture was never bound */ 1393 return; 1394 } 1395 1396 assert(index < NUM_TEXTURE_TARGETS); 1397 1398 for (u = 0; u < ctx->Texture.NumCurrentTexUsed; u++) { 1399 struct gl_texture_unit *unit = &ctx->Texture.Unit[u]; 1400 1401 if (texObj == unit->CurrentTex[index]) { 1402 /* Bind the default texture for this unit/target */ 1403 _mesa_reference_texobj(&unit->CurrentTex[index], 1404 ctx->Shared->DefaultTex[index]); 1405 unit->_BoundTextures &= ~(1 << index); 1406 } 1407 } 1408} 1409 1410 1411/** 1412 * Check if the given texture object is bound to any shader image unit 1413 * and unbind it if that's the case. 1414 */ 1415static void 1416unbind_texobj_from_image_units(struct gl_context *ctx, 1417 struct gl_texture_object *texObj) 1418{ 1419 GLuint i; 1420 1421 for (i = 0; i < ctx->Const.MaxImageUnits; i++) { 1422 struct gl_image_unit *unit = &ctx->ImageUnits[i]; 1423 1424 if (texObj == unit->TexObj) { 1425 _mesa_reference_texobj(&unit->TexObj, NULL); 1426 *unit = _mesa_default_image_unit(ctx); 1427 } 1428 } 1429} 1430 1431 1432/** 1433 * Unbinds all textures bound to the given texture image unit. 1434 */ 1435static void 1436unbind_textures_from_unit(struct gl_context *ctx, GLuint unit) 1437{ 1438 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit]; 1439 1440 while (texUnit->_BoundTextures) { 1441 const GLuint index = ffs(texUnit->_BoundTextures) - 1; 1442 struct gl_texture_object *texObj = ctx->Shared->DefaultTex[index]; 1443 1444 _mesa_reference_texobj(&texUnit->CurrentTex[index], texObj); 1445 1446 /* Pass BindTexture call to device driver */ 1447 if (ctx->Driver.BindTexture) 1448 ctx->Driver.BindTexture(ctx, unit, 0, texObj); 1449 1450 texUnit->_BoundTextures &= ~(1 << index); 1451 ctx->NewState |= _NEW_TEXTURE_OBJECT; 1452 } 1453} 1454 1455 1456/** 1457 * Delete named textures. 1458 * 1459 * \param n number of textures to be deleted. 1460 * \param textures array of texture IDs to be deleted. 1461 * 1462 * \sa glDeleteTextures(). 1463 * 1464 * If we're about to delete a texture that's currently bound to any 1465 * texture unit, unbind the texture first. Decrement the reference 1466 * count on the texture object and delete it if it's zero. 1467 * Recall that texture objects can be shared among several rendering 1468 * contexts. 1469 */ 1470static void 1471delete_textures(struct gl_context *ctx, GLsizei n, const GLuint *textures) 1472{ 1473 FLUSH_VERTICES(ctx, 0); /* too complex */ 1474 1475 if (!textures) 1476 return; 1477 1478 for (GLsizei i = 0; i < n; i++) { 1479 if (textures[i] > 0) { 1480 struct gl_texture_object *delObj 1481 = _mesa_lookup_texture(ctx, textures[i]); 1482 1483 if (delObj) { 1484 _mesa_lock_texture(ctx, delObj); 1485 1486 /* Check if texture is bound to any framebuffer objects. 1487 * If so, unbind. 1488 * See section 4.4.2.3 of GL_EXT_framebuffer_object. 1489 */ 1490 unbind_texobj_from_fbo(ctx, delObj); 1491 1492 /* Check if this texture is currently bound to any texture units. 1493 * If so, unbind it. 1494 */ 1495 unbind_texobj_from_texunits(ctx, delObj); 1496 1497 /* Check if this texture is currently bound to any shader 1498 * image unit. If so, unbind it. 1499 * See section 3.9.X of GL_ARB_shader_image_load_store. 1500 */ 1501 unbind_texobj_from_image_units(ctx, delObj); 1502 1503 /* Make all handles that reference this texture object non-resident 1504 * in the current context. 1505 */ 1506 _mesa_make_texture_handles_non_resident(ctx, delObj); 1507 1508 _mesa_unlock_texture(ctx, delObj); 1509 1510 ctx->NewState |= _NEW_TEXTURE_OBJECT; 1511 1512 /* The texture _name_ is now free for re-use. 1513 * Remove it from the hash table now. 1514 */ 1515 _mesa_HashRemove(ctx->Shared->TexObjects, delObj->Name); 1516 1517 /* Unreference the texobj. If refcount hits zero, the texture 1518 * will be deleted. 1519 */ 1520 _mesa_reference_texobj(&delObj, NULL); 1521 } 1522 } 1523 } 1524} 1525 1526/** 1527 * This deletes a texObj without altering the hash table. 1528 */ 1529void 1530_mesa_delete_nameless_texture(struct gl_context *ctx, 1531 struct gl_texture_object *texObj) 1532{ 1533 if (!texObj) 1534 return; 1535 1536 FLUSH_VERTICES(ctx, 0); 1537 1538 _mesa_lock_texture(ctx, texObj); 1539 { 1540 /* Check if texture is bound to any framebuffer objects. 1541 * If so, unbind. 1542 * See section 4.4.2.3 of GL_EXT_framebuffer_object. 1543 */ 1544 unbind_texobj_from_fbo(ctx, texObj); 1545 1546 /* Check if this texture is currently bound to any texture units. 1547 * If so, unbind it. 1548 */ 1549 unbind_texobj_from_texunits(ctx, texObj); 1550 1551 /* Check if this texture is currently bound to any shader 1552 * image unit. If so, unbind it. 1553 * See section 3.9.X of GL_ARB_shader_image_load_store. 1554 */ 1555 unbind_texobj_from_image_units(ctx, texObj); 1556 } 1557 _mesa_unlock_texture(ctx, texObj); 1558 1559 ctx->NewState |= _NEW_TEXTURE_OBJECT; 1560 1561 /* Unreference the texobj. If refcount hits zero, the texture 1562 * will be deleted. 1563 */ 1564 _mesa_reference_texobj(&texObj, NULL); 1565} 1566 1567 1568void GLAPIENTRY 1569_mesa_DeleteTextures_no_error(GLsizei n, const GLuint *textures) 1570{ 1571 GET_CURRENT_CONTEXT(ctx); 1572 delete_textures(ctx, n, textures); 1573} 1574 1575 1576void GLAPIENTRY 1577_mesa_DeleteTextures(GLsizei n, const GLuint *textures) 1578{ 1579 GET_CURRENT_CONTEXT(ctx); 1580 1581 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE)) 1582 _mesa_debug(ctx, "glDeleteTextures %d\n", n); 1583 1584 if (n < 0) { 1585 _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteTextures(n < 0)"); 1586 return; 1587 } 1588 1589 delete_textures(ctx, n, textures); 1590} 1591 1592 1593/** 1594 * Convert a GL texture target enum such as GL_TEXTURE_2D or GL_TEXTURE_3D 1595 * into the corresponding Mesa texture target index. 1596 * Note that proxy targets are not valid here. 1597 * \return TEXTURE_x_INDEX or -1 if target is invalid 1598 */ 1599int 1600_mesa_tex_target_to_index(const struct gl_context *ctx, GLenum target) 1601{ 1602 switch (target) { 1603 case GL_TEXTURE_1D: 1604 return _mesa_is_desktop_gl(ctx) ? TEXTURE_1D_INDEX : -1; 1605 case GL_TEXTURE_2D: 1606 return TEXTURE_2D_INDEX; 1607 case GL_TEXTURE_3D: 1608 return ctx->API != API_OPENGLES ? TEXTURE_3D_INDEX : -1; 1609 case GL_TEXTURE_CUBE_MAP: 1610 return ctx->Extensions.ARB_texture_cube_map 1611 ? TEXTURE_CUBE_INDEX : -1; 1612 case GL_TEXTURE_RECTANGLE: 1613 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.NV_texture_rectangle 1614 ? TEXTURE_RECT_INDEX : -1; 1615 case GL_TEXTURE_1D_ARRAY: 1616 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_array 1617 ? TEXTURE_1D_ARRAY_INDEX : -1; 1618 case GL_TEXTURE_2D_ARRAY: 1619 return (_mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_array) 1620 || _mesa_is_gles3(ctx) 1621 ? TEXTURE_2D_ARRAY_INDEX : -1; 1622 case GL_TEXTURE_BUFFER: 1623 return (_mesa_has_ARB_texture_buffer_object(ctx) || 1624 _mesa_has_OES_texture_buffer(ctx)) ? 1625 TEXTURE_BUFFER_INDEX : -1; 1626 case GL_TEXTURE_EXTERNAL_OES: 1627 return _mesa_is_gles(ctx) && ctx->Extensions.OES_EGL_image_external 1628 ? TEXTURE_EXTERNAL_INDEX : -1; 1629 case GL_TEXTURE_CUBE_MAP_ARRAY: 1630 return _mesa_has_texture_cube_map_array(ctx) 1631 ? TEXTURE_CUBE_ARRAY_INDEX : -1; 1632 case GL_TEXTURE_2D_MULTISAMPLE: 1633 return ((_mesa_is_desktop_gl(ctx) && ctx->Extensions.ARB_texture_multisample) || 1634 _mesa_is_gles31(ctx)) ? TEXTURE_2D_MULTISAMPLE_INDEX: -1; 1635 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY: 1636 return ((_mesa_is_desktop_gl(ctx) && ctx->Extensions.ARB_texture_multisample) || 1637 _mesa_is_gles31(ctx)) 1638 ? TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX: -1; 1639 default: 1640 return -1; 1641 } 1642} 1643 1644 1645/** 1646 * Do actual texture binding. All error checking should have been done prior 1647 * to calling this function. Note that the texture target (1D, 2D, etc) is 1648 * always specified by the texObj->TargetIndex. 1649 * 1650 * \param unit index of texture unit to update 1651 * \param texObj the new texture object (cannot be NULL) 1652 */ 1653static void 1654bind_texture_object(struct gl_context *ctx, unsigned unit, 1655 struct gl_texture_object *texObj) 1656{ 1657 struct gl_texture_unit *texUnit; 1658 int targetIndex; 1659 1660 assert(unit < ARRAY_SIZE(ctx->Texture.Unit)); 1661 texUnit = &ctx->Texture.Unit[unit]; 1662 1663 assert(texObj); 1664 assert(valid_texture_object(texObj)); 1665 1666 targetIndex = texObj->TargetIndex; 1667 assert(targetIndex >= 0); 1668 assert(targetIndex < NUM_TEXTURE_TARGETS); 1669 1670 /* Check if this texture is only used by this context and is already bound. 1671 * If so, just return. For GL_OES_image_external, rebinding the texture 1672 * always must invalidate cached resources. 1673 */ 1674 if (targetIndex != TEXTURE_EXTERNAL_INDEX) { 1675 bool early_out; 1676 simple_mtx_lock(&ctx->Shared->Mutex); 1677 early_out = ((ctx->Shared->RefCount == 1) 1678 && (texObj == texUnit->CurrentTex[targetIndex])); 1679 simple_mtx_unlock(&ctx->Shared->Mutex); 1680 if (early_out) { 1681 return; 1682 } 1683 } 1684 1685 /* flush before changing binding */ 1686 FLUSH_VERTICES(ctx, _NEW_TEXTURE_OBJECT); 1687 1688 /* If the refcount on the previously bound texture is decremented to 1689 * zero, it'll be deleted here. 1690 */ 1691 _mesa_reference_texobj(&texUnit->CurrentTex[targetIndex], texObj); 1692 1693 ctx->Texture.NumCurrentTexUsed = MAX2(ctx->Texture.NumCurrentTexUsed, 1694 unit + 1); 1695 1696 if (texObj->Name != 0) 1697 texUnit->_BoundTextures |= (1 << targetIndex); 1698 else 1699 texUnit->_BoundTextures &= ~(1 << targetIndex); 1700 1701 /* Pass BindTexture call to device driver */ 1702 if (ctx->Driver.BindTexture) { 1703 ctx->Driver.BindTexture(ctx, unit, texObj->Target, texObj); 1704 } 1705} 1706 1707/** 1708 * Light-weight bind texture for internal users 1709 * 1710 * This is really just \c finish_texture_init plus \c bind_texture_object. 1711 * This is intended to be used by internal Mesa functions that use 1712 * \c _mesa_CreateTexture and need to bind textures (e.g., meta). 1713 */ 1714void 1715_mesa_bind_texture(struct gl_context *ctx, GLenum target, 1716 struct gl_texture_object *tex_obj) 1717{ 1718 const GLint targetIndex = _mesa_tex_target_to_index(ctx, target); 1719 1720 assert(targetIndex >= 0 && targetIndex < NUM_TEXTURE_TARGETS); 1721 1722 if (tex_obj->Target == 0) 1723 finish_texture_init(ctx, target, tex_obj, targetIndex); 1724 1725 assert(tex_obj->Target == target); 1726 assert(tex_obj->TargetIndex == targetIndex); 1727 1728 bind_texture_object(ctx, ctx->Texture.CurrentUnit, tex_obj); 1729} 1730 1731/** 1732 * Implement glBindTexture(). Do error checking, look-up or create a new 1733 * texture object, then bind it in the current texture unit. 1734 * 1735 * \param target texture target. 1736 * \param texName texture name. 1737 */ 1738static ALWAYS_INLINE void 1739bind_texture(struct gl_context *ctx, GLenum target, GLuint texName, 1740 bool no_error) 1741{ 1742 struct gl_texture_object *newTexObj = NULL; 1743 int targetIndex; 1744 1745 targetIndex = _mesa_tex_target_to_index(ctx, target); 1746 if (!no_error && targetIndex < 0) { 1747 _mesa_error(ctx, GL_INVALID_ENUM, "glBindTexture(target = %s)", 1748 _mesa_enum_to_string(target)); 1749 return; 1750 } 1751 assert(targetIndex < NUM_TEXTURE_TARGETS); 1752 1753 /* 1754 * Get pointer to new texture object (newTexObj) 1755 */ 1756 if (texName == 0) { 1757 /* Use a default texture object */ 1758 newTexObj = ctx->Shared->DefaultTex[targetIndex]; 1759 } else { 1760 /* non-default texture object */ 1761 newTexObj = _mesa_lookup_texture(ctx, texName); 1762 if (newTexObj) { 1763 /* error checking */ 1764 if (!no_error && 1765 newTexObj->Target != 0 && newTexObj->Target != target) { 1766 /* The named texture object's target doesn't match the 1767 * given target 1768 */ 1769 _mesa_error( ctx, GL_INVALID_OPERATION, 1770 "glBindTexture(target mismatch)" ); 1771 return; 1772 } 1773 if (newTexObj->Target == 0) { 1774 finish_texture_init(ctx, target, newTexObj, targetIndex); 1775 } 1776 } 1777 else { 1778 if (!no_error && ctx->API == API_OPENGL_CORE) { 1779 _mesa_error(ctx, GL_INVALID_OPERATION, 1780 "glBindTexture(non-gen name)"); 1781 return; 1782 } 1783 1784 /* if this is a new texture id, allocate a texture object now */ 1785 newTexObj = ctx->Driver.NewTextureObject(ctx, texName, target); 1786 if (!newTexObj) { 1787 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindTexture"); 1788 return; 1789 } 1790 1791 /* and insert it into hash table */ 1792 _mesa_HashInsert(ctx->Shared->TexObjects, texName, newTexObj); 1793 } 1794 } 1795 1796 assert(newTexObj->Target == target); 1797 assert(newTexObj->TargetIndex == targetIndex); 1798 1799 bind_texture_object(ctx, ctx->Texture.CurrentUnit, newTexObj); 1800} 1801 1802void GLAPIENTRY 1803_mesa_BindTexture_no_error(GLenum target, GLuint texName) 1804{ 1805 GET_CURRENT_CONTEXT(ctx); 1806 bind_texture(ctx, target, texName, true); 1807} 1808 1809 1810void GLAPIENTRY 1811_mesa_BindTexture(GLenum target, GLuint texName) 1812{ 1813 GET_CURRENT_CONTEXT(ctx); 1814 1815 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE)) 1816 _mesa_debug(ctx, "glBindTexture %s %d\n", 1817 _mesa_enum_to_string(target), (GLint) texName); 1818 1819 bind_texture(ctx, target, texName, false); 1820} 1821 1822 1823/** 1824 * OpenGL 4.5 / GL_ARB_direct_state_access glBindTextureUnit(). 1825 * 1826 * \param unit texture unit. 1827 * \param texture texture name. 1828 * 1829 * \sa glBindTexture(). 1830 * 1831 * If the named texture is 0, this will reset each target for the specified 1832 * texture unit to its default texture. 1833 * If the named texture is not 0 or a recognized texture name, this throws 1834 * GL_INVALID_OPERATION. 1835 */ 1836static ALWAYS_INLINE void 1837bind_texture_unit(struct gl_context *ctx, GLuint unit, GLuint texture, 1838 bool no_error) 1839{ 1840 struct gl_texture_object *texObj; 1841 1842 /* Section 8.1 (Texture Objects) of the OpenGL 4.5 core profile spec 1843 * (20141030) says: 1844 * "When texture is zero, each of the targets enumerated at the 1845 * beginning of this section is reset to its default texture for the 1846 * corresponding texture image unit." 1847 */ 1848 if (texture == 0) { 1849 unbind_textures_from_unit(ctx, unit); 1850 return; 1851 } 1852 1853 /* Get the non-default texture object */ 1854 texObj = _mesa_lookup_texture(ctx, texture); 1855 if (!no_error) { 1856 /* Error checking */ 1857 if (!texObj) { 1858 _mesa_error(ctx, GL_INVALID_OPERATION, 1859 "glBindTextureUnit(non-gen name)"); 1860 return; 1861 } 1862 1863 if (texObj->Target == 0) { 1864 /* Texture object was gen'd but never bound so the target is not set */ 1865 _mesa_error(ctx, GL_INVALID_OPERATION, "glBindTextureUnit(target)"); 1866 return; 1867 } 1868 } 1869 1870 assert(valid_texture_object(texObj)); 1871 1872 bind_texture_object(ctx, unit, texObj); 1873} 1874 1875 1876void GLAPIENTRY 1877_mesa_BindTextureUnit_no_error(GLuint unit, GLuint texture) 1878{ 1879 GET_CURRENT_CONTEXT(ctx); 1880 bind_texture_unit(ctx, unit, texture, true); 1881} 1882 1883 1884void GLAPIENTRY 1885_mesa_BindTextureUnit(GLuint unit, GLuint texture) 1886{ 1887 GET_CURRENT_CONTEXT(ctx); 1888 1889 if (unit >= _mesa_max_tex_unit(ctx)) { 1890 _mesa_error(ctx, GL_INVALID_VALUE, "glBindTextureUnit(unit=%u)", unit); 1891 return; 1892 } 1893 1894 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE)) 1895 _mesa_debug(ctx, "glBindTextureUnit %s %d\n", 1896 _mesa_enum_to_string(GL_TEXTURE0+unit), (GLint) texture); 1897 1898 bind_texture_unit(ctx, unit, texture, false); 1899} 1900 1901 1902/** 1903 * OpenGL 4.4 / GL_ARB_multi_bind glBindTextures(). 1904 */ 1905static ALWAYS_INLINE void 1906bind_textures(struct gl_context *ctx, GLuint first, GLsizei count, 1907 const GLuint *textures, bool no_error) 1908{ 1909 GLsizei i; 1910 1911 if (textures) { 1912 /* Note that the error semantics for multi-bind commands differ from 1913 * those of other GL commands. 1914 * 1915 * The issues section in the ARB_multi_bind spec says: 1916 * 1917 * "(11) Typically, OpenGL specifies that if an error is generated by 1918 * a command, that command has no effect. This is somewhat 1919 * unfortunate for multi-bind commands, because it would require 1920 * a first pass to scan the entire list of bound objects for 1921 * errors and then a second pass to actually perform the 1922 * bindings. Should we have different error semantics? 1923 * 1924 * RESOLVED: Yes. In this specification, when the parameters for 1925 * one of the <count> binding points are invalid, that binding 1926 * point is not updated and an error will be generated. However, 1927 * other binding points in the same command will be updated if 1928 * their parameters are valid and no other error occurs." 1929 */ 1930 1931 _mesa_HashLockMutex(ctx->Shared->TexObjects); 1932 1933 for (i = 0; i < count; i++) { 1934 if (textures[i] != 0) { 1935 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[first + i]; 1936 struct gl_texture_object *current = texUnit->_Current; 1937 struct gl_texture_object *texObj; 1938 1939 if (current && current->Name == textures[i]) 1940 texObj = current; 1941 else 1942 texObj = _mesa_lookup_texture_locked(ctx, textures[i]); 1943 1944 if (texObj && texObj->Target != 0) { 1945 bind_texture_object(ctx, first + i, texObj); 1946 } else if (!no_error) { 1947 /* The ARB_multi_bind spec says: 1948 * 1949 * "An INVALID_OPERATION error is generated if any value 1950 * in <textures> is not zero or the name of an existing 1951 * texture object (per binding)." 1952 */ 1953 _mesa_error(ctx, GL_INVALID_OPERATION, 1954 "glBindTextures(textures[%d]=%u is not zero " 1955 "or the name of an existing texture object)", 1956 i, textures[i]); 1957 } 1958 } else { 1959 unbind_textures_from_unit(ctx, first + i); 1960 } 1961 } 1962 1963 _mesa_HashUnlockMutex(ctx->Shared->TexObjects); 1964 } else { 1965 /* Unbind all textures in the range <first> through <first>+<count>-1 */ 1966 for (i = 0; i < count; i++) 1967 unbind_textures_from_unit(ctx, first + i); 1968 } 1969} 1970 1971 1972void GLAPIENTRY 1973_mesa_BindTextures_no_error(GLuint first, GLsizei count, const GLuint *textures) 1974{ 1975 GET_CURRENT_CONTEXT(ctx); 1976 bind_textures(ctx, first, count, textures, true); 1977} 1978 1979 1980void GLAPIENTRY 1981_mesa_BindTextures(GLuint first, GLsizei count, const GLuint *textures) 1982{ 1983 GET_CURRENT_CONTEXT(ctx); 1984 1985 /* The ARB_multi_bind spec says: 1986 * 1987 * "An INVALID_OPERATION error is generated if <first> + <count> 1988 * is greater than the number of texture image units supported 1989 * by the implementation." 1990 */ 1991 if (first + count > ctx->Const.MaxCombinedTextureImageUnits) { 1992 _mesa_error(ctx, GL_INVALID_OPERATION, 1993 "glBindTextures(first=%u + count=%d > the value of " 1994 "GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS=%u)", 1995 first, count, ctx->Const.MaxCombinedTextureImageUnits); 1996 return; 1997 } 1998 1999 bind_textures(ctx, first, count, textures, false); 2000} 2001 2002 2003/** 2004 * Set texture priorities. 2005 * 2006 * \param n number of textures. 2007 * \param texName texture names. 2008 * \param priorities corresponding texture priorities. 2009 * 2010 * \sa glPrioritizeTextures(). 2011 * 2012 * Looks up each texture in the hash, clamps the corresponding priority between 2013 * 0.0 and 1.0, and calls dd_function_table::PrioritizeTexture. 2014 */ 2015void GLAPIENTRY 2016_mesa_PrioritizeTextures( GLsizei n, const GLuint *texName, 2017 const GLclampf *priorities ) 2018{ 2019 GET_CURRENT_CONTEXT(ctx); 2020 GLint i; 2021 2022 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE)) 2023 _mesa_debug(ctx, "glPrioritizeTextures %d\n", n); 2024 2025 FLUSH_VERTICES(ctx, 0); 2026 2027 if (n < 0) { 2028 _mesa_error( ctx, GL_INVALID_VALUE, "glPrioritizeTextures" ); 2029 return; 2030 } 2031 2032 if (!priorities) 2033 return; 2034 2035 for (i = 0; i < n; i++) { 2036 if (texName[i] > 0) { 2037 struct gl_texture_object *t = _mesa_lookup_texture(ctx, texName[i]); 2038 if (t) { 2039 t->Priority = CLAMP( priorities[i], 0.0F, 1.0F ); 2040 } 2041 } 2042 } 2043 2044 ctx->NewState |= _NEW_TEXTURE_OBJECT; 2045} 2046 2047 2048 2049/** 2050 * See if textures are loaded in texture memory. 2051 * 2052 * \param n number of textures to query. 2053 * \param texName array with the texture names. 2054 * \param residences array which will hold the residence status. 2055 * 2056 * \return GL_TRUE if all textures are resident and 2057 * residences is left unchanged, 2058 * 2059 * Note: we assume all textures are always resident 2060 */ 2061GLboolean GLAPIENTRY 2062_mesa_AreTexturesResident(GLsizei n, const GLuint *texName, 2063 GLboolean *residences) 2064{ 2065 GET_CURRENT_CONTEXT(ctx); 2066 GLboolean allResident = GL_TRUE; 2067 GLint i; 2068 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE); 2069 2070 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE)) 2071 _mesa_debug(ctx, "glAreTexturesResident %d\n", n); 2072 2073 if (n < 0) { 2074 _mesa_error(ctx, GL_INVALID_VALUE, "glAreTexturesResident(n)"); 2075 return GL_FALSE; 2076 } 2077 2078 if (!texName || !residences) 2079 return GL_FALSE; 2080 2081 /* We only do error checking on the texture names */ 2082 for (i = 0; i < n; i++) { 2083 struct gl_texture_object *t; 2084 if (texName[i] == 0) { 2085 _mesa_error(ctx, GL_INVALID_VALUE, "glAreTexturesResident"); 2086 return GL_FALSE; 2087 } 2088 t = _mesa_lookup_texture(ctx, texName[i]); 2089 if (!t) { 2090 _mesa_error(ctx, GL_INVALID_VALUE, "glAreTexturesResident"); 2091 return GL_FALSE; 2092 } 2093 } 2094 2095 return allResident; 2096} 2097 2098 2099/** 2100 * See if a name corresponds to a texture. 2101 * 2102 * \param texture texture name. 2103 * 2104 * \return GL_TRUE if texture name corresponds to a texture, or GL_FALSE 2105 * otherwise. 2106 * 2107 * \sa glIsTexture(). 2108 * 2109 * Calls _mesa_HashLookup(). 2110 */ 2111GLboolean GLAPIENTRY 2112_mesa_IsTexture( GLuint texture ) 2113{ 2114 struct gl_texture_object *t; 2115 GET_CURRENT_CONTEXT(ctx); 2116 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE); 2117 2118 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE)) 2119 _mesa_debug(ctx, "glIsTexture %d\n", texture); 2120 2121 if (!texture) 2122 return GL_FALSE; 2123 2124 t = _mesa_lookup_texture(ctx, texture); 2125 2126 /* IsTexture is true only after object has been bound once. */ 2127 return t && t->Target; 2128} 2129 2130 2131/** 2132 * Simplest implementation of texture locking: grab the shared tex 2133 * mutex. Examine the shared context state timestamp and if there has 2134 * been a change, set the appropriate bits in ctx->NewState. 2135 * 2136 * This is used to deal with synchronizing things when a texture object 2137 * is used/modified by different contexts (or threads) which are sharing 2138 * the texture. 2139 * 2140 * See also _mesa_lock/unlock_texture() in teximage.h 2141 */ 2142void 2143_mesa_lock_context_textures( struct gl_context *ctx ) 2144{ 2145 mtx_lock(&ctx->Shared->TexMutex); 2146 2147 if (ctx->Shared->TextureStateStamp != ctx->TextureStateTimestamp) { 2148 ctx->NewState |= _NEW_TEXTURE_OBJECT; 2149 ctx->TextureStateTimestamp = ctx->Shared->TextureStateStamp; 2150 } 2151} 2152 2153 2154void 2155_mesa_unlock_context_textures( struct gl_context *ctx ) 2156{ 2157 assert(ctx->Shared->TextureStateStamp == ctx->TextureStateTimestamp); 2158 mtx_unlock(&ctx->Shared->TexMutex); 2159} 2160 2161 2162void GLAPIENTRY 2163_mesa_InvalidateTexSubImage_no_error(GLuint texture, GLint level, GLint xoffset, 2164 GLint yoffset, GLint zoffset, 2165 GLsizei width, GLsizei height, 2166 GLsizei depth) 2167{ 2168 /* no-op */ 2169} 2170 2171 2172void GLAPIENTRY 2173_mesa_InvalidateTexSubImage(GLuint texture, GLint level, GLint xoffset, 2174 GLint yoffset, GLint zoffset, GLsizei width, 2175 GLsizei height, GLsizei depth) 2176{ 2177 struct gl_texture_object *t; 2178 struct gl_texture_image *image; 2179 GET_CURRENT_CONTEXT(ctx); 2180 2181 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE)) 2182 _mesa_debug(ctx, "glInvalidateTexSubImage %d\n", texture); 2183 2184 t = invalidate_tex_image_error_check(ctx, texture, level, 2185 "glInvalidateTexSubImage"); 2186 2187 /* The GL_ARB_invalidate_subdata spec says: 2188 * 2189 * "...the specified subregion must be between -<b> and <dim>+<b> where 2190 * <dim> is the size of the dimension of the texture image, and <b> is 2191 * the size of the border of that texture image, otherwise 2192 * INVALID_VALUE is generated (border is not applied to dimensions that 2193 * don't exist in a given texture target)." 2194 */ 2195 image = t->Image[0][level]; 2196 if (image) { 2197 int xBorder; 2198 int yBorder; 2199 int zBorder; 2200 int imageWidth; 2201 int imageHeight; 2202 int imageDepth; 2203 2204 /* The GL_ARB_invalidate_subdata spec says: 2205 * 2206 * "For texture targets that don't have certain dimensions, this 2207 * command treats those dimensions as having a size of 1. For 2208 * example, to invalidate a portion of a two-dimensional texture, 2209 * the application would use <zoffset> equal to zero and <depth> 2210 * equal to one." 2211 */ 2212 switch (t->Target) { 2213 case GL_TEXTURE_BUFFER: 2214 xBorder = 0; 2215 yBorder = 0; 2216 zBorder = 0; 2217 imageWidth = 1; 2218 imageHeight = 1; 2219 imageDepth = 1; 2220 break; 2221 case GL_TEXTURE_1D: 2222 xBorder = image->Border; 2223 yBorder = 0; 2224 zBorder = 0; 2225 imageWidth = image->Width; 2226 imageHeight = 1; 2227 imageDepth = 1; 2228 break; 2229 case GL_TEXTURE_1D_ARRAY: 2230 xBorder = image->Border; 2231 yBorder = 0; 2232 zBorder = 0; 2233 imageWidth = image->Width; 2234 imageHeight = image->Height; 2235 imageDepth = 1; 2236 break; 2237 case GL_TEXTURE_2D: 2238 case GL_TEXTURE_CUBE_MAP: 2239 case GL_TEXTURE_RECTANGLE: 2240 case GL_TEXTURE_2D_MULTISAMPLE: 2241 xBorder = image->Border; 2242 yBorder = image->Border; 2243 zBorder = 0; 2244 imageWidth = image->Width; 2245 imageHeight = image->Height; 2246 imageDepth = 1; 2247 break; 2248 case GL_TEXTURE_2D_ARRAY: 2249 case GL_TEXTURE_CUBE_MAP_ARRAY: 2250 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY: 2251 xBorder = image->Border; 2252 yBorder = image->Border; 2253 zBorder = 0; 2254 imageWidth = image->Width; 2255 imageHeight = image->Height; 2256 imageDepth = image->Depth; 2257 break; 2258 case GL_TEXTURE_3D: 2259 xBorder = image->Border; 2260 yBorder = image->Border; 2261 zBorder = image->Border; 2262 imageWidth = image->Width; 2263 imageHeight = image->Height; 2264 imageDepth = image->Depth; 2265 break; 2266 default: 2267 assert(!"Should not get here."); 2268 xBorder = 0; 2269 yBorder = 0; 2270 zBorder = 0; 2271 imageWidth = 0; 2272 imageHeight = 0; 2273 imageDepth = 0; 2274 break; 2275 } 2276 2277 if (xoffset < -xBorder) { 2278 _mesa_error(ctx, GL_INVALID_VALUE, "glInvalidateSubTexImage(xoffset)"); 2279 return; 2280 } 2281 2282 if (xoffset + width > imageWidth + xBorder) { 2283 _mesa_error(ctx, GL_INVALID_VALUE, 2284 "glInvalidateSubTexImage(xoffset+width)"); 2285 return; 2286 } 2287 2288 if (yoffset < -yBorder) { 2289 _mesa_error(ctx, GL_INVALID_VALUE, "glInvalidateSubTexImage(yoffset)"); 2290 return; 2291 } 2292 2293 if (yoffset + height > imageHeight + yBorder) { 2294 _mesa_error(ctx, GL_INVALID_VALUE, 2295 "glInvalidateSubTexImage(yoffset+height)"); 2296 return; 2297 } 2298 2299 if (zoffset < -zBorder) { 2300 _mesa_error(ctx, GL_INVALID_VALUE, 2301 "glInvalidateSubTexImage(zoffset)"); 2302 return; 2303 } 2304 2305 if (zoffset + depth > imageDepth + zBorder) { 2306 _mesa_error(ctx, GL_INVALID_VALUE, 2307 "glInvalidateSubTexImage(zoffset+depth)"); 2308 return; 2309 } 2310 } 2311 2312 /* We don't actually do anything for this yet. Just return after 2313 * validating the parameters and generating the required errors. 2314 */ 2315 return; 2316} 2317 2318 2319void GLAPIENTRY 2320_mesa_InvalidateTexImage_no_error(GLuint texture, GLint level) 2321{ 2322 /* no-op */ 2323} 2324 2325 2326void GLAPIENTRY 2327_mesa_InvalidateTexImage(GLuint texture, GLint level) 2328{ 2329 GET_CURRENT_CONTEXT(ctx); 2330 2331 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE)) 2332 _mesa_debug(ctx, "glInvalidateTexImage(%d, %d)\n", texture, level); 2333 2334 invalidate_tex_image_error_check(ctx, texture, level, 2335 "glInvalidateTexImage"); 2336 2337 /* We don't actually do anything for this yet. Just return after 2338 * validating the parameters and generating the required errors. 2339 */ 2340 return; 2341} 2342 2343/*@}*/ 2344