mesa: Track maximum CurrentTexUnit to reduce glDeleteTextures() overhead.
[mesa.git] / src / mesa / main / texobj.c
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 "bufferobj.h"
32 #include "colortab.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 "teximage.h"
41 #include "texobj.h"
42 #include "texstate.h"
43 #include "mtypes.h"
44 #include "program/prog_instruction.h"
45
46
47
48 /**********************************************************************/
49 /** \name Internal functions */
50 /*@{*/
51
52
53 /**
54 * Return the gl_texture_object for a given ID.
55 */
56 struct gl_texture_object *
57 _mesa_lookup_texture(struct gl_context *ctx, GLuint id)
58 {
59 return (struct gl_texture_object *)
60 _mesa_HashLookup(ctx->Shared->TexObjects, id);
61 }
62
63
64
65 /**
66 * Allocate and initialize a new texture object. But don't put it into the
67 * texture object hash table.
68 *
69 * Called via ctx->Driver.NewTextureObject, unless overridden by a device
70 * driver.
71 *
72 * \param shared the shared GL state structure to contain the texture object
73 * \param name integer name for the texture object
74 * \param target either GL_TEXTURE_1D, GL_TEXTURE_2D, GL_TEXTURE_3D,
75 * GL_TEXTURE_CUBE_MAP_ARB or GL_TEXTURE_RECTANGLE_NV. zero is ok for the sake
76 * of GenTextures()
77 *
78 * \return pointer to new texture object.
79 */
80 struct gl_texture_object *
81 _mesa_new_texture_object( struct gl_context *ctx, GLuint name, GLenum target )
82 {
83 struct gl_texture_object *obj;
84 (void) ctx;
85 obj = MALLOC_STRUCT(gl_texture_object);
86 _mesa_initialize_texture_object(ctx, obj, name, target);
87 return obj;
88 }
89
90
91 /**
92 * Initialize a new texture object to default values.
93 * \param obj the texture object
94 * \param name the texture name
95 * \param target the texture target
96 */
97 void
98 _mesa_initialize_texture_object( struct gl_context *ctx,
99 struct gl_texture_object *obj,
100 GLuint name, GLenum target )
101 {
102 ASSERT(target == 0 ||
103 target == GL_TEXTURE_1D ||
104 target == GL_TEXTURE_2D ||
105 target == GL_TEXTURE_3D ||
106 target == GL_TEXTURE_CUBE_MAP_ARB ||
107 target == GL_TEXTURE_RECTANGLE_NV ||
108 target == GL_TEXTURE_1D_ARRAY_EXT ||
109 target == GL_TEXTURE_2D_ARRAY_EXT ||
110 target == GL_TEXTURE_EXTERNAL_OES ||
111 target == GL_TEXTURE_CUBE_MAP_ARRAY ||
112 target == GL_TEXTURE_BUFFER ||
113 target == GL_TEXTURE_2D_MULTISAMPLE ||
114 target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY);
115
116 memset(obj, 0, sizeof(*obj));
117 /* init the non-zero fields */
118 mtx_init(&obj->Mutex, mtx_plain);
119 obj->RefCount = 1;
120 obj->Name = name;
121 obj->Target = target;
122 obj->Priority = 1.0F;
123 obj->BaseLevel = 0;
124 obj->MaxLevel = 1000;
125
126 /* must be one; no support for (YUV) planes in separate buffers */
127 obj->RequiredTextureImageUnits = 1;
128
129 /* sampler state */
130 if (target == GL_TEXTURE_RECTANGLE_NV ||
131 target == GL_TEXTURE_EXTERNAL_OES) {
132 obj->Sampler.WrapS = GL_CLAMP_TO_EDGE;
133 obj->Sampler.WrapT = GL_CLAMP_TO_EDGE;
134 obj->Sampler.WrapR = GL_CLAMP_TO_EDGE;
135 obj->Sampler.MinFilter = GL_LINEAR;
136 }
137 else {
138 obj->Sampler.WrapS = GL_REPEAT;
139 obj->Sampler.WrapT = GL_REPEAT;
140 obj->Sampler.WrapR = GL_REPEAT;
141 obj->Sampler.MinFilter = GL_NEAREST_MIPMAP_LINEAR;
142 }
143 obj->Sampler.MagFilter = GL_LINEAR;
144 obj->Sampler.MinLod = -1000.0;
145 obj->Sampler.MaxLod = 1000.0;
146 obj->Sampler.LodBias = 0.0;
147 obj->Sampler.MaxAnisotropy = 1.0;
148 obj->Sampler.CompareMode = GL_NONE; /* ARB_shadow */
149 obj->Sampler.CompareFunc = GL_LEQUAL; /* ARB_shadow */
150 obj->DepthMode = ctx->API == API_OPENGL_CORE ? GL_RED : GL_LUMINANCE;
151 obj->StencilSampling = false;
152 obj->Sampler.CubeMapSeamless = GL_FALSE;
153 obj->Swizzle[0] = GL_RED;
154 obj->Swizzle[1] = GL_GREEN;
155 obj->Swizzle[2] = GL_BLUE;
156 obj->Swizzle[3] = GL_ALPHA;
157 obj->_Swizzle = SWIZZLE_NOOP;
158 obj->Sampler.sRGBDecode = GL_DECODE_EXT;
159 obj->BufferObjectFormat = GL_R8;
160 obj->_BufferObjectFormat = MESA_FORMAT_R_UNORM8;
161 obj->ImageFormatCompatibilityType = GL_IMAGE_FORMAT_COMPATIBILITY_BY_SIZE;
162 }
163
164
165 /**
166 * Some texture initialization can't be finished until we know which
167 * target it's getting bound to (GL_TEXTURE_1D/2D/etc).
168 */
169 static void
170 finish_texture_init(struct gl_context *ctx, GLenum target,
171 struct gl_texture_object *obj)
172 {
173 GLenum filter = GL_LINEAR;
174 assert(obj->Target == 0);
175
176 switch (target) {
177 case GL_TEXTURE_2D_MULTISAMPLE:
178 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
179 filter = GL_NEAREST;
180 /* fallthrough */
181
182 case GL_TEXTURE_RECTANGLE_NV:
183 case GL_TEXTURE_EXTERNAL_OES:
184 /* have to init wrap and filter state here - kind of klunky */
185 obj->Sampler.WrapS = GL_CLAMP_TO_EDGE;
186 obj->Sampler.WrapT = GL_CLAMP_TO_EDGE;
187 obj->Sampler.WrapR = GL_CLAMP_TO_EDGE;
188 obj->Sampler.MinFilter = filter;
189 obj->Sampler.MagFilter = filter;
190 if (ctx->Driver.TexParameter) {
191 static const GLfloat fparam_wrap[1] = {(GLfloat) GL_CLAMP_TO_EDGE};
192 const GLfloat fparam_filter[1] = {(GLfloat) filter};
193 ctx->Driver.TexParameter(ctx, obj, GL_TEXTURE_WRAP_S, fparam_wrap);
194 ctx->Driver.TexParameter(ctx, obj, GL_TEXTURE_WRAP_T, fparam_wrap);
195 ctx->Driver.TexParameter(ctx, obj, GL_TEXTURE_WRAP_R, fparam_wrap);
196 ctx->Driver.TexParameter(ctx, obj,
197 GL_TEXTURE_MIN_FILTER, fparam_filter);
198 ctx->Driver.TexParameter(ctx, obj,
199 GL_TEXTURE_MAG_FILTER, fparam_filter);
200 }
201 break;
202
203 default:
204 /* nothing needs done */
205 break;
206 }
207 }
208
209
210 /**
211 * Deallocate a texture object struct. It should have already been
212 * removed from the texture object pool.
213 * Called via ctx->Driver.DeleteTexture() if not overriden by a driver.
214 *
215 * \param shared the shared GL state to which the object belongs.
216 * \param texObj the texture object to delete.
217 */
218 void
219 _mesa_delete_texture_object(struct gl_context *ctx,
220 struct gl_texture_object *texObj)
221 {
222 GLuint i, face;
223
224 /* Set Target to an invalid value. With some assertions elsewhere
225 * we can try to detect possible use of deleted textures.
226 */
227 texObj->Target = 0x99;
228
229 /* free the texture images */
230 for (face = 0; face < 6; face++) {
231 for (i = 0; i < MAX_TEXTURE_LEVELS; i++) {
232 if (texObj->Image[face][i]) {
233 ctx->Driver.DeleteTextureImage(ctx, texObj->Image[face][i]);
234 }
235 }
236 }
237
238 _mesa_reference_buffer_object(ctx, &texObj->BufferObject, NULL);
239
240 /* destroy the mutex -- it may have allocated memory (eg on bsd) */
241 mtx_destroy(&texObj->Mutex);
242
243 free(texObj->Label);
244
245 /* free this object */
246 free(texObj);
247 }
248
249
250
251 /**
252 * Copy texture object state from one texture object to another.
253 * Use for glPush/PopAttrib.
254 *
255 * \param dest destination texture object.
256 * \param src source texture object.
257 */
258 void
259 _mesa_copy_texture_object( struct gl_texture_object *dest,
260 const struct gl_texture_object *src )
261 {
262 dest->Target = src->Target;
263 dest->Name = src->Name;
264 dest->Priority = src->Priority;
265 dest->Sampler.BorderColor.f[0] = src->Sampler.BorderColor.f[0];
266 dest->Sampler.BorderColor.f[1] = src->Sampler.BorderColor.f[1];
267 dest->Sampler.BorderColor.f[2] = src->Sampler.BorderColor.f[2];
268 dest->Sampler.BorderColor.f[3] = src->Sampler.BorderColor.f[3];
269 dest->Sampler.WrapS = src->Sampler.WrapS;
270 dest->Sampler.WrapT = src->Sampler.WrapT;
271 dest->Sampler.WrapR = src->Sampler.WrapR;
272 dest->Sampler.MinFilter = src->Sampler.MinFilter;
273 dest->Sampler.MagFilter = src->Sampler.MagFilter;
274 dest->Sampler.MinLod = src->Sampler.MinLod;
275 dest->Sampler.MaxLod = src->Sampler.MaxLod;
276 dest->Sampler.LodBias = src->Sampler.LodBias;
277 dest->BaseLevel = src->BaseLevel;
278 dest->MaxLevel = src->MaxLevel;
279 dest->Sampler.MaxAnisotropy = src->Sampler.MaxAnisotropy;
280 dest->Sampler.CompareMode = src->Sampler.CompareMode;
281 dest->Sampler.CompareFunc = src->Sampler.CompareFunc;
282 dest->Sampler.CubeMapSeamless = src->Sampler.CubeMapSeamless;
283 dest->DepthMode = src->DepthMode;
284 dest->StencilSampling = src->StencilSampling;
285 dest->Sampler.sRGBDecode = src->Sampler.sRGBDecode;
286 dest->_MaxLevel = src->_MaxLevel;
287 dest->_MaxLambda = src->_MaxLambda;
288 dest->GenerateMipmap = src->GenerateMipmap;
289 dest->_BaseComplete = src->_BaseComplete;
290 dest->_MipmapComplete = src->_MipmapComplete;
291 COPY_4V(dest->Swizzle, src->Swizzle);
292 dest->_Swizzle = src->_Swizzle;
293
294 dest->RequiredTextureImageUnits = src->RequiredTextureImageUnits;
295 }
296
297
298 /**
299 * Free all texture images of the given texture object.
300 *
301 * \param ctx GL context.
302 * \param t texture object.
303 *
304 * \sa _mesa_clear_texture_image().
305 */
306 void
307 _mesa_clear_texture_object(struct gl_context *ctx,
308 struct gl_texture_object *texObj)
309 {
310 GLuint i, j;
311
312 if (texObj->Target == 0)
313 return;
314
315 for (i = 0; i < MAX_FACES; i++) {
316 for (j = 0; j < MAX_TEXTURE_LEVELS; j++) {
317 struct gl_texture_image *texImage = texObj->Image[i][j];
318 if (texImage)
319 _mesa_clear_texture_image(ctx, texImage);
320 }
321 }
322 }
323
324
325 /**
326 * Check if the given texture object is valid by examining its Target field.
327 * For debugging only.
328 */
329 static GLboolean
330 valid_texture_object(const struct gl_texture_object *tex)
331 {
332 switch (tex->Target) {
333 case 0:
334 case GL_TEXTURE_1D:
335 case GL_TEXTURE_2D:
336 case GL_TEXTURE_3D:
337 case GL_TEXTURE_CUBE_MAP_ARB:
338 case GL_TEXTURE_RECTANGLE_NV:
339 case GL_TEXTURE_1D_ARRAY_EXT:
340 case GL_TEXTURE_2D_ARRAY_EXT:
341 case GL_TEXTURE_BUFFER:
342 case GL_TEXTURE_EXTERNAL_OES:
343 case GL_TEXTURE_CUBE_MAP_ARRAY:
344 case GL_TEXTURE_2D_MULTISAMPLE:
345 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
346 return GL_TRUE;
347 case 0x99:
348 _mesa_problem(NULL, "invalid reference to a deleted texture object");
349 return GL_FALSE;
350 default:
351 _mesa_problem(NULL, "invalid texture object Target 0x%x, Id = %u",
352 tex->Target, tex->Name);
353 return GL_FALSE;
354 }
355 }
356
357
358 /**
359 * Reference (or unreference) a texture object.
360 * If '*ptr', decrement *ptr's refcount (and delete if it becomes zero).
361 * If 'tex' is non-null, increment its refcount.
362 * This is normally only called from the _mesa_reference_texobj() macro
363 * when there's a real pointer change.
364 */
365 void
366 _mesa_reference_texobj_(struct gl_texture_object **ptr,
367 struct gl_texture_object *tex)
368 {
369 assert(ptr);
370
371 if (*ptr) {
372 /* Unreference the old texture */
373 GLboolean deleteFlag = GL_FALSE;
374 struct gl_texture_object *oldTex = *ptr;
375
376 ASSERT(valid_texture_object(oldTex));
377 (void) valid_texture_object; /* silence warning in release builds */
378
379 mtx_lock(&oldTex->Mutex);
380 ASSERT(oldTex->RefCount > 0);
381 oldTex->RefCount--;
382
383 deleteFlag = (oldTex->RefCount == 0);
384 mtx_unlock(&oldTex->Mutex);
385
386 if (deleteFlag) {
387 GET_CURRENT_CONTEXT(ctx);
388 if (ctx)
389 ctx->Driver.DeleteTexture(ctx, oldTex);
390 else
391 _mesa_problem(NULL, "Unable to delete texture, no context");
392 }
393
394 *ptr = NULL;
395 }
396 assert(!*ptr);
397
398 if (tex) {
399 /* reference new texture */
400 ASSERT(valid_texture_object(tex));
401 mtx_lock(&tex->Mutex);
402 if (tex->RefCount == 0) {
403 /* this texture's being deleted (look just above) */
404 /* Not sure this can every really happen. Warn if it does. */
405 _mesa_problem(NULL, "referencing deleted texture object");
406 *ptr = NULL;
407 }
408 else {
409 tex->RefCount++;
410 *ptr = tex;
411 }
412 mtx_unlock(&tex->Mutex);
413 }
414 }
415
416
417 enum base_mipmap { BASE, MIPMAP };
418
419
420 /**
421 * Mark a texture object as incomplete. There are actually three kinds of
422 * (in)completeness:
423 * 1. "base incomplete": the base level of the texture is invalid so no
424 * texturing is possible.
425 * 2. "mipmap incomplete": a non-base level of the texture is invalid so
426 * mipmap filtering isn't possible, but non-mipmap filtering is.
427 * 3. "texture incompleteness": some combination of texture state and
428 * sampler state renders the texture incomplete.
429 *
430 * \param t texture object
431 * \param bm either BASE or MIPMAP to indicate what's incomplete
432 * \param fmt... string describing why it's incomplete (for debugging).
433 */
434 static void
435 incomplete(struct gl_texture_object *t, enum base_mipmap bm,
436 const char *fmt, ...)
437 {
438 if (MESA_DEBUG_FLAGS & DEBUG_INCOMPLETE_TEXTURE) {
439 va_list args;
440 char s[100];
441
442 va_start(args, fmt);
443 vsnprintf(s, sizeof(s), fmt, args);
444 va_end(args);
445
446 _mesa_debug(NULL, "Texture Obj %d incomplete because: %s\n", t->Name, s);
447 }
448
449 if (bm == BASE)
450 t->_BaseComplete = GL_FALSE;
451 t->_MipmapComplete = GL_FALSE;
452 }
453
454
455 /**
456 * Examine a texture object to determine if it is complete.
457 *
458 * The gl_texture_object::Complete flag will be set to GL_TRUE or GL_FALSE
459 * accordingly.
460 *
461 * \param ctx GL context.
462 * \param t texture object.
463 *
464 * According to the texture target, verifies that each of the mipmaps is
465 * present and has the expected size.
466 */
467 void
468 _mesa_test_texobj_completeness( const struct gl_context *ctx,
469 struct gl_texture_object *t )
470 {
471 const GLint baseLevel = t->BaseLevel;
472 const struct gl_texture_image *baseImage;
473 GLint maxLevels = 0;
474
475 /* We'll set these to FALSE if tests fail below */
476 t->_BaseComplete = GL_TRUE;
477 t->_MipmapComplete = GL_TRUE;
478
479 if (t->Target == GL_TEXTURE_BUFFER) {
480 /* Buffer textures are always considered complete. The obvious case where
481 * they would be incomplete (no BO attached) is actually specced to be
482 * undefined rendering results.
483 */
484 return;
485 }
486
487 /* Detect cases where the application set the base level to an invalid
488 * value.
489 */
490 if ((baseLevel < 0) || (baseLevel >= MAX_TEXTURE_LEVELS)) {
491 incomplete(t, BASE, "base level = %d is invalid", baseLevel);
492 return;
493 }
494
495 if (t->MaxLevel < baseLevel) {
496 incomplete(t, MIPMAP, "MAX_LEVEL (%d) < BASE_LEVEL (%d)",
497 t->MaxLevel, baseLevel);
498 return;
499 }
500
501 baseImage = t->Image[0][baseLevel];
502
503 /* Always need the base level image */
504 if (!baseImage) {
505 incomplete(t, BASE, "Image[baseLevel=%d] == NULL", baseLevel);
506 return;
507 }
508
509 /* Check width/height/depth for zero */
510 if (baseImage->Width == 0 ||
511 baseImage->Height == 0 ||
512 baseImage->Depth == 0) {
513 incomplete(t, BASE, "texture width or height or depth = 0");
514 return;
515 }
516
517 /* Check if the texture values are integer */
518 {
519 GLenum datatype = _mesa_get_format_datatype(baseImage->TexFormat);
520 t->_IsIntegerFormat = datatype == GL_INT || datatype == GL_UNSIGNED_INT;
521 }
522
523 /* Compute _MaxLevel (the maximum mipmap level we'll sample from given the
524 * mipmap image sizes and GL_TEXTURE_MAX_LEVEL state).
525 */
526 switch (t->Target) {
527 case GL_TEXTURE_1D:
528 case GL_TEXTURE_1D_ARRAY_EXT:
529 maxLevels = ctx->Const.MaxTextureLevels;
530 break;
531 case GL_TEXTURE_2D:
532 case GL_TEXTURE_2D_ARRAY_EXT:
533 maxLevels = ctx->Const.MaxTextureLevels;
534 break;
535 case GL_TEXTURE_3D:
536 maxLevels = ctx->Const.Max3DTextureLevels;
537 break;
538 case GL_TEXTURE_CUBE_MAP_ARB:
539 case GL_TEXTURE_CUBE_MAP_ARRAY:
540 maxLevels = ctx->Const.MaxCubeTextureLevels;
541 break;
542 case GL_TEXTURE_RECTANGLE_NV:
543 case GL_TEXTURE_BUFFER:
544 case GL_TEXTURE_EXTERNAL_OES:
545 case GL_TEXTURE_2D_MULTISAMPLE:
546 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
547 maxLevels = 1; /* no mipmapping */
548 break;
549 default:
550 _mesa_problem(ctx, "Bad t->Target in _mesa_test_texobj_completeness");
551 return;
552 }
553
554 ASSERT(maxLevels > 0);
555
556 t->_MaxLevel = MIN3(t->MaxLevel,
557 /* 'p' in the GL spec */
558 (int) (baseLevel + baseImage->MaxNumLevels - 1),
559 /* 'q' in the GL spec */
560 maxLevels - 1);
561
562 if (t->Immutable) {
563 /* Adjust max level for views: the data store may have more levels than
564 * the view exposes.
565 */
566 t->_MaxLevel = MIN2(t->_MaxLevel, t->NumLevels - 1);
567 }
568
569 /* Compute _MaxLambda = q - p in the spec used during mipmapping */
570 t->_MaxLambda = (GLfloat) (t->_MaxLevel - baseLevel);
571
572 if (t->Immutable) {
573 /* This texture object was created with glTexStorage1/2/3D() so we
574 * know that all the mipmap levels are the right size and all cube
575 * map faces are the same size.
576 * We don't need to do any of the additional checks below.
577 */
578 return;
579 }
580
581 if (t->Target == GL_TEXTURE_CUBE_MAP_ARB) {
582 /* Make sure that all six cube map level 0 images are the same size.
583 * Note: we know that the image's width==height (we enforce that
584 * at glTexImage time) so we only need to test the width here.
585 */
586 GLuint face;
587 assert(baseImage->Width2 == baseImage->Height);
588 for (face = 1; face < 6; face++) {
589 assert(t->Image[face][baseLevel] == NULL ||
590 t->Image[face][baseLevel]->Width2 ==
591 t->Image[face][baseLevel]->Height2);
592 if (t->Image[face][baseLevel] == NULL ||
593 t->Image[face][baseLevel]->Width2 != baseImage->Width2) {
594 incomplete(t, BASE, "Cube face missing or mismatched size");
595 return;
596 }
597 }
598 }
599
600 /*
601 * Do mipmap consistency checking.
602 * Note: we don't care about the current texture sampler state here.
603 * To determine texture completeness we'll either look at _BaseComplete
604 * or _MipmapComplete depending on the current minification filter mode.
605 */
606 {
607 GLint i;
608 const GLint minLevel = baseLevel;
609 const GLint maxLevel = t->_MaxLevel;
610 const GLuint numFaces = _mesa_num_tex_faces(t->Target);
611 GLuint width, height, depth, face;
612
613 if (minLevel > maxLevel) {
614 incomplete(t, MIPMAP, "minLevel > maxLevel");
615 return;
616 }
617
618 /* Get the base image's dimensions */
619 width = baseImage->Width2;
620 height = baseImage->Height2;
621 depth = baseImage->Depth2;
622
623 /* Note: this loop will be a no-op for RECT, BUFFER, EXTERNAL,
624 * MULTISAMPLE and MULTISAMPLE_ARRAY textures
625 */
626 for (i = baseLevel + 1; i < maxLevels; i++) {
627 /* Compute the expected size of image at level[i] */
628 if (width > 1) {
629 width /= 2;
630 }
631 if (height > 1 && t->Target != GL_TEXTURE_1D_ARRAY) {
632 height /= 2;
633 }
634 if (depth > 1 && t->Target != GL_TEXTURE_2D_ARRAY && t->Target != GL_TEXTURE_CUBE_MAP_ARRAY) {
635 depth /= 2;
636 }
637
638 /* loop over cube faces (or single face otherwise) */
639 for (face = 0; face < numFaces; face++) {
640 if (i >= minLevel && i <= maxLevel) {
641 const struct gl_texture_image *img = t->Image[face][i];
642
643 if (!img) {
644 incomplete(t, MIPMAP, "TexImage[%d] is missing", i);
645 return;
646 }
647 if (img->TexFormat != baseImage->TexFormat) {
648 incomplete(t, MIPMAP, "Format[i] != Format[baseLevel]");
649 return;
650 }
651 if (img->Border != baseImage->Border) {
652 incomplete(t, MIPMAP, "Border[i] != Border[baseLevel]");
653 return;
654 }
655 if (img->Width2 != width) {
656 incomplete(t, MIPMAP, "TexImage[%d] bad width %u", i, img->Width2);
657 return;
658 }
659 if (img->Height2 != height) {
660 incomplete(t, MIPMAP, "TexImage[%d] bad height %u", i, img->Height2);
661 return;
662 }
663 if (img->Depth2 != depth) {
664 incomplete(t, MIPMAP, "TexImage[%d] bad depth %u", i, img->Depth2);
665 return;
666 }
667
668 /* Extra checks for cube textures */
669 if (face > 0) {
670 /* check that cube faces are the same size */
671 if (img->Width2 != t->Image[0][i]->Width2 ||
672 img->Height2 != t->Image[0][i]->Height2) {
673 incomplete(t, MIPMAP, "CubeMap Image[n][i] bad size");
674 return;
675 }
676 }
677 }
678 }
679
680 if (width == 1 && height == 1 && depth == 1) {
681 return; /* found smallest needed mipmap, all done! */
682 }
683 }
684 }
685 }
686
687
688 /**
689 * Check if the given cube map texture is "cube complete" as defined in
690 * the OpenGL specification.
691 */
692 GLboolean
693 _mesa_cube_complete(const struct gl_texture_object *texObj)
694 {
695 const GLint baseLevel = texObj->BaseLevel;
696 const struct gl_texture_image *img0, *img;
697 GLuint face;
698
699 if (texObj->Target != GL_TEXTURE_CUBE_MAP)
700 return GL_FALSE;
701
702 if ((baseLevel < 0) || (baseLevel >= MAX_TEXTURE_LEVELS))
703 return GL_FALSE;
704
705 /* check first face */
706 img0 = texObj->Image[0][baseLevel];
707 if (!img0 ||
708 img0->Width < 1 ||
709 img0->Width != img0->Height)
710 return GL_FALSE;
711
712 /* check remaining faces vs. first face */
713 for (face = 1; face < 6; face++) {
714 img = texObj->Image[face][baseLevel];
715 if (!img ||
716 img->Width != img0->Width ||
717 img->Height != img0->Height ||
718 img->TexFormat != img0->TexFormat)
719 return GL_FALSE;
720 }
721
722 return GL_TRUE;
723 }
724
725
726 /**
727 * Mark a texture object dirty. It forces the object to be incomplete
728 * and forces the context to re-validate its state.
729 *
730 * \param ctx GL context.
731 * \param texObj texture object.
732 */
733 void
734 _mesa_dirty_texobj(struct gl_context *ctx, struct gl_texture_object *texObj)
735 {
736 texObj->_BaseComplete = GL_FALSE;
737 texObj->_MipmapComplete = GL_FALSE;
738 ctx->NewState |= _NEW_TEXTURE;
739 }
740
741
742 /**
743 * Return pointer to a default/fallback texture of the given type/target.
744 * The texture is an RGBA texture with all texels = (0,0,0,1).
745 * That's the value a GLSL sampler should get when sampling from an
746 * incomplete texture.
747 */
748 struct gl_texture_object *
749 _mesa_get_fallback_texture(struct gl_context *ctx, gl_texture_index tex)
750 {
751 if (!ctx->Shared->FallbackTex[tex]) {
752 /* create fallback texture now */
753 const GLsizei width = 1, height = 1, depth = 1;
754 GLubyte texel[4];
755 struct gl_texture_object *texObj;
756 struct gl_texture_image *texImage;
757 mesa_format texFormat;
758 GLuint dims, face, numFaces = 1;
759 GLenum target;
760
761 texel[0] =
762 texel[1] =
763 texel[2] = 0x0;
764 texel[3] = 0xff;
765
766 switch (tex) {
767 case TEXTURE_2D_ARRAY_INDEX:
768 dims = 3;
769 target = GL_TEXTURE_2D_ARRAY;
770 break;
771 case TEXTURE_1D_ARRAY_INDEX:
772 dims = 2;
773 target = GL_TEXTURE_1D_ARRAY;
774 break;
775 case TEXTURE_CUBE_INDEX:
776 dims = 2;
777 target = GL_TEXTURE_CUBE_MAP;
778 numFaces = 6;
779 break;
780 case TEXTURE_3D_INDEX:
781 dims = 3;
782 target = GL_TEXTURE_3D;
783 break;
784 case TEXTURE_RECT_INDEX:
785 dims = 2;
786 target = GL_TEXTURE_RECTANGLE;
787 break;
788 case TEXTURE_2D_INDEX:
789 dims = 2;
790 target = GL_TEXTURE_2D;
791 break;
792 case TEXTURE_1D_INDEX:
793 dims = 1;
794 target = GL_TEXTURE_1D;
795 break;
796 case TEXTURE_BUFFER_INDEX:
797 dims = 0;
798 target = GL_TEXTURE_BUFFER;
799 break;
800 case TEXTURE_CUBE_ARRAY_INDEX:
801 dims = 3;
802 target = GL_TEXTURE_CUBE_MAP_ARRAY;
803 break;
804 case TEXTURE_EXTERNAL_INDEX:
805 dims = 2;
806 target = GL_TEXTURE_EXTERNAL_OES;
807 break;
808 case TEXTURE_2D_MULTISAMPLE_INDEX:
809 dims = 2;
810 target = GL_TEXTURE_2D_MULTISAMPLE;
811 break;
812 case TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX:
813 dims = 3;
814 target = GL_TEXTURE_2D_MULTISAMPLE_ARRAY;
815 break;
816 default:
817 /* no-op */
818 return NULL;
819 }
820
821 /* create texture object */
822 texObj = ctx->Driver.NewTextureObject(ctx, 0, target);
823 if (!texObj)
824 return NULL;
825
826 assert(texObj->RefCount == 1);
827 texObj->Sampler.MinFilter = GL_NEAREST;
828 texObj->Sampler.MagFilter = GL_NEAREST;
829
830 texFormat = ctx->Driver.ChooseTextureFormat(ctx, target,
831 GL_RGBA, GL_RGBA,
832 GL_UNSIGNED_BYTE);
833
834 /* need a loop here just for cube maps */
835 for (face = 0; face < numFaces; face++) {
836 GLenum faceTarget;
837
838 if (target == GL_TEXTURE_CUBE_MAP)
839 faceTarget = GL_TEXTURE_CUBE_MAP_POSITIVE_X + face;
840 else
841 faceTarget = target;
842
843 /* initialize level[0] texture image */
844 texImage = _mesa_get_tex_image(ctx, texObj, faceTarget, 0);
845
846 _mesa_init_teximage_fields(ctx, texImage,
847 width,
848 (dims > 1) ? height : 1,
849 (dims > 2) ? depth : 1,
850 0, /* border */
851 GL_RGBA, texFormat);
852
853 ctx->Driver.TexImage(ctx, dims, texImage,
854 GL_RGBA, GL_UNSIGNED_BYTE, texel,
855 &ctx->DefaultPacking);
856 }
857
858 _mesa_test_texobj_completeness(ctx, texObj);
859 assert(texObj->_BaseComplete);
860 assert(texObj->_MipmapComplete);
861
862 ctx->Shared->FallbackTex[tex] = texObj;
863 }
864 return ctx->Shared->FallbackTex[tex];
865 }
866
867
868 /**
869 * Compute the size of the given texture object, in bytes.
870 */
871 static GLuint
872 texture_size(const struct gl_texture_object *texObj)
873 {
874 const GLuint numFaces = _mesa_num_tex_faces(texObj->Target);
875 GLuint face, level, size = 0;
876
877 for (face = 0; face < numFaces; face++) {
878 for (level = 0; level < MAX_TEXTURE_LEVELS; level++) {
879 const struct gl_texture_image *img = texObj->Image[face][level];
880 if (img) {
881 GLuint sz = _mesa_format_image_size(img->TexFormat, img->Width,
882 img->Height, img->Depth);
883 size += sz;
884 }
885 }
886 }
887
888 return size;
889 }
890
891
892 /**
893 * Callback called from _mesa_HashWalk()
894 */
895 static void
896 count_tex_size(GLuint key, void *data, void *userData)
897 {
898 const struct gl_texture_object *texObj =
899 (const struct gl_texture_object *) data;
900 GLuint *total = (GLuint *) userData;
901
902 (void) key;
903
904 *total = *total + texture_size(texObj);
905 }
906
907
908 /**
909 * Compute total size (in bytes) of all textures for the given context.
910 * For debugging purposes.
911 */
912 GLuint
913 _mesa_total_texture_memory(struct gl_context *ctx)
914 {
915 GLuint tgt, total = 0;
916
917 _mesa_HashWalk(ctx->Shared->TexObjects, count_tex_size, &total);
918
919 /* plus, the default texture objects */
920 for (tgt = 0; tgt < NUM_TEXTURE_TARGETS; tgt++) {
921 total += texture_size(ctx->Shared->DefaultTex[tgt]);
922 }
923
924 return total;
925 }
926
927 static struct gl_texture_object *
928 invalidate_tex_image_error_check(struct gl_context *ctx, GLuint texture,
929 GLint level, const char *name)
930 {
931 /* The GL_ARB_invalidate_subdata spec says:
932 *
933 * "If <texture> is zero or is not the name of a texture, the error
934 * INVALID_VALUE is generated."
935 *
936 * This performs the error check in a different order than listed in the
937 * spec. We have to get the texture object before we can validate the
938 * other parameters against values in the texture object.
939 */
940 struct gl_texture_object *const t = _mesa_lookup_texture(ctx, texture);
941 if (texture == 0 || t == NULL) {
942 _mesa_error(ctx, GL_INVALID_VALUE, "%s(texture)", name);
943 return NULL;
944 }
945
946 /* The GL_ARB_invalidate_subdata spec says:
947 *
948 * "If <level> is less than zero or greater than the base 2 logarithm
949 * of the maximum texture width, height, or depth, the error
950 * INVALID_VALUE is generated."
951 */
952 if (level < 0 || level > t->MaxLevel) {
953 _mesa_error(ctx, GL_INVALID_VALUE, "%s(level)", name);
954 return NULL;
955 }
956
957 /* The GL_ARB_invalidate_subdata spec says:
958 *
959 * "If the target of <texture> is TEXTURE_RECTANGLE, TEXTURE_BUFFER,
960 * TEXTURE_2D_MULTISAMPLE, or TEXTURE_2D_MULTISAMPLE_ARRAY, and <level>
961 * is not zero, the error INVALID_VALUE is generated."
962 */
963 if (level != 0) {
964 switch (t->Target) {
965 case GL_TEXTURE_RECTANGLE:
966 case GL_TEXTURE_BUFFER:
967 case GL_TEXTURE_2D_MULTISAMPLE:
968 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
969 _mesa_error(ctx, GL_INVALID_VALUE, "%s(level)", name);
970 return NULL;
971
972 default:
973 break;
974 }
975 }
976
977 return t;
978 }
979
980 /*@}*/
981
982
983 /***********************************************************************/
984 /** \name API functions */
985 /*@{*/
986
987
988 /**
989 * Generate texture names.
990 *
991 * \param n number of texture names to be generated.
992 * \param textures an array in which will hold the generated texture names.
993 *
994 * \sa glGenTextures().
995 *
996 * Calls _mesa_HashFindFreeKeyBlock() to find a block of free texture
997 * IDs which are stored in \p textures. Corresponding empty texture
998 * objects are also generated.
999 */
1000 void GLAPIENTRY
1001 _mesa_GenTextures( GLsizei n, GLuint *textures )
1002 {
1003 GET_CURRENT_CONTEXT(ctx);
1004 GLuint first;
1005 GLint i;
1006
1007 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
1008 _mesa_debug(ctx, "glGenTextures %d\n", n);
1009
1010 if (n < 0) {
1011 _mesa_error( ctx, GL_INVALID_VALUE, "glGenTextures" );
1012 return;
1013 }
1014
1015 if (!textures)
1016 return;
1017
1018 /*
1019 * This must be atomic (generation and allocation of texture IDs)
1020 */
1021 mtx_lock(&ctx->Shared->Mutex);
1022
1023 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->TexObjects, n);
1024
1025 /* Allocate new, empty texture objects */
1026 for (i = 0; i < n; i++) {
1027 struct gl_texture_object *texObj;
1028 GLuint name = first + i;
1029 GLenum target = 0;
1030 texObj = ctx->Driver.NewTextureObject(ctx, name, target);
1031 if (!texObj) {
1032 mtx_unlock(&ctx->Shared->Mutex);
1033 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGenTextures");
1034 return;
1035 }
1036
1037 /* insert into hash table */
1038 _mesa_HashInsert(ctx->Shared->TexObjects, texObj->Name, texObj);
1039
1040 textures[i] = name;
1041 }
1042
1043 mtx_unlock(&ctx->Shared->Mutex);
1044 }
1045
1046
1047 /**
1048 * Check if the given texture object is bound to the current draw or
1049 * read framebuffer. If so, Unbind it.
1050 */
1051 static void
1052 unbind_texobj_from_fbo(struct gl_context *ctx,
1053 struct gl_texture_object *texObj)
1054 {
1055 bool progress = false;
1056
1057 /* Section 4.4.2 (Attaching Images to Framebuffer Objects), subsection
1058 * "Attaching Texture Images to a Framebuffer," of the OpenGL 3.1 spec
1059 * says:
1060 *
1061 * "If a texture object is deleted while its image is attached to one
1062 * or more attachment points in the currently bound framebuffer, then
1063 * it is as if FramebufferTexture* had been called, with a texture of
1064 * zero, for each attachment point to which this image was attached in
1065 * the currently bound framebuffer. In other words, this texture image
1066 * is first detached from all attachment points in the currently bound
1067 * framebuffer. Note that the texture image is specifically not
1068 * detached from any other framebuffer objects. Detaching the texture
1069 * image from any other framebuffer objects is the responsibility of
1070 * the application."
1071 */
1072 if (_mesa_is_user_fbo(ctx->DrawBuffer)) {
1073 progress = _mesa_detach_renderbuffer(ctx, ctx->DrawBuffer, texObj);
1074 }
1075 if (_mesa_is_user_fbo(ctx->ReadBuffer)
1076 && ctx->ReadBuffer != ctx->DrawBuffer) {
1077 progress = _mesa_detach_renderbuffer(ctx, ctx->ReadBuffer, texObj)
1078 || progress;
1079 }
1080
1081 if (progress)
1082 /* Vertices are already flushed by _mesa_DeleteTextures */
1083 ctx->NewState |= _NEW_BUFFERS;
1084 }
1085
1086
1087 /**
1088 * Check if the given texture object is bound to any texture image units and
1089 * unbind it if so (revert to default textures).
1090 */
1091 static void
1092 unbind_texobj_from_texunits(struct gl_context *ctx,
1093 struct gl_texture_object *texObj)
1094 {
1095 GLuint u, tex;
1096
1097 for (u = 0; u < ctx->Texture.NumCurrentTexUsed; u++) {
1098 struct gl_texture_unit *unit = &ctx->Texture.Unit[u];
1099 for (tex = 0; tex < NUM_TEXTURE_TARGETS; tex++) {
1100 if (texObj == unit->CurrentTex[tex]) {
1101 _mesa_reference_texobj(&unit->CurrentTex[tex],
1102 ctx->Shared->DefaultTex[tex]);
1103 ASSERT(unit->CurrentTex[tex]);
1104 break;
1105 }
1106 }
1107 }
1108 }
1109
1110
1111 /**
1112 * Check if the given texture object is bound to any shader image unit
1113 * and unbind it if that's the case.
1114 */
1115 static void
1116 unbind_texobj_from_image_units(struct gl_context *ctx,
1117 struct gl_texture_object *texObj)
1118 {
1119 GLuint i;
1120
1121 for (i = 0; i < ctx->Const.MaxImageUnits; i++) {
1122 struct gl_image_unit *unit = &ctx->ImageUnits[i];
1123
1124 if (texObj == unit->TexObj)
1125 _mesa_reference_texobj(&unit->TexObj, NULL);
1126 }
1127 }
1128
1129
1130 /**
1131 * Delete named textures.
1132 *
1133 * \param n number of textures to be deleted.
1134 * \param textures array of texture IDs to be deleted.
1135 *
1136 * \sa glDeleteTextures().
1137 *
1138 * If we're about to delete a texture that's currently bound to any
1139 * texture unit, unbind the texture first. Decrement the reference
1140 * count on the texture object and delete it if it's zero.
1141 * Recall that texture objects can be shared among several rendering
1142 * contexts.
1143 */
1144 void GLAPIENTRY
1145 _mesa_DeleteTextures( GLsizei n, const GLuint *textures)
1146 {
1147 GET_CURRENT_CONTEXT(ctx);
1148 GLint i;
1149
1150 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
1151 _mesa_debug(ctx, "glDeleteTextures %d\n", n);
1152
1153 FLUSH_VERTICES(ctx, 0); /* too complex */
1154
1155 if (!textures)
1156 return;
1157
1158 for (i = 0; i < n; i++) {
1159 if (textures[i] > 0) {
1160 struct gl_texture_object *delObj
1161 = _mesa_lookup_texture(ctx, textures[i]);
1162
1163 if (delObj) {
1164 _mesa_lock_texture(ctx, delObj);
1165
1166 /* Check if texture is bound to any framebuffer objects.
1167 * If so, unbind.
1168 * See section 4.4.2.3 of GL_EXT_framebuffer_object.
1169 */
1170 unbind_texobj_from_fbo(ctx, delObj);
1171
1172 /* Check if this texture is currently bound to any texture units.
1173 * If so, unbind it.
1174 */
1175 unbind_texobj_from_texunits(ctx, delObj);
1176
1177 /* Check if this texture is currently bound to any shader
1178 * image unit. If so, unbind it.
1179 * See section 3.9.X of GL_ARB_shader_image_load_store.
1180 */
1181 unbind_texobj_from_image_units(ctx, delObj);
1182
1183 _mesa_unlock_texture(ctx, delObj);
1184
1185 ctx->NewState |= _NEW_TEXTURE;
1186
1187 /* The texture _name_ is now free for re-use.
1188 * Remove it from the hash table now.
1189 */
1190 mtx_lock(&ctx->Shared->Mutex);
1191 _mesa_HashRemove(ctx->Shared->TexObjects, delObj->Name);
1192 mtx_unlock(&ctx->Shared->Mutex);
1193
1194 /* Unreference the texobj. If refcount hits zero, the texture
1195 * will be deleted.
1196 */
1197 _mesa_reference_texobj(&delObj, NULL);
1198 }
1199 }
1200 }
1201 }
1202
1203
1204 /**
1205 * Convert a GL texture target enum such as GL_TEXTURE_2D or GL_TEXTURE_3D
1206 * into the corresponding Mesa texture target index.
1207 * Note that proxy targets are not valid here.
1208 * \return TEXTURE_x_INDEX or -1 if target is invalid
1209 */
1210 int
1211 _mesa_tex_target_to_index(const struct gl_context *ctx, GLenum target)
1212 {
1213 switch (target) {
1214 case GL_TEXTURE_1D:
1215 return _mesa_is_desktop_gl(ctx) ? TEXTURE_1D_INDEX : -1;
1216 case GL_TEXTURE_2D:
1217 return TEXTURE_2D_INDEX;
1218 case GL_TEXTURE_3D:
1219 return ctx->API != API_OPENGLES ? TEXTURE_3D_INDEX : -1;
1220 case GL_TEXTURE_CUBE_MAP:
1221 return ctx->Extensions.ARB_texture_cube_map
1222 ? TEXTURE_CUBE_INDEX : -1;
1223 case GL_TEXTURE_RECTANGLE:
1224 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.NV_texture_rectangle
1225 ? TEXTURE_RECT_INDEX : -1;
1226 case GL_TEXTURE_1D_ARRAY:
1227 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_array
1228 ? TEXTURE_1D_ARRAY_INDEX : -1;
1229 case GL_TEXTURE_2D_ARRAY:
1230 return (_mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_array)
1231 || _mesa_is_gles3(ctx)
1232 ? TEXTURE_2D_ARRAY_INDEX : -1;
1233 case GL_TEXTURE_BUFFER:
1234 return ctx->API == API_OPENGL_CORE &&
1235 ctx->Extensions.ARB_texture_buffer_object ?
1236 TEXTURE_BUFFER_INDEX : -1;
1237 case GL_TEXTURE_EXTERNAL_OES:
1238 return _mesa_is_gles(ctx) && ctx->Extensions.OES_EGL_image_external
1239 ? TEXTURE_EXTERNAL_INDEX : -1;
1240 case GL_TEXTURE_CUBE_MAP_ARRAY:
1241 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.ARB_texture_cube_map_array
1242 ? TEXTURE_CUBE_ARRAY_INDEX : -1;
1243 case GL_TEXTURE_2D_MULTISAMPLE:
1244 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.ARB_texture_multisample
1245 ? TEXTURE_2D_MULTISAMPLE_INDEX: -1;
1246 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
1247 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.ARB_texture_multisample
1248 ? TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX: -1;
1249 default:
1250 return -1;
1251 }
1252 }
1253
1254
1255 /**
1256 * Bind a named texture to a texturing target.
1257 *
1258 * \param target texture target.
1259 * \param texName texture name.
1260 *
1261 * \sa glBindTexture().
1262 *
1263 * Determines the old texture object bound and returns immediately if rebinding
1264 * the same texture. Get the current texture which is either a default texture
1265 * if name is null, a named texture from the hash, or a new texture if the
1266 * given texture name is new. Increments its reference count, binds it, and
1267 * calls dd_function_table::BindTexture. Decrements the old texture reference
1268 * count and deletes it if it reaches zero.
1269 */
1270 void GLAPIENTRY
1271 _mesa_BindTexture( GLenum target, GLuint texName )
1272 {
1273 GET_CURRENT_CONTEXT(ctx);
1274 struct gl_texture_unit *texUnit = _mesa_get_current_tex_unit(ctx);
1275 struct gl_texture_object *newTexObj = NULL;
1276 GLint targetIndex;
1277
1278 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
1279 _mesa_debug(ctx, "glBindTexture %s %d\n",
1280 _mesa_lookup_enum_by_nr(target), (GLint) texName);
1281
1282 targetIndex = _mesa_tex_target_to_index(ctx, target);
1283 if (targetIndex < 0) {
1284 _mesa_error(ctx, GL_INVALID_ENUM, "glBindTexture(target)");
1285 return;
1286 }
1287 assert(targetIndex < NUM_TEXTURE_TARGETS);
1288
1289 /*
1290 * Get pointer to new texture object (newTexObj)
1291 */
1292 if (texName == 0) {
1293 /* Use a default texture object */
1294 newTexObj = ctx->Shared->DefaultTex[targetIndex];
1295 }
1296 else {
1297 /* non-default texture object */
1298 newTexObj = _mesa_lookup_texture(ctx, texName);
1299 if (newTexObj) {
1300 /* error checking */
1301 if (newTexObj->Target != 0 && newTexObj->Target != target) {
1302 /* the named texture object's target doesn't match the given target */
1303 _mesa_error( ctx, GL_INVALID_OPERATION,
1304 "glBindTexture(target mismatch)" );
1305 return;
1306 }
1307 if (newTexObj->Target == 0) {
1308 finish_texture_init(ctx, target, newTexObj);
1309 }
1310 }
1311 else {
1312 if (ctx->API == API_OPENGL_CORE) {
1313 _mesa_error(ctx, GL_INVALID_OPERATION, "glBindTexture(non-gen name)");
1314 return;
1315 }
1316
1317 /* if this is a new texture id, allocate a texture object now */
1318 newTexObj = ctx->Driver.NewTextureObject(ctx, texName, target);
1319 if (!newTexObj) {
1320 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindTexture");
1321 return;
1322 }
1323
1324 /* and insert it into hash table */
1325 mtx_lock(&ctx->Shared->Mutex);
1326 _mesa_HashInsert(ctx->Shared->TexObjects, texName, newTexObj);
1327 mtx_unlock(&ctx->Shared->Mutex);
1328 }
1329 newTexObj->Target = target;
1330 }
1331
1332 assert(valid_texture_object(newTexObj));
1333
1334 /* Check if this texture is only used by this context and is already bound.
1335 * If so, just return.
1336 */
1337 {
1338 GLboolean early_out;
1339 mtx_lock(&ctx->Shared->Mutex);
1340 early_out = ((ctx->Shared->RefCount == 1)
1341 && (newTexObj == texUnit->CurrentTex[targetIndex]));
1342 mtx_unlock(&ctx->Shared->Mutex);
1343 if (early_out) {
1344 return;
1345 }
1346 }
1347
1348 /* flush before changing binding */
1349 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
1350
1351 /* Do the actual binding. The refcount on the previously bound
1352 * texture object will be decremented. It'll be deleted if the
1353 * count hits zero.
1354 */
1355 _mesa_reference_texobj(&texUnit->CurrentTex[targetIndex], newTexObj);
1356 ctx->Texture.NumCurrentTexUsed = MAX2(ctx->Texture.NumCurrentTexUsed,
1357 ctx->Texture.CurrentUnit + 1);
1358 ASSERT(texUnit->CurrentTex[targetIndex]);
1359
1360 /* Pass BindTexture call to device driver */
1361 if (ctx->Driver.BindTexture)
1362 ctx->Driver.BindTexture(ctx, target, newTexObj);
1363 }
1364
1365
1366 /**
1367 * Set texture priorities.
1368 *
1369 * \param n number of textures.
1370 * \param texName texture names.
1371 * \param priorities corresponding texture priorities.
1372 *
1373 * \sa glPrioritizeTextures().
1374 *
1375 * Looks up each texture in the hash, clamps the corresponding priority between
1376 * 0.0 and 1.0, and calls dd_function_table::PrioritizeTexture.
1377 */
1378 void GLAPIENTRY
1379 _mesa_PrioritizeTextures( GLsizei n, const GLuint *texName,
1380 const GLclampf *priorities )
1381 {
1382 GET_CURRENT_CONTEXT(ctx);
1383 GLint i;
1384
1385 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
1386 _mesa_debug(ctx, "glPrioritizeTextures %d\n", n);
1387
1388 FLUSH_VERTICES(ctx, 0);
1389
1390 if (n < 0) {
1391 _mesa_error( ctx, GL_INVALID_VALUE, "glPrioritizeTextures" );
1392 return;
1393 }
1394
1395 if (!priorities)
1396 return;
1397
1398 for (i = 0; i < n; i++) {
1399 if (texName[i] > 0) {
1400 struct gl_texture_object *t = _mesa_lookup_texture(ctx, texName[i]);
1401 if (t) {
1402 t->Priority = CLAMP( priorities[i], 0.0F, 1.0F );
1403 }
1404 }
1405 }
1406
1407 ctx->NewState |= _NEW_TEXTURE;
1408 }
1409
1410
1411
1412 /**
1413 * See if textures are loaded in texture memory.
1414 *
1415 * \param n number of textures to query.
1416 * \param texName array with the texture names.
1417 * \param residences array which will hold the residence status.
1418 *
1419 * \return GL_TRUE if all textures are resident and \p residences is left unchanged,
1420 *
1421 * Note: we assume all textures are always resident
1422 */
1423 GLboolean GLAPIENTRY
1424 _mesa_AreTexturesResident(GLsizei n, const GLuint *texName,
1425 GLboolean *residences)
1426 {
1427 GET_CURRENT_CONTEXT(ctx);
1428 GLboolean allResident = GL_TRUE;
1429 GLint i;
1430 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1431
1432 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
1433 _mesa_debug(ctx, "glAreTexturesResident %d\n", n);
1434
1435 if (n < 0) {
1436 _mesa_error(ctx, GL_INVALID_VALUE, "glAreTexturesResident(n)");
1437 return GL_FALSE;
1438 }
1439
1440 if (!texName || !residences)
1441 return GL_FALSE;
1442
1443 /* We only do error checking on the texture names */
1444 for (i = 0; i < n; i++) {
1445 struct gl_texture_object *t;
1446 if (texName[i] == 0) {
1447 _mesa_error(ctx, GL_INVALID_VALUE, "glAreTexturesResident");
1448 return GL_FALSE;
1449 }
1450 t = _mesa_lookup_texture(ctx, texName[i]);
1451 if (!t) {
1452 _mesa_error(ctx, GL_INVALID_VALUE, "glAreTexturesResident");
1453 return GL_FALSE;
1454 }
1455 }
1456
1457 return allResident;
1458 }
1459
1460
1461 /**
1462 * See if a name corresponds to a texture.
1463 *
1464 * \param texture texture name.
1465 *
1466 * \return GL_TRUE if texture name corresponds to a texture, or GL_FALSE
1467 * otherwise.
1468 *
1469 * \sa glIsTexture().
1470 *
1471 * Calls _mesa_HashLookup().
1472 */
1473 GLboolean GLAPIENTRY
1474 _mesa_IsTexture( GLuint texture )
1475 {
1476 struct gl_texture_object *t;
1477 GET_CURRENT_CONTEXT(ctx);
1478 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1479
1480 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
1481 _mesa_debug(ctx, "glIsTexture %d\n", texture);
1482
1483 if (!texture)
1484 return GL_FALSE;
1485
1486 t = _mesa_lookup_texture(ctx, texture);
1487
1488 /* IsTexture is true only after object has been bound once. */
1489 return t && t->Target;
1490 }
1491
1492
1493 /**
1494 * Simplest implementation of texture locking: grab the shared tex
1495 * mutex. Examine the shared context state timestamp and if there has
1496 * been a change, set the appropriate bits in ctx->NewState.
1497 *
1498 * This is used to deal with synchronizing things when a texture object
1499 * is used/modified by different contexts (or threads) which are sharing
1500 * the texture.
1501 *
1502 * See also _mesa_lock/unlock_texture() in teximage.h
1503 */
1504 void
1505 _mesa_lock_context_textures( struct gl_context *ctx )
1506 {
1507 mtx_lock(&ctx->Shared->TexMutex);
1508
1509 if (ctx->Shared->TextureStateStamp != ctx->TextureStateTimestamp) {
1510 ctx->NewState |= _NEW_TEXTURE;
1511 ctx->TextureStateTimestamp = ctx->Shared->TextureStateStamp;
1512 }
1513 }
1514
1515
1516 void
1517 _mesa_unlock_context_textures( struct gl_context *ctx )
1518 {
1519 assert(ctx->Shared->TextureStateStamp == ctx->TextureStateTimestamp);
1520 mtx_unlock(&ctx->Shared->TexMutex);
1521 }
1522
1523 void GLAPIENTRY
1524 _mesa_InvalidateTexSubImage(GLuint texture, GLint level, GLint xoffset,
1525 GLint yoffset, GLint zoffset, GLsizei width,
1526 GLsizei height, GLsizei depth)
1527 {
1528 struct gl_texture_object *t;
1529 struct gl_texture_image *image;
1530 GET_CURRENT_CONTEXT(ctx);
1531
1532 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
1533 _mesa_debug(ctx, "glInvalidateTexSubImage %d\n", texture);
1534
1535 t = invalidate_tex_image_error_check(ctx, texture, level,
1536 "glInvalidateTexSubImage");
1537
1538 /* The GL_ARB_invalidate_subdata spec says:
1539 *
1540 * "...the specified subregion must be between -<b> and <dim>+<b> where
1541 * <dim> is the size of the dimension of the texture image, and <b> is
1542 * the size of the border of that texture image, otherwise
1543 * INVALID_VALUE is generated (border is not applied to dimensions that
1544 * don't exist in a given texture target)."
1545 */
1546 image = t->Image[0][level];
1547 if (image) {
1548 int xBorder;
1549 int yBorder;
1550 int zBorder;
1551 int imageWidth;
1552 int imageHeight;
1553 int imageDepth;
1554
1555 /* The GL_ARB_invalidate_subdata spec says:
1556 *
1557 * "For texture targets that don't have certain dimensions, this
1558 * command treats those dimensions as having a size of 1. For
1559 * example, to invalidate a portion of a two-dimensional texture,
1560 * the application would use <zoffset> equal to zero and <depth>
1561 * equal to one."
1562 */
1563 switch (t->Target) {
1564 case GL_TEXTURE_BUFFER:
1565 xBorder = 0;
1566 yBorder = 0;
1567 zBorder = 0;
1568 imageWidth = 1;
1569 imageHeight = 1;
1570 imageDepth = 1;
1571 break;
1572 case GL_TEXTURE_1D:
1573 xBorder = image->Border;
1574 yBorder = 0;
1575 zBorder = 0;
1576 imageWidth = image->Width;
1577 imageHeight = 1;
1578 imageDepth = 1;
1579 break;
1580 case GL_TEXTURE_1D_ARRAY:
1581 xBorder = image->Border;
1582 yBorder = 0;
1583 zBorder = 0;
1584 imageWidth = image->Width;
1585 imageHeight = image->Height;
1586 imageDepth = 1;
1587 break;
1588 case GL_TEXTURE_2D:
1589 case GL_TEXTURE_CUBE_MAP:
1590 case GL_TEXTURE_RECTANGLE:
1591 case GL_TEXTURE_2D_MULTISAMPLE:
1592 xBorder = image->Border;
1593 yBorder = image->Border;
1594 zBorder = 0;
1595 imageWidth = image->Width;
1596 imageHeight = image->Height;
1597 imageDepth = 1;
1598 break;
1599 case GL_TEXTURE_2D_ARRAY:
1600 case GL_TEXTURE_CUBE_MAP_ARRAY:
1601 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
1602 xBorder = image->Border;
1603 yBorder = image->Border;
1604 zBorder = 0;
1605 imageWidth = image->Width;
1606 imageHeight = image->Height;
1607 imageDepth = image->Depth;
1608 break;
1609 case GL_TEXTURE_3D:
1610 xBorder = image->Border;
1611 yBorder = image->Border;
1612 zBorder = image->Border;
1613 imageWidth = image->Width;
1614 imageHeight = image->Height;
1615 imageDepth = image->Depth;
1616 break;
1617 default:
1618 assert(!"Should not get here.");
1619 xBorder = 0;
1620 yBorder = 0;
1621 zBorder = 0;
1622 imageWidth = 0;
1623 imageHeight = 0;
1624 imageDepth = 0;
1625 break;
1626 }
1627
1628 if (xoffset < -xBorder) {
1629 _mesa_error(ctx, GL_INVALID_VALUE, "glInvalidateSubTexImage(xoffset)");
1630 return;
1631 }
1632
1633 if (xoffset + width > imageWidth + xBorder) {
1634 _mesa_error(ctx, GL_INVALID_VALUE,
1635 "glInvalidateSubTexImage(xoffset+width)");
1636 return;
1637 }
1638
1639 if (yoffset < -yBorder) {
1640 _mesa_error(ctx, GL_INVALID_VALUE, "glInvalidateSubTexImage(yoffset)");
1641 return;
1642 }
1643
1644 if (yoffset + height > imageHeight + yBorder) {
1645 _mesa_error(ctx, GL_INVALID_VALUE,
1646 "glInvalidateSubTexImage(yoffset+height)");
1647 return;
1648 }
1649
1650 if (zoffset < -zBorder) {
1651 _mesa_error(ctx, GL_INVALID_VALUE,
1652 "glInvalidateSubTexImage(zoffset)");
1653 return;
1654 }
1655
1656 if (zoffset + depth > imageDepth + zBorder) {
1657 _mesa_error(ctx, GL_INVALID_VALUE,
1658 "glInvalidateSubTexImage(zoffset+depth)");
1659 return;
1660 }
1661 }
1662
1663 /* We don't actually do anything for this yet. Just return after
1664 * validating the parameters and generating the required errors.
1665 */
1666 return;
1667 }
1668
1669 void GLAPIENTRY
1670 _mesa_InvalidateTexImage(GLuint texture, GLint level)
1671 {
1672 GET_CURRENT_CONTEXT(ctx);
1673
1674 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
1675 _mesa_debug(ctx, "glInvalidateTexImage(%d, %d)\n", texture, level);
1676
1677 invalidate_tex_image_error_check(ctx, texture, level,
1678 "glInvalidateTexImage");
1679
1680 /* We don't actually do anything for this yet. Just return after
1681 * validating the parameters and generating the required errors.
1682 */
1683 return;
1684 }
1685
1686 /*@}*/