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