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