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