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