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