Merge branch 'mesa_7_6_branch'
[mesa.git] / src / mesa / main / teximage.c
1 /*
2 * mesa 3-D graphics library
3 * Version: 7.6
4 *
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
6 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26
27 /**
28 * \file teximage.c
29 * Texture image-related functions.
30 */
31
32
33 #include "glheader.h"
34 #include "bufferobj.h"
35 #include "context.h"
36 #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 && level == texObj->BaseLevel) {
2039 ASSERT(ctx->Driver.GenerateMipmap);
2040 ctx->Driver.GenerateMipmap(ctx, target, texObj);
2041 }
2042 }
2043
2044
2045 /** Debug helper: override the user-requested internal format */
2046 static GLenum
2047 override_internal_format(GLenum internalFormat, GLint width, GLint height)
2048 {
2049 #if 0
2050 if (internalFormat == GL_RGBA16F_ARB ||
2051 internalFormat == GL_RGBA32F_ARB) {
2052 printf("Convert rgba float tex to int %d x %d\n", width, height);
2053 return GL_RGBA;
2054 }
2055 else if (internalFormat == GL_RGB16F_ARB ||
2056 internalFormat == GL_RGB32F_ARB) {
2057 printf("Convert rgb float tex to int %d x %d\n", width, height);
2058 return GL_RGB;
2059 }
2060 else if (internalFormat == GL_LUMINANCE_ALPHA16F_ARB ||
2061 internalFormat == GL_LUMINANCE_ALPHA32F_ARB) {
2062 printf("Convert luminance float tex to int %d x %d\n", width, height);
2063 return GL_LUMINANCE_ALPHA;
2064 }
2065 else if (internalFormat == GL_LUMINANCE16F_ARB ||
2066 internalFormat == GL_LUMINANCE32F_ARB) {
2067 printf("Convert luminance float tex to int %d x %d\n", width, height);
2068 return GL_LUMINANCE;
2069 }
2070 else if (internalFormat == GL_ALPHA16F_ARB ||
2071 internalFormat == GL_ALPHA32F_ARB) {
2072 printf("Convert luminance float tex to int %d x %d\n", width, height);
2073 return GL_ALPHA;
2074 }
2075 /*
2076 else if (internalFormat == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT) {
2077 internalFormat = GL_RGBA;
2078 }
2079 */
2080 else {
2081 return internalFormat;
2082 }
2083 #else
2084 return internalFormat;
2085 #endif
2086 }
2087
2088
2089 /*
2090 * Called from the API. Note that width includes the border.
2091 */
2092 void GLAPIENTRY
2093 _mesa_TexImage1D( GLenum target, GLint level, GLint internalFormat,
2094 GLsizei width, GLint border, GLenum format,
2095 GLenum type, const GLvoid *pixels )
2096 {
2097 GLsizei postConvWidth = width;
2098 GET_CURRENT_CONTEXT(ctx);
2099 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2100
2101 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
2102 _mesa_debug(ctx, "glTexImage1D %s %d %s %d %d %s %s %p\n",
2103 _mesa_lookup_enum_by_nr(target), level,
2104 _mesa_lookup_enum_by_nr(internalFormat), width, border,
2105 _mesa_lookup_enum_by_nr(format),
2106 _mesa_lookup_enum_by_nr(type), pixels);
2107
2108 internalFormat = override_internal_format(internalFormat, width, 1);
2109
2110 #if FEATURE_convolve
2111 if (_mesa_is_color_format(internalFormat)) {
2112 _mesa_adjust_image_for_convolution(ctx, 1, &postConvWidth, NULL);
2113 }
2114 #endif
2115
2116 if (target == GL_TEXTURE_1D) {
2117 /* non-proxy target */
2118 struct gl_texture_object *texObj;
2119 struct gl_texture_image *texImage;
2120 const GLuint face = _mesa_tex_target_to_face(target);
2121
2122 if (texture_error_check(ctx, target, level, internalFormat,
2123 format, type, 1, postConvWidth, 1, 1, border)) {
2124 return; /* error was recorded */
2125 }
2126
2127 if (ctx->NewState & _MESA_NEW_TRANSFER_STATE)
2128 _mesa_update_state(ctx);
2129
2130 texObj = _mesa_get_current_tex_object(ctx, target);
2131 _mesa_lock_texture(ctx, texObj);
2132 {
2133 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
2134 if (!texImage) {
2135 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage1D");
2136 }
2137 else {
2138 if (texImage->Data) {
2139 ctx->Driver.FreeTexImageData( ctx, texImage );
2140 }
2141
2142 ASSERT(texImage->Data == NULL);
2143
2144 clear_teximage_fields(texImage); /* not really needed, but helpful */
2145 _mesa_init_teximage_fields(ctx, target, texImage,
2146 postConvWidth, 1, 1,
2147 border, internalFormat);
2148
2149 /* Choose actual texture format */
2150 texImage->TexFormat =
2151 ctx->Driver.ChooseTextureFormat(ctx, internalFormat,
2152 format, type);
2153 ASSERT(texImage->TexFormat != MESA_FORMAT_NONE);
2154
2155 /* Give the texture to the driver. <pixels> may be null. */
2156 ASSERT(ctx->Driver.TexImage1D);
2157 ctx->Driver.TexImage1D(ctx, target, level, internalFormat,
2158 width, border, format, type, pixels,
2159 &ctx->Unpack, texObj, texImage);
2160
2161 ASSERT(texImage->TexFormat);
2162
2163 _mesa_set_fetch_functions(texImage, 1);
2164
2165 check_gen_mipmap(ctx, target, texObj, level);
2166
2167 update_fbo_texture(ctx, texObj, face, level);
2168
2169 /* state update */
2170 texObj->_Complete = GL_FALSE;
2171 ctx->NewState |= _NEW_TEXTURE;
2172 }
2173 }
2174 _mesa_unlock_texture(ctx, texObj);
2175 }
2176 else if (target == GL_PROXY_TEXTURE_1D) {
2177 /* Proxy texture: check for errors and update proxy state */
2178 struct gl_texture_image *texImage;
2179 texImage = _mesa_get_proxy_tex_image(ctx, target, level);
2180 if (texture_error_check(ctx, target, level, internalFormat,
2181 format, type, 1, postConvWidth, 1, 1, border)) {
2182 /* when error, clear all proxy texture image parameters */
2183 if (texImage)
2184 clear_teximage_fields(texImage);
2185 }
2186 else {
2187 /* no error, set the tex image parameters */
2188 ASSERT(texImage);
2189 _mesa_init_teximage_fields(ctx, target, texImage,
2190 postConvWidth, 1, 1,
2191 border, internalFormat);
2192 texImage->TexFormat =
2193 ctx->Driver.ChooseTextureFormat(ctx, internalFormat, format, type);
2194 }
2195 }
2196 else {
2197 _mesa_error( ctx, GL_INVALID_ENUM, "glTexImage1D(target)" );
2198 return;
2199 }
2200 }
2201
2202
2203 void GLAPIENTRY
2204 _mesa_TexImage2D( GLenum target, GLint level, GLint internalFormat,
2205 GLsizei width, GLsizei height, GLint border,
2206 GLenum format, GLenum type,
2207 const GLvoid *pixels )
2208 {
2209 GLsizei postConvWidth = width, postConvHeight = height;
2210 GET_CURRENT_CONTEXT(ctx);
2211 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2212
2213 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
2214 _mesa_debug(ctx, "glTexImage2D %s %d %s %d %d %d %s %s %p\n",
2215 _mesa_lookup_enum_by_nr(target), level,
2216 _mesa_lookup_enum_by_nr(internalFormat), width, height,
2217 border, _mesa_lookup_enum_by_nr(format),
2218 _mesa_lookup_enum_by_nr(type), pixels);
2219
2220 internalFormat = override_internal_format(internalFormat, width, height);
2221
2222 #if FEATURE_convolve
2223 if (_mesa_is_color_format(internalFormat)) {
2224 _mesa_adjust_image_for_convolution(ctx, 2, &postConvWidth,
2225 &postConvHeight);
2226 }
2227 #endif
2228
2229 if (target == GL_TEXTURE_2D ||
2230 (ctx->Extensions.ARB_texture_cube_map &&
2231 target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
2232 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB) ||
2233 (ctx->Extensions.NV_texture_rectangle &&
2234 target == GL_TEXTURE_RECTANGLE_NV) ||
2235 (ctx->Extensions.MESA_texture_array &&
2236 target == GL_TEXTURE_1D_ARRAY_EXT)) {
2237 /* non-proxy target */
2238 struct gl_texture_object *texObj;
2239 struct gl_texture_image *texImage;
2240 const GLuint face = _mesa_tex_target_to_face(target);
2241
2242 if (texture_error_check(ctx, target, level, internalFormat,
2243 format, type, 2, postConvWidth, postConvHeight,
2244 1, border)) {
2245 return; /* error was recorded */
2246 }
2247
2248 if (ctx->NewState & _MESA_NEW_TRANSFER_STATE)
2249 _mesa_update_state(ctx);
2250
2251 texObj = _mesa_get_current_tex_object(ctx, target);
2252 _mesa_lock_texture(ctx, texObj);
2253 {
2254 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
2255 if (!texImage) {
2256 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage2D");
2257 }
2258 else {
2259 if (texImage->Data) {
2260 ctx->Driver.FreeTexImageData( ctx, texImage );
2261 }
2262
2263 ASSERT(texImage->Data == NULL);
2264 clear_teximage_fields(texImage); /* not really needed, but helpful */
2265 _mesa_init_teximage_fields(ctx, target, texImage,
2266 postConvWidth, postConvHeight, 1,
2267 border, internalFormat);
2268
2269 /* Choose actual texture format */
2270 texImage->TexFormat =
2271 ctx->Driver.ChooseTextureFormat(ctx, internalFormat,
2272 format, type);
2273 ASSERT(texImage->TexFormat != MESA_FORMAT_NONE);
2274
2275 /* Give the texture to the driver. <pixels> may be null. */
2276 ASSERT(ctx->Driver.TexImage2D);
2277 ctx->Driver.TexImage2D(ctx, target, level, internalFormat,
2278 width, height, border, format, type,
2279 pixels, &ctx->Unpack, texObj, texImage);
2280
2281 ASSERT(texImage->TexFormat);
2282
2283 _mesa_set_fetch_functions(texImage, 2);
2284
2285 check_gen_mipmap(ctx, target, texObj, level);
2286
2287 update_fbo_texture(ctx, texObj, face, level);
2288
2289 /* state update */
2290 texObj->_Complete = GL_FALSE;
2291 ctx->NewState |= _NEW_TEXTURE;
2292 }
2293 }
2294 _mesa_unlock_texture(ctx, texObj);
2295 }
2296 else if (target == GL_PROXY_TEXTURE_2D ||
2297 (target == GL_PROXY_TEXTURE_CUBE_MAP_ARB &&
2298 ctx->Extensions.ARB_texture_cube_map) ||
2299 (target == GL_PROXY_TEXTURE_RECTANGLE_NV &&
2300 ctx->Extensions.NV_texture_rectangle) ||
2301 (ctx->Extensions.MESA_texture_array &&
2302 target == GL_PROXY_TEXTURE_1D_ARRAY_EXT)) {
2303 /* Proxy texture: check for errors and update proxy state */
2304 struct gl_texture_image *texImage;
2305 texImage = _mesa_get_proxy_tex_image(ctx, target, level);
2306 if (texture_error_check(ctx, target, level, internalFormat,
2307 format, type, 2, postConvWidth, postConvHeight,
2308 1, border)) {
2309 /* when error, clear all proxy texture image parameters */
2310 if (texImage)
2311 clear_teximage_fields(texImage);
2312 }
2313 else {
2314 /* no error, set the tex image parameters */
2315 _mesa_init_teximage_fields(ctx, target, texImage,
2316 postConvWidth, postConvHeight, 1,
2317 border, internalFormat);
2318 texImage->TexFormat =
2319 ctx->Driver.ChooseTextureFormat(ctx, internalFormat, format, type);
2320 }
2321 }
2322 else {
2323 _mesa_error( ctx, GL_INVALID_ENUM, "glTexImage2D(target)" );
2324 return;
2325 }
2326 }
2327
2328
2329 /*
2330 * Called by the API or display list executor.
2331 * Note that width and height include the border.
2332 */
2333 void GLAPIENTRY
2334 _mesa_TexImage3D( GLenum target, GLint level, GLint internalFormat,
2335 GLsizei width, GLsizei height, GLsizei depth,
2336 GLint border, GLenum format, GLenum type,
2337 const GLvoid *pixels )
2338 {
2339 GET_CURRENT_CONTEXT(ctx);
2340 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2341
2342 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
2343 _mesa_debug(ctx, "glTexImage3D %s %d %s %d %d %d %d %s %s %p\n",
2344 _mesa_lookup_enum_by_nr(target), level,
2345 _mesa_lookup_enum_by_nr(internalFormat), width, height,
2346 depth, border, _mesa_lookup_enum_by_nr(format),
2347 _mesa_lookup_enum_by_nr(type), pixels);
2348
2349 internalFormat = override_internal_format(internalFormat, width, height);
2350
2351 if (target == GL_TEXTURE_3D ||
2352 (ctx->Extensions.MESA_texture_array &&
2353 target == GL_TEXTURE_2D_ARRAY_EXT)) {
2354 /* non-proxy target */
2355 struct gl_texture_object *texObj;
2356 struct gl_texture_image *texImage;
2357 const GLuint face = _mesa_tex_target_to_face(target);
2358
2359 if (texture_error_check(ctx, target, level, (GLint) internalFormat,
2360 format, type, 3, width, height, depth, border)) {
2361 return; /* error was recorded */
2362 }
2363
2364 if (ctx->NewState & _MESA_NEW_TRANSFER_STATE)
2365 _mesa_update_state(ctx);
2366
2367 texObj = _mesa_get_current_tex_object(ctx, target);
2368 _mesa_lock_texture(ctx, texObj);
2369 {
2370 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
2371 if (!texImage) {
2372 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage3D");
2373 }
2374 else {
2375 if (texImage->Data) {
2376 ctx->Driver.FreeTexImageData( ctx, texImage );
2377 }
2378
2379 ASSERT(texImage->Data == NULL);
2380 clear_teximage_fields(texImage); /* not really needed, but helpful */
2381 _mesa_init_teximage_fields(ctx, target, texImage,
2382 width, height, depth,
2383 border, internalFormat);
2384
2385 /* Choose actual texture format */
2386 texImage->TexFormat =
2387 ctx->Driver.ChooseTextureFormat(ctx, internalFormat,
2388 format, type);
2389 ASSERT(texImage->TexFormat != MESA_FORMAT_NONE);
2390
2391 /* Give the texture to the driver. <pixels> may be null. */
2392 ASSERT(ctx->Driver.TexImage3D);
2393 ctx->Driver.TexImage3D(ctx, target, level, internalFormat,
2394 width, height, depth, border, format, type,
2395 pixels, &ctx->Unpack, texObj, texImage);
2396
2397 ASSERT(texImage->TexFormat);
2398
2399 _mesa_set_fetch_functions(texImage, 3);
2400
2401 check_gen_mipmap(ctx, target, texObj, level);
2402
2403 update_fbo_texture(ctx, texObj, face, level);
2404
2405 /* state update */
2406 texObj->_Complete = GL_FALSE;
2407 ctx->NewState |= _NEW_TEXTURE;
2408 }
2409 }
2410 _mesa_unlock_texture(ctx, texObj);
2411 }
2412 else if (target == GL_PROXY_TEXTURE_3D ||
2413 (ctx->Extensions.MESA_texture_array &&
2414 target == GL_PROXY_TEXTURE_2D_ARRAY_EXT)) {
2415 /* Proxy texture: check for errors and update proxy state */
2416 struct gl_texture_image *texImage;
2417 texImage = _mesa_get_proxy_tex_image(ctx, target, level);
2418 if (texture_error_check(ctx, target, level, internalFormat,
2419 format, type, 3, width, height, depth, border)) {
2420 /* when error, clear all proxy texture image parameters */
2421 if (texImage)
2422 clear_teximage_fields(texImage);
2423 }
2424 else {
2425 /* no error, set the tex image parameters */
2426 _mesa_init_teximage_fields(ctx, target, texImage, width, height,
2427 depth, border, internalFormat);
2428 texImage->TexFormat =
2429 ctx->Driver.ChooseTextureFormat(ctx, internalFormat, format, type);
2430 }
2431 }
2432 else {
2433 _mesa_error( ctx, GL_INVALID_ENUM, "glTexImage3D(target)" );
2434 return;
2435 }
2436 }
2437
2438
2439 void GLAPIENTRY
2440 _mesa_TexImage3DEXT( GLenum target, GLint level, GLenum internalFormat,
2441 GLsizei width, GLsizei height, GLsizei depth,
2442 GLint border, GLenum format, GLenum type,
2443 const GLvoid *pixels )
2444 {
2445 _mesa_TexImage3D(target, level, (GLint) internalFormat, width, height,
2446 depth, border, format, type, pixels);
2447 }
2448
2449
2450
2451 void GLAPIENTRY
2452 _mesa_TexSubImage1D( GLenum target, GLint level,
2453 GLint xoffset, GLsizei width,
2454 GLenum format, GLenum type,
2455 const GLvoid *pixels )
2456 {
2457 GLsizei postConvWidth = width;
2458 struct gl_texture_object *texObj;
2459 struct gl_texture_image *texImage;
2460 GET_CURRENT_CONTEXT(ctx);
2461 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2462
2463 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
2464 _mesa_debug(ctx, "glTexSubImage1D %s %d %d %d %s %s %p\n",
2465 _mesa_lookup_enum_by_nr(target), level,
2466 xoffset, width, _mesa_lookup_enum_by_nr(format),
2467 _mesa_lookup_enum_by_nr(type), pixels);
2468
2469 if (ctx->NewState & _MESA_NEW_TRANSFER_STATE)
2470 _mesa_update_state(ctx);
2471
2472 #if FEATURE_convolve
2473 /* XXX should test internal format */
2474 if (_mesa_is_color_format(format)) {
2475 _mesa_adjust_image_for_convolution(ctx, 1, &postConvWidth, NULL);
2476 }
2477 #endif
2478
2479 if (subtexture_error_check(ctx, 1, target, level, xoffset, 0, 0,
2480 postConvWidth, 1, 1, format, type)) {
2481 return; /* error was detected */
2482 }
2483
2484
2485 texObj = _mesa_get_current_tex_object(ctx, target);
2486 assert(texObj);
2487
2488 _mesa_lock_texture(ctx, texObj);
2489 {
2490 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
2491
2492 if (subtexture_error_check2(ctx, 1, target, level, xoffset, 0, 0,
2493 postConvWidth, 1, 1,
2494 format, type, texImage)) {
2495 /* error was recorded */
2496 }
2497 else if (width > 0) {
2498 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2499 xoffset += texImage->Border;
2500
2501 ASSERT(ctx->Driver.TexSubImage1D);
2502 ctx->Driver.TexSubImage1D(ctx, target, level, xoffset, width,
2503 format, type, pixels, &ctx->Unpack,
2504 texObj, texImage);
2505
2506 check_gen_mipmap(ctx, target, texObj, level);
2507
2508 ctx->NewState |= _NEW_TEXTURE;
2509 }
2510 }
2511 _mesa_unlock_texture(ctx, texObj);
2512 }
2513
2514
2515 void GLAPIENTRY
2516 _mesa_TexSubImage2D( GLenum target, GLint level,
2517 GLint xoffset, GLint yoffset,
2518 GLsizei width, GLsizei height,
2519 GLenum format, GLenum type,
2520 const GLvoid *pixels )
2521 {
2522 GLsizei postConvWidth = width, postConvHeight = height;
2523 struct gl_texture_object *texObj;
2524 struct gl_texture_image *texImage;
2525 GET_CURRENT_CONTEXT(ctx);
2526 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2527
2528 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
2529 _mesa_debug(ctx, "glTexSubImage2D %s %d %d %d %d %d %s %s %p\n",
2530 _mesa_lookup_enum_by_nr(target), level,
2531 xoffset, yoffset, width, height,
2532 _mesa_lookup_enum_by_nr(format),
2533 _mesa_lookup_enum_by_nr(type), pixels);
2534
2535 if (ctx->NewState & _MESA_NEW_TRANSFER_STATE)
2536 _mesa_update_state(ctx);
2537
2538 #if FEATURE_convolve
2539 /* XXX should test internal format */
2540 if (_mesa_is_color_format(format)) {
2541 _mesa_adjust_image_for_convolution(ctx, 2, &postConvWidth,
2542 &postConvHeight);
2543 }
2544 #endif
2545
2546 if (subtexture_error_check(ctx, 2, target, level, xoffset, yoffset, 0,
2547 postConvWidth, postConvHeight, 1, format, type)) {
2548 return; /* error was detected */
2549 }
2550
2551 texObj = _mesa_get_current_tex_object(ctx, target);
2552
2553 _mesa_lock_texture(ctx, texObj);
2554 {
2555 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
2556
2557 if (subtexture_error_check2(ctx, 2, target, level, xoffset, yoffset, 0,
2558 postConvWidth, postConvHeight, 1,
2559 format, type, texImage)) {
2560 /* error was recorded */
2561 }
2562 else if (width > 0 && height >= 0) {
2563 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2564 xoffset += texImage->Border;
2565 yoffset += texImage->Border;
2566
2567 ASSERT(ctx->Driver.TexSubImage2D);
2568 ctx->Driver.TexSubImage2D(ctx, target, level, xoffset, yoffset,
2569 width, height, format, type, pixels,
2570 &ctx->Unpack, texObj, texImage);
2571
2572 check_gen_mipmap(ctx, target, texObj, level);
2573
2574 ctx->NewState |= _NEW_TEXTURE;
2575 }
2576 }
2577 _mesa_unlock_texture(ctx, texObj);
2578 }
2579
2580
2581
2582 void GLAPIENTRY
2583 _mesa_TexSubImage3D( GLenum target, GLint level,
2584 GLint xoffset, GLint yoffset, GLint zoffset,
2585 GLsizei width, GLsizei height, GLsizei depth,
2586 GLenum format, GLenum type,
2587 const GLvoid *pixels )
2588 {
2589 struct gl_texture_object *texObj;
2590 struct gl_texture_image *texImage;
2591 GET_CURRENT_CONTEXT(ctx);
2592 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2593
2594 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
2595 _mesa_debug(ctx, "glTexSubImage3D %s %d %d %d %d %d %d %d %s %s %p\n",
2596 _mesa_lookup_enum_by_nr(target), level,
2597 xoffset, yoffset, zoffset, width, height, depth,
2598 _mesa_lookup_enum_by_nr(format),
2599 _mesa_lookup_enum_by_nr(type), pixels);
2600
2601 if (ctx->NewState & _MESA_NEW_TRANSFER_STATE)
2602 _mesa_update_state(ctx);
2603
2604 if (subtexture_error_check(ctx, 3, target, level, xoffset, yoffset, zoffset,
2605 width, height, depth, format, type)) {
2606 return; /* error was detected */
2607 }
2608
2609 texObj = _mesa_get_current_tex_object(ctx, target);
2610
2611 _mesa_lock_texture(ctx, texObj);
2612 {
2613 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
2614
2615 if (subtexture_error_check2(ctx, 3, target, level,
2616 xoffset, yoffset, zoffset,
2617 width, height, depth,
2618 format, type, texImage)) {
2619 /* error was recorded */
2620 }
2621 else if (width > 0 && height > 0 && height > 0) {
2622 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2623 xoffset += texImage->Border;
2624 yoffset += texImage->Border;
2625 zoffset += texImage->Border;
2626
2627 ASSERT(ctx->Driver.TexSubImage3D);
2628 ctx->Driver.TexSubImage3D(ctx, target, level,
2629 xoffset, yoffset, zoffset,
2630 width, height, depth,
2631 format, type, pixels,
2632 &ctx->Unpack, texObj, texImage );
2633
2634 check_gen_mipmap(ctx, target, texObj, level);
2635
2636 ctx->NewState |= _NEW_TEXTURE;
2637 }
2638 }
2639 _mesa_unlock_texture(ctx, texObj);
2640 }
2641
2642
2643
2644 void GLAPIENTRY
2645 _mesa_CopyTexImage1D( GLenum target, GLint level,
2646 GLenum internalFormat,
2647 GLint x, GLint y,
2648 GLsizei width, GLint border )
2649 {
2650 struct gl_texture_object *texObj;
2651 struct gl_texture_image *texImage;
2652 GLsizei postConvWidth = width;
2653 const GLuint face = _mesa_tex_target_to_face(target);
2654 GET_CURRENT_CONTEXT(ctx);
2655 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2656
2657 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
2658 _mesa_debug(ctx, "glCopyTexImage1D %s %d %s %d %d %d %d\n",
2659 _mesa_lookup_enum_by_nr(target), level,
2660 _mesa_lookup_enum_by_nr(internalFormat),
2661 x, y, width, border);
2662
2663 if (ctx->NewState & NEW_COPY_TEX_STATE)
2664 _mesa_update_state(ctx);
2665
2666 #if FEATURE_convolve
2667 if (_mesa_is_color_format(internalFormat)) {
2668 _mesa_adjust_image_for_convolution(ctx, 1, &postConvWidth, NULL);
2669 }
2670 #endif
2671
2672 if (copytexture_error_check(ctx, 1, target, level, internalFormat,
2673 postConvWidth, 1, border))
2674 return;
2675
2676 texObj = _mesa_get_current_tex_object(ctx, target);
2677
2678 _mesa_lock_texture(ctx, texObj);
2679 {
2680 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
2681 if (!texImage) {
2682 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexImage1D");
2683 }
2684 else {
2685 if (texImage->Data) {
2686 ctx->Driver.FreeTexImageData( ctx, texImage );
2687 }
2688
2689 ASSERT(texImage->Data == NULL);
2690
2691 clear_teximage_fields(texImage); /* not really needed, but helpful */
2692 _mesa_init_teximage_fields(ctx, target, texImage, postConvWidth, 1, 1,
2693 border, internalFormat);
2694
2695 /* Choose actual texture format */
2696 texImage->TexFormat =
2697 ctx->Driver.ChooseTextureFormat(ctx, internalFormat,
2698 GL_NONE, GL_NONE);
2699 ASSERT(texImage->TexFormat != MESA_FORMAT_NONE);
2700
2701 ASSERT(ctx->Driver.CopyTexImage1D);
2702 ctx->Driver.CopyTexImage1D(ctx, target, level, internalFormat,
2703 x, y, width, border);
2704
2705 ASSERT(texImage->TexFormat);
2706
2707 _mesa_set_fetch_functions(texImage, 1);
2708
2709 check_gen_mipmap(ctx, target, texObj, level);
2710
2711 update_fbo_texture(ctx, texObj, face, level);
2712
2713 /* state update */
2714 texObj->_Complete = GL_FALSE;
2715 ctx->NewState |= _NEW_TEXTURE;
2716 }
2717 }
2718 _mesa_unlock_texture(ctx, texObj);
2719 }
2720
2721
2722
2723 void GLAPIENTRY
2724 _mesa_CopyTexImage2D( GLenum target, GLint level, GLenum internalFormat,
2725 GLint x, GLint y, GLsizei width, GLsizei height,
2726 GLint border )
2727 {
2728 struct gl_texture_object *texObj;
2729 struct gl_texture_image *texImage;
2730 GLsizei postConvWidth = width, postConvHeight = height;
2731 const GLuint face = _mesa_tex_target_to_face(target);
2732 GET_CURRENT_CONTEXT(ctx);
2733 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2734
2735 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
2736 _mesa_debug(ctx, "glCopyTexImage2D %s %d %s %d %d %d %d %d\n",
2737 _mesa_lookup_enum_by_nr(target), level,
2738 _mesa_lookup_enum_by_nr(internalFormat),
2739 x, y, width, height, border);
2740
2741 if (ctx->NewState & NEW_COPY_TEX_STATE)
2742 _mesa_update_state(ctx);
2743
2744 #if FEATURE_convolve
2745 if (_mesa_is_color_format(internalFormat)) {
2746 _mesa_adjust_image_for_convolution(ctx, 2, &postConvWidth,
2747 &postConvHeight);
2748 }
2749 #endif
2750
2751 if (copytexture_error_check(ctx, 2, target, level, internalFormat,
2752 postConvWidth, postConvHeight, border))
2753 return;
2754
2755 texObj = _mesa_get_current_tex_object(ctx, target);
2756
2757 _mesa_lock_texture(ctx, texObj);
2758 {
2759 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
2760
2761 if (!texImage) {
2762 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexImage2D");
2763 }
2764 else {
2765 if (texImage->Data) {
2766 ctx->Driver.FreeTexImageData( ctx, texImage );
2767 }
2768
2769 ASSERT(texImage->Data == NULL);
2770
2771 clear_teximage_fields(texImage); /* not really needed, but helpful */
2772 _mesa_init_teximage_fields(ctx, target, texImage,
2773 postConvWidth, postConvHeight, 1,
2774 border, internalFormat);
2775
2776 /* Choose actual texture format */
2777 texImage->TexFormat =
2778 ctx->Driver.ChooseTextureFormat(ctx, internalFormat,
2779 GL_NONE, GL_NONE);
2780 ASSERT(texImage->TexFormat != MESA_FORMAT_NONE);
2781
2782 ASSERT(ctx->Driver.CopyTexImage2D);
2783 ctx->Driver.CopyTexImage2D(ctx, target, level, internalFormat,
2784 x, y, width, height, border);
2785
2786 ASSERT(texImage->TexFormat);
2787
2788 _mesa_set_fetch_functions(texImage, 2);
2789
2790 check_gen_mipmap(ctx, target, texObj, level);
2791
2792 update_fbo_texture(ctx, texObj, face, level);
2793
2794 /* state update */
2795 texObj->_Complete = GL_FALSE;
2796 ctx->NewState |= _NEW_TEXTURE;
2797 }
2798 }
2799 _mesa_unlock_texture(ctx, texObj);
2800 }
2801
2802
2803 void GLAPIENTRY
2804 _mesa_CopyTexSubImage1D( GLenum target, GLint level,
2805 GLint xoffset, GLint x, GLint y, GLsizei width )
2806 {
2807 struct gl_texture_object *texObj;
2808 struct gl_texture_image *texImage;
2809 GLsizei postConvWidth = width;
2810 GLint yoffset = 0;
2811 GLsizei height = 1;
2812
2813 GET_CURRENT_CONTEXT(ctx);
2814 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2815
2816 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
2817 _mesa_debug(ctx, "glCopyTexSubImage1D %s %d %d %d %d %d\n",
2818 _mesa_lookup_enum_by_nr(target),
2819 level, xoffset, x, y, width);
2820
2821 if (ctx->NewState & NEW_COPY_TEX_STATE)
2822 _mesa_update_state(ctx);
2823
2824 if (copytexsubimage_error_check1(ctx, 1, target, level))
2825 return;
2826
2827 texObj = _mesa_get_current_tex_object(ctx, target);
2828
2829 _mesa_lock_texture(ctx, texObj);
2830 {
2831 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
2832
2833 #if FEATURE_convolve
2834 if (texImage && _mesa_is_color_format(texImage->InternalFormat)) {
2835 _mesa_adjust_image_for_convolution(ctx, 1, &postConvWidth, NULL);
2836 }
2837 #endif
2838
2839 if (copytexsubimage_error_check2(ctx, 1, target, level,
2840 xoffset, 0, 0, postConvWidth, 1,
2841 texImage)) {
2842 /* error was recorded */
2843 }
2844 else {
2845 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2846 xoffset += texImage->Border;
2847
2848 if (_mesa_clip_copytexsubimage(ctx, &xoffset, &yoffset, &x, &y,
2849 &width, &height)) {
2850 ASSERT(ctx->Driver.CopyTexSubImage1D);
2851 ctx->Driver.CopyTexSubImage1D(ctx, target, level,
2852 xoffset, x, y, width);
2853
2854 check_gen_mipmap(ctx, target, texObj, level);
2855
2856 ctx->NewState |= _NEW_TEXTURE;
2857 }
2858 }
2859 }
2860 _mesa_unlock_texture(ctx, texObj);
2861 }
2862
2863
2864
2865 void GLAPIENTRY
2866 _mesa_CopyTexSubImage2D( GLenum target, GLint level,
2867 GLint xoffset, GLint yoffset,
2868 GLint x, GLint y, GLsizei width, GLsizei height )
2869 {
2870 struct gl_texture_object *texObj;
2871 struct gl_texture_image *texImage;
2872 GLsizei postConvWidth = width, postConvHeight = height;
2873 GET_CURRENT_CONTEXT(ctx);
2874 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2875
2876 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
2877 _mesa_debug(ctx, "glCopyTexSubImage2D %s %d %d %d %d %d %d %d\n",
2878 _mesa_lookup_enum_by_nr(target),
2879 level, xoffset, yoffset, x, y, width, height);
2880
2881 if (ctx->NewState & NEW_COPY_TEX_STATE)
2882 _mesa_update_state(ctx);
2883
2884 if (copytexsubimage_error_check1(ctx, 2, target, level))
2885 return;
2886
2887 texObj = _mesa_get_current_tex_object(ctx, target);
2888
2889 _mesa_lock_texture(ctx, texObj);
2890 {
2891 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
2892
2893 #if FEATURE_convolve
2894 if (texImage && _mesa_is_color_format(texImage->InternalFormat)) {
2895 _mesa_adjust_image_for_convolution(ctx, 2,
2896 &postConvWidth, &postConvHeight);
2897 }
2898 #endif
2899
2900 if (copytexsubimage_error_check2(ctx, 2, target, level,
2901 xoffset, yoffset, 0,
2902 postConvWidth, postConvHeight,
2903 texImage)) {
2904 /* error was recorded */
2905 }
2906 else {
2907 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2908 xoffset += texImage->Border;
2909 yoffset += texImage->Border;
2910
2911 if (_mesa_clip_copytexsubimage(ctx, &xoffset, &yoffset, &x, &y,
2912 &width, &height)) {
2913 ASSERT(ctx->Driver.CopyTexSubImage2D);
2914 ctx->Driver.CopyTexSubImage2D(ctx, target, level, xoffset, yoffset,
2915 x, y, width, height);
2916
2917 check_gen_mipmap(ctx, target, texObj, level);
2918
2919 ctx->NewState |= _NEW_TEXTURE;
2920 }
2921 }
2922 }
2923 _mesa_unlock_texture(ctx, texObj);
2924 }
2925
2926
2927
2928 void GLAPIENTRY
2929 _mesa_CopyTexSubImage3D( GLenum target, GLint level,
2930 GLint xoffset, GLint yoffset, GLint zoffset,
2931 GLint x, GLint y, GLsizei width, GLsizei height )
2932 {
2933 struct gl_texture_object *texObj;
2934 struct gl_texture_image *texImage;
2935 GLsizei postConvWidth = width, postConvHeight = height;
2936 GET_CURRENT_CONTEXT(ctx);
2937 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2938
2939 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
2940 _mesa_debug(ctx, "glCopyTexSubImage3D %s %d %d %d %d %d %d %d %d\n",
2941 _mesa_lookup_enum_by_nr(target),
2942 level, xoffset, yoffset, zoffset, x, y, width, height);
2943
2944 if (ctx->NewState & NEW_COPY_TEX_STATE)
2945 _mesa_update_state(ctx);
2946
2947 if (copytexsubimage_error_check1(ctx, 3, target, level))
2948 return;
2949
2950 texObj = _mesa_get_current_tex_object(ctx, target);
2951
2952 _mesa_lock_texture(ctx, texObj);
2953 {
2954 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
2955
2956 #if FEATURE_convolve
2957 if (texImage && _mesa_is_color_format(texImage->InternalFormat)) {
2958 _mesa_adjust_image_for_convolution(ctx, 2,
2959 &postConvWidth, &postConvHeight);
2960 }
2961 #endif
2962
2963 if (copytexsubimage_error_check2(ctx, 3, target, level, xoffset, yoffset,
2964 zoffset, postConvWidth, postConvHeight,
2965 texImage)) {
2966 /* error was recored */
2967 }
2968 else {
2969 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2970 xoffset += texImage->Border;
2971 yoffset += texImage->Border;
2972 zoffset += texImage->Border;
2973
2974 if (_mesa_clip_copytexsubimage(ctx, &xoffset, &yoffset, &x, &y,
2975 &width, &height)) {
2976 ASSERT(ctx->Driver.CopyTexSubImage3D);
2977 ctx->Driver.CopyTexSubImage3D(ctx, target, level,
2978 xoffset, yoffset, zoffset,
2979 x, y, width, height);
2980
2981 check_gen_mipmap(ctx, target, texObj, level);
2982
2983 ctx->NewState |= _NEW_TEXTURE;
2984 }
2985 }
2986 }
2987 _mesa_unlock_texture(ctx, texObj);
2988 }
2989
2990
2991
2992
2993 /**********************************************************************/
2994 /****** Compressed Textures ******/
2995 /**********************************************************************/
2996
2997
2998 /**
2999 * Return expected size of a compressed texture.
3000 */
3001 static GLuint
3002 compressed_tex_size(GLsizei width, GLsizei height, GLsizei depth,
3003 GLenum glformat)
3004 {
3005 gl_format mesaFormat = _mesa_glenum_to_compressed_format(glformat);
3006 return _mesa_format_image_size(mesaFormat, width, height, depth);
3007 }
3008
3009
3010 /*
3011 * Return compressed texture block size, in pixels.
3012 */
3013 static void
3014 get_compressed_block_size(GLenum glformat, GLuint *bw, GLuint *bh)
3015 {
3016 gl_format mesaFormat = _mesa_glenum_to_compressed_format(glformat);
3017 _mesa_get_format_block_size(mesaFormat, bw, bh);
3018 }
3019
3020
3021 /**
3022 * Error checking for glCompressedTexImage[123]D().
3023 * \return error code or GL_NO_ERROR.
3024 */
3025 static GLenum
3026 compressed_texture_error_check(GLcontext *ctx, GLint dimensions,
3027 GLenum target, GLint level,
3028 GLenum internalFormat, GLsizei width,
3029 GLsizei height, GLsizei depth, GLint border,
3030 GLsizei imageSize)
3031 {
3032 GLint expectedSize, maxLevels = 0, maxTextureSize;
3033
3034 if (dimensions == 1) {
3035 /* 1D compressed textures not allowed */
3036 return GL_INVALID_ENUM;
3037 }
3038 else if (dimensions == 2) {
3039 if (target == GL_PROXY_TEXTURE_2D) {
3040 maxLevels = ctx->Const.MaxTextureLevels;
3041 }
3042 else if (target == GL_TEXTURE_2D) {
3043 maxLevels = ctx->Const.MaxTextureLevels;
3044 }
3045 else if (target == GL_PROXY_TEXTURE_CUBE_MAP_ARB) {
3046 if (!ctx->Extensions.ARB_texture_cube_map)
3047 return GL_INVALID_ENUM; /*target*/
3048 maxLevels = ctx->Const.MaxCubeTextureLevels;
3049 }
3050 else if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
3051 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB) {
3052 if (!ctx->Extensions.ARB_texture_cube_map)
3053 return GL_INVALID_ENUM; /*target*/
3054 maxLevels = ctx->Const.MaxCubeTextureLevels;
3055 }
3056 else {
3057 return GL_INVALID_ENUM; /*target*/
3058 }
3059 }
3060 else if (dimensions == 3) {
3061 /* 3D compressed textures not allowed */
3062 return GL_INVALID_ENUM;
3063 }
3064
3065 maxTextureSize = 1 << (maxLevels - 1);
3066
3067 /* This will detect any invalid internalFormat value */
3068 if (!is_compressed_format(ctx, internalFormat))
3069 return GL_INVALID_ENUM;
3070
3071 /* This should really never fail */
3072 if (_mesa_base_tex_format(ctx, internalFormat) < 0)
3073 return GL_INVALID_ENUM;
3074
3075 if (border != 0)
3076 return GL_INVALID_VALUE;
3077
3078 /*
3079 * XXX We should probably use the proxy texture error check function here.
3080 */
3081 if (width < 1 || width > maxTextureSize ||
3082 (!ctx->Extensions.ARB_texture_non_power_of_two && !_mesa_is_pow_two(width)))
3083 return GL_INVALID_VALUE;
3084
3085 if ((height < 1 || height > maxTextureSize ||
3086 (!ctx->Extensions.ARB_texture_non_power_of_two && !_mesa_is_pow_two(height)))
3087 && dimensions > 1)
3088 return GL_INVALID_VALUE;
3089
3090 if ((depth < 1 || depth > maxTextureSize ||
3091 (!ctx->Extensions.ARB_texture_non_power_of_two && !_mesa_is_pow_two(depth)))
3092 && dimensions > 2)
3093 return GL_INVALID_VALUE;
3094
3095 /* For cube map, width must equal height */
3096 if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
3097 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB && width != height)
3098 return GL_INVALID_VALUE;
3099
3100 if (level < 0 || level >= maxLevels)
3101 return GL_INVALID_VALUE;
3102
3103 expectedSize = compressed_tex_size(width, height, depth, internalFormat);
3104 if (expectedSize != imageSize)
3105 return GL_INVALID_VALUE;
3106
3107 #if FEATURE_EXT_texture_sRGB
3108 if ((internalFormat == GL_COMPRESSED_SRGB_S3TC_DXT1_EXT ||
3109 internalFormat == GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT ||
3110 internalFormat == GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT ||
3111 internalFormat == GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT)
3112 && border != 0) {
3113 return GL_INVALID_OPERATION;
3114 }
3115 #endif
3116
3117 return GL_NO_ERROR;
3118 }
3119
3120
3121 /**
3122 * Error checking for glCompressedTexSubImage[123]D().
3123 * \warning There are some bad assumptions here about the size of compressed
3124 * texture tiles (multiple of 4) used to test the validity of the
3125 * offset and size parameters.
3126 * \return error code or GL_NO_ERROR.
3127 */
3128 static GLenum
3129 compressed_subtexture_error_check(GLcontext *ctx, GLint dimensions,
3130 GLenum target, GLint level,
3131 GLint xoffset, GLint yoffset, GLint zoffset,
3132 GLsizei width, GLsizei height, GLsizei depth,
3133 GLenum format, GLsizei imageSize)
3134 {
3135 GLint expectedSize, maxLevels = 0, maxTextureSize;
3136 GLuint bw, bh;
3137 (void) zoffset;
3138
3139 if (dimensions == 1) {
3140 /* 1D compressed textures not allowed */
3141 return GL_INVALID_ENUM;
3142 }
3143 else if (dimensions == 2) {
3144 if (target == GL_PROXY_TEXTURE_2D) {
3145 maxLevels = ctx->Const.MaxTextureLevels;
3146 }
3147 else if (target == GL_TEXTURE_2D) {
3148 maxLevels = ctx->Const.MaxTextureLevels;
3149 }
3150 else if (target == GL_PROXY_TEXTURE_CUBE_MAP_ARB) {
3151 if (!ctx->Extensions.ARB_texture_cube_map)
3152 return GL_INVALID_ENUM; /*target*/
3153 maxLevels = ctx->Const.MaxCubeTextureLevels;
3154 }
3155 else if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
3156 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB) {
3157 if (!ctx->Extensions.ARB_texture_cube_map)
3158 return GL_INVALID_ENUM; /*target*/
3159 maxLevels = ctx->Const.MaxCubeTextureLevels;
3160 }
3161 else {
3162 return GL_INVALID_ENUM; /*target*/
3163 }
3164 }
3165 else if (dimensions == 3) {
3166 /* 3D compressed textures not allowed */
3167 return GL_INVALID_ENUM;
3168 }
3169
3170 maxTextureSize = 1 << (maxLevels - 1);
3171
3172 /* this will catch any invalid compressed format token */
3173 if (!is_compressed_format(ctx, format))
3174 return GL_INVALID_ENUM;
3175
3176 if (width < 1 || width > maxTextureSize)
3177 return GL_INVALID_VALUE;
3178
3179 if ((height < 1 || height > maxTextureSize)
3180 && dimensions > 1)
3181 return GL_INVALID_VALUE;
3182
3183 if (level < 0 || level >= maxLevels)
3184 return GL_INVALID_VALUE;
3185
3186 /*
3187 * do checks which depend on compression block size
3188 */
3189 get_compressed_block_size(format, &bw, &bh);
3190
3191 if ((xoffset % bw != 0) || (yoffset % bh != 0))
3192 return GL_INVALID_VALUE;
3193
3194 if ((width % bw != 0) && width != 2 && width != 1)
3195 return GL_INVALID_VALUE;
3196
3197 if ((height % bh != 0) && height != 2 && height != 1)
3198 return GL_INVALID_VALUE;
3199
3200 expectedSize = compressed_tex_size(width, height, depth, format);
3201 if (expectedSize != imageSize)
3202 return GL_INVALID_VALUE;
3203
3204 return GL_NO_ERROR;
3205 }
3206
3207
3208 /**
3209 * Do second part of glCompressedTexSubImage error checking.
3210 * \return GL_TRUE if error found, GL_FALSE otherwise.
3211 */
3212 static GLboolean
3213 compressed_subtexture_error_check2(GLcontext *ctx, GLuint dims,
3214 GLsizei width, GLsizei height,
3215 GLsizei depth, GLenum format,
3216 struct gl_texture_image *texImage)
3217 {
3218
3219 if ((GLint) format != texImage->InternalFormat) {
3220 _mesa_error(ctx, GL_INVALID_OPERATION,
3221 "glCompressedTexSubImage%uD(format=0x%x)", dims, format);
3222 return GL_TRUE;
3223 }
3224
3225 if (((width == 1 || width == 2) &&
3226 (GLuint) width != texImage->Width) ||
3227 (width > texImage->Width)) {
3228 _mesa_error(ctx, GL_INVALID_VALUE,
3229 "glCompressedTexSubImage%uD(width=%d)", dims, width);
3230 return GL_TRUE;
3231 }
3232
3233 if (dims >= 2) {
3234 if (((height == 1 || height == 2) &&
3235 (GLuint) height != texImage->Height) ||
3236 (height > texImage->Height)) {
3237 _mesa_error(ctx, GL_INVALID_VALUE,
3238 "glCompressedTexSubImage%uD(height=%d)", dims, height);
3239 return GL_TRUE;
3240 }
3241 }
3242
3243 if (dims >= 3) {
3244 if (((depth == 1 || depth == 2) &&
3245 (GLuint) depth != texImage->Depth) ||
3246 (depth > texImage->Depth)) {
3247 _mesa_error(ctx, GL_INVALID_VALUE,
3248 "glCompressedTexSubImage%uD(depth=%d)", dims, depth);
3249 return GL_TRUE;
3250 }
3251 }
3252
3253 return GL_FALSE;
3254 }
3255
3256
3257
3258 void GLAPIENTRY
3259 _mesa_CompressedTexImage1DARB(GLenum target, GLint level,
3260 GLenum internalFormat, GLsizei width,
3261 GLint border, GLsizei imageSize,
3262 const GLvoid *data)
3263 {
3264 GET_CURRENT_CONTEXT(ctx);
3265 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3266
3267 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
3268 _mesa_debug(ctx, "glCompressedTexImage1DARB %s %d %s %d %d %d %p\n",
3269 _mesa_lookup_enum_by_nr(target), level,
3270 _mesa_lookup_enum_by_nr(internalFormat),
3271 width, border, imageSize, data);
3272
3273 if (target == GL_TEXTURE_1D) {
3274 /* non-proxy target */
3275 struct gl_texture_object *texObj;
3276 struct gl_texture_image *texImage;
3277 GLenum error = compressed_texture_error_check(ctx, 1, target, level,
3278 internalFormat, width, 1, 1, border, imageSize);
3279 if (error) {
3280 _mesa_error(ctx, error, "glCompressedTexImage1D");
3281 return;
3282 }
3283
3284 texObj = _mesa_get_current_tex_object(ctx, target);
3285
3286 _mesa_lock_texture(ctx, texObj);
3287 {
3288 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
3289 if (!texImage) {
3290 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCompressedTexImage1D");
3291 }
3292 else {
3293 if (texImage->Data) {
3294 ctx->Driver.FreeTexImageData( ctx, texImage );
3295 }
3296 ASSERT(texImage->Data == NULL);
3297
3298 _mesa_init_teximage_fields(ctx, target, texImage, width, 1, 1,
3299 border, internalFormat);
3300
3301 /* Choose actual texture format */
3302 texImage->TexFormat =
3303 ctx->Driver.ChooseTextureFormat(ctx, internalFormat,
3304 GL_NONE, GL_NONE);
3305 ASSERT(texImage->TexFormat != MESA_FORMAT_NONE);
3306
3307 ASSERT(ctx->Driver.CompressedTexImage1D);
3308 ctx->Driver.CompressedTexImage1D(ctx, target, level,
3309 internalFormat, width, border,
3310 imageSize, data,
3311 texObj, texImage);
3312
3313 _mesa_set_fetch_functions(texImage, 1);
3314
3315 check_gen_mipmap(ctx, target, texObj, level);
3316
3317 /* state update */
3318 texObj->_Complete = GL_FALSE;
3319 ctx->NewState |= _NEW_TEXTURE;
3320 }
3321 }
3322 _mesa_unlock_texture(ctx, texObj);
3323 }
3324 else if (target == GL_PROXY_TEXTURE_1D) {
3325 /* Proxy texture: check for errors and update proxy state */
3326 GLenum error = compressed_texture_error_check(ctx, 1, target, level,
3327 internalFormat, width, 1, 1, border, imageSize);
3328 if (!error) {
3329 ASSERT(ctx->Driver.TestProxyTexImage);
3330 error = !(*ctx->Driver.TestProxyTexImage)(ctx, target, level,
3331 internalFormat, GL_NONE, GL_NONE,
3332 width, 1, 1, border);
3333 }
3334 if (error) {
3335 /* if error, clear all proxy texture image parameters */
3336 struct gl_texture_image *texImage;
3337 texImage = _mesa_get_proxy_tex_image(ctx, target, level);
3338 if (texImage)
3339 clear_teximage_fields(texImage);
3340 }
3341 else {
3342 /* store the teximage parameters */
3343 struct gl_texture_object *texObj;
3344 struct gl_texture_image *texImage;
3345
3346 texObj = _mesa_get_current_tex_object(ctx, target);
3347
3348 _mesa_lock_texture(ctx, texObj);
3349 {
3350 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
3351 _mesa_init_teximage_fields(ctx, target, texImage, width, 1, 1,
3352 border, internalFormat);
3353 }
3354 _mesa_unlock_texture(ctx, texObj);
3355 }
3356 }
3357 else {
3358 _mesa_error(ctx, GL_INVALID_ENUM, "glCompressedTexImage1D(target)");
3359 return;
3360 }
3361 }
3362
3363
3364 void GLAPIENTRY
3365 _mesa_CompressedTexImage2DARB(GLenum target, GLint level,
3366 GLenum internalFormat, GLsizei width,
3367 GLsizei height, GLint border, GLsizei imageSize,
3368 const GLvoid *data)
3369 {
3370 GET_CURRENT_CONTEXT(ctx);
3371 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3372
3373 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
3374 _mesa_debug(ctx, "glCompressedTexImage2DARB %s %d %s %d %d %d %d %p\n",
3375 _mesa_lookup_enum_by_nr(target), level,
3376 _mesa_lookup_enum_by_nr(internalFormat),
3377 width, height, border, imageSize, data);
3378
3379 if (target == GL_TEXTURE_2D ||
3380 (ctx->Extensions.ARB_texture_cube_map &&
3381 target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
3382 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB)) {
3383 /* non-proxy target */
3384 struct gl_texture_object *texObj;
3385 struct gl_texture_image *texImage;
3386
3387 GLenum error = compressed_texture_error_check(ctx, 2, target, level,
3388 internalFormat, width, height, 1, border, imageSize);
3389 if (error) {
3390 _mesa_error(ctx, error, "glCompressedTexImage2D");
3391 return;
3392 }
3393
3394 texObj = _mesa_get_current_tex_object(ctx, target);
3395
3396 _mesa_lock_texture(ctx, texObj);
3397 {
3398 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
3399 if (!texImage) {
3400 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCompressedTexImage2D");
3401 }
3402 else {
3403 if (texImage->Data) {
3404 ctx->Driver.FreeTexImageData( ctx, texImage );
3405 }
3406 ASSERT(texImage->Data == NULL);
3407
3408 _mesa_init_teximage_fields(ctx, target, texImage, width, height, 1,
3409 border, internalFormat);
3410
3411 /* Choose actual texture format */
3412 texImage->TexFormat =
3413 ctx->Driver.ChooseTextureFormat(ctx, internalFormat,
3414 GL_NONE, GL_NONE);
3415 ASSERT(texImage->TexFormat != MESA_FORMAT_NONE);
3416
3417 ASSERT(ctx->Driver.CompressedTexImage2D);
3418 ctx->Driver.CompressedTexImage2D(ctx, target, level,
3419 internalFormat, width, height,
3420 border, imageSize, data,
3421 texObj, texImage);
3422
3423 _mesa_set_fetch_functions(texImage, 2);
3424
3425 check_gen_mipmap(ctx, target, texObj, level);
3426
3427 /* state update */
3428 texObj->_Complete = GL_FALSE;
3429 ctx->NewState |= _NEW_TEXTURE;
3430 }
3431 }
3432 _mesa_unlock_texture(ctx, texObj);
3433 }
3434 else if (target == GL_PROXY_TEXTURE_2D ||
3435 (target == GL_PROXY_TEXTURE_CUBE_MAP_ARB &&
3436 ctx->Extensions.ARB_texture_cube_map)) {
3437 /* Proxy texture: check for errors and update proxy state */
3438 GLenum error = compressed_texture_error_check(ctx, 2, target, level,
3439 internalFormat, width, height, 1, border, imageSize);
3440 if (!error) {
3441 ASSERT(ctx->Driver.TestProxyTexImage);
3442 error = !(*ctx->Driver.TestProxyTexImage)(ctx, target, level,
3443 internalFormat, GL_NONE, GL_NONE,
3444 width, height, 1, border);
3445 }
3446 if (error) {
3447 /* if error, clear all proxy texture image parameters */
3448 struct gl_texture_image *texImage;
3449 texImage = _mesa_get_proxy_tex_image(ctx, target, level);
3450 if (texImage)
3451 clear_teximage_fields(texImage);
3452 }
3453 else {
3454 /* store the teximage parameters */
3455 struct gl_texture_object *texObj;
3456 struct gl_texture_image *texImage;
3457
3458 texObj = _mesa_get_current_tex_object(ctx, target);
3459
3460 _mesa_lock_texture(ctx, texObj);
3461 {
3462 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
3463 _mesa_init_teximage_fields(ctx, target, texImage, width, height, 1,
3464 border, internalFormat);
3465 }
3466 _mesa_unlock_texture(ctx, texObj);
3467 }
3468 }
3469 else {
3470 _mesa_error(ctx, GL_INVALID_ENUM, "glCompressedTexImage2D(target)");
3471 return;
3472 }
3473 }
3474
3475
3476 void GLAPIENTRY
3477 _mesa_CompressedTexImage3DARB(GLenum target, GLint level,
3478 GLenum internalFormat, GLsizei width,
3479 GLsizei height, GLsizei depth, GLint border,
3480 GLsizei imageSize, const GLvoid *data)
3481 {
3482 GET_CURRENT_CONTEXT(ctx);
3483 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3484
3485 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
3486 _mesa_debug(ctx, "glCompressedTexImage3DARB %s %d %s %d %d %d %d %d %p\n",
3487 _mesa_lookup_enum_by_nr(target), level,
3488 _mesa_lookup_enum_by_nr(internalFormat),
3489 width, height, depth, border, imageSize, data);
3490
3491 if (target == GL_TEXTURE_3D) {
3492 /* non-proxy target */
3493 struct gl_texture_object *texObj;
3494 struct gl_texture_image *texImage;
3495 GLenum error = compressed_texture_error_check(ctx, 3, target, level,
3496 internalFormat, width, height, depth, border, imageSize);
3497 if (error) {
3498 _mesa_error(ctx, error, "glCompressedTexImage3D");
3499 return;
3500 }
3501
3502 texObj = _mesa_get_current_tex_object(ctx, target);
3503
3504 _mesa_lock_texture(ctx, texObj);
3505 {
3506 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
3507 if (!texImage) {
3508 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCompressedTexImage3D");
3509 }
3510 else {
3511 if (texImage->Data) {
3512 ctx->Driver.FreeTexImageData( ctx, texImage );
3513 }
3514 ASSERT(texImage->Data == NULL);
3515
3516 _mesa_init_teximage_fields(ctx, target, texImage,
3517 width, height, depth,
3518 border, internalFormat);
3519
3520 /* Choose actual texture format */
3521 texImage->TexFormat =
3522 ctx->Driver.ChooseTextureFormat(ctx, internalFormat,
3523 GL_NONE, GL_NONE);
3524 ASSERT(texImage->TexFormat != MESA_FORMAT_NONE);
3525
3526 ASSERT(ctx->Driver.CompressedTexImage3D);
3527 ctx->Driver.CompressedTexImage3D(ctx, target, level,
3528 internalFormat,
3529 width, height, depth,
3530 border, imageSize, data,
3531 texObj, texImage);
3532
3533 _mesa_set_fetch_functions(texImage, 3);
3534
3535 check_gen_mipmap(ctx, target, texObj, level);
3536
3537 /* state update */
3538 texObj->_Complete = GL_FALSE;
3539 ctx->NewState |= _NEW_TEXTURE;
3540 }
3541 }
3542 _mesa_unlock_texture(ctx, texObj);
3543 }
3544 else if (target == GL_PROXY_TEXTURE_3D) {
3545 /* Proxy texture: check for errors and update proxy state */
3546 GLenum error = compressed_texture_error_check(ctx, 3, target, level,
3547 internalFormat, width, height, depth, border, imageSize);
3548 if (!error) {
3549 ASSERT(ctx->Driver.TestProxyTexImage);
3550 error = !(*ctx->Driver.TestProxyTexImage)(ctx, target, level,
3551 internalFormat, GL_NONE, GL_NONE,
3552 width, height, depth, border);
3553 }
3554 if (error) {
3555 /* if error, clear all proxy texture image parameters */
3556 struct gl_texture_image *texImage;
3557 texImage = _mesa_get_proxy_tex_image(ctx, target, level);
3558 if (texImage)
3559 clear_teximage_fields(texImage);
3560 }
3561 else {
3562 /* store the teximage parameters */
3563 struct gl_texture_object *texObj;
3564 struct gl_texture_image *texImage;
3565
3566 texObj = _mesa_get_current_tex_object(ctx, target);
3567
3568 _mesa_lock_texture(ctx, texObj);
3569 {
3570 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
3571 _mesa_init_teximage_fields(ctx, target, texImage, width, height,
3572 depth, border, internalFormat);
3573 }
3574 _mesa_unlock_texture(ctx, texObj);
3575 }
3576 }
3577 else {
3578 _mesa_error(ctx, GL_INVALID_ENUM, "glCompressedTexImage3D(target)");
3579 return;
3580 }
3581 }
3582
3583
3584 /**
3585 * Common helper for glCompressedTexSubImage1/2/3D().
3586 */
3587 static void
3588 compressed_tex_sub_image(GLuint dims, GLenum target, GLint level,
3589 GLint xoffset, GLint yoffset, GLint zoffset,
3590 GLsizei width, GLsizei height, GLsizei depth,
3591 GLenum format, GLsizei imageSize, const GLvoid *data)
3592 {
3593 struct gl_texture_object *texObj;
3594 struct gl_texture_image *texImage;
3595 GLenum error;
3596 GET_CURRENT_CONTEXT(ctx);
3597 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3598
3599 error = compressed_subtexture_error_check(ctx, dims, target, level,
3600 xoffset, 0, 0, /* pos */
3601 width, height, depth, /* size */
3602 format, imageSize);
3603 if (error) {
3604 _mesa_error(ctx, error, "glCompressedTexSubImage%uD", dims);
3605 return;
3606 }
3607
3608 texObj = _mesa_get_current_tex_object(ctx, target);
3609
3610 _mesa_lock_texture(ctx, texObj);
3611 {
3612 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
3613 assert(texImage);
3614
3615 if (compressed_subtexture_error_check2(ctx, dims, width, height, depth,
3616 format, texImage)) {
3617 /* error was recorded */
3618 }
3619 else if (width > 0 && height > 0 && depth > 0) {
3620 switch (dims) {
3621 case 1:
3622 if (ctx->Driver.CompressedTexSubImage1D) {
3623 ctx->Driver.CompressedTexSubImage1D(ctx, target, level,
3624 xoffset, width,
3625 format, imageSize, data,
3626 texObj, texImage);
3627 }
3628 break;
3629 case 2:
3630 if (ctx->Driver.CompressedTexSubImage2D) {
3631 ctx->Driver.CompressedTexSubImage2D(ctx, target, level,
3632 xoffset, yoffset,
3633 width, height,
3634 format, imageSize, data,
3635 texObj, texImage);
3636 }
3637 break;
3638 case 3:
3639 if (ctx->Driver.CompressedTexSubImage3D) {
3640 ctx->Driver.CompressedTexSubImage3D(ctx, target, level,
3641 xoffset, yoffset, zoffset,
3642 width, height, depth,
3643 format, imageSize, data,
3644 texObj, texImage);
3645 }
3646 break;
3647 default:
3648 ;
3649 }
3650
3651 check_gen_mipmap(ctx, target, texObj, level);
3652
3653 ctx->NewState |= _NEW_TEXTURE;
3654 }
3655 }
3656 _mesa_unlock_texture(ctx, texObj);
3657 }
3658
3659
3660 void GLAPIENTRY
3661 _mesa_CompressedTexSubImage1DARB(GLenum target, GLint level, GLint xoffset,
3662 GLsizei width, GLenum format,
3663 GLsizei imageSize, const GLvoid *data)
3664 {
3665 compressed_tex_sub_image(1, target, level, xoffset, 0, 0, width, 1, 1,
3666 format, imageSize, data);
3667 }
3668
3669
3670 void GLAPIENTRY
3671 _mesa_CompressedTexSubImage2DARB(GLenum target, GLint level, GLint xoffset,
3672 GLint yoffset, GLsizei width, GLsizei height,
3673 GLenum format, GLsizei imageSize,
3674 const GLvoid *data)
3675 {
3676 compressed_tex_sub_image(2, target, level, xoffset, yoffset, 0,
3677 width, height, 1, format, imageSize, data);
3678 }
3679
3680
3681 void GLAPIENTRY
3682 _mesa_CompressedTexSubImage3DARB(GLenum target, GLint level, GLint xoffset,
3683 GLint yoffset, GLint zoffset, GLsizei width,
3684 GLsizei height, GLsizei depth, GLenum format,
3685 GLsizei imageSize, const GLvoid *data)
3686 {
3687 compressed_tex_sub_image(3, target, level, xoffset, yoffset, zoffset,
3688 width, height, depth, format, imageSize, data);
3689 }
3690
3691