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