b9625dc247b0f06248406a9b6d52e258483e5a00
[mesa.git] / src / mesa / main / teximage.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.3
4 *
5 * Copyright (C) 1999-2004 Brian Paul All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25
26 /**
27 * \file teximage.c
28 * Texture images manipulation functions.
29 *
30 * \note Mesa's native texture data type is GLchan. Native formats are
31 * GL_ALPHA, GL_LUMINANCE, GL_LUMANCE_ALPHA, GL_INTENSITY, GL_RGB, GL_RGBA, and
32 * GL_COLOR_INDEX.
33 *
34 * \note Device drivers are free to implement any internal format they want.
35 */
36
37
38 #include "glheader.h"
39 #include "bufferobj.h"
40 #include "context.h"
41 #include "convolve.h"
42 #include "image.h"
43 #include "imports.h"
44 #include "macros.h"
45 #include "state.h"
46 #include "texcompress.h"
47 #include "texformat.h"
48 #include "teximage.h"
49 #include "texstate.h"
50 #include "texstore.h"
51 #include "mtypes.h"
52
53
54 #if 0
55 static void PrintTexture(GLcontext *ctx, const struct gl_texture_image *img)
56 {
57 #if CHAN_TYPE == GL_FLOAT
58 _mesa_problem(NULL, "PrintTexture doesn't support float channels");
59 #else
60 GLuint i, j, c;
61 const GLchan *data = (const GLchan *) img->Data;
62
63 if (!data) {
64 _mesa_printf("No texture data\n");
65 return;
66 }
67
68 switch (img->Format) {
69 case GL_ALPHA:
70 case GL_LUMINANCE:
71 case GL_INTENSITY:
72 case GL_COLOR_INDEX:
73 c = 1;
74 break;
75 case GL_LUMINANCE_ALPHA:
76 c = 2;
77 break;
78 case GL_RGB:
79 c = 3;
80 break;
81 case GL_RGBA:
82 c = 4;
83 break;
84 default:
85 _mesa_problem(NULL, "error in PrintTexture\n");
86 return;
87 }
88
89 for (i = 0; i < img->Height; i++) {
90 for (j = 0; j < img->Width; j++) {
91 if (c==1)
92 _mesa_printf("%02x ", data[0]);
93 else if (c==2)
94 _mesa_printf("%02x%02x ", data[0], data[1]);
95 else if (c==3)
96 _mesa_printf("%02x%02x%02x ", data[0], data[1], data[2]);
97 else if (c==4)
98 _mesa_printf("%02x%02x%02x%02x ", data[0], data[1], data[2], data[3]);
99 data += (img->RowStride - img->Width) * c;
100 }
101 _mesa_printf("\n");
102 }
103 #endif
104 }
105 #endif
106
107
108 /*
109 * Compute floor(log_base_2(n)).
110 * If n < 0 return -1.
111 */
112 static int
113 logbase2( int n )
114 {
115 GLint i = 1;
116 GLint log2 = 0;
117
118 if (n < 0)
119 return -1;
120
121 if (n == 0)
122 return 0;
123
124 while ( n > i ) {
125 i *= 2;
126 log2++;
127 }
128 if (i != n) {
129 return log2 - 1;
130 }
131 else {
132 return log2;
133 }
134 }
135
136
137
138 /**
139 * Return the simple base format for a given internal texture format.
140 * For example, given GL_LUMINANCE12_ALPHA4, return GL_LUMINANCE_ALPHA.
141 *
142 * \param ctx GL context.
143 * \param internalFormat the internal texture format token or 1, 2, 3, or 4.
144 *
145 * \return the corresponding \u base internal format (GL_ALPHA, GL_LUMINANCE,
146 * GL_LUMANCE_ALPHA, GL_INTENSITY, GL_RGB, or GL_RGBA), or -1 if invalid enum.
147 *
148 * This is the format which is used during texture application (i.e. the
149 * texture format and env mode determine the arithmetic used.
150 */
151 GLint
152 _mesa_base_tex_format( GLcontext *ctx, GLint internalFormat )
153 {
154 switch (internalFormat) {
155 case GL_ALPHA:
156 case GL_ALPHA4:
157 case GL_ALPHA8:
158 case GL_ALPHA12:
159 case GL_ALPHA16:
160 return GL_ALPHA;
161 case 1:
162 case GL_LUMINANCE:
163 case GL_LUMINANCE4:
164 case GL_LUMINANCE8:
165 case GL_LUMINANCE12:
166 case GL_LUMINANCE16:
167 return GL_LUMINANCE;
168 case 2:
169 case GL_LUMINANCE_ALPHA:
170 case GL_LUMINANCE4_ALPHA4:
171 case GL_LUMINANCE6_ALPHA2:
172 case GL_LUMINANCE8_ALPHA8:
173 case GL_LUMINANCE12_ALPHA4:
174 case GL_LUMINANCE12_ALPHA12:
175 case GL_LUMINANCE16_ALPHA16:
176 return GL_LUMINANCE_ALPHA;
177 case GL_INTENSITY:
178 case GL_INTENSITY4:
179 case GL_INTENSITY8:
180 case GL_INTENSITY12:
181 case GL_INTENSITY16:
182 return GL_INTENSITY;
183 case 3:
184 case GL_RGB:
185 case GL_R3_G3_B2:
186 case GL_RGB4:
187 case GL_RGB5:
188 case GL_RGB8:
189 case GL_RGB10:
190 case GL_RGB12:
191 case GL_RGB16:
192 return GL_RGB;
193 case 4:
194 case GL_RGBA:
195 case GL_RGBA2:
196 case GL_RGBA4:
197 case GL_RGB5_A1:
198 case GL_RGBA8:
199 case GL_RGB10_A2:
200 case GL_RGBA12:
201 case GL_RGBA16:
202 return GL_RGBA;
203 default:
204 ; /* fallthrough */
205 }
206
207 if (ctx->Extensions.EXT_paletted_texture) {
208 switch (internalFormat) {
209 case GL_COLOR_INDEX:
210 case GL_COLOR_INDEX1_EXT:
211 case GL_COLOR_INDEX2_EXT:
212 case GL_COLOR_INDEX4_EXT:
213 case GL_COLOR_INDEX8_EXT:
214 case GL_COLOR_INDEX12_EXT:
215 case GL_COLOR_INDEX16_EXT:
216 return GL_COLOR_INDEX;
217 default:
218 ; /* fallthrough */
219 }
220 }
221
222 if (ctx->Extensions.SGIX_depth_texture) {
223 switch (internalFormat) {
224 case GL_DEPTH_COMPONENT:
225 case GL_DEPTH_COMPONENT16_SGIX:
226 case GL_DEPTH_COMPONENT24_SGIX:
227 case GL_DEPTH_COMPONENT32_SGIX:
228 return GL_DEPTH_COMPONENT;
229 default:
230 ; /* fallthrough */
231 }
232 }
233
234 if (ctx->Extensions.ARB_texture_compression) {
235 switch (internalFormat) {
236 case GL_COMPRESSED_ALPHA:
237 return GL_ALPHA;
238 case GL_COMPRESSED_LUMINANCE:
239 return GL_LUMINANCE;
240 case GL_COMPRESSED_LUMINANCE_ALPHA:
241 return GL_LUMINANCE_ALPHA;
242 case GL_COMPRESSED_INTENSITY:
243 return GL_INTENSITY;
244 case GL_COMPRESSED_RGB:
245 return GL_RGB;
246 case GL_COMPRESSED_RGBA:
247 return GL_RGBA;
248 default:
249 ; /* fallthrough */
250 }
251 }
252
253 if (ctx->Extensions.TDFX_texture_compression_FXT1) {
254 switch (internalFormat) {
255 case GL_COMPRESSED_RGB_FXT1_3DFX:
256 return GL_RGB;
257 case GL_COMPRESSED_RGBA_FXT1_3DFX:
258 return GL_RGBA;
259 default:
260 ; /* fallthrough */
261 }
262 }
263
264 if (ctx->Extensions.EXT_texture_compression_s3tc) {
265 switch (internalFormat) {
266 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
267 return GL_RGB;
268 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
269 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
270 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
271 return GL_RGBA;
272 default:
273 ; /* fallthrough */
274 }
275 }
276
277 if (ctx->Extensions.S3_s3tc) {
278 switch (internalFormat) {
279 case GL_RGB_S3TC:
280 case GL_RGB4_S3TC:
281 return GL_RGB;
282 case GL_RGBA_S3TC:
283 case GL_RGBA4_S3TC:
284 return GL_RGBA;
285 default:
286 ; /* fallthrough */
287 }
288 }
289
290 if (ctx->Extensions.MESA_ycbcr_texture) {
291 if (internalFormat == GL_YCBCR_MESA)
292 return GL_YCBCR_MESA;
293 }
294
295 if (ctx->Extensions.ARB_texture_float) {
296 switch (internalFormat) {
297 case GL_ALPHA16F_ARB:
298 case GL_ALPHA32F_ARB:
299 return GL_ALPHA;
300 case GL_RGBA16F_ARB:
301 case GL_RGBA32F_ARB:
302 return GL_RGBA;
303 case GL_RGB16F_ARB:
304 case GL_RGB32F_ARB:
305 return GL_RGB;
306 case GL_INTENSITY16F_ARB:
307 case GL_INTENSITY32F_ARB:
308 return GL_INTENSITY;
309 case GL_LUMINANCE16F_ARB:
310 case GL_LUMINANCE32F_ARB:
311 return GL_LUMINANCE;
312 case GL_LUMINANCE_ALPHA16F_ARB:
313 case GL_LUMINANCE_ALPHA32F_ARB:
314 return GL_LUMINANCE_ALPHA;
315 default:
316 ; /* fallthrough */
317 }
318 }
319
320 return -1; /* error */
321 }
322
323
324 /**
325 * Test if the given image format is a color/RGBA format (i.e., not color
326 * index, depth, stencil, etc).
327 * \param format the image format value (may by an internal texture format)
328 * \return GL_TRUE if its a color/RGBA format, GL_FALSE otherwise.
329 */
330 static GLboolean
331 is_color_format(GLenum format)
332 {
333 switch (format) {
334 case GL_RED:
335 case GL_GREEN:
336 case GL_BLUE:
337 case GL_ALPHA:
338 case GL_ALPHA4:
339 case GL_ALPHA8:
340 case GL_ALPHA12:
341 case GL_ALPHA16:
342 case 1:
343 case GL_LUMINANCE:
344 case GL_LUMINANCE4:
345 case GL_LUMINANCE8:
346 case GL_LUMINANCE12:
347 case GL_LUMINANCE16:
348 case 2:
349 case GL_LUMINANCE_ALPHA:
350 case GL_LUMINANCE4_ALPHA4:
351 case GL_LUMINANCE6_ALPHA2:
352 case GL_LUMINANCE8_ALPHA8:
353 case GL_LUMINANCE12_ALPHA4:
354 case GL_LUMINANCE12_ALPHA12:
355 case GL_LUMINANCE16_ALPHA16:
356 case GL_INTENSITY:
357 case GL_INTENSITY4:
358 case GL_INTENSITY8:
359 case GL_INTENSITY12:
360 case GL_INTENSITY16:
361 case 3:
362 case GL_RGB:
363 case GL_BGR:
364 case GL_R3_G3_B2:
365 case GL_RGB4:
366 case GL_RGB5:
367 case GL_RGB8:
368 case GL_RGB10:
369 case GL_RGB12:
370 case GL_RGB16:
371 case 4:
372 case GL_ABGR_EXT:
373 case GL_RGBA:
374 case GL_BGRA:
375 case GL_RGBA2:
376 case GL_RGBA4:
377 case GL_RGB5_A1:
378 case GL_RGBA8:
379 case GL_RGB10_A2:
380 case GL_RGBA12:
381 case GL_RGBA16:
382 /* float texture formats */
383 case GL_ALPHA16F_ARB:
384 case GL_ALPHA32F_ARB:
385 case GL_LUMINANCE16F_ARB:
386 case GL_LUMINANCE32F_ARB:
387 case GL_LUMINANCE_ALPHA16F_ARB:
388 case GL_LUMINANCE_ALPHA32F_ARB:
389 case GL_INTENSITY16F_ARB:
390 case GL_INTENSITY32F_ARB:
391 case GL_RGB16F_ARB:
392 case GL_RGB32F_ARB:
393 case GL_RGBA16F_ARB:
394 case GL_RGBA32F_ARB:
395 /* compressed formats */
396 case GL_COMPRESSED_ALPHA:
397 case GL_COMPRESSED_LUMINANCE:
398 case GL_COMPRESSED_LUMINANCE_ALPHA:
399 case GL_COMPRESSED_INTENSITY:
400 case GL_COMPRESSED_RGB:
401 case GL_COMPRESSED_RGBA:
402 case GL_RGB_S3TC:
403 case GL_RGB4_S3TC:
404 case GL_RGBA_S3TC:
405 case GL_RGBA4_S3TC:
406 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
407 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
408 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
409 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
410 case GL_COMPRESSED_RGB_FXT1_3DFX:
411 case GL_COMPRESSED_RGBA_FXT1_3DFX:
412 return GL_TRUE;
413 case GL_YCBCR_MESA: /* not considered to be RGB */
414 default:
415 return GL_FALSE;
416 }
417 }
418
419
420 /**
421 * Test if the given image format is a color index format.
422 */
423 static GLboolean
424 is_index_format(GLenum format)
425 {
426 switch (format) {
427 case GL_COLOR_INDEX:
428 case GL_COLOR_INDEX1_EXT:
429 case GL_COLOR_INDEX2_EXT:
430 case GL_COLOR_INDEX4_EXT:
431 case GL_COLOR_INDEX8_EXT:
432 case GL_COLOR_INDEX12_EXT:
433 case GL_COLOR_INDEX16_EXT:
434 return GL_TRUE;
435 default:
436 return GL_FALSE;
437 }
438 }
439
440
441 /**
442 * Test if the given image format is a depth component format.
443 */
444 static GLboolean
445 is_depth_format(GLenum format)
446 {
447 switch (format) {
448 case GL_DEPTH_COMPONENT16_ARB:
449 case GL_DEPTH_COMPONENT24_ARB:
450 case GL_DEPTH_COMPONENT32_ARB:
451 case GL_DEPTH_COMPONENT:
452 return GL_TRUE;
453 default:
454 return GL_FALSE;
455 }
456 }
457
458
459 /**
460 * Test if the given image format is a YCbCr format.
461 */
462 static GLboolean
463 is_ycbcr_format(GLenum format)
464 {
465 switch (format) {
466 case GL_YCBCR_MESA:
467 return GL_TRUE;
468 default:
469 return GL_FALSE;
470 }
471 }
472
473
474 /**
475 * Test if it is a supported compressed format.
476 *
477 * \param internalFormat the internal format token provided by the user.
478 *
479 * \ret GL_TRUE if \p internalFormat is a supported compressed format, or
480 * GL_FALSE otherwise.
481 *
482 * Currently only GL_COMPRESSED_RGB_FXT1_3DFX and GL_COMPRESSED_RGBA_FXT1_3DFX
483 * are supported.
484 */
485 static GLboolean
486 is_compressed_format(GLcontext *ctx, GLenum internalFormat)
487 {
488 (void) ctx;
489 switch (internalFormat) {
490 case GL_COMPRESSED_RGB_FXT1_3DFX:
491 case GL_COMPRESSED_RGBA_FXT1_3DFX:
492 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
493 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
494 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
495 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
496 case GL_RGB_S3TC:
497 case GL_RGB4_S3TC:
498 case GL_RGBA_S3TC:
499 case GL_RGBA4_S3TC:
500 return GL_TRUE;
501 default:
502 return GL_FALSE;
503 }
504 }
505
506
507 /**
508 * Store a gl_texture_image pointer in a gl_texture_object structure
509 * according to the target and level parameters.
510 *
511 * \param tObj texture object.
512 * \param target texture target.
513 * \param level image level.
514 * \param texImage texture image.
515 *
516 * This was basically prompted by the introduction of cube maps.
517 */
518 void
519 _mesa_set_tex_image(struct gl_texture_object *tObj,
520 GLenum target, GLint level,
521 struct gl_texture_image *texImage)
522 {
523 ASSERT(tObj);
524 ASSERT(texImage);
525 switch (target) {
526 case GL_TEXTURE_1D:
527 case GL_TEXTURE_2D:
528 case GL_TEXTURE_3D:
529 tObj->Image[0][level] = texImage;
530 break;
531 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
532 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
533 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
534 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
535 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
536 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB: {
537 GLuint face = ((GLuint) target -
538 (GLuint) GL_TEXTURE_CUBE_MAP_POSITIVE_X);
539 tObj->Image[face][level] = texImage;
540 break;
541 }
542 case GL_TEXTURE_RECTANGLE_NV:
543 ASSERT(level == 0);
544 tObj->Image[0][level] = texImage;
545 break;
546 default:
547 _mesa_problem(NULL, "bad target in _mesa_set_tex_image()");
548 return;
549 }
550 /* Set the 'back' pointer */
551 texImage->TexObject = tObj;
552 }
553
554
555 /**
556 * Allocate a texture image structure.
557 *
558 * Called via ctx->Driver.NewTextureImage() unless overriden by a device
559 * driver.
560 *
561 * \return a pointer to gl_texture_image struct with all fields initialized to
562 * zero.
563 */
564 struct gl_texture_image *
565 _mesa_new_texture_image( GLcontext *ctx )
566 {
567 (void) ctx;
568 return CALLOC_STRUCT(gl_texture_image);
569 }
570
571
572 /**
573 * Free texture image.
574 *
575 * \param teximage texture image.
576 *
577 * Free the texture image structure and the associated image data if it's not
578 * marked as client data.
579 */
580 void
581 _mesa_delete_texture_image( struct gl_texture_image *teximage )
582 {
583 if (teximage->Data && !teximage->IsClientData) {
584 MESA_PBUFFER_FREE( teximage->Data );
585 teximage->Data = NULL;
586 }
587 FREE( teximage );
588 }
589
590
591 /**
592 * Test if a target is a proxy target.
593 *
594 * \param target texture target.
595 *
596 * \return GL_TRUE if the target is a proxy target, GL_FALSE otherwise.
597 */
598 static GLboolean
599 is_proxy_target(GLenum target)
600 {
601 return (target == GL_PROXY_TEXTURE_1D ||
602 target == GL_PROXY_TEXTURE_2D ||
603 target == GL_PROXY_TEXTURE_3D ||
604 target == GL_PROXY_TEXTURE_CUBE_MAP_ARB ||
605 target == GL_PROXY_TEXTURE_RECTANGLE_NV);
606 }
607
608
609 /**
610 * Get the texture object that corresponds to the target of the given texture unit.
611 *
612 * \param ctx GL context.
613 * \param texUnit texture unit.
614 * \param target texture target.
615 *
616 * \return pointer to the texture object on success, or NULL on failure.
617 *
618 * \sa gl_texture_unit.
619 */
620 struct gl_texture_object *
621 _mesa_select_tex_object(GLcontext *ctx, const struct gl_texture_unit *texUnit,
622 GLenum target)
623 {
624 switch (target) {
625 case GL_TEXTURE_1D:
626 return texUnit->Current1D;
627 case GL_PROXY_TEXTURE_1D:
628 return ctx->Texture.Proxy1D;
629 case GL_TEXTURE_2D:
630 return texUnit->Current2D;
631 case GL_PROXY_TEXTURE_2D:
632 return ctx->Texture.Proxy2D;
633 case GL_TEXTURE_3D:
634 return texUnit->Current3D;
635 case GL_PROXY_TEXTURE_3D:
636 return ctx->Texture.Proxy3D;
637 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
638 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
639 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
640 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
641 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
642 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
643 case GL_TEXTURE_CUBE_MAP_ARB:
644 return ctx->Extensions.ARB_texture_cube_map
645 ? texUnit->CurrentCubeMap : NULL;
646 case GL_PROXY_TEXTURE_CUBE_MAP_ARB:
647 return ctx->Extensions.ARB_texture_cube_map
648 ? ctx->Texture.ProxyCubeMap : NULL;
649 case GL_TEXTURE_RECTANGLE_NV:
650 return ctx->Extensions.NV_texture_rectangle
651 ? texUnit->CurrentRect : NULL;
652 case GL_PROXY_TEXTURE_RECTANGLE_NV:
653 return ctx->Extensions.NV_texture_rectangle
654 ? ctx->Texture.ProxyRect : NULL;
655 default:
656 _mesa_problem(NULL, "bad target in _mesa_select_tex_object()");
657 return NULL;
658 }
659 }
660
661
662 /**
663 * Get the texture image struct which corresponds to target and level
664 * of the given texture unit.
665 *
666 * \param ctx GL context.
667 * \param texUnit texture unit.
668 * \param target texture target.
669 * \param level image level.
670 *
671 * \return pointer to the texture image structure on success, or NULL on failure.
672 *
673 * \sa gl_texture_unit.
674 */
675 struct gl_texture_image *
676 _mesa_select_tex_image(GLcontext *ctx, const struct gl_texture_unit *texUnit,
677 GLenum target, GLint level)
678 {
679 ASSERT(texUnit);
680 ASSERT(level < MAX_TEXTURE_LEVELS);
681 switch (target) {
682 case GL_TEXTURE_1D:
683 return texUnit->Current1D->Image[0][level];
684 case GL_PROXY_TEXTURE_1D:
685 return ctx->Texture.Proxy1D->Image[0][level];
686 case GL_TEXTURE_2D:
687 return texUnit->Current2D->Image[0][level];
688 case GL_PROXY_TEXTURE_2D:
689 return ctx->Texture.Proxy2D->Image[0][level];
690 case GL_TEXTURE_3D:
691 return texUnit->Current3D->Image[0][level];
692 case GL_PROXY_TEXTURE_3D:
693 return ctx->Texture.Proxy3D->Image[0][level];
694 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
695 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
696 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
697 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
698 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
699 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
700 if (ctx->Extensions.ARB_texture_cube_map) {
701 GLuint face = ((GLuint) target -
702 (GLuint) GL_TEXTURE_CUBE_MAP_POSITIVE_X);
703 return texUnit->CurrentCubeMap->Image[face][level];
704 }
705 else
706 return NULL;
707 case GL_PROXY_TEXTURE_CUBE_MAP_ARB:
708 if (ctx->Extensions.ARB_texture_cube_map)
709 return ctx->Texture.ProxyCubeMap->Image[0][level];
710 else
711 return NULL;
712 case GL_TEXTURE_RECTANGLE_NV:
713 if (ctx->Extensions.NV_texture_rectangle) {
714 ASSERT(level == 0);
715 return texUnit->CurrentRect->Image[0][level];
716 }
717 else {
718 return NULL;
719 }
720 case GL_PROXY_TEXTURE_RECTANGLE_NV:
721 if (ctx->Extensions.NV_texture_rectangle) {
722 ASSERT(level == 0);
723 return ctx->Texture.ProxyRect->Image[0][level];
724 }
725 else {
726 return NULL;
727 }
728 default:
729 _mesa_problem(ctx, "bad target in _mesa_select_tex_image()");
730 return NULL;
731 }
732 }
733
734
735 /**
736 * Like _mesa_select_tex_image() but if the image doesn't exist, allocate
737 * it and install it. Only return NULL if passed a bad parameter or run
738 * out of memory.
739 */
740 struct gl_texture_image *
741 _mesa_get_tex_image(GLcontext *ctx, const struct gl_texture_unit *texUnit,
742 GLenum target, GLint level)
743 {
744 struct gl_texture_image *texImage;
745 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
746 if (!texImage) {
747 struct gl_texture_object *texObj;
748 texImage = ctx->Driver.NewTextureImage(ctx);
749 if (!texImage) {
750 _mesa_error(ctx, GL_OUT_OF_MEMORY, "texture image allocation");
751 return NULL;
752 }
753 texObj = _mesa_select_tex_object(ctx, texUnit, target);
754 ASSERT(texObj);
755 _mesa_set_tex_image(texObj, target, level, texImage);
756 }
757 return texImage;
758 }
759
760
761 /**
762 * Return pointer to the specified proxy texture image.
763 * Note that proxy textures are per-context, not per-texture unit.
764 * \return pointer to texture image or NULL if invalid target, invalid
765 * level, or out of memory.
766 */
767 struct gl_texture_image *
768 _mesa_get_proxy_tex_image(GLcontext *ctx, GLenum target, GLint level)
769 {
770 struct gl_texture_image *texImage;
771
772 if (level < 0 )
773 return NULL;
774
775 switch (target) {
776 case GL_PROXY_TEXTURE_1D:
777 if (level >= ctx->Const.MaxTextureLevels)
778 return NULL;
779 texImage = ctx->Texture.Proxy1D->Image[0][level];
780 if (!texImage) {
781 texImage = ctx->Driver.NewTextureImage(ctx);
782 if (!texImage) {
783 _mesa_error(ctx, GL_OUT_OF_MEMORY, "proxy texture allocation");
784 return NULL;
785 }
786 ctx->Texture.Proxy1D->Image[0][level] = texImage;
787 /* Set the 'back' pointer */
788 texImage->TexObject = ctx->Texture.Proxy1D;
789 }
790 return texImage;
791 case GL_PROXY_TEXTURE_2D:
792 if (level >= ctx->Const.MaxTextureLevels)
793 return NULL;
794 texImage = ctx->Texture.Proxy2D->Image[0][level];
795 if (!texImage) {
796 texImage = ctx->Driver.NewTextureImage(ctx);
797 if (!texImage) {
798 _mesa_error(ctx, GL_OUT_OF_MEMORY, "proxy texture allocation");
799 return NULL;
800 }
801 ctx->Texture.Proxy2D->Image[0][level] = texImage;
802 /* Set the 'back' pointer */
803 texImage->TexObject = ctx->Texture.Proxy2D;
804 }
805 return texImage;
806 case GL_PROXY_TEXTURE_3D:
807 if (level >= ctx->Const.Max3DTextureLevels)
808 return NULL;
809 texImage = ctx->Texture.Proxy3D->Image[0][level];
810 if (!texImage) {
811 texImage = ctx->Driver.NewTextureImage(ctx);
812 if (!texImage) {
813 _mesa_error(ctx, GL_OUT_OF_MEMORY, "proxy texture allocation");
814 return NULL;
815 }
816 ctx->Texture.Proxy3D->Image[0][level] = texImage;
817 /* Set the 'back' pointer */
818 texImage->TexObject = ctx->Texture.Proxy3D;
819 }
820 return texImage;
821 case GL_PROXY_TEXTURE_CUBE_MAP:
822 if (level >= ctx->Const.MaxCubeTextureLevels)
823 return NULL;
824 texImage = ctx->Texture.ProxyCubeMap->Image[0][level];
825 if (!texImage) {
826 texImage = ctx->Driver.NewTextureImage(ctx);
827 if (!texImage) {
828 _mesa_error(ctx, GL_OUT_OF_MEMORY, "proxy texture allocation");
829 return NULL;
830 }
831 ctx->Texture.ProxyCubeMap->Image[0][level] = texImage;
832 /* Set the 'back' pointer */
833 texImage->TexObject = ctx->Texture.ProxyCubeMap;
834 }
835 return texImage;
836 case GL_PROXY_TEXTURE_RECTANGLE_NV:
837 if (level > 0)
838 return NULL;
839 texImage = ctx->Texture.ProxyRect->Image[0][level];
840 if (!texImage) {
841 texImage = ctx->Driver.NewTextureImage(ctx);
842 if (!texImage) {
843 _mesa_error(ctx, GL_OUT_OF_MEMORY, "proxy texture allocation");
844 return NULL;
845 }
846 ctx->Texture.ProxyRect->Image[0][level] = texImage;
847 /* Set the 'back' pointer */
848 texImage->TexObject = ctx->Texture.ProxyRect;
849 }
850 return texImage;
851 default:
852 return NULL;
853 }
854 }
855
856
857 /**
858 * Get the maximum number of allowed mipmap levels.
859 *
860 * \param ctx GL context.
861 * \param target texture target.
862 *
863 * \return the maximum number of allowed mipmap levels for the given
864 * texture target, or zero if passed a bad target.
865 *
866 * \sa gl_constants.
867 */
868 GLint
869 _mesa_max_texture_levels(GLcontext *ctx, GLenum target)
870 {
871 switch (target) {
872 case GL_TEXTURE_1D:
873 case GL_PROXY_TEXTURE_1D:
874 case GL_TEXTURE_2D:
875 case GL_PROXY_TEXTURE_2D:
876 return ctx->Const.MaxTextureLevels;
877 case GL_TEXTURE_3D:
878 case GL_PROXY_TEXTURE_3D:
879 return ctx->Const.Max3DTextureLevels;
880 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
881 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
882 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
883 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
884 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
885 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
886 case GL_TEXTURE_CUBE_MAP_ARB:
887 case GL_PROXY_TEXTURE_CUBE_MAP_ARB:
888 return ctx->Const.MaxCubeTextureLevels;
889 case GL_TEXTURE_RECTANGLE_NV:
890 case GL_PROXY_TEXTURE_RECTANGLE_NV:
891 return 1;
892 default:
893 return 0; /* bad target */
894 }
895 }
896
897
898
899 #if 000 /* not used anymore */
900 /*
901 * glTexImage[123]D can accept a NULL image pointer. In this case we
902 * create a texture image with unspecified image contents per the OpenGL
903 * spec.
904 */
905 static GLubyte *
906 make_null_texture(GLint width, GLint height, GLint depth, GLenum format)
907 {
908 const GLint components = _mesa_components_in_format(format);
909 const GLint numPixels = width * height * depth;
910 GLubyte *data = (GLubyte *) MALLOC(numPixels * components * sizeof(GLubyte));
911
912 #ifdef DEBUG
913 /*
914 * Let's see if anyone finds this. If glTexImage2D() is called with
915 * a NULL image pointer then load the texture image with something
916 * interesting instead of leaving it indeterminate.
917 */
918 if (data) {
919 static const char message[8][32] = {
920 " X X XXXXX XXX X ",
921 " XX XX X X X X X ",
922 " X X X X X X X ",
923 " X X XXXX XXX XXXXX ",
924 " X X X X X X ",
925 " X X X X X X X ",
926 " X X XXXXX XXX X X ",
927 " "
928 };
929
930 GLubyte *imgPtr = data;
931 GLint h, i, j, k;
932 for (h = 0; h < depth; h++) {
933 for (i = 0; i < height; i++) {
934 GLint srcRow = 7 - (i % 8);
935 for (j = 0; j < width; j++) {
936 GLint srcCol = j % 32;
937 GLubyte texel = (message[srcRow][srcCol]=='X') ? 255 : 70;
938 for (k = 0; k < components; k++) {
939 *imgPtr++ = texel;
940 }
941 }
942 }
943 }
944 }
945 #endif
946
947 return data;
948 }
949 #endif
950
951
952
953 /**
954 * Reset the fields of a gl_texture_image struct to zero.
955 *
956 * \param img texture image structure.
957 *
958 * This is called when a proxy texture test fails, we set all the
959 * image members (except DriverData) to zero.
960 * It's also used in glTexImage[123]D as a safeguard to be sure all
961 * required fields get initialized properly by the Driver.TexImage[123]D
962 * functions.
963 */
964 static void
965 clear_teximage_fields(struct gl_texture_image *img)
966 {
967 ASSERT(img);
968 img->Format = 0;
969 img->IntFormat = 0;
970 img->Border = 0;
971 img->Width = 0;
972 img->Height = 0;
973 img->Depth = 0;
974 img->RowStride = 0;
975 img->Width2 = 0;
976 img->Height2 = 0;
977 img->Depth2 = 0;
978 img->WidthLog2 = 0;
979 img->HeightLog2 = 0;
980 img->DepthLog2 = 0;
981 img->Data = NULL;
982 img->TexFormat = &_mesa_null_texformat;
983 img->FetchTexelc = NULL;
984 img->FetchTexelf = NULL;
985 img->IsCompressed = 0;
986 img->CompressedSize = 0;
987 }
988
989
990 /**
991 * Initialize basic fields of the gl_texture_image struct.
992 *
993 * \param ctx GL context.
994 * \param target texture target.
995 * \param img texture image structure to be initialized.
996 * \param width image width.
997 * \param height image height.
998 * \param depth image depth.
999 * \param border image border.
1000 * \param internalFormat internal format.
1001 *
1002 * Fills in the fields of \p img with the given information.
1003 * Note: width, height and depth include the border.
1004 */
1005 void
1006 _mesa_init_teximage_fields(GLcontext *ctx, GLenum target,
1007 struct gl_texture_image *img,
1008 GLsizei width, GLsizei height, GLsizei depth,
1009 GLint border, GLenum internalFormat)
1010 {
1011 ASSERT(img);
1012 img->Format = _mesa_base_tex_format( ctx, internalFormat );
1013 ASSERT(img->Format > 0);
1014 img->IntFormat = internalFormat;
1015 img->Border = border;
1016 img->Width = width;
1017 img->Height = height;
1018 img->Depth = depth;
1019 img->RowStride = width;
1020 img->WidthLog2 = logbase2(width - 2 * border);
1021 if (height == 1) /* 1-D texture */
1022 img->HeightLog2 = 0;
1023 else
1024 img->HeightLog2 = logbase2(height - 2 * border);
1025 if (depth == 1) /* 2-D texture */
1026 img->DepthLog2 = 0;
1027 else
1028 img->DepthLog2 = logbase2(depth - 2 * border);
1029 img->Width2 = width - 2 * border; /*1 << img->WidthLog2;*/
1030 img->Height2 = height - 2 * border; /*1 << img->HeightLog2;*/
1031 img->Depth2 = depth - 2 * border; /*1 << img->DepthLog2;*/
1032 img->MaxLog2 = MAX2(img->WidthLog2, img->HeightLog2);
1033 img->IsCompressed = is_compressed_format(ctx, internalFormat);
1034 if (img->IsCompressed)
1035 img->CompressedSize = ctx->Driver.CompressedTextureSize(ctx, width,
1036 height, depth, internalFormat);
1037 else
1038 img->CompressedSize = 0;
1039
1040 if ((width == 1 || _mesa_bitcount(width - 2 * border) == 1) &&
1041 (height == 1 || _mesa_bitcount(height - 2 * border) == 1) &&
1042 (depth == 1 || _mesa_bitcount(depth - 2 * border) == 1))
1043 img->_IsPowerOfTwo = GL_TRUE;
1044 else
1045 img->_IsPowerOfTwo = GL_FALSE;
1046
1047 /* Compute Width/Height/DepthScale for mipmap lod computation */
1048 if (target == GL_TEXTURE_RECTANGLE_NV) {
1049 /* scale = 1.0 since texture coords directly map to texels */
1050 img->WidthScale = 1.0;
1051 img->HeightScale = 1.0;
1052 img->DepthScale = 1.0;
1053 }
1054 else {
1055 img->WidthScale = (GLfloat) img->Width;
1056 img->HeightScale = (GLfloat) img->Height;
1057 img->DepthScale = (GLfloat) img->Depth;
1058 }
1059 }
1060
1061
1062 /**
1063 * This is the fallback for Driver.TestProxyTexImage(). Test the texture
1064 * level, width, height and depth against the ctx->Const limits for textures.
1065 *
1066 * A hardware driver might override this function if, for example, the
1067 * max 3D texture size is 512x512x64 (i.e. not a cube).
1068 *
1069 * \param target one of GL_PROXY_TEXTURE_1D, GL_PROXY_TEXTURE_2D,
1070 * GL_PROXY_TEXTURE_3D, GL_PROXY_TEXTURE_RECTANGLE_NV,
1071 * GL_PROXY_TEXTURE_CUBE_MAP_ARB.
1072 * \param level as passed to glTexImage
1073 * \param internalFormat as passed to glTexImage
1074 * \param format as passed to glTexImage
1075 * \param type as passed to glTexImage
1076 * \param width as passed to glTexImage
1077 * \param height as passed to glTexImage
1078 * \param depth as passed to glTexImage
1079 * \param border as passed to glTexImage
1080 * \return GL_TRUE if the image is acceptable, GL_FALSE if not acceptable.
1081 */
1082 GLboolean
1083 _mesa_test_proxy_teximage(GLcontext *ctx, GLenum target, GLint level,
1084 GLint internalFormat, GLenum format, GLenum type,
1085 GLint width, GLint height, GLint depth, GLint border)
1086 {
1087 GLint maxSize;
1088
1089 (void) internalFormat;
1090 (void) format;
1091 (void) type;
1092
1093 switch (target) {
1094 case GL_PROXY_TEXTURE_1D:
1095 maxSize = 1 << (ctx->Const.MaxTextureLevels - 1);
1096 if (width < 2 * border || width > 2 + maxSize ||
1097 (!ctx->Extensions.ARB_texture_non_power_of_two &&
1098 _mesa_bitcount(width - 2 * border) != 1) ||
1099 level >= ctx->Const.MaxTextureLevels) {
1100 /* bad width or level */
1101 return GL_FALSE;
1102 }
1103 return GL_TRUE;
1104 case GL_PROXY_TEXTURE_2D:
1105 maxSize = 1 << (ctx->Const.MaxTextureLevels - 1);
1106 if (width < 2 * border || width > 2 + maxSize ||
1107 (!ctx->Extensions.ARB_texture_non_power_of_two &&
1108 _mesa_bitcount(width - 2 * border) != 1) ||
1109 height < 2 * border || height > 2 + maxSize ||
1110 (!ctx->Extensions.ARB_texture_non_power_of_two &&
1111 _mesa_bitcount(height - 2 * border) != 1) ||
1112 level >= ctx->Const.MaxTextureLevels) {
1113 /* bad width or height or level */
1114 return GL_FALSE;
1115 }
1116 return GL_TRUE;
1117 case GL_PROXY_TEXTURE_3D:
1118 maxSize = 1 << (ctx->Const.Max3DTextureLevels - 1);
1119 if (width < 2 * border || width > 2 + maxSize ||
1120 (!ctx->Extensions.ARB_texture_non_power_of_two &&
1121 _mesa_bitcount(width - 2 * border) != 1) ||
1122 height < 2 * border || height > 2 + maxSize ||
1123 (!ctx->Extensions.ARB_texture_non_power_of_two &&
1124 _mesa_bitcount(height - 2 * border) != 1) ||
1125 depth < 2 * border || depth > 2 + maxSize ||
1126 (!ctx->Extensions.ARB_texture_non_power_of_two &&
1127 _mesa_bitcount(depth - 2 * border) != 1) ||
1128 level >= ctx->Const.Max3DTextureLevels) {
1129 /* bad width or height or depth or level */
1130 return GL_FALSE;
1131 }
1132 return GL_TRUE;
1133 case GL_PROXY_TEXTURE_RECTANGLE_NV:
1134 if (width < 1 || width > ctx->Const.MaxTextureRectSize ||
1135 height < 1 || height > ctx->Const.MaxTextureRectSize ||
1136 level != 0) {
1137 /* bad width or height or level */
1138 return GL_FALSE;
1139 }
1140 return GL_TRUE;
1141 case GL_PROXY_TEXTURE_CUBE_MAP_ARB:
1142 maxSize = 1 << (ctx->Const.MaxCubeTextureLevels - 1);
1143 if (width < 2 * border || width > 2 + maxSize ||
1144 (!ctx->Extensions.ARB_texture_non_power_of_two &&
1145 _mesa_bitcount(width - 2 * border) != 1) ||
1146 height < 2 * border || height > 2 + maxSize ||
1147 (!ctx->Extensions.ARB_texture_non_power_of_two &&
1148 _mesa_bitcount(height - 2 * border) != 1) ||
1149 level >= ctx->Const.MaxCubeTextureLevels) {
1150 /* bad width or height */
1151 return GL_FALSE;
1152 }
1153 return GL_TRUE;
1154 default:
1155 _mesa_problem(ctx, "Invalid target in _mesa_test_proxy_teximage");
1156 return GL_FALSE;
1157 }
1158 }
1159
1160
1161 /**
1162 * Test the glTexImage[123]D() parameters for errors.
1163 *
1164 * \param ctx GL context.
1165 * \param target texture target given by the user.
1166 * \param level image level given by the user.
1167 * \param internalFormat internal format given by the user.
1168 * \param format pixel data format given by the user.
1169 * \param type pixel data type given by the user.
1170 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1171 * \param width image width given by the user.
1172 * \param height image height given by the user.
1173 * \param depth image depth given by the user.
1174 * \param border image border given by the user.
1175 *
1176 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
1177 *
1178 * Verifies each of the parameters against the constants specified in
1179 * __GLcontextRec::Const and the supported extensions, and according to the
1180 * OpenGL specification.
1181 */
1182 static GLboolean
1183 texture_error_check( GLcontext *ctx, GLenum target,
1184 GLint level, GLint internalFormat,
1185 GLenum format, GLenum type,
1186 GLuint dimensions,
1187 GLint width, GLint height,
1188 GLint depth, GLint border )
1189 {
1190 const GLboolean isProxy = is_proxy_target(target);
1191 GLboolean sizeOK;
1192 GLboolean colorFormat, indexFormat;
1193
1194 /* Basic level check (more checking in ctx->Driver.TestProxyTexImage) */
1195 if (level < 0 || level >= MAX_TEXTURE_LEVELS) {
1196 if (!isProxy) {
1197 _mesa_error(ctx, GL_INVALID_VALUE,
1198 "glTexImage%dD(level=%d)", dimensions, level);
1199 }
1200 return GL_TRUE;
1201 }
1202
1203 /* Check border */
1204 if (border < 0 || border > 1 ||
1205 ((target == GL_TEXTURE_RECTANGLE_NV ||
1206 target == GL_PROXY_TEXTURE_RECTANGLE_NV) && border != 0)) {
1207 if (!isProxy) {
1208 _mesa_error(ctx, GL_INVALID_VALUE,
1209 "glTexImage%dD(border=%d)", dimensions, border);
1210 }
1211 return GL_TRUE;
1212 }
1213
1214 if (width < 0 || height < 0 || depth < 0) {
1215 if (!isProxy) {
1216 _mesa_error(ctx, GL_INVALID_VALUE,
1217 "glTexImage%dD(width, height or depth < 0)", dimensions);
1218 }
1219 return GL_TRUE;
1220 }
1221
1222 /* Check target and call ctx->Driver.TestProxyTexImage() to check the
1223 * level, width, height and depth.
1224 */
1225 if (dimensions == 1) {
1226 if (target == GL_PROXY_TEXTURE_1D || target == GL_TEXTURE_1D) {
1227 sizeOK = ctx->Driver.TestProxyTexImage(ctx, GL_PROXY_TEXTURE_1D,
1228 level, internalFormat,
1229 format, type,
1230 width, 1, 1, border);
1231 }
1232 else {
1233 _mesa_error( ctx, GL_INVALID_ENUM, "glTexImage1D(target)" );
1234 return GL_TRUE;
1235 }
1236 }
1237 else if (dimensions == 2) {
1238 if (target == GL_PROXY_TEXTURE_2D || target == GL_TEXTURE_2D) {
1239 sizeOK = ctx->Driver.TestProxyTexImage(ctx, GL_PROXY_TEXTURE_2D,
1240 level, internalFormat,
1241 format, type,
1242 width, height, 1, border);
1243 }
1244 else if (target == GL_PROXY_TEXTURE_CUBE_MAP_ARB ||
1245 (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
1246 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB)) {
1247 if (!ctx->Extensions.ARB_texture_cube_map) {
1248 _mesa_error(ctx, GL_INVALID_ENUM, "glTexImage2D(target)");
1249 return GL_TRUE;
1250 }
1251 sizeOK = (width == height) &&
1252 ctx->Driver.TestProxyTexImage(ctx, GL_PROXY_TEXTURE_CUBE_MAP_ARB,
1253 level, internalFormat, format, type,
1254 width, height, 1, border);
1255 }
1256 else if (target == GL_PROXY_TEXTURE_RECTANGLE_NV ||
1257 target == GL_TEXTURE_RECTANGLE_NV) {
1258 if (!ctx->Extensions.NV_texture_rectangle) {
1259 _mesa_error(ctx, GL_INVALID_ENUM, "glTexImage2D(target)");
1260 return GL_TRUE;
1261 }
1262 sizeOK = ctx->Driver.TestProxyTexImage(ctx,
1263 GL_PROXY_TEXTURE_RECTANGLE_NV,
1264 level, internalFormat,
1265 format, type,
1266 width, height, 1, border);
1267 }
1268 else {
1269 _mesa_error(ctx, GL_INVALID_ENUM, "glTexImage2D(target)");
1270 return GL_TRUE;
1271 }
1272 }
1273 else if (dimensions == 3) {
1274 if (target == GL_PROXY_TEXTURE_3D || target == GL_TEXTURE_3D) {
1275 sizeOK = ctx->Driver.TestProxyTexImage(ctx, GL_PROXY_TEXTURE_3D,
1276 level, internalFormat,
1277 format, type,
1278 width, height, depth, border);
1279 }
1280 else {
1281 _mesa_error( ctx, GL_INVALID_ENUM, "glTexImage3D(target)" );
1282 return GL_TRUE;
1283 }
1284 }
1285 else {
1286 _mesa_problem( ctx, "bad dims in texture_error_check" );
1287 return GL_TRUE;
1288 }
1289
1290 if (!sizeOK) {
1291 if (!isProxy) {
1292 _mesa_error(ctx, GL_INVALID_VALUE,
1293 "glTexImage%dD(level=%d, width=%d, height=%d, depth=%d)",
1294 dimensions, level, width, height, depth);
1295 }
1296 return GL_TRUE;
1297 }
1298
1299 /* Check internalFormat */
1300 if (_mesa_base_tex_format(ctx, internalFormat) < 0) {
1301 if (!isProxy) {
1302 _mesa_error(ctx, GL_INVALID_VALUE,
1303 "glTexImage%dD(internalFormat=0x%x)",
1304 dimensions, internalFormat);
1305 }
1306 return GL_TRUE;
1307 }
1308
1309 /* Check incoming image format and type */
1310 if (!_mesa_is_legal_format_and_type(ctx, format, type)) {
1311 /* Yes, generate GL_INVALID_OPERATION, not GL_INVALID_ENUM, if there
1312 * is a type/format mismatch. See 1.2 spec page 94, sec 3.6.4.
1313 */
1314 if (!isProxy) {
1315 _mesa_error(ctx, GL_INVALID_OPERATION,
1316 "glTexImage%dD(format or type)", dimensions);
1317 }
1318 return GL_TRUE;
1319 }
1320
1321 /* make sure internal format and format basically agree */
1322 colorFormat = is_color_format(format);
1323 indexFormat = is_index_format(format);
1324 if ((is_color_format(internalFormat) && !colorFormat && !indexFormat) ||
1325 (is_index_format(internalFormat) && !indexFormat) ||
1326 (is_depth_format(internalFormat) != is_depth_format(format)) ||
1327 (is_ycbcr_format(internalFormat) != is_ycbcr_format(format))) {
1328 if (!isProxy)
1329 _mesa_error(ctx, GL_INVALID_OPERATION,
1330 "glTexImage(internalFormat/format)");
1331 return GL_TRUE;
1332 }
1333
1334 /* additional checks for ycbcr textures */
1335 if (internalFormat == GL_YCBCR_MESA) {
1336 ASSERT(ctx->Extensions.MESA_ycbcr_texture);
1337 if (type != GL_UNSIGNED_SHORT_8_8_MESA &&
1338 type != GL_UNSIGNED_SHORT_8_8_REV_MESA) {
1339 char message[100];
1340 _mesa_sprintf(message,
1341 "glTexImage%d(format/type YCBCR mismatch", dimensions);
1342 _mesa_error(ctx, GL_INVALID_ENUM, message);
1343 return GL_TRUE; /* error */
1344 }
1345 if (target != GL_TEXTURE_2D &&
1346 target != GL_PROXY_TEXTURE_2D &&
1347 target != GL_TEXTURE_RECTANGLE_NV &&
1348 target != GL_PROXY_TEXTURE_RECTANGLE_NV) {
1349 if (!isProxy)
1350 _mesa_error(ctx, GL_INVALID_ENUM, "glTexImage(target)");
1351 return GL_TRUE;
1352 }
1353 if (border != 0) {
1354 if (!isProxy) {
1355 char message[100];
1356 _mesa_sprintf(message,
1357 "glTexImage%d(format=GL_YCBCR_MESA and border=%d)",
1358 dimensions, border);
1359 _mesa_error(ctx, GL_INVALID_VALUE, message);
1360 }
1361 return GL_TRUE;
1362 }
1363 }
1364
1365 /* additional checks for depth textures */
1366 if (_mesa_base_tex_format(ctx, internalFormat) == GL_DEPTH_COMPONENT) {
1367 /* Only 1D and 2D textures supported */
1368 if (target != GL_TEXTURE_1D &&
1369 target != GL_PROXY_TEXTURE_1D &&
1370 target != GL_TEXTURE_2D &&
1371 target != GL_PROXY_TEXTURE_2D) {
1372 if (!isProxy)
1373 _mesa_error(ctx, GL_INVALID_ENUM,
1374 "glTexImage(target/internalFormat)");
1375 return GL_TRUE;
1376 }
1377 }
1378
1379 /* additional checks for compressed textures */
1380 if (is_compressed_format(ctx, internalFormat)) {
1381 if (target == GL_TEXTURE_2D || target == GL_PROXY_TEXTURE_2D) {
1382 /* OK */
1383 }
1384 else if (ctx->Extensions.ARB_texture_cube_map &&
1385 (target == GL_PROXY_TEXTURE_CUBE_MAP ||
1386 (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X &&
1387 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z))) {
1388 /* OK */
1389 }
1390 else {
1391 if (!isProxy) {
1392 _mesa_error(ctx, GL_INVALID_ENUM,
1393 "glTexImage%d(target)", dimensions);
1394 return GL_TRUE;
1395 }
1396 }
1397 if (border != 0) {
1398 if (!isProxy) {
1399 _mesa_error(ctx, GL_INVALID_OPERATION,
1400 "glTexImage%D(border!=0)", dimensions);
1401 }
1402 return GL_TRUE;
1403 }
1404 }
1405
1406 /* if we get here, the parameters are OK */
1407 return GL_FALSE;
1408 }
1409
1410
1411 /**
1412 * Test glTexSubImage[123]D() parameters for errors.
1413 *
1414 * \param ctx GL context.
1415 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1416 * \param target texture target given by the user.
1417 * \param level image level given by the user.
1418 * \param xoffset sub-image x offset given by the user.
1419 * \param yoffset sub-image y offset given by the user.
1420 * \param zoffset sub-image z offset given by the user.
1421 * \param format pixel data format given by the user.
1422 * \param type pixel data type given by the user.
1423 * \param width image width given by the user.
1424 * \param height image height given by the user.
1425 * \param depth image depth given by the user.
1426 *
1427 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
1428 *
1429 * Verifies each of the parameters against the constants specified in
1430 * __GLcontextRec::Const and the supported extensions, and according to the
1431 * OpenGL specification.
1432 */
1433 static GLboolean
1434 subtexture_error_check( GLcontext *ctx, GLuint dimensions,
1435 GLenum target, GLint level,
1436 GLint xoffset, GLint yoffset, GLint zoffset,
1437 GLint width, GLint height, GLint depth,
1438 GLenum format, GLenum type )
1439 {
1440 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1441 struct gl_texture_image *destTex;
1442
1443 /* Check target */
1444 if (dimensions == 1) {
1445 if (target != GL_TEXTURE_1D) {
1446 _mesa_error( ctx, GL_INVALID_ENUM, "glTexSubImage1D(target)" );
1447 return GL_TRUE;
1448 }
1449 }
1450 else if (dimensions == 2) {
1451 if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
1452 target <=GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB) {
1453 if (!ctx->Extensions.ARB_texture_cube_map) {
1454 _mesa_error( ctx, GL_INVALID_ENUM, "glTexSubImage2D(target)" );
1455 return GL_TRUE;
1456 }
1457 }
1458 else if (ctx->Extensions.NV_texture_rectangle &&
1459 target == GL_TEXTURE_RECTANGLE_NV) {
1460 if (!ctx->Extensions.NV_texture_rectangle) {
1461 _mesa_error( ctx, GL_INVALID_ENUM, "glTexSubImage2D(target)" );
1462 return GL_TRUE;
1463 }
1464 }
1465 else if (target != GL_TEXTURE_2D) {
1466 _mesa_error( ctx, GL_INVALID_ENUM, "glTexSubImage2D(target)" );
1467 return GL_TRUE;
1468 }
1469 }
1470 else if (dimensions == 3) {
1471 if (target != GL_TEXTURE_3D) {
1472 _mesa_error( ctx, GL_INVALID_ENUM, "glTexSubImage3D(target)" );
1473 return GL_TRUE;
1474 }
1475 }
1476 else {
1477 _mesa_problem( ctx, "invalid dims in texture_error_check" );
1478 return GL_TRUE;
1479 }
1480
1481 /* Basic level check */
1482 if (level < 0 || level >= MAX_TEXTURE_LEVELS) {
1483 _mesa_error(ctx, GL_INVALID_ENUM, "glTexSubImage2D(level=%d)", level);
1484 return GL_TRUE;
1485 }
1486
1487 if (width < 0) {
1488 _mesa_error(ctx, GL_INVALID_VALUE,
1489 "glTexSubImage%dD(width=%d)", dimensions, width);
1490 return GL_TRUE;
1491 }
1492 if (height < 0 && dimensions > 1) {
1493 _mesa_error(ctx, GL_INVALID_VALUE,
1494 "glTexSubImage%dD(height=%d)", dimensions, height);
1495 return GL_TRUE;
1496 }
1497 if (depth < 0 && dimensions > 2) {
1498 _mesa_error(ctx, GL_INVALID_VALUE,
1499 "glTexSubImage%dD(depth=%d)", dimensions, depth);
1500 return GL_TRUE;
1501 }
1502
1503 destTex = _mesa_select_tex_image(ctx, texUnit, target, level);
1504
1505 if (!destTex) {
1506 /* undefined image level */
1507 _mesa_error(ctx, GL_INVALID_OPERATION, "glTexSubImage%dD", dimensions);
1508 return GL_TRUE;
1509 }
1510
1511 if (xoffset < -((GLint)destTex->Border)) {
1512 _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage%dD(xoffset)",
1513 dimensions);
1514 return GL_TRUE;
1515 }
1516 if (xoffset + width > (GLint) (destTex->Width + destTex->Border)) {
1517 _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage%dD(xoffset+width)",
1518 dimensions);
1519 return GL_TRUE;
1520 }
1521 if (dimensions > 1) {
1522 if (yoffset < -((GLint)destTex->Border)) {
1523 _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage%dD(yoffset)",
1524 dimensions);
1525 return GL_TRUE;
1526 }
1527 if (yoffset + height > (GLint) (destTex->Height + destTex->Border)) {
1528 _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage%dD(yoffset+height)",
1529 dimensions);
1530 return GL_TRUE;
1531 }
1532 }
1533 if (dimensions > 2) {
1534 if (zoffset < -((GLint)destTex->Border)) {
1535 _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage3D(zoffset)");
1536 return GL_TRUE;
1537 }
1538 if (zoffset + depth > (GLint) (destTex->Depth + destTex->Border)) {
1539 _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage3D(zoffset+depth)");
1540 return GL_TRUE;
1541 }
1542 }
1543
1544 if (!_mesa_is_legal_format_and_type(ctx, format, type)) {
1545 _mesa_error(ctx, GL_INVALID_ENUM,
1546 "glTexSubImage%dD(format or type)", dimensions);
1547 return GL_TRUE;
1548 }
1549
1550 if (destTex->IsCompressed) {
1551 const struct gl_texture_unit *texUnit;
1552 const struct gl_texture_image *texImage;
1553 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1554 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
1555
1556 if (target == GL_TEXTURE_2D || target == GL_PROXY_TEXTURE_2D) {
1557 /* OK */
1558 }
1559 else if (ctx->Extensions.ARB_texture_cube_map &&
1560 (target == GL_PROXY_TEXTURE_CUBE_MAP ||
1561 (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X &&
1562 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z))) {
1563 /* OK */
1564 }
1565 else {
1566 _mesa_error(ctx, GL_INVALID_ENUM,
1567 "glTexSubImage%D(target)", dimensions);
1568 return GL_TRUE;
1569 }
1570 /* offset must be multiple of 4 */
1571 if ((xoffset & 3) || (yoffset & 3)) {
1572 _mesa_error(ctx, GL_INVALID_OPERATION,
1573 "glTexSubImage%D(xoffset or yoffset)", dimensions);
1574 return GL_TRUE;
1575 }
1576 /* size must be multiple of 4 or equal to whole texture size */
1577 if ((width & 3) && (GLuint) width != texImage->Width) {
1578 _mesa_error(ctx, GL_INVALID_OPERATION,
1579 "glTexSubImage%D(width)", dimensions);
1580 return GL_TRUE;
1581 }
1582 if ((height & 3) && (GLuint) height != texImage->Height) {
1583 _mesa_error(ctx, GL_INVALID_OPERATION,
1584 "glTexSubImage%D(width)", dimensions);
1585 return GL_TRUE;
1586 }
1587 }
1588
1589 return GL_FALSE;
1590 }
1591
1592
1593 /**
1594 * Test glCopyTexImage[12]D() parameters for errors.
1595 *
1596 * \param ctx GL context.
1597 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1598 * \param target texture target given by the user.
1599 * \param level image level given by the user.
1600 * \param internalFormat internal format given by the user.
1601 * \param width image width given by the user.
1602 * \param height image height given by the user.
1603 * \param depth image depth given by the user.
1604 * \param border texture border.
1605 *
1606 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
1607 *
1608 * Verifies each of the parameters against the constants specified in
1609 * __GLcontextRec::Const and the supported extensions, and according to the
1610 * OpenGL specification.
1611 */
1612 static GLboolean
1613 copytexture_error_check( GLcontext *ctx, GLuint dimensions,
1614 GLenum target, GLint level, GLint internalFormat,
1615 GLint width, GLint height, GLint border )
1616 {
1617 GLenum format, type;
1618 GLboolean sizeOK;
1619
1620 /* Basic level check (more checking in ctx->Driver.TestProxyTexImage) */
1621 if (level < 0 || level >= MAX_TEXTURE_LEVELS) {
1622 _mesa_error(ctx, GL_INVALID_VALUE,
1623 "glCopyTexImage%dD(level=%d)", dimensions, level);
1624 return GL_TRUE;
1625 }
1626
1627 /* Check border */
1628 if (border < 0 || border > 1 ||
1629 ((target == GL_TEXTURE_RECTANGLE_NV ||
1630 target == GL_PROXY_TEXTURE_RECTANGLE_NV) && border != 0)) {
1631 return GL_TRUE;
1632 }
1633
1634 /* The format and type aren't really significant here, but we need to pass
1635 * something to TestProxyTexImage().
1636 */
1637 format = _mesa_base_tex_format(ctx, internalFormat);
1638 type = GL_FLOAT;
1639
1640 /* Check target and call ctx->Driver.TestProxyTexImage() to check the
1641 * level, width, height and depth.
1642 */
1643 if (dimensions == 1) {
1644 if (target == GL_TEXTURE_1D) {
1645 sizeOK = ctx->Driver.TestProxyTexImage(ctx, GL_PROXY_TEXTURE_1D,
1646 level, internalFormat,
1647 format, type,
1648 width, 1, 1, border);
1649 }
1650 else {
1651 _mesa_error( ctx, GL_INVALID_ENUM, "glCopyTexImage1D(target)" );
1652 return GL_TRUE;
1653 }
1654 }
1655 else if (dimensions == 2) {
1656 if (target == GL_TEXTURE_2D) {
1657 sizeOK = ctx->Driver.TestProxyTexImage(ctx, GL_PROXY_TEXTURE_2D,
1658 level, internalFormat,
1659 format, type,
1660 width, height, 1, border);
1661 }
1662 else if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
1663 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB) {
1664 if (!ctx->Extensions.ARB_texture_cube_map) {
1665 _mesa_error( ctx, GL_INVALID_ENUM, "glCopyTexImage2D(target)" );
1666 return GL_TRUE;
1667 }
1668 sizeOK = (width == height) &&
1669 ctx->Driver.TestProxyTexImage(ctx, GL_PROXY_TEXTURE_CUBE_MAP_ARB,
1670 level, internalFormat, format, type,
1671 width, height, 1, border);
1672 }
1673 else if (target == GL_TEXTURE_RECTANGLE_NV) {
1674 if (!ctx->Extensions.NV_texture_rectangle) {
1675 _mesa_error( ctx, GL_INVALID_ENUM, "glCopyTexImage2D(target)" );
1676 return GL_TRUE;
1677 }
1678 sizeOK = ctx->Driver.TestProxyTexImage(ctx,
1679 GL_PROXY_TEXTURE_RECTANGLE_NV,
1680 level, internalFormat,
1681 format, type,
1682 width, height, 1, border);
1683 }
1684 else {
1685 _mesa_error( ctx, GL_INVALID_ENUM, "glCopyTexImage2D(target)" );
1686 return GL_TRUE;
1687 }
1688 }
1689 else {
1690 _mesa_problem(ctx, "invalid dimensions in copytexture_error_check");
1691 return GL_TRUE;
1692 }
1693
1694 if (!sizeOK) {
1695 if (dimensions == 1) {
1696 _mesa_error(ctx, GL_INVALID_VALUE,
1697 "glCopyTexImage1D(width=%d)", width);
1698 }
1699 else {
1700 ASSERT(dimensions == 2);
1701 _mesa_error(ctx, GL_INVALID_VALUE,
1702 "glCopyTexImage2D(width=%d, height=%d)", width, height);
1703 }
1704 return GL_TRUE;
1705 }
1706
1707 if (_mesa_base_tex_format(ctx, internalFormat) < 0) {
1708 _mesa_error(ctx, GL_INVALID_VALUE,
1709 "glCopyTexImage%dD(internalFormat)", dimensions);
1710 return GL_TRUE;
1711 }
1712
1713 if (is_compressed_format(ctx, internalFormat)) {
1714 if (target != GL_TEXTURE_2D) {
1715 _mesa_error(ctx, GL_INVALID_ENUM,
1716 "glCopyTexImage%d(target)", dimensions);
1717 return GL_TRUE;
1718 }
1719 if (border != 0) {
1720 _mesa_error(ctx, GL_INVALID_OPERATION,
1721 "glCopyTexImage%D(border!=0)", dimensions);
1722 return GL_TRUE;
1723 }
1724 }
1725
1726 /* if we get here, the parameters are OK */
1727 return GL_FALSE;
1728 }
1729
1730
1731 /**
1732 * Test glCopyTexImage[12]D() parameters for errors.
1733 *
1734 * \param ctx GL context.
1735 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1736 * \param target texture target given by the user.
1737 * \param level image level given by the user.
1738 * \param xoffset sub-image x offset given by the user.
1739 * \param yoffset sub-image y offset given by the user.
1740 * \param zoffset sub-image z offset given by the user.
1741 * \param width image width given by the user.
1742 * \param height image height given by the user.
1743 *
1744 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
1745 *
1746 * Verifies each of the parameters against the constants specified in
1747 * __GLcontextRec::Const and the supported extensions, and according to the
1748 * OpenGL specification.
1749 */
1750 static GLboolean
1751 copytexsubimage_error_check( GLcontext *ctx, GLuint dimensions,
1752 GLenum target, GLint level,
1753 GLint xoffset, GLint yoffset, GLint zoffset,
1754 GLsizei width, GLsizei height )
1755 {
1756 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1757 struct gl_texture_image *teximage;
1758
1759 /* Check target */
1760 if (dimensions == 1) {
1761 if (target != GL_TEXTURE_1D) {
1762 _mesa_error( ctx, GL_INVALID_ENUM, "glCopyTexSubImage1D(target)" );
1763 return GL_TRUE;
1764 }
1765 }
1766 else if (dimensions == 2) {
1767 if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
1768 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB) {
1769 if (!ctx->Extensions.ARB_texture_cube_map) {
1770 _mesa_error( ctx, GL_INVALID_ENUM, "glCopyTexSubImage2D(target)" );
1771 return GL_TRUE;
1772 }
1773 }
1774 else if (target == GL_TEXTURE_RECTANGLE_NV) {
1775 if (!ctx->Extensions.NV_texture_rectangle) {
1776 _mesa_error( ctx, GL_INVALID_ENUM, "glCopyTexSubImage2D(target)" );
1777 return GL_TRUE;
1778 }
1779 }
1780 else if (target != GL_TEXTURE_2D) {
1781 _mesa_error( ctx, GL_INVALID_ENUM, "glCopyTexSubImage2D(target)" );
1782 return GL_TRUE;
1783 }
1784 }
1785 else if (dimensions == 3) {
1786 if (target != GL_TEXTURE_3D) {
1787 _mesa_error( ctx, GL_INVALID_ENUM, "glCopyTexSubImage3D(target)" );
1788 return GL_TRUE;
1789 }
1790 }
1791
1792 /* Check level */
1793 if (level < 0 || level >= MAX_TEXTURE_LEVELS) {
1794 _mesa_error(ctx, GL_INVALID_VALUE,
1795 "glCopyTexSubImage%dD(level=%d)", dimensions, level);
1796 return GL_TRUE;
1797 }
1798
1799 /* Check size */
1800 if (width < 0) {
1801 _mesa_error(ctx, GL_INVALID_VALUE,
1802 "glCopyTexSubImage%dD(width=%d)", dimensions, width);
1803 return GL_TRUE;
1804 }
1805 if (dimensions > 1 && height < 0) {
1806 _mesa_error(ctx, GL_INVALID_VALUE,
1807 "glCopyTexSubImage%dD(height=%d)", dimensions, height);
1808 return GL_TRUE;
1809 }
1810
1811 teximage = _mesa_select_tex_image(ctx, texUnit, target, level);
1812 if (!teximage) {
1813 _mesa_error(ctx, GL_INVALID_OPERATION,
1814 "glCopyTexSubImage%dD(undefined texture level: %d)",
1815 dimensions, level);
1816 return GL_TRUE;
1817 }
1818
1819 if (xoffset < -((GLint)teximage->Border)) {
1820 _mesa_error(ctx, GL_INVALID_VALUE,
1821 "glCopyTexSubImage%dD(xoffset=%d)", dimensions, xoffset);
1822 return GL_TRUE;
1823 }
1824 if (xoffset + width > (GLint) (teximage->Width + teximage->Border)) {
1825 _mesa_error(ctx, GL_INVALID_VALUE,
1826 "glCopyTexSubImage%dD(xoffset+width)", dimensions);
1827 return GL_TRUE;
1828 }
1829 if (dimensions > 1) {
1830 if (yoffset < -((GLint)teximage->Border)) {
1831 _mesa_error(ctx, GL_INVALID_VALUE,
1832 "glCopyTexSubImage%dD(yoffset=%d)", dimensions, yoffset);
1833 return GL_TRUE;
1834 }
1835 /* NOTE: we're adding the border here, not subtracting! */
1836 if (yoffset + height > (GLint) (teximage->Height + teximage->Border)) {
1837 _mesa_error(ctx, GL_INVALID_VALUE,
1838 "glCopyTexSubImage%dD(yoffset+height)", dimensions);
1839 return GL_TRUE;
1840 }
1841 }
1842
1843 if (dimensions > 2) {
1844 if (zoffset < -((GLint)teximage->Border)) {
1845 _mesa_error(ctx, GL_INVALID_VALUE,
1846 "glCopyTexSubImage%dD(zoffset)", dimensions);
1847 return GL_TRUE;
1848 }
1849 if (zoffset > (GLint) (teximage->Depth + teximage->Border)) {
1850 _mesa_error(ctx, GL_INVALID_VALUE,
1851 "glCopyTexSubImage%dD(zoffset+depth)", dimensions);
1852 return GL_TRUE;
1853 }
1854 }
1855
1856 if (teximage->IsCompressed) {
1857 if (target != GL_TEXTURE_2D) {
1858 _mesa_error(ctx, GL_INVALID_ENUM,
1859 "glCopyTexSubImage%d(target)", dimensions);
1860 return GL_TRUE;
1861 }
1862 /* offset must be multiple of 4 */
1863 if ((xoffset & 3) || (yoffset & 3)) {
1864 _mesa_error(ctx, GL_INVALID_VALUE,
1865 "glCopyTexSubImage%D(xoffset or yoffset)", dimensions);
1866 return GL_TRUE;
1867 }
1868 /* size must be multiple of 4 */
1869 if ((width & 3) != 0 && (GLuint) width != teximage->Width) {
1870 _mesa_error(ctx, GL_INVALID_VALUE,
1871 "glCopyTexSubImage%D(width)", dimensions);
1872 return GL_TRUE;
1873 }
1874 if ((height & 3) != 0 && (GLuint) height != teximage->Height) {
1875 _mesa_error(ctx, GL_INVALID_VALUE,
1876 "glCopyTexSubImage%D(height)", dimensions);
1877 return GL_TRUE;
1878 }
1879 }
1880
1881 if (teximage->IntFormat == GL_YCBCR_MESA) {
1882 _mesa_error(ctx, GL_INVALID_OPERATION, "glCopyTexSubImage2D");
1883 return GL_TRUE;
1884 }
1885
1886 /* if we get here, the parameters are OK */
1887 return GL_FALSE;
1888 }
1889
1890
1891 /**
1892 * Get texture image. Called by glGetTexImage.
1893 *
1894 * \param target texture target.
1895 * \param level image level.
1896 * \param format pixel data format for returned image.
1897 * \param type pixel data type for returned image.
1898 * \param pixels returned pixel data.
1899 */
1900 void GLAPIENTRY
1901 _mesa_GetTexImage( GLenum target, GLint level, GLenum format,
1902 GLenum type, GLvoid *pixels )
1903 {
1904 const struct gl_texture_unit *texUnit;
1905 const struct gl_texture_object *texObj;
1906 const struct gl_texture_image *texImage;
1907 GLint maxLevels = 0;
1908 GET_CURRENT_CONTEXT(ctx);
1909 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
1910
1911 texUnit = &(ctx->Texture.Unit[ctx->Texture.CurrentUnit]);
1912 texObj = _mesa_select_tex_object(ctx, texUnit, target);
1913 if (!texObj || is_proxy_target(target)) {
1914 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexImage(target)");
1915 return;
1916 }
1917
1918 maxLevels = _mesa_max_texture_levels(ctx, target);
1919 ASSERT(maxLevels > 0); /* 0 indicates bad target, caught above */
1920
1921 if (level < 0 || level >= maxLevels) {
1922 _mesa_error( ctx, GL_INVALID_VALUE, "glGetTexImage(level)" );
1923 return;
1924 }
1925
1926 if (_mesa_sizeof_packed_type(type) <= 0) {
1927 _mesa_error( ctx, GL_INVALID_ENUM, "glGetTexImage(type)" );
1928 return;
1929 }
1930
1931 if (_mesa_components_in_format(format) <= 0 ||
1932 format == GL_STENCIL_INDEX) {
1933 _mesa_error( ctx, GL_INVALID_ENUM, "glGetTexImage(format)" );
1934 return;
1935 }
1936
1937 if (!ctx->Extensions.EXT_paletted_texture && is_index_format(format)) {
1938 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexImage(format)");
1939 }
1940
1941 if (!ctx->Extensions.SGIX_depth_texture && is_depth_format(format)) {
1942 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexImage(format)");
1943 }
1944
1945 if (!ctx->Extensions.MESA_ycbcr_texture && is_ycbcr_format(format)) {
1946 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexImage(format)");
1947 }
1948
1949 if (!pixels)
1950 return;
1951
1952 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
1953 if (!texImage) {
1954 /* invalid mipmap level, not an error */
1955 return;
1956 }
1957
1958 /* Make sure the requested image format is compatible with the
1959 * texture's format. We let the colorformat-indexformat go through,
1960 * because the texelfetcher will dequantize to full rgba.
1961 */
1962 if (is_color_format(format)
1963 && !is_color_format(texImage->TexFormat->BaseFormat)
1964 && !is_index_format(texImage->TexFormat->BaseFormat)) {
1965 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetTexImage(format mismatch)");
1966 return;
1967 }
1968 else if (is_index_format(format)
1969 && !is_index_format(texImage->TexFormat->BaseFormat)) {
1970 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetTexImage(format mismatch)");
1971 return;
1972 }
1973 else if (is_depth_format(format)
1974 && !is_depth_format(texImage->TexFormat->BaseFormat)) {
1975 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetTexImage(format mismatch)");
1976 return;
1977 }
1978 else if (is_ycbcr_format(format)
1979 && !is_ycbcr_format(texImage->TexFormat->BaseFormat)) {
1980 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetTexImage(format mismatch)");
1981 return;
1982 }
1983
1984 /* typically, this will call _mesa_get_teximage() */
1985 ctx->Driver.GetTexImage(ctx, target, level, format, type, pixels,
1986 texObj, texImage);
1987 }
1988
1989
1990
1991 /*
1992 * Called from the API. Note that width includes the border.
1993 */
1994 void GLAPIENTRY
1995 _mesa_TexImage1D( GLenum target, GLint level, GLint internalFormat,
1996 GLsizei width, GLint border, GLenum format,
1997 GLenum type, const GLvoid *pixels )
1998 {
1999 GLsizei postConvWidth = width;
2000 GET_CURRENT_CONTEXT(ctx);
2001 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2002
2003 if (is_color_format(internalFormat)) {
2004 _mesa_adjust_image_for_convolution(ctx, 1, &postConvWidth, NULL);
2005 }
2006
2007 if (target == GL_TEXTURE_1D) {
2008 struct gl_texture_unit *texUnit;
2009 struct gl_texture_object *texObj;
2010 struct gl_texture_image *texImage;
2011
2012 if (texture_error_check(ctx, target, level, internalFormat,
2013 format, type, 1, postConvWidth, 1, 1, border)) {
2014 return; /* error was recorded */
2015 }
2016
2017 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2018 texObj = _mesa_select_tex_object(ctx, texUnit, target);
2019 texImage = _mesa_get_tex_image(ctx, texUnit, target, level);
2020
2021 if (!texImage) {
2022 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage1D");
2023 return;
2024 }
2025 else if (texImage->Data && !texImage->IsClientData) {
2026 /* free the old texture data */
2027 MESA_PBUFFER_FREE(texImage->Data);
2028 }
2029 texImage->Data = NULL;
2030 clear_teximage_fields(texImage); /* not really needed, but helpful */
2031 _mesa_init_teximage_fields(ctx, target, texImage,
2032 postConvWidth, 1, 1,
2033 border, internalFormat);
2034
2035 if (ctx->NewState & _IMAGE_NEW_TRANSFER_STATE)
2036 _mesa_update_state(ctx);
2037
2038 ASSERT(ctx->Driver.TexImage1D);
2039
2040 /* Give the texture to the driver! <pixels> may be null! */
2041 (*ctx->Driver.TexImage1D)(ctx, target, level, internalFormat,
2042 width, border, format, type, pixels,
2043 &ctx->Unpack, texObj, texImage);
2044
2045 ASSERT(texImage->TexFormat);
2046
2047 /* If driver didn't explicitly set this, use the defaults */
2048 if (!texImage->FetchTexelc)
2049 texImage->FetchTexelc = texImage->TexFormat->FetchTexel1D;
2050 if (!texImage->FetchTexelf)
2051 texImage->FetchTexelf = texImage->TexFormat->FetchTexel1Df;
2052 ASSERT(texImage->FetchTexelc);
2053 ASSERT(texImage->FetchTexelf);
2054
2055 /* state update */
2056 texObj->Complete = GL_FALSE;
2057 ctx->NewState |= _NEW_TEXTURE;
2058 }
2059 else if (target == GL_PROXY_TEXTURE_1D) {
2060 /* Proxy texture: check for errors and update proxy state */
2061 struct gl_texture_image *texImage;
2062 texImage = _mesa_get_proxy_tex_image(ctx, target, level);
2063 if (texture_error_check(ctx, target, level, internalFormat,
2064 format, type, 1, postConvWidth, 1, 1, border)) {
2065 /* when error, clear all proxy texture image parameters */
2066 if (texImage)
2067 clear_teximage_fields(texImage);
2068 }
2069 else {
2070 /* no error, set the tex image parameters */
2071 ASSERT(texImage);
2072 _mesa_init_teximage_fields(ctx, target, texImage,
2073 postConvWidth, 1, 1,
2074 border, internalFormat);
2075 texImage->TexFormat = (*ctx->Driver.ChooseTextureFormat)(ctx,
2076 internalFormat, format, type);
2077 }
2078 }
2079 else {
2080 _mesa_error( ctx, GL_INVALID_ENUM, "glTexImage1D(target)" );
2081 return;
2082 }
2083 }
2084
2085
2086 void GLAPIENTRY
2087 _mesa_TexImage2D( GLenum target, GLint level, GLint internalFormat,
2088 GLsizei width, GLsizei height, GLint border,
2089 GLenum format, GLenum type,
2090 const GLvoid *pixels )
2091 {
2092 GLsizei postConvWidth = width, postConvHeight = height;
2093 GET_CURRENT_CONTEXT(ctx);
2094 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2095
2096 if (is_color_format(internalFormat)) {
2097 _mesa_adjust_image_for_convolution(ctx, 2, &postConvWidth,
2098 &postConvHeight);
2099 }
2100
2101 if (target == GL_TEXTURE_2D ||
2102 (ctx->Extensions.ARB_texture_cube_map &&
2103 target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
2104 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB) ||
2105 (ctx->Extensions.NV_texture_rectangle &&
2106 target == GL_TEXTURE_RECTANGLE_NV)) {
2107 /* non-proxy target */
2108 struct gl_texture_unit *texUnit;
2109 struct gl_texture_object *texObj;
2110 struct gl_texture_image *texImage;
2111
2112 if (texture_error_check(ctx, target, level, internalFormat,
2113 format, type, 2, postConvWidth, postConvHeight,
2114 1, border)) {
2115 return; /* error was recorded */
2116 }
2117
2118 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2119 texObj = _mesa_select_tex_object(ctx, texUnit, target);
2120 texImage = _mesa_get_tex_image(ctx, texUnit, target, level);
2121 if (!texImage) {
2122 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage2D");
2123 return;
2124 }
2125 else if (texImage->Data && !texImage->IsClientData) {
2126 /* free the old texture data */
2127 MESA_PBUFFER_FREE(texImage->Data);
2128 }
2129 texImage->Data = NULL;
2130 clear_teximage_fields(texImage); /* not really needed, but helpful */
2131 _mesa_init_teximage_fields(ctx, target, texImage,
2132 postConvWidth, postConvHeight, 1,
2133 border, internalFormat);
2134
2135 if (ctx->NewState & _IMAGE_NEW_TRANSFER_STATE)
2136 _mesa_update_state(ctx);
2137
2138 ASSERT(ctx->Driver.TexImage2D);
2139
2140 /* Give the texture to the driver! <pixels> may be null! */
2141 (*ctx->Driver.TexImage2D)(ctx, target, level, internalFormat,
2142 width, height, border, format, type, pixels,
2143 &ctx->Unpack, texObj, texImage);
2144
2145 ASSERT(texImage->TexFormat);
2146
2147 /* If driver didn't explicitly set these, use the defaults */
2148 if (!texImage->FetchTexelc)
2149 texImage->FetchTexelc = texImage->TexFormat->FetchTexel2D;
2150 if (!texImage->FetchTexelf)
2151 texImage->FetchTexelf = texImage->TexFormat->FetchTexel2Df;
2152 ASSERT(texImage->FetchTexelc);
2153 ASSERT(texImage->FetchTexelf);
2154
2155 /* state update */
2156 texObj->Complete = GL_FALSE;
2157 ctx->NewState |= _NEW_TEXTURE;
2158 }
2159 else if (target == GL_PROXY_TEXTURE_2D ||
2160 (target == GL_PROXY_TEXTURE_CUBE_MAP_ARB &&
2161 ctx->Extensions.ARB_texture_cube_map) ||
2162 (target == GL_PROXY_TEXTURE_RECTANGLE_NV &&
2163 ctx->Extensions.NV_texture_rectangle)) {
2164 /* Proxy texture: check for errors and update proxy state */
2165 struct gl_texture_image *texImage;
2166 texImage = _mesa_get_proxy_tex_image(ctx, target, level);
2167 if (texture_error_check(ctx, target, level, internalFormat,
2168 format, type, 2, postConvWidth, postConvHeight,
2169 1, border)) {
2170 /* when error, clear all proxy texture image parameters */
2171 if (texImage)
2172 clear_teximage_fields(ctx->Texture.Proxy2D->Image[0][level]);
2173 }
2174 else {
2175 /* no error, set the tex image parameters */
2176 _mesa_init_teximage_fields(ctx, target, texImage,
2177 postConvWidth, postConvHeight, 1,
2178 border, internalFormat);
2179 texImage->TexFormat = (*ctx->Driver.ChooseTextureFormat)(ctx,
2180 internalFormat, format, type);
2181 }
2182 }
2183 else {
2184 _mesa_error( ctx, GL_INVALID_ENUM, "glTexImage2D(target)" );
2185 return;
2186 }
2187 }
2188
2189
2190 /*
2191 * Called by the API or display list executor.
2192 * Note that width and height include the border.
2193 */
2194 void GLAPIENTRY
2195 _mesa_TexImage3D( GLenum target, GLint level, GLint internalFormat,
2196 GLsizei width, GLsizei height, GLsizei depth,
2197 GLint border, GLenum format, GLenum type,
2198 const GLvoid *pixels )
2199 {
2200 GET_CURRENT_CONTEXT(ctx);
2201 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2202
2203 if (target == GL_TEXTURE_3D) {
2204 struct gl_texture_unit *texUnit;
2205 struct gl_texture_object *texObj;
2206 struct gl_texture_image *texImage;
2207
2208 if (texture_error_check(ctx, target, level, (GLint) internalFormat,
2209 format, type, 3, width, height, depth, border)) {
2210 return; /* error was recorded */
2211 }
2212
2213 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2214 texObj = _mesa_select_tex_object(ctx, texUnit, target);
2215 texImage = _mesa_get_tex_image(ctx, texUnit, target, level);
2216 if (!texImage) {
2217 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage3D");
2218 return;
2219 }
2220 else if (texImage->Data && !texImage->IsClientData) {
2221 MESA_PBUFFER_FREE(texImage->Data);
2222 }
2223 texImage->Data = NULL;
2224 clear_teximage_fields(texImage); /* not really needed, but helpful */
2225 _mesa_init_teximage_fields(ctx, target, texImage,
2226 width, height, depth,
2227 border, internalFormat);
2228
2229 if (ctx->NewState & _IMAGE_NEW_TRANSFER_STATE)
2230 _mesa_update_state(ctx);
2231
2232 ASSERT(ctx->Driver.TexImage3D);
2233
2234 /* Give the texture to the driver! <pixels> may be null! */
2235 (*ctx->Driver.TexImage3D)(ctx, target, level, internalFormat,
2236 width, height, depth, border, format, type,
2237 pixels, &ctx->Unpack, texObj, texImage);
2238
2239 ASSERT(texImage->TexFormat);
2240
2241 /* If driver didn't explicitly set these, use the defaults */
2242 if (!texImage->FetchTexelc)
2243 texImage->FetchTexelc = texImage->TexFormat->FetchTexel3D;
2244 if (!texImage->FetchTexelf)
2245 texImage->FetchTexelf = texImage->TexFormat->FetchTexel3Df;
2246 ASSERT(texImage->FetchTexelc);
2247 ASSERT(texImage->FetchTexelf);
2248
2249 /* state update */
2250 texObj->Complete = GL_FALSE;
2251 ctx->NewState |= _NEW_TEXTURE;
2252 }
2253 else if (target == GL_PROXY_TEXTURE_3D) {
2254 /* Proxy texture: check for errors and update proxy state */
2255 struct gl_texture_image *texImage;
2256 texImage = _mesa_get_proxy_tex_image(ctx, target, level);
2257 if (texture_error_check(ctx, target, level, internalFormat,
2258 format, type, 3, width, height, depth, border)) {
2259 /* when error, clear all proxy texture image parameters */
2260 if (texImage)
2261 clear_teximage_fields(texImage);
2262 }
2263 else {
2264 /* no error, set the tex image parameters */
2265 _mesa_init_teximage_fields(ctx, target, texImage, width, height, 1,
2266 border, internalFormat);
2267 texImage->TexFormat = (*ctx->Driver.ChooseTextureFormat)(ctx,
2268 internalFormat, format, type);
2269 }
2270 }
2271 else {
2272 _mesa_error( ctx, GL_INVALID_ENUM, "glTexImage3D(target)" );
2273 return;
2274 }
2275 }
2276
2277
2278 void GLAPIENTRY
2279 _mesa_TexImage3DEXT( GLenum target, GLint level, GLenum internalFormat,
2280 GLsizei width, GLsizei height, GLsizei depth,
2281 GLint border, GLenum format, GLenum type,
2282 const GLvoid *pixels )
2283 {
2284 _mesa_TexImage3D(target, level, (GLint) internalFormat, width, height,
2285 depth, border, format, type, pixels);
2286 }
2287
2288
2289
2290 void GLAPIENTRY
2291 _mesa_TexSubImage1D( GLenum target, GLint level,
2292 GLint xoffset, GLsizei width,
2293 GLenum format, GLenum type,
2294 const GLvoid *pixels )
2295 {
2296 GLsizei postConvWidth = width;
2297 struct gl_texture_unit *texUnit;
2298 struct gl_texture_object *texObj;
2299 struct gl_texture_image *texImage;
2300 GET_CURRENT_CONTEXT(ctx);
2301 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2302
2303 if (ctx->NewState & _IMAGE_NEW_TRANSFER_STATE)
2304 _mesa_update_state(ctx);
2305
2306 /* XXX should test internal format */
2307 if (is_color_format(format)) {
2308 _mesa_adjust_image_for_convolution(ctx, 1, &postConvWidth, NULL);
2309 }
2310
2311 if (subtexture_error_check(ctx, 1, target, level, xoffset, 0, 0,
2312 postConvWidth, 1, 1, format, type)) {
2313 return; /* error was detected */
2314 }
2315
2316 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2317 texObj = _mesa_select_tex_object(ctx, texUnit, target);
2318 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
2319 assert(texImage);
2320
2321 if (width == 0)
2322 return; /* no-op, not an error */
2323
2324 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2325 xoffset += texImage->Border;
2326
2327 ASSERT(ctx->Driver.TexSubImage1D);
2328 (*ctx->Driver.TexSubImage1D)(ctx, target, level, xoffset, width,
2329 format, type, pixels, &ctx->Unpack,
2330 texObj, texImage);
2331 ctx->NewState |= _NEW_TEXTURE;
2332 }
2333
2334
2335 void GLAPIENTRY
2336 _mesa_TexSubImage2D( GLenum target, GLint level,
2337 GLint xoffset, GLint yoffset,
2338 GLsizei width, GLsizei height,
2339 GLenum format, GLenum type,
2340 const GLvoid *pixels )
2341 {
2342 GLsizei postConvWidth = width, postConvHeight = height;
2343 struct gl_texture_unit *texUnit;
2344 struct gl_texture_object *texObj;
2345 struct gl_texture_image *texImage;
2346 GET_CURRENT_CONTEXT(ctx);
2347 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2348
2349 if (ctx->NewState & _IMAGE_NEW_TRANSFER_STATE)
2350 _mesa_update_state(ctx);
2351
2352 /* XXX should test internal format */
2353 if (is_color_format(format)) {
2354 _mesa_adjust_image_for_convolution(ctx, 2, &postConvWidth,
2355 &postConvHeight);
2356 }
2357
2358 if (subtexture_error_check(ctx, 2, target, level, xoffset, yoffset, 0,
2359 postConvWidth, postConvHeight, 1, format, type)) {
2360 return; /* error was detected */
2361 }
2362
2363 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2364 texObj = _mesa_select_tex_object(ctx, texUnit, target);
2365 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
2366 assert(texImage);
2367
2368 if (width == 0 || height == 0)
2369 return; /* no-op, not an error */
2370
2371 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2372 xoffset += texImage->Border;
2373 yoffset += texImage->Border;
2374
2375 ASSERT(ctx->Driver.TexSubImage2D);
2376 (*ctx->Driver.TexSubImage2D)(ctx, target, level, xoffset, yoffset,
2377 width, height, format, type, pixels,
2378 &ctx->Unpack, texObj, texImage);
2379 ctx->NewState |= _NEW_TEXTURE;
2380 }
2381
2382
2383
2384 void GLAPIENTRY
2385 _mesa_TexSubImage3D( GLenum target, GLint level,
2386 GLint xoffset, GLint yoffset, GLint zoffset,
2387 GLsizei width, GLsizei height, GLsizei depth,
2388 GLenum format, GLenum type,
2389 const GLvoid *pixels )
2390 {
2391 struct gl_texture_unit *texUnit;
2392 struct gl_texture_object *texObj;
2393 struct gl_texture_image *texImage;
2394 GET_CURRENT_CONTEXT(ctx);
2395 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2396
2397 if (ctx->NewState & _IMAGE_NEW_TRANSFER_STATE)
2398 _mesa_update_state(ctx);
2399
2400 if (subtexture_error_check(ctx, 3, target, level, xoffset, yoffset, zoffset,
2401 width, height, depth, format, type)) {
2402 return; /* error was detected */
2403 }
2404
2405 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2406 texObj = _mesa_select_tex_object(ctx, texUnit, target);
2407 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
2408 assert(texImage);
2409
2410 if (width == 0 || height == 0 || height == 0)
2411 return; /* no-op, not an error */
2412
2413 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2414 xoffset += texImage->Border;
2415 yoffset += texImage->Border;
2416 zoffset += texImage->Border;
2417
2418 ASSERT(ctx->Driver.TexSubImage3D);
2419 (*ctx->Driver.TexSubImage3D)(ctx, target, level,
2420 xoffset, yoffset, zoffset,
2421 width, height, depth,
2422 format, type, pixels,
2423 &ctx->Unpack, texObj, texImage );
2424 ctx->NewState |= _NEW_TEXTURE;
2425 }
2426
2427
2428
2429 void GLAPIENTRY
2430 _mesa_CopyTexImage1D( GLenum target, GLint level,
2431 GLenum internalFormat,
2432 GLint x, GLint y,
2433 GLsizei width, GLint border )
2434 {
2435 struct gl_texture_unit *texUnit;
2436 struct gl_texture_object *texObj;
2437 struct gl_texture_image *texImage;
2438 GLsizei postConvWidth = width;
2439 GET_CURRENT_CONTEXT(ctx);
2440 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2441
2442 if (ctx->NewState & _IMAGE_NEW_TRANSFER_STATE)
2443 _mesa_update_state(ctx);
2444
2445 if (is_color_format(internalFormat)) {
2446 _mesa_adjust_image_for_convolution(ctx, 1, &postConvWidth, NULL);
2447 }
2448
2449 if (copytexture_error_check(ctx, 1, target, level, internalFormat,
2450 postConvWidth, 1, border))
2451 return;
2452
2453 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2454 texObj = _mesa_select_tex_object(ctx, texUnit, target);
2455 texImage = _mesa_get_tex_image(ctx, texUnit, target, level);
2456 if (!texImage) {
2457 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexImage1D");
2458 return;
2459 }
2460 else if (texImage->Data && !texImage->IsClientData) {
2461 /* free the old texture data */
2462 MESA_PBUFFER_FREE(texImage->Data);
2463 }
2464 texImage->Data = NULL;
2465
2466 clear_teximage_fields(texImage); /* not really needed, but helpful */
2467 _mesa_init_teximage_fields(ctx, target, texImage, postConvWidth, 1, 1,
2468 border, internalFormat);
2469
2470
2471 ASSERT(ctx->Driver.CopyTexImage1D);
2472 (*ctx->Driver.CopyTexImage1D)(ctx, target, level, internalFormat,
2473 x, y, width, border);
2474
2475 ASSERT(texImage->TexFormat);
2476
2477 /* If driver didn't explicitly set these, use the defaults */
2478 if (!texImage->FetchTexelc)
2479 texImage->FetchTexelc = texImage->TexFormat->FetchTexel1D;
2480 if (!texImage->FetchTexelf)
2481 texImage->FetchTexelf = texImage->TexFormat->FetchTexel1Df;
2482 ASSERT(texImage->FetchTexelc);
2483 ASSERT(texImage->FetchTexelf);
2484
2485 /* state update */
2486 texObj->Complete = GL_FALSE;
2487 ctx->NewState |= _NEW_TEXTURE;
2488 }
2489
2490
2491
2492 void GLAPIENTRY
2493 _mesa_CopyTexImage2D( GLenum target, GLint level, GLenum internalFormat,
2494 GLint x, GLint y, GLsizei width, GLsizei height,
2495 GLint border )
2496 {
2497 struct gl_texture_unit *texUnit;
2498 struct gl_texture_object *texObj;
2499 struct gl_texture_image *texImage;
2500 GLsizei postConvWidth = width, postConvHeight = height;
2501 GET_CURRENT_CONTEXT(ctx);
2502 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2503
2504 if (ctx->NewState & _IMAGE_NEW_TRANSFER_STATE)
2505 _mesa_update_state(ctx);
2506
2507 if (is_color_format(internalFormat)) {
2508 _mesa_adjust_image_for_convolution(ctx, 2, &postConvWidth,
2509 &postConvHeight);
2510 }
2511
2512 if (copytexture_error_check(ctx, 2, target, level, internalFormat,
2513 postConvWidth, postConvHeight, border))
2514 return;
2515
2516 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2517 texObj = _mesa_select_tex_object(ctx, texUnit, target);
2518 texImage = _mesa_get_tex_image(ctx, texUnit, target, level);
2519 if (!texImage) {
2520 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexImage2D");
2521 return;
2522 }
2523 else if (texImage->Data && !texImage->IsClientData) {
2524 /* free the old texture data */
2525 MESA_PBUFFER_FREE(texImage->Data);
2526 }
2527 texImage->Data = NULL;
2528
2529 clear_teximage_fields(texImage); /* not really needed, but helpful */
2530 _mesa_init_teximage_fields(ctx, target, texImage,
2531 postConvWidth, postConvHeight, 1,
2532 border, internalFormat);
2533
2534 ASSERT(ctx->Driver.CopyTexImage2D);
2535 (*ctx->Driver.CopyTexImage2D)(ctx, target, level, internalFormat,
2536 x, y, width, height, border);
2537
2538 ASSERT(texImage->TexFormat);
2539
2540 /* If driver didn't explicitly set these, use the defaults */
2541 if (!texImage->FetchTexelc)
2542 texImage->FetchTexelc = texImage->TexFormat->FetchTexel2D;
2543 if (!texImage->FetchTexelf)
2544 texImage->FetchTexelf = texImage->TexFormat->FetchTexel2Df;
2545 ASSERT(texImage->FetchTexelc);
2546 ASSERT(texImage->FetchTexelf);
2547
2548 /* state update */
2549 texObj->Complete = GL_FALSE;
2550 ctx->NewState |= _NEW_TEXTURE;
2551 }
2552
2553
2554
2555 void GLAPIENTRY
2556 _mesa_CopyTexSubImage1D( GLenum target, GLint level,
2557 GLint xoffset, GLint x, GLint y, GLsizei width )
2558 {
2559 struct gl_texture_unit *texUnit;
2560 struct gl_texture_image *texImage;
2561 GLsizei postConvWidth = width;
2562 GET_CURRENT_CONTEXT(ctx);
2563 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2564
2565 if (ctx->NewState & _IMAGE_NEW_TRANSFER_STATE)
2566 _mesa_update_state(ctx);
2567
2568 /* XXX should test internal format */
2569 _mesa_adjust_image_for_convolution(ctx, 1, &postConvWidth, NULL);
2570
2571 if (copytexsubimage_error_check(ctx, 1, target, level,
2572 xoffset, 0, 0, postConvWidth, 1))
2573 return;
2574
2575 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2576 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
2577 ASSERT(texImage);
2578
2579 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2580 xoffset += texImage->Border;
2581
2582 ASSERT(ctx->Driver.CopyTexSubImage1D);
2583 (*ctx->Driver.CopyTexSubImage1D)(ctx, target, level, xoffset, x, y, width);
2584 ctx->NewState |= _NEW_TEXTURE;
2585 }
2586
2587
2588
2589 void GLAPIENTRY
2590 _mesa_CopyTexSubImage2D( GLenum target, GLint level,
2591 GLint xoffset, GLint yoffset,
2592 GLint x, GLint y, GLsizei width, GLsizei height )
2593 {
2594 struct gl_texture_unit *texUnit;
2595 struct gl_texture_image *texImage;
2596 GLsizei postConvWidth = width, postConvHeight = height;
2597 GET_CURRENT_CONTEXT(ctx);
2598 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2599
2600 if (ctx->NewState & _IMAGE_NEW_TRANSFER_STATE)
2601 _mesa_update_state(ctx);
2602
2603 /* XXX should test internal format */
2604 _mesa_adjust_image_for_convolution(ctx, 2, &postConvWidth, &postConvHeight);
2605
2606 if (copytexsubimage_error_check(ctx, 2, target, level, xoffset, yoffset, 0,
2607 postConvWidth, postConvHeight))
2608 return;
2609
2610 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2611 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
2612 ASSERT(texImage);
2613
2614 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2615 xoffset += texImage->Border;
2616 yoffset += texImage->Border;
2617
2618 ASSERT(ctx->Driver.CopyTexSubImage2D);
2619 (*ctx->Driver.CopyTexSubImage2D)(ctx, target, level,
2620 xoffset, yoffset, x, y, width, height);
2621 ctx->NewState |= _NEW_TEXTURE;
2622 }
2623
2624
2625
2626 void GLAPIENTRY
2627 _mesa_CopyTexSubImage3D( GLenum target, GLint level,
2628 GLint xoffset, GLint yoffset, GLint zoffset,
2629 GLint x, GLint y, GLsizei width, GLsizei height )
2630 {
2631 struct gl_texture_unit *texUnit;
2632 struct gl_texture_image *texImage;
2633 GLsizei postConvWidth = width, postConvHeight = height;
2634 GET_CURRENT_CONTEXT(ctx);
2635 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2636
2637 if (ctx->NewState & _IMAGE_NEW_TRANSFER_STATE)
2638 _mesa_update_state(ctx);
2639
2640 /* XXX should test internal format */
2641 _mesa_adjust_image_for_convolution(ctx, 2, &postConvWidth, &postConvHeight);
2642
2643 if (copytexsubimage_error_check(ctx, 3, target, level, xoffset, yoffset,
2644 zoffset, postConvWidth, postConvHeight))
2645 return;
2646
2647 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2648 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
2649 ASSERT(texImage);
2650
2651 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2652 xoffset += texImage->Border;
2653 yoffset += texImage->Border;
2654 zoffset += texImage->Border;
2655
2656 ASSERT(ctx->Driver.CopyTexSubImage3D);
2657 (*ctx->Driver.CopyTexSubImage3D)(ctx, target, level,
2658 xoffset, yoffset, zoffset,
2659 x, y, width, height);
2660 ctx->NewState |= _NEW_TEXTURE;
2661 }
2662
2663
2664
2665
2666 /**********************************************************************/
2667 /****** Compressed Textures ******/
2668 /**********************************************************************/
2669
2670
2671 /**
2672 * Error checking for glCompressedTexImage[123]D().
2673 * \return error code or GL_NO_ERROR.
2674 */
2675 static GLenum
2676 compressed_texture_error_check(GLcontext *ctx, GLint dimensions,
2677 GLenum target, GLint level,
2678 GLenum internalFormat, GLsizei width,
2679 GLsizei height, GLsizei depth, GLint border,
2680 GLsizei imageSize)
2681 {
2682 GLint expectedSize, maxLevels = 0, maxTextureSize;
2683
2684 if (dimensions == 1) {
2685 /* 1D compressed textures not allowed */
2686 return GL_INVALID_ENUM;
2687 }
2688 else if (dimensions == 2) {
2689 if (target == GL_PROXY_TEXTURE_2D) {
2690 maxLevels = ctx->Const.MaxTextureLevels;
2691 }
2692 else if (target == GL_TEXTURE_2D) {
2693 maxLevels = ctx->Const.MaxTextureLevels;
2694 }
2695 else if (target == GL_PROXY_TEXTURE_CUBE_MAP_ARB) {
2696 if (!ctx->Extensions.ARB_texture_cube_map)
2697 return GL_INVALID_ENUM; /*target*/
2698 maxLevels = ctx->Const.MaxCubeTextureLevels;
2699 }
2700 else if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
2701 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB) {
2702 if (!ctx->Extensions.ARB_texture_cube_map)
2703 return GL_INVALID_ENUM; /*target*/
2704 maxLevels = ctx->Const.MaxCubeTextureLevels;
2705 }
2706 else {
2707 return GL_INVALID_ENUM; /*target*/
2708 }
2709 }
2710 else if (dimensions == 3) {
2711 /* 3D compressed textures not allowed */
2712 return GL_INVALID_ENUM;
2713 }
2714
2715 maxTextureSize = 1 << (maxLevels - 1);
2716
2717 if (!is_compressed_format(ctx, internalFormat))
2718 return GL_INVALID_ENUM;
2719
2720 if (_mesa_base_tex_format(ctx, internalFormat) < 0)
2721 return GL_INVALID_ENUM;
2722
2723 if (border != 0)
2724 return GL_INVALID_VALUE;
2725
2726 /*
2727 * XXX We should probably use the proxy texture error check function here.
2728 */
2729 if (width < 1 || width > maxTextureSize ||
2730 (!ctx->Extensions.ARB_texture_non_power_of_two && _mesa_bitcount(width) != 1))
2731 return GL_INVALID_VALUE;
2732
2733 if ((height < 1 || height > maxTextureSize ||
2734 (!ctx->Extensions.ARB_texture_non_power_of_two && _mesa_bitcount(height) != 1))
2735 && dimensions > 1)
2736 return GL_INVALID_VALUE;
2737
2738 if ((depth < 1 || depth > maxTextureSize ||
2739 (!ctx->Extensions.ARB_texture_non_power_of_two && _mesa_bitcount(depth) != 1))
2740 && dimensions > 2)
2741 return GL_INVALID_VALUE;
2742
2743 /* For cube map, width must equal height */
2744 if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
2745 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB && width != height)
2746 return GL_INVALID_VALUE;
2747
2748 if (level < 0 || level >= maxLevels)
2749 return GL_INVALID_VALUE;
2750
2751 expectedSize = ctx->Driver.CompressedTextureSize(ctx, width, height, depth,
2752 internalFormat);
2753 if (expectedSize != imageSize)
2754 return GL_INVALID_VALUE;
2755
2756 return GL_NO_ERROR;
2757 }
2758
2759
2760 /**
2761 * Error checking for glCompressedTexSubImage[123]D().
2762 * \warning There are some bad assumptions here about the size of compressed
2763 * texture tiles (multiple of 4) used to test the validity of the
2764 * offset and size parameters.
2765 * \return error code or GL_NO_ERROR.
2766 */
2767 static GLenum
2768 compressed_subtexture_error_check(GLcontext *ctx, GLint dimensions,
2769 GLenum target, GLint level,
2770 GLint xoffset, GLint yoffset, GLint zoffset,
2771 GLsizei width, GLsizei height, GLsizei depth,
2772 GLenum format, GLsizei imageSize)
2773 {
2774 GLint expectedSize, maxLevels = 0, maxTextureSize;
2775 (void) zoffset;
2776
2777 if (dimensions == 1) {
2778 /* 1D compressed textures not allowed */
2779 return GL_INVALID_ENUM;
2780 }
2781 else if (dimensions == 2) {
2782 if (target == GL_PROXY_TEXTURE_2D) {
2783 maxLevels = ctx->Const.MaxTextureLevels;
2784 }
2785 else if (target == GL_TEXTURE_2D) {
2786 maxLevels = ctx->Const.MaxTextureLevels;
2787 }
2788 else if (target == GL_PROXY_TEXTURE_CUBE_MAP_ARB) {
2789 if (!ctx->Extensions.ARB_texture_cube_map)
2790 return GL_INVALID_ENUM; /*target*/
2791 maxLevels = ctx->Const.MaxCubeTextureLevels;
2792 }
2793 else if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
2794 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB) {
2795 if (!ctx->Extensions.ARB_texture_cube_map)
2796 return GL_INVALID_ENUM; /*target*/
2797 maxLevels = ctx->Const.MaxCubeTextureLevels;
2798 }
2799 else {
2800 return GL_INVALID_ENUM; /*target*/
2801 }
2802 }
2803 else if (dimensions == 3) {
2804 /* 3D compressed textures not allowed */
2805 return GL_INVALID_ENUM;
2806 }
2807
2808 maxTextureSize = 1 << (maxLevels - 1);
2809
2810 if (!is_compressed_format(ctx, format))
2811 return GL_INVALID_ENUM;
2812
2813 if (width < 1 || width > maxTextureSize)
2814 return GL_INVALID_VALUE;
2815
2816 if ((height < 1 || height > maxTextureSize)
2817 && dimensions > 1)
2818 return GL_INVALID_VALUE;
2819
2820 if (level < 0 || level >= maxLevels)
2821 return GL_INVALID_VALUE;
2822
2823 /* XXX these tests are specific to the compressed format.
2824 * this code should be generalized in some way.
2825 */
2826 if ((xoffset & 3) != 0 || (yoffset & 3) != 0)
2827 return GL_INVALID_VALUE;
2828
2829 if ((width & 3) != 0 && width != 2 && width != 1)
2830 return GL_INVALID_VALUE;
2831
2832 if ((height & 3) != 0 && height != 2 && height != 1)
2833 return GL_INVALID_VALUE;
2834
2835 expectedSize = ctx->Driver.CompressedTextureSize(ctx, width, height, depth,
2836 format);
2837 if (expectedSize != imageSize)
2838 return GL_INVALID_VALUE;
2839
2840 return GL_NO_ERROR;
2841 }
2842
2843
2844
2845 void GLAPIENTRY
2846 _mesa_CompressedTexImage1DARB(GLenum target, GLint level,
2847 GLenum internalFormat, GLsizei width,
2848 GLint border, GLsizei imageSize,
2849 const GLvoid *data)
2850 {
2851 GET_CURRENT_CONTEXT(ctx);
2852 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2853
2854 if (target == GL_TEXTURE_1D) {
2855 struct gl_texture_unit *texUnit;
2856 struct gl_texture_object *texObj;
2857 struct gl_texture_image *texImage;
2858 GLenum error = compressed_texture_error_check(ctx, 1, target, level,
2859 internalFormat, width, 1, 1, border, imageSize);
2860 if (error) {
2861 _mesa_error(ctx, error, "glCompressedTexImage1D");
2862 return;
2863 }
2864
2865 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2866 texObj = _mesa_select_tex_object(ctx, texUnit, target);
2867 texImage = _mesa_get_tex_image(ctx, texUnit, target, level);
2868 if (!texImage) {
2869 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCompressedTexImage1D");
2870 return;
2871 }
2872 else if (texImage->Data && !texImage->IsClientData) {
2873 MESA_PBUFFER_FREE(texImage->Data);
2874 }
2875 texImage->Data = NULL;
2876
2877 _mesa_init_teximage_fields(ctx, target, texImage, width, 1, 1,
2878 border, internalFormat);
2879
2880 ASSERT(ctx->Driver.CompressedTexImage1D);
2881 (*ctx->Driver.CompressedTexImage1D)(ctx, target, level,
2882 internalFormat, width, border,
2883 imageSize, data,
2884 texObj, texImage);
2885
2886 /* state update */
2887 texObj->Complete = GL_FALSE;
2888 ctx->NewState |= _NEW_TEXTURE;
2889 }
2890 else if (target == GL_PROXY_TEXTURE_1D) {
2891 /* Proxy texture: check for errors and update proxy state */
2892 GLenum error = compressed_texture_error_check(ctx, 1, target, level,
2893 internalFormat, width, 1, 1, border, imageSize);
2894 if (!error) {
2895 ASSERT(ctx->Driver.TestProxyTexImage);
2896 error = !(*ctx->Driver.TestProxyTexImage)(ctx, target, level,
2897 internalFormat, GL_NONE, GL_NONE,
2898 width, 1, 1, border);
2899 }
2900 if (error) {
2901 /* if error, clear all proxy texture image parameters */
2902 struct gl_texture_image *texImage;
2903 texImage = _mesa_get_proxy_tex_image(ctx, target, level);
2904 if (texImage)
2905 clear_teximage_fields(texImage);
2906 }
2907 else {
2908 /* store the teximage parameters */
2909 struct gl_texture_unit *texUnit;
2910 struct gl_texture_image *texImage;
2911 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2912 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
2913 _mesa_init_teximage_fields(ctx, target, texImage, width, 1, 1,
2914 border, internalFormat);
2915 }
2916 }
2917 else {
2918 _mesa_error(ctx, GL_INVALID_ENUM, "glCompressedTexImage1D(target)");
2919 return;
2920 }
2921 }
2922
2923
2924 void GLAPIENTRY
2925 _mesa_CompressedTexImage2DARB(GLenum target, GLint level,
2926 GLenum internalFormat, GLsizei width,
2927 GLsizei height, GLint border, GLsizei imageSize,
2928 const GLvoid *data)
2929 {
2930 GET_CURRENT_CONTEXT(ctx);
2931 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2932
2933 if (target == GL_TEXTURE_2D ||
2934 (ctx->Extensions.ARB_texture_cube_map &&
2935 target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
2936 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB)) {
2937 struct gl_texture_unit *texUnit;
2938 struct gl_texture_object *texObj;
2939 struct gl_texture_image *texImage;
2940 GLenum error = compressed_texture_error_check(ctx, 2, target, level,
2941 internalFormat, width, height, 1, border, imageSize);
2942 if (error) {
2943 _mesa_error(ctx, error, "glCompressedTexImage2D");
2944 return;
2945 }
2946
2947 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2948 texObj = _mesa_select_tex_object(ctx, texUnit, target);
2949 texImage = _mesa_get_tex_image(ctx, texUnit, target, level);
2950 if (!texImage) {
2951 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCompressedTexImage2D");
2952 return;
2953 }
2954 else if (texImage->Data && !texImage->IsClientData) {
2955 MESA_PBUFFER_FREE(texImage->Data);
2956 }
2957 texImage->Data = NULL;
2958
2959 _mesa_init_teximage_fields(ctx, target, texImage, width, height, 1,
2960 border, internalFormat);
2961
2962 ASSERT(ctx->Driver.CompressedTexImage2D);
2963 (*ctx->Driver.CompressedTexImage2D)(ctx, target, level,
2964 internalFormat, width, height,
2965 border, imageSize, data,
2966 texObj, texImage);
2967
2968 /* state update */
2969 texObj->Complete = GL_FALSE;
2970 ctx->NewState |= _NEW_TEXTURE;
2971 }
2972 else if (target == GL_PROXY_TEXTURE_2D ||
2973 (target == GL_PROXY_TEXTURE_CUBE_MAP_ARB &&
2974 ctx->Extensions.ARB_texture_cube_map)) {
2975 /* Proxy texture: check for errors and update proxy state */
2976 GLenum error = compressed_texture_error_check(ctx, 2, target, level,
2977 internalFormat, width, height, 1, border, imageSize);
2978 if (!error) {
2979 ASSERT(ctx->Driver.TestProxyTexImage);
2980 error = !(*ctx->Driver.TestProxyTexImage)(ctx, target, level,
2981 internalFormat, GL_NONE, GL_NONE,
2982 width, height, 1, border);
2983 }
2984 if (error) {
2985 /* if error, clear all proxy texture image parameters */
2986 struct gl_texture_image *texImage;
2987 texImage = _mesa_get_proxy_tex_image(ctx, target, level);
2988 if (texImage)
2989 clear_teximage_fields(texImage);
2990 }
2991 else {
2992 /* store the teximage parameters */
2993 struct gl_texture_unit *texUnit;
2994 struct gl_texture_image *texImage;
2995 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2996 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
2997 _mesa_init_teximage_fields(ctx, target, texImage, width, height, 1,
2998 border, internalFormat);
2999 }
3000 }
3001 else {
3002 _mesa_error(ctx, GL_INVALID_ENUM, "glCompressedTexImage2D(target)");
3003 return;
3004 }
3005 }
3006
3007
3008 void GLAPIENTRY
3009 _mesa_CompressedTexImage3DARB(GLenum target, GLint level,
3010 GLenum internalFormat, GLsizei width,
3011 GLsizei height, GLsizei depth, GLint border,
3012 GLsizei imageSize, const GLvoid *data)
3013 {
3014 GET_CURRENT_CONTEXT(ctx);
3015 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3016
3017 if (target == GL_TEXTURE_3D) {
3018 struct gl_texture_unit *texUnit;
3019 struct gl_texture_object *texObj;
3020 struct gl_texture_image *texImage;
3021 GLenum error = compressed_texture_error_check(ctx, 3, target, level,
3022 internalFormat, width, height, depth, border, imageSize);
3023 if (error) {
3024 _mesa_error(ctx, error, "glCompressedTexImage3D");
3025 return;
3026 }
3027
3028 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
3029 texObj = _mesa_select_tex_object(ctx, texUnit, target);
3030 texImage = _mesa_get_tex_image(ctx, texUnit, target, level);
3031 if (!texImage) {
3032 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCompressedTexImage3D");
3033 return;
3034 }
3035 else if (texImage->Data && !texImage->IsClientData) {
3036 MESA_PBUFFER_FREE(texImage->Data);
3037 }
3038 texImage->Data = NULL;
3039
3040 _mesa_init_teximage_fields(ctx, target, texImage, width, height, depth,
3041 border, internalFormat);
3042
3043 ASSERT(ctx->Driver.CompressedTexImage3D);
3044 (*ctx->Driver.CompressedTexImage3D)(ctx, target, level,
3045 internalFormat,
3046 width, height, depth,
3047 border, imageSize, data,
3048 texObj, texImage);
3049
3050 /* state update */
3051 texObj->Complete = GL_FALSE;
3052 ctx->NewState |= _NEW_TEXTURE;
3053 }
3054 else if (target == GL_PROXY_TEXTURE_3D) {
3055 /* Proxy texture: check for errors and update proxy state */
3056 GLenum error = compressed_texture_error_check(ctx, 3, target, level,
3057 internalFormat, width, height, depth, border, imageSize);
3058 if (!error) {
3059 ASSERT(ctx->Driver.TestProxyTexImage);
3060 error = !(*ctx->Driver.TestProxyTexImage)(ctx, target, level,
3061 internalFormat, GL_NONE, GL_NONE,
3062 width, height, depth, border);
3063 }
3064 if (error) {
3065 /* if error, clear all proxy texture image parameters */
3066 struct gl_texture_image *texImage;
3067 texImage = _mesa_get_proxy_tex_image(ctx, target, level);
3068 if (texImage)
3069 clear_teximage_fields(texImage);
3070 }
3071 else {
3072 /* store the teximage parameters */
3073 struct gl_texture_unit *texUnit;
3074 struct gl_texture_image *texImage;
3075 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
3076 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
3077 _mesa_init_teximage_fields(ctx, target, texImage, width, height,
3078 depth, border, internalFormat);
3079 }
3080 }
3081 else {
3082 _mesa_error(ctx, GL_INVALID_ENUM, "glCompressedTexImage3D(target)");
3083 return;
3084 }
3085 }
3086
3087
3088 void GLAPIENTRY
3089 _mesa_CompressedTexSubImage1DARB(GLenum target, GLint level, GLint xoffset,
3090 GLsizei width, GLenum format,
3091 GLsizei imageSize, const GLvoid *data)
3092 {
3093 struct gl_texture_unit *texUnit;
3094 struct gl_texture_object *texObj;
3095 struct gl_texture_image *texImage;
3096 GLenum error;
3097 GET_CURRENT_CONTEXT(ctx);
3098 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3099
3100 error = compressed_subtexture_error_check(ctx, 1, target, level,
3101 xoffset, 0, 0, width, 1, 1, format, imageSize);
3102 if (error) {
3103 _mesa_error(ctx, error, "glCompressedTexSubImage1D");
3104 return;
3105 }
3106
3107 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
3108 texObj = _mesa_select_tex_object(ctx, texUnit, target);
3109 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
3110 assert(texImage);
3111
3112 if ((GLint) format != texImage->IntFormat) {
3113 _mesa_error(ctx, GL_INVALID_OPERATION,
3114 "glCompressedTexSubImage1D(format)");
3115 return;
3116 }
3117
3118 if ((width == 1 || width == 2) && (GLuint) width != texImage->Width) {
3119 _mesa_error(ctx, GL_INVALID_VALUE, "glCompressedTexSubImage1D(width)");
3120 return;
3121 }
3122
3123 if (width == 0)
3124 return; /* no-op, not an error */
3125
3126 if (ctx->Driver.CompressedTexSubImage1D) {
3127 (*ctx->Driver.CompressedTexSubImage1D)(ctx, target, level,
3128 xoffset, width,
3129 format, imageSize, data,
3130 texObj, texImage);
3131 }
3132 ctx->NewState |= _NEW_TEXTURE;
3133 }
3134
3135
3136 void GLAPIENTRY
3137 _mesa_CompressedTexSubImage2DARB(GLenum target, GLint level, GLint xoffset,
3138 GLint yoffset, GLsizei width, GLsizei height,
3139 GLenum format, GLsizei imageSize,
3140 const GLvoid *data)
3141 {
3142 struct gl_texture_unit *texUnit;
3143 struct gl_texture_object *texObj;
3144 struct gl_texture_image *texImage;
3145 GLenum error;
3146 GET_CURRENT_CONTEXT(ctx);
3147 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3148
3149 error = compressed_subtexture_error_check(ctx, 2, target, level,
3150 xoffset, yoffset, 0, width, height, 1, format, imageSize);
3151 if (error) {
3152 /* XXX proxy target? */
3153 _mesa_error(ctx, error, "glCompressedTexSubImage2D");
3154 return;
3155 }
3156
3157 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
3158 texObj = _mesa_select_tex_object(ctx, texUnit, target);
3159 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
3160 assert(texImage);
3161
3162 if ((GLint) format != texImage->IntFormat) {
3163 _mesa_error(ctx, GL_INVALID_OPERATION,
3164 "glCompressedTexSubImage2D(format)");
3165 return;
3166 }
3167
3168 if (((width == 1 || width == 2) && (GLuint) width != texImage->Width) ||
3169 ((height == 1 || height == 2) && (GLuint) height != texImage->Height)) {
3170 _mesa_error(ctx, GL_INVALID_VALUE, "glCompressedTexSubImage2D(size)");
3171 return;
3172 }
3173
3174 if (width == 0 || height == 0)
3175 return; /* no-op, not an error */
3176
3177 if (ctx->Driver.CompressedTexSubImage2D) {
3178 (*ctx->Driver.CompressedTexSubImage2D)(ctx, target, level,
3179 xoffset, yoffset, width, height,
3180 format, imageSize, data,
3181 texObj, texImage);
3182 }
3183 ctx->NewState |= _NEW_TEXTURE;
3184 }
3185
3186
3187 void GLAPIENTRY
3188 _mesa_CompressedTexSubImage3DARB(GLenum target, GLint level, GLint xoffset,
3189 GLint yoffset, GLint zoffset, GLsizei width,
3190 GLsizei height, GLsizei depth, GLenum format,
3191 GLsizei imageSize, const GLvoid *data)
3192 {
3193 struct gl_texture_unit *texUnit;
3194 struct gl_texture_object *texObj;
3195 struct gl_texture_image *texImage;
3196 GLenum error;
3197 GET_CURRENT_CONTEXT(ctx);
3198 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3199
3200 error = compressed_subtexture_error_check(ctx, 3, target, level,
3201 xoffset, yoffset, zoffset, width, height, depth, format, imageSize);
3202 if (error) {
3203 _mesa_error(ctx, error, "glCompressedTexSubImage2D");
3204 return;
3205 }
3206
3207 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
3208 texObj = _mesa_select_tex_object(ctx, texUnit, target);
3209 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
3210 assert(texImage);
3211
3212 if ((GLint) format != texImage->IntFormat) {
3213 _mesa_error(ctx, GL_INVALID_OPERATION,
3214 "glCompressedTexSubImage3D(format)");
3215 return;
3216 }
3217
3218 if (((width == 1 || width == 2) && (GLuint) width != texImage->Width) ||
3219 ((height == 1 || height == 2) && (GLuint) height != texImage->Height) ||
3220 ((depth == 1 || depth == 2) && (GLuint) depth != texImage->Depth)) {
3221 _mesa_error(ctx, GL_INVALID_VALUE, "glCompressedTexSubImage3D(size)");
3222 return;
3223 }
3224
3225 if (width == 0 || height == 0 || depth == 0)
3226 return; /* no-op, not an error */
3227
3228 if (ctx->Driver.CompressedTexSubImage3D) {
3229 (*ctx->Driver.CompressedTexSubImage3D)(ctx, target, level,
3230 xoffset, yoffset, zoffset,
3231 width, height, depth,
3232 format, imageSize, data,
3233 texObj, texImage);
3234 }
3235 ctx->NewState |= _NEW_TEXTURE;
3236 }
3237
3238
3239 void GLAPIENTRY
3240 _mesa_GetCompressedTexImageARB(GLenum target, GLint level, GLvoid *img)
3241 {
3242 const struct gl_texture_unit *texUnit;
3243 const struct gl_texture_object *texObj;
3244 struct gl_texture_image *texImage;
3245 GLint maxLevels;
3246 GET_CURRENT_CONTEXT(ctx);
3247 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3248
3249 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
3250 texObj = _mesa_select_tex_object(ctx, texUnit, target);
3251 if (!texObj) {
3252 _mesa_error(ctx, GL_INVALID_ENUM, "glGetCompressedTexImageARB");
3253 return;
3254 }
3255
3256 maxLevels = _mesa_max_texture_levels(ctx, target);
3257 ASSERT(maxLevels > 0); /* 0 indicates bad target, caught above */
3258
3259 if (level < 0 || level >= maxLevels) {
3260 _mesa_error(ctx, GL_INVALID_VALUE, "glGetCompressedTexImageARB(level)");
3261 return;
3262 }
3263
3264 if (is_proxy_target(target)) {
3265 _mesa_error(ctx, GL_INVALID_ENUM, "glGetCompressedTexImageARB(target)");
3266 return;
3267 }
3268
3269 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
3270 if (!texImage) {
3271 /* probably invalid mipmap level */
3272 _mesa_error(ctx, GL_INVALID_VALUE, "glGetCompressedTexImageARB(level)");
3273 return;
3274 }
3275
3276 if (!texImage->IsCompressed) {
3277 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetCompressedTexImageARB");
3278 return;
3279 }
3280
3281 /* this typically calls _mesa_get_compressed_teximage() */
3282 ctx->Driver.GetCompressedTexImage(ctx, target, level, img, texObj,texImage);
3283 }