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