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