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