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