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