1afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg/*
2120d71a45cfda1edfa8cd6b1732e209eb98b53d8Brian Paul * Mesa 3-D graphics library
35e3bc0c2a2bcdf59949410f94c9b705fc1281ce8Jouk Jansen *
49f6022d0567dc1288888212d7128acc48795b306Brian * Copyright (C) 1999-2008  Brian Paul   All Rights Reserved.
580684649a6d01f0e0517b14f61cbcad6fa101929Brian Paul * Copyright (C) 2009  VMware, Inc.  All Rights Reserved.
65e3bc0c2a2bcdf59949410f94c9b705fc1281ce8Jouk Jansen *
7afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg * Permission is hereby granted, free of charge, to any person obtaining a
8afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg * copy of this software and associated documentation files (the "Software"),
9afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg * to deal in the Software without restriction, including without limitation
10afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg * and/or sell copies of the Software, and to permit persons to whom the
12afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg * Software is furnished to do so, subject to the following conditions:
135e3bc0c2a2bcdf59949410f94c9b705fc1281ce8Jouk Jansen *
14afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg * The above copyright notice and this permission notice shall be included
15afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg * in all copies or substantial portions of the Software.
165e3bc0c2a2bcdf59949410f94c9b705fc1281ce8Jouk Jansen *
17afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg */
24afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
256dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell
26add9f2168a5d6b15eb9955ee761246c4f4cf8458Brian Paul/**
27add9f2168a5d6b15eb9955ee761246c4f4cf8458Brian Paul * \file teximage.c
2877ee31930a1b0cc7766939415f4f04ed6a1fa4acBrian Paul * Texture image-related functions.
29add9f2168a5d6b15eb9955ee761246c4f4cf8458Brian Paul */
30add9f2168a5d6b15eb9955ee761246c4f4cf8458Brian Paul
31ea9b212fca78eada7cc7c4449de3f31d2ceea1c3Ian Romanick#include <stdbool.h>
32fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul#include "glheader.h"
33b46712ca9d379d9c091f5543500088d82cf9776cBrian Paul#include "bufferobj.h"
34afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg#include "context.h"
35d100cbf721010f4eacc87507cc87c5314150d493Maciej Cencora#include "enums.h"
3634bd1233a9874fe12a822c4fcb926d48456e1f29Brian Paul#include "fbobject.h"
3734bd1233a9874fe12a822c4fcb926d48456e1f29Brian Paul#include "framebuffer.h"
382897cee99fb877e1f3cd9a881a61418c9c31867fBrian Paul#include "hash.h"
39afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg#include "image.h"
403c63452e64df7e10aa073c6c3b9492b1d7dabbb8Brian Paul#include "imports.h"
41ebb248aa5c018dc676d389221d76ed329059789eBrian Paul#include "macros.h"
42db61b9ce39bccc43140357652ceb78baaf2aea44Vinson Lee#include "mfeatures.h"
43fa4525e289b475b928a7b2c4055af9dd7fe46600Brian Paul#include "state.h"
4489fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul#include "texcompress.h"
45c1a9e6010baceeff78f72ac0692aefc4312d815fBrian Paul#include "texcompress_cpal.h"
46afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg#include "teximage.h"
47878c69fe540a73011b676ead33ac8d9b8c9e63aaBrian Paul#include "texobj.h"
48afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg#include "texstate.h"
495e3bc0c2a2bcdf59949410f94c9b705fc1281ce8Jouk Jansen#include "mtypes.h"
509ad8f431b2a47060bf05517246ab0fa8d249c800Jordan Justen#include "glformats.h"
51afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
52afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
53409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick/* Inexplicably, GL_HALF_FLOAT_OES has a different value than GL_HALF_FLOAT.
54409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick */
55409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick#ifndef GL_HALF_FLOAT_OES
56409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick#define GL_HALF_FLOAT_OES 0x8D61
57409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick#endif
58409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick
594cf6718725c7cf3bfb728118a8b14f8cf206c701Brian Paul/**
6063f01309801c5a900d8d7f5ccd63413e33ff9bffBrian Paul * State changes which we care about for glCopyTex[Sub]Image() calls.
6163f01309801c5a900d8d7f5ccd63413e33ff9bffBrian Paul * In particular, we care about pixel transfer state and buffer state
6263f01309801c5a900d8d7f5ccd63413e33ff9bffBrian Paul * (such as glReadBuffer to make sure we read from the right renderbuffer).
6363f01309801c5a900d8d7f5ccd63413e33ff9bffBrian Paul */
647ecb61c30c4984a8513b306b640f9d274d8ecf61Brian Paul#define NEW_COPY_TEX_STATE (_NEW_BUFFERS | _NEW_PIXEL)
6563f01309801c5a900d8d7f5ccd63413e33ff9bffBrian Paul
6663f01309801c5a900d8d7f5ccd63413e33ff9bffBrian Paul
6763f01309801c5a900d8d7f5ccd63413e33ff9bffBrian Paul
6863f01309801c5a900d8d7f5ccd63413e33ff9bffBrian Paul/**
691749a25ca889d514889b34cf6311c8014d97bf66Brian Paul * Return the simple base format for a given internal texture format.
701749a25ca889d514889b34cf6311c8014d97bf66Brian Paul * For example, given GL_LUMINANCE12_ALPHA4, return GL_LUMINANCE_ALPHA.
716dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell *
726dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param ctx GL context.
731749a25ca889d514889b34cf6311c8014d97bf66Brian Paul * \param internalFormat the internal texture format token or 1, 2, 3, or 4.
746dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell *
756dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \return the corresponding \u base internal format (GL_ALPHA, GL_LUMINANCE,
766dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * GL_LUMANCE_ALPHA, GL_INTENSITY, GL_RGB, or GL_RGBA), or -1 if invalid enum.
7789fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul *
7889fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul * This is the format which is used during texture application (i.e. the
7989fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul * texture format and env mode determine the arithmetic used.
80afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg */
81b132e8da5e5f2b7da1f2141e0322e66bb0608e02Brian PaulGLint
82f9995b30756140724f41daf963fa06167912be7fKristian Høgsberg_mesa_base_tex_format( struct gl_context *ctx, GLint internalFormat )
83afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg{
841749a25ca889d514889b34cf6311c8014d97bf66Brian Paul   switch (internalFormat) {
85afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_ALPHA:
86afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_ALPHA4:
87afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_ALPHA8:
88afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_ALPHA12:
89afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_ALPHA16:
90ae88281b7b67d656a7d1467c39c8d93dc8778485Ian Romanick         return (ctx->API != API_OPENGL_CORE) ? GL_ALPHA : -1;
91afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case 1:
92afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_LUMINANCE:
93afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_LUMINANCE4:
94afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_LUMINANCE8:
95afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_LUMINANCE12:
96afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_LUMINANCE16:
97ae88281b7b67d656a7d1467c39c8d93dc8778485Ian Romanick         return (ctx->API != API_OPENGL_CORE) ? GL_LUMINANCE : -1;
98afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case 2:
99afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_LUMINANCE_ALPHA:
100afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_LUMINANCE4_ALPHA4:
101afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_LUMINANCE6_ALPHA2:
102afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_LUMINANCE8_ALPHA8:
103afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_LUMINANCE12_ALPHA4:
104afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_LUMINANCE12_ALPHA12:
105afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_LUMINANCE16_ALPHA16:
106ae88281b7b67d656a7d1467c39c8d93dc8778485Ian Romanick         return (ctx->API != API_OPENGL_CORE) ? GL_LUMINANCE_ALPHA : -1;
107afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_INTENSITY:
108afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_INTENSITY4:
109afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_INTENSITY8:
110afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_INTENSITY12:
111afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_INTENSITY16:
112ae88281b7b67d656a7d1467c39c8d93dc8778485Ian Romanick         return (ctx->API != API_OPENGL_CORE) ? GL_INTENSITY : -1;
113afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case 3:
114ae88281b7b67d656a7d1467c39c8d93dc8778485Ian Romanick         return (ctx->API != API_OPENGL_CORE) ? GL_RGB : -1;
115afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_RGB:
116afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_R3_G3_B2:
117afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_RGB4:
118afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_RGB5:
119afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_RGB8:
120afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_RGB10:
121afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_RGB12:
122afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_RGB16:
123afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         return GL_RGB;
124afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case 4:
125ae88281b7b67d656a7d1467c39c8d93dc8778485Ian Romanick         return (ctx->API != API_OPENGL_CORE) ? GL_RGBA : -1;
126afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_RGBA:
127afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_RGBA2:
128afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_RGBA4:
129afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_RGB5_A1:
130afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_RGBA8:
131afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_RGB10_A2:
132afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_RGBA12:
133afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_RGBA16:
134afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         return GL_RGBA;
135f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      default:
136f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         ; /* fallthrough */
137f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   }
138f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
13929386d1f2d60e905d63f4c5f045ff3794b2ff99cIan Romanick   /* GL_BGRA can be an internal format *only* in OpenGL ES (1.x or 2.0).
14029386d1f2d60e905d63f4c5f045ff3794b2ff99cIan Romanick    */
14109714c09a40501d82823e42f7461d7b8d7bf11c0Jordan Justen   if (_mesa_is_gles(ctx)) {
142f1452844fe2522387bbc6633f22aec09b8a06204Benjamin Franzke      switch (internalFormat) {
14329386d1f2d60e905d63f4c5f045ff3794b2ff99cIan Romanick         case GL_BGRA:
144f1452844fe2522387bbc6633f22aec09b8a06204Benjamin Franzke            return GL_RGBA;
145f1452844fe2522387bbc6633f22aec09b8a06204Benjamin Franzke         default:
146f1452844fe2522387bbc6633f22aec09b8a06204Benjamin Franzke            ; /* fallthrough */
1471a06e8454ec714e950bc88882cd985534a18bf1fMarek Olšák      }
1481a06e8454ec714e950bc88882cd985534a18bf1fMarek Olšák   }
1491a06e8454ec714e950bc88882cd985534a18bf1fMarek Olšák
1501a06e8454ec714e950bc88882cd985534a18bf1fMarek Olšák   if (ctx->Extensions.ARB_ES2_compatibility) {
1511a06e8454ec714e950bc88882cd985534a18bf1fMarek Olšák      switch (internalFormat) {
1521a06e8454ec714e950bc88882cd985534a18bf1fMarek Olšák         case GL_RGB565:
1531a06e8454ec714e950bc88882cd985534a18bf1fMarek Olšák            return GL_RGB;
1541a06e8454ec714e950bc88882cd985534a18bf1fMarek Olšák         default:
1551a06e8454ec714e950bc88882cd985534a18bf1fMarek Olšák            ; /* fallthrough */
156f1452844fe2522387bbc6633f22aec09b8a06204Benjamin Franzke      }
157f1452844fe2522387bbc6633f22aec09b8a06204Benjamin Franzke   }
158f1452844fe2522387bbc6633f22aec09b8a06204Benjamin Franzke
1594741dbcbbc2514de370a760f4b78a17491014555Ian Romanick   if (ctx->Extensions.ARB_depth_texture) {
160f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      switch (internalFormat) {
161f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         case GL_DEPTH_COMPONENT:
1624741dbcbbc2514de370a760f4b78a17491014555Ian Romanick         case GL_DEPTH_COMPONENT16:
1634741dbcbbc2514de370a760f4b78a17491014555Ian Romanick         case GL_DEPTH_COMPONENT24:
1644741dbcbbc2514de370a760f4b78a17491014555Ian Romanick         case GL_DEPTH_COMPONENT32:
165f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul            return GL_DEPTH_COMPONENT;
166f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         default:
167f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            ; /* fallthrough */
168f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      }
169f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   }
17089fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul
17133fa5e4bfad8005f09ad3c9fc92c40fa863935d1Ian Romanick   switch (internalFormat) {
17233fa5e4bfad8005f09ad3c9fc92c40fa863935d1Ian Romanick   case GL_COMPRESSED_ALPHA:
17333fa5e4bfad8005f09ad3c9fc92c40fa863935d1Ian Romanick      return GL_ALPHA;
17433fa5e4bfad8005f09ad3c9fc92c40fa863935d1Ian Romanick   case GL_COMPRESSED_LUMINANCE:
17533fa5e4bfad8005f09ad3c9fc92c40fa863935d1Ian Romanick      return GL_LUMINANCE;
17633fa5e4bfad8005f09ad3c9fc92c40fa863935d1Ian Romanick   case GL_COMPRESSED_LUMINANCE_ALPHA:
17733fa5e4bfad8005f09ad3c9fc92c40fa863935d1Ian Romanick      return GL_LUMINANCE_ALPHA;
17833fa5e4bfad8005f09ad3c9fc92c40fa863935d1Ian Romanick   case GL_COMPRESSED_INTENSITY:
17933fa5e4bfad8005f09ad3c9fc92c40fa863935d1Ian Romanick      return GL_INTENSITY;
18033fa5e4bfad8005f09ad3c9fc92c40fa863935d1Ian Romanick   case GL_COMPRESSED_RGB:
18133fa5e4bfad8005f09ad3c9fc92c40fa863935d1Ian Romanick      return GL_RGB;
18233fa5e4bfad8005f09ad3c9fc92c40fa863935d1Ian Romanick   case GL_COMPRESSED_RGBA:
18333fa5e4bfad8005f09ad3c9fc92c40fa863935d1Ian Romanick      return GL_RGBA;
18433fa5e4bfad8005f09ad3c9fc92c40fa863935d1Ian Romanick   default:
18533fa5e4bfad8005f09ad3c9fc92c40fa863935d1Ian Romanick      ; /* fallthrough */
186f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   }
187f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
188f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   if (ctx->Extensions.TDFX_texture_compression_FXT1) {
189f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      switch (internalFormat) {
190f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         case GL_COMPRESSED_RGB_FXT1_3DFX:
19189fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul            return GL_RGB;
192f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         case GL_COMPRESSED_RGBA_FXT1_3DFX:
19389fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul            return GL_RGBA;
194f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         default:
195f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            ; /* fallthrough */
196f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      }
197f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   }
198f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
199f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   if (ctx->Extensions.EXT_texture_compression_s3tc) {
200f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      switch (internalFormat) {
2018f04c12e0ad876baa7eb9ed379e2b00150b376e0Brian Paul         case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
2028f04c12e0ad876baa7eb9ed379e2b00150b376e0Brian Paul            return GL_RGB;
203f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
204f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
205f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
20640bd9d0b190e11d39350d1b08d2c2b28e3040bcaDaniel Borca            return GL_RGBA;
207f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         default:
208f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            ; /* fallthrough */
209f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      }
210f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   }
211f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
212f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   if (ctx->Extensions.S3_s3tc) {
213f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      switch (internalFormat) {
214f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         case GL_RGB_S3TC:
215f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         case GL_RGB4_S3TC:
216663a9e1b7ef7b8384abe2f81e1a8749b942f6d3aDaniel Borca            return GL_RGB;
217f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         case GL_RGBA_S3TC:
218f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         case GL_RGBA4_S3TC:
219663a9e1b7ef7b8384abe2f81e1a8749b942f6d3aDaniel Borca            return GL_RGBA;
220f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         default:
221f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            ; /* fallthrough */
222f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      }
223f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   }
22489fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul
225f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   if (ctx->Extensions.MESA_ycbcr_texture) {
226f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      if (internalFormat == GL_YCBCR_MESA)
227f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         return GL_YCBCR_MESA;
228f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   }
2291749a25ca889d514889b34cf6311c8014d97bf66Brian Paul
230f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   if (ctx->Extensions.ARB_texture_float) {
231f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      switch (internalFormat) {
232f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         case GL_ALPHA16F_ARB:
233f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         case GL_ALPHA32F_ARB:
234f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            return GL_ALPHA;
235f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         case GL_RGBA16F_ARB:
236f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         case GL_RGBA32F_ARB:
237f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            return GL_RGBA;
238f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         case GL_RGB16F_ARB:
239f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         case GL_RGB32F_ARB:
240f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            return GL_RGB;
241f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         case GL_INTENSITY16F_ARB:
242f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         case GL_INTENSITY32F_ARB:
243f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            return GL_INTENSITY;
244f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         case GL_LUMINANCE16F_ARB:
245f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         case GL_LUMINANCE32F_ARB:
246f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            return GL_LUMINANCE;
247f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         case GL_LUMINANCE_ALPHA16F_ARB:
248f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         case GL_LUMINANCE_ALPHA32F_ARB:
249f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            return GL_LUMINANCE_ALPHA;
250f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         default:
2518f04c12e0ad876baa7eb9ed379e2b00150b376e0Brian Paul            ; /* fallthrough */
252f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      }
253afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   }
254f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
255114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger   if (ctx->Extensions.ATI_envmap_bumpmap) {
256114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger      switch (internalFormat) {
257114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger         case GL_DUDV_ATI:
258114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger         case GL_DU8DV8_ATI:
259114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger            return GL_DUDV_ATI;
260114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger         default:
261114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger            ; /* fallthrough */
262114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger      }
263114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger   }
264114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger
2650be369975fdfa79bcb702d0c13aa0a221055f61bMarek Olšák   if (ctx->Extensions.EXT_texture_snorm) {
266c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger      switch (internalFormat) {
2670be369975fdfa79bcb702d0c13aa0a221055f61bMarek Olšák         case GL_RED_SNORM:
2680be369975fdfa79bcb702d0c13aa0a221055f61bMarek Olšák         case GL_R8_SNORM:
2690be369975fdfa79bcb702d0c13aa0a221055f61bMarek Olšák         case GL_R16_SNORM:
2700be369975fdfa79bcb702d0c13aa0a221055f61bMarek Olšák            return GL_RED;
2710be369975fdfa79bcb702d0c13aa0a221055f61bMarek Olšák         case GL_RG_SNORM:
2720be369975fdfa79bcb702d0c13aa0a221055f61bMarek Olšák         case GL_RG8_SNORM:
2730be369975fdfa79bcb702d0c13aa0a221055f61bMarek Olšák         case GL_RG16_SNORM:
2740be369975fdfa79bcb702d0c13aa0a221055f61bMarek Olšák            return GL_RG;
2750be369975fdfa79bcb702d0c13aa0a221055f61bMarek Olšák         case GL_RGB_SNORM:
2760be369975fdfa79bcb702d0c13aa0a221055f61bMarek Olšák         case GL_RGB8_SNORM:
2770be369975fdfa79bcb702d0c13aa0a221055f61bMarek Olšák         case GL_RGB16_SNORM:
2780be369975fdfa79bcb702d0c13aa0a221055f61bMarek Olšák            return GL_RGB;
279c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger         case GL_RGBA_SNORM:
280c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger         case GL_RGBA8_SNORM:
2810be369975fdfa79bcb702d0c13aa0a221055f61bMarek Olšák         case GL_RGBA16_SNORM:
282c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger            return GL_RGBA;
2830be369975fdfa79bcb702d0c13aa0a221055f61bMarek Olšák         case GL_ALPHA_SNORM:
2840be369975fdfa79bcb702d0c13aa0a221055f61bMarek Olšák         case GL_ALPHA8_SNORM:
2850be369975fdfa79bcb702d0c13aa0a221055f61bMarek Olšák         case GL_ALPHA16_SNORM:
2860be369975fdfa79bcb702d0c13aa0a221055f61bMarek Olšák            return GL_ALPHA;
2870be369975fdfa79bcb702d0c13aa0a221055f61bMarek Olšák         case GL_LUMINANCE_SNORM:
2880be369975fdfa79bcb702d0c13aa0a221055f61bMarek Olšák         case GL_LUMINANCE8_SNORM:
2890be369975fdfa79bcb702d0c13aa0a221055f61bMarek Olšák         case GL_LUMINANCE16_SNORM:
2900be369975fdfa79bcb702d0c13aa0a221055f61bMarek Olšák            return GL_LUMINANCE;
2910be369975fdfa79bcb702d0c13aa0a221055f61bMarek Olšák         case GL_LUMINANCE_ALPHA_SNORM:
2920be369975fdfa79bcb702d0c13aa0a221055f61bMarek Olšák         case GL_LUMINANCE8_ALPHA8_SNORM:
2930be369975fdfa79bcb702d0c13aa0a221055f61bMarek Olšák         case GL_LUMINANCE16_ALPHA16_SNORM:
2940be369975fdfa79bcb702d0c13aa0a221055f61bMarek Olšák            return GL_LUMINANCE_ALPHA;
2950be369975fdfa79bcb702d0c13aa0a221055f61bMarek Olšák         case GL_INTENSITY_SNORM:
2960be369975fdfa79bcb702d0c13aa0a221055f61bMarek Olšák         case GL_INTENSITY8_SNORM:
2970be369975fdfa79bcb702d0c13aa0a221055f61bMarek Olšák         case GL_INTENSITY16_SNORM:
2980be369975fdfa79bcb702d0c13aa0a221055f61bMarek Olšák            return GL_INTENSITY;
299c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger         default:
300c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger            ; /* fallthrough */
301c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger      }
302c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger   }
303114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger
3041ad7b99925e044f82e635f746c1ef2df77f69ac9Brian Paul   if (ctx->Extensions.EXT_packed_depth_stencil) {
3051ad7b99925e044f82e635f746c1ef2df77f69ac9Brian Paul      switch (internalFormat) {
3061ad7b99925e044f82e635f746c1ef2df77f69ac9Brian Paul         case GL_DEPTH_STENCIL_EXT:
3071ad7b99925e044f82e635f746c1ef2df77f69ac9Brian Paul         case GL_DEPTH24_STENCIL8_EXT:
3081ad7b99925e044f82e635f746c1ef2df77f69ac9Brian Paul            return GL_DEPTH_STENCIL_EXT;
3091ad7b99925e044f82e635f746c1ef2df77f69ac9Brian Paul         default:
3101ad7b99925e044f82e635f746c1ef2df77f69ac9Brian Paul            ; /* fallthrough */
3111ad7b99925e044f82e635f746c1ef2df77f69ac9Brian Paul      }
3121ad7b99925e044f82e635f746c1ef2df77f69ac9Brian Paul   }
3131ad7b99925e044f82e635f746c1ef2df77f69ac9Brian Paul
3140a4be7036864efa6b30d78e0aac449d34b812c13Brian Paul#if FEATURE_EXT_texture_sRGB
3150a4be7036864efa6b30d78e0aac449d34b812c13Brian Paul   if (ctx->Extensions.EXT_texture_sRGB) {
3160a4be7036864efa6b30d78e0aac449d34b812c13Brian Paul      switch (internalFormat) {
3170a4be7036864efa6b30d78e0aac449d34b812c13Brian Paul      case GL_SRGB_EXT:
3180a4be7036864efa6b30d78e0aac449d34b812c13Brian Paul      case GL_SRGB8_EXT:
3190a4be7036864efa6b30d78e0aac449d34b812c13Brian Paul      case GL_COMPRESSED_SRGB_EXT:
3200a4be7036864efa6b30d78e0aac449d34b812c13Brian Paul      case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT:
3210a4be7036864efa6b30d78e0aac449d34b812c13Brian Paul         return GL_RGB;
3220a4be7036864efa6b30d78e0aac449d34b812c13Brian Paul      case GL_SRGB_ALPHA_EXT:
3230a4be7036864efa6b30d78e0aac449d34b812c13Brian Paul      case GL_SRGB8_ALPHA8_EXT:
3240a4be7036864efa6b30d78e0aac449d34b812c13Brian Paul      case GL_COMPRESSED_SRGB_ALPHA_EXT:
3250a4be7036864efa6b30d78e0aac449d34b812c13Brian Paul      case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT:
3260a4be7036864efa6b30d78e0aac449d34b812c13Brian Paul      case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT:
3270a4be7036864efa6b30d78e0aac449d34b812c13Brian Paul      case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT:
3280a4be7036864efa6b30d78e0aac449d34b812c13Brian Paul         return GL_RGBA;
3290a4be7036864efa6b30d78e0aac449d34b812c13Brian Paul      case GL_SLUMINANCE_ALPHA_EXT:
3300a4be7036864efa6b30d78e0aac449d34b812c13Brian Paul      case GL_SLUMINANCE8_ALPHA8_EXT:
3310a4be7036864efa6b30d78e0aac449d34b812c13Brian Paul      case GL_COMPRESSED_SLUMINANCE_ALPHA_EXT:
3320a4be7036864efa6b30d78e0aac449d34b812c13Brian Paul         return GL_LUMINANCE_ALPHA;
3330a4be7036864efa6b30d78e0aac449d34b812c13Brian Paul      case GL_SLUMINANCE_EXT:
3340a4be7036864efa6b30d78e0aac449d34b812c13Brian Paul      case GL_SLUMINANCE8_EXT:
3353488b14a047bf1f05831b3bbdc374a0da482212cEric Anholt      case GL_COMPRESSED_SLUMINANCE_EXT:
3360a4be7036864efa6b30d78e0aac449d34b812c13Brian Paul         return GL_LUMINANCE;
3370a4be7036864efa6b30d78e0aac449d34b812c13Brian Paul      default:
338abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul         ; /* fallthrough */
3390a4be7036864efa6b30d78e0aac449d34b812c13Brian Paul      }
3400a4be7036864efa6b30d78e0aac449d34b812c13Brian Paul   }
3410a4be7036864efa6b30d78e0aac449d34b812c13Brian Paul#endif /* FEATURE_EXT_texture_sRGB */
3420a4be7036864efa6b30d78e0aac449d34b812c13Brian Paul
3439c1b41879aab2ff7386c547a2ccce7686c018cf5Eric Anholt   if (ctx->Version >= 30 ||
3443363e87d7d5b3f19294fc770dc7c506c26646010Marek Olšák       ctx->Extensions.EXT_texture_integer) {
345abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul      switch (internalFormat) {
346abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul      case GL_RGBA8UI_EXT:
347abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul      case GL_RGBA16UI_EXT:
348abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul      case GL_RGBA32UI_EXT:
349abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul      case GL_RGBA8I_EXT:
350abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul      case GL_RGBA16I_EXT:
351abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul      case GL_RGBA32I_EXT:
352f449be660e70aac2aefd2ce84581e137de25520bDave Airlie      case GL_RGB10_A2UI:
353abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul         return GL_RGBA;
354abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul      case GL_RGB8UI_EXT:
355abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul      case GL_RGB16UI_EXT:
356abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul      case GL_RGB32UI_EXT:
357abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul      case GL_RGB8I_EXT:
358abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul      case GL_RGB16I_EXT:
359abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul      case GL_RGB32I_EXT:
360abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul         return GL_RGB;
3613363e87d7d5b3f19294fc770dc7c506c26646010Marek Olšák      }
3623363e87d7d5b3f19294fc770dc7c506c26646010Marek Olšák   }
3633363e87d7d5b3f19294fc770dc7c506c26646010Marek Olšák
3643363e87d7d5b3f19294fc770dc7c506c26646010Marek Olšák   if (ctx->Extensions.EXT_texture_integer) {
3653363e87d7d5b3f19294fc770dc7c506c26646010Marek Olšák      switch (internalFormat) {
366abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul      case GL_ALPHA8UI_EXT:
367abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul      case GL_ALPHA16UI_EXT:
368abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul      case GL_ALPHA32UI_EXT:
369abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul      case GL_ALPHA8I_EXT:
370abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul      case GL_ALPHA16I_EXT:
371abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul      case GL_ALPHA32I_EXT:
372abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul         return GL_ALPHA;
373abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul      case GL_INTENSITY8UI_EXT:
374abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul      case GL_INTENSITY16UI_EXT:
375abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul      case GL_INTENSITY32UI_EXT:
376abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul      case GL_INTENSITY8I_EXT:
377abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul      case GL_INTENSITY16I_EXT:
378abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul      case GL_INTENSITY32I_EXT:
379abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul         return GL_INTENSITY;
380abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul      case GL_LUMINANCE8UI_EXT:
381abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul      case GL_LUMINANCE16UI_EXT:
382abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul      case GL_LUMINANCE32UI_EXT:
383abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul      case GL_LUMINANCE8I_EXT:
384abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul      case GL_LUMINANCE16I_EXT:
385abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul      case GL_LUMINANCE32I_EXT:
386abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul         return GL_LUMINANCE;
387abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul      case GL_LUMINANCE_ALPHA8UI_EXT:
388abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul      case GL_LUMINANCE_ALPHA16UI_EXT:
389abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul      case GL_LUMINANCE_ALPHA32UI_EXT:
390abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul      case GL_LUMINANCE_ALPHA8I_EXT:
391abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul      case GL_LUMINANCE_ALPHA16I_EXT:
392abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul      case GL_LUMINANCE_ALPHA32I_EXT:
393abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul         return GL_LUMINANCE_ALPHA;
394abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul      default:
395abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul         ; /* fallthrough */
396abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul      }
3976988f65e43297ae63bbce30bf882f870b370096cBrian Paul   }
3986988f65e43297ae63bbce30bf882f870b370096cBrian Paul
3996988f65e43297ae63bbce30bf882f870b370096cBrian Paul   if (ctx->Extensions.ARB_texture_rg) {
4006988f65e43297ae63bbce30bf882f870b370096cBrian Paul      switch (internalFormat) {
4016988f65e43297ae63bbce30bf882f870b370096cBrian Paul      case GL_R16F:
4023ebbc176f9200ac954d461758937e755220ac551Ian Romanick	 /* R16F depends on both ARB_half_float_pixel and ARB_texture_float.
4033ebbc176f9200ac954d461758937e755220ac551Ian Romanick	  */
4043ebbc176f9200ac954d461758937e755220ac551Ian Romanick	 if (!ctx->Extensions.ARB_half_float_pixel)
4053ebbc176f9200ac954d461758937e755220ac551Ian Romanick	    break;
4063ebbc176f9200ac954d461758937e755220ac551Ian Romanick	 /* FALLTHROUGH */
4076988f65e43297ae63bbce30bf882f870b370096cBrian Paul      case GL_R32F:
4083ebbc176f9200ac954d461758937e755220ac551Ian Romanick	 if (!ctx->Extensions.ARB_texture_float)
4093ebbc176f9200ac954d461758937e755220ac551Ian Romanick	    break;
4103ebbc176f9200ac954d461758937e755220ac551Ian Romanick         return GL_RED;
4116988f65e43297ae63bbce30bf882f870b370096cBrian Paul      case GL_R8I:
4126988f65e43297ae63bbce30bf882f870b370096cBrian Paul      case GL_R8UI:
4136988f65e43297ae63bbce30bf882f870b370096cBrian Paul      case GL_R16I:
4146988f65e43297ae63bbce30bf882f870b370096cBrian Paul      case GL_R16UI:
4156988f65e43297ae63bbce30bf882f870b370096cBrian Paul      case GL_R32I:
4166988f65e43297ae63bbce30bf882f870b370096cBrian Paul      case GL_R32UI:
4179c1b41879aab2ff7386c547a2ccce7686c018cf5Eric Anholt	 if (ctx->Version < 30 && !ctx->Extensions.EXT_texture_integer)
4183ebbc176f9200ac954d461758937e755220ac551Ian Romanick	    break;
4193ebbc176f9200ac954d461758937e755220ac551Ian Romanick	 /* FALLTHROUGH */
4203ebbc176f9200ac954d461758937e755220ac551Ian Romanick      case GL_R8:
4213ebbc176f9200ac954d461758937e755220ac551Ian Romanick      case GL_R16:
4223ebbc176f9200ac954d461758937e755220ac551Ian Romanick      case GL_RED:
4233ebbc176f9200ac954d461758937e755220ac551Ian Romanick      case GL_COMPRESSED_RED:
4243ebbc176f9200ac954d461758937e755220ac551Ian Romanick         return GL_RED;
4253ebbc176f9200ac954d461758937e755220ac551Ian Romanick
4266988f65e43297ae63bbce30bf882f870b370096cBrian Paul      case GL_RG16F:
4273ebbc176f9200ac954d461758937e755220ac551Ian Romanick	 /* RG16F depends on both ARB_half_float_pixel and ARB_texture_float.
4283ebbc176f9200ac954d461758937e755220ac551Ian Romanick	  */
4293ebbc176f9200ac954d461758937e755220ac551Ian Romanick	 if (!ctx->Extensions.ARB_half_float_pixel)
4303ebbc176f9200ac954d461758937e755220ac551Ian Romanick	    break;
4313ebbc176f9200ac954d461758937e755220ac551Ian Romanick	 /* FALLTHROUGH */
4326988f65e43297ae63bbce30bf882f870b370096cBrian Paul      case GL_RG32F:
4333ebbc176f9200ac954d461758937e755220ac551Ian Romanick	 if (!ctx->Extensions.ARB_texture_float)
4343ebbc176f9200ac954d461758937e755220ac551Ian Romanick	    break;
4353ebbc176f9200ac954d461758937e755220ac551Ian Romanick         return GL_RG;
4366988f65e43297ae63bbce30bf882f870b370096cBrian Paul      case GL_RG8I:
4376988f65e43297ae63bbce30bf882f870b370096cBrian Paul      case GL_RG8UI:
4386988f65e43297ae63bbce30bf882f870b370096cBrian Paul      case GL_RG16I:
4396988f65e43297ae63bbce30bf882f870b370096cBrian Paul      case GL_RG16UI:
4406988f65e43297ae63bbce30bf882f870b370096cBrian Paul      case GL_RG32I:
4416988f65e43297ae63bbce30bf882f870b370096cBrian Paul      case GL_RG32UI:
4429c1b41879aab2ff7386c547a2ccce7686c018cf5Eric Anholt	 if (ctx->Version < 30 && !ctx->Extensions.EXT_texture_integer)
4433ebbc176f9200ac954d461758937e755220ac551Ian Romanick	    break;
4443ebbc176f9200ac954d461758937e755220ac551Ian Romanick	 /* FALLTHROUGH */
4453ebbc176f9200ac954d461758937e755220ac551Ian Romanick      case GL_RG:
4463ebbc176f9200ac954d461758937e755220ac551Ian Romanick      case GL_RG8:
4473ebbc176f9200ac954d461758937e755220ac551Ian Romanick      case GL_RG16:
4483ebbc176f9200ac954d461758937e755220ac551Ian Romanick      case GL_COMPRESSED_RG:
4496988f65e43297ae63bbce30bf882f870b370096cBrian Paul         return GL_RG;
4506988f65e43297ae63bbce30bf882f870b370096cBrian Paul      default:
4516988f65e43297ae63bbce30bf882f870b370096cBrian Paul         ; /* fallthrough */
4526988f65e43297ae63bbce30bf882f870b370096cBrian Paul      }
4536988f65e43297ae63bbce30bf882f870b370096cBrian Paul   }
4546988f65e43297ae63bbce30bf882f870b370096cBrian Paul
4556988f65e43297ae63bbce30bf882f870b370096cBrian Paul   if (ctx->Extensions.EXT_texture_shared_exponent) {
4566988f65e43297ae63bbce30bf882f870b370096cBrian Paul      switch (internalFormat) {
4576988f65e43297ae63bbce30bf882f870b370096cBrian Paul      case GL_RGB9_E5_EXT:
4586988f65e43297ae63bbce30bf882f870b370096cBrian Paul         return GL_RGB;
4596988f65e43297ae63bbce30bf882f870b370096cBrian Paul      default:
4606988f65e43297ae63bbce30bf882f870b370096cBrian Paul         ; /* fallthrough */
4616988f65e43297ae63bbce30bf882f870b370096cBrian Paul      }
4626988f65e43297ae63bbce30bf882f870b370096cBrian Paul   }
4636988f65e43297ae63bbce30bf882f870b370096cBrian Paul
4646988f65e43297ae63bbce30bf882f870b370096cBrian Paul   if (ctx->Extensions.EXT_packed_float) {
4656988f65e43297ae63bbce30bf882f870b370096cBrian Paul      switch (internalFormat) {
4666988f65e43297ae63bbce30bf882f870b370096cBrian Paul      case GL_R11F_G11F_B10F_EXT:
4676988f65e43297ae63bbce30bf882f870b370096cBrian Paul         return GL_RGB;
4686988f65e43297ae63bbce30bf882f870b370096cBrian Paul      default:
4696988f65e43297ae63bbce30bf882f870b370096cBrian Paul         ; /* fallthrough */
4706988f65e43297ae63bbce30bf882f870b370096cBrian Paul      }
4716988f65e43297ae63bbce30bf882f870b370096cBrian Paul   }
4726988f65e43297ae63bbce30bf882f870b370096cBrian Paul
4736988f65e43297ae63bbce30bf882f870b370096cBrian Paul   if (ctx->Extensions.ARB_depth_buffer_float) {
4746988f65e43297ae63bbce30bf882f870b370096cBrian Paul      switch (internalFormat) {
4756988f65e43297ae63bbce30bf882f870b370096cBrian Paul      case GL_DEPTH_COMPONENT32F:
4766988f65e43297ae63bbce30bf882f870b370096cBrian Paul         return GL_DEPTH_COMPONENT;
4776988f65e43297ae63bbce30bf882f870b370096cBrian Paul      case GL_DEPTH32F_STENCIL8:
4786988f65e43297ae63bbce30bf882f870b370096cBrian Paul         return GL_DEPTH_STENCIL;
4796988f65e43297ae63bbce30bf882f870b370096cBrian Paul      default:
4806988f65e43297ae63bbce30bf882f870b370096cBrian Paul         ; /* fallthrough */
4816988f65e43297ae63bbce30bf882f870b370096cBrian Paul      }
4826988f65e43297ae63bbce30bf882f870b370096cBrian Paul   }
4836988f65e43297ae63bbce30bf882f870b370096cBrian Paul
484e2a054b70cb5dace40fc1426cbf936366dc72fb9Ian Romanick   if (ctx->Extensions.ARB_texture_compression_rgtc) {
4856988f65e43297ae63bbce30bf882f870b370096cBrian Paul      switch (internalFormat) {
486e2a054b70cb5dace40fc1426cbf936366dc72fb9Ian Romanick      case GL_COMPRESSED_RED_RGTC1:
487e2a054b70cb5dace40fc1426cbf936366dc72fb9Ian Romanick      case GL_COMPRESSED_SIGNED_RED_RGTC1:
4886988f65e43297ae63bbce30bf882f870b370096cBrian Paul         return GL_RED;
489e2a054b70cb5dace40fc1426cbf936366dc72fb9Ian Romanick      case GL_COMPRESSED_RG_RGTC2:
490e2a054b70cb5dace40fc1426cbf936366dc72fb9Ian Romanick      case GL_COMPRESSED_SIGNED_RG_RGTC2:
4916988f65e43297ae63bbce30bf882f870b370096cBrian Paul         return GL_RG;
4926988f65e43297ae63bbce30bf882f870b370096cBrian Paul      default:
4936988f65e43297ae63bbce30bf882f870b370096cBrian Paul         ; /* fallthrough */
4946988f65e43297ae63bbce30bf882f870b370096cBrian Paul      }
495abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul   }
496abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul
4977d16e2c0cd70dc5a23b746dbc8e44c58366b5353Marek Olšák   if (ctx->Extensions.EXT_texture_compression_latc) {
4987d16e2c0cd70dc5a23b746dbc8e44c58366b5353Marek Olšák      switch (internalFormat) {
4997d16e2c0cd70dc5a23b746dbc8e44c58366b5353Marek Olšák      case GL_COMPRESSED_LUMINANCE_LATC1_EXT:
5007d16e2c0cd70dc5a23b746dbc8e44c58366b5353Marek Olšák      case GL_COMPRESSED_SIGNED_LUMINANCE_LATC1_EXT:
5017d16e2c0cd70dc5a23b746dbc8e44c58366b5353Marek Olšák         return GL_LUMINANCE;
5027d16e2c0cd70dc5a23b746dbc8e44c58366b5353Marek Olšák      case GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT:
5037d16e2c0cd70dc5a23b746dbc8e44c58366b5353Marek Olšák      case GL_COMPRESSED_SIGNED_LUMINANCE_ALPHA_LATC2_EXT:
5047d16e2c0cd70dc5a23b746dbc8e44c58366b5353Marek Olšák         return GL_LUMINANCE_ALPHA;
5057d16e2c0cd70dc5a23b746dbc8e44c58366b5353Marek Olšák      default:
5067d16e2c0cd70dc5a23b746dbc8e44c58366b5353Marek Olšák         ; /* fallthrough */
5077d16e2c0cd70dc5a23b746dbc8e44c58366b5353Marek Olšák      }
5087d16e2c0cd70dc5a23b746dbc8e44c58366b5353Marek Olšák   }
5097d16e2c0cd70dc5a23b746dbc8e44c58366b5353Marek Olšák
51069f16accd0aa3b8d414092a5e52ccc99649da01aMarek Olšák   if (ctx->Extensions.ATI_texture_compression_3dc) {
51169f16accd0aa3b8d414092a5e52ccc99649da01aMarek Olšák      switch (internalFormat) {
51269f16accd0aa3b8d414092a5e52ccc99649da01aMarek Olšák      case GL_COMPRESSED_LUMINANCE_ALPHA_3DC_ATI:
51369f16accd0aa3b8d414092a5e52ccc99649da01aMarek Olšák         return GL_LUMINANCE_ALPHA;
51469f16accd0aa3b8d414092a5e52ccc99649da01aMarek Olšák      default:
51569f16accd0aa3b8d414092a5e52ccc99649da01aMarek Olšák         ; /* fallthrough */
51669f16accd0aa3b8d414092a5e52ccc99649da01aMarek Olšák      }
51769f16accd0aa3b8d414092a5e52ccc99649da01aMarek Olšák   }
51869f16accd0aa3b8d414092a5e52ccc99649da01aMarek Olšák
519d4a38e86d4b4d66cca20ee63222f940cb73fa709Chia-I Wu   if (ctx->Extensions.OES_compressed_ETC1_RGB8_texture) {
520d4a38e86d4b4d66cca20ee63222f940cb73fa709Chia-I Wu      switch (internalFormat) {
521d4a38e86d4b4d66cca20ee63222f940cb73fa709Chia-I Wu      case GL_ETC1_RGB8_OES:
522d4a38e86d4b4d66cca20ee63222f940cb73fa709Chia-I Wu         return GL_RGB;
523d4a38e86d4b4d66cca20ee63222f940cb73fa709Chia-I Wu      default:
524d4a38e86d4b4d66cca20ee63222f940cb73fa709Chia-I Wu         ; /* fallthrough */
525d4a38e86d4b4d66cca20ee63222f940cb73fa709Chia-I Wu      }
526d4a38e86d4b4d66cca20ee63222f940cb73fa709Chia-I Wu   }
527d4a38e86d4b4d66cca20ee63222f940cb73fa709Chia-I Wu
528fc0fa16be3792bdabad0993fc4026bf621d6092fIan Romanick   if (ctx->API == API_OPENGLES) {
529fc0fa16be3792bdabad0993fc4026bf621d6092fIan Romanick      switch (internalFormat) {
530fc0fa16be3792bdabad0993fc4026bf621d6092fIan Romanick      case GL_PALETTE4_RGB8_OES:
531fc0fa16be3792bdabad0993fc4026bf621d6092fIan Romanick      case GL_PALETTE4_R5_G6_B5_OES:
532fc0fa16be3792bdabad0993fc4026bf621d6092fIan Romanick      case GL_PALETTE8_RGB8_OES:
533fc0fa16be3792bdabad0993fc4026bf621d6092fIan Romanick      case GL_PALETTE8_R5_G6_B5_OES:
534fc0fa16be3792bdabad0993fc4026bf621d6092fIan Romanick	 return GL_RGB;
535fc0fa16be3792bdabad0993fc4026bf621d6092fIan Romanick      case GL_PALETTE4_RGBA8_OES:
536fc0fa16be3792bdabad0993fc4026bf621d6092fIan Romanick      case GL_PALETTE8_RGB5_A1_OES:
537fc0fa16be3792bdabad0993fc4026bf621d6092fIan Romanick      case GL_PALETTE4_RGBA4_OES:
538fc0fa16be3792bdabad0993fc4026bf621d6092fIan Romanick      case GL_PALETTE4_RGB5_A1_OES:
539fc0fa16be3792bdabad0993fc4026bf621d6092fIan Romanick      case GL_PALETTE8_RGBA8_OES:
540fc0fa16be3792bdabad0993fc4026bf621d6092fIan Romanick      case GL_PALETTE8_RGBA4_OES:
541fc0fa16be3792bdabad0993fc4026bf621d6092fIan Romanick	 return GL_RGBA;
542fc0fa16be3792bdabad0993fc4026bf621d6092fIan Romanick      default:
543fc0fa16be3792bdabad0993fc4026bf621d6092fIan Romanick         ; /* fallthrough */
544fc0fa16be3792bdabad0993fc4026bf621d6092fIan Romanick      }
545fc0fa16be3792bdabad0993fc4026bf621d6092fIan Romanick   }
546fc0fa16be3792bdabad0993fc4026bf621d6092fIan Romanick
547f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   return -1; /* error */
548afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg}
549afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
550afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
5516dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/**
552e93243f8b7d43695654a36334c8cc5cea140d23cBrian * For cube map faces, return a face index in [0,5].
553e93243f8b7d43695654a36334c8cc5cea140d23cBrian * For other targets return 0;
554e93243f8b7d43695654a36334c8cc5cea140d23cBrian */
555e93243f8b7d43695654a36334c8cc5cea140d23cBrianGLuint
556e93243f8b7d43695654a36334c8cc5cea140d23cBrian_mesa_tex_target_to_face(GLenum target)
557519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul{
5582efa3d4f9fd1e1683c5366d402d8740ecfb688e5Brian Paul   if (_mesa_is_cube_face(target))
559519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul      return (GLuint) target - (GLuint) GL_TEXTURE_CUBE_MAP_POSITIVE_X;
560519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul   else
561519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul      return 0;
562519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul}
563519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul
564519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul
565519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul
5666dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/**
567009ac0618ccb8c1347c353b576442ed70592c20eBrian Paul * Install gl_texture_image in a gl_texture_object according to the target
568009ac0618ccb8c1347c353b576442ed70592c20eBrian Paul * and level parameters.
5696dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell *
5706dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param tObj texture object.
5716dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param target texture target.
5726dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param level image level.
5736dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param texImage texture image.
574afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg */
575009ac0618ccb8c1347c353b576442ed70592c20eBrian Paulstatic void
576009ac0618ccb8c1347c353b576442ed70592c20eBrian Paulset_tex_image(struct gl_texture_object *tObj,
577009ac0618ccb8c1347c353b576442ed70592c20eBrian Paul              GLenum target, GLint level,
578009ac0618ccb8c1347c353b576442ed70592c20eBrian Paul              struct gl_texture_image *texImage)
579fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul{
5809c9a9abd7b953ec9b6cfc52c2f6981c5d38b1691Brian Paul   const GLuint face = _mesa_tex_target_to_face(target);
5819c9a9abd7b953ec9b6cfc52c2f6981c5d38b1691Brian Paul
582fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul   ASSERT(tObj);
583fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul   ASSERT(texImage);
5840c87f16817ff0bf1f05e0d634944fd47b097faeeChia-I Wu   if (target == GL_TEXTURE_RECTANGLE_NV || target == GL_TEXTURE_EXTERNAL_OES)
5850c87f16817ff0bf1f05e0d634944fd47b097faeeChia-I Wu      assert(level == 0);
5869c9a9abd7b953ec9b6cfc52c2f6981c5d38b1691Brian Paul
5879c9a9abd7b953ec9b6cfc52c2f6981c5d38b1691Brian Paul   tObj->Image[face][level] = texImage;
5889c9a9abd7b953ec9b6cfc52c2f6981c5d38b1691Brian Paul
589a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul   /* Set the 'back' pointer */
590a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul   texImage->TexObject = tObj;
591a231d245514b43cf547ccb996b68efc0186e6821Brian Paul   texImage->Level = level;
592a231d245514b43cf547ccb996b68efc0186e6821Brian Paul   texImage->Face = face;
593fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul}
594fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul
595fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul
596a3f137094cd965d27e1b088499dd609b81a91906Brian Paul/**
5976dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Allocate a texture image structure.
5986dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell *
599a3f137094cd965d27e1b088499dd609b81a91906Brian Paul * Called via ctx->Driver.NewTextureImage() unless overriden by a device
600a3f137094cd965d27e1b088499dd609b81a91906Brian Paul * driver.
6016dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell *
6026dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \return a pointer to gl_texture_image struct with all fields initialized to
6036dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * zero.
60477ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paul */
60577ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paulstruct gl_texture_image *
606f9995b30756140724f41daf963fa06167912be7fKristian Høgsberg_mesa_new_texture_image( struct gl_context *ctx )
60777ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paul{
608a3f137094cd965d27e1b088499dd609b81a91906Brian Paul   (void) ctx;
60977ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paul   return CALLOC_STRUCT(gl_texture_image);
61077ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paul}
61177ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paul
61277ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paul
613a3f137094cd965d27e1b088499dd609b81a91906Brian Paul/**
614146f536b3332b7a2022bb4ba5e2d1d2ec4bedd98Brian Paul * Free a gl_texture_image and associated data.
615146f536b3332b7a2022bb4ba5e2d1d2ec4bedd98Brian Paul * This function is a fallback called via ctx->Driver.DeleteTextureImage().
6166dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell *
6171eee1bac1f6d911e6124daafc9b9291666d91cefVinson Lee * \param texImage texture image.
6186dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell *
6193e62d3a8d88b48d4ed19e00ea2bbc3d0a2b6acf7Keith Whitwell * Free the texture image structure and the associated image data.
620a3f137094cd965d27e1b088499dd609b81a91906Brian Paul */
62177ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paulvoid
6223c59febf05e6af80d63e5b9a478a11b275ac429cBrian Paul_mesa_delete_texture_image(struct gl_context *ctx,
6233c59febf05e6af80d63e5b9a478a11b275ac429cBrian Paul                           struct gl_texture_image *texImage)
62477ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paul{
6255ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell   /* Free texImage->Data and/or any other driver-specific texture
6265ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell    * image storage.
6275ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell    */
6280bb29949ba8a9e5a15dc0640dbb0a4e7990a1d57Eric Anholt   ASSERT(ctx->Driver.FreeTextureImageBuffer);
6290bb29949ba8a9e5a15dc0640dbb0a4e7990a1d57Eric Anholt   ctx->Driver.FreeTextureImageBuffer( ctx, texImage );
630dae2fca70521909cfdd8d217d5e42fc6b33b05f6Pekka Paalanen   free(texImage);
63177ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paul}
63277ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paul
63377ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paul
6346dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/**
6356dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Test if a target is a proxy target.
6366dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell *
6376dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param target texture target.
6386dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell *
6396dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \return GL_TRUE if the target is a proxy target, GL_FALSE otherwise.
640aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul */
64177ee31930a1b0cc7766939415f4f04ed6a1fa4acBrian PaulGLboolean
64277ee31930a1b0cc7766939415f4f04ed6a1fa4acBrian Paul_mesa_is_proxy_texture(GLenum target)
643aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul{
6440c87f16817ff0bf1f05e0d634944fd47b097faeeChia-I Wu   /*
6450c87f16817ff0bf1f05e0d634944fd47b097faeeChia-I Wu    * NUM_TEXTURE_TARGETS should match number of terms below, except there's no
6460c87f16817ff0bf1f05e0d634944fd47b097faeeChia-I Wu    * proxy for GL_TEXTURE_BUFFER and GL_TEXTURE_EXTERNAL_OES.
647874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul    */
6480c87f16817ff0bf1f05e0d634944fd47b097faeeChia-I Wu   assert(NUM_TEXTURE_TARGETS == 7 + 2);
6492465c4fa9cabe8c40e526b9e081de3b70c851455Brian Paul
6508e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul   return (target == GL_PROXY_TEXTURE_1D ||
6518e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul           target == GL_PROXY_TEXTURE_2D ||
6528e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul           target == GL_PROXY_TEXTURE_3D ||
65327f4484fb73ac7bf4f790ca2d3efd50b6bea25c3Brian Paul           target == GL_PROXY_TEXTURE_CUBE_MAP_ARB ||
654bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick           target == GL_PROXY_TEXTURE_RECTANGLE_NV ||
655bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick           target == GL_PROXY_TEXTURE_1D_ARRAY_EXT ||
656bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick           target == GL_PROXY_TEXTURE_2D_ARRAY_EXT);
657aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul}
658aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul
659aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul
6606dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/**
66135f620d55ced7fcb3f39465c173ba2ae435a2ba1Brian Paul * Return the proxy target which corresponds to the given texture target
66235f620d55ced7fcb3f39465c173ba2ae435a2ba1Brian Paul */
663f9bb66b1cee2272b20ff3086e84d17026b8e8bb9Brian PaulGLenum
664f9bb66b1cee2272b20ff3086e84d17026b8e8bb9Brian Paul_mesa_get_proxy_target(GLenum target)
66535f620d55ced7fcb3f39465c173ba2ae435a2ba1Brian Paul{
66635f620d55ced7fcb3f39465c173ba2ae435a2ba1Brian Paul   switch (target) {
66735f620d55ced7fcb3f39465c173ba2ae435a2ba1Brian Paul   case GL_TEXTURE_1D:
66835f620d55ced7fcb3f39465c173ba2ae435a2ba1Brian Paul   case GL_PROXY_TEXTURE_1D:
66935f620d55ced7fcb3f39465c173ba2ae435a2ba1Brian Paul      return GL_PROXY_TEXTURE_1D;
67035f620d55ced7fcb3f39465c173ba2ae435a2ba1Brian Paul   case GL_TEXTURE_2D:
67135f620d55ced7fcb3f39465c173ba2ae435a2ba1Brian Paul   case GL_PROXY_TEXTURE_2D:
67235f620d55ced7fcb3f39465c173ba2ae435a2ba1Brian Paul      return GL_PROXY_TEXTURE_2D;
67335f620d55ced7fcb3f39465c173ba2ae435a2ba1Brian Paul   case GL_TEXTURE_3D:
67435f620d55ced7fcb3f39465c173ba2ae435a2ba1Brian Paul   case GL_PROXY_TEXTURE_3D:
67535f620d55ced7fcb3f39465c173ba2ae435a2ba1Brian Paul      return GL_PROXY_TEXTURE_3D;
67635f620d55ced7fcb3f39465c173ba2ae435a2ba1Brian Paul   case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
67735f620d55ced7fcb3f39465c173ba2ae435a2ba1Brian Paul   case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
67835f620d55ced7fcb3f39465c173ba2ae435a2ba1Brian Paul   case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
67935f620d55ced7fcb3f39465c173ba2ae435a2ba1Brian Paul   case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
68035f620d55ced7fcb3f39465c173ba2ae435a2ba1Brian Paul   case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
68135f620d55ced7fcb3f39465c173ba2ae435a2ba1Brian Paul   case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
68235f620d55ced7fcb3f39465c173ba2ae435a2ba1Brian Paul   case GL_TEXTURE_CUBE_MAP_ARB:
68335f620d55ced7fcb3f39465c173ba2ae435a2ba1Brian Paul   case GL_PROXY_TEXTURE_CUBE_MAP_ARB:
68435f620d55ced7fcb3f39465c173ba2ae435a2ba1Brian Paul      return GL_PROXY_TEXTURE_CUBE_MAP_ARB;
68535f620d55ced7fcb3f39465c173ba2ae435a2ba1Brian Paul   case GL_TEXTURE_RECTANGLE_NV:
68635f620d55ced7fcb3f39465c173ba2ae435a2ba1Brian Paul   case GL_PROXY_TEXTURE_RECTANGLE_NV:
68735f620d55ced7fcb3f39465c173ba2ae435a2ba1Brian Paul      return GL_PROXY_TEXTURE_RECTANGLE_NV;
68835f620d55ced7fcb3f39465c173ba2ae435a2ba1Brian Paul   case GL_TEXTURE_1D_ARRAY_EXT:
68935f620d55ced7fcb3f39465c173ba2ae435a2ba1Brian Paul   case GL_PROXY_TEXTURE_1D_ARRAY_EXT:
69035f620d55ced7fcb3f39465c173ba2ae435a2ba1Brian Paul      return GL_PROXY_TEXTURE_1D_ARRAY_EXT;
69135f620d55ced7fcb3f39465c173ba2ae435a2ba1Brian Paul   case GL_TEXTURE_2D_ARRAY_EXT:
69235f620d55ced7fcb3f39465c173ba2ae435a2ba1Brian Paul   case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
69335f620d55ced7fcb3f39465c173ba2ae435a2ba1Brian Paul      return GL_PROXY_TEXTURE_2D_ARRAY_EXT;
69435f620d55ced7fcb3f39465c173ba2ae435a2ba1Brian Paul   default:
695f9bb66b1cee2272b20ff3086e84d17026b8e8bb9Brian Paul      _mesa_problem(NULL, "unexpected target in _mesa_get_proxy_target()");
69635f620d55ced7fcb3f39465c173ba2ae435a2ba1Brian Paul      return 0;
69735f620d55ced7fcb3f39465c173ba2ae435a2ba1Brian Paul   }
69835f620d55ced7fcb3f39465c173ba2ae435a2ba1Brian Paul}
69935f620d55ced7fcb3f39465c173ba2ae435a2ba1Brian Paul
70035f620d55ced7fcb3f39465c173ba2ae435a2ba1Brian Paul
70135f620d55ced7fcb3f39465c173ba2ae435a2ba1Brian Paul/**
7023c59febf05e6af80d63e5b9a478a11b275ac429cBrian Paul * Get the texture object that corresponds to the target of the given
703120d71a45cfda1edfa8cd6b1732e209eb98b53d8Brian Paul * texture unit.  The target should have already been checked for validity.
7046dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell *
7056dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param ctx GL context.
7066dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param texUnit texture unit.
7076dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param target texture target.
7086dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell *
7096dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \return pointer to the texture object on success, or NULL on failure.
71035d5301a54153930ee6fd60dff1010ce9f901397Brian Paul */
71135d5301a54153930ee6fd60dff1010ce9f901397Brian Paulstruct gl_texture_object *
7123c59febf05e6af80d63e5b9a478a11b275ac429cBrian Paul_mesa_select_tex_object(struct gl_context *ctx,
7133c59febf05e6af80d63e5b9a478a11b275ac429cBrian Paul                        const struct gl_texture_unit *texUnit,
71435d5301a54153930ee6fd60dff1010ce9f901397Brian Paul                        GLenum target)
71535d5301a54153930ee6fd60dff1010ce9f901397Brian Paul{
716f2dd11817a66b6254e1533398dc53f1bc51953beBrian Paul   const GLboolean arrayTex = (ctx->Extensions.MESA_texture_array ||
717f2dd11817a66b6254e1533398dc53f1bc51953beBrian Paul                               ctx->Extensions.EXT_texture_array);
718f2dd11817a66b6254e1533398dc53f1bc51953beBrian Paul
71935d5301a54153930ee6fd60dff1010ce9f901397Brian Paul   switch (target) {
72035d5301a54153930ee6fd60dff1010ce9f901397Brian Paul      case GL_TEXTURE_1D:
7219818734e0148510967ca9ee0d1aa8b196b509f02Brian Paul         return texUnit->CurrentTex[TEXTURE_1D_INDEX];
72235d5301a54153930ee6fd60dff1010ce9f901397Brian Paul      case GL_PROXY_TEXTURE_1D:
723fe469007037d9d5cdbe1677d8ff7368b276e9e7cBrian         return ctx->Texture.ProxyTex[TEXTURE_1D_INDEX];
72435d5301a54153930ee6fd60dff1010ce9f901397Brian Paul      case GL_TEXTURE_2D:
7259818734e0148510967ca9ee0d1aa8b196b509f02Brian Paul         return texUnit->CurrentTex[TEXTURE_2D_INDEX];
72635d5301a54153930ee6fd60dff1010ce9f901397Brian Paul      case GL_PROXY_TEXTURE_2D:
727fe469007037d9d5cdbe1677d8ff7368b276e9e7cBrian         return ctx->Texture.ProxyTex[TEXTURE_2D_INDEX];
72835d5301a54153930ee6fd60dff1010ce9f901397Brian Paul      case GL_TEXTURE_3D:
7299818734e0148510967ca9ee0d1aa8b196b509f02Brian Paul         return texUnit->CurrentTex[TEXTURE_3D_INDEX];
73035d5301a54153930ee6fd60dff1010ce9f901397Brian Paul      case GL_PROXY_TEXTURE_3D:
731fe469007037d9d5cdbe1677d8ff7368b276e9e7cBrian         return ctx->Texture.ProxyTex[TEXTURE_3D_INDEX];
73235d5301a54153930ee6fd60dff1010ce9f901397Brian Paul      case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
73335d5301a54153930ee6fd60dff1010ce9f901397Brian Paul      case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
73435d5301a54153930ee6fd60dff1010ce9f901397Brian Paul      case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
73535d5301a54153930ee6fd60dff1010ce9f901397Brian Paul      case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
73635d5301a54153930ee6fd60dff1010ce9f901397Brian Paul      case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
73735d5301a54153930ee6fd60dff1010ce9f901397Brian Paul      case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
7385ff4075a6961b26042dc2d7f4adcf333439823f4Brian Paul      case GL_TEXTURE_CUBE_MAP_ARB:
739a96308c37db0bc0086a017d318bc3504aa5f0b1aKeith Whitwell         return ctx->Extensions.ARB_texture_cube_map
7409818734e0148510967ca9ee0d1aa8b196b509f02Brian Paul                ? texUnit->CurrentTex[TEXTURE_CUBE_INDEX] : NULL;
74135d5301a54153930ee6fd60dff1010ce9f901397Brian Paul      case GL_PROXY_TEXTURE_CUBE_MAP_ARB:
742a96308c37db0bc0086a017d318bc3504aa5f0b1aKeith Whitwell         return ctx->Extensions.ARB_texture_cube_map
743fe469007037d9d5cdbe1677d8ff7368b276e9e7cBrian                ? ctx->Texture.ProxyTex[TEXTURE_CUBE_INDEX] : NULL;
7448afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul      case GL_TEXTURE_RECTANGLE_NV:
7458afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul         return ctx->Extensions.NV_texture_rectangle
7469818734e0148510967ca9ee0d1aa8b196b509f02Brian Paul                ? texUnit->CurrentTex[TEXTURE_RECT_INDEX] : NULL;
7478afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul      case GL_PROXY_TEXTURE_RECTANGLE_NV:
7488afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul         return ctx->Extensions.NV_texture_rectangle
749fe469007037d9d5cdbe1677d8ff7368b276e9e7cBrian                ? ctx->Texture.ProxyTex[TEXTURE_RECT_INDEX] : NULL;
750bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick      case GL_TEXTURE_1D_ARRAY_EXT:
751f2dd11817a66b6254e1533398dc53f1bc51953beBrian Paul         return arrayTex ? texUnit->CurrentTex[TEXTURE_1D_ARRAY_INDEX] : NULL;
752bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick      case GL_PROXY_TEXTURE_1D_ARRAY_EXT:
753f2dd11817a66b6254e1533398dc53f1bc51953beBrian Paul         return arrayTex ? ctx->Texture.ProxyTex[TEXTURE_1D_ARRAY_INDEX] : NULL;
754bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick      case GL_TEXTURE_2D_ARRAY_EXT:
755f2dd11817a66b6254e1533398dc53f1bc51953beBrian Paul         return arrayTex ? texUnit->CurrentTex[TEXTURE_2D_ARRAY_INDEX] : NULL;
756bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick      case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
757f2dd11817a66b6254e1533398dc53f1bc51953beBrian Paul         return arrayTex ? ctx->Texture.ProxyTex[TEXTURE_2D_ARRAY_INDEX] : NULL;
758874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul      case GL_TEXTURE_BUFFER:
759bb4a39ec951a696efa07a120c9446b677703ababEric Anholt         return _mesa_is_desktop_gl(ctx)
760bb4a39ec951a696efa07a120c9446b677703ababEric Anholt            && ctx->Extensions.ARB_texture_buffer_object
761874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul            ? texUnit->CurrentTex[TEXTURE_BUFFER_INDEX] : NULL;
7620c87f16817ff0bf1f05e0d634944fd47b097faeeChia-I Wu      case GL_TEXTURE_EXTERNAL_OES:
7630c87f16817ff0bf1f05e0d634944fd47b097faeeChia-I Wu         return ctx->Extensions.OES_EGL_image_external
7640c87f16817ff0bf1f05e0d634944fd47b097faeeChia-I Wu            ? texUnit->CurrentTex[TEXTURE_EXTERNAL_INDEX] : NULL;
76535d5301a54153930ee6fd60dff1010ce9f901397Brian Paul      default:
76608836341788a9f9d638d9dc8328510ccd18ddeb5Brian Paul         _mesa_problem(NULL, "bad target in _mesa_select_tex_object()");
76735d5301a54153930ee6fd60dff1010ce9f901397Brian Paul         return NULL;
76835d5301a54153930ee6fd60dff1010ce9f901397Brian Paul   }
76935d5301a54153930ee6fd60dff1010ce9f901397Brian Paul}
77035d5301a54153930ee6fd60dff1010ce9f901397Brian Paul
77135d5301a54153930ee6fd60dff1010ce9f901397Brian Paul
7726dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/**
773b8fdb900fb9b1c8b1e9ec88509624237307a869aBrian Paul * Return pointer to texture object for given target on current texture unit.
774b8fdb900fb9b1c8b1e9ec88509624237307a869aBrian Paul */
775b8fdb900fb9b1c8b1e9ec88509624237307a869aBrian Paulstruct gl_texture_object *
776f9995b30756140724f41daf963fa06167912be7fKristian Høgsberg_mesa_get_current_tex_object(struct gl_context *ctx, GLenum target)
777b8fdb900fb9b1c8b1e9ec88509624237307a869aBrian Paul{
778b8fdb900fb9b1c8b1e9ec88509624237307a869aBrian Paul   struct gl_texture_unit *texUnit = _mesa_get_current_tex_unit(ctx);
779b8fdb900fb9b1c8b1e9ec88509624237307a869aBrian Paul   return _mesa_select_tex_object(ctx, texUnit, target);
780b8fdb900fb9b1c8b1e9ec88509624237307a869aBrian Paul}
781b8fdb900fb9b1c8b1e9ec88509624237307a869aBrian Paul
782b8fdb900fb9b1c8b1e9ec88509624237307a869aBrian Paul
783b8fdb900fb9b1c8b1e9ec88509624237307a869aBrian Paul/**
784c156eeb682d673e571c1798ff21e183ad4114feaBrian Paul * Get a texture image pointer from a texture object, given a texture
785c156eeb682d673e571c1798ff21e183ad4114feaBrian Paul * target and mipmap level.  The target and level parameters should
786c156eeb682d673e571c1798ff21e183ad4114feaBrian Paul * have already been error-checked.
7876dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell *
7886dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param ctx GL context.
7891eee1bac1f6d911e6124daafc9b9291666d91cefVinson Lee * \param texObj texture unit.
7906dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param target texture target.
7916dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param level image level.
7926dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell *
793c156eeb682d673e571c1798ff21e183ad4114feaBrian Paul * \return pointer to the texture image structure, or NULL on failure.
794fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul */
795fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paulstruct gl_texture_image *
7963c59febf05e6af80d63e5b9a478a11b275ac429cBrian Paul_mesa_select_tex_image(struct gl_context *ctx,
7973c59febf05e6af80d63e5b9a478a11b275ac429cBrian Paul                       const struct gl_texture_object *texObj,
7985ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell		       GLenum target, GLint level)
799fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul{
800c156eeb682d673e571c1798ff21e183ad4114feaBrian Paul   const GLuint face = _mesa_tex_target_to_face(target);
8015ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell
802c156eeb682d673e571c1798ff21e183ad4114feaBrian Paul   ASSERT(texObj);
803c156eeb682d673e571c1798ff21e183ad4114feaBrian Paul   ASSERT(level >= 0);
804c156eeb682d673e571c1798ff21e183ad4114feaBrian Paul   ASSERT(level < MAX_TEXTURE_LEVELS);
805bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick
806c156eeb682d673e571c1798ff21e183ad4114feaBrian Paul   return texObj->Image[face][level];
807fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul}
808fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul
809fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul
810a3f137094cd965d27e1b088499dd609b81a91906Brian Paul/**
811a3f137094cd965d27e1b088499dd609b81a91906Brian Paul * Like _mesa_select_tex_image() but if the image doesn't exist, allocate
812a3f137094cd965d27e1b088499dd609b81a91906Brian Paul * it and install it.  Only return NULL if passed a bad parameter or run
813a3f137094cd965d27e1b088499dd609b81a91906Brian Paul * out of memory.
814a3f137094cd965d27e1b088499dd609b81a91906Brian Paul */
815a3f137094cd965d27e1b088499dd609b81a91906Brian Paulstruct gl_texture_image *
816f9995b30756140724f41daf963fa06167912be7fKristian Høgsberg_mesa_get_tex_image(struct gl_context *ctx, struct gl_texture_object *texObj,
817a3f137094cd965d27e1b088499dd609b81a91906Brian Paul                    GLenum target, GLint level)
818a3f137094cd965d27e1b088499dd609b81a91906Brian Paul{
819a3f137094cd965d27e1b088499dd609b81a91906Brian Paul   struct gl_texture_image *texImage;
8205ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell
8215ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell   if (!texObj)
8225ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell      return NULL;
8235ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell
8245ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell   texImage = _mesa_select_tex_image(ctx, texObj, target, level);
825a3f137094cd965d27e1b088499dd609b81a91906Brian Paul   if (!texImage) {
826a3f137094cd965d27e1b088499dd609b81a91906Brian Paul      texImage = ctx->Driver.NewTextureImage(ctx);
827a3f137094cd965d27e1b088499dd609b81a91906Brian Paul      if (!texImage) {
828a3f137094cd965d27e1b088499dd609b81a91906Brian Paul         _mesa_error(ctx, GL_OUT_OF_MEMORY, "texture image allocation");
829a3f137094cd965d27e1b088499dd609b81a91906Brian Paul         return NULL;
830a3f137094cd965d27e1b088499dd609b81a91906Brian Paul      }
8315ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell
832009ac0618ccb8c1347c353b576442ed70592c20eBrian Paul      set_tex_image(texObj, target, level, texImage);
833a3f137094cd965d27e1b088499dd609b81a91906Brian Paul   }
8345ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell
835a3f137094cd965d27e1b088499dd609b81a91906Brian Paul   return texImage;
836a3f137094cd965d27e1b088499dd609b81a91906Brian Paul}
837a3f137094cd965d27e1b088499dd609b81a91906Brian Paul
838a3f137094cd965d27e1b088499dd609b81a91906Brian Paul
839a3f137094cd965d27e1b088499dd609b81a91906Brian Paul/**
840a3f137094cd965d27e1b088499dd609b81a91906Brian Paul * Return pointer to the specified proxy texture image.
841a3f137094cd965d27e1b088499dd609b81a91906Brian Paul * Note that proxy textures are per-context, not per-texture unit.
842a3f137094cd965d27e1b088499dd609b81a91906Brian Paul * \return pointer to texture image or NULL if invalid target, invalid
843a3f137094cd965d27e1b088499dd609b81a91906Brian Paul *         level, or out of memory.
844a3f137094cd965d27e1b088499dd609b81a91906Brian Paul */
8458a935d71ff4ded6f3cdd8e06dcd1a5e3de3d3cc0Brian Paulstatic struct gl_texture_image *
8468a935d71ff4ded6f3cdd8e06dcd1a5e3de3d3cc0Brian Paulget_proxy_tex_image(struct gl_context *ctx, GLenum target, GLint level)
847a3f137094cd965d27e1b088499dd609b81a91906Brian Paul{
848a3f137094cd965d27e1b088499dd609b81a91906Brian Paul   struct gl_texture_image *texImage;
84980684649a6d01f0e0517b14f61cbcad6fa101929Brian Paul   GLuint texIndex;
850a3f137094cd965d27e1b088499dd609b81a91906Brian Paul
85115357947226c5630bf546b0deec86f7e994de76aBrian Paul   if (level < 0)
852a3f137094cd965d27e1b088499dd609b81a91906Brian Paul      return NULL;
853a3f137094cd965d27e1b088499dd609b81a91906Brian Paul
854a3f137094cd965d27e1b088499dd609b81a91906Brian Paul   switch (target) {
855a3f137094cd965d27e1b088499dd609b81a91906Brian Paul   case GL_PROXY_TEXTURE_1D:
856a3f137094cd965d27e1b088499dd609b81a91906Brian Paul      if (level >= ctx->Const.MaxTextureLevels)
857a3f137094cd965d27e1b088499dd609b81a91906Brian Paul         return NULL;
85880684649a6d01f0e0517b14f61cbcad6fa101929Brian Paul      texIndex = TEXTURE_1D_INDEX;
85980684649a6d01f0e0517b14f61cbcad6fa101929Brian Paul      break;
860a3f137094cd965d27e1b088499dd609b81a91906Brian Paul   case GL_PROXY_TEXTURE_2D:
861a3f137094cd965d27e1b088499dd609b81a91906Brian Paul      if (level >= ctx->Const.MaxTextureLevels)
862a3f137094cd965d27e1b088499dd609b81a91906Brian Paul         return NULL;
86380684649a6d01f0e0517b14f61cbcad6fa101929Brian Paul      texIndex = TEXTURE_2D_INDEX;
86480684649a6d01f0e0517b14f61cbcad6fa101929Brian Paul      break;
865a3f137094cd965d27e1b088499dd609b81a91906Brian Paul   case GL_PROXY_TEXTURE_3D:
866a3f137094cd965d27e1b088499dd609b81a91906Brian Paul      if (level >= ctx->Const.Max3DTextureLevels)
867a3f137094cd965d27e1b088499dd609b81a91906Brian Paul         return NULL;
86880684649a6d01f0e0517b14f61cbcad6fa101929Brian Paul      texIndex = TEXTURE_3D_INDEX;
86980684649a6d01f0e0517b14f61cbcad6fa101929Brian Paul      break;
870a3f137094cd965d27e1b088499dd609b81a91906Brian Paul   case GL_PROXY_TEXTURE_CUBE_MAP:
871a3f137094cd965d27e1b088499dd609b81a91906Brian Paul      if (level >= ctx->Const.MaxCubeTextureLevels)
872a3f137094cd965d27e1b088499dd609b81a91906Brian Paul         return NULL;
87380684649a6d01f0e0517b14f61cbcad6fa101929Brian Paul      texIndex = TEXTURE_CUBE_INDEX;
87480684649a6d01f0e0517b14f61cbcad6fa101929Brian Paul      break;
875a3f137094cd965d27e1b088499dd609b81a91906Brian Paul   case GL_PROXY_TEXTURE_RECTANGLE_NV:
876a3f137094cd965d27e1b088499dd609b81a91906Brian Paul      if (level > 0)
877a3f137094cd965d27e1b088499dd609b81a91906Brian Paul         return NULL;
87880684649a6d01f0e0517b14f61cbcad6fa101929Brian Paul      texIndex = TEXTURE_RECT_INDEX;
87980684649a6d01f0e0517b14f61cbcad6fa101929Brian Paul      break;
880bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick   case GL_PROXY_TEXTURE_1D_ARRAY_EXT:
881bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick      if (level >= ctx->Const.MaxTextureLevels)
882bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick         return NULL;
88380684649a6d01f0e0517b14f61cbcad6fa101929Brian Paul      texIndex = TEXTURE_1D_ARRAY_INDEX;
88480684649a6d01f0e0517b14f61cbcad6fa101929Brian Paul      break;
885bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick   case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
886bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick      if (level >= ctx->Const.MaxTextureLevels)
887bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick         return NULL;
88880684649a6d01f0e0517b14f61cbcad6fa101929Brian Paul      texIndex = TEXTURE_2D_ARRAY_INDEX;
88980684649a6d01f0e0517b14f61cbcad6fa101929Brian Paul      break;
890a3f137094cd965d27e1b088499dd609b81a91906Brian Paul   default:
891a3f137094cd965d27e1b088499dd609b81a91906Brian Paul      return NULL;
892a3f137094cd965d27e1b088499dd609b81a91906Brian Paul   }
89380684649a6d01f0e0517b14f61cbcad6fa101929Brian Paul
89480684649a6d01f0e0517b14f61cbcad6fa101929Brian Paul   texImage = ctx->Texture.ProxyTex[texIndex]->Image[0][level];
89580684649a6d01f0e0517b14f61cbcad6fa101929Brian Paul   if (!texImage) {
89680684649a6d01f0e0517b14f61cbcad6fa101929Brian Paul      texImage = ctx->Driver.NewTextureImage(ctx);
89780684649a6d01f0e0517b14f61cbcad6fa101929Brian Paul      if (!texImage) {
89880684649a6d01f0e0517b14f61cbcad6fa101929Brian Paul         _mesa_error(ctx, GL_OUT_OF_MEMORY, "proxy texture allocation");
89980684649a6d01f0e0517b14f61cbcad6fa101929Brian Paul         return NULL;
90080684649a6d01f0e0517b14f61cbcad6fa101929Brian Paul      }
90180684649a6d01f0e0517b14f61cbcad6fa101929Brian Paul      ctx->Texture.ProxyTex[texIndex]->Image[0][level] = texImage;
90280684649a6d01f0e0517b14f61cbcad6fa101929Brian Paul      /* Set the 'back' pointer */
90380684649a6d01f0e0517b14f61cbcad6fa101929Brian Paul      texImage->TexObject = ctx->Texture.ProxyTex[texIndex];
90480684649a6d01f0e0517b14f61cbcad6fa101929Brian Paul   }
90580684649a6d01f0e0517b14f61cbcad6fa101929Brian Paul   return texImage;
906a3f137094cd965d27e1b088499dd609b81a91906Brian Paul}
907a3f137094cd965d27e1b088499dd609b81a91906Brian Paul
908a3f137094cd965d27e1b088499dd609b81a91906Brian Paul
9096dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/**
9106dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Get the maximum number of allowed mipmap levels.
9116dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell *
9126dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param ctx GL context.
9136dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param target texture target.
9146dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell *
9156dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \return the maximum number of allowed mipmap levels for the given
9166dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * texture target, or zero if passed a bad target.
9176dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell *
9186dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \sa gl_constants.
919ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul */
920ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian PaulGLint
921f9995b30756140724f41daf963fa06167912be7fKristian Høgsberg_mesa_max_texture_levels(struct gl_context *ctx, GLenum target)
922ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul{
923ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul   switch (target) {
924ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul   case GL_TEXTURE_1D:
925ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul   case GL_PROXY_TEXTURE_1D:
926ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul   case GL_TEXTURE_2D:
927ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul   case GL_PROXY_TEXTURE_2D:
928ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul      return ctx->Const.MaxTextureLevels;
929ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul   case GL_TEXTURE_3D:
930ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul   case GL_PROXY_TEXTURE_3D:
931ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul      return ctx->Const.Max3DTextureLevels;
93258d11524dace34f6e9e513b23eaeb098d1eafd0bKenneth Graunke   case GL_TEXTURE_CUBE_MAP:
933ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul   case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
934ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul   case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
935ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul   case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
936ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul   case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
937ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul   case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
938ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul   case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
939ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul   case GL_PROXY_TEXTURE_CUBE_MAP_ARB:
940fe988d786c4076bfbf410b84085d8c1115baa489Brian Paul      return ctx->Extensions.ARB_texture_cube_map
941fe988d786c4076bfbf410b84085d8c1115baa489Brian Paul         ? ctx->Const.MaxCubeTextureLevels : 0;
942ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul   case GL_TEXTURE_RECTANGLE_NV:
943ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul   case GL_PROXY_TEXTURE_RECTANGLE_NV:
944fe988d786c4076bfbf410b84085d8c1115baa489Brian Paul      return ctx->Extensions.NV_texture_rectangle ? 1 : 0;
945fe988d786c4076bfbf410b84085d8c1115baa489Brian Paul   case GL_TEXTURE_1D_ARRAY_EXT:
946fe988d786c4076bfbf410b84085d8c1115baa489Brian Paul   case GL_PROXY_TEXTURE_1D_ARRAY_EXT:
947fe988d786c4076bfbf410b84085d8c1115baa489Brian Paul   case GL_TEXTURE_2D_ARRAY_EXT:
948fe988d786c4076bfbf410b84085d8c1115baa489Brian Paul   case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
949f2dd11817a66b6254e1533398dc53f1bc51953beBrian Paul      return (ctx->Extensions.MESA_texture_array ||
950f2dd11817a66b6254e1533398dc53f1bc51953beBrian Paul              ctx->Extensions.EXT_texture_array)
951fe988d786c4076bfbf410b84085d8c1115baa489Brian Paul         ? ctx->Const.MaxTextureLevels : 0;
952874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul   case GL_TEXTURE_BUFFER:
953bb4a39ec951a696efa07a120c9446b677703ababEric Anholt      return _mesa_is_desktop_gl(ctx)
954bb4a39ec951a696efa07a120c9446b677703ababEric Anholt         && ctx->Extensions.ARB_texture_buffer_object
955bb4a39ec951a696efa07a120c9446b677703ababEric Anholt         ? 1 : 0;
9560c87f16817ff0bf1f05e0d634944fd47b097faeeChia-I Wu   case GL_TEXTURE_EXTERNAL_OES:
957874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul      /* fall-through */
958ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul   default:
959ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul      return 0; /* bad target */
960ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul   }
961ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul}
962ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul
963ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul
96483e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul/**
9655e3733fadf08178fca7c9f20a0f4783f940383aaBrian Paul * Return number of dimensions per mipmap level for the given texture target.
96683e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul */
967a988ddf3798e0ec568704e4a72b46030079c7e45Dave AirlieGLint
968a988ddf3798e0ec568704e4a72b46030079c7e45Dave Airlie_mesa_get_texture_dimensions(GLenum target)
96983e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul{
97083e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul   switch (target) {
97183e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul   case GL_TEXTURE_1D:
97283e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul   case GL_PROXY_TEXTURE_1D:
97383e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul      return 1;
97483e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul   case GL_TEXTURE_2D:
97583e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul   case GL_TEXTURE_RECTANGLE:
97683e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul   case GL_TEXTURE_CUBE_MAP:
97783e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul   case GL_PROXY_TEXTURE_2D:
97883e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul   case GL_PROXY_TEXTURE_RECTANGLE:
97983e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul   case GL_PROXY_TEXTURE_CUBE_MAP:
98083e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul   case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
98183e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul   case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
98283e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul   case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
98383e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul   case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
98483e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul   case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
98583e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul   case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
9865e3733fadf08178fca7c9f20a0f4783f940383aaBrian Paul   case GL_TEXTURE_1D_ARRAY:
9875e3733fadf08178fca7c9f20a0f4783f940383aaBrian Paul   case GL_PROXY_TEXTURE_1D_ARRAY:
9880c87f16817ff0bf1f05e0d634944fd47b097faeeChia-I Wu   case GL_TEXTURE_EXTERNAL_OES:
98983e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul      return 2;
99083e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul   case GL_TEXTURE_3D:
99183e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul   case GL_PROXY_TEXTURE_3D:
9925e3733fadf08178fca7c9f20a0f4783f940383aaBrian Paul   case GL_TEXTURE_2D_ARRAY:
9935e3733fadf08178fca7c9f20a0f4783f940383aaBrian Paul   case GL_PROXY_TEXTURE_2D_ARRAY:
99483e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul      return 3;
995874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul   case GL_TEXTURE_BUFFER:
996874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul      /* fall-through */
99783e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul   default:
99883e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul      _mesa_problem(NULL, "invalid target 0x%x in get_texture_dimensions()",
99983e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul                    target);
100083e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul      return 2;
100183e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul   }
100283e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul}
100383e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul
100483e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul
1005efa6b5e42b427c1ec9f2d636d280c84e7da71346Marek Olšák/**
1006efa6b5e42b427c1ec9f2d636d280c84e7da71346Marek Olšák * Return the maximum number of mipmap levels for the given target
1007efa6b5e42b427c1ec9f2d636d280c84e7da71346Marek Olšák * and the dimensions.
1008efa6b5e42b427c1ec9f2d636d280c84e7da71346Marek Olšák * The dimensions are expected not to include the border.
1009efa6b5e42b427c1ec9f2d636d280c84e7da71346Marek Olšák */
1010efa6b5e42b427c1ec9f2d636d280c84e7da71346Marek OlšákGLsizei
1011efa6b5e42b427c1ec9f2d636d280c84e7da71346Marek Olšák_mesa_get_tex_max_num_levels(GLenum target, GLsizei width, GLsizei height,
1012efa6b5e42b427c1ec9f2d636d280c84e7da71346Marek Olšák                             GLsizei depth)
1013efa6b5e42b427c1ec9f2d636d280c84e7da71346Marek Olšák{
1014efa6b5e42b427c1ec9f2d636d280c84e7da71346Marek Olšák   GLsizei size;
1015efa6b5e42b427c1ec9f2d636d280c84e7da71346Marek Olšák
1016efa6b5e42b427c1ec9f2d636d280c84e7da71346Marek Olšák   switch (target) {
1017efa6b5e42b427c1ec9f2d636d280c84e7da71346Marek Olšák   case GL_TEXTURE_1D:
1018efa6b5e42b427c1ec9f2d636d280c84e7da71346Marek Olšák   case GL_TEXTURE_1D_ARRAY:
1019efa6b5e42b427c1ec9f2d636d280c84e7da71346Marek Olšák      size = width;
1020efa6b5e42b427c1ec9f2d636d280c84e7da71346Marek Olšák      break;
1021efa6b5e42b427c1ec9f2d636d280c84e7da71346Marek Olšák   case GL_TEXTURE_CUBE_MAP:
1022efa6b5e42b427c1ec9f2d636d280c84e7da71346Marek Olšák   case GL_TEXTURE_CUBE_MAP_ARRAY:
1023efa6b5e42b427c1ec9f2d636d280c84e7da71346Marek Olšák      ASSERT(width == height);
1024efa6b5e42b427c1ec9f2d636d280c84e7da71346Marek Olšák      size = width;
1025efa6b5e42b427c1ec9f2d636d280c84e7da71346Marek Olšák      break;
1026efa6b5e42b427c1ec9f2d636d280c84e7da71346Marek Olšák   case GL_TEXTURE_2D:
1027efa6b5e42b427c1ec9f2d636d280c84e7da71346Marek Olšák   case GL_TEXTURE_2D_ARRAY:
1028efa6b5e42b427c1ec9f2d636d280c84e7da71346Marek Olšák      size = MAX2(width, height);
1029efa6b5e42b427c1ec9f2d636d280c84e7da71346Marek Olšák      break;
1030efa6b5e42b427c1ec9f2d636d280c84e7da71346Marek Olšák   case GL_TEXTURE_3D:
1031efa6b5e42b427c1ec9f2d636d280c84e7da71346Marek Olšák      size = MAX3(width, height, depth);
1032efa6b5e42b427c1ec9f2d636d280c84e7da71346Marek Olšák      break;
1033efa6b5e42b427c1ec9f2d636d280c84e7da71346Marek Olšák   case GL_TEXTURE_RECTANGLE:
1034efa6b5e42b427c1ec9f2d636d280c84e7da71346Marek Olšák      return 1;
1035efa6b5e42b427c1ec9f2d636d280c84e7da71346Marek Olšák   default:
1036efa6b5e42b427c1ec9f2d636d280c84e7da71346Marek Olšák      assert(0);
1037efa6b5e42b427c1ec9f2d636d280c84e7da71346Marek Olšák      return 1;
1038efa6b5e42b427c1ec9f2d636d280c84e7da71346Marek Olšák   }
1039efa6b5e42b427c1ec9f2d636d280c84e7da71346Marek Olšák
1040efa6b5e42b427c1ec9f2d636d280c84e7da71346Marek Olšák   return _mesa_logbase2(size) + 1;
1041efa6b5e42b427c1ec9f2d636d280c84e7da71346Marek Olšák}
104283e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul
1043fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul
104462c734f49948df7aeef55ad23a6664cbf3e11533Brian Paul#if 000 /* not used anymore */
1045f93b3dd69e744cf1dd6b102a11cdb07c2df4a967Brian Paul/*
1046afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg * glTexImage[123]D can accept a NULL image pointer.  In this case we
1047afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg * create a texture image with unspecified image contents per the OpenGL
10488e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul * spec.
1049afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg */
10508e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paulstatic GLubyte *
10518e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paulmake_null_texture(GLint width, GLint height, GLint depth, GLenum format)
1052afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg{
10538e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul   const GLint components = _mesa_components_in_format(format);
10548e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul   const GLint numPixels = width * height * depth;
10558e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul   GLubyte *data = (GLubyte *) MALLOC(numPixels * components * sizeof(GLubyte));
1056afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
1057a1503b00f863a48a517939a42d512f9cfe77f79cBrian Paul#ifdef DEBUG
1058afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   /*
1059afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg    * Let's see if anyone finds this.  If glTexImage2D() is called with
1060afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg    * a NULL image pointer then load the texture image with something
1061afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg    * interesting instead of leaving it indeterminate.
1062afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg    */
10638e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul   if (data) {
106465d54604c387dca986c876e811362d8e8517dcacBrian Paul      static const char message[8][32] = {
1065afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         "   X   X  XXXXX   XXX     X    ",
1066afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         "   XX XX  X      X   X   X X   ",
1067afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         "   X X X  X      X      X   X  ",
1068afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         "   X   X  XXXX    XXX   XXXXX  ",
1069afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         "   X   X  X          X  X   X  ",
1070afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         "   X   X  X      X   X  X   X  ",
1071afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         "   X   X  XXXXX   XXX   X   X  ",
1072afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         "                               "
1073afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      };
1074afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
10758e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul      GLubyte *imgPtr = data;
10768e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul      GLint h, i, j, k;
10778e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul      for (h = 0; h < depth; h++) {
10788e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul         for (i = 0; i < height; i++) {
10798e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul            GLint srcRow = 7 - (i % 8);
10808e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul            for (j = 0; j < width; j++) {
10818e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul               GLint srcCol = j % 32;
10828e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul               GLubyte texel = (message[srcRow][srcCol]=='X') ? 255 : 70;
10838e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul               for (k = 0; k < components; k++) {
10848e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul                  *imgPtr++ = texel;
10858e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul               }
1086afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg            }
1087afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         }
1088afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      }
1089afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   }
1090a1503b00f863a48a517939a42d512f9cfe77f79cBrian Paul#endif
10918e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul
10928e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul   return data;
1093afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg}
109462c734f49948df7aeef55ad23a6664cbf3e11533Brian Paul#endif
1095afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
1096afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
1097afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
1098738318bb75dea8dac4465f53850987f6062a732dBrian Paul/**
1099fc144728c7e33e53f7a4a4486eb81fddff156758Brian Paul * Set the size and format-related fields of a gl_texture_image struct
1100fc144728c7e33e53f7a4a4486eb81fddff156758Brian Paul * to zero.  This is used when a proxy texture test fails.
11019c27278acfb786c8f2fc591eef9ed0c25135bcf0Brian Paul */
11029c27278acfb786c8f2fc591eef9ed0c25135bcf0Brian Paulstatic void
1103f378ab825c0c74aab263e7dec30194eead22c288Brian Paulclear_teximage_fields(struct gl_texture_image *img)
11049c27278acfb786c8f2fc591eef9ed0c25135bcf0Brian Paul{
11059c27278acfb786c8f2fc591eef9ed0c25135bcf0Brian Paul   ASSERT(img);
1106a9fc8ba756dd25a07dc19058fe60f65bda82a055Brian Paul   img->_BaseFormat = 0;
1107a9fc8ba756dd25a07dc19058fe60f65bda82a055Brian Paul   img->InternalFormat = 0;
11089c27278acfb786c8f2fc591eef9ed0c25135bcf0Brian Paul   img->Border = 0;
11099c27278acfb786c8f2fc591eef9ed0c25135bcf0Brian Paul   img->Width = 0;
11109c27278acfb786c8f2fc591eef9ed0c25135bcf0Brian Paul   img->Height = 0;
11119c27278acfb786c8f2fc591eef9ed0c25135bcf0Brian Paul   img->Depth = 0;
11129c27278acfb786c8f2fc591eef9ed0c25135bcf0Brian Paul   img->Width2 = 0;
11139c27278acfb786c8f2fc591eef9ed0c25135bcf0Brian Paul   img->Height2 = 0;
11149c27278acfb786c8f2fc591eef9ed0c25135bcf0Brian Paul   img->Depth2 = 0;
11159c27278acfb786c8f2fc591eef9ed0c25135bcf0Brian Paul   img->WidthLog2 = 0;
11169c27278acfb786c8f2fc591eef9ed0c25135bcf0Brian Paul   img->HeightLog2 = 0;
11179c27278acfb786c8f2fc591eef9ed0c25135bcf0Brian Paul   img->DepthLog2 = 0;
11181f7c914ad0beea8a29c1a171c7cd1a12f2efe0faBrian Paul   img->TexFormat = MESA_FORMAT_NONE;
11199c27278acfb786c8f2fc591eef9ed0c25135bcf0Brian Paul}
11209c27278acfb786c8f2fc591eef9ed0c25135bcf0Brian Paul
11219c27278acfb786c8f2fc591eef9ed0c25135bcf0Brian Paul
11226dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/**
11236628bc9cff74a6d524165e809f73eabc85ba34b5Brian Paul * Initialize basic fields of the gl_texture_image struct.
11246dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell *
11256dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param ctx GL context.
11266dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param img texture image structure to be initialized.
11276dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param width image width.
11286dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param height image height.
11296dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param depth image depth.
11306dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param border image border.
11316dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param internalFormat internal format.
113283e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul * \param format  the actual hardware format (one of MESA_FORMAT_*)
11336dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell *
11346dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Fills in the fields of \p img with the given information.
1135887e2cf01a99f7fe1b7c94320b7bdbbf0d6ad2f8Brian Paul * Note: width, height and depth include the border.
11366628bc9cff74a6d524165e809f73eabc85ba34b5Brian Paul */
11373893e638e6521b9c070e01c0b31d22754ff97a88Brian Paulvoid
1138892a2542a3f0753a7064c710b96f077dd5490624Brian Paul_mesa_init_teximage_fields(struct gl_context *ctx,
11393893e638e6521b9c070e01c0b31d22754ff97a88Brian Paul                           struct gl_texture_image *img,
11403893e638e6521b9c070e01c0b31d22754ff97a88Brian Paul                           GLsizei width, GLsizei height, GLsizei depth,
114183e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul                           GLint border, GLenum internalFormat,
114283e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul                           gl_format format)
11436628bc9cff74a6d524165e809f73eabc85ba34b5Brian Paul{
1144b5c409363cfd144b1caad50b77ccc7591de42af2Anuj Phogat   GLenum target;
11456628bc9cff74a6d524165e809f73eabc85ba34b5Brian Paul   ASSERT(img);
11468bc00c2047f69d8cd37d4fd70256850445b0fa1dBrian Paul   ASSERT(width >= 0);
11478bc00c2047f69d8cd37d4fd70256850445b0fa1dBrian Paul   ASSERT(height >= 0);
11488bc00c2047f69d8cd37d4fd70256850445b0fa1dBrian Paul   ASSERT(depth >= 0);
1149b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul
1150b5c409363cfd144b1caad50b77ccc7591de42af2Anuj Phogat   target = img->TexObject->Target;
1151a9fc8ba756dd25a07dc19058fe60f65bda82a055Brian Paul   img->_BaseFormat = _mesa_base_tex_format( ctx, internalFormat );
1152a9fc8ba756dd25a07dc19058fe60f65bda82a055Brian Paul   ASSERT(img->_BaseFormat > 0);
1153a9fc8ba756dd25a07dc19058fe60f65bda82a055Brian Paul   img->InternalFormat = internalFormat;
11546628bc9cff74a6d524165e809f73eabc85ba34b5Brian Paul   img->Border = border;
11556628bc9cff74a6d524165e809f73eabc85ba34b5Brian Paul   img->Width = width;
11566628bc9cff74a6d524165e809f73eabc85ba34b5Brian Paul   img->Height = height;
11576628bc9cff74a6d524165e809f73eabc85ba34b5Brian Paul   img->Depth = depth;
1158c813b545ab4726fc5030f123ec6255224d64ad82Brian
11594d0b7618cb3ada3b13e9e9b650ace34f5131e318Brian Paul   img->Width2 = width - 2 * border;   /* == 1 << img->WidthLog2; */
1160b537f54b3a2bd47ec861f5c432c49529684eef21Roland Scheidegger   img->WidthLog2 = _mesa_logbase2(img->Width2);
1161c813b545ab4726fc5030f123ec6255224d64ad82Brian
1162b5c409363cfd144b1caad50b77ccc7591de42af2Anuj Phogat   switch(target) {
1163b5c409363cfd144b1caad50b77ccc7591de42af2Anuj Phogat   case GL_TEXTURE_1D:
1164b5c409363cfd144b1caad50b77ccc7591de42af2Anuj Phogat   case GL_TEXTURE_BUFFER:
1165b5c409363cfd144b1caad50b77ccc7591de42af2Anuj Phogat   case GL_PROXY_TEXTURE_1D:
1166b5c409363cfd144b1caad50b77ccc7591de42af2Anuj Phogat      if (height == 0)
1167b5c409363cfd144b1caad50b77ccc7591de42af2Anuj Phogat         img->Height2 = 0;
1168b5c409363cfd144b1caad50b77ccc7591de42af2Anuj Phogat      else
1169b5c409363cfd144b1caad50b77ccc7591de42af2Anuj Phogat         img->Height2 = 1;
11706628bc9cff74a6d524165e809f73eabc85ba34b5Brian Paul      img->HeightLog2 = 0;
1171b5c409363cfd144b1caad50b77ccc7591de42af2Anuj Phogat      if (depth == 0)
1172b5c409363cfd144b1caad50b77ccc7591de42af2Anuj Phogat         img->Depth2 = 0;
1173b5c409363cfd144b1caad50b77ccc7591de42af2Anuj Phogat      else
1174b5c409363cfd144b1caad50b77ccc7591de42af2Anuj Phogat         img->Depth2 = 1;
1175b5c409363cfd144b1caad50b77ccc7591de42af2Anuj Phogat      img->DepthLog2 = 0;
1176b5c409363cfd144b1caad50b77ccc7591de42af2Anuj Phogat      break;
1177b5c409363cfd144b1caad50b77ccc7591de42af2Anuj Phogat   case GL_TEXTURE_1D_ARRAY:
1178b5c409363cfd144b1caad50b77ccc7591de42af2Anuj Phogat   case GL_PROXY_TEXTURE_1D_ARRAY:
1179b5c409363cfd144b1caad50b77ccc7591de42af2Anuj Phogat      img->Height2 = height; /* no border */
1180b5c409363cfd144b1caad50b77ccc7591de42af2Anuj Phogat      img->HeightLog2 = 0; /* not used */
1181b5c409363cfd144b1caad50b77ccc7591de42af2Anuj Phogat      if (depth == 0)
1182b5c409363cfd144b1caad50b77ccc7591de42af2Anuj Phogat         img->Depth2 = 0;
1183b5c409363cfd144b1caad50b77ccc7591de42af2Anuj Phogat      else
1184b5c409363cfd144b1caad50b77ccc7591de42af2Anuj Phogat         img->Depth2 = 1;
1185b5c409363cfd144b1caad50b77ccc7591de42af2Anuj Phogat      img->DepthLog2 = 0;
1186b5c409363cfd144b1caad50b77ccc7591de42af2Anuj Phogat      break;
1187b5c409363cfd144b1caad50b77ccc7591de42af2Anuj Phogat   case GL_TEXTURE_2D:
1188b5c409363cfd144b1caad50b77ccc7591de42af2Anuj Phogat   case GL_TEXTURE_RECTANGLE:
1189b5c409363cfd144b1caad50b77ccc7591de42af2Anuj Phogat   case GL_TEXTURE_CUBE_MAP:
1190b5c409363cfd144b1caad50b77ccc7591de42af2Anuj Phogat   case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
1191b5c409363cfd144b1caad50b77ccc7591de42af2Anuj Phogat   case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
1192b5c409363cfd144b1caad50b77ccc7591de42af2Anuj Phogat   case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
1193b5c409363cfd144b1caad50b77ccc7591de42af2Anuj Phogat   case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
1194b5c409363cfd144b1caad50b77ccc7591de42af2Anuj Phogat   case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
1195b5c409363cfd144b1caad50b77ccc7591de42af2Anuj Phogat   case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
1196b5c409363cfd144b1caad50b77ccc7591de42af2Anuj Phogat   case GL_TEXTURE_EXTERNAL_OES:
1197b5c409363cfd144b1caad50b77ccc7591de42af2Anuj Phogat   case GL_PROXY_TEXTURE_2D:
1198b5c409363cfd144b1caad50b77ccc7591de42af2Anuj Phogat   case GL_PROXY_TEXTURE_RECTANGLE:
1199b5c409363cfd144b1caad50b77ccc7591de42af2Anuj Phogat   case GL_PROXY_TEXTURE_CUBE_MAP:
1200c813b545ab4726fc5030f123ec6255224d64ad82Brian      img->Height2 = height - 2 * border; /* == 1 << img->HeightLog2; */
1201b537f54b3a2bd47ec861f5c432c49529684eef21Roland Scheidegger      img->HeightLog2 = _mesa_logbase2(img->Height2);
1202b5c409363cfd144b1caad50b77ccc7591de42af2Anuj Phogat      if (depth == 0)
1203b5c409363cfd144b1caad50b77ccc7591de42af2Anuj Phogat         img->Depth2 = 0;
1204b5c409363cfd144b1caad50b77ccc7591de42af2Anuj Phogat      else
1205b5c409363cfd144b1caad50b77ccc7591de42af2Anuj Phogat         img->Depth2 = 1;
12066628bc9cff74a6d524165e809f73eabc85ba34b5Brian Paul      img->DepthLog2 = 0;
1207b5c409363cfd144b1caad50b77ccc7591de42af2Anuj Phogat      break;
1208b5c409363cfd144b1caad50b77ccc7591de42af2Anuj Phogat   case GL_TEXTURE_2D_ARRAY:
1209b5c409363cfd144b1caad50b77ccc7591de42af2Anuj Phogat   case GL_PROXY_TEXTURE_2D_ARRAY:
1210b5c409363cfd144b1caad50b77ccc7591de42af2Anuj Phogat      img->Height2 = height - 2 * border; /* == 1 << img->HeightLog2; */
1211b5c409363cfd144b1caad50b77ccc7591de42af2Anuj Phogat      img->HeightLog2 = _mesa_logbase2(img->Height2);
1212b5c409363cfd144b1caad50b77ccc7591de42af2Anuj Phogat      img->Depth2 = depth; /* no border */
1213b5c409363cfd144b1caad50b77ccc7591de42af2Anuj Phogat      img->DepthLog2 = 0; /* not used */
1214b5c409363cfd144b1caad50b77ccc7591de42af2Anuj Phogat      break;
1215b5c409363cfd144b1caad50b77ccc7591de42af2Anuj Phogat   case GL_TEXTURE_3D:
1216b5c409363cfd144b1caad50b77ccc7591de42af2Anuj Phogat   case GL_PROXY_TEXTURE_3D:
1217b5c409363cfd144b1caad50b77ccc7591de42af2Anuj Phogat      img->Height2 = height - 2 * border; /* == 1 << img->HeightLog2; */
1218b5c409363cfd144b1caad50b77ccc7591de42af2Anuj Phogat      img->HeightLog2 = _mesa_logbase2(img->Height2);
1219c813b545ab4726fc5030f123ec6255224d64ad82Brian      img->Depth2 = depth - 2 * border;   /* == 1 << img->DepthLog2; */
1220b537f54b3a2bd47ec861f5c432c49529684eef21Roland Scheidegger      img->DepthLog2 = _mesa_logbase2(img->Depth2);
1221b5c409363cfd144b1caad50b77ccc7591de42af2Anuj Phogat      break;
1222b5c409363cfd144b1caad50b77ccc7591de42af2Anuj Phogat   default:
1223b5c409363cfd144b1caad50b77ccc7591de42af2Anuj Phogat      _mesa_problem(NULL, "invalid target 0x%x in _mesa_init_teximage_fields()",
1224b5c409363cfd144b1caad50b77ccc7591de42af2Anuj Phogat                    target);
1225c813b545ab4726fc5030f123ec6255224d64ad82Brian   }
1226c813b545ab4726fc5030f123ec6255224d64ad82Brian
1227efa6b5e42b427c1ec9f2d636d280c84e7da71346Marek Olšák   img->MaxNumLevels =
1228efa6b5e42b427c1ec9f2d636d280c84e7da71346Marek Olšák      _mesa_get_tex_max_num_levels(target,
1229efa6b5e42b427c1ec9f2d636d280c84e7da71346Marek Olšák                                   img->Width2, img->Height2, img->Depth2);
123083e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul   img->TexFormat = format;
12316628bc9cff74a6d524165e809f73eabc85ba34b5Brian Paul}
12326628bc9cff74a6d524165e809f73eabc85ba34b5Brian Paul
12336628bc9cff74a6d524165e809f73eabc85ba34b5Brian Paul
1234738318bb75dea8dac4465f53850987f6062a732dBrian Paul/**
123542b6b067ac68ac1309d0570613bea4a88f745559Chia-I Wu * Free and clear fields of the gl_texture_image struct.
123642b6b067ac68ac1309d0570613bea4a88f745559Chia-I Wu *
123742b6b067ac68ac1309d0570613bea4a88f745559Chia-I Wu * \param ctx GL context.
123842b6b067ac68ac1309d0570613bea4a88f745559Chia-I Wu * \param texImage texture image structure to be cleared.
123942b6b067ac68ac1309d0570613bea4a88f745559Chia-I Wu *
124042b6b067ac68ac1309d0570613bea4a88f745559Chia-I Wu * After the call, \p texImage will have no data associated with it.  Its
124142b6b067ac68ac1309d0570613bea4a88f745559Chia-I Wu * fields are cleared so that its parent object will test incomplete.
124242b6b067ac68ac1309d0570613bea4a88f745559Chia-I Wu */
124342b6b067ac68ac1309d0570613bea4a88f745559Chia-I Wuvoid
12443c59febf05e6af80d63e5b9a478a11b275ac429cBrian Paul_mesa_clear_texture_image(struct gl_context *ctx,
12453c59febf05e6af80d63e5b9a478a11b275ac429cBrian Paul                          struct gl_texture_image *texImage)
124642b6b067ac68ac1309d0570613bea4a88f745559Chia-I Wu{
12470bb29949ba8a9e5a15dc0640dbb0a4e7990a1d57Eric Anholt   ctx->Driver.FreeTextureImageBuffer(ctx, texImage);
124842b6b067ac68ac1309d0570613bea4a88f745559Chia-I Wu   clear_teximage_fields(texImage);
124942b6b067ac68ac1309d0570613bea4a88f745559Chia-I Wu}
125042b6b067ac68ac1309d0570613bea4a88f745559Chia-I Wu
125142b6b067ac68ac1309d0570613bea4a88f745559Chia-I Wu
125242b6b067ac68ac1309d0570613bea4a88f745559Chia-I Wu/**
1253738318bb75dea8dac4465f53850987f6062a732dBrian Paul * This is the fallback for Driver.TestProxyTexImage().  Test the texture
1254738318bb75dea8dac4465f53850987f6062a732dBrian Paul * level, width, height and depth against the ctx->Const limits for textures.
1255738318bb75dea8dac4465f53850987f6062a732dBrian Paul *
1256738318bb75dea8dac4465f53850987f6062a732dBrian Paul * A hardware driver might override this function if, for example, the
1257738318bb75dea8dac4465f53850987f6062a732dBrian Paul * max 3D texture size is 512x512x64 (i.e. not a cube).
1258738318bb75dea8dac4465f53850987f6062a732dBrian Paul *
1259aa328291c5b015e74ebfd9c5cdb39227265b3000Brian * Note that width, height, depth == 0 is not an error.  However, a
1260aa328291c5b015e74ebfd9c5cdb39227265b3000Brian * texture with zero width/height/depth will be considered "incomplete"
1261aa328291c5b015e74ebfd9c5cdb39227265b3000Brian * and texturing will effectively be disabled.
1262aa328291c5b015e74ebfd9c5cdb39227265b3000Brian *
1263738318bb75dea8dac4465f53850987f6062a732dBrian Paul * \param target  one of GL_PROXY_TEXTURE_1D, GL_PROXY_TEXTURE_2D,
1264738318bb75dea8dac4465f53850987f6062a732dBrian Paul *                GL_PROXY_TEXTURE_3D, GL_PROXY_TEXTURE_RECTANGLE_NV,
1265738318bb75dea8dac4465f53850987f6062a732dBrian Paul *                GL_PROXY_TEXTURE_CUBE_MAP_ARB.
1266738318bb75dea8dac4465f53850987f6062a732dBrian Paul * \param level  as passed to glTexImage
1267738318bb75dea8dac4465f53850987f6062a732dBrian Paul * \param internalFormat  as passed to glTexImage
1268738318bb75dea8dac4465f53850987f6062a732dBrian Paul * \param format  as passed to glTexImage
1269738318bb75dea8dac4465f53850987f6062a732dBrian Paul * \param type  as passed to glTexImage
1270738318bb75dea8dac4465f53850987f6062a732dBrian Paul * \param width  as passed to glTexImage
1271738318bb75dea8dac4465f53850987f6062a732dBrian Paul * \param height  as passed to glTexImage
1272738318bb75dea8dac4465f53850987f6062a732dBrian Paul * \param depth  as passed to glTexImage
1273738318bb75dea8dac4465f53850987f6062a732dBrian Paul * \param border  as passed to glTexImage
1274738318bb75dea8dac4465f53850987f6062a732dBrian Paul * \return GL_TRUE if the image is acceptable, GL_FALSE if not acceptable.
1275738318bb75dea8dac4465f53850987f6062a732dBrian Paul */
1276738318bb75dea8dac4465f53850987f6062a732dBrian PaulGLboolean
1277f9995b30756140724f41daf963fa06167912be7fKristian Høgsberg_mesa_test_proxy_teximage(struct gl_context *ctx, GLenum target, GLint level,
1278738318bb75dea8dac4465f53850987f6062a732dBrian Paul                          GLint internalFormat, GLenum format, GLenum type,
1279738318bb75dea8dac4465f53850987f6062a732dBrian Paul                          GLint width, GLint height, GLint depth, GLint border)
1280738318bb75dea8dac4465f53850987f6062a732dBrian Paul{
1281738318bb75dea8dac4465f53850987f6062a732dBrian Paul   GLint maxSize;
12829c27278acfb786c8f2fc591eef9ed0c25135bcf0Brian Paul
1283738318bb75dea8dac4465f53850987f6062a732dBrian Paul   (void) internalFormat;
1284738318bb75dea8dac4465f53850987f6062a732dBrian Paul   (void) format;
1285738318bb75dea8dac4465f53850987f6062a732dBrian Paul   (void) type;
1286738318bb75dea8dac4465f53850987f6062a732dBrian Paul
1287738318bb75dea8dac4465f53850987f6062a732dBrian Paul   switch (target) {
1288738318bb75dea8dac4465f53850987f6062a732dBrian Paul   case GL_PROXY_TEXTURE_1D:
1289c64447f47de168e82086bc2fb483817b82e59cabBrian Paul      if (level >= ctx->Const.MaxTextureLevels)
1290c64447f47de168e82086bc2fb483817b82e59cabBrian Paul         return GL_FALSE;
12916e9baa85a95495e9343ca899a808d264076d67f8Brian Paul      maxSize = 1 << (ctx->Const.MaxTextureLevels - 1); /* level zero size */
12926e9baa85a95495e9343ca899a808d264076d67f8Brian Paul      maxSize >>= level;  /* level size */
12936e9baa85a95495e9343ca899a808d264076d67f8Brian Paul      if (width < 2 * border || width > 2 * border + maxSize)
12946e9baa85a95495e9343ca899a808d264076d67f8Brian Paul         return GL_FALSE;
1295c64447f47de168e82086bc2fb483817b82e59cabBrian Paul      if (!ctx->Extensions.ARB_texture_non_power_of_two) {
1296c64447f47de168e82086bc2fb483817b82e59cabBrian Paul         if (width > 0 && !_mesa_is_pow_two(width - 2 * border))
1297c64447f47de168e82086bc2fb483817b82e59cabBrian Paul            return GL_FALSE;
1298738318bb75dea8dac4465f53850987f6062a732dBrian Paul      }
1299738318bb75dea8dac4465f53850987f6062a732dBrian Paul      return GL_TRUE;
1300c64447f47de168e82086bc2fb483817b82e59cabBrian Paul
1301738318bb75dea8dac4465f53850987f6062a732dBrian Paul   case GL_PROXY_TEXTURE_2D:
13026e9baa85a95495e9343ca899a808d264076d67f8Brian Paul      if (level >= ctx->Const.MaxTextureLevels)
13036e9baa85a95495e9343ca899a808d264076d67f8Brian Paul         return GL_FALSE;
1304738318bb75dea8dac4465f53850987f6062a732dBrian Paul      maxSize = 1 << (ctx->Const.MaxTextureLevels - 1);
13056e9baa85a95495e9343ca899a808d264076d67f8Brian Paul      maxSize >>= level;
1306ea228d97f811092b9ffcb90565184a7a8f089477Anuj Phogat      if (width < 2 * border || width > 2 * border + maxSize)
1307c64447f47de168e82086bc2fb483817b82e59cabBrian Paul         return GL_FALSE;
1308ea228d97f811092b9ffcb90565184a7a8f089477Anuj Phogat      if (height < 2 * border || height > 2 * border + maxSize)
1309738318bb75dea8dac4465f53850987f6062a732dBrian Paul         return GL_FALSE;
1310c64447f47de168e82086bc2fb483817b82e59cabBrian Paul      if (!ctx->Extensions.ARB_texture_non_power_of_two) {
1311c64447f47de168e82086bc2fb483817b82e59cabBrian Paul         if (width > 0 && !_mesa_is_pow_two(width - 2 * border))
1312c64447f47de168e82086bc2fb483817b82e59cabBrian Paul            return GL_FALSE;
1313c64447f47de168e82086bc2fb483817b82e59cabBrian Paul         if (height > 0 && !_mesa_is_pow_two(height - 2 * border))
1314c64447f47de168e82086bc2fb483817b82e59cabBrian Paul            return GL_FALSE;
1315738318bb75dea8dac4465f53850987f6062a732dBrian Paul      }
1316738318bb75dea8dac4465f53850987f6062a732dBrian Paul      return GL_TRUE;
1317c64447f47de168e82086bc2fb483817b82e59cabBrian Paul
1318738318bb75dea8dac4465f53850987f6062a732dBrian Paul   case GL_PROXY_TEXTURE_3D:
13196e9baa85a95495e9343ca899a808d264076d67f8Brian Paul      if (level >= ctx->Const.Max3DTextureLevels)
13206e9baa85a95495e9343ca899a808d264076d67f8Brian Paul         return GL_FALSE;
1321738318bb75dea8dac4465f53850987f6062a732dBrian Paul      maxSize = 1 << (ctx->Const.Max3DTextureLevels - 1);
13226e9baa85a95495e9343ca899a808d264076d67f8Brian Paul      maxSize >>= level;
1323ea228d97f811092b9ffcb90565184a7a8f089477Anuj Phogat      if (width < 2 * border || width > 2 * border + maxSize)
1324c64447f47de168e82086bc2fb483817b82e59cabBrian Paul         return GL_FALSE;
1325ea228d97f811092b9ffcb90565184a7a8f089477Anuj Phogat      if (height < 2 * border || height > 2 * border + maxSize)
1326738318bb75dea8dac4465f53850987f6062a732dBrian Paul         return GL_FALSE;
1327ea228d97f811092b9ffcb90565184a7a8f089477Anuj Phogat      if (depth < 2 * border || depth > 2 * border + maxSize)
1328c64447f47de168e82086bc2fb483817b82e59cabBrian Paul         return GL_FALSE;
1329c64447f47de168e82086bc2fb483817b82e59cabBrian Paul      if (!ctx->Extensions.ARB_texture_non_power_of_two) {
1330c64447f47de168e82086bc2fb483817b82e59cabBrian Paul         if (width > 0 && !_mesa_is_pow_two(width - 2 * border))
1331c64447f47de168e82086bc2fb483817b82e59cabBrian Paul            return GL_FALSE;
1332c64447f47de168e82086bc2fb483817b82e59cabBrian Paul         if (height > 0 && !_mesa_is_pow_two(height - 2 * border))
1333c64447f47de168e82086bc2fb483817b82e59cabBrian Paul            return GL_FALSE;
1334c64447f47de168e82086bc2fb483817b82e59cabBrian Paul         if (depth > 0 && !_mesa_is_pow_two(depth - 2 * border))
1335c64447f47de168e82086bc2fb483817b82e59cabBrian Paul            return GL_FALSE;
1336738318bb75dea8dac4465f53850987f6062a732dBrian Paul      }
1337738318bb75dea8dac4465f53850987f6062a732dBrian Paul      return GL_TRUE;
1338c64447f47de168e82086bc2fb483817b82e59cabBrian Paul
1339738318bb75dea8dac4465f53850987f6062a732dBrian Paul   case GL_PROXY_TEXTURE_RECTANGLE_NV:
13406e9baa85a95495e9343ca899a808d264076d67f8Brian Paul      if (level != 0)
13416e9baa85a95495e9343ca899a808d264076d67f8Brian Paul         return GL_FALSE;
1342c64447f47de168e82086bc2fb483817b82e59cabBrian Paul      maxSize = ctx->Const.MaxTextureRectSize;
1343c64447f47de168e82086bc2fb483817b82e59cabBrian Paul      if (width < 0 || width > maxSize)
1344c64447f47de168e82086bc2fb483817b82e59cabBrian Paul         return GL_FALSE;
1345c64447f47de168e82086bc2fb483817b82e59cabBrian Paul      if (height < 0 || height > maxSize)
1346c64447f47de168e82086bc2fb483817b82e59cabBrian Paul         return GL_FALSE;
1347738318bb75dea8dac4465f53850987f6062a732dBrian Paul      return GL_TRUE;
1348c64447f47de168e82086bc2fb483817b82e59cabBrian Paul
1349738318bb75dea8dac4465f53850987f6062a732dBrian Paul   case GL_PROXY_TEXTURE_CUBE_MAP_ARB:
13506e9baa85a95495e9343ca899a808d264076d67f8Brian Paul      if (level >= ctx->Const.MaxCubeTextureLevels)
13516e9baa85a95495e9343ca899a808d264076d67f8Brian Paul         return GL_FALSE;
1352738318bb75dea8dac4465f53850987f6062a732dBrian Paul      maxSize = 1 << (ctx->Const.MaxCubeTextureLevels - 1);
13536e9baa85a95495e9343ca899a808d264076d67f8Brian Paul      maxSize >>= level;
1354ea228d97f811092b9ffcb90565184a7a8f089477Anuj Phogat      if (width < 2 * border || width > 2 * border + maxSize)
1355c64447f47de168e82086bc2fb483817b82e59cabBrian Paul         return GL_FALSE;
1356ea228d97f811092b9ffcb90565184a7a8f089477Anuj Phogat      if (height < 2 * border || height > 2 * border + maxSize)
1357c64447f47de168e82086bc2fb483817b82e59cabBrian Paul         return GL_FALSE;
1358c64447f47de168e82086bc2fb483817b82e59cabBrian Paul      if (!ctx->Extensions.ARB_texture_non_power_of_two) {
1359c64447f47de168e82086bc2fb483817b82e59cabBrian Paul         if (width > 0 && !_mesa_is_pow_two(width - 2 * border))
1360c64447f47de168e82086bc2fb483817b82e59cabBrian Paul            return GL_FALSE;
1361c64447f47de168e82086bc2fb483817b82e59cabBrian Paul         if (height > 0 && !_mesa_is_pow_two(height - 2 * border))
1362c64447f47de168e82086bc2fb483817b82e59cabBrian Paul            return GL_FALSE;
1363738318bb75dea8dac4465f53850987f6062a732dBrian Paul      }
1364738318bb75dea8dac4465f53850987f6062a732dBrian Paul      return GL_TRUE;
1365c64447f47de168e82086bc2fb483817b82e59cabBrian Paul
1366bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick   case GL_PROXY_TEXTURE_1D_ARRAY_EXT:
13676e9baa85a95495e9343ca899a808d264076d67f8Brian Paul      if (level >= ctx->Const.MaxTextureLevels)
13686e9baa85a95495e9343ca899a808d264076d67f8Brian Paul         return GL_FALSE;
1369bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick      maxSize = 1 << (ctx->Const.MaxTextureLevels - 1);
13706e9baa85a95495e9343ca899a808d264076d67f8Brian Paul      maxSize >>= level;
1371ea228d97f811092b9ffcb90565184a7a8f089477Anuj Phogat      if (width < 2 * border || width > 2 * border + maxSize)
1372bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick         return GL_FALSE;
1373c64447f47de168e82086bc2fb483817b82e59cabBrian Paul      if (height < 1 || height > ctx->Const.MaxArrayTextureLayers)
1374bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick         return GL_FALSE;
1375c64447f47de168e82086bc2fb483817b82e59cabBrian Paul      if (!ctx->Extensions.ARB_texture_non_power_of_two) {
1376c64447f47de168e82086bc2fb483817b82e59cabBrian Paul         if (width > 0 && !_mesa_is_pow_two(width - 2 * border))
1377c64447f47de168e82086bc2fb483817b82e59cabBrian Paul            return GL_FALSE;
1378bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick      }
1379bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick      return GL_TRUE;
1380c64447f47de168e82086bc2fb483817b82e59cabBrian Paul
1381bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick   case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
13826e9baa85a95495e9343ca899a808d264076d67f8Brian Paul      if (level >= ctx->Const.MaxTextureLevels)
13836e9baa85a95495e9343ca899a808d264076d67f8Brian Paul         return GL_FALSE;
1384bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick      maxSize = 1 << (ctx->Const.MaxTextureLevels - 1);
13856e9baa85a95495e9343ca899a808d264076d67f8Brian Paul      maxSize >>= level;
1386ea228d97f811092b9ffcb90565184a7a8f089477Anuj Phogat      if (width < 2 * border || width > 2 * border + maxSize)
1387bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick         return GL_FALSE;
1388ea228d97f811092b9ffcb90565184a7a8f089477Anuj Phogat      if (height < 2 * border || height > 2 * border + maxSize)
1389c64447f47de168e82086bc2fb483817b82e59cabBrian Paul         return GL_FALSE;
1390c64447f47de168e82086bc2fb483817b82e59cabBrian Paul      if (depth < 1 || depth > ctx->Const.MaxArrayTextureLayers)
1391bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick         return GL_FALSE;
1392c64447f47de168e82086bc2fb483817b82e59cabBrian Paul      if (!ctx->Extensions.ARB_texture_non_power_of_two) {
1393c64447f47de168e82086bc2fb483817b82e59cabBrian Paul         if (width > 0 && !_mesa_is_pow_two(width - 2 * border))
1394c64447f47de168e82086bc2fb483817b82e59cabBrian Paul            return GL_FALSE;
1395c64447f47de168e82086bc2fb483817b82e59cabBrian Paul         if (height > 0 && !_mesa_is_pow_two(height - 2 * border))
1396c64447f47de168e82086bc2fb483817b82e59cabBrian Paul            return GL_FALSE;
1397bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick      }
1398bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick      return GL_TRUE;
1399c64447f47de168e82086bc2fb483817b82e59cabBrian Paul
1400738318bb75dea8dac4465f53850987f6062a732dBrian Paul   default:
1401738318bb75dea8dac4465f53850987f6062a732dBrian Paul      _mesa_problem(ctx, "Invalid target in _mesa_test_proxy_teximage");
1402738318bb75dea8dac4465f53850987f6062a732dBrian Paul      return GL_FALSE;
1403738318bb75dea8dac4465f53850987f6062a732dBrian Paul   }
1404738318bb75dea8dac4465f53850987f6062a732dBrian Paul}
1405738318bb75dea8dac4465f53850987f6062a732dBrian Paul
1406738318bb75dea8dac4465f53850987f6062a732dBrian Paul
1407738318bb75dea8dac4465f53850987f6062a732dBrian Paul/**
14084b08f35487fa439fd9ca4d653d3a146c3dc09c1cBrian Paul * Check if the memory used by the texture would exceed the driver's limit.
14094b08f35487fa439fd9ca4d653d3a146c3dc09c1cBrian Paul * This lets us support a max 3D texture size of 8K (for example) but
14104b08f35487fa439fd9ca4d653d3a146c3dc09c1cBrian Paul * prevents allocating a full 8K x 8K x 8K texture.
14114b08f35487fa439fd9ca4d653d3a146c3dc09c1cBrian Paul * XXX this could be rolled into the proxy texture size test (above) but
14124b08f35487fa439fd9ca4d653d3a146c3dc09c1cBrian Paul * we don't have the actual texture internal format at that point.
14134b08f35487fa439fd9ca4d653d3a146c3dc09c1cBrian Paul */
14144b08f35487fa439fd9ca4d653d3a146c3dc09c1cBrian Paulstatic GLboolean
14154b08f35487fa439fd9ca4d653d3a146c3dc09c1cBrian Paullegal_texture_size(struct gl_context *ctx, gl_format format,
14164b08f35487fa439fd9ca4d653d3a146c3dc09c1cBrian Paul                   GLint width, GLint height, GLint depth)
14174b08f35487fa439fd9ca4d653d3a146c3dc09c1cBrian Paul{
14184b08f35487fa439fd9ca4d653d3a146c3dc09c1cBrian Paul   uint64_t bytes = _mesa_format_image_size64(format, width, height, depth);
14194b08f35487fa439fd9ca4d653d3a146c3dc09c1cBrian Paul   uint64_t mbytes = bytes / (1024 * 1024); /* convert to MB */
14204b08f35487fa439fd9ca4d653d3a146c3dc09c1cBrian Paul   return mbytes <= (uint64_t) ctx->Const.MaxTextureMbytes;
14214b08f35487fa439fd9ca4d653d3a146c3dc09c1cBrian Paul}
14224b08f35487fa439fd9ca4d653d3a146c3dc09c1cBrian Paul
14234b08f35487fa439fd9ca4d653d3a146c3dc09c1cBrian Paul
1424d4a38e86d4b4d66cca20ee63222f940cb73fa709Chia-I Wu/**
1425d4a38e86d4b4d66cca20ee63222f940cb73fa709Chia-I Wu * Return true if the format is only valid for glCompressedTexImage.
1426d4a38e86d4b4d66cca20ee63222f940cb73fa709Chia-I Wu */
1427d4a38e86d4b4d66cca20ee63222f940cb73fa709Chia-I Wustatic GLboolean
1428d4a38e86d4b4d66cca20ee63222f940cb73fa709Chia-I Wucompressedteximage_only_format(const struct gl_context *ctx, GLenum format)
1429d4a38e86d4b4d66cca20ee63222f940cb73fa709Chia-I Wu{
1430d4a38e86d4b4d66cca20ee63222f940cb73fa709Chia-I Wu   switch (format) {
1431d4a38e86d4b4d66cca20ee63222f940cb73fa709Chia-I Wu   case GL_ETC1_RGB8_OES:
14329b028faeaa2c4b0ca2e86892aac70a2f2b872fbbIan Romanick   case GL_PALETTE4_RGB8_OES:
14339b028faeaa2c4b0ca2e86892aac70a2f2b872fbbIan Romanick   case GL_PALETTE4_RGBA8_OES:
14349b028faeaa2c4b0ca2e86892aac70a2f2b872fbbIan Romanick   case GL_PALETTE4_R5_G6_B5_OES:
14359b028faeaa2c4b0ca2e86892aac70a2f2b872fbbIan Romanick   case GL_PALETTE4_RGBA4_OES:
14369b028faeaa2c4b0ca2e86892aac70a2f2b872fbbIan Romanick   case GL_PALETTE4_RGB5_A1_OES:
14379b028faeaa2c4b0ca2e86892aac70a2f2b872fbbIan Romanick   case GL_PALETTE8_RGB8_OES:
14389b028faeaa2c4b0ca2e86892aac70a2f2b872fbbIan Romanick   case GL_PALETTE8_RGBA8_OES:
14399b028faeaa2c4b0ca2e86892aac70a2f2b872fbbIan Romanick   case GL_PALETTE8_R5_G6_B5_OES:
14409b028faeaa2c4b0ca2e86892aac70a2f2b872fbbIan Romanick   case GL_PALETTE8_RGBA4_OES:
14419b028faeaa2c4b0ca2e86892aac70a2f2b872fbbIan Romanick   case GL_PALETTE8_RGB5_A1_OES:
1442d4a38e86d4b4d66cca20ee63222f940cb73fa709Chia-I Wu      return GL_TRUE;
1443d4a38e86d4b4d66cca20ee63222f940cb73fa709Chia-I Wu   default:
1444d4a38e86d4b4d66cca20ee63222f940cb73fa709Chia-I Wu      return GL_FALSE;
1445d4a38e86d4b4d66cca20ee63222f940cb73fa709Chia-I Wu   }
1446d4a38e86d4b4d66cca20ee63222f940cb73fa709Chia-I Wu}
1447d4a38e86d4b4d66cca20ee63222f940cb73fa709Chia-I Wu
14484b08f35487fa439fd9ca4d653d3a146c3dc09c1cBrian Paul
14494b08f35487fa439fd9ca4d653d3a146c3dc09c1cBrian Paul/**
14507404fa3f07a953343f2640fa97d82ab82ba5545dBrian Paul * Helper function to determine whether a target and specific compression
14517404fa3f07a953343f2640fa97d82ab82ba5545dBrian Paul * format are supported.
1452064cd7c78c3108012f5d15206c70470f7b500259Ian Romanick */
1453064cd7c78c3108012f5d15206c70470f7b500259Ian Romanickstatic GLboolean
14547404fa3f07a953343f2640fa97d82ab82ba5545dBrian Paultarget_can_be_compressed(const struct gl_context *ctx, GLenum target,
14557404fa3f07a953343f2640fa97d82ab82ba5545dBrian Paul                         GLenum intFormat)
1456064cd7c78c3108012f5d15206c70470f7b500259Ian Romanick{
14577404fa3f07a953343f2640fa97d82ab82ba5545dBrian Paul   (void) intFormat;  /* not used yet */
14587404fa3f07a953343f2640fa97d82ab82ba5545dBrian Paul
1459646afcc34045cd482e79ded241aac23082e65f6cBrian Paul   switch (target) {
1460646afcc34045cd482e79ded241aac23082e65f6cBrian Paul   case GL_TEXTURE_2D:
1461646afcc34045cd482e79ded241aac23082e65f6cBrian Paul   case GL_PROXY_TEXTURE_2D:
14627404fa3f07a953343f2640fa97d82ab82ba5545dBrian Paul      return GL_TRUE; /* true for any compressed format so far */
1463646afcc34045cd482e79ded241aac23082e65f6cBrian Paul   case GL_PROXY_TEXTURE_CUBE_MAP:
1464646afcc34045cd482e79ded241aac23082e65f6cBrian Paul   case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
1465646afcc34045cd482e79ded241aac23082e65f6cBrian Paul   case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
1466646afcc34045cd482e79ded241aac23082e65f6cBrian Paul   case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
1467646afcc34045cd482e79ded241aac23082e65f6cBrian Paul   case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
1468646afcc34045cd482e79ded241aac23082e65f6cBrian Paul   case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
1469646afcc34045cd482e79ded241aac23082e65f6cBrian Paul   case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
1470646afcc34045cd482e79ded241aac23082e65f6cBrian Paul      return ctx->Extensions.ARB_texture_cube_map;
1471646afcc34045cd482e79ded241aac23082e65f6cBrian Paul   case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
1472646afcc34045cd482e79ded241aac23082e65f6cBrian Paul   case GL_TEXTURE_2D_ARRAY_EXT:
1473f2dd11817a66b6254e1533398dc53f1bc51953beBrian Paul      return (ctx->Extensions.MESA_texture_array ||
1474684c66bb8b8cdaa7fc18b64a6f739ad70a10cd54Brian Paul              ctx->Extensions.EXT_texture_array);
1475646afcc34045cd482e79ded241aac23082e65f6cBrian Paul   default:
1476646afcc34045cd482e79ded241aac23082e65f6cBrian Paul      return GL_FALSE;
1477646afcc34045cd482e79ded241aac23082e65f6cBrian Paul   }
1478064cd7c78c3108012f5d15206c70470f7b500259Ian Romanick}
1479064cd7c78c3108012f5d15206c70470f7b500259Ian Romanick
1480064cd7c78c3108012f5d15206c70470f7b500259Ian Romanick
1481064cd7c78c3108012f5d15206c70470f7b500259Ian Romanick/**
1482d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul * Check if the given texture target value is legal for a
1483d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul * glTexImage1/2/3D call.
1484d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul */
1485d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paulstatic GLboolean
1486d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paullegal_teximage_target(struct gl_context *ctx, GLuint dims, GLenum target)
1487d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul{
1488d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul   switch (dims) {
1489d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul   case 1:
1490d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul      switch (target) {
1491d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul      case GL_TEXTURE_1D:
1492d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul      case GL_PROXY_TEXTURE_1D:
1493530c9d764b84035728a28ee2db996fffe49125ceIan Romanick         return _mesa_is_desktop_gl(ctx);
1494d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul      default:
1495d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul         return GL_FALSE;
1496d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul      }
1497d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul   case 2:
1498d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul      switch (target) {
1499d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul      case GL_TEXTURE_2D:
1500d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul         return GL_TRUE;
1501530c9d764b84035728a28ee2db996fffe49125ceIan Romanick      case GL_PROXY_TEXTURE_2D:
1502530c9d764b84035728a28ee2db996fffe49125ceIan Romanick         return _mesa_is_desktop_gl(ctx);
1503d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul      case GL_PROXY_TEXTURE_CUBE_MAP:
1504530c9d764b84035728a28ee2db996fffe49125ceIan Romanick         return _mesa_is_desktop_gl(ctx)
1505530c9d764b84035728a28ee2db996fffe49125ceIan Romanick            && ctx->Extensions.ARB_texture_cube_map;
1506d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul      case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
1507d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul      case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
1508d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul      case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
1509d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul      case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
1510d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul      case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
1511d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul      case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
1512d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul         return ctx->Extensions.ARB_texture_cube_map;
1513d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul      case GL_TEXTURE_RECTANGLE_NV:
1514d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul      case GL_PROXY_TEXTURE_RECTANGLE_NV:
1515530c9d764b84035728a28ee2db996fffe49125ceIan Romanick         return _mesa_is_desktop_gl(ctx)
1516530c9d764b84035728a28ee2db996fffe49125ceIan Romanick            && ctx->Extensions.NV_texture_rectangle;
1517d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul      case GL_TEXTURE_1D_ARRAY_EXT:
1518d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul      case GL_PROXY_TEXTURE_1D_ARRAY_EXT:
1519530c9d764b84035728a28ee2db996fffe49125ceIan Romanick         return _mesa_is_desktop_gl(ctx)
1520530c9d764b84035728a28ee2db996fffe49125ceIan Romanick            && (ctx->Extensions.MESA_texture_array ||
1521530c9d764b84035728a28ee2db996fffe49125ceIan Romanick                ctx->Extensions.EXT_texture_array);
1522d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul      default:
1523d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul         return GL_FALSE;
1524d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul      }
1525d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul   case 3:
1526d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul      switch (target) {
1527d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul      case GL_TEXTURE_3D:
1528d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul         return GL_TRUE;
1529530c9d764b84035728a28ee2db996fffe49125ceIan Romanick      case GL_PROXY_TEXTURE_3D:
1530530c9d764b84035728a28ee2db996fffe49125ceIan Romanick         return _mesa_is_desktop_gl(ctx);
1531d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul      case GL_TEXTURE_2D_ARRAY_EXT:
1532530c9d764b84035728a28ee2db996fffe49125ceIan Romanick         return (_mesa_is_desktop_gl(ctx)
1533530c9d764b84035728a28ee2db996fffe49125ceIan Romanick                 && (ctx->Extensions.MESA_texture_array ||
1534530c9d764b84035728a28ee2db996fffe49125ceIan Romanick                     ctx->Extensions.EXT_texture_array))
1535530c9d764b84035728a28ee2db996fffe49125ceIan Romanick            || _mesa_is_gles3(ctx);
1536d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul      case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
1537530c9d764b84035728a28ee2db996fffe49125ceIan Romanick         return _mesa_is_desktop_gl(ctx)
1538530c9d764b84035728a28ee2db996fffe49125ceIan Romanick            && (ctx->Extensions.MESA_texture_array ||
1539530c9d764b84035728a28ee2db996fffe49125ceIan Romanick                ctx->Extensions.EXT_texture_array);
1540d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul      default:
1541d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul         return GL_FALSE;
1542d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul      }
1543d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul   default:
1544d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul      _mesa_problem(ctx, "invalid dims=%u in legal_teximage_target()", dims);
1545d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul      return GL_FALSE;
1546d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul   }
1547d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul}
1548d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul
1549d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul
1550d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul/**
155111f386fb504b9f07dd501a65f31185cfa0e9a679Brian Paul * Check if the given texture target value is legal for a
155209a5e028a6d254f619132e302a96c8535018603dBrian Paul * glTexSubImage, glCopyTexSubImage or glCopyTexImage call.
155309a5e028a6d254f619132e302a96c8535018603dBrian Paul * The difference compared to legal_teximage_target() above is that
155409a5e028a6d254f619132e302a96c8535018603dBrian Paul * proxy targets are not supported.
155511f386fb504b9f07dd501a65f31185cfa0e9a679Brian Paul */
155611f386fb504b9f07dd501a65f31185cfa0e9a679Brian Paulstatic GLboolean
155709a5e028a6d254f619132e302a96c8535018603dBrian Paullegal_texsubimage_target(struct gl_context *ctx, GLuint dims, GLenum target)
155811f386fb504b9f07dd501a65f31185cfa0e9a679Brian Paul{
155911f386fb504b9f07dd501a65f31185cfa0e9a679Brian Paul   switch (dims) {
156011f386fb504b9f07dd501a65f31185cfa0e9a679Brian Paul   case 1:
1561530c9d764b84035728a28ee2db996fffe49125ceIan Romanick      return _mesa_is_desktop_gl(ctx) && target == GL_TEXTURE_1D;
156211f386fb504b9f07dd501a65f31185cfa0e9a679Brian Paul   case 2:
156311f386fb504b9f07dd501a65f31185cfa0e9a679Brian Paul      switch (target) {
156411f386fb504b9f07dd501a65f31185cfa0e9a679Brian Paul      case GL_TEXTURE_2D:
156511f386fb504b9f07dd501a65f31185cfa0e9a679Brian Paul         return GL_TRUE;
156611f386fb504b9f07dd501a65f31185cfa0e9a679Brian Paul      case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
156711f386fb504b9f07dd501a65f31185cfa0e9a679Brian Paul      case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
156811f386fb504b9f07dd501a65f31185cfa0e9a679Brian Paul      case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
156911f386fb504b9f07dd501a65f31185cfa0e9a679Brian Paul      case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
157011f386fb504b9f07dd501a65f31185cfa0e9a679Brian Paul      case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
157111f386fb504b9f07dd501a65f31185cfa0e9a679Brian Paul      case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
157211f386fb504b9f07dd501a65f31185cfa0e9a679Brian Paul         return ctx->Extensions.ARB_texture_cube_map;
157311f386fb504b9f07dd501a65f31185cfa0e9a679Brian Paul      case GL_TEXTURE_RECTANGLE_NV:
1574530c9d764b84035728a28ee2db996fffe49125ceIan Romanick         return _mesa_is_desktop_gl(ctx)
1575530c9d764b84035728a28ee2db996fffe49125ceIan Romanick            && ctx->Extensions.NV_texture_rectangle;
157611f386fb504b9f07dd501a65f31185cfa0e9a679Brian Paul      case GL_TEXTURE_1D_ARRAY_EXT:
1577530c9d764b84035728a28ee2db996fffe49125ceIan Romanick         return _mesa_is_desktop_gl(ctx)
1578530c9d764b84035728a28ee2db996fffe49125ceIan Romanick            && (ctx->Extensions.MESA_texture_array ||
1579530c9d764b84035728a28ee2db996fffe49125ceIan Romanick                ctx->Extensions.EXT_texture_array);
158011f386fb504b9f07dd501a65f31185cfa0e9a679Brian Paul      default:
158111f386fb504b9f07dd501a65f31185cfa0e9a679Brian Paul         return GL_FALSE;
158211f386fb504b9f07dd501a65f31185cfa0e9a679Brian Paul      }
158309a5e028a6d254f619132e302a96c8535018603dBrian Paul   case 3:
158409a5e028a6d254f619132e302a96c8535018603dBrian Paul      switch (target) {
158509a5e028a6d254f619132e302a96c8535018603dBrian Paul      case GL_TEXTURE_3D:
158609a5e028a6d254f619132e302a96c8535018603dBrian Paul         return GL_TRUE;
158709a5e028a6d254f619132e302a96c8535018603dBrian Paul      case GL_TEXTURE_2D_ARRAY_EXT:
1588530c9d764b84035728a28ee2db996fffe49125ceIan Romanick         return (_mesa_is_desktop_gl(ctx)
1589530c9d764b84035728a28ee2db996fffe49125ceIan Romanick                 && (ctx->Extensions.MESA_texture_array ||
1590530c9d764b84035728a28ee2db996fffe49125ceIan Romanick                     ctx->Extensions.EXT_texture_array))
1591530c9d764b84035728a28ee2db996fffe49125ceIan Romanick            || _mesa_is_gles3(ctx);
159209a5e028a6d254f619132e302a96c8535018603dBrian Paul      default:
159309a5e028a6d254f619132e302a96c8535018603dBrian Paul         return GL_FALSE;
159409a5e028a6d254f619132e302a96c8535018603dBrian Paul      }
159511f386fb504b9f07dd501a65f31185cfa0e9a679Brian Paul   default:
159609a5e028a6d254f619132e302a96c8535018603dBrian Paul      _mesa_problem(ctx, "invalid dims=%u in legal_texsubimage_target()",
159709a5e028a6d254f619132e302a96c8535018603dBrian Paul                    dims);
159811f386fb504b9f07dd501a65f31185cfa0e9a679Brian Paul      return GL_FALSE;
159911f386fb504b9f07dd501a65f31185cfa0e9a679Brian Paul   }
160011f386fb504b9f07dd501a65f31185cfa0e9a679Brian Paul}
160111f386fb504b9f07dd501a65f31185cfa0e9a679Brian Paul
160211f386fb504b9f07dd501a65f31185cfa0e9a679Brian Paul
160311f386fb504b9f07dd501a65f31185cfa0e9a679Brian Paul/**
1604a0d736b3b7c325ab0295da3b0e3b92d170b44261Brian Paul * Helper function to determine if a texture object is mutable (in terms
1605a0d736b3b7c325ab0295da3b0e3b92d170b44261Brian Paul * of GL_ARB_texture_storage).
1606a0d736b3b7c325ab0295da3b0e3b92d170b44261Brian Paul */
1607a0d736b3b7c325ab0295da3b0e3b92d170b44261Brian Paulstatic GLboolean
1608a0d736b3b7c325ab0295da3b0e3b92d170b44261Brian Paulmutable_tex_object(struct gl_context *ctx, GLenum target)
1609a0d736b3b7c325ab0295da3b0e3b92d170b44261Brian Paul{
1610a0d736b3b7c325ab0295da3b0e3b92d170b44261Brian Paul   if (ctx->Extensions.ARB_texture_storage) {
1611a0d736b3b7c325ab0295da3b0e3b92d170b44261Brian Paul      struct gl_texture_object *texObj =
1612a0d736b3b7c325ab0295da3b0e3b92d170b44261Brian Paul         _mesa_get_current_tex_object(ctx, target);
1613a0d736b3b7c325ab0295da3b0e3b92d170b44261Brian Paul      return !texObj->Immutable;
1614a0d736b3b7c325ab0295da3b0e3b92d170b44261Brian Paul   }
1615a0d736b3b7c325ab0295da3b0e3b92d170b44261Brian Paul   return GL_TRUE;
1616a0d736b3b7c325ab0295da3b0e3b92d170b44261Brian Paul}
1617a0d736b3b7c325ab0295da3b0e3b92d170b44261Brian Paul
1618a0d736b3b7c325ab0295da3b0e3b92d170b44261Brian Paul
1619409620e47748479aaef593dc9bec0d6dd520e14aIan RomanickGLenum
1620409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick_mesa_es_error_check_format_and_type(GLenum format, GLenum type,
1621409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick                                     unsigned dimensions)
1622409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick{
1623409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick   bool type_valid = true;
1624409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick
1625409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick   switch (format) {
1626409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick   case GL_ALPHA:
1627409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick   case GL_LUMINANCE:
1628409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick   case GL_LUMINANCE_ALPHA:
1629409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick      type_valid = (type == GL_UNSIGNED_BYTE
1630409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick                    || type == GL_FLOAT
1631409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick                    || type == GL_HALF_FLOAT_OES);
1632409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick      break;
1633409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick
1634409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick   case GL_RGB:
1635409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick      type_valid = (type == GL_UNSIGNED_BYTE
1636409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick                    || type == GL_UNSIGNED_SHORT_5_6_5
1637409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick                    || type == GL_FLOAT
1638409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick                    || type == GL_HALF_FLOAT_OES);
1639409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick      break;
1640409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick
1641409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick   case GL_RGBA:
1642409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick      type_valid = (type == GL_UNSIGNED_BYTE
1643409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick                    || type == GL_UNSIGNED_SHORT_4_4_4_4
1644409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick                    || type == GL_UNSIGNED_SHORT_5_5_5_1
1645409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick                    || type == GL_FLOAT
1646409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick                    || type == GL_HALF_FLOAT_OES
1647409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick                    || type == GL_UNSIGNED_INT_2_10_10_10_REV);
1648409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick      break;
1649409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick
1650409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick   case GL_DEPTH_COMPONENT:
1651409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick      /* This format is filtered against invalid dimensionalities elsewhere.
1652409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick       */
1653409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick      type_valid = (type == GL_UNSIGNED_SHORT
1654409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick                    || type == GL_UNSIGNED_INT);
1655409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick      break;
1656409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick
1657409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick   case GL_DEPTH_STENCIL:
1658409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick      /* This format is filtered against invalid dimensionalities elsewhere.
1659409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick       */
1660409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick      type_valid = (type == GL_UNSIGNED_INT_24_8);
1661409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick      break;
1662409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick
1663409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick   case GL_BGRA_EXT:
1664409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick      type_valid = (type == GL_UNSIGNED_BYTE);
1665409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick
1666409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick      /* This feels like a bug in the EXT_texture_format_BGRA8888 spec, but
1667409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick       * the format does not appear to be allowed for 3D textures in OpenGL
1668409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick       * ES.
1669409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick       */
1670409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick      if (dimensions != 2)
1671409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick         return GL_INVALID_VALUE;
1672409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick
1673409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick      break;
1674409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick
1675409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick   default:
1676409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick      return GL_INVALID_VALUE;
1677409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick   }
1678409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick
1679409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick   return type_valid ? GL_NO_ERROR : GL_INVALID_OPERATION;
1680409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick}
1681a0d736b3b7c325ab0295da3b0e3b92d170b44261Brian Paul
1682637a79aa2381e363d6b94c0bb5692a1882e9c945Brian Paul
1683637a79aa2381e363d6b94c0bb5692a1882e9c945Brian Paul
1684637a79aa2381e363d6b94c0bb5692a1882e9c945Brian Paul/**
1685fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul * Return expected size of a compressed texture.
1686fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul */
1687fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paulstatic GLuint
1688fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paulcompressed_tex_size(GLsizei width, GLsizei height, GLsizei depth,
1689fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul                    GLenum glformat)
1690fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul{
1691fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul   gl_format mesaFormat = _mesa_glenum_to_compressed_format(glformat);
1692fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul   return _mesa_format_image_size(mesaFormat, width, height, depth);
1693fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul}
1694fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul
1695fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul
1696fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul/*
1697fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul * Return compressed texture block size, in pixels.
1698fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul */
1699fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paulstatic void
1700fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paulget_compressed_block_size(GLenum glformat, GLuint *bw, GLuint *bh)
1701fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul{
1702fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul   gl_format mesaFormat = _mesa_glenum_to_compressed_format(glformat);
1703fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul   _mesa_get_format_block_size(mesaFormat, bw, bh);
1704fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul}
1705fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul
1706fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul
1707fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul/**
1708637a79aa2381e363d6b94c0bb5692a1882e9c945Brian Paul * Special value returned by error some texture error checking functions when
1709637a79aa2381e363d6b94c0bb5692a1882e9c945Brian Paul * an error is detected and the proxy texture image's width/height/depth/format
1710637a79aa2381e363d6b94c0bb5692a1882e9c945Brian Paul * fields should be zeroed-out.
1711637a79aa2381e363d6b94c0bb5692a1882e9c945Brian Paul */
1712637a79aa2381e363d6b94c0bb5692a1882e9c945Brian Paul#define PROXY_ERROR 2
1713637a79aa2381e363d6b94c0bb5692a1882e9c945Brian Paul
1714637a79aa2381e363d6b94c0bb5692a1882e9c945Brian Paul
1715a0d736b3b7c325ab0295da3b0e3b92d170b44261Brian Paul/**
17166dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Test the glTexImage[123]D() parameters for errors.
17176dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell *
17186dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param ctx GL context.
171985288ad722aa0ad378004c523a0e1a4984e15316Brian Paul * \param dimensions texture image dimensions (must be 1, 2 or 3).
17206dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param target texture target given by the user.
17216dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param level image level given by the user.
17226dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param internalFormat internal format given by the user.
17236dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param format pixel data format given by the user.
17246dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param type pixel data type given by the user.
17256dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param width image width given by the user.
17266dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param height image height given by the user.
17276dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param depth image depth given by the user.
17286dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param border image border given by the user.
17296dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell *
1730637a79aa2381e363d6b94c0bb5692a1882e9c945Brian Paul * \return PROXY_ERROR if there's an error that should zero-out the proxy image,
1731637a79aa2381e363d6b94c0bb5692a1882e9c945Brian Paul *         GL_TRUE if a regular GL error is found, or GL_FALSE if no error,
17326dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell *
17336dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Verifies each of the parameters against the constants specified in
17343c59febf05e6af80d63e5b9a478a11b275ac429cBrian Paul * __struct gl_contextRec::Const and the supported extensions, and according
17353c59febf05e6af80d63e5b9a478a11b275ac429cBrian Paul * to the OpenGL specification.
1736afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg */
1737637a79aa2381e363d6b94c0bb5692a1882e9c945Brian Paulstatic GLenum
173885288ad722aa0ad378004c523a0e1a4984e15316Brian Paultexture_error_check( struct gl_context *ctx,
173985288ad722aa0ad378004c523a0e1a4984e15316Brian Paul                     GLuint dimensions, GLenum target,
1740c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul                     GLint level, GLint internalFormat,
1741c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul                     GLenum format, GLenum type,
1742c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul                     GLint width, GLint height,
1743c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul                     GLint depth, GLint border )
1744afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg{
1745f9bb66b1cee2272b20ff3086e84d17026b8e8bb9Brian Paul   const GLenum proxyTarget = _mesa_get_proxy_target(target);
174635f620d55ced7fcb3f39465c173ba2ae435a2ba1Brian Paul   const GLboolean isProxy = target == proxyTarget;
1747e2e4b60c7d9fc3618c0f9d7496c9ce3d5eee3ab5Ian Romanick   GLboolean sizeOK = GL_TRUE;
1748eba527bf9ffc2fd67c44fb77104107556f509b49Ian Romanick   GLboolean colorFormat;
1749627b435dfe17698a1c69e9a259838fc6f2e6bd4eBrian Paul   GLenum err;
1750eba527bf9ffc2fd67c44fb77104107556f509b49Ian Romanick
1751eba527bf9ffc2fd67c44fb77104107556f509b49Ian Romanick   /* Even though there are no color-index textures, we still have to support
1752eba527bf9ffc2fd67c44fb77104107556f509b49Ian Romanick    * uploading color-index data and remapping it to RGB via the
1753eba527bf9ffc2fd67c44fb77104107556f509b49Ian Romanick    * GL_PIXEL_MAP_I_TO_[RGBA] tables.
1754eba527bf9ffc2fd67c44fb77104107556f509b49Ian Romanick    */
1755eba527bf9ffc2fd67c44fb77104107556f509b49Ian Romanick   const GLboolean indexFormat = (format == GL_COLOR_INDEX);
1756afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
17571f5b1f98468d5e80be39e619ed15c422fbede8d3Brian Paul   /* Note: for proxy textures, some error conditions immediately generate
17581f5b1f98468d5e80be39e619ed15c422fbede8d3Brian Paul    * a GL error in the usual way.  But others do not generate a GL error.
17591f5b1f98468d5e80be39e619ed15c422fbede8d3Brian Paul    * Instead, they cause the width, height, depth, format fields of the
17601f5b1f98468d5e80be39e619ed15c422fbede8d3Brian Paul    * texture image to be zeroed-out.  The GL spec seems to indicate that the
17611f5b1f98468d5e80be39e619ed15c422fbede8d3Brian Paul    * zero-out behaviour is only used in cases related to memory allocation.
17621f5b1f98468d5e80be39e619ed15c422fbede8d3Brian Paul    */
17631f5b1f98468d5e80be39e619ed15c422fbede8d3Brian Paul
1764738318bb75dea8dac4465f53850987f6062a732dBrian Paul   /* Basic level check (more checking in ctx->Driver.TestProxyTexImage) */
1765738318bb75dea8dac4465f53850987f6062a732dBrian Paul   if (level < 0 || level >= MAX_TEXTURE_LEVELS) {
17661f5b1f98468d5e80be39e619ed15c422fbede8d3Brian Paul      _mesa_error(ctx, GL_INVALID_VALUE,
17671f5b1f98468d5e80be39e619ed15c422fbede8d3Brian Paul                  "glTexImage%dD(level=%d)", dimensions, level);
1768738318bb75dea8dac4465f53850987f6062a732dBrian Paul      return GL_TRUE;
1769738318bb75dea8dac4465f53850987f6062a732dBrian Paul   }
1770738318bb75dea8dac4465f53850987f6062a732dBrian Paul
1771738318bb75dea8dac4465f53850987f6062a732dBrian Paul   /* Check border */
1772738318bb75dea8dac4465f53850987f6062a732dBrian Paul   if (border < 0 || border > 1 ||
17730686ccac950bac1a650400b29c47ddaafea22fa9Ian Romanick       ((ctx->API != API_OPENGL ||
17740686ccac950bac1a650400b29c47ddaafea22fa9Ian Romanick         target == GL_TEXTURE_RECTANGLE_NV ||
1775738318bb75dea8dac4465f53850987f6062a732dBrian Paul         target == GL_PROXY_TEXTURE_RECTANGLE_NV) && border != 0)) {
17761f5b1f98468d5e80be39e619ed15c422fbede8d3Brian Paul      _mesa_error(ctx, GL_INVALID_VALUE,
17771f5b1f98468d5e80be39e619ed15c422fbede8d3Brian Paul                  "glTexImage%dD(border=%d)", dimensions, border);
1778738318bb75dea8dac4465f53850987f6062a732dBrian Paul      return GL_TRUE;
1779738318bb75dea8dac4465f53850987f6062a732dBrian Paul   }
1780738318bb75dea8dac4465f53850987f6062a732dBrian Paul
1781973da83f6237b5af4a9ee77f32fdfa5c04ecabc8Brian Paul   if (width < 0 || height < 0 || depth < 0) {
17821f5b1f98468d5e80be39e619ed15c422fbede8d3Brian Paul      _mesa_error(ctx, GL_INVALID_VALUE,
17831f5b1f98468d5e80be39e619ed15c422fbede8d3Brian Paul                  "glTexImage%dD(width, height or depth < 0)", dimensions);
1784f6e76fe9b5c4c57ac6dc81143b4474ebfee879d2Brian Paul      return GL_TRUE;
1785f6e76fe9b5c4c57ac6dc81143b4474ebfee879d2Brian Paul   }
1786f6e76fe9b5c4c57ac6dc81143b4474ebfee879d2Brian Paul
1787409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick   /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
1788409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick    * combinations of format, internalFormat, and type that can be used.
1789409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick    * Formats and types that require additional extensions (e.g., GL_FLOAT
1790409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick    * requires GL_OES_texture_float) are filtered elsewhere.
1791409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick    */
1792409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick   if (_mesa_is_gles(ctx) && !_mesa_is_gles3(ctx)) {
1793409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick      if (format != internalFormat) {
1794409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick         _mesa_error(ctx, GL_INVALID_OPERATION,
1795409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick                     "glTexImage%dD(format = %s, internalFormat = %s)",
1796409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick                     dimensions,
1797409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick                     _mesa_lookup_enum_by_nr(format),
1798409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick                     _mesa_lookup_enum_by_nr(internalFormat));
1799409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick         return GL_TRUE;
1800409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick      }
1801409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick
1802409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick      err = _mesa_es_error_check_format_and_type(format, type, dimensions);
1803409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick      if (err != GL_NO_ERROR) {
1804409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick         _mesa_error(ctx, err,
1805409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick                     "glTexImage%dD(format = %s, type = %s)",
1806409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick                     dimensions,
1807409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick                     _mesa_lookup_enum_by_nr(format),
1808409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick                     _mesa_lookup_enum_by_nr(type));
1809409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick         return GL_TRUE;
1810409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick      }
1811409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick   }
1812409620e47748479aaef593dc9bec0d6dd520e14aIan Romanick
181335f620d55ced7fcb3f39465c173ba2ae435a2ba1Brian Paul   /* Do this simple check before calling the TestProxyTexImage() function */
181435f620d55ced7fcb3f39465c173ba2ae435a2ba1Brian Paul   if (proxyTarget == GL_PROXY_TEXTURE_CUBE_MAP_ARB) {
181535f620d55ced7fcb3f39465c173ba2ae435a2ba1Brian Paul      sizeOK = (width == height);
1816afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   }
1817d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul
181835f620d55ced7fcb3f39465c173ba2ae435a2ba1Brian Paul   /*
181935f620d55ced7fcb3f39465c173ba2ae435a2ba1Brian Paul    * Use the proxy texture driver hook to see if the size/level/etc are
182035f620d55ced7fcb3f39465c173ba2ae435a2ba1Brian Paul    * legal.
182135f620d55ced7fcb3f39465c173ba2ae435a2ba1Brian Paul    */
182235f620d55ced7fcb3f39465c173ba2ae435a2ba1Brian Paul   sizeOK = sizeOK && ctx->Driver.TestProxyTexImage(ctx, proxyTarget, level,
1823e2e4b60c7d9fc3618c0f9d7496c9ce3d5eee3ab5Ian Romanick                                                    internalFormat, format,
1824e2e4b60c7d9fc3618c0f9d7496c9ce3d5eee3ab5Ian Romanick                                                    type, width, height,
1825e2e4b60c7d9fc3618c0f9d7496c9ce3d5eee3ab5Ian Romanick                                                    depth, border);
1826738318bb75dea8dac4465f53850987f6062a732dBrian Paul   if (!sizeOK) {
1827637a79aa2381e363d6b94c0bb5692a1882e9c945Brian Paul      if (isProxy) {
1828637a79aa2381e363d6b94c0bb5692a1882e9c945Brian Paul         /* No GL error is recorded, but we need to zero-out the image dims */
1829637a79aa2381e363d6b94c0bb5692a1882e9c945Brian Paul         return PROXY_ERROR;
1830637a79aa2381e363d6b94c0bb5692a1882e9c945Brian Paul      }
1831637a79aa2381e363d6b94c0bb5692a1882e9c945Brian Paul      else {
18324e9676fb13f60ecdbc247b120031f18cd3febcb0Brian Paul         _mesa_error(ctx, GL_INVALID_VALUE,
1833738318bb75dea8dac4465f53850987f6062a732dBrian Paul                     "glTexImage%dD(level=%d, width=%d, height=%d, depth=%d)",
1834738318bb75dea8dac4465f53850987f6062a732dBrian Paul                     dimensions, level, width, height, depth);
1835637a79aa2381e363d6b94c0bb5692a1882e9c945Brian Paul         return GL_TRUE;
1836c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      }
1837afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   }
1838afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
1839738318bb75dea8dac4465f53850987f6062a732dBrian Paul   /* Check internalFormat */
184089fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   if (_mesa_base_tex_format(ctx, internalFormat) < 0) {
18411f5b1f98468d5e80be39e619ed15c422fbede8d3Brian Paul      _mesa_error(ctx, GL_INVALID_VALUE,
18421f5b1f98468d5e80be39e619ed15c422fbede8d3Brian Paul                  "glTexImage%dD(internalFormat=%s)",
18431f5b1f98468d5e80be39e619ed15c422fbede8d3Brian Paul                  dimensions, _mesa_lookup_enum_by_nr(internalFormat));
1844afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      return GL_TRUE;
1845afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   }
1846afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
1847738318bb75dea8dac4465f53850987f6062a732dBrian Paul   /* Check incoming image format and type */
1848627b435dfe17698a1c69e9a259838fc6f2e6bd4eBrian Paul   err = _mesa_error_check_format_and_type(ctx, format, type);
1849627b435dfe17698a1c69e9a259838fc6f2e6bd4eBrian Paul   if (err != GL_NO_ERROR) {
18501f5b1f98468d5e80be39e619ed15c422fbede8d3Brian Paul      _mesa_error(ctx, err,
18511f5b1f98468d5e80be39e619ed15c422fbede8d3Brian Paul                  "glTexImage%dD(incompatible format 0x%x, type 0x%x)",
18521f5b1f98468d5e80be39e619ed15c422fbede8d3Brian Paul                  dimensions, format, type);
185338f28665bf9fb5b2464738ca5074848ec2777ae1Gareth Hughes      return GL_TRUE;
1854afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   }
1855afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
1856601df9c742939c1f77de489561fe3e1d02f49618Brian Paul   /* make sure internal format and format basically agree */
185722e442544bc451f114288f07cf9cc1584ca357a1Brian Paul   colorFormat = _mesa_is_color_format(format);
185822e442544bc451f114288f07cf9cc1584ca357a1Brian Paul   if ((_mesa_is_color_format(internalFormat) && !colorFormat && !indexFormat) ||
1859ba2a55ccd61d9fa5565640faefb64fd6fb0e70abBrian Paul       (_mesa_is_depth_format(internalFormat) != _mesa_is_depth_format(format)) ||
1860ba2a55ccd61d9fa5565640faefb64fd6fb0e70abBrian Paul       (_mesa_is_ycbcr_format(internalFormat) != _mesa_is_ycbcr_format(format)) ||
1861ba2a55ccd61d9fa5565640faefb64fd6fb0e70abBrian Paul       (_mesa_is_depthstencil_format(internalFormat) != _mesa_is_depthstencil_format(format)) ||
1862ba2a55ccd61d9fa5565640faefb64fd6fb0e70abBrian Paul       (_mesa_is_dudv_format(internalFormat) != _mesa_is_dudv_format(format))) {
18631f5b1f98468d5e80be39e619ed15c422fbede8d3Brian Paul      _mesa_error(ctx, GL_INVALID_OPERATION,
18641f5b1f98468d5e80be39e619ed15c422fbede8d3Brian Paul                  "glTexImage%dD(incompatible internalFormat 0x%x, format 0x%x)",
18651f5b1f98468d5e80be39e619ed15c422fbede8d3Brian Paul                  dimensions, internalFormat, format);
1866601df9c742939c1f77de489561fe3e1d02f49618Brian Paul      return GL_TRUE;
1867601df9c742939c1f77de489561fe3e1d02f49618Brian Paul   }
1868601df9c742939c1f77de489561fe3e1d02f49618Brian Paul
1869601df9c742939c1f77de489561fe3e1d02f49618Brian Paul   /* additional checks for ycbcr textures */
1870601df9c742939c1f77de489561fe3e1d02f49618Brian Paul   if (internalFormat == GL_YCBCR_MESA) {
1871c5b995066020191982b2315fc45d05e068eee761Brian Paul      ASSERT(ctx->Extensions.MESA_ycbcr_texture);
1872601df9c742939c1f77de489561fe3e1d02f49618Brian Paul      if (type != GL_UNSIGNED_SHORT_8_8_MESA &&
1873601df9c742939c1f77de489561fe3e1d02f49618Brian Paul          type != GL_UNSIGNED_SHORT_8_8_REV_MESA) {
1874c5b995066020191982b2315fc45d05e068eee761Brian Paul         char message[100];
1875fc1be4a99425d09103bba9e06026f31f2b0142d2Vinson Lee         _mesa_snprintf(message, sizeof(message),
18761f5b1f98468d5e80be39e619ed15c422fbede8d3Brian Paul                        "glTexImage%dD(format/type YCBCR mismatch)",
18771f5b1f98468d5e80be39e619ed15c422fbede8d3Brian Paul                        dimensions);
1878c5dde53f4e42612518cd927bb58f08c0e22db17aVinson Lee         _mesa_error(ctx, GL_INVALID_ENUM, "%s", message);
1879c5b995066020191982b2315fc45d05e068eee761Brian Paul         return GL_TRUE; /* error */
1880c5b995066020191982b2315fc45d05e068eee761Brian Paul      }
1881d8419c730e73c3be2eadfb0bee176ab06885766aBrian Paul      if (target != GL_TEXTURE_2D &&
1882d8419c730e73c3be2eadfb0bee176ab06885766aBrian Paul          target != GL_PROXY_TEXTURE_2D &&
1883d8419c730e73c3be2eadfb0bee176ab06885766aBrian Paul          target != GL_TEXTURE_RECTANGLE_NV &&
1884d8419c730e73c3be2eadfb0bee176ab06885766aBrian Paul          target != GL_PROXY_TEXTURE_RECTANGLE_NV) {
18851f5b1f98468d5e80be39e619ed15c422fbede8d3Brian Paul         _mesa_error(ctx, GL_INVALID_ENUM,
18861f5b1f98468d5e80be39e619ed15c422fbede8d3Brian Paul                     "glTexImage%dD(bad target for YCbCr texture)",
18871f5b1f98468d5e80be39e619ed15c422fbede8d3Brian Paul                     dimensions);
1888d8419c730e73c3be2eadfb0bee176ab06885766aBrian Paul         return GL_TRUE;
1889d8419c730e73c3be2eadfb0bee176ab06885766aBrian Paul      }
1890d8419c730e73c3be2eadfb0bee176ab06885766aBrian Paul      if (border != 0) {
18911f5b1f98468d5e80be39e619ed15c422fbede8d3Brian Paul         char message[100];
18921f5b1f98468d5e80be39e619ed15c422fbede8d3Brian Paul         _mesa_snprintf(message, sizeof(message),
18931f5b1f98468d5e80be39e619ed15c422fbede8d3Brian Paul                        "glTexImage%dD(format=GL_YCBCR_MESA and border=%d)",
18941f5b1f98468d5e80be39e619ed15c422fbede8d3Brian Paul                        dimensions, border);
18951f5b1f98468d5e80be39e619ed15c422fbede8d3Brian Paul         _mesa_error(ctx, GL_INVALID_VALUE, "%s", message);
1896d8419c730e73c3be2eadfb0bee176ab06885766aBrian Paul         return GL_TRUE;
1897d8419c730e73c3be2eadfb0bee176ab06885766aBrian Paul      }
1898c5b995066020191982b2315fc45d05e068eee761Brian Paul   }
1899c5b995066020191982b2315fc45d05e068eee761Brian Paul
1900601df9c742939c1f77de489561fe3e1d02f49618Brian Paul   /* additional checks for depth textures */
1901601df9c742939c1f77de489561fe3e1d02f49618Brian Paul   if (_mesa_base_tex_format(ctx, internalFormat) == GL_DEPTH_COMPONENT) {
190229a7d7784def4f37d3fe38a123a17836dd7fafc0Anuj Phogat      /* Only 1D, 2D, rect, array and cube textures supported, not 3D
190329a7d7784def4f37d3fe38a123a17836dd7fafc0Anuj Phogat       * Cubemaps are only supported for GL version > 3.0 or with EXT_gpu_shader4 */
1904601df9c742939c1f77de489561fe3e1d02f49618Brian Paul      if (target != GL_TEXTURE_1D &&
1905601df9c742939c1f77de489561fe3e1d02f49618Brian Paul          target != GL_PROXY_TEXTURE_1D &&
1906601df9c742939c1f77de489561fe3e1d02f49618Brian Paul          target != GL_TEXTURE_2D &&
19072b04dd9d2cba6ec3506e78016e64cffce6e8abf7Brian Paul          target != GL_PROXY_TEXTURE_2D &&
1908753660780a84187043d0dc4b0e0f449786862182Brian Paul          target != GL_TEXTURE_1D_ARRAY &&
1909753660780a84187043d0dc4b0e0f449786862182Brian Paul          target != GL_PROXY_TEXTURE_1D_ARRAY &&
1910753660780a84187043d0dc4b0e0f449786862182Brian Paul          target != GL_TEXTURE_2D_ARRAY &&
1911753660780a84187043d0dc4b0e0f449786862182Brian Paul          target != GL_PROXY_TEXTURE_2D_ARRAY &&
19122b04dd9d2cba6ec3506e78016e64cffce6e8abf7Brian Paul          target != GL_TEXTURE_RECTANGLE_ARB &&
191329a7d7784def4f37d3fe38a123a17836dd7fafc0Anuj Phogat          target != GL_PROXY_TEXTURE_RECTANGLE_ARB &&
191429a7d7784def4f37d3fe38a123a17836dd7fafc0Anuj Phogat         !((_mesa_is_cube_face(target) || target == GL_PROXY_TEXTURE_CUBE_MAP) &&
19159c1b41879aab2ff7386c547a2ccce7686c018cf5Eric Anholt           (ctx->Version >= 30 || ctx->Extensions.EXT_gpu_shader4))) {
19161f5b1f98468d5e80be39e619ed15c422fbede8d3Brian Paul         _mesa_error(ctx, GL_INVALID_ENUM,
19171f5b1f98468d5e80be39e619ed15c422fbede8d3Brian Paul                     "glTexImage%dD(bad target for depth texture)",
19181f5b1f98468d5e80be39e619ed15c422fbede8d3Brian Paul                     dimensions);
1919601df9c742939c1f77de489561fe3e1d02f49618Brian Paul         return GL_TRUE;
1920601df9c742939c1f77de489561fe3e1d02f49618Brian Paul      }
1921601df9c742939c1f77de489561fe3e1d02f49618Brian Paul   }
1922601df9c742939c1f77de489561fe3e1d02f49618Brian Paul
1923601df9c742939c1f77de489561fe3e1d02f49618Brian Paul   /* additional checks for compressed textures */
1924df2c4cbcedb2dcf2aa44adaa6462d9becccbea49Anuj Phogat   if (_mesa_is_compressed_format(ctx, internalFormat)) {
19257404fa3f07a953343f2640fa97d82ab82ba5545dBrian Paul      if (!target_can_be_compressed(ctx, target, internalFormat)) {
19261f5b1f98468d5e80be39e619ed15c422fbede8d3Brian Paul         _mesa_error(ctx, GL_INVALID_ENUM,
19271f5b1f98468d5e80be39e619ed15c422fbede8d3Brian Paul                     "glTexImage%dD(target can't be compressed)", dimensions);
1928064cd7c78c3108012f5d15206c70470f7b500259Ian Romanick         return GL_TRUE;
192989fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      }
1930d4a38e86d4b4d66cca20ee63222f940cb73fa709Chia-I Wu      if (compressedteximage_only_format(ctx, internalFormat)) {
1931d4a38e86d4b4d66cca20ee63222f940cb73fa709Chia-I Wu         _mesa_error(ctx, GL_INVALID_OPERATION,
19321f5b1f98468d5e80be39e619ed15c422fbede8d3Brian Paul                     "glTexImage%dD(no compression for format)", dimensions);
1933d4a38e86d4b4d66cca20ee63222f940cb73fa709Chia-I Wu         return GL_TRUE;
1934d4a38e86d4b4d66cca20ee63222f940cb73fa709Chia-I Wu      }
193589fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      if (border != 0) {
19361f5b1f98468d5e80be39e619ed15c422fbede8d3Brian Paul         _mesa_error(ctx, GL_INVALID_OPERATION,
19371f5b1f98468d5e80be39e619ed15c422fbede8d3Brian Paul                     "glTexImage%dD(border!=0)", dimensions);
193889fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         return GL_TRUE;
193989fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      }
194089fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   }
194189fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul
1942326b981d3fafbf0cc253d2b224f0c9ad307038a3Brian Paul   /* additional checks for integer textures */
19439c1b41879aab2ff7386c547a2ccce7686c018cf5Eric Anholt   if ((ctx->Version >= 30 || ctx->Extensions.EXT_texture_integer) &&
19449ad8f431b2a47060bf05517246ab0fa8d249c800Jordan Justen       (_mesa_is_enum_format_integer(format) !=
19459ad8f431b2a47060bf05517246ab0fa8d249c800Jordan Justen        _mesa_is_enum_format_integer(internalFormat))) {
19461f5b1f98468d5e80be39e619ed15c422fbede8d3Brian Paul      _mesa_error(ctx, GL_INVALID_OPERATION,
19471f5b1f98468d5e80be39e619ed15c422fbede8d3Brian Paul                  "glTexImage%dD(integer/non-integer format mismatch)",
19481f5b1f98468d5e80be39e619ed15c422fbede8d3Brian Paul                  dimensions);
1949326b981d3fafbf0cc253d2b224f0c9ad307038a3Brian Paul      return GL_TRUE;
1950326b981d3fafbf0cc253d2b224f0c9ad307038a3Brian Paul   }
1951326b981d3fafbf0cc253d2b224f0c9ad307038a3Brian Paul
1952a0d736b3b7c325ab0295da3b0e3b92d170b44261Brian Paul   if (!mutable_tex_object(ctx, target)) {
1953a0d736b3b7c325ab0295da3b0e3b92d170b44261Brian Paul      _mesa_error(ctx, GL_INVALID_OPERATION,
1954a0d736b3b7c325ab0295da3b0e3b92d170b44261Brian Paul                  "glTexImage%dD(immutable texture)", dimensions);
1955a0d736b3b7c325ab0295da3b0e3b92d170b44261Brian Paul      return GL_TRUE;
1956a0d736b3b7c325ab0295da3b0e3b92d170b44261Brian Paul   }
1957a0d736b3b7c325ab0295da3b0e3b92d170b44261Brian Paul
1958afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   /* if we get here, the parameters are OK */
1959afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   return GL_FALSE;
1960afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg}
1961afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
1962afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
1963fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul/**
1964fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul * Error checking for glCompressedTexImage[123]D().
1965fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul * \param reason  returns reason for error, if any
1966fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul * \return error code or GL_NO_ERROR or PROXY_ERROR.
1967fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul */
1968bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paulstatic GLenum
1969bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paulcompressed_texture_error_check(struct gl_context *ctx, GLint dimensions,
1970bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paul                               GLenum target, GLint level,
1971bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paul                               GLenum internalFormat, GLsizei width,
1972bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paul                               GLsizei height, GLsizei depth, GLint border,
1973fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul                               GLsizei imageSize)
1974fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul{
1975f9bb66b1cee2272b20ff3086e84d17026b8e8bb9Brian Paul   const GLenum proxyTarget = _mesa_get_proxy_target(target);
1976fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul   const GLint maxLevels = _mesa_max_texture_levels(ctx, target);
1977fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul   GLint expectedSize;
1978fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul   GLenum choose_format;
1979fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul   GLenum choose_type;
1980fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul   GLenum proxy_format;
1981fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul   GLenum error = GL_NO_ERROR;
1982fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul   char *reason = ""; /* no error */
1983fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul
1984fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul   if (!target_can_be_compressed(ctx, target, internalFormat)) {
1985fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul      reason = "target";
1986fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul      error = GL_INVALID_ENUM;
1987fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul      goto error;
1988fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul   }
1989fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul
1990fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul   /* This will detect any invalid internalFormat value */
1991fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul   if (!_mesa_is_compressed_format(ctx, internalFormat)) {
1992fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul      reason = "internalFormat";
1993fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul      error = GL_INVALID_ENUM;
1994fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul      goto error;
1995fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul   }
1996fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul
1997fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul   switch (internalFormat) {
1998fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul#if FEATURE_ES
1999fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul   case GL_PALETTE4_RGB8_OES:
2000fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul   case GL_PALETTE4_RGBA8_OES:
2001fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul   case GL_PALETTE4_R5_G6_B5_OES:
2002fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul   case GL_PALETTE4_RGBA4_OES:
2003fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul   case GL_PALETTE4_RGB5_A1_OES:
2004fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul   case GL_PALETTE8_RGB8_OES:
2005fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul   case GL_PALETTE8_RGBA8_OES:
2006fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul   case GL_PALETTE8_R5_G6_B5_OES:
2007fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul   case GL_PALETTE8_RGBA4_OES:
2008fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul   case GL_PALETTE8_RGB5_A1_OES:
2009fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul      _mesa_cpal_compressed_format_type(internalFormat, &choose_format,
2010fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul					&choose_type);
2011fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul      proxy_format = choose_format;
2012fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul
2013fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul      /* check level (note that level should be zero or less!) */
2014fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul      if (level > 0 || level < -maxLevels) {
2015fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul	 reason = "level";
2016fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul	 error = GL_INVALID_VALUE;
2017fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul         goto error;
2018fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul      }
2019fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul
2020fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul      if (dimensions != 2) {
2021fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul	 reason = "compressed paletted textures must be 2D";
2022fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul	 error = GL_INVALID_OPERATION;
2023fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul         goto error;
2024fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul      }
2025fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul
2026fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul      /* Figure out the expected texture size (in bytes).  This will be
2027fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul       * checked against the actual size later.
2028fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul       */
2029fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul      expectedSize = _mesa_cpal_compressed_size(level, internalFormat,
2030fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul						width, height);
2031fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul
2032fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul      /* This is for the benefit of the TestProxyTexImage below.  It expects
2033fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul       * level to be non-negative.  OES_compressed_paletted_texture uses a
2034fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul       * weird mechanism where the level specified to glCompressedTexImage2D
2035fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul       * is -(n-1) number of levels in the texture, and the data specifies the
2036fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul       * complete mipmap stack.  This is done to ensure the palette is the
2037fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul       * same for all levels.
2038fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul       */
2039fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul      level = -level;
2040fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul      break;
2041fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul#endif
2042fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul
2043fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul   default:
2044fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul      choose_format = GL_NONE;
2045fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul      choose_type = GL_NONE;
2046fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul      proxy_format = internalFormat;
2047fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul
2048fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul      /* check level */
2049fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul      if (level < 0 || level >= maxLevels) {
2050fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul	 reason = "level";
2051fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul	 error = GL_INVALID_VALUE;
2052fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul         goto error;
2053fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul      }
2054fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul
2055fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul      /* Figure out the expected texture size (in bytes).  This will be
2056fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul       * checked against the actual size later.
2057fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul       */
2058fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul      expectedSize = compressed_tex_size(width, height, depth, internalFormat);
2059fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul      break;
2060fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul   }
2061fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul
2062fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul   /* This should really never fail */
2063fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul   if (_mesa_base_tex_format(ctx, internalFormat) < 0) {
2064fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul      reason = "internalFormat";
2065fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul      error = GL_INVALID_ENUM;
2066fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul      goto error;
2067fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul   }
2068fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul
2069fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul   /* No compressed formats support borders at this time */
2070fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul   if (border != 0) {
2071fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul      reason = "border != 0";
2072fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul      error = GL_INVALID_VALUE;
2073fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul      goto error;
2074fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul   }
2075fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul
2076fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul   /* For cube map, width must equal height */
2077fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul   if (_mesa_is_cube_face(target) && width != height) {
2078fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul      reason = "width != height";
2079fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul      error = GL_INVALID_VALUE;
2080fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul      goto error;
2081fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul   }
2082fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul
2083fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul   /* check image sizes */
2084fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul   if (!ctx->Driver.TestProxyTexImage(ctx, proxyTarget, level,
2085fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul				      proxy_format, choose_format,
2086fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul				      choose_type,
2087fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul				      width, height, depth, border)) {
2088fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul      /* See error comment above */
2089fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul      if (target == proxyTarget) {
2090fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul         return PROXY_ERROR;
2091fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul      }
2092fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul      reason = "invalid width, height or format";
2093fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul      error = GL_INVALID_OPERATION;
2094fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul      goto error;
2095fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul   }
2096fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul
2097fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul   /* check image size in bytes */
2098fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul   if (expectedSize != imageSize) {
2099fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul      /* Per GL_ARB_texture_compression:  GL_INVALID_VALUE is generated [...]
2100fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul       * if <imageSize> is not consistent with the format, dimensions, and
2101fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul       * contents of the specified image.
2102fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul       */
2103fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul      reason = "imageSize inconsistant with width/height/format";
2104fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul      error = GL_INVALID_VALUE;
2105fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul      goto error;
2106fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul   }
2107fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul
2108fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul   if (!mutable_tex_object(ctx, target)) {
2109fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul      reason = "immutable texture";
2110fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul      error = GL_INVALID_OPERATION;
2111fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul      goto error;
2112fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul   }
2113fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul
2114fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul   return GL_NO_ERROR;
2115fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul
2116fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paulerror:
2117fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul   _mesa_error(ctx, error, "glCompressedTexImage%dD(%s)", dimensions, reason);
2118fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul   return error;
2119fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul}
2120fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul
2121fe2cc65fbb37bca40ff215a3709dacbcd317350cBrian Paul
2122bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paul
21236dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/**
2124c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul * Test glTexSubImage[123]D() parameters for errors.
21256dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell *
21266dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param ctx GL context.
21276dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param dimensions texture image dimensions (must be 1, 2 or 3).
21286dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param target texture target given by the user.
21296dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param level image level given by the user.
21306dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param xoffset sub-image x offset given by the user.
21316dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param yoffset sub-image y offset given by the user.
21326dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param zoffset sub-image z offset given by the user.
21336dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param format pixel data format given by the user.
21346dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param type pixel data type given by the user.
21356dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param width image width given by the user.
21366dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param height image height given by the user.
21376dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param depth image depth given by the user.
21386dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell *
21396dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
21406dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell *
21416dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Verifies each of the parameters against the constants specified in
21423c59febf05e6af80d63e5b9a478a11b275ac429cBrian Paul * __struct gl_contextRec::Const and the supported extensions, and according
21433c59febf05e6af80d63e5b9a478a11b275ac429cBrian Paul * to the OpenGL specification.
2144afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg */
2145c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paulstatic GLboolean
2146f9995b30756140724f41daf963fa06167912be7fKristian Høgsbergsubtexture_error_check( struct gl_context *ctx, GLuint dimensions,
2147c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul                        GLenum target, GLint level,
2148c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul                        GLint xoffset, GLint yoffset, GLint zoffset,
2149c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul                        GLint width, GLint height, GLint depth,
2150c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul                        GLenum format, GLenum type )
2151afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg{
2152627b435dfe17698a1c69e9a259838fc6f2e6bd4eBrian Paul   GLenum err;
2153627b435dfe17698a1c69e9a259838fc6f2e6bd4eBrian Paul
2154738318bb75dea8dac4465f53850987f6062a732dBrian Paul   /* Basic level check */
2155738318bb75dea8dac4465f53850987f6062a732dBrian Paul   if (level < 0 || level >= MAX_TEXTURE_LEVELS) {
21564e9676fb13f60ecdbc247b120031f18cd3febcb0Brian Paul      _mesa_error(ctx, GL_INVALID_ENUM, "glTexSubImage2D(level=%d)", level);
2157c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      return GL_TRUE;
2158c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   }
2159afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
216045124e043da5a67e1081526d61b99422f4ccde19Brian Paul   /* Check for negative sizes */
2161c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   if (width < 0) {
21624e9676fb13f60ecdbc247b120031f18cd3febcb0Brian Paul      _mesa_error(ctx, GL_INVALID_VALUE,
21634e9676fb13f60ecdbc247b120031f18cd3febcb0Brian Paul                  "glTexSubImage%dD(width=%d)", dimensions, width);
2164c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      return GL_TRUE;
2165c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   }
2166c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   if (height < 0 && dimensions > 1) {
21674e9676fb13f60ecdbc247b120031f18cd3febcb0Brian Paul      _mesa_error(ctx, GL_INVALID_VALUE,
21684e9676fb13f60ecdbc247b120031f18cd3febcb0Brian Paul                  "glTexSubImage%dD(height=%d)", dimensions, height);
2169c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      return GL_TRUE;
2170c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   }
2171c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   if (depth < 0 && dimensions > 2) {
2172738318bb75dea8dac4465f53850987f6062a732dBrian Paul      _mesa_error(ctx, GL_INVALID_VALUE,
2173738318bb75dea8dac4465f53850987f6062a732dBrian Paul                  "glTexSubImage%dD(depth=%d)", dimensions, depth);
2174c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      return GL_TRUE;
2175c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   }
2176afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
21779848e86af0c937a6f7609289ab2705c3535f378fIan Romanick   /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
21789848e86af0c937a6f7609289ab2705c3535f378fIan Romanick    * combinations of format and type that can be used.  Formats and types
21799848e86af0c937a6f7609289ab2705c3535f378fIan Romanick    * that require additional extensions (e.g., GL_FLOAT requires
21809848e86af0c937a6f7609289ab2705c3535f378fIan Romanick    * GL_OES_texture_float) are filtered elsewhere.
21819848e86af0c937a6f7609289ab2705c3535f378fIan Romanick    */
21829848e86af0c937a6f7609289ab2705c3535f378fIan Romanick   if (_mesa_is_gles(ctx) && !_mesa_is_gles3(ctx)) {
21839848e86af0c937a6f7609289ab2705c3535f378fIan Romanick      err = _mesa_es_error_check_format_and_type(format, type, dimensions);
21849848e86af0c937a6f7609289ab2705c3535f378fIan Romanick      if (err != GL_NO_ERROR) {
21859848e86af0c937a6f7609289ab2705c3535f378fIan Romanick         _mesa_error(ctx, err,
21869848e86af0c937a6f7609289ab2705c3535f378fIan Romanick                     "glTexSubImage%dD(format = %s, type = %s)",
21879848e86af0c937a6f7609289ab2705c3535f378fIan Romanick                     dimensions,
21889848e86af0c937a6f7609289ab2705c3535f378fIan Romanick                     _mesa_lookup_enum_by_nr(format),
21899848e86af0c937a6f7609289ab2705c3535f378fIan Romanick                     _mesa_lookup_enum_by_nr(type));
21909848e86af0c937a6f7609289ab2705c3535f378fIan Romanick         return GL_TRUE;
21919848e86af0c937a6f7609289ab2705c3535f378fIan Romanick      }
21929848e86af0c937a6f7609289ab2705c3535f378fIan Romanick   }
21939848e86af0c937a6f7609289ab2705c3535f378fIan Romanick
2194627b435dfe17698a1c69e9a259838fc6f2e6bd4eBrian Paul   err = _mesa_error_check_format_and_type(ctx, format, type);
2195627b435dfe17698a1c69e9a259838fc6f2e6bd4eBrian Paul   if (err != GL_NO_ERROR) {
2196627b435dfe17698a1c69e9a259838fc6f2e6bd4eBrian Paul      _mesa_error(ctx, err,
21978a8919a7ddd2348c4a4cbcbab2358c49e47e2ea5Brian Paul                  "glTexSubImage%dD(incompatible format 0x%x, type 0x%x)",
21988a8919a7ddd2348c4a4cbcbab2358c49e47e2ea5Brian Paul                  dimensions, format, type);
21995ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell      return GL_TRUE;
22005ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell   }
22015ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell
22025ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell   return GL_FALSE;
22035ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell}
2204f2718b0966f54049056e16e7cca08718341557b2Brian Paul
2205d580c0c8f7cad69b808118ef2aa6161f62f160d8Brian Paul
2206d580c0c8f7cad69b808118ef2aa6161f62f160d8Brian Paul/**
2207d580c0c8f7cad69b808118ef2aa6161f62f160d8Brian Paul * Do second part of glTexSubImage which depends on the destination texture.
2208d580c0c8f7cad69b808118ef2aa6161f62f160d8Brian Paul * \return GL_TRUE if error recorded, GL_FALSE otherwise
2209d580c0c8f7cad69b808118ef2aa6161f62f160d8Brian Paul */
22105ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwellstatic GLboolean
2211f9995b30756140724f41daf963fa06167912be7fKristian Høgsbergsubtexture_error_check2( struct gl_context *ctx, GLuint dimensions,
22125ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell			 GLenum target, GLint level,
22135ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell			 GLint xoffset, GLint yoffset, GLint zoffset,
22145ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell			 GLint width, GLint height, GLint depth,
22155ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell			 GLenum format, GLenum type,
22165ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell			 const struct gl_texture_image *destTex )
22175ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell{
2218c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   if (!destTex) {
2219738318bb75dea8dac4465f53850987f6062a732dBrian Paul      /* undefined image level */
2220738318bb75dea8dac4465f53850987f6062a732dBrian Paul      _mesa_error(ctx, GL_INVALID_OPERATION, "glTexSubImage%dD", dimensions);
2221c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      return GL_TRUE;
2222c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   }
2223afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2224c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   if (xoffset < -((GLint)destTex->Border)) {
2225738318bb75dea8dac4465f53850987f6062a732dBrian Paul      _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage%dD(xoffset)",
2226738318bb75dea8dac4465f53850987f6062a732dBrian Paul                  dimensions);
2227c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      return GL_TRUE;
2228afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   }
2229c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   if (xoffset + width > (GLint) (destTex->Width + destTex->Border)) {
2230738318bb75dea8dac4465f53850987f6062a732dBrian Paul      _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage%dD(xoffset+width)",
2231738318bb75dea8dac4465f53850987f6062a732dBrian Paul                  dimensions);
2232c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      return GL_TRUE;
2233c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   }
2234c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   if (dimensions > 1) {
22356eba268ec65523fe8b2ec7dee670b2b5d4ebe1d9Brian Paul      GLint yBorder = (target == GL_TEXTURE_1D_ARRAY) ? 0 : destTex->Border;
22366eba268ec65523fe8b2ec7dee670b2b5d4ebe1d9Brian Paul      if (yoffset < -yBorder) {
2237738318bb75dea8dac4465f53850987f6062a732dBrian Paul         _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage%dD(yoffset)",
2238738318bb75dea8dac4465f53850987f6062a732dBrian Paul                     dimensions);
2239c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         return GL_TRUE;
2240afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      }
22416eba268ec65523fe8b2ec7dee670b2b5d4ebe1d9Brian Paul      if (yoffset + height > (GLint) destTex->Height + yBorder) {
2242738318bb75dea8dac4465f53850987f6062a732dBrian Paul         _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage%dD(yoffset+height)",
2243738318bb75dea8dac4465f53850987f6062a732dBrian Paul                     dimensions);
2244c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         return GL_TRUE;
2245afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      }
2246afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   }
2247c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   if (dimensions > 2) {
22486eba268ec65523fe8b2ec7dee670b2b5d4ebe1d9Brian Paul      GLint zBorder = (target == GL_TEXTURE_2D_ARRAY) ? 0 : destTex->Border;
22496eba268ec65523fe8b2ec7dee670b2b5d4ebe1d9Brian Paul      if (zoffset < -zBorder) {
225008836341788a9f9d638d9dc8328510ccd18ddeb5Brian Paul         _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage3D(zoffset)");
2251c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         return GL_TRUE;
2252c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      }
22536eba268ec65523fe8b2ec7dee670b2b5d4ebe1d9Brian Paul      if (zoffset + depth  > (GLint) destTex->Depth + zBorder) {
225408836341788a9f9d638d9dc8328510ccd18ddeb5Brian Paul         _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage3D(zoffset+depth)");
2255c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         return GL_TRUE;
2256c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      }
2257afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   }
2258afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2259b6bdafdf2cf1110b4a5ca7cf9e1c3dcb124b800fBrian Paul   if (_mesa_is_format_compressed(destTex->TexFormat)) {
2260d580c0c8f7cad69b808118ef2aa6161f62f160d8Brian Paul      GLuint bw, bh;
2261d580c0c8f7cad69b808118ef2aa6161f62f160d8Brian Paul
2262d4a38e86d4b4d66cca20ee63222f940cb73fa709Chia-I Wu      if (compressedteximage_only_format(ctx, destTex->InternalFormat)) {
2263d4a38e86d4b4d66cca20ee63222f940cb73fa709Chia-I Wu         _mesa_error(ctx, GL_INVALID_OPERATION,
2264d4a38e86d4b4d66cca20ee63222f940cb73fa709Chia-I Wu               "glTexSubImage%dD(no compression for format)", dimensions);
2265d4a38e86d4b4d66cca20ee63222f940cb73fa709Chia-I Wu         return GL_TRUE;
2266d4a38e86d4b4d66cca20ee63222f940cb73fa709Chia-I Wu      }
2267d4a38e86d4b4d66cca20ee63222f940cb73fa709Chia-I Wu
2268d580c0c8f7cad69b808118ef2aa6161f62f160d8Brian Paul      /* do tests which depend on compression block size */
2269d580c0c8f7cad69b808118ef2aa6161f62f160d8Brian Paul      _mesa_get_format_block_size(destTex->TexFormat, &bw, &bh);
2270d580c0c8f7cad69b808118ef2aa6161f62f160d8Brian Paul
2271d580c0c8f7cad69b808118ef2aa6161f62f160d8Brian Paul      /* offset must be multiple of block size */
2272d580c0c8f7cad69b808118ef2aa6161f62f160d8Brian Paul      if ((xoffset % bw != 0) || (yoffset % bh != 0)) {
2273e4276667dafc8de0c6e64af8300fc7598437de6eBrian Paul         _mesa_error(ctx, GL_INVALID_OPERATION,
22748978e45e50118d0e3660fdd9cd58b8790c0a6bb8Vinson Lee                     "glTexSubImage%dD(xoffset = %d, yoffset = %d)",
2275d580c0c8f7cad69b808118ef2aa6161f62f160d8Brian Paul                     dimensions, xoffset, yoffset);
2276e4276667dafc8de0c6e64af8300fc7598437de6eBrian Paul         return GL_TRUE;
2277e4276667dafc8de0c6e64af8300fc7598437de6eBrian Paul      }
2278d580c0c8f7cad69b808118ef2aa6161f62f160d8Brian Paul      /* size must be multiple of bw by bh or equal to whole texture size */
2279d580c0c8f7cad69b808118ef2aa6161f62f160d8Brian Paul      if ((width % bw != 0) && (GLuint) width != destTex->Width) {
2280e4276667dafc8de0c6e64af8300fc7598437de6eBrian Paul         _mesa_error(ctx, GL_INVALID_OPERATION,
22818978e45e50118d0e3660fdd9cd58b8790c0a6bb8Vinson Lee                     "glTexSubImage%dD(width = %d)", dimensions, width);
2282e4276667dafc8de0c6e64af8300fc7598437de6eBrian Paul         return GL_TRUE;
228389fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      }
2284d580c0c8f7cad69b808118ef2aa6161f62f160d8Brian Paul      if ((height % bh != 0) && (GLuint) height != destTex->Height) {
228589fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         _mesa_error(ctx, GL_INVALID_OPERATION,
22868978e45e50118d0e3660fdd9cd58b8790c0a6bb8Vinson Lee                     "glTexSubImage%dD(height = %d)", dimensions, height);
228789fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         return GL_TRUE;
228889fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      }
2289e4276667dafc8de0c6e64af8300fc7598437de6eBrian Paul   }
2290e4276667dafc8de0c6e64af8300fc7598437de6eBrian Paul
22919c1b41879aab2ff7386c547a2ccce7686c018cf5Eric Anholt   if (ctx->Version >= 30 || ctx->Extensions.EXT_texture_integer) {
22926f3d91840945fbd9221a247e4f95b32c4051f202Brian Paul      /* both source and dest must be integer-valued, or neither */
22936f3d91840945fbd9221a247e4f95b32c4051f202Brian Paul      if (_mesa_is_format_integer_color(destTex->TexFormat) !=
22949ad8f431b2a47060bf05517246ab0fa8d249c800Jordan Justen          _mesa_is_enum_format_integer(format)) {
22956f3d91840945fbd9221a247e4f95b32c4051f202Brian Paul         _mesa_error(ctx, GL_INVALID_OPERATION,
22966f3d91840945fbd9221a247e4f95b32c4051f202Brian Paul                     "glTexSubImage%dD(integer/non-integer format mismatch)",
22976f3d91840945fbd9221a247e4f95b32c4051f202Brian Paul                     dimensions);
22986f3d91840945fbd9221a247e4f95b32c4051f202Brian Paul         return GL_TRUE;
22996f3d91840945fbd9221a247e4f95b32c4051f202Brian Paul      }
23006f3d91840945fbd9221a247e4f95b32c4051f202Brian Paul   }
23016f3d91840945fbd9221a247e4f95b32c4051f202Brian Paul
2302c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   return GL_FALSE;
2303c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul}
2304afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2305afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
23066dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/**
2307c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul * Test glCopyTexImage[12]D() parameters for errors.
23086dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell *
23096dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param ctx GL context.
23106dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param dimensions texture image dimensions (must be 1, 2 or 3).
23116dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param target texture target given by the user.
23126dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param level image level given by the user.
23136dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param internalFormat internal format given by the user.
23146dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param width image width given by the user.
23156dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param height image height given by the user.
23166dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param border texture border.
23176dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell *
23186dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
23196dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell *
23206dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Verifies each of the parameters against the constants specified in
23213c59febf05e6af80d63e5b9a478a11b275ac429cBrian Paul * __struct gl_contextRec::Const and the supported extensions, and according
23223c59febf05e6af80d63e5b9a478a11b275ac429cBrian Paul * to the OpenGL specification.
2323afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg */
2324c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paulstatic GLboolean
2325f9995b30756140724f41daf963fa06167912be7fKristian Høgsbergcopytexture_error_check( struct gl_context *ctx, GLuint dimensions,
2326c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul                         GLenum target, GLint level, GLint internalFormat,
2327c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul                         GLint width, GLint height, GLint border )
2328afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg{
2329f9bb66b1cee2272b20ff3086e84d17026b8e8bb9Brian Paul   const GLenum proxyTarget = _mesa_get_proxy_target(target);
233011f386fb504b9f07dd501a65f31185cfa0e9a679Brian Paul   const GLenum type = GL_FLOAT;
2331738318bb75dea8dac4465f53850987f6062a732dBrian Paul   GLboolean sizeOK;
2332e06277bd886a9a79f0bf9b175ec26fbe187689c2Brian Paul   GLint baseFormat;
2333afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
233411f386fb504b9f07dd501a65f31185cfa0e9a679Brian Paul   /* check target */
233509a5e028a6d254f619132e302a96c8535018603dBrian Paul   if (!legal_texsubimage_target(ctx, dimensions, target)) {
233611f386fb504b9f07dd501a65f31185cfa0e9a679Brian Paul      _mesa_error(ctx, GL_INVALID_ENUM, "glCopyTexImage%uD(target=%s)",
233711f386fb504b9f07dd501a65f31185cfa0e9a679Brian Paul                  dimensions, _mesa_lookup_enum_by_nr(target));
233811f386fb504b9f07dd501a65f31185cfa0e9a679Brian Paul      return GL_TRUE;
233911f386fb504b9f07dd501a65f31185cfa0e9a679Brian Paul   }
234011f386fb504b9f07dd501a65f31185cfa0e9a679Brian Paul
2341738318bb75dea8dac4465f53850987f6062a732dBrian Paul   /* Basic level check (more checking in ctx->Driver.TestProxyTexImage) */
2342738318bb75dea8dac4465f53850987f6062a732dBrian Paul   if (level < 0 || level >= MAX_TEXTURE_LEVELS) {
23435ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell      _mesa_error(ctx, GL_INVALID_VALUE,
23445ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell                  "glCopyTexImage%dD(level=%d)", dimensions, level);
23455ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell      return GL_TRUE;
23465ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell   }
23475ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell
234834bd1233a9874fe12a822c4fcb926d48456e1f29Brian Paul   /* Check that the source buffer is complete */
234936ede89687fe2de213f2637ab7acfb80cfd856cdBrian Paul   if (_mesa_is_user_fbo(ctx->ReadBuffer)) {
2350b0fceae22f008f3963a392ebd57d7d2a095a5e05Brian Paul      if (ctx->ReadBuffer->_Status == 0) {
2351b0fceae22f008f3963a392ebd57d7d2a095a5e05Brian Paul         _mesa_test_framebuffer_completeness(ctx, ctx->ReadBuffer);
2352b0fceae22f008f3963a392ebd57d7d2a095a5e05Brian Paul      }
235334bd1233a9874fe12a822c4fcb926d48456e1f29Brian Paul      if (ctx->ReadBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
235434bd1233a9874fe12a822c4fcb926d48456e1f29Brian Paul         _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
235534bd1233a9874fe12a822c4fcb926d48456e1f29Brian Paul                     "glCopyTexImage%dD(invalid readbuffer)", dimensions);
235634bd1233a9874fe12a822c4fcb926d48456e1f29Brian Paul         return GL_TRUE;
235734bd1233a9874fe12a822c4fcb926d48456e1f29Brian Paul      }
23586950a4faf650fe119ee97aa18b006eed099038beEric Anholt
23596950a4faf650fe119ee97aa18b006eed099038beEric Anholt      if (ctx->ReadBuffer->Visual.samples > 0) {
2360e9b908b014be57cd2d3f0432adf16ac5b9631325Paul Berry	 _mesa_error(ctx, GL_INVALID_OPERATION,
23616950a4faf650fe119ee97aa18b006eed099038beEric Anholt		     "glCopyTexImage%dD(multisample FBO)",
23626950a4faf650fe119ee97aa18b006eed099038beEric Anholt		     dimensions);
23636950a4faf650fe119ee97aa18b006eed099038beEric Anholt	 return GL_TRUE;
23646950a4faf650fe119ee97aa18b006eed099038beEric Anholt      }
236534bd1233a9874fe12a822c4fcb926d48456e1f29Brian Paul   }
236634bd1233a9874fe12a822c4fcb926d48456e1f29Brian Paul
2367738318bb75dea8dac4465f53850987f6062a732dBrian Paul   /* Check border */
2368738318bb75dea8dac4465f53850987f6062a732dBrian Paul   if (border < 0 || border > 1 ||
23690686ccac950bac1a650400b29c47ddaafea22fa9Ian Romanick       ((ctx->API != API_OPENGL ||
23700686ccac950bac1a650400b29c47ddaafea22fa9Ian Romanick         target == GL_TEXTURE_RECTANGLE_NV ||
2371738318bb75dea8dac4465f53850987f6062a732dBrian Paul         target == GL_PROXY_TEXTURE_RECTANGLE_NV) && border != 0)) {
237259d965333c05534475b60042571366656b97ab9dIan Romanick      _mesa_error(ctx, GL_INVALID_VALUE,
237359d965333c05534475b60042571366656b97ab9dIan Romanick                  "glCopyTexImage%dD(border=%d)", dimensions, border);
2374738318bb75dea8dac4465f53850987f6062a732dBrian Paul      return GL_TRUE;
2375738318bb75dea8dac4465f53850987f6062a732dBrian Paul   }
2376738318bb75dea8dac4465f53850987f6062a732dBrian Paul
2377c11096e94a68ce0a1d7c9cda0f178ceca44ce25dIan Romanick   /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
2378c11096e94a68ce0a1d7c9cda0f178ceca44ce25dIan Romanick    * internalFormat.
2379c11096e94a68ce0a1d7c9cda0f178ceca44ce25dIan Romanick    */
2380c11096e94a68ce0a1d7c9cda0f178ceca44ce25dIan Romanick   if (_mesa_is_gles(ctx) && !_mesa_is_gles3(ctx)) {
2381c11096e94a68ce0a1d7c9cda0f178ceca44ce25dIan Romanick      switch (internalFormat) {
2382c11096e94a68ce0a1d7c9cda0f178ceca44ce25dIan Romanick      case GL_ALPHA:
2383c11096e94a68ce0a1d7c9cda0f178ceca44ce25dIan Romanick      case GL_RGB:
2384c11096e94a68ce0a1d7c9cda0f178ceca44ce25dIan Romanick      case GL_RGBA:
2385c11096e94a68ce0a1d7c9cda0f178ceca44ce25dIan Romanick      case GL_LUMINANCE:
2386c11096e94a68ce0a1d7c9cda0f178ceca44ce25dIan Romanick      case GL_LUMINANCE_ALPHA:
2387c11096e94a68ce0a1d7c9cda0f178ceca44ce25dIan Romanick         break;
2388c11096e94a68ce0a1d7c9cda0f178ceca44ce25dIan Romanick      default:
2389c11096e94a68ce0a1d7c9cda0f178ceca44ce25dIan Romanick         _mesa_error(ctx, GL_INVALID_VALUE,
2390c11096e94a68ce0a1d7c9cda0f178ceca44ce25dIan Romanick                     "glCopyTexImage%dD(internalFormat)", dimensions);
2391c11096e94a68ce0a1d7c9cda0f178ceca44ce25dIan Romanick         return GL_TRUE;
2392c11096e94a68ce0a1d7c9cda0f178ceca44ce25dIan Romanick      }
2393c11096e94a68ce0a1d7c9cda0f178ceca44ce25dIan Romanick   }
2394c11096e94a68ce0a1d7c9cda0f178ceca44ce25dIan Romanick
2395e06277bd886a9a79f0bf9b175ec26fbe187689c2Brian Paul   baseFormat = _mesa_base_tex_format(ctx, internalFormat);
2396e06277bd886a9a79f0bf9b175ec26fbe187689c2Brian Paul   if (baseFormat < 0) {
23970899afae33a29d1f6d15ebc96dc6ea8a8371cdb6Brian Paul      _mesa_error(ctx, GL_INVALID_VALUE,
23980899afae33a29d1f6d15ebc96dc6ea8a8371cdb6Brian Paul                  "glCopyTexImage%dD(internalFormat)", dimensions);
23990899afae33a29d1f6d15ebc96dc6ea8a8371cdb6Brian Paul      return GL_TRUE;
24000899afae33a29d1f6d15ebc96dc6ea8a8371cdb6Brian Paul   }
24010899afae33a29d1f6d15ebc96dc6ea8a8371cdb6Brian Paul
2402e06277bd886a9a79f0bf9b175ec26fbe187689c2Brian Paul   if (!_mesa_source_buffer_exists(ctx, baseFormat)) {
240334bd1233a9874fe12a822c4fcb926d48456e1f29Brian Paul      _mesa_error(ctx, GL_INVALID_OPERATION,
240434bd1233a9874fe12a822c4fcb926d48456e1f29Brian Paul                  "glCopyTexImage%dD(missing readbuffer)", dimensions);
240534bd1233a9874fe12a822c4fcb926d48456e1f29Brian Paul      return GL_TRUE;
240634bd1233a9874fe12a822c4fcb926d48456e1f29Brian Paul   }
240734bd1233a9874fe12a822c4fcb926d48456e1f29Brian Paul
2408422b18794eacc8f0be5b4e9611e2726f4e0d86b6Eric Anholt   /* From the EXT_texture_integer spec:
2409422b18794eacc8f0be5b4e9611e2726f4e0d86b6Eric Anholt    *
2410422b18794eacc8f0be5b4e9611e2726f4e0d86b6Eric Anholt    *     "INVALID_OPERATION is generated by CopyTexImage* and CopyTexSubImage*
2411422b18794eacc8f0be5b4e9611e2726f4e0d86b6Eric Anholt    *      if the texture internalformat is an integer format and the read color
2412422b18794eacc8f0be5b4e9611e2726f4e0d86b6Eric Anholt    *      buffer is not an integer format, or if the internalformat is not an
2413422b18794eacc8f0be5b4e9611e2726f4e0d86b6Eric Anholt    *      integer format and the read color buffer is an integer format."
2414422b18794eacc8f0be5b4e9611e2726f4e0d86b6Eric Anholt    */
2415422b18794eacc8f0be5b4e9611e2726f4e0d86b6Eric Anholt   if (_mesa_is_color_format(internalFormat)) {
2416422b18794eacc8f0be5b4e9611e2726f4e0d86b6Eric Anholt      struct gl_renderbuffer *rb = ctx->ReadBuffer->_ColorReadBuffer;
2417422b18794eacc8f0be5b4e9611e2726f4e0d86b6Eric Anholt
24189ad8f431b2a47060bf05517246ab0fa8d249c800Jordan Justen      if (_mesa_is_enum_format_integer(rb->InternalFormat) !=
24199ad8f431b2a47060bf05517246ab0fa8d249c800Jordan Justen	  _mesa_is_enum_format_integer(internalFormat)) {
2420422b18794eacc8f0be5b4e9611e2726f4e0d86b6Eric Anholt	 _mesa_error(ctx, GL_INVALID_OPERATION,
2421422b18794eacc8f0be5b4e9611e2726f4e0d86b6Eric Anholt		     "glCopyTexImage%dD(integer vs non-integer)", dimensions);
2422422b18794eacc8f0be5b4e9611e2726f4e0d86b6Eric Anholt	 return GL_TRUE;
2423422b18794eacc8f0be5b4e9611e2726f4e0d86b6Eric Anholt      }
2424422b18794eacc8f0be5b4e9611e2726f4e0d86b6Eric Anholt   }
2425422b18794eacc8f0be5b4e9611e2726f4e0d86b6Eric Anholt
242611f386fb504b9f07dd501a65f31185cfa0e9a679Brian Paul   /* Do size, level checking */
242711f386fb504b9f07dd501a65f31185cfa0e9a679Brian Paul   sizeOK = (proxyTarget == GL_PROXY_TEXTURE_CUBE_MAP_ARB)
242811f386fb504b9f07dd501a65f31185cfa0e9a679Brian Paul      ? (width == height) : 1;
2429738318bb75dea8dac4465f53850987f6062a732dBrian Paul
243011f386fb504b9f07dd501a65f31185cfa0e9a679Brian Paul   sizeOK = sizeOK && ctx->Driver.TestProxyTexImage(ctx, proxyTarget, level,
2431e06277bd886a9a79f0bf9b175ec26fbe187689c2Brian Paul                                                    internalFormat, baseFormat,
243211f386fb504b9f07dd501a65f31185cfa0e9a679Brian Paul                                                    type, width, height,
243311f386fb504b9f07dd501a65f31185cfa0e9a679Brian Paul                                                    1, border);
2434c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul
2435738318bb75dea8dac4465f53850987f6062a732dBrian Paul   if (!sizeOK) {
2436738318bb75dea8dac4465f53850987f6062a732dBrian Paul      if (dimensions == 1) {
24374e9676fb13f60ecdbc247b120031f18cd3febcb0Brian Paul         _mesa_error(ctx, GL_INVALID_VALUE,
2438738318bb75dea8dac4465f53850987f6062a732dBrian Paul                     "glCopyTexImage1D(width=%d)", width);
2439c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      }
2440738318bb75dea8dac4465f53850987f6062a732dBrian Paul      else {
2441738318bb75dea8dac4465f53850987f6062a732dBrian Paul         ASSERT(dimensions == 2);
2442738318bb75dea8dac4465f53850987f6062a732dBrian Paul         _mesa_error(ctx, GL_INVALID_VALUE,
2443738318bb75dea8dac4465f53850987f6062a732dBrian Paul                     "glCopyTexImage2D(width=%d, height=%d)", width, height);
2444ad817704fa2c6d8449f55565f07ab5592a574fb6Brian Paul      }
2445c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      return GL_TRUE;
2446c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   }
2447c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul
2448df2c4cbcedb2dcf2aa44adaa6462d9becccbea49Anuj Phogat   if (_mesa_is_compressed_format(ctx, internalFormat)) {
24497404fa3f07a953343f2640fa97d82ab82ba5545dBrian Paul      if (!target_can_be_compressed(ctx, target, internalFormat)) {
245089fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         _mesa_error(ctx, GL_INVALID_ENUM,
24512236a301c35d29a8e0775d2b62499d8843607ee1Brian Paul                     "glCopyTexImage%dD(target)", dimensions);
245289fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         return GL_TRUE;
245389fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      }
2454d4a38e86d4b4d66cca20ee63222f940cb73fa709Chia-I Wu      if (compressedteximage_only_format(ctx, internalFormat)) {
2455d4a38e86d4b4d66cca20ee63222f940cb73fa709Chia-I Wu         _mesa_error(ctx, GL_INVALID_OPERATION,
2456d4a38e86d4b4d66cca20ee63222f940cb73fa709Chia-I Wu               "glCopyTexImage%dD(no compression for format)", dimensions);
2457d4a38e86d4b4d66cca20ee63222f940cb73fa709Chia-I Wu         return GL_TRUE;
2458d4a38e86d4b4d66cca20ee63222f940cb73fa709Chia-I Wu      }
245989fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      if (border != 0) {
246089fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         _mesa_error(ctx, GL_INVALID_OPERATION,
24618978e45e50118d0e3660fdd9cd58b8790c0a6bb8Vinson Lee                     "glCopyTexImage%dD(border!=0)", dimensions);
24620899afae33a29d1f6d15ebc96dc6ea8a8371cdb6Brian Paul         return GL_TRUE;
24630899afae33a29d1f6d15ebc96dc6ea8a8371cdb6Brian Paul      }
24640899afae33a29d1f6d15ebc96dc6ea8a8371cdb6Brian Paul   }
246589fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul
2466a0d736b3b7c325ab0295da3b0e3b92d170b44261Brian Paul   if (!mutable_tex_object(ctx, target)) {
2467a0d736b3b7c325ab0295da3b0e3b92d170b44261Brian Paul      _mesa_error(ctx, GL_INVALID_OPERATION,
2468a0d736b3b7c325ab0295da3b0e3b92d170b44261Brian Paul                  "glCopyTexImage%dD(immutable texture)", dimensions);
2469a0d736b3b7c325ab0295da3b0e3b92d170b44261Brian Paul      return GL_TRUE;
2470a0d736b3b7c325ab0295da3b0e3b92d170b44261Brian Paul   }
2471a0d736b3b7c325ab0295da3b0e3b92d170b44261Brian Paul
2472c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   /* if we get here, the parameters are OK */
2473c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   return GL_FALSE;
2474c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul}
2475c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul
2476c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul
24776dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/**
247834bd1233a9874fe12a822c4fcb926d48456e1f29Brian Paul * Test glCopyTexSubImage[12]D() parameters for errors.
24799dfd54fa83f9d2266ee637721365f8922deb17fcBrian Paul * Note that this is the first part of error checking.
24809dfd54fa83f9d2266ee637721365f8922deb17fcBrian Paul * See also copytexsubimage_error_check2() below for the second part.
24816dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell *
24826dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param ctx GL context.
24836dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param dimensions texture image dimensions (must be 1, 2 or 3).
24846dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param target texture target given by the user.
24856dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param level image level given by the user.
24866dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell *
24876dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
24886dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell */
2489c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paulstatic GLboolean
2490f9995b30756140724f41daf963fa06167912be7fKristian Høgsbergcopytexsubimage_error_check1( struct gl_context *ctx, GLuint dimensions,
24919dfd54fa83f9d2266ee637721365f8922deb17fcBrian Paul                              GLenum target, GLint level)
2492c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul{
249334bd1233a9874fe12a822c4fcb926d48456e1f29Brian Paul   /* Check that the source buffer is complete */
249436ede89687fe2de213f2637ab7acfb80cfd856cdBrian Paul   if (_mesa_is_user_fbo(ctx->ReadBuffer)) {
2495b0fceae22f008f3963a392ebd57d7d2a095a5e05Brian Paul      if (ctx->ReadBuffer->_Status == 0) {
2496b0fceae22f008f3963a392ebd57d7d2a095a5e05Brian Paul         _mesa_test_framebuffer_completeness(ctx, ctx->ReadBuffer);
2497b0fceae22f008f3963a392ebd57d7d2a095a5e05Brian Paul      }
249834bd1233a9874fe12a822c4fcb926d48456e1f29Brian Paul      if (ctx->ReadBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
249934bd1233a9874fe12a822c4fcb926d48456e1f29Brian Paul         _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
250034bd1233a9874fe12a822c4fcb926d48456e1f29Brian Paul                     "glCopyTexImage%dD(invalid readbuffer)", dimensions);
250134bd1233a9874fe12a822c4fcb926d48456e1f29Brian Paul         return GL_TRUE;
250234bd1233a9874fe12a822c4fcb926d48456e1f29Brian Paul      }
25036950a4faf650fe119ee97aa18b006eed099038beEric Anholt
25046950a4faf650fe119ee97aa18b006eed099038beEric Anholt      if (ctx->ReadBuffer->Visual.samples > 0) {
2505e9b908b014be57cd2d3f0432adf16ac5b9631325Paul Berry	 _mesa_error(ctx, GL_INVALID_OPERATION,
25066950a4faf650fe119ee97aa18b006eed099038beEric Anholt		     "glCopyTexSubImage%dD(multisample FBO)",
25076950a4faf650fe119ee97aa18b006eed099038beEric Anholt		     dimensions);
25086950a4faf650fe119ee97aa18b006eed099038beEric Anholt	 return GL_TRUE;
25096950a4faf650fe119ee97aa18b006eed099038beEric Anholt      }
251034bd1233a9874fe12a822c4fcb926d48456e1f29Brian Paul   }
251134bd1233a9874fe12a822c4fcb926d48456e1f29Brian Paul
25121c23b860cea93534f726d2a18658444f37f43538Brian Paul   /* check target (proxies not allowed) */
251309a5e028a6d254f619132e302a96c8535018603dBrian Paul   if (!legal_texsubimage_target(ctx, dimensions, target)) {
25141c23b860cea93534f726d2a18658444f37f43538Brian Paul      _mesa_error(ctx, GL_INVALID_ENUM, "glCopyTexSubImage%uD(target=%s)",
25151c23b860cea93534f726d2a18658444f37f43538Brian Paul                  dimensions, _mesa_lookup_enum_by_nr(target));
25161c23b860cea93534f726d2a18658444f37f43538Brian Paul      return GL_TRUE;
2517c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   }
2518c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul
2519738318bb75dea8dac4465f53850987f6062a732dBrian Paul   /* Check level */
2520738318bb75dea8dac4465f53850987f6062a732dBrian Paul   if (level < 0 || level >= MAX_TEXTURE_LEVELS) {
25214e9676fb13f60ecdbc247b120031f18cd3febcb0Brian Paul      _mesa_error(ctx, GL_INVALID_VALUE,
25224e9676fb13f60ecdbc247b120031f18cd3febcb0Brian Paul                  "glCopyTexSubImage%dD(level=%d)", dimensions, level);
2523c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      return GL_TRUE;
2524c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   }
2525c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul
25265ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell   return GL_FALSE;
25275ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell}
25285ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell
25299dfd54fa83f9d2266ee637721365f8922deb17fcBrian Paul
25309dfd54fa83f9d2266ee637721365f8922deb17fcBrian Paul/**
25319dfd54fa83f9d2266ee637721365f8922deb17fcBrian Paul * Second part of error checking for glCopyTexSubImage[12]D().
25329dfd54fa83f9d2266ee637721365f8922deb17fcBrian Paul * \param xoffset sub-image x offset given by the user.
25339dfd54fa83f9d2266ee637721365f8922deb17fcBrian Paul * \param yoffset sub-image y offset given by the user.
25349dfd54fa83f9d2266ee637721365f8922deb17fcBrian Paul * \param zoffset sub-image z offset given by the user.
25359dfd54fa83f9d2266ee637721365f8922deb17fcBrian Paul * \param width image width given by the user.
25369dfd54fa83f9d2266ee637721365f8922deb17fcBrian Paul * \param height image height given by the user.
25379dfd54fa83f9d2266ee637721365f8922deb17fcBrian Paul */
25385ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwellstatic GLboolean
2539f9995b30756140724f41daf963fa06167912be7fKristian Høgsbergcopytexsubimage_error_check2( struct gl_context *ctx, GLuint dimensions,
25405ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell			      GLenum target, GLint level,
25415ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell			      GLint xoffset, GLint yoffset, GLint zoffset,
25425ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell			      GLsizei width, GLsizei height,
25435ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell			      const struct gl_texture_image *teximage )
25445ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell{
25459dfd54fa83f9d2266ee637721365f8922deb17fcBrian Paul   /* check that dest tex image exists */
2546c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   if (!teximage) {
25474e9676fb13f60ecdbc247b120031f18cd3febcb0Brian Paul      _mesa_error(ctx, GL_INVALID_OPERATION,
2548b9f75444e60477e7a523e7eec1bb49a8232fd4e7Brian Paul                  "glCopyTexSubImage%dD(undefined texture level: %d)",
2549b9f75444e60477e7a523e7eec1bb49a8232fd4e7Brian Paul                  dimensions, level);
2550c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      return GL_TRUE;
2551c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   }
2552c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul
25539dfd54fa83f9d2266ee637721365f8922deb17fcBrian Paul   /* Check size */
25549dfd54fa83f9d2266ee637721365f8922deb17fcBrian Paul   if (width < 0) {
25559dfd54fa83f9d2266ee637721365f8922deb17fcBrian Paul      _mesa_error(ctx, GL_INVALID_VALUE,
25569dfd54fa83f9d2266ee637721365f8922deb17fcBrian Paul                  "glCopyTexSubImage%dD(width=%d)", dimensions, width);
25579dfd54fa83f9d2266ee637721365f8922deb17fcBrian Paul      return GL_TRUE;
25589dfd54fa83f9d2266ee637721365f8922deb17fcBrian Paul   }
25599dfd54fa83f9d2266ee637721365f8922deb17fcBrian Paul   if (dimensions > 1 && height < 0) {
25609dfd54fa83f9d2266ee637721365f8922deb17fcBrian Paul      _mesa_error(ctx, GL_INVALID_VALUE,
25619dfd54fa83f9d2266ee637721365f8922deb17fcBrian Paul                  "glCopyTexSubImage%dD(height=%d)", dimensions, height);
25629dfd54fa83f9d2266ee637721365f8922deb17fcBrian Paul      return GL_TRUE;
25639dfd54fa83f9d2266ee637721365f8922deb17fcBrian Paul   }
25649dfd54fa83f9d2266ee637721365f8922deb17fcBrian Paul
25659dfd54fa83f9d2266ee637721365f8922deb17fcBrian Paul   /* check x/y offsets */
2566c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   if (xoffset < -((GLint)teximage->Border)) {
25674e9676fb13f60ecdbc247b120031f18cd3febcb0Brian Paul      _mesa_error(ctx, GL_INVALID_VALUE,
25684e9676fb13f60ecdbc247b120031f18cd3febcb0Brian Paul                  "glCopyTexSubImage%dD(xoffset=%d)", dimensions, xoffset);
2569c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      return GL_TRUE;
2570c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   }
257138f28665bf9fb5b2464738ca5074848ec2777ae1Gareth Hughes   if (xoffset + width > (GLint) (teximage->Width + teximage->Border)) {
25724e9676fb13f60ecdbc247b120031f18cd3febcb0Brian Paul      _mesa_error(ctx, GL_INVALID_VALUE,
25734e9676fb13f60ecdbc247b120031f18cd3febcb0Brian Paul                  "glCopyTexSubImage%dD(xoffset+width)", dimensions);
2574c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      return GL_TRUE;
2575c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   }
2576c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   if (dimensions > 1) {
25776eba268ec65523fe8b2ec7dee670b2b5d4ebe1d9Brian Paul      GLint yBorder = (target == GL_TEXTURE_1D_ARRAY) ? 0 : teximage->Border;
25786eba268ec65523fe8b2ec7dee670b2b5d4ebe1d9Brian Paul      if (yoffset < -yBorder) {
25794e9676fb13f60ecdbc247b120031f18cd3febcb0Brian Paul         _mesa_error(ctx, GL_INVALID_VALUE,
25804e9676fb13f60ecdbc247b120031f18cd3febcb0Brian Paul                     "glCopyTexSubImage%dD(yoffset=%d)", dimensions, yoffset);
2581c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         return GL_TRUE;
2582c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      }
2583c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      /* NOTE: we're adding the border here, not subtracting! */
25846eba268ec65523fe8b2ec7dee670b2b5d4ebe1d9Brian Paul      if (yoffset + height > (GLint) teximage->Height + yBorder) {
25854e9676fb13f60ecdbc247b120031f18cd3febcb0Brian Paul         _mesa_error(ctx, GL_INVALID_VALUE,
25864e9676fb13f60ecdbc247b120031f18cd3febcb0Brian Paul                     "glCopyTexSubImage%dD(yoffset+height)", dimensions);
2587c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         return GL_TRUE;
2588c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      }
2589c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   }
2590c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul
25919dfd54fa83f9d2266ee637721365f8922deb17fcBrian Paul   /* check z offset */
2592c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   if (dimensions > 2) {
25936eba268ec65523fe8b2ec7dee670b2b5d4ebe1d9Brian Paul      GLint zBorder = (target == GL_TEXTURE_2D_ARRAY) ? 0 : teximage->Border;
25946eba268ec65523fe8b2ec7dee670b2b5d4ebe1d9Brian Paul      if (zoffset < -zBorder) {
25954e9676fb13f60ecdbc247b120031f18cd3febcb0Brian Paul         _mesa_error(ctx, GL_INVALID_VALUE,
25964e9676fb13f60ecdbc247b120031f18cd3febcb0Brian Paul                     "glCopyTexSubImage%dD(zoffset)", dimensions);
2597c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         return GL_TRUE;
2598c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      }
25996eba268ec65523fe8b2ec7dee670b2b5d4ebe1d9Brian Paul      if (zoffset > (GLint) teximage->Depth + zBorder) {
26004e9676fb13f60ecdbc247b120031f18cd3febcb0Brian Paul         _mesa_error(ctx, GL_INVALID_VALUE,
26014e9676fb13f60ecdbc247b120031f18cd3febcb0Brian Paul                     "glCopyTexSubImage%dD(zoffset+depth)", dimensions);
2602c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         return GL_TRUE;
2603c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      }
2604c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   }
2605c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul
2606b6bdafdf2cf1110b4a5ca7cf9e1c3dcb124b800fBrian Paul   if (_mesa_is_format_compressed(teximage->TexFormat)) {
2607d4a38e86d4b4d66cca20ee63222f940cb73fa709Chia-I Wu      if (compressedteximage_only_format(ctx, teximage->InternalFormat)) {
2608d4a38e86d4b4d66cca20ee63222f940cb73fa709Chia-I Wu         _mesa_error(ctx, GL_INVALID_OPERATION,
2609d4a38e86d4b4d66cca20ee63222f940cb73fa709Chia-I Wu               "glCopyTexSubImage%dD(no compression for format)", dimensions);
2610d4a38e86d4b4d66cca20ee63222f940cb73fa709Chia-I Wu         return GL_TRUE;
2611d4a38e86d4b4d66cca20ee63222f940cb73fa709Chia-I Wu      }
261289fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      /* offset must be multiple of 4 */
261389fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      if ((xoffset & 3) || (yoffset & 3)) {
261489fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         _mesa_error(ctx, GL_INVALID_VALUE,
26158978e45e50118d0e3660fdd9cd58b8790c0a6bb8Vinson Lee                     "glCopyTexSubImage%dD(xoffset or yoffset)", dimensions);
2616e4276667dafc8de0c6e64af8300fc7598437de6eBrian Paul         return GL_TRUE;
2617e4276667dafc8de0c6e64af8300fc7598437de6eBrian Paul      }
261889fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      /* size must be multiple of 4 */
2619798d83cf67098a7df293c711e0d2c8c296fc4c8fKarl Schultz      if ((width & 3) != 0 && (GLuint) width != teximage->Width) {
262089fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         _mesa_error(ctx, GL_INVALID_VALUE,
26218978e45e50118d0e3660fdd9cd58b8790c0a6bb8Vinson Lee                     "glCopyTexSubImage%dD(width)", dimensions);
2622e4276667dafc8de0c6e64af8300fc7598437de6eBrian Paul         return GL_TRUE;
262389fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      }
2624798d83cf67098a7df293c711e0d2c8c296fc4c8fKarl Schultz      if ((height & 3) != 0 && (GLuint) height != teximage->Height) {
262589fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         _mesa_error(ctx, GL_INVALID_VALUE,
26268978e45e50118d0e3660fdd9cd58b8790c0a6bb8Vinson Lee                     "glCopyTexSubImage%dD(height)", dimensions);
262789fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         return GL_TRUE;
262889fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      }
2629e4276667dafc8de0c6e64af8300fc7598437de6eBrian Paul   }
2630e4276667dafc8de0c6e64af8300fc7598437de6eBrian Paul
2631a9fc8ba756dd25a07dc19058fe60f65bda82a055Brian Paul   if (teximage->InternalFormat == GL_YCBCR_MESA) {
2632d8419c730e73c3be2eadfb0bee176ab06885766aBrian Paul      _mesa_error(ctx, GL_INVALID_OPERATION, "glCopyTexSubImage2D");
2633d8419c730e73c3be2eadfb0bee176ab06885766aBrian Paul      return GL_TRUE;
2634d8419c730e73c3be2eadfb0bee176ab06885766aBrian Paul   }
2635d8419c730e73c3be2eadfb0bee176ab06885766aBrian Paul
2636985a2eff82111e81f70b6dea9d9b3184dd0061d3Alan Hourihane   if (!_mesa_source_buffer_exists(ctx, teximage->_BaseFormat)) {
2637985a2eff82111e81f70b6dea9d9b3184dd0061d3Alan Hourihane      _mesa_error(ctx, GL_INVALID_OPERATION,
2638803504e69fbd85713fc6d93af21f5245852cecabBrian Paul                  "glCopyTexSubImage%dD(missing readbuffer, format=0x%x)",
2639803504e69fbd85713fc6d93af21f5245852cecabBrian Paul                  dimensions, teximage->_BaseFormat);
2640985a2eff82111e81f70b6dea9d9b3184dd0061d3Alan Hourihane      return GL_TRUE;
2641985a2eff82111e81f70b6dea9d9b3184dd0061d3Alan Hourihane   }
2642985a2eff82111e81f70b6dea9d9b3184dd0061d3Alan Hourihane
2643422b18794eacc8f0be5b4e9611e2726f4e0d86b6Eric Anholt   /* From the EXT_texture_integer spec:
2644422b18794eacc8f0be5b4e9611e2726f4e0d86b6Eric Anholt    *
2645422b18794eacc8f0be5b4e9611e2726f4e0d86b6Eric Anholt    *     "INVALID_OPERATION is generated by CopyTexImage* and CopyTexSubImage*
2646422b18794eacc8f0be5b4e9611e2726f4e0d86b6Eric Anholt    *      if the texture internalformat is an integer format and the read color
2647422b18794eacc8f0be5b4e9611e2726f4e0d86b6Eric Anholt    *      buffer is not an integer format, or if the internalformat is not an
2648422b18794eacc8f0be5b4e9611e2726f4e0d86b6Eric Anholt    *      integer format and the read color buffer is an integer format."
2649326b981d3fafbf0cc253d2b224f0c9ad307038a3Brian Paul    */
2650422b18794eacc8f0be5b4e9611e2726f4e0d86b6Eric Anholt   if (_mesa_is_color_format(teximage->InternalFormat)) {
2651326b981d3fafbf0cc253d2b224f0c9ad307038a3Brian Paul      struct gl_renderbuffer *rb = ctx->ReadBuffer->_ColorReadBuffer;
2652422b18794eacc8f0be5b4e9611e2726f4e0d86b6Eric Anholt
2653422b18794eacc8f0be5b4e9611e2726f4e0d86b6Eric Anholt      if (_mesa_is_format_integer_color(rb->Format) !=
2654422b18794eacc8f0be5b4e9611e2726f4e0d86b6Eric Anholt	  _mesa_is_format_integer_color(teximage->TexFormat)) {
2655422b18794eacc8f0be5b4e9611e2726f4e0d86b6Eric Anholt	 _mesa_error(ctx, GL_INVALID_OPERATION,
2656422b18794eacc8f0be5b4e9611e2726f4e0d86b6Eric Anholt		     "glCopyTexImage%dD(integer vs non-integer)", dimensions);
2657422b18794eacc8f0be5b4e9611e2726f4e0d86b6Eric Anholt	 return GL_TRUE;
2658326b981d3fafbf0cc253d2b224f0c9ad307038a3Brian Paul      }
2659326b981d3fafbf0cc253d2b224f0c9ad307038a3Brian Paul   }
2660326b981d3fafbf0cc253d2b224f0c9ad307038a3Brian Paul
2661c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   /* if we get here, the parameters are OK */
2662c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   return GL_FALSE;
2663c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul}
2664c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul
2665c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul
26662897cee99fb877e1f3cd9a881a61418c9c31867fBrian Paul/** Callback info for walking over FBO hash table */
26672897cee99fb877e1f3cd9a881a61418c9c31867fBrian Paulstruct cb_info
26682897cee99fb877e1f3cd9a881a61418c9c31867fBrian Paul{
2669f9995b30756140724f41daf963fa06167912be7fKristian Høgsberg   struct gl_context *ctx;
26702897cee99fb877e1f3cd9a881a61418c9c31867fBrian Paul   struct gl_texture_object *texObj;
26712897cee99fb877e1f3cd9a881a61418c9c31867fBrian Paul   GLuint level, face;
26722897cee99fb877e1f3cd9a881a61418c9c31867fBrian Paul};
26732897cee99fb877e1f3cd9a881a61418c9c31867fBrian Paul
26748e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul
2675519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul/**
26762897cee99fb877e1f3cd9a881a61418c9c31867fBrian Paul * Check render to texture callback.  Called from _mesa_HashWalk().
2677519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul */
2678519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paulstatic void
26792897cee99fb877e1f3cd9a881a61418c9c31867fBrian Paulcheck_rtt_cb(GLuint key, void *data, void *userData)
2680519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul{
26812897cee99fb877e1f3cd9a881a61418c9c31867fBrian Paul   struct gl_framebuffer *fb = (struct gl_framebuffer *) data;
26822897cee99fb877e1f3cd9a881a61418c9c31867fBrian Paul   const struct cb_info *info = (struct cb_info *) userData;
2683f9995b30756140724f41daf963fa06167912be7fKristian Høgsberg   struct gl_context *ctx = info->ctx;
26842897cee99fb877e1f3cd9a881a61418c9c31867fBrian Paul   const struct gl_texture_object *texObj = info->texObj;
26852897cee99fb877e1f3cd9a881a61418c9c31867fBrian Paul   const GLuint level = info->level, face = info->face;
26862897cee99fb877e1f3cd9a881a61418c9c31867fBrian Paul
26872897cee99fb877e1f3cd9a881a61418c9c31867fBrian Paul   /* If this is a user-created FBO */
268836ede89687fe2de213f2637ab7acfb80cfd856cdBrian Paul   if (_mesa_is_user_fbo(fb)) {
2689519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul      GLuint i;
26902897cee99fb877e1f3cd9a881a61418c9c31867fBrian Paul      /* check if any of the FBO's attachments point to 'texObj' */
2691519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul      for (i = 0; i < BUFFER_COUNT; i++) {
26922897cee99fb877e1f3cd9a881a61418c9c31867fBrian Paul         struct gl_renderbuffer_attachment *att = fb->Attachment + i;
2693519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul         if (att->Type == GL_TEXTURE &&
2694519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul             att->Texture == texObj &&
2695519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul             att->TextureLevel == level &&
2696519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul             att->CubeMapFace == face) {
2697db0f9e701d59dcfcd3b50f4d245897692f27fec9Brian Paul            ASSERT(_mesa_get_attachment_teximage(att));
2698519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul            /* Tell driver about the new renderbuffer texture */
2699ea4fe661d7f3a95d9db17e1475076f1badf8e1a6Brian Paul            ctx->Driver.RenderTexture(ctx, ctx->DrawBuffer, att);
27002897cee99fb877e1f3cd9a881a61418c9c31867fBrian Paul            /* Mark fb status as indeterminate to force re-validation */
27012897cee99fb877e1f3cd9a881a61418c9c31867fBrian Paul            fb->_Status = 0;
2702519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul         }
2703519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul      }
2704519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul   }
2705519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul}
2706519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul
2707519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul
27082897cee99fb877e1f3cd9a881a61418c9c31867fBrian Paul/**
27092897cee99fb877e1f3cd9a881a61418c9c31867fBrian Paul * When a texture image is specified we have to check if it's bound to
27102897cee99fb877e1f3cd9a881a61418c9c31867fBrian Paul * any framebuffer objects (render to texture) in order to detect changes
27112897cee99fb877e1f3cd9a881a61418c9c31867fBrian Paul * in size or format since that effects FBO completeness.
27122897cee99fb877e1f3cd9a881a61418c9c31867fBrian Paul * Any FBOs rendering into the texture must be re-validated.
27132897cee99fb877e1f3cd9a881a61418c9c31867fBrian Paul */
2714d842a118b26a56c9c5e4310f30f70199c42ea90eBrian Paulvoid
2715d842a118b26a56c9c5e4310f30f70199c42ea90eBrian Paul_mesa_update_fbo_texture(struct gl_context *ctx,
2716d842a118b26a56c9c5e4310f30f70199c42ea90eBrian Paul                         struct gl_texture_object *texObj,
2717d842a118b26a56c9c5e4310f30f70199c42ea90eBrian Paul                         GLuint face, GLuint level)
27182897cee99fb877e1f3cd9a881a61418c9c31867fBrian Paul{
27192897cee99fb877e1f3cd9a881a61418c9c31867fBrian Paul   /* Only check this texture if it's been marked as RenderToTexture */
27202897cee99fb877e1f3cd9a881a61418c9c31867fBrian Paul   if (texObj->_RenderToTexture) {
27212897cee99fb877e1f3cd9a881a61418c9c31867fBrian Paul      struct cb_info info;
27222897cee99fb877e1f3cd9a881a61418c9c31867fBrian Paul      info.ctx = ctx;
27232897cee99fb877e1f3cd9a881a61418c9c31867fBrian Paul      info.texObj = texObj;
27242897cee99fb877e1f3cd9a881a61418c9c31867fBrian Paul      info.level = level;
27252897cee99fb877e1f3cd9a881a61418c9c31867fBrian Paul      info.face = face;
27262897cee99fb877e1f3cd9a881a61418c9c31867fBrian Paul      _mesa_HashWalk(ctx->Shared->FrameBuffers, check_rtt_cb, &info);
27272897cee99fb877e1f3cd9a881a61418c9c31867fBrian Paul   }
27282897cee99fb877e1f3cd9a881a61418c9c31867fBrian Paul}
27292897cee99fb877e1f3cd9a881a61418c9c31867fBrian Paul
27302897cee99fb877e1f3cd9a881a61418c9c31867fBrian Paul
27312914f2b7fd9bd1082f8cce724d234208b900c3d4Brian Paul/**
27322914f2b7fd9bd1082f8cce724d234208b900c3d4Brian Paul * If the texture object's GenerateMipmap flag is set and we've
27332914f2b7fd9bd1082f8cce724d234208b900c3d4Brian Paul * changed the texture base level image, regenerate the rest of the
27342914f2b7fd9bd1082f8cce724d234208b900c3d4Brian Paul * mipmap levels now.
27352914f2b7fd9bd1082f8cce724d234208b900c3d4Brian Paul */
27369520f483b8f1e45fa474674b415554988de5d8d3Brian Paulstatic inline void
2737f9995b30756140724f41daf963fa06167912be7fKristian Høgsbergcheck_gen_mipmap(struct gl_context *ctx, GLenum target,
27382914f2b7fd9bd1082f8cce724d234208b900c3d4Brian Paul                 struct gl_texture_object *texObj, GLint level)
27392914f2b7fd9bd1082f8cce724d234208b900c3d4Brian Paul{
27402914f2b7fd9bd1082f8cce724d234208b900c3d4Brian Paul   ASSERT(target != GL_TEXTURE_CUBE_MAP);
2741ecb03d75a2961b28ab3d90fdd5df768608fc9447Brian Paul   if (texObj->GenerateMipmap &&
2742ecb03d75a2961b28ab3d90fdd5df768608fc9447Brian Paul       level == texObj->BaseLevel &&
2743ecb03d75a2961b28ab3d90fdd5df768608fc9447Brian Paul       level < texObj->MaxLevel) {
27442914f2b7fd9bd1082f8cce724d234208b900c3d4Brian Paul      ASSERT(ctx->Driver.GenerateMipmap);
27452914f2b7fd9bd1082f8cce724d234208b900c3d4Brian Paul      ctx->Driver.GenerateMipmap(ctx, target, texObj);
27462914f2b7fd9bd1082f8cce724d234208b900c3d4Brian Paul   }
27472914f2b7fd9bd1082f8cce724d234208b900c3d4Brian Paul}
27482914f2b7fd9bd1082f8cce724d234208b900c3d4Brian Paul
27492914f2b7fd9bd1082f8cce724d234208b900c3d4Brian Paul
27501826ff3fb34e9fb19bc8d8dee2cce5280da5e069Brian Paul/** Debug helper: override the user-requested internal format */
27511826ff3fb34e9fb19bc8d8dee2cce5280da5e069Brian Paulstatic GLenum
27521826ff3fb34e9fb19bc8d8dee2cce5280da5e069Brian Pauloverride_internal_format(GLenum internalFormat, GLint width, GLint height)
27531826ff3fb34e9fb19bc8d8dee2cce5280da5e069Brian Paul{
27541826ff3fb34e9fb19bc8d8dee2cce5280da5e069Brian Paul#if 0
27551826ff3fb34e9fb19bc8d8dee2cce5280da5e069Brian Paul   if (internalFormat == GL_RGBA16F_ARB ||
27561826ff3fb34e9fb19bc8d8dee2cce5280da5e069Brian Paul       internalFormat == GL_RGBA32F_ARB) {
27571826ff3fb34e9fb19bc8d8dee2cce5280da5e069Brian Paul      printf("Convert rgba float tex to int %d x %d\n", width, height);
27581826ff3fb34e9fb19bc8d8dee2cce5280da5e069Brian Paul      return GL_RGBA;
27591826ff3fb34e9fb19bc8d8dee2cce5280da5e069Brian Paul   }
27601826ff3fb34e9fb19bc8d8dee2cce5280da5e069Brian Paul   else if (internalFormat == GL_RGB16F_ARB ||
27611826ff3fb34e9fb19bc8d8dee2cce5280da5e069Brian Paul            internalFormat == GL_RGB32F_ARB) {
27621826ff3fb34e9fb19bc8d8dee2cce5280da5e069Brian Paul      printf("Convert rgb float tex to int %d x %d\n", width, height);
27631826ff3fb34e9fb19bc8d8dee2cce5280da5e069Brian Paul      return GL_RGB;
27641826ff3fb34e9fb19bc8d8dee2cce5280da5e069Brian Paul   }
27651826ff3fb34e9fb19bc8d8dee2cce5280da5e069Brian Paul   else if (internalFormat == GL_LUMINANCE_ALPHA16F_ARB ||
27661826ff3fb34e9fb19bc8d8dee2cce5280da5e069Brian Paul            internalFormat == GL_LUMINANCE_ALPHA32F_ARB) {
27671826ff3fb34e9fb19bc8d8dee2cce5280da5e069Brian Paul      printf("Convert luminance float tex to int %d x %d\n", width, height);
27681826ff3fb34e9fb19bc8d8dee2cce5280da5e069Brian Paul      return GL_LUMINANCE_ALPHA;
27691826ff3fb34e9fb19bc8d8dee2cce5280da5e069Brian Paul   }
27701826ff3fb34e9fb19bc8d8dee2cce5280da5e069Brian Paul   else if (internalFormat == GL_LUMINANCE16F_ARB ||
27711826ff3fb34e9fb19bc8d8dee2cce5280da5e069Brian Paul            internalFormat == GL_LUMINANCE32F_ARB) {
27721826ff3fb34e9fb19bc8d8dee2cce5280da5e069Brian Paul      printf("Convert luminance float tex to int %d x %d\n", width, height);
27731826ff3fb34e9fb19bc8d8dee2cce5280da5e069Brian Paul      return GL_LUMINANCE;
27741826ff3fb34e9fb19bc8d8dee2cce5280da5e069Brian Paul   }
27751826ff3fb34e9fb19bc8d8dee2cce5280da5e069Brian Paul   else if (internalFormat == GL_ALPHA16F_ARB ||
27761826ff3fb34e9fb19bc8d8dee2cce5280da5e069Brian Paul            internalFormat == GL_ALPHA32F_ARB) {
27771826ff3fb34e9fb19bc8d8dee2cce5280da5e069Brian Paul      printf("Convert luminance float tex to int %d x %d\n", width, height);
27781826ff3fb34e9fb19bc8d8dee2cce5280da5e069Brian Paul      return GL_ALPHA;
27791826ff3fb34e9fb19bc8d8dee2cce5280da5e069Brian Paul   }
27801826ff3fb34e9fb19bc8d8dee2cce5280da5e069Brian Paul   /*
27811826ff3fb34e9fb19bc8d8dee2cce5280da5e069Brian Paul   else if (internalFormat == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT) {
27821826ff3fb34e9fb19bc8d8dee2cce5280da5e069Brian Paul      internalFormat = GL_RGBA;
27831826ff3fb34e9fb19bc8d8dee2cce5280da5e069Brian Paul   }
27841826ff3fb34e9fb19bc8d8dee2cce5280da5e069Brian Paul   */
27851826ff3fb34e9fb19bc8d8dee2cce5280da5e069Brian Paul   else {
27861826ff3fb34e9fb19bc8d8dee2cce5280da5e069Brian Paul      return internalFormat;
27871826ff3fb34e9fb19bc8d8dee2cce5280da5e069Brian Paul   }
27881826ff3fb34e9fb19bc8d8dee2cce5280da5e069Brian Paul#else
27891826ff3fb34e9fb19bc8d8dee2cce5280da5e069Brian Paul   return internalFormat;
27901826ff3fb34e9fb19bc8d8dee2cce5280da5e069Brian Paul#endif
27911826ff3fb34e9fb19bc8d8dee2cce5280da5e069Brian Paul}
27921826ff3fb34e9fb19bc8d8dee2cce5280da5e069Brian Paul
2793519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul
2794bab484a59b21fff84579a492d079d46e27d486ddBrian Paul/**
2795bab484a59b21fff84579a492d079d46e27d486ddBrian Paul * Choose the actual hardware format for a texture image.
2796bab484a59b21fff84579a492d079d46e27d486ddBrian Paul * Try to use the same format as the previous image level when possible.
2797bab484a59b21fff84579a492d079d46e27d486ddBrian Paul * Otherwise, ask the driver for the best format.
2798bab484a59b21fff84579a492d079d46e27d486ddBrian Paul * It's important to try to choose a consistant format for all levels
2799bab484a59b21fff84579a492d079d46e27d486ddBrian Paul * for efficient texture memory layout/allocation.  In particular, this
2800bab484a59b21fff84579a492d079d46e27d486ddBrian Paul * comes up during automatic mipmap generation.
2801bab484a59b21fff84579a492d079d46e27d486ddBrian Paul */
280283e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paulgl_format
2803f9995b30756140724f41daf963fa06167912be7fKristian Høgsberg_mesa_choose_texture_format(struct gl_context *ctx,
2804bab484a59b21fff84579a492d079d46e27d486ddBrian Paul                            struct gl_texture_object *texObj,
2805bab484a59b21fff84579a492d079d46e27d486ddBrian Paul                            GLenum target, GLint level,
2806bab484a59b21fff84579a492d079d46e27d486ddBrian Paul                            GLenum internalFormat, GLenum format, GLenum type)
2807bab484a59b21fff84579a492d079d46e27d486ddBrian Paul{
280883e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul   gl_format f;
280983e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul
2810bab484a59b21fff84579a492d079d46e27d486ddBrian Paul   /* see if we've already chosen a format for the previous level */
2811bab484a59b21fff84579a492d079d46e27d486ddBrian Paul   if (level > 0) {
2812bab484a59b21fff84579a492d079d46e27d486ddBrian Paul      struct gl_texture_image *prevImage =
2813bab484a59b21fff84579a492d079d46e27d486ddBrian Paul	 _mesa_select_tex_image(ctx, texObj, target, level - 1);
2814bab484a59b21fff84579a492d079d46e27d486ddBrian Paul      /* See if the prev level is defined and has an internal format which
2815bab484a59b21fff84579a492d079d46e27d486ddBrian Paul       * matches the new internal format.
2816bab484a59b21fff84579a492d079d46e27d486ddBrian Paul       */
2817bab484a59b21fff84579a492d079d46e27d486ddBrian Paul      if (prevImage &&
2818bab484a59b21fff84579a492d079d46e27d486ddBrian Paul          prevImage->Width > 0 &&
2819bab484a59b21fff84579a492d079d46e27d486ddBrian Paul          prevImage->InternalFormat == internalFormat) {
2820bab484a59b21fff84579a492d079d46e27d486ddBrian Paul         /* use the same format */
282183e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul         ASSERT(prevImage->TexFormat != MESA_FORMAT_NONE);
282283e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul         return prevImage->TexFormat;
2823bab484a59b21fff84579a492d079d46e27d486ddBrian Paul      }
2824bab484a59b21fff84579a492d079d46e27d486ddBrian Paul   }
2825bab484a59b21fff84579a492d079d46e27d486ddBrian Paul
282642723d88d370a7599398cc1c2349aeb951ba1c57Ian Romanick   /* If the application requested compression to an S3TC format but we don't
282742723d88d370a7599398cc1c2349aeb951ba1c57Ian Romanick    * have the DTXn library, force a generic compressed format instead.
282842723d88d370a7599398cc1c2349aeb951ba1c57Ian Romanick    */
28293dd84a58bb3a066eeaf854dd83a8d33801011367Kenneth Graunke   if (internalFormat != format && format != GL_NONE) {
283042723d88d370a7599398cc1c2349aeb951ba1c57Ian Romanick      const GLenum before = internalFormat;
283142723d88d370a7599398cc1c2349aeb951ba1c57Ian Romanick
283242723d88d370a7599398cc1c2349aeb951ba1c57Ian Romanick      switch (internalFormat) {
283342723d88d370a7599398cc1c2349aeb951ba1c57Ian Romanick      case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
283442723d88d370a7599398cc1c2349aeb951ba1c57Ian Romanick         if (!ctx->Mesa_DXTn)
283542723d88d370a7599398cc1c2349aeb951ba1c57Ian Romanick            internalFormat = GL_COMPRESSED_RGB;
283642723d88d370a7599398cc1c2349aeb951ba1c57Ian Romanick         break;
283742723d88d370a7599398cc1c2349aeb951ba1c57Ian Romanick      case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
283842723d88d370a7599398cc1c2349aeb951ba1c57Ian Romanick      case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
283942723d88d370a7599398cc1c2349aeb951ba1c57Ian Romanick      case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
284042723d88d370a7599398cc1c2349aeb951ba1c57Ian Romanick         if (!ctx->Mesa_DXTn)
284142723d88d370a7599398cc1c2349aeb951ba1c57Ian Romanick            internalFormat = GL_COMPRESSED_RGBA;
284242723d88d370a7599398cc1c2349aeb951ba1c57Ian Romanick         break;
284342723d88d370a7599398cc1c2349aeb951ba1c57Ian Romanick      default:
284442723d88d370a7599398cc1c2349aeb951ba1c57Ian Romanick         break;
284542723d88d370a7599398cc1c2349aeb951ba1c57Ian Romanick      }
284642723d88d370a7599398cc1c2349aeb951ba1c57Ian Romanick
284742723d88d370a7599398cc1c2349aeb951ba1c57Ian Romanick      if (before != internalFormat) {
284842723d88d370a7599398cc1c2349aeb951ba1c57Ian Romanick         _mesa_warning(ctx,
284942723d88d370a7599398cc1c2349aeb951ba1c57Ian Romanick                       "DXT compression requested (%s), "
285042723d88d370a7599398cc1c2349aeb951ba1c57Ian Romanick                       "but libtxc_dxtn library not installed.  Using %s "
285142723d88d370a7599398cc1c2349aeb951ba1c57Ian Romanick                       "instead.",
285242723d88d370a7599398cc1c2349aeb951ba1c57Ian Romanick                       _mesa_lookup_enum_by_nr(before),
285342723d88d370a7599398cc1c2349aeb951ba1c57Ian Romanick                       _mesa_lookup_enum_by_nr(internalFormat));
285442723d88d370a7599398cc1c2349aeb951ba1c57Ian Romanick      }
285542723d88d370a7599398cc1c2349aeb951ba1c57Ian Romanick   }
285642723d88d370a7599398cc1c2349aeb951ba1c57Ian Romanick
2857bab484a59b21fff84579a492d079d46e27d486ddBrian Paul   /* choose format from scratch */
2858d47a6ada9ca9670c60fc141fabadf40c63031c08Brian Paul   f = ctx->Driver.ChooseTextureFormat(ctx, texObj->Target, internalFormat,
2859d47a6ada9ca9670c60fc141fabadf40c63031c08Brian Paul                                       format, type);
286083e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul   ASSERT(f != MESA_FORMAT_NONE);
286183e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul   return f;
2862bab484a59b21fff84579a492d079d46e27d486ddBrian Paul}
2863bab484a59b21fff84579a492d079d46e27d486ddBrian Paul
28649c4b02528752eb6392009e41025202fc9f9ca5b3Eric Anholt/**
28659c4b02528752eb6392009e41025202fc9f9ca5b3Eric Anholt * Adjust pixel unpack params and image dimensions to strip off the
28666c1dea54457c1d19b67794f188c40c74c23ba762Brian Paul * one-pixel texture border.
28679c4b02528752eb6392009e41025202fc9f9ca5b3Eric Anholt *
28689c4b02528752eb6392009e41025202fc9f9ca5b3Eric Anholt * Gallium and intel don't support texture borders.  They've seldem been used
28699c4b02528752eb6392009e41025202fc9f9ca5b3Eric Anholt * and seldom been implemented correctly anyway.
28709c4b02528752eb6392009e41025202fc9f9ca5b3Eric Anholt *
28719c4b02528752eb6392009e41025202fc9f9ca5b3Eric Anholt * \param unpackNew returns the new pixel unpack parameters
28729c4b02528752eb6392009e41025202fc9f9ca5b3Eric Anholt */
28739c4b02528752eb6392009e41025202fc9f9ca5b3Eric Anholtstatic void
28746c1dea54457c1d19b67794f188c40c74c23ba762Brian Paulstrip_texture_border(GLenum target,
28759c4b02528752eb6392009e41025202fc9f9ca5b3Eric Anholt                     GLint *width, GLint *height, GLint *depth,
28769c4b02528752eb6392009e41025202fc9f9ca5b3Eric Anholt                     const struct gl_pixelstore_attrib *unpack,
28779c4b02528752eb6392009e41025202fc9f9ca5b3Eric Anholt                     struct gl_pixelstore_attrib *unpackNew)
28789c4b02528752eb6392009e41025202fc9f9ca5b3Eric Anholt{
28796c1dea54457c1d19b67794f188c40c74c23ba762Brian Paul   assert(width);
28806c1dea54457c1d19b67794f188c40c74c23ba762Brian Paul   assert(height);
28816c1dea54457c1d19b67794f188c40c74c23ba762Brian Paul   assert(depth);
28829c4b02528752eb6392009e41025202fc9f9ca5b3Eric Anholt
28839c4b02528752eb6392009e41025202fc9f9ca5b3Eric Anholt   *unpackNew = *unpack;
28849c4b02528752eb6392009e41025202fc9f9ca5b3Eric Anholt
28859c4b02528752eb6392009e41025202fc9f9ca5b3Eric Anholt   if (unpackNew->RowLength == 0)
28869c4b02528752eb6392009e41025202fc9f9ca5b3Eric Anholt      unpackNew->RowLength = *width;
28879c4b02528752eb6392009e41025202fc9f9ca5b3Eric Anholt
28886c1dea54457c1d19b67794f188c40c74c23ba762Brian Paul   if (unpackNew->ImageHeight == 0)
28899c4b02528752eb6392009e41025202fc9f9ca5b3Eric Anholt      unpackNew->ImageHeight = *height;
28909c4b02528752eb6392009e41025202fc9f9ca5b3Eric Anholt
28919c4b02528752eb6392009e41025202fc9f9ca5b3Eric Anholt   assert(*width >= 3);
28926c1dea54457c1d19b67794f188c40c74c23ba762Brian Paul   unpackNew->SkipPixels++;  /* skip the border */
28936c1dea54457c1d19b67794f188c40c74c23ba762Brian Paul   *width = *width - 2;      /* reduce the width by two border pixels */
28946c1dea54457c1d19b67794f188c40c74c23ba762Brian Paul
28956c1dea54457c1d19b67794f188c40c74c23ba762Brian Paul   /* The min height of a texture with a border is 3 */
28966c1dea54457c1d19b67794f188c40c74c23ba762Brian Paul   if (*height >= 3 && target != GL_TEXTURE_1D_ARRAY) {
28976c1dea54457c1d19b67794f188c40c74c23ba762Brian Paul      unpackNew->SkipRows++;  /* skip the border */
28986c1dea54457c1d19b67794f188c40c74c23ba762Brian Paul      *height = *height - 2;  /* reduce the height by two border pixels */
28996c1dea54457c1d19b67794f188c40c74c23ba762Brian Paul   }
29006c1dea54457c1d19b67794f188c40c74c23ba762Brian Paul
29016c1dea54457c1d19b67794f188c40c74c23ba762Brian Paul   if (*depth >= 3 && target != GL_TEXTURE_2D_ARRAY) {
29026c1dea54457c1d19b67794f188c40c74c23ba762Brian Paul      unpackNew->SkipImages++;  /* skip the border */
29036c1dea54457c1d19b67794f188c40c74c23ba762Brian Paul      *depth = *depth - 2;      /* reduce the depth by two border pixels */
29046c1dea54457c1d19b67794f188c40c74c23ba762Brian Paul   }
29059c4b02528752eb6392009e41025202fc9f9ca5b3Eric Anholt}
2906bab484a59b21fff84579a492d079d46e27d486ddBrian Paul
2907bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paul
2908d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul/**
2909bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paul * Common code to implement all the glTexImage1D/2D/3D functions
2910bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paul * as well as glCompressedTexImage1D/2D/3D.
2911bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paul * \param compressed  only GL_TRUE for glCompressedTexImage1D/2D/3D calls.
2912bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paul * \param format  the user's image format (only used if !compressed)
2913bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paul * \param type  the user's image type (only used if !compressed)
2914bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paul * \param imageSize  only used for glCompressedTexImage1D/2D/3D calls.
2915c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul */
2916d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paulstatic void
2917bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paulteximage(struct gl_context *ctx, GLboolean compressed, GLuint dims,
2918d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul         GLenum target, GLint level, GLint internalFormat,
2919d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul         GLsizei width, GLsizei height, GLsizei depth,
2920d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul         GLint border, GLenum format, GLenum type,
2921bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paul         GLsizei imageSize, const GLvoid *pixels)
2922c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul{
2923bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paul   const char *func = compressed ? "glCompressedTexImage" : "glTexImage";
2924637a79aa2381e363d6b94c0bb5692a1882e9c945Brian Paul   GLenum error;
29259c4b02528752eb6392009e41025202fc9f9ca5b3Eric Anholt   struct gl_pixelstore_attrib unpack_no_border;
29269c4b02528752eb6392009e41025202fc9f9ca5b3Eric Anholt   const struct gl_pixelstore_attrib *unpack = &ctx->Unpack;
2927d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul
2928cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2929c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul
2930bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paul   if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE)) {
2931bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paul      if (compressed)
2932bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paul         _mesa_debug(ctx,
2933bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paul                     "glCompressedTexImage%uD %s %d %s %d %d %d %d %p\n",
2934bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paul                     dims,
2935bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paul                     _mesa_lookup_enum_by_nr(target), level,
2936bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paul                     _mesa_lookup_enum_by_nr(internalFormat),
2937bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paul                     width, height, depth, border, pixels);
2938bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paul      else
2939bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paul         _mesa_debug(ctx,
2940bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paul                     "glTexImage%uD %s %d %s %d %d %d %d %s %s %p\n",
2941bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paul                     dims,
2942bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paul                     _mesa_lookup_enum_by_nr(target), level,
2943bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paul                     _mesa_lookup_enum_by_nr(internalFormat),
2944bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paul                     width, height, depth, border,
2945bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paul                     _mesa_lookup_enum_by_nr(format),
2946bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paul                     _mesa_lookup_enum_by_nr(type), pixels);
2947bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paul   }
2948d100cbf721010f4eacc87507cc87c5314150d493Maciej Cencora
2949d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul   internalFormat = override_internal_format(internalFormat, width, height);
295011caea687e3f10ae12d33e44edf84635f73047ddBrian Paul
2951d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul   /* target error checking */
2952d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul   if (!legal_teximage_target(ctx, dims, target)) {
2953bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paul      _mesa_error(ctx, GL_INVALID_ENUM, "%s%uD(target=%s)",
2954bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paul                  func, dims, _mesa_lookup_enum_by_nr(target));
2955d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul      return;
2956d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul   }
2957ecf6f37fc201d7c88f8214b04ca1f61071c460a1Brian Paul
2958d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul   /* general error checking */
2959bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paul   if (compressed) {
2960bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paul      error = compressed_texture_error_check(ctx, dims, target, level,
2961bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paul                                             internalFormat,
2962bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paul                                             width, height, depth,
2963bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paul                                             border, imageSize);
2964bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paul   }
2965bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paul   else {
2966bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paul      error = texture_error_check(ctx, dims, target, level, internalFormat,
2967bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paul                                  format, type, width, height, depth, border);
2968bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paul   }
2969bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paul
2970bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paul#if FEATURE_ES
2971bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paul   /* Here we convert a cpal compressed image into a regular glTexImage2D
2972bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paul    * call by decompressing the texture.  If we really want to support cpal
2973bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paul    * textures in any driver this would have to be changed.
2974bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paul    */
2975bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paul   if (compressed && !error && dims == 2) {
2976bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paul      switch (internalFormat) {
2977bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paul      case GL_PALETTE4_RGB8_OES:
2978bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paul      case GL_PALETTE4_RGBA8_OES:
2979bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paul      case GL_PALETTE4_R5_G6_B5_OES:
2980bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paul      case GL_PALETTE4_RGBA4_OES:
2981bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paul      case GL_PALETTE4_RGB5_A1_OES:
2982bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paul      case GL_PALETTE8_RGB8_OES:
2983bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paul      case GL_PALETTE8_RGBA8_OES:
2984bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paul      case GL_PALETTE8_R5_G6_B5_OES:
2985bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paul      case GL_PALETTE8_RGBA4_OES:
2986bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paul      case GL_PALETTE8_RGB5_A1_OES:
2987bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paul         _mesa_cpal_compressed_teximage2d(target, level, internalFormat,
2988388af5b6f43fc81d7a9423a42358bcea65ab8ba9Brian Paul                                          width, height, imageSize, pixels);
2989bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paul         return;
2990bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paul      }
2991bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paul   }
2992bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paul#endif
29932914f2b7fd9bd1082f8cce724d234208b900c3d4Brian Paul
2994d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul   if (_mesa_is_proxy_texture(target)) {
2995d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul      /* Proxy texture: just clear or set state depending on error checking */
2996d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul      struct gl_texture_image *texImage =
29978a935d71ff4ded6f3cdd8e06dcd1a5e3de3d3cc0Brian Paul         get_proxy_tex_image(ctx, target, level);
2998e93cb4b34fed27eccf291963b5fe2c26cea46251Brian Paul      gl_format texFormat = MESA_FORMAT_NONE;
2999e93cb4b34fed27eccf291963b5fe2c26cea46251Brian Paul
3000e93cb4b34fed27eccf291963b5fe2c26cea46251Brian Paul      if (!error) {
3001e93cb4b34fed27eccf291963b5fe2c26cea46251Brian Paul         /* No parameter errors.  Choose a texture format and see if we
3002e93cb4b34fed27eccf291963b5fe2c26cea46251Brian Paul          * can really allocate the texture.
3003e93cb4b34fed27eccf291963b5fe2c26cea46251Brian Paul          */
3004e93cb4b34fed27eccf291963b5fe2c26cea46251Brian Paul         struct gl_texture_object *texObj =
3005e93cb4b34fed27eccf291963b5fe2c26cea46251Brian Paul            _mesa_get_current_tex_object(ctx, target);
3006e93cb4b34fed27eccf291963b5fe2c26cea46251Brian Paul         texFormat = _mesa_choose_texture_format(ctx, texObj, target, level,
3007e93cb4b34fed27eccf291963b5fe2c26cea46251Brian Paul                                                 internalFormat, format, type);
3008e93cb4b34fed27eccf291963b5fe2c26cea46251Brian Paul         if (!legal_texture_size(ctx, texFormat, width, height, depth)) {
3009e93cb4b34fed27eccf291963b5fe2c26cea46251Brian Paul            error = PROXY_ERROR;
3010e93cb4b34fed27eccf291963b5fe2c26cea46251Brian Paul         }
3011e93cb4b34fed27eccf291963b5fe2c26cea46251Brian Paul      }
3012e93cb4b34fed27eccf291963b5fe2c26cea46251Brian Paul
3013637a79aa2381e363d6b94c0bb5692a1882e9c945Brian Paul      if (error == PROXY_ERROR) {
3014637a79aa2381e363d6b94c0bb5692a1882e9c945Brian Paul         /* image too large, etc.  Clear all proxy texture image parameters. */
3015a3f137094cd965d27e1b088499dd609b81a91906Brian Paul         if (texImage)
3016a3f137094cd965d27e1b088499dd609b81a91906Brian Paul            clear_teximage_fields(texImage);
3017c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      }
3018637a79aa2381e363d6b94c0bb5692a1882e9c945Brian Paul      else if (error == GL_FALSE) {
3019e93cb4b34fed27eccf291963b5fe2c26cea46251Brian Paul         /* no error: store the teximage parameters */
3020e93cb4b34fed27eccf291963b5fe2c26cea46251Brian Paul         if (texImage)
3021e93cb4b34fed27eccf291963b5fe2c26cea46251Brian Paul            _mesa_init_teximage_fields(ctx, texImage, width, height, depth,
3022e93cb4b34fed27eccf291963b5fe2c26cea46251Brian Paul                                       border, internalFormat, texFormat);
3023e93cb4b34fed27eccf291963b5fe2c26cea46251Brian Paul      }
3024e93cb4b34fed27eccf291963b5fe2c26cea46251Brian Paul      else {
3025e93cb4b34fed27eccf291963b5fe2c26cea46251Brian Paul         /* other, regular error (was already recorded) */
302689fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      }
3027c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   }
3028c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   else {
30298e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul      /* non-proxy target */
3030d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul      const GLuint face = _mesa_tex_target_to_face(target);
30310293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      struct gl_texture_object *texObj;
30320293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      struct gl_texture_image *texImage;
3033f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul
3034d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul      if (error) {
30358e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul         return;   /* error was recorded */
3036c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      }
3037c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul
30389c4b02528752eb6392009e41025202fc9f9ca5b3Eric Anholt      /* Allow a hardware driver to just strip out the border, to provide
30399c4b02528752eb6392009e41025202fc9f9ca5b3Eric Anholt       * reliable but slightly incorrect hardware rendering instead of
30409c4b02528752eb6392009e41025202fc9f9ca5b3Eric Anholt       * rarely-tested software fallback rendering.
30419c4b02528752eb6392009e41025202fc9f9ca5b3Eric Anholt       */
30429c4b02528752eb6392009e41025202fc9f9ca5b3Eric Anholt      if (border && ctx->Const.StripTextureBorder) {
30436c1dea54457c1d19b67794f188c40c74c23ba762Brian Paul	 strip_texture_border(target, &width, &height, &depth, unpack,
30449c4b02528752eb6392009e41025202fc9f9ca5b3Eric Anholt			      &unpack_no_border);
30456c1dea54457c1d19b67794f188c40c74c23ba762Brian Paul         border = 0;
30469c4b02528752eb6392009e41025202fc9f9ca5b3Eric Anholt	 unpack = &unpack_no_border;
30479c4b02528752eb6392009e41025202fc9f9ca5b3Eric Anholt      }
30489c4b02528752eb6392009e41025202fc9f9ca5b3Eric Anholt
30497ecb61c30c4984a8513b306b640f9d274d8ecf61Brian Paul      if (ctx->NewState & _NEW_PIXEL)
30505ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell	 _mesa_update_state(ctx);
3051fa4525e289b475b928a7b2c4055af9dd7fe46600Brian Paul
3052b8fdb900fb9b1c8b1e9ec88509624237307a869aBrian Paul      texObj = _mesa_get_current_tex_object(ctx, target);
3053d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul
30545ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell      _mesa_lock_texture(ctx, texObj);
30555ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell      {
30565ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell	 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
3057d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul
30585ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell	 if (!texImage) {
3059bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paul	    _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s%uD", func, dims);
30605ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell	 }
3061ecf6f37fc201d7c88f8214b04ca1f61071c460a1Brian Paul         else {
306283e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul            gl_format texFormat;
306383e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul
306468f8cf72638e974a5bf2ae3e88c2e3539d88e2b6Eric Anholt            ctx->Driver.FreeTextureImageBuffer(ctx, texImage);
3065ecf6f37fc201d7c88f8214b04ca1f61071c460a1Brian Paul
306683e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul            texFormat = _mesa_choose_texture_format(ctx, texObj, target, level,
306783e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul                                                    internalFormat, format,
306883e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul                                                    type);
306983e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul
3070d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul            if (legal_texture_size(ctx, texFormat, width, height, depth)) {
3071892a2542a3f0753a7064c710b96f077dd5490624Brian Paul               _mesa_init_teximage_fields(ctx, texImage,
3072d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul                                          width, height, depth,
3073d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul                                          border, internalFormat, texFormat);
307411caea687e3f10ae12d33e44edf84635f73047ddBrian Paul
30754b08f35487fa439fd9ca4d653d3a146c3dc09c1cBrian Paul               /* Give the texture to the driver.  <pixels> may be null. */
307632faf7ab0de8b88bb15a2cb262a73c411dce9d0dBrian Paul               if (width > 0 && height > 0 && depth > 0) {
307732faf7ab0de8b88bb15a2cb262a73c411dce9d0dBrian Paul                  if (compressed) {
307832faf7ab0de8b88bb15a2cb262a73c411dce9d0dBrian Paul                     ctx->Driver.CompressedTexImage(ctx, dims, texImage,
307932faf7ab0de8b88bb15a2cb262a73c411dce9d0dBrian Paul                                                    imageSize, pixels);
308032faf7ab0de8b88bb15a2cb262a73c411dce9d0dBrian Paul                  }
308132faf7ab0de8b88bb15a2cb262a73c411dce9d0dBrian Paul                  else {
308232faf7ab0de8b88bb15a2cb262a73c411dce9d0dBrian Paul                     ctx->Driver.TexImage(ctx, dims, texImage, format,
308332faf7ab0de8b88bb15a2cb262a73c411dce9d0dBrian Paul                                          type, pixels, unpack);
308432faf7ab0de8b88bb15a2cb262a73c411dce9d0dBrian Paul                  }
3085bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paul               }
3086ecf6f37fc201d7c88f8214b04ca1f61071c460a1Brian Paul
30874b08f35487fa439fd9ca4d653d3a146c3dc09c1cBrian Paul               check_gen_mipmap(ctx, target, texObj, level);
30882914f2b7fd9bd1082f8cce724d234208b900c3d4Brian Paul
3089d842a118b26a56c9c5e4310f30f70199c42ea90eBrian Paul               _mesa_update_fbo_texture(ctx, texObj, face, level);
3090ecf6f37fc201d7c88f8214b04ca1f61071c460a1Brian Paul
3091878c69fe540a73011b676ead33ac8d9b8c9e63aaBrian Paul               _mesa_dirty_texobj(ctx, texObj, GL_TRUE);
30924b08f35487fa439fd9ca4d653d3a146c3dc09c1cBrian Paul            }
30934b08f35487fa439fd9ca4d653d3a146c3dc09c1cBrian Paul            else {
3094bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paul               _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s%uD", func, dims);
30954b08f35487fa439fd9ca4d653d3a146c3dc09c1cBrian Paul            }
3096ecf6f37fc201d7c88f8214b04ca1f61071c460a1Brian Paul         }
3097ecf6f37fc201d7c88f8214b04ca1f61071c460a1Brian Paul      }
30985ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell      _mesa_unlock_texture(ctx, texObj);
3099afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   }
3100d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul}
3101d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul
3102d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul
3103d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul/*
3104d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul * Called from the API.  Note that width includes the border.
3105d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul */
3106d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paulvoid GLAPIENTRY
3107d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul_mesa_TexImage1D( GLenum target, GLint level, GLint internalFormat,
3108d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul                  GLsizei width, GLint border, GLenum format,
3109d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul                  GLenum type, const GLvoid *pixels )
3110d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul{
3111d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul   GET_CURRENT_CONTEXT(ctx);
3112bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paul   teximage(ctx, GL_FALSE, 1, target, level, internalFormat, width, 1, 1,
3113bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paul            border, format, type, 0, pixels);
3114d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul}
3115d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul
3116d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul
3117d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paulvoid GLAPIENTRY
3118d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul_mesa_TexImage2D( GLenum target, GLint level, GLint internalFormat,
3119d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul                  GLsizei width, GLsizei height, GLint border,
3120d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul                  GLenum format, GLenum type,
3121d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul                  const GLvoid *pixels )
3122d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul{
3123d0b2b8da7d363f6b79fcab7bfe6e81d1d436944eBrian Paul   GET_CURRENT_CONTEXT(ctx);
3124bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paul   teximage(ctx, GL_FALSE, 2, target, level, internalFormat, width, height, 1,
3125bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paul            border, format, type, 0, pixels);
3126afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg}
3127afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
3128afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
3129afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg/*
3130afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg * Called by the API or display list executor.
3131afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg * Note that width and height include the border.
3132afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg */
3133c40d1dd62dd9bcbb97128e37a75d991a8d3b2d8cKendall Bennettvoid GLAPIENTRY
3134b5fd886ee12ef52126969f712dee0bd4b2eea706Brian Paul_mesa_TexImage3D( GLenum target, GLint level, GLint internalFormat,
3135fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul                  GLsizei width, GLsizei height, GLsizei depth,
3136fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul                  GLint border, GLenum format, GLenum type,
3137fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul                  const GLvoid *pixels )
3138afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg{
3139fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul   GET_CURRENT_CONTEXT(ctx);
3140bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paul   teximage(ctx, GL_FALSE, 3, target, level, internalFormat,
3141bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paul            width, height, depth,
3142bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paul            border, format, type, 0, pixels);
3143afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg}
3144afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
3145afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
3146c40d1dd62dd9bcbb97128e37a75d991a8d3b2d8cKendall Bennettvoid GLAPIENTRY
314743911c8ef06c44d486a17cf9a8fa8ea263b08adeBrian Paul_mesa_TexImage3DEXT( GLenum target, GLint level, GLenum internalFormat,
3148663049a719ed76aa0a6f24f1951a411d241bc8a4Brian Paul                     GLsizei width, GLsizei height, GLsizei depth,
3149663049a719ed76aa0a6f24f1951a411d241bc8a4Brian Paul                     GLint border, GLenum format, GLenum type,
3150663049a719ed76aa0a6f24f1951a411d241bc8a4Brian Paul                     const GLvoid *pixels )
3151663049a719ed76aa0a6f24f1951a411d241bc8a4Brian Paul{
315289fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   _mesa_TexImage3D(target, level, (GLint) internalFormat, width, height,
3153663049a719ed76aa0a6f24f1951a411d241bc8a4Brian Paul                    depth, border, format, type, pixels);
3154663049a719ed76aa0a6f24f1951a411d241bc8a4Brian Paul}
3155663049a719ed76aa0a6f24f1951a411d241bc8a4Brian Paul
3156663049a719ed76aa0a6f24f1951a411d241bc8a4Brian Paul
3157d1dc5b124e133379be5bb57b68733c09c0e04f71Kristian Høgsberg#if FEATURE_OES_EGL_image
3158d1dc5b124e133379be5bb57b68733c09c0e04f71Kristian Høgsbergvoid GLAPIENTRY
3159d1dc5b124e133379be5bb57b68733c09c0e04f71Kristian Høgsberg_mesa_EGLImageTargetTexture2DOES (GLenum target, GLeglImageOES image)
3160d1dc5b124e133379be5bb57b68733c09c0e04f71Kristian Høgsberg{
3161d1dc5b124e133379be5bb57b68733c09c0e04f71Kristian Høgsberg   struct gl_texture_object *texObj;
3162d1dc5b124e133379be5bb57b68733c09c0e04f71Kristian Høgsberg   struct gl_texture_image *texImage;
3163ea9b212fca78eada7cc7c4449de3f31d2ceea1c3Ian Romanick   bool valid_target;
316417efc8c05c6931a4638ee9f6138794f2b2b29540Vinson Lee   GET_CURRENT_CONTEXT(ctx);
316517efc8c05c6931a4638ee9f6138794f2b2b29540Vinson Lee   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3166d1dc5b124e133379be5bb57b68733c09c0e04f71Kristian Høgsberg
3167ea9b212fca78eada7cc7c4449de3f31d2ceea1c3Ian Romanick   switch (target) {
3168ea9b212fca78eada7cc7c4449de3f31d2ceea1c3Ian Romanick   case GL_TEXTURE_2D:
3169ea9b212fca78eada7cc7c4449de3f31d2ceea1c3Ian Romanick      valid_target = ctx->Extensions.OES_EGL_image;
3170ea9b212fca78eada7cc7c4449de3f31d2ceea1c3Ian Romanick      break;
3171ea9b212fca78eada7cc7c4449de3f31d2ceea1c3Ian Romanick   case GL_TEXTURE_EXTERNAL_OES:
3172ea9b212fca78eada7cc7c4449de3f31d2ceea1c3Ian Romanick      valid_target = ctx->Extensions.OES_EGL_image_external;
3173ea9b212fca78eada7cc7c4449de3f31d2ceea1c3Ian Romanick      break;
3174ea9b212fca78eada7cc7c4449de3f31d2ceea1c3Ian Romanick   default:
3175ea9b212fca78eada7cc7c4449de3f31d2ceea1c3Ian Romanick      valid_target = false;
3176ea9b212fca78eada7cc7c4449de3f31d2ceea1c3Ian Romanick      break;
3177ea9b212fca78eada7cc7c4449de3f31d2ceea1c3Ian Romanick   }
3178ea9b212fca78eada7cc7c4449de3f31d2ceea1c3Ian Romanick
3179ea9b212fca78eada7cc7c4449de3f31d2ceea1c3Ian Romanick   if (!valid_target) {
3180d1dc5b124e133379be5bb57b68733c09c0e04f71Kristian Høgsberg      _mesa_error(ctx, GL_INVALID_ENUM,
3181d1dc5b124e133379be5bb57b68733c09c0e04f71Kristian Høgsberg		  "glEGLImageTargetTexture2D(target=%d)", target);
3182d1dc5b124e133379be5bb57b68733c09c0e04f71Kristian Høgsberg      return;
3183d1dc5b124e133379be5bb57b68733c09c0e04f71Kristian Høgsberg   }
3184d1dc5b124e133379be5bb57b68733c09c0e04f71Kristian Høgsberg
31857ecb61c30c4984a8513b306b640f9d274d8ecf61Brian Paul   if (ctx->NewState & _NEW_PIXEL)
3186d1dc5b124e133379be5bb57b68733c09c0e04f71Kristian Høgsberg      _mesa_update_state(ctx);
3187d1dc5b124e133379be5bb57b68733c09c0e04f71Kristian Høgsberg
3188d1dc5b124e133379be5bb57b68733c09c0e04f71Kristian Høgsberg   texObj = _mesa_get_current_tex_object(ctx, target);
3189d1dc5b124e133379be5bb57b68733c09c0e04f71Kristian Høgsberg   _mesa_lock_texture(ctx, texObj);
3190d1dc5b124e133379be5bb57b68733c09c0e04f71Kristian Høgsberg
319118bb6852b234a69530a4a6f3ff4e9d33b8e8c7fbDylan Noblesmith   if (texObj->Immutable) {
319218bb6852b234a69530a4a6f3ff4e9d33b8e8c7fbDylan Noblesmith      _mesa_error(ctx, GL_INVALID_OPERATION,
319318bb6852b234a69530a4a6f3ff4e9d33b8e8c7fbDylan Noblesmith		  "glEGLImageTargetTexture2D(texture is immutable)");
319418bb6852b234a69530a4a6f3ff4e9d33b8e8c7fbDylan Noblesmith      _mesa_unlock_texture(ctx, texObj);
319518bb6852b234a69530a4a6f3ff4e9d33b8e8c7fbDylan Noblesmith      return;
319618bb6852b234a69530a4a6f3ff4e9d33b8e8c7fbDylan Noblesmith   }
319718bb6852b234a69530a4a6f3ff4e9d33b8e8c7fbDylan Noblesmith
3198d1dc5b124e133379be5bb57b68733c09c0e04f71Kristian Høgsberg   texImage = _mesa_get_tex_image(ctx, texObj, target, 0);
3199d1dc5b124e133379be5bb57b68733c09c0e04f71Kristian Høgsberg   if (!texImage) {
3200d1dc5b124e133379be5bb57b68733c09c0e04f71Kristian Høgsberg      _mesa_error(ctx, GL_OUT_OF_MEMORY, "glEGLImageTargetTexture2D");
3201d1dc5b124e133379be5bb57b68733c09c0e04f71Kristian Høgsberg   } else {
320268f8cf72638e974a5bf2ae3e88c2e3539d88e2b6Eric Anholt      ctx->Driver.FreeTextureImageBuffer(ctx, texImage);
3203d1dc5b124e133379be5bb57b68733c09c0e04f71Kristian Høgsberg
3204d1dc5b124e133379be5bb57b68733c09c0e04f71Kristian Høgsberg      ctx->Driver.EGLImageTargetTexture2D(ctx, target,
3205d1dc5b124e133379be5bb57b68733c09c0e04f71Kristian Høgsberg					  texObj, texImage, image);
3206d1dc5b124e133379be5bb57b68733c09c0e04f71Kristian Høgsberg
3207878c69fe540a73011b676ead33ac8d9b8c9e63aaBrian Paul      _mesa_dirty_texobj(ctx, texObj, GL_TRUE);
3208d1dc5b124e133379be5bb57b68733c09c0e04f71Kristian Høgsberg   }
3209d1dc5b124e133379be5bb57b68733c09c0e04f71Kristian Høgsberg   _mesa_unlock_texture(ctx, texObj);
3210d1dc5b124e133379be5bb57b68733c09c0e04f71Kristian Høgsberg
3211d1dc5b124e133379be5bb57b68733c09c0e04f71Kristian Høgsberg}
3212d1dc5b124e133379be5bb57b68733c09c0e04f71Kristian Høgsberg#endif
3213d1dc5b124e133379be5bb57b68733c09c0e04f71Kristian Høgsberg
3214afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
321545124e043da5a67e1081526d61b99422f4ccde19Brian Paul
321645124e043da5a67e1081526d61b99422f4ccde19Brian Paul/**
321745124e043da5a67e1081526d61b99422f4ccde19Brian Paul * Implement all the glTexSubImage1/2/3D() functions.
321845124e043da5a67e1081526d61b99422f4ccde19Brian Paul */
321945124e043da5a67e1081526d61b99422f4ccde19Brian Paulstatic void
322045124e043da5a67e1081526d61b99422f4ccde19Brian Paultexsubimage(struct gl_context *ctx, GLuint dims, GLenum target, GLint level,
322145124e043da5a67e1081526d61b99422f4ccde19Brian Paul            GLint xoffset, GLint yoffset, GLint zoffset,
322245124e043da5a67e1081526d61b99422f4ccde19Brian Paul            GLsizei width, GLsizei height, GLsizei depth,
322345124e043da5a67e1081526d61b99422f4ccde19Brian Paul            GLenum format, GLenum type, const GLvoid *pixels )
3224afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg{
32250293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul   struct gl_texture_object *texObj;
3226b8fdb900fb9b1c8b1e9ec88509624237307a869aBrian Paul   struct gl_texture_image *texImage;
322745124e043da5a67e1081526d61b99422f4ccde19Brian Paul
322875280a2c742ee241d392618a3b29548fce1520a9Brian Paul   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
322975280a2c742ee241d392618a3b29548fce1520a9Brian Paul
3230d100cbf721010f4eacc87507cc87c5314150d493Maciej Cencora   if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
323145124e043da5a67e1081526d61b99422f4ccde19Brian Paul      _mesa_debug(ctx, "glTexSubImage%uD %s %d %d %d %d %d %d %d %s %s %p\n",
323245124e043da5a67e1081526d61b99422f4ccde19Brian Paul                  dims,
3233d100cbf721010f4eacc87507cc87c5314150d493Maciej Cencora                  _mesa_lookup_enum_by_nr(target), level,
323445124e043da5a67e1081526d61b99422f4ccde19Brian Paul                  xoffset, yoffset, zoffset, width, height, depth,
323545124e043da5a67e1081526d61b99422f4ccde19Brian Paul                  _mesa_lookup_enum_by_nr(format),
3236d100cbf721010f4eacc87507cc87c5314150d493Maciej Cencora                  _mesa_lookup_enum_by_nr(type), pixels);
3237d100cbf721010f4eacc87507cc87c5314150d493Maciej Cencora
323845124e043da5a67e1081526d61b99422f4ccde19Brian Paul   /* check target (proxies not allowed) */
323909a5e028a6d254f619132e302a96c8535018603dBrian Paul   if (!legal_texsubimage_target(ctx, dims, target)) {
324045124e043da5a67e1081526d61b99422f4ccde19Brian Paul      _mesa_error(ctx, GL_INVALID_ENUM, "glTexSubImage%uD(target=%s)",
324145124e043da5a67e1081526d61b99422f4ccde19Brian Paul                  dims, _mesa_lookup_enum_by_nr(target));
324245124e043da5a67e1081526d61b99422f4ccde19Brian Paul      return;
324345124e043da5a67e1081526d61b99422f4ccde19Brian Paul   }
324445124e043da5a67e1081526d61b99422f4ccde19Brian Paul
32457ecb61c30c4984a8513b306b640f9d274d8ecf61Brian Paul   if (ctx->NewState & _NEW_PIXEL)
324608836341788a9f9d638d9dc8328510ccd18ddeb5Brian Paul      _mesa_update_state(ctx);
32472aadbf41dfd4f63c6118d0ad2d8659d289cbe454Brian Paul
324845124e043da5a67e1081526d61b99422f4ccde19Brian Paul   if (subtexture_error_check(ctx, dims, target, level, xoffset, yoffset, zoffset,
324945124e043da5a67e1081526d61b99422f4ccde19Brian Paul                              width, height, depth, format, type)) {
3250f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul      return;   /* error was detected */
3251afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   }
3252afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
3253b8fdb900fb9b1c8b1e9ec88509624237307a869aBrian Paul   texObj = _mesa_get_current_tex_object(ctx, target);
3254afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
32555ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell   _mesa_lock_texture(ctx, texObj);
32565ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell   {
32575ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell      texImage = _mesa_select_tex_image(ctx, texObj, target, level);
3258afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
325945124e043da5a67e1081526d61b99422f4ccde19Brian Paul      if (subtexture_error_check2(ctx, dims, target, level,
326045124e043da5a67e1081526d61b99422f4ccde19Brian Paul                                  xoffset, yoffset, zoffset,
326145124e043da5a67e1081526d61b99422f4ccde19Brian Paul				  width, height, depth,
326245124e043da5a67e1081526d61b99422f4ccde19Brian Paul                                  format, type, texImage)) {
3263a31ac84d743e33dabdcbbb4998fb6b7ceccf27d4Brian Paul         /* error was recorded */
32645ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell      }
32654609e80288bacf19af99b0ed7656eef9bb280912Brian Paul      else if (width > 0 && height > 0 && depth > 0) {
326645124e043da5a67e1081526d61b99422f4ccde19Brian Paul         /* If we have a border, offset=-1 is legal.  Bias by border width. */
326745124e043da5a67e1081526d61b99422f4ccde19Brian Paul         switch (dims) {
326845124e043da5a67e1081526d61b99422f4ccde19Brian Paul         case 3:
32696eba268ec65523fe8b2ec7dee670b2b5d4ebe1d9Brian Paul            if (target != GL_TEXTURE_2D_ARRAY)
32706eba268ec65523fe8b2ec7dee670b2b5d4ebe1d9Brian Paul               zoffset += texImage->Border;
327145124e043da5a67e1081526d61b99422f4ccde19Brian Paul            /* fall-through */
327245124e043da5a67e1081526d61b99422f4ccde19Brian Paul         case 2:
32736eba268ec65523fe8b2ec7dee670b2b5d4ebe1d9Brian Paul            if (target != GL_TEXTURE_1D_ARRAY)
32746eba268ec65523fe8b2ec7dee670b2b5d4ebe1d9Brian Paul               yoffset += texImage->Border;
327545124e043da5a67e1081526d61b99422f4ccde19Brian Paul            /* fall-through */
327645124e043da5a67e1081526d61b99422f4ccde19Brian Paul         case 1:
327745124e043da5a67e1081526d61b99422f4ccde19Brian Paul            xoffset += texImage->Border;
327845124e043da5a67e1081526d61b99422f4ccde19Brian Paul         }
3279347b3f0dafe8d0cc30e9d1e7595d12b0ac31bb4aBrian Paul
3280e42d00b3f4503a0840575c8e5f4517a66c8af613Brian Paul         ctx->Driver.TexSubImage(ctx, dims, texImage,
3281e42d00b3f4503a0840575c8e5f4517a66c8af613Brian Paul                                 xoffset, yoffset, zoffset,
3282e42d00b3f4503a0840575c8e5f4517a66c8af613Brian Paul                                 width, height, depth,
3283e42d00b3f4503a0840575c8e5f4517a66c8af613Brian Paul                                 format, type, pixels, &ctx->Unpack);
32842914f2b7fd9bd1082f8cce724d234208b900c3d4Brian Paul
32852914f2b7fd9bd1082f8cce724d234208b900c3d4Brian Paul         check_gen_mipmap(ctx, target, texObj, level);
32862914f2b7fd9bd1082f8cce724d234208b900c3d4Brian Paul
3287ecf6f37fc201d7c88f8214b04ca1f61071c460a1Brian Paul         ctx->NewState |= _NEW_TEXTURE;
3288ecf6f37fc201d7c88f8214b04ca1f61071c460a1Brian Paul      }
32895ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell   }
32905ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell   _mesa_unlock_texture(ctx, texObj);
3291afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg}
3292afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
3293afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
3294c40d1dd62dd9bcbb97128e37a75d991a8d3b2d8cKendall Bennettvoid GLAPIENTRY
329545124e043da5a67e1081526d61b99422f4ccde19Brian Paul_mesa_TexSubImage1D( GLenum target, GLint level,
329645124e043da5a67e1081526d61b99422f4ccde19Brian Paul                     GLint xoffset, GLsizei width,
329745124e043da5a67e1081526d61b99422f4ccde19Brian Paul                     GLenum format, GLenum type,
329845124e043da5a67e1081526d61b99422f4ccde19Brian Paul                     const GLvoid *pixels )
329945124e043da5a67e1081526d61b99422f4ccde19Brian Paul{
330045124e043da5a67e1081526d61b99422f4ccde19Brian Paul   GET_CURRENT_CONTEXT(ctx);
330145124e043da5a67e1081526d61b99422f4ccde19Brian Paul   texsubimage(ctx, 1, target, level,
330245124e043da5a67e1081526d61b99422f4ccde19Brian Paul               xoffset, 0, 0,
330345124e043da5a67e1081526d61b99422f4ccde19Brian Paul               width, 1, 1,
330445124e043da5a67e1081526d61b99422f4ccde19Brian Paul               format, type, pixels);
330545124e043da5a67e1081526d61b99422f4ccde19Brian Paul}
330645124e043da5a67e1081526d61b99422f4ccde19Brian Paul
330745124e043da5a67e1081526d61b99422f4ccde19Brian Paul
330845124e043da5a67e1081526d61b99422f4ccde19Brian Paulvoid GLAPIENTRY
3309fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul_mesa_TexSubImage2D( GLenum target, GLint level,
3310fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul                     GLint xoffset, GLint yoffset,
3311fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul                     GLsizei width, GLsizei height,
3312fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul                     GLenum format, GLenum type,
3313fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul                     const GLvoid *pixels )
3314afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg{
33152c3d34c905fa6b831a066afae83b938de05eb241Gareth Hughes   GET_CURRENT_CONTEXT(ctx);
331645124e043da5a67e1081526d61b99422f4ccde19Brian Paul   texsubimage(ctx, 2, target, level,
331745124e043da5a67e1081526d61b99422f4ccde19Brian Paul               xoffset, yoffset, 0,
331845124e043da5a67e1081526d61b99422f4ccde19Brian Paul               width, height, 1,
331945124e043da5a67e1081526d61b99422f4ccde19Brian Paul               format, type, pixels);
3320c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul}
3321afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
3322afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
3323afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
3324c40d1dd62dd9bcbb97128e37a75d991a8d3b2d8cKendall Bennettvoid GLAPIENTRY
3325fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul_mesa_TexSubImage3D( GLenum target, GLint level,
3326fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul                     GLint xoffset, GLint yoffset, GLint zoffset,
3327fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul                     GLsizei width, GLsizei height, GLsizei depth,
3328fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul                     GLenum format, GLenum type,
3329fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul                     const GLvoid *pixels )
3330afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg{
33312c3d34c905fa6b831a066afae83b938de05eb241Gareth Hughes   GET_CURRENT_CONTEXT(ctx);
333245124e043da5a67e1081526d61b99422f4ccde19Brian Paul   texsubimage(ctx, 3, target, level,
333345124e043da5a67e1081526d61b99422f4ccde19Brian Paul               xoffset, yoffset, zoffset,
333445124e043da5a67e1081526d61b99422f4ccde19Brian Paul               width, height, depth,
333545124e043da5a67e1081526d61b99422f4ccde19Brian Paul               format, type, pixels);
3336afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg}
3337afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
3338afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
3339afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
334011f386fb504b9f07dd501a65f31185cfa0e9a679Brian Paul/**
334156b57aa360a8bad0c4b68fbdf7c64ac33f9e7661Brian Paul * For glCopyTexSubImage, return the source renderbuffer to copy texel data
334256b57aa360a8bad0c4b68fbdf7c64ac33f9e7661Brian Paul * from.  This depends on whether the texture contains color or depth values.
334356b57aa360a8bad0c4b68fbdf7c64ac33f9e7661Brian Paul */
334456b57aa360a8bad0c4b68fbdf7c64ac33f9e7661Brian Paulstatic struct gl_renderbuffer *
334556b57aa360a8bad0c4b68fbdf7c64ac33f9e7661Brian Paulget_copy_tex_image_source(struct gl_context *ctx, gl_format texFormat)
334656b57aa360a8bad0c4b68fbdf7c64ac33f9e7661Brian Paul{
334756b57aa360a8bad0c4b68fbdf7c64ac33f9e7661Brian Paul   if (_mesa_get_format_bits(texFormat, GL_DEPTH_BITS) > 0) {
334856b57aa360a8bad0c4b68fbdf7c64ac33f9e7661Brian Paul      /* reading from depth/stencil buffer */
334956b57aa360a8bad0c4b68fbdf7c64ac33f9e7661Brian Paul      return ctx->ReadBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;
335056b57aa360a8bad0c4b68fbdf7c64ac33f9e7661Brian Paul   }
335156b57aa360a8bad0c4b68fbdf7c64ac33f9e7661Brian Paul   else {
335256b57aa360a8bad0c4b68fbdf7c64ac33f9e7661Brian Paul      /* copying from color buffer */
335356b57aa360a8bad0c4b68fbdf7c64ac33f9e7661Brian Paul      return ctx->ReadBuffer->_ColorReadBuffer;
335456b57aa360a8bad0c4b68fbdf7c64ac33f9e7661Brian Paul   }
335556b57aa360a8bad0c4b68fbdf7c64ac33f9e7661Brian Paul}
335656b57aa360a8bad0c4b68fbdf7c64ac33f9e7661Brian Paul
335756b57aa360a8bad0c4b68fbdf7c64ac33f9e7661Brian Paul
335856b57aa360a8bad0c4b68fbdf7c64ac33f9e7661Brian Paul
335956b57aa360a8bad0c4b68fbdf7c64ac33f9e7661Brian Paul/**
336011f386fb504b9f07dd501a65f31185cfa0e9a679Brian Paul * Implement the glCopyTexImage1/2D() functions.
336111f386fb504b9f07dd501a65f31185cfa0e9a679Brian Paul */
336211f386fb504b9f07dd501a65f31185cfa0e9a679Brian Paulstatic void
336311f386fb504b9f07dd501a65f31185cfa0e9a679Brian Paulcopyteximage(struct gl_context *ctx, GLuint dims,
336411f386fb504b9f07dd501a65f31185cfa0e9a679Brian Paul             GLenum target, GLint level, GLenum internalFormat,
336511f386fb504b9f07dd501a65f31185cfa0e9a679Brian Paul             GLint x, GLint y, GLsizei width, GLsizei height, GLint border )
3366afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg{
33672aadbf41dfd4f63c6118d0ad2d8659d289cbe454Brian Paul   struct gl_texture_object *texObj;
33682aadbf41dfd4f63c6118d0ad2d8659d289cbe454Brian Paul   struct gl_texture_image *texImage;
3369e93243f8b7d43695654a36334c8cc5cea140d23cBrian   const GLuint face = _mesa_tex_target_to_face(target);
337011f386fb504b9f07dd501a65f31185cfa0e9a679Brian Paul
3371cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3372afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
3373d100cbf721010f4eacc87507cc87c5314150d493Maciej Cencora   if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
337411f386fb504b9f07dd501a65f31185cfa0e9a679Brian Paul      _mesa_debug(ctx, "glCopyTexImage%uD %s %d %s %d %d %d %d %d\n",
337511f386fb504b9f07dd501a65f31185cfa0e9a679Brian Paul                  dims,
3376d100cbf721010f4eacc87507cc87c5314150d493Maciej Cencora                  _mesa_lookup_enum_by_nr(target), level,
3377d100cbf721010f4eacc87507cc87c5314150d493Maciej Cencora                  _mesa_lookup_enum_by_nr(internalFormat),
337811f386fb504b9f07dd501a65f31185cfa0e9a679Brian Paul                  x, y, width, height, border);
3379d100cbf721010f4eacc87507cc87c5314150d493Maciej Cencora
338063f01309801c5a900d8d7f5ccd63413e33ff9bffBrian Paul   if (ctx->NewState & NEW_COPY_TEX_STATE)
338108836341788a9f9d638d9dc8328510ccd18ddeb5Brian Paul      _mesa_update_state(ctx);
3382fa4525e289b475b928a7b2c4055af9dd7fe46600Brian Paul
338311f386fb504b9f07dd501a65f31185cfa0e9a679Brian Paul   if (copytexture_error_check(ctx, dims, target, level, internalFormat,
338411f386fb504b9f07dd501a65f31185cfa0e9a679Brian Paul                               width, height, border))
3385ab6e78f3a0abac7becbd8a58ed2c89f860cc6b0bBrian Paul      return;
3386ab6e78f3a0abac7becbd8a58ed2c89f860cc6b0bBrian Paul
3387b8fdb900fb9b1c8b1e9ec88509624237307a869aBrian Paul   texObj = _mesa_get_current_tex_object(ctx, target);
3388a31ac84d743e33dabdcbbb4998fb6b7ceccf27d4Brian Paul
3389638b657f838266e3d4892d06ff53a1c1bee24a9bEric Anholt   if (border && ctx->Const.StripTextureBorder) {
3390638b657f838266e3d4892d06ff53a1c1bee24a9bEric Anholt      x += border;
3391638b657f838266e3d4892d06ff53a1c1bee24a9bEric Anholt      width -= border * 2;
3392638b657f838266e3d4892d06ff53a1c1bee24a9bEric Anholt      if (dims == 2) {
3393638b657f838266e3d4892d06ff53a1c1bee24a9bEric Anholt	 y += border;
3394638b657f838266e3d4892d06ff53a1c1bee24a9bEric Anholt	 height -= border * 2;
3395638b657f838266e3d4892d06ff53a1c1bee24a9bEric Anholt      }
3396638b657f838266e3d4892d06ff53a1c1bee24a9bEric Anholt      border = 0;
3397638b657f838266e3d4892d06ff53a1c1bee24a9bEric Anholt   }
3398638b657f838266e3d4892d06ff53a1c1bee24a9bEric Anholt
33995ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell   _mesa_lock_texture(ctx, texObj);
34005ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell   {
34015ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell      texImage = _mesa_get_tex_image(ctx, texObj, target, level);
340211f386fb504b9f07dd501a65f31185cfa0e9a679Brian Paul
34035ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell      if (!texImage) {
340411f386fb504b9f07dd501a65f31185cfa0e9a679Brian Paul	 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexImage%uD", dims);
34055ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell      }
3406c352566933b932ef09cea3f144df21147a7f6bc1Brian Paul      else {
34075874890c26f434f54e9218b83fae4eb8175c24e9Brian Paul         /* choose actual hw format */
34085874890c26f434f54e9218b83fae4eb8175c24e9Brian Paul         gl_format texFormat = _mesa_choose_texture_format(ctx, texObj,
34095874890c26f434f54e9218b83fae4eb8175c24e9Brian Paul                                                           target, level,
34105874890c26f434f54e9218b83fae4eb8175c24e9Brian Paul                                                           internalFormat,
34115874890c26f434f54e9218b83fae4eb8175c24e9Brian Paul                                                           GL_NONE, GL_NONE);
34127dac13272a959654f1010b80c1f1ec6dcae5a4faBrian Paul
34135874890c26f434f54e9218b83fae4eb8175c24e9Brian Paul         if (legal_texture_size(ctx, texFormat, width, height, 1)) {
3414cd9ab2584f5e2a5eb0e96a948e6aedc9a33c886dBrian Paul            GLint srcX = x, srcY = y, dstX = 0, dstY = 0, dstZ = 0;
34157dac13272a959654f1010b80c1f1ec6dcae5a4faBrian Paul
34165874890c26f434f54e9218b83fae4eb8175c24e9Brian Paul            /* Free old texture image */
34170bb29949ba8a9e5a15dc0640dbb0a4e7990a1d57Eric Anholt            ctx->Driver.FreeTextureImageBuffer(ctx, texImage);
34182aadbf41dfd4f63c6118d0ad2d8659d289cbe454Brian Paul
3419892a2542a3f0753a7064c710b96f077dd5490624Brian Paul            _mesa_init_teximage_fields(ctx, texImage, width, height, 1,
34204b08f35487fa439fd9ca4d653d3a146c3dc09c1cBrian Paul                                       border, internalFormat, texFormat);
342111caea687e3f10ae12d33e44edf84635f73047ddBrian Paul
34225874890c26f434f54e9218b83fae4eb8175c24e9Brian Paul            /* Allocate texture memory (no pixel data yet) */
3423c5af8891805fc4f590c1371c098cdbc704c44e00Pauli Nieminen            ctx->Driver.TexImage(ctx, dims, texImage,
3424c5af8891805fc4f590c1371c098cdbc704c44e00Pauli Nieminen                                 GL_NONE, GL_NONE,
34258f5fffe75d2f8ae7c7ee706b53379a25bc673ae4Brian Paul                                 NULL, &ctx->Unpack);
34265874890c26f434f54e9218b83fae4eb8175c24e9Brian Paul
34275874890c26f434f54e9218b83fae4eb8175c24e9Brian Paul            if (_mesa_clip_copytexsubimage(ctx, &dstX, &dstY, &srcX, &srcY,
34285874890c26f434f54e9218b83fae4eb8175c24e9Brian Paul                                           &width, &height)) {
342956b57aa360a8bad0c4b68fbdf7c64ac33f9e7661Brian Paul               struct gl_renderbuffer *srcRb =
343056b57aa360a8bad0c4b68fbdf7c64ac33f9e7661Brian Paul                  get_copy_tex_image_source(ctx, texImage->TexFormat);
343156b57aa360a8bad0c4b68fbdf7c64ac33f9e7661Brian Paul
3432cd9ab2584f5e2a5eb0e96a948e6aedc9a33c886dBrian Paul               ctx->Driver.CopyTexSubImage(ctx, dims, texImage, dstX, dstY, dstZ,
3433cd9ab2584f5e2a5eb0e96a948e6aedc9a33c886dBrian Paul                                           srcRb, srcX, srcY, width, height);
34345874890c26f434f54e9218b83fae4eb8175c24e9Brian Paul            }
34352aadbf41dfd4f63c6118d0ad2d8659d289cbe454Brian Paul
34364b08f35487fa439fd9ca4d653d3a146c3dc09c1cBrian Paul            check_gen_mipmap(ctx, target, texObj, level);
34372914f2b7fd9bd1082f8cce724d234208b900c3d4Brian Paul
3438d842a118b26a56c9c5e4310f30f70199c42ea90eBrian Paul            _mesa_update_fbo_texture(ctx, texObj, face, level);
3439519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul
3440878c69fe540a73011b676ead33ac8d9b8c9e63aaBrian Paul            _mesa_dirty_texobj(ctx, texObj, GL_TRUE);
34414b08f35487fa439fd9ca4d653d3a146c3dc09c1cBrian Paul         }
34424b08f35487fa439fd9ca4d653d3a146c3dc09c1cBrian Paul         else {
34435874890c26f434f54e9218b83fae4eb8175c24e9Brian Paul            /* probably too large of image */
344411f386fb504b9f07dd501a65f31185cfa0e9a679Brian Paul            _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexImage%uD", dims);
34454b08f35487fa439fd9ca4d653d3a146c3dc09c1cBrian Paul         }
3446c352566933b932ef09cea3f144df21147a7f6bc1Brian Paul      }
34475ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell   }
34485ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell   _mesa_unlock_texture(ctx, texObj);
3449afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg}
3450afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
3451afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
3452afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
3453c40d1dd62dd9bcbb97128e37a75d991a8d3b2d8cKendall Bennettvoid GLAPIENTRY
345411f386fb504b9f07dd501a65f31185cfa0e9a679Brian Paul_mesa_CopyTexImage1D( GLenum target, GLint level,
345511f386fb504b9f07dd501a65f31185cfa0e9a679Brian Paul                      GLenum internalFormat,
345611f386fb504b9f07dd501a65f31185cfa0e9a679Brian Paul                      GLint x, GLint y,
345711f386fb504b9f07dd501a65f31185cfa0e9a679Brian Paul                      GLsizei width, GLint border )
3458afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg{
3459fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul   GET_CURRENT_CONTEXT(ctx);
346011f386fb504b9f07dd501a65f31185cfa0e9a679Brian Paul   copyteximage(ctx, 1, target, level, internalFormat, x, y, width, 1, border);
346111f386fb504b9f07dd501a65f31185cfa0e9a679Brian Paul}
3462c352566933b932ef09cea3f144df21147a7f6bc1Brian Paul
34632914f2b7fd9bd1082f8cce724d234208b900c3d4Brian Paul
3464c352566933b932ef09cea3f144df21147a7f6bc1Brian Paul
346511f386fb504b9f07dd501a65f31185cfa0e9a679Brian Paulvoid GLAPIENTRY
346611f386fb504b9f07dd501a65f31185cfa0e9a679Brian Paul_mesa_CopyTexImage2D( GLenum target, GLint level, GLenum internalFormat,
346711f386fb504b9f07dd501a65f31185cfa0e9a679Brian Paul                      GLint x, GLint y, GLsizei width, GLsizei height,
346811f386fb504b9f07dd501a65f31185cfa0e9a679Brian Paul                      GLint border )
346911f386fb504b9f07dd501a65f31185cfa0e9a679Brian Paul{
347011f386fb504b9f07dd501a65f31185cfa0e9a679Brian Paul   GET_CURRENT_CONTEXT(ctx);
347111f386fb504b9f07dd501a65f31185cfa0e9a679Brian Paul   copyteximage(ctx, 2, target, level, internalFormat,
347211f386fb504b9f07dd501a65f31185cfa0e9a679Brian Paul                x, y, width, height, border);
3473afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg}
3474afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
3475afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
34761c23b860cea93534f726d2a18658444f37f43538Brian Paul
34771c23b860cea93534f726d2a18658444f37f43538Brian Paul/**
34781c23b860cea93534f726d2a18658444f37f43538Brian Paul * Implementation for glCopyTexSubImage1/2/3D() functions.
34791c23b860cea93534f726d2a18658444f37f43538Brian Paul */
34801c23b860cea93534f726d2a18658444f37f43538Brian Paulstatic void
34811c23b860cea93534f726d2a18658444f37f43538Brian Paulcopytexsubimage(struct gl_context *ctx, GLuint dims, GLenum target, GLint level,
34821c23b860cea93534f726d2a18658444f37f43538Brian Paul                GLint xoffset, GLint yoffset, GLint zoffset,
34831c23b860cea93534f726d2a18658444f37f43538Brian Paul                GLint x, GLint y, GLsizei width, GLsizei height)
3484afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg{
34855ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell   struct gl_texture_object *texObj;
3486347b3f0dafe8d0cc30e9d1e7595d12b0ac31bb4aBrian Paul   struct gl_texture_image *texImage;
348703bafd1f9fa000abdb794b2ae344a68840c83201Brian
3488cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3489afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
3490d100cbf721010f4eacc87507cc87c5314150d493Maciej Cencora   if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
34911c23b860cea93534f726d2a18658444f37f43538Brian Paul      _mesa_debug(ctx, "glCopyTexSubImage%uD %s %d %d %d %d %d %d %d %d\n",
34921c23b860cea93534f726d2a18658444f37f43538Brian Paul                  dims,
3493d100cbf721010f4eacc87507cc87c5314150d493Maciej Cencora                  _mesa_lookup_enum_by_nr(target),
34941c23b860cea93534f726d2a18658444f37f43538Brian Paul                  level, xoffset, yoffset, zoffset, x, y, width, height);
3495d100cbf721010f4eacc87507cc87c5314150d493Maciej Cencora
349663f01309801c5a900d8d7f5ccd63413e33ff9bffBrian Paul   if (ctx->NewState & NEW_COPY_TEX_STATE)
349708836341788a9f9d638d9dc8328510ccd18ddeb5Brian Paul      _mesa_update_state(ctx);
3498fa4525e289b475b928a7b2c4055af9dd7fe46600Brian Paul
34991c23b860cea93534f726d2a18658444f37f43538Brian Paul   if (copytexsubimage_error_check1(ctx, dims, target, level))
3500ab6e78f3a0abac7becbd8a58ed2c89f860cc6b0bBrian Paul      return;
3501ab6e78f3a0abac7becbd8a58ed2c89f860cc6b0bBrian Paul
3502b8fdb900fb9b1c8b1e9ec88509624237307a869aBrian Paul   texObj = _mesa_get_current_tex_object(ctx, target);
35035ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell
35045ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell   _mesa_lock_texture(ctx, texObj);
35055ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell   {
35065ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell      texImage = _mesa_select_tex_image(ctx, texObj, target, level);
35075ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell
35081c23b860cea93534f726d2a18658444f37f43538Brian Paul      if (copytexsubimage_error_check2(ctx, dims, target, level, xoffset, yoffset,
35091c23b860cea93534f726d2a18658444f37f43538Brian Paul				       zoffset, width, height, texImage)) {
35101c23b860cea93534f726d2a18658444f37f43538Brian Paul         /* error was recored */
351103bafd1f9fa000abdb794b2ae344a68840c83201Brian      }
3512a31ac84d743e33dabdcbbb4998fb6b7ceccf27d4Brian Paul      else {
35131c23b860cea93534f726d2a18658444f37f43538Brian Paul         /* If we have a border, offset=-1 is legal.  Bias by border width. */
35141c23b860cea93534f726d2a18658444f37f43538Brian Paul         switch (dims) {
35151c23b860cea93534f726d2a18658444f37f43538Brian Paul         case 3:
35166eba268ec65523fe8b2ec7dee670b2b5d4ebe1d9Brian Paul            if (target != GL_TEXTURE_2D_ARRAY)
35176eba268ec65523fe8b2ec7dee670b2b5d4ebe1d9Brian Paul               zoffset += texImage->Border;
35181c23b860cea93534f726d2a18658444f37f43538Brian Paul            /* fall-through */
35191c23b860cea93534f726d2a18658444f37f43538Brian Paul         case 2:
35206eba268ec65523fe8b2ec7dee670b2b5d4ebe1d9Brian Paul            if (target != GL_TEXTURE_1D_ARRAY)
35216eba268ec65523fe8b2ec7dee670b2b5d4ebe1d9Brian Paul               yoffset += texImage->Border;
35221c23b860cea93534f726d2a18658444f37f43538Brian Paul            /* fall-through */
35231c23b860cea93534f726d2a18658444f37f43538Brian Paul         case 1:
35241c23b860cea93534f726d2a18658444f37f43538Brian Paul            xoffset += texImage->Border;
35251c23b860cea93534f726d2a18658444f37f43538Brian Paul         }
352603bafd1f9fa000abdb794b2ae344a68840c83201Brian
3527a31ac84d743e33dabdcbbb4998fb6b7ceccf27d4Brian Paul         if (_mesa_clip_copytexsubimage(ctx, &xoffset, &yoffset, &x, &y,
3528a31ac84d743e33dabdcbbb4998fb6b7ceccf27d4Brian Paul                                        &width, &height)) {
352956b57aa360a8bad0c4b68fbdf7c64ac33f9e7661Brian Paul            struct gl_renderbuffer *srcRb =
353056b57aa360a8bad0c4b68fbdf7c64ac33f9e7661Brian Paul               get_copy_tex_image_source(ctx, texImage->TexFormat);
353156b57aa360a8bad0c4b68fbdf7c64ac33f9e7661Brian Paul
3532cd9ab2584f5e2a5eb0e96a948e6aedc9a33c886dBrian Paul            ctx->Driver.CopyTexSubImage(ctx, dims, texImage,
3533cd9ab2584f5e2a5eb0e96a948e6aedc9a33c886dBrian Paul                                        xoffset, yoffset, zoffset,
3534cd9ab2584f5e2a5eb0e96a948e6aedc9a33c886dBrian Paul                                        srcRb, x, y, width, height);
35352914f2b7fd9bd1082f8cce724d234208b900c3d4Brian Paul
35362914f2b7fd9bd1082f8cce724d234208b900c3d4Brian Paul            check_gen_mipmap(ctx, target, texObj, level);
35372914f2b7fd9bd1082f8cce724d234208b900c3d4Brian Paul
3538a31ac84d743e33dabdcbbb4998fb6b7ceccf27d4Brian Paul            ctx->NewState |= _NEW_TEXTURE;
3539a31ac84d743e33dabdcbbb4998fb6b7ceccf27d4Brian Paul         }
3540a31ac84d743e33dabdcbbb4998fb6b7ceccf27d4Brian Paul      }
35415ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell   }
35425ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell   _mesa_unlock_texture(ctx, texObj);
3543afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg}
3544afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
3545afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
35461c23b860cea93534f726d2a18658444f37f43538Brian Paulvoid GLAPIENTRY
35471c23b860cea93534f726d2a18658444f37f43538Brian Paul_mesa_CopyTexSubImage1D( GLenum target, GLint level,
35481c23b860cea93534f726d2a18658444f37f43538Brian Paul                         GLint xoffset, GLint x, GLint y, GLsizei width )
35491c23b860cea93534f726d2a18658444f37f43538Brian Paul{
35501c23b860cea93534f726d2a18658444f37f43538Brian Paul   GET_CURRENT_CONTEXT(ctx);
35511c23b860cea93534f726d2a18658444f37f43538Brian Paul   copytexsubimage(ctx, 1, target, level, xoffset, 0, 0, x, y, width, 1);
35521c23b860cea93534f726d2a18658444f37f43538Brian Paul}
35531c23b860cea93534f726d2a18658444f37f43538Brian Paul
35541c23b860cea93534f726d2a18658444f37f43538Brian Paul
3555afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
3556c40d1dd62dd9bcbb97128e37a75d991a8d3b2d8cKendall Bennettvoid GLAPIENTRY
3557fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul_mesa_CopyTexSubImage2D( GLenum target, GLint level,
3558fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul                         GLint xoffset, GLint yoffset,
3559fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul                         GLint x, GLint y, GLsizei width, GLsizei height )
3560afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg{
3561fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul   GET_CURRENT_CONTEXT(ctx);
35621c23b860cea93534f726d2a18658444f37f43538Brian Paul   copytexsubimage(ctx, 2, target, level, xoffset, yoffset, 0, x, y,
35631c23b860cea93534f726d2a18658444f37f43538Brian Paul                   width, height);
3564afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg}
3565afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
3566afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
3567afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
3568c40d1dd62dd9bcbb97128e37a75d991a8d3b2d8cKendall Bennettvoid GLAPIENTRY
3569fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul_mesa_CopyTexSubImage3D( GLenum target, GLint level,
3570fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul                         GLint xoffset, GLint yoffset, GLint zoffset,
3571fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul                         GLint x, GLint y, GLsizei width, GLsizei height )
3572afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg{
3573fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul   GET_CURRENT_CONTEXT(ctx);
35741c23b860cea93534f726d2a18658444f37f43538Brian Paul   copytexsubimage(ctx, 3, target, level, xoffset, yoffset, zoffset,
35751c23b860cea93534f726d2a18658444f37f43538Brian Paul                   x, y, width, height);
3576afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg}
35771207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul
35781207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul
35791207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul
358089fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul
358189fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul/**********************************************************************/
358289fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul/******                   Compressed Textures                    ******/
358389fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul/**********************************************************************/
358489fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul
358589fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul
358689fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul/**
358789fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul * Error checking for glCompressedTexSubImage[123]D().
35882371033bbaf6e7477bfa893654a0467a084f335bBrian Paul * \warning  There are some bad assumptions here about the size of compressed
35892371033bbaf6e7477bfa893654a0467a084f335bBrian Paul *           texture tiles (multiple of 4) used to test the validity of the
35902371033bbaf6e7477bfa893654a0467a084f335bBrian Paul *           offset and size parameters.
359189fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul * \return error code or GL_NO_ERROR.
359289fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul */
359389fb06fcc11cbe3f23521312155d6c55d869f526Brian Paulstatic GLenum
3594f9995b30756140724f41daf963fa06167912be7fKristian Høgsbergcompressed_subtexture_error_check(struct gl_context *ctx, GLint dimensions,
359589fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul                                  GLenum target, GLint level,
359689fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul                                  GLint xoffset, GLint yoffset, GLint zoffset,
359789fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul                                  GLsizei width, GLsizei height, GLsizei depth,
359889fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul                                  GLenum format, GLsizei imageSize)
359989fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul{
360089fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   GLint expectedSize, maxLevels = 0, maxTextureSize;
360107ad6393cb31d8f1f086f9c46705334ef9cf25f6Brian Paul   GLuint bw, bh;
3602a6c423d95663cfd8601cf84e10e8e1b12fa6ef15Brian Paul   (void) zoffset;
360389fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul
360489fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   if (dimensions == 1) {
360589fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      /* 1D compressed textures not allowed */
360689fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      return GL_INVALID_ENUM;
360789fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   }
360889fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   else if (dimensions == 2) {
360989fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      if (target == GL_PROXY_TEXTURE_2D) {
361089fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         maxLevels = ctx->Const.MaxTextureLevels;
361189fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      }
361289fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      else if (target == GL_TEXTURE_2D) {
361389fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         maxLevels = ctx->Const.MaxTextureLevels;
361489fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      }
361589fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      else if (target == GL_PROXY_TEXTURE_CUBE_MAP_ARB) {
361689fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         if (!ctx->Extensions.ARB_texture_cube_map)
361789fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul            return GL_INVALID_ENUM; /*target*/
361889fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         maxLevels = ctx->Const.MaxCubeTextureLevels;
361989fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      }
36202efa3d4f9fd1e1683c5366d402d8740ecfb688e5Brian Paul      else if (_mesa_is_cube_face(target)) {
362189fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         if (!ctx->Extensions.ARB_texture_cube_map)
362289fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul            return GL_INVALID_ENUM; /*target*/
362389fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         maxLevels = ctx->Const.MaxCubeTextureLevels;
362489fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      }
362589fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      else {
362689fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         return GL_INVALID_ENUM; /*target*/
362789fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      }
362889fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   }
362989fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   else if (dimensions == 3) {
363089fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      /* 3D compressed textures not allowed */
363189fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      return GL_INVALID_ENUM;
363289fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   }
363389fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul
363489fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   maxTextureSize = 1 << (maxLevels - 1);
363589fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul
3636c3c19be8e0d0b13916cc128cf3c8e839935c912aBrian Paul   /* this will catch any invalid compressed format token */
3637c34feadd1c2fa5c62022c1f48ee675b25a985ac6Brian Paul   if (!_mesa_is_compressed_format(ctx, format))
363889fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      return GL_INVALID_ENUM;
363989fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul
3640e75051d1967350ceff0209dde24ae42696b13b5cBrian Paul   if (width < 0 || width > maxTextureSize)
364189fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      return GL_INVALID_VALUE;
364289fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul
3643e75051d1967350ceff0209dde24ae42696b13b5cBrian Paul   if ((height < 0 || height > maxTextureSize)
364489fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul       && dimensions > 1)
364589fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      return GL_INVALID_VALUE;
364689fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul
364789fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   if (level < 0 || level >= maxLevels)
364889fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      return GL_INVALID_VALUE;
364989fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul
365007ad6393cb31d8f1f086f9c46705334ef9cf25f6Brian Paul   /*
365107ad6393cb31d8f1f086f9c46705334ef9cf25f6Brian Paul    * do checks which depend on compression block size
36522371033bbaf6e7477bfa893654a0467a084f335bBrian Paul    */
365307ad6393cb31d8f1f086f9c46705334ef9cf25f6Brian Paul   get_compressed_block_size(format, &bw, &bh);
365407ad6393cb31d8f1f086f9c46705334ef9cf25f6Brian Paul
365507ad6393cb31d8f1f086f9c46705334ef9cf25f6Brian Paul   if ((xoffset % bw != 0) || (yoffset % bh != 0))
36565ef472dd83b7b96538432157e2569dedf5625161Brian Paul      return GL_INVALID_OPERATION;
365789fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul
365807ad6393cb31d8f1f086f9c46705334ef9cf25f6Brian Paul   if ((width % bw != 0) && width != 2 && width != 1)
36595ef472dd83b7b96538432157e2569dedf5625161Brian Paul      return GL_INVALID_OPERATION;
366089fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul
366107ad6393cb31d8f1f086f9c46705334ef9cf25f6Brian Paul   if ((height % bh != 0) && height != 2 && height != 1)
36625ef472dd83b7b96538432157e2569dedf5625161Brian Paul      return GL_INVALID_OPERATION;
366389fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul
366454bb414e00a4daedbe530b9933bc11bac4ae7149Brian Paul   expectedSize = compressed_tex_size(width, height, depth, format);
366589fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   if (expectedSize != imageSize)
366689fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      return GL_INVALID_VALUE;
366789fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul
366889fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   return GL_NO_ERROR;
366989fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul}
367089fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul
367189fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul
3672f3c29bd74f01370a6bbff1329966ca9d1864315fBrian Paul/**
3673f3c29bd74f01370a6bbff1329966ca9d1864315fBrian Paul * Do second part of glCompressedTexSubImage error checking.
3674f3c29bd74f01370a6bbff1329966ca9d1864315fBrian Paul * \return GL_TRUE if error found, GL_FALSE otherwise.
3675f3c29bd74f01370a6bbff1329966ca9d1864315fBrian Paul */
3676f3c29bd74f01370a6bbff1329966ca9d1864315fBrian Paulstatic GLboolean
3677f9995b30756140724f41daf963fa06167912be7fKristian Høgsbergcompressed_subtexture_error_check2(struct gl_context *ctx, GLuint dims,
3678f3c29bd74f01370a6bbff1329966ca9d1864315fBrian Paul                                   GLsizei width, GLsizei height,
3679f3c29bd74f01370a6bbff1329966ca9d1864315fBrian Paul                                   GLsizei depth, GLenum format,
3680f3c29bd74f01370a6bbff1329966ca9d1864315fBrian Paul                                   struct gl_texture_image *texImage)
3681f3c29bd74f01370a6bbff1329966ca9d1864315fBrian Paul{
3682604cd6b966d060334fdaa11ae8444797d54d3142Brian Paul   if (!texImage) {
3683604cd6b966d060334fdaa11ae8444797d54d3142Brian Paul      _mesa_error(ctx, GL_INVALID_OPERATION,
3684604cd6b966d060334fdaa11ae8444797d54d3142Brian Paul                  "glCompressedTexSubImage%uD(undefined image level)", dims);
3685604cd6b966d060334fdaa11ae8444797d54d3142Brian Paul      return GL_TRUE;
3686604cd6b966d060334fdaa11ae8444797d54d3142Brian Paul   }
3687f3c29bd74f01370a6bbff1329966ca9d1864315fBrian Paul
3688f3c29bd74f01370a6bbff1329966ca9d1864315fBrian Paul   if ((GLint) format != texImage->InternalFormat) {
3689f3c29bd74f01370a6bbff1329966ca9d1864315fBrian Paul      _mesa_error(ctx, GL_INVALID_OPERATION,
3690f3c29bd74f01370a6bbff1329966ca9d1864315fBrian Paul                  "glCompressedTexSubImage%uD(format=0x%x)", dims, format);
3691f3c29bd74f01370a6bbff1329966ca9d1864315fBrian Paul      return GL_TRUE;
3692f3c29bd74f01370a6bbff1329966ca9d1864315fBrian Paul   }
3693f3c29bd74f01370a6bbff1329966ca9d1864315fBrian Paul
3694d4a38e86d4b4d66cca20ee63222f940cb73fa709Chia-I Wu   if (compressedteximage_only_format(ctx, format)) {
3695d4a38e86d4b4d66cca20ee63222f940cb73fa709Chia-I Wu      _mesa_error(ctx, GL_INVALID_OPERATION,
3696d4a38e86d4b4d66cca20ee63222f940cb73fa709Chia-I Wu                  "glCompressedTexSubImage%uD(format=0x%x cannot be updated)"
3697d4a38e86d4b4d66cca20ee63222f940cb73fa709Chia-I Wu                  , dims, format);
3698d4a38e86d4b4d66cca20ee63222f940cb73fa709Chia-I Wu      return GL_TRUE;
3699d4a38e86d4b4d66cca20ee63222f940cb73fa709Chia-I Wu   }
3700d4a38e86d4b4d66cca20ee63222f940cb73fa709Chia-I Wu
3701f3c29bd74f01370a6bbff1329966ca9d1864315fBrian Paul   if (((width == 1 || width == 2) &&
3702ca0d048e4e0cc5885cd309cdf9057cfe6044924eBrian Paul        width != (GLsizei) texImage->Width) ||
3703ca0d048e4e0cc5885cd309cdf9057cfe6044924eBrian Paul       (width > (GLsizei) texImage->Width)) {
3704f3c29bd74f01370a6bbff1329966ca9d1864315fBrian Paul      _mesa_error(ctx, GL_INVALID_VALUE,
3705f3c29bd74f01370a6bbff1329966ca9d1864315fBrian Paul                  "glCompressedTexSubImage%uD(width=%d)", dims, width);
3706f3c29bd74f01370a6bbff1329966ca9d1864315fBrian Paul      return GL_TRUE;
3707f3c29bd74f01370a6bbff1329966ca9d1864315fBrian Paul   }
3708f3c29bd74f01370a6bbff1329966ca9d1864315fBrian Paul
3709f3c29bd74f01370a6bbff1329966ca9d1864315fBrian Paul   if (dims >= 2) {
3710f3c29bd74f01370a6bbff1329966ca9d1864315fBrian Paul      if (((height == 1 || height == 2) &&
3711ca0d048e4e0cc5885cd309cdf9057cfe6044924eBrian Paul           height != (GLsizei) texImage->Height) ||
3712ca0d048e4e0cc5885cd309cdf9057cfe6044924eBrian Paul          (height > (GLsizei) texImage->Height)) {
3713f3c29bd74f01370a6bbff1329966ca9d1864315fBrian Paul         _mesa_error(ctx, GL_INVALID_VALUE,
3714f3c29bd74f01370a6bbff1329966ca9d1864315fBrian Paul                     "glCompressedTexSubImage%uD(height=%d)", dims, height);
3715f3c29bd74f01370a6bbff1329966ca9d1864315fBrian Paul         return GL_TRUE;
3716f3c29bd74f01370a6bbff1329966ca9d1864315fBrian Paul      }
3717f3c29bd74f01370a6bbff1329966ca9d1864315fBrian Paul   }
3718f3c29bd74f01370a6bbff1329966ca9d1864315fBrian Paul
3719f3c29bd74f01370a6bbff1329966ca9d1864315fBrian Paul   if (dims >= 3) {
3720f3c29bd74f01370a6bbff1329966ca9d1864315fBrian Paul      if (((depth == 1 || depth == 2) &&
3721ca0d048e4e0cc5885cd309cdf9057cfe6044924eBrian Paul           depth != (GLsizei) texImage->Depth) ||
3722ca0d048e4e0cc5885cd309cdf9057cfe6044924eBrian Paul          (depth > (GLsizei) texImage->Depth)) {
3723f3c29bd74f01370a6bbff1329966ca9d1864315fBrian Paul         _mesa_error(ctx, GL_INVALID_VALUE,
3724f3c29bd74f01370a6bbff1329966ca9d1864315fBrian Paul                     "glCompressedTexSubImage%uD(depth=%d)", dims, depth);
3725f3c29bd74f01370a6bbff1329966ca9d1864315fBrian Paul         return GL_TRUE;
3726f3c29bd74f01370a6bbff1329966ca9d1864315fBrian Paul      }
3727f3c29bd74f01370a6bbff1329966ca9d1864315fBrian Paul   }
3728f3c29bd74f01370a6bbff1329966ca9d1864315fBrian Paul
3729f3c29bd74f01370a6bbff1329966ca9d1864315fBrian Paul   return GL_FALSE;
3730f3c29bd74f01370a6bbff1329966ca9d1864315fBrian Paul}
3731f3c29bd74f01370a6bbff1329966ca9d1864315fBrian Paul
3732f3c29bd74f01370a6bbff1329966ca9d1864315fBrian Paul
37334ff70b7a8f5f6383d8f320e68d798d0654799210Brian Paulvoid GLAPIENTRY
37344ff70b7a8f5f6383d8f320e68d798d0654799210Brian Paul_mesa_CompressedTexImage1DARB(GLenum target, GLint level,
37354ff70b7a8f5f6383d8f320e68d798d0654799210Brian Paul                              GLenum internalFormat, GLsizei width,
37364ff70b7a8f5f6383d8f320e68d798d0654799210Brian Paul                              GLint border, GLsizei imageSize,
37374ff70b7a8f5f6383d8f320e68d798d0654799210Brian Paul                              const GLvoid *data)
37384ff70b7a8f5f6383d8f320e68d798d0654799210Brian Paul{
37394ff70b7a8f5f6383d8f320e68d798d0654799210Brian Paul   GET_CURRENT_CONTEXT(ctx);
3740bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paul   teximage(ctx, GL_TRUE, 1, target, level, internalFormat,
3741bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paul            width, 1, 1, border, GL_NONE, GL_NONE, imageSize, data);
37424ff70b7a8f5f6383d8f320e68d798d0654799210Brian Paul}
37434ff70b7a8f5f6383d8f320e68d798d0654799210Brian Paul
37444ff70b7a8f5f6383d8f320e68d798d0654799210Brian Paul
37454ff70b7a8f5f6383d8f320e68d798d0654799210Brian Paulvoid GLAPIENTRY
37464ff70b7a8f5f6383d8f320e68d798d0654799210Brian Paul_mesa_CompressedTexImage2DARB(GLenum target, GLint level,
37474ff70b7a8f5f6383d8f320e68d798d0654799210Brian Paul                              GLenum internalFormat, GLsizei width,
37484ff70b7a8f5f6383d8f320e68d798d0654799210Brian Paul                              GLsizei height, GLint border, GLsizei imageSize,
37494ff70b7a8f5f6383d8f320e68d798d0654799210Brian Paul                              const GLvoid *data)
37504ff70b7a8f5f6383d8f320e68d798d0654799210Brian Paul{
37514ff70b7a8f5f6383d8f320e68d798d0654799210Brian Paul   GET_CURRENT_CONTEXT(ctx);
3752bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paul   teximage(ctx, GL_TRUE, 2, target, level, internalFormat,
3753bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paul            width, height, 1, border, GL_NONE, GL_NONE, imageSize, data);
37544ff70b7a8f5f6383d8f320e68d798d0654799210Brian Paul}
37554ff70b7a8f5f6383d8f320e68d798d0654799210Brian Paul
37564ff70b7a8f5f6383d8f320e68d798d0654799210Brian Paul
37574ff70b7a8f5f6383d8f320e68d798d0654799210Brian Paulvoid GLAPIENTRY
37584ff70b7a8f5f6383d8f320e68d798d0654799210Brian Paul_mesa_CompressedTexImage3DARB(GLenum target, GLint level,
37594ff70b7a8f5f6383d8f320e68d798d0654799210Brian Paul                              GLenum internalFormat, GLsizei width,
37604ff70b7a8f5f6383d8f320e68d798d0654799210Brian Paul                              GLsizei height, GLsizei depth, GLint border,
37614ff70b7a8f5f6383d8f320e68d798d0654799210Brian Paul                              GLsizei imageSize, const GLvoid *data)
37624ff70b7a8f5f6383d8f320e68d798d0654799210Brian Paul{
37634ff70b7a8f5f6383d8f320e68d798d0654799210Brian Paul   GET_CURRENT_CONTEXT(ctx);
3764bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paul   teximage(ctx, GL_TRUE, 3, target, level, internalFormat,
3765bdff1dfb398afdc187787b70bb62511ae1edd4eeBrian Paul            width, height, depth, border, GL_NONE, GL_NONE, imageSize, data);
37661207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul}
37671207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul
37681207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul
37696bc1e9fd6989483fbc1c94730a8014b4c62b242cBrian Paul/**
37706bc1e9fd6989483fbc1c94730a8014b4c62b242cBrian Paul * Common helper for glCompressedTexSubImage1/2/3D().
37716bc1e9fd6989483fbc1c94730a8014b4c62b242cBrian Paul */
37726bc1e9fd6989483fbc1c94730a8014b4c62b242cBrian Paulstatic void
37736bc1e9fd6989483fbc1c94730a8014b4c62b242cBrian Paulcompressed_tex_sub_image(GLuint dims, GLenum target, GLint level,
37746bc1e9fd6989483fbc1c94730a8014b4c62b242cBrian Paul                         GLint xoffset, GLint yoffset, GLint zoffset,
37756bc1e9fd6989483fbc1c94730a8014b4c62b242cBrian Paul                         GLsizei width, GLsizei height, GLsizei depth,
37766bc1e9fd6989483fbc1c94730a8014b4c62b242cBrian Paul                         GLenum format, GLsizei imageSize, const GLvoid *data)
37771207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul{
37789540a1dbcf213645b23c6cd9284e92a1152cca05Brian Paul   struct gl_texture_object *texObj;
37799540a1dbcf213645b23c6cd9284e92a1152cca05Brian Paul   struct gl_texture_image *texImage;
378089fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   GLenum error;
3781a1f1586bcb9fcd4a30930fd14b73323642ce85c5Brian Paul   GET_CURRENT_CONTEXT(ctx);
378275280a2c742ee241d392618a3b29548fce1520a9Brian Paul   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
378375280a2c742ee241d392618a3b29548fce1520a9Brian Paul
37846bc1e9fd6989483fbc1c94730a8014b4c62b242cBrian Paul   error = compressed_subtexture_error_check(ctx, dims, target, level,
3785c3c19be8e0d0b13916cc128cf3c8e839935c912aBrian Paul                                             xoffset, 0, 0, /* pos */
37866bc1e9fd6989483fbc1c94730a8014b4c62b242cBrian Paul                                             width, height, depth,   /* size */
3787c3c19be8e0d0b13916cc128cf3c8e839935c912aBrian Paul                                             format, imageSize);
378889fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   if (error) {
37896bc1e9fd6989483fbc1c94730a8014b4c62b242cBrian Paul      _mesa_error(ctx, error, "glCompressedTexSubImage%uD", dims);
379089fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      return;
37919540a1dbcf213645b23c6cd9284e92a1152cca05Brian Paul   }
37929540a1dbcf213645b23c6cd9284e92a1152cca05Brian Paul
3793b8fdb900fb9b1c8b1e9ec88509624237307a869aBrian Paul   texObj = _mesa_get_current_tex_object(ctx, target);
3794c352566933b932ef09cea3f144df21147a7f6bc1Brian Paul
37955ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell   _mesa_lock_texture(ctx, texObj);
37965ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell   {
37975ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell      texImage = _mesa_select_tex_image(ctx, texObj, target, level);
37989540a1dbcf213645b23c6cd9284e92a1152cca05Brian Paul
37996bc1e9fd6989483fbc1c94730a8014b4c62b242cBrian Paul      if (compressed_subtexture_error_check2(ctx, dims, width, height, depth,
3800f3c29bd74f01370a6bbff1329966ca9d1864315fBrian Paul                                             format, texImage)) {
3801f3c29bd74f01370a6bbff1329966ca9d1864315fBrian Paul         /* error was recorded */
3802c352566933b932ef09cea3f144df21147a7f6bc1Brian Paul      }
38036bc1e9fd6989483fbc1c94730a8014b4c62b242cBrian Paul      else if (width > 0 && height > 0 && depth > 0) {
3804ec19bdd16c3d4070af69fd865042babe0a627595Brian Paul         ctx->Driver.CompressedTexSubImage(ctx, dims, texImage,
3805ec19bdd16c3d4070af69fd865042babe0a627595Brian Paul                                           xoffset, yoffset, zoffset,
3806ec19bdd16c3d4070af69fd865042babe0a627595Brian Paul                                           width, height, depth,
3807ec19bdd16c3d4070af69fd865042babe0a627595Brian Paul                                           format, imageSize, data);
38082914f2b7fd9bd1082f8cce724d234208b900c3d4Brian Paul
38092914f2b7fd9bd1082f8cce724d234208b900c3d4Brian Paul         check_gen_mipmap(ctx, target, texObj, level);
38102914f2b7fd9bd1082f8cce724d234208b900c3d4Brian Paul
38112914f2b7fd9bd1082f8cce724d234208b900c3d4Brian Paul         ctx->NewState |= _NEW_TEXTURE;
38125ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell      }
38139540a1dbcf213645b23c6cd9284e92a1152cca05Brian Paul   }
38145ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell   _mesa_unlock_texture(ctx, texObj);
38151207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul}
38161207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul
38171207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul
3818c40d1dd62dd9bcbb97128e37a75d991a8d3b2d8cKendall Bennettvoid GLAPIENTRY
38196bc1e9fd6989483fbc1c94730a8014b4c62b242cBrian Paul_mesa_CompressedTexSubImage1DARB(GLenum target, GLint level, GLint xoffset,
38206bc1e9fd6989483fbc1c94730a8014b4c62b242cBrian Paul                                 GLsizei width, GLenum format,
38216bc1e9fd6989483fbc1c94730a8014b4c62b242cBrian Paul                                 GLsizei imageSize, const GLvoid *data)
38226bc1e9fd6989483fbc1c94730a8014b4c62b242cBrian Paul{
38236bc1e9fd6989483fbc1c94730a8014b4c62b242cBrian Paul   compressed_tex_sub_image(1, target, level, xoffset, 0, 0, width, 1, 1,
38246bc1e9fd6989483fbc1c94730a8014b4c62b242cBrian Paul                            format, imageSize, data);
38256bc1e9fd6989483fbc1c94730a8014b4c62b242cBrian Paul}
38266bc1e9fd6989483fbc1c94730a8014b4c62b242cBrian Paul
38276bc1e9fd6989483fbc1c94730a8014b4c62b242cBrian Paul
38286bc1e9fd6989483fbc1c94730a8014b4c62b242cBrian Paulvoid GLAPIENTRY
38291207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul_mesa_CompressedTexSubImage2DARB(GLenum target, GLint level, GLint xoffset,
38301207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul                                 GLint yoffset, GLsizei width, GLsizei height,
38311207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul                                 GLenum format, GLsizei imageSize,
38321207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul                                 const GLvoid *data)
38331207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul{
38346bc1e9fd6989483fbc1c94730a8014b4c62b242cBrian Paul   compressed_tex_sub_image(2, target, level, xoffset, yoffset, 0,
38356bc1e9fd6989483fbc1c94730a8014b4c62b242cBrian Paul                            width, height, 1, format, imageSize, data);
38361207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul}
38371207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul
38381207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul
3839c40d1dd62dd9bcbb97128e37a75d991a8d3b2d8cKendall Bennettvoid GLAPIENTRY
38401207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul_mesa_CompressedTexSubImage3DARB(GLenum target, GLint level, GLint xoffset,
38411207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul                                 GLint yoffset, GLint zoffset, GLsizei width,
38421207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul                                 GLsizei height, GLsizei depth, GLenum format,
38431207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul                                 GLsizei imageSize, const GLvoid *data)
38441207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul{
38456bc1e9fd6989483fbc1c94730a8014b4c62b242cBrian Paul   compressed_tex_sub_image(3, target, level, xoffset, yoffset, zoffset,
38466bc1e9fd6989483fbc1c94730a8014b4c62b242cBrian Paul                            width, height, depth, format, imageSize, data);
38471207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul}
3848874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul
3849664355f05b39303fa0995b72160c82f5bf50e95eEric Anholtstatic gl_format
3850664355f05b39303fa0995b72160c82f5bf50e95eEric Anholtget_texbuffer_format(const struct gl_context *ctx, GLenum internalFormat)
3851874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul{
3852874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul   switch (internalFormat) {
3853874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul   case GL_ALPHA8:
3854664355f05b39303fa0995b72160c82f5bf50e95eEric Anholt      return MESA_FORMAT_A8;
3855874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul   case GL_ALPHA16:
3856664355f05b39303fa0995b72160c82f5bf50e95eEric Anholt      return MESA_FORMAT_A16;
3857874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul   case GL_ALPHA16F_ARB:
3858664355f05b39303fa0995b72160c82f5bf50e95eEric Anholt      return MESA_FORMAT_ALPHA_FLOAT16;
3859874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul   case GL_ALPHA32F_ARB:
3860664355f05b39303fa0995b72160c82f5bf50e95eEric Anholt      return MESA_FORMAT_ALPHA_FLOAT32;
3861874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul   case GL_ALPHA8I_EXT:
3862664355f05b39303fa0995b72160c82f5bf50e95eEric Anholt      return MESA_FORMAT_ALPHA_INT8;
3863874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul   case GL_ALPHA16I_EXT:
3864664355f05b39303fa0995b72160c82f5bf50e95eEric Anholt      return MESA_FORMAT_ALPHA_INT16;
3865874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul   case GL_ALPHA32I_EXT:
3866664355f05b39303fa0995b72160c82f5bf50e95eEric Anholt      return MESA_FORMAT_ALPHA_INT32;
3867874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul   case GL_ALPHA8UI_EXT:
3868664355f05b39303fa0995b72160c82f5bf50e95eEric Anholt      return MESA_FORMAT_ALPHA_UINT8;
3869874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul   case GL_ALPHA16UI_EXT:
3870664355f05b39303fa0995b72160c82f5bf50e95eEric Anholt      return MESA_FORMAT_ALPHA_UINT16;
3871874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul   case GL_ALPHA32UI_EXT:
3872664355f05b39303fa0995b72160c82f5bf50e95eEric Anholt      return MESA_FORMAT_ALPHA_UINT32;
3873874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul   case GL_LUMINANCE8:
3874664355f05b39303fa0995b72160c82f5bf50e95eEric Anholt      return MESA_FORMAT_L8;
3875874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul   case GL_LUMINANCE16:
3876664355f05b39303fa0995b72160c82f5bf50e95eEric Anholt      return MESA_FORMAT_L16;
3877874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul   case GL_LUMINANCE16F_ARB:
3878664355f05b39303fa0995b72160c82f5bf50e95eEric Anholt      return MESA_FORMAT_LUMINANCE_FLOAT16;
3879874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul   case GL_LUMINANCE32F_ARB:
3880664355f05b39303fa0995b72160c82f5bf50e95eEric Anholt      return MESA_FORMAT_LUMINANCE_FLOAT32;
3881874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul   case GL_LUMINANCE8I_EXT:
3882664355f05b39303fa0995b72160c82f5bf50e95eEric Anholt      return MESA_FORMAT_LUMINANCE_INT8;
3883874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul   case GL_LUMINANCE16I_EXT:
3884664355f05b39303fa0995b72160c82f5bf50e95eEric Anholt      return MESA_FORMAT_LUMINANCE_INT16;
3885874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul   case GL_LUMINANCE32I_EXT:
3886664355f05b39303fa0995b72160c82f5bf50e95eEric Anholt      return MESA_FORMAT_LUMINANCE_INT32;
3887874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul   case GL_LUMINANCE8UI_EXT:
3888664355f05b39303fa0995b72160c82f5bf50e95eEric Anholt      return MESA_FORMAT_LUMINANCE_UINT8;
3889874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul   case GL_LUMINANCE16UI_EXT:
3890664355f05b39303fa0995b72160c82f5bf50e95eEric Anholt      return MESA_FORMAT_LUMINANCE_UINT16;
3891874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul   case GL_LUMINANCE32UI_EXT:
3892664355f05b39303fa0995b72160c82f5bf50e95eEric Anholt      return MESA_FORMAT_LUMINANCE_UINT32;
3893874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul   case GL_LUMINANCE8_ALPHA8:
3894664355f05b39303fa0995b72160c82f5bf50e95eEric Anholt      return MESA_FORMAT_AL88;
3895874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul   case GL_LUMINANCE16_ALPHA16:
3896664355f05b39303fa0995b72160c82f5bf50e95eEric Anholt      return MESA_FORMAT_AL1616;
3897874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul   case GL_LUMINANCE_ALPHA16F_ARB:
3898664355f05b39303fa0995b72160c82f5bf50e95eEric Anholt      return MESA_FORMAT_LUMINANCE_ALPHA_FLOAT16;
3899874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul   case GL_LUMINANCE_ALPHA32F_ARB:
3900664355f05b39303fa0995b72160c82f5bf50e95eEric Anholt      return MESA_FORMAT_LUMINANCE_ALPHA_FLOAT32;
3901874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul   case GL_LUMINANCE_ALPHA8I_EXT:
3902664355f05b39303fa0995b72160c82f5bf50e95eEric Anholt      return MESA_FORMAT_LUMINANCE_ALPHA_INT8;
3903874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul   case GL_LUMINANCE_ALPHA16I_EXT:
3904664355f05b39303fa0995b72160c82f5bf50e95eEric Anholt      return MESA_FORMAT_LUMINANCE_ALPHA_INT8;
3905874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul   case GL_LUMINANCE_ALPHA32I_EXT:
3906664355f05b39303fa0995b72160c82f5bf50e95eEric Anholt      return MESA_FORMAT_LUMINANCE_ALPHA_INT16;
3907874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul   case GL_LUMINANCE_ALPHA8UI_EXT:
3908664355f05b39303fa0995b72160c82f5bf50e95eEric Anholt      return MESA_FORMAT_LUMINANCE_ALPHA_UINT8;
3909874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul   case GL_LUMINANCE_ALPHA16UI_EXT:
3910664355f05b39303fa0995b72160c82f5bf50e95eEric Anholt      return MESA_FORMAT_LUMINANCE_ALPHA_UINT16;
3911874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul   case GL_LUMINANCE_ALPHA32UI_EXT:
3912664355f05b39303fa0995b72160c82f5bf50e95eEric Anholt      return MESA_FORMAT_LUMINANCE_ALPHA_UINT32;
3913874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul   case GL_INTENSITY8:
3914664355f05b39303fa0995b72160c82f5bf50e95eEric Anholt      return MESA_FORMAT_I8;
3915874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul   case GL_INTENSITY16:
3916664355f05b39303fa0995b72160c82f5bf50e95eEric Anholt      return MESA_FORMAT_I16;
3917874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul   case GL_INTENSITY16F_ARB:
3918664355f05b39303fa0995b72160c82f5bf50e95eEric Anholt      return MESA_FORMAT_INTENSITY_FLOAT16;
3919874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul   case GL_INTENSITY32F_ARB:
3920664355f05b39303fa0995b72160c82f5bf50e95eEric Anholt      return MESA_FORMAT_INTENSITY_FLOAT32;
3921874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul   case GL_INTENSITY8I_EXT:
3922664355f05b39303fa0995b72160c82f5bf50e95eEric Anholt      return MESA_FORMAT_INTENSITY_INT8;
3923874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul   case GL_INTENSITY16I_EXT:
3924664355f05b39303fa0995b72160c82f5bf50e95eEric Anholt      return MESA_FORMAT_INTENSITY_INT16;
3925874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul   case GL_INTENSITY32I_EXT:
3926664355f05b39303fa0995b72160c82f5bf50e95eEric Anholt      return MESA_FORMAT_INTENSITY_INT32;
3927874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul   case GL_INTENSITY8UI_EXT:
3928664355f05b39303fa0995b72160c82f5bf50e95eEric Anholt      return MESA_FORMAT_INTENSITY_UINT8;
3929874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul   case GL_INTENSITY16UI_EXT:
3930664355f05b39303fa0995b72160c82f5bf50e95eEric Anholt      return MESA_FORMAT_INTENSITY_UINT16;
3931874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul   case GL_INTENSITY32UI_EXT:
3932664355f05b39303fa0995b72160c82f5bf50e95eEric Anholt      return MESA_FORMAT_INTENSITY_UINT32;
3933874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul   case GL_RGBA8:
3934664355f05b39303fa0995b72160c82f5bf50e95eEric Anholt      return MESA_FORMAT_RGBA8888_REV;
3935874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul   case GL_RGBA16:
3936664355f05b39303fa0995b72160c82f5bf50e95eEric Anholt      return MESA_FORMAT_RGBA_16;
3937874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul   case GL_RGBA16F_ARB:
3938664355f05b39303fa0995b72160c82f5bf50e95eEric Anholt      return MESA_FORMAT_RGBA_FLOAT16;
3939874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul   case GL_RGBA32F_ARB:
3940664355f05b39303fa0995b72160c82f5bf50e95eEric Anholt      return MESA_FORMAT_RGBA_FLOAT32;
3941874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul   case GL_RGBA8I_EXT:
3942664355f05b39303fa0995b72160c82f5bf50e95eEric Anholt      return MESA_FORMAT_RGBA_INT8;
3943874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul   case GL_RGBA16I_EXT:
3944664355f05b39303fa0995b72160c82f5bf50e95eEric Anholt      return MESA_FORMAT_RGBA_INT16;
3945874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul   case GL_RGBA32I_EXT:
3946664355f05b39303fa0995b72160c82f5bf50e95eEric Anholt      return MESA_FORMAT_RGBA_INT32;
3947874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul   case GL_RGBA8UI_EXT:
3948664355f05b39303fa0995b72160c82f5bf50e95eEric Anholt      return MESA_FORMAT_RGBA_UINT8;
3949874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul   case GL_RGBA16UI_EXT:
3950664355f05b39303fa0995b72160c82f5bf50e95eEric Anholt      return MESA_FORMAT_RGBA_UINT16;
3951874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul   case GL_RGBA32UI_EXT:
3952664355f05b39303fa0995b72160c82f5bf50e95eEric Anholt      return MESA_FORMAT_RGBA_UINT32;
3953bf82ce3c66507e5e1722fccff0df47cf9dcdb2d4Eric Anholt
3954bf82ce3c66507e5e1722fccff0df47cf9dcdb2d4Eric Anholt   case GL_RG8:
3955bf82ce3c66507e5e1722fccff0df47cf9dcdb2d4Eric Anholt      return MESA_FORMAT_GR88;
3956bf82ce3c66507e5e1722fccff0df47cf9dcdb2d4Eric Anholt   case GL_RG16:
3957bf82ce3c66507e5e1722fccff0df47cf9dcdb2d4Eric Anholt      return MESA_FORMAT_RG1616;
3958bf82ce3c66507e5e1722fccff0df47cf9dcdb2d4Eric Anholt   case GL_RG16F:
3959bf82ce3c66507e5e1722fccff0df47cf9dcdb2d4Eric Anholt      return MESA_FORMAT_RG_FLOAT16;
3960bf82ce3c66507e5e1722fccff0df47cf9dcdb2d4Eric Anholt   case GL_RG32F:
3961bf82ce3c66507e5e1722fccff0df47cf9dcdb2d4Eric Anholt      return MESA_FORMAT_RG_FLOAT32;
3962bf82ce3c66507e5e1722fccff0df47cf9dcdb2d4Eric Anholt   case GL_RG8I:
3963bf82ce3c66507e5e1722fccff0df47cf9dcdb2d4Eric Anholt      return MESA_FORMAT_RG_INT8;
3964bf82ce3c66507e5e1722fccff0df47cf9dcdb2d4Eric Anholt   case GL_RG16I:
3965bf82ce3c66507e5e1722fccff0df47cf9dcdb2d4Eric Anholt      return MESA_FORMAT_RG_INT16;
3966bf82ce3c66507e5e1722fccff0df47cf9dcdb2d4Eric Anholt   case GL_RG32I:
3967bf82ce3c66507e5e1722fccff0df47cf9dcdb2d4Eric Anholt      return MESA_FORMAT_RG_INT32;
3968bf82ce3c66507e5e1722fccff0df47cf9dcdb2d4Eric Anholt   case GL_RG8UI:
3969bf82ce3c66507e5e1722fccff0df47cf9dcdb2d4Eric Anholt      return MESA_FORMAT_RG_UINT8;
3970bf82ce3c66507e5e1722fccff0df47cf9dcdb2d4Eric Anholt   case GL_RG16UI:
3971bf82ce3c66507e5e1722fccff0df47cf9dcdb2d4Eric Anholt      return MESA_FORMAT_RG_UINT16;
3972bf82ce3c66507e5e1722fccff0df47cf9dcdb2d4Eric Anholt   case GL_RG32UI:
3973bf82ce3c66507e5e1722fccff0df47cf9dcdb2d4Eric Anholt      return MESA_FORMAT_RG_UINT32;
3974bf82ce3c66507e5e1722fccff0df47cf9dcdb2d4Eric Anholt
3975bf82ce3c66507e5e1722fccff0df47cf9dcdb2d4Eric Anholt   case GL_R8:
3976bf82ce3c66507e5e1722fccff0df47cf9dcdb2d4Eric Anholt      return MESA_FORMAT_R8;
3977bf82ce3c66507e5e1722fccff0df47cf9dcdb2d4Eric Anholt   case GL_R16:
3978bf82ce3c66507e5e1722fccff0df47cf9dcdb2d4Eric Anholt      return MESA_FORMAT_R16;
3979bf82ce3c66507e5e1722fccff0df47cf9dcdb2d4Eric Anholt   case GL_R16F:
3980bf82ce3c66507e5e1722fccff0df47cf9dcdb2d4Eric Anholt      return MESA_FORMAT_R_FLOAT16;
3981bf82ce3c66507e5e1722fccff0df47cf9dcdb2d4Eric Anholt   case GL_R32F:
3982bf82ce3c66507e5e1722fccff0df47cf9dcdb2d4Eric Anholt      return MESA_FORMAT_R_FLOAT32;
3983bf82ce3c66507e5e1722fccff0df47cf9dcdb2d4Eric Anholt   case GL_R8I:
3984bf82ce3c66507e5e1722fccff0df47cf9dcdb2d4Eric Anholt      return MESA_FORMAT_R_INT8;
3985bf82ce3c66507e5e1722fccff0df47cf9dcdb2d4Eric Anholt   case GL_R16I:
3986bf82ce3c66507e5e1722fccff0df47cf9dcdb2d4Eric Anholt      return MESA_FORMAT_R_INT16;
3987bf82ce3c66507e5e1722fccff0df47cf9dcdb2d4Eric Anholt   case GL_R32I:
3988bf82ce3c66507e5e1722fccff0df47cf9dcdb2d4Eric Anholt      return MESA_FORMAT_R_INT32;
3989bf82ce3c66507e5e1722fccff0df47cf9dcdb2d4Eric Anholt   case GL_R8UI:
3990bf82ce3c66507e5e1722fccff0df47cf9dcdb2d4Eric Anholt      return MESA_FORMAT_R_UINT8;
3991bf82ce3c66507e5e1722fccff0df47cf9dcdb2d4Eric Anholt   case GL_R16UI:
3992bf82ce3c66507e5e1722fccff0df47cf9dcdb2d4Eric Anholt      return MESA_FORMAT_R_UINT16;
3993bf82ce3c66507e5e1722fccff0df47cf9dcdb2d4Eric Anholt   case GL_R32UI:
3994bf82ce3c66507e5e1722fccff0df47cf9dcdb2d4Eric Anholt      return MESA_FORMAT_R_UINT32;
3995bf82ce3c66507e5e1722fccff0df47cf9dcdb2d4Eric Anholt
3996874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul   default:
3997664355f05b39303fa0995b72160c82f5bf50e95eEric Anholt      return MESA_FORMAT_NONE;
3998874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul   }
3999664355f05b39303fa0995b72160c82f5bf50e95eEric Anholt}
4000874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul
4001664355f05b39303fa0995b72160c82f5bf50e95eEric Anholtstatic gl_format
4002664355f05b39303fa0995b72160c82f5bf50e95eEric Anholtvalidate_texbuffer_format(const struct gl_context *ctx, GLenum internalFormat)
4003664355f05b39303fa0995b72160c82f5bf50e95eEric Anholt{
4004664355f05b39303fa0995b72160c82f5bf50e95eEric Anholt   gl_format format = get_texbuffer_format(ctx, internalFormat);
4005664355f05b39303fa0995b72160c82f5bf50e95eEric Anholt   GLenum datatype;
4006874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul
4007664355f05b39303fa0995b72160c82f5bf50e95eEric Anholt   if (format == MESA_FORMAT_NONE)
4008664355f05b39303fa0995b72160c82f5bf50e95eEric Anholt      return MESA_FORMAT_NONE;
4009874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul
4010664355f05b39303fa0995b72160c82f5bf50e95eEric Anholt   datatype = _mesa_get_format_datatype(format);
4011664355f05b39303fa0995b72160c82f5bf50e95eEric Anholt   if (datatype == GL_FLOAT && !ctx->Extensions.ARB_texture_float)
4012664355f05b39303fa0995b72160c82f5bf50e95eEric Anholt      return MESA_FORMAT_NONE;
4013664355f05b39303fa0995b72160c82f5bf50e95eEric Anholt
4014664355f05b39303fa0995b72160c82f5bf50e95eEric Anholt   if (datatype == GL_HALF_FLOAT && !ctx->Extensions.ARB_half_float_pixel)
4015664355f05b39303fa0995b72160c82f5bf50e95eEric Anholt      return MESA_FORMAT_NONE;
4016664355f05b39303fa0995b72160c82f5bf50e95eEric Anholt
4017bf82ce3c66507e5e1722fccff0df47cf9dcdb2d4Eric Anholt   /* The GL_ARB_texture_rg and GL_ARB_texture_buffer_object specs don't make
4018bf82ce3c66507e5e1722fccff0df47cf9dcdb2d4Eric Anholt    * any mention of R/RG formats, but they appear in the GL 3.1 core
4019bf82ce3c66507e5e1722fccff0df47cf9dcdb2d4Eric Anholt    * specification.
4020bf82ce3c66507e5e1722fccff0df47cf9dcdb2d4Eric Anholt    */
40219c1b41879aab2ff7386c547a2ccce7686c018cf5Eric Anholt   if (ctx->Version <= 30) {
4022bf82ce3c66507e5e1722fccff0df47cf9dcdb2d4Eric Anholt      GLenum base_format = _mesa_get_format_base_format(format);
4023bf82ce3c66507e5e1722fccff0df47cf9dcdb2d4Eric Anholt
4024bf82ce3c66507e5e1722fccff0df47cf9dcdb2d4Eric Anholt      if (base_format == GL_R || base_format == GL_RG)
4025bf82ce3c66507e5e1722fccff0df47cf9dcdb2d4Eric Anholt	 return MESA_FORMAT_NONE;
4026bf82ce3c66507e5e1722fccff0df47cf9dcdb2d4Eric Anholt   }
4027664355f05b39303fa0995b72160c82f5bf50e95eEric Anholt   return format;
4028874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul}
4029874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul
4030874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul
4031874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul/** GL_ARB_texture_buffer_object */
4032874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paulvoid GLAPIENTRY
4033874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul_mesa_TexBuffer(GLenum target, GLenum internalFormat, GLuint buffer)
4034874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul{
4035874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul   struct gl_texture_object *texObj;
4036874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul   struct gl_buffer_object *bufObj;
4037664355f05b39303fa0995b72160c82f5bf50e95eEric Anholt   gl_format format;
4038874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul
4039874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul   GET_CURRENT_CONTEXT(ctx);
4040874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
4041874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul
4042bb4a39ec951a696efa07a120c9446b677703ababEric Anholt   if (!(ctx->Extensions.ARB_texture_buffer_object
4043bb4a39ec951a696efa07a120c9446b677703ababEric Anholt         && _mesa_is_desktop_gl(ctx))) {
4044874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul      _mesa_error(ctx, GL_INVALID_OPERATION, "glTexBuffer");
4045874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul      return;
4046874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul   }
4047874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul
4048874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul   if (target != GL_TEXTURE_BUFFER_ARB) {
4049874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul      _mesa_error(ctx, GL_INVALID_ENUM, "glTexBuffer(target)");
4050874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul      return;
4051874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul   }
4052874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul
4053664355f05b39303fa0995b72160c82f5bf50e95eEric Anholt   format = validate_texbuffer_format(ctx, internalFormat);
4054664355f05b39303fa0995b72160c82f5bf50e95eEric Anholt   if (format == MESA_FORMAT_NONE) {
4055874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul      _mesa_error(ctx, GL_INVALID_ENUM, "glTexBuffer(internalFormat 0x%x)",
4056874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul                  internalFormat);
4057874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul      return;
4058874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul   }
4059874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul
4060874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul   bufObj = _mesa_lookup_bufferobj(ctx, buffer);
4061874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul   if (buffer && !bufObj) {
4062874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul      _mesa_error(ctx, GL_INVALID_OPERATION, "glTexBuffer(buffer %u)", buffer);
4063874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul      return;
4064874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul   }
4065874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul
4066874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul   texObj = _mesa_get_current_tex_object(ctx, target);
4067874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul
4068874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul   _mesa_lock_texture(ctx, texObj);
4069874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul   {
4070874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul      _mesa_reference_buffer_object(ctx, &texObj->BufferObject, bufObj);
4071874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul      texObj->BufferObjectFormat = internalFormat;
4072664355f05b39303fa0995b72160c82f5bf50e95eEric Anholt      texObj->_BufferObjectFormat = format;
4073874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul   }
4074874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul   _mesa_unlock_texture(ctx, texObj);
4075874a2c0b7da62f4dd08dedcec221f55b22e40e95Brian Paul}
4076