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