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