teximage.c revision b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08
1afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg/*
2afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg * Mesa 3-D graphics library
34618a9bfc2cd57c47ddf3590782c751ec194d362Brian Paul * Version:  6.5
45e3bc0c2a2bcdf59949410f94c9b705fc1281ce8Jouk Jansen *
534bd1233a9874fe12a822c4fcb926d48456e1f29Brian Paul * Copyright (C) 1999-2006  Brian Paul   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
31add9f2168a5d6b15eb9955ee761246c4f4cf8458Brian Paul
32fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul#include "glheader.h"
33b46712ca9d379d9c091f5543500088d82cf9776cBrian Paul#include "bufferobj.h"
34afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg#include "context.h"
35f93b3dd69e744cf1dd6b102a11cdb07c2df4a967Brian Paul#include "convolve.h"
3634bd1233a9874fe12a822c4fcb926d48456e1f29Brian Paul#include "fbobject.h"
3734bd1233a9874fe12a822c4fcb926d48456e1f29Brian Paul#include "framebuffer.h"
38afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg#include "image.h"
393c63452e64df7e10aa073c6c3b9492b1d7dabbb8Brian Paul#include "imports.h"
40ebb248aa5c018dc676d389221d76ed329059789eBrian Paul#include "macros.h"
41fa4525e289b475b928a7b2c4055af9dd7fe46600Brian Paul#include "state.h"
4289fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul#include "texcompress.h"
432c3d34c905fa6b831a066afae83b938de05eb241Gareth Hughes#include "texformat.h"
44afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg#include "teximage.h"
45afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg#include "texstate.h"
463893e638e6521b9c070e01c0b31d22754ff97a88Brian Paul#include "texstore.h"
475e3bc0c2a2bcdf59949410f94c9b705fc1281ce8Jouk Jansen#include "mtypes.h"
48afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
49afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
504cf6718725c7cf3bfb728118a8b14f8cf206c701Brian Paul/**
514cf6718725c7cf3bfb728118a8b14f8cf206c701Brian Paul * We allocate texture memory on 512-byte boundaries so we can use MMX/SSE
524cf6718725c7cf3bfb728118a8b14f8cf206c701Brian Paul * elsewhere.
534cf6718725c7cf3bfb728118a8b14f8cf206c701Brian Paul */
544cf6718725c7cf3bfb728118a8b14f8cf206c701Brian Paulvoid *
554cf6718725c7cf3bfb728118a8b14f8cf206c701Brian Paul_mesa_alloc_texmemory(GLsizei bytes)
564cf6718725c7cf3bfb728118a8b14f8cf206c701Brian Paul{
574cf6718725c7cf3bfb728118a8b14f8cf206c701Brian Paul   return _mesa_align_malloc(bytes, 512);
584cf6718725c7cf3bfb728118a8b14f8cf206c701Brian Paul}
594cf6718725c7cf3bfb728118a8b14f8cf206c701Brian Paul
604cf6718725c7cf3bfb728118a8b14f8cf206c701Brian Paul
614cf6718725c7cf3bfb728118a8b14f8cf206c701Brian Paul/**
624cf6718725c7cf3bfb728118a8b14f8cf206c701Brian Paul * Free texture memory allocated with _mesa_alloc_texmemory()
634cf6718725c7cf3bfb728118a8b14f8cf206c701Brian Paul */
644cf6718725c7cf3bfb728118a8b14f8cf206c701Brian Paulvoid
654cf6718725c7cf3bfb728118a8b14f8cf206c701Brian Paul_mesa_free_texmemory(void *m)
664cf6718725c7cf3bfb728118a8b14f8cf206c701Brian Paul{
674cf6718725c7cf3bfb728118a8b14f8cf206c701Brian Paul   _mesa_align_free(m);
684cf6718725c7cf3bfb728118a8b14f8cf206c701Brian Paul}
694cf6718725c7cf3bfb728118a8b14f8cf206c701Brian Paul
704cf6718725c7cf3bfb728118a8b14f8cf206c701Brian Paul
714cf6718725c7cf3bfb728118a8b14f8cf206c701Brian Paul
724cf6718725c7cf3bfb728118a8b14f8cf206c701Brian Paul
734e9676fb13f60ecdbc247b120031f18cd3febcb0Brian Paul#if 0
744e9676fb13f60ecdbc247b120031f18cd3febcb0Brian Paulstatic void PrintTexture(GLcontext *ctx, const struct gl_texture_image *img)
754827179cc0d314f69671e97dcb016f5a3b90a213Brian Paul{
76f431a3fb4dc1bf860203d79e54657e3a62bc50dfBrian Paul#if CHAN_TYPE == GL_FLOAT
77f431a3fb4dc1bf860203d79e54657e3a62bc50dfBrian Paul   _mesa_problem(NULL, "PrintTexture doesn't support float channels");
78f431a3fb4dc1bf860203d79e54657e3a62bc50dfBrian Paul#else
79b51b0a847d7e7daaea69f77ab569086ef81c24a2Brian Paul   GLuint i, j, c;
80b51b0a847d7e7daaea69f77ab569086ef81c24a2Brian Paul   const GLchan *data = (const GLchan *) img->Data;
81b51b0a847d7e7daaea69f77ab569086ef81c24a2Brian Paul
82b51b0a847d7e7daaea69f77ab569086ef81c24a2Brian Paul   if (!data) {
833c63452e64df7e10aa073c6c3b9492b1d7dabbb8Brian Paul      _mesa_printf("No texture data\n");
84b51b0a847d7e7daaea69f77ab569086ef81c24a2Brian Paul      return;
85b51b0a847d7e7daaea69f77ab569086ef81c24a2Brian Paul   }
86b51b0a847d7e7daaea69f77ab569086ef81c24a2Brian Paul
87b51b0a847d7e7daaea69f77ab569086ef81c24a2Brian Paul   switch (img->Format) {
88b51b0a847d7e7daaea69f77ab569086ef81c24a2Brian Paul      case GL_ALPHA:
89b51b0a847d7e7daaea69f77ab569086ef81c24a2Brian Paul      case GL_LUMINANCE:
90b51b0a847d7e7daaea69f77ab569086ef81c24a2Brian Paul      case GL_INTENSITY:
91b51b0a847d7e7daaea69f77ab569086ef81c24a2Brian Paul      case GL_COLOR_INDEX:
92b51b0a847d7e7daaea69f77ab569086ef81c24a2Brian Paul         c = 1;
93b51b0a847d7e7daaea69f77ab569086ef81c24a2Brian Paul         break;
94b51b0a847d7e7daaea69f77ab569086ef81c24a2Brian Paul      case GL_LUMINANCE_ALPHA:
95b51b0a847d7e7daaea69f77ab569086ef81c24a2Brian Paul         c = 2;
96b51b0a847d7e7daaea69f77ab569086ef81c24a2Brian Paul         break;
97b51b0a847d7e7daaea69f77ab569086ef81c24a2Brian Paul      case GL_RGB:
98b51b0a847d7e7daaea69f77ab569086ef81c24a2Brian Paul         c = 3;
99b51b0a847d7e7daaea69f77ab569086ef81c24a2Brian Paul         break;
100b51b0a847d7e7daaea69f77ab569086ef81c24a2Brian Paul      case GL_RGBA:
101b51b0a847d7e7daaea69f77ab569086ef81c24a2Brian Paul         c = 4;
102b51b0a847d7e7daaea69f77ab569086ef81c24a2Brian Paul         break;
103b51b0a847d7e7daaea69f77ab569086ef81c24a2Brian Paul      default:
104b51b0a847d7e7daaea69f77ab569086ef81c24a2Brian Paul         _mesa_problem(NULL, "error in PrintTexture\n");
105b51b0a847d7e7daaea69f77ab569086ef81c24a2Brian Paul         return;
106b51b0a847d7e7daaea69f77ab569086ef81c24a2Brian Paul   }
107b51b0a847d7e7daaea69f77ab569086ef81c24a2Brian Paul
108b51b0a847d7e7daaea69f77ab569086ef81c24a2Brian Paul   for (i = 0; i < img->Height; i++) {
109b51b0a847d7e7daaea69f77ab569086ef81c24a2Brian Paul      for (j = 0; j < img->Width; j++) {
110b51b0a847d7e7daaea69f77ab569086ef81c24a2Brian Paul         if (c==1)
1113c63452e64df7e10aa073c6c3b9492b1d7dabbb8Brian Paul            _mesa_printf("%02x  ", data[0]);
112b51b0a847d7e7daaea69f77ab569086ef81c24a2Brian Paul         else if (c==2)
1133c63452e64df7e10aa073c6c3b9492b1d7dabbb8Brian Paul            _mesa_printf("%02x%02x  ", data[0], data[1]);
114b51b0a847d7e7daaea69f77ab569086ef81c24a2Brian Paul         else if (c==3)
1153c63452e64df7e10aa073c6c3b9492b1d7dabbb8Brian Paul            _mesa_printf("%02x%02x%02x  ", data[0], data[1], data[2]);
116b51b0a847d7e7daaea69f77ab569086ef81c24a2Brian Paul         else if (c==4)
1173c63452e64df7e10aa073c6c3b9492b1d7dabbb8Brian Paul            _mesa_printf("%02x%02x%02x%02x  ", data[0], data[1], data[2], data[3]);
118681b8c9d1ba06c8c82e687a5ced369b72e6b1eb9Brian Paul         data += (img->RowStride - img->Width) * c;
119b51b0a847d7e7daaea69f77ab569086ef81c24a2Brian Paul      }
120ea4fe661d7f3a95d9db17e1475076f1badf8e1a6Brian Paul      /* XXX use img->ImageStride here */
1213c63452e64df7e10aa073c6c3b9492b1d7dabbb8Brian Paul      _mesa_printf("\n");
122b51b0a847d7e7daaea69f77ab569086ef81c24a2Brian Paul   }
123f431a3fb4dc1bf860203d79e54657e3a62bc50dfBrian Paul#endif
1244827179cc0d314f69671e97dcb016f5a3b90a213Brian Paul}
1254827179cc0d314f69671e97dcb016f5a3b90a213Brian Paul#endif
1264827179cc0d314f69671e97dcb016f5a3b90a213Brian Paul
1274827179cc0d314f69671e97dcb016f5a3b90a213Brian Paul
128f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul/*
129887e2cf01a99f7fe1b7c94320b7bdbbf0d6ad2f8Brian Paul * Compute floor(log_base_2(n)).
130973da83f6237b5af4a9ee77f32fdfa5c04ecabc8Brian Paul * If n < 0 return -1.
131afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg */
132fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paulstatic int
133fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paullogbase2( int n )
134afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg{
135afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   GLint i = 1;
136afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   GLint log2 = 0;
137afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
138973da83f6237b5af4a9ee77f32fdfa5c04ecabc8Brian Paul   if (n < 0)
139afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      return -1;
140973da83f6237b5af4a9ee77f32fdfa5c04ecabc8Brian Paul
141973da83f6237b5af4a9ee77f32fdfa5c04ecabc8Brian Paul   if (n == 0)
142973da83f6237b5af4a9ee77f32fdfa5c04ecabc8Brian Paul      return 0;
143afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
144afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   while ( n > i ) {
145afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      i *= 2;
146afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      log2++;
147afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   }
148afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   if (i != n) {
149887e2cf01a99f7fe1b7c94320b7bdbbf0d6ad2f8Brian Paul      return log2 - 1;
150afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   }
151afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   else {
152afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      return log2;
153afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   }
154afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg}
155afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
156afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
157afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
1586dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/**
1591749a25ca889d514889b34cf6311c8014d97bf66Brian Paul * Return the simple base format for a given internal texture format.
1601749a25ca889d514889b34cf6311c8014d97bf66Brian Paul * For example, given GL_LUMINANCE12_ALPHA4, return GL_LUMINANCE_ALPHA.
1616dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell *
1626dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param ctx GL context.
1631749a25ca889d514889b34cf6311c8014d97bf66Brian Paul * \param internalFormat the internal texture format token or 1, 2, 3, or 4.
1646dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell *
1656dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \return the corresponding \u base internal format (GL_ALPHA, GL_LUMINANCE,
1666dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * GL_LUMANCE_ALPHA, GL_INTENSITY, GL_RGB, or GL_RGBA), or -1 if invalid enum.
16789fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul *
16889fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul * This is the format which is used during texture application (i.e. the
16989fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul * texture format and env mode determine the arithmetic used.
170afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg */
171b132e8da5e5f2b7da1f2141e0322e66bb0608e02Brian PaulGLint
1721749a25ca889d514889b34cf6311c8014d97bf66Brian Paul_mesa_base_tex_format( GLcontext *ctx, GLint internalFormat )
173afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg{
1741749a25ca889d514889b34cf6311c8014d97bf66Brian Paul   switch (internalFormat) {
175afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_ALPHA:
176afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_ALPHA4:
177afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_ALPHA8:
178afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_ALPHA12:
179afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_ALPHA16:
180afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         return GL_ALPHA;
181afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case 1:
182afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_LUMINANCE:
183afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_LUMINANCE4:
184afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_LUMINANCE8:
185afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_LUMINANCE12:
186afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_LUMINANCE16:
187afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         return GL_LUMINANCE;
188afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case 2:
189afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_LUMINANCE_ALPHA:
190afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_LUMINANCE4_ALPHA4:
191afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_LUMINANCE6_ALPHA2:
192afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_LUMINANCE8_ALPHA8:
193afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_LUMINANCE12_ALPHA4:
194afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_LUMINANCE12_ALPHA12:
195afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_LUMINANCE16_ALPHA16:
196afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         return GL_LUMINANCE_ALPHA;
197afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_INTENSITY:
198afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_INTENSITY4:
199afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_INTENSITY8:
200afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_INTENSITY12:
201afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_INTENSITY16:
202afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         return GL_INTENSITY;
203afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case 3:
204afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_RGB:
205afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_R3_G3_B2:
206afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_RGB4:
207afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_RGB5:
208afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_RGB8:
209afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_RGB10:
210afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_RGB12:
211afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_RGB16:
212afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         return GL_RGB;
213afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case 4:
214afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_RGBA:
215afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_RGBA2:
216afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_RGBA4:
217afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_RGB5_A1:
218afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_RGBA8:
219afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_RGB10_A2:
220afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_RGBA12:
221afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_RGBA16:
222afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         return GL_RGBA;
223f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      default:
224f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         ; /* fallthrough */
225f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   }
226f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
227f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   if (ctx->Extensions.EXT_paletted_texture) {
228f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      switch (internalFormat) {
229f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         case GL_COLOR_INDEX:
230f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         case GL_COLOR_INDEX1_EXT:
231f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         case GL_COLOR_INDEX2_EXT:
232f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         case GL_COLOR_INDEX4_EXT:
233f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         case GL_COLOR_INDEX8_EXT:
234f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         case GL_COLOR_INDEX12_EXT:
235f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         case GL_COLOR_INDEX16_EXT:
236e71654961868eac559210ced359c1af114138d8aBrian Paul            return GL_COLOR_INDEX;
237f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         default:
238f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            ; /* fallthrough */
239f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      }
240f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   }
241f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
2424618a9bfc2cd57c47ddf3590782c751ec194d362Brian Paul   if (ctx->Extensions.SGIX_depth_texture ||
2434618a9bfc2cd57c47ddf3590782c751ec194d362Brian Paul       ctx->Extensions.ARB_depth_texture) {
244f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      switch (internalFormat) {
245f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         case GL_DEPTH_COMPONENT:
246f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         case GL_DEPTH_COMPONENT16_SGIX:
247f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         case GL_DEPTH_COMPONENT24_SGIX:
248f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         case GL_DEPTH_COMPONENT32_SGIX:
249f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul            return GL_DEPTH_COMPONENT;
250f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         default:
251f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            ; /* fallthrough */
252f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      }
253f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   }
25489fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul
255f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   if (ctx->Extensions.ARB_texture_compression) {
256f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      switch (internalFormat) {
257f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         case GL_COMPRESSED_ALPHA:
25889fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul            return GL_ALPHA;
259f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         case GL_COMPRESSED_LUMINANCE:
26089fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul            return GL_LUMINANCE;
261f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         case GL_COMPRESSED_LUMINANCE_ALPHA:
26289fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul            return GL_LUMINANCE_ALPHA;
263f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         case GL_COMPRESSED_INTENSITY:
26489fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul            return GL_INTENSITY;
265f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         case GL_COMPRESSED_RGB:
26689fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul            return GL_RGB;
267f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         case GL_COMPRESSED_RGBA:
26889fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul            return GL_RGBA;
269f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         default:
270f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            ; /* fallthrough */
271f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      }
272f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   }
273f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
274f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   if (ctx->Extensions.TDFX_texture_compression_FXT1) {
275f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      switch (internalFormat) {
276f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         case GL_COMPRESSED_RGB_FXT1_3DFX:
27789fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul            return GL_RGB;
278f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         case GL_COMPRESSED_RGBA_FXT1_3DFX:
27989fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul            return GL_RGBA;
280f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         default:
281f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            ; /* fallthrough */
282f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      }
283f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   }
284f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
285f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   if (ctx->Extensions.EXT_texture_compression_s3tc) {
286f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      switch (internalFormat) {
2878f04c12e0ad876baa7eb9ed379e2b00150b376e0Brian Paul         case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
2888f04c12e0ad876baa7eb9ed379e2b00150b376e0Brian Paul            return GL_RGB;
289f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
290f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
291f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
29240bd9d0b190e11d39350d1b08d2c2b28e3040bcaDaniel Borca            return GL_RGBA;
293f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         default:
294f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            ; /* fallthrough */
295f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      }
296f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   }
297f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
298f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   if (ctx->Extensions.S3_s3tc) {
299f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      switch (internalFormat) {
300f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         case GL_RGB_S3TC:
301f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         case GL_RGB4_S3TC:
302663a9e1b7ef7b8384abe2f81e1a8749b942f6d3aDaniel Borca            return GL_RGB;
303f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         case GL_RGBA_S3TC:
304f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         case GL_RGBA4_S3TC:
305663a9e1b7ef7b8384abe2f81e1a8749b942f6d3aDaniel Borca            return GL_RGBA;
306f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         default:
307f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            ; /* fallthrough */
308f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      }
309f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   }
31089fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul
311f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   if (ctx->Extensions.MESA_ycbcr_texture) {
312f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      if (internalFormat == GL_YCBCR_MESA)
313f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         return GL_YCBCR_MESA;
314f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   }
3151749a25ca889d514889b34cf6311c8014d97bf66Brian Paul
316f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   if (ctx->Extensions.ARB_texture_float) {
317f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      switch (internalFormat) {
318f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         case GL_ALPHA16F_ARB:
319f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         case GL_ALPHA32F_ARB:
320f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            return GL_ALPHA;
321f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         case GL_RGBA16F_ARB:
322f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         case GL_RGBA32F_ARB:
323f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            return GL_RGBA;
324f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         case GL_RGB16F_ARB:
325f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         case GL_RGB32F_ARB:
326f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            return GL_RGB;
327f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         case GL_INTENSITY16F_ARB:
328f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         case GL_INTENSITY32F_ARB:
329f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            return GL_INTENSITY;
330f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         case GL_LUMINANCE16F_ARB:
331f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         case GL_LUMINANCE32F_ARB:
332f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            return GL_LUMINANCE;
333f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         case GL_LUMINANCE_ALPHA16F_ARB:
334f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         case GL_LUMINANCE_ALPHA32F_ARB:
335f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            return GL_LUMINANCE_ALPHA;
336f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         default:
3378f04c12e0ad876baa7eb9ed379e2b00150b376e0Brian Paul            ; /* fallthrough */
338f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      }
339afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   }
340f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
3411ad7b99925e044f82e635f746c1ef2df77f69ac9Brian Paul   if (ctx->Extensions.EXT_packed_depth_stencil) {
3421ad7b99925e044f82e635f746c1ef2df77f69ac9Brian Paul      switch (internalFormat) {
3431ad7b99925e044f82e635f746c1ef2df77f69ac9Brian Paul         case GL_DEPTH_STENCIL_EXT:
3441ad7b99925e044f82e635f746c1ef2df77f69ac9Brian Paul         case GL_DEPTH24_STENCIL8_EXT:
3451ad7b99925e044f82e635f746c1ef2df77f69ac9Brian Paul            return GL_DEPTH_STENCIL_EXT;
3461ad7b99925e044f82e635f746c1ef2df77f69ac9Brian Paul         default:
3471ad7b99925e044f82e635f746c1ef2df77f69ac9Brian Paul            ; /* fallthrough */
3481ad7b99925e044f82e635f746c1ef2df77f69ac9Brian Paul      }
3491ad7b99925e044f82e635f746c1ef2df77f69ac9Brian Paul   }
3501ad7b99925e044f82e635f746c1ef2df77f69ac9Brian Paul
351f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   return -1; /* error */
352afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg}
353afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
354afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
3556dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/**
3563405708e21ae36ce3ea92bbaea900bacc70a0034Brian Paul * Test if the given image format is a color/RGBA format (i.e., not color
3573405708e21ae36ce3ea92bbaea900bacc70a0034Brian Paul * index, depth, stencil, etc).
3583405708e21ae36ce3ea92bbaea900bacc70a0034Brian Paul * \param format  the image format value (may by an internal texture format)
3591749a25ca889d514889b34cf6311c8014d97bf66Brian Paul * \return GL_TRUE if its a color/RGBA format, GL_FALSE otherwise.
360f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul */
361f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paulstatic GLboolean
3623405708e21ae36ce3ea92bbaea900bacc70a0034Brian Paulis_color_format(GLenum format)
363f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul{
3643405708e21ae36ce3ea92bbaea900bacc70a0034Brian Paul   switch (format) {
365ebe8d3152d864b9f4a0501c868c54e472c9d67a4Brian Paul      case GL_RED:
366ebe8d3152d864b9f4a0501c868c54e472c9d67a4Brian Paul      case GL_GREEN:
367ebe8d3152d864b9f4a0501c868c54e472c9d67a4Brian Paul      case GL_BLUE:
368f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul      case GL_ALPHA:
369f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul      case GL_ALPHA4:
370f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul      case GL_ALPHA8:
371f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul      case GL_ALPHA12:
372f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul      case GL_ALPHA16:
373f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul      case 1:
374f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul      case GL_LUMINANCE:
375f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul      case GL_LUMINANCE4:
376f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul      case GL_LUMINANCE8:
377f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul      case GL_LUMINANCE12:
378f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul      case GL_LUMINANCE16:
379f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul      case 2:
380f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul      case GL_LUMINANCE_ALPHA:
381f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul      case GL_LUMINANCE4_ALPHA4:
382f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul      case GL_LUMINANCE6_ALPHA2:
383f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul      case GL_LUMINANCE8_ALPHA8:
384f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul      case GL_LUMINANCE12_ALPHA4:
385f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul      case GL_LUMINANCE12_ALPHA12:
386f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul      case GL_LUMINANCE16_ALPHA16:
387f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul      case GL_INTENSITY:
388f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul      case GL_INTENSITY4:
389f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul      case GL_INTENSITY8:
390f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul      case GL_INTENSITY12:
391f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul      case GL_INTENSITY16:
392f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul      case 3:
393f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul      case GL_RGB:
394beef59e73aad91ca52d72fd0224e696826038318Brian Paul      case GL_BGR:
395f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul      case GL_R3_G3_B2:
396f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul      case GL_RGB4:
397f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul      case GL_RGB5:
398f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul      case GL_RGB8:
399f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul      case GL_RGB10:
400f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul      case GL_RGB12:
401f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul      case GL_RGB16:
402f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul      case 4:
403f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul      case GL_ABGR_EXT:
404f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul      case GL_RGBA:
405beef59e73aad91ca52d72fd0224e696826038318Brian Paul      case GL_BGRA:
406f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul      case GL_RGBA2:
407f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul      case GL_RGBA4:
408f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul      case GL_RGB5_A1:
409f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul      case GL_RGBA8:
410f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul      case GL_RGB10_A2:
411f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul      case GL_RGBA12:
412f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul      case GL_RGBA16:
413f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      /* float texture formats */
414f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      case GL_ALPHA16F_ARB:
415f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      case GL_ALPHA32F_ARB:
416f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      case GL_LUMINANCE16F_ARB:
417f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      case GL_LUMINANCE32F_ARB:
418f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      case GL_LUMINANCE_ALPHA16F_ARB:
419f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      case GL_LUMINANCE_ALPHA32F_ARB:
420f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      case GL_INTENSITY16F_ARB:
421f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      case GL_INTENSITY32F_ARB:
422f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      case GL_RGB16F_ARB:
423f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      case GL_RGB32F_ARB:
424f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      case GL_RGBA16F_ARB:
425f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      case GL_RGBA32F_ARB:
4263405708e21ae36ce3ea92bbaea900bacc70a0034Brian Paul      /* compressed formats */
4273405708e21ae36ce3ea92bbaea900bacc70a0034Brian Paul      case GL_COMPRESSED_ALPHA:
4283405708e21ae36ce3ea92bbaea900bacc70a0034Brian Paul      case GL_COMPRESSED_LUMINANCE:
4293405708e21ae36ce3ea92bbaea900bacc70a0034Brian Paul      case GL_COMPRESSED_LUMINANCE_ALPHA:
4303405708e21ae36ce3ea92bbaea900bacc70a0034Brian Paul      case GL_COMPRESSED_INTENSITY:
4313405708e21ae36ce3ea92bbaea900bacc70a0034Brian Paul      case GL_COMPRESSED_RGB:
4323405708e21ae36ce3ea92bbaea900bacc70a0034Brian Paul      case GL_COMPRESSED_RGBA:
433997e81fe75ce32ed3d8bb4d8f73a226644cf3a2fRoland Scheidegger      case GL_RGB_S3TC:
434997e81fe75ce32ed3d8bb4d8f73a226644cf3a2fRoland Scheidegger      case GL_RGB4_S3TC:
435997e81fe75ce32ed3d8bb4d8f73a226644cf3a2fRoland Scheidegger      case GL_RGBA_S3TC:
436997e81fe75ce32ed3d8bb4d8f73a226644cf3a2fRoland Scheidegger      case GL_RGBA4_S3TC:
4373405708e21ae36ce3ea92bbaea900bacc70a0034Brian Paul      case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
4383405708e21ae36ce3ea92bbaea900bacc70a0034Brian Paul      case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
4393405708e21ae36ce3ea92bbaea900bacc70a0034Brian Paul      case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
4403405708e21ae36ce3ea92bbaea900bacc70a0034Brian Paul      case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
4413405708e21ae36ce3ea92bbaea900bacc70a0034Brian Paul      case GL_COMPRESSED_RGB_FXT1_3DFX:
4423405708e21ae36ce3ea92bbaea900bacc70a0034Brian Paul      case GL_COMPRESSED_RGBA_FXT1_3DFX:
443f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul         return GL_TRUE;
444c5b995066020191982b2315fc45d05e068eee761Brian Paul      case GL_YCBCR_MESA:  /* not considered to be RGB */
445f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul      default:
446f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul         return GL_FALSE;
447f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul   }
448f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul}
449f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul
450f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul
4516dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/**
4523405708e21ae36ce3ea92bbaea900bacc70a0034Brian Paul * Test if the given image format is a color index format.
4536dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell */
454f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paulstatic GLboolean
4553405708e21ae36ce3ea92bbaea900bacc70a0034Brian Paulis_index_format(GLenum format)
456f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul{
4573405708e21ae36ce3ea92bbaea900bacc70a0034Brian Paul   switch (format) {
458f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul      case GL_COLOR_INDEX:
459f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul      case GL_COLOR_INDEX1_EXT:
460f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul      case GL_COLOR_INDEX2_EXT:
461f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul      case GL_COLOR_INDEX4_EXT:
462f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul      case GL_COLOR_INDEX8_EXT:
463f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul      case GL_COLOR_INDEX12_EXT:
464f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul      case GL_COLOR_INDEX16_EXT:
465f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul         return GL_TRUE;
466f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul      default:
467f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul         return GL_FALSE;
468f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul   }
469f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul}
470f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul
471afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
47289fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul/**
4733405708e21ae36ce3ea92bbaea900bacc70a0034Brian Paul * Test if the given image format is a depth component format.
4741749a25ca889d514889b34cf6311c8014d97bf66Brian Paul */
4751749a25ca889d514889b34cf6311c8014d97bf66Brian Paulstatic GLboolean
4763405708e21ae36ce3ea92bbaea900bacc70a0034Brian Paulis_depth_format(GLenum format)
4771749a25ca889d514889b34cf6311c8014d97bf66Brian Paul{
4783405708e21ae36ce3ea92bbaea900bacc70a0034Brian Paul   switch (format) {
4791749a25ca889d514889b34cf6311c8014d97bf66Brian Paul      case GL_DEPTH_COMPONENT16_ARB:
4801749a25ca889d514889b34cf6311c8014d97bf66Brian Paul      case GL_DEPTH_COMPONENT24_ARB:
4811749a25ca889d514889b34cf6311c8014d97bf66Brian Paul      case GL_DEPTH_COMPONENT32_ARB:
4821749a25ca889d514889b34cf6311c8014d97bf66Brian Paul      case GL_DEPTH_COMPONENT:
4831749a25ca889d514889b34cf6311c8014d97bf66Brian Paul         return GL_TRUE;
4841749a25ca889d514889b34cf6311c8014d97bf66Brian Paul      default:
4851749a25ca889d514889b34cf6311c8014d97bf66Brian Paul         return GL_FALSE;
4861749a25ca889d514889b34cf6311c8014d97bf66Brian Paul   }
4871749a25ca889d514889b34cf6311c8014d97bf66Brian Paul}
4881749a25ca889d514889b34cf6311c8014d97bf66Brian Paul
4891749a25ca889d514889b34cf6311c8014d97bf66Brian Paul
4901749a25ca889d514889b34cf6311c8014d97bf66Brian Paul/**
4913405708e21ae36ce3ea92bbaea900bacc70a0034Brian Paul * Test if the given image format is a YCbCr format.
4921749a25ca889d514889b34cf6311c8014d97bf66Brian Paul */
4931749a25ca889d514889b34cf6311c8014d97bf66Brian Paulstatic GLboolean
4943405708e21ae36ce3ea92bbaea900bacc70a0034Brian Paulis_ycbcr_format(GLenum format)
4951749a25ca889d514889b34cf6311c8014d97bf66Brian Paul{
4963405708e21ae36ce3ea92bbaea900bacc70a0034Brian Paul   switch (format) {
4971749a25ca889d514889b34cf6311c8014d97bf66Brian Paul      case GL_YCBCR_MESA:
4981749a25ca889d514889b34cf6311c8014d97bf66Brian Paul         return GL_TRUE;
4991749a25ca889d514889b34cf6311c8014d97bf66Brian Paul      default:
5001749a25ca889d514889b34cf6311c8014d97bf66Brian Paul         return GL_FALSE;
5011749a25ca889d514889b34cf6311c8014d97bf66Brian Paul   }
5021749a25ca889d514889b34cf6311c8014d97bf66Brian Paul}
5031749a25ca889d514889b34cf6311c8014d97bf66Brian Paul
5041749a25ca889d514889b34cf6311c8014d97bf66Brian Paul
5051749a25ca889d514889b34cf6311c8014d97bf66Brian Paul/**
5061ad7b99925e044f82e635f746c1ef2df77f69ac9Brian Paul * Test if the given image format is a Depth/Stencil format.
5071ad7b99925e044f82e635f746c1ef2df77f69ac9Brian Paul */
5081ad7b99925e044f82e635f746c1ef2df77f69ac9Brian Paulstatic GLboolean
5091ad7b99925e044f82e635f746c1ef2df77f69ac9Brian Paulis_depthstencil_format(GLenum format)
5101ad7b99925e044f82e635f746c1ef2df77f69ac9Brian Paul{
5111ad7b99925e044f82e635f746c1ef2df77f69ac9Brian Paul   switch (format) {
5121ad7b99925e044f82e635f746c1ef2df77f69ac9Brian Paul      case GL_DEPTH24_STENCIL8_EXT:
5131ad7b99925e044f82e635f746c1ef2df77f69ac9Brian Paul      case GL_DEPTH_STENCIL_EXT:
5141ad7b99925e044f82e635f746c1ef2df77f69ac9Brian Paul         return GL_TRUE;
5151ad7b99925e044f82e635f746c1ef2df77f69ac9Brian Paul      default:
5161ad7b99925e044f82e635f746c1ef2df77f69ac9Brian Paul         return GL_FALSE;
5171ad7b99925e044f82e635f746c1ef2df77f69ac9Brian Paul   }
5181ad7b99925e044f82e635f746c1ef2df77f69ac9Brian Paul}
5191ad7b99925e044f82e635f746c1ef2df77f69ac9Brian Paul
5201ad7b99925e044f82e635f746c1ef2df77f69ac9Brian Paul
5211ad7b99925e044f82e635f746c1ef2df77f69ac9Brian Paul
5221ad7b99925e044f82e635f746c1ef2df77f69ac9Brian Paul/**
5236dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Test if it is a supported compressed format.
5246dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell *
5256dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param internalFormat the internal format token provided by the user.
5266dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell *
5276dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \ret GL_TRUE if \p internalFormat is a supported compressed format, or
5286dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * GL_FALSE otherwise.
5296dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell *
5306dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Currently only GL_COMPRESSED_RGB_FXT1_3DFX and GL_COMPRESSED_RGBA_FXT1_3DFX
5316dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * are supported.
532aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul */
533aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paulstatic GLboolean
53440bd9d0b190e11d39350d1b08d2c2b28e3040bcaDaniel Borcais_compressed_format(GLcontext *ctx, GLenum internalFormat)
535aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul{
536c3c19be8e0d0b13916cc128cf3c8e839935c912aBrian Paul   GLint supported[100]; /* 100 should be plenty */
537c3c19be8e0d0b13916cc128cf3c8e839935c912aBrian Paul   GLuint i, n;
538c3c19be8e0d0b13916cc128cf3c8e839935c912aBrian Paul
539008ed1df83735c72c2731c509a3665938d3c7209Brian Paul   n = _mesa_get_compressed_formats(ctx, supported, GL_TRUE);
540c3c19be8e0d0b13916cc128cf3c8e839935c912aBrian Paul   ASSERT(n < 100);
541c3c19be8e0d0b13916cc128cf3c8e839935c912aBrian Paul   for (i = 0; i < n; i++) {
542c3c19be8e0d0b13916cc128cf3c8e839935c912aBrian Paul      if ((GLint) internalFormat == supported[i]) {
543e4276667dafc8de0c6e64af8300fc7598437de6eBrian Paul         return GL_TRUE;
544c3c19be8e0d0b13916cc128cf3c8e839935c912aBrian Paul      }
545e4276667dafc8de0c6e64af8300fc7598437de6eBrian Paul   }
546c3c19be8e0d0b13916cc128cf3c8e839935c912aBrian Paul   return GL_FALSE;
547aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul}
548aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul
549aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul
550519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paulstatic GLuint
551519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paultexture_face(GLenum target)
552519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul{
553519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul   if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
554519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul       target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB)
555519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul      return (GLuint) target - (GLuint) GL_TEXTURE_CUBE_MAP_POSITIVE_X;
556519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul   else
557519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul      return 0;
558519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul}
559519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul
560519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul
561519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul
5626dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/**
5638e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul * Store a gl_texture_image pointer in a gl_texture_object structure
5648e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul * according to the target and level parameters.
5656dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell *
5666dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param tObj texture object.
5676dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param target texture target.
5686dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param level image level.
5696dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param texImage texture image.
5706dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell *
5718e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul * This was basically prompted by the introduction of cube maps.
572afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg */
5733893e638e6521b9c070e01c0b31d22754ff97a88Brian Paulvoid
5743893e638e6521b9c070e01c0b31d22754ff97a88Brian Paul_mesa_set_tex_image(struct gl_texture_object *tObj,
5753893e638e6521b9c070e01c0b31d22754ff97a88Brian Paul                    GLenum target, GLint level,
5763893e638e6521b9c070e01c0b31d22754ff97a88Brian Paul                    struct gl_texture_image *texImage)
577fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul{
578fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul   ASSERT(tObj);
579fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul   ASSERT(texImage);
580fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul   switch (target) {
5819228e17bb5bf219269daeed5cbfdd912c118e926Brian Paul      case GL_TEXTURE_1D:
582fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul      case GL_TEXTURE_2D:
5839228e17bb5bf219269daeed5cbfdd912c118e926Brian Paul      case GL_TEXTURE_3D:
58418fa367ac6e035341f5eb86ecc4231124b2921e3Keith Whitwell         tObj->Image[0][level] = texImage;
585a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul         break;
586fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul      case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
587fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul      case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
588fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul      case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
589fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul      case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
590fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul      case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
5910d22b9054094c863062c7787b45e1fb4d3834a67Brian Paul      case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
5920d22b9054094c863062c7787b45e1fb4d3834a67Brian Paul         {
5930d22b9054094c863062c7787b45e1fb4d3834a67Brian Paul            GLuint face = ((GLuint) target -
5940d22b9054094c863062c7787b45e1fb4d3834a67Brian Paul                           (GLuint) GL_TEXTURE_CUBE_MAP_POSITIVE_X);
5950d22b9054094c863062c7787b45e1fb4d3834a67Brian Paul            tObj->Image[face][level] = texImage;
5960d22b9054094c863062c7787b45e1fb4d3834a67Brian Paul         }
5970d22b9054094c863062c7787b45e1fb4d3834a67Brian Paul         break;
5988afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul      case GL_TEXTURE_RECTANGLE_NV:
5998afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul         ASSERT(level == 0);
60018fa367ac6e035341f5eb86ecc4231124b2921e3Keith Whitwell         tObj->Image[0][level] = texImage;
601a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul         break;
602fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul      default:
6033893e638e6521b9c070e01c0b31d22754ff97a88Brian Paul         _mesa_problem(NULL, "bad target in _mesa_set_tex_image()");
604fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul         return;
605fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul   }
606a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul   /* Set the 'back' pointer */
607a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul   texImage->TexObject = tObj;
608fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul}
609fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul
610fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul
611a3f137094cd965d27e1b088499dd609b81a91906Brian Paul/**
6126dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Allocate a texture image structure.
6136dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell *
614a3f137094cd965d27e1b088499dd609b81a91906Brian Paul * Called via ctx->Driver.NewTextureImage() unless overriden by a device
615a3f137094cd965d27e1b088499dd609b81a91906Brian Paul * driver.
6166dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell *
6176dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \return a pointer to gl_texture_image struct with all fields initialized to
6186dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * zero.
61977ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paul */
62077ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paulstruct gl_texture_image *
621a3f137094cd965d27e1b088499dd609b81a91906Brian Paul_mesa_new_texture_image( GLcontext *ctx )
62277ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paul{
623a3f137094cd965d27e1b088499dd609b81a91906Brian Paul   (void) ctx;
62477ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paul   return CALLOC_STRUCT(gl_texture_image);
62577ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paul}
62677ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paul
62777ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paul
628a3f137094cd965d27e1b088499dd609b81a91906Brian Paul/**
6293e62d3a8d88b48d4ed19e00ea2bbc3d0a2b6acf7Keith Whitwell * Free texture image data.
6304cf6718725c7cf3bfb728118a8b14f8cf206c701Brian Paul * This function is a fallback called via ctx->Driver.FreeTexImageData().
6313e62d3a8d88b48d4ed19e00ea2bbc3d0a2b6acf7Keith Whitwell *
6323e62d3a8d88b48d4ed19e00ea2bbc3d0a2b6acf7Keith Whitwell * \param teximage texture image.
6333e62d3a8d88b48d4ed19e00ea2bbc3d0a2b6acf7Keith Whitwell *
6343e62d3a8d88b48d4ed19e00ea2bbc3d0a2b6acf7Keith Whitwell * Free the texture image data if it's not marked as client data.
6353e62d3a8d88b48d4ed19e00ea2bbc3d0a2b6acf7Keith Whitwell */
6363e62d3a8d88b48d4ed19e00ea2bbc3d0a2b6acf7Keith Whitwellvoid
6374cf6718725c7cf3bfb728118a8b14f8cf206c701Brian Paul_mesa_free_texture_image_data(GLcontext *ctx,
6384cf6718725c7cf3bfb728118a8b14f8cf206c701Brian Paul                              struct gl_texture_image *texImage)
6393e62d3a8d88b48d4ed19e00ea2bbc3d0a2b6acf7Keith Whitwell{
640c3c19be8e0d0b13916cc128cf3c8e839935c912aBrian Paul   (void) ctx;
641c3c19be8e0d0b13916cc128cf3c8e839935c912aBrian Paul
6423e62d3a8d88b48d4ed19e00ea2bbc3d0a2b6acf7Keith Whitwell   if (texImage->Data && !texImage->IsClientData) {
6433e62d3a8d88b48d4ed19e00ea2bbc3d0a2b6acf7Keith Whitwell      /* free the old texture data */
6444cf6718725c7cf3bfb728118a8b14f8cf206c701Brian Paul      _mesa_free_texmemory(texImage->Data);
6453e62d3a8d88b48d4ed19e00ea2bbc3d0a2b6acf7Keith Whitwell   }
6463e62d3a8d88b48d4ed19e00ea2bbc3d0a2b6acf7Keith Whitwell
6473e62d3a8d88b48d4ed19e00ea2bbc3d0a2b6acf7Keith Whitwell   texImage->Data = NULL;
6483e62d3a8d88b48d4ed19e00ea2bbc3d0a2b6acf7Keith Whitwell}
6493e62d3a8d88b48d4ed19e00ea2bbc3d0a2b6acf7Keith Whitwell
6503e62d3a8d88b48d4ed19e00ea2bbc3d0a2b6acf7Keith Whitwell
6513e62d3a8d88b48d4ed19e00ea2bbc3d0a2b6acf7Keith Whitwell/**
6526dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Free texture image.
6536dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell *
6546dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param teximage texture image.
6556dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell *
6563e62d3a8d88b48d4ed19e00ea2bbc3d0a2b6acf7Keith Whitwell * Free the texture image structure and the associated image data.
657a3f137094cd965d27e1b088499dd609b81a91906Brian Paul */
65877ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paulvoid
6593e62d3a8d88b48d4ed19e00ea2bbc3d0a2b6acf7Keith Whitwell_mesa_delete_texture_image( GLcontext *ctx, struct gl_texture_image *texImage )
66077ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paul{
6613e62d3a8d88b48d4ed19e00ea2bbc3d0a2b6acf7Keith Whitwell   if (texImage->Data) {
6623e62d3a8d88b48d4ed19e00ea2bbc3d0a2b6acf7Keith Whitwell      ctx->Driver.FreeTexImageData( ctx, texImage );
66377ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paul   }
6643e62d3a8d88b48d4ed19e00ea2bbc3d0a2b6acf7Keith Whitwell   ASSERT(texImage->Data == NULL);
665b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul   if (texImage->ImageOffsets)
666b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul      _mesa_free(texImage->ImageOffsets);
667b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul   _mesa_free(texImage);
66877ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paul}
66977ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paul
67077ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paul
6716dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/**
6726dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Test if a target is a proxy target.
6736dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell *
6746dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param target texture target.
6756dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell *
6766dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \return GL_TRUE if the target is a proxy target, GL_FALSE otherwise.
677aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul */
67877ee31930a1b0cc7766939415f4f04ed6a1fa4acBrian PaulGLboolean
67977ee31930a1b0cc7766939415f4f04ed6a1fa4acBrian Paul_mesa_is_proxy_texture(GLenum target)
680aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul{
6818e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul   return (target == GL_PROXY_TEXTURE_1D ||
6828e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul           target == GL_PROXY_TEXTURE_2D ||
6838e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul           target == GL_PROXY_TEXTURE_3D ||
68427f4484fb73ac7bf4f790ca2d3efd50b6bea25c3Brian Paul           target == GL_PROXY_TEXTURE_CUBE_MAP_ARB ||
68527f4484fb73ac7bf4f790ca2d3efd50b6bea25c3Brian Paul           target == GL_PROXY_TEXTURE_RECTANGLE_NV);
686aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul}
687aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul
688aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul
6896dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/**
6906dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Get the texture object that corresponds to the target of the given texture unit.
6916dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell *
6926dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param ctx GL context.
6936dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param texUnit texture unit.
6946dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param target texture target.
6956dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell *
6966dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \return pointer to the texture object on success, or NULL on failure.
6976dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell *
6986dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \sa gl_texture_unit.
69935d5301a54153930ee6fd60dff1010ce9f901397Brian Paul */
70035d5301a54153930ee6fd60dff1010ce9f901397Brian Paulstruct gl_texture_object *
70101e54753ff2bc1831359b04a1906f462d39f33e1Brian Paul_mesa_select_tex_object(GLcontext *ctx, const struct gl_texture_unit *texUnit,
70235d5301a54153930ee6fd60dff1010ce9f901397Brian Paul                        GLenum target)
70335d5301a54153930ee6fd60dff1010ce9f901397Brian Paul{
70435d5301a54153930ee6fd60dff1010ce9f901397Brian Paul   switch (target) {
70535d5301a54153930ee6fd60dff1010ce9f901397Brian Paul      case GL_TEXTURE_1D:
706a852378a6289d154364dde440f89a39bbfc33e2dBrian Paul         return texUnit->Current1D;
70735d5301a54153930ee6fd60dff1010ce9f901397Brian Paul      case GL_PROXY_TEXTURE_1D:
70835d5301a54153930ee6fd60dff1010ce9f901397Brian Paul         return ctx->Texture.Proxy1D;
70935d5301a54153930ee6fd60dff1010ce9f901397Brian Paul      case GL_TEXTURE_2D:
710a852378a6289d154364dde440f89a39bbfc33e2dBrian Paul         return texUnit->Current2D;
71135d5301a54153930ee6fd60dff1010ce9f901397Brian Paul      case GL_PROXY_TEXTURE_2D:
71235d5301a54153930ee6fd60dff1010ce9f901397Brian Paul         return ctx->Texture.Proxy2D;
71335d5301a54153930ee6fd60dff1010ce9f901397Brian Paul      case GL_TEXTURE_3D:
714a852378a6289d154364dde440f89a39bbfc33e2dBrian Paul         return texUnit->Current3D;
71535d5301a54153930ee6fd60dff1010ce9f901397Brian Paul      case GL_PROXY_TEXTURE_3D:
71635d5301a54153930ee6fd60dff1010ce9f901397Brian Paul         return ctx->Texture.Proxy3D;
71735d5301a54153930ee6fd60dff1010ce9f901397Brian Paul      case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
71835d5301a54153930ee6fd60dff1010ce9f901397Brian Paul      case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
71935d5301a54153930ee6fd60dff1010ce9f901397Brian Paul      case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
72035d5301a54153930ee6fd60dff1010ce9f901397Brian Paul      case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
72135d5301a54153930ee6fd60dff1010ce9f901397Brian Paul      case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
72235d5301a54153930ee6fd60dff1010ce9f901397Brian Paul      case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
7235ff4075a6961b26042dc2d7f4adcf333439823f4Brian Paul      case GL_TEXTURE_CUBE_MAP_ARB:
724a96308c37db0bc0086a017d318bc3504aa5f0b1aKeith Whitwell         return ctx->Extensions.ARB_texture_cube_map
72535d5301a54153930ee6fd60dff1010ce9f901397Brian Paul                ? texUnit->CurrentCubeMap : NULL;
72635d5301a54153930ee6fd60dff1010ce9f901397Brian Paul      case GL_PROXY_TEXTURE_CUBE_MAP_ARB:
727a96308c37db0bc0086a017d318bc3504aa5f0b1aKeith Whitwell         return ctx->Extensions.ARB_texture_cube_map
72835d5301a54153930ee6fd60dff1010ce9f901397Brian Paul                ? ctx->Texture.ProxyCubeMap : NULL;
7298afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul      case GL_TEXTURE_RECTANGLE_NV:
7308afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul         return ctx->Extensions.NV_texture_rectangle
7318afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul                ? texUnit->CurrentRect : NULL;
7328afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul      case GL_PROXY_TEXTURE_RECTANGLE_NV:
7338afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul         return ctx->Extensions.NV_texture_rectangle
7348afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul                ? ctx->Texture.ProxyRect : NULL;
73535d5301a54153930ee6fd60dff1010ce9f901397Brian Paul      default:
73608836341788a9f9d638d9dc8328510ccd18ddeb5Brian Paul         _mesa_problem(NULL, "bad target in _mesa_select_tex_object()");
73735d5301a54153930ee6fd60dff1010ce9f901397Brian Paul         return NULL;
73835d5301a54153930ee6fd60dff1010ce9f901397Brian Paul   }
73935d5301a54153930ee6fd60dff1010ce9f901397Brian Paul}
74035d5301a54153930ee6fd60dff1010ce9f901397Brian Paul
74135d5301a54153930ee6fd60dff1010ce9f901397Brian Paul
7426dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/**
7436dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Get the texture image struct which corresponds to target and level
7446dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * of the given texture unit.
7456dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell *
7466dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param ctx GL context.
7476dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param texUnit texture unit.
7486dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param target texture target.
7496dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param level image level.
7506dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell *
7516dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \return pointer to the texture image structure on success, or NULL on failure.
7526dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell *
7536dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \sa gl_texture_unit.
754fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul */
755fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paulstruct gl_texture_image *
756fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul_mesa_select_tex_image(GLcontext *ctx, const struct gl_texture_unit *texUnit,
757fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul                       GLenum target, GLint level)
758fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul{
759fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul   ASSERT(texUnit);
760cd1cefae9146fc14b35ee93a04bdb1b1590fba7bBrian Paul   ASSERT(level < MAX_TEXTURE_LEVELS);
761fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul   switch (target) {
762fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul      case GL_TEXTURE_1D:
76318fa367ac6e035341f5eb86ecc4231124b2921e3Keith Whitwell         return texUnit->Current1D->Image[0][level];
764fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul      case GL_PROXY_TEXTURE_1D:
76518fa367ac6e035341f5eb86ecc4231124b2921e3Keith Whitwell         return ctx->Texture.Proxy1D->Image[0][level];
766fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul      case GL_TEXTURE_2D:
76718fa367ac6e035341f5eb86ecc4231124b2921e3Keith Whitwell         return texUnit->Current2D->Image[0][level];
768fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul      case GL_PROXY_TEXTURE_2D:
76918fa367ac6e035341f5eb86ecc4231124b2921e3Keith Whitwell         return ctx->Texture.Proxy2D->Image[0][level];
770fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul      case GL_TEXTURE_3D:
77118fa367ac6e035341f5eb86ecc4231124b2921e3Keith Whitwell         return texUnit->Current3D->Image[0][level];
772fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul      case GL_PROXY_TEXTURE_3D:
77318fa367ac6e035341f5eb86ecc4231124b2921e3Keith Whitwell         return ctx->Texture.Proxy3D->Image[0][level];
774fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul      case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
775fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul      case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
776fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul      case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
777fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul      case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
778fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul      case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
77918fa367ac6e035341f5eb86ecc4231124b2921e3Keith Whitwell      case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
78018fa367ac6e035341f5eb86ecc4231124b2921e3Keith Whitwell         if (ctx->Extensions.ARB_texture_cube_map) {
78118fa367ac6e035341f5eb86ecc4231124b2921e3Keith Whitwell	    GLuint face = ((GLuint) target -
78218fa367ac6e035341f5eb86ecc4231124b2921e3Keith Whitwell			   (GLuint) GL_TEXTURE_CUBE_MAP_POSITIVE_X);
78318fa367ac6e035341f5eb86ecc4231124b2921e3Keith Whitwell            return texUnit->CurrentCubeMap->Image[face][level];
78418fa367ac6e035341f5eb86ecc4231124b2921e3Keith Whitwell	 }
785fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul         else
786fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul            return NULL;
787fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul      case GL_PROXY_TEXTURE_CUBE_MAP_ARB:
788a96308c37db0bc0086a017d318bc3504aa5f0b1aKeith Whitwell         if (ctx->Extensions.ARB_texture_cube_map)
78918fa367ac6e035341f5eb86ecc4231124b2921e3Keith Whitwell            return ctx->Texture.ProxyCubeMap->Image[0][level];
790fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul         else
791fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul            return NULL;
7928afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul      case GL_TEXTURE_RECTANGLE_NV:
7938afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul         if (ctx->Extensions.NV_texture_rectangle) {
7948afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul            ASSERT(level == 0);
79518fa367ac6e035341f5eb86ecc4231124b2921e3Keith Whitwell            return texUnit->CurrentRect->Image[0][level];
7968afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul         }
7978afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul         else {
7988afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul            return NULL;
7998afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul         }
8008afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul      case GL_PROXY_TEXTURE_RECTANGLE_NV:
8018afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul         if (ctx->Extensions.NV_texture_rectangle) {
8028afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul            ASSERT(level == 0);
80318fa367ac6e035341f5eb86ecc4231124b2921e3Keith Whitwell            return ctx->Texture.ProxyRect->Image[0][level];
8048afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul         }
8058afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul         else {
8068afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul            return NULL;
8078afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul         }
808fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul      default:
80908836341788a9f9d638d9dc8328510ccd18ddeb5Brian Paul         _mesa_problem(ctx, "bad target in _mesa_select_tex_image()");
810fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul         return NULL;
811fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul   }
812fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul}
813fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul
814fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul
815a3f137094cd965d27e1b088499dd609b81a91906Brian Paul/**
816a3f137094cd965d27e1b088499dd609b81a91906Brian Paul * Like _mesa_select_tex_image() but if the image doesn't exist, allocate
817a3f137094cd965d27e1b088499dd609b81a91906Brian Paul * it and install it.  Only return NULL if passed a bad parameter or run
818a3f137094cd965d27e1b088499dd609b81a91906Brian Paul * out of memory.
819a3f137094cd965d27e1b088499dd609b81a91906Brian Paul */
820a3f137094cd965d27e1b088499dd609b81a91906Brian Paulstruct gl_texture_image *
821a3f137094cd965d27e1b088499dd609b81a91906Brian Paul_mesa_get_tex_image(GLcontext *ctx, const struct gl_texture_unit *texUnit,
822a3f137094cd965d27e1b088499dd609b81a91906Brian Paul                    GLenum target, GLint level)
823a3f137094cd965d27e1b088499dd609b81a91906Brian Paul{
824a3f137094cd965d27e1b088499dd609b81a91906Brian Paul   struct gl_texture_image *texImage;
825a3f137094cd965d27e1b088499dd609b81a91906Brian Paul   texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
826a3f137094cd965d27e1b088499dd609b81a91906Brian Paul   if (!texImage) {
827a3f137094cd965d27e1b088499dd609b81a91906Brian Paul      struct gl_texture_object *texObj;
828a3f137094cd965d27e1b088499dd609b81a91906Brian Paul      texImage = ctx->Driver.NewTextureImage(ctx);
829a3f137094cd965d27e1b088499dd609b81a91906Brian Paul      if (!texImage) {
830a3f137094cd965d27e1b088499dd609b81a91906Brian Paul         _mesa_error(ctx, GL_OUT_OF_MEMORY, "texture image allocation");
831a3f137094cd965d27e1b088499dd609b81a91906Brian Paul         return NULL;
832a3f137094cd965d27e1b088499dd609b81a91906Brian Paul      }
833a3f137094cd965d27e1b088499dd609b81a91906Brian Paul      texObj = _mesa_select_tex_object(ctx, texUnit, target);
834a3f137094cd965d27e1b088499dd609b81a91906Brian Paul      ASSERT(texObj);
835a3f137094cd965d27e1b088499dd609b81a91906Brian Paul      _mesa_set_tex_image(texObj, target, level, texImage);
836a3f137094cd965d27e1b088499dd609b81a91906Brian Paul   }
837a3f137094cd965d27e1b088499dd609b81a91906Brian Paul   return texImage;
838a3f137094cd965d27e1b088499dd609b81a91906Brian Paul}
839a3f137094cd965d27e1b088499dd609b81a91906Brian Paul
840a3f137094cd965d27e1b088499dd609b81a91906Brian Paul
841a3f137094cd965d27e1b088499dd609b81a91906Brian Paul/**
842a3f137094cd965d27e1b088499dd609b81a91906Brian Paul * Return pointer to the specified proxy texture image.
843a3f137094cd965d27e1b088499dd609b81a91906Brian Paul * Note that proxy textures are per-context, not per-texture unit.
844a3f137094cd965d27e1b088499dd609b81a91906Brian Paul * \return pointer to texture image or NULL if invalid target, invalid
845a3f137094cd965d27e1b088499dd609b81a91906Brian Paul *         level, or out of memory.
846a3f137094cd965d27e1b088499dd609b81a91906Brian Paul */
847a3f137094cd965d27e1b088499dd609b81a91906Brian Paulstruct gl_texture_image *
848a3f137094cd965d27e1b088499dd609b81a91906Brian Paul_mesa_get_proxy_tex_image(GLcontext *ctx, GLenum target, GLint level)
849a3f137094cd965d27e1b088499dd609b81a91906Brian Paul{
850a3f137094cd965d27e1b088499dd609b81a91906Brian Paul   struct gl_texture_image *texImage;
851a3f137094cd965d27e1b088499dd609b81a91906Brian Paul
852a3f137094cd965d27e1b088499dd609b81a91906Brian Paul   if (level < 0 )
853a3f137094cd965d27e1b088499dd609b81a91906Brian Paul      return NULL;
854a3f137094cd965d27e1b088499dd609b81a91906Brian Paul
855a3f137094cd965d27e1b088499dd609b81a91906Brian Paul   switch (target) {
856a3f137094cd965d27e1b088499dd609b81a91906Brian Paul   case GL_PROXY_TEXTURE_1D:
857a3f137094cd965d27e1b088499dd609b81a91906Brian Paul      if (level >= ctx->Const.MaxTextureLevels)
858a3f137094cd965d27e1b088499dd609b81a91906Brian Paul         return NULL;
85918fa367ac6e035341f5eb86ecc4231124b2921e3Keith Whitwell      texImage = ctx->Texture.Proxy1D->Image[0][level];
860a3f137094cd965d27e1b088499dd609b81a91906Brian Paul      if (!texImage) {
861a3f137094cd965d27e1b088499dd609b81a91906Brian Paul         texImage = ctx->Driver.NewTextureImage(ctx);
862a3f137094cd965d27e1b088499dd609b81a91906Brian Paul         if (!texImage) {
863a3f137094cd965d27e1b088499dd609b81a91906Brian Paul            _mesa_error(ctx, GL_OUT_OF_MEMORY, "proxy texture allocation");
864a3f137094cd965d27e1b088499dd609b81a91906Brian Paul            return NULL;
865a3f137094cd965d27e1b088499dd609b81a91906Brian Paul         }
86618fa367ac6e035341f5eb86ecc4231124b2921e3Keith Whitwell         ctx->Texture.Proxy1D->Image[0][level] = texImage;
867a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul         /* Set the 'back' pointer */
868a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul         texImage->TexObject = ctx->Texture.Proxy1D;
869a3f137094cd965d27e1b088499dd609b81a91906Brian Paul      }
870a3f137094cd965d27e1b088499dd609b81a91906Brian Paul      return texImage;
871a3f137094cd965d27e1b088499dd609b81a91906Brian Paul   case GL_PROXY_TEXTURE_2D:
872a3f137094cd965d27e1b088499dd609b81a91906Brian Paul      if (level >= ctx->Const.MaxTextureLevels)
873a3f137094cd965d27e1b088499dd609b81a91906Brian Paul         return NULL;
87418fa367ac6e035341f5eb86ecc4231124b2921e3Keith Whitwell      texImage = ctx->Texture.Proxy2D->Image[0][level];
875a3f137094cd965d27e1b088499dd609b81a91906Brian Paul      if (!texImage) {
876a3f137094cd965d27e1b088499dd609b81a91906Brian Paul         texImage = ctx->Driver.NewTextureImage(ctx);
877a3f137094cd965d27e1b088499dd609b81a91906Brian Paul         if (!texImage) {
878a3f137094cd965d27e1b088499dd609b81a91906Brian Paul            _mesa_error(ctx, GL_OUT_OF_MEMORY, "proxy texture allocation");
879a3f137094cd965d27e1b088499dd609b81a91906Brian Paul            return NULL;
880a3f137094cd965d27e1b088499dd609b81a91906Brian Paul         }
88118fa367ac6e035341f5eb86ecc4231124b2921e3Keith Whitwell         ctx->Texture.Proxy2D->Image[0][level] = texImage;
882a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul         /* Set the 'back' pointer */
883a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul         texImage->TexObject = ctx->Texture.Proxy2D;
884a3f137094cd965d27e1b088499dd609b81a91906Brian Paul      }
885a3f137094cd965d27e1b088499dd609b81a91906Brian Paul      return texImage;
886a3f137094cd965d27e1b088499dd609b81a91906Brian Paul   case GL_PROXY_TEXTURE_3D:
887a3f137094cd965d27e1b088499dd609b81a91906Brian Paul      if (level >= ctx->Const.Max3DTextureLevels)
888a3f137094cd965d27e1b088499dd609b81a91906Brian Paul         return NULL;
88918fa367ac6e035341f5eb86ecc4231124b2921e3Keith Whitwell      texImage = ctx->Texture.Proxy3D->Image[0][level];
890a3f137094cd965d27e1b088499dd609b81a91906Brian Paul      if (!texImage) {
891a3f137094cd965d27e1b088499dd609b81a91906Brian Paul         texImage = ctx->Driver.NewTextureImage(ctx);
892a3f137094cd965d27e1b088499dd609b81a91906Brian Paul         if (!texImage) {
893a3f137094cd965d27e1b088499dd609b81a91906Brian Paul            _mesa_error(ctx, GL_OUT_OF_MEMORY, "proxy texture allocation");
894a3f137094cd965d27e1b088499dd609b81a91906Brian Paul            return NULL;
895a3f137094cd965d27e1b088499dd609b81a91906Brian Paul         }
89618fa367ac6e035341f5eb86ecc4231124b2921e3Keith Whitwell         ctx->Texture.Proxy3D->Image[0][level] = texImage;
897a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul         /* Set the 'back' pointer */
898a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul         texImage->TexObject = ctx->Texture.Proxy3D;
899a3f137094cd965d27e1b088499dd609b81a91906Brian Paul      }
900a3f137094cd965d27e1b088499dd609b81a91906Brian Paul      return texImage;
901a3f137094cd965d27e1b088499dd609b81a91906Brian Paul   case GL_PROXY_TEXTURE_CUBE_MAP:
902a3f137094cd965d27e1b088499dd609b81a91906Brian Paul      if (level >= ctx->Const.MaxCubeTextureLevels)
903a3f137094cd965d27e1b088499dd609b81a91906Brian Paul         return NULL;
90418fa367ac6e035341f5eb86ecc4231124b2921e3Keith Whitwell      texImage = ctx->Texture.ProxyCubeMap->Image[0][level];
905a3f137094cd965d27e1b088499dd609b81a91906Brian Paul      if (!texImage) {
906a3f137094cd965d27e1b088499dd609b81a91906Brian Paul         texImage = ctx->Driver.NewTextureImage(ctx);
907a3f137094cd965d27e1b088499dd609b81a91906Brian Paul         if (!texImage) {
908a3f137094cd965d27e1b088499dd609b81a91906Brian Paul            _mesa_error(ctx, GL_OUT_OF_MEMORY, "proxy texture allocation");
909a3f137094cd965d27e1b088499dd609b81a91906Brian Paul            return NULL;
910a3f137094cd965d27e1b088499dd609b81a91906Brian Paul         }
91118fa367ac6e035341f5eb86ecc4231124b2921e3Keith Whitwell         ctx->Texture.ProxyCubeMap->Image[0][level] = texImage;
912a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul         /* Set the 'back' pointer */
913a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul         texImage->TexObject = ctx->Texture.ProxyCubeMap;
914a3f137094cd965d27e1b088499dd609b81a91906Brian Paul      }
915a3f137094cd965d27e1b088499dd609b81a91906Brian Paul      return texImage;
916a3f137094cd965d27e1b088499dd609b81a91906Brian Paul   case GL_PROXY_TEXTURE_RECTANGLE_NV:
917a3f137094cd965d27e1b088499dd609b81a91906Brian Paul      if (level > 0)
918a3f137094cd965d27e1b088499dd609b81a91906Brian Paul         return NULL;
91918fa367ac6e035341f5eb86ecc4231124b2921e3Keith Whitwell      texImage = ctx->Texture.ProxyRect->Image[0][level];
920a3f137094cd965d27e1b088499dd609b81a91906Brian Paul      if (!texImage) {
921a3f137094cd965d27e1b088499dd609b81a91906Brian Paul         texImage = ctx->Driver.NewTextureImage(ctx);
922a3f137094cd965d27e1b088499dd609b81a91906Brian Paul         if (!texImage) {
923a3f137094cd965d27e1b088499dd609b81a91906Brian Paul            _mesa_error(ctx, GL_OUT_OF_MEMORY, "proxy texture allocation");
924a3f137094cd965d27e1b088499dd609b81a91906Brian Paul            return NULL;
925a3f137094cd965d27e1b088499dd609b81a91906Brian Paul         }
92618fa367ac6e035341f5eb86ecc4231124b2921e3Keith Whitwell         ctx->Texture.ProxyRect->Image[0][level] = texImage;
927a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul         /* Set the 'back' pointer */
928a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul         texImage->TexObject = ctx->Texture.ProxyRect;
929a3f137094cd965d27e1b088499dd609b81a91906Brian Paul      }
930a3f137094cd965d27e1b088499dd609b81a91906Brian Paul      return texImage;
931a3f137094cd965d27e1b088499dd609b81a91906Brian Paul   default:
932a3f137094cd965d27e1b088499dd609b81a91906Brian Paul      return NULL;
933a3f137094cd965d27e1b088499dd609b81a91906Brian Paul   }
934a3f137094cd965d27e1b088499dd609b81a91906Brian Paul}
935a3f137094cd965d27e1b088499dd609b81a91906Brian Paul
936a3f137094cd965d27e1b088499dd609b81a91906Brian Paul
9376dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/**
9386dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Get the maximum number of allowed mipmap levels.
9396dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell *
9406dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param ctx GL context.
9416dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param target texture target.
9426dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell *
9436dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \return the maximum number of allowed mipmap levels for the given
9446dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * texture target, or zero if passed a bad target.
9456dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell *
9466dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \sa gl_constants.
947ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul */
948ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian PaulGLint
949ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul_mesa_max_texture_levels(GLcontext *ctx, GLenum target)
950ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul{
951ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul   switch (target) {
952ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul   case GL_TEXTURE_1D:
953ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul   case GL_PROXY_TEXTURE_1D:
954ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul   case GL_TEXTURE_2D:
955ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul   case GL_PROXY_TEXTURE_2D:
956ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul      return ctx->Const.MaxTextureLevels;
957ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul   case GL_TEXTURE_3D:
958ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul   case GL_PROXY_TEXTURE_3D:
959ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul      return ctx->Const.Max3DTextureLevels;
960ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul   case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
961ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul   case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
962ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul   case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
963ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul   case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
964ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul   case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
965ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul   case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
966add9f2168a5d6b15eb9955ee761246c4f4cf8458Brian Paul   case GL_TEXTURE_CUBE_MAP_ARB:
967ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul   case GL_PROXY_TEXTURE_CUBE_MAP_ARB:
968ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul      return ctx->Const.MaxCubeTextureLevels;
969ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul   case GL_TEXTURE_RECTANGLE_NV:
970ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul   case GL_PROXY_TEXTURE_RECTANGLE_NV:
971ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul      return 1;
972ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul   default:
973ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul      return 0; /* bad target */
974ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul   }
975ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul}
976ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul
977ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul
978fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul
97962c734f49948df7aeef55ad23a6664cbf3e11533Brian Paul#if 000 /* not used anymore */
980f93b3dd69e744cf1dd6b102a11cdb07c2df4a967Brian Paul/*
981afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg * glTexImage[123]D can accept a NULL image pointer.  In this case we
982afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg * create a texture image with unspecified image contents per the OpenGL
9838e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul * spec.
984afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg */
9858e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paulstatic GLubyte *
9868e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paulmake_null_texture(GLint width, GLint height, GLint depth, GLenum format)
987afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg{
9888e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul   const GLint components = _mesa_components_in_format(format);
9898e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul   const GLint numPixels = width * height * depth;
9908e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul   GLubyte *data = (GLubyte *) MALLOC(numPixels * components * sizeof(GLubyte));
991afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
992a1503b00f863a48a517939a42d512f9cfe77f79cBrian Paul#ifdef DEBUG
993afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   /*
994afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg    * Let's see if anyone finds this.  If glTexImage2D() is called with
995afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg    * a NULL image pointer then load the texture image with something
996afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg    * interesting instead of leaving it indeterminate.
997afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg    */
9988e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul   if (data) {
99965d54604c387dca986c876e811362d8e8517dcacBrian Paul      static const char message[8][32] = {
1000afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         "   X   X  XXXXX   XXX     X    ",
1001afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         "   XX XX  X      X   X   X X   ",
1002afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         "   X X X  X      X      X   X  ",
1003afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         "   X   X  XXXX    XXX   XXXXX  ",
1004afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         "   X   X  X          X  X   X  ",
1005afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         "   X   X  X      X   X  X   X  ",
1006afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         "   X   X  XXXXX   XXX   X   X  ",
1007afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         "                               "
1008afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      };
1009afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
10108e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul      GLubyte *imgPtr = data;
10118e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul      GLint h, i, j, k;
10128e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul      for (h = 0; h < depth; h++) {
10138e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul         for (i = 0; i < height; i++) {
10148e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul            GLint srcRow = 7 - (i % 8);
10158e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul            for (j = 0; j < width; j++) {
10168e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul               GLint srcCol = j % 32;
10178e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul               GLubyte texel = (message[srcRow][srcCol]=='X') ? 255 : 70;
10188e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul               for (k = 0; k < components; k++) {
10198e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul                  *imgPtr++ = texel;
10208e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul               }
1021afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg            }
1022afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         }
1023afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      }
1024afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   }
1025a1503b00f863a48a517939a42d512f9cfe77f79cBrian Paul#endif
10268e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul
10278e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul   return data;
1028afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg}
102962c734f49948df7aeef55ad23a6664cbf3e11533Brian Paul#endif
1030afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
1031afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
1032afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
1033738318bb75dea8dac4465f53850987f6062a732dBrian Paul/**
1034f378ab825c0c74aab263e7dec30194eead22c288Brian Paul * Reset the fields of a gl_texture_image struct to zero.
10356dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell *
10366dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param img texture image structure.
10376dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell *
10389c27278acfb786c8f2fc591eef9ed0c25135bcf0Brian Paul * This is called when a proxy texture test fails, we set all the
10399c27278acfb786c8f2fc591eef9ed0c25135bcf0Brian Paul * image members (except DriverData) to zero.
1040f378ab825c0c74aab263e7dec30194eead22c288Brian Paul * It's also used in glTexImage[123]D as a safeguard to be sure all
1041f378ab825c0c74aab263e7dec30194eead22c288Brian Paul * required fields get initialized properly by the Driver.TexImage[123]D
1042f378ab825c0c74aab263e7dec30194eead22c288Brian Paul * functions.
10439c27278acfb786c8f2fc591eef9ed0c25135bcf0Brian Paul */
10449c27278acfb786c8f2fc591eef9ed0c25135bcf0Brian Paulstatic void
1045f378ab825c0c74aab263e7dec30194eead22c288Brian Paulclear_teximage_fields(struct gl_texture_image *img)
10469c27278acfb786c8f2fc591eef9ed0c25135bcf0Brian Paul{
10479c27278acfb786c8f2fc591eef9ed0c25135bcf0Brian Paul   ASSERT(img);
1048a9fc8ba756dd25a07dc19058fe60f65bda82a055Brian Paul   img->_BaseFormat = 0;
1049a9fc8ba756dd25a07dc19058fe60f65bda82a055Brian Paul   img->InternalFormat = 0;
10509c27278acfb786c8f2fc591eef9ed0c25135bcf0Brian Paul   img->Border = 0;
10519c27278acfb786c8f2fc591eef9ed0c25135bcf0Brian Paul   img->Width = 0;
10529c27278acfb786c8f2fc591eef9ed0c25135bcf0Brian Paul   img->Height = 0;
10539c27278acfb786c8f2fc591eef9ed0c25135bcf0Brian Paul   img->Depth = 0;
1054681b8c9d1ba06c8c82e687a5ced369b72e6b1eb9Brian Paul   img->RowStride = 0;
1055b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul   if (img->ImageOffsets) {
1056b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul      _mesa_free(img->ImageOffsets);
1057b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul      img->ImageOffsets = NULL;
1058b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul   }
10599c27278acfb786c8f2fc591eef9ed0c25135bcf0Brian Paul   img->Width2 = 0;
10609c27278acfb786c8f2fc591eef9ed0c25135bcf0Brian Paul   img->Height2 = 0;
10619c27278acfb786c8f2fc591eef9ed0c25135bcf0Brian Paul   img->Depth2 = 0;
10629c27278acfb786c8f2fc591eef9ed0c25135bcf0Brian Paul   img->WidthLog2 = 0;
10639c27278acfb786c8f2fc591eef9ed0c25135bcf0Brian Paul   img->HeightLog2 = 0;
10649c27278acfb786c8f2fc591eef9ed0c25135bcf0Brian Paul   img->DepthLog2 = 0;
10659c27278acfb786c8f2fc591eef9ed0c25135bcf0Brian Paul   img->Data = NULL;
10662c3d34c905fa6b831a066afae83b938de05eb241Gareth Hughes   img->TexFormat = &_mesa_null_texformat;
10674f295cee73bae1f687efe2dc062522b40d90b1e4Brian Paul   img->FetchTexelc = NULL;
10684f295cee73bae1f687efe2dc062522b40d90b1e4Brian Paul   img->FetchTexelf = NULL;
10699c27278acfb786c8f2fc591eef9ed0c25135bcf0Brian Paul   img->IsCompressed = 0;
10709c27278acfb786c8f2fc591eef9ed0c25135bcf0Brian Paul   img->CompressedSize = 0;
10719c27278acfb786c8f2fc591eef9ed0c25135bcf0Brian Paul}
10729c27278acfb786c8f2fc591eef9ed0c25135bcf0Brian Paul
10739c27278acfb786c8f2fc591eef9ed0c25135bcf0Brian Paul
10746dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/**
10756628bc9cff74a6d524165e809f73eabc85ba34b5Brian Paul * Initialize basic fields of the gl_texture_image struct.
10766dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell *
10776dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param ctx GL context.
1078b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul * \param target texture target (GL_TEXTURE_1D, GL_TEXTURE_RECTANGLE, etc).
10796dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param img texture image structure to be initialized.
10806dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param width image width.
10816dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param height image height.
10826dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param depth image depth.
10836dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param border image border.
10846dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param internalFormat internal format.
10856dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell *
10866dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Fills in the fields of \p img with the given information.
1087887e2cf01a99f7fe1b7c94320b7bdbbf0d6ad2f8Brian Paul * Note: width, height and depth include the border.
10886628bc9cff74a6d524165e809f73eabc85ba34b5Brian Paul */
10893893e638e6521b9c070e01c0b31d22754ff97a88Brian Paulvoid
10908afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul_mesa_init_teximage_fields(GLcontext *ctx, GLenum target,
10913893e638e6521b9c070e01c0b31d22754ff97a88Brian Paul                           struct gl_texture_image *img,
10923893e638e6521b9c070e01c0b31d22754ff97a88Brian Paul                           GLsizei width, GLsizei height, GLsizei depth,
10933893e638e6521b9c070e01c0b31d22754ff97a88Brian Paul                           GLint border, GLenum internalFormat)
10946628bc9cff74a6d524165e809f73eabc85ba34b5Brian Paul{
1095b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul   GLint i;
1096b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul
10976628bc9cff74a6d524165e809f73eabc85ba34b5Brian Paul   ASSERT(img);
1098b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul   ASSERT(width > 0);
1099b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul   ASSERT(height > 0);
1100b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul   ASSERT(depth > 0);
1101b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul
1102a9fc8ba756dd25a07dc19058fe60f65bda82a055Brian Paul   img->_BaseFormat = _mesa_base_tex_format( ctx, internalFormat );
1103a9fc8ba756dd25a07dc19058fe60f65bda82a055Brian Paul   ASSERT(img->_BaseFormat > 0);
1104a9fc8ba756dd25a07dc19058fe60f65bda82a055Brian Paul   img->InternalFormat = internalFormat;
11056628bc9cff74a6d524165e809f73eabc85ba34b5Brian Paul   img->Border = border;
11066628bc9cff74a6d524165e809f73eabc85ba34b5Brian Paul   img->Width = width;
11076628bc9cff74a6d524165e809f73eabc85ba34b5Brian Paul   img->Height = height;
11086628bc9cff74a6d524165e809f73eabc85ba34b5Brian Paul   img->Depth = depth;
11094d0b7618cb3ada3b13e9e9b650ace34f5131e318Brian Paul   img->Width2 = width - 2 * border;   /* == 1 << img->WidthLog2; */
11104d0b7618cb3ada3b13e9e9b650ace34f5131e318Brian Paul   img->Height2 = height - 2 * border; /* == 1 << img->HeightLog2; */
11114d0b7618cb3ada3b13e9e9b650ace34f5131e318Brian Paul   img->Depth2 = depth - 2 * border;   /* == 1 << img->DepthLog2; */
11124d0b7618cb3ada3b13e9e9b650ace34f5131e318Brian Paul   img->WidthLog2 = logbase2(img->Width2);
11136628bc9cff74a6d524165e809f73eabc85ba34b5Brian Paul   if (height == 1)  /* 1-D texture */
11146628bc9cff74a6d524165e809f73eabc85ba34b5Brian Paul      img->HeightLog2 = 0;
11156628bc9cff74a6d524165e809f73eabc85ba34b5Brian Paul   else
11164d0b7618cb3ada3b13e9e9b650ace34f5131e318Brian Paul      img->HeightLog2 = logbase2(img->Height2);
11176628bc9cff74a6d524165e809f73eabc85ba34b5Brian Paul   if (depth == 1)   /* 2-D texture */
11186628bc9cff74a6d524165e809f73eabc85ba34b5Brian Paul      img->DepthLog2 = 0;
11196628bc9cff74a6d524165e809f73eabc85ba34b5Brian Paul   else
11204d0b7618cb3ada3b13e9e9b650ace34f5131e318Brian Paul      img->DepthLog2 = logbase2(img->Depth2);
11216628bc9cff74a6d524165e809f73eabc85ba34b5Brian Paul   img->MaxLog2 = MAX2(img->WidthLog2, img->HeightLog2);
11225999c5b620236fb6a996cf56759aec31f01c126bBrian Paul   img->IsCompressed = GL_FALSE;
11235999c5b620236fb6a996cf56759aec31f01c126bBrian Paul   img->CompressedSize = 0;
112489fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul
11254d0b7618cb3ada3b13e9e9b650ace34f5131e318Brian Paul   if ((width == 1 || _mesa_bitcount(img->Width2) == 1) &&
11264d0b7618cb3ada3b13e9e9b650ace34f5131e318Brian Paul       (height == 1 || _mesa_bitcount(img->Height2) == 1) &&
11274d0b7618cb3ada3b13e9e9b650ace34f5131e318Brian Paul       (depth == 1 || _mesa_bitcount(img->Depth2) == 1))
1128887e2cf01a99f7fe1b7c94320b7bdbbf0d6ad2f8Brian Paul      img->_IsPowerOfTwo = GL_TRUE;
1129887e2cf01a99f7fe1b7c94320b7bdbbf0d6ad2f8Brian Paul   else
1130887e2cf01a99f7fe1b7c94320b7bdbbf0d6ad2f8Brian Paul      img->_IsPowerOfTwo = GL_FALSE;
1131887e2cf01a99f7fe1b7c94320b7bdbbf0d6ad2f8Brian Paul
1132b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul   /* RowStride and ImageOffsets[] describe how to address texels in 'Data' */
1133b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul   img->RowStride = width;
1134b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul   /* Allocate the ImageOffsets array and initialize to typical values.
1135b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul    * We allocate the array for 1D/2D textures too in order to avoid special-
1136b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul    * case code in the texstore routines.
1137b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul    */
1138b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul   img->ImageOffsets = (GLuint *) _mesa_malloc(depth * sizeof(GLuint));
1139b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul   for (i = 0; i < depth; i++) {
1140b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul      img->ImageOffsets[i] = i * width * height;
1141b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul   }
1142b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul
11438afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul   /* Compute Width/Height/DepthScale for mipmap lod computation */
11448afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul   if (target == GL_TEXTURE_RECTANGLE_NV) {
11458afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul      /* scale = 1.0 since texture coords directly map to texels */
11468afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul      img->WidthScale = 1.0;
11478afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul      img->HeightScale = 1.0;
11488afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul      img->DepthScale = 1.0;
11498afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul   }
11508afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul   else {
11518afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul      img->WidthScale = (GLfloat) img->Width;
11528afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul      img->HeightScale = (GLfloat) img->Height;
11538afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul      img->DepthScale = (GLfloat) img->Depth;
11548afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul   }
11556628bc9cff74a6d524165e809f73eabc85ba34b5Brian Paul}
11566628bc9cff74a6d524165e809f73eabc85ba34b5Brian Paul
11576628bc9cff74a6d524165e809f73eabc85ba34b5Brian Paul
1158738318bb75dea8dac4465f53850987f6062a732dBrian Paul/**
1159738318bb75dea8dac4465f53850987f6062a732dBrian Paul * This is the fallback for Driver.TestProxyTexImage().  Test the texture
1160738318bb75dea8dac4465f53850987f6062a732dBrian Paul * level, width, height and depth against the ctx->Const limits for textures.
1161738318bb75dea8dac4465f53850987f6062a732dBrian Paul *
1162738318bb75dea8dac4465f53850987f6062a732dBrian Paul * A hardware driver might override this function if, for example, the
1163738318bb75dea8dac4465f53850987f6062a732dBrian Paul * max 3D texture size is 512x512x64 (i.e. not a cube).
1164738318bb75dea8dac4465f53850987f6062a732dBrian Paul *
1165738318bb75dea8dac4465f53850987f6062a732dBrian Paul * \param target  one of GL_PROXY_TEXTURE_1D, GL_PROXY_TEXTURE_2D,
1166738318bb75dea8dac4465f53850987f6062a732dBrian Paul *                GL_PROXY_TEXTURE_3D, GL_PROXY_TEXTURE_RECTANGLE_NV,
1167738318bb75dea8dac4465f53850987f6062a732dBrian Paul *                GL_PROXY_TEXTURE_CUBE_MAP_ARB.
1168738318bb75dea8dac4465f53850987f6062a732dBrian Paul * \param level  as passed to glTexImage
1169738318bb75dea8dac4465f53850987f6062a732dBrian Paul * \param internalFormat  as passed to glTexImage
1170738318bb75dea8dac4465f53850987f6062a732dBrian Paul * \param format  as passed to glTexImage
1171738318bb75dea8dac4465f53850987f6062a732dBrian Paul * \param type  as passed to glTexImage
1172738318bb75dea8dac4465f53850987f6062a732dBrian Paul * \param width  as passed to glTexImage
1173738318bb75dea8dac4465f53850987f6062a732dBrian Paul * \param height  as passed to glTexImage
1174738318bb75dea8dac4465f53850987f6062a732dBrian Paul * \param depth  as passed to glTexImage
1175738318bb75dea8dac4465f53850987f6062a732dBrian Paul * \param border  as passed to glTexImage
1176738318bb75dea8dac4465f53850987f6062a732dBrian Paul * \return GL_TRUE if the image is acceptable, GL_FALSE if not acceptable.
1177738318bb75dea8dac4465f53850987f6062a732dBrian Paul */
1178738318bb75dea8dac4465f53850987f6062a732dBrian PaulGLboolean
1179738318bb75dea8dac4465f53850987f6062a732dBrian Paul_mesa_test_proxy_teximage(GLcontext *ctx, GLenum target, GLint level,
1180738318bb75dea8dac4465f53850987f6062a732dBrian Paul                          GLint internalFormat, GLenum format, GLenum type,
1181738318bb75dea8dac4465f53850987f6062a732dBrian Paul                          GLint width, GLint height, GLint depth, GLint border)
1182738318bb75dea8dac4465f53850987f6062a732dBrian Paul{
1183738318bb75dea8dac4465f53850987f6062a732dBrian Paul   GLint maxSize;
11849c27278acfb786c8f2fc591eef9ed0c25135bcf0Brian Paul
1185738318bb75dea8dac4465f53850987f6062a732dBrian Paul   (void) internalFormat;
1186738318bb75dea8dac4465f53850987f6062a732dBrian Paul   (void) format;
1187738318bb75dea8dac4465f53850987f6062a732dBrian Paul   (void) type;
1188738318bb75dea8dac4465f53850987f6062a732dBrian Paul
1189738318bb75dea8dac4465f53850987f6062a732dBrian Paul   switch (target) {
1190738318bb75dea8dac4465f53850987f6062a732dBrian Paul   case GL_PROXY_TEXTURE_1D:
1191738318bb75dea8dac4465f53850987f6062a732dBrian Paul      maxSize = 1 << (ctx->Const.MaxTextureLevels - 1);
1192738318bb75dea8dac4465f53850987f6062a732dBrian Paul      if (width < 2 * border || width > 2 + maxSize ||
1193887e2cf01a99f7fe1b7c94320b7bdbbf0d6ad2f8Brian Paul          (!ctx->Extensions.ARB_texture_non_power_of_two &&
1194887e2cf01a99f7fe1b7c94320b7bdbbf0d6ad2f8Brian Paul           _mesa_bitcount(width - 2 * border) != 1) ||
1195738318bb75dea8dac4465f53850987f6062a732dBrian Paul          level >= ctx->Const.MaxTextureLevels) {
1196738318bb75dea8dac4465f53850987f6062a732dBrian Paul         /* bad width or level */
1197738318bb75dea8dac4465f53850987f6062a732dBrian Paul         return GL_FALSE;
1198738318bb75dea8dac4465f53850987f6062a732dBrian Paul      }
1199738318bb75dea8dac4465f53850987f6062a732dBrian Paul      return GL_TRUE;
1200738318bb75dea8dac4465f53850987f6062a732dBrian Paul   case GL_PROXY_TEXTURE_2D:
1201738318bb75dea8dac4465f53850987f6062a732dBrian Paul      maxSize = 1 << (ctx->Const.MaxTextureLevels - 1);
1202738318bb75dea8dac4465f53850987f6062a732dBrian Paul      if (width < 2 * border || width > 2 + maxSize ||
1203887e2cf01a99f7fe1b7c94320b7bdbbf0d6ad2f8Brian Paul          (!ctx->Extensions.ARB_texture_non_power_of_two &&
1204887e2cf01a99f7fe1b7c94320b7bdbbf0d6ad2f8Brian Paul           _mesa_bitcount(width - 2 * border) != 1) ||
1205738318bb75dea8dac4465f53850987f6062a732dBrian Paul          height < 2 * border || height > 2 + maxSize ||
1206887e2cf01a99f7fe1b7c94320b7bdbbf0d6ad2f8Brian Paul          (!ctx->Extensions.ARB_texture_non_power_of_two &&
1207887e2cf01a99f7fe1b7c94320b7bdbbf0d6ad2f8Brian Paul           _mesa_bitcount(height - 2 * border) != 1) ||
1208738318bb75dea8dac4465f53850987f6062a732dBrian Paul          level >= ctx->Const.MaxTextureLevels) {
1209738318bb75dea8dac4465f53850987f6062a732dBrian Paul         /* bad width or height or level */
1210738318bb75dea8dac4465f53850987f6062a732dBrian Paul         return GL_FALSE;
1211738318bb75dea8dac4465f53850987f6062a732dBrian Paul      }
1212738318bb75dea8dac4465f53850987f6062a732dBrian Paul      return GL_TRUE;
1213738318bb75dea8dac4465f53850987f6062a732dBrian Paul   case GL_PROXY_TEXTURE_3D:
1214738318bb75dea8dac4465f53850987f6062a732dBrian Paul      maxSize = 1 << (ctx->Const.Max3DTextureLevels - 1);
1215738318bb75dea8dac4465f53850987f6062a732dBrian Paul      if (width < 2 * border || width > 2 + maxSize ||
1216887e2cf01a99f7fe1b7c94320b7bdbbf0d6ad2f8Brian Paul          (!ctx->Extensions.ARB_texture_non_power_of_two &&
1217887e2cf01a99f7fe1b7c94320b7bdbbf0d6ad2f8Brian Paul           _mesa_bitcount(width - 2 * border) != 1) ||
1218738318bb75dea8dac4465f53850987f6062a732dBrian Paul          height < 2 * border || height > 2 + maxSize ||
1219887e2cf01a99f7fe1b7c94320b7bdbbf0d6ad2f8Brian Paul          (!ctx->Extensions.ARB_texture_non_power_of_two &&
1220887e2cf01a99f7fe1b7c94320b7bdbbf0d6ad2f8Brian Paul           _mesa_bitcount(height - 2 * border) != 1) ||
1221738318bb75dea8dac4465f53850987f6062a732dBrian Paul          depth < 2 * border || depth > 2 + maxSize ||
1222887e2cf01a99f7fe1b7c94320b7bdbbf0d6ad2f8Brian Paul          (!ctx->Extensions.ARB_texture_non_power_of_two &&
1223887e2cf01a99f7fe1b7c94320b7bdbbf0d6ad2f8Brian Paul           _mesa_bitcount(depth - 2 * border) != 1) ||
1224738318bb75dea8dac4465f53850987f6062a732dBrian Paul          level >= ctx->Const.Max3DTextureLevels) {
1225738318bb75dea8dac4465f53850987f6062a732dBrian Paul         /* bad width or height or depth or level */
1226738318bb75dea8dac4465f53850987f6062a732dBrian Paul         return GL_FALSE;
1227738318bb75dea8dac4465f53850987f6062a732dBrian Paul      }
1228738318bb75dea8dac4465f53850987f6062a732dBrian Paul      return GL_TRUE;
1229738318bb75dea8dac4465f53850987f6062a732dBrian Paul   case GL_PROXY_TEXTURE_RECTANGLE_NV:
1230738318bb75dea8dac4465f53850987f6062a732dBrian Paul      if (width < 1 || width > ctx->Const.MaxTextureRectSize ||
1231738318bb75dea8dac4465f53850987f6062a732dBrian Paul          height < 1 || height > ctx->Const.MaxTextureRectSize ||
1232738318bb75dea8dac4465f53850987f6062a732dBrian Paul          level != 0) {
1233738318bb75dea8dac4465f53850987f6062a732dBrian Paul         /* bad width or height or level */
1234738318bb75dea8dac4465f53850987f6062a732dBrian Paul         return GL_FALSE;
1235738318bb75dea8dac4465f53850987f6062a732dBrian Paul      }
1236738318bb75dea8dac4465f53850987f6062a732dBrian Paul      return GL_TRUE;
1237738318bb75dea8dac4465f53850987f6062a732dBrian Paul   case GL_PROXY_TEXTURE_CUBE_MAP_ARB:
1238738318bb75dea8dac4465f53850987f6062a732dBrian Paul      maxSize = 1 << (ctx->Const.MaxCubeTextureLevels - 1);
1239738318bb75dea8dac4465f53850987f6062a732dBrian Paul      if (width < 2 * border || width > 2 + maxSize ||
1240887e2cf01a99f7fe1b7c94320b7bdbbf0d6ad2f8Brian Paul          (!ctx->Extensions.ARB_texture_non_power_of_two &&
1241887e2cf01a99f7fe1b7c94320b7bdbbf0d6ad2f8Brian Paul           _mesa_bitcount(width - 2 * border) != 1) ||
1242738318bb75dea8dac4465f53850987f6062a732dBrian Paul          height < 2 * border || height > 2 + maxSize ||
1243887e2cf01a99f7fe1b7c94320b7bdbbf0d6ad2f8Brian Paul          (!ctx->Extensions.ARB_texture_non_power_of_two &&
1244887e2cf01a99f7fe1b7c94320b7bdbbf0d6ad2f8Brian Paul           _mesa_bitcount(height - 2 * border) != 1) ||
1245738318bb75dea8dac4465f53850987f6062a732dBrian Paul          level >= ctx->Const.MaxCubeTextureLevels) {
1246738318bb75dea8dac4465f53850987f6062a732dBrian Paul         /* bad width or height */
1247738318bb75dea8dac4465f53850987f6062a732dBrian Paul         return GL_FALSE;
1248738318bb75dea8dac4465f53850987f6062a732dBrian Paul      }
1249738318bb75dea8dac4465f53850987f6062a732dBrian Paul      return GL_TRUE;
1250738318bb75dea8dac4465f53850987f6062a732dBrian Paul   default:
1251738318bb75dea8dac4465f53850987f6062a732dBrian Paul      _mesa_problem(ctx, "Invalid target in _mesa_test_proxy_teximage");
1252738318bb75dea8dac4465f53850987f6062a732dBrian Paul      return GL_FALSE;
1253738318bb75dea8dac4465f53850987f6062a732dBrian Paul   }
1254738318bb75dea8dac4465f53850987f6062a732dBrian Paul}
1255738318bb75dea8dac4465f53850987f6062a732dBrian Paul
1256738318bb75dea8dac4465f53850987f6062a732dBrian Paul
1257738318bb75dea8dac4465f53850987f6062a732dBrian Paul/**
12586dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Test the glTexImage[123]D() parameters for errors.
12596dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell *
12606dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param ctx GL context.
12616dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param target texture target given by the user.
12626dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param level image level given by the user.
12636dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param internalFormat internal format given by the user.
12646dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param format pixel data format given by the user.
12656dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param type pixel data type given by the user.
12666dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param dimensions texture image dimensions (must be 1, 2 or 3).
12676dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param width image width given by the user.
12686dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param height image height given by the user.
12696dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param depth image depth given by the user.
12706dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param border image border given by the user.
12716dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell *
12726dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
12736dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell *
12746dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Verifies each of the parameters against the constants specified in
12756dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * __GLcontextRec::Const and the supported extensions, and according to the
12766dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * OpenGL specification.
1277afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg */
1278c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paulstatic GLboolean
1279c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paultexture_error_check( GLcontext *ctx, GLenum target,
1280c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul                     GLint level, GLint internalFormat,
1281c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul                     GLenum format, GLenum type,
12825b37c322741f019118a618bc6220f37adba4fbcdBrian Paul                     GLuint dimensions,
1283c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul                     GLint width, GLint height,
1284c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul                     GLint depth, GLint border )
1285afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg{
128677ee31930a1b0cc7766939415f4f04ed6a1fa4acBrian Paul   const GLboolean isProxy = _mesa_is_proxy_texture(target);
1287738318bb75dea8dac4465f53850987f6062a732dBrian Paul   GLboolean sizeOK;
1288714c36c1202cd49c58cf6462afd391fd059b96c2Daniel Borca   GLboolean colorFormat, indexFormat;
1289afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
1290738318bb75dea8dac4465f53850987f6062a732dBrian Paul   /* Basic level check (more checking in ctx->Driver.TestProxyTexImage) */
1291738318bb75dea8dac4465f53850987f6062a732dBrian Paul   if (level < 0 || level >= MAX_TEXTURE_LEVELS) {
1292738318bb75dea8dac4465f53850987f6062a732dBrian Paul      if (!isProxy) {
1293738318bb75dea8dac4465f53850987f6062a732dBrian Paul         _mesa_error(ctx, GL_INVALID_VALUE,
1294738318bb75dea8dac4465f53850987f6062a732dBrian Paul                     "glTexImage%dD(level=%d)", dimensions, level);
12958afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul      }
1296738318bb75dea8dac4465f53850987f6062a732dBrian Paul      return GL_TRUE;
1297738318bb75dea8dac4465f53850987f6062a732dBrian Paul   }
1298738318bb75dea8dac4465f53850987f6062a732dBrian Paul
1299738318bb75dea8dac4465f53850987f6062a732dBrian Paul   /* Check border */
1300738318bb75dea8dac4465f53850987f6062a732dBrian Paul   if (border < 0 || border > 1 ||
1301738318bb75dea8dac4465f53850987f6062a732dBrian Paul       ((target == GL_TEXTURE_RECTANGLE_NV ||
1302738318bb75dea8dac4465f53850987f6062a732dBrian Paul         target == GL_PROXY_TEXTURE_RECTANGLE_NV) && border != 0)) {
1303738318bb75dea8dac4465f53850987f6062a732dBrian Paul      if (!isProxy) {
1304738318bb75dea8dac4465f53850987f6062a732dBrian Paul         _mesa_error(ctx, GL_INVALID_VALUE,
1305738318bb75dea8dac4465f53850987f6062a732dBrian Paul                     "glTexImage%dD(border=%d)", dimensions, border);
1306738318bb75dea8dac4465f53850987f6062a732dBrian Paul      }
1307738318bb75dea8dac4465f53850987f6062a732dBrian Paul      return GL_TRUE;
1308738318bb75dea8dac4465f53850987f6062a732dBrian Paul   }
1309738318bb75dea8dac4465f53850987f6062a732dBrian Paul
1310973da83f6237b5af4a9ee77f32fdfa5c04ecabc8Brian Paul   if (width < 0 || height < 0 || depth < 0) {
1311f6e76fe9b5c4c57ac6dc81143b4474ebfee879d2Brian Paul      if (!isProxy) {
1312f6e76fe9b5c4c57ac6dc81143b4474ebfee879d2Brian Paul         _mesa_error(ctx, GL_INVALID_VALUE,
1313973da83f6237b5af4a9ee77f32fdfa5c04ecabc8Brian Paul                     "glTexImage%dD(width, height or depth < 0)", dimensions);
1314f6e76fe9b5c4c57ac6dc81143b4474ebfee879d2Brian Paul      }
1315f6e76fe9b5c4c57ac6dc81143b4474ebfee879d2Brian Paul      return GL_TRUE;
1316f6e76fe9b5c4c57ac6dc81143b4474ebfee879d2Brian Paul   }
1317f6e76fe9b5c4c57ac6dc81143b4474ebfee879d2Brian Paul
1318738318bb75dea8dac4465f53850987f6062a732dBrian Paul   /* Check target and call ctx->Driver.TestProxyTexImage() to check the
1319738318bb75dea8dac4465f53850987f6062a732dBrian Paul    * level, width, height and depth.
1320738318bb75dea8dac4465f53850987f6062a732dBrian Paul    */
1321738318bb75dea8dac4465f53850987f6062a732dBrian Paul   if (dimensions == 1) {
1322738318bb75dea8dac4465f53850987f6062a732dBrian Paul      if (target == GL_PROXY_TEXTURE_1D || target == GL_TEXTURE_1D) {
1323738318bb75dea8dac4465f53850987f6062a732dBrian Paul         sizeOK = ctx->Driver.TestProxyTexImage(ctx, GL_PROXY_TEXTURE_1D,
1324738318bb75dea8dac4465f53850987f6062a732dBrian Paul                                                level, internalFormat,
1325738318bb75dea8dac4465f53850987f6062a732dBrian Paul                                                format, type,
1326738318bb75dea8dac4465f53850987f6062a732dBrian Paul                                                width, 1, 1, border);
13278afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul      }
13288afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul      else {
132908836341788a9f9d638d9dc8328510ccd18ddeb5Brian Paul         _mesa_error( ctx, GL_INVALID_ENUM, "glTexImage1D(target)" );
1330afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         return GL_TRUE;
1331afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      }
1332afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   }
1333afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   else if (dimensions == 2) {
1334738318bb75dea8dac4465f53850987f6062a732dBrian Paul      if (target == GL_PROXY_TEXTURE_2D || target == GL_TEXTURE_2D) {
1335738318bb75dea8dac4465f53850987f6062a732dBrian Paul         sizeOK = ctx->Driver.TestProxyTexImage(ctx, GL_PROXY_TEXTURE_2D,
1336738318bb75dea8dac4465f53850987f6062a732dBrian Paul                                                level, internalFormat,
1337738318bb75dea8dac4465f53850987f6062a732dBrian Paul                                                format, type,
1338738318bb75dea8dac4465f53850987f6062a732dBrian Paul                                                width, height, 1, border);
1339738318bb75dea8dac4465f53850987f6062a732dBrian Paul      }
1340738318bb75dea8dac4465f53850987f6062a732dBrian Paul      else if (target == GL_PROXY_TEXTURE_CUBE_MAP_ARB ||
1341738318bb75dea8dac4465f53850987f6062a732dBrian Paul               (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
1342738318bb75dea8dac4465f53850987f6062a732dBrian Paul                target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB)) {
13438afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul         if (!ctx->Extensions.ARB_texture_cube_map) {
13448afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul            _mesa_error(ctx, GL_INVALID_ENUM, "glTexImage2D(target)");
13458afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul            return GL_TRUE;
13468afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul         }
1347738318bb75dea8dac4465f53850987f6062a732dBrian Paul         sizeOK = (width == height) &&
1348738318bb75dea8dac4465f53850987f6062a732dBrian Paul            ctx->Driver.TestProxyTexImage(ctx, GL_PROXY_TEXTURE_CUBE_MAP_ARB,
1349738318bb75dea8dac4465f53850987f6062a732dBrian Paul                                          level, internalFormat, format, type,
1350738318bb75dea8dac4465f53850987f6062a732dBrian Paul                                          width, height, 1, border);
13518afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul      }
1352738318bb75dea8dac4465f53850987f6062a732dBrian Paul      else if (target == GL_PROXY_TEXTURE_RECTANGLE_NV ||
1353738318bb75dea8dac4465f53850987f6062a732dBrian Paul               target == GL_TEXTURE_RECTANGLE_NV) {
13548afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul         if (!ctx->Extensions.NV_texture_rectangle) {
13558afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul            _mesa_error(ctx, GL_INVALID_ENUM, "glTexImage2D(target)");
13568afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul            return GL_TRUE;
13578afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul         }
1358738318bb75dea8dac4465f53850987f6062a732dBrian Paul         sizeOK = ctx->Driver.TestProxyTexImage(ctx,
1359738318bb75dea8dac4465f53850987f6062a732dBrian Paul                                                GL_PROXY_TEXTURE_RECTANGLE_NV,
1360738318bb75dea8dac4465f53850987f6062a732dBrian Paul                                                level, internalFormat,
1361738318bb75dea8dac4465f53850987f6062a732dBrian Paul                                                format, type,
1362738318bb75dea8dac4465f53850987f6062a732dBrian Paul                                                width, height, 1, border);
13638afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul      }
13648afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul      else {
13658afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul         _mesa_error(ctx, GL_INVALID_ENUM, "glTexImage2D(target)");
13668afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul         return GL_TRUE;
13678afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul      }
1368afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   }
1369afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   else if (dimensions == 3) {
1370738318bb75dea8dac4465f53850987f6062a732dBrian Paul      if (target == GL_PROXY_TEXTURE_3D || target == GL_TEXTURE_3D) {
1371738318bb75dea8dac4465f53850987f6062a732dBrian Paul         sizeOK = ctx->Driver.TestProxyTexImage(ctx, GL_PROXY_TEXTURE_3D,
1372738318bb75dea8dac4465f53850987f6062a732dBrian Paul                                                level, internalFormat,
1373738318bb75dea8dac4465f53850987f6062a732dBrian Paul                                                format, type,
1374738318bb75dea8dac4465f53850987f6062a732dBrian Paul                                                width, height, depth, border);
13758afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul      }
13768afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul      else {
137708836341788a9f9d638d9dc8328510ccd18ddeb5Brian Paul         _mesa_error( ctx, GL_INVALID_ENUM, "glTexImage3D(target)" );
1378afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         return GL_TRUE;
1379afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      }
1380afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   }
1381afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   else {
138208836341788a9f9d638d9dc8328510ccd18ddeb5Brian Paul      _mesa_problem( ctx, "bad dims in texture_error_check" );
1383afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      return GL_TRUE;
1384afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   }
1385afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
1386738318bb75dea8dac4465f53850987f6062a732dBrian Paul   if (!sizeOK) {
1387c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      if (!isProxy) {
13884e9676fb13f60ecdbc247b120031f18cd3febcb0Brian Paul         _mesa_error(ctx, GL_INVALID_VALUE,
1389738318bb75dea8dac4465f53850987f6062a732dBrian Paul                     "glTexImage%dD(level=%d, width=%d, height=%d, depth=%d)",
1390738318bb75dea8dac4465f53850987f6062a732dBrian Paul                     dimensions, level, width, height, depth);
1391c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      }
1392afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      return GL_TRUE;
1393afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   }
1394afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
1395738318bb75dea8dac4465f53850987f6062a732dBrian Paul   /* Check internalFormat */
139689fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   if (_mesa_base_tex_format(ctx, internalFormat) < 0) {
1397c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      if (!isProxy) {
13984e9676fb13f60ecdbc247b120031f18cd3febcb0Brian Paul         _mesa_error(ctx, GL_INVALID_VALUE,
13994e9676fb13f60ecdbc247b120031f18cd3febcb0Brian Paul                     "glTexImage%dD(internalFormat=0x%x)",
14004e9676fb13f60ecdbc247b120031f18cd3febcb0Brian Paul                     dimensions, internalFormat);
1401c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      }
1402afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      return GL_TRUE;
1403afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   }
1404afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
1405738318bb75dea8dac4465f53850987f6062a732dBrian Paul   /* Check incoming image format and type */
1406f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   if (!_mesa_is_legal_format_and_type(ctx, format, type)) {
140738f28665bf9fb5b2464738ca5074848ec2777ae1Gareth Hughes      /* Yes, generate GL_INVALID_OPERATION, not GL_INVALID_ENUM, if there
140838f28665bf9fb5b2464738ca5074848ec2777ae1Gareth Hughes       * is a type/format mismatch.  See 1.2 spec page 94, sec 3.6.4.
140938f28665bf9fb5b2464738ca5074848ec2777ae1Gareth Hughes       */
141038f28665bf9fb5b2464738ca5074848ec2777ae1Gareth Hughes      if (!isProxy) {
141189fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         _mesa_error(ctx, GL_INVALID_OPERATION,
14124e9676fb13f60ecdbc247b120031f18cd3febcb0Brian Paul                     "glTexImage%dD(format or type)", dimensions);
1413c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      }
141438f28665bf9fb5b2464738ca5074848ec2777ae1Gareth Hughes      return GL_TRUE;
1415afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   }
1416afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
1417601df9c742939c1f77de489561fe3e1d02f49618Brian Paul   /* make sure internal format and format basically agree */
1418714c36c1202cd49c58cf6462afd391fd059b96c2Daniel Borca   colorFormat = is_color_format(format);
1419714c36c1202cd49c58cf6462afd391fd059b96c2Daniel Borca   indexFormat = is_index_format(format);
1420714c36c1202cd49c58cf6462afd391fd059b96c2Daniel Borca   if ((is_color_format(internalFormat) && !colorFormat && !indexFormat) ||
1421714c36c1202cd49c58cf6462afd391fd059b96c2Daniel Borca       (is_index_format(internalFormat) && !indexFormat) ||
1422601df9c742939c1f77de489561fe3e1d02f49618Brian Paul       (is_depth_format(internalFormat) != is_depth_format(format)) ||
14231ad7b99925e044f82e635f746c1ef2df77f69ac9Brian Paul       (is_ycbcr_format(internalFormat) != is_ycbcr_format(format)) ||
14241ad7b99925e044f82e635f746c1ef2df77f69ac9Brian Paul       (is_depthstencil_format(internalFormat) != is_depthstencil_format(format))) {
1425601df9c742939c1f77de489561fe3e1d02f49618Brian Paul      if (!isProxy)
1426601df9c742939c1f77de489561fe3e1d02f49618Brian Paul         _mesa_error(ctx, GL_INVALID_OPERATION,
1427601df9c742939c1f77de489561fe3e1d02f49618Brian Paul                     "glTexImage(internalFormat/format)");
1428601df9c742939c1f77de489561fe3e1d02f49618Brian Paul      return GL_TRUE;
1429601df9c742939c1f77de489561fe3e1d02f49618Brian Paul   }
1430601df9c742939c1f77de489561fe3e1d02f49618Brian Paul
1431601df9c742939c1f77de489561fe3e1d02f49618Brian Paul   /* additional checks for ycbcr textures */
1432601df9c742939c1f77de489561fe3e1d02f49618Brian Paul   if (internalFormat == GL_YCBCR_MESA) {
1433c5b995066020191982b2315fc45d05e068eee761Brian Paul      ASSERT(ctx->Extensions.MESA_ycbcr_texture);
1434601df9c742939c1f77de489561fe3e1d02f49618Brian Paul      if (type != GL_UNSIGNED_SHORT_8_8_MESA &&
1435601df9c742939c1f77de489561fe3e1d02f49618Brian Paul          type != GL_UNSIGNED_SHORT_8_8_REV_MESA) {
1436c5b995066020191982b2315fc45d05e068eee761Brian Paul         char message[100];
143736b518a7ca98e49d95c3fe54544700004716cdefBrian Paul         _mesa_sprintf(message,
1438601df9c742939c1f77de489561fe3e1d02f49618Brian Paul                 "glTexImage%d(format/type YCBCR mismatch", dimensions);
1439c5b995066020191982b2315fc45d05e068eee761Brian Paul         _mesa_error(ctx, GL_INVALID_ENUM, message);
1440c5b995066020191982b2315fc45d05e068eee761Brian Paul         return GL_TRUE; /* error */
1441c5b995066020191982b2315fc45d05e068eee761Brian Paul      }
1442d8419c730e73c3be2eadfb0bee176ab06885766aBrian Paul      if (target != GL_TEXTURE_2D &&
1443d8419c730e73c3be2eadfb0bee176ab06885766aBrian Paul          target != GL_PROXY_TEXTURE_2D &&
1444d8419c730e73c3be2eadfb0bee176ab06885766aBrian Paul          target != GL_TEXTURE_RECTANGLE_NV &&
1445d8419c730e73c3be2eadfb0bee176ab06885766aBrian Paul          target != GL_PROXY_TEXTURE_RECTANGLE_NV) {
1446d8419c730e73c3be2eadfb0bee176ab06885766aBrian Paul         if (!isProxy)
1447d8419c730e73c3be2eadfb0bee176ab06885766aBrian Paul            _mesa_error(ctx, GL_INVALID_ENUM, "glTexImage(target)");
1448d8419c730e73c3be2eadfb0bee176ab06885766aBrian Paul         return GL_TRUE;
1449d8419c730e73c3be2eadfb0bee176ab06885766aBrian Paul      }
1450d8419c730e73c3be2eadfb0bee176ab06885766aBrian Paul      if (border != 0) {
1451d8419c730e73c3be2eadfb0bee176ab06885766aBrian Paul         if (!isProxy) {
1452d8419c730e73c3be2eadfb0bee176ab06885766aBrian Paul            char message[100];
145336b518a7ca98e49d95c3fe54544700004716cdefBrian Paul            _mesa_sprintf(message,
1454d8419c730e73c3be2eadfb0bee176ab06885766aBrian Paul                    "glTexImage%d(format=GL_YCBCR_MESA and border=%d)",
1455d8419c730e73c3be2eadfb0bee176ab06885766aBrian Paul                    dimensions, border);
1456d8419c730e73c3be2eadfb0bee176ab06885766aBrian Paul            _mesa_error(ctx, GL_INVALID_VALUE, message);
1457d8419c730e73c3be2eadfb0bee176ab06885766aBrian Paul         }
1458d8419c730e73c3be2eadfb0bee176ab06885766aBrian Paul         return GL_TRUE;
1459d8419c730e73c3be2eadfb0bee176ab06885766aBrian Paul      }
1460c5b995066020191982b2315fc45d05e068eee761Brian Paul   }
1461c5b995066020191982b2315fc45d05e068eee761Brian Paul
1462601df9c742939c1f77de489561fe3e1d02f49618Brian Paul   /* additional checks for depth textures */
1463601df9c742939c1f77de489561fe3e1d02f49618Brian Paul   if (_mesa_base_tex_format(ctx, internalFormat) == GL_DEPTH_COMPONENT) {
1464601df9c742939c1f77de489561fe3e1d02f49618Brian Paul      /* Only 1D and 2D textures supported */
1465601df9c742939c1f77de489561fe3e1d02f49618Brian Paul      if (target != GL_TEXTURE_1D &&
1466601df9c742939c1f77de489561fe3e1d02f49618Brian Paul          target != GL_PROXY_TEXTURE_1D &&
1467601df9c742939c1f77de489561fe3e1d02f49618Brian Paul          target != GL_TEXTURE_2D &&
1468601df9c742939c1f77de489561fe3e1d02f49618Brian Paul          target != GL_PROXY_TEXTURE_2D) {
1469601df9c742939c1f77de489561fe3e1d02f49618Brian Paul         if (!isProxy)
1470601df9c742939c1f77de489561fe3e1d02f49618Brian Paul            _mesa_error(ctx, GL_INVALID_ENUM,
1471601df9c742939c1f77de489561fe3e1d02f49618Brian Paul                        "glTexImage(target/internalFormat)");
1472601df9c742939c1f77de489561fe3e1d02f49618Brian Paul         return GL_TRUE;
1473601df9c742939c1f77de489561fe3e1d02f49618Brian Paul      }
1474601df9c742939c1f77de489561fe3e1d02f49618Brian Paul   }
1475601df9c742939c1f77de489561fe3e1d02f49618Brian Paul
1476601df9c742939c1f77de489561fe3e1d02f49618Brian Paul   /* additional checks for compressed textures */
147740bd9d0b190e11d39350d1b08d2c2b28e3040bcaDaniel Borca   if (is_compressed_format(ctx, internalFormat)) {
147889fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      if (target == GL_TEXTURE_2D || target == GL_PROXY_TEXTURE_2D) {
147989fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         /* OK */
148089fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      }
148189fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      else if (ctx->Extensions.ARB_texture_cube_map &&
148289fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul               (target == GL_PROXY_TEXTURE_CUBE_MAP ||
148389fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul                (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X &&
148489fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul                 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z))) {
148589fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         /* OK */
148689fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      }
148789fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      else {
148889fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         if (!isProxy) {
148989fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul            _mesa_error(ctx, GL_INVALID_ENUM,
149089fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul                        "glTexImage%d(target)", dimensions);
149189fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul            return GL_TRUE;
149289fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         }
149389fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      }
149489fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      if (border != 0) {
149589fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         if (!isProxy) {
149689fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul            _mesa_error(ctx, GL_INVALID_OPERATION,
149789fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul                        "glTexImage%D(border!=0)", dimensions);
149889fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         }
149989fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         return GL_TRUE;
150089fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      }
150189fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   }
150289fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul
1503afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   /* if we get here, the parameters are OK */
1504afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   return GL_FALSE;
1505afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg}
1506afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
1507afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
15086dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/**
1509c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul * Test glTexSubImage[123]D() parameters for errors.
15106dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell *
15116dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param ctx GL context.
15126dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param dimensions texture image dimensions (must be 1, 2 or 3).
15136dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param target texture target given by the user.
15146dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param level image level given by the user.
15156dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param xoffset sub-image x offset given by the user.
15166dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param yoffset sub-image y offset given by the user.
15176dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param zoffset sub-image z offset given by the user.
15186dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param format pixel data format given by the user.
15196dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param type pixel data type given by the user.
15206dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param width image width given by the user.
15216dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param height image height given by the user.
15226dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param depth image depth given by the user.
15236dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell *
15246dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
15256dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell *
15266dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Verifies each of the parameters against the constants specified in
15276dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * __GLcontextRec::Const and the supported extensions, and according to the
15286dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * OpenGL specification.
1529afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg */
1530c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paulstatic GLboolean
1531fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paulsubtexture_error_check( GLcontext *ctx, GLuint dimensions,
1532c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul                        GLenum target, GLint level,
1533c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul                        GLint xoffset, GLint yoffset, GLint zoffset,
1534c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul                        GLint width, GLint height, GLint depth,
1535c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul                        GLenum format, GLenum type )
1536afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg{
1537afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   struct gl_texture_unit *texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1538c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   struct gl_texture_image *destTex;
1539afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
1540738318bb75dea8dac4465f53850987f6062a732dBrian Paul   /* Check target */
1541c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   if (dimensions == 1) {
1542738318bb75dea8dac4465f53850987f6062a732dBrian Paul      if (target != GL_TEXTURE_1D) {
154308836341788a9f9d638d9dc8328510ccd18ddeb5Brian Paul         _mesa_error( ctx, GL_INVALID_ENUM, "glTexSubImage1D(target)" );
1544c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         return GL_TRUE;
1545afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      }
1546c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   }
1547c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   else if (dimensions == 2) {
1548738318bb75dea8dac4465f53850987f6062a732dBrian Paul      if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
1549d8419c730e73c3be2eadfb0bee176ab06885766aBrian Paul          target <=GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB) {
1550738318bb75dea8dac4465f53850987f6062a732dBrian Paul         if (!ctx->Extensions.ARB_texture_cube_map) {
1551738318bb75dea8dac4465f53850987f6062a732dBrian Paul            _mesa_error( ctx, GL_INVALID_ENUM, "glTexSubImage2D(target)" );
1552738318bb75dea8dac4465f53850987f6062a732dBrian Paul            return GL_TRUE;
1553738318bb75dea8dac4465f53850987f6062a732dBrian Paul         }
1554fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul      }
1555d8419c730e73c3be2eadfb0bee176ab06885766aBrian Paul      else if (ctx->Extensions.NV_texture_rectangle &&
1556d8419c730e73c3be2eadfb0bee176ab06885766aBrian Paul               target == GL_TEXTURE_RECTANGLE_NV) {
1557738318bb75dea8dac4465f53850987f6062a732dBrian Paul         if (!ctx->Extensions.NV_texture_rectangle) {
1558738318bb75dea8dac4465f53850987f6062a732dBrian Paul            _mesa_error( ctx, GL_INVALID_ENUM, "glTexSubImage2D(target)" );
1559738318bb75dea8dac4465f53850987f6062a732dBrian Paul            return GL_TRUE;
1560738318bb75dea8dac4465f53850987f6062a732dBrian Paul         }
1561d8419c730e73c3be2eadfb0bee176ab06885766aBrian Paul      }
1562738318bb75dea8dac4465f53850987f6062a732dBrian Paul      else if (target != GL_TEXTURE_2D) {
156308836341788a9f9d638d9dc8328510ccd18ddeb5Brian Paul         _mesa_error( ctx, GL_INVALID_ENUM, "glTexSubImage2D(target)" );
1564c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         return GL_TRUE;
1565afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      }
1566c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   }
1567c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   else if (dimensions == 3) {
1568738318bb75dea8dac4465f53850987f6062a732dBrian Paul      if (target != GL_TEXTURE_3D) {
156908836341788a9f9d638d9dc8328510ccd18ddeb5Brian Paul         _mesa_error( ctx, GL_INVALID_ENUM, "glTexSubImage3D(target)" );
1570c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         return GL_TRUE;
1571afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      }
1572c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   }
1573c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   else {
1574738318bb75dea8dac4465f53850987f6062a732dBrian Paul      _mesa_problem( ctx, "invalid dims in texture_error_check" );
1575c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      return GL_TRUE;
1576c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   }
1577afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
1578738318bb75dea8dac4465f53850987f6062a732dBrian Paul   /* Basic level check */
1579738318bb75dea8dac4465f53850987f6062a732dBrian Paul   if (level < 0 || level >= MAX_TEXTURE_LEVELS) {
15804e9676fb13f60ecdbc247b120031f18cd3febcb0Brian Paul      _mesa_error(ctx, GL_INVALID_ENUM, "glTexSubImage2D(level=%d)", level);
1581c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      return GL_TRUE;
1582c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   }
1583afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
1584c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   if (width < 0) {
15854e9676fb13f60ecdbc247b120031f18cd3febcb0Brian Paul      _mesa_error(ctx, GL_INVALID_VALUE,
15864e9676fb13f60ecdbc247b120031f18cd3febcb0Brian Paul                  "glTexSubImage%dD(width=%d)", dimensions, width);
1587c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      return GL_TRUE;
1588c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   }
1589c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   if (height < 0 && dimensions > 1) {
15904e9676fb13f60ecdbc247b120031f18cd3febcb0Brian Paul      _mesa_error(ctx, GL_INVALID_VALUE,
15914e9676fb13f60ecdbc247b120031f18cd3febcb0Brian Paul                  "glTexSubImage%dD(height=%d)", dimensions, height);
1592c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      return GL_TRUE;
1593c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   }
1594c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   if (depth < 0 && dimensions > 2) {
1595738318bb75dea8dac4465f53850987f6062a732dBrian Paul      _mesa_error(ctx, GL_INVALID_VALUE,
1596738318bb75dea8dac4465f53850987f6062a732dBrian Paul                  "glTexSubImage%dD(depth=%d)", dimensions, depth);
1597c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      return GL_TRUE;
1598c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   }
1599afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
1600f2718b0966f54049056e16e7cca08718341557b2Brian Paul   destTex = _mesa_select_tex_image(ctx, texUnit, target, level);
1601f2718b0966f54049056e16e7cca08718341557b2Brian Paul
1602c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   if (!destTex) {
1603738318bb75dea8dac4465f53850987f6062a732dBrian Paul      /* undefined image level */
1604738318bb75dea8dac4465f53850987f6062a732dBrian Paul      _mesa_error(ctx, GL_INVALID_OPERATION, "glTexSubImage%dD", dimensions);
1605c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      return GL_TRUE;
1606c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   }
1607afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
1608c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   if (xoffset < -((GLint)destTex->Border)) {
1609738318bb75dea8dac4465f53850987f6062a732dBrian Paul      _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage%dD(xoffset)",
1610738318bb75dea8dac4465f53850987f6062a732dBrian Paul                  dimensions);
1611c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      return GL_TRUE;
1612afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   }
1613c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   if (xoffset + width > (GLint) (destTex->Width + destTex->Border)) {
1614738318bb75dea8dac4465f53850987f6062a732dBrian Paul      _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage%dD(xoffset+width)",
1615738318bb75dea8dac4465f53850987f6062a732dBrian Paul                  dimensions);
1616c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      return GL_TRUE;
1617c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   }
1618c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   if (dimensions > 1) {
1619c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      if (yoffset < -((GLint)destTex->Border)) {
1620738318bb75dea8dac4465f53850987f6062a732dBrian Paul         _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage%dD(yoffset)",
1621738318bb75dea8dac4465f53850987f6062a732dBrian Paul                     dimensions);
1622c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         return GL_TRUE;
1623afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      }
1624c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      if (yoffset + height > (GLint) (destTex->Height + destTex->Border)) {
1625738318bb75dea8dac4465f53850987f6062a732dBrian Paul         _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage%dD(yoffset+height)",
1626738318bb75dea8dac4465f53850987f6062a732dBrian Paul                     dimensions);
1627c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         return GL_TRUE;
1628afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      }
1629afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   }
1630c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   if (dimensions > 2) {
1631c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      if (zoffset < -((GLint)destTex->Border)) {
163208836341788a9f9d638d9dc8328510ccd18ddeb5Brian Paul         _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage3D(zoffset)");
1633c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         return GL_TRUE;
1634c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      }
163538f28665bf9fb5b2464738ca5074848ec2777ae1Gareth Hughes      if (zoffset + depth  > (GLint) (destTex->Depth + destTex->Border)) {
163608836341788a9f9d638d9dc8328510ccd18ddeb5Brian Paul         _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage3D(zoffset+depth)");
1637c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         return GL_TRUE;
1638c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      }
1639afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   }
1640afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
1641f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   if (!_mesa_is_legal_format_and_type(ctx, format, type)) {
16424e9676fb13f60ecdbc247b120031f18cd3febcb0Brian Paul      _mesa_error(ctx, GL_INVALID_ENUM,
16434e9676fb13f60ecdbc247b120031f18cd3febcb0Brian Paul                  "glTexSubImage%dD(format or type)", dimensions);
164438f28665bf9fb5b2464738ca5074848ec2777ae1Gareth Hughes      return GL_TRUE;
1645c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   }
1646afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
164789fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   if (destTex->IsCompressed) {
164889fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      const struct gl_texture_unit *texUnit;
164989fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      const struct gl_texture_image *texImage;
165089fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
165189fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
165289fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul
165389fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      if (target == GL_TEXTURE_2D || target == GL_PROXY_TEXTURE_2D) {
165489fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         /* OK */
165589fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      }
165689fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      else if (ctx->Extensions.ARB_texture_cube_map &&
165789fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul               (target == GL_PROXY_TEXTURE_CUBE_MAP ||
165889fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul                (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X &&
165989fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul                 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z))) {
166089fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         /* OK */
166189fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      }
166289fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      else {
166389fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         _mesa_error(ctx, GL_INVALID_ENUM,
166489fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul                     "glTexSubImage%D(target)", dimensions);
1665e4276667dafc8de0c6e64af8300fc7598437de6eBrian Paul         return GL_TRUE;
1666e4276667dafc8de0c6e64af8300fc7598437de6eBrian Paul      }
166789fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      /* offset must be multiple of 4 */
166889fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      if ((xoffset & 3) || (yoffset & 3)) {
1669e4276667dafc8de0c6e64af8300fc7598437de6eBrian Paul         _mesa_error(ctx, GL_INVALID_OPERATION,
167089fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul                     "glTexSubImage%D(xoffset or yoffset)", dimensions);
1671e4276667dafc8de0c6e64af8300fc7598437de6eBrian Paul         return GL_TRUE;
1672e4276667dafc8de0c6e64af8300fc7598437de6eBrian Paul      }
167389fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      /* size must be multiple of 4 or equal to whole texture size */
1674798d83cf67098a7df293c711e0d2c8c296fc4c8fKarl Schultz      if ((width & 3) && (GLuint) width != texImage->Width) {
1675e4276667dafc8de0c6e64af8300fc7598437de6eBrian Paul         _mesa_error(ctx, GL_INVALID_OPERATION,
167689fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul                     "glTexSubImage%D(width)", dimensions);
1677e4276667dafc8de0c6e64af8300fc7598437de6eBrian Paul         return GL_TRUE;
167889fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      }
1679798d83cf67098a7df293c711e0d2c8c296fc4c8fKarl Schultz      if ((height & 3) && (GLuint) height != texImage->Height) {
168089fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         _mesa_error(ctx, GL_INVALID_OPERATION,
168189fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul                     "glTexSubImage%D(width)", dimensions);
168289fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         return GL_TRUE;
168389fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      }
1684e4276667dafc8de0c6e64af8300fc7598437de6eBrian Paul   }
1685e4276667dafc8de0c6e64af8300fc7598437de6eBrian Paul
1686c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   return GL_FALSE;
1687c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul}
1688afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
1689afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
16906dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/**
1691c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul * Test glCopyTexImage[12]D() parameters for errors.
16926dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell *
16936dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param ctx GL context.
16946dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param dimensions texture image dimensions (must be 1, 2 or 3).
16956dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param target texture target given by the user.
16966dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param level image level given by the user.
16976dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param internalFormat internal format given by the user.
16986dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param width image width given by the user.
16996dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param height image height given by the user.
17006dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param depth image depth given by the user.
17016dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param border texture border.
17026dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell *
17036dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
17046dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell *
17056dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Verifies each of the parameters against the constants specified in
17066dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * __GLcontextRec::Const and the supported extensions, and according to the
17076dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * OpenGL specification.
1708afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg */
1709c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paulstatic GLboolean
1710fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paulcopytexture_error_check( GLcontext *ctx, GLuint dimensions,
1711c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul                         GLenum target, GLint level, GLint internalFormat,
1712c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul                         GLint width, GLint height, GLint border )
1713afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg{
17140899afae33a29d1f6d15ebc96dc6ea8a8371cdb6Brian Paul   GLenum type;
1715738318bb75dea8dac4465f53850987f6062a732dBrian Paul   GLboolean sizeOK;
17160899afae33a29d1f6d15ebc96dc6ea8a8371cdb6Brian Paul   GLint format;
1717afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
1718738318bb75dea8dac4465f53850987f6062a732dBrian Paul   /* Basic level check (more checking in ctx->Driver.TestProxyTexImage) */
1719738318bb75dea8dac4465f53850987f6062a732dBrian Paul   if (level < 0 || level >= MAX_TEXTURE_LEVELS) {
172034bd1233a9874fe12a822c4fcb926d48456e1f29Brian Paul   /* Check that the source buffer is complete */
172134bd1233a9874fe12a822c4fcb926d48456e1f29Brian Paul   if (ctx->ReadBuffer->Name) {
172234bd1233a9874fe12a822c4fcb926d48456e1f29Brian Paul      _mesa_test_framebuffer_completeness(ctx, ctx->ReadBuffer);
172334bd1233a9874fe12a822c4fcb926d48456e1f29Brian Paul      if (ctx->ReadBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
172434bd1233a9874fe12a822c4fcb926d48456e1f29Brian Paul         _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
172534bd1233a9874fe12a822c4fcb926d48456e1f29Brian Paul                     "glCopyTexImage%dD(invalid readbuffer)", dimensions);
172634bd1233a9874fe12a822c4fcb926d48456e1f29Brian Paul         return GL_TRUE;
172734bd1233a9874fe12a822c4fcb926d48456e1f29Brian Paul      }
172834bd1233a9874fe12a822c4fcb926d48456e1f29Brian Paul   }
172934bd1233a9874fe12a822c4fcb926d48456e1f29Brian Paul
1730738318bb75dea8dac4465f53850987f6062a732dBrian Paul      _mesa_error(ctx, GL_INVALID_VALUE,
1731738318bb75dea8dac4465f53850987f6062a732dBrian Paul                  "glCopyTexImage%dD(level=%d)", dimensions, level);
1732738318bb75dea8dac4465f53850987f6062a732dBrian Paul      return GL_TRUE;
1733738318bb75dea8dac4465f53850987f6062a732dBrian Paul   }
1734738318bb75dea8dac4465f53850987f6062a732dBrian Paul
1735738318bb75dea8dac4465f53850987f6062a732dBrian Paul   /* Check border */
1736738318bb75dea8dac4465f53850987f6062a732dBrian Paul   if (border < 0 || border > 1 ||
1737738318bb75dea8dac4465f53850987f6062a732dBrian Paul       ((target == GL_TEXTURE_RECTANGLE_NV ||
1738738318bb75dea8dac4465f53850987f6062a732dBrian Paul         target == GL_PROXY_TEXTURE_RECTANGLE_NV) && border != 0)) {
1739738318bb75dea8dac4465f53850987f6062a732dBrian Paul      return GL_TRUE;
1740738318bb75dea8dac4465f53850987f6062a732dBrian Paul   }
1741738318bb75dea8dac4465f53850987f6062a732dBrian Paul
1742738318bb75dea8dac4465f53850987f6062a732dBrian Paul   format = _mesa_base_tex_format(ctx, internalFormat);
17430899afae33a29d1f6d15ebc96dc6ea8a8371cdb6Brian Paul   if (format < 0) {
17440899afae33a29d1f6d15ebc96dc6ea8a8371cdb6Brian Paul      _mesa_error(ctx, GL_INVALID_VALUE,
17450899afae33a29d1f6d15ebc96dc6ea8a8371cdb6Brian Paul                  "glCopyTexImage%dD(internalFormat)", dimensions);
17460899afae33a29d1f6d15ebc96dc6ea8a8371cdb6Brian Paul      return GL_TRUE;
17470899afae33a29d1f6d15ebc96dc6ea8a8371cdb6Brian Paul   }
17480899afae33a29d1f6d15ebc96dc6ea8a8371cdb6Brian Paul
17490899afae33a29d1f6d15ebc96dc6ea8a8371cdb6Brian Paul   /* NOTE: the format and type aren't really significant for
17500899afae33a29d1f6d15ebc96dc6ea8a8371cdb6Brian Paul    * TestProxyTexImage().  Only the internalformat really matters.
175134bd1233a9874fe12a822c4fcb926d48456e1f29Brian Paul   if (!_mesa_source_buffer_exists(ctx, format)) {
175234bd1233a9874fe12a822c4fcb926d48456e1f29Brian Paul      _mesa_error(ctx, GL_INVALID_OPERATION,
175334bd1233a9874fe12a822c4fcb926d48456e1f29Brian Paul                  "glCopyTexImage%dD(missing readbuffer)", dimensions);
175434bd1233a9874fe12a822c4fcb926d48456e1f29Brian Paul      return GL_TRUE;
175534bd1233a9874fe12a822c4fcb926d48456e1f29Brian Paul   }
175634bd1233a9874fe12a822c4fcb926d48456e1f29Brian Paul
17570899afae33a29d1f6d15ebc96dc6ea8a8371cdb6Brian Paul    */
1758738318bb75dea8dac4465f53850987f6062a732dBrian Paul   type = GL_FLOAT;
1759738318bb75dea8dac4465f53850987f6062a732dBrian Paul
1760738318bb75dea8dac4465f53850987f6062a732dBrian Paul   /* Check target and call ctx->Driver.TestProxyTexImage() to check the
1761738318bb75dea8dac4465f53850987f6062a732dBrian Paul    * level, width, height and depth.
1762738318bb75dea8dac4465f53850987f6062a732dBrian Paul    */
1763fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul   if (dimensions == 1) {
1764738318bb75dea8dac4465f53850987f6062a732dBrian Paul      if (target == GL_TEXTURE_1D) {
1765738318bb75dea8dac4465f53850987f6062a732dBrian Paul         sizeOK = ctx->Driver.TestProxyTexImage(ctx, GL_PROXY_TEXTURE_1D,
1766738318bb75dea8dac4465f53850987f6062a732dBrian Paul                                                level, internalFormat,
1767738318bb75dea8dac4465f53850987f6062a732dBrian Paul                                                format, type,
1768738318bb75dea8dac4465f53850987f6062a732dBrian Paul                                                width, 1, 1, border);
1769738318bb75dea8dac4465f53850987f6062a732dBrian Paul      }
1770738318bb75dea8dac4465f53850987f6062a732dBrian Paul      else {
177108836341788a9f9d638d9dc8328510ccd18ddeb5Brian Paul         _mesa_error( ctx, GL_INVALID_ENUM, "glCopyTexImage1D(target)" );
1772fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul         return GL_TRUE;
1773fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul      }
1774c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   }
1775fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul   else if (dimensions == 2) {
1776738318bb75dea8dac4465f53850987f6062a732dBrian Paul      if (target == GL_TEXTURE_2D) {
1777738318bb75dea8dac4465f53850987f6062a732dBrian Paul         sizeOK = ctx->Driver.TestProxyTexImage(ctx, GL_PROXY_TEXTURE_2D,
1778738318bb75dea8dac4465f53850987f6062a732dBrian Paul                                                level, internalFormat,
1779738318bb75dea8dac4465f53850987f6062a732dBrian Paul                                                format, type,
1780738318bb75dea8dac4465f53850987f6062a732dBrian Paul                                                width, height, 1, border);
1781738318bb75dea8dac4465f53850987f6062a732dBrian Paul      }
1782738318bb75dea8dac4465f53850987f6062a732dBrian Paul      else if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
1783738318bb75dea8dac4465f53850987f6062a732dBrian Paul               target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB) {
17848afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul         if (!ctx->Extensions.ARB_texture_cube_map) {
17858afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul            _mesa_error( ctx, GL_INVALID_ENUM, "glCopyTexImage2D(target)" );
17868afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul            return GL_TRUE;
17878afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul         }
1788738318bb75dea8dac4465f53850987f6062a732dBrian Paul         sizeOK = (width == height) &&
1789738318bb75dea8dac4465f53850987f6062a732dBrian Paul            ctx->Driver.TestProxyTexImage(ctx, GL_PROXY_TEXTURE_CUBE_MAP_ARB,
1790738318bb75dea8dac4465f53850987f6062a732dBrian Paul                                          level, internalFormat, format, type,
1791738318bb75dea8dac4465f53850987f6062a732dBrian Paul                                          width, height, 1, border);
17928afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul      }
17938afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul      else if (target == GL_TEXTURE_RECTANGLE_NV) {
17948afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul         if (!ctx->Extensions.NV_texture_rectangle) {
179508836341788a9f9d638d9dc8328510ccd18ddeb5Brian Paul            _mesa_error( ctx, GL_INVALID_ENUM, "glCopyTexImage2D(target)" );
1796fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul            return GL_TRUE;
1797fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul         }
1798738318bb75dea8dac4465f53850987f6062a732dBrian Paul         sizeOK = ctx->Driver.TestProxyTexImage(ctx,
1799738318bb75dea8dac4465f53850987f6062a732dBrian Paul                                                GL_PROXY_TEXTURE_RECTANGLE_NV,
1800738318bb75dea8dac4465f53850987f6062a732dBrian Paul                                                level, internalFormat,
1801738318bb75dea8dac4465f53850987f6062a732dBrian Paul                                                format, type,
1802738318bb75dea8dac4465f53850987f6062a732dBrian Paul                                                width, height, 1, border);
1803fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul      }
1804738318bb75dea8dac4465f53850987f6062a732dBrian Paul      else {
180508836341788a9f9d638d9dc8328510ccd18ddeb5Brian Paul         _mesa_error( ctx, GL_INVALID_ENUM, "glCopyTexImage2D(target)" );
1806fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul         return GL_TRUE;
1807fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul      }
1808c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   }
1809738318bb75dea8dac4465f53850987f6062a732dBrian Paul   else {
1810738318bb75dea8dac4465f53850987f6062a732dBrian Paul      _mesa_problem(ctx, "invalid dimensions in copytexture_error_check");
1811c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      return GL_TRUE;
1812c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   }
1813c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul
1814738318bb75dea8dac4465f53850987f6062a732dBrian Paul   if (!sizeOK) {
1815738318bb75dea8dac4465f53850987f6062a732dBrian Paul      if (dimensions == 1) {
18164e9676fb13f60ecdbc247b120031f18cd3febcb0Brian Paul         _mesa_error(ctx, GL_INVALID_VALUE,
1817738318bb75dea8dac4465f53850987f6062a732dBrian Paul                     "glCopyTexImage1D(width=%d)", width);
1818c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      }
1819738318bb75dea8dac4465f53850987f6062a732dBrian Paul      else {
1820738318bb75dea8dac4465f53850987f6062a732dBrian Paul         ASSERT(dimensions == 2);
1821738318bb75dea8dac4465f53850987f6062a732dBrian Paul         _mesa_error(ctx, GL_INVALID_VALUE,
1822738318bb75dea8dac4465f53850987f6062a732dBrian Paul                     "glCopyTexImage2D(width=%d, height=%d)", width, height);
1823ad817704fa2c6d8449f55565f07ab5592a574fb6Brian Paul      }
1824c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      return GL_TRUE;
1825c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   }
1826c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul
182740bd9d0b190e11d39350d1b08d2c2b28e3040bcaDaniel Borca   if (is_compressed_format(ctx, internalFormat)) {
182889fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      if (target != GL_TEXTURE_2D) {
182989fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         _mesa_error(ctx, GL_INVALID_ENUM,
183089fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul                     "glCopyTexImage%d(target)", dimensions);
183189fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         return GL_TRUE;
183289fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      }
183389fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      if (border != 0) {
183489fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         _mesa_error(ctx, GL_INVALID_OPERATION,
183589fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul                     "glCopyTexImage%D(border!=0)", dimensions);
183689fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         return GL_TRUE;
183789fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      }
183889fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   }
18390899afae33a29d1f6d15ebc96dc6ea8a8371cdb6Brian Paul   else if (is_depth_format(internalFormat)) {
18400899afae33a29d1f6d15ebc96dc6ea8a8371cdb6Brian Paul      /* make sure we have depth/stencil buffers */
18411a5f1bdb428ea3ded1e77796590014f89fe78f77Brian Paul      if (!ctx->ReadBuffer->_DepthBuffer) {
18420899afae33a29d1f6d15ebc96dc6ea8a8371cdb6Brian Paul         _mesa_error(ctx, GL_INVALID_OPERATION,
18430899afae33a29d1f6d15ebc96dc6ea8a8371cdb6Brian Paul                     "glCopyTexImage%D(no depth)", dimensions);
18440899afae33a29d1f6d15ebc96dc6ea8a8371cdb6Brian Paul         return GL_TRUE;
18450899afae33a29d1f6d15ebc96dc6ea8a8371cdb6Brian Paul      }
18460899afae33a29d1f6d15ebc96dc6ea8a8371cdb6Brian Paul   }
18470899afae33a29d1f6d15ebc96dc6ea8a8371cdb6Brian Paul   else if (is_depthstencil_format(internalFormat)) {
18480899afae33a29d1f6d15ebc96dc6ea8a8371cdb6Brian Paul      /* make sure we have depth/stencil buffers */
18491a5f1bdb428ea3ded1e77796590014f89fe78f77Brian Paul      if (!ctx->ReadBuffer->_DepthBuffer || !ctx->ReadBuffer->_StencilBuffer) {
18500899afae33a29d1f6d15ebc96dc6ea8a8371cdb6Brian Paul         _mesa_error(ctx, GL_INVALID_OPERATION,
18510899afae33a29d1f6d15ebc96dc6ea8a8371cdb6Brian Paul                     "glCopyTexImage%D(no depth/stencil buffer)", dimensions);
18520899afae33a29d1f6d15ebc96dc6ea8a8371cdb6Brian Paul         return GL_TRUE;
18530899afae33a29d1f6d15ebc96dc6ea8a8371cdb6Brian Paul      }
18540899afae33a29d1f6d15ebc96dc6ea8a8371cdb6Brian Paul   }
185589fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul
1856c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   /* if we get here, the parameters are OK */
1857c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   return GL_FALSE;
1858c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul}
1859c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul
1860c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul
18616dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/**
186234bd1233a9874fe12a822c4fcb926d48456e1f29Brian Paul * Test glCopyTexSubImage[12]D() parameters for errors.
18636dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell *
18646dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param ctx GL context.
18656dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param dimensions texture image dimensions (must be 1, 2 or 3).
18666dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param target texture target given by the user.
18676dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param level image level given by the user.
18686dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param xoffset sub-image x offset given by the user.
18696dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param yoffset sub-image y offset given by the user.
18706dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param zoffset sub-image z offset given by the user.
18716dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param width image width given by the user.
18726dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param height image height given by the user.
18736dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell *
18746dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
18756dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell *
18766dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Verifies each of the parameters against the constants specified in
18776dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * __GLcontextRec::Const and the supported extensions, and according to the
18786dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * OpenGL specification.
18796dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell */
1880c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paulstatic GLboolean
1881fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paulcopytexsubimage_error_check( GLcontext *ctx, GLuint dimensions,
1882c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul                             GLenum target, GLint level,
1883c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul                             GLint xoffset, GLint yoffset, GLint zoffset,
18845b37c322741f019118a618bc6220f37adba4fbcdBrian Paul                             GLsizei width, GLsizei height )
1885c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul{
1886c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   struct gl_texture_unit *texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1887c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   struct gl_texture_image *teximage;
1888c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul
1889738318bb75dea8dac4465f53850987f6062a732dBrian Paul   /* Check target */
189034bd1233a9874fe12a822c4fcb926d48456e1f29Brian Paul   /* Check that the source buffer is complete */
189134bd1233a9874fe12a822c4fcb926d48456e1f29Brian Paul   if (ctx->ReadBuffer->Name) {
189234bd1233a9874fe12a822c4fcb926d48456e1f29Brian Paul      _mesa_test_framebuffer_completeness(ctx, ctx->ReadBuffer);
189334bd1233a9874fe12a822c4fcb926d48456e1f29Brian Paul      if (ctx->ReadBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
189434bd1233a9874fe12a822c4fcb926d48456e1f29Brian Paul         _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
189534bd1233a9874fe12a822c4fcb926d48456e1f29Brian Paul                     "glCopyTexImage%dD(invalid readbuffer)", dimensions);
189634bd1233a9874fe12a822c4fcb926d48456e1f29Brian Paul         return GL_TRUE;
189734bd1233a9874fe12a822c4fcb926d48456e1f29Brian Paul      }
189834bd1233a9874fe12a822c4fcb926d48456e1f29Brian Paul   }
189934bd1233a9874fe12a822c4fcb926d48456e1f29Brian Paul
1900fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul   if (dimensions == 1) {
1901fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul      if (target != GL_TEXTURE_1D) {
190208836341788a9f9d638d9dc8328510ccd18ddeb5Brian Paul         _mesa_error( ctx, GL_INVALID_ENUM, "glCopyTexSubImage1D(target)" );
1903fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul         return GL_TRUE;
1904fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul      }
1905c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   }
1906fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul   else if (dimensions == 2) {
1907fa268adc9df30c8e3413990ee0e68c139ca0c069Brian Paul      if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
1908fa268adc9df30c8e3413990ee0e68c139ca0c069Brian Paul          target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB) {
1909fa268adc9df30c8e3413990ee0e68c139ca0c069Brian Paul         if (!ctx->Extensions.ARB_texture_cube_map) {
1910fa268adc9df30c8e3413990ee0e68c139ca0c069Brian Paul            _mesa_error( ctx, GL_INVALID_ENUM, "glCopyTexSubImage2D(target)" );
1911fa268adc9df30c8e3413990ee0e68c139ca0c069Brian Paul            return GL_TRUE;
1912fa268adc9df30c8e3413990ee0e68c139ca0c069Brian Paul         }
1913fa268adc9df30c8e3413990ee0e68c139ca0c069Brian Paul      }
1914fa268adc9df30c8e3413990ee0e68c139ca0c069Brian Paul      else if (target == GL_TEXTURE_RECTANGLE_NV) {
1915fa268adc9df30c8e3413990ee0e68c139ca0c069Brian Paul         if (!ctx->Extensions.NV_texture_rectangle) {
191608836341788a9f9d638d9dc8328510ccd18ddeb5Brian Paul            _mesa_error( ctx, GL_INVALID_ENUM, "glCopyTexSubImage2D(target)" );
1917fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul            return GL_TRUE;
1918fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul         }
1919fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul      }
1920fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul      else if (target != GL_TEXTURE_2D) {
192108836341788a9f9d638d9dc8328510ccd18ddeb5Brian Paul         _mesa_error( ctx, GL_INVALID_ENUM, "glCopyTexSubImage2D(target)" );
1922fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul         return GL_TRUE;
1923fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul      }
1924c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   }
1925fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul   else if (dimensions == 3) {
1926fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul      if (target != GL_TEXTURE_3D) {
192708836341788a9f9d638d9dc8328510ccd18ddeb5Brian Paul         _mesa_error( ctx, GL_INVALID_ENUM, "glCopyTexSubImage3D(target)" );
1928fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul         return GL_TRUE;
1929fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul      }
1930c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   }
1931c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul
1932738318bb75dea8dac4465f53850987f6062a732dBrian Paul   /* Check level */
1933738318bb75dea8dac4465f53850987f6062a732dBrian Paul   if (level < 0 || level >= MAX_TEXTURE_LEVELS) {
19344e9676fb13f60ecdbc247b120031f18cd3febcb0Brian Paul      _mesa_error(ctx, GL_INVALID_VALUE,
19354e9676fb13f60ecdbc247b120031f18cd3febcb0Brian Paul                  "glCopyTexSubImage%dD(level=%d)", dimensions, level);
1936c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      return GL_TRUE;
1937c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   }
1938c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul
1939738318bb75dea8dac4465f53850987f6062a732dBrian Paul   /* Check size */
1940c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   if (width < 0) {
19414e9676fb13f60ecdbc247b120031f18cd3febcb0Brian Paul      _mesa_error(ctx, GL_INVALID_VALUE,
19424e9676fb13f60ecdbc247b120031f18cd3febcb0Brian Paul                  "glCopyTexSubImage%dD(width=%d)", dimensions, width);
1943c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      return GL_TRUE;
1944c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   }
1945c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   if (dimensions > 1 && height < 0) {
19464e9676fb13f60ecdbc247b120031f18cd3febcb0Brian Paul      _mesa_error(ctx, GL_INVALID_VALUE,
19474e9676fb13f60ecdbc247b120031f18cd3febcb0Brian Paul                  "glCopyTexSubImage%dD(height=%d)", dimensions, height);
1948c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      return GL_TRUE;
1949c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   }
1950c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul
1951a852378a6289d154364dde440f89a39bbfc33e2dBrian Paul   teximage = _mesa_select_tex_image(ctx, texUnit, target, level);
1952c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   if (!teximage) {
19534e9676fb13f60ecdbc247b120031f18cd3febcb0Brian Paul      _mesa_error(ctx, GL_INVALID_OPERATION,
1954b9f75444e60477e7a523e7eec1bb49a8232fd4e7Brian Paul                  "glCopyTexSubImage%dD(undefined texture level: %d)",
1955b9f75444e60477e7a523e7eec1bb49a8232fd4e7Brian Paul                  dimensions, level);
1956c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      return GL_TRUE;
1957c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   }
1958c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul
1959c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   if (xoffset < -((GLint)teximage->Border)) {
19604e9676fb13f60ecdbc247b120031f18cd3febcb0Brian Paul      _mesa_error(ctx, GL_INVALID_VALUE,
19614e9676fb13f60ecdbc247b120031f18cd3febcb0Brian Paul                  "glCopyTexSubImage%dD(xoffset=%d)", dimensions, xoffset);
1962c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      return GL_TRUE;
1963c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   }
196438f28665bf9fb5b2464738ca5074848ec2777ae1Gareth Hughes   if (xoffset + width > (GLint) (teximage->Width + teximage->Border)) {
19654e9676fb13f60ecdbc247b120031f18cd3febcb0Brian Paul      _mesa_error(ctx, GL_INVALID_VALUE,
19664e9676fb13f60ecdbc247b120031f18cd3febcb0Brian Paul                  "glCopyTexSubImage%dD(xoffset+width)", dimensions);
1967c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      return GL_TRUE;
1968c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   }
1969c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   if (dimensions > 1) {
1970c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      if (yoffset < -((GLint)teximage->Border)) {
19714e9676fb13f60ecdbc247b120031f18cd3febcb0Brian Paul         _mesa_error(ctx, GL_INVALID_VALUE,
19724e9676fb13f60ecdbc247b120031f18cd3febcb0Brian Paul                     "glCopyTexSubImage%dD(yoffset=%d)", dimensions, yoffset);
1973c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         return GL_TRUE;
1974c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      }
1975c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      /* NOTE: we're adding the border here, not subtracting! */
197638f28665bf9fb5b2464738ca5074848ec2777ae1Gareth Hughes      if (yoffset + height > (GLint) (teximage->Height + teximage->Border)) {
19774e9676fb13f60ecdbc247b120031f18cd3febcb0Brian Paul         _mesa_error(ctx, GL_INVALID_VALUE,
19784e9676fb13f60ecdbc247b120031f18cd3febcb0Brian Paul                     "glCopyTexSubImage%dD(yoffset+height)", dimensions);
1979c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         return GL_TRUE;
1980c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      }
1981c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   }
1982c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul
1983c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   if (dimensions > 2) {
1984c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      if (zoffset < -((GLint)teximage->Border)) {
19854e9676fb13f60ecdbc247b120031f18cd3febcb0Brian Paul         _mesa_error(ctx, GL_INVALID_VALUE,
19864e9676fb13f60ecdbc247b120031f18cd3febcb0Brian Paul                     "glCopyTexSubImage%dD(zoffset)", dimensions);
1987c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         return GL_TRUE;
1988c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      }
198938f28665bf9fb5b2464738ca5074848ec2777ae1Gareth Hughes      if (zoffset > (GLint) (teximage->Depth + teximage->Border)) {
19904e9676fb13f60ecdbc247b120031f18cd3febcb0Brian Paul         _mesa_error(ctx, GL_INVALID_VALUE,
19914e9676fb13f60ecdbc247b120031f18cd3febcb0Brian Paul                     "glCopyTexSubImage%dD(zoffset+depth)", dimensions);
1992c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         return GL_TRUE;
1993c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      }
1994c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   }
1995c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul
199689fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   if (teximage->IsCompressed) {
199734bd1233a9874fe12a822c4fcb926d48456e1f29Brian Paul   if (!_mesa_source_buffer_exists(ctx, teximage->_BaseFormat)) {
199834bd1233a9874fe12a822c4fcb926d48456e1f29Brian Paul      _mesa_error(ctx, GL_INVALID_OPERATION,
199934bd1233a9874fe12a822c4fcb926d48456e1f29Brian Paul                  "glCopyTexSubImage%dD(missing readbuffer)", dimensions);
200034bd1233a9874fe12a822c4fcb926d48456e1f29Brian Paul      return GL_TRUE;
200134bd1233a9874fe12a822c4fcb926d48456e1f29Brian Paul   }
200234bd1233a9874fe12a822c4fcb926d48456e1f29Brian Paul
200389fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      if (target != GL_TEXTURE_2D) {
200489fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         _mesa_error(ctx, GL_INVALID_ENUM,
200589fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul                     "glCopyTexSubImage%d(target)", dimensions);
2006e4276667dafc8de0c6e64af8300fc7598437de6eBrian Paul         return GL_TRUE;
2007e4276667dafc8de0c6e64af8300fc7598437de6eBrian Paul      }
200889fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      /* offset must be multiple of 4 */
200989fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      if ((xoffset & 3) || (yoffset & 3)) {
201089fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         _mesa_error(ctx, GL_INVALID_VALUE,
201189fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul                     "glCopyTexSubImage%D(xoffset or yoffset)", dimensions);
2012e4276667dafc8de0c6e64af8300fc7598437de6eBrian Paul         return GL_TRUE;
2013e4276667dafc8de0c6e64af8300fc7598437de6eBrian Paul      }
201489fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      /* size must be multiple of 4 */
2015798d83cf67098a7df293c711e0d2c8c296fc4c8fKarl Schultz      if ((width & 3) != 0 && (GLuint) width != teximage->Width) {
201689fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         _mesa_error(ctx, GL_INVALID_VALUE,
201789fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul                     "glCopyTexSubImage%D(width)", dimensions);
2018e4276667dafc8de0c6e64af8300fc7598437de6eBrian Paul         return GL_TRUE;
201989fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      }
2020798d83cf67098a7df293c711e0d2c8c296fc4c8fKarl Schultz      if ((height & 3) != 0 && (GLuint) height != teximage->Height) {
202189fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         _mesa_error(ctx, GL_INVALID_VALUE,
202289fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul                     "glCopyTexSubImage%D(height)", dimensions);
202389fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         return GL_TRUE;
202489fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      }
2025e4276667dafc8de0c6e64af8300fc7598437de6eBrian Paul   }
2026e4276667dafc8de0c6e64af8300fc7598437de6eBrian Paul
2027a9fc8ba756dd25a07dc19058fe60f65bda82a055Brian Paul   if (teximage->InternalFormat == GL_YCBCR_MESA) {
2028d8419c730e73c3be2eadfb0bee176ab06885766aBrian Paul      _mesa_error(ctx, GL_INVALID_OPERATION, "glCopyTexSubImage2D");
2029d8419c730e73c3be2eadfb0bee176ab06885766aBrian Paul      return GL_TRUE;
2030d8419c730e73c3be2eadfb0bee176ab06885766aBrian Paul   }
2031d8419c730e73c3be2eadfb0bee176ab06885766aBrian Paul
2032a9fc8ba756dd25a07dc19058fe60f65bda82a055Brian Paul   if (teximage->_BaseFormat == GL_DEPTH_COMPONENT) {
20331a5f1bdb428ea3ded1e77796590014f89fe78f77Brian Paul      if (!ctx->ReadBuffer->_DepthBuffer) {
20340899afae33a29d1f6d15ebc96dc6ea8a8371cdb6Brian Paul         _mesa_error(ctx, GL_INVALID_OPERATION,
20350899afae33a29d1f6d15ebc96dc6ea8a8371cdb6Brian Paul                     "glCopyTexSubImage%D(no depth buffer)",
20360899afae33a29d1f6d15ebc96dc6ea8a8371cdb6Brian Paul                     dimensions);
20370899afae33a29d1f6d15ebc96dc6ea8a8371cdb6Brian Paul         return GL_TRUE;
20380899afae33a29d1f6d15ebc96dc6ea8a8371cdb6Brian Paul      }
20390899afae33a29d1f6d15ebc96dc6ea8a8371cdb6Brian Paul   }
2040a9fc8ba756dd25a07dc19058fe60f65bda82a055Brian Paul   else if (teximage->_BaseFormat == GL_DEPTH_STENCIL_EXT) {
20411a5f1bdb428ea3ded1e77796590014f89fe78f77Brian Paul      if (!ctx->ReadBuffer->_DepthBuffer || !ctx->ReadBuffer->_StencilBuffer) {
20420899afae33a29d1f6d15ebc96dc6ea8a8371cdb6Brian Paul         _mesa_error(ctx, GL_INVALID_OPERATION,
20430899afae33a29d1f6d15ebc96dc6ea8a8371cdb6Brian Paul                     "glCopyTexSubImage%D(no depth/stencil buffer)",
20440899afae33a29d1f6d15ebc96dc6ea8a8371cdb6Brian Paul                     dimensions);
20450899afae33a29d1f6d15ebc96dc6ea8a8371cdb6Brian Paul         return GL_TRUE;
20460899afae33a29d1f6d15ebc96dc6ea8a8371cdb6Brian Paul      }
20470899afae33a29d1f6d15ebc96dc6ea8a8371cdb6Brian Paul   }
20480899afae33a29d1f6d15ebc96dc6ea8a8371cdb6Brian Paul
2049c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   /* if we get here, the parameters are OK */
2050c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   return GL_FALSE;
2051c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul}
2052c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul
2053c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul
20546dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/**
20551749a25ca889d514889b34cf6311c8014d97bf66Brian Paul * Get texture image.  Called by glGetTexImage.
20566dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell *
20576dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param target texture target.
20586dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param level image level.
20591749a25ca889d514889b34cf6311c8014d97bf66Brian Paul * \param format pixel data format for returned image.
20601749a25ca889d514889b34cf6311c8014d97bf66Brian Paul * \param type pixel data type for returned image.
20611749a25ca889d514889b34cf6311c8014d97bf66Brian Paul * \param pixels returned pixel data.
20626dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell */
2063c40d1dd62dd9bcbb97128e37a75d991a8d3b2d8cKendall Bennettvoid GLAPIENTRY
20648e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul_mesa_GetTexImage( GLenum target, GLint level, GLenum format,
20658e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul                   GLenum type, GLvoid *pixels )
2066289d47eee5342bd7ec1c25a29d9bdc8d6f9dc4edBrian Paul{
20678e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul   const struct gl_texture_unit *texUnit;
2068ea4fe661d7f3a95d9db17e1475076f1badf8e1a6Brian Paul   struct gl_texture_object *texObj;
2069ea4fe661d7f3a95d9db17e1475076f1badf8e1a6Brian Paul   struct gl_texture_image *texImage;
2070cd1cefae9146fc14b35ee93a04bdb1b1590fba7bBrian Paul   GLint maxLevels = 0;
20712c3d34c905fa6b831a066afae83b938de05eb241Gareth Hughes   GET_CURRENT_CONTEXT(ctx);
20728e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
20738e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul
2074cd1cefae9146fc14b35ee93a04bdb1b1590fba7bBrian Paul   texUnit = &(ctx->Texture.Unit[ctx->Texture.CurrentUnit]);
2075cd1cefae9146fc14b35ee93a04bdb1b1590fba7bBrian Paul   texObj = _mesa_select_tex_object(ctx, texUnit, target);
207677ee31930a1b0cc7766939415f4f04ed6a1fa4acBrian Paul   if (!texObj || _mesa_is_proxy_texture(target)) {
2077cd1cefae9146fc14b35ee93a04bdb1b1590fba7bBrian Paul      _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexImage(target)");
2078cd1cefae9146fc14b35ee93a04bdb1b1590fba7bBrian Paul      return;
2079cd1cefae9146fc14b35ee93a04bdb1b1590fba7bBrian Paul   }
2080cd1cefae9146fc14b35ee93a04bdb1b1590fba7bBrian Paul
2081ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul   maxLevels = _mesa_max_texture_levels(ctx, target);
2082ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul   ASSERT(maxLevels > 0);  /* 0 indicates bad target, caught above */
2083cd1cefae9146fc14b35ee93a04bdb1b1590fba7bBrian Paul
2084cd1cefae9146fc14b35ee93a04bdb1b1590fba7bBrian Paul   if (level < 0 || level >= maxLevels) {
208508836341788a9f9d638d9dc8328510ccd18ddeb5Brian Paul      _mesa_error( ctx, GL_INVALID_VALUE, "glGetTexImage(level)" );
20868e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul      return;
2087289d47eee5342bd7ec1c25a29d9bdc8d6f9dc4edBrian Paul   }
2088289d47eee5342bd7ec1c25a29d9bdc8d6f9dc4edBrian Paul
208962c36a2ff39d775c1f1bc377b9ddbc69e2340799Brian Paul   if (_mesa_sizeof_packed_type(type) <= 0) {
209008836341788a9f9d638d9dc8328510ccd18ddeb5Brian Paul      _mesa_error( ctx, GL_INVALID_ENUM, "glGetTexImage(type)" );
20918e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul      return;
20928e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul   }
20938e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul
2094f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul   if (_mesa_components_in_format(format) <= 0 ||
2095f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul       format == GL_STENCIL_INDEX) {
209608836341788a9f9d638d9dc8328510ccd18ddeb5Brian Paul      _mesa_error( ctx, GL_INVALID_ENUM, "glGetTexImage(format)" );
20978e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul      return;
20988e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul   }
20998e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul
2100f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul   if (!ctx->Extensions.EXT_paletted_texture && is_index_format(format)) {
210108836341788a9f9d638d9dc8328510ccd18ddeb5Brian Paul      _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexImage(format)");
2102f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul   }
2103f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul
21044618a9bfc2cd57c47ddf3590782c751ec194d362Brian Paul   if (!ctx->Extensions.SGIX_depth_texture &&
21054618a9bfc2cd57c47ddf3590782c751ec194d362Brian Paul       !ctx->Extensions.ARB_depth_texture && is_depth_format(format)) {
210608836341788a9f9d638d9dc8328510ccd18ddeb5Brian Paul      _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexImage(format)");
2107f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul   }
2108f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul
21091749a25ca889d514889b34cf6311c8014d97bf66Brian Paul   if (!ctx->Extensions.MESA_ycbcr_texture && is_ycbcr_format(format)) {
2110c5b995066020191982b2315fc45d05e068eee761Brian Paul      _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexImage(format)");
2111c5b995066020191982b2315fc45d05e068eee761Brian Paul   }
2112c5b995066020191982b2315fc45d05e068eee761Brian Paul
21131ad7b99925e044f82e635f746c1ef2df77f69ac9Brian Paul   if (!ctx->Extensions.EXT_packed_depth_stencil
21141ad7b99925e044f82e635f746c1ef2df77f69ac9Brian Paul       && is_depthstencil_format(format)) {
21151ad7b99925e044f82e635f746c1ef2df77f69ac9Brian Paul      _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexImage(format)");
21161ad7b99925e044f82e635f746c1ef2df77f69ac9Brian Paul   }
21171ad7b99925e044f82e635f746c1ef2df77f69ac9Brian Paul
21188e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul   if (!pixels)
21198e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul      return;
21208e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul
21218e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul   texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
21228e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul   if (!texImage) {
21238e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul      /* invalid mipmap level, not an error */
21248e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul      return;
21258e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul   }
21268e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul
21271749a25ca889d514889b34cf6311c8014d97bf66Brian Paul   /* Make sure the requested image format is compatible with the
21280899afae33a29d1f6d15ebc96dc6ea8a8371cdb6Brian Paul    * texture's format.  Note that a color index texture can be converted
21290899afae33a29d1f6d15ebc96dc6ea8a8371cdb6Brian Paul    * to RGBA so that combo is allowed.
21301749a25ca889d514889b34cf6311c8014d97bf66Brian Paul    */
21311749a25ca889d514889b34cf6311c8014d97bf66Brian Paul   if (is_color_format(format)
21323cc28c96cd2003f9a598a36520305bba58d6489fDaniel Borca       && !is_color_format(texImage->TexFormat->BaseFormat)
21333cc28c96cd2003f9a598a36520305bba58d6489fDaniel Borca       && !is_index_format(texImage->TexFormat->BaseFormat)) {
21341749a25ca889d514889b34cf6311c8014d97bf66Brian Paul      _mesa_error(ctx, GL_INVALID_OPERATION, "glGetTexImage(format mismatch)");
21351749a25ca889d514889b34cf6311c8014d97bf66Brian Paul      return;
21361749a25ca889d514889b34cf6311c8014d97bf66Brian Paul   }
21371749a25ca889d514889b34cf6311c8014d97bf66Brian Paul   else if (is_index_format(format)
21381749a25ca889d514889b34cf6311c8014d97bf66Brian Paul       && !is_index_format(texImage->TexFormat->BaseFormat)) {
21391749a25ca889d514889b34cf6311c8014d97bf66Brian Paul      _mesa_error(ctx, GL_INVALID_OPERATION, "glGetTexImage(format mismatch)");
21401749a25ca889d514889b34cf6311c8014d97bf66Brian Paul      return;
21411749a25ca889d514889b34cf6311c8014d97bf66Brian Paul   }
21421749a25ca889d514889b34cf6311c8014d97bf66Brian Paul   else if (is_depth_format(format)
21430899afae33a29d1f6d15ebc96dc6ea8a8371cdb6Brian Paul       && !is_depth_format(texImage->TexFormat->BaseFormat)
21440899afae33a29d1f6d15ebc96dc6ea8a8371cdb6Brian Paul       && !is_depthstencil_format(texImage->TexFormat->BaseFormat)) {
21451749a25ca889d514889b34cf6311c8014d97bf66Brian Paul      _mesa_error(ctx, GL_INVALID_OPERATION, "glGetTexImage(format mismatch)");
21461749a25ca889d514889b34cf6311c8014d97bf66Brian Paul      return;
21471749a25ca889d514889b34cf6311c8014d97bf66Brian Paul   }
21481749a25ca889d514889b34cf6311c8014d97bf66Brian Paul   else if (is_ycbcr_format(format)
21491749a25ca889d514889b34cf6311c8014d97bf66Brian Paul       && !is_ycbcr_format(texImage->TexFormat->BaseFormat)) {
21501749a25ca889d514889b34cf6311c8014d97bf66Brian Paul      _mesa_error(ctx, GL_INVALID_OPERATION, "glGetTexImage(format mismatch)");
21511ad7b99925e044f82e635f746c1ef2df77f69ac9Brian Paul      return;
21521ad7b99925e044f82e635f746c1ef2df77f69ac9Brian Paul   }
21531ad7b99925e044f82e635f746c1ef2df77f69ac9Brian Paul   else if (is_depthstencil_format(format)
21541ad7b99925e044f82e635f746c1ef2df77f69ac9Brian Paul       && !is_depthstencil_format(texImage->TexFormat->BaseFormat)) {
21551ad7b99925e044f82e635f746c1ef2df77f69ac9Brian Paul      _mesa_error(ctx, GL_INVALID_OPERATION, "glGetTexImage(format mismatch)");
21561749a25ca889d514889b34cf6311c8014d97bf66Brian Paul      return;
21571749a25ca889d514889b34cf6311c8014d97bf66Brian Paul   }
21581749a25ca889d514889b34cf6311c8014d97bf66Brian Paul
215968d293b03535ca50daf70650b32db780f1718a3bBrian Paul   /* typically, this will call _mesa_get_teximage() */
216068d293b03535ca50daf70650b32db780f1718a3bBrian Paul   ctx->Driver.GetTexImage(ctx, target, level, format, type, pixels,
216168d293b03535ca50daf70650b32db780f1718a3bBrian Paul                           texObj, texImage);
2162289d47eee5342bd7ec1c25a29d9bdc8d6f9dc4edBrian Paul}
2163289d47eee5342bd7ec1c25a29d9bdc8d6f9dc4edBrian Paul
2164289d47eee5342bd7ec1c25a29d9bdc8d6f9dc4edBrian Paul
21658e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul
2166519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul/**
2167519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul * Check if the given texture image is bound to any framebuffer objects
2168519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul * and update/invalidate them.
2169519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul * XXX We're only checking the currently bound framebuffer object for now.
2170519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul * In the future, perhaps struct gl_texture_image should have a pointer (or
2171519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul * list of pointers (yikes)) to the gl_framebuffer(s) which it's bound to.
2172519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul */
2173519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paulstatic void
2174519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paulupdate_fbo_texture(GLcontext *ctx, struct gl_texture_object *texObj,
2175519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul                   GLuint face, GLuint level)
2176519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul{
2177519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul   if (ctx->DrawBuffer->Name) {
2178519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul      GLuint i;
2179519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul      for (i = 0; i < BUFFER_COUNT; i++) {
2180519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul         struct gl_renderbuffer_attachment *att =
2181519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul            ctx->DrawBuffer->Attachment + i;
2182519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul         if (att->Type == GL_TEXTURE &&
2183519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul             att->Texture == texObj &&
2184519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul             att->TextureLevel == level &&
2185519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul             att->CubeMapFace == face) {
2186519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul            ASSERT(att->Texture->Image[att->CubeMapFace][att->TextureLevel]);
2187519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul            /* Tell driver about the new renderbuffer texture */
2188ea4fe661d7f3a95d9db17e1475076f1badf8e1a6Brian Paul            ctx->Driver.RenderTexture(ctx, ctx->DrawBuffer, att);
2189519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul         }
2190519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul      }
2191519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul   }
2192519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul}
2193519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul
2194519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul
2195519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul
2196289d47eee5342bd7ec1c25a29d9bdc8d6f9dc4edBrian Paul/*
2197c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul * Called from the API.  Note that width includes the border.
2198c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul */
2199c40d1dd62dd9bcbb97128e37a75d991a8d3b2d8cKendall Bennettvoid GLAPIENTRY
220043911c8ef06c44d486a17cf9a8fa8ea263b08adeBrian Paul_mesa_TexImage1D( GLenum target, GLint level, GLint internalFormat,
2201fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul                  GLsizei width, GLint border, GLenum format,
2202fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul                  GLenum type, const GLvoid *pixels )
2203c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul{
2204ab6e78f3a0abac7becbd8a58ed2c89f860cc6b0bBrian Paul   GLsizei postConvWidth = width;
2205fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul   GET_CURRENT_CONTEXT(ctx);
2206cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2207c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul
2208f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul   if (is_color_format(internalFormat)) {
2209f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul      _mesa_adjust_image_for_convolution(ctx, 1, &postConvWidth, NULL);
2210f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul   }
2211f93b3dd69e744cf1dd6b102a11cdb07c2df4a967Brian Paul
22128e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul   if (target == GL_TEXTURE_1D) {
22138c3ddf4270ff075ee783a67e5d5d04fa16a9cb45Brian Paul      /* non-proxy target */
2214f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul      struct gl_texture_unit *texUnit;
22150293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      struct gl_texture_object *texObj;
22160293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      struct gl_texture_image *texImage;
2217519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul      const GLuint face = texture_face(target);
2218f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul
2219aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul      if (texture_error_check(ctx, target, level, internalFormat,
2220f93b3dd69e744cf1dd6b102a11cdb07c2df4a967Brian Paul                              format, type, 1, postConvWidth, 1, 1, border)) {
22218e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul         return;   /* error was recorded */
2222c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      }
2223c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul
2224f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul      texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
22258e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul      texObj = _mesa_select_tex_object(ctx, texUnit, target);
2226a3f137094cd965d27e1b088499dd609b81a91906Brian Paul      texImage = _mesa_get_tex_image(ctx, texUnit, target, level);
22270293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul
22280293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      if (!texImage) {
2229a3f137094cd965d27e1b088499dd609b81a91906Brian Paul         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage1D");
2230a3f137094cd965d27e1b088499dd609b81a91906Brian Paul         return;
22310293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      }
22323e62d3a8d88b48d4ed19e00ea2bbc3d0a2b6acf7Keith Whitwell      else if (texImage->Data) {
22333e62d3a8d88b48d4ed19e00ea2bbc3d0a2b6acf7Keith Whitwell	 ctx->Driver.FreeTexImageData( ctx, texImage );
2234c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      }
22353e62d3a8d88b48d4ed19e00ea2bbc3d0a2b6acf7Keith Whitwell      ASSERT(texImage->Data == NULL);
2236f378ab825c0c74aab263e7dec30194eead22c288Brian Paul      clear_teximage_fields(texImage); /* not really needed, but helpful */
22378afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul      _mesa_init_teximage_fields(ctx, target, texImage,
22388afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul                                 postConvWidth, 1, 1,
22393893e638e6521b9c070e01c0b31d22754ff97a88Brian Paul                                 border, internalFormat);
2240c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul
2241b23f5b5a5ccf5910f1d11d749c2e440e6a46259bBrian Paul      if (ctx->NewState & _IMAGE_NEW_TRANSFER_STATE)
224208836341788a9f9d638d9dc8328510ccd18ddeb5Brian Paul         _mesa_update_state(ctx);
2243fa4525e289b475b928a7b2c4055af9dd7fe46600Brian Paul
22448e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul      ASSERT(ctx->Driver.TexImage1D);
224562c734f49948df7aeef55ad23a6664cbf3e11533Brian Paul
224689fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      /* Give the texture to the driver!  <pixels> may be null! */
224762c734f49948df7aeef55ad23a6664cbf3e11533Brian Paul      (*ctx->Driver.TexImage1D)(ctx, target, level, internalFormat,
224862c734f49948df7aeef55ad23a6664cbf3e11533Brian Paul                                width, border, format, type, pixels,
224962c734f49948df7aeef55ad23a6664cbf3e11533Brian Paul                                &ctx->Unpack, texObj, texImage);
2250c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul
225138f28665bf9fb5b2464738ca5074848ec2777ae1Gareth Hughes      ASSERT(texImage->TexFormat);
22524f295cee73bae1f687efe2dc062522b40d90b1e4Brian Paul
2253519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul      update_fbo_texture(ctx, texObj, face, level);
2254519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul
22550293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      /* state update */
2256a852378a6289d154364dde440f89a39bbfc33e2dBrian Paul      texObj->Complete = GL_FALSE;
2257a96308c37db0bc0086a017d318bc3504aa5f0b1aKeith Whitwell      ctx->NewState |= _NEW_TEXTURE;
2258c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   }
225938d3f3d660225150715bc06f6b33e394ba803299Brian Paul   else if (target == GL_PROXY_TEXTURE_1D) {
2260c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      /* Proxy texture: check for errors and update proxy state */
2261738318bb75dea8dac4465f53850987f6062a732dBrian Paul      struct gl_texture_image *texImage;
2262738318bb75dea8dac4465f53850987f6062a732dBrian Paul      texImage = _mesa_get_proxy_tex_image(ctx, target, level);
2263738318bb75dea8dac4465f53850987f6062a732dBrian Paul      if (texture_error_check(ctx, target, level, internalFormat,
2264738318bb75dea8dac4465f53850987f6062a732dBrian Paul                              format, type, 1, postConvWidth, 1, 1, border)) {
2265738318bb75dea8dac4465f53850987f6062a732dBrian Paul         /* when error, clear all proxy texture image parameters */
2266a3f137094cd965d27e1b088499dd609b81a91906Brian Paul         if (texImage)
2267a3f137094cd965d27e1b088499dd609b81a91906Brian Paul            clear_teximage_fields(texImage);
2268c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      }
226989fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      else {
227089fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         /* no error, set the tex image parameters */
2271738318bb75dea8dac4465f53850987f6062a732dBrian Paul         ASSERT(texImage);
227289fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         _mesa_init_teximage_fields(ctx, target, texImage,
227389fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul                                    postConvWidth, 1, 1,
227489fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul                                    border, internalFormat);
2275738318bb75dea8dac4465f53850987f6062a732dBrian Paul         texImage->TexFormat = (*ctx->Driver.ChooseTextureFormat)(ctx,
2276738318bb75dea8dac4465f53850987f6062a732dBrian Paul                                          internalFormat, format, type);
227789fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      }
2278c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   }
2279c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   else {
228008836341788a9f9d638d9dc8328510ccd18ddeb5Brian Paul      _mesa_error( ctx, GL_INVALID_ENUM, "glTexImage1D(target)" );
2281c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      return;
2282c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   }
2283c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul}
2284c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul
2285c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul
2286c40d1dd62dd9bcbb97128e37a75d991a8d3b2d8cKendall Bennettvoid GLAPIENTRY
228743911c8ef06c44d486a17cf9a8fa8ea263b08adeBrian Paul_mesa_TexImage2D( GLenum target, GLint level, GLint internalFormat,
2288fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul                  GLsizei width, GLsizei height, GLint border,
2289fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul                  GLenum format, GLenum type,
2290fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul                  const GLvoid *pixels )
2291c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul{
2292ab6e78f3a0abac7becbd8a58ed2c89f860cc6b0bBrian Paul   GLsizei postConvWidth = width, postConvHeight = height;
2293fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul   GET_CURRENT_CONTEXT(ctx);
2294cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2295c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul
2296f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul   if (is_color_format(internalFormat)) {
2297f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul      _mesa_adjust_image_for_convolution(ctx, 2, &postConvWidth,
229838f28665bf9fb5b2464738ca5074848ec2777ae1Gareth Hughes					 &postConvHeight);
2299f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul   }
2300f93b3dd69e744cf1dd6b102a11cdb07c2df4a967Brian Paul
23018e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul   if (target == GL_TEXTURE_2D ||
2302a96308c37db0bc0086a017d318bc3504aa5f0b1aKeith Whitwell       (ctx->Extensions.ARB_texture_cube_map &&
2303fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul        target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
23048afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul        target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB) ||
23058afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul       (ctx->Extensions.NV_texture_rectangle &&
23068afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul        target == GL_TEXTURE_RECTANGLE_NV)) {
23078e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul      /* non-proxy target */
2308f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul      struct gl_texture_unit *texUnit;
23090293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      struct gl_texture_object *texObj;
23100293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      struct gl_texture_image *texImage;
2311519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul      const GLuint face = texture_face(target);
2312f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul
2313aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul      if (texture_error_check(ctx, target, level, internalFormat,
2314f93b3dd69e744cf1dd6b102a11cdb07c2df4a967Brian Paul                              format, type, 2, postConvWidth, postConvHeight,
2315f93b3dd69e744cf1dd6b102a11cdb07c2df4a967Brian Paul                              1, border)) {
23168e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul         return;   /* error was recorded */
2317c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      }
2318c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul
2319f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul      texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
232035d5301a54153930ee6fd60dff1010ce9f901397Brian Paul      texObj = _mesa_select_tex_object(ctx, texUnit, target);
2321a3f137094cd965d27e1b088499dd609b81a91906Brian Paul      texImage = _mesa_get_tex_image(ctx, texUnit, target, level);
23220293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      if (!texImage) {
2323a3f137094cd965d27e1b088499dd609b81a91906Brian Paul         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage2D");
2324a3f137094cd965d27e1b088499dd609b81a91906Brian Paul         return;
23250293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      }
23263e62d3a8d88b48d4ed19e00ea2bbc3d0a2b6acf7Keith Whitwell      else if (texImage->Data) {
23273e62d3a8d88b48d4ed19e00ea2bbc3d0a2b6acf7Keith Whitwell	 ctx->Driver.FreeTexImageData( ctx, texImage );
2328c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      }
23293e62d3a8d88b48d4ed19e00ea2bbc3d0a2b6acf7Keith Whitwell      ASSERT(texImage->Data == NULL);
2330f378ab825c0c74aab263e7dec30194eead22c288Brian Paul      clear_teximage_fields(texImage); /* not really needed, but helpful */
23318afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul      _mesa_init_teximage_fields(ctx, target, texImage,
23328afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul                                 postConvWidth, postConvHeight, 1,
23338afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul                                 border, internalFormat);
2334c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul
2335b23f5b5a5ccf5910f1d11d749c2e440e6a46259bBrian Paul      if (ctx->NewState & _IMAGE_NEW_TRANSFER_STATE)
233608836341788a9f9d638d9dc8328510ccd18ddeb5Brian Paul         _mesa_update_state(ctx);
2337fa4525e289b475b928a7b2c4055af9dd7fe46600Brian Paul
23388e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul      ASSERT(ctx->Driver.TexImage2D);
233962c734f49948df7aeef55ad23a6664cbf3e11533Brian Paul
234089fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      /* Give the texture to the driver!  <pixels> may be null! */
234162c734f49948df7aeef55ad23a6664cbf3e11533Brian Paul      (*ctx->Driver.TexImage2D)(ctx, target, level, internalFormat,
234262c734f49948df7aeef55ad23a6664cbf3e11533Brian Paul                                width, height, border, format, type, pixels,
234362c734f49948df7aeef55ad23a6664cbf3e11533Brian Paul                                &ctx->Unpack, texObj, texImage);
2344afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
234538f28665bf9fb5b2464738ca5074848ec2777ae1Gareth Hughes      ASSERT(texImage->TexFormat);
23464f295cee73bae1f687efe2dc062522b40d90b1e4Brian Paul
2347519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul      update_fbo_texture(ctx, texObj, face, level);
2348519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul
23490293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      /* state update */
2350a852378a6289d154364dde440f89a39bbfc33e2dBrian Paul      texObj->Complete = GL_FALSE;
2351a96308c37db0bc0086a017d318bc3504aa5f0b1aKeith Whitwell      ctx->NewState |= _NEW_TEXTURE;
2352afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   }
2353172281dad8d1f1d32f5f31583a5fa565aa1bf5daBrian Paul   else if (target == GL_PROXY_TEXTURE_2D ||
2354172281dad8d1f1d32f5f31583a5fa565aa1bf5daBrian Paul            (target == GL_PROXY_TEXTURE_CUBE_MAP_ARB &&
23558afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul             ctx->Extensions.ARB_texture_cube_map) ||
23568afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul            (target == GL_PROXY_TEXTURE_RECTANGLE_NV &&
23578afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul             ctx->Extensions.NV_texture_rectangle)) {
2358afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      /* Proxy texture: check for errors and update proxy state */
2359738318bb75dea8dac4465f53850987f6062a732dBrian Paul      struct gl_texture_image *texImage;
2360738318bb75dea8dac4465f53850987f6062a732dBrian Paul      texImage = _mesa_get_proxy_tex_image(ctx, target, level);
2361738318bb75dea8dac4465f53850987f6062a732dBrian Paul      if (texture_error_check(ctx, target, level, internalFormat,
2362738318bb75dea8dac4465f53850987f6062a732dBrian Paul                              format, type, 2, postConvWidth, postConvHeight,
2363738318bb75dea8dac4465f53850987f6062a732dBrian Paul                              1, border)) {
2364738318bb75dea8dac4465f53850987f6062a732dBrian Paul         /* when error, clear all proxy texture image parameters */
2365a3f137094cd965d27e1b088499dd609b81a91906Brian Paul         if (texImage)
236618fa367ac6e035341f5eb86ecc4231124b2921e3Keith Whitwell            clear_teximage_fields(ctx->Texture.Proxy2D->Image[0][level]);
2367afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      }
236889fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      else {
236989fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         /* no error, set the tex image parameters */
237089fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         _mesa_init_teximage_fields(ctx, target, texImage,
237189fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul                                    postConvWidth, postConvHeight, 1,
237289fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul                                    border, internalFormat);
2373738318bb75dea8dac4465f53850987f6062a732dBrian Paul         texImage->TexFormat = (*ctx->Driver.ChooseTextureFormat)(ctx,
2374738318bb75dea8dac4465f53850987f6062a732dBrian Paul                                          internalFormat, format, type);
237589fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      }
2376afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   }
2377afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   else {
237808836341788a9f9d638d9dc8328510ccd18ddeb5Brian Paul      _mesa_error( ctx, GL_INVALID_ENUM, "glTexImage2D(target)" );
2379afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      return;
2380afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   }
2381afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg}
2382afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2383afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2384afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg/*
2385afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg * Called by the API or display list executor.
2386afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg * Note that width and height include the border.
2387afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg */
2388c40d1dd62dd9bcbb97128e37a75d991a8d3b2d8cKendall Bennettvoid GLAPIENTRY
2389b5fd886ee12ef52126969f712dee0bd4b2eea706Brian Paul_mesa_TexImage3D( GLenum target, GLint level, GLint internalFormat,
2390fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul                  GLsizei width, GLsizei height, GLsizei depth,
2391fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul                  GLint border, GLenum format, GLenum type,
2392fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul                  const GLvoid *pixels )
2393afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg{
2394fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul   GET_CURRENT_CONTEXT(ctx);
2395cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2396afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
23978e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul   if (target == GL_TEXTURE_3D) {
2398519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul      /* non-proxy target */
2399f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul      struct gl_texture_unit *texUnit;
24000293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      struct gl_texture_object *texObj;
24010293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      struct gl_texture_image *texImage;
2402519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul      const GLuint face = texture_face(target);
2403289d47eee5342bd7ec1c25a29d9bdc8d6f9dc4edBrian Paul
24045535aca5dffdfb8ad9a1970a062e6109227aa17fBrian Paul      if (texture_error_check(ctx, target, level, (GLint) internalFormat,
2405aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul                              format, type, 3, width, height, depth, border)) {
24068e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul         return;   /* error was recorded */
2407afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      }
2408afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2409f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul      texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
24108e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul      texObj = _mesa_select_tex_object(ctx, texUnit, target);
2411a3f137094cd965d27e1b088499dd609b81a91906Brian Paul      texImage = _mesa_get_tex_image(ctx, texUnit, target, level);
24120293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      if (!texImage) {
2413a3f137094cd965d27e1b088499dd609b81a91906Brian Paul         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage3D");
2414a3f137094cd965d27e1b088499dd609b81a91906Brian Paul         return;
24150293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      }
24163e62d3a8d88b48d4ed19e00ea2bbc3d0a2b6acf7Keith Whitwell      else if (texImage->Data) {
24173e62d3a8d88b48d4ed19e00ea2bbc3d0a2b6acf7Keith Whitwell	 ctx->Driver.FreeTexImageData( ctx, texImage );
2418afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      }
24193e62d3a8d88b48d4ed19e00ea2bbc3d0a2b6acf7Keith Whitwell      ASSERT(texImage->Data == NULL);
2420f378ab825c0c74aab263e7dec30194eead22c288Brian Paul      clear_teximage_fields(texImage); /* not really needed, but helpful */
24218afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul      _mesa_init_teximage_fields(ctx, target, texImage,
24228afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul                                 width, height, depth,
24238afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul                                 border, internalFormat);
2424afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2425b23f5b5a5ccf5910f1d11d749c2e440e6a46259bBrian Paul      if (ctx->NewState & _IMAGE_NEW_TRANSFER_STATE)
242608836341788a9f9d638d9dc8328510ccd18ddeb5Brian Paul         _mesa_update_state(ctx);
2427fa4525e289b475b928a7b2c4055af9dd7fe46600Brian Paul
24288e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul      ASSERT(ctx->Driver.TexImage3D);
242962c734f49948df7aeef55ad23a6664cbf3e11533Brian Paul
243089fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      /* Give the texture to the driver!  <pixels> may be null! */
243162c734f49948df7aeef55ad23a6664cbf3e11533Brian Paul      (*ctx->Driver.TexImage3D)(ctx, target, level, internalFormat,
243262c734f49948df7aeef55ad23a6664cbf3e11533Brian Paul                                width, height, depth, border, format, type,
243362c734f49948df7aeef55ad23a6664cbf3e11533Brian Paul                                pixels, &ctx->Unpack, texObj, texImage);
2434afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
243538f28665bf9fb5b2464738ca5074848ec2777ae1Gareth Hughes      ASSERT(texImage->TexFormat);
24364f295cee73bae1f687efe2dc062522b40d90b1e4Brian Paul
2437519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul      update_fbo_texture(ctx, texObj, face, level);
2438519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul
24390293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      /* state update */
2440a852378a6289d154364dde440f89a39bbfc33e2dBrian Paul      texObj->Complete = GL_FALSE;
2441a96308c37db0bc0086a017d318bc3504aa5f0b1aKeith Whitwell      ctx->NewState |= _NEW_TEXTURE;
2442afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   }
244338d3f3d660225150715bc06f6b33e394ba803299Brian Paul   else if (target == GL_PROXY_TEXTURE_3D) {
2444afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      /* Proxy texture: check for errors and update proxy state */
2445738318bb75dea8dac4465f53850987f6062a732dBrian Paul      struct gl_texture_image *texImage;
2446738318bb75dea8dac4465f53850987f6062a732dBrian Paul      texImage = _mesa_get_proxy_tex_image(ctx, target, level);
2447738318bb75dea8dac4465f53850987f6062a732dBrian Paul      if (texture_error_check(ctx, target, level, internalFormat,
2448738318bb75dea8dac4465f53850987f6062a732dBrian Paul                              format, type, 3, width, height, depth, border)) {
2449738318bb75dea8dac4465f53850987f6062a732dBrian Paul         /* when error, clear all proxy texture image parameters */
2450a3f137094cd965d27e1b088499dd609b81a91906Brian Paul         if (texImage)
2451a3f137094cd965d27e1b088499dd609b81a91906Brian Paul            clear_teximage_fields(texImage);
2452afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      }
245389fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      else {
245489fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         /* no error, set the tex image parameters */
245589fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         _mesa_init_teximage_fields(ctx, target, texImage, width, height, 1,
245689fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul                                    border, internalFormat);
2457738318bb75dea8dac4465f53850987f6062a732dBrian Paul         texImage->TexFormat = (*ctx->Driver.ChooseTextureFormat)(ctx,
2458738318bb75dea8dac4465f53850987f6062a732dBrian Paul                                          internalFormat, format, type);
245989fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      }
2460afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   }
2461afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   else {
246208836341788a9f9d638d9dc8328510ccd18ddeb5Brian Paul      _mesa_error( ctx, GL_INVALID_ENUM, "glTexImage3D(target)" );
2463afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      return;
2464afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   }
2465afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg}
2466afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2467afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2468c40d1dd62dd9bcbb97128e37a75d991a8d3b2d8cKendall Bennettvoid GLAPIENTRY
246943911c8ef06c44d486a17cf9a8fa8ea263b08adeBrian Paul_mesa_TexImage3DEXT( GLenum target, GLint level, GLenum internalFormat,
2470663049a719ed76aa0a6f24f1951a411d241bc8a4Brian Paul                     GLsizei width, GLsizei height, GLsizei depth,
2471663049a719ed76aa0a6f24f1951a411d241bc8a4Brian Paul                     GLint border, GLenum format, GLenum type,
2472663049a719ed76aa0a6f24f1951a411d241bc8a4Brian Paul                     const GLvoid *pixels )
2473663049a719ed76aa0a6f24f1951a411d241bc8a4Brian Paul{
247489fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   _mesa_TexImage3D(target, level, (GLint) internalFormat, width, height,
2475663049a719ed76aa0a6f24f1951a411d241bc8a4Brian Paul                    depth, border, format, type, pixels);
2476663049a719ed76aa0a6f24f1951a411d241bc8a4Brian Paul}
2477663049a719ed76aa0a6f24f1951a411d241bc8a4Brian Paul
2478663049a719ed76aa0a6f24f1951a411d241bc8a4Brian Paul
2479afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2480c40d1dd62dd9bcbb97128e37a75d991a8d3b2d8cKendall Bennettvoid GLAPIENTRY
2481fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul_mesa_TexSubImage1D( GLenum target, GLint level,
2482fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul                     GLint xoffset, GLsizei width,
2483fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul                     GLenum format, GLenum type,
2484fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul                     const GLvoid *pixels )
2485afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg{
2486ab6e78f3a0abac7becbd8a58ed2c89f860cc6b0bBrian Paul   GLsizei postConvWidth = width;
24870293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul   struct gl_texture_unit *texUnit;
24880293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul   struct gl_texture_object *texObj;
24890293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul   struct gl_texture_image *texImage;
24902c3d34c905fa6b831a066afae83b938de05eb241Gareth Hughes   GET_CURRENT_CONTEXT(ctx);
249175280a2c742ee241d392618a3b29548fce1520a9Brian Paul   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
249275280a2c742ee241d392618a3b29548fce1520a9Brian Paul
2493b23f5b5a5ccf5910f1d11d749c2e440e6a46259bBrian Paul   if (ctx->NewState & _IMAGE_NEW_TRANSFER_STATE)
249408836341788a9f9d638d9dc8328510ccd18ddeb5Brian Paul      _mesa_update_state(ctx);
24952aadbf41dfd4f63c6118d0ad2d8659d289cbe454Brian Paul
2496f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul   /* XXX should test internal format */
2497f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul   if (is_color_format(format)) {
2498f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul      _mesa_adjust_image_for_convolution(ctx, 1, &postConvWidth, NULL);
2499f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul   }
2500afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2501c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   if (subtexture_error_check(ctx, 1, target, level, xoffset, 0, 0,
2502a805bb969199a0f2a5ca7224898a87112ccd2177Brian Paul                              postConvWidth, 1, 1, format, type)) {
2503f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul      return;   /* error was detected */
2504afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   }
2505afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
25060293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul   texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
25078e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul   texObj = _mesa_select_tex_object(ctx, texUnit, target);
25088e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul   texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
25090293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul   assert(texImage);
2510afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2511529e0a98d477e33f41f680926f8b9c745aebbe90Brian Paul   if (width == 0)
2512c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      return;  /* no-op, not an error */
2513afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2514347b3f0dafe8d0cc30e9d1e7595d12b0ac31bb4aBrian Paul   /* If we have a border, xoffset=-1 is legal.  Bias by border width */
2515347b3f0dafe8d0cc30e9d1e7595d12b0ac31bb4aBrian Paul   xoffset += texImage->Border;
2516347b3f0dafe8d0cc30e9d1e7595d12b0ac31bb4aBrian Paul
25178e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul   ASSERT(ctx->Driver.TexSubImage1D);
25188e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul   (*ctx->Driver.TexSubImage1D)(ctx, target, level, xoffset, width,
25198e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul                                format, type, pixels, &ctx->Unpack,
25208e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul                                texObj, texImage);
2521bf8b5f99a11ad43bb3404ed4ae04eceb3027bbabBrian Paul   ctx->NewState |= _NEW_TEXTURE;
2522afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg}
2523afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2524afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2525c40d1dd62dd9bcbb97128e37a75d991a8d3b2d8cKendall Bennettvoid GLAPIENTRY
2526fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul_mesa_TexSubImage2D( GLenum target, GLint level,
2527fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul                     GLint xoffset, GLint yoffset,
2528fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul                     GLsizei width, GLsizei height,
2529fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul                     GLenum format, GLenum type,
2530fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul                     const GLvoid *pixels )
2531afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg{
2532ab6e78f3a0abac7becbd8a58ed2c89f860cc6b0bBrian Paul   GLsizei postConvWidth = width, postConvHeight = height;
25330293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul   struct gl_texture_unit *texUnit;
25340293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul   struct gl_texture_object *texObj;
25350293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul   struct gl_texture_image *texImage;
25362c3d34c905fa6b831a066afae83b938de05eb241Gareth Hughes   GET_CURRENT_CONTEXT(ctx);
253775280a2c742ee241d392618a3b29548fce1520a9Brian Paul   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
253875280a2c742ee241d392618a3b29548fce1520a9Brian Paul
2539b23f5b5a5ccf5910f1d11d749c2e440e6a46259bBrian Paul   if (ctx->NewState & _IMAGE_NEW_TRANSFER_STATE)
254008836341788a9f9d638d9dc8328510ccd18ddeb5Brian Paul      _mesa_update_state(ctx);
25412aadbf41dfd4f63c6118d0ad2d8659d289cbe454Brian Paul
2542f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul   /* XXX should test internal format */
2543f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul   if (is_color_format(format)) {
2544f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul      _mesa_adjust_image_for_convolution(ctx, 2, &postConvWidth,
2545f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul                                         &postConvHeight);
2546f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul   }
2547afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2548c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   if (subtexture_error_check(ctx, 2, target, level, xoffset, yoffset, 0,
2549a805bb969199a0f2a5ca7224898a87112ccd2177Brian Paul                             postConvWidth, postConvHeight, 1, format, type)) {
2550f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul      return;   /* error was detected */
2551afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   }
2552afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
25530293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul   texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
255435d5301a54153930ee6fd60dff1010ce9f901397Brian Paul   texObj = _mesa_select_tex_object(ctx, texUnit, target);
25558e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul   texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
25560293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul   assert(texImage);
2557afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2558529e0a98d477e33f41f680926f8b9c745aebbe90Brian Paul   if (width == 0 || height == 0)
2559c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      return;  /* no-op, not an error */
2560afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2561347b3f0dafe8d0cc30e9d1e7595d12b0ac31bb4aBrian Paul   /* If we have a border, xoffset=-1 is legal.  Bias by border width */
2562347b3f0dafe8d0cc30e9d1e7595d12b0ac31bb4aBrian Paul   xoffset += texImage->Border;
2563347b3f0dafe8d0cc30e9d1e7595d12b0ac31bb4aBrian Paul   yoffset += texImage->Border;
2564347b3f0dafe8d0cc30e9d1e7595d12b0ac31bb4aBrian Paul
25658e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul   ASSERT(ctx->Driver.TexSubImage2D);
25668e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul   (*ctx->Driver.TexSubImage2D)(ctx, target, level, xoffset, yoffset,
25678e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul                                width, height, format, type, pixels,
25688e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul                                &ctx->Unpack, texObj, texImage);
2569bf8b5f99a11ad43bb3404ed4ae04eceb3027bbabBrian Paul   ctx->NewState |= _NEW_TEXTURE;
2570c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul}
2571afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2572afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2573afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2574c40d1dd62dd9bcbb97128e37a75d991a8d3b2d8cKendall Bennettvoid GLAPIENTRY
2575fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul_mesa_TexSubImage3D( GLenum target, GLint level,
2576fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul                     GLint xoffset, GLint yoffset, GLint zoffset,
2577fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul                     GLsizei width, GLsizei height, GLsizei depth,
2578fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul                     GLenum format, GLenum type,
2579fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul                     const GLvoid *pixels )
2580afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg{
25810293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul   struct gl_texture_unit *texUnit;
25820293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul   struct gl_texture_object *texObj;
25830293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul   struct gl_texture_image *texImage;
25842c3d34c905fa6b831a066afae83b938de05eb241Gareth Hughes   GET_CURRENT_CONTEXT(ctx);
258575280a2c742ee241d392618a3b29548fce1520a9Brian Paul   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
258675280a2c742ee241d392618a3b29548fce1520a9Brian Paul
2587b23f5b5a5ccf5910f1d11d749c2e440e6a46259bBrian Paul   if (ctx->NewState & _IMAGE_NEW_TRANSFER_STATE)
258808836341788a9f9d638d9dc8328510ccd18ddeb5Brian Paul      _mesa_update_state(ctx);
25892aadbf41dfd4f63c6118d0ad2d8659d289cbe454Brian Paul
2590c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   if (subtexture_error_check(ctx, 3, target, level, xoffset, yoffset, zoffset,
2591c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul                              width, height, depth, format, type)) {
2592f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul      return;   /* error was detected */
2593afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   }
2594afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
25950293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul   texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
25968e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul   texObj = _mesa_select_tex_object(ctx, texUnit, target);
25978e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul   texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
25980293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul   assert(texImage);
2599afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2600529e0a98d477e33f41f680926f8b9c745aebbe90Brian Paul   if (width == 0 || height == 0 || height == 0)
2601c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      return;  /* no-op, not an error */
2602afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2603347b3f0dafe8d0cc30e9d1e7595d12b0ac31bb4aBrian Paul   /* If we have a border, xoffset=-1 is legal.  Bias by border width */
2604347b3f0dafe8d0cc30e9d1e7595d12b0ac31bb4aBrian Paul   xoffset += texImage->Border;
2605347b3f0dafe8d0cc30e9d1e7595d12b0ac31bb4aBrian Paul   yoffset += texImage->Border;
2606347b3f0dafe8d0cc30e9d1e7595d12b0ac31bb4aBrian Paul   zoffset += texImage->Border;
2607347b3f0dafe8d0cc30e9d1e7595d12b0ac31bb4aBrian Paul
26088e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul   ASSERT(ctx->Driver.TexSubImage3D);
26098e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul   (*ctx->Driver.TexSubImage3D)(ctx, target, level,
26108e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul                                xoffset, yoffset, zoffset,
26118e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul                                width, height, depth,
26128e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul                                format, type, pixels,
26138e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul                                &ctx->Unpack, texObj, texImage );
2614bf8b5f99a11ad43bb3404ed4ae04eceb3027bbabBrian Paul   ctx->NewState |= _NEW_TEXTURE;
2615afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg}
2616afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2617afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2618afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2619c40d1dd62dd9bcbb97128e37a75d991a8d3b2d8cKendall Bennettvoid GLAPIENTRY
2620fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul_mesa_CopyTexImage1D( GLenum target, GLint level,
2621fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul                      GLenum internalFormat,
2622fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul                      GLint x, GLint y,
2623fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul                      GLsizei width, GLint border )
2624afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg{
26252aadbf41dfd4f63c6118d0ad2d8659d289cbe454Brian Paul   struct gl_texture_unit *texUnit;
26262aadbf41dfd4f63c6118d0ad2d8659d289cbe454Brian Paul   struct gl_texture_object *texObj;
26272aadbf41dfd4f63c6118d0ad2d8659d289cbe454Brian Paul   struct gl_texture_image *texImage;
2628ab6e78f3a0abac7becbd8a58ed2c89f860cc6b0bBrian Paul   GLsizei postConvWidth = width;
2629519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul   const GLuint face = texture_face(target);
2630fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul   GET_CURRENT_CONTEXT(ctx);
2631cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2632afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2633b23f5b5a5ccf5910f1d11d749c2e440e6a46259bBrian Paul   if (ctx->NewState & _IMAGE_NEW_TRANSFER_STATE)
263408836341788a9f9d638d9dc8328510ccd18ddeb5Brian Paul      _mesa_update_state(ctx);
2635fa4525e289b475b928a7b2c4055af9dd7fe46600Brian Paul
2636f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul   if (is_color_format(internalFormat)) {
2637f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul      _mesa_adjust_image_for_convolution(ctx, 1, &postConvWidth, NULL);
2638f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul   }
2639ab6e78f3a0abac7becbd8a58ed2c89f860cc6b0bBrian Paul
2640ab6e78f3a0abac7becbd8a58ed2c89f860cc6b0bBrian Paul   if (copytexture_error_check(ctx, 1, target, level, internalFormat,
2641ab6e78f3a0abac7becbd8a58ed2c89f860cc6b0bBrian Paul                               postConvWidth, 1, border))
2642ab6e78f3a0abac7becbd8a58ed2c89f860cc6b0bBrian Paul      return;
2643ab6e78f3a0abac7becbd8a58ed2c89f860cc6b0bBrian Paul
26442aadbf41dfd4f63c6118d0ad2d8659d289cbe454Brian Paul   texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
26452aadbf41dfd4f63c6118d0ad2d8659d289cbe454Brian Paul   texObj = _mesa_select_tex_object(ctx, texUnit, target);
2646a3f137094cd965d27e1b088499dd609b81a91906Brian Paul   texImage = _mesa_get_tex_image(ctx, texUnit, target, level);
26472aadbf41dfd4f63c6118d0ad2d8659d289cbe454Brian Paul   if (!texImage) {
2648a3f137094cd965d27e1b088499dd609b81a91906Brian Paul      _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexImage1D");
2649a3f137094cd965d27e1b088499dd609b81a91906Brian Paul      return;
26502aadbf41dfd4f63c6118d0ad2d8659d289cbe454Brian Paul   }
26513e62d3a8d88b48d4ed19e00ea2bbc3d0a2b6acf7Keith Whitwell   else if (texImage->Data) {
26523e62d3a8d88b48d4ed19e00ea2bbc3d0a2b6acf7Keith Whitwell      ctx->Driver.FreeTexImageData( ctx, texImage );
26532aadbf41dfd4f63c6118d0ad2d8659d289cbe454Brian Paul   }
26543e62d3a8d88b48d4ed19e00ea2bbc3d0a2b6acf7Keith Whitwell   ASSERT(texImage->Data == NULL);
26557dac13272a959654f1010b80c1f1ec6dcae5a4faBrian Paul
26562aadbf41dfd4f63c6118d0ad2d8659d289cbe454Brian Paul   clear_teximage_fields(texImage); /* not really needed, but helpful */
26578afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul   _mesa_init_teximage_fields(ctx, target, texImage, postConvWidth, 1, 1,
26583893e638e6521b9c070e01c0b31d22754ff97a88Brian Paul                              border, internalFormat);
26597dac13272a959654f1010b80c1f1ec6dcae5a4faBrian Paul
26602aadbf41dfd4f63c6118d0ad2d8659d289cbe454Brian Paul
26612aadbf41dfd4f63c6118d0ad2d8659d289cbe454Brian Paul   ASSERT(ctx->Driver.CopyTexImage1D);
26622aadbf41dfd4f63c6118d0ad2d8659d289cbe454Brian Paul   (*ctx->Driver.CopyTexImage1D)(ctx, target, level, internalFormat,
26632aadbf41dfd4f63c6118d0ad2d8659d289cbe454Brian Paul                                 x, y, width, border);
26642aadbf41dfd4f63c6118d0ad2d8659d289cbe454Brian Paul
2665e116d3b3e55a70ceb4af270ecd1b614117f631a2Brian Paul   ASSERT(texImage->TexFormat);
26664f295cee73bae1f687efe2dc062522b40d90b1e4Brian Paul
2667519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul   update_fbo_texture(ctx, texObj, face, level);
2668519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul
26692aadbf41dfd4f63c6118d0ad2d8659d289cbe454Brian Paul   /* state update */
26702aadbf41dfd4f63c6118d0ad2d8659d289cbe454Brian Paul   texObj->Complete = GL_FALSE;
26712aadbf41dfd4f63c6118d0ad2d8659d289cbe454Brian Paul   ctx->NewState |= _NEW_TEXTURE;
2672afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg}
2673afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2674afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2675afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2676c40d1dd62dd9bcbb97128e37a75d991a8d3b2d8cKendall Bennettvoid GLAPIENTRY
2677fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul_mesa_CopyTexImage2D( GLenum target, GLint level, GLenum internalFormat,
2678fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul                      GLint x, GLint y, GLsizei width, GLsizei height,
2679fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul                      GLint border )
2680afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg{
26812aadbf41dfd4f63c6118d0ad2d8659d289cbe454Brian Paul   struct gl_texture_unit *texUnit;
26822aadbf41dfd4f63c6118d0ad2d8659d289cbe454Brian Paul   struct gl_texture_object *texObj;
26832aadbf41dfd4f63c6118d0ad2d8659d289cbe454Brian Paul   struct gl_texture_image *texImage;
2684ab6e78f3a0abac7becbd8a58ed2c89f860cc6b0bBrian Paul   GLsizei postConvWidth = width, postConvHeight = height;
2685519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul   const GLuint face = texture_face(target);
2686fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul   GET_CURRENT_CONTEXT(ctx);
2687cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2688afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2689b23f5b5a5ccf5910f1d11d749c2e440e6a46259bBrian Paul   if (ctx->NewState & _IMAGE_NEW_TRANSFER_STATE)
269008836341788a9f9d638d9dc8328510ccd18ddeb5Brian Paul      _mesa_update_state(ctx);
2691fa4525e289b475b928a7b2c4055af9dd7fe46600Brian Paul
2692f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul   if (is_color_format(internalFormat)) {
2693f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul      _mesa_adjust_image_for_convolution(ctx, 2, &postConvWidth,
2694f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul                                         &postConvHeight);
2695f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul   }
2696ab6e78f3a0abac7becbd8a58ed2c89f860cc6b0bBrian Paul
2697ab6e78f3a0abac7becbd8a58ed2c89f860cc6b0bBrian Paul   if (copytexture_error_check(ctx, 2, target, level, internalFormat,
2698ab6e78f3a0abac7becbd8a58ed2c89f860cc6b0bBrian Paul                               postConvWidth, postConvHeight, border))
2699ab6e78f3a0abac7becbd8a58ed2c89f860cc6b0bBrian Paul      return;
2700ab6e78f3a0abac7becbd8a58ed2c89f860cc6b0bBrian Paul
27012aadbf41dfd4f63c6118d0ad2d8659d289cbe454Brian Paul   texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
27022aadbf41dfd4f63c6118d0ad2d8659d289cbe454Brian Paul   texObj = _mesa_select_tex_object(ctx, texUnit, target);
2703a3f137094cd965d27e1b088499dd609b81a91906Brian Paul   texImage = _mesa_get_tex_image(ctx, texUnit, target, level);
27042aadbf41dfd4f63c6118d0ad2d8659d289cbe454Brian Paul   if (!texImage) {
2705a3f137094cd965d27e1b088499dd609b81a91906Brian Paul      _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexImage2D");
2706a3f137094cd965d27e1b088499dd609b81a91906Brian Paul      return;
27072aadbf41dfd4f63c6118d0ad2d8659d289cbe454Brian Paul   }
27083e62d3a8d88b48d4ed19e00ea2bbc3d0a2b6acf7Keith Whitwell   else if (texImage->Data) {
27093e62d3a8d88b48d4ed19e00ea2bbc3d0a2b6acf7Keith Whitwell      ctx->Driver.FreeTexImageData( ctx, texImage );
27102aadbf41dfd4f63c6118d0ad2d8659d289cbe454Brian Paul   }
27113e62d3a8d88b48d4ed19e00ea2bbc3d0a2b6acf7Keith Whitwell   ASSERT(texImage->Data == NULL);
27127dac13272a959654f1010b80c1f1ec6dcae5a4faBrian Paul
27132aadbf41dfd4f63c6118d0ad2d8659d289cbe454Brian Paul   clear_teximage_fields(texImage); /* not really needed, but helpful */
27148afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul   _mesa_init_teximage_fields(ctx, target, texImage,
27158afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul                              postConvWidth, postConvHeight, 1,
27163893e638e6521b9c070e01c0b31d22754ff97a88Brian Paul                              border, internalFormat);
2717afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
27182aadbf41dfd4f63c6118d0ad2d8659d289cbe454Brian Paul   ASSERT(ctx->Driver.CopyTexImage2D);
27192aadbf41dfd4f63c6118d0ad2d8659d289cbe454Brian Paul   (*ctx->Driver.CopyTexImage2D)(ctx, target, level, internalFormat,
27202aadbf41dfd4f63c6118d0ad2d8659d289cbe454Brian Paul                                 x, y, width, height, border);
27212aadbf41dfd4f63c6118d0ad2d8659d289cbe454Brian Paul
2722e116d3b3e55a70ceb4af270ecd1b614117f631a2Brian Paul   ASSERT(texImage->TexFormat);
27234f295cee73bae1f687efe2dc062522b40d90b1e4Brian Paul
2724519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul   update_fbo_texture(ctx, texObj, face, level);
2725519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul
27262aadbf41dfd4f63c6118d0ad2d8659d289cbe454Brian Paul   /* state update */
27272aadbf41dfd4f63c6118d0ad2d8659d289cbe454Brian Paul   texObj->Complete = GL_FALSE;
27282aadbf41dfd4f63c6118d0ad2d8659d289cbe454Brian Paul   ctx->NewState |= _NEW_TEXTURE;
2729afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg}
2730afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2731afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2732afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2733c40d1dd62dd9bcbb97128e37a75d991a8d3b2d8cKendall Bennettvoid GLAPIENTRY
2734fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul_mesa_CopyTexSubImage1D( GLenum target, GLint level,
2735fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul                         GLint xoffset, GLint x, GLint y, GLsizei width )
2736afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg{
2737347b3f0dafe8d0cc30e9d1e7595d12b0ac31bb4aBrian Paul   struct gl_texture_unit *texUnit;
2738347b3f0dafe8d0cc30e9d1e7595d12b0ac31bb4aBrian Paul   struct gl_texture_image *texImage;
2739ab6e78f3a0abac7becbd8a58ed2c89f860cc6b0bBrian Paul   GLsizei postConvWidth = width;
2740fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul   GET_CURRENT_CONTEXT(ctx);
2741cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2742afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2743b23f5b5a5ccf5910f1d11d749c2e440e6a46259bBrian Paul   if (ctx->NewState & _IMAGE_NEW_TRANSFER_STATE)
274408836341788a9f9d638d9dc8328510ccd18ddeb5Brian Paul      _mesa_update_state(ctx);
2745fa4525e289b475b928a7b2c4055af9dd7fe46600Brian Paul
2746f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul   /* XXX should test internal format */
27478e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul   _mesa_adjust_image_for_convolution(ctx, 1, &postConvWidth, NULL);
2748ab6e78f3a0abac7becbd8a58ed2c89f860cc6b0bBrian Paul
2749ab6e78f3a0abac7becbd8a58ed2c89f860cc6b0bBrian Paul   if (copytexsubimage_error_check(ctx, 1, target, level,
2750ab6e78f3a0abac7becbd8a58ed2c89f860cc6b0bBrian Paul                                   xoffset, 0, 0, postConvWidth, 1))
2751ab6e78f3a0abac7becbd8a58ed2c89f860cc6b0bBrian Paul      return;
2752ab6e78f3a0abac7becbd8a58ed2c89f860cc6b0bBrian Paul
2753347b3f0dafe8d0cc30e9d1e7595d12b0ac31bb4aBrian Paul   texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2754347b3f0dafe8d0cc30e9d1e7595d12b0ac31bb4aBrian Paul   texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
2755a3f137094cd965d27e1b088499dd609b81a91906Brian Paul   ASSERT(texImage);
2756347b3f0dafe8d0cc30e9d1e7595d12b0ac31bb4aBrian Paul
2757347b3f0dafe8d0cc30e9d1e7595d12b0ac31bb4aBrian Paul   /* If we have a border, xoffset=-1 is legal.  Bias by border width */
2758347b3f0dafe8d0cc30e9d1e7595d12b0ac31bb4aBrian Paul   xoffset += texImage->Border;
2759347b3f0dafe8d0cc30e9d1e7595d12b0ac31bb4aBrian Paul
27602aadbf41dfd4f63c6118d0ad2d8659d289cbe454Brian Paul   ASSERT(ctx->Driver.CopyTexSubImage1D);
27612aadbf41dfd4f63c6118d0ad2d8659d289cbe454Brian Paul   (*ctx->Driver.CopyTexSubImage1D)(ctx, target, level, xoffset, x, y, width);
2762bf8b5f99a11ad43bb3404ed4ae04eceb3027bbabBrian Paul   ctx->NewState |= _NEW_TEXTURE;
2763afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg}
2764afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2765afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2766afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2767c40d1dd62dd9bcbb97128e37a75d991a8d3b2d8cKendall Bennettvoid GLAPIENTRY
2768fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul_mesa_CopyTexSubImage2D( GLenum target, GLint level,
2769fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul                         GLint xoffset, GLint yoffset,
2770fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul                         GLint x, GLint y, GLsizei width, GLsizei height )
2771afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg{
2772347b3f0dafe8d0cc30e9d1e7595d12b0ac31bb4aBrian Paul   struct gl_texture_unit *texUnit;
2773347b3f0dafe8d0cc30e9d1e7595d12b0ac31bb4aBrian Paul   struct gl_texture_image *texImage;
2774ab6e78f3a0abac7becbd8a58ed2c89f860cc6b0bBrian Paul   GLsizei postConvWidth = width, postConvHeight = height;
2775fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul   GET_CURRENT_CONTEXT(ctx);
2776cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2777afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2778b23f5b5a5ccf5910f1d11d749c2e440e6a46259bBrian Paul   if (ctx->NewState & _IMAGE_NEW_TRANSFER_STATE)
277908836341788a9f9d638d9dc8328510ccd18ddeb5Brian Paul      _mesa_update_state(ctx);
2780fa4525e289b475b928a7b2c4055af9dd7fe46600Brian Paul
2781f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul   /* XXX should test internal format */
2782f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul   _mesa_adjust_image_for_convolution(ctx, 2, &postConvWidth, &postConvHeight);
2783ab6e78f3a0abac7becbd8a58ed2c89f860cc6b0bBrian Paul
2784ab6e78f3a0abac7becbd8a58ed2c89f860cc6b0bBrian Paul   if (copytexsubimage_error_check(ctx, 2, target, level, xoffset, yoffset, 0,
2785ab6e78f3a0abac7becbd8a58ed2c89f860cc6b0bBrian Paul                                   postConvWidth, postConvHeight))
2786ab6e78f3a0abac7becbd8a58ed2c89f860cc6b0bBrian Paul      return;
2787ab6e78f3a0abac7becbd8a58ed2c89f860cc6b0bBrian Paul
2788347b3f0dafe8d0cc30e9d1e7595d12b0ac31bb4aBrian Paul   texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2789347b3f0dafe8d0cc30e9d1e7595d12b0ac31bb4aBrian Paul   texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
2790a3f137094cd965d27e1b088499dd609b81a91906Brian Paul   ASSERT(texImage);
2791347b3f0dafe8d0cc30e9d1e7595d12b0ac31bb4aBrian Paul
2792347b3f0dafe8d0cc30e9d1e7595d12b0ac31bb4aBrian Paul   /* If we have a border, xoffset=-1 is legal.  Bias by border width */
2793347b3f0dafe8d0cc30e9d1e7595d12b0ac31bb4aBrian Paul   xoffset += texImage->Border;
2794347b3f0dafe8d0cc30e9d1e7595d12b0ac31bb4aBrian Paul   yoffset += texImage->Border;
2795347b3f0dafe8d0cc30e9d1e7595d12b0ac31bb4aBrian Paul
27962aadbf41dfd4f63c6118d0ad2d8659d289cbe454Brian Paul   ASSERT(ctx->Driver.CopyTexSubImage2D);
27972aadbf41dfd4f63c6118d0ad2d8659d289cbe454Brian Paul   (*ctx->Driver.CopyTexSubImage2D)(ctx, target, level,
27982aadbf41dfd4f63c6118d0ad2d8659d289cbe454Brian Paul                                    xoffset, yoffset, x, y, width, height);
2799bf8b5f99a11ad43bb3404ed4ae04eceb3027bbabBrian Paul   ctx->NewState |= _NEW_TEXTURE;
2800afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg}
2801afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2802afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2803afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2804c40d1dd62dd9bcbb97128e37a75d991a8d3b2d8cKendall Bennettvoid GLAPIENTRY
2805fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul_mesa_CopyTexSubImage3D( GLenum target, GLint level,
2806fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul                         GLint xoffset, GLint yoffset, GLint zoffset,
2807fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul                         GLint x, GLint y, GLsizei width, GLsizei height )
2808afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg{
2809347b3f0dafe8d0cc30e9d1e7595d12b0ac31bb4aBrian Paul   struct gl_texture_unit *texUnit;
2810347b3f0dafe8d0cc30e9d1e7595d12b0ac31bb4aBrian Paul   struct gl_texture_image *texImage;
2811ab6e78f3a0abac7becbd8a58ed2c89f860cc6b0bBrian Paul   GLsizei postConvWidth = width, postConvHeight = height;
2812fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul   GET_CURRENT_CONTEXT(ctx);
2813cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2814afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2815b23f5b5a5ccf5910f1d11d749c2e440e6a46259bBrian Paul   if (ctx->NewState & _IMAGE_NEW_TRANSFER_STATE)
281608836341788a9f9d638d9dc8328510ccd18ddeb5Brian Paul      _mesa_update_state(ctx);
2817fa4525e289b475b928a7b2c4055af9dd7fe46600Brian Paul
2818f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul   /* XXX should test internal format */
2819f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul   _mesa_adjust_image_for_convolution(ctx, 2, &postConvWidth, &postConvHeight);
2820ab6e78f3a0abac7becbd8a58ed2c89f860cc6b0bBrian Paul
2821ab6e78f3a0abac7becbd8a58ed2c89f860cc6b0bBrian Paul   if (copytexsubimage_error_check(ctx, 3, target, level, xoffset, yoffset,
2822ab6e78f3a0abac7becbd8a58ed2c89f860cc6b0bBrian Paul                                   zoffset, postConvWidth, postConvHeight))
2823ab6e78f3a0abac7becbd8a58ed2c89f860cc6b0bBrian Paul      return;
2824ab6e78f3a0abac7becbd8a58ed2c89f860cc6b0bBrian Paul
2825347b3f0dafe8d0cc30e9d1e7595d12b0ac31bb4aBrian Paul   texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2826347b3f0dafe8d0cc30e9d1e7595d12b0ac31bb4aBrian Paul   texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
2827a3f137094cd965d27e1b088499dd609b81a91906Brian Paul   ASSERT(texImage);
2828347b3f0dafe8d0cc30e9d1e7595d12b0ac31bb4aBrian Paul
2829347b3f0dafe8d0cc30e9d1e7595d12b0ac31bb4aBrian Paul   /* If we have a border, xoffset=-1 is legal.  Bias by border width */
2830347b3f0dafe8d0cc30e9d1e7595d12b0ac31bb4aBrian Paul   xoffset += texImage->Border;
2831347b3f0dafe8d0cc30e9d1e7595d12b0ac31bb4aBrian Paul   yoffset += texImage->Border;
2832347b3f0dafe8d0cc30e9d1e7595d12b0ac31bb4aBrian Paul   zoffset += texImage->Border;
2833347b3f0dafe8d0cc30e9d1e7595d12b0ac31bb4aBrian Paul
28342aadbf41dfd4f63c6118d0ad2d8659d289cbe454Brian Paul   ASSERT(ctx->Driver.CopyTexSubImage3D);
28352aadbf41dfd4f63c6118d0ad2d8659d289cbe454Brian Paul   (*ctx->Driver.CopyTexSubImage3D)(ctx, target, level,
28362aadbf41dfd4f63c6118d0ad2d8659d289cbe454Brian Paul                                    xoffset, yoffset, zoffset,
28372aadbf41dfd4f63c6118d0ad2d8659d289cbe454Brian Paul                                    x, y, width, height);
2838bf8b5f99a11ad43bb3404ed4ae04eceb3027bbabBrian Paul   ctx->NewState |= _NEW_TEXTURE;
2839afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg}
28401207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul
28411207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul
28421207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul
284389fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul
284489fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul/**********************************************************************/
284589fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul/******                   Compressed Textures                    ******/
284689fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul/**********************************************************************/
284789fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul
284889fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul
284989fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul/**
285089fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul * Error checking for glCompressedTexImage[123]D().
285189fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul * \return error code or GL_NO_ERROR.
285289fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul */
285389fb06fcc11cbe3f23521312155d6c55d869f526Brian Paulstatic GLenum
285489fb06fcc11cbe3f23521312155d6c55d869f526Brian Paulcompressed_texture_error_check(GLcontext *ctx, GLint dimensions,
285589fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul                               GLenum target, GLint level,
285689fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul                               GLenum internalFormat, GLsizei width,
285789fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul                               GLsizei height, GLsizei depth, GLint border,
285889fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul                               GLsizei imageSize)
285989fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul{
2860c3c19be8e0d0b13916cc128cf3c8e839935c912aBrian Paul   GLint expectedSize, maxLevels = 0, maxTextureSize;
286189fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul
286289fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   if (dimensions == 1) {
286389fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      /* 1D compressed textures not allowed */
286489fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      return GL_INVALID_ENUM;
286589fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   }
286689fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   else if (dimensions == 2) {
286789fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      if (target == GL_PROXY_TEXTURE_2D) {
286889fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         maxLevels = ctx->Const.MaxTextureLevels;
286989fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      }
287089fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      else if (target == GL_TEXTURE_2D) {
287189fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         maxLevels = ctx->Const.MaxTextureLevels;
287289fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      }
287389fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      else if (target == GL_PROXY_TEXTURE_CUBE_MAP_ARB) {
287489fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         if (!ctx->Extensions.ARB_texture_cube_map)
287589fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul            return GL_INVALID_ENUM; /*target*/
287689fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         maxLevels = ctx->Const.MaxCubeTextureLevels;
287789fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      }
287889fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      else if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
287989fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul               target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB) {
288089fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         if (!ctx->Extensions.ARB_texture_cube_map)
288189fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul            return GL_INVALID_ENUM; /*target*/
288289fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         maxLevels = ctx->Const.MaxCubeTextureLevels;
288389fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      }
288489fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      else {
288589fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         return GL_INVALID_ENUM; /*target*/
288689fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      }
288789fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   }
288889fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   else if (dimensions == 3) {
288989fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      /* 3D compressed textures not allowed */
289089fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      return GL_INVALID_ENUM;
289189fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   }
289289fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul
289389fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   maxTextureSize = 1 << (maxLevels - 1);
289489fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul
2895c3c19be8e0d0b13916cc128cf3c8e839935c912aBrian Paul   /* This will detect any invalid internalFormat value */
289640bd9d0b190e11d39350d1b08d2c2b28e3040bcaDaniel Borca   if (!is_compressed_format(ctx, internalFormat))
289789fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      return GL_INVALID_ENUM;
289889fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul
2899c3c19be8e0d0b13916cc128cf3c8e839935c912aBrian Paul   /* This should really never fail */
2900a2dc424acfe83f3c66d3509f5a1efc24ce884367Ian Romanick   if (_mesa_base_tex_format(ctx, internalFormat) < 0)
2901a2dc424acfe83f3c66d3509f5a1efc24ce884367Ian Romanick      return GL_INVALID_ENUM;
2902a2dc424acfe83f3c66d3509f5a1efc24ce884367Ian Romanick
290389fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   if (border != 0)
290489fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      return GL_INVALID_VALUE;
290589fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul
2906887e2cf01a99f7fe1b7c94320b7bdbbf0d6ad2f8Brian Paul   /*
2907887e2cf01a99f7fe1b7c94320b7bdbbf0d6ad2f8Brian Paul    * XXX We should probably use the proxy texture error check function here.
2908887e2cf01a99f7fe1b7c94320b7bdbbf0d6ad2f8Brian Paul    */
2909887e2cf01a99f7fe1b7c94320b7bdbbf0d6ad2f8Brian Paul   if (width < 1 || width > maxTextureSize ||
29102982dce27e242c89e4bc99979a878263df33ad2fDaniel Borca       (!ctx->Extensions.ARB_texture_non_power_of_two && _mesa_bitcount(width) != 1))
291189fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      return GL_INVALID_VALUE;
291289fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul
2913887e2cf01a99f7fe1b7c94320b7bdbbf0d6ad2f8Brian Paul   if ((height < 1 || height > maxTextureSize ||
29142982dce27e242c89e4bc99979a878263df33ad2fDaniel Borca       (!ctx->Extensions.ARB_texture_non_power_of_two && _mesa_bitcount(height) != 1))
291589fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul       && dimensions > 1)
291689fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      return GL_INVALID_VALUE;
291789fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul
2918887e2cf01a99f7fe1b7c94320b7bdbbf0d6ad2f8Brian Paul   if ((depth < 1 || depth > maxTextureSize ||
29192982dce27e242c89e4bc99979a878263df33ad2fDaniel Borca       (!ctx->Extensions.ARB_texture_non_power_of_two && _mesa_bitcount(depth) != 1))
292089fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul       && dimensions > 2)
292189fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      return GL_INVALID_VALUE;
292289fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul
292389fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   /* For cube map, width must equal height */
292489fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
292589fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul       target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB && width != height)
292689fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      return GL_INVALID_VALUE;
292789fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul
292889fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   if (level < 0 || level >= maxLevels)
292989fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      return GL_INVALID_VALUE;
293089fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul
2931c3c19be8e0d0b13916cc128cf3c8e839935c912aBrian Paul   expectedSize = _mesa_compressed_texture_size_glenum(ctx, width, height,
2932c3c19be8e0d0b13916cc128cf3c8e839935c912aBrian Paul                                                       depth, internalFormat);
293389fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   if (expectedSize != imageSize)
293489fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      return GL_INVALID_VALUE;
293589fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul
293689fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   return GL_NO_ERROR;
293789fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul}
293889fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul
293989fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul
294089fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul/**
294189fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul * Error checking for glCompressedTexSubImage[123]D().
29422371033bbaf6e7477bfa893654a0467a084f335bBrian Paul * \warning  There are some bad assumptions here about the size of compressed
29432371033bbaf6e7477bfa893654a0467a084f335bBrian Paul *           texture tiles (multiple of 4) used to test the validity of the
29442371033bbaf6e7477bfa893654a0467a084f335bBrian Paul *           offset and size parameters.
294589fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul * \return error code or GL_NO_ERROR.
294689fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul */
294789fb06fcc11cbe3f23521312155d6c55d869f526Brian Paulstatic GLenum
294889fb06fcc11cbe3f23521312155d6c55d869f526Brian Paulcompressed_subtexture_error_check(GLcontext *ctx, GLint dimensions,
294989fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul                                  GLenum target, GLint level,
295089fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul                                  GLint xoffset, GLint yoffset, GLint zoffset,
295189fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul                                  GLsizei width, GLsizei height, GLsizei depth,
295289fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul                                  GLenum format, GLsizei imageSize)
295389fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul{
295489fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   GLint expectedSize, maxLevels = 0, maxTextureSize;
2955a6c423d95663cfd8601cf84e10e8e1b12fa6ef15Brian Paul   (void) zoffset;
295689fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul
295789fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   if (dimensions == 1) {
295889fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      /* 1D compressed textures not allowed */
295989fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      return GL_INVALID_ENUM;
296089fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   }
296189fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   else if (dimensions == 2) {
296289fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      if (target == GL_PROXY_TEXTURE_2D) {
296389fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         maxLevels = ctx->Const.MaxTextureLevels;
296489fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      }
296589fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      else if (target == GL_TEXTURE_2D) {
296689fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         maxLevels = ctx->Const.MaxTextureLevels;
296789fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      }
296889fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      else if (target == GL_PROXY_TEXTURE_CUBE_MAP_ARB) {
296989fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         if (!ctx->Extensions.ARB_texture_cube_map)
297089fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul            return GL_INVALID_ENUM; /*target*/
297189fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         maxLevels = ctx->Const.MaxCubeTextureLevels;
297289fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      }
297389fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      else if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
297489fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul               target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB) {
297589fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         if (!ctx->Extensions.ARB_texture_cube_map)
297689fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul            return GL_INVALID_ENUM; /*target*/
297789fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         maxLevels = ctx->Const.MaxCubeTextureLevels;
297889fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      }
297989fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      else {
298089fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         return GL_INVALID_ENUM; /*target*/
298189fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      }
298289fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   }
298389fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   else if (dimensions == 3) {
298489fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      /* 3D compressed textures not allowed */
298589fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      return GL_INVALID_ENUM;
298689fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   }
298789fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul
298889fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   maxTextureSize = 1 << (maxLevels - 1);
298989fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul
2990c3c19be8e0d0b13916cc128cf3c8e839935c912aBrian Paul   /* this will catch any invalid compressed format token */
299140bd9d0b190e11d39350d1b08d2c2b28e3040bcaDaniel Borca   if (!is_compressed_format(ctx, format))
299289fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      return GL_INVALID_ENUM;
299389fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul
2994887e2cf01a99f7fe1b7c94320b7bdbbf0d6ad2f8Brian Paul   if (width < 1 || width > maxTextureSize)
299589fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      return GL_INVALID_VALUE;
299689fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul
2997887e2cf01a99f7fe1b7c94320b7bdbbf0d6ad2f8Brian Paul   if ((height < 1 || height > maxTextureSize)
299889fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul       && dimensions > 1)
299989fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      return GL_INVALID_VALUE;
300089fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul
300189fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   if (level < 0 || level >= maxLevels)
300289fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      return GL_INVALID_VALUE;
300389fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul
30042371033bbaf6e7477bfa893654a0467a084f335bBrian Paul   /* XXX these tests are specific to the compressed format.
30052371033bbaf6e7477bfa893654a0467a084f335bBrian Paul    * this code should be generalized in some way.
30062371033bbaf6e7477bfa893654a0467a084f335bBrian Paul    */
300789fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   if ((xoffset & 3) != 0 || (yoffset & 3) != 0)
300889fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      return GL_INVALID_VALUE;
300989fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul
301089fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   if ((width & 3) != 0 && width != 2 && width != 1)
301189fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      return GL_INVALID_VALUE;
301289fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul
301389fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   if ((height & 3) != 0 && height != 2 && height != 1)
301489fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      return GL_INVALID_VALUE;
301589fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul
3016c3c19be8e0d0b13916cc128cf3c8e839935c912aBrian Paul   expectedSize = _mesa_compressed_texture_size_glenum(ctx, width, height,
3017c3c19be8e0d0b13916cc128cf3c8e839935c912aBrian Paul                                                       depth, format);
301889fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   if (expectedSize != imageSize)
301989fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      return GL_INVALID_VALUE;
302089fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul
302189fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   return GL_NO_ERROR;
302289fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul}
302389fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul
302489fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul
302589fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul
3026c40d1dd62dd9bcbb97128e37a75d991a8d3b2d8cKendall Bennettvoid GLAPIENTRY
30271207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul_mesa_CompressedTexImage1DARB(GLenum target, GLint level,
3028aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul                              GLenum internalFormat, GLsizei width,
30291207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul                              GLint border, GLsizei imageSize,
30301207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul                              const GLvoid *data)
30311207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul{
3032aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul   GET_CURRENT_CONTEXT(ctx);
3033cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3034aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul
3035aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul   if (target == GL_TEXTURE_1D) {
30368c3ddf4270ff075ee783a67e5d5d04fa16a9cb45Brian Paul      /* non-proxy target */
3037aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul      struct gl_texture_unit *texUnit;
3038aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul      struct gl_texture_object *texObj;
3039aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul      struct gl_texture_image *texImage;
304089fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      GLenum error = compressed_texture_error_check(ctx, 1, target, level,
304189fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul                               internalFormat, width, 1, 1, border, imageSize);
304289fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      if (error) {
304389fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         _mesa_error(ctx, error, "glCompressedTexImage1D");
304489fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         return;
3045aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul      }
3046aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul
3047aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul      texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
30488e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul      texObj = _mesa_select_tex_object(ctx, texUnit, target);
3049a3f137094cd965d27e1b088499dd609b81a91906Brian Paul      texImage = _mesa_get_tex_image(ctx, texUnit, target, level);
3050aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul      if (!texImage) {
3051a3f137094cd965d27e1b088499dd609b81a91906Brian Paul         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCompressedTexImage1D");
3052a3f137094cd965d27e1b088499dd609b81a91906Brian Paul         return;
3053aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul      }
30543e62d3a8d88b48d4ed19e00ea2bbc3d0a2b6acf7Keith Whitwell      else if (texImage->Data) {
30553e62d3a8d88b48d4ed19e00ea2bbc3d0a2b6acf7Keith Whitwell	 ctx->Driver.FreeTexImageData( ctx, texImage );
3056aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul      }
30573e62d3a8d88b48d4ed19e00ea2bbc3d0a2b6acf7Keith Whitwell      ASSERT(texImage->Data == NULL);
3058aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul
30598afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul      _mesa_init_teximage_fields(ctx, target, texImage, width, 1, 1,
30603893e638e6521b9c070e01c0b31d22754ff97a88Brian Paul                                 border, internalFormat);
30616628bc9cff74a6d524165e809f73eabc85ba34b5Brian Paul
306289fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      ASSERT(ctx->Driver.CompressedTexImage1D);
306389fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      (*ctx->Driver.CompressedTexImage1D)(ctx, target, level,
306489fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul                                          internalFormat, width, border,
306589fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul                                          imageSize, data,
306689fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul                                          texObj, texImage);
3067aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul
3068aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul      /* state update */
3069a852378a6289d154364dde440f89a39bbfc33e2dBrian Paul      texObj->Complete = GL_FALSE;
3070a96308c37db0bc0086a017d318bc3504aa5f0b1aKeith Whitwell      ctx->NewState |= _NEW_TEXTURE;
3071aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul   }
3072aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul   else if (target == GL_PROXY_TEXTURE_1D) {
3073aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul      /* Proxy texture: check for errors and update proxy state */
307489fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      GLenum error = compressed_texture_error_check(ctx, 1, target, level,
307589fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul                               internalFormat, width, 1, 1, border, imageSize);
30768e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul      if (!error) {
30778e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul         ASSERT(ctx->Driver.TestProxyTexImage);
307838d3f3d660225150715bc06f6b33e394ba803299Brian Paul         error = !(*ctx->Driver.TestProxyTexImage)(ctx, target, level,
307938d3f3d660225150715bc06f6b33e394ba803299Brian Paul                                             internalFormat, GL_NONE, GL_NONE,
308038d3f3d660225150715bc06f6b33e394ba803299Brian Paul                                             width, 1, 1, border);
308138d3f3d660225150715bc06f6b33e394ba803299Brian Paul      }
308238d3f3d660225150715bc06f6b33e394ba803299Brian Paul      if (error) {
3083aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul         /* if error, clear all proxy texture image parameters */
3084a3f137094cd965d27e1b088499dd609b81a91906Brian Paul         struct gl_texture_image *texImage;
3085a3f137094cd965d27e1b088499dd609b81a91906Brian Paul         texImage = _mesa_get_proxy_tex_image(ctx, target, level);
3086a3f137094cd965d27e1b088499dd609b81a91906Brian Paul         if (texImage)
3087a3f137094cd965d27e1b088499dd609b81a91906Brian Paul            clear_teximage_fields(texImage);
3088aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul      }
308989fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      else {
309089fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         /* store the teximage parameters */
309189fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         struct gl_texture_unit *texUnit;
309289fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         struct gl_texture_image *texImage;
309389fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
309489fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
309589fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         _mesa_init_teximage_fields(ctx, target, texImage, width, 1, 1,
309689fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul                                    border, internalFormat);
309789fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      }
3098aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul   }
3099aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul   else {
310089fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      _mesa_error(ctx, GL_INVALID_ENUM, "glCompressedTexImage1D(target)");
3101aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul      return;
3102aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul   }
31031207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul}
31041207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul
31051207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul
3106c40d1dd62dd9bcbb97128e37a75d991a8d3b2d8cKendall Bennettvoid GLAPIENTRY
31071207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul_mesa_CompressedTexImage2DARB(GLenum target, GLint level,
3108aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul                              GLenum internalFormat, GLsizei width,
31091207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul                              GLsizei height, GLint border, GLsizei imageSize,
31101207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul                              const GLvoid *data)
31111207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul{
3112aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul   GET_CURRENT_CONTEXT(ctx);
3113cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3114aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul
31158e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul   if (target == GL_TEXTURE_2D ||
3116a96308c37db0bc0086a017d318bc3504aa5f0b1aKeith Whitwell       (ctx->Extensions.ARB_texture_cube_map &&
31179540a1dbcf213645b23c6cd9284e92a1152cca05Brian Paul        target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
311889fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul        target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB)) {
31198c3ddf4270ff075ee783a67e5d5d04fa16a9cb45Brian Paul      /* non-proxy target */
3120aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul      struct gl_texture_unit *texUnit;
3121aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul      struct gl_texture_object *texObj;
3122aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul      struct gl_texture_image *texImage;
312389fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      GLenum error = compressed_texture_error_check(ctx, 2, target, level,
312489fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul                          internalFormat, width, height, 1, border, imageSize);
312589fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      if (error) {
312689fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         _mesa_error(ctx, error, "glCompressedTexImage2D");
312789fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         return;
3128aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul      }
3129aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul
3130aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul      texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
31318e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul      texObj = _mesa_select_tex_object(ctx, texUnit, target);
3132a3f137094cd965d27e1b088499dd609b81a91906Brian Paul      texImage = _mesa_get_tex_image(ctx, texUnit, target, level);
3133aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul      if (!texImage) {
3134a3f137094cd965d27e1b088499dd609b81a91906Brian Paul         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCompressedTexImage2D");
3135a3f137094cd965d27e1b088499dd609b81a91906Brian Paul         return;
3136aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul      }
31373e62d3a8d88b48d4ed19e00ea2bbc3d0a2b6acf7Keith Whitwell      else if (texImage->Data) {
31383e62d3a8d88b48d4ed19e00ea2bbc3d0a2b6acf7Keith Whitwell	 ctx->Driver.FreeTexImageData( ctx, texImage );
3139aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul      }
31403e62d3a8d88b48d4ed19e00ea2bbc3d0a2b6acf7Keith Whitwell      ASSERT(texImage->Data == NULL);
3141aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul
31428afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul      _mesa_init_teximage_fields(ctx, target, texImage, width, height, 1,
31438afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul                                 border, internalFormat);
31446628bc9cff74a6d524165e809f73eabc85ba34b5Brian Paul
314589fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      ASSERT(ctx->Driver.CompressedTexImage2D);
314689fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      (*ctx->Driver.CompressedTexImage2D)(ctx, target, level,
314789fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul                                          internalFormat, width, height,
314889fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul                                          border, imageSize, data,
314989fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul                                          texObj, texImage);
3150aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul
3151aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul      /* state update */
3152a852378a6289d154364dde440f89a39bbfc33e2dBrian Paul      texObj->Complete = GL_FALSE;
3153a96308c37db0bc0086a017d318bc3504aa5f0b1aKeith Whitwell      ctx->NewState |= _NEW_TEXTURE;
3154aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul   }
315589fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   else if (target == GL_PROXY_TEXTURE_2D ||
315689fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul            (target == GL_PROXY_TEXTURE_CUBE_MAP_ARB &&
315789fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul             ctx->Extensions.ARB_texture_cube_map)) {
3158aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul      /* Proxy texture: check for errors and update proxy state */
315989fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      GLenum error = compressed_texture_error_check(ctx, 2, target, level,
316089fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul                          internalFormat, width, height, 1, border, imageSize);
31618e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul      if (!error) {
31628e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul         ASSERT(ctx->Driver.TestProxyTexImage);
316338d3f3d660225150715bc06f6b33e394ba803299Brian Paul         error = !(*ctx->Driver.TestProxyTexImage)(ctx, target, level,
316438d3f3d660225150715bc06f6b33e394ba803299Brian Paul                                              internalFormat, GL_NONE, GL_NONE,
316538d3f3d660225150715bc06f6b33e394ba803299Brian Paul                                              width, height, 1, border);
316638d3f3d660225150715bc06f6b33e394ba803299Brian Paul      }
316738d3f3d660225150715bc06f6b33e394ba803299Brian Paul      if (error) {
3168aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul         /* if error, clear all proxy texture image parameters */
3169a3f137094cd965d27e1b088499dd609b81a91906Brian Paul         struct gl_texture_image *texImage;
3170a3f137094cd965d27e1b088499dd609b81a91906Brian Paul         texImage = _mesa_get_proxy_tex_image(ctx, target, level);
3171a3f137094cd965d27e1b088499dd609b81a91906Brian Paul         if (texImage)
3172a3f137094cd965d27e1b088499dd609b81a91906Brian Paul            clear_teximage_fields(texImage);
3173aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul      }
317489fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      else {
317589fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         /* store the teximage parameters */
317689fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         struct gl_texture_unit *texUnit;
317789fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         struct gl_texture_image *texImage;
317889fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
317989fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
318089fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         _mesa_init_teximage_fields(ctx, target, texImage, width, height, 1,
318189fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul                                    border, internalFormat);
318289fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      }
3183aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul   }
3184aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul   else {
318589fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      _mesa_error(ctx, GL_INVALID_ENUM, "glCompressedTexImage2D(target)");
3186aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul      return;
3187aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul   }
31881207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul}
31891207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul
31901207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul
3191c40d1dd62dd9bcbb97128e37a75d991a8d3b2d8cKendall Bennettvoid GLAPIENTRY
31921207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul_mesa_CompressedTexImage3DARB(GLenum target, GLint level,
3193aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul                              GLenum internalFormat, GLsizei width,
31941207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul                              GLsizei height, GLsizei depth, GLint border,
31951207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul                              GLsizei imageSize, const GLvoid *data)
31961207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul{
3197aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul   GET_CURRENT_CONTEXT(ctx);
3198cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3199aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul
32009540a1dbcf213645b23c6cd9284e92a1152cca05Brian Paul   if (target == GL_TEXTURE_3D) {
32018c3ddf4270ff075ee783a67e5d5d04fa16a9cb45Brian Paul      /* non-proxy target */
3202aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul      struct gl_texture_unit *texUnit;
3203aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul      struct gl_texture_object *texObj;
3204aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul      struct gl_texture_image *texImage;
320589fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      GLenum error = compressed_texture_error_check(ctx, 3, target, level,
320689fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul                      internalFormat, width, height, depth, border, imageSize);
320789fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      if (error) {
320889fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         _mesa_error(ctx, error, "glCompressedTexImage3D");
320989fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         return;
3210aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul      }
3211aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul
3212aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul      texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
32138e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul      texObj = _mesa_select_tex_object(ctx, texUnit, target);
3214a3f137094cd965d27e1b088499dd609b81a91906Brian Paul      texImage = _mesa_get_tex_image(ctx, texUnit, target, level);
3215aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul      if (!texImage) {
3216a3f137094cd965d27e1b088499dd609b81a91906Brian Paul         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCompressedTexImage3D");
3217a3f137094cd965d27e1b088499dd609b81a91906Brian Paul         return;
3218aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul      }
32193e62d3a8d88b48d4ed19e00ea2bbc3d0a2b6acf7Keith Whitwell      else if (texImage->Data) {
32203e62d3a8d88b48d4ed19e00ea2bbc3d0a2b6acf7Keith Whitwell	 ctx->Driver.FreeTexImageData( ctx, texImage );
3221aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul      }
32223e62d3a8d88b48d4ed19e00ea2bbc3d0a2b6acf7Keith Whitwell      ASSERT(texImage->Data == NULL);
3223aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul
32248afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul      _mesa_init_teximage_fields(ctx, target, texImage, width, height, depth,
32258afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul                                 border, internalFormat);
32266628bc9cff74a6d524165e809f73eabc85ba34b5Brian Paul
322789fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      ASSERT(ctx->Driver.CompressedTexImage3D);
322889fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      (*ctx->Driver.CompressedTexImage3D)(ctx, target, level,
322989fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul                                          internalFormat,
323089fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul                                          width, height, depth,
323189fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul                                          border, imageSize, data,
323289fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul                                          texObj, texImage);
3233aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul
3234aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul      /* state update */
3235a852378a6289d154364dde440f89a39bbfc33e2dBrian Paul      texObj->Complete = GL_FALSE;
3236a96308c37db0bc0086a017d318bc3504aa5f0b1aKeith Whitwell      ctx->NewState |= _NEW_TEXTURE;
3237aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul   }
3238aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul   else if (target == GL_PROXY_TEXTURE_3D) {
3239aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul      /* Proxy texture: check for errors and update proxy state */
324089fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      GLenum error = compressed_texture_error_check(ctx, 3, target, level,
324189fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul                      internalFormat, width, height, depth, border, imageSize);
32428e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul      if (!error) {
32438e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul         ASSERT(ctx->Driver.TestProxyTexImage);
324438d3f3d660225150715bc06f6b33e394ba803299Brian Paul         error = !(*ctx->Driver.TestProxyTexImage)(ctx, target, level,
324538d3f3d660225150715bc06f6b33e394ba803299Brian Paul                                             internalFormat, GL_NONE, GL_NONE,
324638d3f3d660225150715bc06f6b33e394ba803299Brian Paul                                             width, height, depth, border);
324738d3f3d660225150715bc06f6b33e394ba803299Brian Paul      }
324838d3f3d660225150715bc06f6b33e394ba803299Brian Paul      if (error) {
3249aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul         /* if error, clear all proxy texture image parameters */
3250a3f137094cd965d27e1b088499dd609b81a91906Brian Paul         struct gl_texture_image *texImage;
3251a3f137094cd965d27e1b088499dd609b81a91906Brian Paul         texImage = _mesa_get_proxy_tex_image(ctx, target, level);
3252a3f137094cd965d27e1b088499dd609b81a91906Brian Paul         if (texImage)
3253a3f137094cd965d27e1b088499dd609b81a91906Brian Paul            clear_teximage_fields(texImage);
3254aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul      }
325589fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      else {
325689fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         /* store the teximage parameters */
325789fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         struct gl_texture_unit *texUnit;
325889fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         struct gl_texture_image *texImage;
325989fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
326089fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
326189fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul         _mesa_init_teximage_fields(ctx, target, texImage, width, height,
326289fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul                                    depth, border, internalFormat);
326389fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      }
3264aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul   }
3265aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul   else {
326689fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      _mesa_error(ctx, GL_INVALID_ENUM, "glCompressedTexImage3D(target)");
3267aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul      return;
3268aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul   }
32691207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul}
32701207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul
32711207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul
3272c40d1dd62dd9bcbb97128e37a75d991a8d3b2d8cKendall Bennettvoid GLAPIENTRY
32731207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul_mesa_CompressedTexSubImage1DARB(GLenum target, GLint level, GLint xoffset,
32741207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul                                 GLsizei width, GLenum format,
32751207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul                                 GLsizei imageSize, const GLvoid *data)
32761207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul{
32779540a1dbcf213645b23c6cd9284e92a1152cca05Brian Paul   struct gl_texture_unit *texUnit;
32789540a1dbcf213645b23c6cd9284e92a1152cca05Brian Paul   struct gl_texture_object *texObj;
32799540a1dbcf213645b23c6cd9284e92a1152cca05Brian Paul   struct gl_texture_image *texImage;
328089fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   GLenum error;
3281a1f1586bcb9fcd4a30930fd14b73323642ce85c5Brian Paul   GET_CURRENT_CONTEXT(ctx);
328275280a2c742ee241d392618a3b29548fce1520a9Brian Paul   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
328375280a2c742ee241d392618a3b29548fce1520a9Brian Paul
328489fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   error = compressed_subtexture_error_check(ctx, 1, target, level,
3285c3c19be8e0d0b13916cc128cf3c8e839935c912aBrian Paul                                             xoffset, 0, 0, /* pos */
3286c3c19be8e0d0b13916cc128cf3c8e839935c912aBrian Paul                                             width, 1, 1,   /* size */
3287c3c19be8e0d0b13916cc128cf3c8e839935c912aBrian Paul                                             format, imageSize);
328889fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   if (error) {
328989fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      _mesa_error(ctx, error, "glCompressedTexSubImage1D");
329089fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      return;
32919540a1dbcf213645b23c6cd9284e92a1152cca05Brian Paul   }
32929540a1dbcf213645b23c6cd9284e92a1152cca05Brian Paul
32939540a1dbcf213645b23c6cd9284e92a1152cca05Brian Paul   texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
32949540a1dbcf213645b23c6cd9284e92a1152cca05Brian Paul   texObj = _mesa_select_tex_object(ctx, texUnit, target);
32958e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul   texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
32969540a1dbcf213645b23c6cd9284e92a1152cca05Brian Paul   assert(texImage);
32979540a1dbcf213645b23c6cd9284e92a1152cca05Brian Paul
3298a9fc8ba756dd25a07dc19058fe60f65bda82a055Brian Paul   if ((GLint) format != texImage->InternalFormat) {
329989fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      _mesa_error(ctx, GL_INVALID_OPERATION,
330089fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul                  "glCompressedTexSubImage1D(format)");
330189fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      return;
330289fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   }
330389fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul
3304798d83cf67098a7df293c711e0d2c8c296fc4c8fKarl Schultz   if ((width == 1 || width == 2) && (GLuint) width != texImage->Width) {
330589fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      _mesa_error(ctx, GL_INVALID_VALUE, "glCompressedTexSubImage1D(width)");
330689fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      return;
330789fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   }
330889fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul
3309529e0a98d477e33f41f680926f8b9c745aebbe90Brian Paul   if (width == 0)
33109540a1dbcf213645b23c6cd9284e92a1152cca05Brian Paul      return;  /* no-op, not an error */
33119540a1dbcf213645b23c6cd9284e92a1152cca05Brian Paul
33129540a1dbcf213645b23c6cd9284e92a1152cca05Brian Paul   if (ctx->Driver.CompressedTexSubImage1D) {
33138e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul      (*ctx->Driver.CompressedTexSubImage1D)(ctx, target, level,
33148e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul                                             xoffset, width,
33158e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul                                             format, imageSize, data,
33168e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul                                             texObj, texImage);
33179540a1dbcf213645b23c6cd9284e92a1152cca05Brian Paul   }
3318bf8b5f99a11ad43bb3404ed4ae04eceb3027bbabBrian Paul   ctx->NewState |= _NEW_TEXTURE;
33191207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul}
33201207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul
33211207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul
3322c40d1dd62dd9bcbb97128e37a75d991a8d3b2d8cKendall Bennettvoid GLAPIENTRY
33231207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul_mesa_CompressedTexSubImage2DARB(GLenum target, GLint level, GLint xoffset,
33241207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul                                 GLint yoffset, GLsizei width, GLsizei height,
33251207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul                                 GLenum format, GLsizei imageSize,
33261207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul                                 const GLvoid *data)
33271207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul{
33289540a1dbcf213645b23c6cd9284e92a1152cca05Brian Paul   struct gl_texture_unit *texUnit;
33299540a1dbcf213645b23c6cd9284e92a1152cca05Brian Paul   struct gl_texture_object *texObj;
33309540a1dbcf213645b23c6cd9284e92a1152cca05Brian Paul   struct gl_texture_image *texImage;
333189fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   GLenum error;
3332a1f1586bcb9fcd4a30930fd14b73323642ce85c5Brian Paul   GET_CURRENT_CONTEXT(ctx);
333375280a2c742ee241d392618a3b29548fce1520a9Brian Paul   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
333475280a2c742ee241d392618a3b29548fce1520a9Brian Paul
333589fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   error = compressed_subtexture_error_check(ctx, 2, target, level,
3336c3c19be8e0d0b13916cc128cf3c8e839935c912aBrian Paul                                             xoffset, yoffset, 0, /* pos */
3337c3c19be8e0d0b13916cc128cf3c8e839935c912aBrian Paul                                             width, height, 1,    /* size */
3338c3c19be8e0d0b13916cc128cf3c8e839935c912aBrian Paul                                             format, imageSize);
333989fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   if (error) {
3340b305028464f02947c0cce0476af0e35f4ed1fafaBrian Paul      /* XXX proxy target? */
334189fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      _mesa_error(ctx, error, "glCompressedTexSubImage2D");
334289fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      return;
33439540a1dbcf213645b23c6cd9284e92a1152cca05Brian Paul   }
33449540a1dbcf213645b23c6cd9284e92a1152cca05Brian Paul
33459540a1dbcf213645b23c6cd9284e92a1152cca05Brian Paul   texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
33469540a1dbcf213645b23c6cd9284e92a1152cca05Brian Paul   texObj = _mesa_select_tex_object(ctx, texUnit, target);
33478e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul   texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
33489540a1dbcf213645b23c6cd9284e92a1152cca05Brian Paul   assert(texImage);
33499540a1dbcf213645b23c6cd9284e92a1152cca05Brian Paul
3350a9fc8ba756dd25a07dc19058fe60f65bda82a055Brian Paul   if ((GLint) format != texImage->InternalFormat) {
335189fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      _mesa_error(ctx, GL_INVALID_OPERATION,
335289fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul                  "glCompressedTexSubImage2D(format)");
335389fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      return;
335489fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   }
335589fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul
3356798d83cf67098a7df293c711e0d2c8c296fc4c8fKarl Schultz   if (((width == 1 || width == 2) && (GLuint) width != texImage->Width) ||
3357798d83cf67098a7df293c711e0d2c8c296fc4c8fKarl Schultz       ((height == 1 || height == 2) && (GLuint) height != texImage->Height)) {
335889fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      _mesa_error(ctx, GL_INVALID_VALUE, "glCompressedTexSubImage2D(size)");
335989fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      return;
336089fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   }
336189fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul
3362529e0a98d477e33f41f680926f8b9c745aebbe90Brian Paul   if (width == 0 || height == 0)
33639540a1dbcf213645b23c6cd9284e92a1152cca05Brian Paul      return;  /* no-op, not an error */
33649540a1dbcf213645b23c6cd9284e92a1152cca05Brian Paul
33659540a1dbcf213645b23c6cd9284e92a1152cca05Brian Paul   if (ctx->Driver.CompressedTexSubImage2D) {
33668e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul      (*ctx->Driver.CompressedTexSubImage2D)(ctx, target, level,
33678e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul                                             xoffset, yoffset, width, height,
33688e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul                                             format, imageSize, data,
33698e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul                                             texObj, texImage);
33709540a1dbcf213645b23c6cd9284e92a1152cca05Brian Paul   }
3371bf8b5f99a11ad43bb3404ed4ae04eceb3027bbabBrian Paul   ctx->NewState |= _NEW_TEXTURE;
33721207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul}
33731207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul
33741207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul
3375c40d1dd62dd9bcbb97128e37a75d991a8d3b2d8cKendall Bennettvoid GLAPIENTRY
33761207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul_mesa_CompressedTexSubImage3DARB(GLenum target, GLint level, GLint xoffset,
33771207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul                                 GLint yoffset, GLint zoffset, GLsizei width,
33781207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul                                 GLsizei height, GLsizei depth, GLenum format,
33791207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul                                 GLsizei imageSize, const GLvoid *data)
33801207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul{
33819540a1dbcf213645b23c6cd9284e92a1152cca05Brian Paul   struct gl_texture_unit *texUnit;
33829540a1dbcf213645b23c6cd9284e92a1152cca05Brian Paul   struct gl_texture_object *texObj;
33839540a1dbcf213645b23c6cd9284e92a1152cca05Brian Paul   struct gl_texture_image *texImage;
338489fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   GLenum error;
3385a1f1586bcb9fcd4a30930fd14b73323642ce85c5Brian Paul   GET_CURRENT_CONTEXT(ctx);
338675280a2c742ee241d392618a3b29548fce1520a9Brian Paul   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
338775280a2c742ee241d392618a3b29548fce1520a9Brian Paul
338889fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   error = compressed_subtexture_error_check(ctx, 3, target, level,
3389c3c19be8e0d0b13916cc128cf3c8e839935c912aBrian Paul                                             xoffset, yoffset, zoffset,/*pos*/
3390c3c19be8e0d0b13916cc128cf3c8e839935c912aBrian Paul                                             width, height, depth, /*size*/
3391c3c19be8e0d0b13916cc128cf3c8e839935c912aBrian Paul                                             format, imageSize);
339289fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   if (error) {
339389fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      _mesa_error(ctx, error, "glCompressedTexSubImage2D");
339489fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      return;
33959540a1dbcf213645b23c6cd9284e92a1152cca05Brian Paul   }
33969540a1dbcf213645b23c6cd9284e92a1152cca05Brian Paul
33979540a1dbcf213645b23c6cd9284e92a1152cca05Brian Paul   texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
33989540a1dbcf213645b23c6cd9284e92a1152cca05Brian Paul   texObj = _mesa_select_tex_object(ctx, texUnit, target);
33998e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul   texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
34009540a1dbcf213645b23c6cd9284e92a1152cca05Brian Paul   assert(texImage);
34019540a1dbcf213645b23c6cd9284e92a1152cca05Brian Paul
3402a9fc8ba756dd25a07dc19058fe60f65bda82a055Brian Paul   if ((GLint) format != texImage->InternalFormat) {
340389fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      _mesa_error(ctx, GL_INVALID_OPERATION,
340489fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul                  "glCompressedTexSubImage3D(format)");
340589fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      return;
340689fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   }
340789fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul
3408798d83cf67098a7df293c711e0d2c8c296fc4c8fKarl Schultz   if (((width == 1 || width == 2) && (GLuint) width != texImage->Width) ||
3409798d83cf67098a7df293c711e0d2c8c296fc4c8fKarl Schultz       ((height == 1 || height == 2) && (GLuint) height != texImage->Height) ||
3410798d83cf67098a7df293c711e0d2c8c296fc4c8fKarl Schultz       ((depth == 1 || depth == 2) && (GLuint) depth != texImage->Depth)) {
341189fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      _mesa_error(ctx, GL_INVALID_VALUE, "glCompressedTexSubImage3D(size)");
341289fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      return;
341389fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   }
341489fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul
3415529e0a98d477e33f41f680926f8b9c745aebbe90Brian Paul   if (width == 0 || height == 0 || depth == 0)
34169540a1dbcf213645b23c6cd9284e92a1152cca05Brian Paul      return;  /* no-op, not an error */
34179540a1dbcf213645b23c6cd9284e92a1152cca05Brian Paul
34189540a1dbcf213645b23c6cd9284e92a1152cca05Brian Paul   if (ctx->Driver.CompressedTexSubImage3D) {
34198e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul      (*ctx->Driver.CompressedTexSubImage3D)(ctx, target, level,
34208e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul                                             xoffset, yoffset, zoffset,
34218e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul                                             width, height, depth,
34228e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul                                             format, imageSize, data,
34238e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul                                             texObj, texImage);
34249540a1dbcf213645b23c6cd9284e92a1152cca05Brian Paul   }
3425bf8b5f99a11ad43bb3404ed4ae04eceb3027bbabBrian Paul   ctx->NewState |= _NEW_TEXTURE;
34261207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul}
34271207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul
34281207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul
3429c40d1dd62dd9bcbb97128e37a75d991a8d3b2d8cKendall Bennettvoid GLAPIENTRY
34309540a1dbcf213645b23c6cd9284e92a1152cca05Brian Paul_mesa_GetCompressedTexImageARB(GLenum target, GLint level, GLvoid *img)
34311207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul{
34328e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul   const struct gl_texture_unit *texUnit;
34339540a1dbcf213645b23c6cd9284e92a1152cca05Brian Paul   const struct gl_texture_object *texObj;
34349540a1dbcf213645b23c6cd9284e92a1152cca05Brian Paul   struct gl_texture_image *texImage;
3435cd1cefae9146fc14b35ee93a04bdb1b1590fba7bBrian Paul   GLint maxLevels;
3436a1f1586bcb9fcd4a30930fd14b73323642ce85c5Brian Paul   GET_CURRENT_CONTEXT(ctx);
3437cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
34389540a1dbcf213645b23c6cd9284e92a1152cca05Brian Paul
3439cd1cefae9146fc14b35ee93a04bdb1b1590fba7bBrian Paul   texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
3440cd1cefae9146fc14b35ee93a04bdb1b1590fba7bBrian Paul   texObj = _mesa_select_tex_object(ctx, texUnit, target);
3441cd1cefae9146fc14b35ee93a04bdb1b1590fba7bBrian Paul   if (!texObj) {
3442cd1cefae9146fc14b35ee93a04bdb1b1590fba7bBrian Paul      _mesa_error(ctx, GL_INVALID_ENUM, "glGetCompressedTexImageARB");
3443cd1cefae9146fc14b35ee93a04bdb1b1590fba7bBrian Paul      return;
3444cd1cefae9146fc14b35ee93a04bdb1b1590fba7bBrian Paul   }
3445cd1cefae9146fc14b35ee93a04bdb1b1590fba7bBrian Paul
3446ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul   maxLevels = _mesa_max_texture_levels(ctx, target);
3447ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul   ASSERT(maxLevels > 0); /* 0 indicates bad target, caught above */
3448cd1cefae9146fc14b35ee93a04bdb1b1590fba7bBrian Paul
3449cd1cefae9146fc14b35ee93a04bdb1b1590fba7bBrian Paul   if (level < 0 || level >= maxLevels) {
3450cd1cefae9146fc14b35ee93a04bdb1b1590fba7bBrian Paul      _mesa_error(ctx, GL_INVALID_VALUE, "glGetCompressedTexImageARB(level)");
34519540a1dbcf213645b23c6cd9284e92a1152cca05Brian Paul      return;
34529540a1dbcf213645b23c6cd9284e92a1152cca05Brian Paul   }
34539540a1dbcf213645b23c6cd9284e92a1152cca05Brian Paul
345477ee31930a1b0cc7766939415f4f04ed6a1fa4acBrian Paul   if (_mesa_is_proxy_texture(target)) {
345508836341788a9f9d638d9dc8328510ccd18ddeb5Brian Paul      _mesa_error(ctx, GL_INVALID_ENUM, "glGetCompressedTexImageARB(target)");
34568e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul      return;
34579540a1dbcf213645b23c6cd9284e92a1152cca05Brian Paul   }
34589540a1dbcf213645b23c6cd9284e92a1152cca05Brian Paul
34598e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul   texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
34609540a1dbcf213645b23c6cd9284e92a1152cca05Brian Paul   if (!texImage) {
3461e4276667dafc8de0c6e64af8300fc7598437de6eBrian Paul      /* probably invalid mipmap level */
346208836341788a9f9d638d9dc8328510ccd18ddeb5Brian Paul      _mesa_error(ctx, GL_INVALID_VALUE, "glGetCompressedTexImageARB(level)");
34639540a1dbcf213645b23c6cd9284e92a1152cca05Brian Paul      return;
34649540a1dbcf213645b23c6cd9284e92a1152cca05Brian Paul   }
34659540a1dbcf213645b23c6cd9284e92a1152cca05Brian Paul
34669540a1dbcf213645b23c6cd9284e92a1152cca05Brian Paul   if (!texImage->IsCompressed) {
346708836341788a9f9d638d9dc8328510ccd18ddeb5Brian Paul      _mesa_error(ctx, GL_INVALID_OPERATION, "glGetCompressedTexImageARB");
34689540a1dbcf213645b23c6cd9284e92a1152cca05Brian Paul      return;
34699540a1dbcf213645b23c6cd9284e92a1152cca05Brian Paul   }
34709540a1dbcf213645b23c6cd9284e92a1152cca05Brian Paul
347168d293b03535ca50daf70650b32db780f1718a3bBrian Paul   /* this typically calls _mesa_get_compressed_teximage() */
347268d293b03535ca50daf70650b32db780f1718a3bBrian Paul   ctx->Driver.GetCompressedTexImage(ctx, target, level, img, texObj,texImage);
34731207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul}
3474