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