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