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