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