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