blob: fe5fb449810d6e24464c7fe2222c5eb3ad47b0bf [file] [log] [blame]
Brian Paulf7e1dfe2001-02-17 00:15:39 +00001/* $Id: teximage.c,v 1.76 2001/02/17 00:15:39 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++) {
1183 (*texImage->FetchTexel)(ctx, texObj, texImage, col, row, img,
1184 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++) {
1247 GLchan rgba[1];
1248 /* XXX this won't really work yet */
1249 /*need (*texImage->FetchRawTexel)() */
1250 (*texImage->FetchTexel)(ctx, texObj, texImage,
1251 col, row, img, rgba);
1252 indexRow[col] = rgba[0];
1253 }
1254 _mesa_pack_index_span(ctx, width, type, dest,
1255 indexRow, &ctx->Pack,
1256 ctx->_ImageTransferState);
1257 }
1258 else if (format == GL_DEPTH_COMPONENT) {
1259 /* XXX finish this */
1260 GLfloat depthRow[MAX_WIDTH];
1261 GLint col;
1262 for (col = 0; col < width; col++) {
1263 GLchan rgba[1];
1264 /* XXX this won't really work yet */
1265 /*need (*texImage->FetchRawTexel)() */
1266 (*texImage->FetchTexel)(ctx, texObj, texImage,
1267 col, row, img, rgba);
1268 depthRow[col] = (GLfloat) rgba[0];
1269 }
1270 _mesa_pack_depth_span(ctx, width, dest, type,
1271 depthRow, &ctx->Pack);
1272 }
1273 else {
Brian Paul8e39ad22001-02-06 21:42:48 +00001274 /* general case: convert row to RGBA format */
1275 GLchan rgba[MAX_WIDTH][4];
1276 GLint col;
1277 for (col = 0; col < width; col++) {
1278 (*texImage->FetchTexel)(ctx, texObj, texImage,
Brian Paul9a0b12a2001-02-07 18:59:45 +00001279 col, row, img, rgba[col]);
Brian Paul8e39ad22001-02-06 21:42:48 +00001280 }
Brian Paul8e39ad22001-02-06 21:42:48 +00001281 _mesa_pack_rgba_span( ctx, width, (const GLchan (*)[4])rgba,
1282 format, type, dest, &ctx->Pack,
1283 ctx->_ImageTransferState );
1284 } /* format */
1285 } /* row */
1286 } /* img */
1287 } /* convolution */
1288}
1289
1290
1291
1292/*
1293 * Called from the API. Note that width includes the border.
1294 */
1295void
1296_mesa_TexImage1D( GLenum target, GLint level, GLint internalFormat,
1297 GLsizei width, GLint border, GLenum format,
1298 GLenum type, const GLvoid *pixels )
1299{
1300 GLsizei postConvWidth = width;
1301 GET_CURRENT_CONTEXT(ctx);
1302 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
1303
Brian Paulf7e1dfe2001-02-17 00:15:39 +00001304 if (is_color_format(internalFormat)) {
1305 _mesa_adjust_image_for_convolution(ctx, 1, &postConvWidth, NULL);
1306 }
Brian Paul8e39ad22001-02-06 21:42:48 +00001307
1308 if (target == GL_TEXTURE_1D) {
1309 struct gl_texture_unit *texUnit;
1310 struct gl_texture_object *texObj;
1311 struct gl_texture_image *texImage;
1312
1313 if (texture_error_check(ctx, target, level, internalFormat,
1314 format, type, 1, postConvWidth, 1, 1, border)) {
1315 return; /* error was recorded */
1316 }
1317
1318 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1319 texObj = _mesa_select_tex_object(ctx, texUnit, target);
1320 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
1321
1322 if (!texImage) {
1323 texImage = _mesa_alloc_texture_image();
1324 texObj->Image[level] = texImage;
1325 if (!texImage) {
1326 gl_error(ctx, GL_OUT_OF_MEMORY, "glTexImage1D");
1327 return;
1328 }
1329 }
1330 else if (texImage->Data) {
1331 /* free the old texture data */
1332 FREE(texImage->Data);
1333 texImage->Data = NULL;
1334 }
Brian Paulf378ab82001-02-06 23:35:26 +00001335 clear_teximage_fields(texImage); /* not really needed, but helpful */
Brian Paul6628bc92001-02-07 03:27:41 +00001336 init_teximage_fields(ctx, texImage, postConvWidth, 1, 1,
1337 border, internalFormat);
jtgafb833d1999-08-19 00:55:39 +00001338
Brian Paul9499e012000-10-30 16:32:42 +00001339 if (ctx->NewState & _NEW_PIXEL)
1340 gl_update_state(ctx);
Brian Paulfa4525e2000-08-21 14:22:24 +00001341
Brian Paul8e39ad22001-02-06 21:42:48 +00001342 ASSERT(ctx->Driver.TexImage1D);
1343 if (pixels) {
1344 (*ctx->Driver.TexImage1D)(ctx, target, level, internalFormat,
1345 width, border, format, type, pixels,
1346 &ctx->Unpack, texObj, texImage);
jtgafb833d1999-08-19 00:55:39 +00001347 }
Brian Paulf96ce6a2000-09-06 15:15:43 +00001348 else {
Brian Paul8e39ad22001-02-06 21:42:48 +00001349 GLubyte *dummy = make_null_texture(width, 1, 1, format);
1350 if (dummy) {
1351 (*ctx->Driver.TexImage1D)(ctx, target, level, internalFormat,
1352 width, border,
1353 format, GL_UNSIGNED_BYTE, dummy,
1354 &_mesa_native_packing, texObj, texImage);
1355 FREE(dummy);
1356 }
1357 }
Brian Paulf7b57072000-03-20 14:37:52 +00001358
Brian Paulf378ab82001-02-06 23:35:26 +00001359 /* one of these has to be non-zero! */
1360 ASSERT(texImage->RedBits || texImage->IndexBits || texImage->AlphaBits ||
Brian Paulf7e1dfe2001-02-17 00:15:39 +00001361 texImage->LuminanceBits || texImage->IntensityBits ||
1362 texImage->DepthBits);
Brian Paulf378ab82001-02-06 23:35:26 +00001363 ASSERT(texImage->FetchTexel);
1364
Brian Paul8e39ad22001-02-06 21:42:48 +00001365 /* state update */
1366 texObj->Complete = GL_FALSE;
1367 ctx->NewState |= _NEW_TEXTURE;
1368 }
1369 else if (target == GL_PROXY_TEXTURE_1D) {
1370 /* Proxy texture: check for errors and update proxy state */
1371 GLenum error = texture_error_check(ctx, target, level, internalFormat,
1372 format, type, 1,
1373 postConvWidth, 1, 1, border);
1374 if (!error) {
Brian Paula1f15862001-02-07 16:27:41 +00001375 struct gl_texture_unit *texUnit;
1376 struct gl_texture_image *texImage;
1377 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1378 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
1379 init_teximage_fields(ctx, texImage, postConvWidth, 1, 1,
1380 border, internalFormat);
Brian Paul8e39ad22001-02-06 21:42:48 +00001381 ASSERT(ctx->Driver.TestProxyTexImage);
1382 error = !(*ctx->Driver.TestProxyTexImage)(ctx, target, level,
1383 internalFormat, format, type,
1384 postConvWidth, 1, 1, border);
1385 }
1386 if (error) {
1387 /* if error, clear all proxy texture image parameters */
1388 if (level >= 0 && level < ctx->Const.MaxTextureLevels) {
Brian Paulf378ab82001-02-06 23:35:26 +00001389 clear_teximage_fields(ctx->Texture.Proxy1D->Image[level]);
Brian Paul8e39ad22001-02-06 21:42:48 +00001390 }
1391 }
1392 }
1393 else {
1394 gl_error( ctx, GL_INVALID_ENUM, "glTexImage1D(target)" );
1395 return;
1396 }
1397}
1398
1399
1400void
1401_mesa_TexImage2D( GLenum target, GLint level, GLint internalFormat,
1402 GLsizei width, GLsizei height, GLint border,
1403 GLenum format, GLenum type,
1404 const GLvoid *pixels )
1405{
1406 GLsizei postConvWidth = width, postConvHeight = height;
1407 GET_CURRENT_CONTEXT(ctx);
1408 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
1409
Brian Paulf7e1dfe2001-02-17 00:15:39 +00001410 if (is_color_format(internalFormat)) {
1411 _mesa_adjust_image_for_convolution(ctx, 2, &postConvWidth,
1412 &postConvHeight);
1413 }
Brian Paul8e39ad22001-02-06 21:42:48 +00001414
1415 if (target == GL_TEXTURE_2D ||
1416 (ctx->Extensions.ARB_texture_cube_map &&
1417 target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
1418 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB)) {
1419 /* non-proxy target */
1420 struct gl_texture_unit *texUnit;
1421 struct gl_texture_object *texObj;
1422 struct gl_texture_image *texImage;
1423
1424 if (texture_error_check(ctx, target, level, internalFormat,
1425 format, type, 2, postConvWidth, postConvHeight,
1426 1, border)) {
1427 return; /* error was recorded */
1428 }
1429
1430 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1431 texObj = _mesa_select_tex_object(ctx, texUnit, target);
1432 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
1433
1434 if (!texImage) {
1435 texImage = _mesa_alloc_texture_image();
1436 set_tex_image(texObj, target, level, texImage);
1437 if (!texImage) {
1438 gl_error(ctx, GL_OUT_OF_MEMORY, "glTexImage2D");
1439 return;
1440 }
1441 }
1442 else if (texImage->Data) {
1443 /* free the old texture data */
Brian Paulf7b57072000-03-20 14:37:52 +00001444 FREE(texImage->Data);
1445 texImage->Data = NULL;
1446 }
Brian Paulf378ab82001-02-06 23:35:26 +00001447 clear_teximage_fields(texImage); /* not really needed, but helpful */
Brian Paul6628bc92001-02-07 03:27:41 +00001448 init_teximage_fields(ctx, texImage, postConvWidth, postConvHeight, 1,
1449 border, internalFormat);
Brian Paul8e39ad22001-02-06 21:42:48 +00001450
1451 if (ctx->NewState & _NEW_PIXEL)
1452 gl_update_state(ctx);
1453
1454 ASSERT(ctx->Driver.TexImage2D);
1455 if (pixels) {
1456 (*ctx->Driver.TexImage2D)(ctx, target, level, internalFormat,
1457 width, height, border, format, type, pixels,
1458 &ctx->Unpack, texObj, texImage);
1459 }
1460 else {
1461 GLubyte *dummy = make_null_texture(width, height, 1, format);
1462 if (dummy) {
1463 (*ctx->Driver.TexImage2D)(ctx, target, level, internalFormat,
1464 width, height, border,
1465 format, GL_UNSIGNED_BYTE, dummy,
1466 &_mesa_native_packing, texObj, texImage);
1467 FREE(dummy);
1468 }
1469 }
1470
Brian Paulf378ab82001-02-06 23:35:26 +00001471 /* one of these has to be non-zero! */
1472 ASSERT(texImage->RedBits || texImage->IndexBits || texImage->AlphaBits ||
Brian Paulf7e1dfe2001-02-17 00:15:39 +00001473 texImage->LuminanceBits || texImage->IntensityBits ||
1474 texImage->DepthBits);
Brian Paulf378ab82001-02-06 23:35:26 +00001475 ASSERT(texImage->FetchTexel);
1476
Brian Paul8e39ad22001-02-06 21:42:48 +00001477 /* state update */
1478 texObj->Complete = GL_FALSE;
1479 ctx->NewState |= _NEW_TEXTURE;
jtgafb833d1999-08-19 00:55:39 +00001480 }
Brian Paul8e39ad22001-02-06 21:42:48 +00001481 else if (target == GL_PROXY_TEXTURE_2D) {
1482 /* Proxy texture: check for errors and update proxy state */
1483 GLenum error = texture_error_check(ctx, target, level, internalFormat,
1484 format, type, 2,
1485 postConvWidth, postConvHeight, 1, border);
1486 if (!error) {
Brian Paula1f15862001-02-07 16:27:41 +00001487 struct gl_texture_unit *texUnit;
1488 struct gl_texture_image *texImage;
1489 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1490 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
1491 init_teximage_fields(ctx, texImage, postConvWidth, postConvHeight, 1,
1492 border, internalFormat);
Brian Paul8e39ad22001-02-06 21:42:48 +00001493 ASSERT(ctx->Driver.TestProxyTexImage);
1494 error = !(*ctx->Driver.TestProxyTexImage)(ctx, target, level,
1495 internalFormat, format, type,
1496 postConvWidth, postConvHeight, 1, border);
1497 }
1498 if (error) {
1499 /* if error, clear all proxy texture image parameters */
1500 if (level >= 0 && level < ctx->Const.MaxTextureLevels) {
Brian Paulf378ab82001-02-06 23:35:26 +00001501 clear_teximage_fields(ctx->Texture.Proxy2D->Image[level]);
Brian Paul8e39ad22001-02-06 21:42:48 +00001502 }
1503 }
1504 }
1505 else {
1506 gl_error( ctx, GL_INVALID_ENUM, "glTexImage2D(target)" );
1507 return;
1508 }
1509}
1510
1511
1512/*
1513 * Called by the API or display list executor.
1514 * Note that width and height include the border.
1515 */
1516void
1517_mesa_TexImage3D( GLenum target, GLint level, GLint internalFormat,
1518 GLsizei width, GLsizei height, GLsizei depth,
1519 GLint border, GLenum format, GLenum type,
1520 const GLvoid *pixels )
1521{
1522 GET_CURRENT_CONTEXT(ctx);
1523 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
1524
1525 if (target == GL_TEXTURE_3D) {
1526 struct gl_texture_unit *texUnit;
1527 struct gl_texture_object *texObj;
1528 struct gl_texture_image *texImage;
1529
1530 if (texture_error_check(ctx, target, level, internalFormat,
1531 format, type, 3, width, height, depth, border)) {
1532 return; /* error was recorded */
1533 }
1534
1535 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1536 texObj = _mesa_select_tex_object(ctx, texUnit, target);
1537 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
1538
1539 if (!texImage) {
1540 texImage = _mesa_alloc_texture_image();
1541 texObj->Image[level] = texImage;
1542 if (!texImage) {
1543 gl_error(ctx, GL_OUT_OF_MEMORY, "glTexImage3D");
1544 return;
1545 }
1546 }
1547 else if (texImage->Data) {
1548 FREE(texImage->Data);
1549 texImage->Data = NULL;
1550 }
Brian Paulf378ab82001-02-06 23:35:26 +00001551 clear_teximage_fields(texImage); /* not really needed, but helpful */
Brian Paul6628bc92001-02-07 03:27:41 +00001552 init_teximage_fields(ctx, texImage, width, height, depth, border,
1553 internalFormat);
Brian Paul8e39ad22001-02-06 21:42:48 +00001554
1555 if (ctx->NewState & _NEW_PIXEL)
1556 gl_update_state(ctx);
1557
1558 ASSERT(ctx->Driver.TexImage3D);
1559 if (pixels) {
1560 (*ctx->Driver.TexImage3D)(ctx, target, level, internalFormat,
1561 width, height, depth, border,
1562 format, type, pixels,
1563 &ctx->Unpack, texObj, texImage);
1564 }
1565 else {
1566 GLubyte *dummy = make_null_texture(width, height, depth, format);
1567 if (dummy) {
1568 (*ctx->Driver.TexImage3D)(ctx, target, level, internalFormat,
1569 width, height, depth, border,
1570 format, GL_UNSIGNED_BYTE, dummy,
1571 &_mesa_native_packing, texObj, texImage);
1572 FREE(dummy);
1573 }
1574 }
1575
Brian Paulf378ab82001-02-06 23:35:26 +00001576 /* one of these has to be non-zero! */
1577 ASSERT(texImage->RedBits || texImage->IndexBits || texImage->AlphaBits ||
Brian Paulf7e1dfe2001-02-17 00:15:39 +00001578 texImage->LuminanceBits || texImage->IntensityBits ||
1579 texImage->DepthBits);
Brian Paulf378ab82001-02-06 23:35:26 +00001580 ASSERT(texImage->FetchTexel);
1581
Brian Paul8e39ad22001-02-06 21:42:48 +00001582 /* state update */
1583 texObj->Complete = GL_FALSE;
1584 ctx->NewState |= _NEW_TEXTURE;
1585 }
1586 else if (target == GL_PROXY_TEXTURE_3D) {
1587 /* Proxy texture: check for errors and update proxy state */
1588 GLenum error = texture_error_check(ctx, target, level, internalFormat,
1589 format, type, 3, width, height, depth, border);
1590 if (!error) {
Brian Paula1f15862001-02-07 16:27:41 +00001591 struct gl_texture_unit *texUnit;
1592 struct gl_texture_image *texImage;
1593 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1594 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
1595 init_teximage_fields(ctx, texImage, width, height, 1,
1596 border, internalFormat);
Brian Paul8e39ad22001-02-06 21:42:48 +00001597 ASSERT(ctx->Driver.TestProxyTexImage);
1598 error = !(*ctx->Driver.TestProxyTexImage)(ctx, target, level,
1599 internalFormat, format, type,
1600 width, height, depth, border);
1601 }
1602 if (error) {
1603 /* if error, clear all proxy texture image parameters */
Brian Paulf378ab82001-02-06 23:35:26 +00001604 if (level >= 0 && level < ctx->Const.MaxTextureLevels) {
1605 clear_teximage_fields(ctx->Texture.Proxy3D->Image[level]);
Brian Paul8e39ad22001-02-06 21:42:48 +00001606 }
1607 }
1608 }
1609 else {
1610 gl_error( ctx, GL_INVALID_ENUM, "glTexImage3D(target)" );
1611 return;
1612 }
1613}
1614
1615
1616void
1617_mesa_TexImage3DEXT( GLenum target, GLint level, GLenum internalFormat,
1618 GLsizei width, GLsizei height, GLsizei depth,
1619 GLint border, GLenum format, GLenum type,
1620 const GLvoid *pixels )
1621{
1622 _mesa_TexImage3D(target, level, (GLint) internalFormat, width, height,
1623 depth, border, format, type, pixels);
jtgafb833d1999-08-19 00:55:39 +00001624}
1625
1626
1627
Brian Paulfbd8f211999-11-11 01:22:25 +00001628void
1629_mesa_TexSubImage1D( GLenum target, GLint level,
1630 GLint xoffset, GLsizei width,
1631 GLenum format, GLenum type,
1632 const GLvoid *pixels )
jtgafb833d1999-08-19 00:55:39 +00001633{
Brian Paulab6e78f2000-12-09 21:30:43 +00001634 GLsizei postConvWidth = width;
Brian Paulfbd8f211999-11-11 01:22:25 +00001635 GET_CURRENT_CONTEXT(ctx);
Brian Paul02938782000-03-22 17:38:11 +00001636 struct gl_texture_unit *texUnit;
1637 struct gl_texture_object *texObj;
1638 struct gl_texture_image *texImage;
Brian Paula805bb92000-09-02 17:52:21 +00001639
Brian Paulf7e1dfe2001-02-17 00:15:39 +00001640 /* XXX should test internal format */
1641 if (is_color_format(format)) {
1642 _mesa_adjust_image_for_convolution(ctx, 1, &postConvWidth, NULL);
1643 }
jtgafb833d1999-08-19 00:55:39 +00001644
Brian Paulc3f0a511999-11-03 17:27:05 +00001645 if (subtexture_error_check(ctx, 1, target, level, xoffset, 0, 0,
Brian Paula805bb92000-09-02 17:52:21 +00001646 postConvWidth, 1, 1, format, type)) {
Brian Paulf7b57072000-03-20 14:37:52 +00001647 return; /* error was detected */
jtgafb833d1999-08-19 00:55:39 +00001648 }
1649
Brian Paul02938782000-03-22 17:38:11 +00001650 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
Brian Paul8e39ad22001-02-06 21:42:48 +00001651 texObj = _mesa_select_tex_object(ctx, texUnit, target);
1652 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
Brian Paul02938782000-03-22 17:38:11 +00001653 assert(texImage);
jtgafb833d1999-08-19 00:55:39 +00001654
Brian Paulc3f0a511999-11-03 17:27:05 +00001655 if (width == 0 || !pixels)
1656 return; /* no-op, not an error */
jtgafb833d1999-08-19 00:55:39 +00001657
Brian Paul9499e012000-10-30 16:32:42 +00001658 if (ctx->NewState & _NEW_PIXEL)
1659 gl_update_state(ctx);
Brian Paulc3f0a511999-11-03 17:27:05 +00001660
Brian Paul8e39ad22001-02-06 21:42:48 +00001661 ASSERT(ctx->Driver.TexSubImage1D);
1662 (*ctx->Driver.TexSubImage1D)(ctx, target, level, xoffset, width,
1663 format, type, pixels, &ctx->Unpack,
1664 texObj, texImage);
jtgafb833d1999-08-19 00:55:39 +00001665}
1666
1667
Brian Paulfbd8f211999-11-11 01:22:25 +00001668void
1669_mesa_TexSubImage2D( GLenum target, GLint level,
1670 GLint xoffset, GLint yoffset,
1671 GLsizei width, GLsizei height,
1672 GLenum format, GLenum type,
1673 const GLvoid *pixels )
jtgafb833d1999-08-19 00:55:39 +00001674{
Brian Paulab6e78f2000-12-09 21:30:43 +00001675 GLsizei postConvWidth = width, postConvHeight = height;
Brian Paulfbd8f211999-11-11 01:22:25 +00001676 GET_CURRENT_CONTEXT(ctx);
Brian Paul02938782000-03-22 17:38:11 +00001677 struct gl_texture_unit *texUnit;
1678 struct gl_texture_object *texObj;
1679 struct gl_texture_image *texImage;
Brian Paula805bb92000-09-02 17:52:21 +00001680
Brian Paulf7e1dfe2001-02-17 00:15:39 +00001681 /* XXX should test internal format */
1682 if (is_color_format(format)) {
1683 _mesa_adjust_image_for_convolution(ctx, 2, &postConvWidth,
1684 &postConvHeight);
1685 }
jtgafb833d1999-08-19 00:55:39 +00001686
Brian Paulc3f0a511999-11-03 17:27:05 +00001687 if (subtexture_error_check(ctx, 2, target, level, xoffset, yoffset, 0,
Brian Paula805bb92000-09-02 17:52:21 +00001688 postConvWidth, postConvHeight, 1, format, type)) {
Brian Paulf7b57072000-03-20 14:37:52 +00001689 return; /* error was detected */
jtgafb833d1999-08-19 00:55:39 +00001690 }
1691
Brian Paul02938782000-03-22 17:38:11 +00001692 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
Brian Paul35d53012000-05-23 17:14:49 +00001693 texObj = _mesa_select_tex_object(ctx, texUnit, target);
Brian Paul8e39ad22001-02-06 21:42:48 +00001694 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
Brian Paul02938782000-03-22 17:38:11 +00001695 assert(texImage);
jtgafb833d1999-08-19 00:55:39 +00001696
Brian Paulc3f0a511999-11-03 17:27:05 +00001697 if (width == 0 || height == 0 || !pixels)
1698 return; /* no-op, not an error */
jtgafb833d1999-08-19 00:55:39 +00001699
Brian Paul9499e012000-10-30 16:32:42 +00001700 if (ctx->NewState & _NEW_PIXEL)
1701 gl_update_state(ctx);
Brian Paulfa4525e2000-08-21 14:22:24 +00001702
Brian Paul8e39ad22001-02-06 21:42:48 +00001703 ASSERT(ctx->Driver.TexSubImage2D);
1704 (*ctx->Driver.TexSubImage2D)(ctx, target, level, xoffset, yoffset,
1705 width, height, format, type, pixels,
1706 &ctx->Unpack, texObj, texImage);
jtgafb833d1999-08-19 00:55:39 +00001707}
1708
1709
1710
Brian Paulfbd8f211999-11-11 01:22:25 +00001711void
1712_mesa_TexSubImage3D( GLenum target, GLint level,
1713 GLint xoffset, GLint yoffset, GLint zoffset,
1714 GLsizei width, GLsizei height, GLsizei depth,
1715 GLenum format, GLenum type,
1716 const GLvoid *pixels )
jtgafb833d1999-08-19 00:55:39 +00001717{
Brian Paulfbd8f211999-11-11 01:22:25 +00001718 GET_CURRENT_CONTEXT(ctx);
Brian Paul02938782000-03-22 17:38:11 +00001719 struct gl_texture_unit *texUnit;
1720 struct gl_texture_object *texObj;
1721 struct gl_texture_image *texImage;
jtgafb833d1999-08-19 00:55:39 +00001722
Brian Paulc3f0a511999-11-03 17:27:05 +00001723 if (subtexture_error_check(ctx, 3, target, level, xoffset, yoffset, zoffset,
1724 width, height, depth, format, type)) {
Brian Paulf7b57072000-03-20 14:37:52 +00001725 return; /* error was detected */
jtgafb833d1999-08-19 00:55:39 +00001726 }
1727
Brian Paul02938782000-03-22 17:38:11 +00001728 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
Brian Paul8e39ad22001-02-06 21:42:48 +00001729 texObj = _mesa_select_tex_object(ctx, texUnit, target);
1730 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
Brian Paul02938782000-03-22 17:38:11 +00001731 assert(texImage);
jtgafb833d1999-08-19 00:55:39 +00001732
Brian Paulc3f0a511999-11-03 17:27:05 +00001733 if (width == 0 || height == 0 || height == 0 || !pixels)
1734 return; /* no-op, not an error */
jtgafb833d1999-08-19 00:55:39 +00001735
Brian Paul9499e012000-10-30 16:32:42 +00001736 if (ctx->NewState & _NEW_PIXEL)
1737 gl_update_state(ctx);
Brian Paulfa4525e2000-08-21 14:22:24 +00001738
Brian Paul8e39ad22001-02-06 21:42:48 +00001739 ASSERT(ctx->Driver.TexSubImage3D);
1740 (*ctx->Driver.TexSubImage3D)(ctx, target, level,
1741 xoffset, yoffset, zoffset,
1742 width, height, depth,
1743 format, type, pixels,
1744 &ctx->Unpack, texObj, texImage );
jtgafb833d1999-08-19 00:55:39 +00001745}
1746
1747
1748
1749/*
1750 * Read an RGBA image from the frame buffer.
Brian Paula805bb92000-09-02 17:52:21 +00001751 * This is used by glCopyTex[Sub]Image[12]D().
jtgafb833d1999-08-19 00:55:39 +00001752 * Input: ctx - the context
1753 * x, y - lower left corner
1754 * width, height - size of region to read
Brian Paul699bc7b2000-10-29 18:12:14 +00001755 * Return: pointer to block of GL_RGBA, GLchan data.
jtgafb833d1999-08-19 00:55:39 +00001756 */
Brian Paul699bc7b2000-10-29 18:12:14 +00001757static GLchan *
Brian Paulc3f0a511999-11-03 17:27:05 +00001758read_color_image( GLcontext *ctx, GLint x, GLint y,
1759 GLsizei width, GLsizei height )
jtgafb833d1999-08-19 00:55:39 +00001760{
Brian Paulc3f0a511999-11-03 17:27:05 +00001761 GLint stride, i;
Brian Paul699bc7b2000-10-29 18:12:14 +00001762 GLchan *image, *dst;
jtgafb833d1999-08-19 00:55:39 +00001763
Brian Paul699bc7b2000-10-29 18:12:14 +00001764 image = (GLchan *) MALLOC(width * height * 4 * sizeof(GLchan));
Brian Paulc3f0a511999-11-03 17:27:05 +00001765 if (!image)
jtgafb833d1999-08-19 00:55:39 +00001766 return NULL;
jtgafb833d1999-08-19 00:55:39 +00001767
1768 /* Select buffer to read from */
Brian Paulcea0e8e1999-11-25 17:36:48 +00001769 (*ctx->Driver.SetReadBuffer)( ctx, ctx->ReadBuffer,
1770 ctx->Pixel.DriverReadBuffer );
jtgafb833d1999-08-19 00:55:39 +00001771
Brian Paulc34cea72000-11-21 23:25:40 +00001772 RENDER_START(ctx);
1773
Brian Paulc3f0a511999-11-03 17:27:05 +00001774 dst = image;
Brian Paul699bc7b2000-10-29 18:12:14 +00001775 stride = width * 4;
Brian Paulc3f0a511999-11-03 17:27:05 +00001776 for (i = 0; i < height; i++) {
Brian Paul3f02f901999-11-24 18:48:30 +00001777 gl_read_rgba_span( ctx, ctx->ReadBuffer, width, x, y + i,
Brian Paul699bc7b2000-10-29 18:12:14 +00001778 (GLchan (*)[4]) dst );
Brian Paulc3f0a511999-11-03 17:27:05 +00001779 dst += stride;
1780 }
jtgafb833d1999-08-19 00:55:39 +00001781
Brian Paulc34cea72000-11-21 23:25:40 +00001782 RENDER_FINISH(ctx);
1783
Brian Paulcea0e8e1999-11-25 17:36:48 +00001784 /* Read from draw buffer (the default) */
1785 (*ctx->Driver.SetReadBuffer)( ctx, ctx->DrawBuffer,
1786 ctx->Color.DriverDrawBuffer );
jtgafb833d1999-08-19 00:55:39 +00001787
1788 return image;
1789}
1790
1791
1792
Brian Paulfbd8f211999-11-11 01:22:25 +00001793void
1794_mesa_CopyTexImage1D( GLenum target, GLint level,
1795 GLenum internalFormat,
1796 GLint x, GLint y,
1797 GLsizei width, GLint border )
jtgafb833d1999-08-19 00:55:39 +00001798{
Brian Paulab6e78f2000-12-09 21:30:43 +00001799 GLsizei postConvWidth = width;
Brian Paulfbd8f211999-11-11 01:22:25 +00001800 GET_CURRENT_CONTEXT(ctx);
Keith Whitwellcab974c2000-12-26 05:09:27 +00001801 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
jtgafb833d1999-08-19 00:55:39 +00001802
Brian Paul9499e012000-10-30 16:32:42 +00001803 if (ctx->NewState & _NEW_PIXEL)
1804 gl_update_state(ctx);
Brian Paulfa4525e2000-08-21 14:22:24 +00001805
Brian Paulf7e1dfe2001-02-17 00:15:39 +00001806 if (is_color_format(internalFormat)) {
1807 _mesa_adjust_image_for_convolution(ctx, 1, &postConvWidth, NULL);
1808 }
Brian Paulab6e78f2000-12-09 21:30:43 +00001809
1810 if (copytexture_error_check(ctx, 1, target, level, internalFormat,
1811 postConvWidth, 1, border))
1812 return;
1813
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +00001814 if (ctx->_ImageTransferState || !ctx->Driver.CopyTexImage1D
Brian Paulf7b57072000-03-20 14:37:52 +00001815 || !(*ctx->Driver.CopyTexImage1D)(ctx, target, level,
Brian Paula805bb92000-09-02 17:52:21 +00001816 internalFormat, x, y, width, border)) {
Brian Paul7dac1322000-06-15 19:57:14 +00001817 struct gl_pixelstore_attrib unpackSave;
1818
1819 /* get image from framebuffer */
Brian Paul699bc7b2000-10-29 18:12:14 +00001820 GLchan *image = read_color_image( ctx, x, y, width, 1 );
Brian Paulc3f0a511999-11-03 17:27:05 +00001821 if (!image) {
1822 gl_error( ctx, GL_OUT_OF_MEMORY, "glCopyTexImage1D" );
1823 return;
1824 }
Brian Paul7dac1322000-06-15 19:57:14 +00001825
1826 /* call glTexImage1D to redefine the texture */
1827 unpackSave = ctx->Unpack;
1828 ctx->Unpack = _mesa_native_packing;
Brian Paul3ab6bbe2000-02-12 17:26:15 +00001829 (*ctx->Exec->TexImage1D)( target, level, internalFormat, width,
Brian Paulf7b57072000-03-20 14:37:52 +00001830 border, GL_RGBA, GL_UNSIGNED_BYTE, image );
Brian Paul7dac1322000-06-15 19:57:14 +00001831 ctx->Unpack = unpackSave;
1832
Brian Paulc3f0a511999-11-03 17:27:05 +00001833 FREE(image);
jtgafb833d1999-08-19 00:55:39 +00001834 }
jtgafb833d1999-08-19 00:55:39 +00001835}
1836
1837
1838
Brian Paulfbd8f211999-11-11 01:22:25 +00001839void
1840_mesa_CopyTexImage2D( GLenum target, GLint level, GLenum internalFormat,
1841 GLint x, GLint y, GLsizei width, GLsizei height,
1842 GLint border )
jtgafb833d1999-08-19 00:55:39 +00001843{
Brian Paulab6e78f2000-12-09 21:30:43 +00001844 GLsizei postConvWidth = width, postConvHeight = height;
Brian Paulfbd8f211999-11-11 01:22:25 +00001845 GET_CURRENT_CONTEXT(ctx);
Keith Whitwellcab974c2000-12-26 05:09:27 +00001846 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
jtgafb833d1999-08-19 00:55:39 +00001847
Brian Paul9499e012000-10-30 16:32:42 +00001848 if (ctx->NewState & _NEW_PIXEL)
1849 gl_update_state(ctx);
Brian Paulfa4525e2000-08-21 14:22:24 +00001850
Brian Paulf7e1dfe2001-02-17 00:15:39 +00001851 if (is_color_format(internalFormat)) {
1852 _mesa_adjust_image_for_convolution(ctx, 2, &postConvWidth,
1853 &postConvHeight);
1854 }
Brian Paulab6e78f2000-12-09 21:30:43 +00001855
1856 if (copytexture_error_check(ctx, 2, target, level, internalFormat,
1857 postConvWidth, postConvHeight, border))
1858 return;
1859
Keith Whitwell14940c42000-11-05 18:40:57 +00001860 if (ctx->_ImageTransferState || !ctx->Driver.CopyTexImage2D
Brian Paulf7b57072000-03-20 14:37:52 +00001861 || !(*ctx->Driver.CopyTexImage2D)(ctx, target, level,
Brian Paula805bb92000-09-02 17:52:21 +00001862 internalFormat, x, y, width, height, border)) {
Brian Paul7dac1322000-06-15 19:57:14 +00001863 struct gl_pixelstore_attrib unpackSave;
1864
1865 /* get image from framebuffer */
Brian Paul699bc7b2000-10-29 18:12:14 +00001866 GLchan *image = read_color_image( ctx, x, y, width, height );
Brian Paulc3f0a511999-11-03 17:27:05 +00001867 if (!image) {
1868 gl_error( ctx, GL_OUT_OF_MEMORY, "glCopyTexImage2D" );
1869 return;
1870 }
Brian Paul7dac1322000-06-15 19:57:14 +00001871
1872 /* call glTexImage2D to redefine the texture */
1873 unpackSave = ctx->Unpack;
1874 ctx->Unpack = _mesa_native_packing;
Brian Paul3ab6bbe2000-02-12 17:26:15 +00001875 (ctx->Exec->TexImage2D)( target, level, internalFormat, width,
Brian Paulf7b57072000-03-20 14:37:52 +00001876 height, border, GL_RGBA, GL_UNSIGNED_BYTE, image );
Brian Paul7dac1322000-06-15 19:57:14 +00001877 ctx->Unpack = unpackSave;
1878
Brian Paulc3f0a511999-11-03 17:27:05 +00001879 FREE(image);
jtgafb833d1999-08-19 00:55:39 +00001880 }
jtgafb833d1999-08-19 00:55:39 +00001881}
1882
1883
1884
Brian Paulfbd8f211999-11-11 01:22:25 +00001885void
1886_mesa_CopyTexSubImage1D( GLenum target, GLint level,
1887 GLint xoffset, GLint x, GLint y, GLsizei width )
jtgafb833d1999-08-19 00:55:39 +00001888{
Brian Paulab6e78f2000-12-09 21:30:43 +00001889 GLsizei postConvWidth = width;
Brian Paulfbd8f211999-11-11 01:22:25 +00001890 GET_CURRENT_CONTEXT(ctx);
Keith Whitwellcab974c2000-12-26 05:09:27 +00001891 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
jtgafb833d1999-08-19 00:55:39 +00001892
Brian Paul9499e012000-10-30 16:32:42 +00001893 if (ctx->NewState & _NEW_PIXEL)
1894 gl_update_state(ctx);
Brian Paulfa4525e2000-08-21 14:22:24 +00001895
Brian Paulf7e1dfe2001-02-17 00:15:39 +00001896 /* XXX should test internal format */
Brian Paul8e39ad22001-02-06 21:42:48 +00001897 _mesa_adjust_image_for_convolution(ctx, 1, &postConvWidth, NULL);
Brian Paulab6e78f2000-12-09 21:30:43 +00001898
1899 if (copytexsubimage_error_check(ctx, 1, target, level,
1900 xoffset, 0, 0, postConvWidth, 1))
1901 return;
1902
Keith Whitwell14940c42000-11-05 18:40:57 +00001903 if (ctx->_ImageTransferState || !ctx->Driver.CopyTexSubImage1D
Brian Paulf7b57072000-03-20 14:37:52 +00001904 || !(*ctx->Driver.CopyTexSubImage1D)(ctx, target, level,
1905 xoffset, x, y, width)) {
Brian Paulc3f0a511999-11-03 17:27:05 +00001906 struct gl_texture_unit *texUnit;
1907 struct gl_texture_image *teximage;
Brian Paul7dac1322000-06-15 19:57:14 +00001908 struct gl_pixelstore_attrib unpackSave;
Brian Paul699bc7b2000-10-29 18:12:14 +00001909 GLchan *image;
Brian Paul7dac1322000-06-15 19:57:14 +00001910
Brian Paulc3f0a511999-11-03 17:27:05 +00001911 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
Brian Paula8523782000-11-19 23:10:25 +00001912 teximage = texUnit->Current1D->Image[level];
Brian Paulc3f0a511999-11-03 17:27:05 +00001913 assert(teximage);
Brian Paul7dac1322000-06-15 19:57:14 +00001914
1915 /* get image from frame buffer */
1916 image = read_color_image(ctx, x, y, width, 1);
1917 if (!image) {
Brian Paulab6e78f2000-12-09 21:30:43 +00001918 gl_error( ctx, GL_OUT_OF_MEMORY, "glCopyTexSubImage1D" );
Brian Paul7dac1322000-06-15 19:57:14 +00001919 return;
jtgafb833d1999-08-19 00:55:39 +00001920 }
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +00001921
Brian Paul7dac1322000-06-15 19:57:14 +00001922 /* now call glTexSubImage1D to do the real work */
1923 unpackSave = ctx->Unpack;
1924 ctx->Unpack = _mesa_native_packing;
1925 _mesa_TexSubImage1D(target, level, xoffset, width,
1926 GL_RGBA, GL_UNSIGNED_BYTE, image);
1927 ctx->Unpack = unpackSave;
1928
1929 FREE(image);
jtgafb833d1999-08-19 00:55:39 +00001930 }
Brian Paulc3f0a511999-11-03 17:27:05 +00001931}
1932
1933
1934
Brian Paulfbd8f211999-11-11 01:22:25 +00001935void
1936_mesa_CopyTexSubImage2D( GLenum target, GLint level,
1937 GLint xoffset, GLint yoffset,
1938 GLint x, GLint y, GLsizei width, GLsizei height )
Brian Paulc3f0a511999-11-03 17:27:05 +00001939{
Brian Paulab6e78f2000-12-09 21:30:43 +00001940 GLsizei postConvWidth = width, postConvHeight = height;
Brian Paulfbd8f211999-11-11 01:22:25 +00001941 GET_CURRENT_CONTEXT(ctx);
Keith Whitwellcab974c2000-12-26 05:09:27 +00001942 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
Brian Paulc3f0a511999-11-03 17:27:05 +00001943
Brian Paul9499e012000-10-30 16:32:42 +00001944 if (ctx->NewState & _NEW_PIXEL)
1945 gl_update_state(ctx);
Brian Paulfa4525e2000-08-21 14:22:24 +00001946
Brian Paulf7e1dfe2001-02-17 00:15:39 +00001947 /* XXX should test internal format */
1948 _mesa_adjust_image_for_convolution(ctx, 2, &postConvWidth, &postConvHeight);
Brian Paulab6e78f2000-12-09 21:30:43 +00001949
1950 if (copytexsubimage_error_check(ctx, 2, target, level, xoffset, yoffset, 0,
1951 postConvWidth, postConvHeight))
1952 return;
1953
Keith Whitwell14940c42000-11-05 18:40:57 +00001954 if (ctx->_ImageTransferState || !ctx->Driver.CopyTexSubImage2D
Brian Paulf7b57072000-03-20 14:37:52 +00001955 || !(*ctx->Driver.CopyTexSubImage2D)(ctx, target, level,
1956 xoffset, yoffset, x, y, width, height )) {
Brian Paulc3f0a511999-11-03 17:27:05 +00001957 struct gl_texture_unit *texUnit;
1958 struct gl_texture_image *teximage;
Brian Paul7dac1322000-06-15 19:57:14 +00001959 struct gl_pixelstore_attrib unpackSave;
Brian Paul699bc7b2000-10-29 18:12:14 +00001960 GLchan *image;
Brian Paul7dac1322000-06-15 19:57:14 +00001961
Brian Paulc3f0a511999-11-03 17:27:05 +00001962 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
Brian Paula8523782000-11-19 23:10:25 +00001963 teximage = texUnit->Current2D->Image[level];
Brian Paulc3f0a511999-11-03 17:27:05 +00001964 assert(teximage);
Brian Paul7dac1322000-06-15 19:57:14 +00001965
1966 /* get image from frame buffer */
1967 image = read_color_image(ctx, x, y, width, height);
1968 if (!image) {
1969 gl_error( ctx, GL_OUT_OF_MEMORY, "glCopyTexSubImage2D" );
1970 return;
Brian Paulc3f0a511999-11-03 17:27:05 +00001971 }
Brian Paul7dac1322000-06-15 19:57:14 +00001972
1973 /* now call glTexSubImage2D to do the real work */
1974 unpackSave = ctx->Unpack;
1975 ctx->Unpack = _mesa_native_packing;
1976 _mesa_TexSubImage2D(target, level, xoffset, yoffset, width, height,
1977 GL_RGBA, GL_UNSIGNED_BYTE, image);
1978 ctx->Unpack = unpackSave;
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +00001979
Brian Paul7dac1322000-06-15 19:57:14 +00001980 FREE(image);
Brian Paulc3f0a511999-11-03 17:27:05 +00001981 }
1982}
1983
1984
1985
Brian Paulfbd8f211999-11-11 01:22:25 +00001986void
1987_mesa_CopyTexSubImage3D( GLenum target, GLint level,
1988 GLint xoffset, GLint yoffset, GLint zoffset,
1989 GLint x, GLint y, GLsizei width, GLsizei height )
Brian Paulc3f0a511999-11-03 17:27:05 +00001990{
Brian Paulab6e78f2000-12-09 21:30:43 +00001991 GLsizei postConvWidth = width, postConvHeight = height;
Brian Paulfbd8f211999-11-11 01:22:25 +00001992 GET_CURRENT_CONTEXT(ctx);
Keith Whitwellcab974c2000-12-26 05:09:27 +00001993 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
Brian Paulc3f0a511999-11-03 17:27:05 +00001994
Brian Paul9499e012000-10-30 16:32:42 +00001995 if (ctx->NewState & _NEW_PIXEL)
1996 gl_update_state(ctx);
Brian Paulfa4525e2000-08-21 14:22:24 +00001997
Brian Paulf7e1dfe2001-02-17 00:15:39 +00001998 /* XXX should test internal format */
1999 _mesa_adjust_image_for_convolution(ctx, 2, &postConvWidth, &postConvHeight);
Brian Paulab6e78f2000-12-09 21:30:43 +00002000
2001 if (copytexsubimage_error_check(ctx, 3, target, level, xoffset, yoffset,
2002 zoffset, postConvWidth, postConvHeight))
2003 return;
2004
Keith Whitwell14940c42000-11-05 18:40:57 +00002005 if (ctx->_ImageTransferState || !ctx->Driver.CopyTexSubImage3D
Brian Paulf7b57072000-03-20 14:37:52 +00002006 || !(*ctx->Driver.CopyTexSubImage3D)(ctx, target, level,
Brian Paul02938782000-03-22 17:38:11 +00002007 xoffset, yoffset, zoffset, x, y, width, height )) {
2008 struct gl_texture_unit *texUnit;
2009 struct gl_texture_image *teximage;
Brian Paul7dac1322000-06-15 19:57:14 +00002010 struct gl_pixelstore_attrib unpackSave;
Brian Paul699bc7b2000-10-29 18:12:14 +00002011 GLchan *image;
Brian Paul7dac1322000-06-15 19:57:14 +00002012
Brian Paul02938782000-03-22 17:38:11 +00002013 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
Brian Paula8523782000-11-19 23:10:25 +00002014 teximage = texUnit->Current3D->Image[level];
Brian Paul02938782000-03-22 17:38:11 +00002015 assert(teximage);
Brian Paul7dac1322000-06-15 19:57:14 +00002016
2017 /* get image from frame buffer */
2018 image = read_color_image(ctx, x, y, width, height);
2019 if (!image) {
2020 gl_error( ctx, GL_OUT_OF_MEMORY, "glCopyTexSubImage2D" );
2021 return;
Brian Paulc3f0a511999-11-03 17:27:05 +00002022 }
Brian Paul7dac1322000-06-15 19:57:14 +00002023
2024 /* now call glTexSubImage2D to do the real work */
2025 unpackSave = ctx->Unpack;
2026 ctx->Unpack = _mesa_native_packing;
2027 _mesa_TexSubImage3D(target, level, xoffset, yoffset, zoffset,
2028 width, height, 1, GL_RGBA, GL_UNSIGNED_BYTE, image);
2029 ctx->Unpack = unpackSave;
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +00002030
Brian Paul7dac1322000-06-15 19:57:14 +00002031 FREE(image);
jtgafb833d1999-08-19 00:55:39 +00002032 }
2033}
Brian Paul1207bf02000-05-23 20:10:49 +00002034
2035
2036
2037void
2038_mesa_CompressedTexImage1DARB(GLenum target, GLint level,
Brian Paulaea66b12000-05-24 14:04:06 +00002039 GLenum internalFormat, GLsizei width,
Brian Paul1207bf02000-05-23 20:10:49 +00002040 GLint border, GLsizei imageSize,
2041 const GLvoid *data)
2042{
Brian Paulaea66b12000-05-24 14:04:06 +00002043 GET_CURRENT_CONTEXT(ctx);
Keith Whitwellcab974c2000-12-26 05:09:27 +00002044 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
Brian Paulaea66b12000-05-24 14:04:06 +00002045
Brian Paul289d47e2000-08-29 23:31:23 +00002046 switch (internalFormat) {
2047 case GL_COMPRESSED_ALPHA_ARB:
2048 case GL_COMPRESSED_LUMINANCE_ARB:
2049 case GL_COMPRESSED_LUMINANCE_ALPHA_ARB:
2050 case GL_COMPRESSED_INTENSITY_ARB:
2051 case GL_COMPRESSED_RGB_ARB:
2052 case GL_COMPRESSED_RGBA_ARB:
2053 gl_error(ctx, GL_INVALID_ENUM, "glCompressedTexImage1DARB");
2054 return;
2055 default:
2056 /* silence compiler warning */
2057 ;
2058 }
2059
Brian Paulaea66b12000-05-24 14:04:06 +00002060 if (target == GL_TEXTURE_1D) {
2061 struct gl_texture_unit *texUnit;
2062 struct gl_texture_object *texObj;
2063 struct gl_texture_image *texImage;
2064
2065 if (texture_error_check(ctx, target, level, internalFormat,
2066 GL_NONE, GL_NONE, 1, width, 1, 1, border)) {
2067 return; /* error in texture image was detected */
2068 }
2069
2070 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
Brian Paul8e39ad22001-02-06 21:42:48 +00002071 texObj = _mesa_select_tex_object(ctx, texUnit, target);
2072 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
Brian Paulaea66b12000-05-24 14:04:06 +00002073
2074 if (!texImage) {
2075 texImage = _mesa_alloc_texture_image();
2076 texObj->Image[level] = texImage;
2077 if (!texImage) {
2078 gl_error(ctx, GL_OUT_OF_MEMORY, "glCompressedTexImage1DARB");
2079 return;
2080 }
2081 }
2082 else if (texImage->Data) {
2083 FREE(texImage->Data);
2084 texImage->Data = NULL;
2085 }
2086
Brian Paul6628bc92001-02-07 03:27:41 +00002087 init_teximage_fields(ctx, texImage, width, 1, 1, border, internalFormat);
2088
Brian Paul8e39ad22001-02-06 21:42:48 +00002089 if (ctx->Extensions.ARB_texture_compression) {
2090 ASSERT(ctx->Driver.CompressedTexImage1D);
2091 (*ctx->Driver.CompressedTexImage1D)(ctx, target, level,
2092 internalFormat, width, border,
2093 imageSize, data,
2094 texObj, texImage);
2095 ASSERT(texImage->CompressedSize > 0); /* sanity */
Brian Paulaea66b12000-05-24 14:04:06 +00002096 }
2097
2098 /* state update */
Brian Paula8523782000-11-19 23:10:25 +00002099 texObj->Complete = GL_FALSE;
Keith Whitwella96308c2000-10-30 13:31:59 +00002100 ctx->NewState |= _NEW_TEXTURE;
Brian Paulaea66b12000-05-24 14:04:06 +00002101 }
2102 else if (target == GL_PROXY_TEXTURE_1D) {
2103 /* Proxy texture: check for errors and update proxy state */
Brian Paul38d3f3d2000-09-07 15:38:49 +00002104 GLenum error = texture_error_check(ctx, target, level, internalFormat,
2105 GL_NONE, GL_NONE, 1, width, 1, 1, border);
Brian Paul8e39ad22001-02-06 21:42:48 +00002106 if (!error) {
Brian Paula1f15862001-02-07 16:27:41 +00002107 struct gl_texture_unit *texUnit;
2108 struct gl_texture_image *texImage;
2109 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2110 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
2111 init_teximage_fields(ctx, texImage, width, 1, 1,
2112 border, internalFormat);
Brian Paul8e39ad22001-02-06 21:42:48 +00002113 ASSERT(ctx->Driver.TestProxyTexImage);
Brian Paul38d3f3d2000-09-07 15:38:49 +00002114 error = !(*ctx->Driver.TestProxyTexImage)(ctx, target, level,
2115 internalFormat, GL_NONE, GL_NONE,
2116 width, 1, 1, border);
2117 }
2118 if (error) {
Brian Paulaea66b12000-05-24 14:04:06 +00002119 /* if error, clear all proxy texture image parameters */
Brian Paulf378ab82001-02-06 23:35:26 +00002120 if (level >= 0 && level < ctx->Const.MaxTextureLevels) {
2121 clear_teximage_fields(ctx->Texture.Proxy1D->Image[level]);
Brian Paulaea66b12000-05-24 14:04:06 +00002122 }
2123 }
Brian Paulaea66b12000-05-24 14:04:06 +00002124 }
2125 else {
2126 gl_error( ctx, GL_INVALID_ENUM, "glCompressedTexImage1DARB(target)" );
2127 return;
2128 }
Brian Paul1207bf02000-05-23 20:10:49 +00002129}
2130
2131
2132void
2133_mesa_CompressedTexImage2DARB(GLenum target, GLint level,
Brian Paulaea66b12000-05-24 14:04:06 +00002134 GLenum internalFormat, GLsizei width,
Brian Paul1207bf02000-05-23 20:10:49 +00002135 GLsizei height, GLint border, GLsizei imageSize,
2136 const GLvoid *data)
2137{
Brian Paulaea66b12000-05-24 14:04:06 +00002138 GET_CURRENT_CONTEXT(ctx);
Keith Whitwellcab974c2000-12-26 05:09:27 +00002139 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
Brian Paulaea66b12000-05-24 14:04:06 +00002140
Brian Paul289d47e2000-08-29 23:31:23 +00002141 switch (internalFormat) {
2142 case GL_COMPRESSED_ALPHA_ARB:
2143 case GL_COMPRESSED_LUMINANCE_ARB:
2144 case GL_COMPRESSED_LUMINANCE_ALPHA_ARB:
2145 case GL_COMPRESSED_INTENSITY_ARB:
2146 case GL_COMPRESSED_RGB_ARB:
2147 case GL_COMPRESSED_RGBA_ARB:
2148 gl_error(ctx, GL_INVALID_ENUM, "glCompressedTexImage2DARB");
2149 return;
2150 default:
2151 /* silence compiler warning */
2152 ;
2153 }
2154
Brian Paul8e39ad22001-02-06 21:42:48 +00002155 if (target == GL_TEXTURE_2D ||
Keith Whitwella96308c2000-10-30 13:31:59 +00002156 (ctx->Extensions.ARB_texture_cube_map &&
Brian Paul9540a1d2000-06-06 17:03:38 +00002157 target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
2158 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB)) {
Brian Paulaea66b12000-05-24 14:04:06 +00002159 struct gl_texture_unit *texUnit;
2160 struct gl_texture_object *texObj;
2161 struct gl_texture_image *texImage;
2162
2163 if (texture_error_check(ctx, target, level, internalFormat,
2164 GL_NONE, GL_NONE, 1, width, height, 1, border)) {
2165 return; /* error in texture image was detected */
2166 }
2167
2168 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
Brian Paul8e39ad22001-02-06 21:42:48 +00002169 texObj = _mesa_select_tex_object(ctx, texUnit, target);
2170 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
Brian Paulaea66b12000-05-24 14:04:06 +00002171
2172 if (!texImage) {
2173 texImage = _mesa_alloc_texture_image();
2174 texObj->Image[level] = texImage;
2175 if (!texImage) {
2176 gl_error(ctx, GL_OUT_OF_MEMORY, "glCompressedTexImage2DARB");
2177 return;
2178 }
2179 }
2180 else if (texImage->Data) {
2181 FREE(texImage->Data);
2182 texImage->Data = NULL;
2183 }
2184
Brian Paul6628bc92001-02-07 03:27:41 +00002185 init_teximage_fields(ctx, texImage, width, height, 1, border,
2186 internalFormat);
2187
Brian Paul8e39ad22001-02-06 21:42:48 +00002188 if (ctx->Extensions.ARB_texture_compression) {
2189 ASSERT(ctx->Driver.CompressedTexImage2D);
2190 (*ctx->Driver.CompressedTexImage2D)(ctx, target, level,
2191 internalFormat, width, height,
2192 border, imageSize, data,
2193 texObj, texImage);
2194 ASSERT(texImage->CompressedSize > 0); /* sanity */
Brian Paulaea66b12000-05-24 14:04:06 +00002195 }
2196
2197 /* state update */
Brian Paula8523782000-11-19 23:10:25 +00002198 texObj->Complete = GL_FALSE;
Keith Whitwella96308c2000-10-30 13:31:59 +00002199 ctx->NewState |= _NEW_TEXTURE;
Brian Paulaea66b12000-05-24 14:04:06 +00002200 }
2201 else if (target == GL_PROXY_TEXTURE_2D) {
2202 /* Proxy texture: check for errors and update proxy state */
Brian Paul38d3f3d2000-09-07 15:38:49 +00002203 GLenum error = texture_error_check(ctx, target, level, internalFormat,
2204 GL_NONE, GL_NONE, 2, width, height, 1, border);
Brian Paul8e39ad22001-02-06 21:42:48 +00002205 if (!error) {
Brian Paula1f15862001-02-07 16:27:41 +00002206 struct gl_texture_unit *texUnit;
2207 struct gl_texture_image *texImage;
2208 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2209 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
2210 init_teximage_fields(ctx, texImage, width, height, 1,
2211 border, internalFormat);
Brian Paul8e39ad22001-02-06 21:42:48 +00002212 ASSERT(ctx->Driver.TestProxyTexImage);
Brian Paul38d3f3d2000-09-07 15:38:49 +00002213 error = !(*ctx->Driver.TestProxyTexImage)(ctx, target, level,
2214 internalFormat, GL_NONE, GL_NONE,
2215 width, height, 1, border);
2216 }
2217 if (error) {
Brian Paulaea66b12000-05-24 14:04:06 +00002218 /* if error, clear all proxy texture image parameters */
Brian Paulf378ab82001-02-06 23:35:26 +00002219 if (level >= 0 && level < ctx->Const.MaxTextureLevels) {
2220 clear_teximage_fields(ctx->Texture.Proxy2D->Image[level]);
Brian Paulaea66b12000-05-24 14:04:06 +00002221 }
2222 }
Brian Paulaea66b12000-05-24 14:04:06 +00002223 }
2224 else {
2225 gl_error( ctx, GL_INVALID_ENUM, "glCompressedTexImage2DARB(target)" );
2226 return;
2227 }
Brian Paul1207bf02000-05-23 20:10:49 +00002228}
2229
2230
2231void
2232_mesa_CompressedTexImage3DARB(GLenum target, GLint level,
Brian Paulaea66b12000-05-24 14:04:06 +00002233 GLenum internalFormat, GLsizei width,
Brian Paul1207bf02000-05-23 20:10:49 +00002234 GLsizei height, GLsizei depth, GLint border,
2235 GLsizei imageSize, const GLvoid *data)
2236{
Brian Paulaea66b12000-05-24 14:04:06 +00002237 GET_CURRENT_CONTEXT(ctx);
Keith Whitwellcab974c2000-12-26 05:09:27 +00002238 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
Brian Paulaea66b12000-05-24 14:04:06 +00002239
Brian Paul289d47e2000-08-29 23:31:23 +00002240 switch (internalFormat) {
2241 case GL_COMPRESSED_ALPHA_ARB:
2242 case GL_COMPRESSED_LUMINANCE_ARB:
2243 case GL_COMPRESSED_LUMINANCE_ALPHA_ARB:
2244 case GL_COMPRESSED_INTENSITY_ARB:
2245 case GL_COMPRESSED_RGB_ARB:
2246 case GL_COMPRESSED_RGBA_ARB:
2247 gl_error(ctx, GL_INVALID_ENUM, "glCompressedTexImage3DARB");
2248 return;
2249 default:
2250 /* silence compiler warning */
2251 ;
2252 }
2253
Brian Paul9540a1d2000-06-06 17:03:38 +00002254 if (target == GL_TEXTURE_3D) {
Brian Paulaea66b12000-05-24 14:04:06 +00002255 struct gl_texture_unit *texUnit;
2256 struct gl_texture_object *texObj;
2257 struct gl_texture_image *texImage;
2258
2259 if (texture_error_check(ctx, target, level, internalFormat,
2260 GL_NONE, GL_NONE, 1, width, height, depth, border)) {
2261 return; /* error in texture image was detected */
2262 }
2263
2264 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
Brian Paul8e39ad22001-02-06 21:42:48 +00002265 texObj = _mesa_select_tex_object(ctx, texUnit, target);
2266 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
Brian Paulaea66b12000-05-24 14:04:06 +00002267
2268 if (!texImage) {
2269 texImage = _mesa_alloc_texture_image();
2270 texObj->Image[level] = texImage;
2271 if (!texImage) {
2272 gl_error(ctx, GL_OUT_OF_MEMORY, "glCompressedTexImage3DARB");
2273 return;
2274 }
2275 }
2276 else if (texImage->Data) {
2277 FREE(texImage->Data);
2278 texImage->Data = NULL;
2279 }
2280
Brian Paul6628bc92001-02-07 03:27:41 +00002281 init_teximage_fields(ctx, texImage, width, height, depth, border,
2282 internalFormat);
2283
Brian Paul8e39ad22001-02-06 21:42:48 +00002284 if (ctx->Extensions.ARB_texture_compression) {
2285 ASSERT(ctx->Driver.CompressedTexImage3D);
2286 (*ctx->Driver.CompressedTexImage3D)(ctx, target, level,
2287 internalFormat,
2288 width, height, depth,
2289 border, imageSize, data,
2290 texObj, texImage);
2291 ASSERT(texImage->CompressedSize > 0); /* sanity */
Brian Paulaea66b12000-05-24 14:04:06 +00002292 }
2293
2294 /* state update */
Brian Paula8523782000-11-19 23:10:25 +00002295 texObj->Complete = GL_FALSE;
Keith Whitwella96308c2000-10-30 13:31:59 +00002296 ctx->NewState |= _NEW_TEXTURE;
Brian Paulaea66b12000-05-24 14:04:06 +00002297 }
2298 else if (target == GL_PROXY_TEXTURE_3D) {
2299 /* Proxy texture: check for errors and update proxy state */
Brian Paul38d3f3d2000-09-07 15:38:49 +00002300 GLenum error = texture_error_check(ctx, target, level, internalFormat,
2301 GL_NONE, GL_NONE, 1, width, height, depth, border);
Brian Paul8e39ad22001-02-06 21:42:48 +00002302 if (!error) {
Brian Paula1f15862001-02-07 16:27:41 +00002303 struct gl_texture_unit *texUnit;
2304 struct gl_texture_image *texImage;
2305 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2306 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
2307 init_teximage_fields(ctx, texImage, width, height, depth,
2308 border, internalFormat);
Brian Paul8e39ad22001-02-06 21:42:48 +00002309 ASSERT(ctx->Driver.TestProxyTexImage);
Brian Paul38d3f3d2000-09-07 15:38:49 +00002310 error = !(*ctx->Driver.TestProxyTexImage)(ctx, target, level,
2311 internalFormat, GL_NONE, GL_NONE,
2312 width, height, depth, border);
2313 }
2314 if (error) {
Brian Paulaea66b12000-05-24 14:04:06 +00002315 /* if error, clear all proxy texture image parameters */
Brian Paulf378ab82001-02-06 23:35:26 +00002316 if (level >= 0 && level < ctx->Const.MaxTextureLevels) {
2317 clear_teximage_fields(ctx->Texture.Proxy3D->Image[level]);
Brian Paulaea66b12000-05-24 14:04:06 +00002318 }
2319 }
Brian Paulaea66b12000-05-24 14:04:06 +00002320 }
2321 else {
2322 gl_error( ctx, GL_INVALID_ENUM, "glCompressedTexImage3DARB(target)" );
2323 return;
2324 }
Brian Paul1207bf02000-05-23 20:10:49 +00002325}
2326
2327
2328void
2329_mesa_CompressedTexSubImage1DARB(GLenum target, GLint level, GLint xoffset,
2330 GLsizei width, GLenum format,
2331 GLsizei imageSize, const GLvoid *data)
2332{
Brian Paul9540a1d2000-06-06 17:03:38 +00002333 struct gl_texture_unit *texUnit;
2334 struct gl_texture_object *texObj;
2335 struct gl_texture_image *texImage;
Brian Paula1f15862001-02-07 16:27:41 +00002336 GET_CURRENT_CONTEXT(ctx);
Brian Paul9540a1d2000-06-06 17:03:38 +00002337
2338 if (subtexture_error_check(ctx, 1, target, level, xoffset, 0, 0,
2339 width, 1, 1, format, GL_NONE)) {
2340 return; /* error was detected */
2341 }
2342
2343 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2344 texObj = _mesa_select_tex_object(ctx, texUnit, target);
Brian Paul8e39ad22001-02-06 21:42:48 +00002345 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
Brian Paul9540a1d2000-06-06 17:03:38 +00002346 assert(texImage);
2347
2348 if (width == 0 || !data)
2349 return; /* no-op, not an error */
2350
2351 if (ctx->Driver.CompressedTexSubImage1D) {
Brian Paul8e39ad22001-02-06 21:42:48 +00002352 (*ctx->Driver.CompressedTexSubImage1D)(ctx, target, level,
2353 xoffset, width,
2354 format, imageSize, data,
2355 texObj, texImage);
Brian Paul9540a1d2000-06-06 17:03:38 +00002356 }
Brian Paul1207bf02000-05-23 20:10:49 +00002357}
2358
2359
2360void
2361_mesa_CompressedTexSubImage2DARB(GLenum target, GLint level, GLint xoffset,
2362 GLint yoffset, GLsizei width, GLsizei height,
2363 GLenum format, GLsizei imageSize,
2364 const GLvoid *data)
2365{
Brian Paul9540a1d2000-06-06 17:03:38 +00002366 struct gl_texture_unit *texUnit;
2367 struct gl_texture_object *texObj;
2368 struct gl_texture_image *texImage;
Brian Paula1f15862001-02-07 16:27:41 +00002369 GET_CURRENT_CONTEXT(ctx);
Brian Paul9540a1d2000-06-06 17:03:38 +00002370
2371 if (subtexture_error_check(ctx, 2, target, level, xoffset, yoffset, 0,
2372 width, height, 1, format, GL_NONE)) {
2373 return; /* error was detected */
2374 }
2375
2376 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2377 texObj = _mesa_select_tex_object(ctx, texUnit, target);
Brian Paul8e39ad22001-02-06 21:42:48 +00002378 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
Brian Paul9540a1d2000-06-06 17:03:38 +00002379 assert(texImage);
2380
2381 if (width == 0 || height == 0 || !data)
2382 return; /* no-op, not an error */
2383
2384 if (ctx->Driver.CompressedTexSubImage2D) {
Brian Paul8e39ad22001-02-06 21:42:48 +00002385 (*ctx->Driver.CompressedTexSubImage2D)(ctx, target, level,
2386 xoffset, yoffset, width, height,
2387 format, imageSize, data,
2388 texObj, texImage);
Brian Paul9540a1d2000-06-06 17:03:38 +00002389 }
Brian Paul1207bf02000-05-23 20:10:49 +00002390}
2391
2392
2393void
2394_mesa_CompressedTexSubImage3DARB(GLenum target, GLint level, GLint xoffset,
2395 GLint yoffset, GLint zoffset, GLsizei width,
2396 GLsizei height, GLsizei depth, GLenum format,
2397 GLsizei imageSize, const GLvoid *data)
2398{
Brian Paul9540a1d2000-06-06 17:03:38 +00002399 struct gl_texture_unit *texUnit;
2400 struct gl_texture_object *texObj;
2401 struct gl_texture_image *texImage;
Brian Paula1f15862001-02-07 16:27:41 +00002402 GET_CURRENT_CONTEXT(ctx);
Brian Paul9540a1d2000-06-06 17:03:38 +00002403
2404 if (subtexture_error_check(ctx, 3, target, level, xoffset, yoffset, zoffset,
2405 width, height, depth, format, GL_NONE)) {
2406 return; /* error was detected */
2407 }
2408
2409 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2410 texObj = _mesa_select_tex_object(ctx, texUnit, target);
Brian Paul8e39ad22001-02-06 21:42:48 +00002411 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
Brian Paul9540a1d2000-06-06 17:03:38 +00002412 assert(texImage);
2413
2414 if (width == 0 || height == 0 || depth == 0 || !data)
2415 return; /* no-op, not an error */
2416
2417 if (ctx->Driver.CompressedTexSubImage3D) {
Brian Paul8e39ad22001-02-06 21:42:48 +00002418 (*ctx->Driver.CompressedTexSubImage3D)(ctx, target, level,
2419 xoffset, yoffset, zoffset,
2420 width, height, depth,
2421 format, imageSize, data,
2422 texObj, texImage);
Brian Paul9540a1d2000-06-06 17:03:38 +00002423 }
Brian Paul1207bf02000-05-23 20:10:49 +00002424}
2425
2426
2427void
Brian Paul9540a1d2000-06-06 17:03:38 +00002428_mesa_GetCompressedTexImageARB(GLenum target, GLint level, GLvoid *img)
Brian Paul1207bf02000-05-23 20:10:49 +00002429{
Brian Paul8e39ad22001-02-06 21:42:48 +00002430 const struct gl_texture_unit *texUnit;
Brian Paul9540a1d2000-06-06 17:03:38 +00002431 const struct gl_texture_object *texObj;
2432 struct gl_texture_image *texImage;
Brian Paula1f15862001-02-07 16:27:41 +00002433 GET_CURRENT_CONTEXT(ctx);
Brian Paul9540a1d2000-06-06 17:03:38 +00002434
Keith Whitwellcab974c2000-12-26 05:09:27 +00002435 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
Brian Paul9540a1d2000-06-06 17:03:38 +00002436
2437 if (level < 0 || level >= ctx->Const.MaxTextureLevels) {
2438 gl_error( ctx, GL_INVALID_VALUE, "glGetCompressedTexImageARB(level)" );
2439 return;
2440 }
2441
Brian Paul8e39ad22001-02-06 21:42:48 +00002442 if (is_proxy_target(target)) {
2443 gl_error(ctx, GL_INVALID_ENUM, "glGetCompressedTexImageARB(target)");
2444 return;
Brian Paul9540a1d2000-06-06 17:03:38 +00002445 }
2446
Brian Paul8e39ad22001-02-06 21:42:48 +00002447 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2448 texObj = _mesa_select_tex_object(ctx, texUnit, target);
2449 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
2450
Brian Paul9540a1d2000-06-06 17:03:38 +00002451 if (!texImage) {
2452 /* invalid mipmap level */
2453 gl_error(ctx, GL_INVALID_VALUE, "glGetCompressedTexImageARB(level)");
2454 return;
2455 }
2456
2457 if (!texImage->IsCompressed) {
2458 gl_error(ctx, GL_INVALID_OPERATION, "glGetCompressedTexImageARB");
2459 return;
2460 }
2461
2462 if (!img)
2463 return;
2464
Brian Paul8e39ad22001-02-06 21:42:48 +00002465 if (ctx->Extensions.ARB_texture_compression) {
2466 ASSERT(ctx->Driver.GetCompressedTexImage);
Brian Paul9540a1d2000-06-06 17:03:38 +00002467 (*ctx->Driver.GetCompressedTexImage)(ctx, target, level, img, texObj,
2468 texImage);
2469 }
Brian Paul1207bf02000-05-23 20:10:49 +00002470}