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