OSDN Git Service

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