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