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