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