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