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