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