teximage.c revision 9b028faeaa2c4b0ca2e86892aac70a2f2b872fbb
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 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25
26/**
27 * \file teximage.c
28 * Texture image-related functions.
29 */
30
31#include <stdbool.h>
32#include "glheader.h"
33#include "bufferobj.h"
34#include "context.h"
35#include "enums.h"
36#include "fbobject.h"
37#include "framebuffer.h"
38#include "hash.h"
39#include "image.h"
40#include "imports.h"
41#include "macros.h"
42#include "mfeatures.h"
43#include "state.h"
44#include "texcompress.h"
45#include "teximage.h"
46#include "texobj.h"
47#include "texstate.h"
48#include "texpal.h"
49#include "mtypes.h"
50#include "glformats.h"
51
52
53/* Inexplicably, GL_HALF_FLOAT_OES has a different value than GL_HALF_FLOAT.
54 */
55#ifndef GL_HALF_FLOAT_OES
56#define GL_HALF_FLOAT_OES 0x8D61
57#endif
58
59/**
60 * State changes which we care about for glCopyTex[Sub]Image() calls.
61 * In particular, we care about pixel transfer state and buffer state
62 * (such as glReadBuffer to make sure we read from the right renderbuffer).
63 */
64#define NEW_COPY_TEX_STATE (_NEW_BUFFERS | _NEW_PIXEL)
65
66
67
68/**
69 * Return the simple base format for a given internal texture format.
70 * For example, given GL_LUMINANCE12_ALPHA4, return GL_LUMINANCE_ALPHA.
71 *
72 * \param ctx GL context.
73 * \param internalFormat the internal texture format token or 1, 2, 3, or 4.
74 *
75 * \return the corresponding \u base internal format (GL_ALPHA, GL_LUMINANCE,
76 * GL_LUMANCE_ALPHA, GL_INTENSITY, GL_RGB, or GL_RGBA), or -1 if invalid enum.
77 *
78 * This is the format which is used during texture application (i.e. the
79 * texture format and env mode determine the arithmetic used.
80 */
81GLint
82_mesa_base_tex_format( struct gl_context *ctx, GLint internalFormat )
83{
84   switch (internalFormat) {
85      case GL_ALPHA:
86      case GL_ALPHA4:
87      case GL_ALPHA8:
88      case GL_ALPHA12:
89      case GL_ALPHA16:
90         return GL_ALPHA;
91      case 1:
92      case GL_LUMINANCE:
93      case GL_LUMINANCE4:
94      case GL_LUMINANCE8:
95      case GL_LUMINANCE12:
96      case GL_LUMINANCE16:
97         return GL_LUMINANCE;
98      case 2:
99      case GL_LUMINANCE_ALPHA:
100      case GL_LUMINANCE4_ALPHA4:
101      case GL_LUMINANCE6_ALPHA2:
102      case GL_LUMINANCE8_ALPHA8:
103      case GL_LUMINANCE12_ALPHA4:
104      case GL_LUMINANCE12_ALPHA12:
105      case GL_LUMINANCE16_ALPHA16:
106         return GL_LUMINANCE_ALPHA;
107      case GL_INTENSITY:
108      case GL_INTENSITY4:
109      case GL_INTENSITY8:
110      case GL_INTENSITY12:
111      case GL_INTENSITY16:
112         return GL_INTENSITY;
113      case 3:
114      case GL_RGB:
115      case GL_R3_G3_B2:
116      case GL_RGB4:
117      case GL_RGB5:
118      case GL_RGB8:
119      case GL_RGB10:
120      case GL_RGB12:
121      case GL_RGB16:
122         return GL_RGB;
123      case 4:
124      case GL_RGBA:
125      case GL_RGBA2:
126      case GL_RGBA4:
127      case GL_RGB5_A1:
128      case GL_RGBA8:
129      case GL_RGB10_A2:
130      case GL_RGBA12:
131      case GL_RGBA16:
132         return GL_RGBA;
133      default:
134         ; /* fallthrough */
135   }
136
137   /* GL_BGRA can be an internal format *only* in OpenGL ES (1.x or 2.0).
138    */
139   if (_mesa_is_gles(ctx)) {
140      switch (internalFormat) {
141         case GL_BGRA:
142            return GL_RGBA;
143         default:
144            ; /* fallthrough */
145      }
146   }
147
148   if (ctx->Extensions.ARB_ES2_compatibility) {
149      switch (internalFormat) {
150         case GL_RGB565:
151            return GL_RGB;
152         default:
153            ; /* fallthrough */
154      }
155   }
156
157   if (ctx->Extensions.ARB_depth_texture) {
158      switch (internalFormat) {
159         case GL_DEPTH_COMPONENT:
160         case GL_DEPTH_COMPONENT16:
161         case GL_DEPTH_COMPONENT24:
162         case GL_DEPTH_COMPONENT32:
163            return GL_DEPTH_COMPONENT;
164         default:
165            ; /* fallthrough */
166      }
167   }
168
169   switch (internalFormat) {
170   case GL_COMPRESSED_ALPHA:
171      return GL_ALPHA;
172   case GL_COMPRESSED_LUMINANCE:
173      return GL_LUMINANCE;
174   case GL_COMPRESSED_LUMINANCE_ALPHA:
175      return GL_LUMINANCE_ALPHA;
176   case GL_COMPRESSED_INTENSITY:
177      return GL_INTENSITY;
178   case GL_COMPRESSED_RGB:
179      return GL_RGB;
180   case GL_COMPRESSED_RGBA:
181      return GL_RGBA;
182   default:
183      ; /* fallthrough */
184   }
185
186   if (ctx->Extensions.TDFX_texture_compression_FXT1) {
187      switch (internalFormat) {
188         case GL_COMPRESSED_RGB_FXT1_3DFX:
189            return GL_RGB;
190         case GL_COMPRESSED_RGBA_FXT1_3DFX:
191            return GL_RGBA;
192         default:
193            ; /* fallthrough */
194      }
195   }
196
197   if (ctx->Extensions.EXT_texture_compression_s3tc) {
198      switch (internalFormat) {
199         case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
200            return GL_RGB;
201         case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
202         case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
203         case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
204            return GL_RGBA;
205         default:
206            ; /* fallthrough */
207      }
208   }
209
210   if (ctx->Extensions.S3_s3tc) {
211      switch (internalFormat) {
212         case GL_RGB_S3TC:
213         case GL_RGB4_S3TC:
214            return GL_RGB;
215         case GL_RGBA_S3TC:
216         case GL_RGBA4_S3TC:
217            return GL_RGBA;
218         default:
219            ; /* fallthrough */
220      }
221   }
222
223   if (ctx->Extensions.MESA_ycbcr_texture) {
224      if (internalFormat == GL_YCBCR_MESA)
225         return GL_YCBCR_MESA;
226   }
227
228   if (ctx->Extensions.ARB_texture_float) {
229      switch (internalFormat) {
230         case GL_ALPHA16F_ARB:
231         case GL_ALPHA32F_ARB:
232            return GL_ALPHA;
233         case GL_RGBA16F_ARB:
234         case GL_RGBA32F_ARB:
235            return GL_RGBA;
236         case GL_RGB16F_ARB:
237         case GL_RGB32F_ARB:
238            return GL_RGB;
239         case GL_INTENSITY16F_ARB:
240         case GL_INTENSITY32F_ARB:
241            return GL_INTENSITY;
242         case GL_LUMINANCE16F_ARB:
243         case GL_LUMINANCE32F_ARB:
244            return GL_LUMINANCE;
245         case GL_LUMINANCE_ALPHA16F_ARB:
246         case GL_LUMINANCE_ALPHA32F_ARB:
247            return GL_LUMINANCE_ALPHA;
248         default:
249            ; /* fallthrough */
250      }
251   }
252
253   if (ctx->Extensions.ATI_envmap_bumpmap) {
254      switch (internalFormat) {
255         case GL_DUDV_ATI:
256         case GL_DU8DV8_ATI:
257            return GL_DUDV_ATI;
258         default:
259            ; /* fallthrough */
260      }
261   }
262
263   if (ctx->Extensions.EXT_texture_snorm) {
264      switch (internalFormat) {
265         case GL_RED_SNORM:
266         case GL_R8_SNORM:
267         case GL_R16_SNORM:
268            return GL_RED;
269         case GL_RG_SNORM:
270         case GL_RG8_SNORM:
271         case GL_RG16_SNORM:
272            return GL_RG;
273         case GL_RGB_SNORM:
274         case GL_RGB8_SNORM:
275         case GL_RGB16_SNORM:
276            return GL_RGB;
277         case GL_RGBA_SNORM:
278         case GL_RGBA8_SNORM:
279         case GL_RGBA16_SNORM:
280            return GL_RGBA;
281         case GL_ALPHA_SNORM:
282         case GL_ALPHA8_SNORM:
283         case GL_ALPHA16_SNORM:
284            return GL_ALPHA;
285         case GL_LUMINANCE_SNORM:
286         case GL_LUMINANCE8_SNORM:
287         case GL_LUMINANCE16_SNORM:
288            return GL_LUMINANCE;
289         case GL_LUMINANCE_ALPHA_SNORM:
290         case GL_LUMINANCE8_ALPHA8_SNORM:
291         case GL_LUMINANCE16_ALPHA16_SNORM:
292            return GL_LUMINANCE_ALPHA;
293         case GL_INTENSITY_SNORM:
294         case GL_INTENSITY8_SNORM:
295         case GL_INTENSITY16_SNORM:
296            return GL_INTENSITY;
297         default:
298            ; /* fallthrough */
299      }
300   }
301
302   if (ctx->Extensions.EXT_packed_depth_stencil) {
303      switch (internalFormat) {
304         case GL_DEPTH_STENCIL_EXT:
305         case GL_DEPTH24_STENCIL8_EXT:
306            return GL_DEPTH_STENCIL_EXT;
307         default:
308            ; /* fallthrough */
309      }
310   }
311
312#if FEATURE_EXT_texture_sRGB
313   if (ctx->Extensions.EXT_texture_sRGB) {
314      switch (internalFormat) {
315      case GL_SRGB_EXT:
316      case GL_SRGB8_EXT:
317      case GL_COMPRESSED_SRGB_EXT:
318      case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT:
319         return GL_RGB;
320      case GL_SRGB_ALPHA_EXT:
321      case GL_SRGB8_ALPHA8_EXT:
322      case GL_COMPRESSED_SRGB_ALPHA_EXT:
323      case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT:
324      case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT:
325      case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT:
326         return GL_RGBA;
327      case GL_SLUMINANCE_ALPHA_EXT:
328      case GL_SLUMINANCE8_ALPHA8_EXT:
329      case GL_COMPRESSED_SLUMINANCE_ALPHA_EXT:
330         return GL_LUMINANCE_ALPHA;
331      case GL_SLUMINANCE_EXT:
332      case GL_SLUMINANCE8_EXT:
333      case GL_COMPRESSED_SLUMINANCE_EXT:
334         return GL_LUMINANCE;
335      default:
336         ; /* fallthrough */
337      }
338   }
339#endif /* FEATURE_EXT_texture_sRGB */
340
341   if (ctx->Version >= 30 ||
342       ctx->Extensions.EXT_texture_integer) {
343      switch (internalFormat) {
344      case GL_RGBA8UI_EXT:
345      case GL_RGBA16UI_EXT:
346      case GL_RGBA32UI_EXT:
347      case GL_RGBA8I_EXT:
348      case GL_RGBA16I_EXT:
349      case GL_RGBA32I_EXT:
350      case GL_RGB10_A2UI:
351         return GL_RGBA;
352      case GL_RGB8UI_EXT:
353      case GL_RGB16UI_EXT:
354      case GL_RGB32UI_EXT:
355      case GL_RGB8I_EXT:
356      case GL_RGB16I_EXT:
357      case GL_RGB32I_EXT:
358         return GL_RGB;
359      }
360   }
361
362   if (ctx->Extensions.EXT_texture_integer) {
363      switch (internalFormat) {
364      case GL_ALPHA8UI_EXT:
365      case GL_ALPHA16UI_EXT:
366      case GL_ALPHA32UI_EXT:
367      case GL_ALPHA8I_EXT:
368      case GL_ALPHA16I_EXT:
369      case GL_ALPHA32I_EXT:
370         return GL_ALPHA;
371      case GL_INTENSITY8UI_EXT:
372      case GL_INTENSITY16UI_EXT:
373      case GL_INTENSITY32UI_EXT:
374      case GL_INTENSITY8I_EXT:
375      case GL_INTENSITY16I_EXT:
376      case GL_INTENSITY32I_EXT:
377         return GL_INTENSITY;
378      case GL_LUMINANCE8UI_EXT:
379      case GL_LUMINANCE16UI_EXT:
380      case GL_LUMINANCE32UI_EXT:
381      case GL_LUMINANCE8I_EXT:
382      case GL_LUMINANCE16I_EXT:
383      case GL_LUMINANCE32I_EXT:
384         return GL_LUMINANCE;
385      case GL_LUMINANCE_ALPHA8UI_EXT:
386      case GL_LUMINANCE_ALPHA16UI_EXT:
387      case GL_LUMINANCE_ALPHA32UI_EXT:
388      case GL_LUMINANCE_ALPHA8I_EXT:
389      case GL_LUMINANCE_ALPHA16I_EXT:
390      case GL_LUMINANCE_ALPHA32I_EXT:
391         return GL_LUMINANCE_ALPHA;
392      default:
393         ; /* fallthrough */
394      }
395   }
396
397   if (ctx->Extensions.ARB_texture_rg) {
398      switch (internalFormat) {
399      case GL_R16F:
400	 /* R16F depends on both ARB_half_float_pixel and ARB_texture_float.
401	  */
402	 if (!ctx->Extensions.ARB_half_float_pixel)
403	    break;
404	 /* FALLTHROUGH */
405      case GL_R32F:
406	 if (!ctx->Extensions.ARB_texture_float)
407	    break;
408         return GL_RED;
409      case GL_R8I:
410      case GL_R8UI:
411      case GL_R16I:
412      case GL_R16UI:
413      case GL_R32I:
414      case GL_R32UI:
415	 if (ctx->Version < 30 && !ctx->Extensions.EXT_texture_integer)
416	    break;
417	 /* FALLTHROUGH */
418      case GL_R8:
419      case GL_R16:
420      case GL_RED:
421      case GL_COMPRESSED_RED:
422         return GL_RED;
423
424      case GL_RG16F:
425	 /* RG16F depends on both ARB_half_float_pixel and ARB_texture_float.
426	  */
427	 if (!ctx->Extensions.ARB_half_float_pixel)
428	    break;
429	 /* FALLTHROUGH */
430      case GL_RG32F:
431	 if (!ctx->Extensions.ARB_texture_float)
432	    break;
433         return GL_RG;
434      case GL_RG8I:
435      case GL_RG8UI:
436      case GL_RG16I:
437      case GL_RG16UI:
438      case GL_RG32I:
439      case GL_RG32UI:
440	 if (ctx->Version < 30 && !ctx->Extensions.EXT_texture_integer)
441	    break;
442	 /* FALLTHROUGH */
443      case GL_RG:
444      case GL_RG8:
445      case GL_RG16:
446      case GL_COMPRESSED_RG:
447         return GL_RG;
448      default:
449         ; /* fallthrough */
450      }
451   }
452
453   if (ctx->Extensions.EXT_texture_shared_exponent) {
454      switch (internalFormat) {
455      case GL_RGB9_E5_EXT:
456         return GL_RGB;
457      default:
458         ; /* fallthrough */
459      }
460   }
461
462   if (ctx->Extensions.EXT_packed_float) {
463      switch (internalFormat) {
464      case GL_R11F_G11F_B10F_EXT:
465         return GL_RGB;
466      default:
467         ; /* fallthrough */
468      }
469   }
470
471   if (ctx->Extensions.ARB_depth_buffer_float) {
472      switch (internalFormat) {
473      case GL_DEPTH_COMPONENT32F:
474         return GL_DEPTH_COMPONENT;
475      case GL_DEPTH32F_STENCIL8:
476         return GL_DEPTH_STENCIL;
477      default:
478         ; /* fallthrough */
479      }
480   }
481
482   if (ctx->Extensions.ARB_texture_compression_rgtc) {
483      switch (internalFormat) {
484      case GL_COMPRESSED_RED_RGTC1:
485      case GL_COMPRESSED_SIGNED_RED_RGTC1:
486         return GL_RED;
487      case GL_COMPRESSED_RG_RGTC2:
488      case GL_COMPRESSED_SIGNED_RG_RGTC2:
489         return GL_RG;
490      default:
491         ; /* fallthrough */
492      }
493   }
494
495   if (ctx->Extensions.EXT_texture_compression_latc) {
496      switch (internalFormat) {
497      case GL_COMPRESSED_LUMINANCE_LATC1_EXT:
498      case GL_COMPRESSED_SIGNED_LUMINANCE_LATC1_EXT:
499         return GL_LUMINANCE;
500      case GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT:
501      case GL_COMPRESSED_SIGNED_LUMINANCE_ALPHA_LATC2_EXT:
502         return GL_LUMINANCE_ALPHA;
503      default:
504         ; /* fallthrough */
505      }
506   }
507
508   if (ctx->Extensions.ATI_texture_compression_3dc) {
509      switch (internalFormat) {
510      case GL_COMPRESSED_LUMINANCE_ALPHA_3DC_ATI:
511         return GL_LUMINANCE_ALPHA;
512      default:
513         ; /* fallthrough */
514      }
515   }
516
517   if (ctx->Extensions.OES_compressed_ETC1_RGB8_texture) {
518      switch (internalFormat) {
519      case GL_ETC1_RGB8_OES:
520         return GL_RGB;
521      default:
522         ; /* fallthrough */
523      }
524   }
525
526   if (ctx->API == API_OPENGLES) {
527      switch (internalFormat) {
528      case GL_PALETTE4_RGB8_OES:
529      case GL_PALETTE4_R5_G6_B5_OES:
530      case GL_PALETTE8_RGB8_OES:
531      case GL_PALETTE8_R5_G6_B5_OES:
532	 return GL_RGB;
533      case GL_PALETTE4_RGBA8_OES:
534      case GL_PALETTE8_RGB5_A1_OES:
535      case GL_PALETTE4_RGBA4_OES:
536      case GL_PALETTE4_RGB5_A1_OES:
537      case GL_PALETTE8_RGBA8_OES:
538      case GL_PALETTE8_RGBA4_OES:
539	 return GL_RGBA;
540      default:
541         ; /* fallthrough */
542      }
543   }
544
545   return -1; /* error */
546}
547
548
549/**
550 * For cube map faces, return a face index in [0,5].
551 * For other targets return 0;
552 */
553GLuint
554_mesa_tex_target_to_face(GLenum target)
555{
556   if (_mesa_is_cube_face(target))
557      return (GLuint) target - (GLuint) GL_TEXTURE_CUBE_MAP_POSITIVE_X;
558   else
559      return 0;
560}
561
562
563
564/**
565 * Install gl_texture_image in a gl_texture_object according to the target
566 * and level parameters.
567 *
568 * \param tObj texture object.
569 * \param target texture target.
570 * \param level image level.
571 * \param texImage texture image.
572 */
573static void
574set_tex_image(struct gl_texture_object *tObj,
575              GLenum target, GLint level,
576              struct gl_texture_image *texImage)
577{
578   const GLuint face = _mesa_tex_target_to_face(target);
579
580   ASSERT(tObj);
581   ASSERT(texImage);
582   if (target == GL_TEXTURE_RECTANGLE_NV || target == GL_TEXTURE_EXTERNAL_OES)
583      assert(level == 0);
584
585   tObj->Image[face][level] = texImage;
586
587   /* Set the 'back' pointer */
588   texImage->TexObject = tObj;
589   texImage->Level = level;
590   texImage->Face = face;
591}
592
593
594/**
595 * Allocate a texture image structure.
596 *
597 * Called via ctx->Driver.NewTextureImage() unless overriden by a device
598 * driver.
599 *
600 * \return a pointer to gl_texture_image struct with all fields initialized to
601 * zero.
602 */
603struct gl_texture_image *
604_mesa_new_texture_image( struct gl_context *ctx )
605{
606   (void) ctx;
607   return CALLOC_STRUCT(gl_texture_image);
608}
609
610
611/**
612 * Free a gl_texture_image and associated data.
613 * This function is a fallback called via ctx->Driver.DeleteTextureImage().
614 *
615 * \param texImage texture image.
616 *
617 * Free the texture image structure and the associated image data.
618 */
619void
620_mesa_delete_texture_image(struct gl_context *ctx,
621                           struct gl_texture_image *texImage)
622{
623   /* Free texImage->Data and/or any other driver-specific texture
624    * image storage.
625    */
626   ASSERT(ctx->Driver.FreeTextureImageBuffer);
627   ctx->Driver.FreeTextureImageBuffer( ctx, texImage );
628   free(texImage);
629}
630
631
632/**
633 * Test if a target is a proxy target.
634 *
635 * \param target texture target.
636 *
637 * \return GL_TRUE if the target is a proxy target, GL_FALSE otherwise.
638 */
639GLboolean
640_mesa_is_proxy_texture(GLenum target)
641{
642   /*
643    * NUM_TEXTURE_TARGETS should match number of terms below, except there's no
644    * proxy for GL_TEXTURE_BUFFER and GL_TEXTURE_EXTERNAL_OES.
645    */
646   assert(NUM_TEXTURE_TARGETS == 7 + 2);
647
648   return (target == GL_PROXY_TEXTURE_1D ||
649           target == GL_PROXY_TEXTURE_2D ||
650           target == GL_PROXY_TEXTURE_3D ||
651           target == GL_PROXY_TEXTURE_CUBE_MAP_ARB ||
652           target == GL_PROXY_TEXTURE_RECTANGLE_NV ||
653           target == GL_PROXY_TEXTURE_1D_ARRAY_EXT ||
654           target == GL_PROXY_TEXTURE_2D_ARRAY_EXT);
655}
656
657
658/**
659 * Return the proxy target which corresponds to the given texture target
660 */
661static GLenum
662get_proxy_target(GLenum target)
663{
664   switch (target) {
665   case GL_TEXTURE_1D:
666   case GL_PROXY_TEXTURE_1D:
667      return GL_PROXY_TEXTURE_1D;
668   case GL_TEXTURE_2D:
669   case GL_PROXY_TEXTURE_2D:
670      return GL_PROXY_TEXTURE_2D;
671   case GL_TEXTURE_3D:
672   case GL_PROXY_TEXTURE_3D:
673      return GL_PROXY_TEXTURE_3D;
674   case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
675   case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
676   case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
677   case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
678   case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
679   case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
680   case GL_TEXTURE_CUBE_MAP_ARB:
681   case GL_PROXY_TEXTURE_CUBE_MAP_ARB:
682      return GL_PROXY_TEXTURE_CUBE_MAP_ARB;
683   case GL_TEXTURE_RECTANGLE_NV:
684   case GL_PROXY_TEXTURE_RECTANGLE_NV:
685      return GL_PROXY_TEXTURE_RECTANGLE_NV;
686   case GL_TEXTURE_1D_ARRAY_EXT:
687   case GL_PROXY_TEXTURE_1D_ARRAY_EXT:
688      return GL_PROXY_TEXTURE_1D_ARRAY_EXT;
689   case GL_TEXTURE_2D_ARRAY_EXT:
690   case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
691      return GL_PROXY_TEXTURE_2D_ARRAY_EXT;
692   default:
693      _mesa_problem(NULL, "unexpected target in get_proxy_target()");
694      return 0;
695   }
696}
697
698
699/**
700 * Get the texture object that corresponds to the target of the given
701 * texture unit.  The target should have already been checked for validity.
702 *
703 * \param ctx GL context.
704 * \param texUnit texture unit.
705 * \param target texture target.
706 *
707 * \return pointer to the texture object on success, or NULL on failure.
708 */
709struct gl_texture_object *
710_mesa_select_tex_object(struct gl_context *ctx,
711                        const struct gl_texture_unit *texUnit,
712                        GLenum target)
713{
714   const GLboolean arrayTex = (ctx->Extensions.MESA_texture_array ||
715                               ctx->Extensions.EXT_texture_array);
716
717   switch (target) {
718      case GL_TEXTURE_1D:
719         return texUnit->CurrentTex[TEXTURE_1D_INDEX];
720      case GL_PROXY_TEXTURE_1D:
721         return ctx->Texture.ProxyTex[TEXTURE_1D_INDEX];
722      case GL_TEXTURE_2D:
723         return texUnit->CurrentTex[TEXTURE_2D_INDEX];
724      case GL_PROXY_TEXTURE_2D:
725         return ctx->Texture.ProxyTex[TEXTURE_2D_INDEX];
726      case GL_TEXTURE_3D:
727         return texUnit->CurrentTex[TEXTURE_3D_INDEX];
728      case GL_PROXY_TEXTURE_3D:
729         return ctx->Texture.ProxyTex[TEXTURE_3D_INDEX];
730      case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
731      case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
732      case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
733      case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
734      case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
735      case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
736      case GL_TEXTURE_CUBE_MAP_ARB:
737         return ctx->Extensions.ARB_texture_cube_map
738                ? texUnit->CurrentTex[TEXTURE_CUBE_INDEX] : NULL;
739      case GL_PROXY_TEXTURE_CUBE_MAP_ARB:
740         return ctx->Extensions.ARB_texture_cube_map
741                ? ctx->Texture.ProxyTex[TEXTURE_CUBE_INDEX] : NULL;
742      case GL_TEXTURE_RECTANGLE_NV:
743         return ctx->Extensions.NV_texture_rectangle
744                ? texUnit->CurrentTex[TEXTURE_RECT_INDEX] : NULL;
745      case GL_PROXY_TEXTURE_RECTANGLE_NV:
746         return ctx->Extensions.NV_texture_rectangle
747                ? ctx->Texture.ProxyTex[TEXTURE_RECT_INDEX] : NULL;
748      case GL_TEXTURE_1D_ARRAY_EXT:
749         return arrayTex ? texUnit->CurrentTex[TEXTURE_1D_ARRAY_INDEX] : NULL;
750      case GL_PROXY_TEXTURE_1D_ARRAY_EXT:
751         return arrayTex ? ctx->Texture.ProxyTex[TEXTURE_1D_ARRAY_INDEX] : NULL;
752      case GL_TEXTURE_2D_ARRAY_EXT:
753         return arrayTex ? texUnit->CurrentTex[TEXTURE_2D_ARRAY_INDEX] : NULL;
754      case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
755         return arrayTex ? ctx->Texture.ProxyTex[TEXTURE_2D_ARRAY_INDEX] : NULL;
756      case GL_TEXTURE_BUFFER:
757         return ctx->Extensions.ARB_texture_buffer_object
758            ? texUnit->CurrentTex[TEXTURE_BUFFER_INDEX] : NULL;
759      case GL_TEXTURE_EXTERNAL_OES:
760         return ctx->Extensions.OES_EGL_image_external
761            ? texUnit->CurrentTex[TEXTURE_EXTERNAL_INDEX] : NULL;
762      default:
763         _mesa_problem(NULL, "bad target in _mesa_select_tex_object()");
764         return NULL;
765   }
766}
767
768
769/**
770 * Return pointer to texture object for given target on current texture unit.
771 */
772struct gl_texture_object *
773_mesa_get_current_tex_object(struct gl_context *ctx, GLenum target)
774{
775   struct gl_texture_unit *texUnit = _mesa_get_current_tex_unit(ctx);
776   return _mesa_select_tex_object(ctx, texUnit, target);
777}
778
779
780/**
781 * Get a texture image pointer from a texture object, given a texture
782 * target and mipmap level.  The target and level parameters should
783 * have already been error-checked.
784 *
785 * \param ctx GL context.
786 * \param texObj texture unit.
787 * \param target texture target.
788 * \param level image level.
789 *
790 * \return pointer to the texture image structure, or NULL on failure.
791 */
792struct gl_texture_image *
793_mesa_select_tex_image(struct gl_context *ctx,
794                       const struct gl_texture_object *texObj,
795		       GLenum target, GLint level)
796{
797   const GLuint face = _mesa_tex_target_to_face(target);
798
799   ASSERT(texObj);
800   ASSERT(level >= 0);
801   ASSERT(level < MAX_TEXTURE_LEVELS);
802
803   return texObj->Image[face][level];
804}
805
806
807/**
808 * Like _mesa_select_tex_image() but if the image doesn't exist, allocate
809 * it and install it.  Only return NULL if passed a bad parameter or run
810 * out of memory.
811 */
812struct gl_texture_image *
813_mesa_get_tex_image(struct gl_context *ctx, struct gl_texture_object *texObj,
814                    GLenum target, GLint level)
815{
816   struct gl_texture_image *texImage;
817
818   if (!texObj)
819      return NULL;
820
821   texImage = _mesa_select_tex_image(ctx, texObj, target, level);
822   if (!texImage) {
823      texImage = ctx->Driver.NewTextureImage(ctx);
824      if (!texImage) {
825         _mesa_error(ctx, GL_OUT_OF_MEMORY, "texture image allocation");
826         return NULL;
827      }
828
829      set_tex_image(texObj, target, level, texImage);
830   }
831
832   return texImage;
833}
834
835
836/**
837 * Return pointer to the specified proxy texture image.
838 * Note that proxy textures are per-context, not per-texture unit.
839 * \return pointer to texture image or NULL if invalid target, invalid
840 *         level, or out of memory.
841 */
842struct gl_texture_image *
843_mesa_get_proxy_tex_image(struct gl_context *ctx, GLenum target, GLint level)
844{
845   struct gl_texture_image *texImage;
846   GLuint texIndex;
847
848   if (level < 0)
849      return NULL;
850
851   switch (target) {
852   case GL_PROXY_TEXTURE_1D:
853      if (level >= ctx->Const.MaxTextureLevels)
854         return NULL;
855      texIndex = TEXTURE_1D_INDEX;
856      break;
857   case GL_PROXY_TEXTURE_2D:
858      if (level >= ctx->Const.MaxTextureLevels)
859         return NULL;
860      texIndex = TEXTURE_2D_INDEX;
861      break;
862   case GL_PROXY_TEXTURE_3D:
863      if (level >= ctx->Const.Max3DTextureLevels)
864         return NULL;
865      texIndex = TEXTURE_3D_INDEX;
866      break;
867   case GL_PROXY_TEXTURE_CUBE_MAP:
868      if (level >= ctx->Const.MaxCubeTextureLevels)
869         return NULL;
870      texIndex = TEXTURE_CUBE_INDEX;
871      break;
872   case GL_PROXY_TEXTURE_RECTANGLE_NV:
873      if (level > 0)
874         return NULL;
875      texIndex = TEXTURE_RECT_INDEX;
876      break;
877   case GL_PROXY_TEXTURE_1D_ARRAY_EXT:
878      if (level >= ctx->Const.MaxTextureLevels)
879         return NULL;
880      texIndex = TEXTURE_1D_ARRAY_INDEX;
881      break;
882   case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
883      if (level >= ctx->Const.MaxTextureLevels)
884         return NULL;
885      texIndex = TEXTURE_2D_ARRAY_INDEX;
886      break;
887   default:
888      return NULL;
889   }
890
891   texImage = ctx->Texture.ProxyTex[texIndex]->Image[0][level];
892   if (!texImage) {
893      texImage = ctx->Driver.NewTextureImage(ctx);
894      if (!texImage) {
895         _mesa_error(ctx, GL_OUT_OF_MEMORY, "proxy texture allocation");
896         return NULL;
897      }
898      ctx->Texture.ProxyTex[texIndex]->Image[0][level] = texImage;
899      /* Set the 'back' pointer */
900      texImage->TexObject = ctx->Texture.ProxyTex[texIndex];
901   }
902   return texImage;
903}
904
905
906/**
907 * Get the maximum number of allowed mipmap levels.
908 *
909 * \param ctx GL context.
910 * \param target texture target.
911 *
912 * \return the maximum number of allowed mipmap levels for the given
913 * texture target, or zero if passed a bad target.
914 *
915 * \sa gl_constants.
916 */
917GLint
918_mesa_max_texture_levels(struct gl_context *ctx, GLenum target)
919{
920   switch (target) {
921   case GL_TEXTURE_1D:
922   case GL_PROXY_TEXTURE_1D:
923   case GL_TEXTURE_2D:
924   case GL_PROXY_TEXTURE_2D:
925      return ctx->Const.MaxTextureLevels;
926   case GL_TEXTURE_3D:
927   case GL_PROXY_TEXTURE_3D:
928      return ctx->Const.Max3DTextureLevels;
929   case GL_TEXTURE_CUBE_MAP:
930   case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
931   case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
932   case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
933   case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
934   case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
935   case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
936   case GL_PROXY_TEXTURE_CUBE_MAP_ARB:
937      return ctx->Extensions.ARB_texture_cube_map
938         ? ctx->Const.MaxCubeTextureLevels : 0;
939   case GL_TEXTURE_RECTANGLE_NV:
940   case GL_PROXY_TEXTURE_RECTANGLE_NV:
941      return ctx->Extensions.NV_texture_rectangle ? 1 : 0;
942   case GL_TEXTURE_1D_ARRAY_EXT:
943   case GL_PROXY_TEXTURE_1D_ARRAY_EXT:
944   case GL_TEXTURE_2D_ARRAY_EXT:
945   case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
946      return (ctx->Extensions.MESA_texture_array ||
947              ctx->Extensions.EXT_texture_array)
948         ? ctx->Const.MaxTextureLevels : 0;
949   case GL_TEXTURE_BUFFER:
950      return _mesa_is_desktop_gl(ctx) &&
951         (ctx->Extensions.ARB_texture_buffer_object ||
952          (ctx->Version >= 31)) ? 1 : 0;
953   case GL_TEXTURE_EXTERNAL_OES:
954      /* fall-through */
955   default:
956      return 0; /* bad target */
957   }
958}
959
960
961/**
962 * Return number of dimensions per mipmap level for the given texture target.
963 */
964GLint
965_mesa_get_texture_dimensions(GLenum target)
966{
967   switch (target) {
968   case GL_TEXTURE_1D:
969   case GL_PROXY_TEXTURE_1D:
970      return 1;
971   case GL_TEXTURE_2D:
972   case GL_TEXTURE_RECTANGLE:
973   case GL_TEXTURE_CUBE_MAP:
974   case GL_PROXY_TEXTURE_2D:
975   case GL_PROXY_TEXTURE_RECTANGLE:
976   case GL_PROXY_TEXTURE_CUBE_MAP:
977   case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
978   case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
979   case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
980   case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
981   case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
982   case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
983   case GL_TEXTURE_1D_ARRAY:
984   case GL_PROXY_TEXTURE_1D_ARRAY:
985   case GL_TEXTURE_EXTERNAL_OES:
986      return 2;
987   case GL_TEXTURE_3D:
988   case GL_PROXY_TEXTURE_3D:
989   case GL_TEXTURE_2D_ARRAY:
990   case GL_PROXY_TEXTURE_2D_ARRAY:
991      return 3;
992   case GL_TEXTURE_BUFFER:
993      /* fall-through */
994   default:
995      _mesa_problem(NULL, "invalid target 0x%x in get_texture_dimensions()",
996                    target);
997      return 2;
998   }
999}
1000
1001
1002
1003
1004#if 000 /* not used anymore */
1005/*
1006 * glTexImage[123]D can accept a NULL image pointer.  In this case we
1007 * create a texture image with unspecified image contents per the OpenGL
1008 * spec.
1009 */
1010static GLubyte *
1011make_null_texture(GLint width, GLint height, GLint depth, GLenum format)
1012{
1013   const GLint components = _mesa_components_in_format(format);
1014   const GLint numPixels = width * height * depth;
1015   GLubyte *data = (GLubyte *) MALLOC(numPixels * components * sizeof(GLubyte));
1016
1017#ifdef DEBUG
1018   /*
1019    * Let's see if anyone finds this.  If glTexImage2D() is called with
1020    * a NULL image pointer then load the texture image with something
1021    * interesting instead of leaving it indeterminate.
1022    */
1023   if (data) {
1024      static const char message[8][32] = {
1025         "   X   X  XXXXX   XXX     X    ",
1026         "   XX XX  X      X   X   X X   ",
1027         "   X X X  X      X      X   X  ",
1028         "   X   X  XXXX    XXX   XXXXX  ",
1029         "   X   X  X          X  X   X  ",
1030         "   X   X  X      X   X  X   X  ",
1031         "   X   X  XXXXX   XXX   X   X  ",
1032         "                               "
1033      };
1034
1035      GLubyte *imgPtr = data;
1036      GLint h, i, j, k;
1037      for (h = 0; h < depth; h++) {
1038         for (i = 0; i < height; i++) {
1039            GLint srcRow = 7 - (i % 8);
1040            for (j = 0; j < width; j++) {
1041               GLint srcCol = j % 32;
1042               GLubyte texel = (message[srcRow][srcCol]=='X') ? 255 : 70;
1043               for (k = 0; k < components; k++) {
1044                  *imgPtr++ = texel;
1045               }
1046            }
1047         }
1048      }
1049   }
1050#endif
1051
1052   return data;
1053}
1054#endif
1055
1056
1057
1058/**
1059 * Set the size and format-related fields of a gl_texture_image struct
1060 * to zero.  This is used when a proxy texture test fails.
1061 */
1062static void
1063clear_teximage_fields(struct gl_texture_image *img)
1064{
1065   ASSERT(img);
1066   img->_BaseFormat = 0;
1067   img->InternalFormat = 0;
1068   img->Border = 0;
1069   img->Width = 0;
1070   img->Height = 0;
1071   img->Depth = 0;
1072   img->Width2 = 0;
1073   img->Height2 = 0;
1074   img->Depth2 = 0;
1075   img->WidthLog2 = 0;
1076   img->HeightLog2 = 0;
1077   img->DepthLog2 = 0;
1078   img->TexFormat = MESA_FORMAT_NONE;
1079}
1080
1081
1082/**
1083 * Initialize basic fields of the gl_texture_image struct.
1084 *
1085 * \param ctx GL context.
1086 * \param img texture image structure to be initialized.
1087 * \param width image width.
1088 * \param height image height.
1089 * \param depth image depth.
1090 * \param border image border.
1091 * \param internalFormat internal format.
1092 * \param format  the actual hardware format (one of MESA_FORMAT_*)
1093 *
1094 * Fills in the fields of \p img with the given information.
1095 * Note: width, height and depth include the border.
1096 */
1097void
1098_mesa_init_teximage_fields(struct gl_context *ctx,
1099                           struct gl_texture_image *img,
1100                           GLsizei width, GLsizei height, GLsizei depth,
1101                           GLint border, GLenum internalFormat,
1102                           gl_format format)
1103{
1104   GLenum target;
1105   ASSERT(img);
1106   ASSERT(width >= 0);
1107   ASSERT(height >= 0);
1108   ASSERT(depth >= 0);
1109
1110   target = img->TexObject->Target;
1111   img->_BaseFormat = _mesa_base_tex_format( ctx, internalFormat );
1112   ASSERT(img->_BaseFormat > 0);
1113   img->InternalFormat = internalFormat;
1114   img->Border = border;
1115   img->Width = width;
1116   img->Height = height;
1117   img->Depth = depth;
1118
1119   img->Width2 = width - 2 * border;   /* == 1 << img->WidthLog2; */
1120   img->WidthLog2 = _mesa_logbase2(img->Width2);
1121
1122   switch(target) {
1123   case GL_TEXTURE_1D:
1124   case GL_TEXTURE_BUFFER:
1125   case GL_PROXY_TEXTURE_1D:
1126      if (height == 0)
1127         img->Height2 = 0;
1128      else
1129         img->Height2 = 1;
1130      img->HeightLog2 = 0;
1131      if (depth == 0)
1132         img->Depth2 = 0;
1133      else
1134         img->Depth2 = 1;
1135      img->DepthLog2 = 0;
1136      break;
1137   case GL_TEXTURE_1D_ARRAY:
1138   case GL_PROXY_TEXTURE_1D_ARRAY:
1139      img->Height2 = height; /* no border */
1140      img->HeightLog2 = 0; /* not used */
1141      if (depth == 0)
1142         img->Depth2 = 0;
1143      else
1144         img->Depth2 = 1;
1145      img->DepthLog2 = 0;
1146      break;
1147   case GL_TEXTURE_2D:
1148   case GL_TEXTURE_RECTANGLE:
1149   case GL_TEXTURE_CUBE_MAP:
1150   case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
1151   case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
1152   case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
1153   case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
1154   case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
1155   case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
1156   case GL_TEXTURE_EXTERNAL_OES:
1157   case GL_PROXY_TEXTURE_2D:
1158   case GL_PROXY_TEXTURE_RECTANGLE:
1159   case GL_PROXY_TEXTURE_CUBE_MAP:
1160      img->Height2 = height - 2 * border; /* == 1 << img->HeightLog2; */
1161      img->HeightLog2 = _mesa_logbase2(img->Height2);
1162      if (depth == 0)
1163         img->Depth2 = 0;
1164      else
1165         img->Depth2 = 1;
1166      img->DepthLog2 = 0;
1167      break;
1168   case GL_TEXTURE_2D_ARRAY:
1169   case GL_PROXY_TEXTURE_2D_ARRAY:
1170      img->Height2 = height - 2 * border; /* == 1 << img->HeightLog2; */
1171      img->HeightLog2 = _mesa_logbase2(img->Height2);
1172      img->Depth2 = depth; /* no border */
1173      img->DepthLog2 = 0; /* not used */
1174      break;
1175   case GL_TEXTURE_3D:
1176   case GL_PROXY_TEXTURE_3D:
1177      img->Height2 = height - 2 * border; /* == 1 << img->HeightLog2; */
1178      img->HeightLog2 = _mesa_logbase2(img->Height2);
1179      img->Depth2 = depth - 2 * border;   /* == 1 << img->DepthLog2; */
1180      img->DepthLog2 = _mesa_logbase2(img->Depth2);
1181      break;
1182   default:
1183      _mesa_problem(NULL, "invalid target 0x%x in _mesa_init_teximage_fields()",
1184                    target);
1185   }
1186
1187   img->MaxLog2 = MAX2(img->WidthLog2, img->HeightLog2);
1188   img->TexFormat = format;
1189}
1190
1191
1192/**
1193 * Free and clear fields of the gl_texture_image struct.
1194 *
1195 * \param ctx GL context.
1196 * \param texImage texture image structure to be cleared.
1197 *
1198 * After the call, \p texImage will have no data associated with it.  Its
1199 * fields are cleared so that its parent object will test incomplete.
1200 */
1201void
1202_mesa_clear_texture_image(struct gl_context *ctx,
1203                          struct gl_texture_image *texImage)
1204{
1205   ctx->Driver.FreeTextureImageBuffer(ctx, texImage);
1206   clear_teximage_fields(texImage);
1207}
1208
1209
1210/**
1211 * This is the fallback for Driver.TestProxyTexImage().  Test the texture
1212 * level, width, height and depth against the ctx->Const limits for textures.
1213 *
1214 * A hardware driver might override this function if, for example, the
1215 * max 3D texture size is 512x512x64 (i.e. not a cube).
1216 *
1217 * Note that width, height, depth == 0 is not an error.  However, a
1218 * texture with zero width/height/depth will be considered "incomplete"
1219 * and texturing will effectively be disabled.
1220 *
1221 * \param target  one of GL_PROXY_TEXTURE_1D, GL_PROXY_TEXTURE_2D,
1222 *                GL_PROXY_TEXTURE_3D, GL_PROXY_TEXTURE_RECTANGLE_NV,
1223 *                GL_PROXY_TEXTURE_CUBE_MAP_ARB.
1224 * \param level  as passed to glTexImage
1225 * \param internalFormat  as passed to glTexImage
1226 * \param format  as passed to glTexImage
1227 * \param type  as passed to glTexImage
1228 * \param width  as passed to glTexImage
1229 * \param height  as passed to glTexImage
1230 * \param depth  as passed to glTexImage
1231 * \param border  as passed to glTexImage
1232 * \return GL_TRUE if the image is acceptable, GL_FALSE if not acceptable.
1233 */
1234GLboolean
1235_mesa_test_proxy_teximage(struct gl_context *ctx, GLenum target, GLint level,
1236                          GLint internalFormat, GLenum format, GLenum type,
1237                          GLint width, GLint height, GLint depth, GLint border)
1238{
1239   GLint maxSize;
1240
1241   (void) internalFormat;
1242   (void) format;
1243   (void) type;
1244
1245   switch (target) {
1246   case GL_PROXY_TEXTURE_1D:
1247      maxSize = 1 << (ctx->Const.MaxTextureLevels - 1);
1248      if (width < 2 * border || width > 2 * border + maxSize)
1249         return GL_FALSE;
1250      if (level >= ctx->Const.MaxTextureLevels)
1251         return GL_FALSE;
1252      if (!ctx->Extensions.ARB_texture_non_power_of_two) {
1253         if (width > 0 && !_mesa_is_pow_two(width - 2 * border))
1254            return GL_FALSE;
1255      }
1256      return GL_TRUE;
1257
1258   case GL_PROXY_TEXTURE_2D:
1259      maxSize = 1 << (ctx->Const.MaxTextureLevels - 1);
1260      if (width < 2 * border || width > 2 * border + maxSize)
1261         return GL_FALSE;
1262      if (height < 2 * border || height > 2 * border + maxSize)
1263         return GL_FALSE;
1264      if (level >= ctx->Const.MaxTextureLevels)
1265         return GL_FALSE;
1266      if (!ctx->Extensions.ARB_texture_non_power_of_two) {
1267         if (width > 0 && !_mesa_is_pow_two(width - 2 * border))
1268            return GL_FALSE;
1269         if (height > 0 && !_mesa_is_pow_two(height - 2 * border))
1270            return GL_FALSE;
1271      }
1272      return GL_TRUE;
1273
1274   case GL_PROXY_TEXTURE_3D:
1275      maxSize = 1 << (ctx->Const.Max3DTextureLevels - 1);
1276      if (width < 2 * border || width > 2 * border + maxSize)
1277         return GL_FALSE;
1278      if (height < 2 * border || height > 2 * border + maxSize)
1279         return GL_FALSE;
1280      if (depth < 2 * border || depth > 2 * border + maxSize)
1281         return GL_FALSE;
1282      if (level >= ctx->Const.Max3DTextureLevels)
1283         return GL_FALSE;
1284      if (!ctx->Extensions.ARB_texture_non_power_of_two) {
1285         if (width > 0 && !_mesa_is_pow_two(width - 2 * border))
1286            return GL_FALSE;
1287         if (height > 0 && !_mesa_is_pow_two(height - 2 * border))
1288            return GL_FALSE;
1289         if (depth > 0 && !_mesa_is_pow_two(depth - 2 * border))
1290            return GL_FALSE;
1291      }
1292      return GL_TRUE;
1293
1294   case GL_PROXY_TEXTURE_RECTANGLE_NV:
1295      maxSize = ctx->Const.MaxTextureRectSize;
1296      if (width < 0 || width > maxSize)
1297         return GL_FALSE;
1298      if (height < 0 || height > maxSize)
1299         return GL_FALSE;
1300      if (level != 0)
1301         return GL_FALSE;
1302      return GL_TRUE;
1303
1304   case GL_PROXY_TEXTURE_CUBE_MAP_ARB:
1305      maxSize = 1 << (ctx->Const.MaxCubeTextureLevels - 1);
1306      if (width < 2 * border || width > 2 * border + maxSize)
1307         return GL_FALSE;
1308      if (height < 2 * border || height > 2 * border + maxSize)
1309         return GL_FALSE;
1310      if (level >= ctx->Const.MaxCubeTextureLevels)
1311         return GL_FALSE;
1312      if (!ctx->Extensions.ARB_texture_non_power_of_two) {
1313         if (width > 0 && !_mesa_is_pow_two(width - 2 * border))
1314            return GL_FALSE;
1315         if (height > 0 && !_mesa_is_pow_two(height - 2 * border))
1316            return GL_FALSE;
1317      }
1318      return GL_TRUE;
1319
1320   case GL_PROXY_TEXTURE_1D_ARRAY_EXT:
1321      maxSize = 1 << (ctx->Const.MaxTextureLevels - 1);
1322      if (width < 2 * border || width > 2 * border + maxSize)
1323         return GL_FALSE;
1324      if (height < 1 || height > ctx->Const.MaxArrayTextureLayers)
1325         return GL_FALSE;
1326      if (level >= ctx->Const.MaxTextureLevels)
1327         return GL_FALSE;
1328      if (!ctx->Extensions.ARB_texture_non_power_of_two) {
1329         if (width > 0 && !_mesa_is_pow_two(width - 2 * border))
1330            return GL_FALSE;
1331      }
1332      return GL_TRUE;
1333
1334   case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
1335      maxSize = 1 << (ctx->Const.MaxTextureLevels - 1);
1336      if (width < 2 * border || width > 2 * border + maxSize)
1337         return GL_FALSE;
1338      if (height < 2 * border || height > 2 * border + maxSize)
1339         return GL_FALSE;
1340      if (depth < 1 || depth > ctx->Const.MaxArrayTextureLayers)
1341         return GL_FALSE;
1342      if (level >= ctx->Const.MaxTextureLevels)
1343         return GL_FALSE;
1344      if (!ctx->Extensions.ARB_texture_non_power_of_two) {
1345         if (width > 0 && !_mesa_is_pow_two(width - 2 * border))
1346            return GL_FALSE;
1347         if (height > 0 && !_mesa_is_pow_two(height - 2 * border))
1348            return GL_FALSE;
1349      }
1350      return GL_TRUE;
1351
1352   default:
1353      _mesa_problem(ctx, "Invalid target in _mesa_test_proxy_teximage");
1354      return GL_FALSE;
1355   }
1356}
1357
1358
1359/**
1360 * Check if the memory used by the texture would exceed the driver's limit.
1361 * This lets us support a max 3D texture size of 8K (for example) but
1362 * prevents allocating a full 8K x 8K x 8K texture.
1363 * XXX this could be rolled into the proxy texture size test (above) but
1364 * we don't have the actual texture internal format at that point.
1365 */
1366static GLboolean
1367legal_texture_size(struct gl_context *ctx, gl_format format,
1368                   GLint width, GLint height, GLint depth)
1369{
1370   uint64_t bytes = _mesa_format_image_size64(format, width, height, depth);
1371   uint64_t mbytes = bytes / (1024 * 1024); /* convert to MB */
1372   return mbytes <= (uint64_t) ctx->Const.MaxTextureMbytes;
1373}
1374
1375
1376/**
1377 * Return true if the format is only valid for glCompressedTexImage.
1378 */
1379static GLboolean
1380compressedteximage_only_format(const struct gl_context *ctx, GLenum format)
1381{
1382   switch (format) {
1383   case GL_ETC1_RGB8_OES:
1384   case GL_PALETTE4_RGB8_OES:
1385   case GL_PALETTE4_RGBA8_OES:
1386   case GL_PALETTE4_R5_G6_B5_OES:
1387   case GL_PALETTE4_RGBA4_OES:
1388   case GL_PALETTE4_RGB5_A1_OES:
1389   case GL_PALETTE8_RGB8_OES:
1390   case GL_PALETTE8_RGBA8_OES:
1391   case GL_PALETTE8_R5_G6_B5_OES:
1392   case GL_PALETTE8_RGBA4_OES:
1393   case GL_PALETTE8_RGB5_A1_OES:
1394      return GL_TRUE;
1395   default:
1396      return GL_FALSE;
1397   }
1398}
1399
1400
1401/**
1402 * Helper function to determine whether a target and specific compression
1403 * format are supported.
1404 */
1405static GLboolean
1406target_can_be_compressed(const struct gl_context *ctx, GLenum target,
1407                         GLenum intFormat)
1408{
1409   (void) intFormat;  /* not used yet */
1410
1411   switch (target) {
1412   case GL_TEXTURE_2D:
1413   case GL_PROXY_TEXTURE_2D:
1414      return GL_TRUE; /* true for any compressed format so far */
1415   case GL_PROXY_TEXTURE_CUBE_MAP:
1416   case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
1417   case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
1418   case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
1419   case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
1420   case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
1421   case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
1422      return ctx->Extensions.ARB_texture_cube_map;
1423   case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
1424   case GL_TEXTURE_2D_ARRAY_EXT:
1425      return (ctx->Extensions.MESA_texture_array ||
1426              ctx->Extensions.EXT_texture_array);
1427   default:
1428      return GL_FALSE;
1429   }
1430}
1431
1432
1433/**
1434 * Check if the given texture target value is legal for a
1435 * glTexImage1/2/3D call.
1436 */
1437static GLboolean
1438legal_teximage_target(struct gl_context *ctx, GLuint dims, GLenum target)
1439{
1440   switch (dims) {
1441   case 1:
1442      switch (target) {
1443      case GL_TEXTURE_1D:
1444      case GL_PROXY_TEXTURE_1D:
1445         return _mesa_is_desktop_gl(ctx);
1446      default:
1447         return GL_FALSE;
1448      }
1449   case 2:
1450      switch (target) {
1451      case GL_TEXTURE_2D:
1452         return GL_TRUE;
1453      case GL_PROXY_TEXTURE_2D:
1454         return _mesa_is_desktop_gl(ctx);
1455      case GL_PROXY_TEXTURE_CUBE_MAP:
1456         return _mesa_is_desktop_gl(ctx)
1457            && ctx->Extensions.ARB_texture_cube_map;
1458      case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
1459      case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
1460      case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
1461      case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
1462      case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
1463      case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
1464         return ctx->Extensions.ARB_texture_cube_map;
1465      case GL_TEXTURE_RECTANGLE_NV:
1466      case GL_PROXY_TEXTURE_RECTANGLE_NV:
1467         return _mesa_is_desktop_gl(ctx)
1468            && ctx->Extensions.NV_texture_rectangle;
1469      case GL_TEXTURE_1D_ARRAY_EXT:
1470      case GL_PROXY_TEXTURE_1D_ARRAY_EXT:
1471         return _mesa_is_desktop_gl(ctx)
1472            && (ctx->Extensions.MESA_texture_array ||
1473                ctx->Extensions.EXT_texture_array);
1474      default:
1475         return GL_FALSE;
1476      }
1477   case 3:
1478      switch (target) {
1479      case GL_TEXTURE_3D:
1480         return GL_TRUE;
1481      case GL_PROXY_TEXTURE_3D:
1482         return _mesa_is_desktop_gl(ctx);
1483      case GL_TEXTURE_2D_ARRAY_EXT:
1484         return (_mesa_is_desktop_gl(ctx)
1485                 && (ctx->Extensions.MESA_texture_array ||
1486                     ctx->Extensions.EXT_texture_array))
1487            || _mesa_is_gles3(ctx);
1488      case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
1489         return _mesa_is_desktop_gl(ctx)
1490            && (ctx->Extensions.MESA_texture_array ||
1491                ctx->Extensions.EXT_texture_array);
1492      default:
1493         return GL_FALSE;
1494      }
1495   default:
1496      _mesa_problem(ctx, "invalid dims=%u in legal_teximage_target()", dims);
1497      return GL_FALSE;
1498   }
1499}
1500
1501
1502/**
1503 * Check if the given texture target value is legal for a
1504 * glTexSubImage, glCopyTexSubImage or glCopyTexImage call.
1505 * The difference compared to legal_teximage_target() above is that
1506 * proxy targets are not supported.
1507 */
1508static GLboolean
1509legal_texsubimage_target(struct gl_context *ctx, GLuint dims, GLenum target)
1510{
1511   switch (dims) {
1512   case 1:
1513      return _mesa_is_desktop_gl(ctx) && target == GL_TEXTURE_1D;
1514   case 2:
1515      switch (target) {
1516      case GL_TEXTURE_2D:
1517         return GL_TRUE;
1518      case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
1519      case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
1520      case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
1521      case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
1522      case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
1523      case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
1524         return ctx->Extensions.ARB_texture_cube_map;
1525      case GL_TEXTURE_RECTANGLE_NV:
1526         return _mesa_is_desktop_gl(ctx)
1527            && ctx->Extensions.NV_texture_rectangle;
1528      case GL_TEXTURE_1D_ARRAY_EXT:
1529         return _mesa_is_desktop_gl(ctx)
1530            && (ctx->Extensions.MESA_texture_array ||
1531                ctx->Extensions.EXT_texture_array);
1532      default:
1533         return GL_FALSE;
1534      }
1535   case 3:
1536      switch (target) {
1537      case GL_TEXTURE_3D:
1538         return GL_TRUE;
1539      case GL_TEXTURE_2D_ARRAY_EXT:
1540         return (_mesa_is_desktop_gl(ctx)
1541                 && (ctx->Extensions.MESA_texture_array ||
1542                     ctx->Extensions.EXT_texture_array))
1543            || _mesa_is_gles3(ctx);
1544      default:
1545         return GL_FALSE;
1546      }
1547   default:
1548      _mesa_problem(ctx, "invalid dims=%u in legal_texsubimage_target()",
1549                    dims);
1550      return GL_FALSE;
1551   }
1552}
1553
1554
1555/**
1556 * Helper function to determine if a texture object is mutable (in terms
1557 * of GL_ARB_texture_storage).
1558 */
1559static GLboolean
1560mutable_tex_object(struct gl_context *ctx, GLenum target)
1561{
1562   if (ctx->Extensions.ARB_texture_storage) {
1563      struct gl_texture_object *texObj =
1564         _mesa_get_current_tex_object(ctx, target);
1565      return !texObj->Immutable;
1566   }
1567   return GL_TRUE;
1568}
1569
1570
1571GLenum
1572_mesa_es_error_check_format_and_type(GLenum format, GLenum type,
1573                                     unsigned dimensions)
1574{
1575   bool type_valid = true;
1576
1577   switch (format) {
1578   case GL_ALPHA:
1579   case GL_LUMINANCE:
1580   case GL_LUMINANCE_ALPHA:
1581      type_valid = (type == GL_UNSIGNED_BYTE
1582                    || type == GL_FLOAT
1583                    || type == GL_HALF_FLOAT_OES);
1584      break;
1585
1586   case GL_RGB:
1587      type_valid = (type == GL_UNSIGNED_BYTE
1588                    || type == GL_UNSIGNED_SHORT_5_6_5
1589                    || type == GL_FLOAT
1590                    || type == GL_HALF_FLOAT_OES);
1591      break;
1592
1593   case GL_RGBA:
1594      type_valid = (type == GL_UNSIGNED_BYTE
1595                    || type == GL_UNSIGNED_SHORT_4_4_4_4
1596                    || type == GL_UNSIGNED_SHORT_5_5_5_1
1597                    || type == GL_FLOAT
1598                    || type == GL_HALF_FLOAT_OES
1599                    || type == GL_UNSIGNED_INT_2_10_10_10_REV);
1600      break;
1601
1602   case GL_DEPTH_COMPONENT:
1603      /* This format is filtered against invalid dimensionalities elsewhere.
1604       */
1605      type_valid = (type == GL_UNSIGNED_SHORT
1606                    || type == GL_UNSIGNED_INT);
1607      break;
1608
1609   case GL_DEPTH_STENCIL:
1610      /* This format is filtered against invalid dimensionalities elsewhere.
1611       */
1612      type_valid = (type == GL_UNSIGNED_INT_24_8);
1613      break;
1614
1615   case GL_BGRA_EXT:
1616      type_valid = (type == GL_UNSIGNED_BYTE);
1617
1618      /* This feels like a bug in the EXT_texture_format_BGRA8888 spec, but
1619       * the format does not appear to be allowed for 3D textures in OpenGL
1620       * ES.
1621       */
1622      if (dimensions != 2)
1623         return GL_INVALID_VALUE;
1624
1625      break;
1626
1627   default:
1628      return GL_INVALID_VALUE;
1629   }
1630
1631   return type_valid ? GL_NO_ERROR : GL_INVALID_OPERATION;
1632}
1633
1634/**
1635 * Test the glTexImage[123]D() parameters for errors.
1636 *
1637 * \param ctx GL context.
1638 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1639 * \param target texture target given by the user.
1640 * \param level image level given by the user.
1641 * \param internalFormat internal format given by the user.
1642 * \param format pixel data format given by the user.
1643 * \param type pixel data type given by the user.
1644 * \param width image width given by the user.
1645 * \param height image height given by the user.
1646 * \param depth image depth given by the user.
1647 * \param border image border given by the user.
1648 *
1649 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
1650 *
1651 * Verifies each of the parameters against the constants specified in
1652 * __struct gl_contextRec::Const and the supported extensions, and according
1653 * to the OpenGL specification.
1654 */
1655static GLboolean
1656texture_error_check( struct gl_context *ctx,
1657                     GLuint dimensions, GLenum target,
1658                     GLint level, GLint internalFormat,
1659                     GLenum format, GLenum type,
1660                     GLint width, GLint height,
1661                     GLint depth, GLint border )
1662{
1663   const GLenum proxyTarget = get_proxy_target(target);
1664   const GLboolean isProxy = target == proxyTarget;
1665   GLboolean sizeOK = GL_TRUE;
1666   GLboolean colorFormat;
1667   GLenum err;
1668
1669   /* Even though there are no color-index textures, we still have to support
1670    * uploading color-index data and remapping it to RGB via the
1671    * GL_PIXEL_MAP_I_TO_[RGBA] tables.
1672    */
1673   const GLboolean indexFormat = (format == GL_COLOR_INDEX);
1674
1675   /* Basic level check (more checking in ctx->Driver.TestProxyTexImage) */
1676   if (level < 0 || level >= MAX_TEXTURE_LEVELS) {
1677      if (!isProxy) {
1678         _mesa_error(ctx, GL_INVALID_VALUE,
1679                     "glTexImage%dD(level=%d)", dimensions, level);
1680      }
1681      return GL_TRUE;
1682   }
1683
1684   /* Check border */
1685   if (border < 0 || border > 1 ||
1686       ((ctx->API != API_OPENGL ||
1687         target == GL_TEXTURE_RECTANGLE_NV ||
1688         target == GL_PROXY_TEXTURE_RECTANGLE_NV) && border != 0)) {
1689      if (!isProxy) {
1690         _mesa_error(ctx, GL_INVALID_VALUE,
1691                     "glTexImage%dD(border=%d)", dimensions, border);
1692      }
1693      return GL_TRUE;
1694   }
1695
1696   if (width < 0 || height < 0 || depth < 0) {
1697      if (!isProxy) {
1698         _mesa_error(ctx, GL_INVALID_VALUE,
1699                     "glTexImage%dD(width, height or depth < 0)", dimensions);
1700      }
1701      return GL_TRUE;
1702   }
1703
1704   /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
1705    * combinations of format, internalFormat, and type that can be used.
1706    * Formats and types that require additional extensions (e.g., GL_FLOAT
1707    * requires GL_OES_texture_float) are filtered elsewhere.
1708    */
1709   if (_mesa_is_gles(ctx) && !_mesa_is_gles3(ctx)) {
1710      if (format != internalFormat) {
1711         _mesa_error(ctx, GL_INVALID_OPERATION,
1712                     "glTexImage%dD(format = %s, internalFormat = %s)",
1713                     dimensions,
1714                     _mesa_lookup_enum_by_nr(format),
1715                     _mesa_lookup_enum_by_nr(internalFormat));
1716         return GL_TRUE;
1717      }
1718
1719      err = _mesa_es_error_check_format_and_type(format, type, dimensions);
1720      if (err != GL_NO_ERROR) {
1721         _mesa_error(ctx, err,
1722                     "glTexImage%dD(format = %s, type = %s)",
1723                     dimensions,
1724                     _mesa_lookup_enum_by_nr(format),
1725                     _mesa_lookup_enum_by_nr(type));
1726         return GL_TRUE;
1727      }
1728   }
1729
1730   /* Do this simple check before calling the TestProxyTexImage() function */
1731   if (proxyTarget == GL_PROXY_TEXTURE_CUBE_MAP_ARB) {
1732      sizeOK = (width == height);
1733   }
1734
1735   /*
1736    * Use the proxy texture driver hook to see if the size/level/etc are
1737    * legal.
1738    */
1739   sizeOK = sizeOK && ctx->Driver.TestProxyTexImage(ctx, proxyTarget, level,
1740                                                    internalFormat, format,
1741                                                    type, width, height,
1742                                                    depth, border);
1743   if (!sizeOK) {
1744      if (!isProxy) {
1745         _mesa_error(ctx, GL_INVALID_VALUE,
1746                     "glTexImage%dD(level=%d, width=%d, height=%d, depth=%d)",
1747                     dimensions, level, width, height, depth);
1748      }
1749      return GL_TRUE;
1750   }
1751
1752   /* Check internalFormat */
1753   if (_mesa_base_tex_format(ctx, internalFormat) < 0) {
1754      if (!isProxy) {
1755         _mesa_error(ctx, GL_INVALID_VALUE,
1756                     "glTexImage%dD(internalFormat=%s)",
1757                     dimensions, _mesa_lookup_enum_by_nr(internalFormat));
1758      }
1759      return GL_TRUE;
1760   }
1761
1762   /* Check incoming image format and type */
1763   err = _mesa_error_check_format_and_type(ctx, format, type);
1764   if (err != GL_NO_ERROR) {
1765      if (!isProxy) {
1766         _mesa_error(ctx, err,
1767                     "glTexImage%dD(incompatible format 0x%x, type 0x%x)",
1768                     dimensions, format, type);
1769      }
1770      return GL_TRUE;
1771   }
1772
1773   /* make sure internal format and format basically agree */
1774   colorFormat = _mesa_is_color_format(format);
1775   if ((_mesa_is_color_format(internalFormat) && !colorFormat && !indexFormat) ||
1776       (_mesa_is_depth_format(internalFormat) != _mesa_is_depth_format(format)) ||
1777       (_mesa_is_ycbcr_format(internalFormat) != _mesa_is_ycbcr_format(format)) ||
1778       (_mesa_is_depthstencil_format(internalFormat) != _mesa_is_depthstencil_format(format)) ||
1779       (_mesa_is_dudv_format(internalFormat) != _mesa_is_dudv_format(format))) {
1780      if (!isProxy)
1781         _mesa_error(ctx, GL_INVALID_OPERATION,
1782                     "glTexImage%dD(incompatible internalFormat 0x%x, format 0x%x)",
1783                     dimensions, internalFormat, format);
1784      return GL_TRUE;
1785   }
1786
1787   /* additional checks for ycbcr textures */
1788   if (internalFormat == GL_YCBCR_MESA) {
1789      ASSERT(ctx->Extensions.MESA_ycbcr_texture);
1790      if (type != GL_UNSIGNED_SHORT_8_8_MESA &&
1791          type != GL_UNSIGNED_SHORT_8_8_REV_MESA) {
1792         char message[100];
1793         _mesa_snprintf(message, sizeof(message),
1794                        "glTexImage%dD(format/type YCBCR mismatch", dimensions);
1795         _mesa_error(ctx, GL_INVALID_ENUM, "%s", message);
1796         return GL_TRUE; /* error */
1797      }
1798      if (target != GL_TEXTURE_2D &&
1799          target != GL_PROXY_TEXTURE_2D &&
1800          target != GL_TEXTURE_RECTANGLE_NV &&
1801          target != GL_PROXY_TEXTURE_RECTANGLE_NV) {
1802         if (!isProxy)
1803            _mesa_error(ctx, GL_INVALID_ENUM, "glTexImage(target)");
1804         return GL_TRUE;
1805      }
1806      if (border != 0) {
1807         if (!isProxy) {
1808            char message[100];
1809            _mesa_snprintf(message, sizeof(message),
1810                           "glTexImage%dD(format=GL_YCBCR_MESA and border=%d)",
1811                           dimensions, border);
1812            _mesa_error(ctx, GL_INVALID_VALUE, "%s", message);
1813         }
1814         return GL_TRUE;
1815      }
1816   }
1817
1818   /* additional checks for depth textures */
1819   if (_mesa_base_tex_format(ctx, internalFormat) == GL_DEPTH_COMPONENT) {
1820      /* Only 1D, 2D, rect, array and cube textures supported, not 3D
1821       * Cubemaps are only supported for GL version > 3.0 or with EXT_gpu_shader4 */
1822      if (target != GL_TEXTURE_1D &&
1823          target != GL_PROXY_TEXTURE_1D &&
1824          target != GL_TEXTURE_2D &&
1825          target != GL_PROXY_TEXTURE_2D &&
1826          target != GL_TEXTURE_1D_ARRAY &&
1827          target != GL_PROXY_TEXTURE_1D_ARRAY &&
1828          target != GL_TEXTURE_2D_ARRAY &&
1829          target != GL_PROXY_TEXTURE_2D_ARRAY &&
1830          target != GL_TEXTURE_RECTANGLE_ARB &&
1831          target != GL_PROXY_TEXTURE_RECTANGLE_ARB &&
1832         !((_mesa_is_cube_face(target) || target == GL_PROXY_TEXTURE_CUBE_MAP) &&
1833           (ctx->Version >= 30 || ctx->Extensions.EXT_gpu_shader4))) {
1834         if (!isProxy)
1835            _mesa_error(ctx, GL_INVALID_ENUM,
1836                        "glTexImage(target/internalFormat)");
1837         return GL_TRUE;
1838      }
1839   }
1840
1841   /* additional checks for compressed textures */
1842   if (_mesa_is_compressed_format(ctx, internalFormat)) {
1843      if (!target_can_be_compressed(ctx, target, internalFormat)) {
1844         if (!isProxy)
1845            _mesa_error(ctx, GL_INVALID_ENUM,
1846                        "glTexImage%dD(target)", dimensions);
1847         return GL_TRUE;
1848      }
1849      if (compressedteximage_only_format(ctx, internalFormat)) {
1850         _mesa_error(ctx, GL_INVALID_OPERATION,
1851               "glTexImage%dD(no compression for format)", dimensions);
1852         return GL_TRUE;
1853      }
1854      if (border != 0) {
1855         if (!isProxy) {
1856            _mesa_error(ctx, GL_INVALID_OPERATION,
1857                        "glTexImage%dD(border!=0)", dimensions);
1858         }
1859         return GL_TRUE;
1860      }
1861   }
1862
1863   /* additional checks for integer textures */
1864   if ((ctx->Version >= 30 || ctx->Extensions.EXT_texture_integer) &&
1865       (_mesa_is_enum_format_integer(format) !=
1866        _mesa_is_enum_format_integer(internalFormat))) {
1867      if (!isProxy) {
1868         _mesa_error(ctx, GL_INVALID_OPERATION,
1869                     "glTexImage%dD(integer/non-integer format mismatch)",
1870                     dimensions);
1871      }
1872      return GL_TRUE;
1873   }
1874
1875   if (!mutable_tex_object(ctx, target)) {
1876      _mesa_error(ctx, GL_INVALID_OPERATION,
1877                  "glTexImage%dD(immutable texture)", dimensions);
1878      return GL_TRUE;
1879   }
1880
1881   /* if we get here, the parameters are OK */
1882   return GL_FALSE;
1883}
1884
1885
1886/**
1887 * Test glTexSubImage[123]D() parameters for errors.
1888 *
1889 * \param ctx GL context.
1890 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1891 * \param target texture target given by the user.
1892 * \param level image level given by the user.
1893 * \param xoffset sub-image x offset given by the user.
1894 * \param yoffset sub-image y offset given by the user.
1895 * \param zoffset sub-image z offset given by the user.
1896 * \param format pixel data format given by the user.
1897 * \param type pixel data type given by the user.
1898 * \param width image width given by the user.
1899 * \param height image height given by the user.
1900 * \param depth image depth given by the user.
1901 *
1902 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
1903 *
1904 * Verifies each of the parameters against the constants specified in
1905 * __struct gl_contextRec::Const and the supported extensions, and according
1906 * to the OpenGL specification.
1907 */
1908static GLboolean
1909subtexture_error_check( struct gl_context *ctx, GLuint dimensions,
1910                        GLenum target, GLint level,
1911                        GLint xoffset, GLint yoffset, GLint zoffset,
1912                        GLint width, GLint height, GLint depth,
1913                        GLenum format, GLenum type )
1914{
1915   GLenum err;
1916
1917   /* Basic level check */
1918   if (level < 0 || level >= MAX_TEXTURE_LEVELS) {
1919      _mesa_error(ctx, GL_INVALID_ENUM, "glTexSubImage2D(level=%d)", level);
1920      return GL_TRUE;
1921   }
1922
1923   /* Check for negative sizes */
1924   if (width < 0) {
1925      _mesa_error(ctx, GL_INVALID_VALUE,
1926                  "glTexSubImage%dD(width=%d)", dimensions, width);
1927      return GL_TRUE;
1928   }
1929   if (height < 0 && dimensions > 1) {
1930      _mesa_error(ctx, GL_INVALID_VALUE,
1931                  "glTexSubImage%dD(height=%d)", dimensions, height);
1932      return GL_TRUE;
1933   }
1934   if (depth < 0 && dimensions > 2) {
1935      _mesa_error(ctx, GL_INVALID_VALUE,
1936                  "glTexSubImage%dD(depth=%d)", dimensions, depth);
1937      return GL_TRUE;
1938   }
1939
1940   /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
1941    * combinations of format and type that can be used.  Formats and types
1942    * that require additional extensions (e.g., GL_FLOAT requires
1943    * GL_OES_texture_float) are filtered elsewhere.
1944    */
1945   if (_mesa_is_gles(ctx) && !_mesa_is_gles3(ctx)) {
1946      err = _mesa_es_error_check_format_and_type(format, type, dimensions);
1947      if (err != GL_NO_ERROR) {
1948         _mesa_error(ctx, err,
1949                     "glTexSubImage%dD(format = %s, type = %s)",
1950                     dimensions,
1951                     _mesa_lookup_enum_by_nr(format),
1952                     _mesa_lookup_enum_by_nr(type));
1953         return GL_TRUE;
1954      }
1955   }
1956
1957   err = _mesa_error_check_format_and_type(ctx, format, type);
1958   if (err != GL_NO_ERROR) {
1959      _mesa_error(ctx, err,
1960                  "glTexSubImage%dD(incompatible format 0x%x, type 0x%x)",
1961                  dimensions, format, type);
1962      return GL_TRUE;
1963   }
1964
1965   return GL_FALSE;
1966}
1967
1968
1969/**
1970 * Do second part of glTexSubImage which depends on the destination texture.
1971 * \return GL_TRUE if error recorded, GL_FALSE otherwise
1972 */
1973static GLboolean
1974subtexture_error_check2( struct gl_context *ctx, GLuint dimensions,
1975			 GLenum target, GLint level,
1976			 GLint xoffset, GLint yoffset, GLint zoffset,
1977			 GLint width, GLint height, GLint depth,
1978			 GLenum format, GLenum type,
1979			 const struct gl_texture_image *destTex )
1980{
1981   if (!destTex) {
1982      /* undefined image level */
1983      _mesa_error(ctx, GL_INVALID_OPERATION, "glTexSubImage%dD", dimensions);
1984      return GL_TRUE;
1985   }
1986
1987   if (xoffset < -((GLint)destTex->Border)) {
1988      _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage%dD(xoffset)",
1989                  dimensions);
1990      return GL_TRUE;
1991   }
1992   if (xoffset + width > (GLint) (destTex->Width + destTex->Border)) {
1993      _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage%dD(xoffset+width)",
1994                  dimensions);
1995      return GL_TRUE;
1996   }
1997   if (dimensions > 1) {
1998      GLint yBorder = (target == GL_TEXTURE_1D_ARRAY) ? 0 : destTex->Border;
1999      if (yoffset < -yBorder) {
2000         _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage%dD(yoffset)",
2001                     dimensions);
2002         return GL_TRUE;
2003      }
2004      if (yoffset + height > (GLint) destTex->Height + yBorder) {
2005         _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage%dD(yoffset+height)",
2006                     dimensions);
2007         return GL_TRUE;
2008      }
2009   }
2010   if (dimensions > 2) {
2011      GLint zBorder = (target == GL_TEXTURE_2D_ARRAY) ? 0 : destTex->Border;
2012      if (zoffset < -zBorder) {
2013         _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage3D(zoffset)");
2014         return GL_TRUE;
2015      }
2016      if (zoffset + depth  > (GLint) destTex->Depth + zBorder) {
2017         _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage3D(zoffset+depth)");
2018         return GL_TRUE;
2019      }
2020   }
2021
2022   if (_mesa_is_format_compressed(destTex->TexFormat)) {
2023      GLuint bw, bh;
2024
2025      if (compressedteximage_only_format(ctx, destTex->InternalFormat)) {
2026         _mesa_error(ctx, GL_INVALID_OPERATION,
2027               "glTexSubImage%dD(no compression for format)", dimensions);
2028         return GL_TRUE;
2029      }
2030
2031      /* do tests which depend on compression block size */
2032      _mesa_get_format_block_size(destTex->TexFormat, &bw, &bh);
2033
2034      /* offset must be multiple of block size */
2035      if ((xoffset % bw != 0) || (yoffset % bh != 0)) {
2036         _mesa_error(ctx, GL_INVALID_OPERATION,
2037                     "glTexSubImage%dD(xoffset = %d, yoffset = %d)",
2038                     dimensions, xoffset, yoffset);
2039         return GL_TRUE;
2040      }
2041      /* size must be multiple of bw by bh or equal to whole texture size */
2042      if ((width % bw != 0) && (GLuint) width != destTex->Width) {
2043         _mesa_error(ctx, GL_INVALID_OPERATION,
2044                     "glTexSubImage%dD(width = %d)", dimensions, width);
2045         return GL_TRUE;
2046      }
2047      if ((height % bh != 0) && (GLuint) height != destTex->Height) {
2048         _mesa_error(ctx, GL_INVALID_OPERATION,
2049                     "glTexSubImage%dD(height = %d)", dimensions, height);
2050         return GL_TRUE;
2051      }
2052   }
2053
2054   if (ctx->Version >= 30 || ctx->Extensions.EXT_texture_integer) {
2055      /* both source and dest must be integer-valued, or neither */
2056      if (_mesa_is_format_integer_color(destTex->TexFormat) !=
2057          _mesa_is_enum_format_integer(format)) {
2058         _mesa_error(ctx, GL_INVALID_OPERATION,
2059                     "glTexSubImage%dD(integer/non-integer format mismatch)",
2060                     dimensions);
2061         return GL_TRUE;
2062      }
2063   }
2064
2065   return GL_FALSE;
2066}
2067
2068
2069/**
2070 * Test glCopyTexImage[12]D() parameters for errors.
2071 *
2072 * \param ctx GL context.
2073 * \param dimensions texture image dimensions (must be 1, 2 or 3).
2074 * \param target texture target given by the user.
2075 * \param level image level given by the user.
2076 * \param internalFormat internal format given by the user.
2077 * \param width image width given by the user.
2078 * \param height image height given by the user.
2079 * \param border texture border.
2080 *
2081 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2082 *
2083 * Verifies each of the parameters against the constants specified in
2084 * __struct gl_contextRec::Const and the supported extensions, and according
2085 * to the OpenGL specification.
2086 */
2087static GLboolean
2088copytexture_error_check( struct gl_context *ctx, GLuint dimensions,
2089                         GLenum target, GLint level, GLint internalFormat,
2090                         GLint width, GLint height, GLint border )
2091{
2092   const GLenum proxyTarget = get_proxy_target(target);
2093   const GLenum type = GL_FLOAT;
2094   GLboolean sizeOK;
2095   GLint baseFormat;
2096
2097   /* check target */
2098   if (!legal_texsubimage_target(ctx, dimensions, target)) {
2099      _mesa_error(ctx, GL_INVALID_ENUM, "glCopyTexImage%uD(target=%s)",
2100                  dimensions, _mesa_lookup_enum_by_nr(target));
2101      return GL_TRUE;
2102   }
2103
2104   /* Basic level check (more checking in ctx->Driver.TestProxyTexImage) */
2105   if (level < 0 || level >= MAX_TEXTURE_LEVELS) {
2106      _mesa_error(ctx, GL_INVALID_VALUE,
2107                  "glCopyTexImage%dD(level=%d)", dimensions, level);
2108      return GL_TRUE;
2109   }
2110
2111   /* Check that the source buffer is complete */
2112   if (_mesa_is_user_fbo(ctx->ReadBuffer)) {
2113      if (ctx->ReadBuffer->_Status == 0) {
2114         _mesa_test_framebuffer_completeness(ctx, ctx->ReadBuffer);
2115      }
2116      if (ctx->ReadBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
2117         _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
2118                     "glCopyTexImage%dD(invalid readbuffer)", dimensions);
2119         return GL_TRUE;
2120      }
2121
2122      if (ctx->ReadBuffer->Visual.samples > 0) {
2123	 _mesa_error(ctx, GL_INVALID_OPERATION,
2124		     "glCopyTexImage%dD(multisample FBO)",
2125		     dimensions);
2126	 return GL_TRUE;
2127      }
2128   }
2129
2130   /* Check border */
2131   if (border < 0 || border > 1 ||
2132       ((ctx->API != API_OPENGL ||
2133         target == GL_TEXTURE_RECTANGLE_NV ||
2134         target == GL_PROXY_TEXTURE_RECTANGLE_NV) && border != 0)) {
2135      _mesa_error(ctx, GL_INVALID_VALUE,
2136                  "glCopyTexImage%dD(border=%d)", dimensions, border);
2137      return GL_TRUE;
2138   }
2139
2140   /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
2141    * internalFormat.
2142    */
2143   if (_mesa_is_gles(ctx) && !_mesa_is_gles3(ctx)) {
2144      switch (internalFormat) {
2145      case GL_ALPHA:
2146      case GL_RGB:
2147      case GL_RGBA:
2148      case GL_LUMINANCE:
2149      case GL_LUMINANCE_ALPHA:
2150         break;
2151      default:
2152         _mesa_error(ctx, GL_INVALID_VALUE,
2153                     "glCopyTexImage%dD(internalFormat)", dimensions);
2154         return GL_TRUE;
2155      }
2156   }
2157
2158   baseFormat = _mesa_base_tex_format(ctx, internalFormat);
2159   if (baseFormat < 0) {
2160      _mesa_error(ctx, GL_INVALID_VALUE,
2161                  "glCopyTexImage%dD(internalFormat)", dimensions);
2162      return GL_TRUE;
2163   }
2164
2165   if (!_mesa_source_buffer_exists(ctx, baseFormat)) {
2166      _mesa_error(ctx, GL_INVALID_OPERATION,
2167                  "glCopyTexImage%dD(missing readbuffer)", dimensions);
2168      return GL_TRUE;
2169   }
2170
2171   /* From the EXT_texture_integer spec:
2172    *
2173    *     "INVALID_OPERATION is generated by CopyTexImage* and CopyTexSubImage*
2174    *      if the texture internalformat is an integer format and the read color
2175    *      buffer is not an integer format, or if the internalformat is not an
2176    *      integer format and the read color buffer is an integer format."
2177    */
2178   if (_mesa_is_color_format(internalFormat)) {
2179      struct gl_renderbuffer *rb = ctx->ReadBuffer->_ColorReadBuffer;
2180
2181      if (_mesa_is_enum_format_integer(rb->InternalFormat) !=
2182	  _mesa_is_enum_format_integer(internalFormat)) {
2183	 _mesa_error(ctx, GL_INVALID_OPERATION,
2184		     "glCopyTexImage%dD(integer vs non-integer)", dimensions);
2185	 return GL_TRUE;
2186      }
2187   }
2188
2189   /* Do size, level checking */
2190   sizeOK = (proxyTarget == GL_PROXY_TEXTURE_CUBE_MAP_ARB)
2191      ? (width == height) : 1;
2192
2193   sizeOK = sizeOK && ctx->Driver.TestProxyTexImage(ctx, proxyTarget, level,
2194                                                    internalFormat, baseFormat,
2195                                                    type, width, height,
2196                                                    1, border);
2197
2198   if (!sizeOK) {
2199      if (dimensions == 1) {
2200         _mesa_error(ctx, GL_INVALID_VALUE,
2201                     "glCopyTexImage1D(width=%d)", width);
2202      }
2203      else {
2204         ASSERT(dimensions == 2);
2205         _mesa_error(ctx, GL_INVALID_VALUE,
2206                     "glCopyTexImage2D(width=%d, height=%d)", width, height);
2207      }
2208      return GL_TRUE;
2209   }
2210
2211   if (_mesa_is_compressed_format(ctx, internalFormat)) {
2212      if (!target_can_be_compressed(ctx, target, internalFormat)) {
2213         _mesa_error(ctx, GL_INVALID_ENUM,
2214                     "glCopyTexImage%dD(target)", dimensions);
2215         return GL_TRUE;
2216      }
2217      if (compressedteximage_only_format(ctx, internalFormat)) {
2218         _mesa_error(ctx, GL_INVALID_OPERATION,
2219               "glCopyTexImage%dD(no compression for format)", dimensions);
2220         return GL_TRUE;
2221      }
2222      if (border != 0) {
2223         _mesa_error(ctx, GL_INVALID_OPERATION,
2224                     "glCopyTexImage%dD(border!=0)", dimensions);
2225         return GL_TRUE;
2226      }
2227   }
2228
2229   if (!mutable_tex_object(ctx, target)) {
2230      _mesa_error(ctx, GL_INVALID_OPERATION,
2231                  "glCopyTexImage%dD(immutable texture)", dimensions);
2232      return GL_TRUE;
2233   }
2234
2235   /* if we get here, the parameters are OK */
2236   return GL_FALSE;
2237}
2238
2239
2240/**
2241 * Test glCopyTexSubImage[12]D() parameters for errors.
2242 * Note that this is the first part of error checking.
2243 * See also copytexsubimage_error_check2() below for the second part.
2244 *
2245 * \param ctx GL context.
2246 * \param dimensions texture image dimensions (must be 1, 2 or 3).
2247 * \param target texture target given by the user.
2248 * \param level image level given by the user.
2249 *
2250 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2251 */
2252static GLboolean
2253copytexsubimage_error_check1( struct gl_context *ctx, GLuint dimensions,
2254                              GLenum target, GLint level)
2255{
2256   /* Check that the source buffer is complete */
2257   if (_mesa_is_user_fbo(ctx->ReadBuffer)) {
2258      if (ctx->ReadBuffer->_Status == 0) {
2259         _mesa_test_framebuffer_completeness(ctx, ctx->ReadBuffer);
2260      }
2261      if (ctx->ReadBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
2262         _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
2263                     "glCopyTexImage%dD(invalid readbuffer)", dimensions);
2264         return GL_TRUE;
2265      }
2266
2267      if (ctx->ReadBuffer->Visual.samples > 0) {
2268	 _mesa_error(ctx, GL_INVALID_OPERATION,
2269		     "glCopyTexSubImage%dD(multisample FBO)",
2270		     dimensions);
2271	 return GL_TRUE;
2272      }
2273   }
2274
2275   /* check target (proxies not allowed) */
2276   if (!legal_texsubimage_target(ctx, dimensions, target)) {
2277      _mesa_error(ctx, GL_INVALID_ENUM, "glCopyTexSubImage%uD(target=%s)",
2278                  dimensions, _mesa_lookup_enum_by_nr(target));
2279      return GL_TRUE;
2280   }
2281
2282   /* Check level */
2283   if (level < 0 || level >= MAX_TEXTURE_LEVELS) {
2284      _mesa_error(ctx, GL_INVALID_VALUE,
2285                  "glCopyTexSubImage%dD(level=%d)", dimensions, level);
2286      return GL_TRUE;
2287   }
2288
2289   return GL_FALSE;
2290}
2291
2292
2293/**
2294 * Second part of error checking for glCopyTexSubImage[12]D().
2295 * \param xoffset sub-image x offset given by the user.
2296 * \param yoffset sub-image y offset given by the user.
2297 * \param zoffset sub-image z offset given by the user.
2298 * \param width image width given by the user.
2299 * \param height image height given by the user.
2300 */
2301static GLboolean
2302copytexsubimage_error_check2( struct gl_context *ctx, GLuint dimensions,
2303			      GLenum target, GLint level,
2304			      GLint xoffset, GLint yoffset, GLint zoffset,
2305			      GLsizei width, GLsizei height,
2306			      const struct gl_texture_image *teximage )
2307{
2308   /* check that dest tex image exists */
2309   if (!teximage) {
2310      _mesa_error(ctx, GL_INVALID_OPERATION,
2311                  "glCopyTexSubImage%dD(undefined texture level: %d)",
2312                  dimensions, level);
2313      return GL_TRUE;
2314   }
2315
2316   /* Check size */
2317   if (width < 0) {
2318      _mesa_error(ctx, GL_INVALID_VALUE,
2319                  "glCopyTexSubImage%dD(width=%d)", dimensions, width);
2320      return GL_TRUE;
2321   }
2322   if (dimensions > 1 && height < 0) {
2323      _mesa_error(ctx, GL_INVALID_VALUE,
2324                  "glCopyTexSubImage%dD(height=%d)", dimensions, height);
2325      return GL_TRUE;
2326   }
2327
2328   /* check x/y offsets */
2329   if (xoffset < -((GLint)teximage->Border)) {
2330      _mesa_error(ctx, GL_INVALID_VALUE,
2331                  "glCopyTexSubImage%dD(xoffset=%d)", dimensions, xoffset);
2332      return GL_TRUE;
2333   }
2334   if (xoffset + width > (GLint) (teximage->Width + teximage->Border)) {
2335      _mesa_error(ctx, GL_INVALID_VALUE,
2336                  "glCopyTexSubImage%dD(xoffset+width)", dimensions);
2337      return GL_TRUE;
2338   }
2339   if (dimensions > 1) {
2340      GLint yBorder = (target == GL_TEXTURE_1D_ARRAY) ? 0 : teximage->Border;
2341      if (yoffset < -yBorder) {
2342         _mesa_error(ctx, GL_INVALID_VALUE,
2343                     "glCopyTexSubImage%dD(yoffset=%d)", dimensions, yoffset);
2344         return GL_TRUE;
2345      }
2346      /* NOTE: we're adding the border here, not subtracting! */
2347      if (yoffset + height > (GLint) teximage->Height + yBorder) {
2348         _mesa_error(ctx, GL_INVALID_VALUE,
2349                     "glCopyTexSubImage%dD(yoffset+height)", dimensions);
2350         return GL_TRUE;
2351      }
2352   }
2353
2354   /* check z offset */
2355   if (dimensions > 2) {
2356      GLint zBorder = (target == GL_TEXTURE_2D_ARRAY) ? 0 : teximage->Border;
2357      if (zoffset < -zBorder) {
2358         _mesa_error(ctx, GL_INVALID_VALUE,
2359                     "glCopyTexSubImage%dD(zoffset)", dimensions);
2360         return GL_TRUE;
2361      }
2362      if (zoffset > (GLint) teximage->Depth + zBorder) {
2363         _mesa_error(ctx, GL_INVALID_VALUE,
2364                     "glCopyTexSubImage%dD(zoffset+depth)", dimensions);
2365         return GL_TRUE;
2366      }
2367   }
2368
2369   if (_mesa_is_format_compressed(teximage->TexFormat)) {
2370      if (compressedteximage_only_format(ctx, teximage->InternalFormat)) {
2371         _mesa_error(ctx, GL_INVALID_OPERATION,
2372               "glCopyTexSubImage%dD(no compression for format)", dimensions);
2373         return GL_TRUE;
2374      }
2375      /* offset must be multiple of 4 */
2376      if ((xoffset & 3) || (yoffset & 3)) {
2377         _mesa_error(ctx, GL_INVALID_VALUE,
2378                     "glCopyTexSubImage%dD(xoffset or yoffset)", dimensions);
2379         return GL_TRUE;
2380      }
2381      /* size must be multiple of 4 */
2382      if ((width & 3) != 0 && (GLuint) width != teximage->Width) {
2383         _mesa_error(ctx, GL_INVALID_VALUE,
2384                     "glCopyTexSubImage%dD(width)", dimensions);
2385         return GL_TRUE;
2386      }
2387      if ((height & 3) != 0 && (GLuint) height != teximage->Height) {
2388         _mesa_error(ctx, GL_INVALID_VALUE,
2389                     "glCopyTexSubImage%dD(height)", dimensions);
2390         return GL_TRUE;
2391      }
2392   }
2393
2394   if (teximage->InternalFormat == GL_YCBCR_MESA) {
2395      _mesa_error(ctx, GL_INVALID_OPERATION, "glCopyTexSubImage2D");
2396      return GL_TRUE;
2397   }
2398
2399   if (!_mesa_source_buffer_exists(ctx, teximage->_BaseFormat)) {
2400      _mesa_error(ctx, GL_INVALID_OPERATION,
2401                  "glCopyTexSubImage%dD(missing readbuffer, format=0x%x)",
2402                  dimensions, teximage->_BaseFormat);
2403      return GL_TRUE;
2404   }
2405
2406   /* From the EXT_texture_integer spec:
2407    *
2408    *     "INVALID_OPERATION is generated by CopyTexImage* and CopyTexSubImage*
2409    *      if the texture internalformat is an integer format and the read color
2410    *      buffer is not an integer format, or if the internalformat is not an
2411    *      integer format and the read color buffer is an integer format."
2412    */
2413   if (_mesa_is_color_format(teximage->InternalFormat)) {
2414      struct gl_renderbuffer *rb = ctx->ReadBuffer->_ColorReadBuffer;
2415
2416      if (_mesa_is_format_integer_color(rb->Format) !=
2417	  _mesa_is_format_integer_color(teximage->TexFormat)) {
2418	 _mesa_error(ctx, GL_INVALID_OPERATION,
2419		     "glCopyTexImage%dD(integer vs non-integer)", dimensions);
2420	 return GL_TRUE;
2421      }
2422   }
2423
2424   /* if we get here, the parameters are OK */
2425   return GL_FALSE;
2426}
2427
2428
2429/** Callback info for walking over FBO hash table */
2430struct cb_info
2431{
2432   struct gl_context *ctx;
2433   struct gl_texture_object *texObj;
2434   GLuint level, face;
2435};
2436
2437
2438/**
2439 * Check render to texture callback.  Called from _mesa_HashWalk().
2440 */
2441static void
2442check_rtt_cb(GLuint key, void *data, void *userData)
2443{
2444   struct gl_framebuffer *fb = (struct gl_framebuffer *) data;
2445   const struct cb_info *info = (struct cb_info *) userData;
2446   struct gl_context *ctx = info->ctx;
2447   const struct gl_texture_object *texObj = info->texObj;
2448   const GLuint level = info->level, face = info->face;
2449
2450   /* If this is a user-created FBO */
2451   if (_mesa_is_user_fbo(fb)) {
2452      GLuint i;
2453      /* check if any of the FBO's attachments point to 'texObj' */
2454      for (i = 0; i < BUFFER_COUNT; i++) {
2455         struct gl_renderbuffer_attachment *att = fb->Attachment + i;
2456         if (att->Type == GL_TEXTURE &&
2457             att->Texture == texObj &&
2458             att->TextureLevel == level &&
2459             att->CubeMapFace == face) {
2460            ASSERT(_mesa_get_attachment_teximage(att));
2461            /* Tell driver about the new renderbuffer texture */
2462            ctx->Driver.RenderTexture(ctx, ctx->DrawBuffer, att);
2463            /* Mark fb status as indeterminate to force re-validation */
2464            fb->_Status = 0;
2465         }
2466      }
2467   }
2468}
2469
2470
2471/**
2472 * When a texture image is specified we have to check if it's bound to
2473 * any framebuffer objects (render to texture) in order to detect changes
2474 * in size or format since that effects FBO completeness.
2475 * Any FBOs rendering into the texture must be re-validated.
2476 */
2477void
2478_mesa_update_fbo_texture(struct gl_context *ctx,
2479                         struct gl_texture_object *texObj,
2480                         GLuint face, GLuint level)
2481{
2482   /* Only check this texture if it's been marked as RenderToTexture */
2483   if (texObj->_RenderToTexture) {
2484      struct cb_info info;
2485      info.ctx = ctx;
2486      info.texObj = texObj;
2487      info.level = level;
2488      info.face = face;
2489      _mesa_HashWalk(ctx->Shared->FrameBuffers, check_rtt_cb, &info);
2490   }
2491}
2492
2493
2494/**
2495 * If the texture object's GenerateMipmap flag is set and we've
2496 * changed the texture base level image, regenerate the rest of the
2497 * mipmap levels now.
2498 */
2499static inline void
2500check_gen_mipmap(struct gl_context *ctx, GLenum target,
2501                 struct gl_texture_object *texObj, GLint level)
2502{
2503   ASSERT(target != GL_TEXTURE_CUBE_MAP);
2504   if (texObj->GenerateMipmap &&
2505       level == texObj->BaseLevel &&
2506       level < texObj->MaxLevel) {
2507      ASSERT(ctx->Driver.GenerateMipmap);
2508      ctx->Driver.GenerateMipmap(ctx, target, texObj);
2509   }
2510}
2511
2512
2513/** Debug helper: override the user-requested internal format */
2514static GLenum
2515override_internal_format(GLenum internalFormat, GLint width, GLint height)
2516{
2517#if 0
2518   if (internalFormat == GL_RGBA16F_ARB ||
2519       internalFormat == GL_RGBA32F_ARB) {
2520      printf("Convert rgba float tex to int %d x %d\n", width, height);
2521      return GL_RGBA;
2522   }
2523   else if (internalFormat == GL_RGB16F_ARB ||
2524            internalFormat == GL_RGB32F_ARB) {
2525      printf("Convert rgb float tex to int %d x %d\n", width, height);
2526      return GL_RGB;
2527   }
2528   else if (internalFormat == GL_LUMINANCE_ALPHA16F_ARB ||
2529            internalFormat == GL_LUMINANCE_ALPHA32F_ARB) {
2530      printf("Convert luminance float tex to int %d x %d\n", width, height);
2531      return GL_LUMINANCE_ALPHA;
2532   }
2533   else if (internalFormat == GL_LUMINANCE16F_ARB ||
2534            internalFormat == GL_LUMINANCE32F_ARB) {
2535      printf("Convert luminance float tex to int %d x %d\n", width, height);
2536      return GL_LUMINANCE;
2537   }
2538   else if (internalFormat == GL_ALPHA16F_ARB ||
2539            internalFormat == GL_ALPHA32F_ARB) {
2540      printf("Convert luminance float tex to int %d x %d\n", width, height);
2541      return GL_ALPHA;
2542   }
2543   /*
2544   else if (internalFormat == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT) {
2545      internalFormat = GL_RGBA;
2546   }
2547   */
2548   else {
2549      return internalFormat;
2550   }
2551#else
2552   return internalFormat;
2553#endif
2554}
2555
2556
2557/**
2558 * Choose the actual hardware format for a texture image.
2559 * Try to use the same format as the previous image level when possible.
2560 * Otherwise, ask the driver for the best format.
2561 * It's important to try to choose a consistant format for all levels
2562 * for efficient texture memory layout/allocation.  In particular, this
2563 * comes up during automatic mipmap generation.
2564 */
2565gl_format
2566_mesa_choose_texture_format(struct gl_context *ctx,
2567                            struct gl_texture_object *texObj,
2568                            GLenum target, GLint level,
2569                            GLenum internalFormat, GLenum format, GLenum type)
2570{
2571   gl_format f;
2572
2573   /* see if we've already chosen a format for the previous level */
2574   if (level > 0) {
2575      struct gl_texture_image *prevImage =
2576	 _mesa_select_tex_image(ctx, texObj, target, level - 1);
2577      /* See if the prev level is defined and has an internal format which
2578       * matches the new internal format.
2579       */
2580      if (prevImage &&
2581          prevImage->Width > 0 &&
2582          prevImage->InternalFormat == internalFormat) {
2583         /* use the same format */
2584         ASSERT(prevImage->TexFormat != MESA_FORMAT_NONE);
2585         return prevImage->TexFormat;
2586      }
2587   }
2588
2589   /* choose format from scratch */
2590   f = ctx->Driver.ChooseTextureFormat(ctx, internalFormat, format, type);
2591   ASSERT(f != MESA_FORMAT_NONE);
2592   return f;
2593}
2594
2595/**
2596 * Adjust pixel unpack params and image dimensions to strip off the
2597 * one-pixel texture border.
2598 *
2599 * Gallium and intel don't support texture borders.  They've seldem been used
2600 * and seldom been implemented correctly anyway.
2601 *
2602 * \param unpackNew returns the new pixel unpack parameters
2603 */
2604static void
2605strip_texture_border(GLenum target,
2606                     GLint *width, GLint *height, GLint *depth,
2607                     const struct gl_pixelstore_attrib *unpack,
2608                     struct gl_pixelstore_attrib *unpackNew)
2609{
2610   assert(width);
2611   assert(height);
2612   assert(depth);
2613
2614   *unpackNew = *unpack;
2615
2616   if (unpackNew->RowLength == 0)
2617      unpackNew->RowLength = *width;
2618
2619   if (unpackNew->ImageHeight == 0)
2620      unpackNew->ImageHeight = *height;
2621
2622   assert(*width >= 3);
2623   unpackNew->SkipPixels++;  /* skip the border */
2624   *width = *width - 2;      /* reduce the width by two border pixels */
2625
2626   /* The min height of a texture with a border is 3 */
2627   if (*height >= 3 && target != GL_TEXTURE_1D_ARRAY) {
2628      unpackNew->SkipRows++;  /* skip the border */
2629      *height = *height - 2;  /* reduce the height by two border pixels */
2630   }
2631
2632   if (*depth >= 3 && target != GL_TEXTURE_2D_ARRAY) {
2633      unpackNew->SkipImages++;  /* skip the border */
2634      *depth = *depth - 2;      /* reduce the depth by two border pixels */
2635   }
2636}
2637
2638/**
2639 * Common code to implement all the glTexImage1D/2D/3D functions.
2640 */
2641static void
2642teximage(struct gl_context *ctx, GLuint dims,
2643         GLenum target, GLint level, GLint internalFormat,
2644         GLsizei width, GLsizei height, GLsizei depth,
2645         GLint border, GLenum format, GLenum type,
2646         const GLvoid *pixels)
2647{
2648   GLboolean error;
2649   struct gl_pixelstore_attrib unpack_no_border;
2650   const struct gl_pixelstore_attrib *unpack = &ctx->Unpack;
2651
2652   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2653
2654   if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
2655      _mesa_debug(ctx, "glTexImage%uD %s %d %s %d %d %d %d %s %s %p\n",
2656                  dims,
2657                  _mesa_lookup_enum_by_nr(target), level,
2658                  _mesa_lookup_enum_by_nr(internalFormat),
2659                  width, height, depth, border,
2660                  _mesa_lookup_enum_by_nr(format),
2661                  _mesa_lookup_enum_by_nr(type), pixels);
2662
2663   internalFormat = override_internal_format(internalFormat, width, height);
2664
2665   /* target error checking */
2666   if (!legal_teximage_target(ctx, dims, target)) {
2667      _mesa_error(ctx, GL_INVALID_ENUM, "glTexImage%uD(target=%s)",
2668                  dims, _mesa_lookup_enum_by_nr(target));
2669      return;
2670   }
2671
2672   /* general error checking */
2673   error = texture_error_check(ctx, dims, target, level, internalFormat,
2674                               format, type, width, height, depth, border);
2675
2676   if (_mesa_is_proxy_texture(target)) {
2677      /* Proxy texture: just clear or set state depending on error checking */
2678      struct gl_texture_image *texImage =
2679         _mesa_get_proxy_tex_image(ctx, target, level);
2680
2681      if (error) {
2682         /* when error, clear all proxy texture image parameters */
2683         if (texImage)
2684            clear_teximage_fields(texImage);
2685      }
2686      else {
2687         /* no error, set the tex image parameters */
2688         struct gl_texture_object *texObj =
2689            _mesa_get_current_tex_object(ctx, target);
2690         gl_format texFormat = _mesa_choose_texture_format(ctx, texObj,
2691                                                           target, level,
2692                                                           internalFormat,
2693                                                           format, type);
2694
2695         if (legal_texture_size(ctx, texFormat, width, height, depth)) {
2696            _mesa_init_teximage_fields(ctx, texImage, width, height,
2697                                       depth, border, internalFormat,
2698                                       texFormat);
2699         }
2700         else if (texImage) {
2701            clear_teximage_fields(texImage);
2702         }
2703      }
2704   }
2705   else {
2706      /* non-proxy target */
2707      const GLuint face = _mesa_tex_target_to_face(target);
2708      struct gl_texture_object *texObj;
2709      struct gl_texture_image *texImage;
2710
2711      if (error) {
2712         return;   /* error was recorded */
2713      }
2714
2715      /* Allow a hardware driver to just strip out the border, to provide
2716       * reliable but slightly incorrect hardware rendering instead of
2717       * rarely-tested software fallback rendering.
2718       */
2719      if (border && ctx->Const.StripTextureBorder) {
2720	 strip_texture_border(target, &width, &height, &depth, unpack,
2721			      &unpack_no_border);
2722         border = 0;
2723	 unpack = &unpack_no_border;
2724      }
2725
2726      if (ctx->NewState & _NEW_PIXEL)
2727	 _mesa_update_state(ctx);
2728
2729      texObj = _mesa_get_current_tex_object(ctx, target);
2730
2731      _mesa_lock_texture(ctx, texObj);
2732      {
2733	 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
2734
2735	 if (!texImage) {
2736	    _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage%uD", dims);
2737	 }
2738         else {
2739            gl_format texFormat;
2740
2741            ctx->Driver.FreeTextureImageBuffer(ctx, texImage);
2742
2743            texFormat = _mesa_choose_texture_format(ctx, texObj, target, level,
2744                                                    internalFormat, format,
2745                                                    type);
2746
2747            if (legal_texture_size(ctx, texFormat, width, height, depth)) {
2748               _mesa_init_teximage_fields(ctx, texImage,
2749                                          width, height, depth,
2750                                          border, internalFormat, texFormat);
2751
2752               /* Give the texture to the driver.  <pixels> may be null. */
2753               ctx->Driver.TexImage(ctx, dims, texImage, format,
2754                                    type, pixels, unpack);
2755
2756               check_gen_mipmap(ctx, target, texObj, level);
2757
2758               _mesa_update_fbo_texture(ctx, texObj, face, level);
2759
2760               _mesa_dirty_texobj(ctx, texObj, GL_TRUE);
2761            }
2762            else {
2763               _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage%uD", dims);
2764            }
2765         }
2766      }
2767      _mesa_unlock_texture(ctx, texObj);
2768   }
2769}
2770
2771
2772/*
2773 * Called from the API.  Note that width includes the border.
2774 */
2775void GLAPIENTRY
2776_mesa_TexImage1D( GLenum target, GLint level, GLint internalFormat,
2777                  GLsizei width, GLint border, GLenum format,
2778                  GLenum type, const GLvoid *pixels )
2779{
2780   GET_CURRENT_CONTEXT(ctx);
2781   teximage(ctx, 1, target, level, internalFormat, width, 1, 1,
2782            border, format, type, pixels);
2783}
2784
2785
2786void GLAPIENTRY
2787_mesa_TexImage2D( GLenum target, GLint level, GLint internalFormat,
2788                  GLsizei width, GLsizei height, GLint border,
2789                  GLenum format, GLenum type,
2790                  const GLvoid *pixels )
2791{
2792   GET_CURRENT_CONTEXT(ctx);
2793   teximage(ctx, 2, target, level, internalFormat, width, height, 1,
2794            border, format, type, pixels);
2795}
2796
2797
2798/*
2799 * Called by the API or display list executor.
2800 * Note that width and height include the border.
2801 */
2802void GLAPIENTRY
2803_mesa_TexImage3D( GLenum target, GLint level, GLint internalFormat,
2804                  GLsizei width, GLsizei height, GLsizei depth,
2805                  GLint border, GLenum format, GLenum type,
2806                  const GLvoid *pixels )
2807{
2808   GET_CURRENT_CONTEXT(ctx);
2809   teximage(ctx, 3, target, level, internalFormat, width, height, depth,
2810            border, format, type, pixels);
2811}
2812
2813
2814void GLAPIENTRY
2815_mesa_TexImage3DEXT( GLenum target, GLint level, GLenum internalFormat,
2816                     GLsizei width, GLsizei height, GLsizei depth,
2817                     GLint border, GLenum format, GLenum type,
2818                     const GLvoid *pixels )
2819{
2820   _mesa_TexImage3D(target, level, (GLint) internalFormat, width, height,
2821                    depth, border, format, type, pixels);
2822}
2823
2824
2825#if FEATURE_OES_EGL_image
2826void GLAPIENTRY
2827_mesa_EGLImageTargetTexture2DOES (GLenum target, GLeglImageOES image)
2828{
2829   struct gl_texture_object *texObj;
2830   struct gl_texture_image *texImage;
2831   bool valid_target;
2832   GET_CURRENT_CONTEXT(ctx);
2833   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2834
2835   switch (target) {
2836   case GL_TEXTURE_2D:
2837      valid_target = ctx->Extensions.OES_EGL_image;
2838      break;
2839   case GL_TEXTURE_EXTERNAL_OES:
2840      valid_target = ctx->Extensions.OES_EGL_image_external;
2841      break;
2842   default:
2843      valid_target = false;
2844      break;
2845   }
2846
2847   if (!valid_target) {
2848      _mesa_error(ctx, GL_INVALID_ENUM,
2849		  "glEGLImageTargetTexture2D(target=%d)", target);
2850      return;
2851   }
2852
2853   if (ctx->NewState & _NEW_PIXEL)
2854      _mesa_update_state(ctx);
2855
2856   texObj = _mesa_get_current_tex_object(ctx, target);
2857   _mesa_lock_texture(ctx, texObj);
2858
2859   if (texObj->Immutable) {
2860      _mesa_error(ctx, GL_INVALID_OPERATION,
2861		  "glEGLImageTargetTexture2D(texture is immutable)");
2862      _mesa_unlock_texture(ctx, texObj);
2863      return;
2864   }
2865
2866   texImage = _mesa_get_tex_image(ctx, texObj, target, 0);
2867   if (!texImage) {
2868      _mesa_error(ctx, GL_OUT_OF_MEMORY, "glEGLImageTargetTexture2D");
2869   } else {
2870      ctx->Driver.FreeTextureImageBuffer(ctx, texImage);
2871
2872      ctx->Driver.EGLImageTargetTexture2D(ctx, target,
2873					  texObj, texImage, image);
2874
2875      _mesa_dirty_texobj(ctx, texObj, GL_TRUE);
2876   }
2877   _mesa_unlock_texture(ctx, texObj);
2878
2879}
2880#endif
2881
2882
2883
2884/**
2885 * Implement all the glTexSubImage1/2/3D() functions.
2886 */
2887static void
2888texsubimage(struct gl_context *ctx, GLuint dims, GLenum target, GLint level,
2889            GLint xoffset, GLint yoffset, GLint zoffset,
2890            GLsizei width, GLsizei height, GLsizei depth,
2891            GLenum format, GLenum type, const GLvoid *pixels )
2892{
2893   struct gl_texture_object *texObj;
2894   struct gl_texture_image *texImage;
2895
2896   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2897
2898   if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
2899      _mesa_debug(ctx, "glTexSubImage%uD %s %d %d %d %d %d %d %d %s %s %p\n",
2900                  dims,
2901                  _mesa_lookup_enum_by_nr(target), level,
2902                  xoffset, yoffset, zoffset, width, height, depth,
2903                  _mesa_lookup_enum_by_nr(format),
2904                  _mesa_lookup_enum_by_nr(type), pixels);
2905
2906   /* check target (proxies not allowed) */
2907   if (!legal_texsubimage_target(ctx, dims, target)) {
2908      _mesa_error(ctx, GL_INVALID_ENUM, "glTexSubImage%uD(target=%s)",
2909                  dims, _mesa_lookup_enum_by_nr(target));
2910      return;
2911   }
2912
2913   if (ctx->NewState & _NEW_PIXEL)
2914      _mesa_update_state(ctx);
2915
2916   if (subtexture_error_check(ctx, dims, target, level, xoffset, yoffset, zoffset,
2917                              width, height, depth, format, type)) {
2918      return;   /* error was detected */
2919   }
2920
2921   texObj = _mesa_get_current_tex_object(ctx, target);
2922
2923   _mesa_lock_texture(ctx, texObj);
2924   {
2925      texImage = _mesa_select_tex_image(ctx, texObj, target, level);
2926
2927      if (subtexture_error_check2(ctx, dims, target, level,
2928                                  xoffset, yoffset, zoffset,
2929				  width, height, depth,
2930                                  format, type, texImage)) {
2931         /* error was recorded */
2932      }
2933      else if (width > 0 && height > 0 && depth > 0) {
2934         /* If we have a border, offset=-1 is legal.  Bias by border width. */
2935         switch (dims) {
2936         case 3:
2937            if (target != GL_TEXTURE_2D_ARRAY)
2938               zoffset += texImage->Border;
2939            /* fall-through */
2940         case 2:
2941            if (target != GL_TEXTURE_1D_ARRAY)
2942               yoffset += texImage->Border;
2943            /* fall-through */
2944         case 1:
2945            xoffset += texImage->Border;
2946         }
2947
2948         ctx->Driver.TexSubImage(ctx, dims, texImage,
2949                                 xoffset, yoffset, zoffset,
2950                                 width, height, depth,
2951                                 format, type, pixels, &ctx->Unpack);
2952
2953         check_gen_mipmap(ctx, target, texObj, level);
2954
2955         ctx->NewState |= _NEW_TEXTURE;
2956      }
2957   }
2958   _mesa_unlock_texture(ctx, texObj);
2959}
2960
2961
2962void GLAPIENTRY
2963_mesa_TexSubImage1D( GLenum target, GLint level,
2964                     GLint xoffset, GLsizei width,
2965                     GLenum format, GLenum type,
2966                     const GLvoid *pixels )
2967{
2968   GET_CURRENT_CONTEXT(ctx);
2969   texsubimage(ctx, 1, target, level,
2970               xoffset, 0, 0,
2971               width, 1, 1,
2972               format, type, pixels);
2973}
2974
2975
2976void GLAPIENTRY
2977_mesa_TexSubImage2D( GLenum target, GLint level,
2978                     GLint xoffset, GLint yoffset,
2979                     GLsizei width, GLsizei height,
2980                     GLenum format, GLenum type,
2981                     const GLvoid *pixels )
2982{
2983   GET_CURRENT_CONTEXT(ctx);
2984   texsubimage(ctx, 2, target, level,
2985               xoffset, yoffset, 0,
2986               width, height, 1,
2987               format, type, pixels);
2988}
2989
2990
2991
2992void GLAPIENTRY
2993_mesa_TexSubImage3D( GLenum target, GLint level,
2994                     GLint xoffset, GLint yoffset, GLint zoffset,
2995                     GLsizei width, GLsizei height, GLsizei depth,
2996                     GLenum format, GLenum type,
2997                     const GLvoid *pixels )
2998{
2999   GET_CURRENT_CONTEXT(ctx);
3000   texsubimage(ctx, 3, target, level,
3001               xoffset, yoffset, zoffset,
3002               width, height, depth,
3003               format, type, pixels);
3004}
3005
3006
3007
3008/**
3009 * For glCopyTexSubImage, return the source renderbuffer to copy texel data
3010 * from.  This depends on whether the texture contains color or depth values.
3011 */
3012static struct gl_renderbuffer *
3013get_copy_tex_image_source(struct gl_context *ctx, gl_format texFormat)
3014{
3015   if (_mesa_get_format_bits(texFormat, GL_DEPTH_BITS) > 0) {
3016      /* reading from depth/stencil buffer */
3017      return ctx->ReadBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;
3018   }
3019   else {
3020      /* copying from color buffer */
3021      return ctx->ReadBuffer->_ColorReadBuffer;
3022   }
3023}
3024
3025
3026
3027/**
3028 * Implement the glCopyTexImage1/2D() functions.
3029 */
3030static void
3031copyteximage(struct gl_context *ctx, GLuint dims,
3032             GLenum target, GLint level, GLenum internalFormat,
3033             GLint x, GLint y, GLsizei width, GLsizei height, GLint border )
3034{
3035   struct gl_texture_object *texObj;
3036   struct gl_texture_image *texImage;
3037   const GLuint face = _mesa_tex_target_to_face(target);
3038
3039   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3040
3041   if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
3042      _mesa_debug(ctx, "glCopyTexImage%uD %s %d %s %d %d %d %d %d\n",
3043                  dims,
3044                  _mesa_lookup_enum_by_nr(target), level,
3045                  _mesa_lookup_enum_by_nr(internalFormat),
3046                  x, y, width, height, border);
3047
3048   if (ctx->NewState & NEW_COPY_TEX_STATE)
3049      _mesa_update_state(ctx);
3050
3051   if (copytexture_error_check(ctx, dims, target, level, internalFormat,
3052                               width, height, border))
3053      return;
3054
3055   texObj = _mesa_get_current_tex_object(ctx, target);
3056
3057   if (border && ctx->Const.StripTextureBorder) {
3058      x += border;
3059      width -= border * 2;
3060      if (dims == 2) {
3061	 y += border;
3062	 height -= border * 2;
3063      }
3064      border = 0;
3065   }
3066
3067   _mesa_lock_texture(ctx, texObj);
3068   {
3069      texImage = _mesa_get_tex_image(ctx, texObj, target, level);
3070
3071      if (!texImage) {
3072	 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexImage%uD", dims);
3073      }
3074      else {
3075         /* choose actual hw format */
3076         gl_format texFormat = _mesa_choose_texture_format(ctx, texObj,
3077                                                           target, level,
3078                                                           internalFormat,
3079                                                           GL_NONE, GL_NONE);
3080
3081         if (legal_texture_size(ctx, texFormat, width, height, 1)) {
3082            GLint srcX = x, srcY = y, dstX = 0, dstY = 0, dstZ = 0;
3083
3084            /* Free old texture image */
3085            ctx->Driver.FreeTextureImageBuffer(ctx, texImage);
3086
3087            _mesa_init_teximage_fields(ctx, texImage, width, height, 1,
3088                                       border, internalFormat, texFormat);
3089
3090            /* Allocate texture memory (no pixel data yet) */
3091            ctx->Driver.TexImage(ctx, dims, texImage,
3092                                 GL_NONE, GL_NONE,
3093                                 NULL, &ctx->Unpack);
3094
3095            if (_mesa_clip_copytexsubimage(ctx, &dstX, &dstY, &srcX, &srcY,
3096                                           &width, &height)) {
3097               struct gl_renderbuffer *srcRb =
3098                  get_copy_tex_image_source(ctx, texImage->TexFormat);
3099
3100               ctx->Driver.CopyTexSubImage(ctx, dims, texImage, dstX, dstY, dstZ,
3101                                           srcRb, srcX, srcY, width, height);
3102            }
3103
3104            check_gen_mipmap(ctx, target, texObj, level);
3105
3106            _mesa_update_fbo_texture(ctx, texObj, face, level);
3107
3108            _mesa_dirty_texobj(ctx, texObj, GL_TRUE);
3109         }
3110         else {
3111            /* probably too large of image */
3112            _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexImage%uD", dims);
3113         }
3114      }
3115   }
3116   _mesa_unlock_texture(ctx, texObj);
3117}
3118
3119
3120
3121void GLAPIENTRY
3122_mesa_CopyTexImage1D( GLenum target, GLint level,
3123                      GLenum internalFormat,
3124                      GLint x, GLint y,
3125                      GLsizei width, GLint border )
3126{
3127   GET_CURRENT_CONTEXT(ctx);
3128   copyteximage(ctx, 1, target, level, internalFormat, x, y, width, 1, border);
3129}
3130
3131
3132
3133void GLAPIENTRY
3134_mesa_CopyTexImage2D( GLenum target, GLint level, GLenum internalFormat,
3135                      GLint x, GLint y, GLsizei width, GLsizei height,
3136                      GLint border )
3137{
3138   GET_CURRENT_CONTEXT(ctx);
3139   copyteximage(ctx, 2, target, level, internalFormat,
3140                x, y, width, height, border);
3141}
3142
3143
3144
3145/**
3146 * Implementation for glCopyTexSubImage1/2/3D() functions.
3147 */
3148static void
3149copytexsubimage(struct gl_context *ctx, GLuint dims, GLenum target, GLint level,
3150                GLint xoffset, GLint yoffset, GLint zoffset,
3151                GLint x, GLint y, GLsizei width, GLsizei height)
3152{
3153   struct gl_texture_object *texObj;
3154   struct gl_texture_image *texImage;
3155
3156   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3157
3158   if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
3159      _mesa_debug(ctx, "glCopyTexSubImage%uD %s %d %d %d %d %d %d %d %d\n",
3160                  dims,
3161                  _mesa_lookup_enum_by_nr(target),
3162                  level, xoffset, yoffset, zoffset, x, y, width, height);
3163
3164   if (ctx->NewState & NEW_COPY_TEX_STATE)
3165      _mesa_update_state(ctx);
3166
3167   if (copytexsubimage_error_check1(ctx, dims, target, level))
3168      return;
3169
3170   texObj = _mesa_get_current_tex_object(ctx, target);
3171
3172   _mesa_lock_texture(ctx, texObj);
3173   {
3174      texImage = _mesa_select_tex_image(ctx, texObj, target, level);
3175
3176      if (copytexsubimage_error_check2(ctx, dims, target, level, xoffset, yoffset,
3177				       zoffset, width, height, texImage)) {
3178         /* error was recored */
3179      }
3180      else {
3181         /* If we have a border, offset=-1 is legal.  Bias by border width. */
3182         switch (dims) {
3183         case 3:
3184            if (target != GL_TEXTURE_2D_ARRAY)
3185               zoffset += texImage->Border;
3186            /* fall-through */
3187         case 2:
3188            if (target != GL_TEXTURE_1D_ARRAY)
3189               yoffset += texImage->Border;
3190            /* fall-through */
3191         case 1:
3192            xoffset += texImage->Border;
3193         }
3194
3195         if (_mesa_clip_copytexsubimage(ctx, &xoffset, &yoffset, &x, &y,
3196                                        &width, &height)) {
3197            struct gl_renderbuffer *srcRb =
3198               get_copy_tex_image_source(ctx, texImage->TexFormat);
3199
3200            ctx->Driver.CopyTexSubImage(ctx, dims, texImage,
3201                                        xoffset, yoffset, zoffset,
3202                                        srcRb, x, y, width, height);
3203
3204            check_gen_mipmap(ctx, target, texObj, level);
3205
3206            ctx->NewState |= _NEW_TEXTURE;
3207         }
3208      }
3209   }
3210   _mesa_unlock_texture(ctx, texObj);
3211}
3212
3213
3214void GLAPIENTRY
3215_mesa_CopyTexSubImage1D( GLenum target, GLint level,
3216                         GLint xoffset, GLint x, GLint y, GLsizei width )
3217{
3218   GET_CURRENT_CONTEXT(ctx);
3219   copytexsubimage(ctx, 1, target, level, xoffset, 0, 0, x, y, width, 1);
3220}
3221
3222
3223
3224void GLAPIENTRY
3225_mesa_CopyTexSubImage2D( GLenum target, GLint level,
3226                         GLint xoffset, GLint yoffset,
3227                         GLint x, GLint y, GLsizei width, GLsizei height )
3228{
3229   GET_CURRENT_CONTEXT(ctx);
3230   copytexsubimage(ctx, 2, target, level, xoffset, yoffset, 0, x, y,
3231                   width, height);
3232}
3233
3234
3235
3236void GLAPIENTRY
3237_mesa_CopyTexSubImage3D( GLenum target, GLint level,
3238                         GLint xoffset, GLint yoffset, GLint zoffset,
3239                         GLint x, GLint y, GLsizei width, GLsizei height )
3240{
3241   GET_CURRENT_CONTEXT(ctx);
3242   copytexsubimage(ctx, 3, target, level, xoffset, yoffset, zoffset,
3243                   x, y, width, height);
3244}
3245
3246
3247
3248
3249/**********************************************************************/
3250/******                   Compressed Textures                    ******/
3251/**********************************************************************/
3252
3253
3254/**
3255 * Return expected size of a compressed texture.
3256 */
3257static GLuint
3258compressed_tex_size(GLsizei width, GLsizei height, GLsizei depth,
3259                    GLenum glformat)
3260{
3261   gl_format mesaFormat = _mesa_glenum_to_compressed_format(glformat);
3262   return _mesa_format_image_size(mesaFormat, width, height, depth);
3263}
3264
3265
3266/*
3267 * Return compressed texture block size, in pixels.
3268 */
3269static void
3270get_compressed_block_size(GLenum glformat, GLuint *bw, GLuint *bh)
3271{
3272   gl_format mesaFormat = _mesa_glenum_to_compressed_format(glformat);
3273   _mesa_get_format_block_size(mesaFormat, bw, bh);
3274}
3275
3276
3277/**
3278 * Error checking for glCompressedTexImage[123]D().
3279 * \param reason  returns reason for error, if any
3280 * \return error code or GL_NO_ERROR.
3281 */
3282static GLenum
3283compressed_texture_error_check(struct gl_context *ctx, GLint dimensions,
3284                               GLenum target, GLint level,
3285                               GLenum internalFormat, GLsizei width,
3286                               GLsizei height, GLsizei depth, GLint border,
3287                               GLsizei imageSize, char **reason)
3288{
3289   const GLenum proxyTarget = get_proxy_target(target);
3290   const GLint maxLevels = _mesa_max_texture_levels(ctx, target);
3291   GLint expectedSize;
3292   GLenum choose_format;
3293   GLenum choose_type;
3294   GLenum proxy_format;
3295
3296   *reason = ""; /* no error */
3297
3298   if (!target_can_be_compressed(ctx, target, internalFormat)) {
3299      *reason = "target";
3300      return GL_INVALID_ENUM;
3301   }
3302
3303   /* This will detect any invalid internalFormat value */
3304   if (!_mesa_is_compressed_format(ctx, internalFormat)) {
3305      *reason = "internalFormat";
3306      return GL_INVALID_ENUM;
3307   }
3308
3309   switch (internalFormat) {
3310#if FEATURE_ES
3311   case GL_PALETTE4_RGB8_OES:
3312   case GL_PALETTE4_RGBA8_OES:
3313   case GL_PALETTE4_R5_G6_B5_OES:
3314   case GL_PALETTE4_RGBA4_OES:
3315   case GL_PALETTE4_RGB5_A1_OES:
3316   case GL_PALETTE8_RGB8_OES:
3317   case GL_PALETTE8_RGBA8_OES:
3318   case GL_PALETTE8_R5_G6_B5_OES:
3319   case GL_PALETTE8_RGBA4_OES:
3320   case GL_PALETTE8_RGB5_A1_OES:
3321      _mesa_cpal_compressed_format_type(internalFormat, &choose_format,
3322					&choose_type);
3323      proxy_format = choose_format;
3324
3325      /* check level */
3326      if (level > 0 || level < -maxLevels) {
3327	 *reason = "level";
3328	 return GL_INVALID_VALUE;
3329      }
3330
3331      if (dimensions != 2) {
3332	 *reason = "compressed paletted textures must be 2D";
3333	 return GL_INVALID_OPERATION;
3334      }
3335
3336      /* Figure out the expected texture size (in bytes).  This will be
3337       * checked against the actual size later.
3338       */
3339      expectedSize = _mesa_cpal_compressed_size(level, internalFormat,
3340						width, height);
3341
3342      /* This is for the benefit of the TestProxyTexImage below.  It expects
3343       * level to be non-negative.  OES_compressed_paletted_texture uses a
3344       * weird mechanism where the level specified to glCompressedTexImage2D
3345       * is -(n-1) number of levels in the texture, and the data specifies the
3346       * complete mipmap stack.  This is done to ensure the palette is the
3347       * same for all levels.
3348       */
3349      level = -level;
3350      break;
3351#endif
3352
3353   default:
3354      choose_format = GL_NONE;
3355      choose_type = GL_NONE;
3356      proxy_format = internalFormat;
3357
3358      /* check level */
3359      if (level < 0 || level >= maxLevels) {
3360	 *reason = "level";
3361	 return GL_INVALID_VALUE;
3362      }
3363
3364      /* Figure out the expected texture size (in bytes).  This will be
3365       * checked against the actual size later.
3366       */
3367      expectedSize = compressed_tex_size(width, height, depth, internalFormat);
3368      break;
3369   }
3370
3371   /* This should really never fail */
3372   if (_mesa_base_tex_format(ctx, internalFormat) < 0) {
3373      *reason = "internalFormat";
3374      return GL_INVALID_ENUM;
3375   }
3376
3377   /* No compressed formats support borders at this time */
3378   if (border != 0) {
3379      *reason = "border != 0";
3380      return GL_INVALID_VALUE;
3381   }
3382
3383   /* For cube map, width must equal height */
3384   if (_mesa_is_cube_face(target) && width != height) {
3385      *reason = "width != height";
3386      return GL_INVALID_VALUE;
3387   }
3388
3389   /* check image size against compression block size */
3390   {
3391      gl_format texFormat =
3392         ctx->Driver.ChooseTextureFormat(ctx, proxy_format,
3393					 choose_format, choose_type);
3394      GLuint bw, bh;
3395
3396      _mesa_get_format_block_size(texFormat, &bw, &bh);
3397      if ((width > bw && width % bw > 0) ||
3398          (height > bh && height % bh > 0)) {
3399         /*
3400          * Per GL_ARB_texture_compression:  GL_INVALID_OPERATION is
3401          * generated [...] if any parameter combinations are not
3402          * supported by the specific compressed internal format.
3403          */
3404         *reason = "invalid width or height for compression format";
3405         return GL_INVALID_OPERATION;
3406      }
3407   }
3408
3409   /* check image sizes */
3410   if (!ctx->Driver.TestProxyTexImage(ctx, proxyTarget, level,
3411				      proxy_format, choose_format,
3412				      choose_type,
3413				      width, height, depth, border)) {
3414      /* See error comment above */
3415      *reason = "invalid width, height or format";
3416      return GL_INVALID_OPERATION;
3417   }
3418
3419   /* check image size in bytes */
3420   if (expectedSize != imageSize) {
3421      /* Per GL_ARB_texture_compression:  GL_INVALID_VALUE is generated [...]
3422       * if <imageSize> is not consistent with the format, dimensions, and
3423       * contents of the specified image.
3424       */
3425      *reason = "imageSize inconsistant with width/height/format";
3426      return GL_INVALID_VALUE;
3427   }
3428
3429   if (!mutable_tex_object(ctx, target)) {
3430      *reason = "immutable texture";
3431      return GL_INVALID_OPERATION;
3432   }
3433
3434   return GL_NO_ERROR;
3435}
3436
3437
3438/**
3439 * Error checking for glCompressedTexSubImage[123]D().
3440 * \warning  There are some bad assumptions here about the size of compressed
3441 *           texture tiles (multiple of 4) used to test the validity of the
3442 *           offset and size parameters.
3443 * \return error code or GL_NO_ERROR.
3444 */
3445static GLenum
3446compressed_subtexture_error_check(struct gl_context *ctx, GLint dimensions,
3447                                  GLenum target, GLint level,
3448                                  GLint xoffset, GLint yoffset, GLint zoffset,
3449                                  GLsizei width, GLsizei height, GLsizei depth,
3450                                  GLenum format, GLsizei imageSize)
3451{
3452   GLint expectedSize, maxLevels = 0, maxTextureSize;
3453   GLuint bw, bh;
3454   (void) zoffset;
3455
3456   if (dimensions == 1) {
3457      /* 1D compressed textures not allowed */
3458      return GL_INVALID_ENUM;
3459   }
3460   else if (dimensions == 2) {
3461      if (target == GL_PROXY_TEXTURE_2D) {
3462         maxLevels = ctx->Const.MaxTextureLevels;
3463      }
3464      else if (target == GL_TEXTURE_2D) {
3465         maxLevels = ctx->Const.MaxTextureLevels;
3466      }
3467      else if (target == GL_PROXY_TEXTURE_CUBE_MAP_ARB) {
3468         if (!ctx->Extensions.ARB_texture_cube_map)
3469            return GL_INVALID_ENUM; /*target*/
3470         maxLevels = ctx->Const.MaxCubeTextureLevels;
3471      }
3472      else if (_mesa_is_cube_face(target)) {
3473         if (!ctx->Extensions.ARB_texture_cube_map)
3474            return GL_INVALID_ENUM; /*target*/
3475         maxLevels = ctx->Const.MaxCubeTextureLevels;
3476      }
3477      else {
3478         return GL_INVALID_ENUM; /*target*/
3479      }
3480   }
3481   else if (dimensions == 3) {
3482      /* 3D compressed textures not allowed */
3483      return GL_INVALID_ENUM;
3484   }
3485
3486   maxTextureSize = 1 << (maxLevels - 1);
3487
3488   /* this will catch any invalid compressed format token */
3489   if (!_mesa_is_compressed_format(ctx, format))
3490      return GL_INVALID_ENUM;
3491
3492   if (width < 1 || width > maxTextureSize)
3493      return GL_INVALID_VALUE;
3494
3495   if ((height < 1 || height > maxTextureSize)
3496       && dimensions > 1)
3497      return GL_INVALID_VALUE;
3498
3499   if (level < 0 || level >= maxLevels)
3500      return GL_INVALID_VALUE;
3501
3502   /*
3503    * do checks which depend on compression block size
3504    */
3505   get_compressed_block_size(format, &bw, &bh);
3506
3507   if ((xoffset % bw != 0) || (yoffset % bh != 0))
3508      return GL_INVALID_VALUE;
3509
3510   if ((width % bw != 0) && width != 2 && width != 1)
3511      return GL_INVALID_VALUE;
3512
3513   if ((height % bh != 0) && height != 2 && height != 1)
3514      return GL_INVALID_VALUE;
3515
3516   expectedSize = compressed_tex_size(width, height, depth, format);
3517   if (expectedSize != imageSize)
3518      return GL_INVALID_VALUE;
3519
3520   return GL_NO_ERROR;
3521}
3522
3523
3524/**
3525 * Do second part of glCompressedTexSubImage error checking.
3526 * \return GL_TRUE if error found, GL_FALSE otherwise.
3527 */
3528static GLboolean
3529compressed_subtexture_error_check2(struct gl_context *ctx, GLuint dims,
3530                                   GLsizei width, GLsizei height,
3531                                   GLsizei depth, GLenum format,
3532                                   struct gl_texture_image *texImage)
3533{
3534
3535   if ((GLint) format != texImage->InternalFormat) {
3536      _mesa_error(ctx, GL_INVALID_OPERATION,
3537                  "glCompressedTexSubImage%uD(format=0x%x)", dims, format);
3538      return GL_TRUE;
3539   }
3540
3541   if (compressedteximage_only_format(ctx, format)) {
3542      _mesa_error(ctx, GL_INVALID_OPERATION,
3543                  "glCompressedTexSubImage%uD(format=0x%x cannot be updated)"
3544                  , dims, format);
3545      return GL_TRUE;
3546   }
3547
3548   if (((width == 1 || width == 2) &&
3549        width != (GLsizei) texImage->Width) ||
3550       (width > (GLsizei) texImage->Width)) {
3551      _mesa_error(ctx, GL_INVALID_VALUE,
3552                  "glCompressedTexSubImage%uD(width=%d)", dims, width);
3553      return GL_TRUE;
3554   }
3555
3556   if (dims >= 2) {
3557      if (((height == 1 || height == 2) &&
3558           height != (GLsizei) texImage->Height) ||
3559          (height > (GLsizei) texImage->Height)) {
3560         _mesa_error(ctx, GL_INVALID_VALUE,
3561                     "glCompressedTexSubImage%uD(height=%d)", dims, height);
3562         return GL_TRUE;
3563      }
3564   }
3565
3566   if (dims >= 3) {
3567      if (((depth == 1 || depth == 2) &&
3568           depth != (GLsizei) texImage->Depth) ||
3569          (depth > (GLsizei) texImage->Depth)) {
3570         _mesa_error(ctx, GL_INVALID_VALUE,
3571                     "glCompressedTexSubImage%uD(depth=%d)", dims, depth);
3572         return GL_TRUE;
3573      }
3574   }
3575
3576   return GL_FALSE;
3577}
3578
3579
3580/**
3581 * Implementation of the glCompressedTexImage1/2/3D() functions.
3582 */
3583static void
3584compressedteximage(struct gl_context *ctx, GLuint dims,
3585                   GLenum target, GLint level,
3586                   GLenum internalFormat, GLsizei width,
3587                   GLsizei height, GLsizei depth, GLint border,
3588                   GLsizei imageSize, const GLvoid *data)
3589{
3590   GLenum error;
3591   char *reason = "";
3592
3593   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3594
3595   if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
3596      _mesa_debug(ctx,
3597                  "glCompressedTexImage%uDARB %s %d %s %d %d %d %d %d %p\n",
3598                  dims,
3599                  _mesa_lookup_enum_by_nr(target), level,
3600                  _mesa_lookup_enum_by_nr(internalFormat),
3601                  width, height, depth, border, imageSize, data);
3602
3603   /* check target */
3604   if (!legal_teximage_target(ctx, dims, target)) {
3605      _mesa_error(ctx, GL_INVALID_ENUM, "glCompressedTexImage%uD(target=%s)",
3606                  dims, _mesa_lookup_enum_by_nr(target));
3607      return;
3608   }
3609
3610   error = compressed_texture_error_check(ctx, dims, target, level,
3611                                          internalFormat, width, height, depth,
3612                                          border, imageSize, &reason);
3613
3614#if FEATURE_ES
3615   /* XXX this is kind of a hack */
3616   if (!error && dims == 2) {
3617      switch (internalFormat) {
3618      case GL_PALETTE4_RGB8_OES:
3619      case GL_PALETTE4_RGBA8_OES:
3620      case GL_PALETTE4_R5_G6_B5_OES:
3621      case GL_PALETTE4_RGBA4_OES:
3622      case GL_PALETTE4_RGB5_A1_OES:
3623      case GL_PALETTE8_RGB8_OES:
3624      case GL_PALETTE8_RGBA8_OES:
3625      case GL_PALETTE8_R5_G6_B5_OES:
3626      case GL_PALETTE8_RGBA4_OES:
3627      case GL_PALETTE8_RGB5_A1_OES:
3628         _mesa_cpal_compressed_teximage2d(target, level, internalFormat,
3629                                          width, height, imageSize, data);
3630         return;
3631      }
3632   }
3633#endif
3634
3635   if (_mesa_is_proxy_texture(target)) {
3636      /* Proxy texture: just check for errors and update proxy state */
3637      struct gl_texture_image *texImage;
3638
3639      if (!error) {
3640         struct gl_texture_object *texObj =
3641            _mesa_get_current_tex_object(ctx, target);
3642         gl_format texFormat =
3643            _mesa_choose_texture_format(ctx, texObj, target, level,
3644                                        internalFormat, GL_NONE, GL_NONE);
3645         if (!legal_texture_size(ctx, texFormat, width, height, depth)) {
3646            error = GL_OUT_OF_MEMORY;
3647         }
3648      }
3649
3650      texImage = _mesa_get_proxy_tex_image(ctx, target, level);
3651      if (texImage) {
3652         if (error) {
3653            /* if error, clear all proxy texture image parameters */
3654            clear_teximage_fields(texImage);
3655         }
3656         else {
3657            /* no error: store the teximage parameters */
3658            _mesa_init_teximage_fields(ctx, texImage, width, height,
3659                                       depth, border, internalFormat,
3660                                       MESA_FORMAT_NONE);
3661         }
3662      }
3663   }
3664   else {
3665      /* non-proxy target */
3666      struct gl_texture_object *texObj;
3667      struct gl_texture_image *texImage;
3668
3669      if (error) {
3670         _mesa_error(ctx, error, "glCompressedTexImage%uD(%s)", dims, reason);
3671         return;
3672      }
3673
3674      texObj = _mesa_get_current_tex_object(ctx, target);
3675
3676      _mesa_lock_texture(ctx, texObj);
3677      {
3678	 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
3679	 if (!texImage) {
3680	    _mesa_error(ctx, GL_OUT_OF_MEMORY,
3681                        "glCompressedTexImage%uD", dims);
3682	 }
3683         else {
3684            gl_format texFormat;
3685
3686            ctx->Driver.FreeTextureImageBuffer(ctx, texImage);
3687
3688            texFormat = _mesa_choose_texture_format(ctx, texObj, target, level,
3689                                                    internalFormat, GL_NONE,
3690                                                    GL_NONE);
3691
3692            if (legal_texture_size(ctx, texFormat, width, height, depth)) {
3693               _mesa_init_teximage_fields(ctx, texImage,
3694                                          width, height, depth,
3695                                          border, internalFormat, texFormat);
3696
3697               ctx->Driver.CompressedTexImage(ctx, dims, texImage, imageSize,
3698                                              data);
3699
3700               check_gen_mipmap(ctx, target, texObj, level);
3701
3702               _mesa_dirty_texobj(ctx, texObj, GL_TRUE);
3703            }
3704            else {
3705               _mesa_error(ctx, GL_OUT_OF_MEMORY,
3706                           "glCompressedTexImage%uD", dims);
3707            }
3708         }
3709      }
3710      _mesa_unlock_texture(ctx, texObj);
3711   }
3712}
3713
3714
3715void GLAPIENTRY
3716_mesa_CompressedTexImage1DARB(GLenum target, GLint level,
3717                              GLenum internalFormat, GLsizei width,
3718                              GLint border, GLsizei imageSize,
3719                              const GLvoid *data)
3720{
3721   GET_CURRENT_CONTEXT(ctx);
3722   compressedteximage(ctx, 1, target, level, internalFormat,
3723                      width, 1, 1, border, imageSize, data);
3724}
3725
3726
3727void GLAPIENTRY
3728_mesa_CompressedTexImage2DARB(GLenum target, GLint level,
3729                              GLenum internalFormat, GLsizei width,
3730                              GLsizei height, GLint border, GLsizei imageSize,
3731                              const GLvoid *data)
3732{
3733   GET_CURRENT_CONTEXT(ctx);
3734   compressedteximage(ctx, 2, target, level, internalFormat,
3735                      width, height, 1, border, imageSize, data);
3736}
3737
3738
3739void GLAPIENTRY
3740_mesa_CompressedTexImage3DARB(GLenum target, GLint level,
3741                              GLenum internalFormat, GLsizei width,
3742                              GLsizei height, GLsizei depth, GLint border,
3743                              GLsizei imageSize, const GLvoid *data)
3744{
3745   GET_CURRENT_CONTEXT(ctx);
3746   compressedteximage(ctx, 3, target, level, internalFormat,
3747                      width, height, depth, border, imageSize, data);
3748}
3749
3750
3751/**
3752 * Common helper for glCompressedTexSubImage1/2/3D().
3753 */
3754static void
3755compressed_tex_sub_image(GLuint dims, GLenum target, GLint level,
3756                         GLint xoffset, GLint yoffset, GLint zoffset,
3757                         GLsizei width, GLsizei height, GLsizei depth,
3758                         GLenum format, GLsizei imageSize, const GLvoid *data)
3759{
3760   struct gl_texture_object *texObj;
3761   struct gl_texture_image *texImage;
3762   GLenum error;
3763   GET_CURRENT_CONTEXT(ctx);
3764   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3765
3766   error = compressed_subtexture_error_check(ctx, dims, target, level,
3767                                             xoffset, 0, 0, /* pos */
3768                                             width, height, depth,   /* size */
3769                                             format, imageSize);
3770   if (error) {
3771      _mesa_error(ctx, error, "glCompressedTexSubImage%uD", dims);
3772      return;
3773   }
3774
3775   texObj = _mesa_get_current_tex_object(ctx, target);
3776
3777   _mesa_lock_texture(ctx, texObj);
3778   {
3779      texImage = _mesa_select_tex_image(ctx, texObj, target, level);
3780      assert(texImage);
3781
3782      if (compressed_subtexture_error_check2(ctx, dims, width, height, depth,
3783                                             format, texImage)) {
3784         /* error was recorded */
3785      }
3786      else if (width > 0 && height > 0 && depth > 0) {
3787         ctx->Driver.CompressedTexSubImage(ctx, dims, texImage,
3788                                           xoffset, yoffset, zoffset,
3789                                           width, height, depth,
3790                                           format, imageSize, data);
3791
3792         check_gen_mipmap(ctx, target, texObj, level);
3793
3794         ctx->NewState |= _NEW_TEXTURE;
3795      }
3796   }
3797   _mesa_unlock_texture(ctx, texObj);
3798}
3799
3800
3801void GLAPIENTRY
3802_mesa_CompressedTexSubImage1DARB(GLenum target, GLint level, GLint xoffset,
3803                                 GLsizei width, GLenum format,
3804                                 GLsizei imageSize, const GLvoid *data)
3805{
3806   compressed_tex_sub_image(1, target, level, xoffset, 0, 0, width, 1, 1,
3807                            format, imageSize, data);
3808}
3809
3810
3811void GLAPIENTRY
3812_mesa_CompressedTexSubImage2DARB(GLenum target, GLint level, GLint xoffset,
3813                                 GLint yoffset, GLsizei width, GLsizei height,
3814                                 GLenum format, GLsizei imageSize,
3815                                 const GLvoid *data)
3816{
3817   compressed_tex_sub_image(2, target, level, xoffset, yoffset, 0,
3818                            width, height, 1, format, imageSize, data);
3819}
3820
3821
3822void GLAPIENTRY
3823_mesa_CompressedTexSubImage3DARB(GLenum target, GLint level, GLint xoffset,
3824                                 GLint yoffset, GLint zoffset, GLsizei width,
3825                                 GLsizei height, GLsizei depth, GLenum format,
3826                                 GLsizei imageSize, const GLvoid *data)
3827{
3828   compressed_tex_sub_image(3, target, level, xoffset, yoffset, zoffset,
3829                            width, height, depth, format, imageSize, data);
3830}
3831
3832static gl_format
3833get_texbuffer_format(const struct gl_context *ctx, GLenum internalFormat)
3834{
3835   switch (internalFormat) {
3836   case GL_ALPHA8:
3837      return MESA_FORMAT_A8;
3838   case GL_ALPHA16:
3839      return MESA_FORMAT_A16;
3840   case GL_ALPHA16F_ARB:
3841      return MESA_FORMAT_ALPHA_FLOAT16;
3842   case GL_ALPHA32F_ARB:
3843      return MESA_FORMAT_ALPHA_FLOAT32;
3844   case GL_ALPHA8I_EXT:
3845      return MESA_FORMAT_ALPHA_INT8;
3846   case GL_ALPHA16I_EXT:
3847      return MESA_FORMAT_ALPHA_INT16;
3848   case GL_ALPHA32I_EXT:
3849      return MESA_FORMAT_ALPHA_INT32;
3850   case GL_ALPHA8UI_EXT:
3851      return MESA_FORMAT_ALPHA_UINT8;
3852   case GL_ALPHA16UI_EXT:
3853      return MESA_FORMAT_ALPHA_UINT16;
3854   case GL_ALPHA32UI_EXT:
3855      return MESA_FORMAT_ALPHA_UINT32;
3856   case GL_LUMINANCE8:
3857      return MESA_FORMAT_L8;
3858   case GL_LUMINANCE16:
3859      return MESA_FORMAT_L16;
3860   case GL_LUMINANCE16F_ARB:
3861      return MESA_FORMAT_LUMINANCE_FLOAT16;
3862   case GL_LUMINANCE32F_ARB:
3863      return MESA_FORMAT_LUMINANCE_FLOAT32;
3864   case GL_LUMINANCE8I_EXT:
3865      return MESA_FORMAT_LUMINANCE_INT8;
3866   case GL_LUMINANCE16I_EXT:
3867      return MESA_FORMAT_LUMINANCE_INT16;
3868   case GL_LUMINANCE32I_EXT:
3869      return MESA_FORMAT_LUMINANCE_INT32;
3870   case GL_LUMINANCE8UI_EXT:
3871      return MESA_FORMAT_LUMINANCE_UINT8;
3872   case GL_LUMINANCE16UI_EXT:
3873      return MESA_FORMAT_LUMINANCE_UINT16;
3874   case GL_LUMINANCE32UI_EXT:
3875      return MESA_FORMAT_LUMINANCE_UINT32;
3876   case GL_LUMINANCE8_ALPHA8:
3877      return MESA_FORMAT_AL88;
3878   case GL_LUMINANCE16_ALPHA16:
3879      return MESA_FORMAT_AL1616;
3880   case GL_LUMINANCE_ALPHA16F_ARB:
3881      return MESA_FORMAT_LUMINANCE_ALPHA_FLOAT16;
3882   case GL_LUMINANCE_ALPHA32F_ARB:
3883      return MESA_FORMAT_LUMINANCE_ALPHA_FLOAT32;
3884   case GL_LUMINANCE_ALPHA8I_EXT:
3885      return MESA_FORMAT_LUMINANCE_ALPHA_INT8;
3886   case GL_LUMINANCE_ALPHA16I_EXT:
3887      return MESA_FORMAT_LUMINANCE_ALPHA_INT8;
3888   case GL_LUMINANCE_ALPHA32I_EXT:
3889      return MESA_FORMAT_LUMINANCE_ALPHA_INT16;
3890   case GL_LUMINANCE_ALPHA8UI_EXT:
3891      return MESA_FORMAT_LUMINANCE_ALPHA_UINT8;
3892   case GL_LUMINANCE_ALPHA16UI_EXT:
3893      return MESA_FORMAT_LUMINANCE_ALPHA_UINT16;
3894   case GL_LUMINANCE_ALPHA32UI_EXT:
3895      return MESA_FORMAT_LUMINANCE_ALPHA_UINT32;
3896   case GL_INTENSITY8:
3897      return MESA_FORMAT_I8;
3898   case GL_INTENSITY16:
3899      return MESA_FORMAT_I16;
3900   case GL_INTENSITY16F_ARB:
3901      return MESA_FORMAT_INTENSITY_FLOAT16;
3902   case GL_INTENSITY32F_ARB:
3903      return MESA_FORMAT_INTENSITY_FLOAT32;
3904   case GL_INTENSITY8I_EXT:
3905      return MESA_FORMAT_INTENSITY_INT8;
3906   case GL_INTENSITY16I_EXT:
3907      return MESA_FORMAT_INTENSITY_INT16;
3908   case GL_INTENSITY32I_EXT:
3909      return MESA_FORMAT_INTENSITY_INT32;
3910   case GL_INTENSITY8UI_EXT:
3911      return MESA_FORMAT_INTENSITY_UINT8;
3912   case GL_INTENSITY16UI_EXT:
3913      return MESA_FORMAT_INTENSITY_UINT16;
3914   case GL_INTENSITY32UI_EXT:
3915      return MESA_FORMAT_INTENSITY_UINT32;
3916   case GL_RGBA8:
3917      return MESA_FORMAT_RGBA8888_REV;
3918   case GL_RGBA16:
3919      return MESA_FORMAT_RGBA_16;
3920   case GL_RGBA16F_ARB:
3921      return MESA_FORMAT_RGBA_FLOAT16;
3922   case GL_RGBA32F_ARB:
3923      return MESA_FORMAT_RGBA_FLOAT32;
3924   case GL_RGBA8I_EXT:
3925      return MESA_FORMAT_RGBA_INT8;
3926   case GL_RGBA16I_EXT:
3927      return MESA_FORMAT_RGBA_INT16;
3928   case GL_RGBA32I_EXT:
3929      return MESA_FORMAT_RGBA_INT32;
3930   case GL_RGBA8UI_EXT:
3931      return MESA_FORMAT_RGBA_UINT8;
3932   case GL_RGBA16UI_EXT:
3933      return MESA_FORMAT_RGBA_UINT16;
3934   case GL_RGBA32UI_EXT:
3935      return MESA_FORMAT_RGBA_UINT32;
3936
3937   case GL_RG8:
3938      return MESA_FORMAT_GR88;
3939   case GL_RG16:
3940      return MESA_FORMAT_RG1616;
3941   case GL_RG16F:
3942      return MESA_FORMAT_RG_FLOAT16;
3943   case GL_RG32F:
3944      return MESA_FORMAT_RG_FLOAT32;
3945   case GL_RG8I:
3946      return MESA_FORMAT_RG_INT8;
3947   case GL_RG16I:
3948      return MESA_FORMAT_RG_INT16;
3949   case GL_RG32I:
3950      return MESA_FORMAT_RG_INT32;
3951   case GL_RG8UI:
3952      return MESA_FORMAT_RG_UINT8;
3953   case GL_RG16UI:
3954      return MESA_FORMAT_RG_UINT16;
3955   case GL_RG32UI:
3956      return MESA_FORMAT_RG_UINT32;
3957
3958   case GL_R8:
3959      return MESA_FORMAT_R8;
3960   case GL_R16:
3961      return MESA_FORMAT_R16;
3962   case GL_R16F:
3963      return MESA_FORMAT_R_FLOAT16;
3964   case GL_R32F:
3965      return MESA_FORMAT_R_FLOAT32;
3966   case GL_R8I:
3967      return MESA_FORMAT_R_INT8;
3968   case GL_R16I:
3969      return MESA_FORMAT_R_INT16;
3970   case GL_R32I:
3971      return MESA_FORMAT_R_INT32;
3972   case GL_R8UI:
3973      return MESA_FORMAT_R_UINT8;
3974   case GL_R16UI:
3975      return MESA_FORMAT_R_UINT16;
3976   case GL_R32UI:
3977      return MESA_FORMAT_R_UINT32;
3978
3979   default:
3980      return MESA_FORMAT_NONE;
3981   }
3982}
3983
3984static gl_format
3985validate_texbuffer_format(const struct gl_context *ctx, GLenum internalFormat)
3986{
3987   gl_format format = get_texbuffer_format(ctx, internalFormat);
3988   GLenum datatype;
3989
3990   if (format == MESA_FORMAT_NONE)
3991      return MESA_FORMAT_NONE;
3992
3993   datatype = _mesa_get_format_datatype(format);
3994   if (datatype == GL_FLOAT && !ctx->Extensions.ARB_texture_float)
3995      return MESA_FORMAT_NONE;
3996
3997   if (datatype == GL_HALF_FLOAT && !ctx->Extensions.ARB_half_float_pixel)
3998      return MESA_FORMAT_NONE;
3999
4000   /* The GL_ARB_texture_rg and GL_ARB_texture_buffer_object specs don't make
4001    * any mention of R/RG formats, but they appear in the GL 3.1 core
4002    * specification.
4003    */
4004   if (ctx->Version <= 30) {
4005      GLenum base_format = _mesa_get_format_base_format(format);
4006
4007      if (base_format == GL_R || base_format == GL_RG)
4008	 return MESA_FORMAT_NONE;
4009   }
4010   return format;
4011}
4012
4013
4014/** GL_ARB_texture_buffer_object */
4015void GLAPIENTRY
4016_mesa_TexBuffer(GLenum target, GLenum internalFormat, GLuint buffer)
4017{
4018   struct gl_texture_object *texObj;
4019   struct gl_buffer_object *bufObj;
4020   gl_format format;
4021
4022   GET_CURRENT_CONTEXT(ctx);
4023   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
4024
4025   if (!ctx->Extensions.ARB_texture_buffer_object) {
4026      _mesa_error(ctx, GL_INVALID_OPERATION, "glTexBuffer");
4027      return;
4028   }
4029
4030   if (target != GL_TEXTURE_BUFFER_ARB) {
4031      _mesa_error(ctx, GL_INVALID_ENUM, "glTexBuffer(target)");
4032      return;
4033   }
4034
4035   format = validate_texbuffer_format(ctx, internalFormat);
4036   if (format == MESA_FORMAT_NONE) {
4037      _mesa_error(ctx, GL_INVALID_ENUM, "glTexBuffer(internalFormat 0x%x)",
4038                  internalFormat);
4039      return;
4040   }
4041
4042   bufObj = _mesa_lookup_bufferobj(ctx, buffer);
4043   if (buffer && !bufObj) {
4044      _mesa_error(ctx, GL_INVALID_OPERATION, "glTexBuffer(buffer %u)", buffer);
4045      return;
4046   }
4047
4048   texObj = _mesa_get_current_tex_object(ctx, target);
4049
4050   _mesa_lock_texture(ctx, texObj);
4051   {
4052      _mesa_reference_buffer_object(ctx, &texObj->BufferObject, bufObj);
4053      texObj->BufferObjectFormat = internalFormat;
4054      texObj->_BufferObjectFormat = format;
4055   }
4056   _mesa_unlock_texture(ctx, texObj);
4057}
4058