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