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