mesa: consolidate assertions in teximage code
[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 ASSERT(texImage->TexFormat != MESA_FORMAT_NONE);
2280 return;
2281 }
2282 }
2283
2284 /* choose format from scratch */
2285 texImage->TexFormat = ctx->Driver.ChooseTextureFormat(ctx, internalFormat,
2286 format, type);
2287 ASSERT(texImage->TexFormat != MESA_FORMAT_NONE);
2288 }
2289
2290
2291
2292 /*
2293 * Called from the API. Note that width includes the border.
2294 */
2295 void GLAPIENTRY
2296 _mesa_TexImage1D( GLenum target, GLint level, GLint internalFormat,
2297 GLsizei width, GLint border, GLenum format,
2298 GLenum type, const GLvoid *pixels )
2299 {
2300 GET_CURRENT_CONTEXT(ctx);
2301 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2302
2303 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
2304 _mesa_debug(ctx, "glTexImage1D %s %d %s %d %d %s %s %p\n",
2305 _mesa_lookup_enum_by_nr(target), level,
2306 _mesa_lookup_enum_by_nr(internalFormat), width, border,
2307 _mesa_lookup_enum_by_nr(format),
2308 _mesa_lookup_enum_by_nr(type), pixels);
2309
2310 internalFormat = override_internal_format(internalFormat, width, 1);
2311
2312 if (target == GL_TEXTURE_1D) {
2313 /* non-proxy target */
2314 struct gl_texture_object *texObj;
2315 struct gl_texture_image *texImage;
2316 const GLuint face = _mesa_tex_target_to_face(target);
2317
2318 if (texture_error_check(ctx, target, level, internalFormat,
2319 format, type, 1, width, 1, 1, border)) {
2320 return; /* error was recorded */
2321 }
2322
2323 if (ctx->NewState & _MESA_NEW_TRANSFER_STATE)
2324 _mesa_update_state(ctx);
2325
2326 texObj = _mesa_get_current_tex_object(ctx, target);
2327 _mesa_lock_texture(ctx, texObj);
2328 {
2329 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
2330 if (!texImage) {
2331 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage1D");
2332 }
2333 else {
2334 if (texImage->Data) {
2335 ctx->Driver.FreeTexImageData( ctx, texImage );
2336 }
2337
2338 ASSERT(texImage->Data == NULL);
2339
2340 clear_teximage_fields(texImage); /* not really needed, but helpful */
2341 _mesa_init_teximage_fields(ctx, target, texImage,
2342 width, 1, 1,
2343 border, internalFormat);
2344
2345 _mesa_choose_texture_format(ctx, texObj, texImage, target, level,
2346 internalFormat, format, type);
2347
2348 /* Give the texture to the driver. <pixels> may be null. */
2349 ASSERT(ctx->Driver.TexImage1D);
2350 ctx->Driver.TexImage1D(ctx, target, level, internalFormat,
2351 width, border, format, type, pixels,
2352 &ctx->Unpack, texObj, texImage);
2353
2354 _mesa_set_fetch_functions(texImage, 1);
2355
2356 check_gen_mipmap(ctx, target, texObj, level);
2357
2358 update_fbo_texture(ctx, texObj, face, level);
2359
2360 /* state update */
2361 texObj->_Complete = GL_FALSE;
2362 ctx->NewState |= _NEW_TEXTURE;
2363 }
2364 }
2365 _mesa_unlock_texture(ctx, texObj);
2366 }
2367 else if (target == GL_PROXY_TEXTURE_1D) {
2368 /* Proxy texture: check for errors and update proxy state */
2369 struct gl_texture_image *texImage;
2370 texImage = _mesa_get_proxy_tex_image(ctx, target, level);
2371 if (texture_error_check(ctx, target, level, internalFormat,
2372 format, type, 1, width, 1, 1, border)) {
2373 /* when error, clear all proxy texture image parameters */
2374 if (texImage)
2375 clear_teximage_fields(texImage);
2376 }
2377 else {
2378 /* no error, set the tex image parameters */
2379 struct gl_texture_object *texObj =
2380 _mesa_get_current_tex_object(ctx, target);
2381 ASSERT(texImage);
2382 _mesa_init_teximage_fields(ctx, target, texImage,
2383 width, 1, 1,
2384 border, internalFormat);
2385 _mesa_choose_texture_format(ctx, texObj, texImage, target, level,
2386 internalFormat, format, type);
2387 }
2388 }
2389 else {
2390 _mesa_error( ctx, GL_INVALID_ENUM, "glTexImage1D(target)" );
2391 return;
2392 }
2393 }
2394
2395
2396 void GLAPIENTRY
2397 _mesa_TexImage2D( GLenum target, GLint level, GLint internalFormat,
2398 GLsizei width, GLsizei height, GLint border,
2399 GLenum format, GLenum type,
2400 const GLvoid *pixels )
2401 {
2402 GET_CURRENT_CONTEXT(ctx);
2403 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2404
2405 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
2406 _mesa_debug(ctx, "glTexImage2D %s %d %s %d %d %d %s %s %p\n",
2407 _mesa_lookup_enum_by_nr(target), level,
2408 _mesa_lookup_enum_by_nr(internalFormat), width, height,
2409 border, _mesa_lookup_enum_by_nr(format),
2410 _mesa_lookup_enum_by_nr(type), pixels);
2411
2412 internalFormat = override_internal_format(internalFormat, width, height);
2413
2414 if (target == GL_TEXTURE_2D ||
2415 (ctx->Extensions.ARB_texture_cube_map &&
2416 target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
2417 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB) ||
2418 (ctx->Extensions.NV_texture_rectangle &&
2419 target == GL_TEXTURE_RECTANGLE_NV) ||
2420 (ctx->Extensions.MESA_texture_array &&
2421 target == GL_TEXTURE_1D_ARRAY_EXT)) {
2422 /* non-proxy target */
2423 struct gl_texture_object *texObj;
2424 struct gl_texture_image *texImage;
2425 const GLuint face = _mesa_tex_target_to_face(target);
2426
2427 if (texture_error_check(ctx, target, level, internalFormat,
2428 format, type, 2, width, height, 1, border)) {
2429 return; /* error was recorded */
2430 }
2431
2432 if (ctx->NewState & _MESA_NEW_TRANSFER_STATE)
2433 _mesa_update_state(ctx);
2434
2435 texObj = _mesa_get_current_tex_object(ctx, target);
2436 _mesa_lock_texture(ctx, texObj);
2437 {
2438 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
2439 if (!texImage) {
2440 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage2D");
2441 }
2442 else {
2443 if (texImage->Data) {
2444 ctx->Driver.FreeTexImageData( ctx, texImage );
2445 }
2446
2447 ASSERT(texImage->Data == NULL);
2448 clear_teximage_fields(texImage); /* not really needed, but helpful */
2449 _mesa_init_teximage_fields(ctx, target, texImage,
2450 width, height, 1,
2451 border, internalFormat);
2452
2453 _mesa_choose_texture_format(ctx, texObj, texImage, target, level,
2454 internalFormat, format, type);
2455
2456 /* Give the texture to the driver. <pixels> may be null. */
2457 ASSERT(ctx->Driver.TexImage2D);
2458 ctx->Driver.TexImage2D(ctx, target, level, internalFormat,
2459 width, height, border, format, type,
2460 pixels, &ctx->Unpack, texObj, texImage);
2461
2462 _mesa_set_fetch_functions(texImage, 2);
2463
2464 check_gen_mipmap(ctx, target, texObj, level);
2465
2466 update_fbo_texture(ctx, texObj, face, level);
2467
2468 /* state update */
2469 texObj->_Complete = GL_FALSE;
2470 ctx->NewState |= _NEW_TEXTURE;
2471 }
2472 }
2473 _mesa_unlock_texture(ctx, texObj);
2474 }
2475 else if (target == GL_PROXY_TEXTURE_2D ||
2476 (target == GL_PROXY_TEXTURE_CUBE_MAP_ARB &&
2477 ctx->Extensions.ARB_texture_cube_map) ||
2478 (target == GL_PROXY_TEXTURE_RECTANGLE_NV &&
2479 ctx->Extensions.NV_texture_rectangle) ||
2480 (ctx->Extensions.MESA_texture_array &&
2481 target == GL_PROXY_TEXTURE_1D_ARRAY_EXT)) {
2482 /* Proxy texture: check for errors and update proxy state */
2483 struct gl_texture_image *texImage;
2484 texImage = _mesa_get_proxy_tex_image(ctx, target, level);
2485 if (texture_error_check(ctx, target, level, internalFormat,
2486 format, type, 2, width, height, 1, border)) {
2487 /* when error, clear all proxy texture image parameters */
2488 if (texImage)
2489 clear_teximage_fields(texImage);
2490 }
2491 else {
2492 /* no error, set the tex image parameters */
2493 struct gl_texture_object *texObj =
2494 _mesa_get_current_tex_object(ctx, target);
2495 _mesa_init_teximage_fields(ctx, target, texImage,
2496 width, height, 1,
2497 border, internalFormat);
2498 _mesa_choose_texture_format(ctx, texObj, texImage, target, level,
2499 internalFormat, format, type);
2500 }
2501 }
2502 else {
2503 _mesa_error( ctx, GL_INVALID_ENUM, "glTexImage2D(target)" );
2504 return;
2505 }
2506 }
2507
2508
2509 /*
2510 * Called by the API or display list executor.
2511 * Note that width and height include the border.
2512 */
2513 void GLAPIENTRY
2514 _mesa_TexImage3D( GLenum target, GLint level, GLint internalFormat,
2515 GLsizei width, GLsizei height, GLsizei depth,
2516 GLint border, GLenum format, GLenum type,
2517 const GLvoid *pixels )
2518 {
2519 GET_CURRENT_CONTEXT(ctx);
2520 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2521
2522 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
2523 _mesa_debug(ctx, "glTexImage3D %s %d %s %d %d %d %d %s %s %p\n",
2524 _mesa_lookup_enum_by_nr(target), level,
2525 _mesa_lookup_enum_by_nr(internalFormat), width, height,
2526 depth, border, _mesa_lookup_enum_by_nr(format),
2527 _mesa_lookup_enum_by_nr(type), pixels);
2528
2529 internalFormat = override_internal_format(internalFormat, width, height);
2530
2531 if (target == GL_TEXTURE_3D ||
2532 (ctx->Extensions.MESA_texture_array &&
2533 target == GL_TEXTURE_2D_ARRAY_EXT)) {
2534 /* non-proxy target */
2535 struct gl_texture_object *texObj;
2536 struct gl_texture_image *texImage;
2537 const GLuint face = _mesa_tex_target_to_face(target);
2538
2539 if (texture_error_check(ctx, target, level, (GLint) internalFormat,
2540 format, type, 3, width, height, depth, border)) {
2541 return; /* error was recorded */
2542 }
2543
2544 if (ctx->NewState & _MESA_NEW_TRANSFER_STATE)
2545 _mesa_update_state(ctx);
2546
2547 texObj = _mesa_get_current_tex_object(ctx, target);
2548 _mesa_lock_texture(ctx, texObj);
2549 {
2550 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
2551 if (!texImage) {
2552 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage3D");
2553 }
2554 else {
2555 if (texImage->Data) {
2556 ctx->Driver.FreeTexImageData( ctx, texImage );
2557 }
2558
2559 ASSERT(texImage->Data == NULL);
2560 clear_teximage_fields(texImage); /* not really needed, but helpful */
2561 _mesa_init_teximage_fields(ctx, target, texImage,
2562 width, height, depth,
2563 border, internalFormat);
2564
2565 _mesa_choose_texture_format(ctx, texObj, texImage, target, level,
2566 internalFormat, format, type);
2567
2568 /* Give the texture to the driver. <pixels> may be null. */
2569 ASSERT(ctx->Driver.TexImage3D);
2570 ctx->Driver.TexImage3D(ctx, target, level, internalFormat,
2571 width, height, depth, border, format, type,
2572 pixels, &ctx->Unpack, texObj, texImage);
2573
2574 _mesa_set_fetch_functions(texImage, 3);
2575
2576 check_gen_mipmap(ctx, target, texObj, level);
2577
2578 update_fbo_texture(ctx, texObj, face, level);
2579
2580 /* state update */
2581 texObj->_Complete = GL_FALSE;
2582 ctx->NewState |= _NEW_TEXTURE;
2583 }
2584 }
2585 _mesa_unlock_texture(ctx, texObj);
2586 }
2587 else if (target == GL_PROXY_TEXTURE_3D ||
2588 (ctx->Extensions.MESA_texture_array &&
2589 target == GL_PROXY_TEXTURE_2D_ARRAY_EXT)) {
2590 /* Proxy texture: check for errors and update proxy state */
2591 struct gl_texture_image *texImage;
2592 texImage = _mesa_get_proxy_tex_image(ctx, target, level);
2593 if (texture_error_check(ctx, target, level, internalFormat,
2594 format, type, 3, width, height, depth, border)) {
2595 /* when error, clear all proxy texture image parameters */
2596 if (texImage)
2597 clear_teximage_fields(texImage);
2598 }
2599 else {
2600 /* no error, set the tex image parameters */
2601 struct gl_texture_object *texObj =
2602 _mesa_get_current_tex_object(ctx, target);
2603 _mesa_init_teximage_fields(ctx, target, texImage, width, height,
2604 depth, border, internalFormat);
2605 _mesa_choose_texture_format(ctx, texObj, texImage, target, level,
2606 internalFormat, format, type);
2607 }
2608 }
2609 else {
2610 _mesa_error( ctx, GL_INVALID_ENUM, "glTexImage3D(target)" );
2611 return;
2612 }
2613 }
2614
2615
2616 void GLAPIENTRY
2617 _mesa_TexImage3DEXT( GLenum target, GLint level, GLenum internalFormat,
2618 GLsizei width, GLsizei height, GLsizei depth,
2619 GLint border, GLenum format, GLenum type,
2620 const GLvoid *pixels )
2621 {
2622 _mesa_TexImage3D(target, level, (GLint) internalFormat, width, height,
2623 depth, border, format, type, pixels);
2624 }
2625
2626
2627 #if FEATURE_OES_EGL_image
2628 void GLAPIENTRY
2629 _mesa_EGLImageTargetTexture2DOES (GLenum target, GLeglImageOES image)
2630 {
2631 struct gl_texture_object *texObj;
2632 struct gl_texture_image *texImage;
2633 GET_CURRENT_CONTEXT(ctx);
2634 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2635
2636 if (!ctx->Extensions.OES_EGL_image) {
2637 _mesa_error(ctx, GL_INVALID_OPERATION,
2638 "glEGLImageTargetTexture2DOES(unsupported)");
2639 return;
2640 }
2641
2642 if (target != GL_TEXTURE_2D) {
2643 _mesa_error(ctx, GL_INVALID_ENUM,
2644 "glEGLImageTargetTexture2D(target=%d)", target);
2645 return;
2646 }
2647
2648 if (ctx->NewState & _MESA_NEW_TRANSFER_STATE)
2649 _mesa_update_state(ctx);
2650
2651 texObj = _mesa_get_current_tex_object(ctx, target);
2652 _mesa_lock_texture(ctx, texObj);
2653
2654 texImage = _mesa_get_tex_image(ctx, texObj, target, 0);
2655 if (!texImage) {
2656 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glEGLImageTargetTexture2D");
2657 } else {
2658 if (texImage->Data)
2659 ctx->Driver.FreeTexImageData( ctx, texImage );
2660
2661 ASSERT(texImage->Data == NULL);
2662 ctx->Driver.EGLImageTargetTexture2D(ctx, target,
2663 texObj, texImage, image);
2664
2665 /* state update */
2666 texObj->_Complete = GL_FALSE;
2667 ctx->NewState |= _NEW_TEXTURE;
2668 }
2669 _mesa_unlock_texture(ctx, texObj);
2670
2671 }
2672 #endif
2673
2674
2675 void GLAPIENTRY
2676 _mesa_TexSubImage1D( GLenum target, GLint level,
2677 GLint xoffset, GLsizei width,
2678 GLenum format, GLenum type,
2679 const GLvoid *pixels )
2680 {
2681 struct gl_texture_object *texObj;
2682 struct gl_texture_image *texImage;
2683 GET_CURRENT_CONTEXT(ctx);
2684 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2685
2686 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
2687 _mesa_debug(ctx, "glTexSubImage1D %s %d %d %d %s %s %p\n",
2688 _mesa_lookup_enum_by_nr(target), level,
2689 xoffset, width, _mesa_lookup_enum_by_nr(format),
2690 _mesa_lookup_enum_by_nr(type), pixels);
2691
2692 if (ctx->NewState & _MESA_NEW_TRANSFER_STATE)
2693 _mesa_update_state(ctx);
2694
2695 if (subtexture_error_check(ctx, 1, target, level, xoffset, 0, 0,
2696 width, 1, 1, format, type)) {
2697 return; /* error was detected */
2698 }
2699
2700
2701 texObj = _mesa_get_current_tex_object(ctx, target);
2702 assert(texObj);
2703
2704 _mesa_lock_texture(ctx, texObj);
2705 {
2706 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
2707
2708 if (subtexture_error_check2(ctx, 1, target, level, xoffset, 0, 0,
2709 width, 1, 1, format, type, texImage)) {
2710 /* error was recorded */
2711 }
2712 else if (width > 0) {
2713 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2714 xoffset += texImage->Border;
2715
2716 ASSERT(ctx->Driver.TexSubImage1D);
2717 ctx->Driver.TexSubImage1D(ctx, target, level, xoffset, width,
2718 format, type, pixels, &ctx->Unpack,
2719 texObj, texImage);
2720
2721 check_gen_mipmap(ctx, target, texObj, level);
2722
2723 ctx->NewState |= _NEW_TEXTURE;
2724 }
2725 }
2726 _mesa_unlock_texture(ctx, texObj);
2727 }
2728
2729
2730 void GLAPIENTRY
2731 _mesa_TexSubImage2D( GLenum target, GLint level,
2732 GLint xoffset, GLint yoffset,
2733 GLsizei width, GLsizei height,
2734 GLenum format, GLenum type,
2735 const GLvoid *pixels )
2736 {
2737 struct gl_texture_object *texObj;
2738 struct gl_texture_image *texImage;
2739 GET_CURRENT_CONTEXT(ctx);
2740 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2741
2742 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
2743 _mesa_debug(ctx, "glTexSubImage2D %s %d %d %d %d %d %s %s %p\n",
2744 _mesa_lookup_enum_by_nr(target), level,
2745 xoffset, yoffset, width, height,
2746 _mesa_lookup_enum_by_nr(format),
2747 _mesa_lookup_enum_by_nr(type), pixels);
2748
2749 if (ctx->NewState & _MESA_NEW_TRANSFER_STATE)
2750 _mesa_update_state(ctx);
2751
2752 if (subtexture_error_check(ctx, 2, target, level, xoffset, yoffset, 0,
2753 width, height, 1, format, type)) {
2754 return; /* error was detected */
2755 }
2756
2757 texObj = _mesa_get_current_tex_object(ctx, target);
2758
2759 _mesa_lock_texture(ctx, texObj);
2760 {
2761 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
2762
2763 if (subtexture_error_check2(ctx, 2, target, level, xoffset, yoffset, 0,
2764 width, height, 1, format, type, texImage)) {
2765 /* error was recorded */
2766 }
2767 else if (width > 0 && height >= 0) {
2768 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2769 xoffset += texImage->Border;
2770 yoffset += texImage->Border;
2771
2772 ASSERT(ctx->Driver.TexSubImage2D);
2773 ctx->Driver.TexSubImage2D(ctx, target, level, xoffset, yoffset,
2774 width, height, format, type, pixels,
2775 &ctx->Unpack, texObj, texImage);
2776
2777 check_gen_mipmap(ctx, target, texObj, level);
2778
2779 ctx->NewState |= _NEW_TEXTURE;
2780 }
2781 }
2782 _mesa_unlock_texture(ctx, texObj);
2783 }
2784
2785
2786
2787 void GLAPIENTRY
2788 _mesa_TexSubImage3D( GLenum target, GLint level,
2789 GLint xoffset, GLint yoffset, GLint zoffset,
2790 GLsizei width, GLsizei height, GLsizei depth,
2791 GLenum format, GLenum type,
2792 const GLvoid *pixels )
2793 {
2794 struct gl_texture_object *texObj;
2795 struct gl_texture_image *texImage;
2796 GET_CURRENT_CONTEXT(ctx);
2797 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2798
2799 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
2800 _mesa_debug(ctx, "glTexSubImage3D %s %d %d %d %d %d %d %d %s %s %p\n",
2801 _mesa_lookup_enum_by_nr(target), level,
2802 xoffset, yoffset, zoffset, width, height, depth,
2803 _mesa_lookup_enum_by_nr(format),
2804 _mesa_lookup_enum_by_nr(type), pixels);
2805
2806 if (ctx->NewState & _MESA_NEW_TRANSFER_STATE)
2807 _mesa_update_state(ctx);
2808
2809 if (subtexture_error_check(ctx, 3, target, level, xoffset, yoffset, zoffset,
2810 width, height, depth, format, type)) {
2811 return; /* error was detected */
2812 }
2813
2814 texObj = _mesa_get_current_tex_object(ctx, target);
2815
2816 _mesa_lock_texture(ctx, texObj);
2817 {
2818 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
2819
2820 if (subtexture_error_check2(ctx, 3, target, level,
2821 xoffset, yoffset, zoffset,
2822 width, height, depth,
2823 format, type, texImage)) {
2824 /* error was recorded */
2825 }
2826 else if (width > 0 && height > 0 && height > 0) {
2827 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2828 xoffset += texImage->Border;
2829 yoffset += texImage->Border;
2830 zoffset += texImage->Border;
2831
2832 ASSERT(ctx->Driver.TexSubImage3D);
2833 ctx->Driver.TexSubImage3D(ctx, target, level,
2834 xoffset, yoffset, zoffset,
2835 width, height, depth,
2836 format, type, pixels,
2837 &ctx->Unpack, texObj, texImage );
2838
2839 check_gen_mipmap(ctx, target, texObj, level);
2840
2841 ctx->NewState |= _NEW_TEXTURE;
2842 }
2843 }
2844 _mesa_unlock_texture(ctx, texObj);
2845 }
2846
2847
2848
2849 void GLAPIENTRY
2850 _mesa_CopyTexImage1D( GLenum target, GLint level,
2851 GLenum internalFormat,
2852 GLint x, GLint y,
2853 GLsizei width, GLint border )
2854 {
2855 struct gl_texture_object *texObj;
2856 struct gl_texture_image *texImage;
2857 const GLuint face = _mesa_tex_target_to_face(target);
2858 GET_CURRENT_CONTEXT(ctx);
2859 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2860
2861 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
2862 _mesa_debug(ctx, "glCopyTexImage1D %s %d %s %d %d %d %d\n",
2863 _mesa_lookup_enum_by_nr(target), level,
2864 _mesa_lookup_enum_by_nr(internalFormat),
2865 x, y, width, border);
2866
2867 if (ctx->NewState & NEW_COPY_TEX_STATE)
2868 _mesa_update_state(ctx);
2869
2870 if (copytexture_error_check(ctx, 1, target, level, internalFormat,
2871 width, 1, border))
2872 return;
2873
2874 texObj = _mesa_get_current_tex_object(ctx, target);
2875
2876 _mesa_lock_texture(ctx, texObj);
2877 {
2878 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
2879 if (!texImage) {
2880 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexImage1D");
2881 }
2882 else {
2883 if (texImage->Data) {
2884 ctx->Driver.FreeTexImageData( ctx, texImage );
2885 }
2886
2887 ASSERT(texImage->Data == NULL);
2888
2889 clear_teximage_fields(texImage); /* not really needed, but helpful */
2890 _mesa_init_teximage_fields(ctx, target, texImage, width, 1, 1,
2891 border, internalFormat);
2892
2893 _mesa_choose_texture_format(ctx, texObj, texImage, target, level,
2894 internalFormat, GL_NONE, GL_NONE);
2895
2896 ASSERT(ctx->Driver.CopyTexImage1D);
2897 ctx->Driver.CopyTexImage1D(ctx, target, level, internalFormat,
2898 x, y, width, border);
2899
2900 _mesa_set_fetch_functions(texImage, 1);
2901
2902 check_gen_mipmap(ctx, target, texObj, level);
2903
2904 update_fbo_texture(ctx, texObj, face, level);
2905
2906 /* state update */
2907 texObj->_Complete = GL_FALSE;
2908 ctx->NewState |= _NEW_TEXTURE;
2909 }
2910 }
2911 _mesa_unlock_texture(ctx, texObj);
2912 }
2913
2914
2915
2916 void GLAPIENTRY
2917 _mesa_CopyTexImage2D( GLenum target, GLint level, GLenum internalFormat,
2918 GLint x, GLint y, GLsizei width, GLsizei height,
2919 GLint border )
2920 {
2921 struct gl_texture_object *texObj;
2922 struct gl_texture_image *texImage;
2923 const GLuint face = _mesa_tex_target_to_face(target);
2924 GET_CURRENT_CONTEXT(ctx);
2925 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2926
2927 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
2928 _mesa_debug(ctx, "glCopyTexImage2D %s %d %s %d %d %d %d %d\n",
2929 _mesa_lookup_enum_by_nr(target), level,
2930 _mesa_lookup_enum_by_nr(internalFormat),
2931 x, y, width, height, border);
2932
2933 if (ctx->NewState & NEW_COPY_TEX_STATE)
2934 _mesa_update_state(ctx);
2935
2936 if (copytexture_error_check(ctx, 2, target, level, internalFormat,
2937 width, height, border))
2938 return;
2939
2940 texObj = _mesa_get_current_tex_object(ctx, target);
2941
2942 _mesa_lock_texture(ctx, texObj);
2943 {
2944 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
2945
2946 if (!texImage) {
2947 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexImage2D");
2948 }
2949 else {
2950 if (texImage->Data) {
2951 ctx->Driver.FreeTexImageData( ctx, texImage );
2952 }
2953
2954 ASSERT(texImage->Data == NULL);
2955
2956 clear_teximage_fields(texImage); /* not really needed, but helpful */
2957 _mesa_init_teximage_fields(ctx, target, texImage,
2958 width, height, 1,
2959 border, internalFormat);
2960
2961 _mesa_choose_texture_format(ctx, texObj, texImage, target, level,
2962 internalFormat, GL_NONE, GL_NONE);
2963
2964 ASSERT(ctx->Driver.CopyTexImage2D);
2965 ctx->Driver.CopyTexImage2D(ctx, target, level, internalFormat,
2966 x, y, width, height, border);
2967
2968 _mesa_set_fetch_functions(texImage, 2);
2969
2970 check_gen_mipmap(ctx, target, texObj, level);
2971
2972 update_fbo_texture(ctx, texObj, face, level);
2973
2974 /* state update */
2975 texObj->_Complete = GL_FALSE;
2976 ctx->NewState |= _NEW_TEXTURE;
2977 }
2978 }
2979 _mesa_unlock_texture(ctx, texObj);
2980 }
2981
2982
2983 void GLAPIENTRY
2984 _mesa_CopyTexSubImage1D( GLenum target, GLint level,
2985 GLint xoffset, GLint x, GLint y, GLsizei width )
2986 {
2987 struct gl_texture_object *texObj;
2988 struct gl_texture_image *texImage;
2989 GLint yoffset = 0;
2990 GLsizei height = 1;
2991
2992 GET_CURRENT_CONTEXT(ctx);
2993 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2994
2995 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
2996 _mesa_debug(ctx, "glCopyTexSubImage1D %s %d %d %d %d %d\n",
2997 _mesa_lookup_enum_by_nr(target),
2998 level, xoffset, x, y, width);
2999
3000 if (ctx->NewState & NEW_COPY_TEX_STATE)
3001 _mesa_update_state(ctx);
3002
3003 if (copytexsubimage_error_check1(ctx, 1, target, level))
3004 return;
3005
3006 texObj = _mesa_get_current_tex_object(ctx, target);
3007
3008 _mesa_lock_texture(ctx, texObj);
3009 {
3010 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
3011
3012 if (copytexsubimage_error_check2(ctx, 1, target, level,
3013 xoffset, 0, 0, width, 1, texImage)) {
3014 /* error was recorded */
3015 }
3016 else {
3017 /* If we have a border, xoffset=-1 is legal. Bias by border width */
3018 xoffset += texImage->Border;
3019
3020 if (_mesa_clip_copytexsubimage(ctx, &xoffset, &yoffset, &x, &y,
3021 &width, &height)) {
3022 ASSERT(ctx->Driver.CopyTexSubImage1D);
3023 ctx->Driver.CopyTexSubImage1D(ctx, target, level,
3024 xoffset, x, y, width);
3025
3026 check_gen_mipmap(ctx, target, texObj, level);
3027
3028 ctx->NewState |= _NEW_TEXTURE;
3029 }
3030 }
3031 }
3032 _mesa_unlock_texture(ctx, texObj);
3033 }
3034
3035
3036
3037 void GLAPIENTRY
3038 _mesa_CopyTexSubImage2D( GLenum target, GLint level,
3039 GLint xoffset, GLint yoffset,
3040 GLint x, GLint y, GLsizei width, GLsizei height )
3041 {
3042 struct gl_texture_object *texObj;
3043 struct gl_texture_image *texImage;
3044 GET_CURRENT_CONTEXT(ctx);
3045 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3046
3047 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
3048 _mesa_debug(ctx, "glCopyTexSubImage2D %s %d %d %d %d %d %d %d\n",
3049 _mesa_lookup_enum_by_nr(target),
3050 level, xoffset, yoffset, x, y, width, height);
3051
3052 if (ctx->NewState & NEW_COPY_TEX_STATE)
3053 _mesa_update_state(ctx);
3054
3055 if (copytexsubimage_error_check1(ctx, 2, target, level))
3056 return;
3057
3058 texObj = _mesa_get_current_tex_object(ctx, target);
3059
3060 _mesa_lock_texture(ctx, texObj);
3061 {
3062 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
3063
3064 if (copytexsubimage_error_check2(ctx, 2, target, level,
3065 xoffset, yoffset, 0,
3066 width, height, texImage)) {
3067 /* error was recorded */
3068 }
3069 else {
3070 /* If we have a border, xoffset=-1 is legal. Bias by border width */
3071 xoffset += texImage->Border;
3072 yoffset += texImage->Border;
3073
3074 if (_mesa_clip_copytexsubimage(ctx, &xoffset, &yoffset, &x, &y,
3075 &width, &height)) {
3076 ASSERT(ctx->Driver.CopyTexSubImage2D);
3077 ctx->Driver.CopyTexSubImage2D(ctx, target, level, xoffset, yoffset,
3078 x, y, width, height);
3079
3080 check_gen_mipmap(ctx, target, texObj, level);
3081
3082 ctx->NewState |= _NEW_TEXTURE;
3083 }
3084 }
3085 }
3086 _mesa_unlock_texture(ctx, texObj);
3087 }
3088
3089
3090
3091 void GLAPIENTRY
3092 _mesa_CopyTexSubImage3D( GLenum target, GLint level,
3093 GLint xoffset, GLint yoffset, GLint zoffset,
3094 GLint x, GLint y, GLsizei width, GLsizei height )
3095 {
3096 struct gl_texture_object *texObj;
3097 struct gl_texture_image *texImage;
3098 GET_CURRENT_CONTEXT(ctx);
3099 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3100
3101 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
3102 _mesa_debug(ctx, "glCopyTexSubImage3D %s %d %d %d %d %d %d %d %d\n",
3103 _mesa_lookup_enum_by_nr(target),
3104 level, xoffset, yoffset, zoffset, x, y, width, height);
3105
3106 if (ctx->NewState & NEW_COPY_TEX_STATE)
3107 _mesa_update_state(ctx);
3108
3109 if (copytexsubimage_error_check1(ctx, 3, target, level))
3110 return;
3111
3112 texObj = _mesa_get_current_tex_object(ctx, target);
3113
3114 _mesa_lock_texture(ctx, texObj);
3115 {
3116 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
3117
3118 if (copytexsubimage_error_check2(ctx, 3, target, level, xoffset, yoffset,
3119 zoffset, width, height, texImage)) {
3120 /* error was recored */
3121 }
3122 else {
3123 /* If we have a border, xoffset=-1 is legal. Bias by border width */
3124 xoffset += texImage->Border;
3125 yoffset += texImage->Border;
3126 zoffset += texImage->Border;
3127
3128 if (_mesa_clip_copytexsubimage(ctx, &xoffset, &yoffset, &x, &y,
3129 &width, &height)) {
3130 ASSERT(ctx->Driver.CopyTexSubImage3D);
3131 ctx->Driver.CopyTexSubImage3D(ctx, target, level,
3132 xoffset, yoffset, zoffset,
3133 x, y, width, height);
3134
3135 check_gen_mipmap(ctx, target, texObj, level);
3136
3137 ctx->NewState |= _NEW_TEXTURE;
3138 }
3139 }
3140 }
3141 _mesa_unlock_texture(ctx, texObj);
3142 }
3143
3144
3145
3146
3147 /**********************************************************************/
3148 /****** Compressed Textures ******/
3149 /**********************************************************************/
3150
3151
3152 /**
3153 * Return expected size of a compressed texture.
3154 */
3155 static GLuint
3156 compressed_tex_size(GLsizei width, GLsizei height, GLsizei depth,
3157 GLenum glformat)
3158 {
3159 gl_format mesaFormat = _mesa_glenum_to_compressed_format(glformat);
3160 return _mesa_format_image_size(mesaFormat, width, height, depth);
3161 }
3162
3163
3164 /*
3165 * Return compressed texture block size, in pixels.
3166 */
3167 static void
3168 get_compressed_block_size(GLenum glformat, GLuint *bw, GLuint *bh)
3169 {
3170 gl_format mesaFormat = _mesa_glenum_to_compressed_format(glformat);
3171 _mesa_get_format_block_size(mesaFormat, bw, bh);
3172 }
3173
3174
3175 /**
3176 * Error checking for glCompressedTexImage[123]D().
3177 * \return error code or GL_NO_ERROR.
3178 */
3179 static GLenum
3180 compressed_texture_error_check(struct gl_context *ctx, GLint dimensions,
3181 GLenum target, GLint level,
3182 GLenum internalFormat, GLsizei width,
3183 GLsizei height, GLsizei depth, GLint border,
3184 GLsizei imageSize)
3185 {
3186 GLint expectedSize, maxLevels = 0, maxTextureSize;
3187
3188 if (dimensions == 1) {
3189 /* 1D compressed textures not allowed */
3190 return GL_INVALID_ENUM;
3191 }
3192 else if (dimensions == 2) {
3193 if (target == GL_PROXY_TEXTURE_2D) {
3194 maxLevels = ctx->Const.MaxTextureLevels;
3195 }
3196 else if (target == GL_TEXTURE_2D) {
3197 maxLevels = ctx->Const.MaxTextureLevels;
3198 }
3199 else if (target == GL_PROXY_TEXTURE_CUBE_MAP_ARB) {
3200 if (!ctx->Extensions.ARB_texture_cube_map)
3201 return GL_INVALID_ENUM; /*target*/
3202 maxLevels = ctx->Const.MaxCubeTextureLevels;
3203 }
3204 else if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
3205 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB) {
3206 if (!ctx->Extensions.ARB_texture_cube_map)
3207 return GL_INVALID_ENUM; /*target*/
3208 maxLevels = ctx->Const.MaxCubeTextureLevels;
3209 }
3210 else {
3211 return GL_INVALID_ENUM; /*target*/
3212 }
3213 }
3214 else if (dimensions == 3) {
3215 /* 3D compressed textures not allowed */
3216 return GL_INVALID_ENUM;
3217 }
3218 else {
3219 assert(0);
3220 return GL_INVALID_ENUM;
3221 }
3222
3223 maxTextureSize = 1 << (maxLevels - 1);
3224
3225 /* This will detect any invalid internalFormat value */
3226 if (!_mesa_is_compressed_format(ctx, internalFormat))
3227 return GL_INVALID_ENUM;
3228
3229 /* This should really never fail */
3230 if (_mesa_base_tex_format(ctx, internalFormat) < 0)
3231 return GL_INVALID_ENUM;
3232
3233 if (border != 0)
3234 return GL_INVALID_VALUE;
3235
3236 /*
3237 * XXX We should probably use the proxy texture error check function here.
3238 */
3239 if (width < 1 || width > maxTextureSize ||
3240 (!ctx->Extensions.ARB_texture_non_power_of_two && !_mesa_is_pow_two(width)))
3241 return GL_INVALID_VALUE;
3242
3243 if ((height < 1 || height > maxTextureSize ||
3244 (!ctx->Extensions.ARB_texture_non_power_of_two && !_mesa_is_pow_two(height)))
3245 && dimensions > 1)
3246 return GL_INVALID_VALUE;
3247
3248 if ((depth < 1 || depth > maxTextureSize ||
3249 (!ctx->Extensions.ARB_texture_non_power_of_two && !_mesa_is_pow_two(depth)))
3250 && dimensions > 2)
3251 return GL_INVALID_VALUE;
3252
3253 /* For cube map, width must equal height */
3254 if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
3255 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB && width != height)
3256 return GL_INVALID_VALUE;
3257
3258 if (level < 0 || level >= maxLevels)
3259 return GL_INVALID_VALUE;
3260
3261 expectedSize = compressed_tex_size(width, height, depth, internalFormat);
3262 if (expectedSize != imageSize)
3263 return GL_INVALID_VALUE;
3264
3265 #if FEATURE_EXT_texture_sRGB
3266 if ((internalFormat == GL_COMPRESSED_SRGB_S3TC_DXT1_EXT ||
3267 internalFormat == GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT ||
3268 internalFormat == GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT ||
3269 internalFormat == GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT)
3270 && border != 0) {
3271 return GL_INVALID_OPERATION;
3272 }
3273 #endif
3274
3275 return GL_NO_ERROR;
3276 }
3277
3278
3279 /**
3280 * Error checking for glCompressedTexSubImage[123]D().
3281 * \warning There are some bad assumptions here about the size of compressed
3282 * texture tiles (multiple of 4) used to test the validity of the
3283 * offset and size parameters.
3284 * \return error code or GL_NO_ERROR.
3285 */
3286 static GLenum
3287 compressed_subtexture_error_check(struct gl_context *ctx, GLint dimensions,
3288 GLenum target, GLint level,
3289 GLint xoffset, GLint yoffset, GLint zoffset,
3290 GLsizei width, GLsizei height, GLsizei depth,
3291 GLenum format, GLsizei imageSize)
3292 {
3293 GLint expectedSize, maxLevels = 0, maxTextureSize;
3294 GLuint bw, bh;
3295 (void) zoffset;
3296
3297 if (dimensions == 1) {
3298 /* 1D compressed textures not allowed */
3299 return GL_INVALID_ENUM;
3300 }
3301 else if (dimensions == 2) {
3302 if (target == GL_PROXY_TEXTURE_2D) {
3303 maxLevels = ctx->Const.MaxTextureLevels;
3304 }
3305 else if (target == GL_TEXTURE_2D) {
3306 maxLevels = ctx->Const.MaxTextureLevels;
3307 }
3308 else if (target == GL_PROXY_TEXTURE_CUBE_MAP_ARB) {
3309 if (!ctx->Extensions.ARB_texture_cube_map)
3310 return GL_INVALID_ENUM; /*target*/
3311 maxLevels = ctx->Const.MaxCubeTextureLevels;
3312 }
3313 else if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
3314 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB) {
3315 if (!ctx->Extensions.ARB_texture_cube_map)
3316 return GL_INVALID_ENUM; /*target*/
3317 maxLevels = ctx->Const.MaxCubeTextureLevels;
3318 }
3319 else {
3320 return GL_INVALID_ENUM; /*target*/
3321 }
3322 }
3323 else if (dimensions == 3) {
3324 /* 3D compressed textures not allowed */
3325 return GL_INVALID_ENUM;
3326 }
3327
3328 maxTextureSize = 1 << (maxLevels - 1);
3329
3330 /* this will catch any invalid compressed format token */
3331 if (!_mesa_is_compressed_format(ctx, format))
3332 return GL_INVALID_ENUM;
3333
3334 if (width < 1 || width > maxTextureSize)
3335 return GL_INVALID_VALUE;
3336
3337 if ((height < 1 || height > maxTextureSize)
3338 && dimensions > 1)
3339 return GL_INVALID_VALUE;
3340
3341 if (level < 0 || level >= maxLevels)
3342 return GL_INVALID_VALUE;
3343
3344 /*
3345 * do checks which depend on compression block size
3346 */
3347 get_compressed_block_size(format, &bw, &bh);
3348
3349 if ((xoffset % bw != 0) || (yoffset % bh != 0))
3350 return GL_INVALID_VALUE;
3351
3352 if ((width % bw != 0) && width != 2 && width != 1)
3353 return GL_INVALID_VALUE;
3354
3355 if ((height % bh != 0) && height != 2 && height != 1)
3356 return GL_INVALID_VALUE;
3357
3358 expectedSize = compressed_tex_size(width, height, depth, format);
3359 if (expectedSize != imageSize)
3360 return GL_INVALID_VALUE;
3361
3362 return GL_NO_ERROR;
3363 }
3364
3365
3366 /**
3367 * Do second part of glCompressedTexSubImage error checking.
3368 * \return GL_TRUE if error found, GL_FALSE otherwise.
3369 */
3370 static GLboolean
3371 compressed_subtexture_error_check2(struct gl_context *ctx, GLuint dims,
3372 GLsizei width, GLsizei height,
3373 GLsizei depth, GLenum format,
3374 struct gl_texture_image *texImage)
3375 {
3376
3377 if ((GLint) format != texImage->InternalFormat) {
3378 _mesa_error(ctx, GL_INVALID_OPERATION,
3379 "glCompressedTexSubImage%uD(format=0x%x)", dims, format);
3380 return GL_TRUE;
3381 }
3382
3383 if (((width == 1 || width == 2) &&
3384 width != (GLsizei) texImage->Width) ||
3385 (width > (GLsizei) texImage->Width)) {
3386 _mesa_error(ctx, GL_INVALID_VALUE,
3387 "glCompressedTexSubImage%uD(width=%d)", dims, width);
3388 return GL_TRUE;
3389 }
3390
3391 if (dims >= 2) {
3392 if (((height == 1 || height == 2) &&
3393 height != (GLsizei) texImage->Height) ||
3394 (height > (GLsizei) texImage->Height)) {
3395 _mesa_error(ctx, GL_INVALID_VALUE,
3396 "glCompressedTexSubImage%uD(height=%d)", dims, height);
3397 return GL_TRUE;
3398 }
3399 }
3400
3401 if (dims >= 3) {
3402 if (((depth == 1 || depth == 2) &&
3403 depth != (GLsizei) texImage->Depth) ||
3404 (depth > (GLsizei) texImage->Depth)) {
3405 _mesa_error(ctx, GL_INVALID_VALUE,
3406 "glCompressedTexSubImage%uD(depth=%d)", dims, depth);
3407 return GL_TRUE;
3408 }
3409 }
3410
3411 return GL_FALSE;
3412 }
3413
3414
3415
3416 void GLAPIENTRY
3417 _mesa_CompressedTexImage1DARB(GLenum target, GLint level,
3418 GLenum internalFormat, GLsizei width,
3419 GLint border, GLsizei imageSize,
3420 const GLvoid *data)
3421 {
3422 GET_CURRENT_CONTEXT(ctx);
3423 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3424
3425 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
3426 _mesa_debug(ctx, "glCompressedTexImage1DARB %s %d %s %d %d %d %p\n",
3427 _mesa_lookup_enum_by_nr(target), level,
3428 _mesa_lookup_enum_by_nr(internalFormat),
3429 width, border, imageSize, data);
3430
3431 if (target == GL_TEXTURE_1D) {
3432 /* non-proxy target */
3433 struct gl_texture_object *texObj;
3434 struct gl_texture_image *texImage;
3435 GLenum error = compressed_texture_error_check(ctx, 1, target, level,
3436 internalFormat, width, 1, 1, border, imageSize);
3437 if (error) {
3438 _mesa_error(ctx, error, "glCompressedTexImage1D");
3439 return;
3440 }
3441
3442 texObj = _mesa_get_current_tex_object(ctx, target);
3443
3444 _mesa_lock_texture(ctx, texObj);
3445 {
3446 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
3447 if (!texImage) {
3448 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCompressedTexImage1D");
3449 }
3450 else {
3451 if (texImage->Data) {
3452 ctx->Driver.FreeTexImageData( ctx, texImage );
3453 }
3454 ASSERT(texImage->Data == NULL);
3455
3456 _mesa_init_teximage_fields(ctx, target, texImage, width, 1, 1,
3457 border, internalFormat);
3458
3459 _mesa_choose_texture_format(ctx, texObj, texImage, target, level,
3460 internalFormat, GL_NONE, GL_NONE);
3461
3462 ASSERT(ctx->Driver.CompressedTexImage1D);
3463 ctx->Driver.CompressedTexImage1D(ctx, target, level,
3464 internalFormat, width, border,
3465 imageSize, data,
3466 texObj, texImage);
3467
3468 _mesa_set_fetch_functions(texImage, 1);
3469
3470 check_gen_mipmap(ctx, target, texObj, level);
3471
3472 /* state update */
3473 texObj->_Complete = GL_FALSE;
3474 ctx->NewState |= _NEW_TEXTURE;
3475 }
3476 }
3477 _mesa_unlock_texture(ctx, texObj);
3478 }
3479 else if (target == GL_PROXY_TEXTURE_1D) {
3480 /* Proxy texture: check for errors and update proxy state */
3481 GLenum error = compressed_texture_error_check(ctx, 1, target, level,
3482 internalFormat, width, 1, 1, border, imageSize);
3483 if (!error) {
3484 ASSERT(ctx->Driver.TestProxyTexImage);
3485 error = !(*ctx->Driver.TestProxyTexImage)(ctx, target, level,
3486 internalFormat, GL_NONE, GL_NONE,
3487 width, 1, 1, border);
3488 }
3489 if (error) {
3490 /* if error, clear all proxy texture image parameters */
3491 struct gl_texture_image *texImage;
3492 texImage = _mesa_get_proxy_tex_image(ctx, target, level);
3493 if (texImage)
3494 clear_teximage_fields(texImage);
3495 }
3496 else {
3497 /* store the teximage parameters */
3498 struct gl_texture_object *texObj;
3499 struct gl_texture_image *texImage;
3500
3501 texObj = _mesa_get_current_tex_object(ctx, target);
3502
3503 _mesa_lock_texture(ctx, texObj);
3504 {
3505 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
3506 _mesa_init_teximage_fields(ctx, target, texImage, width, 1, 1,
3507 border, internalFormat);
3508 _mesa_choose_texture_format(ctx, texObj, texImage, target, level,
3509 internalFormat, GL_NONE, GL_NONE);
3510 }
3511 _mesa_unlock_texture(ctx, texObj);
3512 }
3513 }
3514 else {
3515 _mesa_error(ctx, GL_INVALID_ENUM, "glCompressedTexImage1D(target)");
3516 return;
3517 }
3518 }
3519
3520 void GLAPIENTRY
3521 _mesa_CompressedTexImage2DARB(GLenum target, GLint level,
3522 GLenum internalFormat, GLsizei width,
3523 GLsizei height, GLint border, GLsizei imageSize,
3524 const GLvoid *data)
3525 {
3526 GET_CURRENT_CONTEXT(ctx);
3527 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3528
3529 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
3530 _mesa_debug(ctx, "glCompressedTexImage2DARB %s %d %s %d %d %d %d %p\n",
3531 _mesa_lookup_enum_by_nr(target), level,
3532 _mesa_lookup_enum_by_nr(internalFormat),
3533 width, height, border, imageSize, data);
3534
3535 #if FEATURE_ES
3536 switch (internalFormat) {
3537 case GL_PALETTE4_RGB8_OES:
3538 case GL_PALETTE4_RGBA8_OES:
3539 case GL_PALETTE4_R5_G6_B5_OES:
3540 case GL_PALETTE4_RGBA4_OES:
3541 case GL_PALETTE4_RGB5_A1_OES:
3542 case GL_PALETTE8_RGB8_OES:
3543 case GL_PALETTE8_RGBA8_OES:
3544 case GL_PALETTE8_R5_G6_B5_OES:
3545 case GL_PALETTE8_RGBA4_OES:
3546 case GL_PALETTE8_RGB5_A1_OES:
3547 _mesa_cpal_compressed_teximage2d(target, level, internalFormat,
3548 width, height, imageSize, data);
3549 return;
3550 }
3551 #endif
3552
3553 if (target == GL_TEXTURE_2D ||
3554 (ctx->Extensions.ARB_texture_cube_map &&
3555 target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
3556 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB)) {
3557 /* non-proxy target */
3558 struct gl_texture_object *texObj;
3559 struct gl_texture_image *texImage;
3560
3561 GLenum error = compressed_texture_error_check(ctx, 2, target, level,
3562 internalFormat, width, height, 1, border, imageSize);
3563 if (error) {
3564 _mesa_error(ctx, error, "glCompressedTexImage2D");
3565 return;
3566 }
3567
3568 texObj = _mesa_get_current_tex_object(ctx, target);
3569
3570 _mesa_lock_texture(ctx, texObj);
3571 {
3572 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
3573 if (!texImage) {
3574 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCompressedTexImage2D");
3575 }
3576 else {
3577 if (texImage->Data) {
3578 ctx->Driver.FreeTexImageData( ctx, texImage );
3579 }
3580 ASSERT(texImage->Data == NULL);
3581
3582 _mesa_init_teximage_fields(ctx, target, texImage, width, height, 1,
3583 border, internalFormat);
3584
3585 _mesa_choose_texture_format(ctx, texObj, texImage, target, level,
3586 internalFormat, GL_NONE, GL_NONE);
3587
3588 ASSERT(ctx->Driver.CompressedTexImage2D);
3589 ctx->Driver.CompressedTexImage2D(ctx, target, level,
3590 internalFormat, width, height,
3591 border, imageSize, data,
3592 texObj, texImage);
3593
3594 _mesa_set_fetch_functions(texImage, 2);
3595
3596 check_gen_mipmap(ctx, target, texObj, level);
3597
3598 /* state update */
3599 texObj->_Complete = GL_FALSE;
3600 ctx->NewState |= _NEW_TEXTURE;
3601 }
3602 }
3603 _mesa_unlock_texture(ctx, texObj);
3604 }
3605 else if (target == GL_PROXY_TEXTURE_2D ||
3606 (target == GL_PROXY_TEXTURE_CUBE_MAP_ARB &&
3607 ctx->Extensions.ARB_texture_cube_map)) {
3608 /* Proxy texture: check for errors and update proxy state */
3609 GLenum error = compressed_texture_error_check(ctx, 2, target, level,
3610 internalFormat, width, height, 1, border, imageSize);
3611 if (!error) {
3612 ASSERT(ctx->Driver.TestProxyTexImage);
3613 error = !(*ctx->Driver.TestProxyTexImage)(ctx, target, level,
3614 internalFormat, GL_NONE, GL_NONE,
3615 width, height, 1, border);
3616 }
3617 if (error) {
3618 /* if error, clear all proxy texture image parameters */
3619 struct gl_texture_image *texImage;
3620 texImage = _mesa_get_proxy_tex_image(ctx, target, level);
3621 if (texImage)
3622 clear_teximage_fields(texImage);
3623 }
3624 else {
3625 /* store the teximage parameters */
3626 struct gl_texture_object *texObj;
3627 struct gl_texture_image *texImage;
3628
3629 texObj = _mesa_get_current_tex_object(ctx, target);
3630
3631 _mesa_lock_texture(ctx, texObj);
3632 {
3633 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
3634 _mesa_init_teximage_fields(ctx, target, texImage, width, height, 1,
3635 border, internalFormat);
3636 _mesa_choose_texture_format(ctx, texObj, texImage, target, level,
3637 internalFormat, GL_NONE, GL_NONE);
3638 }
3639 _mesa_unlock_texture(ctx, texObj);
3640 }
3641 }
3642 else {
3643 _mesa_error(ctx, GL_INVALID_ENUM, "glCompressedTexImage2D(target)");
3644 return;
3645 }
3646 }
3647
3648
3649 void GLAPIENTRY
3650 _mesa_CompressedTexImage3DARB(GLenum target, GLint level,
3651 GLenum internalFormat, GLsizei width,
3652 GLsizei height, GLsizei depth, GLint border,
3653 GLsizei imageSize, const GLvoid *data)
3654 {
3655 GET_CURRENT_CONTEXT(ctx);
3656 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3657
3658 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
3659 _mesa_debug(ctx, "glCompressedTexImage3DARB %s %d %s %d %d %d %d %d %p\n",
3660 _mesa_lookup_enum_by_nr(target), level,
3661 _mesa_lookup_enum_by_nr(internalFormat),
3662 width, height, depth, border, imageSize, data);
3663
3664 if (target == GL_TEXTURE_3D) {
3665 /* non-proxy target */
3666 struct gl_texture_object *texObj;
3667 struct gl_texture_image *texImage;
3668 GLenum error = compressed_texture_error_check(ctx, 3, target, level,
3669 internalFormat, width, height, depth, border, imageSize);
3670 if (error) {
3671 _mesa_error(ctx, error, "glCompressedTexImage3D");
3672 return;
3673 }
3674
3675 texObj = _mesa_get_current_tex_object(ctx, target);
3676
3677 _mesa_lock_texture(ctx, texObj);
3678 {
3679 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
3680 if (!texImage) {
3681 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCompressedTexImage3D");
3682 }
3683 else {
3684 if (texImage->Data) {
3685 ctx->Driver.FreeTexImageData( ctx, texImage );
3686 }
3687 ASSERT(texImage->Data == NULL);
3688
3689 _mesa_init_teximage_fields(ctx, target, texImage,
3690 width, height, depth,
3691 border, internalFormat);
3692
3693 /* Choose actual texture format */
3694 _mesa_choose_texture_format(ctx, texObj, texImage, target, level,
3695 internalFormat, GL_NONE, GL_NONE);
3696
3697 ASSERT(ctx->Driver.CompressedTexImage3D);
3698 ctx->Driver.CompressedTexImage3D(ctx, target, level,
3699 internalFormat,
3700 width, height, depth,
3701 border, imageSize, data,
3702 texObj, texImage);
3703
3704 _mesa_set_fetch_functions(texImage, 3);
3705
3706 check_gen_mipmap(ctx, target, texObj, level);
3707
3708 /* state update */
3709 texObj->_Complete = GL_FALSE;
3710 ctx->NewState |= _NEW_TEXTURE;
3711 }
3712 }
3713 _mesa_unlock_texture(ctx, texObj);
3714 }
3715 else if (target == GL_PROXY_TEXTURE_3D) {
3716 /* Proxy texture: check for errors and update proxy state */
3717 GLenum error = compressed_texture_error_check(ctx, 3, target, level,
3718 internalFormat, width, height, depth, border, imageSize);
3719 if (!error) {
3720 ASSERT(ctx->Driver.TestProxyTexImage);
3721 error = !(*ctx->Driver.TestProxyTexImage)(ctx, target, level,
3722 internalFormat, GL_NONE, GL_NONE,
3723 width, height, depth, border);
3724 }
3725 if (error) {
3726 /* if error, clear all proxy texture image parameters */
3727 struct gl_texture_image *texImage;
3728 texImage = _mesa_get_proxy_tex_image(ctx, target, level);
3729 if (texImage)
3730 clear_teximage_fields(texImage);
3731 }
3732 else {
3733 /* store the teximage parameters */
3734 struct gl_texture_object *texObj;
3735 struct gl_texture_image *texImage;
3736
3737 texObj = _mesa_get_current_tex_object(ctx, target);
3738
3739 _mesa_lock_texture(ctx, texObj);
3740 {
3741 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
3742 _mesa_init_teximage_fields(ctx, target, texImage, width, height,
3743 depth, border, internalFormat);
3744 _mesa_choose_texture_format(ctx, texObj, texImage, target, level,
3745 internalFormat, GL_NONE, GL_NONE);
3746 }
3747 _mesa_unlock_texture(ctx, texObj);
3748 }
3749 }
3750 else {
3751 _mesa_error(ctx, GL_INVALID_ENUM, "glCompressedTexImage3D(target)");
3752 return;
3753 }
3754 }
3755
3756
3757 /**
3758 * Common helper for glCompressedTexSubImage1/2/3D().
3759 */
3760 static void
3761 compressed_tex_sub_image(GLuint dims, GLenum target, GLint level,
3762 GLint xoffset, GLint yoffset, GLint zoffset,
3763 GLsizei width, GLsizei height, GLsizei depth,
3764 GLenum format, GLsizei imageSize, const GLvoid *data)
3765 {
3766 struct gl_texture_object *texObj;
3767 struct gl_texture_image *texImage;
3768 GLenum error;
3769 GET_CURRENT_CONTEXT(ctx);
3770 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3771
3772 error = compressed_subtexture_error_check(ctx, dims, target, level,
3773 xoffset, 0, 0, /* pos */
3774 width, height, depth, /* size */
3775 format, imageSize);
3776 if (error) {
3777 _mesa_error(ctx, error, "glCompressedTexSubImage%uD", dims);
3778 return;
3779 }
3780
3781 texObj = _mesa_get_current_tex_object(ctx, target);
3782
3783 _mesa_lock_texture(ctx, texObj);
3784 {
3785 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
3786 assert(texImage);
3787
3788 if (compressed_subtexture_error_check2(ctx, dims, width, height, depth,
3789 format, texImage)) {
3790 /* error was recorded */
3791 }
3792 else if (width > 0 && height > 0 && depth > 0) {
3793 switch (dims) {
3794 case 1:
3795 if (ctx->Driver.CompressedTexSubImage1D) {
3796 ctx->Driver.CompressedTexSubImage1D(ctx, target, level,
3797 xoffset, width,
3798 format, imageSize, data,
3799 texObj, texImage);
3800 }
3801 break;
3802 case 2:
3803 if (ctx->Driver.CompressedTexSubImage2D) {
3804 ctx->Driver.CompressedTexSubImage2D(ctx, target, level,
3805 xoffset, yoffset,
3806 width, height,
3807 format, imageSize, data,
3808 texObj, texImage);
3809 }
3810 break;
3811 case 3:
3812 if (ctx->Driver.CompressedTexSubImage3D) {
3813 ctx->Driver.CompressedTexSubImage3D(ctx, target, level,
3814 xoffset, yoffset, zoffset,
3815 width, height, depth,
3816 format, imageSize, data,
3817 texObj, texImage);
3818 }
3819 break;
3820 default:
3821 ;
3822 }
3823
3824 check_gen_mipmap(ctx, target, texObj, level);
3825
3826 ctx->NewState |= _NEW_TEXTURE;
3827 }
3828 }
3829 _mesa_unlock_texture(ctx, texObj);
3830 }
3831
3832
3833 void GLAPIENTRY
3834 _mesa_CompressedTexSubImage1DARB(GLenum target, GLint level, GLint xoffset,
3835 GLsizei width, GLenum format,
3836 GLsizei imageSize, const GLvoid *data)
3837 {
3838 compressed_tex_sub_image(1, target, level, xoffset, 0, 0, width, 1, 1,
3839 format, imageSize, data);
3840 }
3841
3842
3843 void GLAPIENTRY
3844 _mesa_CompressedTexSubImage2DARB(GLenum target, GLint level, GLint xoffset,
3845 GLint yoffset, GLsizei width, GLsizei height,
3846 GLenum format, GLsizei imageSize,
3847 const GLvoid *data)
3848 {
3849 compressed_tex_sub_image(2, target, level, xoffset, yoffset, 0,
3850 width, height, 1, format, imageSize, data);
3851 }
3852
3853
3854 void GLAPIENTRY
3855 _mesa_CompressedTexSubImage3DARB(GLenum target, GLint level, GLint xoffset,
3856 GLint yoffset, GLint zoffset, GLsizei width,
3857 GLsizei height, GLsizei depth, GLenum format,
3858 GLsizei imageSize, const GLvoid *data)
3859 {
3860 compressed_tex_sub_image(3, target, level, xoffset, yoffset, zoffset,
3861 width, height, depth, format, imageSize, data);
3862 }
3863
3864