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