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