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