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