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