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