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