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