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