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