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