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