blob: e534924e80c4f087ce6c8ed2a748e5468ee3a211 [file] [log] [blame]
Brian Paule75d2422001-02-17 18:41:01 +00001/* $Id: teximage.c,v 1.77 2001/02/17 18:41:01 brianp Exp $ */
jtgafb833d1999-08-19 00:55:39 +00002
3/*
4 * Mesa 3-D graphics library
Brian Paul01e54752000-09-05 15:40:34 +00005 * Version: 3.5
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +00006 *
Brian Paul663049a2000-01-31 23:10:16 +00007 * Copyright (C) 1999-2000 Brian Paul All Rights Reserved.
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +00008 *
jtgafb833d1999-08-19 00:55:39 +00009 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +000015 *
jtgafb833d1999-08-19 00:55:39 +000016 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +000018 *
jtgafb833d1999-08-19 00:55:39 +000019 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 */
26
27
28#ifdef PC_HEADER
29#include "all.h"
30#else
Brian Paulfbd8f211999-11-11 01:22:25 +000031#include "glheader.h"
jtgafb833d1999-08-19 00:55:39 +000032#include "context.h"
Brian Paulf93b3dd2000-08-30 18:22:28 +000033#include "convolve.h"
jtgafb833d1999-08-19 00:55:39 +000034#include "image.h"
Brian Paulebb248a2000-10-29 18:23:16 +000035#include "macros.h"
Brian Paulfbd8f211999-11-11 01:22:25 +000036#include "mem.h"
jtgafb833d1999-08-19 00:55:39 +000037#include "mmath.h"
Brian Paulfa4525e2000-08-21 14:22:24 +000038#include "state.h"
jtgafb833d1999-08-19 00:55:39 +000039#include "teximage.h"
40#include "texstate.h"
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +000041#include "mtypes.h"
Brian Paul7298e712000-11-07 16:40:37 +000042#include "swrast/s_span.h" /* XXX SWRAST hack */
jtgafb833d1999-08-19 00:55:39 +000043#endif
44
45
46/*
47 * NOTES:
48 *
Brian Paul699bc7b2000-10-29 18:12:14 +000049 * Mesa's native texture datatype is GLchan. Native formats are
Brian Paulc3f0a511999-11-03 17:27:05 +000050 * GL_ALPHA, GL_LUMINANCE, GL_LUMANCE_ALPHA, GL_INTENSITY, GL_RGB, GL_RGBA,
51 * and GL_COLOR_INDEX.
52 * Device drivers are free to implement any internal format they want.
jtgafb833d1999-08-19 00:55:39 +000053 */
54
55
Brian Paul48271792000-03-29 18:13:59 +000056#ifdef DEBUG
Brian Paule5d68a22000-03-30 18:37:51 +000057static void PrintTexture(const struct gl_texture_image *img)
Brian Paul48271792000-03-29 18:13:59 +000058{
Brian Paule5d68a22000-03-30 18:37:51 +000059 int i, j, c;
Brian Paul699bc7b2000-10-29 18:12:14 +000060 GLchan *data = img->Data;
Brian Paule5d68a22000-03-30 18:37:51 +000061
62 if (!data) {
63 printf("No texture data\n");
64 return;
65 }
66
67 switch (img->Format) {
68 case GL_ALPHA:
69 case GL_LUMINANCE:
70 case GL_INTENSITY:
71 case GL_COLOR_INDEX:
72 c = 1;
73 break;
74 case GL_LUMINANCE_ALPHA:
75 c = 2;
76 break;
77 case GL_RGB:
78 c = 3;
79 break;
80 case GL_RGBA:
81 c = 4;
82 break;
83 default:
84 gl_problem(NULL, "error in PrintTexture\n");
85 return;
86 }
87
88
89 for (i = 0; i < img->Height; i++) {
90 for (j = 0; j < img->Width; j++) {
Brian Paul48271792000-03-29 18:13:59 +000091 if (c==1)
92 printf("%02x ", data[0]);
93 else if (c==2)
Brian Paule5d68a22000-03-30 18:37:51 +000094 printf("%02x%02x ", data[0], data[1]);
Brian Paul48271792000-03-29 18:13:59 +000095 else if (c==3)
Brian Paule5d68a22000-03-30 18:37:51 +000096 printf("%02x%02x%02x ", data[0], data[1], data[2]);
Brian Paul48271792000-03-29 18:13:59 +000097 else if (c==4)
Brian Paule5d68a22000-03-30 18:37:51 +000098 printf("%02x%02x%02x%02x ", data[0], data[1], data[2], data[3]);
Brian Paul48271792000-03-29 18:13:59 +000099 data += c;
100 }
101 printf("\n");
102 }
103}
104#endif
105
106
107
Brian Paulf7b57072000-03-20 14:37:52 +0000108/*
jtgafb833d1999-08-19 00:55:39 +0000109 * Compute log base 2 of n.
110 * If n isn't an exact power of two return -1.
111 * If n<0 return -1.
112 */
Brian Paulfbd8f211999-11-11 01:22:25 +0000113static int
114logbase2( int n )
jtgafb833d1999-08-19 00:55:39 +0000115{
116 GLint i = 1;
117 GLint log2 = 0;
118
119 if (n<0) {
120 return -1;
121 }
122
123 while ( n > i ) {
124 i *= 2;
125 log2++;
126 }
127 if (i != n) {
128 return -1;
129 }
130 else {
131 return log2;
132 }
133}
134
135
136
137/*
138 * Given an internal texture format enum or 1, 2, 3, 4 return the
139 * corresponding _base_ internal format: GL_ALPHA, GL_LUMINANCE,
Brian Paulc3f0a511999-11-03 17:27:05 +0000140 * GL_LUMANCE_ALPHA, GL_INTENSITY, GL_RGB, or GL_RGBA.
141 * Return -1 if invalid enum.
jtgafb833d1999-08-19 00:55:39 +0000142 */
Brian Paulb132e8d2000-03-23 16:23:14 +0000143GLint
Brian Paulaea66b12000-05-24 14:04:06 +0000144_mesa_base_tex_format( GLcontext *ctx, GLint format )
jtgafb833d1999-08-19 00:55:39 +0000145{
Brian Paul289d47e2000-08-29 23:31:23 +0000146 /*
147 * Ask the driver for the base format, if it doesn't
148 * know, it will return -1;
149 */
150 if (ctx->Driver.BaseCompressedTexFormat) {
151 GLint ifmt = (*ctx->Driver.BaseCompressedTexFormat)(ctx, format);
152 if (ifmt >= 0) {
153 return ifmt;
154 }
155 }
jtgafb833d1999-08-19 00:55:39 +0000156 switch (format) {
157 case GL_ALPHA:
158 case GL_ALPHA4:
159 case GL_ALPHA8:
160 case GL_ALPHA12:
161 case GL_ALPHA16:
162 return GL_ALPHA;
163 case 1:
164 case GL_LUMINANCE:
165 case GL_LUMINANCE4:
166 case GL_LUMINANCE8:
167 case GL_LUMINANCE12:
168 case GL_LUMINANCE16:
169 return GL_LUMINANCE;
170 case 2:
171 case GL_LUMINANCE_ALPHA:
172 case GL_LUMINANCE4_ALPHA4:
173 case GL_LUMINANCE6_ALPHA2:
174 case GL_LUMINANCE8_ALPHA8:
175 case GL_LUMINANCE12_ALPHA4:
176 case GL_LUMINANCE12_ALPHA12:
177 case GL_LUMINANCE16_ALPHA16:
178 return GL_LUMINANCE_ALPHA;
179 case GL_INTENSITY:
180 case GL_INTENSITY4:
181 case GL_INTENSITY8:
182 case GL_INTENSITY12:
183 case GL_INTENSITY16:
184 return GL_INTENSITY;
185 case 3:
186 case GL_RGB:
187 case GL_R3_G3_B2:
188 case GL_RGB4:
189 case GL_RGB5:
190 case GL_RGB8:
191 case GL_RGB10:
192 case GL_RGB12:
193 case GL_RGB16:
194 return GL_RGB;
195 case 4:
196 case GL_RGBA:
197 case GL_RGBA2:
198 case GL_RGBA4:
199 case GL_RGB5_A1:
200 case GL_RGBA8:
201 case GL_RGB10_A2:
202 case GL_RGBA12:
203 case GL_RGBA16:
204 return GL_RGBA;
205 case GL_COLOR_INDEX:
206 case GL_COLOR_INDEX1_EXT:
207 case GL_COLOR_INDEX2_EXT:
208 case GL_COLOR_INDEX4_EXT:
209 case GL_COLOR_INDEX8_EXT:
210 case GL_COLOR_INDEX12_EXT:
211 case GL_COLOR_INDEX16_EXT:
212 return GL_COLOR_INDEX;
Brian Paulf7e1dfe2001-02-17 00:15:39 +0000213 case GL_DEPTH_COMPONENT:
214 case GL_DEPTH_COMPONENT16_SGIX:
215 case GL_DEPTH_COMPONENT24_SGIX:
216 case GL_DEPTH_COMPONENT32_SGIX:
217 if (ctx->Extensions.SGIX_depth_texture)
218 return GL_DEPTH_COMPONENT;
219 else
220 return -1;
jtgafb833d1999-08-19 00:55:39 +0000221 default:
222 return -1; /* error */
223 }
224}
225
226
Brian Paulf7e1dfe2001-02-17 00:15:39 +0000227/*
228 * Test if the given image format is a color/rgba format. That is,
229 * not color index, depth, stencil, etc.
230 */
231static GLboolean
232is_color_format(GLenum format)
233{
234 switch (format) {
235 case GL_ALPHA:
236 case GL_ALPHA4:
237 case GL_ALPHA8:
238 case GL_ALPHA12:
239 case GL_ALPHA16:
240 case 1:
241 case GL_LUMINANCE:
242 case GL_LUMINANCE4:
243 case GL_LUMINANCE8:
244 case GL_LUMINANCE12:
245 case GL_LUMINANCE16:
246 case 2:
247 case GL_LUMINANCE_ALPHA:
248 case GL_LUMINANCE4_ALPHA4:
249 case GL_LUMINANCE6_ALPHA2:
250 case GL_LUMINANCE8_ALPHA8:
251 case GL_LUMINANCE12_ALPHA4:
252 case GL_LUMINANCE12_ALPHA12:
253 case GL_LUMINANCE16_ALPHA16:
254 case GL_INTENSITY:
255 case GL_INTENSITY4:
256 case GL_INTENSITY8:
257 case GL_INTENSITY12:
258 case GL_INTENSITY16:
259 case 3:
260 case GL_RGB:
261 case GL_R3_G3_B2:
262 case GL_RGB4:
263 case GL_RGB5:
264 case GL_RGB8:
265 case GL_RGB10:
266 case GL_RGB12:
267 case GL_RGB16:
268 case 4:
269 case GL_RGBA:
270 case GL_RGBA2:
271 case GL_RGBA4:
272 case GL_RGB5_A1:
273 case GL_RGBA8:
274 case GL_RGB10_A2:
275 case GL_RGBA12:
276 case GL_RGBA16:
277 return GL_TRUE;
278 default:
279 return GL_FALSE;
280 }
281}
282
283
284static GLboolean
285is_index_format(GLenum format)
286{
287 switch (format) {
288 case GL_COLOR_INDEX:
289 case GL_COLOR_INDEX1_EXT:
290 case GL_COLOR_INDEX2_EXT:
291 case GL_COLOR_INDEX4_EXT:
292 case GL_COLOR_INDEX8_EXT:
293 case GL_COLOR_INDEX12_EXT:
294 case GL_COLOR_INDEX16_EXT:
295 return GL_TRUE;
296 default:
297 return GL_FALSE;
298 }
299}
300
jtgafb833d1999-08-19 00:55:39 +0000301
302/*
Brian Paulaea66b12000-05-24 14:04:06 +0000303 * Return GL_TRUE if internalFormat is a compressed format, return GL_FALSE
304 * otherwise.
305 */
306static GLboolean
Brian Paul289d47e2000-08-29 23:31:23 +0000307is_compressed_format(GLcontext *ctx, GLenum internalFormat)
Brian Paulaea66b12000-05-24 14:04:06 +0000308{
Brian Paul289d47e2000-08-29 23:31:23 +0000309 if (ctx->Driver.IsCompressedFormat) {
310 return (*ctx->Driver.IsCompressedFormat)(ctx, internalFormat);
311 }
312 return GL_FALSE;
Brian Paulaea66b12000-05-24 14:04:06 +0000313}
314
315
jtgafb833d1999-08-19 00:55:39 +0000316
jtgafb833d1999-08-19 00:55:39 +0000317/*
Brian Paul8e39ad22001-02-06 21:42:48 +0000318 * Store a gl_texture_image pointer in a gl_texture_object structure
319 * according to the target and level parameters.
320 * This was basically prompted by the introduction of cube maps.
jtgafb833d1999-08-19 00:55:39 +0000321 */
Brian Paulfbd8f211999-11-11 01:22:25 +0000322static void
Brian Paulfc4b4432000-05-23 15:17:12 +0000323set_tex_image(struct gl_texture_object *tObj,
324 GLenum target, GLint level,
325 struct gl_texture_image *texImage)
326{
327 ASSERT(tObj);
328 ASSERT(texImage);
329 switch (target) {
330 case GL_TEXTURE_2D:
331 tObj->Image[level] = texImage;
332 return;
333 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
Brian Paul413d6a22000-05-26 14:44:59 +0000334 tObj->Image[level] = texImage;
Brian Paulfc4b4432000-05-23 15:17:12 +0000335 return;
336 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
337 tObj->NegX[level] = texImage;
338 return;
339 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
340 tObj->PosY[level] = texImage;
341 return;
342 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
343 tObj->NegY[level] = texImage;
344 return;
345 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
346 tObj->PosZ[level] = texImage;
347 return;
348 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
349 tObj->NegZ[level] = texImage;
350 return;
351 default:
352 gl_problem(NULL, "bad target in set_tex_image()");
353 return;
354 }
355}
356
357
Brian Paul8e39ad22001-02-06 21:42:48 +0000358
Brian Paul77ce6da2000-03-20 23:40:12 +0000359/*
360 * Return new gl_texture_image struct with all fields initialized to zero.
361 */
362struct gl_texture_image *
Brian Paul021a5252000-03-27 17:54:17 +0000363_mesa_alloc_texture_image( void )
Brian Paul77ce6da2000-03-20 23:40:12 +0000364{
365 return CALLOC_STRUCT(gl_texture_image);
366}
367
368
369
Brian Paul77ce6da2000-03-20 23:40:12 +0000370void
Brian Paul021a5252000-03-27 17:54:17 +0000371_mesa_free_texture_image( struct gl_texture_image *teximage )
Brian Paul77ce6da2000-03-20 23:40:12 +0000372{
373 if (teximage->Data) {
374 FREE( teximage->Data );
375 teximage->Data = NULL;
376 }
377 FREE( teximage );
378}
379
380
Brian Paulfc4b4432000-05-23 15:17:12 +0000381/*
Brian Paul8e39ad22001-02-06 21:42:48 +0000382 * Return GL_TRUE if the target is a proxy target.
Brian Paulaea66b12000-05-24 14:04:06 +0000383 */
Brian Paul8e39ad22001-02-06 21:42:48 +0000384static GLboolean
385is_proxy_target(GLenum target)
Brian Paulaea66b12000-05-24 14:04:06 +0000386{
Brian Paul8e39ad22001-02-06 21:42:48 +0000387 return (target == GL_PROXY_TEXTURE_1D ||
388 target == GL_PROXY_TEXTURE_2D ||
389 target == GL_PROXY_TEXTURE_3D ||
390 target == GL_PROXY_TEXTURE_CUBE_MAP_ARB);
Brian Paulaea66b12000-05-24 14:04:06 +0000391}
392
393
Brian Paulaea66b12000-05-24 14:04:06 +0000394/*
Brian Paul35d53012000-05-23 17:14:49 +0000395 * Given a texture unit and a texture target, return the corresponding
396 * texture object.
397 */
398struct gl_texture_object *
Brian Paul01e54752000-09-05 15:40:34 +0000399_mesa_select_tex_object(GLcontext *ctx, const struct gl_texture_unit *texUnit,
Brian Paul35d53012000-05-23 17:14:49 +0000400 GLenum target)
401{
402 switch (target) {
403 case GL_TEXTURE_1D:
Brian Paula8523782000-11-19 23:10:25 +0000404 return texUnit->Current1D;
Brian Paul35d53012000-05-23 17:14:49 +0000405 case GL_PROXY_TEXTURE_1D:
406 return ctx->Texture.Proxy1D;
407 case GL_TEXTURE_2D:
Brian Paula8523782000-11-19 23:10:25 +0000408 return texUnit->Current2D;
Brian Paul35d53012000-05-23 17:14:49 +0000409 case GL_PROXY_TEXTURE_2D:
410 return ctx->Texture.Proxy2D;
411 case GL_TEXTURE_3D:
Brian Paula8523782000-11-19 23:10:25 +0000412 return texUnit->Current3D;
Brian Paul35d53012000-05-23 17:14:49 +0000413 case GL_PROXY_TEXTURE_3D:
414 return ctx->Texture.Proxy3D;
415 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
416 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
417 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
418 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
419 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
420 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
Keith Whitwella96308c2000-10-30 13:31:59 +0000421 return ctx->Extensions.ARB_texture_cube_map
Brian Paul35d53012000-05-23 17:14:49 +0000422 ? texUnit->CurrentCubeMap : NULL;
423 case GL_PROXY_TEXTURE_CUBE_MAP_ARB:
Keith Whitwella96308c2000-10-30 13:31:59 +0000424 return ctx->Extensions.ARB_texture_cube_map
Brian Paul35d53012000-05-23 17:14:49 +0000425 ? ctx->Texture.ProxyCubeMap : NULL;
426 default:
427 gl_problem(NULL, "bad target in _mesa_select_tex_object()");
428 return NULL;
429 }
430}
431
432
433/*
Brian Paulfc4b4432000-05-23 15:17:12 +0000434 * Return the texture image struct which corresponds to target and level
435 * for the given texture unit.
436 */
437struct gl_texture_image *
438_mesa_select_tex_image(GLcontext *ctx, const struct gl_texture_unit *texUnit,
439 GLenum target, GLint level)
440{
441 ASSERT(texUnit);
442 switch (target) {
443 case GL_TEXTURE_1D:
Brian Paula8523782000-11-19 23:10:25 +0000444 return texUnit->Current1D->Image[level];
Brian Paulfc4b4432000-05-23 15:17:12 +0000445 case GL_PROXY_TEXTURE_1D:
446 return ctx->Texture.Proxy1D->Image[level];
447 case GL_TEXTURE_2D:
Brian Paula8523782000-11-19 23:10:25 +0000448 return texUnit->Current2D->Image[level];
Brian Paulfc4b4432000-05-23 15:17:12 +0000449 case GL_PROXY_TEXTURE_2D:
450 return ctx->Texture.Proxy2D->Image[level];
451 case GL_TEXTURE_3D:
Brian Paula8523782000-11-19 23:10:25 +0000452 return texUnit->Current3D->Image[level];
Brian Paulfc4b4432000-05-23 15:17:12 +0000453 case GL_PROXY_TEXTURE_3D:
454 return ctx->Texture.Proxy3D->Image[level];
455 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
Keith Whitwella96308c2000-10-30 13:31:59 +0000456 if (ctx->Extensions.ARB_texture_cube_map)
Brian Paul413d6a22000-05-26 14:44:59 +0000457 return texUnit->CurrentCubeMap->Image[level];
Brian Paulfc4b4432000-05-23 15:17:12 +0000458 else
459 return NULL;
460 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
Keith Whitwella96308c2000-10-30 13:31:59 +0000461 if (ctx->Extensions.ARB_texture_cube_map)
Brian Paulfc4b4432000-05-23 15:17:12 +0000462 return texUnit->CurrentCubeMap->NegX[level];
463 else
464 return NULL;
465 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
Keith Whitwella96308c2000-10-30 13:31:59 +0000466 if (ctx->Extensions.ARB_texture_cube_map)
Brian Paulfc4b4432000-05-23 15:17:12 +0000467 return texUnit->CurrentCubeMap->PosY[level];
468 else
469 return NULL;
470 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
Keith Whitwella96308c2000-10-30 13:31:59 +0000471 if (ctx->Extensions.ARB_texture_cube_map)
Brian Paulfc4b4432000-05-23 15:17:12 +0000472 return texUnit->CurrentCubeMap->NegY[level];
473 else
474 return NULL;
475 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
Keith Whitwella96308c2000-10-30 13:31:59 +0000476 if (ctx->Extensions.ARB_texture_cube_map)
Brian Paulfc4b4432000-05-23 15:17:12 +0000477 return texUnit->CurrentCubeMap->PosZ[level];
478 else
479 return NULL;
480 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
Keith Whitwella96308c2000-10-30 13:31:59 +0000481 if (ctx->Extensions.ARB_texture_cube_map)
Brian Paulfc4b4432000-05-23 15:17:12 +0000482 return texUnit->CurrentCubeMap->NegZ[level];
483 else
484 return NULL;
485 case GL_PROXY_TEXTURE_CUBE_MAP_ARB:
Keith Whitwella96308c2000-10-30 13:31:59 +0000486 if (ctx->Extensions.ARB_texture_cube_map)
Brian Paul413d6a22000-05-26 14:44:59 +0000487 return ctx->Texture.ProxyCubeMap->Image[level];
Brian Paulfc4b4432000-05-23 15:17:12 +0000488 else
489 return NULL;
490 default:
491 gl_problem(ctx, "bad target in _mesa_select_tex_image()");
492 return NULL;
493 }
494}
495
496
497
Brian Paulf93b3dd2000-08-30 18:22:28 +0000498/*
jtgafb833d1999-08-19 00:55:39 +0000499 * glTexImage[123]D can accept a NULL image pointer. In this case we
500 * create a texture image with unspecified image contents per the OpenGL
Brian Paul8e39ad22001-02-06 21:42:48 +0000501 * spec.
jtgafb833d1999-08-19 00:55:39 +0000502 */
Brian Paul8e39ad22001-02-06 21:42:48 +0000503static GLubyte *
504make_null_texture(GLint width, GLint height, GLint depth, GLenum format)
jtgafb833d1999-08-19 00:55:39 +0000505{
Brian Paul8e39ad22001-02-06 21:42:48 +0000506 const GLint components = _mesa_components_in_format(format);
507 const GLint numPixels = width * height * depth;
508 GLubyte *data = (GLubyte *) MALLOC(numPixels * components * sizeof(GLubyte));
jtgafb833d1999-08-19 00:55:39 +0000509
510 /*
511 * Let's see if anyone finds this. If glTexImage2D() is called with
512 * a NULL image pointer then load the texture image with something
513 * interesting instead of leaving it indeterminate.
514 */
Brian Paul8e39ad22001-02-06 21:42:48 +0000515 if (data) {
Brian Paul65d54602000-03-01 23:28:20 +0000516 static const char message[8][32] = {
jtgafb833d1999-08-19 00:55:39 +0000517 " X X XXXXX XXX X ",
518 " XX XX X X X X X ",
519 " X X X X X X X ",
520 " X X XXXX XXX XXXXX ",
521 " X X X X X X ",
522 " X X X X X X X ",
523 " X X XXXXX XXX X X ",
524 " "
525 };
526
Brian Paul8e39ad22001-02-06 21:42:48 +0000527 GLubyte *imgPtr = data;
528 GLint h, i, j, k;
529 for (h = 0; h < depth; h++) {
530 for (i = 0; i < height; i++) {
531 GLint srcRow = 7 - (i % 8);
532 for (j = 0; j < width; j++) {
533 GLint srcCol = j % 32;
534 GLubyte texel = (message[srcRow][srcCol]=='X') ? 255 : 70;
535 for (k = 0; k < components; k++) {
536 *imgPtr++ = texel;
537 }
jtgafb833d1999-08-19 00:55:39 +0000538 }
539 }
540 }
541 }
Brian Paul8e39ad22001-02-06 21:42:48 +0000542
543 return data;
jtgafb833d1999-08-19 00:55:39 +0000544}
545
546
547
548/*
Brian Paulf378ab82001-02-06 23:35:26 +0000549 * Reset the fields of a gl_texture_image struct to zero.
Brian Paul9c272782000-09-05 22:04:30 +0000550 * This is called when a proxy texture test fails, we set all the
551 * image members (except DriverData) to zero.
Brian Paulf378ab82001-02-06 23:35:26 +0000552 * It's also used in glTexImage[123]D as a safeguard to be sure all
553 * required fields get initialized properly by the Driver.TexImage[123]D
554 * functions.
Brian Paul9c272782000-09-05 22:04:30 +0000555 */
556static void
Brian Paulf378ab82001-02-06 23:35:26 +0000557clear_teximage_fields(struct gl_texture_image *img)
Brian Paul9c272782000-09-05 22:04:30 +0000558{
559 ASSERT(img);
560 img->Format = 0;
Brian Paulf7e1dfe2001-02-17 00:15:39 +0000561 img->Type = 0;
Brian Paul9c272782000-09-05 22:04:30 +0000562 img->IntFormat = 0;
563 img->RedBits = 0;
564 img->GreenBits = 0;
565 img->BlueBits = 0;
566 img->AlphaBits = 0;
567 img->IntensityBits = 0;
568 img->LuminanceBits = 0;
569 img->IndexBits = 0;
570 img->Border = 0;
571 img->Width = 0;
572 img->Height = 0;
573 img->Depth = 0;
574 img->Width2 = 0;
575 img->Height2 = 0;
576 img->Depth2 = 0;
577 img->WidthLog2 = 0;
578 img->HeightLog2 = 0;
579 img->DepthLog2 = 0;
580 img->Data = NULL;
581 img->IsCompressed = 0;
582 img->CompressedSize = 0;
Brian Paulf378ab82001-02-06 23:35:26 +0000583 img->FetchTexel = NULL;
Brian Paul9c272782000-09-05 22:04:30 +0000584}
585
586
Brian Paul6628bc92001-02-07 03:27:41 +0000587/*
588 * Initialize basic fields of the gl_texture_image struct.
589 */
590static void
591init_teximage_fields(GLcontext *ctx,
592 struct gl_texture_image *img,
593 GLsizei width, GLsizei height, GLsizei depth,
594 GLint border, GLenum internalFormat)
595{
596 ASSERT(img);
597
598 img->IntFormat = internalFormat;
599 img->Border = border;
600 img->Width = width;
601 img->Height = height;
602 img->Depth = depth;
603 img->WidthLog2 = logbase2(width - 2 * border);
604 if (height == 1) /* 1-D texture */
605 img->HeightLog2 = 0;
606 else
607 img->HeightLog2 = logbase2(height - 2 * border);
608 if (depth == 1) /* 2-D texture */
609 img->DepthLog2 = 0;
610 else
611 img->DepthLog2 = logbase2(depth - 2 * border);
612 img->Width2 = 1 << img->WidthLog2;
613 img->Height2 = 1 << img->HeightLog2;
614 img->Depth2 = 1 << img->DepthLog2;
615 img->MaxLog2 = MAX2(img->WidthLog2, img->HeightLog2);
616 img->IsCompressed = is_compressed_format(ctx, internalFormat);
617}
618
619
Brian Paul9c272782000-09-05 22:04:30 +0000620
621/*
Brian Paulc3f0a511999-11-03 17:27:05 +0000622 * Test glTexImage[123]D() parameters for errors.
jtgafb833d1999-08-19 00:55:39 +0000623 * Input:
624 * dimensions - must be 1 or 2 or 3
625 * Return: GL_TRUE = an error was detected, GL_FALSE = no errors
626 */
Brian Paulc3f0a511999-11-03 17:27:05 +0000627static GLboolean
628texture_error_check( GLcontext *ctx, GLenum target,
629 GLint level, GLint internalFormat,
630 GLenum format, GLenum type,
Brian Paul5b37c321999-11-05 06:43:10 +0000631 GLuint dimensions,
Brian Paulc3f0a511999-11-03 17:27:05 +0000632 GLint width, GLint height,
633 GLint depth, GLint border )
jtgafb833d1999-08-19 00:55:39 +0000634{
635 GLboolean isProxy;
636 GLint iformat;
637
638 if (dimensions == 1) {
Brian Paul5b37c321999-11-05 06:43:10 +0000639 isProxy = (GLboolean) (target == GL_PROXY_TEXTURE_1D);
jtgafb833d1999-08-19 00:55:39 +0000640 if (target != GL_TEXTURE_1D && !isProxy) {
641 gl_error( ctx, GL_INVALID_ENUM, "glTexImage1D(target)" );
642 return GL_TRUE;
643 }
644 }
645 else if (dimensions == 2) {
Brian Paul8e39ad22001-02-06 21:42:48 +0000646 isProxy = (GLboolean) (target == GL_PROXY_TEXTURE_2D ||
647 target == GL_PROXY_TEXTURE_CUBE_MAP_ARB);
Brian Paul413d6a22000-05-26 14:44:59 +0000648 if (target != GL_TEXTURE_2D && !isProxy &&
Keith Whitwella96308c2000-10-30 13:31:59 +0000649 !(ctx->Extensions.ARB_texture_cube_map &&
Brian Paul413d6a22000-05-26 14:44:59 +0000650 target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
651 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB)) {
jtgafb833d1999-08-19 00:55:39 +0000652 gl_error( ctx, GL_INVALID_ENUM, "glTexImage2D(target)" );
653 return GL_TRUE;
654 }
655 }
656 else if (dimensions == 3) {
Brian Paul5b37c321999-11-05 06:43:10 +0000657 isProxy = (GLboolean) (target == GL_PROXY_TEXTURE_3D);
jtgafb833d1999-08-19 00:55:39 +0000658 if (target != GL_TEXTURE_3D && !isProxy) {
659 gl_error( ctx, GL_INVALID_ENUM, "glTexImage3D(target)" );
660 return GL_TRUE;
661 }
662 }
663 else {
664 gl_problem( ctx, "bad dims in texture_error_check" );
665 return GL_TRUE;
666 }
667
668 /* Border */
Brian Paul9fd2b0a2000-03-24 23:59:06 +0000669 if (border != 0 && border != 1) {
jtgafb833d1999-08-19 00:55:39 +0000670 if (!isProxy) {
Brian Paulc3f0a511999-11-03 17:27:05 +0000671 char message[100];
Brian Paulec153982000-12-08 18:09:33 +0000672 sprintf(message, "glTexImage%dD(border=%d)", dimensions, border);
Brian Paulc3f0a511999-11-03 17:27:05 +0000673 gl_error(ctx, GL_INVALID_VALUE, message);
jtgafb833d1999-08-19 00:55:39 +0000674 }
675 return GL_TRUE;
676 }
677
678 /* Width */
679 if (width < 2 * border || width > 2 + ctx->Const.MaxTextureSize
680 || logbase2( width - 2 * border ) < 0) {
681 if (!isProxy) {
Brian Paulc3f0a511999-11-03 17:27:05 +0000682 char message[100];
Brian Paulec153982000-12-08 18:09:33 +0000683 sprintf(message, "glTexImage%dD(width=%d)", dimensions, width);
Brian Paulc3f0a511999-11-03 17:27:05 +0000684 gl_error(ctx, GL_INVALID_VALUE, message);
jtgafb833d1999-08-19 00:55:39 +0000685 }
686 return GL_TRUE;
687 }
688
689 /* Height */
690 if (dimensions >= 2) {
691 if (height < 2 * border || height > 2 + ctx->Const.MaxTextureSize
692 || logbase2( height - 2 * border ) < 0) {
693 if (!isProxy) {
Brian Paulc3f0a511999-11-03 17:27:05 +0000694 char message[100];
Brian Paulec153982000-12-08 18:09:33 +0000695 sprintf(message, "glTexImage%dD(height=%d)", dimensions, height);
Brian Paulc3f0a511999-11-03 17:27:05 +0000696 gl_error(ctx, GL_INVALID_VALUE, message);
jtgafb833d1999-08-19 00:55:39 +0000697 }
Brian Paulc3f0a511999-11-03 17:27:05 +0000698 return GL_TRUE;
jtgafb833d1999-08-19 00:55:39 +0000699 }
700 }
701
Brian Paulad817702000-05-30 00:27:24 +0000702 /* For cube map, width must equal height */
703 if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
704 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB) {
705 if (width != height) {
706 if (!isProxy) {
707 gl_error(ctx, GL_INVALID_VALUE, "glTexImage2D(width != height)");
708 }
709 return GL_TRUE;
710 }
711 }
712
jtgafb833d1999-08-19 00:55:39 +0000713 /* Depth */
714 if (dimensions >= 3) {
715 if (depth < 2 * border || depth > 2 + ctx->Const.MaxTextureSize
716 || logbase2( depth - 2 * border ) < 0) {
717 if (!isProxy) {
Brian Paulec153982000-12-08 18:09:33 +0000718 char message[100];
719 sprintf(message, "glTexImage3D(depth=%d)", depth );
720 gl_error( ctx, GL_INVALID_VALUE, message );
jtgafb833d1999-08-19 00:55:39 +0000721 }
722 return GL_TRUE;
723 }
724 }
725
726 /* Level */
Brian Paul9fd2b0a2000-03-24 23:59:06 +0000727 if (level < 0 || level >= ctx->Const.MaxTextureLevels) {
Brian Paulc3f0a511999-11-03 17:27:05 +0000728 if (!isProxy) {
729 char message[100];
Brian Paulec153982000-12-08 18:09:33 +0000730 sprintf(message, "glTexImage%dD(level=%d)", dimensions, level);
Brian Paulc3f0a511999-11-03 17:27:05 +0000731 gl_error(ctx, GL_INVALID_VALUE, message);
732 }
jtgafb833d1999-08-19 00:55:39 +0000733 return GL_TRUE;
734 }
735
Brian Paulaea66b12000-05-24 14:04:06 +0000736 iformat = _mesa_base_tex_format( ctx, internalFormat );
jtgafb833d1999-08-19 00:55:39 +0000737 if (iformat < 0) {
Brian Paulc3f0a511999-11-03 17:27:05 +0000738 if (!isProxy) {
739 char message[100];
Brian Paulec153982000-12-08 18:09:33 +0000740 sprintf(message, "glTexImage%dD(internalFormat=0x%x)", dimensions,
741 internalFormat);
Brian Paulc3f0a511999-11-03 17:27:05 +0000742 gl_error(ctx, GL_INVALID_VALUE, message);
743 }
jtgafb833d1999-08-19 00:55:39 +0000744 return GL_TRUE;
745 }
746
Brian Paul289d47e2000-08-29 23:31:23 +0000747 if (!is_compressed_format(ctx, internalFormat)) {
Brian Paulaea66b12000-05-24 14:04:06 +0000748 if (!_mesa_is_legal_format_and_type( format, type )) {
749 /* Yes, generate GL_INVALID_OPERATION, not GL_INVALID_ENUM, if there
750 * is a type/format mismatch. See 1.2 spec page 94, sec 3.6.4.
751 */
752 if (!isProxy) {
753 char message[100];
754 sprintf(message, "glTexImage%dD(format or type)", dimensions);
755 gl_error(ctx, GL_INVALID_OPERATION, message);
756 }
757 return GL_TRUE;
Brian Paulc3f0a511999-11-03 17:27:05 +0000758 }
jtgafb833d1999-08-19 00:55:39 +0000759 }
760
761 /* if we get here, the parameters are OK */
762 return GL_FALSE;
763}
764
765
766
767/*
Brian Paulc3f0a511999-11-03 17:27:05 +0000768 * Test glTexSubImage[123]D() parameters for errors.
769 * Input:
770 * dimensions - must be 1 or 2 or 3
771 * Return: GL_TRUE = an error was detected, GL_FALSE = no errors
772 */
773static GLboolean
Brian Paulfbd8f211999-11-11 01:22:25 +0000774subtexture_error_check( GLcontext *ctx, GLuint dimensions,
Brian Paulc3f0a511999-11-03 17:27:05 +0000775 GLenum target, GLint level,
776 GLint xoffset, GLint yoffset, GLint zoffset,
777 GLint width, GLint height, GLint depth,
778 GLenum format, GLenum type )
779{
780 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
781 struct gl_texture_image *destTex;
782
783 if (dimensions == 1) {
784 if (target != GL_TEXTURE_1D) {
785 gl_error( ctx, GL_INVALID_ENUM, "glTexSubImage1D(target)" );
786 return GL_TRUE;
787 }
788 }
789 else if (dimensions == 2) {
Keith Whitwella96308c2000-10-30 13:31:59 +0000790 if (ctx->Extensions.ARB_texture_cube_map) {
Brian Paulfc4b4432000-05-23 15:17:12 +0000791 if ((target < GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB ||
792 target > GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB) &&
793 target != GL_TEXTURE_2D) {
794 gl_error( ctx, GL_INVALID_ENUM, "glTexSubImage2D(target)" );
795 return GL_TRUE;
796 }
797 }
798 else if (target != GL_TEXTURE_2D) {
Brian Paulc3f0a511999-11-03 17:27:05 +0000799 gl_error( ctx, GL_INVALID_ENUM, "glTexSubImage2D(target)" );
800 return GL_TRUE;
801 }
802 }
803 else if (dimensions == 3) {
804 if (target != GL_TEXTURE_3D) {
805 gl_error( ctx, GL_INVALID_ENUM, "glTexSubImage3D(target)" );
806 return GL_TRUE;
807 }
808 }
809 else {
810 gl_problem( ctx, "bad dims in texture_error_check" );
811 return GL_TRUE;
812 }
813
814 if (level < 0 || level >= ctx->Const.MaxTextureLevels) {
Brian Paulec153982000-12-08 18:09:33 +0000815 char message[100];
816 sprintf(message, "glTexSubImage2D(level=%d)", level);
817 gl_error(ctx, GL_INVALID_ENUM, message);
Brian Paulc3f0a511999-11-03 17:27:05 +0000818 return GL_TRUE;
819 }
820
821 if (width < 0) {
822 char message[100];
Brian Paulec153982000-12-08 18:09:33 +0000823 sprintf(message, "glTexSubImage%dD(width=%d)", dimensions, width);
Brian Paulc3f0a511999-11-03 17:27:05 +0000824 gl_error(ctx, GL_INVALID_VALUE, message);
825 return GL_TRUE;
826 }
827 if (height < 0 && dimensions > 1) {
828 char message[100];
Brian Paulec153982000-12-08 18:09:33 +0000829 sprintf(message, "glTexSubImage%dD(height=%d)", dimensions, height);
Brian Paulc3f0a511999-11-03 17:27:05 +0000830 gl_error(ctx, GL_INVALID_VALUE, message);
831 return GL_TRUE;
832 }
833 if (depth < 0 && dimensions > 2) {
834 char message[100];
Brian Paulec153982000-12-08 18:09:33 +0000835 sprintf(message, "glTexSubImage%dD(depth=%d)", dimensions, depth);
Brian Paulc3f0a511999-11-03 17:27:05 +0000836 gl_error(ctx, GL_INVALID_VALUE, message);
837 return GL_TRUE;
838 }
839
Brian Paulf2718b02001-01-23 23:35:23 +0000840 destTex = _mesa_select_tex_image(ctx, texUnit, target, level);
841
Brian Paulc3f0a511999-11-03 17:27:05 +0000842 if (!destTex) {
843 gl_error(ctx, GL_INVALID_OPERATION, "glTexSubImage2D");
844 return GL_TRUE;
845 }
846
847 if (xoffset < -((GLint)destTex->Border)) {
848 gl_error(ctx, GL_INVALID_VALUE, "glTexSubImage1/2/3D(xoffset)");
849 return GL_TRUE;
850 }
851 if (xoffset + width > (GLint) (destTex->Width + destTex->Border)) {
852 gl_error(ctx, GL_INVALID_VALUE, "glTexSubImage1/2/3D(xoffset+width)");
853 return GL_TRUE;
854 }
855 if (dimensions > 1) {
856 if (yoffset < -((GLint)destTex->Border)) {
857 gl_error(ctx, GL_INVALID_VALUE, "glTexSubImage2/3D(yoffset)");
858 return GL_TRUE;
859 }
860 if (yoffset + height > (GLint) (destTex->Height + destTex->Border)) {
861 gl_error(ctx, GL_INVALID_VALUE, "glTexSubImage2/3D(yoffset+height)");
862 return GL_TRUE;
863 }
864 }
865 if (dimensions > 2) {
866 if (zoffset < -((GLint)destTex->Border)) {
867 gl_error(ctx, GL_INVALID_VALUE, "glTexSubImage3D(zoffset)");
868 return GL_TRUE;
869 }
870 if (zoffset + depth > (GLint) (destTex->Depth+destTex->Border)) {
871 gl_error(ctx, GL_INVALID_VALUE, "glTexSubImage3D(zoffset+depth)");
872 return GL_TRUE;
873 }
874 }
875
Brian Paul289d47e2000-08-29 23:31:23 +0000876 if (!is_compressed_format(ctx, destTex->IntFormat)) {
Brian Paul9540a1d2000-06-06 17:03:38 +0000877 if (!_mesa_is_legal_format_and_type(format, type)) {
878 char message[100];
879 sprintf(message, "glTexSubImage%dD(format or type)", dimensions);
880 gl_error(ctx, GL_INVALID_ENUM, message);
881 return GL_TRUE;
882 }
Brian Paulc3f0a511999-11-03 17:27:05 +0000883 }
884
885 return GL_FALSE;
886}
887
888
889/*
890 * Test glCopyTexImage[12]D() parameters for errors.
891 * Input: dimensions - must be 1 or 2 or 3
892 * Return: GL_TRUE = an error was detected, GL_FALSE = no errors
893 */
894static GLboolean
Brian Paulfbd8f211999-11-11 01:22:25 +0000895copytexture_error_check( GLcontext *ctx, GLuint dimensions,
Brian Paulc3f0a511999-11-03 17:27:05 +0000896 GLenum target, GLint level, GLint internalFormat,
897 GLint width, GLint height, GLint border )
898{
899 GLint iformat;
900
Brian Paulfc4b4432000-05-23 15:17:12 +0000901 if (dimensions == 1) {
902 if (target != GL_TEXTURE_1D) {
903 gl_error( ctx, GL_INVALID_ENUM, "glCopyTexImage1D(target)" );
904 return GL_TRUE;
905 }
Brian Paulc3f0a511999-11-03 17:27:05 +0000906 }
Brian Paulfc4b4432000-05-23 15:17:12 +0000907 else if (dimensions == 2) {
Keith Whitwella96308c2000-10-30 13:31:59 +0000908 if (ctx->Extensions.ARB_texture_cube_map) {
Brian Paulfc4b4432000-05-23 15:17:12 +0000909 if ((target < GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB ||
910 target > GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB) &&
911 target != GL_TEXTURE_2D) {
912 gl_error( ctx, GL_INVALID_ENUM, "glCopyTexImage2D(target)" );
913 return GL_TRUE;
914 }
915 }
916 else if (target != GL_TEXTURE_2D) {
917 gl_error( ctx, GL_INVALID_ENUM, "glCopyTexImage2D(target)" );
918 return GL_TRUE;
919 }
Brian Paulc3f0a511999-11-03 17:27:05 +0000920 }
921
922 /* Border */
923 if (border!=0 && border!=1) {
924 char message[100];
925 sprintf(message, "glCopyTexImage%dD(border)", dimensions);
926 gl_error(ctx, GL_INVALID_VALUE, message);
927 return GL_TRUE;
928 }
929
930 /* Width */
931 if (width < 2 * border || width > 2 + ctx->Const.MaxTextureSize
932 || logbase2( width - 2 * border ) < 0) {
933 char message[100];
Brian Paulec153982000-12-08 18:09:33 +0000934 sprintf(message, "glCopyTexImage%dD(width=%d)", dimensions, width);
Brian Paulc3f0a511999-11-03 17:27:05 +0000935 gl_error(ctx, GL_INVALID_VALUE, message);
936 return GL_TRUE;
937 }
938
939 /* Height */
940 if (dimensions >= 2) {
941 if (height < 2 * border || height > 2 + ctx->Const.MaxTextureSize
942 || logbase2( height - 2 * border ) < 0) {
943 char message[100];
Brian Paulec153982000-12-08 18:09:33 +0000944 sprintf(message, "glCopyTexImage%dD(height=%d)", dimensions, height);
Brian Paulc3f0a511999-11-03 17:27:05 +0000945 gl_error(ctx, GL_INVALID_VALUE, message);
946 return GL_TRUE;
947 }
948 }
949
Brian Paulad817702000-05-30 00:27:24 +0000950 /* For cube map, width must equal height */
951 if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
952 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB) {
953 if (width != height) {
954 gl_error(ctx, GL_INVALID_VALUE, "glCopyTexImage2D(width != height)");
955 return GL_TRUE;
956 }
957 }
958
Brian Paulc3f0a511999-11-03 17:27:05 +0000959 /* Level */
960 if (level<0 || level>=ctx->Const.MaxTextureLevels) {
961 char message[100];
Brian Paulec153982000-12-08 18:09:33 +0000962 sprintf(message, "glCopyTexImage%dD(level=%d)", dimensions, level);
Brian Paulc3f0a511999-11-03 17:27:05 +0000963 gl_error(ctx, GL_INVALID_VALUE, message);
964 return GL_TRUE;
965 }
966
Brian Paulaea66b12000-05-24 14:04:06 +0000967 iformat = _mesa_base_tex_format( ctx, internalFormat );
Brian Paulc3f0a511999-11-03 17:27:05 +0000968 if (iformat < 0) {
969 char message[100];
970 sprintf(message, "glCopyTexImage%dD(internalFormat)", dimensions);
971 gl_error(ctx, GL_INVALID_VALUE, message);
972 return GL_TRUE;
973 }
974
975 /* if we get here, the parameters are OK */
976 return GL_FALSE;
977}
978
979
980static GLboolean
Brian Paulfbd8f211999-11-11 01:22:25 +0000981copytexsubimage_error_check( GLcontext *ctx, GLuint dimensions,
Brian Paulc3f0a511999-11-03 17:27:05 +0000982 GLenum target, GLint level,
983 GLint xoffset, GLint yoffset, GLint zoffset,
Brian Paul5b37c321999-11-05 06:43:10 +0000984 GLsizei width, GLsizei height )
Brian Paulc3f0a511999-11-03 17:27:05 +0000985{
986 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
987 struct gl_texture_image *teximage;
988
Brian Paulfc4b4432000-05-23 15:17:12 +0000989 if (dimensions == 1) {
990 if (target != GL_TEXTURE_1D) {
991 gl_error( ctx, GL_INVALID_ENUM, "glCopyTexSubImage1D(target)" );
992 return GL_TRUE;
993 }
Brian Paulc3f0a511999-11-03 17:27:05 +0000994 }
Brian Paulfc4b4432000-05-23 15:17:12 +0000995 else if (dimensions == 2) {
Keith Whitwella96308c2000-10-30 13:31:59 +0000996 if (ctx->Extensions.ARB_texture_cube_map) {
Brian Paulfc4b4432000-05-23 15:17:12 +0000997 if ((target < GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB ||
998 target > GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB) &&
999 target != GL_TEXTURE_2D) {
1000 gl_error( ctx, GL_INVALID_ENUM, "glCopyTexSubImage2D(target)" );
1001 return GL_TRUE;
1002 }
1003 }
1004 else if (target != GL_TEXTURE_2D) {
1005 gl_error( ctx, GL_INVALID_ENUM, "glCopyTexSubImage2D(target)" );
1006 return GL_TRUE;
1007 }
Brian Paulc3f0a511999-11-03 17:27:05 +00001008 }
Brian Paulfc4b4432000-05-23 15:17:12 +00001009 else if (dimensions == 3) {
1010 if (target != GL_TEXTURE_3D) {
1011 gl_error( ctx, GL_INVALID_ENUM, "glCopyTexSubImage3D(target)" );
1012 return GL_TRUE;
1013 }
Brian Paulc3f0a511999-11-03 17:27:05 +00001014 }
1015
1016 if (level < 0 || level >= ctx->Const.MaxTextureLevels) {
1017 char message[100];
Brian Paulec153982000-12-08 18:09:33 +00001018 sprintf(message, "glCopyTexSubImage%dD(level=%d)", dimensions, level);
Brian Paulc3f0a511999-11-03 17:27:05 +00001019 gl_error(ctx, GL_INVALID_VALUE, message);
1020 return GL_TRUE;
1021 }
1022
1023 if (width < 0) {
1024 char message[100];
Brian Paulec153982000-12-08 18:09:33 +00001025 sprintf(message, "glCopyTexSubImage%dD(width=%d)", dimensions, width);
Brian Paulc3f0a511999-11-03 17:27:05 +00001026 gl_error(ctx, GL_INVALID_VALUE, message);
1027 return GL_TRUE;
1028 }
1029 if (dimensions > 1 && height < 0) {
1030 char message[100];
Brian Paulec153982000-12-08 18:09:33 +00001031 sprintf(message, "glCopyTexSubImage%dD(height=%d)", dimensions, height);
Brian Paulc3f0a511999-11-03 17:27:05 +00001032 gl_error(ctx, GL_INVALID_VALUE, message);
1033 return GL_TRUE;
1034 }
1035
Brian Paula8523782000-11-19 23:10:25 +00001036 teximage = _mesa_select_tex_image(ctx, texUnit, target, level);
Brian Paulc3f0a511999-11-03 17:27:05 +00001037 if (!teximage) {
1038 char message[100];
1039 sprintf(message, "glCopyTexSubImage%dD(undefined texture)", dimensions);
1040 gl_error(ctx, GL_INVALID_OPERATION, message);
1041 return GL_TRUE;
1042 }
1043
1044 if (xoffset < -((GLint)teximage->Border)) {
1045 char message[100];
Brian Paulec153982000-12-08 18:09:33 +00001046 sprintf(message, "glCopyTexSubImage%dD(xoffset=%d)", dimensions, xoffset);
Brian Paulc3f0a511999-11-03 17:27:05 +00001047 gl_error(ctx, GL_INVALID_VALUE, message);
1048 return GL_TRUE;
1049 }
1050 if (xoffset+width > (GLint) (teximage->Width+teximage->Border)) {
1051 char message[100];
1052 sprintf(message, "glCopyTexSubImage%dD(xoffset+width)", dimensions);
1053 gl_error(ctx, GL_INVALID_VALUE, message);
1054 return GL_TRUE;
1055 }
1056 if (dimensions > 1) {
1057 if (yoffset < -((GLint)teximage->Border)) {
1058 char message[100];
Brian Paulec153982000-12-08 18:09:33 +00001059 sprintf(message, "glCopyTexSubImage%dD(yoffset=%d)", dimensions, yoffset);
Brian Paulc3f0a511999-11-03 17:27:05 +00001060 gl_error(ctx, GL_INVALID_VALUE, message);
1061 return GL_TRUE;
1062 }
1063 /* NOTE: we're adding the border here, not subtracting! */
1064 if (yoffset+height > (GLint) (teximage->Height+teximage->Border)) {
1065 char message[100];
1066 sprintf(message, "glCopyTexSubImage%dD(yoffset+height)", dimensions);
1067 gl_error(ctx, GL_INVALID_VALUE, message);
1068 return GL_TRUE;
1069 }
1070 }
1071
1072 if (dimensions > 2) {
1073 if (zoffset < -((GLint)teximage->Border)) {
1074 char message[100];
1075 sprintf(message, "glCopyTexSubImage%dD(zoffset)", dimensions);
1076 gl_error(ctx, GL_INVALID_VALUE, message);
1077 return GL_TRUE;
1078 }
1079 if (zoffset > (GLint) (teximage->Depth+teximage->Border)) {
1080 char message[100];
1081 sprintf(message, "glCopyTexSubImage%dD(zoffset+depth)", dimensions);
1082 gl_error(ctx, GL_INVALID_VALUE, message);
1083 return GL_TRUE;
1084 }
1085 }
1086
1087 /* if we get here, the parameters are OK */
1088 return GL_FALSE;
1089}
1090
1091
1092
Brian Paulfbd8f211999-11-11 01:22:25 +00001093void
1094_mesa_GetTexImage( GLenum target, GLint level, GLenum format,
1095 GLenum type, GLvoid *pixels )
jtgafb833d1999-08-19 00:55:39 +00001096{
Brian Paulfbd8f211999-11-11 01:22:25 +00001097 GET_CURRENT_CONTEXT(ctx);
Brian Paul01e54752000-09-05 15:40:34 +00001098 const struct gl_texture_unit *texUnit;
jtgafb833d1999-08-19 00:55:39 +00001099 const struct gl_texture_object *texObj;
Brian Paulf7b57072000-03-20 14:37:52 +00001100 struct gl_texture_image *texImage;
jtgafb833d1999-08-19 00:55:39 +00001101
Keith Whitwellcab974c2000-12-26 05:09:27 +00001102 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
jtgafb833d1999-08-19 00:55:39 +00001103
1104 if (level < 0 || level >= ctx->Const.MaxTextureLevels) {
1105 gl_error( ctx, GL_INVALID_VALUE, "glGetTexImage(level)" );
1106 return;
1107 }
1108
Brian Paulb7d076f2000-03-21 01:03:40 +00001109 if (_mesa_sizeof_type(type) <= 0) {
jtgafb833d1999-08-19 00:55:39 +00001110 gl_error( ctx, GL_INVALID_ENUM, "glGetTexImage(type)" );
1111 return;
1112 }
1113
Brian Paulf7e1dfe2001-02-17 00:15:39 +00001114 if (_mesa_components_in_format(format) <= 0 ||
1115 format == GL_STENCIL_INDEX) {
jtgafb833d1999-08-19 00:55:39 +00001116 gl_error( ctx, GL_INVALID_ENUM, "glGetTexImage(format)" );
1117 return;
1118 }
1119
Brian Paulf7e1dfe2001-02-17 00:15:39 +00001120 if (!ctx->Extensions.EXT_paletted_texture && is_index_format(format)) {
1121 gl_error(ctx, GL_INVALID_ENUM, "glGetTexImage(format)");
1122 }
1123
1124 if (!ctx->Extensions.SGIX_depth_texture && format == GL_DEPTH_COMPONENT) {
1125 gl_error(ctx, GL_INVALID_ENUM, "glGetTexImage(format)");
1126 }
1127
1128 /* XXX what if format/type doesn't match texture format/type? */
1129
jtgafb833d1999-08-19 00:55:39 +00001130 if (!pixels)
Brian Paulf7b57072000-03-20 14:37:52 +00001131 return;
jtgafb833d1999-08-19 00:55:39 +00001132
Brian Paul01e54752000-09-05 15:40:34 +00001133 texUnit = &(ctx->Texture.Unit[ctx->Texture.CurrentUnit]);
1134 texObj = _mesa_select_tex_object(ctx, texUnit, target);
Brian Paul8e39ad22001-02-06 21:42:48 +00001135 if (!texObj || is_proxy_target(target)) {
Brian Paul01e54752000-09-05 15:40:34 +00001136 gl_error(ctx, GL_INVALID_ENUM, "glGetTexImage(target)");
1137 return;
jtgafb833d1999-08-19 00:55:39 +00001138 }
1139
Brian Paul8e3366f2000-11-10 15:32:07 +00001140 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
Brian Paulf7b57072000-03-20 14:37:52 +00001141 if (!texImage) {
Brian Paul7298e712000-11-07 16:40:37 +00001142 /* invalid mipmap level, not an error */
Brian Paulf7b57072000-03-20 14:37:52 +00001143 return;
1144 }
1145
1146 if (!texImage->Data) {
Brian Paul8e39ad22001-02-06 21:42:48 +00001147 /* no image data, not an error */
1148 return;
Brian Paulf7b57072000-03-20 14:37:52 +00001149 }
1150
Brian Paul8e39ad22001-02-06 21:42:48 +00001151 if (ctx->NewState & _NEW_PIXEL)
1152 gl_update_state(ctx);
1153
Brian Paulf7e1dfe2001-02-17 00:15:39 +00001154 if (is_color_format(format) &&
1155 ctx->_ImageTransferState & IMAGE_CONVOLUTION_BIT) {
Brian Paul8e39ad22001-02-06 21:42:48 +00001156 /* convert texture image to GL_RGBA, GL_FLOAT */
jtgafb833d1999-08-19 00:55:39 +00001157 GLint width = texImage->Width;
1158 GLint height = texImage->Height;
Brian Paulf96ce6a2000-09-06 15:15:43 +00001159 GLint depth = texImage->Depth;
1160 GLint img, row;
Brian Paul8e39ad22001-02-06 21:42:48 +00001161 GLfloat *tmpImage, *convImage;
1162 tmpImage = (GLfloat *) MALLOC(width * height * 4 * sizeof(GLfloat));
1163 if (!tmpImage) {
1164 gl_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage");
1165 return;
1166 }
1167 convImage = (GLfloat *) MALLOC(width * height * 4 * sizeof(GLfloat));
1168 if (!convImage) {
1169 FREE(tmpImage);
1170 gl_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage");
1171 return;
1172 }
1173
1174 for (img = 0; img < depth; img++) {
1175 GLint convWidth, convHeight;
1176
1177 /* convert texture data to GLfloat/GL_RGBA */
1178 for (row = 0; row < height; row++) {
1179 GLchan texels[1 << MAX_TEXTURE_LEVELS][4];
1180 GLint col;
1181 GLfloat *dst = tmpImage + row * width * 4;
1182 for (col = 0; col < width; col++) {
Brian Paule75d2422001-02-17 18:41:01 +00001183 (*texImage->FetchTexel)(texImage, col, row, img,
Brian Paul8e39ad22001-02-06 21:42:48 +00001184 texels[col]);
1185 }
1186 _mesa_unpack_float_color_span(ctx, width, GL_RGBA, dst,
1187 GL_RGBA, CHAN_TYPE, texels,
1188 &_mesa_native_packing,
1189 ctx->_ImageTransferState & IMAGE_PRE_CONVOLUTION_BITS,
1190 GL_FALSE);
1191 }
1192
1193 convWidth = width;
1194 convHeight = height;
1195
1196 /* convolve */
1197 if (target == GL_TEXTURE_1D) {
1198 if (ctx->Pixel.Convolution1DEnabled) {
1199 _mesa_convolve_1d_image(ctx, &convWidth, tmpImage, convImage);
1200 }
1201 }
1202 else {
1203 if (ctx->Pixel.Convolution2DEnabled) {
1204 _mesa_convolve_2d_image(ctx, &convWidth, &convHeight,
1205 tmpImage, convImage);
1206 }
1207 else if (ctx->Pixel.Separable2DEnabled) {
1208 _mesa_convolve_sep_image(ctx, &convWidth, &convHeight,
1209 tmpImage, convImage);
1210 }
1211 }
1212
1213 /* pack convolved image */
1214 for (row = 0; row < convHeight; row++) {
1215 const GLfloat *src = convImage + row * convWidth * 4;
1216 GLvoid *dest = _mesa_image_address(&ctx->Pack, pixels,
1217 convWidth, convHeight,
1218 format, type, img, row, 0);
1219 _mesa_pack_float_rgba_span(ctx, convWidth,
1220 (const GLfloat(*)[4]) src,
1221 format, type, dest, &ctx->Pack,
1222 ctx->_ImageTransferState & IMAGE_POST_CONVOLUTION_BITS);
1223 }
1224 }
1225
1226 FREE(tmpImage);
1227 FREE(convImage);
1228 }
1229 else {
Brian Paulf7e1dfe2001-02-17 00:15:39 +00001230 /* no convolution, or non-rgba image */
Brian Paul8e39ad22001-02-06 21:42:48 +00001231 GLint width = texImage->Width;
1232 GLint height = texImage->Height;
1233 GLint depth = texImage->Depth;
1234 GLint img, row;
1235 for (img = 0; img < depth; img++) {
1236 for (row = 0; row < height; row++) {
1237 /* compute destination address in client memory */
1238 GLvoid *dest = _mesa_image_address( &ctx->Unpack, pixels,
Brian Paulf7e1dfe2001-02-17 00:15:39 +00001239 width, height, format, type,
1240 img, row, 0);
Brian Paul8e39ad22001-02-06 21:42:48 +00001241 assert(dest);
1242
Brian Paulf7e1dfe2001-02-17 00:15:39 +00001243 if (format == GL_COLOR_INDEX) {
1244 GLuint indexRow[MAX_WIDTH];
1245 GLint col;
1246 for (col = 0; col < width; col++) {
Brian Paule75d2422001-02-17 18:41:01 +00001247 (*texImage->FetchTexel)(texImage, col, row, img,
1248 (GLvoid *) &indexRow[col]);
Brian Paulf7e1dfe2001-02-17 00:15:39 +00001249 }
1250 _mesa_pack_index_span(ctx, width, type, dest,
1251 indexRow, &ctx->Pack,
1252 ctx->_ImageTransferState);
1253 }
1254 else if (format == GL_DEPTH_COMPONENT) {
Brian Paulf7e1dfe2001-02-17 00:15:39 +00001255 GLfloat depthRow[MAX_WIDTH];
1256 GLint col;
1257 for (col = 0; col < width; col++) {
Brian Paule75d2422001-02-17 18:41:01 +00001258 (*texImage->FetchTexel)(texImage, col, row, img,
1259 (GLvoid *) &depthRow[col]);
Brian Paulf7e1dfe2001-02-17 00:15:39 +00001260 }
1261 _mesa_pack_depth_span(ctx, width, dest, type,
1262 depthRow, &ctx->Pack);
1263 }
1264 else {
Brian Paul8e39ad22001-02-06 21:42:48 +00001265 /* general case: convert row to RGBA format */
1266 GLchan rgba[MAX_WIDTH][4];
1267 GLint col;
1268 for (col = 0; col < width; col++) {
Brian Paule75d2422001-02-17 18:41:01 +00001269 (*texImage->FetchTexel)(texImage, col, row, img,
1270 (GLvoid *) rgba[col]);
Brian Paul8e39ad22001-02-06 21:42:48 +00001271 }
Brian Paule75d2422001-02-17 18:41:01 +00001272 _mesa_pack_rgba_span(ctx, width, (const GLchan (*)[4])rgba,
1273 format, type, dest, &ctx->Pack,
1274 ctx->_ImageTransferState);
Brian Paul8e39ad22001-02-06 21:42:48 +00001275 } /* format */
1276 } /* row */
1277 } /* img */
1278 } /* convolution */
1279}
1280
1281
1282
1283/*
1284 * Called from the API. Note that width includes the border.
1285 */
1286void
1287_mesa_TexImage1D( GLenum target, GLint level, GLint internalFormat,
1288 GLsizei width, GLint border, GLenum format,
1289 GLenum type, const GLvoid *pixels )
1290{
1291 GLsizei postConvWidth = width;
1292 GET_CURRENT_CONTEXT(ctx);
1293 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
1294
Brian Paulf7e1dfe2001-02-17 00:15:39 +00001295 if (is_color_format(internalFormat)) {
1296 _mesa_adjust_image_for_convolution(ctx, 1, &postConvWidth, NULL);
1297 }
Brian Paul8e39ad22001-02-06 21:42:48 +00001298
1299 if (target == GL_TEXTURE_1D) {
1300 struct gl_texture_unit *texUnit;
1301 struct gl_texture_object *texObj;
1302 struct gl_texture_image *texImage;
1303
1304 if (texture_error_check(ctx, target, level, internalFormat,
1305 format, type, 1, postConvWidth, 1, 1, border)) {
1306 return; /* error was recorded */
1307 }
1308
1309 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1310 texObj = _mesa_select_tex_object(ctx, texUnit, target);
1311 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
1312
1313 if (!texImage) {
1314 texImage = _mesa_alloc_texture_image();
1315 texObj->Image[level] = texImage;
1316 if (!texImage) {
1317 gl_error(ctx, GL_OUT_OF_MEMORY, "glTexImage1D");
1318 return;
1319 }
1320 }
1321 else if (texImage->Data) {
1322 /* free the old texture data */
1323 FREE(texImage->Data);
1324 texImage->Data = NULL;
1325 }
Brian Paulf378ab82001-02-06 23:35:26 +00001326 clear_teximage_fields(texImage); /* not really needed, but helpful */
Brian Paul6628bc92001-02-07 03:27:41 +00001327 init_teximage_fields(ctx, texImage, postConvWidth, 1, 1,
1328 border, internalFormat);
jtgafb833d1999-08-19 00:55:39 +00001329
Brian Paul9499e012000-10-30 16:32:42 +00001330 if (ctx->NewState & _NEW_PIXEL)
1331 gl_update_state(ctx);
Brian Paulfa4525e2000-08-21 14:22:24 +00001332
Brian Paul8e39ad22001-02-06 21:42:48 +00001333 ASSERT(ctx->Driver.TexImage1D);
1334 if (pixels) {
1335 (*ctx->Driver.TexImage1D)(ctx, target, level, internalFormat,
1336 width, border, format, type, pixels,
1337 &ctx->Unpack, texObj, texImage);
jtgafb833d1999-08-19 00:55:39 +00001338 }
Brian Paulf96ce6a2000-09-06 15:15:43 +00001339 else {
Brian Paul8e39ad22001-02-06 21:42:48 +00001340 GLubyte *dummy = make_null_texture(width, 1, 1, format);
1341 if (dummy) {
1342 (*ctx->Driver.TexImage1D)(ctx, target, level, internalFormat,
1343 width, border,
1344 format, GL_UNSIGNED_BYTE, dummy,
1345 &_mesa_native_packing, texObj, texImage);
1346 FREE(dummy);
1347 }
1348 }
Brian Paulf7b57072000-03-20 14:37:52 +00001349
Brian Paulf378ab82001-02-06 23:35:26 +00001350 /* one of these has to be non-zero! */
1351 ASSERT(texImage->RedBits || texImage->IndexBits || texImage->AlphaBits ||
Brian Paulf7e1dfe2001-02-17 00:15:39 +00001352 texImage->LuminanceBits || texImage->IntensityBits ||
1353 texImage->DepthBits);
Brian Paulf378ab82001-02-06 23:35:26 +00001354 ASSERT(texImage->FetchTexel);
1355
Brian Paul8e39ad22001-02-06 21:42:48 +00001356 /* state update */
1357 texObj->Complete = GL_FALSE;
1358 ctx->NewState |= _NEW_TEXTURE;
1359 }
1360 else if (target == GL_PROXY_TEXTURE_1D) {
1361 /* Proxy texture: check for errors and update proxy state */
1362 GLenum error = texture_error_check(ctx, target, level, internalFormat,
1363 format, type, 1,
1364 postConvWidth, 1, 1, border);
1365 if (!error) {
Brian Paula1f15862001-02-07 16:27:41 +00001366 struct gl_texture_unit *texUnit;
1367 struct gl_texture_image *texImage;
1368 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1369 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
1370 init_teximage_fields(ctx, texImage, postConvWidth, 1, 1,
1371 border, internalFormat);
Brian Paul8e39ad22001-02-06 21:42:48 +00001372 ASSERT(ctx->Driver.TestProxyTexImage);
1373 error = !(*ctx->Driver.TestProxyTexImage)(ctx, target, level,
1374 internalFormat, format, type,
1375 postConvWidth, 1, 1, border);
1376 }
1377 if (error) {
1378 /* if error, clear all proxy texture image parameters */
1379 if (level >= 0 && level < ctx->Const.MaxTextureLevels) {
Brian Paulf378ab82001-02-06 23:35:26 +00001380 clear_teximage_fields(ctx->Texture.Proxy1D->Image[level]);
Brian Paul8e39ad22001-02-06 21:42:48 +00001381 }
1382 }
1383 }
1384 else {
1385 gl_error( ctx, GL_INVALID_ENUM, "glTexImage1D(target)" );
1386 return;
1387 }
1388}
1389
1390
1391void
1392_mesa_TexImage2D( GLenum target, GLint level, GLint internalFormat,
1393 GLsizei width, GLsizei height, GLint border,
1394 GLenum format, GLenum type,
1395 const GLvoid *pixels )
1396{
1397 GLsizei postConvWidth = width, postConvHeight = height;
1398 GET_CURRENT_CONTEXT(ctx);
1399 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
1400
Brian Paulf7e1dfe2001-02-17 00:15:39 +00001401 if (is_color_format(internalFormat)) {
1402 _mesa_adjust_image_for_convolution(ctx, 2, &postConvWidth,
1403 &postConvHeight);
1404 }
Brian Paul8e39ad22001-02-06 21:42:48 +00001405
1406 if (target == GL_TEXTURE_2D ||
1407 (ctx->Extensions.ARB_texture_cube_map &&
1408 target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
1409 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB)) {
1410 /* non-proxy target */
1411 struct gl_texture_unit *texUnit;
1412 struct gl_texture_object *texObj;
1413 struct gl_texture_image *texImage;
1414
1415 if (texture_error_check(ctx, target, level, internalFormat,
1416 format, type, 2, postConvWidth, postConvHeight,
1417 1, border)) {
1418 return; /* error was recorded */
1419 }
1420
1421 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1422 texObj = _mesa_select_tex_object(ctx, texUnit, target);
1423 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
1424
1425 if (!texImage) {
1426 texImage = _mesa_alloc_texture_image();
1427 set_tex_image(texObj, target, level, texImage);
1428 if (!texImage) {
1429 gl_error(ctx, GL_OUT_OF_MEMORY, "glTexImage2D");
1430 return;
1431 }
1432 }
1433 else if (texImage->Data) {
1434 /* free the old texture data */
Brian Paulf7b57072000-03-20 14:37:52 +00001435 FREE(texImage->Data);
1436 texImage->Data = NULL;
1437 }
Brian Paulf378ab82001-02-06 23:35:26 +00001438 clear_teximage_fields(texImage); /* not really needed, but helpful */
Brian Paul6628bc92001-02-07 03:27:41 +00001439 init_teximage_fields(ctx, texImage, postConvWidth, postConvHeight, 1,
1440 border, internalFormat);
Brian Paul8e39ad22001-02-06 21:42:48 +00001441
1442 if (ctx->NewState & _NEW_PIXEL)
1443 gl_update_state(ctx);
1444
1445 ASSERT(ctx->Driver.TexImage2D);
1446 if (pixels) {
1447 (*ctx->Driver.TexImage2D)(ctx, target, level, internalFormat,
1448 width, height, border, format, type, pixels,
1449 &ctx->Unpack, texObj, texImage);
1450 }
1451 else {
1452 GLubyte *dummy = make_null_texture(width, height, 1, format);
1453 if (dummy) {
1454 (*ctx->Driver.TexImage2D)(ctx, target, level, internalFormat,
1455 width, height, border,
1456 format, GL_UNSIGNED_BYTE, dummy,
1457 &_mesa_native_packing, texObj, texImage);
1458 FREE(dummy);
1459 }
1460 }
1461
Brian Paulf378ab82001-02-06 23:35:26 +00001462 /* one of these has to be non-zero! */
1463 ASSERT(texImage->RedBits || texImage->IndexBits || texImage->AlphaBits ||
Brian Paulf7e1dfe2001-02-17 00:15:39 +00001464 texImage->LuminanceBits || texImage->IntensityBits ||
1465 texImage->DepthBits);
Brian Paulf378ab82001-02-06 23:35:26 +00001466 ASSERT(texImage->FetchTexel);
1467
Brian Paul8e39ad22001-02-06 21:42:48 +00001468 /* state update */
1469 texObj->Complete = GL_FALSE;
1470 ctx->NewState |= _NEW_TEXTURE;
jtgafb833d1999-08-19 00:55:39 +00001471 }
Brian Paul8e39ad22001-02-06 21:42:48 +00001472 else if (target == GL_PROXY_TEXTURE_2D) {
1473 /* Proxy texture: check for errors and update proxy state */
1474 GLenum error = texture_error_check(ctx, target, level, internalFormat,
1475 format, type, 2,
1476 postConvWidth, postConvHeight, 1, border);
1477 if (!error) {
Brian Paula1f15862001-02-07 16:27:41 +00001478 struct gl_texture_unit *texUnit;
1479 struct gl_texture_image *texImage;
1480 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1481 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
1482 init_teximage_fields(ctx, texImage, postConvWidth, postConvHeight, 1,
1483 border, internalFormat);
Brian Paul8e39ad22001-02-06 21:42:48 +00001484 ASSERT(ctx->Driver.TestProxyTexImage);
1485 error = !(*ctx->Driver.TestProxyTexImage)(ctx, target, level,
1486 internalFormat, format, type,
1487 postConvWidth, postConvHeight, 1, border);
1488 }
1489 if (error) {
1490 /* if error, clear all proxy texture image parameters */
1491 if (level >= 0 && level < ctx->Const.MaxTextureLevels) {
Brian Paulf378ab82001-02-06 23:35:26 +00001492 clear_teximage_fields(ctx->Texture.Proxy2D->Image[level]);
Brian Paul8e39ad22001-02-06 21:42:48 +00001493 }
1494 }
1495 }
1496 else {
1497 gl_error( ctx, GL_INVALID_ENUM, "glTexImage2D(target)" );
1498 return;
1499 }
1500}
1501
1502
1503/*
1504 * Called by the API or display list executor.
1505 * Note that width and height include the border.
1506 */
1507void
1508_mesa_TexImage3D( GLenum target, GLint level, GLint internalFormat,
1509 GLsizei width, GLsizei height, GLsizei depth,
1510 GLint border, GLenum format, GLenum type,
1511 const GLvoid *pixels )
1512{
1513 GET_CURRENT_CONTEXT(ctx);
1514 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
1515
1516 if (target == GL_TEXTURE_3D) {
1517 struct gl_texture_unit *texUnit;
1518 struct gl_texture_object *texObj;
1519 struct gl_texture_image *texImage;
1520
1521 if (texture_error_check(ctx, target, level, internalFormat,
1522 format, type, 3, width, height, depth, border)) {
1523 return; /* error was recorded */
1524 }
1525
1526 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1527 texObj = _mesa_select_tex_object(ctx, texUnit, target);
1528 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
1529
1530 if (!texImage) {
1531 texImage = _mesa_alloc_texture_image();
1532 texObj->Image[level] = texImage;
1533 if (!texImage) {
1534 gl_error(ctx, GL_OUT_OF_MEMORY, "glTexImage3D");
1535 return;
1536 }
1537 }
1538 else if (texImage->Data) {
1539 FREE(texImage->Data);
1540 texImage->Data = NULL;
1541 }
Brian Paulf378ab82001-02-06 23:35:26 +00001542 clear_teximage_fields(texImage); /* not really needed, but helpful */
Brian Paul6628bc92001-02-07 03:27:41 +00001543 init_teximage_fields(ctx, texImage, width, height, depth, border,
1544 internalFormat);
Brian Paul8e39ad22001-02-06 21:42:48 +00001545
1546 if (ctx->NewState & _NEW_PIXEL)
1547 gl_update_state(ctx);
1548
1549 ASSERT(ctx->Driver.TexImage3D);
1550 if (pixels) {
1551 (*ctx->Driver.TexImage3D)(ctx, target, level, internalFormat,
1552 width, height, depth, border,
1553 format, type, pixels,
1554 &ctx->Unpack, texObj, texImage);
1555 }
1556 else {
1557 GLubyte *dummy = make_null_texture(width, height, depth, format);
1558 if (dummy) {
1559 (*ctx->Driver.TexImage3D)(ctx, target, level, internalFormat,
1560 width, height, depth, border,
1561 format, GL_UNSIGNED_BYTE, dummy,
1562 &_mesa_native_packing, texObj, texImage);
1563 FREE(dummy);
1564 }
1565 }
1566
Brian Paulf378ab82001-02-06 23:35:26 +00001567 /* one of these has to be non-zero! */
1568 ASSERT(texImage->RedBits || texImage->IndexBits || texImage->AlphaBits ||
Brian Paulf7e1dfe2001-02-17 00:15:39 +00001569 texImage->LuminanceBits || texImage->IntensityBits ||
1570 texImage->DepthBits);
Brian Paulf378ab82001-02-06 23:35:26 +00001571 ASSERT(texImage->FetchTexel);
1572
Brian Paul8e39ad22001-02-06 21:42:48 +00001573 /* state update */
1574 texObj->Complete = GL_FALSE;
1575 ctx->NewState |= _NEW_TEXTURE;
1576 }
1577 else if (target == GL_PROXY_TEXTURE_3D) {
1578 /* Proxy texture: check for errors and update proxy state */
1579 GLenum error = texture_error_check(ctx, target, level, internalFormat,
1580 format, type, 3, width, height, depth, border);
1581 if (!error) {
Brian Paula1f15862001-02-07 16:27:41 +00001582 struct gl_texture_unit *texUnit;
1583 struct gl_texture_image *texImage;
1584 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1585 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
1586 init_teximage_fields(ctx, texImage, width, height, 1,
1587 border, internalFormat);
Brian Paul8e39ad22001-02-06 21:42:48 +00001588 ASSERT(ctx->Driver.TestProxyTexImage);
1589 error = !(*ctx->Driver.TestProxyTexImage)(ctx, target, level,
1590 internalFormat, format, type,
1591 width, height, depth, border);
1592 }
1593 if (error) {
1594 /* if error, clear all proxy texture image parameters */
Brian Paulf378ab82001-02-06 23:35:26 +00001595 if (level >= 0 && level < ctx->Const.MaxTextureLevels) {
1596 clear_teximage_fields(ctx->Texture.Proxy3D->Image[level]);
Brian Paul8e39ad22001-02-06 21:42:48 +00001597 }
1598 }
1599 }
1600 else {
1601 gl_error( ctx, GL_INVALID_ENUM, "glTexImage3D(target)" );
1602 return;
1603 }
1604}
1605
1606
1607void
1608_mesa_TexImage3DEXT( GLenum target, GLint level, GLenum internalFormat,
1609 GLsizei width, GLsizei height, GLsizei depth,
1610 GLint border, GLenum format, GLenum type,
1611 const GLvoid *pixels )
1612{
1613 _mesa_TexImage3D(target, level, (GLint) internalFormat, width, height,
1614 depth, border, format, type, pixels);
jtgafb833d1999-08-19 00:55:39 +00001615}
1616
1617
1618
Brian Paulfbd8f211999-11-11 01:22:25 +00001619void
1620_mesa_TexSubImage1D( GLenum target, GLint level,
1621 GLint xoffset, GLsizei width,
1622 GLenum format, GLenum type,
1623 const GLvoid *pixels )
jtgafb833d1999-08-19 00:55:39 +00001624{
Brian Paulab6e78f2000-12-09 21:30:43 +00001625 GLsizei postConvWidth = width;
Brian Paulfbd8f211999-11-11 01:22:25 +00001626 GET_CURRENT_CONTEXT(ctx);
Brian Paul02938782000-03-22 17:38:11 +00001627 struct gl_texture_unit *texUnit;
1628 struct gl_texture_object *texObj;
1629 struct gl_texture_image *texImage;
Brian Paula805bb92000-09-02 17:52:21 +00001630
Brian Paulf7e1dfe2001-02-17 00:15:39 +00001631 /* XXX should test internal format */
1632 if (is_color_format(format)) {
1633 _mesa_adjust_image_for_convolution(ctx, 1, &postConvWidth, NULL);
1634 }
jtgafb833d1999-08-19 00:55:39 +00001635
Brian Paulc3f0a511999-11-03 17:27:05 +00001636 if (subtexture_error_check(ctx, 1, target, level, xoffset, 0, 0,
Brian Paula805bb92000-09-02 17:52:21 +00001637 postConvWidth, 1, 1, format, type)) {
Brian Paulf7b57072000-03-20 14:37:52 +00001638 return; /* error was detected */
jtgafb833d1999-08-19 00:55:39 +00001639 }
1640
Brian Paul02938782000-03-22 17:38:11 +00001641 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
Brian Paul8e39ad22001-02-06 21:42:48 +00001642 texObj = _mesa_select_tex_object(ctx, texUnit, target);
1643 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
Brian Paul02938782000-03-22 17:38:11 +00001644 assert(texImage);
jtgafb833d1999-08-19 00:55:39 +00001645
Brian Paulc3f0a511999-11-03 17:27:05 +00001646 if (width == 0 || !pixels)
1647 return; /* no-op, not an error */
jtgafb833d1999-08-19 00:55:39 +00001648
Brian Paul9499e012000-10-30 16:32:42 +00001649 if (ctx->NewState & _NEW_PIXEL)
1650 gl_update_state(ctx);
Brian Paulc3f0a511999-11-03 17:27:05 +00001651
Brian Paul8e39ad22001-02-06 21:42:48 +00001652 ASSERT(ctx->Driver.TexSubImage1D);
1653 (*ctx->Driver.TexSubImage1D)(ctx, target, level, xoffset, width,
1654 format, type, pixels, &ctx->Unpack,
1655 texObj, texImage);
jtgafb833d1999-08-19 00:55:39 +00001656}
1657
1658
Brian Paulfbd8f211999-11-11 01:22:25 +00001659void
1660_mesa_TexSubImage2D( GLenum target, GLint level,
1661 GLint xoffset, GLint yoffset,
1662 GLsizei width, GLsizei height,
1663 GLenum format, GLenum type,
1664 const GLvoid *pixels )
jtgafb833d1999-08-19 00:55:39 +00001665{
Brian Paulab6e78f2000-12-09 21:30:43 +00001666 GLsizei postConvWidth = width, postConvHeight = height;
Brian Paulfbd8f211999-11-11 01:22:25 +00001667 GET_CURRENT_CONTEXT(ctx);
Brian Paul02938782000-03-22 17:38:11 +00001668 struct gl_texture_unit *texUnit;
1669 struct gl_texture_object *texObj;
1670 struct gl_texture_image *texImage;
Brian Paula805bb92000-09-02 17:52:21 +00001671
Brian Paulf7e1dfe2001-02-17 00:15:39 +00001672 /* XXX should test internal format */
1673 if (is_color_format(format)) {
1674 _mesa_adjust_image_for_convolution(ctx, 2, &postConvWidth,
1675 &postConvHeight);
1676 }
jtgafb833d1999-08-19 00:55:39 +00001677
Brian Paulc3f0a511999-11-03 17:27:05 +00001678 if (subtexture_error_check(ctx, 2, target, level, xoffset, yoffset, 0,
Brian Paula805bb92000-09-02 17:52:21 +00001679 postConvWidth, postConvHeight, 1, format, type)) {
Brian Paulf7b57072000-03-20 14:37:52 +00001680 return; /* error was detected */
jtgafb833d1999-08-19 00:55:39 +00001681 }
1682
Brian Paul02938782000-03-22 17:38:11 +00001683 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
Brian Paul35d53012000-05-23 17:14:49 +00001684 texObj = _mesa_select_tex_object(ctx, texUnit, target);
Brian Paul8e39ad22001-02-06 21:42:48 +00001685 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
Brian Paul02938782000-03-22 17:38:11 +00001686 assert(texImage);
jtgafb833d1999-08-19 00:55:39 +00001687
Brian Paulc3f0a511999-11-03 17:27:05 +00001688 if (width == 0 || height == 0 || !pixels)
1689 return; /* no-op, not an error */
jtgafb833d1999-08-19 00:55:39 +00001690
Brian Paul9499e012000-10-30 16:32:42 +00001691 if (ctx->NewState & _NEW_PIXEL)
1692 gl_update_state(ctx);
Brian Paulfa4525e2000-08-21 14:22:24 +00001693
Brian Paul8e39ad22001-02-06 21:42:48 +00001694 ASSERT(ctx->Driver.TexSubImage2D);
1695 (*ctx->Driver.TexSubImage2D)(ctx, target, level, xoffset, yoffset,
1696 width, height, format, type, pixels,
1697 &ctx->Unpack, texObj, texImage);
jtgafb833d1999-08-19 00:55:39 +00001698}
1699
1700
1701
Brian Paulfbd8f211999-11-11 01:22:25 +00001702void
1703_mesa_TexSubImage3D( GLenum target, GLint level,
1704 GLint xoffset, GLint yoffset, GLint zoffset,
1705 GLsizei width, GLsizei height, GLsizei depth,
1706 GLenum format, GLenum type,
1707 const GLvoid *pixels )
jtgafb833d1999-08-19 00:55:39 +00001708{
Brian Paulfbd8f211999-11-11 01:22:25 +00001709 GET_CURRENT_CONTEXT(ctx);
Brian Paul02938782000-03-22 17:38:11 +00001710 struct gl_texture_unit *texUnit;
1711 struct gl_texture_object *texObj;
1712 struct gl_texture_image *texImage;
jtgafb833d1999-08-19 00:55:39 +00001713
Brian Paulc3f0a511999-11-03 17:27:05 +00001714 if (subtexture_error_check(ctx, 3, target, level, xoffset, yoffset, zoffset,
1715 width, height, depth, format, type)) {
Brian Paulf7b57072000-03-20 14:37:52 +00001716 return; /* error was detected */
jtgafb833d1999-08-19 00:55:39 +00001717 }
1718
Brian Paul02938782000-03-22 17:38:11 +00001719 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
Brian Paul8e39ad22001-02-06 21:42:48 +00001720 texObj = _mesa_select_tex_object(ctx, texUnit, target);
1721 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
Brian Paul02938782000-03-22 17:38:11 +00001722 assert(texImage);
jtgafb833d1999-08-19 00:55:39 +00001723
Brian Paulc3f0a511999-11-03 17:27:05 +00001724 if (width == 0 || height == 0 || height == 0 || !pixels)
1725 return; /* no-op, not an error */
jtgafb833d1999-08-19 00:55:39 +00001726
Brian Paul9499e012000-10-30 16:32:42 +00001727 if (ctx->NewState & _NEW_PIXEL)
1728 gl_update_state(ctx);
Brian Paulfa4525e2000-08-21 14:22:24 +00001729
Brian Paul8e39ad22001-02-06 21:42:48 +00001730 ASSERT(ctx->Driver.TexSubImage3D);
1731 (*ctx->Driver.TexSubImage3D)(ctx, target, level,
1732 xoffset, yoffset, zoffset,
1733 width, height, depth,
1734 format, type, pixels,
1735 &ctx->Unpack, texObj, texImage );
jtgafb833d1999-08-19 00:55:39 +00001736}
1737
1738
1739
1740/*
1741 * Read an RGBA image from the frame buffer.
Brian Paula805bb92000-09-02 17:52:21 +00001742 * This is used by glCopyTex[Sub]Image[12]D().
jtgafb833d1999-08-19 00:55:39 +00001743 * Input: ctx - the context
1744 * x, y - lower left corner
1745 * width, height - size of region to read
Brian Paul699bc7b2000-10-29 18:12:14 +00001746 * Return: pointer to block of GL_RGBA, GLchan data.
jtgafb833d1999-08-19 00:55:39 +00001747 */
Brian Paul699bc7b2000-10-29 18:12:14 +00001748static GLchan *
Brian Paulc3f0a511999-11-03 17:27:05 +00001749read_color_image( GLcontext *ctx, GLint x, GLint y,
1750 GLsizei width, GLsizei height )
jtgafb833d1999-08-19 00:55:39 +00001751{
Brian Paulc3f0a511999-11-03 17:27:05 +00001752 GLint stride, i;
Brian Paul699bc7b2000-10-29 18:12:14 +00001753 GLchan *image, *dst;
jtgafb833d1999-08-19 00:55:39 +00001754
Brian Paul699bc7b2000-10-29 18:12:14 +00001755 image = (GLchan *) MALLOC(width * height * 4 * sizeof(GLchan));
Brian Paulc3f0a511999-11-03 17:27:05 +00001756 if (!image)
jtgafb833d1999-08-19 00:55:39 +00001757 return NULL;
jtgafb833d1999-08-19 00:55:39 +00001758
1759 /* Select buffer to read from */
Brian Paulcea0e8e1999-11-25 17:36:48 +00001760 (*ctx->Driver.SetReadBuffer)( ctx, ctx->ReadBuffer,
1761 ctx->Pixel.DriverReadBuffer );
jtgafb833d1999-08-19 00:55:39 +00001762
Brian Paulc34cea72000-11-21 23:25:40 +00001763 RENDER_START(ctx);
1764
Brian Paulc3f0a511999-11-03 17:27:05 +00001765 dst = image;
Brian Paul699bc7b2000-10-29 18:12:14 +00001766 stride = width * 4;
Brian Paulc3f0a511999-11-03 17:27:05 +00001767 for (i = 0; i < height; i++) {
Brian Paul3f02f901999-11-24 18:48:30 +00001768 gl_read_rgba_span( ctx, ctx->ReadBuffer, width, x, y + i,
Brian Paul699bc7b2000-10-29 18:12:14 +00001769 (GLchan (*)[4]) dst );
Brian Paulc3f0a511999-11-03 17:27:05 +00001770 dst += stride;
1771 }
jtgafb833d1999-08-19 00:55:39 +00001772
Brian Paulc34cea72000-11-21 23:25:40 +00001773 RENDER_FINISH(ctx);
1774
Brian Paulcea0e8e1999-11-25 17:36:48 +00001775 /* Read from draw buffer (the default) */
1776 (*ctx->Driver.SetReadBuffer)( ctx, ctx->DrawBuffer,
1777 ctx->Color.DriverDrawBuffer );
jtgafb833d1999-08-19 00:55:39 +00001778
1779 return image;
1780}
1781
1782
1783
Brian Paulfbd8f211999-11-11 01:22:25 +00001784void
1785_mesa_CopyTexImage1D( GLenum target, GLint level,
1786 GLenum internalFormat,
1787 GLint x, GLint y,
1788 GLsizei width, GLint border )
jtgafb833d1999-08-19 00:55:39 +00001789{
Brian Paulab6e78f2000-12-09 21:30:43 +00001790 GLsizei postConvWidth = width;
Brian Paulfbd8f211999-11-11 01:22:25 +00001791 GET_CURRENT_CONTEXT(ctx);
Keith Whitwellcab974c2000-12-26 05:09:27 +00001792 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
jtgafb833d1999-08-19 00:55:39 +00001793
Brian Paul9499e012000-10-30 16:32:42 +00001794 if (ctx->NewState & _NEW_PIXEL)
1795 gl_update_state(ctx);
Brian Paulfa4525e2000-08-21 14:22:24 +00001796
Brian Paulf7e1dfe2001-02-17 00:15:39 +00001797 if (is_color_format(internalFormat)) {
1798 _mesa_adjust_image_for_convolution(ctx, 1, &postConvWidth, NULL);
1799 }
Brian Paulab6e78f2000-12-09 21:30:43 +00001800
1801 if (copytexture_error_check(ctx, 1, target, level, internalFormat,
1802 postConvWidth, 1, border))
1803 return;
1804
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +00001805 if (ctx->_ImageTransferState || !ctx->Driver.CopyTexImage1D
Brian Paulf7b57072000-03-20 14:37:52 +00001806 || !(*ctx->Driver.CopyTexImage1D)(ctx, target, level,
Brian Paula805bb92000-09-02 17:52:21 +00001807 internalFormat, x, y, width, border)) {
Brian Paul7dac1322000-06-15 19:57:14 +00001808 struct gl_pixelstore_attrib unpackSave;
1809
1810 /* get image from framebuffer */
Brian Paul699bc7b2000-10-29 18:12:14 +00001811 GLchan *image = read_color_image( ctx, x, y, width, 1 );
Brian Paulc3f0a511999-11-03 17:27:05 +00001812 if (!image) {
1813 gl_error( ctx, GL_OUT_OF_MEMORY, "glCopyTexImage1D" );
1814 return;
1815 }
Brian Paul7dac1322000-06-15 19:57:14 +00001816
1817 /* call glTexImage1D to redefine the texture */
1818 unpackSave = ctx->Unpack;
1819 ctx->Unpack = _mesa_native_packing;
Brian Paul3ab6bbe2000-02-12 17:26:15 +00001820 (*ctx->Exec->TexImage1D)( target, level, internalFormat, width,
Brian Paulf7b57072000-03-20 14:37:52 +00001821 border, GL_RGBA, GL_UNSIGNED_BYTE, image );
Brian Paul7dac1322000-06-15 19:57:14 +00001822 ctx->Unpack = unpackSave;
1823
Brian Paulc3f0a511999-11-03 17:27:05 +00001824 FREE(image);
jtgafb833d1999-08-19 00:55:39 +00001825 }
jtgafb833d1999-08-19 00:55:39 +00001826}
1827
1828
1829
Brian Paulfbd8f211999-11-11 01:22:25 +00001830void
1831_mesa_CopyTexImage2D( GLenum target, GLint level, GLenum internalFormat,
1832 GLint x, GLint y, GLsizei width, GLsizei height,
1833 GLint border )
jtgafb833d1999-08-19 00:55:39 +00001834{
Brian Paulab6e78f2000-12-09 21:30:43 +00001835 GLsizei postConvWidth = width, postConvHeight = height;
Brian Paulfbd8f211999-11-11 01:22:25 +00001836 GET_CURRENT_CONTEXT(ctx);
Keith Whitwellcab974c2000-12-26 05:09:27 +00001837 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
jtgafb833d1999-08-19 00:55:39 +00001838
Brian Paul9499e012000-10-30 16:32:42 +00001839 if (ctx->NewState & _NEW_PIXEL)
1840 gl_update_state(ctx);
Brian Paulfa4525e2000-08-21 14:22:24 +00001841
Brian Paulf7e1dfe2001-02-17 00:15:39 +00001842 if (is_color_format(internalFormat)) {
1843 _mesa_adjust_image_for_convolution(ctx, 2, &postConvWidth,
1844 &postConvHeight);
1845 }
Brian Paulab6e78f2000-12-09 21:30:43 +00001846
1847 if (copytexture_error_check(ctx, 2, target, level, internalFormat,
1848 postConvWidth, postConvHeight, border))
1849 return;
1850
Keith Whitwell14940c42000-11-05 18:40:57 +00001851 if (ctx->_ImageTransferState || !ctx->Driver.CopyTexImage2D
Brian Paulf7b57072000-03-20 14:37:52 +00001852 || !(*ctx->Driver.CopyTexImage2D)(ctx, target, level,
Brian Paula805bb92000-09-02 17:52:21 +00001853 internalFormat, x, y, width, height, border)) {
Brian Paul7dac1322000-06-15 19:57:14 +00001854 struct gl_pixelstore_attrib unpackSave;
1855
1856 /* get image from framebuffer */
Brian Paul699bc7b2000-10-29 18:12:14 +00001857 GLchan *image = read_color_image( ctx, x, y, width, height );
Brian Paulc3f0a511999-11-03 17:27:05 +00001858 if (!image) {
1859 gl_error( ctx, GL_OUT_OF_MEMORY, "glCopyTexImage2D" );
1860 return;
1861 }
Brian Paul7dac1322000-06-15 19:57:14 +00001862
1863 /* call glTexImage2D to redefine the texture */
1864 unpackSave = ctx->Unpack;
1865 ctx->Unpack = _mesa_native_packing;
Brian Paul3ab6bbe2000-02-12 17:26:15 +00001866 (ctx->Exec->TexImage2D)( target, level, internalFormat, width,
Brian Paulf7b57072000-03-20 14:37:52 +00001867 height, border, GL_RGBA, GL_UNSIGNED_BYTE, image );
Brian Paul7dac1322000-06-15 19:57:14 +00001868 ctx->Unpack = unpackSave;
1869
Brian Paulc3f0a511999-11-03 17:27:05 +00001870 FREE(image);
jtgafb833d1999-08-19 00:55:39 +00001871 }
jtgafb833d1999-08-19 00:55:39 +00001872}
1873
1874
1875
Brian Paulfbd8f211999-11-11 01:22:25 +00001876void
1877_mesa_CopyTexSubImage1D( GLenum target, GLint level,
1878 GLint xoffset, GLint x, GLint y, GLsizei width )
jtgafb833d1999-08-19 00:55:39 +00001879{
Brian Paulab6e78f2000-12-09 21:30:43 +00001880 GLsizei postConvWidth = width;
Brian Paulfbd8f211999-11-11 01:22:25 +00001881 GET_CURRENT_CONTEXT(ctx);
Keith Whitwellcab974c2000-12-26 05:09:27 +00001882 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
jtgafb833d1999-08-19 00:55:39 +00001883
Brian Paul9499e012000-10-30 16:32:42 +00001884 if (ctx->NewState & _NEW_PIXEL)
1885 gl_update_state(ctx);
Brian Paulfa4525e2000-08-21 14:22:24 +00001886
Brian Paulf7e1dfe2001-02-17 00:15:39 +00001887 /* XXX should test internal format */
Brian Paul8e39ad22001-02-06 21:42:48 +00001888 _mesa_adjust_image_for_convolution(ctx, 1, &postConvWidth, NULL);
Brian Paulab6e78f2000-12-09 21:30:43 +00001889
1890 if (copytexsubimage_error_check(ctx, 1, target, level,
1891 xoffset, 0, 0, postConvWidth, 1))
1892 return;
1893
Keith Whitwell14940c42000-11-05 18:40:57 +00001894 if (ctx->_ImageTransferState || !ctx->Driver.CopyTexSubImage1D
Brian Paulf7b57072000-03-20 14:37:52 +00001895 || !(*ctx->Driver.CopyTexSubImage1D)(ctx, target, level,
1896 xoffset, x, y, width)) {
Brian Paulc3f0a511999-11-03 17:27:05 +00001897 struct gl_texture_unit *texUnit;
1898 struct gl_texture_image *teximage;
Brian Paul7dac1322000-06-15 19:57:14 +00001899 struct gl_pixelstore_attrib unpackSave;
Brian Paul699bc7b2000-10-29 18:12:14 +00001900 GLchan *image;
Brian Paul7dac1322000-06-15 19:57:14 +00001901
Brian Paulc3f0a511999-11-03 17:27:05 +00001902 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
Brian Paula8523782000-11-19 23:10:25 +00001903 teximage = texUnit->Current1D->Image[level];
Brian Paulc3f0a511999-11-03 17:27:05 +00001904 assert(teximage);
Brian Paul7dac1322000-06-15 19:57:14 +00001905
1906 /* get image from frame buffer */
1907 image = read_color_image(ctx, x, y, width, 1);
1908 if (!image) {
Brian Paulab6e78f2000-12-09 21:30:43 +00001909 gl_error( ctx, GL_OUT_OF_MEMORY, "glCopyTexSubImage1D" );
Brian Paul7dac1322000-06-15 19:57:14 +00001910 return;
jtgafb833d1999-08-19 00:55:39 +00001911 }
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +00001912
Brian Paul7dac1322000-06-15 19:57:14 +00001913 /* now call glTexSubImage1D to do the real work */
1914 unpackSave = ctx->Unpack;
1915 ctx->Unpack = _mesa_native_packing;
1916 _mesa_TexSubImage1D(target, level, xoffset, width,
1917 GL_RGBA, GL_UNSIGNED_BYTE, image);
1918 ctx->Unpack = unpackSave;
1919
1920 FREE(image);
jtgafb833d1999-08-19 00:55:39 +00001921 }
Brian Paulc3f0a511999-11-03 17:27:05 +00001922}
1923
1924
1925
Brian Paulfbd8f211999-11-11 01:22:25 +00001926void
1927_mesa_CopyTexSubImage2D( GLenum target, GLint level,
1928 GLint xoffset, GLint yoffset,
1929 GLint x, GLint y, GLsizei width, GLsizei height )
Brian Paulc3f0a511999-11-03 17:27:05 +00001930{
Brian Paulab6e78f2000-12-09 21:30:43 +00001931 GLsizei postConvWidth = width, postConvHeight = height;
Brian Paulfbd8f211999-11-11 01:22:25 +00001932 GET_CURRENT_CONTEXT(ctx);
Keith Whitwellcab974c2000-12-26 05:09:27 +00001933 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
Brian Paulc3f0a511999-11-03 17:27:05 +00001934
Brian Paul9499e012000-10-30 16:32:42 +00001935 if (ctx->NewState & _NEW_PIXEL)
1936 gl_update_state(ctx);
Brian Paulfa4525e2000-08-21 14:22:24 +00001937
Brian Paulf7e1dfe2001-02-17 00:15:39 +00001938 /* XXX should test internal format */
1939 _mesa_adjust_image_for_convolution(ctx, 2, &postConvWidth, &postConvHeight);
Brian Paulab6e78f2000-12-09 21:30:43 +00001940
1941 if (copytexsubimage_error_check(ctx, 2, target, level, xoffset, yoffset, 0,
1942 postConvWidth, postConvHeight))
1943 return;
1944
Keith Whitwell14940c42000-11-05 18:40:57 +00001945 if (ctx->_ImageTransferState || !ctx->Driver.CopyTexSubImage2D
Brian Paulf7b57072000-03-20 14:37:52 +00001946 || !(*ctx->Driver.CopyTexSubImage2D)(ctx, target, level,
1947 xoffset, yoffset, x, y, width, height )) {
Brian Paulc3f0a511999-11-03 17:27:05 +00001948 struct gl_texture_unit *texUnit;
1949 struct gl_texture_image *teximage;
Brian Paul7dac1322000-06-15 19:57:14 +00001950 struct gl_pixelstore_attrib unpackSave;
Brian Paul699bc7b2000-10-29 18:12:14 +00001951 GLchan *image;
Brian Paul7dac1322000-06-15 19:57:14 +00001952
Brian Paulc3f0a511999-11-03 17:27:05 +00001953 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
Brian Paula8523782000-11-19 23:10:25 +00001954 teximage = texUnit->Current2D->Image[level];
Brian Paulc3f0a511999-11-03 17:27:05 +00001955 assert(teximage);
Brian Paul7dac1322000-06-15 19:57:14 +00001956
1957 /* get image from frame buffer */
1958 image = read_color_image(ctx, x, y, width, height);
1959 if (!image) {
1960 gl_error( ctx, GL_OUT_OF_MEMORY, "glCopyTexSubImage2D" );
1961 return;
Brian Paulc3f0a511999-11-03 17:27:05 +00001962 }
Brian Paul7dac1322000-06-15 19:57:14 +00001963
1964 /* now call glTexSubImage2D to do the real work */
1965 unpackSave = ctx->Unpack;
1966 ctx->Unpack = _mesa_native_packing;
1967 _mesa_TexSubImage2D(target, level, xoffset, yoffset, width, height,
1968 GL_RGBA, GL_UNSIGNED_BYTE, image);
1969 ctx->Unpack = unpackSave;
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +00001970
Brian Paul7dac1322000-06-15 19:57:14 +00001971 FREE(image);
Brian Paulc3f0a511999-11-03 17:27:05 +00001972 }
1973}
1974
1975
1976
Brian Paulfbd8f211999-11-11 01:22:25 +00001977void
1978_mesa_CopyTexSubImage3D( GLenum target, GLint level,
1979 GLint xoffset, GLint yoffset, GLint zoffset,
1980 GLint x, GLint y, GLsizei width, GLsizei height )
Brian Paulc3f0a511999-11-03 17:27:05 +00001981{
Brian Paulab6e78f2000-12-09 21:30:43 +00001982 GLsizei postConvWidth = width, postConvHeight = height;
Brian Paulfbd8f211999-11-11 01:22:25 +00001983 GET_CURRENT_CONTEXT(ctx);
Keith Whitwellcab974c2000-12-26 05:09:27 +00001984 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
Brian Paulc3f0a511999-11-03 17:27:05 +00001985
Brian Paul9499e012000-10-30 16:32:42 +00001986 if (ctx->NewState & _NEW_PIXEL)
1987 gl_update_state(ctx);
Brian Paulfa4525e2000-08-21 14:22:24 +00001988
Brian Paulf7e1dfe2001-02-17 00:15:39 +00001989 /* XXX should test internal format */
1990 _mesa_adjust_image_for_convolution(ctx, 2, &postConvWidth, &postConvHeight);
Brian Paulab6e78f2000-12-09 21:30:43 +00001991
1992 if (copytexsubimage_error_check(ctx, 3, target, level, xoffset, yoffset,
1993 zoffset, postConvWidth, postConvHeight))
1994 return;
1995
Keith Whitwell14940c42000-11-05 18:40:57 +00001996 if (ctx->_ImageTransferState || !ctx->Driver.CopyTexSubImage3D
Brian Paulf7b57072000-03-20 14:37:52 +00001997 || !(*ctx->Driver.CopyTexSubImage3D)(ctx, target, level,
Brian Paul02938782000-03-22 17:38:11 +00001998 xoffset, yoffset, zoffset, x, y, width, height )) {
1999 struct gl_texture_unit *texUnit;
2000 struct gl_texture_image *teximage;
Brian Paul7dac1322000-06-15 19:57:14 +00002001 struct gl_pixelstore_attrib unpackSave;
Brian Paul699bc7b2000-10-29 18:12:14 +00002002 GLchan *image;
Brian Paul7dac1322000-06-15 19:57:14 +00002003
Brian Paul02938782000-03-22 17:38:11 +00002004 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
Brian Paula8523782000-11-19 23:10:25 +00002005 teximage = texUnit->Current3D->Image[level];
Brian Paul02938782000-03-22 17:38:11 +00002006 assert(teximage);
Brian Paul7dac1322000-06-15 19:57:14 +00002007
2008 /* get image from frame buffer */
2009 image = read_color_image(ctx, x, y, width, height);
2010 if (!image) {
2011 gl_error( ctx, GL_OUT_OF_MEMORY, "glCopyTexSubImage2D" );
2012 return;
Brian Paulc3f0a511999-11-03 17:27:05 +00002013 }
Brian Paul7dac1322000-06-15 19:57:14 +00002014
2015 /* now call glTexSubImage2D to do the real work */
2016 unpackSave = ctx->Unpack;
2017 ctx->Unpack = _mesa_native_packing;
2018 _mesa_TexSubImage3D(target, level, xoffset, yoffset, zoffset,
2019 width, height, 1, GL_RGBA, GL_UNSIGNED_BYTE, image);
2020 ctx->Unpack = unpackSave;
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +00002021
Brian Paul7dac1322000-06-15 19:57:14 +00002022 FREE(image);
jtgafb833d1999-08-19 00:55:39 +00002023 }
2024}
Brian Paul1207bf02000-05-23 20:10:49 +00002025
2026
2027
2028void
2029_mesa_CompressedTexImage1DARB(GLenum target, GLint level,
Brian Paulaea66b12000-05-24 14:04:06 +00002030 GLenum internalFormat, GLsizei width,
Brian Paul1207bf02000-05-23 20:10:49 +00002031 GLint border, GLsizei imageSize,
2032 const GLvoid *data)
2033{
Brian Paulaea66b12000-05-24 14:04:06 +00002034 GET_CURRENT_CONTEXT(ctx);
Keith Whitwellcab974c2000-12-26 05:09:27 +00002035 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
Brian Paulaea66b12000-05-24 14:04:06 +00002036
Brian Paul289d47e2000-08-29 23:31:23 +00002037 switch (internalFormat) {
2038 case GL_COMPRESSED_ALPHA_ARB:
2039 case GL_COMPRESSED_LUMINANCE_ARB:
2040 case GL_COMPRESSED_LUMINANCE_ALPHA_ARB:
2041 case GL_COMPRESSED_INTENSITY_ARB:
2042 case GL_COMPRESSED_RGB_ARB:
2043 case GL_COMPRESSED_RGBA_ARB:
2044 gl_error(ctx, GL_INVALID_ENUM, "glCompressedTexImage1DARB");
2045 return;
2046 default:
2047 /* silence compiler warning */
2048 ;
2049 }
2050
Brian Paulaea66b12000-05-24 14:04:06 +00002051 if (target == GL_TEXTURE_1D) {
2052 struct gl_texture_unit *texUnit;
2053 struct gl_texture_object *texObj;
2054 struct gl_texture_image *texImage;
2055
2056 if (texture_error_check(ctx, target, level, internalFormat,
2057 GL_NONE, GL_NONE, 1, width, 1, 1, border)) {
2058 return; /* error in texture image was detected */
2059 }
2060
2061 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
Brian Paul8e39ad22001-02-06 21:42:48 +00002062 texObj = _mesa_select_tex_object(ctx, texUnit, target);
2063 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
Brian Paulaea66b12000-05-24 14:04:06 +00002064
2065 if (!texImage) {
2066 texImage = _mesa_alloc_texture_image();
2067 texObj->Image[level] = texImage;
2068 if (!texImage) {
2069 gl_error(ctx, GL_OUT_OF_MEMORY, "glCompressedTexImage1DARB");
2070 return;
2071 }
2072 }
2073 else if (texImage->Data) {
2074 FREE(texImage->Data);
2075 texImage->Data = NULL;
2076 }
2077
Brian Paul6628bc92001-02-07 03:27:41 +00002078 init_teximage_fields(ctx, texImage, width, 1, 1, border, internalFormat);
2079
Brian Paul8e39ad22001-02-06 21:42:48 +00002080 if (ctx->Extensions.ARB_texture_compression) {
2081 ASSERT(ctx->Driver.CompressedTexImage1D);
2082 (*ctx->Driver.CompressedTexImage1D)(ctx, target, level,
2083 internalFormat, width, border,
2084 imageSize, data,
2085 texObj, texImage);
2086 ASSERT(texImage->CompressedSize > 0); /* sanity */
Brian Paulaea66b12000-05-24 14:04:06 +00002087 }
2088
2089 /* state update */
Brian Paula8523782000-11-19 23:10:25 +00002090 texObj->Complete = GL_FALSE;
Keith Whitwella96308c2000-10-30 13:31:59 +00002091 ctx->NewState |= _NEW_TEXTURE;
Brian Paulaea66b12000-05-24 14:04:06 +00002092 }
2093 else if (target == GL_PROXY_TEXTURE_1D) {
2094 /* Proxy texture: check for errors and update proxy state */
Brian Paul38d3f3d2000-09-07 15:38:49 +00002095 GLenum error = texture_error_check(ctx, target, level, internalFormat,
2096 GL_NONE, GL_NONE, 1, width, 1, 1, border);
Brian Paul8e39ad22001-02-06 21:42:48 +00002097 if (!error) {
Brian Paula1f15862001-02-07 16:27:41 +00002098 struct gl_texture_unit *texUnit;
2099 struct gl_texture_image *texImage;
2100 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2101 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
2102 init_teximage_fields(ctx, texImage, width, 1, 1,
2103 border, internalFormat);
Brian Paul8e39ad22001-02-06 21:42:48 +00002104 ASSERT(ctx->Driver.TestProxyTexImage);
Brian Paul38d3f3d2000-09-07 15:38:49 +00002105 error = !(*ctx->Driver.TestProxyTexImage)(ctx, target, level,
2106 internalFormat, GL_NONE, GL_NONE,
2107 width, 1, 1, border);
2108 }
2109 if (error) {
Brian Paulaea66b12000-05-24 14:04:06 +00002110 /* if error, clear all proxy texture image parameters */
Brian Paulf378ab82001-02-06 23:35:26 +00002111 if (level >= 0 && level < ctx->Const.MaxTextureLevels) {
2112 clear_teximage_fields(ctx->Texture.Proxy1D->Image[level]);
Brian Paulaea66b12000-05-24 14:04:06 +00002113 }
2114 }
Brian Paulaea66b12000-05-24 14:04:06 +00002115 }
2116 else {
2117 gl_error( ctx, GL_INVALID_ENUM, "glCompressedTexImage1DARB(target)" );
2118 return;
2119 }
Brian Paul1207bf02000-05-23 20:10:49 +00002120}
2121
2122
2123void
2124_mesa_CompressedTexImage2DARB(GLenum target, GLint level,
Brian Paulaea66b12000-05-24 14:04:06 +00002125 GLenum internalFormat, GLsizei width,
Brian Paul1207bf02000-05-23 20:10:49 +00002126 GLsizei height, GLint border, GLsizei imageSize,
2127 const GLvoid *data)
2128{
Brian Paulaea66b12000-05-24 14:04:06 +00002129 GET_CURRENT_CONTEXT(ctx);
Keith Whitwellcab974c2000-12-26 05:09:27 +00002130 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
Brian Paulaea66b12000-05-24 14:04:06 +00002131
Brian Paul289d47e2000-08-29 23:31:23 +00002132 switch (internalFormat) {
2133 case GL_COMPRESSED_ALPHA_ARB:
2134 case GL_COMPRESSED_LUMINANCE_ARB:
2135 case GL_COMPRESSED_LUMINANCE_ALPHA_ARB:
2136 case GL_COMPRESSED_INTENSITY_ARB:
2137 case GL_COMPRESSED_RGB_ARB:
2138 case GL_COMPRESSED_RGBA_ARB:
2139 gl_error(ctx, GL_INVALID_ENUM, "glCompressedTexImage2DARB");
2140 return;
2141 default:
2142 /* silence compiler warning */
2143 ;
2144 }
2145
Brian Paul8e39ad22001-02-06 21:42:48 +00002146 if (target == GL_TEXTURE_2D ||
Keith Whitwella96308c2000-10-30 13:31:59 +00002147 (ctx->Extensions.ARB_texture_cube_map &&
Brian Paul9540a1d2000-06-06 17:03:38 +00002148 target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
2149 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB)) {
Brian Paulaea66b12000-05-24 14:04:06 +00002150 struct gl_texture_unit *texUnit;
2151 struct gl_texture_object *texObj;
2152 struct gl_texture_image *texImage;
2153
2154 if (texture_error_check(ctx, target, level, internalFormat,
2155 GL_NONE, GL_NONE, 1, width, height, 1, border)) {
2156 return; /* error in texture image was detected */
2157 }
2158
2159 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
Brian Paul8e39ad22001-02-06 21:42:48 +00002160 texObj = _mesa_select_tex_object(ctx, texUnit, target);
2161 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
Brian Paulaea66b12000-05-24 14:04:06 +00002162
2163 if (!texImage) {
2164 texImage = _mesa_alloc_texture_image();
2165 texObj->Image[level] = texImage;
2166 if (!texImage) {
2167 gl_error(ctx, GL_OUT_OF_MEMORY, "glCompressedTexImage2DARB");
2168 return;
2169 }
2170 }
2171 else if (texImage->Data) {
2172 FREE(texImage->Data);
2173 texImage->Data = NULL;
2174 }
2175
Brian Paul6628bc92001-02-07 03:27:41 +00002176 init_teximage_fields(ctx, texImage, width, height, 1, border,
2177 internalFormat);
2178
Brian Paul8e39ad22001-02-06 21:42:48 +00002179 if (ctx->Extensions.ARB_texture_compression) {
2180 ASSERT(ctx->Driver.CompressedTexImage2D);
2181 (*ctx->Driver.CompressedTexImage2D)(ctx, target, level,
2182 internalFormat, width, height,
2183 border, imageSize, data,
2184 texObj, texImage);
2185 ASSERT(texImage->CompressedSize > 0); /* sanity */
Brian Paulaea66b12000-05-24 14:04:06 +00002186 }
2187
2188 /* state update */
Brian Paula8523782000-11-19 23:10:25 +00002189 texObj->Complete = GL_FALSE;
Keith Whitwella96308c2000-10-30 13:31:59 +00002190 ctx->NewState |= _NEW_TEXTURE;
Brian Paulaea66b12000-05-24 14:04:06 +00002191 }
2192 else if (target == GL_PROXY_TEXTURE_2D) {
2193 /* Proxy texture: check for errors and update proxy state */
Brian Paul38d3f3d2000-09-07 15:38:49 +00002194 GLenum error = texture_error_check(ctx, target, level, internalFormat,
2195 GL_NONE, GL_NONE, 2, width, height, 1, border);
Brian Paul8e39ad22001-02-06 21:42:48 +00002196 if (!error) {
Brian Paula1f15862001-02-07 16:27:41 +00002197 struct gl_texture_unit *texUnit;
2198 struct gl_texture_image *texImage;
2199 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2200 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
2201 init_teximage_fields(ctx, texImage, width, height, 1,
2202 border, internalFormat);
Brian Paul8e39ad22001-02-06 21:42:48 +00002203 ASSERT(ctx->Driver.TestProxyTexImage);
Brian Paul38d3f3d2000-09-07 15:38:49 +00002204 error = !(*ctx->Driver.TestProxyTexImage)(ctx, target, level,
2205 internalFormat, GL_NONE, GL_NONE,
2206 width, height, 1, border);
2207 }
2208 if (error) {
Brian Paulaea66b12000-05-24 14:04:06 +00002209 /* if error, clear all proxy texture image parameters */
Brian Paulf378ab82001-02-06 23:35:26 +00002210 if (level >= 0 && level < ctx->Const.MaxTextureLevels) {
2211 clear_teximage_fields(ctx->Texture.Proxy2D->Image[level]);
Brian Paulaea66b12000-05-24 14:04:06 +00002212 }
2213 }
Brian Paulaea66b12000-05-24 14:04:06 +00002214 }
2215 else {
2216 gl_error( ctx, GL_INVALID_ENUM, "glCompressedTexImage2DARB(target)" );
2217 return;
2218 }
Brian Paul1207bf02000-05-23 20:10:49 +00002219}
2220
2221
2222void
2223_mesa_CompressedTexImage3DARB(GLenum target, GLint level,
Brian Paulaea66b12000-05-24 14:04:06 +00002224 GLenum internalFormat, GLsizei width,
Brian Paul1207bf02000-05-23 20:10:49 +00002225 GLsizei height, GLsizei depth, GLint border,
2226 GLsizei imageSize, const GLvoid *data)
2227{
Brian Paulaea66b12000-05-24 14:04:06 +00002228 GET_CURRENT_CONTEXT(ctx);
Keith Whitwellcab974c2000-12-26 05:09:27 +00002229 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
Brian Paulaea66b12000-05-24 14:04:06 +00002230
Brian Paul289d47e2000-08-29 23:31:23 +00002231 switch (internalFormat) {
2232 case GL_COMPRESSED_ALPHA_ARB:
2233 case GL_COMPRESSED_LUMINANCE_ARB:
2234 case GL_COMPRESSED_LUMINANCE_ALPHA_ARB:
2235 case GL_COMPRESSED_INTENSITY_ARB:
2236 case GL_COMPRESSED_RGB_ARB:
2237 case GL_COMPRESSED_RGBA_ARB:
2238 gl_error(ctx, GL_INVALID_ENUM, "glCompressedTexImage3DARB");
2239 return;
2240 default:
2241 /* silence compiler warning */
2242 ;
2243 }
2244
Brian Paul9540a1d2000-06-06 17:03:38 +00002245 if (target == GL_TEXTURE_3D) {
Brian Paulaea66b12000-05-24 14:04:06 +00002246 struct gl_texture_unit *texUnit;
2247 struct gl_texture_object *texObj;
2248 struct gl_texture_image *texImage;
2249
2250 if (texture_error_check(ctx, target, level, internalFormat,
2251 GL_NONE, GL_NONE, 1, width, height, depth, border)) {
2252 return; /* error in texture image was detected */
2253 }
2254
2255 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
Brian Paul8e39ad22001-02-06 21:42:48 +00002256 texObj = _mesa_select_tex_object(ctx, texUnit, target);
2257 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
Brian Paulaea66b12000-05-24 14:04:06 +00002258
2259 if (!texImage) {
2260 texImage = _mesa_alloc_texture_image();
2261 texObj->Image[level] = texImage;
2262 if (!texImage) {
2263 gl_error(ctx, GL_OUT_OF_MEMORY, "glCompressedTexImage3DARB");
2264 return;
2265 }
2266 }
2267 else if (texImage->Data) {
2268 FREE(texImage->Data);
2269 texImage->Data = NULL;
2270 }
2271
Brian Paul6628bc92001-02-07 03:27:41 +00002272 init_teximage_fields(ctx, texImage, width, height, depth, border,
2273 internalFormat);
2274
Brian Paul8e39ad22001-02-06 21:42:48 +00002275 if (ctx->Extensions.ARB_texture_compression) {
2276 ASSERT(ctx->Driver.CompressedTexImage3D);
2277 (*ctx->Driver.CompressedTexImage3D)(ctx, target, level,
2278 internalFormat,
2279 width, height, depth,
2280 border, imageSize, data,
2281 texObj, texImage);
2282 ASSERT(texImage->CompressedSize > 0); /* sanity */
Brian Paulaea66b12000-05-24 14:04:06 +00002283 }
2284
2285 /* state update */
Brian Paula8523782000-11-19 23:10:25 +00002286 texObj->Complete = GL_FALSE;
Keith Whitwella96308c2000-10-30 13:31:59 +00002287 ctx->NewState |= _NEW_TEXTURE;
Brian Paulaea66b12000-05-24 14:04:06 +00002288 }
2289 else if (target == GL_PROXY_TEXTURE_3D) {
2290 /* Proxy texture: check for errors and update proxy state */
Brian Paul38d3f3d2000-09-07 15:38:49 +00002291 GLenum error = texture_error_check(ctx, target, level, internalFormat,
2292 GL_NONE, GL_NONE, 1, width, height, depth, border);
Brian Paul8e39ad22001-02-06 21:42:48 +00002293 if (!error) {
Brian Paula1f15862001-02-07 16:27:41 +00002294 struct gl_texture_unit *texUnit;
2295 struct gl_texture_image *texImage;
2296 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2297 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
2298 init_teximage_fields(ctx, texImage, width, height, depth,
2299 border, internalFormat);
Brian Paul8e39ad22001-02-06 21:42:48 +00002300 ASSERT(ctx->Driver.TestProxyTexImage);
Brian Paul38d3f3d2000-09-07 15:38:49 +00002301 error = !(*ctx->Driver.TestProxyTexImage)(ctx, target, level,
2302 internalFormat, GL_NONE, GL_NONE,
2303 width, height, depth, border);
2304 }
2305 if (error) {
Brian Paulaea66b12000-05-24 14:04:06 +00002306 /* if error, clear all proxy texture image parameters */
Brian Paulf378ab82001-02-06 23:35:26 +00002307 if (level >= 0 && level < ctx->Const.MaxTextureLevels) {
2308 clear_teximage_fields(ctx->Texture.Proxy3D->Image[level]);
Brian Paulaea66b12000-05-24 14:04:06 +00002309 }
2310 }
Brian Paulaea66b12000-05-24 14:04:06 +00002311 }
2312 else {
2313 gl_error( ctx, GL_INVALID_ENUM, "glCompressedTexImage3DARB(target)" );
2314 return;
2315 }
Brian Paul1207bf02000-05-23 20:10:49 +00002316}
2317
2318
2319void
2320_mesa_CompressedTexSubImage1DARB(GLenum target, GLint level, GLint xoffset,
2321 GLsizei width, GLenum format,
2322 GLsizei imageSize, const GLvoid *data)
2323{
Brian Paul9540a1d2000-06-06 17:03:38 +00002324 struct gl_texture_unit *texUnit;
2325 struct gl_texture_object *texObj;
2326 struct gl_texture_image *texImage;
Brian Paula1f15862001-02-07 16:27:41 +00002327 GET_CURRENT_CONTEXT(ctx);
Brian Paul9540a1d2000-06-06 17:03:38 +00002328
2329 if (subtexture_error_check(ctx, 1, target, level, xoffset, 0, 0,
2330 width, 1, 1, format, GL_NONE)) {
2331 return; /* error was detected */
2332 }
2333
2334 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2335 texObj = _mesa_select_tex_object(ctx, texUnit, target);
Brian Paul8e39ad22001-02-06 21:42:48 +00002336 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
Brian Paul9540a1d2000-06-06 17:03:38 +00002337 assert(texImage);
2338
2339 if (width == 0 || !data)
2340 return; /* no-op, not an error */
2341
2342 if (ctx->Driver.CompressedTexSubImage1D) {
Brian Paul8e39ad22001-02-06 21:42:48 +00002343 (*ctx->Driver.CompressedTexSubImage1D)(ctx, target, level,
2344 xoffset, width,
2345 format, imageSize, data,
2346 texObj, texImage);
Brian Paul9540a1d2000-06-06 17:03:38 +00002347 }
Brian Paul1207bf02000-05-23 20:10:49 +00002348}
2349
2350
2351void
2352_mesa_CompressedTexSubImage2DARB(GLenum target, GLint level, GLint xoffset,
2353 GLint yoffset, GLsizei width, GLsizei height,
2354 GLenum format, GLsizei imageSize,
2355 const GLvoid *data)
2356{
Brian Paul9540a1d2000-06-06 17:03:38 +00002357 struct gl_texture_unit *texUnit;
2358 struct gl_texture_object *texObj;
2359 struct gl_texture_image *texImage;
Brian Paula1f15862001-02-07 16:27:41 +00002360 GET_CURRENT_CONTEXT(ctx);
Brian Paul9540a1d2000-06-06 17:03:38 +00002361
2362 if (subtexture_error_check(ctx, 2, target, level, xoffset, yoffset, 0,
2363 width, height, 1, format, GL_NONE)) {
2364 return; /* error was detected */
2365 }
2366
2367 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2368 texObj = _mesa_select_tex_object(ctx, texUnit, target);
Brian Paul8e39ad22001-02-06 21:42:48 +00002369 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
Brian Paul9540a1d2000-06-06 17:03:38 +00002370 assert(texImage);
2371
2372 if (width == 0 || height == 0 || !data)
2373 return; /* no-op, not an error */
2374
2375 if (ctx->Driver.CompressedTexSubImage2D) {
Brian Paul8e39ad22001-02-06 21:42:48 +00002376 (*ctx->Driver.CompressedTexSubImage2D)(ctx, target, level,
2377 xoffset, yoffset, width, height,
2378 format, imageSize, data,
2379 texObj, texImage);
Brian Paul9540a1d2000-06-06 17:03:38 +00002380 }
Brian Paul1207bf02000-05-23 20:10:49 +00002381}
2382
2383
2384void
2385_mesa_CompressedTexSubImage3DARB(GLenum target, GLint level, GLint xoffset,
2386 GLint yoffset, GLint zoffset, GLsizei width,
2387 GLsizei height, GLsizei depth, GLenum format,
2388 GLsizei imageSize, const GLvoid *data)
2389{
Brian Paul9540a1d2000-06-06 17:03:38 +00002390 struct gl_texture_unit *texUnit;
2391 struct gl_texture_object *texObj;
2392 struct gl_texture_image *texImage;
Brian Paula1f15862001-02-07 16:27:41 +00002393 GET_CURRENT_CONTEXT(ctx);
Brian Paul9540a1d2000-06-06 17:03:38 +00002394
2395 if (subtexture_error_check(ctx, 3, target, level, xoffset, yoffset, zoffset,
2396 width, height, depth, format, GL_NONE)) {
2397 return; /* error was detected */
2398 }
2399
2400 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2401 texObj = _mesa_select_tex_object(ctx, texUnit, target);
Brian Paul8e39ad22001-02-06 21:42:48 +00002402 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
Brian Paul9540a1d2000-06-06 17:03:38 +00002403 assert(texImage);
2404
2405 if (width == 0 || height == 0 || depth == 0 || !data)
2406 return; /* no-op, not an error */
2407
2408 if (ctx->Driver.CompressedTexSubImage3D) {
Brian Paul8e39ad22001-02-06 21:42:48 +00002409 (*ctx->Driver.CompressedTexSubImage3D)(ctx, target, level,
2410 xoffset, yoffset, zoffset,
2411 width, height, depth,
2412 format, imageSize, data,
2413 texObj, texImage);
Brian Paul9540a1d2000-06-06 17:03:38 +00002414 }
Brian Paul1207bf02000-05-23 20:10:49 +00002415}
2416
2417
2418void
Brian Paul9540a1d2000-06-06 17:03:38 +00002419_mesa_GetCompressedTexImageARB(GLenum target, GLint level, GLvoid *img)
Brian Paul1207bf02000-05-23 20:10:49 +00002420{
Brian Paul8e39ad22001-02-06 21:42:48 +00002421 const struct gl_texture_unit *texUnit;
Brian Paul9540a1d2000-06-06 17:03:38 +00002422 const struct gl_texture_object *texObj;
2423 struct gl_texture_image *texImage;
Brian Paula1f15862001-02-07 16:27:41 +00002424 GET_CURRENT_CONTEXT(ctx);
Brian Paul9540a1d2000-06-06 17:03:38 +00002425
Keith Whitwellcab974c2000-12-26 05:09:27 +00002426 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
Brian Paul9540a1d2000-06-06 17:03:38 +00002427
2428 if (level < 0 || level >= ctx->Const.MaxTextureLevels) {
2429 gl_error( ctx, GL_INVALID_VALUE, "glGetCompressedTexImageARB(level)" );
2430 return;
2431 }
2432
Brian Paul8e39ad22001-02-06 21:42:48 +00002433 if (is_proxy_target(target)) {
2434 gl_error(ctx, GL_INVALID_ENUM, "glGetCompressedTexImageARB(target)");
2435 return;
Brian Paul9540a1d2000-06-06 17:03:38 +00002436 }
2437
Brian Paul8e39ad22001-02-06 21:42:48 +00002438 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2439 texObj = _mesa_select_tex_object(ctx, texUnit, target);
2440 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
2441
Brian Paul9540a1d2000-06-06 17:03:38 +00002442 if (!texImage) {
2443 /* invalid mipmap level */
2444 gl_error(ctx, GL_INVALID_VALUE, "glGetCompressedTexImageARB(level)");
2445 return;
2446 }
2447
2448 if (!texImage->IsCompressed) {
2449 gl_error(ctx, GL_INVALID_OPERATION, "glGetCompressedTexImageARB");
2450 return;
2451 }
2452
2453 if (!img)
2454 return;
2455
Brian Paul8e39ad22001-02-06 21:42:48 +00002456 if (ctx->Extensions.ARB_texture_compression) {
2457 ASSERT(ctx->Driver.GetCompressedTexImage);
Brian Paul9540a1d2000-06-06 17:03:38 +00002458 (*ctx->Driver.GetCompressedTexImage)(ctx, target, level, img, texObj,
2459 texImage);
2460 }
Brian Paul1207bf02000-05-23 20:10:49 +00002461}