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