i965/fs: Fix regression in comparison handling from ANDs change.
[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 * Version: 7.1
9 *
10 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
11 *
12 * Permission is hereby granted, free of charge, to any person obtaining a
13 * copy of this software and associated documentation files (the "Software"),
14 * to deal in the Software without restriction, including without limitation
15 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16 * and/or sell copies of the Software, and to permit persons to whom the
17 * Software is furnished to do so, subject to the following conditions:
18 *
19 * The above copyright notice and this permission notice shall be included
20 * in all copies or substantial portions of the Software.
21 *
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
27 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 */
29
30
31 #include "mfeatures.h"
32 #include "bufferobj.h"
33 #include "colortab.h"
34 #include "context.h"
35 #include "enums.h"
36 #include "fbobject.h"
37 #include "formats.h"
38 #include "hash.h"
39 #include "imports.h"
40 #include "macros.h"
41 #include "teximage.h"
42 #include "texobj.h"
43 #include "texstate.h"
44 #include "mtypes.h"
45 #include "program/prog_instruction.h"
46
47
48
49 /**********************************************************************/
50 /** \name Internal functions */
51 /*@{*/
52
53
54 /**
55 * Return the gl_texture_object for a given ID.
56 */
57 struct gl_texture_object *
58 _mesa_lookup_texture(struct gl_context *ctx, GLuint id)
59 {
60 return (struct gl_texture_object *)
61 _mesa_HashLookup(ctx->Shared->TexObjects, id);
62 }
63
64
65
66 /**
67 * Allocate and initialize a new texture object. But don't put it into the
68 * texture object hash table.
69 *
70 * Called via ctx->Driver.NewTextureObject, unless overridden by a device
71 * driver.
72 *
73 * \param shared the shared GL state structure to contain the texture object
74 * \param name integer name for the texture object
75 * \param target either GL_TEXTURE_1D, GL_TEXTURE_2D, GL_TEXTURE_3D,
76 * GL_TEXTURE_CUBE_MAP_ARB or GL_TEXTURE_RECTANGLE_NV. zero is ok for the sake
77 * of GenTextures()
78 *
79 * \return pointer to new texture object.
80 */
81 struct gl_texture_object *
82 _mesa_new_texture_object( struct gl_context *ctx, GLuint name, GLenum target )
83 {
84 struct gl_texture_object *obj;
85 (void) ctx;
86 obj = MALLOC_STRUCT(gl_texture_object);
87 _mesa_initialize_texture_object(obj, name, target);
88 return obj;
89 }
90
91
92 /**
93 * Initialize a new texture object to default values.
94 * \param obj the texture object
95 * \param name the texture name
96 * \param target the texture target
97 */
98 void
99 _mesa_initialize_texture_object( 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_BUFFER);
112
113 memset(obj, 0, sizeof(*obj));
114 /* init the non-zero fields */
115 _glthread_INIT_MUTEX(obj->Mutex);
116 obj->RefCount = 1;
117 obj->Name = name;
118 obj->Target = target;
119 obj->Priority = 1.0F;
120 obj->BaseLevel = 0;
121 obj->MaxLevel = 1000;
122
123 /* must be one; no support for (YUV) planes in separate buffers */
124 obj->RequiredTextureImageUnits = 1;
125
126 /* sampler state */
127 if (target == GL_TEXTURE_RECTANGLE_NV ||
128 target == GL_TEXTURE_EXTERNAL_OES) {
129 obj->Sampler.WrapS = GL_CLAMP_TO_EDGE;
130 obj->Sampler.WrapT = GL_CLAMP_TO_EDGE;
131 obj->Sampler.WrapR = GL_CLAMP_TO_EDGE;
132 obj->Sampler.MinFilter = GL_LINEAR;
133 }
134 else {
135 obj->Sampler.WrapS = GL_REPEAT;
136 obj->Sampler.WrapT = GL_REPEAT;
137 obj->Sampler.WrapR = GL_REPEAT;
138 obj->Sampler.MinFilter = GL_NEAREST_MIPMAP_LINEAR;
139 }
140 obj->Sampler.MagFilter = GL_LINEAR;
141 obj->Sampler.MinLod = -1000.0;
142 obj->Sampler.MaxLod = 1000.0;
143 obj->Sampler.LodBias = 0.0;
144 obj->Sampler.MaxAnisotropy = 1.0;
145 obj->Sampler.CompareMode = GL_NONE; /* ARB_shadow */
146 obj->Sampler.CompareFunc = GL_LEQUAL; /* ARB_shadow */
147 obj->Sampler.CompareFailValue = 0.0F; /* ARB_shadow_ambient */
148 obj->Sampler.DepthMode = GL_LUMINANCE; /* ARB_depth_texture */
149 obj->Sampler.CubeMapSeamless = GL_FALSE;
150 obj->Swizzle[0] = GL_RED;
151 obj->Swizzle[1] = GL_GREEN;
152 obj->Swizzle[2] = GL_BLUE;
153 obj->Swizzle[3] = GL_ALPHA;
154 obj->_Swizzle = SWIZZLE_NOOP;
155 obj->Sampler.sRGBDecode = GL_DECODE_EXT;
156 obj->BufferObjectFormat = GL_LUMINANCE8;
157 obj->_BufferObjectFormat = MESA_FORMAT_L8;
158 }
159
160
161 /**
162 * Some texture initialization can't be finished until we know which
163 * target it's getting bound to (GL_TEXTURE_1D/2D/etc).
164 */
165 static void
166 finish_texture_init(struct gl_context *ctx, GLenum target,
167 struct gl_texture_object *obj)
168 {
169 assert(obj->Target == 0);
170
171 if (target == GL_TEXTURE_RECTANGLE_NV ||
172 target == GL_TEXTURE_EXTERNAL_OES) {
173 /* have to init wrap and filter state here - kind of klunky */
174 obj->Sampler.WrapS = GL_CLAMP_TO_EDGE;
175 obj->Sampler.WrapT = GL_CLAMP_TO_EDGE;
176 obj->Sampler.WrapR = GL_CLAMP_TO_EDGE;
177 obj->Sampler.MinFilter = GL_LINEAR;
178 if (ctx->Driver.TexParameter) {
179 static const GLfloat fparam_wrap[1] = {(GLfloat) GL_CLAMP_TO_EDGE};
180 static const GLfloat fparam_filter[1] = {(GLfloat) GL_LINEAR};
181 ctx->Driver.TexParameter(ctx, target, obj, GL_TEXTURE_WRAP_S, fparam_wrap);
182 ctx->Driver.TexParameter(ctx, target, obj, GL_TEXTURE_WRAP_T, fparam_wrap);
183 ctx->Driver.TexParameter(ctx, target, obj, GL_TEXTURE_WRAP_R, fparam_wrap);
184 ctx->Driver.TexParameter(ctx, target, obj, GL_TEXTURE_MIN_FILTER, fparam_filter);
185 }
186 }
187 }
188
189
190 /**
191 * Deallocate a texture object struct. It should have already been
192 * removed from the texture object pool.
193 * Called via ctx->Driver.DeleteTexture() if not overriden by a driver.
194 *
195 * \param shared the shared GL state to which the object belongs.
196 * \param texObj the texture object to delete.
197 */
198 void
199 _mesa_delete_texture_object(struct gl_context *ctx,
200 struct gl_texture_object *texObj)
201 {
202 GLuint i, face;
203
204 /* Set Target to an invalid value. With some assertions elsewhere
205 * we can try to detect possible use of deleted textures.
206 */
207 texObj->Target = 0x99;
208
209 /* free the texture images */
210 for (face = 0; face < 6; face++) {
211 for (i = 0; i < MAX_TEXTURE_LEVELS; i++) {
212 if (texObj->Image[face][i]) {
213 ctx->Driver.DeleteTextureImage(ctx, texObj->Image[face][i]);
214 }
215 }
216 }
217
218 _mesa_reference_buffer_object(ctx, &texObj->BufferObject, NULL);
219
220 /* destroy the mutex -- it may have allocated memory (eg on bsd) */
221 _glthread_DESTROY_MUTEX(texObj->Mutex);
222
223 /* free this object */
224 free(texObj);
225 }
226
227
228
229 /**
230 * Copy texture object state from one texture object to another.
231 * Use for glPush/PopAttrib.
232 *
233 * \param dest destination texture object.
234 * \param src source texture object.
235 */
236 void
237 _mesa_copy_texture_object( struct gl_texture_object *dest,
238 const struct gl_texture_object *src )
239 {
240 dest->Target = src->Target;
241 dest->Name = src->Name;
242 dest->Priority = src->Priority;
243 dest->Sampler.BorderColor.f[0] = src->Sampler.BorderColor.f[0];
244 dest->Sampler.BorderColor.f[1] = src->Sampler.BorderColor.f[1];
245 dest->Sampler.BorderColor.f[2] = src->Sampler.BorderColor.f[2];
246 dest->Sampler.BorderColor.f[3] = src->Sampler.BorderColor.f[3];
247 dest->Sampler.WrapS = src->Sampler.WrapS;
248 dest->Sampler.WrapT = src->Sampler.WrapT;
249 dest->Sampler.WrapR = src->Sampler.WrapR;
250 dest->Sampler.MinFilter = src->Sampler.MinFilter;
251 dest->Sampler.MagFilter = src->Sampler.MagFilter;
252 dest->Sampler.MinLod = src->Sampler.MinLod;
253 dest->Sampler.MaxLod = src->Sampler.MaxLod;
254 dest->Sampler.LodBias = src->Sampler.LodBias;
255 dest->BaseLevel = src->BaseLevel;
256 dest->MaxLevel = src->MaxLevel;
257 dest->Sampler.MaxAnisotropy = src->Sampler.MaxAnisotropy;
258 dest->Sampler.CompareMode = src->Sampler.CompareMode;
259 dest->Sampler.CompareFunc = src->Sampler.CompareFunc;
260 dest->Sampler.CompareFailValue = src->Sampler.CompareFailValue;
261 dest->Sampler.CubeMapSeamless = src->Sampler.CubeMapSeamless;
262 dest->Sampler.DepthMode = src->Sampler.DepthMode;
263 dest->Sampler.sRGBDecode = src->Sampler.sRGBDecode;
264 dest->_MaxLevel = src->_MaxLevel;
265 dest->_MaxLambda = src->_MaxLambda;
266 dest->GenerateMipmap = src->GenerateMipmap;
267 dest->_BaseComplete = src->_BaseComplete;
268 dest->_MipmapComplete = src->_MipmapComplete;
269 COPY_4V(dest->Swizzle, src->Swizzle);
270 dest->_Swizzle = src->_Swizzle;
271
272 dest->RequiredTextureImageUnits = src->RequiredTextureImageUnits;
273 }
274
275
276 /**
277 * Free all texture images of the given texture object.
278 *
279 * \param ctx GL context.
280 * \param t texture object.
281 *
282 * \sa _mesa_clear_texture_image().
283 */
284 void
285 _mesa_clear_texture_object(struct gl_context *ctx,
286 struct gl_texture_object *texObj)
287 {
288 GLuint i, j;
289
290 if (texObj->Target == 0)
291 return;
292
293 for (i = 0; i < MAX_FACES; i++) {
294 for (j = 0; j < MAX_TEXTURE_LEVELS; j++) {
295 struct gl_texture_image *texImage = texObj->Image[i][j];
296 if (texImage)
297 _mesa_clear_texture_image(ctx, texImage);
298 }
299 }
300 }
301
302
303 /**
304 * Check if the given texture object is valid by examining its Target field.
305 * For debugging only.
306 */
307 static GLboolean
308 valid_texture_object(const struct gl_texture_object *tex)
309 {
310 switch (tex->Target) {
311 case 0:
312 case GL_TEXTURE_1D:
313 case GL_TEXTURE_2D:
314 case GL_TEXTURE_3D:
315 case GL_TEXTURE_CUBE_MAP_ARB:
316 case GL_TEXTURE_RECTANGLE_NV:
317 case GL_TEXTURE_1D_ARRAY_EXT:
318 case GL_TEXTURE_2D_ARRAY_EXT:
319 case GL_TEXTURE_BUFFER:
320 case GL_TEXTURE_EXTERNAL_OES:
321 return GL_TRUE;
322 case 0x99:
323 _mesa_problem(NULL, "invalid reference to a deleted texture object");
324 return GL_FALSE;
325 default:
326 _mesa_problem(NULL, "invalid texture object Target 0x%x, Id = %u",
327 tex->Target, tex->Name);
328 return GL_FALSE;
329 }
330 }
331
332
333 /**
334 * Reference (or unreference) a texture object.
335 * If '*ptr', decrement *ptr's refcount (and delete if it becomes zero).
336 * If 'tex' is non-null, increment its refcount.
337 * This is normally only called from the _mesa_reference_texobj() macro
338 * when there's a real pointer change.
339 */
340 void
341 _mesa_reference_texobj_(struct gl_texture_object **ptr,
342 struct gl_texture_object *tex)
343 {
344 assert(ptr);
345
346 if (*ptr) {
347 /* Unreference the old texture */
348 GLboolean deleteFlag = GL_FALSE;
349 struct gl_texture_object *oldTex = *ptr;
350
351 ASSERT(valid_texture_object(oldTex));
352 (void) valid_texture_object; /* silence warning in release builds */
353
354 _glthread_LOCK_MUTEX(oldTex->Mutex);
355 ASSERT(oldTex->RefCount > 0);
356 oldTex->RefCount--;
357
358 deleteFlag = (oldTex->RefCount == 0);
359 _glthread_UNLOCK_MUTEX(oldTex->Mutex);
360
361 if (deleteFlag) {
362 GET_CURRENT_CONTEXT(ctx);
363 if (ctx)
364 ctx->Driver.DeleteTexture(ctx, oldTex);
365 else
366 _mesa_problem(NULL, "Unable to delete texture, no context");
367 }
368
369 *ptr = NULL;
370 }
371 assert(!*ptr);
372
373 if (tex) {
374 /* reference new texture */
375 ASSERT(valid_texture_object(tex));
376 _glthread_LOCK_MUTEX(tex->Mutex);
377 if (tex->RefCount == 0) {
378 /* this texture's being deleted (look just above) */
379 /* Not sure this can every really happen. Warn if it does. */
380 _mesa_problem(NULL, "referencing deleted texture object");
381 *ptr = NULL;
382 }
383 else {
384 tex->RefCount++;
385 *ptr = tex;
386 }
387 _glthread_UNLOCK_MUTEX(tex->Mutex);
388 }
389 }
390
391
392 enum base_mipmap { BASE, MIPMAP };
393
394
395 /**
396 * Mark a texture object as incomplete. There are actually three kinds of
397 * (in)completeness:
398 * 1. "base incomplete": the base level of the texture is invalid so no
399 * texturing is possible.
400 * 2. "mipmap incomplete": a non-base level of the texture is invalid so
401 * mipmap filtering isn't possible, but non-mipmap filtering is.
402 * 3. "texture incompleteness": some combination of texture state and
403 * sampler state renders the texture incomplete.
404 *
405 * \param t texture object
406 * \param bm either BASE or MIPMAP to indicate what's incomplete
407 * \param fmt... string describing why it's incomplete (for debugging).
408 */
409 static void
410 incomplete(struct gl_texture_object *t, enum base_mipmap bm,
411 const char *fmt, ...)
412 {
413 #if 0
414 va_list args;
415 char s[100];
416
417 va_start(args, fmt);
418 vsnprintf(s, sizeof(s), fmt, args);
419 va_end(args);
420
421 printf("Texture Obj %d incomplete because: %s\n", t->Name, s);
422 #endif
423 if (bm == BASE)
424 t->_BaseComplete = GL_FALSE;
425 t->_MipmapComplete = GL_FALSE;
426 }
427
428
429 /**
430 * Examine a texture object to determine if it is complete.
431 *
432 * The gl_texture_object::Complete flag will be set to GL_TRUE or GL_FALSE
433 * accordingly.
434 *
435 * \param ctx GL context.
436 * \param t texture object.
437 *
438 * According to the texture target, verifies that each of the mipmaps is
439 * present and has the expected size.
440 */
441 void
442 _mesa_test_texobj_completeness( const struct gl_context *ctx,
443 struct gl_texture_object *t )
444 {
445 const GLint baseLevel = t->BaseLevel;
446 const struct gl_texture_image *baseImage;
447 GLint maxLog2 = 0, maxLevels = 0;
448
449 /* We'll set these to FALSE if tests fail below */
450 t->_BaseComplete = GL_TRUE;
451 t->_MipmapComplete = GL_TRUE;
452
453 if (t->Target == GL_TEXTURE_BUFFER) {
454 /* Buffer textures are always considered complete. The obvious case where
455 * they would be incomplete (no BO attached) is actually specced to be
456 * undefined rendering results.
457 */
458 return;
459 }
460
461 /* Detect cases where the application set the base level to an invalid
462 * value.
463 */
464 if ((baseLevel < 0) || (baseLevel >= MAX_TEXTURE_LEVELS)) {
465 incomplete(t, BASE, "base level = %d is invalid", baseLevel);
466 return;
467 }
468
469 if (t->MaxLevel < baseLevel) {
470 incomplete(t, BASE, "MAX_LEVEL (%d) < BASE_LEVEL (%d)",
471 t->MaxLevel, baseLevel);
472 return;
473 }
474
475 baseImage = t->Image[0][baseLevel];
476
477 /* Always need the base level image */
478 if (!baseImage) {
479 incomplete(t, BASE, "Image[baseLevel=%d] == NULL", baseLevel);
480 return;
481 }
482
483 /* Check width/height/depth for zero */
484 if (baseImage->Width == 0 ||
485 baseImage->Height == 0 ||
486 baseImage->Depth == 0) {
487 incomplete(t, BASE, "texture width or height or depth = 0");
488 return;
489 }
490
491 /* Check if the texture values are integer */
492 {
493 GLenum datatype = _mesa_get_format_datatype(baseImage->TexFormat);
494 t->_IsIntegerFormat = datatype == GL_INT || datatype == GL_UNSIGNED_INT;
495 }
496
497 /* Compute _MaxLevel (the maximum mipmap level we'll sample from given the
498 * mipmap image sizes and GL_TEXTURE_MAX_LEVEL state).
499 */
500 switch (t->Target) {
501 case GL_TEXTURE_1D:
502 case GL_TEXTURE_1D_ARRAY_EXT:
503 maxLog2 = baseImage->WidthLog2;
504 maxLevels = ctx->Const.MaxTextureLevels;
505 break;
506 case GL_TEXTURE_2D:
507 case GL_TEXTURE_2D_ARRAY_EXT:
508 maxLog2 = MAX2(baseImage->WidthLog2,
509 baseImage->HeightLog2);
510 maxLevels = ctx->Const.MaxTextureLevels;
511 break;
512 case GL_TEXTURE_3D:
513 maxLog2 = MAX3(baseImage->WidthLog2,
514 baseImage->HeightLog2,
515 baseImage->DepthLog2);
516 maxLevels = ctx->Const.Max3DTextureLevels;
517 break;
518 case GL_TEXTURE_CUBE_MAP_ARB:
519 maxLog2 = MAX2(baseImage->WidthLog2,
520 baseImage->HeightLog2);
521 maxLevels = ctx->Const.MaxCubeTextureLevels;
522 break;
523 case GL_TEXTURE_RECTANGLE_NV:
524 case GL_TEXTURE_BUFFER:
525 case GL_TEXTURE_EXTERNAL_OES:
526 maxLog2 = 0; /* not applicable */
527 maxLevels = 1; /* no mipmapping */
528 break;
529 default:
530 _mesa_problem(ctx, "Bad t->Target in _mesa_test_texobj_completeness");
531 return;
532 }
533
534 ASSERT(maxLevels > 0);
535
536 t->_MaxLevel = baseLevel + maxLog2; /* 'p' in the GL spec */
537 t->_MaxLevel = MIN2(t->_MaxLevel, t->MaxLevel);
538 t->_MaxLevel = MIN2(t->_MaxLevel, maxLevels - 1); /* 'q' in the GL spec */
539
540 /* Compute _MaxLambda = q - b (see the 1.2 spec) used during mipmapping */
541 t->_MaxLambda = (GLfloat) (t->_MaxLevel - baseLevel);
542
543 if (t->Immutable) {
544 /* This texture object was created with glTexStorage1/2/3D() so we
545 * know that all the mipmap levels are the right size and all cube
546 * map faces are the same size.
547 * We don't need to do any of the additional checks below.
548 */
549 return;
550 }
551
552 if (t->Target == GL_TEXTURE_CUBE_MAP_ARB) {
553 /* Make sure that all six cube map level 0 images are the same size.
554 * Note: we know that the image's width==height (we enforce that
555 * at glTexImage time) so we only need to test the width here.
556 */
557 GLuint face;
558 assert(baseImage->Width2 == baseImage->Height);
559 for (face = 1; face < 6; face++) {
560 assert(t->Image[face][baseLevel]->Width2 ==
561 t->Image[face][baseLevel]->Height2);
562 if (t->Image[face][baseLevel] == NULL ||
563 t->Image[face][baseLevel]->Width2 != baseImage->Width2) {
564 incomplete(t, BASE, "Cube face missing or mismatched size");
565 return;
566 }
567 }
568 }
569
570 /*
571 * Do mipmap consistency checking.
572 * Note: we don't care about the current texture sampler state here.
573 * To determine texture completeness we'll either look at _BaseComplete
574 * or _MipmapComplete depending on the current minification filter mode.
575 */
576 {
577 GLint i;
578 const GLint minLevel = baseLevel;
579 const GLint maxLevel = t->_MaxLevel;
580 const GLuint numFaces = t->Target == GL_TEXTURE_CUBE_MAP ? 6 : 1;
581 GLuint width, height, depth, face;
582
583 if (minLevel > maxLevel) {
584 incomplete(t, BASE, "minLevel > maxLevel");
585 return;
586 }
587
588 /* Get the base image's dimensions */
589 width = baseImage->Width2;
590 height = baseImage->Height2;
591 depth = baseImage->Depth2;
592
593 /* Note: this loop will be a no-op for RECT, BUFFER, EXTERNAL textures */
594 for (i = baseLevel + 1; i < maxLevels; i++) {
595 /* Compute the expected size of image at level[i] */
596 if (width > 1) {
597 width /= 2;
598 }
599 if (height > 1 && t->Target != GL_TEXTURE_1D_ARRAY) {
600 height /= 2;
601 }
602 if (depth > 1 && t->Target != GL_TEXTURE_2D_ARRAY) {
603 depth /= 2;
604 }
605
606 /* loop over cube faces (or single face otherwise) */
607 for (face = 0; face < numFaces; face++) {
608 if (i >= minLevel && i <= maxLevel) {
609 const struct gl_texture_image *img = t->Image[face][i];
610
611 if (!img) {
612 incomplete(t, MIPMAP, "TexImage[%d] is missing", i);
613 return;
614 }
615 if (img->TexFormat != baseImage->TexFormat) {
616 incomplete(t, MIPMAP, "Format[i] != Format[baseLevel]");
617 return;
618 }
619 if (img->Border != baseImage->Border) {
620 incomplete(t, MIPMAP, "Border[i] != Border[baseLevel]");
621 return;
622 }
623 if (img->Width2 != width) {
624 incomplete(t, MIPMAP, "TexImage[%d] bad width %u", i, img->Width2);
625 return;
626 }
627 if (img->Height2 != height) {
628 incomplete(t, MIPMAP, "TexImage[%d] bad height %u", i, img->Height2);
629 return;
630 }
631 if (img->Depth2 != depth) {
632 incomplete(t, MIPMAP, "TexImage[%d] bad depth %u", i, img->Depth2);
633 return;
634 }
635
636 /* Extra checks for cube textures */
637 if (face > 0) {
638 /* check that cube faces are the same size */
639 if (img->Width2 != t->Image[0][i]->Width2 ||
640 img->Height2 != t->Image[0][i]->Height2) {
641 incomplete(t, MIPMAP, "CubeMap Image[n][i] bad size");
642 return;
643 }
644 }
645 }
646 }
647
648 if (width == 1 && height == 1 && depth == 1) {
649 return; /* found smallest needed mipmap, all done! */
650 }
651 }
652 }
653 }
654
655
656 /**
657 * Check if the given cube map texture is "cube complete" as defined in
658 * the OpenGL specification.
659 */
660 GLboolean
661 _mesa_cube_complete(const struct gl_texture_object *texObj)
662 {
663 const GLint baseLevel = texObj->BaseLevel;
664 const struct gl_texture_image *img0, *img;
665 GLuint face;
666
667 if (texObj->Target != GL_TEXTURE_CUBE_MAP)
668 return GL_FALSE;
669
670 if ((baseLevel < 0) || (baseLevel >= MAX_TEXTURE_LEVELS))
671 return GL_FALSE;
672
673 /* check first face */
674 img0 = texObj->Image[0][baseLevel];
675 if (!img0 ||
676 img0->Width < 1 ||
677 img0->Width != img0->Height)
678 return GL_FALSE;
679
680 /* check remaining faces vs. first face */
681 for (face = 1; face < 6; face++) {
682 img = texObj->Image[face][baseLevel];
683 if (!img ||
684 img->Width != img0->Width ||
685 img->Height != img0->Height ||
686 img->TexFormat != img0->TexFormat)
687 return GL_FALSE;
688 }
689
690 return GL_TRUE;
691 }
692
693
694 /**
695 * Mark a texture object dirty. It forces the object to be incomplete
696 * and optionally forces the context to re-validate its state.
697 *
698 * \param ctx GL context.
699 * \param texObj texture object.
700 * \param invalidate_state also invalidate context state.
701 */
702 void
703 _mesa_dirty_texobj(struct gl_context *ctx, struct gl_texture_object *texObj,
704 GLboolean invalidate_state)
705 {
706 texObj->_BaseComplete = GL_FALSE;
707 texObj->_MipmapComplete = GL_FALSE;
708 if (invalidate_state)
709 ctx->NewState |= _NEW_TEXTURE;
710 }
711
712
713 /**
714 * Return pointer to a default/fallback texture of the given type/target.
715 * The texture is an RGBA texture with all texels = (0,0,0,1).
716 * That's the value a GLSL sampler should get when sampling from an
717 * incomplete texture.
718 */
719 struct gl_texture_object *
720 _mesa_get_fallback_texture(struct gl_context *ctx, gl_texture_index tex)
721 {
722 if (!ctx->Shared->FallbackTex[tex]) {
723 /* create fallback texture now */
724 const GLsizei width = 1, height = 1, depth = 1;
725 GLubyte texel[4];
726 struct gl_texture_object *texObj;
727 struct gl_texture_image *texImage;
728 gl_format texFormat;
729 GLuint dims, face, numFaces = 1;
730 GLenum target;
731
732 texel[0] =
733 texel[1] =
734 texel[2] = 0x0;
735 texel[3] = 0xff;
736
737 switch (tex) {
738 case TEXTURE_2D_ARRAY_INDEX:
739 dims = 3;
740 target = GL_TEXTURE_2D_ARRAY;
741 break;
742 case TEXTURE_1D_ARRAY_INDEX:
743 dims = 2;
744 target = GL_TEXTURE_1D_ARRAY;
745 break;
746 case TEXTURE_CUBE_INDEX:
747 dims = 2;
748 target = GL_TEXTURE_CUBE_MAP;
749 numFaces = 6;
750 break;
751 case TEXTURE_3D_INDEX:
752 dims = 3;
753 target = GL_TEXTURE_3D;
754 break;
755 case TEXTURE_RECT_INDEX:
756 dims = 2;
757 target = GL_TEXTURE_RECTANGLE;
758 break;
759 case TEXTURE_2D_INDEX:
760 dims = 2;
761 target = GL_TEXTURE_2D;
762 break;
763 case TEXTURE_1D_INDEX:
764 dims = 1;
765 target = GL_TEXTURE_1D;
766 break;
767 case TEXTURE_BUFFER_INDEX:
768 dims = 0;
769 target = GL_TEXTURE_BUFFER;
770 break;
771 case TEXTURE_EXTERNAL_INDEX:
772 default:
773 /* no-op */
774 return NULL;
775 }
776
777 /* create texture object */
778 texObj = ctx->Driver.NewTextureObject(ctx, 0, target);
779 if (!texObj)
780 return NULL;
781
782 assert(texObj->RefCount == 1);
783 texObj->Sampler.MinFilter = GL_NEAREST;
784 texObj->Sampler.MagFilter = GL_NEAREST;
785
786 texFormat = ctx->Driver.ChooseTextureFormat(ctx, GL_RGBA, GL_RGBA,
787 GL_UNSIGNED_BYTE);
788
789 /* need a loop here just for cube maps */
790 for (face = 0; face < numFaces; face++) {
791 GLenum faceTarget;
792
793 if (target == GL_TEXTURE_CUBE_MAP)
794 faceTarget = GL_TEXTURE_CUBE_MAP_POSITIVE_X + face;
795 else
796 faceTarget = target;
797
798 /* initialize level[0] texture image */
799 texImage = _mesa_get_tex_image(ctx, texObj, faceTarget, 0);
800
801 _mesa_init_teximage_fields(ctx, texImage,
802 width,
803 (dims > 1) ? height : 1,
804 (dims > 2) ? depth : 1,
805 0, /* border */
806 GL_RGBA, texFormat);
807
808 switch (dims) {
809 case 0:
810 break;
811 case 1:
812 ctx->Driver.TexImage1D(ctx, texImage, GL_RGBA,
813 width, 0,
814 GL_RGBA, GL_UNSIGNED_BYTE, texel,
815 &ctx->DefaultPacking);
816 break;
817 case 2:
818 ctx->Driver.TexImage2D(ctx, texImage, GL_RGBA,
819 width, height, 0,
820 GL_RGBA, GL_UNSIGNED_BYTE, texel,
821 &ctx->DefaultPacking);
822 break;
823 case 3:
824 ctx->Driver.TexImage3D(ctx, texImage, GL_RGBA,
825 width, height, depth, 0,
826 GL_RGBA, GL_UNSIGNED_BYTE, texel,
827 &ctx->DefaultPacking);
828 break;
829 default:
830 _mesa_problem(ctx, "bad dims in _mesa_get_fallback_texture()");
831 }
832 }
833
834 _mesa_test_texobj_completeness(ctx, texObj);
835 assert(texObj->_BaseComplete);
836 assert(texObj->_MipmapComplete);
837
838 ctx->Shared->FallbackTex[tex] = texObj;
839 }
840 return ctx->Shared->FallbackTex[tex];
841 }
842
843
844 /**
845 * Compute the size of the given texture object, in bytes.
846 */
847 static GLuint
848 texture_size(const struct gl_texture_object *texObj)
849 {
850 const GLuint numFaces = texObj->Target == GL_TEXTURE_CUBE_MAP ? 6 : 1;
851 GLuint face, level, size = 0;
852
853 for (face = 0; face < numFaces; face++) {
854 for (level = 0; level < MAX_TEXTURE_LEVELS; level++) {
855 const struct gl_texture_image *img = texObj->Image[face][level];
856 if (img) {
857 GLuint sz = _mesa_format_image_size(img->TexFormat, img->Width,
858 img->Height, img->Depth);
859 size += sz;
860 }
861 }
862 }
863
864 return size;
865 }
866
867
868 /**
869 * Callback called from _mesa_HashWalk()
870 */
871 static void
872 count_tex_size(GLuint key, void *data, void *userData)
873 {
874 const struct gl_texture_object *texObj =
875 (const struct gl_texture_object *) data;
876 GLuint *total = (GLuint *) userData;
877
878 *total = *total + texture_size(texObj);
879 }
880
881
882 /**
883 * Compute total size (in bytes) of all textures for the given context.
884 * For debugging purposes.
885 */
886 GLuint
887 _mesa_total_texture_memory(struct gl_context *ctx)
888 {
889 GLuint tgt, total = 0;
890
891 _mesa_HashWalk(ctx->Shared->TexObjects, count_tex_size, &total);
892
893 /* plus, the default texture objects */
894 for (tgt = 0; tgt < NUM_TEXTURE_TARGETS; tgt++) {
895 total += texture_size(ctx->Shared->DefaultTex[tgt]);
896 }
897
898 return total;
899 }
900
901
902
903 /*@}*/
904
905
906 /***********************************************************************/
907 /** \name API functions */
908 /*@{*/
909
910
911 /**
912 * Generate texture names.
913 *
914 * \param n number of texture names to be generated.
915 * \param textures an array in which will hold the generated texture names.
916 *
917 * \sa glGenTextures().
918 *
919 * Calls _mesa_HashFindFreeKeyBlock() to find a block of free texture
920 * IDs which are stored in \p textures. Corresponding empty texture
921 * objects are also generated.
922 */
923 void GLAPIENTRY
924 _mesa_GenTextures( GLsizei n, GLuint *textures )
925 {
926 GET_CURRENT_CONTEXT(ctx);
927 GLuint first;
928 GLint i;
929 ASSERT_OUTSIDE_BEGIN_END(ctx);
930
931 if (n < 0) {
932 _mesa_error( ctx, GL_INVALID_VALUE, "glGenTextures" );
933 return;
934 }
935
936 if (!textures)
937 return;
938
939 /*
940 * This must be atomic (generation and allocation of texture IDs)
941 */
942 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
943
944 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->TexObjects, n);
945
946 /* Allocate new, empty texture objects */
947 for (i = 0; i < n; i++) {
948 struct gl_texture_object *texObj;
949 GLuint name = first + i;
950 GLenum target = 0;
951 texObj = ctx->Driver.NewTextureObject(ctx, name, target);
952 if (!texObj) {
953 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
954 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGenTextures");
955 return;
956 }
957
958 /* insert into hash table */
959 _mesa_HashInsert(ctx->Shared->TexObjects, texObj->Name, texObj);
960
961 textures[i] = name;
962 }
963
964 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
965 }
966
967
968 /**
969 * Check if the given texture object is bound to the current draw or
970 * read framebuffer. If so, Unbind it.
971 */
972 static void
973 unbind_texobj_from_fbo(struct gl_context *ctx,
974 struct gl_texture_object *texObj)
975 {
976 const GLuint n = (ctx->DrawBuffer == ctx->ReadBuffer) ? 1 : 2;
977 GLuint i;
978
979 for (i = 0; i < n; i++) {
980 struct gl_framebuffer *fb = (i == 0) ? ctx->DrawBuffer : ctx->ReadBuffer;
981 if (_mesa_is_user_fbo(fb)) {
982 GLuint j;
983 for (j = 0; j < BUFFER_COUNT; j++) {
984 if (fb->Attachment[j].Type == GL_TEXTURE &&
985 fb->Attachment[j].Texture == texObj) {
986 /* Vertices are already flushed by _mesa_DeleteTextures */
987 ctx->NewState |= _NEW_BUFFERS;
988 _mesa_remove_attachment(ctx, fb->Attachment + j);
989 }
990 }
991 }
992 }
993 }
994
995
996 /**
997 * Check if the given texture object is bound to any texture image units and
998 * unbind it if so (revert to default textures).
999 */
1000 static void
1001 unbind_texobj_from_texunits(struct gl_context *ctx,
1002 struct gl_texture_object *texObj)
1003 {
1004 GLuint u, tex;
1005
1006 for (u = 0; u < Elements(ctx->Texture.Unit); u++) {
1007 struct gl_texture_unit *unit = &ctx->Texture.Unit[u];
1008 for (tex = 0; tex < NUM_TEXTURE_TARGETS; tex++) {
1009 if (texObj == unit->CurrentTex[tex]) {
1010 _mesa_reference_texobj(&unit->CurrentTex[tex],
1011 ctx->Shared->DefaultTex[tex]);
1012 ASSERT(unit->CurrentTex[tex]);
1013 break;
1014 }
1015 }
1016 }
1017 }
1018
1019
1020 /**
1021 * Delete named textures.
1022 *
1023 * \param n number of textures to be deleted.
1024 * \param textures array of texture IDs to be deleted.
1025 *
1026 * \sa glDeleteTextures().
1027 *
1028 * If we're about to delete a texture that's currently bound to any
1029 * texture unit, unbind the texture first. Decrement the reference
1030 * count on the texture object and delete it if it's zero.
1031 * Recall that texture objects can be shared among several rendering
1032 * contexts.
1033 */
1034 void GLAPIENTRY
1035 _mesa_DeleteTextures( GLsizei n, const GLuint *textures)
1036 {
1037 GET_CURRENT_CONTEXT(ctx);
1038 GLint i;
1039 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx); /* too complex */
1040
1041 if (!textures)
1042 return;
1043
1044 for (i = 0; i < n; i++) {
1045 if (textures[i] > 0) {
1046 struct gl_texture_object *delObj
1047 = _mesa_lookup_texture(ctx, textures[i]);
1048
1049 if (delObj) {
1050 _mesa_lock_texture(ctx, delObj);
1051
1052 /* Check if texture is bound to any framebuffer objects.
1053 * If so, unbind.
1054 * See section 4.4.2.3 of GL_EXT_framebuffer_object.
1055 */
1056 unbind_texobj_from_fbo(ctx, delObj);
1057
1058 /* Check if this texture is currently bound to any texture units.
1059 * If so, unbind it.
1060 */
1061 unbind_texobj_from_texunits(ctx, delObj);
1062
1063 _mesa_unlock_texture(ctx, delObj);
1064
1065 ctx->NewState |= _NEW_TEXTURE;
1066
1067 /* The texture _name_ is now free for re-use.
1068 * Remove it from the hash table now.
1069 */
1070 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
1071 _mesa_HashRemove(ctx->Shared->TexObjects, delObj->Name);
1072 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1073
1074 /* Unreference the texobj. If refcount hits zero, the texture
1075 * will be deleted.
1076 */
1077 _mesa_reference_texobj(&delObj, NULL);
1078 }
1079 }
1080 }
1081 }
1082
1083
1084 /**
1085 * Convert a GL texture target enum such as GL_TEXTURE_2D or GL_TEXTURE_3D
1086 * into the corresponding Mesa texture target index.
1087 * Note that proxy targets are not valid here.
1088 * \return TEXTURE_x_INDEX or -1 if target is invalid
1089 */
1090 static GLint
1091 target_enum_to_index(GLenum target)
1092 {
1093 switch (target) {
1094 case GL_TEXTURE_1D:
1095 return TEXTURE_1D_INDEX;
1096 case GL_TEXTURE_2D:
1097 return TEXTURE_2D_INDEX;
1098 case GL_TEXTURE_3D:
1099 return TEXTURE_3D_INDEX;
1100 case GL_TEXTURE_CUBE_MAP_ARB:
1101 return TEXTURE_CUBE_INDEX;
1102 case GL_TEXTURE_RECTANGLE_NV:
1103 return TEXTURE_RECT_INDEX;
1104 case GL_TEXTURE_1D_ARRAY_EXT:
1105 return TEXTURE_1D_ARRAY_INDEX;
1106 case GL_TEXTURE_2D_ARRAY_EXT:
1107 return TEXTURE_2D_ARRAY_INDEX;
1108 case GL_TEXTURE_BUFFER_ARB:
1109 return TEXTURE_BUFFER_INDEX;
1110 case GL_TEXTURE_EXTERNAL_OES:
1111 return TEXTURE_EXTERNAL_INDEX;
1112 default:
1113 return -1;
1114 }
1115 }
1116
1117
1118 /**
1119 * Bind a named texture to a texturing target.
1120 *
1121 * \param target texture target.
1122 * \param texName texture name.
1123 *
1124 * \sa glBindTexture().
1125 *
1126 * Determines the old texture object bound and returns immediately if rebinding
1127 * the same texture. Get the current texture which is either a default texture
1128 * if name is null, a named texture from the hash, or a new texture if the
1129 * given texture name is new. Increments its reference count, binds it, and
1130 * calls dd_function_table::BindTexture. Decrements the old texture reference
1131 * count and deletes it if it reaches zero.
1132 */
1133 void GLAPIENTRY
1134 _mesa_BindTexture( GLenum target, GLuint texName )
1135 {
1136 GET_CURRENT_CONTEXT(ctx);
1137 struct gl_texture_unit *texUnit = _mesa_get_current_tex_unit(ctx);
1138 struct gl_texture_object *newTexObj = NULL;
1139 GLint targetIndex;
1140 ASSERT_OUTSIDE_BEGIN_END(ctx);
1141
1142 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
1143 _mesa_debug(ctx, "glBindTexture %s %d\n",
1144 _mesa_lookup_enum_by_nr(target), (GLint) texName);
1145
1146 targetIndex = target_enum_to_index(target);
1147 if (targetIndex < 0) {
1148 _mesa_error(ctx, GL_INVALID_ENUM, "glBindTexture(target)");
1149 return;
1150 }
1151 assert(targetIndex < NUM_TEXTURE_TARGETS);
1152
1153 /*
1154 * Get pointer to new texture object (newTexObj)
1155 */
1156 if (texName == 0) {
1157 /* Use a default texture object */
1158 newTexObj = ctx->Shared->DefaultTex[targetIndex];
1159 }
1160 else {
1161 /* non-default texture object */
1162 newTexObj = _mesa_lookup_texture(ctx, texName);
1163 if (newTexObj) {
1164 /* error checking */
1165 if (newTexObj->Target != 0 && newTexObj->Target != target) {
1166 /* the named texture object's target doesn't match the given target */
1167 _mesa_error( ctx, GL_INVALID_OPERATION,
1168 "glBindTexture(target mismatch)" );
1169 return;
1170 }
1171 if (newTexObj->Target == 0) {
1172 finish_texture_init(ctx, target, newTexObj);
1173 }
1174 }
1175 else {
1176 /* if this is a new texture id, allocate a texture object now */
1177 newTexObj = ctx->Driver.NewTextureObject(ctx, texName, target);
1178 if (!newTexObj) {
1179 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindTexture");
1180 return;
1181 }
1182
1183 /* and insert it into hash table */
1184 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
1185 _mesa_HashInsert(ctx->Shared->TexObjects, texName, newTexObj);
1186 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1187 }
1188 newTexObj->Target = target;
1189 }
1190
1191 assert(valid_texture_object(newTexObj));
1192
1193 /* Check if this texture is only used by this context and is already bound.
1194 * If so, just return.
1195 */
1196 {
1197 GLboolean early_out;
1198 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
1199 early_out = ((ctx->Shared->RefCount == 1)
1200 && (newTexObj == texUnit->CurrentTex[targetIndex]));
1201 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1202 if (early_out) {
1203 return;
1204 }
1205 }
1206
1207 /* flush before changing binding */
1208 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
1209
1210 /* Do the actual binding. The refcount on the previously bound
1211 * texture object will be decremented. It'll be deleted if the
1212 * count hits zero.
1213 */
1214 _mesa_reference_texobj(&texUnit->CurrentTex[targetIndex], newTexObj);
1215 ASSERT(texUnit->CurrentTex[targetIndex]);
1216
1217 /* Pass BindTexture call to device driver */
1218 if (ctx->Driver.BindTexture)
1219 ctx->Driver.BindTexture(ctx, target, newTexObj);
1220 }
1221
1222
1223 /**
1224 * Set texture priorities.
1225 *
1226 * \param n number of textures.
1227 * \param texName texture names.
1228 * \param priorities corresponding texture priorities.
1229 *
1230 * \sa glPrioritizeTextures().
1231 *
1232 * Looks up each texture in the hash, clamps the corresponding priority between
1233 * 0.0 and 1.0, and calls dd_function_table::PrioritizeTexture.
1234 */
1235 void GLAPIENTRY
1236 _mesa_PrioritizeTextures( GLsizei n, const GLuint *texName,
1237 const GLclampf *priorities )
1238 {
1239 GET_CURRENT_CONTEXT(ctx);
1240 GLint i;
1241 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
1242
1243 if (n < 0) {
1244 _mesa_error( ctx, GL_INVALID_VALUE, "glPrioritizeTextures" );
1245 return;
1246 }
1247
1248 if (!priorities)
1249 return;
1250
1251 for (i = 0; i < n; i++) {
1252 if (texName[i] > 0) {
1253 struct gl_texture_object *t = _mesa_lookup_texture(ctx, texName[i]);
1254 if (t) {
1255 t->Priority = CLAMP( priorities[i], 0.0F, 1.0F );
1256 }
1257 }
1258 }
1259
1260 ctx->NewState |= _NEW_TEXTURE;
1261 }
1262
1263
1264
1265 /**
1266 * See if textures are loaded in texture memory.
1267 *
1268 * \param n number of textures to query.
1269 * \param texName array with the texture names.
1270 * \param residences array which will hold the residence status.
1271 *
1272 * \return GL_TRUE if all textures are resident and \p residences is left unchanged,
1273 *
1274 * Note: we assume all textures are always resident
1275 */
1276 GLboolean GLAPIENTRY
1277 _mesa_AreTexturesResident(GLsizei n, const GLuint *texName,
1278 GLboolean *residences)
1279 {
1280 GET_CURRENT_CONTEXT(ctx);
1281 GLboolean allResident = GL_TRUE;
1282 GLint i;
1283 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1284
1285 if (n < 0) {
1286 _mesa_error(ctx, GL_INVALID_VALUE, "glAreTexturesResident(n)");
1287 return GL_FALSE;
1288 }
1289
1290 if (!texName || !residences)
1291 return GL_FALSE;
1292
1293 /* We only do error checking on the texture names */
1294 for (i = 0; i < n; i++) {
1295 struct gl_texture_object *t;
1296 if (texName[i] == 0) {
1297 _mesa_error(ctx, GL_INVALID_VALUE, "glAreTexturesResident");
1298 return GL_FALSE;
1299 }
1300 t = _mesa_lookup_texture(ctx, texName[i]);
1301 if (!t) {
1302 _mesa_error(ctx, GL_INVALID_VALUE, "glAreTexturesResident");
1303 return GL_FALSE;
1304 }
1305 }
1306
1307 return allResident;
1308 }
1309
1310
1311 /**
1312 * See if a name corresponds to a texture.
1313 *
1314 * \param texture texture name.
1315 *
1316 * \return GL_TRUE if texture name corresponds to a texture, or GL_FALSE
1317 * otherwise.
1318 *
1319 * \sa glIsTexture().
1320 *
1321 * Calls _mesa_HashLookup().
1322 */
1323 GLboolean GLAPIENTRY
1324 _mesa_IsTexture( GLuint texture )
1325 {
1326 struct gl_texture_object *t;
1327 GET_CURRENT_CONTEXT(ctx);
1328 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1329
1330 if (!texture)
1331 return GL_FALSE;
1332
1333 t = _mesa_lookup_texture(ctx, texture);
1334
1335 /* IsTexture is true only after object has been bound once. */
1336 return t && t->Target;
1337 }
1338
1339
1340 /**
1341 * Simplest implementation of texture locking: grab the shared tex
1342 * mutex. Examine the shared context state timestamp and if there has
1343 * been a change, set the appropriate bits in ctx->NewState.
1344 *
1345 * This is used to deal with synchronizing things when a texture object
1346 * is used/modified by different contexts (or threads) which are sharing
1347 * the texture.
1348 *
1349 * See also _mesa_lock/unlock_texture() in teximage.h
1350 */
1351 void
1352 _mesa_lock_context_textures( struct gl_context *ctx )
1353 {
1354 _glthread_LOCK_MUTEX(ctx->Shared->TexMutex);
1355
1356 if (ctx->Shared->TextureStateStamp != ctx->TextureStateTimestamp) {
1357 ctx->NewState |= _NEW_TEXTURE;
1358 ctx->TextureStateTimestamp = ctx->Shared->TextureStateStamp;
1359 }
1360 }
1361
1362
1363 void
1364 _mesa_unlock_context_textures( struct gl_context *ctx )
1365 {
1366 assert(ctx->Shared->TextureStateStamp == ctx->TextureStateTimestamp);
1367 _glthread_UNLOCK_MUTEX(ctx->Shared->TexMutex);
1368 }
1369
1370 /*@}*/