teximage.c revision b9abf16e
1/*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2008  Brian Paul   All Rights Reserved.
5 * Copyright (C) 2009  VMware, Inc.  All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26
27/**
28 * \file teximage.c
29 * Texture image-related functions.
30 */
31
32#include <stdbool.h>
33#include "glheader.h"
34#include "bufferobj.h"
35#include "context.h"
36#include "enums.h"
37#include "fbobject.h"
38#include "framebuffer.h"
39#include "hash.h"
40#include "image.h"
41#include "imports.h"
42#include "macros.h"
43#include "mipmap.h"
44#include "multisample.h"
45#include "pixelstore.h"
46#include "state.h"
47#include "texcompress.h"
48#include "texcompress_cpal.h"
49#include "teximage.h"
50#include "texobj.h"
51#include "texstate.h"
52#include "texstorage.h"
53#include "textureview.h"
54#include "mtypes.h"
55#include "glformats.h"
56#include "texstore.h"
57#include "pbo.h"
58
59
60/**
61 * State changes which we care about for glCopyTex[Sub]Image() calls.
62 * In particular, we care about pixel transfer state and buffer state
63 * (such as glReadBuffer to make sure we read from the right renderbuffer).
64 */
65#define NEW_COPY_TEX_STATE (_NEW_BUFFERS | _NEW_PIXEL)
66
67/**
68 * Returns a corresponding internal floating point format for a given base
69 * format as specifed by OES_texture_float. In case of GL_FLOAT, the internal
70 * format needs to be a 32 bit component and in case of GL_HALF_FLOAT_OES it
71 * needs to be a 16 bit component.
72 *
73 * For example, given base format GL_RGBA, type GL_FLOAT return GL_RGBA32F_ARB.
74 */
75static GLenum
76adjust_for_oes_float_texture(const struct gl_context *ctx,
77                             GLenum format, GLenum type)
78{
79   switch (type) {
80   case GL_FLOAT:
81      if (ctx->Extensions.OES_texture_float) {
82         switch (format) {
83         case GL_RGBA:
84            return GL_RGBA32F;
85         case GL_RGB:
86            return GL_RGB32F;
87         case GL_ALPHA:
88            return GL_ALPHA32F_ARB;
89         case GL_LUMINANCE:
90            return GL_LUMINANCE32F_ARB;
91         case GL_LUMINANCE_ALPHA:
92            return GL_LUMINANCE_ALPHA32F_ARB;
93         default:
94            break;
95         }
96      }
97      break;
98
99   case GL_HALF_FLOAT_OES:
100      if (ctx->Extensions.OES_texture_half_float) {
101         switch (format) {
102         case GL_RGBA:
103            return GL_RGBA16F;
104         case GL_RGB:
105            return GL_RGB16F;
106         case GL_ALPHA:
107            return GL_ALPHA16F_ARB;
108         case GL_LUMINANCE:
109            return GL_LUMINANCE16F_ARB;
110         case GL_LUMINANCE_ALPHA:
111            return GL_LUMINANCE_ALPHA16F_ARB;
112         default:
113            break;
114         }
115      }
116      break;
117
118   default:
119      break;
120   }
121
122   return format;
123}
124
125/**
126 * Returns a corresponding base format for a given internal floating point
127 * format as specifed by OES_texture_float.
128 */
129static GLenum
130oes_float_internal_format(const struct gl_context *ctx,
131                          GLenum format, GLenum type)
132{
133   switch (type) {
134   case GL_FLOAT:
135      if (ctx->Extensions.OES_texture_float) {
136         switch (format) {
137         case GL_RGBA32F:
138            return GL_RGBA;
139         case GL_RGB32F:
140            return GL_RGB;
141         case GL_ALPHA32F_ARB:
142            return GL_ALPHA;
143         case GL_LUMINANCE32F_ARB:
144            return GL_LUMINANCE;
145         case GL_LUMINANCE_ALPHA32F_ARB:
146            return GL_LUMINANCE_ALPHA;
147         default:
148            break;
149         }
150      }
151      break;
152
153   case GL_HALF_FLOAT_OES:
154      if (ctx->Extensions.OES_texture_half_float) {
155         switch (format) {
156         case GL_RGBA16F:
157            return GL_RGBA;
158         case GL_RGB16F:
159            return GL_RGB;
160         case GL_ALPHA16F_ARB:
161            return GL_ALPHA;
162         case GL_LUMINANCE16F_ARB:
163            return GL_LUMINANCE;
164         case GL_LUMINANCE_ALPHA16F_ARB:
165            return GL_LUMINANCE_ALPHA;
166         default:
167            break;
168         }
169      }
170      break;
171   }
172   return format;
173}
174
175
176/**
177 * Install gl_texture_image in a gl_texture_object according to the target
178 * and level parameters.
179 *
180 * \param tObj texture object.
181 * \param target texture target.
182 * \param level image level.
183 * \param texImage texture image.
184 */
185static void
186set_tex_image(struct gl_texture_object *tObj,
187              GLenum target, GLint level,
188              struct gl_texture_image *texImage)
189{
190   const GLuint face = _mesa_tex_target_to_face(target);
191
192   assert(tObj);
193   assert(texImage);
194   if (target == GL_TEXTURE_RECTANGLE_NV || target == GL_TEXTURE_EXTERNAL_OES)
195      assert(level == 0);
196
197   tObj->Image[face][level] = texImage;
198
199   /* Set the 'back' pointer */
200   texImage->TexObject = tObj;
201   texImage->Level = level;
202   texImage->Face = face;
203}
204
205
206/**
207 * Free a gl_texture_image and associated data.
208 * This function is a fallback called via ctx->Driver.DeleteTextureImage().
209 *
210 * \param texImage texture image.
211 *
212 * Free the texture image structure and the associated image data.
213 */
214void
215_mesa_delete_texture_image(struct gl_context *ctx,
216                           struct gl_texture_image *texImage)
217{
218   /* Free texImage->Data and/or any other driver-specific texture
219    * image storage.
220    */
221   assert(ctx->Driver.FreeTextureImageBuffer);
222   ctx->Driver.FreeTextureImageBuffer( ctx, texImage );
223   free(texImage);
224}
225
226
227/**
228 * Test if a target is a proxy target.
229 *
230 * \param target texture target.
231 *
232 * \return GL_TRUE if the target is a proxy target, GL_FALSE otherwise.
233 */
234GLboolean
235_mesa_is_proxy_texture(GLenum target)
236{
237   unsigned i;
238   static const GLenum targets[] = {
239      GL_PROXY_TEXTURE_1D,
240      GL_PROXY_TEXTURE_2D,
241      GL_PROXY_TEXTURE_3D,
242      GL_PROXY_TEXTURE_CUBE_MAP,
243      GL_PROXY_TEXTURE_RECTANGLE,
244      GL_PROXY_TEXTURE_1D_ARRAY,
245      GL_PROXY_TEXTURE_2D_ARRAY,
246      GL_PROXY_TEXTURE_CUBE_MAP_ARRAY,
247      GL_PROXY_TEXTURE_2D_MULTISAMPLE,
248      GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
249   };
250   /*
251    * NUM_TEXTURE_TARGETS should match number of terms above, except there's no
252    * proxy for GL_TEXTURE_BUFFER and GL_TEXTURE_EXTERNAL_OES.
253    */
254   STATIC_ASSERT(NUM_TEXTURE_TARGETS == ARRAY_SIZE(targets) + 2);
255
256   for (i = 0; i < ARRAY_SIZE(targets); ++i)
257      if (target == targets[i])
258         return GL_TRUE;
259   return GL_FALSE;
260}
261
262
263/**
264 * Test if a target is an array target.
265 *
266 * \param target texture target.
267 *
268 * \return true if the target is an array target, false otherwise.
269 */
270bool
271_mesa_is_array_texture(GLenum target)
272{
273   switch (target) {
274   case GL_TEXTURE_1D_ARRAY:
275   case GL_TEXTURE_2D_ARRAY:
276   case GL_TEXTURE_CUBE_MAP_ARRAY:
277   case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
278      return true;
279   default:
280      return false;
281   };
282}
283
284/**
285 * Test if a target is a cube map.
286 *
287 * \param target texture target.
288 *
289 * \return true if the target is a cube map, false otherwise.
290 */
291bool
292_mesa_is_cube_map_texture(GLenum target)
293{
294   switch(target) {
295   case GL_TEXTURE_CUBE_MAP:
296   case GL_TEXTURE_CUBE_MAP_ARRAY:
297      return true;
298   default:
299      return false;
300   }
301}
302
303/**
304 * Return the proxy target which corresponds to the given texture target
305 */
306static GLenum
307proxy_target(GLenum target)
308{
309   switch (target) {
310   case GL_TEXTURE_1D:
311   case GL_PROXY_TEXTURE_1D:
312      return GL_PROXY_TEXTURE_1D;
313   case GL_TEXTURE_2D:
314   case GL_PROXY_TEXTURE_2D:
315      return GL_PROXY_TEXTURE_2D;
316   case GL_TEXTURE_3D:
317   case GL_PROXY_TEXTURE_3D:
318      return GL_PROXY_TEXTURE_3D;
319   case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
320   case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
321   case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
322   case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
323   case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
324   case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
325   case GL_TEXTURE_CUBE_MAP:
326   case GL_PROXY_TEXTURE_CUBE_MAP:
327      return GL_PROXY_TEXTURE_CUBE_MAP;
328   case GL_TEXTURE_RECTANGLE_NV:
329   case GL_PROXY_TEXTURE_RECTANGLE_NV:
330      return GL_PROXY_TEXTURE_RECTANGLE_NV;
331   case GL_TEXTURE_1D_ARRAY_EXT:
332   case GL_PROXY_TEXTURE_1D_ARRAY_EXT:
333      return GL_PROXY_TEXTURE_1D_ARRAY_EXT;
334   case GL_TEXTURE_2D_ARRAY_EXT:
335   case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
336      return GL_PROXY_TEXTURE_2D_ARRAY_EXT;
337   case GL_TEXTURE_CUBE_MAP_ARRAY:
338   case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
339      return GL_PROXY_TEXTURE_CUBE_MAP_ARRAY;
340   case GL_TEXTURE_2D_MULTISAMPLE:
341   case GL_PROXY_TEXTURE_2D_MULTISAMPLE:
342      return GL_PROXY_TEXTURE_2D_MULTISAMPLE;
343   case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
344   case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY:
345      return GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY;
346   default:
347      _mesa_problem(NULL, "unexpected target in proxy_target()");
348      return 0;
349   }
350}
351
352
353
354
355/**
356 * Get a texture image pointer from a texture object, given a texture
357 * target and mipmap level.  The target and level parameters should
358 * have already been error-checked.
359 *
360 * \param texObj texture unit.
361 * \param target texture target.
362 * \param level image level.
363 *
364 * \return pointer to the texture image structure, or NULL on failure.
365 */
366struct gl_texture_image *
367_mesa_select_tex_image(const struct gl_texture_object *texObj,
368		                 GLenum target, GLint level)
369{
370   const GLuint face = _mesa_tex_target_to_face(target);
371
372   assert(texObj);
373   assert(level >= 0);
374   assert(level < MAX_TEXTURE_LEVELS);
375
376   return texObj->Image[face][level];
377}
378
379
380/**
381 * Like _mesa_select_tex_image() but if the image doesn't exist, allocate
382 * it and install it.  Only return NULL if passed a bad parameter or run
383 * out of memory.
384 */
385struct gl_texture_image *
386_mesa_get_tex_image(struct gl_context *ctx, struct gl_texture_object *texObj,
387                    GLenum target, GLint level)
388{
389   struct gl_texture_image *texImage;
390
391   if (!texObj)
392      return NULL;
393
394   texImage = _mesa_select_tex_image(texObj, target, level);
395   if (!texImage) {
396      texImage = ctx->Driver.NewTextureImage(ctx);
397      if (!texImage) {
398         _mesa_error(ctx, GL_OUT_OF_MEMORY, "texture image allocation");
399         return NULL;
400      }
401
402      set_tex_image(texObj, target, level, texImage);
403   }
404
405   return texImage;
406}
407
408
409/**
410 * Return pointer to the specified proxy texture image.
411 * Note that proxy textures are per-context, not per-texture unit.
412 * \return pointer to texture image or NULL if invalid target, invalid
413 *         level, or out of memory.
414 */
415static struct gl_texture_image *
416get_proxy_tex_image(struct gl_context *ctx, GLenum target, GLint level)
417{
418   struct gl_texture_image *texImage;
419   GLuint texIndex;
420
421   if (level < 0)
422      return NULL;
423
424   switch (target) {
425   case GL_PROXY_TEXTURE_1D:
426      if (level >= ctx->Const.MaxTextureLevels)
427         return NULL;
428      texIndex = TEXTURE_1D_INDEX;
429      break;
430   case GL_PROXY_TEXTURE_2D:
431      if (level >= ctx->Const.MaxTextureLevels)
432         return NULL;
433      texIndex = TEXTURE_2D_INDEX;
434      break;
435   case GL_PROXY_TEXTURE_3D:
436      if (level >= ctx->Const.Max3DTextureLevels)
437         return NULL;
438      texIndex = TEXTURE_3D_INDEX;
439      break;
440   case GL_PROXY_TEXTURE_CUBE_MAP:
441      if (level >= ctx->Const.MaxCubeTextureLevels)
442         return NULL;
443      texIndex = TEXTURE_CUBE_INDEX;
444      break;
445   case GL_PROXY_TEXTURE_RECTANGLE_NV:
446      if (level > 0)
447         return NULL;
448      texIndex = TEXTURE_RECT_INDEX;
449      break;
450   case GL_PROXY_TEXTURE_1D_ARRAY_EXT:
451      if (level >= ctx->Const.MaxTextureLevels)
452         return NULL;
453      texIndex = TEXTURE_1D_ARRAY_INDEX;
454      break;
455   case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
456      if (level >= ctx->Const.MaxTextureLevels)
457         return NULL;
458      texIndex = TEXTURE_2D_ARRAY_INDEX;
459      break;
460   case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
461      if (level >= ctx->Const.MaxCubeTextureLevels)
462         return NULL;
463      texIndex = TEXTURE_CUBE_ARRAY_INDEX;
464      break;
465   case GL_PROXY_TEXTURE_2D_MULTISAMPLE:
466      if (level > 0)
467         return 0;
468      texIndex = TEXTURE_2D_MULTISAMPLE_INDEX;
469      break;
470   case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY:
471      if (level > 0)
472         return 0;
473      texIndex = TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX;
474      break;
475   default:
476      return NULL;
477   }
478
479   texImage = ctx->Texture.ProxyTex[texIndex]->Image[0][level];
480   if (!texImage) {
481      texImage = ctx->Driver.NewTextureImage(ctx);
482      if (!texImage) {
483         _mesa_error(ctx, GL_OUT_OF_MEMORY, "proxy texture allocation");
484         return NULL;
485      }
486      ctx->Texture.ProxyTex[texIndex]->Image[0][level] = texImage;
487      /* Set the 'back' pointer */
488      texImage->TexObject = ctx->Texture.ProxyTex[texIndex];
489   }
490   return texImage;
491}
492
493
494/**
495 * Get the maximum number of allowed mipmap levels.
496 *
497 * \param ctx GL context.
498 * \param target texture target.
499 *
500 * \return the maximum number of allowed mipmap levels for the given
501 * texture target, or zero if passed a bad target.
502 *
503 * \sa gl_constants.
504 */
505GLint
506_mesa_max_texture_levels(struct gl_context *ctx, GLenum target)
507{
508   switch (target) {
509   case GL_TEXTURE_1D:
510   case GL_PROXY_TEXTURE_1D:
511   case GL_TEXTURE_2D:
512   case GL_PROXY_TEXTURE_2D:
513      return ctx->Const.MaxTextureLevels;
514   case GL_TEXTURE_3D:
515   case GL_PROXY_TEXTURE_3D:
516      return ctx->Const.Max3DTextureLevels;
517   case GL_TEXTURE_CUBE_MAP:
518   case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
519   case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
520   case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
521   case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
522   case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
523   case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
524   case GL_PROXY_TEXTURE_CUBE_MAP:
525      return ctx->Extensions.ARB_texture_cube_map
526         ? ctx->Const.MaxCubeTextureLevels : 0;
527   case GL_TEXTURE_RECTANGLE_NV:
528   case GL_PROXY_TEXTURE_RECTANGLE_NV:
529      return ctx->Extensions.NV_texture_rectangle ? 1 : 0;
530   case GL_TEXTURE_1D_ARRAY_EXT:
531   case GL_PROXY_TEXTURE_1D_ARRAY_EXT:
532   case GL_TEXTURE_2D_ARRAY_EXT:
533   case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
534      return ctx->Extensions.EXT_texture_array
535         ? ctx->Const.MaxTextureLevels : 0;
536   case GL_TEXTURE_CUBE_MAP_ARRAY:
537   case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
538      return _mesa_has_texture_cube_map_array(ctx)
539         ? ctx->Const.MaxCubeTextureLevels : 0;
540   case GL_TEXTURE_BUFFER:
541      return (_mesa_has_ARB_texture_buffer_object(ctx) ||
542              _mesa_has_OES_texture_buffer(ctx)) ? 1 : 0;
543   case GL_TEXTURE_2D_MULTISAMPLE:
544   case GL_PROXY_TEXTURE_2D_MULTISAMPLE:
545   case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
546   case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY:
547      return (_mesa_is_desktop_gl(ctx) || _mesa_is_gles31(ctx))
548         && ctx->Extensions.ARB_texture_multisample
549         ? 1 : 0;
550   case GL_TEXTURE_EXTERNAL_OES:
551      /* fall-through */
552   default:
553      return 0; /* bad target */
554   }
555}
556
557
558/**
559 * Return number of dimensions per mipmap level for the given texture target.
560 */
561GLint
562_mesa_get_texture_dimensions(GLenum target)
563{
564   switch (target) {
565   case GL_TEXTURE_1D:
566   case GL_PROXY_TEXTURE_1D:
567      return 1;
568   case GL_TEXTURE_2D:
569   case GL_TEXTURE_RECTANGLE:
570   case GL_TEXTURE_CUBE_MAP:
571   case GL_PROXY_TEXTURE_2D:
572   case GL_PROXY_TEXTURE_RECTANGLE:
573   case GL_PROXY_TEXTURE_CUBE_MAP:
574   case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
575   case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
576   case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
577   case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
578   case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
579   case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
580   case GL_TEXTURE_1D_ARRAY:
581   case GL_PROXY_TEXTURE_1D_ARRAY:
582   case GL_TEXTURE_EXTERNAL_OES:
583   case GL_TEXTURE_2D_MULTISAMPLE:
584   case GL_PROXY_TEXTURE_2D_MULTISAMPLE:
585      return 2;
586   case GL_TEXTURE_3D:
587   case GL_PROXY_TEXTURE_3D:
588   case GL_TEXTURE_2D_ARRAY:
589   case GL_PROXY_TEXTURE_2D_ARRAY:
590   case GL_TEXTURE_CUBE_MAP_ARRAY:
591   case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
592   case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
593   case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY:
594      return 3;
595   case GL_TEXTURE_BUFFER:
596      /* fall-through */
597   default:
598      _mesa_problem(NULL, "invalid target 0x%x in get_texture_dimensions()",
599                    target);
600      return 2;
601   }
602}
603
604
605/**
606 * Check if a texture target can have more than one layer.
607 */
608GLboolean
609_mesa_tex_target_is_layered(GLenum target)
610{
611   switch (target) {
612   case GL_TEXTURE_1D:
613   case GL_PROXY_TEXTURE_1D:
614   case GL_TEXTURE_2D:
615   case GL_PROXY_TEXTURE_2D:
616   case GL_TEXTURE_RECTANGLE:
617   case GL_PROXY_TEXTURE_RECTANGLE:
618   case GL_TEXTURE_2D_MULTISAMPLE:
619   case GL_PROXY_TEXTURE_2D_MULTISAMPLE:
620   case GL_TEXTURE_BUFFER:
621   case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
622   case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
623   case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
624   case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
625   case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
626   case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
627   case GL_TEXTURE_EXTERNAL_OES:
628      return GL_FALSE;
629
630   case GL_TEXTURE_3D:
631   case GL_PROXY_TEXTURE_3D:
632   case GL_TEXTURE_CUBE_MAP:
633   case GL_PROXY_TEXTURE_CUBE_MAP:
634   case GL_TEXTURE_1D_ARRAY:
635   case GL_PROXY_TEXTURE_1D_ARRAY:
636   case GL_TEXTURE_2D_ARRAY:
637   case GL_PROXY_TEXTURE_2D_ARRAY:
638   case GL_TEXTURE_CUBE_MAP_ARRAY:
639   case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
640   case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
641   case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY:
642      return GL_TRUE;
643
644   default:
645      assert(!"Invalid texture target.");
646      return GL_FALSE;
647   }
648}
649
650
651/**
652 * Return the number of layers present in the given level of an array,
653 * cubemap or 3D texture.  If the texture is not layered return zero.
654 */
655GLuint
656_mesa_get_texture_layers(const struct gl_texture_object *texObj, GLint level)
657{
658   assert(level >= 0 && level < MAX_TEXTURE_LEVELS);
659
660   switch (texObj->Target) {
661   case GL_TEXTURE_1D:
662   case GL_TEXTURE_2D:
663   case GL_TEXTURE_RECTANGLE:
664   case GL_TEXTURE_2D_MULTISAMPLE:
665   case GL_TEXTURE_BUFFER:
666   case GL_TEXTURE_EXTERNAL_OES:
667      return 0;
668
669   case GL_TEXTURE_CUBE_MAP:
670      return 6;
671
672   case GL_TEXTURE_1D_ARRAY: {
673      struct gl_texture_image *img = texObj->Image[0][level];
674      return img ? img->Height : 0;
675   }
676
677   case GL_TEXTURE_3D:
678   case GL_TEXTURE_2D_ARRAY:
679   case GL_TEXTURE_CUBE_MAP_ARRAY:
680   case GL_TEXTURE_2D_MULTISAMPLE_ARRAY: {
681      struct gl_texture_image *img = texObj->Image[0][level];
682      return img ? img->Depth : 0;
683   }
684
685   default:
686      assert(!"Invalid texture target.");
687      return 0;
688   }
689}
690
691
692/**
693 * Return the maximum number of mipmap levels for the given target
694 * and the dimensions.
695 * The dimensions are expected not to include the border.
696 */
697GLsizei
698_mesa_get_tex_max_num_levels(GLenum target, GLsizei width, GLsizei height,
699                             GLsizei depth)
700{
701   GLsizei size;
702
703   switch (target) {
704   case GL_TEXTURE_1D:
705   case GL_TEXTURE_1D_ARRAY:
706   case GL_PROXY_TEXTURE_1D:
707   case GL_PROXY_TEXTURE_1D_ARRAY:
708      size = width;
709      break;
710   case GL_TEXTURE_CUBE_MAP:
711   case GL_TEXTURE_CUBE_MAP_ARRAY:
712   case GL_PROXY_TEXTURE_CUBE_MAP:
713   case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
714      size = width;
715      break;
716   case GL_TEXTURE_2D:
717   case GL_TEXTURE_2D_ARRAY:
718   case GL_PROXY_TEXTURE_2D:
719   case GL_PROXY_TEXTURE_2D_ARRAY:
720      size = MAX2(width, height);
721      break;
722   case GL_TEXTURE_3D:
723   case GL_PROXY_TEXTURE_3D:
724      size = MAX3(width, height, depth);
725      break;
726   case GL_TEXTURE_RECTANGLE:
727   case GL_TEXTURE_EXTERNAL_OES:
728   case GL_TEXTURE_2D_MULTISAMPLE:
729   case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
730   case GL_PROXY_TEXTURE_RECTANGLE:
731   case GL_PROXY_TEXTURE_2D_MULTISAMPLE:
732   case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY:
733      return 1;
734   default:
735      assert(0);
736      return 1;
737   }
738
739   return _mesa_logbase2(size) + 1;
740}
741
742
743#if 000 /* not used anymore */
744/*
745 * glTexImage[123]D can accept a NULL image pointer.  In this case we
746 * create a texture image with unspecified image contents per the OpenGL
747 * spec.
748 */
749static GLubyte *
750make_null_texture(GLint width, GLint height, GLint depth, GLenum format)
751{
752   const GLint components = _mesa_components_in_format(format);
753   const GLint numPixels = width * height * depth;
754   GLubyte *data = (GLubyte *) malloc(numPixels * components * sizeof(GLubyte));
755
756#ifdef DEBUG
757   /*
758    * Let's see if anyone finds this.  If glTexImage2D() is called with
759    * a NULL image pointer then load the texture image with something
760    * interesting instead of leaving it indeterminate.
761    */
762   if (data) {
763      static const char message[8][32] = {
764         "   X   X  XXXXX   XXX     X    ",
765         "   XX XX  X      X   X   X X   ",
766         "   X X X  X      X      X   X  ",
767         "   X   X  XXXX    XXX   XXXXX  ",
768         "   X   X  X          X  X   X  ",
769         "   X   X  X      X   X  X   X  ",
770         "   X   X  XXXXX   XXX   X   X  ",
771         "                               "
772      };
773
774      GLubyte *imgPtr = data;
775      GLint h, i, j, k;
776      for (h = 0; h < depth; h++) {
777         for (i = 0; i < height; i++) {
778            GLint srcRow = 7 - (i % 8);
779            for (j = 0; j < width; j++) {
780               GLint srcCol = j % 32;
781               GLubyte texel = (message[srcRow][srcCol]=='X') ? 255 : 70;
782               for (k = 0; k < components; k++) {
783                  *imgPtr++ = texel;
784               }
785            }
786         }
787      }
788   }
789#endif
790
791   return data;
792}
793#endif
794
795
796
797/**
798 * Set the size and format-related fields of a gl_texture_image struct
799 * to zero.  This is used when a proxy texture test fails.
800 */
801static void
802clear_teximage_fields(struct gl_texture_image *img)
803{
804   assert(img);
805   img->_BaseFormat = 0;
806   img->InternalFormat = 0;
807   img->Border = 0;
808   img->Width = 0;
809   img->Height = 0;
810   img->Depth = 0;
811   img->Width2 = 0;
812   img->Height2 = 0;
813   img->Depth2 = 0;
814   img->WidthLog2 = 0;
815   img->HeightLog2 = 0;
816   img->DepthLog2 = 0;
817   img->TexFormat = MESA_FORMAT_NONE;
818   img->NumSamples = 0;
819   img->FixedSampleLocations = GL_TRUE;
820}
821
822
823/**
824 * Initialize basic fields of the gl_texture_image struct.
825 *
826 * \param ctx GL context.
827 * \param img texture image structure to be initialized.
828 * \param width image width.
829 * \param height image height.
830 * \param depth image depth.
831 * \param border image border.
832 * \param internalFormat internal format.
833 * \param format  the actual hardware format (one of MESA_FORMAT_*)
834 * \param numSamples  number of samples per texel, or zero for non-MS.
835 * \param fixedSampleLocations  are sample locations fixed?
836 *
837 * Fills in the fields of \p img with the given information.
838 * Note: width, height and depth include the border.
839 */
840void
841_mesa_init_teximage_fields_ms(struct gl_context *ctx,
842                        struct gl_texture_image *img,
843                        GLsizei width, GLsizei height, GLsizei depth,
844                        GLint border, GLenum internalFormat,
845                        mesa_format format,
846                        GLuint numSamples, GLboolean fixedSampleLocations)
847{
848   const GLint base_format =_mesa_base_tex_format(ctx, internalFormat);
849   GLenum target;
850   assert(img);
851   assert(width >= 0);
852   assert(height >= 0);
853   assert(depth >= 0);
854
855   target = img->TexObject->Target;
856   assert(base_format != -1);
857   img->_BaseFormat = (GLenum16)base_format;
858   img->InternalFormat = internalFormat;
859   img->Border = border;
860   img->Width = width;
861   img->Height = height;
862   img->Depth = depth;
863
864   img->Width2 = width - 2 * border;   /* == 1 << img->WidthLog2; */
865   img->WidthLog2 = _mesa_logbase2(img->Width2);
866
867   switch(target) {
868   case GL_TEXTURE_1D:
869   case GL_TEXTURE_BUFFER:
870   case GL_PROXY_TEXTURE_1D:
871      if (height == 0)
872         img->Height2 = 0;
873      else
874         img->Height2 = 1;
875      img->HeightLog2 = 0;
876      if (depth == 0)
877         img->Depth2 = 0;
878      else
879         img->Depth2 = 1;
880      img->DepthLog2 = 0;
881      break;
882   case GL_TEXTURE_1D_ARRAY:
883   case GL_PROXY_TEXTURE_1D_ARRAY:
884      img->Height2 = height; /* no border */
885      img->HeightLog2 = 0; /* not used */
886      if (depth == 0)
887         img->Depth2 = 0;
888      else
889         img->Depth2 = 1;
890      img->DepthLog2 = 0;
891      break;
892   case GL_TEXTURE_2D:
893   case GL_TEXTURE_RECTANGLE:
894   case GL_TEXTURE_CUBE_MAP:
895   case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
896   case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
897   case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
898   case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
899   case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
900   case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
901   case GL_TEXTURE_EXTERNAL_OES:
902   case GL_PROXY_TEXTURE_2D:
903   case GL_PROXY_TEXTURE_RECTANGLE:
904   case GL_PROXY_TEXTURE_CUBE_MAP:
905   case GL_TEXTURE_2D_MULTISAMPLE:
906   case GL_PROXY_TEXTURE_2D_MULTISAMPLE:
907      img->Height2 = height - 2 * border; /* == 1 << img->HeightLog2; */
908      img->HeightLog2 = _mesa_logbase2(img->Height2);
909      if (depth == 0)
910         img->Depth2 = 0;
911      else
912         img->Depth2 = 1;
913      img->DepthLog2 = 0;
914      break;
915   case GL_TEXTURE_2D_ARRAY:
916   case GL_PROXY_TEXTURE_2D_ARRAY:
917   case GL_TEXTURE_CUBE_MAP_ARRAY:
918   case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
919   case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
920   case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY:
921      img->Height2 = height - 2 * border; /* == 1 << img->HeightLog2; */
922      img->HeightLog2 = _mesa_logbase2(img->Height2);
923      img->Depth2 = depth; /* no border */
924      img->DepthLog2 = 0; /* not used */
925      break;
926   case GL_TEXTURE_3D:
927   case GL_PROXY_TEXTURE_3D:
928      img->Height2 = height - 2 * border; /* == 1 << img->HeightLog2; */
929      img->HeightLog2 = _mesa_logbase2(img->Height2);
930      img->Depth2 = depth - 2 * border;   /* == 1 << img->DepthLog2; */
931      img->DepthLog2 = _mesa_logbase2(img->Depth2);
932      break;
933   default:
934      _mesa_problem(NULL, "invalid target 0x%x in _mesa_init_teximage_fields()",
935                    target);
936   }
937
938   img->MaxNumLevels =
939      _mesa_get_tex_max_num_levels(target,
940                                   img->Width2, img->Height2, img->Depth2);
941   img->TexFormat = format;
942   img->NumSamples = numSamples;
943   img->FixedSampleLocations = fixedSampleLocations;
944}
945
946
947void
948_mesa_init_teximage_fields(struct gl_context *ctx,
949                           struct gl_texture_image *img,
950                           GLsizei width, GLsizei height, GLsizei depth,
951                           GLint border, GLenum internalFormat,
952                           mesa_format format)
953{
954   _mesa_init_teximage_fields_ms(ctx, img, width, height, depth, border,
955                                 internalFormat, format, 0, GL_TRUE);
956}
957
958
959/**
960 * Free and clear fields of the gl_texture_image struct.
961 *
962 * \param ctx GL context.
963 * \param texImage texture image structure to be cleared.
964 *
965 * After the call, \p texImage will have no data associated with it.  Its
966 * fields are cleared so that its parent object will test incomplete.
967 */
968void
969_mesa_clear_texture_image(struct gl_context *ctx,
970                          struct gl_texture_image *texImage)
971{
972   ctx->Driver.FreeTextureImageBuffer(ctx, texImage);
973   clear_teximage_fields(texImage);
974}
975
976
977/**
978 * Check the width, height, depth and border of a texture image are legal.
979 * Used by all the glTexImage, glCompressedTexImage and glCopyTexImage
980 * functions.
981 * The target and level parameters will have already been validated.
982 * \return GL_TRUE if size is OK, GL_FALSE otherwise.
983 */
984GLboolean
985_mesa_legal_texture_dimensions(struct gl_context *ctx, GLenum target,
986                               GLint level, GLint width, GLint height,
987                               GLint depth, GLint border)
988{
989   GLint maxSize;
990
991   switch (target) {
992   case GL_TEXTURE_1D:
993   case GL_PROXY_TEXTURE_1D:
994      maxSize = 1 << (ctx->Const.MaxTextureLevels - 1); /* level zero size */
995      maxSize >>= level;  /* level size */
996      if (width < 2 * border || width > 2 * border + maxSize)
997         return GL_FALSE;
998      if (!ctx->Extensions.ARB_texture_non_power_of_two) {
999         if (width > 0 && !_mesa_is_pow_two(width - 2 * border))
1000            return GL_FALSE;
1001      }
1002      return GL_TRUE;
1003
1004   case GL_TEXTURE_2D:
1005   case GL_PROXY_TEXTURE_2D:
1006   case GL_TEXTURE_2D_MULTISAMPLE:
1007   case GL_PROXY_TEXTURE_2D_MULTISAMPLE:
1008      maxSize = 1 << (ctx->Const.MaxTextureLevels - 1);
1009      maxSize >>= level;
1010      if (width < 2 * border || width > 2 * border + maxSize)
1011         return GL_FALSE;
1012      if (height < 2 * border || height > 2 * border + maxSize)
1013         return GL_FALSE;
1014      if (!ctx->Extensions.ARB_texture_non_power_of_two) {
1015         if (width > 0 && !_mesa_is_pow_two(width - 2 * border))
1016            return GL_FALSE;
1017         if (height > 0 && !_mesa_is_pow_two(height - 2 * border))
1018            return GL_FALSE;
1019      }
1020      return GL_TRUE;
1021
1022   case GL_TEXTURE_3D:
1023   case GL_PROXY_TEXTURE_3D:
1024      maxSize = 1 << (ctx->Const.Max3DTextureLevels - 1);
1025      maxSize >>= level;
1026      if (width < 2 * border || width > 2 * border + maxSize)
1027         return GL_FALSE;
1028      if (height < 2 * border || height > 2 * border + maxSize)
1029         return GL_FALSE;
1030      if (depth < 2 * border || depth > 2 * border + maxSize)
1031         return GL_FALSE;
1032      if (!ctx->Extensions.ARB_texture_non_power_of_two) {
1033         if (width > 0 && !_mesa_is_pow_two(width - 2 * border))
1034            return GL_FALSE;
1035         if (height > 0 && !_mesa_is_pow_two(height - 2 * border))
1036            return GL_FALSE;
1037         if (depth > 0 && !_mesa_is_pow_two(depth - 2 * border))
1038            return GL_FALSE;
1039      }
1040      return GL_TRUE;
1041
1042   case GL_TEXTURE_RECTANGLE_NV:
1043   case GL_PROXY_TEXTURE_RECTANGLE_NV:
1044      if (level != 0)
1045         return GL_FALSE;
1046      maxSize = ctx->Const.MaxTextureRectSize;
1047      if (width < 0 || width > maxSize)
1048         return GL_FALSE;
1049      if (height < 0 || height > maxSize)
1050         return GL_FALSE;
1051      return GL_TRUE;
1052
1053   case GL_TEXTURE_CUBE_MAP:
1054   case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
1055   case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
1056   case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
1057   case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
1058   case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
1059   case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
1060   case GL_PROXY_TEXTURE_CUBE_MAP:
1061      maxSize = 1 << (ctx->Const.MaxCubeTextureLevels - 1);
1062      maxSize >>= level;
1063      if (width != height)
1064         return GL_FALSE;
1065      if (width < 2 * border || width > 2 * border + maxSize)
1066         return GL_FALSE;
1067      if (height < 2 * border || height > 2 * border + maxSize)
1068         return GL_FALSE;
1069      if (!ctx->Extensions.ARB_texture_non_power_of_two) {
1070         if (width > 0 && !_mesa_is_pow_two(width - 2 * border))
1071            return GL_FALSE;
1072         if (height > 0 && !_mesa_is_pow_two(height - 2 * border))
1073            return GL_FALSE;
1074      }
1075      return GL_TRUE;
1076
1077   case GL_TEXTURE_1D_ARRAY_EXT:
1078   case GL_PROXY_TEXTURE_1D_ARRAY_EXT:
1079      maxSize = 1 << (ctx->Const.MaxTextureLevels - 1);
1080      maxSize >>= level;
1081      if (width < 2 * border || width > 2 * border + maxSize)
1082         return GL_FALSE;
1083      if (height < 0 || height > ctx->Const.MaxArrayTextureLayers)
1084         return GL_FALSE;
1085      if (!ctx->Extensions.ARB_texture_non_power_of_two) {
1086         if (width > 0 && !_mesa_is_pow_two(width - 2 * border))
1087            return GL_FALSE;
1088      }
1089      return GL_TRUE;
1090
1091   case GL_TEXTURE_2D_ARRAY_EXT:
1092   case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
1093   case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
1094   case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY:
1095      maxSize = 1 << (ctx->Const.MaxTextureLevels - 1);
1096      maxSize >>= level;
1097      if (width < 2 * border || width > 2 * border + maxSize)
1098         return GL_FALSE;
1099      if (height < 2 * border || height > 2 * border + maxSize)
1100         return GL_FALSE;
1101      if (depth < 0 || depth > ctx->Const.MaxArrayTextureLayers)
1102         return GL_FALSE;
1103      if (!ctx->Extensions.ARB_texture_non_power_of_two) {
1104         if (width > 0 && !_mesa_is_pow_two(width - 2 * border))
1105            return GL_FALSE;
1106         if (height > 0 && !_mesa_is_pow_two(height - 2 * border))
1107            return GL_FALSE;
1108      }
1109      return GL_TRUE;
1110
1111   case GL_TEXTURE_CUBE_MAP_ARRAY:
1112   case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
1113      maxSize = 1 << (ctx->Const.MaxCubeTextureLevels - 1);
1114      if (width < 2 * border || width > 2 * border + maxSize)
1115         return GL_FALSE;
1116      if (height < 2 * border || height > 2 * border + maxSize)
1117         return GL_FALSE;
1118      if (depth < 0 || depth > ctx->Const.MaxArrayTextureLayers || depth % 6)
1119         return GL_FALSE;
1120      if (width != height)
1121         return GL_FALSE;
1122      if (level >= ctx->Const.MaxCubeTextureLevels)
1123         return GL_FALSE;
1124      if (!ctx->Extensions.ARB_texture_non_power_of_two) {
1125         if (width > 0 && !_mesa_is_pow_two(width - 2 * border))
1126            return GL_FALSE;
1127         if (height > 0 && !_mesa_is_pow_two(height - 2 * border))
1128            return GL_FALSE;
1129      }
1130      return GL_TRUE;
1131   default:
1132      _mesa_problem(ctx, "Invalid target in _mesa_legal_texture_dimensions()");
1133      return GL_FALSE;
1134   }
1135}
1136
1137static bool
1138error_check_subtexture_negative_dimensions(struct gl_context *ctx,
1139                                           GLuint dims,
1140                                           GLsizei subWidth,
1141                                           GLsizei subHeight,
1142                                           GLsizei subDepth,
1143                                           const char *func)
1144{
1145   /* Check size */
1146   if (subWidth < 0) {
1147      _mesa_error(ctx, GL_INVALID_VALUE, "%s(width=%d)", func, subWidth);
1148      return true;
1149   }
1150
1151   if (dims > 1 && subHeight < 0) {
1152      _mesa_error(ctx, GL_INVALID_VALUE, "%s(height=%d)", func, subHeight);
1153      return true;
1154   }
1155
1156   if (dims > 2 && subDepth < 0) {
1157      _mesa_error(ctx, GL_INVALID_VALUE, "%s(depth=%d)", func, subDepth);
1158      return true;
1159   }
1160
1161   return false;
1162}
1163
1164/**
1165 * Do error checking of xoffset, yoffset, zoffset, width, height and depth
1166 * for glTexSubImage, glCopyTexSubImage and glCompressedTexSubImage.
1167 * \param destImage  the destination texture image.
1168 * \return GL_TRUE if error found, GL_FALSE otherwise.
1169 */
1170static GLboolean
1171error_check_subtexture_dimensions(struct gl_context *ctx, GLuint dims,
1172                                  const struct gl_texture_image *destImage,
1173                                  GLint xoffset, GLint yoffset, GLint zoffset,
1174                                  GLsizei subWidth, GLsizei subHeight,
1175                                  GLsizei subDepth, const char *func)
1176{
1177   const GLenum target = destImage->TexObject->Target;
1178   GLuint bw, bh, bd;
1179
1180   /* check xoffset and width */
1181   if (xoffset < - (GLint) destImage->Border) {
1182      _mesa_error(ctx, GL_INVALID_VALUE, "%s(xoffset)", func);
1183      return GL_TRUE;
1184   }
1185
1186   if (xoffset + subWidth > (GLint) destImage->Width) {
1187      _mesa_error(ctx, GL_INVALID_VALUE, "%s(xoffset %d + width %d > %u)", func,
1188                  xoffset, subWidth, destImage->Width);
1189      return GL_TRUE;
1190   }
1191
1192   /* check yoffset and height */
1193   if (dims > 1) {
1194      GLint yBorder = (target == GL_TEXTURE_1D_ARRAY) ? 0 : destImage->Border;
1195      if (yoffset < -yBorder) {
1196         _mesa_error(ctx, GL_INVALID_VALUE, "%s(yoffset)", func);
1197         return GL_TRUE;
1198      }
1199      if (yoffset + subHeight > (GLint) destImage->Height) {
1200         _mesa_error(ctx, GL_INVALID_VALUE, "%s(yoffset %d + height %d > %u)",
1201                     func, yoffset, subHeight, destImage->Height);
1202         return GL_TRUE;
1203      }
1204   }
1205
1206   /* check zoffset and depth */
1207   if (dims > 2) {
1208      GLint depth;
1209      GLint zBorder = (target == GL_TEXTURE_2D_ARRAY ||
1210                       target == GL_TEXTURE_CUBE_MAP_ARRAY) ?
1211                         0 : destImage->Border;
1212
1213      if (zoffset < -zBorder) {
1214         _mesa_error(ctx, GL_INVALID_VALUE, "%s(zoffset)", func);
1215         return GL_TRUE;
1216      }
1217
1218      depth = (GLint) destImage->Depth;
1219      if (target == GL_TEXTURE_CUBE_MAP)
1220         depth = 6;
1221      if (zoffset + subDepth  > depth) {
1222         _mesa_error(ctx, GL_INVALID_VALUE, "%s(zoffset %d + depth %d > %u)",
1223                     func, zoffset, subDepth, depth);
1224         return GL_TRUE;
1225      }
1226   }
1227
1228   /*
1229    * The OpenGL spec (and GL_ARB_texture_compression) says only whole
1230    * compressed texture images can be updated.  But, that restriction may be
1231    * relaxed for particular compressed formats.  At this time, all the
1232    * compressed formats supported by Mesa allow sub-textures to be updated
1233    * along compressed block boundaries.
1234    */
1235   _mesa_get_format_block_size_3d(destImage->TexFormat, &bw, &bh, &bd);
1236
1237   if (bw != 1 || bh != 1 || bd != 1) {
1238      /* offset must be multiple of block size */
1239      if ((xoffset % bw != 0) || (yoffset % bh != 0) || (zoffset % bd != 0)) {
1240         _mesa_error(ctx, GL_INVALID_OPERATION,
1241                     "%s(xoffset = %d, yoffset = %d, zoffset = %d)",
1242                     func, xoffset, yoffset, zoffset);
1243         return GL_TRUE;
1244      }
1245
1246      /* The size must be a multiple of bw x bh, or we must be using a
1247       * offset+size that exactly hits the edge of the image.  This
1248       * is important for small mipmap levels (1x1, 2x1, etc) and for
1249       * NPOT textures.
1250       */
1251      if ((subWidth % bw != 0) &&
1252          (xoffset + subWidth != (GLint) destImage->Width)) {
1253         _mesa_error(ctx, GL_INVALID_OPERATION,
1254                     "%s(width = %d)", func, subWidth);
1255         return GL_TRUE;
1256      }
1257
1258      if ((subHeight % bh != 0) &&
1259          (yoffset + subHeight != (GLint) destImage->Height)) {
1260         _mesa_error(ctx, GL_INVALID_OPERATION,
1261                     "%s(height = %d)", func, subHeight);
1262         return GL_TRUE;
1263      }
1264
1265      if ((subDepth % bd != 0) &&
1266          (zoffset + subDepth != (GLint) destImage->Depth)) {
1267         _mesa_error(ctx, GL_INVALID_OPERATION,
1268                     "%s(depth = %d)", func, subDepth);
1269         return GL_TRUE;
1270      }
1271   }
1272
1273   return GL_FALSE;
1274}
1275
1276
1277
1278
1279/**
1280 * This is the fallback for Driver.TestProxyTexImage() for doing device-
1281 * specific texture image size checks.
1282 *
1283 * A hardware driver might override this function if, for example, the
1284 * max 3D texture size is 512x512x64 (i.e. not a cube).
1285 *
1286 * Note that width, height, depth == 0 is not an error.  However, a
1287 * texture with zero width/height/depth will be considered "incomplete"
1288 * and texturing will effectively be disabled.
1289 *
1290 * \param target  any texture target/type
1291 * \param numLevels  number of mipmap levels in the texture or 0 if not known
1292 * \param level  as passed to glTexImage
1293 * \param format  the MESA_FORMAT_x for the tex image
1294 * \param numSamples  number of samples per texel
1295 * \param width  as passed to glTexImage
1296 * \param height  as passed to glTexImage
1297 * \param depth  as passed to glTexImage
1298 * \return GL_TRUE if the image is acceptable, GL_FALSE if not acceptable.
1299 */
1300GLboolean
1301_mesa_test_proxy_teximage(struct gl_context *ctx, GLenum target,
1302                          GLuint numLevels, MAYBE_UNUSED GLint level,
1303                          mesa_format format, GLuint numSamples,
1304                          GLint width, GLint height, GLint depth)
1305{
1306   uint64_t bytes, mbytes;
1307
1308   if (numLevels > 0) {
1309      /* Compute total memory for a whole mipmap.  This is the path
1310       * taken for glTexStorage(GL_PROXY_TEXTURE_x).
1311       */
1312      unsigned l;
1313
1314      assert(level == 0);
1315
1316      bytes = 0;
1317
1318      for (l = 0; l < numLevels; l++) {
1319         GLint nextWidth, nextHeight, nextDepth;
1320
1321         bytes += _mesa_format_image_size64(format, width, height, depth);
1322
1323         if (_mesa_next_mipmap_level_size(target, 0, width, height, depth,
1324                                          &nextWidth, &nextHeight,
1325                                          &nextDepth)) {
1326            width = nextWidth;
1327            height = nextHeight;
1328            depth = nextDepth;
1329         } else {
1330            break;
1331         }
1332      }
1333   } else {
1334      /* We just compute the size of one mipmap level.  This is the path
1335       * taken for glTexImage(GL_PROXY_TEXTURE_x).
1336       */
1337      bytes = _mesa_format_image_size64(format, width, height, depth);
1338   }
1339
1340   bytes *= _mesa_num_tex_faces(target);
1341   bytes *= MAX2(1, numSamples);
1342
1343   mbytes = bytes / (1024 * 1024); /* convert to MB */
1344
1345   /* We just check if the image size is less than MaxTextureMbytes.
1346    * Some drivers may do more specific checks.
1347    */
1348   return mbytes <= (uint64_t) ctx->Const.MaxTextureMbytes;
1349}
1350
1351
1352/**
1353 * Return true if the format is only valid for glCompressedTexImage.
1354 */
1355static bool
1356compressedteximage_only_format(GLenum format)
1357{
1358   switch (format) {
1359   case GL_PALETTE4_RGB8_OES:
1360   case GL_PALETTE4_RGBA8_OES:
1361   case GL_PALETTE4_R5_G6_B5_OES:
1362   case GL_PALETTE4_RGBA4_OES:
1363   case GL_PALETTE4_RGB5_A1_OES:
1364   case GL_PALETTE8_RGB8_OES:
1365   case GL_PALETTE8_RGBA8_OES:
1366   case GL_PALETTE8_R5_G6_B5_OES:
1367   case GL_PALETTE8_RGBA4_OES:
1368   case GL_PALETTE8_RGB5_A1_OES:
1369   case GL_ATC_RGB_AMD:
1370   case GL_ATC_RGBA_EXPLICIT_ALPHA_AMD:
1371   case GL_ATC_RGBA_INTERPOLATED_ALPHA_AMD:
1372      return true;
1373   default:
1374      return false;
1375   }
1376}
1377
1378/**
1379 * Return true if the format doesn't support online compression.
1380 */
1381bool
1382_mesa_format_no_online_compression(GLenum format)
1383{
1384   return _mesa_is_astc_format(format) ||
1385          _mesa_is_etc2_format(format) ||
1386          compressedteximage_only_format(format);
1387}
1388
1389/* Writes to an GL error pointer if non-null and returns whether or not the
1390 * error is GL_NO_ERROR */
1391static bool
1392write_error(GLenum *err_ptr, GLenum error)
1393{
1394   if (err_ptr)
1395      *err_ptr = error;
1396
1397   return error == GL_NO_ERROR;
1398}
1399
1400/**
1401 * Helper function to determine whether a target and specific compression
1402 * format are supported. The error parameter returns GL_NO_ERROR if the
1403 * target can be compressed. Otherwise it returns either GL_INVALID_OPERATION
1404 * or GL_INVALID_ENUM, whichever is more appropriate.
1405 */
1406GLboolean
1407_mesa_target_can_be_compressed(const struct gl_context *ctx, GLenum target,
1408                               GLenum intFormat, GLenum *error)
1409{
1410   GLboolean target_can_be_compresed = GL_FALSE;
1411   mesa_format format = _mesa_glenum_to_compressed_format(intFormat);
1412   enum mesa_format_layout layout = _mesa_get_format_layout(format);
1413
1414   switch (target) {
1415   case GL_TEXTURE_2D:
1416   case GL_PROXY_TEXTURE_2D:
1417      target_can_be_compresed = GL_TRUE; /* true for any compressed format so far */
1418      break;
1419   case GL_PROXY_TEXTURE_CUBE_MAP:
1420   case GL_TEXTURE_CUBE_MAP:
1421   case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
1422   case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
1423   case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
1424   case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
1425   case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
1426   case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
1427      target_can_be_compresed = ctx->Extensions.ARB_texture_cube_map;
1428      break;
1429   case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
1430   case GL_TEXTURE_2D_ARRAY_EXT:
1431      target_can_be_compresed = ctx->Extensions.EXT_texture_array;
1432      break;
1433   case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
1434   case GL_TEXTURE_CUBE_MAP_ARRAY:
1435      /* From the KHR_texture_compression_astc_hdr spec:
1436       *
1437       *     Add a second new column "3D Tex." which is empty for all non-ASTC
1438       *     formats. If only the LDR profile is supported by the
1439       *     implementation, this column is also empty for all ASTC formats. If
1440       *     both the LDR and HDR profiles are supported only, this column is
1441       *     checked for all ASTC formats.
1442       *
1443       *     Add a third new column "Cube Map Array Tex." which is empty for all
1444       *     non-ASTC formats, and checked for all ASTC formats.
1445       *
1446       * and,
1447       *
1448       *     'An INVALID_OPERATION error is generated by CompressedTexImage3D
1449       *      if <internalformat> is TEXTURE_CUBE_MAP_ARRAY and the
1450       *      "Cube Map Array" column of table 8.19 is *not* checked, or if
1451       *      <internalformat> is TEXTURE_3D and the "3D Tex." column of table
1452       *      8.19 is *not* checked'
1453       *
1454       * The instances of <internalformat> above should say <target>.
1455       *
1456       * ETC2/EAC formats are the only alternative in GLES and thus such errors
1457       * have already been handled by normal ETC2/EAC behavior.
1458       */
1459
1460      /* From section 3.8.6, page 146 of OpenGL ES 3.0 spec:
1461       *
1462       *    "The ETC2/EAC texture compression algorithm supports only
1463       *     two-dimensional images. If internalformat is an ETC2/EAC format,
1464       *     glCompressedTexImage3D will generate an INVALID_OPERATION error if
1465       *     target is not TEXTURE_2D_ARRAY."
1466       *
1467       * This should also be applicable for glTexStorage3D(). Other available
1468       * targets for these functions are: TEXTURE_3D and TEXTURE_CUBE_MAP_ARRAY.
1469       *
1470       * Section 8.7, page 179 of OpenGL ES 3.2 adds:
1471       *
1472       *      An INVALID_OPERATION error is generated by CompressedTexImage3D
1473       *      if internalformat is one of the the formats in table 8.17 and target is
1474       *      not TEXTURE_2D_ARRAY, TEXTURE_CUBE_MAP_ARRAY or TEXTURE_3D.
1475       *
1476       *      An INVALID_OPERATION error is generated by CompressedTexImage3D
1477       *      if internalformat is TEXTURE_CUBE_MAP_ARRAY and the “Cube Map
1478       *      Array” column of table 8.17 is not checked, or if internalformat
1479       *      is TEXTURE_- 3D and the “3D Tex.” column of table 8.17 is not
1480       *      checked.
1481       *
1482       * The instances of <internalformat> above should say <target>.
1483       *
1484       * Such table 8.17 has checked "Cube Map Array" column for all the
1485       * cases. So in practice, TEXTURE_CUBE_MAP_ARRAY is now valid for OpenGL ES 3.2
1486       */
1487      if (layout == MESA_FORMAT_LAYOUT_ETC2 && _mesa_is_gles3(ctx) &&
1488          !_mesa_is_gles32(ctx))
1489            return write_error(error, GL_INVALID_OPERATION);
1490      target_can_be_compresed = _mesa_has_texture_cube_map_array(ctx);
1491      break;
1492   case GL_TEXTURE_3D:
1493      switch (layout) {
1494      case MESA_FORMAT_LAYOUT_ETC2:
1495         /* See ETC2/EAC comment in case GL_TEXTURE_CUBE_MAP_ARRAY. */
1496         if (_mesa_is_gles3(ctx))
1497            return write_error(error, GL_INVALID_OPERATION);
1498         break;
1499      case MESA_FORMAT_LAYOUT_BPTC:
1500         target_can_be_compresed = ctx->Extensions.ARB_texture_compression_bptc;
1501         break;
1502      case MESA_FORMAT_LAYOUT_ASTC:
1503         target_can_be_compresed =
1504            ctx->Extensions.KHR_texture_compression_astc_hdr ||
1505            ctx->Extensions.KHR_texture_compression_astc_sliced_3d;
1506
1507         /* Throw an INVALID_OPERATION error if the target is TEXTURE_3D and
1508          * neither of the above extensions are supported. See comment in
1509          * switch case GL_TEXTURE_CUBE_MAP_ARRAY for more info.
1510          */
1511         if (!target_can_be_compresed)
1512            return write_error(error, GL_INVALID_OPERATION);
1513         break;
1514      default:
1515         break;
1516      }
1517   default:
1518      break;
1519   }
1520   return write_error(error,
1521                      target_can_be_compresed ? GL_NO_ERROR : GL_INVALID_ENUM);
1522}
1523
1524
1525/**
1526 * Check if the given texture target value is legal for a
1527 * glTexImage1/2/3D call.
1528 */
1529static GLboolean
1530legal_teximage_target(struct gl_context *ctx, GLuint dims, GLenum target)
1531{
1532   switch (dims) {
1533   case 1:
1534      switch (target) {
1535      case GL_TEXTURE_1D:
1536      case GL_PROXY_TEXTURE_1D:
1537         return _mesa_is_desktop_gl(ctx);
1538      default:
1539         return GL_FALSE;
1540      }
1541   case 2:
1542      switch (target) {
1543      case GL_TEXTURE_2D:
1544         return GL_TRUE;
1545      case GL_PROXY_TEXTURE_2D:
1546         return _mesa_is_desktop_gl(ctx);
1547      case GL_PROXY_TEXTURE_CUBE_MAP:
1548         return _mesa_is_desktop_gl(ctx)
1549            && ctx->Extensions.ARB_texture_cube_map;
1550      case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
1551      case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
1552      case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
1553      case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
1554      case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
1555      case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
1556         return ctx->Extensions.ARB_texture_cube_map;
1557      case GL_TEXTURE_RECTANGLE_NV:
1558      case GL_PROXY_TEXTURE_RECTANGLE_NV:
1559         return _mesa_is_desktop_gl(ctx)
1560            && ctx->Extensions.NV_texture_rectangle;
1561      case GL_TEXTURE_1D_ARRAY_EXT:
1562      case GL_PROXY_TEXTURE_1D_ARRAY_EXT:
1563         return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_array;
1564      default:
1565         return GL_FALSE;
1566      }
1567   case 3:
1568      switch (target) {
1569      case GL_TEXTURE_3D:
1570         return GL_TRUE;
1571      case GL_PROXY_TEXTURE_3D:
1572         return _mesa_is_desktop_gl(ctx);
1573      case GL_TEXTURE_2D_ARRAY_EXT:
1574         return (_mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_array)
1575            || _mesa_is_gles3(ctx);
1576      case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
1577         return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_array;
1578      case GL_TEXTURE_CUBE_MAP_ARRAY:
1579      case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
1580         return _mesa_has_texture_cube_map_array(ctx);
1581      default:
1582         return GL_FALSE;
1583      }
1584   default:
1585      _mesa_problem(ctx, "invalid dims=%u in legal_teximage_target()", dims);
1586      return GL_FALSE;
1587   }
1588}
1589
1590
1591/**
1592 * Check if the given texture target value is legal for a
1593 * glTexSubImage, glCopyTexSubImage or glCopyTexImage call.
1594 * The difference compared to legal_teximage_target() above is that
1595 * proxy targets are not supported.
1596 */
1597static GLboolean
1598legal_texsubimage_target(struct gl_context *ctx, GLuint dims, GLenum target,
1599                         bool dsa)
1600{
1601   switch (dims) {
1602   case 1:
1603      return _mesa_is_desktop_gl(ctx) && target == GL_TEXTURE_1D;
1604   case 2:
1605      switch (target) {
1606      case GL_TEXTURE_2D:
1607         return GL_TRUE;
1608      case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
1609      case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
1610      case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
1611      case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
1612      case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
1613      case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
1614         return ctx->Extensions.ARB_texture_cube_map;
1615      case GL_TEXTURE_RECTANGLE_NV:
1616         return _mesa_is_desktop_gl(ctx)
1617            && ctx->Extensions.NV_texture_rectangle;
1618      case GL_TEXTURE_1D_ARRAY_EXT:
1619         return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_array;
1620      default:
1621         return GL_FALSE;
1622      }
1623   case 3:
1624      switch (target) {
1625      case GL_TEXTURE_3D:
1626         return GL_TRUE;
1627      case GL_TEXTURE_2D_ARRAY_EXT:
1628         return (_mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_array)
1629            || _mesa_is_gles3(ctx);
1630      case GL_TEXTURE_CUBE_MAP_ARRAY:
1631      case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
1632         return _mesa_has_texture_cube_map_array(ctx);
1633
1634      /* Table 8.15 of the OpenGL 4.5 core profile spec
1635       * (20141030) says that TEXTURE_CUBE_MAP is valid for TextureSubImage3D
1636       * and CopyTextureSubImage3D.
1637       */
1638      case GL_TEXTURE_CUBE_MAP:
1639         return dsa;
1640      default:
1641         return GL_FALSE;
1642      }
1643   default:
1644      _mesa_problem(ctx, "invalid dims=%u in legal_texsubimage_target()",
1645                    dims);
1646      return GL_FALSE;
1647   }
1648}
1649
1650
1651/**
1652 * Helper function to determine if a texture object is mutable (in terms
1653 * of GL_ARB_texture_storage/GL_ARB_bindless_texture).
1654 */
1655static GLboolean
1656mutable_tex_object(struct gl_context *ctx, GLenum target)
1657{
1658   struct gl_texture_object *texObj = _mesa_get_current_tex_object(ctx, target);
1659   if (!texObj)
1660      return GL_FALSE;
1661
1662   if (texObj->HandleAllocated) {
1663      /* The ARB_bindless_texture spec says:
1664       *
1665       * "The error INVALID_OPERATION is generated by TexImage*, CopyTexImage*,
1666       *  CompressedTexImage*, TexBuffer*, TexParameter*, as well as other
1667       *  functions defined in terms of these, if the texture object to be
1668       *  modified is referenced by one or more texture or image handles."
1669       */
1670      return GL_FALSE;
1671   }
1672
1673   return !texObj->Immutable;
1674}
1675
1676
1677/**
1678 * Return expected size of a compressed texture.
1679 */
1680static GLuint
1681compressed_tex_size(GLsizei width, GLsizei height, GLsizei depth,
1682                    GLenum glformat)
1683{
1684   mesa_format mesaFormat = _mesa_glenum_to_compressed_format(glformat);
1685   return _mesa_format_image_size(mesaFormat, width, height, depth);
1686}
1687
1688/**
1689 * Verify that a texture format is valid with a particular target
1690 *
1691 * In particular, textures with base format of \c GL_DEPTH_COMPONENT or
1692 * \c GL_DEPTH_STENCIL are only valid with certain, context dependent texture
1693 * targets.
1694 *
1695 * \param ctx             GL context
1696 * \param target          Texture target
1697 * \param internalFormat  Internal format of the texture image
1698 *
1699 * \returns true if the combination is legal, false otherwise.
1700 */
1701bool
1702_mesa_legal_texture_base_format_for_target(struct gl_context *ctx,
1703                                           GLenum target, GLenum internalFormat)
1704{
1705   if (_mesa_base_tex_format(ctx, internalFormat) == GL_DEPTH_COMPONENT
1706       || _mesa_base_tex_format(ctx, internalFormat) == GL_DEPTH_STENCIL
1707       || _mesa_base_tex_format(ctx, internalFormat) == GL_STENCIL_INDEX) {
1708      /* Section 3.8.3 (Texture Image Specification) of the OpenGL 3.3 Core
1709       * Profile spec says:
1710       *
1711       *     "Textures with a base internal format of DEPTH_COMPONENT or
1712       *     DEPTH_STENCIL are supported by texture image specification
1713       *     commands only if target is TEXTURE_1D, TEXTURE_2D,
1714       *     TEXTURE_1D_ARRAY, TEXTURE_2D_ARRAY, TEXTURE_RECTANGLE,
1715       *     TEXTURE_CUBE_MAP, PROXY_TEXTURE_1D, PROXY_TEXTURE_2D,
1716       *     PROXY_TEXTURE_1D_ARRAY, PROXY_TEXTURE_2D_ARRAY,
1717       *     PROXY_TEXTURE_RECTANGLE, or PROXY_TEXTURE_CUBE_MAP. Using these
1718       *     formats in conjunction with any other target will result in an
1719       *     INVALID_OPERATION error."
1720       *
1721       * Cubemaps are only supported with desktop OpenGL version >= 3.0,
1722       * EXT_gpu_shader4, or, on OpenGL ES 2.0+, OES_depth_texture_cube_map.
1723       */
1724      if (target != GL_TEXTURE_1D &&
1725          target != GL_PROXY_TEXTURE_1D &&
1726          target != GL_TEXTURE_2D &&
1727          target != GL_PROXY_TEXTURE_2D &&
1728          target != GL_TEXTURE_1D_ARRAY &&
1729          target != GL_PROXY_TEXTURE_1D_ARRAY &&
1730          target != GL_TEXTURE_2D_ARRAY &&
1731          target != GL_PROXY_TEXTURE_2D_ARRAY &&
1732          target != GL_TEXTURE_RECTANGLE_ARB &&
1733          target != GL_PROXY_TEXTURE_RECTANGLE_ARB &&
1734         !((_mesa_is_cube_face(target) ||
1735            target == GL_TEXTURE_CUBE_MAP ||
1736            target == GL_PROXY_TEXTURE_CUBE_MAP) &&
1737           (ctx->Version >= 30 || ctx->Extensions.EXT_gpu_shader4
1738            || (ctx->API == API_OPENGLES2 && ctx->Extensions.OES_depth_texture_cube_map))) &&
1739          !((target == GL_TEXTURE_CUBE_MAP_ARRAY ||
1740             target == GL_PROXY_TEXTURE_CUBE_MAP_ARRAY) &&
1741            _mesa_has_texture_cube_map_array(ctx))) {
1742         return false;
1743      }
1744   }
1745
1746   return true;
1747}
1748
1749static bool
1750texture_formats_agree(GLenum internalFormat,
1751                      GLenum format)
1752{
1753   GLboolean colorFormat;
1754   GLboolean is_format_depth_or_depthstencil;
1755   GLboolean is_internalFormat_depth_or_depthstencil;
1756
1757   /* Even though there are no color-index textures, we still have to support
1758    * uploading color-index data and remapping it to RGB via the
1759    * GL_PIXEL_MAP_I_TO_[RGBA] tables.
1760    */
1761   const GLboolean indexFormat = (format == GL_COLOR_INDEX);
1762
1763   is_internalFormat_depth_or_depthstencil =
1764      _mesa_is_depth_format(internalFormat) ||
1765      _mesa_is_depthstencil_format(internalFormat);
1766
1767   is_format_depth_or_depthstencil =
1768      _mesa_is_depth_format(format) ||
1769      _mesa_is_depthstencil_format(format);
1770
1771   colorFormat = _mesa_is_color_format(format);
1772
1773   if (_mesa_is_color_format(internalFormat) && !colorFormat && !indexFormat)
1774      return false;
1775
1776   if (is_internalFormat_depth_or_depthstencil !=
1777       is_format_depth_or_depthstencil)
1778      return false;
1779
1780   if (_mesa_is_ycbcr_format(internalFormat) != _mesa_is_ycbcr_format(format))
1781      return false;
1782
1783   return true;
1784}
1785
1786/**
1787 * Test the combination of format, type and internal format arguments of
1788 * different texture operations on GLES.
1789 *
1790 * \param ctx GL context.
1791 * \param format pixel data format given by the user.
1792 * \param type pixel data type given by the user.
1793 * \param internalFormat internal format given by the user.
1794 * \param callerName name of the caller function to print in the error message
1795 *
1796 * \return true if a error is found, false otherwise
1797 *
1798 * Currently, it is used by texture_error_check() and texsubimage_error_check().
1799 */
1800static bool
1801texture_format_error_check_gles(struct gl_context *ctx, GLenum format,
1802                                GLenum type, GLenum internalFormat, const char *callerName)
1803{
1804   GLenum err = _mesa_gles_error_check_format_and_type(ctx, format, type,
1805                                                       internalFormat);
1806   if (err != GL_NO_ERROR) {
1807      _mesa_error(ctx, err,
1808                  "%s(format = %s, type = %s, internalformat = %s)",
1809                  callerName, _mesa_enum_to_string(format),
1810                  _mesa_enum_to_string(type),
1811                  _mesa_enum_to_string(internalFormat));
1812      return true;
1813   }
1814
1815   return false;
1816}
1817
1818/**
1819 * Test the glTexImage[123]D() parameters for errors.
1820 *
1821 * \param ctx GL context.
1822 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1823 * \param target texture target given by the user (already validated).
1824 * \param level image level given by the user.
1825 * \param internalFormat internal format given by the user.
1826 * \param format pixel data format given by the user.
1827 * \param type pixel data type given by the user.
1828 * \param width image width given by the user.
1829 * \param height image height given by the user.
1830 * \param depth image depth given by the user.
1831 * \param border image border given by the user.
1832 *
1833 * \return GL_TRUE if a error is found, GL_FALSE otherwise
1834 *
1835 * Verifies each of the parameters against the constants specified in
1836 * __struct gl_contextRec::Const and the supported extensions, and according
1837 * to the OpenGL specification.
1838 * Note that we don't fully error-check the width, height, depth values
1839 * here.  That's done in _mesa_legal_texture_dimensions() which is used
1840 * by several other GL entrypoints.  Plus, texture dims have a special
1841 * interaction with proxy textures.
1842 */
1843static GLboolean
1844texture_error_check( struct gl_context *ctx,
1845                     GLuint dimensions, GLenum target,
1846                     GLint level, GLint internalFormat,
1847                     GLenum format, GLenum type,
1848                     GLint width, GLint height,
1849                     GLint depth, GLint border,
1850                     const GLvoid *pixels )
1851{
1852   GLenum err;
1853
1854   /* Note: for proxy textures, some error conditions immediately generate
1855    * a GL error in the usual way.  But others do not generate a GL error.
1856    * Instead, they cause the width, height, depth, format fields of the
1857    * texture image to be zeroed-out.  The GL spec seems to indicate that the
1858    * zero-out behaviour is only used in cases related to memory allocation.
1859    */
1860
1861   /* level check */
1862   if (level < 0 || level >= _mesa_max_texture_levels(ctx, target)) {
1863      _mesa_error(ctx, GL_INVALID_VALUE,
1864                  "glTexImage%dD(level=%d)", dimensions, level);
1865      return GL_TRUE;
1866   }
1867
1868   /* Check border */
1869   if (border < 0 || border > 1 ||
1870       ((ctx->API != API_OPENGL_COMPAT ||
1871         target == GL_TEXTURE_RECTANGLE_NV ||
1872         target == GL_PROXY_TEXTURE_RECTANGLE_NV) && border != 0)) {
1873      _mesa_error(ctx, GL_INVALID_VALUE,
1874                  "glTexImage%dD(border=%d)", dimensions, border);
1875      return GL_TRUE;
1876   }
1877
1878   if (width < 0 || height < 0 || depth < 0) {
1879      _mesa_error(ctx, GL_INVALID_VALUE,
1880                  "glTexImage%dD(width, height or depth < 0)", dimensions);
1881      return GL_TRUE;
1882   }
1883
1884   /* Check incoming image format and type */
1885   err = _mesa_error_check_format_and_type(ctx, format, type);
1886   if (err != GL_NO_ERROR) {
1887      /* Prior to OpenGL-ES 2.0, an INVALID_VALUE is expected instead of
1888       * INVALID_ENUM. From page 73 OpenGL ES 1.1 spec:
1889       *
1890       *     "Specifying a value for internalformat that is not one of the
1891       *      above (acceptable) values generates the error INVALID VALUE."
1892       */
1893      if (err == GL_INVALID_ENUM && _mesa_is_gles(ctx) && ctx->Version < 20)
1894         err = GL_INVALID_VALUE;
1895
1896      _mesa_error(ctx, err,
1897                  "glTexImage%dD(incompatible format = %s, type = %s)",
1898                  dimensions, _mesa_enum_to_string(format),
1899                  _mesa_enum_to_string(type));
1900      return GL_TRUE;
1901   }
1902
1903   /* Check internalFormat */
1904   if (_mesa_base_tex_format(ctx, internalFormat) < 0) {
1905      _mesa_error(ctx, GL_INVALID_VALUE,
1906                  "glTexImage%dD(internalFormat=%s)",
1907                  dimensions, _mesa_enum_to_string(internalFormat));
1908      return GL_TRUE;
1909   }
1910
1911   /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
1912    * combinations of format, internalFormat, and type that can be used.
1913    * Formats and types that require additional extensions (e.g., GL_FLOAT
1914    * requires GL_OES_texture_float) are filtered elsewhere.
1915    */
1916   char bufCallerName[20];
1917   _mesa_snprintf(bufCallerName, 20, "glTexImage%dD", dimensions);
1918   if (_mesa_is_gles(ctx) &&
1919       texture_format_error_check_gles(ctx, format, type,
1920                                       internalFormat, bufCallerName)) {
1921      return GL_TRUE;
1922   }
1923
1924   /* validate the bound PBO, if any */
1925   if (!_mesa_validate_pbo_source(ctx, dimensions, &ctx->Unpack,
1926                                  width, height, depth, format, type,
1927                                  INT_MAX, pixels, "glTexImage")) {
1928      return GL_TRUE;
1929   }
1930
1931   /* make sure internal format and format basically agree */
1932   if (!texture_formats_agree(internalFormat, format)) {
1933      _mesa_error(ctx, GL_INVALID_OPERATION,
1934                  "glTexImage%dD(incompatible internalFormat = %s, format = %s)",
1935                  dimensions, _mesa_enum_to_string(internalFormat),
1936                  _mesa_enum_to_string(format));
1937      return GL_TRUE;
1938   }
1939
1940   /* additional checks for ycbcr textures */
1941   if (internalFormat == GL_YCBCR_MESA) {
1942      assert(ctx->Extensions.MESA_ycbcr_texture);
1943      if (type != GL_UNSIGNED_SHORT_8_8_MESA &&
1944          type != GL_UNSIGNED_SHORT_8_8_REV_MESA) {
1945         char message[100];
1946         _mesa_snprintf(message, sizeof(message),
1947                        "glTexImage%dD(format/type YCBCR mismatch)",
1948                        dimensions);
1949         _mesa_error(ctx, GL_INVALID_ENUM, "%s", message);
1950         return GL_TRUE; /* error */
1951      }
1952      if (target != GL_TEXTURE_2D &&
1953          target != GL_PROXY_TEXTURE_2D &&
1954          target != GL_TEXTURE_RECTANGLE_NV &&
1955          target != GL_PROXY_TEXTURE_RECTANGLE_NV) {
1956         _mesa_error(ctx, GL_INVALID_ENUM,
1957                     "glTexImage%dD(bad target for YCbCr texture)",
1958                     dimensions);
1959         return GL_TRUE;
1960      }
1961      if (border != 0) {
1962         char message[100];
1963         _mesa_snprintf(message, sizeof(message),
1964                        "glTexImage%dD(format=GL_YCBCR_MESA and border=%d)",
1965                        dimensions, border);
1966         _mesa_error(ctx, GL_INVALID_VALUE, "%s", message);
1967         return GL_TRUE;
1968      }
1969   }
1970
1971   /* additional checks for depth textures */
1972   if (!_mesa_legal_texture_base_format_for_target(ctx, target, internalFormat)) {
1973      _mesa_error(ctx, GL_INVALID_OPERATION,
1974                  "glTexImage%dD(bad target for texture)", dimensions);
1975      return GL_TRUE;
1976   }
1977
1978   /* additional checks for compressed textures */
1979   if (_mesa_is_compressed_format(ctx, internalFormat)) {
1980      GLenum err;
1981      if (!_mesa_target_can_be_compressed(ctx, target, internalFormat, &err)) {
1982         _mesa_error(ctx, err,
1983                     "glTexImage%dD(target can't be compressed)", dimensions);
1984         return GL_TRUE;
1985      }
1986      if (_mesa_format_no_online_compression(internalFormat)) {
1987         _mesa_error(ctx, GL_INVALID_OPERATION,
1988                     "glTexImage%dD(no compression for format)", dimensions);
1989         return GL_TRUE;
1990      }
1991      if (border != 0) {
1992         _mesa_error(ctx, GL_INVALID_OPERATION,
1993                     "glTexImage%dD(border!=0)", dimensions);
1994         return GL_TRUE;
1995      }
1996   }
1997
1998   /* additional checks for integer textures */
1999   if ((ctx->Version >= 30 || ctx->Extensions.EXT_texture_integer) &&
2000       (_mesa_is_enum_format_integer(format) !=
2001        _mesa_is_enum_format_integer(internalFormat))) {
2002      _mesa_error(ctx, GL_INVALID_OPERATION,
2003                  "glTexImage%dD(integer/non-integer format mismatch)",
2004                  dimensions);
2005      return GL_TRUE;
2006   }
2007
2008   if (!mutable_tex_object(ctx, target)) {
2009      _mesa_error(ctx, GL_INVALID_OPERATION,
2010                  "glTexImage%dD(immutable texture)", dimensions);
2011      return GL_TRUE;
2012   }
2013
2014   /* if we get here, the parameters are OK */
2015   return GL_FALSE;
2016}
2017
2018
2019/**
2020 * Error checking for glCompressedTexImage[123]D().
2021 * Note that the width, height and depth values are not fully error checked
2022 * here.
2023 * \return GL_TRUE if a error is found, GL_FALSE otherwise
2024 */
2025static GLenum
2026compressed_texture_error_check(struct gl_context *ctx, GLint dimensions,
2027                               GLenum target, GLint level,
2028                               GLenum internalFormat, GLsizei width,
2029                               GLsizei height, GLsizei depth, GLint border,
2030                               GLsizei imageSize, const GLvoid *data)
2031{
2032   const GLint maxLevels = _mesa_max_texture_levels(ctx, target);
2033   GLint expectedSize;
2034   GLenum error = GL_NO_ERROR;
2035   char *reason = ""; /* no error */
2036
2037   if (!_mesa_target_can_be_compressed(ctx, target, internalFormat, &error)) {
2038      reason = "target";
2039      goto error;
2040   }
2041
2042   /* This will detect any invalid internalFormat value */
2043   if (!_mesa_is_compressed_format(ctx, internalFormat)) {
2044      _mesa_error(ctx, GL_INVALID_ENUM,
2045                  "glCompressedTexImage%dD(internalFormat=%s)",
2046                  dimensions, _mesa_enum_to_string(internalFormat));
2047      return GL_TRUE;
2048   }
2049
2050   /* validate the bound PBO, if any */
2051   if (!_mesa_validate_pbo_source_compressed(ctx, dimensions, &ctx->Unpack,
2052                                             imageSize, data,
2053                                             "glCompressedTexImage")) {
2054      return GL_TRUE;
2055   }
2056
2057   switch (internalFormat) {
2058   case GL_PALETTE4_RGB8_OES:
2059   case GL_PALETTE4_RGBA8_OES:
2060   case GL_PALETTE4_R5_G6_B5_OES:
2061   case GL_PALETTE4_RGBA4_OES:
2062   case GL_PALETTE4_RGB5_A1_OES:
2063   case GL_PALETTE8_RGB8_OES:
2064   case GL_PALETTE8_RGBA8_OES:
2065   case GL_PALETTE8_R5_G6_B5_OES:
2066   case GL_PALETTE8_RGBA4_OES:
2067   case GL_PALETTE8_RGB5_A1_OES:
2068      /* check level (note that level should be zero or less!) */
2069      if (level > 0 || level < -maxLevels) {
2070         reason = "level";
2071         error = GL_INVALID_VALUE;
2072         goto error;
2073      }
2074
2075      if (dimensions != 2) {
2076         reason = "compressed paletted textures must be 2D";
2077         error = GL_INVALID_OPERATION;
2078         goto error;
2079      }
2080
2081      /* Figure out the expected texture size (in bytes).  This will be
2082       * checked against the actual size later.
2083       */
2084      expectedSize = _mesa_cpal_compressed_size(level, internalFormat,
2085                                                width, height);
2086
2087      /* This is for the benefit of the TestProxyTexImage below.  It expects
2088       * level to be non-negative.  OES_compressed_paletted_texture uses a
2089       * weird mechanism where the level specified to glCompressedTexImage2D
2090       * is -(n-1) number of levels in the texture, and the data specifies the
2091       * complete mipmap stack.  This is done to ensure the palette is the
2092       * same for all levels.
2093       */
2094      level = -level;
2095      break;
2096
2097   default:
2098      /* check level */
2099      if (level < 0 || level >= maxLevels) {
2100         reason = "level";
2101         error = GL_INVALID_VALUE;
2102         goto error;
2103      }
2104
2105      /* Figure out the expected texture size (in bytes).  This will be
2106       * checked against the actual size later.
2107       */
2108      expectedSize = compressed_tex_size(width, height, depth, internalFormat);
2109      break;
2110   }
2111
2112   /* This should really never fail */
2113   if (_mesa_base_tex_format(ctx, internalFormat) < 0) {
2114      reason = "internalFormat";
2115      error = GL_INVALID_ENUM;
2116      goto error;
2117   }
2118
2119   /* No compressed formats support borders at this time */
2120   if (border != 0) {
2121      reason = "border != 0";
2122      error = GL_INVALID_VALUE;
2123      goto error;
2124   }
2125
2126   /* Check for invalid pixel storage modes */
2127   if (!_mesa_compressed_pixel_storage_error_check(ctx, dimensions,
2128                                                   &ctx->Unpack,
2129                                                   "glCompressedTexImage")) {
2130      return GL_FALSE;
2131   }
2132
2133   /* check image size in bytes */
2134   if (expectedSize != imageSize) {
2135      /* Per GL_ARB_texture_compression:  GL_INVALID_VALUE is generated [...]
2136       * if <imageSize> is not consistent with the format, dimensions, and
2137       * contents of the specified image.
2138       */
2139      reason = "imageSize inconsistent with width/height/format";
2140      error = GL_INVALID_VALUE;
2141      goto error;
2142   }
2143
2144   if (!mutable_tex_object(ctx, target)) {
2145      reason = "immutable texture";
2146      error = GL_INVALID_OPERATION;
2147      goto error;
2148   }
2149
2150   return GL_FALSE;
2151
2152error:
2153   /* Note: not all error paths exit through here. */
2154   _mesa_error(ctx, error, "glCompressedTexImage%dD(%s)",
2155               dimensions, reason);
2156   return GL_TRUE;
2157}
2158
2159
2160
2161/**
2162 * Test glTexSubImage[123]D() parameters for errors.
2163 *
2164 * \param ctx GL context.
2165 * \param dimensions texture image dimensions (must be 1, 2 or 3).
2166 * \param target texture target given by the user (already validated)
2167 * \param level image level given by the user.
2168 * \param xoffset sub-image x offset given by the user.
2169 * \param yoffset sub-image y offset given by the user.
2170 * \param zoffset sub-image z offset given by the user.
2171 * \param format pixel data format given by the user.
2172 * \param type pixel data type given by the user.
2173 * \param width image width given by the user.
2174 * \param height image height given by the user.
2175 * \param depth image depth given by the user.
2176 *
2177 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2178 *
2179 * Verifies each of the parameters against the constants specified in
2180 * __struct gl_contextRec::Const and the supported extensions, and according
2181 * to the OpenGL specification.
2182 */
2183static GLboolean
2184texsubimage_error_check(struct gl_context *ctx, GLuint dimensions,
2185                        struct gl_texture_object *texObj,
2186                        GLenum target, GLint level,
2187                        GLint xoffset, GLint yoffset, GLint zoffset,
2188                        GLint width, GLint height, GLint depth,
2189                        GLenum format, GLenum type, const GLvoid *pixels,
2190                        const char *callerName)
2191{
2192   struct gl_texture_image *texImage;
2193   GLenum err;
2194
2195   if (!texObj) {
2196      /* must be out of memory */
2197      _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s()", callerName);
2198      return GL_TRUE;
2199   }
2200
2201   /* level check */
2202   if (level < 0 || level >= _mesa_max_texture_levels(ctx, target)) {
2203      _mesa_error(ctx, GL_INVALID_VALUE, "%s(level=%d)", callerName, level);
2204      return GL_TRUE;
2205   }
2206
2207   if (error_check_subtexture_negative_dimensions(ctx, dimensions,
2208                                                  width, height, depth,
2209                                                  callerName)) {
2210      return GL_TRUE;
2211   }
2212
2213   texImage = _mesa_select_tex_image(texObj, target, level);
2214   if (!texImage) {
2215      /* non-existant texture level */
2216      _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid texture level %d)",
2217                  callerName, level);
2218      return GL_TRUE;
2219   }
2220
2221   err = _mesa_error_check_format_and_type(ctx, format, type);
2222   if (err != GL_NO_ERROR) {
2223      _mesa_error(ctx, err,
2224                  "%s(incompatible format = %s, type = %s)",
2225                  callerName, _mesa_enum_to_string(format),
2226                  _mesa_enum_to_string(type));
2227      return GL_TRUE;
2228   }
2229
2230   GLenum internalFormat = _mesa_is_gles(ctx) ?
2231      oes_float_internal_format(ctx, texImage->InternalFormat, type) :
2232      texImage->InternalFormat;
2233
2234   /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
2235    * combinations of format, internalFormat, and type that can be used.
2236    * Formats and types that require additional extensions (e.g., GL_FLOAT
2237    * requires GL_OES_texture_float) are filtered elsewhere.
2238    */
2239   if (_mesa_is_gles(ctx) &&
2240       texture_format_error_check_gles(ctx, format, type,
2241                                       internalFormat, callerName)) {
2242      return GL_TRUE;
2243   }
2244
2245   /* validate the bound PBO, if any */
2246   if (!_mesa_validate_pbo_source(ctx, dimensions, &ctx->Unpack,
2247                                  width, height, depth, format, type,
2248                                  INT_MAX, pixels, callerName)) {
2249      return GL_TRUE;
2250   }
2251
2252   if (error_check_subtexture_dimensions(ctx, dimensions,
2253                                         texImage, xoffset, yoffset, zoffset,
2254                                         width, height, depth, callerName)) {
2255      return GL_TRUE;
2256   }
2257
2258   if (_mesa_is_format_compressed(texImage->TexFormat)) {
2259      if (_mesa_format_no_online_compression(texImage->InternalFormat)) {
2260         _mesa_error(ctx, GL_INVALID_OPERATION,
2261               "%s(no compression for format)", callerName);
2262         return GL_TRUE;
2263      }
2264   }
2265
2266   if (ctx->Version >= 30 || ctx->Extensions.EXT_texture_integer) {
2267      /* both source and dest must be integer-valued, or neither */
2268      if (_mesa_is_format_integer_color(texImage->TexFormat) !=
2269          _mesa_is_enum_format_integer(format)) {
2270         _mesa_error(ctx, GL_INVALID_OPERATION,
2271                     "%s(integer/non-integer format mismatch)", callerName);
2272         return GL_TRUE;
2273      }
2274   }
2275
2276   return GL_FALSE;
2277}
2278
2279
2280/**
2281 * Test glCopyTexImage[12]D() parameters for errors.
2282 *
2283 * \param ctx GL context.
2284 * \param dimensions texture image dimensions (must be 1, 2 or 3).
2285 * \param target texture target given by the user.
2286 * \param level image level given by the user.
2287 * \param internalFormat internal format given by the user.
2288 * \param width image width given by the user.
2289 * \param height image height given by the user.
2290 * \param border texture border.
2291 *
2292 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2293 *
2294 * Verifies each of the parameters against the constants specified in
2295 * __struct gl_contextRec::Const and the supported extensions, and according
2296 * to the OpenGL specification.
2297 */
2298static GLboolean
2299copytexture_error_check( struct gl_context *ctx, GLuint dimensions,
2300                         GLenum target, GLint level, GLint internalFormat,
2301                         GLint border )
2302{
2303   GLint baseFormat;
2304   GLint rb_base_format;
2305   struct gl_renderbuffer *rb;
2306   GLenum rb_internal_format;
2307
2308   /* check target */
2309   if (!legal_texsubimage_target(ctx, dimensions, target, false)) {
2310      _mesa_error(ctx, GL_INVALID_ENUM, "glCopyTexImage%uD(target=%s)",
2311                  dimensions, _mesa_enum_to_string(target));
2312      return GL_TRUE;
2313   }
2314
2315   /* level check */
2316   if (level < 0 || level >= _mesa_max_texture_levels(ctx, target)) {
2317      _mesa_error(ctx, GL_INVALID_VALUE,
2318                  "glCopyTexImage%dD(level=%d)", dimensions, level);
2319      return GL_TRUE;
2320   }
2321
2322   /* Check that the source buffer is complete */
2323   if (_mesa_is_user_fbo(ctx->ReadBuffer)) {
2324      if (ctx->ReadBuffer->_Status == 0) {
2325         _mesa_test_framebuffer_completeness(ctx, ctx->ReadBuffer);
2326      }
2327      if (ctx->ReadBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
2328         _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
2329                     "glCopyTexImage%dD(invalid readbuffer)", dimensions);
2330         return GL_TRUE;
2331      }
2332
2333      if (ctx->ReadBuffer->Visual.samples > 0) {
2334         _mesa_error(ctx, GL_INVALID_OPERATION,
2335                     "glCopyTexImage%dD(multisample FBO)", dimensions);
2336         return GL_TRUE;
2337      }
2338   }
2339
2340   /* Check border */
2341   if (border < 0 || border > 1 ||
2342       ((ctx->API != API_OPENGL_COMPAT ||
2343         target == GL_TEXTURE_RECTANGLE_NV ||
2344         target == GL_PROXY_TEXTURE_RECTANGLE_NV) && border != 0)) {
2345      _mesa_error(ctx, GL_INVALID_VALUE,
2346                  "glCopyTexImage%dD(border=%d)", dimensions, border);
2347      return GL_TRUE;
2348   }
2349
2350   /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
2351    * internalFormat.
2352    */
2353   if (_mesa_is_gles(ctx) && !_mesa_is_gles3(ctx)) {
2354      switch (internalFormat) {
2355      case GL_ALPHA:
2356      case GL_RGB:
2357      case GL_RGBA:
2358      case GL_LUMINANCE:
2359      case GL_LUMINANCE_ALPHA:
2360         break;
2361      default:
2362         _mesa_error(ctx, GL_INVALID_ENUM,
2363                     "glCopyTexImage%dD(internalFormat=%s)", dimensions,
2364                     _mesa_enum_to_string(internalFormat));
2365         return GL_TRUE;
2366      }
2367   } else {
2368      /*
2369       * Section 8.6 (Alternate Texture Image Specification Commands) of the
2370       * OpenGL 4.5 (Compatibility Profile) spec says:
2371       *
2372       *     "Parameters level, internalformat, and border are specified using
2373       *     the same values, with the same meanings, as the corresponding
2374       *     arguments of TexImage2D, except that internalformat may not be
2375       *     specified as 1, 2, 3, or 4."
2376       */
2377      if (internalFormat >= 1 && internalFormat <= 4) {
2378         _mesa_error(ctx, GL_INVALID_ENUM,
2379                     "glCopyTexImage%dD(internalFormat=%d)", dimensions,
2380                     internalFormat);
2381         return GL_TRUE;
2382      }
2383   }
2384
2385   baseFormat = _mesa_base_tex_format(ctx, internalFormat);
2386   if (baseFormat < 0) {
2387      _mesa_error(ctx, GL_INVALID_ENUM,
2388                  "glCopyTexImage%dD(internalFormat=%s)", dimensions,
2389                  _mesa_enum_to_string(internalFormat));
2390      return GL_TRUE;
2391   }
2392
2393   rb = _mesa_get_read_renderbuffer_for_format(ctx, internalFormat);
2394   if (rb == NULL) {
2395      _mesa_error(ctx, GL_INVALID_OPERATION,
2396                  "glCopyTexImage%dD(read buffer)", dimensions);
2397      return GL_TRUE;
2398   }
2399
2400   rb_internal_format = rb->InternalFormat;
2401   rb_base_format = _mesa_base_tex_format(ctx, rb->InternalFormat);
2402   if (_mesa_is_color_format(internalFormat)) {
2403      if (rb_base_format < 0) {
2404         _mesa_error(ctx, GL_INVALID_VALUE,
2405                     "glCopyTexImage%dD(internalFormat=%s)", dimensions,
2406                     _mesa_enum_to_string(internalFormat));
2407         return GL_TRUE;
2408      }
2409   }
2410
2411   if (_mesa_is_gles(ctx)) {
2412      bool valid = true;
2413      if (_mesa_components_in_format(baseFormat) >
2414          _mesa_components_in_format(rb_base_format)) {
2415         valid = false;
2416      }
2417      if (baseFormat == GL_DEPTH_COMPONENT ||
2418          baseFormat == GL_DEPTH_STENCIL ||
2419          baseFormat == GL_STENCIL_INDEX ||
2420          rb_base_format == GL_DEPTH_COMPONENT ||
2421          rb_base_format == GL_DEPTH_STENCIL ||
2422          rb_base_format == GL_STENCIL_INDEX ||
2423          ((baseFormat == GL_LUMINANCE_ALPHA ||
2424            baseFormat == GL_ALPHA) &&
2425           rb_base_format != GL_RGBA) ||
2426          internalFormat == GL_RGB9_E5) {
2427         valid = false;
2428      }
2429      if (internalFormat == GL_RGB9_E5) {
2430         valid = false;
2431      }
2432      if (!valid) {
2433         _mesa_error(ctx, GL_INVALID_OPERATION,
2434                     "glCopyTexImage%dD(internalFormat=%s)", dimensions,
2435                     _mesa_enum_to_string(internalFormat));
2436         return GL_TRUE;
2437      }
2438   }
2439
2440   if (_mesa_is_gles3(ctx)) {
2441      bool rb_is_srgb = false;
2442      bool dst_is_srgb = false;
2443
2444      if (ctx->Extensions.EXT_sRGB &&
2445          _mesa_get_format_color_encoding(rb->Format) == GL_SRGB) {
2446         rb_is_srgb = true;
2447      }
2448
2449      if (_mesa_get_linear_internalformat(internalFormat) != internalFormat) {
2450         dst_is_srgb = true;
2451      }
2452
2453      if (rb_is_srgb != dst_is_srgb) {
2454         /* Page 137 (page 149 of the PDF) in section 3.8.5 of the
2455          * OpenGLES 3.0.0 spec says:
2456          *
2457          *     "The error INVALID_OPERATION is also generated if the
2458          *     value of FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING for the
2459          *     framebuffer attachment corresponding to the read buffer
2460          *     is LINEAR (see section 6.1.13) and internalformat is
2461          *     one of the sRGB formats described in section 3.8.16, or
2462          *     if the value of FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING is
2463          *     SRGB and internalformat is not one of the sRGB formats."
2464          */
2465         _mesa_error(ctx, GL_INVALID_OPERATION,
2466                     "glCopyTexImage%dD(srgb usage mismatch)", dimensions);
2467         return GL_TRUE;
2468      }
2469
2470      /* Page 139, Table 3.15 of OpenGL ES 3.0 spec does not define ReadPixels
2471       * types for SNORM formats. Also, conversion to SNORM formats is not
2472       * allowed by Table 3.2 on Page 110.
2473       */
2474      if (!_mesa_has_EXT_render_snorm(ctx) &&
2475          _mesa_is_enum_format_snorm(internalFormat)) {
2476         _mesa_error(ctx, GL_INVALID_OPERATION,
2477                     "glCopyTexImage%dD(internalFormat=%s)", dimensions,
2478                     _mesa_enum_to_string(internalFormat));
2479         return GL_TRUE;
2480      }
2481   }
2482
2483   if (!_mesa_source_buffer_exists(ctx, baseFormat)) {
2484      _mesa_error(ctx, GL_INVALID_OPERATION,
2485                  "glCopyTexImage%dD(missing readbuffer)", dimensions);
2486      return GL_TRUE;
2487   }
2488
2489   /* From the EXT_texture_integer spec:
2490    *
2491    *     "INVALID_OPERATION is generated by CopyTexImage* and CopyTexSubImage*
2492    *      if the texture internalformat is an integer format and the read color
2493    *      buffer is not an integer format, or if the internalformat is not an
2494    *      integer format and the read color buffer is an integer format."
2495    */
2496   if (_mesa_is_color_format(internalFormat)) {
2497      bool is_int = _mesa_is_enum_format_integer(internalFormat);
2498      bool is_rbint = _mesa_is_enum_format_integer(rb_internal_format);
2499      bool is_unorm = _mesa_is_enum_format_unorm(internalFormat);
2500      bool is_rbunorm = _mesa_is_enum_format_unorm(rb_internal_format);
2501      if (is_int || is_rbint) {
2502         if (is_int != is_rbint) {
2503            _mesa_error(ctx, GL_INVALID_OPERATION,
2504                        "glCopyTexImage%dD(integer vs non-integer)", dimensions);
2505            return GL_TRUE;
2506         } else if (_mesa_is_gles(ctx) &&
2507                    _mesa_is_enum_format_unsigned_int(internalFormat) !=
2508                      _mesa_is_enum_format_unsigned_int(rb_internal_format)) {
2509            _mesa_error(ctx, GL_INVALID_OPERATION,
2510                        "glCopyTexImage%dD(signed vs unsigned integer)",
2511                        dimensions);
2512            return GL_TRUE;
2513         }
2514      }
2515
2516      /* From page 138 of OpenGL ES 3.0 spec:
2517       *    "The error INVALID_OPERATION is generated if floating-point RGBA
2518       *    data is required; if signed integer RGBA data is required and the
2519       *    format of the current color buffer is not signed integer; if
2520       *    unsigned integer RGBA data is required and the format of the
2521       *    current color buffer is not unsigned integer; or if fixed-point
2522       *    RGBA data is required and the format of the current color buffer
2523       *    is not fixed-point.
2524       */
2525      if (_mesa_is_gles(ctx) && is_unorm != is_rbunorm)
2526            _mesa_error(ctx, GL_INVALID_OPERATION,
2527                        "glCopyTexImage%dD(unorm vs non-unorm)", dimensions);
2528   }
2529
2530   if (_mesa_is_compressed_format(ctx, internalFormat)) {
2531      GLenum err;
2532      if (!_mesa_target_can_be_compressed(ctx, target, internalFormat, &err)) {
2533         _mesa_error(ctx, err,
2534                     "glCopyTexImage%dD(target can't be compressed)", dimensions);
2535         return GL_TRUE;
2536      }
2537      if (_mesa_format_no_online_compression(internalFormat)) {
2538         _mesa_error(ctx, GL_INVALID_OPERATION,
2539               "glCopyTexImage%dD(no compression for format)", dimensions);
2540         return GL_TRUE;
2541      }
2542      if (border != 0) {
2543         _mesa_error(ctx, GL_INVALID_OPERATION,
2544                     "glCopyTexImage%dD(border!=0)", dimensions);
2545         return GL_TRUE;
2546      }
2547   }
2548
2549   if (!mutable_tex_object(ctx, target)) {
2550      _mesa_error(ctx, GL_INVALID_OPERATION,
2551                  "glCopyTexImage%dD(immutable texture)", dimensions);
2552      return GL_TRUE;
2553   }
2554
2555   /* if we get here, the parameters are OK */
2556   return GL_FALSE;
2557}
2558
2559
2560/**
2561 * Test glCopyTexSubImage[12]D() parameters for errors.
2562 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2563 */
2564static GLboolean
2565copytexsubimage_error_check(struct gl_context *ctx, GLuint dimensions,
2566                            const struct gl_texture_object *texObj,
2567                            GLenum target, GLint level,
2568                            GLint xoffset, GLint yoffset, GLint zoffset,
2569                            GLint width, GLint height, const char *caller)
2570{
2571   assert(texObj);
2572
2573   struct gl_texture_image *texImage;
2574
2575   /* Check that the source buffer is complete */
2576   if (_mesa_is_user_fbo(ctx->ReadBuffer)) {
2577      if (ctx->ReadBuffer->_Status == 0) {
2578         _mesa_test_framebuffer_completeness(ctx, ctx->ReadBuffer);
2579      }
2580      if (ctx->ReadBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
2581         _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
2582                     "%s(invalid readbuffer)", caller);
2583         return GL_TRUE;
2584      }
2585
2586      if (ctx->ReadBuffer->Visual.samples > 0) {
2587         _mesa_error(ctx, GL_INVALID_OPERATION,
2588                "%s(multisample FBO)", caller);
2589         return GL_TRUE;
2590      }
2591   }
2592
2593   /* Check level */
2594   if (level < 0 || level >= _mesa_max_texture_levels(ctx, target)) {
2595      _mesa_error(ctx, GL_INVALID_VALUE, "%s(level=%d)", caller, level);
2596      return GL_TRUE;
2597   }
2598
2599   texImage = _mesa_select_tex_image(texObj, target, level);
2600   if (!texImage) {
2601      /* destination image does not exist */
2602      _mesa_error(ctx, GL_INVALID_OPERATION,
2603                  "%s(invalid texture level %d)", caller, level);
2604      return GL_TRUE;
2605   }
2606
2607   if (error_check_subtexture_negative_dimensions(ctx, dimensions,
2608                                                  width, height, 1, caller)) {
2609      return GL_TRUE;
2610   }
2611
2612   if (error_check_subtexture_dimensions(ctx, dimensions, texImage,
2613                                         xoffset, yoffset, zoffset,
2614                                         width, height, 1, caller)) {
2615      return GL_TRUE;
2616   }
2617
2618   if (_mesa_is_format_compressed(texImage->TexFormat)) {
2619      if (_mesa_format_no_online_compression(texImage->InternalFormat)) {
2620         _mesa_error(ctx, GL_INVALID_OPERATION,
2621               "%s(no compression for format)", caller);
2622         return GL_TRUE;
2623      }
2624   }
2625
2626   if (texImage->InternalFormat == GL_YCBCR_MESA) {
2627      _mesa_error(ctx, GL_INVALID_OPERATION, "%s()", caller);
2628      return GL_TRUE;
2629   }
2630
2631   /* From OpenGL ES 3.2 spec, section 8.6:
2632    *
2633    *     "An INVALID_OPERATION error is generated by CopyTexSubImage3D,
2634    *      CopyTexImage2D, or CopyTexSubImage2D if the internalformat of the
2635    *      texture image being (re)specified is RGB9_E5"
2636    */
2637   if (texImage->InternalFormat == GL_RGB9_E5 &&
2638       !_mesa_is_desktop_gl(ctx)) {
2639      _mesa_error(ctx, GL_INVALID_OPERATION,
2640                  "%s(invalid internal format %s)", caller,
2641                  _mesa_enum_to_string(texImage->InternalFormat));
2642      return GL_TRUE;
2643   }
2644
2645   if (!_mesa_source_buffer_exists(ctx, texImage->_BaseFormat)) {
2646      _mesa_error(ctx, GL_INVALID_OPERATION,
2647                  "%s(missing readbuffer, format=%s)", caller,
2648                  _mesa_enum_to_string(texImage->_BaseFormat));
2649      return GL_TRUE;
2650   }
2651
2652   /* From the EXT_texture_integer spec:
2653    *
2654    *     "INVALID_OPERATION is generated by CopyTexImage* and
2655    *     CopyTexSubImage* if the texture internalformat is an integer format
2656    *     and the read color buffer is not an integer format, or if the
2657    *     internalformat is not an integer format and the read color buffer
2658    *     is an integer format."
2659    */
2660   if (_mesa_is_color_format(texImage->InternalFormat)) {
2661      struct gl_renderbuffer *rb = ctx->ReadBuffer->_ColorReadBuffer;
2662
2663      if (_mesa_is_format_integer_color(rb->Format) !=
2664          _mesa_is_format_integer_color(texImage->TexFormat)) {
2665         _mesa_error(ctx, GL_INVALID_OPERATION,
2666                     "%s(integer vs non-integer)", caller);
2667         return GL_TRUE;
2668      }
2669   }
2670
2671   /* In the ES 3.2 specification's Table 8.13 (Valid CopyTexImage source
2672    * framebuffer/destination texture base internal format combinations),
2673    * all the entries for stencil are left blank (unsupported).
2674    */
2675   if (_mesa_is_gles(ctx) && _mesa_is_stencil_format(texImage->_BaseFormat)) {
2676      _mesa_error(ctx, GL_INVALID_OPERATION, "%s(stencil disallowed)", caller);
2677      return GL_TRUE;
2678   }
2679
2680   /* if we get here, the parameters are OK */
2681   return GL_FALSE;
2682}
2683
2684
2685/** Callback info for walking over FBO hash table */
2686struct cb_info
2687{
2688   struct gl_context *ctx;
2689   struct gl_texture_object *texObj;
2690   GLuint level, face;
2691};
2692
2693
2694/**
2695 * Check render to texture callback.  Called from _mesa_HashWalk().
2696 */
2697static void
2698check_rtt_cb(UNUSED GLuint key, void *data, void *userData)
2699{
2700   struct gl_framebuffer *fb = (struct gl_framebuffer *) data;
2701   const struct cb_info *info = (struct cb_info *) userData;
2702   struct gl_context *ctx = info->ctx;
2703   const struct gl_texture_object *texObj = info->texObj;
2704   const GLuint level = info->level, face = info->face;
2705
2706   /* If this is a user-created FBO */
2707   if (_mesa_is_user_fbo(fb)) {
2708      GLuint i;
2709      /* check if any of the FBO's attachments point to 'texObj' */
2710      for (i = 0; i < BUFFER_COUNT; i++) {
2711         struct gl_renderbuffer_attachment *att = fb->Attachment + i;
2712         if (att->Type == GL_TEXTURE &&
2713             att->Texture == texObj &&
2714             att->TextureLevel == level &&
2715             att->CubeMapFace == face) {
2716            _mesa_update_texture_renderbuffer(ctx, fb, att);
2717            assert(att->Renderbuffer->TexImage);
2718            /* Mark fb status as indeterminate to force re-validation */
2719            fb->_Status = 0;
2720
2721            /* Make sure that the revalidation actually happens if this is
2722             * being done to currently-bound buffers.
2723             */
2724            if (fb == ctx->DrawBuffer || fb == ctx->ReadBuffer)
2725               ctx->NewState |= _NEW_BUFFERS;
2726         }
2727      }
2728   }
2729}
2730
2731
2732/**
2733 * When a texture image is specified we have to check if it's bound to
2734 * any framebuffer objects (render to texture) in order to detect changes
2735 * in size or format since that effects FBO completeness.
2736 * Any FBOs rendering into the texture must be re-validated.
2737 */
2738void
2739_mesa_update_fbo_texture(struct gl_context *ctx,
2740                         struct gl_texture_object *texObj,
2741                         GLuint face, GLuint level)
2742{
2743   /* Only check this texture if it's been marked as RenderToTexture */
2744   if (texObj->_RenderToTexture) {
2745      struct cb_info info;
2746      info.ctx = ctx;
2747      info.texObj = texObj;
2748      info.level = level;
2749      info.face = face;
2750      _mesa_HashWalk(ctx->Shared->FrameBuffers, check_rtt_cb, &info);
2751   }
2752}
2753
2754
2755/**
2756 * If the texture object's GenerateMipmap flag is set and we've
2757 * changed the texture base level image, regenerate the rest of the
2758 * mipmap levels now.
2759 */
2760static inline void
2761check_gen_mipmap(struct gl_context *ctx, GLenum target,
2762                 struct gl_texture_object *texObj, GLint level)
2763{
2764   if (texObj->GenerateMipmap &&
2765       level == texObj->BaseLevel &&
2766       level < texObj->MaxLevel) {
2767      assert(ctx->Driver.GenerateMipmap);
2768      ctx->Driver.GenerateMipmap(ctx, target, texObj);
2769   }
2770}
2771
2772
2773/** Debug helper: override the user-requested internal format */
2774static GLenum
2775override_internal_format(GLenum internalFormat, UNUSED GLint width,
2776                         UNUSED GLint height)
2777{
2778#if 0
2779   if (internalFormat == GL_RGBA16F_ARB ||
2780       internalFormat == GL_RGBA32F_ARB) {
2781      printf("Convert rgba float tex to int %d x %d\n", width, height);
2782      return GL_RGBA;
2783   }
2784   else if (internalFormat == GL_RGB16F_ARB ||
2785            internalFormat == GL_RGB32F_ARB) {
2786      printf("Convert rgb float tex to int %d x %d\n", width, height);
2787      return GL_RGB;
2788   }
2789   else if (internalFormat == GL_LUMINANCE_ALPHA16F_ARB ||
2790            internalFormat == GL_LUMINANCE_ALPHA32F_ARB) {
2791      printf("Convert luminance float tex to int %d x %d\n", width, height);
2792      return GL_LUMINANCE_ALPHA;
2793   }
2794   else if (internalFormat == GL_LUMINANCE16F_ARB ||
2795            internalFormat == GL_LUMINANCE32F_ARB) {
2796      printf("Convert luminance float tex to int %d x %d\n", width, height);
2797      return GL_LUMINANCE;
2798   }
2799   else if (internalFormat == GL_ALPHA16F_ARB ||
2800            internalFormat == GL_ALPHA32F_ARB) {
2801      printf("Convert luminance float tex to int %d x %d\n", width, height);
2802      return GL_ALPHA;
2803   }
2804   /*
2805   else if (internalFormat == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT) {
2806      internalFormat = GL_RGBA;
2807   }
2808   */
2809   else {
2810      return internalFormat;
2811   }
2812#else
2813   return internalFormat;
2814#endif
2815}
2816
2817
2818/**
2819 * Choose the actual hardware format for a texture image.
2820 * Try to use the same format as the previous image level when possible.
2821 * Otherwise, ask the driver for the best format.
2822 * It's important to try to choose a consistant format for all levels
2823 * for efficient texture memory layout/allocation.  In particular, this
2824 * comes up during automatic mipmap generation.
2825 */
2826mesa_format
2827_mesa_choose_texture_format(struct gl_context *ctx,
2828                            struct gl_texture_object *texObj,
2829                            GLenum target, GLint level,
2830                            GLenum internalFormat, GLenum format, GLenum type)
2831{
2832   mesa_format f;
2833
2834   /* see if we've already chosen a format for the previous level */
2835   if (level > 0) {
2836      struct gl_texture_image *prevImage =
2837         _mesa_select_tex_image(texObj, target, level - 1);
2838      /* See if the prev level is defined and has an internal format which
2839       * matches the new internal format.
2840       */
2841      if (prevImage &&
2842          prevImage->Width > 0 &&
2843          prevImage->InternalFormat == internalFormat) {
2844         /* use the same format */
2845         assert(prevImage->TexFormat != MESA_FORMAT_NONE);
2846         return prevImage->TexFormat;
2847      }
2848   }
2849
2850   f = ctx->Driver.ChooseTextureFormat(ctx, target, internalFormat,
2851                                       format, type);
2852   assert(f != MESA_FORMAT_NONE);
2853   return f;
2854}
2855
2856
2857/**
2858 * Adjust pixel unpack params and image dimensions to strip off the
2859 * one-pixel texture border.
2860 *
2861 * Gallium and intel don't support texture borders.  They've seldem been used
2862 * and seldom been implemented correctly anyway.
2863 *
2864 * \param unpackNew returns the new pixel unpack parameters
2865 */
2866static void
2867strip_texture_border(GLenum target,
2868                     GLint *width, GLint *height, GLint *depth,
2869                     const struct gl_pixelstore_attrib *unpack,
2870                     struct gl_pixelstore_attrib *unpackNew)
2871{
2872   assert(width);
2873   assert(height);
2874   assert(depth);
2875
2876   *unpackNew = *unpack;
2877
2878   if (unpackNew->RowLength == 0)
2879      unpackNew->RowLength = *width;
2880
2881   if (unpackNew->ImageHeight == 0)
2882      unpackNew->ImageHeight = *height;
2883
2884   assert(*width >= 3);
2885   unpackNew->SkipPixels++;  /* skip the border */
2886   *width = *width - 2;      /* reduce the width by two border pixels */
2887
2888   /* The min height of a texture with a border is 3 */
2889   if (*height >= 3 && target != GL_TEXTURE_1D_ARRAY) {
2890      unpackNew->SkipRows++;  /* skip the border */
2891      *height = *height - 2;  /* reduce the height by two border pixels */
2892   }
2893
2894   if (*depth >= 3 &&
2895       target != GL_TEXTURE_2D_ARRAY &&
2896       target != GL_TEXTURE_CUBE_MAP_ARRAY) {
2897      unpackNew->SkipImages++;  /* skip the border */
2898      *depth = *depth - 2;      /* reduce the depth by two border pixels */
2899   }
2900}
2901
2902
2903/**
2904 * Common code to implement all the glTexImage1D/2D/3D functions
2905 * as well as glCompressedTexImage1D/2D/3D.
2906 * \param compressed  only GL_TRUE for glCompressedTexImage1D/2D/3D calls.
2907 * \param format  the user's image format (only used if !compressed)
2908 * \param type  the user's image type (only used if !compressed)
2909 * \param imageSize  only used for glCompressedTexImage1D/2D/3D calls.
2910 */
2911static ALWAYS_INLINE void
2912teximage(struct gl_context *ctx, GLboolean compressed, GLuint dims,
2913         GLenum target, GLint level, GLint internalFormat,
2914         GLsizei width, GLsizei height, GLsizei depth,
2915         GLint border, GLenum format, GLenum type,
2916         GLsizei imageSize, const GLvoid *pixels, bool no_error)
2917{
2918   const char *func = compressed ? "glCompressedTexImage" : "glTexImage";
2919   struct gl_pixelstore_attrib unpack_no_border;
2920   const struct gl_pixelstore_attrib *unpack = &ctx->Unpack;
2921   struct gl_texture_object *texObj;
2922   mesa_format texFormat;
2923   bool dimensionsOK = true, sizeOK = true;
2924
2925   FLUSH_VERTICES(ctx, 0);
2926
2927   if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE)) {
2928      if (compressed)
2929         _mesa_debug(ctx,
2930                     "glCompressedTexImage%uD %s %d %s %d %d %d %d %p\n",
2931                     dims,
2932                     _mesa_enum_to_string(target), level,
2933                     _mesa_enum_to_string(internalFormat),
2934                     width, height, depth, border, pixels);
2935      else
2936         _mesa_debug(ctx,
2937                     "glTexImage%uD %s %d %s %d %d %d %d %s %s %p\n",
2938                     dims,
2939                     _mesa_enum_to_string(target), level,
2940                     _mesa_enum_to_string(internalFormat),
2941                     width, height, depth, border,
2942                     _mesa_enum_to_string(format),
2943                     _mesa_enum_to_string(type), pixels);
2944   }
2945
2946   internalFormat = override_internal_format(internalFormat, width, height);
2947
2948   if (!no_error) {
2949      /* target error checking */
2950      if (!legal_teximage_target(ctx, dims, target)) {
2951         _mesa_error(ctx, GL_INVALID_ENUM, "%s%uD(target=%s)",
2952                     func, dims, _mesa_enum_to_string(target));
2953         return;
2954      }
2955
2956      /* general error checking */
2957      if (compressed) {
2958         if (compressed_texture_error_check(ctx, dims, target, level,
2959                                            internalFormat,
2960                                            width, height, depth,
2961                                            border, imageSize, pixels))
2962            return;
2963      } else {
2964         if (texture_error_check(ctx, dims, target, level, internalFormat,
2965                                 format, type, width, height, depth, border,
2966                                 pixels))
2967            return;
2968      }
2969   }
2970
2971   /* Here we convert a cpal compressed image into a regular glTexImage2D
2972    * call by decompressing the texture.  If we really want to support cpal
2973    * textures in any driver this would have to be changed.
2974    */
2975   if (ctx->API == API_OPENGLES && compressed && dims == 2) {
2976      switch (internalFormat) {
2977      case GL_PALETTE4_RGB8_OES:
2978      case GL_PALETTE4_RGBA8_OES:
2979      case GL_PALETTE4_R5_G6_B5_OES:
2980      case GL_PALETTE4_RGBA4_OES:
2981      case GL_PALETTE4_RGB5_A1_OES:
2982      case GL_PALETTE8_RGB8_OES:
2983      case GL_PALETTE8_RGBA8_OES:
2984      case GL_PALETTE8_R5_G6_B5_OES:
2985      case GL_PALETTE8_RGBA4_OES:
2986      case GL_PALETTE8_RGB5_A1_OES:
2987         _mesa_cpal_compressed_teximage2d(target, level, internalFormat,
2988                                          width, height, imageSize, pixels);
2989         return;
2990      }
2991   }
2992
2993   texObj = _mesa_get_current_tex_object(ctx, target);
2994   assert(texObj);
2995
2996   if (compressed) {
2997      /* For glCompressedTexImage() the driver has no choice about the
2998       * texture format since we'll never transcode the user's compressed
2999       * image data.  The internalFormat was error checked earlier.
3000       */
3001      texFormat = _mesa_glenum_to_compressed_format(internalFormat);
3002   }
3003   else {
3004      /* In case of HALF_FLOAT_OES or FLOAT_OES, find corresponding sized
3005       * internal floating point format for the given base format.
3006       */
3007      if (_mesa_is_gles(ctx) && format == internalFormat) {
3008         if (type == GL_FLOAT) {
3009            texObj->_IsFloat = GL_TRUE;
3010         } else if (type == GL_HALF_FLOAT_OES || type == GL_HALF_FLOAT) {
3011            texObj->_IsHalfFloat = GL_TRUE;
3012         }
3013
3014         internalFormat = adjust_for_oes_float_texture(ctx, format, type);
3015      }
3016
3017      texFormat = _mesa_choose_texture_format(ctx, texObj, target, level,
3018                                              internalFormat, format, type);
3019   }
3020
3021   assert(texFormat != MESA_FORMAT_NONE);
3022
3023   if (!no_error) {
3024      /* check that width, height, depth are legal for the mipmap level */
3025      dimensionsOK = _mesa_legal_texture_dimensions(ctx, target, level, width,
3026                                                    height, depth, border);
3027
3028      /* check that the texture won't take too much memory, etc */
3029      sizeOK = ctx->Driver.TestProxyTexImage(ctx, proxy_target(target),
3030                                             0, level, texFormat, 1,
3031                                             width, height, depth);
3032   }
3033
3034   if (_mesa_is_proxy_texture(target)) {
3035      /* Proxy texture: just clear or set state depending on error checking */
3036      struct gl_texture_image *texImage =
3037         get_proxy_tex_image(ctx, target, level);
3038
3039      if (!texImage)
3040         return;  /* GL_OUT_OF_MEMORY already recorded */
3041
3042      if (dimensionsOK && sizeOK) {
3043         _mesa_init_teximage_fields(ctx, texImage, width, height, depth,
3044                                    border, internalFormat, texFormat);
3045      }
3046      else {
3047         clear_teximage_fields(texImage);
3048      }
3049   }
3050   else {
3051      /* non-proxy target */
3052      const GLuint face = _mesa_tex_target_to_face(target);
3053      struct gl_texture_image *texImage;
3054
3055      if (!dimensionsOK) {
3056         _mesa_error(ctx, GL_INVALID_VALUE,
3057                     "%s%uD(invalid width=%d or height=%d or depth=%d)",
3058                     func, dims, width, height, depth);
3059         return;
3060      }
3061
3062      if (!sizeOK) {
3063         _mesa_error(ctx, GL_OUT_OF_MEMORY,
3064                     "%s%uD(image too large: %d x %d x %d, %s format)",
3065                     func, dims, width, height, depth,
3066                     _mesa_enum_to_string(internalFormat));
3067         return;
3068      }
3069
3070      /* Allow a hardware driver to just strip out the border, to provide
3071       * reliable but slightly incorrect hardware rendering instead of
3072       * rarely-tested software fallback rendering.
3073       */
3074      if (border && ctx->Const.StripTextureBorder) {
3075         strip_texture_border(target, &width, &height, &depth, unpack,
3076                              &unpack_no_border);
3077         border = 0;
3078         unpack = &unpack_no_border;
3079      }
3080
3081      if (ctx->NewState & _NEW_PIXEL)
3082         _mesa_update_state(ctx);
3083
3084      _mesa_lock_texture(ctx, texObj);
3085      {
3086         texImage = _mesa_get_tex_image(ctx, texObj, target, level);
3087
3088         if (!texImage) {
3089            _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s%uD", func, dims);
3090         }
3091         else {
3092            ctx->Driver.FreeTextureImageBuffer(ctx, texImage);
3093
3094            _mesa_init_teximage_fields(ctx, texImage,
3095                                       width, height, depth,
3096                                       border, internalFormat, texFormat);
3097
3098            /* Give the texture to the driver.  <pixels> may be null. */
3099            if (width > 0 && height > 0 && depth > 0) {
3100               if (compressed) {
3101                  ctx->Driver.CompressedTexImage(ctx, dims, texImage,
3102                                                 imageSize, pixels);
3103               }
3104               else {
3105                  ctx->Driver.TexImage(ctx, dims, texImage, format,
3106                                       type, pixels, unpack);
3107               }
3108            }
3109
3110            check_gen_mipmap(ctx, target, texObj, level);
3111
3112            _mesa_update_fbo_texture(ctx, texObj, face, level);
3113
3114            _mesa_dirty_texobj(ctx, texObj);
3115         }
3116      }
3117      _mesa_unlock_texture(ctx, texObj);
3118   }
3119}
3120
3121
3122/* This is a wrapper around teximage() so that we can force the KHR_no_error
3123 * logic to be inlined without inlining the function into all the callers.
3124 */
3125static void
3126teximage_err(struct gl_context *ctx, GLboolean compressed, GLuint dims,
3127             GLenum target, GLint level, GLint internalFormat,
3128             GLsizei width, GLsizei height, GLsizei depth,
3129             GLint border, GLenum format, GLenum type,
3130             GLsizei imageSize, const GLvoid *pixels)
3131{
3132   teximage(ctx, compressed, dims, target, level, internalFormat, width, height,
3133            depth, border, format, type, imageSize, pixels, false);
3134}
3135
3136
3137static void
3138teximage_no_error(struct gl_context *ctx, GLboolean compressed, GLuint dims,
3139                  GLenum target, GLint level, GLint internalFormat,
3140                  GLsizei width, GLsizei height, GLsizei depth,
3141                  GLint border, GLenum format, GLenum type,
3142                  GLsizei imageSize, const GLvoid *pixels)
3143{
3144   teximage(ctx, compressed, dims, target, level, internalFormat, width, height,
3145            depth, border, format, type, imageSize, pixels, true);
3146}
3147
3148
3149/*
3150 * Called from the API.  Note that width includes the border.
3151 */
3152void GLAPIENTRY
3153_mesa_TexImage1D( GLenum target, GLint level, GLint internalFormat,
3154                  GLsizei width, GLint border, GLenum format,
3155                  GLenum type, const GLvoid *pixels )
3156{
3157   GET_CURRENT_CONTEXT(ctx);
3158   teximage_err(ctx, GL_FALSE, 1, target, level, internalFormat, width, 1, 1,
3159                border, format, type, 0, pixels);
3160}
3161
3162
3163void GLAPIENTRY
3164_mesa_TexImage2D( GLenum target, GLint level, GLint internalFormat,
3165                  GLsizei width, GLsizei height, GLint border,
3166                  GLenum format, GLenum type,
3167                  const GLvoid *pixels )
3168{
3169   GET_CURRENT_CONTEXT(ctx);
3170   teximage_err(ctx, GL_FALSE, 2, target, level, internalFormat, width, height, 1,
3171                border, format, type, 0, pixels);
3172}
3173
3174
3175/*
3176 * Called by the API or display list executor.
3177 * Note that width and height include the border.
3178 */
3179void GLAPIENTRY
3180_mesa_TexImage3D( GLenum target, GLint level, GLint internalFormat,
3181                  GLsizei width, GLsizei height, GLsizei depth,
3182                  GLint border, GLenum format, GLenum type,
3183                  const GLvoid *pixels )
3184{
3185   GET_CURRENT_CONTEXT(ctx);
3186   teximage_err(ctx, GL_FALSE, 3, target, level, internalFormat,
3187                width, height, depth, border, format, type, 0, pixels);
3188}
3189
3190
3191void GLAPIENTRY
3192_mesa_TexImage3DEXT( GLenum target, GLint level, GLenum internalFormat,
3193                     GLsizei width, GLsizei height, GLsizei depth,
3194                     GLint border, GLenum format, GLenum type,
3195                     const GLvoid *pixels )
3196{
3197   _mesa_TexImage3D(target, level, (GLint) internalFormat, width, height,
3198                    depth, border, format, type, pixels);
3199}
3200
3201
3202void GLAPIENTRY
3203_mesa_TexImage1D_no_error(GLenum target, GLint level, GLint internalFormat,
3204                          GLsizei width, GLint border, GLenum format,
3205                          GLenum type, const GLvoid *pixels)
3206{
3207   GET_CURRENT_CONTEXT(ctx);
3208   teximage_no_error(ctx, GL_FALSE, 1, target, level, internalFormat, width, 1,
3209                     1, border, format, type, 0, pixels);
3210}
3211
3212
3213void GLAPIENTRY
3214_mesa_TexImage2D_no_error(GLenum target, GLint level, GLint internalFormat,
3215                          GLsizei width, GLsizei height, GLint border,
3216                          GLenum format, GLenum type, const GLvoid *pixels)
3217{
3218   GET_CURRENT_CONTEXT(ctx);
3219   teximage_no_error(ctx, GL_FALSE, 2, target, level, internalFormat, width,
3220                     height, 1, border, format, type, 0, pixels);
3221}
3222
3223
3224void GLAPIENTRY
3225_mesa_TexImage3D_no_error(GLenum target, GLint level, GLint internalFormat,
3226                          GLsizei width, GLsizei height, GLsizei depth,
3227                          GLint border, GLenum format, GLenum type,
3228                          const GLvoid *pixels )
3229{
3230   GET_CURRENT_CONTEXT(ctx);
3231   teximage_no_error(ctx, GL_FALSE, 3, target, level, internalFormat,
3232                     width, height, depth, border, format, type, 0, pixels);
3233}
3234
3235
3236void GLAPIENTRY
3237_mesa_EGLImageTargetTexture2DOES (GLenum target, GLeglImageOES image)
3238{
3239   struct gl_texture_object *texObj;
3240   struct gl_texture_image *texImage;
3241   bool valid_target;
3242   GET_CURRENT_CONTEXT(ctx);
3243   FLUSH_VERTICES(ctx, 0);
3244
3245   switch (target) {
3246   case GL_TEXTURE_2D:
3247      valid_target = ctx->Extensions.OES_EGL_image;
3248      break;
3249   case GL_TEXTURE_EXTERNAL_OES:
3250      valid_target =
3251         _mesa_is_gles(ctx) ? ctx->Extensions.OES_EGL_image_external : false;
3252      break;
3253   default:
3254      valid_target = false;
3255      break;
3256   }
3257
3258   if (!valid_target) {
3259      _mesa_error(ctx, GL_INVALID_ENUM,
3260                  "glEGLImageTargetTexture2D(target=%d)", target);
3261      return;
3262   }
3263
3264   if (!image) {
3265      _mesa_error(ctx, GL_INVALID_OPERATION,
3266                  "glEGLImageTargetTexture2D(image=%p)", image);
3267      return;
3268   }
3269
3270   if (ctx->NewState & _NEW_PIXEL)
3271      _mesa_update_state(ctx);
3272
3273   texObj = _mesa_get_current_tex_object(ctx, target);
3274   if (!texObj)
3275      return;
3276
3277   _mesa_lock_texture(ctx, texObj);
3278
3279   if (texObj->Immutable) {
3280      _mesa_error(ctx, GL_INVALID_OPERATION,
3281                  "glEGLImageTargetTexture2D(texture is immutable)");
3282      _mesa_unlock_texture(ctx, texObj);
3283      return;
3284   }
3285
3286   texImage = _mesa_get_tex_image(ctx, texObj, target, 0);
3287   if (!texImage) {
3288      _mesa_error(ctx, GL_OUT_OF_MEMORY, "glEGLImageTargetTexture2D");
3289   } else {
3290      ctx->Driver.FreeTextureImageBuffer(ctx, texImage);
3291
3292      ctx->Driver.EGLImageTargetTexture2D(ctx, target,
3293                                          texObj, texImage, image);
3294
3295      _mesa_dirty_texobj(ctx, texObj);
3296   }
3297   _mesa_unlock_texture(ctx, texObj);
3298}
3299
3300
3301/**
3302 * Helper that implements the glTexSubImage1/2/3D()
3303 * and glTextureSubImage1/2/3D() functions.
3304 */
3305static void
3306texture_sub_image(struct gl_context *ctx, GLuint dims,
3307                  struct gl_texture_object *texObj,
3308                  struct gl_texture_image *texImage,
3309                  GLenum target, GLint level,
3310                  GLint xoffset, GLint yoffset, GLint zoffset,
3311                  GLsizei width, GLsizei height, GLsizei depth,
3312                  GLenum format, GLenum type, const GLvoid *pixels)
3313{
3314   FLUSH_VERTICES(ctx, 0);
3315
3316   if (ctx->NewState & _NEW_PIXEL)
3317      _mesa_update_state(ctx);
3318
3319   _mesa_lock_texture(ctx, texObj);
3320   {
3321      if (width > 0 && height > 0 && depth > 0) {
3322         /* If we have a border, offset=-1 is legal.  Bias by border width. */
3323         switch (dims) {
3324         case 3:
3325            if (target != GL_TEXTURE_2D_ARRAY)
3326               zoffset += texImage->Border;
3327            /* fall-through */
3328         case 2:
3329            if (target != GL_TEXTURE_1D_ARRAY)
3330               yoffset += texImage->Border;
3331            /* fall-through */
3332         case 1:
3333            xoffset += texImage->Border;
3334         }
3335
3336         ctx->Driver.TexSubImage(ctx, dims, texImage,
3337                                 xoffset, yoffset, zoffset,
3338                                 width, height, depth,
3339                                 format, type, pixels, &ctx->Unpack);
3340
3341         check_gen_mipmap(ctx, target, texObj, level);
3342
3343         /* NOTE: Don't signal _NEW_TEXTURE_OBJECT since we've only changed
3344          * the texel data, not the texture format, size, etc.
3345          */
3346      }
3347   }
3348   _mesa_unlock_texture(ctx, texObj);
3349}
3350
3351/**
3352 * Implement all the glTexSubImage1/2/3D() functions.
3353 * Must split this out this way because of GL_TEXTURE_CUBE_MAP.
3354 */
3355static void
3356texsubimage_err(struct gl_context *ctx, GLuint dims, GLenum target, GLint level,
3357                GLint xoffset, GLint yoffset, GLint zoffset,
3358                GLsizei width, GLsizei height, GLsizei depth,
3359                GLenum format, GLenum type, const GLvoid *pixels,
3360                const char *callerName)
3361{
3362   struct gl_texture_object *texObj;
3363   struct gl_texture_image *texImage;
3364
3365   /* check target (proxies not allowed) */
3366   if (!legal_texsubimage_target(ctx, dims, target, false)) {
3367      _mesa_error(ctx, GL_INVALID_ENUM, "glTexSubImage%uD(target=%s)",
3368                  dims, _mesa_enum_to_string(target));
3369      return;
3370   }
3371
3372   texObj = _mesa_get_current_tex_object(ctx, target);
3373   if (!texObj)
3374      return;
3375
3376   if (texsubimage_error_check(ctx, dims, texObj, target, level,
3377                               xoffset, yoffset, zoffset,
3378                               width, height, depth, format, type,
3379                               pixels, callerName)) {
3380      return;   /* error was detected */
3381   }
3382
3383   texImage = _mesa_select_tex_image(texObj, target, level);
3384   /* texsubimage_error_check ensures that texImage is not NULL */
3385
3386   if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
3387      _mesa_debug(ctx, "glTexSubImage%uD %s %d %d %d %d %d %d %d %s %s %p\n",
3388                  dims,
3389                  _mesa_enum_to_string(target), level,
3390                  xoffset, yoffset, zoffset, width, height, depth,
3391                  _mesa_enum_to_string(format),
3392                  _mesa_enum_to_string(type), pixels);
3393
3394   texture_sub_image(ctx, dims, texObj, texImage, target, level,
3395                     xoffset, yoffset, zoffset, width, height, depth,
3396                     format, type, pixels);
3397}
3398
3399
3400static void
3401texsubimage(struct gl_context *ctx, GLuint dims, GLenum target, GLint level,
3402            GLint xoffset, GLint yoffset, GLint zoffset,
3403            GLsizei width, GLsizei height, GLsizei depth,
3404            GLenum format, GLenum type, const GLvoid *pixels)
3405{
3406   struct gl_texture_object *texObj;
3407   struct gl_texture_image *texImage;
3408
3409   texObj = _mesa_get_current_tex_object(ctx, target);
3410   texImage = _mesa_select_tex_image(texObj, target, level);
3411
3412   texture_sub_image(ctx, dims, texObj, texImage, target, level,
3413                     xoffset, yoffset, zoffset, width, height, depth,
3414                     format, type, pixels);
3415}
3416
3417
3418/**
3419 * Implement all the glTextureSubImage1/2/3D() functions.
3420 * Must split this out this way because of GL_TEXTURE_CUBE_MAP.
3421 */
3422static ALWAYS_INLINE void
3423texturesubimage(struct gl_context *ctx, GLuint dims,
3424                GLuint texture, GLint level,
3425                GLint xoffset, GLint yoffset, GLint zoffset,
3426                GLsizei width, GLsizei height, GLsizei depth,
3427                GLenum format, GLenum type, const GLvoid *pixels,
3428                const char *callerName, bool no_error)
3429{
3430   struct gl_texture_object *texObj;
3431   struct gl_texture_image *texImage;
3432   int i;
3433
3434   if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
3435      _mesa_debug(ctx,
3436                  "glTextureSubImage%uD %d %d %d %d %d %d %d %d %s %s %p\n",
3437                  dims, texture, level,
3438                  xoffset, yoffset, zoffset, width, height, depth,
3439                  _mesa_enum_to_string(format),
3440                  _mesa_enum_to_string(type), pixels);
3441
3442   /* Get the texture object by Name. */
3443   if (!no_error) {
3444      texObj = _mesa_lookup_texture_err(ctx, texture, callerName);
3445      if (!texObj)
3446         return;
3447   } else {
3448      texObj = _mesa_lookup_texture(ctx, texture);
3449   }
3450
3451   if (!no_error) {
3452      /* check target (proxies not allowed) */
3453      if (!legal_texsubimage_target(ctx, dims, texObj->Target, true)) {
3454         _mesa_error(ctx, GL_INVALID_ENUM, "%s(target=%s)",
3455                     callerName, _mesa_enum_to_string(texObj->Target));
3456         return;
3457      }
3458
3459      if (texsubimage_error_check(ctx, dims, texObj, texObj->Target, level,
3460                                  xoffset, yoffset, zoffset,
3461                                  width, height, depth, format, type,
3462                                  pixels, callerName)) {
3463         return;   /* error was detected */
3464      }
3465   }
3466
3467   /* Must handle special case GL_TEXTURE_CUBE_MAP. */
3468   if (texObj->Target == GL_TEXTURE_CUBE_MAP) {
3469      GLint imageStride;
3470
3471      /*
3472       * What do we do if the user created a texture with the following code
3473       * and then called this function with its handle?
3474       *
3475       *    GLuint tex;
3476       *    glCreateTextures(GL_TEXTURE_CUBE_MAP, 1, &tex);
3477       *    glBindTexture(GL_TEXTURE_CUBE_MAP, tex);
3478       *    glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, ...);
3479       *    glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, ...);
3480       *    glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, ...);
3481       *    // Note: GL_TEXTURE_CUBE_MAP_NEGATIVE_Y not set, or given the
3482       *    // wrong format, or given the wrong size, etc.
3483       *    glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, ...);
3484       *    glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, ...);
3485       *
3486       * A bug has been filed against the spec for this case.  In the
3487       * meantime, we will check for cube completeness.
3488       *
3489       * According to Section 8.17 Texture Completeness in the OpenGL 4.5
3490       * Core Profile spec (30.10.2014):
3491       *    "[A] cube map texture is cube complete if the
3492       *    following conditions all hold true: The [base level] texture
3493       *    images of each of the six cube map faces have identical, positive,
3494       *    and square dimensions. The [base level] images were each specified
3495       *    with the same internal format."
3496       *
3497       * It seems reasonable to check for cube completeness of an arbitrary
3498       * level here so that the image data has a consistent format and size.
3499       */
3500      if (!no_error && !_mesa_cube_level_complete(texObj, level)) {
3501         _mesa_error(ctx, GL_INVALID_OPERATION,
3502                     "glTextureSubImage%uD(cube map incomplete)",
3503                     dims);
3504         return;
3505      }
3506
3507      imageStride = _mesa_image_image_stride(&ctx->Unpack, width, height,
3508                                             format, type);
3509      /* Copy in each face. */
3510      for (i = zoffset; i < zoffset + depth; ++i) {
3511         texImage = texObj->Image[i][level];
3512         assert(texImage);
3513
3514         texture_sub_image(ctx, 3, texObj, texImage, texObj->Target,
3515                           level, xoffset, yoffset, 0,
3516                           width, height, 1, format,
3517                           type, pixels);
3518         pixels = (GLubyte *) pixels + imageStride;
3519      }
3520   }
3521   else {
3522      texImage = _mesa_select_tex_image(texObj, texObj->Target, level);
3523      assert(texImage);
3524
3525      texture_sub_image(ctx, dims, texObj, texImage, texObj->Target,
3526                        level, xoffset, yoffset, zoffset,
3527                        width, height, depth, format,
3528                        type, pixels);
3529   }
3530}
3531
3532
3533static void
3534texturesubimage_error(struct gl_context *ctx, GLuint dims,
3535                      GLuint texture, GLint level,
3536                      GLint xoffset, GLint yoffset, GLint zoffset,
3537                      GLsizei width, GLsizei height, GLsizei depth,
3538                      GLenum format, GLenum type, const GLvoid *pixels,
3539                      const char *callerName)
3540{
3541   texturesubimage(ctx, dims, texture, level, xoffset, yoffset, zoffset,
3542                   width, height, depth, format, type, pixels, callerName,
3543                   false);
3544}
3545
3546
3547static void
3548texturesubimage_no_error(struct gl_context *ctx, GLuint dims,
3549                         GLuint texture, GLint level,
3550                         GLint xoffset, GLint yoffset, GLint zoffset,
3551                         GLsizei width, GLsizei height, GLsizei depth,
3552                         GLenum format, GLenum type, const GLvoid *pixels,
3553                         const char *callerName)
3554{
3555   texturesubimage(ctx, dims, texture, level, xoffset, yoffset, zoffset,
3556                   width, height, depth, format, type, pixels, callerName,
3557                   true);
3558}
3559
3560
3561void GLAPIENTRY
3562_mesa_TexSubImage1D_no_error(GLenum target, GLint level,
3563                             GLint xoffset, GLsizei width,
3564                             GLenum format, GLenum type,
3565                             const GLvoid *pixels)
3566{
3567   GET_CURRENT_CONTEXT(ctx);
3568   texsubimage(ctx, 1, target, level,
3569               xoffset, 0, 0,
3570               width, 1, 1,
3571               format, type, pixels);
3572}
3573
3574
3575void GLAPIENTRY
3576_mesa_TexSubImage1D( GLenum target, GLint level,
3577                     GLint xoffset, GLsizei width,
3578                     GLenum format, GLenum type,
3579                     const GLvoid *pixels )
3580{
3581   GET_CURRENT_CONTEXT(ctx);
3582   texsubimage_err(ctx, 1, target, level,
3583                   xoffset, 0, 0,
3584                   width, 1, 1,
3585                   format, type, pixels, "glTexSubImage1D");
3586}
3587
3588
3589void GLAPIENTRY
3590_mesa_TexSubImage2D_no_error(GLenum target, GLint level,
3591                             GLint xoffset, GLint yoffset,
3592                             GLsizei width, GLsizei height,
3593                             GLenum format, GLenum type,
3594                             const GLvoid *pixels)
3595{
3596   GET_CURRENT_CONTEXT(ctx);
3597   texsubimage(ctx, 2, target, level,
3598               xoffset, yoffset, 0,
3599               width, height, 1,
3600               format, type, pixels);
3601}
3602
3603
3604void GLAPIENTRY
3605_mesa_TexSubImage2D( GLenum target, GLint level,
3606                     GLint xoffset, GLint yoffset,
3607                     GLsizei width, GLsizei height,
3608                     GLenum format, GLenum type,
3609                     const GLvoid *pixels )
3610{
3611   GET_CURRENT_CONTEXT(ctx);
3612   texsubimage_err(ctx, 2, target, level,
3613                   xoffset, yoffset, 0,
3614                   width, height, 1,
3615                   format, type, pixels, "glTexSubImage2D");
3616}
3617
3618
3619void GLAPIENTRY
3620_mesa_TexSubImage3D_no_error(GLenum target, GLint level,
3621                             GLint xoffset, GLint yoffset, GLint zoffset,
3622                             GLsizei width, GLsizei height, GLsizei depth,
3623                             GLenum format, GLenum type,
3624                             const GLvoid *pixels)
3625{
3626   GET_CURRENT_CONTEXT(ctx);
3627   texsubimage(ctx, 3, target, level,
3628               xoffset, yoffset, zoffset,
3629               width, height, depth,
3630               format, type, pixels);
3631}
3632
3633
3634void GLAPIENTRY
3635_mesa_TexSubImage3D( GLenum target, GLint level,
3636                     GLint xoffset, GLint yoffset, GLint zoffset,
3637                     GLsizei width, GLsizei height, GLsizei depth,
3638                     GLenum format, GLenum type,
3639                     const GLvoid *pixels )
3640{
3641   GET_CURRENT_CONTEXT(ctx);
3642   texsubimage_err(ctx, 3, target, level,
3643                   xoffset, yoffset, zoffset,
3644                   width, height, depth,
3645                   format, type, pixels, "glTexSubImage3D");
3646}
3647
3648
3649void GLAPIENTRY
3650_mesa_TextureSubImage1D_no_error(GLuint texture, GLint level, GLint xoffset,
3651                                 GLsizei width, GLenum format, GLenum type,
3652                                 const GLvoid *pixels)
3653{
3654   GET_CURRENT_CONTEXT(ctx);
3655   texturesubimage_no_error(ctx, 1, texture, level, xoffset, 0, 0, width, 1, 1,
3656                            format, type, pixels, "glTextureSubImage1D");
3657}
3658
3659
3660void GLAPIENTRY
3661_mesa_TextureSubImage1D(GLuint texture, GLint level,
3662                        GLint xoffset, GLsizei width,
3663                        GLenum format, GLenum type,
3664                        const GLvoid *pixels)
3665{
3666   GET_CURRENT_CONTEXT(ctx);
3667   texturesubimage_error(ctx, 1, texture, level, xoffset, 0, 0, width, 1, 1,
3668                         format, type, pixels, "glTextureSubImage1D");
3669}
3670
3671
3672void GLAPIENTRY
3673_mesa_TextureSubImage2D_no_error(GLuint texture, GLint level, GLint xoffset,
3674                                 GLint yoffset, GLsizei width, GLsizei height,
3675                                 GLenum format, GLenum type,
3676                                 const GLvoid *pixels)
3677{
3678   GET_CURRENT_CONTEXT(ctx);
3679   texturesubimage_no_error(ctx, 2, texture, level, xoffset, yoffset, 0, width,
3680                            height, 1, format, type, pixels,
3681                            "glTextureSubImage2D");
3682}
3683
3684
3685void GLAPIENTRY
3686_mesa_TextureSubImage2D(GLuint texture, GLint level,
3687                        GLint xoffset, GLint yoffset,
3688                        GLsizei width, GLsizei height,
3689                        GLenum format, GLenum type,
3690                        const GLvoid *pixels)
3691{
3692   GET_CURRENT_CONTEXT(ctx);
3693   texturesubimage_error(ctx, 2, texture, level, xoffset, yoffset, 0, width,
3694                         height, 1, format, type, pixels,
3695                         "glTextureSubImage2D");
3696}
3697
3698
3699void GLAPIENTRY
3700_mesa_TextureSubImage3D_no_error(GLuint texture, GLint level, GLint xoffset,
3701                                 GLint yoffset, GLint zoffset, GLsizei width,
3702                                 GLsizei height, GLsizei depth, GLenum format,
3703                                 GLenum type, const GLvoid *pixels)
3704{
3705   GET_CURRENT_CONTEXT(ctx);
3706   texturesubimage_no_error(ctx, 3, texture, level, xoffset, yoffset, zoffset,
3707                            width, height, depth, format, type, pixels,
3708                            "glTextureSubImage3D");
3709}
3710
3711
3712void GLAPIENTRY
3713_mesa_TextureSubImage3D(GLuint texture, GLint level,
3714                        GLint xoffset, GLint yoffset, GLint zoffset,
3715                        GLsizei width, GLsizei height, GLsizei depth,
3716                        GLenum format, GLenum type,
3717                        const GLvoid *pixels)
3718{
3719   GET_CURRENT_CONTEXT(ctx);
3720   texturesubimage_error(ctx, 3, texture, level, xoffset, yoffset, zoffset,
3721                         width, height, depth, format, type, pixels,
3722                         "glTextureSubImage3D");
3723}
3724
3725
3726/**
3727 * For glCopyTexSubImage, return the source renderbuffer to copy texel data
3728 * from.  This depends on whether the texture contains color or depth values.
3729 */
3730static struct gl_renderbuffer *
3731get_copy_tex_image_source(struct gl_context *ctx, mesa_format texFormat)
3732{
3733   if (_mesa_get_format_bits(texFormat, GL_DEPTH_BITS) > 0) {
3734      /* reading from depth/stencil buffer */
3735      return ctx->ReadBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;
3736   } else if (_mesa_get_format_bits(texFormat, GL_STENCIL_BITS) > 0) {
3737      return ctx->ReadBuffer->Attachment[BUFFER_STENCIL].Renderbuffer;
3738   } else {
3739      /* copying from color buffer */
3740      return ctx->ReadBuffer->_ColorReadBuffer;
3741   }
3742}
3743
3744
3745static void
3746copytexsubimage_by_slice(struct gl_context *ctx,
3747                         struct gl_texture_image *texImage,
3748                         GLuint dims,
3749                         GLint xoffset, GLint yoffset, GLint zoffset,
3750                         struct gl_renderbuffer *rb,
3751                         GLint x, GLint y,
3752                         GLsizei width, GLsizei height)
3753{
3754   if (texImage->TexObject->Target == GL_TEXTURE_1D_ARRAY) {
3755      int slice;
3756
3757      /* For 1D arrays, we copy each scanline of the source rectangle into the
3758       * next array slice.
3759       */
3760      assert(zoffset == 0);
3761
3762      for (slice = 0; slice < height; slice++) {
3763         assert(yoffset + slice < texImage->Height);
3764         ctx->Driver.CopyTexSubImage(ctx, 2, texImage,
3765                                     xoffset, 0, yoffset + slice,
3766                                     rb, x, y + slice, width, 1);
3767      }
3768   } else {
3769      ctx->Driver.CopyTexSubImage(ctx, dims, texImage,
3770                                  xoffset, yoffset, zoffset,
3771                                  rb, x, y, width, height);
3772   }
3773}
3774
3775
3776static GLboolean
3777formats_differ_in_component_sizes(mesa_format f1, mesa_format f2)
3778{
3779   GLint f1_r_bits = _mesa_get_format_bits(f1, GL_RED_BITS);
3780   GLint f1_g_bits = _mesa_get_format_bits(f1, GL_GREEN_BITS);
3781   GLint f1_b_bits = _mesa_get_format_bits(f1, GL_BLUE_BITS);
3782   GLint f1_a_bits = _mesa_get_format_bits(f1, GL_ALPHA_BITS);
3783
3784   GLint f2_r_bits = _mesa_get_format_bits(f2, GL_RED_BITS);
3785   GLint f2_g_bits = _mesa_get_format_bits(f2, GL_GREEN_BITS);
3786   GLint f2_b_bits = _mesa_get_format_bits(f2, GL_BLUE_BITS);
3787   GLint f2_a_bits = _mesa_get_format_bits(f2, GL_ALPHA_BITS);
3788
3789   if ((f1_r_bits && f2_r_bits && f1_r_bits != f2_r_bits)
3790       || (f1_g_bits && f2_g_bits && f1_g_bits != f2_g_bits)
3791       || (f1_b_bits && f2_b_bits && f1_b_bits != f2_b_bits)
3792       || (f1_a_bits && f2_a_bits && f1_a_bits != f2_a_bits))
3793      return GL_TRUE;
3794
3795   return GL_FALSE;
3796}
3797
3798
3799/**
3800 * Check if the given texture format and size arguments match those
3801 * of the texture image.
3802 * \param return true if arguments match, false otherwise.
3803 */
3804static bool
3805can_avoid_reallocation(const struct gl_texture_image *texImage,
3806                       GLenum internalFormat,
3807                       mesa_format texFormat, GLsizei width,
3808                       GLsizei height, GLint border)
3809{
3810   if (texImage->InternalFormat != internalFormat)
3811      return false;
3812   if (texImage->TexFormat != texFormat)
3813      return false;
3814   if (texImage->Border != border)
3815      return false;
3816   if (texImage->Width2 != width)
3817      return false;
3818   if (texImage->Height2 != height)
3819      return false;
3820   return true;
3821}
3822
3823
3824/**
3825 * Implementation for glCopyTex(ture)SubImage1/2/3D() functions.
3826 */
3827static void
3828copy_texture_sub_image(struct gl_context *ctx, GLuint dims,
3829                       struct gl_texture_object *texObj,
3830                       GLenum target, GLint level,
3831                       GLint xoffset, GLint yoffset, GLint zoffset,
3832                       GLint x, GLint y, GLsizei width, GLsizei height)
3833{
3834   struct gl_texture_image *texImage;
3835
3836   _mesa_lock_texture(ctx, texObj);
3837
3838   texImage = _mesa_select_tex_image(texObj, target, level);
3839
3840   /* If we have a border, offset=-1 is legal.  Bias by border width. */
3841   switch (dims) {
3842   case 3:
3843      if (target != GL_TEXTURE_2D_ARRAY)
3844         zoffset += texImage->Border;
3845      /* fall-through */
3846   case 2:
3847      if (target != GL_TEXTURE_1D_ARRAY)
3848         yoffset += texImage->Border;
3849      /* fall-through */
3850   case 1:
3851      xoffset += texImage->Border;
3852   }
3853
3854   if (_mesa_clip_copytexsubimage(ctx, &xoffset, &yoffset, &x, &y,
3855                                  &width, &height)) {
3856      struct gl_renderbuffer *srcRb =
3857         get_copy_tex_image_source(ctx, texImage->TexFormat);
3858
3859      copytexsubimage_by_slice(ctx, texImage, dims, xoffset, yoffset, zoffset,
3860                               srcRb, x, y, width, height);
3861
3862      check_gen_mipmap(ctx, target, texObj, level);
3863
3864      /* NOTE: Don't signal _NEW_TEXTURE_OBJECT since we've only changed
3865       * the texel data, not the texture format, size, etc.
3866       */
3867   }
3868
3869   _mesa_unlock_texture(ctx, texObj);
3870}
3871
3872
3873static void
3874copy_texture_sub_image_err(struct gl_context *ctx, GLuint dims,
3875                           struct gl_texture_object *texObj,
3876                           GLenum target, GLint level,
3877                           GLint xoffset, GLint yoffset, GLint zoffset,
3878                           GLint x, GLint y, GLsizei width, GLsizei height,
3879                           const char *caller)
3880{
3881   FLUSH_VERTICES(ctx, 0);
3882
3883   if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
3884      _mesa_debug(ctx, "%s %s %d %d %d %d %d %d %d %d\n", caller,
3885                  _mesa_enum_to_string(target),
3886                  level, xoffset, yoffset, zoffset, x, y, width, height);
3887
3888   if (ctx->NewState & NEW_COPY_TEX_STATE)
3889      _mesa_update_state(ctx);
3890
3891   if (copytexsubimage_error_check(ctx, dims, texObj, target, level,
3892                                   xoffset, yoffset, zoffset,
3893                                   width, height, caller)) {
3894      return;
3895   }
3896
3897   copy_texture_sub_image(ctx, dims, texObj, target, level, xoffset, yoffset,
3898                          zoffset, x, y, width, height);
3899}
3900
3901
3902static void
3903copy_texture_sub_image_no_error(struct gl_context *ctx, GLuint dims,
3904                                struct gl_texture_object *texObj,
3905                                GLenum target, GLint level,
3906                                GLint xoffset, GLint yoffset, GLint zoffset,
3907                                GLint x, GLint y, GLsizei width, GLsizei height)
3908{
3909   FLUSH_VERTICES(ctx, 0);
3910
3911   if (ctx->NewState & NEW_COPY_TEX_STATE)
3912      _mesa_update_state(ctx);
3913
3914   copy_texture_sub_image(ctx, dims, texObj, target, level, xoffset, yoffset,
3915                          zoffset, x, y, width, height);
3916}
3917
3918
3919/**
3920 * Implement the glCopyTexImage1/2D() functions.
3921 */
3922static ALWAYS_INLINE void
3923copyteximage(struct gl_context *ctx, GLuint dims,
3924             GLenum target, GLint level, GLenum internalFormat,
3925             GLint x, GLint y, GLsizei width, GLsizei height, GLint border,
3926             bool no_error)
3927{
3928   struct gl_texture_image *texImage;
3929   struct gl_texture_object *texObj;
3930   mesa_format texFormat;
3931
3932   FLUSH_VERTICES(ctx, 0);
3933
3934   if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
3935      _mesa_debug(ctx, "glCopyTexImage%uD %s %d %s %d %d %d %d %d\n",
3936                  dims,
3937                  _mesa_enum_to_string(target), level,
3938                  _mesa_enum_to_string(internalFormat),
3939                  x, y, width, height, border);
3940
3941   if (ctx->NewState & NEW_COPY_TEX_STATE)
3942      _mesa_update_state(ctx);
3943
3944   if (!no_error) {
3945      if (copytexture_error_check(ctx, dims, target, level, internalFormat,
3946                                  border))
3947         return;
3948
3949      if (!_mesa_legal_texture_dimensions(ctx, target, level, width, height,
3950                                          1, border)) {
3951         _mesa_error(ctx, GL_INVALID_VALUE,
3952                     "glCopyTexImage%uD(invalid width=%d or height=%d)",
3953                     dims, width, height);
3954         return;
3955      }
3956   }
3957
3958   texObj = _mesa_get_current_tex_object(ctx, target);
3959   assert(texObj);
3960
3961   texFormat = _mesa_choose_texture_format(ctx, texObj, target, level,
3962                                           internalFormat, GL_NONE, GL_NONE);
3963
3964   /* First check if reallocating the texture buffer can be avoided.
3965    * Without the realloc the copy can be 20x faster.
3966    */
3967   _mesa_lock_texture(ctx, texObj);
3968   {
3969      texImage = _mesa_select_tex_image(texObj, target, level);
3970      if (texImage && can_avoid_reallocation(texImage, internalFormat, texFormat,
3971                                             width, height, border)) {
3972         _mesa_unlock_texture(ctx, texObj);
3973         if (no_error) {
3974            copy_texture_sub_image_no_error(ctx, dims, texObj, target, level, 0,
3975                                            0, 0, x, y, width, height);
3976         } else {
3977            copy_texture_sub_image_err(ctx, dims, texObj, target, level, 0, 0,
3978                                       0, x, y, width, height,"CopyTexImage");
3979         }
3980         return;
3981      }
3982   }
3983   _mesa_unlock_texture(ctx, texObj);
3984   _mesa_perf_debug(ctx, MESA_DEBUG_SEVERITY_LOW, "glCopyTexImage "
3985                    "can't avoid reallocating texture storage\n");
3986
3987   if (!no_error && _mesa_is_gles3(ctx)) {
3988      struct gl_renderbuffer *rb =
3989         _mesa_get_read_renderbuffer_for_format(ctx, internalFormat);
3990
3991      if (_mesa_is_enum_format_unsized(internalFormat)) {
3992      /* Conversion from GL_RGB10_A2 source buffer format is not allowed in
3993       * OpenGL ES 3.0. Khronos bug# 9807.
3994       */
3995         if (rb->InternalFormat == GL_RGB10_A2) {
3996               _mesa_error(ctx, GL_INVALID_OPERATION,
3997                           "glCopyTexImage%uD(Reading from GL_RGB10_A2 buffer"
3998                           " and writing to unsized internal format)", dims);
3999               return;
4000         }
4001      }
4002      /* From Page 139 of OpenGL ES 3.0 spec:
4003       *    "If internalformat is sized, the internal format of the new texel
4004       *    array is internalformat, and this is also the new texel array’s
4005       *    effective internal format. If the component sizes of internalformat
4006       *    do not exactly match the corresponding component sizes of the source
4007       *    buffer’s effective internal format, described below, an
4008       *    INVALID_OPERATION error is generated. If internalformat is unsized,
4009       *    the internal format of the new texel array is the effective internal
4010       *    format of the source buffer, and this is also the new texel array’s
4011       *    effective internal format.
4012       */
4013      else if (formats_differ_in_component_sizes (texFormat, rb->Format)) {
4014            _mesa_error(ctx, GL_INVALID_OPERATION,
4015                        "glCopyTexImage%uD(component size changed in"
4016                        " internal format)", dims);
4017            return;
4018      }
4019   }
4020
4021   assert(texFormat != MESA_FORMAT_NONE);
4022
4023   if (!ctx->Driver.TestProxyTexImage(ctx, proxy_target(target),
4024                                      0, level, texFormat, 1,
4025                                      width, height, 1)) {
4026      _mesa_error(ctx, GL_OUT_OF_MEMORY,
4027                  "glCopyTexImage%uD(image too large)", dims);
4028      return;
4029   }
4030
4031   if (border && ctx->Const.StripTextureBorder) {
4032      x += border;
4033      width -= border * 2;
4034      if (dims == 2) {
4035         y += border;
4036         height -= border * 2;
4037      }
4038      border = 0;
4039   }
4040
4041   _mesa_lock_texture(ctx, texObj);
4042   {
4043      texImage = _mesa_get_tex_image(ctx, texObj, target, level);
4044
4045      if (!texImage) {
4046         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexImage%uD", dims);
4047      }
4048      else {
4049         GLint srcX = x, srcY = y, dstX = 0, dstY = 0, dstZ = 0;
4050         const GLuint face = _mesa_tex_target_to_face(target);
4051
4052         /* Free old texture image */
4053         ctx->Driver.FreeTextureImageBuffer(ctx, texImage);
4054
4055         _mesa_init_teximage_fields(ctx, texImage, width, height, 1,
4056                                    border, internalFormat, texFormat);
4057
4058         if (width && height) {
4059            /* Allocate texture memory (no pixel data yet) */
4060            ctx->Driver.AllocTextureImageBuffer(ctx, texImage);
4061
4062            if (_mesa_clip_copytexsubimage(ctx, &dstX, &dstY, &srcX, &srcY,
4063                                           &width, &height)) {
4064               struct gl_renderbuffer *srcRb =
4065                  get_copy_tex_image_source(ctx, texImage->TexFormat);
4066
4067               copytexsubimage_by_slice(ctx, texImage, dims,
4068                                        dstX, dstY, dstZ,
4069                                        srcRb, srcX, srcY, width, height);
4070            }
4071
4072            check_gen_mipmap(ctx, target, texObj, level);
4073         }
4074
4075         _mesa_update_fbo_texture(ctx, texObj, face, level);
4076
4077         _mesa_dirty_texobj(ctx, texObj);
4078      }
4079   }
4080   _mesa_unlock_texture(ctx, texObj);
4081}
4082
4083
4084static void
4085copyteximage_err(struct gl_context *ctx, GLuint dims, GLenum target,
4086                 GLint level, GLenum internalFormat, GLint x, GLint y,
4087                 GLsizei width, GLsizei height, GLint border)
4088{
4089   copyteximage(ctx, dims, target, level, internalFormat, x, y, width, height,
4090                border, false);
4091}
4092
4093
4094static void
4095copyteximage_no_error(struct gl_context *ctx, GLuint dims, GLenum target,
4096                      GLint level, GLenum internalFormat, GLint x, GLint y,
4097                      GLsizei width, GLsizei height, GLint border)
4098{
4099   copyteximage(ctx, dims, target, level, internalFormat, x, y, width, height,
4100                border, true);
4101}
4102
4103
4104void GLAPIENTRY
4105_mesa_CopyTexImage1D( GLenum target, GLint level,
4106                      GLenum internalFormat,
4107                      GLint x, GLint y,
4108                      GLsizei width, GLint border )
4109{
4110   GET_CURRENT_CONTEXT(ctx);
4111   copyteximage_err(ctx, 1, target, level, internalFormat, x, y, width, 1,
4112                    border);
4113}
4114
4115
4116void GLAPIENTRY
4117_mesa_CopyTexImage2D( GLenum target, GLint level, GLenum internalFormat,
4118                      GLint x, GLint y, GLsizei width, GLsizei height,
4119                      GLint border )
4120{
4121   GET_CURRENT_CONTEXT(ctx);
4122   copyteximage_err(ctx, 2, target, level, internalFormat,
4123                    x, y, width, height, border);
4124}
4125
4126
4127void GLAPIENTRY
4128_mesa_CopyTexImage1D_no_error(GLenum target, GLint level, GLenum internalFormat,
4129                              GLint x, GLint y, GLsizei width, GLint border)
4130{
4131   GET_CURRENT_CONTEXT(ctx);
4132   copyteximage_no_error(ctx, 1, target, level, internalFormat, x, y, width, 1,
4133                         border);
4134}
4135
4136
4137void GLAPIENTRY
4138_mesa_CopyTexImage2D_no_error(GLenum target, GLint level, GLenum internalFormat,
4139                              GLint x, GLint y, GLsizei width, GLsizei height,
4140                              GLint border)
4141{
4142   GET_CURRENT_CONTEXT(ctx);
4143   copyteximage_no_error(ctx, 2, target, level, internalFormat,
4144                         x, y, width, height, border);
4145}
4146
4147
4148void GLAPIENTRY
4149_mesa_CopyTexSubImage1D(GLenum target, GLint level,
4150                        GLint xoffset, GLint x, GLint y, GLsizei width)
4151{
4152   struct gl_texture_object* texObj;
4153   const char *self = "glCopyTexSubImage1D";
4154   GET_CURRENT_CONTEXT(ctx);
4155
4156   /* Check target (proxies not allowed). Target must be checked prior to
4157    * calling _mesa_get_current_tex_object.
4158    */
4159   if (!legal_texsubimage_target(ctx, 1, target, false)) {
4160      _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid target %s)", self,
4161                  _mesa_enum_to_string(target));
4162      return;
4163   }
4164
4165   texObj = _mesa_get_current_tex_object(ctx, target);
4166   if (!texObj)
4167      return;
4168
4169   copy_texture_sub_image_err(ctx, 1, texObj, target, level, xoffset, 0, 0,
4170                              x, y, width, 1, self);
4171}
4172
4173
4174void GLAPIENTRY
4175_mesa_CopyTexSubImage2D(GLenum target, GLint level,
4176                        GLint xoffset, GLint yoffset,
4177                        GLint x, GLint y, GLsizei width, GLsizei height)
4178{
4179   struct gl_texture_object* texObj;
4180   const char *self = "glCopyTexSubImage2D";
4181   GET_CURRENT_CONTEXT(ctx);
4182
4183   /* Check target (proxies not allowed). Target must be checked prior to
4184    * calling _mesa_get_current_tex_object.
4185    */
4186   if (!legal_texsubimage_target(ctx, 2, target, false)) {
4187      _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid target %s)", self,
4188                  _mesa_enum_to_string(target));
4189      return;
4190   }
4191
4192   texObj = _mesa_get_current_tex_object(ctx, target);
4193   if (!texObj)
4194      return;
4195
4196   copy_texture_sub_image_err(ctx, 2, texObj, target, level, xoffset, yoffset,
4197                              0, x, y, width, height, self);
4198}
4199
4200
4201void GLAPIENTRY
4202_mesa_CopyTexSubImage3D(GLenum target, GLint level,
4203                        GLint xoffset, GLint yoffset, GLint zoffset,
4204                        GLint x, GLint y, GLsizei width, GLsizei height)
4205{
4206   struct gl_texture_object* texObj;
4207   const char *self = "glCopyTexSubImage3D";
4208   GET_CURRENT_CONTEXT(ctx);
4209
4210   /* Check target (proxies not allowed). Target must be checked prior to
4211    * calling _mesa_get_current_tex_object.
4212    */
4213   if (!legal_texsubimage_target(ctx, 3, target, false)) {
4214      _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid target %s)", self,
4215                  _mesa_enum_to_string(target));
4216      return;
4217   }
4218
4219   texObj = _mesa_get_current_tex_object(ctx, target);
4220   if (!texObj)
4221      return;
4222
4223   copy_texture_sub_image_err(ctx, 3, texObj, target, level, xoffset, yoffset,
4224                              zoffset, x, y, width, height, self);
4225}
4226
4227
4228void GLAPIENTRY
4229_mesa_CopyTextureSubImage1D(GLuint texture, GLint level,
4230                            GLint xoffset, GLint x, GLint y, GLsizei width)
4231{
4232   struct gl_texture_object* texObj;
4233   const char *self = "glCopyTextureSubImage1D";
4234   GET_CURRENT_CONTEXT(ctx);
4235
4236   texObj = _mesa_lookup_texture_err(ctx, texture, self);
4237   if (!texObj)
4238      return;
4239
4240   /* Check target (proxies not allowed). */
4241   if (!legal_texsubimage_target(ctx, 1, texObj->Target, true)) {
4242      _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid target %s)", self,
4243                  _mesa_enum_to_string(texObj->Target));
4244      return;
4245   }
4246
4247   copy_texture_sub_image_err(ctx, 1, texObj, texObj->Target, level, xoffset, 0,
4248                              0, x, y, width, 1, self);
4249}
4250
4251
4252void GLAPIENTRY
4253_mesa_CopyTextureSubImage2D(GLuint texture, GLint level,
4254                            GLint xoffset, GLint yoffset,
4255                            GLint x, GLint y, GLsizei width, GLsizei height)
4256{
4257   struct gl_texture_object* texObj;
4258   const char *self = "glCopyTextureSubImage2D";
4259   GET_CURRENT_CONTEXT(ctx);
4260
4261   texObj = _mesa_lookup_texture_err(ctx, texture, self);
4262   if (!texObj)
4263      return;
4264
4265   /* Check target (proxies not allowed). */
4266   if (!legal_texsubimage_target(ctx, 2, texObj->Target, true)) {
4267      _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid target %s)", self,
4268                  _mesa_enum_to_string(texObj->Target));
4269      return;
4270   }
4271
4272   copy_texture_sub_image_err(ctx, 2, texObj, texObj->Target, level, xoffset,
4273                              yoffset, 0, x, y, width, height, self);
4274}
4275
4276
4277void GLAPIENTRY
4278_mesa_CopyTextureSubImage3D(GLuint texture, GLint level,
4279                            GLint xoffset, GLint yoffset, GLint zoffset,
4280                            GLint x, GLint y, GLsizei width, GLsizei height)
4281{
4282   struct gl_texture_object* texObj;
4283   const char *self = "glCopyTextureSubImage3D";
4284   GET_CURRENT_CONTEXT(ctx);
4285
4286   texObj = _mesa_lookup_texture_err(ctx, texture, self);
4287   if (!texObj)
4288      return;
4289
4290   /* Check target (proxies not allowed). */
4291   if (!legal_texsubimage_target(ctx, 3, texObj->Target, true)) {
4292      _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid target %s)", self,
4293                  _mesa_enum_to_string(texObj->Target));
4294      return;
4295   }
4296
4297   if (texObj->Target == GL_TEXTURE_CUBE_MAP) {
4298      /* Act like CopyTexSubImage2D */
4299      copy_texture_sub_image_err(ctx, 2, texObj,
4300                                GL_TEXTURE_CUBE_MAP_POSITIVE_X + zoffset,
4301                                level, xoffset, yoffset, 0, x, y, width, height,
4302                                self);
4303   }
4304   else
4305      copy_texture_sub_image_err(ctx, 3, texObj, texObj->Target, level, xoffset,
4306                                 yoffset, zoffset, x, y, width, height, self);
4307}
4308
4309
4310void GLAPIENTRY
4311_mesa_CopyTexSubImage1D_no_error(GLenum target, GLint level, GLint xoffset,
4312                                 GLint x, GLint y, GLsizei width)
4313{
4314   GET_CURRENT_CONTEXT(ctx);
4315
4316   struct gl_texture_object* texObj = _mesa_get_current_tex_object(ctx, target);
4317   copy_texture_sub_image_no_error(ctx, 1, texObj, target, level, xoffset, 0, 0,
4318                                   x, y, width, 1);
4319}
4320
4321
4322void GLAPIENTRY
4323_mesa_CopyTexSubImage2D_no_error(GLenum target, GLint level, GLint xoffset,
4324                                 GLint yoffset, GLint x, GLint y, GLsizei width,
4325                                 GLsizei height)
4326{
4327   GET_CURRENT_CONTEXT(ctx);
4328
4329   struct gl_texture_object* texObj = _mesa_get_current_tex_object(ctx, target);
4330   copy_texture_sub_image_no_error(ctx, 2, texObj, target, level, xoffset,
4331                                   yoffset, 0, x, y, width, height);
4332}
4333
4334
4335void GLAPIENTRY
4336_mesa_CopyTexSubImage3D_no_error(GLenum target, GLint level, GLint xoffset,
4337                                 GLint yoffset, GLint zoffset, GLint x, GLint y,
4338                                 GLsizei width, GLsizei height)
4339{
4340   GET_CURRENT_CONTEXT(ctx);
4341
4342   struct gl_texture_object* texObj = _mesa_get_current_tex_object(ctx, target);
4343   copy_texture_sub_image_no_error(ctx, 3, texObj, target, level, xoffset,
4344                                   yoffset, zoffset, x, y, width, height);
4345}
4346
4347
4348void GLAPIENTRY
4349_mesa_CopyTextureSubImage1D_no_error(GLuint texture, GLint level, GLint xoffset,
4350                                     GLint x, GLint y, GLsizei width)
4351{
4352   GET_CURRENT_CONTEXT(ctx);
4353
4354   struct gl_texture_object* texObj = _mesa_lookup_texture(ctx, texture);
4355   copy_texture_sub_image_no_error(ctx, 1, texObj, texObj->Target, level,
4356                                   xoffset, 0, 0, x, y, width, 1);
4357}
4358
4359
4360void GLAPIENTRY
4361_mesa_CopyTextureSubImage2D_no_error(GLuint texture, GLint level, GLint xoffset,
4362                                     GLint yoffset, GLint x, GLint y,
4363                                     GLsizei width, GLsizei height)
4364{
4365   GET_CURRENT_CONTEXT(ctx);
4366
4367   struct gl_texture_object* texObj = _mesa_lookup_texture(ctx, texture);
4368   copy_texture_sub_image_no_error(ctx, 2, texObj, texObj->Target, level,
4369                                   xoffset, yoffset, 0, x, y, width, height);
4370}
4371
4372
4373void GLAPIENTRY
4374_mesa_CopyTextureSubImage3D_no_error(GLuint texture, GLint level, GLint xoffset,
4375                                     GLint yoffset, GLint zoffset, GLint x,
4376                                     GLint y, GLsizei width, GLsizei height)
4377{
4378   GET_CURRENT_CONTEXT(ctx);
4379
4380   struct gl_texture_object* texObj = _mesa_lookup_texture(ctx, texture);
4381   if (texObj->Target == GL_TEXTURE_CUBE_MAP) {
4382      /* Act like CopyTexSubImage2D */
4383      copy_texture_sub_image_no_error(ctx, 2, texObj,
4384                                      GL_TEXTURE_CUBE_MAP_POSITIVE_X + zoffset,
4385                                      level, xoffset, yoffset, 0, x, y, width,
4386                                      height);
4387   }
4388   else
4389      copy_texture_sub_image_no_error(ctx, 3, texObj, texObj->Target, level,
4390                                      xoffset, yoffset, zoffset, x, y, width,
4391                                      height);
4392}
4393
4394
4395static bool
4396check_clear_tex_image(struct gl_context *ctx,
4397                      const char *function,
4398                      struct gl_texture_image *texImage,
4399                      GLenum format, GLenum type,
4400                      const void *data,
4401                      GLubyte *clearValue)
4402{
4403   struct gl_texture_object *texObj = texImage->TexObject;
4404   static const GLubyte zeroData[MAX_PIXEL_BYTES];
4405   GLenum internalFormat = texImage->InternalFormat;
4406   GLenum err;
4407
4408   if (texObj->Target == GL_TEXTURE_BUFFER) {
4409      _mesa_error(ctx, GL_INVALID_OPERATION,
4410                  "%s(buffer texture)", function);
4411      return false;
4412   }
4413
4414   if (_mesa_is_compressed_format(ctx, internalFormat)) {
4415      _mesa_error(ctx, GL_INVALID_OPERATION,
4416                  "%s(compressed texture)", function);
4417      return false;
4418   }
4419
4420   err = _mesa_error_check_format_and_type(ctx, format, type);
4421   if (err != GL_NO_ERROR) {
4422      _mesa_error(ctx, err,
4423                  "%s(incompatible format = %s, type = %s)",
4424                  function,
4425                  _mesa_enum_to_string(format),
4426                  _mesa_enum_to_string(type));
4427      return false;
4428   }
4429
4430   /* make sure internal format and format basically agree */
4431   if (!texture_formats_agree(internalFormat, format)) {
4432      _mesa_error(ctx, GL_INVALID_OPERATION,
4433                  "%s(incompatible internalFormat = %s, format = %s)",
4434                  function,
4435                  _mesa_enum_to_string(internalFormat),
4436                  _mesa_enum_to_string(format));
4437      return false;
4438   }
4439
4440   if (ctx->Version >= 30 || ctx->Extensions.EXT_texture_integer) {
4441      /* both source and dest must be integer-valued, or neither */
4442      if (_mesa_is_format_integer_color(texImage->TexFormat) !=
4443          _mesa_is_enum_format_integer(format)) {
4444         _mesa_error(ctx, GL_INVALID_OPERATION,
4445                     "%s(integer/non-integer format mismatch)",
4446                     function);
4447         return false;
4448      }
4449   }
4450
4451   if (!_mesa_texstore(ctx,
4452                       1, /* dims */
4453                       texImage->_BaseFormat,
4454                       texImage->TexFormat,
4455                       0, /* dstRowStride */
4456                       &clearValue,
4457                       1, 1, 1, /* srcWidth/Height/Depth */
4458                       format, type,
4459                       data ? data : zeroData,
4460                       &ctx->DefaultPacking)) {
4461      _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid format)", function);
4462      return false;
4463   }
4464
4465   return true;
4466}
4467
4468
4469static struct gl_texture_object *
4470get_tex_obj_for_clear(struct gl_context *ctx,
4471                      const char *function,
4472                      GLuint texture)
4473{
4474   struct gl_texture_object *texObj;
4475
4476   texObj = _mesa_lookup_texture_err(ctx, texture, function);
4477   if (!texObj)
4478      return NULL;
4479
4480   if (texObj->Target == 0) {
4481      _mesa_error(ctx, GL_INVALID_OPERATION, "%s(unbound tex)", function);
4482      return NULL;
4483   }
4484
4485   return texObj;
4486}
4487
4488
4489/**
4490 * For clearing cube textures, the zoffset and depth parameters indicate
4491 * which cube map faces are to be cleared.  This is the one case where we
4492 * need to be concerned with multiple gl_texture_images.  This function
4493 * returns the array of texture images to clear for cube maps, or one
4494 * texture image otherwise.
4495 * \return number of texture images, 0 for error, 6 for cube, 1 otherwise.
4496 */
4497static int
4498get_tex_images_for_clear(struct gl_context *ctx,
4499                         const char *function,
4500                         struct gl_texture_object *texObj,
4501                         GLint level,
4502                         struct gl_texture_image **texImages)
4503{
4504   GLenum target;
4505   int numFaces, i;
4506
4507   if (level < 0 || level >= MAX_TEXTURE_LEVELS) {
4508      _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid level)", function);
4509      return 0;
4510   }
4511
4512   if (texObj->Target == GL_TEXTURE_CUBE_MAP) {
4513      target = GL_TEXTURE_CUBE_MAP_POSITIVE_X;
4514      numFaces = MAX_FACES;
4515   }
4516   else {
4517      target = texObj->Target;
4518      numFaces = 1;
4519   }
4520
4521   for (i = 0; i < numFaces; i++) {
4522      texImages[i] = _mesa_select_tex_image(texObj, target + i, level);
4523      if (texImages[i] == NULL) {
4524         _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid level)", function);
4525         return 0;
4526      }
4527   }
4528
4529   return numFaces;
4530}
4531
4532
4533void GLAPIENTRY
4534_mesa_ClearTexSubImage(GLuint texture, GLint level,
4535                       GLint xoffset, GLint yoffset, GLint zoffset,
4536                       GLsizei width, GLsizei height, GLsizei depth,
4537                       GLenum format, GLenum type, const void *data)
4538{
4539   GET_CURRENT_CONTEXT(ctx);
4540   struct gl_texture_object *texObj;
4541   struct gl_texture_image *texImages[MAX_FACES];
4542   GLubyte clearValue[MAX_FACES][MAX_PIXEL_BYTES];
4543   int i, numImages;
4544   int minDepth, maxDepth;
4545
4546   texObj = get_tex_obj_for_clear(ctx, "glClearTexSubImage", texture);
4547
4548   if (texObj == NULL)
4549      return;
4550
4551   _mesa_lock_texture(ctx, texObj);
4552
4553   numImages = get_tex_images_for_clear(ctx, "glClearTexSubImage",
4554                                        texObj, level, texImages);
4555   if (numImages == 0)
4556      goto out;
4557
4558   if (numImages == 1) {
4559      minDepth = -(int) texImages[0]->Border;
4560      maxDepth = texImages[0]->Depth;
4561   } else {
4562      assert(numImages == MAX_FACES);
4563      minDepth = 0;
4564      maxDepth = numImages;
4565   }
4566
4567   if (xoffset < -(GLint) texImages[0]->Border ||
4568       yoffset < -(GLint) texImages[0]->Border ||
4569       zoffset < minDepth ||
4570       width < 0 ||
4571       height < 0 ||
4572       depth < 0 ||
4573       xoffset + width > texImages[0]->Width ||
4574       yoffset + height > texImages[0]->Height ||
4575       zoffset + depth > maxDepth) {
4576      _mesa_error(ctx, GL_INVALID_OPERATION,
4577                  "glClearSubTexImage(invalid dimensions)");
4578      goto out;
4579   }
4580
4581   if (numImages == 1) {
4582      if (check_clear_tex_image(ctx, "glClearTexSubImage", texImages[0],
4583                                format, type, data, clearValue[0])) {
4584         ctx->Driver.ClearTexSubImage(ctx,
4585                                      texImages[0],
4586                                      xoffset, yoffset, zoffset,
4587                                      width, height, depth,
4588                                      data ? clearValue[0] : NULL);
4589      }
4590   } else {
4591      /* loop over cube face images */
4592      for (i = zoffset; i < zoffset + depth; i++) {
4593         assert(i < MAX_FACES);
4594         if (!check_clear_tex_image(ctx, "glClearTexSubImage", texImages[i],
4595                                    format, type, data, clearValue[i]))
4596            goto out;
4597      }
4598      for (i = zoffset; i < zoffset + depth; i++) {
4599         ctx->Driver.ClearTexSubImage(ctx,
4600                                      texImages[i],
4601                                      xoffset, yoffset, 0,
4602                                      width, height, 1,
4603                                      data ? clearValue[i] : NULL);
4604      }
4605   }
4606
4607 out:
4608   _mesa_unlock_texture(ctx, texObj);
4609}
4610
4611
4612void GLAPIENTRY
4613_mesa_ClearTexImage( GLuint texture, GLint level,
4614                     GLenum format, GLenum type, const void *data )
4615{
4616   GET_CURRENT_CONTEXT(ctx);
4617   struct gl_texture_object *texObj;
4618   struct gl_texture_image *texImages[MAX_FACES];
4619   GLubyte clearValue[MAX_FACES][MAX_PIXEL_BYTES];
4620   int i, numImages;
4621
4622   texObj = get_tex_obj_for_clear(ctx, "glClearTexImage", texture);
4623
4624   if (texObj == NULL)
4625      return;
4626
4627   _mesa_lock_texture(ctx, texObj);
4628
4629   numImages = get_tex_images_for_clear(ctx, "glClearTexImage",
4630                                        texObj, level, texImages);
4631
4632   for (i = 0; i < numImages; i++) {
4633      if (!check_clear_tex_image(ctx, "glClearTexImage", texImages[i], format,
4634                                 type, data, clearValue[i]))
4635         goto out;
4636   }
4637
4638   for (i = 0; i < numImages; i++) {
4639      ctx->Driver.ClearTexSubImage(ctx, texImages[i],
4640                                   -(GLint) texImages[i]->Border, /* xoffset */
4641                                   -(GLint) texImages[i]->Border, /* yoffset */
4642                                   -(GLint) texImages[i]->Border, /* zoffset */
4643                                   texImages[i]->Width,
4644                                   texImages[i]->Height,
4645                                   texImages[i]->Depth,
4646                                   data ? clearValue[i] : NULL);
4647   }
4648
4649out:
4650   _mesa_unlock_texture(ctx, texObj);
4651}
4652
4653
4654
4655
4656/**********************************************************************/
4657/******                   Compressed Textures                    ******/
4658/**********************************************************************/
4659
4660
4661/**
4662 * Target checking for glCompressedTexSubImage[123]D().
4663 * \return GL_TRUE if error, GL_FALSE if no error
4664 * Must come before other error checking so that the texture object can
4665 * be correctly retrieved using _mesa_get_current_tex_object.
4666 */
4667static GLboolean
4668compressed_subtexture_target_check(struct gl_context *ctx, GLenum target,
4669                                   GLint dims, GLenum intFormat, bool dsa,
4670                                   const char *caller)
4671{
4672   GLboolean targetOK;
4673   mesa_format format;
4674   enum mesa_format_layout layout;
4675
4676   if (dsa && target == GL_TEXTURE_RECTANGLE) {
4677      _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid target %s)", caller,
4678                  _mesa_enum_to_string(target));
4679      return GL_TRUE;
4680   }
4681
4682   switch (dims) {
4683   case 2:
4684      switch (target) {
4685      case GL_TEXTURE_2D:
4686         targetOK = GL_TRUE;
4687         break;
4688      case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
4689      case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
4690      case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
4691      case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
4692      case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
4693      case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
4694         targetOK = ctx->Extensions.ARB_texture_cube_map;
4695         break;
4696      default:
4697         targetOK = GL_FALSE;
4698         break;
4699      }
4700      break;
4701   case 3:
4702      switch (target) {
4703      case GL_TEXTURE_CUBE_MAP:
4704         targetOK = dsa && ctx->Extensions.ARB_texture_cube_map;
4705         break;
4706      case GL_TEXTURE_2D_ARRAY:
4707         targetOK = _mesa_is_gles3(ctx) ||
4708            (_mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_array);
4709         break;
4710      case GL_TEXTURE_CUBE_MAP_ARRAY:
4711         targetOK = _mesa_has_texture_cube_map_array(ctx);
4712         break;
4713      case GL_TEXTURE_3D:
4714         targetOK = GL_TRUE;
4715         /*
4716          * OpenGL 4.5 spec (30.10.2014) says in Section 8.7 Compressed Texture
4717          * Images:
4718          *    "An INVALID_OPERATION error is generated by
4719          *    CompressedTex*SubImage3D if the internal format of the texture
4720          *    is one of the EAC, ETC2, or RGTC formats and either border is
4721          *    non-zero, or the effective target for the texture is not
4722          *    TEXTURE_2D_ARRAY."
4723          *
4724          * NOTE: that's probably a spec error.  It should probably say
4725          *    "... or the effective target for the texture is not
4726          *    TEXTURE_2D_ARRAY, TEXTURE_CUBE_MAP, nor
4727          *    GL_TEXTURE_CUBE_MAP_ARRAY."
4728          * since those targets are 2D images and they support all compression
4729          * formats.
4730          *
4731          * Instead of listing all these, just list those which are allowed,
4732          * which is (at this time) only bptc. Otherwise we'd say s3tc (and
4733          * more) are valid here, which they are not, but of course not
4734          * mentioned by core spec.
4735          *
4736          * Also, from GL_KHR_texture_compression_astc_{hdr,ldr}:
4737          *
4738          *    "Add a second new column "3D Tex." which is empty for all non-ASTC
4739          *     formats. If only the LDR profile is supported by the implementation,
4740          *     this column is also empty for all ASTC formats. If both the LDR and HDR
4741          *     profiles are supported, this column is checked for all ASTC formats."
4742          *
4743          *    "An INVALID_OPERATION error is generated by CompressedTexSubImage3D if
4744          *     <format> is one of the formats in table 8.19 and <target> is not
4745          *     TEXTURE_2D_ARRAY, TEXTURE_CUBE_MAP_ARRAY, or TEXTURE_3D.
4746          *
4747          *     An INVALID_OPERATION error is generated by CompressedTexSubImage3D if
4748          *     <format> is TEXTURE_CUBE_MAP_ARRAY and the "Cube Map Array" column of
4749          *     table 8.19 is *not* checked, or if <format> is TEXTURE_3D and the "3D
4750          *     Tex." column of table 8.19 is *not* checked"
4751          *
4752          * And from GL_KHR_texture_compression_astc_sliced_3d:
4753          *
4754          *    "Modify the "3D Tex." column to be checked for all ASTC formats."
4755          */
4756         format = _mesa_glenum_to_compressed_format(intFormat);
4757         layout = _mesa_get_format_layout(format);
4758         switch (layout) {
4759         case MESA_FORMAT_LAYOUT_BPTC:
4760            /* valid format */
4761            break;
4762         case MESA_FORMAT_LAYOUT_ASTC:
4763            targetOK =
4764               ctx->Extensions.KHR_texture_compression_astc_hdr ||
4765               ctx->Extensions.KHR_texture_compression_astc_sliced_3d;
4766            break;
4767         default:
4768            /* invalid format */
4769            _mesa_error(ctx, GL_INVALID_OPERATION,
4770                        "%s(invalid target %s for format %s)", caller,
4771                        _mesa_enum_to_string(target),
4772                        _mesa_enum_to_string(intFormat));
4773            return GL_TRUE;
4774         }
4775         break;
4776      default:
4777         targetOK = GL_FALSE;
4778      }
4779
4780      break;
4781   default:
4782      assert(dims == 1);
4783      /* no 1D compressed textures at this time */
4784      targetOK = GL_FALSE;
4785      break;
4786   }
4787
4788   if (!targetOK) {
4789      _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid target %s)", caller,
4790                  _mesa_enum_to_string(target));
4791      return GL_TRUE;
4792   }
4793
4794   return GL_FALSE;
4795}
4796
4797/**
4798 * Error checking for glCompressedTexSubImage[123]D().
4799 * \return GL_TRUE if error, GL_FALSE if no error
4800 */
4801static GLboolean
4802compressed_subtexture_error_check(struct gl_context *ctx, GLint dims,
4803                                  const struct gl_texture_object *texObj,
4804                                  GLenum target, GLint level,
4805                                  GLint xoffset, GLint yoffset, GLint zoffset,
4806                                  GLsizei width, GLsizei height, GLsizei depth,
4807                                  GLenum format, GLsizei imageSize,
4808                                  const GLvoid *data, const char *callerName)
4809{
4810   struct gl_texture_image *texImage;
4811   GLint expectedSize;
4812
4813   /* this will catch any invalid compressed format token */
4814   if (!_mesa_is_compressed_format(ctx, format)) {
4815      _mesa_error(ctx, GL_INVALID_ENUM, "%s(format)", callerName);
4816      return GL_TRUE;
4817   }
4818
4819   if (level < 0 || level >= _mesa_max_texture_levels(ctx, target)) {
4820      _mesa_error(ctx, GL_INVALID_VALUE, "%s(level=%d)", callerName, level);
4821      return GL_TRUE;
4822   }
4823
4824   /* validate the bound PBO, if any */
4825   if (!_mesa_validate_pbo_source_compressed(ctx, dims, &ctx->Unpack,
4826                                     imageSize, data, callerName)) {
4827      return GL_TRUE;
4828   }
4829
4830   /* Check for invalid pixel storage modes */
4831   if (!_mesa_compressed_pixel_storage_error_check(ctx, dims,
4832                                                   &ctx->Unpack, callerName)) {
4833      return GL_TRUE;
4834   }
4835
4836   expectedSize = compressed_tex_size(width, height, depth, format);
4837   if (expectedSize != imageSize) {
4838      _mesa_error(ctx, GL_INVALID_VALUE, "%s(size=%d)", callerName, imageSize);
4839      return GL_TRUE;
4840   }
4841
4842   texImage = _mesa_select_tex_image(texObj, target, level);
4843   if (!texImage) {
4844      _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid texture level %d)",
4845                  callerName, level);
4846      return GL_TRUE;
4847   }
4848
4849   if ((GLint) format != texImage->InternalFormat) {
4850      _mesa_error(ctx, GL_INVALID_OPERATION, "%s(format=%s)",
4851                  callerName, _mesa_enum_to_string(format));
4852      return GL_TRUE;
4853   }
4854
4855   if (compressedteximage_only_format(format)) {
4856      _mesa_error(ctx, GL_INVALID_OPERATION, "%s(format=%s cannot be updated)",
4857                  callerName, _mesa_enum_to_string(format));
4858      return GL_TRUE;
4859   }
4860
4861   if (error_check_subtexture_negative_dimensions(ctx, dims, width, height,
4862                                                  depth, callerName)) {
4863      return GL_TRUE;
4864   }
4865
4866   if (error_check_subtexture_dimensions(ctx, dims, texImage, xoffset, yoffset,
4867                                         zoffset, width, height, depth,
4868                                         callerName)) {
4869      return GL_TRUE;
4870   }
4871
4872   return GL_FALSE;
4873}
4874
4875
4876void GLAPIENTRY
4877_mesa_CompressedTexImage1D(GLenum target, GLint level,
4878                              GLenum internalFormat, GLsizei width,
4879                              GLint border, GLsizei imageSize,
4880                              const GLvoid *data)
4881{
4882   GET_CURRENT_CONTEXT(ctx);
4883   teximage_err(ctx, GL_TRUE, 1, target, level, internalFormat,
4884                width, 1, 1, border, GL_NONE, GL_NONE, imageSize, data);
4885}
4886
4887
4888void GLAPIENTRY
4889_mesa_CompressedTexImage2D(GLenum target, GLint level,
4890                              GLenum internalFormat, GLsizei width,
4891                              GLsizei height, GLint border, GLsizei imageSize,
4892                              const GLvoid *data)
4893{
4894   GET_CURRENT_CONTEXT(ctx);
4895   teximage_err(ctx, GL_TRUE, 2, target, level, internalFormat,
4896                width, height, 1, border, GL_NONE, GL_NONE, imageSize, data);
4897}
4898
4899
4900void GLAPIENTRY
4901_mesa_CompressedTexImage3D(GLenum target, GLint level,
4902                              GLenum internalFormat, GLsizei width,
4903                              GLsizei height, GLsizei depth, GLint border,
4904                              GLsizei imageSize, const GLvoid *data)
4905{
4906   GET_CURRENT_CONTEXT(ctx);
4907   teximage_err(ctx, GL_TRUE, 3, target, level, internalFormat, width, height,
4908                depth, border, GL_NONE, GL_NONE, imageSize, data);
4909}
4910
4911
4912void GLAPIENTRY
4913_mesa_CompressedTexImage1D_no_error(GLenum target, GLint level,
4914                                    GLenum internalFormat, GLsizei width,
4915                                    GLint border, GLsizei imageSize,
4916                                    const GLvoid *data)
4917{
4918   GET_CURRENT_CONTEXT(ctx);
4919   teximage_no_error(ctx, GL_TRUE, 1, target, level, internalFormat, width, 1,
4920                     1, border, GL_NONE, GL_NONE, imageSize, data);
4921}
4922
4923
4924void GLAPIENTRY
4925_mesa_CompressedTexImage2D_no_error(GLenum target, GLint level,
4926                                    GLenum internalFormat, GLsizei width,
4927                                    GLsizei height, GLint border,
4928                                    GLsizei imageSize, const GLvoid *data)
4929{
4930   GET_CURRENT_CONTEXT(ctx);
4931   teximage_no_error(ctx, GL_TRUE, 2, target, level, internalFormat, width,
4932                     height, 1, border, GL_NONE, GL_NONE, imageSize, data);
4933}
4934
4935
4936void GLAPIENTRY
4937_mesa_CompressedTexImage3D_no_error(GLenum target, GLint level,
4938                                    GLenum internalFormat, GLsizei width,
4939                                    GLsizei height, GLsizei depth, GLint border,
4940                                    GLsizei imageSize, const GLvoid *data)
4941{
4942   GET_CURRENT_CONTEXT(ctx);
4943   teximage_no_error(ctx, GL_TRUE, 3, target, level, internalFormat, width,
4944                     height, depth, border, GL_NONE, GL_NONE, imageSize, data);
4945}
4946
4947
4948/**
4949 * Common helper for glCompressedTexSubImage1/2/3D() and
4950 * glCompressedTextureSubImage1/2/3D().
4951 */
4952static void
4953compressed_texture_sub_image(struct gl_context *ctx, GLuint dims,
4954                             struct gl_texture_object *texObj,
4955                             struct gl_texture_image *texImage,
4956                             GLenum target, GLint level, GLint xoffset,
4957                             GLint yoffset, GLint zoffset, GLsizei width,
4958                             GLsizei height, GLsizei depth, GLenum format,
4959                             GLsizei imageSize, const GLvoid *data)
4960{
4961   FLUSH_VERTICES(ctx, 0);
4962
4963   _mesa_lock_texture(ctx, texObj);
4964   {
4965      if (width > 0 && height > 0 && depth > 0) {
4966         ctx->Driver.CompressedTexSubImage(ctx, dims, texImage,
4967                                           xoffset, yoffset, zoffset,
4968                                           width, height, depth,
4969                                           format, imageSize, data);
4970
4971         check_gen_mipmap(ctx, target, texObj, level);
4972
4973         /* NOTE: Don't signal _NEW_TEXTURE_OBJECT since we've only changed
4974          * the texel data, not the texture format, size, etc.
4975          */
4976      }
4977   }
4978   _mesa_unlock_texture(ctx, texObj);
4979}
4980
4981
4982static ALWAYS_INLINE void
4983compressed_tex_sub_image(unsigned dim, GLenum target, GLuint texture,
4984                         GLint level, GLint xoffset, GLint yoffset,
4985                         GLint zoffset, GLsizei width, GLsizei height,
4986                         GLsizei depth, GLenum format, GLsizei imageSize,
4987                         const GLvoid *data, bool dsa, bool no_error,
4988                         const char *caller)
4989{
4990   struct gl_texture_object *texObj = NULL;
4991   struct gl_texture_image *texImage;
4992
4993   GET_CURRENT_CONTEXT(ctx);
4994
4995   if (dsa) {
4996      if (no_error) {
4997         texObj = _mesa_lookup_texture(ctx, texture);
4998      } else {
4999         texObj = _mesa_lookup_texture_err(ctx, texture, caller);
5000         if (!texObj)
5001            return;
5002      }
5003
5004      target = texObj->Target;
5005   }
5006
5007   if (!no_error &&
5008       compressed_subtexture_target_check(ctx, target, dim, format, dsa,
5009                                          caller)) {
5010      return;
5011   }
5012
5013   if (!dsa) {
5014      texObj = _mesa_get_current_tex_object(ctx, target);
5015         if (!no_error && !texObj)
5016            return;
5017   }
5018
5019   if (!no_error &&
5020       compressed_subtexture_error_check(ctx, dim, texObj, target, level,
5021                                         xoffset, yoffset, zoffset, width,
5022                                         height, depth, format,
5023                                         imageSize, data, caller)) {
5024      return;
5025   }
5026
5027   /* Must handle special case GL_TEXTURE_CUBE_MAP. */
5028   if (dim == 3 && dsa && texObj->Target == GL_TEXTURE_CUBE_MAP) {
5029      const char *pixels = data;
5030      GLint image_stride;
5031
5032      /* Make sure the texture object is a proper cube.
5033       * (See texturesubimage in teximage.c for details on why this check is
5034       * performed.)
5035       */
5036      if (!no_error && !_mesa_cube_level_complete(texObj, level)) {
5037         _mesa_error(ctx, GL_INVALID_OPERATION,
5038                     "glCompressedTextureSubImage3D(cube map incomplete)");
5039         return;
5040      }
5041
5042      /* Copy in each face. */
5043      for (int i = zoffset; i < zoffset + depth; ++i) {
5044         texImage = texObj->Image[i][level];
5045         assert(texImage);
5046
5047         compressed_texture_sub_image(ctx, 3, texObj, texImage,
5048                                      texObj->Target, level, xoffset, yoffset,
5049                                      0, width, height, 1, format,
5050                                      imageSize, pixels);
5051
5052         /* Compressed images don't have a client format */
5053         image_stride = _mesa_format_image_size(texImage->TexFormat,
5054                                                texImage->Width,
5055                                                texImage->Height, 1);
5056
5057         pixels += image_stride;
5058         imageSize -= image_stride;
5059      }
5060   } else {
5061      texImage = _mesa_select_tex_image(texObj, target, level);
5062      assert(texImage);
5063
5064      compressed_texture_sub_image(ctx, dim, texObj, texImage, target, level,
5065                                   xoffset, yoffset, zoffset, width, height,
5066                                   depth, format, imageSize, data);
5067   }
5068}
5069
5070static void
5071compressed_tex_sub_image_error(unsigned dim, GLenum target, GLuint texture,
5072                               GLint level, GLint xoffset, GLint yoffset,
5073                               GLint zoffset, GLsizei width, GLsizei height,
5074                               GLsizei depth, GLenum format, GLsizei imageSize,
5075                               const GLvoid *data, bool dsa,
5076                               const char *caller)
5077{
5078   compressed_tex_sub_image(dim, target, texture, level, xoffset, yoffset,
5079                            zoffset, width, height, depth, format, imageSize,
5080                            data, dsa, false, caller);
5081}
5082
5083static void
5084compressed_tex_sub_image_no_error(unsigned dim, GLenum target, GLuint texture,
5085                                  GLint level, GLint xoffset, GLint yoffset,
5086                                  GLint zoffset, GLsizei width, GLsizei height,
5087                                  GLsizei depth, GLenum format, GLsizei imageSize,
5088                                  const GLvoid *data, bool dsa,
5089                                  const char *caller)
5090{
5091   compressed_tex_sub_image(dim, target, texture, level, xoffset, yoffset,
5092                            zoffset, width, height, depth, format, imageSize,
5093                            data, dsa, true, caller);
5094}
5095
5096void GLAPIENTRY
5097_mesa_CompressedTexSubImage1D_no_error(GLenum target, GLint level,
5098                                       GLint xoffset, GLsizei width,
5099                                       GLenum format, GLsizei imageSize,
5100                                       const GLvoid *data)
5101{
5102   compressed_tex_sub_image_no_error(1, target, 0, level, xoffset, 0, 0, width,
5103                                     1, 1, format, imageSize, data, false,
5104                                     "glCompressedTexSubImage1D");
5105}
5106
5107
5108void GLAPIENTRY
5109_mesa_CompressedTexSubImage1D(GLenum target, GLint level, GLint xoffset,
5110                              GLsizei width, GLenum format,
5111                              GLsizei imageSize, const GLvoid *data)
5112{
5113   compressed_tex_sub_image_error(1, target, 0, level, xoffset, 0, 0, width, 1,
5114                                  1, format, imageSize, data, false,
5115                                  "glCompressedTexSubImage1D");
5116}
5117
5118
5119void GLAPIENTRY
5120_mesa_CompressedTextureSubImage1D_no_error(GLuint texture, GLint level,
5121                                           GLint xoffset, GLsizei width,
5122                                           GLenum format, GLsizei imageSize,
5123                                           const GLvoid *data)
5124{
5125   compressed_tex_sub_image_no_error(1, 0, texture, level, xoffset, 0, 0, width,
5126                                     1, 1, format, imageSize, data, true,
5127                                     "glCompressedTextureSubImage1D");
5128}
5129
5130
5131void GLAPIENTRY
5132_mesa_CompressedTextureSubImage1D(GLuint texture, GLint level, GLint xoffset,
5133                                  GLsizei width, GLenum format,
5134                                  GLsizei imageSize, const GLvoid *data)
5135{
5136   compressed_tex_sub_image_error(1, 0, texture, level, xoffset, 0, 0, width,
5137                                  1, 1, format, imageSize, data, true,
5138                                  "glCompressedTextureSubImage1D");
5139}
5140
5141void GLAPIENTRY
5142_mesa_CompressedTexSubImage2D_no_error(GLenum target, GLint level,
5143                                       GLint xoffset, GLint yoffset,
5144                                       GLsizei width, GLsizei height,
5145                                       GLenum format, GLsizei imageSize,
5146                                       const GLvoid *data)
5147{
5148   compressed_tex_sub_image_no_error(2, target, 0, level, xoffset, yoffset, 0,
5149                                     width, height, 1, format, imageSize, data,
5150                                     false, "glCompressedTexSubImage2D");
5151}
5152
5153
5154void GLAPIENTRY
5155_mesa_CompressedTexSubImage2D(GLenum target, GLint level, GLint xoffset,
5156                              GLint yoffset, GLsizei width, GLsizei height,
5157                              GLenum format, GLsizei imageSize,
5158                              const GLvoid *data)
5159{
5160   compressed_tex_sub_image_error(2, target, 0, level, xoffset, yoffset, 0,
5161                                  width, height, 1, format, imageSize, data,
5162                                  false, "glCompressedTexSubImage2D");
5163}
5164
5165
5166void GLAPIENTRY
5167_mesa_CompressedTextureSubImage2D_no_error(GLuint texture, GLint level,
5168                                           GLint xoffset, GLint yoffset,
5169                                           GLsizei width, GLsizei height,
5170                                           GLenum format, GLsizei imageSize,
5171                                           const GLvoid *data)
5172{
5173   compressed_tex_sub_image_no_error(2, 0, texture, level, xoffset, yoffset, 0,
5174                                     width, height, 1, format, imageSize, data,
5175                                     true, "glCompressedTextureSubImage2D");
5176}
5177
5178
5179void GLAPIENTRY
5180_mesa_CompressedTextureSubImage2D(GLuint texture, GLint level, GLint xoffset,
5181                                  GLint yoffset,
5182                                  GLsizei width, GLsizei height,
5183                                  GLenum format, GLsizei imageSize,
5184                                  const GLvoid *data)
5185{
5186   compressed_tex_sub_image_error(2, 0, texture, level, xoffset, yoffset, 0,
5187                                  width, height, 1, format, imageSize, data,
5188                                  true, "glCompressedTextureSubImage2D");
5189}
5190
5191void GLAPIENTRY
5192_mesa_CompressedTexSubImage3D_no_error(GLenum target, GLint level,
5193                                       GLint xoffset, GLint yoffset,
5194                                       GLint zoffset, GLsizei width,
5195                                       GLsizei height, GLsizei depth,
5196                                       GLenum format, GLsizei imageSize,
5197                                       const GLvoid *data)
5198{
5199   compressed_tex_sub_image_no_error(3, target, 0, level, xoffset, yoffset,
5200                                     zoffset, width, height, depth, format,
5201                                     imageSize, data, false,
5202                                     "glCompressedTexSubImage3D");
5203}
5204
5205void GLAPIENTRY
5206_mesa_CompressedTexSubImage3D(GLenum target, GLint level, GLint xoffset,
5207                              GLint yoffset, GLint zoffset, GLsizei width,
5208                              GLsizei height, GLsizei depth, GLenum format,
5209                              GLsizei imageSize, const GLvoid *data)
5210{
5211   compressed_tex_sub_image_error(3, target, 0, level, xoffset, yoffset,
5212                                  zoffset, width, height, depth, format,
5213                                  imageSize, data, false,
5214                                  "glCompressedTexSubImage3D");
5215}
5216
5217void GLAPIENTRY
5218_mesa_CompressedTextureSubImage3D_no_error(GLuint texture, GLint level,
5219                                           GLint xoffset, GLint yoffset,
5220                                           GLint zoffset, GLsizei width,
5221                                           GLsizei height, GLsizei depth,
5222                                           GLenum format, GLsizei imageSize,
5223                                           const GLvoid *data)
5224{
5225   compressed_tex_sub_image_no_error(3, 0, texture, level, xoffset, yoffset,
5226                                     zoffset, width, height, depth, format,
5227                                     imageSize, data, true,
5228                                     "glCompressedTextureSubImage3D");
5229}
5230
5231void GLAPIENTRY
5232_mesa_CompressedTextureSubImage3D(GLuint texture, GLint level, GLint xoffset,
5233                                  GLint yoffset, GLint zoffset, GLsizei width,
5234                                  GLsizei height, GLsizei depth,
5235                                  GLenum format, GLsizei imageSize,
5236                                  const GLvoid *data)
5237{
5238   compressed_tex_sub_image_error(3, 0, texture, level, xoffset, yoffset,
5239                                  zoffset, width, height, depth, format,
5240                                  imageSize, data, true,
5241                                  "glCompressedTextureSubImage3D");
5242}
5243
5244mesa_format
5245_mesa_get_texbuffer_format(const struct gl_context *ctx, GLenum internalFormat)
5246{
5247   if (ctx->API == API_OPENGL_COMPAT) {
5248      switch (internalFormat) {
5249      case GL_ALPHA8:
5250         return MESA_FORMAT_A_UNORM8;
5251      case GL_ALPHA16:
5252         return MESA_FORMAT_A_UNORM16;
5253      case GL_ALPHA16F_ARB:
5254         return MESA_FORMAT_A_FLOAT16;
5255      case GL_ALPHA32F_ARB:
5256         return MESA_FORMAT_A_FLOAT32;
5257      case GL_ALPHA8I_EXT:
5258         return MESA_FORMAT_A_SINT8;
5259      case GL_ALPHA16I_EXT:
5260         return MESA_FORMAT_A_SINT16;
5261      case GL_ALPHA32I_EXT:
5262         return MESA_FORMAT_A_SINT32;
5263      case GL_ALPHA8UI_EXT:
5264         return MESA_FORMAT_A_UINT8;
5265      case GL_ALPHA16UI_EXT:
5266         return MESA_FORMAT_A_UINT16;
5267      case GL_ALPHA32UI_EXT:
5268         return MESA_FORMAT_A_UINT32;
5269      case GL_LUMINANCE8:
5270         return MESA_FORMAT_L_UNORM8;
5271      case GL_LUMINANCE16:
5272         return MESA_FORMAT_L_UNORM16;
5273      case GL_LUMINANCE16F_ARB:
5274         return MESA_FORMAT_L_FLOAT16;
5275      case GL_LUMINANCE32F_ARB:
5276         return MESA_FORMAT_L_FLOAT32;
5277      case GL_LUMINANCE8I_EXT:
5278         return MESA_FORMAT_L_SINT8;
5279      case GL_LUMINANCE16I_EXT:
5280         return MESA_FORMAT_L_SINT16;
5281      case GL_LUMINANCE32I_EXT:
5282         return MESA_FORMAT_L_SINT32;
5283      case GL_LUMINANCE8UI_EXT:
5284         return MESA_FORMAT_L_UINT8;
5285      case GL_LUMINANCE16UI_EXT:
5286         return MESA_FORMAT_L_UINT16;
5287      case GL_LUMINANCE32UI_EXT:
5288         return MESA_FORMAT_L_UINT32;
5289      case GL_LUMINANCE8_ALPHA8:
5290         return MESA_FORMAT_L8A8_UNORM;
5291      case GL_LUMINANCE16_ALPHA16:
5292         return MESA_FORMAT_L16A16_UNORM;
5293      case GL_LUMINANCE_ALPHA16F_ARB:
5294         return MESA_FORMAT_LA_FLOAT16;
5295      case GL_LUMINANCE_ALPHA32F_ARB:
5296         return MESA_FORMAT_LA_FLOAT32;
5297      case GL_LUMINANCE_ALPHA8I_EXT:
5298         return MESA_FORMAT_LA_SINT8;
5299      case GL_LUMINANCE_ALPHA16I_EXT:
5300         return MESA_FORMAT_LA_SINT16;
5301      case GL_LUMINANCE_ALPHA32I_EXT:
5302         return MESA_FORMAT_LA_SINT32;
5303      case GL_LUMINANCE_ALPHA8UI_EXT:
5304         return MESA_FORMAT_LA_UINT8;
5305      case GL_LUMINANCE_ALPHA16UI_EXT:
5306         return MESA_FORMAT_LA_UINT16;
5307      case GL_LUMINANCE_ALPHA32UI_EXT:
5308         return MESA_FORMAT_LA_UINT32;
5309      case GL_INTENSITY8:
5310         return MESA_FORMAT_I_UNORM8;
5311      case GL_INTENSITY16:
5312         return MESA_FORMAT_I_UNORM16;
5313      case GL_INTENSITY16F_ARB:
5314         return MESA_FORMAT_I_FLOAT16;
5315      case GL_INTENSITY32F_ARB:
5316         return MESA_FORMAT_I_FLOAT32;
5317      case GL_INTENSITY8I_EXT:
5318         return MESA_FORMAT_I_SINT8;
5319      case GL_INTENSITY16I_EXT:
5320         return MESA_FORMAT_I_SINT16;
5321      case GL_INTENSITY32I_EXT:
5322         return MESA_FORMAT_I_SINT32;
5323      case GL_INTENSITY8UI_EXT:
5324         return MESA_FORMAT_I_UINT8;
5325      case GL_INTENSITY16UI_EXT:
5326         return MESA_FORMAT_I_UINT16;
5327      case GL_INTENSITY32UI_EXT:
5328         return MESA_FORMAT_I_UINT32;
5329      default:
5330         break;
5331      }
5332   }
5333
5334   if (_mesa_has_ARB_texture_buffer_object_rgb32(ctx) ||
5335       _mesa_has_OES_texture_buffer(ctx)) {
5336      switch (internalFormat) {
5337      case GL_RGB32F:
5338         return MESA_FORMAT_RGB_FLOAT32;
5339      case GL_RGB32UI:
5340         return MESA_FORMAT_RGB_UINT32;
5341      case GL_RGB32I:
5342         return MESA_FORMAT_RGB_SINT32;
5343      default:
5344         break;
5345      }
5346   }
5347
5348   switch (internalFormat) {
5349   case GL_RGBA8:
5350      return MESA_FORMAT_R8G8B8A8_UNORM;
5351   case GL_RGBA16:
5352      if (_mesa_is_gles(ctx) && !_mesa_has_EXT_texture_norm16(ctx))
5353         return MESA_FORMAT_NONE;
5354      return MESA_FORMAT_RGBA_UNORM16;
5355   case GL_RGBA16F_ARB:
5356      return MESA_FORMAT_RGBA_FLOAT16;
5357   case GL_RGBA32F_ARB:
5358      return MESA_FORMAT_RGBA_FLOAT32;
5359   case GL_RGBA8I_EXT:
5360      return MESA_FORMAT_RGBA_SINT8;
5361   case GL_RGBA16I_EXT:
5362      return MESA_FORMAT_RGBA_SINT16;
5363   case GL_RGBA32I_EXT:
5364      return MESA_FORMAT_RGBA_SINT32;
5365   case GL_RGBA8UI_EXT:
5366      return MESA_FORMAT_RGBA_UINT8;
5367   case GL_RGBA16UI_EXT:
5368      return MESA_FORMAT_RGBA_UINT16;
5369   case GL_RGBA32UI_EXT:
5370      return MESA_FORMAT_RGBA_UINT32;
5371
5372   case GL_RG8:
5373      return MESA_FORMAT_R8G8_UNORM;
5374   case GL_RG16:
5375      if (_mesa_is_gles(ctx) && !_mesa_has_EXT_texture_norm16(ctx))
5376         return MESA_FORMAT_NONE;
5377      return MESA_FORMAT_R16G16_UNORM;
5378   case GL_RG16F:
5379      return MESA_FORMAT_RG_FLOAT16;
5380   case GL_RG32F:
5381      return MESA_FORMAT_RG_FLOAT32;
5382   case GL_RG8I:
5383      return MESA_FORMAT_RG_SINT8;
5384   case GL_RG16I:
5385      return MESA_FORMAT_RG_SINT16;
5386   case GL_RG32I:
5387      return MESA_FORMAT_RG_SINT32;
5388   case GL_RG8UI:
5389      return MESA_FORMAT_RG_UINT8;
5390   case GL_RG16UI:
5391      return MESA_FORMAT_RG_UINT16;
5392   case GL_RG32UI:
5393      return MESA_FORMAT_RG_UINT32;
5394
5395   case GL_R8:
5396      return MESA_FORMAT_R_UNORM8;
5397   case GL_R16:
5398      if (_mesa_is_gles(ctx) && !_mesa_has_EXT_texture_norm16(ctx))
5399         return MESA_FORMAT_NONE;
5400      return MESA_FORMAT_R_UNORM16;
5401   case GL_R16F:
5402      return MESA_FORMAT_R_FLOAT16;
5403   case GL_R32F:
5404      return MESA_FORMAT_R_FLOAT32;
5405   case GL_R8I:
5406      return MESA_FORMAT_R_SINT8;
5407   case GL_R16I:
5408      return MESA_FORMAT_R_SINT16;
5409   case GL_R32I:
5410      return MESA_FORMAT_R_SINT32;
5411   case GL_R8UI:
5412      return MESA_FORMAT_R_UINT8;
5413   case GL_R16UI:
5414      return MESA_FORMAT_R_UINT16;
5415   case GL_R32UI:
5416      return MESA_FORMAT_R_UINT32;
5417
5418   default:
5419      return MESA_FORMAT_NONE;
5420   }
5421}
5422
5423
5424mesa_format
5425_mesa_validate_texbuffer_format(const struct gl_context *ctx,
5426                                GLenum internalFormat)
5427{
5428   mesa_format format = _mesa_get_texbuffer_format(ctx, internalFormat);
5429   GLenum datatype;
5430
5431   if (format == MESA_FORMAT_NONE)
5432      return MESA_FORMAT_NONE;
5433
5434   datatype = _mesa_get_format_datatype(format);
5435
5436   /* The GL_ARB_texture_buffer_object spec says:
5437    *
5438    *     "If ARB_texture_float is not supported, references to the
5439    *     floating-point internal formats provided by that extension should be
5440    *     removed, and such formats may not be passed to TexBufferARB."
5441    *
5442    * As a result, GL_HALF_FLOAT internal format depends on both
5443    * GL_ARB_texture_float and GL_ARB_half_float_pixel.
5444    */
5445   if ((datatype == GL_FLOAT || datatype == GL_HALF_FLOAT) &&
5446       !ctx->Extensions.ARB_texture_float)
5447      return MESA_FORMAT_NONE;
5448
5449   if (!ctx->Extensions.ARB_texture_rg) {
5450      GLenum base_format = _mesa_get_format_base_format(format);
5451      if (base_format == GL_R || base_format == GL_RG)
5452         return MESA_FORMAT_NONE;
5453   }
5454
5455   if (!ctx->Extensions.ARB_texture_buffer_object_rgb32) {
5456      GLenum base_format = _mesa_get_format_base_format(format);
5457      if (base_format == GL_RGB)
5458         return MESA_FORMAT_NONE;
5459   }
5460   return format;
5461}
5462
5463
5464/**
5465 * Do work common to glTexBuffer, glTexBufferRange, glTextureBuffer
5466 * and glTextureBufferRange, including some error checking.
5467 */
5468static void
5469texture_buffer_range(struct gl_context *ctx,
5470                     struct gl_texture_object *texObj,
5471                     GLenum internalFormat,
5472                     struct gl_buffer_object *bufObj,
5473                     GLintptr offset, GLsizeiptr size,
5474                     const char *caller)
5475{
5476   GLintptr oldOffset = texObj->BufferOffset;
5477   GLsizeiptr oldSize = texObj->BufferSize;
5478   mesa_format format;
5479
5480   /* NOTE: ARB_texture_buffer_object might not be supported in
5481    * the compatibility profile.
5482    */
5483   if (!_mesa_has_ARB_texture_buffer_object(ctx) &&
5484       !_mesa_has_OES_texture_buffer(ctx)) {
5485      _mesa_error(ctx, GL_INVALID_OPERATION,
5486                  "%s(ARB_texture_buffer_object is not"
5487                  " implemented for the compatibility profile)", caller);
5488      return;
5489   }
5490
5491   if (texObj->HandleAllocated) {
5492      /* The ARB_bindless_texture spec says:
5493       *
5494       * "The error INVALID_OPERATION is generated by TexImage*, CopyTexImage*,
5495       *  CompressedTexImage*, TexBuffer*, TexParameter*, as well as other
5496       *  functions defined in terms of these, if the texture object to be
5497       *  modified is referenced by one or more texture or image handles."
5498       */
5499      _mesa_error(ctx, GL_INVALID_OPERATION, "%s(immutable texture)", caller);
5500      return;
5501   }
5502
5503   format = _mesa_validate_texbuffer_format(ctx, internalFormat);
5504   if (format == MESA_FORMAT_NONE) {
5505      _mesa_error(ctx, GL_INVALID_ENUM, "%s(internalFormat %s)",
5506                  caller, _mesa_enum_to_string(internalFormat));
5507      return;
5508   }
5509
5510   FLUSH_VERTICES(ctx, 0);
5511
5512   _mesa_lock_texture(ctx, texObj);
5513   {
5514      _mesa_reference_buffer_object(ctx, &texObj->BufferObject, bufObj);
5515      texObj->BufferObjectFormat = internalFormat;
5516      texObj->_BufferObjectFormat = format;
5517      texObj->BufferOffset = offset;
5518      texObj->BufferSize = size;
5519   }
5520   _mesa_unlock_texture(ctx, texObj);
5521
5522   if (ctx->Driver.TexParameter) {
5523      if (offset != oldOffset) {
5524         ctx->Driver.TexParameter(ctx, texObj, GL_TEXTURE_BUFFER_OFFSET);
5525      }
5526      if (size != oldSize) {
5527         ctx->Driver.TexParameter(ctx, texObj, GL_TEXTURE_BUFFER_SIZE);
5528      }
5529   }
5530
5531   ctx->NewDriverState |= ctx->DriverFlags.NewTextureBuffer;
5532
5533   if (bufObj) {
5534      bufObj->UsageHistory |= USAGE_TEXTURE_BUFFER;
5535   }
5536}
5537
5538
5539/**
5540 * Make sure the texture buffer target is GL_TEXTURE_BUFFER.
5541 * Return true if it is, and return false if it is not
5542 * (and throw INVALID ENUM as dictated in the OpenGL 4.5
5543 * core spec, 02.02.2015, PDF page 245).
5544 */
5545static bool
5546check_texture_buffer_target(struct gl_context *ctx, GLenum target,
5547                            const char *caller)
5548{
5549   if (target != GL_TEXTURE_BUFFER_ARB) {
5550      _mesa_error(ctx, GL_INVALID_ENUM,
5551                  "%s(texture target is not GL_TEXTURE_BUFFER)", caller);
5552      return false;
5553   }
5554   else
5555      return true;
5556}
5557
5558/**
5559 * Check for errors related to the texture buffer range.
5560 * Return false if errors are found, true if none are found.
5561 */
5562static bool
5563check_texture_buffer_range(struct gl_context *ctx,
5564                           struct gl_buffer_object *bufObj,
5565                           GLintptr offset, GLsizeiptr size,
5566                           const char *caller)
5567{
5568   /* OpenGL 4.5 core spec (02.02.2015) says in Section 8.9 Buffer
5569    * Textures (PDF page 245):
5570    *    "An INVALID_VALUE error is generated if offset is negative, if
5571    *    size is less than or equal to zero, or if offset + size is greater
5572    *    than the value of BUFFER_SIZE for the buffer bound to target."
5573    */
5574   if (offset < 0) {
5575      _mesa_error(ctx, GL_INVALID_VALUE, "%s(offset=%d < 0)", caller,
5576                  (int) offset);
5577      return false;
5578   }
5579
5580   if (size <= 0) {
5581      _mesa_error(ctx, GL_INVALID_VALUE, "%s(size=%d <= 0)", caller,
5582                  (int) size);
5583      return false;
5584   }
5585
5586   if (offset + size > bufObj->Size) {
5587      _mesa_error(ctx, GL_INVALID_VALUE,
5588                  "%s(offset=%d + size=%d > buffer_size=%d)", caller,
5589                  (int) offset, (int) size, (int) bufObj->Size);
5590      return false;
5591   }
5592
5593   /* OpenGL 4.5 core spec (02.02.2015) says in Section 8.9 Buffer
5594    * Textures (PDF page 245):
5595    *    "An INVALID_VALUE error is generated if offset is not an integer
5596    *    multiple of the value of TEXTURE_BUFFER_OFFSET_ALIGNMENT."
5597    */
5598   if (offset % ctx->Const.TextureBufferOffsetAlignment) {
5599      _mesa_error(ctx, GL_INVALID_VALUE,
5600                  "%s(invalid offset alignment)", caller);
5601      return false;
5602   }
5603
5604   return true;
5605}
5606
5607
5608/** GL_ARB_texture_buffer_object */
5609void GLAPIENTRY
5610_mesa_TexBuffer(GLenum target, GLenum internalFormat, GLuint buffer)
5611{
5612   struct gl_texture_object *texObj;
5613   struct gl_buffer_object *bufObj;
5614
5615   GET_CURRENT_CONTEXT(ctx);
5616
5617   /* Need to catch a bad target before it gets to
5618    * _mesa_get_current_tex_object.
5619    */
5620   if (!check_texture_buffer_target(ctx, target, "glTexBuffer"))
5621      return;
5622
5623   if (buffer) {
5624      bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, "glTexBuffer");
5625      if (!bufObj)
5626         return;
5627   } else
5628      bufObj = NULL;
5629
5630   texObj = _mesa_get_current_tex_object(ctx, target);
5631   if (!texObj)
5632      return;
5633
5634   texture_buffer_range(ctx, texObj, internalFormat, bufObj, 0,
5635                        buffer ? -1 : 0, "glTexBuffer");
5636}
5637
5638
5639/** GL_ARB_texture_buffer_range */
5640void GLAPIENTRY
5641_mesa_TexBufferRange(GLenum target, GLenum internalFormat, GLuint buffer,
5642                     GLintptr offset, GLsizeiptr size)
5643{
5644   struct gl_texture_object *texObj;
5645   struct gl_buffer_object *bufObj;
5646
5647   GET_CURRENT_CONTEXT(ctx);
5648
5649   /* Need to catch a bad target before it gets to
5650    * _mesa_get_current_tex_object.
5651    */
5652   if (!check_texture_buffer_target(ctx, target, "glTexBufferRange"))
5653      return;
5654
5655   if (buffer) {
5656      bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, "glTexBufferRange");
5657      if (!bufObj)
5658         return;
5659
5660      if (!check_texture_buffer_range(ctx, bufObj, offset, size,
5661          "glTexBufferRange"))
5662         return;
5663
5664   } else {
5665      /* OpenGL 4.5 core spec (02.02.2015) says in Section 8.9 Buffer
5666       * Textures (PDF page 254):
5667       *    "If buffer is zero, then any buffer object attached to the buffer
5668       *    texture is detached, the values offset and size are ignored and
5669       *    the state for offset and size for the buffer texture are reset to
5670       *    zero."
5671       */
5672      offset = 0;
5673      size = 0;
5674      bufObj = NULL;
5675   }
5676
5677   texObj = _mesa_get_current_tex_object(ctx, target);
5678   if (!texObj)
5679      return;
5680
5681   texture_buffer_range(ctx, texObj, internalFormat, bufObj,
5682                        offset, size, "glTexBufferRange");
5683}
5684
5685void GLAPIENTRY
5686_mesa_TextureBuffer(GLuint texture, GLenum internalFormat, GLuint buffer)
5687{
5688   struct gl_texture_object *texObj;
5689   struct gl_buffer_object *bufObj;
5690
5691   GET_CURRENT_CONTEXT(ctx);
5692
5693   if (buffer) {
5694      bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, "glTextureBuffer");
5695      if (!bufObj)
5696         return;
5697   } else
5698      bufObj = NULL;
5699
5700   /* Get the texture object by Name. */
5701   texObj = _mesa_lookup_texture_err(ctx, texture, "glTextureBuffer");
5702   if (!texObj)
5703      return;
5704
5705   if (!check_texture_buffer_target(ctx, texObj->Target, "glTextureBuffer"))
5706      return;
5707
5708   texture_buffer_range(ctx, texObj, internalFormat,
5709                        bufObj, 0, buffer ? -1 : 0, "glTextureBuffer");
5710}
5711
5712void GLAPIENTRY
5713_mesa_TextureBufferRange(GLuint texture, GLenum internalFormat, GLuint buffer,
5714                         GLintptr offset, GLsizeiptr size)
5715{
5716   struct gl_texture_object *texObj;
5717   struct gl_buffer_object *bufObj;
5718
5719   GET_CURRENT_CONTEXT(ctx);
5720
5721   if (buffer) {
5722      bufObj = _mesa_lookup_bufferobj_err(ctx, buffer,
5723                                          "glTextureBufferRange");
5724      if (!bufObj)
5725         return;
5726
5727      if (!check_texture_buffer_range(ctx, bufObj, offset, size,
5728          "glTextureBufferRange"))
5729         return;
5730
5731   } else {
5732      /* OpenGL 4.5 core spec (02.02.2015) says in Section 8.9 Buffer
5733       * Textures (PDF page 254):
5734       *    "If buffer is zero, then any buffer object attached to the buffer
5735       *    texture is detached, the values offset and size are ignored and
5736       *    the state for offset and size for the buffer texture are reset to
5737       *    zero."
5738       */
5739      offset = 0;
5740      size = 0;
5741      bufObj = NULL;
5742   }
5743
5744   /* Get the texture object by Name. */
5745   texObj = _mesa_lookup_texture_err(ctx, texture, "glTextureBufferRange");
5746   if (!texObj)
5747      return;
5748
5749   if (!check_texture_buffer_target(ctx, texObj->Target,
5750       "glTextureBufferRange"))
5751      return;
5752
5753   texture_buffer_range(ctx, texObj, internalFormat,
5754                        bufObj, offset, size, "glTextureBufferRange");
5755}
5756
5757GLboolean
5758_mesa_is_renderable_texture_format(const struct gl_context *ctx,
5759                                   GLenum internalformat)
5760{
5761   /* Everything that is allowed for renderbuffers,
5762    * except for a base format of GL_STENCIL_INDEX, unless supported.
5763    */
5764   GLenum baseFormat = _mesa_base_fbo_format(ctx, internalformat);
5765   if (ctx->Extensions.ARB_texture_stencil8)
5766      return baseFormat != 0;
5767   else
5768      return baseFormat != 0 && baseFormat != GL_STENCIL_INDEX;
5769}
5770
5771
5772/** GL_ARB_texture_multisample */
5773static GLboolean
5774check_multisample_target(GLuint dims, GLenum target, bool dsa)
5775{
5776   switch(target) {
5777   case GL_TEXTURE_2D_MULTISAMPLE:
5778      return dims == 2;
5779   case GL_PROXY_TEXTURE_2D_MULTISAMPLE:
5780      return dims == 2 && !dsa;
5781   case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
5782      return dims == 3;
5783   case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY:
5784      return dims == 3 && !dsa;
5785   default:
5786      return GL_FALSE;
5787   }
5788}
5789
5790
5791static void
5792texture_image_multisample(struct gl_context *ctx, GLuint dims,
5793                          struct gl_texture_object *texObj,
5794                          struct gl_memory_object *memObj,
5795                          GLenum target, GLsizei samples,
5796                          GLint internalformat, GLsizei width,
5797                          GLsizei height, GLsizei depth,
5798                          GLboolean fixedsamplelocations,
5799                          GLboolean immutable, GLuint64 offset,
5800                          const char *func)
5801{
5802   struct gl_texture_image *texImage;
5803   GLboolean sizeOK, dimensionsOK, samplesOK;
5804   mesa_format texFormat;
5805   GLenum sample_count_error;
5806   bool dsa = strstr(func, "ture") ? true : false;
5807
5808   if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE)) {
5809      _mesa_debug(ctx, "%s(target=%s, samples=%d)\n", func,
5810                  _mesa_enum_to_string(target), samples);
5811   }
5812
5813   if (!((ctx->Extensions.ARB_texture_multisample
5814         && _mesa_is_desktop_gl(ctx))) && !_mesa_is_gles31(ctx)) {
5815      _mesa_error(ctx, GL_INVALID_OPERATION, "%s(unsupported)", func);
5816      return;
5817   }
5818
5819   if (samples < 1) {
5820      _mesa_error(ctx, GL_INVALID_VALUE, "%s(samples < 1)", func);
5821      return;
5822   }
5823
5824   if (!check_multisample_target(dims, target, dsa)) {
5825      GLenum err = dsa ? GL_INVALID_OPERATION : GL_INVALID_ENUM;
5826      _mesa_error(ctx, err, "%s(target=%s)", func,
5827                  _mesa_enum_to_string(target));
5828      return;
5829   }
5830
5831   /* check that the specified internalformat is color/depth/stencil-renderable;
5832    * refer GL3.1 spec 4.4.4
5833    */
5834
5835   if (immutable && !_mesa_is_legal_tex_storage_format(ctx, internalformat)) {
5836      _mesa_error(ctx, GL_INVALID_ENUM,
5837            "%s(internalformat=%s not legal for immutable-format)",
5838            func, _mesa_enum_to_string(internalformat));
5839      return;
5840   }
5841
5842   if (!_mesa_is_renderable_texture_format(ctx, internalformat)) {
5843      /* Page 172 of OpenGL ES 3.1 spec says:
5844       *   "An INVALID_ENUM error is generated if sizedinternalformat is not
5845       *   color-renderable, depth-renderable, or stencil-renderable (as
5846       *   defined in section 9.4).
5847       *
5848       *  (Same error is also defined for desktop OpenGL for multisample
5849       *  teximage/texstorage functions.)
5850       */
5851      _mesa_error(ctx, GL_INVALID_ENUM, "%s(internalformat=%s)", func,
5852                  _mesa_enum_to_string(internalformat));
5853      return;
5854   }
5855
5856   sample_count_error = _mesa_check_sample_count(ctx, target,
5857         internalformat, samples, samples);
5858   samplesOK = sample_count_error == GL_NO_ERROR;
5859
5860   /* Page 254 of OpenGL 4.4 spec says:
5861    *   "Proxy arrays for two-dimensional multisample and two-dimensional
5862    *    multisample array textures are operated on in the same way when
5863    *    TexImage2DMultisample is called with target specified as
5864    *    PROXY_TEXTURE_2D_MULTISAMPLE, or TexImage3DMultisample is called
5865    *    with target specified as PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY.
5866    *    However, if samples is not supported, then no error is generated.
5867    */
5868   if (!samplesOK && !_mesa_is_proxy_texture(target)) {
5869      _mesa_error(ctx, sample_count_error, "%s(samples=%d)", func, samples);
5870      return;
5871   }
5872
5873   if (immutable && (!texObj || (texObj->Name == 0))) {
5874      _mesa_error(ctx, GL_INVALID_OPERATION,
5875            "%s(texture object 0)",
5876            func);
5877      return;
5878   }
5879
5880   texImage = _mesa_get_tex_image(ctx, texObj, 0, 0);
5881
5882   if (texImage == NULL) {
5883      _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s()", func);
5884      return;
5885   }
5886
5887   texFormat = _mesa_choose_texture_format(ctx, texObj, target, 0,
5888         internalformat, GL_NONE, GL_NONE);
5889   assert(texFormat != MESA_FORMAT_NONE);
5890
5891   dimensionsOK = _mesa_legal_texture_dimensions(ctx, target, 0,
5892         width, height, depth, 0);
5893
5894   sizeOK = ctx->Driver.TestProxyTexImage(ctx, target, 0, 0, texFormat,
5895                                          samples, width, height, depth);
5896
5897   if (_mesa_is_proxy_texture(target)) {
5898      if (samplesOK && dimensionsOK && sizeOK) {
5899         _mesa_init_teximage_fields_ms(ctx, texImage, width, height, depth, 0,
5900                                       internalformat, texFormat,
5901                                       samples, fixedsamplelocations);
5902      }
5903      else {
5904         /* clear all image fields */
5905         clear_teximage_fields(texImage);
5906      }
5907   }
5908   else {
5909      if (!dimensionsOK) {
5910         _mesa_error(ctx, GL_INVALID_VALUE,
5911                     "%s(invalid width=%d or height=%d)", func, width, height);
5912         return;
5913      }
5914
5915      if (!sizeOK) {
5916         _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s(texture too large)", func);
5917         return;
5918      }
5919
5920      /* Check if texObj->Immutable is set */
5921      if (texObj->Immutable) {
5922         _mesa_error(ctx, GL_INVALID_OPERATION, "%s(immutable)", func);
5923         return;
5924      }
5925
5926      ctx->Driver.FreeTextureImageBuffer(ctx, texImage);
5927
5928      _mesa_init_teximage_fields_ms(ctx, texImage, width, height, depth, 0,
5929                                    internalformat, texFormat,
5930                                    samples, fixedsamplelocations);
5931
5932      if (width > 0 && height > 0 && depth > 0) {
5933         if (memObj) {
5934            if (!ctx->Driver.SetTextureStorageForMemoryObject(ctx, texObj,
5935                                                              memObj, 1, width,
5936                                                              height, depth,
5937                                                              offset)) {
5938
5939               _mesa_init_teximage_fields(ctx, texImage, 0, 0, 0, 0,
5940                                          internalformat, texFormat);
5941            }
5942         } else {
5943            if (!ctx->Driver.AllocTextureStorage(ctx, texObj, 1,
5944                                                 width, height, depth)) {
5945               /* tidy up the texture image state. strictly speaking,
5946                * we're allowed to just leave this in whatever state we
5947                * like, but being tidy is good.
5948                */
5949               _mesa_init_teximage_fields(ctx, texImage, 0, 0, 0, 0,
5950                                          internalformat, texFormat);
5951            }
5952         }
5953      }
5954
5955      texObj->Immutable |= immutable;
5956
5957      if (immutable) {
5958         _mesa_set_texture_view_state(ctx, texObj, target, 1);
5959      }
5960
5961      _mesa_update_fbo_texture(ctx, texObj, 0, 0);
5962   }
5963}
5964
5965
5966void GLAPIENTRY
5967_mesa_TexImage2DMultisample(GLenum target, GLsizei samples,
5968                            GLenum internalformat, GLsizei width,
5969                            GLsizei height, GLboolean fixedsamplelocations)
5970{
5971   struct gl_texture_object *texObj;
5972   GET_CURRENT_CONTEXT(ctx);
5973
5974   texObj = _mesa_get_current_tex_object(ctx, target);
5975   if (!texObj)
5976      return;
5977
5978   texture_image_multisample(ctx, 2, texObj, NULL, target, samples,
5979                             internalformat, width, height, 1,
5980                             fixedsamplelocations, GL_FALSE, 0,
5981                             "glTexImage2DMultisample");
5982}
5983
5984
5985void GLAPIENTRY
5986_mesa_TexImage3DMultisample(GLenum target, GLsizei samples,
5987                            GLenum internalformat, GLsizei width,
5988                            GLsizei height, GLsizei depth,
5989                            GLboolean fixedsamplelocations)
5990{
5991   struct gl_texture_object *texObj;
5992   GET_CURRENT_CONTEXT(ctx);
5993
5994   texObj = _mesa_get_current_tex_object(ctx, target);
5995   if (!texObj)
5996      return;
5997
5998   texture_image_multisample(ctx, 3, texObj, NULL, target, samples,
5999                             internalformat, width, height, depth,
6000                             fixedsamplelocations, GL_FALSE, 0,
6001                             "glTexImage3DMultisample");
6002}
6003
6004static bool
6005valid_texstorage_ms_parameters(GLsizei width, GLsizei height, GLsizei depth,
6006                               unsigned dims)
6007{
6008   GET_CURRENT_CONTEXT(ctx);
6009
6010   if (!_mesa_valid_tex_storage_dim(width, height, depth)) {
6011      _mesa_error(ctx, GL_INVALID_VALUE,
6012                  "glTexStorage%uDMultisample(width=%d,height=%d,depth=%d)",
6013                  dims, width, height, depth);
6014      return false;
6015   }
6016   return true;
6017}
6018
6019void GLAPIENTRY
6020_mesa_TexStorage2DMultisample(GLenum target, GLsizei samples,
6021                              GLenum internalformat, GLsizei width,
6022                              GLsizei height, GLboolean fixedsamplelocations)
6023{
6024   struct gl_texture_object *texObj;
6025   GET_CURRENT_CONTEXT(ctx);
6026
6027   texObj = _mesa_get_current_tex_object(ctx, target);
6028   if (!texObj)
6029      return;
6030
6031   if (!valid_texstorage_ms_parameters(width, height, 1, 2))
6032      return;
6033
6034   texture_image_multisample(ctx, 2, texObj, NULL, target, samples,
6035                             internalformat, width, height, 1,
6036                             fixedsamplelocations, GL_TRUE, 0,
6037                             "glTexStorage2DMultisample");
6038}
6039
6040void GLAPIENTRY
6041_mesa_TexStorage3DMultisample(GLenum target, GLsizei samples,
6042                              GLenum internalformat, GLsizei width,
6043                              GLsizei height, GLsizei depth,
6044                              GLboolean fixedsamplelocations)
6045{
6046   struct gl_texture_object *texObj;
6047   GET_CURRENT_CONTEXT(ctx);
6048
6049   texObj = _mesa_get_current_tex_object(ctx, target);
6050   if (!texObj)
6051      return;
6052
6053   if (!valid_texstorage_ms_parameters(width, height, depth, 3))
6054      return;
6055
6056   texture_image_multisample(ctx, 3, texObj, NULL, target, samples,
6057                             internalformat, width, height, depth,
6058                             fixedsamplelocations, GL_TRUE, 0,
6059                             "glTexStorage3DMultisample");
6060}
6061
6062void GLAPIENTRY
6063_mesa_TextureStorage2DMultisample(GLuint texture, GLsizei samples,
6064                                  GLenum internalformat, GLsizei width,
6065                                  GLsizei height,
6066                                  GLboolean fixedsamplelocations)
6067{
6068   struct gl_texture_object *texObj;
6069   GET_CURRENT_CONTEXT(ctx);
6070
6071   texObj = _mesa_lookup_texture_err(ctx, texture,
6072                                     "glTextureStorage2DMultisample");
6073   if (!texObj)
6074      return;
6075
6076   if (!valid_texstorage_ms_parameters(width, height, 1, 2))
6077      return;
6078
6079   texture_image_multisample(ctx, 2, texObj, NULL, texObj->Target,
6080                             samples, internalformat, width, height, 1,
6081                             fixedsamplelocations, GL_TRUE, 0,
6082                             "glTextureStorage2DMultisample");
6083}
6084
6085void GLAPIENTRY
6086_mesa_TextureStorage3DMultisample(GLuint texture, GLsizei samples,
6087                                  GLenum internalformat, GLsizei width,
6088                                  GLsizei height, GLsizei depth,
6089                                  GLboolean fixedsamplelocations)
6090{
6091   struct gl_texture_object *texObj;
6092   GET_CURRENT_CONTEXT(ctx);
6093
6094   /* Get the texture object by Name. */
6095   texObj = _mesa_lookup_texture_err(ctx, texture,
6096                                     "glTextureStorage3DMultisample");
6097   if (!texObj)
6098      return;
6099
6100   if (!valid_texstorage_ms_parameters(width, height, depth, 3))
6101      return;
6102
6103   texture_image_multisample(ctx, 3, texObj, NULL, texObj->Target, samples,
6104                             internalformat, width, height, depth,
6105                             fixedsamplelocations, GL_TRUE, 0,
6106                             "glTextureStorage3DMultisample");
6107}
6108
6109void
6110_mesa_texture_storage_ms_memory(struct gl_context *ctx, GLuint dims,
6111                                struct gl_texture_object *texObj,
6112                                struct gl_memory_object *memObj,
6113                                GLenum target, GLsizei samples,
6114                                GLenum internalFormat, GLsizei width,
6115                                GLsizei height, GLsizei depth,
6116                                GLboolean fixedSampleLocations,
6117                                GLuint64 offset, const char* func)
6118{
6119   assert(memObj);
6120
6121   texture_image_multisample(ctx, dims, texObj, memObj, target, samples,
6122                             internalFormat, width, height, depth,
6123                             fixedSampleLocations, GL_TRUE, offset,
6124                             func);
6125}
6126