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