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