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