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