Merge branch 'texformat-rework'
[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 "texfetch.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 }
907
908
909 /**
910 * Initialize basic fields of the gl_texture_image struct.
911 *
912 * \param ctx GL context.
913 * \param target texture target (GL_TEXTURE_1D, GL_TEXTURE_RECTANGLE, etc).
914 * \param img texture image structure to be initialized.
915 * \param width image width.
916 * \param height image height.
917 * \param depth image depth.
918 * \param border image border.
919 * \param internalFormat internal format.
920 *
921 * Fills in the fields of \p img with the given information.
922 * Note: width, height and depth include the border.
923 */
924 void
925 _mesa_init_teximage_fields(GLcontext *ctx, GLenum target,
926 struct gl_texture_image *img,
927 GLsizei width, GLsizei height, GLsizei depth,
928 GLint border, GLenum internalFormat)
929 {
930 GLint i;
931
932 ASSERT(img);
933 ASSERT(width >= 0);
934 ASSERT(height >= 0);
935 ASSERT(depth >= 0);
936
937 img->_BaseFormat = _mesa_base_tex_format( ctx, internalFormat );
938 ASSERT(img->_BaseFormat > 0);
939 img->InternalFormat = internalFormat;
940 img->Border = border;
941 img->Width = width;
942 img->Height = height;
943 img->Depth = depth;
944
945 img->Width2 = width - 2 * border; /* == 1 << img->WidthLog2; */
946 img->WidthLog2 = logbase2(img->Width2);
947
948 if (height == 1) { /* 1-D texture */
949 img->Height2 = 1;
950 img->HeightLog2 = 0;
951 }
952 else {
953 img->Height2 = height - 2 * border; /* == 1 << img->HeightLog2; */
954 img->HeightLog2 = logbase2(img->Height2);
955 }
956
957 if (depth == 1) { /* 2-D texture */
958 img->Depth2 = 1;
959 img->DepthLog2 = 0;
960 }
961 else {
962 img->Depth2 = depth - 2 * border; /* == 1 << img->DepthLog2; */
963 img->DepthLog2 = logbase2(img->Depth2);
964 }
965
966 img->MaxLog2 = MAX2(img->WidthLog2, img->HeightLog2);
967
968 if ((width == 1 || _mesa_is_pow_two(img->Width2)) &&
969 (height == 1 || _mesa_is_pow_two(img->Height2)) &&
970 (depth == 1 || _mesa_is_pow_two(img->Depth2)))
971 img->_IsPowerOfTwo = GL_TRUE;
972 else
973 img->_IsPowerOfTwo = GL_FALSE;
974
975 /* RowStride and ImageOffsets[] describe how to address texels in 'Data' */
976 img->RowStride = width;
977 /* Allocate the ImageOffsets array and initialize to typical values.
978 * We allocate the array for 1D/2D textures too in order to avoid special-
979 * case code in the texstore routines.
980 */
981 if (img->ImageOffsets)
982 _mesa_free(img->ImageOffsets);
983 img->ImageOffsets = (GLuint *) _mesa_malloc(depth * sizeof(GLuint));
984 for (i = 0; i < depth; i++) {
985 img->ImageOffsets[i] = i * width * height;
986 }
987
988 /* Compute Width/Height/DepthScale for mipmap lod computation */
989 if (target == GL_TEXTURE_RECTANGLE_NV) {
990 /* scale = 1.0 since texture coords directly map to texels */
991 img->WidthScale = 1.0;
992 img->HeightScale = 1.0;
993 img->DepthScale = 1.0;
994 }
995 else {
996 img->WidthScale = (GLfloat) img->Width;
997 img->HeightScale = (GLfloat) img->Height;
998 img->DepthScale = (GLfloat) img->Depth;
999 }
1000
1001 img->FetchTexelc = NULL;
1002 img->FetchTexelf = NULL;
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 * Return pointer to texture object for given target on current texture unit.
1025 */
1026 static struct gl_texture_object *
1027 get_current_tex_object(GLcontext *ctx, GLenum target)
1028 {
1029 struct gl_texture_unit *texUnit = _mesa_get_current_tex_unit(ctx);
1030 return _mesa_select_tex_object(ctx, texUnit, target);
1031 }
1032
1033
1034
1035 /**
1036 * This is the fallback for Driver.TestProxyTexImage(). Test the texture
1037 * level, width, height and depth against the ctx->Const limits for textures.
1038 *
1039 * A hardware driver might override this function if, for example, the
1040 * max 3D texture size is 512x512x64 (i.e. not a cube).
1041 *
1042 * Note that width, height, depth == 0 is not an error. However, a
1043 * texture with zero width/height/depth will be considered "incomplete"
1044 * and texturing will effectively be disabled.
1045 *
1046 * \param target one of GL_PROXY_TEXTURE_1D, GL_PROXY_TEXTURE_2D,
1047 * GL_PROXY_TEXTURE_3D, GL_PROXY_TEXTURE_RECTANGLE_NV,
1048 * GL_PROXY_TEXTURE_CUBE_MAP_ARB.
1049 * \param level as passed to glTexImage
1050 * \param internalFormat as passed to glTexImage
1051 * \param format as passed to glTexImage
1052 * \param type as passed to glTexImage
1053 * \param width as passed to glTexImage
1054 * \param height as passed to glTexImage
1055 * \param depth as passed to glTexImage
1056 * \param border as passed to glTexImage
1057 * \return GL_TRUE if the image is acceptable, GL_FALSE if not acceptable.
1058 */
1059 GLboolean
1060 _mesa_test_proxy_teximage(GLcontext *ctx, GLenum target, GLint level,
1061 GLint internalFormat, GLenum format, GLenum type,
1062 GLint width, GLint height, GLint depth, GLint border)
1063 {
1064 GLint maxSize;
1065
1066 (void) internalFormat;
1067 (void) format;
1068 (void) type;
1069
1070 switch (target) {
1071 case GL_PROXY_TEXTURE_1D:
1072 maxSize = 1 << (ctx->Const.MaxTextureLevels - 1);
1073 if (width < 2 * border || width > 2 + maxSize ||
1074 (!ctx->Extensions.ARB_texture_non_power_of_two &&
1075 width >0 && !_mesa_is_pow_two(width - 2 * border)) ||
1076 level >= ctx->Const.MaxTextureLevels) {
1077 /* bad width or level */
1078 return GL_FALSE;
1079 }
1080 return GL_TRUE;
1081 case GL_PROXY_TEXTURE_2D:
1082 maxSize = 1 << (ctx->Const.MaxTextureLevels - 1);
1083 if (width < 2 * border || width > 2 + maxSize ||
1084 (!ctx->Extensions.ARB_texture_non_power_of_two &&
1085 width > 0 && !_mesa_is_pow_two(width - 2 * border)) ||
1086 height < 2 * border || height > 2 + maxSize ||
1087 (!ctx->Extensions.ARB_texture_non_power_of_two &&
1088 height > 0 && !_mesa_is_pow_two(height - 2 * border)) ||
1089 level >= ctx->Const.MaxTextureLevels) {
1090 /* bad width or height or level */
1091 return GL_FALSE;
1092 }
1093 return GL_TRUE;
1094 case GL_PROXY_TEXTURE_3D:
1095 maxSize = 1 << (ctx->Const.Max3DTextureLevels - 1);
1096 if (width < 2 * border || width > 2 + maxSize ||
1097 (!ctx->Extensions.ARB_texture_non_power_of_two &&
1098 width > 0 && !_mesa_is_pow_two(width - 2 * border)) ||
1099 height < 2 * border || height > 2 + maxSize ||
1100 (!ctx->Extensions.ARB_texture_non_power_of_two &&
1101 height > 0 && !_mesa_is_pow_two(height - 2 * border)) ||
1102 depth < 2 * border || depth > 2 + maxSize ||
1103 (!ctx->Extensions.ARB_texture_non_power_of_two &&
1104 depth > 0 && !_mesa_is_pow_two(depth - 2 * border)) ||
1105 level >= ctx->Const.Max3DTextureLevels) {
1106 /* bad width or height or depth or level */
1107 return GL_FALSE;
1108 }
1109 return GL_TRUE;
1110 case GL_PROXY_TEXTURE_RECTANGLE_NV:
1111 if (width < 0 || width > ctx->Const.MaxTextureRectSize ||
1112 height < 0 || height > ctx->Const.MaxTextureRectSize ||
1113 level != 0) {
1114 /* bad width or height or level */
1115 return GL_FALSE;
1116 }
1117 return GL_TRUE;
1118 case GL_PROXY_TEXTURE_CUBE_MAP_ARB:
1119 maxSize = 1 << (ctx->Const.MaxCubeTextureLevels - 1);
1120 if (width < 2 * border || width > 2 + maxSize ||
1121 (!ctx->Extensions.ARB_texture_non_power_of_two &&
1122 width > 0 && !_mesa_is_pow_two(width - 2 * border)) ||
1123 height < 2 * border || height > 2 + maxSize ||
1124 (!ctx->Extensions.ARB_texture_non_power_of_two &&
1125 height > 0 && !_mesa_is_pow_two(height - 2 * border)) ||
1126 level >= ctx->Const.MaxCubeTextureLevels) {
1127 /* bad width or height */
1128 return GL_FALSE;
1129 }
1130 return GL_TRUE;
1131 case GL_PROXY_TEXTURE_1D_ARRAY_EXT:
1132 maxSize = 1 << (ctx->Const.MaxTextureLevels - 1);
1133 if (width < 2 * border || width > 2 + maxSize ||
1134 (!ctx->Extensions.ARB_texture_non_power_of_two &&
1135 width > 0 && !_mesa_is_pow_two(width - 2 * border)) ||
1136 level >= ctx->Const.MaxTextureLevels) {
1137 /* bad width or level */
1138 return GL_FALSE;
1139 }
1140
1141 if (height < 1 || height > ctx->Const.MaxArrayTextureLayers) {
1142 return GL_FALSE;
1143 }
1144 return GL_TRUE;
1145 case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
1146 maxSize = 1 << (ctx->Const.MaxTextureLevels - 1);
1147 if (width < 2 * border || width > 2 + maxSize ||
1148 (!ctx->Extensions.ARB_texture_non_power_of_two &&
1149 width > 0 && !_mesa_is_pow_two(width - 2 * border)) ||
1150 height < 2 * border || height > 2 + maxSize ||
1151 (!ctx->Extensions.ARB_texture_non_power_of_two &&
1152 height > 0 && !_mesa_is_pow_two(height - 2 * border)) ||
1153 level >= ctx->Const.MaxTextureLevels) {
1154 /* bad width or height or level */
1155 return GL_FALSE;
1156 }
1157 if (depth < 1 || depth > ctx->Const.MaxArrayTextureLayers) {
1158 return GL_FALSE;
1159 }
1160 return GL_TRUE;
1161 default:
1162 _mesa_problem(ctx, "Invalid target in _mesa_test_proxy_teximage");
1163 return GL_FALSE;
1164 }
1165 }
1166
1167
1168 /**
1169 * Helper function to determine whether a target supports compressed textures
1170 */
1171 static GLboolean
1172 target_can_be_compressed(GLcontext *ctx, GLenum target)
1173 {
1174 return (((target == GL_TEXTURE_2D || target == GL_PROXY_TEXTURE_2D))
1175 || ((ctx->Extensions.ARB_texture_cube_map &&
1176 (target == GL_PROXY_TEXTURE_CUBE_MAP ||
1177 (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X &&
1178 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z))))
1179 || ((ctx->Extensions.MESA_texture_array &&
1180 ((target == GL_PROXY_TEXTURE_2D_ARRAY_EXT) ||
1181 (target == GL_TEXTURE_2D_ARRAY_EXT)))));
1182 }
1183
1184
1185 /**
1186 * Test the glTexImage[123]D() parameters for errors.
1187 *
1188 * \param ctx GL context.
1189 * \param target texture target given by the user.
1190 * \param level image level given by the user.
1191 * \param internalFormat internal format given by the user.
1192 * \param format pixel data format given by the user.
1193 * \param type pixel data type given by the user.
1194 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1195 * \param width image width given by the user.
1196 * \param height image height given by the user.
1197 * \param depth image depth given by the user.
1198 * \param border image border given by the user.
1199 *
1200 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
1201 *
1202 * Verifies each of the parameters against the constants specified in
1203 * __GLcontextRec::Const and the supported extensions, and according to the
1204 * OpenGL specification.
1205 */
1206 static GLboolean
1207 texture_error_check( GLcontext *ctx, GLenum target,
1208 GLint level, GLint internalFormat,
1209 GLenum format, GLenum type,
1210 GLuint dimensions,
1211 GLint width, GLint height,
1212 GLint depth, GLint border )
1213 {
1214 const GLboolean isProxy = _mesa_is_proxy_texture(target);
1215 GLboolean sizeOK = GL_TRUE;
1216 GLboolean colorFormat, indexFormat;
1217 GLenum proxy_target;
1218
1219 /* Basic level check (more checking in ctx->Driver.TestProxyTexImage) */
1220 if (level < 0 || level >= MAX_TEXTURE_LEVELS) {
1221 if (!isProxy) {
1222 _mesa_error(ctx, GL_INVALID_VALUE,
1223 "glTexImage%dD(level=%d)", dimensions, level);
1224 }
1225 return GL_TRUE;
1226 }
1227
1228 /* Check border */
1229 if (border < 0 || border > 1 ||
1230 ((target == GL_TEXTURE_RECTANGLE_NV ||
1231 target == GL_PROXY_TEXTURE_RECTANGLE_NV) && border != 0)) {
1232 if (!isProxy) {
1233 _mesa_error(ctx, GL_INVALID_VALUE,
1234 "glTexImage%dD(border=%d)", dimensions, border);
1235 }
1236 return GL_TRUE;
1237 }
1238
1239 if (width < 0 || height < 0 || depth < 0) {
1240 if (!isProxy) {
1241 _mesa_error(ctx, GL_INVALID_VALUE,
1242 "glTexImage%dD(width, height or depth < 0)", dimensions);
1243 }
1244 return GL_TRUE;
1245 }
1246
1247 /* Check target and call ctx->Driver.TestProxyTexImage() to check the
1248 * level, width, height and depth.
1249 */
1250 if (dimensions == 1) {
1251 if (target == GL_PROXY_TEXTURE_1D || target == GL_TEXTURE_1D) {
1252 proxy_target = GL_PROXY_TEXTURE_1D;
1253 height = 1;
1254 depth = 1;
1255 }
1256 else {
1257 _mesa_error( ctx, GL_INVALID_ENUM, "glTexImage1D(target)" );
1258 return GL_TRUE;
1259 }
1260 }
1261 else if (dimensions == 2) {
1262 depth = 1;
1263 if (target == GL_PROXY_TEXTURE_2D || target == GL_TEXTURE_2D) {
1264 proxy_target = GL_PROXY_TEXTURE_2D;
1265 }
1266 else if (target == GL_PROXY_TEXTURE_CUBE_MAP_ARB ||
1267 (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
1268 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB)) {
1269 if (!ctx->Extensions.ARB_texture_cube_map) {
1270 _mesa_error(ctx, GL_INVALID_ENUM, "glTexImage2D(target)");
1271 return GL_TRUE;
1272 }
1273 proxy_target = GL_PROXY_TEXTURE_CUBE_MAP_ARB;
1274 sizeOK = (width == height);
1275 }
1276 else if (target == GL_PROXY_TEXTURE_RECTANGLE_NV ||
1277 target == GL_TEXTURE_RECTANGLE_NV) {
1278 if (!ctx->Extensions.NV_texture_rectangle) {
1279 _mesa_error(ctx, GL_INVALID_ENUM, "glTexImage2D(target)");
1280 return GL_TRUE;
1281 }
1282 proxy_target = GL_PROXY_TEXTURE_RECTANGLE_NV;
1283 }
1284 else if (target == GL_PROXY_TEXTURE_1D_ARRAY_EXT ||
1285 target == GL_TEXTURE_1D_ARRAY_EXT) {
1286 proxy_target = GL_PROXY_TEXTURE_1D_ARRAY_EXT;
1287 }
1288 else {
1289 _mesa_error(ctx, GL_INVALID_ENUM, "glTexImage2D(target)");
1290 return GL_TRUE;
1291 }
1292 }
1293 else if (dimensions == 3) {
1294 if (target == GL_PROXY_TEXTURE_3D || target == GL_TEXTURE_3D) {
1295 proxy_target = GL_PROXY_TEXTURE_3D;
1296 }
1297 else if (target == GL_PROXY_TEXTURE_2D_ARRAY_EXT ||
1298 target == GL_TEXTURE_2D_ARRAY_EXT) {
1299 proxy_target = GL_PROXY_TEXTURE_2D_ARRAY_EXT;
1300 }
1301 else {
1302 _mesa_error( ctx, GL_INVALID_ENUM, "glTexImage3D(target)" );
1303 return GL_TRUE;
1304 }
1305 }
1306 else {
1307 _mesa_problem( ctx, "bad dims in texture_error_check" );
1308 return GL_TRUE;
1309 }
1310
1311 sizeOK = sizeOK && ctx->Driver.TestProxyTexImage(ctx, proxy_target, level,
1312 internalFormat, format,
1313 type, width, height,
1314 depth, border);
1315 if (!sizeOK) {
1316 if (!isProxy) {
1317 _mesa_error(ctx, GL_INVALID_VALUE,
1318 "glTexImage%dD(level=%d, width=%d, height=%d, depth=%d)",
1319 dimensions, level, width, height, depth);
1320 }
1321 return GL_TRUE;
1322 }
1323
1324 /* Check internalFormat */
1325 if (_mesa_base_tex_format(ctx, internalFormat) < 0) {
1326 if (!isProxy) {
1327 _mesa_error(ctx, GL_INVALID_VALUE,
1328 "glTexImage%dD(internalFormat=0x%x)",
1329 dimensions, internalFormat);
1330 }
1331 return GL_TRUE;
1332 }
1333
1334 /* Check incoming image format and type */
1335 if (!_mesa_is_legal_format_and_type(ctx, format, type)) {
1336 /* Yes, generate GL_INVALID_OPERATION, not GL_INVALID_ENUM, if there
1337 * is a type/format mismatch. See 1.2 spec page 94, sec 3.6.4.
1338 */
1339 if (!isProxy) {
1340 _mesa_error(ctx, GL_INVALID_OPERATION,
1341 "glTexImage%dD(incompatible format 0x%x, type 0x%x)",
1342 dimensions, format, type);
1343 }
1344 return GL_TRUE;
1345 }
1346
1347 /* make sure internal format and format basically agree */
1348 colorFormat = _mesa_is_color_format(format);
1349 indexFormat = _mesa_is_index_format(format);
1350 if ((_mesa_is_color_format(internalFormat) && !colorFormat && !indexFormat) ||
1351 (_mesa_is_index_format(internalFormat) && !indexFormat) ||
1352 (_mesa_is_depth_format(internalFormat) != _mesa_is_depth_format(format)) ||
1353 (_mesa_is_ycbcr_format(internalFormat) != _mesa_is_ycbcr_format(format)) ||
1354 (_mesa_is_depthstencil_format(internalFormat) != _mesa_is_depthstencil_format(format)) ||
1355 (_mesa_is_dudv_format(internalFormat) != _mesa_is_dudv_format(format))) {
1356 if (!isProxy)
1357 _mesa_error(ctx, GL_INVALID_OPERATION,
1358 "glTexImage%dD(incompatible internalFormat 0x%x, format 0x%x)",
1359 dimensions, internalFormat, format);
1360 return GL_TRUE;
1361 }
1362
1363 /* additional checks for ycbcr textures */
1364 if (internalFormat == GL_YCBCR_MESA) {
1365 ASSERT(ctx->Extensions.MESA_ycbcr_texture);
1366 if (type != GL_UNSIGNED_SHORT_8_8_MESA &&
1367 type != GL_UNSIGNED_SHORT_8_8_REV_MESA) {
1368 char message[100];
1369 _mesa_sprintf(message,
1370 "glTexImage%d(format/type YCBCR mismatch", dimensions);
1371 _mesa_error(ctx, GL_INVALID_ENUM, message);
1372 return GL_TRUE; /* error */
1373 }
1374 if (target != GL_TEXTURE_2D &&
1375 target != GL_PROXY_TEXTURE_2D &&
1376 target != GL_TEXTURE_RECTANGLE_NV &&
1377 target != GL_PROXY_TEXTURE_RECTANGLE_NV) {
1378 if (!isProxy)
1379 _mesa_error(ctx, GL_INVALID_ENUM, "glTexImage(target)");
1380 return GL_TRUE;
1381 }
1382 if (border != 0) {
1383 if (!isProxy) {
1384 char message[100];
1385 _mesa_sprintf(message,
1386 "glTexImage%d(format=GL_YCBCR_MESA and border=%d)",
1387 dimensions, border);
1388 _mesa_error(ctx, GL_INVALID_VALUE, message);
1389 }
1390 return GL_TRUE;
1391 }
1392 }
1393
1394 /* additional checks for depth textures */
1395 if (_mesa_base_tex_format(ctx, internalFormat) == GL_DEPTH_COMPONENT) {
1396 /* Only 1D, 2D and rectangular textures supported, not 3D or cubes */
1397 if (target != GL_TEXTURE_1D &&
1398 target != GL_PROXY_TEXTURE_1D &&
1399 target != GL_TEXTURE_2D &&
1400 target != GL_PROXY_TEXTURE_2D &&
1401 target != GL_TEXTURE_RECTANGLE_ARB &&
1402 target != GL_PROXY_TEXTURE_RECTANGLE_ARB) {
1403 if (!isProxy)
1404 _mesa_error(ctx, GL_INVALID_ENUM,
1405 "glTexImage(target/internalFormat)");
1406 return GL_TRUE;
1407 }
1408 }
1409
1410 /* additional checks for compressed textures */
1411 if (is_compressed_format(ctx, internalFormat)) {
1412 if (!target_can_be_compressed(ctx, target) && !isProxy) {
1413 _mesa_error(ctx, GL_INVALID_ENUM,
1414 "glTexImage%d(target)", dimensions);
1415 return GL_TRUE;
1416 }
1417 if (border != 0) {
1418 if (!isProxy) {
1419 _mesa_error(ctx, GL_INVALID_OPERATION,
1420 "glTexImage%D(border!=0)", dimensions);
1421 }
1422 return GL_TRUE;
1423 }
1424 }
1425
1426 /* if we get here, the parameters are OK */
1427 return GL_FALSE;
1428 }
1429
1430
1431 /**
1432 * Test glTexSubImage[123]D() parameters for errors.
1433 *
1434 * \param ctx GL context.
1435 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1436 * \param target texture target given by the user.
1437 * \param level image level given by the user.
1438 * \param xoffset sub-image x offset given by the user.
1439 * \param yoffset sub-image y offset given by the user.
1440 * \param zoffset sub-image z offset given by the user.
1441 * \param format pixel data format given by the user.
1442 * \param type pixel data type given by the user.
1443 * \param width image width given by the user.
1444 * \param height image height given by the user.
1445 * \param depth image depth given by the user.
1446 *
1447 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
1448 *
1449 * Verifies each of the parameters against the constants specified in
1450 * __GLcontextRec::Const and the supported extensions, and according to the
1451 * OpenGL specification.
1452 */
1453 static GLboolean
1454 subtexture_error_check( GLcontext *ctx, GLuint dimensions,
1455 GLenum target, GLint level,
1456 GLint xoffset, GLint yoffset, GLint zoffset,
1457 GLint width, GLint height, GLint depth,
1458 GLenum format, GLenum type )
1459 {
1460 /* Check target */
1461 if (dimensions == 1) {
1462 if (target != GL_TEXTURE_1D) {
1463 _mesa_error( ctx, GL_INVALID_ENUM, "glTexSubImage1D(target)" );
1464 return GL_TRUE;
1465 }
1466 }
1467 else if (dimensions == 2) {
1468 if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
1469 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB) {
1470 if (!ctx->Extensions.ARB_texture_cube_map) {
1471 _mesa_error( ctx, GL_INVALID_ENUM, "glTexSubImage2D(target)" );
1472 return GL_TRUE;
1473 }
1474 }
1475 else if (target == GL_TEXTURE_RECTANGLE_NV) {
1476 if (!ctx->Extensions.NV_texture_rectangle) {
1477 _mesa_error( ctx, GL_INVALID_ENUM, "glTexSubImage2D(target)" );
1478 return GL_TRUE;
1479 }
1480 }
1481 else if (target == GL_TEXTURE_1D_ARRAY_EXT) {
1482 if (!ctx->Extensions.MESA_texture_array) {
1483 _mesa_error( ctx, GL_INVALID_ENUM, "glTexSubImage2D(target)" );
1484 return GL_TRUE;
1485 }
1486 }
1487 else if (target != GL_TEXTURE_2D) {
1488 _mesa_error( ctx, GL_INVALID_ENUM, "glTexSubImage2D(target)" );
1489 return GL_TRUE;
1490 }
1491 }
1492 else if (dimensions == 3) {
1493 if (target == GL_TEXTURE_2D_ARRAY_EXT) {
1494 if (!ctx->Extensions.MESA_texture_array) {
1495 _mesa_error( ctx, GL_INVALID_ENUM, "glTexSubImage3D(target)" );
1496 return GL_TRUE;
1497 }
1498 }
1499 else if (target != GL_TEXTURE_3D) {
1500 _mesa_error( ctx, GL_INVALID_ENUM, "glTexSubImage3D(target)" );
1501 return GL_TRUE;
1502 }
1503 }
1504 else {
1505 _mesa_problem( ctx, "invalid dims in texture_error_check" );
1506 return GL_TRUE;
1507 }
1508
1509 /* Basic level check */
1510 if (level < 0 || level >= MAX_TEXTURE_LEVELS) {
1511 _mesa_error(ctx, GL_INVALID_ENUM, "glTexSubImage2D(level=%d)", level);
1512 return GL_TRUE;
1513 }
1514
1515 if (width < 0) {
1516 _mesa_error(ctx, GL_INVALID_VALUE,
1517 "glTexSubImage%dD(width=%d)", dimensions, width);
1518 return GL_TRUE;
1519 }
1520 if (height < 0 && dimensions > 1) {
1521 _mesa_error(ctx, GL_INVALID_VALUE,
1522 "glTexSubImage%dD(height=%d)", dimensions, height);
1523 return GL_TRUE;
1524 }
1525 if (depth < 0 && dimensions > 2) {
1526 _mesa_error(ctx, GL_INVALID_VALUE,
1527 "glTexSubImage%dD(depth=%d)", dimensions, depth);
1528 return GL_TRUE;
1529 }
1530
1531 if (!_mesa_is_legal_format_and_type(ctx, format, type)) {
1532 _mesa_error(ctx, GL_INVALID_ENUM,
1533 "glTexSubImage%dD(incompatible format 0x%x, type 0x%x)",
1534 dimensions, format, type);
1535 return GL_TRUE;
1536 }
1537
1538 return GL_FALSE;
1539 }
1540
1541 static GLboolean
1542 subtexture_error_check2( GLcontext *ctx, GLuint dimensions,
1543 GLenum target, GLint level,
1544 GLint xoffset, GLint yoffset, GLint zoffset,
1545 GLint width, GLint height, GLint depth,
1546 GLenum format, GLenum type,
1547 const struct gl_texture_image *destTex )
1548 {
1549 if (!destTex) {
1550 /* undefined image level */
1551 _mesa_error(ctx, GL_INVALID_OPERATION, "glTexSubImage%dD", dimensions);
1552 return GL_TRUE;
1553 }
1554
1555 if (xoffset < -((GLint)destTex->Border)) {
1556 _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage%dD(xoffset)",
1557 dimensions);
1558 return GL_TRUE;
1559 }
1560 if (xoffset + width > (GLint) (destTex->Width + destTex->Border)) {
1561 _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage%dD(xoffset+width)",
1562 dimensions);
1563 return GL_TRUE;
1564 }
1565 if (dimensions > 1) {
1566 if (yoffset < -((GLint)destTex->Border)) {
1567 _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage%dD(yoffset)",
1568 dimensions);
1569 return GL_TRUE;
1570 }
1571 if (yoffset + height > (GLint) (destTex->Height + destTex->Border)) {
1572 _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage%dD(yoffset+height)",
1573 dimensions);
1574 return GL_TRUE;
1575 }
1576 }
1577 if (dimensions > 2) {
1578 if (zoffset < -((GLint)destTex->Border)) {
1579 _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage3D(zoffset)");
1580 return GL_TRUE;
1581 }
1582 if (zoffset + depth > (GLint) (destTex->Depth + destTex->Border)) {
1583 _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage3D(zoffset+depth)");
1584 return GL_TRUE;
1585 }
1586 }
1587
1588 #if FEATURE_EXT_texture_sRGB
1589 if (destTex->InternalFormat == GL_COMPRESSED_SRGB_S3TC_DXT1_EXT ||
1590 destTex->InternalFormat == GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT ||
1591 destTex->InternalFormat == GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT ||
1592 destTex->InternalFormat == GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT) {
1593 if ((width & 0x3) || (height & 0x3) ||
1594 (xoffset & 0x3) || (yoffset & 0x3))
1595 _mesa_error(ctx, GL_INVALID_OPERATION,
1596 "glTexSubImage%dD(size or offset not multiple of 4)",
1597 dimensions);
1598 return GL_TRUE;
1599 }
1600 #endif
1601
1602 if (_mesa_is_format_compressed(destTex->TexFormat)) {
1603 if (!target_can_be_compressed(ctx, target)) {
1604 _mesa_error(ctx, GL_INVALID_ENUM,
1605 "glTexSubImage%D(target)", dimensions);
1606 return GL_TRUE;
1607 }
1608 /* offset must be multiple of 4 */
1609 if ((xoffset & 3) || (yoffset & 3)) {
1610 _mesa_error(ctx, GL_INVALID_OPERATION,
1611 "glTexSubImage%D(xoffset or yoffset)", dimensions);
1612 return GL_TRUE;
1613 }
1614 /* size must be multiple of 4 or equal to whole texture size */
1615 if ((width & 3) && (GLuint) width != destTex->Width) {
1616 _mesa_error(ctx, GL_INVALID_OPERATION,
1617 "glTexSubImage%D(width)", dimensions);
1618 return GL_TRUE;
1619 }
1620 if ((height & 3) && (GLuint) height != destTex->Height) {
1621 _mesa_error(ctx, GL_INVALID_OPERATION,
1622 "glTexSubImage%D(width)", dimensions);
1623 return GL_TRUE;
1624 }
1625 }
1626
1627 return GL_FALSE;
1628 }
1629
1630
1631 /**
1632 * Test glCopyTexImage[12]D() parameters for errors.
1633 *
1634 * \param ctx GL context.
1635 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1636 * \param target texture target given by the user.
1637 * \param level image level given by the user.
1638 * \param internalFormat internal format given by the user.
1639 * \param width image width given by the user.
1640 * \param height image height given by the user.
1641 * \param border texture border.
1642 *
1643 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
1644 *
1645 * Verifies each of the parameters against the constants specified in
1646 * __GLcontextRec::Const and the supported extensions, and according to the
1647 * OpenGL specification.
1648 */
1649 static GLboolean
1650 copytexture_error_check( GLcontext *ctx, GLuint dimensions,
1651 GLenum target, GLint level, GLint internalFormat,
1652 GLint width, GLint height, GLint border )
1653 {
1654 GLenum type;
1655 GLboolean sizeOK;
1656 GLint format;
1657
1658 /* Basic level check (more checking in ctx->Driver.TestProxyTexImage) */
1659 if (level < 0 || level >= MAX_TEXTURE_LEVELS) {
1660 _mesa_error(ctx, GL_INVALID_VALUE,
1661 "glCopyTexImage%dD(level=%d)", dimensions, level);
1662 return GL_TRUE;
1663 }
1664
1665 /* Check that the source buffer is complete */
1666 if (ctx->ReadBuffer->Name) {
1667 _mesa_test_framebuffer_completeness(ctx, ctx->ReadBuffer);
1668 if (ctx->ReadBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
1669 _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
1670 "glCopyTexImage%dD(invalid readbuffer)", dimensions);
1671 return GL_TRUE;
1672 }
1673 }
1674
1675 /* Check border */
1676 if (border < 0 || border > 1 ||
1677 ((target == GL_TEXTURE_RECTANGLE_NV ||
1678 target == GL_PROXY_TEXTURE_RECTANGLE_NV) && border != 0)) {
1679 return GL_TRUE;
1680 }
1681
1682 format = _mesa_base_tex_format(ctx, internalFormat);
1683 if (format < 0) {
1684 _mesa_error(ctx, GL_INVALID_VALUE,
1685 "glCopyTexImage%dD(internalFormat)", dimensions);
1686 return GL_TRUE;
1687 }
1688
1689 if (!_mesa_source_buffer_exists(ctx, format)) {
1690 _mesa_error(ctx, GL_INVALID_OPERATION,
1691 "glCopyTexImage%dD(missing readbuffer)", dimensions);
1692 return GL_TRUE;
1693 }
1694
1695 /* NOTE: the format and type aren't really significant for
1696 * TestProxyTexImage(). Only the internalformat really matters.
1697 */
1698 type = GL_FLOAT;
1699
1700 /* Check target and call ctx->Driver.TestProxyTexImage() to check the
1701 * level, width, height and depth.
1702 */
1703 if (dimensions == 1) {
1704 if (target == GL_TEXTURE_1D) {
1705 sizeOK = ctx->Driver.TestProxyTexImage(ctx, GL_PROXY_TEXTURE_1D,
1706 level, internalFormat,
1707 format, type,
1708 width, 1, 1, border);
1709 }
1710 else {
1711 _mesa_error( ctx, GL_INVALID_ENUM, "glCopyTexImage1D(target)" );
1712 return GL_TRUE;
1713 }
1714 }
1715 else if (dimensions == 2) {
1716 if (target == GL_TEXTURE_2D) {
1717 sizeOK = ctx->Driver.TestProxyTexImage(ctx, GL_PROXY_TEXTURE_2D,
1718 level, internalFormat,
1719 format, type,
1720 width, height, 1, border);
1721 }
1722 else if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
1723 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB) {
1724 if (!ctx->Extensions.ARB_texture_cube_map) {
1725 _mesa_error( ctx, GL_INVALID_ENUM, "glCopyTexImage2D(target)" );
1726 return GL_TRUE;
1727 }
1728 sizeOK = (width == height) &&
1729 ctx->Driver.TestProxyTexImage(ctx, GL_PROXY_TEXTURE_CUBE_MAP_ARB,
1730 level, internalFormat, format, type,
1731 width, height, 1, border);
1732 }
1733 else if (target == GL_TEXTURE_RECTANGLE_NV) {
1734 if (!ctx->Extensions.NV_texture_rectangle) {
1735 _mesa_error( ctx, GL_INVALID_ENUM, "glCopyTexImage2D(target)" );
1736 return GL_TRUE;
1737 }
1738 sizeOK = ctx->Driver.TestProxyTexImage(ctx,
1739 GL_PROXY_TEXTURE_RECTANGLE_NV,
1740 level, internalFormat,
1741 format, type,
1742 width, height, 1, border);
1743 }
1744 else if (target == GL_TEXTURE_1D_ARRAY_EXT) {
1745 if (!ctx->Extensions.MESA_texture_array) {
1746 _mesa_error(ctx, GL_INVALID_ENUM, "glCopyTexImage2D(target)");
1747 return GL_TRUE;
1748 }
1749 sizeOK = ctx->Driver.TestProxyTexImage(ctx,
1750 GL_PROXY_TEXTURE_1D_ARRAY_EXT,
1751 level, internalFormat,
1752 format, type,
1753 width, height, 1, border);
1754 }
1755 else {
1756 _mesa_error( ctx, GL_INVALID_ENUM, "glCopyTexImage2D(target)" );
1757 return GL_TRUE;
1758 }
1759 }
1760 else {
1761 _mesa_problem(ctx, "invalid dimensions in copytexture_error_check");
1762 return GL_TRUE;
1763 }
1764
1765 if (!sizeOK) {
1766 if (dimensions == 1) {
1767 _mesa_error(ctx, GL_INVALID_VALUE,
1768 "glCopyTexImage1D(width=%d)", width);
1769 }
1770 else {
1771 ASSERT(dimensions == 2);
1772 _mesa_error(ctx, GL_INVALID_VALUE,
1773 "glCopyTexImage2D(width=%d, height=%d)", width, height);
1774 }
1775 return GL_TRUE;
1776 }
1777
1778 if (is_compressed_format(ctx, internalFormat)) {
1779 if (!target_can_be_compressed(ctx, target)) {
1780 _mesa_error(ctx, GL_INVALID_ENUM,
1781 "glCopyTexImage%d(target)", dimensions);
1782 return GL_TRUE;
1783 }
1784 if (border != 0) {
1785 _mesa_error(ctx, GL_INVALID_OPERATION,
1786 "glCopyTexImage%D(border!=0)", dimensions);
1787 return GL_TRUE;
1788 }
1789 }
1790 else if (_mesa_is_depth_format(internalFormat)) {
1791 /* make sure we have depth/stencil buffers */
1792 if (!ctx->ReadBuffer->_DepthBuffer) {
1793 _mesa_error(ctx, GL_INVALID_OPERATION,
1794 "glCopyTexImage%D(no depth)", dimensions);
1795 return GL_TRUE;
1796 }
1797 }
1798 else if (_mesa_is_depthstencil_format(internalFormat)) {
1799 /* make sure we have depth/stencil buffers */
1800 if (!ctx->ReadBuffer->_DepthBuffer || !ctx->ReadBuffer->_StencilBuffer) {
1801 _mesa_error(ctx, GL_INVALID_OPERATION,
1802 "glCopyTexImage%D(no depth/stencil buffer)", dimensions);
1803 return GL_TRUE;
1804 }
1805 }
1806
1807 /* if we get here, the parameters are OK */
1808 return GL_FALSE;
1809 }
1810
1811
1812 /**
1813 * Test glCopyTexSubImage[12]D() parameters for errors.
1814 * Note that this is the first part of error checking.
1815 * See also copytexsubimage_error_check2() below for the second part.
1816 *
1817 * \param ctx GL context.
1818 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1819 * \param target texture target given by the user.
1820 * \param level image level given by the user.
1821 *
1822 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
1823 */
1824 static GLboolean
1825 copytexsubimage_error_check1( GLcontext *ctx, GLuint dimensions,
1826 GLenum target, GLint level)
1827 {
1828 /* Check that the source buffer is complete */
1829 if (ctx->ReadBuffer->Name) {
1830 _mesa_test_framebuffer_completeness(ctx, ctx->ReadBuffer);
1831 if (ctx->ReadBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
1832 _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
1833 "glCopyTexImage%dD(invalid readbuffer)", dimensions);
1834 return GL_TRUE;
1835 }
1836 }
1837
1838 /* Check target */
1839 if (dimensions == 1) {
1840 if (target != GL_TEXTURE_1D) {
1841 _mesa_error( ctx, GL_INVALID_ENUM, "glCopyTexSubImage1D(target)" );
1842 return GL_TRUE;
1843 }
1844 }
1845 else if (dimensions == 2) {
1846 if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
1847 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB) {
1848 if (!ctx->Extensions.ARB_texture_cube_map) {
1849 _mesa_error( ctx, GL_INVALID_ENUM, "glCopyTexSubImage2D(target)" );
1850 return GL_TRUE;
1851 }
1852 }
1853 else if (target == GL_TEXTURE_RECTANGLE_NV) {
1854 if (!ctx->Extensions.NV_texture_rectangle) {
1855 _mesa_error( ctx, GL_INVALID_ENUM, "glCopyTexSubImage2D(target)" );
1856 return GL_TRUE;
1857 }
1858 }
1859 else if (target == GL_TEXTURE_1D_ARRAY_EXT) {
1860 if (!ctx->Extensions.MESA_texture_array) {
1861 _mesa_error( ctx, GL_INVALID_ENUM, "glCopyTexSubImage2D(target)" );
1862 return GL_TRUE;
1863 }
1864 }
1865 else if (target != GL_TEXTURE_2D) {
1866 _mesa_error( ctx, GL_INVALID_ENUM, "glCopyTexSubImage2D(target)" );
1867 return GL_TRUE;
1868 }
1869 }
1870 else if (dimensions == 3) {
1871 if (((target != GL_TEXTURE_2D_ARRAY_EXT) ||
1872 (!ctx->Extensions.MESA_texture_array))
1873 && (target != GL_TEXTURE_3D)) {
1874 _mesa_error( ctx, GL_INVALID_ENUM, "glCopyTexSubImage3D(target)" );
1875 return GL_TRUE;
1876 }
1877 }
1878
1879 /* Check level */
1880 if (level < 0 || level >= MAX_TEXTURE_LEVELS) {
1881 _mesa_error(ctx, GL_INVALID_VALUE,
1882 "glCopyTexSubImage%dD(level=%d)", dimensions, level);
1883 return GL_TRUE;
1884 }
1885
1886 return GL_FALSE;
1887 }
1888
1889
1890 /**
1891 * Second part of error checking for glCopyTexSubImage[12]D().
1892 * \param xoffset sub-image x offset given by the user.
1893 * \param yoffset sub-image y offset given by the user.
1894 * \param zoffset sub-image z offset given by the user.
1895 * \param width image width given by the user.
1896 * \param height image height given by the user.
1897 */
1898 static GLboolean
1899 copytexsubimage_error_check2( GLcontext *ctx, GLuint dimensions,
1900 GLenum target, GLint level,
1901 GLint xoffset, GLint yoffset, GLint zoffset,
1902 GLsizei width, GLsizei height,
1903 const struct gl_texture_image *teximage )
1904 {
1905 /* check that dest tex image exists */
1906 if (!teximage) {
1907 _mesa_error(ctx, GL_INVALID_OPERATION,
1908 "glCopyTexSubImage%dD(undefined texture level: %d)",
1909 dimensions, level);
1910 return GL_TRUE;
1911 }
1912
1913 /* Check size */
1914 if (width < 0) {
1915 _mesa_error(ctx, GL_INVALID_VALUE,
1916 "glCopyTexSubImage%dD(width=%d)", dimensions, width);
1917 return GL_TRUE;
1918 }
1919 if (dimensions > 1 && height < 0) {
1920 _mesa_error(ctx, GL_INVALID_VALUE,
1921 "glCopyTexSubImage%dD(height=%d)", dimensions, height);
1922 return GL_TRUE;
1923 }
1924
1925 /* check x/y offsets */
1926 if (xoffset < -((GLint)teximage->Border)) {
1927 _mesa_error(ctx, GL_INVALID_VALUE,
1928 "glCopyTexSubImage%dD(xoffset=%d)", dimensions, xoffset);
1929 return GL_TRUE;
1930 }
1931 if (xoffset + width > (GLint) (teximage->Width + teximage->Border)) {
1932 _mesa_error(ctx, GL_INVALID_VALUE,
1933 "glCopyTexSubImage%dD(xoffset+width)", dimensions);
1934 return GL_TRUE;
1935 }
1936 if (dimensions > 1) {
1937 if (yoffset < -((GLint)teximage->Border)) {
1938 _mesa_error(ctx, GL_INVALID_VALUE,
1939 "glCopyTexSubImage%dD(yoffset=%d)", dimensions, yoffset);
1940 return GL_TRUE;
1941 }
1942 /* NOTE: we're adding the border here, not subtracting! */
1943 if (yoffset + height > (GLint) (teximage->Height + teximage->Border)) {
1944 _mesa_error(ctx, GL_INVALID_VALUE,
1945 "glCopyTexSubImage%dD(yoffset+height)", dimensions);
1946 return GL_TRUE;
1947 }
1948 }
1949
1950 /* check z offset */
1951 if (dimensions > 2) {
1952 if (zoffset < -((GLint)teximage->Border)) {
1953 _mesa_error(ctx, GL_INVALID_VALUE,
1954 "glCopyTexSubImage%dD(zoffset)", dimensions);
1955 return GL_TRUE;
1956 }
1957 if (zoffset > (GLint) (teximage->Depth + teximage->Border)) {
1958 _mesa_error(ctx, GL_INVALID_VALUE,
1959 "glCopyTexSubImage%dD(zoffset+depth)", dimensions);
1960 return GL_TRUE;
1961 }
1962 }
1963
1964 if (_mesa_is_format_compressed(teximage->TexFormat)) {
1965 if (!target_can_be_compressed(ctx, target)) {
1966 _mesa_error(ctx, GL_INVALID_ENUM,
1967 "glCopyTexSubImage%d(target)", dimensions);
1968 return GL_TRUE;
1969 }
1970 /* offset must be multiple of 4 */
1971 if ((xoffset & 3) || (yoffset & 3)) {
1972 _mesa_error(ctx, GL_INVALID_VALUE,
1973 "glCopyTexSubImage%D(xoffset or yoffset)", dimensions);
1974 return GL_TRUE;
1975 }
1976 /* size must be multiple of 4 */
1977 if ((width & 3) != 0 && (GLuint) width != teximage->Width) {
1978 _mesa_error(ctx, GL_INVALID_VALUE,
1979 "glCopyTexSubImage%D(width)", dimensions);
1980 return GL_TRUE;
1981 }
1982 if ((height & 3) != 0 && (GLuint) height != teximage->Height) {
1983 _mesa_error(ctx, GL_INVALID_VALUE,
1984 "glCopyTexSubImage%D(height)", dimensions);
1985 return GL_TRUE;
1986 }
1987 }
1988
1989 if (teximage->InternalFormat == GL_YCBCR_MESA) {
1990 _mesa_error(ctx, GL_INVALID_OPERATION, "glCopyTexSubImage2D");
1991 return GL_TRUE;
1992 }
1993
1994 if (!_mesa_source_buffer_exists(ctx, teximage->_BaseFormat)) {
1995 _mesa_error(ctx, GL_INVALID_OPERATION,
1996 "glCopyTexSubImage%dD(missing readbuffer, format=0x%x)",
1997 dimensions, teximage->_BaseFormat);
1998 return GL_TRUE;
1999 }
2000
2001 if (teximage->_BaseFormat == GL_DEPTH_COMPONENT) {
2002 if (!ctx->ReadBuffer->_DepthBuffer) {
2003 _mesa_error(ctx, GL_INVALID_OPERATION,
2004 "glCopyTexSubImage%D(no depth buffer)",
2005 dimensions);
2006 return GL_TRUE;
2007 }
2008 }
2009 else if (teximage->_BaseFormat == GL_DEPTH_STENCIL_EXT) {
2010 if (!ctx->ReadBuffer->_DepthBuffer || !ctx->ReadBuffer->_StencilBuffer) {
2011 _mesa_error(ctx, GL_INVALID_OPERATION,
2012 "glCopyTexSubImage%D(no depth/stencil buffer)",
2013 dimensions);
2014 return GL_TRUE;
2015 }
2016 }
2017
2018 /* if we get here, the parameters are OK */
2019 return GL_FALSE;
2020 }
2021
2022
2023 /** Callback info for walking over FBO hash table */
2024 struct cb_info
2025 {
2026 GLcontext *ctx;
2027 struct gl_texture_object *texObj;
2028 GLuint level, face;
2029 };
2030
2031
2032 /**
2033 * Check render to texture callback. Called from _mesa_HashWalk().
2034 */
2035 static void
2036 check_rtt_cb(GLuint key, void *data, void *userData)
2037 {
2038 struct gl_framebuffer *fb = (struct gl_framebuffer *) data;
2039 const struct cb_info *info = (struct cb_info *) userData;
2040 GLcontext *ctx = info->ctx;
2041 const struct gl_texture_object *texObj = info->texObj;
2042 const GLuint level = info->level, face = info->face;
2043
2044 /* If this is a user-created FBO */
2045 if (fb->Name) {
2046 GLuint i;
2047 /* check if any of the FBO's attachments point to 'texObj' */
2048 for (i = 0; i < BUFFER_COUNT; i++) {
2049 struct gl_renderbuffer_attachment *att = fb->Attachment + i;
2050 if (att->Type == GL_TEXTURE &&
2051 att->Texture == texObj &&
2052 att->TextureLevel == level &&
2053 att->CubeMapFace == face) {
2054 ASSERT(att->Texture->Image[att->CubeMapFace][att->TextureLevel]);
2055 /* Tell driver about the new renderbuffer texture */
2056 ctx->Driver.RenderTexture(ctx, ctx->DrawBuffer, att);
2057 /* Mark fb status as indeterminate to force re-validation */
2058 fb->_Status = 0;
2059 }
2060 }
2061 }
2062 }
2063
2064
2065 /**
2066 * When a texture image is specified we have to check if it's bound to
2067 * any framebuffer objects (render to texture) in order to detect changes
2068 * in size or format since that effects FBO completeness.
2069 * Any FBOs rendering into the texture must be re-validated.
2070 */
2071 static void
2072 update_fbo_texture(GLcontext *ctx, struct gl_texture_object *texObj,
2073 GLuint face, GLuint level)
2074 {
2075 /* Only check this texture if it's been marked as RenderToTexture */
2076 if (texObj->_RenderToTexture) {
2077 struct cb_info info;
2078 info.ctx = ctx;
2079 info.texObj = texObj;
2080 info.level = level;
2081 info.face = face;
2082 _mesa_HashWalk(ctx->Shared->FrameBuffers, check_rtt_cb, &info);
2083 }
2084 }
2085
2086
2087 /**
2088 * If the texture object's GenerateMipmap flag is set and we've
2089 * changed the texture base level image, regenerate the rest of the
2090 * mipmap levels now.
2091 */
2092 static INLINE void
2093 check_gen_mipmap(GLcontext *ctx, GLenum target,
2094 struct gl_texture_object *texObj, GLint level)
2095 {
2096 ASSERT(target != GL_TEXTURE_CUBE_MAP);
2097 if (texObj->GenerateMipmap && level == texObj->BaseLevel) {
2098 ASSERT(ctx->Driver.GenerateMipmap);
2099 ctx->Driver.GenerateMipmap(ctx, target, texObj);
2100 }
2101 }
2102
2103
2104 /** Debug helper: override the user-requested internal format */
2105 static GLenum
2106 override_internal_format(GLenum internalFormat, GLint width, GLint height)
2107 {
2108 #if 0
2109 if (internalFormat == GL_RGBA16F_ARB ||
2110 internalFormat == GL_RGBA32F_ARB) {
2111 printf("Convert rgba float tex to int %d x %d\n", width, height);
2112 return GL_RGBA;
2113 }
2114 else if (internalFormat == GL_RGB16F_ARB ||
2115 internalFormat == GL_RGB32F_ARB) {
2116 printf("Convert rgb float tex to int %d x %d\n", width, height);
2117 return GL_RGB;
2118 }
2119 else if (internalFormat == GL_LUMINANCE_ALPHA16F_ARB ||
2120 internalFormat == GL_LUMINANCE_ALPHA32F_ARB) {
2121 printf("Convert luminance float tex to int %d x %d\n", width, height);
2122 return GL_LUMINANCE_ALPHA;
2123 }
2124 else if (internalFormat == GL_LUMINANCE16F_ARB ||
2125 internalFormat == GL_LUMINANCE32F_ARB) {
2126 printf("Convert luminance float tex to int %d x %d\n", width, height);
2127 return GL_LUMINANCE;
2128 }
2129 else if (internalFormat == GL_ALPHA16F_ARB ||
2130 internalFormat == GL_ALPHA32F_ARB) {
2131 printf("Convert luminance float tex to int %d x %d\n", width, height);
2132 return GL_ALPHA;
2133 }
2134 /*
2135 else if (internalFormat == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT) {
2136 internalFormat = GL_RGBA;
2137 }
2138 */
2139 else {
2140 return internalFormat;
2141 }
2142 #else
2143 return internalFormat;
2144 #endif
2145 }
2146
2147
2148 /*
2149 * Called from the API. Note that width includes the border.
2150 */
2151 void GLAPIENTRY
2152 _mesa_TexImage1D( GLenum target, GLint level, GLint internalFormat,
2153 GLsizei width, GLint border, GLenum format,
2154 GLenum type, const GLvoid *pixels )
2155 {
2156 GLsizei postConvWidth = width;
2157 GET_CURRENT_CONTEXT(ctx);
2158 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2159
2160 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
2161 _mesa_debug(ctx, "glTexImage1D %s %d %s %d %d %s %s %p\n",
2162 _mesa_lookup_enum_by_nr(target), level,
2163 _mesa_lookup_enum_by_nr(internalFormat), width, border,
2164 _mesa_lookup_enum_by_nr(format),
2165 _mesa_lookup_enum_by_nr(type), pixels);
2166
2167 internalFormat = override_internal_format(internalFormat, width, 1);
2168
2169 #if FEATURE_convolve
2170 if (_mesa_is_color_format(internalFormat)) {
2171 _mesa_adjust_image_for_convolution(ctx, 1, &postConvWidth, NULL);
2172 }
2173 #endif
2174
2175 if (target == GL_TEXTURE_1D) {
2176 /* non-proxy target */
2177 struct gl_texture_object *texObj;
2178 struct gl_texture_image *texImage;
2179 const GLuint face = _mesa_tex_target_to_face(target);
2180
2181 if (texture_error_check(ctx, target, level, internalFormat,
2182 format, type, 1, postConvWidth, 1, 1, border)) {
2183 return; /* error was recorded */
2184 }
2185
2186 if (ctx->NewState & _MESA_NEW_TRANSFER_STATE)
2187 _mesa_update_state(ctx);
2188
2189 texObj = get_current_tex_object(ctx, target);
2190 _mesa_lock_texture(ctx, texObj);
2191 {
2192 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
2193 if (!texImage) {
2194 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage1D");
2195 }
2196 else {
2197 if (texImage->Data) {
2198 ctx->Driver.FreeTexImageData( ctx, texImage );
2199 }
2200
2201 ASSERT(texImage->Data == NULL);
2202
2203 clear_teximage_fields(texImage); /* not really needed, but helpful */
2204 _mesa_init_teximage_fields(ctx, target, texImage,
2205 postConvWidth, 1, 1,
2206 border, internalFormat);
2207
2208 /* Choose actual texture format */
2209 texImage->TexFormat =
2210 ctx->Driver.ChooseTextureFormat(ctx, internalFormat,
2211 format, type);
2212 ASSERT(texImage->TexFormat != MESA_FORMAT_NONE);
2213
2214 /* Give the texture to the driver. <pixels> may be null. */
2215 ASSERT(ctx->Driver.TexImage1D);
2216 ctx->Driver.TexImage1D(ctx, target, level, internalFormat,
2217 width, border, format, type, pixels,
2218 &ctx->Unpack, texObj, texImage);
2219
2220 ASSERT(texImage->TexFormat);
2221
2222 _mesa_set_fetch_functions(texImage, 1);
2223
2224 check_gen_mipmap(ctx, target, texObj, level);
2225
2226 update_fbo_texture(ctx, texObj, face, level);
2227
2228 /* state update */
2229 texObj->_Complete = GL_FALSE;
2230 ctx->NewState |= _NEW_TEXTURE;
2231 }
2232 }
2233 _mesa_unlock_texture(ctx, texObj);
2234 }
2235 else if (target == GL_PROXY_TEXTURE_1D) {
2236 /* Proxy texture: check for errors and update proxy state */
2237 struct gl_texture_image *texImage;
2238 texImage = _mesa_get_proxy_tex_image(ctx, target, level);
2239 if (texture_error_check(ctx, target, level, internalFormat,
2240 format, type, 1, postConvWidth, 1, 1, border)) {
2241 /* when error, clear all proxy texture image parameters */
2242 if (texImage)
2243 clear_teximage_fields(texImage);
2244 }
2245 else {
2246 /* no error, set the tex image parameters */
2247 ASSERT(texImage);
2248 _mesa_init_teximage_fields(ctx, target, texImage,
2249 postConvWidth, 1, 1,
2250 border, internalFormat);
2251 texImage->TexFormat =
2252 ctx->Driver.ChooseTextureFormat(ctx, internalFormat, format, type);
2253 }
2254 }
2255 else {
2256 _mesa_error( ctx, GL_INVALID_ENUM, "glTexImage1D(target)" );
2257 return;
2258 }
2259 }
2260
2261
2262 void GLAPIENTRY
2263 _mesa_TexImage2D( GLenum target, GLint level, GLint internalFormat,
2264 GLsizei width, GLsizei height, GLint border,
2265 GLenum format, GLenum type,
2266 const GLvoid *pixels )
2267 {
2268 GLsizei postConvWidth = width, postConvHeight = height;
2269 GET_CURRENT_CONTEXT(ctx);
2270 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2271
2272 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
2273 _mesa_debug(ctx, "glTexImage2D %s %d %s %d %d %d %s %s %p\n",
2274 _mesa_lookup_enum_by_nr(target), level,
2275 _mesa_lookup_enum_by_nr(internalFormat), width, height,
2276 border, _mesa_lookup_enum_by_nr(format),
2277 _mesa_lookup_enum_by_nr(type), pixels);
2278
2279 internalFormat = override_internal_format(internalFormat, width, height);
2280
2281 #if FEATURE_convolve
2282 if (_mesa_is_color_format(internalFormat)) {
2283 _mesa_adjust_image_for_convolution(ctx, 2, &postConvWidth,
2284 &postConvHeight);
2285 }
2286 #endif
2287
2288 if (target == GL_TEXTURE_2D ||
2289 (ctx->Extensions.ARB_texture_cube_map &&
2290 target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
2291 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB) ||
2292 (ctx->Extensions.NV_texture_rectangle &&
2293 target == GL_TEXTURE_RECTANGLE_NV) ||
2294 (ctx->Extensions.MESA_texture_array &&
2295 target == GL_TEXTURE_1D_ARRAY_EXT)) {
2296 /* non-proxy target */
2297 struct gl_texture_object *texObj;
2298 struct gl_texture_image *texImage;
2299 const GLuint face = _mesa_tex_target_to_face(target);
2300
2301 if (texture_error_check(ctx, target, level, internalFormat,
2302 format, type, 2, postConvWidth, postConvHeight,
2303 1, border)) {
2304 return; /* error was recorded */
2305 }
2306
2307 if (ctx->NewState & _MESA_NEW_TRANSFER_STATE)
2308 _mesa_update_state(ctx);
2309
2310 texObj = get_current_tex_object(ctx, target);
2311 _mesa_lock_texture(ctx, texObj);
2312 {
2313 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
2314 if (!texImage) {
2315 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage2D");
2316 }
2317 else {
2318 if (texImage->Data) {
2319 ctx->Driver.FreeTexImageData( ctx, texImage );
2320 }
2321
2322 ASSERT(texImage->Data == NULL);
2323 clear_teximage_fields(texImage); /* not really needed, but helpful */
2324 _mesa_init_teximage_fields(ctx, target, texImage,
2325 postConvWidth, postConvHeight, 1,
2326 border, internalFormat);
2327
2328 /* Choose actual texture format */
2329 texImage->TexFormat =
2330 ctx->Driver.ChooseTextureFormat(ctx, internalFormat,
2331 format, type);
2332 ASSERT(texImage->TexFormat != MESA_FORMAT_NONE);
2333
2334 /* Give the texture to the driver. <pixels> may be null. */
2335 ASSERT(ctx->Driver.TexImage2D);
2336 ctx->Driver.TexImage2D(ctx, target, level, internalFormat,
2337 width, height, border, format, type,
2338 pixels, &ctx->Unpack, texObj, texImage);
2339
2340 ASSERT(texImage->TexFormat);
2341
2342 _mesa_set_fetch_functions(texImage, 2);
2343
2344 check_gen_mipmap(ctx, target, texObj, level);
2345
2346 update_fbo_texture(ctx, texObj, face, level);
2347
2348 /* state update */
2349 texObj->_Complete = GL_FALSE;
2350 ctx->NewState |= _NEW_TEXTURE;
2351 }
2352 }
2353 _mesa_unlock_texture(ctx, texObj);
2354 }
2355 else if (target == GL_PROXY_TEXTURE_2D ||
2356 (target == GL_PROXY_TEXTURE_CUBE_MAP_ARB &&
2357 ctx->Extensions.ARB_texture_cube_map) ||
2358 (target == GL_PROXY_TEXTURE_RECTANGLE_NV &&
2359 ctx->Extensions.NV_texture_rectangle) ||
2360 (ctx->Extensions.MESA_texture_array &&
2361 target == GL_PROXY_TEXTURE_1D_ARRAY_EXT)) {
2362 /* Proxy texture: check for errors and update proxy state */
2363 struct gl_texture_image *texImage;
2364 texImage = _mesa_get_proxy_tex_image(ctx, target, level);
2365 if (texture_error_check(ctx, target, level, internalFormat,
2366 format, type, 2, postConvWidth, postConvHeight,
2367 1, border)) {
2368 /* when error, clear all proxy texture image parameters */
2369 if (texImage)
2370 clear_teximage_fields(texImage);
2371 }
2372 else {
2373 /* no error, set the tex image parameters */
2374 _mesa_init_teximage_fields(ctx, target, texImage,
2375 postConvWidth, postConvHeight, 1,
2376 border, internalFormat);
2377 texImage->TexFormat =
2378 ctx->Driver.ChooseTextureFormat(ctx, internalFormat, format, type);
2379 }
2380 }
2381 else {
2382 _mesa_error( ctx, GL_INVALID_ENUM, "glTexImage2D(target)" );
2383 return;
2384 }
2385 }
2386
2387
2388 /*
2389 * Called by the API or display list executor.
2390 * Note that width and height include the border.
2391 */
2392 void GLAPIENTRY
2393 _mesa_TexImage3D( GLenum target, GLint level, GLint internalFormat,
2394 GLsizei width, GLsizei height, GLsizei depth,
2395 GLint border, GLenum format, GLenum type,
2396 const GLvoid *pixels )
2397 {
2398 GET_CURRENT_CONTEXT(ctx);
2399 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2400
2401 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
2402 _mesa_debug(ctx, "glTexImage3D %s %d %s %d %d %d %d %s %s %p\n",
2403 _mesa_lookup_enum_by_nr(target), level,
2404 _mesa_lookup_enum_by_nr(internalFormat), width, height,
2405 depth, border, _mesa_lookup_enum_by_nr(format),
2406 _mesa_lookup_enum_by_nr(type), pixels);
2407
2408 internalFormat = override_internal_format(internalFormat, width, height);
2409
2410 if (target == GL_TEXTURE_3D ||
2411 (ctx->Extensions.MESA_texture_array &&
2412 target == GL_TEXTURE_2D_ARRAY_EXT)) {
2413 /* non-proxy target */
2414 struct gl_texture_object *texObj;
2415 struct gl_texture_image *texImage;
2416 const GLuint face = _mesa_tex_target_to_face(target);
2417
2418 if (texture_error_check(ctx, target, level, (GLint) internalFormat,
2419 format, type, 3, width, height, depth, border)) {
2420 return; /* error was recorded */
2421 }
2422
2423 if (ctx->NewState & _MESA_NEW_TRANSFER_STATE)
2424 _mesa_update_state(ctx);
2425
2426 texObj = get_current_tex_object(ctx, target);
2427 _mesa_lock_texture(ctx, texObj);
2428 {
2429 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
2430 if (!texImage) {
2431 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage3D");
2432 }
2433 else {
2434 if (texImage->Data) {
2435 ctx->Driver.FreeTexImageData( ctx, texImage );
2436 }
2437
2438 ASSERT(texImage->Data == NULL);
2439 clear_teximage_fields(texImage); /* not really needed, but helpful */
2440 _mesa_init_teximage_fields(ctx, target, texImage,
2441 width, height, depth,
2442 border, internalFormat);
2443
2444 /* Choose actual texture format */
2445 texImage->TexFormat =
2446 ctx->Driver.ChooseTextureFormat(ctx, internalFormat,
2447 format, type);
2448 ASSERT(texImage->TexFormat != MESA_FORMAT_NONE);
2449
2450 /* Give the texture to the driver. <pixels> may be null. */
2451 ASSERT(ctx->Driver.TexImage3D);
2452 ctx->Driver.TexImage3D(ctx, target, level, internalFormat,
2453 width, height, depth, border, format, type,
2454 pixels, &ctx->Unpack, texObj, texImage);
2455
2456 ASSERT(texImage->TexFormat);
2457
2458 _mesa_set_fetch_functions(texImage, 3);
2459
2460 check_gen_mipmap(ctx, target, texObj, level);
2461
2462 update_fbo_texture(ctx, texObj, face, level);
2463
2464 /* state update */
2465 texObj->_Complete = GL_FALSE;
2466 ctx->NewState |= _NEW_TEXTURE;
2467 }
2468 }
2469 _mesa_unlock_texture(ctx, texObj);
2470 }
2471 else if (target == GL_PROXY_TEXTURE_3D ||
2472 (ctx->Extensions.MESA_texture_array &&
2473 target == GL_PROXY_TEXTURE_2D_ARRAY_EXT)) {
2474 /* Proxy texture: check for errors and update proxy state */
2475 struct gl_texture_image *texImage;
2476 texImage = _mesa_get_proxy_tex_image(ctx, target, level);
2477 if (texture_error_check(ctx, target, level, internalFormat,
2478 format, type, 3, width, height, depth, border)) {
2479 /* when error, clear all proxy texture image parameters */
2480 if (texImage)
2481 clear_teximage_fields(texImage);
2482 }
2483 else {
2484 /* no error, set the tex image parameters */
2485 _mesa_init_teximage_fields(ctx, target, texImage, width, height,
2486 depth, border, internalFormat);
2487 texImage->TexFormat =
2488 ctx->Driver.ChooseTextureFormat(ctx, internalFormat, format, type);
2489 }
2490 }
2491 else {
2492 _mesa_error( ctx, GL_INVALID_ENUM, "glTexImage3D(target)" );
2493 return;
2494 }
2495 }
2496
2497
2498 void GLAPIENTRY
2499 _mesa_TexImage3DEXT( GLenum target, GLint level, GLenum internalFormat,
2500 GLsizei width, GLsizei height, GLsizei depth,
2501 GLint border, GLenum format, GLenum type,
2502 const GLvoid *pixels )
2503 {
2504 _mesa_TexImage3D(target, level, (GLint) internalFormat, width, height,
2505 depth, border, format, type, pixels);
2506 }
2507
2508
2509
2510 void GLAPIENTRY
2511 _mesa_TexSubImage1D( GLenum target, GLint level,
2512 GLint xoffset, GLsizei width,
2513 GLenum format, GLenum type,
2514 const GLvoid *pixels )
2515 {
2516 GLsizei postConvWidth = width;
2517 struct gl_texture_object *texObj;
2518 struct gl_texture_image *texImage = NULL;
2519 GET_CURRENT_CONTEXT(ctx);
2520 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2521
2522 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
2523 _mesa_debug(ctx, "glTexSubImage1D %s %d %d %d %s %s %p\n",
2524 _mesa_lookup_enum_by_nr(target), level,
2525 xoffset, width, _mesa_lookup_enum_by_nr(format),
2526 _mesa_lookup_enum_by_nr(type), pixels);
2527
2528 if (ctx->NewState & _MESA_NEW_TRANSFER_STATE)
2529 _mesa_update_state(ctx);
2530
2531 #if FEATURE_convolve
2532 /* XXX should test internal format */
2533 if (_mesa_is_color_format(format)) {
2534 _mesa_adjust_image_for_convolution(ctx, 1, &postConvWidth, NULL);
2535 }
2536 #endif
2537
2538 if (subtexture_error_check(ctx, 1, target, level, xoffset, 0, 0,
2539 postConvWidth, 1, 1, format, type)) {
2540 return; /* error was detected */
2541 }
2542
2543
2544 texObj = get_current_tex_object(ctx, target);
2545 assert(texObj);
2546
2547 _mesa_lock_texture(ctx, texObj);
2548 {
2549 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
2550
2551 if (subtexture_error_check2(ctx, 1, target, level, xoffset, 0, 0,
2552 postConvWidth, 1, 1,
2553 format, type, texImage)) {
2554 /* error was recorded */
2555 }
2556 else if (width > 0) {
2557 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2558 xoffset += texImage->Border;
2559
2560 ASSERT(ctx->Driver.TexSubImage1D);
2561 ctx->Driver.TexSubImage1D(ctx, target, level, xoffset, width,
2562 format, type, pixels, &ctx->Unpack,
2563 texObj, texImage);
2564
2565 check_gen_mipmap(ctx, target, texObj, level);
2566
2567 ctx->NewState |= _NEW_TEXTURE;
2568 }
2569 }
2570 _mesa_unlock_texture(ctx, texObj);
2571 }
2572
2573
2574 void GLAPIENTRY
2575 _mesa_TexSubImage2D( GLenum target, GLint level,
2576 GLint xoffset, GLint yoffset,
2577 GLsizei width, GLsizei height,
2578 GLenum format, GLenum type,
2579 const GLvoid *pixels )
2580 {
2581 GLsizei postConvWidth = width, postConvHeight = height;
2582 struct gl_texture_object *texObj;
2583 struct gl_texture_image *texImage;
2584 GET_CURRENT_CONTEXT(ctx);
2585 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2586
2587 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
2588 _mesa_debug(ctx, "glTexSubImage2D %s %d %d %d %d %d %s %s %p\n",
2589 _mesa_lookup_enum_by_nr(target), level,
2590 xoffset, yoffset, width, height,
2591 _mesa_lookup_enum_by_nr(format),
2592 _mesa_lookup_enum_by_nr(type), pixels);
2593
2594 if (ctx->NewState & _MESA_NEW_TRANSFER_STATE)
2595 _mesa_update_state(ctx);
2596
2597 #if FEATURE_convolve
2598 /* XXX should test internal format */
2599 if (_mesa_is_color_format(format)) {
2600 _mesa_adjust_image_for_convolution(ctx, 2, &postConvWidth,
2601 &postConvHeight);
2602 }
2603 #endif
2604
2605 if (subtexture_error_check(ctx, 2, target, level, xoffset, yoffset, 0,
2606 postConvWidth, postConvHeight, 1, format, type)) {
2607 return; /* error was detected */
2608 }
2609
2610 texObj = get_current_tex_object(ctx, target);
2611
2612 _mesa_lock_texture(ctx, texObj);
2613 {
2614 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
2615
2616 if (subtexture_error_check2(ctx, 2, target, level, xoffset, yoffset, 0,
2617 postConvWidth, postConvHeight, 1,
2618 format, type, texImage)) {
2619 /* error was recorded */
2620 }
2621 else if (width > 0 && height >= 0) {
2622 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2623 xoffset += texImage->Border;
2624 yoffset += texImage->Border;
2625
2626 ASSERT(ctx->Driver.TexSubImage2D);
2627 ctx->Driver.TexSubImage2D(ctx, target, level, xoffset, yoffset,
2628 width, height, format, type, pixels,
2629 &ctx->Unpack, texObj, texImage);
2630
2631 check_gen_mipmap(ctx, target, texObj, level);
2632
2633 ctx->NewState |= _NEW_TEXTURE;
2634 }
2635 }
2636 _mesa_unlock_texture(ctx, texObj);
2637 }
2638
2639
2640
2641 void GLAPIENTRY
2642 _mesa_TexSubImage3D( GLenum target, GLint level,
2643 GLint xoffset, GLint yoffset, GLint zoffset,
2644 GLsizei width, GLsizei height, GLsizei depth,
2645 GLenum format, GLenum type,
2646 const GLvoid *pixels )
2647 {
2648 struct gl_texture_object *texObj;
2649 struct gl_texture_image *texImage;
2650 GET_CURRENT_CONTEXT(ctx);
2651 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2652
2653 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
2654 _mesa_debug(ctx, "glTexSubImage3D %s %d %d %d %d %d %d %d %s %s %p\n",
2655 _mesa_lookup_enum_by_nr(target), level,
2656 xoffset, yoffset, zoffset, width, height, depth,
2657 _mesa_lookup_enum_by_nr(format),
2658 _mesa_lookup_enum_by_nr(type), pixels);
2659
2660 if (ctx->NewState & _MESA_NEW_TRANSFER_STATE)
2661 _mesa_update_state(ctx);
2662
2663 if (subtexture_error_check(ctx, 3, target, level, xoffset, yoffset, zoffset,
2664 width, height, depth, format, type)) {
2665 return; /* error was detected */
2666 }
2667
2668 texObj = get_current_tex_object(ctx, target);
2669
2670 _mesa_lock_texture(ctx, texObj);
2671 {
2672 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
2673
2674 if (subtexture_error_check2(ctx, 3, target, level,
2675 xoffset, yoffset, zoffset,
2676 width, height, depth,
2677 format, type, texImage)) {
2678 /* error was recorded */
2679 }
2680 else if (width > 0 && height > 0 && height > 0) {
2681 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2682 xoffset += texImage->Border;
2683 yoffset += texImage->Border;
2684 zoffset += texImage->Border;
2685
2686 ASSERT(ctx->Driver.TexSubImage3D);
2687 ctx->Driver.TexSubImage3D(ctx, target, level,
2688 xoffset, yoffset, zoffset,
2689 width, height, depth,
2690 format, type, pixels,
2691 &ctx->Unpack, texObj, texImage );
2692
2693 check_gen_mipmap(ctx, target, texObj, level);
2694
2695 ctx->NewState |= _NEW_TEXTURE;
2696 }
2697 }
2698 _mesa_unlock_texture(ctx, texObj);
2699 }
2700
2701
2702
2703 void GLAPIENTRY
2704 _mesa_CopyTexImage1D( GLenum target, GLint level,
2705 GLenum internalFormat,
2706 GLint x, GLint y,
2707 GLsizei width, GLint border )
2708 {
2709 struct gl_texture_object *texObj;
2710 struct gl_texture_image *texImage;
2711 GLsizei postConvWidth = width;
2712 const GLuint face = _mesa_tex_target_to_face(target);
2713 GET_CURRENT_CONTEXT(ctx);
2714 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2715
2716 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
2717 _mesa_debug(ctx, "glCopyTexImage1D %s %d %s %d %d %d %d\n",
2718 _mesa_lookup_enum_by_nr(target), level,
2719 _mesa_lookup_enum_by_nr(internalFormat),
2720 x, y, width, border);
2721
2722 if (ctx->NewState & NEW_COPY_TEX_STATE)
2723 _mesa_update_state(ctx);
2724
2725 #if FEATURE_convolve
2726 if (_mesa_is_color_format(internalFormat)) {
2727 _mesa_adjust_image_for_convolution(ctx, 1, &postConvWidth, NULL);
2728 }
2729 #endif
2730
2731 if (copytexture_error_check(ctx, 1, target, level, internalFormat,
2732 postConvWidth, 1, border))
2733 return;
2734
2735 texObj = get_current_tex_object(ctx, target);
2736
2737 _mesa_lock_texture(ctx, texObj);
2738 {
2739 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
2740 if (!texImage) {
2741 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexImage1D");
2742 }
2743 else {
2744 if (texImage->Data) {
2745 ctx->Driver.FreeTexImageData( ctx, texImage );
2746 }
2747
2748 ASSERT(texImage->Data == NULL);
2749
2750 clear_teximage_fields(texImage); /* not really needed, but helpful */
2751 _mesa_init_teximage_fields(ctx, target, texImage, postConvWidth, 1, 1,
2752 border, internalFormat);
2753
2754 /* Choose actual texture format */
2755 texImage->TexFormat =
2756 ctx->Driver.ChooseTextureFormat(ctx, internalFormat,
2757 GL_NONE, GL_NONE);
2758 ASSERT(texImage->TexFormat != MESA_FORMAT_NONE);
2759
2760 ASSERT(ctx->Driver.CopyTexImage1D);
2761 ctx->Driver.CopyTexImage1D(ctx, target, level, internalFormat,
2762 x, y, width, border);
2763
2764 ASSERT(texImage->TexFormat);
2765
2766 _mesa_set_fetch_functions(texImage, 1);
2767
2768 check_gen_mipmap(ctx, target, texObj, level);
2769
2770 update_fbo_texture(ctx, texObj, face, level);
2771
2772 /* state update */
2773 texObj->_Complete = GL_FALSE;
2774 ctx->NewState |= _NEW_TEXTURE;
2775 }
2776 }
2777 _mesa_unlock_texture(ctx, texObj);
2778 }
2779
2780
2781
2782 void GLAPIENTRY
2783 _mesa_CopyTexImage2D( GLenum target, GLint level, GLenum internalFormat,
2784 GLint x, GLint y, GLsizei width, GLsizei height,
2785 GLint border )
2786 {
2787 struct gl_texture_object *texObj;
2788 struct gl_texture_image *texImage;
2789 GLsizei postConvWidth = width, postConvHeight = height;
2790 const GLuint face = _mesa_tex_target_to_face(target);
2791 GET_CURRENT_CONTEXT(ctx);
2792 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2793
2794 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
2795 _mesa_debug(ctx, "glCopyTexImage2D %s %d %s %d %d %d %d %d\n",
2796 _mesa_lookup_enum_by_nr(target), level,
2797 _mesa_lookup_enum_by_nr(internalFormat),
2798 x, y, width, height, border);
2799
2800 if (ctx->NewState & NEW_COPY_TEX_STATE)
2801 _mesa_update_state(ctx);
2802
2803 #if FEATURE_convolve
2804 if (_mesa_is_color_format(internalFormat)) {
2805 _mesa_adjust_image_for_convolution(ctx, 2, &postConvWidth,
2806 &postConvHeight);
2807 }
2808 #endif
2809
2810 if (copytexture_error_check(ctx, 2, target, level, internalFormat,
2811 postConvWidth, postConvHeight, border))
2812 return;
2813
2814 texObj = get_current_tex_object(ctx, target);
2815
2816 _mesa_lock_texture(ctx, texObj);
2817 {
2818 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
2819
2820 if (!texImage) {
2821 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexImage2D");
2822 }
2823 else {
2824 if (texImage->Data) {
2825 ctx->Driver.FreeTexImageData( ctx, texImage );
2826 }
2827
2828 ASSERT(texImage->Data == NULL);
2829
2830 clear_teximage_fields(texImage); /* not really needed, but helpful */
2831 _mesa_init_teximage_fields(ctx, target, texImage,
2832 postConvWidth, postConvHeight, 1,
2833 border, internalFormat);
2834
2835 /* Choose actual texture format */
2836 texImage->TexFormat =
2837 ctx->Driver.ChooseTextureFormat(ctx, internalFormat,
2838 GL_NONE, GL_NONE);
2839 ASSERT(texImage->TexFormat != MESA_FORMAT_NONE);
2840
2841 ASSERT(ctx->Driver.CopyTexImage2D);
2842 ctx->Driver.CopyTexImage2D(ctx, target, level, internalFormat,
2843 x, y, width, height, border);
2844
2845 ASSERT(texImage->TexFormat);
2846
2847 _mesa_set_fetch_functions(texImage, 2);
2848
2849 check_gen_mipmap(ctx, target, texObj, level);
2850
2851 update_fbo_texture(ctx, texObj, face, level);
2852
2853 /* state update */
2854 texObj->_Complete = GL_FALSE;
2855 ctx->NewState |= _NEW_TEXTURE;
2856 }
2857 }
2858 _mesa_unlock_texture(ctx, texObj);
2859 }
2860
2861
2862 void GLAPIENTRY
2863 _mesa_CopyTexSubImage1D( GLenum target, GLint level,
2864 GLint xoffset, GLint x, GLint y, GLsizei width )
2865 {
2866 struct gl_texture_object *texObj;
2867 struct gl_texture_image *texImage;
2868 GLsizei postConvWidth = width;
2869 GLint yoffset = 0;
2870 GLsizei height = 1;
2871
2872 GET_CURRENT_CONTEXT(ctx);
2873 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2874
2875 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
2876 _mesa_debug(ctx, "glCopyTexSubImage1D %s %d %d %d %d %d\n",
2877 _mesa_lookup_enum_by_nr(target),
2878 level, xoffset, x, y, width);
2879
2880 if (ctx->NewState & NEW_COPY_TEX_STATE)
2881 _mesa_update_state(ctx);
2882
2883 if (copytexsubimage_error_check1(ctx, 1, target, level))
2884 return;
2885
2886 texObj = get_current_tex_object(ctx, target);
2887
2888 _mesa_lock_texture(ctx, texObj);
2889 {
2890 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
2891
2892 #if FEATURE_convolve
2893 if (texImage && _mesa_is_color_format(texImage->InternalFormat)) {
2894 _mesa_adjust_image_for_convolution(ctx, 1, &postConvWidth, NULL);
2895 }
2896 #endif
2897
2898 if (copytexsubimage_error_check2(ctx, 1, target, level,
2899 xoffset, 0, 0, postConvWidth, 1,
2900 texImage)) {
2901 /* error was recorded */
2902 }
2903 else {
2904 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2905 xoffset += texImage->Border;
2906
2907 if (_mesa_clip_copytexsubimage(ctx, &xoffset, &yoffset, &x, &y,
2908 &width, &height)) {
2909 ASSERT(ctx->Driver.CopyTexSubImage1D);
2910 ctx->Driver.CopyTexSubImage1D(ctx, target, level,
2911 xoffset, x, y, width);
2912
2913 check_gen_mipmap(ctx, target, texObj, level);
2914
2915 ctx->NewState |= _NEW_TEXTURE;
2916 }
2917 }
2918 }
2919 _mesa_unlock_texture(ctx, texObj);
2920 }
2921
2922
2923
2924 void GLAPIENTRY
2925 _mesa_CopyTexSubImage2D( GLenum target, GLint level,
2926 GLint xoffset, GLint yoffset,
2927 GLint x, GLint y, GLsizei width, GLsizei height )
2928 {
2929 struct gl_texture_object *texObj;
2930 struct gl_texture_image *texImage;
2931 GLsizei postConvWidth = width, postConvHeight = height;
2932 GET_CURRENT_CONTEXT(ctx);
2933 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2934
2935 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
2936 _mesa_debug(ctx, "glCopyTexSubImage2D %s %d %d %d %d %d %d %d\n",
2937 _mesa_lookup_enum_by_nr(target),
2938 level, xoffset, yoffset, x, y, width, height);
2939
2940 if (ctx->NewState & NEW_COPY_TEX_STATE)
2941 _mesa_update_state(ctx);
2942
2943 if (copytexsubimage_error_check1(ctx, 2, target, level))
2944 return;
2945
2946 texObj = get_current_tex_object(ctx, target);
2947
2948 _mesa_lock_texture(ctx, texObj);
2949 {
2950 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
2951
2952 #if FEATURE_convolve
2953 if (texImage && _mesa_is_color_format(texImage->InternalFormat)) {
2954 _mesa_adjust_image_for_convolution(ctx, 2,
2955 &postConvWidth, &postConvHeight);
2956 }
2957 #endif
2958
2959 if (copytexsubimage_error_check2(ctx, 2, target, level,
2960 xoffset, yoffset, 0,
2961 postConvWidth, postConvHeight,
2962 texImage)) {
2963 /* error was recorded */
2964 }
2965 else {
2966 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2967 xoffset += texImage->Border;
2968 yoffset += texImage->Border;
2969
2970 if (_mesa_clip_copytexsubimage(ctx, &xoffset, &yoffset, &x, &y,
2971 &width, &height)) {
2972 ASSERT(ctx->Driver.CopyTexSubImage2D);
2973 ctx->Driver.CopyTexSubImage2D(ctx, target, level, xoffset, yoffset,
2974 x, y, width, height);
2975
2976 check_gen_mipmap(ctx, target, texObj, level);
2977
2978 ctx->NewState |= _NEW_TEXTURE;
2979 }
2980 }
2981 }
2982 _mesa_unlock_texture(ctx, texObj);
2983 }
2984
2985
2986
2987 void GLAPIENTRY
2988 _mesa_CopyTexSubImage3D( GLenum target, GLint level,
2989 GLint xoffset, GLint yoffset, GLint zoffset,
2990 GLint x, GLint y, GLsizei width, GLsizei height )
2991 {
2992 struct gl_texture_object *texObj;
2993 struct gl_texture_image *texImage;
2994 GLsizei postConvWidth = width, postConvHeight = height;
2995 GET_CURRENT_CONTEXT(ctx);
2996 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2997
2998 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
2999 _mesa_debug(ctx, "glCopyTexSubImage3D %s %d %d %d %d %d %d %d %d\n",
3000 _mesa_lookup_enum_by_nr(target),
3001 level, xoffset, yoffset, zoffset, x, y, width, height);
3002
3003 if (ctx->NewState & NEW_COPY_TEX_STATE)
3004 _mesa_update_state(ctx);
3005
3006 if (copytexsubimage_error_check1(ctx, 3, target, level))
3007 return;
3008
3009 texObj = get_current_tex_object(ctx, target);
3010
3011 _mesa_lock_texture(ctx, texObj);
3012 {
3013 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
3014
3015 #if FEATURE_convolve
3016 if (texImage && _mesa_is_color_format(texImage->InternalFormat)) {
3017 _mesa_adjust_image_for_convolution(ctx, 2,
3018 &postConvWidth, &postConvHeight);
3019 }
3020 #endif
3021
3022 if (copytexsubimage_error_check2(ctx, 3, target, level, xoffset, yoffset,
3023 zoffset, postConvWidth, postConvHeight,
3024 texImage)) {
3025 /* error was recored */
3026 }
3027 else {
3028 /* If we have a border, xoffset=-1 is legal. Bias by border width */
3029 xoffset += texImage->Border;
3030 yoffset += texImage->Border;
3031 zoffset += texImage->Border;
3032
3033 if (_mesa_clip_copytexsubimage(ctx, &xoffset, &yoffset, &x, &y,
3034 &width, &height)) {
3035 ASSERT(ctx->Driver.CopyTexSubImage3D);
3036 ctx->Driver.CopyTexSubImage3D(ctx, target, level,
3037 xoffset, yoffset, zoffset,
3038 x, y, width, height);
3039
3040 check_gen_mipmap(ctx, target, texObj, level);
3041
3042 ctx->NewState |= _NEW_TEXTURE;
3043 }
3044 }
3045 }
3046 _mesa_unlock_texture(ctx, texObj);
3047 }
3048
3049
3050
3051
3052 /**********************************************************************/
3053 /****** Compressed Textures ******/
3054 /**********************************************************************/
3055
3056
3057 /**
3058 * Return expected size of a compressed texture.
3059 */
3060 static GLuint
3061 compressed_tex_size(GLsizei width, GLsizei height, GLsizei depth,
3062 GLenum glformat)
3063 {
3064 gl_format mesaFormat = _mesa_glenum_to_compressed_format(glformat);
3065 return _mesa_format_image_size(mesaFormat, width, height, depth);
3066 }
3067
3068
3069 /*
3070 * Return compressed texture block size, in pixels.
3071 */
3072 static void
3073 get_compressed_block_size(GLenum glformat, GLuint *bw, GLuint *bh)
3074 {
3075 gl_format mesaFormat = _mesa_glenum_to_compressed_format(glformat);
3076 _mesa_get_format_block_size(mesaFormat, bw, bh);
3077 }
3078
3079
3080 /**
3081 * Error checking for glCompressedTexImage[123]D().
3082 * \return error code or GL_NO_ERROR.
3083 */
3084 static GLenum
3085 compressed_texture_error_check(GLcontext *ctx, GLint dimensions,
3086 GLenum target, GLint level,
3087 GLenum internalFormat, GLsizei width,
3088 GLsizei height, GLsizei depth, GLint border,
3089 GLsizei imageSize)
3090 {
3091 GLint expectedSize, maxLevels = 0, maxTextureSize;
3092
3093 if (dimensions == 1) {
3094 /* 1D compressed textures not allowed */
3095 return GL_INVALID_ENUM;
3096 }
3097 else if (dimensions == 2) {
3098 if (target == GL_PROXY_TEXTURE_2D) {
3099 maxLevels = ctx->Const.MaxTextureLevels;
3100 }
3101 else if (target == GL_TEXTURE_2D) {
3102 maxLevels = ctx->Const.MaxTextureLevels;
3103 }
3104 else if (target == GL_PROXY_TEXTURE_CUBE_MAP_ARB) {
3105 if (!ctx->Extensions.ARB_texture_cube_map)
3106 return GL_INVALID_ENUM; /*target*/
3107 maxLevels = ctx->Const.MaxCubeTextureLevels;
3108 }
3109 else if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
3110 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB) {
3111 if (!ctx->Extensions.ARB_texture_cube_map)
3112 return GL_INVALID_ENUM; /*target*/
3113 maxLevels = ctx->Const.MaxCubeTextureLevels;
3114 }
3115 else {
3116 return GL_INVALID_ENUM; /*target*/
3117 }
3118 }
3119 else if (dimensions == 3) {
3120 /* 3D compressed textures not allowed */
3121 return GL_INVALID_ENUM;
3122 }
3123
3124 maxTextureSize = 1 << (maxLevels - 1);
3125
3126 /* This will detect any invalid internalFormat value */
3127 if (!is_compressed_format(ctx, internalFormat))
3128 return GL_INVALID_ENUM;
3129
3130 /* This should really never fail */
3131 if (_mesa_base_tex_format(ctx, internalFormat) < 0)
3132 return GL_INVALID_ENUM;
3133
3134 if (border != 0)
3135 return GL_INVALID_VALUE;
3136
3137 /*
3138 * XXX We should probably use the proxy texture error check function here.
3139 */
3140 if (width < 1 || width > maxTextureSize ||
3141 (!ctx->Extensions.ARB_texture_non_power_of_two && !_mesa_is_pow_two(width)))
3142 return GL_INVALID_VALUE;
3143
3144 if ((height < 1 || height > maxTextureSize ||
3145 (!ctx->Extensions.ARB_texture_non_power_of_two && !_mesa_is_pow_two(height)))
3146 && dimensions > 1)
3147 return GL_INVALID_VALUE;
3148
3149 if ((depth < 1 || depth > maxTextureSize ||
3150 (!ctx->Extensions.ARB_texture_non_power_of_two && !_mesa_is_pow_two(depth)))
3151 && dimensions > 2)
3152 return GL_INVALID_VALUE;
3153
3154 /* For cube map, width must equal height */
3155 if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
3156 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB && width != height)
3157 return GL_INVALID_VALUE;
3158
3159 if (level < 0 || level >= maxLevels)
3160 return GL_INVALID_VALUE;
3161
3162 expectedSize = compressed_tex_size(width, height, depth, internalFormat);
3163 if (expectedSize != imageSize)
3164 return GL_INVALID_VALUE;
3165
3166 #if FEATURE_EXT_texture_sRGB
3167 if ((internalFormat == GL_COMPRESSED_SRGB_S3TC_DXT1_EXT ||
3168 internalFormat == GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT ||
3169 internalFormat == GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT ||
3170 internalFormat == GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT)
3171 && border != 0) {
3172 return GL_INVALID_OPERATION;
3173 }
3174 #endif
3175
3176 return GL_NO_ERROR;
3177 }
3178
3179
3180 /**
3181 * Error checking for glCompressedTexSubImage[123]D().
3182 * \warning There are some bad assumptions here about the size of compressed
3183 * texture tiles (multiple of 4) used to test the validity of the
3184 * offset and size parameters.
3185 * \return error code or GL_NO_ERROR.
3186 */
3187 static GLenum
3188 compressed_subtexture_error_check(GLcontext *ctx, GLint dimensions,
3189 GLenum target, GLint level,
3190 GLint xoffset, GLint yoffset, GLint zoffset,
3191 GLsizei width, GLsizei height, GLsizei depth,
3192 GLenum format, GLsizei imageSize)
3193 {
3194 GLint expectedSize, maxLevels = 0, maxTextureSize;
3195 GLuint bw, bh;
3196 (void) zoffset;
3197
3198 if (dimensions == 1) {
3199 /* 1D compressed textures not allowed */
3200 return GL_INVALID_ENUM;
3201 }
3202 else if (dimensions == 2) {
3203 if (target == GL_PROXY_TEXTURE_2D) {
3204 maxLevels = ctx->Const.MaxTextureLevels;
3205 }
3206 else if (target == GL_TEXTURE_2D) {
3207 maxLevels = ctx->Const.MaxTextureLevels;
3208 }
3209 else if (target == GL_PROXY_TEXTURE_CUBE_MAP_ARB) {
3210 if (!ctx->Extensions.ARB_texture_cube_map)
3211 return GL_INVALID_ENUM; /*target*/
3212 maxLevels = ctx->Const.MaxCubeTextureLevels;
3213 }
3214 else if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
3215 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB) {
3216 if (!ctx->Extensions.ARB_texture_cube_map)
3217 return GL_INVALID_ENUM; /*target*/
3218 maxLevels = ctx->Const.MaxCubeTextureLevels;
3219 }
3220 else {
3221 return GL_INVALID_ENUM; /*target*/
3222 }
3223 }
3224 else if (dimensions == 3) {
3225 /* 3D compressed textures not allowed */
3226 return GL_INVALID_ENUM;
3227 }
3228
3229 maxTextureSize = 1 << (maxLevels - 1);
3230
3231 /* this will catch any invalid compressed format token */
3232 if (!is_compressed_format(ctx, format))
3233 return GL_INVALID_ENUM;
3234
3235 if (width < 1 || width > maxTextureSize)
3236 return GL_INVALID_VALUE;
3237
3238 if ((height < 1 || height > maxTextureSize)
3239 && dimensions > 1)
3240 return GL_INVALID_VALUE;
3241
3242 if (level < 0 || level >= maxLevels)
3243 return GL_INVALID_VALUE;
3244
3245 /*
3246 * do checks which depend on compression block size
3247 */
3248 get_compressed_block_size(format, &bw, &bh);
3249
3250 if ((xoffset % bw != 0) || (yoffset % bh != 0))
3251 return GL_INVALID_VALUE;
3252
3253 if ((width % bw != 0) && width != 2 && width != 1)
3254 return GL_INVALID_VALUE;
3255
3256 if ((height % bh != 0) && height != 2 && height != 1)
3257 return GL_INVALID_VALUE;
3258
3259 expectedSize = compressed_tex_size(width, height, depth, format);
3260 if (expectedSize != imageSize)
3261 return GL_INVALID_VALUE;
3262
3263 return GL_NO_ERROR;
3264 }
3265
3266
3267 /**
3268 * Do second part of glCompressedTexSubImage error checking.
3269 * \return GL_TRUE if error found, GL_FALSE otherwise.
3270 */
3271 static GLboolean
3272 compressed_subtexture_error_check2(GLcontext *ctx, GLuint dims,
3273 GLsizei width, GLsizei height,
3274 GLsizei depth, GLenum format,
3275 struct gl_texture_image *texImage)
3276 {
3277
3278 if ((GLint) format != texImage->InternalFormat) {
3279 _mesa_error(ctx, GL_INVALID_OPERATION,
3280 "glCompressedTexSubImage%uD(format=0x%x)", dims, format);
3281 return GL_TRUE;
3282 }
3283
3284 if (((width == 1 || width == 2) &&
3285 (GLuint) width != texImage->Width) ||
3286 (width > texImage->Width)) {
3287 _mesa_error(ctx, GL_INVALID_VALUE,
3288 "glCompressedTexSubImage%uD(width=%d)", dims, width);
3289 return GL_TRUE;
3290 }
3291
3292 if (dims >= 2) {
3293 if (((height == 1 || height == 2) &&
3294 (GLuint) height != texImage->Height) ||
3295 (height > texImage->Height)) {
3296 _mesa_error(ctx, GL_INVALID_VALUE,
3297 "glCompressedTexSubImage%uD(height=%d)", dims, height);
3298 return GL_TRUE;
3299 }
3300 }
3301
3302 if (dims >= 3) {
3303 if (((depth == 1 || depth == 2) &&
3304 (GLuint) depth != texImage->Depth) ||
3305 (depth > texImage->Depth)) {
3306 _mesa_error(ctx, GL_INVALID_VALUE,
3307 "glCompressedTexSubImage%uD(depth=%d)", dims, depth);
3308 return GL_TRUE;
3309 }
3310 }
3311
3312 return GL_FALSE;
3313 }
3314
3315
3316
3317 void GLAPIENTRY
3318 _mesa_CompressedTexImage1DARB(GLenum target, GLint level,
3319 GLenum internalFormat, GLsizei width,
3320 GLint border, GLsizei imageSize,
3321 const GLvoid *data)
3322 {
3323 GET_CURRENT_CONTEXT(ctx);
3324 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3325
3326 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
3327 _mesa_debug(ctx, "glCompressedTexImage1DARB %s %d %s %d %d %d %p\n",
3328 _mesa_lookup_enum_by_nr(target), level,
3329 _mesa_lookup_enum_by_nr(internalFormat),
3330 width, border, imageSize, data);
3331
3332 if (target == GL_TEXTURE_1D) {
3333 /* non-proxy target */
3334 struct gl_texture_object *texObj;
3335 struct gl_texture_image *texImage;
3336 GLenum error = compressed_texture_error_check(ctx, 1, target, level,
3337 internalFormat, width, 1, 1, border, imageSize);
3338 if (error) {
3339 _mesa_error(ctx, error, "glCompressedTexImage1D");
3340 return;
3341 }
3342
3343 texObj = get_current_tex_object(ctx, target);
3344
3345 _mesa_lock_texture(ctx, texObj);
3346 {
3347 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
3348 if (!texImage) {
3349 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCompressedTexImage1D");
3350 }
3351 else {
3352 if (texImage->Data) {
3353 ctx->Driver.FreeTexImageData( ctx, texImage );
3354 }
3355 ASSERT(texImage->Data == NULL);
3356
3357 _mesa_init_teximage_fields(ctx, target, texImage, width, 1, 1,
3358 border, internalFormat);
3359
3360 /* Choose actual texture format */
3361 texImage->TexFormat =
3362 ctx->Driver.ChooseTextureFormat(ctx, internalFormat,
3363 GL_NONE, GL_NONE);
3364 ASSERT(texImage->TexFormat != MESA_FORMAT_NONE);
3365
3366 ASSERT(ctx->Driver.CompressedTexImage1D);
3367 ctx->Driver.CompressedTexImage1D(ctx, target, level,
3368 internalFormat, width, border,
3369 imageSize, data,
3370 texObj, texImage);
3371
3372 _mesa_set_fetch_functions(texImage, 1);
3373
3374 check_gen_mipmap(ctx, target, texObj, level);
3375
3376 /* state update */
3377 texObj->_Complete = GL_FALSE;
3378 ctx->NewState |= _NEW_TEXTURE;
3379 }
3380 }
3381 _mesa_unlock_texture(ctx, texObj);
3382 }
3383 else if (target == GL_PROXY_TEXTURE_1D) {
3384 /* Proxy texture: check for errors and update proxy state */
3385 GLenum error = compressed_texture_error_check(ctx, 1, target, level,
3386 internalFormat, width, 1, 1, border, imageSize);
3387 if (!error) {
3388 ASSERT(ctx->Driver.TestProxyTexImage);
3389 error = !(*ctx->Driver.TestProxyTexImage)(ctx, target, level,
3390 internalFormat, GL_NONE, GL_NONE,
3391 width, 1, 1, border);
3392 }
3393 if (error) {
3394 /* if error, clear all proxy texture image parameters */
3395 struct gl_texture_image *texImage;
3396 texImage = _mesa_get_proxy_tex_image(ctx, target, level);
3397 if (texImage)
3398 clear_teximage_fields(texImage);
3399 }
3400 else {
3401 /* store the teximage parameters */
3402 struct gl_texture_object *texObj;
3403 struct gl_texture_image *texImage;
3404
3405 texObj = get_current_tex_object(ctx, target);
3406
3407 _mesa_lock_texture(ctx, texObj);
3408 {
3409 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
3410 _mesa_init_teximage_fields(ctx, target, texImage, width, 1, 1,
3411 border, internalFormat);
3412 }
3413 _mesa_unlock_texture(ctx, texObj);
3414 }
3415 }
3416 else {
3417 _mesa_error(ctx, GL_INVALID_ENUM, "glCompressedTexImage1D(target)");
3418 return;
3419 }
3420 }
3421
3422
3423 void GLAPIENTRY
3424 _mesa_CompressedTexImage2DARB(GLenum target, GLint level,
3425 GLenum internalFormat, GLsizei width,
3426 GLsizei height, GLint border, GLsizei imageSize,
3427 const GLvoid *data)
3428 {
3429 GET_CURRENT_CONTEXT(ctx);
3430 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3431
3432 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
3433 _mesa_debug(ctx, "glCompressedTexImage2DARB %s %d %s %d %d %d %d %p\n",
3434 _mesa_lookup_enum_by_nr(target), level,
3435 _mesa_lookup_enum_by_nr(internalFormat),
3436 width, height, border, imageSize, data);
3437
3438 if (target == GL_TEXTURE_2D ||
3439 (ctx->Extensions.ARB_texture_cube_map &&
3440 target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
3441 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB)) {
3442 /* non-proxy target */
3443 struct gl_texture_object *texObj;
3444 struct gl_texture_image *texImage;
3445
3446 GLenum error = compressed_texture_error_check(ctx, 2, target, level,
3447 internalFormat, width, height, 1, border, imageSize);
3448 if (error) {
3449 _mesa_error(ctx, error, "glCompressedTexImage2D");
3450 return;
3451 }
3452
3453 texObj = get_current_tex_object(ctx, target);
3454
3455 _mesa_lock_texture(ctx, texObj);
3456 {
3457 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
3458 if (!texImage) {
3459 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCompressedTexImage2D");
3460 }
3461 else {
3462 if (texImage->Data) {
3463 ctx->Driver.FreeTexImageData( ctx, texImage );
3464 }
3465 ASSERT(texImage->Data == NULL);
3466
3467 _mesa_init_teximage_fields(ctx, target, texImage, width, height, 1,
3468 border, internalFormat);
3469
3470 /* Choose actual texture format */
3471 texImage->TexFormat =
3472 ctx->Driver.ChooseTextureFormat(ctx, internalFormat,
3473 GL_NONE, GL_NONE);
3474 ASSERT(texImage->TexFormat != MESA_FORMAT_NONE);
3475
3476 ASSERT(ctx->Driver.CompressedTexImage2D);
3477 ctx->Driver.CompressedTexImage2D(ctx, target, level,
3478 internalFormat, width, height,
3479 border, imageSize, data,
3480 texObj, texImage);
3481
3482 _mesa_set_fetch_functions(texImage, 2);
3483
3484 check_gen_mipmap(ctx, target, texObj, level);
3485
3486 /* state update */
3487 texObj->_Complete = GL_FALSE;
3488 ctx->NewState |= _NEW_TEXTURE;
3489 }
3490 }
3491 _mesa_unlock_texture(ctx, texObj);
3492 }
3493 else if (target == GL_PROXY_TEXTURE_2D ||
3494 (target == GL_PROXY_TEXTURE_CUBE_MAP_ARB &&
3495 ctx->Extensions.ARB_texture_cube_map)) {
3496 /* Proxy texture: check for errors and update proxy state */
3497 GLenum error = compressed_texture_error_check(ctx, 2, target, level,
3498 internalFormat, width, height, 1, border, imageSize);
3499 if (!error) {
3500 ASSERT(ctx->Driver.TestProxyTexImage);
3501 error = !(*ctx->Driver.TestProxyTexImage)(ctx, target, level,
3502 internalFormat, GL_NONE, GL_NONE,
3503 width, height, 1, border);
3504 }
3505 if (error) {
3506 /* if error, clear all proxy texture image parameters */
3507 struct gl_texture_image *texImage;
3508 texImage = _mesa_get_proxy_tex_image(ctx, target, level);
3509 if (texImage)
3510 clear_teximage_fields(texImage);
3511 }
3512 else {
3513 /* store the teximage parameters */
3514 struct gl_texture_object *texObj;
3515 struct gl_texture_image *texImage;
3516
3517 texObj = get_current_tex_object(ctx, target);
3518
3519 _mesa_lock_texture(ctx, texObj);
3520 {
3521 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
3522 _mesa_init_teximage_fields(ctx, target, texImage, width, height, 1,
3523 border, internalFormat);
3524 }
3525 _mesa_unlock_texture(ctx, texObj);
3526 }
3527 }
3528 else {
3529 _mesa_error(ctx, GL_INVALID_ENUM, "glCompressedTexImage2D(target)");
3530 return;
3531 }
3532 }
3533
3534
3535 void GLAPIENTRY
3536 _mesa_CompressedTexImage3DARB(GLenum target, GLint level,
3537 GLenum internalFormat, GLsizei width,
3538 GLsizei height, GLsizei depth, GLint border,
3539 GLsizei imageSize, const GLvoid *data)
3540 {
3541 GET_CURRENT_CONTEXT(ctx);
3542 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3543
3544 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
3545 _mesa_debug(ctx, "glCompressedTexImage3DARB %s %d %s %d %d %d %d %d %p\n",
3546 _mesa_lookup_enum_by_nr(target), level,
3547 _mesa_lookup_enum_by_nr(internalFormat),
3548 width, height, depth, border, imageSize, data);
3549
3550 if (target == GL_TEXTURE_3D) {
3551 /* non-proxy target */
3552 struct gl_texture_object *texObj;
3553 struct gl_texture_image *texImage;
3554 GLenum error = compressed_texture_error_check(ctx, 3, target, level,
3555 internalFormat, width, height, depth, border, imageSize);
3556 if (error) {
3557 _mesa_error(ctx, error, "glCompressedTexImage3D");
3558 return;
3559 }
3560
3561 texObj = get_current_tex_object(ctx, target);
3562
3563 _mesa_lock_texture(ctx, texObj);
3564 {
3565 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
3566 if (!texImage) {
3567 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCompressedTexImage3D");
3568 }
3569 else {
3570 if (texImage->Data) {
3571 ctx->Driver.FreeTexImageData( ctx, texImage );
3572 }
3573 ASSERT(texImage->Data == NULL);
3574
3575 _mesa_init_teximage_fields(ctx, target, texImage,
3576 width, height, depth,
3577 border, internalFormat);
3578
3579 /* Choose actual texture format */
3580 texImage->TexFormat =
3581 ctx->Driver.ChooseTextureFormat(ctx, internalFormat,
3582 GL_NONE, GL_NONE);
3583 ASSERT(texImage->TexFormat != MESA_FORMAT_NONE);
3584
3585 ASSERT(ctx->Driver.CompressedTexImage3D);
3586 ctx->Driver.CompressedTexImage3D(ctx, target, level,
3587 internalFormat,
3588 width, height, depth,
3589 border, imageSize, data,
3590 texObj, texImage);
3591
3592 _mesa_set_fetch_functions(texImage, 3);
3593
3594 check_gen_mipmap(ctx, target, texObj, level);
3595
3596 /* state update */
3597 texObj->_Complete = GL_FALSE;
3598 ctx->NewState |= _NEW_TEXTURE;
3599 }
3600 }
3601 _mesa_unlock_texture(ctx, texObj);
3602 }
3603 else if (target == GL_PROXY_TEXTURE_3D) {
3604 /* Proxy texture: check for errors and update proxy state */
3605 GLenum error = compressed_texture_error_check(ctx, 3, target, level,
3606 internalFormat, width, height, depth, border, imageSize);
3607 if (!error) {
3608 ASSERT(ctx->Driver.TestProxyTexImage);
3609 error = !(*ctx->Driver.TestProxyTexImage)(ctx, target, level,
3610 internalFormat, GL_NONE, GL_NONE,
3611 width, height, depth, border);
3612 }
3613 if (error) {
3614 /* if error, clear all proxy texture image parameters */
3615 struct gl_texture_image *texImage;
3616 texImage = _mesa_get_proxy_tex_image(ctx, target, level);
3617 if (texImage)
3618 clear_teximage_fields(texImage);
3619 }
3620 else {
3621 /* store the teximage parameters */
3622 struct gl_texture_object *texObj;
3623 struct gl_texture_image *texImage;
3624
3625 texObj = get_current_tex_object(ctx, target);
3626
3627 _mesa_lock_texture(ctx, texObj);
3628 {
3629 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
3630 _mesa_init_teximage_fields(ctx, target, texImage, width, height,
3631 depth, border, internalFormat);
3632 }
3633 _mesa_unlock_texture(ctx, texObj);
3634 }
3635 }
3636 else {
3637 _mesa_error(ctx, GL_INVALID_ENUM, "glCompressedTexImage3D(target)");
3638 return;
3639 }
3640 }
3641
3642
3643 /**
3644 * Common helper for glCompressedTexSubImage1/2/3D().
3645 */
3646 static void
3647 compressed_tex_sub_image(GLuint dims, GLenum target, GLint level,
3648 GLint xoffset, GLint yoffset, GLint zoffset,
3649 GLsizei width, GLsizei height, GLsizei depth,
3650 GLenum format, GLsizei imageSize, const GLvoid *data)
3651 {
3652 struct gl_texture_object *texObj;
3653 struct gl_texture_image *texImage;
3654 GLenum error;
3655 GET_CURRENT_CONTEXT(ctx);
3656 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3657
3658 error = compressed_subtexture_error_check(ctx, dims, target, level,
3659 xoffset, 0, 0, /* pos */
3660 width, height, depth, /* size */
3661 format, imageSize);
3662 if (error) {
3663 _mesa_error(ctx, error, "glCompressedTexSubImage%uD", dims);
3664 return;
3665 }
3666
3667 texObj = get_current_tex_object(ctx, target);
3668
3669 _mesa_lock_texture(ctx, texObj);
3670 {
3671 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
3672 assert(texImage);
3673
3674 if (compressed_subtexture_error_check2(ctx, dims, width, height, depth,
3675 format, texImage)) {
3676 /* error was recorded */
3677 }
3678 else if (width > 0 && height > 0 && depth > 0) {
3679 switch (dims) {
3680 case 1:
3681 if (ctx->Driver.CompressedTexSubImage1D) {
3682 ctx->Driver.CompressedTexSubImage1D(ctx, target, level,
3683 xoffset, width,
3684 format, imageSize, data,
3685 texObj, texImage);
3686 }
3687 break;
3688 case 2:
3689 if (ctx->Driver.CompressedTexSubImage2D) {
3690 ctx->Driver.CompressedTexSubImage2D(ctx, target, level,
3691 xoffset, yoffset,
3692 width, height,
3693 format, imageSize, data,
3694 texObj, texImage);
3695 }
3696 break;
3697 case 3:
3698 if (ctx->Driver.CompressedTexSubImage3D) {
3699 ctx->Driver.CompressedTexSubImage3D(ctx, target, level,
3700 xoffset, yoffset, zoffset,
3701 width, height, depth,
3702 format, imageSize, data,
3703 texObj, texImage);
3704 }
3705 break;
3706 default:
3707 ;
3708 }
3709
3710 check_gen_mipmap(ctx, target, texObj, level);
3711
3712 ctx->NewState |= _NEW_TEXTURE;
3713 }
3714 }
3715 _mesa_unlock_texture(ctx, texObj);
3716 }
3717
3718
3719 void GLAPIENTRY
3720 _mesa_CompressedTexSubImage1DARB(GLenum target, GLint level, GLint xoffset,
3721 GLsizei width, GLenum format,
3722 GLsizei imageSize, const GLvoid *data)
3723 {
3724 compressed_tex_sub_image(1, target, level, xoffset, 0, 0, width, 1, 1,
3725 format, imageSize, data);
3726 }
3727
3728
3729 void GLAPIENTRY
3730 _mesa_CompressedTexSubImage2DARB(GLenum target, GLint level, GLint xoffset,
3731 GLint yoffset, GLsizei width, GLsizei height,
3732 GLenum format, GLsizei imageSize,
3733 const GLvoid *data)
3734 {
3735 compressed_tex_sub_image(2, target, level, xoffset, yoffset, 0,
3736 width, height, 1, format, imageSize, data);
3737 }
3738
3739
3740 void GLAPIENTRY
3741 _mesa_CompressedTexSubImage3DARB(GLenum target, GLint level, GLint xoffset,
3742 GLint yoffset, GLint zoffset, GLsizei width,
3743 GLsizei height, GLsizei depth, GLenum format,
3744 GLsizei imageSize, const GLvoid *data)
3745 {
3746 compressed_tex_sub_image(3, target, level, xoffset, yoffset, zoffset,
3747 width, height, depth, format, imageSize, data);
3748 }
3749
3750