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