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