mesa: don't enable online compression for ASTC formats
[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 break;
1858 case GL_TEXTURE_3D:
1859
1860 /* See ETC2/EAC comment in switch case GL_TEXTURE_CUBE_MAP_ARRAY. */
1861 if (layout == MESA_FORMAT_LAYOUT_ETC2 && _mesa_is_gles3(ctx))
1862 return write_error(error, GL_INVALID_OPERATION);
1863
1864 if (layout == MESA_FORMAT_LAYOUT_BPTC) {
1865 target_can_be_compresed = ctx->Extensions.ARB_texture_compression_bptc;
1866 break;
1867 }
1868
1869 break;
1870 default:
1871 break;
1872 }
1873 return write_error(error,
1874 target_can_be_compresed ? GL_NO_ERROR : GL_INVALID_ENUM);
1875 }
1876
1877
1878 /**
1879 * Check if the given texture target value is legal for a
1880 * glTexImage1/2/3D call.
1881 */
1882 static GLboolean
1883 legal_teximage_target(struct gl_context *ctx, GLuint dims, GLenum target)
1884 {
1885 switch (dims) {
1886 case 1:
1887 switch (target) {
1888 case GL_TEXTURE_1D:
1889 case GL_PROXY_TEXTURE_1D:
1890 return _mesa_is_desktop_gl(ctx);
1891 default:
1892 return GL_FALSE;
1893 }
1894 case 2:
1895 switch (target) {
1896 case GL_TEXTURE_2D:
1897 return GL_TRUE;
1898 case GL_PROXY_TEXTURE_2D:
1899 return _mesa_is_desktop_gl(ctx);
1900 case GL_PROXY_TEXTURE_CUBE_MAP:
1901 return _mesa_is_desktop_gl(ctx)
1902 && ctx->Extensions.ARB_texture_cube_map;
1903 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
1904 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
1905 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
1906 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
1907 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
1908 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
1909 return ctx->Extensions.ARB_texture_cube_map;
1910 case GL_TEXTURE_RECTANGLE_NV:
1911 case GL_PROXY_TEXTURE_RECTANGLE_NV:
1912 return _mesa_is_desktop_gl(ctx)
1913 && ctx->Extensions.NV_texture_rectangle;
1914 case GL_TEXTURE_1D_ARRAY_EXT:
1915 case GL_PROXY_TEXTURE_1D_ARRAY_EXT:
1916 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_array;
1917 default:
1918 return GL_FALSE;
1919 }
1920 case 3:
1921 switch (target) {
1922 case GL_TEXTURE_3D:
1923 return GL_TRUE;
1924 case GL_PROXY_TEXTURE_3D:
1925 return _mesa_is_desktop_gl(ctx);
1926 case GL_TEXTURE_2D_ARRAY_EXT:
1927 return (_mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_array)
1928 || _mesa_is_gles3(ctx);
1929 case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
1930 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_array;
1931 case GL_TEXTURE_CUBE_MAP_ARRAY:
1932 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
1933 return ctx->Extensions.ARB_texture_cube_map_array;
1934 default:
1935 return GL_FALSE;
1936 }
1937 default:
1938 _mesa_problem(ctx, "invalid dims=%u in legal_teximage_target()", dims);
1939 return GL_FALSE;
1940 }
1941 }
1942
1943
1944 /**
1945 * Check if the given texture target value is legal for a
1946 * glTexSubImage, glCopyTexSubImage or glCopyTexImage call.
1947 * The difference compared to legal_teximage_target() above is that
1948 * proxy targets are not supported.
1949 */
1950 static GLboolean
1951 legal_texsubimage_target(struct gl_context *ctx, GLuint dims, GLenum target,
1952 bool dsa)
1953 {
1954 switch (dims) {
1955 case 1:
1956 return _mesa_is_desktop_gl(ctx) && target == GL_TEXTURE_1D;
1957 case 2:
1958 switch (target) {
1959 case GL_TEXTURE_2D:
1960 return GL_TRUE;
1961 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
1962 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
1963 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
1964 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
1965 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
1966 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
1967 return ctx->Extensions.ARB_texture_cube_map;
1968 case GL_TEXTURE_RECTANGLE_NV:
1969 return _mesa_is_desktop_gl(ctx)
1970 && ctx->Extensions.NV_texture_rectangle;
1971 case GL_TEXTURE_1D_ARRAY_EXT:
1972 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_array;
1973 default:
1974 return GL_FALSE;
1975 }
1976 case 3:
1977 switch (target) {
1978 case GL_TEXTURE_3D:
1979 return GL_TRUE;
1980 case GL_TEXTURE_2D_ARRAY_EXT:
1981 return (_mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_array)
1982 || _mesa_is_gles3(ctx);
1983 case GL_TEXTURE_CUBE_MAP_ARRAY:
1984 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
1985 return ctx->Extensions.ARB_texture_cube_map_array;
1986
1987 /* Table 8.15 of the OpenGL 4.5 core profile spec
1988 * (20141030) says that TEXTURE_CUBE_MAP is valid for TextureSubImage3D
1989 * and CopyTextureSubImage3D.
1990 */
1991 case GL_TEXTURE_CUBE_MAP:
1992 return dsa;
1993 default:
1994 return GL_FALSE;
1995 }
1996 default:
1997 _mesa_problem(ctx, "invalid dims=%u in legal_texsubimage_target()",
1998 dims);
1999 return GL_FALSE;
2000 }
2001 }
2002
2003
2004 /**
2005 * Helper function to determine if a texture object is mutable (in terms
2006 * of GL_ARB_texture_storage).
2007 */
2008 static GLboolean
2009 mutable_tex_object(struct gl_context *ctx, GLenum target)
2010 {
2011 struct gl_texture_object *texObj = _mesa_get_current_tex_object(ctx, target);
2012 if (!texObj)
2013 return GL_FALSE;
2014
2015 return !texObj->Immutable;
2016 }
2017
2018
2019 /**
2020 * Return expected size of a compressed texture.
2021 */
2022 static GLuint
2023 compressed_tex_size(GLsizei width, GLsizei height, GLsizei depth,
2024 GLenum glformat)
2025 {
2026 mesa_format mesaFormat = _mesa_glenum_to_compressed_format(glformat);
2027 return _mesa_format_image_size(mesaFormat, width, height, depth);
2028 }
2029
2030 /**
2031 * Verify that a texture format is valid with a particular target
2032 *
2033 * In particular, textures with base format of \c GL_DEPTH_COMPONENT or
2034 * \c GL_DEPTH_STENCIL are only valid with certain, context dependent texture
2035 * targets.
2036 *
2037 * \param ctx GL context
2038 * \param target Texture target
2039 * \param internalFormat Internal format of the texture image
2040 * \param dimensions Dimensionality at the caller. This is \b not used
2041 * in the validation. It is only used when logging
2042 * error messages.
2043 * \param caller Base name of the calling function (e.g.,
2044 * "glTexImage" or "glTexStorage").
2045 *
2046 * \returns true if the combination is legal, false otherwise.
2047 */
2048 bool
2049 _mesa_legal_texture_base_format_for_target(struct gl_context *ctx,
2050 GLenum target, GLenum internalFormat,
2051 unsigned dimensions,
2052 const char *caller)
2053 {
2054 if (_mesa_base_tex_format(ctx, internalFormat) == GL_DEPTH_COMPONENT
2055 || _mesa_base_tex_format(ctx, internalFormat) == GL_DEPTH_STENCIL
2056 || _mesa_base_tex_format(ctx, internalFormat) == GL_STENCIL_INDEX) {
2057 /* Section 3.8.3 (Texture Image Specification) of the OpenGL 3.3 Core
2058 * Profile spec says:
2059 *
2060 * "Textures with a base internal format of DEPTH_COMPONENT or
2061 * DEPTH_STENCIL are supported by texture image specification
2062 * commands only if target is TEXTURE_1D, TEXTURE_2D,
2063 * TEXTURE_1D_ARRAY, TEXTURE_2D_ARRAY, TEXTURE_RECTANGLE,
2064 * TEXTURE_CUBE_MAP, PROXY_TEXTURE_1D, PROXY_TEXTURE_2D,
2065 * PROXY_TEXTURE_1D_ARRAY, PROXY_TEXTURE_2D_ARRAY,
2066 * PROXY_TEXTURE_RECTANGLE, or PROXY_TEXTURE_CUBE_MAP. Using these
2067 * formats in conjunction with any other target will result in an
2068 * INVALID_OPERATION error."
2069 *
2070 * Cubemaps are only supported with desktop OpenGL version >= 3.0,
2071 * EXT_gpu_shader4, or, on OpenGL ES 2.0+, OES_depth_texture_cube_map.
2072 */
2073 if (target != GL_TEXTURE_1D &&
2074 target != GL_PROXY_TEXTURE_1D &&
2075 target != GL_TEXTURE_2D &&
2076 target != GL_PROXY_TEXTURE_2D &&
2077 target != GL_TEXTURE_1D_ARRAY &&
2078 target != GL_PROXY_TEXTURE_1D_ARRAY &&
2079 target != GL_TEXTURE_2D_ARRAY &&
2080 target != GL_PROXY_TEXTURE_2D_ARRAY &&
2081 target != GL_TEXTURE_RECTANGLE_ARB &&
2082 target != GL_PROXY_TEXTURE_RECTANGLE_ARB &&
2083 !((_mesa_is_cube_face(target) ||
2084 target == GL_TEXTURE_CUBE_MAP ||
2085 target == GL_PROXY_TEXTURE_CUBE_MAP) &&
2086 (ctx->Version >= 30 || ctx->Extensions.EXT_gpu_shader4
2087 || (ctx->API == API_OPENGLES2 && ctx->Extensions.OES_depth_texture_cube_map))) &&
2088 !((target == GL_TEXTURE_CUBE_MAP_ARRAY ||
2089 target == GL_PROXY_TEXTURE_CUBE_MAP_ARRAY) &&
2090 ctx->Extensions.ARB_texture_cube_map_array)) {
2091 _mesa_error(ctx, GL_INVALID_OPERATION,
2092 "%s%dD(bad target for depth texture)",
2093 caller, dimensions);
2094 return false;
2095 }
2096 }
2097
2098 return true;
2099 }
2100
2101 static bool
2102 texture_formats_agree(GLenum internalFormat,
2103 GLenum format)
2104 {
2105 GLboolean colorFormat;
2106 GLboolean is_format_depth_or_depthstencil;
2107 GLboolean is_internalFormat_depth_or_depthstencil;
2108
2109 /* Even though there are no color-index textures, we still have to support
2110 * uploading color-index data and remapping it to RGB via the
2111 * GL_PIXEL_MAP_I_TO_[RGBA] tables.
2112 */
2113 const GLboolean indexFormat = (format == GL_COLOR_INDEX);
2114
2115 is_internalFormat_depth_or_depthstencil =
2116 _mesa_is_depth_format(internalFormat) ||
2117 _mesa_is_depthstencil_format(internalFormat);
2118
2119 is_format_depth_or_depthstencil =
2120 _mesa_is_depth_format(format) ||
2121 _mesa_is_depthstencil_format(format);
2122
2123 colorFormat = _mesa_is_color_format(format);
2124
2125 if (_mesa_is_color_format(internalFormat) && !colorFormat && !indexFormat)
2126 return false;
2127
2128 if (is_internalFormat_depth_or_depthstencil !=
2129 is_format_depth_or_depthstencil)
2130 return false;
2131
2132 if (_mesa_is_ycbcr_format(internalFormat) != _mesa_is_ycbcr_format(format))
2133 return false;
2134
2135 return true;
2136 }
2137
2138 /**
2139 * Test the combination of format, type and internal format arguments of
2140 * different texture operations on GLES.
2141 *
2142 * \param ctx GL context.
2143 * \param format pixel data format given by the user.
2144 * \param type pixel data type given by the user.
2145 * \param internalFormat internal format given by the user.
2146 * \param dimensions texture image dimensions (must be 1, 2 or 3).
2147 * \param callerName name of the caller function to print in the error message
2148 *
2149 * \return true if a error is found, false otherwise
2150 *
2151 * Currently, it is used by texture_error_check() and texsubimage_error_check().
2152 */
2153 static bool
2154 texture_format_error_check_gles(struct gl_context *ctx, GLenum format,
2155 GLenum type, GLenum internalFormat,
2156 GLuint dimensions, const char *callerName)
2157 {
2158 GLenum err;
2159
2160 if (_mesa_is_gles3(ctx)) {
2161 err = _mesa_es3_error_check_format_and_type(ctx, format, type,
2162 internalFormat);
2163 if (err != GL_NO_ERROR) {
2164 _mesa_error(ctx, err,
2165 "%s(format = %s, type = %s, internalformat = %s)",
2166 callerName, _mesa_enum_to_string(format),
2167 _mesa_enum_to_string(type),
2168 _mesa_enum_to_string(internalFormat));
2169 return true;
2170 }
2171 }
2172 else {
2173 err = _mesa_es_error_check_format_and_type(format, type, dimensions);
2174 if (err != GL_NO_ERROR) {
2175 _mesa_error(ctx, err, "%s(format = %s, type = %s)",
2176 callerName, _mesa_enum_to_string(format),
2177 _mesa_enum_to_string(type));
2178 return true;
2179 }
2180 }
2181
2182 return false;
2183 }
2184
2185 /**
2186 * Test the glTexImage[123]D() parameters for errors.
2187 *
2188 * \param ctx GL context.
2189 * \param dimensions texture image dimensions (must be 1, 2 or 3).
2190 * \param target texture target given by the user (already validated).
2191 * \param level image level given by the user.
2192 * \param internalFormat internal format given by the user.
2193 * \param format pixel data format given by the user.
2194 * \param type pixel data type given by the user.
2195 * \param width image width given by the user.
2196 * \param height image height given by the user.
2197 * \param depth image depth given by the user.
2198 * \param border image border given by the user.
2199 *
2200 * \return GL_TRUE if a error is found, GL_FALSE otherwise
2201 *
2202 * Verifies each of the parameters against the constants specified in
2203 * __struct gl_contextRec::Const and the supported extensions, and according
2204 * to the OpenGL specification.
2205 * Note that we don't fully error-check the width, height, depth values
2206 * here. That's done in _mesa_legal_texture_dimensions() which is used
2207 * by several other GL entrypoints. Plus, texture dims have a special
2208 * interaction with proxy textures.
2209 */
2210 static GLboolean
2211 texture_error_check( struct gl_context *ctx,
2212 GLuint dimensions, GLenum target,
2213 GLint level, GLint internalFormat,
2214 GLenum format, GLenum type,
2215 GLint width, GLint height,
2216 GLint depth, GLint border,
2217 const GLvoid *pixels )
2218 {
2219 GLenum err;
2220
2221 /* Note: for proxy textures, some error conditions immediately generate
2222 * a GL error in the usual way. But others do not generate a GL error.
2223 * Instead, they cause the width, height, depth, format fields of the
2224 * texture image to be zeroed-out. The GL spec seems to indicate that the
2225 * zero-out behaviour is only used in cases related to memory allocation.
2226 */
2227
2228 /* level check */
2229 if (level < 0 || level >= _mesa_max_texture_levels(ctx, target)) {
2230 _mesa_error(ctx, GL_INVALID_VALUE,
2231 "glTexImage%dD(level=%d)", dimensions, level);
2232 return GL_TRUE;
2233 }
2234
2235 /* Check border */
2236 if (border < 0 || border > 1 ||
2237 ((ctx->API != API_OPENGL_COMPAT ||
2238 target == GL_TEXTURE_RECTANGLE_NV ||
2239 target == GL_PROXY_TEXTURE_RECTANGLE_NV) && border != 0)) {
2240 _mesa_error(ctx, GL_INVALID_VALUE,
2241 "glTexImage%dD(border=%d)", dimensions, border);
2242 return GL_TRUE;
2243 }
2244
2245 if (width < 0 || height < 0 || depth < 0) {
2246 _mesa_error(ctx, GL_INVALID_VALUE,
2247 "glTexImage%dD(width, height or depth < 0)", dimensions);
2248 return GL_TRUE;
2249 }
2250
2251 /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
2252 * combinations of format, internalFormat, and type that can be used.
2253 * Formats and types that require additional extensions (e.g., GL_FLOAT
2254 * requires GL_OES_texture_float) are filtered elsewhere.
2255 */
2256 if (_mesa_is_gles(ctx) &&
2257 texture_format_error_check_gles(ctx, format, type, internalFormat,
2258 dimensions, "glTexImage%dD")) {
2259 return GL_TRUE;
2260 }
2261
2262 /* Check internalFormat */
2263 if (_mesa_base_tex_format(ctx, internalFormat) < 0) {
2264 _mesa_error(ctx, GL_INVALID_VALUE,
2265 "glTexImage%dD(internalFormat=%s)",
2266 dimensions, _mesa_enum_to_string(internalFormat));
2267 return GL_TRUE;
2268 }
2269
2270 /* Check incoming image format and type */
2271 err = _mesa_error_check_format_and_type(ctx, format, type);
2272 if (err != GL_NO_ERROR) {
2273 _mesa_error(ctx, err,
2274 "glTexImage%dD(incompatible format = %s, type = %s)",
2275 dimensions, _mesa_enum_to_string(format),
2276 _mesa_enum_to_string(type));
2277 return GL_TRUE;
2278 }
2279
2280 /* validate the bound PBO, if any */
2281 if (!_mesa_validate_pbo_source(ctx, dimensions, &ctx->Unpack,
2282 width, height, depth, format, type,
2283 INT_MAX, pixels, "glTexImage")) {
2284 return GL_TRUE;
2285 }
2286
2287 /* make sure internal format and format basically agree */
2288 if (!texture_formats_agree(internalFormat, format)) {
2289 _mesa_error(ctx, GL_INVALID_OPERATION,
2290 "glTexImage%dD(incompatible internalFormat = %s, format = %s)",
2291 dimensions, _mesa_enum_to_string(internalFormat),
2292 _mesa_enum_to_string(format));
2293 return GL_TRUE;
2294 }
2295
2296 /* additional checks for ycbcr textures */
2297 if (internalFormat == GL_YCBCR_MESA) {
2298 assert(ctx->Extensions.MESA_ycbcr_texture);
2299 if (type != GL_UNSIGNED_SHORT_8_8_MESA &&
2300 type != GL_UNSIGNED_SHORT_8_8_REV_MESA) {
2301 char message[100];
2302 _mesa_snprintf(message, sizeof(message),
2303 "glTexImage%dD(format/type YCBCR mismatch)",
2304 dimensions);
2305 _mesa_error(ctx, GL_INVALID_ENUM, "%s", message);
2306 return GL_TRUE; /* error */
2307 }
2308 if (target != GL_TEXTURE_2D &&
2309 target != GL_PROXY_TEXTURE_2D &&
2310 target != GL_TEXTURE_RECTANGLE_NV &&
2311 target != GL_PROXY_TEXTURE_RECTANGLE_NV) {
2312 _mesa_error(ctx, GL_INVALID_ENUM,
2313 "glTexImage%dD(bad target for YCbCr texture)",
2314 dimensions);
2315 return GL_TRUE;
2316 }
2317 if (border != 0) {
2318 char message[100];
2319 _mesa_snprintf(message, sizeof(message),
2320 "glTexImage%dD(format=GL_YCBCR_MESA and border=%d)",
2321 dimensions, border);
2322 _mesa_error(ctx, GL_INVALID_VALUE, "%s", message);
2323 return GL_TRUE;
2324 }
2325 }
2326
2327 /* additional checks for depth textures */
2328 if (!_mesa_legal_texture_base_format_for_target(ctx, target, internalFormat,
2329 dimensions, "glTexImage"))
2330 return GL_TRUE;
2331
2332 /* additional checks for compressed textures */
2333 if (_mesa_is_compressed_format(ctx, internalFormat)) {
2334 GLenum err;
2335 if (!_mesa_target_can_be_compressed(ctx, target, internalFormat, &err)) {
2336 _mesa_error(ctx, err,
2337 "glTexImage%dD(target can't be compressed)", dimensions);
2338 return GL_TRUE;
2339 }
2340 if (_mesa_format_no_online_compression(ctx, internalFormat)) {
2341 _mesa_error(ctx, GL_INVALID_OPERATION,
2342 "glTexImage%dD(no compression for format)", dimensions);
2343 return GL_TRUE;
2344 }
2345 if (border != 0) {
2346 _mesa_error(ctx, GL_INVALID_OPERATION,
2347 "glTexImage%dD(border!=0)", dimensions);
2348 return GL_TRUE;
2349 }
2350 }
2351
2352 /* additional checks for integer textures */
2353 if ((ctx->Version >= 30 || ctx->Extensions.EXT_texture_integer) &&
2354 (_mesa_is_enum_format_integer(format) !=
2355 _mesa_is_enum_format_integer(internalFormat))) {
2356 _mesa_error(ctx, GL_INVALID_OPERATION,
2357 "glTexImage%dD(integer/non-integer format mismatch)",
2358 dimensions);
2359 return GL_TRUE;
2360 }
2361
2362 if (!mutable_tex_object(ctx, target)) {
2363 _mesa_error(ctx, GL_INVALID_OPERATION,
2364 "glTexImage%dD(immutable texture)", dimensions);
2365 return GL_TRUE;
2366 }
2367
2368 /* if we get here, the parameters are OK */
2369 return GL_FALSE;
2370 }
2371
2372
2373 /**
2374 * Error checking for glCompressedTexImage[123]D().
2375 * Note that the width, height and depth values are not fully error checked
2376 * here.
2377 * \return GL_TRUE if a error is found, GL_FALSE otherwise
2378 */
2379 static GLenum
2380 compressed_texture_error_check(struct gl_context *ctx, GLint dimensions,
2381 GLenum target, GLint level,
2382 GLenum internalFormat, GLsizei width,
2383 GLsizei height, GLsizei depth, GLint border,
2384 GLsizei imageSize, const GLvoid *data)
2385 {
2386 const GLint maxLevels = _mesa_max_texture_levels(ctx, target);
2387 GLint expectedSize;
2388 GLenum error = GL_NO_ERROR;
2389 char *reason = ""; /* no error */
2390
2391 if (!_mesa_target_can_be_compressed(ctx, target, internalFormat, &error)) {
2392 reason = "target";
2393 goto error;
2394 }
2395
2396 /* This will detect any invalid internalFormat value */
2397 if (!_mesa_is_compressed_format(ctx, internalFormat)) {
2398 _mesa_error(ctx, GL_INVALID_ENUM,
2399 "glCompressedTexImage%dD(internalFormat=%s)",
2400 dimensions, _mesa_enum_to_string(internalFormat));
2401 return GL_TRUE;
2402 }
2403
2404 /* validate the bound PBO, if any */
2405 if (!_mesa_validate_pbo_source_compressed(ctx, dimensions, &ctx->Unpack,
2406 imageSize, data,
2407 "glCompressedTexImage")) {
2408 return GL_TRUE;
2409 }
2410
2411 switch (internalFormat) {
2412 case GL_PALETTE4_RGB8_OES:
2413 case GL_PALETTE4_RGBA8_OES:
2414 case GL_PALETTE4_R5_G6_B5_OES:
2415 case GL_PALETTE4_RGBA4_OES:
2416 case GL_PALETTE4_RGB5_A1_OES:
2417 case GL_PALETTE8_RGB8_OES:
2418 case GL_PALETTE8_RGBA8_OES:
2419 case GL_PALETTE8_R5_G6_B5_OES:
2420 case GL_PALETTE8_RGBA4_OES:
2421 case GL_PALETTE8_RGB5_A1_OES:
2422 /* check level (note that level should be zero or less!) */
2423 if (level > 0 || level < -maxLevels) {
2424 reason = "level";
2425 error = GL_INVALID_VALUE;
2426 goto error;
2427 }
2428
2429 if (dimensions != 2) {
2430 reason = "compressed paletted textures must be 2D";
2431 error = GL_INVALID_OPERATION;
2432 goto error;
2433 }
2434
2435 /* Figure out the expected texture size (in bytes). This will be
2436 * checked against the actual size later.
2437 */
2438 expectedSize = _mesa_cpal_compressed_size(level, internalFormat,
2439 width, height);
2440
2441 /* This is for the benefit of the TestProxyTexImage below. It expects
2442 * level to be non-negative. OES_compressed_paletted_texture uses a
2443 * weird mechanism where the level specified to glCompressedTexImage2D
2444 * is -(n-1) number of levels in the texture, and the data specifies the
2445 * complete mipmap stack. This is done to ensure the palette is the
2446 * same for all levels.
2447 */
2448 level = -level;
2449 break;
2450
2451 default:
2452 /* check level */
2453 if (level < 0 || level >= maxLevels) {
2454 reason = "level";
2455 error = GL_INVALID_VALUE;
2456 goto error;
2457 }
2458
2459 /* Figure out the expected texture size (in bytes). This will be
2460 * checked against the actual size later.
2461 */
2462 expectedSize = compressed_tex_size(width, height, depth, internalFormat);
2463 break;
2464 }
2465
2466 /* This should really never fail */
2467 if (_mesa_base_tex_format(ctx, internalFormat) < 0) {
2468 reason = "internalFormat";
2469 error = GL_INVALID_ENUM;
2470 goto error;
2471 }
2472
2473 /* No compressed formats support borders at this time */
2474 if (border != 0) {
2475 reason = "border != 0";
2476 error = GL_INVALID_VALUE;
2477 goto error;
2478 }
2479
2480 /* Check for invalid pixel storage modes */
2481 if (!_mesa_compressed_pixel_storage_error_check(ctx, dimensions,
2482 &ctx->Unpack,
2483 "glCompressedTexImage")) {
2484 return GL_FALSE;
2485 }
2486
2487 /* check image size in bytes */
2488 if (expectedSize != imageSize) {
2489 /* Per GL_ARB_texture_compression: GL_INVALID_VALUE is generated [...]
2490 * if <imageSize> is not consistent with the format, dimensions, and
2491 * contents of the specified image.
2492 */
2493 reason = "imageSize inconsistant with width/height/format";
2494 error = GL_INVALID_VALUE;
2495 goto error;
2496 }
2497
2498 if (!mutable_tex_object(ctx, target)) {
2499 reason = "immutable texture";
2500 error = GL_INVALID_OPERATION;
2501 goto error;
2502 }
2503
2504 return GL_FALSE;
2505
2506 error:
2507 /* Note: not all error paths exit through here. */
2508 _mesa_error(ctx, error, "glCompressedTexImage%dD(%s)",
2509 dimensions, reason);
2510 return GL_TRUE;
2511 }
2512
2513
2514
2515 /**
2516 * Test glTexSubImage[123]D() parameters for errors.
2517 *
2518 * \param ctx GL context.
2519 * \param dimensions texture image dimensions (must be 1, 2 or 3).
2520 * \param target texture target given by the user (already validated)
2521 * \param level image level given by the user.
2522 * \param xoffset sub-image x offset given by the user.
2523 * \param yoffset sub-image y offset given by the user.
2524 * \param zoffset sub-image z offset given by the user.
2525 * \param format pixel data format given by the user.
2526 * \param type pixel data type given by the user.
2527 * \param width image width given by the user.
2528 * \param height image height given by the user.
2529 * \param depth image depth given by the user.
2530 *
2531 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2532 *
2533 * Verifies each of the parameters against the constants specified in
2534 * __struct gl_contextRec::Const and the supported extensions, and according
2535 * to the OpenGL specification.
2536 */
2537 static GLboolean
2538 texsubimage_error_check(struct gl_context *ctx, GLuint dimensions,
2539 struct gl_texture_object *texObj,
2540 GLenum target, GLint level,
2541 GLint xoffset, GLint yoffset, GLint zoffset,
2542 GLint width, GLint height, GLint depth,
2543 GLenum format, GLenum type, const GLvoid *pixels,
2544 bool dsa, const char *callerName)
2545 {
2546 struct gl_texture_image *texImage;
2547 GLenum err;
2548
2549 if (!texObj) {
2550 /* must be out of memory */
2551 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s()", callerName);
2552 return GL_TRUE;
2553 }
2554
2555 /* level check */
2556 if (level < 0 || level >= _mesa_max_texture_levels(ctx, target)) {
2557 _mesa_error(ctx, GL_INVALID_VALUE, "%s(level=%d)", callerName, level);
2558 return GL_TRUE;
2559 }
2560
2561 texImage = _mesa_select_tex_image(texObj, target, level);
2562 if (!texImage) {
2563 /* non-existant texture level */
2564 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid texture image)",
2565 callerName);
2566 return GL_TRUE;
2567 }
2568
2569 err = _mesa_error_check_format_and_type(ctx, format, type);
2570 if (err != GL_NO_ERROR) {
2571 _mesa_error(ctx, err,
2572 "%s(incompatible format = %s, type = %s)",
2573 callerName, _mesa_enum_to_string(format),
2574 _mesa_enum_to_string(type));
2575 return GL_TRUE;
2576 }
2577
2578 /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
2579 * combinations of format, internalFormat, and type that can be used.
2580 * Formats and types that require additional extensions (e.g., GL_FLOAT
2581 * requires GL_OES_texture_float) are filtered elsewhere.
2582 */
2583 if (_mesa_is_gles(ctx) &&
2584 texture_format_error_check_gles(ctx, format, type,
2585 texImage->InternalFormat,
2586 dimensions, callerName)) {
2587 return GL_TRUE;
2588 }
2589
2590 /* validate the bound PBO, if any */
2591 if (!_mesa_validate_pbo_source(ctx, dimensions, &ctx->Unpack,
2592 width, height, depth, format, type,
2593 INT_MAX, pixels, callerName)) {
2594 return GL_TRUE;
2595 }
2596
2597 if (error_check_subtexture_dimensions(ctx, dimensions,
2598 texImage, xoffset, yoffset, zoffset,
2599 width, height, depth, callerName)) {
2600 return GL_TRUE;
2601 }
2602
2603 if (_mesa_is_format_compressed(texImage->TexFormat)) {
2604 if (_mesa_format_no_online_compression(ctx, texImage->InternalFormat)) {
2605 _mesa_error(ctx, GL_INVALID_OPERATION,
2606 "%s(no compression for format)", callerName);
2607 return GL_TRUE;
2608 }
2609 }
2610
2611 if (ctx->Version >= 30 || ctx->Extensions.EXT_texture_integer) {
2612 /* both source and dest must be integer-valued, or neither */
2613 if (_mesa_is_format_integer_color(texImage->TexFormat) !=
2614 _mesa_is_enum_format_integer(format)) {
2615 _mesa_error(ctx, GL_INVALID_OPERATION,
2616 "%s(integer/non-integer format mismatch)", callerName);
2617 return GL_TRUE;
2618 }
2619 }
2620
2621 return GL_FALSE;
2622 }
2623
2624
2625 /**
2626 * Test glCopyTexImage[12]D() parameters for errors.
2627 *
2628 * \param ctx GL context.
2629 * \param dimensions texture image dimensions (must be 1, 2 or 3).
2630 * \param target texture target given by the user.
2631 * \param level image level given by the user.
2632 * \param internalFormat internal format given by the user.
2633 * \param width image width given by the user.
2634 * \param height image height given by the user.
2635 * \param border texture border.
2636 *
2637 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2638 *
2639 * Verifies each of the parameters against the constants specified in
2640 * __struct gl_contextRec::Const and the supported extensions, and according
2641 * to the OpenGL specification.
2642 */
2643 static GLboolean
2644 copytexture_error_check( struct gl_context *ctx, GLuint dimensions,
2645 GLenum target, GLint level, GLint internalFormat,
2646 GLint width, GLint height, GLint border )
2647 {
2648 GLint baseFormat;
2649 GLint rb_base_format;
2650 struct gl_renderbuffer *rb;
2651 GLenum rb_internal_format;
2652
2653 /* check target */
2654 if (!legal_texsubimage_target(ctx, dimensions, target, false)) {
2655 _mesa_error(ctx, GL_INVALID_ENUM, "glCopyTexImage%uD(target=%s)",
2656 dimensions, _mesa_enum_to_string(target));
2657 return GL_TRUE;
2658 }
2659
2660 /* level check */
2661 if (level < 0 || level >= _mesa_max_texture_levels(ctx, target)) {
2662 _mesa_error(ctx, GL_INVALID_VALUE,
2663 "glCopyTexImage%dD(level=%d)", dimensions, level);
2664 return GL_TRUE;
2665 }
2666
2667 /* Check that the source buffer is complete */
2668 if (_mesa_is_user_fbo(ctx->ReadBuffer)) {
2669 if (ctx->ReadBuffer->_Status == 0) {
2670 _mesa_test_framebuffer_completeness(ctx, ctx->ReadBuffer);
2671 }
2672 if (ctx->ReadBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
2673 _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
2674 "glCopyTexImage%dD(invalid readbuffer)", dimensions);
2675 return GL_TRUE;
2676 }
2677
2678 if (ctx->ReadBuffer->Visual.samples > 0) {
2679 _mesa_error(ctx, GL_INVALID_OPERATION,
2680 "glCopyTexImage%dD(multisample FBO)", dimensions);
2681 return GL_TRUE;
2682 }
2683 }
2684
2685 /* Check border */
2686 if (border < 0 || border > 1 ||
2687 ((ctx->API != API_OPENGL_COMPAT ||
2688 target == GL_TEXTURE_RECTANGLE_NV ||
2689 target == GL_PROXY_TEXTURE_RECTANGLE_NV) && border != 0)) {
2690 _mesa_error(ctx, GL_INVALID_VALUE,
2691 "glCopyTexImage%dD(border=%d)", dimensions, border);
2692 return GL_TRUE;
2693 }
2694
2695 /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
2696 * internalFormat.
2697 */
2698 if (_mesa_is_gles(ctx) && !_mesa_is_gles3(ctx)) {
2699 switch (internalFormat) {
2700 case GL_ALPHA:
2701 case GL_RGB:
2702 case GL_RGBA:
2703 case GL_LUMINANCE:
2704 case GL_LUMINANCE_ALPHA:
2705 break;
2706 default:
2707 _mesa_error(ctx, GL_INVALID_ENUM,
2708 "glCopyTexImage%dD(internalFormat=%s)", dimensions,
2709 _mesa_enum_to_string(internalFormat));
2710 return GL_TRUE;
2711 }
2712 }
2713
2714 baseFormat = _mesa_base_tex_format(ctx, internalFormat);
2715 if (baseFormat < 0) {
2716 _mesa_error(ctx, GL_INVALID_ENUM,
2717 "glCopyTexImage%dD(internalFormat=%s)", dimensions,
2718 _mesa_enum_to_string(internalFormat));
2719 return GL_TRUE;
2720 }
2721
2722 rb = _mesa_get_read_renderbuffer_for_format(ctx, internalFormat);
2723 if (rb == NULL) {
2724 _mesa_error(ctx, GL_INVALID_OPERATION,
2725 "glCopyTexImage%dD(read buffer)", dimensions);
2726 return GL_TRUE;
2727 }
2728
2729 rb_internal_format = rb->InternalFormat;
2730 rb_base_format = _mesa_base_tex_format(ctx, rb->InternalFormat);
2731 if (_mesa_is_color_format(internalFormat)) {
2732 if (rb_base_format < 0) {
2733 _mesa_error(ctx, GL_INVALID_VALUE,
2734 "glCopyTexImage%dD(internalFormat=%s)", dimensions,
2735 _mesa_enum_to_string(internalFormat));
2736 return GL_TRUE;
2737 }
2738 }
2739
2740 if (_mesa_is_gles(ctx)) {
2741 bool valid = true;
2742 if (_mesa_base_format_component_count(baseFormat) >
2743 _mesa_base_format_component_count(rb_base_format)) {
2744 valid = false;
2745 }
2746 if (baseFormat == GL_DEPTH_COMPONENT ||
2747 baseFormat == GL_DEPTH_STENCIL ||
2748 rb_base_format == GL_DEPTH_COMPONENT ||
2749 rb_base_format == GL_DEPTH_STENCIL ||
2750 ((baseFormat == GL_LUMINANCE_ALPHA ||
2751 baseFormat == GL_ALPHA) &&
2752 rb_base_format != GL_RGBA) ||
2753 internalFormat == GL_RGB9_E5) {
2754 valid = false;
2755 }
2756 if (internalFormat == GL_RGB9_E5) {
2757 valid = false;
2758 }
2759 if (!valid) {
2760 _mesa_error(ctx, GL_INVALID_OPERATION,
2761 "glCopyTexImage%dD(internalFormat=%s)", dimensions,
2762 _mesa_enum_to_string(internalFormat));
2763 return GL_TRUE;
2764 }
2765 }
2766
2767 if (_mesa_is_gles3(ctx)) {
2768 bool rb_is_srgb = false;
2769 bool dst_is_srgb = false;
2770
2771 if (ctx->Extensions.EXT_framebuffer_sRGB &&
2772 _mesa_get_format_color_encoding(rb->Format) == GL_SRGB) {
2773 rb_is_srgb = true;
2774 }
2775
2776 if (_mesa_get_linear_internalformat(internalFormat) != internalFormat) {
2777 dst_is_srgb = true;
2778 }
2779
2780 if (rb_is_srgb != dst_is_srgb) {
2781 /* Page 137 (page 149 of the PDF) in section 3.8.5 of the
2782 * OpenGLES 3.0.0 spec says:
2783 *
2784 * "The error INVALID_OPERATION is also generated if the
2785 * value of FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING for the
2786 * framebuffer attachment corresponding to the read buffer
2787 * is LINEAR (see section 6.1.13) and internalformat is
2788 * one of the sRGB formats described in section 3.8.16, or
2789 * if the value of FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING is
2790 * SRGB and internalformat is not one of the sRGB formats."
2791 */
2792 _mesa_error(ctx, GL_INVALID_OPERATION,
2793 "glCopyTexImage%dD(srgb usage mismatch)", dimensions);
2794 return GL_TRUE;
2795 }
2796
2797 /* Page 139, Table 3.15 of OpenGL ES 3.0 spec does not define ReadPixels
2798 * types for SNORM formats. Also, conversion to SNORM formats is not
2799 * allowed by Table 3.2 on Page 110.
2800 */
2801 if (_mesa_is_enum_format_snorm(internalFormat)) {
2802 _mesa_error(ctx, GL_INVALID_OPERATION,
2803 "glCopyTexImage%dD(internalFormat=%s)", dimensions,
2804 _mesa_enum_to_string(internalFormat));
2805 return GL_TRUE;
2806 }
2807 }
2808
2809 if (!_mesa_source_buffer_exists(ctx, baseFormat)) {
2810 _mesa_error(ctx, GL_INVALID_OPERATION,
2811 "glCopyTexImage%dD(missing readbuffer)", dimensions);
2812 return GL_TRUE;
2813 }
2814
2815 /* From the EXT_texture_integer spec:
2816 *
2817 * "INVALID_OPERATION is generated by CopyTexImage* and CopyTexSubImage*
2818 * if the texture internalformat is an integer format and the read color
2819 * buffer is not an integer format, or if the internalformat is not an
2820 * integer format and the read color buffer is an integer format."
2821 */
2822 if (_mesa_is_color_format(internalFormat)) {
2823 bool is_int = _mesa_is_enum_format_integer(internalFormat);
2824 bool is_rbint = _mesa_is_enum_format_integer(rb_internal_format);
2825 bool is_unorm = _mesa_is_enum_format_unorm(internalFormat);
2826 bool is_rbunorm = _mesa_is_enum_format_unorm(rb_internal_format);
2827 if (is_int || is_rbint) {
2828 if (is_int != is_rbint) {
2829 _mesa_error(ctx, GL_INVALID_OPERATION,
2830 "glCopyTexImage%dD(integer vs non-integer)", dimensions);
2831 return GL_TRUE;
2832 } else if (_mesa_is_gles(ctx) &&
2833 _mesa_is_enum_format_unsigned_int(internalFormat) !=
2834 _mesa_is_enum_format_unsigned_int(rb_internal_format)) {
2835 _mesa_error(ctx, GL_INVALID_OPERATION,
2836 "glCopyTexImage%dD(signed vs unsigned integer)", dimensions);
2837 return GL_TRUE;
2838 }
2839 }
2840
2841 /* From page 138 of OpenGL ES 3.0 spec:
2842 * "The error INVALID_OPERATION is generated if floating-point RGBA
2843 * data is required; if signed integer RGBA data is required and the
2844 * format of the current color buffer is not signed integer; if
2845 * unsigned integer RGBA data is required and the format of the
2846 * current color buffer is not unsigned integer; or if fixed-point
2847 * RGBA data is required and the format of the current color buffer
2848 * is not fixed-point.
2849 */
2850 if (_mesa_is_gles(ctx) && is_unorm != is_rbunorm)
2851 _mesa_error(ctx, GL_INVALID_OPERATION,
2852 "glCopyTexImage%dD(unorm vs non-unorm)", dimensions);
2853 }
2854
2855 if (_mesa_is_compressed_format(ctx, internalFormat)) {
2856 GLenum err;
2857 if (!_mesa_target_can_be_compressed(ctx, target, internalFormat, &err)) {
2858 _mesa_error(ctx, err,
2859 "glCopyTexImage%dD(target can't be compressed)", dimensions);
2860 return GL_TRUE;
2861 }
2862 if (_mesa_format_no_online_compression(ctx, internalFormat)) {
2863 _mesa_error(ctx, GL_INVALID_OPERATION,
2864 "glCopyTexImage%dD(no compression for format)", dimensions);
2865 return GL_TRUE;
2866 }
2867 if (border != 0) {
2868 _mesa_error(ctx, GL_INVALID_OPERATION,
2869 "glCopyTexImage%dD(border!=0)", dimensions);
2870 return GL_TRUE;
2871 }
2872 }
2873
2874 if (!mutable_tex_object(ctx, target)) {
2875 _mesa_error(ctx, GL_INVALID_OPERATION,
2876 "glCopyTexImage%dD(immutable texture)", dimensions);
2877 return GL_TRUE;
2878 }
2879
2880 /* if we get here, the parameters are OK */
2881 return GL_FALSE;
2882 }
2883
2884
2885 /**
2886 * Test glCopyTexSubImage[12]D() parameters for errors.
2887 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2888 */
2889 static GLboolean
2890 copytexsubimage_error_check(struct gl_context *ctx, GLuint dimensions,
2891 const struct gl_texture_object *texObj,
2892 GLenum target, GLint level,
2893 GLint xoffset, GLint yoffset, GLint zoffset,
2894 GLint width, GLint height, const char *caller)
2895 {
2896 struct gl_texture_image *texImage;
2897
2898 /* Check that the source buffer is complete */
2899 if (_mesa_is_user_fbo(ctx->ReadBuffer)) {
2900 if (ctx->ReadBuffer->_Status == 0) {
2901 _mesa_test_framebuffer_completeness(ctx, ctx->ReadBuffer);
2902 }
2903 if (ctx->ReadBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
2904 _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
2905 "%s(invalid readbuffer)", caller);
2906 return GL_TRUE;
2907 }
2908
2909 if (ctx->ReadBuffer->Visual.samples > 0) {
2910 _mesa_error(ctx, GL_INVALID_OPERATION,
2911 "%s(multisample FBO)", caller);
2912 return GL_TRUE;
2913 }
2914 }
2915
2916 /* Check level */
2917 if (level < 0 || level >= _mesa_max_texture_levels(ctx, target)) {
2918 _mesa_error(ctx, GL_INVALID_VALUE, "%s(level=%d)", caller, level);
2919 return GL_TRUE;
2920 }
2921
2922 /* Get dest image pointers */
2923 if (!texObj) {
2924 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s()", caller);
2925 return GL_TRUE;
2926 }
2927
2928 texImage = _mesa_select_tex_image(texObj, target, level);
2929 if (!texImage) {
2930 /* destination image does not exist */
2931 _mesa_error(ctx, GL_INVALID_OPERATION,
2932 "%s(invalid texture image)", caller);
2933 return GL_TRUE;
2934 }
2935
2936 if (error_check_subtexture_dimensions(ctx, dimensions, texImage,
2937 xoffset, yoffset, zoffset,
2938 width, height, 1, caller)) {
2939 return GL_TRUE;
2940 }
2941
2942 if (_mesa_is_format_compressed(texImage->TexFormat)) {
2943 if (_mesa_format_no_online_compression(ctx, texImage->InternalFormat)) {
2944 _mesa_error(ctx, GL_INVALID_OPERATION,
2945 "%s(no compression for format)", caller);
2946 return GL_TRUE;
2947 }
2948 }
2949
2950 if (texImage->InternalFormat == GL_YCBCR_MESA) {
2951 _mesa_error(ctx, GL_INVALID_OPERATION, "%s()", caller);
2952 return GL_TRUE;
2953 }
2954
2955 if (!_mesa_source_buffer_exists(ctx, texImage->_BaseFormat)) {
2956 _mesa_error(ctx, GL_INVALID_OPERATION,
2957 "%s(missing readbuffer, format=0x%x)", caller,
2958 texImage->_BaseFormat);
2959 return GL_TRUE;
2960 }
2961
2962 /* From the EXT_texture_integer spec:
2963 *
2964 * "INVALID_OPERATION is generated by CopyTexImage* and
2965 * CopyTexSubImage* if the texture internalformat is an integer format
2966 * and the read color buffer is not an integer format, or if the
2967 * internalformat is not an integer format and the read color buffer
2968 * is an integer format."
2969 */
2970 if (_mesa_is_color_format(texImage->InternalFormat)) {
2971 struct gl_renderbuffer *rb = ctx->ReadBuffer->_ColorReadBuffer;
2972
2973 if (_mesa_is_format_integer_color(rb->Format) !=
2974 _mesa_is_format_integer_color(texImage->TexFormat)) {
2975 _mesa_error(ctx, GL_INVALID_OPERATION,
2976 "%s(integer vs non-integer)", caller);
2977 return GL_TRUE;
2978 }
2979 }
2980
2981 /* if we get here, the parameters are OK */
2982 return GL_FALSE;
2983 }
2984
2985
2986 /** Callback info for walking over FBO hash table */
2987 struct cb_info
2988 {
2989 struct gl_context *ctx;
2990 struct gl_texture_object *texObj;
2991 GLuint level, face;
2992 };
2993
2994
2995 /**
2996 * Check render to texture callback. Called from _mesa_HashWalk().
2997 */
2998 static void
2999 check_rtt_cb(GLuint key, void *data, void *userData)
3000 {
3001 struct gl_framebuffer *fb = (struct gl_framebuffer *) data;
3002 const struct cb_info *info = (struct cb_info *) userData;
3003 struct gl_context *ctx = info->ctx;
3004 const struct gl_texture_object *texObj = info->texObj;
3005 const GLuint level = info->level, face = info->face;
3006
3007 /* If this is a user-created FBO */
3008 if (_mesa_is_user_fbo(fb)) {
3009 GLuint i;
3010 /* check if any of the FBO's attachments point to 'texObj' */
3011 for (i = 0; i < BUFFER_COUNT; i++) {
3012 struct gl_renderbuffer_attachment *att = fb->Attachment + i;
3013 if (att->Type == GL_TEXTURE &&
3014 att->Texture == texObj &&
3015 att->TextureLevel == level &&
3016 att->CubeMapFace == face) {
3017 _mesa_update_texture_renderbuffer(ctx, ctx->DrawBuffer, att);
3018 assert(att->Renderbuffer->TexImage);
3019 /* Mark fb status as indeterminate to force re-validation */
3020 fb->_Status = 0;
3021 }
3022 }
3023 }
3024 }
3025
3026
3027 /**
3028 * When a texture image is specified we have to check if it's bound to
3029 * any framebuffer objects (render to texture) in order to detect changes
3030 * in size or format since that effects FBO completeness.
3031 * Any FBOs rendering into the texture must be re-validated.
3032 */
3033 void
3034 _mesa_update_fbo_texture(struct gl_context *ctx,
3035 struct gl_texture_object *texObj,
3036 GLuint face, GLuint level)
3037 {
3038 /* Only check this texture if it's been marked as RenderToTexture */
3039 if (texObj->_RenderToTexture) {
3040 struct cb_info info;
3041 info.ctx = ctx;
3042 info.texObj = texObj;
3043 info.level = level;
3044 info.face = face;
3045 _mesa_HashWalk(ctx->Shared->FrameBuffers, check_rtt_cb, &info);
3046 }
3047 }
3048
3049
3050 /**
3051 * If the texture object's GenerateMipmap flag is set and we've
3052 * changed the texture base level image, regenerate the rest of the
3053 * mipmap levels now.
3054 */
3055 static inline void
3056 check_gen_mipmap(struct gl_context *ctx, GLenum target,
3057 struct gl_texture_object *texObj, GLint level)
3058 {
3059 if (texObj->GenerateMipmap &&
3060 level == texObj->BaseLevel &&
3061 level < texObj->MaxLevel) {
3062 assert(ctx->Driver.GenerateMipmap);
3063 ctx->Driver.GenerateMipmap(ctx, target, texObj);
3064 }
3065 }
3066
3067
3068 /** Debug helper: override the user-requested internal format */
3069 static GLenum
3070 override_internal_format(GLenum internalFormat, GLint width, GLint height)
3071 {
3072 #if 0
3073 if (internalFormat == GL_RGBA16F_ARB ||
3074 internalFormat == GL_RGBA32F_ARB) {
3075 printf("Convert rgba float tex to int %d x %d\n", width, height);
3076 return GL_RGBA;
3077 }
3078 else if (internalFormat == GL_RGB16F_ARB ||
3079 internalFormat == GL_RGB32F_ARB) {
3080 printf("Convert rgb float tex to int %d x %d\n", width, height);
3081 return GL_RGB;
3082 }
3083 else if (internalFormat == GL_LUMINANCE_ALPHA16F_ARB ||
3084 internalFormat == GL_LUMINANCE_ALPHA32F_ARB) {
3085 printf("Convert luminance float tex to int %d x %d\n", width, height);
3086 return GL_LUMINANCE_ALPHA;
3087 }
3088 else if (internalFormat == GL_LUMINANCE16F_ARB ||
3089 internalFormat == GL_LUMINANCE32F_ARB) {
3090 printf("Convert luminance float tex to int %d x %d\n", width, height);
3091 return GL_LUMINANCE;
3092 }
3093 else if (internalFormat == GL_ALPHA16F_ARB ||
3094 internalFormat == GL_ALPHA32F_ARB) {
3095 printf("Convert luminance float tex to int %d x %d\n", width, height);
3096 return GL_ALPHA;
3097 }
3098 /*
3099 else if (internalFormat == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT) {
3100 internalFormat = GL_RGBA;
3101 }
3102 */
3103 else {
3104 return internalFormat;
3105 }
3106 #else
3107 return internalFormat;
3108 #endif
3109 }
3110
3111
3112 /**
3113 * Choose the actual hardware format for a texture image.
3114 * Try to use the same format as the previous image level when possible.
3115 * Otherwise, ask the driver for the best format.
3116 * It's important to try to choose a consistant format for all levels
3117 * for efficient texture memory layout/allocation. In particular, this
3118 * comes up during automatic mipmap generation.
3119 */
3120 mesa_format
3121 _mesa_choose_texture_format(struct gl_context *ctx,
3122 struct gl_texture_object *texObj,
3123 GLenum target, GLint level,
3124 GLenum internalFormat, GLenum format, GLenum type)
3125 {
3126 mesa_format f;
3127
3128 /* see if we've already chosen a format for the previous level */
3129 if (level > 0) {
3130 struct gl_texture_image *prevImage =
3131 _mesa_select_tex_image(texObj, target, level - 1);
3132 /* See if the prev level is defined and has an internal format which
3133 * matches the new internal format.
3134 */
3135 if (prevImage &&
3136 prevImage->Width > 0 &&
3137 prevImage->InternalFormat == internalFormat) {
3138 /* use the same format */
3139 assert(prevImage->TexFormat != MESA_FORMAT_NONE);
3140 return prevImage->TexFormat;
3141 }
3142 }
3143
3144 /* If the application requested compression to an S3TC format but we don't
3145 * have the DXTn library, force a generic compressed format instead.
3146 */
3147 if (internalFormat != format && format != GL_NONE) {
3148 const GLenum before = internalFormat;
3149
3150 switch (internalFormat) {
3151 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
3152 if (!ctx->Mesa_DXTn)
3153 internalFormat = GL_COMPRESSED_RGB;
3154 break;
3155 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
3156 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
3157 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
3158 if (!ctx->Mesa_DXTn)
3159 internalFormat = GL_COMPRESSED_RGBA;
3160 break;
3161 default:
3162 break;
3163 }
3164
3165 if (before != internalFormat) {
3166 _mesa_warning(ctx,
3167 "DXT compression requested (%s), "
3168 "but libtxc_dxtn library not installed. Using %s "
3169 "instead.",
3170 _mesa_enum_to_string(before),
3171 _mesa_enum_to_string(internalFormat));
3172 }
3173 }
3174
3175 /* choose format from scratch */
3176 f = ctx->Driver.ChooseTextureFormat(ctx, target, internalFormat,
3177 format, type);
3178 assert(f != MESA_FORMAT_NONE);
3179 return f;
3180 }
3181
3182
3183 /**
3184 * Adjust pixel unpack params and image dimensions to strip off the
3185 * one-pixel texture border.
3186 *
3187 * Gallium and intel don't support texture borders. They've seldem been used
3188 * and seldom been implemented correctly anyway.
3189 *
3190 * \param unpackNew returns the new pixel unpack parameters
3191 */
3192 static void
3193 strip_texture_border(GLenum target,
3194 GLint *width, GLint *height, GLint *depth,
3195 const struct gl_pixelstore_attrib *unpack,
3196 struct gl_pixelstore_attrib *unpackNew)
3197 {
3198 assert(width);
3199 assert(height);
3200 assert(depth);
3201
3202 *unpackNew = *unpack;
3203
3204 if (unpackNew->RowLength == 0)
3205 unpackNew->RowLength = *width;
3206
3207 if (unpackNew->ImageHeight == 0)
3208 unpackNew->ImageHeight = *height;
3209
3210 assert(*width >= 3);
3211 unpackNew->SkipPixels++; /* skip the border */
3212 *width = *width - 2; /* reduce the width by two border pixels */
3213
3214 /* The min height of a texture with a border is 3 */
3215 if (*height >= 3 && target != GL_TEXTURE_1D_ARRAY) {
3216 unpackNew->SkipRows++; /* skip the border */
3217 *height = *height - 2; /* reduce the height by two border pixels */
3218 }
3219
3220 if (*depth >= 3 &&
3221 target != GL_TEXTURE_2D_ARRAY &&
3222 target != GL_TEXTURE_CUBE_MAP_ARRAY) {
3223 unpackNew->SkipImages++; /* skip the border */
3224 *depth = *depth - 2; /* reduce the depth by two border pixels */
3225 }
3226 }
3227
3228
3229 /**
3230 * Common code to implement all the glTexImage1D/2D/3D functions
3231 * as well as glCompressedTexImage1D/2D/3D.
3232 * \param compressed only GL_TRUE for glCompressedTexImage1D/2D/3D calls.
3233 * \param format the user's image format (only used if !compressed)
3234 * \param type the user's image type (only used if !compressed)
3235 * \param imageSize only used for glCompressedTexImage1D/2D/3D calls.
3236 */
3237 static void
3238 teximage(struct gl_context *ctx, GLboolean compressed, GLuint dims,
3239 GLenum target, GLint level, GLint internalFormat,
3240 GLsizei width, GLsizei height, GLsizei depth,
3241 GLint border, GLenum format, GLenum type,
3242 GLsizei imageSize, const GLvoid *pixels)
3243 {
3244 const char *func = compressed ? "glCompressedTexImage" : "glTexImage";
3245 struct gl_pixelstore_attrib unpack_no_border;
3246 const struct gl_pixelstore_attrib *unpack = &ctx->Unpack;
3247 struct gl_texture_object *texObj;
3248 mesa_format texFormat;
3249 GLboolean dimensionsOK, sizeOK;
3250
3251 FLUSH_VERTICES(ctx, 0);
3252
3253 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE)) {
3254 if (compressed)
3255 _mesa_debug(ctx,
3256 "glCompressedTexImage%uD %s %d %s %d %d %d %d %p\n",
3257 dims,
3258 _mesa_enum_to_string(target), level,
3259 _mesa_enum_to_string(internalFormat),
3260 width, height, depth, border, pixels);
3261 else
3262 _mesa_debug(ctx,
3263 "glTexImage%uD %s %d %s %d %d %d %d %s %s %p\n",
3264 dims,
3265 _mesa_enum_to_string(target), level,
3266 _mesa_enum_to_string(internalFormat),
3267 width, height, depth, border,
3268 _mesa_enum_to_string(format),
3269 _mesa_enum_to_string(type), pixels);
3270 }
3271
3272 internalFormat = override_internal_format(internalFormat, width, height);
3273
3274 /* target error checking */
3275 if (!legal_teximage_target(ctx, dims, target)) {
3276 _mesa_error(ctx, GL_INVALID_ENUM, "%s%uD(target=%s)",
3277 func, dims, _mesa_enum_to_string(target));
3278 return;
3279 }
3280
3281 /* general error checking */
3282 if (compressed) {
3283 if (compressed_texture_error_check(ctx, dims, target, level,
3284 internalFormat,
3285 width, height, depth,
3286 border, imageSize, pixels))
3287 return;
3288 }
3289 else {
3290 if (texture_error_check(ctx, dims, target, level, internalFormat,
3291 format, type, width, height, depth, border,
3292 pixels))
3293 return;
3294 }
3295
3296 /* Here we convert a cpal compressed image into a regular glTexImage2D
3297 * call by decompressing the texture. If we really want to support cpal
3298 * textures in any driver this would have to be changed.
3299 */
3300 if (ctx->API == API_OPENGLES && compressed && dims == 2) {
3301 switch (internalFormat) {
3302 case GL_PALETTE4_RGB8_OES:
3303 case GL_PALETTE4_RGBA8_OES:
3304 case GL_PALETTE4_R5_G6_B5_OES:
3305 case GL_PALETTE4_RGBA4_OES:
3306 case GL_PALETTE4_RGB5_A1_OES:
3307 case GL_PALETTE8_RGB8_OES:
3308 case GL_PALETTE8_RGBA8_OES:
3309 case GL_PALETTE8_R5_G6_B5_OES:
3310 case GL_PALETTE8_RGBA4_OES:
3311 case GL_PALETTE8_RGB5_A1_OES:
3312 _mesa_cpal_compressed_teximage2d(target, level, internalFormat,
3313 width, height, imageSize, pixels);
3314 return;
3315 }
3316 }
3317
3318 texObj = _mesa_get_current_tex_object(ctx, target);
3319 assert(texObj);
3320
3321 if (compressed) {
3322 /* For glCompressedTexImage() the driver has no choice about the
3323 * texture format since we'll never transcode the user's compressed
3324 * image data. The internalFormat was error checked earlier.
3325 */
3326 texFormat = _mesa_glenum_to_compressed_format(internalFormat);
3327 }
3328 else {
3329 /* In case of HALF_FLOAT_OES or FLOAT_OES, find corresponding sized
3330 * internal floating point format for the given base format.
3331 */
3332 if (_mesa_is_gles(ctx) && format == internalFormat) {
3333 if (type == GL_FLOAT) {
3334 texObj->_IsFloat = GL_TRUE;
3335 } else if (type == GL_HALF_FLOAT_OES || type == GL_HALF_FLOAT) {
3336 texObj->_IsHalfFloat = GL_TRUE;
3337 }
3338
3339 internalFormat = adjust_for_oes_float_texture(format, type);
3340 }
3341
3342 texFormat = _mesa_choose_texture_format(ctx, texObj, target, level,
3343 internalFormat, format, type);
3344 }
3345
3346 assert(texFormat != MESA_FORMAT_NONE);
3347
3348 /* check that width, height, depth are legal for the mipmap level */
3349 dimensionsOK = _mesa_legal_texture_dimensions(ctx, target, level, width,
3350 height, depth, border);
3351
3352 /* check that the texture won't take too much memory, etc */
3353 sizeOK = ctx->Driver.TestProxyTexImage(ctx, proxy_target(target),
3354 level, texFormat,
3355 width, height, depth, border);
3356
3357 if (_mesa_is_proxy_texture(target)) {
3358 /* Proxy texture: just clear or set state depending on error checking */
3359 struct gl_texture_image *texImage =
3360 get_proxy_tex_image(ctx, target, level);
3361
3362 if (!texImage)
3363 return; /* GL_OUT_OF_MEMORY already recorded */
3364
3365 if (dimensionsOK && sizeOK) {
3366 _mesa_init_teximage_fields(ctx, texImage, width, height, depth,
3367 border, internalFormat, texFormat);
3368 }
3369 else {
3370 clear_teximage_fields(texImage);
3371 }
3372 }
3373 else {
3374 /* non-proxy target */
3375 const GLuint face = _mesa_tex_target_to_face(target);
3376 struct gl_texture_image *texImage;
3377
3378 if (!dimensionsOK) {
3379 _mesa_error(ctx, GL_INVALID_VALUE,
3380 "%s%uD(invalid width or height or depth)",
3381 func, dims);
3382 return;
3383 }
3384
3385 if (!sizeOK) {
3386 _mesa_error(ctx, GL_OUT_OF_MEMORY,
3387 "%s%uD(image too large: %d x %d x %d, %s format)",
3388 func, dims, width, height, depth,
3389 _mesa_enum_to_string(internalFormat));
3390 return;
3391 }
3392
3393 /* Allow a hardware driver to just strip out the border, to provide
3394 * reliable but slightly incorrect hardware rendering instead of
3395 * rarely-tested software fallback rendering.
3396 */
3397 if (border && ctx->Const.StripTextureBorder) {
3398 strip_texture_border(target, &width, &height, &depth, unpack,
3399 &unpack_no_border);
3400 border = 0;
3401 unpack = &unpack_no_border;
3402 }
3403
3404 if (ctx->NewState & _NEW_PIXEL)
3405 _mesa_update_state(ctx);
3406
3407 _mesa_lock_texture(ctx, texObj);
3408 {
3409 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
3410
3411 if (!texImage) {
3412 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s%uD", func, dims);
3413 }
3414 else {
3415 ctx->Driver.FreeTextureImageBuffer(ctx, texImage);
3416
3417 _mesa_init_teximage_fields(ctx, texImage,
3418 width, height, depth,
3419 border, internalFormat, texFormat);
3420
3421 /* Give the texture to the driver. <pixels> may be null. */
3422 if (width > 0 && height > 0 && depth > 0) {
3423 if (compressed) {
3424 ctx->Driver.CompressedTexImage(ctx, dims, texImage,
3425 imageSize, pixels);
3426 }
3427 else {
3428 ctx->Driver.TexImage(ctx, dims, texImage, format,
3429 type, pixels, unpack);
3430 }
3431 }
3432
3433 check_gen_mipmap(ctx, target, texObj, level);
3434
3435 _mesa_update_fbo_texture(ctx, texObj, face, level);
3436
3437 _mesa_dirty_texobj(ctx, texObj);
3438 }
3439 }
3440 _mesa_unlock_texture(ctx, texObj);
3441 }
3442 }
3443
3444
3445
3446 /*
3447 * Called from the API. Note that width includes the border.
3448 */
3449 void GLAPIENTRY
3450 _mesa_TexImage1D( GLenum target, GLint level, GLint internalFormat,
3451 GLsizei width, GLint border, GLenum format,
3452 GLenum type, const GLvoid *pixels )
3453 {
3454 GET_CURRENT_CONTEXT(ctx);
3455 teximage(ctx, GL_FALSE, 1, target, level, internalFormat, width, 1, 1,
3456 border, format, type, 0, pixels);
3457 }
3458
3459
3460 void GLAPIENTRY
3461 _mesa_TexImage2D( GLenum target, GLint level, GLint internalFormat,
3462 GLsizei width, GLsizei height, GLint border,
3463 GLenum format, GLenum type,
3464 const GLvoid *pixels )
3465 {
3466 GET_CURRENT_CONTEXT(ctx);
3467 teximage(ctx, GL_FALSE, 2, target, level, internalFormat, width, height, 1,
3468 border, format, type, 0, pixels);
3469 }
3470
3471
3472 /*
3473 * Called by the API or display list executor.
3474 * Note that width and height include the border.
3475 */
3476 void GLAPIENTRY
3477 _mesa_TexImage3D( GLenum target, GLint level, GLint internalFormat,
3478 GLsizei width, GLsizei height, GLsizei depth,
3479 GLint border, GLenum format, GLenum type,
3480 const GLvoid *pixels )
3481 {
3482 GET_CURRENT_CONTEXT(ctx);
3483 teximage(ctx, GL_FALSE, 3, target, level, internalFormat,
3484 width, height, depth,
3485 border, format, type, 0, pixels);
3486 }
3487
3488
3489 void GLAPIENTRY
3490 _mesa_TexImage3DEXT( GLenum target, GLint level, GLenum internalFormat,
3491 GLsizei width, GLsizei height, GLsizei depth,
3492 GLint border, GLenum format, GLenum type,
3493 const GLvoid *pixels )
3494 {
3495 _mesa_TexImage3D(target, level, (GLint) internalFormat, width, height,
3496 depth, border, format, type, pixels);
3497 }
3498
3499
3500 void GLAPIENTRY
3501 _mesa_EGLImageTargetTexture2DOES (GLenum target, GLeglImageOES image)
3502 {
3503 struct gl_texture_object *texObj;
3504 struct gl_texture_image *texImage;
3505 bool valid_target;
3506 GET_CURRENT_CONTEXT(ctx);
3507 FLUSH_VERTICES(ctx, 0);
3508
3509 switch (target) {
3510 case GL_TEXTURE_2D:
3511 valid_target = ctx->Extensions.OES_EGL_image;
3512 break;
3513 case GL_TEXTURE_EXTERNAL_OES:
3514 valid_target =
3515 _mesa_is_gles(ctx) ? ctx->Extensions.OES_EGL_image_external : false;
3516 break;
3517 default:
3518 valid_target = false;
3519 break;
3520 }
3521
3522 if (!valid_target) {
3523 _mesa_error(ctx, GL_INVALID_ENUM,
3524 "glEGLImageTargetTexture2D(target=%d)", target);
3525 return;
3526 }
3527
3528 if (!image) {
3529 _mesa_error(ctx, GL_INVALID_OPERATION,
3530 "glEGLImageTargetTexture2D(image=%p)", image);
3531 return;
3532 }
3533
3534 if (ctx->NewState & _NEW_PIXEL)
3535 _mesa_update_state(ctx);
3536
3537 texObj = _mesa_get_current_tex_object(ctx, target);
3538 if (!texObj)
3539 return;
3540
3541 _mesa_lock_texture(ctx, texObj);
3542
3543 if (texObj->Immutable) {
3544 _mesa_error(ctx, GL_INVALID_OPERATION,
3545 "glEGLImageTargetTexture2D(texture is immutable)");
3546 _mesa_unlock_texture(ctx, texObj);
3547 return;
3548 }
3549
3550 texImage = _mesa_get_tex_image(ctx, texObj, target, 0);
3551 if (!texImage) {
3552 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glEGLImageTargetTexture2D");
3553 } else {
3554 ctx->Driver.FreeTextureImageBuffer(ctx, texImage);
3555
3556 ctx->Driver.EGLImageTargetTexture2D(ctx, target,
3557 texObj, texImage, image);
3558
3559 _mesa_dirty_texobj(ctx, texObj);
3560 }
3561 _mesa_unlock_texture(ctx, texObj);
3562 }
3563
3564
3565 /**
3566 * Helper that implements the glTexSubImage1/2/3D()
3567 * and glTextureSubImage1/2/3D() functions.
3568 */
3569 void
3570 _mesa_texture_sub_image(struct gl_context *ctx, GLuint dims,
3571 struct gl_texture_object *texObj,
3572 struct gl_texture_image *texImage,
3573 GLenum target, GLint level,
3574 GLint xoffset, GLint yoffset, GLint zoffset,
3575 GLsizei width, GLsizei height, GLsizei depth,
3576 GLenum format, GLenum type, const GLvoid *pixels,
3577 bool dsa)
3578 {
3579 FLUSH_VERTICES(ctx, 0);
3580
3581 if (ctx->NewState & _NEW_PIXEL)
3582 _mesa_update_state(ctx);
3583
3584 _mesa_lock_texture(ctx, texObj);
3585 {
3586 if (width > 0 && height > 0 && depth > 0) {
3587 /* If we have a border, offset=-1 is legal. Bias by border width. */
3588 switch (dims) {
3589 case 3:
3590 if (target != GL_TEXTURE_2D_ARRAY)
3591 zoffset += texImage->Border;
3592 /* fall-through */
3593 case 2:
3594 if (target != GL_TEXTURE_1D_ARRAY)
3595 yoffset += texImage->Border;
3596 /* fall-through */
3597 case 1:
3598 xoffset += texImage->Border;
3599 }
3600
3601 ctx->Driver.TexSubImage(ctx, dims, texImage,
3602 xoffset, yoffset, zoffset,
3603 width, height, depth,
3604 format, type, pixels, &ctx->Unpack);
3605
3606 check_gen_mipmap(ctx, target, texObj, level);
3607
3608 /* NOTE: Don't signal _NEW_TEXTURE since we've only changed
3609 * the texel data, not the texture format, size, etc.
3610 */
3611 }
3612 }
3613 _mesa_unlock_texture(ctx, texObj);
3614 }
3615
3616 /**
3617 * Implement all the glTexSubImage1/2/3D() functions.
3618 * Must split this out this way because of GL_TEXTURE_CUBE_MAP.
3619 */
3620 static void
3621 texsubimage(struct gl_context *ctx, GLuint dims, GLenum target, GLint level,
3622 GLint xoffset, GLint yoffset, GLint zoffset,
3623 GLsizei width, GLsizei height, GLsizei depth,
3624 GLenum format, GLenum type, const GLvoid *pixels,
3625 const char *callerName)
3626 {
3627 struct gl_texture_object *texObj;
3628 struct gl_texture_image *texImage;
3629
3630 /* check target (proxies not allowed) */
3631 if (!legal_texsubimage_target(ctx, dims, target, false)) {
3632 _mesa_error(ctx, GL_INVALID_ENUM, "glTexSubImage%uD(target=%s)",
3633 dims, _mesa_enum_to_string(target));
3634 return;
3635 }
3636
3637 texObj = _mesa_get_current_tex_object(ctx, target);
3638 if (!texObj)
3639 return;
3640
3641 if (texsubimage_error_check(ctx, dims, texObj, target, level,
3642 xoffset, yoffset, zoffset,
3643 width, height, depth, format, type,
3644 pixels, false, callerName)) {
3645 return; /* error was detected */
3646 }
3647
3648 texImage = _mesa_select_tex_image(texObj, target, level);
3649 /* texsubimage_error_check ensures that texImage is not NULL */
3650
3651 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
3652 _mesa_debug(ctx, "glTexSubImage%uD %s %d %d %d %d %d %d %d %s %s %p\n",
3653 dims,
3654 _mesa_enum_to_string(target), level,
3655 xoffset, yoffset, zoffset, width, height, depth,
3656 _mesa_enum_to_string(format),
3657 _mesa_enum_to_string(type), pixels);
3658
3659 _mesa_texture_sub_image(ctx, dims, texObj, texImage, target, level,
3660 xoffset, yoffset, zoffset, width, height, depth,
3661 format, type, pixels, false);
3662 }
3663
3664
3665 /**
3666 * Implement all the glTextureSubImage1/2/3D() functions.
3667 * Must split this out this way because of GL_TEXTURE_CUBE_MAP.
3668 */
3669 static void
3670 texturesubimage(struct gl_context *ctx, GLuint dims,
3671 GLuint texture, GLint level,
3672 GLint xoffset, GLint yoffset, GLint zoffset,
3673 GLsizei width, GLsizei height, GLsizei depth,
3674 GLenum format, GLenum type, const GLvoid *pixels,
3675 const char *callerName)
3676 {
3677 struct gl_texture_object *texObj;
3678 struct gl_texture_image *texImage;
3679 int i;
3680
3681 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
3682 _mesa_debug(ctx,
3683 "glTextureSubImage%uD %d %d %d %d %d %d %d %d %s %s %p\n",
3684 dims, texture, level,
3685 xoffset, yoffset, zoffset, width, height, depth,
3686 _mesa_enum_to_string(format),
3687 _mesa_enum_to_string(type), pixels);
3688
3689 /* Get the texture object by Name. */
3690 texObj = _mesa_lookup_texture(ctx, texture);
3691 if (!texObj) {
3692 _mesa_error(ctx, GL_INVALID_OPERATION, "glTextureSubImage%uD(texture)",
3693 dims);
3694 return;
3695 }
3696
3697 /* check target (proxies not allowed) */
3698 if (!legal_texsubimage_target(ctx, dims, texObj->Target, true)) {
3699 _mesa_error(ctx, GL_INVALID_ENUM, "%s(target=%s)",
3700 callerName, _mesa_enum_to_string(texObj->Target));
3701 return;
3702 }
3703
3704 if (texsubimage_error_check(ctx, dims, texObj, texObj->Target, level,
3705 xoffset, yoffset, zoffset,
3706 width, height, depth, format, type,
3707 pixels, true, callerName)) {
3708 return; /* error was detected */
3709 }
3710
3711
3712 /* Must handle special case GL_TEXTURE_CUBE_MAP. */
3713 if (texObj->Target == GL_TEXTURE_CUBE_MAP) {
3714 GLint rowStride;
3715
3716 /*
3717 * What do we do if the user created a texture with the following code
3718 * and then called this function with its handle?
3719 *
3720 * GLuint tex;
3721 * glCreateTextures(GL_TEXTURE_CUBE_MAP, 1, &tex);
3722 * glBindTexture(GL_TEXTURE_CUBE_MAP, tex);
3723 * glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, ...);
3724 * glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, ...);
3725 * glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, ...);
3726 * // Note: GL_TEXTURE_CUBE_MAP_NEGATIVE_Y not set, or given the
3727 * // wrong format, or given the wrong size, etc.
3728 * glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, ...);
3729 * glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, ...);
3730 *
3731 * A bug has been filed against the spec for this case. In the
3732 * meantime, we will check for cube completeness.
3733 *
3734 * According to Section 8.17 Texture Completeness in the OpenGL 4.5
3735 * Core Profile spec (30.10.2014):
3736 * "[A] cube map texture is cube complete if the
3737 * following conditions all hold true: The [base level] texture
3738 * images of each of the six cube map faces have identical, positive,
3739 * and square dimensions. The [base level] images were each specified
3740 * with the same internal format."
3741 *
3742 * It seems reasonable to check for cube completeness of an arbitrary
3743 * level here so that the image data has a consistent format and size.
3744 */
3745 if (!_mesa_cube_level_complete(texObj, level)) {
3746 _mesa_error(ctx, GL_INVALID_OPERATION,
3747 "glTextureSubImage%uD(cube map incomplete)",
3748 dims);
3749 return;
3750 }
3751
3752 rowStride = _mesa_image_image_stride(&ctx->Unpack, width, height,
3753 format, type);
3754 /* Copy in each face. */
3755 for (i = 0; i < 6; ++i) {
3756 texImage = texObj->Image[i][level];
3757 assert(texImage);
3758
3759 _mesa_texture_sub_image(ctx, 3, texObj, texImage, texObj->Target,
3760 level, xoffset, yoffset, zoffset,
3761 width, height, 1, format,
3762 type, pixels, true);
3763 pixels = (GLubyte *) pixels + rowStride;
3764 }
3765 }
3766 else {
3767 texImage = _mesa_select_tex_image(texObj, texObj->Target, level);
3768 assert(texImage);
3769
3770 _mesa_texture_sub_image(ctx, dims, texObj, texImage, texObj->Target,
3771 level, xoffset, yoffset, zoffset,
3772 width, height, depth, format,
3773 type, pixels, true);
3774 }
3775 }
3776
3777
3778 void GLAPIENTRY
3779 _mesa_TexSubImage1D( GLenum target, GLint level,
3780 GLint xoffset, GLsizei width,
3781 GLenum format, GLenum type,
3782 const GLvoid *pixels )
3783 {
3784 GET_CURRENT_CONTEXT(ctx);
3785 texsubimage(ctx, 1, target, level,
3786 xoffset, 0, 0,
3787 width, 1, 1,
3788 format, type, pixels, "glTexSubImage1D");
3789 }
3790
3791
3792 void GLAPIENTRY
3793 _mesa_TexSubImage2D( GLenum target, GLint level,
3794 GLint xoffset, GLint yoffset,
3795 GLsizei width, GLsizei height,
3796 GLenum format, GLenum type,
3797 const GLvoid *pixels )
3798 {
3799 GET_CURRENT_CONTEXT(ctx);
3800 texsubimage(ctx, 2, target, level,
3801 xoffset, yoffset, 0,
3802 width, height, 1,
3803 format, type, pixels, "glTexSubImage2D");
3804 }
3805
3806
3807
3808 void GLAPIENTRY
3809 _mesa_TexSubImage3D( GLenum target, GLint level,
3810 GLint xoffset, GLint yoffset, GLint zoffset,
3811 GLsizei width, GLsizei height, GLsizei depth,
3812 GLenum format, GLenum type,
3813 const GLvoid *pixels )
3814 {
3815 GET_CURRENT_CONTEXT(ctx);
3816 texsubimage(ctx, 3, target, level,
3817 xoffset, yoffset, zoffset,
3818 width, height, depth,
3819 format, type, pixels, "glTexSubImage3D");
3820 }
3821
3822 void GLAPIENTRY
3823 _mesa_TextureSubImage1D(GLuint texture, GLint level,
3824 GLint xoffset, GLsizei width,
3825 GLenum format, GLenum type,
3826 const GLvoid *pixels)
3827 {
3828 GET_CURRENT_CONTEXT(ctx);
3829 texturesubimage(ctx, 1, texture, level,
3830 xoffset, 0, 0,
3831 width, 1, 1,
3832 format, type, pixels, "glTextureSubImage1D");
3833 }
3834
3835
3836 void GLAPIENTRY
3837 _mesa_TextureSubImage2D(GLuint texture, GLint level,
3838 GLint xoffset, GLint yoffset,
3839 GLsizei width, GLsizei height,
3840 GLenum format, GLenum type,
3841 const GLvoid *pixels)
3842 {
3843 GET_CURRENT_CONTEXT(ctx);
3844 texturesubimage(ctx, 2, texture, level,
3845 xoffset, yoffset, 0,
3846 width, height, 1,
3847 format, type, pixels, "glTextureSubImage2D");
3848 }
3849
3850
3851 void GLAPIENTRY
3852 _mesa_TextureSubImage3D(GLuint texture, GLint level,
3853 GLint xoffset, GLint yoffset, GLint zoffset,
3854 GLsizei width, GLsizei height, GLsizei depth,
3855 GLenum format, GLenum type,
3856 const GLvoid *pixels)
3857 {
3858 GET_CURRENT_CONTEXT(ctx);
3859 texturesubimage(ctx, 3, texture, level,
3860 xoffset, yoffset, zoffset,
3861 width, height, depth,
3862 format, type, pixels, "glTextureSubImage3D");
3863 }
3864
3865
3866 /**
3867 * For glCopyTexSubImage, return the source renderbuffer to copy texel data
3868 * from. This depends on whether the texture contains color or depth values.
3869 */
3870 static struct gl_renderbuffer *
3871 get_copy_tex_image_source(struct gl_context *ctx, mesa_format texFormat)
3872 {
3873 if (_mesa_get_format_bits(texFormat, GL_DEPTH_BITS) > 0) {
3874 /* reading from depth/stencil buffer */
3875 return ctx->ReadBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;
3876 }
3877 else {
3878 /* copying from color buffer */
3879 return ctx->ReadBuffer->_ColorReadBuffer;
3880 }
3881 }
3882
3883 static void
3884 copytexsubimage_by_slice(struct gl_context *ctx,
3885 struct gl_texture_image *texImage,
3886 GLuint dims,
3887 GLint xoffset, GLint yoffset, GLint zoffset,
3888 struct gl_renderbuffer *rb,
3889 GLint x, GLint y,
3890 GLsizei width, GLsizei height)
3891 {
3892 if (texImage->TexObject->Target == GL_TEXTURE_1D_ARRAY) {
3893 int slice;
3894
3895 /* For 1D arrays, we copy each scanline of the source rectangle into the
3896 * next array slice.
3897 */
3898 assert(zoffset == 0);
3899
3900 for (slice = 0; slice < height; slice++) {
3901 assert(yoffset + slice < texImage->Height);
3902 ctx->Driver.CopyTexSubImage(ctx, 2, texImage,
3903 xoffset, 0, yoffset + slice,
3904 rb, x, y + slice, width, 1);
3905 }
3906 } else {
3907 ctx->Driver.CopyTexSubImage(ctx, dims, texImage,
3908 xoffset, yoffset, zoffset,
3909 rb, x, y, width, height);
3910 }
3911 }
3912
3913 static GLboolean
3914 formats_differ_in_component_sizes(mesa_format f1, mesa_format f2)
3915 {
3916 GLint f1_r_bits = _mesa_get_format_bits(f1, GL_RED_BITS);
3917 GLint f1_g_bits = _mesa_get_format_bits(f1, GL_GREEN_BITS);
3918 GLint f1_b_bits = _mesa_get_format_bits(f1, GL_BLUE_BITS);
3919 GLint f1_a_bits = _mesa_get_format_bits(f1, GL_ALPHA_BITS);
3920
3921 GLint f2_r_bits = _mesa_get_format_bits(f2, GL_RED_BITS);
3922 GLint f2_g_bits = _mesa_get_format_bits(f2, GL_GREEN_BITS);
3923 GLint f2_b_bits = _mesa_get_format_bits(f2, GL_BLUE_BITS);
3924 GLint f2_a_bits = _mesa_get_format_bits(f2, GL_ALPHA_BITS);
3925
3926 if ((f1_r_bits && f2_r_bits && f1_r_bits != f2_r_bits)
3927 || (f1_g_bits && f2_g_bits && f1_g_bits != f2_g_bits)
3928 || (f1_b_bits && f2_b_bits && f1_b_bits != f2_b_bits)
3929 || (f1_a_bits && f2_a_bits && f1_a_bits != f2_a_bits))
3930 return GL_TRUE;
3931
3932 return GL_FALSE;
3933 }
3934
3935 /**
3936 * Implement the glCopyTexImage1/2D() functions.
3937 */
3938 static void
3939 copyteximage(struct gl_context *ctx, GLuint dims,
3940 GLenum target, GLint level, GLenum internalFormat,
3941 GLint x, GLint y, GLsizei width, GLsizei height, GLint border )
3942 {
3943 struct gl_texture_object *texObj;
3944 struct gl_texture_image *texImage;
3945 const GLuint face = _mesa_tex_target_to_face(target);
3946 mesa_format texFormat;
3947 struct gl_renderbuffer *rb;
3948
3949 FLUSH_VERTICES(ctx, 0);
3950
3951 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
3952 _mesa_debug(ctx, "glCopyTexImage%uD %s %d %s %d %d %d %d %d\n",
3953 dims,
3954 _mesa_enum_to_string(target), level,
3955 _mesa_enum_to_string(internalFormat),
3956 x, y, width, height, border);
3957
3958 if (ctx->NewState & NEW_COPY_TEX_STATE)
3959 _mesa_update_state(ctx);
3960
3961 if (copytexture_error_check(ctx, dims, target, level, internalFormat,
3962 width, height, border))
3963 return;
3964
3965 if (!_mesa_legal_texture_dimensions(ctx, target, level, width, height,
3966 1, border)) {
3967 _mesa_error(ctx, GL_INVALID_VALUE,
3968 "glCopyTexImage%uD(invalid width or height)", dims);
3969 return;
3970 }
3971
3972 texObj = _mesa_get_current_tex_object(ctx, target);
3973 assert(texObj);
3974
3975 texFormat = _mesa_choose_texture_format(ctx, texObj, target, level,
3976 internalFormat, GL_NONE, GL_NONE);
3977
3978 rb = _mesa_get_read_renderbuffer_for_format(ctx, internalFormat);
3979
3980 if (_mesa_is_gles3(ctx)) {
3981 if (_mesa_is_enum_format_unsized(internalFormat)) {
3982 /* Conversion from GL_RGB10_A2 source buffer format is not allowed in
3983 * OpenGL ES 3.0. Khronos bug# 9807.
3984 */
3985 if (rb->InternalFormat == GL_RGB10_A2) {
3986 _mesa_error(ctx, GL_INVALID_OPERATION,
3987 "glCopyTexImage%uD(Reading from GL_RGB10_A2 buffer"
3988 " and writing to unsized internal format)", dims);
3989 return;
3990 }
3991 }
3992 /* From Page 139 of OpenGL ES 3.0 spec:
3993 * "If internalformat is sized, the internal format of the new texel
3994 * array is internalformat, and this is also the new texel array’s
3995 * effective internal format. If the component sizes of internalformat
3996 * do not exactly match the corresponding component sizes of the source
3997 * buffer’s effective internal format, described below, an
3998 * INVALID_OPERATION error is generated. If internalformat is unsized,
3999 * the internal format of the new texel array is the effective internal
4000 * format of the source buffer, and this is also the new texel array’s
4001 * effective internal format.
4002 */
4003 else if (formats_differ_in_component_sizes (texFormat, rb->Format)) {
4004 _mesa_error(ctx, GL_INVALID_OPERATION,
4005 "glCopyTexImage%uD(componenet size changed in"
4006 " internal format)", dims);
4007 return;
4008 }
4009 }
4010
4011 assert(texFormat != MESA_FORMAT_NONE);
4012
4013 if (!ctx->Driver.TestProxyTexImage(ctx, proxy_target(target),
4014 level, texFormat,
4015 width, height, 1, border)) {
4016 _mesa_error(ctx, GL_OUT_OF_MEMORY,
4017 "glCopyTexImage%uD(image too large)", dims);
4018 return;
4019 }
4020
4021 if (border && ctx->Const.StripTextureBorder) {
4022 x += border;
4023 width -= border * 2;
4024 if (dims == 2) {
4025 y += border;
4026 height -= border * 2;
4027 }
4028 border = 0;
4029 }
4030
4031 _mesa_lock_texture(ctx, texObj);
4032 {
4033 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
4034
4035 if (!texImage) {
4036 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexImage%uD", dims);
4037 }
4038 else {
4039 GLint srcX = x, srcY = y, dstX = 0, dstY = 0, dstZ = 0;
4040
4041 /* Free old texture image */
4042 ctx->Driver.FreeTextureImageBuffer(ctx, texImage);
4043
4044 _mesa_init_teximage_fields(ctx, texImage, width, height, 1,
4045 border, internalFormat, texFormat);
4046
4047 if (width && height) {
4048 /* Allocate texture memory (no pixel data yet) */
4049 ctx->Driver.AllocTextureImageBuffer(ctx, texImage);
4050
4051 if (_mesa_clip_copytexsubimage(ctx, &dstX, &dstY, &srcX, &srcY,
4052 &width, &height)) {
4053 struct gl_renderbuffer *srcRb =
4054 get_copy_tex_image_source(ctx, texImage->TexFormat);
4055
4056 copytexsubimage_by_slice(ctx, texImage, dims,
4057 dstX, dstY, dstZ,
4058 srcRb, srcX, srcY, width, height);
4059 }
4060
4061 check_gen_mipmap(ctx, target, texObj, level);
4062 }
4063
4064 _mesa_update_fbo_texture(ctx, texObj, face, level);
4065
4066 _mesa_dirty_texobj(ctx, texObj);
4067 }
4068 }
4069 _mesa_unlock_texture(ctx, texObj);
4070 }
4071
4072
4073
4074 void GLAPIENTRY
4075 _mesa_CopyTexImage1D( GLenum target, GLint level,
4076 GLenum internalFormat,
4077 GLint x, GLint y,
4078 GLsizei width, GLint border )
4079 {
4080 GET_CURRENT_CONTEXT(ctx);
4081 copyteximage(ctx, 1, target, level, internalFormat, x, y, width, 1, border);
4082 }
4083
4084
4085
4086 void GLAPIENTRY
4087 _mesa_CopyTexImage2D( GLenum target, GLint level, GLenum internalFormat,
4088 GLint x, GLint y, GLsizei width, GLsizei height,
4089 GLint border )
4090 {
4091 GET_CURRENT_CONTEXT(ctx);
4092 copyteximage(ctx, 2, target, level, internalFormat,
4093 x, y, width, height, border);
4094 }
4095
4096 /**
4097 * Implementation for glCopyTex(ture)SubImage1/2/3D() functions.
4098 */
4099 void
4100 _mesa_copy_texture_sub_image(struct gl_context *ctx, GLuint dims,
4101 struct gl_texture_object *texObj,
4102 GLenum target, GLint level,
4103 GLint xoffset, GLint yoffset, GLint zoffset,
4104 GLint x, GLint y,
4105 GLsizei width, GLsizei height,
4106 const char *caller)
4107 {
4108 struct gl_texture_image *texImage;
4109
4110 FLUSH_VERTICES(ctx, 0);
4111
4112 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
4113 _mesa_debug(ctx, "%s %s %d %d %d %d %d %d %d %d\n", caller,
4114 _mesa_enum_to_string(target),
4115 level, xoffset, yoffset, zoffset, x, y, width, height);
4116
4117 if (ctx->NewState & NEW_COPY_TEX_STATE)
4118 _mesa_update_state(ctx);
4119
4120 if (copytexsubimage_error_check(ctx, dims, texObj, target, level,
4121 xoffset, yoffset, zoffset,
4122 width, height, caller)) {
4123 return;
4124 }
4125
4126 _mesa_lock_texture(ctx, texObj);
4127 {
4128 texImage = _mesa_select_tex_image(texObj, target, level);
4129
4130 /* If we have a border, offset=-1 is legal. Bias by border width. */
4131 switch (dims) {
4132 case 3:
4133 if (target != GL_TEXTURE_2D_ARRAY)
4134 zoffset += texImage->Border;
4135 /* fall-through */
4136 case 2:
4137 if (target != GL_TEXTURE_1D_ARRAY)
4138 yoffset += texImage->Border;
4139 /* fall-through */
4140 case 1:
4141 xoffset += texImage->Border;
4142 }
4143
4144 if (_mesa_clip_copytexsubimage(ctx, &xoffset, &yoffset, &x, &y,
4145 &width, &height)) {
4146 struct gl_renderbuffer *srcRb =
4147 get_copy_tex_image_source(ctx, texImage->TexFormat);
4148
4149 copytexsubimage_by_slice(ctx, texImage, dims,
4150 xoffset, yoffset, zoffset,
4151 srcRb, x, y, width, height);
4152
4153 check_gen_mipmap(ctx, target, texObj, level);
4154
4155 /* NOTE: Don't signal _NEW_TEXTURE since we've only changed
4156 * the texel data, not the texture format, size, etc.
4157 */
4158 }
4159 }
4160 _mesa_unlock_texture(ctx, texObj);
4161 }
4162
4163 void GLAPIENTRY
4164 _mesa_CopyTexSubImage1D( GLenum target, GLint level,
4165 GLint xoffset, GLint x, GLint y, GLsizei width )
4166 {
4167 struct gl_texture_object* texObj;
4168 const char *self = "glCopyTexSubImage1D";
4169 GET_CURRENT_CONTEXT(ctx);
4170
4171 /* Check target (proxies not allowed). Target must be checked prior to
4172 * calling _mesa_get_current_tex_object.
4173 */
4174 if (!legal_texsubimage_target(ctx, 1, target, false)) {
4175 _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid target %s)", self,
4176 _mesa_enum_to_string(target));
4177 return;
4178 }
4179
4180 texObj = _mesa_get_current_tex_object(ctx, target);
4181 if (!texObj)
4182 return;
4183
4184 _mesa_copy_texture_sub_image(ctx, 1, texObj, target, level, xoffset, 0, 0,
4185 x, y, width, 1, self);
4186 }
4187
4188
4189
4190 void GLAPIENTRY
4191 _mesa_CopyTexSubImage2D( GLenum target, GLint level,
4192 GLint xoffset, GLint yoffset,
4193 GLint x, GLint y, GLsizei width, GLsizei height )
4194 {
4195 struct gl_texture_object* texObj;
4196 const char *self = "glCopyTexSubImage2D";
4197 GET_CURRENT_CONTEXT(ctx);
4198
4199 /* Check target (proxies not allowed). Target must be checked prior to
4200 * calling _mesa_get_current_tex_object.
4201 */
4202 if (!legal_texsubimage_target(ctx, 2, target, false)) {
4203 _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid target %s)", self,
4204 _mesa_enum_to_string(target));
4205 return;
4206 }
4207
4208 texObj = _mesa_get_current_tex_object(ctx, target);
4209 if (!texObj)
4210 return;
4211
4212 _mesa_copy_texture_sub_image(ctx, 2, texObj, target, level,
4213 xoffset, yoffset, 0,
4214 x, y, width, height, self);
4215 }
4216
4217
4218
4219 void GLAPIENTRY
4220 _mesa_CopyTexSubImage3D( GLenum target, GLint level,
4221 GLint xoffset, GLint yoffset, GLint zoffset,
4222 GLint x, GLint y, GLsizei width, GLsizei height )
4223 {
4224 struct gl_texture_object* texObj;
4225 const char *self = "glCopyTexSubImage3D";
4226 GET_CURRENT_CONTEXT(ctx);
4227
4228 /* Check target (proxies not allowed). Target must be checked prior to
4229 * calling _mesa_get_current_tex_object.
4230 */
4231 if (!legal_texsubimage_target(ctx, 3, target, false)) {
4232 _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid target %s)", self,
4233 _mesa_enum_to_string(target));
4234 return;
4235 }
4236
4237 texObj = _mesa_get_current_tex_object(ctx, target);
4238 if (!texObj)
4239 return;
4240
4241 _mesa_copy_texture_sub_image(ctx, 3, texObj, target, level,
4242 xoffset, yoffset, zoffset,
4243 x, y, width, height, self);
4244 }
4245
4246 void GLAPIENTRY
4247 _mesa_CopyTextureSubImage1D(GLuint texture, GLint level,
4248 GLint xoffset, GLint x, GLint y, GLsizei width)
4249 {
4250 struct gl_texture_object* texObj;
4251 const char *self = "glCopyTextureSubImage1D";
4252 GET_CURRENT_CONTEXT(ctx);
4253
4254 texObj = _mesa_lookup_texture_err(ctx, texture, self);
4255 if (!texObj)
4256 return;
4257
4258 /* Check target (proxies not allowed). */
4259 if (!legal_texsubimage_target(ctx, 1, texObj->Target, true)) {
4260 _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid target %s)", self,
4261 _mesa_enum_to_string(texObj->Target));
4262 return;
4263 }
4264
4265 _mesa_copy_texture_sub_image(ctx, 1, texObj, texObj->Target, level,
4266 xoffset, 0, 0, x, y, width, 1, self);
4267 }
4268
4269 void GLAPIENTRY
4270 _mesa_CopyTextureSubImage2D(GLuint texture, GLint level,
4271 GLint xoffset, GLint yoffset,
4272 GLint x, GLint y, GLsizei width, GLsizei height)
4273 {
4274 struct gl_texture_object* texObj;
4275 const char *self = "glCopyTextureSubImage2D";
4276 GET_CURRENT_CONTEXT(ctx);
4277
4278 texObj = _mesa_lookup_texture_err(ctx, texture, self);
4279 if (!texObj)
4280 return;
4281
4282 /* Check target (proxies not allowed). */
4283 if (!legal_texsubimage_target(ctx, 2, texObj->Target, true)) {
4284 _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid target %s)", self,
4285 _mesa_enum_to_string(texObj->Target));
4286 return;
4287 }
4288
4289 _mesa_copy_texture_sub_image(ctx, 2, texObj, texObj->Target, level,
4290 xoffset, yoffset, 0,
4291 x, y, width, height, self);
4292 }
4293
4294
4295
4296 void GLAPIENTRY
4297 _mesa_CopyTextureSubImage3D(GLuint texture, GLint level,
4298 GLint xoffset, GLint yoffset, GLint zoffset,
4299 GLint x, GLint y, GLsizei width, GLsizei height)
4300 {
4301 struct gl_texture_object* texObj;
4302 const char *self = "glCopyTextureSubImage3D";
4303 GET_CURRENT_CONTEXT(ctx);
4304
4305 texObj = _mesa_lookup_texture_err(ctx, texture, self);
4306 if (!texObj)
4307 return;
4308
4309 /* Check target (proxies not allowed). */
4310 if (!legal_texsubimage_target(ctx, 3, texObj->Target, true)) {
4311 _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid target %s)", self,
4312 _mesa_enum_to_string(texObj->Target));
4313 return;
4314 }
4315
4316 if (texObj->Target == GL_TEXTURE_CUBE_MAP) {
4317 /* Act like CopyTexSubImage2D */
4318 _mesa_copy_texture_sub_image(ctx, 2, texObj,
4319 GL_TEXTURE_CUBE_MAP_POSITIVE_X + zoffset,
4320 level, xoffset, yoffset, 0,
4321 x, y, width, height, self);
4322 }
4323 else
4324 _mesa_copy_texture_sub_image(ctx, 3, texObj, texObj->Target, level,
4325 xoffset, yoffset, zoffset,
4326 x, y, width, height, self);
4327 }
4328
4329 static bool
4330 check_clear_tex_image(struct gl_context *ctx,
4331 const char *function,
4332 struct gl_texture_image *texImage,
4333 GLenum format, GLenum type,
4334 const void *data,
4335 GLubyte *clearValue)
4336 {
4337 struct gl_texture_object *texObj = texImage->TexObject;
4338 static const GLubyte zeroData[MAX_PIXEL_BYTES];
4339 GLenum internalFormat = texImage->InternalFormat;
4340 GLenum err;
4341
4342 if (texObj->Target == GL_TEXTURE_BUFFER) {
4343 _mesa_error(ctx, GL_INVALID_OPERATION,
4344 "%s(buffer texture)", function);
4345 return false;
4346 }
4347
4348 if (_mesa_is_compressed_format(ctx, internalFormat)) {
4349 _mesa_error(ctx, GL_INVALID_OPERATION,
4350 "%s(compressed texture)", function);
4351 return false;
4352 }
4353
4354 err = _mesa_error_check_format_and_type(ctx, format, type);
4355 if (err != GL_NO_ERROR) {
4356 _mesa_error(ctx, err,
4357 "%s(incompatible format = %s, type = %s)",
4358 function,
4359 _mesa_enum_to_string(format),
4360 _mesa_enum_to_string(type));
4361 return false;
4362 }
4363
4364 /* make sure internal format and format basically agree */
4365 if (!texture_formats_agree(internalFormat, format)) {
4366 _mesa_error(ctx, GL_INVALID_OPERATION,
4367 "%s(incompatible internalFormat = %s, format = %s)",
4368 function,
4369 _mesa_enum_to_string(internalFormat),
4370 _mesa_enum_to_string(format));
4371 return false;
4372 }
4373
4374 if (ctx->Version >= 30 || ctx->Extensions.EXT_texture_integer) {
4375 /* both source and dest must be integer-valued, or neither */
4376 if (_mesa_is_format_integer_color(texImage->TexFormat) !=
4377 _mesa_is_enum_format_integer(format)) {
4378 _mesa_error(ctx, GL_INVALID_OPERATION,
4379 "%s(integer/non-integer format mismatch)",
4380 function);
4381 return false;
4382 }
4383 }
4384
4385 if (!_mesa_texstore(ctx,
4386 1, /* dims */
4387 texImage->_BaseFormat,
4388 texImage->TexFormat,
4389 0, /* dstRowStride */
4390 &clearValue,
4391 1, 1, 1, /* srcWidth/Height/Depth */
4392 format, type,
4393 data ? data : zeroData,
4394 &ctx->DefaultPacking)) {
4395 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid format)", function);
4396 return false;
4397 }
4398
4399 return true;
4400 }
4401
4402 static struct gl_texture_object *
4403 get_tex_obj_for_clear(struct gl_context *ctx,
4404 const char *function,
4405 GLuint texture)
4406 {
4407 struct gl_texture_object *texObj;
4408
4409 if (texture == 0) {
4410 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(zero texture)", function);
4411 return NULL;
4412 }
4413
4414 texObj = _mesa_lookup_texture(ctx, texture);
4415
4416 if (texObj == NULL) {
4417 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(non-gen name)", function);
4418 return NULL;
4419 }
4420
4421 if (texObj->Target == 0) {
4422 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(unbound tex)", function);
4423 return NULL;
4424 }
4425
4426 return texObj;
4427 }
4428
4429 static int
4430 get_tex_images_for_clear(struct gl_context *ctx,
4431 const char *function,
4432 struct gl_texture_object *texObj,
4433 GLint level,
4434 struct gl_texture_image **texImages)
4435 {
4436 GLenum target;
4437 int i;
4438
4439 if (level < 0 || level >= MAX_TEXTURE_LEVELS) {
4440 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid level)", function);
4441 return 0;
4442 }
4443
4444 if (texObj->Target == GL_TEXTURE_CUBE_MAP) {
4445 for (i = 0; i < MAX_FACES; i++) {
4446 target = GL_TEXTURE_CUBE_MAP_POSITIVE_X + i;
4447
4448 texImages[i] = _mesa_select_tex_image(texObj, target, level);
4449 if (texImages[i] == NULL) {
4450 _mesa_error(ctx, GL_INVALID_OPERATION,
4451 "%s(invalid level)", function);
4452 return 0;
4453 }
4454 }
4455
4456 return MAX_FACES;
4457 }
4458
4459 texImages[0] = _mesa_select_tex_image(texObj, texObj->Target, level);
4460
4461 if (texImages[0] == NULL) {
4462 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid level)", function);
4463 return 0;
4464 }
4465
4466 return 1;
4467 }
4468
4469 void GLAPIENTRY
4470 _mesa_ClearTexSubImage( GLuint texture, GLint level,
4471 GLint xoffset, GLint yoffset, GLint zoffset,
4472 GLsizei width, GLsizei height, GLsizei depth,
4473 GLenum format, GLenum type, const void *data )
4474 {
4475 GET_CURRENT_CONTEXT(ctx);
4476 struct gl_texture_object *texObj;
4477 struct gl_texture_image *texImages[MAX_FACES];
4478 GLubyte clearValue[MAX_FACES][MAX_PIXEL_BYTES];
4479 int i, numImages;
4480 int minDepth, maxDepth;
4481
4482 texObj = get_tex_obj_for_clear(ctx, "glClearTexSubImage", texture);
4483
4484 if (texObj == NULL)
4485 return;
4486
4487 _mesa_lock_texture(ctx, texObj);
4488
4489 numImages = get_tex_images_for_clear(ctx, "glClearTexSubImage",
4490 texObj, level, texImages);
4491 if (numImages == 0)
4492 goto out;
4493
4494 if (numImages == 1) {
4495 minDepth = -(int) texImages[0]->Border;
4496 maxDepth = texImages[0]->Depth;
4497 } else {
4498 minDepth = 0;
4499 maxDepth = numImages;
4500 }
4501
4502 if (xoffset < -(GLint) texImages[0]->Border ||
4503 yoffset < -(GLint) texImages[0]->Border ||
4504 zoffset < minDepth ||
4505 width < 0 ||
4506 height < 0 ||
4507 depth < 0 ||
4508 xoffset + width > texImages[0]->Width ||
4509 yoffset + height > texImages[0]->Height ||
4510 zoffset + depth > maxDepth) {
4511 _mesa_error(ctx, GL_INVALID_OPERATION,
4512 "glClearSubTexImage(invalid dimensions)");
4513 goto out;
4514 }
4515
4516 if (numImages == 1) {
4517 if (check_clear_tex_image(ctx, "glClearTexSubImage",
4518 texImages[0],
4519 format, type, data, clearValue[0])) {
4520 ctx->Driver.ClearTexSubImage(ctx,
4521 texImages[0],
4522 xoffset, yoffset, zoffset,
4523 width, height, depth,
4524 data ? clearValue[0] : NULL);
4525 }
4526 } else {
4527 for (i = zoffset; i < zoffset + depth; i++) {
4528 if (!check_clear_tex_image(ctx, "glClearTexSubImage",
4529 texImages[i],
4530 format, type, data, clearValue[i]))
4531 goto out;
4532 }
4533 for (i = zoffset; i < zoffset + depth; i++) {
4534 ctx->Driver.ClearTexSubImage(ctx,
4535 texImages[i],
4536 xoffset, yoffset, 0,
4537 width, height, 1,
4538 data ? clearValue[i] : NULL);
4539 }
4540 }
4541
4542 out:
4543 _mesa_unlock_texture(ctx, texObj);
4544 }
4545
4546 void GLAPIENTRY
4547 _mesa_ClearTexImage( GLuint texture, GLint level,
4548 GLenum format, GLenum type, const void *data )
4549 {
4550 GET_CURRENT_CONTEXT(ctx);
4551 struct gl_texture_object *texObj;
4552 struct gl_texture_image *texImages[MAX_FACES];
4553 GLubyte clearValue[MAX_FACES][MAX_PIXEL_BYTES];
4554 int i, numImages;
4555
4556 texObj = get_tex_obj_for_clear(ctx, "glClearTexImage", texture);
4557
4558 if (texObj == NULL)
4559 return;
4560
4561 _mesa_lock_texture(ctx, texObj);
4562
4563 numImages = get_tex_images_for_clear(ctx, "glClearTexImage",
4564 texObj, level, texImages);
4565
4566 for (i = 0; i < numImages; i++) {
4567 if (!check_clear_tex_image(ctx, "glClearTexImage",
4568 texImages[i],
4569 format, type, data,
4570 clearValue[i]))
4571 goto out;
4572 }
4573
4574 for (i = 0; i < numImages; i++) {
4575 ctx->Driver.ClearTexSubImage(ctx, texImages[i],
4576 -(GLint) texImages[i]->Border, /* xoffset */
4577 -(GLint) texImages[i]->Border, /* yoffset */
4578 -(GLint) texImages[i]->Border, /* zoffset */
4579 texImages[i]->Width,
4580 texImages[i]->Height,
4581 texImages[i]->Depth,
4582 data ? clearValue[i] : NULL);
4583 }
4584
4585 out:
4586 _mesa_unlock_texture(ctx, texObj);
4587 }
4588
4589
4590
4591
4592 /**********************************************************************/
4593 /****** Compressed Textures ******/
4594 /**********************************************************************/
4595
4596
4597 /**
4598 * Target checking for glCompressedTexSubImage[123]D().
4599 * \return GL_TRUE if error, GL_FALSE if no error
4600 * Must come before other error checking so that the texture object can
4601 * be correctly retrieved using _mesa_get_current_tex_object.
4602 */
4603 static GLboolean
4604 compressed_subtexture_target_check(struct gl_context *ctx, GLenum target,
4605 GLint dims, GLenum format, bool dsa,
4606 const char *caller)
4607 {
4608 GLboolean targetOK;
4609
4610 if (dsa && target == GL_TEXTURE_RECTANGLE) {
4611 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid target %s)", caller,
4612 _mesa_enum_to_string(target));
4613 return GL_TRUE;
4614 }
4615
4616 switch (dims) {
4617 case 2:
4618 switch (target) {
4619 case GL_TEXTURE_2D:
4620 targetOK = GL_TRUE;
4621 break;
4622 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
4623 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
4624 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
4625 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
4626 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
4627 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
4628 targetOK = ctx->Extensions.ARB_texture_cube_map;
4629 break;
4630 default:
4631 targetOK = GL_FALSE;
4632 break;
4633 }
4634 break;
4635 case 3:
4636 switch (target) {
4637 case GL_TEXTURE_CUBE_MAP:
4638 targetOK = dsa && ctx->Extensions.ARB_texture_cube_map;
4639 break;
4640 case GL_TEXTURE_2D_ARRAY:
4641 targetOK = _mesa_is_gles3(ctx) ||
4642 (_mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_array);
4643 break;
4644 case GL_TEXTURE_CUBE_MAP_ARRAY:
4645 targetOK = ctx->Extensions.ARB_texture_cube_map_array;
4646 break;
4647 case GL_TEXTURE_3D:
4648 targetOK = GL_TRUE;
4649 /*
4650 * OpenGL 4.5 spec (30.10.2014) says in Section 8.7 Compressed Texture
4651 * Images:
4652 * "An INVALID_OPERATION error is generated by
4653 * CompressedTex*SubImage3D if the internal format of the texture
4654 * is one of the EAC, ETC2, or RGTC formats and either border is
4655 * non-zero, or the effective target for the texture is not
4656 * TEXTURE_2D_ARRAY."
4657 *
4658 * NOTE: that's probably a spec error. It should probably say
4659 * "... or the effective target for the texture is not
4660 * TEXTURE_2D_ARRAY, TEXTURE_CUBE_MAP, nor
4661 * GL_TEXTURE_CUBE_MAP_ARRAY."
4662 * since those targets are 2D images and they support all compression
4663 * formats.
4664 *
4665 * Instead of listing all these, just list those which are allowed,
4666 * which is (at this time) only bptc. Otherwise we'd say s3tc (and
4667 * more) are valid here, which they are not, but of course not
4668 * mentioned by core spec.
4669 */
4670 switch (format) {
4671 /* These are the only 3D compression formats supported at this time */
4672 case GL_COMPRESSED_RGBA_BPTC_UNORM:
4673 case GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM:
4674 case GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT:
4675 case GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT:
4676 /* valid format */
4677 break;
4678 default:
4679 /* invalid format */
4680 _mesa_error(ctx, GL_INVALID_OPERATION,
4681 "%s(invalid target %s for format %s)", caller,
4682 _mesa_enum_to_string(target),
4683 _mesa_enum_to_string(format));
4684 return GL_TRUE;
4685 }
4686 break;
4687 default:
4688 targetOK = GL_FALSE;
4689 }
4690
4691 break;
4692 default:
4693 assert(dims == 1);
4694 /* no 1D compressed textures at this time */
4695 targetOK = GL_FALSE;
4696 break;
4697 }
4698
4699 if (!targetOK) {
4700 _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid target %s)", caller,
4701 _mesa_enum_to_string(target));
4702 return GL_TRUE;
4703 }
4704
4705 return GL_FALSE;
4706 }
4707
4708 /**
4709 * Error checking for glCompressedTexSubImage[123]D().
4710 * \return GL_TRUE if error, GL_FALSE if no error
4711 */
4712 static GLboolean
4713 compressed_subtexture_error_check(struct gl_context *ctx, GLint dims,
4714 const struct gl_texture_object *texObj,
4715 GLenum target, GLint level,
4716 GLint xoffset, GLint yoffset, GLint zoffset,
4717 GLsizei width, GLsizei height, GLsizei depth,
4718 GLenum format, GLsizei imageSize,
4719 const GLvoid *data, const char *callerName)
4720 {
4721 struct gl_texture_image *texImage;
4722 GLint expectedSize;
4723
4724 /* this will catch any invalid compressed format token */
4725 if (!_mesa_is_compressed_format(ctx, format)) {
4726 _mesa_error(ctx, GL_INVALID_ENUM,
4727 "%s(format)", callerName);
4728 return GL_TRUE;
4729 }
4730
4731 if (level < 0 || level >= _mesa_max_texture_levels(ctx, target)) {
4732 _mesa_error(ctx, GL_INVALID_VALUE,
4733 "%s(level=%d)",
4734 callerName, level);
4735 return GL_TRUE;
4736 }
4737
4738 /* validate the bound PBO, if any */
4739 if (!_mesa_validate_pbo_source_compressed(ctx, dims, &ctx->Unpack,
4740 imageSize, data, callerName)) {
4741 return GL_TRUE;
4742 }
4743
4744 /* Check for invalid pixel storage modes */
4745 if (!_mesa_compressed_pixel_storage_error_check(ctx, dims,
4746 &ctx->Unpack, callerName)) {
4747 return GL_TRUE;
4748 }
4749
4750 expectedSize = compressed_tex_size(width, height, depth, format);
4751 if (expectedSize != imageSize) {
4752 _mesa_error(ctx, GL_INVALID_VALUE,
4753 "%s(size=%d)",
4754 callerName, imageSize);
4755 return GL_TRUE;
4756 }
4757
4758 texImage = _mesa_select_tex_image(texObj, target, level);
4759 if (!texImage) {
4760 _mesa_error(ctx, GL_INVALID_OPERATION,
4761 "%s(invalid texture image)",
4762 callerName);
4763 return GL_TRUE;
4764 }
4765
4766 if ((GLint) format != texImage->InternalFormat) {
4767 _mesa_error(ctx, GL_INVALID_OPERATION,
4768 "%s(format=0x%x)",
4769 callerName, format);
4770 return GL_TRUE;
4771 }
4772
4773 if (compressedteximage_only_format(ctx, format)) {
4774 _mesa_error(ctx, GL_INVALID_OPERATION,
4775 "%s(format=0x%x cannot be updated)",
4776 callerName, format);
4777 return GL_TRUE;
4778 }
4779
4780 if (error_check_subtexture_dimensions(ctx, dims,
4781 texImage, xoffset, yoffset, zoffset,
4782 width, height, depth,
4783 callerName)) {
4784 return GL_TRUE;
4785 }
4786
4787 return GL_FALSE;
4788 }
4789
4790
4791 void GLAPIENTRY
4792 _mesa_CompressedTexImage1D(GLenum target, GLint level,
4793 GLenum internalFormat, GLsizei width,
4794 GLint border, GLsizei imageSize,
4795 const GLvoid *data)
4796 {
4797 GET_CURRENT_CONTEXT(ctx);
4798 teximage(ctx, GL_TRUE, 1, target, level, internalFormat,
4799 width, 1, 1, border, GL_NONE, GL_NONE, imageSize, data);
4800 }
4801
4802
4803 void GLAPIENTRY
4804 _mesa_CompressedTexImage2D(GLenum target, GLint level,
4805 GLenum internalFormat, GLsizei width,
4806 GLsizei height, GLint border, GLsizei imageSize,
4807 const GLvoid *data)
4808 {
4809 GET_CURRENT_CONTEXT(ctx);
4810 teximage(ctx, GL_TRUE, 2, target, level, internalFormat,
4811 width, height, 1, border, GL_NONE, GL_NONE, imageSize, data);
4812 }
4813
4814
4815 void GLAPIENTRY
4816 _mesa_CompressedTexImage3D(GLenum target, GLint level,
4817 GLenum internalFormat, GLsizei width,
4818 GLsizei height, GLsizei depth, GLint border,
4819 GLsizei imageSize, const GLvoid *data)
4820 {
4821 GET_CURRENT_CONTEXT(ctx);
4822 teximage(ctx, GL_TRUE, 3, target, level, internalFormat,
4823 width, height, depth, border, GL_NONE, GL_NONE, imageSize, data);
4824 }
4825
4826
4827 /**
4828 * Common helper for glCompressedTexSubImage1/2/3D() and
4829 * glCompressedTextureSubImage1/2/3D().
4830 */
4831 void
4832 _mesa_compressed_texture_sub_image(struct gl_context *ctx, GLuint dims,
4833 struct gl_texture_object *texObj,
4834 struct gl_texture_image *texImage,
4835 GLenum target, GLint level,
4836 GLint xoffset, GLint yoffset,
4837 GLint zoffset,
4838 GLsizei width, GLsizei height,
4839 GLsizei depth,
4840 GLenum format, GLsizei imageSize,
4841 const GLvoid *data)
4842 {
4843 FLUSH_VERTICES(ctx, 0);
4844
4845 _mesa_lock_texture(ctx, texObj);
4846 {
4847 if (width > 0 && height > 0 && depth > 0) {
4848 ctx->Driver.CompressedTexSubImage(ctx, dims, texImage,
4849 xoffset, yoffset, zoffset,
4850 width, height, depth,
4851 format, imageSize, data);
4852
4853 check_gen_mipmap(ctx, target, texObj, level);
4854
4855 /* NOTE: Don't signal _NEW_TEXTURE since we've only changed
4856 * the texel data, not the texture format, size, etc.
4857 */
4858 }
4859 }
4860 _mesa_unlock_texture(ctx, texObj);
4861 }
4862
4863
4864 void GLAPIENTRY
4865 _mesa_CompressedTexSubImage1D(GLenum target, GLint level, GLint xoffset,
4866 GLsizei width, GLenum format,
4867 GLsizei imageSize, const GLvoid *data)
4868 {
4869 struct gl_texture_object *texObj;
4870 struct gl_texture_image *texImage;
4871
4872 GET_CURRENT_CONTEXT(ctx);
4873
4874 if (compressed_subtexture_target_check(ctx, target, 1, format, false,
4875 "glCompressedTexSubImage1D")) {
4876 return;
4877 }
4878
4879 texObj = _mesa_get_current_tex_object(ctx, target);
4880 if (!texObj)
4881 return;
4882
4883 if (compressed_subtexture_error_check(ctx, 1, texObj, target,
4884 level, xoffset, 0, 0,
4885 width, 1, 1,
4886 format, imageSize, data,
4887 "glCompressedTexSubImage1D")) {
4888 return;
4889 }
4890
4891 texImage = _mesa_select_tex_image(texObj, target, level);
4892 assert(texImage);
4893
4894 _mesa_compressed_texture_sub_image(ctx, 1, texObj, texImage, target, level,
4895 xoffset, 0, 0, width, 1, 1,
4896 format, imageSize, data);
4897 }
4898
4899 void GLAPIENTRY
4900 _mesa_CompressedTextureSubImage1D(GLuint texture, GLint level, GLint xoffset,
4901 GLsizei width, GLenum format,
4902 GLsizei imageSize, const GLvoid *data)
4903 {
4904 struct gl_texture_object *texObj;
4905 struct gl_texture_image *texImage;
4906
4907 GET_CURRENT_CONTEXT(ctx);
4908
4909 texObj = _mesa_lookup_texture_err(ctx, texture,
4910 "glCompressedTextureSubImage1D");
4911 if (!texObj)
4912 return;
4913
4914 if (compressed_subtexture_target_check(ctx, texObj->Target, 1, format, true,
4915 "glCompressedTextureSubImage1D")) {
4916 return;
4917 }
4918
4919 if (compressed_subtexture_error_check(ctx, 1, texObj, texObj->Target,
4920 level, xoffset, 0, 0,
4921 width, 1, 1,
4922 format, imageSize, data,
4923 "glCompressedTextureSubImage1D")) {
4924 return;
4925 }
4926
4927 texImage = _mesa_select_tex_image(texObj, texObj->Target, level);
4928 assert(texImage);
4929
4930 _mesa_compressed_texture_sub_image(ctx, 1, texObj, texImage,
4931 texObj->Target, level,
4932 xoffset, 0, 0, width, 1, 1,
4933 format, imageSize, data);
4934 }
4935
4936
4937 void GLAPIENTRY
4938 _mesa_CompressedTexSubImage2D(GLenum target, GLint level, GLint xoffset,
4939 GLint yoffset, GLsizei width, GLsizei height,
4940 GLenum format, GLsizei imageSize,
4941 const GLvoid *data)
4942 {
4943 struct gl_texture_object *texObj;
4944 struct gl_texture_image *texImage;
4945
4946 GET_CURRENT_CONTEXT(ctx);
4947
4948 if (compressed_subtexture_target_check(ctx, target, 2, format, false,
4949 "glCompressedTexSubImage2D")) {
4950 return;
4951 }
4952
4953 texObj = _mesa_get_current_tex_object(ctx, target);
4954 if (!texObj)
4955 return;
4956
4957 if (compressed_subtexture_error_check(ctx, 2, texObj, target,
4958 level, xoffset, yoffset, 0,
4959 width, height, 1,
4960 format, imageSize, data,
4961 "glCompressedTexSubImage2D")) {
4962 return;
4963 }
4964
4965
4966 texImage = _mesa_select_tex_image(texObj, target, level);
4967 assert(texImage);
4968
4969 _mesa_compressed_texture_sub_image(ctx, 2, texObj, texImage, target, level,
4970 xoffset, yoffset, 0, width, height, 1,
4971 format, imageSize, data);
4972 }
4973
4974 void GLAPIENTRY
4975 _mesa_CompressedTextureSubImage2D(GLuint texture, GLint level, GLint xoffset,
4976 GLint yoffset,
4977 GLsizei width, GLsizei height,
4978 GLenum format, GLsizei imageSize,
4979 const GLvoid *data)
4980 {
4981 struct gl_texture_object *texObj;
4982 struct gl_texture_image *texImage;
4983
4984 GET_CURRENT_CONTEXT(ctx);
4985
4986 texObj = _mesa_lookup_texture_err(ctx, texture,
4987 "glCompressedTextureSubImage2D");
4988 if (!texObj)
4989 return;
4990
4991 if (compressed_subtexture_target_check(ctx, texObj->Target, 2, format, true,
4992 "glCompressedTextureSubImage2D")) {
4993 return;
4994 }
4995
4996 if (compressed_subtexture_error_check(ctx, 2, texObj, texObj->Target,
4997 level, xoffset, yoffset, 0,
4998 width, height, 1,
4999 format, imageSize, data,
5000 "glCompressedTextureSubImage2D")) {
5001 return;
5002 }
5003
5004 texImage = _mesa_select_tex_image(texObj, texObj->Target, level);
5005 assert(texImage);
5006
5007 _mesa_compressed_texture_sub_image(ctx, 2, texObj, texImage,
5008 texObj->Target, level,
5009 xoffset, yoffset, 0, width, height, 1,
5010 format, imageSize, data);
5011 }
5012
5013 void GLAPIENTRY
5014 _mesa_CompressedTexSubImage3D(GLenum target, GLint level, GLint xoffset,
5015 GLint yoffset, GLint zoffset, GLsizei width,
5016 GLsizei height, GLsizei depth, GLenum format,
5017 GLsizei imageSize, const GLvoid *data)
5018 {
5019 struct gl_texture_object *texObj;
5020 struct gl_texture_image *texImage;
5021
5022 GET_CURRENT_CONTEXT(ctx);
5023
5024 if (compressed_subtexture_target_check(ctx, target, 3, format, false,
5025 "glCompressedTexSubImage3D")) {
5026 return;
5027 }
5028
5029 texObj = _mesa_get_current_tex_object(ctx, target);
5030 if (!texObj)
5031 return;
5032
5033 if (compressed_subtexture_error_check(ctx, 3, texObj, target,
5034 level, xoffset, yoffset, zoffset,
5035 width, height, depth,
5036 format, imageSize, data,
5037 "glCompressedTexSubImage3D")) {
5038 return;
5039 }
5040
5041
5042 texImage = _mesa_select_tex_image(texObj, target, level);
5043 assert(texImage);
5044
5045 _mesa_compressed_texture_sub_image(ctx, 3, texObj, texImage, target, level,
5046 xoffset, yoffset, zoffset,
5047 width, height, depth,
5048 format, imageSize, data);
5049 }
5050
5051 void GLAPIENTRY
5052 _mesa_CompressedTextureSubImage3D(GLuint texture, GLint level, GLint xoffset,
5053 GLint yoffset, GLint zoffset, GLsizei width,
5054 GLsizei height, GLsizei depth,
5055 GLenum format, GLsizei imageSize,
5056 const GLvoid *data)
5057 {
5058 struct gl_texture_object *texObj;
5059 struct gl_texture_image *texImage;
5060
5061 GET_CURRENT_CONTEXT(ctx);
5062
5063 texObj = _mesa_lookup_texture_err(ctx, texture,
5064 "glCompressedTextureSubImage3D");
5065 if (!texObj)
5066 return;
5067
5068 if (compressed_subtexture_target_check(ctx, texObj->Target, 3, format, true,
5069 "glCompressedTextureSubImage3D")) {
5070 return;
5071 }
5072
5073 if (compressed_subtexture_error_check(ctx, 3, texObj, texObj->Target,
5074 level, xoffset, yoffset, zoffset,
5075 width, height, depth,
5076 format, imageSize, data,
5077 "glCompressedTextureSubImage3D")) {
5078 return;
5079 }
5080
5081 /* Must handle special case GL_TEXTURE_CUBE_MAP. */
5082 if (texObj->Target == GL_TEXTURE_CUBE_MAP) {
5083 const char *pixels = data;
5084 int i;
5085 GLint image_stride;
5086
5087 /* Make sure the texture object is a proper cube.
5088 * (See texturesubimage in teximage.c for details on why this check is
5089 * performed.)
5090 */
5091 if (!_mesa_cube_level_complete(texObj, level)) {
5092 _mesa_error(ctx, GL_INVALID_OPERATION,
5093 "glCompressedTextureSubImage3D(cube map incomplete)");
5094 return;
5095 }
5096
5097 /* Copy in each face. */
5098 for (i = 0; i < 6; ++i) {
5099 texImage = texObj->Image[i][level];
5100 assert(texImage);
5101
5102 _mesa_compressed_texture_sub_image(ctx, 3, texObj, texImage,
5103 texObj->Target, level,
5104 xoffset, yoffset, zoffset,
5105 width, height, 1,
5106 format, imageSize, pixels);
5107
5108 /* Compressed images don't have a client format */
5109 image_stride = _mesa_format_image_size(texImage->TexFormat,
5110 texImage->Width,
5111 texImage->Height, 1);
5112
5113 pixels += image_stride;
5114 imageSize -= image_stride;
5115 }
5116 }
5117 else {
5118 texImage = _mesa_select_tex_image(texObj, texObj->Target, level);
5119 assert(texImage);
5120
5121 _mesa_compressed_texture_sub_image(ctx, 3, texObj, texImage,
5122 texObj->Target, level,
5123 xoffset, yoffset, zoffset,
5124 width, height, depth,
5125 format, imageSize, data);
5126 }
5127 }
5128
5129 static mesa_format
5130 get_texbuffer_format(const struct gl_context *ctx, GLenum internalFormat)
5131 {
5132 if (ctx->API != API_OPENGL_CORE) {
5133 switch (internalFormat) {
5134 case GL_ALPHA8:
5135 return MESA_FORMAT_A_UNORM8;
5136 case GL_ALPHA16:
5137 return MESA_FORMAT_A_UNORM16;
5138 case GL_ALPHA16F_ARB:
5139 return MESA_FORMAT_A_FLOAT16;
5140 case GL_ALPHA32F_ARB:
5141 return MESA_FORMAT_A_FLOAT32;
5142 case GL_ALPHA8I_EXT:
5143 return MESA_FORMAT_A_SINT8;
5144 case GL_ALPHA16I_EXT:
5145 return MESA_FORMAT_A_SINT16;
5146 case GL_ALPHA32I_EXT:
5147 return MESA_FORMAT_A_SINT32;
5148 case GL_ALPHA8UI_EXT:
5149 return MESA_FORMAT_A_UINT8;
5150 case GL_ALPHA16UI_EXT:
5151 return MESA_FORMAT_A_UINT16;
5152 case GL_ALPHA32UI_EXT:
5153 return MESA_FORMAT_A_UINT32;
5154 case GL_LUMINANCE8:
5155 return MESA_FORMAT_L_UNORM8;
5156 case GL_LUMINANCE16:
5157 return MESA_FORMAT_L_UNORM16;
5158 case GL_LUMINANCE16F_ARB:
5159 return MESA_FORMAT_L_FLOAT16;
5160 case GL_LUMINANCE32F_ARB:
5161 return MESA_FORMAT_L_FLOAT32;
5162 case GL_LUMINANCE8I_EXT:
5163 return MESA_FORMAT_L_SINT8;
5164 case GL_LUMINANCE16I_EXT:
5165 return MESA_FORMAT_L_SINT16;
5166 case GL_LUMINANCE32I_EXT:
5167 return MESA_FORMAT_L_SINT32;
5168 case GL_LUMINANCE8UI_EXT:
5169 return MESA_FORMAT_L_UINT8;
5170 case GL_LUMINANCE16UI_EXT:
5171 return MESA_FORMAT_L_UINT16;
5172 case GL_LUMINANCE32UI_EXT:
5173 return MESA_FORMAT_L_UINT32;
5174 case GL_LUMINANCE8_ALPHA8:
5175 return MESA_FORMAT_L8A8_UNORM;
5176 case GL_LUMINANCE16_ALPHA16:
5177 return MESA_FORMAT_L16A16_UNORM;
5178 case GL_LUMINANCE_ALPHA16F_ARB:
5179 return MESA_FORMAT_LA_FLOAT16;
5180 case GL_LUMINANCE_ALPHA32F_ARB:
5181 return MESA_FORMAT_LA_FLOAT32;
5182 case GL_LUMINANCE_ALPHA8I_EXT:
5183 return MESA_FORMAT_LA_SINT8;
5184 case GL_LUMINANCE_ALPHA16I_EXT:
5185 return MESA_FORMAT_LA_SINT16;
5186 case GL_LUMINANCE_ALPHA32I_EXT:
5187 return MESA_FORMAT_LA_SINT32;
5188 case GL_LUMINANCE_ALPHA8UI_EXT:
5189 return MESA_FORMAT_LA_UINT8;
5190 case GL_LUMINANCE_ALPHA16UI_EXT:
5191 return MESA_FORMAT_LA_UINT16;
5192 case GL_LUMINANCE_ALPHA32UI_EXT:
5193 return MESA_FORMAT_LA_UINT32;
5194 case GL_INTENSITY8:
5195 return MESA_FORMAT_I_UNORM8;
5196 case GL_INTENSITY16:
5197 return MESA_FORMAT_I_UNORM16;
5198 case GL_INTENSITY16F_ARB:
5199 return MESA_FORMAT_I_FLOAT16;
5200 case GL_INTENSITY32F_ARB:
5201 return MESA_FORMAT_I_FLOAT32;
5202 case GL_INTENSITY8I_EXT:
5203 return MESA_FORMAT_I_SINT8;
5204 case GL_INTENSITY16I_EXT:
5205 return MESA_FORMAT_I_SINT16;
5206 case GL_INTENSITY32I_EXT:
5207 return MESA_FORMAT_I_SINT32;
5208 case GL_INTENSITY8UI_EXT:
5209 return MESA_FORMAT_I_UINT8;
5210 case GL_INTENSITY16UI_EXT:
5211 return MESA_FORMAT_I_UINT16;
5212 case GL_INTENSITY32UI_EXT:
5213 return MESA_FORMAT_I_UINT32;
5214 default:
5215 break;
5216 }
5217 }
5218
5219 if (ctx->API == API_OPENGL_CORE &&
5220 ctx->Extensions.ARB_texture_buffer_object_rgb32) {
5221 switch (internalFormat) {
5222 case GL_RGB32F:
5223 return MESA_FORMAT_RGB_FLOAT32;
5224 case GL_RGB32UI:
5225 return MESA_FORMAT_RGB_UINT32;
5226 case GL_RGB32I:
5227 return MESA_FORMAT_RGB_SINT32;
5228 default:
5229 break;
5230 }
5231 }
5232
5233 switch (internalFormat) {
5234 case GL_RGBA8:
5235 return MESA_FORMAT_R8G8B8A8_UNORM;
5236 case GL_RGBA16:
5237 return MESA_FORMAT_RGBA_UNORM16;
5238 case GL_RGBA16F_ARB:
5239 return MESA_FORMAT_RGBA_FLOAT16;
5240 case GL_RGBA32F_ARB:
5241 return MESA_FORMAT_RGBA_FLOAT32;
5242 case GL_RGBA8I_EXT:
5243 return MESA_FORMAT_RGBA_SINT8;
5244 case GL_RGBA16I_EXT:
5245 return MESA_FORMAT_RGBA_SINT16;
5246 case GL_RGBA32I_EXT:
5247 return MESA_FORMAT_RGBA_SINT32;
5248 case GL_RGBA8UI_EXT:
5249 return MESA_FORMAT_RGBA_UINT8;
5250 case GL_RGBA16UI_EXT:
5251 return MESA_FORMAT_RGBA_UINT16;
5252 case GL_RGBA32UI_EXT:
5253 return MESA_FORMAT_RGBA_UINT32;
5254
5255 case GL_RG8:
5256 return MESA_FORMAT_R8G8_UNORM;
5257 case GL_RG16:
5258 return MESA_FORMAT_R16G16_UNORM;
5259 case GL_RG16F:
5260 return MESA_FORMAT_RG_FLOAT16;
5261 case GL_RG32F:
5262 return MESA_FORMAT_RG_FLOAT32;
5263 case GL_RG8I:
5264 return MESA_FORMAT_RG_SINT8;
5265 case GL_RG16I:
5266 return MESA_FORMAT_RG_SINT16;
5267 case GL_RG32I:
5268 return MESA_FORMAT_RG_SINT32;
5269 case GL_RG8UI:
5270 return MESA_FORMAT_RG_UINT8;
5271 case GL_RG16UI:
5272 return MESA_FORMAT_RG_UINT16;
5273 case GL_RG32UI:
5274 return MESA_FORMAT_RG_UINT32;
5275
5276 case GL_R8:
5277 return MESA_FORMAT_R_UNORM8;
5278 case GL_R16:
5279 return MESA_FORMAT_R_UNORM16;
5280 case GL_R16F:
5281 return MESA_FORMAT_R_FLOAT16;
5282 case GL_R32F:
5283 return MESA_FORMAT_R_FLOAT32;
5284 case GL_R8I:
5285 return MESA_FORMAT_R_SINT8;
5286 case GL_R16I:
5287 return MESA_FORMAT_R_SINT16;
5288 case GL_R32I:
5289 return MESA_FORMAT_R_SINT32;
5290 case GL_R8UI:
5291 return MESA_FORMAT_R_UINT8;
5292 case GL_R16UI:
5293 return MESA_FORMAT_R_UINT16;
5294 case GL_R32UI:
5295 return MESA_FORMAT_R_UINT32;
5296
5297 default:
5298 return MESA_FORMAT_NONE;
5299 }
5300 }
5301
5302
5303 mesa_format
5304 _mesa_validate_texbuffer_format(const struct gl_context *ctx,
5305 GLenum internalFormat)
5306 {
5307 mesa_format format = get_texbuffer_format(ctx, internalFormat);
5308 GLenum datatype;
5309
5310 if (format == MESA_FORMAT_NONE)
5311 return MESA_FORMAT_NONE;
5312
5313 datatype = _mesa_get_format_datatype(format);
5314
5315 /* The GL_ARB_texture_buffer_object spec says:
5316 *
5317 * "If ARB_texture_float is not supported, references to the
5318 * floating-point internal formats provided by that extension should be
5319 * removed, and such formats may not be passed to TexBufferARB."
5320 *
5321 * As a result, GL_HALF_FLOAT internal format depends on both
5322 * GL_ARB_texture_float and GL_ARB_half_float_pixel.
5323 */
5324 if ((datatype == GL_FLOAT || datatype == GL_HALF_FLOAT) &&
5325 !ctx->Extensions.ARB_texture_float)
5326 return MESA_FORMAT_NONE;
5327
5328 if (!ctx->Extensions.ARB_texture_rg) {
5329 GLenum base_format = _mesa_get_format_base_format(format);
5330 if (base_format == GL_R || base_format == GL_RG)
5331 return MESA_FORMAT_NONE;
5332 }
5333
5334 if (!ctx->Extensions.ARB_texture_buffer_object_rgb32) {
5335 GLenum base_format = _mesa_get_format_base_format(format);
5336 if (base_format == GL_RGB)
5337 return MESA_FORMAT_NONE;
5338 }
5339 return format;
5340 }
5341
5342
5343 void
5344 _mesa_texture_buffer_range(struct gl_context *ctx,
5345 struct gl_texture_object *texObj,
5346 GLenum internalFormat,
5347 struct gl_buffer_object *bufObj,
5348 GLintptr offset, GLsizeiptr size,
5349 const char *caller)
5350 {
5351 mesa_format format;
5352
5353 /* NOTE: ARB_texture_buffer_object has interactions with
5354 * the compatibility profile that are not implemented.
5355 */
5356 if (!(ctx->API == API_OPENGL_CORE &&
5357 ctx->Extensions.ARB_texture_buffer_object)) {
5358 _mesa_error(ctx, GL_INVALID_OPERATION,
5359 "%s(ARB_texture_buffer_object is not"
5360 " implemented for the compatibility profile)", caller);
5361 return;
5362 }
5363
5364 format = _mesa_validate_texbuffer_format(ctx, internalFormat);
5365 if (format == MESA_FORMAT_NONE) {
5366 _mesa_error(ctx, GL_INVALID_ENUM,
5367 "%s(internalFormat 0x%x)", caller, internalFormat);
5368 return;
5369 }
5370
5371 FLUSH_VERTICES(ctx, 0);
5372
5373 _mesa_lock_texture(ctx, texObj);
5374 {
5375 _mesa_reference_buffer_object(ctx, &texObj->BufferObject, bufObj);
5376 texObj->BufferObjectFormat = internalFormat;
5377 texObj->_BufferObjectFormat = format;
5378 texObj->BufferOffset = offset;
5379 texObj->BufferSize = size;
5380 }
5381 _mesa_unlock_texture(ctx, texObj);
5382
5383 ctx->NewDriverState |= ctx->DriverFlags.NewTextureBuffer;
5384
5385 if (bufObj) {
5386 bufObj->UsageHistory |= USAGE_TEXTURE_BUFFER;
5387 }
5388 }
5389
5390
5391 /**
5392 * Make sure the texture buffer target is GL_TEXTURE_BUFFER.
5393 * Return true if it is, and return false if it is not
5394 * (and throw INVALID ENUM as dictated in the OpenGL 4.5
5395 * core spec, 02.02.2015, PDF page 245).
5396 */
5397 static bool
5398 check_texture_buffer_target(struct gl_context *ctx, GLenum target,
5399 const char *caller)
5400 {
5401 if (target != GL_TEXTURE_BUFFER_ARB) {
5402 _mesa_error(ctx, GL_INVALID_ENUM,
5403 "%s(texture target is not GL_TEXTURE_BUFFER)", caller);
5404 return false;
5405 }
5406 else
5407 return true;
5408 }
5409
5410 /**
5411 * Check for errors related to the texture buffer range.
5412 * Return false if errors are found, true if none are found.
5413 */
5414 static bool
5415 check_texture_buffer_range(struct gl_context *ctx,
5416 struct gl_buffer_object *bufObj,
5417 GLintptr offset, GLsizeiptr size,
5418 const char *caller)
5419 {
5420 /* OpenGL 4.5 core spec (02.02.2015) says in Section 8.9 Buffer
5421 * Textures (PDF page 245):
5422 * "An INVALID_VALUE error is generated if offset is negative, if
5423 * size is less than or equal to zero, or if offset + size is greater
5424 * than the value of BUFFER_SIZE for the buffer bound to target."
5425 */
5426 if (offset < 0) {
5427 _mesa_error(ctx, GL_INVALID_VALUE, "%s(offset=%d < 0)", caller,
5428 (int) offset);
5429 return false;
5430 }
5431
5432 if (size <= 0) {
5433 _mesa_error(ctx, GL_INVALID_VALUE, "%s(size=%d <= 0)", caller,
5434 (int) size);
5435 return false;
5436 }
5437
5438 if (offset + size > bufObj->Size) {
5439 _mesa_error(ctx, GL_INVALID_VALUE,
5440 "%s(offset=%d + size=%d > buffer_size=%d)", caller,
5441 (int) offset, (int) size, (int) bufObj->Size);
5442 return false;
5443 }
5444
5445 /* OpenGL 4.5 core spec (02.02.2015) says in Section 8.9 Buffer
5446 * Textures (PDF page 245):
5447 * "An INVALID_VALUE error is generated if offset is not an integer
5448 * multiple of the value of TEXTURE_BUFFER_OFFSET_ALIGNMENT."
5449 */
5450 if (offset % ctx->Const.TextureBufferOffsetAlignment) {
5451 _mesa_error(ctx, GL_INVALID_VALUE,
5452 "%s(invalid offset alignment)", caller);
5453 return false;
5454 }
5455
5456 return true;
5457 }
5458
5459
5460 /** GL_ARB_texture_buffer_object */
5461 void GLAPIENTRY
5462 _mesa_TexBuffer(GLenum target, GLenum internalFormat, GLuint buffer)
5463 {
5464 struct gl_texture_object *texObj;
5465 struct gl_buffer_object *bufObj;
5466
5467 GET_CURRENT_CONTEXT(ctx);
5468
5469 /* Need to catch a bad target before it gets to
5470 * _mesa_get_current_tex_object.
5471 */
5472 if (!check_texture_buffer_target(ctx, target, "glTexBuffer"))
5473 return;
5474
5475 if (buffer) {
5476 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, "glTexBuffer");
5477 if (!bufObj)
5478 return;
5479 } else
5480 bufObj = NULL;
5481
5482 texObj = _mesa_get_current_tex_object(ctx, target);
5483 if (!texObj)
5484 return;
5485
5486 _mesa_texture_buffer_range(ctx, texObj, internalFormat, bufObj, 0,
5487 buffer ? -1 : 0, "glTexBuffer");
5488 }
5489
5490
5491 /** GL_ARB_texture_buffer_range */
5492 void GLAPIENTRY
5493 _mesa_TexBufferRange(GLenum target, GLenum internalFormat, GLuint buffer,
5494 GLintptr offset, GLsizeiptr size)
5495 {
5496 struct gl_texture_object *texObj;
5497 struct gl_buffer_object *bufObj;
5498
5499 GET_CURRENT_CONTEXT(ctx);
5500
5501 /* Need to catch a bad target before it gets to
5502 * _mesa_get_current_tex_object.
5503 */
5504 if (!check_texture_buffer_target(ctx, target, "glTexBufferRange"))
5505 return;
5506
5507 if (buffer) {
5508 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, "glTexBufferRange");
5509 if (!bufObj)
5510 return;
5511
5512 if (!check_texture_buffer_range(ctx, bufObj, offset, size,
5513 "glTexBufferRange"))
5514 return;
5515
5516 } else {
5517 /* OpenGL 4.5 core spec (02.02.2015) says in Section 8.9 Buffer
5518 * Textures (PDF page 254):
5519 * "If buffer is zero, then any buffer object attached to the buffer
5520 * texture is detached, the values offset and size are ignored and
5521 * the state for offset and size for the buffer texture are reset to
5522 * zero."
5523 */
5524 offset = 0;
5525 size = 0;
5526 bufObj = NULL;
5527 }
5528
5529 texObj = _mesa_get_current_tex_object(ctx, target);
5530 if (!texObj)
5531 return;
5532
5533 _mesa_texture_buffer_range(ctx, texObj, internalFormat, bufObj,
5534 offset, size, "glTexBufferRange");
5535 }
5536
5537 void GLAPIENTRY
5538 _mesa_TextureBuffer(GLuint texture, GLenum internalFormat, GLuint buffer)
5539 {
5540 struct gl_texture_object *texObj;
5541 struct gl_buffer_object *bufObj;
5542
5543 GET_CURRENT_CONTEXT(ctx);
5544
5545 if (buffer) {
5546 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, "glTextureBuffer");
5547 if (!bufObj)
5548 return;
5549 } else
5550 bufObj = NULL;
5551
5552 /* Get the texture object by Name. */
5553 texObj = _mesa_lookup_texture_err(ctx, texture, "glTextureBuffer");
5554 if (!texObj)
5555 return;
5556
5557 if (!check_texture_buffer_target(ctx, texObj->Target, "glTextureBuffer"))
5558 return;
5559
5560 _mesa_texture_buffer_range(ctx, texObj, internalFormat,
5561 bufObj, 0, buffer ? -1 : 0, "glTextureBuffer");
5562 }
5563
5564 void GLAPIENTRY
5565 _mesa_TextureBufferRange(GLuint texture, GLenum internalFormat, GLuint buffer,
5566 GLintptr offset, GLsizeiptr size)
5567 {
5568 struct gl_texture_object *texObj;
5569 struct gl_buffer_object *bufObj;
5570
5571 GET_CURRENT_CONTEXT(ctx);
5572
5573 if (buffer) {
5574 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer,
5575 "glTextureBufferRange");
5576 if (!bufObj)
5577 return;
5578
5579 if (!check_texture_buffer_range(ctx, bufObj, offset, size,
5580 "glTextureBufferRange"))
5581 return;
5582
5583 } else {
5584 /* OpenGL 4.5 core spec (02.02.2015) says in Section 8.9 Buffer
5585 * Textures (PDF page 254):
5586 * "If buffer is zero, then any buffer object attached to the buffer
5587 * texture is detached, the values offset and size are ignored and
5588 * the state for offset and size for the buffer texture are reset to
5589 * zero."
5590 */
5591 offset = 0;
5592 size = 0;
5593 bufObj = NULL;
5594 }
5595
5596 /* Get the texture object by Name. */
5597 texObj = _mesa_lookup_texture_err(ctx, texture, "glTextureBufferRange");
5598 if (!texObj)
5599 return;
5600
5601 if (!check_texture_buffer_target(ctx, texObj->Target,
5602 "glTextureBufferRange"))
5603 return;
5604
5605 _mesa_texture_buffer_range(ctx, texObj, internalFormat,
5606 bufObj, offset, size, "glTextureBufferRange");
5607 }
5608
5609 static GLboolean
5610 is_renderable_texture_format(struct gl_context *ctx, GLenum internalformat)
5611 {
5612 /* Everything that is allowed for renderbuffers,
5613 * except for a base format of GL_STENCIL_INDEX, unless supported.
5614 */
5615 GLenum baseFormat = _mesa_base_fbo_format(ctx, internalformat);
5616 if (ctx->Extensions.ARB_texture_stencil8)
5617 return baseFormat != 0;
5618 else
5619 return baseFormat != 0 && baseFormat != GL_STENCIL_INDEX;
5620 }
5621
5622
5623 /** GL_ARB_texture_multisample */
5624 static GLboolean
5625 check_multisample_target(GLuint dims, GLenum target, bool dsa)
5626 {
5627 switch(target) {
5628 case GL_TEXTURE_2D_MULTISAMPLE:
5629 return dims == 2;
5630 case GL_PROXY_TEXTURE_2D_MULTISAMPLE:
5631 return dims == 2 && !dsa;
5632 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
5633 return dims == 3;
5634 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY:
5635 return dims == 3 && !dsa;
5636 default:
5637 return GL_FALSE;
5638 }
5639 }
5640
5641
5642 static void
5643 texture_image_multisample(struct gl_context *ctx, GLuint dims,
5644 struct gl_texture_object *texObj,
5645 GLenum target, GLsizei samples,
5646 GLint internalformat, GLsizei width,
5647 GLsizei height, GLsizei depth,
5648 GLboolean fixedsamplelocations,
5649 GLboolean immutable, const char *func)
5650 {
5651 struct gl_texture_image *texImage;
5652 GLboolean sizeOK, dimensionsOK, samplesOK;
5653 mesa_format texFormat;
5654 GLenum sample_count_error;
5655 bool dsa = strstr(func, "ture") ? true : false;
5656
5657 if (!((ctx->Extensions.ARB_texture_multisample
5658 && _mesa_is_desktop_gl(ctx))) && !_mesa_is_gles31(ctx)) {
5659 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(unsupported)", func);
5660 return;
5661 }
5662
5663 if (samples < 1) {
5664 _mesa_error(ctx, GL_INVALID_VALUE, "%s(samples < 1)", func);
5665 return;
5666 }
5667
5668 if (!check_multisample_target(dims, target, dsa)) {
5669 if (dsa) {
5670 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(target)", func);
5671 return;
5672 }
5673 else {
5674 _mesa_error(ctx, GL_INVALID_ENUM, "%s(target)", func);
5675 return;
5676 }
5677 }
5678
5679 /* check that the specified internalformat is color/depth/stencil-renderable;
5680 * refer GL3.1 spec 4.4.4
5681 */
5682
5683 if (immutable && !_mesa_is_legal_tex_storage_format(ctx, internalformat)) {
5684 _mesa_error(ctx, GL_INVALID_ENUM,
5685 "%s(internalformat=%s not legal for immutable-format)",
5686 func, _mesa_enum_to_string(internalformat));
5687 return;
5688 }
5689
5690 if (!is_renderable_texture_format(ctx, internalformat)) {
5691 /* Page 172 of OpenGL ES 3.1 spec says:
5692 * "An INVALID_ENUM error is generated if sizedinternalformat is not
5693 * color-renderable, depth-renderable, or stencil-renderable (as
5694 * defined in section 9.4).
5695 *
5696 * (Same error is also defined for desktop OpenGL for multisample
5697 * teximage/texstorage functions.)
5698 */
5699 _mesa_error(ctx, GL_INVALID_ENUM, "%s(internalformat=%s)", func,
5700 _mesa_enum_to_string(internalformat));
5701 return;
5702 }
5703
5704 sample_count_error = _mesa_check_sample_count(ctx, target,
5705 internalformat, samples);
5706 samplesOK = sample_count_error == GL_NO_ERROR;
5707
5708 /* Page 254 of OpenGL 4.4 spec says:
5709 * "Proxy arrays for two-dimensional multisample and two-dimensional
5710 * multisample array textures are operated on in the same way when
5711 * TexImage2DMultisample is called with target specified as
5712 * PROXY_TEXTURE_2D_MULTISAMPLE, or TexImage3DMultisample is called
5713 * with target specified as PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY.
5714 * However, if samples is not supported, then no error is generated.
5715 */
5716 if (!samplesOK && !_mesa_is_proxy_texture(target)) {
5717 _mesa_error(ctx, sample_count_error, "%s(samples)", func);
5718 return;
5719 }
5720
5721 if (immutable && (!texObj || (texObj->Name == 0))) {
5722 _mesa_error(ctx, GL_INVALID_OPERATION,
5723 "%s(texture object 0)",
5724 func);
5725 return;
5726 }
5727
5728 texImage = _mesa_get_tex_image(ctx, texObj, 0, 0);
5729
5730 if (texImage == NULL) {
5731 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s()", func);
5732 return;
5733 }
5734
5735 texFormat = _mesa_choose_texture_format(ctx, texObj, target, 0,
5736 internalformat, GL_NONE, GL_NONE);
5737 assert(texFormat != MESA_FORMAT_NONE);
5738
5739 dimensionsOK = _mesa_legal_texture_dimensions(ctx, target, 0,
5740 width, height, depth, 0);
5741
5742 sizeOK = ctx->Driver.TestProxyTexImage(ctx, target, 0, texFormat,
5743 width, height, depth, 0);
5744
5745 if (_mesa_is_proxy_texture(target)) {
5746 if (samplesOK && dimensionsOK && sizeOK) {
5747 init_teximage_fields_ms(ctx, texImage, width, height, depth, 0,
5748 internalformat, texFormat,
5749 samples, fixedsamplelocations);
5750 }
5751 else {
5752 /* clear all image fields */
5753 clear_teximage_fields(texImage);
5754 }
5755 }
5756 else {
5757 if (!dimensionsOK) {
5758 _mesa_error(ctx, GL_INVALID_VALUE,
5759 "%s(invalid width or height)", func);
5760 return;
5761 }
5762
5763 if (!sizeOK) {
5764 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s(texture too large)", func);
5765 return;
5766 }
5767
5768 /* Check if texObj->Immutable is set */
5769 if (texObj->Immutable) {
5770 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(immutable)", func);
5771 return;
5772 }
5773
5774 ctx->Driver.FreeTextureImageBuffer(ctx, texImage);
5775
5776 init_teximage_fields_ms(ctx, texImage, width, height, depth, 0,
5777 internalformat, texFormat,
5778 samples, fixedsamplelocations);
5779
5780 if (width > 0 && height > 0 && depth > 0) {
5781 if (!ctx->Driver.AllocTextureStorage(ctx, texObj, 1,
5782 width, height, depth)) {
5783 /* tidy up the texture image state. strictly speaking,
5784 * we're allowed to just leave this in whatever state we
5785 * like, but being tidy is good.
5786 */
5787 _mesa_init_teximage_fields(ctx, texImage,
5788 0, 0, 0, 0, GL_NONE, MESA_FORMAT_NONE);
5789 }
5790 }
5791
5792 texObj->Immutable |= immutable;
5793
5794 if (immutable) {
5795 _mesa_set_texture_view_state(ctx, texObj, target, 1);
5796 }
5797
5798 _mesa_update_fbo_texture(ctx, texObj, 0, 0);
5799 }
5800 }
5801
5802
5803 void GLAPIENTRY
5804 _mesa_TexImage2DMultisample(GLenum target, GLsizei samples,
5805 GLenum internalformat, GLsizei width,
5806 GLsizei height, GLboolean fixedsamplelocations)
5807 {
5808 struct gl_texture_object *texObj;
5809 GET_CURRENT_CONTEXT(ctx);
5810
5811 texObj = _mesa_get_current_tex_object(ctx, target);
5812 if (!texObj)
5813 return;
5814
5815 texture_image_multisample(ctx, 2, texObj, target, samples,
5816 internalformat, width, height, 1,
5817 fixedsamplelocations, GL_FALSE,
5818 "glTexImage2DMultisample");
5819 }
5820
5821
5822 void GLAPIENTRY
5823 _mesa_TexImage3DMultisample(GLenum target, GLsizei samples,
5824 GLenum internalformat, GLsizei width,
5825 GLsizei height, GLsizei depth,
5826 GLboolean fixedsamplelocations)
5827 {
5828 struct gl_texture_object *texObj;
5829 GET_CURRENT_CONTEXT(ctx);
5830
5831 texObj = _mesa_get_current_tex_object(ctx, target);
5832 if (!texObj)
5833 return;
5834
5835 texture_image_multisample(ctx, 3, texObj, target, samples,
5836 internalformat, width, height, depth,
5837 fixedsamplelocations, GL_FALSE,
5838 "glTexImage3DMultisample");
5839 }
5840
5841 static bool
5842 valid_texstorage_ms_parameters(GLsizei width, GLsizei height, GLsizei depth,
5843 GLsizei samples, unsigned dims)
5844 {
5845 GET_CURRENT_CONTEXT(ctx);
5846
5847 if (!_mesa_valid_tex_storage_dim(width, height, depth)) {
5848 _mesa_error(ctx, GL_INVALID_VALUE,
5849 "glTexStorage%uDMultisample(width=%d,height=%d,depth=%d)",
5850 dims, width, height, depth);
5851 return false;
5852 }
5853 return true;
5854 }
5855
5856 void GLAPIENTRY
5857 _mesa_TexStorage2DMultisample(GLenum target, GLsizei samples,
5858 GLenum internalformat, GLsizei width,
5859 GLsizei height, GLboolean fixedsamplelocations)
5860 {
5861 struct gl_texture_object *texObj;
5862 GET_CURRENT_CONTEXT(ctx);
5863
5864 texObj = _mesa_get_current_tex_object(ctx, target);
5865 if (!texObj)
5866 return;
5867
5868 if (!valid_texstorage_ms_parameters(width, height, 1, samples, 2))
5869 return;
5870
5871 texture_image_multisample(ctx, 2, texObj, target, samples,
5872 internalformat, width, height, 1,
5873 fixedsamplelocations, GL_TRUE,
5874 "glTexStorage2DMultisample");
5875 }
5876
5877 void GLAPIENTRY
5878 _mesa_TexStorage3DMultisample(GLenum target, GLsizei samples,
5879 GLenum internalformat, GLsizei width,
5880 GLsizei height, GLsizei depth,
5881 GLboolean fixedsamplelocations)
5882 {
5883 struct gl_texture_object *texObj;
5884 GET_CURRENT_CONTEXT(ctx);
5885
5886 texObj = _mesa_get_current_tex_object(ctx, target);
5887 if (!texObj)
5888 return;
5889
5890 if (!valid_texstorage_ms_parameters(width, height, depth, samples, 3))
5891 return;
5892
5893 texture_image_multisample(ctx, 3, texObj, target, samples,
5894 internalformat, width, height, depth,
5895 fixedsamplelocations, GL_TRUE,
5896 "glTexStorage3DMultisample");
5897 }
5898
5899 void GLAPIENTRY
5900 _mesa_TextureStorage2DMultisample(GLuint texture, GLsizei samples,
5901 GLenum internalformat, GLsizei width,
5902 GLsizei height,
5903 GLboolean fixedsamplelocations)
5904 {
5905 struct gl_texture_object *texObj;
5906 GET_CURRENT_CONTEXT(ctx);
5907
5908 texObj = _mesa_lookup_texture_err(ctx, texture,
5909 "glTextureStorage2DMultisample");
5910 if (!texObj)
5911 return;
5912
5913 if (!valid_texstorage_ms_parameters(width, height, 1, samples, 2))
5914 return;
5915
5916 texture_image_multisample(ctx, 2, texObj, texObj->Target, samples,
5917 internalformat, width, height, 1,
5918 fixedsamplelocations, GL_TRUE,
5919 "glTextureStorage2DMultisample");
5920 }
5921
5922 void GLAPIENTRY
5923 _mesa_TextureStorage3DMultisample(GLuint texture, GLsizei samples,
5924 GLenum internalformat, GLsizei width,
5925 GLsizei height, GLsizei depth,
5926 GLboolean fixedsamplelocations)
5927 {
5928 struct gl_texture_object *texObj;
5929 GET_CURRENT_CONTEXT(ctx);
5930
5931 /* Get the texture object by Name. */
5932 texObj = _mesa_lookup_texture_err(ctx, texture,
5933 "glTextureStorage3DMultisample");
5934 if (!texObj)
5935 return;
5936
5937 if (!valid_texstorage_ms_parameters(width, height, depth, samples, 3))
5938 return;
5939
5940 texture_image_multisample(ctx, 3, texObj, texObj->Target, samples,
5941 internalformat, width, height, depth,
5942 fixedsamplelocations, GL_TRUE,
5943 "glTextureStorage3DMultisample");
5944 }