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