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