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