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