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