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