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