mesa: remove remaining FEATURE_* defines where protected by API check.
[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 * This is the fallback for Driver.TestProxyTexImage(). Test the texture
1213 * level, width, height and depth against the ctx->Const limits for textures.
1214 *
1215 * A hardware driver might override this function if, for example, the
1216 * max 3D texture size is 512x512x64 (i.e. not a cube).
1217 *
1218 * Note that width, height, depth == 0 is not an error. However, a
1219 * texture with zero width/height/depth will be considered "incomplete"
1220 * and texturing will effectively be disabled.
1221 *
1222 * \param target one of GL_PROXY_TEXTURE_1D, GL_PROXY_TEXTURE_2D,
1223 * GL_PROXY_TEXTURE_3D, GL_PROXY_TEXTURE_RECTANGLE_NV,
1224 * GL_PROXY_TEXTURE_CUBE_MAP_ARB.
1225 * \param level as passed to glTexImage
1226 * \param internalFormat as passed to glTexImage
1227 * \param format as passed to glTexImage
1228 * \param type as passed to glTexImage
1229 * \param width as passed to glTexImage
1230 * \param height as passed to glTexImage
1231 * \param depth as passed to glTexImage
1232 * \param border as passed to glTexImage
1233 * \return GL_TRUE if the image is acceptable, GL_FALSE if not acceptable.
1234 */
1235 GLboolean
1236 _mesa_test_proxy_teximage(struct gl_context *ctx, GLenum target, GLint level,
1237 GLint internalFormat, GLenum format, GLenum type,
1238 GLint width, GLint height, GLint depth, GLint border)
1239 {
1240 GLint maxSize;
1241
1242 (void) internalFormat;
1243 (void) format;
1244 (void) type;
1245
1246 switch (target) {
1247 case GL_PROXY_TEXTURE_1D:
1248 if (level >= ctx->Const.MaxTextureLevels)
1249 return GL_FALSE;
1250 maxSize = 1 << (ctx->Const.MaxTextureLevels - 1); /* level zero size */
1251 maxSize >>= level; /* level size */
1252 if (width < 2 * border || width > 2 * border + maxSize)
1253 return GL_FALSE;
1254 if (!ctx->Extensions.ARB_texture_non_power_of_two) {
1255 if (width > 0 && !_mesa_is_pow_two(width - 2 * border))
1256 return GL_FALSE;
1257 }
1258 return GL_TRUE;
1259
1260 case GL_PROXY_TEXTURE_2D:
1261 if (level >= ctx->Const.MaxTextureLevels)
1262 return GL_FALSE;
1263 maxSize = 1 << (ctx->Const.MaxTextureLevels - 1);
1264 maxSize >>= level;
1265 if (width < 2 * border || width > 2 * border + maxSize)
1266 return GL_FALSE;
1267 if (height < 2 * border || height > 2 * border + maxSize)
1268 return GL_FALSE;
1269 if (!ctx->Extensions.ARB_texture_non_power_of_two) {
1270 if (width > 0 && !_mesa_is_pow_two(width - 2 * border))
1271 return GL_FALSE;
1272 if (height > 0 && !_mesa_is_pow_two(height - 2 * border))
1273 return GL_FALSE;
1274 }
1275 return GL_TRUE;
1276
1277 case GL_PROXY_TEXTURE_3D:
1278 if (level >= ctx->Const.Max3DTextureLevels)
1279 return GL_FALSE;
1280 maxSize = 1 << (ctx->Const.Max3DTextureLevels - 1);
1281 maxSize >>= level;
1282 if (width < 2 * border || width > 2 * border + maxSize)
1283 return GL_FALSE;
1284 if (height < 2 * border || height > 2 * border + maxSize)
1285 return GL_FALSE;
1286 if (depth < 2 * border || depth > 2 * border + maxSize)
1287 return GL_FALSE;
1288 if (!ctx->Extensions.ARB_texture_non_power_of_two) {
1289 if (width > 0 && !_mesa_is_pow_two(width - 2 * border))
1290 return GL_FALSE;
1291 if (height > 0 && !_mesa_is_pow_two(height - 2 * border))
1292 return GL_FALSE;
1293 if (depth > 0 && !_mesa_is_pow_two(depth - 2 * border))
1294 return GL_FALSE;
1295 }
1296 return GL_TRUE;
1297
1298 case GL_PROXY_TEXTURE_RECTANGLE_NV:
1299 if (level != 0)
1300 return GL_FALSE;
1301 maxSize = ctx->Const.MaxTextureRectSize;
1302 if (width < 0 || width > maxSize)
1303 return GL_FALSE;
1304 if (height < 0 || height > maxSize)
1305 return GL_FALSE;
1306 return GL_TRUE;
1307
1308 case GL_PROXY_TEXTURE_CUBE_MAP_ARB:
1309 if (level >= ctx->Const.MaxCubeTextureLevels)
1310 return GL_FALSE;
1311 maxSize = 1 << (ctx->Const.MaxCubeTextureLevels - 1);
1312 maxSize >>= level;
1313 if (width < 2 * border || width > 2 * border + maxSize)
1314 return GL_FALSE;
1315 if (height < 2 * border || height > 2 * border + maxSize)
1316 return GL_FALSE;
1317 if (!ctx->Extensions.ARB_texture_non_power_of_two) {
1318 if (width > 0 && !_mesa_is_pow_two(width - 2 * border))
1319 return GL_FALSE;
1320 if (height > 0 && !_mesa_is_pow_two(height - 2 * border))
1321 return GL_FALSE;
1322 }
1323 return GL_TRUE;
1324
1325 case GL_PROXY_TEXTURE_1D_ARRAY_EXT:
1326 if (level >= ctx->Const.MaxTextureLevels)
1327 return GL_FALSE;
1328 maxSize = 1 << (ctx->Const.MaxTextureLevels - 1);
1329 maxSize >>= level;
1330 if (width < 2 * border || width > 2 * border + maxSize)
1331 return GL_FALSE;
1332 if (height < 1 || height > ctx->Const.MaxArrayTextureLayers)
1333 return GL_FALSE;
1334 if (!ctx->Extensions.ARB_texture_non_power_of_two) {
1335 if (width > 0 && !_mesa_is_pow_two(width - 2 * border))
1336 return GL_FALSE;
1337 }
1338 return GL_TRUE;
1339
1340 case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
1341 if (level >= ctx->Const.MaxTextureLevels)
1342 return GL_FALSE;
1343 maxSize = 1 << (ctx->Const.MaxTextureLevels - 1);
1344 maxSize >>= level;
1345 if (width < 2 * border || width > 2 * border + maxSize)
1346 return GL_FALSE;
1347 if (height < 2 * border || height > 2 * border + maxSize)
1348 return GL_FALSE;
1349 if (depth < 1 || depth > ctx->Const.MaxArrayTextureLayers)
1350 return GL_FALSE;
1351 if (!ctx->Extensions.ARB_texture_non_power_of_two) {
1352 if (width > 0 && !_mesa_is_pow_two(width - 2 * border))
1353 return GL_FALSE;
1354 if (height > 0 && !_mesa_is_pow_two(height - 2 * border))
1355 return GL_FALSE;
1356 }
1357 return GL_TRUE;
1358
1359 default:
1360 _mesa_problem(ctx, "Invalid target in _mesa_test_proxy_teximage");
1361 return GL_FALSE;
1362 }
1363 }
1364
1365
1366 /**
1367 * Check if the memory used by the texture would exceed the driver's limit.
1368 * This lets us support a max 3D texture size of 8K (for example) but
1369 * prevents allocating a full 8K x 8K x 8K texture.
1370 * XXX this could be rolled into the proxy texture size test (above) but
1371 * we don't have the actual texture internal format at that point.
1372 */
1373 static GLboolean
1374 legal_texture_size(struct gl_context *ctx, gl_format format,
1375 GLint width, GLint height, GLint depth)
1376 {
1377 uint64_t bytes = _mesa_format_image_size64(format, width, height, depth);
1378 uint64_t mbytes = bytes / (1024 * 1024); /* convert to MB */
1379 return mbytes <= (uint64_t) ctx->Const.MaxTextureMbytes;
1380 }
1381
1382
1383 /**
1384 * Return true if the format is only valid for glCompressedTexImage.
1385 */
1386 static GLboolean
1387 compressedteximage_only_format(const struct gl_context *ctx, GLenum format)
1388 {
1389 switch (format) {
1390 case GL_ETC1_RGB8_OES:
1391 case GL_PALETTE4_RGB8_OES:
1392 case GL_PALETTE4_RGBA8_OES:
1393 case GL_PALETTE4_R5_G6_B5_OES:
1394 case GL_PALETTE4_RGBA4_OES:
1395 case GL_PALETTE4_RGB5_A1_OES:
1396 case GL_PALETTE8_RGB8_OES:
1397 case GL_PALETTE8_RGBA8_OES:
1398 case GL_PALETTE8_R5_G6_B5_OES:
1399 case GL_PALETTE8_RGBA4_OES:
1400 case GL_PALETTE8_RGB5_A1_OES:
1401 return GL_TRUE;
1402 default:
1403 return GL_FALSE;
1404 }
1405 }
1406
1407
1408 /**
1409 * Helper function to determine whether a target and specific compression
1410 * format are supported.
1411 */
1412 static GLboolean
1413 target_can_be_compressed(const struct gl_context *ctx, GLenum target,
1414 GLenum intFormat)
1415 {
1416 (void) intFormat; /* not used yet */
1417
1418 switch (target) {
1419 case GL_TEXTURE_2D:
1420 case GL_PROXY_TEXTURE_2D:
1421 return GL_TRUE; /* true for any compressed format so far */
1422 case GL_PROXY_TEXTURE_CUBE_MAP:
1423 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
1424 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
1425 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
1426 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
1427 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
1428 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
1429 return ctx->Extensions.ARB_texture_cube_map;
1430 case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
1431 case GL_TEXTURE_2D_ARRAY_EXT:
1432 return (ctx->Extensions.MESA_texture_array ||
1433 ctx->Extensions.EXT_texture_array);
1434 default:
1435 return GL_FALSE;
1436 }
1437 }
1438
1439
1440 /**
1441 * Check if the given texture target value is legal for a
1442 * glTexImage1/2/3D call.
1443 */
1444 static GLboolean
1445 legal_teximage_target(struct gl_context *ctx, GLuint dims, GLenum target)
1446 {
1447 switch (dims) {
1448 case 1:
1449 switch (target) {
1450 case GL_TEXTURE_1D:
1451 case GL_PROXY_TEXTURE_1D:
1452 return _mesa_is_desktop_gl(ctx);
1453 default:
1454 return GL_FALSE;
1455 }
1456 case 2:
1457 switch (target) {
1458 case GL_TEXTURE_2D:
1459 return GL_TRUE;
1460 case GL_PROXY_TEXTURE_2D:
1461 return _mesa_is_desktop_gl(ctx);
1462 case GL_PROXY_TEXTURE_CUBE_MAP:
1463 return _mesa_is_desktop_gl(ctx)
1464 && ctx->Extensions.ARB_texture_cube_map;
1465 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
1466 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
1467 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
1468 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
1469 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
1470 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
1471 return ctx->Extensions.ARB_texture_cube_map;
1472 case GL_TEXTURE_RECTANGLE_NV:
1473 case GL_PROXY_TEXTURE_RECTANGLE_NV:
1474 return _mesa_is_desktop_gl(ctx)
1475 && ctx->Extensions.NV_texture_rectangle;
1476 case GL_TEXTURE_1D_ARRAY_EXT:
1477 case GL_PROXY_TEXTURE_1D_ARRAY_EXT:
1478 return _mesa_is_desktop_gl(ctx)
1479 && (ctx->Extensions.MESA_texture_array ||
1480 ctx->Extensions.EXT_texture_array);
1481 default:
1482 return GL_FALSE;
1483 }
1484 case 3:
1485 switch (target) {
1486 case GL_TEXTURE_3D:
1487 return GL_TRUE;
1488 case GL_PROXY_TEXTURE_3D:
1489 return _mesa_is_desktop_gl(ctx);
1490 case GL_TEXTURE_2D_ARRAY_EXT:
1491 return (_mesa_is_desktop_gl(ctx)
1492 && (ctx->Extensions.MESA_texture_array ||
1493 ctx->Extensions.EXT_texture_array))
1494 || _mesa_is_gles3(ctx);
1495 case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
1496 return _mesa_is_desktop_gl(ctx)
1497 && (ctx->Extensions.MESA_texture_array ||
1498 ctx->Extensions.EXT_texture_array);
1499 default:
1500 return GL_FALSE;
1501 }
1502 default:
1503 _mesa_problem(ctx, "invalid dims=%u in legal_teximage_target()", dims);
1504 return GL_FALSE;
1505 }
1506 }
1507
1508
1509 /**
1510 * Check if the given texture target value is legal for a
1511 * glTexSubImage, glCopyTexSubImage or glCopyTexImage call.
1512 * The difference compared to legal_teximage_target() above is that
1513 * proxy targets are not supported.
1514 */
1515 static GLboolean
1516 legal_texsubimage_target(struct gl_context *ctx, GLuint dims, GLenum target)
1517 {
1518 switch (dims) {
1519 case 1:
1520 return _mesa_is_desktop_gl(ctx) && target == GL_TEXTURE_1D;
1521 case 2:
1522 switch (target) {
1523 case GL_TEXTURE_2D:
1524 return GL_TRUE;
1525 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
1526 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
1527 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
1528 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
1529 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
1530 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
1531 return ctx->Extensions.ARB_texture_cube_map;
1532 case GL_TEXTURE_RECTANGLE_NV:
1533 return _mesa_is_desktop_gl(ctx)
1534 && ctx->Extensions.NV_texture_rectangle;
1535 case GL_TEXTURE_1D_ARRAY_EXT:
1536 return _mesa_is_desktop_gl(ctx)
1537 && (ctx->Extensions.MESA_texture_array ||
1538 ctx->Extensions.EXT_texture_array);
1539 default:
1540 return GL_FALSE;
1541 }
1542 case 3:
1543 switch (target) {
1544 case GL_TEXTURE_3D:
1545 return GL_TRUE;
1546 case GL_TEXTURE_2D_ARRAY_EXT:
1547 return (_mesa_is_desktop_gl(ctx)
1548 && (ctx->Extensions.MESA_texture_array ||
1549 ctx->Extensions.EXT_texture_array))
1550 || _mesa_is_gles3(ctx);
1551 default:
1552 return GL_FALSE;
1553 }
1554 default:
1555 _mesa_problem(ctx, "invalid dims=%u in legal_texsubimage_target()",
1556 dims);
1557 return GL_FALSE;
1558 }
1559 }
1560
1561
1562 /**
1563 * Helper function to determine if a texture object is mutable (in terms
1564 * of GL_ARB_texture_storage).
1565 */
1566 static GLboolean
1567 mutable_tex_object(struct gl_context *ctx, GLenum target)
1568 {
1569 if (ctx->Extensions.ARB_texture_storage) {
1570 struct gl_texture_object *texObj =
1571 _mesa_get_current_tex_object(ctx, target);
1572 return !texObj->Immutable;
1573 }
1574 return GL_TRUE;
1575 }
1576
1577
1578 GLenum
1579 _mesa_es_error_check_format_and_type(GLenum format, GLenum type,
1580 unsigned dimensions)
1581 {
1582 bool type_valid = true;
1583
1584 switch (format) {
1585 case GL_ALPHA:
1586 case GL_LUMINANCE:
1587 case GL_LUMINANCE_ALPHA:
1588 type_valid = (type == GL_UNSIGNED_BYTE
1589 || type == GL_FLOAT
1590 || type == GL_HALF_FLOAT_OES);
1591 break;
1592
1593 case GL_RGB:
1594 type_valid = (type == GL_UNSIGNED_BYTE
1595 || type == GL_UNSIGNED_SHORT_5_6_5
1596 || type == GL_FLOAT
1597 || type == GL_HALF_FLOAT_OES);
1598 break;
1599
1600 case GL_RGBA:
1601 type_valid = (type == GL_UNSIGNED_BYTE
1602 || type == GL_UNSIGNED_SHORT_4_4_4_4
1603 || type == GL_UNSIGNED_SHORT_5_5_5_1
1604 || type == GL_FLOAT
1605 || type == GL_HALF_FLOAT_OES
1606 || type == GL_UNSIGNED_INT_2_10_10_10_REV);
1607 break;
1608
1609 case GL_DEPTH_COMPONENT:
1610 /* This format is filtered against invalid dimensionalities elsewhere.
1611 */
1612 type_valid = (type == GL_UNSIGNED_SHORT
1613 || type == GL_UNSIGNED_INT);
1614 break;
1615
1616 case GL_DEPTH_STENCIL:
1617 /* This format is filtered against invalid dimensionalities elsewhere.
1618 */
1619 type_valid = (type == GL_UNSIGNED_INT_24_8);
1620 break;
1621
1622 case GL_BGRA_EXT:
1623 type_valid = (type == GL_UNSIGNED_BYTE);
1624
1625 /* This feels like a bug in the EXT_texture_format_BGRA8888 spec, but
1626 * the format does not appear to be allowed for 3D textures in OpenGL
1627 * ES.
1628 */
1629 if (dimensions != 2)
1630 return GL_INVALID_VALUE;
1631
1632 break;
1633
1634 default:
1635 return GL_INVALID_VALUE;
1636 }
1637
1638 return type_valid ? GL_NO_ERROR : GL_INVALID_OPERATION;
1639 }
1640
1641
1642
1643 /**
1644 * Return expected size of a compressed texture.
1645 */
1646 static GLuint
1647 compressed_tex_size(GLsizei width, GLsizei height, GLsizei depth,
1648 GLenum glformat)
1649 {
1650 gl_format mesaFormat = _mesa_glenum_to_compressed_format(glformat);
1651 return _mesa_format_image_size(mesaFormat, width, height, depth);
1652 }
1653
1654
1655 /*
1656 * Return compressed texture block size, in pixels.
1657 */
1658 static void
1659 get_compressed_block_size(GLenum glformat, GLuint *bw, GLuint *bh)
1660 {
1661 gl_format mesaFormat = _mesa_glenum_to_compressed_format(glformat);
1662 _mesa_get_format_block_size(mesaFormat, bw, bh);
1663 }
1664
1665
1666 /**
1667 * Special value returned by error some texture error checking functions when
1668 * an error is detected and the proxy texture image's width/height/depth/format
1669 * fields should be zeroed-out.
1670 */
1671 #define PROXY_ERROR 2
1672
1673
1674 /**
1675 * Test the glTexImage[123]D() parameters for errors.
1676 *
1677 * \param ctx GL context.
1678 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1679 * \param target texture target given by the user.
1680 * \param level image level given by the user.
1681 * \param internalFormat internal format given by the user.
1682 * \param format pixel data format given by the user.
1683 * \param type pixel data type given by the user.
1684 * \param width image width given by the user.
1685 * \param height image height given by the user.
1686 * \param depth image depth given by the user.
1687 * \param border image border given by the user.
1688 *
1689 * \return PROXY_ERROR if there's an error that should zero-out the proxy image,
1690 * GL_TRUE if a regular GL error is found, or GL_FALSE if no error,
1691 *
1692 * Verifies each of the parameters against the constants specified in
1693 * __struct gl_contextRec::Const and the supported extensions, and according
1694 * to the OpenGL specification.
1695 */
1696 static GLenum
1697 texture_error_check( struct gl_context *ctx,
1698 GLuint dimensions, GLenum target,
1699 GLint level, GLint internalFormat,
1700 GLenum format, GLenum type,
1701 GLint width, GLint height,
1702 GLint depth, GLint border )
1703 {
1704 const GLenum proxyTarget = _mesa_get_proxy_target(target);
1705 const GLboolean isProxy = target == proxyTarget;
1706 GLboolean sizeOK = GL_TRUE;
1707 GLboolean colorFormat;
1708 GLenum err;
1709
1710 /* Even though there are no color-index textures, we still have to support
1711 * uploading color-index data and remapping it to RGB via the
1712 * GL_PIXEL_MAP_I_TO_[RGBA] tables.
1713 */
1714 const GLboolean indexFormat = (format == GL_COLOR_INDEX);
1715
1716 /* Note: for proxy textures, some error conditions immediately generate
1717 * a GL error in the usual way. But others do not generate a GL error.
1718 * Instead, they cause the width, height, depth, format fields of the
1719 * texture image to be zeroed-out. The GL spec seems to indicate that the
1720 * zero-out behaviour is only used in cases related to memory allocation.
1721 */
1722
1723 /* Basic level check (more checking in ctx->Driver.TestProxyTexImage) */
1724 if (level < 0 || level >= MAX_TEXTURE_LEVELS) {
1725 _mesa_error(ctx, GL_INVALID_VALUE,
1726 "glTexImage%dD(level=%d)", dimensions, level);
1727 return GL_TRUE;
1728 }
1729
1730 /* Check border */
1731 if (border < 0 || border > 1 ||
1732 ((ctx->API != API_OPENGL ||
1733 target == GL_TEXTURE_RECTANGLE_NV ||
1734 target == GL_PROXY_TEXTURE_RECTANGLE_NV) && border != 0)) {
1735 _mesa_error(ctx, GL_INVALID_VALUE,
1736 "glTexImage%dD(border=%d)", dimensions, border);
1737 return GL_TRUE;
1738 }
1739
1740 if (width < 0 || height < 0 || depth < 0) {
1741 _mesa_error(ctx, GL_INVALID_VALUE,
1742 "glTexImage%dD(width, height or depth < 0)", dimensions);
1743 return GL_TRUE;
1744 }
1745
1746 /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
1747 * combinations of format, internalFormat, and type that can be used.
1748 * Formats and types that require additional extensions (e.g., GL_FLOAT
1749 * requires GL_OES_texture_float) are filtered elsewhere.
1750 */
1751 if (_mesa_is_gles(ctx) && !_mesa_is_gles3(ctx)) {
1752 if (format != internalFormat) {
1753 _mesa_error(ctx, GL_INVALID_OPERATION,
1754 "glTexImage%dD(format = %s, internalFormat = %s)",
1755 dimensions,
1756 _mesa_lookup_enum_by_nr(format),
1757 _mesa_lookup_enum_by_nr(internalFormat));
1758 return GL_TRUE;
1759 }
1760
1761 err = _mesa_es_error_check_format_and_type(format, type, dimensions);
1762 if (err != GL_NO_ERROR) {
1763 _mesa_error(ctx, err,
1764 "glTexImage%dD(format = %s, type = %s)",
1765 dimensions,
1766 _mesa_lookup_enum_by_nr(format),
1767 _mesa_lookup_enum_by_nr(type));
1768 return GL_TRUE;
1769 }
1770 }
1771
1772 /* Do this simple check before calling the TestProxyTexImage() function */
1773 if (proxyTarget == GL_PROXY_TEXTURE_CUBE_MAP_ARB) {
1774 sizeOK = (width == height);
1775 }
1776
1777 /*
1778 * Use the proxy texture driver hook to see if the size/level/etc are
1779 * legal.
1780 */
1781 sizeOK = sizeOK && ctx->Driver.TestProxyTexImage(ctx, proxyTarget, level,
1782 internalFormat, format,
1783 type, width, height,
1784 depth, border);
1785 if (!sizeOK) {
1786 if (isProxy) {
1787 /* No GL error is recorded, but we need to zero-out the image dims */
1788 return PROXY_ERROR;
1789 }
1790 else {
1791 _mesa_error(ctx, GL_INVALID_VALUE,
1792 "glTexImage%dD(level=%d, width=%d, height=%d, depth=%d)",
1793 dimensions, level, width, height, depth);
1794 return GL_TRUE;
1795 }
1796 }
1797
1798 /* Check internalFormat */
1799 if (_mesa_base_tex_format(ctx, internalFormat) < 0) {
1800 _mesa_error(ctx, GL_INVALID_VALUE,
1801 "glTexImage%dD(internalFormat=%s)",
1802 dimensions, _mesa_lookup_enum_by_nr(internalFormat));
1803 return GL_TRUE;
1804 }
1805
1806 /* Check incoming image format and type */
1807 err = _mesa_error_check_format_and_type(ctx, format, type);
1808 if (err != GL_NO_ERROR) {
1809 _mesa_error(ctx, err,
1810 "glTexImage%dD(incompatible format 0x%x, type 0x%x)",
1811 dimensions, format, type);
1812 return GL_TRUE;
1813 }
1814
1815 /* make sure internal format and format basically agree */
1816 colorFormat = _mesa_is_color_format(format);
1817 if ((_mesa_is_color_format(internalFormat) && !colorFormat && !indexFormat) ||
1818 (_mesa_is_depth_format(internalFormat) != _mesa_is_depth_format(format)) ||
1819 (_mesa_is_ycbcr_format(internalFormat) != _mesa_is_ycbcr_format(format)) ||
1820 (_mesa_is_depthstencil_format(internalFormat) != _mesa_is_depthstencil_format(format)) ||
1821 (_mesa_is_dudv_format(internalFormat) != _mesa_is_dudv_format(format))) {
1822 _mesa_error(ctx, GL_INVALID_OPERATION,
1823 "glTexImage%dD(incompatible internalFormat 0x%x, format 0x%x)",
1824 dimensions, internalFormat, format);
1825 return GL_TRUE;
1826 }
1827
1828 /* additional checks for ycbcr textures */
1829 if (internalFormat == GL_YCBCR_MESA) {
1830 ASSERT(ctx->Extensions.MESA_ycbcr_texture);
1831 if (type != GL_UNSIGNED_SHORT_8_8_MESA &&
1832 type != GL_UNSIGNED_SHORT_8_8_REV_MESA) {
1833 char message[100];
1834 _mesa_snprintf(message, sizeof(message),
1835 "glTexImage%dD(format/type YCBCR mismatch)",
1836 dimensions);
1837 _mesa_error(ctx, GL_INVALID_ENUM, "%s", message);
1838 return GL_TRUE; /* error */
1839 }
1840 if (target != GL_TEXTURE_2D &&
1841 target != GL_PROXY_TEXTURE_2D &&
1842 target != GL_TEXTURE_RECTANGLE_NV &&
1843 target != GL_PROXY_TEXTURE_RECTANGLE_NV) {
1844 _mesa_error(ctx, GL_INVALID_ENUM,
1845 "glTexImage%dD(bad target for YCbCr texture)",
1846 dimensions);
1847 return GL_TRUE;
1848 }
1849 if (border != 0) {
1850 char message[100];
1851 _mesa_snprintf(message, sizeof(message),
1852 "glTexImage%dD(format=GL_YCBCR_MESA and border=%d)",
1853 dimensions, border);
1854 _mesa_error(ctx, GL_INVALID_VALUE, "%s", message);
1855 return GL_TRUE;
1856 }
1857 }
1858
1859 /* additional checks for depth textures */
1860 if (_mesa_base_tex_format(ctx, internalFormat) == GL_DEPTH_COMPONENT) {
1861 /* Only 1D, 2D, rect, array and cube textures supported, not 3D
1862 * Cubemaps are only supported for GL version > 3.0 or with EXT_gpu_shader4 */
1863 if (target != GL_TEXTURE_1D &&
1864 target != GL_PROXY_TEXTURE_1D &&
1865 target != GL_TEXTURE_2D &&
1866 target != GL_PROXY_TEXTURE_2D &&
1867 target != GL_TEXTURE_1D_ARRAY &&
1868 target != GL_PROXY_TEXTURE_1D_ARRAY &&
1869 target != GL_TEXTURE_2D_ARRAY &&
1870 target != GL_PROXY_TEXTURE_2D_ARRAY &&
1871 target != GL_TEXTURE_RECTANGLE_ARB &&
1872 target != GL_PROXY_TEXTURE_RECTANGLE_ARB &&
1873 !((_mesa_is_cube_face(target) || target == GL_PROXY_TEXTURE_CUBE_MAP) &&
1874 (ctx->Version >= 30 || ctx->Extensions.EXT_gpu_shader4))) {
1875 _mesa_error(ctx, GL_INVALID_ENUM,
1876 "glTexImage%dD(bad target for depth texture)",
1877 dimensions);
1878 return GL_TRUE;
1879 }
1880 }
1881
1882 /* additional checks for compressed textures */
1883 if (_mesa_is_compressed_format(ctx, internalFormat)) {
1884 if (!target_can_be_compressed(ctx, target, internalFormat)) {
1885 _mesa_error(ctx, GL_INVALID_ENUM,
1886 "glTexImage%dD(target can't be compressed)", dimensions);
1887 return GL_TRUE;
1888 }
1889 if (compressedteximage_only_format(ctx, internalFormat)) {
1890 _mesa_error(ctx, GL_INVALID_OPERATION,
1891 "glTexImage%dD(no compression for format)", dimensions);
1892 return GL_TRUE;
1893 }
1894 if (border != 0) {
1895 _mesa_error(ctx, GL_INVALID_OPERATION,
1896 "glTexImage%dD(border!=0)", dimensions);
1897 return GL_TRUE;
1898 }
1899 }
1900
1901 /* additional checks for integer textures */
1902 if ((ctx->Version >= 30 || ctx->Extensions.EXT_texture_integer) &&
1903 (_mesa_is_enum_format_integer(format) !=
1904 _mesa_is_enum_format_integer(internalFormat))) {
1905 _mesa_error(ctx, GL_INVALID_OPERATION,
1906 "glTexImage%dD(integer/non-integer format mismatch)",
1907 dimensions);
1908 return GL_TRUE;
1909 }
1910
1911 if (!mutable_tex_object(ctx, target)) {
1912 _mesa_error(ctx, GL_INVALID_OPERATION,
1913 "glTexImage%dD(immutable texture)", dimensions);
1914 return GL_TRUE;
1915 }
1916
1917 /* if we get here, the parameters are OK */
1918 return GL_FALSE;
1919 }
1920
1921
1922 /**
1923 * Error checking for glCompressedTexImage[123]D().
1924 * \param reason returns reason for error, if any
1925 * \return error code or GL_NO_ERROR or PROXY_ERROR.
1926 */
1927 static GLenum
1928 compressed_texture_error_check(struct gl_context *ctx, GLint dimensions,
1929 GLenum target, GLint level,
1930 GLenum internalFormat, GLsizei width,
1931 GLsizei height, GLsizei depth, GLint border,
1932 GLsizei imageSize)
1933 {
1934 const GLenum proxyTarget = _mesa_get_proxy_target(target);
1935 const GLint maxLevels = _mesa_max_texture_levels(ctx, target);
1936 GLint expectedSize;
1937 GLenum choose_format;
1938 GLenum choose_type;
1939 GLenum proxy_format;
1940 GLenum error = GL_NO_ERROR;
1941 char *reason = ""; /* no error */
1942
1943 if (!target_can_be_compressed(ctx, target, internalFormat)) {
1944 reason = "target";
1945 error = GL_INVALID_ENUM;
1946 goto error;
1947 }
1948
1949 /* This will detect any invalid internalFormat value */
1950 if (!_mesa_is_compressed_format(ctx, internalFormat)) {
1951 reason = "internalFormat";
1952 error = GL_INVALID_ENUM;
1953 goto error;
1954 }
1955
1956 switch (internalFormat) {
1957 case GL_PALETTE4_RGB8_OES:
1958 case GL_PALETTE4_RGBA8_OES:
1959 case GL_PALETTE4_R5_G6_B5_OES:
1960 case GL_PALETTE4_RGBA4_OES:
1961 case GL_PALETTE4_RGB5_A1_OES:
1962 case GL_PALETTE8_RGB8_OES:
1963 case GL_PALETTE8_RGBA8_OES:
1964 case GL_PALETTE8_R5_G6_B5_OES:
1965 case GL_PALETTE8_RGBA4_OES:
1966 case GL_PALETTE8_RGB5_A1_OES:
1967 _mesa_cpal_compressed_format_type(internalFormat, &choose_format,
1968 &choose_type);
1969 proxy_format = choose_format;
1970
1971 /* check level (note that level should be zero or less!) */
1972 if (level > 0 || level < -maxLevels) {
1973 reason = "level";
1974 error = GL_INVALID_VALUE;
1975 goto error;
1976 }
1977
1978 if (dimensions != 2) {
1979 reason = "compressed paletted textures must be 2D";
1980 error = GL_INVALID_OPERATION;
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 = _mesa_cpal_compressed_size(level, internalFormat,
1988 width, height);
1989
1990 /* This is for the benefit of the TestProxyTexImage below. It expects
1991 * level to be non-negative. OES_compressed_paletted_texture uses a
1992 * weird mechanism where the level specified to glCompressedTexImage2D
1993 * is -(n-1) number of levels in the texture, and the data specifies the
1994 * complete mipmap stack. This is done to ensure the palette is the
1995 * same for all levels.
1996 */
1997 level = -level;
1998 break;
1999
2000 default:
2001 choose_format = GL_NONE;
2002 choose_type = GL_NONE;
2003 proxy_format = internalFormat;
2004
2005 /* check level */
2006 if (level < 0 || level >= maxLevels) {
2007 reason = "level";
2008 error = GL_INVALID_VALUE;
2009 goto error;
2010 }
2011
2012 /* Figure out the expected texture size (in bytes). This will be
2013 * checked against the actual size later.
2014 */
2015 expectedSize = compressed_tex_size(width, height, depth, internalFormat);
2016 break;
2017 }
2018
2019 /* This should really never fail */
2020 if (_mesa_base_tex_format(ctx, internalFormat) < 0) {
2021 reason = "internalFormat";
2022 error = GL_INVALID_ENUM;
2023 goto error;
2024 }
2025
2026 /* No compressed formats support borders at this time */
2027 if (border != 0) {
2028 reason = "border != 0";
2029 error = GL_INVALID_VALUE;
2030 goto error;
2031 }
2032
2033 /* For cube map, width must equal height */
2034 if (_mesa_is_cube_face(target) && width != height) {
2035 reason = "width != height";
2036 error = GL_INVALID_VALUE;
2037 goto error;
2038 }
2039
2040 /* check image size against compression block size */
2041 {
2042 gl_format texFormat =
2043 ctx->Driver.ChooseTextureFormat(ctx, target, proxy_format,
2044 choose_format, choose_type);
2045 GLuint bw, bh;
2046
2047 _mesa_get_format_block_size(texFormat, &bw, &bh);
2048 if ((width > bw && width % bw > 0) ||
2049 (height > bh && height % bh > 0)) {
2050 /*
2051 * Per GL_ARB_texture_compression: GL_INVALID_OPERATION is
2052 * generated [...] if any parameter combinations are not
2053 * supported by the specific compressed internal format.
2054 */
2055 reason = "invalid width or height for compression format";
2056 error = GL_INVALID_OPERATION;
2057 goto error;
2058 }
2059 }
2060
2061 /* check image sizes */
2062 if (!ctx->Driver.TestProxyTexImage(ctx, proxyTarget, level,
2063 proxy_format, choose_format,
2064 choose_type,
2065 width, height, depth, border)) {
2066 /* See error comment above */
2067 if (target == proxyTarget) {
2068 return PROXY_ERROR;
2069 }
2070 reason = "invalid width, height or format";
2071 error = GL_INVALID_OPERATION;
2072 goto error;
2073 }
2074
2075 /* check image size in bytes */
2076 if (expectedSize != imageSize) {
2077 /* Per GL_ARB_texture_compression: GL_INVALID_VALUE is generated [...]
2078 * if <imageSize> is not consistent with the format, dimensions, and
2079 * contents of the specified image.
2080 */
2081 reason = "imageSize inconsistant with width/height/format";
2082 error = GL_INVALID_VALUE;
2083 goto error;
2084 }
2085
2086 if (!mutable_tex_object(ctx, target)) {
2087 reason = "immutable texture";
2088 error = GL_INVALID_OPERATION;
2089 goto error;
2090 }
2091
2092 return GL_NO_ERROR;
2093
2094 error:
2095 _mesa_error(ctx, error, "glCompressedTexImage%dD(%s)", dimensions, reason);
2096 return error;
2097 }
2098
2099
2100
2101 /**
2102 * Test glTexSubImage[123]D() parameters for errors.
2103 *
2104 * \param ctx GL context.
2105 * \param dimensions texture image dimensions (must be 1, 2 or 3).
2106 * \param target texture target given by the user.
2107 * \param level image level given by the user.
2108 * \param xoffset sub-image x offset given by the user.
2109 * \param yoffset sub-image y offset given by the user.
2110 * \param zoffset sub-image z offset given by the user.
2111 * \param format pixel data format given by the user.
2112 * \param type pixel data type given by the user.
2113 * \param width image width given by the user.
2114 * \param height image height given by the user.
2115 * \param depth image depth given by the user.
2116 *
2117 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2118 *
2119 * Verifies each of the parameters against the constants specified in
2120 * __struct gl_contextRec::Const and the supported extensions, and according
2121 * to the OpenGL specification.
2122 */
2123 static GLboolean
2124 subtexture_error_check( struct gl_context *ctx, GLuint dimensions,
2125 GLenum target, GLint level,
2126 GLint xoffset, GLint yoffset, GLint zoffset,
2127 GLint width, GLint height, GLint depth,
2128 GLenum format, GLenum type )
2129 {
2130 GLenum err;
2131
2132 /* Basic level check */
2133 if (level < 0 || level >= MAX_TEXTURE_LEVELS) {
2134 _mesa_error(ctx, GL_INVALID_ENUM, "glTexSubImage2D(level=%d)", level);
2135 return GL_TRUE;
2136 }
2137
2138 /* Check for negative sizes */
2139 if (width < 0) {
2140 _mesa_error(ctx, GL_INVALID_VALUE,
2141 "glTexSubImage%dD(width=%d)", dimensions, width);
2142 return GL_TRUE;
2143 }
2144 if (height < 0 && dimensions > 1) {
2145 _mesa_error(ctx, GL_INVALID_VALUE,
2146 "glTexSubImage%dD(height=%d)", dimensions, height);
2147 return GL_TRUE;
2148 }
2149 if (depth < 0 && dimensions > 2) {
2150 _mesa_error(ctx, GL_INVALID_VALUE,
2151 "glTexSubImage%dD(depth=%d)", dimensions, depth);
2152 return GL_TRUE;
2153 }
2154
2155 /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
2156 * combinations of format and type that can be used. Formats and types
2157 * that require additional extensions (e.g., GL_FLOAT requires
2158 * GL_OES_texture_float) are filtered elsewhere.
2159 */
2160 if (_mesa_is_gles(ctx) && !_mesa_is_gles3(ctx)) {
2161 err = _mesa_es_error_check_format_and_type(format, type, dimensions);
2162 if (err != GL_NO_ERROR) {
2163 _mesa_error(ctx, err,
2164 "glTexSubImage%dD(format = %s, type = %s)",
2165 dimensions,
2166 _mesa_lookup_enum_by_nr(format),
2167 _mesa_lookup_enum_by_nr(type));
2168 return GL_TRUE;
2169 }
2170 }
2171
2172 err = _mesa_error_check_format_and_type(ctx, format, type);
2173 if (err != GL_NO_ERROR) {
2174 _mesa_error(ctx, err,
2175 "glTexSubImage%dD(incompatible format 0x%x, type 0x%x)",
2176 dimensions, format, type);
2177 return GL_TRUE;
2178 }
2179
2180 return GL_FALSE;
2181 }
2182
2183
2184 /**
2185 * Do second part of glTexSubImage which depends on the destination texture.
2186 * \return GL_TRUE if error recorded, GL_FALSE otherwise
2187 */
2188 static GLboolean
2189 subtexture_error_check2( struct gl_context *ctx, GLuint dimensions,
2190 GLenum target, GLint level,
2191 GLint xoffset, GLint yoffset, GLint zoffset,
2192 GLint width, GLint height, GLint depth,
2193 GLenum format, GLenum type,
2194 const struct gl_texture_image *destTex )
2195 {
2196 if (!destTex) {
2197 /* undefined image level */
2198 _mesa_error(ctx, GL_INVALID_OPERATION, "glTexSubImage%dD", dimensions);
2199 return GL_TRUE;
2200 }
2201
2202 if (xoffset < -((GLint)destTex->Border)) {
2203 _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage%dD(xoffset)",
2204 dimensions);
2205 return GL_TRUE;
2206 }
2207 if (xoffset + width > (GLint) (destTex->Width + destTex->Border)) {
2208 _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage%dD(xoffset+width)",
2209 dimensions);
2210 return GL_TRUE;
2211 }
2212 if (dimensions > 1) {
2213 GLint yBorder = (target == GL_TEXTURE_1D_ARRAY) ? 0 : destTex->Border;
2214 if (yoffset < -yBorder) {
2215 _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage%dD(yoffset)",
2216 dimensions);
2217 return GL_TRUE;
2218 }
2219 if (yoffset + height > (GLint) destTex->Height + yBorder) {
2220 _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage%dD(yoffset+height)",
2221 dimensions);
2222 return GL_TRUE;
2223 }
2224 }
2225 if (dimensions > 2) {
2226 GLint zBorder = (target == GL_TEXTURE_2D_ARRAY) ? 0 : destTex->Border;
2227 if (zoffset < -zBorder) {
2228 _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage3D(zoffset)");
2229 return GL_TRUE;
2230 }
2231 if (zoffset + depth > (GLint) destTex->Depth + zBorder) {
2232 _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage3D(zoffset+depth)");
2233 return GL_TRUE;
2234 }
2235 }
2236
2237 if (_mesa_is_format_compressed(destTex->TexFormat)) {
2238 GLuint bw, bh;
2239
2240 if (compressedteximage_only_format(ctx, destTex->InternalFormat)) {
2241 _mesa_error(ctx, GL_INVALID_OPERATION,
2242 "glTexSubImage%dD(no compression for format)", dimensions);
2243 return GL_TRUE;
2244 }
2245
2246 /* do tests which depend on compression block size */
2247 _mesa_get_format_block_size(destTex->TexFormat, &bw, &bh);
2248
2249 /* offset must be multiple of block size */
2250 if ((xoffset % bw != 0) || (yoffset % bh != 0)) {
2251 _mesa_error(ctx, GL_INVALID_OPERATION,
2252 "glTexSubImage%dD(xoffset = %d, yoffset = %d)",
2253 dimensions, xoffset, yoffset);
2254 return GL_TRUE;
2255 }
2256 /* size must be multiple of bw by bh or equal to whole texture size */
2257 if ((width % bw != 0) && (GLuint) width != destTex->Width) {
2258 _mesa_error(ctx, GL_INVALID_OPERATION,
2259 "glTexSubImage%dD(width = %d)", dimensions, width);
2260 return GL_TRUE;
2261 }
2262 if ((height % bh != 0) && (GLuint) height != destTex->Height) {
2263 _mesa_error(ctx, GL_INVALID_OPERATION,
2264 "glTexSubImage%dD(height = %d)", dimensions, height);
2265 return GL_TRUE;
2266 }
2267 }
2268
2269 if (ctx->Version >= 30 || ctx->Extensions.EXT_texture_integer) {
2270 /* both source and dest must be integer-valued, or neither */
2271 if (_mesa_is_format_integer_color(destTex->TexFormat) !=
2272 _mesa_is_enum_format_integer(format)) {
2273 _mesa_error(ctx, GL_INVALID_OPERATION,
2274 "glTexSubImage%dD(integer/non-integer format mismatch)",
2275 dimensions);
2276 return GL_TRUE;
2277 }
2278 }
2279
2280 return GL_FALSE;
2281 }
2282
2283
2284 /**
2285 * Test glCopyTexImage[12]D() parameters for errors.
2286 *
2287 * \param ctx GL context.
2288 * \param dimensions texture image dimensions (must be 1, 2 or 3).
2289 * \param target texture target given by the user.
2290 * \param level image level given by the user.
2291 * \param internalFormat internal format given by the user.
2292 * \param width image width given by the user.
2293 * \param height image height given by the user.
2294 * \param border texture border.
2295 *
2296 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2297 *
2298 * Verifies each of the parameters against the constants specified in
2299 * __struct gl_contextRec::Const and the supported extensions, and according
2300 * to the OpenGL specification.
2301 */
2302 static GLboolean
2303 copytexture_error_check( struct gl_context *ctx, GLuint dimensions,
2304 GLenum target, GLint level, GLint internalFormat,
2305 GLint width, GLint height, GLint border )
2306 {
2307 const GLenum proxyTarget = _mesa_get_proxy_target(target);
2308 const GLenum type = GL_FLOAT;
2309 GLboolean sizeOK;
2310 GLint baseFormat;
2311
2312 /* check target */
2313 if (!legal_texsubimage_target(ctx, dimensions, target)) {
2314 _mesa_error(ctx, GL_INVALID_ENUM, "glCopyTexImage%uD(target=%s)",
2315 dimensions, _mesa_lookup_enum_by_nr(target));
2316 return GL_TRUE;
2317 }
2318
2319 /* Basic level check (more checking in ctx->Driver.TestProxyTexImage) */
2320 if (level < 0 || level >= MAX_TEXTURE_LEVELS) {
2321 _mesa_error(ctx, GL_INVALID_VALUE,
2322 "glCopyTexImage%dD(level=%d)", dimensions, level);
2323 return GL_TRUE;
2324 }
2325
2326 /* Check that the source buffer is complete */
2327 if (_mesa_is_user_fbo(ctx->ReadBuffer)) {
2328 if (ctx->ReadBuffer->_Status == 0) {
2329 _mesa_test_framebuffer_completeness(ctx, ctx->ReadBuffer);
2330 }
2331 if (ctx->ReadBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
2332 _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
2333 "glCopyTexImage%dD(invalid readbuffer)", dimensions);
2334 return GL_TRUE;
2335 }
2336
2337 if (ctx->ReadBuffer->Visual.samples > 0) {
2338 _mesa_error(ctx, GL_INVALID_OPERATION,
2339 "glCopyTexImage%dD(multisample FBO)",
2340 dimensions);
2341 return GL_TRUE;
2342 }
2343 }
2344
2345 /* Check border */
2346 if (border < 0 || border > 1 ||
2347 ((ctx->API != API_OPENGL ||
2348 target == GL_TEXTURE_RECTANGLE_NV ||
2349 target == GL_PROXY_TEXTURE_RECTANGLE_NV) && border != 0)) {
2350 _mesa_error(ctx, GL_INVALID_VALUE,
2351 "glCopyTexImage%dD(border=%d)", dimensions, border);
2352 return GL_TRUE;
2353 }
2354
2355 /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
2356 * internalFormat.
2357 */
2358 if (_mesa_is_gles(ctx) && !_mesa_is_gles3(ctx)) {
2359 switch (internalFormat) {
2360 case GL_ALPHA:
2361 case GL_RGB:
2362 case GL_RGBA:
2363 case GL_LUMINANCE:
2364 case GL_LUMINANCE_ALPHA:
2365 break;
2366 default:
2367 _mesa_error(ctx, GL_INVALID_VALUE,
2368 "glCopyTexImage%dD(internalFormat)", dimensions);
2369 return GL_TRUE;
2370 }
2371 }
2372
2373 baseFormat = _mesa_base_tex_format(ctx, internalFormat);
2374 if (baseFormat < 0) {
2375 _mesa_error(ctx, GL_INVALID_VALUE,
2376 "glCopyTexImage%dD(internalFormat)", dimensions);
2377 return GL_TRUE;
2378 }
2379
2380 if (!_mesa_source_buffer_exists(ctx, baseFormat)) {
2381 _mesa_error(ctx, GL_INVALID_OPERATION,
2382 "glCopyTexImage%dD(missing readbuffer)", dimensions);
2383 return GL_TRUE;
2384 }
2385
2386 /* From the EXT_texture_integer spec:
2387 *
2388 * "INVALID_OPERATION is generated by CopyTexImage* and CopyTexSubImage*
2389 * if the texture internalformat is an integer format and the read color
2390 * buffer is not an integer format, or if the internalformat is not an
2391 * integer format and the read color buffer is an integer format."
2392 */
2393 if (_mesa_is_color_format(internalFormat)) {
2394 struct gl_renderbuffer *rb = ctx->ReadBuffer->_ColorReadBuffer;
2395
2396 if (_mesa_is_enum_format_integer(rb->InternalFormat) !=
2397 _mesa_is_enum_format_integer(internalFormat)) {
2398 _mesa_error(ctx, GL_INVALID_OPERATION,
2399 "glCopyTexImage%dD(integer vs non-integer)", dimensions);
2400 return GL_TRUE;
2401 }
2402 }
2403
2404 /* Do size, level checking */
2405 sizeOK = (proxyTarget == GL_PROXY_TEXTURE_CUBE_MAP_ARB)
2406 ? (width == height) : 1;
2407
2408 sizeOK = sizeOK && ctx->Driver.TestProxyTexImage(ctx, proxyTarget, level,
2409 internalFormat, baseFormat,
2410 type, width, height,
2411 1, border);
2412
2413 if (!sizeOK) {
2414 if (dimensions == 1) {
2415 _mesa_error(ctx, GL_INVALID_VALUE,
2416 "glCopyTexImage1D(width=%d)", width);
2417 }
2418 else {
2419 ASSERT(dimensions == 2);
2420 _mesa_error(ctx, GL_INVALID_VALUE,
2421 "glCopyTexImage2D(width=%d, height=%d)", width, height);
2422 }
2423 return GL_TRUE;
2424 }
2425
2426 if (_mesa_is_compressed_format(ctx, internalFormat)) {
2427 if (!target_can_be_compressed(ctx, target, internalFormat)) {
2428 _mesa_error(ctx, GL_INVALID_ENUM,
2429 "glCopyTexImage%dD(target)", dimensions);
2430 return GL_TRUE;
2431 }
2432 if (compressedteximage_only_format(ctx, internalFormat)) {
2433 _mesa_error(ctx, GL_INVALID_OPERATION,
2434 "glCopyTexImage%dD(no compression for format)", dimensions);
2435 return GL_TRUE;
2436 }
2437 if (border != 0) {
2438 _mesa_error(ctx, GL_INVALID_OPERATION,
2439 "glCopyTexImage%dD(border!=0)", dimensions);
2440 return GL_TRUE;
2441 }
2442 }
2443
2444 if (!mutable_tex_object(ctx, target)) {
2445 _mesa_error(ctx, GL_INVALID_OPERATION,
2446 "glCopyTexImage%dD(immutable texture)", dimensions);
2447 return GL_TRUE;
2448 }
2449
2450 /* if we get here, the parameters are OK */
2451 return GL_FALSE;
2452 }
2453
2454
2455 /**
2456 * Test glCopyTexSubImage[12]D() parameters for errors.
2457 * Note that this is the first part of error checking.
2458 * See also copytexsubimage_error_check2() below for the second part.
2459 *
2460 * \param ctx GL context.
2461 * \param dimensions texture image dimensions (must be 1, 2 or 3).
2462 * \param target texture target given by the user.
2463 * \param level image level given by the user.
2464 *
2465 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2466 */
2467 static GLboolean
2468 copytexsubimage_error_check1( struct gl_context *ctx, GLuint dimensions,
2469 GLenum target, GLint level)
2470 {
2471 /* Check that the source buffer is complete */
2472 if (_mesa_is_user_fbo(ctx->ReadBuffer)) {
2473 if (ctx->ReadBuffer->_Status == 0) {
2474 _mesa_test_framebuffer_completeness(ctx, ctx->ReadBuffer);
2475 }
2476 if (ctx->ReadBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
2477 _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
2478 "glCopyTexImage%dD(invalid readbuffer)", dimensions);
2479 return GL_TRUE;
2480 }
2481
2482 if (ctx->ReadBuffer->Visual.samples > 0) {
2483 _mesa_error(ctx, GL_INVALID_OPERATION,
2484 "glCopyTexSubImage%dD(multisample FBO)",
2485 dimensions);
2486 return GL_TRUE;
2487 }
2488 }
2489
2490 /* check target (proxies not allowed) */
2491 if (!legal_texsubimage_target(ctx, dimensions, target)) {
2492 _mesa_error(ctx, GL_INVALID_ENUM, "glCopyTexSubImage%uD(target=%s)",
2493 dimensions, _mesa_lookup_enum_by_nr(target));
2494 return GL_TRUE;
2495 }
2496
2497 /* Check level */
2498 if (level < 0 || level >= MAX_TEXTURE_LEVELS) {
2499 _mesa_error(ctx, GL_INVALID_VALUE,
2500 "glCopyTexSubImage%dD(level=%d)", dimensions, level);
2501 return GL_TRUE;
2502 }
2503
2504 return GL_FALSE;
2505 }
2506
2507
2508 /**
2509 * Second part of error checking for glCopyTexSubImage[12]D().
2510 * \param xoffset sub-image x offset given by the user.
2511 * \param yoffset sub-image y offset given by the user.
2512 * \param zoffset sub-image z offset given by the user.
2513 * \param width image width given by the user.
2514 * \param height image height given by the user.
2515 */
2516 static GLboolean
2517 copytexsubimage_error_check2( struct gl_context *ctx, GLuint dimensions,
2518 GLenum target, GLint level,
2519 GLint xoffset, GLint yoffset, GLint zoffset,
2520 GLsizei width, GLsizei height,
2521 const struct gl_texture_image *teximage )
2522 {
2523 /* check that dest tex image exists */
2524 if (!teximage) {
2525 _mesa_error(ctx, GL_INVALID_OPERATION,
2526 "glCopyTexSubImage%dD(undefined texture level: %d)",
2527 dimensions, level);
2528 return GL_TRUE;
2529 }
2530
2531 /* Check size */
2532 if (width < 0) {
2533 _mesa_error(ctx, GL_INVALID_VALUE,
2534 "glCopyTexSubImage%dD(width=%d)", dimensions, width);
2535 return GL_TRUE;
2536 }
2537 if (dimensions > 1 && height < 0) {
2538 _mesa_error(ctx, GL_INVALID_VALUE,
2539 "glCopyTexSubImage%dD(height=%d)", dimensions, height);
2540 return GL_TRUE;
2541 }
2542
2543 /* check x/y offsets */
2544 if (xoffset < -((GLint)teximage->Border)) {
2545 _mesa_error(ctx, GL_INVALID_VALUE,
2546 "glCopyTexSubImage%dD(xoffset=%d)", dimensions, xoffset);
2547 return GL_TRUE;
2548 }
2549 if (xoffset + width > (GLint) (teximage->Width + teximage->Border)) {
2550 _mesa_error(ctx, GL_INVALID_VALUE,
2551 "glCopyTexSubImage%dD(xoffset+width)", dimensions);
2552 return GL_TRUE;
2553 }
2554 if (dimensions > 1) {
2555 GLint yBorder = (target == GL_TEXTURE_1D_ARRAY) ? 0 : teximage->Border;
2556 if (yoffset < -yBorder) {
2557 _mesa_error(ctx, GL_INVALID_VALUE,
2558 "glCopyTexSubImage%dD(yoffset=%d)", dimensions, yoffset);
2559 return GL_TRUE;
2560 }
2561 /* NOTE: we're adding the border here, not subtracting! */
2562 if (yoffset + height > (GLint) teximage->Height + yBorder) {
2563 _mesa_error(ctx, GL_INVALID_VALUE,
2564 "glCopyTexSubImage%dD(yoffset+height)", dimensions);
2565 return GL_TRUE;
2566 }
2567 }
2568
2569 /* check z offset */
2570 if (dimensions > 2) {
2571 GLint zBorder = (target == GL_TEXTURE_2D_ARRAY) ? 0 : teximage->Border;
2572 if (zoffset < -zBorder) {
2573 _mesa_error(ctx, GL_INVALID_VALUE,
2574 "glCopyTexSubImage%dD(zoffset)", dimensions);
2575 return GL_TRUE;
2576 }
2577 if (zoffset > (GLint) teximage->Depth + zBorder) {
2578 _mesa_error(ctx, GL_INVALID_VALUE,
2579 "glCopyTexSubImage%dD(zoffset+depth)", dimensions);
2580 return GL_TRUE;
2581 }
2582 }
2583
2584 if (_mesa_is_format_compressed(teximage->TexFormat)) {
2585 if (compressedteximage_only_format(ctx, teximage->InternalFormat)) {
2586 _mesa_error(ctx, GL_INVALID_OPERATION,
2587 "glCopyTexSubImage%dD(no compression for format)", dimensions);
2588 return GL_TRUE;
2589 }
2590 /* offset must be multiple of 4 */
2591 if ((xoffset & 3) || (yoffset & 3)) {
2592 _mesa_error(ctx, GL_INVALID_VALUE,
2593 "glCopyTexSubImage%dD(xoffset or yoffset)", dimensions);
2594 return GL_TRUE;
2595 }
2596 /* size must be multiple of 4 */
2597 if ((width & 3) != 0 && (GLuint) width != teximage->Width) {
2598 _mesa_error(ctx, GL_INVALID_VALUE,
2599 "glCopyTexSubImage%dD(width)", dimensions);
2600 return GL_TRUE;
2601 }
2602 if ((height & 3) != 0 && (GLuint) height != teximage->Height) {
2603 _mesa_error(ctx, GL_INVALID_VALUE,
2604 "glCopyTexSubImage%dD(height)", dimensions);
2605 return GL_TRUE;
2606 }
2607 }
2608
2609 if (teximage->InternalFormat == GL_YCBCR_MESA) {
2610 _mesa_error(ctx, GL_INVALID_OPERATION, "glCopyTexSubImage2D");
2611 return GL_TRUE;
2612 }
2613
2614 if (!_mesa_source_buffer_exists(ctx, teximage->_BaseFormat)) {
2615 _mesa_error(ctx, GL_INVALID_OPERATION,
2616 "glCopyTexSubImage%dD(missing readbuffer, format=0x%x)",
2617 dimensions, teximage->_BaseFormat);
2618 return GL_TRUE;
2619 }
2620
2621 /* From the EXT_texture_integer spec:
2622 *
2623 * "INVALID_OPERATION is generated by CopyTexImage* and CopyTexSubImage*
2624 * if the texture internalformat is an integer format and the read color
2625 * buffer is not an integer format, or if the internalformat is not an
2626 * integer format and the read color buffer is an integer format."
2627 */
2628 if (_mesa_is_color_format(teximage->InternalFormat)) {
2629 struct gl_renderbuffer *rb = ctx->ReadBuffer->_ColorReadBuffer;
2630
2631 if (_mesa_is_format_integer_color(rb->Format) !=
2632 _mesa_is_format_integer_color(teximage->TexFormat)) {
2633 _mesa_error(ctx, GL_INVALID_OPERATION,
2634 "glCopyTexImage%dD(integer vs non-integer)", dimensions);
2635 return GL_TRUE;
2636 }
2637 }
2638
2639 /* if we get here, the parameters are OK */
2640 return GL_FALSE;
2641 }
2642
2643
2644 /** Callback info for walking over FBO hash table */
2645 struct cb_info
2646 {
2647 struct gl_context *ctx;
2648 struct gl_texture_object *texObj;
2649 GLuint level, face;
2650 };
2651
2652
2653 /**
2654 * Check render to texture callback. Called from _mesa_HashWalk().
2655 */
2656 static void
2657 check_rtt_cb(GLuint key, void *data, void *userData)
2658 {
2659 struct gl_framebuffer *fb = (struct gl_framebuffer *) data;
2660 const struct cb_info *info = (struct cb_info *) userData;
2661 struct gl_context *ctx = info->ctx;
2662 const struct gl_texture_object *texObj = info->texObj;
2663 const GLuint level = info->level, face = info->face;
2664
2665 /* If this is a user-created FBO */
2666 if (_mesa_is_user_fbo(fb)) {
2667 GLuint i;
2668 /* check if any of the FBO's attachments point to 'texObj' */
2669 for (i = 0; i < BUFFER_COUNT; i++) {
2670 struct gl_renderbuffer_attachment *att = fb->Attachment + i;
2671 if (att->Type == GL_TEXTURE &&
2672 att->Texture == texObj &&
2673 att->TextureLevel == level &&
2674 att->CubeMapFace == face) {
2675 ASSERT(_mesa_get_attachment_teximage(att));
2676 /* Tell driver about the new renderbuffer texture */
2677 ctx->Driver.RenderTexture(ctx, ctx->DrawBuffer, att);
2678 /* Mark fb status as indeterminate to force re-validation */
2679 fb->_Status = 0;
2680 }
2681 }
2682 }
2683 }
2684
2685
2686 /**
2687 * When a texture image is specified we have to check if it's bound to
2688 * any framebuffer objects (render to texture) in order to detect changes
2689 * in size or format since that effects FBO completeness.
2690 * Any FBOs rendering into the texture must be re-validated.
2691 */
2692 void
2693 _mesa_update_fbo_texture(struct gl_context *ctx,
2694 struct gl_texture_object *texObj,
2695 GLuint face, GLuint level)
2696 {
2697 /* Only check this texture if it's been marked as RenderToTexture */
2698 if (texObj->_RenderToTexture) {
2699 struct cb_info info;
2700 info.ctx = ctx;
2701 info.texObj = texObj;
2702 info.level = level;
2703 info.face = face;
2704 _mesa_HashWalk(ctx->Shared->FrameBuffers, check_rtt_cb, &info);
2705 }
2706 }
2707
2708
2709 /**
2710 * If the texture object's GenerateMipmap flag is set and we've
2711 * changed the texture base level image, regenerate the rest of the
2712 * mipmap levels now.
2713 */
2714 static inline void
2715 check_gen_mipmap(struct gl_context *ctx, GLenum target,
2716 struct gl_texture_object *texObj, GLint level)
2717 {
2718 ASSERT(target != GL_TEXTURE_CUBE_MAP);
2719 if (texObj->GenerateMipmap &&
2720 level == texObj->BaseLevel &&
2721 level < texObj->MaxLevel) {
2722 ASSERT(ctx->Driver.GenerateMipmap);
2723 ctx->Driver.GenerateMipmap(ctx, target, texObj);
2724 }
2725 }
2726
2727
2728 /** Debug helper: override the user-requested internal format */
2729 static GLenum
2730 override_internal_format(GLenum internalFormat, GLint width, GLint height)
2731 {
2732 #if 0
2733 if (internalFormat == GL_RGBA16F_ARB ||
2734 internalFormat == GL_RGBA32F_ARB) {
2735 printf("Convert rgba float tex to int %d x %d\n", width, height);
2736 return GL_RGBA;
2737 }
2738 else if (internalFormat == GL_RGB16F_ARB ||
2739 internalFormat == GL_RGB32F_ARB) {
2740 printf("Convert rgb float tex to int %d x %d\n", width, height);
2741 return GL_RGB;
2742 }
2743 else if (internalFormat == GL_LUMINANCE_ALPHA16F_ARB ||
2744 internalFormat == GL_LUMINANCE_ALPHA32F_ARB) {
2745 printf("Convert luminance float tex to int %d x %d\n", width, height);
2746 return GL_LUMINANCE_ALPHA;
2747 }
2748 else if (internalFormat == GL_LUMINANCE16F_ARB ||
2749 internalFormat == GL_LUMINANCE32F_ARB) {
2750 printf("Convert luminance float tex to int %d x %d\n", width, height);
2751 return GL_LUMINANCE;
2752 }
2753 else if (internalFormat == GL_ALPHA16F_ARB ||
2754 internalFormat == GL_ALPHA32F_ARB) {
2755 printf("Convert luminance float tex to int %d x %d\n", width, height);
2756 return GL_ALPHA;
2757 }
2758 /*
2759 else if (internalFormat == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT) {
2760 internalFormat = GL_RGBA;
2761 }
2762 */
2763 else {
2764 return internalFormat;
2765 }
2766 #else
2767 return internalFormat;
2768 #endif
2769 }
2770
2771
2772 /**
2773 * Choose the actual hardware format for a texture image.
2774 * Try to use the same format as the previous image level when possible.
2775 * Otherwise, ask the driver for the best format.
2776 * It's important to try to choose a consistant format for all levels
2777 * for efficient texture memory layout/allocation. In particular, this
2778 * comes up during automatic mipmap generation.
2779 */
2780 gl_format
2781 _mesa_choose_texture_format(struct gl_context *ctx,
2782 struct gl_texture_object *texObj,
2783 GLenum target, GLint level,
2784 GLenum internalFormat, GLenum format, GLenum type)
2785 {
2786 gl_format f;
2787
2788 /* see if we've already chosen a format for the previous level */
2789 if (level > 0) {
2790 struct gl_texture_image *prevImage =
2791 _mesa_select_tex_image(ctx, texObj, target, level - 1);
2792 /* See if the prev level is defined and has an internal format which
2793 * matches the new internal format.
2794 */
2795 if (prevImage &&
2796 prevImage->Width > 0 &&
2797 prevImage->InternalFormat == internalFormat) {
2798 /* use the same format */
2799 ASSERT(prevImage->TexFormat != MESA_FORMAT_NONE);
2800 return prevImage->TexFormat;
2801 }
2802 }
2803
2804 /* If the application requested compression to an S3TC format but we don't
2805 * have the DTXn library, force a generic compressed format instead.
2806 */
2807 if (internalFormat != format) {
2808 const GLenum before = internalFormat;
2809
2810 switch (internalFormat) {
2811 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
2812 if (!ctx->Mesa_DXTn)
2813 internalFormat = GL_COMPRESSED_RGB;
2814 break;
2815 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
2816 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
2817 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
2818 if (!ctx->Mesa_DXTn)
2819 internalFormat = GL_COMPRESSED_RGBA;
2820 break;
2821 default:
2822 break;
2823 }
2824
2825 if (before != internalFormat) {
2826 _mesa_warning(ctx,
2827 "DXT compression requested (%s), "
2828 "but libtxc_dxtn library not installed. Using %s "
2829 "instead.",
2830 _mesa_lookup_enum_by_nr(before),
2831 _mesa_lookup_enum_by_nr(internalFormat));
2832 }
2833 }
2834
2835 /* choose format from scratch */
2836 f = ctx->Driver.ChooseTextureFormat(ctx, texObj->Target, internalFormat,
2837 format, type);
2838 ASSERT(f != MESA_FORMAT_NONE);
2839 return f;
2840 }
2841
2842 /**
2843 * Adjust pixel unpack params and image dimensions to strip off the
2844 * one-pixel texture border.
2845 *
2846 * Gallium and intel don't support texture borders. They've seldem been used
2847 * and seldom been implemented correctly anyway.
2848 *
2849 * \param unpackNew returns the new pixel unpack parameters
2850 */
2851 static void
2852 strip_texture_border(GLenum target,
2853 GLint *width, GLint *height, GLint *depth,
2854 const struct gl_pixelstore_attrib *unpack,
2855 struct gl_pixelstore_attrib *unpackNew)
2856 {
2857 assert(width);
2858 assert(height);
2859 assert(depth);
2860
2861 *unpackNew = *unpack;
2862
2863 if (unpackNew->RowLength == 0)
2864 unpackNew->RowLength = *width;
2865
2866 if (unpackNew->ImageHeight == 0)
2867 unpackNew->ImageHeight = *height;
2868
2869 assert(*width >= 3);
2870 unpackNew->SkipPixels++; /* skip the border */
2871 *width = *width - 2; /* reduce the width by two border pixels */
2872
2873 /* The min height of a texture with a border is 3 */
2874 if (*height >= 3 && target != GL_TEXTURE_1D_ARRAY) {
2875 unpackNew->SkipRows++; /* skip the border */
2876 *height = *height - 2; /* reduce the height by two border pixels */
2877 }
2878
2879 if (*depth >= 3 && target != GL_TEXTURE_2D_ARRAY) {
2880 unpackNew->SkipImages++; /* skip the border */
2881 *depth = *depth - 2; /* reduce the depth by two border pixels */
2882 }
2883 }
2884
2885
2886 /**
2887 * Common code to implement all the glTexImage1D/2D/3D functions
2888 * as well as glCompressedTexImage1D/2D/3D.
2889 * \param compressed only GL_TRUE for glCompressedTexImage1D/2D/3D calls.
2890 * \param format the user's image format (only used if !compressed)
2891 * \param type the user's image type (only used if !compressed)
2892 * \param imageSize only used for glCompressedTexImage1D/2D/3D calls.
2893 */
2894 static void
2895 teximage(struct gl_context *ctx, GLboolean compressed, GLuint dims,
2896 GLenum target, GLint level, GLint internalFormat,
2897 GLsizei width, GLsizei height, GLsizei depth,
2898 GLint border, GLenum format, GLenum type,
2899 GLsizei imageSize, const GLvoid *pixels)
2900 {
2901 const char *func = compressed ? "glCompressedTexImage" : "glTexImage";
2902 GLenum error;
2903 struct gl_pixelstore_attrib unpack_no_border;
2904 const struct gl_pixelstore_attrib *unpack = &ctx->Unpack;
2905
2906 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2907
2908 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE)) {
2909 if (compressed)
2910 _mesa_debug(ctx,
2911 "glCompressedTexImage%uD %s %d %s %d %d %d %d %p\n",
2912 dims,
2913 _mesa_lookup_enum_by_nr(target), level,
2914 _mesa_lookup_enum_by_nr(internalFormat),
2915 width, height, depth, border, pixels);
2916 else
2917 _mesa_debug(ctx,
2918 "glTexImage%uD %s %d %s %d %d %d %d %s %s %p\n",
2919 dims,
2920 _mesa_lookup_enum_by_nr(target), level,
2921 _mesa_lookup_enum_by_nr(internalFormat),
2922 width, height, depth, border,
2923 _mesa_lookup_enum_by_nr(format),
2924 _mesa_lookup_enum_by_nr(type), pixels);
2925 }
2926
2927 internalFormat = override_internal_format(internalFormat, width, height);
2928
2929 /* target error checking */
2930 if (!legal_teximage_target(ctx, dims, target)) {
2931 _mesa_error(ctx, GL_INVALID_ENUM, "%s%uD(target=%s)",
2932 func, dims, _mesa_lookup_enum_by_nr(target));
2933 return;
2934 }
2935
2936 /* general error checking */
2937 if (compressed) {
2938 error = compressed_texture_error_check(ctx, dims, target, level,
2939 internalFormat,
2940 width, height, depth,
2941 border, imageSize);
2942 }
2943 else {
2944 error = texture_error_check(ctx, dims, target, level, internalFormat,
2945 format, type, width, height, depth, border);
2946 }
2947
2948 /* Here we convert a cpal compressed image into a regular glTexImage2D
2949 * call by decompressing the texture. If we really want to support cpal
2950 * textures in any driver this would have to be changed.
2951 */
2952 if (ctx->API == API_OPENGLES && compressed && !error && dims == 2) {
2953 switch (internalFormat) {
2954 case GL_PALETTE4_RGB8_OES:
2955 case GL_PALETTE4_RGBA8_OES:
2956 case GL_PALETTE4_R5_G6_B5_OES:
2957 case GL_PALETTE4_RGBA4_OES:
2958 case GL_PALETTE4_RGB5_A1_OES:
2959 case GL_PALETTE8_RGB8_OES:
2960 case GL_PALETTE8_RGBA8_OES:
2961 case GL_PALETTE8_R5_G6_B5_OES:
2962 case GL_PALETTE8_RGBA4_OES:
2963 case GL_PALETTE8_RGB5_A1_OES:
2964 _mesa_cpal_compressed_teximage2d(target, level, internalFormat,
2965 width, height, imageSize, pixels);
2966 return;
2967 }
2968 }
2969
2970 if (_mesa_is_proxy_texture(target)) {
2971 /* Proxy texture: just clear or set state depending on error checking */
2972 struct gl_texture_image *texImage =
2973 get_proxy_tex_image(ctx, target, level);
2974 gl_format texFormat = MESA_FORMAT_NONE;
2975
2976 if (!error) {
2977 /* No parameter errors. Choose a texture format and see if we
2978 * can really allocate the texture.
2979 */
2980 struct gl_texture_object *texObj =
2981 _mesa_get_current_tex_object(ctx, target);
2982 texFormat = _mesa_choose_texture_format(ctx, texObj, target, level,
2983 internalFormat, format, type);
2984 if (!legal_texture_size(ctx, texFormat, width, height, depth)) {
2985 error = PROXY_ERROR;
2986 }
2987 }
2988
2989 if (error == PROXY_ERROR) {
2990 /* image too large, etc. Clear all proxy texture image parameters. */
2991 if (texImage)
2992 clear_teximage_fields(texImage);
2993 }
2994 else if (error == GL_FALSE) {
2995 /* no error: store the teximage parameters */
2996 if (texImage)
2997 _mesa_init_teximage_fields(ctx, texImage, width, height, depth,
2998 border, internalFormat, texFormat);
2999 }
3000 else {
3001 /* other, regular error (was already recorded) */
3002 }
3003 }
3004 else {
3005 /* non-proxy target */
3006 const GLuint face = _mesa_tex_target_to_face(target);
3007 struct gl_texture_object *texObj;
3008 struct gl_texture_image *texImage;
3009
3010 if (error) {
3011 return; /* error was recorded */
3012 }
3013
3014 /* Allow a hardware driver to just strip out the border, to provide
3015 * reliable but slightly incorrect hardware rendering instead of
3016 * rarely-tested software fallback rendering.
3017 */
3018 if (border && ctx->Const.StripTextureBorder) {
3019 strip_texture_border(target, &width, &height, &depth, unpack,
3020 &unpack_no_border);
3021 border = 0;
3022 unpack = &unpack_no_border;
3023 }
3024
3025 if (ctx->NewState & _NEW_PIXEL)
3026 _mesa_update_state(ctx);
3027
3028 texObj = _mesa_get_current_tex_object(ctx, target);
3029
3030 _mesa_lock_texture(ctx, texObj);
3031 {
3032 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
3033
3034 if (!texImage) {
3035 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s%uD", func, dims);
3036 }
3037 else {
3038 gl_format texFormat;
3039
3040 ctx->Driver.FreeTextureImageBuffer(ctx, texImage);
3041
3042 texFormat = _mesa_choose_texture_format(ctx, texObj, target, level,
3043 internalFormat, format,
3044 type);
3045
3046 if (legal_texture_size(ctx, texFormat, width, height, depth)) {
3047 _mesa_init_teximage_fields(ctx, texImage,
3048 width, height, depth,
3049 border, internalFormat, texFormat);
3050
3051 /* Give the texture to the driver. <pixels> may be null. */
3052 if (compressed) {
3053 ctx->Driver.CompressedTexImage(ctx, dims, texImage,
3054 imageSize, pixels);
3055 }
3056 else {
3057 ctx->Driver.TexImage(ctx, dims, texImage, format,
3058 type, pixels, unpack);
3059 }
3060
3061 check_gen_mipmap(ctx, target, texObj, level);
3062
3063 _mesa_update_fbo_texture(ctx, texObj, face, level);
3064
3065 _mesa_dirty_texobj(ctx, texObj, GL_TRUE);
3066 }
3067 else {
3068 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s%uD", func, dims);
3069 }
3070 }
3071 }
3072 _mesa_unlock_texture(ctx, texObj);
3073 }
3074 }
3075
3076
3077 /*
3078 * Called from the API. Note that width includes the border.
3079 */
3080 void GLAPIENTRY
3081 _mesa_TexImage1D( GLenum target, GLint level, GLint internalFormat,
3082 GLsizei width, GLint border, GLenum format,
3083 GLenum type, const GLvoid *pixels )
3084 {
3085 GET_CURRENT_CONTEXT(ctx);
3086 teximage(ctx, GL_FALSE, 1, target, level, internalFormat, width, 1, 1,
3087 border, format, type, 0, pixels);
3088 }
3089
3090
3091 void GLAPIENTRY
3092 _mesa_TexImage2D( GLenum target, GLint level, GLint internalFormat,
3093 GLsizei width, GLsizei height, GLint border,
3094 GLenum format, GLenum type,
3095 const GLvoid *pixels )
3096 {
3097 GET_CURRENT_CONTEXT(ctx);
3098 teximage(ctx, GL_FALSE, 2, target, level, internalFormat, width, height, 1,
3099 border, format, type, 0, pixels);
3100 }
3101
3102
3103 /*
3104 * Called by the API or display list executor.
3105 * Note that width and height include the border.
3106 */
3107 void GLAPIENTRY
3108 _mesa_TexImage3D( GLenum target, GLint level, GLint internalFormat,
3109 GLsizei width, GLsizei height, GLsizei depth,
3110 GLint border, GLenum format, GLenum type,
3111 const GLvoid *pixels )
3112 {
3113 GET_CURRENT_CONTEXT(ctx);
3114 teximage(ctx, GL_FALSE, 3, target, level, internalFormat,
3115 width, height, depth,
3116 border, format, type, 0, pixels);
3117 }
3118
3119
3120 void GLAPIENTRY
3121 _mesa_TexImage3DEXT( GLenum target, GLint level, GLenum internalFormat,
3122 GLsizei width, GLsizei height, GLsizei depth,
3123 GLint border, GLenum format, GLenum type,
3124 const GLvoid *pixels )
3125 {
3126 _mesa_TexImage3D(target, level, (GLint) internalFormat, width, height,
3127 depth, border, format, type, pixels);
3128 }
3129
3130
3131 void GLAPIENTRY
3132 _mesa_EGLImageTargetTexture2DOES (GLenum target, GLeglImageOES image)
3133 {
3134 struct gl_texture_object *texObj;
3135 struct gl_texture_image *texImage;
3136 bool valid_target;
3137 GET_CURRENT_CONTEXT(ctx);
3138 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3139
3140 switch (target) {
3141 case GL_TEXTURE_2D:
3142 valid_target = ctx->Extensions.OES_EGL_image;
3143 break;
3144 case GL_TEXTURE_EXTERNAL_OES:
3145 valid_target = ctx->Extensions.OES_EGL_image_external;
3146 break;
3147 default:
3148 valid_target = false;
3149 break;
3150 }
3151
3152 if (!valid_target) {
3153 _mesa_error(ctx, GL_INVALID_ENUM,
3154 "glEGLImageTargetTexture2D(target=%d)", target);
3155 return;
3156 }
3157
3158 if (ctx->NewState & _NEW_PIXEL)
3159 _mesa_update_state(ctx);
3160
3161 texObj = _mesa_get_current_tex_object(ctx, target);
3162 _mesa_lock_texture(ctx, texObj);
3163
3164 if (texObj->Immutable) {
3165 _mesa_error(ctx, GL_INVALID_OPERATION,
3166 "glEGLImageTargetTexture2D(texture is immutable)");
3167 _mesa_unlock_texture(ctx, texObj);
3168 return;
3169 }
3170
3171 texImage = _mesa_get_tex_image(ctx, texObj, target, 0);
3172 if (!texImage) {
3173 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glEGLImageTargetTexture2D");
3174 } else {
3175 ctx->Driver.FreeTextureImageBuffer(ctx, texImage);
3176
3177 ctx->Driver.EGLImageTargetTexture2D(ctx, target,
3178 texObj, texImage, image);
3179
3180 _mesa_dirty_texobj(ctx, texObj, GL_TRUE);
3181 }
3182 _mesa_unlock_texture(ctx, texObj);
3183
3184 }
3185
3186
3187
3188 /**
3189 * Implement all the glTexSubImage1/2/3D() functions.
3190 */
3191 static void
3192 texsubimage(struct gl_context *ctx, GLuint dims, GLenum target, GLint level,
3193 GLint xoffset, GLint yoffset, GLint zoffset,
3194 GLsizei width, GLsizei height, GLsizei depth,
3195 GLenum format, GLenum type, const GLvoid *pixels )
3196 {
3197 struct gl_texture_object *texObj;
3198 struct gl_texture_image *texImage;
3199
3200 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3201
3202 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
3203 _mesa_debug(ctx, "glTexSubImage%uD %s %d %d %d %d %d %d %d %s %s %p\n",
3204 dims,
3205 _mesa_lookup_enum_by_nr(target), level,
3206 xoffset, yoffset, zoffset, width, height, depth,
3207 _mesa_lookup_enum_by_nr(format),
3208 _mesa_lookup_enum_by_nr(type), pixels);
3209
3210 /* check target (proxies not allowed) */
3211 if (!legal_texsubimage_target(ctx, dims, target)) {
3212 _mesa_error(ctx, GL_INVALID_ENUM, "glTexSubImage%uD(target=%s)",
3213 dims, _mesa_lookup_enum_by_nr(target));
3214 return;
3215 }
3216
3217 if (ctx->NewState & _NEW_PIXEL)
3218 _mesa_update_state(ctx);
3219
3220 if (subtexture_error_check(ctx, dims, target, level, xoffset, yoffset, zoffset,
3221 width, height, depth, format, type)) {
3222 return; /* error was detected */
3223 }
3224
3225 texObj = _mesa_get_current_tex_object(ctx, target);
3226
3227 _mesa_lock_texture(ctx, texObj);
3228 {
3229 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
3230
3231 if (subtexture_error_check2(ctx, dims, target, level,
3232 xoffset, yoffset, zoffset,
3233 width, height, depth,
3234 format, type, texImage)) {
3235 /* error was recorded */
3236 }
3237 else if (width > 0 && height > 0 && depth > 0) {
3238 /* If we have a border, offset=-1 is legal. Bias by border width. */
3239 switch (dims) {
3240 case 3:
3241 if (target != GL_TEXTURE_2D_ARRAY)
3242 zoffset += texImage->Border;
3243 /* fall-through */
3244 case 2:
3245 if (target != GL_TEXTURE_1D_ARRAY)
3246 yoffset += texImage->Border;
3247 /* fall-through */
3248 case 1:
3249 xoffset += texImage->Border;
3250 }
3251
3252 ctx->Driver.TexSubImage(ctx, dims, texImage,
3253 xoffset, yoffset, zoffset,
3254 width, height, depth,
3255 format, type, pixels, &ctx->Unpack);
3256
3257 check_gen_mipmap(ctx, target, texObj, level);
3258
3259 ctx->NewState |= _NEW_TEXTURE;
3260 }
3261 }
3262 _mesa_unlock_texture(ctx, texObj);
3263 }
3264
3265
3266 void GLAPIENTRY
3267 _mesa_TexSubImage1D( GLenum target, GLint level,
3268 GLint xoffset, GLsizei width,
3269 GLenum format, GLenum type,
3270 const GLvoid *pixels )
3271 {
3272 GET_CURRENT_CONTEXT(ctx);
3273 texsubimage(ctx, 1, target, level,
3274 xoffset, 0, 0,
3275 width, 1, 1,
3276 format, type, pixels);
3277 }
3278
3279
3280 void GLAPIENTRY
3281 _mesa_TexSubImage2D( GLenum target, GLint level,
3282 GLint xoffset, GLint yoffset,
3283 GLsizei width, GLsizei height,
3284 GLenum format, GLenum type,
3285 const GLvoid *pixels )
3286 {
3287 GET_CURRENT_CONTEXT(ctx);
3288 texsubimage(ctx, 2, target, level,
3289 xoffset, yoffset, 0,
3290 width, height, 1,
3291 format, type, pixels);
3292 }
3293
3294
3295
3296 void GLAPIENTRY
3297 _mesa_TexSubImage3D( GLenum target, GLint level,
3298 GLint xoffset, GLint yoffset, GLint zoffset,
3299 GLsizei width, GLsizei height, GLsizei depth,
3300 GLenum format, GLenum type,
3301 const GLvoid *pixels )
3302 {
3303 GET_CURRENT_CONTEXT(ctx);
3304 texsubimage(ctx, 3, target, level,
3305 xoffset, yoffset, zoffset,
3306 width, height, depth,
3307 format, type, pixels);
3308 }
3309
3310
3311
3312 /**
3313 * For glCopyTexSubImage, return the source renderbuffer to copy texel data
3314 * from. This depends on whether the texture contains color or depth values.
3315 */
3316 static struct gl_renderbuffer *
3317 get_copy_tex_image_source(struct gl_context *ctx, gl_format texFormat)
3318 {
3319 if (_mesa_get_format_bits(texFormat, GL_DEPTH_BITS) > 0) {
3320 /* reading from depth/stencil buffer */
3321 return ctx->ReadBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;
3322 }
3323 else {
3324 /* copying from color buffer */
3325 return ctx->ReadBuffer->_ColorReadBuffer;
3326 }
3327 }
3328
3329
3330
3331 /**
3332 * Implement the glCopyTexImage1/2D() functions.
3333 */
3334 static void
3335 copyteximage(struct gl_context *ctx, GLuint dims,
3336 GLenum target, GLint level, GLenum internalFormat,
3337 GLint x, GLint y, GLsizei width, GLsizei height, GLint border )
3338 {
3339 struct gl_texture_object *texObj;
3340 struct gl_texture_image *texImage;
3341 const GLuint face = _mesa_tex_target_to_face(target);
3342
3343 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3344
3345 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
3346 _mesa_debug(ctx, "glCopyTexImage%uD %s %d %s %d %d %d %d %d\n",
3347 dims,
3348 _mesa_lookup_enum_by_nr(target), level,
3349 _mesa_lookup_enum_by_nr(internalFormat),
3350 x, y, width, height, border);
3351
3352 if (ctx->NewState & NEW_COPY_TEX_STATE)
3353 _mesa_update_state(ctx);
3354
3355 if (copytexture_error_check(ctx, dims, target, level, internalFormat,
3356 width, height, border))
3357 return;
3358
3359 texObj = _mesa_get_current_tex_object(ctx, target);
3360
3361 if (border && ctx->Const.StripTextureBorder) {
3362 x += border;
3363 width -= border * 2;
3364 if (dims == 2) {
3365 y += border;
3366 height -= border * 2;
3367 }
3368 border = 0;
3369 }
3370
3371 _mesa_lock_texture(ctx, texObj);
3372 {
3373 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
3374
3375 if (!texImage) {
3376 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexImage%uD", dims);
3377 }
3378 else {
3379 /* choose actual hw format */
3380 gl_format texFormat = _mesa_choose_texture_format(ctx, texObj,
3381 target, level,
3382 internalFormat,
3383 GL_NONE, GL_NONE);
3384
3385 if (legal_texture_size(ctx, texFormat, width, height, 1)) {
3386 GLint srcX = x, srcY = y, dstX = 0, dstY = 0, dstZ = 0;
3387
3388 /* Free old texture image */
3389 ctx->Driver.FreeTextureImageBuffer(ctx, texImage);
3390
3391 _mesa_init_teximage_fields(ctx, texImage, width, height, 1,
3392 border, internalFormat, texFormat);
3393
3394 /* Allocate texture memory (no pixel data yet) */
3395 ctx->Driver.TexImage(ctx, dims, texImage,
3396 GL_NONE, GL_NONE,
3397 NULL, &ctx->Unpack);
3398
3399 if (_mesa_clip_copytexsubimage(ctx, &dstX, &dstY, &srcX, &srcY,
3400 &width, &height)) {
3401 struct gl_renderbuffer *srcRb =
3402 get_copy_tex_image_source(ctx, texImage->TexFormat);
3403
3404 ctx->Driver.CopyTexSubImage(ctx, dims, texImage, dstX, dstY, dstZ,
3405 srcRb, srcX, srcY, width, height);
3406 }
3407
3408 check_gen_mipmap(ctx, target, texObj, level);
3409
3410 _mesa_update_fbo_texture(ctx, texObj, face, level);
3411
3412 _mesa_dirty_texobj(ctx, texObj, GL_TRUE);
3413 }
3414 else {
3415 /* probably too large of image */
3416 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexImage%uD", dims);
3417 }
3418 }
3419 }
3420 _mesa_unlock_texture(ctx, texObj);
3421 }
3422
3423
3424
3425 void GLAPIENTRY
3426 _mesa_CopyTexImage1D( GLenum target, GLint level,
3427 GLenum internalFormat,
3428 GLint x, GLint y,
3429 GLsizei width, GLint border )
3430 {
3431 GET_CURRENT_CONTEXT(ctx);
3432 copyteximage(ctx, 1, target, level, internalFormat, x, y, width, 1, border);
3433 }
3434
3435
3436
3437 void GLAPIENTRY
3438 _mesa_CopyTexImage2D( GLenum target, GLint level, GLenum internalFormat,
3439 GLint x, GLint y, GLsizei width, GLsizei height,
3440 GLint border )
3441 {
3442 GET_CURRENT_CONTEXT(ctx);
3443 copyteximage(ctx, 2, target, level, internalFormat,
3444 x, y, width, height, border);
3445 }
3446
3447
3448
3449 /**
3450 * Implementation for glCopyTexSubImage1/2/3D() functions.
3451 */
3452 static void
3453 copytexsubimage(struct gl_context *ctx, GLuint dims, GLenum target, GLint level,
3454 GLint xoffset, GLint yoffset, GLint zoffset,
3455 GLint x, GLint y, GLsizei width, GLsizei height)
3456 {
3457 struct gl_texture_object *texObj;
3458 struct gl_texture_image *texImage;
3459
3460 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3461
3462 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
3463 _mesa_debug(ctx, "glCopyTexSubImage%uD %s %d %d %d %d %d %d %d %d\n",
3464 dims,
3465 _mesa_lookup_enum_by_nr(target),
3466 level, xoffset, yoffset, zoffset, x, y, width, height);
3467
3468 if (ctx->NewState & NEW_COPY_TEX_STATE)
3469 _mesa_update_state(ctx);
3470
3471 if (copytexsubimage_error_check1(ctx, dims, target, level))
3472 return;
3473
3474 texObj = _mesa_get_current_tex_object(ctx, target);
3475
3476 _mesa_lock_texture(ctx, texObj);
3477 {
3478 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
3479
3480 if (copytexsubimage_error_check2(ctx, dims, target, level, xoffset, yoffset,
3481 zoffset, width, height, texImage)) {
3482 /* error was recored */
3483 }
3484 else {
3485 /* If we have a border, offset=-1 is legal. Bias by border width. */
3486 switch (dims) {
3487 case 3:
3488 if (target != GL_TEXTURE_2D_ARRAY)
3489 zoffset += texImage->Border;
3490 /* fall-through */
3491 case 2:
3492 if (target != GL_TEXTURE_1D_ARRAY)
3493 yoffset += texImage->Border;
3494 /* fall-through */
3495 case 1:
3496 xoffset += texImage->Border;
3497 }
3498
3499 if (_mesa_clip_copytexsubimage(ctx, &xoffset, &yoffset, &x, &y,
3500 &width, &height)) {
3501 struct gl_renderbuffer *srcRb =
3502 get_copy_tex_image_source(ctx, texImage->TexFormat);
3503
3504 ctx->Driver.CopyTexSubImage(ctx, dims, texImage,
3505 xoffset, yoffset, zoffset,
3506 srcRb, x, y, width, height);
3507
3508 check_gen_mipmap(ctx, target, texObj, level);
3509
3510 ctx->NewState |= _NEW_TEXTURE;
3511 }
3512 }
3513 }
3514 _mesa_unlock_texture(ctx, texObj);
3515 }
3516
3517
3518 void GLAPIENTRY
3519 _mesa_CopyTexSubImage1D( GLenum target, GLint level,
3520 GLint xoffset, GLint x, GLint y, GLsizei width )
3521 {
3522 GET_CURRENT_CONTEXT(ctx);
3523 copytexsubimage(ctx, 1, target, level, xoffset, 0, 0, x, y, width, 1);
3524 }
3525
3526
3527
3528 void GLAPIENTRY
3529 _mesa_CopyTexSubImage2D( GLenum target, GLint level,
3530 GLint xoffset, GLint yoffset,
3531 GLint x, GLint y, GLsizei width, GLsizei height )
3532 {
3533 GET_CURRENT_CONTEXT(ctx);
3534 copytexsubimage(ctx, 2, target, level, xoffset, yoffset, 0, x, y,
3535 width, height);
3536 }
3537
3538
3539
3540 void GLAPIENTRY
3541 _mesa_CopyTexSubImage3D( GLenum target, GLint level,
3542 GLint xoffset, GLint yoffset, GLint zoffset,
3543 GLint x, GLint y, GLsizei width, GLsizei height )
3544 {
3545 GET_CURRENT_CONTEXT(ctx);
3546 copytexsubimage(ctx, 3, target, level, xoffset, yoffset, zoffset,
3547 x, y, width, height);
3548 }
3549
3550
3551
3552
3553 /**********************************************************************/
3554 /****** Compressed Textures ******/
3555 /**********************************************************************/
3556
3557
3558 /**
3559 * Error checking for glCompressedTexSubImage[123]D().
3560 * \warning There are some bad assumptions here about the size of compressed
3561 * texture tiles (multiple of 4) used to test the validity of the
3562 * offset and size parameters.
3563 * \return error code or GL_NO_ERROR.
3564 */
3565 static GLenum
3566 compressed_subtexture_error_check(struct gl_context *ctx, GLint dimensions,
3567 GLenum target, GLint level,
3568 GLint xoffset, GLint yoffset, GLint zoffset,
3569 GLsizei width, GLsizei height, GLsizei depth,
3570 GLenum format, GLsizei imageSize)
3571 {
3572 GLint expectedSize, maxLevels = 0, maxTextureSize;
3573 GLuint bw, bh;
3574 (void) zoffset;
3575
3576 if (dimensions == 1) {
3577 /* 1D compressed textures not allowed */
3578 return GL_INVALID_ENUM;
3579 }
3580 else if (dimensions == 2) {
3581 if (target == GL_PROXY_TEXTURE_2D) {
3582 maxLevels = ctx->Const.MaxTextureLevels;
3583 }
3584 else if (target == GL_TEXTURE_2D) {
3585 maxLevels = ctx->Const.MaxTextureLevels;
3586 }
3587 else if (target == GL_PROXY_TEXTURE_CUBE_MAP_ARB) {
3588 if (!ctx->Extensions.ARB_texture_cube_map)
3589 return GL_INVALID_ENUM; /*target*/
3590 maxLevels = ctx->Const.MaxCubeTextureLevels;
3591 }
3592 else if (_mesa_is_cube_face(target)) {
3593 if (!ctx->Extensions.ARB_texture_cube_map)
3594 return GL_INVALID_ENUM; /*target*/
3595 maxLevels = ctx->Const.MaxCubeTextureLevels;
3596 }
3597 else {
3598 return GL_INVALID_ENUM; /*target*/
3599 }
3600 }
3601 else if (dimensions == 3) {
3602 /* 3D compressed textures not allowed */
3603 return GL_INVALID_ENUM;
3604 }
3605
3606 maxTextureSize = 1 << (maxLevels - 1);
3607
3608 /* this will catch any invalid compressed format token */
3609 if (!_mesa_is_compressed_format(ctx, format))
3610 return GL_INVALID_ENUM;
3611
3612 if (width < 1 || width > maxTextureSize)
3613 return GL_INVALID_VALUE;
3614
3615 if ((height < 1 || height > maxTextureSize)
3616 && dimensions > 1)
3617 return GL_INVALID_VALUE;
3618
3619 if (level < 0 || level >= maxLevels)
3620 return GL_INVALID_VALUE;
3621
3622 /*
3623 * do checks which depend on compression block size
3624 */
3625 get_compressed_block_size(format, &bw, &bh);
3626
3627 if ((xoffset % bw != 0) || (yoffset % bh != 0))
3628 return GL_INVALID_VALUE;
3629
3630 if ((width % bw != 0) && width != 2 && width != 1)
3631 return GL_INVALID_VALUE;
3632
3633 if ((height % bh != 0) && height != 2 && height != 1)
3634 return GL_INVALID_VALUE;
3635
3636 expectedSize = compressed_tex_size(width, height, depth, format);
3637 if (expectedSize != imageSize)
3638 return GL_INVALID_VALUE;
3639
3640 return GL_NO_ERROR;
3641 }
3642
3643
3644 /**
3645 * Do second part of glCompressedTexSubImage error checking.
3646 * \return GL_TRUE if error found, GL_FALSE otherwise.
3647 */
3648 static GLboolean
3649 compressed_subtexture_error_check2(struct gl_context *ctx, GLuint dims,
3650 GLsizei width, GLsizei height,
3651 GLsizei depth, GLenum format,
3652 struct gl_texture_image *texImage)
3653 {
3654
3655 if ((GLint) format != texImage->InternalFormat) {
3656 _mesa_error(ctx, GL_INVALID_OPERATION,
3657 "glCompressedTexSubImage%uD(format=0x%x)", dims, format);
3658 return GL_TRUE;
3659 }
3660
3661 if (compressedteximage_only_format(ctx, format)) {
3662 _mesa_error(ctx, GL_INVALID_OPERATION,
3663 "glCompressedTexSubImage%uD(format=0x%x cannot be updated)"
3664 , dims, format);
3665 return GL_TRUE;
3666 }
3667
3668 if (((width == 1 || width == 2) &&
3669 width != (GLsizei) texImage->Width) ||
3670 (width > (GLsizei) texImage->Width)) {
3671 _mesa_error(ctx, GL_INVALID_VALUE,
3672 "glCompressedTexSubImage%uD(width=%d)", dims, width);
3673 return GL_TRUE;
3674 }
3675
3676 if (dims >= 2) {
3677 if (((height == 1 || height == 2) &&
3678 height != (GLsizei) texImage->Height) ||
3679 (height > (GLsizei) texImage->Height)) {
3680 _mesa_error(ctx, GL_INVALID_VALUE,
3681 "glCompressedTexSubImage%uD(height=%d)", dims, height);
3682 return GL_TRUE;
3683 }
3684 }
3685
3686 if (dims >= 3) {
3687 if (((depth == 1 || depth == 2) &&
3688 depth != (GLsizei) texImage->Depth) ||
3689 (depth > (GLsizei) texImage->Depth)) {
3690 _mesa_error(ctx, GL_INVALID_VALUE,
3691 "glCompressedTexSubImage%uD(depth=%d)", dims, depth);
3692 return GL_TRUE;
3693 }
3694 }
3695
3696 return GL_FALSE;
3697 }
3698
3699
3700 void GLAPIENTRY
3701 _mesa_CompressedTexImage1DARB(GLenum target, GLint level,
3702 GLenum internalFormat, GLsizei width,
3703 GLint border, GLsizei imageSize,
3704 const GLvoid *data)
3705 {
3706 GET_CURRENT_CONTEXT(ctx);
3707 teximage(ctx, GL_TRUE, 1, target, level, internalFormat,
3708 width, 1, 1, border, GL_NONE, GL_NONE, imageSize, data);
3709 }
3710
3711
3712 void GLAPIENTRY
3713 _mesa_CompressedTexImage2DARB(GLenum target, GLint level,
3714 GLenum internalFormat, GLsizei width,
3715 GLsizei height, GLint border, GLsizei imageSize,
3716 const GLvoid *data)
3717 {
3718 GET_CURRENT_CONTEXT(ctx);
3719 teximage(ctx, GL_TRUE, 2, target, level, internalFormat,
3720 width, height, 1, border, GL_NONE, GL_NONE, imageSize, data);
3721 }
3722
3723
3724 void GLAPIENTRY
3725 _mesa_CompressedTexImage3DARB(GLenum target, GLint level,
3726 GLenum internalFormat, GLsizei width,
3727 GLsizei height, GLsizei depth, GLint border,
3728 GLsizei imageSize, const GLvoid *data)
3729 {
3730 GET_CURRENT_CONTEXT(ctx);
3731 teximage(ctx, GL_TRUE, 3, target, level, internalFormat,
3732 width, height, depth, border, GL_NONE, GL_NONE, imageSize, data);
3733 }
3734
3735
3736 /**
3737 * Common helper for glCompressedTexSubImage1/2/3D().
3738 */
3739 static void
3740 compressed_tex_sub_image(GLuint dims, GLenum target, GLint level,
3741 GLint xoffset, GLint yoffset, GLint zoffset,
3742 GLsizei width, GLsizei height, GLsizei depth,
3743 GLenum format, GLsizei imageSize, const GLvoid *data)
3744 {
3745 struct gl_texture_object *texObj;
3746 struct gl_texture_image *texImage;
3747 GLenum error;
3748 GET_CURRENT_CONTEXT(ctx);
3749 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3750
3751 error = compressed_subtexture_error_check(ctx, dims, target, level,
3752 xoffset, 0, 0, /* pos */
3753 width, height, depth, /* size */
3754 format, imageSize);
3755 if (error) {
3756 _mesa_error(ctx, error, "glCompressedTexSubImage%uD", dims);
3757 return;
3758 }
3759
3760 texObj = _mesa_get_current_tex_object(ctx, target);
3761
3762 _mesa_lock_texture(ctx, texObj);
3763 {
3764 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
3765 assert(texImage);
3766
3767 if (compressed_subtexture_error_check2(ctx, dims, width, height, depth,
3768 format, texImage)) {
3769 /* error was recorded */
3770 }
3771 else if (width > 0 && height > 0 && depth > 0) {
3772 ctx->Driver.CompressedTexSubImage(ctx, dims, texImage,
3773 xoffset, yoffset, zoffset,
3774 width, height, depth,
3775 format, imageSize, data);
3776
3777 check_gen_mipmap(ctx, target, texObj, level);
3778
3779 ctx->NewState |= _NEW_TEXTURE;
3780 }
3781 }
3782 _mesa_unlock_texture(ctx, texObj);
3783 }
3784
3785
3786 void GLAPIENTRY
3787 _mesa_CompressedTexSubImage1DARB(GLenum target, GLint level, GLint xoffset,
3788 GLsizei width, GLenum format,
3789 GLsizei imageSize, const GLvoid *data)
3790 {
3791 compressed_tex_sub_image(1, target, level, xoffset, 0, 0, width, 1, 1,
3792 format, imageSize, data);
3793 }
3794
3795
3796 void GLAPIENTRY
3797 _mesa_CompressedTexSubImage2DARB(GLenum target, GLint level, GLint xoffset,
3798 GLint yoffset, GLsizei width, GLsizei height,
3799 GLenum format, GLsizei imageSize,
3800 const GLvoid *data)
3801 {
3802 compressed_tex_sub_image(2, target, level, xoffset, yoffset, 0,
3803 width, height, 1, format, imageSize, data);
3804 }
3805
3806
3807 void GLAPIENTRY
3808 _mesa_CompressedTexSubImage3DARB(GLenum target, GLint level, GLint xoffset,
3809 GLint yoffset, GLint zoffset, GLsizei width,
3810 GLsizei height, GLsizei depth, GLenum format,
3811 GLsizei imageSize, const GLvoid *data)
3812 {
3813 compressed_tex_sub_image(3, target, level, xoffset, yoffset, zoffset,
3814 width, height, depth, format, imageSize, data);
3815 }
3816
3817 static gl_format
3818 get_texbuffer_format(const struct gl_context *ctx, GLenum internalFormat)
3819 {
3820 switch (internalFormat) {
3821 case GL_ALPHA8:
3822 return MESA_FORMAT_A8;
3823 case GL_ALPHA16:
3824 return MESA_FORMAT_A16;
3825 case GL_ALPHA16F_ARB:
3826 return MESA_FORMAT_ALPHA_FLOAT16;
3827 case GL_ALPHA32F_ARB:
3828 return MESA_FORMAT_ALPHA_FLOAT32;
3829 case GL_ALPHA8I_EXT:
3830 return MESA_FORMAT_ALPHA_INT8;
3831 case GL_ALPHA16I_EXT:
3832 return MESA_FORMAT_ALPHA_INT16;
3833 case GL_ALPHA32I_EXT:
3834 return MESA_FORMAT_ALPHA_INT32;
3835 case GL_ALPHA8UI_EXT:
3836 return MESA_FORMAT_ALPHA_UINT8;
3837 case GL_ALPHA16UI_EXT:
3838 return MESA_FORMAT_ALPHA_UINT16;
3839 case GL_ALPHA32UI_EXT:
3840 return MESA_FORMAT_ALPHA_UINT32;
3841 case GL_LUMINANCE8:
3842 return MESA_FORMAT_L8;
3843 case GL_LUMINANCE16:
3844 return MESA_FORMAT_L16;
3845 case GL_LUMINANCE16F_ARB:
3846 return MESA_FORMAT_LUMINANCE_FLOAT16;
3847 case GL_LUMINANCE32F_ARB:
3848 return MESA_FORMAT_LUMINANCE_FLOAT32;
3849 case GL_LUMINANCE8I_EXT:
3850 return MESA_FORMAT_LUMINANCE_INT8;
3851 case GL_LUMINANCE16I_EXT:
3852 return MESA_FORMAT_LUMINANCE_INT16;
3853 case GL_LUMINANCE32I_EXT:
3854 return MESA_FORMAT_LUMINANCE_INT32;
3855 case GL_LUMINANCE8UI_EXT:
3856 return MESA_FORMAT_LUMINANCE_UINT8;
3857 case GL_LUMINANCE16UI_EXT:
3858 return MESA_FORMAT_LUMINANCE_UINT16;
3859 case GL_LUMINANCE32UI_EXT:
3860 return MESA_FORMAT_LUMINANCE_UINT32;
3861 case GL_LUMINANCE8_ALPHA8:
3862 return MESA_FORMAT_AL88;
3863 case GL_LUMINANCE16_ALPHA16:
3864 return MESA_FORMAT_AL1616;
3865 case GL_LUMINANCE_ALPHA16F_ARB:
3866 return MESA_FORMAT_LUMINANCE_ALPHA_FLOAT16;
3867 case GL_LUMINANCE_ALPHA32F_ARB:
3868 return MESA_FORMAT_LUMINANCE_ALPHA_FLOAT32;
3869 case GL_LUMINANCE_ALPHA8I_EXT:
3870 return MESA_FORMAT_LUMINANCE_ALPHA_INT8;
3871 case GL_LUMINANCE_ALPHA16I_EXT:
3872 return MESA_FORMAT_LUMINANCE_ALPHA_INT8;
3873 case GL_LUMINANCE_ALPHA32I_EXT:
3874 return MESA_FORMAT_LUMINANCE_ALPHA_INT16;
3875 case GL_LUMINANCE_ALPHA8UI_EXT:
3876 return MESA_FORMAT_LUMINANCE_ALPHA_UINT8;
3877 case GL_LUMINANCE_ALPHA16UI_EXT:
3878 return MESA_FORMAT_LUMINANCE_ALPHA_UINT16;
3879 case GL_LUMINANCE_ALPHA32UI_EXT:
3880 return MESA_FORMAT_LUMINANCE_ALPHA_UINT32;
3881 case GL_INTENSITY8:
3882 return MESA_FORMAT_I8;
3883 case GL_INTENSITY16:
3884 return MESA_FORMAT_I16;
3885 case GL_INTENSITY16F_ARB:
3886 return MESA_FORMAT_INTENSITY_FLOAT16;
3887 case GL_INTENSITY32F_ARB:
3888 return MESA_FORMAT_INTENSITY_FLOAT32;
3889 case GL_INTENSITY8I_EXT:
3890 return MESA_FORMAT_INTENSITY_INT8;
3891 case GL_INTENSITY16I_EXT:
3892 return MESA_FORMAT_INTENSITY_INT16;
3893 case GL_INTENSITY32I_EXT:
3894 return MESA_FORMAT_INTENSITY_INT32;
3895 case GL_INTENSITY8UI_EXT:
3896 return MESA_FORMAT_INTENSITY_UINT8;
3897 case GL_INTENSITY16UI_EXT:
3898 return MESA_FORMAT_INTENSITY_UINT16;
3899 case GL_INTENSITY32UI_EXT:
3900 return MESA_FORMAT_INTENSITY_UINT32;
3901 case GL_RGBA8:
3902 return MESA_FORMAT_RGBA8888_REV;
3903 case GL_RGBA16:
3904 return MESA_FORMAT_RGBA_16;
3905 case GL_RGBA16F_ARB:
3906 return MESA_FORMAT_RGBA_FLOAT16;
3907 case GL_RGBA32F_ARB:
3908 return MESA_FORMAT_RGBA_FLOAT32;
3909 case GL_RGBA8I_EXT:
3910 return MESA_FORMAT_RGBA_INT8;
3911 case GL_RGBA16I_EXT:
3912 return MESA_FORMAT_RGBA_INT16;
3913 case GL_RGBA32I_EXT:
3914 return MESA_FORMAT_RGBA_INT32;
3915 case GL_RGBA8UI_EXT:
3916 return MESA_FORMAT_RGBA_UINT8;
3917 case GL_RGBA16UI_EXT:
3918 return MESA_FORMAT_RGBA_UINT16;
3919 case GL_RGBA32UI_EXT:
3920 return MESA_FORMAT_RGBA_UINT32;
3921
3922 case GL_RG8:
3923 return MESA_FORMAT_GR88;
3924 case GL_RG16:
3925 return MESA_FORMAT_RG1616;
3926 case GL_RG16F:
3927 return MESA_FORMAT_RG_FLOAT16;
3928 case GL_RG32F:
3929 return MESA_FORMAT_RG_FLOAT32;
3930 case GL_RG8I:
3931 return MESA_FORMAT_RG_INT8;
3932 case GL_RG16I:
3933 return MESA_FORMAT_RG_INT16;
3934 case GL_RG32I:
3935 return MESA_FORMAT_RG_INT32;
3936 case GL_RG8UI:
3937 return MESA_FORMAT_RG_UINT8;
3938 case GL_RG16UI:
3939 return MESA_FORMAT_RG_UINT16;
3940 case GL_RG32UI:
3941 return MESA_FORMAT_RG_UINT32;
3942
3943 case GL_R8:
3944 return MESA_FORMAT_R8;
3945 case GL_R16:
3946 return MESA_FORMAT_R16;
3947 case GL_R16F:
3948 return MESA_FORMAT_R_FLOAT16;
3949 case GL_R32F:
3950 return MESA_FORMAT_R_FLOAT32;
3951 case GL_R8I:
3952 return MESA_FORMAT_R_INT8;
3953 case GL_R16I:
3954 return MESA_FORMAT_R_INT16;
3955 case GL_R32I:
3956 return MESA_FORMAT_R_INT32;
3957 case GL_R8UI:
3958 return MESA_FORMAT_R_UINT8;
3959 case GL_R16UI:
3960 return MESA_FORMAT_R_UINT16;
3961 case GL_R32UI:
3962 return MESA_FORMAT_R_UINT32;
3963
3964 default:
3965 return MESA_FORMAT_NONE;
3966 }
3967 }
3968
3969 static gl_format
3970 validate_texbuffer_format(const struct gl_context *ctx, GLenum internalFormat)
3971 {
3972 gl_format format = get_texbuffer_format(ctx, internalFormat);
3973 GLenum datatype;
3974
3975 if (format == MESA_FORMAT_NONE)
3976 return MESA_FORMAT_NONE;
3977
3978 datatype = _mesa_get_format_datatype(format);
3979 if (datatype == GL_FLOAT && !ctx->Extensions.ARB_texture_float)
3980 return MESA_FORMAT_NONE;
3981
3982 if (datatype == GL_HALF_FLOAT && !ctx->Extensions.ARB_half_float_pixel)
3983 return MESA_FORMAT_NONE;
3984
3985 /* The GL_ARB_texture_rg and GL_ARB_texture_buffer_object specs don't make
3986 * any mention of R/RG formats, but they appear in the GL 3.1 core
3987 * specification.
3988 */
3989 if (ctx->Version <= 30) {
3990 GLenum base_format = _mesa_get_format_base_format(format);
3991
3992 if (base_format == GL_R || base_format == GL_RG)
3993 return MESA_FORMAT_NONE;
3994 }
3995 return format;
3996 }
3997
3998
3999 /** GL_ARB_texture_buffer_object */
4000 void GLAPIENTRY
4001 _mesa_TexBuffer(GLenum target, GLenum internalFormat, GLuint buffer)
4002 {
4003 struct gl_texture_object *texObj;
4004 struct gl_buffer_object *bufObj;
4005 gl_format format;
4006
4007 GET_CURRENT_CONTEXT(ctx);
4008 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
4009
4010 if (!(ctx->Extensions.ARB_texture_buffer_object
4011 && _mesa_is_desktop_gl(ctx))) {
4012 _mesa_error(ctx, GL_INVALID_OPERATION, "glTexBuffer");
4013 return;
4014 }
4015
4016 if (target != GL_TEXTURE_BUFFER_ARB) {
4017 _mesa_error(ctx, GL_INVALID_ENUM, "glTexBuffer(target)");
4018 return;
4019 }
4020
4021 format = validate_texbuffer_format(ctx, internalFormat);
4022 if (format == MESA_FORMAT_NONE) {
4023 _mesa_error(ctx, GL_INVALID_ENUM, "glTexBuffer(internalFormat 0x%x)",
4024 internalFormat);
4025 return;
4026 }
4027
4028 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
4029 if (buffer && !bufObj) {
4030 _mesa_error(ctx, GL_INVALID_OPERATION, "glTexBuffer(buffer %u)", buffer);
4031 return;
4032 }
4033
4034 texObj = _mesa_get_current_tex_object(ctx, target);
4035
4036 _mesa_lock_texture(ctx, texObj);
4037 {
4038 _mesa_reference_buffer_object(ctx, &texObj->BufferObject, bufObj);
4039 texObj->BufferObjectFormat = internalFormat;
4040 texObj->_BufferObjectFormat = format;
4041 }
4042 _mesa_unlock_texture(ctx, texObj);
4043 }