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