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