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