mesa: Don't check for image->Data when freeing an image's contents.
[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.FreeTextureImageBuffer().
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.FreeTextureImageBuffer);
634 ctx->Driver.FreeTextureImageBuffer( 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.FreeTextureImageBuffer(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 ctx->Driver.FreeTextureImageBuffer(ctx, texImage);
2460
2461 ASSERT(texImage->Data == NULL);
2462 texFormat = _mesa_choose_texture_format(ctx, texObj, target, level,
2463 internalFormat, format,
2464 type);
2465
2466 if (legal_texture_size(ctx, texFormat, width, height, depth)) {
2467 _mesa_init_teximage_fields(ctx, target, texImage,
2468 width, height, depth,
2469 border, internalFormat, texFormat);
2470
2471 /* Give the texture to the driver. <pixels> may be null. */
2472 ASSERT(ctx->Driver.TexImage3D);
2473 switch (dims) {
2474 case 1:
2475 ctx->Driver.TexImage1D(ctx, target, level, internalFormat,
2476 width, border, format,
2477 type, pixels, &ctx->Unpack, texObj,
2478 texImage);
2479 break;
2480 case 2:
2481 ctx->Driver.TexImage2D(ctx, target, level, internalFormat,
2482 width, height, border, format,
2483 type, pixels, &ctx->Unpack, texObj,
2484 texImage);
2485 break;
2486 case 3:
2487 ctx->Driver.TexImage3D(ctx, target, level, internalFormat,
2488 width, height, depth, border, format,
2489 type, pixels, &ctx->Unpack, texObj,
2490 texImage);
2491 break;
2492 default:
2493 _mesa_problem(ctx, "invalid dims=%u in teximage()", dims);
2494 }
2495
2496 check_gen_mipmap(ctx, target, texObj, level);
2497
2498 update_fbo_texture(ctx, texObj, face, level);
2499
2500 /* state update */
2501 texObj->_Complete = GL_FALSE;
2502 ctx->NewState |= _NEW_TEXTURE;
2503 }
2504 else {
2505 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage%uD", dims);
2506 }
2507 }
2508 }
2509 _mesa_unlock_texture(ctx, texObj);
2510 }
2511 }
2512
2513
2514 /*
2515 * Called from the API. Note that width includes the border.
2516 */
2517 void GLAPIENTRY
2518 _mesa_TexImage1D( GLenum target, GLint level, GLint internalFormat,
2519 GLsizei width, GLint border, GLenum format,
2520 GLenum type, const GLvoid *pixels )
2521 {
2522 GET_CURRENT_CONTEXT(ctx);
2523 teximage(ctx, 1, target, level, internalFormat, width, 1, 1,
2524 border, format, type, pixels);
2525 }
2526
2527
2528 void GLAPIENTRY
2529 _mesa_TexImage2D( GLenum target, GLint level, GLint internalFormat,
2530 GLsizei width, GLsizei height, GLint border,
2531 GLenum format, GLenum type,
2532 const GLvoid *pixels )
2533 {
2534 GET_CURRENT_CONTEXT(ctx);
2535 teximage(ctx, 2, target, level, internalFormat, width, height, 1,
2536 border, format, type, pixels);
2537 }
2538
2539
2540 /*
2541 * Called by the API or display list executor.
2542 * Note that width and height include the border.
2543 */
2544 void GLAPIENTRY
2545 _mesa_TexImage3D( GLenum target, GLint level, GLint internalFormat,
2546 GLsizei width, GLsizei height, GLsizei depth,
2547 GLint border, GLenum format, GLenum type,
2548 const GLvoid *pixels )
2549 {
2550 GET_CURRENT_CONTEXT(ctx);
2551 teximage(ctx, 3, target, level, internalFormat, width, height, depth,
2552 border, format, type, pixels);
2553 }
2554
2555
2556 void GLAPIENTRY
2557 _mesa_TexImage3DEXT( GLenum target, GLint level, GLenum internalFormat,
2558 GLsizei width, GLsizei height, GLsizei depth,
2559 GLint border, GLenum format, GLenum type,
2560 const GLvoid *pixels )
2561 {
2562 _mesa_TexImage3D(target, level, (GLint) internalFormat, width, height,
2563 depth, border, format, type, pixels);
2564 }
2565
2566
2567 #if FEATURE_OES_EGL_image
2568 void GLAPIENTRY
2569 _mesa_EGLImageTargetTexture2DOES (GLenum target, GLeglImageOES image)
2570 {
2571 struct gl_texture_object *texObj;
2572 struct gl_texture_image *texImage;
2573 GET_CURRENT_CONTEXT(ctx);
2574 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2575
2576 if (!ctx->Extensions.OES_EGL_image) {
2577 _mesa_error(ctx, GL_INVALID_OPERATION,
2578 "glEGLImageTargetTexture2DOES(unsupported)");
2579 return;
2580 }
2581
2582 if (target != GL_TEXTURE_2D) {
2583 _mesa_error(ctx, GL_INVALID_ENUM,
2584 "glEGLImageTargetTexture2D(target=%d)", target);
2585 return;
2586 }
2587
2588 if (ctx->NewState & _NEW_PIXEL)
2589 _mesa_update_state(ctx);
2590
2591 texObj = _mesa_get_current_tex_object(ctx, target);
2592 _mesa_lock_texture(ctx, texObj);
2593
2594 texImage = _mesa_get_tex_image(ctx, texObj, target, 0);
2595 if (!texImage) {
2596 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glEGLImageTargetTexture2D");
2597 } else {
2598 ctx->Driver.FreeTextureImageBuffer(ctx, texImage);
2599
2600 ASSERT(texImage->Data == NULL);
2601 ctx->Driver.EGLImageTargetTexture2D(ctx, target,
2602 texObj, texImage, image);
2603
2604 /* state update */
2605 texObj->_Complete = GL_FALSE;
2606 ctx->NewState |= _NEW_TEXTURE;
2607 }
2608 _mesa_unlock_texture(ctx, texObj);
2609
2610 }
2611 #endif
2612
2613
2614
2615 /**
2616 * Implement all the glTexSubImage1/2/3D() functions.
2617 */
2618 static void
2619 texsubimage(struct gl_context *ctx, GLuint dims, GLenum target, GLint level,
2620 GLint xoffset, GLint yoffset, GLint zoffset,
2621 GLsizei width, GLsizei height, GLsizei depth,
2622 GLenum format, GLenum type, const GLvoid *pixels )
2623 {
2624 struct gl_texture_object *texObj;
2625 struct gl_texture_image *texImage;
2626
2627 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2628
2629 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
2630 _mesa_debug(ctx, "glTexSubImage%uD %s %d %d %d %d %d %d %d %s %s %p\n",
2631 dims,
2632 _mesa_lookup_enum_by_nr(target), level,
2633 xoffset, yoffset, zoffset, width, height, depth,
2634 _mesa_lookup_enum_by_nr(format),
2635 _mesa_lookup_enum_by_nr(type), pixels);
2636
2637 /* check target (proxies not allowed) */
2638 if (!legal_texsubimage_target(ctx, dims, target)) {
2639 _mesa_error(ctx, GL_INVALID_ENUM, "glTexSubImage%uD(target=%s)",
2640 dims, _mesa_lookup_enum_by_nr(target));
2641 return;
2642 }
2643
2644 if (ctx->NewState & _NEW_PIXEL)
2645 _mesa_update_state(ctx);
2646
2647 if (subtexture_error_check(ctx, dims, target, level, xoffset, yoffset, zoffset,
2648 width, height, depth, format, type)) {
2649 return; /* error was detected */
2650 }
2651
2652 texObj = _mesa_get_current_tex_object(ctx, target);
2653
2654 _mesa_lock_texture(ctx, texObj);
2655 {
2656 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
2657
2658 if (subtexture_error_check2(ctx, dims, target, level,
2659 xoffset, yoffset, zoffset,
2660 width, height, depth,
2661 format, type, texImage)) {
2662 /* error was recorded */
2663 }
2664 else if (width > 0 && height > 0 && depth > 0) {
2665 /* If we have a border, offset=-1 is legal. Bias by border width. */
2666 switch (dims) {
2667 case 3:
2668 zoffset += texImage->Border;
2669 /* fall-through */
2670 case 2:
2671 yoffset += texImage->Border;
2672 /* fall-through */
2673 case 1:
2674 xoffset += texImage->Border;
2675 }
2676
2677 switch (dims) {
2678 case 1:
2679 ctx->Driver.TexSubImage1D(ctx, target, level,
2680 xoffset, width,
2681 format, type, pixels,
2682 &ctx->Unpack, texObj, texImage );
2683 break;
2684 case 2:
2685 ctx->Driver.TexSubImage2D(ctx, target, level,
2686 xoffset, yoffset, width, height,
2687 format, type, pixels,
2688 &ctx->Unpack, texObj, texImage );
2689 break;
2690 case 3:
2691 ctx->Driver.TexSubImage3D(ctx, target, level,
2692 xoffset, yoffset, zoffset,
2693 width, height, depth,
2694 format, type, pixels,
2695 &ctx->Unpack, texObj, texImage );
2696 break;
2697 default:
2698 _mesa_problem(ctx, "unexpected dims in subteximage()");
2699 }
2700
2701 check_gen_mipmap(ctx, target, texObj, level);
2702
2703 ctx->NewState |= _NEW_TEXTURE;
2704 }
2705 }
2706 _mesa_unlock_texture(ctx, texObj);
2707 }
2708
2709
2710 void GLAPIENTRY
2711 _mesa_TexSubImage1D( GLenum target, GLint level,
2712 GLint xoffset, GLsizei width,
2713 GLenum format, GLenum type,
2714 const GLvoid *pixels )
2715 {
2716 GET_CURRENT_CONTEXT(ctx);
2717 texsubimage(ctx, 1, target, level,
2718 xoffset, 0, 0,
2719 width, 1, 1,
2720 format, type, pixels);
2721 }
2722
2723
2724 void GLAPIENTRY
2725 _mesa_TexSubImage2D( GLenum target, GLint level,
2726 GLint xoffset, GLint yoffset,
2727 GLsizei width, GLsizei height,
2728 GLenum format, GLenum type,
2729 const GLvoid *pixels )
2730 {
2731 GET_CURRENT_CONTEXT(ctx);
2732 texsubimage(ctx, 2, target, level,
2733 xoffset, yoffset, 0,
2734 width, height, 1,
2735 format, type, pixels);
2736 }
2737
2738
2739
2740 void GLAPIENTRY
2741 _mesa_TexSubImage3D( GLenum target, GLint level,
2742 GLint xoffset, GLint yoffset, GLint zoffset,
2743 GLsizei width, GLsizei height, GLsizei depth,
2744 GLenum format, GLenum type,
2745 const GLvoid *pixels )
2746 {
2747 GET_CURRENT_CONTEXT(ctx);
2748 texsubimage(ctx, 3, target, level,
2749 xoffset, yoffset, zoffset,
2750 width, height, depth,
2751 format, type, pixels);
2752 }
2753
2754
2755
2756 /**
2757 * Implement the glCopyTexImage1/2D() functions.
2758 */
2759 static void
2760 copyteximage(struct gl_context *ctx, GLuint dims,
2761 GLenum target, GLint level, GLenum internalFormat,
2762 GLint x, GLint y, GLsizei width, GLsizei height, GLint border )
2763 {
2764 struct gl_texture_object *texObj;
2765 struct gl_texture_image *texImage;
2766 const GLuint face = _mesa_tex_target_to_face(target);
2767
2768 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2769
2770 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
2771 _mesa_debug(ctx, "glCopyTexImage%uD %s %d %s %d %d %d %d %d\n",
2772 dims,
2773 _mesa_lookup_enum_by_nr(target), level,
2774 _mesa_lookup_enum_by_nr(internalFormat),
2775 x, y, width, height, border);
2776
2777 if (ctx->NewState & NEW_COPY_TEX_STATE)
2778 _mesa_update_state(ctx);
2779
2780 if (copytexture_error_check(ctx, dims, target, level, internalFormat,
2781 width, height, border))
2782 return;
2783
2784 texObj = _mesa_get_current_tex_object(ctx, target);
2785
2786 _mesa_lock_texture(ctx, texObj);
2787 {
2788 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
2789
2790 if (!texImage) {
2791 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexImage%uD", dims);
2792 }
2793 else {
2794 /* choose actual hw format */
2795 gl_format texFormat = _mesa_choose_texture_format(ctx, texObj,
2796 target, level,
2797 internalFormat,
2798 GL_NONE, GL_NONE);
2799
2800 if (legal_texture_size(ctx, texFormat, width, height, 1)) {
2801 GLint srcX = x, srcY = y, dstX = 0, dstY = 0;
2802
2803 /* Free old texture image */
2804 ctx->Driver.FreeTextureImageBuffer(ctx, texImage);
2805
2806 _mesa_init_teximage_fields(ctx, target, texImage, width, height, 1,
2807 border, internalFormat, texFormat);
2808
2809 /* Allocate texture memory (no pixel data yet) */
2810 if (dims == 1) {
2811 ctx->Driver.TexImage1D(ctx, target, level, internalFormat,
2812 width, border, GL_NONE, GL_NONE, NULL,
2813 &ctx->Unpack, texObj, texImage);
2814 }
2815 else {
2816 ctx->Driver.TexImage2D(ctx, target, level, internalFormat,
2817 width, height, border, GL_NONE, GL_NONE,
2818 NULL, &ctx->Unpack, texObj, texImage);
2819 }
2820
2821 if (_mesa_clip_copytexsubimage(ctx, &dstX, &dstY, &srcX, &srcY,
2822 &width, &height)) {
2823 if (dims == 1)
2824 ctx->Driver.CopyTexSubImage1D(ctx, target, level, dstX,
2825 srcX, srcY, width);
2826
2827 else
2828 ctx->Driver.CopyTexSubImage2D(ctx, target, level, dstX, dstY,
2829 srcX, srcY, width, height);
2830 }
2831
2832 check_gen_mipmap(ctx, target, texObj, level);
2833
2834 update_fbo_texture(ctx, texObj, face, level);
2835
2836 /* state update */
2837 texObj->_Complete = GL_FALSE;
2838 ctx->NewState |= _NEW_TEXTURE;
2839 }
2840 else {
2841 /* probably too large of image */
2842 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexImage%uD", dims);
2843 }
2844 }
2845 }
2846 _mesa_unlock_texture(ctx, texObj);
2847 }
2848
2849
2850
2851 void GLAPIENTRY
2852 _mesa_CopyTexImage1D( GLenum target, GLint level,
2853 GLenum internalFormat,
2854 GLint x, GLint y,
2855 GLsizei width, GLint border )
2856 {
2857 GET_CURRENT_CONTEXT(ctx);
2858 copyteximage(ctx, 1, target, level, internalFormat, x, y, width, 1, border);
2859 }
2860
2861
2862
2863 void GLAPIENTRY
2864 _mesa_CopyTexImage2D( GLenum target, GLint level, GLenum internalFormat,
2865 GLint x, GLint y, GLsizei width, GLsizei height,
2866 GLint border )
2867 {
2868 GET_CURRENT_CONTEXT(ctx);
2869 copyteximage(ctx, 2, target, level, internalFormat,
2870 x, y, width, height, border);
2871 }
2872
2873
2874
2875 /**
2876 * Implementation for glCopyTexSubImage1/2/3D() functions.
2877 */
2878 static void
2879 copytexsubimage(struct gl_context *ctx, GLuint dims, GLenum target, GLint level,
2880 GLint xoffset, GLint yoffset, GLint zoffset,
2881 GLint x, GLint y, GLsizei width, GLsizei height)
2882 {
2883 struct gl_texture_object *texObj;
2884 struct gl_texture_image *texImage;
2885
2886 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2887
2888 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
2889 _mesa_debug(ctx, "glCopyTexSubImage%uD %s %d %d %d %d %d %d %d %d\n",
2890 dims,
2891 _mesa_lookup_enum_by_nr(target),
2892 level, xoffset, yoffset, zoffset, x, y, width, height);
2893
2894 if (ctx->NewState & NEW_COPY_TEX_STATE)
2895 _mesa_update_state(ctx);
2896
2897 if (copytexsubimage_error_check1(ctx, dims, target, level))
2898 return;
2899
2900 texObj = _mesa_get_current_tex_object(ctx, target);
2901
2902 _mesa_lock_texture(ctx, texObj);
2903 {
2904 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
2905
2906 if (copytexsubimage_error_check2(ctx, dims, target, level, xoffset, yoffset,
2907 zoffset, width, height, texImage)) {
2908 /* error was recored */
2909 }
2910 else {
2911 /* If we have a border, offset=-1 is legal. Bias by border width. */
2912 switch (dims) {
2913 case 3:
2914 zoffset += texImage->Border;
2915 /* fall-through */
2916 case 2:
2917 yoffset += texImage->Border;
2918 /* fall-through */
2919 case 1:
2920 xoffset += texImage->Border;
2921 }
2922
2923 if (_mesa_clip_copytexsubimage(ctx, &xoffset, &yoffset, &x, &y,
2924 &width, &height)) {
2925 switch (dims) {
2926 case 1:
2927 ctx->Driver.CopyTexSubImage1D(ctx, target, level,
2928 xoffset, x, y, width);
2929 break;
2930 case 2:
2931 ctx->Driver.CopyTexSubImage2D(ctx, target, level,
2932 xoffset, yoffset,
2933 x, y, width, height);
2934 break;
2935 case 3:
2936 ctx->Driver.CopyTexSubImage3D(ctx, target, level,
2937 xoffset, yoffset, zoffset,
2938 x, y, width, height);
2939 break;
2940 default:
2941 _mesa_problem(ctx, "bad dims in copytexsubimage()");
2942 }
2943
2944 check_gen_mipmap(ctx, target, texObj, level);
2945
2946 ctx->NewState |= _NEW_TEXTURE;
2947 }
2948 }
2949 }
2950 _mesa_unlock_texture(ctx, texObj);
2951 }
2952
2953
2954 void GLAPIENTRY
2955 _mesa_CopyTexSubImage1D( GLenum target, GLint level,
2956 GLint xoffset, GLint x, GLint y, GLsizei width )
2957 {
2958 GET_CURRENT_CONTEXT(ctx);
2959 copytexsubimage(ctx, 1, target, level, xoffset, 0, 0, x, y, width, 1);
2960 }
2961
2962
2963
2964 void GLAPIENTRY
2965 _mesa_CopyTexSubImage2D( GLenum target, GLint level,
2966 GLint xoffset, GLint yoffset,
2967 GLint x, GLint y, GLsizei width, GLsizei height )
2968 {
2969 GET_CURRENT_CONTEXT(ctx);
2970 copytexsubimage(ctx, 2, target, level, xoffset, yoffset, 0, x, y,
2971 width, height);
2972 }
2973
2974
2975
2976 void GLAPIENTRY
2977 _mesa_CopyTexSubImage3D( GLenum target, GLint level,
2978 GLint xoffset, GLint yoffset, GLint zoffset,
2979 GLint x, GLint y, GLsizei width, GLsizei height )
2980 {
2981 GET_CURRENT_CONTEXT(ctx);
2982 copytexsubimage(ctx, 3, target, level, xoffset, yoffset, zoffset,
2983 x, y, width, height);
2984 }
2985
2986
2987
2988
2989 /**********************************************************************/
2990 /****** Compressed Textures ******/
2991 /**********************************************************************/
2992
2993
2994 /**
2995 * Return expected size of a compressed texture.
2996 */
2997 static GLuint
2998 compressed_tex_size(GLsizei width, GLsizei height, GLsizei depth,
2999 GLenum glformat)
3000 {
3001 gl_format mesaFormat = _mesa_glenum_to_compressed_format(glformat);
3002 return _mesa_format_image_size(mesaFormat, width, height, depth);
3003 }
3004
3005
3006 /*
3007 * Return compressed texture block size, in pixels.
3008 */
3009 static void
3010 get_compressed_block_size(GLenum glformat, GLuint *bw, GLuint *bh)
3011 {
3012 gl_format mesaFormat = _mesa_glenum_to_compressed_format(glformat);
3013 _mesa_get_format_block_size(mesaFormat, bw, bh);
3014 }
3015
3016
3017 /**
3018 * Error checking for glCompressedTexImage[123]D().
3019 * \param reason returns reason for error, if any
3020 * \return error code or GL_NO_ERROR.
3021 */
3022 static GLenum
3023 compressed_texture_error_check(struct gl_context *ctx, GLint dimensions,
3024 GLenum target, GLint level,
3025 GLenum internalFormat, GLsizei width,
3026 GLsizei height, GLsizei depth, GLint border,
3027 GLsizei imageSize, char **reason)
3028 {
3029 const GLenum proxyTarget = get_proxy_target(target);
3030 const GLint maxLevels = _mesa_max_texture_levels(ctx, target);
3031 GLint expectedSize;
3032
3033 *reason = ""; /* no error */
3034
3035 /* check level */
3036 if (level < 0 || level >= maxLevels) {
3037 *reason = "level";
3038 return GL_INVALID_VALUE;
3039 }
3040
3041 if (!target_can_be_compressed(ctx, target, internalFormat)) {
3042 *reason = "target";
3043 return GL_INVALID_ENUM;
3044 }
3045
3046 /* This will detect any invalid internalFormat value */
3047 if (!_mesa_is_compressed_format(ctx, internalFormat)) {
3048 *reason = "internalFormat";
3049 return GL_INVALID_ENUM;
3050 }
3051
3052 /* This should really never fail */
3053 if (_mesa_base_tex_format(ctx, internalFormat) < 0) {
3054 *reason = "internalFormat";
3055 return GL_INVALID_ENUM;
3056 }
3057
3058 /* No compressed formats support borders at this time */
3059 if (border != 0) {
3060 *reason = "border != 0";
3061 return GL_INVALID_VALUE;
3062 }
3063
3064 /* For cube map, width must equal height */
3065 if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
3066 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB && width != height) {
3067 *reason = "width != height";
3068 return GL_INVALID_VALUE;
3069 }
3070
3071 /* check image size against compression block size */
3072 {
3073 gl_format texFormat =
3074 ctx->Driver.ChooseTextureFormat(ctx, internalFormat,
3075 GL_NONE, GL_NONE);
3076 GLuint bw, bh;
3077
3078 _mesa_get_format_block_size(texFormat, &bw, &bh);
3079 if ((width > bw && width % bw > 0) ||
3080 (height > bh && height % bh > 0)) {
3081 /*
3082 * Per GL_ARB_texture_compression: GL_INVALID_OPERATION is
3083 * generated [...] if any parameter combinations are not
3084 * supported by the specific compressed internal format.
3085 */
3086 *reason = "invalid width or height for compression format";
3087 return GL_INVALID_OPERATION;
3088 }
3089 }
3090
3091 /* check image sizes */
3092 if (!ctx->Driver.TestProxyTexImage(ctx, proxyTarget, level,
3093 internalFormat, GL_NONE, GL_NONE,
3094 width, height, depth, border)) {
3095 /* See error comment above */
3096 *reason = "invalid width, height or format";
3097 return GL_INVALID_OPERATION;
3098 }
3099
3100 /* check image size in bytes */
3101 expectedSize = compressed_tex_size(width, height, depth, internalFormat);
3102 if (expectedSize != imageSize) {
3103 /* Per GL_ARB_texture_compression: GL_INVALID_VALUE is generated [...]
3104 * if <imageSize> is not consistent with the format, dimensions, and
3105 * contents of the specified image.
3106 */
3107 *reason = "imageSize inconsistant with width/height/format";
3108 return GL_INVALID_VALUE;
3109 }
3110
3111 return GL_NO_ERROR;
3112 }
3113
3114
3115 /**
3116 * Error checking for glCompressedTexSubImage[123]D().
3117 * \warning There are some bad assumptions here about the size of compressed
3118 * texture tiles (multiple of 4) used to test the validity of the
3119 * offset and size parameters.
3120 * \return error code or GL_NO_ERROR.
3121 */
3122 static GLenum
3123 compressed_subtexture_error_check(struct gl_context *ctx, GLint dimensions,
3124 GLenum target, GLint level,
3125 GLint xoffset, GLint yoffset, GLint zoffset,
3126 GLsizei width, GLsizei height, GLsizei depth,
3127 GLenum format, GLsizei imageSize)
3128 {
3129 GLint expectedSize, maxLevels = 0, maxTextureSize;
3130 GLuint bw, bh;
3131 (void) zoffset;
3132
3133 if (dimensions == 1) {
3134 /* 1D compressed textures not allowed */
3135 return GL_INVALID_ENUM;
3136 }
3137 else if (dimensions == 2) {
3138 if (target == GL_PROXY_TEXTURE_2D) {
3139 maxLevels = ctx->Const.MaxTextureLevels;
3140 }
3141 else if (target == GL_TEXTURE_2D) {
3142 maxLevels = ctx->Const.MaxTextureLevels;
3143 }
3144 else if (target == GL_PROXY_TEXTURE_CUBE_MAP_ARB) {
3145 if (!ctx->Extensions.ARB_texture_cube_map)
3146 return GL_INVALID_ENUM; /*target*/
3147 maxLevels = ctx->Const.MaxCubeTextureLevels;
3148 }
3149 else if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
3150 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB) {
3151 if (!ctx->Extensions.ARB_texture_cube_map)
3152 return GL_INVALID_ENUM; /*target*/
3153 maxLevels = ctx->Const.MaxCubeTextureLevels;
3154 }
3155 else {
3156 return GL_INVALID_ENUM; /*target*/
3157 }
3158 }
3159 else if (dimensions == 3) {
3160 /* 3D compressed textures not allowed */
3161 return GL_INVALID_ENUM;
3162 }
3163
3164 maxTextureSize = 1 << (maxLevels - 1);
3165
3166 /* this will catch any invalid compressed format token */
3167 if (!_mesa_is_compressed_format(ctx, format))
3168 return GL_INVALID_ENUM;
3169
3170 if (width < 1 || width > maxTextureSize)
3171 return GL_INVALID_VALUE;
3172
3173 if ((height < 1 || height > maxTextureSize)
3174 && dimensions > 1)
3175 return GL_INVALID_VALUE;
3176
3177 if (level < 0 || level >= maxLevels)
3178 return GL_INVALID_VALUE;
3179
3180 /*
3181 * do checks which depend on compression block size
3182 */
3183 get_compressed_block_size(format, &bw, &bh);
3184
3185 if ((xoffset % bw != 0) || (yoffset % bh != 0))
3186 return GL_INVALID_VALUE;
3187
3188 if ((width % bw != 0) && width != 2 && width != 1)
3189 return GL_INVALID_VALUE;
3190
3191 if ((height % bh != 0) && height != 2 && height != 1)
3192 return GL_INVALID_VALUE;
3193
3194 expectedSize = compressed_tex_size(width, height, depth, format);
3195 if (expectedSize != imageSize)
3196 return GL_INVALID_VALUE;
3197
3198 return GL_NO_ERROR;
3199 }
3200
3201
3202 /**
3203 * Do second part of glCompressedTexSubImage error checking.
3204 * \return GL_TRUE if error found, GL_FALSE otherwise.
3205 */
3206 static GLboolean
3207 compressed_subtexture_error_check2(struct gl_context *ctx, GLuint dims,
3208 GLsizei width, GLsizei height,
3209 GLsizei depth, GLenum format,
3210 struct gl_texture_image *texImage)
3211 {
3212
3213 if ((GLint) format != texImage->InternalFormat) {
3214 _mesa_error(ctx, GL_INVALID_OPERATION,
3215 "glCompressedTexSubImage%uD(format=0x%x)", dims, format);
3216 return GL_TRUE;
3217 }
3218
3219 if (((width == 1 || width == 2) &&
3220 width != (GLsizei) texImage->Width) ||
3221 (width > (GLsizei) texImage->Width)) {
3222 _mesa_error(ctx, GL_INVALID_VALUE,
3223 "glCompressedTexSubImage%uD(width=%d)", dims, width);
3224 return GL_TRUE;
3225 }
3226
3227 if (dims >= 2) {
3228 if (((height == 1 || height == 2) &&
3229 height != (GLsizei) texImage->Height) ||
3230 (height > (GLsizei) texImage->Height)) {
3231 _mesa_error(ctx, GL_INVALID_VALUE,
3232 "glCompressedTexSubImage%uD(height=%d)", dims, height);
3233 return GL_TRUE;
3234 }
3235 }
3236
3237 if (dims >= 3) {
3238 if (((depth == 1 || depth == 2) &&
3239 depth != (GLsizei) texImage->Depth) ||
3240 (depth > (GLsizei) texImage->Depth)) {
3241 _mesa_error(ctx, GL_INVALID_VALUE,
3242 "glCompressedTexSubImage%uD(depth=%d)", dims, depth);
3243 return GL_TRUE;
3244 }
3245 }
3246
3247 return GL_FALSE;
3248 }
3249
3250
3251 /**
3252 * Implementation of the glCompressedTexImage1/2/3D() functions.
3253 */
3254 static void
3255 compressedteximage(struct gl_context *ctx, GLuint dims,
3256 GLenum target, GLint level,
3257 GLenum internalFormat, GLsizei width,
3258 GLsizei height, GLsizei depth, GLint border,
3259 GLsizei imageSize, const GLvoid *data)
3260 {
3261 GLenum error;
3262 char *reason = "";
3263
3264 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3265
3266 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
3267 _mesa_debug(ctx,
3268 "glCompressedTexImage%uDARB %s %d %s %d %d %d %d %d %p\n",
3269 dims,
3270 _mesa_lookup_enum_by_nr(target), level,
3271 _mesa_lookup_enum_by_nr(internalFormat),
3272 width, height, depth, border, imageSize, data);
3273
3274 /* check target */
3275 if (!legal_teximage_target(ctx, dims, target)) {
3276 _mesa_error(ctx, GL_INVALID_ENUM, "glCompressedTexImage%uD(target=%s)",
3277 dims, _mesa_lookup_enum_by_nr(target));
3278 return;
3279 }
3280
3281 error = compressed_texture_error_check(ctx, dims, target, level,
3282 internalFormat, width, height, depth,
3283 border, imageSize, &reason);
3284
3285 if (error) {
3286 _mesa_error(ctx, error, "glCompressedTexImage%uD(%s)", dims, reason);
3287 return;
3288 }
3289
3290 #if FEATURE_ES
3291 /* XXX this is kind of a hack */
3292 if (dims == 2) {
3293 switch (internalFormat) {
3294 case GL_PALETTE4_RGB8_OES:
3295 case GL_PALETTE4_RGBA8_OES:
3296 case GL_PALETTE4_R5_G6_B5_OES:
3297 case GL_PALETTE4_RGBA4_OES:
3298 case GL_PALETTE4_RGB5_A1_OES:
3299 case GL_PALETTE8_RGB8_OES:
3300 case GL_PALETTE8_RGBA8_OES:
3301 case GL_PALETTE8_R5_G6_B5_OES:
3302 case GL_PALETTE8_RGBA4_OES:
3303 case GL_PALETTE8_RGB5_A1_OES:
3304 _mesa_cpal_compressed_teximage2d(target, level, internalFormat,
3305 width, height, imageSize, data);
3306 return;
3307 }
3308 }
3309 #endif
3310
3311 if (_mesa_is_proxy_texture(target)) {
3312 /* Proxy texture: just check for errors and update proxy state */
3313 struct gl_texture_image *texImage;
3314
3315 if (!error) {
3316 struct gl_texture_object *texObj =
3317 _mesa_get_current_tex_object(ctx, target);
3318 gl_format texFormat =
3319 _mesa_choose_texture_format(ctx, texObj, target, level,
3320 internalFormat, GL_NONE, GL_NONE);
3321 if (!legal_texture_size(ctx, texFormat, width, height, depth)) {
3322 error = GL_OUT_OF_MEMORY;
3323 }
3324 }
3325
3326 texImage = _mesa_get_proxy_tex_image(ctx, target, level);
3327 if (texImage) {
3328 if (error) {
3329 /* if error, clear all proxy texture image parameters */
3330 clear_teximage_fields(texImage);
3331 }
3332 else {
3333 /* no error: store the teximage parameters */
3334 _mesa_init_teximage_fields(ctx, target, texImage, width, height,
3335 depth, border, internalFormat,
3336 MESA_FORMAT_NONE);
3337 }
3338 }
3339 }
3340 else {
3341 /* non-proxy target */
3342 struct gl_texture_object *texObj;
3343 struct gl_texture_image *texImage;
3344
3345 if (error) {
3346 _mesa_error(ctx, error, "glCompressedTexImage%uD", dims);
3347 return;
3348 }
3349
3350 texObj = _mesa_get_current_tex_object(ctx, target);
3351
3352 _mesa_lock_texture(ctx, texObj);
3353 {
3354 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
3355 if (!texImage) {
3356 _mesa_error(ctx, GL_OUT_OF_MEMORY,
3357 "glCompressedTexImage%uD", dims);
3358 }
3359 else {
3360 gl_format texFormat;
3361
3362 ctx->Driver.FreeTextureImageBuffer(ctx, texImage);
3363 ASSERT(texImage->Data == NULL);
3364
3365 texFormat = _mesa_choose_texture_format(ctx, texObj, target, level,
3366 internalFormat, GL_NONE,
3367 GL_NONE);
3368
3369 if (legal_texture_size(ctx, texFormat, width, height, depth)) {
3370 _mesa_init_teximage_fields(ctx, target, texImage,
3371 width, height, depth,
3372 border, internalFormat, texFormat);
3373
3374 switch (dims) {
3375 case 1:
3376 ASSERT(ctx->Driver.CompressedTexImage1D);
3377 ctx->Driver.CompressedTexImage1D(ctx, target, level,
3378 internalFormat,
3379 width,
3380 border, imageSize, data,
3381 texObj, texImage);
3382 break;
3383 case 2:
3384 ASSERT(ctx->Driver.CompressedTexImage2D);
3385 ctx->Driver.CompressedTexImage2D(ctx, target, level,
3386 internalFormat,
3387 width, height,
3388 border, imageSize, data,
3389 texObj, texImage);
3390 break;
3391 case 3:
3392 ASSERT(ctx->Driver.CompressedTexImage3D);
3393 ctx->Driver.CompressedTexImage3D(ctx, target, level,
3394 internalFormat,
3395 width, height, depth,
3396 border, imageSize, data,
3397 texObj, texImage);
3398 break;
3399 default:
3400 _mesa_problem(ctx, "bad dims in compressedteximage");
3401 }
3402
3403 check_gen_mipmap(ctx, target, texObj, level);
3404
3405 /* state update */
3406 texObj->_Complete = GL_FALSE;
3407 ctx->NewState |= _NEW_TEXTURE;
3408 }
3409 else {
3410 _mesa_error(ctx, GL_OUT_OF_MEMORY,
3411 "glCompressedTexImage%uD", dims);
3412 }
3413 }
3414 }
3415 _mesa_unlock_texture(ctx, texObj);
3416 }
3417 }
3418
3419
3420 void GLAPIENTRY
3421 _mesa_CompressedTexImage1DARB(GLenum target, GLint level,
3422 GLenum internalFormat, GLsizei width,
3423 GLint border, GLsizei imageSize,
3424 const GLvoid *data)
3425 {
3426 GET_CURRENT_CONTEXT(ctx);
3427 compressedteximage(ctx, 1, target, level, internalFormat,
3428 width, 1, 1, border, imageSize, data);
3429 }
3430
3431
3432 void GLAPIENTRY
3433 _mesa_CompressedTexImage2DARB(GLenum target, GLint level,
3434 GLenum internalFormat, GLsizei width,
3435 GLsizei height, GLint border, GLsizei imageSize,
3436 const GLvoid *data)
3437 {
3438 GET_CURRENT_CONTEXT(ctx);
3439 compressedteximage(ctx, 2, target, level, internalFormat,
3440 width, height, 1, border, imageSize, data);
3441 }
3442
3443
3444 void GLAPIENTRY
3445 _mesa_CompressedTexImage3DARB(GLenum target, GLint level,
3446 GLenum internalFormat, GLsizei width,
3447 GLsizei height, GLsizei depth, GLint border,
3448 GLsizei imageSize, const GLvoid *data)
3449 {
3450 GET_CURRENT_CONTEXT(ctx);
3451 compressedteximage(ctx, 3, target, level, internalFormat,
3452 width, height, depth, border, imageSize, data);
3453 }
3454
3455
3456 /**
3457 * Common helper for glCompressedTexSubImage1/2/3D().
3458 */
3459 static void
3460 compressed_tex_sub_image(GLuint dims, GLenum target, GLint level,
3461 GLint xoffset, GLint yoffset, GLint zoffset,
3462 GLsizei width, GLsizei height, GLsizei depth,
3463 GLenum format, GLsizei imageSize, const GLvoid *data)
3464 {
3465 struct gl_texture_object *texObj;
3466 struct gl_texture_image *texImage;
3467 GLenum error;
3468 GET_CURRENT_CONTEXT(ctx);
3469 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3470
3471 error = compressed_subtexture_error_check(ctx, dims, target, level,
3472 xoffset, 0, 0, /* pos */
3473 width, height, depth, /* size */
3474 format, imageSize);
3475 if (error) {
3476 _mesa_error(ctx, error, "glCompressedTexSubImage%uD", dims);
3477 return;
3478 }
3479
3480 texObj = _mesa_get_current_tex_object(ctx, target);
3481
3482 _mesa_lock_texture(ctx, texObj);
3483 {
3484 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
3485 assert(texImage);
3486
3487 if (compressed_subtexture_error_check2(ctx, dims, width, height, depth,
3488 format, texImage)) {
3489 /* error was recorded */
3490 }
3491 else if (width > 0 && height > 0 && depth > 0) {
3492 switch (dims) {
3493 case 1:
3494 if (ctx->Driver.CompressedTexSubImage1D) {
3495 ctx->Driver.CompressedTexSubImage1D(ctx, target, level,
3496 xoffset, width,
3497 format, imageSize, data,
3498 texObj, texImage);
3499 }
3500 break;
3501 case 2:
3502 if (ctx->Driver.CompressedTexSubImage2D) {
3503 ctx->Driver.CompressedTexSubImage2D(ctx, target, level,
3504 xoffset, yoffset,
3505 width, height,
3506 format, imageSize, data,
3507 texObj, texImage);
3508 }
3509 break;
3510 case 3:
3511 if (ctx->Driver.CompressedTexSubImage3D) {
3512 ctx->Driver.CompressedTexSubImage3D(ctx, target, level,
3513 xoffset, yoffset, zoffset,
3514 width, height, depth,
3515 format, imageSize, data,
3516 texObj, texImage);
3517 }
3518 break;
3519 default:
3520 ;
3521 }
3522
3523 check_gen_mipmap(ctx, target, texObj, level);
3524
3525 ctx->NewState |= _NEW_TEXTURE;
3526 }
3527 }
3528 _mesa_unlock_texture(ctx, texObj);
3529 }
3530
3531
3532 void GLAPIENTRY
3533 _mesa_CompressedTexSubImage1DARB(GLenum target, GLint level, GLint xoffset,
3534 GLsizei width, GLenum format,
3535 GLsizei imageSize, const GLvoid *data)
3536 {
3537 compressed_tex_sub_image(1, target, level, xoffset, 0, 0, width, 1, 1,
3538 format, imageSize, data);
3539 }
3540
3541
3542 void GLAPIENTRY
3543 _mesa_CompressedTexSubImage2DARB(GLenum target, GLint level, GLint xoffset,
3544 GLint yoffset, GLsizei width, GLsizei height,
3545 GLenum format, GLsizei imageSize,
3546 const GLvoid *data)
3547 {
3548 compressed_tex_sub_image(2, target, level, xoffset, yoffset, 0,
3549 width, height, 1, format, imageSize, data);
3550 }
3551
3552
3553 void GLAPIENTRY
3554 _mesa_CompressedTexSubImage3DARB(GLenum target, GLint level, GLint xoffset,
3555 GLint yoffset, GLint zoffset, GLsizei width,
3556 GLsizei height, GLsizei depth, GLenum format,
3557 GLsizei imageSize, const GLvoid *data)
3558 {
3559 compressed_tex_sub_image(3, target, level, xoffset, yoffset, zoffset,
3560 width, height, depth, format, imageSize, data);
3561 }
3562
3563
3564 /**
3565 * Helper for glTexBuffer(). Check if internalFormat is legal. If so,
3566 * return the basic data type and number of components for the format.
3567 * \param return GL_TRUE if internalFormat is legal, GL_FALSE otherwise
3568 */
3569 static GLboolean
3570 get_sized_format_info(const struct gl_context *ctx, GLenum internalFormat,
3571 GLenum *datatype, GLuint *components)
3572 {
3573 switch (internalFormat) {
3574 case GL_ALPHA8:
3575 *datatype = GL_UNSIGNED_BYTE;
3576 *components = 1;
3577 break;
3578 case GL_ALPHA16:
3579 *datatype = GL_UNSIGNED_SHORT;
3580 *components = 1;
3581 break;
3582 case GL_ALPHA16F_ARB:
3583 *datatype = GL_HALF_FLOAT;
3584 *components = 1;
3585 break;
3586 case GL_ALPHA32F_ARB:
3587 *datatype = GL_FLOAT;
3588 *components = 1;
3589 break;
3590 case GL_ALPHA8I_EXT:
3591 *datatype = GL_BYTE;
3592 *components = 1;
3593 break;
3594 case GL_ALPHA16I_EXT:
3595 *datatype = GL_SHORT;
3596 *components = 1;
3597 break;
3598 case GL_ALPHA32I_EXT:
3599 *datatype = GL_INT;
3600 *components = 1;
3601 break;
3602 case GL_ALPHA8UI_EXT:
3603 *datatype = GL_UNSIGNED_BYTE;
3604 *components = 1;
3605 break;
3606 case GL_ALPHA16UI_EXT:
3607 *datatype = GL_UNSIGNED_SHORT;
3608 *components = 1;
3609 break;
3610 case GL_ALPHA32UI_EXT:
3611 *datatype = GL_UNSIGNED_INT;
3612 *components = 1;
3613 break;
3614 case GL_LUMINANCE8:
3615 *datatype = GL_UNSIGNED_BYTE;
3616 *components = 1;
3617 break;
3618 case GL_LUMINANCE16:
3619 *datatype = GL_UNSIGNED_SHORT;
3620 *components = 1;
3621 break;
3622 case GL_LUMINANCE16F_ARB:
3623 *datatype = GL_HALF_FLOAT;
3624 *components = 1;
3625 break;
3626 case GL_LUMINANCE32F_ARB:
3627 *datatype = GL_FLOAT;
3628 *components = 1;
3629 break;
3630 case GL_LUMINANCE8I_EXT:
3631 *datatype = GL_BYTE;
3632 *components = 1;
3633 break;
3634 case GL_LUMINANCE16I_EXT:
3635 *datatype = GL_SHORT;
3636 *components = 1;
3637 break;
3638 case GL_LUMINANCE32I_EXT:
3639 *datatype = GL_INT;
3640 *components = 1;
3641 break;
3642 case GL_LUMINANCE8UI_EXT:
3643 *datatype = GL_UNSIGNED_BYTE;
3644 *components = 1;
3645 break;
3646 case GL_LUMINANCE16UI_EXT:
3647 *datatype = GL_UNSIGNED_SHORT;
3648 *components = 1;
3649 break;
3650 case GL_LUMINANCE32UI_EXT:
3651 *datatype = GL_UNSIGNED_INT;
3652 *components = 1;
3653 break;
3654 case GL_LUMINANCE8_ALPHA8:
3655 *datatype = GL_UNSIGNED_BYTE;
3656 *components = 2;
3657 break;
3658 case GL_LUMINANCE16_ALPHA16:
3659 *datatype = GL_UNSIGNED_SHORT;
3660 *components = 2;
3661 break;
3662 case GL_LUMINANCE_ALPHA16F_ARB:
3663 *datatype = GL_HALF_FLOAT;
3664 *components = 2;
3665 break;
3666 case GL_LUMINANCE_ALPHA32F_ARB:
3667 *datatype = GL_FLOAT;
3668 *components = 2;
3669 break;
3670 case GL_LUMINANCE_ALPHA8I_EXT:
3671 *datatype = GL_BYTE;
3672 *components = 2;
3673 break;
3674 case GL_LUMINANCE_ALPHA16I_EXT:
3675 *datatype = GL_SHORT;
3676 *components = 2;
3677 break;
3678 case GL_LUMINANCE_ALPHA32I_EXT:
3679 *datatype = GL_INT;
3680 *components = 2;
3681 break;
3682 case GL_LUMINANCE_ALPHA8UI_EXT:
3683 *datatype = GL_UNSIGNED_BYTE;
3684 *components = 2;
3685 break;
3686 case GL_LUMINANCE_ALPHA16UI_EXT:
3687 *datatype = GL_UNSIGNED_SHORT;
3688 *components = 2;
3689 break;
3690 case GL_LUMINANCE_ALPHA32UI_EXT:
3691 *datatype = GL_UNSIGNED_INT;
3692 *components = 2;
3693 break;
3694 case GL_INTENSITY8:
3695 *datatype = GL_UNSIGNED_BYTE;
3696 *components = 1;
3697 break;
3698 case GL_INTENSITY16:
3699 *datatype = GL_UNSIGNED_SHORT;
3700 *components = 1;
3701 break;
3702 case GL_INTENSITY16F_ARB:
3703 *datatype = GL_HALF_FLOAT;
3704 *components = 1;
3705 break;
3706 case GL_INTENSITY32F_ARB:
3707 *datatype = GL_FLOAT;
3708 *components = 1;
3709 break;
3710 case GL_INTENSITY8I_EXT:
3711 *datatype = GL_BYTE;
3712 *components = 1;
3713 break;
3714 case GL_INTENSITY16I_EXT:
3715 *datatype = GL_SHORT;
3716 *components = 1;
3717 break;
3718 case GL_INTENSITY32I_EXT:
3719 *datatype = GL_INT;
3720 *components = 1;
3721 break;
3722 case GL_INTENSITY8UI_EXT:
3723 *datatype = GL_UNSIGNED_BYTE;
3724 *components = 1;
3725 break;
3726 case GL_INTENSITY16UI_EXT:
3727 *datatype = GL_UNSIGNED_SHORT;
3728 *components = 1;
3729 break;
3730 case GL_INTENSITY32UI_EXT:
3731 *datatype = GL_UNSIGNED_INT;
3732 *components = 1;
3733 break;
3734 case GL_RGBA8:
3735 *datatype = GL_UNSIGNED_BYTE;
3736 *components = 4;
3737 break;
3738 case GL_RGBA16:
3739 *datatype = GL_UNSIGNED_SHORT;
3740 *components = 4;
3741 break;
3742 case GL_RGBA16F_ARB:
3743 *datatype = GL_HALF_FLOAT;
3744 *components = 4;
3745 break;
3746 case GL_RGBA32F_ARB:
3747 *datatype = GL_FLOAT;
3748 *components = 4;
3749 break;
3750 case GL_RGBA8I_EXT:
3751 *datatype = GL_BYTE;
3752 *components = 4;
3753 break;
3754 case GL_RGBA16I_EXT:
3755 *datatype = GL_SHORT;
3756 *components = 4;
3757 break;
3758 case GL_RGBA32I_EXT:
3759 *datatype = GL_INT;
3760 *components = 4;
3761 break;
3762 case GL_RGBA8UI_EXT:
3763 *datatype = GL_UNSIGNED_BYTE;
3764 *components = 4;
3765 break;
3766 case GL_RGBA16UI_EXT:
3767 *datatype = GL_UNSIGNED_SHORT;
3768 *components = 4;
3769 break;
3770 case GL_RGBA32UI_EXT:
3771 *datatype = GL_UNSIGNED_INT;
3772 *components = 4;
3773 break;
3774 default:
3775 return GL_FALSE;
3776 }
3777
3778 if (*datatype == GL_FLOAT && !ctx->Extensions.ARB_texture_float)
3779 return GL_FALSE;
3780
3781 if (*datatype == GL_HALF_FLOAT && !ctx->Extensions.ARB_half_float_pixel)
3782 return GL_FALSE;
3783
3784 return GL_TRUE;
3785 }
3786
3787
3788 /** GL_ARB_texture_buffer_object */
3789 void GLAPIENTRY
3790 _mesa_TexBuffer(GLenum target, GLenum internalFormat, GLuint buffer)
3791 {
3792 struct gl_texture_object *texObj;
3793 struct gl_buffer_object *bufObj;
3794 GLenum dataType;
3795 GLuint comps;
3796
3797 GET_CURRENT_CONTEXT(ctx);
3798 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3799
3800 if (!ctx->Extensions.ARB_texture_buffer_object) {
3801 _mesa_error(ctx, GL_INVALID_OPERATION, "glTexBuffer");
3802 return;
3803 }
3804
3805 if (target != GL_TEXTURE_BUFFER_ARB) {
3806 _mesa_error(ctx, GL_INVALID_ENUM, "glTexBuffer(target)");
3807 return;
3808 }
3809
3810 if (!get_sized_format_info(ctx, internalFormat, &dataType, &comps)) {
3811 _mesa_error(ctx, GL_INVALID_ENUM, "glTexBuffer(internalFormat 0x%x)",
3812 internalFormat);
3813 return;
3814 }
3815
3816 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
3817 if (buffer && !bufObj) {
3818 _mesa_error(ctx, GL_INVALID_OPERATION, "glTexBuffer(buffer %u)", buffer);
3819 return;
3820 }
3821
3822 texObj = _mesa_get_current_tex_object(ctx, target);
3823
3824 _mesa_lock_texture(ctx, texObj);
3825 {
3826 _mesa_reference_buffer_object(ctx, &texObj->BufferObject, bufObj);
3827 texObj->BufferObjectFormat = internalFormat;
3828 }
3829 _mesa_unlock_texture(ctx, texObj);
3830 }