Fix some warnings
[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
1193 /* Basic level check (more checking in ctx->Driver.TestProxyTexImage) */
1194 if (level < 0 || level >= MAX_TEXTURE_LEVELS) {
1195 if (!isProxy) {
1196 _mesa_error(ctx, GL_INVALID_VALUE,
1197 "glTexImage%dD(level=%d)", dimensions, level);
1198 }
1199 return GL_TRUE;
1200 }
1201
1202 /* Check border */
1203 if (border < 0 || border > 1 ||
1204 ((target == GL_TEXTURE_RECTANGLE_NV ||
1205 target == GL_PROXY_TEXTURE_RECTANGLE_NV) && border != 0)) {
1206 if (!isProxy) {
1207 _mesa_error(ctx, GL_INVALID_VALUE,
1208 "glTexImage%dD(border=%d)", dimensions, border);
1209 }
1210 return GL_TRUE;
1211 }
1212
1213 if (width < 0 || height < 0 || depth < 0) {
1214 if (!isProxy) {
1215 _mesa_error(ctx, GL_INVALID_VALUE,
1216 "glTexImage%dD(width, height or depth < 0)", dimensions);
1217 }
1218 return GL_TRUE;
1219 }
1220
1221 /* Check target and call ctx->Driver.TestProxyTexImage() to check the
1222 * level, width, height and depth.
1223 */
1224 if (dimensions == 1) {
1225 if (target == GL_PROXY_TEXTURE_1D || target == GL_TEXTURE_1D) {
1226 sizeOK = ctx->Driver.TestProxyTexImage(ctx, GL_PROXY_TEXTURE_1D,
1227 level, internalFormat,
1228 format, type,
1229 width, 1, 1, border);
1230 }
1231 else {
1232 _mesa_error( ctx, GL_INVALID_ENUM, "glTexImage1D(target)" );
1233 return GL_TRUE;
1234 }
1235 }
1236 else if (dimensions == 2) {
1237 if (target == GL_PROXY_TEXTURE_2D || target == GL_TEXTURE_2D) {
1238 sizeOK = ctx->Driver.TestProxyTexImage(ctx, GL_PROXY_TEXTURE_2D,
1239 level, internalFormat,
1240 format, type,
1241 width, height, 1, border);
1242 }
1243 else if (target == GL_PROXY_TEXTURE_CUBE_MAP_ARB ||
1244 (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
1245 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB)) {
1246 if (!ctx->Extensions.ARB_texture_cube_map) {
1247 _mesa_error(ctx, GL_INVALID_ENUM, "glTexImage2D(target)");
1248 return GL_TRUE;
1249 }
1250 sizeOK = (width == height) &&
1251 ctx->Driver.TestProxyTexImage(ctx, GL_PROXY_TEXTURE_CUBE_MAP_ARB,
1252 level, internalFormat, format, type,
1253 width, height, 1, border);
1254 }
1255 else if (target == GL_PROXY_TEXTURE_RECTANGLE_NV ||
1256 target == GL_TEXTURE_RECTANGLE_NV) {
1257 if (!ctx->Extensions.NV_texture_rectangle) {
1258 _mesa_error(ctx, GL_INVALID_ENUM, "glTexImage2D(target)");
1259 return GL_TRUE;
1260 }
1261 sizeOK = ctx->Driver.TestProxyTexImage(ctx,
1262 GL_PROXY_TEXTURE_RECTANGLE_NV,
1263 level, internalFormat,
1264 format, type,
1265 width, height, 1, border);
1266 }
1267 else {
1268 _mesa_error(ctx, GL_INVALID_ENUM, "glTexImage2D(target)");
1269 return GL_TRUE;
1270 }
1271 }
1272 else if (dimensions == 3) {
1273 if (target == GL_PROXY_TEXTURE_3D || target == GL_TEXTURE_3D) {
1274 sizeOK = ctx->Driver.TestProxyTexImage(ctx, GL_PROXY_TEXTURE_3D,
1275 level, internalFormat,
1276 format, type,
1277 width, height, depth, border);
1278 }
1279 else {
1280 _mesa_error( ctx, GL_INVALID_ENUM, "glTexImage3D(target)" );
1281 return GL_TRUE;
1282 }
1283 }
1284 else {
1285 _mesa_problem( ctx, "bad dims in texture_error_check" );
1286 return GL_TRUE;
1287 }
1288
1289 if (!sizeOK) {
1290 if (!isProxy) {
1291 _mesa_error(ctx, GL_INVALID_VALUE,
1292 "glTexImage%dD(level=%d, width=%d, height=%d, depth=%d)",
1293 dimensions, level, width, height, depth);
1294 }
1295 return GL_TRUE;
1296 }
1297
1298 /* Check internalFormat */
1299 if (_mesa_base_tex_format(ctx, internalFormat) < 0) {
1300 if (!isProxy) {
1301 _mesa_error(ctx, GL_INVALID_VALUE,
1302 "glTexImage%dD(internalFormat=0x%x)",
1303 dimensions, internalFormat);
1304 }
1305 return GL_TRUE;
1306 }
1307
1308 /* Check incoming image format and type */
1309 if (!_mesa_is_legal_format_and_type(ctx, format, type)) {
1310 /* Yes, generate GL_INVALID_OPERATION, not GL_INVALID_ENUM, if there
1311 * is a type/format mismatch. See 1.2 spec page 94, sec 3.6.4.
1312 */
1313 if (!isProxy) {
1314 _mesa_error(ctx, GL_INVALID_OPERATION,
1315 "glTexImage%dD(format or type)", dimensions);
1316 }
1317 return GL_TRUE;
1318 }
1319
1320 /* make sure internal format and format basically agree */
1321 if ((is_color_format(internalFormat) != is_color_format(format)) ||
1322 (is_index_format(internalFormat) != is_index_format(format)) ||
1323 (is_depth_format(internalFormat) != is_depth_format(format)) ||
1324 (is_ycbcr_format(internalFormat) != is_ycbcr_format(format))) {
1325 if (!isProxy)
1326 _mesa_error(ctx, GL_INVALID_OPERATION,
1327 "glTexImage(internalFormat/format)");
1328 return GL_TRUE;
1329 }
1330
1331 /* additional checks for ycbcr textures */
1332 if (internalFormat == GL_YCBCR_MESA) {
1333 ASSERT(ctx->Extensions.MESA_ycbcr_texture);
1334 if (type != GL_UNSIGNED_SHORT_8_8_MESA &&
1335 type != GL_UNSIGNED_SHORT_8_8_REV_MESA) {
1336 char message[100];
1337 _mesa_sprintf(message,
1338 "glTexImage%d(format/type YCBCR mismatch", dimensions);
1339 _mesa_error(ctx, GL_INVALID_ENUM, message);
1340 return GL_TRUE; /* error */
1341 }
1342 if (target != GL_TEXTURE_2D &&
1343 target != GL_PROXY_TEXTURE_2D &&
1344 target != GL_TEXTURE_RECTANGLE_NV &&
1345 target != GL_PROXY_TEXTURE_RECTANGLE_NV) {
1346 if (!isProxy)
1347 _mesa_error(ctx, GL_INVALID_ENUM, "glTexImage(target)");
1348 return GL_TRUE;
1349 }
1350 if (border != 0) {
1351 if (!isProxy) {
1352 char message[100];
1353 _mesa_sprintf(message,
1354 "glTexImage%d(format=GL_YCBCR_MESA and border=%d)",
1355 dimensions, border);
1356 _mesa_error(ctx, GL_INVALID_VALUE, message);
1357 }
1358 return GL_TRUE;
1359 }
1360 }
1361
1362 /* additional checks for depth textures */
1363 if (_mesa_base_tex_format(ctx, internalFormat) == GL_DEPTH_COMPONENT) {
1364 /* Only 1D and 2D textures supported */
1365 if (target != GL_TEXTURE_1D &&
1366 target != GL_PROXY_TEXTURE_1D &&
1367 target != GL_TEXTURE_2D &&
1368 target != GL_PROXY_TEXTURE_2D) {
1369 if (!isProxy)
1370 _mesa_error(ctx, GL_INVALID_ENUM,
1371 "glTexImage(target/internalFormat)");
1372 return GL_TRUE;
1373 }
1374 }
1375
1376 /* additional checks for compressed textures */
1377 if (is_compressed_format(ctx, internalFormat)) {
1378 if (target == GL_TEXTURE_2D || target == GL_PROXY_TEXTURE_2D) {
1379 /* OK */
1380 }
1381 else if (ctx->Extensions.ARB_texture_cube_map &&
1382 (target == GL_PROXY_TEXTURE_CUBE_MAP ||
1383 (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X &&
1384 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z))) {
1385 /* OK */
1386 }
1387 else {
1388 if (!isProxy) {
1389 _mesa_error(ctx, GL_INVALID_ENUM,
1390 "glTexImage%d(target)", dimensions);
1391 return GL_TRUE;
1392 }
1393 }
1394 if (border != 0) {
1395 if (!isProxy) {
1396 _mesa_error(ctx, GL_INVALID_OPERATION,
1397 "glTexImage%D(border!=0)", dimensions);
1398 }
1399 return GL_TRUE;
1400 }
1401 }
1402
1403 /* if we get here, the parameters are OK */
1404 return GL_FALSE;
1405 }
1406
1407
1408 /**
1409 * Test glTexSubImage[123]D() parameters for errors.
1410 *
1411 * \param ctx GL context.
1412 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1413 * \param target texture target given by the user.
1414 * \param level image level given by the user.
1415 * \param xoffset sub-image x offset given by the user.
1416 * \param yoffset sub-image y offset given by the user.
1417 * \param zoffset sub-image z offset given by the user.
1418 * \param format pixel data format given by the user.
1419 * \param type pixel data type given by the user.
1420 * \param width image width given by the user.
1421 * \param height image height given by the user.
1422 * \param depth image depth given by the user.
1423 *
1424 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
1425 *
1426 * Verifies each of the parameters against the constants specified in
1427 * __GLcontextRec::Const and the supported extensions, and according to the
1428 * OpenGL specification.
1429 */
1430 static GLboolean
1431 subtexture_error_check( GLcontext *ctx, GLuint dimensions,
1432 GLenum target, GLint level,
1433 GLint xoffset, GLint yoffset, GLint zoffset,
1434 GLint width, GLint height, GLint depth,
1435 GLenum format, GLenum type )
1436 {
1437 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1438 struct gl_texture_image *destTex;
1439
1440 /* Check target */
1441 if (dimensions == 1) {
1442 if (target != GL_TEXTURE_1D) {
1443 _mesa_error( ctx, GL_INVALID_ENUM, "glTexSubImage1D(target)" );
1444 return GL_TRUE;
1445 }
1446 }
1447 else if (dimensions == 2) {
1448 if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
1449 target <=GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB) {
1450 if (!ctx->Extensions.ARB_texture_cube_map) {
1451 _mesa_error( ctx, GL_INVALID_ENUM, "glTexSubImage2D(target)" );
1452 return GL_TRUE;
1453 }
1454 }
1455 else if (ctx->Extensions.NV_texture_rectangle &&
1456 target == GL_TEXTURE_RECTANGLE_NV) {
1457 if (!ctx->Extensions.NV_texture_rectangle) {
1458 _mesa_error( ctx, GL_INVALID_ENUM, "glTexSubImage2D(target)" );
1459 return GL_TRUE;
1460 }
1461 }
1462 else if (target != GL_TEXTURE_2D) {
1463 _mesa_error( ctx, GL_INVALID_ENUM, "glTexSubImage2D(target)" );
1464 return GL_TRUE;
1465 }
1466 }
1467 else if (dimensions == 3) {
1468 if (target != GL_TEXTURE_3D) {
1469 _mesa_error( ctx, GL_INVALID_ENUM, "glTexSubImage3D(target)" );
1470 return GL_TRUE;
1471 }
1472 }
1473 else {
1474 _mesa_problem( ctx, "invalid dims in texture_error_check" );
1475 return GL_TRUE;
1476 }
1477
1478 /* Basic level check */
1479 if (level < 0 || level >= MAX_TEXTURE_LEVELS) {
1480 _mesa_error(ctx, GL_INVALID_ENUM, "glTexSubImage2D(level=%d)", level);
1481 return GL_TRUE;
1482 }
1483
1484 if (width < 0) {
1485 _mesa_error(ctx, GL_INVALID_VALUE,
1486 "glTexSubImage%dD(width=%d)", dimensions, width);
1487 return GL_TRUE;
1488 }
1489 if (height < 0 && dimensions > 1) {
1490 _mesa_error(ctx, GL_INVALID_VALUE,
1491 "glTexSubImage%dD(height=%d)", dimensions, height);
1492 return GL_TRUE;
1493 }
1494 if (depth < 0 && dimensions > 2) {
1495 _mesa_error(ctx, GL_INVALID_VALUE,
1496 "glTexSubImage%dD(depth=%d)", dimensions, depth);
1497 return GL_TRUE;
1498 }
1499
1500 destTex = _mesa_select_tex_image(ctx, texUnit, target, level);
1501
1502 if (!destTex) {
1503 /* undefined image level */
1504 _mesa_error(ctx, GL_INVALID_OPERATION, "glTexSubImage%dD", dimensions);
1505 return GL_TRUE;
1506 }
1507
1508 if (xoffset < -((GLint)destTex->Border)) {
1509 _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage%dD(xoffset)",
1510 dimensions);
1511 return GL_TRUE;
1512 }
1513 if (xoffset + width > (GLint) (destTex->Width + destTex->Border)) {
1514 _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage%dD(xoffset+width)",
1515 dimensions);
1516 return GL_TRUE;
1517 }
1518 if (dimensions > 1) {
1519 if (yoffset < -((GLint)destTex->Border)) {
1520 _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage%dD(yoffset)",
1521 dimensions);
1522 return GL_TRUE;
1523 }
1524 if (yoffset + height > (GLint) (destTex->Height + destTex->Border)) {
1525 _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage%dD(yoffset+height)",
1526 dimensions);
1527 return GL_TRUE;
1528 }
1529 }
1530 if (dimensions > 2) {
1531 if (zoffset < -((GLint)destTex->Border)) {
1532 _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage3D(zoffset)");
1533 return GL_TRUE;
1534 }
1535 if (zoffset + depth > (GLint) (destTex->Depth + destTex->Border)) {
1536 _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage3D(zoffset+depth)");
1537 return GL_TRUE;
1538 }
1539 }
1540
1541 if (!_mesa_is_legal_format_and_type(ctx, format, type)) {
1542 _mesa_error(ctx, GL_INVALID_ENUM,
1543 "glTexSubImage%dD(format or type)", dimensions);
1544 return GL_TRUE;
1545 }
1546
1547 if (destTex->IsCompressed) {
1548 const struct gl_texture_unit *texUnit;
1549 const struct gl_texture_image *texImage;
1550 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1551 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
1552
1553 if (target == GL_TEXTURE_2D || target == GL_PROXY_TEXTURE_2D) {
1554 /* OK */
1555 }
1556 else if (ctx->Extensions.ARB_texture_cube_map &&
1557 (target == GL_PROXY_TEXTURE_CUBE_MAP ||
1558 (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X &&
1559 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z))) {
1560 /* OK */
1561 }
1562 else {
1563 _mesa_error(ctx, GL_INVALID_ENUM,
1564 "glTexSubImage%D(target)", dimensions);
1565 return GL_TRUE;
1566 }
1567 /* offset must be multiple of 4 */
1568 if ((xoffset & 3) || (yoffset & 3)) {
1569 _mesa_error(ctx, GL_INVALID_OPERATION,
1570 "glTexSubImage%D(xoffset or yoffset)", dimensions);
1571 return GL_TRUE;
1572 }
1573 /* size must be multiple of 4 or equal to whole texture size */
1574 if ((width & 3) && (GLuint) width != texImage->Width) {
1575 _mesa_error(ctx, GL_INVALID_OPERATION,
1576 "glTexSubImage%D(width)", dimensions);
1577 return GL_TRUE;
1578 }
1579 if ((height & 3) && (GLuint) height != texImage->Height) {
1580 _mesa_error(ctx, GL_INVALID_OPERATION,
1581 "glTexSubImage%D(width)", dimensions);
1582 return GL_TRUE;
1583 }
1584 }
1585
1586 return GL_FALSE;
1587 }
1588
1589
1590 /**
1591 * Test glCopyTexImage[12]D() parameters for errors.
1592 *
1593 * \param ctx GL context.
1594 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1595 * \param target texture target given by the user.
1596 * \param level image level given by the user.
1597 * \param internalFormat internal format given by the user.
1598 * \param width image width given by the user.
1599 * \param height image height given by the user.
1600 * \param depth image depth given by the user.
1601 * \param border texture border.
1602 *
1603 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
1604 *
1605 * Verifies each of the parameters against the constants specified in
1606 * __GLcontextRec::Const and the supported extensions, and according to the
1607 * OpenGL specification.
1608 */
1609 static GLboolean
1610 copytexture_error_check( GLcontext *ctx, GLuint dimensions,
1611 GLenum target, GLint level, GLint internalFormat,
1612 GLint width, GLint height, GLint border )
1613 {
1614 GLenum format, type;
1615 GLboolean sizeOK;
1616
1617 /* Basic level check (more checking in ctx->Driver.TestProxyTexImage) */
1618 if (level < 0 || level >= MAX_TEXTURE_LEVELS) {
1619 _mesa_error(ctx, GL_INVALID_VALUE,
1620 "glCopyTexImage%dD(level=%d)", dimensions, level);
1621 return GL_TRUE;
1622 }
1623
1624 /* Check border */
1625 if (border < 0 || border > 1 ||
1626 ((target == GL_TEXTURE_RECTANGLE_NV ||
1627 target == GL_PROXY_TEXTURE_RECTANGLE_NV) && border != 0)) {
1628 return GL_TRUE;
1629 }
1630
1631 /* The format and type aren't really significant here, but we need to pass
1632 * something to TestProxyTexImage().
1633 */
1634 format = _mesa_base_tex_format(ctx, internalFormat);
1635 type = GL_FLOAT;
1636
1637 /* Check target and call ctx->Driver.TestProxyTexImage() to check the
1638 * level, width, height and depth.
1639 */
1640 if (dimensions == 1) {
1641 if (target == GL_TEXTURE_1D) {
1642 sizeOK = ctx->Driver.TestProxyTexImage(ctx, GL_PROXY_TEXTURE_1D,
1643 level, internalFormat,
1644 format, type,
1645 width, 1, 1, border);
1646 }
1647 else {
1648 _mesa_error( ctx, GL_INVALID_ENUM, "glCopyTexImage1D(target)" );
1649 return GL_TRUE;
1650 }
1651 }
1652 else if (dimensions == 2) {
1653 if (target == GL_TEXTURE_2D) {
1654 sizeOK = ctx->Driver.TestProxyTexImage(ctx, GL_PROXY_TEXTURE_2D,
1655 level, internalFormat,
1656 format, type,
1657 width, height, 1, border);
1658 }
1659 else if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
1660 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB) {
1661 if (!ctx->Extensions.ARB_texture_cube_map) {
1662 _mesa_error( ctx, GL_INVALID_ENUM, "glCopyTexImage2D(target)" );
1663 return GL_TRUE;
1664 }
1665 sizeOK = (width == height) &&
1666 ctx->Driver.TestProxyTexImage(ctx, GL_PROXY_TEXTURE_CUBE_MAP_ARB,
1667 level, internalFormat, format, type,
1668 width, height, 1, border);
1669 }
1670 else if (target == GL_TEXTURE_RECTANGLE_NV) {
1671 if (!ctx->Extensions.NV_texture_rectangle) {
1672 _mesa_error( ctx, GL_INVALID_ENUM, "glCopyTexImage2D(target)" );
1673 return GL_TRUE;
1674 }
1675 sizeOK = ctx->Driver.TestProxyTexImage(ctx,
1676 GL_PROXY_TEXTURE_RECTANGLE_NV,
1677 level, internalFormat,
1678 format, type,
1679 width, height, 1, border);
1680 }
1681 else {
1682 _mesa_error( ctx, GL_INVALID_ENUM, "glCopyTexImage2D(target)" );
1683 return GL_TRUE;
1684 }
1685 }
1686 else {
1687 _mesa_problem(ctx, "invalid dimensions in copytexture_error_check");
1688 return GL_TRUE;
1689 }
1690
1691 if (!sizeOK) {
1692 if (dimensions == 1) {
1693 _mesa_error(ctx, GL_INVALID_VALUE,
1694 "glCopyTexImage1D(width=%d)", width);
1695 }
1696 else {
1697 ASSERT(dimensions == 2);
1698 _mesa_error(ctx, GL_INVALID_VALUE,
1699 "glCopyTexImage2D(width=%d, height=%d)", width, height);
1700 }
1701 return GL_TRUE;
1702 }
1703
1704 if (_mesa_base_tex_format(ctx, internalFormat) < 0) {
1705 _mesa_error(ctx, GL_INVALID_VALUE,
1706 "glCopyTexImage%dD(internalFormat)", dimensions);
1707 return GL_TRUE;
1708 }
1709
1710 if (is_compressed_format(ctx, internalFormat)) {
1711 if (target != GL_TEXTURE_2D) {
1712 _mesa_error(ctx, GL_INVALID_ENUM,
1713 "glCopyTexImage%d(target)", dimensions);
1714 return GL_TRUE;
1715 }
1716 if (border != 0) {
1717 _mesa_error(ctx, GL_INVALID_OPERATION,
1718 "glCopyTexImage%D(border!=0)", dimensions);
1719 return GL_TRUE;
1720 }
1721 }
1722
1723 /* if we get here, the parameters are OK */
1724 return GL_FALSE;
1725 }
1726
1727
1728 /**
1729 * Test glCopyTexImage[12]D() parameters for errors.
1730 *
1731 * \param ctx GL context.
1732 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1733 * \param target texture target given by the user.
1734 * \param level image level given by the user.
1735 * \param xoffset sub-image x offset given by the user.
1736 * \param yoffset sub-image y offset given by the user.
1737 * \param zoffset sub-image z offset given by the user.
1738 * \param width image width given by the user.
1739 * \param height image height given by the user.
1740 *
1741 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
1742 *
1743 * Verifies each of the parameters against the constants specified in
1744 * __GLcontextRec::Const and the supported extensions, and according to the
1745 * OpenGL specification.
1746 */
1747 static GLboolean
1748 copytexsubimage_error_check( GLcontext *ctx, GLuint dimensions,
1749 GLenum target, GLint level,
1750 GLint xoffset, GLint yoffset, GLint zoffset,
1751 GLsizei width, GLsizei height )
1752 {
1753 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1754 struct gl_texture_image *teximage;
1755
1756 /* Check target */
1757 if (dimensions == 1) {
1758 if (target != GL_TEXTURE_1D) {
1759 _mesa_error( ctx, GL_INVALID_ENUM, "glCopyTexSubImage1D(target)" );
1760 return GL_TRUE;
1761 }
1762 }
1763 else if (dimensions == 2) {
1764 if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
1765 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB) {
1766 if (!ctx->Extensions.ARB_texture_cube_map) {
1767 _mesa_error( ctx, GL_INVALID_ENUM, "glCopyTexSubImage2D(target)" );
1768 return GL_TRUE;
1769 }
1770 }
1771 else if (target == GL_TEXTURE_RECTANGLE_NV) {
1772 if (!ctx->Extensions.NV_texture_rectangle) {
1773 _mesa_error( ctx, GL_INVALID_ENUM, "glCopyTexSubImage2D(target)" );
1774 return GL_TRUE;
1775 }
1776 }
1777 else if (target != GL_TEXTURE_2D) {
1778 _mesa_error( ctx, GL_INVALID_ENUM, "glCopyTexSubImage2D(target)" );
1779 return GL_TRUE;
1780 }
1781 }
1782 else if (dimensions == 3) {
1783 if (target != GL_TEXTURE_3D) {
1784 _mesa_error( ctx, GL_INVALID_ENUM, "glCopyTexSubImage3D(target)" );
1785 return GL_TRUE;
1786 }
1787 }
1788
1789 /* Check level */
1790 if (level < 0 || level >= MAX_TEXTURE_LEVELS) {
1791 _mesa_error(ctx, GL_INVALID_VALUE,
1792 "glCopyTexSubImage%dD(level=%d)", dimensions, level);
1793 return GL_TRUE;
1794 }
1795
1796 /* Check size */
1797 if (width < 0) {
1798 _mesa_error(ctx, GL_INVALID_VALUE,
1799 "glCopyTexSubImage%dD(width=%d)", dimensions, width);
1800 return GL_TRUE;
1801 }
1802 if (dimensions > 1 && height < 0) {
1803 _mesa_error(ctx, GL_INVALID_VALUE,
1804 "glCopyTexSubImage%dD(height=%d)", dimensions, height);
1805 return GL_TRUE;
1806 }
1807
1808 teximage = _mesa_select_tex_image(ctx, texUnit, target, level);
1809 if (!teximage) {
1810 _mesa_error(ctx, GL_INVALID_OPERATION,
1811 "glCopyTexSubImage%dD(undefined texture level: %d)",
1812 dimensions, level);
1813 return GL_TRUE;
1814 }
1815
1816 if (xoffset < -((GLint)teximage->Border)) {
1817 _mesa_error(ctx, GL_INVALID_VALUE,
1818 "glCopyTexSubImage%dD(xoffset=%d)", dimensions, xoffset);
1819 return GL_TRUE;
1820 }
1821 if (xoffset + width > (GLint) (teximage->Width + teximage->Border)) {
1822 _mesa_error(ctx, GL_INVALID_VALUE,
1823 "glCopyTexSubImage%dD(xoffset+width)", dimensions);
1824 return GL_TRUE;
1825 }
1826 if (dimensions > 1) {
1827 if (yoffset < -((GLint)teximage->Border)) {
1828 _mesa_error(ctx, GL_INVALID_VALUE,
1829 "glCopyTexSubImage%dD(yoffset=%d)", dimensions, yoffset);
1830 return GL_TRUE;
1831 }
1832 /* NOTE: we're adding the border here, not subtracting! */
1833 if (yoffset + height > (GLint) (teximage->Height + teximage->Border)) {
1834 _mesa_error(ctx, GL_INVALID_VALUE,
1835 "glCopyTexSubImage%dD(yoffset+height)", dimensions);
1836 return GL_TRUE;
1837 }
1838 }
1839
1840 if (dimensions > 2) {
1841 if (zoffset < -((GLint)teximage->Border)) {
1842 _mesa_error(ctx, GL_INVALID_VALUE,
1843 "glCopyTexSubImage%dD(zoffset)", dimensions);
1844 return GL_TRUE;
1845 }
1846 if (zoffset > (GLint) (teximage->Depth + teximage->Border)) {
1847 _mesa_error(ctx, GL_INVALID_VALUE,
1848 "glCopyTexSubImage%dD(zoffset+depth)", dimensions);
1849 return GL_TRUE;
1850 }
1851 }
1852
1853 if (teximage->IsCompressed) {
1854 if (target != GL_TEXTURE_2D) {
1855 _mesa_error(ctx, GL_INVALID_ENUM,
1856 "glCopyTexSubImage%d(target)", dimensions);
1857 return GL_TRUE;
1858 }
1859 /* offset must be multiple of 4 */
1860 if ((xoffset & 3) || (yoffset & 3)) {
1861 _mesa_error(ctx, GL_INVALID_VALUE,
1862 "glCopyTexSubImage%D(xoffset or yoffset)", dimensions);
1863 return GL_TRUE;
1864 }
1865 /* size must be multiple of 4 */
1866 if ((width & 3) != 0 && (GLuint) width != teximage->Width) {
1867 _mesa_error(ctx, GL_INVALID_VALUE,
1868 "glCopyTexSubImage%D(width)", dimensions);
1869 return GL_TRUE;
1870 }
1871 if ((height & 3) != 0 && (GLuint) height != teximage->Height) {
1872 _mesa_error(ctx, GL_INVALID_VALUE,
1873 "glCopyTexSubImage%D(height)", dimensions);
1874 return GL_TRUE;
1875 }
1876 }
1877
1878 if (teximage->IntFormat == GL_YCBCR_MESA) {
1879 _mesa_error(ctx, GL_INVALID_OPERATION, "glCopyTexSubImage2D");
1880 return GL_TRUE;
1881 }
1882
1883 /* if we get here, the parameters are OK */
1884 return GL_FALSE;
1885 }
1886
1887
1888 /**
1889 * Get texture image. Called by glGetTexImage.
1890 *
1891 * \param target texture target.
1892 * \param level image level.
1893 * \param format pixel data format for returned image.
1894 * \param type pixel data type for returned image.
1895 * \param pixels returned pixel data.
1896 */
1897 void GLAPIENTRY
1898 _mesa_GetTexImage( GLenum target, GLint level, GLenum format,
1899 GLenum type, GLvoid *pixels )
1900 {
1901 const struct gl_texture_unit *texUnit;
1902 const struct gl_texture_object *texObj;
1903 const struct gl_texture_image *texImage;
1904 GLint maxLevels = 0;
1905 GLuint dimensions;
1906 GET_CURRENT_CONTEXT(ctx);
1907 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
1908
1909 texUnit = &(ctx->Texture.Unit[ctx->Texture.CurrentUnit]);
1910 texObj = _mesa_select_tex_object(ctx, texUnit, target);
1911 if (!texObj || is_proxy_target(target)) {
1912 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexImage(target)");
1913 return;
1914 }
1915
1916 maxLevels = _mesa_max_texture_levels(ctx, target);
1917 ASSERT(maxLevels > 0); /* 0 indicates bad target, caught above */
1918
1919 if (level < 0 || level >= maxLevels) {
1920 _mesa_error( ctx, GL_INVALID_VALUE, "glGetTexImage(level)" );
1921 return;
1922 }
1923
1924 if (_mesa_sizeof_packed_type(type) <= 0) {
1925 _mesa_error( ctx, GL_INVALID_ENUM, "glGetTexImage(type)" );
1926 return;
1927 }
1928
1929 if (_mesa_components_in_format(format) <= 0 ||
1930 format == GL_STENCIL_INDEX) {
1931 _mesa_error( ctx, GL_INVALID_ENUM, "glGetTexImage(format)" );
1932 return;
1933 }
1934
1935 if (!ctx->Extensions.EXT_paletted_texture && is_index_format(format)) {
1936 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexImage(format)");
1937 }
1938
1939 if (!ctx->Extensions.SGIX_depth_texture && is_depth_format(format)) {
1940 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexImage(format)");
1941 }
1942
1943 if (!ctx->Extensions.MESA_ycbcr_texture && is_ycbcr_format(format)) {
1944 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexImage(format)");
1945 }
1946
1947 if (!pixels)
1948 return;
1949
1950 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
1951 if (!texImage) {
1952 /* invalid mipmap level, not an error */
1953 return;
1954 }
1955
1956 /* Make sure the requested image format is compatible with the
1957 * texture's format.
1958 */
1959 if (is_color_format(format)
1960 && !is_color_format(texImage->TexFormat->BaseFormat)) {
1961 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetTexImage(format mismatch)");
1962 return;
1963 }
1964 else if (is_index_format(format)
1965 && !is_index_format(texImage->TexFormat->BaseFormat)) {
1966 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetTexImage(format mismatch)");
1967 return;
1968 }
1969 else if (is_depth_format(format)
1970 && !is_depth_format(texImage->TexFormat->BaseFormat)) {
1971 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetTexImage(format mismatch)");
1972 return;
1973 }
1974 else if (is_ycbcr_format(format)
1975 && !is_ycbcr_format(texImage->TexFormat->BaseFormat)) {
1976 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetTexImage(format mismatch)");
1977 return;
1978 }
1979
1980 dimensions = (target == GL_TEXTURE_3D) ? 3 : 2;
1981
1982 /* XXX - someday the rest of this function should be moved into a
1983 * fallback routine called via ctx->Driver.GetTexImage()
1984 */
1985
1986 if (ctx->Pack.BufferObj->Name) {
1987 /* pack texture image into a PBO */
1988 GLubyte *buf;
1989 if (!_mesa_validate_pbo_access(dimensions, &ctx->Pack, texImage->Width,
1990 texImage->Height, texImage->Depth,
1991 format, type, pixels)) {
1992 _mesa_error(ctx, GL_INVALID_OPERATION,
1993 "glGetTexImage(invalid PBO access)");
1994 return;
1995 }
1996 buf = (GLubyte *) ctx->Driver.MapBuffer(ctx, GL_PIXEL_PACK_BUFFER_EXT,
1997 GL_WRITE_ONLY_ARB,
1998 ctx->Pack.BufferObj);
1999 if (!buf) {
2000 /* buffer is already mapped - that's an error */
2001 _mesa_error(ctx, GL_INVALID_OPERATION,"glGetTexImage(PBO is mapped)");
2002 return;
2003 }
2004 pixels = ADD_POINTERS(buf, pixels);
2005 }
2006 else if (!pixels) {
2007 /* not an error */
2008 return;
2009 }
2010
2011 /*
2012 * XXX Move this code into a new driver fall-back function
2013 */
2014 {
2015 const GLint width = texImage->Width;
2016 const GLint height = texImage->Height;
2017 const GLint depth = texImage->Depth;
2018 GLint img, row;
2019 for (img = 0; img < depth; img++) {
2020 for (row = 0; row < height; row++) {
2021 /* compute destination address in client memory */
2022 GLvoid *dest = _mesa_image_address( dimensions, &ctx->Pack, pixels,
2023 width, height, format, type,
2024 img, row, 0);
2025 assert(dest);
2026
2027 if (format == GL_COLOR_INDEX) {
2028 GLuint indexRow[MAX_WIDTH];
2029 GLint col;
2030 /* Can't use FetchTexel here because that returns RGBA */
2031 if (texImage->TexFormat->IndexBits == 8) {
2032 const GLubyte *src = (const GLubyte *) texImage->Data;
2033 for (col = 0; col < width; col++) {
2034 indexRow[col] = src[texImage->Width *
2035 (img * texImage->Height + row) + col];
2036 }
2037 }
2038 else if (texImage->TexFormat->IndexBits == 16) {
2039 const GLushort *src = (const GLushort *) texImage->Data;
2040 for (col = 0; col < width; col++) {
2041 indexRow[col] = src[texImage->Width *
2042 (img * texImage->Height + row) + col];
2043 }
2044 }
2045 else {
2046 _mesa_problem(ctx,
2047 "Color index problem in _mesa_GetTexImage");
2048 }
2049 _mesa_pack_index_span(ctx, width, type, dest,
2050 indexRow, &ctx->Pack,
2051 0 /* no image transfer */);
2052 }
2053 else if (format == GL_DEPTH_COMPONENT) {
2054 GLfloat depthRow[MAX_WIDTH];
2055 GLint col;
2056 for (col = 0; col < width; col++) {
2057 (*texImage->FetchTexelf)(texImage, col, row, img,
2058 depthRow + col);
2059 }
2060 _mesa_pack_depth_span(ctx, width, dest, type,
2061 depthRow, &ctx->Pack);
2062 }
2063 else if (format == GL_YCBCR_MESA) {
2064 /* No pixel transfer */
2065 const GLint rowstride = texImage->RowStride;
2066 MEMCPY(dest,
2067 (const GLushort *) texImage->Data + row * rowstride,
2068 width * sizeof(GLushort));
2069 /* check for byte swapping */
2070 if ((texImage->TexFormat->MesaFormat == MESA_FORMAT_YCBCR
2071 && type == GL_UNSIGNED_SHORT_8_8_REV_MESA) ||
2072 (texImage->TexFormat->MesaFormat == MESA_FORMAT_YCBCR_REV
2073 && type == GL_UNSIGNED_SHORT_8_8_MESA)) {
2074 if (!ctx->Pack.SwapBytes)
2075 _mesa_swap2((GLushort *) dest, width);
2076 }
2077 else if (ctx->Pack.SwapBytes) {
2078 _mesa_swap2((GLushort *) dest, width);
2079 }
2080 }
2081 else {
2082 /* general case: convert row to RGBA format */
2083 GLfloat rgba[MAX_WIDTH][4];
2084 GLint col;
2085 for (col = 0; col < width; col++) {
2086 (*texImage->FetchTexelf)(texImage, col, row, img, rgba[col]);
2087 }
2088 _mesa_pack_rgba_span_float(ctx, width,
2089 (const GLfloat (*)[4]) rgba,
2090 format, type, dest, &ctx->Pack,
2091 0 /* no image transfer */);
2092 } /* format */
2093 } /* row */
2094 } /* img */
2095 }
2096
2097 if (ctx->Pack.BufferObj->Name) {
2098 ctx->Driver.UnmapBuffer(ctx, GL_PIXEL_PACK_BUFFER_EXT,
2099 ctx->Pack.BufferObj);
2100 }
2101 }
2102
2103
2104
2105 /*
2106 * Called from the API. Note that width includes the border.
2107 */
2108 void GLAPIENTRY
2109 _mesa_TexImage1D( GLenum target, GLint level, GLint internalFormat,
2110 GLsizei width, GLint border, GLenum format,
2111 GLenum type, const GLvoid *pixels )
2112 {
2113 GLsizei postConvWidth = width;
2114 GET_CURRENT_CONTEXT(ctx);
2115 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2116
2117 if (is_color_format(internalFormat)) {
2118 _mesa_adjust_image_for_convolution(ctx, 1, &postConvWidth, NULL);
2119 }
2120
2121 if (target == GL_TEXTURE_1D) {
2122 struct gl_texture_unit *texUnit;
2123 struct gl_texture_object *texObj;
2124 struct gl_texture_image *texImage;
2125
2126 if (texture_error_check(ctx, target, level, internalFormat,
2127 format, type, 1, postConvWidth, 1, 1, border)) {
2128 return; /* error was recorded */
2129 }
2130
2131 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2132 texObj = _mesa_select_tex_object(ctx, texUnit, target);
2133 texImage = _mesa_get_tex_image(ctx, texUnit, target, level);
2134
2135 if (!texImage) {
2136 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage1D");
2137 return;
2138 }
2139 else if (texImage->Data && !texImage->IsClientData) {
2140 /* free the old texture data */
2141 MESA_PBUFFER_FREE(texImage->Data);
2142 }
2143 texImage->Data = NULL;
2144 clear_teximage_fields(texImage); /* not really needed, but helpful */
2145 _mesa_init_teximage_fields(ctx, target, texImage,
2146 postConvWidth, 1, 1,
2147 border, internalFormat);
2148
2149 if (ctx->NewState & _IMAGE_NEW_TRANSFER_STATE)
2150 _mesa_update_state(ctx);
2151
2152 ASSERT(ctx->Driver.TexImage1D);
2153
2154 /* Give the texture to the driver! <pixels> may be null! */
2155 (*ctx->Driver.TexImage1D)(ctx, target, level, internalFormat,
2156 width, border, format, type, pixels,
2157 &ctx->Unpack, texObj, texImage);
2158
2159 ASSERT(texImage->TexFormat);
2160
2161 /* If driver didn't explicitly set this, use the defaults */
2162 if (!texImage->FetchTexelc)
2163 texImage->FetchTexelc = texImage->TexFormat->FetchTexel1D;
2164 if (!texImage->FetchTexelf)
2165 texImage->FetchTexelf = texImage->TexFormat->FetchTexel1Df;
2166 ASSERT(texImage->FetchTexelc);
2167 ASSERT(texImage->FetchTexelf);
2168
2169 /* state update */
2170 texObj->Complete = GL_FALSE;
2171 ctx->NewState |= _NEW_TEXTURE;
2172 }
2173 else if (target == GL_PROXY_TEXTURE_1D) {
2174 /* Proxy texture: check for errors and update proxy state */
2175 struct gl_texture_image *texImage;
2176 texImage = _mesa_get_proxy_tex_image(ctx, target, level);
2177 if (texture_error_check(ctx, target, level, internalFormat,
2178 format, type, 1, postConvWidth, 1, 1, border)) {
2179 /* when error, clear all proxy texture image parameters */
2180 if (texImage)
2181 clear_teximage_fields(texImage);
2182 }
2183 else {
2184 /* no error, set the tex image parameters */
2185 ASSERT(texImage);
2186 _mesa_init_teximage_fields(ctx, target, texImage,
2187 postConvWidth, 1, 1,
2188 border, internalFormat);
2189 texImage->TexFormat = (*ctx->Driver.ChooseTextureFormat)(ctx,
2190 internalFormat, format, type);
2191 }
2192 }
2193 else {
2194 _mesa_error( ctx, GL_INVALID_ENUM, "glTexImage1D(target)" );
2195 return;
2196 }
2197 }
2198
2199
2200 void GLAPIENTRY
2201 _mesa_TexImage2D( GLenum target, GLint level, GLint internalFormat,
2202 GLsizei width, GLsizei height, GLint border,
2203 GLenum format, GLenum type,
2204 const GLvoid *pixels )
2205 {
2206 GLsizei postConvWidth = width, postConvHeight = height;
2207 GET_CURRENT_CONTEXT(ctx);
2208 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2209
2210 if (is_color_format(internalFormat)) {
2211 _mesa_adjust_image_for_convolution(ctx, 2, &postConvWidth,
2212 &postConvHeight);
2213 }
2214
2215 if (target == GL_TEXTURE_2D ||
2216 (ctx->Extensions.ARB_texture_cube_map &&
2217 target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
2218 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB) ||
2219 (ctx->Extensions.NV_texture_rectangle &&
2220 target == GL_TEXTURE_RECTANGLE_NV)) {
2221 /* non-proxy target */
2222 struct gl_texture_unit *texUnit;
2223 struct gl_texture_object *texObj;
2224 struct gl_texture_image *texImage;
2225
2226 if (texture_error_check(ctx, target, level, internalFormat,
2227 format, type, 2, postConvWidth, postConvHeight,
2228 1, border)) {
2229 return; /* error was recorded */
2230 }
2231
2232 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2233 texObj = _mesa_select_tex_object(ctx, texUnit, target);
2234 texImage = _mesa_get_tex_image(ctx, texUnit, target, level);
2235 if (!texImage) {
2236 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage2D");
2237 return;
2238 }
2239 else if (texImage->Data && !texImage->IsClientData) {
2240 /* free the old texture data */
2241 MESA_PBUFFER_FREE(texImage->Data);
2242 }
2243 texImage->Data = NULL;
2244 clear_teximage_fields(texImage); /* not really needed, but helpful */
2245 _mesa_init_teximage_fields(ctx, target, texImage,
2246 postConvWidth, postConvHeight, 1,
2247 border, internalFormat);
2248
2249 if (ctx->NewState & _IMAGE_NEW_TRANSFER_STATE)
2250 _mesa_update_state(ctx);
2251
2252 ASSERT(ctx->Driver.TexImage2D);
2253
2254 /* Give the texture to the driver! <pixels> may be null! */
2255 (*ctx->Driver.TexImage2D)(ctx, target, level, internalFormat,
2256 width, height, border, format, type, pixels,
2257 &ctx->Unpack, texObj, texImage);
2258
2259 ASSERT(texImage->TexFormat);
2260
2261 /* If driver didn't explicitly set these, use the defaults */
2262 if (!texImage->FetchTexelc)
2263 texImage->FetchTexelc = texImage->TexFormat->FetchTexel2D;
2264 if (!texImage->FetchTexelf)
2265 texImage->FetchTexelf = texImage->TexFormat->FetchTexel2Df;
2266 ASSERT(texImage->FetchTexelc);
2267 ASSERT(texImage->FetchTexelf);
2268
2269 /* state update */
2270 texObj->Complete = GL_FALSE;
2271 ctx->NewState |= _NEW_TEXTURE;
2272 }
2273 else if (target == GL_PROXY_TEXTURE_2D ||
2274 (target == GL_PROXY_TEXTURE_CUBE_MAP_ARB &&
2275 ctx->Extensions.ARB_texture_cube_map) ||
2276 (target == GL_PROXY_TEXTURE_RECTANGLE_NV &&
2277 ctx->Extensions.NV_texture_rectangle)) {
2278 /* Proxy texture: check for errors and update proxy state */
2279 struct gl_texture_image *texImage;
2280 texImage = _mesa_get_proxy_tex_image(ctx, target, level);
2281 if (texture_error_check(ctx, target, level, internalFormat,
2282 format, type, 2, postConvWidth, postConvHeight,
2283 1, border)) {
2284 /* when error, clear all proxy texture image parameters */
2285 if (texImage)
2286 clear_teximage_fields(ctx->Texture.Proxy2D->Image[0][level]);
2287 }
2288 else {
2289 /* no error, set the tex image parameters */
2290 _mesa_init_teximage_fields(ctx, target, texImage,
2291 postConvWidth, postConvHeight, 1,
2292 border, internalFormat);
2293 texImage->TexFormat = (*ctx->Driver.ChooseTextureFormat)(ctx,
2294 internalFormat, format, type);
2295 }
2296 }
2297 else {
2298 _mesa_error( ctx, GL_INVALID_ENUM, "glTexImage2D(target)" );
2299 return;
2300 }
2301 }
2302
2303
2304 /*
2305 * Called by the API or display list executor.
2306 * Note that width and height include the border.
2307 */
2308 void GLAPIENTRY
2309 _mesa_TexImage3D( GLenum target, GLint level, GLint internalFormat,
2310 GLsizei width, GLsizei height, GLsizei depth,
2311 GLint border, GLenum format, GLenum type,
2312 const GLvoid *pixels )
2313 {
2314 GET_CURRENT_CONTEXT(ctx);
2315 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2316
2317 if (target == GL_TEXTURE_3D) {
2318 struct gl_texture_unit *texUnit;
2319 struct gl_texture_object *texObj;
2320 struct gl_texture_image *texImage;
2321
2322 if (texture_error_check(ctx, target, level, (GLint) internalFormat,
2323 format, type, 3, width, height, depth, border)) {
2324 return; /* error was recorded */
2325 }
2326
2327 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2328 texObj = _mesa_select_tex_object(ctx, texUnit, target);
2329 texImage = _mesa_get_tex_image(ctx, texUnit, target, level);
2330 if (!texImage) {
2331 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage3D");
2332 return;
2333 }
2334 else if (texImage->Data && !texImage->IsClientData) {
2335 MESA_PBUFFER_FREE(texImage->Data);
2336 }
2337 texImage->Data = NULL;
2338 clear_teximage_fields(texImage); /* not really needed, but helpful */
2339 _mesa_init_teximage_fields(ctx, target, texImage,
2340 width, height, depth,
2341 border, internalFormat);
2342
2343 if (ctx->NewState & _IMAGE_NEW_TRANSFER_STATE)
2344 _mesa_update_state(ctx);
2345
2346 ASSERT(ctx->Driver.TexImage3D);
2347
2348 /* Give the texture to the driver! <pixels> may be null! */
2349 (*ctx->Driver.TexImage3D)(ctx, target, level, internalFormat,
2350 width, height, depth, border, format, type,
2351 pixels, &ctx->Unpack, texObj, texImage);
2352
2353 ASSERT(texImage->TexFormat);
2354
2355 /* If driver didn't explicitly set these, use the defaults */
2356 if (!texImage->FetchTexelc)
2357 texImage->FetchTexelc = texImage->TexFormat->FetchTexel3D;
2358 if (!texImage->FetchTexelf)
2359 texImage->FetchTexelf = texImage->TexFormat->FetchTexel3Df;
2360 ASSERT(texImage->FetchTexelc);
2361 ASSERT(texImage->FetchTexelf);
2362
2363 /* state update */
2364 texObj->Complete = GL_FALSE;
2365 ctx->NewState |= _NEW_TEXTURE;
2366 }
2367 else if (target == GL_PROXY_TEXTURE_3D) {
2368 /* Proxy texture: check for errors and update proxy state */
2369 struct gl_texture_image *texImage;
2370 texImage = _mesa_get_proxy_tex_image(ctx, target, level);
2371 if (texture_error_check(ctx, target, level, internalFormat,
2372 format, type, 3, width, height, depth, border)) {
2373 /* when error, clear all proxy texture image parameters */
2374 if (texImage)
2375 clear_teximage_fields(texImage);
2376 }
2377 else {
2378 /* no error, set the tex image parameters */
2379 _mesa_init_teximage_fields(ctx, target, texImage, width, height, 1,
2380 border, internalFormat);
2381 texImage->TexFormat = (*ctx->Driver.ChooseTextureFormat)(ctx,
2382 internalFormat, format, type);
2383 }
2384 }
2385 else {
2386 _mesa_error( ctx, GL_INVALID_ENUM, "glTexImage3D(target)" );
2387 return;
2388 }
2389 }
2390
2391
2392 void GLAPIENTRY
2393 _mesa_TexImage3DEXT( GLenum target, GLint level, GLenum internalFormat,
2394 GLsizei width, GLsizei height, GLsizei depth,
2395 GLint border, GLenum format, GLenum type,
2396 const GLvoid *pixels )
2397 {
2398 _mesa_TexImage3D(target, level, (GLint) internalFormat, width, height,
2399 depth, border, format, type, pixels);
2400 }
2401
2402
2403
2404 void GLAPIENTRY
2405 _mesa_TexSubImage1D( GLenum target, GLint level,
2406 GLint xoffset, GLsizei width,
2407 GLenum format, GLenum type,
2408 const GLvoid *pixels )
2409 {
2410 GLsizei postConvWidth = width;
2411 struct gl_texture_unit *texUnit;
2412 struct gl_texture_object *texObj;
2413 struct gl_texture_image *texImage;
2414 GET_CURRENT_CONTEXT(ctx);
2415 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2416
2417 if (ctx->NewState & _IMAGE_NEW_TRANSFER_STATE)
2418 _mesa_update_state(ctx);
2419
2420 /* XXX should test internal format */
2421 if (is_color_format(format)) {
2422 _mesa_adjust_image_for_convolution(ctx, 1, &postConvWidth, NULL);
2423 }
2424
2425 if (subtexture_error_check(ctx, 1, target, level, xoffset, 0, 0,
2426 postConvWidth, 1, 1, format, type)) {
2427 return; /* error was detected */
2428 }
2429
2430 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2431 texObj = _mesa_select_tex_object(ctx, texUnit, target);
2432 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
2433 assert(texImage);
2434
2435 if (width == 0)
2436 return; /* no-op, not an error */
2437
2438 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2439 xoffset += texImage->Border;
2440
2441 ASSERT(ctx->Driver.TexSubImage1D);
2442 (*ctx->Driver.TexSubImage1D)(ctx, target, level, xoffset, width,
2443 format, type, pixels, &ctx->Unpack,
2444 texObj, texImage);
2445 ctx->NewState |= _NEW_TEXTURE;
2446 }
2447
2448
2449 void GLAPIENTRY
2450 _mesa_TexSubImage2D( GLenum target, GLint level,
2451 GLint xoffset, GLint yoffset,
2452 GLsizei width, GLsizei height,
2453 GLenum format, GLenum type,
2454 const GLvoid *pixels )
2455 {
2456 GLsizei postConvWidth = width, postConvHeight = height;
2457 struct gl_texture_unit *texUnit;
2458 struct gl_texture_object *texObj;
2459 struct gl_texture_image *texImage;
2460 GET_CURRENT_CONTEXT(ctx);
2461 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2462
2463 if (ctx->NewState & _IMAGE_NEW_TRANSFER_STATE)
2464 _mesa_update_state(ctx);
2465
2466 /* XXX should test internal format */
2467 if (is_color_format(format)) {
2468 _mesa_adjust_image_for_convolution(ctx, 2, &postConvWidth,
2469 &postConvHeight);
2470 }
2471
2472 if (subtexture_error_check(ctx, 2, target, level, xoffset, yoffset, 0,
2473 postConvWidth, postConvHeight, 1, format, type)) {
2474 return; /* error was detected */
2475 }
2476
2477 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2478 texObj = _mesa_select_tex_object(ctx, texUnit, target);
2479 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
2480 assert(texImage);
2481
2482 if (width == 0 || height == 0)
2483 return; /* no-op, not an error */
2484
2485 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2486 xoffset += texImage->Border;
2487 yoffset += texImage->Border;
2488
2489 ASSERT(ctx->Driver.TexSubImage2D);
2490 (*ctx->Driver.TexSubImage2D)(ctx, target, level, xoffset, yoffset,
2491 width, height, format, type, pixels,
2492 &ctx->Unpack, texObj, texImage);
2493 ctx->NewState |= _NEW_TEXTURE;
2494 }
2495
2496
2497
2498 void GLAPIENTRY
2499 _mesa_TexSubImage3D( GLenum target, GLint level,
2500 GLint xoffset, GLint yoffset, GLint zoffset,
2501 GLsizei width, GLsizei height, GLsizei depth,
2502 GLenum format, GLenum type,
2503 const GLvoid *pixels )
2504 {
2505 struct gl_texture_unit *texUnit;
2506 struct gl_texture_object *texObj;
2507 struct gl_texture_image *texImage;
2508 GET_CURRENT_CONTEXT(ctx);
2509 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2510
2511 if (ctx->NewState & _IMAGE_NEW_TRANSFER_STATE)
2512 _mesa_update_state(ctx);
2513
2514 if (subtexture_error_check(ctx, 3, target, level, xoffset, yoffset, zoffset,
2515 width, height, depth, format, type)) {
2516 return; /* error was detected */
2517 }
2518
2519 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2520 texObj = _mesa_select_tex_object(ctx, texUnit, target);
2521 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
2522 assert(texImage);
2523
2524 if (width == 0 || height == 0 || height == 0)
2525 return; /* no-op, not an error */
2526
2527 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2528 xoffset += texImage->Border;
2529 yoffset += texImage->Border;
2530 zoffset += texImage->Border;
2531
2532 ASSERT(ctx->Driver.TexSubImage3D);
2533 (*ctx->Driver.TexSubImage3D)(ctx, target, level,
2534 xoffset, yoffset, zoffset,
2535 width, height, depth,
2536 format, type, pixels,
2537 &ctx->Unpack, texObj, texImage );
2538 ctx->NewState |= _NEW_TEXTURE;
2539 }
2540
2541
2542
2543 void GLAPIENTRY
2544 _mesa_CopyTexImage1D( GLenum target, GLint level,
2545 GLenum internalFormat,
2546 GLint x, GLint y,
2547 GLsizei width, GLint border )
2548 {
2549 struct gl_texture_unit *texUnit;
2550 struct gl_texture_object *texObj;
2551 struct gl_texture_image *texImage;
2552 GLsizei postConvWidth = width;
2553 GET_CURRENT_CONTEXT(ctx);
2554 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2555
2556 if (ctx->NewState & _IMAGE_NEW_TRANSFER_STATE)
2557 _mesa_update_state(ctx);
2558
2559 if (is_color_format(internalFormat)) {
2560 _mesa_adjust_image_for_convolution(ctx, 1, &postConvWidth, NULL);
2561 }
2562
2563 if (copytexture_error_check(ctx, 1, target, level, internalFormat,
2564 postConvWidth, 1, border))
2565 return;
2566
2567 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2568 texObj = _mesa_select_tex_object(ctx, texUnit, target);
2569 texImage = _mesa_get_tex_image(ctx, texUnit, target, level);
2570 if (!texImage) {
2571 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexImage1D");
2572 return;
2573 }
2574 else if (texImage->Data && !texImage->IsClientData) {
2575 /* free the old texture data */
2576 MESA_PBUFFER_FREE(texImage->Data);
2577 }
2578 texImage->Data = NULL;
2579
2580 clear_teximage_fields(texImage); /* not really needed, but helpful */
2581 _mesa_init_teximage_fields(ctx, target, texImage, postConvWidth, 1, 1,
2582 border, internalFormat);
2583
2584
2585 ASSERT(ctx->Driver.CopyTexImage1D);
2586 (*ctx->Driver.CopyTexImage1D)(ctx, target, level, internalFormat,
2587 x, y, width, border);
2588
2589 ASSERT(texImage->TexFormat);
2590
2591 /* If driver didn't explicitly set these, use the defaults */
2592 if (!texImage->FetchTexelc)
2593 texImage->FetchTexelc = texImage->TexFormat->FetchTexel1D;
2594 if (!texImage->FetchTexelf)
2595 texImage->FetchTexelf = texImage->TexFormat->FetchTexel1Df;
2596 ASSERT(texImage->FetchTexelc);
2597 ASSERT(texImage->FetchTexelf);
2598
2599 /* state update */
2600 texObj->Complete = GL_FALSE;
2601 ctx->NewState |= _NEW_TEXTURE;
2602 }
2603
2604
2605
2606 void GLAPIENTRY
2607 _mesa_CopyTexImage2D( GLenum target, GLint level, GLenum internalFormat,
2608 GLint x, GLint y, GLsizei width, GLsizei height,
2609 GLint border )
2610 {
2611 struct gl_texture_unit *texUnit;
2612 struct gl_texture_object *texObj;
2613 struct gl_texture_image *texImage;
2614 GLsizei postConvWidth = width, postConvHeight = height;
2615 GET_CURRENT_CONTEXT(ctx);
2616 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2617
2618 if (ctx->NewState & _IMAGE_NEW_TRANSFER_STATE)
2619 _mesa_update_state(ctx);
2620
2621 if (is_color_format(internalFormat)) {
2622 _mesa_adjust_image_for_convolution(ctx, 2, &postConvWidth,
2623 &postConvHeight);
2624 }
2625
2626 if (copytexture_error_check(ctx, 2, target, level, internalFormat,
2627 postConvWidth, postConvHeight, border))
2628 return;
2629
2630 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2631 texObj = _mesa_select_tex_object(ctx, texUnit, target);
2632 texImage = _mesa_get_tex_image(ctx, texUnit, target, level);
2633 if (!texImage) {
2634 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexImage2D");
2635 return;
2636 }
2637 else if (texImage->Data && !texImage->IsClientData) {
2638 /* free the old texture data */
2639 MESA_PBUFFER_FREE(texImage->Data);
2640 }
2641 texImage->Data = NULL;
2642
2643 clear_teximage_fields(texImage); /* not really needed, but helpful */
2644 _mesa_init_teximage_fields(ctx, target, texImage,
2645 postConvWidth, postConvHeight, 1,
2646 border, internalFormat);
2647
2648 ASSERT(ctx->Driver.CopyTexImage2D);
2649 (*ctx->Driver.CopyTexImage2D)(ctx, target, level, internalFormat,
2650 x, y, width, height, border);
2651
2652 ASSERT(texImage->TexFormat);
2653
2654 /* If driver didn't explicitly set these, use the defaults */
2655 if (!texImage->FetchTexelc)
2656 texImage->FetchTexelc = texImage->TexFormat->FetchTexel2D;
2657 if (!texImage->FetchTexelf)
2658 texImage->FetchTexelf = texImage->TexFormat->FetchTexel2Df;
2659 ASSERT(texImage->FetchTexelc);
2660 ASSERT(texImage->FetchTexelf);
2661
2662 /* state update */
2663 texObj->Complete = GL_FALSE;
2664 ctx->NewState |= _NEW_TEXTURE;
2665 }
2666
2667
2668
2669 void GLAPIENTRY
2670 _mesa_CopyTexSubImage1D( GLenum target, GLint level,
2671 GLint xoffset, GLint x, GLint y, GLsizei width )
2672 {
2673 struct gl_texture_unit *texUnit;
2674 struct gl_texture_image *texImage;
2675 GLsizei postConvWidth = width;
2676 GET_CURRENT_CONTEXT(ctx);
2677 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2678
2679 if (ctx->NewState & _IMAGE_NEW_TRANSFER_STATE)
2680 _mesa_update_state(ctx);
2681
2682 /* XXX should test internal format */
2683 _mesa_adjust_image_for_convolution(ctx, 1, &postConvWidth, NULL);
2684
2685 if (copytexsubimage_error_check(ctx, 1, target, level,
2686 xoffset, 0, 0, postConvWidth, 1))
2687 return;
2688
2689 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2690 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
2691 ASSERT(texImage);
2692
2693 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2694 xoffset += texImage->Border;
2695
2696 ASSERT(ctx->Driver.CopyTexSubImage1D);
2697 (*ctx->Driver.CopyTexSubImage1D)(ctx, target, level, xoffset, x, y, width);
2698 ctx->NewState |= _NEW_TEXTURE;
2699 }
2700
2701
2702
2703 void GLAPIENTRY
2704 _mesa_CopyTexSubImage2D( GLenum target, GLint level,
2705 GLint xoffset, GLint yoffset,
2706 GLint x, GLint y, GLsizei width, GLsizei height )
2707 {
2708 struct gl_texture_unit *texUnit;
2709 struct gl_texture_image *texImage;
2710 GLsizei postConvWidth = width, postConvHeight = height;
2711 GET_CURRENT_CONTEXT(ctx);
2712 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2713
2714 if (ctx->NewState & _IMAGE_NEW_TRANSFER_STATE)
2715 _mesa_update_state(ctx);
2716
2717 /* XXX should test internal format */
2718 _mesa_adjust_image_for_convolution(ctx, 2, &postConvWidth, &postConvHeight);
2719
2720 if (copytexsubimage_error_check(ctx, 2, target, level, xoffset, yoffset, 0,
2721 postConvWidth, postConvHeight))
2722 return;
2723
2724 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2725 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
2726 ASSERT(texImage);
2727
2728 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2729 xoffset += texImage->Border;
2730 yoffset += texImage->Border;
2731
2732 ASSERT(ctx->Driver.CopyTexSubImage2D);
2733 (*ctx->Driver.CopyTexSubImage2D)(ctx, target, level,
2734 xoffset, yoffset, x, y, width, height);
2735 ctx->NewState |= _NEW_TEXTURE;
2736 }
2737
2738
2739
2740 void GLAPIENTRY
2741 _mesa_CopyTexSubImage3D( GLenum target, GLint level,
2742 GLint xoffset, GLint yoffset, GLint zoffset,
2743 GLint x, GLint y, GLsizei width, GLsizei height )
2744 {
2745 struct gl_texture_unit *texUnit;
2746 struct gl_texture_image *texImage;
2747 GLsizei postConvWidth = width, postConvHeight = height;
2748 GET_CURRENT_CONTEXT(ctx);
2749 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2750
2751 if (ctx->NewState & _IMAGE_NEW_TRANSFER_STATE)
2752 _mesa_update_state(ctx);
2753
2754 /* XXX should test internal format */
2755 _mesa_adjust_image_for_convolution(ctx, 2, &postConvWidth, &postConvHeight);
2756
2757 if (copytexsubimage_error_check(ctx, 3, target, level, xoffset, yoffset,
2758 zoffset, postConvWidth, postConvHeight))
2759 return;
2760
2761 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2762 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
2763 ASSERT(texImage);
2764
2765 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2766 xoffset += texImage->Border;
2767 yoffset += texImage->Border;
2768 zoffset += texImage->Border;
2769
2770 ASSERT(ctx->Driver.CopyTexSubImage3D);
2771 (*ctx->Driver.CopyTexSubImage3D)(ctx, target, level,
2772 xoffset, yoffset, zoffset,
2773 x, y, width, height);
2774 ctx->NewState |= _NEW_TEXTURE;
2775 }
2776
2777
2778
2779
2780 /**********************************************************************/
2781 /****** Compressed Textures ******/
2782 /**********************************************************************/
2783
2784
2785 /**
2786 * Error checking for glCompressedTexImage[123]D().
2787 * \return error code or GL_NO_ERROR.
2788 */
2789 static GLenum
2790 compressed_texture_error_check(GLcontext *ctx, GLint dimensions,
2791 GLenum target, GLint level,
2792 GLenum internalFormat, GLsizei width,
2793 GLsizei height, GLsizei depth, GLint border,
2794 GLsizei imageSize)
2795 {
2796 GLint expectedSize, maxLevels = 0, maxTextureSize;
2797
2798 if (dimensions == 1) {
2799 /* 1D compressed textures not allowed */
2800 return GL_INVALID_ENUM;
2801 }
2802 else if (dimensions == 2) {
2803 if (target == GL_PROXY_TEXTURE_2D) {
2804 maxLevels = ctx->Const.MaxTextureLevels;
2805 }
2806 else if (target == GL_TEXTURE_2D) {
2807 maxLevels = ctx->Const.MaxTextureLevels;
2808 }
2809 else if (target == GL_PROXY_TEXTURE_CUBE_MAP_ARB) {
2810 if (!ctx->Extensions.ARB_texture_cube_map)
2811 return GL_INVALID_ENUM; /*target*/
2812 maxLevels = ctx->Const.MaxCubeTextureLevels;
2813 }
2814 else if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
2815 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB) {
2816 if (!ctx->Extensions.ARB_texture_cube_map)
2817 return GL_INVALID_ENUM; /*target*/
2818 maxLevels = ctx->Const.MaxCubeTextureLevels;
2819 }
2820 else {
2821 return GL_INVALID_ENUM; /*target*/
2822 }
2823 }
2824 else if (dimensions == 3) {
2825 /* 3D compressed textures not allowed */
2826 return GL_INVALID_ENUM;
2827 }
2828
2829 maxTextureSize = 1 << (maxLevels - 1);
2830
2831 if (!is_compressed_format(ctx, internalFormat))
2832 return GL_INVALID_ENUM;
2833
2834 if (_mesa_base_tex_format(ctx, internalFormat) < 0)
2835 return GL_INVALID_ENUM;
2836
2837 if (border != 0)
2838 return GL_INVALID_VALUE;
2839
2840 /*
2841 * XXX We should probably use the proxy texture error check function here.
2842 */
2843 if (width < 1 || width > maxTextureSize ||
2844 (!ctx->Extensions.ARB_texture_non_power_of_two && logbase2(width) < 0))
2845 return GL_INVALID_VALUE;
2846
2847 if ((height < 1 || height > maxTextureSize ||
2848 (!ctx->Extensions.ARB_texture_non_power_of_two && logbase2(height) < 0))
2849 && dimensions > 1)
2850 return GL_INVALID_VALUE;
2851
2852 if ((depth < 1 || depth > maxTextureSize ||
2853 (!ctx->Extensions.ARB_texture_non_power_of_two && logbase2(depth) < 0))
2854 && dimensions > 2)
2855 return GL_INVALID_VALUE;
2856
2857 /* For cube map, width must equal height */
2858 if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
2859 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB && width != height)
2860 return GL_INVALID_VALUE;
2861
2862 if (level < 0 || level >= maxLevels)
2863 return GL_INVALID_VALUE;
2864
2865 expectedSize = ctx->Driver.CompressedTextureSize(ctx, width, height, depth,
2866 internalFormat);
2867 if (expectedSize != imageSize)
2868 return GL_INVALID_VALUE;
2869
2870 return GL_NO_ERROR;
2871 }
2872
2873
2874 /**
2875 * Error checking for glCompressedTexSubImage[123]D().
2876 * \warning There are some bad assumptions here about the size of compressed
2877 * texture tiles (multiple of 4) used to test the validity of the
2878 * offset and size parameters.
2879 * \return error code or GL_NO_ERROR.
2880 */
2881 static GLenum
2882 compressed_subtexture_error_check(GLcontext *ctx, GLint dimensions,
2883 GLenum target, GLint level,
2884 GLint xoffset, GLint yoffset, GLint zoffset,
2885 GLsizei width, GLsizei height, GLsizei depth,
2886 GLenum format, GLsizei imageSize)
2887 {
2888 GLint expectedSize, maxLevels = 0, maxTextureSize;
2889 (void) zoffset;
2890
2891 if (dimensions == 1) {
2892 /* 1D compressed textures not allowed */
2893 return GL_INVALID_ENUM;
2894 }
2895 else if (dimensions == 2) {
2896 if (target == GL_PROXY_TEXTURE_2D) {
2897 maxLevels = ctx->Const.MaxTextureLevels;
2898 }
2899 else if (target == GL_TEXTURE_2D) {
2900 maxLevels = ctx->Const.MaxTextureLevels;
2901 }
2902 else if (target == GL_PROXY_TEXTURE_CUBE_MAP_ARB) {
2903 if (!ctx->Extensions.ARB_texture_cube_map)
2904 return GL_INVALID_ENUM; /*target*/
2905 maxLevels = ctx->Const.MaxCubeTextureLevels;
2906 }
2907 else if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
2908 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB) {
2909 if (!ctx->Extensions.ARB_texture_cube_map)
2910 return GL_INVALID_ENUM; /*target*/
2911 maxLevels = ctx->Const.MaxCubeTextureLevels;
2912 }
2913 else {
2914 return GL_INVALID_ENUM; /*target*/
2915 }
2916 }
2917 else if (dimensions == 3) {
2918 /* 3D compressed textures not allowed */
2919 return GL_INVALID_ENUM;
2920 }
2921
2922 maxTextureSize = 1 << (maxLevels - 1);
2923
2924 if (!is_compressed_format(ctx, format))
2925 return GL_INVALID_ENUM;
2926
2927 if (width < 1 || width > maxTextureSize)
2928 return GL_INVALID_VALUE;
2929
2930 if ((height < 1 || height > maxTextureSize)
2931 && dimensions > 1)
2932 return GL_INVALID_VALUE;
2933
2934 if (level < 0 || level >= maxLevels)
2935 return GL_INVALID_VALUE;
2936
2937 /* XXX these tests are specific to the compressed format.
2938 * this code should be generalized in some way.
2939 */
2940 if ((xoffset & 3) != 0 || (yoffset & 3) != 0)
2941 return GL_INVALID_VALUE;
2942
2943 if ((width & 3) != 0 && width != 2 && width != 1)
2944 return GL_INVALID_VALUE;
2945
2946 if ((height & 3) != 0 && height != 2 && height != 1)
2947 return GL_INVALID_VALUE;
2948
2949 expectedSize = ctx->Driver.CompressedTextureSize(ctx, width, height, depth,
2950 format);
2951 if (expectedSize != imageSize)
2952 return GL_INVALID_VALUE;
2953
2954 return GL_NO_ERROR;
2955 }
2956
2957
2958
2959 void GLAPIENTRY
2960 _mesa_CompressedTexImage1DARB(GLenum target, GLint level,
2961 GLenum internalFormat, GLsizei width,
2962 GLint border, GLsizei imageSize,
2963 const GLvoid *data)
2964 {
2965 GET_CURRENT_CONTEXT(ctx);
2966 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2967
2968 if (target == GL_TEXTURE_1D) {
2969 struct gl_texture_unit *texUnit;
2970 struct gl_texture_object *texObj;
2971 struct gl_texture_image *texImage;
2972 GLenum error = compressed_texture_error_check(ctx, 1, target, level,
2973 internalFormat, width, 1, 1, border, imageSize);
2974 if (error) {
2975 _mesa_error(ctx, error, "glCompressedTexImage1D");
2976 return;
2977 }
2978
2979 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2980 texObj = _mesa_select_tex_object(ctx, texUnit, target);
2981 texImage = _mesa_get_tex_image(ctx, texUnit, target, level);
2982 if (!texImage) {
2983 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCompressedTexImage1D");
2984 return;
2985 }
2986 else if (texImage->Data && !texImage->IsClientData) {
2987 MESA_PBUFFER_FREE(texImage->Data);
2988 }
2989 texImage->Data = NULL;
2990
2991 _mesa_init_teximage_fields(ctx, target, texImage, width, 1, 1,
2992 border, internalFormat);
2993
2994 ASSERT(ctx->Driver.CompressedTexImage1D);
2995 (*ctx->Driver.CompressedTexImage1D)(ctx, target, level,
2996 internalFormat, width, border,
2997 imageSize, data,
2998 texObj, texImage);
2999
3000 /* state update */
3001 texObj->Complete = GL_FALSE;
3002 ctx->NewState |= _NEW_TEXTURE;
3003 }
3004 else if (target == GL_PROXY_TEXTURE_1D) {
3005 /* Proxy texture: check for errors and update proxy state */
3006 GLenum error = compressed_texture_error_check(ctx, 1, target, level,
3007 internalFormat, width, 1, 1, border, imageSize);
3008 if (!error) {
3009 ASSERT(ctx->Driver.TestProxyTexImage);
3010 error = !(*ctx->Driver.TestProxyTexImage)(ctx, target, level,
3011 internalFormat, GL_NONE, GL_NONE,
3012 width, 1, 1, border);
3013 }
3014 if (error) {
3015 /* if error, clear all proxy texture image parameters */
3016 struct gl_texture_image *texImage;
3017 texImage = _mesa_get_proxy_tex_image(ctx, target, level);
3018 if (texImage)
3019 clear_teximage_fields(texImage);
3020 }
3021 else {
3022 /* store the teximage parameters */
3023 struct gl_texture_unit *texUnit;
3024 struct gl_texture_image *texImage;
3025 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
3026 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
3027 _mesa_init_teximage_fields(ctx, target, texImage, width, 1, 1,
3028 border, internalFormat);
3029 }
3030 }
3031 else {
3032 _mesa_error(ctx, GL_INVALID_ENUM, "glCompressedTexImage1D(target)");
3033 return;
3034 }
3035 }
3036
3037
3038 void GLAPIENTRY
3039 _mesa_CompressedTexImage2DARB(GLenum target, GLint level,
3040 GLenum internalFormat, GLsizei width,
3041 GLsizei height, GLint border, GLsizei imageSize,
3042 const GLvoid *data)
3043 {
3044 GET_CURRENT_CONTEXT(ctx);
3045 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3046
3047 if (target == GL_TEXTURE_2D ||
3048 (ctx->Extensions.ARB_texture_cube_map &&
3049 target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
3050 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB)) {
3051 struct gl_texture_unit *texUnit;
3052 struct gl_texture_object *texObj;
3053 struct gl_texture_image *texImage;
3054 GLenum error = compressed_texture_error_check(ctx, 2, target, level,
3055 internalFormat, width, height, 1, border, imageSize);
3056 if (error) {
3057 _mesa_error(ctx, error, "glCompressedTexImage2D");
3058 return;
3059 }
3060
3061 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
3062 texObj = _mesa_select_tex_object(ctx, texUnit, target);
3063 texImage = _mesa_get_tex_image(ctx, texUnit, target, level);
3064 if (!texImage) {
3065 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCompressedTexImage2D");
3066 return;
3067 }
3068 else if (texImage->Data && !texImage->IsClientData) {
3069 MESA_PBUFFER_FREE(texImage->Data);
3070 }
3071 texImage->Data = NULL;
3072
3073 _mesa_init_teximage_fields(ctx, target, texImage, width, height, 1,
3074 border, internalFormat);
3075
3076 ASSERT(ctx->Driver.CompressedTexImage2D);
3077 (*ctx->Driver.CompressedTexImage2D)(ctx, target, level,
3078 internalFormat, width, height,
3079 border, imageSize, data,
3080 texObj, texImage);
3081
3082 /* state update */
3083 texObj->Complete = GL_FALSE;
3084 ctx->NewState |= _NEW_TEXTURE;
3085 }
3086 else if (target == GL_PROXY_TEXTURE_2D ||
3087 (target == GL_PROXY_TEXTURE_CUBE_MAP_ARB &&
3088 ctx->Extensions.ARB_texture_cube_map)) {
3089 /* Proxy texture: check for errors and update proxy state */
3090 GLenum error = compressed_texture_error_check(ctx, 2, target, level,
3091 internalFormat, width, height, 1, border, imageSize);
3092 if (!error) {
3093 ASSERT(ctx->Driver.TestProxyTexImage);
3094 error = !(*ctx->Driver.TestProxyTexImage)(ctx, target, level,
3095 internalFormat, GL_NONE, GL_NONE,
3096 width, height, 1, border);
3097 }
3098 if (error) {
3099 /* if error, clear all proxy texture image parameters */
3100 struct gl_texture_image *texImage;
3101 texImage = _mesa_get_proxy_tex_image(ctx, target, level);
3102 if (texImage)
3103 clear_teximage_fields(texImage);
3104 }
3105 else {
3106 /* store the teximage parameters */
3107 struct gl_texture_unit *texUnit;
3108 struct gl_texture_image *texImage;
3109 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
3110 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
3111 _mesa_init_teximage_fields(ctx, target, texImage, width, height, 1,
3112 border, internalFormat);
3113 }
3114 }
3115 else {
3116 _mesa_error(ctx, GL_INVALID_ENUM, "glCompressedTexImage2D(target)");
3117 return;
3118 }
3119 }
3120
3121
3122 void GLAPIENTRY
3123 _mesa_CompressedTexImage3DARB(GLenum target, GLint level,
3124 GLenum internalFormat, GLsizei width,
3125 GLsizei height, GLsizei depth, GLint border,
3126 GLsizei imageSize, const GLvoid *data)
3127 {
3128 GET_CURRENT_CONTEXT(ctx);
3129 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3130
3131 if (target == GL_TEXTURE_3D) {
3132 struct gl_texture_unit *texUnit;
3133 struct gl_texture_object *texObj;
3134 struct gl_texture_image *texImage;
3135 GLenum error = compressed_texture_error_check(ctx, 3, target, level,
3136 internalFormat, width, height, depth, border, imageSize);
3137 if (error) {
3138 _mesa_error(ctx, error, "glCompressedTexImage3D");
3139 return;
3140 }
3141
3142 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
3143 texObj = _mesa_select_tex_object(ctx, texUnit, target);
3144 texImage = _mesa_get_tex_image(ctx, texUnit, target, level);
3145 if (!texImage) {
3146 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCompressedTexImage3D");
3147 return;
3148 }
3149 else if (texImage->Data && !texImage->IsClientData) {
3150 MESA_PBUFFER_FREE(texImage->Data);
3151 }
3152 texImage->Data = NULL;
3153
3154 _mesa_init_teximage_fields(ctx, target, texImage, width, height, depth,
3155 border, internalFormat);
3156
3157 ASSERT(ctx->Driver.CompressedTexImage3D);
3158 (*ctx->Driver.CompressedTexImage3D)(ctx, target, level,
3159 internalFormat,
3160 width, height, depth,
3161 border, imageSize, data,
3162 texObj, texImage);
3163
3164 /* state update */
3165 texObj->Complete = GL_FALSE;
3166 ctx->NewState |= _NEW_TEXTURE;
3167 }
3168 else if (target == GL_PROXY_TEXTURE_3D) {
3169 /* Proxy texture: check for errors and update proxy state */
3170 GLenum error = compressed_texture_error_check(ctx, 3, target, level,
3171 internalFormat, width, height, depth, border, imageSize);
3172 if (!error) {
3173 ASSERT(ctx->Driver.TestProxyTexImage);
3174 error = !(*ctx->Driver.TestProxyTexImage)(ctx, target, level,
3175 internalFormat, GL_NONE, GL_NONE,
3176 width, height, depth, border);
3177 }
3178 if (error) {
3179 /* if error, clear all proxy texture image parameters */
3180 struct gl_texture_image *texImage;
3181 texImage = _mesa_get_proxy_tex_image(ctx, target, level);
3182 if (texImage)
3183 clear_teximage_fields(texImage);
3184 }
3185 else {
3186 /* store the teximage parameters */
3187 struct gl_texture_unit *texUnit;
3188 struct gl_texture_image *texImage;
3189 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
3190 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
3191 _mesa_init_teximage_fields(ctx, target, texImage, width, height,
3192 depth, border, internalFormat);
3193 }
3194 }
3195 else {
3196 _mesa_error(ctx, GL_INVALID_ENUM, "glCompressedTexImage3D(target)");
3197 return;
3198 }
3199 }
3200
3201
3202 void GLAPIENTRY
3203 _mesa_CompressedTexSubImage1DARB(GLenum target, GLint level, GLint xoffset,
3204 GLsizei width, GLenum format,
3205 GLsizei imageSize, const GLvoid *data)
3206 {
3207 struct gl_texture_unit *texUnit;
3208 struct gl_texture_object *texObj;
3209 struct gl_texture_image *texImage;
3210 GLenum error;
3211 GET_CURRENT_CONTEXT(ctx);
3212 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3213
3214 error = compressed_subtexture_error_check(ctx, 1, target, level,
3215 xoffset, 0, 0, width, 1, 1, format, imageSize);
3216 if (error) {
3217 _mesa_error(ctx, error, "glCompressedTexSubImage1D");
3218 return;
3219 }
3220
3221 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
3222 texObj = _mesa_select_tex_object(ctx, texUnit, target);
3223 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
3224 assert(texImage);
3225
3226 if ((GLint) format != texImage->IntFormat) {
3227 _mesa_error(ctx, GL_INVALID_OPERATION,
3228 "glCompressedTexSubImage1D(format)");
3229 return;
3230 }
3231
3232 if ((width == 1 || width == 2) && (GLuint) width != texImage->Width) {
3233 _mesa_error(ctx, GL_INVALID_VALUE, "glCompressedTexSubImage1D(width)");
3234 return;
3235 }
3236
3237 if (width == 0)
3238 return; /* no-op, not an error */
3239
3240 if (ctx->Driver.CompressedTexSubImage1D) {
3241 (*ctx->Driver.CompressedTexSubImage1D)(ctx, target, level,
3242 xoffset, width,
3243 format, imageSize, data,
3244 texObj, texImage);
3245 }
3246 ctx->NewState |= _NEW_TEXTURE;
3247 }
3248
3249
3250 void GLAPIENTRY
3251 _mesa_CompressedTexSubImage2DARB(GLenum target, GLint level, GLint xoffset,
3252 GLint yoffset, GLsizei width, GLsizei height,
3253 GLenum format, GLsizei imageSize,
3254 const GLvoid *data)
3255 {
3256 struct gl_texture_unit *texUnit;
3257 struct gl_texture_object *texObj;
3258 struct gl_texture_image *texImage;
3259 GLenum error;
3260 GET_CURRENT_CONTEXT(ctx);
3261 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3262
3263 error = compressed_subtexture_error_check(ctx, 2, target, level,
3264 xoffset, yoffset, 0, width, height, 1, format, imageSize);
3265 if (error) {
3266 /* XXX proxy target? */
3267 _mesa_error(ctx, error, "glCompressedTexSubImage2D");
3268 return;
3269 }
3270
3271 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
3272 texObj = _mesa_select_tex_object(ctx, texUnit, target);
3273 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
3274 assert(texImage);
3275
3276 if ((GLint) format != texImage->IntFormat) {
3277 _mesa_error(ctx, GL_INVALID_OPERATION,
3278 "glCompressedTexSubImage2D(format)");
3279 return;
3280 }
3281
3282 if (((width == 1 || width == 2) && (GLuint) width != texImage->Width) ||
3283 ((height == 1 || height == 2) && (GLuint) height != texImage->Height)) {
3284 _mesa_error(ctx, GL_INVALID_VALUE, "glCompressedTexSubImage2D(size)");
3285 return;
3286 }
3287
3288 if (width == 0 || height == 0)
3289 return; /* no-op, not an error */
3290
3291 if (ctx->Driver.CompressedTexSubImage2D) {
3292 (*ctx->Driver.CompressedTexSubImage2D)(ctx, target, level,
3293 xoffset, yoffset, width, height,
3294 format, imageSize, data,
3295 texObj, texImage);
3296 }
3297 ctx->NewState |= _NEW_TEXTURE;
3298 }
3299
3300
3301 void GLAPIENTRY
3302 _mesa_CompressedTexSubImage3DARB(GLenum target, GLint level, GLint xoffset,
3303 GLint yoffset, GLint zoffset, GLsizei width,
3304 GLsizei height, GLsizei depth, GLenum format,
3305 GLsizei imageSize, const GLvoid *data)
3306 {
3307 struct gl_texture_unit *texUnit;
3308 struct gl_texture_object *texObj;
3309 struct gl_texture_image *texImage;
3310 GLenum error;
3311 GET_CURRENT_CONTEXT(ctx);
3312 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3313
3314 error = compressed_subtexture_error_check(ctx, 3, target, level,
3315 xoffset, yoffset, zoffset, width, height, depth, format, imageSize);
3316 if (error) {
3317 _mesa_error(ctx, error, "glCompressedTexSubImage2D");
3318 return;
3319 }
3320
3321 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
3322 texObj = _mesa_select_tex_object(ctx, texUnit, target);
3323 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
3324 assert(texImage);
3325
3326 if ((GLint) format != texImage->IntFormat) {
3327 _mesa_error(ctx, GL_INVALID_OPERATION,
3328 "glCompressedTexSubImage3D(format)");
3329 return;
3330 }
3331
3332 if (((width == 1 || width == 2) && (GLuint) width != texImage->Width) ||
3333 ((height == 1 || height == 2) && (GLuint) height != texImage->Height) ||
3334 ((depth == 1 || depth == 2) && (GLuint) depth != texImage->Depth)) {
3335 _mesa_error(ctx, GL_INVALID_VALUE, "glCompressedTexSubImage3D(size)");
3336 return;
3337 }
3338
3339 if (width == 0 || height == 0 || depth == 0)
3340 return; /* no-op, not an error */
3341
3342 if (ctx->Driver.CompressedTexSubImage3D) {
3343 (*ctx->Driver.CompressedTexSubImage3D)(ctx, target, level,
3344 xoffset, yoffset, zoffset,
3345 width, height, depth,
3346 format, imageSize, data,
3347 texObj, texImage);
3348 }
3349 ctx->NewState |= _NEW_TEXTURE;
3350 }
3351
3352
3353 void GLAPIENTRY
3354 _mesa_GetCompressedTexImageARB(GLenum target, GLint level, GLvoid *img)
3355 {
3356 const struct gl_texture_unit *texUnit;
3357 const struct gl_texture_object *texObj;
3358 struct gl_texture_image *texImage;
3359 GLint maxLevels;
3360 GET_CURRENT_CONTEXT(ctx);
3361 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3362
3363 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
3364 texObj = _mesa_select_tex_object(ctx, texUnit, target);
3365 if (!texObj) {
3366 _mesa_error(ctx, GL_INVALID_ENUM, "glGetCompressedTexImageARB");
3367 return;
3368 }
3369
3370 maxLevels = _mesa_max_texture_levels(ctx, target);
3371 ASSERT(maxLevels > 0); /* 0 indicates bad target, caught above */
3372
3373 if (level < 0 || level >= maxLevels) {
3374 _mesa_error(ctx, GL_INVALID_VALUE, "glGetCompressedTexImageARB(level)");
3375 return;
3376 }
3377
3378 if (is_proxy_target(target)) {
3379 _mesa_error(ctx, GL_INVALID_ENUM, "glGetCompressedTexImageARB(target)");
3380 return;
3381 }
3382
3383 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
3384 if (!texImage) {
3385 /* probably invalid mipmap level */
3386 _mesa_error(ctx, GL_INVALID_VALUE, "glGetCompressedTexImageARB(level)");
3387 return;
3388 }
3389
3390 if (!texImage->IsCompressed) {
3391 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetCompressedTexImageARB");
3392 return;
3393 }
3394
3395 /* XXX - someday the rest of this function should be moved into a
3396 * fallback routine called via ctx->Driver.GetCompressedTexImage()
3397 */
3398
3399 if (ctx->Pack.BufferObj->Name) {
3400 /* pack texture image into a PBO */
3401 GLubyte *buf;
3402 if ((const GLubyte *) img + texImage->CompressedSize >
3403 (const GLubyte *) ctx->Pack.BufferObj->Size) {
3404 _mesa_error(ctx, GL_INVALID_OPERATION,
3405 "glGetCompressedTexImage(invalid PBO access)");
3406 return;
3407 }
3408 buf = (GLubyte *) ctx->Driver.MapBuffer(ctx, GL_PIXEL_PACK_BUFFER_EXT,
3409 GL_WRITE_ONLY_ARB,
3410 ctx->Pack.BufferObj);
3411 if (!buf) {
3412 /* buffer is already mapped - that's an error */
3413 _mesa_error(ctx, GL_INVALID_OPERATION,
3414 "glGetCompressedTexImage(PBO is mapped)");
3415 return;
3416 }
3417 img = ADD_POINTERS(buf, img);
3418 }
3419 else if (!img) {
3420 /* not an error */
3421 return;
3422 }
3423
3424 /* just memcpy, no pixelstore or pixel transfer */
3425 MEMCPY(img, texImage->Data, texImage->CompressedSize);
3426
3427 if (ctx->Pack.BufferObj->Name) {
3428 ctx->Driver.UnmapBuffer(ctx, GL_PIXEL_PACK_BUFFER_EXT,
3429 ctx->Pack.BufferObj);
3430 }
3431 }