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