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