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