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