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