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