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