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