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