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