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