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