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