mesa: move _mesa_es_error_check_format_and_type() to glformats.c
[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 /**
1685 * Return expected size of a compressed texture.
1686 */
1687 static GLuint
1688 compressed_tex_size(GLsizei width, GLsizei height, GLsizei depth,
1689 GLenum glformat)
1690 {
1691 gl_format mesaFormat = _mesa_glenum_to_compressed_format(glformat);
1692 return _mesa_format_image_size(mesaFormat, width, height, depth);
1693 }
1694
1695
1696 /**
1697 * Test the glTexImage[123]D() parameters for errors.
1698 *
1699 * \param ctx GL context.
1700 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1701 * \param target texture target given by the user (already validated).
1702 * \param level image level given by the user.
1703 * \param internalFormat internal format given by the user.
1704 * \param format pixel data format given by the user.
1705 * \param type pixel data type given by the user.
1706 * \param width image width given by the user.
1707 * \param height image height given by the user.
1708 * \param depth image depth given by the user.
1709 * \param border image border given by the user.
1710 *
1711 * \return GL_TRUE if a error is found, GL_FALSE otherwise
1712 *
1713 * Verifies each of the parameters against the constants specified in
1714 * __struct gl_contextRec::Const and the supported extensions, and according
1715 * to the OpenGL specification.
1716 * Note that we don't fully error-check the width, height, depth values
1717 * here. That's done in _mesa_legal_texture_dimensions() which is used
1718 * by several other GL entrypoints. Plus, texture dims have a special
1719 * interaction with proxy textures.
1720 */
1721 static GLboolean
1722 texture_error_check( struct gl_context *ctx,
1723 GLuint dimensions, GLenum target,
1724 GLint level, GLint internalFormat,
1725 GLenum format, GLenum type,
1726 GLint width, GLint height,
1727 GLint depth, GLint border )
1728 {
1729 GLboolean colorFormat;
1730 GLenum err;
1731
1732 /* Even though there are no color-index textures, we still have to support
1733 * uploading color-index data and remapping it to RGB via the
1734 * GL_PIXEL_MAP_I_TO_[RGBA] tables.
1735 */
1736 const GLboolean indexFormat = (format == GL_COLOR_INDEX);
1737
1738 /* Note: for proxy textures, some error conditions immediately generate
1739 * a GL error in the usual way. But others do not generate a GL error.
1740 * Instead, they cause the width, height, depth, format fields of the
1741 * texture image to be zeroed-out. The GL spec seems to indicate that the
1742 * zero-out behaviour is only used in cases related to memory allocation.
1743 */
1744
1745 /* level check */
1746 if (level < 0 || level >= _mesa_max_texture_levels(ctx, target)) {
1747 _mesa_error(ctx, GL_INVALID_VALUE,
1748 "glTexImage%dD(level=%d)", dimensions, level);
1749 return GL_TRUE;
1750 }
1751
1752 /* Check border */
1753 if (border < 0 || border > 1 ||
1754 ((ctx->API != API_OPENGL ||
1755 target == GL_TEXTURE_RECTANGLE_NV ||
1756 target == GL_PROXY_TEXTURE_RECTANGLE_NV) && border != 0)) {
1757 _mesa_error(ctx, GL_INVALID_VALUE,
1758 "glTexImage%dD(border=%d)", dimensions, border);
1759 return GL_TRUE;
1760 }
1761
1762 if (width < 0 || height < 0 || depth < 0) {
1763 _mesa_error(ctx, GL_INVALID_VALUE,
1764 "glTexImage%dD(width, height or depth < 0)", dimensions);
1765 return GL_TRUE;
1766 }
1767
1768 /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
1769 * combinations of format, internalFormat, and type that can be used.
1770 * Formats and types that require additional extensions (e.g., GL_FLOAT
1771 * requires GL_OES_texture_float) are filtered elsewhere.
1772 */
1773 if (_mesa_is_gles(ctx) && !_mesa_is_gles3(ctx)) {
1774 if (format != internalFormat) {
1775 _mesa_error(ctx, GL_INVALID_OPERATION,
1776 "glTexImage%dD(format = %s, internalFormat = %s)",
1777 dimensions,
1778 _mesa_lookup_enum_by_nr(format),
1779 _mesa_lookup_enum_by_nr(internalFormat));
1780 return GL_TRUE;
1781 }
1782
1783 err = _mesa_es_error_check_format_and_type(format, type, dimensions);
1784 if (err != GL_NO_ERROR) {
1785 _mesa_error(ctx, err,
1786 "glTexImage%dD(format = %s, type = %s)",
1787 dimensions,
1788 _mesa_lookup_enum_by_nr(format),
1789 _mesa_lookup_enum_by_nr(type));
1790 return GL_TRUE;
1791 }
1792 }
1793
1794 if ((target == GL_PROXY_TEXTURE_CUBE_MAP_ARB ||
1795 _mesa_is_cube_face(target)) && width != height) {
1796 _mesa_error(ctx, GL_INVALID_VALUE,
1797 "glTexImage2D(cube width != height)");
1798 return GL_TRUE;
1799 }
1800
1801 /* Check internalFormat */
1802 if (_mesa_base_tex_format(ctx, internalFormat) < 0) {
1803 _mesa_error(ctx, GL_INVALID_VALUE,
1804 "glTexImage%dD(internalFormat=%s)",
1805 dimensions, _mesa_lookup_enum_by_nr(internalFormat));
1806 return GL_TRUE;
1807 }
1808
1809 /* Check incoming image format and type */
1810 err = _mesa_error_check_format_and_type(ctx, format, type);
1811 if (err != GL_NO_ERROR) {
1812 _mesa_error(ctx, err,
1813 "glTexImage%dD(incompatible format 0x%x, type 0x%x)",
1814 dimensions, format, type);
1815 return GL_TRUE;
1816 }
1817
1818 /* make sure internal format and format basically agree */
1819 colorFormat = _mesa_is_color_format(format);
1820 if ((_mesa_is_color_format(internalFormat) && !colorFormat && !indexFormat) ||
1821 (_mesa_is_depth_format(internalFormat) != _mesa_is_depth_format(format)) ||
1822 (_mesa_is_ycbcr_format(internalFormat) != _mesa_is_ycbcr_format(format)) ||
1823 (_mesa_is_depthstencil_format(internalFormat) != _mesa_is_depthstencil_format(format)) ||
1824 (_mesa_is_dudv_format(internalFormat) != _mesa_is_dudv_format(format))) {
1825 _mesa_error(ctx, GL_INVALID_OPERATION,
1826 "glTexImage%dD(incompatible internalFormat 0x%x, format 0x%x)",
1827 dimensions, internalFormat, format);
1828 return GL_TRUE;
1829 }
1830
1831 /* additional checks for ycbcr textures */
1832 if (internalFormat == GL_YCBCR_MESA) {
1833 ASSERT(ctx->Extensions.MESA_ycbcr_texture);
1834 if (type != GL_UNSIGNED_SHORT_8_8_MESA &&
1835 type != GL_UNSIGNED_SHORT_8_8_REV_MESA) {
1836 char message[100];
1837 _mesa_snprintf(message, sizeof(message),
1838 "glTexImage%dD(format/type YCBCR mismatch)",
1839 dimensions);
1840 _mesa_error(ctx, GL_INVALID_ENUM, "%s", message);
1841 return GL_TRUE; /* error */
1842 }
1843 if (target != GL_TEXTURE_2D &&
1844 target != GL_PROXY_TEXTURE_2D &&
1845 target != GL_TEXTURE_RECTANGLE_NV &&
1846 target != GL_PROXY_TEXTURE_RECTANGLE_NV) {
1847 _mesa_error(ctx, GL_INVALID_ENUM,
1848 "glTexImage%dD(bad target for YCbCr texture)",
1849 dimensions);
1850 return GL_TRUE;
1851 }
1852 if (border != 0) {
1853 char message[100];
1854 _mesa_snprintf(message, sizeof(message),
1855 "glTexImage%dD(format=GL_YCBCR_MESA and border=%d)",
1856 dimensions, border);
1857 _mesa_error(ctx, GL_INVALID_VALUE, "%s", message);
1858 return GL_TRUE;
1859 }
1860 }
1861
1862 /* additional checks for depth textures */
1863 if (_mesa_base_tex_format(ctx, internalFormat) == GL_DEPTH_COMPONENT) {
1864 /* Only 1D, 2D, rect, array and cube textures supported, not 3D
1865 * Cubemaps are only supported for GL version > 3.0 or with EXT_gpu_shader4 */
1866 if (target != GL_TEXTURE_1D &&
1867 target != GL_PROXY_TEXTURE_1D &&
1868 target != GL_TEXTURE_2D &&
1869 target != GL_PROXY_TEXTURE_2D &&
1870 target != GL_TEXTURE_1D_ARRAY &&
1871 target != GL_PROXY_TEXTURE_1D_ARRAY &&
1872 target != GL_TEXTURE_2D_ARRAY &&
1873 target != GL_PROXY_TEXTURE_2D_ARRAY &&
1874 target != GL_TEXTURE_RECTANGLE_ARB &&
1875 target != GL_PROXY_TEXTURE_RECTANGLE_ARB &&
1876 !((_mesa_is_cube_face(target) || target == GL_PROXY_TEXTURE_CUBE_MAP) &&
1877 (ctx->Version >= 30 || ctx->Extensions.EXT_gpu_shader4))) {
1878 _mesa_error(ctx, GL_INVALID_ENUM,
1879 "glTexImage%dD(bad target for depth texture)",
1880 dimensions);
1881 return GL_TRUE;
1882 }
1883 }
1884
1885 /* additional checks for compressed textures */
1886 if (_mesa_is_compressed_format(ctx, internalFormat)) {
1887 if (!target_can_be_compressed(ctx, target, internalFormat)) {
1888 _mesa_error(ctx, GL_INVALID_ENUM,
1889 "glTexImage%dD(target can't be compressed)", dimensions);
1890 return GL_TRUE;
1891 }
1892 if (compressedteximage_only_format(ctx, internalFormat)) {
1893 _mesa_error(ctx, GL_INVALID_OPERATION,
1894 "glTexImage%dD(no compression for format)", dimensions);
1895 return GL_TRUE;
1896 }
1897 if (border != 0) {
1898 _mesa_error(ctx, GL_INVALID_OPERATION,
1899 "glTexImage%dD(border!=0)", dimensions);
1900 return GL_TRUE;
1901 }
1902 }
1903
1904 /* additional checks for integer textures */
1905 if ((ctx->Version >= 30 || ctx->Extensions.EXT_texture_integer) &&
1906 (_mesa_is_enum_format_integer(format) !=
1907 _mesa_is_enum_format_integer(internalFormat))) {
1908 _mesa_error(ctx, GL_INVALID_OPERATION,
1909 "glTexImage%dD(integer/non-integer format mismatch)",
1910 dimensions);
1911 return GL_TRUE;
1912 }
1913
1914 if (!mutable_tex_object(ctx, target)) {
1915 _mesa_error(ctx, GL_INVALID_OPERATION,
1916 "glTexImage%dD(immutable texture)", dimensions);
1917 return GL_TRUE;
1918 }
1919
1920 /* if we get here, the parameters are OK */
1921 return GL_FALSE;
1922 }
1923
1924
1925 /**
1926 * Error checking for glCompressedTexImage[123]D().
1927 * Note that the width, height and depth values are not fully error checked
1928 * here.
1929 * \return GL_TRUE if a error is found, GL_FALSE otherwise
1930 */
1931 static GLenum
1932 compressed_texture_error_check(struct gl_context *ctx, GLint dimensions,
1933 GLenum target, GLint level,
1934 GLenum internalFormat, GLsizei width,
1935 GLsizei height, GLsizei depth, GLint border,
1936 GLsizei imageSize)
1937 {
1938 const GLint maxLevels = _mesa_max_texture_levels(ctx, target);
1939 GLint expectedSize;
1940 GLenum choose_format;
1941 GLenum choose_type;
1942 GLenum proxy_format;
1943 GLenum error = GL_NO_ERROR;
1944 char *reason = ""; /* no error */
1945
1946 if (!target_can_be_compressed(ctx, target, internalFormat)) {
1947 reason = "target";
1948 error = GL_INVALID_ENUM;
1949 goto error;
1950 }
1951
1952 /* This will detect any invalid internalFormat value */
1953 if (!_mesa_is_compressed_format(ctx, internalFormat)) {
1954 reason = "internalFormat";
1955 error = GL_INVALID_ENUM;
1956 goto error;
1957 }
1958
1959 switch (internalFormat) {
1960 #if FEATURE_ES
1961 case GL_PALETTE4_RGB8_OES:
1962 case GL_PALETTE4_RGBA8_OES:
1963 case GL_PALETTE4_R5_G6_B5_OES:
1964 case GL_PALETTE4_RGBA4_OES:
1965 case GL_PALETTE4_RGB5_A1_OES:
1966 case GL_PALETTE8_RGB8_OES:
1967 case GL_PALETTE8_RGBA8_OES:
1968 case GL_PALETTE8_R5_G6_B5_OES:
1969 case GL_PALETTE8_RGBA4_OES:
1970 case GL_PALETTE8_RGB5_A1_OES:
1971 _mesa_cpal_compressed_format_type(internalFormat, &choose_format,
1972 &choose_type);
1973 proxy_format = choose_format;
1974
1975 /* check level (note that level should be zero or less!) */
1976 if (level > 0 || level < -maxLevels) {
1977 reason = "level";
1978 error = GL_INVALID_VALUE;
1979 goto error;
1980 }
1981
1982 if (dimensions != 2) {
1983 reason = "compressed paletted textures must be 2D";
1984 error = GL_INVALID_OPERATION;
1985 goto error;
1986 }
1987
1988 /* Figure out the expected texture size (in bytes). This will be
1989 * checked against the actual size later.
1990 */
1991 expectedSize = _mesa_cpal_compressed_size(level, internalFormat,
1992 width, height);
1993
1994 /* This is for the benefit of the TestProxyTexImage below. It expects
1995 * level to be non-negative. OES_compressed_paletted_texture uses a
1996 * weird mechanism where the level specified to glCompressedTexImage2D
1997 * is -(n-1) number of levels in the texture, and the data specifies the
1998 * complete mipmap stack. This is done to ensure the palette is the
1999 * same for all levels.
2000 */
2001 level = -level;
2002 break;
2003 #endif
2004
2005 default:
2006 choose_format = GL_NONE;
2007 choose_type = GL_NONE;
2008 proxy_format = internalFormat;
2009
2010 /* check level */
2011 if (level < 0 || level >= maxLevels) {
2012 reason = "level";
2013 error = GL_INVALID_VALUE;
2014 goto error;
2015 }
2016
2017 /* Figure out the expected texture size (in bytes). This will be
2018 * checked against the actual size later.
2019 */
2020 expectedSize = compressed_tex_size(width, height, depth, internalFormat);
2021 break;
2022 }
2023
2024 /* This should really never fail */
2025 if (_mesa_base_tex_format(ctx, internalFormat) < 0) {
2026 reason = "internalFormat";
2027 error = GL_INVALID_ENUM;
2028 goto error;
2029 }
2030
2031 /* No compressed formats support borders at this time */
2032 if (border != 0) {
2033 reason = "border != 0";
2034 error = GL_INVALID_VALUE;
2035 goto error;
2036 }
2037
2038 /* For cube map, width must equal height */
2039 if ((target == GL_PROXY_TEXTURE_CUBE_MAP_ARB ||
2040 _mesa_is_cube_face(target)) && width != height) {
2041 reason = "width != height";
2042 error = GL_INVALID_VALUE;
2043 goto error;
2044 }
2045
2046 /* check image size against compression block size */
2047 /* XXX possibly move this into the _mesa_legal_texture_dimensions() func */
2048 {
2049 gl_format texFormat =
2050 ctx->Driver.ChooseTextureFormat(ctx, target, proxy_format,
2051 choose_format, choose_type);
2052 GLuint bw, bh;
2053
2054 _mesa_get_format_block_size(texFormat, &bw, &bh);
2055 if ((width > bw && width % bw > 0) ||
2056 (height > bh && height % bh > 0)) {
2057 /*
2058 * Per GL_ARB_texture_compression: GL_INVALID_OPERATION is
2059 * generated [...] if any parameter combinations are not
2060 * supported by the specific compressed internal format.
2061 */
2062 reason = "invalid width or height for compression format";
2063 error = GL_INVALID_OPERATION;
2064 goto error;
2065 }
2066 }
2067
2068 /* check image size in bytes */
2069 if (expectedSize != imageSize) {
2070 /* Per GL_ARB_texture_compression: GL_INVALID_VALUE is generated [...]
2071 * if <imageSize> is not consistent with the format, dimensions, and
2072 * contents of the specified image.
2073 */
2074 reason = "imageSize inconsistant with width/height/format";
2075 error = GL_INVALID_VALUE;
2076 goto error;
2077 }
2078
2079 if (!mutable_tex_object(ctx, target)) {
2080 reason = "immutable texture";
2081 error = GL_INVALID_OPERATION;
2082 goto error;
2083 }
2084
2085 return GL_FALSE;
2086
2087 error:
2088 _mesa_error(ctx, error, "glCompressedTexImage%dD(%s)", dimensions, reason);
2089 return GL_TRUE;
2090 }
2091
2092
2093
2094 /**
2095 * Test glTexSubImage[123]D() parameters for errors.
2096 *
2097 * \param ctx GL context.
2098 * \param dimensions texture image dimensions (must be 1, 2 or 3).
2099 * \param target texture target given by the user (already validated)
2100 * \param level image level given by the user.
2101 * \param xoffset sub-image x offset given by the user.
2102 * \param yoffset sub-image y offset given by the user.
2103 * \param zoffset sub-image z offset given by the user.
2104 * \param format pixel data format given by the user.
2105 * \param type pixel data type given by the user.
2106 * \param width image width given by the user.
2107 * \param height image height given by the user.
2108 * \param depth image depth given by the user.
2109 *
2110 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2111 *
2112 * Verifies each of the parameters against the constants specified in
2113 * __struct gl_contextRec::Const and the supported extensions, and according
2114 * to the OpenGL specification.
2115 */
2116 static GLboolean
2117 texsubimage_error_check(struct gl_context *ctx, GLuint dimensions,
2118 GLenum target, GLint level,
2119 GLint xoffset, GLint yoffset, GLint zoffset,
2120 GLint width, GLint height, GLint depth,
2121 GLenum format, GLenum type)
2122 {
2123 struct gl_texture_object *texObj;
2124 struct gl_texture_image *texImage;
2125 GLenum err;
2126
2127 /* check target (proxies not allowed) */
2128 if (!legal_texsubimage_target(ctx, dimensions, target)) {
2129 _mesa_error(ctx, GL_INVALID_ENUM, "glTexSubImage%uD(target=%s)",
2130 dimensions, _mesa_lookup_enum_by_nr(target));
2131 return GL_TRUE;
2132 }
2133
2134 /* level check */
2135 if (level < 0 || level >= _mesa_max_texture_levels(ctx, target)) {
2136 _mesa_error(ctx, GL_INVALID_ENUM, "glTexSubImage%uD(level=%d)",
2137 dimensions, level);
2138 return GL_TRUE;
2139 }
2140
2141 /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
2142 * combinations of format and type that can be used. Formats and types
2143 * that require additional extensions (e.g., GL_FLOAT requires
2144 * GL_OES_texture_float) are filtered elsewhere.
2145 */
2146 if (_mesa_is_gles(ctx) && !_mesa_is_gles3(ctx)) {
2147 err = _mesa_es_error_check_format_and_type(format, type, dimensions);
2148 if (err != GL_NO_ERROR) {
2149 _mesa_error(ctx, err,
2150 "glTexSubImage%dD(format = %s, type = %s)",
2151 dimensions,
2152 _mesa_lookup_enum_by_nr(format),
2153 _mesa_lookup_enum_by_nr(type));
2154 return GL_TRUE;
2155 }
2156 }
2157
2158 err = _mesa_error_check_format_and_type(ctx, format, type);
2159 if (err != GL_NO_ERROR) {
2160 _mesa_error(ctx, err,
2161 "glTexSubImage%dD(incompatible format 0x%x, type 0x%x)",
2162 dimensions, format, type);
2163 return GL_TRUE;
2164 }
2165
2166 /* Get dest texture object / image pointers */
2167 texObj = _mesa_get_current_tex_object(ctx, target);
2168 if (!texObj) {
2169 /* must be out of memory */
2170 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexSubImage%dD()", dimensions);
2171 return GL_TRUE;
2172 }
2173
2174 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
2175 if (!texImage) {
2176 /* non-existant texture level */
2177 _mesa_error(ctx, GL_INVALID_OPERATION,
2178 "glTexSubImage%dD(invalid texture image)", dimensions);
2179 return GL_TRUE;
2180 }
2181
2182 if (error_check_subtexture_dimensions(ctx, "glTexSubImage", dimensions,
2183 texImage, xoffset, yoffset, 0,
2184 width, height, 1)) {
2185 return GL_TRUE;
2186 }
2187
2188 if (_mesa_is_format_compressed(texImage->TexFormat)) {
2189 if (compressedteximage_only_format(ctx, texImage->InternalFormat)) {
2190 _mesa_error(ctx, GL_INVALID_OPERATION,
2191 "glTexSubImage%dD(no compression for format)", dimensions);
2192 return GL_TRUE;
2193 }
2194 }
2195
2196 if (ctx->Version >= 30 || ctx->Extensions.EXT_texture_integer) {
2197 /* both source and dest must be integer-valued, or neither */
2198 if (_mesa_is_format_integer_color(texImage->TexFormat) !=
2199 _mesa_is_enum_format_integer(format)) {
2200 _mesa_error(ctx, GL_INVALID_OPERATION,
2201 "glTexSubImage%dD(integer/non-integer format mismatch)",
2202 dimensions);
2203 return GL_TRUE;
2204 }
2205 }
2206
2207 return GL_FALSE;
2208 }
2209
2210
2211 /**
2212 * Test glCopyTexImage[12]D() parameters for errors.
2213 *
2214 * \param ctx GL context.
2215 * \param dimensions texture image dimensions (must be 1, 2 or 3).
2216 * \param target texture target given by the user.
2217 * \param level image level given by the user.
2218 * \param internalFormat internal format given by the user.
2219 * \param width image width given by the user.
2220 * \param height image height given by the user.
2221 * \param border texture border.
2222 *
2223 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2224 *
2225 * Verifies each of the parameters against the constants specified in
2226 * __struct gl_contextRec::Const and the supported extensions, and according
2227 * to the OpenGL specification.
2228 */
2229 static GLboolean
2230 copytexture_error_check( struct gl_context *ctx, GLuint dimensions,
2231 GLenum target, GLint level, GLint internalFormat,
2232 GLint width, GLint height, GLint border )
2233 {
2234 GLint baseFormat;
2235
2236 /* check target */
2237 if (!legal_texsubimage_target(ctx, dimensions, target)) {
2238 _mesa_error(ctx, GL_INVALID_ENUM, "glCopyTexImage%uD(target=%s)",
2239 dimensions, _mesa_lookup_enum_by_nr(target));
2240 return GL_TRUE;
2241 }
2242
2243 /* level check */
2244 if (level < 0 || level >= _mesa_max_texture_levels(ctx, target)) {
2245 _mesa_error(ctx, GL_INVALID_VALUE,
2246 "glCopyTexImage%dD(level=%d)", dimensions, level);
2247 return GL_TRUE;
2248 }
2249
2250 /* Check that the source buffer is complete */
2251 if (_mesa_is_user_fbo(ctx->ReadBuffer)) {
2252 if (ctx->ReadBuffer->_Status == 0) {
2253 _mesa_test_framebuffer_completeness(ctx, ctx->ReadBuffer);
2254 }
2255 if (ctx->ReadBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
2256 _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
2257 "glCopyTexImage%dD(invalid readbuffer)", dimensions);
2258 return GL_TRUE;
2259 }
2260
2261 if (ctx->ReadBuffer->Visual.samples > 0) {
2262 _mesa_error(ctx, GL_INVALID_OPERATION,
2263 "glCopyTexImage%dD(multisample FBO)",
2264 dimensions);
2265 return GL_TRUE;
2266 }
2267 }
2268
2269 /* Check border */
2270 if (border < 0 || border > 1 ||
2271 ((ctx->API != API_OPENGL ||
2272 target == GL_TEXTURE_RECTANGLE_NV ||
2273 target == GL_PROXY_TEXTURE_RECTANGLE_NV) && border != 0)) {
2274 _mesa_error(ctx, GL_INVALID_VALUE,
2275 "glCopyTexImage%dD(border=%d)", dimensions, border);
2276 return GL_TRUE;
2277 }
2278
2279 /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
2280 * internalFormat.
2281 */
2282 if (_mesa_is_gles(ctx) && !_mesa_is_gles3(ctx)) {
2283 switch (internalFormat) {
2284 case GL_ALPHA:
2285 case GL_RGB:
2286 case GL_RGBA:
2287 case GL_LUMINANCE:
2288 case GL_LUMINANCE_ALPHA:
2289 break;
2290 default:
2291 _mesa_error(ctx, GL_INVALID_VALUE,
2292 "glCopyTexImage%dD(internalFormat)", dimensions);
2293 return GL_TRUE;
2294 }
2295 }
2296
2297 baseFormat = _mesa_base_tex_format(ctx, internalFormat);
2298 if (baseFormat < 0) {
2299 _mesa_error(ctx, GL_INVALID_VALUE,
2300 "glCopyTexImage%dD(internalFormat)", dimensions);
2301 return GL_TRUE;
2302 }
2303
2304 if (!_mesa_source_buffer_exists(ctx, baseFormat)) {
2305 _mesa_error(ctx, GL_INVALID_OPERATION,
2306 "glCopyTexImage%dD(missing readbuffer)", dimensions);
2307 return GL_TRUE;
2308 }
2309
2310 /* From the EXT_texture_integer spec:
2311 *
2312 * "INVALID_OPERATION is generated by CopyTexImage* and CopyTexSubImage*
2313 * if the texture internalformat is an integer format and the read color
2314 * buffer is not an integer format, or if the internalformat is not an
2315 * integer format and the read color buffer is an integer format."
2316 */
2317 if (_mesa_is_color_format(internalFormat)) {
2318 struct gl_renderbuffer *rb = ctx->ReadBuffer->_ColorReadBuffer;
2319
2320 if (_mesa_is_enum_format_integer(rb->InternalFormat) !=
2321 _mesa_is_enum_format_integer(internalFormat)) {
2322 _mesa_error(ctx, GL_INVALID_OPERATION,
2323 "glCopyTexImage%dD(integer vs non-integer)", dimensions);
2324 return GL_TRUE;
2325 }
2326 }
2327
2328 if ((target == GL_PROXY_TEXTURE_CUBE_MAP_ARB ||
2329 _mesa_is_cube_face(target)) && width != height) {
2330 _mesa_error(ctx, GL_INVALID_VALUE,
2331 "glTexImage2D(cube width != height)");
2332 return GL_TRUE;
2333 }
2334
2335 if (_mesa_is_compressed_format(ctx, internalFormat)) {
2336 if (!target_can_be_compressed(ctx, target, internalFormat)) {
2337 _mesa_error(ctx, GL_INVALID_ENUM,
2338 "glCopyTexImage%dD(target)", dimensions);
2339 return GL_TRUE;
2340 }
2341 if (compressedteximage_only_format(ctx, internalFormat)) {
2342 _mesa_error(ctx, GL_INVALID_OPERATION,
2343 "glCopyTexImage%dD(no compression for format)", dimensions);
2344 return GL_TRUE;
2345 }
2346 if (border != 0) {
2347 _mesa_error(ctx, GL_INVALID_OPERATION,
2348 "glCopyTexImage%dD(border!=0)", dimensions);
2349 return GL_TRUE;
2350 }
2351 }
2352
2353 if (!mutable_tex_object(ctx, target)) {
2354 _mesa_error(ctx, GL_INVALID_OPERATION,
2355 "glCopyTexImage%dD(immutable texture)", dimensions);
2356 return GL_TRUE;
2357 }
2358
2359 /* if we get here, the parameters are OK */
2360 return GL_FALSE;
2361 }
2362
2363
2364 /**
2365 * Test glCopyTexSubImage[12]D() parameters for errors.
2366 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2367 */
2368 static GLboolean
2369 copytexsubimage_error_check(struct gl_context *ctx, GLuint dimensions,
2370 GLenum target, GLint level,
2371 GLint xoffset, GLint yoffset, GLint zoffset,
2372 GLint width, GLint height)
2373 {
2374 struct gl_texture_object *texObj;
2375 struct gl_texture_image *texImage;
2376
2377 /* Check that the source buffer is complete */
2378 if (_mesa_is_user_fbo(ctx->ReadBuffer)) {
2379 if (ctx->ReadBuffer->_Status == 0) {
2380 _mesa_test_framebuffer_completeness(ctx, ctx->ReadBuffer);
2381 }
2382 if (ctx->ReadBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
2383 _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
2384 "glCopyTexImage%dD(invalid readbuffer)", dimensions);
2385 return GL_TRUE;
2386 }
2387
2388 if (ctx->ReadBuffer->Visual.samples > 0) {
2389 _mesa_error(ctx, GL_INVALID_OPERATION,
2390 "glCopyTexSubImage%dD(multisample FBO)",
2391 dimensions);
2392 return GL_TRUE;
2393 }
2394 }
2395
2396 /* check target (proxies not allowed) */
2397 if (!legal_texsubimage_target(ctx, dimensions, target)) {
2398 _mesa_error(ctx, GL_INVALID_ENUM, "glCopyTexSubImage%uD(target=%s)",
2399 dimensions, _mesa_lookup_enum_by_nr(target));
2400 return GL_TRUE;
2401 }
2402
2403 /* Check level */
2404 if (level < 0 || level >= _mesa_max_texture_levels(ctx, target)) {
2405 _mesa_error(ctx, GL_INVALID_VALUE,
2406 "glCopyTexSubImage%dD(level=%d)", dimensions, level);
2407 return GL_TRUE;
2408 }
2409
2410 /* Get dest texture object / image pointers */
2411 texObj = _mesa_get_current_tex_object(ctx, target);
2412 if (!texObj) {
2413 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexSubImage%dD()", dimensions);
2414 return GL_TRUE;
2415 }
2416
2417 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
2418 if (!texImage) {
2419 /* destination image does not exist */
2420 _mesa_error(ctx, GL_INVALID_OPERATION,
2421 "glCopyTexSubImage%dD(invalid texture image)", dimensions);
2422 return GL_TRUE;
2423 }
2424
2425 if (error_check_subtexture_dimensions(ctx, "glCopyTexSubImage",
2426 dimensions, texImage,
2427 xoffset, yoffset, zoffset,
2428 width, height, 1)) {
2429 return GL_TRUE;
2430 }
2431
2432 if (_mesa_is_format_compressed(texImage->TexFormat)) {
2433 if (compressedteximage_only_format(ctx, texImage->InternalFormat)) {
2434 _mesa_error(ctx, GL_INVALID_OPERATION,
2435 "glCopyTexSubImage%dD(no compression for format)", dimensions);
2436 return GL_TRUE;
2437 }
2438 }
2439
2440 if (texImage->InternalFormat == GL_YCBCR_MESA) {
2441 _mesa_error(ctx, GL_INVALID_OPERATION, "glCopyTexSubImage2D");
2442 return GL_TRUE;
2443 }
2444
2445 if (!_mesa_source_buffer_exists(ctx, texImage->_BaseFormat)) {
2446 _mesa_error(ctx, GL_INVALID_OPERATION,
2447 "glCopyTexSubImage%dD(missing readbuffer, format=0x%x)",
2448 dimensions, texImage->_BaseFormat);
2449 return GL_TRUE;
2450 }
2451
2452 /* From the EXT_texture_integer spec:
2453 *
2454 * "INVALID_OPERATION is generated by CopyTexImage* and CopyTexSubImage*
2455 * if the texture internalformat is an integer format and the read color
2456 * buffer is not an integer format, or if the internalformat is not an
2457 * integer format and the read color buffer is an integer format."
2458 */
2459 if (_mesa_is_color_format(texImage->InternalFormat)) {
2460 struct gl_renderbuffer *rb = ctx->ReadBuffer->_ColorReadBuffer;
2461
2462 if (_mesa_is_format_integer_color(rb->Format) !=
2463 _mesa_is_format_integer_color(texImage->TexFormat)) {
2464 _mesa_error(ctx, GL_INVALID_OPERATION,
2465 "glCopyTexImage%dD(integer vs non-integer)", dimensions);
2466 return GL_TRUE;
2467 }
2468 }
2469
2470 /* if we get here, the parameters are OK */
2471 return GL_FALSE;
2472 }
2473
2474
2475 /** Callback info for walking over FBO hash table */
2476 struct cb_info
2477 {
2478 struct gl_context *ctx;
2479 struct gl_texture_object *texObj;
2480 GLuint level, face;
2481 };
2482
2483
2484 /**
2485 * Check render to texture callback. Called from _mesa_HashWalk().
2486 */
2487 static void
2488 check_rtt_cb(GLuint key, void *data, void *userData)
2489 {
2490 struct gl_framebuffer *fb = (struct gl_framebuffer *) data;
2491 const struct cb_info *info = (struct cb_info *) userData;
2492 struct gl_context *ctx = info->ctx;
2493 const struct gl_texture_object *texObj = info->texObj;
2494 const GLuint level = info->level, face = info->face;
2495
2496 /* If this is a user-created FBO */
2497 if (_mesa_is_user_fbo(fb)) {
2498 GLuint i;
2499 /* check if any of the FBO's attachments point to 'texObj' */
2500 for (i = 0; i < BUFFER_COUNT; i++) {
2501 struct gl_renderbuffer_attachment *att = fb->Attachment + i;
2502 if (att->Type == GL_TEXTURE &&
2503 att->Texture == texObj &&
2504 att->TextureLevel == level &&
2505 att->CubeMapFace == face) {
2506 ASSERT(_mesa_get_attachment_teximage(att));
2507 /* Tell driver about the new renderbuffer texture */
2508 ctx->Driver.RenderTexture(ctx, ctx->DrawBuffer, att);
2509 /* Mark fb status as indeterminate to force re-validation */
2510 fb->_Status = 0;
2511 }
2512 }
2513 }
2514 }
2515
2516
2517 /**
2518 * When a texture image is specified we have to check if it's bound to
2519 * any framebuffer objects (render to texture) in order to detect changes
2520 * in size or format since that effects FBO completeness.
2521 * Any FBOs rendering into the texture must be re-validated.
2522 */
2523 void
2524 _mesa_update_fbo_texture(struct gl_context *ctx,
2525 struct gl_texture_object *texObj,
2526 GLuint face, GLuint level)
2527 {
2528 /* Only check this texture if it's been marked as RenderToTexture */
2529 if (texObj->_RenderToTexture) {
2530 struct cb_info info;
2531 info.ctx = ctx;
2532 info.texObj = texObj;
2533 info.level = level;
2534 info.face = face;
2535 _mesa_HashWalk(ctx->Shared->FrameBuffers, check_rtt_cb, &info);
2536 }
2537 }
2538
2539
2540 /**
2541 * If the texture object's GenerateMipmap flag is set and we've
2542 * changed the texture base level image, regenerate the rest of the
2543 * mipmap levels now.
2544 */
2545 static inline void
2546 check_gen_mipmap(struct gl_context *ctx, GLenum target,
2547 struct gl_texture_object *texObj, GLint level)
2548 {
2549 ASSERT(target != GL_TEXTURE_CUBE_MAP);
2550 if (texObj->GenerateMipmap &&
2551 level == texObj->BaseLevel &&
2552 level < texObj->MaxLevel) {
2553 ASSERT(ctx->Driver.GenerateMipmap);
2554 ctx->Driver.GenerateMipmap(ctx, target, texObj);
2555 }
2556 }
2557
2558
2559 /** Debug helper: override the user-requested internal format */
2560 static GLenum
2561 override_internal_format(GLenum internalFormat, GLint width, GLint height)
2562 {
2563 #if 0
2564 if (internalFormat == GL_RGBA16F_ARB ||
2565 internalFormat == GL_RGBA32F_ARB) {
2566 printf("Convert rgba float tex to int %d x %d\n", width, height);
2567 return GL_RGBA;
2568 }
2569 else if (internalFormat == GL_RGB16F_ARB ||
2570 internalFormat == GL_RGB32F_ARB) {
2571 printf("Convert rgb float tex to int %d x %d\n", width, height);
2572 return GL_RGB;
2573 }
2574 else if (internalFormat == GL_LUMINANCE_ALPHA16F_ARB ||
2575 internalFormat == GL_LUMINANCE_ALPHA32F_ARB) {
2576 printf("Convert luminance float tex to int %d x %d\n", width, height);
2577 return GL_LUMINANCE_ALPHA;
2578 }
2579 else if (internalFormat == GL_LUMINANCE16F_ARB ||
2580 internalFormat == GL_LUMINANCE32F_ARB) {
2581 printf("Convert luminance float tex to int %d x %d\n", width, height);
2582 return GL_LUMINANCE;
2583 }
2584 else if (internalFormat == GL_ALPHA16F_ARB ||
2585 internalFormat == GL_ALPHA32F_ARB) {
2586 printf("Convert luminance float tex to int %d x %d\n", width, height);
2587 return GL_ALPHA;
2588 }
2589 /*
2590 else if (internalFormat == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT) {
2591 internalFormat = GL_RGBA;
2592 }
2593 */
2594 else {
2595 return internalFormat;
2596 }
2597 #else
2598 return internalFormat;
2599 #endif
2600 }
2601
2602
2603 /**
2604 * Choose the actual hardware format for a texture image.
2605 * Try to use the same format as the previous image level when possible.
2606 * Otherwise, ask the driver for the best format.
2607 * It's important to try to choose a consistant format for all levels
2608 * for efficient texture memory layout/allocation. In particular, this
2609 * comes up during automatic mipmap generation.
2610 */
2611 gl_format
2612 _mesa_choose_texture_format(struct gl_context *ctx,
2613 struct gl_texture_object *texObj,
2614 GLenum target, GLint level,
2615 GLenum internalFormat, GLenum format, GLenum type)
2616 {
2617 gl_format f;
2618
2619 /* see if we've already chosen a format for the previous level */
2620 if (level > 0) {
2621 struct gl_texture_image *prevImage =
2622 _mesa_select_tex_image(ctx, texObj, target, level - 1);
2623 /* See if the prev level is defined and has an internal format which
2624 * matches the new internal format.
2625 */
2626 if (prevImage &&
2627 prevImage->Width > 0 &&
2628 prevImage->InternalFormat == internalFormat) {
2629 /* use the same format */
2630 ASSERT(prevImage->TexFormat != MESA_FORMAT_NONE);
2631 return prevImage->TexFormat;
2632 }
2633 }
2634
2635 /* If the application requested compression to an S3TC format but we don't
2636 * have the DTXn library, force a generic compressed format instead.
2637 */
2638 if (internalFormat != format && format != GL_NONE) {
2639 const GLenum before = internalFormat;
2640
2641 switch (internalFormat) {
2642 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
2643 if (!ctx->Mesa_DXTn)
2644 internalFormat = GL_COMPRESSED_RGB;
2645 break;
2646 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
2647 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
2648 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
2649 if (!ctx->Mesa_DXTn)
2650 internalFormat = GL_COMPRESSED_RGBA;
2651 break;
2652 default:
2653 break;
2654 }
2655
2656 if (before != internalFormat) {
2657 _mesa_warning(ctx,
2658 "DXT compression requested (%s), "
2659 "but libtxc_dxtn library not installed. Using %s "
2660 "instead.",
2661 _mesa_lookup_enum_by_nr(before),
2662 _mesa_lookup_enum_by_nr(internalFormat));
2663 }
2664 }
2665
2666 /* choose format from scratch */
2667 f = ctx->Driver.ChooseTextureFormat(ctx, texObj->Target, internalFormat,
2668 format, type);
2669 ASSERT(f != MESA_FORMAT_NONE);
2670 return f;
2671 }
2672
2673 /**
2674 * Adjust pixel unpack params and image dimensions to strip off the
2675 * one-pixel texture border.
2676 *
2677 * Gallium and intel don't support texture borders. They've seldem been used
2678 * and seldom been implemented correctly anyway.
2679 *
2680 * \param unpackNew returns the new pixel unpack parameters
2681 */
2682 static void
2683 strip_texture_border(GLenum target,
2684 GLint *width, GLint *height, GLint *depth,
2685 const struct gl_pixelstore_attrib *unpack,
2686 struct gl_pixelstore_attrib *unpackNew)
2687 {
2688 assert(width);
2689 assert(height);
2690 assert(depth);
2691
2692 *unpackNew = *unpack;
2693
2694 if (unpackNew->RowLength == 0)
2695 unpackNew->RowLength = *width;
2696
2697 if (unpackNew->ImageHeight == 0)
2698 unpackNew->ImageHeight = *height;
2699
2700 assert(*width >= 3);
2701 unpackNew->SkipPixels++; /* skip the border */
2702 *width = *width - 2; /* reduce the width by two border pixels */
2703
2704 /* The min height of a texture with a border is 3 */
2705 if (*height >= 3 && target != GL_TEXTURE_1D_ARRAY) {
2706 unpackNew->SkipRows++; /* skip the border */
2707 *height = *height - 2; /* reduce the height by two border pixels */
2708 }
2709
2710 if (*depth >= 3 && target != GL_TEXTURE_2D_ARRAY) {
2711 unpackNew->SkipImages++; /* skip the border */
2712 *depth = *depth - 2; /* reduce the depth by two border pixels */
2713 }
2714 }
2715
2716
2717 /**
2718 * Common code to implement all the glTexImage1D/2D/3D functions
2719 * as well as glCompressedTexImage1D/2D/3D.
2720 * \param compressed only GL_TRUE for glCompressedTexImage1D/2D/3D calls.
2721 * \param format the user's image format (only used if !compressed)
2722 * \param type the user's image type (only used if !compressed)
2723 * \param imageSize only used for glCompressedTexImage1D/2D/3D calls.
2724 */
2725 static void
2726 teximage(struct gl_context *ctx, GLboolean compressed, GLuint dims,
2727 GLenum target, GLint level, GLint internalFormat,
2728 GLsizei width, GLsizei height, GLsizei depth,
2729 GLint border, GLenum format, GLenum type,
2730 GLsizei imageSize, const GLvoid *pixels)
2731 {
2732 const char *func = compressed ? "glCompressedTexImage" : "glTexImage";
2733 struct gl_pixelstore_attrib unpack_no_border;
2734 const struct gl_pixelstore_attrib *unpack = &ctx->Unpack;
2735 struct gl_texture_object *texObj;
2736 gl_format texFormat;
2737 GLboolean dimensionsOK, sizeOK;
2738
2739 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2740
2741 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE)) {
2742 if (compressed)
2743 _mesa_debug(ctx,
2744 "glCompressedTexImage%uD %s %d %s %d %d %d %d %p\n",
2745 dims,
2746 _mesa_lookup_enum_by_nr(target), level,
2747 _mesa_lookup_enum_by_nr(internalFormat),
2748 width, height, depth, border, pixels);
2749 else
2750 _mesa_debug(ctx,
2751 "glTexImage%uD %s %d %s %d %d %d %d %s %s %p\n",
2752 dims,
2753 _mesa_lookup_enum_by_nr(target), level,
2754 _mesa_lookup_enum_by_nr(internalFormat),
2755 width, height, depth, border,
2756 _mesa_lookup_enum_by_nr(format),
2757 _mesa_lookup_enum_by_nr(type), pixels);
2758 }
2759
2760 internalFormat = override_internal_format(internalFormat, width, height);
2761
2762 /* target error checking */
2763 if (!legal_teximage_target(ctx, dims, target)) {
2764 _mesa_error(ctx, GL_INVALID_ENUM, "%s%uD(target=%s)",
2765 func, dims, _mesa_lookup_enum_by_nr(target));
2766 return;
2767 }
2768
2769 /* general error checking */
2770 if (compressed) {
2771 if (compressed_texture_error_check(ctx, dims, target, level,
2772 internalFormat,
2773 width, height, depth,
2774 border, imageSize))
2775 return;
2776 }
2777 else {
2778 if (texture_error_check(ctx, dims, target, level, internalFormat,
2779 format, type, width, height, depth, border))
2780 return;
2781 }
2782
2783 /* Here we convert a cpal compressed image into a regular glTexImage2D
2784 * call by decompressing the texture. If we really want to support cpal
2785 * textures in any driver this would have to be changed.
2786 */
2787 if (ctx->API == API_OPENGLES && compressed && dims == 2) {
2788 switch (internalFormat) {
2789 case GL_PALETTE4_RGB8_OES:
2790 case GL_PALETTE4_RGBA8_OES:
2791 case GL_PALETTE4_R5_G6_B5_OES:
2792 case GL_PALETTE4_RGBA4_OES:
2793 case GL_PALETTE4_RGB5_A1_OES:
2794 case GL_PALETTE8_RGB8_OES:
2795 case GL_PALETTE8_RGBA8_OES:
2796 case GL_PALETTE8_R5_G6_B5_OES:
2797 case GL_PALETTE8_RGBA4_OES:
2798 case GL_PALETTE8_RGB5_A1_OES:
2799 _mesa_cpal_compressed_teximage2d(target, level, internalFormat,
2800 width, height, imageSize, pixels);
2801 return;
2802 }
2803 }
2804
2805 texObj = _mesa_get_current_tex_object(ctx, target);
2806 assert(texObj);
2807
2808 texFormat = _mesa_choose_texture_format(ctx, texObj, target, level,
2809 internalFormat, format, type);
2810 assert(texFormat != MESA_FORMAT_NONE);
2811
2812 /* check that width, height, depth are legal for the mipmap level */
2813 dimensionsOK = _mesa_legal_texture_dimensions(ctx, target, level, width,
2814 height, depth, border);
2815
2816 /* check that the texture won't take too much memory, etc */
2817 sizeOK = ctx->Driver.TestProxyTexImage(ctx, _mesa_get_proxy_target(target),
2818 level, texFormat,
2819 width, height, depth, border);
2820
2821 if (_mesa_is_proxy_texture(target)) {
2822 /* Proxy texture: just clear or set state depending on error checking */
2823 struct gl_texture_image *texImage =
2824 get_proxy_tex_image(ctx, target, level);
2825
2826 if (!texImage)
2827 return; /* GL_OUT_OF_MEMORY already recorded */
2828
2829 if (dimensionsOK && sizeOK) {
2830 _mesa_init_teximage_fields(ctx, texImage, width, height, depth,
2831 border, internalFormat, texFormat);
2832 }
2833 else {
2834 clear_teximage_fields(texImage);
2835 }
2836 }
2837 else {
2838 /* non-proxy target */
2839 const GLuint face = _mesa_tex_target_to_face(target);
2840 struct gl_texture_image *texImage;
2841
2842 if (!dimensionsOK) {
2843 _mesa_error(ctx, GL_INVALID_VALUE,
2844 "glTexImage%uD(invalid width or height or depth)",
2845 dims);
2846 return;
2847 }
2848
2849 if (!sizeOK) {
2850 _mesa_error(ctx, GL_OUT_OF_MEMORY,
2851 "glTexImage%uD(image too large)", dims);
2852 return;
2853 }
2854
2855 /* Allow a hardware driver to just strip out the border, to provide
2856 * reliable but slightly incorrect hardware rendering instead of
2857 * rarely-tested software fallback rendering.
2858 */
2859 if (border && ctx->Const.StripTextureBorder) {
2860 strip_texture_border(target, &width, &height, &depth, unpack,
2861 &unpack_no_border);
2862 border = 0;
2863 unpack = &unpack_no_border;
2864 }
2865
2866 if (ctx->NewState & _NEW_PIXEL)
2867 _mesa_update_state(ctx);
2868
2869 _mesa_lock_texture(ctx, texObj);
2870 {
2871 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
2872
2873 if (!texImage) {
2874 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s%uD", func, dims);
2875 }
2876 else {
2877 ctx->Driver.FreeTextureImageBuffer(ctx, texImage);
2878
2879 _mesa_init_teximage_fields(ctx, texImage,
2880 width, height, depth,
2881 border, internalFormat, texFormat);
2882
2883 /* Give the texture to the driver. <pixels> may be null. */
2884 if (compressed) {
2885 ctx->Driver.CompressedTexImage(ctx, dims, texImage,
2886 imageSize, pixels);
2887 }
2888 else {
2889 ctx->Driver.TexImage(ctx, dims, texImage, format,
2890 type, pixels, unpack);
2891 }
2892
2893 check_gen_mipmap(ctx, target, texObj, level);
2894
2895 _mesa_update_fbo_texture(ctx, texObj, face, level);
2896
2897 _mesa_dirty_texobj(ctx, texObj, GL_TRUE);
2898 }
2899 }
2900 _mesa_unlock_texture(ctx, texObj);
2901 }
2902 }
2903
2904
2905
2906 /*
2907 * Called from the API. Note that width includes the border.
2908 */
2909 void GLAPIENTRY
2910 _mesa_TexImage1D( GLenum target, GLint level, GLint internalFormat,
2911 GLsizei width, GLint border, GLenum format,
2912 GLenum type, const GLvoid *pixels )
2913 {
2914 GET_CURRENT_CONTEXT(ctx);
2915 teximage(ctx, GL_FALSE, 1, target, level, internalFormat, width, 1, 1,
2916 border, format, type, 0, pixels);
2917 }
2918
2919
2920 void GLAPIENTRY
2921 _mesa_TexImage2D( GLenum target, GLint level, GLint internalFormat,
2922 GLsizei width, GLsizei height, GLint border,
2923 GLenum format, GLenum type,
2924 const GLvoid *pixels )
2925 {
2926 GET_CURRENT_CONTEXT(ctx);
2927 teximage(ctx, GL_FALSE, 2, target, level, internalFormat, width, height, 1,
2928 border, format, type, 0, pixels);
2929 }
2930
2931
2932 /*
2933 * Called by the API or display list executor.
2934 * Note that width and height include the border.
2935 */
2936 void GLAPIENTRY
2937 _mesa_TexImage3D( GLenum target, GLint level, GLint internalFormat,
2938 GLsizei width, GLsizei height, GLsizei depth,
2939 GLint border, GLenum format, GLenum type,
2940 const GLvoid *pixels )
2941 {
2942 GET_CURRENT_CONTEXT(ctx);
2943 teximage(ctx, GL_FALSE, 3, target, level, internalFormat,
2944 width, height, depth,
2945 border, format, type, 0, pixels);
2946 }
2947
2948
2949 void GLAPIENTRY
2950 _mesa_TexImage3DEXT( GLenum target, GLint level, GLenum internalFormat,
2951 GLsizei width, GLsizei height, GLsizei depth,
2952 GLint border, GLenum format, GLenum type,
2953 const GLvoid *pixels )
2954 {
2955 _mesa_TexImage3D(target, level, (GLint) internalFormat, width, height,
2956 depth, border, format, type, pixels);
2957 }
2958
2959
2960 void GLAPIENTRY
2961 _mesa_EGLImageTargetTexture2DOES (GLenum target, GLeglImageOES image)
2962 {
2963 struct gl_texture_object *texObj;
2964 struct gl_texture_image *texImage;
2965 bool valid_target;
2966 GET_CURRENT_CONTEXT(ctx);
2967 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2968
2969 switch (target) {
2970 case GL_TEXTURE_2D:
2971 valid_target = ctx->Extensions.OES_EGL_image;
2972 break;
2973 case GL_TEXTURE_EXTERNAL_OES:
2974 valid_target = ctx->Extensions.OES_EGL_image_external;
2975 break;
2976 default:
2977 valid_target = false;
2978 break;
2979 }
2980
2981 if (!valid_target) {
2982 _mesa_error(ctx, GL_INVALID_ENUM,
2983 "glEGLImageTargetTexture2D(target=%d)", target);
2984 return;
2985 }
2986
2987 if (ctx->NewState & _NEW_PIXEL)
2988 _mesa_update_state(ctx);
2989
2990 texObj = _mesa_get_current_tex_object(ctx, target);
2991 _mesa_lock_texture(ctx, texObj);
2992
2993 if (texObj->Immutable) {
2994 _mesa_error(ctx, GL_INVALID_OPERATION,
2995 "glEGLImageTargetTexture2D(texture is immutable)");
2996 _mesa_unlock_texture(ctx, texObj);
2997 return;
2998 }
2999
3000 texImage = _mesa_get_tex_image(ctx, texObj, target, 0);
3001 if (!texImage) {
3002 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glEGLImageTargetTexture2D");
3003 } else {
3004 ctx->Driver.FreeTextureImageBuffer(ctx, texImage);
3005
3006 ctx->Driver.EGLImageTargetTexture2D(ctx, target,
3007 texObj, texImage, image);
3008
3009 _mesa_dirty_texobj(ctx, texObj, GL_TRUE);
3010 }
3011 _mesa_unlock_texture(ctx, texObj);
3012
3013 }
3014
3015
3016
3017 /**
3018 * Implement all the glTexSubImage1/2/3D() functions.
3019 */
3020 static void
3021 texsubimage(struct gl_context *ctx, GLuint dims, GLenum target, GLint level,
3022 GLint xoffset, GLint yoffset, GLint zoffset,
3023 GLsizei width, GLsizei height, GLsizei depth,
3024 GLenum format, GLenum type, const GLvoid *pixels )
3025 {
3026 struct gl_texture_object *texObj;
3027 struct gl_texture_image *texImage;
3028
3029 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3030
3031 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
3032 _mesa_debug(ctx, "glTexSubImage%uD %s %d %d %d %d %d %d %d %s %s %p\n",
3033 dims,
3034 _mesa_lookup_enum_by_nr(target), level,
3035 xoffset, yoffset, zoffset, width, height, depth,
3036 _mesa_lookup_enum_by_nr(format),
3037 _mesa_lookup_enum_by_nr(type), pixels);
3038
3039 /* check target (proxies not allowed) */
3040 if (!legal_texsubimage_target(ctx, dims, target)) {
3041 _mesa_error(ctx, GL_INVALID_ENUM, "glTexSubImage%uD(target=%s)",
3042 dims, _mesa_lookup_enum_by_nr(target));
3043 return;
3044 }
3045
3046 if (ctx->NewState & _NEW_PIXEL)
3047 _mesa_update_state(ctx);
3048
3049 if (texsubimage_error_check(ctx, dims, target, level,
3050 xoffset, yoffset, zoffset,
3051 width, height, depth, format, type)) {
3052 return; /* error was detected */
3053 }
3054
3055 texObj = _mesa_get_current_tex_object(ctx, target);
3056
3057 _mesa_lock_texture(ctx, texObj);
3058 {
3059 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
3060
3061 if (width > 0 && height > 0 && depth > 0) {
3062 /* If we have a border, offset=-1 is legal. Bias by border width. */
3063 switch (dims) {
3064 case 3:
3065 if (target != GL_TEXTURE_2D_ARRAY)
3066 zoffset += texImage->Border;
3067 /* fall-through */
3068 case 2:
3069 if (target != GL_TEXTURE_1D_ARRAY)
3070 yoffset += texImage->Border;
3071 /* fall-through */
3072 case 1:
3073 xoffset += texImage->Border;
3074 }
3075
3076 ctx->Driver.TexSubImage(ctx, dims, texImage,
3077 xoffset, yoffset, zoffset,
3078 width, height, depth,
3079 format, type, pixels, &ctx->Unpack);
3080
3081 check_gen_mipmap(ctx, target, texObj, level);
3082
3083 ctx->NewState |= _NEW_TEXTURE;
3084 }
3085 }
3086 _mesa_unlock_texture(ctx, texObj);
3087 }
3088
3089
3090 void GLAPIENTRY
3091 _mesa_TexSubImage1D( GLenum target, GLint level,
3092 GLint xoffset, GLsizei width,
3093 GLenum format, GLenum type,
3094 const GLvoid *pixels )
3095 {
3096 GET_CURRENT_CONTEXT(ctx);
3097 texsubimage(ctx, 1, target, level,
3098 xoffset, 0, 0,
3099 width, 1, 1,
3100 format, type, pixels);
3101 }
3102
3103
3104 void GLAPIENTRY
3105 _mesa_TexSubImage2D( GLenum target, GLint level,
3106 GLint xoffset, GLint yoffset,
3107 GLsizei width, GLsizei height,
3108 GLenum format, GLenum type,
3109 const GLvoid *pixels )
3110 {
3111 GET_CURRENT_CONTEXT(ctx);
3112 texsubimage(ctx, 2, target, level,
3113 xoffset, yoffset, 0,
3114 width, height, 1,
3115 format, type, pixels);
3116 }
3117
3118
3119
3120 void GLAPIENTRY
3121 _mesa_TexSubImage3D( GLenum target, GLint level,
3122 GLint xoffset, GLint yoffset, GLint zoffset,
3123 GLsizei width, GLsizei height, GLsizei depth,
3124 GLenum format, GLenum type,
3125 const GLvoid *pixels )
3126 {
3127 GET_CURRENT_CONTEXT(ctx);
3128 texsubimage(ctx, 3, target, level,
3129 xoffset, yoffset, zoffset,
3130 width, height, depth,
3131 format, type, pixels);
3132 }
3133
3134
3135
3136 /**
3137 * For glCopyTexSubImage, return the source renderbuffer to copy texel data
3138 * from. This depends on whether the texture contains color or depth values.
3139 */
3140 static struct gl_renderbuffer *
3141 get_copy_tex_image_source(struct gl_context *ctx, gl_format texFormat)
3142 {
3143 if (_mesa_get_format_bits(texFormat, GL_DEPTH_BITS) > 0) {
3144 /* reading from depth/stencil buffer */
3145 return ctx->ReadBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;
3146 }
3147 else {
3148 /* copying from color buffer */
3149 return ctx->ReadBuffer->_ColorReadBuffer;
3150 }
3151 }
3152
3153
3154
3155 /**
3156 * Implement the glCopyTexImage1/2D() functions.
3157 */
3158 static void
3159 copyteximage(struct gl_context *ctx, GLuint dims,
3160 GLenum target, GLint level, GLenum internalFormat,
3161 GLint x, GLint y, GLsizei width, GLsizei height, GLint border )
3162 {
3163 struct gl_texture_object *texObj;
3164 struct gl_texture_image *texImage;
3165 const GLuint face = _mesa_tex_target_to_face(target);
3166 gl_format texFormat;
3167
3168 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3169
3170 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
3171 _mesa_debug(ctx, "glCopyTexImage%uD %s %d %s %d %d %d %d %d\n",
3172 dims,
3173 _mesa_lookup_enum_by_nr(target), level,
3174 _mesa_lookup_enum_by_nr(internalFormat),
3175 x, y, width, height, border);
3176
3177 if (ctx->NewState & NEW_COPY_TEX_STATE)
3178 _mesa_update_state(ctx);
3179
3180 if (copytexture_error_check(ctx, dims, target, level, internalFormat,
3181 width, height, border))
3182 return;
3183
3184 if (!_mesa_legal_texture_dimensions(ctx, target, level, width, height,
3185 1, border)) {
3186 _mesa_error(ctx, GL_INVALID_VALUE,
3187 "glCopyTexImage%uD(invalid width or height)", dims);
3188 return;
3189 }
3190
3191 texObj = _mesa_get_current_tex_object(ctx, target);
3192 assert(texObj);
3193
3194 texFormat = _mesa_choose_texture_format(ctx, texObj, target, level,
3195 internalFormat, GL_NONE, GL_NONE);
3196 assert(texFormat != MESA_FORMAT_NONE);
3197
3198 if (!ctx->Driver.TestProxyTexImage(ctx, _mesa_get_proxy_target(target),
3199 level, texFormat,
3200 width, height, 1, border)) {
3201 _mesa_error(ctx, GL_OUT_OF_MEMORY,
3202 "glCopyTexImage%uD(image too large)", dims);
3203 return;
3204 }
3205
3206 if (border && ctx->Const.StripTextureBorder) {
3207 x += border;
3208 width -= border * 2;
3209 if (dims == 2) {
3210 y += border;
3211 height -= border * 2;
3212 }
3213 border = 0;
3214 }
3215
3216 _mesa_lock_texture(ctx, texObj);
3217 {
3218 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
3219
3220 if (!texImage) {
3221 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexImage%uD", dims);
3222 }
3223 else {
3224 GLint srcX = x, srcY = y, dstX = 0, dstY = 0, dstZ = 0;
3225
3226 /* Free old texture image */
3227 ctx->Driver.FreeTextureImageBuffer(ctx, texImage);
3228
3229 _mesa_init_teximage_fields(ctx, texImage, width, height, 1,
3230 border, internalFormat, texFormat);
3231
3232 /* Allocate texture memory (no pixel data yet) */
3233 ctx->Driver.TexImage(ctx, dims, texImage,
3234 GL_NONE, GL_NONE,
3235 NULL, &ctx->Unpack);
3236
3237 if (_mesa_clip_copytexsubimage(ctx, &dstX, &dstY, &srcX, &srcY,
3238 &width, &height)) {
3239 struct gl_renderbuffer *srcRb =
3240 get_copy_tex_image_source(ctx, texImage->TexFormat);
3241
3242 ctx->Driver.CopyTexSubImage(ctx, dims, texImage, dstX, dstY, dstZ,
3243 srcRb, srcX, srcY, width, height);
3244 }
3245
3246 check_gen_mipmap(ctx, target, texObj, level);
3247
3248 _mesa_update_fbo_texture(ctx, texObj, face, level);
3249
3250 _mesa_dirty_texobj(ctx, texObj, GL_TRUE);
3251 }
3252 }
3253 _mesa_unlock_texture(ctx, texObj);
3254 }
3255
3256
3257
3258 void GLAPIENTRY
3259 _mesa_CopyTexImage1D( GLenum target, GLint level,
3260 GLenum internalFormat,
3261 GLint x, GLint y,
3262 GLsizei width, GLint border )
3263 {
3264 GET_CURRENT_CONTEXT(ctx);
3265 copyteximage(ctx, 1, target, level, internalFormat, x, y, width, 1, border);
3266 }
3267
3268
3269
3270 void GLAPIENTRY
3271 _mesa_CopyTexImage2D( GLenum target, GLint level, GLenum internalFormat,
3272 GLint x, GLint y, GLsizei width, GLsizei height,
3273 GLint border )
3274 {
3275 GET_CURRENT_CONTEXT(ctx);
3276 copyteximage(ctx, 2, target, level, internalFormat,
3277 x, y, width, height, border);
3278 }
3279
3280
3281
3282 /**
3283 * Implementation for glCopyTexSubImage1/2/3D() functions.
3284 */
3285 static void
3286 copytexsubimage(struct gl_context *ctx, GLuint dims, GLenum target, GLint level,
3287 GLint xoffset, GLint yoffset, GLint zoffset,
3288 GLint x, GLint y, GLsizei width, GLsizei height)
3289 {
3290 struct gl_texture_object *texObj;
3291 struct gl_texture_image *texImage;
3292
3293 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3294
3295 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
3296 _mesa_debug(ctx, "glCopyTexSubImage%uD %s %d %d %d %d %d %d %d %d\n",
3297 dims,
3298 _mesa_lookup_enum_by_nr(target),
3299 level, xoffset, yoffset, zoffset, x, y, width, height);
3300
3301 if (ctx->NewState & NEW_COPY_TEX_STATE)
3302 _mesa_update_state(ctx);
3303
3304 if (copytexsubimage_error_check(ctx, dims, target, level,
3305 xoffset, yoffset, zoffset, width, height)) {
3306 return;
3307 }
3308
3309 texObj = _mesa_get_current_tex_object(ctx, target);
3310
3311 _mesa_lock_texture(ctx, texObj);
3312 {
3313 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
3314
3315 /* If we have a border, offset=-1 is legal. Bias by border width. */
3316 switch (dims) {
3317 case 3:
3318 if (target != GL_TEXTURE_2D_ARRAY)
3319 zoffset += texImage->Border;
3320 /* fall-through */
3321 case 2:
3322 if (target != GL_TEXTURE_1D_ARRAY)
3323 yoffset += texImage->Border;
3324 /* fall-through */
3325 case 1:
3326 xoffset += texImage->Border;
3327 }
3328
3329 if (_mesa_clip_copytexsubimage(ctx, &xoffset, &yoffset, &x, &y,
3330 &width, &height)) {
3331 struct gl_renderbuffer *srcRb =
3332 get_copy_tex_image_source(ctx, texImage->TexFormat);
3333
3334 ctx->Driver.CopyTexSubImage(ctx, dims, texImage,
3335 xoffset, yoffset, zoffset,
3336 srcRb, x, y, width, height);
3337
3338 check_gen_mipmap(ctx, target, texObj, level);
3339
3340 ctx->NewState |= _NEW_TEXTURE;
3341 }
3342 }
3343 _mesa_unlock_texture(ctx, texObj);
3344 }
3345
3346
3347 void GLAPIENTRY
3348 _mesa_CopyTexSubImage1D( GLenum target, GLint level,
3349 GLint xoffset, GLint x, GLint y, GLsizei width )
3350 {
3351 GET_CURRENT_CONTEXT(ctx);
3352 copytexsubimage(ctx, 1, target, level, xoffset, 0, 0, x, y, width, 1);
3353 }
3354
3355
3356
3357 void GLAPIENTRY
3358 _mesa_CopyTexSubImage2D( GLenum target, GLint level,
3359 GLint xoffset, GLint yoffset,
3360 GLint x, GLint y, GLsizei width, GLsizei height )
3361 {
3362 GET_CURRENT_CONTEXT(ctx);
3363 copytexsubimage(ctx, 2, target, level, xoffset, yoffset, 0, x, y,
3364 width, height);
3365 }
3366
3367
3368
3369 void GLAPIENTRY
3370 _mesa_CopyTexSubImage3D( GLenum target, GLint level,
3371 GLint xoffset, GLint yoffset, GLint zoffset,
3372 GLint x, GLint y, GLsizei width, GLsizei height )
3373 {
3374 GET_CURRENT_CONTEXT(ctx);
3375 copytexsubimage(ctx, 3, target, level, xoffset, yoffset, zoffset,
3376 x, y, width, height);
3377 }
3378
3379
3380
3381
3382 /**********************************************************************/
3383 /****** Compressed Textures ******/
3384 /**********************************************************************/
3385
3386
3387 /**
3388 * Error checking for glCompressedTexSubImage[123]D().
3389 * \return error code or GL_NO_ERROR.
3390 */
3391 static GLenum
3392 compressed_subtexture_error_check(struct gl_context *ctx, GLint dims,
3393 GLenum target, GLint level,
3394 GLint xoffset, GLint yoffset, GLint zoffset,
3395 GLsizei width, GLsizei height, GLsizei depth,
3396 GLenum format, GLsizei imageSize)
3397 {
3398 struct gl_texture_object *texObj;
3399 struct gl_texture_image *texImage;
3400 GLint expectedSize;
3401 GLboolean targetOK;
3402
3403 if (dims == 2) {
3404 switch (target) {
3405 case GL_TEXTURE_2D:
3406 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
3407 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
3408 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
3409 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
3410 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
3411 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
3412 targetOK = GL_TRUE;
3413 break;
3414 default:
3415 targetOK = GL_FALSE;
3416 }
3417 }
3418 else {
3419 assert(dims == 1 || dims == 3);
3420 /* no 1D or 3D compressed textures at this time */
3421 targetOK = GL_FALSE;
3422 }
3423
3424 if (!targetOK) {
3425 _mesa_error(ctx, GL_INVALID_ENUM, "glCompressedTexImage%uD(target)",
3426 dims);
3427 return GL_TRUE;
3428 }
3429
3430 /* this will catch any invalid compressed format token */
3431 if (!_mesa_is_compressed_format(ctx, format)) {
3432 _mesa_error(ctx, GL_INVALID_ENUM, "glCompressedTexImage%uD(format)",
3433 dims);
3434 return GL_TRUE;
3435 }
3436
3437 if (level < 0 || level >= _mesa_max_texture_levels(ctx, target)) {
3438 _mesa_error(ctx, GL_INVALID_VALUE, "glCompressedTexImage%uD(level=%d)",
3439 dims, level);
3440 return GL_TRUE;
3441 }
3442
3443 expectedSize = compressed_tex_size(width, height, depth, format);
3444 if (expectedSize != imageSize) {
3445 _mesa_error(ctx, GL_INVALID_VALUE, "glCompressedTexImage%uD(size=%d)",
3446 dims, imageSize);
3447 return GL_TRUE;
3448 }
3449
3450 texObj = _mesa_get_current_tex_object(ctx, target);
3451 if (!texObj) {
3452 _mesa_error(ctx, GL_OUT_OF_MEMORY,
3453 "glCompressedTexSubImage%uD()", dims);
3454 return GL_TRUE;
3455 }
3456
3457 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
3458 if (!texImage) {
3459 _mesa_error(ctx, GL_INVALID_OPERATION,
3460 "glCompressedTexSubImage%uD(invalid texture image)", dims);
3461 return GL_TRUE;
3462 }
3463
3464 if ((GLint) format != texImage->InternalFormat) {
3465 _mesa_error(ctx, GL_INVALID_OPERATION,
3466 "glCompressedTexSubImage%uD(format=0x%x)", dims, format);
3467 return GL_TRUE;
3468 }
3469
3470 if (compressedteximage_only_format(ctx, format)) {
3471 _mesa_error(ctx, GL_INVALID_OPERATION,
3472 "glCompressedTexSubImage%uD(format=0x%x cannot be updated)"
3473 , dims, format);
3474 return GL_TRUE;
3475 }
3476
3477 if (error_check_subtexture_dimensions(ctx, "glCompressedTexSubImage", dims,
3478 texImage, xoffset, yoffset, zoffset,
3479 width, height, depth)) {
3480 return GL_TRUE;
3481 }
3482
3483 return GL_FALSE;
3484 }
3485
3486
3487 void GLAPIENTRY
3488 _mesa_CompressedTexImage1DARB(GLenum target, GLint level,
3489 GLenum internalFormat, GLsizei width,
3490 GLint border, GLsizei imageSize,
3491 const GLvoid *data)
3492 {
3493 GET_CURRENT_CONTEXT(ctx);
3494 teximage(ctx, GL_TRUE, 1, target, level, internalFormat,
3495 width, 1, 1, border, GL_NONE, GL_NONE, imageSize, data);
3496 }
3497
3498
3499 void GLAPIENTRY
3500 _mesa_CompressedTexImage2DARB(GLenum target, GLint level,
3501 GLenum internalFormat, GLsizei width,
3502 GLsizei height, GLint border, GLsizei imageSize,
3503 const GLvoid *data)
3504 {
3505 GET_CURRENT_CONTEXT(ctx);
3506 teximage(ctx, GL_TRUE, 2, target, level, internalFormat,
3507 width, height, 1, border, GL_NONE, GL_NONE, imageSize, data);
3508 }
3509
3510
3511 void GLAPIENTRY
3512 _mesa_CompressedTexImage3DARB(GLenum target, GLint level,
3513 GLenum internalFormat, GLsizei width,
3514 GLsizei height, GLsizei depth, GLint border,
3515 GLsizei imageSize, const GLvoid *data)
3516 {
3517 GET_CURRENT_CONTEXT(ctx);
3518 teximage(ctx, GL_TRUE, 3, target, level, internalFormat,
3519 width, height, depth, border, GL_NONE, GL_NONE, imageSize, data);
3520 }
3521
3522
3523 /**
3524 * Common helper for glCompressedTexSubImage1/2/3D().
3525 */
3526 static void
3527 compressed_tex_sub_image(GLuint dims, GLenum target, GLint level,
3528 GLint xoffset, GLint yoffset, GLint zoffset,
3529 GLsizei width, GLsizei height, GLsizei depth,
3530 GLenum format, GLsizei imageSize, const GLvoid *data)
3531 {
3532 struct gl_texture_object *texObj;
3533 struct gl_texture_image *texImage;
3534 GET_CURRENT_CONTEXT(ctx);
3535 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3536
3537 if (compressed_subtexture_error_check(ctx, dims, target, level,
3538 xoffset, yoffset, zoffset,
3539 width, height, depth,
3540 format, imageSize)) {
3541 return;
3542 }
3543
3544 texObj = _mesa_get_current_tex_object(ctx, target);
3545
3546 _mesa_lock_texture(ctx, texObj);
3547 {
3548 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
3549 assert(texImage);
3550
3551 if (width > 0 && height > 0 && depth > 0) {
3552 ctx->Driver.CompressedTexSubImage(ctx, dims, texImage,
3553 xoffset, yoffset, zoffset,
3554 width, height, depth,
3555 format, imageSize, data);
3556
3557 check_gen_mipmap(ctx, target, texObj, level);
3558
3559 ctx->NewState |= _NEW_TEXTURE;
3560 }
3561 }
3562 _mesa_unlock_texture(ctx, texObj);
3563 }
3564
3565
3566 void GLAPIENTRY
3567 _mesa_CompressedTexSubImage1DARB(GLenum target, GLint level, GLint xoffset,
3568 GLsizei width, GLenum format,
3569 GLsizei imageSize, const GLvoid *data)
3570 {
3571 compressed_tex_sub_image(1, target, level, xoffset, 0, 0, width, 1, 1,
3572 format, imageSize, data);
3573 }
3574
3575
3576 void GLAPIENTRY
3577 _mesa_CompressedTexSubImage2DARB(GLenum target, GLint level, GLint xoffset,
3578 GLint yoffset, GLsizei width, GLsizei height,
3579 GLenum format, GLsizei imageSize,
3580 const GLvoid *data)
3581 {
3582 compressed_tex_sub_image(2, target, level, xoffset, yoffset, 0,
3583 width, height, 1, format, imageSize, data);
3584 }
3585
3586
3587 void GLAPIENTRY
3588 _mesa_CompressedTexSubImage3DARB(GLenum target, GLint level, GLint xoffset,
3589 GLint yoffset, GLint zoffset, GLsizei width,
3590 GLsizei height, GLsizei depth, GLenum format,
3591 GLsizei imageSize, const GLvoid *data)
3592 {
3593 compressed_tex_sub_image(3, target, level, xoffset, yoffset, zoffset,
3594 width, height, depth, format, imageSize, data);
3595 }
3596
3597 static gl_format
3598 get_texbuffer_format(const struct gl_context *ctx, GLenum internalFormat)
3599 {
3600 switch (internalFormat) {
3601 case GL_ALPHA8:
3602 return MESA_FORMAT_A8;
3603 case GL_ALPHA16:
3604 return MESA_FORMAT_A16;
3605 case GL_ALPHA16F_ARB:
3606 return MESA_FORMAT_ALPHA_FLOAT16;
3607 case GL_ALPHA32F_ARB:
3608 return MESA_FORMAT_ALPHA_FLOAT32;
3609 case GL_ALPHA8I_EXT:
3610 return MESA_FORMAT_ALPHA_INT8;
3611 case GL_ALPHA16I_EXT:
3612 return MESA_FORMAT_ALPHA_INT16;
3613 case GL_ALPHA32I_EXT:
3614 return MESA_FORMAT_ALPHA_INT32;
3615 case GL_ALPHA8UI_EXT:
3616 return MESA_FORMAT_ALPHA_UINT8;
3617 case GL_ALPHA16UI_EXT:
3618 return MESA_FORMAT_ALPHA_UINT16;
3619 case GL_ALPHA32UI_EXT:
3620 return MESA_FORMAT_ALPHA_UINT32;
3621 case GL_LUMINANCE8:
3622 return MESA_FORMAT_L8;
3623 case GL_LUMINANCE16:
3624 return MESA_FORMAT_L16;
3625 case GL_LUMINANCE16F_ARB:
3626 return MESA_FORMAT_LUMINANCE_FLOAT16;
3627 case GL_LUMINANCE32F_ARB:
3628 return MESA_FORMAT_LUMINANCE_FLOAT32;
3629 case GL_LUMINANCE8I_EXT:
3630 return MESA_FORMAT_LUMINANCE_INT8;
3631 case GL_LUMINANCE16I_EXT:
3632 return MESA_FORMAT_LUMINANCE_INT16;
3633 case GL_LUMINANCE32I_EXT:
3634 return MESA_FORMAT_LUMINANCE_INT32;
3635 case GL_LUMINANCE8UI_EXT:
3636 return MESA_FORMAT_LUMINANCE_UINT8;
3637 case GL_LUMINANCE16UI_EXT:
3638 return MESA_FORMAT_LUMINANCE_UINT16;
3639 case GL_LUMINANCE32UI_EXT:
3640 return MESA_FORMAT_LUMINANCE_UINT32;
3641 case GL_LUMINANCE8_ALPHA8:
3642 return MESA_FORMAT_AL88;
3643 case GL_LUMINANCE16_ALPHA16:
3644 return MESA_FORMAT_AL1616;
3645 case GL_LUMINANCE_ALPHA16F_ARB:
3646 return MESA_FORMAT_LUMINANCE_ALPHA_FLOAT16;
3647 case GL_LUMINANCE_ALPHA32F_ARB:
3648 return MESA_FORMAT_LUMINANCE_ALPHA_FLOAT32;
3649 case GL_LUMINANCE_ALPHA8I_EXT:
3650 return MESA_FORMAT_LUMINANCE_ALPHA_INT8;
3651 case GL_LUMINANCE_ALPHA16I_EXT:
3652 return MESA_FORMAT_LUMINANCE_ALPHA_INT8;
3653 case GL_LUMINANCE_ALPHA32I_EXT:
3654 return MESA_FORMAT_LUMINANCE_ALPHA_INT16;
3655 case GL_LUMINANCE_ALPHA8UI_EXT:
3656 return MESA_FORMAT_LUMINANCE_ALPHA_UINT8;
3657 case GL_LUMINANCE_ALPHA16UI_EXT:
3658 return MESA_FORMAT_LUMINANCE_ALPHA_UINT16;
3659 case GL_LUMINANCE_ALPHA32UI_EXT:
3660 return MESA_FORMAT_LUMINANCE_ALPHA_UINT32;
3661 case GL_INTENSITY8:
3662 return MESA_FORMAT_I8;
3663 case GL_INTENSITY16:
3664 return MESA_FORMAT_I16;
3665 case GL_INTENSITY16F_ARB:
3666 return MESA_FORMAT_INTENSITY_FLOAT16;
3667 case GL_INTENSITY32F_ARB:
3668 return MESA_FORMAT_INTENSITY_FLOAT32;
3669 case GL_INTENSITY8I_EXT:
3670 return MESA_FORMAT_INTENSITY_INT8;
3671 case GL_INTENSITY16I_EXT:
3672 return MESA_FORMAT_INTENSITY_INT16;
3673 case GL_INTENSITY32I_EXT:
3674 return MESA_FORMAT_INTENSITY_INT32;
3675 case GL_INTENSITY8UI_EXT:
3676 return MESA_FORMAT_INTENSITY_UINT8;
3677 case GL_INTENSITY16UI_EXT:
3678 return MESA_FORMAT_INTENSITY_UINT16;
3679 case GL_INTENSITY32UI_EXT:
3680 return MESA_FORMAT_INTENSITY_UINT32;
3681 case GL_RGBA8:
3682 return MESA_FORMAT_RGBA8888_REV;
3683 case GL_RGBA16:
3684 return MESA_FORMAT_RGBA_16;
3685 case GL_RGBA16F_ARB:
3686 return MESA_FORMAT_RGBA_FLOAT16;
3687 case GL_RGBA32F_ARB:
3688 return MESA_FORMAT_RGBA_FLOAT32;
3689 case GL_RGBA8I_EXT:
3690 return MESA_FORMAT_RGBA_INT8;
3691 case GL_RGBA16I_EXT:
3692 return MESA_FORMAT_RGBA_INT16;
3693 case GL_RGBA32I_EXT:
3694 return MESA_FORMAT_RGBA_INT32;
3695 case GL_RGBA8UI_EXT:
3696 return MESA_FORMAT_RGBA_UINT8;
3697 case GL_RGBA16UI_EXT:
3698 return MESA_FORMAT_RGBA_UINT16;
3699 case GL_RGBA32UI_EXT:
3700 return MESA_FORMAT_RGBA_UINT32;
3701
3702 case GL_RG8:
3703 return MESA_FORMAT_GR88;
3704 case GL_RG16:
3705 return MESA_FORMAT_RG1616;
3706 case GL_RG16F:
3707 return MESA_FORMAT_RG_FLOAT16;
3708 case GL_RG32F:
3709 return MESA_FORMAT_RG_FLOAT32;
3710 case GL_RG8I:
3711 return MESA_FORMAT_RG_INT8;
3712 case GL_RG16I:
3713 return MESA_FORMAT_RG_INT16;
3714 case GL_RG32I:
3715 return MESA_FORMAT_RG_INT32;
3716 case GL_RG8UI:
3717 return MESA_FORMAT_RG_UINT8;
3718 case GL_RG16UI:
3719 return MESA_FORMAT_RG_UINT16;
3720 case GL_RG32UI:
3721 return MESA_FORMAT_RG_UINT32;
3722
3723 case GL_R8:
3724 return MESA_FORMAT_R8;
3725 case GL_R16:
3726 return MESA_FORMAT_R16;
3727 case GL_R16F:
3728 return MESA_FORMAT_R_FLOAT16;
3729 case GL_R32F:
3730 return MESA_FORMAT_R_FLOAT32;
3731 case GL_R8I:
3732 return MESA_FORMAT_R_INT8;
3733 case GL_R16I:
3734 return MESA_FORMAT_R_INT16;
3735 case GL_R32I:
3736 return MESA_FORMAT_R_INT32;
3737 case GL_R8UI:
3738 return MESA_FORMAT_R_UINT8;
3739 case GL_R16UI:
3740 return MESA_FORMAT_R_UINT16;
3741 case GL_R32UI:
3742 return MESA_FORMAT_R_UINT32;
3743
3744 default:
3745 return MESA_FORMAT_NONE;
3746 }
3747 }
3748
3749 static gl_format
3750 validate_texbuffer_format(const struct gl_context *ctx, GLenum internalFormat)
3751 {
3752 gl_format format = get_texbuffer_format(ctx, internalFormat);
3753 GLenum datatype;
3754
3755 if (format == MESA_FORMAT_NONE)
3756 return MESA_FORMAT_NONE;
3757
3758 datatype = _mesa_get_format_datatype(format);
3759 if (datatype == GL_FLOAT && !ctx->Extensions.ARB_texture_float)
3760 return MESA_FORMAT_NONE;
3761
3762 if (datatype == GL_HALF_FLOAT && !ctx->Extensions.ARB_half_float_pixel)
3763 return MESA_FORMAT_NONE;
3764
3765 /* The GL_ARB_texture_rg and GL_ARB_texture_buffer_object specs don't make
3766 * any mention of R/RG formats, but they appear in the GL 3.1 core
3767 * specification.
3768 */
3769 if (ctx->Version <= 30) {
3770 GLenum base_format = _mesa_get_format_base_format(format);
3771
3772 if (base_format == GL_R || base_format == GL_RG)
3773 return MESA_FORMAT_NONE;
3774 }
3775 return format;
3776 }
3777
3778
3779 /** GL_ARB_texture_buffer_object */
3780 void GLAPIENTRY
3781 _mesa_TexBuffer(GLenum target, GLenum internalFormat, GLuint buffer)
3782 {
3783 struct gl_texture_object *texObj;
3784 struct gl_buffer_object *bufObj;
3785 gl_format format;
3786
3787 GET_CURRENT_CONTEXT(ctx);
3788 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3789
3790 if (!(ctx->Extensions.ARB_texture_buffer_object
3791 && _mesa_is_desktop_gl(ctx))) {
3792 _mesa_error(ctx, GL_INVALID_OPERATION, "glTexBuffer");
3793 return;
3794 }
3795
3796 if (target != GL_TEXTURE_BUFFER_ARB) {
3797 _mesa_error(ctx, GL_INVALID_ENUM, "glTexBuffer(target)");
3798 return;
3799 }
3800
3801 format = validate_texbuffer_format(ctx, internalFormat);
3802 if (format == MESA_FORMAT_NONE) {
3803 _mesa_error(ctx, GL_INVALID_ENUM, "glTexBuffer(internalFormat 0x%x)",
3804 internalFormat);
3805 return;
3806 }
3807
3808 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
3809 if (buffer && !bufObj) {
3810 _mesa_error(ctx, GL_INVALID_OPERATION, "glTexBuffer(buffer %u)", buffer);
3811 return;
3812 }
3813
3814 texObj = _mesa_get_current_tex_object(ctx, target);
3815
3816 _mesa_lock_texture(ctx, texObj);
3817 {
3818 _mesa_reference_buffer_object(ctx, &texObj->BufferObject, bufObj);
3819 texObj->BufferObjectFormat = internalFormat;
3820 texObj->_BufferObjectFormat = format;
3821 }
3822 _mesa_unlock_texture(ctx, texObj);
3823 }