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