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