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