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