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