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