- use new program option values from arbprogram.syn
[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 * XXX Move this code into a new driver fall-back function
1984 */
1985 {
1986 const GLint width = texImage->Width;
1987 const GLint height = texImage->Height;
1988 const GLint depth = texImage->Depth;
1989 GLint img, row;
1990 for (img = 0; img < depth; img++) {
1991 for (row = 0; row < height; row++) {
1992 /* compute destination address in client memory */
1993 GLvoid *dest = _mesa_image_address( &ctx->Pack, pixels,
1994 width, height, format, type,
1995 img, row, 0);
1996 assert(dest);
1997
1998 if (format == GL_COLOR_INDEX) {
1999 GLuint indexRow[MAX_WIDTH];
2000 GLint col;
2001 /* Can't use FetchTexel here because that returns RGBA */
2002 if (texImage->TexFormat->IndexBits == 8) {
2003 const GLubyte *src = (const GLubyte *) texImage->Data;
2004 for (col = 0; col < width; col++) {
2005 indexRow[col] = src[texImage->Width *
2006 (img * texImage->Height + row) + col];
2007 }
2008 }
2009 else if (texImage->TexFormat->IndexBits == 16) {
2010 const GLushort *src = (const GLushort *) texImage->Data;
2011 for (col = 0; col < width; col++) {
2012 indexRow[col] = src[texImage->Width *
2013 (img * texImage->Height + row) + col];
2014 }
2015 }
2016 else {
2017 _mesa_problem(ctx,
2018 "Color index problem in _mesa_GetTexImage");
2019 return;
2020 }
2021 _mesa_pack_index_span(ctx, width, type, dest,
2022 indexRow, &ctx->Pack,
2023 0 /* no image transfer */);
2024 }
2025 else if (format == GL_DEPTH_COMPONENT) {
2026 GLfloat depthRow[MAX_WIDTH];
2027 GLint col;
2028 for (col = 0; col < width; col++) {
2029 (*texImage->FetchTexelf)(texImage, col, row, img,
2030 depthRow + col);
2031 }
2032 _mesa_pack_depth_span(ctx, width, dest, type,
2033 depthRow, &ctx->Pack);
2034 }
2035 else if (format == GL_YCBCR_MESA) {
2036 /* No pixel transfer */
2037 const GLint rowstride = texImage->RowStride;
2038 MEMCPY(dest,
2039 (const GLushort *) texImage->Data + row * rowstride,
2040 width * sizeof(GLushort));
2041 /* check for byte swapping */
2042 if ((texImage->TexFormat->MesaFormat == MESA_FORMAT_YCBCR
2043 && type == GL_UNSIGNED_SHORT_8_8_REV_MESA) ||
2044 (texImage->TexFormat->MesaFormat == MESA_FORMAT_YCBCR_REV
2045 && type == GL_UNSIGNED_SHORT_8_8_MESA)) {
2046 if (!ctx->Pack.SwapBytes)
2047 _mesa_swap2((GLushort *) dest, width);
2048 }
2049 else if (ctx->Pack.SwapBytes) {
2050 _mesa_swap2((GLushort *) dest, width);
2051 }
2052 }
2053 else {
2054 /* general case: convert row to RGBA format */
2055 GLfloat rgba[MAX_WIDTH][4];
2056 GLint col;
2057 for (col = 0; col < width; col++) {
2058 (*texImage->FetchTexelf)(texImage, col, row, img, rgba[col]);
2059 }
2060 _mesa_pack_rgba_span_float(ctx, width,
2061 (const GLfloat (*)[4]) rgba,
2062 format, type, dest, &ctx->Pack,
2063 0 /* no image transfer */);
2064 } /* format */
2065 } /* row */
2066 } /* img */
2067 }
2068 }
2069
2070
2071
2072 /*
2073 * Called from the API. Note that width includes the border.
2074 */
2075 void GLAPIENTRY
2076 _mesa_TexImage1D( GLenum target, GLint level, GLint internalFormat,
2077 GLsizei width, GLint border, GLenum format,
2078 GLenum type, const GLvoid *pixels )
2079 {
2080 GLsizei postConvWidth = width;
2081 GET_CURRENT_CONTEXT(ctx);
2082 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2083
2084 if (is_color_format(internalFormat)) {
2085 _mesa_adjust_image_for_convolution(ctx, 1, &postConvWidth, NULL);
2086 }
2087
2088 if (target == GL_TEXTURE_1D) {
2089 struct gl_texture_unit *texUnit;
2090 struct gl_texture_object *texObj;
2091 struct gl_texture_image *texImage;
2092
2093 if (texture_error_check(ctx, target, level, internalFormat,
2094 format, type, 1, postConvWidth, 1, 1, border)) {
2095 return; /* error was recorded */
2096 }
2097
2098 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2099 texObj = _mesa_select_tex_object(ctx, texUnit, target);
2100 texImage = _mesa_get_tex_image(ctx, texUnit, target, level);
2101
2102 if (!texImage) {
2103 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage1D");
2104 return;
2105 }
2106 else if (texImage->Data && !texImage->IsClientData) {
2107 /* free the old texture data */
2108 MESA_PBUFFER_FREE(texImage->Data);
2109 }
2110 texImage->Data = NULL;
2111 clear_teximage_fields(texImage); /* not really needed, but helpful */
2112 _mesa_init_teximage_fields(ctx, target, texImage,
2113 postConvWidth, 1, 1,
2114 border, internalFormat);
2115
2116 if (ctx->NewState & _IMAGE_NEW_TRANSFER_STATE)
2117 _mesa_update_state(ctx);
2118
2119 ASSERT(ctx->Driver.TexImage1D);
2120
2121 /* Give the texture to the driver! <pixels> may be null! */
2122 (*ctx->Driver.TexImage1D)(ctx, target, level, internalFormat,
2123 width, border, format, type, pixels,
2124 &ctx->Unpack, texObj, texImage);
2125
2126 ASSERT(texImage->TexFormat);
2127
2128 /* If driver didn't explicitly set this, use the defaults */
2129 if (!texImage->FetchTexelc)
2130 texImage->FetchTexelc = texImage->TexFormat->FetchTexel1D;
2131 if (!texImage->FetchTexelf)
2132 texImage->FetchTexelf = texImage->TexFormat->FetchTexel1Df;
2133 ASSERT(texImage->FetchTexelc);
2134 ASSERT(texImage->FetchTexelf);
2135
2136 /* state update */
2137 texObj->Complete = GL_FALSE;
2138 ctx->NewState |= _NEW_TEXTURE;
2139 }
2140 else if (target == GL_PROXY_TEXTURE_1D) {
2141 /* Proxy texture: check for errors and update proxy state */
2142 struct gl_texture_image *texImage;
2143 texImage = _mesa_get_proxy_tex_image(ctx, target, level);
2144 if (texture_error_check(ctx, target, level, internalFormat,
2145 format, type, 1, postConvWidth, 1, 1, border)) {
2146 /* when error, clear all proxy texture image parameters */
2147 if (texImage)
2148 clear_teximage_fields(texImage);
2149 }
2150 else {
2151 /* no error, set the tex image parameters */
2152 ASSERT(texImage);
2153 _mesa_init_teximage_fields(ctx, target, texImage,
2154 postConvWidth, 1, 1,
2155 border, internalFormat);
2156 texImage->TexFormat = (*ctx->Driver.ChooseTextureFormat)(ctx,
2157 internalFormat, format, type);
2158 }
2159 }
2160 else {
2161 _mesa_error( ctx, GL_INVALID_ENUM, "glTexImage1D(target)" );
2162 return;
2163 }
2164 }
2165
2166
2167 void GLAPIENTRY
2168 _mesa_TexImage2D( GLenum target, GLint level, GLint internalFormat,
2169 GLsizei width, GLsizei height, GLint border,
2170 GLenum format, GLenum type,
2171 const GLvoid *pixels )
2172 {
2173 GLsizei postConvWidth = width, postConvHeight = height;
2174 GET_CURRENT_CONTEXT(ctx);
2175 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2176
2177 if (is_color_format(internalFormat)) {
2178 _mesa_adjust_image_for_convolution(ctx, 2, &postConvWidth,
2179 &postConvHeight);
2180 }
2181
2182 if (target == GL_TEXTURE_2D ||
2183 (ctx->Extensions.ARB_texture_cube_map &&
2184 target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
2185 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB) ||
2186 (ctx->Extensions.NV_texture_rectangle &&
2187 target == GL_TEXTURE_RECTANGLE_NV)) {
2188 /* non-proxy target */
2189 struct gl_texture_unit *texUnit;
2190 struct gl_texture_object *texObj;
2191 struct gl_texture_image *texImage;
2192
2193 if (texture_error_check(ctx, target, level, internalFormat,
2194 format, type, 2, postConvWidth, postConvHeight,
2195 1, border)) {
2196 return; /* error was recorded */
2197 }
2198
2199 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2200 texObj = _mesa_select_tex_object(ctx, texUnit, target);
2201 texImage = _mesa_get_tex_image(ctx, texUnit, target, level);
2202 if (!texImage) {
2203 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage2D");
2204 return;
2205 }
2206 else if (texImage->Data && !texImage->IsClientData) {
2207 /* free the old texture data */
2208 MESA_PBUFFER_FREE(texImage->Data);
2209 }
2210 texImage->Data = NULL;
2211 clear_teximage_fields(texImage); /* not really needed, but helpful */
2212 _mesa_init_teximage_fields(ctx, target, texImage,
2213 postConvWidth, postConvHeight, 1,
2214 border, internalFormat);
2215
2216 if (ctx->NewState & _IMAGE_NEW_TRANSFER_STATE)
2217 _mesa_update_state(ctx);
2218
2219 ASSERT(ctx->Driver.TexImage2D);
2220
2221 /* Give the texture to the driver! <pixels> may be null! */
2222 (*ctx->Driver.TexImage2D)(ctx, target, level, internalFormat,
2223 width, height, border, format, type, pixels,
2224 &ctx->Unpack, texObj, texImage);
2225
2226 ASSERT(texImage->TexFormat);
2227
2228 /* If driver didn't explicitly set these, use the defaults */
2229 if (!texImage->FetchTexelc)
2230 texImage->FetchTexelc = texImage->TexFormat->FetchTexel2D;
2231 if (!texImage->FetchTexelf)
2232 texImage->FetchTexelf = texImage->TexFormat->FetchTexel2Df;
2233 ASSERT(texImage->FetchTexelc);
2234 ASSERT(texImage->FetchTexelf);
2235
2236 /* state update */
2237 texObj->Complete = GL_FALSE;
2238 ctx->NewState |= _NEW_TEXTURE;
2239 }
2240 else if (target == GL_PROXY_TEXTURE_2D ||
2241 (target == GL_PROXY_TEXTURE_CUBE_MAP_ARB &&
2242 ctx->Extensions.ARB_texture_cube_map) ||
2243 (target == GL_PROXY_TEXTURE_RECTANGLE_NV &&
2244 ctx->Extensions.NV_texture_rectangle)) {
2245 /* Proxy texture: check for errors and update proxy state */
2246 struct gl_texture_image *texImage;
2247 texImage = _mesa_get_proxy_tex_image(ctx, target, level);
2248 if (texture_error_check(ctx, target, level, internalFormat,
2249 format, type, 2, postConvWidth, postConvHeight,
2250 1, border)) {
2251 /* when error, clear all proxy texture image parameters */
2252 if (texImage)
2253 clear_teximage_fields(ctx->Texture.Proxy2D->Image[0][level]);
2254 }
2255 else {
2256 /* no error, set the tex image parameters */
2257 _mesa_init_teximage_fields(ctx, target, texImage,
2258 postConvWidth, postConvHeight, 1,
2259 border, internalFormat);
2260 texImage->TexFormat = (*ctx->Driver.ChooseTextureFormat)(ctx,
2261 internalFormat, format, type);
2262 }
2263 }
2264 else {
2265 _mesa_error( ctx, GL_INVALID_ENUM, "glTexImage2D(target)" );
2266 return;
2267 }
2268 }
2269
2270
2271 /*
2272 * Called by the API or display list executor.
2273 * Note that width and height include the border.
2274 */
2275 void GLAPIENTRY
2276 _mesa_TexImage3D( GLenum target, GLint level, GLint internalFormat,
2277 GLsizei width, GLsizei height, GLsizei depth,
2278 GLint border, GLenum format, GLenum type,
2279 const GLvoid *pixels )
2280 {
2281 GET_CURRENT_CONTEXT(ctx);
2282 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2283
2284 if (target == GL_TEXTURE_3D) {
2285 struct gl_texture_unit *texUnit;
2286 struct gl_texture_object *texObj;
2287 struct gl_texture_image *texImage;
2288
2289 if (texture_error_check(ctx, target, level, (GLint) internalFormat,
2290 format, type, 3, width, height, depth, border)) {
2291 return; /* error was recorded */
2292 }
2293
2294 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2295 texObj = _mesa_select_tex_object(ctx, texUnit, target);
2296 texImage = _mesa_get_tex_image(ctx, texUnit, target, level);
2297 if (!texImage) {
2298 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage3D");
2299 return;
2300 }
2301 else if (texImage->Data && !texImage->IsClientData) {
2302 MESA_PBUFFER_FREE(texImage->Data);
2303 }
2304 texImage->Data = NULL;
2305 clear_teximage_fields(texImage); /* not really needed, but helpful */
2306 _mesa_init_teximage_fields(ctx, target, texImage,
2307 width, height, depth,
2308 border, internalFormat);
2309
2310 if (ctx->NewState & _IMAGE_NEW_TRANSFER_STATE)
2311 _mesa_update_state(ctx);
2312
2313 ASSERT(ctx->Driver.TexImage3D);
2314
2315 /* Give the texture to the driver! <pixels> may be null! */
2316 (*ctx->Driver.TexImage3D)(ctx, target, level, internalFormat,
2317 width, height, depth, border, format, type,
2318 pixels, &ctx->Unpack, texObj, texImage);
2319
2320 ASSERT(texImage->TexFormat);
2321
2322 /* If driver didn't explicitly set these, use the defaults */
2323 if (!texImage->FetchTexelc)
2324 texImage->FetchTexelc = texImage->TexFormat->FetchTexel3D;
2325 if (!texImage->FetchTexelf)
2326 texImage->FetchTexelf = texImage->TexFormat->FetchTexel3Df;
2327 ASSERT(texImage->FetchTexelc);
2328 ASSERT(texImage->FetchTexelf);
2329
2330 /* state update */
2331 texObj->Complete = GL_FALSE;
2332 ctx->NewState |= _NEW_TEXTURE;
2333 }
2334 else if (target == GL_PROXY_TEXTURE_3D) {
2335 /* Proxy texture: check for errors and update proxy state */
2336 struct gl_texture_image *texImage;
2337 texImage = _mesa_get_proxy_tex_image(ctx, target, level);
2338 if (texture_error_check(ctx, target, level, internalFormat,
2339 format, type, 3, width, height, depth, border)) {
2340 /* when error, clear all proxy texture image parameters */
2341 if (texImage)
2342 clear_teximage_fields(texImage);
2343 }
2344 else {
2345 /* no error, set the tex image parameters */
2346 _mesa_init_teximage_fields(ctx, target, texImage, width, height, 1,
2347 border, internalFormat);
2348 texImage->TexFormat = (*ctx->Driver.ChooseTextureFormat)(ctx,
2349 internalFormat, format, type);
2350 }
2351 }
2352 else {
2353 _mesa_error( ctx, GL_INVALID_ENUM, "glTexImage3D(target)" );
2354 return;
2355 }
2356 }
2357
2358
2359 void GLAPIENTRY
2360 _mesa_TexImage3DEXT( GLenum target, GLint level, GLenum internalFormat,
2361 GLsizei width, GLsizei height, GLsizei depth,
2362 GLint border, GLenum format, GLenum type,
2363 const GLvoid *pixels )
2364 {
2365 _mesa_TexImage3D(target, level, (GLint) internalFormat, width, height,
2366 depth, border, format, type, pixels);
2367 }
2368
2369
2370
2371 void GLAPIENTRY
2372 _mesa_TexSubImage1D( GLenum target, GLint level,
2373 GLint xoffset, GLsizei width,
2374 GLenum format, GLenum type,
2375 const GLvoid *pixels )
2376 {
2377 GLsizei postConvWidth = width;
2378 struct gl_texture_unit *texUnit;
2379 struct gl_texture_object *texObj;
2380 struct gl_texture_image *texImage;
2381 GET_CURRENT_CONTEXT(ctx);
2382 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2383
2384 if (ctx->NewState & _IMAGE_NEW_TRANSFER_STATE)
2385 _mesa_update_state(ctx);
2386
2387 /* XXX should test internal format */
2388 if (is_color_format(format)) {
2389 _mesa_adjust_image_for_convolution(ctx, 1, &postConvWidth, NULL);
2390 }
2391
2392 if (subtexture_error_check(ctx, 1, target, level, xoffset, 0, 0,
2393 postConvWidth, 1, 1, format, type)) {
2394 return; /* error was detected */
2395 }
2396
2397 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2398 texObj = _mesa_select_tex_object(ctx, texUnit, target);
2399 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
2400 assert(texImage);
2401
2402 if (width == 0)
2403 return; /* no-op, not an error */
2404
2405 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2406 xoffset += texImage->Border;
2407
2408 ASSERT(ctx->Driver.TexSubImage1D);
2409 (*ctx->Driver.TexSubImage1D)(ctx, target, level, xoffset, width,
2410 format, type, pixels, &ctx->Unpack,
2411 texObj, texImage);
2412 ctx->NewState |= _NEW_TEXTURE;
2413 }
2414
2415
2416 void GLAPIENTRY
2417 _mesa_TexSubImage2D( GLenum target, GLint level,
2418 GLint xoffset, GLint yoffset,
2419 GLsizei width, GLsizei height,
2420 GLenum format, GLenum type,
2421 const GLvoid *pixels )
2422 {
2423 GLsizei postConvWidth = width, postConvHeight = height;
2424 struct gl_texture_unit *texUnit;
2425 struct gl_texture_object *texObj;
2426 struct gl_texture_image *texImage;
2427 GET_CURRENT_CONTEXT(ctx);
2428 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2429
2430 if (ctx->NewState & _IMAGE_NEW_TRANSFER_STATE)
2431 _mesa_update_state(ctx);
2432
2433 /* XXX should test internal format */
2434 if (is_color_format(format)) {
2435 _mesa_adjust_image_for_convolution(ctx, 2, &postConvWidth,
2436 &postConvHeight);
2437 }
2438
2439 if (subtexture_error_check(ctx, 2, target, level, xoffset, yoffset, 0,
2440 postConvWidth, postConvHeight, 1, format, type)) {
2441 return; /* error was detected */
2442 }
2443
2444 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2445 texObj = _mesa_select_tex_object(ctx, texUnit, target);
2446 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
2447 assert(texImage);
2448
2449 if (width == 0 || height == 0)
2450 return; /* no-op, not an error */
2451
2452 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2453 xoffset += texImage->Border;
2454 yoffset += texImage->Border;
2455
2456 ASSERT(ctx->Driver.TexSubImage2D);
2457 (*ctx->Driver.TexSubImage2D)(ctx, target, level, xoffset, yoffset,
2458 width, height, format, type, pixels,
2459 &ctx->Unpack, texObj, texImage);
2460 ctx->NewState |= _NEW_TEXTURE;
2461 }
2462
2463
2464
2465 void GLAPIENTRY
2466 _mesa_TexSubImage3D( GLenum target, GLint level,
2467 GLint xoffset, GLint yoffset, GLint zoffset,
2468 GLsizei width, GLsizei height, GLsizei depth,
2469 GLenum format, GLenum type,
2470 const GLvoid *pixels )
2471 {
2472 struct gl_texture_unit *texUnit;
2473 struct gl_texture_object *texObj;
2474 struct gl_texture_image *texImage;
2475 GET_CURRENT_CONTEXT(ctx);
2476 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2477
2478 if (ctx->NewState & _IMAGE_NEW_TRANSFER_STATE)
2479 _mesa_update_state(ctx);
2480
2481 if (subtexture_error_check(ctx, 3, target, level, xoffset, yoffset, zoffset,
2482 width, height, depth, format, type)) {
2483 return; /* error was detected */
2484 }
2485
2486 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2487 texObj = _mesa_select_tex_object(ctx, texUnit, target);
2488 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
2489 assert(texImage);
2490
2491 if (width == 0 || height == 0 || height == 0)
2492 return; /* no-op, not an error */
2493
2494 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2495 xoffset += texImage->Border;
2496 yoffset += texImage->Border;
2497 zoffset += texImage->Border;
2498
2499 ASSERT(ctx->Driver.TexSubImage3D);
2500 (*ctx->Driver.TexSubImage3D)(ctx, target, level,
2501 xoffset, yoffset, zoffset,
2502 width, height, depth,
2503 format, type, pixels,
2504 &ctx->Unpack, texObj, texImage );
2505 ctx->NewState |= _NEW_TEXTURE;
2506 }
2507
2508
2509
2510 void GLAPIENTRY
2511 _mesa_CopyTexImage1D( GLenum target, GLint level,
2512 GLenum internalFormat,
2513 GLint x, GLint y,
2514 GLsizei width, GLint border )
2515 {
2516 struct gl_texture_unit *texUnit;
2517 struct gl_texture_object *texObj;
2518 struct gl_texture_image *texImage;
2519 GLsizei postConvWidth = width;
2520 GET_CURRENT_CONTEXT(ctx);
2521 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2522
2523 if (ctx->NewState & _IMAGE_NEW_TRANSFER_STATE)
2524 _mesa_update_state(ctx);
2525
2526 if (is_color_format(internalFormat)) {
2527 _mesa_adjust_image_for_convolution(ctx, 1, &postConvWidth, NULL);
2528 }
2529
2530 if (copytexture_error_check(ctx, 1, target, level, internalFormat,
2531 postConvWidth, 1, border))
2532 return;
2533
2534 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2535 texObj = _mesa_select_tex_object(ctx, texUnit, target);
2536 texImage = _mesa_get_tex_image(ctx, texUnit, target, level);
2537 if (!texImage) {
2538 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexImage1D");
2539 return;
2540 }
2541 else if (texImage->Data && !texImage->IsClientData) {
2542 /* free the old texture data */
2543 MESA_PBUFFER_FREE(texImage->Data);
2544 }
2545 texImage->Data = NULL;
2546
2547 clear_teximage_fields(texImage); /* not really needed, but helpful */
2548 _mesa_init_teximage_fields(ctx, target, texImage, postConvWidth, 1, 1,
2549 border, internalFormat);
2550
2551
2552 ASSERT(ctx->Driver.CopyTexImage1D);
2553 (*ctx->Driver.CopyTexImage1D)(ctx, target, level, internalFormat,
2554 x, y, width, border);
2555
2556 ASSERT(texImage->TexFormat);
2557
2558 /* If driver didn't explicitly set these, use the defaults */
2559 if (!texImage->FetchTexelc)
2560 texImage->FetchTexelc = texImage->TexFormat->FetchTexel1D;
2561 if (!texImage->FetchTexelf)
2562 texImage->FetchTexelf = texImage->TexFormat->FetchTexel1Df;
2563 ASSERT(texImage->FetchTexelc);
2564 ASSERT(texImage->FetchTexelf);
2565
2566 /* state update */
2567 texObj->Complete = GL_FALSE;
2568 ctx->NewState |= _NEW_TEXTURE;
2569 }
2570
2571
2572
2573 void GLAPIENTRY
2574 _mesa_CopyTexImage2D( GLenum target, GLint level, GLenum internalFormat,
2575 GLint x, GLint y, GLsizei width, GLsizei height,
2576 GLint border )
2577 {
2578 struct gl_texture_unit *texUnit;
2579 struct gl_texture_object *texObj;
2580 struct gl_texture_image *texImage;
2581 GLsizei postConvWidth = width, postConvHeight = height;
2582 GET_CURRENT_CONTEXT(ctx);
2583 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2584
2585 if (ctx->NewState & _IMAGE_NEW_TRANSFER_STATE)
2586 _mesa_update_state(ctx);
2587
2588 if (is_color_format(internalFormat)) {
2589 _mesa_adjust_image_for_convolution(ctx, 2, &postConvWidth,
2590 &postConvHeight);
2591 }
2592
2593 if (copytexture_error_check(ctx, 2, target, level, internalFormat,
2594 postConvWidth, postConvHeight, border))
2595 return;
2596
2597 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2598 texObj = _mesa_select_tex_object(ctx, texUnit, target);
2599 texImage = _mesa_get_tex_image(ctx, texUnit, target, level);
2600 if (!texImage) {
2601 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexImage2D");
2602 return;
2603 }
2604 else if (texImage->Data && !texImage->IsClientData) {
2605 /* free the old texture data */
2606 MESA_PBUFFER_FREE(texImage->Data);
2607 }
2608 texImage->Data = NULL;
2609
2610 clear_teximage_fields(texImage); /* not really needed, but helpful */
2611 _mesa_init_teximage_fields(ctx, target, texImage,
2612 postConvWidth, postConvHeight, 1,
2613 border, internalFormat);
2614
2615 ASSERT(ctx->Driver.CopyTexImage2D);
2616 (*ctx->Driver.CopyTexImage2D)(ctx, target, level, internalFormat,
2617 x, y, width, height, border);
2618
2619 ASSERT(texImage->TexFormat);
2620
2621 /* If driver didn't explicitly set these, use the defaults */
2622 if (!texImage->FetchTexelc)
2623 texImage->FetchTexelc = texImage->TexFormat->FetchTexel2D;
2624 if (!texImage->FetchTexelf)
2625 texImage->FetchTexelf = texImage->TexFormat->FetchTexel2Df;
2626 ASSERT(texImage->FetchTexelc);
2627 ASSERT(texImage->FetchTexelf);
2628
2629 /* state update */
2630 texObj->Complete = GL_FALSE;
2631 ctx->NewState |= _NEW_TEXTURE;
2632 }
2633
2634
2635
2636 void GLAPIENTRY
2637 _mesa_CopyTexSubImage1D( GLenum target, GLint level,
2638 GLint xoffset, GLint x, GLint y, GLsizei width )
2639 {
2640 struct gl_texture_unit *texUnit;
2641 struct gl_texture_image *texImage;
2642 GLsizei postConvWidth = width;
2643 GET_CURRENT_CONTEXT(ctx);
2644 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2645
2646 if (ctx->NewState & _IMAGE_NEW_TRANSFER_STATE)
2647 _mesa_update_state(ctx);
2648
2649 /* XXX should test internal format */
2650 _mesa_adjust_image_for_convolution(ctx, 1, &postConvWidth, NULL);
2651
2652 if (copytexsubimage_error_check(ctx, 1, target, level,
2653 xoffset, 0, 0, postConvWidth, 1))
2654 return;
2655
2656 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2657 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
2658 ASSERT(texImage);
2659
2660 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2661 xoffset += texImage->Border;
2662
2663 ASSERT(ctx->Driver.CopyTexSubImage1D);
2664 (*ctx->Driver.CopyTexSubImage1D)(ctx, target, level, xoffset, x, y, width);
2665 ctx->NewState |= _NEW_TEXTURE;
2666 }
2667
2668
2669
2670 void GLAPIENTRY
2671 _mesa_CopyTexSubImage2D( GLenum target, GLint level,
2672 GLint xoffset, GLint yoffset,
2673 GLint x, GLint y, GLsizei width, GLsizei height )
2674 {
2675 struct gl_texture_unit *texUnit;
2676 struct gl_texture_image *texImage;
2677 GLsizei postConvWidth = width, postConvHeight = height;
2678 GET_CURRENT_CONTEXT(ctx);
2679 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2680
2681 if (ctx->NewState & _IMAGE_NEW_TRANSFER_STATE)
2682 _mesa_update_state(ctx);
2683
2684 /* XXX should test internal format */
2685 _mesa_adjust_image_for_convolution(ctx, 2, &postConvWidth, &postConvHeight);
2686
2687 if (copytexsubimage_error_check(ctx, 2, target, level, xoffset, yoffset, 0,
2688 postConvWidth, postConvHeight))
2689 return;
2690
2691 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2692 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
2693 ASSERT(texImage);
2694
2695 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2696 xoffset += texImage->Border;
2697 yoffset += texImage->Border;
2698
2699 ASSERT(ctx->Driver.CopyTexSubImage2D);
2700 (*ctx->Driver.CopyTexSubImage2D)(ctx, target, level,
2701 xoffset, yoffset, x, y, width, height);
2702 ctx->NewState |= _NEW_TEXTURE;
2703 }
2704
2705
2706
2707 void GLAPIENTRY
2708 _mesa_CopyTexSubImage3D( GLenum target, GLint level,
2709 GLint xoffset, GLint yoffset, GLint zoffset,
2710 GLint x, GLint y, GLsizei width, GLsizei height )
2711 {
2712 struct gl_texture_unit *texUnit;
2713 struct gl_texture_image *texImage;
2714 GLsizei postConvWidth = width, postConvHeight = height;
2715 GET_CURRENT_CONTEXT(ctx);
2716 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2717
2718 if (ctx->NewState & _IMAGE_NEW_TRANSFER_STATE)
2719 _mesa_update_state(ctx);
2720
2721 /* XXX should test internal format */
2722 _mesa_adjust_image_for_convolution(ctx, 2, &postConvWidth, &postConvHeight);
2723
2724 if (copytexsubimage_error_check(ctx, 3, target, level, xoffset, yoffset,
2725 zoffset, postConvWidth, postConvHeight))
2726 return;
2727
2728 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2729 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
2730 ASSERT(texImage);
2731
2732 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2733 xoffset += texImage->Border;
2734 yoffset += texImage->Border;
2735 zoffset += texImage->Border;
2736
2737 ASSERT(ctx->Driver.CopyTexSubImage3D);
2738 (*ctx->Driver.CopyTexSubImage3D)(ctx, target, level,
2739 xoffset, yoffset, zoffset,
2740 x, y, width, height);
2741 ctx->NewState |= _NEW_TEXTURE;
2742 }
2743
2744
2745
2746
2747 /**********************************************************************/
2748 /****** Compressed Textures ******/
2749 /**********************************************************************/
2750
2751
2752 /**
2753 * Error checking for glCompressedTexImage[123]D().
2754 * \return error code or GL_NO_ERROR.
2755 */
2756 static GLenum
2757 compressed_texture_error_check(GLcontext *ctx, GLint dimensions,
2758 GLenum target, GLint level,
2759 GLenum internalFormat, GLsizei width,
2760 GLsizei height, GLsizei depth, GLint border,
2761 GLsizei imageSize)
2762 {
2763 GLint expectedSize, maxLevels = 0, maxTextureSize;
2764
2765 if (dimensions == 1) {
2766 /* 1D compressed textures not allowed */
2767 return GL_INVALID_ENUM;
2768 }
2769 else if (dimensions == 2) {
2770 if (target == GL_PROXY_TEXTURE_2D) {
2771 maxLevels = ctx->Const.MaxTextureLevels;
2772 }
2773 else if (target == GL_TEXTURE_2D) {
2774 maxLevels = ctx->Const.MaxTextureLevels;
2775 }
2776 else if (target == GL_PROXY_TEXTURE_CUBE_MAP_ARB) {
2777 if (!ctx->Extensions.ARB_texture_cube_map)
2778 return GL_INVALID_ENUM; /*target*/
2779 maxLevels = ctx->Const.MaxCubeTextureLevels;
2780 }
2781 else if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
2782 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB) {
2783 if (!ctx->Extensions.ARB_texture_cube_map)
2784 return GL_INVALID_ENUM; /*target*/
2785 maxLevels = ctx->Const.MaxCubeTextureLevels;
2786 }
2787 else {
2788 return GL_INVALID_ENUM; /*target*/
2789 }
2790 }
2791 else if (dimensions == 3) {
2792 /* 3D compressed textures not allowed */
2793 return GL_INVALID_ENUM;
2794 }
2795
2796 maxTextureSize = 1 << (maxLevels - 1);
2797
2798 if (!is_compressed_format(ctx, internalFormat))
2799 return GL_INVALID_ENUM;
2800
2801 if (_mesa_base_tex_format(ctx, internalFormat) < 0)
2802 return GL_INVALID_ENUM;
2803
2804 if (border != 0)
2805 return GL_INVALID_VALUE;
2806
2807 /*
2808 * XXX We should probably use the proxy texture error check function here.
2809 */
2810 if (width < 1 || width > maxTextureSize ||
2811 (!ctx->Extensions.ARB_texture_non_power_of_two && logbase2(width) < 0))
2812 return GL_INVALID_VALUE;
2813
2814 if ((height < 1 || height > maxTextureSize ||
2815 (!ctx->Extensions.ARB_texture_non_power_of_two && logbase2(height) < 0))
2816 && dimensions > 1)
2817 return GL_INVALID_VALUE;
2818
2819 if ((depth < 1 || depth > maxTextureSize ||
2820 (!ctx->Extensions.ARB_texture_non_power_of_two && logbase2(depth) < 0))
2821 && dimensions > 2)
2822 return GL_INVALID_VALUE;
2823
2824 /* For cube map, width must equal height */
2825 if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
2826 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB && width != height)
2827 return GL_INVALID_VALUE;
2828
2829 if (level < 0 || level >= maxLevels)
2830 return GL_INVALID_VALUE;
2831
2832 expectedSize = ctx->Driver.CompressedTextureSize(ctx, width, height, depth,
2833 internalFormat);
2834 if (expectedSize != imageSize)
2835 return GL_INVALID_VALUE;
2836
2837 return GL_NO_ERROR;
2838 }
2839
2840
2841 /**
2842 * Error checking for glCompressedTexSubImage[123]D().
2843 * \warning There are some bad assumptions here about the size of compressed
2844 * texture tiles (multiple of 4) used to test the validity of the
2845 * offset and size parameters.
2846 * \return error code or GL_NO_ERROR.
2847 */
2848 static GLenum
2849 compressed_subtexture_error_check(GLcontext *ctx, GLint dimensions,
2850 GLenum target, GLint level,
2851 GLint xoffset, GLint yoffset, GLint zoffset,
2852 GLsizei width, GLsizei height, GLsizei depth,
2853 GLenum format, GLsizei imageSize)
2854 {
2855 GLint expectedSize, maxLevels = 0, maxTextureSize;
2856 (void) zoffset;
2857
2858 if (dimensions == 1) {
2859 /* 1D compressed textures not allowed */
2860 return GL_INVALID_ENUM;
2861 }
2862 else if (dimensions == 2) {
2863 if (target == GL_PROXY_TEXTURE_2D) {
2864 maxLevels = ctx->Const.MaxTextureLevels;
2865 }
2866 else if (target == GL_TEXTURE_2D) {
2867 maxLevels = ctx->Const.MaxTextureLevels;
2868 }
2869 else if (target == GL_PROXY_TEXTURE_CUBE_MAP_ARB) {
2870 if (!ctx->Extensions.ARB_texture_cube_map)
2871 return GL_INVALID_ENUM; /*target*/
2872 maxLevels = ctx->Const.MaxCubeTextureLevels;
2873 }
2874 else if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
2875 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB) {
2876 if (!ctx->Extensions.ARB_texture_cube_map)
2877 return GL_INVALID_ENUM; /*target*/
2878 maxLevels = ctx->Const.MaxCubeTextureLevels;
2879 }
2880 else {
2881 return GL_INVALID_ENUM; /*target*/
2882 }
2883 }
2884 else if (dimensions == 3) {
2885 /* 3D compressed textures not allowed */
2886 return GL_INVALID_ENUM;
2887 }
2888
2889 maxTextureSize = 1 << (maxLevels - 1);
2890
2891 if (!is_compressed_format(ctx, format))
2892 return GL_INVALID_ENUM;
2893
2894 if (width < 1 || width > maxTextureSize)
2895 return GL_INVALID_VALUE;
2896
2897 if ((height < 1 || height > maxTextureSize)
2898 && dimensions > 1)
2899 return GL_INVALID_VALUE;
2900
2901 if (level < 0 || level >= maxLevels)
2902 return GL_INVALID_VALUE;
2903
2904 /* XXX these tests are specific to the compressed format.
2905 * this code should be generalized in some way.
2906 */
2907 if ((xoffset & 3) != 0 || (yoffset & 3) != 0)
2908 return GL_INVALID_VALUE;
2909
2910 if ((width & 3) != 0 && width != 2 && width != 1)
2911 return GL_INVALID_VALUE;
2912
2913 if ((height & 3) != 0 && height != 2 && height != 1)
2914 return GL_INVALID_VALUE;
2915
2916 expectedSize = ctx->Driver.CompressedTextureSize(ctx, width, height, depth,
2917 format);
2918 if (expectedSize != imageSize)
2919 return GL_INVALID_VALUE;
2920
2921 return GL_NO_ERROR;
2922 }
2923
2924
2925
2926 void GLAPIENTRY
2927 _mesa_CompressedTexImage1DARB(GLenum target, GLint level,
2928 GLenum internalFormat, GLsizei width,
2929 GLint border, GLsizei imageSize,
2930 const GLvoid *data)
2931 {
2932 GET_CURRENT_CONTEXT(ctx);
2933 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2934
2935 if (target == GL_TEXTURE_1D) {
2936 struct gl_texture_unit *texUnit;
2937 struct gl_texture_object *texObj;
2938 struct gl_texture_image *texImage;
2939 GLenum error = compressed_texture_error_check(ctx, 1, target, level,
2940 internalFormat, width, 1, 1, border, imageSize);
2941 if (error) {
2942 _mesa_error(ctx, error, "glCompressedTexImage1D");
2943 return;
2944 }
2945
2946 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2947 texObj = _mesa_select_tex_object(ctx, texUnit, target);
2948 texImage = _mesa_get_tex_image(ctx, texUnit, target, level);
2949 if (!texImage) {
2950 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCompressedTexImage1D");
2951 return;
2952 }
2953 else if (texImage->Data && !texImage->IsClientData) {
2954 MESA_PBUFFER_FREE(texImage->Data);
2955 }
2956 texImage->Data = NULL;
2957
2958 _mesa_init_teximage_fields(ctx, target, texImage, width, 1, 1,
2959 border, internalFormat);
2960
2961 ASSERT(ctx->Driver.CompressedTexImage1D);
2962 (*ctx->Driver.CompressedTexImage1D)(ctx, target, level,
2963 internalFormat, width, border,
2964 imageSize, data,
2965 texObj, texImage);
2966
2967 /* state update */
2968 texObj->Complete = GL_FALSE;
2969 ctx->NewState |= _NEW_TEXTURE;
2970 }
2971 else if (target == GL_PROXY_TEXTURE_1D) {
2972 /* Proxy texture: check for errors and update proxy state */
2973 GLenum error = compressed_texture_error_check(ctx, 1, target, level,
2974 internalFormat, width, 1, 1, border, imageSize);
2975 if (!error) {
2976 ASSERT(ctx->Driver.TestProxyTexImage);
2977 error = !(*ctx->Driver.TestProxyTexImage)(ctx, target, level,
2978 internalFormat, GL_NONE, GL_NONE,
2979 width, 1, 1, border);
2980 }
2981 if (error) {
2982 /* if error, clear all proxy texture image parameters */
2983 struct gl_texture_image *texImage;
2984 texImage = _mesa_get_proxy_tex_image(ctx, target, level);
2985 if (texImage)
2986 clear_teximage_fields(texImage);
2987 }
2988 else {
2989 /* store the teximage parameters */
2990 struct gl_texture_unit *texUnit;
2991 struct gl_texture_image *texImage;
2992 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2993 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
2994 _mesa_init_teximage_fields(ctx, target, texImage, width, 1, 1,
2995 border, internalFormat);
2996 }
2997 }
2998 else {
2999 _mesa_error(ctx, GL_INVALID_ENUM, "glCompressedTexImage1D(target)");
3000 return;
3001 }
3002 }
3003
3004
3005 void GLAPIENTRY
3006 _mesa_CompressedTexImage2DARB(GLenum target, GLint level,
3007 GLenum internalFormat, GLsizei width,
3008 GLsizei height, GLint border, GLsizei imageSize,
3009 const GLvoid *data)
3010 {
3011 GET_CURRENT_CONTEXT(ctx);
3012 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3013
3014 if (target == GL_TEXTURE_2D ||
3015 (ctx->Extensions.ARB_texture_cube_map &&
3016 target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
3017 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB)) {
3018 struct gl_texture_unit *texUnit;
3019 struct gl_texture_object *texObj;
3020 struct gl_texture_image *texImage;
3021 GLenum error = compressed_texture_error_check(ctx, 2, target, level,
3022 internalFormat, width, height, 1, border, imageSize);
3023 if (error) {
3024 _mesa_error(ctx, error, "glCompressedTexImage2D");
3025 return;
3026 }
3027
3028 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
3029 texObj = _mesa_select_tex_object(ctx, texUnit, target);
3030 texImage = _mesa_get_tex_image(ctx, texUnit, target, level);
3031 if (!texImage) {
3032 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCompressedTexImage2D");
3033 return;
3034 }
3035 else if (texImage->Data && !texImage->IsClientData) {
3036 MESA_PBUFFER_FREE(texImage->Data);
3037 }
3038 texImage->Data = NULL;
3039
3040 _mesa_init_teximage_fields(ctx, target, texImage, width, height, 1,
3041 border, internalFormat);
3042
3043 ASSERT(ctx->Driver.CompressedTexImage2D);
3044 (*ctx->Driver.CompressedTexImage2D)(ctx, target, level,
3045 internalFormat, width, height,
3046 border, imageSize, data,
3047 texObj, texImage);
3048
3049 /* state update */
3050 texObj->Complete = GL_FALSE;
3051 ctx->NewState |= _NEW_TEXTURE;
3052 }
3053 else if (target == GL_PROXY_TEXTURE_2D ||
3054 (target == GL_PROXY_TEXTURE_CUBE_MAP_ARB &&
3055 ctx->Extensions.ARB_texture_cube_map)) {
3056 /* Proxy texture: check for errors and update proxy state */
3057 GLenum error = compressed_texture_error_check(ctx, 2, target, level,
3058 internalFormat, width, height, 1, border, imageSize);
3059 if (!error) {
3060 ASSERT(ctx->Driver.TestProxyTexImage);
3061 error = !(*ctx->Driver.TestProxyTexImage)(ctx, target, level,
3062 internalFormat, GL_NONE, GL_NONE,
3063 width, height, 1, border);
3064 }
3065 if (error) {
3066 /* if error, clear all proxy texture image parameters */
3067 struct gl_texture_image *texImage;
3068 texImage = _mesa_get_proxy_tex_image(ctx, target, level);
3069 if (texImage)
3070 clear_teximage_fields(texImage);
3071 }
3072 else {
3073 /* store the teximage parameters */
3074 struct gl_texture_unit *texUnit;
3075 struct gl_texture_image *texImage;
3076 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
3077 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
3078 _mesa_init_teximage_fields(ctx, target, texImage, width, height, 1,
3079 border, internalFormat);
3080 }
3081 }
3082 else {
3083 _mesa_error(ctx, GL_INVALID_ENUM, "glCompressedTexImage2D(target)");
3084 return;
3085 }
3086 }
3087
3088
3089 void GLAPIENTRY
3090 _mesa_CompressedTexImage3DARB(GLenum target, GLint level,
3091 GLenum internalFormat, GLsizei width,
3092 GLsizei height, GLsizei depth, GLint border,
3093 GLsizei imageSize, const GLvoid *data)
3094 {
3095 GET_CURRENT_CONTEXT(ctx);
3096 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3097
3098 if (target == GL_TEXTURE_3D) {
3099 struct gl_texture_unit *texUnit;
3100 struct gl_texture_object *texObj;
3101 struct gl_texture_image *texImage;
3102 GLenum error = compressed_texture_error_check(ctx, 3, target, level,
3103 internalFormat, width, height, depth, border, imageSize);
3104 if (error) {
3105 _mesa_error(ctx, error, "glCompressedTexImage3D");
3106 return;
3107 }
3108
3109 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
3110 texObj = _mesa_select_tex_object(ctx, texUnit, target);
3111 texImage = _mesa_get_tex_image(ctx, texUnit, target, level);
3112 if (!texImage) {
3113 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCompressedTexImage3D");
3114 return;
3115 }
3116 else if (texImage->Data && !texImage->IsClientData) {
3117 MESA_PBUFFER_FREE(texImage->Data);
3118 }
3119 texImage->Data = NULL;
3120
3121 _mesa_init_teximage_fields(ctx, target, texImage, width, height, depth,
3122 border, internalFormat);
3123
3124 ASSERT(ctx->Driver.CompressedTexImage3D);
3125 (*ctx->Driver.CompressedTexImage3D)(ctx, target, level,
3126 internalFormat,
3127 width, height, depth,
3128 border, imageSize, data,
3129 texObj, texImage);
3130
3131 /* state update */
3132 texObj->Complete = GL_FALSE;
3133 ctx->NewState |= _NEW_TEXTURE;
3134 }
3135 else if (target == GL_PROXY_TEXTURE_3D) {
3136 /* Proxy texture: check for errors and update proxy state */
3137 GLenum error = compressed_texture_error_check(ctx, 3, target, level,
3138 internalFormat, width, height, depth, border, imageSize);
3139 if (!error) {
3140 ASSERT(ctx->Driver.TestProxyTexImage);
3141 error = !(*ctx->Driver.TestProxyTexImage)(ctx, target, level,
3142 internalFormat, GL_NONE, GL_NONE,
3143 width, height, depth, border);
3144 }
3145 if (error) {
3146 /* if error, clear all proxy texture image parameters */
3147 struct gl_texture_image *texImage;
3148 texImage = _mesa_get_proxy_tex_image(ctx, target, level);
3149 if (texImage)
3150 clear_teximage_fields(texImage);
3151 }
3152 else {
3153 /* store the teximage parameters */
3154 struct gl_texture_unit *texUnit;
3155 struct gl_texture_image *texImage;
3156 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
3157 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
3158 _mesa_init_teximage_fields(ctx, target, texImage, width, height,
3159 depth, border, internalFormat);
3160 }
3161 }
3162 else {
3163 _mesa_error(ctx, GL_INVALID_ENUM, "glCompressedTexImage3D(target)");
3164 return;
3165 }
3166 }
3167
3168
3169 void GLAPIENTRY
3170 _mesa_CompressedTexSubImage1DARB(GLenum target, GLint level, GLint xoffset,
3171 GLsizei width, GLenum format,
3172 GLsizei imageSize, const GLvoid *data)
3173 {
3174 struct gl_texture_unit *texUnit;
3175 struct gl_texture_object *texObj;
3176 struct gl_texture_image *texImage;
3177 GLenum error;
3178 GET_CURRENT_CONTEXT(ctx);
3179 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3180
3181 error = compressed_subtexture_error_check(ctx, 1, target, level,
3182 xoffset, 0, 0, width, 1, 1, format, imageSize);
3183 if (error) {
3184 _mesa_error(ctx, error, "glCompressedTexSubImage1D");
3185 return;
3186 }
3187
3188 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
3189 texObj = _mesa_select_tex_object(ctx, texUnit, target);
3190 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
3191 assert(texImage);
3192
3193 if ((GLint) format != texImage->IntFormat) {
3194 _mesa_error(ctx, GL_INVALID_OPERATION,
3195 "glCompressedTexSubImage1D(format)");
3196 return;
3197 }
3198
3199 if ((width == 1 || width == 2) && (GLuint) width != texImage->Width) {
3200 _mesa_error(ctx, GL_INVALID_VALUE, "glCompressedTexSubImage1D(width)");
3201 return;
3202 }
3203
3204 if (width == 0)
3205 return; /* no-op, not an error */
3206
3207 if (ctx->Driver.CompressedTexSubImage1D) {
3208 (*ctx->Driver.CompressedTexSubImage1D)(ctx, target, level,
3209 xoffset, width,
3210 format, imageSize, data,
3211 texObj, texImage);
3212 }
3213 ctx->NewState |= _NEW_TEXTURE;
3214 }
3215
3216
3217 void GLAPIENTRY
3218 _mesa_CompressedTexSubImage2DARB(GLenum target, GLint level, GLint xoffset,
3219 GLint yoffset, GLsizei width, GLsizei height,
3220 GLenum format, GLsizei imageSize,
3221 const GLvoid *data)
3222 {
3223 struct gl_texture_unit *texUnit;
3224 struct gl_texture_object *texObj;
3225 struct gl_texture_image *texImage;
3226 GLenum error;
3227 GET_CURRENT_CONTEXT(ctx);
3228 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3229
3230 error = compressed_subtexture_error_check(ctx, 2, target, level,
3231 xoffset, yoffset, 0, width, height, 1, format, imageSize);
3232 if (error) {
3233 /* XXX proxy target? */
3234 _mesa_error(ctx, error, "glCompressedTexSubImage2D");
3235 return;
3236 }
3237
3238 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
3239 texObj = _mesa_select_tex_object(ctx, texUnit, target);
3240 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
3241 assert(texImage);
3242
3243 if ((GLint) format != texImage->IntFormat) {
3244 _mesa_error(ctx, GL_INVALID_OPERATION,
3245 "glCompressedTexSubImage2D(format)");
3246 return;
3247 }
3248
3249 if (((width == 1 || width == 2) && (GLuint) width != texImage->Width) ||
3250 ((height == 1 || height == 2) && (GLuint) height != texImage->Height)) {
3251 _mesa_error(ctx, GL_INVALID_VALUE, "glCompressedTexSubImage2D(size)");
3252 return;
3253 }
3254
3255 if (width == 0 || height == 0)
3256 return; /* no-op, not an error */
3257
3258 if (ctx->Driver.CompressedTexSubImage2D) {
3259 (*ctx->Driver.CompressedTexSubImage2D)(ctx, target, level,
3260 xoffset, yoffset, width, height,
3261 format, imageSize, data,
3262 texObj, texImage);
3263 }
3264 ctx->NewState |= _NEW_TEXTURE;
3265 }
3266
3267
3268 void GLAPIENTRY
3269 _mesa_CompressedTexSubImage3DARB(GLenum target, GLint level, GLint xoffset,
3270 GLint yoffset, GLint zoffset, GLsizei width,
3271 GLsizei height, GLsizei depth, GLenum format,
3272 GLsizei imageSize, const GLvoid *data)
3273 {
3274 struct gl_texture_unit *texUnit;
3275 struct gl_texture_object *texObj;
3276 struct gl_texture_image *texImage;
3277 GLenum error;
3278 GET_CURRENT_CONTEXT(ctx);
3279 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3280
3281 error = compressed_subtexture_error_check(ctx, 3, target, level,
3282 xoffset, yoffset, zoffset, width, height, depth, format, imageSize);
3283 if (error) {
3284 _mesa_error(ctx, error, "glCompressedTexSubImage2D");
3285 return;
3286 }
3287
3288 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
3289 texObj = _mesa_select_tex_object(ctx, texUnit, target);
3290 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
3291 assert(texImage);
3292
3293 if ((GLint) format != texImage->IntFormat) {
3294 _mesa_error(ctx, GL_INVALID_OPERATION,
3295 "glCompressedTexSubImage3D(format)");
3296 return;
3297 }
3298
3299 if (((width == 1 || width == 2) && (GLuint) width != texImage->Width) ||
3300 ((height == 1 || height == 2) && (GLuint) height != texImage->Height) ||
3301 ((depth == 1 || depth == 2) && (GLuint) depth != texImage->Depth)) {
3302 _mesa_error(ctx, GL_INVALID_VALUE, "glCompressedTexSubImage3D(size)");
3303 return;
3304 }
3305
3306 if (width == 0 || height == 0 || depth == 0)
3307 return; /* no-op, not an error */
3308
3309 if (ctx->Driver.CompressedTexSubImage3D) {
3310 (*ctx->Driver.CompressedTexSubImage3D)(ctx, target, level,
3311 xoffset, yoffset, zoffset,
3312 width, height, depth,
3313 format, imageSize, data,
3314 texObj, texImage);
3315 }
3316 ctx->NewState |= _NEW_TEXTURE;
3317 }
3318
3319
3320 void GLAPIENTRY
3321 _mesa_GetCompressedTexImageARB(GLenum target, GLint level, GLvoid *img)
3322 {
3323 const struct gl_texture_unit *texUnit;
3324 const struct gl_texture_object *texObj;
3325 struct gl_texture_image *texImage;
3326 GLint maxLevels;
3327 GET_CURRENT_CONTEXT(ctx);
3328 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3329
3330 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
3331 texObj = _mesa_select_tex_object(ctx, texUnit, target);
3332 if (!texObj) {
3333 _mesa_error(ctx, GL_INVALID_ENUM, "glGetCompressedTexImageARB");
3334 return;
3335 }
3336
3337 maxLevels = _mesa_max_texture_levels(ctx, target);
3338 ASSERT(maxLevels > 0); /* 0 indicates bad target, caught above */
3339
3340 if (level < 0 || level >= maxLevels) {
3341 _mesa_error(ctx, GL_INVALID_VALUE, "glGetCompressedTexImageARB(level)");
3342 return;
3343 }
3344
3345 if (is_proxy_target(target)) {
3346 _mesa_error(ctx, GL_INVALID_ENUM, "glGetCompressedTexImageARB(target)");
3347 return;
3348 }
3349
3350 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
3351 if (!texImage) {
3352 /* probably invalid mipmap level */
3353 _mesa_error(ctx, GL_INVALID_VALUE, "glGetCompressedTexImageARB(level)");
3354 return;
3355 }
3356
3357 if (!texImage->IsCompressed) {
3358 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetCompressedTexImageARB");
3359 return;
3360 }
3361
3362 if (!img)
3363 return;
3364
3365 /* just memcpy, no pixelstore or pixel transfer */
3366 MEMCPY(img, texImage->Data, texImage->CompressedSize);
3367 }