Added driver hooks for GetTexImage() and GetCompressedTexImage().
[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 GET_CURRENT_CONTEXT(ctx);
1906 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
1907
1908 texUnit = &(ctx->Texture.Unit[ctx->Texture.CurrentUnit]);
1909 texObj = _mesa_select_tex_object(ctx, texUnit, target);
1910 if (!texObj || is_proxy_target(target)) {
1911 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexImage(target)");
1912 return;
1913 }
1914
1915 maxLevels = _mesa_max_texture_levels(ctx, target);
1916 ASSERT(maxLevels > 0); /* 0 indicates bad target, caught above */
1917
1918 if (level < 0 || level >= maxLevels) {
1919 _mesa_error( ctx, GL_INVALID_VALUE, "glGetTexImage(level)" );
1920 return;
1921 }
1922
1923 if (_mesa_sizeof_packed_type(type) <= 0) {
1924 _mesa_error( ctx, GL_INVALID_ENUM, "glGetTexImage(type)" );
1925 return;
1926 }
1927
1928 if (_mesa_components_in_format(format) <= 0 ||
1929 format == GL_STENCIL_INDEX) {
1930 _mesa_error( ctx, GL_INVALID_ENUM, "glGetTexImage(format)" );
1931 return;
1932 }
1933
1934 if (!ctx->Extensions.EXT_paletted_texture && is_index_format(format)) {
1935 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexImage(format)");
1936 }
1937
1938 if (!ctx->Extensions.SGIX_depth_texture && is_depth_format(format)) {
1939 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexImage(format)");
1940 }
1941
1942 if (!ctx->Extensions.MESA_ycbcr_texture && is_ycbcr_format(format)) {
1943 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexImage(format)");
1944 }
1945
1946 if (!pixels)
1947 return;
1948
1949 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
1950 if (!texImage) {
1951 /* invalid mipmap level, not an error */
1952 return;
1953 }
1954
1955 /* Make sure the requested image format is compatible with the
1956 * texture's format. We let the colorformat-indexformat go through,
1957 * because the texelfetcher will dequantize to full rgba.
1958 */
1959 if (is_color_format(format)
1960 && !is_color_format(texImage->TexFormat->BaseFormat)
1961 && !is_index_format(texImage->TexFormat->BaseFormat)) {
1962 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetTexImage(format mismatch)");
1963 return;
1964 }
1965 else if (is_index_format(format)
1966 && !is_index_format(texImage->TexFormat->BaseFormat)) {
1967 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetTexImage(format mismatch)");
1968 return;
1969 }
1970 else if (is_depth_format(format)
1971 && !is_depth_format(texImage->TexFormat->BaseFormat)) {
1972 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetTexImage(format mismatch)");
1973 return;
1974 }
1975 else if (is_ycbcr_format(format)
1976 && !is_ycbcr_format(texImage->TexFormat->BaseFormat)) {
1977 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetTexImage(format mismatch)");
1978 return;
1979 }
1980
1981 /* typically, this will call _mesa_get_teximage() */
1982 ctx->Driver.GetTexImage(ctx, target, level, format, type, pixels,
1983 texObj, texImage);
1984 }
1985
1986
1987
1988 /*
1989 * Called from the API. Note that width includes the border.
1990 */
1991 void GLAPIENTRY
1992 _mesa_TexImage1D( GLenum target, GLint level, GLint internalFormat,
1993 GLsizei width, GLint border, GLenum format,
1994 GLenum type, const GLvoid *pixels )
1995 {
1996 GLsizei postConvWidth = width;
1997 GET_CURRENT_CONTEXT(ctx);
1998 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
1999
2000 if (is_color_format(internalFormat)) {
2001 _mesa_adjust_image_for_convolution(ctx, 1, &postConvWidth, NULL);
2002 }
2003
2004 if (target == GL_TEXTURE_1D) {
2005 struct gl_texture_unit *texUnit;
2006 struct gl_texture_object *texObj;
2007 struct gl_texture_image *texImage;
2008
2009 if (texture_error_check(ctx, target, level, internalFormat,
2010 format, type, 1, postConvWidth, 1, 1, border)) {
2011 return; /* error was recorded */
2012 }
2013
2014 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2015 texObj = _mesa_select_tex_object(ctx, texUnit, target);
2016 texImage = _mesa_get_tex_image(ctx, texUnit, target, level);
2017
2018 if (!texImage) {
2019 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage1D");
2020 return;
2021 }
2022 else if (texImage->Data && !texImage->IsClientData) {
2023 /* free the old texture data */
2024 MESA_PBUFFER_FREE(texImage->Data);
2025 }
2026 texImage->Data = NULL;
2027 clear_teximage_fields(texImage); /* not really needed, but helpful */
2028 _mesa_init_teximage_fields(ctx, target, texImage,
2029 postConvWidth, 1, 1,
2030 border, internalFormat);
2031
2032 if (ctx->NewState & _IMAGE_NEW_TRANSFER_STATE)
2033 _mesa_update_state(ctx);
2034
2035 ASSERT(ctx->Driver.TexImage1D);
2036
2037 /* Give the texture to the driver! <pixels> may be null! */
2038 (*ctx->Driver.TexImage1D)(ctx, target, level, internalFormat,
2039 width, border, format, type, pixels,
2040 &ctx->Unpack, texObj, texImage);
2041
2042 ASSERT(texImage->TexFormat);
2043
2044 /* If driver didn't explicitly set this, use the defaults */
2045 if (!texImage->FetchTexelc)
2046 texImage->FetchTexelc = texImage->TexFormat->FetchTexel1D;
2047 if (!texImage->FetchTexelf)
2048 texImage->FetchTexelf = texImage->TexFormat->FetchTexel1Df;
2049 ASSERT(texImage->FetchTexelc);
2050 ASSERT(texImage->FetchTexelf);
2051
2052 /* state update */
2053 texObj->Complete = GL_FALSE;
2054 ctx->NewState |= _NEW_TEXTURE;
2055 }
2056 else if (target == GL_PROXY_TEXTURE_1D) {
2057 /* Proxy texture: check for errors and update proxy state */
2058 struct gl_texture_image *texImage;
2059 texImage = _mesa_get_proxy_tex_image(ctx, target, level);
2060 if (texture_error_check(ctx, target, level, internalFormat,
2061 format, type, 1, postConvWidth, 1, 1, border)) {
2062 /* when error, clear all proxy texture image parameters */
2063 if (texImage)
2064 clear_teximage_fields(texImage);
2065 }
2066 else {
2067 /* no error, set the tex image parameters */
2068 ASSERT(texImage);
2069 _mesa_init_teximage_fields(ctx, target, texImage,
2070 postConvWidth, 1, 1,
2071 border, internalFormat);
2072 texImage->TexFormat = (*ctx->Driver.ChooseTextureFormat)(ctx,
2073 internalFormat, format, type);
2074 }
2075 }
2076 else {
2077 _mesa_error( ctx, GL_INVALID_ENUM, "glTexImage1D(target)" );
2078 return;
2079 }
2080 }
2081
2082
2083 void GLAPIENTRY
2084 _mesa_TexImage2D( GLenum target, GLint level, GLint internalFormat,
2085 GLsizei width, GLsizei height, GLint border,
2086 GLenum format, GLenum type,
2087 const GLvoid *pixels )
2088 {
2089 GLsizei postConvWidth = width, postConvHeight = height;
2090 GET_CURRENT_CONTEXT(ctx);
2091 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2092
2093 if (is_color_format(internalFormat)) {
2094 _mesa_adjust_image_for_convolution(ctx, 2, &postConvWidth,
2095 &postConvHeight);
2096 }
2097
2098 if (target == GL_TEXTURE_2D ||
2099 (ctx->Extensions.ARB_texture_cube_map &&
2100 target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
2101 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB) ||
2102 (ctx->Extensions.NV_texture_rectangle &&
2103 target == GL_TEXTURE_RECTANGLE_NV)) {
2104 /* non-proxy target */
2105 struct gl_texture_unit *texUnit;
2106 struct gl_texture_object *texObj;
2107 struct gl_texture_image *texImage;
2108
2109 if (texture_error_check(ctx, target, level, internalFormat,
2110 format, type, 2, postConvWidth, postConvHeight,
2111 1, border)) {
2112 return; /* error was recorded */
2113 }
2114
2115 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2116 texObj = _mesa_select_tex_object(ctx, texUnit, target);
2117 texImage = _mesa_get_tex_image(ctx, texUnit, target, level);
2118 if (!texImage) {
2119 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage2D");
2120 return;
2121 }
2122 else if (texImage->Data && !texImage->IsClientData) {
2123 /* free the old texture data */
2124 MESA_PBUFFER_FREE(texImage->Data);
2125 }
2126 texImage->Data = NULL;
2127 clear_teximage_fields(texImage); /* not really needed, but helpful */
2128 _mesa_init_teximage_fields(ctx, target, texImage,
2129 postConvWidth, postConvHeight, 1,
2130 border, internalFormat);
2131
2132 if (ctx->NewState & _IMAGE_NEW_TRANSFER_STATE)
2133 _mesa_update_state(ctx);
2134
2135 ASSERT(ctx->Driver.TexImage2D);
2136
2137 /* Give the texture to the driver! <pixels> may be null! */
2138 (*ctx->Driver.TexImage2D)(ctx, target, level, internalFormat,
2139 width, height, border, format, type, pixels,
2140 &ctx->Unpack, texObj, texImage);
2141
2142 ASSERT(texImage->TexFormat);
2143
2144 /* If driver didn't explicitly set these, use the defaults */
2145 if (!texImage->FetchTexelc)
2146 texImage->FetchTexelc = texImage->TexFormat->FetchTexel2D;
2147 if (!texImage->FetchTexelf)
2148 texImage->FetchTexelf = texImage->TexFormat->FetchTexel2Df;
2149 ASSERT(texImage->FetchTexelc);
2150 ASSERT(texImage->FetchTexelf);
2151
2152 /* state update */
2153 texObj->Complete = GL_FALSE;
2154 ctx->NewState |= _NEW_TEXTURE;
2155 }
2156 else if (target == GL_PROXY_TEXTURE_2D ||
2157 (target == GL_PROXY_TEXTURE_CUBE_MAP_ARB &&
2158 ctx->Extensions.ARB_texture_cube_map) ||
2159 (target == GL_PROXY_TEXTURE_RECTANGLE_NV &&
2160 ctx->Extensions.NV_texture_rectangle)) {
2161 /* Proxy texture: check for errors and update proxy state */
2162 struct gl_texture_image *texImage;
2163 texImage = _mesa_get_proxy_tex_image(ctx, target, level);
2164 if (texture_error_check(ctx, target, level, internalFormat,
2165 format, type, 2, postConvWidth, postConvHeight,
2166 1, border)) {
2167 /* when error, clear all proxy texture image parameters */
2168 if (texImage)
2169 clear_teximage_fields(ctx->Texture.Proxy2D->Image[0][level]);
2170 }
2171 else {
2172 /* no error, set the tex image parameters */
2173 _mesa_init_teximage_fields(ctx, target, texImage,
2174 postConvWidth, postConvHeight, 1,
2175 border, internalFormat);
2176 texImage->TexFormat = (*ctx->Driver.ChooseTextureFormat)(ctx,
2177 internalFormat, format, type);
2178 }
2179 }
2180 else {
2181 _mesa_error( ctx, GL_INVALID_ENUM, "glTexImage2D(target)" );
2182 return;
2183 }
2184 }
2185
2186
2187 /*
2188 * Called by the API or display list executor.
2189 * Note that width and height include the border.
2190 */
2191 void GLAPIENTRY
2192 _mesa_TexImage3D( GLenum target, GLint level, GLint internalFormat,
2193 GLsizei width, GLsizei height, GLsizei depth,
2194 GLint border, GLenum format, GLenum type,
2195 const GLvoid *pixels )
2196 {
2197 GET_CURRENT_CONTEXT(ctx);
2198 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2199
2200 if (target == GL_TEXTURE_3D) {
2201 struct gl_texture_unit *texUnit;
2202 struct gl_texture_object *texObj;
2203 struct gl_texture_image *texImage;
2204
2205 if (texture_error_check(ctx, target, level, (GLint) internalFormat,
2206 format, type, 3, width, height, depth, border)) {
2207 return; /* error was recorded */
2208 }
2209
2210 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2211 texObj = _mesa_select_tex_object(ctx, texUnit, target);
2212 texImage = _mesa_get_tex_image(ctx, texUnit, target, level);
2213 if (!texImage) {
2214 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage3D");
2215 return;
2216 }
2217 else if (texImage->Data && !texImage->IsClientData) {
2218 MESA_PBUFFER_FREE(texImage->Data);
2219 }
2220 texImage->Data = NULL;
2221 clear_teximage_fields(texImage); /* not really needed, but helpful */
2222 _mesa_init_teximage_fields(ctx, target, texImage,
2223 width, height, depth,
2224 border, internalFormat);
2225
2226 if (ctx->NewState & _IMAGE_NEW_TRANSFER_STATE)
2227 _mesa_update_state(ctx);
2228
2229 ASSERT(ctx->Driver.TexImage3D);
2230
2231 /* Give the texture to the driver! <pixels> may be null! */
2232 (*ctx->Driver.TexImage3D)(ctx, target, level, internalFormat,
2233 width, height, depth, border, format, type,
2234 pixels, &ctx->Unpack, texObj, texImage);
2235
2236 ASSERT(texImage->TexFormat);
2237
2238 /* If driver didn't explicitly set these, use the defaults */
2239 if (!texImage->FetchTexelc)
2240 texImage->FetchTexelc = texImage->TexFormat->FetchTexel3D;
2241 if (!texImage->FetchTexelf)
2242 texImage->FetchTexelf = texImage->TexFormat->FetchTexel3Df;
2243 ASSERT(texImage->FetchTexelc);
2244 ASSERT(texImage->FetchTexelf);
2245
2246 /* state update */
2247 texObj->Complete = GL_FALSE;
2248 ctx->NewState |= _NEW_TEXTURE;
2249 }
2250 else if (target == GL_PROXY_TEXTURE_3D) {
2251 /* Proxy texture: check for errors and update proxy state */
2252 struct gl_texture_image *texImage;
2253 texImage = _mesa_get_proxy_tex_image(ctx, target, level);
2254 if (texture_error_check(ctx, target, level, internalFormat,
2255 format, type, 3, width, height, depth, border)) {
2256 /* when error, clear all proxy texture image parameters */
2257 if (texImage)
2258 clear_teximage_fields(texImage);
2259 }
2260 else {
2261 /* no error, set the tex image parameters */
2262 _mesa_init_teximage_fields(ctx, target, texImage, width, height, 1,
2263 border, internalFormat);
2264 texImage->TexFormat = (*ctx->Driver.ChooseTextureFormat)(ctx,
2265 internalFormat, format, type);
2266 }
2267 }
2268 else {
2269 _mesa_error( ctx, GL_INVALID_ENUM, "glTexImage3D(target)" );
2270 return;
2271 }
2272 }
2273
2274
2275 void GLAPIENTRY
2276 _mesa_TexImage3DEXT( GLenum target, GLint level, GLenum internalFormat,
2277 GLsizei width, GLsizei height, GLsizei depth,
2278 GLint border, GLenum format, GLenum type,
2279 const GLvoid *pixels )
2280 {
2281 _mesa_TexImage3D(target, level, (GLint) internalFormat, width, height,
2282 depth, border, format, type, pixels);
2283 }
2284
2285
2286
2287 void GLAPIENTRY
2288 _mesa_TexSubImage1D( GLenum target, GLint level,
2289 GLint xoffset, GLsizei width,
2290 GLenum format, GLenum type,
2291 const GLvoid *pixels )
2292 {
2293 GLsizei postConvWidth = width;
2294 struct gl_texture_unit *texUnit;
2295 struct gl_texture_object *texObj;
2296 struct gl_texture_image *texImage;
2297 GET_CURRENT_CONTEXT(ctx);
2298 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2299
2300 if (ctx->NewState & _IMAGE_NEW_TRANSFER_STATE)
2301 _mesa_update_state(ctx);
2302
2303 /* XXX should test internal format */
2304 if (is_color_format(format)) {
2305 _mesa_adjust_image_for_convolution(ctx, 1, &postConvWidth, NULL);
2306 }
2307
2308 if (subtexture_error_check(ctx, 1, target, level, xoffset, 0, 0,
2309 postConvWidth, 1, 1, format, type)) {
2310 return; /* error was detected */
2311 }
2312
2313 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2314 texObj = _mesa_select_tex_object(ctx, texUnit, target);
2315 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
2316 assert(texImage);
2317
2318 if (width == 0)
2319 return; /* no-op, not an error */
2320
2321 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2322 xoffset += texImage->Border;
2323
2324 ASSERT(ctx->Driver.TexSubImage1D);
2325 (*ctx->Driver.TexSubImage1D)(ctx, target, level, xoffset, width,
2326 format, type, pixels, &ctx->Unpack,
2327 texObj, texImage);
2328 ctx->NewState |= _NEW_TEXTURE;
2329 }
2330
2331
2332 void GLAPIENTRY
2333 _mesa_TexSubImage2D( GLenum target, GLint level,
2334 GLint xoffset, GLint yoffset,
2335 GLsizei width, GLsizei height,
2336 GLenum format, GLenum type,
2337 const GLvoid *pixels )
2338 {
2339 GLsizei postConvWidth = width, postConvHeight = height;
2340 struct gl_texture_unit *texUnit;
2341 struct gl_texture_object *texObj;
2342 struct gl_texture_image *texImage;
2343 GET_CURRENT_CONTEXT(ctx);
2344 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2345
2346 if (ctx->NewState & _IMAGE_NEW_TRANSFER_STATE)
2347 _mesa_update_state(ctx);
2348
2349 /* XXX should test internal format */
2350 if (is_color_format(format)) {
2351 _mesa_adjust_image_for_convolution(ctx, 2, &postConvWidth,
2352 &postConvHeight);
2353 }
2354
2355 if (subtexture_error_check(ctx, 2, target, level, xoffset, yoffset, 0,
2356 postConvWidth, postConvHeight, 1, format, type)) {
2357 return; /* error was detected */
2358 }
2359
2360 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2361 texObj = _mesa_select_tex_object(ctx, texUnit, target);
2362 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
2363 assert(texImage);
2364
2365 if (width == 0 || height == 0)
2366 return; /* no-op, not an error */
2367
2368 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2369 xoffset += texImage->Border;
2370 yoffset += texImage->Border;
2371
2372 ASSERT(ctx->Driver.TexSubImage2D);
2373 (*ctx->Driver.TexSubImage2D)(ctx, target, level, xoffset, yoffset,
2374 width, height, format, type, pixels,
2375 &ctx->Unpack, texObj, texImage);
2376 ctx->NewState |= _NEW_TEXTURE;
2377 }
2378
2379
2380
2381 void GLAPIENTRY
2382 _mesa_TexSubImage3D( GLenum target, GLint level,
2383 GLint xoffset, GLint yoffset, GLint zoffset,
2384 GLsizei width, GLsizei height, GLsizei depth,
2385 GLenum format, GLenum type,
2386 const GLvoid *pixels )
2387 {
2388 struct gl_texture_unit *texUnit;
2389 struct gl_texture_object *texObj;
2390 struct gl_texture_image *texImage;
2391 GET_CURRENT_CONTEXT(ctx);
2392 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2393
2394 if (ctx->NewState & _IMAGE_NEW_TRANSFER_STATE)
2395 _mesa_update_state(ctx);
2396
2397 if (subtexture_error_check(ctx, 3, target, level, xoffset, yoffset, zoffset,
2398 width, height, depth, format, type)) {
2399 return; /* error was detected */
2400 }
2401
2402 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2403 texObj = _mesa_select_tex_object(ctx, texUnit, target);
2404 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
2405 assert(texImage);
2406
2407 if (width == 0 || height == 0 || height == 0)
2408 return; /* no-op, not an error */
2409
2410 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2411 xoffset += texImage->Border;
2412 yoffset += texImage->Border;
2413 zoffset += texImage->Border;
2414
2415 ASSERT(ctx->Driver.TexSubImage3D);
2416 (*ctx->Driver.TexSubImage3D)(ctx, target, level,
2417 xoffset, yoffset, zoffset,
2418 width, height, depth,
2419 format, type, pixels,
2420 &ctx->Unpack, texObj, texImage );
2421 ctx->NewState |= _NEW_TEXTURE;
2422 }
2423
2424
2425
2426 void GLAPIENTRY
2427 _mesa_CopyTexImage1D( GLenum target, GLint level,
2428 GLenum internalFormat,
2429 GLint x, GLint y,
2430 GLsizei width, GLint border )
2431 {
2432 struct gl_texture_unit *texUnit;
2433 struct gl_texture_object *texObj;
2434 struct gl_texture_image *texImage;
2435 GLsizei postConvWidth = width;
2436 GET_CURRENT_CONTEXT(ctx);
2437 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2438
2439 if (ctx->NewState & _IMAGE_NEW_TRANSFER_STATE)
2440 _mesa_update_state(ctx);
2441
2442 if (is_color_format(internalFormat)) {
2443 _mesa_adjust_image_for_convolution(ctx, 1, &postConvWidth, NULL);
2444 }
2445
2446 if (copytexture_error_check(ctx, 1, target, level, internalFormat,
2447 postConvWidth, 1, border))
2448 return;
2449
2450 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2451 texObj = _mesa_select_tex_object(ctx, texUnit, target);
2452 texImage = _mesa_get_tex_image(ctx, texUnit, target, level);
2453 if (!texImage) {
2454 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexImage1D");
2455 return;
2456 }
2457 else if (texImage->Data && !texImage->IsClientData) {
2458 /* free the old texture data */
2459 MESA_PBUFFER_FREE(texImage->Data);
2460 }
2461 texImage->Data = NULL;
2462
2463 clear_teximage_fields(texImage); /* not really needed, but helpful */
2464 _mesa_init_teximage_fields(ctx, target, texImage, postConvWidth, 1, 1,
2465 border, internalFormat);
2466
2467
2468 ASSERT(ctx->Driver.CopyTexImage1D);
2469 (*ctx->Driver.CopyTexImage1D)(ctx, target, level, internalFormat,
2470 x, y, width, border);
2471
2472 ASSERT(texImage->TexFormat);
2473
2474 /* If driver didn't explicitly set these, use the defaults */
2475 if (!texImage->FetchTexelc)
2476 texImage->FetchTexelc = texImage->TexFormat->FetchTexel1D;
2477 if (!texImage->FetchTexelf)
2478 texImage->FetchTexelf = texImage->TexFormat->FetchTexel1Df;
2479 ASSERT(texImage->FetchTexelc);
2480 ASSERT(texImage->FetchTexelf);
2481
2482 /* state update */
2483 texObj->Complete = GL_FALSE;
2484 ctx->NewState |= _NEW_TEXTURE;
2485 }
2486
2487
2488
2489 void GLAPIENTRY
2490 _mesa_CopyTexImage2D( GLenum target, GLint level, GLenum internalFormat,
2491 GLint x, GLint y, GLsizei width, GLsizei height,
2492 GLint border )
2493 {
2494 struct gl_texture_unit *texUnit;
2495 struct gl_texture_object *texObj;
2496 struct gl_texture_image *texImage;
2497 GLsizei postConvWidth = width, postConvHeight = height;
2498 GET_CURRENT_CONTEXT(ctx);
2499 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2500
2501 if (ctx->NewState & _IMAGE_NEW_TRANSFER_STATE)
2502 _mesa_update_state(ctx);
2503
2504 if (is_color_format(internalFormat)) {
2505 _mesa_adjust_image_for_convolution(ctx, 2, &postConvWidth,
2506 &postConvHeight);
2507 }
2508
2509 if (copytexture_error_check(ctx, 2, target, level, internalFormat,
2510 postConvWidth, postConvHeight, border))
2511 return;
2512
2513 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2514 texObj = _mesa_select_tex_object(ctx, texUnit, target);
2515 texImage = _mesa_get_tex_image(ctx, texUnit, target, level);
2516 if (!texImage) {
2517 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexImage2D");
2518 return;
2519 }
2520 else if (texImage->Data && !texImage->IsClientData) {
2521 /* free the old texture data */
2522 MESA_PBUFFER_FREE(texImage->Data);
2523 }
2524 texImage->Data = NULL;
2525
2526 clear_teximage_fields(texImage); /* not really needed, but helpful */
2527 _mesa_init_teximage_fields(ctx, target, texImage,
2528 postConvWidth, postConvHeight, 1,
2529 border, internalFormat);
2530
2531 ASSERT(ctx->Driver.CopyTexImage2D);
2532 (*ctx->Driver.CopyTexImage2D)(ctx, target, level, internalFormat,
2533 x, y, width, height, border);
2534
2535 ASSERT(texImage->TexFormat);
2536
2537 /* If driver didn't explicitly set these, use the defaults */
2538 if (!texImage->FetchTexelc)
2539 texImage->FetchTexelc = texImage->TexFormat->FetchTexel2D;
2540 if (!texImage->FetchTexelf)
2541 texImage->FetchTexelf = texImage->TexFormat->FetchTexel2Df;
2542 ASSERT(texImage->FetchTexelc);
2543 ASSERT(texImage->FetchTexelf);
2544
2545 /* state update */
2546 texObj->Complete = GL_FALSE;
2547 ctx->NewState |= _NEW_TEXTURE;
2548 }
2549
2550
2551
2552 void GLAPIENTRY
2553 _mesa_CopyTexSubImage1D( GLenum target, GLint level,
2554 GLint xoffset, GLint x, GLint y, GLsizei width )
2555 {
2556 struct gl_texture_unit *texUnit;
2557 struct gl_texture_image *texImage;
2558 GLsizei postConvWidth = width;
2559 GET_CURRENT_CONTEXT(ctx);
2560 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2561
2562 if (ctx->NewState & _IMAGE_NEW_TRANSFER_STATE)
2563 _mesa_update_state(ctx);
2564
2565 /* XXX should test internal format */
2566 _mesa_adjust_image_for_convolution(ctx, 1, &postConvWidth, NULL);
2567
2568 if (copytexsubimage_error_check(ctx, 1, target, level,
2569 xoffset, 0, 0, postConvWidth, 1))
2570 return;
2571
2572 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2573 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
2574 ASSERT(texImage);
2575
2576 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2577 xoffset += texImage->Border;
2578
2579 ASSERT(ctx->Driver.CopyTexSubImage1D);
2580 (*ctx->Driver.CopyTexSubImage1D)(ctx, target, level, xoffset, x, y, width);
2581 ctx->NewState |= _NEW_TEXTURE;
2582 }
2583
2584
2585
2586 void GLAPIENTRY
2587 _mesa_CopyTexSubImage2D( GLenum target, GLint level,
2588 GLint xoffset, GLint yoffset,
2589 GLint x, GLint y, GLsizei width, GLsizei height )
2590 {
2591 struct gl_texture_unit *texUnit;
2592 struct gl_texture_image *texImage;
2593 GLsizei postConvWidth = width, postConvHeight = height;
2594 GET_CURRENT_CONTEXT(ctx);
2595 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2596
2597 if (ctx->NewState & _IMAGE_NEW_TRANSFER_STATE)
2598 _mesa_update_state(ctx);
2599
2600 /* XXX should test internal format */
2601 _mesa_adjust_image_for_convolution(ctx, 2, &postConvWidth, &postConvHeight);
2602
2603 if (copytexsubimage_error_check(ctx, 2, target, level, xoffset, yoffset, 0,
2604 postConvWidth, postConvHeight))
2605 return;
2606
2607 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2608 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
2609 ASSERT(texImage);
2610
2611 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2612 xoffset += texImage->Border;
2613 yoffset += texImage->Border;
2614
2615 ASSERT(ctx->Driver.CopyTexSubImage2D);
2616 (*ctx->Driver.CopyTexSubImage2D)(ctx, target, level,
2617 xoffset, yoffset, x, y, width, height);
2618 ctx->NewState |= _NEW_TEXTURE;
2619 }
2620
2621
2622
2623 void GLAPIENTRY
2624 _mesa_CopyTexSubImage3D( GLenum target, GLint level,
2625 GLint xoffset, GLint yoffset, GLint zoffset,
2626 GLint x, GLint y, GLsizei width, GLsizei height )
2627 {
2628 struct gl_texture_unit *texUnit;
2629 struct gl_texture_image *texImage;
2630 GLsizei postConvWidth = width, postConvHeight = height;
2631 GET_CURRENT_CONTEXT(ctx);
2632 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2633
2634 if (ctx->NewState & _IMAGE_NEW_TRANSFER_STATE)
2635 _mesa_update_state(ctx);
2636
2637 /* XXX should test internal format */
2638 _mesa_adjust_image_for_convolution(ctx, 2, &postConvWidth, &postConvHeight);
2639
2640 if (copytexsubimage_error_check(ctx, 3, target, level, xoffset, yoffset,
2641 zoffset, postConvWidth, postConvHeight))
2642 return;
2643
2644 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2645 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
2646 ASSERT(texImage);
2647
2648 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2649 xoffset += texImage->Border;
2650 yoffset += texImage->Border;
2651 zoffset += texImage->Border;
2652
2653 ASSERT(ctx->Driver.CopyTexSubImage3D);
2654 (*ctx->Driver.CopyTexSubImage3D)(ctx, target, level,
2655 xoffset, yoffset, zoffset,
2656 x, y, width, height);
2657 ctx->NewState |= _NEW_TEXTURE;
2658 }
2659
2660
2661
2662
2663 /**********************************************************************/
2664 /****** Compressed Textures ******/
2665 /**********************************************************************/
2666
2667
2668 /**
2669 * Error checking for glCompressedTexImage[123]D().
2670 * \return error code or GL_NO_ERROR.
2671 */
2672 static GLenum
2673 compressed_texture_error_check(GLcontext *ctx, GLint dimensions,
2674 GLenum target, GLint level,
2675 GLenum internalFormat, GLsizei width,
2676 GLsizei height, GLsizei depth, GLint border,
2677 GLsizei imageSize)
2678 {
2679 GLint expectedSize, maxLevels = 0, maxTextureSize;
2680
2681 if (dimensions == 1) {
2682 /* 1D compressed textures not allowed */
2683 return GL_INVALID_ENUM;
2684 }
2685 else if (dimensions == 2) {
2686 if (target == GL_PROXY_TEXTURE_2D) {
2687 maxLevels = ctx->Const.MaxTextureLevels;
2688 }
2689 else if (target == GL_TEXTURE_2D) {
2690 maxLevels = ctx->Const.MaxTextureLevels;
2691 }
2692 else if (target == GL_PROXY_TEXTURE_CUBE_MAP_ARB) {
2693 if (!ctx->Extensions.ARB_texture_cube_map)
2694 return GL_INVALID_ENUM; /*target*/
2695 maxLevels = ctx->Const.MaxCubeTextureLevels;
2696 }
2697 else if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
2698 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB) {
2699 if (!ctx->Extensions.ARB_texture_cube_map)
2700 return GL_INVALID_ENUM; /*target*/
2701 maxLevels = ctx->Const.MaxCubeTextureLevels;
2702 }
2703 else {
2704 return GL_INVALID_ENUM; /*target*/
2705 }
2706 }
2707 else if (dimensions == 3) {
2708 /* 3D compressed textures not allowed */
2709 return GL_INVALID_ENUM;
2710 }
2711
2712 maxTextureSize = 1 << (maxLevels - 1);
2713
2714 if (!is_compressed_format(ctx, internalFormat))
2715 return GL_INVALID_ENUM;
2716
2717 if (_mesa_base_tex_format(ctx, internalFormat) < 0)
2718 return GL_INVALID_ENUM;
2719
2720 if (border != 0)
2721 return GL_INVALID_VALUE;
2722
2723 /*
2724 * XXX We should probably use the proxy texture error check function here.
2725 */
2726 if (width < 1 || width > maxTextureSize ||
2727 (!ctx->Extensions.ARB_texture_non_power_of_two && logbase2(width) < 0))
2728 return GL_INVALID_VALUE;
2729
2730 if ((height < 1 || height > maxTextureSize ||
2731 (!ctx->Extensions.ARB_texture_non_power_of_two && logbase2(height) < 0))
2732 && dimensions > 1)
2733 return GL_INVALID_VALUE;
2734
2735 if ((depth < 1 || depth > maxTextureSize ||
2736 (!ctx->Extensions.ARB_texture_non_power_of_two && logbase2(depth) < 0))
2737 && dimensions > 2)
2738 return GL_INVALID_VALUE;
2739
2740 /* For cube map, width must equal height */
2741 if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
2742 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB && width != height)
2743 return GL_INVALID_VALUE;
2744
2745 if (level < 0 || level >= maxLevels)
2746 return GL_INVALID_VALUE;
2747
2748 expectedSize = ctx->Driver.CompressedTextureSize(ctx, width, height, depth,
2749 internalFormat);
2750 if (expectedSize != imageSize)
2751 return GL_INVALID_VALUE;
2752
2753 return GL_NO_ERROR;
2754 }
2755
2756
2757 /**
2758 * Error checking for glCompressedTexSubImage[123]D().
2759 * \warning There are some bad assumptions here about the size of compressed
2760 * texture tiles (multiple of 4) used to test the validity of the
2761 * offset and size parameters.
2762 * \return error code or GL_NO_ERROR.
2763 */
2764 static GLenum
2765 compressed_subtexture_error_check(GLcontext *ctx, GLint dimensions,
2766 GLenum target, GLint level,
2767 GLint xoffset, GLint yoffset, GLint zoffset,
2768 GLsizei width, GLsizei height, GLsizei depth,
2769 GLenum format, GLsizei imageSize)
2770 {
2771 GLint expectedSize, maxLevels = 0, maxTextureSize;
2772 (void) zoffset;
2773
2774 if (dimensions == 1) {
2775 /* 1D compressed textures not allowed */
2776 return GL_INVALID_ENUM;
2777 }
2778 else if (dimensions == 2) {
2779 if (target == GL_PROXY_TEXTURE_2D) {
2780 maxLevels = ctx->Const.MaxTextureLevels;
2781 }
2782 else if (target == GL_TEXTURE_2D) {
2783 maxLevels = ctx->Const.MaxTextureLevels;
2784 }
2785 else if (target == GL_PROXY_TEXTURE_CUBE_MAP_ARB) {
2786 if (!ctx->Extensions.ARB_texture_cube_map)
2787 return GL_INVALID_ENUM; /*target*/
2788 maxLevels = ctx->Const.MaxCubeTextureLevels;
2789 }
2790 else if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
2791 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB) {
2792 if (!ctx->Extensions.ARB_texture_cube_map)
2793 return GL_INVALID_ENUM; /*target*/
2794 maxLevels = ctx->Const.MaxCubeTextureLevels;
2795 }
2796 else {
2797 return GL_INVALID_ENUM; /*target*/
2798 }
2799 }
2800 else if (dimensions == 3) {
2801 /* 3D compressed textures not allowed */
2802 return GL_INVALID_ENUM;
2803 }
2804
2805 maxTextureSize = 1 << (maxLevels - 1);
2806
2807 if (!is_compressed_format(ctx, format))
2808 return GL_INVALID_ENUM;
2809
2810 if (width < 1 || width > maxTextureSize)
2811 return GL_INVALID_VALUE;
2812
2813 if ((height < 1 || height > maxTextureSize)
2814 && dimensions > 1)
2815 return GL_INVALID_VALUE;
2816
2817 if (level < 0 || level >= maxLevels)
2818 return GL_INVALID_VALUE;
2819
2820 /* XXX these tests are specific to the compressed format.
2821 * this code should be generalized in some way.
2822 */
2823 if ((xoffset & 3) != 0 || (yoffset & 3) != 0)
2824 return GL_INVALID_VALUE;
2825
2826 if ((width & 3) != 0 && width != 2 && width != 1)
2827 return GL_INVALID_VALUE;
2828
2829 if ((height & 3) != 0 && height != 2 && height != 1)
2830 return GL_INVALID_VALUE;
2831
2832 expectedSize = ctx->Driver.CompressedTextureSize(ctx, width, height, depth,
2833 format);
2834 if (expectedSize != imageSize)
2835 return GL_INVALID_VALUE;
2836
2837 return GL_NO_ERROR;
2838 }
2839
2840
2841
2842 void GLAPIENTRY
2843 _mesa_CompressedTexImage1DARB(GLenum target, GLint level,
2844 GLenum internalFormat, GLsizei width,
2845 GLint border, GLsizei imageSize,
2846 const GLvoid *data)
2847 {
2848 GET_CURRENT_CONTEXT(ctx);
2849 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2850
2851 if (target == GL_TEXTURE_1D) {
2852 struct gl_texture_unit *texUnit;
2853 struct gl_texture_object *texObj;
2854 struct gl_texture_image *texImage;
2855 GLenum error = compressed_texture_error_check(ctx, 1, target, level,
2856 internalFormat, width, 1, 1, border, imageSize);
2857 if (error) {
2858 _mesa_error(ctx, error, "glCompressedTexImage1D");
2859 return;
2860 }
2861
2862 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2863 texObj = _mesa_select_tex_object(ctx, texUnit, target);
2864 texImage = _mesa_get_tex_image(ctx, texUnit, target, level);
2865 if (!texImage) {
2866 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCompressedTexImage1D");
2867 return;
2868 }
2869 else if (texImage->Data && !texImage->IsClientData) {
2870 MESA_PBUFFER_FREE(texImage->Data);
2871 }
2872 texImage->Data = NULL;
2873
2874 _mesa_init_teximage_fields(ctx, target, texImage, width, 1, 1,
2875 border, internalFormat);
2876
2877 ASSERT(ctx->Driver.CompressedTexImage1D);
2878 (*ctx->Driver.CompressedTexImage1D)(ctx, target, level,
2879 internalFormat, width, border,
2880 imageSize, data,
2881 texObj, texImage);
2882
2883 /* state update */
2884 texObj->Complete = GL_FALSE;
2885 ctx->NewState |= _NEW_TEXTURE;
2886 }
2887 else if (target == GL_PROXY_TEXTURE_1D) {
2888 /* Proxy texture: check for errors and update proxy state */
2889 GLenum error = compressed_texture_error_check(ctx, 1, target, level,
2890 internalFormat, width, 1, 1, border, imageSize);
2891 if (!error) {
2892 ASSERT(ctx->Driver.TestProxyTexImage);
2893 error = !(*ctx->Driver.TestProxyTexImage)(ctx, target, level,
2894 internalFormat, GL_NONE, GL_NONE,
2895 width, 1, 1, border);
2896 }
2897 if (error) {
2898 /* if error, clear all proxy texture image parameters */
2899 struct gl_texture_image *texImage;
2900 texImage = _mesa_get_proxy_tex_image(ctx, target, level);
2901 if (texImage)
2902 clear_teximage_fields(texImage);
2903 }
2904 else {
2905 /* store the teximage parameters */
2906 struct gl_texture_unit *texUnit;
2907 struct gl_texture_image *texImage;
2908 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2909 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
2910 _mesa_init_teximage_fields(ctx, target, texImage, width, 1, 1,
2911 border, internalFormat);
2912 }
2913 }
2914 else {
2915 _mesa_error(ctx, GL_INVALID_ENUM, "glCompressedTexImage1D(target)");
2916 return;
2917 }
2918 }
2919
2920
2921 void GLAPIENTRY
2922 _mesa_CompressedTexImage2DARB(GLenum target, GLint level,
2923 GLenum internalFormat, GLsizei width,
2924 GLsizei height, GLint border, GLsizei imageSize,
2925 const GLvoid *data)
2926 {
2927 GET_CURRENT_CONTEXT(ctx);
2928 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2929
2930 if (target == GL_TEXTURE_2D ||
2931 (ctx->Extensions.ARB_texture_cube_map &&
2932 target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
2933 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB)) {
2934 struct gl_texture_unit *texUnit;
2935 struct gl_texture_object *texObj;
2936 struct gl_texture_image *texImage;
2937 GLenum error = compressed_texture_error_check(ctx, 2, target, level,
2938 internalFormat, width, height, 1, border, imageSize);
2939 if (error) {
2940 _mesa_error(ctx, error, "glCompressedTexImage2D");
2941 return;
2942 }
2943
2944 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2945 texObj = _mesa_select_tex_object(ctx, texUnit, target);
2946 texImage = _mesa_get_tex_image(ctx, texUnit, target, level);
2947 if (!texImage) {
2948 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCompressedTexImage2D");
2949 return;
2950 }
2951 else if (texImage->Data && !texImage->IsClientData) {
2952 MESA_PBUFFER_FREE(texImage->Data);
2953 }
2954 texImage->Data = NULL;
2955
2956 _mesa_init_teximage_fields(ctx, target, texImage, width, height, 1,
2957 border, internalFormat);
2958
2959 ASSERT(ctx->Driver.CompressedTexImage2D);
2960 (*ctx->Driver.CompressedTexImage2D)(ctx, target, level,
2961 internalFormat, width, height,
2962 border, imageSize, data,
2963 texObj, texImage);
2964
2965 /* state update */
2966 texObj->Complete = GL_FALSE;
2967 ctx->NewState |= _NEW_TEXTURE;
2968 }
2969 else if (target == GL_PROXY_TEXTURE_2D ||
2970 (target == GL_PROXY_TEXTURE_CUBE_MAP_ARB &&
2971 ctx->Extensions.ARB_texture_cube_map)) {
2972 /* Proxy texture: check for errors and update proxy state */
2973 GLenum error = compressed_texture_error_check(ctx, 2, target, level,
2974 internalFormat, width, height, 1, border, imageSize);
2975 if (!error) {
2976 ASSERT(ctx->Driver.TestProxyTexImage);
2977 error = !(*ctx->Driver.TestProxyTexImage)(ctx, target, level,
2978 internalFormat, GL_NONE, GL_NONE,
2979 width, height, 1, border);
2980 }
2981 if (error) {
2982 /* if error, clear all proxy texture image parameters */
2983 struct gl_texture_image *texImage;
2984 texImage = _mesa_get_proxy_tex_image(ctx, target, level);
2985 if (texImage)
2986 clear_teximage_fields(texImage);
2987 }
2988 else {
2989 /* store the teximage parameters */
2990 struct gl_texture_unit *texUnit;
2991 struct gl_texture_image *texImage;
2992 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2993 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
2994 _mesa_init_teximage_fields(ctx, target, texImage, width, height, 1,
2995 border, internalFormat);
2996 }
2997 }
2998 else {
2999 _mesa_error(ctx, GL_INVALID_ENUM, "glCompressedTexImage2D(target)");
3000 return;
3001 }
3002 }
3003
3004
3005 void GLAPIENTRY
3006 _mesa_CompressedTexImage3DARB(GLenum target, GLint level,
3007 GLenum internalFormat, GLsizei width,
3008 GLsizei height, GLsizei depth, GLint border,
3009 GLsizei imageSize, const GLvoid *data)
3010 {
3011 GET_CURRENT_CONTEXT(ctx);
3012 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3013
3014 if (target == GL_TEXTURE_3D) {
3015 struct gl_texture_unit *texUnit;
3016 struct gl_texture_object *texObj;
3017 struct gl_texture_image *texImage;
3018 GLenum error = compressed_texture_error_check(ctx, 3, target, level,
3019 internalFormat, width, height, depth, border, imageSize);
3020 if (error) {
3021 _mesa_error(ctx, error, "glCompressedTexImage3D");
3022 return;
3023 }
3024
3025 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
3026 texObj = _mesa_select_tex_object(ctx, texUnit, target);
3027 texImage = _mesa_get_tex_image(ctx, texUnit, target, level);
3028 if (!texImage) {
3029 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCompressedTexImage3D");
3030 return;
3031 }
3032 else if (texImage->Data && !texImage->IsClientData) {
3033 MESA_PBUFFER_FREE(texImage->Data);
3034 }
3035 texImage->Data = NULL;
3036
3037 _mesa_init_teximage_fields(ctx, target, texImage, width, height, depth,
3038 border, internalFormat);
3039
3040 ASSERT(ctx->Driver.CompressedTexImage3D);
3041 (*ctx->Driver.CompressedTexImage3D)(ctx, target, level,
3042 internalFormat,
3043 width, height, depth,
3044 border, imageSize, data,
3045 texObj, texImage);
3046
3047 /* state update */
3048 texObj->Complete = GL_FALSE;
3049 ctx->NewState |= _NEW_TEXTURE;
3050 }
3051 else if (target == GL_PROXY_TEXTURE_3D) {
3052 /* Proxy texture: check for errors and update proxy state */
3053 GLenum error = compressed_texture_error_check(ctx, 3, target, level,
3054 internalFormat, width, height, depth, border, imageSize);
3055 if (!error) {
3056 ASSERT(ctx->Driver.TestProxyTexImage);
3057 error = !(*ctx->Driver.TestProxyTexImage)(ctx, target, level,
3058 internalFormat, GL_NONE, GL_NONE,
3059 width, height, depth, border);
3060 }
3061 if (error) {
3062 /* if error, clear all proxy texture image parameters */
3063 struct gl_texture_image *texImage;
3064 texImage = _mesa_get_proxy_tex_image(ctx, target, level);
3065 if (texImage)
3066 clear_teximage_fields(texImage);
3067 }
3068 else {
3069 /* store the teximage parameters */
3070 struct gl_texture_unit *texUnit;
3071 struct gl_texture_image *texImage;
3072 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
3073 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
3074 _mesa_init_teximage_fields(ctx, target, texImage, width, height,
3075 depth, border, internalFormat);
3076 }
3077 }
3078 else {
3079 _mesa_error(ctx, GL_INVALID_ENUM, "glCompressedTexImage3D(target)");
3080 return;
3081 }
3082 }
3083
3084
3085 void GLAPIENTRY
3086 _mesa_CompressedTexSubImage1DARB(GLenum target, GLint level, GLint xoffset,
3087 GLsizei width, GLenum format,
3088 GLsizei imageSize, const GLvoid *data)
3089 {
3090 struct gl_texture_unit *texUnit;
3091 struct gl_texture_object *texObj;
3092 struct gl_texture_image *texImage;
3093 GLenum error;
3094 GET_CURRENT_CONTEXT(ctx);
3095 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3096
3097 error = compressed_subtexture_error_check(ctx, 1, target, level,
3098 xoffset, 0, 0, width, 1, 1, format, imageSize);
3099 if (error) {
3100 _mesa_error(ctx, error, "glCompressedTexSubImage1D");
3101 return;
3102 }
3103
3104 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
3105 texObj = _mesa_select_tex_object(ctx, texUnit, target);
3106 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
3107 assert(texImage);
3108
3109 if ((GLint) format != texImage->IntFormat) {
3110 _mesa_error(ctx, GL_INVALID_OPERATION,
3111 "glCompressedTexSubImage1D(format)");
3112 return;
3113 }
3114
3115 if ((width == 1 || width == 2) && (GLuint) width != texImage->Width) {
3116 _mesa_error(ctx, GL_INVALID_VALUE, "glCompressedTexSubImage1D(width)");
3117 return;
3118 }
3119
3120 if (width == 0)
3121 return; /* no-op, not an error */
3122
3123 if (ctx->Driver.CompressedTexSubImage1D) {
3124 (*ctx->Driver.CompressedTexSubImage1D)(ctx, target, level,
3125 xoffset, width,
3126 format, imageSize, data,
3127 texObj, texImage);
3128 }
3129 ctx->NewState |= _NEW_TEXTURE;
3130 }
3131
3132
3133 void GLAPIENTRY
3134 _mesa_CompressedTexSubImage2DARB(GLenum target, GLint level, GLint xoffset,
3135 GLint yoffset, GLsizei width, GLsizei height,
3136 GLenum format, GLsizei imageSize,
3137 const GLvoid *data)
3138 {
3139 struct gl_texture_unit *texUnit;
3140 struct gl_texture_object *texObj;
3141 struct gl_texture_image *texImage;
3142 GLenum error;
3143 GET_CURRENT_CONTEXT(ctx);
3144 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3145
3146 error = compressed_subtexture_error_check(ctx, 2, target, level,
3147 xoffset, yoffset, 0, width, height, 1, format, imageSize);
3148 if (error) {
3149 /* XXX proxy target? */
3150 _mesa_error(ctx, error, "glCompressedTexSubImage2D");
3151 return;
3152 }
3153
3154 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
3155 texObj = _mesa_select_tex_object(ctx, texUnit, target);
3156 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
3157 assert(texImage);
3158
3159 if ((GLint) format != texImage->IntFormat) {
3160 _mesa_error(ctx, GL_INVALID_OPERATION,
3161 "glCompressedTexSubImage2D(format)");
3162 return;
3163 }
3164
3165 if (((width == 1 || width == 2) && (GLuint) width != texImage->Width) ||
3166 ((height == 1 || height == 2) && (GLuint) height != texImage->Height)) {
3167 _mesa_error(ctx, GL_INVALID_VALUE, "glCompressedTexSubImage2D(size)");
3168 return;
3169 }
3170
3171 if (width == 0 || height == 0)
3172 return; /* no-op, not an error */
3173
3174 if (ctx->Driver.CompressedTexSubImage2D) {
3175 (*ctx->Driver.CompressedTexSubImage2D)(ctx, target, level,
3176 xoffset, yoffset, width, height,
3177 format, imageSize, data,
3178 texObj, texImage);
3179 }
3180 ctx->NewState |= _NEW_TEXTURE;
3181 }
3182
3183
3184 void GLAPIENTRY
3185 _mesa_CompressedTexSubImage3DARB(GLenum target, GLint level, GLint xoffset,
3186 GLint yoffset, GLint zoffset, GLsizei width,
3187 GLsizei height, GLsizei depth, GLenum format,
3188 GLsizei imageSize, const GLvoid *data)
3189 {
3190 struct gl_texture_unit *texUnit;
3191 struct gl_texture_object *texObj;
3192 struct gl_texture_image *texImage;
3193 GLenum error;
3194 GET_CURRENT_CONTEXT(ctx);
3195 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3196
3197 error = compressed_subtexture_error_check(ctx, 3, target, level,
3198 xoffset, yoffset, zoffset, width, height, depth, format, imageSize);
3199 if (error) {
3200 _mesa_error(ctx, error, "glCompressedTexSubImage2D");
3201 return;
3202 }
3203
3204 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
3205 texObj = _mesa_select_tex_object(ctx, texUnit, target);
3206 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
3207 assert(texImage);
3208
3209 if ((GLint) format != texImage->IntFormat) {
3210 _mesa_error(ctx, GL_INVALID_OPERATION,
3211 "glCompressedTexSubImage3D(format)");
3212 return;
3213 }
3214
3215 if (((width == 1 || width == 2) && (GLuint) width != texImage->Width) ||
3216 ((height == 1 || height == 2) && (GLuint) height != texImage->Height) ||
3217 ((depth == 1 || depth == 2) && (GLuint) depth != texImage->Depth)) {
3218 _mesa_error(ctx, GL_INVALID_VALUE, "glCompressedTexSubImage3D(size)");
3219 return;
3220 }
3221
3222 if (width == 0 || height == 0 || depth == 0)
3223 return; /* no-op, not an error */
3224
3225 if (ctx->Driver.CompressedTexSubImage3D) {
3226 (*ctx->Driver.CompressedTexSubImage3D)(ctx, target, level,
3227 xoffset, yoffset, zoffset,
3228 width, height, depth,
3229 format, imageSize, data,
3230 texObj, texImage);
3231 }
3232 ctx->NewState |= _NEW_TEXTURE;
3233 }
3234
3235
3236 void GLAPIENTRY
3237 _mesa_GetCompressedTexImageARB(GLenum target, GLint level, GLvoid *img)
3238 {
3239 const struct gl_texture_unit *texUnit;
3240 const struct gl_texture_object *texObj;
3241 struct gl_texture_image *texImage;
3242 GLint maxLevels;
3243 GET_CURRENT_CONTEXT(ctx);
3244 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3245
3246 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
3247 texObj = _mesa_select_tex_object(ctx, texUnit, target);
3248 if (!texObj) {
3249 _mesa_error(ctx, GL_INVALID_ENUM, "glGetCompressedTexImageARB");
3250 return;
3251 }
3252
3253 maxLevels = _mesa_max_texture_levels(ctx, target);
3254 ASSERT(maxLevels > 0); /* 0 indicates bad target, caught above */
3255
3256 if (level < 0 || level >= maxLevels) {
3257 _mesa_error(ctx, GL_INVALID_VALUE, "glGetCompressedTexImageARB(level)");
3258 return;
3259 }
3260
3261 if (is_proxy_target(target)) {
3262 _mesa_error(ctx, GL_INVALID_ENUM, "glGetCompressedTexImageARB(target)");
3263 return;
3264 }
3265
3266 texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
3267 if (!texImage) {
3268 /* probably invalid mipmap level */
3269 _mesa_error(ctx, GL_INVALID_VALUE, "glGetCompressedTexImageARB(level)");
3270 return;
3271 }
3272
3273 if (!texImage->IsCompressed) {
3274 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetCompressedTexImageARB");
3275 return;
3276 }
3277
3278 /* this typically calls _mesa_get_compressed_teximage() */
3279 ctx->Driver.GetCompressedTexImage(ctx, target, level, img, texObj,texImage);
3280 }