teximage.c revision 1207bf057aeea3376e5e0f03f789a5d67ed4d29d
1afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg/*
3afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg * Mesa 3-D graphics library
4fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul * Version:  3.3
5afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg *
6663049a719ed76aa0a6f24f1951a411d241bc8a4Brian Paul * Copyright (C) 1999-2000  Brian Paul   All Rights Reserved.
7afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg *
8afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg * Permission is hereby granted, free of charge, to any person obtaining a
9afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg * copy of this software and associated documentation files (the "Software"),
10afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg * to deal in the Software without restriction, including without limitation
11afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg * and/or sell copies of the Software, and to permit persons to whom the
13afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg * Software is furnished to do so, subject to the following conditions:
14afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg *
15afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg * The above copyright notice and this permission notice shall be included
16afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg * in all copies or substantial portions of the Software.
17afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg *
18afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
21afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg */
25afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
26afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
27afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg#ifdef PC_HEADER
28afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg#include "all.h"
29afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg#else
30fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul#include "glheader.h"
31afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg#include "context.h"
32afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg#include "image.h"
33fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul#include "mem.h"
34afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg#include "mmath.h"
35afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg#include "span.h"
36afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg#include "teximage.h"
37afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg#include "texstate.h"
38afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg#include "types.h"
39afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg#endif
40afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
41afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
42afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg/*
43afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg * NOTES:
44afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg *
45c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul * Mesa's native texture datatype is GLubyte.  Native formats are
46c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul * GL_ALPHA, GL_LUMINANCE, GL_LUMANCE_ALPHA, GL_INTENSITY, GL_RGB, GL_RGBA,
47c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul * and GL_COLOR_INDEX.
48c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul * Device drivers are free to implement any internal format they want.
49afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg */
50afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
51afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
524827179cc0d314f69671e97dcb016f5a3b90a213Brian Paul#ifdef DEBUG
53e5d68a2b7dba4d4402ee117dcaebde0b35c87956Brian Paulstatic void PrintTexture(const struct gl_texture_image *img)
544827179cc0d314f69671e97dcb016f5a3b90a213Brian Paul{
55e5d68a2b7dba4d4402ee117dcaebde0b35c87956Brian Paul  int i, j, c;
56e5d68a2b7dba4d4402ee117dcaebde0b35c87956Brian Paul  GLubyte *data = img->Data;
57e5d68a2b7dba4d4402ee117dcaebde0b35c87956Brian Paul
58e5d68a2b7dba4d4402ee117dcaebde0b35c87956Brian Paul  if (!data) {
59e5d68a2b7dba4d4402ee117dcaebde0b35c87956Brian Paul     printf("No texture data\n");
60e5d68a2b7dba4d4402ee117dcaebde0b35c87956Brian Paul     return;
61e5d68a2b7dba4d4402ee117dcaebde0b35c87956Brian Paul  }
62e5d68a2b7dba4d4402ee117dcaebde0b35c87956Brian Paul
63e5d68a2b7dba4d4402ee117dcaebde0b35c87956Brian Paul  switch (img->Format) {
64e5d68a2b7dba4d4402ee117dcaebde0b35c87956Brian Paul     case GL_ALPHA:
65e5d68a2b7dba4d4402ee117dcaebde0b35c87956Brian Paul     case GL_LUMINANCE:
66e5d68a2b7dba4d4402ee117dcaebde0b35c87956Brian Paul     case GL_INTENSITY:
67e5d68a2b7dba4d4402ee117dcaebde0b35c87956Brian Paul     case GL_COLOR_INDEX:
68e5d68a2b7dba4d4402ee117dcaebde0b35c87956Brian Paul        c = 1;
69e5d68a2b7dba4d4402ee117dcaebde0b35c87956Brian Paul        break;
70e5d68a2b7dba4d4402ee117dcaebde0b35c87956Brian Paul     case GL_LUMINANCE_ALPHA:
71e5d68a2b7dba4d4402ee117dcaebde0b35c87956Brian Paul        c = 2;
72e5d68a2b7dba4d4402ee117dcaebde0b35c87956Brian Paul        break;
73e5d68a2b7dba4d4402ee117dcaebde0b35c87956Brian Paul     case GL_RGB:
74e5d68a2b7dba4d4402ee117dcaebde0b35c87956Brian Paul        c = 3;
75e5d68a2b7dba4d4402ee117dcaebde0b35c87956Brian Paul        break;
76e5d68a2b7dba4d4402ee117dcaebde0b35c87956Brian Paul     case GL_RGBA:
77e5d68a2b7dba4d4402ee117dcaebde0b35c87956Brian Paul        c = 4;
78e5d68a2b7dba4d4402ee117dcaebde0b35c87956Brian Paul        break;
79e5d68a2b7dba4d4402ee117dcaebde0b35c87956Brian Paul     default:
80e5d68a2b7dba4d4402ee117dcaebde0b35c87956Brian Paul        gl_problem(NULL, "error in PrintTexture\n");
81e5d68a2b7dba4d4402ee117dcaebde0b35c87956Brian Paul        return;
82e5d68a2b7dba4d4402ee117dcaebde0b35c87956Brian Paul  }
83e5d68a2b7dba4d4402ee117dcaebde0b35c87956Brian Paul
84e5d68a2b7dba4d4402ee117dcaebde0b35c87956Brian Paul
85e5d68a2b7dba4d4402ee117dcaebde0b35c87956Brian Paul  for (i = 0; i < img->Height; i++) {
86e5d68a2b7dba4d4402ee117dcaebde0b35c87956Brian Paul    for (j = 0; j < img->Width; j++) {
874827179cc0d314f69671e97dcb016f5a3b90a213Brian Paul      if (c==1)
884827179cc0d314f69671e97dcb016f5a3b90a213Brian Paul        printf("%02x  ", data[0]);
894827179cc0d314f69671e97dcb016f5a3b90a213Brian Paul      else if (c==2)
90e5d68a2b7dba4d4402ee117dcaebde0b35c87956Brian Paul        printf("%02x%02x  ", data[0], data[1]);
914827179cc0d314f69671e97dcb016f5a3b90a213Brian Paul      else if (c==3)
92e5d68a2b7dba4d4402ee117dcaebde0b35c87956Brian Paul        printf("%02x%02x%02x  ", data[0], data[1], data[2]);
934827179cc0d314f69671e97dcb016f5a3b90a213Brian Paul      else if (c==4)
94e5d68a2b7dba4d4402ee117dcaebde0b35c87956Brian Paul        printf("%02x%02x%02x%02x  ", data[0], data[1], data[2], data[3]);
954827179cc0d314f69671e97dcb016f5a3b90a213Brian Paul      data += c;
964827179cc0d314f69671e97dcb016f5a3b90a213Brian Paul    }
974827179cc0d314f69671e97dcb016f5a3b90a213Brian Paul    printf("\n");
984827179cc0d314f69671e97dcb016f5a3b90a213Brian Paul  }
994827179cc0d314f69671e97dcb016f5a3b90a213Brian Paul}
1004827179cc0d314f69671e97dcb016f5a3b90a213Brian Paul#endif
1014827179cc0d314f69671e97dcb016f5a3b90a213Brian Paul
1024827179cc0d314f69671e97dcb016f5a3b90a213Brian Paul
1034827179cc0d314f69671e97dcb016f5a3b90a213Brian Paul
104f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul/*
105afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg * Compute log base 2 of n.
106afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg * If n isn't an exact power of two return -1.
107afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg * If n<0 return -1.
108afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg */
109fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paulstatic int
110fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paullogbase2( int n )
111afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg{
112afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   GLint i = 1;
113afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   GLint log2 = 0;
114afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
115afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   if (n<0) {
116afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      return -1;
117afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   }
118afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
119afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   while ( n > i ) {
120afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      i *= 2;
121afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      log2++;
122afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   }
123afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   if (i != n) {
124afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      return -1;
125afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   }
126afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   else {
127afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      return log2;
128afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   }
129afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg}
130afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
131afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
132afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
133afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg/*
134afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg * Given an internal texture format enum or 1, 2, 3, 4 return the
135afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg * corresponding _base_ internal format:  GL_ALPHA, GL_LUMINANCE,
136c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul * GL_LUMANCE_ALPHA, GL_INTENSITY, GL_RGB, or GL_RGBA.
137c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul * Return -1 if invalid enum.
138afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg */
139b132e8da5e5f2b7da1f2141e0322e66bb0608e02Brian PaulGLint
140b132e8da5e5f2b7da1f2141e0322e66bb0608e02Brian Paul_mesa_base_tex_format( GLint format )
141afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg{
142afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   switch (format) {
143afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_ALPHA:
144afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_ALPHA4:
145afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_ALPHA8:
146afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_ALPHA12:
147afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_ALPHA16:
148afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         return GL_ALPHA;
149afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case 1:
150afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_LUMINANCE:
151afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_LUMINANCE4:
152afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_LUMINANCE8:
153afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_LUMINANCE12:
154afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_LUMINANCE16:
155afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         return GL_LUMINANCE;
156afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case 2:
157afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_LUMINANCE_ALPHA:
158afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_LUMINANCE4_ALPHA4:
159afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_LUMINANCE6_ALPHA2:
160afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_LUMINANCE8_ALPHA8:
161afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_LUMINANCE12_ALPHA4:
162afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_LUMINANCE12_ALPHA12:
163afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_LUMINANCE16_ALPHA16:
164afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         return GL_LUMINANCE_ALPHA;
165afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_INTENSITY:
166afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_INTENSITY4:
167afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_INTENSITY8:
168afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_INTENSITY12:
169afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_INTENSITY16:
170afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         return GL_INTENSITY;
171afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case 3:
172afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_RGB:
173afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_R3_G3_B2:
174afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_RGB4:
175afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_RGB5:
176afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_RGB8:
177afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_RGB10:
178afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_RGB12:
179afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_RGB16:
180afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         return GL_RGB;
181afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case 4:
182afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_RGBA:
183afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_RGBA2:
184afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_RGBA4:
185afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_RGB5_A1:
186afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_RGBA8:
187afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_RGB10_A2:
188afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_RGBA12:
189afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_RGBA16:
190afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         return GL_RGBA;
191afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_COLOR_INDEX:
192afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_COLOR_INDEX1_EXT:
193afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_COLOR_INDEX2_EXT:
194afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_COLOR_INDEX4_EXT:
195afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_COLOR_INDEX8_EXT:
196afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_COLOR_INDEX12_EXT:
197afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_COLOR_INDEX16_EXT:
198afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         return GL_COLOR_INDEX;
199afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      default:
200afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         return -1;  /* error */
201afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   }
202afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg}
203afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
204afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
205afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
206afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg/*
207afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg * Given an internal texture format enum or 1, 2, 3, 4 return the
208afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg * corresponding _base_ internal format:  GL_ALPHA, GL_LUMINANCE,
209afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg * GL_LUMANCE_ALPHA, GL_INTENSITY, GL_RGB, or GL_RGBA.  Return the
210afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg * number of components for the format.  Return -1 if invalid enum.
211afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg */
212fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paulstatic GLint
213fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paulcomponents_in_intformat( GLint format )
214afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg{
215afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   switch (format) {
216afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_ALPHA:
217afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_ALPHA4:
218afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_ALPHA8:
219afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_ALPHA12:
220afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_ALPHA16:
221afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         return 1;
222afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case 1:
223afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_LUMINANCE:
224afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_LUMINANCE4:
225afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_LUMINANCE8:
226afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_LUMINANCE12:
227afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_LUMINANCE16:
228afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         return 1;
229afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case 2:
230afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_LUMINANCE_ALPHA:
231afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_LUMINANCE4_ALPHA4:
232afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_LUMINANCE6_ALPHA2:
233afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_LUMINANCE8_ALPHA8:
234afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_LUMINANCE12_ALPHA4:
235afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_LUMINANCE12_ALPHA12:
236afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_LUMINANCE16_ALPHA16:
237afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         return 2;
238afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_INTENSITY:
239afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_INTENSITY4:
240afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_INTENSITY8:
241afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_INTENSITY12:
242afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_INTENSITY16:
243afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         return 1;
244afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case 3:
245afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_RGB:
246afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_R3_G3_B2:
247afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_RGB4:
248afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_RGB5:
249afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_RGB8:
250afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_RGB10:
251afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_RGB12:
252afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_RGB16:
253afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         return 3;
254afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case 4:
255afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_RGBA:
256afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_RGBA2:
257afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_RGBA4:
258afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_RGB5_A1:
259afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_RGBA8:
260afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_RGB10_A2:
261afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_RGBA12:
262afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_RGBA16:
263afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         return 4;
264afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_COLOR_INDEX:
265afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_COLOR_INDEX1_EXT:
266afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_COLOR_INDEX2_EXT:
267afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_COLOR_INDEX4_EXT:
268afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_COLOR_INDEX8_EXT:
269afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_COLOR_INDEX12_EXT:
270afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_COLOR_INDEX16_EXT:
271afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         return 1;
272afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      default:
273afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         return -1;  /* error */
274afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   }
275afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg}
276afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
277afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
278afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
279afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg/*
280afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg * Examine the texImage->Format field and set the Red, Green, Blue, etc
281afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg * texel component sizes to default values.
282afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg * These fields are set only here by core Mesa but device drivers may
283afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg * overwritting these fields to indicate true texel resolution.
284afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg */
285fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paulstatic void
286fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paulset_teximage_component_sizes( struct gl_texture_image *texImage )
287afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg{
288afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   switch (texImage->Format) {
289afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_ALPHA:
290afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         texImage->RedBits = 0;
291afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         texImage->GreenBits = 0;
292afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         texImage->BlueBits = 0;
293afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         texImage->AlphaBits = 8;
294afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         texImage->IntensityBits = 0;
295afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         texImage->LuminanceBits = 0;
296afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         texImage->IndexBits = 0;
297afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         break;
298afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_LUMINANCE:
299afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         texImage->RedBits = 0;
300afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         texImage->GreenBits = 0;
301afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         texImage->BlueBits = 0;
302afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         texImage->AlphaBits = 0;
303afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         texImage->IntensityBits = 0;
304afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         texImage->LuminanceBits = 8;
305afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         texImage->IndexBits = 0;
306afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         break;
307afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_LUMINANCE_ALPHA:
308afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         texImage->RedBits = 0;
309afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         texImage->GreenBits = 0;
310afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         texImage->BlueBits = 0;
311afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         texImage->AlphaBits = 8;
312afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         texImage->IntensityBits = 0;
313afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         texImage->LuminanceBits = 8;
314afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         texImage->IndexBits = 0;
315afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         break;
316afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_INTENSITY:
317afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         texImage->RedBits = 0;
318afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         texImage->GreenBits = 0;
319afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         texImage->BlueBits = 0;
320afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         texImage->AlphaBits = 0;
321afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         texImage->IntensityBits = 8;
322afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         texImage->LuminanceBits = 0;
323afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         texImage->IndexBits = 0;
324afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         break;
32591baaa3d5a34a0a746dcb70967364a388d5ca438Brian Paul      case GL_RED:
32691baaa3d5a34a0a746dcb70967364a388d5ca438Brian Paul         texImage->RedBits = 8;
32791baaa3d5a34a0a746dcb70967364a388d5ca438Brian Paul         texImage->GreenBits = 0;
32891baaa3d5a34a0a746dcb70967364a388d5ca438Brian Paul         texImage->BlueBits = 0;
32991baaa3d5a34a0a746dcb70967364a388d5ca438Brian Paul         texImage->AlphaBits = 0;
33091baaa3d5a34a0a746dcb70967364a388d5ca438Brian Paul         texImage->IntensityBits = 0;
33191baaa3d5a34a0a746dcb70967364a388d5ca438Brian Paul         texImage->LuminanceBits = 0;
33291baaa3d5a34a0a746dcb70967364a388d5ca438Brian Paul         texImage->IndexBits = 0;
33391baaa3d5a34a0a746dcb70967364a388d5ca438Brian Paul         break;
33491baaa3d5a34a0a746dcb70967364a388d5ca438Brian Paul      case GL_GREEN:
33591baaa3d5a34a0a746dcb70967364a388d5ca438Brian Paul         texImage->RedBits = 0;
33691baaa3d5a34a0a746dcb70967364a388d5ca438Brian Paul         texImage->GreenBits = 8;
33791baaa3d5a34a0a746dcb70967364a388d5ca438Brian Paul         texImage->BlueBits = 0;
33891baaa3d5a34a0a746dcb70967364a388d5ca438Brian Paul         texImage->AlphaBits = 0;
33991baaa3d5a34a0a746dcb70967364a388d5ca438Brian Paul         texImage->IntensityBits = 0;
34091baaa3d5a34a0a746dcb70967364a388d5ca438Brian Paul         texImage->LuminanceBits = 0;
34191baaa3d5a34a0a746dcb70967364a388d5ca438Brian Paul         texImage->IndexBits = 0;
34291baaa3d5a34a0a746dcb70967364a388d5ca438Brian Paul         break;
34391baaa3d5a34a0a746dcb70967364a388d5ca438Brian Paul      case GL_BLUE:
34491baaa3d5a34a0a746dcb70967364a388d5ca438Brian Paul         texImage->RedBits = 0;
34591baaa3d5a34a0a746dcb70967364a388d5ca438Brian Paul         texImage->GreenBits = 0;
34691baaa3d5a34a0a746dcb70967364a388d5ca438Brian Paul         texImage->BlueBits = 8;
34791baaa3d5a34a0a746dcb70967364a388d5ca438Brian Paul         texImage->AlphaBits = 0;
34891baaa3d5a34a0a746dcb70967364a388d5ca438Brian Paul         texImage->IntensityBits = 0;
34991baaa3d5a34a0a746dcb70967364a388d5ca438Brian Paul         texImage->LuminanceBits = 0;
35091baaa3d5a34a0a746dcb70967364a388d5ca438Brian Paul         texImage->IndexBits = 0;
35191baaa3d5a34a0a746dcb70967364a388d5ca438Brian Paul         break;
352afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_RGB:
353d53573daf7bb53a41c9307339d70cfbba4f3b7a5Brian Paul      case GL_BGR:
354afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         texImage->RedBits = 8;
355afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         texImage->GreenBits = 8;
356afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         texImage->BlueBits = 8;
357afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         texImage->AlphaBits = 0;
358afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         texImage->IntensityBits = 0;
359afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         texImage->LuminanceBits = 0;
360afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         texImage->IndexBits = 0;
361afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         break;
362afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_RGBA:
363d53573daf7bb53a41c9307339d70cfbba4f3b7a5Brian Paul      case GL_BGRA:
364d53573daf7bb53a41c9307339d70cfbba4f3b7a5Brian Paul      case GL_ABGR_EXT:
365afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         texImage->RedBits = 8;
366afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         texImage->GreenBits = 8;
367afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         texImage->BlueBits = 8;
368afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         texImage->AlphaBits = 8;
369afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         texImage->IntensityBits = 0;
370afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         texImage->LuminanceBits = 0;
371afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         texImage->IndexBits = 0;
372afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         break;
373afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_COLOR_INDEX:
374afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         texImage->RedBits = 0;
375afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         texImage->GreenBits = 0;
376afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         texImage->BlueBits = 0;
377afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         texImage->AlphaBits = 0;
378afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         texImage->IntensityBits = 0;
379afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         texImage->LuminanceBits = 0;
380afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         texImage->IndexBits = 8;
381afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         break;
382afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      default:
383afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         gl_problem(NULL, "unexpected format in set_teximage_component_sizes");
384afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   }
385afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg}
386afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
387afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
388fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paulstatic void
389fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paulset_tex_image(struct gl_texture_object *tObj,
390fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul              GLenum target, GLint level,
391fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul              struct gl_texture_image *texImage)
392fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul{
393fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul   ASSERT(tObj);
394fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul   ASSERT(texImage);
395fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul   switch (target) {
396fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul      case GL_TEXTURE_2D:
397fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul         tObj->Image[level] = texImage;
398fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul         return;
399fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul      case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
400fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul         tObj->PosX[level] = texImage;
401fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul         return;
402fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul      case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
403fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul         tObj->NegX[level] = texImage;
404fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul         return;
405fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul      case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
406fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul         tObj->PosY[level] = texImage;
407fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul         return;
408fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul      case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
409fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul         tObj->NegY[level] = texImage;
410fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul         return;
411fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul      case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
412fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul         tObj->PosZ[level] = texImage;
413fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul         return;
414fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul      case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
415fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul         tObj->NegZ[level] = texImage;
416fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul         return;
417fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul      default:
418fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul         gl_problem(NULL, "bad target in set_tex_image()");
419fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul         return;
420fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul   }
421fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul}
422fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul
423fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul
42477ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paul/*
42577ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paul * Return new gl_texture_image struct with all fields initialized to zero.
42677ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paul */
42777ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paulstruct gl_texture_image *
428021a525616bef2bafc8f60edc193b975ed2b0efaBrian Paul_mesa_alloc_texture_image( void )
42977ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paul{
43077ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paul   return CALLOC_STRUCT(gl_texture_image);
43177ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paul}
43277ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paul
43377ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paul
43477ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paul
43577ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paul/*
4360293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul * Initialize most fields of a gl_texture_image struct.
43777ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paul */
4380293878846551bfe349a4aa029a0489f9a73f1ffBrian Paulstatic void
4390293878846551bfe349a4aa029a0489f9a73f1ffBrian Paulinit_texture_image( struct gl_texture_image *img,
4400293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul                    GLsizei width, GLsizei height, GLsizei depth,
4410293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul                    GLint border, GLenum internalFormat )
44277ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paul{
4430293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul   ASSERT(img);
4440293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul   ASSERT(!img->Data);
445b132e8da5e5f2b7da1f2141e0322e66bb0608e02Brian Paul   img->Format = (GLenum) _mesa_base_tex_format(internalFormat);
44677ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paul   set_teximage_component_sizes( img );
44777ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paul   img->IntFormat = (GLenum) internalFormat;
44877ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paul   img->Border = border;
44977ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paul   img->Width = width;
45077ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paul   img->Height = height;
45177ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paul   img->Depth = depth;
45277ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paul   img->WidthLog2 = logbase2(width - 2 * border);
45377ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paul   if (height == 1)  /* 1-D texture */
45477ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paul      img->HeightLog2 = 0;
45577ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paul   else
45677ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paul      img->HeightLog2 = logbase2(height - 2 * border);
45777ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paul   if (depth == 1)   /* 2-D texture */
45877ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paul      img->DepthLog2 = 0;
45977ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paul   else
46077ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paul      img->DepthLog2 = logbase2(depth - 2 * border);
46177ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paul   img->Width2 = 1 << img->WidthLog2;
46277ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paul   img->Height2 = 1 << img->HeightLog2;
46377ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paul   img->Depth2 = 1 << img->DepthLog2;
46477ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paul   img->MaxLog2 = MAX2(img->WidthLog2, img->HeightLog2);
46577ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paul}
46677ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paul
46777ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paul
46877ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paul
46977ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paulvoid
470021a525616bef2bafc8f60edc193b975ed2b0efaBrian Paul_mesa_free_texture_image( struct gl_texture_image *teximage )
47177ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paul{
47277ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paul   if (teximage->Data) {
47377ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paul      FREE( teximage->Data );
47477ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paul      teximage->Data = NULL;
47577ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paul   }
47677ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paul   FREE( teximage );
47777ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paul}
47877ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paul
47977ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paul
48077ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paul
481fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul/*
48235d5301a54153930ee6fd60dff1010ce9f901397Brian Paul * Given a texture unit and a texture target, return the corresponding
48335d5301a54153930ee6fd60dff1010ce9f901397Brian Paul * texture object.
48435d5301a54153930ee6fd60dff1010ce9f901397Brian Paul */
48535d5301a54153930ee6fd60dff1010ce9f901397Brian Paulstruct gl_texture_object *
48635d5301a54153930ee6fd60dff1010ce9f901397Brian Paul_mesa_select_tex_object(GLcontext *ctx, struct gl_texture_unit *texUnit,
48735d5301a54153930ee6fd60dff1010ce9f901397Brian Paul                        GLenum target)
48835d5301a54153930ee6fd60dff1010ce9f901397Brian Paul{
48935d5301a54153930ee6fd60dff1010ce9f901397Brian Paul   switch (target) {
49035d5301a54153930ee6fd60dff1010ce9f901397Brian Paul      case GL_TEXTURE_1D:
49135d5301a54153930ee6fd60dff1010ce9f901397Brian Paul         return texUnit->CurrentD[1];
49235d5301a54153930ee6fd60dff1010ce9f901397Brian Paul      case GL_PROXY_TEXTURE_1D:
49335d5301a54153930ee6fd60dff1010ce9f901397Brian Paul         return ctx->Texture.Proxy1D;
49435d5301a54153930ee6fd60dff1010ce9f901397Brian Paul      case GL_TEXTURE_2D:
49535d5301a54153930ee6fd60dff1010ce9f901397Brian Paul         return texUnit->CurrentD[2];
49635d5301a54153930ee6fd60dff1010ce9f901397Brian Paul      case GL_PROXY_TEXTURE_2D:
49735d5301a54153930ee6fd60dff1010ce9f901397Brian Paul         return ctx->Texture.Proxy2D;
49835d5301a54153930ee6fd60dff1010ce9f901397Brian Paul      case GL_TEXTURE_3D:
49935d5301a54153930ee6fd60dff1010ce9f901397Brian Paul         return texUnit->CurrentD[3];
50035d5301a54153930ee6fd60dff1010ce9f901397Brian Paul      case GL_PROXY_TEXTURE_3D:
50135d5301a54153930ee6fd60dff1010ce9f901397Brian Paul         return ctx->Texture.Proxy3D;
50235d5301a54153930ee6fd60dff1010ce9f901397Brian Paul      case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
50335d5301a54153930ee6fd60dff1010ce9f901397Brian Paul      case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
50435d5301a54153930ee6fd60dff1010ce9f901397Brian Paul      case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
50535d5301a54153930ee6fd60dff1010ce9f901397Brian Paul      case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
50635d5301a54153930ee6fd60dff1010ce9f901397Brian Paul      case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
50735d5301a54153930ee6fd60dff1010ce9f901397Brian Paul      case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
50835d5301a54153930ee6fd60dff1010ce9f901397Brian Paul         return ctx->Extensions.HaveTextureCubeMap
50935d5301a54153930ee6fd60dff1010ce9f901397Brian Paul                ? texUnit->CurrentCubeMap : NULL;
51035d5301a54153930ee6fd60dff1010ce9f901397Brian Paul      case GL_PROXY_TEXTURE_CUBE_MAP_ARB:
51135d5301a54153930ee6fd60dff1010ce9f901397Brian Paul         return ctx->Extensions.HaveTextureCubeMap
51235d5301a54153930ee6fd60dff1010ce9f901397Brian Paul                ? ctx->Texture.ProxyCubeMap : NULL;
51335d5301a54153930ee6fd60dff1010ce9f901397Brian Paul      default:
51435d5301a54153930ee6fd60dff1010ce9f901397Brian Paul         gl_problem(NULL, "bad target in _mesa_select_tex_object()");
51535d5301a54153930ee6fd60dff1010ce9f901397Brian Paul         return NULL;
51635d5301a54153930ee6fd60dff1010ce9f901397Brian Paul   }
51735d5301a54153930ee6fd60dff1010ce9f901397Brian Paul}
51835d5301a54153930ee6fd60dff1010ce9f901397Brian Paul
51935d5301a54153930ee6fd60dff1010ce9f901397Brian Paul
52035d5301a54153930ee6fd60dff1010ce9f901397Brian Paul/*
521fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul * Return the texture image struct which corresponds to target and level
522fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul * for the given texture unit.
523fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul */
524fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paulstruct gl_texture_image *
525fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul_mesa_select_tex_image(GLcontext *ctx, const struct gl_texture_unit *texUnit,
526fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul                       GLenum target, GLint level)
527fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul{
528fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul   ASSERT(texUnit);
529fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul   switch (target) {
530fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul      case GL_TEXTURE_1D:
531fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul         return texUnit->CurrentD[1]->Image[level];
532fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul      case GL_PROXY_TEXTURE_1D:
533fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul         return ctx->Texture.Proxy1D->Image[level];
534fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul      case GL_TEXTURE_2D:
535fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul         return texUnit->CurrentD[2]->Image[level];
536fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul      case GL_PROXY_TEXTURE_2D:
537fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul         return ctx->Texture.Proxy2D->Image[level];
538fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul      case GL_TEXTURE_3D:
539fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul         return texUnit->CurrentD[3]->Image[level];
540fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul      case GL_PROXY_TEXTURE_3D:
541fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul         return ctx->Texture.Proxy3D->Image[level];
542fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul      case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
543fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul         if (ctx->Extensions.HaveTextureCubeMap)
544fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul            return texUnit->CurrentCubeMap->PosX[level];
545fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul         else
546fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul            return NULL;
547fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul      case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
548fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul         if (ctx->Extensions.HaveTextureCubeMap)
549fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul            return texUnit->CurrentCubeMap->NegX[level];
550fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul         else
551fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul            return NULL;
552fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul      case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
553fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul         if (ctx->Extensions.HaveTextureCubeMap)
554fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul            return texUnit->CurrentCubeMap->PosY[level];
555fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul         else
556fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul            return NULL;
557fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul      case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
558fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul         if (ctx->Extensions.HaveTextureCubeMap)
559fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul            return texUnit->CurrentCubeMap->NegY[level];
560fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul         else
561fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul            return NULL;
562fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul      case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
563fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul         if (ctx->Extensions.HaveTextureCubeMap)
564fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul            return texUnit->CurrentCubeMap->PosZ[level];
565fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul         else
566fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul            return NULL;
567fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul      case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
568fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul         if (ctx->Extensions.HaveTextureCubeMap)
569fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul            return texUnit->CurrentCubeMap->NegZ[level];
570fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul         else
571fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul            return NULL;
572fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul      case GL_PROXY_TEXTURE_CUBE_MAP_ARB:
573fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul         if (ctx->Extensions.HaveTextureCubeMap)
574fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul            return ctx->Texture.ProxyCubeMap->PosX[level];
575fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul         else
576fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul            return NULL;
577fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul      default:
578fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul         gl_problem(ctx, "bad target in _mesa_select_tex_image()");
579fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul         return NULL;
580fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul   }
581fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul}
582fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul
583fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul
584fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul
585afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg/* Need this to prevent an out-of-bounds memory access when using
586afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg * X86 optimized code.
587afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg */
588afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg#ifdef USE_X86_ASM
589afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg#  define EXTRA_BYTE 1
590afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg#else
591afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg#  define EXTRA_BYTE 0
592afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg#endif
593afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
594afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
595c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul
596afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg/*
59743911c8ef06c44d486a17cf9a8fa8ea263b08adeBrian Paul * Called by glTexImage[123]D.  Fill in a texture image with data given
59843911c8ef06c44d486a17cf9a8fa8ea263b08adeBrian Paul * by the client.  All pixel transfer and unpack modes are handled here.
59943911c8ef06c44d486a17cf9a8fa8ea263b08adeBrian Paul * NOTE: All texture image parameters should have already been error checked.
600afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg */
60143911c8ef06c44d486a17cf9a8fa8ea263b08adeBrian Paulstatic void
60243911c8ef06c44d486a17cf9a8fa8ea263b08adeBrian Paulmake_texture_image( GLcontext *ctx,
60343911c8ef06c44d486a17cf9a8fa8ea263b08adeBrian Paul                    struct gl_texture_image *texImage,
604c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul                    GLenum srcFormat, GLenum srcType, const GLvoid *pixels,
605c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul                    const struct gl_pixelstore_attrib *unpacking)
606afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg{
607c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   GLint components, numPixels;
60843911c8ef06c44d486a17cf9a8fa8ea263b08adeBrian Paul   GLint internalFormat, width, height, depth, border;
609afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
61043911c8ef06c44d486a17cf9a8fa8ea263b08adeBrian Paul   ASSERT(ctx);
61143911c8ef06c44d486a17cf9a8fa8ea263b08adeBrian Paul   ASSERT(texImage);
61243911c8ef06c44d486a17cf9a8fa8ea263b08adeBrian Paul   ASSERT(!texImage->Data);
61343911c8ef06c44d486a17cf9a8fa8ea263b08adeBrian Paul   ASSERT(pixels);
61443911c8ef06c44d486a17cf9a8fa8ea263b08adeBrian Paul   ASSERT(unpacking);
61543911c8ef06c44d486a17cf9a8fa8ea263b08adeBrian Paul
61643911c8ef06c44d486a17cf9a8fa8ea263b08adeBrian Paul   internalFormat = texImage->IntFormat;
61743911c8ef06c44d486a17cf9a8fa8ea263b08adeBrian Paul   width = texImage->Width;
61843911c8ef06c44d486a17cf9a8fa8ea263b08adeBrian Paul   height = texImage->Height;
61943911c8ef06c44d486a17cf9a8fa8ea263b08adeBrian Paul   depth = texImage->Depth;
62043911c8ef06c44d486a17cf9a8fa8ea263b08adeBrian Paul   border = texImage->Border;
621c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   components = components_in_intformat(internalFormat);
622c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul
62343911c8ef06c44d486a17cf9a8fa8ea263b08adeBrian Paul   ASSERT(width > 0);
62443911c8ef06c44d486a17cf9a8fa8ea263b08adeBrian Paul   ASSERT(height > 0);
62543911c8ef06c44d486a17cf9a8fa8ea263b08adeBrian Paul   ASSERT(depth > 0);
62643911c8ef06c44d486a17cf9a8fa8ea263b08adeBrian Paul   ASSERT(border == 0 || border == 1);
62743911c8ef06c44d486a17cf9a8fa8ea263b08adeBrian Paul   ASSERT(pixels);
62843911c8ef06c44d486a17cf9a8fa8ea263b08adeBrian Paul   ASSERT(unpacking);
62943911c8ef06c44d486a17cf9a8fa8ea263b08adeBrian Paul   ASSERT(components);
630afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
63143911c8ef06c44d486a17cf9a8fa8ea263b08adeBrian Paul   numPixels = width * height * depth;
632afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
63343911c8ef06c44d486a17cf9a8fa8ea263b08adeBrian Paul   texImage->Data = (GLubyte *) MALLOC(numPixels * components + EXTRA_BYTE);
63443911c8ef06c44d486a17cf9a8fa8ea263b08adeBrian Paul   if (!texImage->Data)
63543911c8ef06c44d486a17cf9a8fa8ea263b08adeBrian Paul      return;      /* out of memory */
636afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
637c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   /*
638c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul    * OK, the texture image struct has been initialized and the texture
639c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul    * image memory has been allocated.
640c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul    * Now fill in the texture image from the source data.
641c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul    * This includes applying the pixel transfer operations.
642c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul    */
643c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul
644c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   /* try common 2D texture cases first */
645c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   if (!ctx->Pixel.ScaleOrBiasRGBA && !ctx->Pixel.MapColorFlag
646c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul       && !ctx->Pixel.IndexOffset && !ctx->Pixel.IndexShift
647c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul       && srcType == GL_UNSIGNED_BYTE && depth == 1) {
648c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul
64943911c8ef06c44d486a17cf9a8fa8ea263b08adeBrian Paul      if (srcFormat == internalFormat ||
65043911c8ef06c44d486a17cf9a8fa8ea263b08adeBrian Paul          (srcFormat == GL_LUMINANCE && internalFormat == 1) ||
65143911c8ef06c44d486a17cf9a8fa8ea263b08adeBrian Paul          (srcFormat == GL_LUMINANCE_ALPHA && internalFormat == 2) ||
65243911c8ef06c44d486a17cf9a8fa8ea263b08adeBrian Paul          (srcFormat == GL_RGB && internalFormat == 3) ||
65343911c8ef06c44d486a17cf9a8fa8ea263b08adeBrian Paul          (srcFormat == GL_RGBA && internalFormat == 4)) {
654c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         /* This will cover the common GL_RGB, GL_RGBA, GL_ALPHA,
655c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul          * GL_LUMINANCE_ALPHA, etc. texture formats.
656c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul          */
657b7d076fc96ac27117421653a043d00a95f789d24Brian Paul         const GLubyte *src = (const GLubyte *) _mesa_image_address(
65843911c8ef06c44d486a17cf9a8fa8ea263b08adeBrian Paul            unpacking, pixels, width, height, srcFormat, srcType, 0, 0, 0);
65943911c8ef06c44d486a17cf9a8fa8ea263b08adeBrian Paul         const GLint srcStride = _mesa_image_row_stride(unpacking, width,
66043911c8ef06c44d486a17cf9a8fa8ea263b08adeBrian Paul                                                        srcFormat, srcType);
661c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         GLubyte *dst = texImage->Data;
662c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         GLint dstBytesPerRow = width * components * sizeof(GLubyte);
663c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         if (srcStride == dstBytesPerRow) {
664c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul            MEMCPY(dst, src, height * dstBytesPerRow);
665afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         }
666c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         else {
667c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul            GLint i;
668c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul            for (i = 0; i < height; i++) {
669c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul               MEMCPY(dst, src, dstBytesPerRow);
670c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul               src += srcStride;
671c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul               dst += dstBytesPerRow;
672afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg            }
673c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         }
67443911c8ef06c44d486a17cf9a8fa8ea263b08adeBrian Paul         return;  /* all done */
675c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      }
676c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      else if (srcFormat == GL_RGBA && internalFormat == GL_RGB) {
677c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         /* commonly used by Quake */
678b7d076fc96ac27117421653a043d00a95f789d24Brian Paul         const GLubyte *src = (const GLubyte *) _mesa_image_address(
67943911c8ef06c44d486a17cf9a8fa8ea263b08adeBrian Paul            unpacking, pixels, width, height, srcFormat, srcType, 0, 0, 0);
68043911c8ef06c44d486a17cf9a8fa8ea263b08adeBrian Paul         const GLint srcStride = _mesa_image_row_stride(unpacking, width,
68143911c8ef06c44d486a17cf9a8fa8ea263b08adeBrian Paul                                                        srcFormat, srcType);
682c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         GLubyte *dst = texImage->Data;
683c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         GLint i, j;
684c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         for (i = 0; i < height; i++) {
685c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul            const GLubyte *s = src;
686c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul            for (j = 0; j < width; j++) {
687c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul               *dst++ = *s++;  /*red*/
688c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul               *dst++ = *s++;  /*green*/
689c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul               *dst++ = *s++;  /*blue*/
690c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul               s++;            /*alpha*/
691afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg            }
692c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul            src += srcStride;
693afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         }
69443911c8ef06c44d486a17cf9a8fa8ea263b08adeBrian Paul         return;  /* all done */
695c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      }
696c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   }
697afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
698afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
699c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   /*
700c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul    * General case solutions
701c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul    */
702c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   if (texImage->Format == GL_COLOR_INDEX) {
703c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      /* color index texture */
704c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      const GLint destBytesPerRow = width * components * sizeof(GLubyte);
705c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      const GLenum dstType = GL_UNSIGNED_BYTE;
706c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      GLubyte *dest = texImage->Data;
707c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      GLint img, row;
708c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      for (img = 0; img < depth; img++) {
709c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         for (row = 0; row < height; row++) {
710b7d076fc96ac27117421653a043d00a95f789d24Brian Paul            const GLvoid *source = _mesa_image_address(unpacking,
711c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul                pixels, width, height, srcFormat, srcType, img, row, 0);
712c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul            _mesa_unpack_index_span(ctx, width, dstType, dest,
713c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul                                    srcType, source, unpacking, GL_TRUE);
714c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul            dest += destBytesPerRow;
715afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         }
716c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      }
717c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   }
718c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   else {
719c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      /* regular, color texture */
720c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      const GLint destBytesPerRow = width * components * sizeof(GLubyte);
721c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      const GLenum dstFormat = texImage->Format;
722c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      GLubyte *dest = texImage->Data;
723c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      GLint img, row;
724c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      for (img = 0; img < depth; img++) {
725c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         for (row = 0; row < height; row++) {
726b7d076fc96ac27117421653a043d00a95f789d24Brian Paul            const GLvoid *source = _mesa_image_address(unpacking,
727c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul                   pixels, width, height, srcFormat, srcType, img, row, 0);
728c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul            _mesa_unpack_ubyte_color_span(ctx, width, dstFormat, dest,
729c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul                   srcFormat, srcType, source, unpacking, GL_TRUE);
730c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul            dest += destBytesPerRow;
731c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         }
732c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      }
733afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   }
734afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg}
735afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
736afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
737afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
738afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg/*
739afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg * glTexImage[123]D can accept a NULL image pointer.  In this case we
740afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg * create a texture image with unspecified image contents per the OpenGL
74143911c8ef06c44d486a17cf9a8fa8ea263b08adeBrian Paul * spec.  This function creates an empty image for the given texture image.
742afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg */
74343911c8ef06c44d486a17cf9a8fa8ea263b08adeBrian Paulstatic void
74443911c8ef06c44d486a17cf9a8fa8ea263b08adeBrian Paulmake_null_texture( struct gl_texture_image *texImage )
745afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg{
746afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   GLint components;
747afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   GLint numPixels;
748afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
74943911c8ef06c44d486a17cf9a8fa8ea263b08adeBrian Paul   ASSERT(texImage);
75043911c8ef06c44d486a17cf9a8fa8ea263b08adeBrian Paul   ASSERT(!texImage->Data);
751afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
75243911c8ef06c44d486a17cf9a8fa8ea263b08adeBrian Paul   components = components_in_intformat(texImage->IntFormat);
75343911c8ef06c44d486a17cf9a8fa8ea263b08adeBrian Paul   numPixels = texImage->Width * texImage->Height * texImage->Depth;
754afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
755bd5cdaf4442872d3cd2ff94eeafadd481d27fcfbBrian Paul   texImage->Data = (GLubyte *) MALLOC( numPixels * components + EXTRA_BYTE );
756afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
757afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   /*
758afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg    * Let's see if anyone finds this.  If glTexImage2D() is called with
759afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg    * a NULL image pointer then load the texture image with something
760afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg    * interesting instead of leaving it indeterminate.
761afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg    */
762afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   if (texImage->Data) {
76365d54604c387dca986c876e811362d8e8517dcacBrian Paul      static const char message[8][32] = {
764afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         "   X   X  XXXXX   XXX     X    ",
765afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         "   XX XX  X      X   X   X X   ",
766afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         "   X X X  X      X      X   X  ",
767afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         "   X   X  XXXX    XXX   XXXXX  ",
768afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         "   X   X  X          X  X   X  ",
769afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         "   X   X  X      X   X  X   X  ",
770afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         "   X   X  XXXXX   XXX   X   X  ",
771afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         "                               "
772afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      };
773afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
774afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      GLubyte *imgPtr = texImage->Data;
775afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      GLint i, j, k;
77643911c8ef06c44d486a17cf9a8fa8ea263b08adeBrian Paul      for (i = 0; i < texImage->Height; i++) {
777afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         GLint srcRow = 7 - i % 8;
77843911c8ef06c44d486a17cf9a8fa8ea263b08adeBrian Paul         for (j = 0; j < texImage->Width; j++) {
779afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg            GLint srcCol = j % 32;
7805b37c322741f019118a618bc6220f37adba4fbcdBrian Paul            GLint texel = (message[srcRow][srcCol]=='X') ? 255 : 70;
781afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg            for (k=0;k<components;k++) {
7825b37c322741f019118a618bc6220f37adba4fbcdBrian Paul               *imgPtr++ = (GLubyte) texel;
783afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg            }
784afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         }
785afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      }
786afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   }
787afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg}
788afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
789afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
790afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
791afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg/*
792c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul * Test glTexImage[123]D() parameters for errors.
793afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg * Input:
794afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg *         dimensions - must be 1 or 2 or 3
795afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg * Return:  GL_TRUE = an error was detected, GL_FALSE = no errors
796afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg */
797c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paulstatic GLboolean
798c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paultexture_error_check( GLcontext *ctx, GLenum target,
799c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul                     GLint level, GLint internalFormat,
800c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul                     GLenum format, GLenum type,
8015b37c322741f019118a618bc6220f37adba4fbcdBrian Paul                     GLuint dimensions,
802c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul                     GLint width, GLint height,
803c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul                     GLint depth, GLint border )
804afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg{
805afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   GLboolean isProxy;
806afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   GLint iformat;
807afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
808afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   if (dimensions == 1) {
8095b37c322741f019118a618bc6220f37adba4fbcdBrian Paul      isProxy = (GLboolean) (target == GL_PROXY_TEXTURE_1D);
810afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      if (target != GL_TEXTURE_1D && !isProxy) {
811afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         gl_error( ctx, GL_INVALID_ENUM, "glTexImage1D(target)" );
812afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         return GL_TRUE;
813afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      }
814afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   }
815afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   else if (dimensions == 2) {
8165b37c322741f019118a618bc6220f37adba4fbcdBrian Paul      isProxy = (GLboolean) (target == GL_PROXY_TEXTURE_2D);
817afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      if (target != GL_TEXTURE_2D && !isProxy) {
818afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg          gl_error( ctx, GL_INVALID_ENUM, "glTexImage2D(target)" );
819afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg          return GL_TRUE;
820afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      }
821afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   }
822afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   else if (dimensions == 3) {
8235b37c322741f019118a618bc6220f37adba4fbcdBrian Paul      isProxy = (GLboolean) (target == GL_PROXY_TEXTURE_3D);
824afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      if (target != GL_TEXTURE_3D && !isProxy) {
825afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         gl_error( ctx, GL_INVALID_ENUM, "glTexImage3D(target)" );
826afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         return GL_TRUE;
827afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      }
828afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   }
829afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   else {
830afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      gl_problem( ctx, "bad dims in texture_error_check" );
831afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      return GL_TRUE;
832afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   }
833afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
834afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   /* Border */
8359fd2b0a698163b397b0a17493e52b27273e51b63Brian Paul   if (border != 0 && border != 1) {
836afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      if (!isProxy) {
837c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         char message[100];
838c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         sprintf(message, "glTexImage%dD(border)", dimensions);
839c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         gl_error(ctx, GL_INVALID_VALUE, message);
840afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      }
841afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      return GL_TRUE;
842afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   }
843afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
844afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   /* Width */
845afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   if (width < 2 * border || width > 2 + ctx->Const.MaxTextureSize
846afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg       || logbase2( width - 2 * border ) < 0) {
847afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      if (!isProxy) {
848c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         char message[100];
849c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         sprintf(message, "glTexImage%dD(width)", dimensions);
850c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         gl_error(ctx, GL_INVALID_VALUE, message);
851afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      }
852afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      return GL_TRUE;
853afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   }
854afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
855afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   /* Height */
856afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   if (dimensions >= 2) {
857afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      if (height < 2 * border || height > 2 + ctx->Const.MaxTextureSize
858afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg          || logbase2( height - 2 * border ) < 0) {
859afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         if (!isProxy) {
860c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul            char message[100];
861c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul            sprintf(message, "glTexImage%dD(height)", dimensions);
862c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul            gl_error(ctx, GL_INVALID_VALUE, message);
863afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         }
864c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         return GL_TRUE;
865afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      }
866afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   }
867afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
868afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   /* Depth */
869afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   if (dimensions >= 3) {
870afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      if (depth < 2 * border || depth > 2 + ctx->Const.MaxTextureSize
871afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg          || logbase2( depth - 2 * border ) < 0) {
872afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         if (!isProxy) {
873afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg            gl_error( ctx, GL_INVALID_VALUE, "glTexImage3D(depth)" );
874afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         }
875afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         return GL_TRUE;
876afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      }
877afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   }
878afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
879afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   /* Level */
8809fd2b0a698163b397b0a17493e52b27273e51b63Brian Paul   if (level < 0 || level >= ctx->Const.MaxTextureLevels) {
881c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      if (!isProxy) {
882c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         char message[100];
883c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         sprintf(message, "glTexImage%dD(level)", dimensions);
884c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         gl_error(ctx, GL_INVALID_VALUE, message);
885c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      }
886afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      return GL_TRUE;
887afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   }
888afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
889b132e8da5e5f2b7da1f2141e0322e66bb0608e02Brian Paul   iformat = _mesa_base_tex_format( internalFormat );
890afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   if (iformat < 0) {
891c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      if (!isProxy) {
892c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         char message[100];
893c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         sprintf(message, "glTexImage%dD(internalFormat)", dimensions);
894c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         gl_error(ctx, GL_INVALID_VALUE, message);
895c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      }
896afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      return GL_TRUE;
897afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   }
898afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
899b7d076fc96ac27117421653a043d00a95f789d24Brian Paul   if (!_mesa_is_legal_format_and_type( format, type )) {
900d53573daf7bb53a41c9307339d70cfbba4f3b7a5Brian Paul      /* Yes, generate GL_INVALID_OPERATION, not GL_INVALID_ENUM, if there
901d53573daf7bb53a41c9307339d70cfbba4f3b7a5Brian Paul       * is a type/format mismatch.  See 1.2 spec page 94, sec 3.6.4.
902d53573daf7bb53a41c9307339d70cfbba4f3b7a5Brian Paul       */
903c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      if (!isProxy) {
904c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         char message[100];
905c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         sprintf(message, "glTexImage%dD(format or type)", dimensions);
906c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         gl_error(ctx, GL_INVALID_OPERATION, message);
907c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      }
908afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      return GL_TRUE;
909afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   }
910afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
911afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   /* if we get here, the parameters are OK */
912afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   return GL_FALSE;
913afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg}
914afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
915afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
916afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
917afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg/*
918c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul * Test glTexSubImage[123]D() parameters for errors.
919c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul * Input:
920c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul *         dimensions - must be 1 or 2 or 3
921c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul * Return:  GL_TRUE = an error was detected, GL_FALSE = no errors
922afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg */
923c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paulstatic GLboolean
924fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paulsubtexture_error_check( GLcontext *ctx, GLuint dimensions,
925c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul                        GLenum target, GLint level,
926c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul                        GLint xoffset, GLint yoffset, GLint zoffset,
927c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul                        GLint width, GLint height, GLint depth,
928c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul                        GLenum format, GLenum type )
929afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg{
930afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   struct gl_texture_unit *texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
931c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   struct gl_texture_image *destTex;
932afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
933c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   if (dimensions == 1) {
934c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      if (target != GL_TEXTURE_1D) {
935c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         gl_error( ctx, GL_INVALID_ENUM, "glTexSubImage1D(target)" );
936c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         return GL_TRUE;
937afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      }
938c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   }
939c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   else if (dimensions == 2) {
940fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul      if (ctx->Extensions.HaveTextureCubeMap) {
941fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul         if ((target < GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB ||
942fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul              target > GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB) &&
943fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul             target != GL_TEXTURE_2D) {
944fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul            gl_error( ctx, GL_INVALID_ENUM, "glTexSubImage2D(target)" );
945fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul            return GL_TRUE;
946fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul         }
947fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul      }
948fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul      else if (target != GL_TEXTURE_2D) {
949c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         gl_error( ctx, GL_INVALID_ENUM, "glTexSubImage2D(target)" );
950c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         return GL_TRUE;
951afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      }
952c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   }
953c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   else if (dimensions == 3) {
954c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      if (target != GL_TEXTURE_3D) {
955c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         gl_error( ctx, GL_INVALID_ENUM, "glTexSubImage3D(target)" );
956c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         return GL_TRUE;
957afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      }
958c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   }
959c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   else {
960c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      gl_problem( ctx, "bad dims in texture_error_check" );
961c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      return GL_TRUE;
962c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   }
963afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
964c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   if (level < 0 || level >= ctx->Const.MaxTextureLevels) {
965c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      gl_error(ctx, GL_INVALID_ENUM, "glTexSubImage2D(level)");
966c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      return GL_TRUE;
967c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   }
968afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
969c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   if (width < 0) {
970c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      char message[100];
971c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      sprintf(message, "glTexSubImage%dD(width)", dimensions);
972c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      gl_error(ctx, GL_INVALID_VALUE, message);
973c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      return GL_TRUE;
974c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   }
975c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   if (height < 0 && dimensions > 1) {
976c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      char message[100];
977c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      sprintf(message, "glTexSubImage%dD(height)", dimensions);
978c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      gl_error(ctx, GL_INVALID_VALUE, message);
979c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      return GL_TRUE;
980c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   }
981c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   if (depth < 0 && dimensions > 2) {
982c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      char message[100];
983c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      sprintf(message, "glTexSubImage%dD(depth)", dimensions);
984c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      gl_error(ctx, GL_INVALID_VALUE, message);
985c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      return GL_TRUE;
986c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   }
987afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
988c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   destTex = texUnit->CurrentD[2]->Image[level];
989c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   if (!destTex) {
990c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      gl_error(ctx, GL_INVALID_OPERATION, "glTexSubImage2D");
991c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      return GL_TRUE;
992c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   }
993afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
994c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   if (xoffset < -((GLint)destTex->Border)) {
995c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      gl_error(ctx, GL_INVALID_VALUE, "glTexSubImage1/2/3D(xoffset)");
996c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      return GL_TRUE;
997afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   }
998c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   if (xoffset + width > (GLint) (destTex->Width + destTex->Border)) {
999c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      gl_error(ctx, GL_INVALID_VALUE, "glTexSubImage1/2/3D(xoffset+width)");
1000c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      return GL_TRUE;
1001c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   }
1002c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   if (dimensions > 1) {
1003c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      if (yoffset < -((GLint)destTex->Border)) {
1004c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         gl_error(ctx, GL_INVALID_VALUE, "glTexSubImage2/3D(yoffset)");
1005c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         return GL_TRUE;
1006afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      }
1007c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      if (yoffset + height > (GLint) (destTex->Height + destTex->Border)) {
1008c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         gl_error(ctx, GL_INVALID_VALUE, "glTexSubImage2/3D(yoffset+height)");
1009c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         return GL_TRUE;
1010afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      }
1011afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   }
1012c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   if (dimensions > 2) {
1013c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      if (zoffset < -((GLint)destTex->Border)) {
1014c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         gl_error(ctx, GL_INVALID_VALUE, "glTexSubImage3D(zoffset)");
1015c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         return GL_TRUE;
1016c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      }
1017c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      if (zoffset + depth  > (GLint) (destTex->Depth+destTex->Border)) {
1018c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         gl_error(ctx, GL_INVALID_VALUE, "glTexSubImage3D(zoffset+depth)");
1019c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         return GL_TRUE;
1020c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      }
1021afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   }
1022afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
1023b7d076fc96ac27117421653a043d00a95f789d24Brian Paul   if (!_mesa_is_legal_format_and_type(format, type)) {
1024c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      char message[100];
1025c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      sprintf(message, "glTexSubImage%dD(format or type)", dimensions);
1026c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      gl_error(ctx, GL_INVALID_ENUM, message);
1027c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      return GL_TRUE;
1028c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   }
1029afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
1030c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   return GL_FALSE;
1031c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul}
1032afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
1033afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
1034afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg/*
1035c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul * Test glCopyTexImage[12]D() parameters for errors.
1036c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul * Input:  dimensions - must be 1 or 2 or 3
1037c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul * Return:  GL_TRUE = an error was detected, GL_FALSE = no errors
1038afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg */
1039c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paulstatic GLboolean
1040fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paulcopytexture_error_check( GLcontext *ctx, GLuint dimensions,
1041c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul                         GLenum target, GLint level, GLint internalFormat,
1042c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul                         GLint width, GLint height, GLint border )
1043afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg{
1044c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   GLint iformat;
1045afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
1046fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul   if (dimensions == 1) {
1047fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul      if (target != GL_TEXTURE_1D) {
1048fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul         gl_error( ctx, GL_INVALID_ENUM, "glCopyTexImage1D(target)" );
1049fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul         return GL_TRUE;
1050fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul      }
1051c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   }
1052fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul   else if (dimensions == 2) {
1053fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul      if (ctx->Extensions.HaveTextureCubeMap) {
1054fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul         if ((target < GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB ||
1055fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul              target > GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB) &&
1056fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul             target != GL_TEXTURE_2D) {
1057fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul            gl_error( ctx, GL_INVALID_ENUM, "glCopyTexImage2D(target)" );
1058fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul            return GL_TRUE;
1059fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul         }
1060fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul      }
1061fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul      else if (target != GL_TEXTURE_2D) {
1062fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul         gl_error( ctx, GL_INVALID_ENUM, "glCopyTexImage2D(target)" );
1063fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul         return GL_TRUE;
1064fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul      }
1065c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   }
1066c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul
1067c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   /* Border */
1068c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   if (border!=0 && border!=1) {
1069c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      char message[100];
1070c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      sprintf(message, "glCopyTexImage%dD(border)", dimensions);
1071c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      gl_error(ctx, GL_INVALID_VALUE, message);
1072c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      return GL_TRUE;
1073c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   }
1074c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul
1075c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   /* Width */
1076c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   if (width < 2 * border || width > 2 + ctx->Const.MaxTextureSize
1077c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul       || logbase2( width - 2 * border ) < 0) {
1078c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      char message[100];
1079c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      sprintf(message, "glCopyTexImage%dD(width)", dimensions);
1080c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      gl_error(ctx, GL_INVALID_VALUE, message);
1081c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      return GL_TRUE;
1082c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   }
1083c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul
1084c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   /* Height */
1085c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   if (dimensions >= 2) {
1086c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      if (height < 2 * border || height > 2 + ctx->Const.MaxTextureSize
1087c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul          || logbase2( height - 2 * border ) < 0) {
1088c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         char message[100];
1089c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         sprintf(message, "glCopyTexImage%dD(height)", dimensions);
1090c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         gl_error(ctx, GL_INVALID_VALUE, message);
1091c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         return GL_TRUE;
1092c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      }
1093c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   }
1094c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul
1095c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   /* Level */
1096c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   if (level<0 || level>=ctx->Const.MaxTextureLevels) {
1097c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      char message[100];
1098c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      sprintf(message, "glCopyTexImage%dD(level)", dimensions);
1099c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      gl_error(ctx, GL_INVALID_VALUE, message);
1100c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      return GL_TRUE;
1101c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   }
1102c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul
1103b132e8da5e5f2b7da1f2141e0322e66bb0608e02Brian Paul   iformat = _mesa_base_tex_format( internalFormat );
1104c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   if (iformat < 0) {
1105c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      char message[100];
1106c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      sprintf(message, "glCopyTexImage%dD(internalFormat)", dimensions);
1107c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      gl_error(ctx, GL_INVALID_VALUE, message);
1108c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      return GL_TRUE;
1109c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   }
1110c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul
1111c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   /* if we get here, the parameters are OK */
1112c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   return GL_FALSE;
1113c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul}
1114c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul
1115c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul
1116c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paulstatic GLboolean
1117fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paulcopytexsubimage_error_check( GLcontext *ctx, GLuint dimensions,
1118c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul                             GLenum target, GLint level,
1119c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul                             GLint xoffset, GLint yoffset, GLint zoffset,
11205b37c322741f019118a618bc6220f37adba4fbcdBrian Paul                             GLsizei width, GLsizei height )
1121c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul{
1122c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   struct gl_texture_unit *texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1123c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   struct gl_texture_image *teximage;
1124c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul
1125fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul   if (dimensions == 1) {
1126fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul      if (target != GL_TEXTURE_1D) {
1127fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul         gl_error( ctx, GL_INVALID_ENUM, "glCopyTexSubImage1D(target)" );
1128fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul         return GL_TRUE;
1129fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul      }
1130c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   }
1131fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul   else if (dimensions == 2) {
1132fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul      if (ctx->Extensions.HaveTextureCubeMap) {
1133fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul         if ((target < GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB ||
1134fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul              target > GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB) &&
1135fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul             target != GL_TEXTURE_2D) {
1136fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul            gl_error( ctx, GL_INVALID_ENUM, "glCopyTexSubImage2D(target)" );
1137fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul            return GL_TRUE;
1138fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul         }
1139fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul      }
1140fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul      else if (target != GL_TEXTURE_2D) {
1141fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul         gl_error( ctx, GL_INVALID_ENUM, "glCopyTexSubImage2D(target)" );
1142fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul         return GL_TRUE;
1143fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul      }
1144c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   }
1145fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul   else if (dimensions == 3) {
1146fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul      if (target != GL_TEXTURE_3D) {
1147fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul         gl_error( ctx, GL_INVALID_ENUM, "glCopyTexSubImage3D(target)" );
1148fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul         return GL_TRUE;
1149fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul      }
1150c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   }
1151c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul
1152c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   if (level < 0 || level >= ctx->Const.MaxTextureLevels) {
1153c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      char message[100];
1154c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      sprintf(message, "glCopyTexSubImage%dD(level)", dimensions);
1155c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      gl_error(ctx, GL_INVALID_VALUE, message);
1156c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      return GL_TRUE;
1157c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   }
1158c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul
1159c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   if (width < 0) {
1160c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      char message[100];
1161c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      sprintf(message, "glCopyTexSubImage%dD(width)", dimensions );
1162c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      gl_error(ctx, GL_INVALID_VALUE, message);
1163c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      return GL_TRUE;
1164c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   }
1165c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   if (dimensions > 1 && height < 0) {
1166c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      char message[100];
1167c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      sprintf(message, "glCopyTexSubImage%dD(height)", dimensions );
1168c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      gl_error(ctx, GL_INVALID_VALUE, message);
1169c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      return GL_TRUE;
1170c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   }
1171c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul
1172df6a28d105a31cfdc3c7d52574ef81f9d31bd3bdBrian Paul   teximage = texUnit->CurrentD[dimensions]->Image[level];
1173c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   if (!teximage) {
1174c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      char message[100];
1175c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      sprintf(message, "glCopyTexSubImage%dD(undefined texture)", dimensions);
1176c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      gl_error(ctx, GL_INVALID_OPERATION, message);
1177c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      return GL_TRUE;
1178c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   }
1179c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul
1180c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   if (xoffset < -((GLint)teximage->Border)) {
1181c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      char message[100];
1182c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      sprintf(message, "glCopyTexSubImage%dD(xoffset)", dimensions);
1183c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      gl_error(ctx, GL_INVALID_VALUE, message);
1184c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      return GL_TRUE;
1185c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   }
1186c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   if (xoffset+width > (GLint) (teximage->Width+teximage->Border)) {
1187c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      char message[100];
1188c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      sprintf(message, "glCopyTexSubImage%dD(xoffset+width)", dimensions);
1189c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      gl_error(ctx, GL_INVALID_VALUE, message);
1190c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      return GL_TRUE;
1191c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   }
1192c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   if (dimensions > 1) {
1193c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      if (yoffset < -((GLint)teximage->Border)) {
1194c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         char message[100];
1195c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         sprintf(message, "glCopyTexSubImage%dD(yoffset)", dimensions);
1196c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         gl_error(ctx, GL_INVALID_VALUE, message);
1197c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         return GL_TRUE;
1198c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      }
1199c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      /* NOTE: we're adding the border here, not subtracting! */
1200c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      if (yoffset+height > (GLint) (teximage->Height+teximage->Border)) {
1201c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         char message[100];
1202c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         sprintf(message, "glCopyTexSubImage%dD(yoffset+height)", dimensions);
1203c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         gl_error(ctx, GL_INVALID_VALUE, message);
1204c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         return GL_TRUE;
1205c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      }
1206c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   }
1207c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul
1208c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   if (dimensions > 2) {
1209c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      if (zoffset < -((GLint)teximage->Border)) {
1210c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         char message[100];
1211c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         sprintf(message, "glCopyTexSubImage%dD(zoffset)", dimensions);
1212c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         gl_error(ctx, GL_INVALID_VALUE, message);
1213c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         return GL_TRUE;
1214c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      }
1215c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      if (zoffset > (GLint) (teximage->Depth+teximage->Border)) {
1216c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         char message[100];
1217c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         sprintf(message, "glCopyTexSubImage%dD(zoffset+depth)", dimensions);
1218c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         gl_error(ctx, GL_INVALID_VALUE, message);
1219c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         return GL_TRUE;
1220c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      }
1221c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   }
1222c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul
1223c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   /* if we get here, the parameters are OK */
1224c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   return GL_FALSE;
1225c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul}
1226c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul
1227c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul
1228c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul
1229c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul
1230c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul/*
1231c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul * Called from the API.  Note that width includes the border.
1232c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul */
1233fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paulvoid
123443911c8ef06c44d486a17cf9a8fa8ea263b08adeBrian Paul_mesa_TexImage1D( GLenum target, GLint level, GLint internalFormat,
1235fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul                  GLsizei width, GLint border, GLenum format,
1236fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul                  GLenum type, const GLvoid *pixels )
1237c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul{
1238fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul   GET_CURRENT_CONTEXT(ctx);
1239c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glTexImage1D");
1240c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul
1241c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   if (target==GL_TEXTURE_1D) {
1242f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul      struct gl_texture_unit *texUnit;
12430293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      struct gl_texture_object *texObj;
12440293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      struct gl_texture_image *texImage;
1245f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul
124643911c8ef06c44d486a17cf9a8fa8ea263b08adeBrian Paul      if (texture_error_check( ctx, target, level, internalFormat,
1247c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul                               format, type, 1, width, 1, 1, border )) {
1248f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul         return;   /* error in texture image was detected */
1249c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      }
1250c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul
1251f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul      texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
12520293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      texObj = texUnit->CurrentD[1];
12530293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      texImage = texObj->Image[level];
12540293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul
12550293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      if (!texImage) {
1256021a525616bef2bafc8f60edc193b975ed2b0efaBrian Paul         texImage = _mesa_alloc_texture_image();
12570293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul         texObj->Image[level] = texImage;
12580293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul         if (!texImage) {
12590293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul            gl_error(ctx, GL_OUT_OF_MEMORY, "glTexImage1D");
12600293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul            return;
12610293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul         }
12620293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      }
12630293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      else if (texImage->Data) {
12640293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul         FREE(texImage->Data);
12650293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul         texImage->Data = NULL;
1266c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      }
1267c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul
12680293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      /* setup the teximage struct's fields */
12690293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      init_texture_image(texImage, width, 1, 1, border, internalFormat);
127043911c8ef06c44d486a17cf9a8fa8ea263b08adeBrian Paul
12710293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      /* process the texture image */
1272c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      if (pixels) {
12730293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul         GLboolean retain = GL_TRUE;
12740293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul         GLboolean success = GL_FALSE;
12750293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul         if (!ctx->Pixel.MapColorFlag && !ctx->Pixel.ScaleOrBiasRGBA
12760293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul             && ctx->Driver.TexImage1D) {
12770293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul            /* let device driver try to use raw image */
12780293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul            success = (*ctx->Driver.TexImage1D)( ctx, target, level, format,
12790293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul                                                 type, pixels, &ctx->Unpack,
12800293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul                                                 texObj, texImage, &retain);
12810293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul         }
12820293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul         if (retain || !success) {
12830293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul            /* make internal copy of the texture image */
12840293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul            make_texture_image(ctx, texImage, format, type,
12850293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul                               pixels, &ctx->Unpack);
12860293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul            if (!success && ctx->Driver.TexImage1D) {
12870293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul               /* let device driver try to use unpacked image */
12880293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul               (*ctx->Driver.TexImage1D)( ctx, target, level, texImage->Format,
12890293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul                                          GL_UNSIGNED_BYTE, texImage->Data,
12900293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul                                          &_mesa_native_packing,
12910293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul                                          texObj, texImage, &retain);
12920293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul            }
12930293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul         }
12940293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul         if (!retain && texImage->Data) {
12950293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul            FREE(texImage->Data);
12960293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul            texImage->Data = NULL;
12970293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul         }
1298c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      }
1299c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      else {
13000293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul         make_null_texture(texImage);
13010293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul         if (ctx->Driver.TexImage1D) {
13020293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul            GLboolean retain;
13030293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul            (*ctx->Driver.TexImage1D)( ctx, target, level, texImage->Format,
13040293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul                                       GL_UNSIGNED_BYTE, texImage->Data,
13050293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul                                       &_mesa_native_packing,
13060293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul                                       texObj, texImage, &retain);
13070293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul         }
1308c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      }
1309c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul
13100293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      /* state update */
13110293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      gl_put_texobj_on_dirty_list( ctx, texObj );
1312c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      ctx->NewState |= NEW_TEXTURING;
1313c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   }
1314c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   else if (target==GL_PROXY_TEXTURE_1D) {
1315c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      /* Proxy texture: check for errors and update proxy state */
131643911c8ef06c44d486a17cf9a8fa8ea263b08adeBrian Paul      if (texture_error_check( ctx, target, level, internalFormat,
1317c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul                               format, type, 1, width, 1, 1, border )) {
1318c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         if (level>=0 && level<ctx->Const.MaxTextureLevels) {
1319c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul            MEMSET( ctx->Texture.Proxy1D->Image[level], 0,
1320c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul                    sizeof(struct gl_texture_image) );
1321c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         }
1322c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      }
1323c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      else {
1324c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         ctx->Texture.Proxy1D->Image[level]->Format = (GLenum) format;
1325c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         set_teximage_component_sizes( ctx->Texture.Proxy1D->Image[level] );
132643911c8ef06c44d486a17cf9a8fa8ea263b08adeBrian Paul         ctx->Texture.Proxy1D->Image[level]->IntFormat = (GLenum) internalFormat;
1327c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         ctx->Texture.Proxy1D->Image[level]->Border = border;
1328c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         ctx->Texture.Proxy1D->Image[level]->Width = width;
1329c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         ctx->Texture.Proxy1D->Image[level]->Height = 1;
1330c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         ctx->Texture.Proxy1D->Image[level]->Depth = 1;
1331c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      }
1332c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   }
1333c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   else {
1334c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      gl_error( ctx, GL_INVALID_ENUM, "glTexImage1D(target)" );
1335c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      return;
1336c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   }
1337c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul}
1338c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul
1339c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul
1340fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paulvoid
134143911c8ef06c44d486a17cf9a8fa8ea263b08adeBrian Paul_mesa_TexImage2D( GLenum target, GLint level, GLint internalFormat,
1342fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul                  GLsizei width, GLsizei height, GLint border,
1343fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul                  GLenum format, GLenum type,
1344fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul                  const GLvoid *pixels )
1345c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul{
1346fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul   GET_CURRENT_CONTEXT(ctx);
1347c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glTexImage2D");
1348c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul
1349fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul   if (target==GL_TEXTURE_2D ||
1350fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul       (ctx->Extensions.HaveTextureCubeMap &&
1351fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul        target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
1352fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul        target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB)) {
1353f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul      struct gl_texture_unit *texUnit;
13540293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      struct gl_texture_object *texObj;
13550293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      struct gl_texture_image *texImage;
1356f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul
135743911c8ef06c44d486a17cf9a8fa8ea263b08adeBrian Paul      if (texture_error_check( ctx, target, level, internalFormat,
1358c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul                               format, type, 2, width, height, 1, border )) {
1359f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul         return;   /* error in texture image was detected */
1360c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      }
1361c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul
1362f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul      texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
136335d5301a54153930ee6fd60dff1010ce9f901397Brian Paul      texObj = _mesa_select_tex_object(ctx, texUnit, target);
1364fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul      texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
13650293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul
13660293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      if (!texImage) {
1367021a525616bef2bafc8f60edc193b975ed2b0efaBrian Paul         texImage = _mesa_alloc_texture_image();
1368fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul         set_tex_image(texObj, target, level, texImage);
1369fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul         /*texObj->Image[level] = texImage;*/
13700293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul         if (!texImage) {
13710293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul            gl_error(ctx, GL_OUT_OF_MEMORY, "glTexImage2D");
13720293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul            return;
13730293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul         }
13740293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      }
13750293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      else if (texImage->Data) {
13760293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul         FREE(texImage->Data);
13770293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul         texImage->Data = NULL;
1378c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      }
1379c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul
13800293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      /* setup the teximage struct's fields */
13810293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      init_texture_image(texImage, width, height, 1, border, internalFormat);
138243911c8ef06c44d486a17cf9a8fa8ea263b08adeBrian Paul
13830293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      /* process the texture image */
1384c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      if (pixels) {
13850293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul         GLboolean retain = GL_TRUE;
13860293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul         GLboolean success = GL_FALSE;
13870293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul         if (!ctx->Pixel.MapColorFlag && !ctx->Pixel.ScaleOrBiasRGBA
13880293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul             && ctx->Driver.TexImage2D) {
13890293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul            /* let device driver try to use raw image */
13900293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul            success = (*ctx->Driver.TexImage2D)( ctx, target, level, format,
13910293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul                                                 type, pixels, &ctx->Unpack,
13920293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul                                                 texObj, texImage, &retain);
13930293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul         }
13940293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul         if (retain || !success) {
13950293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul            /* make internal copy of the texture image */
13960293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul            make_texture_image(ctx, texImage, format, type,
13970293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul                               pixels, &ctx->Unpack);
13980293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul            if (!success && ctx->Driver.TexImage2D) {
13990293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul               /* let device driver try to use unpacked image */
14000293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul               (*ctx->Driver.TexImage2D)( ctx, target, level, texImage->Format,
14010293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul                                          GL_UNSIGNED_BYTE, texImage->Data,
14020293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul                                          &_mesa_native_packing,
14030293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul                                          texObj, texImage, &retain);
14040293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul            }
14050293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul         }
14060293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul         if (!retain && texImage->Data) {
14070293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul            FREE(texImage->Data);
14080293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul            texImage->Data = NULL;
14090293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul         }
1410c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      }
1411c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      else {
14120293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul         make_null_texture(texImage);
14130293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul         if (ctx->Driver.TexImage2D) {
14140293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul            GLboolean retain;
14150293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul            (*ctx->Driver.TexImage2D)( ctx, target, level, texImage->Format,
14160293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul                                       GL_UNSIGNED_BYTE, texImage->Data,
14170293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul                                       &_mesa_native_packing,
14180293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul                                       texObj, texImage, &retain);
14190293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul         }
1420afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      }
1421afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
14220293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul#define OLD_DD_TEXTURE
14230293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul#ifdef OLD_DD_TEXTURE
14240293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      /* XXX this will be removed in the future */
1425afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      if (ctx->Driver.TexImage) {
14260293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul         (*ctx->Driver.TexImage)( ctx, target, texObj, level, internalFormat,
14270293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul                                  texImage );
1428afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      }
14290293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul#endif
14300293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul
14310293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      /* state update */
14320293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      gl_put_texobj_on_dirty_list( ctx, texObj );
14330293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      ctx->NewState |= NEW_TEXTURING;
1434afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   }
1435afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   else if (target==GL_PROXY_TEXTURE_2D) {
1436afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      /* Proxy texture: check for errors and update proxy state */
143743911c8ef06c44d486a17cf9a8fa8ea263b08adeBrian Paul      if (texture_error_check( ctx, target, level, internalFormat,
1438afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg                               format, type, 2, width, height, 1, border )) {
1439afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         if (level>=0 && level<ctx->Const.MaxTextureLevels) {
1440afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg            MEMSET( ctx->Texture.Proxy2D->Image[level], 0,
1441afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg                    sizeof(struct gl_texture_image) );
1442afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         }
1443afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      }
1444afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      else {
1445afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         ctx->Texture.Proxy2D->Image[level]->Format = (GLenum) format;
1446afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         set_teximage_component_sizes( ctx->Texture.Proxy2D->Image[level] );
144743911c8ef06c44d486a17cf9a8fa8ea263b08adeBrian Paul         ctx->Texture.Proxy2D->Image[level]->IntFormat = (GLenum) internalFormat;
1448afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         ctx->Texture.Proxy2D->Image[level]->Border = border;
1449afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         ctx->Texture.Proxy2D->Image[level]->Width = width;
1450afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         ctx->Texture.Proxy2D->Image[level]->Height = height;
1451afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         ctx->Texture.Proxy2D->Image[level]->Depth = 1;
1452afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      }
1453afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   }
1454afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   else {
1455afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      gl_error( ctx, GL_INVALID_ENUM, "glTexImage2D(target)" );
1456afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      return;
1457afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   }
1458afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg}
1459afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
1460afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
1461afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
1462afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg/*
1463afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg * Called by the API or display list executor.
1464afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg * Note that width and height include the border.
1465afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg */
1466fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paulvoid
146743911c8ef06c44d486a17cf9a8fa8ea263b08adeBrian Paul_mesa_TexImage3D( GLenum target, GLint level, GLint internalFormat,
1468fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul                  GLsizei width, GLsizei height, GLsizei depth,
1469fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul                  GLint border, GLenum format, GLenum type,
1470fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul                  const GLvoid *pixels )
1471afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg{
1472fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul   GET_CURRENT_CONTEXT(ctx);
1473f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glTexImage3D");
1474afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
1475fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul   if (target==GL_TEXTURE_3D_EXT) {
1476f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul      struct gl_texture_unit *texUnit;
14770293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      struct gl_texture_object *texObj;
14780293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      struct gl_texture_image *texImage;
147943911c8ef06c44d486a17cf9a8fa8ea263b08adeBrian Paul      if (texture_error_check( ctx, target, level, internalFormat,
1480afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg                               format, type, 3, width, height, depth,
1481afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg                               border )) {
1482f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul         return;   /* error in texture image was detected */
1483afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      }
1484afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
1485f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul      texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
14860293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      texObj = texUnit->CurrentD[3];
14870293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      texImage = texObj->Image[level];
14880293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul
14890293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      if (!texImage) {
1490021a525616bef2bafc8f60edc193b975ed2b0efaBrian Paul         texImage = _mesa_alloc_texture_image();
14910293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul         texObj->Image[level] = texImage;
14920293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul         if (!texImage) {
14930293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul            gl_error(ctx, GL_OUT_OF_MEMORY, "glTexImage3D");
14940293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul            return;
14950293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul         }
14960293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      }
14970293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      else if (texImage->Data) {
14980293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul         FREE(texImage->Data);
14990293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul         texImage->Data = NULL;
1500afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      }
1501afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
15020293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      /* setup the teximage struct's fields */
15030293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      init_texture_image(texImage, width, height, depth,
15040293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul                         border, internalFormat);
150543911c8ef06c44d486a17cf9a8fa8ea263b08adeBrian Paul
15060293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      /* process the texture image */
1507c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      if (pixels) {
15080293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul         GLboolean retain = GL_TRUE;
15090293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul         GLboolean success = GL_FALSE;
15100293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul         if (!ctx->Pixel.MapColorFlag && !ctx->Pixel.ScaleOrBiasRGBA
15110293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul             && ctx->Driver.TexImage3D) {
15120293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul            /* let device driver try to use raw image */
15130293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul            success = (*ctx->Driver.TexImage3D)( ctx, target, level, format,
15140293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul                                                 type, pixels, &ctx->Unpack,
15150293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul                                                 texObj, texImage, &retain);
15160293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul         }
15170293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul         if (retain || !success) {
15180293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul            /* make internal copy of the texture image */
15190293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul            make_texture_image(ctx, texImage, format, type,
15200293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul                               pixels, &ctx->Unpack);
15210293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul            if (!success && ctx->Driver.TexImage3D) {
15220293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul               /* let device driver try to use unpacked image */
15230293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul               (*ctx->Driver.TexImage3D)( ctx, target, level, texImage->Format,
15240293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul                                          GL_UNSIGNED_BYTE, texImage->Data,
15250293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul                                          &_mesa_native_packing,
15260293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul                                          texObj, texImage, &retain);
15270293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul            }
15280293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul         }
15290293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul         if (!retain && texImage->Data) {
15300293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul            FREE(texImage->Data);
15310293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul            texImage->Data = NULL;
15320293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul         }
1533afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      }
1534afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      else {
15350293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul         make_null_texture(texImage);
15360293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul         if (ctx->Driver.TexImage3D) {
15370293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul            GLboolean retain;
15380293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul            (*ctx->Driver.TexImage3D)( ctx, target, level, texImage->Format,
15390293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul                                       GL_UNSIGNED_BYTE, texImage->Data,
15400293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul                                       &_mesa_native_packing,
15410293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul                                       texObj, texImage, &retain);
15420293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul         }
1543afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      }
1544afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
15450293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      /* state update */
15460293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      gl_put_texobj_on_dirty_list( ctx, texObj );
1547afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      ctx->NewState |= NEW_TEXTURING;
1548afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   }
1549afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   else if (target==GL_PROXY_TEXTURE_3D_EXT) {
1550afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      /* Proxy texture: check for errors and update proxy state */
155143911c8ef06c44d486a17cf9a8fa8ea263b08adeBrian Paul      if (texture_error_check( ctx, target, level, internalFormat,
1552afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg                               format, type, 3, width, height, depth,
1553afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg                               border )) {
1554afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         if (level>=0 && level<ctx->Const.MaxTextureLevels) {
1555afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg            MEMSET( ctx->Texture.Proxy3D->Image[level], 0,
1556afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg                    sizeof(struct gl_texture_image) );
1557afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         }
1558afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      }
1559afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      else {
1560afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         ctx->Texture.Proxy3D->Image[level]->Format = (GLenum) format;
1561afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         set_teximage_component_sizes( ctx->Texture.Proxy3D->Image[level] );
156243911c8ef06c44d486a17cf9a8fa8ea263b08adeBrian Paul         ctx->Texture.Proxy3D->Image[level]->IntFormat = (GLenum) internalFormat;
1563afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         ctx->Texture.Proxy3D->Image[level]->Border = border;
1564afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         ctx->Texture.Proxy3D->Image[level]->Width = width;
1565afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         ctx->Texture.Proxy3D->Image[level]->Height = height;
1566afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         ctx->Texture.Proxy3D->Image[level]->Depth  = depth;
1567afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      }
1568afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   }
1569afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   else {
1570c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      gl_error( ctx, GL_INVALID_ENUM, "glTexImage3D(target)" );
1571afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      return;
1572afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   }
1573afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg}
1574afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
1575afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
1576663049a719ed76aa0a6f24f1951a411d241bc8a4Brian Paulvoid
157743911c8ef06c44d486a17cf9a8fa8ea263b08adeBrian Paul_mesa_TexImage3DEXT( GLenum target, GLint level, GLenum internalFormat,
1578663049a719ed76aa0a6f24f1951a411d241bc8a4Brian Paul                     GLsizei width, GLsizei height, GLsizei depth,
1579663049a719ed76aa0a6f24f1951a411d241bc8a4Brian Paul                     GLint border, GLenum format, GLenum type,
1580663049a719ed76aa0a6f24f1951a411d241bc8a4Brian Paul                     const GLvoid *pixels )
1581663049a719ed76aa0a6f24f1951a411d241bc8a4Brian Paul{
158243911c8ef06c44d486a17cf9a8fa8ea263b08adeBrian Paul   _mesa_TexImage3D(target, level, (GLint) internalFormat, width, height,
1583663049a719ed76aa0a6f24f1951a411d241bc8a4Brian Paul                    depth, border, format, type, pixels);
1584663049a719ed76aa0a6f24f1951a411d241bc8a4Brian Paul}
1585663049a719ed76aa0a6f24f1951a411d241bc8a4Brian Paul
1586663049a719ed76aa0a6f24f1951a411d241bc8a4Brian Paul
1587f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul/*
1588f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul * Fetch a texture image from the device driver.
1589f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul * Store the results in the given texture object at the given mipmap level.
1590f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul */
1591021a525616bef2bafc8f60edc193b975ed2b0efaBrian Paulvoid
1592021a525616bef2bafc8f60edc193b975ed2b0efaBrian Paul_mesa_get_teximage_from_driver( GLcontext *ctx, GLenum target, GLint level,
1593021a525616bef2bafc8f60edc193b975ed2b0efaBrian Paul                                const struct gl_texture_object *texObj )
1594f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul{
1595f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul   GLvoid *image;
1596f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul   GLenum imgFormat, imgType;
1597f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul   GLboolean freeImage;
1598f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul   struct gl_texture_image *texImage;
1599f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul   GLint destComponents, numPixels, srcBytesPerTexel;
1600f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul
1601f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul   if (!ctx->Driver.GetTexImage)
1602f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul      return;
1603f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul
16044827179cc0d314f69671e97dcb016f5a3b90a213Brian Paul   image = (*ctx->Driver.GetTexImage)( ctx, target, level, texObj,
1605f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul                                       &imgFormat, &imgType, &freeImage);
1606f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul   if (!image)
1607f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul      return;
1608f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul
1609f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul   texImage = texObj->Image[level];
1610f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul   ASSERT(texImage);
1611f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul   if (!texImage)
1612f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul      return;
1613f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul
1614f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul   destComponents = components_in_intformat(texImage->Format);
1615f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul   assert(destComponents > 0);
1616f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul   numPixels = texImage->Width * texImage->Height * texImage->Depth;
1617f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul   assert(numPixels > 0);
1618b7d076fc96ac27117421653a043d00a95f789d24Brian Paul   srcBytesPerTexel = _mesa_bytes_per_pixel(imgFormat, imgType);
1619f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul   assert(srcBytesPerTexel > 0);
1620f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul
1621f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul   if (!texImage->Data) {
1622f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul      /* Allocate memory for the texture image data */
1623f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul      texImage->Data = (GLubyte *) MALLOC(numPixels * destComponents + EXTRA_BYTE);
1624f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul   }
1625f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul
1626f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul   if (imgFormat == texImage->Format && imgType == GL_UNSIGNED_BYTE) {
1627f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul      /* We got lucky!  The driver's format and type match Mesa's format. */
1628f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul      if (texImage->Data) {
1629f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul         MEMCPY(texImage->Data, image, numPixels * destComponents);
1630f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul      }
1631f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul   }
1632f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul   else {
1633f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul      /* Convert the texture image from the driver's format to Mesa's
1634f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul       * internal format.
1635f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul       */
1636f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul      const GLint width = texImage->Width;
1637f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul      const GLint height = texImage->Height;
1638f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul      const GLint depth = texImage->Depth;
1639f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul      const GLint destBytesPerRow = width * destComponents * sizeof(GLchan);
1640f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul      const GLint srcBytesPerRow = width * srcBytesPerTexel;
1641f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul      const GLenum dstType = GL_UNSIGNED_BYTE;
1642f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul      const GLenum dstFormat = texImage->Format;
1643f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul      const GLubyte *srcPtr = (const GLubyte *) image;
1644f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul      GLubyte *destPtr = texImage->Data;
1645f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul
1646f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul      if (texImage->Format == GL_COLOR_INDEX) {
1647f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul         /* color index texture */
1648f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul         GLint img, row;
1649f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul         assert(imgFormat == GL_COLOR_INDEX);
1650f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul         for (img = 0; img < depth; img++) {
1651f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul            for (row = 0; row < height; row++) {
1652f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul               _mesa_unpack_index_span(ctx, width, dstType, destPtr,
16530293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul                             imgType, srcPtr, &_mesa_native_packing, GL_FALSE);
1654f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul               destPtr += destBytesPerRow;
1655f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul               srcPtr += srcBytesPerRow;
1656f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul            }
1657f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul         }
1658f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul      }
1659f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul      else {
1660f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul         /* color texture */
1661f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul         GLint img, row;
1662f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul         for (img = 0; img < depth; img++) {
1663f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul            for (row = 0; row < height; row++) {
1664f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul               _mesa_unpack_ubyte_color_span(ctx, width, dstFormat, destPtr,
16650293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul                  imgFormat, imgType, srcPtr, &_mesa_native_packing, GL_FALSE);
1666f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul               destPtr += destBytesPerRow;
1667f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul               srcPtr += srcBytesPerRow;
1668f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul            }
1669f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul         }
1670f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul      }
1671f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul   }
1672f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul
1673f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul   if (freeImage)
1674f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul      FREE(image);
1675f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul}
1676f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul
1677afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
1678fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paulvoid
1679fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul_mesa_GetTexImage( GLenum target, GLint level, GLenum format,
1680fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul                   GLenum type, GLvoid *pixels )
1681afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg{
1682fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul   GET_CURRENT_CONTEXT(ctx);
1683afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   const struct gl_texture_object *texObj;
1684f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul   struct gl_texture_image *texImage;
1685f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul   GLboolean discardImage;
1686afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
1687afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glGetTexImage");
1688afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
1689afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   if (level < 0 || level >= ctx->Const.MaxTextureLevels) {
1690afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      gl_error( ctx, GL_INVALID_VALUE, "glGetTexImage(level)" );
1691afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      return;
1692afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   }
1693afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
1694b7d076fc96ac27117421653a043d00a95f789d24Brian Paul   if (_mesa_sizeof_type(type) <= 0) {
1695afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      gl_error( ctx, GL_INVALID_ENUM, "glGetTexImage(type)" );
1696afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      return;
1697afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   }
1698afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
1699b7d076fc96ac27117421653a043d00a95f789d24Brian Paul   if (_mesa_components_in_format(format) <= 0) {
1700afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      gl_error( ctx, GL_INVALID_ENUM, "glGetTexImage(format)" );
1701afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      return;
1702afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   }
1703afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
1704afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   if (!pixels)
1705f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul      return;
1706afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
1707afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   switch (target) {
1708afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_TEXTURE_1D:
1709afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         texObj = ctx->Texture.Unit[ctx->Texture.CurrentUnit].CurrentD[1];
1710afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         break;
1711afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_TEXTURE_2D:
1712afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         texObj = ctx->Texture.Unit[ctx->Texture.CurrentUnit].CurrentD[2];
1713afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         break;
1714afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      case GL_TEXTURE_3D:
1715afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         texObj = ctx->Texture.Unit[ctx->Texture.CurrentUnit].CurrentD[3];
1716afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         break;
1717afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      default:
1718afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         gl_error( ctx, GL_INVALID_ENUM, "glGetTexImage(target)" );
1719afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         return;
1720afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   }
1721afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
1722f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul   texImage = texObj->Image[level];
1723f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul   if (!texImage) {
1724f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul      /* invalid mipmap level */
1725f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul      return;
1726f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul   }
1727f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul
1728f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul   if (!texImage->Data) {
1729f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul      /* try to get the texture image from the device driver */
1730021a525616bef2bafc8f60edc193b975ed2b0efaBrian Paul      _mesa_get_teximage_from_driver(ctx, target, level, texObj);
1731f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul      discardImage = GL_TRUE;
1732f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul   }
1733f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul   else {
1734f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul      discardImage = GL_FALSE;
1735f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul   }
1736f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul
1737f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul   if (texImage->Data) {
1738afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      GLint width = texImage->Width;
1739afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      GLint height = texImage->Height;
1740afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      GLint row;
1741afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
1742afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      for (row = 0; row < height; row++) {
1743afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         /* compute destination address in client memory */
1744b7d076fc96ac27117421653a043d00a95f789d24Brian Paul         GLvoid *dest = _mesa_image_address( &ctx->Unpack, pixels,
1745afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg                                                width, height,
1746afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg                                                format, type, 0, row, 0);
1747afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
1748afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         assert(dest);
1749afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         if (texImage->Format == GL_RGBA) {
1750afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg            const GLubyte *src = texImage->Data + row * width * 4 * sizeof(GLubyte);
1751b7d076fc96ac27117421653a043d00a95f789d24Brian Paul            _mesa_pack_rgba_span( ctx, width, (CONST GLubyte (*)[4]) src,
1752b7d076fc96ac27117421653a043d00a95f789d24Brian Paul                                  format, type, dest, &ctx->Pack, GL_TRUE );
1753afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         }
1754afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         else {
1755afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg            /* fetch RGBA row from texture image then pack it in client mem */
1756afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg            GLubyte rgba[MAX_WIDTH][4];
1757afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg            GLint i;
1758afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg            const GLubyte *src;
1759afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg            switch (texImage->Format) {
1760afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg               case GL_ALPHA:
1761afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg                  src = texImage->Data + row * width * sizeof(GLubyte);
1762afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg                  for (i = 0; i < width; i++) {
1763afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg                     rgba[i][RCOMP] = 255;
1764afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg                     rgba[i][GCOMP] = 255;
1765afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg                     rgba[i][BCOMP] = 255;
1766afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg                     rgba[i][ACOMP] = src[i];
1767afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg                  }
1768afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg                  break;
1769afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg               case GL_LUMINANCE:
1770afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg                  src = texImage->Data + row * width * sizeof(GLubyte);
1771afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg                  for (i = 0; i < width; i++) {
1772afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg                     rgba[i][RCOMP] = src[i];
1773afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg                     rgba[i][GCOMP] = src[i];
1774afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg                     rgba[i][BCOMP] = src[i];
1775afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg                     rgba[i][ACOMP] = 255;
1776afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg                   }
1777afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg                  break;
1778afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg               case GL_LUMINANCE_ALPHA:
1779afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg                  src = texImage->Data + row * 2 * width * sizeof(GLubyte);
1780afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg                  for (i = 0; i < width; i++) {
1781afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg                     rgba[i][RCOMP] = src[i*2+0];
1782afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg                     rgba[i][GCOMP] = src[i*2+0];
1783afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg                     rgba[i][BCOMP] = src[i*2+0];
1784afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg                     rgba[i][ACOMP] = src[i*2+1];
1785afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg                  }
1786afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg                  break;
1787afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg               case GL_INTENSITY:
1788afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg                  src = texImage->Data + row * width * sizeof(GLubyte);
1789afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg                  for (i = 0; i < width; i++) {
1790afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg                     rgba[i][RCOMP] = src[i];
1791afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg                     rgba[i][GCOMP] = src[i];
1792afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg                     rgba[i][BCOMP] = src[i];
1793afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg                     rgba[i][ACOMP] = 255;
1794afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg                  }
1795afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg                  break;
1796afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg               case GL_RGB:
1797afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg                  src = texImage->Data + row * 3 * width * sizeof(GLubyte);
1798afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg                  for (i = 0; i < width; i++) {
1799afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg                     rgba[i][RCOMP] = src[i*3+0];
1800afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg                     rgba[i][GCOMP] = src[i*3+1];
1801afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg                     rgba[i][BCOMP] = src[i*3+2];
1802afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg                     rgba[i][ACOMP] = 255;
1803afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg                  }
1804afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg                  break;
1805afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg               case GL_RGBA:
1806afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg                  /* this special case should have been handled above! */
1807afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg                  gl_problem( ctx, "error 1 in gl_GetTexImage" );
1808afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg                  break;
1809afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg               case GL_COLOR_INDEX:
1810afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg                  gl_problem( ctx, "GL_COLOR_INDEX not implemented in gl_GetTexImage" );
1811afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg                  break;
1812afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg               default:
1813afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg                  gl_problem( ctx, "bad format in gl_GetTexImage" );
1814afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg            }
1815b7d076fc96ac27117421653a043d00a95f789d24Brian Paul            _mesa_pack_rgba_span( ctx, width, (const GLubyte (*)[4])rgba,
1816b7d076fc96ac27117421653a043d00a95f789d24Brian Paul                                  format, type, dest, &ctx->Pack, GL_TRUE );
1817afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         }
1818afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      }
1819f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul
1820f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul      /* if we got the teximage from the device driver we'll discard it now */
1821f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul      if (discardImage) {
1822f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul         FREE(texImage->Data);
1823f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul         texImage->Data = NULL;
1824f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul      }
1825afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   }
1826afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg}
1827afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
1828afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
1829afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
1830fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paulvoid
1831fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul_mesa_TexSubImage1D( GLenum target, GLint level,
1832fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul                     GLint xoffset, GLsizei width,
1833fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul                     GLenum format, GLenum type,
1834fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul                     const GLvoid *pixels )
1835afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg{
1836fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul   GET_CURRENT_CONTEXT(ctx);
18370293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul   struct gl_texture_unit *texUnit;
18380293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul   struct gl_texture_object *texObj;
18390293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul   struct gl_texture_image *texImage;
18400293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul   GLboolean success = GL_FALSE;
1841afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
1842c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   if (subtexture_error_check(ctx, 1, target, level, xoffset, 0, 0,
1843c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul                              width, 1, 1, format, type)) {
1844f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul      return;   /* error was detected */
1845afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   }
1846afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
18470293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul   texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
18480293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul   texObj = texUnit->CurrentD[1];
18490293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul   texImage = texObj->Image[level];
18500293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul   assert(texImage);
1851afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
1852c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   if (width == 0 || !pixels)
1853c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      return;  /* no-op, not an error */
1854afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
1855afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
18560293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul   if (!ctx->Pixel.MapColorFlag && !ctx->Pixel.ScaleOrBiasRGBA
18570293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul       && ctx->Driver.TexSubImage1D) {
18580293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      success = (*ctx->Driver.TexSubImage1D)( ctx, target, level, xoffset,
18590293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul                                              width, format, type, pixels,
18600293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul                                              &ctx->Unpack, texObj, texImage );
18610293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul   }
18620293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul   if (!success) {
18630293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      /* XXX if Driver.TexSubImage1D, unpack image and try again? */
18640293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul
18650293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      const GLint texComponents = components_in_intformat(texImage->Format);
18660293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      const GLenum texFormat = texImage->Format;
18670293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      const GLint xoffsetb = xoffset + texImage->Border;
18680293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      GLboolean retain = GL_TRUE;
18690293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      if (!texImage->Data) {
1870021a525616bef2bafc8f60edc193b975ed2b0efaBrian Paul         _mesa_get_teximage_from_driver( ctx, target, level, texObj );
18710293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul         if (!texImage->Data) {
18720293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul            make_null_texture(texImage);
18730293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul         }
18740293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul         if (!texImage->Data)
18750293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul            return;  /* we're really out of luck! */
18760293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      }
18770293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul
1878c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      if (texFormat == GL_COLOR_INDEX) {
1879c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         /* color index texture */
18800293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul         GLubyte *dst = texImage->Data + xoffsetb * texComponents;
18810293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul         const GLvoid *src = _mesa_image_address(&ctx->Unpack, pixels, width,
18820293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul                                                 1, format, type, 0, 0, 0);
1883c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         _mesa_unpack_index_span(ctx, width, GL_UNSIGNED_BYTE, dst,
18840293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul                                 type, src, &ctx->Unpack, GL_TRUE);
1885c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      }
1886c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      else {
1887c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         /* color texture */
18880293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul         GLubyte *dst = texImage->Data + xoffsetb * texComponents;
18890293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul         const GLvoid *src = _mesa_image_address(&ctx->Unpack, pixels, width,
18900293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul                                                 1, format, type, 0, 0, 0);
18910293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul         _mesa_unpack_ubyte_color_span(ctx, width, texFormat, dst, format,
18920293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul                                       type, src, &ctx->Unpack, GL_TRUE);
1893c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      }
1894afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
18950293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      if (ctx->Driver.TexImage1D) {
18960293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul         (*ctx->Driver.TexImage1D)( ctx, target, level, texImage->Format,
18970293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul                                    GL_UNSIGNED_BYTE, texImage->Data,
18980293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul                                    &_mesa_native_packing, texObj, texImage,
18990293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul                                    &retain );
19000293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      }
1901afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
19020293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      if (!retain && texImage->Data) {
19030293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul         FREE(texImage->Data);
19040293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul         texImage->Data = NULL;
1905c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      }
1906afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   }
19070293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul
19080293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul   /*gl_put_texobj_on_dirty_list( ctx, texUnit->CurrentD[1] );*/
1909afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg}
1910afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
1911afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
1912fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paulvoid
1913fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul_mesa_TexSubImage2D( GLenum target, GLint level,
1914fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul                     GLint xoffset, GLint yoffset,
1915fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul                     GLsizei width, GLsizei height,
1916fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul                     GLenum format, GLenum type,
1917fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul                     const GLvoid *pixels )
1918afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg{
1919fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul   GET_CURRENT_CONTEXT(ctx);
19200293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul   struct gl_texture_unit *texUnit;
19210293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul   struct gl_texture_object *texObj;
19220293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul   struct gl_texture_image *texImage;
19230293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul   GLboolean success = GL_FALSE;
1924afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
1925c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   if (subtexture_error_check(ctx, 2, target, level, xoffset, yoffset, 0,
1926c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul                              width, height, 1, format, type)) {
1927f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul      return;   /* error was detected */
1928afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   }
1929afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
19300293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul   texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
193135d5301a54153930ee6fd60dff1010ce9f901397Brian Paul   texObj = _mesa_select_tex_object(ctx, texUnit, target);
19320293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul   texImage = texObj->Image[level];
19330293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul   assert(texImage);
1934afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
1935c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   if (width == 0 || height == 0 || !pixels)
1936c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      return;  /* no-op, not an error */
1937afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
19380293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul   if (!ctx->Pixel.MapColorFlag && !ctx->Pixel.ScaleOrBiasRGBA
19390293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul       && ctx->Driver.TexSubImage2D) {
19400293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      success = (*ctx->Driver.TexSubImage2D)( ctx, target, level, xoffset,
19410293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul                                     yoffset, width, height, format, type,
19420293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul                                     pixels, &ctx->Unpack, texObj, texImage );
19430293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul   }
19440293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul   if (!success) {
19450293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      /* XXX if Driver.TexSubImage2D, unpack image and try again? */
19460293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul
19470293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      const GLint texComponents = components_in_intformat(texImage->Format);
19480293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      const GLenum texFormat = texImage->Format;
19490293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      const GLint xoffsetb = xoffset + texImage->Border;
19500293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      const GLint yoffsetb = yoffset + texImage->Border;
19510293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      const GLint srcStride = _mesa_image_row_stride(&ctx->Unpack, width,
19520293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul                                                     format, type);
19530293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      const GLint dstStride = texImage->Width * texComponents *sizeof(GLubyte);
19540293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      GLboolean retain = GL_TRUE;
19550293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul
19560293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      if (!texImage->Data) {
1957021a525616bef2bafc8f60edc193b975ed2b0efaBrian Paul         _mesa_get_teximage_from_driver( ctx, target, level, texObj );
19580293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul         if (!texImage->Data) {
19590293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul            make_null_texture(texImage);
19600293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul         }
19610293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul         if (!texImage->Data)
19620293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul            return;  /* we're really out of luck! */
19630293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      }
1964afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
1965c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      if (texFormat == GL_COLOR_INDEX) {
1966c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         /* color index texture */
19670293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul         GLubyte *dst = texImage->Data
19680293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul                    + (yoffsetb * texImage->Width + xoffsetb) * texComponents;
19690293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul         const GLubyte *src = _mesa_image_address(&ctx->Unpack, pixels,
19700293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul                                     width, height, format, type, 0, 0, 0);
1971c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         GLint row;
1972c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         for (row = 0; row < height; row++) {
19730293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul            _mesa_unpack_index_span(ctx, width, GL_UNSIGNED_BYTE, dst, type,
19740293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul                                 (const GLvoid *) src, &ctx->Unpack, GL_TRUE);
19750293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul            src += srcStride;
19760293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul            dst += dstStride;
1977c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         }
1978afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      }
1979afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      else {
1980c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         /* color texture */
19810293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul         GLubyte *dst = texImage->Data
19820293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul                    + (yoffsetb * texImage->Width + xoffsetb) * texComponents;
19830293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul         const GLubyte *src = _mesa_image_address(&ctx->Unpack, pixels,
19840293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul                                     width, height, format, type, 0, 0, 0);
1985c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         GLint row;
1986c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         for (row = 0; row < height; row++) {
19870293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul            _mesa_unpack_ubyte_color_span(ctx, width, texFormat, dst, format,
19880293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul                           type, (const GLvoid *) src, &ctx->Unpack, GL_TRUE);
19890293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul            src += srcStride;
19900293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul            dst += dstStride;
1991c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         }
1992afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      }
1993afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
19940293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      if (ctx->Driver.TexImage2D) {
19950293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul         (*ctx->Driver.TexImage2D)(ctx, target, level, texImage->Format,
19960293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul                                   GL_UNSIGNED_BYTE, texImage->Data,
19970293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul                                   &_mesa_native_packing, texObj, texImage,
19980293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul                                   &retain);
19990293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      }
2000c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul
20010293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      if (!retain && texImage->Data) {
20020293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul         FREE(texImage->Data);
20030293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul         texImage->Data = NULL;
2004afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      }
20050293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul
20060293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul#ifdef OLD_DD_TEXTURE
20070293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      /* XXX this will be removed in the future */
20080293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      if (ctx->Driver.TexSubImage) {
20090293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul         (*ctx->Driver.TexSubImage)(ctx, target, texObj, level,
20100293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul                                    xoffset, yoffset, width, height,
20110293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul                                    texImage->IntFormat, texImage);
20120293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      }
20130293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      else if (ctx->Driver.TexImage) {
20149fd2b0a698163b397b0a17493e52b27273e51b63Brian Paul         (*ctx->Driver.TexImage)(ctx, GL_TEXTURE_2D, texObj,
20150293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul                                 level, texImage->IntFormat, texImage );
20160293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      }
20170293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul#endif
2018c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   }
2019c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul}
2020afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2021afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2022afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2023fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paulvoid
2024fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul_mesa_TexSubImage3D( GLenum target, GLint level,
2025fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul                     GLint xoffset, GLint yoffset, GLint zoffset,
2026fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul                     GLsizei width, GLsizei height, GLsizei depth,
2027fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul                     GLenum format, GLenum type,
2028fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul                     const GLvoid *pixels )
2029afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg{
2030fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul   GET_CURRENT_CONTEXT(ctx);
20310293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul   struct gl_texture_unit *texUnit;
20320293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul   struct gl_texture_object *texObj;
20330293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul   struct gl_texture_image *texImage;
20340293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul   GLboolean success = GL_FALSE;
2035afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2036c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   if (subtexture_error_check(ctx, 3, target, level, xoffset, yoffset, zoffset,
2037c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul                              width, height, depth, format, type)) {
2038f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul      return;   /* error was detected */
2039afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   }
2040afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
20410293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul   texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
20420293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul   texObj = texUnit->CurrentD[3];
20430293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul   texImage = texObj->Image[level];
20440293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul   assert(texImage);
2045afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2046c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   if (width == 0 || height == 0 || height == 0 || !pixels)
2047c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      return;  /* no-op, not an error */
2048afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
20490293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul   if (!ctx->Pixel.MapColorFlag && !ctx->Pixel.ScaleOrBiasRGBA
20500293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul       && ctx->Driver.TexSubImage3D) {
20510293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      success = (*ctx->Driver.TexSubImage3D)( ctx, target, level, xoffset,
20520293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul                                yoffset, zoffset, width, height, depth, format,
20530293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul                                type, pixels, &ctx->Unpack, texObj, texImage );
20540293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul   }
20550293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul   if (!success) {
20560293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      /* XXX if Driver.TexSubImage3D, unpack image and try again? */
20570293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul
20580293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      const GLint texComponents = components_in_intformat(texImage->Format);
20590293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      const GLenum texFormat = texImage->Format;
20600293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      const GLint xoffsetb = xoffset + texImage->Border;
20610293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      const GLint yoffsetb = yoffset + texImage->Border;
20620293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      const GLint zoffsetb = zoffset + texImage->Border;
20630293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      const GLint texWidth = texImage->Width;
20640293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      const GLint dstRectArea = texWidth * texImage->Height;
20650293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      const GLint srcStride = _mesa_image_row_stride(&ctx->Unpack,
20660293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul                                                     width, format, type);
20670293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      const GLint dstStride = texWidth * texComponents * sizeof(GLubyte);
20680293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      GLboolean retain = GL_TRUE;
2069c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul
2070c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      if (texFormat == GL_COLOR_INDEX) {
2071c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         /* color index texture */
2072c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         GLint img, row;
2073c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         for (img = 0; img < depth; img++) {
20740293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul            const GLubyte *src = _mesa_image_address(&ctx->Unpack, pixels,
20750293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul                                    width, height, format, type, img, 0, 0);
20760293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul            GLubyte *dst = texImage->Data + ((zoffsetb + img) * dstRectArea
20770293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul                     + yoffsetb * texWidth + xoffsetb) * texComponents;
2078c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul            for (row = 0; row < height; row++) {
2079c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul               _mesa_unpack_index_span(ctx, width, GL_UNSIGNED_BYTE, dst,
20800293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul                         type, (const GLvoid *) src, &ctx->Unpack, GL_TRUE);
20810293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul               src += srcStride;
20820293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul               dst += dstStride;
2083c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul            }
2084afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         }
2085afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      }
2086afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      else {
2087c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         /* color texture */
2088c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         GLint img, row;
2089c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         for (img = 0; img < depth; img++) {
20900293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul            const GLubyte *src = _mesa_image_address(&ctx->Unpack, pixels,
20910293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul                                     width, height, format, type, img, 0, 0);
20920293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul            GLubyte *dst = texImage->Data + ((zoffsetb + img) * dstRectArea
20930293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul                     + yoffsetb * texWidth + xoffsetb) * texComponents;
2094c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul            for (row = 0; row < height; row++) {
2095c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul               _mesa_unpack_ubyte_color_span(ctx, width, texFormat, dst,
20960293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul                   format, type, (const GLvoid *) src, &ctx->Unpack, GL_TRUE);
20970293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul               src += srcStride;
20980293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul               dst += dstStride;
2099c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul            }
2100afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         }
2101afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      }
2102afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
21030293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      if (ctx->Driver.TexImage3D) {
21040293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul         (*ctx->Driver.TexImage3D)(ctx, target, level, texImage->Format,
21050293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul                                   GL_UNSIGNED_BYTE, texImage->Data,
21060293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul                                   &_mesa_native_packing, texObj, texImage,
21070293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul                                   &retain);
21080293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      }
2109c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul
21100293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      if (!retain && texImage->Data) {
21110293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul         FREE(texImage->Data);
21120293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul         texImage->Data = NULL;
21130293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      }
21140293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul   }
2115afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg}
2116afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2117afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2118afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2119afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg/*
2120afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg * Read an RGBA image from the frame buffer.
2121c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul * This is used by glCopyTexSubImage[12]D().
2122afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg * Input:  ctx - the context
2123afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg *         x, y - lower left corner
2124afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg *         width, height - size of region to read
2125c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul * Return: pointer to block of GL_RGBA, GLubyte data.
2126afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg */
2127c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paulstatic GLubyte *
2128c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paulread_color_image( GLcontext *ctx, GLint x, GLint y,
2129c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul                  GLsizei width, GLsizei height )
2130afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg{
2131c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   GLint stride, i;
2132c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   GLubyte *image, *dst;
2133afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2134959f802dabd4f4347dc0ea925ae687abb6938588Brian Paul   image = (GLubyte *) MALLOC(width * height * 4 * sizeof(GLubyte));
2135c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   if (!image)
2136afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      return NULL;
2137afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2138afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   /* Select buffer to read from */
2139cea0e8e6700e871bb9bcb334cb75fbfbfcdfd702Brian Paul   (*ctx->Driver.SetReadBuffer)( ctx, ctx->ReadBuffer,
2140cea0e8e6700e871bb9bcb334cb75fbfbfcdfd702Brian Paul                                 ctx->Pixel.DriverReadBuffer );
2141afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2142c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   dst = image;
2143c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   stride = width * 4 * sizeof(GLubyte);
2144c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   for (i = 0; i < height; i++) {
21453f02f90f943a996d88abc20f74503afbb56a4c98Brian Paul      gl_read_rgba_span( ctx, ctx->ReadBuffer, width, x, y + i,
21463f02f90f943a996d88abc20f74503afbb56a4c98Brian Paul                         (GLubyte (*)[4]) dst );
2147c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      dst += stride;
2148c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   }
2149afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2150cea0e8e6700e871bb9bcb334cb75fbfbfcdfd702Brian Paul   /* Read from draw buffer (the default) */
2151cea0e8e6700e871bb9bcb334cb75fbfbfcdfd702Brian Paul   (*ctx->Driver.SetReadBuffer)( ctx, ctx->DrawBuffer,
2152cea0e8e6700e871bb9bcb334cb75fbfbfcdfd702Brian Paul                                 ctx->Color.DriverDrawBuffer );
2153afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2154afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   return image;
2155afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg}
2156afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2157afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2158afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2159fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paulvoid
2160fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul_mesa_CopyTexImage1D( GLenum target, GLint level,
2161fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul                      GLenum internalFormat,
2162fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul                      GLint x, GLint y,
2163fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul                      GLsizei width, GLint border )
2164afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg{
2165fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul   GET_CURRENT_CONTEXT(ctx);
2166afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glCopyTexImage1D");
2167afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2168f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul   if (copytexture_error_check(ctx, 1, target, level, internalFormat,
2169f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul                               width, 1, border))
2170f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul      return;
2171f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul
2172f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul   if (ctx->Pixel.MapColorFlag || ctx->Pixel.ScaleOrBiasRGBA
2173f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul       || !ctx->Driver.CopyTexImage1D
2174f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul       || !(*ctx->Driver.CopyTexImage1D)(ctx, target, level,
2175f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul                         internalFormat, x, y, width, border))
2176f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul   {
2177c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      GLubyte *image  = read_color_image( ctx, x, y, width, 1 );
2178c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      if (!image) {
2179c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         gl_error( ctx, GL_OUT_OF_MEMORY, "glCopyTexImage1D" );
2180c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         return;
2181c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      }
21823ab6bbe6135da26dfe9a9ba880386fdc98f6580aBrian Paul      (*ctx->Exec->TexImage1D)( target, level, internalFormat, width,
2183f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul                                border, GL_RGBA, GL_UNSIGNED_BYTE, image );
2184c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      FREE(image);
2185afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   }
2186afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg}
2187afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2188afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2189afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2190fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paulvoid
2191fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul_mesa_CopyTexImage2D( GLenum target, GLint level, GLenum internalFormat,
2192fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul                      GLint x, GLint y, GLsizei width, GLsizei height,
2193fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul                      GLint border )
2194afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg{
2195fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul   GET_CURRENT_CONTEXT(ctx);
2196afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glCopyTexImage2D");
2197afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2198f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul   if (copytexture_error_check(ctx, 2, target, level, internalFormat,
2199f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul                               width, height, border))
2200f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul      return;
2201f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul
2202f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul   if (ctx->Pixel.MapColorFlag || ctx->Pixel.ScaleOrBiasRGBA
2203f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul       || !ctx->Driver.CopyTexImage2D
2204f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul       || !(*ctx->Driver.CopyTexImage2D)(ctx, target, level,
2205f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul                         internalFormat, x, y, width, height, border))
2206f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul   {
2207c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      GLubyte *image  = read_color_image( ctx, x, y, width, height );
2208c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      if (!image) {
2209c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         gl_error( ctx, GL_OUT_OF_MEMORY, "glCopyTexImage2D" );
2210c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         return;
2211c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      }
22123ab6bbe6135da26dfe9a9ba880386fdc98f6580aBrian Paul      (ctx->Exec->TexImage2D)( target, level, internalFormat, width,
2213f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul                      height, border, GL_RGBA, GL_UNSIGNED_BYTE, image );
2214c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      FREE(image);
2215afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   }
2216afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg}
2217afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2218afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2219afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2220afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg/*
2221afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg * Do the work of glCopyTexSubImage[123]D.
2222afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg */
2223c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paulstatic void
2224c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paulcopy_tex_sub_image( GLcontext *ctx, struct gl_texture_image *dest,
2225c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul                    GLint width, GLint height,
2226c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul                    GLint srcx, GLint srcy,
2227c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul                    GLint dstx, GLint dsty, GLint dstz )
2228afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg{
2229c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   GLint i;
2230afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   GLint format, components, rectarea;
223191baaa3d5a34a0a746dcb70967364a388d5ca438Brian Paul   GLint texwidth, texheight, zoffset;
2232afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
223391baaa3d5a34a0a746dcb70967364a388d5ca438Brian Paul   /* dst[xyz] may be negative if we have a texture border! */
223491baaa3d5a34a0a746dcb70967364a388d5ca438Brian Paul   dstx += dest->Border;
223591baaa3d5a34a0a746dcb70967364a388d5ca438Brian Paul   dsty += dest->Border;
223691baaa3d5a34a0a746dcb70967364a388d5ca438Brian Paul   dstz += dest->Border;
2237afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   texwidth = dest->Width;
2238afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   texheight = dest->Height;
2239afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   rectarea = texwidth * texheight;
224091baaa3d5a34a0a746dcb70967364a388d5ca438Brian Paul   zoffset = dstz * rectarea;
2241afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   format = dest->Format;
2242afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   components = components_in_intformat( format );
2243afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2244afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   /* Select buffer to read from */
2245cea0e8e6700e871bb9bcb334cb75fbfbfcdfd702Brian Paul   (*ctx->Driver.SetReadBuffer)( ctx, ctx->ReadBuffer,
2246cea0e8e6700e871bb9bcb334cb75fbfbfcdfd702Brian Paul                                 ctx->Pixel.DriverReadBuffer );
2247afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2248c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   for (i = 0;i < height; i++) {
2249afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      GLubyte rgba[MAX_WIDTH][4];
2250c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      GLubyte *dst;
22513f02f90f943a996d88abc20f74503afbb56a4c98Brian Paul      gl_read_rgba_span( ctx, ctx->ReadBuffer, width, srcx, srcy + i, rgba );
2252c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      dst = dest->Data + ( zoffset + (dsty+i) * texwidth + dstx) * components;
2253c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      _mesa_unpack_ubyte_color_span(ctx, width, format, dst,
2254c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul                                    GL_RGBA, GL_UNSIGNED_BYTE, rgba,
22550293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul                                    &_mesa_native_packing, GL_TRUE);
2256c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   }
2257afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2258cea0e8e6700e871bb9bcb334cb75fbfbfcdfd702Brian Paul   /* Read from draw buffer (the default) */
2259cea0e8e6700e871bb9bcb334cb75fbfbfcdfd702Brian Paul   (*ctx->Driver.SetReadBuffer)( ctx, ctx->DrawBuffer,
2260cea0e8e6700e871bb9bcb334cb75fbfbfcdfd702Brian Paul                                 ctx->Color.DriverDrawBuffer );
2261afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg}
2262afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2263afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2264afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2265afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2266fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paulvoid
2267fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul_mesa_CopyTexSubImage1D( GLenum target, GLint level,
2268fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul                         GLint xoffset, GLint x, GLint y, GLsizei width )
2269afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg{
2270fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul   GET_CURRENT_CONTEXT(ctx);
2271afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glCopyTexSubImage1D");
2272afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2273f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul   if (copytexsubimage_error_check(ctx, 1, target, level,
2274f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul                                   xoffset, 0, 0, width, 1))
2275f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul      return;
2276f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul
2277f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul   if (ctx->Pixel.MapColorFlag || ctx->Pixel.ScaleOrBiasRGBA
2278f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul       || !ctx->Driver.CopyTexSubImage1D
2279f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul       || !(*ctx->Driver.CopyTexSubImage1D)(ctx, target, level,
2280f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul                                            xoffset, x, y, width)) {
2281c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      struct gl_texture_unit *texUnit;
2282c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      struct gl_texture_image *teximage;
2283c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2284c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      teximage = texUnit->CurrentD[1]->Image[level];
2285c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      assert(teximage);
2286afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      if (teximage->Data) {
2287c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         copy_tex_sub_image(ctx, teximage, width, 1, x, y, xoffset, 0, 0);
2288f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul         /* tell driver about the change */
22899fd2b0a698163b397b0a17493e52b27273e51b63Brian Paul         /* XXX this is obsolete */
2290f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul         if (ctx->Driver.TexImage) {
2291f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul            (*ctx->Driver.TexImage)( ctx, GL_TEXTURE_1D,
2292f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul                                     texUnit->CurrentD[1],
2293f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul                                     level, teximage->IntFormat, teximage );
2294f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul         }
2295afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      }
2296afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   }
2297afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg}
2298afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2299afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2300afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2301fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paulvoid
2302fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul_mesa_CopyTexSubImage2D( GLenum target, GLint level,
2303fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul                         GLint xoffset, GLint yoffset,
2304fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul                         GLint x, GLint y, GLsizei width, GLsizei height )
2305afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg{
2306fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul   GET_CURRENT_CONTEXT(ctx);
2307afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glCopyTexSubImage2D");
2308afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2309f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul   if (copytexsubimage_error_check(ctx, 2, target, level,
2310f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul                                   xoffset, yoffset, 0, width, height))
2311f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul      return;
2312f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul
2313f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul   if (ctx->Pixel.MapColorFlag || ctx->Pixel.ScaleOrBiasRGBA
2314f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul       || !ctx->Driver.CopyTexSubImage2D
2315f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul       || !(*ctx->Driver.CopyTexSubImage2D)(ctx, target, level,
2316f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul                                xoffset, yoffset, x, y, width, height )) {
2317c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      struct gl_texture_unit *texUnit;
2318c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      struct gl_texture_image *teximage;
2319c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2320c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      teximage = texUnit->CurrentD[2]->Image[level];
2321c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul      assert(teximage);
2322afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      if (teximage->Data) {
2323c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul         copy_tex_sub_image(ctx, teximage, width, height,
2324c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul                            x, y, xoffset, yoffset, 0);
2325f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul         /* tell driver about the change */
23269fd2b0a698163b397b0a17493e52b27273e51b63Brian Paul         /* XXX this is obsolete */
2327f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul         if (ctx->Driver.TexImage) {
2328f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul            (*ctx->Driver.TexImage)( ctx, GL_TEXTURE_2D,
2329f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul                                     texUnit->CurrentD[2],
2330f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul                                     level, teximage->IntFormat, teximage );
2331afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg	 }
2332afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      }
2333afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   }
2334afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg}
2335afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2336afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2337afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2338fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paulvoid
2339fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul_mesa_CopyTexSubImage3D( GLenum target, GLint level,
2340fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul                         GLint xoffset, GLint yoffset, GLint zoffset,
2341fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul                         GLint x, GLint y, GLsizei width, GLsizei height )
2342afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg{
2343fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul   GET_CURRENT_CONTEXT(ctx);
2344c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glCopyTexSubImage3D");
2345afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
2346f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul   if (copytexsubimage_error_check(ctx, 3, target, level,
2347f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul                    xoffset, yoffset, zoffset, width, height))
2348f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul      return;
2349f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul
23500293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul   if (ctx->Pixel.MapColorFlag || ctx->Pixel.ScaleOrBiasRGBA
2351f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul       || !ctx->Driver.CopyTexSubImage3D
2352f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul       || !(*ctx->Driver.CopyTexSubImage3D)(ctx, target, level,
23530293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul                     xoffset, yoffset, zoffset, x, y, width, height )) {
23540293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      struct gl_texture_unit *texUnit;
23550293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      struct gl_texture_image *teximage;
23560293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
23570293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      teximage = texUnit->CurrentD[3]->Image[level];
23580293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      assert(teximage);
23590293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul      if (teximage->Data) {
23600293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul         copy_tex_sub_image(ctx, teximage, width, height,
2361c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul                            x, y, xoffset, yoffset, zoffset);
23620293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul         /* tell driver about the change */
23639fd2b0a698163b397b0a17493e52b27273e51b63Brian Paul         /* XXX this is obsolete */
23640293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul         if (ctx->Driver.TexImage) {
23650293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul            (*ctx->Driver.TexImage)( ctx, GL_TEXTURE_3D,
23660293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul                                     texUnit->CurrentD[3],
23670293878846551bfe349a4aa029a0489f9a73f1ffBrian Paul                                     level, teximage->IntFormat, teximage );
2368f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul         }
2369afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      }
2370afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   }
2371afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg}
23721207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul
23731207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul
23741207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul
23751207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paulvoid
23761207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul_mesa_CompressedTexImage1DARB(GLenum target, GLint level,
23771207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul                              GLenum internalformat, GLsizei width,
23781207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul                              GLint border, GLsizei imageSize,
23791207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul                              const GLvoid *data)
23801207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul{
23811207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul}
23821207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul
23831207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul
23841207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paulvoid
23851207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul_mesa_CompressedTexImage2DARB(GLenum target, GLint level,
23861207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul                              GLenum internalformat, GLsizei width,
23871207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul                              GLsizei height, GLint border, GLsizei imageSize,
23881207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul                              const GLvoid *data)
23891207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul{
23901207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul}
23911207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul
23921207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul
23931207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paulvoid
23941207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul_mesa_CompressedTexImage3DARB(GLenum target, GLint level,
23951207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul                              GLenum internalformat, GLsizei width,
23961207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul                              GLsizei height, GLsizei depth, GLint border,
23971207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul                              GLsizei imageSize, const GLvoid *data)
23981207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul{
23991207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul}
24001207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul
24011207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul
24021207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paulvoid
24031207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul_mesa_CompressedTexSubImage1DARB(GLenum target, GLint level, GLint xoffset,
24041207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul                                 GLsizei width, GLenum format,
24051207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul                                 GLsizei imageSize, const GLvoid *data)
24061207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul{
24071207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul}
24081207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul
24091207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul
24101207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paulvoid
24111207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul_mesa_CompressedTexSubImage2DARB(GLenum target, GLint level, GLint xoffset,
24121207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul                                 GLint yoffset, GLsizei width, GLsizei height,
24131207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul                                 GLenum format, GLsizei imageSize,
24141207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul                                 const GLvoid *data)
24151207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul{
24161207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul}
24171207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul
24181207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul
24191207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paulvoid
24201207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul_mesa_CompressedTexSubImage3DARB(GLenum target, GLint level, GLint xoffset,
24211207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul                                 GLint yoffset, GLint zoffset, GLsizei width,
24221207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul                                 GLsizei height, GLsizei depth, GLenum format,
24231207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul                                 GLsizei imageSize, const GLvoid *data)
24241207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul{
24251207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul}
24261207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul
24271207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul
24281207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paulvoid
24291207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul_mesa_GetCompressedTexImageARB(GLenum target, GLint lod, GLvoid *img)
24301207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul{
24311207bf057aeea3376e5e0f03f789a5d67ed4d29dBrian Paul}
2432