mesa: stop using ctx->Driver.CopyTexImage1D/2D() hooks
[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 /* choose actual hw format */
2801 gl_format texFormat = _mesa_choose_texture_format(ctx, texObj,
2802 target, level,
2803 internalFormat,
2804 GL_NONE, GL_NONE);
2805
2806 if (legal_texture_size(ctx, texFormat, width, height, 1)) {
2807 GLint srcX = x, srcY = y, dstX = 0, dstY = 0;
2808
2809 /* Free old texture image */
2810 ctx->Driver.FreeTexImageData(ctx, texImage);
2811
2812 _mesa_init_teximage_fields(ctx, target, texImage, width, height, 1,
2813 border, internalFormat, texFormat);
2814
2815 /* Allocate texture memory (no pixel data yet) */
2816 if (dims == 1) {
2817 ctx->Driver.TexImage1D(ctx, target, level, internalFormat,
2818 width, border, GL_NONE, GL_NONE, NULL,
2819 &ctx->Unpack, texObj, texImage);
2820 }
2821 else {
2822 ctx->Driver.TexImage2D(ctx, target, level, internalFormat,
2823 width, height, border, GL_NONE, GL_NONE,
2824 NULL, &ctx->Unpack, texObj, texImage);
2825 }
2826
2827 if (_mesa_clip_copytexsubimage(ctx, &dstX, &dstY, &srcX, &srcY,
2828 &width, &height)) {
2829 if (dims == 1)
2830 ctx->Driver.CopyTexSubImage1D(ctx, target, level, dstX,
2831 srcX, srcY, width);
2832
2833 else
2834 ctx->Driver.CopyTexSubImage2D(ctx, target, level, dstX, dstY,
2835 srcX, srcY, width, height);
2836 }
2837
2838 check_gen_mipmap(ctx, target, texObj, level);
2839
2840 update_fbo_texture(ctx, texObj, face, level);
2841
2842 /* state update */
2843 texObj->_Complete = GL_FALSE;
2844 ctx->NewState |= _NEW_TEXTURE;
2845 }
2846 else {
2847 /* probably too large of image */
2848 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexImage%uD", dims);
2849 }
2850 }
2851 }
2852 _mesa_unlock_texture(ctx, texObj);
2853 }
2854
2855
2856
2857 void GLAPIENTRY
2858 _mesa_CopyTexImage1D( GLenum target, GLint level,
2859 GLenum internalFormat,
2860 GLint x, GLint y,
2861 GLsizei width, GLint border )
2862 {
2863 GET_CURRENT_CONTEXT(ctx);
2864 copyteximage(ctx, 1, target, level, internalFormat, x, y, width, 1, border);
2865 }
2866
2867
2868
2869 void GLAPIENTRY
2870 _mesa_CopyTexImage2D( GLenum target, GLint level, GLenum internalFormat,
2871 GLint x, GLint y, GLsizei width, GLsizei height,
2872 GLint border )
2873 {
2874 GET_CURRENT_CONTEXT(ctx);
2875 copyteximage(ctx, 2, target, level, internalFormat,
2876 x, y, width, height, border);
2877 }
2878
2879
2880
2881 /**
2882 * Implementation for glCopyTexSubImage1/2/3D() functions.
2883 */
2884 static void
2885 copytexsubimage(struct gl_context *ctx, GLuint dims, GLenum target, GLint level,
2886 GLint xoffset, GLint yoffset, GLint zoffset,
2887 GLint x, GLint y, GLsizei width, GLsizei height)
2888 {
2889 struct gl_texture_object *texObj;
2890 struct gl_texture_image *texImage;
2891
2892 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2893
2894 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
2895 _mesa_debug(ctx, "glCopyTexSubImage%uD %s %d %d %d %d %d %d %d %d\n",
2896 dims,
2897 _mesa_lookup_enum_by_nr(target),
2898 level, xoffset, yoffset, zoffset, x, y, width, height);
2899
2900 if (ctx->NewState & NEW_COPY_TEX_STATE)
2901 _mesa_update_state(ctx);
2902
2903 if (copytexsubimage_error_check1(ctx, dims, target, level))
2904 return;
2905
2906 texObj = _mesa_get_current_tex_object(ctx, target);
2907
2908 _mesa_lock_texture(ctx, texObj);
2909 {
2910 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
2911
2912 if (copytexsubimage_error_check2(ctx, dims, target, level, xoffset, yoffset,
2913 zoffset, width, height, texImage)) {
2914 /* error was recored */
2915 }
2916 else {
2917 /* If we have a border, offset=-1 is legal. Bias by border width. */
2918 switch (dims) {
2919 case 3:
2920 zoffset += texImage->Border;
2921 /* fall-through */
2922 case 2:
2923 yoffset += texImage->Border;
2924 /* fall-through */
2925 case 1:
2926 xoffset += texImage->Border;
2927 }
2928
2929 if (_mesa_clip_copytexsubimage(ctx, &xoffset, &yoffset, &x, &y,
2930 &width, &height)) {
2931 switch (dims) {
2932 case 1:
2933 ctx->Driver.CopyTexSubImage1D(ctx, target, level,
2934 xoffset, x, y, width);
2935 break;
2936 case 2:
2937 ctx->Driver.CopyTexSubImage2D(ctx, target, level,
2938 xoffset, yoffset,
2939 x, y, width, height);
2940 break;
2941 case 3:
2942 ctx->Driver.CopyTexSubImage3D(ctx, target, level,
2943 xoffset, yoffset, zoffset,
2944 x, y, width, height);
2945 break;
2946 default:
2947 _mesa_problem(ctx, "bad dims in copytexsubimage()");
2948 }
2949
2950 check_gen_mipmap(ctx, target, texObj, level);
2951
2952 ctx->NewState |= _NEW_TEXTURE;
2953 }
2954 }
2955 }
2956 _mesa_unlock_texture(ctx, texObj);
2957 }
2958
2959
2960 void GLAPIENTRY
2961 _mesa_CopyTexSubImage1D( GLenum target, GLint level,
2962 GLint xoffset, GLint x, GLint y, GLsizei width )
2963 {
2964 GET_CURRENT_CONTEXT(ctx);
2965 copytexsubimage(ctx, 1, target, level, xoffset, 0, 0, x, y, width, 1);
2966 }
2967
2968
2969
2970 void GLAPIENTRY
2971 _mesa_CopyTexSubImage2D( GLenum target, GLint level,
2972 GLint xoffset, GLint yoffset,
2973 GLint x, GLint y, GLsizei width, GLsizei height )
2974 {
2975 GET_CURRENT_CONTEXT(ctx);
2976 copytexsubimage(ctx, 2, target, level, xoffset, yoffset, 0, x, y,
2977 width, height);
2978 }
2979
2980
2981
2982 void GLAPIENTRY
2983 _mesa_CopyTexSubImage3D( GLenum target, GLint level,
2984 GLint xoffset, GLint yoffset, GLint zoffset,
2985 GLint x, GLint y, GLsizei width, GLsizei height )
2986 {
2987 GET_CURRENT_CONTEXT(ctx);
2988 copytexsubimage(ctx, 3, target, level, xoffset, yoffset, zoffset,
2989 x, y, width, height);
2990 }
2991
2992
2993
2994
2995 /**********************************************************************/
2996 /****** Compressed Textures ******/
2997 /**********************************************************************/
2998
2999
3000 /**
3001 * Return expected size of a compressed texture.
3002 */
3003 static GLuint
3004 compressed_tex_size(GLsizei width, GLsizei height, GLsizei depth,
3005 GLenum glformat)
3006 {
3007 gl_format mesaFormat = _mesa_glenum_to_compressed_format(glformat);
3008 return _mesa_format_image_size(mesaFormat, width, height, depth);
3009 }
3010
3011
3012 /*
3013 * Return compressed texture block size, in pixels.
3014 */
3015 static void
3016 get_compressed_block_size(GLenum glformat, GLuint *bw, GLuint *bh)
3017 {
3018 gl_format mesaFormat = _mesa_glenum_to_compressed_format(glformat);
3019 _mesa_get_format_block_size(mesaFormat, bw, bh);
3020 }
3021
3022
3023 /**
3024 * Error checking for glCompressedTexImage[123]D().
3025 * \param reason returns reason for error, if any
3026 * \return error code or GL_NO_ERROR.
3027 */
3028 static GLenum
3029 compressed_texture_error_check(struct gl_context *ctx, GLint dimensions,
3030 GLenum target, GLint level,
3031 GLenum internalFormat, GLsizei width,
3032 GLsizei height, GLsizei depth, GLint border,
3033 GLsizei imageSize, char **reason)
3034 {
3035 const GLenum proxyTarget = get_proxy_target(target);
3036 const GLint maxLevels = _mesa_max_texture_levels(ctx, target);
3037 GLint expectedSize;
3038
3039 *reason = ""; /* no error */
3040
3041 /* check level */
3042 if (level < 0 || level >= maxLevels) {
3043 *reason = "level";
3044 return GL_INVALID_VALUE;
3045 }
3046
3047 if (!target_can_be_compressed(ctx, target, internalFormat)) {
3048 *reason = "target";
3049 return GL_INVALID_ENUM;
3050 }
3051
3052 /* This will detect any invalid internalFormat value */
3053 if (!_mesa_is_compressed_format(ctx, internalFormat)) {
3054 *reason = "internalFormat";
3055 return GL_INVALID_ENUM;
3056 }
3057
3058 /* This should really never fail */
3059 if (_mesa_base_tex_format(ctx, internalFormat) < 0) {
3060 *reason = "internalFormat";
3061 return GL_INVALID_ENUM;
3062 }
3063
3064 /* No compressed formats support borders at this time */
3065 if (border != 0) {
3066 *reason = "border != 0";
3067 return GL_INVALID_VALUE;
3068 }
3069
3070 /* For cube map, width must equal height */
3071 if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
3072 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB && width != height) {
3073 *reason = "width != height";
3074 return GL_INVALID_VALUE;
3075 }
3076
3077 /* check image size against compression block size */
3078 {
3079 gl_format texFormat =
3080 ctx->Driver.ChooseTextureFormat(ctx, internalFormat,
3081 GL_NONE, GL_NONE);
3082 GLuint bw, bh;
3083
3084 _mesa_get_format_block_size(texFormat, &bw, &bh);
3085 if ((width > bw && width % bw > 0) ||
3086 (height > bh && height % bh > 0)) {
3087 /*
3088 * Per GL_ARB_texture_compression: GL_INVALID_OPERATION is
3089 * generated [...] if any parameter combinations are not
3090 * supported by the specific compressed internal format.
3091 */
3092 *reason = "invalid width or height for compression format";
3093 return GL_INVALID_OPERATION;
3094 }
3095 }
3096
3097 /* check image sizes */
3098 if (!ctx->Driver.TestProxyTexImage(ctx, proxyTarget, level,
3099 internalFormat, GL_NONE, GL_NONE,
3100 width, height, depth, border)) {
3101 /* See error comment above */
3102 *reason = "invalid width, height or format";
3103 return GL_INVALID_OPERATION;
3104 }
3105
3106 /* check image size in bytes */
3107 expectedSize = compressed_tex_size(width, height, depth, internalFormat);
3108 if (expectedSize != imageSize) {
3109 /* Per GL_ARB_texture_compression: GL_INVALID_VALUE is generated [...]
3110 * if <imageSize> is not consistent with the format, dimensions, and
3111 * contents of the specified image.
3112 */
3113 *reason = "imageSize inconsistant with width/height/format";
3114 return GL_INVALID_VALUE;
3115 }
3116
3117 return GL_NO_ERROR;
3118 }
3119
3120
3121 /**
3122 * Error checking for glCompressedTexSubImage[123]D().
3123 * \warning There are some bad assumptions here about the size of compressed
3124 * texture tiles (multiple of 4) used to test the validity of the
3125 * offset and size parameters.
3126 * \return error code or GL_NO_ERROR.
3127 */
3128 static GLenum
3129 compressed_subtexture_error_check(struct gl_context *ctx, GLint dimensions,
3130 GLenum target, GLint level,
3131 GLint xoffset, GLint yoffset, GLint zoffset,
3132 GLsizei width, GLsizei height, GLsizei depth,
3133 GLenum format, GLsizei imageSize)
3134 {
3135 GLint expectedSize, maxLevels = 0, maxTextureSize;
3136 GLuint bw, bh;
3137 (void) zoffset;
3138
3139 if (dimensions == 1) {
3140 /* 1D compressed textures not allowed */
3141 return GL_INVALID_ENUM;
3142 }
3143 else if (dimensions == 2) {
3144 if (target == GL_PROXY_TEXTURE_2D) {
3145 maxLevels = ctx->Const.MaxTextureLevels;
3146 }
3147 else if (target == GL_TEXTURE_2D) {
3148 maxLevels = ctx->Const.MaxTextureLevels;
3149 }
3150 else if (target == GL_PROXY_TEXTURE_CUBE_MAP_ARB) {
3151 if (!ctx->Extensions.ARB_texture_cube_map)
3152 return GL_INVALID_ENUM; /*target*/
3153 maxLevels = ctx->Const.MaxCubeTextureLevels;
3154 }
3155 else if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
3156 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB) {
3157 if (!ctx->Extensions.ARB_texture_cube_map)
3158 return GL_INVALID_ENUM; /*target*/
3159 maxLevels = ctx->Const.MaxCubeTextureLevels;
3160 }
3161 else {
3162 return GL_INVALID_ENUM; /*target*/
3163 }
3164 }
3165 else if (dimensions == 3) {
3166 /* 3D compressed textures not allowed */
3167 return GL_INVALID_ENUM;
3168 }
3169
3170 maxTextureSize = 1 << (maxLevels - 1);
3171
3172 /* this will catch any invalid compressed format token */
3173 if (!_mesa_is_compressed_format(ctx, format))
3174 return GL_INVALID_ENUM;
3175
3176 if (width < 1 || width > maxTextureSize)
3177 return GL_INVALID_VALUE;
3178
3179 if ((height < 1 || height > maxTextureSize)
3180 && dimensions > 1)
3181 return GL_INVALID_VALUE;
3182
3183 if (level < 0 || level >= maxLevels)
3184 return GL_INVALID_VALUE;
3185
3186 /*
3187 * do checks which depend on compression block size
3188 */
3189 get_compressed_block_size(format, &bw, &bh);
3190
3191 if ((xoffset % bw != 0) || (yoffset % bh != 0))
3192 return GL_INVALID_VALUE;
3193
3194 if ((width % bw != 0) && width != 2 && width != 1)
3195 return GL_INVALID_VALUE;
3196
3197 if ((height % bh != 0) && height != 2 && height != 1)
3198 return GL_INVALID_VALUE;
3199
3200 expectedSize = compressed_tex_size(width, height, depth, format);
3201 if (expectedSize != imageSize)
3202 return GL_INVALID_VALUE;
3203
3204 return GL_NO_ERROR;
3205 }
3206
3207
3208 /**
3209 * Do second part of glCompressedTexSubImage error checking.
3210 * \return GL_TRUE if error found, GL_FALSE otherwise.
3211 */
3212 static GLboolean
3213 compressed_subtexture_error_check2(struct gl_context *ctx, GLuint dims,
3214 GLsizei width, GLsizei height,
3215 GLsizei depth, GLenum format,
3216 struct gl_texture_image *texImage)
3217 {
3218
3219 if ((GLint) format != texImage->InternalFormat) {
3220 _mesa_error(ctx, GL_INVALID_OPERATION,
3221 "glCompressedTexSubImage%uD(format=0x%x)", dims, format);
3222 return GL_TRUE;
3223 }
3224
3225 if (((width == 1 || width == 2) &&
3226 width != (GLsizei) texImage->Width) ||
3227 (width > (GLsizei) texImage->Width)) {
3228 _mesa_error(ctx, GL_INVALID_VALUE,
3229 "glCompressedTexSubImage%uD(width=%d)", dims, width);
3230 return GL_TRUE;
3231 }
3232
3233 if (dims >= 2) {
3234 if (((height == 1 || height == 2) &&
3235 height != (GLsizei) texImage->Height) ||
3236 (height > (GLsizei) texImage->Height)) {
3237 _mesa_error(ctx, GL_INVALID_VALUE,
3238 "glCompressedTexSubImage%uD(height=%d)", dims, height);
3239 return GL_TRUE;
3240 }
3241 }
3242
3243 if (dims >= 3) {
3244 if (((depth == 1 || depth == 2) &&
3245 depth != (GLsizei) texImage->Depth) ||
3246 (depth > (GLsizei) texImage->Depth)) {
3247 _mesa_error(ctx, GL_INVALID_VALUE,
3248 "glCompressedTexSubImage%uD(depth=%d)", dims, depth);
3249 return GL_TRUE;
3250 }
3251 }
3252
3253 return GL_FALSE;
3254 }
3255
3256
3257 /**
3258 * Implementation of the glCompressedTexImage1/2/3D() functions.
3259 */
3260 static void
3261 compressedteximage(struct gl_context *ctx, GLuint dims,
3262 GLenum target, GLint level,
3263 GLenum internalFormat, GLsizei width,
3264 GLsizei height, GLsizei depth, GLint border,
3265 GLsizei imageSize, const GLvoid *data)
3266 {
3267 GLenum error;
3268 char *reason = "";
3269
3270 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3271
3272 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
3273 _mesa_debug(ctx,
3274 "glCompressedTexImage%uDARB %s %d %s %d %d %d %d %d %p\n",
3275 dims,
3276 _mesa_lookup_enum_by_nr(target), level,
3277 _mesa_lookup_enum_by_nr(internalFormat),
3278 width, height, depth, border, imageSize, data);
3279
3280 /* check target */
3281 if (!legal_teximage_target(ctx, dims, target)) {
3282 _mesa_error(ctx, GL_INVALID_ENUM, "glCompressedTexImage%uD(target=%s)",
3283 dims, _mesa_lookup_enum_by_nr(target));
3284 return;
3285 }
3286
3287 error = compressed_texture_error_check(ctx, dims, target, level,
3288 internalFormat, width, height, depth,
3289 border, imageSize, &reason);
3290
3291 if (error) {
3292 _mesa_error(ctx, error, "glCompressedTexImage%uD(%s)", dims, reason);
3293 return;
3294 }
3295
3296 #if FEATURE_ES
3297 /* XXX this is kind of a hack */
3298 if (dims == 2) {
3299 switch (internalFormat) {
3300 case GL_PALETTE4_RGB8_OES:
3301 case GL_PALETTE4_RGBA8_OES:
3302 case GL_PALETTE4_R5_G6_B5_OES:
3303 case GL_PALETTE4_RGBA4_OES:
3304 case GL_PALETTE4_RGB5_A1_OES:
3305 case GL_PALETTE8_RGB8_OES:
3306 case GL_PALETTE8_RGBA8_OES:
3307 case GL_PALETTE8_R5_G6_B5_OES:
3308 case GL_PALETTE8_RGBA4_OES:
3309 case GL_PALETTE8_RGB5_A1_OES:
3310 _mesa_cpal_compressed_teximage2d(target, level, internalFormat,
3311 width, height, imageSize, data);
3312 return;
3313 }
3314 }
3315 #endif
3316
3317 if (_mesa_is_proxy_texture(target)) {
3318 /* Proxy texture: just check for errors and update proxy state */
3319 struct gl_texture_image *texImage;
3320
3321 if (!error) {
3322 struct gl_texture_object *texObj =
3323 _mesa_get_current_tex_object(ctx, target);
3324 gl_format texFormat =
3325 _mesa_choose_texture_format(ctx, texObj, target, level,
3326 internalFormat, GL_NONE, GL_NONE);
3327 if (!legal_texture_size(ctx, texFormat, width, height, depth)) {
3328 error = GL_OUT_OF_MEMORY;
3329 }
3330 }
3331
3332 texImage = _mesa_get_proxy_tex_image(ctx, target, level);
3333 if (texImage) {
3334 if (error) {
3335 /* if error, clear all proxy texture image parameters */
3336 clear_teximage_fields(texImage);
3337 }
3338 else {
3339 /* no error: store the teximage parameters */
3340 _mesa_init_teximage_fields(ctx, target, texImage, width, height,
3341 depth, border, internalFormat,
3342 MESA_FORMAT_NONE);
3343 }
3344 }
3345 }
3346 else {
3347 /* non-proxy target */
3348 struct gl_texture_object *texObj;
3349 struct gl_texture_image *texImage;
3350
3351 if (error) {
3352 _mesa_error(ctx, error, "glCompressedTexImage%uD", dims);
3353 return;
3354 }
3355
3356 texObj = _mesa_get_current_tex_object(ctx, target);
3357
3358 _mesa_lock_texture(ctx, texObj);
3359 {
3360 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
3361 if (!texImage) {
3362 _mesa_error(ctx, GL_OUT_OF_MEMORY,
3363 "glCompressedTexImage%uD", dims);
3364 }
3365 else {
3366 gl_format texFormat;
3367
3368 if (texImage->Data) {
3369 ctx->Driver.FreeTexImageData( ctx, texImage );
3370 }
3371 ASSERT(texImage->Data == NULL);
3372
3373 texFormat = _mesa_choose_texture_format(ctx, texObj, target, level,
3374 internalFormat, GL_NONE,
3375 GL_NONE);
3376
3377 if (legal_texture_size(ctx, texFormat, width, height, depth)) {
3378 _mesa_init_teximage_fields(ctx, target, texImage,
3379 width, height, depth,
3380 border, internalFormat, texFormat);
3381
3382 switch (dims) {
3383 case 1:
3384 ASSERT(ctx->Driver.CompressedTexImage1D);
3385 ctx->Driver.CompressedTexImage1D(ctx, target, level,
3386 internalFormat,
3387 width,
3388 border, imageSize, data,
3389 texObj, texImage);
3390 break;
3391 case 2:
3392 ASSERT(ctx->Driver.CompressedTexImage2D);
3393 ctx->Driver.CompressedTexImage2D(ctx, target, level,
3394 internalFormat,
3395 width, height,
3396 border, imageSize, data,
3397 texObj, texImage);
3398 break;
3399 case 3:
3400 ASSERT(ctx->Driver.CompressedTexImage3D);
3401 ctx->Driver.CompressedTexImage3D(ctx, target, level,
3402 internalFormat,
3403 width, height, depth,
3404 border, imageSize, data,
3405 texObj, texImage);
3406 break;
3407 default:
3408 _mesa_problem(ctx, "bad dims in compressedteximage");
3409 }
3410
3411 check_gen_mipmap(ctx, target, texObj, level);
3412
3413 /* state update */
3414 texObj->_Complete = GL_FALSE;
3415 ctx->NewState |= _NEW_TEXTURE;
3416 }
3417 else {
3418 _mesa_error(ctx, GL_OUT_OF_MEMORY,
3419 "glCompressedTexImage%uD", dims);
3420 }
3421 }
3422 }
3423 _mesa_unlock_texture(ctx, texObj);
3424 }
3425 }
3426
3427
3428 void GLAPIENTRY
3429 _mesa_CompressedTexImage1DARB(GLenum target, GLint level,
3430 GLenum internalFormat, GLsizei width,
3431 GLint border, GLsizei imageSize,
3432 const GLvoid *data)
3433 {
3434 GET_CURRENT_CONTEXT(ctx);
3435 compressedteximage(ctx, 1, target, level, internalFormat,
3436 width, 1, 1, border, imageSize, data);
3437 }
3438
3439
3440 void GLAPIENTRY
3441 _mesa_CompressedTexImage2DARB(GLenum target, GLint level,
3442 GLenum internalFormat, GLsizei width,
3443 GLsizei height, GLint border, GLsizei imageSize,
3444 const GLvoid *data)
3445 {
3446 GET_CURRENT_CONTEXT(ctx);
3447 compressedteximage(ctx, 2, target, level, internalFormat,
3448 width, height, 1, border, imageSize, data);
3449 }
3450
3451
3452 void GLAPIENTRY
3453 _mesa_CompressedTexImage3DARB(GLenum target, GLint level,
3454 GLenum internalFormat, GLsizei width,
3455 GLsizei height, GLsizei depth, GLint border,
3456 GLsizei imageSize, const GLvoid *data)
3457 {
3458 GET_CURRENT_CONTEXT(ctx);
3459 compressedteximage(ctx, 3, target, level, internalFormat,
3460 width, height, depth, border, imageSize, data);
3461 }
3462
3463
3464 /**
3465 * Common helper for glCompressedTexSubImage1/2/3D().
3466 */
3467 static void
3468 compressed_tex_sub_image(GLuint dims, GLenum target, GLint level,
3469 GLint xoffset, GLint yoffset, GLint zoffset,
3470 GLsizei width, GLsizei height, GLsizei depth,
3471 GLenum format, GLsizei imageSize, const GLvoid *data)
3472 {
3473 struct gl_texture_object *texObj;
3474 struct gl_texture_image *texImage;
3475 GLenum error;
3476 GET_CURRENT_CONTEXT(ctx);
3477 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3478
3479 error = compressed_subtexture_error_check(ctx, dims, target, level,
3480 xoffset, 0, 0, /* pos */
3481 width, height, depth, /* size */
3482 format, imageSize);
3483 if (error) {
3484 _mesa_error(ctx, error, "glCompressedTexSubImage%uD", dims);
3485 return;
3486 }
3487
3488 texObj = _mesa_get_current_tex_object(ctx, target);
3489
3490 _mesa_lock_texture(ctx, texObj);
3491 {
3492 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
3493 assert(texImage);
3494
3495 if (compressed_subtexture_error_check2(ctx, dims, width, height, depth,
3496 format, texImage)) {
3497 /* error was recorded */
3498 }
3499 else if (width > 0 && height > 0 && depth > 0) {
3500 switch (dims) {
3501 case 1:
3502 if (ctx->Driver.CompressedTexSubImage1D) {
3503 ctx->Driver.CompressedTexSubImage1D(ctx, target, level,
3504 xoffset, width,
3505 format, imageSize, data,
3506 texObj, texImage);
3507 }
3508 break;
3509 case 2:
3510 if (ctx->Driver.CompressedTexSubImage2D) {
3511 ctx->Driver.CompressedTexSubImage2D(ctx, target, level,
3512 xoffset, yoffset,
3513 width, height,
3514 format, imageSize, data,
3515 texObj, texImage);
3516 }
3517 break;
3518 case 3:
3519 if (ctx->Driver.CompressedTexSubImage3D) {
3520 ctx->Driver.CompressedTexSubImage3D(ctx, target, level,
3521 xoffset, yoffset, zoffset,
3522 width, height, depth,
3523 format, imageSize, data,
3524 texObj, texImage);
3525 }
3526 break;
3527 default:
3528 ;
3529 }
3530
3531 check_gen_mipmap(ctx, target, texObj, level);
3532
3533 ctx->NewState |= _NEW_TEXTURE;
3534 }
3535 }
3536 _mesa_unlock_texture(ctx, texObj);
3537 }
3538
3539
3540 void GLAPIENTRY
3541 _mesa_CompressedTexSubImage1DARB(GLenum target, GLint level, GLint xoffset,
3542 GLsizei width, GLenum format,
3543 GLsizei imageSize, const GLvoid *data)
3544 {
3545 compressed_tex_sub_image(1, target, level, xoffset, 0, 0, width, 1, 1,
3546 format, imageSize, data);
3547 }
3548
3549
3550 void GLAPIENTRY
3551 _mesa_CompressedTexSubImage2DARB(GLenum target, GLint level, GLint xoffset,
3552 GLint yoffset, GLsizei width, GLsizei height,
3553 GLenum format, GLsizei imageSize,
3554 const GLvoid *data)
3555 {
3556 compressed_tex_sub_image(2, target, level, xoffset, yoffset, 0,
3557 width, height, 1, format, imageSize, data);
3558 }
3559
3560
3561 void GLAPIENTRY
3562 _mesa_CompressedTexSubImage3DARB(GLenum target, GLint level, GLint xoffset,
3563 GLint yoffset, GLint zoffset, GLsizei width,
3564 GLsizei height, GLsizei depth, GLenum format,
3565 GLsizei imageSize, const GLvoid *data)
3566 {
3567 compressed_tex_sub_image(3, target, level, xoffset, yoffset, zoffset,
3568 width, height, depth, format, imageSize, data);
3569 }
3570
3571
3572 /**
3573 * Helper for glTexBuffer(). Check if internalFormat is legal. If so,
3574 * return the basic data type and number of components for the format.
3575 * \param return GL_TRUE if internalFormat is legal, GL_FALSE otherwise
3576 */
3577 static GLboolean
3578 get_sized_format_info(const struct gl_context *ctx, GLenum internalFormat,
3579 GLenum *datatype, GLuint *components)
3580 {
3581 switch (internalFormat) {
3582 case GL_ALPHA8:
3583 *datatype = GL_UNSIGNED_BYTE;
3584 *components = 1;
3585 break;
3586 case GL_ALPHA16:
3587 *datatype = GL_UNSIGNED_SHORT;
3588 *components = 1;
3589 break;
3590 case GL_ALPHA16F_ARB:
3591 *datatype = GL_HALF_FLOAT;
3592 *components = 1;
3593 break;
3594 case GL_ALPHA32F_ARB:
3595 *datatype = GL_FLOAT;
3596 *components = 1;
3597 break;
3598 case GL_ALPHA8I_EXT:
3599 *datatype = GL_BYTE;
3600 *components = 1;
3601 break;
3602 case GL_ALPHA16I_EXT:
3603 *datatype = GL_SHORT;
3604 *components = 1;
3605 break;
3606 case GL_ALPHA32I_EXT:
3607 *datatype = GL_INT;
3608 *components = 1;
3609 break;
3610 case GL_ALPHA8UI_EXT:
3611 *datatype = GL_UNSIGNED_BYTE;
3612 *components = 1;
3613 break;
3614 case GL_ALPHA16UI_EXT:
3615 *datatype = GL_UNSIGNED_SHORT;
3616 *components = 1;
3617 break;
3618 case GL_ALPHA32UI_EXT:
3619 *datatype = GL_UNSIGNED_INT;
3620 *components = 1;
3621 break;
3622 case GL_LUMINANCE8:
3623 *datatype = GL_UNSIGNED_BYTE;
3624 *components = 1;
3625 break;
3626 case GL_LUMINANCE16:
3627 *datatype = GL_UNSIGNED_SHORT;
3628 *components = 1;
3629 break;
3630 case GL_LUMINANCE16F_ARB:
3631 *datatype = GL_HALF_FLOAT;
3632 *components = 1;
3633 break;
3634 case GL_LUMINANCE32F_ARB:
3635 *datatype = GL_FLOAT;
3636 *components = 1;
3637 break;
3638 case GL_LUMINANCE8I_EXT:
3639 *datatype = GL_BYTE;
3640 *components = 1;
3641 break;
3642 case GL_LUMINANCE16I_EXT:
3643 *datatype = GL_SHORT;
3644 *components = 1;
3645 break;
3646 case GL_LUMINANCE32I_EXT:
3647 *datatype = GL_INT;
3648 *components = 1;
3649 break;
3650 case GL_LUMINANCE8UI_EXT:
3651 *datatype = GL_UNSIGNED_BYTE;
3652 *components = 1;
3653 break;
3654 case GL_LUMINANCE16UI_EXT:
3655 *datatype = GL_UNSIGNED_SHORT;
3656 *components = 1;
3657 break;
3658 case GL_LUMINANCE32UI_EXT:
3659 *datatype = GL_UNSIGNED_INT;
3660 *components = 1;
3661 break;
3662 case GL_LUMINANCE8_ALPHA8:
3663 *datatype = GL_UNSIGNED_BYTE;
3664 *components = 2;
3665 break;
3666 case GL_LUMINANCE16_ALPHA16:
3667 *datatype = GL_UNSIGNED_SHORT;
3668 *components = 2;
3669 break;
3670 case GL_LUMINANCE_ALPHA16F_ARB:
3671 *datatype = GL_HALF_FLOAT;
3672 *components = 2;
3673 break;
3674 case GL_LUMINANCE_ALPHA32F_ARB:
3675 *datatype = GL_FLOAT;
3676 *components = 2;
3677 break;
3678 case GL_LUMINANCE_ALPHA8I_EXT:
3679 *datatype = GL_BYTE;
3680 *components = 2;
3681 break;
3682 case GL_LUMINANCE_ALPHA16I_EXT:
3683 *datatype = GL_SHORT;
3684 *components = 2;
3685 break;
3686 case GL_LUMINANCE_ALPHA32I_EXT:
3687 *datatype = GL_INT;
3688 *components = 2;
3689 break;
3690 case GL_LUMINANCE_ALPHA8UI_EXT:
3691 *datatype = GL_UNSIGNED_BYTE;
3692 *components = 2;
3693 break;
3694 case GL_LUMINANCE_ALPHA16UI_EXT:
3695 *datatype = GL_UNSIGNED_SHORT;
3696 *components = 2;
3697 break;
3698 case GL_LUMINANCE_ALPHA32UI_EXT:
3699 *datatype = GL_UNSIGNED_INT;
3700 *components = 2;
3701 break;
3702 case GL_INTENSITY8:
3703 *datatype = GL_UNSIGNED_BYTE;
3704 *components = 1;
3705 break;
3706 case GL_INTENSITY16:
3707 *datatype = GL_UNSIGNED_SHORT;
3708 *components = 1;
3709 break;
3710 case GL_INTENSITY16F_ARB:
3711 *datatype = GL_HALF_FLOAT;
3712 *components = 1;
3713 break;
3714 case GL_INTENSITY32F_ARB:
3715 *datatype = GL_FLOAT;
3716 *components = 1;
3717 break;
3718 case GL_INTENSITY8I_EXT:
3719 *datatype = GL_BYTE;
3720 *components = 1;
3721 break;
3722 case GL_INTENSITY16I_EXT:
3723 *datatype = GL_SHORT;
3724 *components = 1;
3725 break;
3726 case GL_INTENSITY32I_EXT:
3727 *datatype = GL_INT;
3728 *components = 1;
3729 break;
3730 case GL_INTENSITY8UI_EXT:
3731 *datatype = GL_UNSIGNED_BYTE;
3732 *components = 1;
3733 break;
3734 case GL_INTENSITY16UI_EXT:
3735 *datatype = GL_UNSIGNED_SHORT;
3736 *components = 1;
3737 break;
3738 case GL_INTENSITY32UI_EXT:
3739 *datatype = GL_UNSIGNED_INT;
3740 *components = 1;
3741 break;
3742 case GL_RGBA8:
3743 *datatype = GL_UNSIGNED_BYTE;
3744 *components = 4;
3745 break;
3746 case GL_RGBA16:
3747 *datatype = GL_UNSIGNED_SHORT;
3748 *components = 4;
3749 break;
3750 case GL_RGBA16F_ARB:
3751 *datatype = GL_HALF_FLOAT;
3752 *components = 4;
3753 break;
3754 case GL_RGBA32F_ARB:
3755 *datatype = GL_FLOAT;
3756 *components = 4;
3757 break;
3758 case GL_RGBA8I_EXT:
3759 *datatype = GL_BYTE;
3760 *components = 4;
3761 break;
3762 case GL_RGBA16I_EXT:
3763 *datatype = GL_SHORT;
3764 *components = 4;
3765 break;
3766 case GL_RGBA32I_EXT:
3767 *datatype = GL_INT;
3768 *components = 4;
3769 break;
3770 case GL_RGBA8UI_EXT:
3771 *datatype = GL_UNSIGNED_BYTE;
3772 *components = 4;
3773 break;
3774 case GL_RGBA16UI_EXT:
3775 *datatype = GL_UNSIGNED_SHORT;
3776 *components = 4;
3777 break;
3778 case GL_RGBA32UI_EXT:
3779 *datatype = GL_UNSIGNED_INT;
3780 *components = 4;
3781 break;
3782 default:
3783 return GL_FALSE;
3784 }
3785
3786 if (*datatype == GL_FLOAT && !ctx->Extensions.ARB_texture_float)
3787 return GL_FALSE;
3788
3789 if (*datatype == GL_HALF_FLOAT && !ctx->Extensions.ARB_half_float_pixel)
3790 return GL_FALSE;
3791
3792 return GL_TRUE;
3793 }
3794
3795
3796 /** GL_ARB_texture_buffer_object */
3797 void GLAPIENTRY
3798 _mesa_TexBuffer(GLenum target, GLenum internalFormat, GLuint buffer)
3799 {
3800 struct gl_texture_object *texObj;
3801 struct gl_buffer_object *bufObj;
3802 GLenum dataType;
3803 GLuint comps;
3804
3805 GET_CURRENT_CONTEXT(ctx);
3806 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3807
3808 if (!ctx->Extensions.ARB_texture_buffer_object) {
3809 _mesa_error(ctx, GL_INVALID_OPERATION, "glTexBuffer");
3810 return;
3811 }
3812
3813 if (target != GL_TEXTURE_BUFFER_ARB) {
3814 _mesa_error(ctx, GL_INVALID_ENUM, "glTexBuffer(target)");
3815 return;
3816 }
3817
3818 if (!get_sized_format_info(ctx, internalFormat, &dataType, &comps)) {
3819 _mesa_error(ctx, GL_INVALID_ENUM, "glTexBuffer(internalFormat 0x%x)",
3820 internalFormat);
3821 return;
3822 }
3823
3824 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
3825 if (buffer && !bufObj) {
3826 _mesa_error(ctx, GL_INVALID_OPERATION, "glTexBuffer(buffer %u)", buffer);
3827 return;
3828 }
3829
3830 texObj = _mesa_get_current_tex_object(ctx, target);
3831
3832 _mesa_lock_texture(ctx, texObj);
3833 {
3834 _mesa_reference_buffer_object(ctx, &texObj->BufferObject, bufObj);
3835 texObj->BufferObjectFormat = internalFormat;
3836 }
3837 _mesa_unlock_texture(ctx, texObj);
3838 }