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