b60a91f370377a5ead70a6fb126c74cce63df5f0
[mesa.git] / src / mesa / main / teximage.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.1
4 *
5 * Copyright (C) 1999-2004 Brian Paul All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25
26 /**
27 * \file teximage.c
28 * Texture images manipulation functions.
29 *
30 * \note Mesa's native texture data type is GLchan. Native formats are
31 * GL_ALPHA, GL_LUMINANCE, GL_LUMANCE_ALPHA, GL_INTENSITY, GL_RGB, GL_RGBA, and
32 * GL_COLOR_INDEX.
33 *
34 * \note Device drivers are free to implement any internal format they want.
35 */
36
37
38 #include "glheader.h"
39 #include "context.h"
40 #include "convolve.h"
41 #include "image.h"
42 #include "imports.h"
43 #include "macros.h"
44 #include "state.h"
45 #include "texcompress.h"
46 #include "texformat.h"
47 #include "teximage.h"
48 #include "texstate.h"
49 #include "texstore.h"
50 #include "mtypes.h"
51
52
53 #if 0
54 static void PrintTexture(GLcontext *ctx, const struct gl_texture_image *img)
55 {
56 #if CHAN_TYPE == GL_FLOAT
57 _mesa_problem(NULL, "PrintTexture doesn't support float channels");
58 #else
59 GLuint i, j, c;
60 const GLchan *data = (const GLchan *) img->Data;
61
62 if (!data) {
63 _mesa_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 _mesa_problem(NULL, "error in PrintTexture\n");
85 return;
86 }
87
88 for (i = 0; i < img->Height; i++) {
89 for (j = 0; j < img->Width; j++) {
90 if (c==1)
91 _mesa_printf("%02x ", data[0]);
92 else if (c==2)
93 _mesa_printf("%02x%02x ", data[0], data[1]);
94 else if (c==3)
95 _mesa_printf("%02x%02x%02x ", data[0], data[1], data[2]);
96 else if (c==4)
97 _mesa_printf("%02x%02x%02x%02x ", data[0], data[1], data[2], data[3]);
98 data += (img->RowStride - img->Width) * c;
99 }
100 _mesa_printf("\n");
101 }
102 #endif
103 }
104 #endif
105
106
107 /*
108 * Compute floor(log_base_2(n)).
109 * If n < 0 return -1.
110 */
111 static int
112 logbase2( int n )
113 {
114 GLint i = 1;
115 GLint log2 = 0;
116
117 if (n < 0)
118 return -1;
119
120 if (n == 0)
121 return 0;
122
123 while ( n > i ) {
124 i *= 2;
125 log2++;
126 }
127 if (i != n) {
128 return log2 - 1;
129 }
130 else {
131 return log2;
132 }
133 }
134
135
136
137 /**
138 * Return the simple base format for a given internal texture format.
139 * For example, given GL_LUMINANCE12_ALPHA4, return GL_LUMINANCE_ALPHA.
140 *
141 * \param ctx GL context.
142 * \param internalFormat the internal texture format token or 1, 2, 3, or 4.
143 *
144 * \return the corresponding \u base internal format (GL_ALPHA, GL_LUMINANCE,
145 * GL_LUMANCE_ALPHA, GL_INTENSITY, GL_RGB, or GL_RGBA), or -1 if invalid enum.
146 *
147 * This is the format which is used during texture application (i.e. the
148 * texture format and env mode determine the arithmetic used.
149 */
150 GLint
151 _mesa_base_tex_format( GLcontext *ctx, GLint internalFormat )
152 {
153 switch (internalFormat) {
154 case GL_ALPHA:
155 case GL_ALPHA4:
156 case GL_ALPHA8:
157 case GL_ALPHA12:
158 case GL_ALPHA16:
159 return GL_ALPHA;
160 case 1:
161 case GL_LUMINANCE:
162 case GL_LUMINANCE4:
163 case GL_LUMINANCE8:
164 case GL_LUMINANCE12:
165 case GL_LUMINANCE16:
166 return GL_LUMINANCE;
167 case 2:
168 case GL_LUMINANCE_ALPHA:
169 case GL_LUMINANCE4_ALPHA4:
170 case GL_LUMINANCE6_ALPHA2:
171 case GL_LUMINANCE8_ALPHA8:
172 case GL_LUMINANCE12_ALPHA4:
173 case GL_LUMINANCE12_ALPHA12:
174 case GL_LUMINANCE16_ALPHA16:
175 return GL_LUMINANCE_ALPHA;
176 case GL_INTENSITY:
177 case GL_INTENSITY4:
178 case GL_INTENSITY8:
179 case GL_INTENSITY12:
180 case GL_INTENSITY16:
181 return GL_INTENSITY;
182 case 3:
183 case GL_RGB:
184 case GL_R3_G3_B2:
185 case GL_RGB4:
186 case GL_RGB5:
187 case GL_RGB8:
188 case GL_RGB10:
189 case GL_RGB12:
190 case GL_RGB16:
191 return GL_RGB;
192 case 4:
193 case GL_RGBA:
194 case GL_RGBA2:
195 case GL_RGBA4:
196 case GL_RGB5_A1:
197 case GL_RGBA8:
198 case GL_RGB10_A2:
199 case GL_RGBA12:
200 case GL_RGBA16:
201 return GL_RGBA;
202 default:
203 ; /* fallthrough */
204 }
205
206 if (ctx->Extensions.EXT_paletted_texture) {
207 switch (internalFormat) {
208 case GL_COLOR_INDEX:
209 case GL_COLOR_INDEX1_EXT:
210 case GL_COLOR_INDEX2_EXT:
211 case GL_COLOR_INDEX4_EXT:
212 case GL_COLOR_INDEX8_EXT:
213 case GL_COLOR_INDEX12_EXT:
214 case GL_COLOR_INDEX16_EXT:
215 return GL_COLOR_INDEX;
216 default:
217 ; /* fallthrough */
218 }
219 }
220
221 if (ctx->Extensions.SGIX_depth_texture) {
222 switch (internalFormat) {
223 case GL_DEPTH_COMPONENT:
224 case GL_DEPTH_COMPONENT16_SGIX:
225 case GL_DEPTH_COMPONENT24_SGIX:
226 case GL_DEPTH_COMPONENT32_SGIX:
227 return GL_DEPTH_COMPONENT;
228 default:
229 ; /* fallthrough */
230 }
231 }
232
233 if (ctx->Extensions.ARB_texture_compression) {
234 switch (internalFormat) {
235 case GL_COMPRESSED_ALPHA:
236 return GL_ALPHA;
237 case GL_COMPRESSED_LUMINANCE:
238 return GL_LUMINANCE;
239 case GL_COMPRESSED_LUMINANCE_ALPHA:
240 return GL_LUMINANCE_ALPHA;
241 case GL_COMPRESSED_INTENSITY:
242 return GL_INTENSITY;
243 case GL_COMPRESSED_RGB:
244 return GL_RGB;
245 case GL_COMPRESSED_RGBA:
246 return GL_RGBA;
247 default:
248 ; /* fallthrough */
249 }
250 }
251
252 if (ctx->Extensions.TDFX_texture_compression_FXT1) {
253 switch (internalFormat) {
254 case GL_COMPRESSED_RGB_FXT1_3DFX:
255 return GL_RGB;
256 case GL_COMPRESSED_RGBA_FXT1_3DFX:
257 return GL_RGBA;
258 default:
259 ; /* fallthrough */
260 }
261 }
262
263 if (ctx->Extensions.EXT_texture_compression_s3tc) {
264 switch (internalFormat) {
265 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
266 return GL_RGB;
267 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
268 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
269 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
270 return GL_RGBA;
271 default:
272 ; /* fallthrough */
273 }
274 }
275
276 if (ctx->Extensions.S3_s3tc) {
277 switch (internalFormat) {
278 case GL_RGB_S3TC:
279 case GL_RGB4_S3TC:
280 return GL_RGB;
281 case GL_RGBA_S3TC:
282 case GL_RGBA4_S3TC:
283 return GL_RGBA;
284 default:
285 ; /* fallthrough */
286 }
287 }
288
289 if (ctx->Extensions.MESA_ycbcr_texture) {
290 if (internalFormat == GL_YCBCR_MESA)
291 return GL_YCBCR_MESA;
292 }
293
294 if (ctx->Extensions.ARB_texture_float) {
295 switch (internalFormat) {
296 case GL_ALPHA16F_ARB:
297 case GL_ALPHA32F_ARB:
298 return GL_ALPHA;
299 case GL_RGBA16F_ARB:
300 case GL_RGBA32F_ARB:
301 return GL_RGBA;
302 case GL_RGB16F_ARB:
303 case GL_RGB32F_ARB:
304 return GL_RGB;
305 case GL_INTENSITY16F_ARB:
306 case GL_INTENSITY32F_ARB:
307 return GL_INTENSITY;
308 case GL_LUMINANCE16F_ARB:
309 case GL_LUMINANCE32F_ARB:
310 return GL_LUMINANCE;
311 case GL_LUMINANCE_ALPHA16F_ARB:
312 case GL_LUMINANCE_ALPHA32F_ARB:
313 return GL_LUMINANCE_ALPHA;
314 default:
315 ; /* fallthrough */
316 }
317 }
318
319 return -1; /* error */
320 }
321
322
323 /**
324 * Test if the given image format is a color/RGBA format (i.e., not color
325 * index, depth, stencil, etc).
326 * \param format the image format value (may by an internal texture format)
327 * \return GL_TRUE if its a color/RGBA format, GL_FALSE otherwise.
328 */
329 static GLboolean
330 is_color_format(GLenum format)
331 {
332 switch (format) {
333 case GL_ALPHA:
334 case GL_ALPHA4:
335 case GL_ALPHA8:
336 case GL_ALPHA12:
337 case GL_ALPHA16:
338 case 1:
339 case GL_LUMINANCE:
340 case GL_LUMINANCE4:
341 case GL_LUMINANCE8:
342 case GL_LUMINANCE12:
343 case GL_LUMINANCE16:
344 case 2:
345 case GL_LUMINANCE_ALPHA:
346 case GL_LUMINANCE4_ALPHA4:
347 case GL_LUMINANCE6_ALPHA2:
348 case GL_LUMINANCE8_ALPHA8:
349 case GL_LUMINANCE12_ALPHA4:
350 case GL_LUMINANCE12_ALPHA12:
351 case GL_LUMINANCE16_ALPHA16:
352 case GL_INTENSITY:
353 case GL_INTENSITY4:
354 case GL_INTENSITY8:
355 case GL_INTENSITY12:
356 case GL_INTENSITY16:
357 case 3:
358 case GL_RGB:
359 case GL_BGR:
360 case GL_R3_G3_B2:
361 case GL_RGB4:
362 case GL_RGB5:
363 case GL_RGB8:
364 case GL_RGB10:
365 case GL_RGB12:
366 case GL_RGB16:
367 case 4:
368 case GL_ABGR_EXT:
369 case GL_RGBA:
370 case GL_BGRA:
371 case GL_RGBA2:
372 case GL_RGBA4:
373 case GL_RGB5_A1:
374 case GL_RGBA8:
375 case GL_RGB10_A2:
376 case GL_RGBA12:
377 case GL_RGBA16:
378 /* float texture formats */
379 case GL_ALPHA16F_ARB:
380 case GL_ALPHA32F_ARB:
381 case GL_LUMINANCE16F_ARB:
382 case GL_LUMINANCE32F_ARB:
383 case GL_LUMINANCE_ALPHA16F_ARB:
384 case GL_LUMINANCE_ALPHA32F_ARB:
385 case GL_INTENSITY16F_ARB:
386 case GL_INTENSITY32F_ARB:
387 case GL_RGB16F_ARB:
388 case GL_RGB32F_ARB:
389 case GL_RGBA16F_ARB:
390 case GL_RGBA32F_ARB:
391 /* compressed formats */
392 case GL_COMPRESSED_ALPHA:
393 case GL_COMPRESSED_LUMINANCE:
394 case GL_COMPRESSED_LUMINANCE_ALPHA:
395 case GL_COMPRESSED_INTENSITY:
396 case GL_COMPRESSED_RGB:
397 case GL_COMPRESSED_RGBA:
398 case GL_RGB_S3TC:
399 case GL_RGB4_S3TC:
400 case GL_RGBA_S3TC:
401 case GL_RGBA4_S3TC:
402 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
403 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
404 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
405 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
406 case GL_COMPRESSED_RGB_FXT1_3DFX:
407 case GL_COMPRESSED_RGBA_FXT1_3DFX:
408 return GL_TRUE;
409 case GL_YCBCR_MESA: /* not considered to be RGB */
410 default:
411 return GL_FALSE;
412 }
413 }
414
415
416 /**
417 * Test if the given image format is a color index format.
418 */
419 static GLboolean
420 is_index_format(GLenum format)
421 {
422 switch (format) {
423 case GL_COLOR_INDEX:
424 case GL_COLOR_INDEX1_EXT:
425 case GL_COLOR_INDEX2_EXT:
426 case GL_COLOR_INDEX4_EXT:
427 case GL_COLOR_INDEX8_EXT:
428 case GL_COLOR_INDEX12_EXT:
429 case GL_COLOR_INDEX16_EXT:
430 return GL_TRUE;
431 default:
432 return GL_FALSE;
433 }
434 }
435
436
437 /**
438 * Test if the given image format is a depth component format.
439 */
440 static GLboolean
441 is_depth_format(GLenum format)
442 {
443 switch (format) {
444 case GL_DEPTH_COMPONENT16_ARB:
445 case GL_DEPTH_COMPONENT24_ARB:
446 case GL_DEPTH_COMPONENT32_ARB:
447 case GL_DEPTH_COMPONENT:
448 return GL_TRUE;
449 default:
450 return GL_FALSE;
451 }
452 }
453
454
455 /**
456 * Test if the given image format is a YCbCr format.
457 */
458 static GLboolean
459 is_ycbcr_format(GLenum format)
460 {
461 switch (format) {
462 case GL_YCBCR_MESA:
463 return GL_TRUE;
464 default:
465 return GL_FALSE;
466 }
467 }
468
469
470 /**
471 * Test if it is a supported compressed format.
472 *
473 * \param internalFormat the internal format token provided by the user.
474 *
475 * \ret GL_TRUE if \p internalFormat is a supported compressed format, or
476 * GL_FALSE otherwise.
477 *
478 * Currently only GL_COMPRESSED_RGB_FXT1_3DFX and GL_COMPRESSED_RGBA_FXT1_3DFX
479 * are supported.
480 */
481 static GLboolean
482 is_compressed_format(GLcontext *ctx, GLenum internalFormat)
483 {
484 (void) ctx;
485 switch (internalFormat) {
486 case GL_COMPRESSED_RGB_FXT1_3DFX:
487 case GL_COMPRESSED_RGBA_FXT1_3DFX:
488 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
489 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
490 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
491 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
492 case GL_RGB_S3TC:
493 case GL_RGB4_S3TC:
494 case GL_RGBA_S3TC:
495 case GL_RGBA4_S3TC:
496 return GL_TRUE;
497 default:
498 return GL_FALSE;
499 }
500 }
501
502
503 /**
504 * Store a gl_texture_image pointer in a gl_texture_object structure
505 * according to the target and level parameters.
506 *
507 * \param tObj texture object.
508 * \param target texture target.
509 * \param level image level.
510 * \param texImage texture image.
511 *
512 * This was basically prompted by the introduction of cube maps.
513 */
514 void
515 _mesa_set_tex_image(struct gl_texture_object *tObj,
516 GLenum target, GLint level,
517 struct gl_texture_image *texImage)
518 {
519 ASSERT(tObj);
520 ASSERT(texImage);
521 switch (target) {
522 case GL_TEXTURE_1D:
523 case GL_TEXTURE_2D:
524 case GL_TEXTURE_3D:
525 tObj->Image[0][level] = texImage;
526 break;
527 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
528 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
529 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
530 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
531 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
532 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB: {
533 GLuint face = ((GLuint) target -
534 (GLuint) GL_TEXTURE_CUBE_MAP_POSITIVE_X);
535 tObj->Image[face][level] = texImage;
536 break;
537 }
538 case GL_TEXTURE_RECTANGLE_NV:
539 ASSERT(level == 0);
540 tObj->Image[0][level] = texImage;
541 break;
542 default:
543 _mesa_problem(NULL, "bad target in _mesa_set_tex_image()");
544 return;
545 }
546 /* Set the 'back' pointer */
547 texImage->TexObject = tObj;
548 }
549
550
551 /**
552 * Allocate a texture image structure.
553 *
554 * Called via ctx->Driver.NewTextureImage() unless overriden by a device
555 * driver.
556 *
557 * \return a pointer to gl_texture_image struct with all fields initialized to
558 * zero.
559 */
560 struct gl_texture_image *
561 _mesa_new_texture_image( GLcontext *ctx )
562 {
563 (void) ctx;
564 return CALLOC_STRUCT(gl_texture_image);
565 }
566
567
568 /**
569 * Free texture image.
570 *
571 * \param teximage texture image.
572 *
573 * Free the texture image structure and the associated image data if it's not
574 * marked as client data.
575 */
576 void
577 _mesa_delete_texture_image( struct gl_texture_image *teximage )
578 {
579 if (teximage->Data && !teximage->IsClientData) {
580 MESA_PBUFFER_FREE( teximage->Data );
581 teximage->Data = NULL;
582 }
583 FREE( teximage );
584 }
585
586
587 /**
588 * Test if a target is a proxy target.
589 *
590 * \param target texture target.
591 *
592 * \return GL_TRUE if the target is a proxy target, GL_FALSE otherwise.
593 */
594 static GLboolean
595 is_proxy_target(GLenum target)
596 {
597 return (target == GL_PROXY_TEXTURE_1D ||
598 target == GL_PROXY_TEXTURE_2D ||
599 target == GL_PROXY_TEXTURE_3D ||
600 target == GL_PROXY_TEXTURE_CUBE_MAP_ARB ||
601 target == GL_PROXY_TEXTURE_RECTANGLE_NV);
602 }
603
604
605 /**
606 * Get the texture object that corresponds to the target of the given texture unit.
607 *
608 * \param ctx GL context.
609 * \param texUnit texture unit.
610 * \param target texture target.
611 *
612 * \return pointer to the texture object on success, or NULL on failure.
613 *
614 * \sa gl_texture_unit.
615 */
616 struct gl_texture_object *
617 _mesa_select_tex_object(GLcontext *ctx, const struct gl_texture_unit *texUnit,
618 GLenum target)
619 {
620 switch (target) {
621 case GL_TEXTURE_1D:
622 return texUnit->Current1D;
623 case GL_PROXY_TEXTURE_1D:
624 return ctx->Texture.Proxy1D;
625 case GL_TEXTURE_2D:
626 return texUnit->Current2D;
627 case GL_PROXY_TEXTURE_2D:
628 return ctx->Texture.Proxy2D;
629 case GL_TEXTURE_3D:
630 return texUnit->Current3D;
631 case GL_PROXY_TEXTURE_3D:
632 return ctx->Texture.Proxy3D;
633 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
634 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
635 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
636 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
637 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
638 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
639 case GL_TEXTURE_CUBE_MAP_ARB:
640 return ctx->Extensions.ARB_texture_cube_map
641 ? texUnit->CurrentCubeMap : NULL;
642 case GL_PROXY_TEXTURE_CUBE_MAP_ARB:
643 return ctx->Extensions.ARB_texture_cube_map
644 ? ctx->Texture.ProxyCubeMap : NULL;
645 case GL_TEXTURE_RECTANGLE_NV:
646 return ctx->Extensions.NV_texture_rectangle
647 ? texUnit->CurrentRect : NULL;
648 case GL_PROXY_TEXTURE_RECTANGLE_NV:
649 return ctx->Extensions.NV_texture_rectangle
650 ? ctx->Texture.ProxyRect : NULL;
651 default:
652 _mesa_problem(NULL, "bad target in _mesa_select_tex_object()");
653 return NULL;
654 }
655 }
656
657
658 /**
659 * Get the texture image struct which corresponds to target and level
660 * of the given texture unit.
661 *
662 * \param ctx GL context.
663 * \param texUnit texture unit.
664 * \param target texture target.
665 * \param level image level.
666 *
667 * \return pointer to the texture image structure on success, or NULL on failure.
668 *
669 * \sa gl_texture_unit.
670 */
671 struct gl_texture_image *
672 _mesa_select_tex_image(GLcontext *ctx, const struct gl_texture_unit *texUnit,
673 GLenum target, GLint level)
674 {
675 ASSERT(texUnit);
676 ASSERT(level < MAX_TEXTURE_LEVELS);
677 switch (target) {
678 case GL_TEXTURE_1D:
679 return texUnit->Current1D->Image[0][level];
680 case GL_PROXY_TEXTURE_1D:
681 return ctx->Texture.Proxy1D->Image[0][level];
682 case GL_TEXTURE_2D:
683 return texUnit->Current2D->Image[0][level];
684 case GL_PROXY_TEXTURE_2D:
685 return ctx->Texture.Proxy2D->Image[0][level];
686 case GL_TEXTURE_3D:
687 return texUnit->Current3D->Image[0][level];
688 case GL_PROXY_TEXTURE_3D:
689 return ctx->Texture.Proxy3D->Image[0][level];
690 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
691 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
692 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
693 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
694 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
695 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
696 if (ctx->Extensions.ARB_texture_cube_map) {
697 GLuint face = ((GLuint) target -
698 (GLuint) GL_TEXTURE_CUBE_MAP_POSITIVE_X);
699 return texUnit->CurrentCubeMap->Image[face][level];
700 }
701 else
702 return NULL;
703 case GL_PROXY_TEXTURE_CUBE_MAP_ARB:
704 if (ctx->Extensions.ARB_texture_cube_map)
705 return ctx->Texture.ProxyCubeMap->Image[0][level];
706 else
707 return NULL;
708 case GL_TEXTURE_RECTANGLE_NV:
709 if (ctx->Extensions.NV_texture_rectangle) {
710 ASSERT(level == 0);
711 return texUnit->CurrentRect->Image[0][level];
712 }
713 else {
714 return NULL;
715 }
716 case GL_PROXY_TEXTURE_RECTANGLE_NV:
717 if (ctx->Extensions.NV_texture_rectangle) {
718 ASSERT(level == 0);
719 return ctx->Texture.ProxyRect->Image[0][level];
720 }
721 else {
722 return NULL;
723 }
724 default:
725 _mesa_problem(ctx, "bad target in _mesa_select_tex_image()");
726 return NULL;
727 }
728 }
729
730
731 /**
732 * Like _mesa_select_tex_image() but if the image doesn't exist, allocate
733 * it and install it. Only return NULL if passed a bad parameter or run
734 * out of memory.
735 */
736 struct gl_texture_image *
737 _mesa_get_tex_image(GLcontext *ctx, const struct gl_texture_unit *texUnit,
738 GLenum target, GLint level)
739 {
740 struct gl_texture_image *texImage;
741 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
742 if (!texImage) {
743 struct gl_texture_object *texObj;
744 texImage = ctx->Driver.NewTextureImage(ctx);
745 if (!texImage) {
746 _mesa_error(ctx, GL_OUT_OF_MEMORY, "texture image allocation");
747 return NULL;
748 }
749 texObj = _mesa_select_tex_object(ctx, texUnit, target);
750 ASSERT(texObj);
751 _mesa_set_tex_image(texObj, target, level, texImage);
752 }
753 return texImage;
754 }
755
756
757 /**
758 * Return pointer to the specified proxy texture image.
759 * Note that proxy textures are per-context, not per-texture unit.
760 * \return pointer to texture image or NULL if invalid target, invalid
761 * level, or out of memory.
762 */
763 struct gl_texture_image *
764 _mesa_get_proxy_tex_image(GLcontext *ctx, GLenum target, GLint level)
765 {
766 struct gl_texture_image *texImage;
767
768 if (level < 0 )
769 return NULL;
770
771 switch (target) {
772 case GL_PROXY_TEXTURE_1D:
773 if (level >= ctx->Const.MaxTextureLevels)
774 return NULL;
775 texImage = ctx->Texture.Proxy1D->Image[0][level];
776 if (!texImage) {
777 texImage = ctx->Driver.NewTextureImage(ctx);
778 if (!texImage) {
779 _mesa_error(ctx, GL_OUT_OF_MEMORY, "proxy texture allocation");
780 return NULL;
781 }
782 ctx->Texture.Proxy1D->Image[0][level] = texImage;
783 /* Set the 'back' pointer */
784 texImage->TexObject = ctx->Texture.Proxy1D;
785 }
786 return texImage;
787 case GL_PROXY_TEXTURE_2D:
788 if (level >= ctx->Const.MaxTextureLevels)
789 return NULL;
790 texImage = ctx->Texture.Proxy2D->Image[0][level];
791 if (!texImage) {
792 texImage = ctx->Driver.NewTextureImage(ctx);
793 if (!texImage) {
794 _mesa_error(ctx, GL_OUT_OF_MEMORY, "proxy texture allocation");
795 return NULL;
796 }
797 ctx->Texture.Proxy2D->Image[0][level] = texImage;
798 /* Set the 'back' pointer */
799 texImage->TexObject = ctx->Texture.Proxy2D;
800 }
801 return texImage;
802 case GL_PROXY_TEXTURE_3D:
803 if (level >= ctx->Const.Max3DTextureLevels)
804 return NULL;
805 texImage = ctx->Texture.Proxy3D->Image[0][level];
806 if (!texImage) {
807 texImage = ctx->Driver.NewTextureImage(ctx);
808 if (!texImage) {
809 _mesa_error(ctx, GL_OUT_OF_MEMORY, "proxy texture allocation");
810 return NULL;
811 }
812 ctx->Texture.Proxy3D->Image[0][level] = texImage;
813 /* Set the 'back' pointer */
814 texImage->TexObject = ctx->Texture.Proxy3D;
815 }
816 return texImage;
817 case GL_PROXY_TEXTURE_CUBE_MAP:
818 if (level >= ctx->Const.MaxCubeTextureLevels)
819 return NULL;
820 texImage = ctx->Texture.ProxyCubeMap->Image[0][level];
821 if (!texImage) {
822 texImage = ctx->Driver.NewTextureImage(ctx);
823 if (!texImage) {
824 _mesa_error(ctx, GL_OUT_OF_MEMORY, "proxy texture allocation");
825 return NULL;
826 }
827 ctx->Texture.ProxyCubeMap->Image[0][level] = texImage;
828 /* Set the 'back' pointer */
829 texImage->TexObject = ctx->Texture.ProxyCubeMap;
830 }
831 return texImage;
832 case GL_PROXY_TEXTURE_RECTANGLE_NV:
833 if (level > 0)
834 return NULL;
835 texImage = ctx->Texture.ProxyRect->Image[0][level];
836 if (!texImage) {
837 texImage = ctx->Driver.NewTextureImage(ctx);
838 if (!texImage) {
839 _mesa_error(ctx, GL_OUT_OF_MEMORY, "proxy texture allocation");
840 return NULL;
841 }
842 ctx->Texture.ProxyRect->Image[0][level] = texImage;
843 /* Set the 'back' pointer */
844 texImage->TexObject = ctx->Texture.ProxyRect;
845 }
846 return texImage;
847 default:
848 return NULL;
849 }
850 }
851
852
853 /**
854 * Get the maximum number of allowed mipmap levels.
855 *
856 * \param ctx GL context.
857 * \param target texture target.
858 *
859 * \return the maximum number of allowed mipmap levels for the given
860 * texture target, or zero if passed a bad target.
861 *
862 * \sa gl_constants.
863 */
864 GLint
865 _mesa_max_texture_levels(GLcontext *ctx, GLenum target)
866 {
867 switch (target) {
868 case GL_TEXTURE_1D:
869 case GL_PROXY_TEXTURE_1D:
870 case GL_TEXTURE_2D:
871 case GL_PROXY_TEXTURE_2D:
872 return ctx->Const.MaxTextureLevels;
873 case GL_TEXTURE_3D:
874 case GL_PROXY_TEXTURE_3D:
875 return ctx->Const.Max3DTextureLevels;
876 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
877 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
878 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
879 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
880 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
881 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
882 case GL_TEXTURE_CUBE_MAP_ARB:
883 case GL_PROXY_TEXTURE_CUBE_MAP_ARB:
884 return ctx->Const.MaxCubeTextureLevels;
885 break;
886 case GL_TEXTURE_RECTANGLE_NV:
887 case GL_PROXY_TEXTURE_RECTANGLE_NV:
888 return 1;
889 break;
890 default:
891 return 0; /* bad target */
892 }
893 }
894
895
896
897 #if 000 /* not used anymore */
898 /*
899 * glTexImage[123]D can accept a NULL image pointer. In this case we
900 * create a texture image with unspecified image contents per the OpenGL
901 * spec.
902 */
903 static GLubyte *
904 make_null_texture(GLint width, GLint height, GLint depth, GLenum format)
905 {
906 const GLint components = _mesa_components_in_format(format);
907 const GLint numPixels = width * height * depth;
908 GLubyte *data = (GLubyte *) MALLOC(numPixels * components * sizeof(GLubyte));
909
910 #ifdef DEBUG
911 /*
912 * Let's see if anyone finds this. If glTexImage2D() is called with
913 * a NULL image pointer then load the texture image with something
914 * interesting instead of leaving it indeterminate.
915 */
916 if (data) {
917 static const char message[8][32] = {
918 " X X XXXXX XXX X ",
919 " XX XX X X X X X ",
920 " X X X X X X X ",
921 " X X XXXX XXX XXXXX ",
922 " X X X X X X ",
923 " X X X X X X X ",
924 " X X XXXXX XXX X X ",
925 " "
926 };
927
928 GLubyte *imgPtr = data;
929 GLint h, i, j, k;
930 for (h = 0; h < depth; h++) {
931 for (i = 0; i < height; i++) {
932 GLint srcRow = 7 - (i % 8);
933 for (j = 0; j < width; j++) {
934 GLint srcCol = j % 32;
935 GLubyte texel = (message[srcRow][srcCol]=='X') ? 255 : 70;
936 for (k = 0; k < components; k++) {
937 *imgPtr++ = texel;
938 }
939 }
940 }
941 }
942 }
943 #endif
944
945 return data;
946 }
947 #endif
948
949
950
951 /**
952 * Reset the fields of a gl_texture_image struct to zero.
953 *
954 * \param img texture image structure.
955 *
956 * This is called when a proxy texture test fails, we set all the
957 * image members (except DriverData) to zero.
958 * It's also used in glTexImage[123]D as a safeguard to be sure all
959 * required fields get initialized properly by the Driver.TexImage[123]D
960 * functions.
961 */
962 static void
963 clear_teximage_fields(struct gl_texture_image *img)
964 {
965 ASSERT(img);
966 img->Format = 0;
967 img->IntFormat = 0;
968 img->Border = 0;
969 img->Width = 0;
970 img->Height = 0;
971 img->Depth = 0;
972 img->RowStride = 0;
973 img->Width2 = 0;
974 img->Height2 = 0;
975 img->Depth2 = 0;
976 img->WidthLog2 = 0;
977 img->HeightLog2 = 0;
978 img->DepthLog2 = 0;
979 img->Data = NULL;
980 img->TexFormat = &_mesa_null_texformat;
981 img->FetchTexelc = NULL;
982 img->FetchTexelf = NULL;
983 img->IsCompressed = 0;
984 img->CompressedSize = 0;
985 }
986
987
988 /**
989 * Initialize basic fields of the gl_texture_image struct.
990 *
991 * \param ctx GL context.
992 * \param target texture target.
993 * \param img texture image structure to be initialized.
994 * \param width image width.
995 * \param height image height.
996 * \param depth image depth.
997 * \param border image border.
998 * \param internalFormat internal format.
999 *
1000 * Fills in the fields of \p img with the given information.
1001 * Note: width, height and depth include the border.
1002 */
1003 void
1004 _mesa_init_teximage_fields(GLcontext *ctx, GLenum target,
1005 struct gl_texture_image *img,
1006 GLsizei width, GLsizei height, GLsizei depth,
1007 GLint border, GLenum internalFormat)
1008 {
1009 ASSERT(img);
1010 img->Format = _mesa_base_tex_format( ctx, internalFormat );
1011 ASSERT(img->Format > 0);
1012 img->IntFormat = internalFormat;
1013 img->Border = border;
1014 img->Width = width;
1015 img->Height = height;
1016 img->Depth = depth;
1017 img->RowStride = width;
1018 img->WidthLog2 = logbase2(width - 2 * border);
1019 if (height == 1) /* 1-D texture */
1020 img->HeightLog2 = 0;
1021 else
1022 img->HeightLog2 = logbase2(height - 2 * border);
1023 if (depth == 1) /* 2-D texture */
1024 img->DepthLog2 = 0;
1025 else
1026 img->DepthLog2 = logbase2(depth - 2 * border);
1027 img->Width2 = width - 2 * border; /*1 << img->WidthLog2;*/
1028 img->Height2 = height - 2 * border; /*1 << img->HeightLog2;*/
1029 img->Depth2 = depth - 2 * border; /*1 << img->DepthLog2;*/
1030 img->MaxLog2 = MAX2(img->WidthLog2, img->HeightLog2);
1031 img->IsCompressed = is_compressed_format(ctx, internalFormat);
1032 if (img->IsCompressed)
1033 img->CompressedSize = ctx->Driver.CompressedTextureSize(ctx, width,
1034 height, depth, internalFormat);
1035 else
1036 img->CompressedSize = 0;
1037
1038 if ((width == 1 || _mesa_bitcount(width - 2 * border) == 1) &&
1039 (height == 1 || _mesa_bitcount(height - 2 * border) == 1) &&
1040 (depth == 1 || _mesa_bitcount(depth - 2 * border) == 1))
1041 img->_IsPowerOfTwo = GL_TRUE;
1042 else
1043 img->_IsPowerOfTwo = GL_FALSE;
1044
1045 /* Compute Width/Height/DepthScale for mipmap lod computation */
1046 if (target == GL_TEXTURE_RECTANGLE_NV) {
1047 /* scale = 1.0 since texture coords directly map to texels */
1048 img->WidthScale = 1.0;
1049 img->HeightScale = 1.0;
1050 img->DepthScale = 1.0;
1051 }
1052 else {
1053 img->WidthScale = (GLfloat) img->Width;
1054 img->HeightScale = (GLfloat) img->Height;
1055 img->DepthScale = (GLfloat) img->Depth;
1056 }
1057 }
1058
1059
1060 /**
1061 * This is the fallback for Driver.TestProxyTexImage(). Test the texture
1062 * level, width, height and depth against the ctx->Const limits for textures.
1063 *
1064 * A hardware driver might override this function if, for example, the
1065 * max 3D texture size is 512x512x64 (i.e. not a cube).
1066 *
1067 * \param target one of GL_PROXY_TEXTURE_1D, GL_PROXY_TEXTURE_2D,
1068 * GL_PROXY_TEXTURE_3D, GL_PROXY_TEXTURE_RECTANGLE_NV,
1069 * GL_PROXY_TEXTURE_CUBE_MAP_ARB.
1070 * \param level as passed to glTexImage
1071 * \param internalFormat as passed to glTexImage
1072 * \param format as passed to glTexImage
1073 * \param type as passed to glTexImage
1074 * \param width as passed to glTexImage
1075 * \param height as passed to glTexImage
1076 * \param depth as passed to glTexImage
1077 * \param border as passed to glTexImage
1078 * \return GL_TRUE if the image is acceptable, GL_FALSE if not acceptable.
1079 */
1080 GLboolean
1081 _mesa_test_proxy_teximage(GLcontext *ctx, GLenum target, GLint level,
1082 GLint internalFormat, GLenum format, GLenum type,
1083 GLint width, GLint height, GLint depth, GLint border)
1084 {
1085 GLint maxSize;
1086
1087 (void) internalFormat;
1088 (void) format;
1089 (void) type;
1090
1091 switch (target) {
1092 case GL_PROXY_TEXTURE_1D:
1093 maxSize = 1 << (ctx->Const.MaxTextureLevels - 1);
1094 if (width < 2 * border || width > 2 + maxSize ||
1095 (!ctx->Extensions.ARB_texture_non_power_of_two &&
1096 _mesa_bitcount(width - 2 * border) != 1) ||
1097 level >= ctx->Const.MaxTextureLevels) {
1098 /* bad width or level */
1099 return GL_FALSE;
1100 }
1101 return GL_TRUE;
1102 case GL_PROXY_TEXTURE_2D:
1103 maxSize = 1 << (ctx->Const.MaxTextureLevels - 1);
1104 if (width < 2 * border || width > 2 + maxSize ||
1105 (!ctx->Extensions.ARB_texture_non_power_of_two &&
1106 _mesa_bitcount(width - 2 * border) != 1) ||
1107 height < 2 * border || height > 2 + maxSize ||
1108 (!ctx->Extensions.ARB_texture_non_power_of_two &&
1109 _mesa_bitcount(height - 2 * border) != 1) ||
1110 level >= ctx->Const.MaxTextureLevels) {
1111 /* bad width or height or level */
1112 return GL_FALSE;
1113 }
1114 return GL_TRUE;
1115 case GL_PROXY_TEXTURE_3D:
1116 maxSize = 1 << (ctx->Const.Max3DTextureLevels - 1);
1117 if (width < 2 * border || width > 2 + maxSize ||
1118 (!ctx->Extensions.ARB_texture_non_power_of_two &&
1119 _mesa_bitcount(width - 2 * border) != 1) ||
1120 height < 2 * border || height > 2 + maxSize ||
1121 (!ctx->Extensions.ARB_texture_non_power_of_two &&
1122 _mesa_bitcount(height - 2 * border) != 1) ||
1123 depth < 2 * border || depth > 2 + maxSize ||
1124 (!ctx->Extensions.ARB_texture_non_power_of_two &&
1125 _mesa_bitcount(depth - 2 * border) != 1) ||
1126 level >= ctx->Const.Max3DTextureLevels) {
1127 /* bad width or height or depth or level */
1128 return GL_FALSE;
1129 }
1130 return GL_TRUE;
1131 case GL_PROXY_TEXTURE_RECTANGLE_NV:
1132 if (width < 1 || width > ctx->Const.MaxTextureRectSize ||
1133 height < 1 || height > ctx->Const.MaxTextureRectSize ||
1134 level != 0) {
1135 /* bad width or height or level */
1136 return GL_FALSE;
1137 }
1138 return GL_TRUE;
1139 case GL_PROXY_TEXTURE_CUBE_MAP_ARB:
1140 maxSize = 1 << (ctx->Const.MaxCubeTextureLevels - 1);
1141 if (width < 2 * border || width > 2 + maxSize ||
1142 (!ctx->Extensions.ARB_texture_non_power_of_two &&
1143 _mesa_bitcount(width - 2 * border) != 1) ||
1144 height < 2 * border || height > 2 + maxSize ||
1145 (!ctx->Extensions.ARB_texture_non_power_of_two &&
1146 _mesa_bitcount(height - 2 * border) != 1) ||
1147 level >= ctx->Const.MaxCubeTextureLevels) {
1148 /* bad width or height */
1149 return GL_FALSE;
1150 }
1151 return GL_TRUE;
1152 default:
1153 _mesa_problem(ctx, "Invalid target in _mesa_test_proxy_teximage");
1154 return GL_FALSE;
1155 }
1156 }
1157
1158
1159 /**
1160 * Test the glTexImage[123]D() parameters for errors.
1161 *
1162 * \param ctx GL context.
1163 * \param target texture target given by the user.
1164 * \param level image level given by the user.
1165 * \param internalFormat internal format given by the user.
1166 * \param format pixel data format given by the user.
1167 * \param type pixel data type given by the user.
1168 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1169 * \param width image width given by the user.
1170 * \param height image height given by the user.
1171 * \param depth image depth given by the user.
1172 * \param border image border given by the user.
1173 *
1174 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
1175 *
1176 * Verifies each of the parameters against the constants specified in
1177 * __GLcontextRec::Const and the supported extensions, and according to the
1178 * OpenGL specification.
1179 */
1180 static GLboolean
1181 texture_error_check( GLcontext *ctx, GLenum target,
1182 GLint level, GLint internalFormat,
1183 GLenum format, GLenum type,
1184 GLuint dimensions,
1185 GLint width, GLint height,
1186 GLint depth, GLint border )
1187 {
1188 const GLboolean isProxy = is_proxy_target(target);
1189 GLboolean sizeOK;
1190
1191 /* Basic level check (more checking in ctx->Driver.TestProxyTexImage) */
1192 if (level < 0 || level >= MAX_TEXTURE_LEVELS) {
1193 if (!isProxy) {
1194 _mesa_error(ctx, GL_INVALID_VALUE,
1195 "glTexImage%dD(level=%d)", dimensions, level);
1196 }
1197 return GL_TRUE;
1198 }
1199
1200 /* Check border */
1201 if (border < 0 || border > 1 ||
1202 ((target == GL_TEXTURE_RECTANGLE_NV ||
1203 target == GL_PROXY_TEXTURE_RECTANGLE_NV) && border != 0)) {
1204 if (!isProxy) {
1205 _mesa_error(ctx, GL_INVALID_VALUE,
1206 "glTexImage%dD(border=%d)", dimensions, border);
1207 }
1208 return GL_TRUE;
1209 }
1210
1211 if (width < 0 || height < 0 || depth < 0) {
1212 if (!isProxy) {
1213 _mesa_error(ctx, GL_INVALID_VALUE,
1214 "glTexImage%dD(width, height or depth < 0)", dimensions);
1215 }
1216 return GL_TRUE;
1217 }
1218
1219 /* Check target and call ctx->Driver.TestProxyTexImage() to check the
1220 * level, width, height and depth.
1221 */
1222 if (dimensions == 1) {
1223 if (target == GL_PROXY_TEXTURE_1D || target == GL_TEXTURE_1D) {
1224 sizeOK = ctx->Driver.TestProxyTexImage(ctx, GL_PROXY_TEXTURE_1D,
1225 level, internalFormat,
1226 format, type,
1227 width, 1, 1, border);
1228 }
1229 else {
1230 _mesa_error( ctx, GL_INVALID_ENUM, "glTexImage1D(target)" );
1231 return GL_TRUE;
1232 }
1233 }
1234 else if (dimensions == 2) {
1235 if (target == GL_PROXY_TEXTURE_2D || target == GL_TEXTURE_2D) {
1236 sizeOK = ctx->Driver.TestProxyTexImage(ctx, GL_PROXY_TEXTURE_2D,
1237 level, internalFormat,
1238 format, type,
1239 width, height, 1, border);
1240 }
1241 else if (target == GL_PROXY_TEXTURE_CUBE_MAP_ARB ||
1242 (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
1243 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB)) {
1244 if (!ctx->Extensions.ARB_texture_cube_map) {
1245 _mesa_error(ctx, GL_INVALID_ENUM, "glTexImage2D(target)");
1246 return GL_TRUE;
1247 }
1248 sizeOK = (width == height) &&
1249 ctx->Driver.TestProxyTexImage(ctx, GL_PROXY_TEXTURE_CUBE_MAP_ARB,
1250 level, internalFormat, format, type,
1251 width, height, 1, border);
1252 }
1253 else if (target == GL_PROXY_TEXTURE_RECTANGLE_NV ||
1254 target == GL_TEXTURE_RECTANGLE_NV) {
1255 if (!ctx->Extensions.NV_texture_rectangle) {
1256 _mesa_error(ctx, GL_INVALID_ENUM, "glTexImage2D(target)");
1257 return GL_TRUE;
1258 }
1259 sizeOK = ctx->Driver.TestProxyTexImage(ctx,
1260 GL_PROXY_TEXTURE_RECTANGLE_NV,
1261 level, internalFormat,
1262 format, type,
1263 width, height, 1, border);
1264 }
1265 else {
1266 _mesa_error(ctx, GL_INVALID_ENUM, "glTexImage2D(target)");
1267 return GL_TRUE;
1268 }
1269 }
1270 else if (dimensions == 3) {
1271 if (target == GL_PROXY_TEXTURE_3D || target == GL_TEXTURE_3D) {
1272 sizeOK = ctx->Driver.TestProxyTexImage(ctx, GL_PROXY_TEXTURE_3D,
1273 level, internalFormat,
1274 format, type,
1275 width, height, depth, border);
1276 }
1277 else {
1278 _mesa_error( ctx, GL_INVALID_ENUM, "glTexImage3D(target)" );
1279 return GL_TRUE;
1280 }
1281 }
1282 else {
1283 _mesa_problem( ctx, "bad dims in texture_error_check" );
1284 return GL_TRUE;
1285 }
1286
1287 if (!sizeOK) {
1288 if (!isProxy) {
1289 _mesa_error(ctx, GL_INVALID_VALUE,
1290 "glTexImage%dD(level=%d, width=%d, height=%d, depth=%d)",
1291 dimensions, level, width, height, depth);
1292 }
1293 return GL_TRUE;
1294 }
1295
1296 /* Check internalFormat */
1297 if (_mesa_base_tex_format(ctx, internalFormat) < 0) {
1298 if (!isProxy) {
1299 _mesa_error(ctx, GL_INVALID_VALUE,
1300 "glTexImage%dD(internalFormat=0x%x)",
1301 dimensions, internalFormat);
1302 }
1303 return GL_TRUE;
1304 }
1305
1306 /* Check incoming image format and type */
1307 if (!_mesa_is_legal_format_and_type(ctx, format, type)) {
1308 /* Yes, generate GL_INVALID_OPERATION, not GL_INVALID_ENUM, if there
1309 * is a type/format mismatch. See 1.2 spec page 94, sec 3.6.4.
1310 */
1311 if (!isProxy) {
1312 _mesa_error(ctx, GL_INVALID_OPERATION,
1313 "glTexImage%dD(format or type)", dimensions);
1314 }
1315 return GL_TRUE;
1316 }
1317
1318 /* make sure internal format and format basically agree */
1319 if ((is_color_format(internalFormat) != is_color_format(format)) ||
1320 (is_index_format(internalFormat) != is_index_format(format)) ||
1321 (is_depth_format(internalFormat) != is_depth_format(format)) ||
1322 (is_ycbcr_format(internalFormat) != is_ycbcr_format(format))) {
1323 if (!isProxy)
1324 _mesa_error(ctx, GL_INVALID_OPERATION,
1325 "glTexImage(internalFormat/format)");
1326 return GL_TRUE;
1327 }
1328
1329 /* additional checks for ycbcr textures */
1330 if (internalFormat == GL_YCBCR_MESA) {
1331 ASSERT(ctx->Extensions.MESA_ycbcr_texture);
1332 if (type != GL_UNSIGNED_SHORT_8_8_MESA &&
1333 type != GL_UNSIGNED_SHORT_8_8_REV_MESA) {
1334 char message[100];
1335 _mesa_sprintf(message,
1336 "glTexImage%d(format/type YCBCR mismatch", dimensions);
1337 _mesa_error(ctx, GL_INVALID_ENUM, message);
1338 return GL_TRUE; /* error */
1339 }
1340 if (target != GL_TEXTURE_2D &&
1341 target != GL_PROXY_TEXTURE_2D &&
1342 target != GL_TEXTURE_RECTANGLE_NV &&
1343 target != GL_PROXY_TEXTURE_RECTANGLE_NV) {
1344 if (!isProxy)
1345 _mesa_error(ctx, GL_INVALID_ENUM, "glTexImage(target)");
1346 return GL_TRUE;
1347 }
1348 if (border != 0) {
1349 if (!isProxy) {
1350 char message[100];
1351 _mesa_sprintf(message,
1352 "glTexImage%d(format=GL_YCBCR_MESA and border=%d)",
1353 dimensions, border);
1354 _mesa_error(ctx, GL_INVALID_VALUE, message);
1355 }
1356 return GL_TRUE;
1357 }
1358 }
1359
1360 /* additional checks for depth textures */
1361 if (_mesa_base_tex_format(ctx, internalFormat) == GL_DEPTH_COMPONENT) {
1362 /* Only 1D and 2D textures supported */
1363 if (target != GL_TEXTURE_1D &&
1364 target != GL_PROXY_TEXTURE_1D &&
1365 target != GL_TEXTURE_2D &&
1366 target != GL_PROXY_TEXTURE_2D) {
1367 if (!isProxy)
1368 _mesa_error(ctx, GL_INVALID_ENUM,
1369 "glTexImage(target/internalFormat)");
1370 return GL_TRUE;
1371 }
1372 }
1373
1374 /* additional checks for compressed textures */
1375 if (is_compressed_format(ctx, internalFormat)) {
1376 if (target == GL_TEXTURE_2D || target == GL_PROXY_TEXTURE_2D) {
1377 /* OK */
1378 }
1379 else if (ctx->Extensions.ARB_texture_cube_map &&
1380 (target == GL_PROXY_TEXTURE_CUBE_MAP ||
1381 (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X &&
1382 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z))) {
1383 /* OK */
1384 }
1385 else {
1386 if (!isProxy) {
1387 _mesa_error(ctx, GL_INVALID_ENUM,
1388 "glTexImage%d(target)", dimensions);
1389 return GL_TRUE;
1390 }
1391 }
1392 if (border != 0) {
1393 if (!isProxy) {
1394 _mesa_error(ctx, GL_INVALID_OPERATION,
1395 "glTexImage%D(border!=0)", dimensions);
1396 }
1397 return GL_TRUE;
1398 }
1399 }
1400
1401 /* if we get here, the parameters are OK */
1402 return GL_FALSE;
1403 }
1404
1405
1406 /**
1407 * Test glTexSubImage[123]D() parameters for errors.
1408 *
1409 * \param ctx GL context.
1410 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1411 * \param target texture target given by the user.
1412 * \param level image level given by the user.
1413 * \param xoffset sub-image x offset given by the user.
1414 * \param yoffset sub-image y offset given by the user.
1415 * \param zoffset sub-image z offset given by the user.
1416 * \param format pixel data format given by the user.
1417 * \param type pixel data type given by the user.
1418 * \param width image width given by the user.
1419 * \param height image height given by the user.
1420 * \param depth image depth given by the user.
1421 *
1422 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
1423 *
1424 * Verifies each of the parameters against the constants specified in
1425 * __GLcontextRec::Const and the supported extensions, and according to the
1426 * OpenGL specification.
1427 */
1428 static GLboolean
1429 subtexture_error_check( GLcontext *ctx, GLuint dimensions,
1430 GLenum target, GLint level,
1431 GLint xoffset, GLint yoffset, GLint zoffset,
1432 GLint width, GLint height, GLint depth,
1433 GLenum format, GLenum type )
1434 {
1435 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1436 struct gl_texture_image *destTex;
1437
1438 /* Check target */
1439 if (dimensions == 1) {
1440 if (target != GL_TEXTURE_1D) {
1441 _mesa_error( ctx, GL_INVALID_ENUM, "glTexSubImage1D(target)" );
1442 return GL_TRUE;
1443 }
1444 }
1445 else if (dimensions == 2) {
1446 if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
1447 target <=GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB) {
1448 if (!ctx->Extensions.ARB_texture_cube_map) {
1449 _mesa_error( ctx, GL_INVALID_ENUM, "glTexSubImage2D(target)" );
1450 return GL_TRUE;
1451 }
1452 }
1453 else if (ctx->Extensions.NV_texture_rectangle &&
1454 target == GL_TEXTURE_RECTANGLE_NV) {
1455 if (!ctx->Extensions.NV_texture_rectangle) {
1456 _mesa_error( ctx, GL_INVALID_ENUM, "glTexSubImage2D(target)" );
1457 return GL_TRUE;
1458 }
1459 }
1460 else if (target != GL_TEXTURE_2D) {
1461 _mesa_error( ctx, GL_INVALID_ENUM, "glTexSubImage2D(target)" );
1462 return GL_TRUE;
1463 }
1464 }
1465 else if (dimensions == 3) {
1466 if (target != GL_TEXTURE_3D) {
1467 _mesa_error( ctx, GL_INVALID_ENUM, "glTexSubImage3D(target)" );
1468 return GL_TRUE;
1469 }
1470 }
1471 else {
1472 _mesa_problem( ctx, "invalid dims in texture_error_check" );
1473 return GL_TRUE;
1474 }
1475
1476 /* Basic level check */
1477 if (level < 0 || level >= MAX_TEXTURE_LEVELS) {
1478 _mesa_error(ctx, GL_INVALID_ENUM, "glTexSubImage2D(level=%d)", level);
1479 return GL_TRUE;
1480 }
1481
1482 if (width < 0) {
1483 _mesa_error(ctx, GL_INVALID_VALUE,
1484 "glTexSubImage%dD(width=%d)", dimensions, width);
1485 return GL_TRUE;
1486 }
1487 if (height < 0 && dimensions > 1) {
1488 _mesa_error(ctx, GL_INVALID_VALUE,
1489 "glTexSubImage%dD(height=%d)", dimensions, height);
1490 return GL_TRUE;
1491 }
1492 if (depth < 0 && dimensions > 2) {
1493 _mesa_error(ctx, GL_INVALID_VALUE,
1494 "glTexSubImage%dD(depth=%d)", dimensions, depth);
1495 return GL_TRUE;
1496 }
1497
1498 destTex = _mesa_select_tex_image(ctx, texUnit, target, level);
1499
1500 if (!destTex) {
1501 /* undefined image level */
1502 _mesa_error(ctx, GL_INVALID_OPERATION, "glTexSubImage%dD", dimensions);
1503 return GL_TRUE;
1504 }
1505
1506 if (xoffset < -((GLint)destTex->Border)) {
1507 _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage%dD(xoffset)",
1508 dimensions);
1509 return GL_TRUE;
1510 }
1511 if (xoffset + width > (GLint) (destTex->Width + destTex->Border)) {
1512 _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage%dD(xoffset+width)",
1513 dimensions);
1514 return GL_TRUE;
1515 }
1516 if (dimensions > 1) {
1517 if (yoffset < -((GLint)destTex->Border)) {
1518 _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage%dD(yoffset)",
1519 dimensions);
1520 return GL_TRUE;
1521 }
1522 if (yoffset + height > (GLint) (destTex->Height + destTex->Border)) {
1523 _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage%dD(yoffset+height)",
1524 dimensions);
1525 return GL_TRUE;
1526 }
1527 }
1528 if (dimensions > 2) {
1529 if (zoffset < -((GLint)destTex->Border)) {
1530 _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage3D(zoffset)");
1531 return GL_TRUE;
1532 }
1533 if (zoffset + depth > (GLint) (destTex->Depth + destTex->Border)) {
1534 _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage3D(zoffset+depth)");
1535 return GL_TRUE;
1536 }
1537 }
1538
1539 if (!_mesa_is_legal_format_and_type(ctx, format, type)) {
1540 _mesa_error(ctx, GL_INVALID_ENUM,
1541 "glTexSubImage%dD(format or type)", dimensions);
1542 return GL_TRUE;
1543 }
1544
1545 if (destTex->IsCompressed) {
1546 const struct gl_texture_unit *texUnit;
1547 const struct gl_texture_image *texImage;
1548 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1549 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
1550
1551 if (target == GL_TEXTURE_2D || target == GL_PROXY_TEXTURE_2D) {
1552 /* OK */
1553 }
1554 else if (ctx->Extensions.ARB_texture_cube_map &&
1555 (target == GL_PROXY_TEXTURE_CUBE_MAP ||
1556 (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X &&
1557 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z))) {
1558 /* OK */
1559 }
1560 else {
1561 _mesa_error(ctx, GL_INVALID_ENUM,
1562 "glTexSubImage%D(target)", dimensions);
1563 return GL_TRUE;
1564 }
1565 /* offset must be multiple of 4 */
1566 if ((xoffset & 3) || (yoffset & 3)) {
1567 _mesa_error(ctx, GL_INVALID_OPERATION,
1568 "glTexSubImage%D(xoffset or yoffset)", dimensions);
1569 return GL_TRUE;
1570 }
1571 /* size must be multiple of 4 or equal to whole texture size */
1572 if ((width & 3) && (GLuint) width != texImage->Width) {
1573 _mesa_error(ctx, GL_INVALID_OPERATION,
1574 "glTexSubImage%D(width)", dimensions);
1575 return GL_TRUE;
1576 }
1577 if ((height & 3) && (GLuint) height != texImage->Height) {
1578 _mesa_error(ctx, GL_INVALID_OPERATION,
1579 "glTexSubImage%D(width)", dimensions);
1580 return GL_TRUE;
1581 }
1582 }
1583
1584 return GL_FALSE;
1585 }
1586
1587
1588 /**
1589 * Test glCopyTexImage[12]D() parameters for errors.
1590 *
1591 * \param ctx GL context.
1592 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1593 * \param target texture target given by the user.
1594 * \param level image level given by the user.
1595 * \param internalFormat internal format given by the user.
1596 * \param width image width given by the user.
1597 * \param height image height given by the user.
1598 * \param depth image depth given by the user.
1599 * \param border texture border.
1600 *
1601 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
1602 *
1603 * Verifies each of the parameters against the constants specified in
1604 * __GLcontextRec::Const and the supported extensions, and according to the
1605 * OpenGL specification.
1606 */
1607 static GLboolean
1608 copytexture_error_check( GLcontext *ctx, GLuint dimensions,
1609 GLenum target, GLint level, GLint internalFormat,
1610 GLint width, GLint height, GLint border )
1611 {
1612 GLenum format, type;
1613 GLboolean sizeOK;
1614
1615 /* Basic level check (more checking in ctx->Driver.TestProxyTexImage) */
1616 if (level < 0 || level >= MAX_TEXTURE_LEVELS) {
1617 _mesa_error(ctx, GL_INVALID_VALUE,
1618 "glCopyTexImage%dD(level=%d)", dimensions, level);
1619 return GL_TRUE;
1620 }
1621
1622 /* Check border */
1623 if (border < 0 || border > 1 ||
1624 ((target == GL_TEXTURE_RECTANGLE_NV ||
1625 target == GL_PROXY_TEXTURE_RECTANGLE_NV) && border != 0)) {
1626 return GL_TRUE;
1627 }
1628
1629 /* The format and type aren't really significant here, but we need to pass
1630 * something to TestProxyTexImage().
1631 */
1632 format = _mesa_base_tex_format(ctx, internalFormat);
1633 type = GL_FLOAT;
1634
1635 /* Check target and call ctx->Driver.TestProxyTexImage() to check the
1636 * level, width, height and depth.
1637 */
1638 if (dimensions == 1) {
1639 if (target == GL_TEXTURE_1D) {
1640 sizeOK = ctx->Driver.TestProxyTexImage(ctx, GL_PROXY_TEXTURE_1D,
1641 level, internalFormat,
1642 format, type,
1643 width, 1, 1, border);
1644 }
1645 else {
1646 _mesa_error( ctx, GL_INVALID_ENUM, "glCopyTexImage1D(target)" );
1647 return GL_TRUE;
1648 }
1649 }
1650 else if (dimensions == 2) {
1651 if (target == GL_TEXTURE_2D) {
1652 sizeOK = ctx->Driver.TestProxyTexImage(ctx, GL_PROXY_TEXTURE_2D,
1653 level, internalFormat,
1654 format, type,
1655 width, height, 1, border);
1656 }
1657 else if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
1658 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB) {
1659 if (!ctx->Extensions.ARB_texture_cube_map) {
1660 _mesa_error( ctx, GL_INVALID_ENUM, "glCopyTexImage2D(target)" );
1661 return GL_TRUE;
1662 }
1663 sizeOK = (width == height) &&
1664 ctx->Driver.TestProxyTexImage(ctx, GL_PROXY_TEXTURE_CUBE_MAP_ARB,
1665 level, internalFormat, format, type,
1666 width, height, 1, border);
1667 }
1668 else if (target == GL_TEXTURE_RECTANGLE_NV) {
1669 if (!ctx->Extensions.NV_texture_rectangle) {
1670 _mesa_error( ctx, GL_INVALID_ENUM, "glCopyTexImage2D(target)" );
1671 return GL_TRUE;
1672 }
1673 sizeOK = ctx->Driver.TestProxyTexImage(ctx,
1674 GL_PROXY_TEXTURE_RECTANGLE_NV,
1675 level, internalFormat,
1676 format, type,
1677 width, height, 1, border);
1678 }
1679 else {
1680 _mesa_error( ctx, GL_INVALID_ENUM, "glCopyTexImage2D(target)" );
1681 return GL_TRUE;
1682 }
1683 }
1684 else {
1685 _mesa_problem(ctx, "invalid dimensions in copytexture_error_check");
1686 return GL_TRUE;
1687 }
1688
1689 if (!sizeOK) {
1690 if (dimensions == 1) {
1691 _mesa_error(ctx, GL_INVALID_VALUE,
1692 "glCopyTexImage1D(width=%d)", width);
1693 }
1694 else {
1695 ASSERT(dimensions == 2);
1696 _mesa_error(ctx, GL_INVALID_VALUE,
1697 "glCopyTexImage2D(width=%d, height=%d)", width, height);
1698 }
1699 return GL_TRUE;
1700 }
1701
1702 if (_mesa_base_tex_format(ctx, internalFormat) < 0) {
1703 _mesa_error(ctx, GL_INVALID_VALUE,
1704 "glCopyTexImage%dD(internalFormat)", dimensions);
1705 return GL_TRUE;
1706 }
1707
1708 if (is_compressed_format(ctx, internalFormat)) {
1709 if (target != GL_TEXTURE_2D) {
1710 _mesa_error(ctx, GL_INVALID_ENUM,
1711 "glCopyTexImage%d(target)", dimensions);
1712 return GL_TRUE;
1713 }
1714 if (border != 0) {
1715 _mesa_error(ctx, GL_INVALID_OPERATION,
1716 "glCopyTexImage%D(border!=0)", dimensions);
1717 return GL_TRUE;
1718 }
1719 }
1720
1721 /* if we get here, the parameters are OK */
1722 return GL_FALSE;
1723 }
1724
1725
1726 /**
1727 * Test glCopyTexImage[12]D() parameters for errors.
1728 *
1729 * \param ctx GL context.
1730 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1731 * \param target texture target given by the user.
1732 * \param level image level given by the user.
1733 * \param xoffset sub-image x offset given by the user.
1734 * \param yoffset sub-image y offset given by the user.
1735 * \param zoffset sub-image z offset given by the user.
1736 * \param width image width given by the user.
1737 * \param height image height given by the user.
1738 *
1739 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
1740 *
1741 * Verifies each of the parameters against the constants specified in
1742 * __GLcontextRec::Const and the supported extensions, and according to the
1743 * OpenGL specification.
1744 */
1745 static GLboolean
1746 copytexsubimage_error_check( GLcontext *ctx, GLuint dimensions,
1747 GLenum target, GLint level,
1748 GLint xoffset, GLint yoffset, GLint zoffset,
1749 GLsizei width, GLsizei height )
1750 {
1751 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1752 struct gl_texture_image *teximage;
1753
1754 /* Check target */
1755 if (dimensions == 1) {
1756 if (target != GL_TEXTURE_1D) {
1757 _mesa_error( ctx, GL_INVALID_ENUM, "glCopyTexSubImage1D(target)" );
1758 return GL_TRUE;
1759 }
1760 }
1761 else if (dimensions == 2) {
1762 if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
1763 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB) {
1764 if (!ctx->Extensions.ARB_texture_cube_map) {
1765 _mesa_error( ctx, GL_INVALID_ENUM, "glCopyTexSubImage2D(target)" );
1766 return GL_TRUE;
1767 }
1768 }
1769 else if (target == GL_TEXTURE_RECTANGLE_NV) {
1770 if (!ctx->Extensions.NV_texture_rectangle) {
1771 _mesa_error( ctx, GL_INVALID_ENUM, "glCopyTexSubImage2D(target)" );
1772 return GL_TRUE;
1773 }
1774 }
1775 else if (target != GL_TEXTURE_2D) {
1776 _mesa_error( ctx, GL_INVALID_ENUM, "glCopyTexSubImage2D(target)" );
1777 return GL_TRUE;
1778 }
1779 }
1780 else if (dimensions == 3) {
1781 if (target != GL_TEXTURE_3D) {
1782 _mesa_error( ctx, GL_INVALID_ENUM, "glCopyTexSubImage3D(target)" );
1783 return GL_TRUE;
1784 }
1785 }
1786
1787 /* Check level */
1788 if (level < 0 || level >= MAX_TEXTURE_LEVELS) {
1789 _mesa_error(ctx, GL_INVALID_VALUE,
1790 "glCopyTexSubImage%dD(level=%d)", dimensions, level);
1791 return GL_TRUE;
1792 }
1793
1794 /* Check size */
1795 if (width < 0) {
1796 _mesa_error(ctx, GL_INVALID_VALUE,
1797 "glCopyTexSubImage%dD(width=%d)", dimensions, width);
1798 return GL_TRUE;
1799 }
1800 if (dimensions > 1 && height < 0) {
1801 _mesa_error(ctx, GL_INVALID_VALUE,
1802 "glCopyTexSubImage%dD(height=%d)", dimensions, height);
1803 return GL_TRUE;
1804 }
1805
1806 teximage = _mesa_select_tex_image(ctx, texUnit, target, level);
1807 if (!teximage) {
1808 _mesa_error(ctx, GL_INVALID_OPERATION,
1809 "glCopyTexSubImage%dD(undefined texture level: %d)",
1810 dimensions, level);
1811 return GL_TRUE;
1812 }
1813
1814 if (xoffset < -((GLint)teximage->Border)) {
1815 _mesa_error(ctx, GL_INVALID_VALUE,
1816 "glCopyTexSubImage%dD(xoffset=%d)", dimensions, xoffset);
1817 return GL_TRUE;
1818 }
1819 if (xoffset + width > (GLint) (teximage->Width + teximage->Border)) {
1820 _mesa_error(ctx, GL_INVALID_VALUE,
1821 "glCopyTexSubImage%dD(xoffset+width)", dimensions);
1822 return GL_TRUE;
1823 }
1824 if (dimensions > 1) {
1825 if (yoffset < -((GLint)teximage->Border)) {
1826 _mesa_error(ctx, GL_INVALID_VALUE,
1827 "glCopyTexSubImage%dD(yoffset=%d)", dimensions, yoffset);
1828 return GL_TRUE;
1829 }
1830 /* NOTE: we're adding the border here, not subtracting! */
1831 if (yoffset + height > (GLint) (teximage->Height + teximage->Border)) {
1832 _mesa_error(ctx, GL_INVALID_VALUE,
1833 "glCopyTexSubImage%dD(yoffset+height)", dimensions);
1834 return GL_TRUE;
1835 }
1836 }
1837
1838 if (dimensions > 2) {
1839 if (zoffset < -((GLint)teximage->Border)) {
1840 _mesa_error(ctx, GL_INVALID_VALUE,
1841 "glCopyTexSubImage%dD(zoffset)", dimensions);
1842 return GL_TRUE;
1843 }
1844 if (zoffset > (GLint) (teximage->Depth + teximage->Border)) {
1845 _mesa_error(ctx, GL_INVALID_VALUE,
1846 "glCopyTexSubImage%dD(zoffset+depth)", dimensions);
1847 return GL_TRUE;
1848 }
1849 }
1850
1851 if (teximage->IsCompressed) {
1852 if (target != GL_TEXTURE_2D) {
1853 _mesa_error(ctx, GL_INVALID_ENUM,
1854 "glCopyTexSubImage%d(target)", dimensions);
1855 return GL_TRUE;
1856 }
1857 /* offset must be multiple of 4 */
1858 if ((xoffset & 3) || (yoffset & 3)) {
1859 _mesa_error(ctx, GL_INVALID_VALUE,
1860 "glCopyTexSubImage%D(xoffset or yoffset)", dimensions);
1861 return GL_TRUE;
1862 }
1863 /* size must be multiple of 4 */
1864 if ((width & 3) != 0 && (GLuint) width != teximage->Width) {
1865 _mesa_error(ctx, GL_INVALID_VALUE,
1866 "glCopyTexSubImage%D(width)", dimensions);
1867 return GL_TRUE;
1868 }
1869 if ((height & 3) != 0 && (GLuint) height != teximage->Height) {
1870 _mesa_error(ctx, GL_INVALID_VALUE,
1871 "glCopyTexSubImage%D(height)", dimensions);
1872 return GL_TRUE;
1873 }
1874 }
1875
1876 if (teximage->IntFormat == GL_YCBCR_MESA) {
1877 _mesa_error(ctx, GL_INVALID_OPERATION, "glCopyTexSubImage2D");
1878 return GL_TRUE;
1879 }
1880
1881 /* if we get here, the parameters are OK */
1882 return GL_FALSE;
1883 }
1884
1885
1886 /**
1887 * Get texture image. Called by glGetTexImage.
1888 *
1889 * \param target texture target.
1890 * \param level image level.
1891 * \param format pixel data format for returned image.
1892 * \param type pixel data type for returned image.
1893 * \param pixels returned pixel data.
1894 */
1895 void GLAPIENTRY
1896 _mesa_GetTexImage( GLenum target, GLint level, GLenum format,
1897 GLenum type, GLvoid *pixels )
1898 {
1899 const struct gl_texture_unit *texUnit;
1900 const struct gl_texture_object *texObj;
1901 const struct gl_texture_image *texImage;
1902 GLint maxLevels = 0;
1903 GET_CURRENT_CONTEXT(ctx);
1904 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
1905
1906 texUnit = &(ctx->Texture.Unit[ctx->Texture.CurrentUnit]);
1907 texObj = _mesa_select_tex_object(ctx, texUnit, target);
1908 if (!texObj || is_proxy_target(target)) {
1909 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexImage(target)");
1910 return;
1911 }
1912
1913 maxLevels = _mesa_max_texture_levels(ctx, target);
1914 ASSERT(maxLevels > 0); /* 0 indicates bad target, caught above */
1915
1916 if (level < 0 || level >= maxLevels) {
1917 _mesa_error( ctx, GL_INVALID_VALUE, "glGetTexImage(level)" );
1918 return;
1919 }
1920
1921 if (_mesa_sizeof_packed_type(type) <= 0) {
1922 _mesa_error( ctx, GL_INVALID_ENUM, "glGetTexImage(type)" );
1923 return;
1924 }
1925
1926 if (_mesa_components_in_format(format) <= 0 ||
1927 format == GL_STENCIL_INDEX) {
1928 _mesa_error( ctx, GL_INVALID_ENUM, "glGetTexImage(format)" );
1929 return;
1930 }
1931
1932 if (!ctx->Extensions.EXT_paletted_texture && is_index_format(format)) {
1933 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexImage(format)");
1934 }
1935
1936 if (!ctx->Extensions.SGIX_depth_texture && is_depth_format(format)) {
1937 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexImage(format)");
1938 }
1939
1940 if (!ctx->Extensions.MESA_ycbcr_texture && is_ycbcr_format(format)) {
1941 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexImage(format)");
1942 }
1943
1944 if (!pixels)
1945 return;
1946
1947 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
1948 if (!texImage) {
1949 /* invalid mipmap level, not an error */
1950 return;
1951 }
1952
1953 if (!texImage->Data) {
1954 /* no image data, not an error */
1955 return;
1956 }
1957
1958 /* Make sure the requested image format is compatible with the
1959 * texture's format.
1960 */
1961 if (is_color_format(format)
1962 && !is_color_format(texImage->TexFormat->BaseFormat)) {
1963 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetTexImage(format mismatch)");
1964 return;
1965 }
1966 else if (is_index_format(format)
1967 && !is_index_format(texImage->TexFormat->BaseFormat)) {
1968 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetTexImage(format mismatch)");
1969 return;
1970 }
1971 else if (is_depth_format(format)
1972 && !is_depth_format(texImage->TexFormat->BaseFormat)) {
1973 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetTexImage(format mismatch)");
1974 return;
1975 }
1976 else if (is_ycbcr_format(format)
1977 && !is_ycbcr_format(texImage->TexFormat->BaseFormat)) {
1978 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetTexImage(format mismatch)");
1979 return;
1980 }
1981
1982 {
1983 const GLint width = texImage->Width;
1984 const GLint height = texImage->Height;
1985 const GLint depth = texImage->Depth;
1986 GLint img, row;
1987 for (img = 0; img < depth; img++) {
1988 for (row = 0; row < height; row++) {
1989 /* compute destination address in client memory */
1990 GLvoid *dest = _mesa_image_address( &ctx->Pack, pixels,
1991 width, height, format, type,
1992 img, row, 0);
1993 assert(dest);
1994
1995 if (format == GL_COLOR_INDEX) {
1996 GLuint indexRow[MAX_WIDTH];
1997 GLint col;
1998 /* Can't use FetchTexel here because that returns RGBA */
1999 if (texImage->TexFormat->IndexBits == 8) {
2000 const GLubyte *src = (const GLubyte *) texImage->Data;
2001 for (col = 0; col < width; col++) {
2002 indexRow[col] = src[texImage->Width *
2003 (img * texImage->Height + row) + col];
2004 }
2005 }
2006 else if (texImage->TexFormat->IndexBits == 16) {
2007 const GLushort *src = (const GLushort *) texImage->Data;
2008 for (col = 0; col < width; col++) {
2009 indexRow[col] = src[texImage->Width *
2010 (img * texImage->Height + row) + col];
2011 }
2012 }
2013 else {
2014 _mesa_problem(ctx,
2015 "Color index problem in _mesa_GetTexImage");
2016 return;
2017 }
2018 _mesa_pack_index_span(ctx, width, type, dest,
2019 indexRow, &ctx->Pack,
2020 0 /* no image transfer */);
2021 }
2022 else if (format == GL_DEPTH_COMPONENT) {
2023 GLfloat depthRow[MAX_WIDTH];
2024 GLint col;
2025 for (col = 0; col < width; col++) {
2026 (*texImage->FetchTexelf)(texImage, col, row, img,
2027 depthRow + col);
2028 }
2029 _mesa_pack_depth_span(ctx, width, dest, type,
2030 depthRow, &ctx->Pack);
2031 }
2032 else if (format == GL_YCBCR_MESA) {
2033 /* No pixel transfer */
2034 const GLint rowstride = texImage->RowStride;
2035 MEMCPY(dest,
2036 (const GLushort *) texImage->Data + row * rowstride,
2037 width * sizeof(GLushort));
2038 /* check for byte swapping */
2039 if ((texImage->TexFormat->MesaFormat == MESA_FORMAT_YCBCR
2040 && type == GL_UNSIGNED_SHORT_8_8_REV_MESA) ||
2041 (texImage->TexFormat->MesaFormat == MESA_FORMAT_YCBCR_REV
2042 && type == GL_UNSIGNED_SHORT_8_8_MESA)) {
2043 if (!ctx->Pack.SwapBytes)
2044 _mesa_swap2((GLushort *) dest, width);
2045 }
2046 else if (ctx->Pack.SwapBytes) {
2047 _mesa_swap2((GLushort *) dest, width);
2048 }
2049 }
2050 else {
2051 /* general case: convert row to RGBA format */
2052 GLfloat rgba[MAX_WIDTH][4];
2053 GLint col;
2054 for (col = 0; col < width; col++) {
2055 (*texImage->FetchTexelf)(texImage, col, row, img, rgba[col]);
2056 }
2057 _mesa_pack_rgba_span_float(ctx, width,
2058 (const GLfloat (*)[4]) rgba,
2059 format, type, dest, &ctx->Pack,
2060 0 /* no image transfer */);
2061 } /* format */
2062 } /* row */
2063 } /* img */
2064 }
2065 }
2066
2067
2068
2069 /*
2070 * Called from the API. Note that width includes the border.
2071 */
2072 void GLAPIENTRY
2073 _mesa_TexImage1D( GLenum target, GLint level, GLint internalFormat,
2074 GLsizei width, GLint border, GLenum format,
2075 GLenum type, const GLvoid *pixels )
2076 {
2077 GLsizei postConvWidth = width;
2078 GET_CURRENT_CONTEXT(ctx);
2079 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2080
2081 if (is_color_format(internalFormat)) {
2082 _mesa_adjust_image_for_convolution(ctx, 1, &postConvWidth, NULL);
2083 }
2084
2085 if (target == GL_TEXTURE_1D) {
2086 struct gl_texture_unit *texUnit;
2087 struct gl_texture_object *texObj;
2088 struct gl_texture_image *texImage;
2089
2090 if (texture_error_check(ctx, target, level, internalFormat,
2091 format, type, 1, postConvWidth, 1, 1, border)) {
2092 return; /* error was recorded */
2093 }
2094
2095 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2096 texObj = _mesa_select_tex_object(ctx, texUnit, target);
2097 texImage = _mesa_get_tex_image(ctx, texUnit, target, level);
2098
2099 if (!texImage) {
2100 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage1D");
2101 return;
2102 }
2103 else if (texImage->Data && !texImage->IsClientData) {
2104 /* free the old texture data */
2105 MESA_PBUFFER_FREE(texImage->Data);
2106 }
2107 texImage->Data = NULL;
2108 clear_teximage_fields(texImage); /* not really needed, but helpful */
2109 _mesa_init_teximage_fields(ctx, target, texImage,
2110 postConvWidth, 1, 1,
2111 border, internalFormat);
2112
2113 if (ctx->NewState & _IMAGE_NEW_TRANSFER_STATE)
2114 _mesa_update_state(ctx);
2115
2116 ASSERT(ctx->Driver.TexImage1D);
2117
2118 /* Give the texture to the driver! <pixels> may be null! */
2119 (*ctx->Driver.TexImage1D)(ctx, target, level, internalFormat,
2120 width, border, format, type, pixels,
2121 &ctx->Unpack, texObj, texImage);
2122
2123 ASSERT(texImage->TexFormat);
2124
2125 /* If driver didn't explicitly set this, use the defaults */
2126 if (!texImage->FetchTexelc)
2127 texImage->FetchTexelc = texImage->TexFormat->FetchTexel1D;
2128 if (!texImage->FetchTexelf)
2129 texImage->FetchTexelf = texImage->TexFormat->FetchTexel1Df;
2130 ASSERT(texImage->FetchTexelc);
2131 ASSERT(texImage->FetchTexelf);
2132
2133 /* state update */
2134 texObj->Complete = GL_FALSE;
2135 ctx->NewState |= _NEW_TEXTURE;
2136 }
2137 else if (target == GL_PROXY_TEXTURE_1D) {
2138 /* Proxy texture: check for errors and update proxy state */
2139 struct gl_texture_image *texImage;
2140 texImage = _mesa_get_proxy_tex_image(ctx, target, level);
2141 if (texture_error_check(ctx, target, level, internalFormat,
2142 format, type, 1, postConvWidth, 1, 1, border)) {
2143 /* when error, clear all proxy texture image parameters */
2144 if (texImage)
2145 clear_teximage_fields(texImage);
2146 }
2147 else {
2148 /* no error, set the tex image parameters */
2149 ASSERT(texImage);
2150 _mesa_init_teximage_fields(ctx, target, texImage,
2151 postConvWidth, 1, 1,
2152 border, internalFormat);
2153 texImage->TexFormat = (*ctx->Driver.ChooseTextureFormat)(ctx,
2154 internalFormat, format, type);
2155 }
2156 }
2157 else {
2158 _mesa_error( ctx, GL_INVALID_ENUM, "glTexImage1D(target)" );
2159 return;
2160 }
2161 }
2162
2163
2164 void GLAPIENTRY
2165 _mesa_TexImage2D( GLenum target, GLint level, GLint internalFormat,
2166 GLsizei width, GLsizei height, GLint border,
2167 GLenum format, GLenum type,
2168 const GLvoid *pixels )
2169 {
2170 GLsizei postConvWidth = width, postConvHeight = height;
2171 GET_CURRENT_CONTEXT(ctx);
2172 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2173
2174 if (is_color_format(internalFormat)) {
2175 _mesa_adjust_image_for_convolution(ctx, 2, &postConvWidth,
2176 &postConvHeight);
2177 }
2178
2179 if (target == GL_TEXTURE_2D ||
2180 (ctx->Extensions.ARB_texture_cube_map &&
2181 target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
2182 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB) ||
2183 (ctx->Extensions.NV_texture_rectangle &&
2184 target == GL_TEXTURE_RECTANGLE_NV)) {
2185 /* non-proxy target */
2186 struct gl_texture_unit *texUnit;
2187 struct gl_texture_object *texObj;
2188 struct gl_texture_image *texImage;
2189
2190 if (texture_error_check(ctx, target, level, internalFormat,
2191 format, type, 2, postConvWidth, postConvHeight,
2192 1, border)) {
2193 return; /* error was recorded */
2194 }
2195
2196 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2197 texObj = _mesa_select_tex_object(ctx, texUnit, target);
2198 texImage = _mesa_get_tex_image(ctx, texUnit, target, level);
2199 if (!texImage) {
2200 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage2D");
2201 return;
2202 }
2203 else if (texImage->Data && !texImage->IsClientData) {
2204 /* free the old texture data */
2205 MESA_PBUFFER_FREE(texImage->Data);
2206 }
2207 texImage->Data = NULL;
2208 clear_teximage_fields(texImage); /* not really needed, but helpful */
2209 _mesa_init_teximage_fields(ctx, target, texImage,
2210 postConvWidth, postConvHeight, 1,
2211 border, internalFormat);
2212
2213 if (ctx->NewState & _IMAGE_NEW_TRANSFER_STATE)
2214 _mesa_update_state(ctx);
2215
2216 ASSERT(ctx->Driver.TexImage2D);
2217
2218 /* Give the texture to the driver! <pixels> may be null! */
2219 (*ctx->Driver.TexImage2D)(ctx, target, level, internalFormat,
2220 width, height, border, format, type, pixels,
2221 &ctx->Unpack, texObj, texImage);
2222
2223 ASSERT(texImage->TexFormat);
2224
2225 /* If driver didn't explicitly set these, use the defaults */
2226 if (!texImage->FetchTexelc)
2227 texImage->FetchTexelc = texImage->TexFormat->FetchTexel2D;
2228 if (!texImage->FetchTexelf)
2229 texImage->FetchTexelf = texImage->TexFormat->FetchTexel2Df;
2230 ASSERT(texImage->FetchTexelc);
2231 ASSERT(texImage->FetchTexelf);
2232
2233 /* state update */
2234 texObj->Complete = GL_FALSE;
2235 ctx->NewState |= _NEW_TEXTURE;
2236 }
2237 else if (target == GL_PROXY_TEXTURE_2D ||
2238 (target == GL_PROXY_TEXTURE_CUBE_MAP_ARB &&
2239 ctx->Extensions.ARB_texture_cube_map) ||
2240 (target == GL_PROXY_TEXTURE_RECTANGLE_NV &&
2241 ctx->Extensions.NV_texture_rectangle)) {
2242 /* Proxy texture: check for errors and update proxy state */
2243 struct gl_texture_image *texImage;
2244 texImage = _mesa_get_proxy_tex_image(ctx, target, level);
2245 if (texture_error_check(ctx, target, level, internalFormat,
2246 format, type, 2, postConvWidth, postConvHeight,
2247 1, border)) {
2248 /* when error, clear all proxy texture image parameters */
2249 if (texImage)
2250 clear_teximage_fields(ctx->Texture.Proxy2D->Image[0][level]);
2251 }
2252 else {
2253 /* no error, set the tex image parameters */
2254 _mesa_init_teximage_fields(ctx, target, texImage,
2255 postConvWidth, postConvHeight, 1,
2256 border, internalFormat);
2257 texImage->TexFormat = (*ctx->Driver.ChooseTextureFormat)(ctx,
2258 internalFormat, format, type);
2259 }
2260 }
2261 else {
2262 _mesa_error( ctx, GL_INVALID_ENUM, "glTexImage2D(target)" );
2263 return;
2264 }
2265 }
2266
2267
2268 /*
2269 * Called by the API or display list executor.
2270 * Note that width and height include the border.
2271 */
2272 void GLAPIENTRY
2273 _mesa_TexImage3D( GLenum target, GLint level, GLint internalFormat,
2274 GLsizei width, GLsizei height, GLsizei depth,
2275 GLint border, GLenum format, GLenum type,
2276 const GLvoid *pixels )
2277 {
2278 GET_CURRENT_CONTEXT(ctx);
2279 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2280
2281 if (target == GL_TEXTURE_3D) {
2282 struct gl_texture_unit *texUnit;
2283 struct gl_texture_object *texObj;
2284 struct gl_texture_image *texImage;
2285
2286 if (texture_error_check(ctx, target, level, (GLint) internalFormat,
2287 format, type, 3, width, height, depth, border)) {
2288 return; /* error was recorded */
2289 }
2290
2291 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2292 texObj = _mesa_select_tex_object(ctx, texUnit, target);
2293 texImage = _mesa_get_tex_image(ctx, texUnit, target, level);
2294 if (!texImage) {
2295 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage3D");
2296 return;
2297 }
2298 else if (texImage->Data && !texImage->IsClientData) {
2299 MESA_PBUFFER_FREE(texImage->Data);
2300 }
2301 texImage->Data = NULL;
2302 clear_teximage_fields(texImage); /* not really needed, but helpful */
2303 _mesa_init_teximage_fields(ctx, target, texImage,
2304 width, height, depth,
2305 border, internalFormat);
2306
2307 if (ctx->NewState & _IMAGE_NEW_TRANSFER_STATE)
2308 _mesa_update_state(ctx);
2309
2310 ASSERT(ctx->Driver.TexImage3D);
2311
2312 /* Give the texture to the driver! <pixels> may be null! */
2313 (*ctx->Driver.TexImage3D)(ctx, target, level, internalFormat,
2314 width, height, depth, border, format, type,
2315 pixels, &ctx->Unpack, texObj, texImage);
2316
2317 ASSERT(texImage->TexFormat);
2318
2319 /* If driver didn't explicitly set these, use the defaults */
2320 if (!texImage->FetchTexelc)
2321 texImage->FetchTexelc = texImage->TexFormat->FetchTexel3D;
2322 if (!texImage->FetchTexelf)
2323 texImage->FetchTexelf = texImage->TexFormat->FetchTexel3Df;
2324 ASSERT(texImage->FetchTexelc);
2325 ASSERT(texImage->FetchTexelf);
2326
2327 /* state update */
2328 texObj->Complete = GL_FALSE;
2329 ctx->NewState |= _NEW_TEXTURE;
2330 }
2331 else if (target == GL_PROXY_TEXTURE_3D) {
2332 /* Proxy texture: check for errors and update proxy state */
2333 struct gl_texture_image *texImage;
2334 texImage = _mesa_get_proxy_tex_image(ctx, target, level);
2335 if (texture_error_check(ctx, target, level, internalFormat,
2336 format, type, 3, width, height, depth, border)) {
2337 /* when error, clear all proxy texture image parameters */
2338 if (texImage)
2339 clear_teximage_fields(texImage);
2340 }
2341 else {
2342 /* no error, set the tex image parameters */
2343 _mesa_init_teximage_fields(ctx, target, texImage, width, height, 1,
2344 border, internalFormat);
2345 texImage->TexFormat = (*ctx->Driver.ChooseTextureFormat)(ctx,
2346 internalFormat, format, type);
2347 }
2348 }
2349 else {
2350 _mesa_error( ctx, GL_INVALID_ENUM, "glTexImage3D(target)" );
2351 return;
2352 }
2353 }
2354
2355
2356 void GLAPIENTRY
2357 _mesa_TexImage3DEXT( GLenum target, GLint level, GLenum internalFormat,
2358 GLsizei width, GLsizei height, GLsizei depth,
2359 GLint border, GLenum format, GLenum type,
2360 const GLvoid *pixels )
2361 {
2362 _mesa_TexImage3D(target, level, (GLint) internalFormat, width, height,
2363 depth, border, format, type, pixels);
2364 }
2365
2366
2367
2368 void GLAPIENTRY
2369 _mesa_TexSubImage1D( GLenum target, GLint level,
2370 GLint xoffset, GLsizei width,
2371 GLenum format, GLenum type,
2372 const GLvoid *pixels )
2373 {
2374 GLsizei postConvWidth = width;
2375 struct gl_texture_unit *texUnit;
2376 struct gl_texture_object *texObj;
2377 struct gl_texture_image *texImage;
2378 GET_CURRENT_CONTEXT(ctx);
2379 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2380
2381 if (ctx->NewState & _IMAGE_NEW_TRANSFER_STATE)
2382 _mesa_update_state(ctx);
2383
2384 /* XXX should test internal format */
2385 if (is_color_format(format)) {
2386 _mesa_adjust_image_for_convolution(ctx, 1, &postConvWidth, NULL);
2387 }
2388
2389 if (subtexture_error_check(ctx, 1, target, level, xoffset, 0, 0,
2390 postConvWidth, 1, 1, format, type)) {
2391 return; /* error was detected */
2392 }
2393
2394 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2395 texObj = _mesa_select_tex_object(ctx, texUnit, target);
2396 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
2397 assert(texImage);
2398
2399 if (width == 0 || !pixels)
2400 return; /* no-op, not an error */
2401
2402 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2403 xoffset += texImage->Border;
2404
2405 ASSERT(ctx->Driver.TexSubImage1D);
2406 (*ctx->Driver.TexSubImage1D)(ctx, target, level, xoffset, width,
2407 format, type, pixels, &ctx->Unpack,
2408 texObj, texImage);
2409 ctx->NewState |= _NEW_TEXTURE;
2410 }
2411
2412
2413 void GLAPIENTRY
2414 _mesa_TexSubImage2D( GLenum target, GLint level,
2415 GLint xoffset, GLint yoffset,
2416 GLsizei width, GLsizei height,
2417 GLenum format, GLenum type,
2418 const GLvoid *pixels )
2419 {
2420 GLsizei postConvWidth = width, postConvHeight = height;
2421 struct gl_texture_unit *texUnit;
2422 struct gl_texture_object *texObj;
2423 struct gl_texture_image *texImage;
2424 GET_CURRENT_CONTEXT(ctx);
2425 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2426
2427 if (ctx->NewState & _IMAGE_NEW_TRANSFER_STATE)
2428 _mesa_update_state(ctx);
2429
2430 /* XXX should test internal format */
2431 if (is_color_format(format)) {
2432 _mesa_adjust_image_for_convolution(ctx, 2, &postConvWidth,
2433 &postConvHeight);
2434 }
2435
2436 if (subtexture_error_check(ctx, 2, target, level, xoffset, yoffset, 0,
2437 postConvWidth, postConvHeight, 1, format, type)) {
2438 return; /* error was detected */
2439 }
2440
2441 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2442 texObj = _mesa_select_tex_object(ctx, texUnit, target);
2443 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
2444 assert(texImage);
2445
2446 if (width == 0 || height == 0 || !pixels)
2447 return; /* no-op, not an error */
2448
2449 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2450 xoffset += texImage->Border;
2451 yoffset += texImage->Border;
2452
2453 ASSERT(ctx->Driver.TexSubImage2D);
2454 (*ctx->Driver.TexSubImage2D)(ctx, target, level, xoffset, yoffset,
2455 width, height, format, type, pixels,
2456 &ctx->Unpack, texObj, texImage);
2457 ctx->NewState |= _NEW_TEXTURE;
2458 }
2459
2460
2461
2462 void GLAPIENTRY
2463 _mesa_TexSubImage3D( GLenum target, GLint level,
2464 GLint xoffset, GLint yoffset, GLint zoffset,
2465 GLsizei width, GLsizei height, GLsizei depth,
2466 GLenum format, GLenum type,
2467 const GLvoid *pixels )
2468 {
2469 struct gl_texture_unit *texUnit;
2470 struct gl_texture_object *texObj;
2471 struct gl_texture_image *texImage;
2472 GET_CURRENT_CONTEXT(ctx);
2473 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2474
2475 if (ctx->NewState & _IMAGE_NEW_TRANSFER_STATE)
2476 _mesa_update_state(ctx);
2477
2478 if (subtexture_error_check(ctx, 3, target, level, xoffset, yoffset, zoffset,
2479 width, height, depth, format, type)) {
2480 return; /* error was detected */
2481 }
2482
2483 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2484 texObj = _mesa_select_tex_object(ctx, texUnit, target);
2485 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
2486 assert(texImage);
2487
2488 if (width == 0 || height == 0 || height == 0 || !pixels)
2489 return; /* no-op, not an error */
2490
2491 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2492 xoffset += texImage->Border;
2493 yoffset += texImage->Border;
2494 zoffset += texImage->Border;
2495
2496 ASSERT(ctx->Driver.TexSubImage3D);
2497 (*ctx->Driver.TexSubImage3D)(ctx, target, level,
2498 xoffset, yoffset, zoffset,
2499 width, height, depth,
2500 format, type, pixels,
2501 &ctx->Unpack, texObj, texImage );
2502 ctx->NewState |= _NEW_TEXTURE;
2503 }
2504
2505
2506
2507 void GLAPIENTRY
2508 _mesa_CopyTexImage1D( GLenum target, GLint level,
2509 GLenum internalFormat,
2510 GLint x, GLint y,
2511 GLsizei width, GLint border )
2512 {
2513 struct gl_texture_unit *texUnit;
2514 struct gl_texture_object *texObj;
2515 struct gl_texture_image *texImage;
2516 GLsizei postConvWidth = width;
2517 GET_CURRENT_CONTEXT(ctx);
2518 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2519
2520 if (ctx->NewState & _IMAGE_NEW_TRANSFER_STATE)
2521 _mesa_update_state(ctx);
2522
2523 if (is_color_format(internalFormat)) {
2524 _mesa_adjust_image_for_convolution(ctx, 1, &postConvWidth, NULL);
2525 }
2526
2527 if (copytexture_error_check(ctx, 1, target, level, internalFormat,
2528 postConvWidth, 1, border))
2529 return;
2530
2531 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2532 texObj = _mesa_select_tex_object(ctx, texUnit, target);
2533 texImage = _mesa_get_tex_image(ctx, texUnit, target, level);
2534 if (!texImage) {
2535 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexImage1D");
2536 return;
2537 }
2538 else if (texImage->Data && !texImage->IsClientData) {
2539 /* free the old texture data */
2540 MESA_PBUFFER_FREE(texImage->Data);
2541 }
2542 texImage->Data = NULL;
2543
2544 clear_teximage_fields(texImage); /* not really needed, but helpful */
2545 _mesa_init_teximage_fields(ctx, target, texImage, postConvWidth, 1, 1,
2546 border, internalFormat);
2547
2548
2549 ASSERT(ctx->Driver.CopyTexImage1D);
2550 (*ctx->Driver.CopyTexImage1D)(ctx, target, level, internalFormat,
2551 x, y, width, border);
2552
2553 ASSERT(texImage->TexFormat);
2554
2555 /* If driver didn't explicitly set these, use the defaults */
2556 if (!texImage->FetchTexelc)
2557 texImage->FetchTexelc = texImage->TexFormat->FetchTexel1D;
2558 if (!texImage->FetchTexelf)
2559 texImage->FetchTexelf = texImage->TexFormat->FetchTexel1Df;
2560 ASSERT(texImage->FetchTexelc);
2561 ASSERT(texImage->FetchTexelf);
2562
2563 /* state update */
2564 texObj->Complete = GL_FALSE;
2565 ctx->NewState |= _NEW_TEXTURE;
2566 }
2567
2568
2569
2570 void GLAPIENTRY
2571 _mesa_CopyTexImage2D( GLenum target, GLint level, GLenum internalFormat,
2572 GLint x, GLint y, GLsizei width, GLsizei height,
2573 GLint border )
2574 {
2575 struct gl_texture_unit *texUnit;
2576 struct gl_texture_object *texObj;
2577 struct gl_texture_image *texImage;
2578 GLsizei postConvWidth = width, postConvHeight = height;
2579 GET_CURRENT_CONTEXT(ctx);
2580 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2581
2582 if (ctx->NewState & _IMAGE_NEW_TRANSFER_STATE)
2583 _mesa_update_state(ctx);
2584
2585 if (is_color_format(internalFormat)) {
2586 _mesa_adjust_image_for_convolution(ctx, 2, &postConvWidth,
2587 &postConvHeight);
2588 }
2589
2590 if (copytexture_error_check(ctx, 2, target, level, internalFormat,
2591 postConvWidth, postConvHeight, border))
2592 return;
2593
2594 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2595 texObj = _mesa_select_tex_object(ctx, texUnit, target);
2596 texImage = _mesa_get_tex_image(ctx, texUnit, target, level);
2597 if (!texImage) {
2598 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexImage2D");
2599 return;
2600 }
2601 else if (texImage->Data && !texImage->IsClientData) {
2602 /* free the old texture data */
2603 MESA_PBUFFER_FREE(texImage->Data);
2604 }
2605 texImage->Data = NULL;
2606
2607 clear_teximage_fields(texImage); /* not really needed, but helpful */
2608 _mesa_init_teximage_fields(ctx, target, texImage,
2609 postConvWidth, postConvHeight, 1,
2610 border, internalFormat);
2611
2612 ASSERT(ctx->Driver.CopyTexImage2D);
2613 (*ctx->Driver.CopyTexImage2D)(ctx, target, level, internalFormat,
2614 x, y, width, height, border);
2615
2616 ASSERT(texImage->TexFormat);
2617
2618 /* If driver didn't explicitly set these, use the defaults */
2619 if (!texImage->FetchTexelc)
2620 texImage->FetchTexelc = texImage->TexFormat->FetchTexel2D;
2621 if (!texImage->FetchTexelf)
2622 texImage->FetchTexelf = texImage->TexFormat->FetchTexel2Df;
2623 ASSERT(texImage->FetchTexelc);
2624 ASSERT(texImage->FetchTexelf);
2625
2626 /* state update */
2627 texObj->Complete = GL_FALSE;
2628 ctx->NewState |= _NEW_TEXTURE;
2629 }
2630
2631
2632
2633 void GLAPIENTRY
2634 _mesa_CopyTexSubImage1D( GLenum target, GLint level,
2635 GLint xoffset, GLint x, GLint y, GLsizei width )
2636 {
2637 struct gl_texture_unit *texUnit;
2638 struct gl_texture_image *texImage;
2639 GLsizei postConvWidth = width;
2640 GET_CURRENT_CONTEXT(ctx);
2641 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2642
2643 if (ctx->NewState & _IMAGE_NEW_TRANSFER_STATE)
2644 _mesa_update_state(ctx);
2645
2646 /* XXX should test internal format */
2647 _mesa_adjust_image_for_convolution(ctx, 1, &postConvWidth, NULL);
2648
2649 if (copytexsubimage_error_check(ctx, 1, target, level,
2650 xoffset, 0, 0, postConvWidth, 1))
2651 return;
2652
2653 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2654 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
2655 ASSERT(texImage);
2656
2657 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2658 xoffset += texImage->Border;
2659
2660 ASSERT(ctx->Driver.CopyTexSubImage1D);
2661 (*ctx->Driver.CopyTexSubImage1D)(ctx, target, level, xoffset, x, y, width);
2662 ctx->NewState |= _NEW_TEXTURE;
2663 }
2664
2665
2666
2667 void GLAPIENTRY
2668 _mesa_CopyTexSubImage2D( GLenum target, GLint level,
2669 GLint xoffset, GLint yoffset,
2670 GLint x, GLint y, GLsizei width, GLsizei height )
2671 {
2672 struct gl_texture_unit *texUnit;
2673 struct gl_texture_image *texImage;
2674 GLsizei postConvWidth = width, postConvHeight = height;
2675 GET_CURRENT_CONTEXT(ctx);
2676 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2677
2678 if (ctx->NewState & _IMAGE_NEW_TRANSFER_STATE)
2679 _mesa_update_state(ctx);
2680
2681 /* XXX should test internal format */
2682 _mesa_adjust_image_for_convolution(ctx, 2, &postConvWidth, &postConvHeight);
2683
2684 if (copytexsubimage_error_check(ctx, 2, target, level, xoffset, yoffset, 0,
2685 postConvWidth, postConvHeight))
2686 return;
2687
2688 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2689 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
2690 ASSERT(texImage);
2691
2692 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2693 xoffset += texImage->Border;
2694 yoffset += texImage->Border;
2695
2696 ASSERT(ctx->Driver.CopyTexSubImage2D);
2697 (*ctx->Driver.CopyTexSubImage2D)(ctx, target, level,
2698 xoffset, yoffset, x, y, width, height);
2699 ctx->NewState |= _NEW_TEXTURE;
2700 }
2701
2702
2703
2704 void GLAPIENTRY
2705 _mesa_CopyTexSubImage3D( GLenum target, GLint level,
2706 GLint xoffset, GLint yoffset, GLint zoffset,
2707 GLint x, GLint y, GLsizei width, GLsizei height )
2708 {
2709 struct gl_texture_unit *texUnit;
2710 struct gl_texture_image *texImage;
2711 GLsizei postConvWidth = width, postConvHeight = height;
2712 GET_CURRENT_CONTEXT(ctx);
2713 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2714
2715 if (ctx->NewState & _IMAGE_NEW_TRANSFER_STATE)
2716 _mesa_update_state(ctx);
2717
2718 /* XXX should test internal format */
2719 _mesa_adjust_image_for_convolution(ctx, 2, &postConvWidth, &postConvHeight);
2720
2721 if (copytexsubimage_error_check(ctx, 3, target, level, xoffset, yoffset,
2722 zoffset, postConvWidth, postConvHeight))
2723 return;
2724
2725 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2726 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
2727 ASSERT(texImage);
2728
2729 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2730 xoffset += texImage->Border;
2731 yoffset += texImage->Border;
2732 zoffset += texImage->Border;
2733
2734 ASSERT(ctx->Driver.CopyTexSubImage3D);
2735 (*ctx->Driver.CopyTexSubImage3D)(ctx, target, level,
2736 xoffset, yoffset, zoffset,
2737 x, y, width, height);
2738 ctx->NewState |= _NEW_TEXTURE;
2739 }
2740
2741
2742
2743
2744 /**********************************************************************/
2745 /****** Compressed Textures ******/
2746 /**********************************************************************/
2747
2748
2749 /**
2750 * Error checking for glCompressedTexImage[123]D().
2751 * \return error code or GL_NO_ERROR.
2752 */
2753 static GLenum
2754 compressed_texture_error_check(GLcontext *ctx, GLint dimensions,
2755 GLenum target, GLint level,
2756 GLenum internalFormat, GLsizei width,
2757 GLsizei height, GLsizei depth, GLint border,
2758 GLsizei imageSize)
2759 {
2760 GLint expectedSize, maxLevels = 0, maxTextureSize;
2761
2762 if (dimensions == 1) {
2763 /* 1D compressed textures not allowed */
2764 return GL_INVALID_ENUM;
2765 }
2766 else if (dimensions == 2) {
2767 if (target == GL_PROXY_TEXTURE_2D) {
2768 maxLevels = ctx->Const.MaxTextureLevels;
2769 }
2770 else if (target == GL_TEXTURE_2D) {
2771 maxLevels = ctx->Const.MaxTextureLevels;
2772 }
2773 else if (target == GL_PROXY_TEXTURE_CUBE_MAP_ARB) {
2774 if (!ctx->Extensions.ARB_texture_cube_map)
2775 return GL_INVALID_ENUM; /*target*/
2776 maxLevels = ctx->Const.MaxCubeTextureLevels;
2777 }
2778 else if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
2779 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB) {
2780 if (!ctx->Extensions.ARB_texture_cube_map)
2781 return GL_INVALID_ENUM; /*target*/
2782 maxLevels = ctx->Const.MaxCubeTextureLevels;
2783 }
2784 else {
2785 return GL_INVALID_ENUM; /*target*/
2786 }
2787 }
2788 else if (dimensions == 3) {
2789 /* 3D compressed textures not allowed */
2790 return GL_INVALID_ENUM;
2791 }
2792
2793 maxTextureSize = 1 << (maxLevels - 1);
2794
2795 if (!is_compressed_format(ctx, internalFormat))
2796 return GL_INVALID_ENUM;
2797
2798 if (border != 0)
2799 return GL_INVALID_VALUE;
2800
2801 /*
2802 * XXX We should probably use the proxy texture error check function here.
2803 */
2804 if (width < 1 || width > maxTextureSize ||
2805 (!ctx->Extensions.ARB_texture_non_power_of_two && logbase2(width) < 0))
2806 return GL_INVALID_VALUE;
2807
2808 if ((height < 1 || height > maxTextureSize ||
2809 (!ctx->Extensions.ARB_texture_non_power_of_two && logbase2(height) < 0))
2810 && dimensions > 1)
2811 return GL_INVALID_VALUE;
2812
2813 if ((depth < 1 || depth > maxTextureSize ||
2814 (!ctx->Extensions.ARB_texture_non_power_of_two && logbase2(depth) < 0))
2815 && dimensions > 2)
2816 return GL_INVALID_VALUE;
2817
2818 /* For cube map, width must equal height */
2819 if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
2820 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB && width != height)
2821 return GL_INVALID_VALUE;
2822
2823 if (level < 0 || level >= maxLevels)
2824 return GL_INVALID_VALUE;
2825
2826 expectedSize = ctx->Driver.CompressedTextureSize(ctx, width, height, depth,
2827 internalFormat);
2828 if (expectedSize != imageSize)
2829 return GL_INVALID_VALUE;
2830
2831 return GL_NO_ERROR;
2832 }
2833
2834
2835 /**
2836 * Error checking for glCompressedTexSubImage[123]D().
2837 * \warning There are some bad assumptions here about the size of compressed
2838 * texture tiles (multiple of 4) used to test the validity of the
2839 * offset and size parameters.
2840 * \return error code or GL_NO_ERROR.
2841 */
2842 static GLenum
2843 compressed_subtexture_error_check(GLcontext *ctx, GLint dimensions,
2844 GLenum target, GLint level,
2845 GLint xoffset, GLint yoffset, GLint zoffset,
2846 GLsizei width, GLsizei height, GLsizei depth,
2847 GLenum format, GLsizei imageSize)
2848 {
2849 GLint expectedSize, maxLevels = 0, maxTextureSize;
2850 (void) zoffset;
2851
2852 if (dimensions == 1) {
2853 /* 1D compressed textures not allowed */
2854 return GL_INVALID_ENUM;
2855 }
2856 else if (dimensions == 2) {
2857 if (target == GL_PROXY_TEXTURE_2D) {
2858 maxLevels = ctx->Const.MaxTextureLevels;
2859 }
2860 else if (target == GL_TEXTURE_2D) {
2861 maxLevels = ctx->Const.MaxTextureLevels;
2862 }
2863 else if (target == GL_PROXY_TEXTURE_CUBE_MAP_ARB) {
2864 if (!ctx->Extensions.ARB_texture_cube_map)
2865 return GL_INVALID_ENUM; /*target*/
2866 maxLevels = ctx->Const.MaxCubeTextureLevels;
2867 }
2868 else if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
2869 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB) {
2870 if (!ctx->Extensions.ARB_texture_cube_map)
2871 return GL_INVALID_ENUM; /*target*/
2872 maxLevels = ctx->Const.MaxCubeTextureLevels;
2873 }
2874 else {
2875 return GL_INVALID_ENUM; /*target*/
2876 }
2877 }
2878 else if (dimensions == 3) {
2879 /* 3D compressed textures not allowed */
2880 return GL_INVALID_ENUM;
2881 }
2882
2883 maxTextureSize = 1 << (maxLevels - 1);
2884
2885 if (!is_compressed_format(ctx, format))
2886 return GL_INVALID_ENUM;
2887
2888 if (width < 1 || width > maxTextureSize)
2889 return GL_INVALID_VALUE;
2890
2891 if ((height < 1 || height > maxTextureSize)
2892 && dimensions > 1)
2893 return GL_INVALID_VALUE;
2894
2895 if (level < 0 || level >= maxLevels)
2896 return GL_INVALID_VALUE;
2897
2898 /* XXX these tests are specific to the compressed format.
2899 * this code should be generalized in some way.
2900 */
2901 if ((xoffset & 3) != 0 || (yoffset & 3) != 0)
2902 return GL_INVALID_VALUE;
2903
2904 if ((width & 3) != 0 && width != 2 && width != 1)
2905 return GL_INVALID_VALUE;
2906
2907 if ((height & 3) != 0 && height != 2 && height != 1)
2908 return GL_INVALID_VALUE;
2909
2910 expectedSize = ctx->Driver.CompressedTextureSize(ctx, width, height, depth,
2911 format);
2912 if (expectedSize != imageSize)
2913 return GL_INVALID_VALUE;
2914
2915 return GL_NO_ERROR;
2916 }
2917
2918
2919
2920 void GLAPIENTRY
2921 _mesa_CompressedTexImage1DARB(GLenum target, GLint level,
2922 GLenum internalFormat, GLsizei width,
2923 GLint border, GLsizei imageSize,
2924 const GLvoid *data)
2925 {
2926 GET_CURRENT_CONTEXT(ctx);
2927 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2928
2929 if (target == GL_TEXTURE_1D) {
2930 struct gl_texture_unit *texUnit;
2931 struct gl_texture_object *texObj;
2932 struct gl_texture_image *texImage;
2933 GLenum error = compressed_texture_error_check(ctx, 1, target, level,
2934 internalFormat, width, 1, 1, border, imageSize);
2935 if (error) {
2936 _mesa_error(ctx, error, "glCompressedTexImage1D");
2937 return;
2938 }
2939
2940 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2941 texObj = _mesa_select_tex_object(ctx, texUnit, target);
2942 texImage = _mesa_get_tex_image(ctx, texUnit, target, level);
2943 if (!texImage) {
2944 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCompressedTexImage1D");
2945 return;
2946 }
2947 else if (texImage->Data && !texImage->IsClientData) {
2948 MESA_PBUFFER_FREE(texImage->Data);
2949 }
2950 texImage->Data = NULL;
2951
2952 _mesa_init_teximage_fields(ctx, target, texImage, width, 1, 1,
2953 border, internalFormat);
2954
2955 ASSERT(ctx->Driver.CompressedTexImage1D);
2956 (*ctx->Driver.CompressedTexImage1D)(ctx, target, level,
2957 internalFormat, width, border,
2958 imageSize, data,
2959 texObj, texImage);
2960
2961 /* state update */
2962 texObj->Complete = GL_FALSE;
2963 ctx->NewState |= _NEW_TEXTURE;
2964 }
2965 else if (target == GL_PROXY_TEXTURE_1D) {
2966 /* Proxy texture: check for errors and update proxy state */
2967 GLenum error = compressed_texture_error_check(ctx, 1, target, level,
2968 internalFormat, width, 1, 1, border, imageSize);
2969 if (!error) {
2970 ASSERT(ctx->Driver.TestProxyTexImage);
2971 error = !(*ctx->Driver.TestProxyTexImage)(ctx, target, level,
2972 internalFormat, GL_NONE, GL_NONE,
2973 width, 1, 1, border);
2974 }
2975 if (error) {
2976 /* if error, clear all proxy texture image parameters */
2977 struct gl_texture_image *texImage;
2978 texImage = _mesa_get_proxy_tex_image(ctx, target, level);
2979 if (texImage)
2980 clear_teximage_fields(texImage);
2981 }
2982 else {
2983 /* store the teximage parameters */
2984 struct gl_texture_unit *texUnit;
2985 struct gl_texture_image *texImage;
2986 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2987 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
2988 _mesa_init_teximage_fields(ctx, target, texImage, width, 1, 1,
2989 border, internalFormat);
2990 }
2991 }
2992 else {
2993 _mesa_error(ctx, GL_INVALID_ENUM, "glCompressedTexImage1D(target)");
2994 return;
2995 }
2996 }
2997
2998
2999 void GLAPIENTRY
3000 _mesa_CompressedTexImage2DARB(GLenum target, GLint level,
3001 GLenum internalFormat, GLsizei width,
3002 GLsizei height, GLint border, GLsizei imageSize,
3003 const GLvoid *data)
3004 {
3005 GET_CURRENT_CONTEXT(ctx);
3006 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3007
3008 if (target == GL_TEXTURE_2D ||
3009 (ctx->Extensions.ARB_texture_cube_map &&
3010 target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
3011 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB)) {
3012 struct gl_texture_unit *texUnit;
3013 struct gl_texture_object *texObj;
3014 struct gl_texture_image *texImage;
3015 GLenum error = compressed_texture_error_check(ctx, 2, target, level,
3016 internalFormat, width, height, 1, border, imageSize);
3017 if (error) {
3018 _mesa_error(ctx, error, "glCompressedTexImage2D");
3019 return;
3020 }
3021
3022 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
3023 texObj = _mesa_select_tex_object(ctx, texUnit, target);
3024 texImage = _mesa_get_tex_image(ctx, texUnit, target, level);
3025 if (!texImage) {
3026 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCompressedTexImage2D");
3027 return;
3028 }
3029 else if (texImage->Data && !texImage->IsClientData) {
3030 MESA_PBUFFER_FREE(texImage->Data);
3031 }
3032 texImage->Data = NULL;
3033
3034 _mesa_init_teximage_fields(ctx, target, texImage, width, height, 1,
3035 border, internalFormat);
3036
3037 ASSERT(ctx->Driver.CompressedTexImage2D);
3038 (*ctx->Driver.CompressedTexImage2D)(ctx, target, level,
3039 internalFormat, width, height,
3040 border, imageSize, data,
3041 texObj, texImage);
3042
3043 /* state update */
3044 texObj->Complete = GL_FALSE;
3045 ctx->NewState |= _NEW_TEXTURE;
3046 }
3047 else if (target == GL_PROXY_TEXTURE_2D ||
3048 (target == GL_PROXY_TEXTURE_CUBE_MAP_ARB &&
3049 ctx->Extensions.ARB_texture_cube_map)) {
3050 /* Proxy texture: check for errors and update proxy state */
3051 GLenum error = compressed_texture_error_check(ctx, 2, target, level,
3052 internalFormat, width, height, 1, border, imageSize);
3053 if (!error) {
3054 ASSERT(ctx->Driver.TestProxyTexImage);
3055 error = !(*ctx->Driver.TestProxyTexImage)(ctx, target, level,
3056 internalFormat, GL_NONE, GL_NONE,
3057 width, height, 1, border);
3058 }
3059 if (error) {
3060 /* if error, clear all proxy texture image parameters */
3061 struct gl_texture_image *texImage;
3062 texImage = _mesa_get_proxy_tex_image(ctx, target, level);
3063 if (texImage)
3064 clear_teximage_fields(texImage);
3065 }
3066 else {
3067 /* store the teximage parameters */
3068 struct gl_texture_unit *texUnit;
3069 struct gl_texture_image *texImage;
3070 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
3071 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
3072 _mesa_init_teximage_fields(ctx, target, texImage, width, height, 1,
3073 border, internalFormat);
3074 }
3075 }
3076 else {
3077 _mesa_error(ctx, GL_INVALID_ENUM, "glCompressedTexImage2D(target)");
3078 return;
3079 }
3080 }
3081
3082
3083 void GLAPIENTRY
3084 _mesa_CompressedTexImage3DARB(GLenum target, GLint level,
3085 GLenum internalFormat, GLsizei width,
3086 GLsizei height, GLsizei depth, GLint border,
3087 GLsizei imageSize, const GLvoid *data)
3088 {
3089 GET_CURRENT_CONTEXT(ctx);
3090 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3091
3092 if (target == GL_TEXTURE_3D) {
3093 struct gl_texture_unit *texUnit;
3094 struct gl_texture_object *texObj;
3095 struct gl_texture_image *texImage;
3096 GLenum error = compressed_texture_error_check(ctx, 3, target, level,
3097 internalFormat, width, height, depth, border, imageSize);
3098 if (error) {
3099 _mesa_error(ctx, error, "glCompressedTexImage3D");
3100 return;
3101 }
3102
3103 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
3104 texObj = _mesa_select_tex_object(ctx, texUnit, target);
3105 texImage = _mesa_get_tex_image(ctx, texUnit, target, level);
3106 if (!texImage) {
3107 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCompressedTexImage3D");
3108 return;
3109 }
3110 else if (texImage->Data && !texImage->IsClientData) {
3111 MESA_PBUFFER_FREE(texImage->Data);
3112 }
3113 texImage->Data = NULL;
3114
3115 _mesa_init_teximage_fields(ctx, target, texImage, width, height, depth,
3116 border, internalFormat);
3117
3118 ASSERT(ctx->Driver.CompressedTexImage3D);
3119 (*ctx->Driver.CompressedTexImage3D)(ctx, target, level,
3120 internalFormat,
3121 width, height, depth,
3122 border, imageSize, data,
3123 texObj, texImage);
3124
3125 /* state update */
3126 texObj->Complete = GL_FALSE;
3127 ctx->NewState |= _NEW_TEXTURE;
3128 }
3129 else if (target == GL_PROXY_TEXTURE_3D) {
3130 /* Proxy texture: check for errors and update proxy state */
3131 GLenum error = compressed_texture_error_check(ctx, 3, target, level,
3132 internalFormat, width, height, depth, border, imageSize);
3133 if (!error) {
3134 ASSERT(ctx->Driver.TestProxyTexImage);
3135 error = !(*ctx->Driver.TestProxyTexImage)(ctx, target, level,
3136 internalFormat, GL_NONE, GL_NONE,
3137 width, height, depth, border);
3138 }
3139 if (error) {
3140 /* if error, clear all proxy texture image parameters */
3141 struct gl_texture_image *texImage;
3142 texImage = _mesa_get_proxy_tex_image(ctx, target, level);
3143 if (texImage)
3144 clear_teximage_fields(texImage);
3145 }
3146 else {
3147 /* store the teximage parameters */
3148 struct gl_texture_unit *texUnit;
3149 struct gl_texture_image *texImage;
3150 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
3151 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
3152 _mesa_init_teximage_fields(ctx, target, texImage, width, height,
3153 depth, border, internalFormat);
3154 }
3155 }
3156 else {
3157 _mesa_error(ctx, GL_INVALID_ENUM, "glCompressedTexImage3D(target)");
3158 return;
3159 }
3160 }
3161
3162
3163 void GLAPIENTRY
3164 _mesa_CompressedTexSubImage1DARB(GLenum target, GLint level, GLint xoffset,
3165 GLsizei width, GLenum format,
3166 GLsizei imageSize, const GLvoid *data)
3167 {
3168 struct gl_texture_unit *texUnit;
3169 struct gl_texture_object *texObj;
3170 struct gl_texture_image *texImage;
3171 GLenum error;
3172 GET_CURRENT_CONTEXT(ctx);
3173 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3174
3175 error = compressed_subtexture_error_check(ctx, 1, target, level,
3176 xoffset, 0, 0, width, 1, 1, format, imageSize);
3177 if (error) {
3178 _mesa_error(ctx, error, "glCompressedTexSubImage1D");
3179 return;
3180 }
3181
3182 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
3183 texObj = _mesa_select_tex_object(ctx, texUnit, target);
3184 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
3185 assert(texImage);
3186
3187 if ((GLint) format != texImage->IntFormat) {
3188 _mesa_error(ctx, GL_INVALID_OPERATION,
3189 "glCompressedTexSubImage1D(format)");
3190 return;
3191 }
3192
3193 if ((width == 1 || width == 2) && (GLuint) width != texImage->Width) {
3194 _mesa_error(ctx, GL_INVALID_VALUE, "glCompressedTexSubImage1D(width)");
3195 return;
3196 }
3197
3198 if (width == 0 || !data)
3199 return; /* no-op, not an error */
3200
3201 if (ctx->Driver.CompressedTexSubImage1D) {
3202 (*ctx->Driver.CompressedTexSubImage1D)(ctx, target, level,
3203 xoffset, width,
3204 format, imageSize, data,
3205 texObj, texImage);
3206 }
3207 ctx->NewState |= _NEW_TEXTURE;
3208 }
3209
3210
3211 void GLAPIENTRY
3212 _mesa_CompressedTexSubImage2DARB(GLenum target, GLint level, GLint xoffset,
3213 GLint yoffset, GLsizei width, GLsizei height,
3214 GLenum format, GLsizei imageSize,
3215 const GLvoid *data)
3216 {
3217 struct gl_texture_unit *texUnit;
3218 struct gl_texture_object *texObj;
3219 struct gl_texture_image *texImage;
3220 GLenum error;
3221 GET_CURRENT_CONTEXT(ctx);
3222 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3223
3224 error = compressed_subtexture_error_check(ctx, 2, target, level,
3225 xoffset, yoffset, 0, width, height, 1, format, imageSize);
3226 if (error) {
3227 /* XXX proxy target? */
3228 _mesa_error(ctx, error, "glCompressedTexSubImage2D");
3229 return;
3230 }
3231
3232 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
3233 texObj = _mesa_select_tex_object(ctx, texUnit, target);
3234 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
3235 assert(texImage);
3236
3237 if ((GLint) format != texImage->IntFormat) {
3238 _mesa_error(ctx, GL_INVALID_OPERATION,
3239 "glCompressedTexSubImage2D(format)");
3240 return;
3241 }
3242
3243 if (((width == 1 || width == 2) && (GLuint) width != texImage->Width) ||
3244 ((height == 1 || height == 2) && (GLuint) height != texImage->Height)) {
3245 _mesa_error(ctx, GL_INVALID_VALUE, "glCompressedTexSubImage2D(size)");
3246 return;
3247 }
3248
3249 if (width == 0 || height == 0 || !data)
3250 return; /* no-op, not an error */
3251
3252 if (ctx->Driver.CompressedTexSubImage2D) {
3253 (*ctx->Driver.CompressedTexSubImage2D)(ctx, target, level,
3254 xoffset, yoffset, width, height,
3255 format, imageSize, data,
3256 texObj, texImage);
3257 }
3258 ctx->NewState |= _NEW_TEXTURE;
3259 }
3260
3261
3262 void GLAPIENTRY
3263 _mesa_CompressedTexSubImage3DARB(GLenum target, GLint level, GLint xoffset,
3264 GLint yoffset, GLint zoffset, GLsizei width,
3265 GLsizei height, GLsizei depth, GLenum format,
3266 GLsizei imageSize, const GLvoid *data)
3267 {
3268 struct gl_texture_unit *texUnit;
3269 struct gl_texture_object *texObj;
3270 struct gl_texture_image *texImage;
3271 GLenum error;
3272 GET_CURRENT_CONTEXT(ctx);
3273 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3274
3275 error = compressed_subtexture_error_check(ctx, 3, target, level,
3276 xoffset, yoffset, zoffset, width, height, depth, format, imageSize);
3277 if (error) {
3278 _mesa_error(ctx, error, "glCompressedTexSubImage2D");
3279 return;
3280 }
3281
3282 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
3283 texObj = _mesa_select_tex_object(ctx, texUnit, target);
3284 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
3285 assert(texImage);
3286
3287 if ((GLint) format != texImage->IntFormat) {
3288 _mesa_error(ctx, GL_INVALID_OPERATION,
3289 "glCompressedTexSubImage3D(format)");
3290 return;
3291 }
3292
3293 if (((width == 1 || width == 2) && (GLuint) width != texImage->Width) ||
3294 ((height == 1 || height == 2) && (GLuint) height != texImage->Height) ||
3295 ((depth == 1 || depth == 2) && (GLuint) depth != texImage->Depth)) {
3296 _mesa_error(ctx, GL_INVALID_VALUE, "glCompressedTexSubImage3D(size)");
3297 return;
3298 }
3299
3300 if (width == 0 || height == 0 || depth == 0 || !data)
3301 return; /* no-op, not an error */
3302
3303 if (ctx->Driver.CompressedTexSubImage3D) {
3304 (*ctx->Driver.CompressedTexSubImage3D)(ctx, target, level,
3305 xoffset, yoffset, zoffset,
3306 width, height, depth,
3307 format, imageSize, data,
3308 texObj, texImage);
3309 }
3310 ctx->NewState |= _NEW_TEXTURE;
3311 }
3312
3313
3314 void GLAPIENTRY
3315 _mesa_GetCompressedTexImageARB(GLenum target, GLint level, GLvoid *img)
3316 {
3317 const struct gl_texture_unit *texUnit;
3318 const struct gl_texture_object *texObj;
3319 struct gl_texture_image *texImage;
3320 GLint maxLevels;
3321 GET_CURRENT_CONTEXT(ctx);
3322 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3323
3324 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
3325 texObj = _mesa_select_tex_object(ctx, texUnit, target);
3326 if (!texObj) {
3327 _mesa_error(ctx, GL_INVALID_ENUM, "glGetCompressedTexImageARB");
3328 return;
3329 }
3330
3331 maxLevels = _mesa_max_texture_levels(ctx, target);
3332 ASSERT(maxLevels > 0); /* 0 indicates bad target, caught above */
3333
3334 if (level < 0 || level >= maxLevels) {
3335 _mesa_error(ctx, GL_INVALID_VALUE, "glGetCompressedTexImageARB(level)");
3336 return;
3337 }
3338
3339 if (is_proxy_target(target)) {
3340 _mesa_error(ctx, GL_INVALID_ENUM, "glGetCompressedTexImageARB(target)");
3341 return;
3342 }
3343
3344 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
3345 if (!texImage) {
3346 /* probably invalid mipmap level */
3347 _mesa_error(ctx, GL_INVALID_VALUE, "glGetCompressedTexImageARB(level)");
3348 return;
3349 }
3350
3351 if (!texImage->IsCompressed) {
3352 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetCompressedTexImageARB");
3353 return;
3354 }
3355
3356 if (!img)
3357 return;
3358
3359 /* just memcpy, no pixelstore or pixel transfer */
3360 MEMCPY(img, texImage->Data, texImage->CompressedSize);
3361 }