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