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