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