teximage.c revision 32faf7ab0de8b88bb15a2cb262a73c411dce9d0d
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 */
663GLenum
664_mesa_get_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 _mesa_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      if (level >= ctx->Const.MaxTextureLevels)
1251         return GL_FALSE;
1252      maxSize = 1 << (ctx->Const.MaxTextureLevels - 1); /* level zero size */
1253      maxSize >>= level;  /* level size */
1254      if (width < 2 * border || width > 2 * border + maxSize)
1255         return GL_FALSE;
1256      if (!ctx->Extensions.ARB_texture_non_power_of_two) {
1257         if (width > 0 && !_mesa_is_pow_two(width - 2 * border))
1258            return GL_FALSE;
1259      }
1260      return GL_TRUE;
1261
1262   case GL_PROXY_TEXTURE_2D:
1263      if (level >= ctx->Const.MaxTextureLevels)
1264         return GL_FALSE;
1265      maxSize = 1 << (ctx->Const.MaxTextureLevels - 1);
1266      maxSize >>= level;
1267      if (width < 2 * border || width > 2 * border + maxSize)
1268         return GL_FALSE;
1269      if (height < 2 * border || height > 2 * border + maxSize)
1270         return GL_FALSE;
1271      if (!ctx->Extensions.ARB_texture_non_power_of_two) {
1272         if (width > 0 && !_mesa_is_pow_two(width - 2 * border))
1273            return GL_FALSE;
1274         if (height > 0 && !_mesa_is_pow_two(height - 2 * border))
1275            return GL_FALSE;
1276      }
1277      return GL_TRUE;
1278
1279   case GL_PROXY_TEXTURE_3D:
1280      if (level >= ctx->Const.Max3DTextureLevels)
1281         return GL_FALSE;
1282      maxSize = 1 << (ctx->Const.Max3DTextureLevels - 1);
1283      maxSize >>= level;
1284      if (width < 2 * border || width > 2 * border + maxSize)
1285         return GL_FALSE;
1286      if (height < 2 * border || height > 2 * border + maxSize)
1287         return GL_FALSE;
1288      if (depth < 2 * border || depth > 2 * border + maxSize)
1289         return GL_FALSE;
1290      if (!ctx->Extensions.ARB_texture_non_power_of_two) {
1291         if (width > 0 && !_mesa_is_pow_two(width - 2 * border))
1292            return GL_FALSE;
1293         if (height > 0 && !_mesa_is_pow_two(height - 2 * border))
1294            return GL_FALSE;
1295         if (depth > 0 && !_mesa_is_pow_two(depth - 2 * border))
1296            return GL_FALSE;
1297      }
1298      return GL_TRUE;
1299
1300   case GL_PROXY_TEXTURE_RECTANGLE_NV:
1301      if (level != 0)
1302         return GL_FALSE;
1303      maxSize = ctx->Const.MaxTextureRectSize;
1304      if (width < 0 || width > maxSize)
1305         return GL_FALSE;
1306      if (height < 0 || height > maxSize)
1307         return GL_FALSE;
1308      return GL_TRUE;
1309
1310   case GL_PROXY_TEXTURE_CUBE_MAP_ARB:
1311      if (level >= ctx->Const.MaxCubeTextureLevels)
1312         return GL_FALSE;
1313      maxSize = 1 << (ctx->Const.MaxCubeTextureLevels - 1);
1314      maxSize >>= level;
1315      if (width < 2 * border || width > 2 * border + maxSize)
1316         return GL_FALSE;
1317      if (height < 2 * border || height > 2 * border + maxSize)
1318         return GL_FALSE;
1319      if (!ctx->Extensions.ARB_texture_non_power_of_two) {
1320         if (width > 0 && !_mesa_is_pow_two(width - 2 * border))
1321            return GL_FALSE;
1322         if (height > 0 && !_mesa_is_pow_two(height - 2 * border))
1323            return GL_FALSE;
1324      }
1325      return GL_TRUE;
1326
1327   case GL_PROXY_TEXTURE_1D_ARRAY_EXT:
1328      if (level >= ctx->Const.MaxTextureLevels)
1329         return GL_FALSE;
1330      maxSize = 1 << (ctx->Const.MaxTextureLevels - 1);
1331      maxSize >>= level;
1332      if (width < 2 * border || width > 2 * border + maxSize)
1333         return GL_FALSE;
1334      if (height < 1 || height > ctx->Const.MaxArrayTextureLayers)
1335         return GL_FALSE;
1336      if (!ctx->Extensions.ARB_texture_non_power_of_two) {
1337         if (width > 0 && !_mesa_is_pow_two(width - 2 * border))
1338            return GL_FALSE;
1339      }
1340      return GL_TRUE;
1341
1342   case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
1343      if (level >= ctx->Const.MaxTextureLevels)
1344         return GL_FALSE;
1345      maxSize = 1 << (ctx->Const.MaxTextureLevels - 1);
1346      maxSize >>= level;
1347      if (width < 2 * border || width > 2 * border + maxSize)
1348         return GL_FALSE;
1349      if (height < 2 * border || height > 2 * border + maxSize)
1350         return GL_FALSE;
1351      if (depth < 1 || depth > ctx->Const.MaxArrayTextureLayers)
1352         return GL_FALSE;
1353      if (!ctx->Extensions.ARB_texture_non_power_of_two) {
1354         if (width > 0 && !_mesa_is_pow_two(width - 2 * border))
1355            return GL_FALSE;
1356         if (height > 0 && !_mesa_is_pow_two(height - 2 * border))
1357            return GL_FALSE;
1358      }
1359      return GL_TRUE;
1360
1361   default:
1362      _mesa_problem(ctx, "Invalid target in _mesa_test_proxy_teximage");
1363      return GL_FALSE;
1364   }
1365}
1366
1367
1368/**
1369 * Check if the memory used by the texture would exceed the driver's limit.
1370 * This lets us support a max 3D texture size of 8K (for example) but
1371 * prevents allocating a full 8K x 8K x 8K texture.
1372 * XXX this could be rolled into the proxy texture size test (above) but
1373 * we don't have the actual texture internal format at that point.
1374 */
1375static GLboolean
1376legal_texture_size(struct gl_context *ctx, gl_format format,
1377                   GLint width, GLint height, GLint depth)
1378{
1379   uint64_t bytes = _mesa_format_image_size64(format, width, height, depth);
1380   uint64_t mbytes = bytes / (1024 * 1024); /* convert to MB */
1381   return mbytes <= (uint64_t) ctx->Const.MaxTextureMbytes;
1382}
1383
1384
1385/**
1386 * Return true if the format is only valid for glCompressedTexImage.
1387 */
1388static GLboolean
1389compressedteximage_only_format(const struct gl_context *ctx, GLenum format)
1390{
1391   switch (format) {
1392   case GL_ETC1_RGB8_OES:
1393   case GL_PALETTE4_RGB8_OES:
1394   case GL_PALETTE4_RGBA8_OES:
1395   case GL_PALETTE4_R5_G6_B5_OES:
1396   case GL_PALETTE4_RGBA4_OES:
1397   case GL_PALETTE4_RGB5_A1_OES:
1398   case GL_PALETTE8_RGB8_OES:
1399   case GL_PALETTE8_RGBA8_OES:
1400   case GL_PALETTE8_R5_G6_B5_OES:
1401   case GL_PALETTE8_RGBA4_OES:
1402   case GL_PALETTE8_RGB5_A1_OES:
1403      return GL_TRUE;
1404   default:
1405      return GL_FALSE;
1406   }
1407}
1408
1409
1410/**
1411 * Helper function to determine whether a target and specific compression
1412 * format are supported.
1413 */
1414static GLboolean
1415target_can_be_compressed(const struct gl_context *ctx, GLenum target,
1416                         GLenum intFormat)
1417{
1418   (void) intFormat;  /* not used yet */
1419
1420   switch (target) {
1421   case GL_TEXTURE_2D:
1422   case GL_PROXY_TEXTURE_2D:
1423      return GL_TRUE; /* true for any compressed format so far */
1424   case GL_PROXY_TEXTURE_CUBE_MAP:
1425   case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
1426   case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
1427   case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
1428   case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
1429   case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
1430   case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
1431      return ctx->Extensions.ARB_texture_cube_map;
1432   case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
1433   case GL_TEXTURE_2D_ARRAY_EXT:
1434      return (ctx->Extensions.MESA_texture_array ||
1435              ctx->Extensions.EXT_texture_array);
1436   default:
1437      return GL_FALSE;
1438   }
1439}
1440
1441
1442/**
1443 * Check if the given texture target value is legal for a
1444 * glTexImage1/2/3D call.
1445 */
1446static GLboolean
1447legal_teximage_target(struct gl_context *ctx, GLuint dims, GLenum target)
1448{
1449   switch (dims) {
1450   case 1:
1451      switch (target) {
1452      case GL_TEXTURE_1D:
1453      case GL_PROXY_TEXTURE_1D:
1454         return _mesa_is_desktop_gl(ctx);
1455      default:
1456         return GL_FALSE;
1457      }
1458   case 2:
1459      switch (target) {
1460      case GL_TEXTURE_2D:
1461         return GL_TRUE;
1462      case GL_PROXY_TEXTURE_2D:
1463         return _mesa_is_desktop_gl(ctx);
1464      case GL_PROXY_TEXTURE_CUBE_MAP:
1465         return _mesa_is_desktop_gl(ctx)
1466            && ctx->Extensions.ARB_texture_cube_map;
1467      case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
1468      case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
1469      case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
1470      case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
1471      case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
1472      case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
1473         return ctx->Extensions.ARB_texture_cube_map;
1474      case GL_TEXTURE_RECTANGLE_NV:
1475      case GL_PROXY_TEXTURE_RECTANGLE_NV:
1476         return _mesa_is_desktop_gl(ctx)
1477            && ctx->Extensions.NV_texture_rectangle;
1478      case GL_TEXTURE_1D_ARRAY_EXT:
1479      case GL_PROXY_TEXTURE_1D_ARRAY_EXT:
1480         return _mesa_is_desktop_gl(ctx)
1481            && (ctx->Extensions.MESA_texture_array ||
1482                ctx->Extensions.EXT_texture_array);
1483      default:
1484         return GL_FALSE;
1485      }
1486   case 3:
1487      switch (target) {
1488      case GL_TEXTURE_3D:
1489         return GL_TRUE;
1490      case GL_PROXY_TEXTURE_3D:
1491         return _mesa_is_desktop_gl(ctx);
1492      case GL_TEXTURE_2D_ARRAY_EXT:
1493         return (_mesa_is_desktop_gl(ctx)
1494                 && (ctx->Extensions.MESA_texture_array ||
1495                     ctx->Extensions.EXT_texture_array))
1496            || _mesa_is_gles3(ctx);
1497      case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
1498         return _mesa_is_desktop_gl(ctx)
1499            && (ctx->Extensions.MESA_texture_array ||
1500                ctx->Extensions.EXT_texture_array);
1501      default:
1502         return GL_FALSE;
1503      }
1504   default:
1505      _mesa_problem(ctx, "invalid dims=%u in legal_teximage_target()", dims);
1506      return GL_FALSE;
1507   }
1508}
1509
1510
1511/**
1512 * Check if the given texture target value is legal for a
1513 * glTexSubImage, glCopyTexSubImage or glCopyTexImage call.
1514 * The difference compared to legal_teximage_target() above is that
1515 * proxy targets are not supported.
1516 */
1517static GLboolean
1518legal_texsubimage_target(struct gl_context *ctx, GLuint dims, GLenum target)
1519{
1520   switch (dims) {
1521   case 1:
1522      return _mesa_is_desktop_gl(ctx) && target == GL_TEXTURE_1D;
1523   case 2:
1524      switch (target) {
1525      case GL_TEXTURE_2D:
1526         return GL_TRUE;
1527      case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
1528      case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
1529      case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
1530      case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
1531      case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
1532      case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
1533         return ctx->Extensions.ARB_texture_cube_map;
1534      case GL_TEXTURE_RECTANGLE_NV:
1535         return _mesa_is_desktop_gl(ctx)
1536            && ctx->Extensions.NV_texture_rectangle;
1537      case GL_TEXTURE_1D_ARRAY_EXT:
1538         return _mesa_is_desktop_gl(ctx)
1539            && (ctx->Extensions.MESA_texture_array ||
1540                ctx->Extensions.EXT_texture_array);
1541      default:
1542         return GL_FALSE;
1543      }
1544   case 3:
1545      switch (target) {
1546      case GL_TEXTURE_3D:
1547         return GL_TRUE;
1548      case GL_TEXTURE_2D_ARRAY_EXT:
1549         return (_mesa_is_desktop_gl(ctx)
1550                 && (ctx->Extensions.MESA_texture_array ||
1551                     ctx->Extensions.EXT_texture_array))
1552            || _mesa_is_gles3(ctx);
1553      default:
1554         return GL_FALSE;
1555      }
1556   default:
1557      _mesa_problem(ctx, "invalid dims=%u in legal_texsubimage_target()",
1558                    dims);
1559      return GL_FALSE;
1560   }
1561}
1562
1563
1564/**
1565 * Helper function to determine if a texture object is mutable (in terms
1566 * of GL_ARB_texture_storage).
1567 */
1568static GLboolean
1569mutable_tex_object(struct gl_context *ctx, GLenum target)
1570{
1571   if (ctx->Extensions.ARB_texture_storage) {
1572      struct gl_texture_object *texObj =
1573         _mesa_get_current_tex_object(ctx, target);
1574      return !texObj->Immutable;
1575   }
1576   return GL_TRUE;
1577}
1578
1579
1580GLenum
1581_mesa_es_error_check_format_and_type(GLenum format, GLenum type,
1582                                     unsigned dimensions)
1583{
1584   bool type_valid = true;
1585
1586   switch (format) {
1587   case GL_ALPHA:
1588   case GL_LUMINANCE:
1589   case GL_LUMINANCE_ALPHA:
1590      type_valid = (type == GL_UNSIGNED_BYTE
1591                    || type == GL_FLOAT
1592                    || type == GL_HALF_FLOAT_OES);
1593      break;
1594
1595   case GL_RGB:
1596      type_valid = (type == GL_UNSIGNED_BYTE
1597                    || type == GL_UNSIGNED_SHORT_5_6_5
1598                    || type == GL_FLOAT
1599                    || type == GL_HALF_FLOAT_OES);
1600      break;
1601
1602   case GL_RGBA:
1603      type_valid = (type == GL_UNSIGNED_BYTE
1604                    || type == GL_UNSIGNED_SHORT_4_4_4_4
1605                    || type == GL_UNSIGNED_SHORT_5_5_5_1
1606                    || type == GL_FLOAT
1607                    || type == GL_HALF_FLOAT_OES
1608                    || type == GL_UNSIGNED_INT_2_10_10_10_REV);
1609      break;
1610
1611   case GL_DEPTH_COMPONENT:
1612      /* This format is filtered against invalid dimensionalities elsewhere.
1613       */
1614      type_valid = (type == GL_UNSIGNED_SHORT
1615                    || type == GL_UNSIGNED_INT);
1616      break;
1617
1618   case GL_DEPTH_STENCIL:
1619      /* This format is filtered against invalid dimensionalities elsewhere.
1620       */
1621      type_valid = (type == GL_UNSIGNED_INT_24_8);
1622      break;
1623
1624   case GL_BGRA_EXT:
1625      type_valid = (type == GL_UNSIGNED_BYTE);
1626
1627      /* This feels like a bug in the EXT_texture_format_BGRA8888 spec, but
1628       * the format does not appear to be allowed for 3D textures in OpenGL
1629       * ES.
1630       */
1631      if (dimensions != 2)
1632         return GL_INVALID_VALUE;
1633
1634      break;
1635
1636   default:
1637      return GL_INVALID_VALUE;
1638   }
1639
1640   return type_valid ? GL_NO_ERROR : GL_INVALID_OPERATION;
1641}
1642
1643
1644
1645/**
1646 * Return expected size of a compressed texture.
1647 */
1648static GLuint
1649compressed_tex_size(GLsizei width, GLsizei height, GLsizei depth,
1650                    GLenum glformat)
1651{
1652   gl_format mesaFormat = _mesa_glenum_to_compressed_format(glformat);
1653   return _mesa_format_image_size(mesaFormat, width, height, depth);
1654}
1655
1656
1657/*
1658 * Return compressed texture block size, in pixels.
1659 */
1660static void
1661get_compressed_block_size(GLenum glformat, GLuint *bw, GLuint *bh)
1662{
1663   gl_format mesaFormat = _mesa_glenum_to_compressed_format(glformat);
1664   _mesa_get_format_block_size(mesaFormat, bw, bh);
1665}
1666
1667
1668/**
1669 * Special value returned by error some texture error checking functions when
1670 * an error is detected and the proxy texture image's width/height/depth/format
1671 * fields should be zeroed-out.
1672 */
1673#define PROXY_ERROR 2
1674
1675
1676/**
1677 * Test the glTexImage[123]D() parameters for errors.
1678 *
1679 * \param ctx GL context.
1680 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1681 * \param target texture target given by the user.
1682 * \param level image level given by the user.
1683 * \param internalFormat internal format given by the user.
1684 * \param format pixel data format given by the user.
1685 * \param type pixel data type given by the user.
1686 * \param width image width given by the user.
1687 * \param height image height given by the user.
1688 * \param depth image depth given by the user.
1689 * \param border image border given by the user.
1690 *
1691 * \return PROXY_ERROR if there's an error that should zero-out the proxy image,
1692 *         GL_TRUE if a regular GL error is found, or GL_FALSE if no error,
1693 *
1694 * Verifies each of the parameters against the constants specified in
1695 * __struct gl_contextRec::Const and the supported extensions, and according
1696 * to the OpenGL specification.
1697 */
1698static GLenum
1699texture_error_check( struct gl_context *ctx,
1700                     GLuint dimensions, GLenum target,
1701                     GLint level, GLint internalFormat,
1702                     GLenum format, GLenum type,
1703                     GLint width, GLint height,
1704                     GLint depth, GLint border )
1705{
1706   const GLenum proxyTarget = _mesa_get_proxy_target(target);
1707   const GLboolean isProxy = target == proxyTarget;
1708   GLboolean sizeOK = GL_TRUE;
1709   GLboolean colorFormat;
1710   GLenum err;
1711
1712   /* Even though there are no color-index textures, we still have to support
1713    * uploading color-index data and remapping it to RGB via the
1714    * GL_PIXEL_MAP_I_TO_[RGBA] tables.
1715    */
1716   const GLboolean indexFormat = (format == GL_COLOR_INDEX);
1717
1718   /* Note: for proxy textures, some error conditions immediately generate
1719    * a GL error in the usual way.  But others do not generate a GL error.
1720    * Instead, they cause the width, height, depth, format fields of the
1721    * texture image to be zeroed-out.  The GL spec seems to indicate that the
1722    * zero-out behaviour is only used in cases related to memory allocation.
1723    */
1724
1725   /* Basic level check (more checking in ctx->Driver.TestProxyTexImage) */
1726   if (level < 0 || level >= MAX_TEXTURE_LEVELS) {
1727      _mesa_error(ctx, GL_INVALID_VALUE,
1728                  "glTexImage%dD(level=%d)", dimensions, level);
1729      return GL_TRUE;
1730   }
1731
1732   /* Check border */
1733   if (border < 0 || border > 1 ||
1734       ((ctx->API != API_OPENGL ||
1735         target == GL_TEXTURE_RECTANGLE_NV ||
1736         target == GL_PROXY_TEXTURE_RECTANGLE_NV) && border != 0)) {
1737      _mesa_error(ctx, GL_INVALID_VALUE,
1738                  "glTexImage%dD(border=%d)", dimensions, border);
1739      return GL_TRUE;
1740   }
1741
1742   if (width < 0 || height < 0 || depth < 0) {
1743      _mesa_error(ctx, GL_INVALID_VALUE,
1744                  "glTexImage%dD(width, height or depth < 0)", dimensions);
1745      return GL_TRUE;
1746   }
1747
1748   /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
1749    * combinations of format, internalFormat, and type that can be used.
1750    * Formats and types that require additional extensions (e.g., GL_FLOAT
1751    * requires GL_OES_texture_float) are filtered elsewhere.
1752    */
1753   if (_mesa_is_gles(ctx) && !_mesa_is_gles3(ctx)) {
1754      if (format != internalFormat) {
1755         _mesa_error(ctx, GL_INVALID_OPERATION,
1756                     "glTexImage%dD(format = %s, internalFormat = %s)",
1757                     dimensions,
1758                     _mesa_lookup_enum_by_nr(format),
1759                     _mesa_lookup_enum_by_nr(internalFormat));
1760         return GL_TRUE;
1761      }
1762
1763      err = _mesa_es_error_check_format_and_type(format, type, dimensions);
1764      if (err != GL_NO_ERROR) {
1765         _mesa_error(ctx, err,
1766                     "glTexImage%dD(format = %s, type = %s)",
1767                     dimensions,
1768                     _mesa_lookup_enum_by_nr(format),
1769                     _mesa_lookup_enum_by_nr(type));
1770         return GL_TRUE;
1771      }
1772   }
1773
1774   /* Do this simple check before calling the TestProxyTexImage() function */
1775   if (proxyTarget == GL_PROXY_TEXTURE_CUBE_MAP_ARB) {
1776      sizeOK = (width == height);
1777   }
1778
1779   /*
1780    * Use the proxy texture driver hook to see if the size/level/etc are
1781    * legal.
1782    */
1783   sizeOK = sizeOK && ctx->Driver.TestProxyTexImage(ctx, proxyTarget, level,
1784                                                    internalFormat, format,
1785                                                    type, width, height,
1786                                                    depth, border);
1787   if (!sizeOK) {
1788      if (isProxy) {
1789         /* No GL error is recorded, but we need to zero-out the image dims */
1790         return PROXY_ERROR;
1791      }
1792      else {
1793         _mesa_error(ctx, GL_INVALID_VALUE,
1794                     "glTexImage%dD(level=%d, width=%d, height=%d, depth=%d)",
1795                     dimensions, level, width, height, depth);
1796         return GL_TRUE;
1797      }
1798   }
1799
1800   /* Check internalFormat */
1801   if (_mesa_base_tex_format(ctx, internalFormat) < 0) {
1802      _mesa_error(ctx, GL_INVALID_VALUE,
1803                  "glTexImage%dD(internalFormat=%s)",
1804                  dimensions, _mesa_lookup_enum_by_nr(internalFormat));
1805      return GL_TRUE;
1806   }
1807
1808   /* Check incoming image format and type */
1809   err = _mesa_error_check_format_and_type(ctx, format, type);
1810   if (err != GL_NO_ERROR) {
1811      _mesa_error(ctx, err,
1812                  "glTexImage%dD(incompatible format 0x%x, type 0x%x)",
1813                  dimensions, format, type);
1814      return GL_TRUE;
1815   }
1816
1817   /* make sure internal format and format basically agree */
1818   colorFormat = _mesa_is_color_format(format);
1819   if ((_mesa_is_color_format(internalFormat) && !colorFormat && !indexFormat) ||
1820       (_mesa_is_depth_format(internalFormat) != _mesa_is_depth_format(format)) ||
1821       (_mesa_is_ycbcr_format(internalFormat) != _mesa_is_ycbcr_format(format)) ||
1822       (_mesa_is_depthstencil_format(internalFormat) != _mesa_is_depthstencil_format(format)) ||
1823       (_mesa_is_dudv_format(internalFormat) != _mesa_is_dudv_format(format))) {
1824      _mesa_error(ctx, GL_INVALID_OPERATION,
1825                  "glTexImage%dD(incompatible internalFormat 0x%x, format 0x%x)",
1826                  dimensions, internalFormat, format);
1827      return GL_TRUE;
1828   }
1829
1830   /* additional checks for ycbcr textures */
1831   if (internalFormat == GL_YCBCR_MESA) {
1832      ASSERT(ctx->Extensions.MESA_ycbcr_texture);
1833      if (type != GL_UNSIGNED_SHORT_8_8_MESA &&
1834          type != GL_UNSIGNED_SHORT_8_8_REV_MESA) {
1835         char message[100];
1836         _mesa_snprintf(message, sizeof(message),
1837                        "glTexImage%dD(format/type YCBCR mismatch)",
1838                        dimensions);
1839         _mesa_error(ctx, GL_INVALID_ENUM, "%s", message);
1840         return GL_TRUE; /* error */
1841      }
1842      if (target != GL_TEXTURE_2D &&
1843          target != GL_PROXY_TEXTURE_2D &&
1844          target != GL_TEXTURE_RECTANGLE_NV &&
1845          target != GL_PROXY_TEXTURE_RECTANGLE_NV) {
1846         _mesa_error(ctx, GL_INVALID_ENUM,
1847                     "glTexImage%dD(bad target for YCbCr texture)",
1848                     dimensions);
1849         return GL_TRUE;
1850      }
1851      if (border != 0) {
1852         char message[100];
1853         _mesa_snprintf(message, sizeof(message),
1854                        "glTexImage%dD(format=GL_YCBCR_MESA and border=%d)",
1855                        dimensions, border);
1856         _mesa_error(ctx, GL_INVALID_VALUE, "%s", message);
1857         return GL_TRUE;
1858      }
1859   }
1860
1861   /* additional checks for depth textures */
1862   if (_mesa_base_tex_format(ctx, internalFormat) == GL_DEPTH_COMPONENT) {
1863      /* Only 1D, 2D, rect, array and cube textures supported, not 3D
1864       * Cubemaps are only supported for GL version > 3.0 or with EXT_gpu_shader4 */
1865      if (target != GL_TEXTURE_1D &&
1866          target != GL_PROXY_TEXTURE_1D &&
1867          target != GL_TEXTURE_2D &&
1868          target != GL_PROXY_TEXTURE_2D &&
1869          target != GL_TEXTURE_1D_ARRAY &&
1870          target != GL_PROXY_TEXTURE_1D_ARRAY &&
1871          target != GL_TEXTURE_2D_ARRAY &&
1872          target != GL_PROXY_TEXTURE_2D_ARRAY &&
1873          target != GL_TEXTURE_RECTANGLE_ARB &&
1874          target != GL_PROXY_TEXTURE_RECTANGLE_ARB &&
1875         !((_mesa_is_cube_face(target) || target == GL_PROXY_TEXTURE_CUBE_MAP) &&
1876           (ctx->Version >= 30 || ctx->Extensions.EXT_gpu_shader4))) {
1877         _mesa_error(ctx, GL_INVALID_ENUM,
1878                     "glTexImage%dD(bad target for depth texture)",
1879                     dimensions);
1880         return GL_TRUE;
1881      }
1882   }
1883
1884   /* additional checks for compressed textures */
1885   if (_mesa_is_compressed_format(ctx, internalFormat)) {
1886      if (!target_can_be_compressed(ctx, target, internalFormat)) {
1887         _mesa_error(ctx, GL_INVALID_ENUM,
1888                     "glTexImage%dD(target can't be compressed)", dimensions);
1889         return GL_TRUE;
1890      }
1891      if (compressedteximage_only_format(ctx, internalFormat)) {
1892         _mesa_error(ctx, GL_INVALID_OPERATION,
1893                     "glTexImage%dD(no compression for format)", dimensions);
1894         return GL_TRUE;
1895      }
1896      if (border != 0) {
1897         _mesa_error(ctx, GL_INVALID_OPERATION,
1898                     "glTexImage%dD(border!=0)", dimensions);
1899         return GL_TRUE;
1900      }
1901   }
1902
1903   /* additional checks for integer textures */
1904   if ((ctx->Version >= 30 || ctx->Extensions.EXT_texture_integer) &&
1905       (_mesa_is_enum_format_integer(format) !=
1906        _mesa_is_enum_format_integer(internalFormat))) {
1907      _mesa_error(ctx, GL_INVALID_OPERATION,
1908                  "glTexImage%dD(integer/non-integer format mismatch)",
1909                  dimensions);
1910      return GL_TRUE;
1911   }
1912
1913   if (!mutable_tex_object(ctx, target)) {
1914      _mesa_error(ctx, GL_INVALID_OPERATION,
1915                  "glTexImage%dD(immutable texture)", dimensions);
1916      return GL_TRUE;
1917   }
1918
1919   /* if we get here, the parameters are OK */
1920   return GL_FALSE;
1921}
1922
1923
1924/**
1925 * Error checking for glCompressedTexImage[123]D().
1926 * \param reason  returns reason for error, if any
1927 * \return error code or GL_NO_ERROR or PROXY_ERROR.
1928 */
1929static GLenum
1930compressed_texture_error_check(struct gl_context *ctx, GLint dimensions,
1931                               GLenum target, GLint level,
1932                               GLenum internalFormat, GLsizei width,
1933                               GLsizei height, GLsizei depth, GLint border,
1934                               GLsizei imageSize)
1935{
1936   const GLenum proxyTarget = _mesa_get_proxy_target(target);
1937   const GLint maxLevels = _mesa_max_texture_levels(ctx, target);
1938   GLint expectedSize;
1939   GLenum choose_format;
1940   GLenum choose_type;
1941   GLenum proxy_format;
1942   GLenum error = GL_NO_ERROR;
1943   char *reason = ""; /* no error */
1944
1945   if (!target_can_be_compressed(ctx, target, internalFormat)) {
1946      reason = "target";
1947      error = GL_INVALID_ENUM;
1948      goto error;
1949   }
1950
1951   /* This will detect any invalid internalFormat value */
1952   if (!_mesa_is_compressed_format(ctx, internalFormat)) {
1953      reason = "internalFormat";
1954      error = GL_INVALID_ENUM;
1955      goto error;
1956   }
1957
1958   switch (internalFormat) {
1959#if FEATURE_ES
1960   case GL_PALETTE4_RGB8_OES:
1961   case GL_PALETTE4_RGBA8_OES:
1962   case GL_PALETTE4_R5_G6_B5_OES:
1963   case GL_PALETTE4_RGBA4_OES:
1964   case GL_PALETTE4_RGB5_A1_OES:
1965   case GL_PALETTE8_RGB8_OES:
1966   case GL_PALETTE8_RGBA8_OES:
1967   case GL_PALETTE8_R5_G6_B5_OES:
1968   case GL_PALETTE8_RGBA4_OES:
1969   case GL_PALETTE8_RGB5_A1_OES:
1970      _mesa_cpal_compressed_format_type(internalFormat, &choose_format,
1971					&choose_type);
1972      proxy_format = choose_format;
1973
1974      /* check level (note that level should be zero or less!) */
1975      if (level > 0 || level < -maxLevels) {
1976	 reason = "level";
1977	 error = GL_INVALID_VALUE;
1978         goto error;
1979      }
1980
1981      if (dimensions != 2) {
1982	 reason = "compressed paletted textures must be 2D";
1983	 error = GL_INVALID_OPERATION;
1984         goto error;
1985      }
1986
1987      /* Figure out the expected texture size (in bytes).  This will be
1988       * checked against the actual size later.
1989       */
1990      expectedSize = _mesa_cpal_compressed_size(level, internalFormat,
1991						width, height);
1992
1993      /* This is for the benefit of the TestProxyTexImage below.  It expects
1994       * level to be non-negative.  OES_compressed_paletted_texture uses a
1995       * weird mechanism where the level specified to glCompressedTexImage2D
1996       * is -(n-1) number of levels in the texture, and the data specifies the
1997       * complete mipmap stack.  This is done to ensure the palette is the
1998       * same for all levels.
1999       */
2000      level = -level;
2001      break;
2002#endif
2003
2004   default:
2005      choose_format = GL_NONE;
2006      choose_type = GL_NONE;
2007      proxy_format = internalFormat;
2008
2009      /* check level */
2010      if (level < 0 || level >= maxLevels) {
2011	 reason = "level";
2012	 error = GL_INVALID_VALUE;
2013         goto error;
2014      }
2015
2016      /* Figure out the expected texture size (in bytes).  This will be
2017       * checked against the actual size later.
2018       */
2019      expectedSize = compressed_tex_size(width, height, depth, internalFormat);
2020      break;
2021   }
2022
2023   /* This should really never fail */
2024   if (_mesa_base_tex_format(ctx, internalFormat) < 0) {
2025      reason = "internalFormat";
2026      error = GL_INVALID_ENUM;
2027      goto error;
2028   }
2029
2030   /* No compressed formats support borders at this time */
2031   if (border != 0) {
2032      reason = "border != 0";
2033      error = GL_INVALID_VALUE;
2034      goto error;
2035   }
2036
2037   /* For cube map, width must equal height */
2038   if (_mesa_is_cube_face(target) && width != height) {
2039      reason = "width != height";
2040      error = GL_INVALID_VALUE;
2041      goto error;
2042   }
2043
2044   /* check image sizes */
2045   if (!ctx->Driver.TestProxyTexImage(ctx, proxyTarget, level,
2046				      proxy_format, choose_format,
2047				      choose_type,
2048				      width, height, depth, border)) {
2049      /* See error comment above */
2050      if (target == proxyTarget) {
2051         return PROXY_ERROR;
2052      }
2053      reason = "invalid width, height or format";
2054      error = GL_INVALID_OPERATION;
2055      goto error;
2056   }
2057
2058   /* check image size in bytes */
2059   if (expectedSize != imageSize) {
2060      /* Per GL_ARB_texture_compression:  GL_INVALID_VALUE is generated [...]
2061       * if <imageSize> is not consistent with the format, dimensions, and
2062       * contents of the specified image.
2063       */
2064      reason = "imageSize inconsistant with width/height/format";
2065      error = GL_INVALID_VALUE;
2066      goto error;
2067   }
2068
2069   if (!mutable_tex_object(ctx, target)) {
2070      reason = "immutable texture";
2071      error = GL_INVALID_OPERATION;
2072      goto error;
2073   }
2074
2075   return GL_NO_ERROR;
2076
2077error:
2078   _mesa_error(ctx, error, "glCompressedTexImage%dD(%s)", dimensions, reason);
2079   return error;
2080}
2081
2082
2083
2084/**
2085 * Test glTexSubImage[123]D() parameters for errors.
2086 *
2087 * \param ctx GL context.
2088 * \param dimensions texture image dimensions (must be 1, 2 or 3).
2089 * \param target texture target given by the user.
2090 * \param level image level given by the user.
2091 * \param xoffset sub-image x offset given by the user.
2092 * \param yoffset sub-image y offset given by the user.
2093 * \param zoffset sub-image z offset given by the user.
2094 * \param format pixel data format given by the user.
2095 * \param type pixel data type given by the user.
2096 * \param width image width given by the user.
2097 * \param height image height given by the user.
2098 * \param depth image depth given by the user.
2099 *
2100 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2101 *
2102 * Verifies each of the parameters against the constants specified in
2103 * __struct gl_contextRec::Const and the supported extensions, and according
2104 * to the OpenGL specification.
2105 */
2106static GLboolean
2107subtexture_error_check( struct gl_context *ctx, GLuint dimensions,
2108                        GLenum target, GLint level,
2109                        GLint xoffset, GLint yoffset, GLint zoffset,
2110                        GLint width, GLint height, GLint depth,
2111                        GLenum format, GLenum type )
2112{
2113   GLenum err;
2114
2115   /* Basic level check */
2116   if (level < 0 || level >= MAX_TEXTURE_LEVELS) {
2117      _mesa_error(ctx, GL_INVALID_ENUM, "glTexSubImage2D(level=%d)", level);
2118      return GL_TRUE;
2119   }
2120
2121   /* Check for negative sizes */
2122   if (width < 0) {
2123      _mesa_error(ctx, GL_INVALID_VALUE,
2124                  "glTexSubImage%dD(width=%d)", dimensions, width);
2125      return GL_TRUE;
2126   }
2127   if (height < 0 && dimensions > 1) {
2128      _mesa_error(ctx, GL_INVALID_VALUE,
2129                  "glTexSubImage%dD(height=%d)", dimensions, height);
2130      return GL_TRUE;
2131   }
2132   if (depth < 0 && dimensions > 2) {
2133      _mesa_error(ctx, GL_INVALID_VALUE,
2134                  "glTexSubImage%dD(depth=%d)", dimensions, depth);
2135      return GL_TRUE;
2136   }
2137
2138   /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
2139    * combinations of format and type that can be used.  Formats and types
2140    * that require additional extensions (e.g., GL_FLOAT requires
2141    * GL_OES_texture_float) are filtered elsewhere.
2142    */
2143   if (_mesa_is_gles(ctx) && !_mesa_is_gles3(ctx)) {
2144      err = _mesa_es_error_check_format_and_type(format, type, dimensions);
2145      if (err != GL_NO_ERROR) {
2146         _mesa_error(ctx, err,
2147                     "glTexSubImage%dD(format = %s, type = %s)",
2148                     dimensions,
2149                     _mesa_lookup_enum_by_nr(format),
2150                     _mesa_lookup_enum_by_nr(type));
2151         return GL_TRUE;
2152      }
2153   }
2154
2155   err = _mesa_error_check_format_and_type(ctx, format, type);
2156   if (err != GL_NO_ERROR) {
2157      _mesa_error(ctx, err,
2158                  "glTexSubImage%dD(incompatible format 0x%x, type 0x%x)",
2159                  dimensions, format, type);
2160      return GL_TRUE;
2161   }
2162
2163   return GL_FALSE;
2164}
2165
2166
2167/**
2168 * Do second part of glTexSubImage which depends on the destination texture.
2169 * \return GL_TRUE if error recorded, GL_FALSE otherwise
2170 */
2171static GLboolean
2172subtexture_error_check2( struct gl_context *ctx, GLuint dimensions,
2173			 GLenum target, GLint level,
2174			 GLint xoffset, GLint yoffset, GLint zoffset,
2175			 GLint width, GLint height, GLint depth,
2176			 GLenum format, GLenum type,
2177			 const struct gl_texture_image *destTex )
2178{
2179   if (!destTex) {
2180      /* undefined image level */
2181      _mesa_error(ctx, GL_INVALID_OPERATION, "glTexSubImage%dD", dimensions);
2182      return GL_TRUE;
2183   }
2184
2185   if (xoffset < -((GLint)destTex->Border)) {
2186      _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage%dD(xoffset)",
2187                  dimensions);
2188      return GL_TRUE;
2189   }
2190   if (xoffset + width > (GLint) (destTex->Width + destTex->Border)) {
2191      _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage%dD(xoffset+width)",
2192                  dimensions);
2193      return GL_TRUE;
2194   }
2195   if (dimensions > 1) {
2196      GLint yBorder = (target == GL_TEXTURE_1D_ARRAY) ? 0 : destTex->Border;
2197      if (yoffset < -yBorder) {
2198         _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage%dD(yoffset)",
2199                     dimensions);
2200         return GL_TRUE;
2201      }
2202      if (yoffset + height > (GLint) destTex->Height + yBorder) {
2203         _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage%dD(yoffset+height)",
2204                     dimensions);
2205         return GL_TRUE;
2206      }
2207   }
2208   if (dimensions > 2) {
2209      GLint zBorder = (target == GL_TEXTURE_2D_ARRAY) ? 0 : destTex->Border;
2210      if (zoffset < -zBorder) {
2211         _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage3D(zoffset)");
2212         return GL_TRUE;
2213      }
2214      if (zoffset + depth  > (GLint) destTex->Depth + zBorder) {
2215         _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage3D(zoffset+depth)");
2216         return GL_TRUE;
2217      }
2218   }
2219
2220   if (_mesa_is_format_compressed(destTex->TexFormat)) {
2221      GLuint bw, bh;
2222
2223      if (compressedteximage_only_format(ctx, destTex->InternalFormat)) {
2224         _mesa_error(ctx, GL_INVALID_OPERATION,
2225               "glTexSubImage%dD(no compression for format)", dimensions);
2226         return GL_TRUE;
2227      }
2228
2229      /* do tests which depend on compression block size */
2230      _mesa_get_format_block_size(destTex->TexFormat, &bw, &bh);
2231
2232      /* offset must be multiple of block size */
2233      if ((xoffset % bw != 0) || (yoffset % bh != 0)) {
2234         _mesa_error(ctx, GL_INVALID_OPERATION,
2235                     "glTexSubImage%dD(xoffset = %d, yoffset = %d)",
2236                     dimensions, xoffset, yoffset);
2237         return GL_TRUE;
2238      }
2239      /* size must be multiple of bw by bh or equal to whole texture size */
2240      if ((width % bw != 0) && (GLuint) width != destTex->Width) {
2241         _mesa_error(ctx, GL_INVALID_OPERATION,
2242                     "glTexSubImage%dD(width = %d)", dimensions, width);
2243         return GL_TRUE;
2244      }
2245      if ((height % bh != 0) && (GLuint) height != destTex->Height) {
2246         _mesa_error(ctx, GL_INVALID_OPERATION,
2247                     "glTexSubImage%dD(height = %d)", dimensions, height);
2248         return GL_TRUE;
2249      }
2250   }
2251
2252   if (ctx->Version >= 30 || ctx->Extensions.EXT_texture_integer) {
2253      /* both source and dest must be integer-valued, or neither */
2254      if (_mesa_is_format_integer_color(destTex->TexFormat) !=
2255          _mesa_is_enum_format_integer(format)) {
2256         _mesa_error(ctx, GL_INVALID_OPERATION,
2257                     "glTexSubImage%dD(integer/non-integer format mismatch)",
2258                     dimensions);
2259         return GL_TRUE;
2260      }
2261   }
2262
2263   return GL_FALSE;
2264}
2265
2266
2267/**
2268 * Test glCopyTexImage[12]D() parameters for errors.
2269 *
2270 * \param ctx GL context.
2271 * \param dimensions texture image dimensions (must be 1, 2 or 3).
2272 * \param target texture target given by the user.
2273 * \param level image level given by the user.
2274 * \param internalFormat internal format given by the user.
2275 * \param width image width given by the user.
2276 * \param height image height given by the user.
2277 * \param border texture border.
2278 *
2279 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2280 *
2281 * Verifies each of the parameters against the constants specified in
2282 * __struct gl_contextRec::Const and the supported extensions, and according
2283 * to the OpenGL specification.
2284 */
2285static GLboolean
2286copytexture_error_check( struct gl_context *ctx, GLuint dimensions,
2287                         GLenum target, GLint level, GLint internalFormat,
2288                         GLint width, GLint height, GLint border )
2289{
2290   const GLenum proxyTarget = _mesa_get_proxy_target(target);
2291   const GLenum type = GL_FLOAT;
2292   GLboolean sizeOK;
2293   GLint baseFormat;
2294
2295   /* check target */
2296   if (!legal_texsubimage_target(ctx, dimensions, target)) {
2297      _mesa_error(ctx, GL_INVALID_ENUM, "glCopyTexImage%uD(target=%s)",
2298                  dimensions, _mesa_lookup_enum_by_nr(target));
2299      return GL_TRUE;
2300   }
2301
2302   /* Basic level check (more checking in ctx->Driver.TestProxyTexImage) */
2303   if (level < 0 || level >= MAX_TEXTURE_LEVELS) {
2304      _mesa_error(ctx, GL_INVALID_VALUE,
2305                  "glCopyTexImage%dD(level=%d)", dimensions, level);
2306      return GL_TRUE;
2307   }
2308
2309   /* Check that the source buffer is complete */
2310   if (_mesa_is_user_fbo(ctx->ReadBuffer)) {
2311      if (ctx->ReadBuffer->_Status == 0) {
2312         _mesa_test_framebuffer_completeness(ctx, ctx->ReadBuffer);
2313      }
2314      if (ctx->ReadBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
2315         _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
2316                     "glCopyTexImage%dD(invalid readbuffer)", dimensions);
2317         return GL_TRUE;
2318      }
2319
2320      if (ctx->ReadBuffer->Visual.samples > 0) {
2321	 _mesa_error(ctx, GL_INVALID_OPERATION,
2322		     "glCopyTexImage%dD(multisample FBO)",
2323		     dimensions);
2324	 return GL_TRUE;
2325      }
2326   }
2327
2328   /* Check border */
2329   if (border < 0 || border > 1 ||
2330       ((ctx->API != API_OPENGL ||
2331         target == GL_TEXTURE_RECTANGLE_NV ||
2332         target == GL_PROXY_TEXTURE_RECTANGLE_NV) && border != 0)) {
2333      _mesa_error(ctx, GL_INVALID_VALUE,
2334                  "glCopyTexImage%dD(border=%d)", dimensions, border);
2335      return GL_TRUE;
2336   }
2337
2338   /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
2339    * internalFormat.
2340    */
2341   if (_mesa_is_gles(ctx) && !_mesa_is_gles3(ctx)) {
2342      switch (internalFormat) {
2343      case GL_ALPHA:
2344      case GL_RGB:
2345      case GL_RGBA:
2346      case GL_LUMINANCE:
2347      case GL_LUMINANCE_ALPHA:
2348         break;
2349      default:
2350         _mesa_error(ctx, GL_INVALID_VALUE,
2351                     "glCopyTexImage%dD(internalFormat)", dimensions);
2352         return GL_TRUE;
2353      }
2354   }
2355
2356   baseFormat = _mesa_base_tex_format(ctx, internalFormat);
2357   if (baseFormat < 0) {
2358      _mesa_error(ctx, GL_INVALID_VALUE,
2359                  "glCopyTexImage%dD(internalFormat)", dimensions);
2360      return GL_TRUE;
2361   }
2362
2363   if (!_mesa_source_buffer_exists(ctx, baseFormat)) {
2364      _mesa_error(ctx, GL_INVALID_OPERATION,
2365                  "glCopyTexImage%dD(missing readbuffer)", dimensions);
2366      return GL_TRUE;
2367   }
2368
2369   /* From the EXT_texture_integer spec:
2370    *
2371    *     "INVALID_OPERATION is generated by CopyTexImage* and CopyTexSubImage*
2372    *      if the texture internalformat is an integer format and the read color
2373    *      buffer is not an integer format, or if the internalformat is not an
2374    *      integer format and the read color buffer is an integer format."
2375    */
2376   if (_mesa_is_color_format(internalFormat)) {
2377      struct gl_renderbuffer *rb = ctx->ReadBuffer->_ColorReadBuffer;
2378
2379      if (_mesa_is_enum_format_integer(rb->InternalFormat) !=
2380	  _mesa_is_enum_format_integer(internalFormat)) {
2381	 _mesa_error(ctx, GL_INVALID_OPERATION,
2382		     "glCopyTexImage%dD(integer vs non-integer)", dimensions);
2383	 return GL_TRUE;
2384      }
2385   }
2386
2387   /* Do size, level checking */
2388   sizeOK = (proxyTarget == GL_PROXY_TEXTURE_CUBE_MAP_ARB)
2389      ? (width == height) : 1;
2390
2391   sizeOK = sizeOK && ctx->Driver.TestProxyTexImage(ctx, proxyTarget, level,
2392                                                    internalFormat, baseFormat,
2393                                                    type, width, height,
2394                                                    1, border);
2395
2396   if (!sizeOK) {
2397      if (dimensions == 1) {
2398         _mesa_error(ctx, GL_INVALID_VALUE,
2399                     "glCopyTexImage1D(width=%d)", width);
2400      }
2401      else {
2402         ASSERT(dimensions == 2);
2403         _mesa_error(ctx, GL_INVALID_VALUE,
2404                     "glCopyTexImage2D(width=%d, height=%d)", width, height);
2405      }
2406      return GL_TRUE;
2407   }
2408
2409   if (_mesa_is_compressed_format(ctx, internalFormat)) {
2410      if (!target_can_be_compressed(ctx, target, internalFormat)) {
2411         _mesa_error(ctx, GL_INVALID_ENUM,
2412                     "glCopyTexImage%dD(target)", dimensions);
2413         return GL_TRUE;
2414      }
2415      if (compressedteximage_only_format(ctx, internalFormat)) {
2416         _mesa_error(ctx, GL_INVALID_OPERATION,
2417               "glCopyTexImage%dD(no compression for format)", dimensions);
2418         return GL_TRUE;
2419      }
2420      if (border != 0) {
2421         _mesa_error(ctx, GL_INVALID_OPERATION,
2422                     "glCopyTexImage%dD(border!=0)", dimensions);
2423         return GL_TRUE;
2424      }
2425   }
2426
2427   if (!mutable_tex_object(ctx, target)) {
2428      _mesa_error(ctx, GL_INVALID_OPERATION,
2429                  "glCopyTexImage%dD(immutable texture)", dimensions);
2430      return GL_TRUE;
2431   }
2432
2433   /* if we get here, the parameters are OK */
2434   return GL_FALSE;
2435}
2436
2437
2438/**
2439 * Test glCopyTexSubImage[12]D() parameters for errors.
2440 * Note that this is the first part of error checking.
2441 * See also copytexsubimage_error_check2() below for the second part.
2442 *
2443 * \param ctx GL context.
2444 * \param dimensions texture image dimensions (must be 1, 2 or 3).
2445 * \param target texture target given by the user.
2446 * \param level image level given by the user.
2447 *
2448 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2449 */
2450static GLboolean
2451copytexsubimage_error_check1( struct gl_context *ctx, GLuint dimensions,
2452                              GLenum target, GLint level)
2453{
2454   /* Check that the source buffer is complete */
2455   if (_mesa_is_user_fbo(ctx->ReadBuffer)) {
2456      if (ctx->ReadBuffer->_Status == 0) {
2457         _mesa_test_framebuffer_completeness(ctx, ctx->ReadBuffer);
2458      }
2459      if (ctx->ReadBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
2460         _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
2461                     "glCopyTexImage%dD(invalid readbuffer)", dimensions);
2462         return GL_TRUE;
2463      }
2464
2465      if (ctx->ReadBuffer->Visual.samples > 0) {
2466	 _mesa_error(ctx, GL_INVALID_OPERATION,
2467		     "glCopyTexSubImage%dD(multisample FBO)",
2468		     dimensions);
2469	 return GL_TRUE;
2470      }
2471   }
2472
2473   /* check target (proxies not allowed) */
2474   if (!legal_texsubimage_target(ctx, dimensions, target)) {
2475      _mesa_error(ctx, GL_INVALID_ENUM, "glCopyTexSubImage%uD(target=%s)",
2476                  dimensions, _mesa_lookup_enum_by_nr(target));
2477      return GL_TRUE;
2478   }
2479
2480   /* Check level */
2481   if (level < 0 || level >= MAX_TEXTURE_LEVELS) {
2482      _mesa_error(ctx, GL_INVALID_VALUE,
2483                  "glCopyTexSubImage%dD(level=%d)", dimensions, level);
2484      return GL_TRUE;
2485   }
2486
2487   return GL_FALSE;
2488}
2489
2490
2491/**
2492 * Second part of error checking for glCopyTexSubImage[12]D().
2493 * \param xoffset sub-image x offset given by the user.
2494 * \param yoffset sub-image y offset given by the user.
2495 * \param zoffset sub-image z offset given by the user.
2496 * \param width image width given by the user.
2497 * \param height image height given by the user.
2498 */
2499static GLboolean
2500copytexsubimage_error_check2( struct gl_context *ctx, GLuint dimensions,
2501			      GLenum target, GLint level,
2502			      GLint xoffset, GLint yoffset, GLint zoffset,
2503			      GLsizei width, GLsizei height,
2504			      const struct gl_texture_image *teximage )
2505{
2506   /* check that dest tex image exists */
2507   if (!teximage) {
2508      _mesa_error(ctx, GL_INVALID_OPERATION,
2509                  "glCopyTexSubImage%dD(undefined texture level: %d)",
2510                  dimensions, level);
2511      return GL_TRUE;
2512   }
2513
2514   /* Check size */
2515   if (width < 0) {
2516      _mesa_error(ctx, GL_INVALID_VALUE,
2517                  "glCopyTexSubImage%dD(width=%d)", dimensions, width);
2518      return GL_TRUE;
2519   }
2520   if (dimensions > 1 && height < 0) {
2521      _mesa_error(ctx, GL_INVALID_VALUE,
2522                  "glCopyTexSubImage%dD(height=%d)", dimensions, height);
2523      return GL_TRUE;
2524   }
2525
2526   /* check x/y offsets */
2527   if (xoffset < -((GLint)teximage->Border)) {
2528      _mesa_error(ctx, GL_INVALID_VALUE,
2529                  "glCopyTexSubImage%dD(xoffset=%d)", dimensions, xoffset);
2530      return GL_TRUE;
2531   }
2532   if (xoffset + width > (GLint) (teximage->Width + teximage->Border)) {
2533      _mesa_error(ctx, GL_INVALID_VALUE,
2534                  "glCopyTexSubImage%dD(xoffset+width)", dimensions);
2535      return GL_TRUE;
2536   }
2537   if (dimensions > 1) {
2538      GLint yBorder = (target == GL_TEXTURE_1D_ARRAY) ? 0 : teximage->Border;
2539      if (yoffset < -yBorder) {
2540         _mesa_error(ctx, GL_INVALID_VALUE,
2541                     "glCopyTexSubImage%dD(yoffset=%d)", dimensions, yoffset);
2542         return GL_TRUE;
2543      }
2544      /* NOTE: we're adding the border here, not subtracting! */
2545      if (yoffset + height > (GLint) teximage->Height + yBorder) {
2546         _mesa_error(ctx, GL_INVALID_VALUE,
2547                     "glCopyTexSubImage%dD(yoffset+height)", dimensions);
2548         return GL_TRUE;
2549      }
2550   }
2551
2552   /* check z offset */
2553   if (dimensions > 2) {
2554      GLint zBorder = (target == GL_TEXTURE_2D_ARRAY) ? 0 : teximage->Border;
2555      if (zoffset < -zBorder) {
2556         _mesa_error(ctx, GL_INVALID_VALUE,
2557                     "glCopyTexSubImage%dD(zoffset)", dimensions);
2558         return GL_TRUE;
2559      }
2560      if (zoffset > (GLint) teximage->Depth + zBorder) {
2561         _mesa_error(ctx, GL_INVALID_VALUE,
2562                     "glCopyTexSubImage%dD(zoffset+depth)", dimensions);
2563         return GL_TRUE;
2564      }
2565   }
2566
2567   if (_mesa_is_format_compressed(teximage->TexFormat)) {
2568      if (compressedteximage_only_format(ctx, teximage->InternalFormat)) {
2569         _mesa_error(ctx, GL_INVALID_OPERATION,
2570               "glCopyTexSubImage%dD(no compression for format)", dimensions);
2571         return GL_TRUE;
2572      }
2573      /* offset must be multiple of 4 */
2574      if ((xoffset & 3) || (yoffset & 3)) {
2575         _mesa_error(ctx, GL_INVALID_VALUE,
2576                     "glCopyTexSubImage%dD(xoffset or yoffset)", dimensions);
2577         return GL_TRUE;
2578      }
2579      /* size must be multiple of 4 */
2580      if ((width & 3) != 0 && (GLuint) width != teximage->Width) {
2581         _mesa_error(ctx, GL_INVALID_VALUE,
2582                     "glCopyTexSubImage%dD(width)", dimensions);
2583         return GL_TRUE;
2584      }
2585      if ((height & 3) != 0 && (GLuint) height != teximage->Height) {
2586         _mesa_error(ctx, GL_INVALID_VALUE,
2587                     "glCopyTexSubImage%dD(height)", dimensions);
2588         return GL_TRUE;
2589      }
2590   }
2591
2592   if (teximage->InternalFormat == GL_YCBCR_MESA) {
2593      _mesa_error(ctx, GL_INVALID_OPERATION, "glCopyTexSubImage2D");
2594      return GL_TRUE;
2595   }
2596
2597   if (!_mesa_source_buffer_exists(ctx, teximage->_BaseFormat)) {
2598      _mesa_error(ctx, GL_INVALID_OPERATION,
2599                  "glCopyTexSubImage%dD(missing readbuffer, format=0x%x)",
2600                  dimensions, teximage->_BaseFormat);
2601      return GL_TRUE;
2602   }
2603
2604   /* From the EXT_texture_integer spec:
2605    *
2606    *     "INVALID_OPERATION is generated by CopyTexImage* and CopyTexSubImage*
2607    *      if the texture internalformat is an integer format and the read color
2608    *      buffer is not an integer format, or if the internalformat is not an
2609    *      integer format and the read color buffer is an integer format."
2610    */
2611   if (_mesa_is_color_format(teximage->InternalFormat)) {
2612      struct gl_renderbuffer *rb = ctx->ReadBuffer->_ColorReadBuffer;
2613
2614      if (_mesa_is_format_integer_color(rb->Format) !=
2615	  _mesa_is_format_integer_color(teximage->TexFormat)) {
2616	 _mesa_error(ctx, GL_INVALID_OPERATION,
2617		     "glCopyTexImage%dD(integer vs non-integer)", dimensions);
2618	 return GL_TRUE;
2619      }
2620   }
2621
2622   /* if we get here, the parameters are OK */
2623   return GL_FALSE;
2624}
2625
2626
2627/** Callback info for walking over FBO hash table */
2628struct cb_info
2629{
2630   struct gl_context *ctx;
2631   struct gl_texture_object *texObj;
2632   GLuint level, face;
2633};
2634
2635
2636/**
2637 * Check render to texture callback.  Called from _mesa_HashWalk().
2638 */
2639static void
2640check_rtt_cb(GLuint key, void *data, void *userData)
2641{
2642   struct gl_framebuffer *fb = (struct gl_framebuffer *) data;
2643   const struct cb_info *info = (struct cb_info *) userData;
2644   struct gl_context *ctx = info->ctx;
2645   const struct gl_texture_object *texObj = info->texObj;
2646   const GLuint level = info->level, face = info->face;
2647
2648   /* If this is a user-created FBO */
2649   if (_mesa_is_user_fbo(fb)) {
2650      GLuint i;
2651      /* check if any of the FBO's attachments point to 'texObj' */
2652      for (i = 0; i < BUFFER_COUNT; i++) {
2653         struct gl_renderbuffer_attachment *att = fb->Attachment + i;
2654         if (att->Type == GL_TEXTURE &&
2655             att->Texture == texObj &&
2656             att->TextureLevel == level &&
2657             att->CubeMapFace == face) {
2658            ASSERT(_mesa_get_attachment_teximage(att));
2659            /* Tell driver about the new renderbuffer texture */
2660            ctx->Driver.RenderTexture(ctx, ctx->DrawBuffer, att);
2661            /* Mark fb status as indeterminate to force re-validation */
2662            fb->_Status = 0;
2663         }
2664      }
2665   }
2666}
2667
2668
2669/**
2670 * When a texture image is specified we have to check if it's bound to
2671 * any framebuffer objects (render to texture) in order to detect changes
2672 * in size or format since that effects FBO completeness.
2673 * Any FBOs rendering into the texture must be re-validated.
2674 */
2675void
2676_mesa_update_fbo_texture(struct gl_context *ctx,
2677                         struct gl_texture_object *texObj,
2678                         GLuint face, GLuint level)
2679{
2680   /* Only check this texture if it's been marked as RenderToTexture */
2681   if (texObj->_RenderToTexture) {
2682      struct cb_info info;
2683      info.ctx = ctx;
2684      info.texObj = texObj;
2685      info.level = level;
2686      info.face = face;
2687      _mesa_HashWalk(ctx->Shared->FrameBuffers, check_rtt_cb, &info);
2688   }
2689}
2690
2691
2692/**
2693 * If the texture object's GenerateMipmap flag is set and we've
2694 * changed the texture base level image, regenerate the rest of the
2695 * mipmap levels now.
2696 */
2697static inline void
2698check_gen_mipmap(struct gl_context *ctx, GLenum target,
2699                 struct gl_texture_object *texObj, GLint level)
2700{
2701   ASSERT(target != GL_TEXTURE_CUBE_MAP);
2702   if (texObj->GenerateMipmap &&
2703       level == texObj->BaseLevel &&
2704       level < texObj->MaxLevel) {
2705      ASSERT(ctx->Driver.GenerateMipmap);
2706      ctx->Driver.GenerateMipmap(ctx, target, texObj);
2707   }
2708}
2709
2710
2711/** Debug helper: override the user-requested internal format */
2712static GLenum
2713override_internal_format(GLenum internalFormat, GLint width, GLint height)
2714{
2715#if 0
2716   if (internalFormat == GL_RGBA16F_ARB ||
2717       internalFormat == GL_RGBA32F_ARB) {
2718      printf("Convert rgba float tex to int %d x %d\n", width, height);
2719      return GL_RGBA;
2720   }
2721   else if (internalFormat == GL_RGB16F_ARB ||
2722            internalFormat == GL_RGB32F_ARB) {
2723      printf("Convert rgb float tex to int %d x %d\n", width, height);
2724      return GL_RGB;
2725   }
2726   else if (internalFormat == GL_LUMINANCE_ALPHA16F_ARB ||
2727            internalFormat == GL_LUMINANCE_ALPHA32F_ARB) {
2728      printf("Convert luminance float tex to int %d x %d\n", width, height);
2729      return GL_LUMINANCE_ALPHA;
2730   }
2731   else if (internalFormat == GL_LUMINANCE16F_ARB ||
2732            internalFormat == GL_LUMINANCE32F_ARB) {
2733      printf("Convert luminance float tex to int %d x %d\n", width, height);
2734      return GL_LUMINANCE;
2735   }
2736   else if (internalFormat == GL_ALPHA16F_ARB ||
2737            internalFormat == GL_ALPHA32F_ARB) {
2738      printf("Convert luminance float tex to int %d x %d\n", width, height);
2739      return GL_ALPHA;
2740   }
2741   /*
2742   else if (internalFormat == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT) {
2743      internalFormat = GL_RGBA;
2744   }
2745   */
2746   else {
2747      return internalFormat;
2748   }
2749#else
2750   return internalFormat;
2751#endif
2752}
2753
2754
2755/**
2756 * Choose the actual hardware format for a texture image.
2757 * Try to use the same format as the previous image level when possible.
2758 * Otherwise, ask the driver for the best format.
2759 * It's important to try to choose a consistant format for all levels
2760 * for efficient texture memory layout/allocation.  In particular, this
2761 * comes up during automatic mipmap generation.
2762 */
2763gl_format
2764_mesa_choose_texture_format(struct gl_context *ctx,
2765                            struct gl_texture_object *texObj,
2766                            GLenum target, GLint level,
2767                            GLenum internalFormat, GLenum format, GLenum type)
2768{
2769   gl_format f;
2770
2771   /* see if we've already chosen a format for the previous level */
2772   if (level > 0) {
2773      struct gl_texture_image *prevImage =
2774	 _mesa_select_tex_image(ctx, texObj, target, level - 1);
2775      /* See if the prev level is defined and has an internal format which
2776       * matches the new internal format.
2777       */
2778      if (prevImage &&
2779          prevImage->Width > 0 &&
2780          prevImage->InternalFormat == internalFormat) {
2781         /* use the same format */
2782         ASSERT(prevImage->TexFormat != MESA_FORMAT_NONE);
2783         return prevImage->TexFormat;
2784      }
2785   }
2786
2787   /* If the application requested compression to an S3TC format but we don't
2788    * have the DTXn library, force a generic compressed format instead.
2789    */
2790   if (internalFormat != format && format != GL_NONE) {
2791      const GLenum before = internalFormat;
2792
2793      switch (internalFormat) {
2794      case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
2795         if (!ctx->Mesa_DXTn)
2796            internalFormat = GL_COMPRESSED_RGB;
2797         break;
2798      case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
2799      case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
2800      case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
2801         if (!ctx->Mesa_DXTn)
2802            internalFormat = GL_COMPRESSED_RGBA;
2803         break;
2804      default:
2805         break;
2806      }
2807
2808      if (before != internalFormat) {
2809         _mesa_warning(ctx,
2810                       "DXT compression requested (%s), "
2811                       "but libtxc_dxtn library not installed.  Using %s "
2812                       "instead.",
2813                       _mesa_lookup_enum_by_nr(before),
2814                       _mesa_lookup_enum_by_nr(internalFormat));
2815      }
2816   }
2817
2818   /* choose format from scratch */
2819   f = ctx->Driver.ChooseTextureFormat(ctx, texObj->Target, internalFormat,
2820                                       format, type);
2821   ASSERT(f != MESA_FORMAT_NONE);
2822   return f;
2823}
2824
2825/**
2826 * Adjust pixel unpack params and image dimensions to strip off the
2827 * one-pixel texture border.
2828 *
2829 * Gallium and intel don't support texture borders.  They've seldem been used
2830 * and seldom been implemented correctly anyway.
2831 *
2832 * \param unpackNew returns the new pixel unpack parameters
2833 */
2834static void
2835strip_texture_border(GLenum target,
2836                     GLint *width, GLint *height, GLint *depth,
2837                     const struct gl_pixelstore_attrib *unpack,
2838                     struct gl_pixelstore_attrib *unpackNew)
2839{
2840   assert(width);
2841   assert(height);
2842   assert(depth);
2843
2844   *unpackNew = *unpack;
2845
2846   if (unpackNew->RowLength == 0)
2847      unpackNew->RowLength = *width;
2848
2849   if (unpackNew->ImageHeight == 0)
2850      unpackNew->ImageHeight = *height;
2851
2852   assert(*width >= 3);
2853   unpackNew->SkipPixels++;  /* skip the border */
2854   *width = *width - 2;      /* reduce the width by two border pixels */
2855
2856   /* The min height of a texture with a border is 3 */
2857   if (*height >= 3 && target != GL_TEXTURE_1D_ARRAY) {
2858      unpackNew->SkipRows++;  /* skip the border */
2859      *height = *height - 2;  /* reduce the height by two border pixels */
2860   }
2861
2862   if (*depth >= 3 && target != GL_TEXTURE_2D_ARRAY) {
2863      unpackNew->SkipImages++;  /* skip the border */
2864      *depth = *depth - 2;      /* reduce the depth by two border pixels */
2865   }
2866}
2867
2868
2869/**
2870 * Common code to implement all the glTexImage1D/2D/3D functions
2871 * as well as glCompressedTexImage1D/2D/3D.
2872 * \param compressed  only GL_TRUE for glCompressedTexImage1D/2D/3D calls.
2873 * \param format  the user's image format (only used if !compressed)
2874 * \param type  the user's image type (only used if !compressed)
2875 * \param imageSize  only used for glCompressedTexImage1D/2D/3D calls.
2876 */
2877static void
2878teximage(struct gl_context *ctx, GLboolean compressed, GLuint dims,
2879         GLenum target, GLint level, GLint internalFormat,
2880         GLsizei width, GLsizei height, GLsizei depth,
2881         GLint border, GLenum format, GLenum type,
2882         GLsizei imageSize, const GLvoid *pixels)
2883{
2884   const char *func = compressed ? "glCompressedTexImage" : "glTexImage";
2885   GLenum error;
2886   struct gl_pixelstore_attrib unpack_no_border;
2887   const struct gl_pixelstore_attrib *unpack = &ctx->Unpack;
2888
2889   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2890
2891   if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE)) {
2892      if (compressed)
2893         _mesa_debug(ctx,
2894                     "glCompressedTexImage%uD %s %d %s %d %d %d %d %p\n",
2895                     dims,
2896                     _mesa_lookup_enum_by_nr(target), level,
2897                     _mesa_lookup_enum_by_nr(internalFormat),
2898                     width, height, depth, border, pixels);
2899      else
2900         _mesa_debug(ctx,
2901                     "glTexImage%uD %s %d %s %d %d %d %d %s %s %p\n",
2902                     dims,
2903                     _mesa_lookup_enum_by_nr(target), level,
2904                     _mesa_lookup_enum_by_nr(internalFormat),
2905                     width, height, depth, border,
2906                     _mesa_lookup_enum_by_nr(format),
2907                     _mesa_lookup_enum_by_nr(type), pixels);
2908   }
2909
2910   internalFormat = override_internal_format(internalFormat, width, height);
2911
2912   /* target error checking */
2913   if (!legal_teximage_target(ctx, dims, target)) {
2914      _mesa_error(ctx, GL_INVALID_ENUM, "%s%uD(target=%s)",
2915                  func, dims, _mesa_lookup_enum_by_nr(target));
2916      return;
2917   }
2918
2919   /* general error checking */
2920   if (compressed) {
2921      error = compressed_texture_error_check(ctx, dims, target, level,
2922                                             internalFormat,
2923                                             width, height, depth,
2924                                             border, imageSize);
2925   }
2926   else {
2927      error = texture_error_check(ctx, dims, target, level, internalFormat,
2928                                  format, type, width, height, depth, border);
2929   }
2930
2931#if FEATURE_ES
2932   /* Here we convert a cpal compressed image into a regular glTexImage2D
2933    * call by decompressing the texture.  If we really want to support cpal
2934    * textures in any driver this would have to be changed.
2935    */
2936   if (compressed && !error && dims == 2) {
2937      switch (internalFormat) {
2938      case GL_PALETTE4_RGB8_OES:
2939      case GL_PALETTE4_RGBA8_OES:
2940      case GL_PALETTE4_R5_G6_B5_OES:
2941      case GL_PALETTE4_RGBA4_OES:
2942      case GL_PALETTE4_RGB5_A1_OES:
2943      case GL_PALETTE8_RGB8_OES:
2944      case GL_PALETTE8_RGBA8_OES:
2945      case GL_PALETTE8_R5_G6_B5_OES:
2946      case GL_PALETTE8_RGBA4_OES:
2947      case GL_PALETTE8_RGB5_A1_OES:
2948         _mesa_cpal_compressed_teximage2d(target, level, internalFormat,
2949                                          width, height, imageSize, pixels);
2950         return;
2951      }
2952   }
2953#endif
2954
2955   if (_mesa_is_proxy_texture(target)) {
2956      /* Proxy texture: just clear or set state depending on error checking */
2957      struct gl_texture_image *texImage =
2958         get_proxy_tex_image(ctx, target, level);
2959      gl_format texFormat = MESA_FORMAT_NONE;
2960
2961      if (!error) {
2962         /* No parameter errors.  Choose a texture format and see if we
2963          * can really allocate the texture.
2964          */
2965         struct gl_texture_object *texObj =
2966            _mesa_get_current_tex_object(ctx, target);
2967         texFormat = _mesa_choose_texture_format(ctx, texObj, target, level,
2968                                                 internalFormat, format, type);
2969         if (!legal_texture_size(ctx, texFormat, width, height, depth)) {
2970            error = PROXY_ERROR;
2971         }
2972      }
2973
2974      if (error == PROXY_ERROR) {
2975         /* image too large, etc.  Clear all proxy texture image parameters. */
2976         if (texImage)
2977            clear_teximage_fields(texImage);
2978      }
2979      else if (error == GL_FALSE) {
2980         /* no error: store the teximage parameters */
2981         if (texImage)
2982            _mesa_init_teximage_fields(ctx, texImage, width, height, depth,
2983                                       border, internalFormat, texFormat);
2984      }
2985      else {
2986         /* other, regular error (was already recorded) */
2987      }
2988   }
2989   else {
2990      /* non-proxy target */
2991      const GLuint face = _mesa_tex_target_to_face(target);
2992      struct gl_texture_object *texObj;
2993      struct gl_texture_image *texImage;
2994
2995      if (error) {
2996         return;   /* error was recorded */
2997      }
2998
2999      /* Allow a hardware driver to just strip out the border, to provide
3000       * reliable but slightly incorrect hardware rendering instead of
3001       * rarely-tested software fallback rendering.
3002       */
3003      if (border && ctx->Const.StripTextureBorder) {
3004	 strip_texture_border(target, &width, &height, &depth, unpack,
3005			      &unpack_no_border);
3006         border = 0;
3007	 unpack = &unpack_no_border;
3008      }
3009
3010      if (ctx->NewState & _NEW_PIXEL)
3011	 _mesa_update_state(ctx);
3012
3013      texObj = _mesa_get_current_tex_object(ctx, target);
3014
3015      _mesa_lock_texture(ctx, texObj);
3016      {
3017	 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
3018
3019	 if (!texImage) {
3020	    _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s%uD", func, dims);
3021	 }
3022         else {
3023            gl_format texFormat;
3024
3025            ctx->Driver.FreeTextureImageBuffer(ctx, texImage);
3026
3027            texFormat = _mesa_choose_texture_format(ctx, texObj, target, level,
3028                                                    internalFormat, format,
3029                                                    type);
3030
3031            if (legal_texture_size(ctx, texFormat, width, height, depth)) {
3032               _mesa_init_teximage_fields(ctx, texImage,
3033                                          width, height, depth,
3034                                          border, internalFormat, texFormat);
3035
3036               /* Give the texture to the driver.  <pixels> may be null. */
3037               if (width > 0 && height > 0 && depth > 0) {
3038                  if (compressed) {
3039                     ctx->Driver.CompressedTexImage(ctx, dims, texImage,
3040                                                    imageSize, pixels);
3041                  }
3042                  else {
3043                     ctx->Driver.TexImage(ctx, dims, texImage, format,
3044                                          type, pixels, unpack);
3045                  }
3046               }
3047
3048               check_gen_mipmap(ctx, target, texObj, level);
3049
3050               _mesa_update_fbo_texture(ctx, texObj, face, level);
3051
3052               _mesa_dirty_texobj(ctx, texObj, GL_TRUE);
3053            }
3054            else {
3055               _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s%uD", func, dims);
3056            }
3057         }
3058      }
3059      _mesa_unlock_texture(ctx, texObj);
3060   }
3061}
3062
3063
3064/*
3065 * Called from the API.  Note that width includes the border.
3066 */
3067void GLAPIENTRY
3068_mesa_TexImage1D( GLenum target, GLint level, GLint internalFormat,
3069                  GLsizei width, GLint border, GLenum format,
3070                  GLenum type, const GLvoid *pixels )
3071{
3072   GET_CURRENT_CONTEXT(ctx);
3073   teximage(ctx, GL_FALSE, 1, target, level, internalFormat, width, 1, 1,
3074            border, format, type, 0, pixels);
3075}
3076
3077
3078void GLAPIENTRY
3079_mesa_TexImage2D( GLenum target, GLint level, GLint internalFormat,
3080                  GLsizei width, GLsizei height, GLint border,
3081                  GLenum format, GLenum type,
3082                  const GLvoid *pixels )
3083{
3084   GET_CURRENT_CONTEXT(ctx);
3085   teximage(ctx, GL_FALSE, 2, target, level, internalFormat, width, height, 1,
3086            border, format, type, 0, pixels);
3087}
3088
3089
3090/*
3091 * Called by the API or display list executor.
3092 * Note that width and height include the border.
3093 */
3094void GLAPIENTRY
3095_mesa_TexImage3D( GLenum target, GLint level, GLint internalFormat,
3096                  GLsizei width, GLsizei height, GLsizei depth,
3097                  GLint border, GLenum format, GLenum type,
3098                  const GLvoid *pixels )
3099{
3100   GET_CURRENT_CONTEXT(ctx);
3101   teximage(ctx, GL_FALSE, 3, target, level, internalFormat,
3102            width, height, depth,
3103            border, format, type, 0, pixels);
3104}
3105
3106
3107void GLAPIENTRY
3108_mesa_TexImage3DEXT( GLenum target, GLint level, GLenum internalFormat,
3109                     GLsizei width, GLsizei height, GLsizei depth,
3110                     GLint border, GLenum format, GLenum type,
3111                     const GLvoid *pixels )
3112{
3113   _mesa_TexImage3D(target, level, (GLint) internalFormat, width, height,
3114                    depth, border, format, type, pixels);
3115}
3116
3117
3118#if FEATURE_OES_EGL_image
3119void GLAPIENTRY
3120_mesa_EGLImageTargetTexture2DOES (GLenum target, GLeglImageOES image)
3121{
3122   struct gl_texture_object *texObj;
3123   struct gl_texture_image *texImage;
3124   bool valid_target;
3125   GET_CURRENT_CONTEXT(ctx);
3126   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3127
3128   switch (target) {
3129   case GL_TEXTURE_2D:
3130      valid_target = ctx->Extensions.OES_EGL_image;
3131      break;
3132   case GL_TEXTURE_EXTERNAL_OES:
3133      valid_target = ctx->Extensions.OES_EGL_image_external;
3134      break;
3135   default:
3136      valid_target = false;
3137      break;
3138   }
3139
3140   if (!valid_target) {
3141      _mesa_error(ctx, GL_INVALID_ENUM,
3142		  "glEGLImageTargetTexture2D(target=%d)", target);
3143      return;
3144   }
3145
3146   if (ctx->NewState & _NEW_PIXEL)
3147      _mesa_update_state(ctx);
3148
3149   texObj = _mesa_get_current_tex_object(ctx, target);
3150   _mesa_lock_texture(ctx, texObj);
3151
3152   if (texObj->Immutable) {
3153      _mesa_error(ctx, GL_INVALID_OPERATION,
3154		  "glEGLImageTargetTexture2D(texture is immutable)");
3155      _mesa_unlock_texture(ctx, texObj);
3156      return;
3157   }
3158
3159   texImage = _mesa_get_tex_image(ctx, texObj, target, 0);
3160   if (!texImage) {
3161      _mesa_error(ctx, GL_OUT_OF_MEMORY, "glEGLImageTargetTexture2D");
3162   } else {
3163      ctx->Driver.FreeTextureImageBuffer(ctx, texImage);
3164
3165      ctx->Driver.EGLImageTargetTexture2D(ctx, target,
3166					  texObj, texImage, image);
3167
3168      _mesa_dirty_texobj(ctx, texObj, GL_TRUE);
3169   }
3170   _mesa_unlock_texture(ctx, texObj);
3171
3172}
3173#endif
3174
3175
3176
3177/**
3178 * Implement all the glTexSubImage1/2/3D() functions.
3179 */
3180static void
3181texsubimage(struct gl_context *ctx, GLuint dims, GLenum target, GLint level,
3182            GLint xoffset, GLint yoffset, GLint zoffset,
3183            GLsizei width, GLsizei height, GLsizei depth,
3184            GLenum format, GLenum type, const GLvoid *pixels )
3185{
3186   struct gl_texture_object *texObj;
3187   struct gl_texture_image *texImage;
3188
3189   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3190
3191   if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
3192      _mesa_debug(ctx, "glTexSubImage%uD %s %d %d %d %d %d %d %d %s %s %p\n",
3193                  dims,
3194                  _mesa_lookup_enum_by_nr(target), level,
3195                  xoffset, yoffset, zoffset, width, height, depth,
3196                  _mesa_lookup_enum_by_nr(format),
3197                  _mesa_lookup_enum_by_nr(type), pixels);
3198
3199   /* check target (proxies not allowed) */
3200   if (!legal_texsubimage_target(ctx, dims, target)) {
3201      _mesa_error(ctx, GL_INVALID_ENUM, "glTexSubImage%uD(target=%s)",
3202                  dims, _mesa_lookup_enum_by_nr(target));
3203      return;
3204   }
3205
3206   if (ctx->NewState & _NEW_PIXEL)
3207      _mesa_update_state(ctx);
3208
3209   if (subtexture_error_check(ctx, dims, target, level, xoffset, yoffset, zoffset,
3210                              width, height, depth, format, type)) {
3211      return;   /* error was detected */
3212   }
3213
3214   texObj = _mesa_get_current_tex_object(ctx, target);
3215
3216   _mesa_lock_texture(ctx, texObj);
3217   {
3218      texImage = _mesa_select_tex_image(ctx, texObj, target, level);
3219
3220      if (subtexture_error_check2(ctx, dims, target, level,
3221                                  xoffset, yoffset, zoffset,
3222				  width, height, depth,
3223                                  format, type, texImage)) {
3224         /* error was recorded */
3225      }
3226      else if (width > 0 && height > 0 && depth > 0) {
3227         /* If we have a border, offset=-1 is legal.  Bias by border width. */
3228         switch (dims) {
3229         case 3:
3230            if (target != GL_TEXTURE_2D_ARRAY)
3231               zoffset += texImage->Border;
3232            /* fall-through */
3233         case 2:
3234            if (target != GL_TEXTURE_1D_ARRAY)
3235               yoffset += texImage->Border;
3236            /* fall-through */
3237         case 1:
3238            xoffset += texImage->Border;
3239         }
3240
3241         ctx->Driver.TexSubImage(ctx, dims, texImage,
3242                                 xoffset, yoffset, zoffset,
3243                                 width, height, depth,
3244                                 format, type, pixels, &ctx->Unpack);
3245
3246         check_gen_mipmap(ctx, target, texObj, level);
3247
3248         ctx->NewState |= _NEW_TEXTURE;
3249      }
3250   }
3251   _mesa_unlock_texture(ctx, texObj);
3252}
3253
3254
3255void GLAPIENTRY
3256_mesa_TexSubImage1D( GLenum target, GLint level,
3257                     GLint xoffset, GLsizei width,
3258                     GLenum format, GLenum type,
3259                     const GLvoid *pixels )
3260{
3261   GET_CURRENT_CONTEXT(ctx);
3262   texsubimage(ctx, 1, target, level,
3263               xoffset, 0, 0,
3264               width, 1, 1,
3265               format, type, pixels);
3266}
3267
3268
3269void GLAPIENTRY
3270_mesa_TexSubImage2D( GLenum target, GLint level,
3271                     GLint xoffset, GLint yoffset,
3272                     GLsizei width, GLsizei height,
3273                     GLenum format, GLenum type,
3274                     const GLvoid *pixels )
3275{
3276   GET_CURRENT_CONTEXT(ctx);
3277   texsubimage(ctx, 2, target, level,
3278               xoffset, yoffset, 0,
3279               width, height, 1,
3280               format, type, pixels);
3281}
3282
3283
3284
3285void GLAPIENTRY
3286_mesa_TexSubImage3D( GLenum target, GLint level,
3287                     GLint xoffset, GLint yoffset, GLint zoffset,
3288                     GLsizei width, GLsizei height, GLsizei depth,
3289                     GLenum format, GLenum type,
3290                     const GLvoid *pixels )
3291{
3292   GET_CURRENT_CONTEXT(ctx);
3293   texsubimage(ctx, 3, target, level,
3294               xoffset, yoffset, zoffset,
3295               width, height, depth,
3296               format, type, pixels);
3297}
3298
3299
3300
3301/**
3302 * For glCopyTexSubImage, return the source renderbuffer to copy texel data
3303 * from.  This depends on whether the texture contains color or depth values.
3304 */
3305static struct gl_renderbuffer *
3306get_copy_tex_image_source(struct gl_context *ctx, gl_format texFormat)
3307{
3308   if (_mesa_get_format_bits(texFormat, GL_DEPTH_BITS) > 0) {
3309      /* reading from depth/stencil buffer */
3310      return ctx->ReadBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;
3311   }
3312   else {
3313      /* copying from color buffer */
3314      return ctx->ReadBuffer->_ColorReadBuffer;
3315   }
3316}
3317
3318
3319
3320/**
3321 * Implement the glCopyTexImage1/2D() functions.
3322 */
3323static void
3324copyteximage(struct gl_context *ctx, GLuint dims,
3325             GLenum target, GLint level, GLenum internalFormat,
3326             GLint x, GLint y, GLsizei width, GLsizei height, GLint border )
3327{
3328   struct gl_texture_object *texObj;
3329   struct gl_texture_image *texImage;
3330   const GLuint face = _mesa_tex_target_to_face(target);
3331
3332   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3333
3334   if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
3335      _mesa_debug(ctx, "glCopyTexImage%uD %s %d %s %d %d %d %d %d\n",
3336                  dims,
3337                  _mesa_lookup_enum_by_nr(target), level,
3338                  _mesa_lookup_enum_by_nr(internalFormat),
3339                  x, y, width, height, border);
3340
3341   if (ctx->NewState & NEW_COPY_TEX_STATE)
3342      _mesa_update_state(ctx);
3343
3344   if (copytexture_error_check(ctx, dims, target, level, internalFormat,
3345                               width, height, border))
3346      return;
3347
3348   texObj = _mesa_get_current_tex_object(ctx, target);
3349
3350   if (border && ctx->Const.StripTextureBorder) {
3351      x += border;
3352      width -= border * 2;
3353      if (dims == 2) {
3354	 y += border;
3355	 height -= border * 2;
3356      }
3357      border = 0;
3358   }
3359
3360   _mesa_lock_texture(ctx, texObj);
3361   {
3362      texImage = _mesa_get_tex_image(ctx, texObj, target, level);
3363
3364      if (!texImage) {
3365	 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexImage%uD", dims);
3366      }
3367      else {
3368         /* choose actual hw format */
3369         gl_format texFormat = _mesa_choose_texture_format(ctx, texObj,
3370                                                           target, level,
3371                                                           internalFormat,
3372                                                           GL_NONE, GL_NONE);
3373
3374         if (legal_texture_size(ctx, texFormat, width, height, 1)) {
3375            GLint srcX = x, srcY = y, dstX = 0, dstY = 0, dstZ = 0;
3376
3377            /* Free old texture image */
3378            ctx->Driver.FreeTextureImageBuffer(ctx, texImage);
3379
3380            _mesa_init_teximage_fields(ctx, texImage, width, height, 1,
3381                                       border, internalFormat, texFormat);
3382
3383            /* Allocate texture memory (no pixel data yet) */
3384            ctx->Driver.TexImage(ctx, dims, texImage,
3385                                 GL_NONE, GL_NONE,
3386                                 NULL, &ctx->Unpack);
3387
3388            if (_mesa_clip_copytexsubimage(ctx, &dstX, &dstY, &srcX, &srcY,
3389                                           &width, &height)) {
3390               struct gl_renderbuffer *srcRb =
3391                  get_copy_tex_image_source(ctx, texImage->TexFormat);
3392
3393               ctx->Driver.CopyTexSubImage(ctx, dims, texImage, dstX, dstY, dstZ,
3394                                           srcRb, srcX, srcY, width, height);
3395            }
3396
3397            check_gen_mipmap(ctx, target, texObj, level);
3398
3399            _mesa_update_fbo_texture(ctx, texObj, face, level);
3400
3401            _mesa_dirty_texobj(ctx, texObj, GL_TRUE);
3402         }
3403         else {
3404            /* probably too large of image */
3405            _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexImage%uD", dims);
3406         }
3407      }
3408   }
3409   _mesa_unlock_texture(ctx, texObj);
3410}
3411
3412
3413
3414void GLAPIENTRY
3415_mesa_CopyTexImage1D( GLenum target, GLint level,
3416                      GLenum internalFormat,
3417                      GLint x, GLint y,
3418                      GLsizei width, GLint border )
3419{
3420   GET_CURRENT_CONTEXT(ctx);
3421   copyteximage(ctx, 1, target, level, internalFormat, x, y, width, 1, border);
3422}
3423
3424
3425
3426void GLAPIENTRY
3427_mesa_CopyTexImage2D( GLenum target, GLint level, GLenum internalFormat,
3428                      GLint x, GLint y, GLsizei width, GLsizei height,
3429                      GLint border )
3430{
3431   GET_CURRENT_CONTEXT(ctx);
3432   copyteximage(ctx, 2, target, level, internalFormat,
3433                x, y, width, height, border);
3434}
3435
3436
3437
3438/**
3439 * Implementation for glCopyTexSubImage1/2/3D() functions.
3440 */
3441static void
3442copytexsubimage(struct gl_context *ctx, GLuint dims, GLenum target, GLint level,
3443                GLint xoffset, GLint yoffset, GLint zoffset,
3444                GLint x, GLint y, GLsizei width, GLsizei height)
3445{
3446   struct gl_texture_object *texObj;
3447   struct gl_texture_image *texImage;
3448
3449   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3450
3451   if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
3452      _mesa_debug(ctx, "glCopyTexSubImage%uD %s %d %d %d %d %d %d %d %d\n",
3453                  dims,
3454                  _mesa_lookup_enum_by_nr(target),
3455                  level, xoffset, yoffset, zoffset, x, y, width, height);
3456
3457   if (ctx->NewState & NEW_COPY_TEX_STATE)
3458      _mesa_update_state(ctx);
3459
3460   if (copytexsubimage_error_check1(ctx, dims, target, level))
3461      return;
3462
3463   texObj = _mesa_get_current_tex_object(ctx, target);
3464
3465   _mesa_lock_texture(ctx, texObj);
3466   {
3467      texImage = _mesa_select_tex_image(ctx, texObj, target, level);
3468
3469      if (copytexsubimage_error_check2(ctx, dims, target, level, xoffset, yoffset,
3470				       zoffset, width, height, texImage)) {
3471         /* error was recored */
3472      }
3473      else {
3474         /* If we have a border, offset=-1 is legal.  Bias by border width. */
3475         switch (dims) {
3476         case 3:
3477            if (target != GL_TEXTURE_2D_ARRAY)
3478               zoffset += texImage->Border;
3479            /* fall-through */
3480         case 2:
3481            if (target != GL_TEXTURE_1D_ARRAY)
3482               yoffset += texImage->Border;
3483            /* fall-through */
3484         case 1:
3485            xoffset += texImage->Border;
3486         }
3487
3488         if (_mesa_clip_copytexsubimage(ctx, &xoffset, &yoffset, &x, &y,
3489                                        &width, &height)) {
3490            struct gl_renderbuffer *srcRb =
3491               get_copy_tex_image_source(ctx, texImage->TexFormat);
3492
3493            ctx->Driver.CopyTexSubImage(ctx, dims, texImage,
3494                                        xoffset, yoffset, zoffset,
3495                                        srcRb, x, y, width, height);
3496
3497            check_gen_mipmap(ctx, target, texObj, level);
3498
3499            ctx->NewState |= _NEW_TEXTURE;
3500         }
3501      }
3502   }
3503   _mesa_unlock_texture(ctx, texObj);
3504}
3505
3506
3507void GLAPIENTRY
3508_mesa_CopyTexSubImage1D( GLenum target, GLint level,
3509                         GLint xoffset, GLint x, GLint y, GLsizei width )
3510{
3511   GET_CURRENT_CONTEXT(ctx);
3512   copytexsubimage(ctx, 1, target, level, xoffset, 0, 0, x, y, width, 1);
3513}
3514
3515
3516
3517void GLAPIENTRY
3518_mesa_CopyTexSubImage2D( GLenum target, GLint level,
3519                         GLint xoffset, GLint yoffset,
3520                         GLint x, GLint y, GLsizei width, GLsizei height )
3521{
3522   GET_CURRENT_CONTEXT(ctx);
3523   copytexsubimage(ctx, 2, target, level, xoffset, yoffset, 0, x, y,
3524                   width, height);
3525}
3526
3527
3528
3529void GLAPIENTRY
3530_mesa_CopyTexSubImage3D( GLenum target, GLint level,
3531                         GLint xoffset, GLint yoffset, GLint zoffset,
3532                         GLint x, GLint y, GLsizei width, GLsizei height )
3533{
3534   GET_CURRENT_CONTEXT(ctx);
3535   copytexsubimage(ctx, 3, target, level, xoffset, yoffset, zoffset,
3536                   x, y, width, height);
3537}
3538
3539
3540
3541
3542/**********************************************************************/
3543/******                   Compressed Textures                    ******/
3544/**********************************************************************/
3545
3546
3547/**
3548 * Error checking for glCompressedTexSubImage[123]D().
3549 * \warning  There are some bad assumptions here about the size of compressed
3550 *           texture tiles (multiple of 4) used to test the validity of the
3551 *           offset and size parameters.
3552 * \return error code or GL_NO_ERROR.
3553 */
3554static GLenum
3555compressed_subtexture_error_check(struct gl_context *ctx, GLint dimensions,
3556                                  GLenum target, GLint level,
3557                                  GLint xoffset, GLint yoffset, GLint zoffset,
3558                                  GLsizei width, GLsizei height, GLsizei depth,
3559                                  GLenum format, GLsizei imageSize)
3560{
3561   GLint expectedSize, maxLevels = 0, maxTextureSize;
3562   GLuint bw, bh;
3563   (void) zoffset;
3564
3565   if (dimensions == 1) {
3566      /* 1D compressed textures not allowed */
3567      return GL_INVALID_ENUM;
3568   }
3569   else if (dimensions == 2) {
3570      if (target == GL_PROXY_TEXTURE_2D) {
3571         maxLevels = ctx->Const.MaxTextureLevels;
3572      }
3573      else if (target == GL_TEXTURE_2D) {
3574         maxLevels = ctx->Const.MaxTextureLevels;
3575      }
3576      else if (target == GL_PROXY_TEXTURE_CUBE_MAP_ARB) {
3577         if (!ctx->Extensions.ARB_texture_cube_map)
3578            return GL_INVALID_ENUM; /*target*/
3579         maxLevels = ctx->Const.MaxCubeTextureLevels;
3580      }
3581      else if (_mesa_is_cube_face(target)) {
3582         if (!ctx->Extensions.ARB_texture_cube_map)
3583            return GL_INVALID_ENUM; /*target*/
3584         maxLevels = ctx->Const.MaxCubeTextureLevels;
3585      }
3586      else {
3587         return GL_INVALID_ENUM; /*target*/
3588      }
3589   }
3590   else if (dimensions == 3) {
3591      /* 3D compressed textures not allowed */
3592      return GL_INVALID_ENUM;
3593   }
3594
3595   maxTextureSize = 1 << (maxLevels - 1);
3596
3597   /* this will catch any invalid compressed format token */
3598   if (!_mesa_is_compressed_format(ctx, format))
3599      return GL_INVALID_ENUM;
3600
3601   if (width < 1 || width > maxTextureSize)
3602      return GL_INVALID_VALUE;
3603
3604   if ((height < 1 || height > maxTextureSize)
3605       && dimensions > 1)
3606      return GL_INVALID_VALUE;
3607
3608   if (level < 0 || level >= maxLevels)
3609      return GL_INVALID_VALUE;
3610
3611   /*
3612    * do checks which depend on compression block size
3613    */
3614   get_compressed_block_size(format, &bw, &bh);
3615
3616   if ((xoffset % bw != 0) || (yoffset % bh != 0))
3617      return GL_INVALID_OPERATION;
3618
3619   if ((width % bw != 0) && width != 2 && width != 1)
3620      return GL_INVALID_OPERATION;
3621
3622   if ((height % bh != 0) && height != 2 && height != 1)
3623      return GL_INVALID_OPERATION;
3624
3625   expectedSize = compressed_tex_size(width, height, depth, format);
3626   if (expectedSize != imageSize)
3627      return GL_INVALID_VALUE;
3628
3629   return GL_NO_ERROR;
3630}
3631
3632
3633/**
3634 * Do second part of glCompressedTexSubImage error checking.
3635 * \return GL_TRUE if error found, GL_FALSE otherwise.
3636 */
3637static GLboolean
3638compressed_subtexture_error_check2(struct gl_context *ctx, GLuint dims,
3639                                   GLsizei width, GLsizei height,
3640                                   GLsizei depth, GLenum format,
3641                                   struct gl_texture_image *texImage)
3642{
3643   if (!texImage) {
3644      _mesa_error(ctx, GL_INVALID_OPERATION,
3645                  "glCompressedTexSubImage%uD(undefined image level)", dims);
3646      return GL_TRUE;
3647   }
3648
3649   if ((GLint) format != texImage->InternalFormat) {
3650      _mesa_error(ctx, GL_INVALID_OPERATION,
3651                  "glCompressedTexSubImage%uD(format=0x%x)", dims, format);
3652      return GL_TRUE;
3653   }
3654
3655   if (compressedteximage_only_format(ctx, format)) {
3656      _mesa_error(ctx, GL_INVALID_OPERATION,
3657                  "glCompressedTexSubImage%uD(format=0x%x cannot be updated)"
3658                  , dims, format);
3659      return GL_TRUE;
3660   }
3661
3662   if (((width == 1 || width == 2) &&
3663        width != (GLsizei) texImage->Width) ||
3664       (width > (GLsizei) texImage->Width)) {
3665      _mesa_error(ctx, GL_INVALID_VALUE,
3666                  "glCompressedTexSubImage%uD(width=%d)", dims, width);
3667      return GL_TRUE;
3668   }
3669
3670   if (dims >= 2) {
3671      if (((height == 1 || height == 2) &&
3672           height != (GLsizei) texImage->Height) ||
3673          (height > (GLsizei) texImage->Height)) {
3674         _mesa_error(ctx, GL_INVALID_VALUE,
3675                     "glCompressedTexSubImage%uD(height=%d)", dims, height);
3676         return GL_TRUE;
3677      }
3678   }
3679
3680   if (dims >= 3) {
3681      if (((depth == 1 || depth == 2) &&
3682           depth != (GLsizei) texImage->Depth) ||
3683          (depth > (GLsizei) texImage->Depth)) {
3684         _mesa_error(ctx, GL_INVALID_VALUE,
3685                     "glCompressedTexSubImage%uD(depth=%d)", dims, depth);
3686         return GL_TRUE;
3687      }
3688   }
3689
3690   return GL_FALSE;
3691}
3692
3693
3694void GLAPIENTRY
3695_mesa_CompressedTexImage1DARB(GLenum target, GLint level,
3696                              GLenum internalFormat, GLsizei width,
3697                              GLint border, GLsizei imageSize,
3698                              const GLvoid *data)
3699{
3700   GET_CURRENT_CONTEXT(ctx);
3701   teximage(ctx, GL_TRUE, 1, target, level, internalFormat,
3702            width, 1, 1, border, GL_NONE, GL_NONE, imageSize, data);
3703}
3704
3705
3706void GLAPIENTRY
3707_mesa_CompressedTexImage2DARB(GLenum target, GLint level,
3708                              GLenum internalFormat, GLsizei width,
3709                              GLsizei height, GLint border, GLsizei imageSize,
3710                              const GLvoid *data)
3711{
3712   GET_CURRENT_CONTEXT(ctx);
3713   teximage(ctx, GL_TRUE, 2, target, level, internalFormat,
3714            width, height, 1, border, GL_NONE, GL_NONE, imageSize, data);
3715}
3716
3717
3718void GLAPIENTRY
3719_mesa_CompressedTexImage3DARB(GLenum target, GLint level,
3720                              GLenum internalFormat, GLsizei width,
3721                              GLsizei height, GLsizei depth, GLint border,
3722                              GLsizei imageSize, const GLvoid *data)
3723{
3724   GET_CURRENT_CONTEXT(ctx);
3725   teximage(ctx, GL_TRUE, 3, target, level, internalFormat,
3726            width, height, depth, border, GL_NONE, GL_NONE, imageSize, data);
3727}
3728
3729
3730/**
3731 * Common helper for glCompressedTexSubImage1/2/3D().
3732 */
3733static void
3734compressed_tex_sub_image(GLuint dims, GLenum target, GLint level,
3735                         GLint xoffset, GLint yoffset, GLint zoffset,
3736                         GLsizei width, GLsizei height, GLsizei depth,
3737                         GLenum format, GLsizei imageSize, const GLvoid *data)
3738{
3739   struct gl_texture_object *texObj;
3740   struct gl_texture_image *texImage;
3741   GLenum error;
3742   GET_CURRENT_CONTEXT(ctx);
3743   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3744
3745   error = compressed_subtexture_error_check(ctx, dims, target, level,
3746                                             xoffset, 0, 0, /* pos */
3747                                             width, height, depth,   /* size */
3748                                             format, imageSize);
3749   if (error) {
3750      _mesa_error(ctx, error, "glCompressedTexSubImage%uD", dims);
3751      return;
3752   }
3753
3754   texObj = _mesa_get_current_tex_object(ctx, target);
3755
3756   _mesa_lock_texture(ctx, texObj);
3757   {
3758      texImage = _mesa_select_tex_image(ctx, texObj, target, level);
3759
3760      if (compressed_subtexture_error_check2(ctx, dims, width, height, depth,
3761                                             format, texImage)) {
3762         /* error was recorded */
3763      }
3764      else if (width > 0 && height > 0 && depth > 0) {
3765         ctx->Driver.CompressedTexSubImage(ctx, dims, texImage,
3766                                           xoffset, yoffset, zoffset,
3767                                           width, height, depth,
3768                                           format, imageSize, data);
3769
3770         check_gen_mipmap(ctx, target, texObj, level);
3771
3772         ctx->NewState |= _NEW_TEXTURE;
3773      }
3774   }
3775   _mesa_unlock_texture(ctx, texObj);
3776}
3777
3778
3779void GLAPIENTRY
3780_mesa_CompressedTexSubImage1DARB(GLenum target, GLint level, GLint xoffset,
3781                                 GLsizei width, GLenum format,
3782                                 GLsizei imageSize, const GLvoid *data)
3783{
3784   compressed_tex_sub_image(1, target, level, xoffset, 0, 0, width, 1, 1,
3785                            format, imageSize, data);
3786}
3787
3788
3789void GLAPIENTRY
3790_mesa_CompressedTexSubImage2DARB(GLenum target, GLint level, GLint xoffset,
3791                                 GLint yoffset, GLsizei width, GLsizei height,
3792                                 GLenum format, GLsizei imageSize,
3793                                 const GLvoid *data)
3794{
3795   compressed_tex_sub_image(2, target, level, xoffset, yoffset, 0,
3796                            width, height, 1, format, imageSize, data);
3797}
3798
3799
3800void GLAPIENTRY
3801_mesa_CompressedTexSubImage3DARB(GLenum target, GLint level, GLint xoffset,
3802                                 GLint yoffset, GLint zoffset, GLsizei width,
3803                                 GLsizei height, GLsizei depth, GLenum format,
3804                                 GLsizei imageSize, const GLvoid *data)
3805{
3806   compressed_tex_sub_image(3, target, level, xoffset, yoffset, zoffset,
3807                            width, height, depth, format, imageSize, data);
3808}
3809
3810static gl_format
3811get_texbuffer_format(const struct gl_context *ctx, GLenum internalFormat)
3812{
3813   switch (internalFormat) {
3814   case GL_ALPHA8:
3815      return MESA_FORMAT_A8;
3816   case GL_ALPHA16:
3817      return MESA_FORMAT_A16;
3818   case GL_ALPHA16F_ARB:
3819      return MESA_FORMAT_ALPHA_FLOAT16;
3820   case GL_ALPHA32F_ARB:
3821      return MESA_FORMAT_ALPHA_FLOAT32;
3822   case GL_ALPHA8I_EXT:
3823      return MESA_FORMAT_ALPHA_INT8;
3824   case GL_ALPHA16I_EXT:
3825      return MESA_FORMAT_ALPHA_INT16;
3826   case GL_ALPHA32I_EXT:
3827      return MESA_FORMAT_ALPHA_INT32;
3828   case GL_ALPHA8UI_EXT:
3829      return MESA_FORMAT_ALPHA_UINT8;
3830   case GL_ALPHA16UI_EXT:
3831      return MESA_FORMAT_ALPHA_UINT16;
3832   case GL_ALPHA32UI_EXT:
3833      return MESA_FORMAT_ALPHA_UINT32;
3834   case GL_LUMINANCE8:
3835      return MESA_FORMAT_L8;
3836   case GL_LUMINANCE16:
3837      return MESA_FORMAT_L16;
3838   case GL_LUMINANCE16F_ARB:
3839      return MESA_FORMAT_LUMINANCE_FLOAT16;
3840   case GL_LUMINANCE32F_ARB:
3841      return MESA_FORMAT_LUMINANCE_FLOAT32;
3842   case GL_LUMINANCE8I_EXT:
3843      return MESA_FORMAT_LUMINANCE_INT8;
3844   case GL_LUMINANCE16I_EXT:
3845      return MESA_FORMAT_LUMINANCE_INT16;
3846   case GL_LUMINANCE32I_EXT:
3847      return MESA_FORMAT_LUMINANCE_INT32;
3848   case GL_LUMINANCE8UI_EXT:
3849      return MESA_FORMAT_LUMINANCE_UINT8;
3850   case GL_LUMINANCE16UI_EXT:
3851      return MESA_FORMAT_LUMINANCE_UINT16;
3852   case GL_LUMINANCE32UI_EXT:
3853      return MESA_FORMAT_LUMINANCE_UINT32;
3854   case GL_LUMINANCE8_ALPHA8:
3855      return MESA_FORMAT_AL88;
3856   case GL_LUMINANCE16_ALPHA16:
3857      return MESA_FORMAT_AL1616;
3858   case GL_LUMINANCE_ALPHA16F_ARB:
3859      return MESA_FORMAT_LUMINANCE_ALPHA_FLOAT16;
3860   case GL_LUMINANCE_ALPHA32F_ARB:
3861      return MESA_FORMAT_LUMINANCE_ALPHA_FLOAT32;
3862   case GL_LUMINANCE_ALPHA8I_EXT:
3863      return MESA_FORMAT_LUMINANCE_ALPHA_INT8;
3864   case GL_LUMINANCE_ALPHA16I_EXT:
3865      return MESA_FORMAT_LUMINANCE_ALPHA_INT8;
3866   case GL_LUMINANCE_ALPHA32I_EXT:
3867      return MESA_FORMAT_LUMINANCE_ALPHA_INT16;
3868   case GL_LUMINANCE_ALPHA8UI_EXT:
3869      return MESA_FORMAT_LUMINANCE_ALPHA_UINT8;
3870   case GL_LUMINANCE_ALPHA16UI_EXT:
3871      return MESA_FORMAT_LUMINANCE_ALPHA_UINT16;
3872   case GL_LUMINANCE_ALPHA32UI_EXT:
3873      return MESA_FORMAT_LUMINANCE_ALPHA_UINT32;
3874   case GL_INTENSITY8:
3875      return MESA_FORMAT_I8;
3876   case GL_INTENSITY16:
3877      return MESA_FORMAT_I16;
3878   case GL_INTENSITY16F_ARB:
3879      return MESA_FORMAT_INTENSITY_FLOAT16;
3880   case GL_INTENSITY32F_ARB:
3881      return MESA_FORMAT_INTENSITY_FLOAT32;
3882   case GL_INTENSITY8I_EXT:
3883      return MESA_FORMAT_INTENSITY_INT8;
3884   case GL_INTENSITY16I_EXT:
3885      return MESA_FORMAT_INTENSITY_INT16;
3886   case GL_INTENSITY32I_EXT:
3887      return MESA_FORMAT_INTENSITY_INT32;
3888   case GL_INTENSITY8UI_EXT:
3889      return MESA_FORMAT_INTENSITY_UINT8;
3890   case GL_INTENSITY16UI_EXT:
3891      return MESA_FORMAT_INTENSITY_UINT16;
3892   case GL_INTENSITY32UI_EXT:
3893      return MESA_FORMAT_INTENSITY_UINT32;
3894   case GL_RGBA8:
3895      return MESA_FORMAT_RGBA8888_REV;
3896   case GL_RGBA16:
3897      return MESA_FORMAT_RGBA_16;
3898   case GL_RGBA16F_ARB:
3899      return MESA_FORMAT_RGBA_FLOAT16;
3900   case GL_RGBA32F_ARB:
3901      return MESA_FORMAT_RGBA_FLOAT32;
3902   case GL_RGBA8I_EXT:
3903      return MESA_FORMAT_RGBA_INT8;
3904   case GL_RGBA16I_EXT:
3905      return MESA_FORMAT_RGBA_INT16;
3906   case GL_RGBA32I_EXT:
3907      return MESA_FORMAT_RGBA_INT32;
3908   case GL_RGBA8UI_EXT:
3909      return MESA_FORMAT_RGBA_UINT8;
3910   case GL_RGBA16UI_EXT:
3911      return MESA_FORMAT_RGBA_UINT16;
3912   case GL_RGBA32UI_EXT:
3913      return MESA_FORMAT_RGBA_UINT32;
3914
3915   case GL_RG8:
3916      return MESA_FORMAT_GR88;
3917   case GL_RG16:
3918      return MESA_FORMAT_RG1616;
3919   case GL_RG16F:
3920      return MESA_FORMAT_RG_FLOAT16;
3921   case GL_RG32F:
3922      return MESA_FORMAT_RG_FLOAT32;
3923   case GL_RG8I:
3924      return MESA_FORMAT_RG_INT8;
3925   case GL_RG16I:
3926      return MESA_FORMAT_RG_INT16;
3927   case GL_RG32I:
3928      return MESA_FORMAT_RG_INT32;
3929   case GL_RG8UI:
3930      return MESA_FORMAT_RG_UINT8;
3931   case GL_RG16UI:
3932      return MESA_FORMAT_RG_UINT16;
3933   case GL_RG32UI:
3934      return MESA_FORMAT_RG_UINT32;
3935
3936   case GL_R8:
3937      return MESA_FORMAT_R8;
3938   case GL_R16:
3939      return MESA_FORMAT_R16;
3940   case GL_R16F:
3941      return MESA_FORMAT_R_FLOAT16;
3942   case GL_R32F:
3943      return MESA_FORMAT_R_FLOAT32;
3944   case GL_R8I:
3945      return MESA_FORMAT_R_INT8;
3946   case GL_R16I:
3947      return MESA_FORMAT_R_INT16;
3948   case GL_R32I:
3949      return MESA_FORMAT_R_INT32;
3950   case GL_R8UI:
3951      return MESA_FORMAT_R_UINT8;
3952   case GL_R16UI:
3953      return MESA_FORMAT_R_UINT16;
3954   case GL_R32UI:
3955      return MESA_FORMAT_R_UINT32;
3956
3957   default:
3958      return MESA_FORMAT_NONE;
3959   }
3960}
3961
3962static gl_format
3963validate_texbuffer_format(const struct gl_context *ctx, GLenum internalFormat)
3964{
3965   gl_format format = get_texbuffer_format(ctx, internalFormat);
3966   GLenum datatype;
3967
3968   if (format == MESA_FORMAT_NONE)
3969      return MESA_FORMAT_NONE;
3970
3971   datatype = _mesa_get_format_datatype(format);
3972   if (datatype == GL_FLOAT && !ctx->Extensions.ARB_texture_float)
3973      return MESA_FORMAT_NONE;
3974
3975   if (datatype == GL_HALF_FLOAT && !ctx->Extensions.ARB_half_float_pixel)
3976      return MESA_FORMAT_NONE;
3977
3978   /* The GL_ARB_texture_rg and GL_ARB_texture_buffer_object specs don't make
3979    * any mention of R/RG formats, but they appear in the GL 3.1 core
3980    * specification.
3981    */
3982   if (ctx->Version <= 30) {
3983      GLenum base_format = _mesa_get_format_base_format(format);
3984
3985      if (base_format == GL_R || base_format == GL_RG)
3986	 return MESA_FORMAT_NONE;
3987   }
3988   return format;
3989}
3990
3991
3992/** GL_ARB_texture_buffer_object */
3993void GLAPIENTRY
3994_mesa_TexBuffer(GLenum target, GLenum internalFormat, GLuint buffer)
3995{
3996   struct gl_texture_object *texObj;
3997   struct gl_buffer_object *bufObj;
3998   gl_format format;
3999
4000   GET_CURRENT_CONTEXT(ctx);
4001   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
4002
4003   if (!(ctx->Extensions.ARB_texture_buffer_object
4004         && _mesa_is_desktop_gl(ctx))) {
4005      _mesa_error(ctx, GL_INVALID_OPERATION, "glTexBuffer");
4006      return;
4007   }
4008
4009   if (target != GL_TEXTURE_BUFFER_ARB) {
4010      _mesa_error(ctx, GL_INVALID_ENUM, "glTexBuffer(target)");
4011      return;
4012   }
4013
4014   format = validate_texbuffer_format(ctx, internalFormat);
4015   if (format == MESA_FORMAT_NONE) {
4016      _mesa_error(ctx, GL_INVALID_ENUM, "glTexBuffer(internalFormat 0x%x)",
4017                  internalFormat);
4018      return;
4019   }
4020
4021   bufObj = _mesa_lookup_bufferobj(ctx, buffer);
4022   if (buffer && !bufObj) {
4023      _mesa_error(ctx, GL_INVALID_OPERATION, "glTexBuffer(buffer %u)", buffer);
4024      return;
4025   }
4026
4027   texObj = _mesa_get_current_tex_object(ctx, target);
4028
4029   _mesa_lock_texture(ctx, texObj);
4030   {
4031      _mesa_reference_buffer_object(ctx, &texObj->BufferObject, bufObj);
4032      texObj->BufferObjectFormat = internalFormat;
4033      texObj->_BufferObjectFormat = format;
4034   }
4035   _mesa_unlock_texture(ctx, texObj);
4036}
4037