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