intel/disasm: brw_label and support functions
[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
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 * Returns a corresponding base format for a given internal floating point
127 * format as specifed by OES_texture_float.
128 */
129 static GLenum
130 oes_float_internal_format(const struct gl_context *ctx,
131 GLenum format, GLenum type)
132 {
133 switch (type) {
134 case GL_FLOAT:
135 if (ctx->Extensions.OES_texture_float) {
136 switch (format) {
137 case GL_RGBA32F:
138 return GL_RGBA;
139 case GL_RGB32F:
140 return GL_RGB;
141 case GL_ALPHA32F_ARB:
142 return GL_ALPHA;
143 case GL_LUMINANCE32F_ARB:
144 return GL_LUMINANCE;
145 case GL_LUMINANCE_ALPHA32F_ARB:
146 return GL_LUMINANCE_ALPHA;
147 default:
148 break;
149 }
150 }
151 break;
152
153 case GL_HALF_FLOAT_OES:
154 if (ctx->Extensions.OES_texture_half_float) {
155 switch (format) {
156 case GL_RGBA16F:
157 return GL_RGBA;
158 case GL_RGB16F:
159 return GL_RGB;
160 case GL_ALPHA16F_ARB:
161 return GL_ALPHA;
162 case GL_LUMINANCE16F_ARB:
163 return GL_LUMINANCE;
164 case GL_LUMINANCE_ALPHA16F_ARB:
165 return GL_LUMINANCE_ALPHA;
166 default:
167 break;
168 }
169 }
170 break;
171 }
172 return format;
173 }
174
175
176 /**
177 * Install gl_texture_image in a gl_texture_object according to the target
178 * and level parameters.
179 *
180 * \param tObj texture object.
181 * \param target texture target.
182 * \param level image level.
183 * \param texImage texture image.
184 */
185 static void
186 set_tex_image(struct gl_texture_object *tObj,
187 GLenum target, GLint level,
188 struct gl_texture_image *texImage)
189 {
190 const GLuint face = _mesa_tex_target_to_face(target);
191
192 assert(tObj);
193 assert(texImage);
194 if (target == GL_TEXTURE_RECTANGLE_NV || target == GL_TEXTURE_EXTERNAL_OES)
195 assert(level == 0);
196
197 tObj->Image[face][level] = texImage;
198
199 /* Set the 'back' pointer */
200 texImage->TexObject = tObj;
201 texImage->Level = level;
202 texImage->Face = face;
203 }
204
205
206 /**
207 * Free a gl_texture_image and associated data.
208 * This function is a fallback called via ctx->Driver.DeleteTextureImage().
209 *
210 * \param texImage texture image.
211 *
212 * Free the texture image structure and the associated image data.
213 */
214 void
215 _mesa_delete_texture_image(struct gl_context *ctx,
216 struct gl_texture_image *texImage)
217 {
218 /* Free texImage->Data and/or any other driver-specific texture
219 * image storage.
220 */
221 assert(ctx->Driver.FreeTextureImageBuffer);
222 ctx->Driver.FreeTextureImageBuffer( ctx, texImage );
223 free(texImage);
224 }
225
226
227 /**
228 * Test if a target is a proxy target.
229 *
230 * \param target texture target.
231 *
232 * \return GL_TRUE if the target is a proxy target, GL_FALSE otherwise.
233 */
234 GLboolean
235 _mesa_is_proxy_texture(GLenum target)
236 {
237 unsigned i;
238 static const GLenum targets[] = {
239 GL_PROXY_TEXTURE_1D,
240 GL_PROXY_TEXTURE_2D,
241 GL_PROXY_TEXTURE_3D,
242 GL_PROXY_TEXTURE_CUBE_MAP,
243 GL_PROXY_TEXTURE_RECTANGLE,
244 GL_PROXY_TEXTURE_1D_ARRAY,
245 GL_PROXY_TEXTURE_2D_ARRAY,
246 GL_PROXY_TEXTURE_CUBE_MAP_ARRAY,
247 GL_PROXY_TEXTURE_2D_MULTISAMPLE,
248 GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
249 };
250 /*
251 * NUM_TEXTURE_TARGETS should match number of terms above, except there's no
252 * proxy for GL_TEXTURE_BUFFER and GL_TEXTURE_EXTERNAL_OES.
253 */
254 STATIC_ASSERT(NUM_TEXTURE_TARGETS == ARRAY_SIZE(targets) + 2);
255
256 for (i = 0; i < ARRAY_SIZE(targets); ++i)
257 if (target == targets[i])
258 return GL_TRUE;
259 return GL_FALSE;
260 }
261
262
263 /**
264 * Test if a target is an array target.
265 *
266 * \param target texture target.
267 *
268 * \return true if the target is an array target, false otherwise.
269 */
270 bool
271 _mesa_is_array_texture(GLenum target)
272 {
273 switch (target) {
274 case GL_TEXTURE_1D_ARRAY:
275 case GL_TEXTURE_2D_ARRAY:
276 case GL_TEXTURE_CUBE_MAP_ARRAY:
277 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
278 return true;
279 default:
280 return false;
281 };
282 }
283
284 /**
285 * Test if a target is a cube map.
286 *
287 * \param target texture target.
288 *
289 * \return true if the target is a cube map, false otherwise.
290 */
291 bool
292 _mesa_is_cube_map_texture(GLenum target)
293 {
294 switch(target) {
295 case GL_TEXTURE_CUBE_MAP:
296 case GL_TEXTURE_CUBE_MAP_ARRAY:
297 return true;
298 default:
299 return false;
300 }
301 }
302
303 /**
304 * Return the proxy target which corresponds to the given texture target
305 */
306 static GLenum
307 proxy_target(GLenum target)
308 {
309 switch (target) {
310 case GL_TEXTURE_1D:
311 case GL_PROXY_TEXTURE_1D:
312 return GL_PROXY_TEXTURE_1D;
313 case GL_TEXTURE_2D:
314 case GL_PROXY_TEXTURE_2D:
315 return GL_PROXY_TEXTURE_2D;
316 case GL_TEXTURE_3D:
317 case GL_PROXY_TEXTURE_3D:
318 return GL_PROXY_TEXTURE_3D;
319 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
320 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
321 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
322 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
323 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
324 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
325 case GL_TEXTURE_CUBE_MAP:
326 case GL_PROXY_TEXTURE_CUBE_MAP:
327 return GL_PROXY_TEXTURE_CUBE_MAP;
328 case GL_TEXTURE_RECTANGLE_NV:
329 case GL_PROXY_TEXTURE_RECTANGLE_NV:
330 return GL_PROXY_TEXTURE_RECTANGLE_NV;
331 case GL_TEXTURE_1D_ARRAY_EXT:
332 case GL_PROXY_TEXTURE_1D_ARRAY_EXT:
333 return GL_PROXY_TEXTURE_1D_ARRAY_EXT;
334 case GL_TEXTURE_2D_ARRAY_EXT:
335 case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
336 return GL_PROXY_TEXTURE_2D_ARRAY_EXT;
337 case GL_TEXTURE_CUBE_MAP_ARRAY:
338 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
339 return GL_PROXY_TEXTURE_CUBE_MAP_ARRAY;
340 case GL_TEXTURE_2D_MULTISAMPLE:
341 case GL_PROXY_TEXTURE_2D_MULTISAMPLE:
342 return GL_PROXY_TEXTURE_2D_MULTISAMPLE;
343 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
344 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY:
345 return GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY;
346 default:
347 _mesa_problem(NULL, "unexpected target in proxy_target()");
348 return 0;
349 }
350 }
351
352
353
354
355 /**
356 * Get a texture image pointer from a texture object, given a texture
357 * target and mipmap level. The target and level parameters should
358 * have already been error-checked.
359 *
360 * \param texObj texture unit.
361 * \param target texture target.
362 * \param level image level.
363 *
364 * \return pointer to the texture image structure, or NULL on failure.
365 */
366 struct gl_texture_image *
367 _mesa_select_tex_image(const struct gl_texture_object *texObj,
368 GLenum target, GLint level)
369 {
370 const GLuint face = _mesa_tex_target_to_face(target);
371
372 assert(texObj);
373 assert(level >= 0);
374 assert(level < MAX_TEXTURE_LEVELS);
375
376 return texObj->Image[face][level];
377 }
378
379
380 /**
381 * Like _mesa_select_tex_image() but if the image doesn't exist, allocate
382 * it and install it. Only return NULL if passed a bad parameter or run
383 * out of memory.
384 */
385 struct gl_texture_image *
386 _mesa_get_tex_image(struct gl_context *ctx, struct gl_texture_object *texObj,
387 GLenum target, GLint level)
388 {
389 struct gl_texture_image *texImage;
390
391 if (!texObj)
392 return NULL;
393
394 texImage = _mesa_select_tex_image(texObj, target, level);
395 if (!texImage) {
396 texImage = ctx->Driver.NewTextureImage(ctx);
397 if (!texImage) {
398 _mesa_error(ctx, GL_OUT_OF_MEMORY, "texture image allocation");
399 return NULL;
400 }
401
402 set_tex_image(texObj, target, level, texImage);
403 }
404
405 return texImage;
406 }
407
408
409 /**
410 * Return pointer to the specified proxy texture image.
411 * Note that proxy textures are per-context, not per-texture unit.
412 * \return pointer to texture image or NULL if invalid target, invalid
413 * level, or out of memory.
414 */
415 static struct gl_texture_image *
416 get_proxy_tex_image(struct gl_context *ctx, GLenum target, GLint level)
417 {
418 struct gl_texture_image *texImage;
419 GLuint texIndex;
420
421 if (level < 0)
422 return NULL;
423
424 switch (target) {
425 case GL_PROXY_TEXTURE_1D:
426 texIndex = TEXTURE_1D_INDEX;
427 break;
428 case GL_PROXY_TEXTURE_2D:
429 texIndex = TEXTURE_2D_INDEX;
430 break;
431 case GL_PROXY_TEXTURE_3D:
432 texIndex = TEXTURE_3D_INDEX;
433 break;
434 case GL_PROXY_TEXTURE_CUBE_MAP:
435 texIndex = TEXTURE_CUBE_INDEX;
436 break;
437 case GL_PROXY_TEXTURE_RECTANGLE_NV:
438 if (level > 0)
439 return NULL;
440 texIndex = TEXTURE_RECT_INDEX;
441 break;
442 case GL_PROXY_TEXTURE_1D_ARRAY_EXT:
443 texIndex = TEXTURE_1D_ARRAY_INDEX;
444 break;
445 case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
446 texIndex = TEXTURE_2D_ARRAY_INDEX;
447 break;
448 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
449 texIndex = TEXTURE_CUBE_ARRAY_INDEX;
450 break;
451 case GL_PROXY_TEXTURE_2D_MULTISAMPLE:
452 texIndex = TEXTURE_2D_MULTISAMPLE_INDEX;
453 break;
454 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY:
455 texIndex = TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX;
456 break;
457 default:
458 return NULL;
459 }
460
461 texImage = ctx->Texture.ProxyTex[texIndex]->Image[0][level];
462 if (!texImage) {
463 texImage = ctx->Driver.NewTextureImage(ctx);
464 if (!texImage) {
465 _mesa_error(ctx, GL_OUT_OF_MEMORY, "proxy texture allocation");
466 return NULL;
467 }
468 ctx->Texture.ProxyTex[texIndex]->Image[0][level] = texImage;
469 /* Set the 'back' pointer */
470 texImage->TexObject = ctx->Texture.ProxyTex[texIndex];
471 }
472 return texImage;
473 }
474
475
476 /**
477 * Get the maximum number of allowed mipmap levels.
478 *
479 * \param ctx GL context.
480 * \param target texture target.
481 *
482 * \return the maximum number of allowed mipmap levels for the given
483 * texture target, or zero if passed a bad target.
484 *
485 * \sa gl_constants.
486 */
487 GLint
488 _mesa_max_texture_levels(const struct gl_context *ctx, GLenum target)
489 {
490 switch (target) {
491 case GL_TEXTURE_1D:
492 case GL_PROXY_TEXTURE_1D:
493 case GL_TEXTURE_2D:
494 case GL_PROXY_TEXTURE_2D:
495 return ffs(util_next_power_of_two(ctx->Const.MaxTextureSize));
496 case GL_TEXTURE_3D:
497 case GL_PROXY_TEXTURE_3D:
498 return ctx->Const.Max3DTextureLevels;
499 case GL_TEXTURE_CUBE_MAP:
500 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
501 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
502 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
503 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
504 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
505 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
506 case GL_PROXY_TEXTURE_CUBE_MAP:
507 return ctx->Extensions.ARB_texture_cube_map
508 ? ctx->Const.MaxCubeTextureLevels : 0;
509 case GL_TEXTURE_RECTANGLE_NV:
510 case GL_PROXY_TEXTURE_RECTANGLE_NV:
511 return ctx->Extensions.NV_texture_rectangle ? 1 : 0;
512 case GL_TEXTURE_1D_ARRAY_EXT:
513 case GL_PROXY_TEXTURE_1D_ARRAY_EXT:
514 case GL_TEXTURE_2D_ARRAY_EXT:
515 case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
516 return ctx->Extensions.EXT_texture_array
517 ? ffs(util_next_power_of_two(ctx->Const.MaxTextureSize)) : 0;
518 case GL_TEXTURE_CUBE_MAP_ARRAY:
519 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
520 return _mesa_has_texture_cube_map_array(ctx)
521 ? ctx->Const.MaxCubeTextureLevels : 0;
522 case GL_TEXTURE_BUFFER:
523 return (_mesa_has_ARB_texture_buffer_object(ctx) ||
524 _mesa_has_OES_texture_buffer(ctx)) ? 1 : 0;
525 case GL_TEXTURE_2D_MULTISAMPLE:
526 case GL_PROXY_TEXTURE_2D_MULTISAMPLE:
527 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
528 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY:
529 return (_mesa_is_desktop_gl(ctx) || _mesa_is_gles31(ctx))
530 && ctx->Extensions.ARB_texture_multisample
531 ? 1 : 0;
532 case GL_TEXTURE_EXTERNAL_OES:
533 return _mesa_has_OES_EGL_image_external(ctx) ? 1 : 0;
534 default:
535 return 0; /* bad target */
536 }
537 }
538
539
540 /**
541 * Return number of dimensions per mipmap level for the given texture target.
542 */
543 GLint
544 _mesa_get_texture_dimensions(GLenum target)
545 {
546 switch (target) {
547 case GL_TEXTURE_1D:
548 case GL_PROXY_TEXTURE_1D:
549 return 1;
550 case GL_TEXTURE_2D:
551 case GL_TEXTURE_RECTANGLE:
552 case GL_TEXTURE_CUBE_MAP:
553 case GL_PROXY_TEXTURE_2D:
554 case GL_PROXY_TEXTURE_RECTANGLE:
555 case GL_PROXY_TEXTURE_CUBE_MAP:
556 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
557 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
558 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
559 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
560 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
561 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
562 case GL_TEXTURE_1D_ARRAY:
563 case GL_PROXY_TEXTURE_1D_ARRAY:
564 case GL_TEXTURE_EXTERNAL_OES:
565 case GL_TEXTURE_2D_MULTISAMPLE:
566 case GL_PROXY_TEXTURE_2D_MULTISAMPLE:
567 return 2;
568 case GL_TEXTURE_3D:
569 case GL_PROXY_TEXTURE_3D:
570 case GL_TEXTURE_2D_ARRAY:
571 case GL_PROXY_TEXTURE_2D_ARRAY:
572 case GL_TEXTURE_CUBE_MAP_ARRAY:
573 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
574 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
575 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY:
576 return 3;
577 case GL_TEXTURE_BUFFER:
578 /* fall-through */
579 default:
580 _mesa_problem(NULL, "invalid target 0x%x in get_texture_dimensions()",
581 target);
582 return 2;
583 }
584 }
585
586
587 /**
588 * Check if a texture target can have more than one layer.
589 */
590 GLboolean
591 _mesa_tex_target_is_layered(GLenum target)
592 {
593 switch (target) {
594 case GL_TEXTURE_1D:
595 case GL_PROXY_TEXTURE_1D:
596 case GL_TEXTURE_2D:
597 case GL_PROXY_TEXTURE_2D:
598 case GL_TEXTURE_RECTANGLE:
599 case GL_PROXY_TEXTURE_RECTANGLE:
600 case GL_TEXTURE_2D_MULTISAMPLE:
601 case GL_PROXY_TEXTURE_2D_MULTISAMPLE:
602 case GL_TEXTURE_BUFFER:
603 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
604 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
605 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
606 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
607 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
608 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
609 case GL_TEXTURE_EXTERNAL_OES:
610 return GL_FALSE;
611
612 case GL_TEXTURE_3D:
613 case GL_PROXY_TEXTURE_3D:
614 case GL_TEXTURE_CUBE_MAP:
615 case GL_PROXY_TEXTURE_CUBE_MAP:
616 case GL_TEXTURE_1D_ARRAY:
617 case GL_PROXY_TEXTURE_1D_ARRAY:
618 case GL_TEXTURE_2D_ARRAY:
619 case GL_PROXY_TEXTURE_2D_ARRAY:
620 case GL_TEXTURE_CUBE_MAP_ARRAY:
621 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
622 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
623 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY:
624 return GL_TRUE;
625
626 default:
627 assert(!"Invalid texture target.");
628 return GL_FALSE;
629 }
630 }
631
632
633 /**
634 * Return the number of layers present in the given level of an array,
635 * cubemap or 3D texture. If the texture is not layered return zero.
636 */
637 GLuint
638 _mesa_get_texture_layers(const struct gl_texture_object *texObj, GLint level)
639 {
640 assert(level >= 0 && level < MAX_TEXTURE_LEVELS);
641
642 switch (texObj->Target) {
643 case GL_TEXTURE_1D:
644 case GL_TEXTURE_2D:
645 case GL_TEXTURE_RECTANGLE:
646 case GL_TEXTURE_2D_MULTISAMPLE:
647 case GL_TEXTURE_BUFFER:
648 case GL_TEXTURE_EXTERNAL_OES:
649 return 0;
650
651 case GL_TEXTURE_CUBE_MAP:
652 return 6;
653
654 case GL_TEXTURE_1D_ARRAY: {
655 struct gl_texture_image *img = texObj->Image[0][level];
656 return img ? img->Height : 0;
657 }
658
659 case GL_TEXTURE_3D:
660 case GL_TEXTURE_2D_ARRAY:
661 case GL_TEXTURE_CUBE_MAP_ARRAY:
662 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY: {
663 struct gl_texture_image *img = texObj->Image[0][level];
664 return img ? img->Depth : 0;
665 }
666
667 default:
668 assert(!"Invalid texture target.");
669 return 0;
670 }
671 }
672
673
674 /**
675 * Return the maximum number of mipmap levels for the given target
676 * and the dimensions.
677 * The dimensions are expected not to include the border.
678 */
679 GLsizei
680 _mesa_get_tex_max_num_levels(GLenum target, GLsizei width, GLsizei height,
681 GLsizei depth)
682 {
683 GLsizei size;
684
685 switch (target) {
686 case GL_TEXTURE_1D:
687 case GL_TEXTURE_1D_ARRAY:
688 case GL_PROXY_TEXTURE_1D:
689 case GL_PROXY_TEXTURE_1D_ARRAY:
690 size = width;
691 break;
692 case GL_TEXTURE_CUBE_MAP:
693 case GL_TEXTURE_CUBE_MAP_ARRAY:
694 case GL_PROXY_TEXTURE_CUBE_MAP:
695 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
696 size = width;
697 break;
698 case GL_TEXTURE_2D:
699 case GL_TEXTURE_2D_ARRAY:
700 case GL_PROXY_TEXTURE_2D:
701 case GL_PROXY_TEXTURE_2D_ARRAY:
702 size = MAX2(width, height);
703 break;
704 case GL_TEXTURE_3D:
705 case GL_PROXY_TEXTURE_3D:
706 size = MAX3(width, height, depth);
707 break;
708 case GL_TEXTURE_RECTANGLE:
709 case GL_TEXTURE_EXTERNAL_OES:
710 case GL_TEXTURE_2D_MULTISAMPLE:
711 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
712 case GL_PROXY_TEXTURE_RECTANGLE:
713 case GL_PROXY_TEXTURE_2D_MULTISAMPLE:
714 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY:
715 return 1;
716 default:
717 assert(0);
718 return 1;
719 }
720
721 return util_logbase2(size) + 1;
722 }
723
724
725 #if 000 /* not used anymore */
726 /*
727 * glTexImage[123]D can accept a NULL image pointer. In this case we
728 * create a texture image with unspecified image contents per the OpenGL
729 * spec.
730 */
731 static GLubyte *
732 make_null_texture(GLint width, GLint height, GLint depth, GLenum format)
733 {
734 const GLint components = _mesa_components_in_format(format);
735 const GLint numPixels = width * height * depth;
736 GLubyte *data = (GLubyte *) malloc(numPixels * components * sizeof(GLubyte));
737
738 #ifdef DEBUG
739 /*
740 * Let's see if anyone finds this. If glTexImage2D() is called with
741 * a NULL image pointer then load the texture image with something
742 * interesting instead of leaving it indeterminate.
743 */
744 if (data) {
745 static const char message[8][32] = {
746 " X X XXXXX XXX X ",
747 " XX XX X X X X X ",
748 " X X X X X X X ",
749 " X X XXXX XXX XXXXX ",
750 " X X X X X X ",
751 " X X X X X X X ",
752 " X X XXXXX XXX X X ",
753 " "
754 };
755
756 GLubyte *imgPtr = data;
757 GLint h, i, j, k;
758 for (h = 0; h < depth; h++) {
759 for (i = 0; i < height; i++) {
760 GLint srcRow = 7 - (i % 8);
761 for (j = 0; j < width; j++) {
762 GLint srcCol = j % 32;
763 GLubyte texel = (message[srcRow][srcCol]=='X') ? 255 : 70;
764 for (k = 0; k < components; k++) {
765 *imgPtr++ = texel;
766 }
767 }
768 }
769 }
770 }
771 #endif
772
773 return data;
774 }
775 #endif
776
777
778
779 /**
780 * Set the size and format-related fields of a gl_texture_image struct
781 * to zero. This is used when a proxy texture test fails.
782 */
783 static void
784 clear_teximage_fields(struct gl_texture_image *img)
785 {
786 assert(img);
787 img->_BaseFormat = 0;
788 img->InternalFormat = 0;
789 img->Border = 0;
790 img->Width = 0;
791 img->Height = 0;
792 img->Depth = 0;
793 img->Width2 = 0;
794 img->Height2 = 0;
795 img->Depth2 = 0;
796 img->WidthLog2 = 0;
797 img->HeightLog2 = 0;
798 img->DepthLog2 = 0;
799 img->TexFormat = MESA_FORMAT_NONE;
800 img->NumSamples = 0;
801 img->FixedSampleLocations = GL_TRUE;
802 }
803
804
805 /**
806 * Initialize basic fields of the gl_texture_image struct.
807 *
808 * \param ctx GL context.
809 * \param img texture image structure to be initialized.
810 * \param width image width.
811 * \param height image height.
812 * \param depth image depth.
813 * \param border image border.
814 * \param internalFormat internal format.
815 * \param format the actual hardware format (one of MESA_FORMAT_*)
816 * \param numSamples number of samples per texel, or zero for non-MS.
817 * \param fixedSampleLocations are sample locations fixed?
818 *
819 * Fills in the fields of \p img with the given information.
820 * Note: width, height and depth include the border.
821 */
822 void
823 _mesa_init_teximage_fields_ms(struct gl_context *ctx,
824 struct gl_texture_image *img,
825 GLsizei width, GLsizei height, GLsizei depth,
826 GLint border, GLenum internalFormat,
827 mesa_format format,
828 GLuint numSamples, GLboolean fixedSampleLocations)
829 {
830 const GLint base_format =_mesa_base_tex_format(ctx, internalFormat);
831 GLenum target;
832 assert(img);
833 assert(width >= 0);
834 assert(height >= 0);
835 assert(depth >= 0);
836
837 target = img->TexObject->Target;
838 assert(base_format != -1);
839 img->_BaseFormat = (GLenum16)base_format;
840 img->InternalFormat = internalFormat;
841 img->Border = border;
842 img->Width = width;
843 img->Height = height;
844 img->Depth = depth;
845
846 img->Width2 = width - 2 * border; /* == 1 << img->WidthLog2; */
847 img->WidthLog2 = util_logbase2(img->Width2);
848
849 switch(target) {
850 case GL_TEXTURE_1D:
851 case GL_TEXTURE_BUFFER:
852 case GL_PROXY_TEXTURE_1D:
853 if (height == 0)
854 img->Height2 = 0;
855 else
856 img->Height2 = 1;
857 img->HeightLog2 = 0;
858 if (depth == 0)
859 img->Depth2 = 0;
860 else
861 img->Depth2 = 1;
862 img->DepthLog2 = 0;
863 break;
864 case GL_TEXTURE_1D_ARRAY:
865 case GL_PROXY_TEXTURE_1D_ARRAY:
866 img->Height2 = height; /* no border */
867 img->HeightLog2 = 0; /* not used */
868 if (depth == 0)
869 img->Depth2 = 0;
870 else
871 img->Depth2 = 1;
872 img->DepthLog2 = 0;
873 break;
874 case GL_TEXTURE_2D:
875 case GL_TEXTURE_RECTANGLE:
876 case GL_TEXTURE_CUBE_MAP:
877 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
878 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
879 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
880 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
881 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
882 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
883 case GL_TEXTURE_EXTERNAL_OES:
884 case GL_PROXY_TEXTURE_2D:
885 case GL_PROXY_TEXTURE_RECTANGLE:
886 case GL_PROXY_TEXTURE_CUBE_MAP:
887 case GL_TEXTURE_2D_MULTISAMPLE:
888 case GL_PROXY_TEXTURE_2D_MULTISAMPLE:
889 img->Height2 = height - 2 * border; /* == 1 << img->HeightLog2; */
890 img->HeightLog2 = util_logbase2(img->Height2);
891 if (depth == 0)
892 img->Depth2 = 0;
893 else
894 img->Depth2 = 1;
895 img->DepthLog2 = 0;
896 break;
897 case GL_TEXTURE_2D_ARRAY:
898 case GL_PROXY_TEXTURE_2D_ARRAY:
899 case GL_TEXTURE_CUBE_MAP_ARRAY:
900 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
901 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
902 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY:
903 img->Height2 = height - 2 * border; /* == 1 << img->HeightLog2; */
904 img->HeightLog2 = util_logbase2(img->Height2);
905 img->Depth2 = depth; /* no border */
906 img->DepthLog2 = 0; /* not used */
907 break;
908 case GL_TEXTURE_3D:
909 case GL_PROXY_TEXTURE_3D:
910 img->Height2 = height - 2 * border; /* == 1 << img->HeightLog2; */
911 img->HeightLog2 = util_logbase2(img->Height2);
912 img->Depth2 = depth - 2 * border; /* == 1 << img->DepthLog2; */
913 img->DepthLog2 = util_logbase2(img->Depth2);
914 break;
915 default:
916 _mesa_problem(NULL, "invalid target 0x%x in _mesa_init_teximage_fields()",
917 target);
918 }
919
920 img->MaxNumLevels =
921 _mesa_get_tex_max_num_levels(target,
922 img->Width2, img->Height2, img->Depth2);
923 img->TexFormat = format;
924 img->NumSamples = numSamples;
925 img->FixedSampleLocations = fixedSampleLocations;
926 }
927
928
929 void
930 _mesa_init_teximage_fields(struct gl_context *ctx,
931 struct gl_texture_image *img,
932 GLsizei width, GLsizei height, GLsizei depth,
933 GLint border, GLenum internalFormat,
934 mesa_format format)
935 {
936 _mesa_init_teximage_fields_ms(ctx, img, width, height, depth, border,
937 internalFormat, format, 0, GL_TRUE);
938 }
939
940
941 /**
942 * Free and clear fields of the gl_texture_image struct.
943 *
944 * \param ctx GL context.
945 * \param texImage texture image structure to be cleared.
946 *
947 * After the call, \p texImage will have no data associated with it. Its
948 * fields are cleared so that its parent object will test incomplete.
949 */
950 void
951 _mesa_clear_texture_image(struct gl_context *ctx,
952 struct gl_texture_image *texImage)
953 {
954 ctx->Driver.FreeTextureImageBuffer(ctx, texImage);
955 clear_teximage_fields(texImage);
956 }
957
958
959 /**
960 * Check the width, height, depth and border of a texture image are legal.
961 * Used by all the glTexImage, glCompressedTexImage and glCopyTexImage
962 * functions.
963 * The target and level parameters will have already been validated.
964 * \return GL_TRUE if size is OK, GL_FALSE otherwise.
965 */
966 GLboolean
967 _mesa_legal_texture_dimensions(struct gl_context *ctx, GLenum target,
968 GLint level, GLint width, GLint height,
969 GLint depth, GLint border)
970 {
971 GLint maxSize;
972
973 switch (target) {
974 case GL_TEXTURE_1D:
975 case GL_PROXY_TEXTURE_1D:
976 maxSize = ctx->Const.MaxTextureSize >> level;
977 if (width < 2 * border || width > 2 * border + maxSize)
978 return GL_FALSE;
979 if (!ctx->Extensions.ARB_texture_non_power_of_two) {
980 if (width > 0 && !util_is_power_of_two_nonzero(width - 2 * border))
981 return GL_FALSE;
982 }
983 return GL_TRUE;
984
985 case GL_TEXTURE_2D:
986 case GL_PROXY_TEXTURE_2D:
987 case GL_TEXTURE_2D_MULTISAMPLE:
988 case GL_PROXY_TEXTURE_2D_MULTISAMPLE:
989 maxSize = ctx->Const.MaxTextureSize >> level;
990 if (width < 2 * border || width > 2 * border + maxSize)
991 return GL_FALSE;
992 if (height < 2 * border || height > 2 * border + maxSize)
993 return GL_FALSE;
994 if (!ctx->Extensions.ARB_texture_non_power_of_two) {
995 if (width > 0 && !util_is_power_of_two_nonzero(width - 2 * border))
996 return GL_FALSE;
997 if (height > 0 && !util_is_power_of_two_nonzero(height - 2 * border))
998 return GL_FALSE;
999 }
1000 return GL_TRUE;
1001
1002 case GL_TEXTURE_3D:
1003 case GL_PROXY_TEXTURE_3D:
1004 maxSize = 1 << (ctx->Const.Max3DTextureLevels - 1);
1005 maxSize >>= level;
1006 if (width < 2 * border || width > 2 * border + maxSize)
1007 return GL_FALSE;
1008 if (height < 2 * border || height > 2 * border + maxSize)
1009 return GL_FALSE;
1010 if (depth < 2 * border || depth > 2 * border + maxSize)
1011 return GL_FALSE;
1012 if (!ctx->Extensions.ARB_texture_non_power_of_two) {
1013 if (width > 0 && !util_is_power_of_two_nonzero(width - 2 * border))
1014 return GL_FALSE;
1015 if (height > 0 && !util_is_power_of_two_nonzero(height - 2 * border))
1016 return GL_FALSE;
1017 if (depth > 0 && !util_is_power_of_two_nonzero(depth - 2 * border))
1018 return GL_FALSE;
1019 }
1020 return GL_TRUE;
1021
1022 case GL_TEXTURE_RECTANGLE_NV:
1023 case GL_PROXY_TEXTURE_RECTANGLE_NV:
1024 if (level != 0)
1025 return GL_FALSE;
1026 maxSize = ctx->Const.MaxTextureRectSize;
1027 if (width < 0 || width > maxSize)
1028 return GL_FALSE;
1029 if (height < 0 || height > maxSize)
1030 return GL_FALSE;
1031 return GL_TRUE;
1032
1033 case GL_TEXTURE_CUBE_MAP:
1034 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
1035 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
1036 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
1037 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
1038 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
1039 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
1040 case GL_PROXY_TEXTURE_CUBE_MAP:
1041 maxSize = 1 << (ctx->Const.MaxCubeTextureLevels - 1);
1042 maxSize >>= level;
1043 if (width != height)
1044 return GL_FALSE;
1045 if (width < 2 * border || width > 2 * border + maxSize)
1046 return GL_FALSE;
1047 if (height < 2 * border || height > 2 * border + maxSize)
1048 return GL_FALSE;
1049 if (!ctx->Extensions.ARB_texture_non_power_of_two) {
1050 if (width > 0 && !util_is_power_of_two_nonzero(width - 2 * border))
1051 return GL_FALSE;
1052 if (height > 0 && !util_is_power_of_two_nonzero(height - 2 * border))
1053 return GL_FALSE;
1054 }
1055 return GL_TRUE;
1056
1057 case GL_TEXTURE_1D_ARRAY_EXT:
1058 case GL_PROXY_TEXTURE_1D_ARRAY_EXT:
1059 maxSize = ctx->Const.MaxTextureSize >> level;
1060 if (width < 2 * border || width > 2 * border + maxSize)
1061 return GL_FALSE;
1062 if (height < 0 || height > ctx->Const.MaxArrayTextureLayers)
1063 return GL_FALSE;
1064 if (!ctx->Extensions.ARB_texture_non_power_of_two) {
1065 if (width > 0 && !util_is_power_of_two_nonzero(width - 2 * border))
1066 return GL_FALSE;
1067 }
1068 return GL_TRUE;
1069
1070 case GL_TEXTURE_2D_ARRAY_EXT:
1071 case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
1072 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
1073 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY:
1074 maxSize = ctx->Const.MaxTextureSize >> level;
1075 if (width < 2 * border || width > 2 * border + maxSize)
1076 return GL_FALSE;
1077 if (height < 2 * border || height > 2 * border + maxSize)
1078 return GL_FALSE;
1079 if (depth < 0 || depth > ctx->Const.MaxArrayTextureLayers)
1080 return GL_FALSE;
1081 if (!ctx->Extensions.ARB_texture_non_power_of_two) {
1082 if (width > 0 && !util_is_power_of_two_nonzero(width - 2 * border))
1083 return GL_FALSE;
1084 if (height > 0 && !util_is_power_of_two_nonzero(height - 2 * border))
1085 return GL_FALSE;
1086 }
1087 return GL_TRUE;
1088
1089 case GL_TEXTURE_CUBE_MAP_ARRAY:
1090 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
1091 maxSize = 1 << (ctx->Const.MaxCubeTextureLevels - 1);
1092 if (width < 2 * border || width > 2 * border + maxSize)
1093 return GL_FALSE;
1094 if (height < 2 * border || height > 2 * border + maxSize)
1095 return GL_FALSE;
1096 if (depth < 0 || depth > ctx->Const.MaxArrayTextureLayers || depth % 6)
1097 return GL_FALSE;
1098 if (width != height)
1099 return GL_FALSE;
1100 if (level >= ctx->Const.MaxCubeTextureLevels)
1101 return GL_FALSE;
1102 if (!ctx->Extensions.ARB_texture_non_power_of_two) {
1103 if (width > 0 && !util_is_power_of_two_nonzero(width - 2 * border))
1104 return GL_FALSE;
1105 if (height > 0 && !util_is_power_of_two_nonzero(height - 2 * border))
1106 return GL_FALSE;
1107 }
1108 return GL_TRUE;
1109 default:
1110 _mesa_problem(ctx, "Invalid target in _mesa_legal_texture_dimensions()");
1111 return GL_FALSE;
1112 }
1113 }
1114
1115 static bool
1116 error_check_subtexture_negative_dimensions(struct gl_context *ctx,
1117 GLuint dims,
1118 GLsizei subWidth,
1119 GLsizei subHeight,
1120 GLsizei subDepth,
1121 const char *func)
1122 {
1123 /* Check size */
1124 if (subWidth < 0) {
1125 _mesa_error(ctx, GL_INVALID_VALUE, "%s(width=%d)", func, subWidth);
1126 return true;
1127 }
1128
1129 if (dims > 1 && subHeight < 0) {
1130 _mesa_error(ctx, GL_INVALID_VALUE, "%s(height=%d)", func, subHeight);
1131 return true;
1132 }
1133
1134 if (dims > 2 && subDepth < 0) {
1135 _mesa_error(ctx, GL_INVALID_VALUE, "%s(depth=%d)", func, subDepth);
1136 return true;
1137 }
1138
1139 return false;
1140 }
1141
1142 /**
1143 * Do error checking of xoffset, yoffset, zoffset, width, height and depth
1144 * for glTexSubImage, glCopyTexSubImage and glCompressedTexSubImage.
1145 * \param destImage the destination texture image.
1146 * \return GL_TRUE if error found, GL_FALSE otherwise.
1147 */
1148 static GLboolean
1149 error_check_subtexture_dimensions(struct gl_context *ctx, GLuint dims,
1150 const struct gl_texture_image *destImage,
1151 GLint xoffset, GLint yoffset, GLint zoffset,
1152 GLsizei subWidth, GLsizei subHeight,
1153 GLsizei subDepth, const char *func)
1154 {
1155 const GLenum target = destImage->TexObject->Target;
1156 GLuint bw, bh, bd;
1157
1158 /* check xoffset and width */
1159 if (xoffset < - (GLint) destImage->Border) {
1160 _mesa_error(ctx, GL_INVALID_VALUE, "%s(xoffset)", func);
1161 return GL_TRUE;
1162 }
1163
1164 if (xoffset + subWidth > (GLint) destImage->Width) {
1165 _mesa_error(ctx, GL_INVALID_VALUE, "%s(xoffset %d + width %d > %u)", func,
1166 xoffset, subWidth, destImage->Width);
1167 return GL_TRUE;
1168 }
1169
1170 /* check yoffset and height */
1171 if (dims > 1) {
1172 GLint yBorder = (target == GL_TEXTURE_1D_ARRAY) ? 0 : destImage->Border;
1173 if (yoffset < -yBorder) {
1174 _mesa_error(ctx, GL_INVALID_VALUE, "%s(yoffset)", func);
1175 return GL_TRUE;
1176 }
1177 if (yoffset + subHeight > (GLint) destImage->Height) {
1178 _mesa_error(ctx, GL_INVALID_VALUE, "%s(yoffset %d + height %d > %u)",
1179 func, yoffset, subHeight, destImage->Height);
1180 return GL_TRUE;
1181 }
1182 }
1183
1184 /* check zoffset and depth */
1185 if (dims > 2) {
1186 GLint depth;
1187 GLint zBorder = (target == GL_TEXTURE_2D_ARRAY ||
1188 target == GL_TEXTURE_CUBE_MAP_ARRAY) ?
1189 0 : destImage->Border;
1190
1191 if (zoffset < -zBorder) {
1192 _mesa_error(ctx, GL_INVALID_VALUE, "%s(zoffset)", func);
1193 return GL_TRUE;
1194 }
1195
1196 depth = (GLint) destImage->Depth;
1197 if (target == GL_TEXTURE_CUBE_MAP)
1198 depth = 6;
1199 if (zoffset + subDepth > depth) {
1200 _mesa_error(ctx, GL_INVALID_VALUE, "%s(zoffset %d + depth %d > %u)",
1201 func, zoffset, subDepth, depth);
1202 return GL_TRUE;
1203 }
1204 }
1205
1206 /*
1207 * The OpenGL spec (and GL_ARB_texture_compression) says only whole
1208 * compressed texture images can be updated. But, that restriction may be
1209 * relaxed for particular compressed formats. At this time, all the
1210 * compressed formats supported by Mesa allow sub-textures to be updated
1211 * along compressed block boundaries.
1212 */
1213 _mesa_get_format_block_size_3d(destImage->TexFormat, &bw, &bh, &bd);
1214
1215 if (bw != 1 || bh != 1 || bd != 1) {
1216 /* offset must be multiple of block size */
1217 if ((xoffset % bw != 0) || (yoffset % bh != 0) || (zoffset % bd != 0)) {
1218 _mesa_error(ctx, GL_INVALID_OPERATION,
1219 "%s(xoffset = %d, yoffset = %d, zoffset = %d)",
1220 func, xoffset, yoffset, zoffset);
1221 return GL_TRUE;
1222 }
1223
1224 /* The size must be a multiple of bw x bh, or we must be using a
1225 * offset+size that exactly hits the edge of the image. This
1226 * is important for small mipmap levels (1x1, 2x1, etc) and for
1227 * NPOT textures.
1228 */
1229 if ((subWidth % bw != 0) &&
1230 (xoffset + subWidth != (GLint) destImage->Width)) {
1231 _mesa_error(ctx, GL_INVALID_OPERATION,
1232 "%s(width = %d)", func, subWidth);
1233 return GL_TRUE;
1234 }
1235
1236 if ((subHeight % bh != 0) &&
1237 (yoffset + subHeight != (GLint) destImage->Height)) {
1238 _mesa_error(ctx, GL_INVALID_OPERATION,
1239 "%s(height = %d)", func, subHeight);
1240 return GL_TRUE;
1241 }
1242
1243 if ((subDepth % bd != 0) &&
1244 (zoffset + subDepth != (GLint) destImage->Depth)) {
1245 _mesa_error(ctx, GL_INVALID_OPERATION,
1246 "%s(depth = %d)", func, subDepth);
1247 return GL_TRUE;
1248 }
1249 }
1250
1251 return GL_FALSE;
1252 }
1253
1254
1255
1256
1257 /**
1258 * This is the fallback for Driver.TestProxyTexImage() for doing device-
1259 * specific texture image size checks.
1260 *
1261 * A hardware driver might override this function if, for example, the
1262 * max 3D texture size is 512x512x64 (i.e. not a cube).
1263 *
1264 * Note that width, height, depth == 0 is not an error. However, a
1265 * texture with zero width/height/depth will be considered "incomplete"
1266 * and texturing will effectively be disabled.
1267 *
1268 * \param target any texture target/type
1269 * \param numLevels number of mipmap levels in the texture or 0 if not known
1270 * \param level as passed to glTexImage
1271 * \param format the MESA_FORMAT_x for the tex image
1272 * \param numSamples number of samples per texel
1273 * \param width as passed to glTexImage
1274 * \param height as passed to glTexImage
1275 * \param depth as passed to glTexImage
1276 * \return GL_TRUE if the image is acceptable, GL_FALSE if not acceptable.
1277 */
1278 GLboolean
1279 _mesa_test_proxy_teximage(struct gl_context *ctx, GLenum target,
1280 GLuint numLevels, ASSERTED GLint level,
1281 mesa_format format, GLuint numSamples,
1282 GLint width, GLint height, GLint depth)
1283 {
1284 uint64_t bytes, mbytes;
1285
1286 if (numLevels > 0) {
1287 /* Compute total memory for a whole mipmap. This is the path
1288 * taken for glTexStorage(GL_PROXY_TEXTURE_x).
1289 */
1290 unsigned l;
1291
1292 assert(level == 0);
1293
1294 bytes = 0;
1295
1296 for (l = 0; l < numLevels; l++) {
1297 GLint nextWidth, nextHeight, nextDepth;
1298
1299 bytes += _mesa_format_image_size64(format, width, height, depth);
1300
1301 if (_mesa_next_mipmap_level_size(target, 0, width, height, depth,
1302 &nextWidth, &nextHeight,
1303 &nextDepth)) {
1304 width = nextWidth;
1305 height = nextHeight;
1306 depth = nextDepth;
1307 } else {
1308 break;
1309 }
1310 }
1311 } else {
1312 /* We just compute the size of one mipmap level. This is the path
1313 * taken for glTexImage(GL_PROXY_TEXTURE_x).
1314 */
1315 bytes = _mesa_format_image_size64(format, width, height, depth);
1316 }
1317
1318 bytes *= _mesa_num_tex_faces(target);
1319 bytes *= MAX2(1, numSamples);
1320
1321 mbytes = bytes / (1024 * 1024); /* convert to MB */
1322
1323 /* We just check if the image size is less than MaxTextureMbytes.
1324 * Some drivers may do more specific checks.
1325 */
1326 return mbytes <= (uint64_t) ctx->Const.MaxTextureMbytes;
1327 }
1328
1329
1330 /**
1331 * Return true if the format is only valid for glCompressedTexImage.
1332 */
1333 static bool
1334 compressedteximage_only_format(GLenum format)
1335 {
1336 switch (format) {
1337 case GL_PALETTE4_RGB8_OES:
1338 case GL_PALETTE4_RGBA8_OES:
1339 case GL_PALETTE4_R5_G6_B5_OES:
1340 case GL_PALETTE4_RGBA4_OES:
1341 case GL_PALETTE4_RGB5_A1_OES:
1342 case GL_PALETTE8_RGB8_OES:
1343 case GL_PALETTE8_RGBA8_OES:
1344 case GL_PALETTE8_R5_G6_B5_OES:
1345 case GL_PALETTE8_RGBA4_OES:
1346 case GL_PALETTE8_RGB5_A1_OES:
1347 case GL_ATC_RGB_AMD:
1348 case GL_ATC_RGBA_EXPLICIT_ALPHA_AMD:
1349 case GL_ATC_RGBA_INTERPOLATED_ALPHA_AMD:
1350 return true;
1351 default:
1352 return false;
1353 }
1354 }
1355
1356 /**
1357 * Return true if the format doesn't support online compression.
1358 */
1359 bool
1360 _mesa_format_no_online_compression(GLenum format)
1361 {
1362 return _mesa_is_astc_format(format) ||
1363 _mesa_is_etc2_format(format) ||
1364 compressedteximage_only_format(format);
1365 }
1366
1367 /* Writes to an GL error pointer if non-null and returns whether or not the
1368 * error is GL_NO_ERROR */
1369 static bool
1370 write_error(GLenum *err_ptr, GLenum error)
1371 {
1372 if (err_ptr)
1373 *err_ptr = error;
1374
1375 return error == GL_NO_ERROR;
1376 }
1377
1378 /**
1379 * Helper function to determine whether a target and specific compression
1380 * format are supported. The error parameter returns GL_NO_ERROR if the
1381 * target can be compressed. Otherwise it returns either GL_INVALID_OPERATION
1382 * or GL_INVALID_ENUM, whichever is more appropriate.
1383 */
1384 GLboolean
1385 _mesa_target_can_be_compressed(const struct gl_context *ctx, GLenum target,
1386 GLenum intFormat, GLenum *error)
1387 {
1388 GLboolean target_can_be_compresed = GL_FALSE;
1389 mesa_format format = _mesa_glenum_to_compressed_format(intFormat);
1390 enum mesa_format_layout layout = _mesa_get_format_layout(format);
1391
1392 switch (target) {
1393 case GL_TEXTURE_2D:
1394 case GL_PROXY_TEXTURE_2D:
1395 target_can_be_compresed = GL_TRUE; /* true for any compressed format so far */
1396 break;
1397 case GL_PROXY_TEXTURE_CUBE_MAP:
1398 case GL_TEXTURE_CUBE_MAP:
1399 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
1400 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
1401 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
1402 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
1403 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
1404 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
1405 target_can_be_compresed = ctx->Extensions.ARB_texture_cube_map;
1406 break;
1407 case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
1408 case GL_TEXTURE_2D_ARRAY_EXT:
1409 target_can_be_compresed = ctx->Extensions.EXT_texture_array;
1410 break;
1411 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
1412 case GL_TEXTURE_CUBE_MAP_ARRAY:
1413 /* From the KHR_texture_compression_astc_hdr spec:
1414 *
1415 * Add a second new column "3D Tex." which is empty for all non-ASTC
1416 * formats. If only the LDR profile is supported by the
1417 * implementation, this column is also empty for all ASTC formats. If
1418 * both the LDR and HDR profiles are supported only, this column is
1419 * checked for all ASTC formats.
1420 *
1421 * Add a third new column "Cube Map Array Tex." which is empty for all
1422 * non-ASTC formats, and checked for all ASTC formats.
1423 *
1424 * and,
1425 *
1426 * 'An INVALID_OPERATION error is generated by CompressedTexImage3D
1427 * if <internalformat> is TEXTURE_CUBE_MAP_ARRAY and the
1428 * "Cube Map Array" column of table 8.19 is *not* checked, or if
1429 * <internalformat> is TEXTURE_3D and the "3D Tex." column of table
1430 * 8.19 is *not* checked'
1431 *
1432 * The instances of <internalformat> above should say <target>.
1433 *
1434 * ETC2/EAC formats are the only alternative in GLES and thus such errors
1435 * have already been handled by normal ETC2/EAC behavior.
1436 */
1437
1438 /* From section 3.8.6, page 146 of OpenGL ES 3.0 spec:
1439 *
1440 * "The ETC2/EAC texture compression algorithm supports only
1441 * two-dimensional images. If internalformat is an ETC2/EAC format,
1442 * glCompressedTexImage3D will generate an INVALID_OPERATION error if
1443 * target is not TEXTURE_2D_ARRAY."
1444 *
1445 * This should also be applicable for glTexStorage3D(). Other available
1446 * targets for these functions are: TEXTURE_3D and TEXTURE_CUBE_MAP_ARRAY.
1447 *
1448 * Section 8.7, page 179 of OpenGL ES 3.2 adds:
1449 *
1450 * An INVALID_OPERATION error is generated by CompressedTexImage3D
1451 * if internalformat is one of the the formats in table 8.17 and target is
1452 * not TEXTURE_2D_ARRAY, TEXTURE_CUBE_MAP_ARRAY or TEXTURE_3D.
1453 *
1454 * An INVALID_OPERATION error is generated by CompressedTexImage3D
1455 * if internalformat is TEXTURE_CUBE_MAP_ARRAY and the “Cube Map
1456 * Array” column of table 8.17 is not checked, or if internalformat
1457 * is TEXTURE_- 3D and the “3D Tex.” column of table 8.17 is not
1458 * checked.
1459 *
1460 * The instances of <internalformat> above should say <target>.
1461 *
1462 * Such table 8.17 has checked "Cube Map Array" column for all the
1463 * cases. So in practice, TEXTURE_CUBE_MAP_ARRAY is now valid for OpenGL ES 3.2
1464 */
1465 if (layout == MESA_FORMAT_LAYOUT_ETC2 && _mesa_is_gles3(ctx) &&
1466 !_mesa_is_gles32(ctx))
1467 return write_error(error, GL_INVALID_OPERATION);
1468 target_can_be_compresed = _mesa_has_texture_cube_map_array(ctx);
1469 break;
1470 case GL_TEXTURE_3D:
1471 switch (layout) {
1472 case MESA_FORMAT_LAYOUT_ETC2:
1473 /* See ETC2/EAC comment in case GL_TEXTURE_CUBE_MAP_ARRAY. */
1474 if (_mesa_is_gles3(ctx))
1475 return write_error(error, GL_INVALID_OPERATION);
1476 break;
1477 case MESA_FORMAT_LAYOUT_BPTC:
1478 target_can_be_compresed = ctx->Extensions.ARB_texture_compression_bptc;
1479 break;
1480 case MESA_FORMAT_LAYOUT_ASTC:
1481 target_can_be_compresed =
1482 ctx->Extensions.KHR_texture_compression_astc_hdr ||
1483 ctx->Extensions.KHR_texture_compression_astc_sliced_3d;
1484
1485 /* Throw an INVALID_OPERATION error if the target is TEXTURE_3D and
1486 * neither of the above extensions are supported. See comment in
1487 * switch case GL_TEXTURE_CUBE_MAP_ARRAY for more info.
1488 */
1489 if (!target_can_be_compresed)
1490 return write_error(error, GL_INVALID_OPERATION);
1491 break;
1492 default:
1493 break;
1494 }
1495 default:
1496 break;
1497 }
1498 return write_error(error,
1499 target_can_be_compresed ? GL_NO_ERROR : GL_INVALID_ENUM);
1500 }
1501
1502
1503 /**
1504 * Check if the given texture target value is legal for a
1505 * glTexImage1/2/3D call.
1506 */
1507 static GLboolean
1508 legal_teximage_target(struct gl_context *ctx, GLuint dims, GLenum target)
1509 {
1510 switch (dims) {
1511 case 1:
1512 switch (target) {
1513 case GL_TEXTURE_1D:
1514 case GL_PROXY_TEXTURE_1D:
1515 return _mesa_is_desktop_gl(ctx);
1516 default:
1517 return GL_FALSE;
1518 }
1519 case 2:
1520 switch (target) {
1521 case GL_TEXTURE_2D:
1522 return GL_TRUE;
1523 case GL_PROXY_TEXTURE_2D:
1524 return _mesa_is_desktop_gl(ctx);
1525 case GL_PROXY_TEXTURE_CUBE_MAP:
1526 return _mesa_is_desktop_gl(ctx)
1527 && ctx->Extensions.ARB_texture_cube_map;
1528 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
1529 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
1530 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
1531 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
1532 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
1533 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
1534 return ctx->Extensions.ARB_texture_cube_map;
1535 case GL_TEXTURE_RECTANGLE_NV:
1536 case GL_PROXY_TEXTURE_RECTANGLE_NV:
1537 return _mesa_is_desktop_gl(ctx)
1538 && ctx->Extensions.NV_texture_rectangle;
1539 case GL_TEXTURE_1D_ARRAY_EXT:
1540 case GL_PROXY_TEXTURE_1D_ARRAY_EXT:
1541 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_array;
1542 default:
1543 return GL_FALSE;
1544 }
1545 case 3:
1546 switch (target) {
1547 case GL_TEXTURE_3D:
1548 return GL_TRUE;
1549 case GL_PROXY_TEXTURE_3D:
1550 return _mesa_is_desktop_gl(ctx);
1551 case GL_TEXTURE_2D_ARRAY_EXT:
1552 return (_mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_array)
1553 || _mesa_is_gles3(ctx);
1554 case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
1555 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_array;
1556 case GL_TEXTURE_CUBE_MAP_ARRAY:
1557 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
1558 return _mesa_has_texture_cube_map_array(ctx);
1559 default:
1560 return GL_FALSE;
1561 }
1562 default:
1563 _mesa_problem(ctx, "invalid dims=%u in legal_teximage_target()", dims);
1564 return GL_FALSE;
1565 }
1566 }
1567
1568
1569 /**
1570 * Check if the given texture target value is legal for a
1571 * glTexSubImage, glCopyTexSubImage or glCopyTexImage call.
1572 * The difference compared to legal_teximage_target() above is that
1573 * proxy targets are not supported.
1574 */
1575 static GLboolean
1576 legal_texsubimage_target(struct gl_context *ctx, GLuint dims, GLenum target,
1577 bool dsa)
1578 {
1579 switch (dims) {
1580 case 1:
1581 return _mesa_is_desktop_gl(ctx) && target == GL_TEXTURE_1D;
1582 case 2:
1583 switch (target) {
1584 case GL_TEXTURE_2D:
1585 return GL_TRUE;
1586 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
1587 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
1588 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
1589 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
1590 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
1591 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
1592 return ctx->Extensions.ARB_texture_cube_map;
1593 case GL_TEXTURE_RECTANGLE_NV:
1594 return _mesa_is_desktop_gl(ctx)
1595 && ctx->Extensions.NV_texture_rectangle;
1596 case GL_TEXTURE_1D_ARRAY_EXT:
1597 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_array;
1598 default:
1599 return GL_FALSE;
1600 }
1601 case 3:
1602 switch (target) {
1603 case GL_TEXTURE_3D:
1604 return GL_TRUE;
1605 case GL_TEXTURE_2D_ARRAY_EXT:
1606 return (_mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_array)
1607 || _mesa_is_gles3(ctx);
1608 case GL_TEXTURE_CUBE_MAP_ARRAY:
1609 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
1610 return _mesa_has_texture_cube_map_array(ctx);
1611
1612 /* Table 8.15 of the OpenGL 4.5 core profile spec
1613 * (20141030) says that TEXTURE_CUBE_MAP is valid for TextureSubImage3D
1614 * and CopyTextureSubImage3D.
1615 */
1616 case GL_TEXTURE_CUBE_MAP:
1617 return dsa;
1618 default:
1619 return GL_FALSE;
1620 }
1621 default:
1622 _mesa_problem(ctx, "invalid dims=%u in legal_texsubimage_target()",
1623 dims);
1624 return GL_FALSE;
1625 }
1626 }
1627
1628
1629 /**
1630 * Helper function to determine if a texture object is mutable (in terms
1631 * of GL_ARB_texture_storage/GL_ARB_bindless_texture).
1632 */
1633 static GLboolean
1634 mutable_tex_object(struct gl_texture_object *texObj)
1635 {
1636 if (!texObj)
1637 return GL_FALSE;
1638
1639 if (texObj->HandleAllocated) {
1640 /* The ARB_bindless_texture spec says:
1641 *
1642 * "The error INVALID_OPERATION is generated by TexImage*, CopyTexImage*,
1643 * CompressedTexImage*, TexBuffer*, TexParameter*, as well as other
1644 * functions defined in terms of these, if the texture object to be
1645 * modified is referenced by one or more texture or image handles."
1646 */
1647 return GL_FALSE;
1648 }
1649
1650 return !texObj->Immutable;
1651 }
1652
1653
1654 /**
1655 * Return expected size of a compressed texture.
1656 */
1657 static GLuint
1658 compressed_tex_size(GLsizei width, GLsizei height, GLsizei depth,
1659 GLenum glformat)
1660 {
1661 mesa_format mesaFormat = _mesa_glenum_to_compressed_format(glformat);
1662 return _mesa_format_image_size(mesaFormat, width, height, depth);
1663 }
1664
1665 /**
1666 * Verify that a texture format is valid with a particular target
1667 *
1668 * In particular, textures with base format of \c GL_DEPTH_COMPONENT or
1669 * \c GL_DEPTH_STENCIL are only valid with certain, context dependent texture
1670 * targets.
1671 *
1672 * \param ctx GL context
1673 * \param target Texture target
1674 * \param internalFormat Internal format of the texture image
1675 *
1676 * \returns true if the combination is legal, false otherwise.
1677 */
1678 bool
1679 _mesa_legal_texture_base_format_for_target(struct gl_context *ctx,
1680 GLenum target, GLenum internalFormat)
1681 {
1682 if (_mesa_base_tex_format(ctx, internalFormat) == GL_DEPTH_COMPONENT
1683 || _mesa_base_tex_format(ctx, internalFormat) == GL_DEPTH_STENCIL
1684 || _mesa_base_tex_format(ctx, internalFormat) == GL_STENCIL_INDEX) {
1685 /* Section 3.8.3 (Texture Image Specification) of the OpenGL 3.3 Core
1686 * Profile spec says:
1687 *
1688 * "Textures with a base internal format of DEPTH_COMPONENT or
1689 * DEPTH_STENCIL are supported by texture image specification
1690 * commands only if target is TEXTURE_1D, TEXTURE_2D,
1691 * TEXTURE_1D_ARRAY, TEXTURE_2D_ARRAY, TEXTURE_RECTANGLE,
1692 * TEXTURE_CUBE_MAP, PROXY_TEXTURE_1D, PROXY_TEXTURE_2D,
1693 * PROXY_TEXTURE_1D_ARRAY, PROXY_TEXTURE_2D_ARRAY,
1694 * PROXY_TEXTURE_RECTANGLE, or PROXY_TEXTURE_CUBE_MAP. Using these
1695 * formats in conjunction with any other target will result in an
1696 * INVALID_OPERATION error."
1697 *
1698 * Cubemaps are only supported with desktop OpenGL version >= 3.0,
1699 * EXT_gpu_shader4, or, on OpenGL ES 2.0+, OES_depth_texture_cube_map.
1700 */
1701 if (target != GL_TEXTURE_1D &&
1702 target != GL_PROXY_TEXTURE_1D &&
1703 target != GL_TEXTURE_2D &&
1704 target != GL_PROXY_TEXTURE_2D &&
1705 target != GL_TEXTURE_1D_ARRAY &&
1706 target != GL_PROXY_TEXTURE_1D_ARRAY &&
1707 target != GL_TEXTURE_2D_ARRAY &&
1708 target != GL_PROXY_TEXTURE_2D_ARRAY &&
1709 target != GL_TEXTURE_RECTANGLE_ARB &&
1710 target != GL_PROXY_TEXTURE_RECTANGLE_ARB &&
1711 !((_mesa_is_cube_face(target) ||
1712 target == GL_TEXTURE_CUBE_MAP ||
1713 target == GL_PROXY_TEXTURE_CUBE_MAP) &&
1714 (ctx->Version >= 30 || ctx->Extensions.EXT_gpu_shader4
1715 || (ctx->API == API_OPENGLES2 && ctx->Extensions.OES_depth_texture_cube_map))) &&
1716 !((target == GL_TEXTURE_CUBE_MAP_ARRAY ||
1717 target == GL_PROXY_TEXTURE_CUBE_MAP_ARRAY) &&
1718 _mesa_has_texture_cube_map_array(ctx))) {
1719 return false;
1720 }
1721 }
1722
1723 return true;
1724 }
1725
1726 static bool
1727 texture_formats_agree(GLenum internalFormat,
1728 GLenum format)
1729 {
1730 GLboolean colorFormat;
1731 GLboolean is_format_depth_or_depthstencil;
1732 GLboolean is_internalFormat_depth_or_depthstencil;
1733
1734 /* Even though there are no color-index textures, we still have to support
1735 * uploading color-index data and remapping it to RGB via the
1736 * GL_PIXEL_MAP_I_TO_[RGBA] tables.
1737 */
1738 const GLboolean indexFormat = (format == GL_COLOR_INDEX);
1739
1740 is_internalFormat_depth_or_depthstencil =
1741 _mesa_is_depth_format(internalFormat) ||
1742 _mesa_is_depthstencil_format(internalFormat);
1743
1744 is_format_depth_or_depthstencil =
1745 _mesa_is_depth_format(format) ||
1746 _mesa_is_depthstencil_format(format);
1747
1748 colorFormat = _mesa_is_color_format(format);
1749
1750 if (_mesa_is_color_format(internalFormat) && !colorFormat && !indexFormat)
1751 return false;
1752
1753 if (is_internalFormat_depth_or_depthstencil !=
1754 is_format_depth_or_depthstencil)
1755 return false;
1756
1757 if (_mesa_is_ycbcr_format(internalFormat) != _mesa_is_ycbcr_format(format))
1758 return false;
1759
1760 return true;
1761 }
1762
1763 /**
1764 * Test the combination of format, type and internal format arguments of
1765 * different texture operations on GLES.
1766 *
1767 * \param ctx GL context.
1768 * \param format pixel data format given by the user.
1769 * \param type pixel data type given by the user.
1770 * \param internalFormat internal format given by the user.
1771 * \param callerName name of the caller function to print in the error message
1772 *
1773 * \return true if a error is found, false otherwise
1774 *
1775 * Currently, it is used by texture_error_check() and texsubimage_error_check().
1776 */
1777 static bool
1778 texture_format_error_check_gles(struct gl_context *ctx, GLenum format,
1779 GLenum type, GLenum internalFormat, const char *callerName)
1780 {
1781 GLenum err = _mesa_gles_error_check_format_and_type(ctx, format, type,
1782 internalFormat);
1783 if (err != GL_NO_ERROR) {
1784 _mesa_error(ctx, err,
1785 "%s(format = %s, type = %s, internalformat = %s)",
1786 callerName, _mesa_enum_to_string(format),
1787 _mesa_enum_to_string(type),
1788 _mesa_enum_to_string(internalFormat));
1789 return true;
1790 }
1791
1792 return false;
1793 }
1794
1795 /**
1796 * Test the glTexImage[123]D() parameters for errors.
1797 *
1798 * \param ctx GL context.
1799 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1800 * \param target texture target given by the user (already validated).
1801 * \param level image level given by the user.
1802 * \param internalFormat internal format given by the user.
1803 * \param format pixel data format given by the user.
1804 * \param type pixel data type given by the user.
1805 * \param width image width given by the user.
1806 * \param height image height given by the user.
1807 * \param depth image depth given by the user.
1808 * \param border image border given by the user.
1809 *
1810 * \return GL_TRUE if a error is found, GL_FALSE otherwise
1811 *
1812 * Verifies each of the parameters against the constants specified in
1813 * __struct gl_contextRec::Const and the supported extensions, and according
1814 * to the OpenGL specification.
1815 * Note that we don't fully error-check the width, height, depth values
1816 * here. That's done in _mesa_legal_texture_dimensions() which is used
1817 * by several other GL entrypoints. Plus, texture dims have a special
1818 * interaction with proxy textures.
1819 */
1820 static GLboolean
1821 texture_error_check( struct gl_context *ctx,
1822 GLuint dimensions, GLenum target,
1823 struct gl_texture_object* texObj,
1824 GLint level, GLint internalFormat,
1825 GLenum format, GLenum type,
1826 GLint width, GLint height,
1827 GLint depth, GLint border,
1828 const GLvoid *pixels )
1829 {
1830 GLenum err;
1831
1832 /* Note: for proxy textures, some error conditions immediately generate
1833 * a GL error in the usual way. But others do not generate a GL error.
1834 * Instead, they cause the width, height, depth, format fields of the
1835 * texture image to be zeroed-out. The GL spec seems to indicate that the
1836 * zero-out behaviour is only used in cases related to memory allocation.
1837 */
1838
1839 /* level check */
1840 if (level < 0 || level >= _mesa_max_texture_levels(ctx, target)) {
1841 _mesa_error(ctx, GL_INVALID_VALUE,
1842 "glTexImage%dD(level=%d)", dimensions, level);
1843 return GL_TRUE;
1844 }
1845
1846 /* Check border */
1847 if (border < 0 || border > 1 ||
1848 ((ctx->API != API_OPENGL_COMPAT ||
1849 target == GL_TEXTURE_RECTANGLE_NV ||
1850 target == GL_PROXY_TEXTURE_RECTANGLE_NV) && border != 0)) {
1851 _mesa_error(ctx, GL_INVALID_VALUE,
1852 "glTexImage%dD(border=%d)", dimensions, border);
1853 return GL_TRUE;
1854 }
1855
1856 if (width < 0 || height < 0 || depth < 0) {
1857 _mesa_error(ctx, GL_INVALID_VALUE,
1858 "glTexImage%dD(width, height or depth < 0)", dimensions);
1859 return GL_TRUE;
1860 }
1861
1862 /* Check incoming image format and type */
1863 err = _mesa_error_check_format_and_type(ctx, format, type);
1864 if (err != GL_NO_ERROR) {
1865 /* Prior to OpenGL-ES 2.0, an INVALID_VALUE is expected instead of
1866 * INVALID_ENUM. From page 73 OpenGL ES 1.1 spec:
1867 *
1868 * "Specifying a value for internalformat that is not one of the
1869 * above (acceptable) values generates the error INVALID VALUE."
1870 */
1871 if (err == GL_INVALID_ENUM && _mesa_is_gles(ctx) && ctx->Version < 20)
1872 err = GL_INVALID_VALUE;
1873
1874 _mesa_error(ctx, err,
1875 "glTexImage%dD(incompatible format = %s, type = %s)",
1876 dimensions, _mesa_enum_to_string(format),
1877 _mesa_enum_to_string(type));
1878 return GL_TRUE;
1879 }
1880
1881 /* Check internalFormat */
1882 if (_mesa_base_tex_format(ctx, internalFormat) < 0) {
1883 _mesa_error(ctx, GL_INVALID_VALUE,
1884 "glTexImage%dD(internalFormat=%s)",
1885 dimensions, _mesa_enum_to_string(internalFormat));
1886 return GL_TRUE;
1887 }
1888
1889 /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
1890 * combinations of format, internalFormat, and type that can be used.
1891 * Formats and types that require additional extensions (e.g., GL_FLOAT
1892 * requires GL_OES_texture_float) are filtered elsewhere.
1893 */
1894 char bufCallerName[20];
1895 snprintf(bufCallerName, 20, "glTexImage%dD", dimensions);
1896 if (_mesa_is_gles(ctx) &&
1897 texture_format_error_check_gles(ctx, format, type,
1898 internalFormat, bufCallerName)) {
1899 return GL_TRUE;
1900 }
1901
1902 /* validate the bound PBO, if any */
1903 if (!_mesa_validate_pbo_source(ctx, dimensions, &ctx->Unpack,
1904 width, height, depth, format, type,
1905 INT_MAX, pixels, "glTexImage")) {
1906 return GL_TRUE;
1907 }
1908
1909 /* make sure internal format and format basically agree */
1910 if (!texture_formats_agree(internalFormat, format)) {
1911 _mesa_error(ctx, GL_INVALID_OPERATION,
1912 "glTexImage%dD(incompatible internalFormat = %s, format = %s)",
1913 dimensions, _mesa_enum_to_string(internalFormat),
1914 _mesa_enum_to_string(format));
1915 return GL_TRUE;
1916 }
1917
1918 /* additional checks for ycbcr textures */
1919 if (internalFormat == GL_YCBCR_MESA) {
1920 assert(ctx->Extensions.MESA_ycbcr_texture);
1921 if (type != GL_UNSIGNED_SHORT_8_8_MESA &&
1922 type != GL_UNSIGNED_SHORT_8_8_REV_MESA) {
1923 char message[100];
1924 snprintf(message, sizeof(message),
1925 "glTexImage%dD(format/type YCBCR mismatch)",
1926 dimensions);
1927 _mesa_error(ctx, GL_INVALID_ENUM, "%s", message);
1928 return GL_TRUE; /* error */
1929 }
1930 if (target != GL_TEXTURE_2D &&
1931 target != GL_PROXY_TEXTURE_2D &&
1932 target != GL_TEXTURE_RECTANGLE_NV &&
1933 target != GL_PROXY_TEXTURE_RECTANGLE_NV) {
1934 _mesa_error(ctx, GL_INVALID_ENUM,
1935 "glTexImage%dD(bad target for YCbCr texture)",
1936 dimensions);
1937 return GL_TRUE;
1938 }
1939 if (border != 0) {
1940 char message[100];
1941 snprintf(message, sizeof(message),
1942 "glTexImage%dD(format=GL_YCBCR_MESA and border=%d)",
1943 dimensions, border);
1944 _mesa_error(ctx, GL_INVALID_VALUE, "%s", message);
1945 return GL_TRUE;
1946 }
1947 }
1948
1949 /* additional checks for depth textures */
1950 if (!_mesa_legal_texture_base_format_for_target(ctx, target, internalFormat)) {
1951 _mesa_error(ctx, GL_INVALID_OPERATION,
1952 "glTexImage%dD(bad target for texture)", dimensions);
1953 return GL_TRUE;
1954 }
1955
1956 /* additional checks for compressed textures */
1957 if (_mesa_is_compressed_format(ctx, internalFormat)) {
1958 GLenum err;
1959 if (!_mesa_target_can_be_compressed(ctx, target, internalFormat, &err)) {
1960 _mesa_error(ctx, err,
1961 "glTexImage%dD(target can't be compressed)", dimensions);
1962 return GL_TRUE;
1963 }
1964 if (_mesa_format_no_online_compression(internalFormat)) {
1965 _mesa_error(ctx, GL_INVALID_OPERATION,
1966 "glTexImage%dD(no compression for format)", dimensions);
1967 return GL_TRUE;
1968 }
1969 if (border != 0) {
1970 _mesa_error(ctx, GL_INVALID_OPERATION,
1971 "glTexImage%dD(border!=0)", dimensions);
1972 return GL_TRUE;
1973 }
1974 }
1975
1976 /* additional checks for integer textures */
1977 if ((ctx->Version >= 30 || ctx->Extensions.EXT_texture_integer) &&
1978 (_mesa_is_enum_format_integer(format) !=
1979 _mesa_is_enum_format_integer(internalFormat))) {
1980 _mesa_error(ctx, GL_INVALID_OPERATION,
1981 "glTexImage%dD(integer/non-integer format mismatch)",
1982 dimensions);
1983 return GL_TRUE;
1984 }
1985
1986 if (!mutable_tex_object(texObj)) {
1987 _mesa_error(ctx, GL_INVALID_OPERATION,
1988 "glTexImage%dD(immutable texture)", dimensions);
1989 return GL_TRUE;
1990 }
1991
1992 /* if we get here, the parameters are OK */
1993 return GL_FALSE;
1994 }
1995
1996
1997 /**
1998 * Error checking for glCompressedTexImage[123]D().
1999 * Note that the width, height and depth values are not fully error checked
2000 * here.
2001 * \return GL_TRUE if a error is found, GL_FALSE otherwise
2002 */
2003 static GLenum
2004 compressed_texture_error_check(struct gl_context *ctx, GLint dimensions,
2005 GLenum target, struct gl_texture_object* texObj,
2006 GLint level, GLenum internalFormat, GLsizei width,
2007 GLsizei height, GLsizei depth, GLint border,
2008 GLsizei imageSize, const GLvoid *data)
2009 {
2010 const GLint maxLevels = _mesa_max_texture_levels(ctx, target);
2011 GLint expectedSize;
2012 GLenum error = GL_NO_ERROR;
2013 char *reason = ""; /* no error */
2014
2015 if (!_mesa_target_can_be_compressed(ctx, target, internalFormat, &error)) {
2016 reason = "target";
2017 goto error;
2018 }
2019
2020 /* This will detect any invalid internalFormat value */
2021 if (!_mesa_is_compressed_format(ctx, internalFormat)) {
2022 _mesa_error(ctx, GL_INVALID_ENUM,
2023 "glCompressedTexImage%dD(internalFormat=%s)",
2024 dimensions, _mesa_enum_to_string(internalFormat));
2025 return GL_TRUE;
2026 }
2027
2028 /* validate the bound PBO, if any */
2029 if (!_mesa_validate_pbo_source_compressed(ctx, dimensions, &ctx->Unpack,
2030 imageSize, data,
2031 "glCompressedTexImage")) {
2032 return GL_TRUE;
2033 }
2034
2035 switch (internalFormat) {
2036 case GL_PALETTE4_RGB8_OES:
2037 case GL_PALETTE4_RGBA8_OES:
2038 case GL_PALETTE4_R5_G6_B5_OES:
2039 case GL_PALETTE4_RGBA4_OES:
2040 case GL_PALETTE4_RGB5_A1_OES:
2041 case GL_PALETTE8_RGB8_OES:
2042 case GL_PALETTE8_RGBA8_OES:
2043 case GL_PALETTE8_R5_G6_B5_OES:
2044 case GL_PALETTE8_RGBA4_OES:
2045 case GL_PALETTE8_RGB5_A1_OES:
2046 /* check level (note that level should be zero or less!) */
2047 if (level > 0 || level < -maxLevels) {
2048 reason = "level";
2049 error = GL_INVALID_VALUE;
2050 goto error;
2051 }
2052
2053 if (dimensions != 2) {
2054 reason = "compressed paletted textures must be 2D";
2055 error = GL_INVALID_OPERATION;
2056 goto error;
2057 }
2058
2059 /* Figure out the expected texture size (in bytes). This will be
2060 * checked against the actual size later.
2061 */
2062 expectedSize = _mesa_cpal_compressed_size(level, internalFormat,
2063 width, height);
2064
2065 /* This is for the benefit of the TestProxyTexImage below. It expects
2066 * level to be non-negative. OES_compressed_paletted_texture uses a
2067 * weird mechanism where the level specified to glCompressedTexImage2D
2068 * is -(n-1) number of levels in the texture, and the data specifies the
2069 * complete mipmap stack. This is done to ensure the palette is the
2070 * same for all levels.
2071 */
2072 level = -level;
2073 break;
2074
2075 default:
2076 /* check level */
2077 if (level < 0 || level >= maxLevels) {
2078 reason = "level";
2079 error = GL_INVALID_VALUE;
2080 goto error;
2081 }
2082
2083 /* Figure out the expected texture size (in bytes). This will be
2084 * checked against the actual size later.
2085 */
2086 expectedSize = compressed_tex_size(width, height, depth, internalFormat);
2087 break;
2088 }
2089
2090 /* This should really never fail */
2091 if (_mesa_base_tex_format(ctx, internalFormat) < 0) {
2092 reason = "internalFormat";
2093 error = GL_INVALID_ENUM;
2094 goto error;
2095 }
2096
2097 /* No compressed formats support borders at this time */
2098 if (border != 0) {
2099 reason = "border != 0";
2100 error = GL_INVALID_VALUE;
2101 goto error;
2102 }
2103
2104 /* Check for invalid pixel storage modes */
2105 if (!_mesa_compressed_pixel_storage_error_check(ctx, dimensions,
2106 &ctx->Unpack,
2107 "glCompressedTexImage")) {
2108 return GL_FALSE;
2109 }
2110
2111 /* check image size in bytes */
2112 if (expectedSize != imageSize) {
2113 /* Per GL_ARB_texture_compression: GL_INVALID_VALUE is generated [...]
2114 * if <imageSize> is not consistent with the format, dimensions, and
2115 * contents of the specified image.
2116 */
2117 reason = "imageSize inconsistent with width/height/format";
2118 error = GL_INVALID_VALUE;
2119 goto error;
2120 }
2121
2122 if (!mutable_tex_object(texObj)) {
2123 reason = "immutable texture";
2124 error = GL_INVALID_OPERATION;
2125 goto error;
2126 }
2127
2128 return GL_FALSE;
2129
2130 error:
2131 /* Note: not all error paths exit through here. */
2132 _mesa_error(ctx, error, "glCompressedTexImage%dD(%s)",
2133 dimensions, reason);
2134 return GL_TRUE;
2135 }
2136
2137
2138
2139 /**
2140 * Test glTexSubImage[123]D() parameters for errors.
2141 *
2142 * \param ctx GL context.
2143 * \param dimensions texture image dimensions (must be 1, 2 or 3).
2144 * \param target texture target given by the user (already validated)
2145 * \param level image level given by the user.
2146 * \param xoffset sub-image x offset given by the user.
2147 * \param yoffset sub-image y offset given by the user.
2148 * \param zoffset sub-image z offset given by the user.
2149 * \param format pixel data format given by the user.
2150 * \param type pixel data type given by the user.
2151 * \param width image width given by the user.
2152 * \param height image height given by the user.
2153 * \param depth image depth given by the user.
2154 *
2155 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2156 *
2157 * Verifies each of the parameters against the constants specified in
2158 * __struct gl_contextRec::Const and the supported extensions, and according
2159 * to the OpenGL specification.
2160 */
2161 static GLboolean
2162 texsubimage_error_check(struct gl_context *ctx, GLuint dimensions,
2163 struct gl_texture_object *texObj,
2164 GLenum target, GLint level,
2165 GLint xoffset, GLint yoffset, GLint zoffset,
2166 GLint width, GLint height, GLint depth,
2167 GLenum format, GLenum type, const GLvoid *pixels,
2168 const char *callerName)
2169 {
2170 struct gl_texture_image *texImage;
2171 GLenum err;
2172
2173 if (!texObj) {
2174 /* must be out of memory */
2175 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s()", callerName);
2176 return GL_TRUE;
2177 }
2178
2179 /* level check */
2180 if (level < 0 || level >= _mesa_max_texture_levels(ctx, target)) {
2181 _mesa_error(ctx, GL_INVALID_VALUE, "%s(level=%d)", callerName, level);
2182 return GL_TRUE;
2183 }
2184
2185 if (error_check_subtexture_negative_dimensions(ctx, dimensions,
2186 width, height, depth,
2187 callerName)) {
2188 return GL_TRUE;
2189 }
2190
2191 texImage = _mesa_select_tex_image(texObj, target, level);
2192 if (!texImage) {
2193 /* non-existant texture level */
2194 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid texture level %d)",
2195 callerName, level);
2196 return GL_TRUE;
2197 }
2198
2199 err = _mesa_error_check_format_and_type(ctx, format, type);
2200 if (err != GL_NO_ERROR) {
2201 _mesa_error(ctx, err,
2202 "%s(incompatible format = %s, type = %s)",
2203 callerName, _mesa_enum_to_string(format),
2204 _mesa_enum_to_string(type));
2205 return GL_TRUE;
2206 }
2207
2208 if (!texture_formats_agree(texImage->InternalFormat, format)) {
2209 _mesa_error(ctx, GL_INVALID_OPERATION,
2210 "%s(incompatible internalFormat = %s, format = %s)",
2211 callerName,
2212 _mesa_enum_to_string(texImage->InternalFormat),
2213 _mesa_enum_to_string(format));
2214 return GL_TRUE;
2215 }
2216
2217 GLenum internalFormat = _mesa_is_gles(ctx) ?
2218 oes_float_internal_format(ctx, texImage->InternalFormat, type) :
2219 texImage->InternalFormat;
2220
2221 /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
2222 * combinations of format, internalFormat, and type that can be used.
2223 * Formats and types that require additional extensions (e.g., GL_FLOAT
2224 * requires GL_OES_texture_float) are filtered elsewhere.
2225 */
2226 if (_mesa_is_gles(ctx) &&
2227 texture_format_error_check_gles(ctx, format, type,
2228 internalFormat, callerName)) {
2229 return GL_TRUE;
2230 }
2231
2232 /* validate the bound PBO, if any */
2233 if (!_mesa_validate_pbo_source(ctx, dimensions, &ctx->Unpack,
2234 width, height, depth, format, type,
2235 INT_MAX, pixels, callerName)) {
2236 return GL_TRUE;
2237 }
2238
2239 if (error_check_subtexture_dimensions(ctx, dimensions,
2240 texImage, xoffset, yoffset, zoffset,
2241 width, height, depth, callerName)) {
2242 return GL_TRUE;
2243 }
2244
2245 if (_mesa_is_format_compressed(texImage->TexFormat)) {
2246 if (_mesa_format_no_online_compression(texImage->InternalFormat)) {
2247 _mesa_error(ctx, GL_INVALID_OPERATION,
2248 "%s(no compression for format)", callerName);
2249 return GL_TRUE;
2250 }
2251 }
2252
2253 if (ctx->Version >= 30 || ctx->Extensions.EXT_texture_integer) {
2254 /* both source and dest must be integer-valued, or neither */
2255 if (_mesa_is_format_integer_color(texImage->TexFormat) !=
2256 _mesa_is_enum_format_integer(format)) {
2257 _mesa_error(ctx, GL_INVALID_OPERATION,
2258 "%s(integer/non-integer format mismatch)", callerName);
2259 return GL_TRUE;
2260 }
2261 }
2262
2263 return GL_FALSE;
2264 }
2265
2266
2267 /**
2268 * Test glCopyTexImage[12]D() parameters for errors.
2269 *
2270 * \param ctx GL context.
2271 * \param dimensions texture image dimensions (must be 1, 2 or 3).
2272 * \param target texture target given by the user.
2273 * \param level image level given by the user.
2274 * \param internalFormat internal format given by the user.
2275 * \param width image width given by the user.
2276 * \param height image height given by the user.
2277 * \param border texture border.
2278 *
2279 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2280 *
2281 * Verifies each of the parameters against the constants specified in
2282 * __struct gl_contextRec::Const and the supported extensions, and according
2283 * to the OpenGL specification.
2284 */
2285 static GLboolean
2286 copytexture_error_check( struct gl_context *ctx, GLuint dimensions,
2287 GLenum target, struct gl_texture_object* texObj,
2288 GLint level, GLint internalFormat, GLint border )
2289 {
2290 GLint baseFormat;
2291 GLint rb_base_format;
2292 struct gl_renderbuffer *rb;
2293 GLenum rb_internal_format;
2294
2295 /* check target */
2296 if (!legal_texsubimage_target(ctx, dimensions, target, false)) {
2297 _mesa_error(ctx, GL_INVALID_ENUM, "glCopyTexImage%uD(target=%s)",
2298 dimensions, _mesa_enum_to_string(target));
2299 return GL_TRUE;
2300 }
2301
2302 /* level check */
2303 if (level < 0 || level >= _mesa_max_texture_levels(ctx, target)) {
2304 _mesa_error(ctx, GL_INVALID_VALUE,
2305 "glCopyTexImage%dD(level=%d)", dimensions, level);
2306 return GL_TRUE;
2307 }
2308
2309 /* Check that the source buffer is complete */
2310 if (_mesa_is_user_fbo(ctx->ReadBuffer)) {
2311 if (ctx->ReadBuffer->_Status == 0) {
2312 _mesa_test_framebuffer_completeness(ctx, ctx->ReadBuffer);
2313 }
2314 if (ctx->ReadBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
2315 _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
2316 "glCopyTexImage%dD(invalid readbuffer)", dimensions);
2317 return GL_TRUE;
2318 }
2319
2320 if (ctx->ReadBuffer->Visual.samples > 0) {
2321 _mesa_error(ctx, GL_INVALID_OPERATION,
2322 "glCopyTexImage%dD(multisample FBO)", dimensions);
2323 return GL_TRUE;
2324 }
2325 }
2326
2327 /* Check border */
2328 if (border < 0 || border > 1 ||
2329 ((ctx->API != API_OPENGL_COMPAT ||
2330 target == GL_TEXTURE_RECTANGLE_NV ||
2331 target == GL_PROXY_TEXTURE_RECTANGLE_NV) && border != 0)) {
2332 _mesa_error(ctx, GL_INVALID_VALUE,
2333 "glCopyTexImage%dD(border=%d)", dimensions, border);
2334 return GL_TRUE;
2335 }
2336
2337 /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
2338 * internalFormat.
2339 */
2340 if (_mesa_is_gles(ctx) && !_mesa_is_gles3(ctx)) {
2341 switch (internalFormat) {
2342 case GL_ALPHA:
2343 case GL_RGB:
2344 case GL_RGBA:
2345 case GL_LUMINANCE:
2346 case GL_LUMINANCE_ALPHA:
2347 break;
2348 default:
2349 _mesa_error(ctx, GL_INVALID_ENUM,
2350 "glCopyTexImage%dD(internalFormat=%s)", dimensions,
2351 _mesa_enum_to_string(internalFormat));
2352 return GL_TRUE;
2353 }
2354 } else {
2355 /*
2356 * Section 8.6 (Alternate Texture Image Specification Commands) of the
2357 * OpenGL 4.5 (Compatibility Profile) spec says:
2358 *
2359 * "Parameters level, internalformat, and border are specified using
2360 * the same values, with the same meanings, as the corresponding
2361 * arguments of TexImage2D, except that internalformat may not be
2362 * specified as 1, 2, 3, or 4."
2363 */
2364 if (internalFormat >= 1 && internalFormat <= 4) {
2365 _mesa_error(ctx, GL_INVALID_ENUM,
2366 "glCopyTexImage%dD(internalFormat=%d)", dimensions,
2367 internalFormat);
2368 return GL_TRUE;
2369 }
2370 }
2371
2372 baseFormat = _mesa_base_tex_format(ctx, internalFormat);
2373 if (baseFormat < 0) {
2374 _mesa_error(ctx, GL_INVALID_ENUM,
2375 "glCopyTexImage%dD(internalFormat=%s)", dimensions,
2376 _mesa_enum_to_string(internalFormat));
2377 return GL_TRUE;
2378 }
2379
2380 rb = _mesa_get_read_renderbuffer_for_format(ctx, internalFormat);
2381 if (rb == NULL) {
2382 _mesa_error(ctx, GL_INVALID_OPERATION,
2383 "glCopyTexImage%dD(read buffer)", dimensions);
2384 return GL_TRUE;
2385 }
2386
2387 rb_internal_format = rb->InternalFormat;
2388 rb_base_format = _mesa_base_tex_format(ctx, rb->InternalFormat);
2389 if (_mesa_is_color_format(internalFormat)) {
2390 if (rb_base_format < 0) {
2391 _mesa_error(ctx, GL_INVALID_VALUE,
2392 "glCopyTexImage%dD(internalFormat=%s)", dimensions,
2393 _mesa_enum_to_string(internalFormat));
2394 return GL_TRUE;
2395 }
2396 }
2397
2398 if (_mesa_is_gles(ctx)) {
2399 bool valid = true;
2400 if (_mesa_components_in_format(baseFormat) >
2401 _mesa_components_in_format(rb_base_format)) {
2402 valid = false;
2403 }
2404 if (baseFormat == GL_DEPTH_COMPONENT ||
2405 baseFormat == GL_DEPTH_STENCIL ||
2406 baseFormat == GL_STENCIL_INDEX ||
2407 rb_base_format == GL_DEPTH_COMPONENT ||
2408 rb_base_format == GL_DEPTH_STENCIL ||
2409 rb_base_format == GL_STENCIL_INDEX ||
2410 ((baseFormat == GL_LUMINANCE_ALPHA ||
2411 baseFormat == GL_ALPHA) &&
2412 rb_base_format != GL_RGBA) ||
2413 internalFormat == GL_RGB9_E5) {
2414 valid = false;
2415 }
2416 if (internalFormat == GL_RGB9_E5) {
2417 valid = false;
2418 }
2419 if (!valid) {
2420 _mesa_error(ctx, GL_INVALID_OPERATION,
2421 "glCopyTexImage%dD(internalFormat=%s)", dimensions,
2422 _mesa_enum_to_string(internalFormat));
2423 return GL_TRUE;
2424 }
2425 }
2426
2427 if (_mesa_is_gles3(ctx)) {
2428 bool rb_is_srgb = (ctx->Extensions.EXT_sRGB &&
2429 _mesa_is_format_srgb(rb->Format));
2430 bool dst_is_srgb = false;
2431
2432 if (_mesa_get_linear_internalformat(internalFormat) != internalFormat) {
2433 dst_is_srgb = true;
2434 }
2435
2436 if (rb_is_srgb != dst_is_srgb) {
2437 /* Page 137 (page 149 of the PDF) in section 3.8.5 of the
2438 * OpenGLES 3.0.0 spec says:
2439 *
2440 * "The error INVALID_OPERATION is also generated if the
2441 * value of FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING for the
2442 * framebuffer attachment corresponding to the read buffer
2443 * is LINEAR (see section 6.1.13) and internalformat is
2444 * one of the sRGB formats described in section 3.8.16, or
2445 * if the value of FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING is
2446 * SRGB and internalformat is not one of the sRGB formats."
2447 */
2448 _mesa_error(ctx, GL_INVALID_OPERATION,
2449 "glCopyTexImage%dD(srgb usage mismatch)", dimensions);
2450 return GL_TRUE;
2451 }
2452
2453 /* Page 139, Table 3.15 of OpenGL ES 3.0 spec does not define ReadPixels
2454 * types for SNORM formats. Also, conversion to SNORM formats is not
2455 * allowed by Table 3.2 on Page 110.
2456 */
2457 if (!_mesa_has_EXT_render_snorm(ctx) &&
2458 _mesa_is_enum_format_snorm(internalFormat)) {
2459 _mesa_error(ctx, GL_INVALID_OPERATION,
2460 "glCopyTexImage%dD(internalFormat=%s)", dimensions,
2461 _mesa_enum_to_string(internalFormat));
2462 return GL_TRUE;
2463 }
2464 }
2465
2466 if (!_mesa_source_buffer_exists(ctx, baseFormat)) {
2467 _mesa_error(ctx, GL_INVALID_OPERATION,
2468 "glCopyTexImage%dD(missing readbuffer)", dimensions);
2469 return GL_TRUE;
2470 }
2471
2472 /* From the EXT_texture_integer spec:
2473 *
2474 * "INVALID_OPERATION is generated by CopyTexImage* and CopyTexSubImage*
2475 * if the texture internalformat is an integer format and the read color
2476 * buffer is not an integer format, or if the internalformat is not an
2477 * integer format and the read color buffer is an integer format."
2478 */
2479 if (_mesa_is_color_format(internalFormat)) {
2480 bool is_int = _mesa_is_enum_format_integer(internalFormat);
2481 bool is_rbint = _mesa_is_enum_format_integer(rb_internal_format);
2482 bool is_unorm = _mesa_is_enum_format_unorm(internalFormat);
2483 bool is_rbunorm = _mesa_is_enum_format_unorm(rb_internal_format);
2484 if (is_int || is_rbint) {
2485 if (is_int != is_rbint) {
2486 _mesa_error(ctx, GL_INVALID_OPERATION,
2487 "glCopyTexImage%dD(integer vs non-integer)", dimensions);
2488 return GL_TRUE;
2489 } else if (_mesa_is_gles(ctx) &&
2490 _mesa_is_enum_format_unsigned_int(internalFormat) !=
2491 _mesa_is_enum_format_unsigned_int(rb_internal_format)) {
2492 _mesa_error(ctx, GL_INVALID_OPERATION,
2493 "glCopyTexImage%dD(signed vs unsigned integer)",
2494 dimensions);
2495 return GL_TRUE;
2496 }
2497 }
2498
2499 /* From page 138 of OpenGL ES 3.0 spec:
2500 * "The error INVALID_OPERATION is generated if floating-point RGBA
2501 * data is required; if signed integer RGBA data is required and the
2502 * format of the current color buffer is not signed integer; if
2503 * unsigned integer RGBA data is required and the format of the
2504 * current color buffer is not unsigned integer; or if fixed-point
2505 * RGBA data is required and the format of the current color buffer
2506 * is not fixed-point.
2507 */
2508 if (_mesa_is_gles(ctx) && is_unorm != is_rbunorm)
2509 _mesa_error(ctx, GL_INVALID_OPERATION,
2510 "glCopyTexImage%dD(unorm vs non-unorm)", dimensions);
2511 }
2512
2513 if (_mesa_is_compressed_format(ctx, internalFormat)) {
2514 GLenum err;
2515 if (!_mesa_target_can_be_compressed(ctx, target, internalFormat, &err)) {
2516 _mesa_error(ctx, err,
2517 "glCopyTexImage%dD(target can't be compressed)", dimensions);
2518 return GL_TRUE;
2519 }
2520 if (_mesa_format_no_online_compression(internalFormat)) {
2521 _mesa_error(ctx, GL_INVALID_OPERATION,
2522 "glCopyTexImage%dD(no compression for format)", dimensions);
2523 return GL_TRUE;
2524 }
2525 if (border != 0) {
2526 _mesa_error(ctx, GL_INVALID_OPERATION,
2527 "glCopyTexImage%dD(border!=0)", dimensions);
2528 return GL_TRUE;
2529 }
2530 }
2531
2532 if (!mutable_tex_object(texObj)) {
2533 _mesa_error(ctx, GL_INVALID_OPERATION,
2534 "glCopyTexImage%dD(immutable texture)", dimensions);
2535 return GL_TRUE;
2536 }
2537
2538 /* if we get here, the parameters are OK */
2539 return GL_FALSE;
2540 }
2541
2542
2543 /**
2544 * Test glCopyTexSubImage[12]D() parameters for errors.
2545 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2546 */
2547 static GLboolean
2548 copytexsubimage_error_check(struct gl_context *ctx, GLuint dimensions,
2549 const struct gl_texture_object *texObj,
2550 GLenum target, GLint level,
2551 GLint xoffset, GLint yoffset, GLint zoffset,
2552 GLint width, GLint height, const char *caller)
2553 {
2554 assert(texObj);
2555
2556 struct gl_texture_image *texImage;
2557
2558 /* Check that the source buffer is complete */
2559 if (_mesa_is_user_fbo(ctx->ReadBuffer)) {
2560 if (ctx->ReadBuffer->_Status == 0) {
2561 _mesa_test_framebuffer_completeness(ctx, ctx->ReadBuffer);
2562 }
2563 if (ctx->ReadBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
2564 _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
2565 "%s(invalid readbuffer)", caller);
2566 return GL_TRUE;
2567 }
2568
2569 if (ctx->ReadBuffer->Visual.samples > 0) {
2570 _mesa_error(ctx, GL_INVALID_OPERATION,
2571 "%s(multisample FBO)", caller);
2572 return GL_TRUE;
2573 }
2574 }
2575
2576 /* Check level */
2577 if (level < 0 || level >= _mesa_max_texture_levels(ctx, target)) {
2578 _mesa_error(ctx, GL_INVALID_VALUE, "%s(level=%d)", caller, level);
2579 return GL_TRUE;
2580 }
2581
2582 texImage = _mesa_select_tex_image(texObj, target, level);
2583 if (!texImage) {
2584 /* destination image does not exist */
2585 _mesa_error(ctx, GL_INVALID_OPERATION,
2586 "%s(invalid texture level %d)", caller, level);
2587 return GL_TRUE;
2588 }
2589
2590 if (error_check_subtexture_negative_dimensions(ctx, dimensions,
2591 width, height, 1, caller)) {
2592 return GL_TRUE;
2593 }
2594
2595 if (error_check_subtexture_dimensions(ctx, dimensions, texImage,
2596 xoffset, yoffset, zoffset,
2597 width, height, 1, caller)) {
2598 return GL_TRUE;
2599 }
2600
2601 if (_mesa_is_format_compressed(texImage->TexFormat)) {
2602 if (_mesa_format_no_online_compression(texImage->InternalFormat)) {
2603 _mesa_error(ctx, GL_INVALID_OPERATION,
2604 "%s(no compression for format)", caller);
2605 return GL_TRUE;
2606 }
2607 }
2608
2609 if (texImage->InternalFormat == GL_YCBCR_MESA) {
2610 _mesa_error(ctx, GL_INVALID_OPERATION, "%s()", caller);
2611 return GL_TRUE;
2612 }
2613
2614 /* From OpenGL ES 3.2 spec, section 8.6:
2615 *
2616 * "An INVALID_OPERATION error is generated by CopyTexSubImage3D,
2617 * CopyTexImage2D, or CopyTexSubImage2D if the internalformat of the
2618 * texture image being (re)specified is RGB9_E5"
2619 */
2620 if (texImage->InternalFormat == GL_RGB9_E5 &&
2621 !_mesa_is_desktop_gl(ctx)) {
2622 _mesa_error(ctx, GL_INVALID_OPERATION,
2623 "%s(invalid internal format %s)", caller,
2624 _mesa_enum_to_string(texImage->InternalFormat));
2625 return GL_TRUE;
2626 }
2627
2628 if (!_mesa_source_buffer_exists(ctx, texImage->_BaseFormat)) {
2629 _mesa_error(ctx, GL_INVALID_OPERATION,
2630 "%s(missing readbuffer, format=%s)", caller,
2631 _mesa_enum_to_string(texImage->_BaseFormat));
2632 return GL_TRUE;
2633 }
2634
2635 /* From the EXT_texture_integer spec:
2636 *
2637 * "INVALID_OPERATION is generated by CopyTexImage* and
2638 * CopyTexSubImage* if the texture internalformat is an integer format
2639 * and the read color buffer is not an integer format, or if the
2640 * internalformat is not an integer format and the read color buffer
2641 * is an integer format."
2642 */
2643 if (_mesa_is_color_format(texImage->InternalFormat)) {
2644 struct gl_renderbuffer *rb = ctx->ReadBuffer->_ColorReadBuffer;
2645
2646 if (_mesa_is_format_integer_color(rb->Format) !=
2647 _mesa_is_format_integer_color(texImage->TexFormat)) {
2648 _mesa_error(ctx, GL_INVALID_OPERATION,
2649 "%s(integer vs non-integer)", caller);
2650 return GL_TRUE;
2651 }
2652 }
2653
2654 /* In the ES 3.2 specification's Table 8.13 (Valid CopyTexImage source
2655 * framebuffer/destination texture base internal format combinations),
2656 * all the entries for stencil are left blank (unsupported).
2657 */
2658 if (_mesa_is_gles(ctx) && _mesa_is_stencil_format(texImage->_BaseFormat)) {
2659 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(stencil disallowed)", caller);
2660 return GL_TRUE;
2661 }
2662
2663 /* if we get here, the parameters are OK */
2664 return GL_FALSE;
2665 }
2666
2667
2668 /** Callback info for walking over FBO hash table */
2669 struct cb_info
2670 {
2671 struct gl_context *ctx;
2672 struct gl_texture_object *texObj;
2673 GLuint level, face;
2674 };
2675
2676
2677 /**
2678 * Check render to texture callback. Called from _mesa_HashWalk().
2679 */
2680 static void
2681 check_rtt_cb(UNUSED GLuint key, void *data, void *userData)
2682 {
2683 struct gl_framebuffer *fb = (struct gl_framebuffer *) data;
2684 const struct cb_info *info = (struct cb_info *) userData;
2685 struct gl_context *ctx = info->ctx;
2686 const struct gl_texture_object *texObj = info->texObj;
2687 const GLuint level = info->level, face = info->face;
2688
2689 /* If this is a user-created FBO */
2690 if (_mesa_is_user_fbo(fb)) {
2691 GLuint i;
2692 /* check if any of the FBO's attachments point to 'texObj' */
2693 for (i = 0; i < BUFFER_COUNT; i++) {
2694 struct gl_renderbuffer_attachment *att = fb->Attachment + i;
2695 if (att->Type == GL_TEXTURE &&
2696 att->Texture == texObj &&
2697 att->TextureLevel == level &&
2698 att->CubeMapFace == face) {
2699 _mesa_update_texture_renderbuffer(ctx, fb, att);
2700 assert(att->Renderbuffer->TexImage);
2701 /* Mark fb status as indeterminate to force re-validation */
2702 fb->_Status = 0;
2703
2704 /* Make sure that the revalidation actually happens if this is
2705 * being done to currently-bound buffers.
2706 */
2707 if (fb == ctx->DrawBuffer || fb == ctx->ReadBuffer)
2708 ctx->NewState |= _NEW_BUFFERS;
2709 }
2710 }
2711 }
2712 }
2713
2714
2715 /**
2716 * When a texture image is specified we have to check if it's bound to
2717 * any framebuffer objects (render to texture) in order to detect changes
2718 * in size or format since that effects FBO completeness.
2719 * Any FBOs rendering into the texture must be re-validated.
2720 */
2721 void
2722 _mesa_update_fbo_texture(struct gl_context *ctx,
2723 struct gl_texture_object *texObj,
2724 GLuint face, GLuint level)
2725 {
2726 /* Only check this texture if it's been marked as RenderToTexture */
2727 if (texObj->_RenderToTexture) {
2728 struct cb_info info;
2729 info.ctx = ctx;
2730 info.texObj = texObj;
2731 info.level = level;
2732 info.face = face;
2733 _mesa_HashWalk(ctx->Shared->FrameBuffers, check_rtt_cb, &info);
2734 }
2735 }
2736
2737
2738 /**
2739 * If the texture object's GenerateMipmap flag is set and we've
2740 * changed the texture base level image, regenerate the rest of the
2741 * mipmap levels now.
2742 */
2743 static inline void
2744 check_gen_mipmap(struct gl_context *ctx, GLenum target,
2745 struct gl_texture_object *texObj, GLint level)
2746 {
2747 if (texObj->GenerateMipmap &&
2748 level == texObj->BaseLevel &&
2749 level < texObj->MaxLevel) {
2750 assert(ctx->Driver.GenerateMipmap);
2751 ctx->Driver.GenerateMipmap(ctx, target, texObj);
2752 }
2753 }
2754
2755
2756 /** Debug helper: override the user-requested internal format */
2757 static GLenum
2758 override_internal_format(GLenum internalFormat, UNUSED GLint width,
2759 UNUSED GLint height)
2760 {
2761 #if 0
2762 if (internalFormat == GL_RGBA16F_ARB ||
2763 internalFormat == GL_RGBA32F_ARB) {
2764 printf("Convert rgba float tex to int %d x %d\n", width, height);
2765 return GL_RGBA;
2766 }
2767 else if (internalFormat == GL_RGB16F_ARB ||
2768 internalFormat == GL_RGB32F_ARB) {
2769 printf("Convert rgb float tex to int %d x %d\n", width, height);
2770 return GL_RGB;
2771 }
2772 else if (internalFormat == GL_LUMINANCE_ALPHA16F_ARB ||
2773 internalFormat == GL_LUMINANCE_ALPHA32F_ARB) {
2774 printf("Convert luminance float tex to int %d x %d\n", width, height);
2775 return GL_LUMINANCE_ALPHA;
2776 }
2777 else if (internalFormat == GL_LUMINANCE16F_ARB ||
2778 internalFormat == GL_LUMINANCE32F_ARB) {
2779 printf("Convert luminance float tex to int %d x %d\n", width, height);
2780 return GL_LUMINANCE;
2781 }
2782 else if (internalFormat == GL_ALPHA16F_ARB ||
2783 internalFormat == GL_ALPHA32F_ARB) {
2784 printf("Convert luminance float tex to int %d x %d\n", width, height);
2785 return GL_ALPHA;
2786 }
2787 /*
2788 else if (internalFormat == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT) {
2789 internalFormat = GL_RGBA;
2790 }
2791 */
2792 else {
2793 return internalFormat;
2794 }
2795 #else
2796 return internalFormat;
2797 #endif
2798 }
2799
2800
2801 /**
2802 * Choose the actual hardware format for a texture image.
2803 * Try to use the same format as the previous image level when possible.
2804 * Otherwise, ask the driver for the best format.
2805 * It's important to try to choose a consistant format for all levels
2806 * for efficient texture memory layout/allocation. In particular, this
2807 * comes up during automatic mipmap generation.
2808 */
2809 mesa_format
2810 _mesa_choose_texture_format(struct gl_context *ctx,
2811 struct gl_texture_object *texObj,
2812 GLenum target, GLint level,
2813 GLenum internalFormat, GLenum format, GLenum type)
2814 {
2815 mesa_format f;
2816
2817 /* see if we've already chosen a format for the previous level */
2818 if (level > 0) {
2819 struct gl_texture_image *prevImage =
2820 _mesa_select_tex_image(texObj, target, level - 1);
2821 /* See if the prev level is defined and has an internal format which
2822 * matches the new internal format.
2823 */
2824 if (prevImage &&
2825 prevImage->Width > 0 &&
2826 prevImage->InternalFormat == internalFormat) {
2827 /* use the same format */
2828 assert(prevImage->TexFormat != MESA_FORMAT_NONE);
2829 return prevImage->TexFormat;
2830 }
2831 }
2832
2833 f = ctx->Driver.ChooseTextureFormat(ctx, target, internalFormat,
2834 format, type);
2835 assert(f != MESA_FORMAT_NONE);
2836 return f;
2837 }
2838
2839
2840 /**
2841 * Adjust pixel unpack params and image dimensions to strip off the
2842 * one-pixel texture border.
2843 *
2844 * Gallium and intel don't support texture borders. They've seldem been used
2845 * and seldom been implemented correctly anyway.
2846 *
2847 * \param unpackNew returns the new pixel unpack parameters
2848 */
2849 static void
2850 strip_texture_border(GLenum target,
2851 GLint *width, GLint *height, GLint *depth,
2852 const struct gl_pixelstore_attrib *unpack,
2853 struct gl_pixelstore_attrib *unpackNew)
2854 {
2855 assert(width);
2856 assert(height);
2857 assert(depth);
2858
2859 *unpackNew = *unpack;
2860
2861 if (unpackNew->RowLength == 0)
2862 unpackNew->RowLength = *width;
2863
2864 if (unpackNew->ImageHeight == 0)
2865 unpackNew->ImageHeight = *height;
2866
2867 assert(*width >= 3);
2868 unpackNew->SkipPixels++; /* skip the border */
2869 *width = *width - 2; /* reduce the width by two border pixels */
2870
2871 /* The min height of a texture with a border is 3 */
2872 if (*height >= 3 && target != GL_TEXTURE_1D_ARRAY) {
2873 unpackNew->SkipRows++; /* skip the border */
2874 *height = *height - 2; /* reduce the height by two border pixels */
2875 }
2876
2877 if (*depth >= 3 &&
2878 target != GL_TEXTURE_2D_ARRAY &&
2879 target != GL_TEXTURE_CUBE_MAP_ARRAY) {
2880 unpackNew->SkipImages++; /* skip the border */
2881 *depth = *depth - 2; /* reduce the depth by two border pixels */
2882 }
2883 }
2884
2885 static struct gl_texture_object *
2886 lookup_texture_ext_dsa(struct gl_context *ctx, GLenum target, GLuint texture,
2887 const char *caller)
2888 {
2889 GLenum boundTarget;
2890 switch (target) {
2891 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
2892 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
2893 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
2894 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
2895 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
2896 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
2897 boundTarget = GL_TEXTURE_CUBE_MAP;
2898 break;
2899 default:
2900 boundTarget = target;
2901 break;
2902 }
2903
2904 int targetIndex = _mesa_tex_target_to_index(ctx, boundTarget);
2905 if (targetIndex < 0) {
2906 _mesa_error(ctx, GL_INVALID_ENUM, "%s(target = %s)", caller,
2907 _mesa_enum_to_string(target));
2908 return NULL;
2909 }
2910 assert(targetIndex < NUM_TEXTURE_TARGETS);
2911
2912 struct gl_texture_object *texObj;
2913 if (texture == 0) {
2914 /* Use a default texture object */
2915 texObj = ctx->Shared->DefaultTex[targetIndex];
2916 assert(texObj);
2917 } else {
2918 texObj = _mesa_lookup_texture(ctx, texture);
2919 if (!texObj && ctx->API == API_OPENGL_CORE) {
2920 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(non-gen name)", caller);
2921 return NULL;
2922 }
2923
2924 if (!texObj) {
2925 texObj = ctx->Driver.NewTextureObject(ctx, texture, boundTarget);
2926 if (!texObj) {
2927 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", caller);
2928 return NULL;
2929 }
2930
2931 /* insert into hash table */
2932 _mesa_HashInsert(ctx->Shared->TexObjects, texObj->Name, texObj);
2933 }
2934
2935 if (texObj->Target != boundTarget) {
2936 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(%s != %s)",
2937 caller, _mesa_enum_to_string(texObj->Target),
2938 _mesa_enum_to_string(target));
2939 return NULL;
2940 }
2941 }
2942
2943 return texObj;
2944 }
2945
2946 /**
2947 * Common code to implement all the glTexImage1D/2D/3D functions,
2948 * glCompressedTexImage1D/2D/3D and glTextureImage1D/2D/3DEXT
2949 * \param compressed only GL_TRUE for glCompressedTexImage1D/2D/3D calls.
2950 * \param format the user's image format (only used if !compressed)
2951 * \param type the user's image type (only used if !compressed)
2952 * \param imageSize only used for glCompressedTexImage1D/2D/3D calls.
2953 */
2954 static ALWAYS_INLINE void
2955 teximage(struct gl_context *ctx, GLboolean compressed, GLuint dims,
2956 struct gl_texture_object *texObj,
2957 GLenum target, GLint level, GLint internalFormat,
2958 GLsizei width, GLsizei height, GLsizei depth,
2959 GLint border, GLenum format, GLenum type,
2960 GLsizei imageSize, const GLvoid *pixels, bool no_error)
2961 {
2962 const char *func = compressed ? "glCompressedTexImage" : "glTexImage";
2963 struct gl_pixelstore_attrib unpack_no_border;
2964 const struct gl_pixelstore_attrib *unpack = &ctx->Unpack;
2965 mesa_format texFormat;
2966 bool dimensionsOK = true, sizeOK = true;
2967
2968 FLUSH_VERTICES(ctx, 0);
2969
2970 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE)) {
2971 if (compressed)
2972 _mesa_debug(ctx,
2973 "glCompressedTexImage%uD %s %d %s %d %d %d %d %p\n",
2974 dims,
2975 _mesa_enum_to_string(target), level,
2976 _mesa_enum_to_string(internalFormat),
2977 width, height, depth, border, pixels);
2978 else
2979 _mesa_debug(ctx,
2980 "glTexImage%uD %s %d %s %d %d %d %d %s %s %p\n",
2981 dims,
2982 _mesa_enum_to_string(target), level,
2983 _mesa_enum_to_string(internalFormat),
2984 width, height, depth, border,
2985 _mesa_enum_to_string(format),
2986 _mesa_enum_to_string(type), pixels);
2987 }
2988
2989 internalFormat = override_internal_format(internalFormat, width, height);
2990
2991 if (!no_error &&
2992 /* target error checking */
2993 !legal_teximage_target(ctx, dims, target)) {
2994 _mesa_error(ctx, GL_INVALID_ENUM, "%s%uD(target=%s)",
2995 func, dims, _mesa_enum_to_string(target));
2996 return;
2997 }
2998
2999 if (!texObj)
3000 texObj = _mesa_get_current_tex_object(ctx, target);
3001
3002 if (!no_error) {
3003 /* general error checking */
3004 if (compressed) {
3005 if (compressed_texture_error_check(ctx, dims, target, texObj,
3006 level, internalFormat,
3007 width, height, depth,
3008 border, imageSize, pixels))
3009 return;
3010 } else {
3011 if (texture_error_check(ctx, dims, target, texObj, level, internalFormat,
3012 format, type, width, height, depth, border,
3013 pixels))
3014 return;
3015 }
3016 }
3017 assert(texObj);
3018
3019 /* Here we convert a cpal compressed image into a regular glTexImage2D
3020 * call by decompressing the texture. If we really want to support cpal
3021 * textures in any driver this would have to be changed.
3022 */
3023 if (ctx->API == API_OPENGLES && compressed && dims == 2) {
3024 switch (internalFormat) {
3025 case GL_PALETTE4_RGB8_OES:
3026 case GL_PALETTE4_RGBA8_OES:
3027 case GL_PALETTE4_R5_G6_B5_OES:
3028 case GL_PALETTE4_RGBA4_OES:
3029 case GL_PALETTE4_RGB5_A1_OES:
3030 case GL_PALETTE8_RGB8_OES:
3031 case GL_PALETTE8_RGBA8_OES:
3032 case GL_PALETTE8_R5_G6_B5_OES:
3033 case GL_PALETTE8_RGBA4_OES:
3034 case GL_PALETTE8_RGB5_A1_OES:
3035 _mesa_cpal_compressed_teximage2d(target, level, internalFormat,
3036 width, height, imageSize, pixels);
3037 return;
3038 }
3039 }
3040
3041 if (compressed) {
3042 /* For glCompressedTexImage() the driver has no choice about the
3043 * texture format since we'll never transcode the user's compressed
3044 * image data. The internalFormat was error checked earlier.
3045 */
3046 texFormat = _mesa_glenum_to_compressed_format(internalFormat);
3047 }
3048 else {
3049 /* In case of HALF_FLOAT_OES or FLOAT_OES, find corresponding sized
3050 * internal floating point format for the given base format.
3051 */
3052 if (_mesa_is_gles(ctx) && format == internalFormat) {
3053 if (type == GL_FLOAT) {
3054 texObj->_IsFloat = GL_TRUE;
3055 } else if (type == GL_HALF_FLOAT_OES || type == GL_HALF_FLOAT) {
3056 texObj->_IsHalfFloat = GL_TRUE;
3057 }
3058
3059 internalFormat = adjust_for_oes_float_texture(ctx, format, type);
3060 }
3061
3062 texFormat = _mesa_choose_texture_format(ctx, texObj, target, level,
3063 internalFormat, format, type);
3064 }
3065
3066 assert(texFormat != MESA_FORMAT_NONE);
3067
3068 if (!no_error) {
3069 /* check that width, height, depth are legal for the mipmap level */
3070 dimensionsOK = _mesa_legal_texture_dimensions(ctx, target, level, width,
3071 height, depth, border);
3072
3073 /* check that the texture won't take too much memory, etc */
3074 sizeOK = ctx->Driver.TestProxyTexImage(ctx, proxy_target(target),
3075 0, level, texFormat, 1,
3076 width, height, depth);
3077 }
3078
3079 if (_mesa_is_proxy_texture(target)) {
3080 /* Proxy texture: just clear or set state depending on error checking */
3081 struct gl_texture_image *texImage =
3082 get_proxy_tex_image(ctx, target, level);
3083
3084 if (!texImage)
3085 return; /* GL_OUT_OF_MEMORY already recorded */
3086
3087 if (dimensionsOK && sizeOK) {
3088 _mesa_init_teximage_fields(ctx, texImage, width, height, depth,
3089 border, internalFormat, texFormat);
3090 }
3091 else {
3092 clear_teximage_fields(texImage);
3093 }
3094 }
3095 else {
3096 /* non-proxy target */
3097 const GLuint face = _mesa_tex_target_to_face(target);
3098 struct gl_texture_image *texImage;
3099
3100 if (!dimensionsOK) {
3101 _mesa_error(ctx, GL_INVALID_VALUE,
3102 "%s%uD(invalid width=%d or height=%d or depth=%d)",
3103 func, dims, width, height, depth);
3104 return;
3105 }
3106
3107 if (!sizeOK) {
3108 _mesa_error(ctx, GL_OUT_OF_MEMORY,
3109 "%s%uD(image too large: %d x %d x %d, %s format)",
3110 func, dims, width, height, depth,
3111 _mesa_enum_to_string(internalFormat));
3112 return;
3113 }
3114
3115 /* Allow a hardware driver to just strip out the border, to provide
3116 * reliable but slightly incorrect hardware rendering instead of
3117 * rarely-tested software fallback rendering.
3118 */
3119 if (border && ctx->Const.StripTextureBorder) {
3120 strip_texture_border(target, &width, &height, &depth, unpack,
3121 &unpack_no_border);
3122 border = 0;
3123 unpack = &unpack_no_border;
3124 }
3125
3126 if (ctx->NewState & _NEW_PIXEL)
3127 _mesa_update_state(ctx);
3128
3129 _mesa_lock_texture(ctx, texObj);
3130 {
3131 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
3132
3133 if (!texImage) {
3134 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s%uD", func, dims);
3135 }
3136 else {
3137 ctx->Driver.FreeTextureImageBuffer(ctx, texImage);
3138
3139 _mesa_init_teximage_fields(ctx, texImage,
3140 width, height, depth,
3141 border, internalFormat, texFormat);
3142
3143 /* Give the texture to the driver. <pixels> may be null. */
3144 if (width > 0 && height > 0 && depth > 0) {
3145 if (compressed) {
3146 ctx->Driver.CompressedTexImage(ctx, dims, texImage,
3147 imageSize, pixels);
3148 }
3149 else {
3150 ctx->Driver.TexImage(ctx, dims, texImage, format,
3151 type, pixels, unpack);
3152 }
3153 }
3154
3155 check_gen_mipmap(ctx, target, texObj, level);
3156
3157 _mesa_update_fbo_texture(ctx, texObj, face, level);
3158
3159 _mesa_dirty_texobj(ctx, texObj);
3160 }
3161 }
3162 _mesa_unlock_texture(ctx, texObj);
3163 }
3164 }
3165
3166
3167 /* This is a wrapper around teximage() so that we can force the KHR_no_error
3168 * logic to be inlined without inlining the function into all the callers.
3169 */
3170 static void
3171 teximage_err(struct gl_context *ctx, GLboolean compressed, GLuint dims,
3172 GLenum target, GLint level, GLint internalFormat,
3173 GLsizei width, GLsizei height, GLsizei depth,
3174 GLint border, GLenum format, GLenum type,
3175 GLsizei imageSize, const GLvoid *pixels)
3176 {
3177 teximage(ctx, compressed, dims, NULL, target, level, internalFormat, width, height,
3178 depth, border, format, type, imageSize, pixels, false);
3179 }
3180
3181
3182 static void
3183 teximage_no_error(struct gl_context *ctx, GLboolean compressed, GLuint dims,
3184 GLenum target, GLint level, GLint internalFormat,
3185 GLsizei width, GLsizei height, GLsizei depth,
3186 GLint border, GLenum format, GLenum type,
3187 GLsizei imageSize, const GLvoid *pixels)
3188 {
3189 teximage(ctx, compressed, dims, NULL, target, level, internalFormat, width, height,
3190 depth, border, format, type, imageSize, pixels, true);
3191 }
3192
3193
3194 /*
3195 * Called from the API. Note that width includes the border.
3196 */
3197 void GLAPIENTRY
3198 _mesa_TexImage1D( GLenum target, GLint level, GLint internalFormat,
3199 GLsizei width, GLint border, GLenum format,
3200 GLenum type, const GLvoid *pixels )
3201 {
3202 GET_CURRENT_CONTEXT(ctx);
3203 teximage_err(ctx, GL_FALSE, 1, target, level, internalFormat, width, 1, 1,
3204 border, format, type, 0, pixels);
3205 }
3206
3207 void GLAPIENTRY
3208 _mesa_TextureImage1DEXT(GLuint texture, GLenum target, GLint level,
3209 GLint internalFormat, GLsizei width, GLint border,
3210 GLenum format, GLenum type, const GLvoid *pixels )
3211 {
3212 struct gl_texture_object* texObj;
3213 GET_CURRENT_CONTEXT(ctx);
3214
3215 texObj = _mesa_lookup_or_create_texture(ctx, target, texture, false, true,
3216 "glTextureImage1DEXT");
3217 if (!texObj)
3218 return;
3219 teximage(ctx, GL_FALSE, 1, texObj, target, level, internalFormat,
3220 width, 1, 1, border, format, type, 0, pixels, false);
3221 }
3222
3223 void GLAPIENTRY
3224 _mesa_MultiTexImage1DEXT(GLenum texunit, GLenum target, GLint level,
3225 GLint internalFormat, GLsizei width, GLint border,
3226 GLenum format, GLenum type, const GLvoid *pixels )
3227 {
3228 struct gl_texture_object* texObj;
3229 GET_CURRENT_CONTEXT(ctx);
3230
3231 texObj = _mesa_get_texobj_by_target_and_texunit(ctx, target,
3232 texunit - GL_TEXTURE0,
3233 true,
3234 "glMultiTexImage1DEXT");
3235 if (!texObj)
3236 return;
3237 teximage(ctx, GL_FALSE, 1, texObj, target, level, internalFormat, width, 1, 1,
3238 border, format, type, 0, pixels, false);
3239 }
3240
3241 void GLAPIENTRY
3242 _mesa_TexImage2D( GLenum target, GLint level, GLint internalFormat,
3243 GLsizei width, GLsizei height, GLint border,
3244 GLenum format, GLenum type,
3245 const GLvoid *pixels )
3246 {
3247 GET_CURRENT_CONTEXT(ctx);
3248 teximage_err(ctx, GL_FALSE, 2, target, level, internalFormat, width, height, 1,
3249 border, format, type, 0, pixels);
3250 }
3251
3252 void GLAPIENTRY
3253 _mesa_TextureImage2DEXT(GLuint texture, GLenum target, GLint level,
3254 GLint internalFormat, GLsizei width, GLsizei height,
3255 GLint border,
3256 GLenum format, GLenum type, const GLvoid *pixels )
3257 {
3258 struct gl_texture_object* texObj;
3259 GET_CURRENT_CONTEXT(ctx);
3260
3261 texObj = _mesa_lookup_or_create_texture(ctx, target, texture, false, true,
3262 "glTextureImage2DEXT");
3263 if (!texObj)
3264 return;
3265 teximage(ctx, GL_FALSE, 2, texObj, target, level, internalFormat,
3266 width, height, 1, border, format, type, 0, pixels, false);
3267 }
3268
3269 void GLAPIENTRY
3270 _mesa_MultiTexImage2DEXT(GLenum texunit, GLenum target, GLint level,
3271 GLint internalFormat, GLsizei width, GLsizei height,
3272 GLint border,
3273 GLenum format, GLenum type, const GLvoid *pixels )
3274 {
3275 struct gl_texture_object* texObj;
3276 GET_CURRENT_CONTEXT(ctx);
3277
3278 texObj = _mesa_get_texobj_by_target_and_texunit(ctx, target,
3279 texunit - GL_TEXTURE0,
3280 true,
3281 "glMultiTexImage2DEXT");
3282 if (!texObj)
3283 return;
3284 teximage(ctx, GL_FALSE, 2, texObj, target, level, internalFormat, width, height, 1,
3285 border, format, type, 0, pixels, false);
3286 }
3287
3288 /*
3289 * Called by the API or display list executor.
3290 * Note that width and height include the border.
3291 */
3292 void GLAPIENTRY
3293 _mesa_TexImage3D( GLenum target, GLint level, GLint internalFormat,
3294 GLsizei width, GLsizei height, GLsizei depth,
3295 GLint border, GLenum format, GLenum type,
3296 const GLvoid *pixels )
3297 {
3298 GET_CURRENT_CONTEXT(ctx);
3299 teximage_err(ctx, GL_FALSE, 3, target, level, internalFormat,
3300 width, height, depth, border, format, type, 0, pixels);
3301 }
3302
3303 void GLAPIENTRY
3304 _mesa_TextureImage3DEXT(GLuint texture, GLenum target, GLint level,
3305 GLint internalFormat, GLsizei width, GLsizei height,
3306 GLsizei depth, GLint border,
3307 GLenum format, GLenum type, const GLvoid *pixels )
3308 {
3309 struct gl_texture_object* texObj;
3310 GET_CURRENT_CONTEXT(ctx);
3311
3312 texObj = _mesa_lookup_or_create_texture(ctx, target, texture, false, true,
3313 "glTextureImage3DEXT");
3314 if (!texObj)
3315 return;
3316 teximage(ctx, GL_FALSE, 3, texObj, target, level, internalFormat,
3317 width, height, depth, border, format, type, 0, pixels, false);
3318 }
3319
3320
3321 void GLAPIENTRY
3322 _mesa_MultiTexImage3DEXT(GLenum texunit, GLenum target, GLint level,
3323 GLint internalFormat, GLsizei width, GLsizei height,
3324 GLsizei depth, GLint border, GLenum format, GLenum type,
3325 const GLvoid *pixels )
3326 {
3327 struct gl_texture_object* texObj;
3328 GET_CURRENT_CONTEXT(ctx);
3329
3330 texObj = _mesa_get_texobj_by_target_and_texunit(ctx, target,
3331 texunit - GL_TEXTURE0,
3332 true,
3333 "glMultiTexImage3DEXT");
3334 if (!texObj)
3335 return;
3336 teximage(ctx, GL_FALSE, 3, texObj, target, level, internalFormat,
3337 width, height, depth, border, format, type, 0, pixels, false);
3338 }
3339
3340
3341 void GLAPIENTRY
3342 _mesa_TexImage3DEXT( GLenum target, GLint level, GLenum internalFormat,
3343 GLsizei width, GLsizei height, GLsizei depth,
3344 GLint border, GLenum format, GLenum type,
3345 const GLvoid *pixels )
3346 {
3347 _mesa_TexImage3D(target, level, (GLint) internalFormat, width, height,
3348 depth, border, format, type, pixels);
3349 }
3350
3351
3352 void GLAPIENTRY
3353 _mesa_TexImage1D_no_error(GLenum target, GLint level, GLint internalFormat,
3354 GLsizei width, GLint border, GLenum format,
3355 GLenum type, const GLvoid *pixels)
3356 {
3357 GET_CURRENT_CONTEXT(ctx);
3358 teximage_no_error(ctx, GL_FALSE, 1, target, level, internalFormat, width, 1,
3359 1, border, format, type, 0, pixels);
3360 }
3361
3362
3363 void GLAPIENTRY
3364 _mesa_TexImage2D_no_error(GLenum target, GLint level, GLint internalFormat,
3365 GLsizei width, GLsizei height, GLint border,
3366 GLenum format, GLenum type, const GLvoid *pixels)
3367 {
3368 GET_CURRENT_CONTEXT(ctx);
3369 teximage_no_error(ctx, GL_FALSE, 2, target, level, internalFormat, width,
3370 height, 1, border, format, type, 0, pixels);
3371 }
3372
3373
3374 void GLAPIENTRY
3375 _mesa_TexImage3D_no_error(GLenum target, GLint level, GLint internalFormat,
3376 GLsizei width, GLsizei height, GLsizei depth,
3377 GLint border, GLenum format, GLenum type,
3378 const GLvoid *pixels )
3379 {
3380 GET_CURRENT_CONTEXT(ctx);
3381 teximage_no_error(ctx, GL_FALSE, 3, target, level, internalFormat,
3382 width, height, depth, border, format, type, 0, pixels);
3383 }
3384
3385 /*
3386 * Helper used by __mesa_EGLImageTargetTexture2DOES and
3387 * _mesa_EGLImageTargetTexStorageEXT.
3388 */
3389 static void
3390 egl_image_target_texture(struct gl_context *ctx,
3391 struct gl_texture_object *texObj, GLenum target,
3392 GLeglImageOES image, bool tex_storage,
3393 const char *caller)
3394 {
3395 struct gl_texture_image *texImage;
3396 bool valid_target;
3397 FLUSH_VERTICES(ctx, 0);
3398
3399 switch (target) {
3400 case GL_TEXTURE_2D:
3401 valid_target = _mesa_has_OES_EGL_image(ctx) ||
3402 (tex_storage && _mesa_has_EXT_EGL_image_storage(ctx));
3403 break;
3404 case GL_TEXTURE_EXTERNAL_OES:
3405 valid_target =
3406 _mesa_is_gles(ctx) ? _mesa_has_OES_EGL_image_external(ctx) : false;
3407 break;
3408 default:
3409 valid_target = false;
3410 break;
3411 }
3412
3413 if (!valid_target) {
3414 _mesa_error(ctx, GL_INVALID_ENUM, "%s(target=%d)", caller, target);
3415 return;
3416 }
3417
3418 if (!image) {
3419 _mesa_error(ctx, GL_INVALID_VALUE, "%s(image=%p)", caller, image);
3420 return;
3421 }
3422
3423 if (ctx->NewState & _NEW_PIXEL)
3424 _mesa_update_state(ctx);
3425
3426 _mesa_lock_texture(ctx, texObj);
3427
3428 if (texObj->Immutable) {
3429 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(texture is immutable)", caller);
3430 _mesa_unlock_texture(ctx, texObj);
3431 return;
3432 }
3433
3434 texImage = _mesa_get_tex_image(ctx, texObj, target, 0);
3435 if (!texImage) {
3436 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", caller);
3437 } else {
3438 ctx->Driver.FreeTextureImageBuffer(ctx, texImage);
3439
3440 if (tex_storage) {
3441 ctx->Driver.EGLImageTargetTexStorage(ctx, target, texObj, texImage,
3442 image);
3443 } else {
3444 ctx->Driver.EGLImageTargetTexture2D(ctx, target, texObj, texImage,
3445 image);
3446 }
3447
3448 _mesa_dirty_texobj(ctx, texObj);
3449 }
3450
3451 if (tex_storage)
3452 _mesa_set_texture_view_state(ctx, texObj, target, 1);
3453
3454 _mesa_unlock_texture(ctx, texObj);
3455 }
3456
3457 void GLAPIENTRY
3458 _mesa_EGLImageTargetTexture2DOES(GLenum target, GLeglImageOES image)
3459 {
3460 struct gl_texture_object *texObj;
3461 const char *func = "glEGLImageTargetTexture2D";
3462 GET_CURRENT_CONTEXT(ctx);
3463
3464 texObj = _mesa_get_current_tex_object(ctx, target);
3465 if (!texObj) {
3466 _mesa_error(ctx, GL_INVALID_ENUM, "%s(target=%d)", func, target);
3467 return;
3468 }
3469
3470 egl_image_target_texture(ctx, texObj, target, image, false, func);
3471 }
3472
3473 static void
3474 egl_image_target_texture_storage(struct gl_context *ctx,
3475 struct gl_texture_object *texObj, GLenum target,
3476 GLeglImageOES image, const GLint *attrib_list,
3477 const char *caller)
3478 {
3479 /*
3480 * EXT_EGL_image_storage:
3481 *
3482 * "<attrib_list> must be NULL or a pointer to the value GL_NONE."
3483 */
3484 if (attrib_list && attrib_list[0] != GL_NONE) {
3485 _mesa_error(ctx, GL_INVALID_VALUE, "%s(image=%p)", caller, image);
3486 return;
3487 }
3488
3489 switch (target) {
3490 case GL_TEXTURE_2D:
3491 case GL_TEXTURE_EXTERNAL_OES:
3492 break;
3493 default:
3494 /*
3495 * The EXT_EGL_image_storage spec allows for many other targets besides
3496 * GL_TEXTURE_2D and GL_TEXTURE_EXTERNAL_OES, however these are complicated
3497 * to implement.
3498 */
3499 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(unsupported target=%d)",
3500 caller, target);
3501 return;
3502 }
3503
3504 egl_image_target_texture(ctx, texObj, target, image, true, caller);
3505 }
3506
3507
3508 void GLAPIENTRY
3509 _mesa_EGLImageTargetTexStorageEXT(GLenum target, GLeglImageOES image,
3510 const GLint *attrib_list)
3511 {
3512 struct gl_texture_object *texObj;
3513 const char *func = "glEGLImageTargetTexStorageEXT";
3514 GET_CURRENT_CONTEXT(ctx);
3515
3516 texObj = _mesa_get_current_tex_object(ctx, target);
3517 if (!texObj) {
3518 _mesa_error(ctx, GL_INVALID_ENUM, "%s(target=%d)", func, target);
3519 return;
3520 }
3521
3522 egl_image_target_texture_storage(ctx, texObj, target, image, attrib_list,
3523 func);
3524 }
3525
3526 void GLAPIENTRY
3527 _mesa_EGLImageTargetTextureStorageEXT(GLuint texture, GLeglImageOES image,
3528 const GLint *attrib_list)
3529 {
3530 struct gl_texture_object *texObj;
3531 const char *func = "glEGLImageTargetTextureStorageEXT";
3532 GET_CURRENT_CONTEXT(ctx);
3533
3534 if (!(_mesa_is_desktop_gl(ctx) && ctx->Version >= 45) &&
3535 !_mesa_has_ARB_direct_state_access(ctx) &&
3536 !_mesa_has_EXT_direct_state_access(ctx)) {
3537 _mesa_error(ctx, GL_INVALID_OPERATION, "direct access not supported");
3538 return;
3539 }
3540
3541 texObj = _mesa_lookup_texture_err(ctx, texture, func);
3542 if (!texObj)
3543 return;
3544
3545 egl_image_target_texture_storage(ctx, texObj, texObj->Target, image,
3546 attrib_list, func);
3547 }
3548
3549 /**
3550 * Helper that implements the glTexSubImage1/2/3D()
3551 * and glTextureSubImage1/2/3D() functions.
3552 */
3553 static void
3554 texture_sub_image(struct gl_context *ctx, GLuint dims,
3555 struct gl_texture_object *texObj,
3556 struct gl_texture_image *texImage,
3557 GLenum target, GLint level,
3558 GLint xoffset, GLint yoffset, GLint zoffset,
3559 GLsizei width, GLsizei height, GLsizei depth,
3560 GLenum format, GLenum type, const GLvoid *pixels)
3561 {
3562 FLUSH_VERTICES(ctx, 0);
3563
3564 if (ctx->NewState & _NEW_PIXEL)
3565 _mesa_update_state(ctx);
3566
3567 _mesa_lock_texture(ctx, texObj);
3568 {
3569 if (width > 0 && height > 0 && depth > 0) {
3570 /* If we have a border, offset=-1 is legal. Bias by border width. */
3571 switch (dims) {
3572 case 3:
3573 if (target != GL_TEXTURE_2D_ARRAY)
3574 zoffset += texImage->Border;
3575 /* fall-through */
3576 case 2:
3577 if (target != GL_TEXTURE_1D_ARRAY)
3578 yoffset += texImage->Border;
3579 /* fall-through */
3580 case 1:
3581 xoffset += texImage->Border;
3582 }
3583
3584 ctx->Driver.TexSubImage(ctx, dims, texImage,
3585 xoffset, yoffset, zoffset,
3586 width, height, depth,
3587 format, type, pixels, &ctx->Unpack);
3588
3589 check_gen_mipmap(ctx, target, texObj, level);
3590
3591 /* NOTE: Don't signal _NEW_TEXTURE_OBJECT since we've only changed
3592 * the texel data, not the texture format, size, etc.
3593 */
3594 }
3595 }
3596 _mesa_unlock_texture(ctx, texObj);
3597 }
3598
3599 /**
3600 * Implement all the glTexSubImage1/2/3D() functions.
3601 * Must split this out this way because of GL_TEXTURE_CUBE_MAP.
3602 */
3603 static void
3604 texsubimage_err(struct gl_context *ctx, GLuint dims, GLenum target, GLint level,
3605 GLint xoffset, GLint yoffset, GLint zoffset,
3606 GLsizei width, GLsizei height, GLsizei depth,
3607 GLenum format, GLenum type, const GLvoid *pixels,
3608 const char *callerName)
3609 {
3610 struct gl_texture_object *texObj;
3611 struct gl_texture_image *texImage;
3612
3613 /* check target (proxies not allowed) */
3614 if (!legal_texsubimage_target(ctx, dims, target, false)) {
3615 _mesa_error(ctx, GL_INVALID_ENUM, "glTexSubImage%uD(target=%s)",
3616 dims, _mesa_enum_to_string(target));
3617 return;
3618 }
3619
3620 texObj = _mesa_get_current_tex_object(ctx, target);
3621 if (!texObj)
3622 return;
3623
3624 if (texsubimage_error_check(ctx, dims, texObj, target, level,
3625 xoffset, yoffset, zoffset,
3626 width, height, depth, format, type,
3627 pixels, callerName)) {
3628 return; /* error was detected */
3629 }
3630
3631 texImage = _mesa_select_tex_image(texObj, target, level);
3632 /* texsubimage_error_check ensures that texImage is not NULL */
3633
3634 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
3635 _mesa_debug(ctx, "glTexSubImage%uD %s %d %d %d %d %d %d %d %s %s %p\n",
3636 dims,
3637 _mesa_enum_to_string(target), level,
3638 xoffset, yoffset, zoffset, width, height, depth,
3639 _mesa_enum_to_string(format),
3640 _mesa_enum_to_string(type), pixels);
3641
3642 texture_sub_image(ctx, dims, texObj, texImage, target, level,
3643 xoffset, yoffset, zoffset, width, height, depth,
3644 format, type, pixels);
3645 }
3646
3647
3648 static void
3649 texsubimage(struct gl_context *ctx, GLuint dims, GLenum target, GLint level,
3650 GLint xoffset, GLint yoffset, GLint zoffset,
3651 GLsizei width, GLsizei height, GLsizei depth,
3652 GLenum format, GLenum type, const GLvoid *pixels)
3653 {
3654 struct gl_texture_object *texObj;
3655 struct gl_texture_image *texImage;
3656
3657 texObj = _mesa_get_current_tex_object(ctx, target);
3658 texImage = _mesa_select_tex_image(texObj, target, level);
3659
3660 texture_sub_image(ctx, dims, texObj, texImage, target, level,
3661 xoffset, yoffset, zoffset, width, height, depth,
3662 format, type, pixels);
3663 }
3664
3665
3666 /**
3667 * Implement all the glTextureSubImage1/2/3D() functions.
3668 * Must split this out this way because of GL_TEXTURE_CUBE_MAP.
3669 */
3670 static ALWAYS_INLINE void
3671 texturesubimage(struct gl_context *ctx, GLuint dims,
3672 GLuint texture, GLenum target, GLint level,
3673 GLint xoffset, GLint yoffset, GLint zoffset,
3674 GLsizei width, GLsizei height, GLsizei depth,
3675 GLenum format, GLenum type, const GLvoid *pixels,
3676 const char *callerName, bool no_error, bool ext_dsa)
3677 {
3678 struct gl_texture_object *texObj;
3679 struct gl_texture_image *texImage;
3680 int i;
3681
3682 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
3683 _mesa_debug(ctx,
3684 "glTextureSubImage%uD %d %d %d %d %d %d %d %d %s %s %p\n",
3685 dims, texture, level,
3686 xoffset, yoffset, zoffset, width, height, depth,
3687 _mesa_enum_to_string(format),
3688 _mesa_enum_to_string(type), pixels);
3689
3690 /* Get the texture object by Name. */
3691 if (!no_error) {
3692 if (!ext_dsa) {
3693 texObj = _mesa_lookup_texture_err(ctx, texture, callerName);
3694 } else {
3695 texObj = lookup_texture_ext_dsa(ctx, target, texture, callerName);
3696 }
3697 if (!texObj)
3698 return;
3699 } else {
3700 texObj = _mesa_lookup_texture(ctx, texture);
3701 }
3702
3703 if (!no_error) {
3704 /* check target (proxies not allowed) */
3705 if (!legal_texsubimage_target(ctx, dims, texObj->Target, true)) {
3706 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(target=%s)",
3707 callerName, _mesa_enum_to_string(texObj->Target));
3708 return;
3709 }
3710
3711 if (texsubimage_error_check(ctx, dims, texObj, texObj->Target, level,
3712 xoffset, yoffset, zoffset,
3713 width, height, depth, format, type,
3714 pixels, callerName)) {
3715 return; /* error was detected */
3716 }
3717 }
3718
3719 /* Must handle special case GL_TEXTURE_CUBE_MAP. */
3720 if (texObj->Target == GL_TEXTURE_CUBE_MAP) {
3721 GLint imageStride;
3722
3723 /*
3724 * What do we do if the user created a texture with the following code
3725 * and then called this function with its handle?
3726 *
3727 * GLuint tex;
3728 * glCreateTextures(GL_TEXTURE_CUBE_MAP, 1, &tex);
3729 * glBindTexture(GL_TEXTURE_CUBE_MAP, tex);
3730 * glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, ...);
3731 * glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, ...);
3732 * glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, ...);
3733 * // Note: GL_TEXTURE_CUBE_MAP_NEGATIVE_Y not set, or given the
3734 * // wrong format, or given the wrong size, etc.
3735 * glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, ...);
3736 * glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, ...);
3737 *
3738 * A bug has been filed against the spec for this case. In the
3739 * meantime, we will check for cube completeness.
3740 *
3741 * According to Section 8.17 Texture Completeness in the OpenGL 4.5
3742 * Core Profile spec (30.10.2014):
3743 * "[A] cube map texture is cube complete if the
3744 * following conditions all hold true: The [base level] texture
3745 * images of each of the six cube map faces have identical, positive,
3746 * and square dimensions. The [base level] images were each specified
3747 * with the same internal format."
3748 *
3749 * It seems reasonable to check for cube completeness of an arbitrary
3750 * level here so that the image data has a consistent format and size.
3751 */
3752 if (!no_error && !_mesa_cube_level_complete(texObj, level)) {
3753 _mesa_error(ctx, GL_INVALID_OPERATION,
3754 "glTextureSubImage%uD(cube map incomplete)",
3755 dims);
3756 return;
3757 }
3758
3759 imageStride = _mesa_image_image_stride(&ctx->Unpack, width, height,
3760 format, type);
3761 /* Copy in each face. */
3762 for (i = zoffset; i < zoffset + depth; ++i) {
3763 texImage = texObj->Image[i][level];
3764 assert(texImage);
3765
3766 texture_sub_image(ctx, 3, texObj, texImage, texObj->Target,
3767 level, xoffset, yoffset, 0,
3768 width, height, 1, format,
3769 type, pixels);
3770 pixels = (GLubyte *) pixels + imageStride;
3771 }
3772 }
3773 else {
3774 texImage = _mesa_select_tex_image(texObj, texObj->Target, level);
3775 assert(texImage);
3776
3777 texture_sub_image(ctx, dims, texObj, texImage, texObj->Target,
3778 level, xoffset, yoffset, zoffset,
3779 width, height, depth, format,
3780 type, pixels);
3781 }
3782 }
3783
3784
3785 static void
3786 texturesubimage_error(struct gl_context *ctx, GLuint dims,
3787 GLuint texture, GLenum target, GLint level,
3788 GLint xoffset, GLint yoffset, GLint zoffset,
3789 GLsizei width, GLsizei height, GLsizei depth,
3790 GLenum format, GLenum type, const GLvoid *pixels,
3791 const char *callerName, bool ext_dsa)
3792 {
3793 texturesubimage(ctx, dims, texture, target, level, xoffset, yoffset,
3794 zoffset, width, height, depth, format, type, pixels,
3795 callerName, false, ext_dsa);
3796 }
3797
3798
3799 static void
3800 texturesubimage_no_error(struct gl_context *ctx, GLuint dims,
3801 GLuint texture, GLenum target, GLint level,
3802 GLint xoffset, GLint yoffset, GLint zoffset,
3803 GLsizei width, GLsizei height, GLsizei depth,
3804 GLenum format, GLenum type, const GLvoid *pixels,
3805 const char *callerName, bool ext_dsa)
3806 {
3807 texturesubimage(ctx, dims, texture, target, level, xoffset, yoffset,
3808 zoffset, width, height, depth, format, type, pixels,
3809 callerName, true, ext_dsa);
3810 }
3811
3812
3813 void GLAPIENTRY
3814 _mesa_TexSubImage1D_no_error(GLenum target, GLint level,
3815 GLint xoffset, GLsizei width,
3816 GLenum format, GLenum type,
3817 const GLvoid *pixels)
3818 {
3819 GET_CURRENT_CONTEXT(ctx);
3820 texsubimage(ctx, 1, target, level,
3821 xoffset, 0, 0,
3822 width, 1, 1,
3823 format, type, pixels);
3824 }
3825
3826
3827 void GLAPIENTRY
3828 _mesa_TexSubImage1D( GLenum target, GLint level,
3829 GLint xoffset, GLsizei width,
3830 GLenum format, GLenum type,
3831 const GLvoid *pixels )
3832 {
3833 GET_CURRENT_CONTEXT(ctx);
3834 texsubimage_err(ctx, 1, target, level,
3835 xoffset, 0, 0,
3836 width, 1, 1,
3837 format, type, pixels, "glTexSubImage1D");
3838 }
3839
3840
3841 void GLAPIENTRY
3842 _mesa_TexSubImage2D_no_error(GLenum target, GLint level,
3843 GLint xoffset, GLint yoffset,
3844 GLsizei width, GLsizei height,
3845 GLenum format, GLenum type,
3846 const GLvoid *pixels)
3847 {
3848 GET_CURRENT_CONTEXT(ctx);
3849 texsubimage(ctx, 2, target, level,
3850 xoffset, yoffset, 0,
3851 width, height, 1,
3852 format, type, pixels);
3853 }
3854
3855
3856 void GLAPIENTRY
3857 _mesa_TexSubImage2D( GLenum target, GLint level,
3858 GLint xoffset, GLint yoffset,
3859 GLsizei width, GLsizei height,
3860 GLenum format, GLenum type,
3861 const GLvoid *pixels )
3862 {
3863 GET_CURRENT_CONTEXT(ctx);
3864 texsubimage_err(ctx, 2, target, level,
3865 xoffset, yoffset, 0,
3866 width, height, 1,
3867 format, type, pixels, "glTexSubImage2D");
3868 }
3869
3870
3871 void GLAPIENTRY
3872 _mesa_TexSubImage3D_no_error(GLenum target, GLint level,
3873 GLint xoffset, GLint yoffset, GLint zoffset,
3874 GLsizei width, GLsizei height, GLsizei depth,
3875 GLenum format, GLenum type,
3876 const GLvoid *pixels)
3877 {
3878 GET_CURRENT_CONTEXT(ctx);
3879 texsubimage(ctx, 3, target, level,
3880 xoffset, yoffset, zoffset,
3881 width, height, depth,
3882 format, type, pixels);
3883 }
3884
3885
3886 void GLAPIENTRY
3887 _mesa_TexSubImage3D( GLenum target, GLint level,
3888 GLint xoffset, GLint yoffset, GLint zoffset,
3889 GLsizei width, GLsizei height, GLsizei depth,
3890 GLenum format, GLenum type,
3891 const GLvoid *pixels )
3892 {
3893 GET_CURRENT_CONTEXT(ctx);
3894 texsubimage_err(ctx, 3, target, level,
3895 xoffset, yoffset, zoffset,
3896 width, height, depth,
3897 format, type, pixels, "glTexSubImage3D");
3898 }
3899
3900
3901 void GLAPIENTRY
3902 _mesa_TextureSubImage1D_no_error(GLuint texture, GLint level, GLint xoffset,
3903 GLsizei width, GLenum format, GLenum type,
3904 const GLvoid *pixels)
3905 {
3906 GET_CURRENT_CONTEXT(ctx);
3907 texturesubimage_no_error(ctx, 1, texture, 0, level, xoffset, 0, 0, width,
3908 1, 1, format, type, pixels, "glTextureSubImage1D",
3909 false);
3910 }
3911
3912
3913 void GLAPIENTRY
3914 _mesa_TextureSubImage1DEXT(GLuint texture, GLenum target, GLint level,
3915 GLint xoffset, GLsizei width,
3916 GLenum format, GLenum type,
3917 const GLvoid *pixels)
3918 {
3919 GET_CURRENT_CONTEXT(ctx);
3920 texturesubimage_error(ctx, 1, texture, target, level, xoffset, 0, 0, width, 1,
3921 1, format, type, pixels, "glTextureSubImage1DEXT",
3922 false);
3923 }
3924
3925
3926 void GLAPIENTRY
3927 _mesa_MultiTexSubImage1DEXT(GLenum texunit, GLenum target, GLint level,
3928 GLint xoffset, GLsizei width,
3929 GLenum format, GLenum type,
3930 const GLvoid *pixels)
3931 {
3932 GET_CURRENT_CONTEXT(ctx);
3933 struct gl_texture_object *texObj;
3934 struct gl_texture_image *texImage;
3935
3936 texObj = _mesa_get_texobj_by_target_and_texunit(ctx, target,
3937 texunit - GL_TEXTURE0,
3938 false,
3939 "glMultiTexImage1DEXT");
3940 texImage = _mesa_select_tex_image(texObj, target, level);
3941
3942 texture_sub_image(ctx, 1, texObj, texImage, target, level,
3943 xoffset, 0, 0, width, 1, 1,
3944 format, type, pixels);
3945 }
3946
3947
3948 void GLAPIENTRY
3949 _mesa_TextureSubImage1D(GLuint texture, GLint level,
3950 GLint xoffset, GLsizei width,
3951 GLenum format, GLenum type,
3952 const GLvoid *pixels)
3953 {
3954 GET_CURRENT_CONTEXT(ctx);
3955 texturesubimage_error(ctx, 1, texture, 0, level, xoffset, 0, 0, width, 1,
3956 1, format, type, pixels, "glTextureSubImage1D",
3957 false);
3958 }
3959
3960
3961 void GLAPIENTRY
3962 _mesa_TextureSubImage2D_no_error(GLuint texture, GLint level, GLint xoffset,
3963 GLint yoffset, GLsizei width, GLsizei height,
3964 GLenum format, GLenum type,
3965 const GLvoid *pixels)
3966 {
3967 GET_CURRENT_CONTEXT(ctx);
3968 texturesubimage_no_error(ctx, 2, texture, 0, level, xoffset, yoffset, 0,
3969 width, height, 1, format, type, pixels,
3970 "glTextureSubImage2D", false);
3971 }
3972
3973
3974 void GLAPIENTRY
3975 _mesa_TextureSubImage2DEXT(GLuint texture, GLenum target, GLint level,
3976 GLint xoffset, GLint yoffset, GLsizei width,
3977 GLsizei height, GLenum format, GLenum type,
3978 const GLvoid *pixels)
3979 {
3980 GET_CURRENT_CONTEXT(ctx);
3981 texturesubimage_error(ctx, 2, texture, target, level, xoffset, yoffset, 0,
3982 width, height, 1, format, type, pixels,
3983 "glTextureSubImage2DEXT", true);
3984 }
3985
3986
3987 void GLAPIENTRY
3988 _mesa_MultiTexSubImage2DEXT(GLenum texunit, GLenum target, GLint level,
3989 GLint xoffset, GLint yoffset, GLsizei width,
3990 GLsizei height, GLenum format, GLenum type,
3991 const GLvoid *pixels)
3992 {
3993 GET_CURRENT_CONTEXT(ctx);
3994 struct gl_texture_object *texObj;
3995 struct gl_texture_image *texImage;
3996
3997 texObj = _mesa_get_texobj_by_target_and_texunit(ctx, target,
3998 texunit - GL_TEXTURE0,
3999 false,
4000 "glMultiTexImage2DEXT");
4001 texImage = _mesa_select_tex_image(texObj, target, level);
4002
4003 texture_sub_image(ctx, 2, texObj, texImage, target, level,
4004 xoffset, yoffset, 0, width, height, 1,
4005 format, type, pixels);
4006 }
4007
4008
4009 void GLAPIENTRY
4010 _mesa_TextureSubImage2D(GLuint texture, GLint level,
4011 GLint xoffset, GLint yoffset,
4012 GLsizei width, GLsizei height,
4013 GLenum format, GLenum type,
4014 const GLvoid *pixels)
4015 {
4016 GET_CURRENT_CONTEXT(ctx);
4017 texturesubimage_error(ctx, 2, texture, 0, level, xoffset, yoffset, 0,
4018 width, height, 1, format, type, pixels,
4019 "glTextureSubImage2D", false);
4020 }
4021
4022
4023 void GLAPIENTRY
4024 _mesa_TextureSubImage3D_no_error(GLuint texture, GLint level, GLint xoffset,
4025 GLint yoffset, GLint zoffset, GLsizei width,
4026 GLsizei height, GLsizei depth, GLenum format,
4027 GLenum type, const GLvoid *pixels)
4028 {
4029 GET_CURRENT_CONTEXT(ctx);
4030 texturesubimage_no_error(ctx, 3, texture, 0, level, xoffset, yoffset,
4031 zoffset, width, height, depth, format, type,
4032 pixels, "glTextureSubImage3D", false);
4033 }
4034
4035
4036 void GLAPIENTRY
4037 _mesa_TextureSubImage3DEXT(GLuint texture, GLenum target, GLint level,
4038 GLint xoffset, GLint yoffset, GLint zoffset,
4039 GLsizei width, GLsizei height, GLsizei depth,
4040 GLenum format, GLenum type, const GLvoid *pixels)
4041 {
4042 GET_CURRENT_CONTEXT(ctx);
4043 texturesubimage_error(ctx, 3, texture, target, level, xoffset, yoffset,
4044 zoffset, width, height, depth, format, type,
4045 pixels, "glTextureSubImage3DEXT", true);
4046 }
4047
4048
4049 void GLAPIENTRY
4050 _mesa_MultiTexSubImage3DEXT(GLenum texunit, GLenum target, GLint level,
4051 GLint xoffset, GLint yoffset, GLint zoffset,
4052 GLsizei width, GLsizei height, GLsizei depth,
4053 GLenum format, GLenum type, const GLvoid *pixels)
4054 {
4055 GET_CURRENT_CONTEXT(ctx);
4056 struct gl_texture_object *texObj;
4057 struct gl_texture_image *texImage;
4058
4059 texObj = _mesa_get_texobj_by_target_and_texunit(ctx, target,
4060 texunit - GL_TEXTURE0,
4061 false,
4062 "glMultiTexImage3DEXT");
4063 texImage = _mesa_select_tex_image(texObj, target, level);
4064
4065 texture_sub_image(ctx, 3, texObj, texImage, target, level,
4066 xoffset, yoffset, zoffset, width, height, depth,
4067 format, type, pixels);
4068 }
4069
4070
4071 void GLAPIENTRY
4072 _mesa_TextureSubImage3D(GLuint texture, GLint level,
4073 GLint xoffset, GLint yoffset, GLint zoffset,
4074 GLsizei width, GLsizei height, GLsizei depth,
4075 GLenum format, GLenum type,
4076 const GLvoid *pixels)
4077 {
4078 GET_CURRENT_CONTEXT(ctx);
4079 texturesubimage_error(ctx, 3, texture, 0, level, xoffset, yoffset, zoffset,
4080 width, height, depth, format, type, pixels,
4081 "glTextureSubImage3D", false);
4082 }
4083
4084
4085 /**
4086 * For glCopyTexSubImage, return the source renderbuffer to copy texel data
4087 * from. This depends on whether the texture contains color or depth values.
4088 */
4089 static struct gl_renderbuffer *
4090 get_copy_tex_image_source(struct gl_context *ctx, mesa_format texFormat)
4091 {
4092 if (_mesa_get_format_bits(texFormat, GL_DEPTH_BITS) > 0) {
4093 /* reading from depth/stencil buffer */
4094 return ctx->ReadBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;
4095 } else if (_mesa_get_format_bits(texFormat, GL_STENCIL_BITS) > 0) {
4096 return ctx->ReadBuffer->Attachment[BUFFER_STENCIL].Renderbuffer;
4097 } else {
4098 /* copying from color buffer */
4099 return ctx->ReadBuffer->_ColorReadBuffer;
4100 }
4101 }
4102
4103
4104 static void
4105 copytexsubimage_by_slice(struct gl_context *ctx,
4106 struct gl_texture_image *texImage,
4107 GLuint dims,
4108 GLint xoffset, GLint yoffset, GLint zoffset,
4109 struct gl_renderbuffer *rb,
4110 GLint x, GLint y,
4111 GLsizei width, GLsizei height)
4112 {
4113 if (texImage->TexObject->Target == GL_TEXTURE_1D_ARRAY) {
4114 int slice;
4115
4116 /* For 1D arrays, we copy each scanline of the source rectangle into the
4117 * next array slice.
4118 */
4119 assert(zoffset == 0);
4120
4121 for (slice = 0; slice < height; slice++) {
4122 assert(yoffset + slice < texImage->Height);
4123 ctx->Driver.CopyTexSubImage(ctx, 2, texImage,
4124 xoffset, 0, yoffset + slice,
4125 rb, x, y + slice, width, 1);
4126 }
4127 } else {
4128 ctx->Driver.CopyTexSubImage(ctx, dims, texImage,
4129 xoffset, yoffset, zoffset,
4130 rb, x, y, width, height);
4131 }
4132 }
4133
4134
4135 static GLboolean
4136 formats_differ_in_component_sizes(mesa_format f1, mesa_format f2)
4137 {
4138 GLint f1_r_bits = _mesa_get_format_bits(f1, GL_RED_BITS);
4139 GLint f1_g_bits = _mesa_get_format_bits(f1, GL_GREEN_BITS);
4140 GLint f1_b_bits = _mesa_get_format_bits(f1, GL_BLUE_BITS);
4141 GLint f1_a_bits = _mesa_get_format_bits(f1, GL_ALPHA_BITS);
4142
4143 GLint f2_r_bits = _mesa_get_format_bits(f2, GL_RED_BITS);
4144 GLint f2_g_bits = _mesa_get_format_bits(f2, GL_GREEN_BITS);
4145 GLint f2_b_bits = _mesa_get_format_bits(f2, GL_BLUE_BITS);
4146 GLint f2_a_bits = _mesa_get_format_bits(f2, GL_ALPHA_BITS);
4147
4148 if ((f1_r_bits && f2_r_bits && f1_r_bits != f2_r_bits)
4149 || (f1_g_bits && f2_g_bits && f1_g_bits != f2_g_bits)
4150 || (f1_b_bits && f2_b_bits && f1_b_bits != f2_b_bits)
4151 || (f1_a_bits && f2_a_bits && f1_a_bits != f2_a_bits))
4152 return GL_TRUE;
4153
4154 return GL_FALSE;
4155 }
4156
4157
4158 /**
4159 * Check if the given texture format and size arguments match those
4160 * of the texture image.
4161 * \param return true if arguments match, false otherwise.
4162 */
4163 static bool
4164 can_avoid_reallocation(const struct gl_texture_image *texImage,
4165 GLenum internalFormat,
4166 mesa_format texFormat, GLsizei width,
4167 GLsizei height, GLint border)
4168 {
4169 if (texImage->InternalFormat != internalFormat)
4170 return false;
4171 if (texImage->TexFormat != texFormat)
4172 return false;
4173 if (texImage->Border != border)
4174 return false;
4175 if (texImage->Width2 != width)
4176 return false;
4177 if (texImage->Height2 != height)
4178 return false;
4179 return true;
4180 }
4181
4182
4183 /**
4184 * Implementation for glCopyTex(ture)SubImage1/2/3D() functions.
4185 */
4186 static void
4187 copy_texture_sub_image(struct gl_context *ctx, GLuint dims,
4188 struct gl_texture_object *texObj,
4189 GLenum target, GLint level,
4190 GLint xoffset, GLint yoffset, GLint zoffset,
4191 GLint x, GLint y, GLsizei width, GLsizei height)
4192 {
4193 struct gl_texture_image *texImage;
4194
4195 _mesa_lock_texture(ctx, texObj);
4196
4197 texImage = _mesa_select_tex_image(texObj, target, level);
4198
4199 /* If we have a border, offset=-1 is legal. Bias by border width. */
4200 switch (dims) {
4201 case 3:
4202 if (target != GL_TEXTURE_2D_ARRAY)
4203 zoffset += texImage->Border;
4204 /* fall-through */
4205 case 2:
4206 if (target != GL_TEXTURE_1D_ARRAY)
4207 yoffset += texImage->Border;
4208 /* fall-through */
4209 case 1:
4210 xoffset += texImage->Border;
4211 }
4212
4213 if (ctx->Const.NoClippingOnCopyTex ||
4214 _mesa_clip_copytexsubimage(ctx, &xoffset, &yoffset, &x, &y,
4215 &width, &height)) {
4216 struct gl_renderbuffer *srcRb =
4217 get_copy_tex_image_source(ctx, texImage->TexFormat);
4218
4219 copytexsubimage_by_slice(ctx, texImage, dims, xoffset, yoffset, zoffset,
4220 srcRb, x, y, width, height);
4221
4222 check_gen_mipmap(ctx, target, texObj, level);
4223
4224 /* NOTE: Don't signal _NEW_TEXTURE_OBJECT since we've only changed
4225 * the texel data, not the texture format, size, etc.
4226 */
4227 }
4228
4229 _mesa_unlock_texture(ctx, texObj);
4230 }
4231
4232
4233 static void
4234 copy_texture_sub_image_err(struct gl_context *ctx, GLuint dims,
4235 struct gl_texture_object *texObj,
4236 GLenum target, GLint level,
4237 GLint xoffset, GLint yoffset, GLint zoffset,
4238 GLint x, GLint y, GLsizei width, GLsizei height,
4239 const char *caller)
4240 {
4241 FLUSH_VERTICES(ctx, 0);
4242
4243 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
4244 _mesa_debug(ctx, "%s %s %d %d %d %d %d %d %d %d\n", caller,
4245 _mesa_enum_to_string(target),
4246 level, xoffset, yoffset, zoffset, x, y, width, height);
4247
4248 if (ctx->NewState & NEW_COPY_TEX_STATE)
4249 _mesa_update_state(ctx);
4250
4251 if (copytexsubimage_error_check(ctx, dims, texObj, target, level,
4252 xoffset, yoffset, zoffset,
4253 width, height, caller)) {
4254 return;
4255 }
4256
4257 copy_texture_sub_image(ctx, dims, texObj, target, level, xoffset, yoffset,
4258 zoffset, x, y, width, height);
4259 }
4260
4261
4262 static void
4263 copy_texture_sub_image_no_error(struct gl_context *ctx, GLuint dims,
4264 struct gl_texture_object *texObj,
4265 GLenum target, GLint level,
4266 GLint xoffset, GLint yoffset, GLint zoffset,
4267 GLint x, GLint y, GLsizei width, GLsizei height)
4268 {
4269 FLUSH_VERTICES(ctx, 0);
4270
4271 if (ctx->NewState & NEW_COPY_TEX_STATE)
4272 _mesa_update_state(ctx);
4273
4274 copy_texture_sub_image(ctx, dims, texObj, target, level, xoffset, yoffset,
4275 zoffset, x, y, width, height);
4276 }
4277
4278
4279 /**
4280 * Implement the glCopyTexImage1/2D() functions.
4281 */
4282 static ALWAYS_INLINE void
4283 copyteximage(struct gl_context *ctx, GLuint dims, struct gl_texture_object *texObj,
4284 GLenum target, GLint level, GLenum internalFormat,
4285 GLint x, GLint y, GLsizei width, GLsizei height, GLint border,
4286 bool no_error)
4287 {
4288 struct gl_texture_image *texImage;
4289 mesa_format texFormat;
4290
4291 FLUSH_VERTICES(ctx, 0);
4292
4293 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
4294 _mesa_debug(ctx, "glCopyTexImage%uD %s %d %s %d %d %d %d %d\n",
4295 dims,
4296 _mesa_enum_to_string(target), level,
4297 _mesa_enum_to_string(internalFormat),
4298 x, y, width, height, border);
4299
4300 if (ctx->NewState & NEW_COPY_TEX_STATE)
4301 _mesa_update_state(ctx);
4302
4303 if (!no_error) {
4304 if (copytexture_error_check(ctx, dims, target, texObj, level,
4305 internalFormat, border))
4306 return;
4307
4308 if (!_mesa_legal_texture_dimensions(ctx, target, level, width, height,
4309 1, border)) {
4310 _mesa_error(ctx, GL_INVALID_VALUE,
4311 "glCopyTexImage%uD(invalid width=%d or height=%d)",
4312 dims, width, height);
4313 return;
4314 }
4315 }
4316
4317 assert(texObj);
4318
4319 texFormat = _mesa_choose_texture_format(ctx, texObj, target, level,
4320 internalFormat, GL_NONE, GL_NONE);
4321
4322 /* First check if reallocating the texture buffer can be avoided.
4323 * Without the realloc the copy can be 20x faster.
4324 */
4325 _mesa_lock_texture(ctx, texObj);
4326 {
4327 texImage = _mesa_select_tex_image(texObj, target, level);
4328 if (texImage && can_avoid_reallocation(texImage, internalFormat, texFormat,
4329 width, height, border)) {
4330 _mesa_unlock_texture(ctx, texObj);
4331 if (no_error) {
4332 copy_texture_sub_image_no_error(ctx, dims, texObj, target, level, 0,
4333 0, 0, x, y, width, height);
4334 } else {
4335 copy_texture_sub_image_err(ctx, dims, texObj, target, level, 0, 0,
4336 0, x, y, width, height,"CopyTexImage");
4337 }
4338 return;
4339 }
4340 }
4341 _mesa_unlock_texture(ctx, texObj);
4342 _mesa_perf_debug(ctx, MESA_DEBUG_SEVERITY_LOW, "glCopyTexImage "
4343 "can't avoid reallocating texture storage\n");
4344
4345 if (!no_error && _mesa_is_gles3(ctx)) {
4346 struct gl_renderbuffer *rb =
4347 _mesa_get_read_renderbuffer_for_format(ctx, internalFormat);
4348
4349 if (_mesa_is_enum_format_unsized(internalFormat)) {
4350 /* Conversion from GL_RGB10_A2 source buffer format is not allowed in
4351 * OpenGL ES 3.0. Khronos bug# 9807.
4352 */
4353 if (rb->InternalFormat == GL_RGB10_A2) {
4354 _mesa_error(ctx, GL_INVALID_OPERATION,
4355 "glCopyTexImage%uD(Reading from GL_RGB10_A2 buffer"
4356 " and writing to unsized internal format)", dims);
4357 return;
4358 }
4359 }
4360 /* From Page 139 of OpenGL ES 3.0 spec:
4361 * "If internalformat is sized, the internal format of the new texel
4362 * array is internalformat, and this is also the new texel array’s
4363 * effective internal format. If the component sizes of internalformat
4364 * do not exactly match the corresponding component sizes of the source
4365 * buffer’s effective internal format, described below, an
4366 * INVALID_OPERATION error is generated. If internalformat is unsized,
4367 * the internal format of the new texel array is the effective internal
4368 * format of the source buffer, and this is also the new texel array’s
4369 * effective internal format.
4370 */
4371 else if (formats_differ_in_component_sizes (texFormat, rb->Format)) {
4372 _mesa_error(ctx, GL_INVALID_OPERATION,
4373 "glCopyTexImage%uD(component size changed in"
4374 " internal format)", dims);
4375 return;
4376 }
4377 }
4378
4379 assert(texFormat != MESA_FORMAT_NONE);
4380
4381 if (!ctx->Driver.TestProxyTexImage(ctx, proxy_target(target),
4382 0, level, texFormat, 1,
4383 width, height, 1)) {
4384 _mesa_error(ctx, GL_OUT_OF_MEMORY,
4385 "glCopyTexImage%uD(image too large)", dims);
4386 return;
4387 }
4388
4389 if (border && ctx->Const.StripTextureBorder) {
4390 x += border;
4391 width -= border * 2;
4392 if (dims == 2) {
4393 y += border;
4394 height -= border * 2;
4395 }
4396 border = 0;
4397 }
4398
4399 _mesa_lock_texture(ctx, texObj);
4400 {
4401 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
4402
4403 if (!texImage) {
4404 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexImage%uD", dims);
4405 }
4406 else {
4407 GLint srcX = x, srcY = y, dstX = 0, dstY = 0, dstZ = 0;
4408 const GLuint face = _mesa_tex_target_to_face(target);
4409
4410 /* Free old texture image */
4411 ctx->Driver.FreeTextureImageBuffer(ctx, texImage);
4412
4413 _mesa_init_teximage_fields(ctx, texImage, width, height, 1,
4414 border, internalFormat, texFormat);
4415
4416 if (width && height) {
4417 /* Allocate texture memory (no pixel data yet) */
4418 ctx->Driver.AllocTextureImageBuffer(ctx, texImage);
4419
4420 if (ctx->Const.NoClippingOnCopyTex ||
4421 _mesa_clip_copytexsubimage(ctx, &dstX, &dstY, &srcX, &srcY,
4422 &width, &height)) {
4423 struct gl_renderbuffer *srcRb =
4424 get_copy_tex_image_source(ctx, texImage->TexFormat);
4425
4426 copytexsubimage_by_slice(ctx, texImage, dims,
4427 dstX, dstY, dstZ,
4428 srcRb, srcX, srcY, width, height);
4429 }
4430
4431 check_gen_mipmap(ctx, target, texObj, level);
4432 }
4433
4434 _mesa_update_fbo_texture(ctx, texObj, face, level);
4435
4436 _mesa_dirty_texobj(ctx, texObj);
4437 }
4438 }
4439 _mesa_unlock_texture(ctx, texObj);
4440 }
4441
4442
4443 static void
4444 copyteximage_err(struct gl_context *ctx, GLuint dims,
4445 GLenum target,
4446 GLint level, GLenum internalFormat, GLint x, GLint y,
4447 GLsizei width, GLsizei height, GLint border)
4448 {
4449 struct gl_texture_object* texObj = _mesa_get_current_tex_object(ctx, target);
4450 copyteximage(ctx, dims, texObj, target, level, internalFormat, x, y, width, height,
4451 border, false);
4452 }
4453
4454
4455 static void
4456 copyteximage_no_error(struct gl_context *ctx, GLuint dims, GLenum target,
4457 GLint level, GLenum internalFormat, GLint x, GLint y,
4458 GLsizei width, GLsizei height, GLint border)
4459 {
4460 struct gl_texture_object* texObj = _mesa_get_current_tex_object(ctx, target);
4461 copyteximage(ctx, dims, texObj, target, level, internalFormat, x, y, width, height,
4462 border, true);
4463 }
4464
4465
4466 void GLAPIENTRY
4467 _mesa_CopyTexImage1D( GLenum target, GLint level,
4468 GLenum internalFormat,
4469 GLint x, GLint y,
4470 GLsizei width, GLint border )
4471 {
4472 GET_CURRENT_CONTEXT(ctx);
4473 copyteximage_err(ctx, 1, target, level, internalFormat, x, y, width, 1,
4474 border);
4475 }
4476
4477
4478 void GLAPIENTRY
4479 _mesa_CopyTextureImage1DEXT( GLuint texture, GLenum target, GLint level,
4480 GLenum internalFormat,
4481 GLint x, GLint y,
4482 GLsizei width, GLint border )
4483 {
4484 GET_CURRENT_CONTEXT(ctx);
4485 struct gl_texture_object* texObj =
4486 _mesa_lookup_or_create_texture(ctx, target, texture, false, true,
4487 "glCopyTextureImage1DEXT");
4488 if (!texObj)
4489 return;
4490 copyteximage(ctx, 1, texObj, target, level, internalFormat, x, y, width, 1,
4491 border, false);
4492 }
4493
4494
4495 void GLAPIENTRY
4496 _mesa_CopyMultiTexImage1DEXT( GLenum texunit, GLenum target, GLint level,
4497 GLenum internalFormat,
4498 GLint x, GLint y,
4499 GLsizei width, GLint border )
4500 {
4501 GET_CURRENT_CONTEXT(ctx);
4502 struct gl_texture_object* texObj =
4503 _mesa_get_texobj_by_target_and_texunit(ctx, target,
4504 texunit - GL_TEXTURE0,
4505 false,
4506 "glCopyMultiTexImage1DEXT");
4507 if (!texObj)
4508 return;
4509 copyteximage(ctx, 1, texObj, target, level, internalFormat, x, y, width, 1,
4510 border, false);
4511 }
4512
4513
4514 void GLAPIENTRY
4515 _mesa_CopyTexImage2D( GLenum target, GLint level, GLenum internalFormat,
4516 GLint x, GLint y, GLsizei width, GLsizei height,
4517 GLint border )
4518 {
4519 GET_CURRENT_CONTEXT(ctx);
4520 copyteximage_err(ctx, 2, target, level, internalFormat,
4521 x, y, width, height, border);
4522 }
4523
4524
4525 void GLAPIENTRY
4526 _mesa_CopyTextureImage2DEXT( GLuint texture, GLenum target, GLint level,
4527 GLenum internalFormat,
4528 GLint x, GLint y,
4529 GLsizei width, GLsizei height,
4530 GLint border )
4531 {
4532 GET_CURRENT_CONTEXT(ctx);
4533 struct gl_texture_object* texObj =
4534 _mesa_lookup_or_create_texture(ctx, target, texture, false, true,
4535 "glCopyTextureImage2DEXT");
4536 if (!texObj)
4537 return;
4538 copyteximage(ctx, 2, texObj, target, level, internalFormat, x, y, width, height,
4539 border, false);
4540 }
4541
4542
4543 void GLAPIENTRY
4544 _mesa_CopyMultiTexImage2DEXT( GLenum texunit, GLenum target, GLint level,
4545 GLenum internalFormat,
4546 GLint x, GLint y,
4547 GLsizei width, GLsizei height, GLint border )
4548 {
4549 GET_CURRENT_CONTEXT(ctx);
4550 struct gl_texture_object* texObj =
4551 _mesa_get_texobj_by_target_and_texunit(ctx, target,
4552 texunit - GL_TEXTURE0,
4553 false,
4554 "glCopyMultiTexImage2DEXT");
4555 if (!texObj)
4556 return;
4557 copyteximage(ctx, 2, texObj, target, level, internalFormat, x, y, width, height,
4558 border, false);
4559 }
4560
4561
4562 void GLAPIENTRY
4563 _mesa_CopyTexImage1D_no_error(GLenum target, GLint level, GLenum internalFormat,
4564 GLint x, GLint y, GLsizei width, GLint border)
4565 {
4566 GET_CURRENT_CONTEXT(ctx);
4567 copyteximage_no_error(ctx, 1, target, level, internalFormat, x, y, width, 1,
4568 border);
4569 }
4570
4571
4572 void GLAPIENTRY
4573 _mesa_CopyTexImage2D_no_error(GLenum target, GLint level, GLenum internalFormat,
4574 GLint x, GLint y, GLsizei width, GLsizei height,
4575 GLint border)
4576 {
4577 GET_CURRENT_CONTEXT(ctx);
4578 copyteximage_no_error(ctx, 2, target, level, internalFormat,
4579 x, y, width, height, border);
4580 }
4581
4582
4583 void GLAPIENTRY
4584 _mesa_CopyTexSubImage1D(GLenum target, GLint level,
4585 GLint xoffset, GLint x, GLint y, GLsizei width)
4586 {
4587 struct gl_texture_object* texObj;
4588 const char *self = "glCopyTexSubImage1D";
4589 GET_CURRENT_CONTEXT(ctx);
4590
4591 /* Check target (proxies not allowed). Target must be checked prior to
4592 * calling _mesa_get_current_tex_object.
4593 */
4594 if (!legal_texsubimage_target(ctx, 1, target, false)) {
4595 _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid target %s)", self,
4596 _mesa_enum_to_string(target));
4597 return;
4598 }
4599
4600 texObj = _mesa_get_current_tex_object(ctx, target);
4601 if (!texObj)
4602 return;
4603
4604 copy_texture_sub_image_err(ctx, 1, texObj, target, level, xoffset, 0, 0,
4605 x, y, width, 1, self);
4606 }
4607
4608
4609 void GLAPIENTRY
4610 _mesa_CopyTexSubImage2D(GLenum target, GLint level,
4611 GLint xoffset, GLint yoffset,
4612 GLint x, GLint y, GLsizei width, GLsizei height)
4613 {
4614 struct gl_texture_object* texObj;
4615 const char *self = "glCopyTexSubImage2D";
4616 GET_CURRENT_CONTEXT(ctx);
4617
4618 /* Check target (proxies not allowed). Target must be checked prior to
4619 * calling _mesa_get_current_tex_object.
4620 */
4621 if (!legal_texsubimage_target(ctx, 2, target, false)) {
4622 _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid target %s)", self,
4623 _mesa_enum_to_string(target));
4624 return;
4625 }
4626
4627 texObj = _mesa_get_current_tex_object(ctx, target);
4628 if (!texObj)
4629 return;
4630
4631 copy_texture_sub_image_err(ctx, 2, texObj, target, level, xoffset, yoffset,
4632 0, x, y, width, height, self);
4633 }
4634
4635
4636 void GLAPIENTRY
4637 _mesa_CopyTexSubImage3D(GLenum target, GLint level,
4638 GLint xoffset, GLint yoffset, GLint zoffset,
4639 GLint x, GLint y, GLsizei width, GLsizei height)
4640 {
4641 struct gl_texture_object* texObj;
4642 const char *self = "glCopyTexSubImage3D";
4643 GET_CURRENT_CONTEXT(ctx);
4644
4645 /* Check target (proxies not allowed). Target must be checked prior to
4646 * calling _mesa_get_current_tex_object.
4647 */
4648 if (!legal_texsubimage_target(ctx, 3, target, false)) {
4649 _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid target %s)", self,
4650 _mesa_enum_to_string(target));
4651 return;
4652 }
4653
4654 texObj = _mesa_get_current_tex_object(ctx, target);
4655 if (!texObj)
4656 return;
4657
4658 copy_texture_sub_image_err(ctx, 3, texObj, target, level, xoffset, yoffset,
4659 zoffset, x, y, width, height, self);
4660 }
4661
4662
4663 void GLAPIENTRY
4664 _mesa_CopyTextureSubImage1D(GLuint texture, GLint level,
4665 GLint xoffset, GLint x, GLint y, GLsizei width)
4666 {
4667 struct gl_texture_object* texObj;
4668 const char *self = "glCopyTextureSubImage1D";
4669 GET_CURRENT_CONTEXT(ctx);
4670
4671 texObj = _mesa_lookup_texture_err(ctx, texture, self);
4672 if (!texObj)
4673 return;
4674
4675 /* Check target (proxies not allowed). */
4676 if (!legal_texsubimage_target(ctx, 1, texObj->Target, true)) {
4677 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid target %s)", self,
4678 _mesa_enum_to_string(texObj->Target));
4679 return;
4680 }
4681
4682 copy_texture_sub_image_err(ctx, 1, texObj, texObj->Target, level, xoffset, 0,
4683 0, x, y, width, 1, self);
4684 }
4685
4686
4687 void GLAPIENTRY
4688 _mesa_CopyTextureSubImage1DEXT(GLuint texture, GLenum target, GLint level,
4689 GLint xoffset, GLint x, GLint y, GLsizei width)
4690 {
4691 struct gl_texture_object* texObj;
4692 const char *self = "glCopyTextureSubImage1DEXT";
4693 GET_CURRENT_CONTEXT(ctx);
4694
4695 texObj = _mesa_lookup_or_create_texture(ctx, target, texture, false, true,
4696 self);
4697 if (!texObj)
4698 return;
4699
4700 /* Check target (proxies not allowed). */
4701 if (!legal_texsubimage_target(ctx, 1, texObj->Target, true)) {
4702 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid target %s)", self,
4703 _mesa_enum_to_string(texObj->Target));
4704 return;
4705 }
4706
4707 copy_texture_sub_image_err(ctx, 1, texObj, texObj->Target, level, xoffset, 0,
4708 0, x, y, width, 1, self);
4709 }
4710
4711
4712 void GLAPIENTRY
4713 _mesa_CopyMultiTexSubImage1DEXT(GLenum texunit, GLenum target, GLint level,
4714 GLint xoffset, GLint x, GLint y, GLsizei width)
4715 {
4716 struct gl_texture_object* texObj;
4717 const char *self = "glCopyMultiTexSubImage1DEXT";
4718 GET_CURRENT_CONTEXT(ctx);
4719
4720 texObj = _mesa_get_texobj_by_target_and_texunit(ctx, target,
4721 texunit - GL_TEXTURE0,
4722 false, self);
4723 if (!texObj)
4724 return;
4725
4726 copy_texture_sub_image_err(ctx, 1, texObj, texObj->Target, level, xoffset, 0,
4727 0, x, y, width, 1, self);
4728 }
4729
4730
4731 void GLAPIENTRY
4732 _mesa_CopyTextureSubImage2D(GLuint texture, GLint level,
4733 GLint xoffset, GLint yoffset,
4734 GLint x, GLint y, GLsizei width, GLsizei height)
4735 {
4736 struct gl_texture_object* texObj;
4737 const char *self = "glCopyTextureSubImage2D";
4738 GET_CURRENT_CONTEXT(ctx);
4739
4740 texObj = _mesa_lookup_texture_err(ctx, texture, self);
4741 if (!texObj)
4742 return;
4743
4744 /* Check target (proxies not allowed). */
4745 if (!legal_texsubimage_target(ctx, 2, texObj->Target, true)) {
4746 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid target %s)", self,
4747 _mesa_enum_to_string(texObj->Target));
4748 return;
4749 }
4750
4751 copy_texture_sub_image_err(ctx, 2, texObj, texObj->Target, level, xoffset,
4752 yoffset, 0, x, y, width, height, self);
4753 }
4754
4755
4756 void GLAPIENTRY
4757 _mesa_CopyTextureSubImage2DEXT(GLuint texture, GLenum target, GLint level,
4758 GLint xoffset, GLint yoffset,
4759 GLint x, GLint y, GLsizei width, GLsizei height)
4760 {
4761 struct gl_texture_object* texObj;
4762 const char *self = "glCopyTextureSubImage2DEXT";
4763 GET_CURRENT_CONTEXT(ctx);
4764
4765 texObj = _mesa_lookup_or_create_texture(ctx, target, texture, false, true, self);
4766 if (!texObj)
4767 return;
4768
4769 /* Check target (proxies not allowed). */
4770 if (!legal_texsubimage_target(ctx, 2, texObj->Target, true)) {
4771 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid target %s)", self,
4772 _mesa_enum_to_string(texObj->Target));
4773 return;
4774 }
4775
4776 copy_texture_sub_image_err(ctx, 2, texObj, texObj->Target, level, xoffset,
4777 yoffset, 0, x, y, width, height, self);
4778 }
4779
4780
4781 void GLAPIENTRY
4782 _mesa_CopyMultiTexSubImage2DEXT(GLenum texunit, GLenum target, GLint level,
4783 GLint xoffset, GLint yoffset,
4784 GLint x, GLint y, GLsizei width, GLsizei height)
4785 {
4786 struct gl_texture_object* texObj;
4787 const char *self = "glCopyMultiTexSubImage2DEXT";
4788 GET_CURRENT_CONTEXT(ctx);
4789
4790 texObj = _mesa_get_texobj_by_target_and_texunit(ctx, target,
4791 texunit - GL_TEXTURE0,
4792 false, self);
4793 if (!texObj)
4794 return;
4795
4796 copy_texture_sub_image_err(ctx, 2, texObj, texObj->Target, level, xoffset,
4797 yoffset, 0, x, y, width, height, self);
4798 }
4799
4800 void GLAPIENTRY
4801 _mesa_CopyTextureSubImage3D(GLuint texture, GLint level,
4802 GLint xoffset, GLint yoffset, GLint zoffset,
4803 GLint x, GLint y, GLsizei width, GLsizei height)
4804 {
4805 struct gl_texture_object* texObj;
4806 const char *self = "glCopyTextureSubImage3D";
4807 GET_CURRENT_CONTEXT(ctx);
4808
4809 texObj = _mesa_lookup_texture_err(ctx, texture, self);
4810 if (!texObj)
4811 return;
4812
4813 /* Check target (proxies not allowed). */
4814 if (!legal_texsubimage_target(ctx, 3, texObj->Target, true)) {
4815 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid target %s)", self,
4816 _mesa_enum_to_string(texObj->Target));
4817 return;
4818 }
4819
4820 if (texObj->Target == GL_TEXTURE_CUBE_MAP) {
4821 /* Act like CopyTexSubImage2D */
4822 copy_texture_sub_image_err(ctx, 2, texObj,
4823 GL_TEXTURE_CUBE_MAP_POSITIVE_X + zoffset,
4824 level, xoffset, yoffset, 0, x, y, width, height,
4825 self);
4826 }
4827 else
4828 copy_texture_sub_image_err(ctx, 3, texObj, texObj->Target, level, xoffset,
4829 yoffset, zoffset, x, y, width, height, self);
4830 }
4831
4832
4833 void GLAPIENTRY
4834 _mesa_CopyTextureSubImage3DEXT(GLuint texture, GLenum target, GLint level,
4835 GLint xoffset, GLint yoffset, GLint zoffset,
4836 GLint x, GLint y, GLsizei width, GLsizei height)
4837 {
4838 struct gl_texture_object* texObj;
4839 const char *self = "glCopyTextureSubImage3D";
4840 GET_CURRENT_CONTEXT(ctx);
4841
4842 texObj = _mesa_lookup_or_create_texture(ctx, target, texture, false, true, self);
4843 if (!texObj)
4844 return;
4845
4846 /* Check target (proxies not allowed). */
4847 if (!legal_texsubimage_target(ctx, 3, texObj->Target, true)) {
4848 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid target %s)", self,
4849 _mesa_enum_to_string(texObj->Target));
4850 return;
4851 }
4852
4853 if (texObj->Target == GL_TEXTURE_CUBE_MAP) {
4854 /* Act like CopyTexSubImage2D */
4855 copy_texture_sub_image_err(ctx, 2, texObj,
4856 GL_TEXTURE_CUBE_MAP_POSITIVE_X + zoffset,
4857 level, xoffset, yoffset, 0, x, y, width, height,
4858 self);
4859 }
4860 else
4861 copy_texture_sub_image_err(ctx, 3, texObj, texObj->Target, level, xoffset,
4862 yoffset, zoffset, x, y, width, height, self);
4863 }
4864
4865
4866 void GLAPIENTRY
4867 _mesa_CopyMultiTexSubImage3DEXT(GLenum texunit, GLenum target, GLint level,
4868 GLint xoffset, GLint yoffset, GLint zoffset,
4869 GLint x, GLint y, GLsizei width, GLsizei height)
4870 {
4871 struct gl_texture_object* texObj;
4872 const char *self = "glCopyMultiTexSubImage3D";
4873 GET_CURRENT_CONTEXT(ctx);
4874
4875 texObj = _mesa_get_texobj_by_target_and_texunit(ctx, target,
4876 texunit - GL_TEXTURE0,
4877 false, self);
4878 if (!texObj)
4879 return;
4880
4881 if (texObj->Target == GL_TEXTURE_CUBE_MAP) {
4882 /* Act like CopyTexSubImage2D */
4883 copy_texture_sub_image_err(ctx, 2, texObj,
4884 GL_TEXTURE_CUBE_MAP_POSITIVE_X + zoffset,
4885 level, xoffset, yoffset, 0, x, y, width, height,
4886 self);
4887 }
4888 else
4889 copy_texture_sub_image_err(ctx, 3, texObj, texObj->Target, level, xoffset,
4890 yoffset, zoffset, x, y, width, height, self);
4891 }
4892
4893
4894 void GLAPIENTRY
4895 _mesa_CopyTexSubImage1D_no_error(GLenum target, GLint level, GLint xoffset,
4896 GLint x, GLint y, GLsizei width)
4897 {
4898 GET_CURRENT_CONTEXT(ctx);
4899
4900 struct gl_texture_object* texObj = _mesa_get_current_tex_object(ctx, target);
4901 copy_texture_sub_image_no_error(ctx, 1, texObj, target, level, xoffset, 0, 0,
4902 x, y, width, 1);
4903 }
4904
4905
4906 void GLAPIENTRY
4907 _mesa_CopyTexSubImage2D_no_error(GLenum target, GLint level, GLint xoffset,
4908 GLint yoffset, GLint x, GLint y, GLsizei width,
4909 GLsizei height)
4910 {
4911 GET_CURRENT_CONTEXT(ctx);
4912
4913 struct gl_texture_object* texObj = _mesa_get_current_tex_object(ctx, target);
4914 copy_texture_sub_image_no_error(ctx, 2, texObj, target, level, xoffset,
4915 yoffset, 0, x, y, width, height);
4916 }
4917
4918
4919 void GLAPIENTRY
4920 _mesa_CopyTexSubImage3D_no_error(GLenum target, GLint level, GLint xoffset,
4921 GLint yoffset, GLint zoffset, GLint x, GLint y,
4922 GLsizei width, GLsizei height)
4923 {
4924 GET_CURRENT_CONTEXT(ctx);
4925
4926 struct gl_texture_object* texObj = _mesa_get_current_tex_object(ctx, target);
4927 copy_texture_sub_image_no_error(ctx, 3, texObj, target, level, xoffset,
4928 yoffset, zoffset, x, y, width, height);
4929 }
4930
4931
4932 void GLAPIENTRY
4933 _mesa_CopyTextureSubImage1D_no_error(GLuint texture, GLint level, GLint xoffset,
4934 GLint x, GLint y, GLsizei width)
4935 {
4936 GET_CURRENT_CONTEXT(ctx);
4937
4938 struct gl_texture_object* texObj = _mesa_lookup_texture(ctx, texture);
4939 copy_texture_sub_image_no_error(ctx, 1, texObj, texObj->Target, level,
4940 xoffset, 0, 0, x, y, width, 1);
4941 }
4942
4943
4944 void GLAPIENTRY
4945 _mesa_CopyTextureSubImage2D_no_error(GLuint texture, GLint level, GLint xoffset,
4946 GLint yoffset, GLint x, GLint y,
4947 GLsizei width, GLsizei height)
4948 {
4949 GET_CURRENT_CONTEXT(ctx);
4950
4951 struct gl_texture_object* texObj = _mesa_lookup_texture(ctx, texture);
4952 copy_texture_sub_image_no_error(ctx, 2, texObj, texObj->Target, level,
4953 xoffset, yoffset, 0, x, y, width, height);
4954 }
4955
4956
4957 void GLAPIENTRY
4958 _mesa_CopyTextureSubImage3D_no_error(GLuint texture, GLint level, GLint xoffset,
4959 GLint yoffset, GLint zoffset, GLint x,
4960 GLint y, GLsizei width, GLsizei height)
4961 {
4962 GET_CURRENT_CONTEXT(ctx);
4963
4964 struct gl_texture_object* texObj = _mesa_lookup_texture(ctx, texture);
4965 if (texObj->Target == GL_TEXTURE_CUBE_MAP) {
4966 /* Act like CopyTexSubImage2D */
4967 copy_texture_sub_image_no_error(ctx, 2, texObj,
4968 GL_TEXTURE_CUBE_MAP_POSITIVE_X + zoffset,
4969 level, xoffset, yoffset, 0, x, y, width,
4970 height);
4971 }
4972 else
4973 copy_texture_sub_image_no_error(ctx, 3, texObj, texObj->Target, level,
4974 xoffset, yoffset, zoffset, x, y, width,
4975 height);
4976 }
4977
4978
4979 static bool
4980 check_clear_tex_image(struct gl_context *ctx,
4981 const char *function,
4982 struct gl_texture_image *texImage,
4983 GLenum format, GLenum type,
4984 const void *data,
4985 GLubyte *clearValue)
4986 {
4987 struct gl_texture_object *texObj = texImage->TexObject;
4988 static const GLubyte zeroData[MAX_PIXEL_BYTES];
4989 GLenum internalFormat = texImage->InternalFormat;
4990 GLenum err;
4991
4992 if (texObj->Target == GL_TEXTURE_BUFFER) {
4993 _mesa_error(ctx, GL_INVALID_OPERATION,
4994 "%s(buffer texture)", function);
4995 return false;
4996 }
4997
4998 if (_mesa_is_compressed_format(ctx, internalFormat)) {
4999 _mesa_error(ctx, GL_INVALID_OPERATION,
5000 "%s(compressed texture)", function);
5001 return false;
5002 }
5003
5004 err = _mesa_error_check_format_and_type(ctx, format, type);
5005 if (err != GL_NO_ERROR) {
5006 _mesa_error(ctx, err,
5007 "%s(incompatible format = %s, type = %s)",
5008 function,
5009 _mesa_enum_to_string(format),
5010 _mesa_enum_to_string(type));
5011 return false;
5012 }
5013
5014 /* make sure internal format and format basically agree */
5015 if (!texture_formats_agree(internalFormat, format)) {
5016 _mesa_error(ctx, GL_INVALID_OPERATION,
5017 "%s(incompatible internalFormat = %s, format = %s)",
5018 function,
5019 _mesa_enum_to_string(internalFormat),
5020 _mesa_enum_to_string(format));
5021 return false;
5022 }
5023
5024 if (ctx->Version >= 30 || ctx->Extensions.EXT_texture_integer) {
5025 /* both source and dest must be integer-valued, or neither */
5026 if (_mesa_is_format_integer_color(texImage->TexFormat) !=
5027 _mesa_is_enum_format_integer(format)) {
5028 _mesa_error(ctx, GL_INVALID_OPERATION,
5029 "%s(integer/non-integer format mismatch)",
5030 function);
5031 return false;
5032 }
5033 }
5034
5035 if (!_mesa_texstore(ctx,
5036 1, /* dims */
5037 texImage->_BaseFormat,
5038 texImage->TexFormat,
5039 0, /* dstRowStride */
5040 &clearValue,
5041 1, 1, 1, /* srcWidth/Height/Depth */
5042 format, type,
5043 data ? data : zeroData,
5044 &ctx->DefaultPacking)) {
5045 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid format)", function);
5046 return false;
5047 }
5048
5049 return true;
5050 }
5051
5052
5053 static struct gl_texture_object *
5054 get_tex_obj_for_clear(struct gl_context *ctx,
5055 const char *function,
5056 GLuint texture)
5057 {
5058 struct gl_texture_object *texObj;
5059
5060 texObj = _mesa_lookup_texture_err(ctx, texture, function);
5061 if (!texObj)
5062 return NULL;
5063
5064 if (texObj->Target == 0) {
5065 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(unbound tex)", function);
5066 return NULL;
5067 }
5068
5069 return texObj;
5070 }
5071
5072
5073 /**
5074 * For clearing cube textures, the zoffset and depth parameters indicate
5075 * which cube map faces are to be cleared. This is the one case where we
5076 * need to be concerned with multiple gl_texture_images. This function
5077 * returns the array of texture images to clear for cube maps, or one
5078 * texture image otherwise.
5079 * \return number of texture images, 0 for error, 6 for cube, 1 otherwise.
5080 */
5081 static int
5082 get_tex_images_for_clear(struct gl_context *ctx,
5083 const char *function,
5084 struct gl_texture_object *texObj,
5085 GLint level,
5086 struct gl_texture_image **texImages)
5087 {
5088 GLenum target;
5089 int numFaces, i;
5090
5091 if (level < 0 || level >= MAX_TEXTURE_LEVELS) {
5092 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid level)", function);
5093 return 0;
5094 }
5095
5096 if (texObj->Target == GL_TEXTURE_CUBE_MAP) {
5097 target = GL_TEXTURE_CUBE_MAP_POSITIVE_X;
5098 numFaces = MAX_FACES;
5099 }
5100 else {
5101 target = texObj->Target;
5102 numFaces = 1;
5103 }
5104
5105 for (i = 0; i < numFaces; i++) {
5106 texImages[i] = _mesa_select_tex_image(texObj, target + i, level);
5107 if (texImages[i] == NULL) {
5108 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid level)", function);
5109 return 0;
5110 }
5111 }
5112
5113 return numFaces;
5114 }
5115
5116
5117 void GLAPIENTRY
5118 _mesa_ClearTexSubImage(GLuint texture, GLint level,
5119 GLint xoffset, GLint yoffset, GLint zoffset,
5120 GLsizei width, GLsizei height, GLsizei depth,
5121 GLenum format, GLenum type, const void *data)
5122 {
5123 GET_CURRENT_CONTEXT(ctx);
5124 struct gl_texture_object *texObj;
5125 struct gl_texture_image *texImages[MAX_FACES];
5126 GLubyte clearValue[MAX_FACES][MAX_PIXEL_BYTES];
5127 int i, numImages;
5128 int minDepth, maxDepth;
5129
5130 texObj = get_tex_obj_for_clear(ctx, "glClearTexSubImage", texture);
5131
5132 if (texObj == NULL)
5133 return;
5134
5135 _mesa_lock_texture(ctx, texObj);
5136
5137 numImages = get_tex_images_for_clear(ctx, "glClearTexSubImage",
5138 texObj, level, texImages);
5139 if (numImages == 0)
5140 goto out;
5141
5142 if (numImages == 1) {
5143 minDepth = -(int) texImages[0]->Border;
5144 maxDepth = texImages[0]->Depth;
5145 } else {
5146 assert(numImages == MAX_FACES);
5147 minDepth = 0;
5148 maxDepth = numImages;
5149 }
5150
5151 if (xoffset < -(GLint) texImages[0]->Border ||
5152 yoffset < -(GLint) texImages[0]->Border ||
5153 zoffset < minDepth ||
5154 width < 0 ||
5155 height < 0 ||
5156 depth < 0 ||
5157 xoffset + width > texImages[0]->Width ||
5158 yoffset + height > texImages[0]->Height ||
5159 zoffset + depth > maxDepth) {
5160 _mesa_error(ctx, GL_INVALID_OPERATION,
5161 "glClearSubTexImage(invalid dimensions)");
5162 goto out;
5163 }
5164
5165 if (numImages == 1) {
5166 if (check_clear_tex_image(ctx, "glClearTexSubImage", texImages[0],
5167 format, type, data, clearValue[0])) {
5168 ctx->Driver.ClearTexSubImage(ctx,
5169 texImages[0],
5170 xoffset, yoffset, zoffset,
5171 width, height, depth,
5172 data ? clearValue[0] : NULL);
5173 }
5174 } else {
5175 /* loop over cube face images */
5176 for (i = zoffset; i < zoffset + depth; i++) {
5177 assert(i < MAX_FACES);
5178 if (!check_clear_tex_image(ctx, "glClearTexSubImage", texImages[i],
5179 format, type, data, clearValue[i]))
5180 goto out;
5181 }
5182 for (i = zoffset; i < zoffset + depth; i++) {
5183 ctx->Driver.ClearTexSubImage(ctx,
5184 texImages[i],
5185 xoffset, yoffset, 0,
5186 width, height, 1,
5187 data ? clearValue[i] : NULL);
5188 }
5189 }
5190
5191 out:
5192 _mesa_unlock_texture(ctx, texObj);
5193 }
5194
5195
5196 void GLAPIENTRY
5197 _mesa_ClearTexImage( GLuint texture, GLint level,
5198 GLenum format, GLenum type, const void *data )
5199 {
5200 GET_CURRENT_CONTEXT(ctx);
5201 struct gl_texture_object *texObj;
5202 struct gl_texture_image *texImages[MAX_FACES];
5203 GLubyte clearValue[MAX_FACES][MAX_PIXEL_BYTES];
5204 int i, numImages;
5205
5206 texObj = get_tex_obj_for_clear(ctx, "glClearTexImage", texture);
5207
5208 if (texObj == NULL)
5209 return;
5210
5211 _mesa_lock_texture(ctx, texObj);
5212
5213 numImages = get_tex_images_for_clear(ctx, "glClearTexImage",
5214 texObj, level, texImages);
5215
5216 for (i = 0; i < numImages; i++) {
5217 if (!check_clear_tex_image(ctx, "glClearTexImage", texImages[i], format,
5218 type, data, clearValue[i]))
5219 goto out;
5220 }
5221
5222 for (i = 0; i < numImages; i++) {
5223 ctx->Driver.ClearTexSubImage(ctx, texImages[i],
5224 -(GLint) texImages[i]->Border, /* xoffset */
5225 -(GLint) texImages[i]->Border, /* yoffset */
5226 -(GLint) texImages[i]->Border, /* zoffset */
5227 texImages[i]->Width,
5228 texImages[i]->Height,
5229 texImages[i]->Depth,
5230 data ? clearValue[i] : NULL);
5231 }
5232
5233 out:
5234 _mesa_unlock_texture(ctx, texObj);
5235 }
5236
5237
5238
5239
5240 /**********************************************************************/
5241 /****** Compressed Textures ******/
5242 /**********************************************************************/
5243
5244
5245 /**
5246 * Target checking for glCompressedTexSubImage[123]D().
5247 * \return GL_TRUE if error, GL_FALSE if no error
5248 * Must come before other error checking so that the texture object can
5249 * be correctly retrieved using _mesa_get_current_tex_object.
5250 */
5251 static GLboolean
5252 compressed_subtexture_target_check(struct gl_context *ctx, GLenum target,
5253 GLint dims, GLenum intFormat, bool dsa,
5254 const char *caller)
5255 {
5256 GLboolean targetOK;
5257 mesa_format format;
5258 enum mesa_format_layout layout;
5259
5260 if (dsa && target == GL_TEXTURE_RECTANGLE) {
5261 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid target %s)", caller,
5262 _mesa_enum_to_string(target));
5263 return GL_TRUE;
5264 }
5265
5266 switch (dims) {
5267 case 2:
5268 switch (target) {
5269 case GL_TEXTURE_2D:
5270 targetOK = GL_TRUE;
5271 break;
5272 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
5273 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
5274 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
5275 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
5276 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
5277 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
5278 targetOK = ctx->Extensions.ARB_texture_cube_map;
5279 break;
5280 default:
5281 targetOK = GL_FALSE;
5282 break;
5283 }
5284 break;
5285 case 3:
5286 switch (target) {
5287 case GL_TEXTURE_CUBE_MAP:
5288 targetOK = dsa && ctx->Extensions.ARB_texture_cube_map;
5289 break;
5290 case GL_TEXTURE_2D_ARRAY:
5291 targetOK = _mesa_is_gles3(ctx) ||
5292 (_mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_array);
5293 break;
5294 case GL_TEXTURE_CUBE_MAP_ARRAY:
5295 targetOK = _mesa_has_texture_cube_map_array(ctx);
5296 break;
5297 case GL_TEXTURE_3D:
5298 targetOK = GL_TRUE;
5299 /*
5300 * OpenGL 4.5 spec (30.10.2014) says in Section 8.7 Compressed Texture
5301 * Images:
5302 * "An INVALID_OPERATION error is generated by
5303 * CompressedTex*SubImage3D if the internal format of the texture
5304 * is one of the EAC, ETC2, or RGTC formats and either border is
5305 * non-zero, or the effective target for the texture is not
5306 * TEXTURE_2D_ARRAY."
5307 *
5308 * NOTE: that's probably a spec error. It should probably say
5309 * "... or the effective target for the texture is not
5310 * TEXTURE_2D_ARRAY, TEXTURE_CUBE_MAP, nor
5311 * GL_TEXTURE_CUBE_MAP_ARRAY."
5312 * since those targets are 2D images and they support all compression
5313 * formats.
5314 *
5315 * Instead of listing all these, just list those which are allowed,
5316 * which is (at this time) only bptc. Otherwise we'd say s3tc (and
5317 * more) are valid here, which they are not, but of course not
5318 * mentioned by core spec.
5319 *
5320 * Also, from GL_KHR_texture_compression_astc_{hdr,ldr}:
5321 *
5322 * "Add a second new column "3D Tex." which is empty for all non-ASTC
5323 * formats. If only the LDR profile is supported by the implementation,
5324 * this column is also empty for all ASTC formats. If both the LDR and HDR
5325 * profiles are supported, this column is checked for all ASTC formats."
5326 *
5327 * "An INVALID_OPERATION error is generated by CompressedTexSubImage3D if
5328 * <format> is one of the formats in table 8.19 and <target> is not
5329 * TEXTURE_2D_ARRAY, TEXTURE_CUBE_MAP_ARRAY, or TEXTURE_3D.
5330 *
5331 * An INVALID_OPERATION error is generated by CompressedTexSubImage3D if
5332 * <format> is TEXTURE_CUBE_MAP_ARRAY and the "Cube Map Array" column of
5333 * table 8.19 is *not* checked, or if <format> is TEXTURE_3D and the "3D
5334 * Tex." column of table 8.19 is *not* checked"
5335 *
5336 * And from GL_KHR_texture_compression_astc_sliced_3d:
5337 *
5338 * "Modify the "3D Tex." column to be checked for all ASTC formats."
5339 */
5340 format = _mesa_glenum_to_compressed_format(intFormat);
5341 layout = _mesa_get_format_layout(format);
5342 switch (layout) {
5343 case MESA_FORMAT_LAYOUT_BPTC:
5344 /* valid format */
5345 break;
5346 case MESA_FORMAT_LAYOUT_ASTC:
5347 targetOK =
5348 ctx->Extensions.KHR_texture_compression_astc_hdr ||
5349 ctx->Extensions.KHR_texture_compression_astc_sliced_3d;
5350 break;
5351 default:
5352 /* invalid format */
5353 _mesa_error(ctx, GL_INVALID_OPERATION,
5354 "%s(invalid target %s for format %s)", caller,
5355 _mesa_enum_to_string(target),
5356 _mesa_enum_to_string(intFormat));
5357 return GL_TRUE;
5358 }
5359 break;
5360 default:
5361 targetOK = GL_FALSE;
5362 }
5363
5364 break;
5365 default:
5366 assert(dims == 1);
5367 /* no 1D compressed textures at this time */
5368 targetOK = GL_FALSE;
5369 break;
5370 }
5371
5372 if (!targetOK) {
5373 _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid target %s)", caller,
5374 _mesa_enum_to_string(target));
5375 return GL_TRUE;
5376 }
5377
5378 return GL_FALSE;
5379 }
5380
5381 /**
5382 * Error checking for glCompressedTexSubImage[123]D().
5383 * \return GL_TRUE if error, GL_FALSE if no error
5384 */
5385 static GLboolean
5386 compressed_subtexture_error_check(struct gl_context *ctx, GLint dims,
5387 const struct gl_texture_object *texObj,
5388 GLenum target, GLint level,
5389 GLint xoffset, GLint yoffset, GLint zoffset,
5390 GLsizei width, GLsizei height, GLsizei depth,
5391 GLenum format, GLsizei imageSize,
5392 const GLvoid *data, const char *callerName)
5393 {
5394 struct gl_texture_image *texImage;
5395 GLint expectedSize;
5396
5397 /* this will catch any invalid compressed format token */
5398 if (!_mesa_is_compressed_format(ctx, format)) {
5399 _mesa_error(ctx, GL_INVALID_ENUM, "%s(format)", callerName);
5400 return GL_TRUE;
5401 }
5402
5403 if (level < 0 || level >= _mesa_max_texture_levels(ctx, target)) {
5404 _mesa_error(ctx, GL_INVALID_VALUE, "%s(level=%d)", callerName, level);
5405 return GL_TRUE;
5406 }
5407
5408 /* validate the bound PBO, if any */
5409 if (!_mesa_validate_pbo_source_compressed(ctx, dims, &ctx->Unpack,
5410 imageSize, data, callerName)) {
5411 return GL_TRUE;
5412 }
5413
5414 /* Check for invalid pixel storage modes */
5415 if (!_mesa_compressed_pixel_storage_error_check(ctx, dims,
5416 &ctx->Unpack, callerName)) {
5417 return GL_TRUE;
5418 }
5419
5420 expectedSize = compressed_tex_size(width, height, depth, format);
5421 if (expectedSize != imageSize) {
5422 _mesa_error(ctx, GL_INVALID_VALUE, "%s(size=%d)", callerName, imageSize);
5423 return GL_TRUE;
5424 }
5425
5426 texImage = _mesa_select_tex_image(texObj, target, level);
5427 if (!texImage) {
5428 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid texture level %d)",
5429 callerName, level);
5430 return GL_TRUE;
5431 }
5432
5433 if ((GLint) format != texImage->InternalFormat) {
5434 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(format=%s)",
5435 callerName, _mesa_enum_to_string(format));
5436 return GL_TRUE;
5437 }
5438
5439 if (compressedteximage_only_format(format)) {
5440 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(format=%s cannot be updated)",
5441 callerName, _mesa_enum_to_string(format));
5442 return GL_TRUE;
5443 }
5444
5445 if (error_check_subtexture_negative_dimensions(ctx, dims, width, height,
5446 depth, callerName)) {
5447 return GL_TRUE;
5448 }
5449
5450 if (error_check_subtexture_dimensions(ctx, dims, texImage, xoffset, yoffset,
5451 zoffset, width, height, depth,
5452 callerName)) {
5453 return GL_TRUE;
5454 }
5455
5456 return GL_FALSE;
5457 }
5458
5459
5460 void GLAPIENTRY
5461 _mesa_CompressedTexImage1D(GLenum target, GLint level,
5462 GLenum internalFormat, GLsizei width,
5463 GLint border, GLsizei imageSize,
5464 const GLvoid *data)
5465 {
5466 GET_CURRENT_CONTEXT(ctx);
5467 teximage_err(ctx, GL_TRUE, 1, target, level, internalFormat,
5468 width, 1, 1, border, GL_NONE, GL_NONE, imageSize, data);
5469 }
5470
5471
5472 void GLAPIENTRY
5473 _mesa_CompressedTextureImage1DEXT(GLuint texture, GLenum target, GLint level,
5474 GLenum internalFormat, GLsizei width,
5475 GLint border, GLsizei imageSize,
5476 const GLvoid *pixels)
5477 {
5478 struct gl_texture_object* texObj;
5479 GET_CURRENT_CONTEXT(ctx);
5480
5481 texObj = _mesa_lookup_or_create_texture(ctx, target, texture, false, true,
5482 "glCompressedTextureImage1DEXT");
5483 if (!texObj)
5484 return;
5485 teximage(ctx, GL_TRUE, 1, texObj, target, level, internalFormat,
5486 width, 1, 1, border, GL_NONE, GL_NONE, imageSize, pixels, false);
5487 }
5488
5489
5490 void GLAPIENTRY
5491 _mesa_CompressedMultiTexImage1DEXT(GLenum texunit, GLenum target, GLint level,
5492 GLenum internalFormat, GLsizei width,
5493 GLint border, GLsizei imageSize,
5494 const GLvoid *pixels)
5495 {
5496 struct gl_texture_object* texObj;
5497 GET_CURRENT_CONTEXT(ctx);
5498
5499 texObj = _mesa_get_texobj_by_target_and_texunit(ctx, target,
5500 texunit - GL_TEXTURE0,
5501 true,
5502 "glCompressedMultiTexImage1DEXT");
5503 if (!texObj)
5504 return;
5505 teximage(ctx, GL_TRUE, 1, texObj, target, level, internalFormat,
5506 width, 1, 1, border, GL_NONE, GL_NONE, imageSize, pixels, false);
5507 }
5508
5509
5510 void GLAPIENTRY
5511 _mesa_CompressedTexImage2D(GLenum target, GLint level,
5512 GLenum internalFormat, GLsizei width,
5513 GLsizei height, GLint border, GLsizei imageSize,
5514 const GLvoid *data)
5515 {
5516 GET_CURRENT_CONTEXT(ctx);
5517 teximage_err(ctx, GL_TRUE, 2, target, level, internalFormat,
5518 width, height, 1, border, GL_NONE, GL_NONE, imageSize, data);
5519 }
5520
5521
5522 void GLAPIENTRY
5523 _mesa_CompressedTextureImage2DEXT(GLuint texture, GLenum target, GLint level,
5524 GLenum internalFormat, GLsizei width,
5525 GLsizei height, GLint border, GLsizei imageSize,
5526 const GLvoid *pixels)
5527 {
5528 struct gl_texture_object* texObj;
5529 GET_CURRENT_CONTEXT(ctx);
5530
5531 texObj = _mesa_lookup_or_create_texture(ctx, target, texture, false, true,
5532 "glCompressedTextureImage2DEXT");
5533 if (!texObj)
5534 return;
5535 teximage(ctx, GL_TRUE, 2, texObj, target, level, internalFormat,
5536 width, height, 1, border, GL_NONE, GL_NONE, imageSize, pixels, false);
5537 }
5538
5539
5540 void GLAPIENTRY
5541 _mesa_CompressedMultiTexImage2DEXT(GLenum texunit, GLenum target, GLint level,
5542 GLenum internalFormat, GLsizei width,
5543 GLsizei height, GLint border, GLsizei imageSize,
5544 const GLvoid *pixels)
5545 {
5546 struct gl_texture_object* texObj;
5547 GET_CURRENT_CONTEXT(ctx);
5548
5549 texObj = _mesa_get_texobj_by_target_and_texunit(ctx, target,
5550 texunit - GL_TEXTURE0,
5551 true,
5552 "glCompressedMultiTexImage2DEXT");
5553 if (!texObj)
5554 return;
5555 teximage(ctx, GL_TRUE, 2, texObj, target, level, internalFormat,
5556 width, height, 1, border, GL_NONE, GL_NONE, imageSize, pixels, false);
5557 }
5558
5559
5560 void GLAPIENTRY
5561 _mesa_CompressedTexImage3D(GLenum target, GLint level,
5562 GLenum internalFormat, GLsizei width,
5563 GLsizei height, GLsizei depth, GLint border,
5564 GLsizei imageSize, const GLvoid *data)
5565 {
5566 GET_CURRENT_CONTEXT(ctx);
5567 teximage_err(ctx, GL_TRUE, 3, target, level, internalFormat, width, height,
5568 depth, border, GL_NONE, GL_NONE, imageSize, data);
5569 }
5570
5571
5572 void GLAPIENTRY
5573 _mesa_CompressedTextureImage3DEXT(GLuint texture, GLenum target, GLint level,
5574 GLenum internalFormat, GLsizei width,
5575 GLsizei height, GLsizei depth, GLint border,
5576 GLsizei imageSize, const GLvoid *pixels)
5577 {
5578 struct gl_texture_object* texObj;
5579 GET_CURRENT_CONTEXT(ctx);
5580
5581 texObj = _mesa_lookup_or_create_texture(ctx, target, texture, false, true,
5582 "glCompressedTextureImage3DEXT");
5583 if (!texObj)
5584 return;
5585 teximage(ctx, GL_TRUE, 3, texObj, target, level, internalFormat,
5586 width, height, depth, border, GL_NONE, GL_NONE, imageSize, pixels, false);
5587 }
5588
5589
5590 void GLAPIENTRY
5591 _mesa_CompressedMultiTexImage3DEXT(GLenum texunit, GLenum target, GLint level,
5592 GLenum internalFormat, GLsizei width,
5593 GLsizei height, GLsizei depth, GLint border,
5594 GLsizei imageSize, const GLvoid *pixels)
5595 {
5596 struct gl_texture_object* texObj;
5597 GET_CURRENT_CONTEXT(ctx);
5598
5599 texObj = _mesa_get_texobj_by_target_and_texunit(ctx, target,
5600 texunit - GL_TEXTURE0,
5601 true,
5602 "glCompressedMultiTexImage3DEXT");
5603 if (!texObj)
5604 return;
5605 teximage(ctx, GL_TRUE, 3, texObj, target, level, internalFormat,
5606 width, height, depth, border, GL_NONE, GL_NONE, imageSize, pixels, false);
5607 }
5608
5609
5610 void GLAPIENTRY
5611 _mesa_CompressedTexImage1D_no_error(GLenum target, GLint level,
5612 GLenum internalFormat, GLsizei width,
5613 GLint border, GLsizei imageSize,
5614 const GLvoid *data)
5615 {
5616 GET_CURRENT_CONTEXT(ctx);
5617 teximage_no_error(ctx, GL_TRUE, 1, target, level, internalFormat, width, 1,
5618 1, border, GL_NONE, GL_NONE, imageSize, data);
5619 }
5620
5621
5622 void GLAPIENTRY
5623 _mesa_CompressedTexImage2D_no_error(GLenum target, GLint level,
5624 GLenum internalFormat, GLsizei width,
5625 GLsizei height, GLint border,
5626 GLsizei imageSize, const GLvoid *data)
5627 {
5628 GET_CURRENT_CONTEXT(ctx);
5629 teximage_no_error(ctx, GL_TRUE, 2, target, level, internalFormat, width,
5630 height, 1, border, GL_NONE, GL_NONE, imageSize, data);
5631 }
5632
5633
5634 void GLAPIENTRY
5635 _mesa_CompressedTexImage3D_no_error(GLenum target, GLint level,
5636 GLenum internalFormat, GLsizei width,
5637 GLsizei height, GLsizei depth, GLint border,
5638 GLsizei imageSize, const GLvoid *data)
5639 {
5640 GET_CURRENT_CONTEXT(ctx);
5641 teximage_no_error(ctx, GL_TRUE, 3, target, level, internalFormat, width,
5642 height, depth, border, GL_NONE, GL_NONE, imageSize, data);
5643 }
5644
5645
5646 /**
5647 * Common helper for glCompressedTexSubImage1/2/3D() and
5648 * glCompressedTextureSubImage1/2/3D().
5649 */
5650 static void
5651 compressed_texture_sub_image(struct gl_context *ctx, GLuint dims,
5652 struct gl_texture_object *texObj,
5653 struct gl_texture_image *texImage,
5654 GLenum target, GLint level, GLint xoffset,
5655 GLint yoffset, GLint zoffset, GLsizei width,
5656 GLsizei height, GLsizei depth, GLenum format,
5657 GLsizei imageSize, const GLvoid *data)
5658 {
5659 FLUSH_VERTICES(ctx, 0);
5660
5661 _mesa_lock_texture(ctx, texObj);
5662 {
5663 if (width > 0 && height > 0 && depth > 0) {
5664 ctx->Driver.CompressedTexSubImage(ctx, dims, texImage,
5665 xoffset, yoffset, zoffset,
5666 width, height, depth,
5667 format, imageSize, data);
5668
5669 check_gen_mipmap(ctx, target, texObj, level);
5670
5671 /* NOTE: Don't signal _NEW_TEXTURE_OBJECT since we've only changed
5672 * the texel data, not the texture format, size, etc.
5673 */
5674 }
5675 }
5676 _mesa_unlock_texture(ctx, texObj);
5677 }
5678
5679
5680 enum tex_mode {
5681 /* Use bound texture to current unit */
5682 TEX_MODE_CURRENT_NO_ERROR = 0,
5683 TEX_MODE_CURRENT_ERROR,
5684 /* Use the specified texture name */
5685 TEX_MODE_DSA_NO_ERROR,
5686 TEX_MODE_DSA_ERROR,
5687 /* Use the specified texture name + target */
5688 TEX_MODE_EXT_DSA_TEXTURE,
5689 /* Use the specified texture unit + target */
5690 TEX_MODE_EXT_DSA_TEXUNIT,
5691 };
5692
5693
5694 static void
5695 compressed_tex_sub_image(unsigned dim, GLenum target, GLuint textureOrIndex,
5696 GLint level, GLint xoffset, GLint yoffset,
5697 GLint zoffset, GLsizei width, GLsizei height,
5698 GLsizei depth, GLenum format, GLsizei imageSize,
5699 const GLvoid *data, enum tex_mode mode,
5700 const char *caller)
5701 {
5702 struct gl_texture_object *texObj = NULL;
5703 struct gl_texture_image *texImage;
5704 bool no_error = false;
5705 GET_CURRENT_CONTEXT(ctx);
5706
5707 switch (mode) {
5708 case TEX_MODE_DSA_ERROR:
5709 assert(target == 0);
5710 texObj = _mesa_lookup_texture_err(ctx, textureOrIndex, caller);
5711 if (texObj)
5712 target = texObj->Target;
5713 break;
5714 case TEX_MODE_DSA_NO_ERROR:
5715 assert(target == 0);
5716 texObj = _mesa_lookup_texture(ctx, textureOrIndex);
5717 if (texObj)
5718 target = texObj->Target;
5719 no_error = true;
5720 break;
5721 case TEX_MODE_EXT_DSA_TEXTURE:
5722 texObj = _mesa_lookup_or_create_texture(ctx, target, textureOrIndex,
5723 false, true, caller);
5724 break;
5725 case TEX_MODE_EXT_DSA_TEXUNIT:
5726 texObj = _mesa_get_texobj_by_target_and_texunit(ctx, target,
5727 textureOrIndex,
5728 false,
5729 caller);
5730 break;
5731 case TEX_MODE_CURRENT_NO_ERROR:
5732 no_error = true;
5733 /* fallthrough */
5734 case TEX_MODE_CURRENT_ERROR:
5735 default:
5736 assert(textureOrIndex == 0);
5737 break;
5738 }
5739
5740 if (!no_error &&
5741 compressed_subtexture_target_check(ctx, target, dim, format,
5742 mode == TEX_MODE_DSA_ERROR,
5743 caller)) {
5744 return;
5745 }
5746
5747 if (mode == TEX_MODE_CURRENT_NO_ERROR ||
5748 mode == TEX_MODE_CURRENT_ERROR) {
5749 texObj = _mesa_get_current_tex_object(ctx, target);
5750 }
5751
5752 if (!texObj)
5753 return;
5754
5755 if (!no_error &&
5756 compressed_subtexture_error_check(ctx, dim, texObj, target, level,
5757 xoffset, yoffset, zoffset, width,
5758 height, depth, format,
5759 imageSize, data, caller)) {
5760 return;
5761 }
5762
5763 /* Must handle special case GL_TEXTURE_CUBE_MAP. */
5764 if (dim == 3 &&
5765 (mode == TEX_MODE_DSA_ERROR || mode == TEX_MODE_DSA_NO_ERROR) &&
5766 texObj->Target == GL_TEXTURE_CUBE_MAP) {
5767 const char *pixels = data;
5768 GLint image_stride;
5769
5770 /* Make sure the texture object is a proper cube.
5771 * (See texturesubimage in teximage.c for details on why this check is
5772 * performed.)
5773 */
5774 if (!no_error && !_mesa_cube_level_complete(texObj, level)) {
5775 _mesa_error(ctx, GL_INVALID_OPERATION,
5776 "glCompressedTextureSubImage3D(cube map incomplete)");
5777 return;
5778 }
5779
5780 /* Copy in each face. */
5781 for (int i = zoffset; i < zoffset + depth; ++i) {
5782 texImage = texObj->Image[i][level];
5783 assert(texImage);
5784
5785 compressed_texture_sub_image(ctx, 3, texObj, texImage,
5786 texObj->Target, level, xoffset, yoffset,
5787 0, width, height, 1, format,
5788 imageSize, pixels);
5789
5790 /* Compressed images don't have a client format */
5791 image_stride = _mesa_format_image_size(texImage->TexFormat,
5792 texImage->Width,
5793 texImage->Height, 1);
5794
5795 pixels += image_stride;
5796 imageSize -= image_stride;
5797 }
5798 } else {
5799 texImage = _mesa_select_tex_image(texObj, target, level);
5800 assert(texImage);
5801
5802 compressed_texture_sub_image(ctx, dim, texObj, texImage, target, level,
5803 xoffset, yoffset, zoffset, width, height,
5804 depth, format, imageSize, data);
5805 }
5806 }
5807
5808
5809 void GLAPIENTRY
5810 _mesa_CompressedTexSubImage1D_no_error(GLenum target, GLint level,
5811 GLint xoffset, GLsizei width,
5812 GLenum format, GLsizei imageSize,
5813 const GLvoid *data)
5814 {
5815 compressed_tex_sub_image(1, target, 0,
5816 level, xoffset, 0, 0, width,
5817 1, 1, format, imageSize, data,
5818 TEX_MODE_CURRENT_NO_ERROR,
5819 "glCompressedTexSubImage1D");
5820 }
5821
5822
5823 void GLAPIENTRY
5824 _mesa_CompressedTexSubImage1D(GLenum target, GLint level, GLint xoffset,
5825 GLsizei width, GLenum format,
5826 GLsizei imageSize, const GLvoid *data)
5827 {
5828 compressed_tex_sub_image(1, target, 0,
5829 level, xoffset, 0, 0, width,
5830 1, 1, format, imageSize, data,
5831 TEX_MODE_CURRENT_ERROR,
5832 "glCompressedTexSubImage1D");
5833 }
5834
5835
5836 void GLAPIENTRY
5837 _mesa_CompressedTextureSubImage1D_no_error(GLuint texture, GLint level,
5838 GLint xoffset, GLsizei width,
5839 GLenum format, GLsizei imageSize,
5840 const GLvoid *data)
5841 {
5842 compressed_tex_sub_image(1, 0, texture,
5843 level, xoffset, 0, 0,
5844 width, 1, 1, format, imageSize, data,
5845 TEX_MODE_DSA_NO_ERROR,
5846 "glCompressedTextureSubImage1D");
5847 }
5848
5849
5850 void GLAPIENTRY
5851 _mesa_CompressedTextureSubImage1D(GLuint texture, GLint level, GLint xoffset,
5852 GLsizei width, GLenum format,
5853 GLsizei imageSize, const GLvoid *data)
5854 {
5855 compressed_tex_sub_image(1, 0, texture,
5856 level, xoffset, 0, 0,
5857 width, 1, 1, format, imageSize, data,
5858 TEX_MODE_DSA_ERROR,
5859 "glCompressedTextureSubImage1D");
5860 }
5861
5862
5863 void GLAPIENTRY
5864 _mesa_CompressedTextureSubImage1DEXT(GLuint texture, GLenum target,
5865 GLint level, GLint xoffset,
5866 GLsizei width, GLenum format,
5867 GLsizei imageSize, const GLvoid *data)
5868 {
5869 compressed_tex_sub_image(1, target, texture, level, xoffset, 0,
5870 0, width, 1, 1, format, imageSize,
5871 data,
5872 TEX_MODE_EXT_DSA_TEXTURE,
5873 "glCompressedTextureSubImage1DEXT");
5874 }
5875
5876
5877 void GLAPIENTRY
5878 _mesa_CompressedMultiTexSubImage1DEXT(GLenum texunit, GLenum target,
5879 GLint level, GLint xoffset,
5880 GLsizei width, GLenum format,
5881 GLsizei imageSize, const GLvoid *data)
5882 {
5883 compressed_tex_sub_image(1, target, texunit - GL_TEXTURE0, level,
5884 xoffset, 0, 0, width, 1, 1, format, imageSize,
5885 data,
5886 TEX_MODE_EXT_DSA_TEXUNIT,
5887 "glCompressedMultiTexSubImage1DEXT");
5888 }
5889
5890
5891 void GLAPIENTRY
5892 _mesa_CompressedTexSubImage2D_no_error(GLenum target, GLint level,
5893 GLint xoffset, GLint yoffset,
5894 GLsizei width, GLsizei height,
5895 GLenum format, GLsizei imageSize,
5896 const GLvoid *data)
5897 {
5898 compressed_tex_sub_image(2, target, 0, level,
5899 xoffset, yoffset, 0,
5900 width, height, 1, format, imageSize, data,
5901 TEX_MODE_CURRENT_NO_ERROR,
5902 "glCompressedTexSubImage2D");
5903 }
5904
5905
5906 void GLAPIENTRY
5907 _mesa_CompressedTexSubImage2D(GLenum target, GLint level, GLint xoffset,
5908 GLint yoffset, GLsizei width, GLsizei height,
5909 GLenum format, GLsizei imageSize,
5910 const GLvoid *data)
5911 {
5912 compressed_tex_sub_image(2, target, 0, level,
5913 xoffset, yoffset, 0,
5914 width, height, 1, format, imageSize, data,
5915 TEX_MODE_CURRENT_ERROR,
5916 "glCompressedTexSubImage2D");
5917 }
5918
5919
5920 void GLAPIENTRY
5921 _mesa_CompressedTextureSubImage2DEXT(GLuint texture, GLenum target,
5922 GLint level, GLint xoffset,
5923 GLint yoffset, GLsizei width,
5924 GLsizei height, GLenum format,
5925 GLsizei imageSize, const GLvoid *data)
5926 {
5927 compressed_tex_sub_image(2, target, texture, level, xoffset,
5928 yoffset, 0, width, height, 1, format,
5929 imageSize, data,
5930 TEX_MODE_EXT_DSA_TEXTURE,
5931 "glCompressedTextureSubImage2DEXT");
5932 }
5933
5934
5935 void GLAPIENTRY
5936 _mesa_CompressedMultiTexSubImage2DEXT(GLenum texunit, GLenum target,
5937 GLint level, GLint xoffset, GLint yoffset,
5938 GLsizei width, GLsizei height, GLenum format,
5939 GLsizei imageSize, const GLvoid *data)
5940 {
5941 compressed_tex_sub_image(2, target, texunit - GL_TEXTURE0, level,
5942 xoffset, yoffset, 0, width, height, 1, format,
5943 imageSize, data,
5944 TEX_MODE_EXT_DSA_TEXUNIT,
5945 "glCompressedMultiTexSubImage2DEXT");
5946 }
5947
5948
5949 void GLAPIENTRY
5950 _mesa_CompressedTextureSubImage2D_no_error(GLuint texture, GLint level,
5951 GLint xoffset, GLint yoffset,
5952 GLsizei width, GLsizei height,
5953 GLenum format, GLsizei imageSize,
5954 const GLvoid *data)
5955 {
5956 compressed_tex_sub_image(2, 0, texture, level, xoffset, yoffset, 0,
5957 width, height, 1, format, imageSize, data,
5958 TEX_MODE_DSA_NO_ERROR,
5959 "glCompressedTextureSubImage2D");
5960 }
5961
5962
5963 void GLAPIENTRY
5964 _mesa_CompressedTextureSubImage2D(GLuint texture, GLint level, GLint xoffset,
5965 GLint yoffset,
5966 GLsizei width, GLsizei height,
5967 GLenum format, GLsizei imageSize,
5968 const GLvoid *data)
5969 {
5970 compressed_tex_sub_image(2, 0, texture, level, xoffset, yoffset, 0,
5971 width, height, 1, format, imageSize, data,
5972 TEX_MODE_DSA_ERROR,
5973 "glCompressedTextureSubImage2D");
5974 }
5975
5976 void GLAPIENTRY
5977 _mesa_CompressedTexSubImage3D_no_error(GLenum target, GLint level,
5978 GLint xoffset, GLint yoffset,
5979 GLint zoffset, GLsizei width,
5980 GLsizei height, GLsizei depth,
5981 GLenum format, GLsizei imageSize,
5982 const GLvoid *data)
5983 {
5984 compressed_tex_sub_image(3, target, 0, level, xoffset, yoffset,
5985 zoffset, width, height, depth, format,
5986 imageSize, data,
5987 TEX_MODE_CURRENT_NO_ERROR,
5988 "glCompressedTexSubImage3D");
5989 }
5990
5991 void GLAPIENTRY
5992 _mesa_CompressedTexSubImage3D(GLenum target, GLint level, GLint xoffset,
5993 GLint yoffset, GLint zoffset, GLsizei width,
5994 GLsizei height, GLsizei depth, GLenum format,
5995 GLsizei imageSize, const GLvoid *data)
5996 {
5997 compressed_tex_sub_image(3, target, 0, level, xoffset, yoffset,
5998 zoffset, width, height, depth, format,
5999 imageSize, data,
6000 TEX_MODE_CURRENT_ERROR,
6001 "glCompressedTexSubImage3D");
6002 }
6003
6004 void GLAPIENTRY
6005 _mesa_CompressedTextureSubImage3D_no_error(GLuint texture, GLint level,
6006 GLint xoffset, GLint yoffset,
6007 GLint zoffset, GLsizei width,
6008 GLsizei height, GLsizei depth,
6009 GLenum format, GLsizei imageSize,
6010 const GLvoid *data)
6011 {
6012 compressed_tex_sub_image(3, 0, texture, level, xoffset, yoffset,
6013 zoffset, width, height, depth, format,
6014 imageSize, data,
6015 TEX_MODE_DSA_NO_ERROR,
6016 "glCompressedTextureSubImage3D");
6017 }
6018
6019 void GLAPIENTRY
6020 _mesa_CompressedTextureSubImage3D(GLuint texture, GLint level, GLint xoffset,
6021 GLint yoffset, GLint zoffset, GLsizei width,
6022 GLsizei height, GLsizei depth,
6023 GLenum format, GLsizei imageSize,
6024 const GLvoid *data)
6025 {
6026 compressed_tex_sub_image(3, 0, texture, level, xoffset, yoffset,
6027 zoffset, width, height, depth, format,
6028 imageSize, data,
6029 TEX_MODE_DSA_ERROR,
6030 "glCompressedTextureSubImage3D");
6031 }
6032
6033
6034 void GLAPIENTRY
6035 _mesa_CompressedTextureSubImage3DEXT(GLuint texture, GLenum target,
6036 GLint level, GLint xoffset,
6037 GLint yoffset, GLint zoffset,
6038 GLsizei width, GLsizei height,
6039 GLsizei depth, GLenum format,
6040 GLsizei imageSize, const GLvoid *data)
6041 {
6042 compressed_tex_sub_image(3, target, texture, level, xoffset, yoffset,
6043 zoffset, width, height, depth, format,
6044 imageSize, data,
6045 TEX_MODE_EXT_DSA_TEXTURE,
6046 "glCompressedTextureSubImage3DEXT");
6047 }
6048
6049
6050 void GLAPIENTRY
6051 _mesa_CompressedMultiTexSubImage3DEXT(GLenum texunit, GLenum target,
6052 GLint level, GLint xoffset, GLint yoffset,
6053 GLint zoffset, GLsizei width, GLsizei height,
6054 GLsizei depth, GLenum format,
6055 GLsizei imageSize, const GLvoid *data)
6056 {
6057 compressed_tex_sub_image(3, target, texunit - GL_TEXTURE0, level,
6058 xoffset, yoffset, zoffset, width, height, depth,
6059 format, imageSize, data,
6060 TEX_MODE_EXT_DSA_TEXUNIT,
6061 "glCompressedMultiTexSubImage3DEXT");
6062 }
6063
6064
6065 mesa_format
6066 _mesa_get_texbuffer_format(const struct gl_context *ctx, GLenum internalFormat)
6067 {
6068 if (ctx->API == API_OPENGL_COMPAT) {
6069 switch (internalFormat) {
6070 case GL_ALPHA8:
6071 return MESA_FORMAT_A_UNORM8;
6072 case GL_ALPHA16:
6073 return MESA_FORMAT_A_UNORM16;
6074 case GL_ALPHA16F_ARB:
6075 return MESA_FORMAT_A_FLOAT16;
6076 case GL_ALPHA32F_ARB:
6077 return MESA_FORMAT_A_FLOAT32;
6078 case GL_ALPHA8I_EXT:
6079 return MESA_FORMAT_A_SINT8;
6080 case GL_ALPHA16I_EXT:
6081 return MESA_FORMAT_A_SINT16;
6082 case GL_ALPHA32I_EXT:
6083 return MESA_FORMAT_A_SINT32;
6084 case GL_ALPHA8UI_EXT:
6085 return MESA_FORMAT_A_UINT8;
6086 case GL_ALPHA16UI_EXT:
6087 return MESA_FORMAT_A_UINT16;
6088 case GL_ALPHA32UI_EXT:
6089 return MESA_FORMAT_A_UINT32;
6090 case GL_LUMINANCE8:
6091 return MESA_FORMAT_L_UNORM8;
6092 case GL_LUMINANCE16:
6093 return MESA_FORMAT_L_UNORM16;
6094 case GL_LUMINANCE16F_ARB:
6095 return MESA_FORMAT_L_FLOAT16;
6096 case GL_LUMINANCE32F_ARB:
6097 return MESA_FORMAT_L_FLOAT32;
6098 case GL_LUMINANCE8I_EXT:
6099 return MESA_FORMAT_L_SINT8;
6100 case GL_LUMINANCE16I_EXT:
6101 return MESA_FORMAT_L_SINT16;
6102 case GL_LUMINANCE32I_EXT:
6103 return MESA_FORMAT_L_SINT32;
6104 case GL_LUMINANCE8UI_EXT:
6105 return MESA_FORMAT_L_UINT8;
6106 case GL_LUMINANCE16UI_EXT:
6107 return MESA_FORMAT_L_UINT16;
6108 case GL_LUMINANCE32UI_EXT:
6109 return MESA_FORMAT_L_UINT32;
6110 case GL_LUMINANCE8_ALPHA8:
6111 return MESA_FORMAT_LA_UNORM8;
6112 case GL_LUMINANCE16_ALPHA16:
6113 return MESA_FORMAT_LA_UNORM16;
6114 case GL_LUMINANCE_ALPHA16F_ARB:
6115 return MESA_FORMAT_LA_FLOAT16;
6116 case GL_LUMINANCE_ALPHA32F_ARB:
6117 return MESA_FORMAT_LA_FLOAT32;
6118 case GL_LUMINANCE_ALPHA8I_EXT:
6119 return MESA_FORMAT_LA_SINT8;
6120 case GL_LUMINANCE_ALPHA16I_EXT:
6121 return MESA_FORMAT_LA_SINT16;
6122 case GL_LUMINANCE_ALPHA32I_EXT:
6123 return MESA_FORMAT_LA_SINT32;
6124 case GL_LUMINANCE_ALPHA8UI_EXT:
6125 return MESA_FORMAT_LA_UINT8;
6126 case GL_LUMINANCE_ALPHA16UI_EXT:
6127 return MESA_FORMAT_LA_UINT16;
6128 case GL_LUMINANCE_ALPHA32UI_EXT:
6129 return MESA_FORMAT_LA_UINT32;
6130 case GL_INTENSITY8:
6131 return MESA_FORMAT_I_UNORM8;
6132 case GL_INTENSITY16:
6133 return MESA_FORMAT_I_UNORM16;
6134 case GL_INTENSITY16F_ARB:
6135 return MESA_FORMAT_I_FLOAT16;
6136 case GL_INTENSITY32F_ARB:
6137 return MESA_FORMAT_I_FLOAT32;
6138 case GL_INTENSITY8I_EXT:
6139 return MESA_FORMAT_I_SINT8;
6140 case GL_INTENSITY16I_EXT:
6141 return MESA_FORMAT_I_SINT16;
6142 case GL_INTENSITY32I_EXT:
6143 return MESA_FORMAT_I_SINT32;
6144 case GL_INTENSITY8UI_EXT:
6145 return MESA_FORMAT_I_UINT8;
6146 case GL_INTENSITY16UI_EXT:
6147 return MESA_FORMAT_I_UINT16;
6148 case GL_INTENSITY32UI_EXT:
6149 return MESA_FORMAT_I_UINT32;
6150 default:
6151 break;
6152 }
6153 }
6154
6155 if (_mesa_has_ARB_texture_buffer_object_rgb32(ctx) ||
6156 _mesa_has_OES_texture_buffer(ctx)) {
6157 switch (internalFormat) {
6158 case GL_RGB32F:
6159 return MESA_FORMAT_RGB_FLOAT32;
6160 case GL_RGB32UI:
6161 return MESA_FORMAT_RGB_UINT32;
6162 case GL_RGB32I:
6163 return MESA_FORMAT_RGB_SINT32;
6164 default:
6165 break;
6166 }
6167 }
6168
6169 switch (internalFormat) {
6170 case GL_RGBA8:
6171 return MESA_FORMAT_R8G8B8A8_UNORM;
6172 case GL_RGBA16:
6173 if (_mesa_is_gles(ctx) && !_mesa_has_EXT_texture_norm16(ctx))
6174 return MESA_FORMAT_NONE;
6175 return MESA_FORMAT_RGBA_UNORM16;
6176 case GL_RGBA16F_ARB:
6177 return MESA_FORMAT_RGBA_FLOAT16;
6178 case GL_RGBA32F_ARB:
6179 return MESA_FORMAT_RGBA_FLOAT32;
6180 case GL_RGBA8I_EXT:
6181 return MESA_FORMAT_RGBA_SINT8;
6182 case GL_RGBA16I_EXT:
6183 return MESA_FORMAT_RGBA_SINT16;
6184 case GL_RGBA32I_EXT:
6185 return MESA_FORMAT_RGBA_SINT32;
6186 case GL_RGBA8UI_EXT:
6187 return MESA_FORMAT_RGBA_UINT8;
6188 case GL_RGBA16UI_EXT:
6189 return MESA_FORMAT_RGBA_UINT16;
6190 case GL_RGBA32UI_EXT:
6191 return MESA_FORMAT_RGBA_UINT32;
6192
6193 case GL_RG8:
6194 return MESA_FORMAT_RG_UNORM8;
6195 case GL_RG16:
6196 if (_mesa_is_gles(ctx) && !_mesa_has_EXT_texture_norm16(ctx))
6197 return MESA_FORMAT_NONE;
6198 return MESA_FORMAT_RG_UNORM16;
6199 case GL_RG16F:
6200 return MESA_FORMAT_RG_FLOAT16;
6201 case GL_RG32F:
6202 return MESA_FORMAT_RG_FLOAT32;
6203 case GL_RG8I:
6204 return MESA_FORMAT_RG_SINT8;
6205 case GL_RG16I:
6206 return MESA_FORMAT_RG_SINT16;
6207 case GL_RG32I:
6208 return MESA_FORMAT_RG_SINT32;
6209 case GL_RG8UI:
6210 return MESA_FORMAT_RG_UINT8;
6211 case GL_RG16UI:
6212 return MESA_FORMAT_RG_UINT16;
6213 case GL_RG32UI:
6214 return MESA_FORMAT_RG_UINT32;
6215
6216 case GL_R8:
6217 return MESA_FORMAT_R_UNORM8;
6218 case GL_R16:
6219 if (_mesa_is_gles(ctx) && !_mesa_has_EXT_texture_norm16(ctx))
6220 return MESA_FORMAT_NONE;
6221 return MESA_FORMAT_R_UNORM16;
6222 case GL_R16F:
6223 return MESA_FORMAT_R_FLOAT16;
6224 case GL_R32F:
6225 return MESA_FORMAT_R_FLOAT32;
6226 case GL_R8I:
6227 return MESA_FORMAT_R_SINT8;
6228 case GL_R16I:
6229 return MESA_FORMAT_R_SINT16;
6230 case GL_R32I:
6231 return MESA_FORMAT_R_SINT32;
6232 case GL_R8UI:
6233 return MESA_FORMAT_R_UINT8;
6234 case GL_R16UI:
6235 return MESA_FORMAT_R_UINT16;
6236 case GL_R32UI:
6237 return MESA_FORMAT_R_UINT32;
6238
6239 default:
6240 return MESA_FORMAT_NONE;
6241 }
6242 }
6243
6244
6245 mesa_format
6246 _mesa_validate_texbuffer_format(const struct gl_context *ctx,
6247 GLenum internalFormat)
6248 {
6249 mesa_format format = _mesa_get_texbuffer_format(ctx, internalFormat);
6250 GLenum datatype;
6251
6252 if (format == MESA_FORMAT_NONE)
6253 return MESA_FORMAT_NONE;
6254
6255 datatype = _mesa_get_format_datatype(format);
6256
6257 /* The GL_ARB_texture_buffer_object spec says:
6258 *
6259 * "If ARB_texture_float is not supported, references to the
6260 * floating-point internal formats provided by that extension should be
6261 * removed, and such formats may not be passed to TexBufferARB."
6262 *
6263 * As a result, GL_HALF_FLOAT internal format depends on both
6264 * GL_ARB_texture_float and GL_ARB_half_float_pixel.
6265 */
6266 if ((datatype == GL_FLOAT || datatype == GL_HALF_FLOAT) &&
6267 !ctx->Extensions.ARB_texture_float)
6268 return MESA_FORMAT_NONE;
6269
6270 if (!ctx->Extensions.ARB_texture_rg) {
6271 GLenum base_format = _mesa_get_format_base_format(format);
6272 if (base_format == GL_R || base_format == GL_RG)
6273 return MESA_FORMAT_NONE;
6274 }
6275
6276 if (!ctx->Extensions.ARB_texture_buffer_object_rgb32) {
6277 GLenum base_format = _mesa_get_format_base_format(format);
6278 if (base_format == GL_RGB)
6279 return MESA_FORMAT_NONE;
6280 }
6281 return format;
6282 }
6283
6284
6285 /**
6286 * Do work common to glTexBuffer, glTexBufferRange, glTextureBuffer
6287 * and glTextureBufferRange, including some error checking.
6288 */
6289 static void
6290 texture_buffer_range(struct gl_context *ctx,
6291 struct gl_texture_object *texObj,
6292 GLenum internalFormat,
6293 struct gl_buffer_object *bufObj,
6294 GLintptr offset, GLsizeiptr size,
6295 const char *caller)
6296 {
6297 GLintptr oldOffset = texObj->BufferOffset;
6298 GLsizeiptr oldSize = texObj->BufferSize;
6299 mesa_format format;
6300
6301 /* NOTE: ARB_texture_buffer_object might not be supported in
6302 * the compatibility profile.
6303 */
6304 if (!_mesa_has_ARB_texture_buffer_object(ctx) &&
6305 !_mesa_has_OES_texture_buffer(ctx)) {
6306 _mesa_error(ctx, GL_INVALID_OPERATION,
6307 "%s(ARB_texture_buffer_object is not"
6308 " implemented for the compatibility profile)", caller);
6309 return;
6310 }
6311
6312 if (texObj->HandleAllocated) {
6313 /* The ARB_bindless_texture spec says:
6314 *
6315 * "The error INVALID_OPERATION is generated by TexImage*, CopyTexImage*,
6316 * CompressedTexImage*, TexBuffer*, TexParameter*, as well as other
6317 * functions defined in terms of these, if the texture object to be
6318 * modified is referenced by one or more texture or image handles."
6319 */
6320 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(immutable texture)", caller);
6321 return;
6322 }
6323
6324 format = _mesa_validate_texbuffer_format(ctx, internalFormat);
6325 if (format == MESA_FORMAT_NONE) {
6326 _mesa_error(ctx, GL_INVALID_ENUM, "%s(internalFormat %s)",
6327 caller, _mesa_enum_to_string(internalFormat));
6328 return;
6329 }
6330
6331 FLUSH_VERTICES(ctx, 0);
6332
6333 _mesa_lock_texture(ctx, texObj);
6334 {
6335 _mesa_reference_buffer_object(ctx, &texObj->BufferObject, bufObj);
6336 texObj->BufferObjectFormat = internalFormat;
6337 texObj->_BufferObjectFormat = format;
6338 texObj->BufferOffset = offset;
6339 texObj->BufferSize = size;
6340 }
6341 _mesa_unlock_texture(ctx, texObj);
6342
6343 if (ctx->Driver.TexParameter) {
6344 if (offset != oldOffset) {
6345 ctx->Driver.TexParameter(ctx, texObj, GL_TEXTURE_BUFFER_OFFSET);
6346 }
6347 if (size != oldSize) {
6348 ctx->Driver.TexParameter(ctx, texObj, GL_TEXTURE_BUFFER_SIZE);
6349 }
6350 }
6351
6352 ctx->NewDriverState |= ctx->DriverFlags.NewTextureBuffer;
6353
6354 if (bufObj) {
6355 bufObj->UsageHistory |= USAGE_TEXTURE_BUFFER;
6356 }
6357 }
6358
6359
6360 /**
6361 * Make sure the texture buffer target is GL_TEXTURE_BUFFER.
6362 * Return true if it is, and return false if it is not
6363 * (and throw INVALID ENUM as dictated in the OpenGL 4.5
6364 * core spec, 02.02.2015, PDF page 245).
6365 */
6366 static bool
6367 check_texture_buffer_target(struct gl_context *ctx, GLenum target,
6368 const char *caller, bool dsa)
6369 {
6370 if (target != GL_TEXTURE_BUFFER_ARB) {
6371 _mesa_error(ctx, dsa ? GL_INVALID_OPERATION : GL_INVALID_ENUM,
6372 "%s(texture target is not GL_TEXTURE_BUFFER)", caller);
6373 return false;
6374 }
6375 else
6376 return true;
6377 }
6378
6379 /**
6380 * Check for errors related to the texture buffer range.
6381 * Return false if errors are found, true if none are found.
6382 */
6383 static bool
6384 check_texture_buffer_range(struct gl_context *ctx,
6385 struct gl_buffer_object *bufObj,
6386 GLintptr offset, GLsizeiptr size,
6387 const char *caller)
6388 {
6389 /* OpenGL 4.5 core spec (02.02.2015) says in Section 8.9 Buffer
6390 * Textures (PDF page 245):
6391 * "An INVALID_VALUE error is generated if offset is negative, if
6392 * size is less than or equal to zero, or if offset + size is greater
6393 * than the value of BUFFER_SIZE for the buffer bound to target."
6394 */
6395 if (offset < 0) {
6396 _mesa_error(ctx, GL_INVALID_VALUE, "%s(offset=%d < 0)", caller,
6397 (int) offset);
6398 return false;
6399 }
6400
6401 if (size <= 0) {
6402 _mesa_error(ctx, GL_INVALID_VALUE, "%s(size=%d <= 0)", caller,
6403 (int) size);
6404 return false;
6405 }
6406
6407 if (offset + size > bufObj->Size) {
6408 _mesa_error(ctx, GL_INVALID_VALUE,
6409 "%s(offset=%d + size=%d > buffer_size=%d)", caller,
6410 (int) offset, (int) size, (int) bufObj->Size);
6411 return false;
6412 }
6413
6414 /* OpenGL 4.5 core spec (02.02.2015) says in Section 8.9 Buffer
6415 * Textures (PDF page 245):
6416 * "An INVALID_VALUE error is generated if offset is not an integer
6417 * multiple of the value of TEXTURE_BUFFER_OFFSET_ALIGNMENT."
6418 */
6419 if (offset % ctx->Const.TextureBufferOffsetAlignment) {
6420 _mesa_error(ctx, GL_INVALID_VALUE,
6421 "%s(invalid offset alignment)", caller);
6422 return false;
6423 }
6424
6425 return true;
6426 }
6427
6428
6429 /** GL_ARB_texture_buffer_object */
6430 void GLAPIENTRY
6431 _mesa_TexBuffer(GLenum target, GLenum internalFormat, GLuint buffer)
6432 {
6433 struct gl_texture_object *texObj;
6434 struct gl_buffer_object *bufObj;
6435
6436 GET_CURRENT_CONTEXT(ctx);
6437
6438 /* Need to catch a bad target before it gets to
6439 * _mesa_get_current_tex_object.
6440 */
6441 if (!check_texture_buffer_target(ctx, target, "glTexBuffer", false))
6442 return;
6443
6444 if (buffer) {
6445 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, "glTexBuffer");
6446 if (!bufObj)
6447 return;
6448 } else
6449 bufObj = NULL;
6450
6451 texObj = _mesa_get_current_tex_object(ctx, target);
6452 if (!texObj)
6453 return;
6454
6455 texture_buffer_range(ctx, texObj, internalFormat, bufObj, 0,
6456 buffer ? -1 : 0, "glTexBuffer");
6457 }
6458
6459
6460 /** GL_ARB_texture_buffer_range */
6461 void GLAPIENTRY
6462 _mesa_TexBufferRange(GLenum target, GLenum internalFormat, GLuint buffer,
6463 GLintptr offset, GLsizeiptr size)
6464 {
6465 struct gl_texture_object *texObj;
6466 struct gl_buffer_object *bufObj;
6467
6468 GET_CURRENT_CONTEXT(ctx);
6469
6470 /* Need to catch a bad target before it gets to
6471 * _mesa_get_current_tex_object.
6472 */
6473 if (!check_texture_buffer_target(ctx, target, "glTexBufferRange", false))
6474 return;
6475
6476 if (buffer) {
6477 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, "glTexBufferRange");
6478 if (!bufObj)
6479 return;
6480
6481 if (!check_texture_buffer_range(ctx, bufObj, offset, size,
6482 "glTexBufferRange"))
6483 return;
6484
6485 } else {
6486 /* OpenGL 4.5 core spec (02.02.2015) says in Section 8.9 Buffer
6487 * Textures (PDF page 254):
6488 * "If buffer is zero, then any buffer object attached to the buffer
6489 * texture is detached, the values offset and size are ignored and
6490 * the state for offset and size for the buffer texture are reset to
6491 * zero."
6492 */
6493 offset = 0;
6494 size = 0;
6495 bufObj = NULL;
6496 }
6497
6498 texObj = _mesa_get_current_tex_object(ctx, target);
6499 if (!texObj)
6500 return;
6501
6502 texture_buffer_range(ctx, texObj, internalFormat, bufObj,
6503 offset, size, "glTexBufferRange");
6504 }
6505
6506
6507 /** GL_ARB_texture_buffer_range + GL_EXT_direct_state_access */
6508 void GLAPIENTRY
6509 _mesa_TextureBufferRangeEXT(GLuint texture, GLenum target, GLenum internalFormat,
6510 GLuint buffer, GLintptr offset, GLsizeiptr size)
6511 {
6512 struct gl_texture_object *texObj;
6513 struct gl_buffer_object *bufObj;
6514
6515 GET_CURRENT_CONTEXT(ctx);
6516
6517 texObj = _mesa_lookup_or_create_texture(ctx, target, texture, false, true,
6518 "glTextureBufferRangeEXT");
6519 if (!texObj)
6520 return;
6521
6522 if (!check_texture_buffer_target(ctx, target, "glTextureBufferRangeEXT", true))
6523 return;
6524
6525 if (buffer) {
6526 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, "glTextureBufferRangeEXT");
6527 if (!bufObj)
6528 return;
6529
6530 if (!check_texture_buffer_range(ctx, bufObj, offset, size,
6531 "glTextureBufferRangeEXT"))
6532 return;
6533
6534 } else {
6535 /* OpenGL 4.5 core spec (02.02.2015) says in Section 8.9 Buffer
6536 * Textures (PDF page 254):
6537 * "If buffer is zero, then any buffer object attached to the buffer
6538 * texture is detached, the values offset and size are ignored and
6539 * the state for offset and size for the buffer texture are reset to
6540 * zero."
6541 */
6542 offset = 0;
6543 size = 0;
6544 bufObj = NULL;
6545 }
6546
6547 texture_buffer_range(ctx, texObj, internalFormat, bufObj,
6548 offset, size, "glTextureBufferRangeEXT");
6549 }
6550
6551
6552 void GLAPIENTRY
6553 _mesa_TextureBuffer(GLuint texture, GLenum internalFormat, GLuint buffer)
6554 {
6555 struct gl_texture_object *texObj;
6556 struct gl_buffer_object *bufObj;
6557
6558 GET_CURRENT_CONTEXT(ctx);
6559
6560 if (buffer) {
6561 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, "glTextureBuffer");
6562 if (!bufObj)
6563 return;
6564 } else
6565 bufObj = NULL;
6566
6567 /* Get the texture object by Name. */
6568 texObj = _mesa_lookup_texture_err(ctx, texture, "glTextureBuffer");
6569 if (!texObj)
6570 return;
6571
6572 if (!check_texture_buffer_target(ctx, texObj->Target, "glTextureBuffer", true))
6573 return;
6574
6575 texture_buffer_range(ctx, texObj, internalFormat,
6576 bufObj, 0, buffer ? -1 : 0, "glTextureBuffer");
6577 }
6578
6579 void GLAPIENTRY
6580 _mesa_TextureBufferEXT(GLuint texture, GLenum target,
6581 GLenum internalFormat, GLuint buffer)
6582 {
6583 struct gl_texture_object *texObj;
6584 struct gl_buffer_object *bufObj;
6585
6586 GET_CURRENT_CONTEXT(ctx);
6587
6588 if (buffer) {
6589 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, "glTextureBuffer");
6590 if (!bufObj)
6591 return;
6592 } else
6593 bufObj = NULL;
6594
6595 /* Get the texture object by Name. */
6596 texObj = _mesa_lookup_or_create_texture(ctx, target, texture,
6597 false, true,
6598 "glTextureBufferEXT");
6599
6600 if (!texObj ||
6601 !check_texture_buffer_target(ctx, texObj->Target, "glTextureBufferEXT", true))
6602 return;
6603
6604 texture_buffer_range(ctx, texObj, internalFormat,
6605 bufObj, 0, buffer ? -1 : 0, "glTextureBufferEXT");
6606 }
6607
6608 void GLAPIENTRY
6609 _mesa_MultiTexBufferEXT(GLenum texunit, GLenum target,
6610 GLenum internalFormat, GLuint buffer)
6611 {
6612 struct gl_texture_object *texObj;
6613 struct gl_buffer_object *bufObj;
6614
6615 GET_CURRENT_CONTEXT(ctx);
6616
6617 if (buffer) {
6618 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, "glMultiTexBufferEXT");
6619 if (!bufObj)
6620 return;
6621 } else
6622 bufObj = NULL;
6623
6624 /* Get the texture object */
6625 texObj = _mesa_get_texobj_by_target_and_texunit(ctx, target,
6626 texunit - GL_TEXTURE0,
6627 true,
6628 "glMultiTexBufferEXT");
6629
6630 if (!texObj ||
6631 !check_texture_buffer_target(ctx, texObj->Target, "glMultiTexBufferEXT", false))
6632 return;
6633
6634 texture_buffer_range(ctx, texObj, internalFormat,
6635 bufObj, 0, buffer ? -1 : 0, "glMultiTexBufferEXT");
6636 }
6637
6638 void GLAPIENTRY
6639 _mesa_TextureBufferRange(GLuint texture, GLenum internalFormat, GLuint buffer,
6640 GLintptr offset, GLsizeiptr size)
6641 {
6642 struct gl_texture_object *texObj;
6643 struct gl_buffer_object *bufObj;
6644
6645 GET_CURRENT_CONTEXT(ctx);
6646
6647 if (buffer) {
6648 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer,
6649 "glTextureBufferRange");
6650 if (!bufObj)
6651 return;
6652
6653 if (!check_texture_buffer_range(ctx, bufObj, offset, size,
6654 "glTextureBufferRange"))
6655 return;
6656
6657 } else {
6658 /* OpenGL 4.5 core spec (02.02.2015) says in Section 8.9 Buffer
6659 * Textures (PDF page 254):
6660 * "If buffer is zero, then any buffer object attached to the buffer
6661 * texture is detached, the values offset and size are ignored and
6662 * the state for offset and size for the buffer texture are reset to
6663 * zero."
6664 */
6665 offset = 0;
6666 size = 0;
6667 bufObj = NULL;
6668 }
6669
6670 /* Get the texture object by Name. */
6671 texObj = _mesa_lookup_texture_err(ctx, texture, "glTextureBufferRange");
6672 if (!texObj)
6673 return;
6674
6675 if (!check_texture_buffer_target(ctx, texObj->Target,
6676 "glTextureBufferRange", true))
6677 return;
6678
6679 texture_buffer_range(ctx, texObj, internalFormat,
6680 bufObj, offset, size, "glTextureBufferRange");
6681 }
6682
6683 GLboolean
6684 _mesa_is_renderable_texture_format(const struct gl_context *ctx,
6685 GLenum internalformat)
6686 {
6687 /* Everything that is allowed for renderbuffers,
6688 * except for a base format of GL_STENCIL_INDEX, unless supported.
6689 */
6690 GLenum baseFormat = _mesa_base_fbo_format(ctx, internalformat);
6691 if (ctx->Extensions.ARB_texture_stencil8)
6692 return baseFormat != 0;
6693 else
6694 return baseFormat != 0 && baseFormat != GL_STENCIL_INDEX;
6695 }
6696
6697
6698 /** GL_ARB_texture_multisample */
6699 static GLboolean
6700 check_multisample_target(GLuint dims, GLenum target, bool dsa)
6701 {
6702 switch(target) {
6703 case GL_TEXTURE_2D_MULTISAMPLE:
6704 return dims == 2;
6705 case GL_PROXY_TEXTURE_2D_MULTISAMPLE:
6706 return dims == 2 && !dsa;
6707 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
6708 return dims == 3;
6709 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY:
6710 return dims == 3 && !dsa;
6711 default:
6712 return GL_FALSE;
6713 }
6714 }
6715
6716
6717 static void
6718 texture_image_multisample(struct gl_context *ctx, GLuint dims,
6719 struct gl_texture_object *texObj,
6720 struct gl_memory_object *memObj,
6721 GLenum target, GLsizei samples,
6722 GLint internalformat, GLsizei width,
6723 GLsizei height, GLsizei depth,
6724 GLboolean fixedsamplelocations,
6725 GLboolean immutable, GLuint64 offset,
6726 const char *func)
6727 {
6728 struct gl_texture_image *texImage;
6729 GLboolean sizeOK, dimensionsOK, samplesOK;
6730 mesa_format texFormat;
6731 GLenum sample_count_error;
6732 bool dsa = strstr(func, "ture") ? true : false;
6733
6734 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE)) {
6735 _mesa_debug(ctx, "%s(target=%s, samples=%d)\n", func,
6736 _mesa_enum_to_string(target), samples);
6737 }
6738
6739 if (!((ctx->Extensions.ARB_texture_multisample
6740 && _mesa_is_desktop_gl(ctx))) && !_mesa_is_gles31(ctx)) {
6741 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(unsupported)", func);
6742 return;
6743 }
6744
6745 if (samples < 1) {
6746 _mesa_error(ctx, GL_INVALID_VALUE, "%s(samples < 1)", func);
6747 return;
6748 }
6749
6750 if (!check_multisample_target(dims, target, dsa)) {
6751 GLenum err = dsa ? GL_INVALID_OPERATION : GL_INVALID_ENUM;
6752 _mesa_error(ctx, err, "%s(target=%s)", func,
6753 _mesa_enum_to_string(target));
6754 return;
6755 }
6756
6757 /* check that the specified internalformat is color/depth/stencil-renderable;
6758 * refer GL3.1 spec 4.4.4
6759 */
6760
6761 if (immutable && !_mesa_is_legal_tex_storage_format(ctx, internalformat)) {
6762 _mesa_error(ctx, GL_INVALID_ENUM,
6763 "%s(internalformat=%s not legal for immutable-format)",
6764 func, _mesa_enum_to_string(internalformat));
6765 return;
6766 }
6767
6768 if (!_mesa_is_renderable_texture_format(ctx, internalformat)) {
6769 /* Page 172 of OpenGL ES 3.1 spec says:
6770 * "An INVALID_ENUM error is generated if sizedinternalformat is not
6771 * color-renderable, depth-renderable, or stencil-renderable (as
6772 * defined in section 9.4).
6773 *
6774 * (Same error is also defined for desktop OpenGL for multisample
6775 * teximage/texstorage functions.)
6776 */
6777 _mesa_error(ctx, GL_INVALID_ENUM, "%s(internalformat=%s)", func,
6778 _mesa_enum_to_string(internalformat));
6779 return;
6780 }
6781
6782 sample_count_error = _mesa_check_sample_count(ctx, target,
6783 internalformat, samples, samples);
6784 samplesOK = sample_count_error == GL_NO_ERROR;
6785
6786 /* Page 254 of OpenGL 4.4 spec says:
6787 * "Proxy arrays for two-dimensional multisample and two-dimensional
6788 * multisample array textures are operated on in the same way when
6789 * TexImage2DMultisample is called with target specified as
6790 * PROXY_TEXTURE_2D_MULTISAMPLE, or TexImage3DMultisample is called
6791 * with target specified as PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY.
6792 * However, if samples is not supported, then no error is generated.
6793 */
6794 if (!samplesOK && !_mesa_is_proxy_texture(target)) {
6795 _mesa_error(ctx, sample_count_error, "%s(samples=%d)", func, samples);
6796 return;
6797 }
6798
6799 if (immutable && (!texObj || (texObj->Name == 0))) {
6800 _mesa_error(ctx, GL_INVALID_OPERATION,
6801 "%s(texture object 0)",
6802 func);
6803 return;
6804 }
6805
6806 texImage = _mesa_get_tex_image(ctx, texObj, 0, 0);
6807
6808 if (texImage == NULL) {
6809 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s()", func);
6810 return;
6811 }
6812
6813 texFormat = _mesa_choose_texture_format(ctx, texObj, target, 0,
6814 internalformat, GL_NONE, GL_NONE);
6815 assert(texFormat != MESA_FORMAT_NONE);
6816
6817 dimensionsOK = _mesa_legal_texture_dimensions(ctx, target, 0,
6818 width, height, depth, 0);
6819
6820 sizeOK = ctx->Driver.TestProxyTexImage(ctx, target, 0, 0, texFormat,
6821 samples, width, height, depth);
6822
6823 if (_mesa_is_proxy_texture(target)) {
6824 if (samplesOK && dimensionsOK && sizeOK) {
6825 _mesa_init_teximage_fields_ms(ctx, texImage, width, height, depth, 0,
6826 internalformat, texFormat,
6827 samples, fixedsamplelocations);
6828 }
6829 else {
6830 /* clear all image fields */
6831 clear_teximage_fields(texImage);
6832 }
6833 }
6834 else {
6835 if (!dimensionsOK) {
6836 _mesa_error(ctx, GL_INVALID_VALUE,
6837 "%s(invalid width=%d or height=%d)", func, width, height);
6838 return;
6839 }
6840
6841 if (!sizeOK) {
6842 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s(texture too large)", func);
6843 return;
6844 }
6845
6846 /* Check if texObj->Immutable is set */
6847 if (texObj->Immutable) {
6848 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(immutable)", func);
6849 return;
6850 }
6851
6852 ctx->Driver.FreeTextureImageBuffer(ctx, texImage);
6853
6854 _mesa_init_teximage_fields_ms(ctx, texImage, width, height, depth, 0,
6855 internalformat, texFormat,
6856 samples, fixedsamplelocations);
6857
6858 if (width > 0 && height > 0 && depth > 0) {
6859 if (memObj) {
6860 if (!ctx->Driver.SetTextureStorageForMemoryObject(ctx, texObj,
6861 memObj, 1, width,
6862 height, depth,
6863 offset)) {
6864
6865 _mesa_init_teximage_fields(ctx, texImage, 0, 0, 0, 0,
6866 internalformat, texFormat);
6867 }
6868 } else {
6869 if (!ctx->Driver.AllocTextureStorage(ctx, texObj, 1,
6870 width, height, depth)) {
6871 /* tidy up the texture image state. strictly speaking,
6872 * we're allowed to just leave this in whatever state we
6873 * like, but being tidy is good.
6874 */
6875 _mesa_init_teximage_fields(ctx, texImage, 0, 0, 0, 0,
6876 internalformat, texFormat);
6877 }
6878 }
6879 }
6880
6881 texObj->Immutable |= immutable;
6882
6883 if (immutable) {
6884 _mesa_set_texture_view_state(ctx, texObj, target, 1);
6885 }
6886
6887 _mesa_update_fbo_texture(ctx, texObj, 0, 0);
6888 }
6889 }
6890
6891
6892 void GLAPIENTRY
6893 _mesa_TexImage2DMultisample(GLenum target, GLsizei samples,
6894 GLenum internalformat, GLsizei width,
6895 GLsizei height, GLboolean fixedsamplelocations)
6896 {
6897 struct gl_texture_object *texObj;
6898 GET_CURRENT_CONTEXT(ctx);
6899
6900 texObj = _mesa_get_current_tex_object(ctx, target);
6901 if (!texObj)
6902 return;
6903
6904 texture_image_multisample(ctx, 2, texObj, NULL, target, samples,
6905 internalformat, width, height, 1,
6906 fixedsamplelocations, GL_FALSE, 0,
6907 "glTexImage2DMultisample");
6908 }
6909
6910
6911 void GLAPIENTRY
6912 _mesa_TexImage3DMultisample(GLenum target, GLsizei samples,
6913 GLenum internalformat, GLsizei width,
6914 GLsizei height, GLsizei depth,
6915 GLboolean fixedsamplelocations)
6916 {
6917 struct gl_texture_object *texObj;
6918 GET_CURRENT_CONTEXT(ctx);
6919
6920 texObj = _mesa_get_current_tex_object(ctx, target);
6921 if (!texObj)
6922 return;
6923
6924 texture_image_multisample(ctx, 3, texObj, NULL, target, samples,
6925 internalformat, width, height, depth,
6926 fixedsamplelocations, GL_FALSE, 0,
6927 "glTexImage3DMultisample");
6928 }
6929
6930 static bool
6931 valid_texstorage_ms_parameters(GLsizei width, GLsizei height, GLsizei depth,
6932 unsigned dims)
6933 {
6934 GET_CURRENT_CONTEXT(ctx);
6935
6936 if (!_mesa_valid_tex_storage_dim(width, height, depth)) {
6937 _mesa_error(ctx, GL_INVALID_VALUE,
6938 "glTexStorage%uDMultisample(width=%d,height=%d,depth=%d)",
6939 dims, width, height, depth);
6940 return false;
6941 }
6942 return true;
6943 }
6944
6945 void GLAPIENTRY
6946 _mesa_TexStorage2DMultisample(GLenum target, GLsizei samples,
6947 GLenum internalformat, GLsizei width,
6948 GLsizei height, GLboolean fixedsamplelocations)
6949 {
6950 struct gl_texture_object *texObj;
6951 GET_CURRENT_CONTEXT(ctx);
6952
6953 texObj = _mesa_get_current_tex_object(ctx, target);
6954 if (!texObj)
6955 return;
6956
6957 if (!valid_texstorage_ms_parameters(width, height, 1, 2))
6958 return;
6959
6960 texture_image_multisample(ctx, 2, texObj, NULL, target, samples,
6961 internalformat, width, height, 1,
6962 fixedsamplelocations, GL_TRUE, 0,
6963 "glTexStorage2DMultisample");
6964 }
6965
6966 void GLAPIENTRY
6967 _mesa_TexStorage3DMultisample(GLenum target, GLsizei samples,
6968 GLenum internalformat, GLsizei width,
6969 GLsizei height, GLsizei depth,
6970 GLboolean fixedsamplelocations)
6971 {
6972 struct gl_texture_object *texObj;
6973 GET_CURRENT_CONTEXT(ctx);
6974
6975 texObj = _mesa_get_current_tex_object(ctx, target);
6976 if (!texObj)
6977 return;
6978
6979 if (!valid_texstorage_ms_parameters(width, height, depth, 3))
6980 return;
6981
6982 texture_image_multisample(ctx, 3, texObj, NULL, target, samples,
6983 internalformat, width, height, depth,
6984 fixedsamplelocations, GL_TRUE, 0,
6985 "glTexStorage3DMultisample");
6986 }
6987
6988 void GLAPIENTRY
6989 _mesa_TextureStorage2DMultisample(GLuint texture, GLsizei samples,
6990 GLenum internalformat, GLsizei width,
6991 GLsizei height,
6992 GLboolean fixedsamplelocations)
6993 {
6994 struct gl_texture_object *texObj;
6995 GET_CURRENT_CONTEXT(ctx);
6996
6997 texObj = _mesa_lookup_texture_err(ctx, texture,
6998 "glTextureStorage2DMultisample");
6999 if (!texObj)
7000 return;
7001
7002 if (!valid_texstorage_ms_parameters(width, height, 1, 2))
7003 return;
7004
7005 texture_image_multisample(ctx, 2, texObj, NULL, texObj->Target,
7006 samples, internalformat, width, height, 1,
7007 fixedsamplelocations, GL_TRUE, 0,
7008 "glTextureStorage2DMultisample");
7009 }
7010
7011 void GLAPIENTRY
7012 _mesa_TextureStorage3DMultisample(GLuint texture, GLsizei samples,
7013 GLenum internalformat, GLsizei width,
7014 GLsizei height, GLsizei depth,
7015 GLboolean fixedsamplelocations)
7016 {
7017 struct gl_texture_object *texObj;
7018 GET_CURRENT_CONTEXT(ctx);
7019
7020 /* Get the texture object by Name. */
7021 texObj = _mesa_lookup_texture_err(ctx, texture,
7022 "glTextureStorage3DMultisample");
7023 if (!texObj)
7024 return;
7025
7026 if (!valid_texstorage_ms_parameters(width, height, depth, 3))
7027 return;
7028
7029 texture_image_multisample(ctx, 3, texObj, NULL, texObj->Target, samples,
7030 internalformat, width, height, depth,
7031 fixedsamplelocations, GL_TRUE, 0,
7032 "glTextureStorage3DMultisample");
7033 }
7034
7035 void GLAPIENTRY
7036 _mesa_TextureStorage2DMultisampleEXT(GLuint texture, GLenum target, GLsizei samples,
7037 GLenum internalformat, GLsizei width,
7038 GLsizei height,
7039 GLboolean fixedsamplelocations)
7040 {
7041 struct gl_texture_object *texObj;
7042 GET_CURRENT_CONTEXT(ctx);
7043
7044 texObj = lookup_texture_ext_dsa(ctx, target, texture,
7045 "glTextureStorage2DMultisampleEXT");
7046 if (!texObj)
7047 return;
7048
7049 if (!valid_texstorage_ms_parameters(width, height, 1, 2))
7050 return;
7051
7052 texture_image_multisample(ctx, 2, texObj, NULL, texObj->Target,
7053 samples, internalformat, width, height, 1,
7054 fixedsamplelocations, GL_TRUE, 0,
7055 "glTextureStorage2DMultisampleEXT");
7056 }
7057
7058 void GLAPIENTRY
7059 _mesa_TextureStorage3DMultisampleEXT(GLuint texture, GLenum target, GLsizei samples,
7060 GLenum internalformat, GLsizei width,
7061 GLsizei height, GLsizei depth,
7062 GLboolean fixedsamplelocations)
7063 {
7064 struct gl_texture_object *texObj;
7065 GET_CURRENT_CONTEXT(ctx);
7066
7067 texObj = lookup_texture_ext_dsa(ctx, target, texture,
7068 "glTextureStorage3DMultisampleEXT");
7069 if (!texObj)
7070 return;
7071
7072 if (!valid_texstorage_ms_parameters(width, height, depth, 3))
7073 return;
7074
7075 texture_image_multisample(ctx, 3, texObj, NULL, texObj->Target, samples,
7076 internalformat, width, height, depth,
7077 fixedsamplelocations, GL_TRUE, 0,
7078 "glTextureStorage3DMultisampleEXT");
7079 }
7080
7081 void
7082 _mesa_texture_storage_ms_memory(struct gl_context *ctx, GLuint dims,
7083 struct gl_texture_object *texObj,
7084 struct gl_memory_object *memObj,
7085 GLenum target, GLsizei samples,
7086 GLenum internalFormat, GLsizei width,
7087 GLsizei height, GLsizei depth,
7088 GLboolean fixedSampleLocations,
7089 GLuint64 offset, const char* func)
7090 {
7091 assert(memObj);
7092
7093 texture_image_multisample(ctx, dims, texObj, memObj, target, samples,
7094 internalFormat, width, height, depth,
7095 fixedSampleLocations, GL_TRUE, offset,
7096 func);
7097 }