teximage.c revision 01e04c3f
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      return true;
1370   default:
1371      return false;
1372   }
1373}
1374
1375/**
1376 * Return true if the format doesn't support online compression.
1377 */
1378bool
1379_mesa_format_no_online_compression(GLenum format)
1380{
1381   return _mesa_is_astc_format(format) ||
1382          _mesa_is_etc2_format(format) ||
1383          compressedteximage_only_format(format);
1384}
1385
1386/* Writes to an GL error pointer if non-null and returns whether or not the
1387 * error is GL_NO_ERROR */
1388static bool
1389write_error(GLenum *err_ptr, GLenum error)
1390{
1391   if (err_ptr)
1392      *err_ptr = error;
1393
1394   return error == GL_NO_ERROR;
1395}
1396
1397/**
1398 * Helper function to determine whether a target and specific compression
1399 * format are supported. The error parameter returns GL_NO_ERROR if the
1400 * target can be compressed. Otherwise it returns either GL_INVALID_OPERATION
1401 * or GL_INVALID_ENUM, whichever is more appropriate.
1402 */
1403GLboolean
1404_mesa_target_can_be_compressed(const struct gl_context *ctx, GLenum target,
1405                               GLenum intFormat, GLenum *error)
1406{
1407   GLboolean target_can_be_compresed = GL_FALSE;
1408   mesa_format format = _mesa_glenum_to_compressed_format(intFormat);
1409   enum mesa_format_layout layout = _mesa_get_format_layout(format);
1410
1411   switch (target) {
1412   case GL_TEXTURE_2D:
1413   case GL_PROXY_TEXTURE_2D:
1414      target_can_be_compresed = GL_TRUE; /* true for any compressed format so far */
1415      break;
1416   case GL_PROXY_TEXTURE_CUBE_MAP:
1417   case GL_TEXTURE_CUBE_MAP:
1418   case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
1419   case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
1420   case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
1421   case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
1422   case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
1423   case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
1424      target_can_be_compresed = ctx->Extensions.ARB_texture_cube_map;
1425      break;
1426   case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
1427   case GL_TEXTURE_2D_ARRAY_EXT:
1428      target_can_be_compresed = ctx->Extensions.EXT_texture_array;
1429      break;
1430   case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
1431   case GL_TEXTURE_CUBE_MAP_ARRAY:
1432      /* From the KHR_texture_compression_astc_hdr spec:
1433       *
1434       *     Add a second new column "3D Tex." which is empty for all non-ASTC
1435       *     formats. If only the LDR profile is supported by the
1436       *     implementation, this column is also empty for all ASTC formats. If
1437       *     both the LDR and HDR profiles are supported only, this column is
1438       *     checked for all ASTC formats.
1439       *
1440       *     Add a third new column "Cube Map Array Tex." which is empty for all
1441       *     non-ASTC formats, and checked for all ASTC formats.
1442       *
1443       * and,
1444       *
1445       *     'An INVALID_OPERATION error is generated by CompressedTexImage3D
1446       *      if <internalformat> is TEXTURE_CUBE_MAP_ARRAY and the
1447       *      "Cube Map Array" column of table 8.19 is *not* checked, or if
1448       *      <internalformat> is TEXTURE_3D and the "3D Tex." column of table
1449       *      8.19 is *not* checked'
1450       *
1451       * The instances of <internalformat> above should say <target>.
1452       *
1453       * ETC2/EAC formats are the only alternative in GLES and thus such errors
1454       * have already been handled by normal ETC2/EAC behavior.
1455       */
1456
1457      /* From section 3.8.6, page 146 of OpenGL ES 3.0 spec:
1458       *
1459       *    "The ETC2/EAC texture compression algorithm supports only
1460       *     two-dimensional images. If internalformat is an ETC2/EAC format,
1461       *     glCompressedTexImage3D will generate an INVALID_OPERATION error if
1462       *     target is not TEXTURE_2D_ARRAY."
1463       *
1464       * This should also be applicable for glTexStorage3D(). Other available
1465       * targets for these functions are: TEXTURE_3D and TEXTURE_CUBE_MAP_ARRAY.
1466       *
1467       * Section 8.7, page 179 of OpenGL ES 3.2 adds:
1468       *
1469       *      An INVALID_OPERATION error is generated by CompressedTexImage3D
1470       *      if internalformat is one of the the formats in table 8.17 and target is
1471       *      not TEXTURE_2D_ARRAY, TEXTURE_CUBE_MAP_ARRAY or TEXTURE_3D.
1472       *
1473       *      An INVALID_OPERATION error is generated by CompressedTexImage3D
1474       *      if internalformat is TEXTURE_CUBE_MAP_ARRAY and the “Cube Map
1475       *      Array” column of table 8.17 is not checked, or if internalformat
1476       *      is TEXTURE_- 3D and the “3D Tex.” column of table 8.17 is not
1477       *      checked.
1478       *
1479       * The instances of <internalformat> above should say <target>.
1480       *
1481       * Such table 8.17 has checked "Cube Map Array" column for all the
1482       * cases. So in practice, TEXTURE_CUBE_MAP_ARRAY is now valid for OpenGL ES 3.2
1483       */
1484      if (layout == MESA_FORMAT_LAYOUT_ETC2 && _mesa_is_gles3(ctx) &&
1485          !_mesa_is_gles32(ctx))
1486            return write_error(error, GL_INVALID_OPERATION);
1487      target_can_be_compresed = _mesa_has_texture_cube_map_array(ctx);
1488      break;
1489   case GL_TEXTURE_3D:
1490      switch (layout) {
1491      case MESA_FORMAT_LAYOUT_ETC2:
1492         /* See ETC2/EAC comment in case GL_TEXTURE_CUBE_MAP_ARRAY. */
1493         if (_mesa_is_gles3(ctx))
1494            return write_error(error, GL_INVALID_OPERATION);
1495         break;
1496      case MESA_FORMAT_LAYOUT_BPTC:
1497         target_can_be_compresed = ctx->Extensions.ARB_texture_compression_bptc;
1498         break;
1499      case MESA_FORMAT_LAYOUT_ASTC:
1500         target_can_be_compresed =
1501            ctx->Extensions.KHR_texture_compression_astc_hdr ||
1502            ctx->Extensions.KHR_texture_compression_astc_sliced_3d;
1503
1504         /* Throw an INVALID_OPERATION error if the target is TEXTURE_3D and
1505          * neither of the above extensions are supported. See comment in
1506          * switch case GL_TEXTURE_CUBE_MAP_ARRAY for more info.
1507          */
1508         if (!target_can_be_compresed)
1509            return write_error(error, GL_INVALID_OPERATION);
1510         break;
1511      default:
1512         break;
1513      }
1514   default:
1515      break;
1516   }
1517   return write_error(error,
1518                      target_can_be_compresed ? GL_NO_ERROR : GL_INVALID_ENUM);
1519}
1520
1521
1522/**
1523 * Check if the given texture target value is legal for a
1524 * glTexImage1/2/3D call.
1525 */
1526static GLboolean
1527legal_teximage_target(struct gl_context *ctx, GLuint dims, GLenum target)
1528{
1529   switch (dims) {
1530   case 1:
1531      switch (target) {
1532      case GL_TEXTURE_1D:
1533      case GL_PROXY_TEXTURE_1D:
1534         return _mesa_is_desktop_gl(ctx);
1535      default:
1536         return GL_FALSE;
1537      }
1538   case 2:
1539      switch (target) {
1540      case GL_TEXTURE_2D:
1541         return GL_TRUE;
1542      case GL_PROXY_TEXTURE_2D:
1543         return _mesa_is_desktop_gl(ctx);
1544      case GL_PROXY_TEXTURE_CUBE_MAP:
1545         return _mesa_is_desktop_gl(ctx)
1546            && ctx->Extensions.ARB_texture_cube_map;
1547      case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
1548      case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
1549      case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
1550      case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
1551      case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
1552      case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
1553         return ctx->Extensions.ARB_texture_cube_map;
1554      case GL_TEXTURE_RECTANGLE_NV:
1555      case GL_PROXY_TEXTURE_RECTANGLE_NV:
1556         return _mesa_is_desktop_gl(ctx)
1557            && ctx->Extensions.NV_texture_rectangle;
1558      case GL_TEXTURE_1D_ARRAY_EXT:
1559      case GL_PROXY_TEXTURE_1D_ARRAY_EXT:
1560         return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_array;
1561      default:
1562         return GL_FALSE;
1563      }
1564   case 3:
1565      switch (target) {
1566      case GL_TEXTURE_3D:
1567         return GL_TRUE;
1568      case GL_PROXY_TEXTURE_3D:
1569         return _mesa_is_desktop_gl(ctx);
1570      case GL_TEXTURE_2D_ARRAY_EXT:
1571         return (_mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_array)
1572            || _mesa_is_gles3(ctx);
1573      case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
1574         return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_array;
1575      case GL_TEXTURE_CUBE_MAP_ARRAY:
1576      case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
1577         return _mesa_has_texture_cube_map_array(ctx);
1578      default:
1579         return GL_FALSE;
1580      }
1581   default:
1582      _mesa_problem(ctx, "invalid dims=%u in legal_teximage_target()", dims);
1583      return GL_FALSE;
1584   }
1585}
1586
1587
1588/**
1589 * Check if the given texture target value is legal for a
1590 * glTexSubImage, glCopyTexSubImage or glCopyTexImage call.
1591 * The difference compared to legal_teximage_target() above is that
1592 * proxy targets are not supported.
1593 */
1594static GLboolean
1595legal_texsubimage_target(struct gl_context *ctx, GLuint dims, GLenum target,
1596                         bool dsa)
1597{
1598   switch (dims) {
1599   case 1:
1600      return _mesa_is_desktop_gl(ctx) && target == GL_TEXTURE_1D;
1601   case 2:
1602      switch (target) {
1603      case GL_TEXTURE_2D:
1604         return GL_TRUE;
1605      case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
1606      case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
1607      case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
1608      case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
1609      case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
1610      case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
1611         return ctx->Extensions.ARB_texture_cube_map;
1612      case GL_TEXTURE_RECTANGLE_NV:
1613         return _mesa_is_desktop_gl(ctx)
1614            && ctx->Extensions.NV_texture_rectangle;
1615      case GL_TEXTURE_1D_ARRAY_EXT:
1616         return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_array;
1617      default:
1618         return GL_FALSE;
1619      }
1620   case 3:
1621      switch (target) {
1622      case GL_TEXTURE_3D:
1623         return GL_TRUE;
1624      case GL_TEXTURE_2D_ARRAY_EXT:
1625         return (_mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_array)
1626            || _mesa_is_gles3(ctx);
1627      case GL_TEXTURE_CUBE_MAP_ARRAY:
1628      case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
1629         return _mesa_has_texture_cube_map_array(ctx);
1630
1631      /* Table 8.15 of the OpenGL 4.5 core profile spec
1632       * (20141030) says that TEXTURE_CUBE_MAP is valid for TextureSubImage3D
1633       * and CopyTextureSubImage3D.
1634       */
1635      case GL_TEXTURE_CUBE_MAP:
1636         return dsa;
1637      default:
1638         return GL_FALSE;
1639      }
1640   default:
1641      _mesa_problem(ctx, "invalid dims=%u in legal_texsubimage_target()",
1642                    dims);
1643      return GL_FALSE;
1644   }
1645}
1646
1647
1648/**
1649 * Helper function to determine if a texture object is mutable (in terms
1650 * of GL_ARB_texture_storage/GL_ARB_bindless_texture).
1651 */
1652static GLboolean
1653mutable_tex_object(struct gl_context *ctx, GLenum target)
1654{
1655   struct gl_texture_object *texObj = _mesa_get_current_tex_object(ctx, target);
1656   if (!texObj)
1657      return GL_FALSE;
1658
1659   if (texObj->HandleAllocated) {
1660      /* The ARB_bindless_texture spec says:
1661       *
1662       * "The error INVALID_OPERATION is generated by TexImage*, CopyTexImage*,
1663       *  CompressedTexImage*, TexBuffer*, TexParameter*, as well as other
1664       *  functions defined in terms of these, if the texture object to be
1665       *  modified is referenced by one or more texture or image handles."
1666       */
1667      return GL_FALSE;
1668   }
1669
1670   return !texObj->Immutable;
1671}
1672
1673
1674/**
1675 * Return expected size of a compressed texture.
1676 */
1677static GLuint
1678compressed_tex_size(GLsizei width, GLsizei height, GLsizei depth,
1679                    GLenum glformat)
1680{
1681   mesa_format mesaFormat = _mesa_glenum_to_compressed_format(glformat);
1682   return _mesa_format_image_size(mesaFormat, width, height, depth);
1683}
1684
1685/**
1686 * Verify that a texture format is valid with a particular target
1687 *
1688 * In particular, textures with base format of \c GL_DEPTH_COMPONENT or
1689 * \c GL_DEPTH_STENCIL are only valid with certain, context dependent texture
1690 * targets.
1691 *
1692 * \param ctx             GL context
1693 * \param target          Texture target
1694 * \param internalFormat  Internal format of the texture image
1695 *
1696 * \returns true if the combination is legal, false otherwise.
1697 */
1698bool
1699_mesa_legal_texture_base_format_for_target(struct gl_context *ctx,
1700                                           GLenum target, GLenum internalFormat)
1701{
1702   if (_mesa_base_tex_format(ctx, internalFormat) == GL_DEPTH_COMPONENT
1703       || _mesa_base_tex_format(ctx, internalFormat) == GL_DEPTH_STENCIL
1704       || _mesa_base_tex_format(ctx, internalFormat) == GL_STENCIL_INDEX) {
1705      /* Section 3.8.3 (Texture Image Specification) of the OpenGL 3.3 Core
1706       * Profile spec says:
1707       *
1708       *     "Textures with a base internal format of DEPTH_COMPONENT or
1709       *     DEPTH_STENCIL are supported by texture image specification
1710       *     commands only if target is TEXTURE_1D, TEXTURE_2D,
1711       *     TEXTURE_1D_ARRAY, TEXTURE_2D_ARRAY, TEXTURE_RECTANGLE,
1712       *     TEXTURE_CUBE_MAP, PROXY_TEXTURE_1D, PROXY_TEXTURE_2D,
1713       *     PROXY_TEXTURE_1D_ARRAY, PROXY_TEXTURE_2D_ARRAY,
1714       *     PROXY_TEXTURE_RECTANGLE, or PROXY_TEXTURE_CUBE_MAP. Using these
1715       *     formats in conjunction with any other target will result in an
1716       *     INVALID_OPERATION error."
1717       *
1718       * Cubemaps are only supported with desktop OpenGL version >= 3.0,
1719       * EXT_gpu_shader4, or, on OpenGL ES 2.0+, OES_depth_texture_cube_map.
1720       */
1721      if (target != GL_TEXTURE_1D &&
1722          target != GL_PROXY_TEXTURE_1D &&
1723          target != GL_TEXTURE_2D &&
1724          target != GL_PROXY_TEXTURE_2D &&
1725          target != GL_TEXTURE_1D_ARRAY &&
1726          target != GL_PROXY_TEXTURE_1D_ARRAY &&
1727          target != GL_TEXTURE_2D_ARRAY &&
1728          target != GL_PROXY_TEXTURE_2D_ARRAY &&
1729          target != GL_TEXTURE_RECTANGLE_ARB &&
1730          target != GL_PROXY_TEXTURE_RECTANGLE_ARB &&
1731         !((_mesa_is_cube_face(target) ||
1732            target == GL_TEXTURE_CUBE_MAP ||
1733            target == GL_PROXY_TEXTURE_CUBE_MAP) &&
1734           (ctx->Version >= 30 || ctx->Extensions.EXT_gpu_shader4
1735            || (ctx->API == API_OPENGLES2 && ctx->Extensions.OES_depth_texture_cube_map))) &&
1736          !((target == GL_TEXTURE_CUBE_MAP_ARRAY ||
1737             target == GL_PROXY_TEXTURE_CUBE_MAP_ARRAY) &&
1738            _mesa_has_texture_cube_map_array(ctx))) {
1739         return false;
1740      }
1741   }
1742
1743   return true;
1744}
1745
1746static bool
1747texture_formats_agree(GLenum internalFormat,
1748                      GLenum format)
1749{
1750   GLboolean colorFormat;
1751   GLboolean is_format_depth_or_depthstencil;
1752   GLboolean is_internalFormat_depth_or_depthstencil;
1753
1754   /* Even though there are no color-index textures, we still have to support
1755    * uploading color-index data and remapping it to RGB via the
1756    * GL_PIXEL_MAP_I_TO_[RGBA] tables.
1757    */
1758   const GLboolean indexFormat = (format == GL_COLOR_INDEX);
1759
1760   is_internalFormat_depth_or_depthstencil =
1761      _mesa_is_depth_format(internalFormat) ||
1762      _mesa_is_depthstencil_format(internalFormat);
1763
1764   is_format_depth_or_depthstencil =
1765      _mesa_is_depth_format(format) ||
1766      _mesa_is_depthstencil_format(format);
1767
1768   colorFormat = _mesa_is_color_format(format);
1769
1770   if (_mesa_is_color_format(internalFormat) && !colorFormat && !indexFormat)
1771      return false;
1772
1773   if (is_internalFormat_depth_or_depthstencil !=
1774       is_format_depth_or_depthstencil)
1775      return false;
1776
1777   if (_mesa_is_ycbcr_format(internalFormat) != _mesa_is_ycbcr_format(format))
1778      return false;
1779
1780   return true;
1781}
1782
1783/**
1784 * Test the combination of format, type and internal format arguments of
1785 * different texture operations on GLES.
1786 *
1787 * \param ctx GL context.
1788 * \param format pixel data format given by the user.
1789 * \param type pixel data type given by the user.
1790 * \param internalFormat internal format given by the user.
1791 * \param callerName name of the caller function to print in the error message
1792 *
1793 * \return true if a error is found, false otherwise
1794 *
1795 * Currently, it is used by texture_error_check() and texsubimage_error_check().
1796 */
1797static bool
1798texture_format_error_check_gles(struct gl_context *ctx, GLenum format,
1799                                GLenum type, GLenum internalFormat, const char *callerName)
1800{
1801   GLenum err = _mesa_es3_error_check_format_and_type(ctx, format, type,
1802                                                      internalFormat);
1803   if (err != GL_NO_ERROR) {
1804      _mesa_error(ctx, err,
1805                  "%s(format = %s, type = %s, internalformat = %s)",
1806                  callerName, _mesa_enum_to_string(format),
1807                  _mesa_enum_to_string(type),
1808                  _mesa_enum_to_string(internalFormat));
1809      return true;
1810   }
1811
1812   return false;
1813}
1814
1815/**
1816 * Test the glTexImage[123]D() parameters for errors.
1817 *
1818 * \param ctx GL context.
1819 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1820 * \param target texture target given by the user (already validated).
1821 * \param level image level given by the user.
1822 * \param internalFormat internal format given by the user.
1823 * \param format pixel data format given by the user.
1824 * \param type pixel data type given by the user.
1825 * \param width image width given by the user.
1826 * \param height image height given by the user.
1827 * \param depth image depth given by the user.
1828 * \param border image border given by the user.
1829 *
1830 * \return GL_TRUE if a error is found, GL_FALSE otherwise
1831 *
1832 * Verifies each of the parameters against the constants specified in
1833 * __struct gl_contextRec::Const and the supported extensions, and according
1834 * to the OpenGL specification.
1835 * Note that we don't fully error-check the width, height, depth values
1836 * here.  That's done in _mesa_legal_texture_dimensions() which is used
1837 * by several other GL entrypoints.  Plus, texture dims have a special
1838 * interaction with proxy textures.
1839 */
1840static GLboolean
1841texture_error_check( struct gl_context *ctx,
1842                     GLuint dimensions, GLenum target,
1843                     GLint level, GLint internalFormat,
1844                     GLenum format, GLenum type,
1845                     GLint width, GLint height,
1846                     GLint depth, GLint border,
1847                     const GLvoid *pixels )
1848{
1849   GLenum err;
1850
1851   /* Note: for proxy textures, some error conditions immediately generate
1852    * a GL error in the usual way.  But others do not generate a GL error.
1853    * Instead, they cause the width, height, depth, format fields of the
1854    * texture image to be zeroed-out.  The GL spec seems to indicate that the
1855    * zero-out behaviour is only used in cases related to memory allocation.
1856    */
1857
1858   /* level check */
1859   if (level < 0 || level >= _mesa_max_texture_levels(ctx, target)) {
1860      _mesa_error(ctx, GL_INVALID_VALUE,
1861                  "glTexImage%dD(level=%d)", dimensions, level);
1862      return GL_TRUE;
1863   }
1864
1865   /* Check border */
1866   if (border < 0 || border > 1 ||
1867       ((ctx->API != API_OPENGL_COMPAT ||
1868         target == GL_TEXTURE_RECTANGLE_NV ||
1869         target == GL_PROXY_TEXTURE_RECTANGLE_NV) && border != 0)) {
1870      _mesa_error(ctx, GL_INVALID_VALUE,
1871                  "glTexImage%dD(border=%d)", dimensions, border);
1872      return GL_TRUE;
1873   }
1874
1875   if (width < 0 || height < 0 || depth < 0) {
1876      _mesa_error(ctx, GL_INVALID_VALUE,
1877                  "glTexImage%dD(width, height or depth < 0)", dimensions);
1878      return GL_TRUE;
1879   }
1880
1881   /* Check incoming image format and type */
1882   err = _mesa_error_check_format_and_type(ctx, format, type);
1883   if (err != GL_NO_ERROR) {
1884      /* Prior to OpenGL-ES 2.0, an INVALID_VALUE is expected instead of
1885       * INVALID_ENUM. From page 73 OpenGL ES 1.1 spec:
1886       *
1887       *     "Specifying a value for internalformat that is not one of the
1888       *      above (acceptable) values generates the error INVALID VALUE."
1889       */
1890      if (err == GL_INVALID_ENUM && _mesa_is_gles(ctx) && ctx->Version < 20)
1891         err = GL_INVALID_VALUE;
1892
1893      _mesa_error(ctx, err,
1894                  "glTexImage%dD(incompatible format = %s, type = %s)",
1895                  dimensions, _mesa_enum_to_string(format),
1896                  _mesa_enum_to_string(type));
1897      return GL_TRUE;
1898   }
1899
1900   /* Check internalFormat */
1901   if (_mesa_base_tex_format(ctx, internalFormat) < 0) {
1902      _mesa_error(ctx, GL_INVALID_VALUE,
1903                  "glTexImage%dD(internalFormat=%s)",
1904                  dimensions, _mesa_enum_to_string(internalFormat));
1905      return GL_TRUE;
1906   }
1907
1908   /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
1909    * combinations of format, internalFormat, and type that can be used.
1910    * Formats and types that require additional extensions (e.g., GL_FLOAT
1911    * requires GL_OES_texture_float) are filtered elsewhere.
1912    */
1913   char bufCallerName[20];
1914   _mesa_snprintf(bufCallerName, 20, "glTexImage%dD", dimensions);
1915   if (_mesa_is_gles(ctx) &&
1916       texture_format_error_check_gles(ctx, format, type,
1917                                       internalFormat, bufCallerName)) {
1918      return GL_TRUE;
1919   }
1920
1921   /* validate the bound PBO, if any */
1922   if (!_mesa_validate_pbo_source(ctx, dimensions, &ctx->Unpack,
1923                                  width, height, depth, format, type,
1924                                  INT_MAX, pixels, "glTexImage")) {
1925      return GL_TRUE;
1926   }
1927
1928   /* make sure internal format and format basically agree */
1929   if (!texture_formats_agree(internalFormat, format)) {
1930      _mesa_error(ctx, GL_INVALID_OPERATION,
1931                  "glTexImage%dD(incompatible internalFormat = %s, format = %s)",
1932                  dimensions, _mesa_enum_to_string(internalFormat),
1933                  _mesa_enum_to_string(format));
1934      return GL_TRUE;
1935   }
1936
1937   /* additional checks for ycbcr textures */
1938   if (internalFormat == GL_YCBCR_MESA) {
1939      assert(ctx->Extensions.MESA_ycbcr_texture);
1940      if (type != GL_UNSIGNED_SHORT_8_8_MESA &&
1941          type != GL_UNSIGNED_SHORT_8_8_REV_MESA) {
1942         char message[100];
1943         _mesa_snprintf(message, sizeof(message),
1944                        "glTexImage%dD(format/type YCBCR mismatch)",
1945                        dimensions);
1946         _mesa_error(ctx, GL_INVALID_ENUM, "%s", message);
1947         return GL_TRUE; /* error */
1948      }
1949      if (target != GL_TEXTURE_2D &&
1950          target != GL_PROXY_TEXTURE_2D &&
1951          target != GL_TEXTURE_RECTANGLE_NV &&
1952          target != GL_PROXY_TEXTURE_RECTANGLE_NV) {
1953         _mesa_error(ctx, GL_INVALID_ENUM,
1954                     "glTexImage%dD(bad target for YCbCr texture)",
1955                     dimensions);
1956         return GL_TRUE;
1957      }
1958      if (border != 0) {
1959         char message[100];
1960         _mesa_snprintf(message, sizeof(message),
1961                        "glTexImage%dD(format=GL_YCBCR_MESA and border=%d)",
1962                        dimensions, border);
1963         _mesa_error(ctx, GL_INVALID_VALUE, "%s", message);
1964         return GL_TRUE;
1965      }
1966   }
1967
1968   /* additional checks for depth textures */
1969   if (!_mesa_legal_texture_base_format_for_target(ctx, target, internalFormat)) {
1970      _mesa_error(ctx, GL_INVALID_OPERATION,
1971                  "glTexImage%dD(bad target for texture)", dimensions);
1972      return GL_TRUE;
1973   }
1974
1975   /* additional checks for compressed textures */
1976   if (_mesa_is_compressed_format(ctx, internalFormat)) {
1977      GLenum err;
1978      if (!_mesa_target_can_be_compressed(ctx, target, internalFormat, &err)) {
1979         _mesa_error(ctx, err,
1980                     "glTexImage%dD(target can't be compressed)", dimensions);
1981         return GL_TRUE;
1982      }
1983      if (_mesa_format_no_online_compression(internalFormat)) {
1984         _mesa_error(ctx, GL_INVALID_OPERATION,
1985                     "glTexImage%dD(no compression for format)", dimensions);
1986         return GL_TRUE;
1987      }
1988      if (border != 0) {
1989         _mesa_error(ctx, GL_INVALID_OPERATION,
1990                     "glTexImage%dD(border!=0)", dimensions);
1991         return GL_TRUE;
1992      }
1993   }
1994
1995   /* additional checks for integer textures */
1996   if ((ctx->Version >= 30 || ctx->Extensions.EXT_texture_integer) &&
1997       (_mesa_is_enum_format_integer(format) !=
1998        _mesa_is_enum_format_integer(internalFormat))) {
1999      _mesa_error(ctx, GL_INVALID_OPERATION,
2000                  "glTexImage%dD(integer/non-integer format mismatch)",
2001                  dimensions);
2002      return GL_TRUE;
2003   }
2004
2005   if (!mutable_tex_object(ctx, target)) {
2006      _mesa_error(ctx, GL_INVALID_OPERATION,
2007                  "glTexImage%dD(immutable texture)", dimensions);
2008      return GL_TRUE;
2009   }
2010
2011   /* if we get here, the parameters are OK */
2012   return GL_FALSE;
2013}
2014
2015
2016/**
2017 * Error checking for glCompressedTexImage[123]D().
2018 * Note that the width, height and depth values are not fully error checked
2019 * here.
2020 * \return GL_TRUE if a error is found, GL_FALSE otherwise
2021 */
2022static GLenum
2023compressed_texture_error_check(struct gl_context *ctx, GLint dimensions,
2024                               GLenum target, GLint level,
2025                               GLenum internalFormat, GLsizei width,
2026                               GLsizei height, GLsizei depth, GLint border,
2027                               GLsizei imageSize, const GLvoid *data)
2028{
2029   const GLint maxLevels = _mesa_max_texture_levels(ctx, target);
2030   GLint expectedSize;
2031   GLenum error = GL_NO_ERROR;
2032   char *reason = ""; /* no error */
2033
2034   if (!_mesa_target_can_be_compressed(ctx, target, internalFormat, &error)) {
2035      reason = "target";
2036      goto error;
2037   }
2038
2039   /* This will detect any invalid internalFormat value */
2040   if (!_mesa_is_compressed_format(ctx, internalFormat)) {
2041      _mesa_error(ctx, GL_INVALID_ENUM,
2042                  "glCompressedTexImage%dD(internalFormat=%s)",
2043                  dimensions, _mesa_enum_to_string(internalFormat));
2044      return GL_TRUE;
2045   }
2046
2047   /* validate the bound PBO, if any */
2048   if (!_mesa_validate_pbo_source_compressed(ctx, dimensions, &ctx->Unpack,
2049                                             imageSize, data,
2050                                             "glCompressedTexImage")) {
2051      return GL_TRUE;
2052   }
2053
2054   switch (internalFormat) {
2055   case GL_PALETTE4_RGB8_OES:
2056   case GL_PALETTE4_RGBA8_OES:
2057   case GL_PALETTE4_R5_G6_B5_OES:
2058   case GL_PALETTE4_RGBA4_OES:
2059   case GL_PALETTE4_RGB5_A1_OES:
2060   case GL_PALETTE8_RGB8_OES:
2061   case GL_PALETTE8_RGBA8_OES:
2062   case GL_PALETTE8_R5_G6_B5_OES:
2063   case GL_PALETTE8_RGBA4_OES:
2064   case GL_PALETTE8_RGB5_A1_OES:
2065      /* check level (note that level should be zero or less!) */
2066      if (level > 0 || level < -maxLevels) {
2067         reason = "level";
2068         error = GL_INVALID_VALUE;
2069         goto error;
2070      }
2071
2072      if (dimensions != 2) {
2073         reason = "compressed paletted textures must be 2D";
2074         error = GL_INVALID_OPERATION;
2075         goto error;
2076      }
2077
2078      /* Figure out the expected texture size (in bytes).  This will be
2079       * checked against the actual size later.
2080       */
2081      expectedSize = _mesa_cpal_compressed_size(level, internalFormat,
2082                                                width, height);
2083
2084      /* This is for the benefit of the TestProxyTexImage below.  It expects
2085       * level to be non-negative.  OES_compressed_paletted_texture uses a
2086       * weird mechanism where the level specified to glCompressedTexImage2D
2087       * is -(n-1) number of levels in the texture, and the data specifies the
2088       * complete mipmap stack.  This is done to ensure the palette is the
2089       * same for all levels.
2090       */
2091      level = -level;
2092      break;
2093
2094   default:
2095      /* check level */
2096      if (level < 0 || level >= maxLevels) {
2097         reason = "level";
2098         error = GL_INVALID_VALUE;
2099         goto error;
2100      }
2101
2102      /* Figure out the expected texture size (in bytes).  This will be
2103       * checked against the actual size later.
2104       */
2105      expectedSize = compressed_tex_size(width, height, depth, internalFormat);
2106      break;
2107   }
2108
2109   /* This should really never fail */
2110   if (_mesa_base_tex_format(ctx, internalFormat) < 0) {
2111      reason = "internalFormat";
2112      error = GL_INVALID_ENUM;
2113      goto error;
2114   }
2115
2116   /* No compressed formats support borders at this time */
2117   if (border != 0) {
2118      reason = "border != 0";
2119      error = GL_INVALID_VALUE;
2120      goto error;
2121   }
2122
2123   /* Check for invalid pixel storage modes */
2124   if (!_mesa_compressed_pixel_storage_error_check(ctx, dimensions,
2125                                                   &ctx->Unpack,
2126                                                   "glCompressedTexImage")) {
2127      return GL_FALSE;
2128   }
2129
2130   /* check image size in bytes */
2131   if (expectedSize != imageSize) {
2132      /* Per GL_ARB_texture_compression:  GL_INVALID_VALUE is generated [...]
2133       * if <imageSize> is not consistent with the format, dimensions, and
2134       * contents of the specified image.
2135       */
2136      reason = "imageSize inconsistent with width/height/format";
2137      error = GL_INVALID_VALUE;
2138      goto error;
2139   }
2140
2141   if (!mutable_tex_object(ctx, target)) {
2142      reason = "immutable texture";
2143      error = GL_INVALID_OPERATION;
2144      goto error;
2145   }
2146
2147   return GL_FALSE;
2148
2149error:
2150   /* Note: not all error paths exit through here. */
2151   _mesa_error(ctx, error, "glCompressedTexImage%dD(%s)",
2152               dimensions, reason);
2153   return GL_TRUE;
2154}
2155
2156
2157
2158/**
2159 * Test glTexSubImage[123]D() parameters for errors.
2160 *
2161 * \param ctx GL context.
2162 * \param dimensions texture image dimensions (must be 1, 2 or 3).
2163 * \param target texture target given by the user (already validated)
2164 * \param level image level given by the user.
2165 * \param xoffset sub-image x offset given by the user.
2166 * \param yoffset sub-image y offset given by the user.
2167 * \param zoffset sub-image z offset given by the user.
2168 * \param format pixel data format given by the user.
2169 * \param type pixel data type given by the user.
2170 * \param width image width given by the user.
2171 * \param height image height given by the user.
2172 * \param depth image depth given by the user.
2173 *
2174 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2175 *
2176 * Verifies each of the parameters against the constants specified in
2177 * __struct gl_contextRec::Const and the supported extensions, and according
2178 * to the OpenGL specification.
2179 */
2180static GLboolean
2181texsubimage_error_check(struct gl_context *ctx, GLuint dimensions,
2182                        struct gl_texture_object *texObj,
2183                        GLenum target, GLint level,
2184                        GLint xoffset, GLint yoffset, GLint zoffset,
2185                        GLint width, GLint height, GLint depth,
2186                        GLenum format, GLenum type, const GLvoid *pixels,
2187                        const char *callerName)
2188{
2189   struct gl_texture_image *texImage;
2190   GLenum err;
2191
2192   if (!texObj) {
2193      /* must be out of memory */
2194      _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s()", callerName);
2195      return GL_TRUE;
2196   }
2197
2198   /* level check */
2199   if (level < 0 || level >= _mesa_max_texture_levels(ctx, target)) {
2200      _mesa_error(ctx, GL_INVALID_VALUE, "%s(level=%d)", callerName, level);
2201      return GL_TRUE;
2202   }
2203
2204   if (error_check_subtexture_negative_dimensions(ctx, dimensions,
2205                                                  width, height, depth,
2206                                                  callerName)) {
2207      return GL_TRUE;
2208   }
2209
2210   texImage = _mesa_select_tex_image(texObj, target, level);
2211   if (!texImage) {
2212      /* non-existant texture level */
2213      _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid texture level %d)",
2214                  callerName, level);
2215      return GL_TRUE;
2216   }
2217
2218   err = _mesa_error_check_format_and_type(ctx, format, type);
2219   if (err != GL_NO_ERROR) {
2220      _mesa_error(ctx, err,
2221                  "%s(incompatible format = %s, type = %s)",
2222                  callerName, _mesa_enum_to_string(format),
2223                  _mesa_enum_to_string(type));
2224      return GL_TRUE;
2225   }
2226
2227   GLenum internalFormat = _mesa_is_gles(ctx) ?
2228      oes_float_internal_format(ctx, texImage->InternalFormat, type) :
2229      texImage->InternalFormat;
2230
2231   /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
2232    * combinations of format, internalFormat, and type that can be used.
2233    * Formats and types that require additional extensions (e.g., GL_FLOAT
2234    * requires GL_OES_texture_float) are filtered elsewhere.
2235    */
2236   if (_mesa_is_gles(ctx) &&
2237       texture_format_error_check_gles(ctx, format, type,
2238                                       internalFormat, callerName)) {
2239      return GL_TRUE;
2240   }
2241
2242   /* validate the bound PBO, if any */
2243   if (!_mesa_validate_pbo_source(ctx, dimensions, &ctx->Unpack,
2244                                  width, height, depth, format, type,
2245                                  INT_MAX, pixels, callerName)) {
2246      return GL_TRUE;
2247   }
2248
2249   if (error_check_subtexture_dimensions(ctx, dimensions,
2250                                         texImage, xoffset, yoffset, zoffset,
2251                                         width, height, depth, callerName)) {
2252      return GL_TRUE;
2253   }
2254
2255   if (_mesa_is_format_compressed(texImage->TexFormat)) {
2256      if (_mesa_format_no_online_compression(texImage->InternalFormat)) {
2257         _mesa_error(ctx, GL_INVALID_OPERATION,
2258               "%s(no compression for format)", callerName);
2259         return GL_TRUE;
2260      }
2261   }
2262
2263   if (ctx->Version >= 30 || ctx->Extensions.EXT_texture_integer) {
2264      /* both source and dest must be integer-valued, or neither */
2265      if (_mesa_is_format_integer_color(texImage->TexFormat) !=
2266          _mesa_is_enum_format_integer(format)) {
2267         _mesa_error(ctx, GL_INVALID_OPERATION,
2268                     "%s(integer/non-integer format mismatch)", callerName);
2269         return GL_TRUE;
2270      }
2271   }
2272
2273   return GL_FALSE;
2274}
2275
2276
2277/**
2278 * Test glCopyTexImage[12]D() parameters for errors.
2279 *
2280 * \param ctx GL context.
2281 * \param dimensions texture image dimensions (must be 1, 2 or 3).
2282 * \param target texture target given by the user.
2283 * \param level image level given by the user.
2284 * \param internalFormat internal format given by the user.
2285 * \param width image width given by the user.
2286 * \param height image height given by the user.
2287 * \param border texture border.
2288 *
2289 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2290 *
2291 * Verifies each of the parameters against the constants specified in
2292 * __struct gl_contextRec::Const and the supported extensions, and according
2293 * to the OpenGL specification.
2294 */
2295static GLboolean
2296copytexture_error_check( struct gl_context *ctx, GLuint dimensions,
2297                         GLenum target, GLint level, GLint internalFormat,
2298                         GLint border )
2299{
2300   GLint baseFormat;
2301   GLint rb_base_format;
2302   struct gl_renderbuffer *rb;
2303   GLenum rb_internal_format;
2304
2305   /* check target */
2306   if (!legal_texsubimage_target(ctx, dimensions, target, false)) {
2307      _mesa_error(ctx, GL_INVALID_ENUM, "glCopyTexImage%uD(target=%s)",
2308                  dimensions, _mesa_enum_to_string(target));
2309      return GL_TRUE;
2310   }
2311
2312   /* level check */
2313   if (level < 0 || level >= _mesa_max_texture_levels(ctx, target)) {
2314      _mesa_error(ctx, GL_INVALID_VALUE,
2315                  "glCopyTexImage%dD(level=%d)", dimensions, level);
2316      return GL_TRUE;
2317   }
2318
2319   /* Check that the source buffer is complete */
2320   if (_mesa_is_user_fbo(ctx->ReadBuffer)) {
2321      if (ctx->ReadBuffer->_Status == 0) {
2322         _mesa_test_framebuffer_completeness(ctx, ctx->ReadBuffer);
2323      }
2324      if (ctx->ReadBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
2325         _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
2326                     "glCopyTexImage%dD(invalid readbuffer)", dimensions);
2327         return GL_TRUE;
2328      }
2329
2330      if (ctx->ReadBuffer->Visual.samples > 0) {
2331         _mesa_error(ctx, GL_INVALID_OPERATION,
2332                     "glCopyTexImage%dD(multisample FBO)", dimensions);
2333         return GL_TRUE;
2334      }
2335   }
2336
2337   /* Check border */
2338   if (border < 0 || border > 1 ||
2339       ((ctx->API != API_OPENGL_COMPAT ||
2340         target == GL_TEXTURE_RECTANGLE_NV ||
2341         target == GL_PROXY_TEXTURE_RECTANGLE_NV) && border != 0)) {
2342      _mesa_error(ctx, GL_INVALID_VALUE,
2343                  "glCopyTexImage%dD(border=%d)", dimensions, border);
2344      return GL_TRUE;
2345   }
2346
2347   /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
2348    * internalFormat.
2349    */
2350   if (_mesa_is_gles(ctx) && !_mesa_is_gles3(ctx)) {
2351      switch (internalFormat) {
2352      case GL_ALPHA:
2353      case GL_RGB:
2354      case GL_RGBA:
2355      case GL_LUMINANCE:
2356      case GL_LUMINANCE_ALPHA:
2357         break;
2358      default:
2359         _mesa_error(ctx, GL_INVALID_ENUM,
2360                     "glCopyTexImage%dD(internalFormat=%s)", dimensions,
2361                     _mesa_enum_to_string(internalFormat));
2362         return GL_TRUE;
2363      }
2364   } else {
2365      /*
2366       * Section 8.6 (Alternate Texture Image Specification Commands) of the
2367       * OpenGL 4.5 (Compatibility Profile) spec says:
2368       *
2369       *     "Parameters level, internalformat, and border are specified using
2370       *     the same values, with the same meanings, as the corresponding
2371       *     arguments of TexImage2D, except that internalformat may not be
2372       *     specified as 1, 2, 3, or 4."
2373       */
2374      if (internalFormat >= 1 && internalFormat <= 4) {
2375         _mesa_error(ctx, GL_INVALID_ENUM,
2376                     "glCopyTexImage%dD(internalFormat=%d)", dimensions,
2377                     internalFormat);
2378         return GL_TRUE;
2379      }
2380   }
2381
2382   baseFormat = _mesa_base_tex_format(ctx, internalFormat);
2383   if (baseFormat < 0) {
2384      _mesa_error(ctx, GL_INVALID_ENUM,
2385                  "glCopyTexImage%dD(internalFormat=%s)", dimensions,
2386                  _mesa_enum_to_string(internalFormat));
2387      return GL_TRUE;
2388   }
2389
2390   rb = _mesa_get_read_renderbuffer_for_format(ctx, internalFormat);
2391   if (rb == NULL) {
2392      _mesa_error(ctx, GL_INVALID_OPERATION,
2393                  "glCopyTexImage%dD(read buffer)", dimensions);
2394      return GL_TRUE;
2395   }
2396
2397   rb_internal_format = rb->InternalFormat;
2398   rb_base_format = _mesa_base_tex_format(ctx, rb->InternalFormat);
2399   if (_mesa_is_color_format(internalFormat)) {
2400      if (rb_base_format < 0) {
2401         _mesa_error(ctx, GL_INVALID_VALUE,
2402                     "glCopyTexImage%dD(internalFormat=%s)", dimensions,
2403                     _mesa_enum_to_string(internalFormat));
2404         return GL_TRUE;
2405      }
2406   }
2407
2408   if (_mesa_is_gles(ctx)) {
2409      bool valid = true;
2410      if (_mesa_components_in_format(baseFormat) >
2411          _mesa_components_in_format(rb_base_format)) {
2412         valid = false;
2413      }
2414      if (baseFormat == GL_DEPTH_COMPONENT ||
2415          baseFormat == GL_DEPTH_STENCIL ||
2416          baseFormat == GL_STENCIL_INDEX ||
2417          rb_base_format == GL_DEPTH_COMPONENT ||
2418          rb_base_format == GL_DEPTH_STENCIL ||
2419          rb_base_format == GL_STENCIL_INDEX ||
2420          ((baseFormat == GL_LUMINANCE_ALPHA ||
2421            baseFormat == GL_ALPHA) &&
2422           rb_base_format != GL_RGBA) ||
2423          internalFormat == GL_RGB9_E5) {
2424         valid = false;
2425      }
2426      if (internalFormat == GL_RGB9_E5) {
2427         valid = false;
2428      }
2429      if (!valid) {
2430         _mesa_error(ctx, GL_INVALID_OPERATION,
2431                     "glCopyTexImage%dD(internalFormat=%s)", dimensions,
2432                     _mesa_enum_to_string(internalFormat));
2433         return GL_TRUE;
2434      }
2435   }
2436
2437   if (_mesa_is_gles3(ctx)) {
2438      bool rb_is_srgb = false;
2439      bool dst_is_srgb = false;
2440
2441      if (ctx->Extensions.EXT_framebuffer_sRGB &&
2442          _mesa_get_format_color_encoding(rb->Format) == GL_SRGB) {
2443         rb_is_srgb = true;
2444      }
2445
2446      if (_mesa_get_linear_internalformat(internalFormat) != internalFormat) {
2447         dst_is_srgb = true;
2448      }
2449
2450      if (rb_is_srgb != dst_is_srgb) {
2451         /* Page 137 (page 149 of the PDF) in section 3.8.5 of the
2452          * OpenGLES 3.0.0 spec says:
2453          *
2454          *     "The error INVALID_OPERATION is also generated if the
2455          *     value of FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING for the
2456          *     framebuffer attachment corresponding to the read buffer
2457          *     is LINEAR (see section 6.1.13) and internalformat is
2458          *     one of the sRGB formats described in section 3.8.16, or
2459          *     if the value of FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING is
2460          *     SRGB and internalformat is not one of the sRGB formats."
2461          */
2462         _mesa_error(ctx, GL_INVALID_OPERATION,
2463                     "glCopyTexImage%dD(srgb usage mismatch)", dimensions);
2464         return GL_TRUE;
2465      }
2466
2467      /* Page 139, Table 3.15 of OpenGL ES 3.0 spec does not define ReadPixels
2468       * types for SNORM formats. Also, conversion to SNORM formats is not
2469       * allowed by Table 3.2 on Page 110.
2470       */
2471      if (!_mesa_has_EXT_render_snorm(ctx) &&
2472          _mesa_is_enum_format_snorm(internalFormat)) {
2473         _mesa_error(ctx, GL_INVALID_OPERATION,
2474                     "glCopyTexImage%dD(internalFormat=%s)", dimensions,
2475                     _mesa_enum_to_string(internalFormat));
2476         return GL_TRUE;
2477      }
2478   }
2479
2480   if (!_mesa_source_buffer_exists(ctx, baseFormat)) {
2481      _mesa_error(ctx, GL_INVALID_OPERATION,
2482                  "glCopyTexImage%dD(missing readbuffer)", dimensions);
2483      return GL_TRUE;
2484   }
2485
2486   /* From the EXT_texture_integer spec:
2487    *
2488    *     "INVALID_OPERATION is generated by CopyTexImage* and CopyTexSubImage*
2489    *      if the texture internalformat is an integer format and the read color
2490    *      buffer is not an integer format, or if the internalformat is not an
2491    *      integer format and the read color buffer is an integer format."
2492    */
2493   if (_mesa_is_color_format(internalFormat)) {
2494      bool is_int = _mesa_is_enum_format_integer(internalFormat);
2495      bool is_rbint = _mesa_is_enum_format_integer(rb_internal_format);
2496      bool is_unorm = _mesa_is_enum_format_unorm(internalFormat);
2497      bool is_rbunorm = _mesa_is_enum_format_unorm(rb_internal_format);
2498      if (is_int || is_rbint) {
2499         if (is_int != is_rbint) {
2500            _mesa_error(ctx, GL_INVALID_OPERATION,
2501                        "glCopyTexImage%dD(integer vs non-integer)", dimensions);
2502            return GL_TRUE;
2503         } else if (_mesa_is_gles(ctx) &&
2504                    _mesa_is_enum_format_unsigned_int(internalFormat) !=
2505                      _mesa_is_enum_format_unsigned_int(rb_internal_format)) {
2506            _mesa_error(ctx, GL_INVALID_OPERATION,
2507                        "glCopyTexImage%dD(signed vs unsigned integer)",
2508                        dimensions);
2509            return GL_TRUE;
2510         }
2511      }
2512
2513      /* From page 138 of OpenGL ES 3.0 spec:
2514       *    "The error INVALID_OPERATION is generated if floating-point RGBA
2515       *    data is required; if signed integer RGBA data is required and the
2516       *    format of the current color buffer is not signed integer; if
2517       *    unsigned integer RGBA data is required and the format of the
2518       *    current color buffer is not unsigned integer; or if fixed-point
2519       *    RGBA data is required and the format of the current color buffer
2520       *    is not fixed-point.
2521       */
2522      if (_mesa_is_gles(ctx) && is_unorm != is_rbunorm)
2523            _mesa_error(ctx, GL_INVALID_OPERATION,
2524                        "glCopyTexImage%dD(unorm vs non-unorm)", dimensions);
2525   }
2526
2527   if (_mesa_is_compressed_format(ctx, internalFormat)) {
2528      GLenum err;
2529      if (!_mesa_target_can_be_compressed(ctx, target, internalFormat, &err)) {
2530         _mesa_error(ctx, err,
2531                     "glCopyTexImage%dD(target can't be compressed)", dimensions);
2532         return GL_TRUE;
2533      }
2534      if (_mesa_format_no_online_compression(internalFormat)) {
2535         _mesa_error(ctx, GL_INVALID_OPERATION,
2536               "glCopyTexImage%dD(no compression for format)", dimensions);
2537         return GL_TRUE;
2538      }
2539      if (border != 0) {
2540         _mesa_error(ctx, GL_INVALID_OPERATION,
2541                     "glCopyTexImage%dD(border!=0)", dimensions);
2542         return GL_TRUE;
2543      }
2544   }
2545
2546   if (!mutable_tex_object(ctx, target)) {
2547      _mesa_error(ctx, GL_INVALID_OPERATION,
2548                  "glCopyTexImage%dD(immutable texture)", dimensions);
2549      return GL_TRUE;
2550   }
2551
2552   /* if we get here, the parameters are OK */
2553   return GL_FALSE;
2554}
2555
2556
2557/**
2558 * Test glCopyTexSubImage[12]D() parameters for errors.
2559 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2560 */
2561static GLboolean
2562copytexsubimage_error_check(struct gl_context *ctx, GLuint dimensions,
2563                            const struct gl_texture_object *texObj,
2564                            GLenum target, GLint level,
2565                            GLint xoffset, GLint yoffset, GLint zoffset,
2566                            GLint width, GLint height, const char *caller)
2567{
2568   assert(texObj);
2569
2570   struct gl_texture_image *texImage;
2571
2572   /* Check that the source buffer is complete */
2573   if (_mesa_is_user_fbo(ctx->ReadBuffer)) {
2574      if (ctx->ReadBuffer->_Status == 0) {
2575         _mesa_test_framebuffer_completeness(ctx, ctx->ReadBuffer);
2576      }
2577      if (ctx->ReadBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
2578         _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
2579                     "%s(invalid readbuffer)", caller);
2580         return GL_TRUE;
2581      }
2582
2583      if (ctx->ReadBuffer->Visual.samples > 0) {
2584         _mesa_error(ctx, GL_INVALID_OPERATION,
2585                "%s(multisample FBO)", caller);
2586         return GL_TRUE;
2587      }
2588   }
2589
2590   /* Check level */
2591   if (level < 0 || level >= _mesa_max_texture_levels(ctx, target)) {
2592      _mesa_error(ctx, GL_INVALID_VALUE, "%s(level=%d)", caller, level);
2593      return GL_TRUE;
2594   }
2595
2596   texImage = _mesa_select_tex_image(texObj, target, level);
2597   if (!texImage) {
2598      /* destination image does not exist */
2599      _mesa_error(ctx, GL_INVALID_OPERATION,
2600                  "%s(invalid texture level %d)", caller, level);
2601      return GL_TRUE;
2602   }
2603
2604   if (error_check_subtexture_negative_dimensions(ctx, dimensions,
2605                                                  width, height, 1, caller)) {
2606      return GL_TRUE;
2607   }
2608
2609   if (error_check_subtexture_dimensions(ctx, dimensions, texImage,
2610                                         xoffset, yoffset, zoffset,
2611                                         width, height, 1, caller)) {
2612      return GL_TRUE;
2613   }
2614
2615   if (_mesa_is_format_compressed(texImage->TexFormat)) {
2616      if (_mesa_format_no_online_compression(texImage->InternalFormat)) {
2617         _mesa_error(ctx, GL_INVALID_OPERATION,
2618               "%s(no compression for format)", caller);
2619         return GL_TRUE;
2620      }
2621   }
2622
2623   if (texImage->InternalFormat == GL_YCBCR_MESA) {
2624      _mesa_error(ctx, GL_INVALID_OPERATION, "%s()", caller);
2625      return GL_TRUE;
2626   }
2627
2628   /* From OpenGL ES 3.2 spec, section 8.6:
2629    *
2630    *     "An INVALID_OPERATION error is generated by CopyTexSubImage3D,
2631    *      CopyTexImage2D, or CopyTexSubImage2D if the internalformat of the
2632    *      texture image being (re)specified is RGB9_E5"
2633    */
2634   if (texImage->InternalFormat == GL_RGB9_E5 &&
2635       !_mesa_is_desktop_gl(ctx)) {
2636      _mesa_error(ctx, GL_INVALID_OPERATION,
2637                  "%s(invalid internal format %s)", caller,
2638                  _mesa_enum_to_string(texImage->InternalFormat));
2639      return GL_TRUE;
2640   }
2641
2642   if (!_mesa_source_buffer_exists(ctx, texImage->_BaseFormat)) {
2643      _mesa_error(ctx, GL_INVALID_OPERATION,
2644                  "%s(missing readbuffer, format=%s)", caller,
2645                  _mesa_enum_to_string(texImage->_BaseFormat));
2646      return GL_TRUE;
2647   }
2648
2649   /* From the EXT_texture_integer spec:
2650    *
2651    *     "INVALID_OPERATION is generated by CopyTexImage* and
2652    *     CopyTexSubImage* if the texture internalformat is an integer format
2653    *     and the read color buffer is not an integer format, or if the
2654    *     internalformat is not an integer format and the read color buffer
2655    *     is an integer format."
2656    */
2657   if (_mesa_is_color_format(texImage->InternalFormat)) {
2658      struct gl_renderbuffer *rb = ctx->ReadBuffer->_ColorReadBuffer;
2659
2660      if (_mesa_is_format_integer_color(rb->Format) !=
2661          _mesa_is_format_integer_color(texImage->TexFormat)) {
2662         _mesa_error(ctx, GL_INVALID_OPERATION,
2663                     "%s(integer vs non-integer)", caller);
2664         return GL_TRUE;
2665      }
2666   }
2667
2668   /* In the ES 3.2 specification's Table 8.13 (Valid CopyTexImage source
2669    * framebuffer/destination texture base internal format combinations),
2670    * all the entries for stencil are left blank (unsupported).
2671    */
2672   if (_mesa_is_gles(ctx) && _mesa_is_stencil_format(texImage->_BaseFormat)) {
2673      _mesa_error(ctx, GL_INVALID_OPERATION, "%s(stencil disallowed)", caller);
2674      return GL_TRUE;
2675   }
2676
2677   /* if we get here, the parameters are OK */
2678   return GL_FALSE;
2679}
2680
2681
2682/** Callback info for walking over FBO hash table */
2683struct cb_info
2684{
2685   struct gl_context *ctx;
2686   struct gl_texture_object *texObj;
2687   GLuint level, face;
2688};
2689
2690
2691/**
2692 * Check render to texture callback.  Called from _mesa_HashWalk().
2693 */
2694static void
2695check_rtt_cb(UNUSED GLuint key, void *data, void *userData)
2696{
2697   struct gl_framebuffer *fb = (struct gl_framebuffer *) data;
2698   const struct cb_info *info = (struct cb_info *) userData;
2699   struct gl_context *ctx = info->ctx;
2700   const struct gl_texture_object *texObj = info->texObj;
2701   const GLuint level = info->level, face = info->face;
2702
2703   /* If this is a user-created FBO */
2704   if (_mesa_is_user_fbo(fb)) {
2705      GLuint i;
2706      /* check if any of the FBO's attachments point to 'texObj' */
2707      for (i = 0; i < BUFFER_COUNT; i++) {
2708         struct gl_renderbuffer_attachment *att = fb->Attachment + i;
2709         if (att->Type == GL_TEXTURE &&
2710             att->Texture == texObj &&
2711             att->TextureLevel == level &&
2712             att->CubeMapFace == face) {
2713            _mesa_update_texture_renderbuffer(ctx, fb, att);
2714            assert(att->Renderbuffer->TexImage);
2715            /* Mark fb status as indeterminate to force re-validation */
2716            fb->_Status = 0;
2717
2718            /* Make sure that the revalidation actually happens if this is
2719             * being done to currently-bound buffers.
2720             */
2721            if (fb == ctx->DrawBuffer || fb == ctx->ReadBuffer)
2722               ctx->NewState |= _NEW_BUFFERS;
2723         }
2724      }
2725   }
2726}
2727
2728
2729/**
2730 * When a texture image is specified we have to check if it's bound to
2731 * any framebuffer objects (render to texture) in order to detect changes
2732 * in size or format since that effects FBO completeness.
2733 * Any FBOs rendering into the texture must be re-validated.
2734 */
2735void
2736_mesa_update_fbo_texture(struct gl_context *ctx,
2737                         struct gl_texture_object *texObj,
2738                         GLuint face, GLuint level)
2739{
2740   /* Only check this texture if it's been marked as RenderToTexture */
2741   if (texObj->_RenderToTexture) {
2742      struct cb_info info;
2743      info.ctx = ctx;
2744      info.texObj = texObj;
2745      info.level = level;
2746      info.face = face;
2747      _mesa_HashWalk(ctx->Shared->FrameBuffers, check_rtt_cb, &info);
2748   }
2749}
2750
2751
2752/**
2753 * If the texture object's GenerateMipmap flag is set and we've
2754 * changed the texture base level image, regenerate the rest of the
2755 * mipmap levels now.
2756 */
2757static inline void
2758check_gen_mipmap(struct gl_context *ctx, GLenum target,
2759                 struct gl_texture_object *texObj, GLint level)
2760{
2761   if (texObj->GenerateMipmap &&
2762       level == texObj->BaseLevel &&
2763       level < texObj->MaxLevel) {
2764      assert(ctx->Driver.GenerateMipmap);
2765      ctx->Driver.GenerateMipmap(ctx, target, texObj);
2766   }
2767}
2768
2769
2770/** Debug helper: override the user-requested internal format */
2771static GLenum
2772override_internal_format(GLenum internalFormat, UNUSED GLint width,
2773                         UNUSED GLint height)
2774{
2775#if 0
2776   if (internalFormat == GL_RGBA16F_ARB ||
2777       internalFormat == GL_RGBA32F_ARB) {
2778      printf("Convert rgba float tex to int %d x %d\n", width, height);
2779      return GL_RGBA;
2780   }
2781   else if (internalFormat == GL_RGB16F_ARB ||
2782            internalFormat == GL_RGB32F_ARB) {
2783      printf("Convert rgb float tex to int %d x %d\n", width, height);
2784      return GL_RGB;
2785   }
2786   else if (internalFormat == GL_LUMINANCE_ALPHA16F_ARB ||
2787            internalFormat == GL_LUMINANCE_ALPHA32F_ARB) {
2788      printf("Convert luminance float tex to int %d x %d\n", width, height);
2789      return GL_LUMINANCE_ALPHA;
2790   }
2791   else if (internalFormat == GL_LUMINANCE16F_ARB ||
2792            internalFormat == GL_LUMINANCE32F_ARB) {
2793      printf("Convert luminance float tex to int %d x %d\n", width, height);
2794      return GL_LUMINANCE;
2795   }
2796   else if (internalFormat == GL_ALPHA16F_ARB ||
2797            internalFormat == GL_ALPHA32F_ARB) {
2798      printf("Convert luminance float tex to int %d x %d\n", width, height);
2799      return GL_ALPHA;
2800   }
2801   /*
2802   else if (internalFormat == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT) {
2803      internalFormat = GL_RGBA;
2804   }
2805   */
2806   else {
2807      return internalFormat;
2808   }
2809#else
2810   return internalFormat;
2811#endif
2812}
2813
2814
2815/**
2816 * Choose the actual hardware format for a texture image.
2817 * Try to use the same format as the previous image level when possible.
2818 * Otherwise, ask the driver for the best format.
2819 * It's important to try to choose a consistant format for all levels
2820 * for efficient texture memory layout/allocation.  In particular, this
2821 * comes up during automatic mipmap generation.
2822 */
2823mesa_format
2824_mesa_choose_texture_format(struct gl_context *ctx,
2825                            struct gl_texture_object *texObj,
2826                            GLenum target, GLint level,
2827                            GLenum internalFormat, GLenum format, GLenum type)
2828{
2829   mesa_format f;
2830
2831   /* see if we've already chosen a format for the previous level */
2832   if (level > 0) {
2833      struct gl_texture_image *prevImage =
2834         _mesa_select_tex_image(texObj, target, level - 1);
2835      /* See if the prev level is defined and has an internal format which
2836       * matches the new internal format.
2837       */
2838      if (prevImage &&
2839          prevImage->Width > 0 &&
2840          prevImage->InternalFormat == internalFormat) {
2841         /* use the same format */
2842         assert(prevImage->TexFormat != MESA_FORMAT_NONE);
2843         return prevImage->TexFormat;
2844      }
2845   }
2846
2847   f = ctx->Driver.ChooseTextureFormat(ctx, target, internalFormat,
2848                                       format, type);
2849   assert(f != MESA_FORMAT_NONE);
2850   return f;
2851}
2852
2853
2854/**
2855 * Adjust pixel unpack params and image dimensions to strip off the
2856 * one-pixel texture border.
2857 *
2858 * Gallium and intel don't support texture borders.  They've seldem been used
2859 * and seldom been implemented correctly anyway.
2860 *
2861 * \param unpackNew returns the new pixel unpack parameters
2862 */
2863static void
2864strip_texture_border(GLenum target,
2865                     GLint *width, GLint *height, GLint *depth,
2866                     const struct gl_pixelstore_attrib *unpack,
2867                     struct gl_pixelstore_attrib *unpackNew)
2868{
2869   assert(width);
2870   assert(height);
2871   assert(depth);
2872
2873   *unpackNew = *unpack;
2874
2875   if (unpackNew->RowLength == 0)
2876      unpackNew->RowLength = *width;
2877
2878   if (unpackNew->ImageHeight == 0)
2879      unpackNew->ImageHeight = *height;
2880
2881   assert(*width >= 3);
2882   unpackNew->SkipPixels++;  /* skip the border */
2883   *width = *width - 2;      /* reduce the width by two border pixels */
2884
2885   /* The min height of a texture with a border is 3 */
2886   if (*height >= 3 && target != GL_TEXTURE_1D_ARRAY) {
2887      unpackNew->SkipRows++;  /* skip the border */
2888      *height = *height - 2;  /* reduce the height by two border pixels */
2889   }
2890
2891   if (*depth >= 3 &&
2892       target != GL_TEXTURE_2D_ARRAY &&
2893       target != GL_TEXTURE_CUBE_MAP_ARRAY) {
2894      unpackNew->SkipImages++;  /* skip the border */
2895      *depth = *depth - 2;      /* reduce the depth by two border pixels */
2896   }
2897}
2898
2899
2900/**
2901 * Common code to implement all the glTexImage1D/2D/3D functions
2902 * as well as glCompressedTexImage1D/2D/3D.
2903 * \param compressed  only GL_TRUE for glCompressedTexImage1D/2D/3D calls.
2904 * \param format  the user's image format (only used if !compressed)
2905 * \param type  the user's image type (only used if !compressed)
2906 * \param imageSize  only used for glCompressedTexImage1D/2D/3D calls.
2907 */
2908static ALWAYS_INLINE void
2909teximage(struct gl_context *ctx, GLboolean compressed, GLuint dims,
2910         GLenum target, GLint level, GLint internalFormat,
2911         GLsizei width, GLsizei height, GLsizei depth,
2912         GLint border, GLenum format, GLenum type,
2913         GLsizei imageSize, const GLvoid *pixels, bool no_error)
2914{
2915   const char *func = compressed ? "glCompressedTexImage" : "glTexImage";
2916   struct gl_pixelstore_attrib unpack_no_border;
2917   const struct gl_pixelstore_attrib *unpack = &ctx->Unpack;
2918   struct gl_texture_object *texObj;
2919   mesa_format texFormat;
2920   bool dimensionsOK = true, sizeOK = true;
2921
2922   FLUSH_VERTICES(ctx, 0);
2923
2924   if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE)) {
2925      if (compressed)
2926         _mesa_debug(ctx,
2927                     "glCompressedTexImage%uD %s %d %s %d %d %d %d %p\n",
2928                     dims,
2929                     _mesa_enum_to_string(target), level,
2930                     _mesa_enum_to_string(internalFormat),
2931                     width, height, depth, border, pixels);
2932      else
2933         _mesa_debug(ctx,
2934                     "glTexImage%uD %s %d %s %d %d %d %d %s %s %p\n",
2935                     dims,
2936                     _mesa_enum_to_string(target), level,
2937                     _mesa_enum_to_string(internalFormat),
2938                     width, height, depth, border,
2939                     _mesa_enum_to_string(format),
2940                     _mesa_enum_to_string(type), pixels);
2941   }
2942
2943   internalFormat = override_internal_format(internalFormat, width, height);
2944
2945   if (!no_error) {
2946      /* target error checking */
2947      if (!legal_teximage_target(ctx, dims, target)) {
2948         _mesa_error(ctx, GL_INVALID_ENUM, "%s%uD(target=%s)",
2949                     func, dims, _mesa_enum_to_string(target));
2950         return;
2951      }
2952
2953      /* general error checking */
2954      if (compressed) {
2955         if (compressed_texture_error_check(ctx, dims, target, level,
2956                                            internalFormat,
2957                                            width, height, depth,
2958                                            border, imageSize, pixels))
2959            return;
2960      } else {
2961         if (texture_error_check(ctx, dims, target, level, internalFormat,
2962                                 format, type, width, height, depth, border,
2963                                 pixels))
2964            return;
2965      }
2966   }
2967
2968   /* Here we convert a cpal compressed image into a regular glTexImage2D
2969    * call by decompressing the texture.  If we really want to support cpal
2970    * textures in any driver this would have to be changed.
2971    */
2972   if (ctx->API == API_OPENGLES && compressed && dims == 2) {
2973      switch (internalFormat) {
2974      case GL_PALETTE4_RGB8_OES:
2975      case GL_PALETTE4_RGBA8_OES:
2976      case GL_PALETTE4_R5_G6_B5_OES:
2977      case GL_PALETTE4_RGBA4_OES:
2978      case GL_PALETTE4_RGB5_A1_OES:
2979      case GL_PALETTE8_RGB8_OES:
2980      case GL_PALETTE8_RGBA8_OES:
2981      case GL_PALETTE8_R5_G6_B5_OES:
2982      case GL_PALETTE8_RGBA4_OES:
2983      case GL_PALETTE8_RGB5_A1_OES:
2984         _mesa_cpal_compressed_teximage2d(target, level, internalFormat,
2985                                          width, height, imageSize, pixels);
2986         return;
2987      }
2988   }
2989
2990   texObj = _mesa_get_current_tex_object(ctx, target);
2991   assert(texObj);
2992
2993   if (compressed) {
2994      /* For glCompressedTexImage() the driver has no choice about the
2995       * texture format since we'll never transcode the user's compressed
2996       * image data.  The internalFormat was error checked earlier.
2997       */
2998      texFormat = _mesa_glenum_to_compressed_format(internalFormat);
2999   }
3000   else {
3001      /* In case of HALF_FLOAT_OES or FLOAT_OES, find corresponding sized
3002       * internal floating point format for the given base format.
3003       */
3004      if (_mesa_is_gles(ctx) && format == internalFormat) {
3005         if (type == GL_FLOAT) {
3006            texObj->_IsFloat = GL_TRUE;
3007         } else if (type == GL_HALF_FLOAT_OES || type == GL_HALF_FLOAT) {
3008            texObj->_IsHalfFloat = GL_TRUE;
3009         }
3010
3011         internalFormat = adjust_for_oes_float_texture(ctx, format, type);
3012      }
3013
3014      texFormat = _mesa_choose_texture_format(ctx, texObj, target, level,
3015                                              internalFormat, format, type);
3016   }
3017
3018   assert(texFormat != MESA_FORMAT_NONE);
3019
3020   if (!no_error) {
3021      /* check that width, height, depth are legal for the mipmap level */
3022      dimensionsOK = _mesa_legal_texture_dimensions(ctx, target, level, width,
3023                                                    height, depth, border);
3024
3025      /* check that the texture won't take too much memory, etc */
3026      sizeOK = ctx->Driver.TestProxyTexImage(ctx, proxy_target(target),
3027                                             0, level, texFormat, 1,
3028                                             width, height, depth);
3029   }
3030
3031   if (_mesa_is_proxy_texture(target)) {
3032      /* Proxy texture: just clear or set state depending on error checking */
3033      struct gl_texture_image *texImage =
3034         get_proxy_tex_image(ctx, target, level);
3035
3036      if (!texImage)
3037         return;  /* GL_OUT_OF_MEMORY already recorded */
3038
3039      if (dimensionsOK && sizeOK) {
3040         _mesa_init_teximage_fields(ctx, texImage, width, height, depth,
3041                                    border, internalFormat, texFormat);
3042      }
3043      else {
3044         clear_teximage_fields(texImage);
3045      }
3046   }
3047   else {
3048      /* non-proxy target */
3049      const GLuint face = _mesa_tex_target_to_face(target);
3050      struct gl_texture_image *texImage;
3051
3052      if (!dimensionsOK) {
3053         _mesa_error(ctx, GL_INVALID_VALUE,
3054                     "%s%uD(invalid width=%d or height=%d or depth=%d)",
3055                     func, dims, width, height, depth);
3056         return;
3057      }
3058
3059      if (!sizeOK) {
3060         _mesa_error(ctx, GL_OUT_OF_MEMORY,
3061                     "%s%uD(image too large: %d x %d x %d, %s format)",
3062                     func, dims, width, height, depth,
3063                     _mesa_enum_to_string(internalFormat));
3064         return;
3065      }
3066
3067      /* Allow a hardware driver to just strip out the border, to provide
3068       * reliable but slightly incorrect hardware rendering instead of
3069       * rarely-tested software fallback rendering.
3070       */
3071      if (border && ctx->Const.StripTextureBorder) {
3072         strip_texture_border(target, &width, &height, &depth, unpack,
3073                              &unpack_no_border);
3074         border = 0;
3075         unpack = &unpack_no_border;
3076      }
3077
3078      if (ctx->NewState & _NEW_PIXEL)
3079         _mesa_update_state(ctx);
3080
3081      _mesa_lock_texture(ctx, texObj);
3082      {
3083         texImage = _mesa_get_tex_image(ctx, texObj, target, level);
3084
3085         if (!texImage) {
3086            _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s%uD", func, dims);
3087         }
3088         else {
3089            ctx->Driver.FreeTextureImageBuffer(ctx, texImage);
3090
3091            _mesa_init_teximage_fields(ctx, texImage,
3092                                       width, height, depth,
3093                                       border, internalFormat, texFormat);
3094
3095            /* Give the texture to the driver.  <pixels> may be null. */
3096            if (width > 0 && height > 0 && depth > 0) {
3097               if (compressed) {
3098                  ctx->Driver.CompressedTexImage(ctx, dims, texImage,
3099                                                 imageSize, pixels);
3100               }
3101               else {
3102                  ctx->Driver.TexImage(ctx, dims, texImage, format,
3103                                       type, pixels, unpack);
3104               }
3105            }
3106
3107            check_gen_mipmap(ctx, target, texObj, level);
3108
3109            _mesa_update_fbo_texture(ctx, texObj, face, level);
3110
3111            _mesa_dirty_texobj(ctx, texObj);
3112         }
3113      }
3114      _mesa_unlock_texture(ctx, texObj);
3115   }
3116}
3117
3118
3119/* This is a wrapper around teximage() so that we can force the KHR_no_error
3120 * logic to be inlined without inlining the function into all the callers.
3121 */
3122static void
3123teximage_err(struct gl_context *ctx, GLboolean compressed, GLuint dims,
3124             GLenum target, GLint level, GLint internalFormat,
3125             GLsizei width, GLsizei height, GLsizei depth,
3126             GLint border, GLenum format, GLenum type,
3127             GLsizei imageSize, const GLvoid *pixels)
3128{
3129   teximage(ctx, compressed, dims, target, level, internalFormat, width, height,
3130            depth, border, format, type, imageSize, pixels, false);
3131}
3132
3133
3134static void
3135teximage_no_error(struct gl_context *ctx, GLboolean compressed, GLuint dims,
3136                  GLenum target, GLint level, GLint internalFormat,
3137                  GLsizei width, GLsizei height, GLsizei depth,
3138                  GLint border, GLenum format, GLenum type,
3139                  GLsizei imageSize, const GLvoid *pixels)
3140{
3141   teximage(ctx, compressed, dims, target, level, internalFormat, width, height,
3142            depth, border, format, type, imageSize, pixels, true);
3143}
3144
3145
3146/*
3147 * Called from the API.  Note that width includes the border.
3148 */
3149void GLAPIENTRY
3150_mesa_TexImage1D( GLenum target, GLint level, GLint internalFormat,
3151                  GLsizei width, GLint border, GLenum format,
3152                  GLenum type, const GLvoid *pixels )
3153{
3154   GET_CURRENT_CONTEXT(ctx);
3155   teximage_err(ctx, GL_FALSE, 1, target, level, internalFormat, width, 1, 1,
3156                border, format, type, 0, pixels);
3157}
3158
3159
3160void GLAPIENTRY
3161_mesa_TexImage2D( GLenum target, GLint level, GLint internalFormat,
3162                  GLsizei width, GLsizei height, GLint border,
3163                  GLenum format, GLenum type,
3164                  const GLvoid *pixels )
3165{
3166   GET_CURRENT_CONTEXT(ctx);
3167   teximage_err(ctx, GL_FALSE, 2, target, level, internalFormat, width, height, 1,
3168                border, format, type, 0, pixels);
3169}
3170
3171
3172/*
3173 * Called by the API or display list executor.
3174 * Note that width and height include the border.
3175 */
3176void GLAPIENTRY
3177_mesa_TexImage3D( GLenum target, GLint level, GLint internalFormat,
3178                  GLsizei width, GLsizei height, GLsizei depth,
3179                  GLint border, GLenum format, GLenum type,
3180                  const GLvoid *pixels )
3181{
3182   GET_CURRENT_CONTEXT(ctx);
3183   teximage_err(ctx, GL_FALSE, 3, target, level, internalFormat,
3184                width, height, depth, border, format, type, 0, pixels);
3185}
3186
3187
3188void GLAPIENTRY
3189_mesa_TexImage3DEXT( GLenum target, GLint level, GLenum internalFormat,
3190                     GLsizei width, GLsizei height, GLsizei depth,
3191                     GLint border, GLenum format, GLenum type,
3192                     const GLvoid *pixels )
3193{
3194   _mesa_TexImage3D(target, level, (GLint) internalFormat, width, height,
3195                    depth, border, format, type, pixels);
3196}
3197
3198
3199void GLAPIENTRY
3200_mesa_TexImage1D_no_error(GLenum target, GLint level, GLint internalFormat,
3201                          GLsizei width, GLint border, GLenum format,
3202                          GLenum type, const GLvoid *pixels)
3203{
3204   GET_CURRENT_CONTEXT(ctx);
3205   teximage_no_error(ctx, GL_FALSE, 1, target, level, internalFormat, width, 1,
3206                     1, border, format, type, 0, pixels);
3207}
3208
3209
3210void GLAPIENTRY
3211_mesa_TexImage2D_no_error(GLenum target, GLint level, GLint internalFormat,
3212                          GLsizei width, GLsizei height, GLint border,
3213                          GLenum format, GLenum type, const GLvoid *pixels)
3214{
3215   GET_CURRENT_CONTEXT(ctx);
3216   teximage_no_error(ctx, GL_FALSE, 2, target, level, internalFormat, width,
3217                     height, 1, border, format, type, 0, pixels);
3218}
3219
3220
3221void GLAPIENTRY
3222_mesa_TexImage3D_no_error(GLenum target, GLint level, GLint internalFormat,
3223                          GLsizei width, GLsizei height, GLsizei depth,
3224                          GLint border, GLenum format, GLenum type,
3225                          const GLvoid *pixels )
3226{
3227   GET_CURRENT_CONTEXT(ctx);
3228   teximage_no_error(ctx, GL_FALSE, 3, target, level, internalFormat,
3229                     width, height, depth, border, format, type, 0, pixels);
3230}
3231
3232
3233void GLAPIENTRY
3234_mesa_EGLImageTargetTexture2DOES (GLenum target, GLeglImageOES image)
3235{
3236   struct gl_texture_object *texObj;
3237   struct gl_texture_image *texImage;
3238   bool valid_target;
3239   GET_CURRENT_CONTEXT(ctx);
3240   FLUSH_VERTICES(ctx, 0);
3241
3242   switch (target) {
3243   case GL_TEXTURE_2D:
3244      valid_target = ctx->Extensions.OES_EGL_image;
3245      break;
3246   case GL_TEXTURE_EXTERNAL_OES:
3247      valid_target =
3248         _mesa_is_gles(ctx) ? ctx->Extensions.OES_EGL_image_external : false;
3249      break;
3250   default:
3251      valid_target = false;
3252      break;
3253   }
3254
3255   if (!valid_target) {
3256      _mesa_error(ctx, GL_INVALID_ENUM,
3257                  "glEGLImageTargetTexture2D(target=%d)", target);
3258      return;
3259   }
3260
3261   if (!image) {
3262      _mesa_error(ctx, GL_INVALID_OPERATION,
3263                  "glEGLImageTargetTexture2D(image=%p)", image);
3264      return;
3265   }
3266
3267   if (ctx->NewState & _NEW_PIXEL)
3268      _mesa_update_state(ctx);
3269
3270   texObj = _mesa_get_current_tex_object(ctx, target);
3271   if (!texObj)
3272      return;
3273
3274   _mesa_lock_texture(ctx, texObj);
3275
3276   if (texObj->Immutable) {
3277      _mesa_error(ctx, GL_INVALID_OPERATION,
3278                  "glEGLImageTargetTexture2D(texture is immutable)");
3279      _mesa_unlock_texture(ctx, texObj);
3280      return;
3281   }
3282
3283   texImage = _mesa_get_tex_image(ctx, texObj, target, 0);
3284   if (!texImage) {
3285      _mesa_error(ctx, GL_OUT_OF_MEMORY, "glEGLImageTargetTexture2D");
3286   } else {
3287      ctx->Driver.FreeTextureImageBuffer(ctx, texImage);
3288
3289      ctx->Driver.EGLImageTargetTexture2D(ctx, target,
3290                                          texObj, texImage, image);
3291
3292      _mesa_dirty_texobj(ctx, texObj);
3293   }
3294   _mesa_unlock_texture(ctx, texObj);
3295}
3296
3297
3298/**
3299 * Helper that implements the glTexSubImage1/2/3D()
3300 * and glTextureSubImage1/2/3D() functions.
3301 */
3302static void
3303texture_sub_image(struct gl_context *ctx, GLuint dims,
3304                  struct gl_texture_object *texObj,
3305                  struct gl_texture_image *texImage,
3306                  GLenum target, GLint level,
3307                  GLint xoffset, GLint yoffset, GLint zoffset,
3308                  GLsizei width, GLsizei height, GLsizei depth,
3309                  GLenum format, GLenum type, const GLvoid *pixels)
3310{
3311   FLUSH_VERTICES(ctx, 0);
3312
3313   if (ctx->NewState & _NEW_PIXEL)
3314      _mesa_update_state(ctx);
3315
3316   _mesa_lock_texture(ctx, texObj);
3317   {
3318      if (width > 0 && height > 0 && depth > 0) {
3319         /* If we have a border, offset=-1 is legal.  Bias by border width. */
3320         switch (dims) {
3321         case 3:
3322            if (target != GL_TEXTURE_2D_ARRAY)
3323               zoffset += texImage->Border;
3324            /* fall-through */
3325         case 2:
3326            if (target != GL_TEXTURE_1D_ARRAY)
3327               yoffset += texImage->Border;
3328            /* fall-through */
3329         case 1:
3330            xoffset += texImage->Border;
3331         }
3332
3333         ctx->Driver.TexSubImage(ctx, dims, texImage,
3334                                 xoffset, yoffset, zoffset,
3335                                 width, height, depth,
3336                                 format, type, pixels, &ctx->Unpack);
3337
3338         check_gen_mipmap(ctx, target, texObj, level);
3339
3340         /* NOTE: Don't signal _NEW_TEXTURE_OBJECT since we've only changed
3341          * the texel data, not the texture format, size, etc.
3342          */
3343      }
3344   }
3345   _mesa_unlock_texture(ctx, texObj);
3346}
3347
3348/**
3349 * Implement all the glTexSubImage1/2/3D() functions.
3350 * Must split this out this way because of GL_TEXTURE_CUBE_MAP.
3351 */
3352static void
3353texsubimage_err(struct gl_context *ctx, GLuint dims, GLenum target, GLint level,
3354                GLint xoffset, GLint yoffset, GLint zoffset,
3355                GLsizei width, GLsizei height, GLsizei depth,
3356                GLenum format, GLenum type, const GLvoid *pixels,
3357                const char *callerName)
3358{
3359   struct gl_texture_object *texObj;
3360   struct gl_texture_image *texImage;
3361
3362   /* check target (proxies not allowed) */
3363   if (!legal_texsubimage_target(ctx, dims, target, false)) {
3364      _mesa_error(ctx, GL_INVALID_ENUM, "glTexSubImage%uD(target=%s)",
3365                  dims, _mesa_enum_to_string(target));
3366      return;
3367   }
3368
3369   texObj = _mesa_get_current_tex_object(ctx, target);
3370   if (!texObj)
3371      return;
3372
3373   if (texsubimage_error_check(ctx, dims, texObj, target, level,
3374                               xoffset, yoffset, zoffset,
3375                               width, height, depth, format, type,
3376                               pixels, callerName)) {
3377      return;   /* error was detected */
3378   }
3379
3380   texImage = _mesa_select_tex_image(texObj, target, level);
3381   /* texsubimage_error_check ensures that texImage is not NULL */
3382
3383   if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
3384      _mesa_debug(ctx, "glTexSubImage%uD %s %d %d %d %d %d %d %d %s %s %p\n",
3385                  dims,
3386                  _mesa_enum_to_string(target), level,
3387                  xoffset, yoffset, zoffset, width, height, depth,
3388                  _mesa_enum_to_string(format),
3389                  _mesa_enum_to_string(type), pixels);
3390
3391   texture_sub_image(ctx, dims, texObj, texImage, target, level,
3392                     xoffset, yoffset, zoffset, width, height, depth,
3393                     format, type, pixels);
3394}
3395
3396
3397static void
3398texsubimage(struct gl_context *ctx, GLuint dims, GLenum target, GLint level,
3399            GLint xoffset, GLint yoffset, GLint zoffset,
3400            GLsizei width, GLsizei height, GLsizei depth,
3401            GLenum format, GLenum type, const GLvoid *pixels)
3402{
3403   struct gl_texture_object *texObj;
3404   struct gl_texture_image *texImage;
3405
3406   texObj = _mesa_get_current_tex_object(ctx, target);
3407   texImage = _mesa_select_tex_image(texObj, target, level);
3408
3409   texture_sub_image(ctx, dims, texObj, texImage, target, level,
3410                     xoffset, yoffset, zoffset, width, height, depth,
3411                     format, type, pixels);
3412}
3413
3414
3415/**
3416 * Implement all the glTextureSubImage1/2/3D() functions.
3417 * Must split this out this way because of GL_TEXTURE_CUBE_MAP.
3418 */
3419static ALWAYS_INLINE void
3420texturesubimage(struct gl_context *ctx, GLuint dims,
3421                GLuint texture, GLint level,
3422                GLint xoffset, GLint yoffset, GLint zoffset,
3423                GLsizei width, GLsizei height, GLsizei depth,
3424                GLenum format, GLenum type, const GLvoid *pixels,
3425                const char *callerName, bool no_error)
3426{
3427   struct gl_texture_object *texObj;
3428   struct gl_texture_image *texImage;
3429   int i;
3430
3431   if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
3432      _mesa_debug(ctx,
3433                  "glTextureSubImage%uD %d %d %d %d %d %d %d %d %s %s %p\n",
3434                  dims, texture, level,
3435                  xoffset, yoffset, zoffset, width, height, depth,
3436                  _mesa_enum_to_string(format),
3437                  _mesa_enum_to_string(type), pixels);
3438
3439   /* Get the texture object by Name. */
3440   if (!no_error) {
3441      texObj = _mesa_lookup_texture_err(ctx, texture, callerName);
3442      if (!texObj)
3443         return;
3444   } else {
3445      texObj = _mesa_lookup_texture(ctx, texture);
3446   }
3447
3448   if (!no_error) {
3449      /* check target (proxies not allowed) */
3450      if (!legal_texsubimage_target(ctx, dims, texObj->Target, true)) {
3451         _mesa_error(ctx, GL_INVALID_ENUM, "%s(target=%s)",
3452                     callerName, _mesa_enum_to_string(texObj->Target));
3453         return;
3454      }
3455
3456      if (texsubimage_error_check(ctx, dims, texObj, texObj->Target, level,
3457                                  xoffset, yoffset, zoffset,
3458                                  width, height, depth, format, type,
3459                                  pixels, callerName)) {
3460         return;   /* error was detected */
3461      }
3462   }
3463
3464   /* Must handle special case GL_TEXTURE_CUBE_MAP. */
3465   if (texObj->Target == GL_TEXTURE_CUBE_MAP) {
3466      GLint imageStride;
3467
3468      /*
3469       * What do we do if the user created a texture with the following code
3470       * and then called this function with its handle?
3471       *
3472       *    GLuint tex;
3473       *    glCreateTextures(GL_TEXTURE_CUBE_MAP, 1, &tex);
3474       *    glBindTexture(GL_TEXTURE_CUBE_MAP, tex);
3475       *    glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, ...);
3476       *    glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, ...);
3477       *    glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, ...);
3478       *    // Note: GL_TEXTURE_CUBE_MAP_NEGATIVE_Y not set, or given the
3479       *    // wrong format, or given the wrong size, etc.
3480       *    glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, ...);
3481       *    glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, ...);
3482       *
3483       * A bug has been filed against the spec for this case.  In the
3484       * meantime, we will check for cube completeness.
3485       *
3486       * According to Section 8.17 Texture Completeness in the OpenGL 4.5
3487       * Core Profile spec (30.10.2014):
3488       *    "[A] cube map texture is cube complete if the
3489       *    following conditions all hold true: The [base level] texture
3490       *    images of each of the six cube map faces have identical, positive,
3491       *    and square dimensions. The [base level] images were each specified
3492       *    with the same internal format."
3493       *
3494       * It seems reasonable to check for cube completeness of an arbitrary
3495       * level here so that the image data has a consistent format and size.
3496       */
3497      if (!no_error && !_mesa_cube_level_complete(texObj, level)) {
3498         _mesa_error(ctx, GL_INVALID_OPERATION,
3499                     "glTextureSubImage%uD(cube map incomplete)",
3500                     dims);
3501         return;
3502      }
3503
3504      imageStride = _mesa_image_image_stride(&ctx->Unpack, width, height,
3505                                             format, type);
3506      /* Copy in each face. */
3507      for (i = zoffset; i < zoffset + depth; ++i) {
3508         texImage = texObj->Image[i][level];
3509         assert(texImage);
3510
3511         texture_sub_image(ctx, 3, texObj, texImage, texObj->Target,
3512                           level, xoffset, yoffset, 0,
3513                           width, height, 1, format,
3514                           type, pixels);
3515         pixels = (GLubyte *) pixels + imageStride;
3516      }
3517   }
3518   else {
3519      texImage = _mesa_select_tex_image(texObj, texObj->Target, level);
3520      assert(texImage);
3521
3522      texture_sub_image(ctx, dims, texObj, texImage, texObj->Target,
3523                        level, xoffset, yoffset, zoffset,
3524                        width, height, depth, format,
3525                        type, pixels);
3526   }
3527}
3528
3529
3530static void
3531texturesubimage_error(struct gl_context *ctx, GLuint dims,
3532                      GLuint texture, GLint level,
3533                      GLint xoffset, GLint yoffset, GLint zoffset,
3534                      GLsizei width, GLsizei height, GLsizei depth,
3535                      GLenum format, GLenum type, const GLvoid *pixels,
3536                      const char *callerName)
3537{
3538   texturesubimage(ctx, dims, texture, level, xoffset, yoffset, zoffset,
3539                   width, height, depth, format, type, pixels, callerName,
3540                   false);
3541}
3542
3543
3544static void
3545texturesubimage_no_error(struct gl_context *ctx, GLuint dims,
3546                         GLuint texture, GLint level,
3547                         GLint xoffset, GLint yoffset, GLint zoffset,
3548                         GLsizei width, GLsizei height, GLsizei depth,
3549                         GLenum format, GLenum type, const GLvoid *pixels,
3550                         const char *callerName)
3551{
3552   texturesubimage(ctx, dims, texture, level, xoffset, yoffset, zoffset,
3553                   width, height, depth, format, type, pixels, callerName,
3554                   true);
3555}
3556
3557
3558void GLAPIENTRY
3559_mesa_TexSubImage1D_no_error(GLenum target, GLint level,
3560                             GLint xoffset, GLsizei width,
3561                             GLenum format, GLenum type,
3562                             const GLvoid *pixels)
3563{
3564   GET_CURRENT_CONTEXT(ctx);
3565   texsubimage(ctx, 1, target, level,
3566               xoffset, 0, 0,
3567               width, 1, 1,
3568               format, type, pixels);
3569}
3570
3571
3572void GLAPIENTRY
3573_mesa_TexSubImage1D( GLenum target, GLint level,
3574                     GLint xoffset, GLsizei width,
3575                     GLenum format, GLenum type,
3576                     const GLvoid *pixels )
3577{
3578   GET_CURRENT_CONTEXT(ctx);
3579   texsubimage_err(ctx, 1, target, level,
3580                   xoffset, 0, 0,
3581                   width, 1, 1,
3582                   format, type, pixels, "glTexSubImage1D");
3583}
3584
3585
3586void GLAPIENTRY
3587_mesa_TexSubImage2D_no_error(GLenum target, GLint level,
3588                             GLint xoffset, GLint yoffset,
3589                             GLsizei width, GLsizei height,
3590                             GLenum format, GLenum type,
3591                             const GLvoid *pixels)
3592{
3593   GET_CURRENT_CONTEXT(ctx);
3594   texsubimage(ctx, 2, target, level,
3595               xoffset, yoffset, 0,
3596               width, height, 1,
3597               format, type, pixels);
3598}
3599
3600
3601void GLAPIENTRY
3602_mesa_TexSubImage2D( GLenum target, GLint level,
3603                     GLint xoffset, GLint yoffset,
3604                     GLsizei width, GLsizei height,
3605                     GLenum format, GLenum type,
3606                     const GLvoid *pixels )
3607{
3608   GET_CURRENT_CONTEXT(ctx);
3609   texsubimage_err(ctx, 2, target, level,
3610                   xoffset, yoffset, 0,
3611                   width, height, 1,
3612                   format, type, pixels, "glTexSubImage2D");
3613}
3614
3615
3616void GLAPIENTRY
3617_mesa_TexSubImage3D_no_error(GLenum target, GLint level,
3618                             GLint xoffset, GLint yoffset, GLint zoffset,
3619                             GLsizei width, GLsizei height, GLsizei depth,
3620                             GLenum format, GLenum type,
3621                             const GLvoid *pixels)
3622{
3623   GET_CURRENT_CONTEXT(ctx);
3624   texsubimage(ctx, 3, target, level,
3625               xoffset, yoffset, zoffset,
3626               width, height, depth,
3627               format, type, pixels);
3628}
3629
3630
3631void GLAPIENTRY
3632_mesa_TexSubImage3D( GLenum target, GLint level,
3633                     GLint xoffset, GLint yoffset, GLint zoffset,
3634                     GLsizei width, GLsizei height, GLsizei depth,
3635                     GLenum format, GLenum type,
3636                     const GLvoid *pixels )
3637{
3638   GET_CURRENT_CONTEXT(ctx);
3639   texsubimage_err(ctx, 3, target, level,
3640                   xoffset, yoffset, zoffset,
3641                   width, height, depth,
3642                   format, type, pixels, "glTexSubImage3D");
3643}
3644
3645
3646void GLAPIENTRY
3647_mesa_TextureSubImage1D_no_error(GLuint texture, GLint level, GLint xoffset,
3648                                 GLsizei width, GLenum format, GLenum type,
3649                                 const GLvoid *pixels)
3650{
3651   GET_CURRENT_CONTEXT(ctx);
3652   texturesubimage_no_error(ctx, 1, texture, level, xoffset, 0, 0, width, 1, 1,
3653                            format, type, pixels, "glTextureSubImage1D");
3654}
3655
3656
3657void GLAPIENTRY
3658_mesa_TextureSubImage1D(GLuint texture, GLint level,
3659                        GLint xoffset, GLsizei width,
3660                        GLenum format, GLenum type,
3661                        const GLvoid *pixels)
3662{
3663   GET_CURRENT_CONTEXT(ctx);
3664   texturesubimage_error(ctx, 1, texture, level, xoffset, 0, 0, width, 1, 1,
3665                         format, type, pixels, "glTextureSubImage1D");
3666}
3667
3668
3669void GLAPIENTRY
3670_mesa_TextureSubImage2D_no_error(GLuint texture, GLint level, GLint xoffset,
3671                                 GLint yoffset, GLsizei width, GLsizei height,
3672                                 GLenum format, GLenum type,
3673                                 const GLvoid *pixels)
3674{
3675   GET_CURRENT_CONTEXT(ctx);
3676   texturesubimage_no_error(ctx, 2, texture, level, xoffset, yoffset, 0, width,
3677                            height, 1, format, type, pixels,
3678                            "glTextureSubImage2D");
3679}
3680
3681
3682void GLAPIENTRY
3683_mesa_TextureSubImage2D(GLuint texture, GLint level,
3684                        GLint xoffset, GLint yoffset,
3685                        GLsizei width, GLsizei height,
3686                        GLenum format, GLenum type,
3687                        const GLvoid *pixels)
3688{
3689   GET_CURRENT_CONTEXT(ctx);
3690   texturesubimage_error(ctx, 2, texture, level, xoffset, yoffset, 0, width,
3691                         height, 1, format, type, pixels,
3692                         "glTextureSubImage2D");
3693}
3694
3695
3696void GLAPIENTRY
3697_mesa_TextureSubImage3D_no_error(GLuint texture, GLint level, GLint xoffset,
3698                                 GLint yoffset, GLint zoffset, GLsizei width,
3699                                 GLsizei height, GLsizei depth, GLenum format,
3700                                 GLenum type, const GLvoid *pixels)
3701{
3702   GET_CURRENT_CONTEXT(ctx);
3703   texturesubimage_no_error(ctx, 3, texture, level, xoffset, yoffset, zoffset,
3704                            width, height, depth, format, type, pixels,
3705                            "glTextureSubImage3D");
3706}
3707
3708
3709void GLAPIENTRY
3710_mesa_TextureSubImage3D(GLuint texture, GLint level,
3711                        GLint xoffset, GLint yoffset, GLint zoffset,
3712                        GLsizei width, GLsizei height, GLsizei depth,
3713                        GLenum format, GLenum type,
3714                        const GLvoid *pixels)
3715{
3716   GET_CURRENT_CONTEXT(ctx);
3717   texturesubimage_error(ctx, 3, texture, level, xoffset, yoffset, zoffset,
3718                         width, height, depth, format, type, pixels,
3719                         "glTextureSubImage3D");
3720}
3721
3722
3723/**
3724 * For glCopyTexSubImage, return the source renderbuffer to copy texel data
3725 * from.  This depends on whether the texture contains color or depth values.
3726 */
3727static struct gl_renderbuffer *
3728get_copy_tex_image_source(struct gl_context *ctx, mesa_format texFormat)
3729{
3730   if (_mesa_get_format_bits(texFormat, GL_DEPTH_BITS) > 0) {
3731      /* reading from depth/stencil buffer */
3732      return ctx->ReadBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;
3733   } else if (_mesa_get_format_bits(texFormat, GL_STENCIL_BITS) > 0) {
3734      return ctx->ReadBuffer->Attachment[BUFFER_STENCIL].Renderbuffer;
3735   } else {
3736      /* copying from color buffer */
3737      return ctx->ReadBuffer->_ColorReadBuffer;
3738   }
3739}
3740
3741
3742static void
3743copytexsubimage_by_slice(struct gl_context *ctx,
3744                         struct gl_texture_image *texImage,
3745                         GLuint dims,
3746                         GLint xoffset, GLint yoffset, GLint zoffset,
3747                         struct gl_renderbuffer *rb,
3748                         GLint x, GLint y,
3749                         GLsizei width, GLsizei height)
3750{
3751   if (texImage->TexObject->Target == GL_TEXTURE_1D_ARRAY) {
3752      int slice;
3753
3754      /* For 1D arrays, we copy each scanline of the source rectangle into the
3755       * next array slice.
3756       */
3757      assert(zoffset == 0);
3758
3759      for (slice = 0; slice < height; slice++) {
3760         assert(yoffset + slice < texImage->Height);
3761         ctx->Driver.CopyTexSubImage(ctx, 2, texImage,
3762                                     xoffset, 0, yoffset + slice,
3763                                     rb, x, y + slice, width, 1);
3764      }
3765   } else {
3766      ctx->Driver.CopyTexSubImage(ctx, dims, texImage,
3767                                  xoffset, yoffset, zoffset,
3768                                  rb, x, y, width, height);
3769   }
3770}
3771
3772
3773static GLboolean
3774formats_differ_in_component_sizes(mesa_format f1, mesa_format f2)
3775{
3776   GLint f1_r_bits = _mesa_get_format_bits(f1, GL_RED_BITS);
3777   GLint f1_g_bits = _mesa_get_format_bits(f1, GL_GREEN_BITS);
3778   GLint f1_b_bits = _mesa_get_format_bits(f1, GL_BLUE_BITS);
3779   GLint f1_a_bits = _mesa_get_format_bits(f1, GL_ALPHA_BITS);
3780
3781   GLint f2_r_bits = _mesa_get_format_bits(f2, GL_RED_BITS);
3782   GLint f2_g_bits = _mesa_get_format_bits(f2, GL_GREEN_BITS);
3783   GLint f2_b_bits = _mesa_get_format_bits(f2, GL_BLUE_BITS);
3784   GLint f2_a_bits = _mesa_get_format_bits(f2, GL_ALPHA_BITS);
3785
3786   if ((f1_r_bits && f2_r_bits && f1_r_bits != f2_r_bits)
3787       || (f1_g_bits && f2_g_bits && f1_g_bits != f2_g_bits)
3788       || (f1_b_bits && f2_b_bits && f1_b_bits != f2_b_bits)
3789       || (f1_a_bits && f2_a_bits && f1_a_bits != f2_a_bits))
3790      return GL_TRUE;
3791
3792   return GL_FALSE;
3793}
3794
3795
3796/**
3797 * Check if the given texture format and size arguments match those
3798 * of the texture image.
3799 * \param return true if arguments match, false otherwise.
3800 */
3801static bool
3802can_avoid_reallocation(const struct gl_texture_image *texImage,
3803                       GLenum internalFormat,
3804                       mesa_format texFormat, GLsizei width,
3805                       GLsizei height, GLint border)
3806{
3807   if (texImage->InternalFormat != internalFormat)
3808      return false;
3809   if (texImage->TexFormat != texFormat)
3810      return false;
3811   if (texImage->Border != border)
3812      return false;
3813   if (texImage->Width2 != width)
3814      return false;
3815   if (texImage->Height2 != height)
3816      return false;
3817   return true;
3818}
3819
3820
3821/**
3822 * Implementation for glCopyTex(ture)SubImage1/2/3D() functions.
3823 */
3824static void
3825copy_texture_sub_image(struct gl_context *ctx, GLuint dims,
3826                       struct gl_texture_object *texObj,
3827                       GLenum target, GLint level,
3828                       GLint xoffset, GLint yoffset, GLint zoffset,
3829                       GLint x, GLint y, GLsizei width, GLsizei height)
3830{
3831   struct gl_texture_image *texImage;
3832
3833   _mesa_lock_texture(ctx, texObj);
3834
3835   texImage = _mesa_select_tex_image(texObj, target, level);
3836
3837   /* If we have a border, offset=-1 is legal.  Bias by border width. */
3838   switch (dims) {
3839   case 3:
3840      if (target != GL_TEXTURE_2D_ARRAY)
3841         zoffset += texImage->Border;
3842      /* fall-through */
3843   case 2:
3844      if (target != GL_TEXTURE_1D_ARRAY)
3845         yoffset += texImage->Border;
3846      /* fall-through */
3847   case 1:
3848      xoffset += texImage->Border;
3849   }
3850
3851   if (_mesa_clip_copytexsubimage(ctx, &xoffset, &yoffset, &x, &y,
3852                                  &width, &height)) {
3853      struct gl_renderbuffer *srcRb =
3854         get_copy_tex_image_source(ctx, texImage->TexFormat);
3855
3856      copytexsubimage_by_slice(ctx, texImage, dims, xoffset, yoffset, zoffset,
3857                               srcRb, x, y, width, height);
3858
3859      check_gen_mipmap(ctx, target, texObj, level);
3860
3861      /* NOTE: Don't signal _NEW_TEXTURE_OBJECT since we've only changed
3862       * the texel data, not the texture format, size, etc.
3863       */
3864   }
3865
3866   _mesa_unlock_texture(ctx, texObj);
3867}
3868
3869
3870static void
3871copy_texture_sub_image_err(struct gl_context *ctx, GLuint dims,
3872                           struct gl_texture_object *texObj,
3873                           GLenum target, GLint level,
3874                           GLint xoffset, GLint yoffset, GLint zoffset,
3875                           GLint x, GLint y, GLsizei width, GLsizei height,
3876                           const char *caller)
3877{
3878   FLUSH_VERTICES(ctx, 0);
3879
3880   if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
3881      _mesa_debug(ctx, "%s %s %d %d %d %d %d %d %d %d\n", caller,
3882                  _mesa_enum_to_string(target),
3883                  level, xoffset, yoffset, zoffset, x, y, width, height);
3884
3885   if (ctx->NewState & NEW_COPY_TEX_STATE)
3886      _mesa_update_state(ctx);
3887
3888   if (copytexsubimage_error_check(ctx, dims, texObj, target, level,
3889                                   xoffset, yoffset, zoffset,
3890                                   width, height, caller)) {
3891      return;
3892   }
3893
3894   copy_texture_sub_image(ctx, dims, texObj, target, level, xoffset, yoffset,
3895                          zoffset, x, y, width, height);
3896}
3897
3898
3899static void
3900copy_texture_sub_image_no_error(struct gl_context *ctx, GLuint dims,
3901                                struct gl_texture_object *texObj,
3902                                GLenum target, GLint level,
3903                                GLint xoffset, GLint yoffset, GLint zoffset,
3904                                GLint x, GLint y, GLsizei width, GLsizei height)
3905{
3906   FLUSH_VERTICES(ctx, 0);
3907
3908   if (ctx->NewState & NEW_COPY_TEX_STATE)
3909      _mesa_update_state(ctx);
3910
3911   copy_texture_sub_image(ctx, dims, texObj, target, level, xoffset, yoffset,
3912                          zoffset, x, y, width, height);
3913}
3914
3915
3916/**
3917 * Implement the glCopyTexImage1/2D() functions.
3918 */
3919static ALWAYS_INLINE void
3920copyteximage(struct gl_context *ctx, GLuint dims,
3921             GLenum target, GLint level, GLenum internalFormat,
3922             GLint x, GLint y, GLsizei width, GLsizei height, GLint border,
3923             bool no_error)
3924{
3925   struct gl_texture_image *texImage;
3926   struct gl_texture_object *texObj;
3927   mesa_format texFormat;
3928
3929   FLUSH_VERTICES(ctx, 0);
3930
3931   if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
3932      _mesa_debug(ctx, "glCopyTexImage%uD %s %d %s %d %d %d %d %d\n",
3933                  dims,
3934                  _mesa_enum_to_string(target), level,
3935                  _mesa_enum_to_string(internalFormat),
3936                  x, y, width, height, border);
3937
3938   if (ctx->NewState & NEW_COPY_TEX_STATE)
3939      _mesa_update_state(ctx);
3940
3941   if (!no_error) {
3942      if (copytexture_error_check(ctx, dims, target, level, internalFormat,
3943                                  border))
3944         return;
3945
3946      if (!_mesa_legal_texture_dimensions(ctx, target, level, width, height,
3947                                          1, border)) {
3948         _mesa_error(ctx, GL_INVALID_VALUE,
3949                     "glCopyTexImage%uD(invalid width=%d or height=%d)",
3950                     dims, width, height);
3951         return;
3952      }
3953   }
3954
3955   texObj = _mesa_get_current_tex_object(ctx, target);
3956   assert(texObj);
3957
3958   texFormat = _mesa_choose_texture_format(ctx, texObj, target, level,
3959                                           internalFormat, GL_NONE, GL_NONE);
3960
3961   /* First check if reallocating the texture buffer can be avoided.
3962    * Without the realloc the copy can be 20x faster.
3963    */
3964   _mesa_lock_texture(ctx, texObj);
3965   {
3966      texImage = _mesa_select_tex_image(texObj, target, level);
3967      if (texImage && can_avoid_reallocation(texImage, internalFormat, texFormat,
3968                                             width, height, border)) {
3969         _mesa_unlock_texture(ctx, texObj);
3970         if (no_error) {
3971            copy_texture_sub_image_no_error(ctx, dims, texObj, target, level, 0,
3972                                            0, 0, x, y, width, height);
3973         } else {
3974            copy_texture_sub_image_err(ctx, dims, texObj, target, level, 0, 0,
3975                                       0, x, y, width, height,"CopyTexImage");
3976         }
3977         return;
3978      }
3979   }
3980   _mesa_unlock_texture(ctx, texObj);
3981   _mesa_perf_debug(ctx, MESA_DEBUG_SEVERITY_LOW, "glCopyTexImage "
3982                    "can't avoid reallocating texture storage\n");
3983
3984   if (!no_error && _mesa_is_gles3(ctx)) {
3985      struct gl_renderbuffer *rb =
3986         _mesa_get_read_renderbuffer_for_format(ctx, internalFormat);
3987
3988      if (_mesa_is_enum_format_unsized(internalFormat)) {
3989      /* Conversion from GL_RGB10_A2 source buffer format is not allowed in
3990       * OpenGL ES 3.0. Khronos bug# 9807.
3991       */
3992         if (rb->InternalFormat == GL_RGB10_A2) {
3993               _mesa_error(ctx, GL_INVALID_OPERATION,
3994                           "glCopyTexImage%uD(Reading from GL_RGB10_A2 buffer"
3995                           " and writing to unsized internal format)", dims);
3996               return;
3997         }
3998      }
3999      /* From Page 139 of OpenGL ES 3.0 spec:
4000       *    "If internalformat is sized, the internal format of the new texel
4001       *    array is internalformat, and this is also the new texel array’s
4002       *    effective internal format. If the component sizes of internalformat
4003       *    do not exactly match the corresponding component sizes of the source
4004       *    buffer’s effective internal format, described below, an
4005       *    INVALID_OPERATION error is generated. If internalformat is unsized,
4006       *    the internal format of the new texel array is the effective internal
4007       *    format of the source buffer, and this is also the new texel array’s
4008       *    effective internal format.
4009       */
4010      else if (formats_differ_in_component_sizes (texFormat, rb->Format)) {
4011            _mesa_error(ctx, GL_INVALID_OPERATION,
4012                        "glCopyTexImage%uD(component size changed in"
4013                        " internal format)", dims);
4014            return;
4015      }
4016   }
4017
4018   assert(texFormat != MESA_FORMAT_NONE);
4019
4020   if (!ctx->Driver.TestProxyTexImage(ctx, proxy_target(target),
4021                                      0, level, texFormat, 1,
4022                                      width, height, 1)) {
4023      _mesa_error(ctx, GL_OUT_OF_MEMORY,
4024                  "glCopyTexImage%uD(image too large)", dims);
4025      return;
4026   }
4027
4028   if (border && ctx->Const.StripTextureBorder) {
4029      x += border;
4030      width -= border * 2;
4031      if (dims == 2) {
4032         y += border;
4033         height -= border * 2;
4034      }
4035      border = 0;
4036   }
4037
4038   _mesa_lock_texture(ctx, texObj);
4039   {
4040      texImage = _mesa_get_tex_image(ctx, texObj, target, level);
4041
4042      if (!texImage) {
4043         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexImage%uD", dims);
4044      }
4045      else {
4046         GLint srcX = x, srcY = y, dstX = 0, dstY = 0, dstZ = 0;
4047         const GLuint face = _mesa_tex_target_to_face(target);
4048
4049         /* Free old texture image */
4050         ctx->Driver.FreeTextureImageBuffer(ctx, texImage);
4051
4052         _mesa_init_teximage_fields(ctx, texImage, width, height, 1,
4053                                    border, internalFormat, texFormat);
4054
4055         if (width && height) {
4056            /* Allocate texture memory (no pixel data yet) */
4057            ctx->Driver.AllocTextureImageBuffer(ctx, texImage);
4058
4059            if (_mesa_clip_copytexsubimage(ctx, &dstX, &dstY, &srcX, &srcY,
4060                                           &width, &height)) {
4061               struct gl_renderbuffer *srcRb =
4062                  get_copy_tex_image_source(ctx, texImage->TexFormat);
4063
4064               copytexsubimage_by_slice(ctx, texImage, dims,
4065                                        dstX, dstY, dstZ,
4066                                        srcRb, srcX, srcY, width, height);
4067            }
4068
4069            check_gen_mipmap(ctx, target, texObj, level);
4070         }
4071
4072         _mesa_update_fbo_texture(ctx, texObj, face, level);
4073
4074         _mesa_dirty_texobj(ctx, texObj);
4075      }
4076   }
4077   _mesa_unlock_texture(ctx, texObj);
4078}
4079
4080
4081static void
4082copyteximage_err(struct gl_context *ctx, GLuint dims, GLenum target,
4083                 GLint level, GLenum internalFormat, GLint x, GLint y,
4084                 GLsizei width, GLsizei height, GLint border)
4085{
4086   copyteximage(ctx, dims, target, level, internalFormat, x, y, width, height,
4087                border, false);
4088}
4089
4090
4091static void
4092copyteximage_no_error(struct gl_context *ctx, GLuint dims, GLenum target,
4093                      GLint level, GLenum internalFormat, GLint x, GLint y,
4094                      GLsizei width, GLsizei height, GLint border)
4095{
4096   copyteximage(ctx, dims, target, level, internalFormat, x, y, width, height,
4097                border, true);
4098}
4099
4100
4101void GLAPIENTRY
4102_mesa_CopyTexImage1D( GLenum target, GLint level,
4103                      GLenum internalFormat,
4104                      GLint x, GLint y,
4105                      GLsizei width, GLint border )
4106{
4107   GET_CURRENT_CONTEXT(ctx);
4108   copyteximage_err(ctx, 1, target, level, internalFormat, x, y, width, 1,
4109                    border);
4110}
4111
4112
4113void GLAPIENTRY
4114_mesa_CopyTexImage2D( GLenum target, GLint level, GLenum internalFormat,
4115                      GLint x, GLint y, GLsizei width, GLsizei height,
4116                      GLint border )
4117{
4118   GET_CURRENT_CONTEXT(ctx);
4119   copyteximage_err(ctx, 2, target, level, internalFormat,
4120                    x, y, width, height, border);
4121}
4122
4123
4124void GLAPIENTRY
4125_mesa_CopyTexImage1D_no_error(GLenum target, GLint level, GLenum internalFormat,
4126                              GLint x, GLint y, GLsizei width, GLint border)
4127{
4128   GET_CURRENT_CONTEXT(ctx);
4129   copyteximage_no_error(ctx, 1, target, level, internalFormat, x, y, width, 1,
4130                         border);
4131}
4132
4133
4134void GLAPIENTRY
4135_mesa_CopyTexImage2D_no_error(GLenum target, GLint level, GLenum internalFormat,
4136                              GLint x, GLint y, GLsizei width, GLsizei height,
4137                              GLint border)
4138{
4139   GET_CURRENT_CONTEXT(ctx);
4140   copyteximage_no_error(ctx, 2, target, level, internalFormat,
4141                         x, y, width, height, border);
4142}
4143
4144
4145void GLAPIENTRY
4146_mesa_CopyTexSubImage1D(GLenum target, GLint level,
4147                        GLint xoffset, GLint x, GLint y, GLsizei width)
4148{
4149   struct gl_texture_object* texObj;
4150   const char *self = "glCopyTexSubImage1D";
4151   GET_CURRENT_CONTEXT(ctx);
4152
4153   /* Check target (proxies not allowed). Target must be checked prior to
4154    * calling _mesa_get_current_tex_object.
4155    */
4156   if (!legal_texsubimage_target(ctx, 1, target, false)) {
4157      _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid target %s)", self,
4158                  _mesa_enum_to_string(target));
4159      return;
4160   }
4161
4162   texObj = _mesa_get_current_tex_object(ctx, target);
4163   if (!texObj)
4164      return;
4165
4166   copy_texture_sub_image_err(ctx, 1, texObj, target, level, xoffset, 0, 0,
4167                              x, y, width, 1, self);
4168}
4169
4170
4171void GLAPIENTRY
4172_mesa_CopyTexSubImage2D(GLenum target, GLint level,
4173                        GLint xoffset, GLint yoffset,
4174                        GLint x, GLint y, GLsizei width, GLsizei height)
4175{
4176   struct gl_texture_object* texObj;
4177   const char *self = "glCopyTexSubImage2D";
4178   GET_CURRENT_CONTEXT(ctx);
4179
4180   /* Check target (proxies not allowed). Target must be checked prior to
4181    * calling _mesa_get_current_tex_object.
4182    */
4183   if (!legal_texsubimage_target(ctx, 2, target, false)) {
4184      _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid target %s)", self,
4185                  _mesa_enum_to_string(target));
4186      return;
4187   }
4188
4189   texObj = _mesa_get_current_tex_object(ctx, target);
4190   if (!texObj)
4191      return;
4192
4193   copy_texture_sub_image_err(ctx, 2, texObj, target, level, xoffset, yoffset,
4194                              0, x, y, width, height, self);
4195}
4196
4197
4198void GLAPIENTRY
4199_mesa_CopyTexSubImage3D(GLenum target, GLint level,
4200                        GLint xoffset, GLint yoffset, GLint zoffset,
4201                        GLint x, GLint y, GLsizei width, GLsizei height)
4202{
4203   struct gl_texture_object* texObj;
4204   const char *self = "glCopyTexSubImage3D";
4205   GET_CURRENT_CONTEXT(ctx);
4206
4207   /* Check target (proxies not allowed). Target must be checked prior to
4208    * calling _mesa_get_current_tex_object.
4209    */
4210   if (!legal_texsubimage_target(ctx, 3, target, false)) {
4211      _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid target %s)", self,
4212                  _mesa_enum_to_string(target));
4213      return;
4214   }
4215
4216   texObj = _mesa_get_current_tex_object(ctx, target);
4217   if (!texObj)
4218      return;
4219
4220   copy_texture_sub_image_err(ctx, 3, texObj, target, level, xoffset, yoffset,
4221                              zoffset, x, y, width, height, self);
4222}
4223
4224
4225void GLAPIENTRY
4226_mesa_CopyTextureSubImage1D(GLuint texture, GLint level,
4227                            GLint xoffset, GLint x, GLint y, GLsizei width)
4228{
4229   struct gl_texture_object* texObj;
4230   const char *self = "glCopyTextureSubImage1D";
4231   GET_CURRENT_CONTEXT(ctx);
4232
4233   texObj = _mesa_lookup_texture_err(ctx, texture, self);
4234   if (!texObj)
4235      return;
4236
4237   /* Check target (proxies not allowed). */
4238   if (!legal_texsubimage_target(ctx, 1, texObj->Target, true)) {
4239      _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid target %s)", self,
4240                  _mesa_enum_to_string(texObj->Target));
4241      return;
4242   }
4243
4244   copy_texture_sub_image_err(ctx, 1, texObj, texObj->Target, level, xoffset, 0,
4245                              0, x, y, width, 1, self);
4246}
4247
4248
4249void GLAPIENTRY
4250_mesa_CopyTextureSubImage2D(GLuint texture, GLint level,
4251                            GLint xoffset, GLint yoffset,
4252                            GLint x, GLint y, GLsizei width, GLsizei height)
4253{
4254   struct gl_texture_object* texObj;
4255   const char *self = "glCopyTextureSubImage2D";
4256   GET_CURRENT_CONTEXT(ctx);
4257
4258   texObj = _mesa_lookup_texture_err(ctx, texture, self);
4259   if (!texObj)
4260      return;
4261
4262   /* Check target (proxies not allowed). */
4263   if (!legal_texsubimage_target(ctx, 2, texObj->Target, true)) {
4264      _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid target %s)", self,
4265                  _mesa_enum_to_string(texObj->Target));
4266      return;
4267   }
4268
4269   copy_texture_sub_image_err(ctx, 2, texObj, texObj->Target, level, xoffset,
4270                              yoffset, 0, x, y, width, height, self);
4271}
4272
4273
4274void GLAPIENTRY
4275_mesa_CopyTextureSubImage3D(GLuint texture, GLint level,
4276                            GLint xoffset, GLint yoffset, GLint zoffset,
4277                            GLint x, GLint y, GLsizei width, GLsizei height)
4278{
4279   struct gl_texture_object* texObj;
4280   const char *self = "glCopyTextureSubImage3D";
4281   GET_CURRENT_CONTEXT(ctx);
4282
4283   texObj = _mesa_lookup_texture_err(ctx, texture, self);
4284   if (!texObj)
4285      return;
4286
4287   /* Check target (proxies not allowed). */
4288   if (!legal_texsubimage_target(ctx, 3, texObj->Target, true)) {
4289      _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid target %s)", self,
4290                  _mesa_enum_to_string(texObj->Target));
4291      return;
4292   }
4293
4294   if (texObj->Target == GL_TEXTURE_CUBE_MAP) {
4295      /* Act like CopyTexSubImage2D */
4296      copy_texture_sub_image_err(ctx, 2, texObj,
4297                                GL_TEXTURE_CUBE_MAP_POSITIVE_X + zoffset,
4298                                level, xoffset, yoffset, 0, x, y, width, height,
4299                                self);
4300   }
4301   else
4302      copy_texture_sub_image_err(ctx, 3, texObj, texObj->Target, level, xoffset,
4303                                 yoffset, zoffset, x, y, width, height, self);
4304}
4305
4306
4307void GLAPIENTRY
4308_mesa_CopyTexSubImage1D_no_error(GLenum target, GLint level, GLint xoffset,
4309                                 GLint x, GLint y, GLsizei width)
4310{
4311   GET_CURRENT_CONTEXT(ctx);
4312
4313   struct gl_texture_object* texObj = _mesa_get_current_tex_object(ctx, target);
4314   copy_texture_sub_image_no_error(ctx, 1, texObj, target, level, xoffset, 0, 0,
4315                                   x, y, width, 1);
4316}
4317
4318
4319void GLAPIENTRY
4320_mesa_CopyTexSubImage2D_no_error(GLenum target, GLint level, GLint xoffset,
4321                                 GLint yoffset, GLint x, GLint y, GLsizei width,
4322                                 GLsizei height)
4323{
4324   GET_CURRENT_CONTEXT(ctx);
4325
4326   struct gl_texture_object* texObj = _mesa_get_current_tex_object(ctx, target);
4327   copy_texture_sub_image_no_error(ctx, 2, texObj, target, level, xoffset,
4328                                   yoffset, 0, x, y, width, height);
4329}
4330
4331
4332void GLAPIENTRY
4333_mesa_CopyTexSubImage3D_no_error(GLenum target, GLint level, GLint xoffset,
4334                                 GLint yoffset, GLint zoffset, GLint x, GLint y,
4335                                 GLsizei width, GLsizei height)
4336{
4337   GET_CURRENT_CONTEXT(ctx);
4338
4339   struct gl_texture_object* texObj = _mesa_get_current_tex_object(ctx, target);
4340   copy_texture_sub_image_no_error(ctx, 3, texObj, target, level, xoffset,
4341                                   yoffset, zoffset, x, y, width, height);
4342}
4343
4344
4345void GLAPIENTRY
4346_mesa_CopyTextureSubImage1D_no_error(GLuint texture, GLint level, GLint xoffset,
4347                                     GLint x, GLint y, GLsizei width)
4348{
4349   GET_CURRENT_CONTEXT(ctx);
4350
4351   struct gl_texture_object* texObj = _mesa_lookup_texture(ctx, texture);
4352   copy_texture_sub_image_no_error(ctx, 1, texObj, texObj->Target, level,
4353                                   xoffset, 0, 0, x, y, width, 1);
4354}
4355
4356
4357void GLAPIENTRY
4358_mesa_CopyTextureSubImage2D_no_error(GLuint texture, GLint level, GLint xoffset,
4359                                     GLint yoffset, GLint x, GLint y,
4360                                     GLsizei width, GLsizei height)
4361{
4362   GET_CURRENT_CONTEXT(ctx);
4363
4364   struct gl_texture_object* texObj = _mesa_lookup_texture(ctx, texture);
4365   copy_texture_sub_image_no_error(ctx, 2, texObj, texObj->Target, level,
4366                                   xoffset, yoffset, 0, x, y, width, height);
4367}
4368
4369
4370void GLAPIENTRY
4371_mesa_CopyTextureSubImage3D_no_error(GLuint texture, GLint level, GLint xoffset,
4372                                     GLint yoffset, GLint zoffset, GLint x,
4373                                     GLint y, GLsizei width, GLsizei height)
4374{
4375   GET_CURRENT_CONTEXT(ctx);
4376
4377   struct gl_texture_object* texObj = _mesa_lookup_texture(ctx, texture);
4378   if (texObj->Target == GL_TEXTURE_CUBE_MAP) {
4379      /* Act like CopyTexSubImage2D */
4380      copy_texture_sub_image_no_error(ctx, 2, texObj,
4381                                      GL_TEXTURE_CUBE_MAP_POSITIVE_X + zoffset,
4382                                      level, xoffset, yoffset, 0, x, y, width,
4383                                      height);
4384   }
4385   else
4386      copy_texture_sub_image_no_error(ctx, 3, texObj, texObj->Target, level,
4387                                      xoffset, yoffset, zoffset, x, y, width,
4388                                      height);
4389}
4390
4391
4392static bool
4393check_clear_tex_image(struct gl_context *ctx,
4394                      const char *function,
4395                      struct gl_texture_image *texImage,
4396                      GLenum format, GLenum type,
4397                      const void *data,
4398                      GLubyte *clearValue)
4399{
4400   struct gl_texture_object *texObj = texImage->TexObject;
4401   static const GLubyte zeroData[MAX_PIXEL_BYTES];
4402   GLenum internalFormat = texImage->InternalFormat;
4403   GLenum err;
4404
4405   if (texObj->Target == GL_TEXTURE_BUFFER) {
4406      _mesa_error(ctx, GL_INVALID_OPERATION,
4407                  "%s(buffer texture)", function);
4408      return false;
4409   }
4410
4411   if (_mesa_is_compressed_format(ctx, internalFormat)) {
4412      _mesa_error(ctx, GL_INVALID_OPERATION,
4413                  "%s(compressed texture)", function);
4414      return false;
4415   }
4416
4417   err = _mesa_error_check_format_and_type(ctx, format, type);
4418   if (err != GL_NO_ERROR) {
4419      _mesa_error(ctx, err,
4420                  "%s(incompatible format = %s, type = %s)",
4421                  function,
4422                  _mesa_enum_to_string(format),
4423                  _mesa_enum_to_string(type));
4424      return false;
4425   }
4426
4427   /* make sure internal format and format basically agree */
4428   if (!texture_formats_agree(internalFormat, format)) {
4429      _mesa_error(ctx, GL_INVALID_OPERATION,
4430                  "%s(incompatible internalFormat = %s, format = %s)",
4431                  function,
4432                  _mesa_enum_to_string(internalFormat),
4433                  _mesa_enum_to_string(format));
4434      return false;
4435   }
4436
4437   if (ctx->Version >= 30 || ctx->Extensions.EXT_texture_integer) {
4438      /* both source and dest must be integer-valued, or neither */
4439      if (_mesa_is_format_integer_color(texImage->TexFormat) !=
4440          _mesa_is_enum_format_integer(format)) {
4441         _mesa_error(ctx, GL_INVALID_OPERATION,
4442                     "%s(integer/non-integer format mismatch)",
4443                     function);
4444         return false;
4445      }
4446   }
4447
4448   if (!_mesa_texstore(ctx,
4449                       1, /* dims */
4450                       texImage->_BaseFormat,
4451                       texImage->TexFormat,
4452                       0, /* dstRowStride */
4453                       &clearValue,
4454                       1, 1, 1, /* srcWidth/Height/Depth */
4455                       format, type,
4456                       data ? data : zeroData,
4457                       &ctx->DefaultPacking)) {
4458      _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid format)", function);
4459      return false;
4460   }
4461
4462   return true;
4463}
4464
4465
4466static struct gl_texture_object *
4467get_tex_obj_for_clear(struct gl_context *ctx,
4468                      const char *function,
4469                      GLuint texture)
4470{
4471   struct gl_texture_object *texObj;
4472
4473   texObj = _mesa_lookup_texture_err(ctx, texture, function);
4474   if (!texObj)
4475      return NULL;
4476
4477   if (texObj->Target == 0) {
4478      _mesa_error(ctx, GL_INVALID_OPERATION, "%s(unbound tex)", function);
4479      return NULL;
4480   }
4481
4482   return texObj;
4483}
4484
4485
4486/**
4487 * For clearing cube textures, the zoffset and depth parameters indicate
4488 * which cube map faces are to be cleared.  This is the one case where we
4489 * need to be concerned with multiple gl_texture_images.  This function
4490 * returns the array of texture images to clear for cube maps, or one
4491 * texture image otherwise.
4492 * \return number of texture images, 0 for error, 6 for cube, 1 otherwise.
4493 */
4494static int
4495get_tex_images_for_clear(struct gl_context *ctx,
4496                         const char *function,
4497                         struct gl_texture_object *texObj,
4498                         GLint level,
4499                         struct gl_texture_image **texImages)
4500{
4501   GLenum target;
4502   int numFaces, i;
4503
4504   if (level < 0 || level >= MAX_TEXTURE_LEVELS) {
4505      _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid level)", function);
4506      return 0;
4507   }
4508
4509   if (texObj->Target == GL_TEXTURE_CUBE_MAP) {
4510      target = GL_TEXTURE_CUBE_MAP_POSITIVE_X;
4511      numFaces = MAX_FACES;
4512   }
4513   else {
4514      target = texObj->Target;
4515      numFaces = 1;
4516   }
4517
4518   for (i = 0; i < numFaces; i++) {
4519      texImages[i] = _mesa_select_tex_image(texObj, target + i, level);
4520      if (texImages[i] == NULL) {
4521         _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid level)", function);
4522         return 0;
4523      }
4524   }
4525
4526   return numFaces;
4527}
4528
4529
4530void GLAPIENTRY
4531_mesa_ClearTexSubImage(GLuint texture, GLint level,
4532                       GLint xoffset, GLint yoffset, GLint zoffset,
4533                       GLsizei width, GLsizei height, GLsizei depth,
4534                       GLenum format, GLenum type, const void *data)
4535{
4536   GET_CURRENT_CONTEXT(ctx);
4537   struct gl_texture_object *texObj;
4538   struct gl_texture_image *texImages[MAX_FACES];
4539   GLubyte clearValue[MAX_FACES][MAX_PIXEL_BYTES];
4540   int i, numImages;
4541   int minDepth, maxDepth;
4542
4543   texObj = get_tex_obj_for_clear(ctx, "glClearTexSubImage", texture);
4544
4545   if (texObj == NULL)
4546      return;
4547
4548   _mesa_lock_texture(ctx, texObj);
4549
4550   numImages = get_tex_images_for_clear(ctx, "glClearTexSubImage",
4551                                        texObj, level, texImages);
4552   if (numImages == 0)
4553      goto out;
4554
4555   if (numImages == 1) {
4556      minDepth = -(int) texImages[0]->Border;
4557      maxDepth = texImages[0]->Depth;
4558   } else {
4559      assert(numImages == MAX_FACES);
4560      minDepth = 0;
4561      maxDepth = numImages;
4562   }
4563
4564   if (xoffset < -(GLint) texImages[0]->Border ||
4565       yoffset < -(GLint) texImages[0]->Border ||
4566       zoffset < minDepth ||
4567       width < 0 ||
4568       height < 0 ||
4569       depth < 0 ||
4570       xoffset + width > texImages[0]->Width ||
4571       yoffset + height > texImages[0]->Height ||
4572       zoffset + depth > maxDepth) {
4573      _mesa_error(ctx, GL_INVALID_OPERATION,
4574                  "glClearSubTexImage(invalid dimensions)");
4575      goto out;
4576   }
4577
4578   if (numImages == 1) {
4579      if (check_clear_tex_image(ctx, "glClearTexSubImage", texImages[0],
4580                                format, type, data, clearValue[0])) {
4581         ctx->Driver.ClearTexSubImage(ctx,
4582                                      texImages[0],
4583                                      xoffset, yoffset, zoffset,
4584                                      width, height, depth,
4585                                      data ? clearValue[0] : NULL);
4586      }
4587   } else {
4588      /* loop over cube face images */
4589      for (i = zoffset; i < zoffset + depth; i++) {
4590         assert(i < MAX_FACES);
4591         if (!check_clear_tex_image(ctx, "glClearTexSubImage", texImages[i],
4592                                    format, type, data, clearValue[i]))
4593            goto out;
4594      }
4595      for (i = zoffset; i < zoffset + depth; i++) {
4596         ctx->Driver.ClearTexSubImage(ctx,
4597                                      texImages[i],
4598                                      xoffset, yoffset, 0,
4599                                      width, height, 1,
4600                                      data ? clearValue[i] : NULL);
4601      }
4602   }
4603
4604 out:
4605   _mesa_unlock_texture(ctx, texObj);
4606}
4607
4608
4609void GLAPIENTRY
4610_mesa_ClearTexImage( GLuint texture, GLint level,
4611                     GLenum format, GLenum type, const void *data )
4612{
4613   GET_CURRENT_CONTEXT(ctx);
4614   struct gl_texture_object *texObj;
4615   struct gl_texture_image *texImages[MAX_FACES];
4616   GLubyte clearValue[MAX_FACES][MAX_PIXEL_BYTES];
4617   int i, numImages;
4618
4619   texObj = get_tex_obj_for_clear(ctx, "glClearTexImage", texture);
4620
4621   if (texObj == NULL)
4622      return;
4623
4624   _mesa_lock_texture(ctx, texObj);
4625
4626   numImages = get_tex_images_for_clear(ctx, "glClearTexImage",
4627                                        texObj, level, texImages);
4628
4629   for (i = 0; i < numImages; i++) {
4630      if (!check_clear_tex_image(ctx, "glClearTexImage", texImages[i], format,
4631                                 type, data, clearValue[i]))
4632         goto out;
4633   }
4634
4635   for (i = 0; i < numImages; i++) {
4636      ctx->Driver.ClearTexSubImage(ctx, texImages[i],
4637                                   -(GLint) texImages[i]->Border, /* xoffset */
4638                                   -(GLint) texImages[i]->Border, /* yoffset */
4639                                   -(GLint) texImages[i]->Border, /* zoffset */
4640                                   texImages[i]->Width,
4641                                   texImages[i]->Height,
4642                                   texImages[i]->Depth,
4643                                   data ? clearValue[i] : NULL);
4644   }
4645
4646out:
4647   _mesa_unlock_texture(ctx, texObj);
4648}
4649
4650
4651
4652
4653/**********************************************************************/
4654/******                   Compressed Textures                    ******/
4655/**********************************************************************/
4656
4657
4658/**
4659 * Target checking for glCompressedTexSubImage[123]D().
4660 * \return GL_TRUE if error, GL_FALSE if no error
4661 * Must come before other error checking so that the texture object can
4662 * be correctly retrieved using _mesa_get_current_tex_object.
4663 */
4664static GLboolean
4665compressed_subtexture_target_check(struct gl_context *ctx, GLenum target,
4666                                   GLint dims, GLenum intFormat, bool dsa,
4667                                   const char *caller)
4668{
4669   GLboolean targetOK;
4670   mesa_format format;
4671   enum mesa_format_layout layout;
4672
4673   if (dsa && target == GL_TEXTURE_RECTANGLE) {
4674      _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid target %s)", caller,
4675                  _mesa_enum_to_string(target));
4676      return GL_TRUE;
4677   }
4678
4679   switch (dims) {
4680   case 2:
4681      switch (target) {
4682      case GL_TEXTURE_2D:
4683         targetOK = GL_TRUE;
4684         break;
4685      case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
4686      case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
4687      case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
4688      case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
4689      case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
4690      case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
4691         targetOK = ctx->Extensions.ARB_texture_cube_map;
4692         break;
4693      default:
4694         targetOK = GL_FALSE;
4695         break;
4696      }
4697      break;
4698   case 3:
4699      switch (target) {
4700      case GL_TEXTURE_CUBE_MAP:
4701         targetOK = dsa && ctx->Extensions.ARB_texture_cube_map;
4702         break;
4703      case GL_TEXTURE_2D_ARRAY:
4704         targetOK = _mesa_is_gles3(ctx) ||
4705            (_mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_array);
4706         break;
4707      case GL_TEXTURE_CUBE_MAP_ARRAY:
4708         targetOK = _mesa_has_texture_cube_map_array(ctx);
4709         break;
4710      case GL_TEXTURE_3D:
4711         targetOK = GL_TRUE;
4712         /*
4713          * OpenGL 4.5 spec (30.10.2014) says in Section 8.7 Compressed Texture
4714          * Images:
4715          *    "An INVALID_OPERATION error is generated by
4716          *    CompressedTex*SubImage3D if the internal format of the texture
4717          *    is one of the EAC, ETC2, or RGTC formats and either border is
4718          *    non-zero, or the effective target for the texture is not
4719          *    TEXTURE_2D_ARRAY."
4720          *
4721          * NOTE: that's probably a spec error.  It should probably say
4722          *    "... or the effective target for the texture is not
4723          *    TEXTURE_2D_ARRAY, TEXTURE_CUBE_MAP, nor
4724          *    GL_TEXTURE_CUBE_MAP_ARRAY."
4725          * since those targets are 2D images and they support all compression
4726          * formats.
4727          *
4728          * Instead of listing all these, just list those which are allowed,
4729          * which is (at this time) only bptc. Otherwise we'd say s3tc (and
4730          * more) are valid here, which they are not, but of course not
4731          * mentioned by core spec.
4732          *
4733          * Also, from GL_KHR_texture_compression_astc_{hdr,ldr}:
4734          *
4735          *    "Add a second new column "3D Tex." which is empty for all non-ASTC
4736          *     formats. If only the LDR profile is supported by the implementation,
4737          *     this column is also empty for all ASTC formats. If both the LDR and HDR
4738          *     profiles are supported, this column is checked for all ASTC formats."
4739          *
4740          *    "An INVALID_OPERATION error is generated by CompressedTexSubImage3D if
4741          *     <format> is one of the formats in table 8.19 and <target> is not
4742          *     TEXTURE_2D_ARRAY, TEXTURE_CUBE_MAP_ARRAY, or TEXTURE_3D.
4743          *
4744          *     An INVALID_OPERATION error is generated by CompressedTexSubImage3D if
4745          *     <format> is TEXTURE_CUBE_MAP_ARRAY and the "Cube Map Array" column of
4746          *     table 8.19 is *not* checked, or if <format> is TEXTURE_3D and the "3D
4747          *     Tex." column of table 8.19 is *not* checked"
4748          *
4749          * And from GL_KHR_texture_compression_astc_sliced_3d:
4750          *
4751          *    "Modify the "3D Tex." column to be checked for all ASTC formats."
4752          */
4753         format = _mesa_glenum_to_compressed_format(intFormat);
4754         layout = _mesa_get_format_layout(format);
4755         switch (layout) {
4756         case MESA_FORMAT_LAYOUT_BPTC:
4757            /* valid format */
4758            break;
4759         case MESA_FORMAT_LAYOUT_ASTC:
4760            targetOK =
4761               ctx->Extensions.KHR_texture_compression_astc_hdr ||
4762               ctx->Extensions.KHR_texture_compression_astc_sliced_3d;
4763            break;
4764         default:
4765            /* invalid format */
4766            _mesa_error(ctx, GL_INVALID_OPERATION,
4767                        "%s(invalid target %s for format %s)", caller,
4768                        _mesa_enum_to_string(target),
4769                        _mesa_enum_to_string(intFormat));
4770            return GL_TRUE;
4771         }
4772         break;
4773      default:
4774         targetOK = GL_FALSE;
4775      }
4776
4777      break;
4778   default:
4779      assert(dims == 1);
4780      /* no 1D compressed textures at this time */
4781      targetOK = GL_FALSE;
4782      break;
4783   }
4784
4785   if (!targetOK) {
4786      _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid target %s)", caller,
4787                  _mesa_enum_to_string(target));
4788      return GL_TRUE;
4789   }
4790
4791   return GL_FALSE;
4792}
4793
4794/**
4795 * Error checking for glCompressedTexSubImage[123]D().
4796 * \return GL_TRUE if error, GL_FALSE if no error
4797 */
4798static GLboolean
4799compressed_subtexture_error_check(struct gl_context *ctx, GLint dims,
4800                                  const struct gl_texture_object *texObj,
4801                                  GLenum target, GLint level,
4802                                  GLint xoffset, GLint yoffset, GLint zoffset,
4803                                  GLsizei width, GLsizei height, GLsizei depth,
4804                                  GLenum format, GLsizei imageSize,
4805                                  const GLvoid *data, const char *callerName)
4806{
4807   struct gl_texture_image *texImage;
4808   GLint expectedSize;
4809
4810   /* this will catch any invalid compressed format token */
4811   if (!_mesa_is_compressed_format(ctx, format)) {
4812      _mesa_error(ctx, GL_INVALID_ENUM, "%s(format)", callerName);
4813      return GL_TRUE;
4814   }
4815
4816   if (level < 0 || level >= _mesa_max_texture_levels(ctx, target)) {
4817      _mesa_error(ctx, GL_INVALID_VALUE, "%s(level=%d)", callerName, level);
4818      return GL_TRUE;
4819   }
4820
4821   /* validate the bound PBO, if any */
4822   if (!_mesa_validate_pbo_source_compressed(ctx, dims, &ctx->Unpack,
4823                                     imageSize, data, callerName)) {
4824      return GL_TRUE;
4825   }
4826
4827   /* Check for invalid pixel storage modes */
4828   if (!_mesa_compressed_pixel_storage_error_check(ctx, dims,
4829                                                   &ctx->Unpack, callerName)) {
4830      return GL_TRUE;
4831   }
4832
4833   expectedSize = compressed_tex_size(width, height, depth, format);
4834   if (expectedSize != imageSize) {
4835      _mesa_error(ctx, GL_INVALID_VALUE, "%s(size=%d)", callerName, imageSize);
4836      return GL_TRUE;
4837   }
4838
4839   texImage = _mesa_select_tex_image(texObj, target, level);
4840   if (!texImage) {
4841      _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid texture level %d)",
4842                  callerName, level);
4843      return GL_TRUE;
4844   }
4845
4846   if ((GLint) format != texImage->InternalFormat) {
4847      _mesa_error(ctx, GL_INVALID_OPERATION, "%s(format=%s)",
4848                  callerName, _mesa_enum_to_string(format));
4849      return GL_TRUE;
4850   }
4851
4852   if (compressedteximage_only_format(format)) {
4853      _mesa_error(ctx, GL_INVALID_OPERATION, "%s(format=%s cannot be updated)",
4854                  callerName, _mesa_enum_to_string(format));
4855      return GL_TRUE;
4856   }
4857
4858   if (error_check_subtexture_negative_dimensions(ctx, dims, width, height,
4859                                                  depth, callerName)) {
4860      return GL_TRUE;
4861   }
4862
4863   if (error_check_subtexture_dimensions(ctx, dims, texImage, xoffset, yoffset,
4864                                         zoffset, width, height, depth,
4865                                         callerName)) {
4866      return GL_TRUE;
4867   }
4868
4869   return GL_FALSE;
4870}
4871
4872
4873void GLAPIENTRY
4874_mesa_CompressedTexImage1D(GLenum target, GLint level,
4875                              GLenum internalFormat, GLsizei width,
4876                              GLint border, GLsizei imageSize,
4877                              const GLvoid *data)
4878{
4879   GET_CURRENT_CONTEXT(ctx);
4880   teximage_err(ctx, GL_TRUE, 1, target, level, internalFormat,
4881                width, 1, 1, border, GL_NONE, GL_NONE, imageSize, data);
4882}
4883
4884
4885void GLAPIENTRY
4886_mesa_CompressedTexImage2D(GLenum target, GLint level,
4887                              GLenum internalFormat, GLsizei width,
4888                              GLsizei height, GLint border, GLsizei imageSize,
4889                              const GLvoid *data)
4890{
4891   GET_CURRENT_CONTEXT(ctx);
4892   teximage_err(ctx, GL_TRUE, 2, target, level, internalFormat,
4893                width, height, 1, border, GL_NONE, GL_NONE, imageSize, data);
4894}
4895
4896
4897void GLAPIENTRY
4898_mesa_CompressedTexImage3D(GLenum target, GLint level,
4899                              GLenum internalFormat, GLsizei width,
4900                              GLsizei height, GLsizei depth, GLint border,
4901                              GLsizei imageSize, const GLvoid *data)
4902{
4903   GET_CURRENT_CONTEXT(ctx);
4904   teximage_err(ctx, GL_TRUE, 3, target, level, internalFormat, width, height,
4905                depth, border, GL_NONE, GL_NONE, imageSize, data);
4906}
4907
4908
4909void GLAPIENTRY
4910_mesa_CompressedTexImage1D_no_error(GLenum target, GLint level,
4911                                    GLenum internalFormat, GLsizei width,
4912                                    GLint border, GLsizei imageSize,
4913                                    const GLvoid *data)
4914{
4915   GET_CURRENT_CONTEXT(ctx);
4916   teximage_no_error(ctx, GL_TRUE, 1, target, level, internalFormat, width, 1,
4917                     1, border, GL_NONE, GL_NONE, imageSize, data);
4918}
4919
4920
4921void GLAPIENTRY
4922_mesa_CompressedTexImage2D_no_error(GLenum target, GLint level,
4923                                    GLenum internalFormat, GLsizei width,
4924                                    GLsizei height, GLint border,
4925                                    GLsizei imageSize, const GLvoid *data)
4926{
4927   GET_CURRENT_CONTEXT(ctx);
4928   teximage_no_error(ctx, GL_TRUE, 2, target, level, internalFormat, width,
4929                     height, 1, border, GL_NONE, GL_NONE, imageSize, data);
4930}
4931
4932
4933void GLAPIENTRY
4934_mesa_CompressedTexImage3D_no_error(GLenum target, GLint level,
4935                                    GLenum internalFormat, GLsizei width,
4936                                    GLsizei height, GLsizei depth, GLint border,
4937                                    GLsizei imageSize, const GLvoid *data)
4938{
4939   GET_CURRENT_CONTEXT(ctx);
4940   teximage_no_error(ctx, GL_TRUE, 3, target, level, internalFormat, width,
4941                     height, depth, border, GL_NONE, GL_NONE, imageSize, data);
4942}
4943
4944
4945/**
4946 * Common helper for glCompressedTexSubImage1/2/3D() and
4947 * glCompressedTextureSubImage1/2/3D().
4948 */
4949static void
4950compressed_texture_sub_image(struct gl_context *ctx, GLuint dims,
4951                             struct gl_texture_object *texObj,
4952                             struct gl_texture_image *texImage,
4953                             GLenum target, GLint level, GLint xoffset,
4954                             GLint yoffset, GLint zoffset, GLsizei width,
4955                             GLsizei height, GLsizei depth, GLenum format,
4956                             GLsizei imageSize, const GLvoid *data)
4957{
4958   FLUSH_VERTICES(ctx, 0);
4959
4960   _mesa_lock_texture(ctx, texObj);
4961   {
4962      if (width > 0 && height > 0 && depth > 0) {
4963         ctx->Driver.CompressedTexSubImage(ctx, dims, texImage,
4964                                           xoffset, yoffset, zoffset,
4965                                           width, height, depth,
4966                                           format, imageSize, data);
4967
4968         check_gen_mipmap(ctx, target, texObj, level);
4969
4970         /* NOTE: Don't signal _NEW_TEXTURE_OBJECT since we've only changed
4971          * the texel data, not the texture format, size, etc.
4972          */
4973      }
4974   }
4975   _mesa_unlock_texture(ctx, texObj);
4976}
4977
4978
4979static ALWAYS_INLINE void
4980compressed_tex_sub_image(unsigned dim, GLenum target, GLuint texture,
4981                         GLint level, GLint xoffset, GLint yoffset,
4982                         GLint zoffset, GLsizei width, GLsizei height,
4983                         GLsizei depth, GLenum format, GLsizei imageSize,
4984                         const GLvoid *data, bool dsa, bool no_error,
4985                         const char *caller)
4986{
4987   struct gl_texture_object *texObj = NULL;
4988   struct gl_texture_image *texImage;
4989
4990   GET_CURRENT_CONTEXT(ctx);
4991
4992   if (dsa) {
4993      if (no_error) {
4994         texObj = _mesa_lookup_texture(ctx, texture);
4995      } else {
4996         texObj = _mesa_lookup_texture_err(ctx, texture, caller);
4997         if (!texObj)
4998            return;
4999      }
5000
5001      target = texObj->Target;
5002   }
5003
5004   if (!no_error &&
5005       compressed_subtexture_target_check(ctx, target, dim, format, dsa,
5006                                          caller)) {
5007      return;
5008   }
5009
5010   if (!dsa) {
5011      texObj = _mesa_get_current_tex_object(ctx, target);
5012         if (!no_error && !texObj)
5013            return;
5014   }
5015
5016   if (!no_error &&
5017       compressed_subtexture_error_check(ctx, dim, texObj, target, level,
5018                                         xoffset, yoffset, zoffset, width,
5019                                         height, depth, format,
5020                                         imageSize, data, caller)) {
5021      return;
5022   }
5023
5024   /* Must handle special case GL_TEXTURE_CUBE_MAP. */
5025   if (dim == 3 && dsa && texObj->Target == GL_TEXTURE_CUBE_MAP) {
5026      const char *pixels = data;
5027      GLint image_stride;
5028
5029      /* Make sure the texture object is a proper cube.
5030       * (See texturesubimage in teximage.c for details on why this check is
5031       * performed.)
5032       */
5033      if (!no_error && !_mesa_cube_level_complete(texObj, level)) {
5034         _mesa_error(ctx, GL_INVALID_OPERATION,
5035                     "glCompressedTextureSubImage3D(cube map incomplete)");
5036         return;
5037      }
5038
5039      /* Copy in each face. */
5040      for (int i = zoffset; i < zoffset + depth; ++i) {
5041         texImage = texObj->Image[i][level];
5042         assert(texImage);
5043
5044         compressed_texture_sub_image(ctx, 3, texObj, texImage,
5045                                      texObj->Target, level, xoffset, yoffset,
5046                                      0, width, height, 1, format,
5047                                      imageSize, pixels);
5048
5049         /* Compressed images don't have a client format */
5050         image_stride = _mesa_format_image_size(texImage->TexFormat,
5051                                                texImage->Width,
5052                                                texImage->Height, 1);
5053
5054         pixels += image_stride;
5055         imageSize -= image_stride;
5056      }
5057   } else {
5058      texImage = _mesa_select_tex_image(texObj, target, level);
5059      assert(texImage);
5060
5061      compressed_texture_sub_image(ctx, dim, texObj, texImage, target, level,
5062                                   xoffset, yoffset, zoffset, width, height,
5063                                   depth, format, imageSize, data);
5064   }
5065}
5066
5067static void
5068compressed_tex_sub_image_error(unsigned dim, GLenum target, GLuint texture,
5069                               GLint level, GLint xoffset, GLint yoffset,
5070                               GLint zoffset, GLsizei width, GLsizei height,
5071                               GLsizei depth, GLenum format, GLsizei imageSize,
5072                               const GLvoid *data, bool dsa,
5073                               const char *caller)
5074{
5075   compressed_tex_sub_image(dim, target, texture, level, xoffset, yoffset,
5076                            zoffset, width, height, depth, format, imageSize,
5077                            data, dsa, false, caller);
5078}
5079
5080static void
5081compressed_tex_sub_image_no_error(unsigned dim, GLenum target, GLuint texture,
5082                                  GLint level, GLint xoffset, GLint yoffset,
5083                                  GLint zoffset, GLsizei width, GLsizei height,
5084                                  GLsizei depth, GLenum format, GLsizei imageSize,
5085                                  const GLvoid *data, bool dsa,
5086                                  const char *caller)
5087{
5088   compressed_tex_sub_image(dim, target, texture, level, xoffset, yoffset,
5089                            zoffset, width, height, depth, format, imageSize,
5090                            data, dsa, true, caller);
5091}
5092
5093void GLAPIENTRY
5094_mesa_CompressedTexSubImage1D_no_error(GLenum target, GLint level,
5095                                       GLint xoffset, GLsizei width,
5096                                       GLenum format, GLsizei imageSize,
5097                                       const GLvoid *data)
5098{
5099   compressed_tex_sub_image_no_error(1, target, 0, level, xoffset, 0, 0, width,
5100                                     1, 1, format, imageSize, data, false,
5101                                     "glCompressedTexSubImage1D");
5102}
5103
5104
5105void GLAPIENTRY
5106_mesa_CompressedTexSubImage1D(GLenum target, GLint level, GLint xoffset,
5107                              GLsizei width, GLenum format,
5108                              GLsizei imageSize, const GLvoid *data)
5109{
5110   compressed_tex_sub_image_error(1, target, 0, level, xoffset, 0, 0, width, 1,
5111                                  1, format, imageSize, data, false,
5112                                  "glCompressedTexSubImage1D");
5113}
5114
5115
5116void GLAPIENTRY
5117_mesa_CompressedTextureSubImage1D_no_error(GLuint texture, GLint level,
5118                                           GLint xoffset, GLsizei width,
5119                                           GLenum format, GLsizei imageSize,
5120                                           const GLvoid *data)
5121{
5122   compressed_tex_sub_image_no_error(1, 0, texture, level, xoffset, 0, 0, width,
5123                                     1, 1, format, imageSize, data, true,
5124                                     "glCompressedTextureSubImage1D");
5125}
5126
5127
5128void GLAPIENTRY
5129_mesa_CompressedTextureSubImage1D(GLuint texture, GLint level, GLint xoffset,
5130                                  GLsizei width, GLenum format,
5131                                  GLsizei imageSize, const GLvoid *data)
5132{
5133   compressed_tex_sub_image_error(1, 0, texture, level, xoffset, 0, 0, width,
5134                                  1, 1, format, imageSize, data, true,
5135                                  "glCompressedTextureSubImage1D");
5136}
5137
5138void GLAPIENTRY
5139_mesa_CompressedTexSubImage2D_no_error(GLenum target, GLint level,
5140                                       GLint xoffset, GLint yoffset,
5141                                       GLsizei width, GLsizei height,
5142                                       GLenum format, GLsizei imageSize,
5143                                       const GLvoid *data)
5144{
5145   compressed_tex_sub_image_no_error(2, target, 0, level, xoffset, yoffset, 0,
5146                                     width, height, 1, format, imageSize, data,
5147                                     false, "glCompressedTexSubImage2D");
5148}
5149
5150
5151void GLAPIENTRY
5152_mesa_CompressedTexSubImage2D(GLenum target, GLint level, GLint xoffset,
5153                              GLint yoffset, GLsizei width, GLsizei height,
5154                              GLenum format, GLsizei imageSize,
5155                              const GLvoid *data)
5156{
5157   compressed_tex_sub_image_error(2, target, 0, level, xoffset, yoffset, 0,
5158                                  width, height, 1, format, imageSize, data,
5159                                  false, "glCompressedTexSubImage2D");
5160}
5161
5162
5163void GLAPIENTRY
5164_mesa_CompressedTextureSubImage2D_no_error(GLuint texture, GLint level,
5165                                           GLint xoffset, GLint yoffset,
5166                                           GLsizei width, GLsizei height,
5167                                           GLenum format, GLsizei imageSize,
5168                                           const GLvoid *data)
5169{
5170   compressed_tex_sub_image_no_error(2, 0, texture, level, xoffset, yoffset, 0,
5171                                     width, height, 1, format, imageSize, data,
5172                                     true, "glCompressedTextureSubImage2D");
5173}
5174
5175
5176void GLAPIENTRY
5177_mesa_CompressedTextureSubImage2D(GLuint texture, GLint level, GLint xoffset,
5178                                  GLint yoffset,
5179                                  GLsizei width, GLsizei height,
5180                                  GLenum format, GLsizei imageSize,
5181                                  const GLvoid *data)
5182{
5183   compressed_tex_sub_image_error(2, 0, texture, level, xoffset, yoffset, 0,
5184                                  width, height, 1, format, imageSize, data,
5185                                  true, "glCompressedTextureSubImage2D");
5186}
5187
5188void GLAPIENTRY
5189_mesa_CompressedTexSubImage3D_no_error(GLenum target, GLint level,
5190                                       GLint xoffset, GLint yoffset,
5191                                       GLint zoffset, GLsizei width,
5192                                       GLsizei height, GLsizei depth,
5193                                       GLenum format, GLsizei imageSize,
5194                                       const GLvoid *data)
5195{
5196   compressed_tex_sub_image_no_error(3, target, 0, level, xoffset, yoffset,
5197                                     zoffset, width, height, depth, format,
5198                                     imageSize, data, false,
5199                                     "glCompressedTexSubImage3D");
5200}
5201
5202void GLAPIENTRY
5203_mesa_CompressedTexSubImage3D(GLenum target, GLint level, GLint xoffset,
5204                              GLint yoffset, GLint zoffset, GLsizei width,
5205                              GLsizei height, GLsizei depth, GLenum format,
5206                              GLsizei imageSize, const GLvoid *data)
5207{
5208   compressed_tex_sub_image_error(3, target, 0, level, xoffset, yoffset,
5209                                  zoffset, width, height, depth, format,
5210                                  imageSize, data, false,
5211                                  "glCompressedTexSubImage3D");
5212}
5213
5214void GLAPIENTRY
5215_mesa_CompressedTextureSubImage3D_no_error(GLuint texture, GLint level,
5216                                           GLint xoffset, GLint yoffset,
5217                                           GLint zoffset, GLsizei width,
5218                                           GLsizei height, GLsizei depth,
5219                                           GLenum format, GLsizei imageSize,
5220                                           const GLvoid *data)
5221{
5222   compressed_tex_sub_image_no_error(3, 0, texture, level, xoffset, yoffset,
5223                                     zoffset, width, height, depth, format,
5224                                     imageSize, data, true,
5225                                     "glCompressedTextureSubImage3D");
5226}
5227
5228void GLAPIENTRY
5229_mesa_CompressedTextureSubImage3D(GLuint texture, GLint level, GLint xoffset,
5230                                  GLint yoffset, GLint zoffset, GLsizei width,
5231                                  GLsizei height, GLsizei depth,
5232                                  GLenum format, GLsizei imageSize,
5233                                  const GLvoid *data)
5234{
5235   compressed_tex_sub_image_error(3, 0, texture, level, xoffset, yoffset,
5236                                  zoffset, width, height, depth, format,
5237                                  imageSize, data, true,
5238                                  "glCompressedTextureSubImage3D");
5239}
5240
5241mesa_format
5242_mesa_get_texbuffer_format(const struct gl_context *ctx, GLenum internalFormat)
5243{
5244   if (ctx->API == API_OPENGL_COMPAT) {
5245      switch (internalFormat) {
5246      case GL_ALPHA8:
5247         return MESA_FORMAT_A_UNORM8;
5248      case GL_ALPHA16:
5249         return MESA_FORMAT_A_UNORM16;
5250      case GL_ALPHA16F_ARB:
5251         return MESA_FORMAT_A_FLOAT16;
5252      case GL_ALPHA32F_ARB:
5253         return MESA_FORMAT_A_FLOAT32;
5254      case GL_ALPHA8I_EXT:
5255         return MESA_FORMAT_A_SINT8;
5256      case GL_ALPHA16I_EXT:
5257         return MESA_FORMAT_A_SINT16;
5258      case GL_ALPHA32I_EXT:
5259         return MESA_FORMAT_A_SINT32;
5260      case GL_ALPHA8UI_EXT:
5261         return MESA_FORMAT_A_UINT8;
5262      case GL_ALPHA16UI_EXT:
5263         return MESA_FORMAT_A_UINT16;
5264      case GL_ALPHA32UI_EXT:
5265         return MESA_FORMAT_A_UINT32;
5266      case GL_LUMINANCE8:
5267         return MESA_FORMAT_L_UNORM8;
5268      case GL_LUMINANCE16:
5269         return MESA_FORMAT_L_UNORM16;
5270      case GL_LUMINANCE16F_ARB:
5271         return MESA_FORMAT_L_FLOAT16;
5272      case GL_LUMINANCE32F_ARB:
5273         return MESA_FORMAT_L_FLOAT32;
5274      case GL_LUMINANCE8I_EXT:
5275         return MESA_FORMAT_L_SINT8;
5276      case GL_LUMINANCE16I_EXT:
5277         return MESA_FORMAT_L_SINT16;
5278      case GL_LUMINANCE32I_EXT:
5279         return MESA_FORMAT_L_SINT32;
5280      case GL_LUMINANCE8UI_EXT:
5281         return MESA_FORMAT_L_UINT8;
5282      case GL_LUMINANCE16UI_EXT:
5283         return MESA_FORMAT_L_UINT16;
5284      case GL_LUMINANCE32UI_EXT:
5285         return MESA_FORMAT_L_UINT32;
5286      case GL_LUMINANCE8_ALPHA8:
5287         return MESA_FORMAT_L8A8_UNORM;
5288      case GL_LUMINANCE16_ALPHA16:
5289         return MESA_FORMAT_L16A16_UNORM;
5290      case GL_LUMINANCE_ALPHA16F_ARB:
5291         return MESA_FORMAT_LA_FLOAT16;
5292      case GL_LUMINANCE_ALPHA32F_ARB:
5293         return MESA_FORMAT_LA_FLOAT32;
5294      case GL_LUMINANCE_ALPHA8I_EXT:
5295         return MESA_FORMAT_LA_SINT8;
5296      case GL_LUMINANCE_ALPHA16I_EXT:
5297         return MESA_FORMAT_LA_SINT16;
5298      case GL_LUMINANCE_ALPHA32I_EXT:
5299         return MESA_FORMAT_LA_SINT32;
5300      case GL_LUMINANCE_ALPHA8UI_EXT:
5301         return MESA_FORMAT_LA_UINT8;
5302      case GL_LUMINANCE_ALPHA16UI_EXT:
5303         return MESA_FORMAT_LA_UINT16;
5304      case GL_LUMINANCE_ALPHA32UI_EXT:
5305         return MESA_FORMAT_LA_UINT32;
5306      case GL_INTENSITY8:
5307         return MESA_FORMAT_I_UNORM8;
5308      case GL_INTENSITY16:
5309         return MESA_FORMAT_I_UNORM16;
5310      case GL_INTENSITY16F_ARB:
5311         return MESA_FORMAT_I_FLOAT16;
5312      case GL_INTENSITY32F_ARB:
5313         return MESA_FORMAT_I_FLOAT32;
5314      case GL_INTENSITY8I_EXT:
5315         return MESA_FORMAT_I_SINT8;
5316      case GL_INTENSITY16I_EXT:
5317         return MESA_FORMAT_I_SINT16;
5318      case GL_INTENSITY32I_EXT:
5319         return MESA_FORMAT_I_SINT32;
5320      case GL_INTENSITY8UI_EXT:
5321         return MESA_FORMAT_I_UINT8;
5322      case GL_INTENSITY16UI_EXT:
5323         return MESA_FORMAT_I_UINT16;
5324      case GL_INTENSITY32UI_EXT:
5325         return MESA_FORMAT_I_UINT32;
5326      default:
5327         break;
5328      }
5329   }
5330
5331   if (_mesa_has_ARB_texture_buffer_object_rgb32(ctx) ||
5332       _mesa_has_OES_texture_buffer(ctx)) {
5333      switch (internalFormat) {
5334      case GL_RGB32F:
5335         return MESA_FORMAT_RGB_FLOAT32;
5336      case GL_RGB32UI:
5337         return MESA_FORMAT_RGB_UINT32;
5338      case GL_RGB32I:
5339         return MESA_FORMAT_RGB_SINT32;
5340      default:
5341         break;
5342      }
5343   }
5344
5345   switch (internalFormat) {
5346   case GL_RGBA8:
5347      return MESA_FORMAT_R8G8B8A8_UNORM;
5348   case GL_RGBA16:
5349      if (_mesa_is_gles(ctx) && !_mesa_has_EXT_texture_norm16(ctx))
5350         return MESA_FORMAT_NONE;
5351      return MESA_FORMAT_RGBA_UNORM16;
5352   case GL_RGBA16F_ARB:
5353      return MESA_FORMAT_RGBA_FLOAT16;
5354   case GL_RGBA32F_ARB:
5355      return MESA_FORMAT_RGBA_FLOAT32;
5356   case GL_RGBA8I_EXT:
5357      return MESA_FORMAT_RGBA_SINT8;
5358   case GL_RGBA16I_EXT:
5359      return MESA_FORMAT_RGBA_SINT16;
5360   case GL_RGBA32I_EXT:
5361      return MESA_FORMAT_RGBA_SINT32;
5362   case GL_RGBA8UI_EXT:
5363      return MESA_FORMAT_RGBA_UINT8;
5364   case GL_RGBA16UI_EXT:
5365      return MESA_FORMAT_RGBA_UINT16;
5366   case GL_RGBA32UI_EXT:
5367      return MESA_FORMAT_RGBA_UINT32;
5368
5369   case GL_RG8:
5370      return MESA_FORMAT_R8G8_UNORM;
5371   case GL_RG16:
5372      if (_mesa_is_gles(ctx) && !_mesa_has_EXT_texture_norm16(ctx))
5373         return MESA_FORMAT_NONE;
5374      return MESA_FORMAT_R16G16_UNORM;
5375   case GL_RG16F:
5376      return MESA_FORMAT_RG_FLOAT16;
5377   case GL_RG32F:
5378      return MESA_FORMAT_RG_FLOAT32;
5379   case GL_RG8I:
5380      return MESA_FORMAT_RG_SINT8;
5381   case GL_RG16I:
5382      return MESA_FORMAT_RG_SINT16;
5383   case GL_RG32I:
5384      return MESA_FORMAT_RG_SINT32;
5385   case GL_RG8UI:
5386      return MESA_FORMAT_RG_UINT8;
5387   case GL_RG16UI:
5388      return MESA_FORMAT_RG_UINT16;
5389   case GL_RG32UI:
5390      return MESA_FORMAT_RG_UINT32;
5391
5392   case GL_R8:
5393      return MESA_FORMAT_R_UNORM8;
5394   case GL_R16:
5395      if (_mesa_is_gles(ctx) && !_mesa_has_EXT_texture_norm16(ctx))
5396         return MESA_FORMAT_NONE;
5397      return MESA_FORMAT_R_UNORM16;
5398   case GL_R16F:
5399      return MESA_FORMAT_R_FLOAT16;
5400   case GL_R32F:
5401      return MESA_FORMAT_R_FLOAT32;
5402   case GL_R8I:
5403      return MESA_FORMAT_R_SINT8;
5404   case GL_R16I:
5405      return MESA_FORMAT_R_SINT16;
5406   case GL_R32I:
5407      return MESA_FORMAT_R_SINT32;
5408   case GL_R8UI:
5409      return MESA_FORMAT_R_UINT8;
5410   case GL_R16UI:
5411      return MESA_FORMAT_R_UINT16;
5412   case GL_R32UI:
5413      return MESA_FORMAT_R_UINT32;
5414
5415   default:
5416      return MESA_FORMAT_NONE;
5417   }
5418}
5419
5420
5421mesa_format
5422_mesa_validate_texbuffer_format(const struct gl_context *ctx,
5423                                GLenum internalFormat)
5424{
5425   mesa_format format = _mesa_get_texbuffer_format(ctx, internalFormat);
5426   GLenum datatype;
5427
5428   if (format == MESA_FORMAT_NONE)
5429      return MESA_FORMAT_NONE;
5430
5431   datatype = _mesa_get_format_datatype(format);
5432
5433   /* The GL_ARB_texture_buffer_object spec says:
5434    *
5435    *     "If ARB_texture_float is not supported, references to the
5436    *     floating-point internal formats provided by that extension should be
5437    *     removed, and such formats may not be passed to TexBufferARB."
5438    *
5439    * As a result, GL_HALF_FLOAT internal format depends on both
5440    * GL_ARB_texture_float and GL_ARB_half_float_pixel.
5441    */
5442   if ((datatype == GL_FLOAT || datatype == GL_HALF_FLOAT) &&
5443       !ctx->Extensions.ARB_texture_float)
5444      return MESA_FORMAT_NONE;
5445
5446   if (!ctx->Extensions.ARB_texture_rg) {
5447      GLenum base_format = _mesa_get_format_base_format(format);
5448      if (base_format == GL_R || base_format == GL_RG)
5449         return MESA_FORMAT_NONE;
5450   }
5451
5452   if (!ctx->Extensions.ARB_texture_buffer_object_rgb32) {
5453      GLenum base_format = _mesa_get_format_base_format(format);
5454      if (base_format == GL_RGB)
5455         return MESA_FORMAT_NONE;
5456   }
5457   return format;
5458}
5459
5460
5461/**
5462 * Do work common to glTexBuffer, glTexBufferRange, glTextureBuffer
5463 * and glTextureBufferRange, including some error checking.
5464 */
5465static void
5466texture_buffer_range(struct gl_context *ctx,
5467                     struct gl_texture_object *texObj,
5468                     GLenum internalFormat,
5469                     struct gl_buffer_object *bufObj,
5470                     GLintptr offset, GLsizeiptr size,
5471                     const char *caller)
5472{
5473   GLintptr oldOffset = texObj->BufferOffset;
5474   GLsizeiptr oldSize = texObj->BufferSize;
5475   mesa_format format;
5476
5477   /* NOTE: ARB_texture_buffer_object might not be supported in
5478    * the compatibility profile.
5479    */
5480   if (!_mesa_has_ARB_texture_buffer_object(ctx) &&
5481       !_mesa_has_OES_texture_buffer(ctx)) {
5482      _mesa_error(ctx, GL_INVALID_OPERATION,
5483                  "%s(ARB_texture_buffer_object is not"
5484                  " implemented for the compatibility profile)", caller);
5485      return;
5486   }
5487
5488   if (texObj->HandleAllocated) {
5489      /* The ARB_bindless_texture spec says:
5490       *
5491       * "The error INVALID_OPERATION is generated by TexImage*, CopyTexImage*,
5492       *  CompressedTexImage*, TexBuffer*, TexParameter*, as well as other
5493       *  functions defined in terms of these, if the texture object to be
5494       *  modified is referenced by one or more texture or image handles."
5495       */
5496      _mesa_error(ctx, GL_INVALID_OPERATION, "%s(immutable texture)", caller);
5497      return;
5498   }
5499
5500   format = _mesa_validate_texbuffer_format(ctx, internalFormat);
5501   if (format == MESA_FORMAT_NONE) {
5502      _mesa_error(ctx, GL_INVALID_ENUM, "%s(internalFormat %s)",
5503                  caller, _mesa_enum_to_string(internalFormat));
5504      return;
5505   }
5506
5507   FLUSH_VERTICES(ctx, 0);
5508
5509   _mesa_lock_texture(ctx, texObj);
5510   {
5511      _mesa_reference_buffer_object(ctx, &texObj->BufferObject, bufObj);
5512      texObj->BufferObjectFormat = internalFormat;
5513      texObj->_BufferObjectFormat = format;
5514      texObj->BufferOffset = offset;
5515      texObj->BufferSize = size;
5516   }
5517   _mesa_unlock_texture(ctx, texObj);
5518
5519   if (ctx->Driver.TexParameter) {
5520      if (offset != oldOffset) {
5521         ctx->Driver.TexParameter(ctx, texObj, GL_TEXTURE_BUFFER_OFFSET);
5522      }
5523      if (size != oldSize) {
5524         ctx->Driver.TexParameter(ctx, texObj, GL_TEXTURE_BUFFER_SIZE);
5525      }
5526   }
5527
5528   ctx->NewDriverState |= ctx->DriverFlags.NewTextureBuffer;
5529
5530   if (bufObj) {
5531      bufObj->UsageHistory |= USAGE_TEXTURE_BUFFER;
5532   }
5533}
5534
5535
5536/**
5537 * Make sure the texture buffer target is GL_TEXTURE_BUFFER.
5538 * Return true if it is, and return false if it is not
5539 * (and throw INVALID ENUM as dictated in the OpenGL 4.5
5540 * core spec, 02.02.2015, PDF page 245).
5541 */
5542static bool
5543check_texture_buffer_target(struct gl_context *ctx, GLenum target,
5544                            const char *caller)
5545{
5546   if (target != GL_TEXTURE_BUFFER_ARB) {
5547      _mesa_error(ctx, GL_INVALID_ENUM,
5548                  "%s(texture target is not GL_TEXTURE_BUFFER)", caller);
5549      return false;
5550   }
5551   else
5552      return true;
5553}
5554
5555/**
5556 * Check for errors related to the texture buffer range.
5557 * Return false if errors are found, true if none are found.
5558 */
5559static bool
5560check_texture_buffer_range(struct gl_context *ctx,
5561                           struct gl_buffer_object *bufObj,
5562                           GLintptr offset, GLsizeiptr size,
5563                           const char *caller)
5564{
5565   /* OpenGL 4.5 core spec (02.02.2015) says in Section 8.9 Buffer
5566    * Textures (PDF page 245):
5567    *    "An INVALID_VALUE error is generated if offset is negative, if
5568    *    size is less than or equal to zero, or if offset + size is greater
5569    *    than the value of BUFFER_SIZE for the buffer bound to target."
5570    */
5571   if (offset < 0) {
5572      _mesa_error(ctx, GL_INVALID_VALUE, "%s(offset=%d < 0)", caller,
5573                  (int) offset);
5574      return false;
5575   }
5576
5577   if (size <= 0) {
5578      _mesa_error(ctx, GL_INVALID_VALUE, "%s(size=%d <= 0)", caller,
5579                  (int) size);
5580      return false;
5581   }
5582
5583   if (offset + size > bufObj->Size) {
5584      _mesa_error(ctx, GL_INVALID_VALUE,
5585                  "%s(offset=%d + size=%d > buffer_size=%d)", caller,
5586                  (int) offset, (int) size, (int) bufObj->Size);
5587      return false;
5588   }
5589
5590   /* OpenGL 4.5 core spec (02.02.2015) says in Section 8.9 Buffer
5591    * Textures (PDF page 245):
5592    *    "An INVALID_VALUE error is generated if offset is not an integer
5593    *    multiple of the value of TEXTURE_BUFFER_OFFSET_ALIGNMENT."
5594    */
5595   if (offset % ctx->Const.TextureBufferOffsetAlignment) {
5596      _mesa_error(ctx, GL_INVALID_VALUE,
5597                  "%s(invalid offset alignment)", caller);
5598      return false;
5599   }
5600
5601   return true;
5602}
5603
5604
5605/** GL_ARB_texture_buffer_object */
5606void GLAPIENTRY
5607_mesa_TexBuffer(GLenum target, GLenum internalFormat, GLuint buffer)
5608{
5609   struct gl_texture_object *texObj;
5610   struct gl_buffer_object *bufObj;
5611
5612   GET_CURRENT_CONTEXT(ctx);
5613
5614   /* Need to catch a bad target before it gets to
5615    * _mesa_get_current_tex_object.
5616    */
5617   if (!check_texture_buffer_target(ctx, target, "glTexBuffer"))
5618      return;
5619
5620   if (buffer) {
5621      bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, "glTexBuffer");
5622      if (!bufObj)
5623         return;
5624   } else
5625      bufObj = NULL;
5626
5627   texObj = _mesa_get_current_tex_object(ctx, target);
5628   if (!texObj)
5629      return;
5630
5631   texture_buffer_range(ctx, texObj, internalFormat, bufObj, 0,
5632                        buffer ? -1 : 0, "glTexBuffer");
5633}
5634
5635
5636/** GL_ARB_texture_buffer_range */
5637void GLAPIENTRY
5638_mesa_TexBufferRange(GLenum target, GLenum internalFormat, GLuint buffer,
5639                     GLintptr offset, GLsizeiptr size)
5640{
5641   struct gl_texture_object *texObj;
5642   struct gl_buffer_object *bufObj;
5643
5644   GET_CURRENT_CONTEXT(ctx);
5645
5646   /* Need to catch a bad target before it gets to
5647    * _mesa_get_current_tex_object.
5648    */
5649   if (!check_texture_buffer_target(ctx, target, "glTexBufferRange"))
5650      return;
5651
5652   if (buffer) {
5653      bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, "glTexBufferRange");
5654      if (!bufObj)
5655         return;
5656
5657      if (!check_texture_buffer_range(ctx, bufObj, offset, size,
5658          "glTexBufferRange"))
5659         return;
5660
5661   } else {
5662      /* OpenGL 4.5 core spec (02.02.2015) says in Section 8.9 Buffer
5663       * Textures (PDF page 254):
5664       *    "If buffer is zero, then any buffer object attached to the buffer
5665       *    texture is detached, the values offset and size are ignored and
5666       *    the state for offset and size for the buffer texture are reset to
5667       *    zero."
5668       */
5669      offset = 0;
5670      size = 0;
5671      bufObj = NULL;
5672   }
5673
5674   texObj = _mesa_get_current_tex_object(ctx, target);
5675   if (!texObj)
5676      return;
5677
5678   texture_buffer_range(ctx, texObj, internalFormat, bufObj,
5679                        offset, size, "glTexBufferRange");
5680}
5681
5682void GLAPIENTRY
5683_mesa_TextureBuffer(GLuint texture, GLenum internalFormat, GLuint buffer)
5684{
5685   struct gl_texture_object *texObj;
5686   struct gl_buffer_object *bufObj;
5687
5688   GET_CURRENT_CONTEXT(ctx);
5689
5690   if (buffer) {
5691      bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, "glTextureBuffer");
5692      if (!bufObj)
5693         return;
5694   } else
5695      bufObj = NULL;
5696
5697   /* Get the texture object by Name. */
5698   texObj = _mesa_lookup_texture_err(ctx, texture, "glTextureBuffer");
5699   if (!texObj)
5700      return;
5701
5702   if (!check_texture_buffer_target(ctx, texObj->Target, "glTextureBuffer"))
5703      return;
5704
5705   texture_buffer_range(ctx, texObj, internalFormat,
5706                        bufObj, 0, buffer ? -1 : 0, "glTextureBuffer");
5707}
5708
5709void GLAPIENTRY
5710_mesa_TextureBufferRange(GLuint texture, GLenum internalFormat, GLuint buffer,
5711                         GLintptr offset, GLsizeiptr size)
5712{
5713   struct gl_texture_object *texObj;
5714   struct gl_buffer_object *bufObj;
5715
5716   GET_CURRENT_CONTEXT(ctx);
5717
5718   if (buffer) {
5719      bufObj = _mesa_lookup_bufferobj_err(ctx, buffer,
5720                                          "glTextureBufferRange");
5721      if (!bufObj)
5722         return;
5723
5724      if (!check_texture_buffer_range(ctx, bufObj, offset, size,
5725          "glTextureBufferRange"))
5726         return;
5727
5728   } else {
5729      /* OpenGL 4.5 core spec (02.02.2015) says in Section 8.9 Buffer
5730       * Textures (PDF page 254):
5731       *    "If buffer is zero, then any buffer object attached to the buffer
5732       *    texture is detached, the values offset and size are ignored and
5733       *    the state for offset and size for the buffer texture are reset to
5734       *    zero."
5735       */
5736      offset = 0;
5737      size = 0;
5738      bufObj = NULL;
5739   }
5740
5741   /* Get the texture object by Name. */
5742   texObj = _mesa_lookup_texture_err(ctx, texture, "glTextureBufferRange");
5743   if (!texObj)
5744      return;
5745
5746   if (!check_texture_buffer_target(ctx, texObj->Target,
5747       "glTextureBufferRange"))
5748      return;
5749
5750   texture_buffer_range(ctx, texObj, internalFormat,
5751                        bufObj, offset, size, "glTextureBufferRange");
5752}
5753
5754GLboolean
5755_mesa_is_renderable_texture_format(const struct gl_context *ctx,
5756                                   GLenum internalformat)
5757{
5758   /* Everything that is allowed for renderbuffers,
5759    * except for a base format of GL_STENCIL_INDEX, unless supported.
5760    */
5761   GLenum baseFormat = _mesa_base_fbo_format(ctx, internalformat);
5762   if (ctx->Extensions.ARB_texture_stencil8)
5763      return baseFormat != 0;
5764   else
5765      return baseFormat != 0 && baseFormat != GL_STENCIL_INDEX;
5766}
5767
5768
5769/** GL_ARB_texture_multisample */
5770static GLboolean
5771check_multisample_target(GLuint dims, GLenum target, bool dsa)
5772{
5773   switch(target) {
5774   case GL_TEXTURE_2D_MULTISAMPLE:
5775      return dims == 2;
5776   case GL_PROXY_TEXTURE_2D_MULTISAMPLE:
5777      return dims == 2 && !dsa;
5778   case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
5779      return dims == 3;
5780   case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY:
5781      return dims == 3 && !dsa;
5782   default:
5783      return GL_FALSE;
5784   }
5785}
5786
5787
5788static void
5789texture_image_multisample(struct gl_context *ctx, GLuint dims,
5790                          struct gl_texture_object *texObj,
5791                          struct gl_memory_object *memObj,
5792                          GLenum target, GLsizei samples,
5793                          GLint internalformat, GLsizei width,
5794                          GLsizei height, GLsizei depth,
5795                          GLboolean fixedsamplelocations,
5796                          GLboolean immutable, GLuint64 offset,
5797                          const char *func)
5798{
5799   struct gl_texture_image *texImage;
5800   GLboolean sizeOK, dimensionsOK, samplesOK;
5801   mesa_format texFormat;
5802   GLenum sample_count_error;
5803   bool dsa = strstr(func, "ture") ? true : false;
5804
5805   if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE)) {
5806      _mesa_debug(ctx, "%s(target=%s, samples=%d)\n", func,
5807                  _mesa_enum_to_string(target), samples);
5808   }
5809
5810   if (!((ctx->Extensions.ARB_texture_multisample
5811         && _mesa_is_desktop_gl(ctx))) && !_mesa_is_gles31(ctx)) {
5812      _mesa_error(ctx, GL_INVALID_OPERATION, "%s(unsupported)", func);
5813      return;
5814   }
5815
5816   if (samples < 1) {
5817      _mesa_error(ctx, GL_INVALID_VALUE, "%s(samples < 1)", func);
5818      return;
5819   }
5820
5821   if (!check_multisample_target(dims, target, dsa)) {
5822      GLenum err = dsa ? GL_INVALID_OPERATION : GL_INVALID_ENUM;
5823      _mesa_error(ctx, err, "%s(target=%s)", func,
5824                  _mesa_enum_to_string(target));
5825      return;
5826   }
5827
5828   /* check that the specified internalformat is color/depth/stencil-renderable;
5829    * refer GL3.1 spec 4.4.4
5830    */
5831
5832   if (immutable && !_mesa_is_legal_tex_storage_format(ctx, internalformat)) {
5833      _mesa_error(ctx, GL_INVALID_ENUM,
5834            "%s(internalformat=%s not legal for immutable-format)",
5835            func, _mesa_enum_to_string(internalformat));
5836      return;
5837   }
5838
5839   if (!_mesa_is_renderable_texture_format(ctx, internalformat)) {
5840      /* Page 172 of OpenGL ES 3.1 spec says:
5841       *   "An INVALID_ENUM error is generated if sizedinternalformat is not
5842       *   color-renderable, depth-renderable, or stencil-renderable (as
5843       *   defined in section 9.4).
5844       *
5845       *  (Same error is also defined for desktop OpenGL for multisample
5846       *  teximage/texstorage functions.)
5847       */
5848      _mesa_error(ctx, GL_INVALID_ENUM, "%s(internalformat=%s)", func,
5849                  _mesa_enum_to_string(internalformat));
5850      return;
5851   }
5852
5853   sample_count_error = _mesa_check_sample_count(ctx, target,
5854         internalformat, samples, samples);
5855   samplesOK = sample_count_error == GL_NO_ERROR;
5856
5857   /* Page 254 of OpenGL 4.4 spec says:
5858    *   "Proxy arrays for two-dimensional multisample and two-dimensional
5859    *    multisample array textures are operated on in the same way when
5860    *    TexImage2DMultisample is called with target specified as
5861    *    PROXY_TEXTURE_2D_MULTISAMPLE, or TexImage3DMultisample is called
5862    *    with target specified as PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY.
5863    *    However, if samples is not supported, then no error is generated.
5864    */
5865   if (!samplesOK && !_mesa_is_proxy_texture(target)) {
5866      _mesa_error(ctx, sample_count_error, "%s(samples=%d)", func, samples);
5867      return;
5868   }
5869
5870   if (immutable && (!texObj || (texObj->Name == 0))) {
5871      _mesa_error(ctx, GL_INVALID_OPERATION,
5872            "%s(texture object 0)",
5873            func);
5874      return;
5875   }
5876
5877   texImage = _mesa_get_tex_image(ctx, texObj, 0, 0);
5878
5879   if (texImage == NULL) {
5880      _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s()", func);
5881      return;
5882   }
5883
5884   texFormat = _mesa_choose_texture_format(ctx, texObj, target, 0,
5885         internalformat, GL_NONE, GL_NONE);
5886   assert(texFormat != MESA_FORMAT_NONE);
5887
5888   dimensionsOK = _mesa_legal_texture_dimensions(ctx, target, 0,
5889         width, height, depth, 0);
5890
5891   sizeOK = ctx->Driver.TestProxyTexImage(ctx, target, 0, 0, texFormat,
5892                                          samples, width, height, depth);
5893
5894   if (_mesa_is_proxy_texture(target)) {
5895      if (samplesOK && dimensionsOK && sizeOK) {
5896         _mesa_init_teximage_fields_ms(ctx, texImage, width, height, depth, 0,
5897                                       internalformat, texFormat,
5898                                       samples, fixedsamplelocations);
5899      }
5900      else {
5901         /* clear all image fields */
5902         clear_teximage_fields(texImage);
5903      }
5904   }
5905   else {
5906      if (!dimensionsOK) {
5907         _mesa_error(ctx, GL_INVALID_VALUE,
5908                     "%s(invalid width=%d or height=%d)", func, width, height);
5909         return;
5910      }
5911
5912      if (!sizeOK) {
5913         _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s(texture too large)", func);
5914         return;
5915      }
5916
5917      /* Check if texObj->Immutable is set */
5918      if (texObj->Immutable) {
5919         _mesa_error(ctx, GL_INVALID_OPERATION, "%s(immutable)", func);
5920         return;
5921      }
5922
5923      ctx->Driver.FreeTextureImageBuffer(ctx, texImage);
5924
5925      _mesa_init_teximage_fields_ms(ctx, texImage, width, height, depth, 0,
5926                                    internalformat, texFormat,
5927                                    samples, fixedsamplelocations);
5928
5929      if (width > 0 && height > 0 && depth > 0) {
5930         if (memObj) {
5931            if (!ctx->Driver.SetTextureStorageForMemoryObject(ctx, texObj,
5932                                                              memObj, 1, width,
5933                                                              height, depth,
5934                                                              offset)) {
5935
5936               _mesa_init_teximage_fields(ctx, texImage, 0, 0, 0, 0,
5937                                          internalformat, texFormat);
5938            }
5939         } else {
5940            if (!ctx->Driver.AllocTextureStorage(ctx, texObj, 1,
5941                                                 width, height, depth)) {
5942               /* tidy up the texture image state. strictly speaking,
5943                * we're allowed to just leave this in whatever state we
5944                * like, but being tidy is good.
5945                */
5946               _mesa_init_teximage_fields(ctx, texImage, 0, 0, 0, 0,
5947                                          internalformat, texFormat);
5948            }
5949         }
5950      }
5951
5952      texObj->Immutable |= immutable;
5953
5954      if (immutable) {
5955         _mesa_set_texture_view_state(ctx, texObj, target, 1);
5956      }
5957
5958      _mesa_update_fbo_texture(ctx, texObj, 0, 0);
5959   }
5960}
5961
5962
5963void GLAPIENTRY
5964_mesa_TexImage2DMultisample(GLenum target, GLsizei samples,
5965                            GLenum internalformat, GLsizei width,
5966                            GLsizei height, GLboolean fixedsamplelocations)
5967{
5968   struct gl_texture_object *texObj;
5969   GET_CURRENT_CONTEXT(ctx);
5970
5971   texObj = _mesa_get_current_tex_object(ctx, target);
5972   if (!texObj)
5973      return;
5974
5975   texture_image_multisample(ctx, 2, texObj, NULL, target, samples,
5976                             internalformat, width, height, 1,
5977                             fixedsamplelocations, GL_FALSE, 0,
5978                             "glTexImage2DMultisample");
5979}
5980
5981
5982void GLAPIENTRY
5983_mesa_TexImage3DMultisample(GLenum target, GLsizei samples,
5984                            GLenum internalformat, GLsizei width,
5985                            GLsizei height, GLsizei depth,
5986                            GLboolean fixedsamplelocations)
5987{
5988   struct gl_texture_object *texObj;
5989   GET_CURRENT_CONTEXT(ctx);
5990
5991   texObj = _mesa_get_current_tex_object(ctx, target);
5992   if (!texObj)
5993      return;
5994
5995   texture_image_multisample(ctx, 3, texObj, NULL, target, samples,
5996                             internalformat, width, height, depth,
5997                             fixedsamplelocations, GL_FALSE, 0,
5998                             "glTexImage3DMultisample");
5999}
6000
6001static bool
6002valid_texstorage_ms_parameters(GLsizei width, GLsizei height, GLsizei depth,
6003                               unsigned dims)
6004{
6005   GET_CURRENT_CONTEXT(ctx);
6006
6007   if (!_mesa_valid_tex_storage_dim(width, height, depth)) {
6008      _mesa_error(ctx, GL_INVALID_VALUE,
6009                  "glTexStorage%uDMultisample(width=%d,height=%d,depth=%d)",
6010                  dims, width, height, depth);
6011      return false;
6012   }
6013   return true;
6014}
6015
6016void GLAPIENTRY
6017_mesa_TexStorage2DMultisample(GLenum target, GLsizei samples,
6018                              GLenum internalformat, GLsizei width,
6019                              GLsizei height, GLboolean fixedsamplelocations)
6020{
6021   struct gl_texture_object *texObj;
6022   GET_CURRENT_CONTEXT(ctx);
6023
6024   texObj = _mesa_get_current_tex_object(ctx, target);
6025   if (!texObj)
6026      return;
6027
6028   if (!valid_texstorage_ms_parameters(width, height, 1, 2))
6029      return;
6030
6031   texture_image_multisample(ctx, 2, texObj, NULL, target, samples,
6032                             internalformat, width, height, 1,
6033                             fixedsamplelocations, GL_TRUE, 0,
6034                             "glTexStorage2DMultisample");
6035}
6036
6037void GLAPIENTRY
6038_mesa_TexStorage3DMultisample(GLenum target, GLsizei samples,
6039                              GLenum internalformat, GLsizei width,
6040                              GLsizei height, GLsizei depth,
6041                              GLboolean fixedsamplelocations)
6042{
6043   struct gl_texture_object *texObj;
6044   GET_CURRENT_CONTEXT(ctx);
6045
6046   texObj = _mesa_get_current_tex_object(ctx, target);
6047   if (!texObj)
6048      return;
6049
6050   if (!valid_texstorage_ms_parameters(width, height, depth, 3))
6051      return;
6052
6053   texture_image_multisample(ctx, 3, texObj, NULL, target, samples,
6054                             internalformat, width, height, depth,
6055                             fixedsamplelocations, GL_TRUE, 0,
6056                             "glTexStorage3DMultisample");
6057}
6058
6059void GLAPIENTRY
6060_mesa_TextureStorage2DMultisample(GLuint texture, GLsizei samples,
6061                                  GLenum internalformat, GLsizei width,
6062                                  GLsizei height,
6063                                  GLboolean fixedsamplelocations)
6064{
6065   struct gl_texture_object *texObj;
6066   GET_CURRENT_CONTEXT(ctx);
6067
6068   texObj = _mesa_lookup_texture_err(ctx, texture,
6069                                     "glTextureStorage2DMultisample");
6070   if (!texObj)
6071      return;
6072
6073   if (!valid_texstorage_ms_parameters(width, height, 1, 2))
6074      return;
6075
6076   texture_image_multisample(ctx, 2, texObj, NULL, texObj->Target,
6077                             samples, internalformat, width, height, 1,
6078                             fixedsamplelocations, GL_TRUE, 0,
6079                             "glTextureStorage2DMultisample");
6080}
6081
6082void GLAPIENTRY
6083_mesa_TextureStorage3DMultisample(GLuint texture, GLsizei samples,
6084                                  GLenum internalformat, GLsizei width,
6085                                  GLsizei height, GLsizei depth,
6086                                  GLboolean fixedsamplelocations)
6087{
6088   struct gl_texture_object *texObj;
6089   GET_CURRENT_CONTEXT(ctx);
6090
6091   /* Get the texture object by Name. */
6092   texObj = _mesa_lookup_texture_err(ctx, texture,
6093                                     "glTextureStorage3DMultisample");
6094   if (!texObj)
6095      return;
6096
6097   if (!valid_texstorage_ms_parameters(width, height, depth, 3))
6098      return;
6099
6100   texture_image_multisample(ctx, 3, texObj, NULL, texObj->Target, samples,
6101                             internalformat, width, height, depth,
6102                             fixedsamplelocations, GL_TRUE, 0,
6103                             "glTextureStorage3DMultisample");
6104}
6105
6106void
6107_mesa_texture_storage_ms_memory(struct gl_context *ctx, GLuint dims,
6108                                struct gl_texture_object *texObj,
6109                                struct gl_memory_object *memObj,
6110                                GLenum target, GLsizei samples,
6111                                GLenum internalFormat, GLsizei width,
6112                                GLsizei height, GLsizei depth,
6113                                GLboolean fixedSampleLocations,
6114                                GLuint64 offset, const char* func)
6115{
6116   assert(memObj);
6117
6118   texture_image_multisample(ctx, dims, texObj, memObj, target, samples,
6119                             internalFormat, width, height, depth,
6120                             fixedSampleLocations, GL_TRUE, offset,
6121                             func);
6122}
6123