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