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