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